Skip to content

perf(agent): keep turn context on its own prompt so the cache survives - #419

Merged
mrnasil merged 2 commits into
mainfrom
perf/stable-turn-context
Nov 7, 2026
Merged

mrnasil merged 2 commits into
mainfrom
perf/stable-turn-context

Conversation

@mrnasil

@mrnasil mrnasil commented Nov 7, 2026 •

Copy link
Copy Markdown
Member

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:

  1. At every new turn. The previous prompt was re-sent without its recall. In a captured two-turn session, the turn-1 prompt was 689 tokens with recall and 18 tokens without. The cached prefix ended right after the system prompt, so the whole history of earlier turns was processed again on every turn.
  2. Mid-turn. A recovery note, a steering message or a background notice became the latest user message, and the recall moved onto it.
  3. Step 2. The posture and notice were sent on step 1 only, so step 2 re-sent the prompt without them.

Change

  • At turn start the three pieces are joined once and pinned to the turn's prompt, keyed by its created_at (Session::pin_prompt_suffix).
  • build_request re-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.
  • The transcript still holds only what the user typed, so titles, memory retain, session search and exports are unchanged.
  • If compaction removes the pinned prompt, the suffix is pinned again to the latest prompt. A prompt without a stamp (an old resumed transcript) falls back to the previous per-request append.
  • token_count / token_count_cached include the pinned text, so the compaction threshold and the context meter see what is actually sent.
  • cache_warm no longer appends anything itself; build_request already 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_request covers the fallback path.
  • The session tests cover pinning, re-pinning, skipping agent notes, block content, dropping pins of removed prompts, and token counts.
  • The existing retried_first_step_keeps_suffix… test (compaction mid-turn) passes through the re-pin path.
  • whycodes-agent --lib 766 passed; whycodes-session --lib 172 passed. fmt is ok.
  • docs/internals/prompt-cache.md (break table row 7 / 8) and turn-loop.md are 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

mrnasil and others added 2 commits November 7, 2026 16:09
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]>
@mrnasil
mrnasil force-pushed the perf/stable-turn-context branch from b2ea0e9 to 419ffd2 Compare November 7, 2026 13:09
@mrnasil
mrnasil merged commit f55940d into main Nov 7, 2026
7 checks passed
@mrnasil
mrnasil deleted the perf/stable-turn-context branch November 7, 2026 13:18
@mrnasil mrnasil mentioned this pull request Nov 7, 2026
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