Skip to content

feat(cognition): measure the prompt prefix a persona's turn keeps, and name the block that broke it (card 5b09111e) - #4456

Merged
joelteply merged 1 commit into
canaryfrom
feat/measure-the-prompt-prefix-a-turn-keeps
Sep 27, 2026
Merged

joelteply merged 1 commit into
canaryfrom
feat/measure-the-prompt-prefix-a-turn-keeps

Conversation

@joelteply

Copy link
Copy Markdown
Contributor

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_prefix probe. It carries:

  • how many characters of the persona's previous request this one shares from the start;
  • the total length, and that share as a fraction;
  • the first message that changed, named by its banner line.

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_prefix keeps each persona's previous rendered messages in memory, bounded by the number of residents. The comparison runs at dispatch in generate_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

…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
@joelteply

Copy link
Copy Markdown
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:

  1. rendered() leaves out request.tools. On the Qwen-family templates the tool block renders INSIDE the system turn, ahead of everything, so a tool list whose order or content changes between requests (a per-turn tool selection, a HashMap-ordered spec) breaks the KV prefix at byte 0 while this probe reads ~100%. Prepending the tool names in order (or a hash of openai_tools(tools)) as element 0 makes the probe see what the template sees.
  2. It measures per PERSONA, while the KV cache is per SLOT. When another mind's request lands on her slot between two of hers, the probe reads a stable prefix and the engine still misses. It is an upper bound on reuse; read it beside the engine's own cache_n per turn, which is the truth.

🤖 Generated with Claude Code

https://claude.ai/code/session_01FRQWzgSfo79JtnwZywHKrE

@joelteply

Copy link
Copy Markdown
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

https://claude.ai/code/session_01FRQWzgSfo79JtnwZywHKrE

@joelteply
joelteply merged commit 5ef9451 into canary Sep 27, 2026
8 checks passed
@joelteply
joelteply deleted the feat/measure-the-prompt-prefix-a-turn-keeps branch September 27, 2026 12:54
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant