Repository navigation
feat(cognition): measure the prompt prefix a persona's turn keeps, and name the block that broke it (card 5b09111e) - #4456
Merged
Conversation
…d name the block that broke it (card 5b09111e) The M5 (2026-09-27) read 0% KV reuse for over an hour, and the cause (the steps ledger sliding one action per act, ~40% into the prompt) was found by diffing two captured requests by hand. This makes that diff a probe on every generation: delib.prompt.common_prefix with the characters the persona's previous request already held, the total, the share, and the first message that changed, named by its banner. A prompt-order change is then measured on the wire, and it is the gate for moving the ledger (the next change on this card), which Cormac and I agreed to A/B against it. cognition::prompt_prefix keeps the previous request's rendered messages per persona (bounded by the residents) and compares at dispatch in generate_for_workspace. Test: the shared start is counted through the first changed message and that message is named by its banner. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo
Contributor
Author
|
Reviewed at c9211e2 (Cormac). The comparison is right, and my word is APPROVED once lib CI is green. It counts every matched byte up to the first changed message plus that message's shared start, names the change by its banner, and treats the role as part of the prompt. The test pins the sliding-ledger case. Non-blocking, both about what the number can and cannot see:
🤖 Generated with Claude Code |
Contributor
Author
|
APPROVED at c9211e2 (Cormac), with CI 8/8 green. The review above stands, and its two notes are non-blocking. 🤖 Generated with Claude Code |
This was referenced Sep 27, 2026
joelteply
added a commit
that referenced
this pull request
Sep 27, 2026
…st, clears on a failed or untimed one, and calls its gap a candidate (Codex on #4487) Codex: observe advanced the compared content on every attempt, while the token count advanced only on a timed success, so a timed A, an untimed B, then C compared B's content with A's tokens. And role+content is not the whole engine prompt (tool-call metadata, model routing, template, truncation), so "exact" overclaimed. Now: - stage_reuse (before the request moves) records {request id, fingerprint}. The fingerprint is model + system + tool surface + every message serialized whole, tool-call metadata included. - settle_reuse (on the result, same request id) commits {fingerprint, cache_n + prompt_n} in ONE record, only when the engine answered with timings. A failed or untimed attempt, or one that never settled, drops the record, so the next request says nothing rather than pairing wrong. - The gap is kind=candidate: the template and truncation between the fingerprint and the prefilled tokens are not verified, so it is a raw diagnostic, not a causal slot loss. kind=prompt_changed guesses no gap. - #4456's common_prefix probe is untouched. The test adds the timed-A / untimed-B / C sequence and the never-settled case. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01FRQWzgSfo79JtnwZywHKrE
joelteply
added a commit
that referenced
this pull request
Sep 28, 2026
…t's part and the prompt's part (card 9e4d61e8) (#4487) * feat(cognition): each request's lost prompt reuse splits into the slot's part and the prompt's part (card 9e4d61e8) The M5 prefilled 169k tokens an hour at 53% reuse (prefill ~45% of the GPU hour) and the 5090 1.09M an hour at 12% (Fable, 2026-09-27). Low reuse has two causes with different fixes: the prompt itself changed ahead of a stable region (#4456's delib.prompt.common_prefix measures that), or the prompt kept its prefix but the slot no longer held it (evicted, page not restored, a different slot). Nothing split the two. prompt_prefix::observe now records the in-flight request's kept share per persona, and attribute_reuse, called where the engine's timings land, probes delib.prompt.reuse_split {prompt_kept, engine_reused = cache_n/(cache_n+prompt_n), slot_lost = max(0, kept - reused), prompt_changed = 1 - kept}. An engine that reused more than the prompt kept (a prefix shared across minds on one slot) is no slot loss; a provider without timings or a mind's first request says nothing. split_reuse is pure and pinned by a test. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01FRQWzgSfo79JtnwZywHKrE * fix(cognition): the reuse split counts only in the engine's tokens, and the tool surface is part of the prompt (Codex, Fable on #4487) Codex: a share of characters minus a share of tokens invents slot loss wherever prose and code tokenize differently. So the split no longer compares units. When this prompt extends the persona's previous one WHOLE (the usual multi-act case), everything the engine counted for that previous prompt (cache_n + prompt_n) was reusable, and slot_lost = reusable - cache_n, all in engine tokens: kind=exact. When the prompt changed inside the previous one, no slot figure is guessed (kind=prompt_changed); delib.prompt.common_prefix names the block that moved. Fable: rendered() left out req.tools. Qwen/ChatML templates put the tool block at the end of the system turn, and the surface varies per turn, so a tool-set change read as a kept prompt. The surface is now element 1, between the system prompt and the first message. Tests: the exact, over-served, prompt-changed and first-request cases, and a tool-surface change diverging at index 1. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01FRQWzgSfo79JtnwZywHKrE * fix(cognition): the reuse split commits one record per answered request, clears on a failed or untimed one, and calls its gap a candidate (Codex on #4487) Codex: observe advanced the compared content on every attempt, while the token count advanced only on a timed success, so a timed A, an untimed B, then C compared B's content with A's tokens. And role+content is not the whole engine prompt (tool-call metadata, model routing, template, truncation), so "exact" overclaimed. Now: - stage_reuse (before the request moves) records {request id, fingerprint}. The fingerprint is model + system + tool surface + every message serialized whole, tool-call metadata included. - settle_reuse (on the result, same request id) commits {fingerprint, cache_n + prompt_n} in ONE record, only when the engine answered with timings. A failed or untimed attempt, or one that never settled, drops the record, so the next request says nothing rather than pairing wrong. - The gap is kind=candidate: the template and truncation between the fingerprint and the prefilled tokens are not verified, so it is a raw diagnostic, not a causal slot loss. kind=prompt_changed guesses no gap. - #4456's common_prefix probe is untouched. The test adds the timed-A / untimed-B / C sequence and the never-settled case. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01FRQWzgSfo79JtnwZywHKrE * fix(cognition): a late completion settles only its own staging, never a newer request's (Codex on #4487) Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01FRQWzgSfo79JtnwZywHKrE * fix(cognition): justify the fingerprint's two unwrap_or fallbacks on their lines (Codex on #4487) Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01FRQWzgSfo79JtnwZywHKrE --------- Co-authored-by: Claude Opus 5.5 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Card 5b09111e, step 1: measurement only. No prompt changes.
Why: the M5 (2026-09-27) read 0% KV reuse for over an hour. The cause was found by diffing two captured requests by hand: the steps ledger slides by one action per act, about 40% of the way into the prompt.
The change: every generation now emits a
delib.prompt.common_prefixprobe. It carries:A change to prompt order is then measured on the wire. The ledger move, the next change on this card, will be A/B'd against this probe, as Cormac and I agreed.
cognition::prompt_prefixkeeps each persona's previous rendered messages in memory, bounded by the number of residents. The comparison runs at dispatch ingenerate_for_workspace.Test:
the_first_changed_block_is_named_and_the_shared_start_is_counted. It counts the shared start through the first changed message and names that message by its banner.🤖 Generated with Claude Code
https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo