fix(proxy/ccr): self-heal dangling headroom_retrieve reference after model switch#2500
Open
nangsontay wants to merge 2 commits into
Open
fix(proxy/ccr): self-heal dangling headroom_retrieve reference after model switch#2500nangsontay wants to merge 2 commits into
nangsontay wants to merge 2 commits into
Conversation
…model switch
Switching models mid-session (/model in Claude Code) or restarting the
proxy rotated the model-scoped in-memory sticky session key, so the proxy
stopped injecting headroom_retrieve while the client transcript still
carried a tool_reference naming it — Anthropic 400'd every subsequent turn
("Tool reference 'headroom_retrieve' not found in available tools").
Injection now self-heals from the transcript: transcript_references_ccr_tool
scans about-to-forward messages for a bare headroom_retrieve in anthropic
tool_reference/tool_use blocks (one level of nesting) or an openai-chat
assistant.tool_calls entry, and a new transcript_requires_tool flag forces
(re-)injection through should_inject_ccr_tool and apply_session_sticky_ccr_tool
(decision inject_transcript_recovery), recording sticky state so subsequent
turns resume normal sticky replay. Exact bare-name match only, so a
client-owned mcp__headroom__headroom_retrieve never triggers proxy injection.
The openai /v1/responses path never proxy-injects the tool, so recovery is
N/A there.
Contributor
PR governanceThis PR does not yet satisfy the required template fields:
Please update the PR body, or move the PR back to draft while it is still in progress. |
|
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
nangsontay
marked this pull request as draft
July 23, 2026 04:26
Extract a local matches() helper in _anthropic_content_references_tool so the tool_reference/tool_use type+name check is written once and reused for both top-level and one-level-nested blocks.
nangsontay
marked this pull request as ready for review
July 23, 2026 10:15
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.
Description
Through the Headroom proxy, switching models mid-session (
/modelin Claude Code) — or restarting the proxy — 400'd every subsequent turn withTool reference 'headroom_retrieve' not found in available tools.Root cause: the sticky CCR retrieve-tool injection is keyed by a model-scoped, in-memory session id (
compute_session_idhashesmodel + leading system text). A/modelswitch or restart rotates/loses that key, soapply_session_sticky_ccr_toolstops injectingheadroom_retrieve— while the client transcript still carries atool_referenceblock naming it (Anthropic validates everytool_referenceagainst the request'stoolsarray).Fix: make the invariant self-heal from the transcript instead of from tracker state. Key-tweaking alone can't fix it (the system-prompt half of the hash rotates on model switch, and restarts lose the tracker regardless).
Plan:
plans/260723-1004-ccr-dangling-tool-reference-reinjection/.Closes #
Type of Change
Changes Made
headroom/proxy/ccr_marker_policy.py— newtranscript_references_ccr_tool()that scans about-to-forward messages for a bareheadroom_retrievein anthropictool_reference/tool_useblocks (one level of nesting intotool_result/tool_search_tool_result) or an openai-chatassistant.tool_callsentry; extendedshould_inject_ccr_tool()with atranscript_requires_tooloverride.headroom/proxy/tool_injection_logging.py— addedinject_transcript_recoveryto the closedToolInjectionDecisionliteral.headroom/proxy/helpers.py— re-export the policy fn; threadtranscript_requires_toolthrough theshould_inject_ccr_toolwrapper andapply_session_sticky_ccr_tool(both the no-session-id and fresh-session paths now inject on the transcript signal, loginject_transcript_recovery, and record sticky state so subsequent turns resume normal sticky replay).headroom/proxy/handlers/anthropic.py— compute the flag overoptimized_messages, pass it through, add a distinct recovery log branch.headroom/proxy/handlers/openai.py— same wiring on the chat path.tests/test_ccr_tool_always_on.py— 15 new tests (policy wire-shapes, mcp-prefixed exclusion for both providers, override semantics, and end-to-end recovery through the sticky helper).docs/content/docs/troubleshooting.mdx— new troubleshooting entry (symptom, cause, fix, and workarounds for older versions).Exact bare-name match only, so a client-owned
mcp__headroom__headroom_retrieve(MCP, client-owned lifecycle) never triggers proxy injection. The openai/v1/responsespath never proxy-injects the CCR tool, so recovery is N/A there (plan decision D1).Testing
pytest) — scoped to the CCR/tool-injection blast radius (16 files, 256 tests); the full repo suite has a known noisy baseline unrelated to this change.ruff check) — on the touched files.mypy headroom) — not run this pass./modelswitch not exercised; reproduced via unit tests (see Real Behavior Proof).Test Output
Real Behavior Proof
uv run pytest(pytest 9.0.3).pytest/ruffcommands above. The/model-switch failure is reproduced as a unit test —test_transcript_recovery_injects_on_fresh_trackerconstructs the exact failing shape (danglingtool_reference, freshly-rotated/empty tracker, no fresh CCR markers) and asserts the tool is re-injected withdecision=inject_transcript_recovery;test_transcript_recovery_records_sticky_stateasserts the following turn resumes sticky replay.Review Readiness
Checklist
CHANGELOG.md— it is generated by release-please from my Conventional Commit PR title (a CI guard enforces this)Screenshots (if applicable)
N/A — proxy/backend change, no UI surface.
Additional Notes