Skip to content

A turn's cost counts every call it made, the plane's row follows it, and compacting stops when it no longer helps - #1265

Merged
misterjohannesson merged 3 commits into
development-2026-11-28-2from
fix/1050-turn-cost
Nov 28, 2026
Merged

misterjohannesson merged 3 commits into
development-2026-11-28-2from
fix/1050-turn-cost

Conversation

@misterjohannesson

Copy link
Copy Markdown
Contributor

Fixes #1050 (entries D72, D53, D122; D92 measured, with a proposal below).

What was there

  • D72. A summary that failed (refused, timed out, stream crashed) was written nowhere for a threshold or overflow compaction, so a stopped summariser's usage was counted live only and a replay, the listing and the ledger missed it. Session.Summary.fold counted llm_response only, so its totals disagreed with the listing, which also counts the summariser's call on compacted and a stopped call's stopped.
  • D53. A pod reported session.status on lifecycle changes only. A subagent's calls move the cost while the root stands still on its delegate, so the plane's row kept the cost from before them until the root moved.
  • D122. compacted had no thinking_resent / prefix_mismatch, so Troupe.Bench.Prefix.count/1 missed a summariser call sent again. With a context window close to the base prompt's size, what a compaction left was still over the threshold and the agent compacted before every call, with nothing to stop it. llm_request.prompt_bytes.brief was 0 under the stable system prompt.

