Repository navigation
feat(crew-log): bill subagent and background credits in the usage fold - #14966
Conversation
|
Intent: make the crew log the durable record of what a session's subagents and background helpers cost, so |
Design Review (Fable 5) — ✅ PASSDesign-level review of Design-Verdict: PASS Verify stage 2 is tracked: [DESIGN-REVIEWED] 7a8bcd5 Description read[DESCRIPTION-READ] de90e5418d316d07023f9c74c2f10ba95c80047522800ee9c08bc13a6d79c915 That sha256 names everything this verdict read: the PR description, plus the 1 evidence file(s) the workflow collected from it and handed this review. It covers that evidence because the media strip replaces every attachment URL with the same placeholder, so an edit swapping one attachment for another would leave the description's captured text identical while what this review judged changed. Recompute it with |
GPT 5.6 Review — ✅ no blocking findingsGPT 5.6 completed its review of This comment is updated in place on each push. Review detailsNo findings. False positive or not applicable? A repository writer can comment: |
First Principles Review (Fable 5) — 🟡 CONCERNSPremise-level review of First-Principles-Verdict: CONCERNS
Not justified as shipped
What this change shipsInventory (8 items) — 7 justifiedIntent: make the crew log's
The rewritten "nothing measures either" pin is reversed with evidence: [FIRST-PRINCIPLES-REVIEWED] 7a8bcd5 Description read[DESCRIPTION-READ] 5e9e1df428dc3af770d11f0ace9cd9f2a3e4cf44eac6c04fd4130d7ae9584915 That sha256 names the PR description this verdict read. Recompute it with |
Opus 5 Review — ✅ no blocking findingsReviewed Review detailsAdvisory only — nothing blocks; FINDING — src/kiro_crew/crew_log/projection.py:1472 — [OPUS-REVIEWED] 7a8bcd5 Verdict parsed from the review's SHA-scoped output markers for commit False positive or not applicable? A repository writer can comment: |
52edec6 to
477c670
Compare
|
self-added: yes
|
|
|
477c670 to
9e03b28
Compare
|
self-added: yes
|
A child's spend was measured but never written down: the subagent closers carried no `credits`, and the `usage` fold billed turns alone, so a session that spent most of its budget on a wave of children read as cheap. The credits `background/completed` already carried were folded by nothing. Both closers now carry `credits`, written only when positive so absent keeps meaning unmetered. `usage` bills all three spenders into one total and keeps `credits_by_source` beside it. `FOLD_STATE_VERSION` moves to 4, retiring every savepoint written against the turn-only state. `_timeline_step` also copies `ms`, the spelling the subagent closers and steps use for a measured duration, so a child's run time reaches the timeline at all.
9e03b28 to
7a8bcd5
Compare
|
self-added: yes
|
|
|
|
Problem / Motivation
Goal: the crew log records what a session's subagents cost, so the
usagefold answers "what did this session spend" instead of "what did its own turns spend".A subagent's credits are measured. #14913 wired
SubagentInfo.creditsand the card shows it. But that number never reaches the crew log:subagent/completedandsubagent/failedcarry nocreditsfield, and the entry type's own note still says the runtime cannot measure one. So the log of a session that dispatched twenty children says nothing about what those children cost.The
usagefold has the matching hole. It bills four entry types, all turn-shaped.background/completedhas carried acreditsfield all along and no fold reads it. A session that spent most of its budget on a wave of subagents reads as cheap.A third, smaller one:
_timeline_stepcopiesduration_ms, which is the turn entry's spelling. The subagent closers and steps call theirsms, so a child's run duration never reached the timeline at all.Why it matters
The crew log is the durable record. The card's number lives in runtime memory, goes out over a WebSocket frame, and is gone when the process ends -- so "what did last Tuesday's session cost" has no answer, and the panel's own credits stat under-reports every session that used subagents. Anyone reading the log to attribute spend gets a number that is quietly wrong rather than absent.
Not a goal
subagentsfold. This adds the number and the buckets; a fold that answers "which children ran" is separate.CrewLogPaneldraws them is a separate decision.What changed (motivation → approach → change)
subagent/completed,subagent/failedcreditsfieldcredits, written only when positiveusage.creditsusage.credits_by_sourceturn/subagent/background, each withcreditsandreportedtimelinemomentsduration_msmstooFOLD_STATE_VERSIONThe measurement existed and the write did not, so the write is what this adds.
subagent_manager/terminal.pyis the one place a child closes, and it already readsinfo.elapsedthere; it now readsinfo.creditsbeside it and hands it over as measured.crew_log/entry_types.pydeclares the two new fields andcrew_log/emit.pywrites them.Both emitters drop a zero. A provider that does not bill in credits reports
0.0through the sharedTurnUsagecontract, which at this seam looks exactly like a run that was free. Writing the zero would state a measurement nobody made, so absent keeps meaning unmetered -- the posturebackground/completedalready takes._bill_creditsstates one invariant, about the RESULT of the addition rather than the charge: a spend total stays finite, and it never goes down. A charge is billed only if the total and its bucket still hold that afterwards.Not a list of bad inputs, deliberately.
NaN, each infinity, a negative, and two finite values that overflow on the way up are four inputs with one consequence, and that consequence is the part nothing can undo:roundkeeps a non-finite total non-finite and the savepoint persists it. Zero passes, because it does not move the total and a provider reporting0.0measured zero.Two things sit beside it.
float(credits)is exception-safe, since a JSON integer is unbounded andOverflowErroris caught nowhere between_usage_stepandfold_session. Andby_modelneeds no check: every billed charge is non-negative, so a model's row is a sub-sum of a total already proved finite.The subagent emitters also refuse a non-finite charge on the way in, guarding a different property --
jsonwritesinfasInfinity, which is not valid strict JSON, so the writer protects the log file's parseability and the fold protects the total.In
crew_log/projection.py, one function,_bill_credits, adds every charge to the session total and to its own source bucket. Both in one place is the point: a bucket incremented where the total is not would make the two disagree, and a reader could not tell which half was wrong.turns.credits_reportedandby_modelstay turn-scoped -- a child's closer names no model, so charging the parent turn's model for a child's work would attribute one model's spend to another.usagenow stores a bucket per source, soFOLD_STATE_VERSIONmoves to 4. That retires every savepoint written against the turn-only state; each affected session pays one cold refold, which is the expected cost and the only in-product way to retire them.The three system specs state the same shape:
crew-log-emitter.mdon why the field is written and why a zero is not,crew-log-projection.mdon the closed bucket set, andinjected-messages.mdon the completion message and the entry reading one accumulator.Backwards compatibility
Compatible: both new
creditsfields are optional additions to existing entry types, an old entry without one reads exactly as before (as unmetered, which is what it was),usagegains a key and no consumer loses one, and old savepoints are refused rather than misread, which is what theFOLD_STATE_VERSIONbump is for.One interim mismatch, named here rather than hidden.
usage.creditsnow covers three spenders whileturns.credits_reportedbeside it stays turn-scoped, andCrewLogPanelpairs the two:measured(value.credits, turns.credits_reported)atCrewLogPanel.tsx:338and the collapsedsummary_usageline both render--when the count is zero. So a session whose turn closers reported no cost, but whose child or title call did, shows a dash where the fold holds a real number. Reaching it takes a synthesized turn closer (a crash-repair close) or a session billed only for a background call; the fold's number stays correct and readable throughGET /api/sessions/{id}/crew-log/projection/usage. Gating and labelling that stat on a session-wide count is stage 2's work, in the same PR that gives the panel itssubagentssection -- doing it here would widen this PR intoCrewLogPanel.tsxplus 13 locale files and a surface another stage owns.Tests
test_crew_log_emit.py-- a completed child recordscredits; a child that billed nothing writes nocreditskey; both again forfailedandstopped; a non-finite charge is written for no outcome.test_crew_log_projection.py-- turn 2.0 + subagent 1.0 + background 0.5 folds to a total of 3.5 with each bucket right; a stopped child bills thesubagentbucket; a closer with nocreditsleaves that bucket at zero withreported: 0; a child'smsreaches the timeline.NaN,inf,-inf,-0.5and-1e9are each driven through all four writers -- both child closers, a background helper, and the turn path that predates the buckets -- and must leave the total and every bucket untouched. A10 ** 400charge must skip its own line and not fail the fold. Two1e308charges must leave the total finite at1e308with the second refused, which is the case no input check can see. An explicit0.0must stay counted as reported.test_crew_log_checkpoint.py-- a savepoint written under the previousFOLD_STATE_VERSIONis refused and the fold runs cold. The digest fixture also grew the two non-turn spenders, so the state pin covers the new buckets rather than only the turn path.test_crew_log_resolver.py-- the existing closer-ordering test's stand-inSubagentInfogainedcredits, which the closer now reads.Every test above was watched fail before its fix existed. Six mutations were each caught: relaxing the drop-zero gate to
>= 0on both emitters, removingmsfrom the timeline key list, routing subagent charges into theturnbucket, and deleting either half of the invariant on its own. TheFOLD_STATE_VERSIONobligation is enforced by the existing state-digest pin, which went red on the state change before the bump.One result is worth stating because it is what shows the invariant replaced the earlier guards rather than joining them: the
NaN,inf,-infand negative cases still pass with every per-input magnitude check deleted from the function. They are caught by the finite-and-never-decreasing test on the result.Manual verification
N/A -- unit coverage sufficient. Every changed path is a pure function over log entries or a single emitter call, and each is driven directly by a test.
Related Issues
no linked issue: stage 1 of a three-stage plan; the later stages move the card onto the fold.
Checklist