Skip to content

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
jiwangyihao:pr/openai-session-affinity
Open

fix(openai): honor x-session-affinity in session hash and upstream session injection for better OpenCode request sticky and higher cache hit rate#1578
jiwangyihao wants to merge 1 commit intoWei-Shaw:mainfrom
jiwangyihao:pr/openai-session-affinity

Conversation

@jiwangyihao
Copy link
Copy Markdown

@jiwangyihao jiwangyihao commented Apr 11, 2026

x-session-affinity 是 OpenCode 使用的 session 标记请求头。

本项目中将 OpenCode 作为和 Codex CLI 并列的客户端推荐,应当支持 OpenCode 使用的标记请求头。进行此变更后在我自己的实例上可以将 OpenCode相关请求的缓存命中率从 1x%提高到 90%+。(顺带一提,现在的缓存命中率计算也是错误的)

Summary

  • honor x-session-affinity between conversation_id and prompt_cache_key when extracting the effective OpenAI session signal
  • use the same priority for sticky-session hash generation and OAuth upstream session_id / conversation_id injection
  • add focused regression tests for session-hash priority and upstream request building

Testing

  • go test ./internal/service -run "GenerateSessionHash_UsesXSessionAffinity|ExtractSessionID_PrefersXSessionAffinity|BuildUpstreamRequest_UsesXSessionAffinity" -count=1

@jiwangyihao jiwangyihao force-pushed the pr/openai-session-affinity branch from 7a7a081 to a217e74 Compare April 11, 2026 17:23
@jiwangyihao jiwangyihao changed the title fix(openai): honor x-session-affinity in session hash and upstream session injection fix(openai): honor x-session-affinity in session hash and upstream session injection for better OpenCode request sticky and higher cache hit rate Apr 11, 2026
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>
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