contiai / apps /api /scripts /smoke_small_book.py
aliroya's picture
ContiAI platform: API and queue worker in one container (part 2)
2bb5af3 verified
Raw History Blame Contribute Delete
7.75 kB
# 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())