Skip to content

fix: Codex session_id fallback to prompt_cache_key#521

Merged
ding113 merged 1 commit intodevfrom
fix/codex-session-id-prompt-cache-key
Jan 3, 2026
Merged

fix: Codex session_id fallback to prompt_cache_key#521
ding113 merged 1 commit intodevfrom
fix/codex-session-id-prompt-cache-key

Conversation

@ding113
Copy link
Owner

@ding113 ding113 commented Jan 3, 2026

背景

改动

  • 将 body prompt_cache_key 纳入 Codex session_id 提取链路(仅在 header 未命中时启用,优先级高于 metadata.session_id)。
  • 新增/补齐单测覆盖该回退与优先级/非法值场景。

提取优先级

  1. headers["session_id"]
  2. headers["x-session-id"]
  3. body.prompt_cache_key ← 新增
  4. body.metadata.session_id
  5. body.previous_response_id (with codex_prev_ prefix)

验证

  • bun run lint
  • bun run typecheck
  • bun run test:coverage
  • bun run build

Related:


Enhanced by Claude Code

@coderabbitai
Copy link

coderabbitai bot commented Jan 3, 2026

📝 Walkthrough

演练

引入新的会话源类型"body_prompt_cache_key"用于Codex会话ID提取。实现提取逻辑,更新优先级顺序,将body.prompt_cache_key置于metadata.session_id之上。为新源和优先级回退行为添加全面测试覆盖。

更改

队列 / 文件 更改概述
会话ID提取核心逻辑
src/app/v1/_lib/codex/session-extractor.ts
CodexSessionIdSource类型添加新变体"body_prompt_cache_key";将会话ID提取优先级重新排序为:header session_id → header x-session-id → body.prompt_cache_key → body.metadata.session_id → body.previous_response_id;引入body.prompt_cache_key提取逻辑,当headers中无session_id时优先使用。
会话ID提取测试套件
src/app/v1/_lib/codex/__tests__/session-extractor.test.ts
添加从body.prompt_cache_key提取会话ID的测试用例;验证body.prompt_cache_key覆盖metadata.session_id时的优先级规则;测试无效prompt_cache_key时回退至metadata.session_id的行为;确保header和body提取路径保持兼容。

预估代码审查工作量

🎯 3 (中等) | ⏱️ ~20 分钟

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed 标题准确概括了主要改动:为Codex session_id添加prompt_cache_key作为回退机制。
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Description check ✅ Passed 拉取请求的描述详细说明了背景、改动内容、提取优先级和验证步骤,与代码变更高度相关。
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/codex-session-id-prompt-cache-key

Comment @coderabbitai help to get the list of available commands and usage tips.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @ding113, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

此拉取请求旨在通过引入 prompt_cache_key 作为 Codex 会话ID的可靠回退源,来提高会话的稳定性。当请求头中未提供会话ID时,系统将利用 prompt_cache_key 来维持会话粘性,从而解决现有机制在某些情况下可能导致会话不稳定的问题。

Highlights

  • 会话ID回退机制增强: 当请求头中缺少 session_id 时,现在会优先从请求体中的 prompt_cache_key 字段提取会话ID。
  • 优先级调整: prompt_cache_key 的优先级高于 metadata.session_id,确保在没有头部会话ID时,能更稳定地获取会话。
  • 测试覆盖: 新增了单元测试,以验证 prompt_cache_key 回退逻辑、优先级以及对非法值的处理。
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

本次 PR 的改动目标明确,即在 Codex 会话 ID 提取逻辑中增加 prompt_cache_key 作为备选方案,以提高会话粘性的稳定性。代码实现清晰、直接,并且与现有逻辑融合得很好。新的提取优先级(header > prompt_cache_key > metadata.session_id)已正确实现。

值得称赞的是,您为新功能和边缘场景(如优先级、无效值回退)补充了全面的单元测试,这大大保证了代码的质量和可靠性。

整体来看,这是一次高质量的修复,代码风格一致,逻辑严谨。我没有发现任何需要修改的问题。

@github-actions github-actions bot added bug Something isn't working area:session area:core labels Jan 3, 2026
@github-actions
Copy link
Contributor

github-actions bot commented Jan 3, 2026

🧪 测试结果

测试类型 状态
代码质量
单元测试
集成测试
API 测试

