feat(opencode): inject x-opencode-session header for prompt caching - #12615
Closed
pspranger-throw wants to merge 1 commit into
Closed
feat(opencode): inject x-opencode-session header for prompt caching#12615pspranger-throw wants to merge 1 commit into
pspranger-throw wants to merge 1 commit into
Conversation
Send the x-opencode-session header on every API request to OpenCode Go/Zen providers so the proxy can cache the prompt prefix across requests in the same conversation. The header is injected per-request in _build_api_kwargs() across all three API modes: - chat_completions (opencode-go default, opencode-zen default) - anthropic_messages (opencode-go minimax-*, opencode-zen claude-*) - codex_responses (opencode-zen gpt-*) Includes 9 tests covering all three paths, negative cases (no header leakage to other providers), and state mutation (model switch, session rotation after compression).
Author
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.
Send the x-opencode-session header on every API request to OpenCode Go/Zen providers so the proxy can cache the prompt prefix across requests in the same conversation.
The header is injected per-request in _build_api_kwargs() across all three API modes:
Includes 9 tests covering all three paths, negative cases (no header leakage to other providers), and state mutation (model switch, session rotation after compression).
What does this PR do?
Adds a header to all opencode zen/go api calls to ensure that opencode can track sessions for context caching, otherwise each call pays for the full uncached context!
Solution orients itself after the existing x-grok-conv-id implementation.
Related Issue
Related to #5252. This is a minimal fix for OpenCode Go and Zen while the broader routing table architecture from #5252 is developed.
This PR takes a minimal, targeted approach: direct conditionals in
_build_api_kwargs()for the OpenCode provider only. When thedata-driven routing table from #5252 lands, the OpenCode entries
can be migrated into it.
Type of Change
Changes Made
Defensive attribute access: Uses
getattr(self, "_base_url_lower", "")rather thanself._base_url_lowerbecause some code paths (notably tests usingobject.__new__()to bypass
__init__) reach_build_api_kwargswithout the setter having run. Returns""which safely skips the opencode check — correct behavior for non-opencode agents.How to Test
Checklist
Code
fix(scope):,feat(scope):, etc.)pytest tests/ -qand all tests pass ( there are pre-existing unrelated failures!)Documentation & Housekeeping
docs/, docstrings) — or N/Acli-config.yaml.exampleif I added/changed config keys — or N/ACONTRIBUTING.mdorAGENTS.mdif I changed architecture or workflows — or N/AScreenshots / Logs