Repository navigation
perf(agent): keep turn context on its own prompt so the cache survives - #419
Merged
Merged
Conversation
This was referenced Nov 7, 2026
Memory recall, the intent posture and the magic-keyword notice were added at request time to whichever user message came last. Three cache breaks followed: - At the next turn the previous prompt was re-sent without its recall (measured 689 -> 18 tokens for that message), so the cached prefix ended right after the system prompt and every earlier turn was processed again. - A recovery note or steering message mid-turn became the latest user message and the recall moved onto it. - The posture and notice were sent on step 1 only, so step 2 re-sent the prompt without them. The three are now joined once per turn and pinned to the turn's prompt by its `created_at` stamp (`Session::pin_prompt_suffix`); `build_request` re-applies each pin to its own message on every request. The transcript still holds only what the user typed, so titles, memory retain, search and exports are unchanged. A pin follows its prompt until compaction removes it, when the suffix is pinned again; a prompt without a stamp falls back to the old per-request append. Token counts include the pins. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
mrnasil
force-pushed
the
perf/stable-turn-context
branch
from
November 7, 2026 13:09
b2ea0e9 to
419ffd2
Compare
Merged
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.
Second of the token-efficiency PRs (after #418).
Problem
Three pieces of model-only text are added for each turn: memory recall (
<recalled-context>), the intent posture and the magic-keyword notice. They were appended at request time to whichever user message came last. That broke the provider's cached prefix in three places:Change
created_at(Session::pin_prompt_suffix).build_requestre-applies each pin to its own message on every request. Each request therefore starts with the exact messages of the previous one, across steps, notes and turns.token_count/token_count_cachedinclude the pinned text, so the compaction threshold and the context meter see what is actually sent.cache_warmno longer appends anything itself;build_requestalready produces the last step's request.Tests
turn_context_keeps_history_byte_stable_across_steps_and_turns: two turns with a failing tool in turn 1. It asserts that request N+1 starts with exactly request N's messages, that the note carries no recall, and that the transcript has no recall text.unstamped_prompt_still_gets_turn_context_on_each_requestcovers the fallback path.retried_first_step_keeps_suffix…test (compaction mid-turn) passes through the re-pin path.whycodes-agent --lib766 passed;whycodes-session --lib172 passed. fmt is ok.docs/internals/prompt-cache.md(break table row 7 / 8) andturn-loop.mdare updated.The local release build for a before/after capture was stopped by the host running low on memory. The byte-stability test above is the measurement.
🤖 Generated with Claude Code