feat(claude): add native Codex compaction bridge(为Claude通道引入Codex原生上下文压缩) - #4465
Johnnybyzhang wants to merge 9 commits into
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces a Claude-to-Codex responses bridge and compaction mechanism, allowing Claude /messages requests to use the Codex Responses API while maintaining Claude-compatible responses. It adds support for token estimation, context window validation, live streaming usage estimation, and compaction capsule encoding/decoding. The review feedback highlights several places in the newly added handlers (responses_bridge.go and compact_bridge.go) where defensive nil checks should be added for the returned response or stream objects to prevent potential nil pointer dereferences.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
|
补充一次真实 Claude App 长会话验证:前两次自动压缩可以正常继续,但第三、第四次压缩虽然成功,随后的普通请求仍会收到 根因不是 capsule 损坏或旧压缩窗口被重复追加。
本地 focused tests、race tests、 |
36f8d78 to
5bd0715
Compare
|
测试了,完全不可用,使用 cpa_responses_compaction 插入到普通消息上下文,导致后续处理不正常。
原本 claude 本身没有压缩协议,如果要实现 codex 的压缩,最优方案就是 CPA 服务端缓存 |
5bd0715 to
77314b7
Compare
77314b7 to
dba5cd2
Compare
dbe0f5d to
f309f9b
Compare
f309f9b to
a9c897e
Compare
摘要
为 Claude
/v1/messages增加一条面向 Codex Responses API 的协议桥接路径,让 Claude App、Claude Code 等客户端继续按 Claude Messages 协议工作,同时复用 CPA 现有的 Codex OAuth、模型路由和上游连接。这条路径不引入新的服务,也不增加 endpoint 或 target model 配置。CPA 仍然只是路由器:普通请求发往当前 Codex 上游的
/responses;当 Claude 自己发出自动压缩请求时,同一条路由直接改用该上游的/responses/compact。上游模型沿用请求解码后的模型,不做额外覆盖。背景
Claude 客户端会根据流式 usage 判断上下文消耗,并自行决定何时压缩、重放和继续会话。Codex Responses 流此前只有结束事件携带完整 usage;长时间运行的主会话或 worker 在执行过程中看不到持续增长的 token 数,容易在客户端尚未触发自动压缩前耗尽上下文。
此外,
/responses/compact返回的压缩状态包含不透明加密数据,后续重放必须继续使用生成该状态的原始 Codex 凭据。普通请求不应因此被固定到某个凭据,只有携带压缩状态的续接请求需要保持 auth affinity。实现
/v1/messages→ Codex Responses/responses,再转换为 Claude Messages 响应,并把客户端看到的模型 ID 恢复为原值。Claude 驱动的自动压缩
/responses,避免误判。/responses/compact。/responses/compact的结果,再按 Claude SSE 事件序列输出。运行中的 token usage
message_start提供本地计算的输入 token 数,供 Claude 从响应开始阶段就建立上下文基线。message_delta.usage.output_tokens,覆盖文本、工具参数和 reasoning summary 等增量。output_tokens_details.thinking_tokens;如果客户端启用 thinking-token-count beta,也会发送符合 Claude 事件形状的estimated_tokens。(仍在测试中)response.completed中的上游 usage 始终覆盖本地估算,作为本轮最终准确值;Codex 内部 prompt cache 不会伪装成 Anthropic cache-control usage。context_too_large,由 Claude 客户端执行原生 compact-and-retry。稳定性修正
CountTokens保持精确计数;用于 bridge 上下文预算的 256-token 保守 framing allowance 不再污染公开结果。用户侧效果
/responses/compact和安全重放。验证
覆盖范围包括 handler 路由、compact 检测与重放、HTTP/SSE、WebSocket、usage 累计、reasoning token 估算、取消清理、精确 CountTokens 和 HTTP fallback。
本地端到端验证使用未修改的 Claude Code
2.1.211和 CPA localhost 实例,通过程序化工具调用持续生成上下文;测试观察到tool_calls=6、compact_boundaries=1,最终输出CLAUDE_NATIVE_AUTOCOMPACT_E2E_OK。测试期间只降低了触发阈值,没有修改 Claude 客户端。范围
本 PR 只修改 Claude/Codex bridge 所需的 handler、executor、translator、usage helper 及对应测试,共 13 个文件;不涉及文档、模型目录或其他 provider 的改动。