Python
Carry reviewer cache tokens into the metering badge
Devika Verma36656407ee40
diff --git a/coworker/audit.py b/coworker/audit.pyindex d05a6976..5199a7b7 100644--- a/coworker/audit.py+++ b/coworker/audit.py@@ -47,7 +47,9 @@ class AuditStore:resource TEXT,call_id TEXT,tokens_in INTEGER DEFAULT 0,- tokens_out INTEGER DEFAULT 0+ tokens_out INTEGER DEFAULT 0,+ cache_read INTEGER DEFAULT 0,+ cache_write INTEGER DEFAULT 0)""")# Existing databases predate the reviewer columns (2026-08-12): call_id joins a@@ -58,6 +60,13 @@ class AuditStore:("call_id", "TEXT"),("tokens_in", "INTEGER DEFAULT 0"),("tokens_out", "INTEGER DEFAULT 0"),+ # Cached-prefix share of a reviewer check (2026-08-22). Without these the+ # metering badge could only ever see the FRESH tokens — ~75 of a ~1,500-token+ # check once the provider caches the instruction prefix — so it under-reported+ # cost by more the longer a session ran. Same defect class as OPE-101, one+ # layer further out.+ ("cache_read", "INTEGER DEFAULT 0"),+ ("cache_write", "INTEGER DEFAULT 0"),):try:self._conn.execute(@@ -78,8 +87,8 @@ class AuditStore:self._conn.execute("""INSERT INTO audit_events- (session_id, agent, workspace, connector, tool, stage, status, approval, args, result_preview, reason, resource, call_id, tokens_in, tokens_out)- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)+ (session_id, agent, workspace, connector, tool, stage, status, approval, args, result_preview, reason, resource, call_id, tokens_in, tokens_out, cache_read, cache_write)+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)""",(event.get("session_id") or "",@@ -97,6 +106,8 @@ class AuditStore:str(event.get("call_id") or ""),int(event.get("tokens_in") or 0),int(event.get("tokens_out") or 0),+ int(event.get("cache_read") or 0),+ int(event.get("cache_write") or 0),),)self._conn.commit()@@ -112,14 +123,19 @@ class AuditStore:"""SELECT status, COUNT(*) AS n,COALESCE(SUM(tokens_in), 0) AS tin,- COALESCE(SUM(tokens_out), 0) AS tout+ COALESCE(SUM(tokens_out), 0) AS tout,+ COALESCE(SUM(cache_read), 0) AS cread,+ COALESCE(SUM(cache_write), 0) AS cwriteFROM audit_eventsWHERE session_id = ? AND stage = ?GROUP BY status""",(session_id, stage),).fetchall()- out = {"checks": 0, "allow": 0, "deny": 0, "unsure": 0, "tokens_in": 0, "tokens_out": 0}+ out = {+ "checks": 0, "allow": 0, "deny": 0, "unsure": 0,+ "tokens_in": 0, "tokens_out": 0, "cache_read": 0, "cache_write": 0,+ }for row in rows:status = str(row["status"])if status in ("allow", "deny", "unsure"):@@ -127,6 +143,8 @@ class AuditStore:out["checks"] += int(row["n"])out["tokens_in"] += int(row["tin"])out["tokens_out"] += int(row["tout"])+ out["cache_read"] += int(row["cread"])+ out["cache_write"] += int(row["cwrite"])return outreturn {"live": _bucket("reviewer_verdict"), "shadow": _bucket("reviewer_shadow")}diff --git a/coworker/engine.py b/coworker/engine.pyindex 09b65d8c..46b7eba7 100644--- a/coworker/engine.py+++ b/coworker/engine.py@@ -1030,6 +1030,8 @@ class TurnEngine:call_id=tool_call.id,tokens_in=verdict.tokens_in,tokens_out=verdict.tokens_out,+ cache_read=verdict.cache_read,+ cache_write=verdict.cache_write,)except Exception:pass # shadow must never surface a failure@@ -1117,6 +1119,8 @@ class TurnEngine:reason=verdict.reason,tokens_in=verdict.tokens_in,tokens_out=verdict.tokens_out,+ cache_read=verdict.cache_read,+ cache_write=verdict.cache_write,)if verdict.verdict == "allow":allowed = Truediff --git a/surfaces/gui/src/api.ts b/surfaces/gui/src/api.tsindex 9cf13a78..f355faad 100644--- a/surfaces/gui/src/api.ts+++ b/surfaces/gui/src/api.ts@@ -1657,6 +1657,10 @@ export interface ReviewerBucket {unsure: number;tokens_in: number;tokens_out: number;+ // Cached-prefix share of the input, billed at ~10%. Without it the badge only ever+ // showed the FRESH tokens — a fraction of what a check really processes.+ cache_read: number;+ cache_write: number;}export interface ReviewerStats {live: ReviewerBucket; // the mode actually deciding (Mode.AUTO_APPROVE)diff --git a/surfaces/gui/src/components/Composer.tsx b/surfaces/gui/src/components/Composer.tsxindex f0230f0c..26e52da3 100644--- a/surfaces/gui/src/components/Composer.tsx+++ b/surfaces/gui/src/components/Composer.tsx@@ -109,7 +109,7 @@ interface Props {onUnattendedChange?: (on: boolean) => void;// Auto-Approve metering (§1.7): the "Auto-Approve · N checks" badge + mode-menu summary.// Absent/zero hides both; only the LIVE bucket surfaces here (shadow is a Settings concern).- reviewerStats?: { checks: number; allow: number; deny: number; unsure: number; tokens_in: number; tokens_out: number } | null;+ reviewerStats?: { checks: number; allow: number; deny: number; unsure: number; tokens_in: number; tokens_out: number; cache_read?: number; cache_write?: number } | null;approvalSlot?: ReactNode;// Push text + attachments into the composer (e.g. a start-panel task card). The `nonce` makes// repeated identical prefills re-apply; the user can still edit before sending.@@ -882,7 +882,7 @@ function ModeMenu({onModeChange: (mode: string) => void;unattended?: boolean;onUnattendedChange?: (on: boolean) => void;- reviewerStats?: { checks: number; allow: number; deny: number; unsure: number; tokens_in: number; tokens_out: number } | null;+ reviewerStats?: { checks: number; allow: number; deny: number; unsure: number; tokens_in: number; tokens_out: number; cache_read?: number; cache_write?: number } | null;}) {const [open, setOpen] = useState(false);// The Auto-Approve entry is gated on the server flag. Fetch once on first open; a session@@ -962,7 +962,15 @@ function ModeMenu({This session: {reviewerStats.checks} {reviewerStats.checks === 1 ? "check" : "checks"} ·{" "}{reviewerStats.allow} cleared · {reviewerStats.deny} blocked · {reviewerStats.unsure} asked you{reviewerStats.tokens_in + reviewerStats.tokens_out > 0 && (- <> · ~{Math.round((reviewerStats.tokens_in + reviewerStats.tokens_out) / 100) / 10}k tokens</>+ // Fresh and cached shown separately: cached input bills at ~10%, so+ // one blended number would either overstate cost or hide most of the+ // work. Two honest figures beat one misleading one.+ <>+ {" "}· ~{Math.round((reviewerStats.tokens_in + reviewerStats.tokens_out) / 100) / 10}k tokens+ {(reviewerStats.cache_read ?? 0) > 0 && (+ <> (+{Math.round((reviewerStats.cache_read ?? 0) / 100) / 10}k cached)</>+ )}+ </>)}</div></>diff --git a/tests/test_auto_approve.py b/tests/test_auto_approve.pyindex c838dca0..cb69bc29 100644--- a/tests/test_auto_approve.py+++ b/tests/test_auto_approve.py@@ -9,7 +9,7 @@ from __future__ import annotationsimport asyncioimport json-from dataclasses import dataclass+from dataclasses import dataclass, replaceimport pytest@@ -942,3 +942,24 @@ def test_the_reviewer_is_never_asked_about_a_skill_save(tmp_path):# The human decided, and not a single reviewer token was spent asking.assert approvals == ["save_skill"]assert engine.reviewer.asked == []+++def test_verdict_audit_rows_carry_the_verdicts_cache_figures(tmp_path):+ # The Verdict's cache_read/cache_write must survive into the audit row —+ # this is the exact seam where OPE-101's numbers were dropped one layer earlier.+ engine, rows, _approvals = _engine(+ tmp_path,+ [_tool_turn(("run_shell", {"command": "pytest -q"})), AssistantTurn(text="ok", finish_reason="stop")],+ )++ class _CachedVerdictReviewer(_FakeReviewer):+ async def review(self, **kw):+ verdict = await super().review(**kw)+ return replace(verdict, tokens_in=75, tokens_out=60, cache_read=1400, cache_write=25)++ engine.reviewer = _CachedVerdictReviewer({"run_shell": "allow"})+ _run(engine, "run the tests")++ verdict_rows = [r for r in rows if r.get("stage") == "reviewer_verdict"]+ assert verdict_rows and verdict_rows[0]["cache_read"] == 1400+ assert verdict_rows[0]["cache_write"] == 25diff --git a/tests/test_auto_approve_settings.py b/tests/test_auto_approve_settings.pyindex f5dcd4cd..a7a37315 100644--- a/tests/test_auto_approve_settings.py+++ b/tests/test_auto_approve_settings.py@@ -105,6 +105,7 @@ def test_reviewer_stats_aggregates_by_stage(tmp_path):assert stats["live"] == {"checks": 4, "allow": 2, "deny": 1, "unsure": 1,"tokens_in": 380, "tokens_out": 90,+ "cache_read": 0, "cache_write": 0,}assert stats["shadow"]["checks"] == 1 and stats["shadow"]["allow"] == 1store.close()@@ -146,3 +147,77 @@ def test_audit_migration_adds_columns_to_a_legacy_db(tmp_path):def test_reviewer_stats_endpoint(client):empty = client.get("/v1/sessions/nope/reviewer-stats").json()assert empty["live"]["checks"] == 0 and empty["shadow"]["checks"] == 0+++def test_reviewer_stats_carry_the_cached_share(tmp_path):+ # The badge could only ever see FRESH tokens (~75 of a ~1,500-token check once the+ # provider caches the instruction prefix), so it under-reported cost by more the+ # longer a session ran. The cached share now rides every verdict row into the sums.+ from coworker.audit import AuditStore++ store = AuditStore(tmp_path / "audit.db")+ for _ in range(3):+ store.append(+ {+ "session_id": "s1",+ "tool": "run_shell",+ "stage": "reviewer_verdict",+ "status": "allow",+ "tokens_in": 75,+ "tokens_out": 60,+ "cache_read": 1400,+ "cache_write": 0,+ }+ )+ live = store.reviewer_stats("s1")["live"]+ assert live["checks"] == 3+ assert (live["tokens_in"], live["tokens_out"]) == (225, 180)+ assert (live["cache_read"], live["cache_write"]) == (4200, 0)+++def test_existing_databases_gain_the_cache_columns(tmp_path):+ # A database created before 2026-08-22 has no cache columns. Opening it must migrate+ # in place — old rows read as zero, new rows record the real figures.+ import sqlite3++ from coworker.audit import AuditStore++ db = tmp_path / "audit.db"+ con = sqlite3.connect(db)+ con.execute(+ """+ CREATE TABLE audit_events (+ id INTEGER PRIMARY KEY AUTOINCREMENT,+ timestamp TEXT DEFAULT CURRENT_TIMESTAMP,+ session_id TEXT, agent TEXT, workspace TEXT, connector TEXT,+ tool TEXT, stage TEXT, status TEXT, approval TEXT, args TEXT,+ result_preview TEXT, reason TEXT, resource TEXT, call_id TEXT,+ tokens_in INTEGER DEFAULT 0, tokens_out INTEGER DEFAULT 0+ )+ """+ )+ con.execute(+ "INSERT INTO audit_events (session_id, tool, stage, status, tokens_in, tokens_out)"+ " VALUES ('s1', 'run_shell', 'reviewer_verdict', 'allow', 100, 50)"+ )+ con.commit()+ con.close()++ store = AuditStore(db) # migration happens on open+ store.append(+ {+ "session_id": "s1",+ "tool": "run_shell",+ "stage": "reviewer_verdict",+ "status": "allow",+ "tokens_in": 75,+ "tokens_out": 60,+ "cache_read": 1400,+ "cache_write": 25,+ }+ )+ live = store.reviewer_stats("s1")["live"]+ assert live["checks"] == 2+ assert (live["tokens_in"], live["tokens_out"]) == (175, 110)+ # The pre-migration row contributes zero cached, not garbage.+ assert (live["cache_read"], live["cache_write"]) == (1400, 25)