总体结果: ✅ 所有测试通过

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
src/app/v1/_lib/codex/session-extractor.ts (1)

91-99: 实现逻辑正确,但建议统一注释语言。

提取逻辑正确实现了 prompt_cache_key 的验证和优先级处理,位置恰当地插入在 header 检查之后、metadata 检查之前。

Line 91 的中文注释与代码库中其他英文注释风格不一致,建议改为英文以保持一致性。

🔎 建议修改为英文注释
-  // 当请求头未提供 session_id 时,优先尝试使用 prompt_cache_key 作为稳定的会话标识
+  // When the request header does not provide session_id, prefer using prompt_cache_key as a stable session identifier
   const bodyPromptCacheKey = normalizeCodexSessionId(requestBody.prompt_cache_key);
📜 Review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Cache: Disabled due to Reviews > Disable Cache setting

📥 Commits

Reviewing files that changed from the base of the PR and between a479718 and f3e8e05.

📒 Files selected for processing (2)
  • src/app/v1/_lib/codex/__tests__/session-extractor.test.ts
  • src/app/v1/_lib/codex/session-extractor.ts
🧰 Additional context used
📓 Path-based instructions (9)
**/*.{ts,tsx,js,jsx,json}

📄 CodeRabbit inference engine (CLAUDE.md)

Use 2-space indentation in all code files

Files:

  • src/app/v1/_lib/codex/session-extractor.ts
  • src/app/v1/_lib/codex/__tests__/session-extractor.test.ts
**/*.{ts,tsx,js,jsx}

📄 CodeRabbit inference engine (CLAUDE.md)

**/*.{ts,tsx,js,jsx}: Use double quotes for strings instead of single quotes
Use trailing commas in multi-line structures
Enforce maximum line length of 100 characters
Use path alias @/* to reference files from ./src/* directory

**/*.{ts,tsx,js,jsx}: Use Biome for linting and formatting with 2-space indent, double quotes, trailing commas, and 100 character max line length
Use path alias @/* to reference files in ./src/* directory

Files:

  • src/app/v1/_lib/codex/session-extractor.ts
  • src/app/v1/_lib/codex/__tests__/session-extractor.test.ts
src/app/v1/_lib/**/*.ts

📄 CodeRabbit inference engine (CLAUDE.md)

Guard pipeline must execute in order: ProxyAuthenticator, SensitiveWordGuard, VersionGuard, ProxySessionGuard, ProxyRateLimitGuard, ProxyProviderResolver

Files:

  • src/app/v1/_lib/codex/session-extractor.ts
  • src/app/v1/_lib/codex/__tests__/session-extractor.test.ts
**/*.{ts,tsx}

📄 CodeRabbit inference engine (AGENTS.md)

**/*.{ts,tsx}: Use TypeScript strict mode for type safety
Use readonly or const assertions for immutable data structures

Files:

  • src/app/v1/_lib/codex/session-extractor.ts
  • src/app/v1/_lib/codex/__tests__/session-extractor.test.ts
src/**/*.ts

📄 CodeRabbit inference engine (AGENTS.md)

src/**/*.ts: Hash API keys using SHA-256 before storing in database, never store plaintext keys
Mask API keys and sensitive data in application logs
Validate required environment variables at startup with clear error messages

Files:

  • src/app/v1/_lib/codex/session-extractor.ts
  • src/app/v1/_lib/codex/__tests__/session-extractor.test.ts
src/app/**/*.ts

📄 CodeRabbit inference engine (AGENTS.md)

Implement Content-Security-Policy headers for XSS prevention

Files:

  • src/app/v1/_lib/codex/session-extractor.ts
  • src/app/v1/_lib/codex/__tests__/session-extractor.test.ts
src/app/v1/**/*.ts

📄 CodeRabbit inference engine (AGENTS.md)

Use Hono router for ultrafast, lightweight routing in proxy endpoints

Files:

  • src/app/v1/_lib/codex/session-extractor.ts
  • src/app/v1/_lib/codex/__tests__/session-extractor.test.ts
**/*.test.{ts,tsx}

📄 CodeRabbit inference engine (AGENTS.md)

Use Vitest for unit testing with Node environment, coverage thresholds: 50% lines/functions, 40% branches

Files:

  • src/app/v1/_lib/codex/__tests__/session-extractor.test.ts
**/*.test.ts

