count cache tokens in telemetry, budget and report

This commit is contained in:
hermes committed 2026-11-13 18:29:37 +00:00
1 parent 67a5bdedb9
commit 8d3af8b629
7 files changed
+90 -13

No files matched your search

+7 -2
View File
@@ -51,6 +51,7 @@ def check(p, st, reason=None):
profile("p", 'gate = "test -f out.txt || { echo MARKER-GATE-OUT; exit 3; }"\n')
s = check(run("ok", body="WRITE"), "done")
assert s["gate_exit"] == 0 and s["input_tokens"] == 100 and s["session"] == "/s.jsonl", s
assert (s["cache_read_tokens"], s["cache_write_tokens"]) == (1000, 10), s
assert "out.txt" in subprocess.run(["git", "ls-tree", "--name-only", s["branch"]], cwd=repo, capture_output=True, text=True).stdout
s = check(run("nowrite", body="NEVERFIX", env={"MODE": "NEVER"}), "needs-human", "gate-failed")
assert s["attempts"] == 3 and s["gate_exit"] != 0, s # 1 + 2 retries
@@ -73,8 +74,10 @@ profile("t", "[budget]\nwall_secs = 1\n")
check(run("slow", "t", body="HANG"), "failed", "timeout")
profile("m", "[budget]\nmax_turns = 2\n")
check(run("turns", "m", body="TURNS=5"), "failed", "budget:turns")
profile("k", "[budget]\nmax_tokens = 200\n")
check(run("tokens", "k", body="TURNS=5"), "failed", "budget:tokens")
# 1160 tokens per turn incl. cache (fresh-only counting would be 150/turn and never reach 2000 in 5 turns)
profile("k", "[budget]\nmax_tokens = 2000\n")
s = check(run("tokens", "k", body="TURNS=5"), "failed", "budget:tokens")
assert s["turns"] == 2 and s["spent_tokens"] == 2320, s
# yolo without sandbox is refused at load time
profile("y", "yolo = true\n")
@@ -274,11 +277,13 @@ recs = [json.loads(l) for l in tel.read_text().splitlines() if l.strip()]
assert len(recs) >= 30, len(recs)
ok_rec = next(r for r in recs if r["task"] == "ok")
assert (ok_rec["status"], ok_rec["profile"], ok_rec["hx_version"], ok_rec["input_tokens"], ok_rec["attempts"]) == ("done", "p", "hx 9.9.9", 100, 1), ok_rec
assert (ok_rec["cache_read_tokens"], ok_rec["cache_write_tokens"]) == (1000, 10), ok_rec
assert next(r for r in recs if r["task"] == "nowrite")["status"] == "needs-human"
assert next(r for r in recs if r["task"] == "nowrite")["attempts"] == 3
assert any(r["status"] == "requeued" for r in recs)
rep = subprocess.run([orch, "report"], env=env, capture_output=True, text=True).stdout
assert "hx 9.9.9" in rep and "profile" in rep and rep.splitlines()[-1].startswith("retries"), rep
assert "cache" in rep.splitlines()[0], rep
allrow = next(l for l in rep.splitlines() if l.startswith("all"))
assert allrow.split()[1] == str(len([r for r in recs])), (allrow, len(recs))
# a half-written line (crash) is skipped and does not hide the next record
+2 -2
View File
@@ -53,9 +53,9 @@ if "QUOTA" in body:
e({"type": "stream", "event": {"type": "quota", "windows": [{"label": "5h", "used_pct": 100.0, "resets_at": int(time.time()) + 3600}], "credits": None}})
for i in range(int(body.split("TURNS=")[1].split()[0]) if "TURNS=" in body else 1):
e({"type": "turn_start"})
e({"type": "stream", "event": {"type": "done", "usage": {"input_tokens": 100, "output_tokens": 50}}})
e({"type": "stream", "event": {"type": "done", "usage": {"input_tokens": 100, "output_tokens": 50, "cache_read_tokens": 1000, "cache_write_tokens": 10}}})
if "WRITE" in body: open("out.txt", "w").write("x")
if "--session" in sys.argv:
open("retry-prompt.txt", "w").write(body)
if "RETRYFIX" in os.environ.get("MODE", "RETRYFIX"): open("out.txt", "w").write("x")
e({"type": "done", "error": "api 429: slow down" if "RATELIMIT" in body else "boom" if "FAIL" in body else None, "usage": {"input_tokens": 100, "output_tokens": 50}, "session": "/s.jsonl"})
e({"type": "done", "error": "api 429: slow down" if "RATELIMIT" in body else "boom" if "FAIL" in body else None, "usage": {"input_tokens": 100, "output_tokens": 50, "cache_read_tokens": 1000, "cache_write_tokens": 10}, "session": "/s.jsonl"})