Repository navigation
fix(cheapmodel): insert the prefix ask before a trailing system message - #413
Conversation
Fixes #412. Every prefix ask failed on claude-sonnet-5[1m] when Claude Code's own last stored message was a mid-conversation system reminder. The provider rejected it: "role 'system' must precede an 'assistant' message or end the array". CompletePrefixed always appended the ask after the stored request's last message. Claude Code sends a role:system reminder as that last message on almost every turn, so appending after it moved the reminder out of the only two positions Anthropic allows it: right before an assistant message, or at the end of the array. This broke cache_aware_summarizer and extract_llm_sweep on Sonnet, and is very likely part of the user's real sweep_ask_failed traffic there. Fix: insert the ask one slot earlier, before the trailing run of system messages, instead of after it. This repo already encodes the exact rule offline (schema.ValidateShapeFor's RuleSystemPosition): a system message away from index 0 is legal only if it ends the array or precedes an assistant message. Inserting before the run keeps the array ending in system, which clears the rule. Two consecutive user-role messages are legal on Anthropic (this repo already relies on that for summarize's own output), and the result reproduces Claude Code's own ordinary turn shape -- [user turn, system reminder] -- which is proven to work, since it is what the client sends on nearly every turn. Nothing before the insertion point moves a byte, so the cache entry Claude Code places on that trailing message stays intact. Added insertAskMessage to do this and used it from CompletePrefixed in place of the old unconditional append. Added unit tests for one trailing system message, two in a row, and the ordinary no-trailing-system case (unaffected, byte-for-byte the old behavior). Added a gated live test (zz_live_trailing_system_test.go, skipped unless CG_LIVE=1) that runs this exact shape against a real gateway. Assisted-By: Claude Sonnet 5 <[email protected]> Signed-off-by: DAVID AMID <[email protected]>
98c9ad5 to
0e51464
Compare
|
Rebased onto main after #406; -race still running. |
amiddavid
left a comment
There was a problem hiding this comment.
Summary
The PR fixes the 400 error. But on most requests it changes a cache read into a cache write. See the inline comment on insertAskMessage. That comment includes a tested fix.
How it was tested
All live calls went to the plain gateway, not Context Guru. Model: aws/claude-sonnet-5.
- Unit tests:
go test ./internal/cheapmodel/ ./schema/pass on PR head. - Shapes, PR head against
main(df37564) as the control:
| Original messages | Main agent's request | main prefix ask |
PR prefix ask |
|---|---|---|---|
[user, sys] |
200 | 400 | OK |
[user, sys, sys] |
200 | 400 | OK |
[sys, sys] |
400 | 400 | OK |
[sys] |
400 | 400 | OK |
[user] |
200 | OK | OK |
- Cache against the main agent's entry: see the inline comment.
Findings I checked and dropped
- Two trailing system messages leave an invalid array. Not true. The provider accepts
[user, ask, sys, sys]. - An all-system array becomes invalid. The provider already rejects
[sys, ...]at index 0 ("use the top-level 'system' parameter"). So no working agent sends this shape.
🤖 Generated with Claude Code
| msgs := gjson.GetBytes(prefixBody, "messages").Array() | ||
| k := len(msgs) // insertion point: before the trailing run of system messages, if any | ||
| for k > 0 && msgs[k-1].Get("role").String() == "system" { | ||
| k-- |
There was a problem hiding this comment.
Blocking: with a trailing system message, the prefix ask no longer reads the main agent's cache. It writes a new entry.
Live result on the plain gateway (aws/claude-sonnet-5, prompt of about 21.6k tokens, PR head 0e51464):
| Shape | Main-agent call | Prefix ask after it |
|---|---|---|
ends in system reminder with cache_control (the 466/472 case) |
write=21,644 | read=0, write=21,652 |
ends in user turn with cache_control |
write=21,625 | read=21,625, write=0 |
Why it happens: The main agent puts its breakpoint on the system reminder. This PR puts the ask before that reminder. So the bytes up to the breakpoint change, and the cache key changes with them. The comment at L451-452 says the breakpoint "still covers the identical byte prefix". That is not true.
Cost: On most requests, each prefix ask pays a full cache write (1.25x) where it should pay a read (0.1x). This is the read the method exists to get.
Proposal (tested live, 2 of 2 runs): Leave the system run where it is. Add one short assistant message after it, then the ask:
[..., user "continue", system(cache_control), assistant "Understood.", user <ask>]
- status 200. The system message is followed by an assistant message, so the provider rule passes.
- read=21,644, write=0. This is the full main-agent entry.
- The request ends in a user message again, so the prefill bullet at L249-251 becomes true again. With the current code that bullet is out of date, because the request now ends in a system message.
|
|
||
| // Second call, over the IDENTICAL prefixBody: this is the live assertion that matters. Before | ||
| // this fix, EVERY prefix ask on this shape 400'd ("role 'system' must precede an 'assistant' | ||
| // message or end the array"); a plain err == nil here is itself the proof the defect is gone. |
There was a problem hiding this comment.
This test cannot catch the cache loss in the comment on insertAskMessage.
The second call reads the entry that the first prefix ask wrote, because both calls use the same changed body. In production, the entry to read is the one the main agent wrote, from the original body.
Suggestion: make the first call a plain POST of raw, as the main agent sends it. Then assert that CompletePrefixed reads about the same token count. On PR head, that version gives read=0, write=21,652.
Also: this test failed once in my runs (cache_read == 0 on the second call) and passed on the re-run (read=21,645).
Fixes the cache-loss finding from review on #413. insertAskMessage's first version inserted the ask BEFORE a trailing role:"system" message, to satisfy Anthropic's rule that a system message away from index 0 must precede an assistant message or end the array. That fixed the 400, but review measured a worse cost live: Claude Code's own cache_control breakpoint sits ON that trailing system message, and inserting anything before it changes the bytes the provider hashes up to the breakpoint. Live, that version got read=0, write=21,652 -- a full cache write where the whole point of this method is a cheap read. Fixed: leave the system message exactly where it is. When the array's last message is role:"system", append one short synthetic assistant turn ("Understood.") and then the ask, both strictly after the existing array -- nothing before or at the breakpoint moves. Live, this version got read=21,644, write=0: the full entry, read rather than rewritten. Why a synthetic assistant turn: Anthropic's own rule needs the system message followed by an assistant message or ending the array. A second user message would not clear it; "Understood." is the reviewer's own live-tested text (2 of 2 runs). Open question, documented rather than silently assumed: for a thinking.type: "enabled" session (Claude Code's shape on haiku, which PR #406's thinkingAdjustedMaxTokens exists for), Anthropic's docs say "the final assistant turn of a thinking-enabled request" must begin with a thinking block in manual mode. The synthetic turn here carries none. Whether that rule reaches a turn not continuing an active tool-use loop is not settled by the docs text alone. Added a second gated live test for exactly this shape; I could not run it myself -- typing a real API token into a command on this machine is blocked as credential leakage, and the eval box has no credentials of its own either. Rewrote the live test per review: the first call is now a plain POST of the body exactly as the main agent would send it, not a second CompletePrefixed call. The previous version's two calls both went through CompletePrefixed, so the first call's own write WAS the bug under test, and the second call read that already-wrong entry back rather than a real main-agent entry -- a passing test proved nothing. Updated the two comments review named: the "still covers the identical byte prefix" claim at CompletePrefixed's own doc comment, and insertAskMessage's doc comment, now describe the append-after shape and cite the measured numbers for both versions. Assisted-By: Claude Sonnet 5 <[email protected]> Signed-off-by: DAVID AMID <[email protected]>
ResultFixed. Appends after the trailing system message now, not before it. What changed
Extended thinking: open, not closedYou asked me to check Haiku's extended-thinking shape. Anthropic's Test results (eval box)
Pushed as |
xhigh used summarize for its summarizer. summarize builds a new prompt for its summary call. This prompt does not reuse the cached conversation. Every summary call pays full price. cache_aware_summarizer does the same job a different way. It sends the conversation itself, plus one short instruction at the end. This lets the call reuse the cache the agent's own turn already wrote. The call pays full price only for the one short instruction, not for the whole conversation. This commit swaps summarize for cache_aware_summarizer in the xhigh preset only. high keeps summarize unchanged. cache_aware_summarizer's own default trigger (fire at 90% of the context window, on any cache state) is used as is. No trigger values are pinned in the preset. high was left alone on purpose. cache_aware_summarizer needed three separate fixes before it could do anything on real Anthropic traffic: a working trigger and keep-alive integration (#402), a header-size fix for its side call (#406), and a message-order fix for the same call (#413). high's own use of summarize never needed any of these. Moving both presets at once would have mixed two unrelated changes into one commit. Also updated: the preset's own comments in config/config.go, the preset tables in docs/reference/presets.md and docs/components.md, the plugin's own copy of the preset pipeline table (context-guru-plugin/scripts/insights.py, pinned against config.go by TestPluginPresetPipelinesAgreeWithConfig) and its component description table (context-guru-plugin/skills/insights-components/ SKILL.md). docs/components.md's own description of cache_aware_summarizer said it declines without a MessagesModel; that sentence was already stale after #402 added the prefix-ask fallback, so it is corrected here too. This PR must merge after #402, #406 and #413, in that order. It is based on #402's branch so its own diff shows only the preset change. It needs to be retargeted to main once all three are merged. Assisted-By: Claude Sonnet 5 <[email protected]> Signed-off-by: DAVID AMID <[email protected]>
amiddavid
left a comment
There was a problem hiding this comment.
Summary
Re-review of 58e7070. The fix works. The 400 error is gone, and the prefix ask reads the full cache entry from the main agent's call. That includes the open question about extended thinking. I found one minor issue (inline): the thinking test defaults to a model that cannot run it.
How it was tested
All live calls went to the plain gateway, not Context Guru. The model is aws/claude-sonnet-5. Each row first sends the body as the main agent does, then runs CompletePrefixed on that body.
| Shape | Main agent's call | Prefix ask |
|---|---|---|
your test: one trailing system |
write=21,636 | read=21,636, write=0 |
your test: same shape, thinking: enabled |
write=21,636 | read=21,636, write=0 |
two trailing system messages |
write=21,660 | read=21,660, write=0 |
thinking: adaptive |
write=21,643 | read=21,643, write=0 |
thinking: enabled, mid tool loop, real signed thinking + tool_use turn |
write=22,135 | read=22,135, write=0 |
- Unit tests and vet:
go vet ./internal/cheapmodel/passes.go test ./internal/cheapmodel/ ./schema/ ./components/offload/...passes. - Extended thinking, the open question: on Sonnet 5, the provider accepts the synthetic "Understood." turn without a thinking block. This is also true in the middle of a tool loop whose earlier assistant turn has real signed thinking blocks.
- Haiku 4.5: it rejects any
role: systemmessage ("role 'system' is not supported on this model"). So Haiku traffic never has this shape, and the thinking question does not apply to it.
🤖 Generated with Claude Code
| } | ||
| model := os.Getenv("CG_MODEL") | ||
| if model == "" { | ||
| model = "claude-haiku-4-5" |
There was a problem hiding this comment.
Minor: with this default model, the test cannot pass.
claude-haiku-4-5 does not accept a role: system message at all. The main agent's own call gets 400 before the prefix ask runs:
role 'system' is not supported on this model
Live, both trailing-system tests fail at the first call when CG_MODEL=claude-haiku-4-5. Haiku 4.5 also rejects thinking: adaptive.
Suggestion: default to the same Sonnet model the other test uses (claude-sonnet-5[1m]). On aws/claude-sonnet-5, this test passes: main write=21,636, prefix read=21,636, write=0. Also change the doc comment above it. It names Haiku as the target shape, but no Haiku traffic can have this shape.
Review on 58e7070 found Haiku 4.5 rejects any role:"system" message outright, so no Haiku traffic can carry the trailing-system shape this test exists to exercise. Changed the default CG_MODEL to the same claude-sonnet-5[1m] the other live test uses. Fixed the doc comment above it, which named Haiku as the target shape. Assisted-By: Claude Sonnet 5 <[email protected]> Signed-off-by: DAVID AMID <[email protected]>
ResultFixed. The thinking live test now defaults to Sonnet. What changed
Test results (eval box)
Pushed as |
xhigh used summarize for its summarizer. summarize builds a new prompt for its summary call. This prompt does not reuse the cached conversation. Every summary call pays full price. cache_aware_summarizer does the same job a different way. It sends the conversation itself, plus one short instruction at the end. This lets the call reuse the cache the agent's own turn already wrote. The call pays full price only for the one short instruction, not for the whole conversation. This commit swaps summarize for cache_aware_summarizer in the xhigh preset only. high keeps summarize unchanged. cache_aware_summarizer's own default trigger (fire at 90% of the context window, on any cache state) is used as is. No trigger values are pinned in the preset. high was left alone on purpose. cache_aware_summarizer needed three separate fixes before it could do anything on real Anthropic traffic: a working trigger and keep-alive integration (#402), a header-size fix for its side call (#406), and a message-order fix for the same call (#413). high's own use of summarize never needed any of these. Moving both presets at once would have mixed two unrelated changes into one commit. Also updated: the preset's own comments in config/config.go, the preset tables in docs/reference/presets.md and docs/components.md, the plugin's own copy of the preset pipeline table (context-guru-plugin/scripts/insights.py, pinned against config.go by TestPluginPresetPipelinesAgreeWithConfig) and its component description table (context-guru-plugin/skills/insights-components/ SKILL.md). docs/components.md's own description of cache_aware_summarizer said it declines without a MessagesModel; that sentence was already stale after #402 added the prefix-ask fallback, so it is corrected here too. This PR must merge after #402, #406 and #413, in that order. It is based on #402's branch so its own diff shows only the preset change. It needs to be retargeted to main once all three are merged. Assisted-By: Claude Sonnet 5 <[email protected]> Signed-off-by: DAVID AMID <[email protected]>
xhigh used summarize for its summarizer. summarize builds a new prompt for its summary call. This prompt does not reuse the cached conversation. Every summary call pays full price. cache_aware_summarizer does the same job a different way. It sends the conversation itself, plus one short instruction at the end. This lets the call reuse the cache the agent's own turn already wrote. The call pays full price only for the one short instruction, not for the whole conversation. This commit swaps summarize for cache_aware_summarizer in the xhigh preset only. high keeps summarize unchanged. cache_aware_summarizer's own default trigger (fire at 90% of the context window, on any cache state) is used as is. No trigger values are pinned in the preset. high was left alone on purpose. cache_aware_summarizer needed three separate fixes before it could do anything on real Anthropic traffic: a working trigger and keep-alive integration (#402), a header-size fix for its side call (#406), and a message-order fix for the same call (#413). high's own use of summarize never needed any of these. Moving both presets at once would have mixed two unrelated changes into one commit. Also updated: the preset's own comments in config/config.go, the preset tables in docs/reference/presets.md and docs/components.md, the plugin's own copy of the preset pipeline table (context-guru-plugin/scripts/insights.py, pinned against config.go by TestPluginPresetPipelinesAgreeWithConfig) and its component description table (context-guru-plugin/skills/insights-components/ SKILL.md). docs/components.md's own description of cache_aware_summarizer said it declines without a MessagesModel; that sentence was already stale after #402 added the prefix-ask fallback, so it is corrected here too. This PR must merge after #402, #406 and #413, in that order. It is based on #402's branch so its own diff shows only the preset change. It needs to be retargeted to main once all three are merged. Assisted-By: Claude Sonnet 5 <[email protected]> Signed-off-by: DAVID AMID <[email protected]>
xhigh used summarize for its summarizer. summarize builds a new prompt for its summary call. This prompt does not reuse the cached conversation. Every summary call pays full price. cache_aware_summarizer does the same job a different way. It sends the conversation itself, plus one short instruction at the end. This lets the call reuse the cache the agent's own turn already wrote. The call pays full price only for the one short instruction, not for the whole conversation. This commit swaps summarize for cache_aware_summarizer in the xhigh preset only. high keeps summarize unchanged. cache_aware_summarizer's own default trigger (fire at 90% of the context window, on any cache state) is used as is. No trigger values are pinned in the preset. high was left alone on purpose. cache_aware_summarizer needed three separate fixes before it could do anything on real Anthropic traffic: a working trigger and keep-alive integration (#402), a header-size fix for its side call (#406), and a message-order fix for the same call (#413). high's own use of summarize never needed any of these. Moving both presets at once would have mixed two unrelated changes into one commit. Also updated: the preset's own comments in config/config.go, the preset tables in docs/reference/presets.md and docs/components.md, the plugin's own copy of the preset pipeline table (context-guru-plugin/scripts/insights.py, pinned against config.go by TestPluginPresetPipelinesAgreeWithConfig) and its component description table (context-guru-plugin/skills/insights-components/ SKILL.md). docs/components.md's own description of cache_aware_summarizer said it declines without a MessagesModel; that sentence was already stale after #402 added the prefix-ask fallback, so it is corrected here too. This PR must merge after #402, #406 and #413, in that order. It is based on #402's branch so its own diff shows only the preset change. It needs to be retargeted to main once all three are merged. Assisted-By: Claude Sonnet 5 <[email protected]> Signed-off-by: DAVID AMID <[email protected]>
xhigh used summarize for its summarizer. summarize builds a new prompt for its summary call. This prompt does not reuse the cached conversation. Every summary call pays full price. cache_aware_summarizer does the same job a different way. It sends the conversation itself, plus one short instruction at the end. This lets the call reuse the cache the agent's own turn already wrote. The call pays full price only for the one short instruction, not for the whole conversation. This commit swaps summarize for cache_aware_summarizer in the xhigh preset only. high keeps summarize unchanged. cache_aware_summarizer's own default trigger (fire at 90% of the context window, on any cache state) is used as is. No trigger values are pinned in the preset. high was left alone on purpose. cache_aware_summarizer needed three separate fixes before it could do anything on real Anthropic traffic: a working trigger and keep-alive integration (#402), a header-size fix for its side call (#406), and a message-order fix for the same call (#413). high's own use of summarize never needed any of these. Moving both presets at once would have mixed two unrelated changes into one commit. Also updated: the preset's own comments in config/config.go, the preset tables in docs/reference/presets.md and docs/components.md, the plugin's own copy of the preset pipeline table (context-guru-plugin/scripts/insights.py, pinned against config.go by TestPluginPresetPipelinesAgreeWithConfig) and its component description table (context-guru-plugin/skills/insights-components/ SKILL.md). docs/components.md's own description of cache_aware_summarizer said it declines without a MessagesModel; that sentence was already stale after #402 added the prefix-ask fallback, so it is corrected here too. This PR must merge after #402, #406 and #413, in that order. It is based on #402's branch so its own diff shows only the preset change. It needs to be retargeted to main once all three are merged. Assisted-By: Claude Sonnet 5 <[email protected]> Signed-off-by: DAVID AMID <[email protected]>
xhigh used summarize for its summarizer. summarize builds a new prompt for its summary call. This prompt does not reuse the cached conversation. Every summary call pays full price. cache_aware_summarizer does the same job a different way. It sends the conversation itself, plus one short instruction at the end. This lets the call reuse the cache the agent's own turn already wrote. The call pays full price only for the one short instruction, not for the whole conversation. This commit swaps summarize for cache_aware_summarizer in the xhigh preset only. high keeps summarize unchanged. cache_aware_summarizer's own default trigger (fire at 90% of the context window, on any cache state) is used as is. No trigger values are pinned in the preset. high was left alone on purpose. cache_aware_summarizer needed three separate fixes before it could do anything on real Anthropic traffic: a working trigger and keep-alive integration (#402), a header-size fix for its side call (#406), and a message-order fix for the same call (#413). high's own use of summarize never needed any of these. Moving both presets at once would have mixed two unrelated changes into one commit. Also updated: the preset's own comments in config/config.go, the preset tables in docs/reference/presets.md and docs/components.md, the plugin's own copy of the preset pipeline table (context-guru-plugin/scripts/insights.py, pinned against config.go by TestPluginPresetPipelinesAgreeWithConfig) and its component description table (context-guru-plugin/skills/insights-components/ SKILL.md). docs/components.md's own description of cache_aware_summarizer said it declines without a MessagesModel; that sentence was already stale after #402 added the prefix-ask fallback, so it is corrected here too. This PR must merge after #402, #406 and #413, in that order. It is based on #402's branch so its own diff shows only the preset change. It needs to be retargeted to main once all three are merged. Assisted-By: Claude Sonnet 5 <[email protected]> Signed-off-by: DAVID AMID <[email protected]>
xhigh used summarize for its summarizer. summarize builds a new prompt for its summary call. This prompt does not reuse the cached conversation. Every summary call pays full price. cache_aware_summarizer does the same job a different way. It sends the conversation itself, plus one short instruction at the end. This lets the call reuse the cache the agent's own turn already wrote. The call pays full price only for the one short instruction, not for the whole conversation. This commit swaps summarize for cache_aware_summarizer in the xhigh preset only. high keeps summarize unchanged. cache_aware_summarizer's own default trigger (fire at 90% of the context window, on any cache state) is used as is. No trigger values are pinned in the preset. high was left alone on purpose. cache_aware_summarizer needed three separate fixes before it could do anything on real Anthropic traffic: a working trigger and keep-alive integration (#402), a header-size fix for its side call (#406), and a message-order fix for the same call (#413). high's own use of summarize never needed any of these. Moving both presets at once would have mixed two unrelated changes into one commit. Also updated: the preset's own comments in config/config.go, the preset tables in docs/reference/presets.md and docs/components.md, the plugin's own copy of the preset pipeline table (context-guru-plugin/scripts/insights.py, pinned against config.go by TestPluginPresetPipelinesAgreeWithConfig) and its component description table (context-guru-plugin/skills/insights-components/ SKILL.md). docs/components.md's own description of cache_aware_summarizer said it declines without a MessagesModel; that sentence was already stale after #402 added the prefix-ask fallback, so it is corrected here too. This PR must merge after #402, #406 and #413, in that order. It is based on #402's branch so its own diff shows only the preset change. It needs to be retargeted to main once all three are merged. Assisted-By: Claude Sonnet 5 <[email protected]> Signed-off-by: DAVID AMID <[email protected]>
xhigh used summarize for its summarizer. summarize builds a new prompt for its summary call. This prompt does not reuse the cached conversation. Every summary call pays full price. cache_aware_summarizer does the same job a different way. It sends the conversation itself, plus one short instruction at the end. This lets the call reuse the cache the agent's own turn already wrote. The call pays full price only for the one short instruction, not for the whole conversation. This commit swaps summarize for cache_aware_summarizer in the xhigh preset only. high keeps summarize unchanged. cache_aware_summarizer's own default trigger (fire at 90% of the context window, on any cache state) is used as is. No trigger values are pinned in the preset. high was left alone on purpose. cache_aware_summarizer needed three separate fixes before it could do anything on real Anthropic traffic: a working trigger and keep-alive integration (#402), a header-size fix for its side call (#406), and a message-order fix for the same call (#413). high's own use of summarize never needed any of these. Moving both presets at once would have mixed two unrelated changes into one commit. Also updated: the preset's own comments in config/config.go, the preset tables in docs/reference/presets.md and docs/components.md, the plugin's own copy of the preset pipeline table (context-guru-plugin/scripts/insights.py, pinned against config.go by TestPluginPresetPipelinesAgreeWithConfig) and its component description table (context-guru-plugin/skills/insights-components/ SKILL.md). docs/components.md's own description of cache_aware_summarizer said it declines without a MessagesModel; that sentence was already stale after #402 added the prefix-ask fallback, so it is corrected here too. This PR must merge after #402, #406 and #413, in that order. It is based on #402's branch so its own diff shows only the preset change. It needs to be retargeted to main once all three are merged. Assisted-By: Claude Sonnet 5 <[email protected]> Signed-off-by: DAVID AMID <[email protected]>
xhigh used summarize for its summarizer. summarize builds a new prompt for its summary call. This prompt does not reuse the cached conversation. Every summary call pays full price. cache_aware_summarizer does the same job a different way. It sends the conversation itself, plus one short instruction at the end. This lets the call reuse the cache the agent's own turn already wrote. The call pays full price only for the one short instruction, not for the whole conversation. This commit swaps summarize for cache_aware_summarizer in the xhigh preset only. high keeps summarize unchanged. cache_aware_summarizer's own default trigger (fire at 90% of the context window, on any cache state) is used as is. No trigger values are pinned in the preset. high was left alone on purpose. cache_aware_summarizer needed three separate fixes before it could do anything on real Anthropic traffic: a working trigger and keep-alive integration (#402), a header-size fix for its side call (#406), and a message-order fix for the same call (#413). high's own use of summarize never needed any of these. Moving both presets at once would have mixed two unrelated changes into one commit. Also updated: the preset's own comments in config/config.go, the preset tables in docs/reference/presets.md and docs/components.md, the plugin's own copy of the preset pipeline table (context-guru-plugin/scripts/insights.py, pinned against config.go by TestPluginPresetPipelinesAgreeWithConfig) and its component description table (context-guru-plugin/skills/insights-components/ SKILL.md). docs/components.md's own description of cache_aware_summarizer said it declines without a MessagesModel; that sentence was already stale after #402 added the prefix-ask fallback, so it is corrected here too. This PR must merge after #402, #406 and #413, in that order. It is based on #402's branch so its own diff shows only the preset change. It needs to be retargeted to main once all three are merged. Assisted-By: Claude Sonnet 5 <[email protected]> Signed-off-by: DAVID AMID <[email protected]>
Fixes #412.
Result
Every prefix ask over a request ending in a mid-conversation system
reminder now succeeds, and reads the cache instead of rewriting it.
What this fix does
role: systemmessage exactly where it is.after it.
lands at the end, byte-for-byte the old behavior.
Why the first version of this fix was wrong
The first version inserted the ask BEFORE the trailing system
message instead of after it. That cleared the 400. Review then found
a cost regression:
message.
up to the breakpoint.
read=0,write=21,652. A full cache write, wherethis method exists to get a cheap read.
The earlier "measured live... cache still reads at the same level"
claim in this PR body was wrong. It came from a test that read back
its own first call, not the main agent's real entry. See "What the
live test checks now" below.
The fix now in this PR
Append after the system message, not before it:
read=21,644,write=0. The full main-agententry, read rather than rewritten.
followed by an assistant message, not by the ask directly.
What the live test checks now
The previous version of
zz_live_trailing_system_test.gomade twocalls, both through
CompletePrefixed. The first call's own writewas the bug under test. The second call read that already-wrong
entry back, not a real entry from the main agent. A passing test
proved nothing.
The test now makes the first call a plain POST, exactly as the main
agent sends it. The second call is
CompletePrefixed, and the testchecks that its
cache_readlands near what the first call wrote. Itmust fail against the "insert before" version and pass against this
one.
Open question: extended thinking
Claude Code uses
thinking.type: "enabled"(manual mode, a fixedtoken budget) on Haiku; see #406. Anthropic's docs say:
The synthetic "Understood." turn this fix adds carries no thinking
block. Whether "the final assistant turn" means the literal last
assistant message regardless of what follows it, or only one an
active tool-use loop is continuing through, is not settled by the
docs text alone — this turn is neither active nor a tool-use
continuation, but it is literally the last assistant message.
I added a second gated live test for this exact shape
(
TestLiveCompletePrefixedWithTrailingSystemMessageAndThinkingEnabled).I could not run it myself: typing a real API token into a command on
this machine is blocked as credential leakage, and I did not try to
route around it. The eval box has no credentials of its own either.
This needs a run with
thinking.type: "enabled"before the questioncan be called settled either way.
Test plan
TestCompletePrefixedAppendsAfterATrailingSystemMessage: onetrailing system message. Checks every original message is
byte-identical and unmoved, the synthetic turn and the ask land
after it in order, and the ask is last.
TestCompletePrefixedAppendsAfterMultipleTrailingSystemMessages:two in a row, both stay exactly where they are.
TestCompletePrefixedAppendsAtTheEndWithNoTrailingSystemMessage:the ordinary case is unaffected.
TestInsertAskMessage: unit table test on the shape directly.TestCompletePrefixedReturnsATruncatedReplyRatherThanAnError,TestCompletePrefixedCapsAtTheBodysOwnMaxTokensWhenWantWouldExceedTheModelsCap,and the other
#406-era tests: unaffected by this change, stillpassing after the rebase.
zz_live_trailing_system_test.go: two gated live tests(
CG_LIVE=1 CG_BASE=... CG_TOKEN=... CG_MODEL=...), one for theplain shape and one with extended thinking enabled. Neither could
be run by me; see "Open question" above for the thinking one.
Offline, every other test passed on the eval box:
go build ./...— okgo vet— okgofmt -lon the changed files — emptygo test ./internal/cheapmodel/... ./components/offload/...— okgo test -race -count=1 ./internal/cheapmodel/... ./components/offload/...— ok (1.833s / 150.114s)Scope
Touches only
CompletePrefixed's message-array construction and itsown doc comments. Kept away from #406's
max_tokenslines in thesame function, to avoid conflicts — rebased cleanly onto main after
#406 merged.