File size: 7,753 Bytes
2bb5af3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | # ruff: noqa: T201
"""A whole book, small enough to run often.
`smoke_full_book.py` is a hundred pages across three units and costs what a
real book costs, so it gets run once before a release and not while chasing a
defect. This one asks for two chapters and about thirty-five pages - every step
of the pipeline runs, in the same order, through the same API the site calls,
and the whole thing finishes in a fraction of the time and the spend.
It goes through HTTP on purpose. Calling the services directly would skip the
gate, the artifact rows, the ownership checks and the cost ledger - which is
most of what a person actually touches.
Usage: .venv/Scripts/python.exe scripts/smoke_small_book.py [--pages 35]
.venv/Scripts/python.exe scripts/smoke_small_book.py --run <id>
"""
from __future__ import annotations
import argparse
import sys
import time
sys.path.insert(0, ".")
# Everything this prints is Arabic, and a redirected stdout on Windows is
# cp1252 - which cannot encode a single letter of it. Without this the script
# dies on its first status line while the run it started carries on unwatched.
for stream in (sys.stdout, sys.stderr):
if hasattr(stream, "reconfigure"):
stream.reconfigure(encoding="utf-8", errors="replace")
from scripts.smoke_local import ( # noqa: E402
ADMIN_EMAIL,
client,
existing_user_id,
token_for,
)
TOPIC = "ุงูุชุณููู ูุงูุฐูุงุก ุงูุงุตุทูุงุนู"
DESCRIPTION = (
"ูุชุงุจ ุนู
ูู ูุดุฑุญ ููู ูุบููุฑ ุงูุฐูุงุก ุงูุงุตุทูุงุนู ุนู
ู ุงูุชุณููู: ู
ู ููู
ุงูุฌู
ููุฑ "
"ูุชูุณูู
ูุ ุฅูู ุชูููุฏ ุงูู
ุญุชูู ูููุงุณ ุฃุซุฑู. ู
ูุฌูู ูู
ู ูุนู
ู ูู ุงูุชุณููู ููุง "
"ุฎูููุฉ ุชูููุฉ ููุ ูููุฏูู
ุงูู
ูุงููู
ุจูุบุฉ ุงูุนู
ู ูุง ุจูุบุฉ ุงูุฎูุงุฑุฒู
ูุงุชุ ููุนุฑุถ "
"ููู ุฃุฏุงุฉ ู
ุง ุชุตูุญ ูู ูู
ุง ูุง ุชุตูุญ."
)
AUDIENCE = "ู
ุฎุชุตู ุงูุชุณููู ูู
ุฏูุฑู ุงูุชุณููู ุงูุฑูู
ู ูู ุงูุดุฑูุงุช ุงูู
ุชูุณุทุฉ"
NOTES = (
"ูุตูุงู ุงุซูุงู ููุท. ุงูุฃูู ุนู ุฃุซุฑ ุงูุฐูุงุก ุงูุงุตุทูุงุนู ูู ููู
ุงูุนู
ูู ูุงุณุชูุฏุงููุ "
"ูุงูุซุงูู ุนู ุชูููุฏ ุงูู
ุญุชูู ุงูุชุณูููู ูููุงุณ ุฃุซุฑู. ุงุฌุนู ูู ูุตู ุซูุงุซุฉ ู
ุญุงูุฑ."
)
MARK = {"running": "..", "succeeded": "ok", "failed": "!!",
"awaiting_review": "??", "skipped": "--", "queued": " "}
def watch(user, run_id: str, until, limit: int, seen: dict[str, str]) -> dict:
"""Print each step as it changes state; return the run when `until` holds."""
begun = time.time()
body: dict = {}
while time.time() - begun < limit:
response = user.get(f"/v1/runs/{run_id}")
if response.status_code != 200:
print(f" ! run unreadable: {response.status_code} {response.text[:160]}")
time.sleep(5)
continue
body = response.json()
for step in body.get("steps", []):
key, state = step["step_key"], step["status"]
if seen.get(key) != state and state != "pending":
minutes = int((time.time() - begun) // 60)
print(f" {MARK.get(state, state)} {key} ({minutes}ุฏ)")
seen[key] = state
if until(body["status"]):
return body
time.sleep(6)
print(f" ! timed out after {limit}s")
return body
def show_failure(body: dict) -> None:
print("\nerror:")
print(str(body.get("error"))[:2000])
for step in body.get("steps", []):
if step["status"] == "failed":
print(f"\nfailed at {step['step_key']}:")
print(str(step.get("error"))[:3000])
def main() -> int:
parser = argparse.ArgumentParser()
parser.add_argument("--pages", type=int, default=35)
parser.add_argument("--timeout", type=int, default=5400)
parser.add_argument("--run", help="watch an existing run instead of starting one")
args = parser.parse_args()
admin_id = existing_user_id(ADMIN_EMAIL)
if admin_id is None:
print("no bootstrap admin in the database; sign in once first")
return 2
user = client(token_for(ADMIN_EMAIL, admin_id))
seen: dict[str, str] = {}
if args.run:
run_id = args.run
project_id = user.get(f"/v1/runs/{run_id}").json()["project_id"]
print(f"watching run {run_id}")
else:
project = user.post(
"/v1/projects",
json={"kind": "book", "title": f"{TOPIC} ({args.pages} ุตูุญุฉ)"},
)
if project.status_code != 201:
print("could not create the project:", project.status_code, project.text[:300])
return 1
project_id = project.json()["id"]
started = user.post(
f"/v1/projects/{project_id}/runs",
json={"params": {
"variant": "from_scratch",
"brief": {
"topic": TOPIC,
"units_number": 2,
"course_audience": AUDIENCE,
"course_description": DESCRIPTION,
"notes": NOTES,
"total_pages": args.pages,
},
}},
)
if started.status_code != 202:
print("run refused:", started.status_code, started.text[:400])
return 1
run_id = started.json()["id"]
print(f"project {project_id}\nrun {run_id}\n")
print("== to the gate ==")
body = watch(user, run_id, lambda s: s not in ("queued", "running"), 1800, seen)
if body.get("status") == "awaiting_review":
review = user.get(f"/v1/runs/{run_id}/review").json()
chapters = review.get("blueprint", {}).get("chapters", [])
print(f"\n blueprint: {len(chapters)} chapters")
for chapter in chapters:
sections = chapter.get("sections") or []
print(f" - {chapter.get('chapter_title', '?')} ({len(sections)} ู
ุญุงูุฑ)")
for section in sections:
subs = section.get("subsections") or []
print(f" ยท {section.get('title', '?')} ({len(subs)} ู
ูุงุถูุน)")
print("\n== approved ==")
approved = user.post(f"/v1/runs/{run_id}/approve", json={})
if approved.status_code != 202:
print("approve refused:", approved.status_code, approved.text[:300])
return 1
elif body.get("status") not in ("succeeded", "running", "queued"):
print("did not reach the gate:", body.get("status"))
show_failure(body)
return 1
body = watch(user, run_id, lambda s: s in ("succeeded", "failed", "cancelled"),
args.timeout, seen)
status = body.get("status")
print(f"\nfinal status: {status}")
if status != "succeeded":
show_failure(body)
return 1
artifacts = user.get(f"/v1/projects/{project_id}/artifacts").json().get("items", [])
print(f"\nartifacts: {len(artifacts)}")
for artifact in artifacts:
fetched = user.get(f"/v1/artifacts/{artifact['id']}")
print(f" {artifact['format']:5} {artifact['title'][:46]:48} "
f"{(artifact['size_bytes'] or 0):>9,}b download={fetched.status_code} "
f"{len(fetched.content):>9,}b")
spend = user.get(f"/v1/admin/users/{admin_id}/by-task-type?days=1").json()
book = next((r for r in spend if r["task_type"] == "book"), None)
if book:
print(f"\ncost today on books: ${book['cost_usd']} "
f"({book['total_tokens']:,} tokens)")
else:
print("\n!! no cost recorded for this run - the meter is not seeing the calls")
return 0
if __name__ == "__main__":
sys.exit(main())
|