What changed

  • D72. A failed summary is written as a new event, compaction_failed: reason (threshold, context_overflow, manual, as compacted has it), error (a sentence), command_id, model, prompt_bytes, and stopped in llm_error's words when the agent stopped the call, or when the provider had reported usage before a refusal or a crash. A new event and not llm_error with a field, because clients and the plane read llm_error as the turn ending on a failed call (the desktop's rest state, A2A's failed), and this turn goes on. A manual compaction still writes its llm_error after it, as before. The agent's replay, Spend.of, Log.Fold, Session.Usage (the ledger), the index's dormant totals, the live bench and Session.Summary count its stopped. Session.Summary.fold now counts compacted usage and stopped on llm_error, cancelled and compaction_failed. The terminal and @troupe/client count its usage and show one line for a compaction the harness started ("context not compacted: …; the conversation goes on as it was"). For one a person asked for, the llm_error already says it. Schema regenerated; PROTOCOL.md says all of this.
  • D53. The worker's manager watches the events a call's cost is folded from (any agent's llm_response, compacted with usage, and a stopped on llm_error, cancelled or compaction_failed). The first one starts a 5 s timer, and when it fires the manager reports status if the fields moved. So a busy session reports its cost at most once every five seconds, and lifecycle reports still carry the cost as before.
  • D122.
    • compacted carries thinking_resent, thinking_dropped and prefix_mismatch, as llm_response does, and Bench.Prefix.count/1 counts them.
    • The loop guard: the first call after a compaction shows whether it helped. If its prompt is still over the threshold, compacting is held until a prompt is under it again. The person is told in one sentence, as a config_note (key compaction, with model and limit), the same shape the harness already uses for what the configuration did to the session: "Compaction is paused: the prompt was still N tokens right after compacting, over the T at which compacts, so the agent goes on without compacting until a prompt is under that again." A replay folds the hold back from that note and lets it go at an llm_response under the threshold. Log.Fold witnesses the note. A provider refusing a prompt as too long is still handled as before, and a manual compaction still runs.
    • prompt_bytes.brief under the stable system prompt is the brief's size once the turn context has put it into the conversation, until a compaction rewrites the conversation; before that it is 0.
    • The issue lists three D122 bullets; all three are done.
  • The scripted model gains two steps for tests: {:input_tokens, n, step} and {:thinking_resent, mismatch, step}.

D92: measured, not changed

Decision 793 is not changed here.

  • On the fake, a 15-call turn with no task list on a model that does not bind its thinking (fake-model): llm_request.tools_changed is true once, at call 11, when todo_write / todo_read join the tools. The same turn on claude-opus-5-5, which binds its thinking: 0 changes, with the todo tools there from call 1.
  • In the recordings under test/fixtures/ (nine logs with the field, 33 compared requests): one tools_changed: true, in checkpoints/manual.jsonl at its third call, where a list had been written and the tools joined.

So the cost is one full cache write of the prompt prefix per long turn with no list, plus one after a list first exists.

Proposal for the next chunk. Offer todo_write and todo_read from the first call on every model, as 867 does for models that bind. Keep 793's point, fewer list calls on small tasks for models like qwen3-235b, by enforcing it in the tool instead of in the tool set: before ten calls, a todo_write on a turn with no list answers with a refusal sentence ("a task list is for work longer than this turn has run; carry on without one"), and that call costs no extra round trip. The tools then stay the same for the whole session on every provider, and the prefix stays cacheable. The cost is the two definitions (about 1 kB) on every call, which a cached prefix serves at the read price. This would take a decision that supersedes 793's gate; TaskListCallsTest would need to show follow_steps still within nine calls.

Verified

Each new or changed test fails before the change (run against the lib files set aside) and passes after:

  • core: CompactionTest (thinking_resent on compacted; window close to the prompt holds after one compaction and says so in one sentence; lets go once a prompt is under the threshold; a refused summary is written with no stopped), StoppedCallTest (a stopped summary is written with stopped, Session.Summary agrees with Spend.of, the ledger makes a row, and a restart charges the budget the same), TurnCostTest (brief counted under the stable prompt), SummaryTest (fold counts every call the listing counts).
  • worker: TurnCostStatusTest ("a subagent's calls reach the row while the root waits on its delegation" fails before with no matching session.status within 8000ms).
  • The existing "compacts again and again without a refusal" relied on the loop (the fake reports a 100-token prompt on every call). It now scripts the call after each compaction under the threshold, as a compaction that helps leaves it, and still asserts at least three compactions with no refusal.
  • TUI TurnCostTest and @troupe/client turn-cost.test.ts cover the new event.

Gates (WSL, empty KUBECONFIG):

  • root mix check: core Result: 2019/2031 passed (13/13 doctests, 3/3 properties, 2003/2015 tests). The 12 failures are the known rg-shim set (FileToolsTest grep ×6, ReadOutputTest, ReadRootsTest, PathOnlyLookupTest, BenchTest ×2 on cut_output, DoctorTest bench). Also: 1606 passed, 9 excluded (the apps before core), gateway 437 passed (2 properties, 435 tests), 2 skipped, worker 361 passed, daemon 32 passed, a2a 53 passed.
  • mix credo --strict: no issues. mix format --check-formatted: ok. mix troupe.boundaries: no violations.
  • clients/tui mix check: 991/997 passed (2/2 doctests, 989/995 tests). The failures are BenchCLITest ×2 and DoctorCLITest (the rg shim) and AgentsMendTest ×3 under load; AgentsMendTest and TurnCostTest run alone: 12 passed.
  • clients/gui: typecheck, build, tokens, icons and lines all pass; tests: client 527 pass, 0 fail, console 409 passed, desktop 552 passed, 2 skipped.
  • mix troupe.schema.diff: schema unchanged: 230 documents, after troupe.schema.gen (new events/compaction_failed.json, compacted.json and index.json updated).
  • decision-citations.exs --base origin/development-2026-11-28-2: none adds a decision number to code. Citations in the comment blocks I touched were taken out.
  • doc-size.exs, doc-links.exs: hold.
  • After merging the chunk branch (An MCP session is held for as long as its caller needs it and ended when it is done #1046, A TUI's plane token hand-over in flight during troupe logout leaves the daemon holding the token #1245, Anthropic's cache, output and beta fields reach the model through an Anthropic-speaking gateway #1048), run again: core Result: 2029/2041 passed (13/13 doctests, 3/3 properties, 2013/2025 tests), the same 12 rg-shim failures; worker Result: 361 passed; schema unchanged: 230 documents.

Decisions

Read 788, 793, 769, 815, 863, 867, 774, 1029. Took none: 788's stopped call is now also written for a summary, as 788 meant ("at its timeout in a turn or in a compaction"), and 793 is unchanged. 769's brief now counts the brief where a stable prompt puts it; the schema and PROTOCOL.md say so.

Left

  • Session.Checkpoint facts do not count a stopped on llm_error or cancelled (nor the new compaction_failed) in their spend. That was already true before this change and is out of A turn's cost counts every call it made and its prompt prefix stays cacheable #1050's files. Worth its own issue if checkpoint spend feeds a total.
  • A turn's own llm_error after a stream that had reported usage (not a timeout) is still not counted in thinking. Only the summariser's case is fixed here.

… no longer helps

A summary that fails (refused, timed out, its stream crashed) is written as
compaction_failed, with the call as stopped, so a replay, the listing, the
ledger and the session's summary count it; Session.Summary.fold counts the
summariser's call on compacted and a stopped call on llm_error, cancelled and
compaction_failed, so its totals agree with the listing.

A pod reports what a session has cost as calls are folded, at most once every
five seconds, so the plane's row says a subagent's calls while its root waits.

compacted carries thinking_resent, thinking_dropped and prefix_mismatch, which
Bench.Prefix counts. A compaction that leaves the prompt over the threshold
holds compacting until a prompt is under it again, said in a
config_note when it starts to, rather than compacting before every call. prompt_bytes.brief is
the brief's size under a stable system prompt too, where it is part of the
conversation.

Refs #1050

Signed-off-by: Martin Johannesson <martin.johannesson@it-minds.dk>
…UI formats

Signed-off-by: Martin Johannesson <martin.johannesson@it-minds.dk>
…ix/1050-turn-cost

Signed-off-by: Martin Johannesson <martin.johannesson@it-minds.dk>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant