fix(openai): honor x-session-affinity in session hash and upstream session injection for better OpenCode request sticky and higher cache hit rate#1578
Open
jiwangyihao wants to merge 1 commit intoWei-Shaw:mainfrom
Conversation
7a7a081 to
a217e74
Compare
lpdsdx
added a commit
to lpdsdx/sub2api
that referenced
this pull request
Apr 17, 2026
…jection Cherry-pick from upstream PR Wei-Shaw#1578 (Wei-Shaw/sub2api) Adds x-session-affinity header support (used by OpenCode) to session hash generation and upstream request building. Improves cache hit rate from ~1x% to 90%+ for OpenCode requests. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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.
x-session-affinity 是 OpenCode 使用的 session 标记请求头。
本项目中将 OpenCode 作为和 Codex CLI 并列的客户端推荐,应当支持 OpenCode 使用的标记请求头。进行此变更后在我自己的实例上可以将 OpenCode相关请求的缓存命中率从 1x%提高到 90%+。(顺带一提,现在的缓存命中率计算也是错误的)
Summary
x-session-affinitybetweenconversation_idandprompt_cache_keywhen extracting the effective OpenAI session signalsession_id/conversation_idinjectionTesting
go test ./internal/service -run "GenerateSessionHash_UsesXSessionAffinity|ExtractSessionID_PrefersXSessionAffinity|BuildUpstreamRequest_UsesXSessionAffinity" -count=1