📄 CodeRabbit inference engine (AGENTS.md)

Ensure test database names contain 'test' keyword for safety validation

Files:

  • src/app/v1/_lib/codex/__tests__/session-extractor.test.ts
🧬 Code graph analysis (1)
src/app/v1/_lib/codex/__tests__/session-extractor.test.ts (1)
src/app/v1/_lib/codex/session-extractor.ts (1)
  • extractCodexSessionId (66-124)
🪛 Gitleaks (8.30.0)
src/app/v1/_lib/codex/__tests__/session-extractor.test.ts

[high] 25-25: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.

(generic-api-key)


[high] 49-49: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.

(generic-api-key)


[high] 61-61: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.

(generic-api-key)


[high] 116-116: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.

(generic-api-key)

⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (7)
  • GitHub Check: pr-description
  • GitHub Check: Docker Build Test
  • GitHub Check: 🌐 API Tests
  • GitHub Check: check-codex-status
  • GitHub Check: pr-review
  • GitHub Check: pr-label
  • GitHub Check: dev-build-deploy
🔇 Additional comments (7)
src/app/v1/_lib/codex/__tests__/session-extractor.test.ts (5)

25-25: 测试数据合理,静态分析误报可忽略。

添加 prompt_cache_key 到测试数据中是正确的,确保了即使存在 body 字段时,header 依然具有最高优先级。

注意:Gitleaks 将此 UUID 标记为 API key 是误报,这是标准的测试数据,不涉及安全风险。


48-58: 新测试覆盖了 prompt_cache_key 提取路径。

测试逻辑正确,验证了从 body.prompt_cache_key 提取会话 ID 的基本功能。

注意:Line 49 的静态分析警告是误报,UUID 是测试数据而非真实 API key。


60-71: 优先级测试逻辑正确。

此测试确保 prompt_cache_key 在优先级链中高于 metadata.session_id,符合 PR 目标和实现逻辑。

注意:Line 61 的静态分析警告是误报。


73-83: 边界测试覆盖了无效值降级逻辑。

测试确保了当 prompt_cache_key 不符合验证规则时(长度 < 21),系统正确降级到 metadata.session_id,体现了健壮的错误处理。


116-116: 完善了优先级测试的完整性。

在所有可能的会话来源都存在时,测试验证了 header session_id 具有最高优先级,覆盖了新增的 prompt_cache_key 字段。

注意:Line 116 的静态分析警告是误报。

src/app/v1/_lib/codex/session-extractor.ts (2)

6-6: 类型定义正确添加了新的会话来源。

"body_prompt_cache_key" 的命名风格与现有来源一致,TypeScript 类型系统将确保此值在整个代码库中的正确使用。


60-62: 优先级文档准确更新。

注释清晰地说明了新的提取优先级顺序,将 body.prompt_cache_key 置于第 3 位,高于 body.metadata.session_id,符合 PR 目标。

@github-actions github-actions bot added the size/S Small PR (< 200 lines) label Jan 3, 2026
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review Summary

No significant issues identified in this PR.

PR Size: S

  • Lines changed: 55
  • Files changed: 2

Review Coverage

  • Logic and correctness - Clean
  • Security (OWASP Top 10) - Clean
  • Error handling - Clean
  • Type safety - Clean
  • Documentation accuracy - Clean
  • Test coverage - Adequate
  • Code clarity - Good

Automated review by Codex AI

@ding113 ding113 merged commit 5190691 into dev Jan 3, 2026
17 checks passed
@github-project-automation github-project-automation bot moved this from Backlog to Done in Claude Code Hub Roadmap Jan 3, 2026
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • PR #521 classified as S (55 lines changed, 2 files) and labeled size/S.
  • Reviewed the diff in src/app/v1/_lib/codex/session-extractor.ts and src/app/v1/_lib/codex/__tests__/session-extractor.test.ts; no significant issues found within the changed lines.
  • Submitted the required summary review comment on the PR via gh pr review.

@github-actions github-actions bot mentioned this pull request Jan 3, 2026
@ding113 ding113 deleted the fix/codex-session-id-prompt-cache-key branch January 27, 2026 09:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:core area:session bug Something isn't working size/S Small PR (< 200 lines)

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant

Comments