[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"project-92668":3},{"id":4,"name":5,"fullName":6,"owner":7,"repo":5,"description":8,"homepage":9,"htmlUrl":9,"language":10,"languages":9,"totalLinesOfCode":9,"stars":11,"forks":12,"watchers":13,"openIssues":14,"contributorsCount":15,"subscribersCount":15,"size":15,"stars1d":15,"stars7d":15,"stars30d":14,"stars90d":15,"forks30d":15,"starsTrendScore":15,"compositeScore":16,"rankGlobal":9,"rankLanguage":9,"license":17,"archived":18,"fork":18,"defaultBranch":19,"hasWiki":20,"hasPages":18,"topics":21,"createdAt":9,"pushedAt":9,"updatedAt":22,"readmeContent":23,"aiSummary":24,"trendingCount":15,"starSnapshotCount":15,"syncStatus":25,"lastSyncTime":26,"discoverSource":27},92668,"cpa-plugin-codexcomp","uf-hy\u002Fcpa-plugin-codexcomp","uf-hy","CLIProxyAPI plugin that detects and repairs gpt-5.5 reasoning truncation (518n−2 pattern) via encrypted_content replay and multi-round folding",null,"Go",65,7,51,1,0,42.81,"MIT License",false,"master",true,[],"2026-07-22 04:02:06","# CPA 插件：CodexComp\n\n[![Go 1.26+](https:\u002F\u002Fimg.shields.io\u002Fbadge\u002FGo-1.26%2B-00ADD8?logo=go)](https:\u002F\u002Fgo.dev\u002F)\n[![License: MIT](https:\u002F\u002Fimg.shields.io\u002Fbadge\u002FLicense-MIT-yellow.svg)](LICENSE)\n\n[简体中文](README.md) | [English](README_EN.md)\n\n[CLIProxyAPI](https:\u002F\u002Fgithub.com\u002Frouter-for-me\u002FCLIProxyAPI) 插件，检测并修复可配置模型（默认 `gpt-5.5` 和 `gpt-5.6-luna`）流式请求中的推理截断，以减少偶发的模型降智，支持 OpenAI Responses API、OpenAI Chat Completions API 和 Anthropic Messages API 三种客户端协议\n\ngpt-5.5 在 agent 场景下推理 token 会精确停在 518n−2（516、1034、1552……），这个截断会导致意料之外的降智问题。使用插件检测到该种截断后，通过 encrypted_content 重放自动续写推理，并将多轮折叠为单个响应，对下游客户端完全透明。\n\n## 快速安装（Agent）\n\n如果你使用 AI agent（自动化代理，如 Codex、Claude Code 等），把下面这段提示词发给它：\n\n```\n请帮我安装 CPA 插件 codexcomp。安装说明在 https:\u002F\u002Fgithub.com\u002Fuf-hy\u002Fcpa-plugin-codexcomp\u002Fblob\u002Fmaster\u002FSETUP.md ，请先读取这个文档再执行安装。\n```\n\n人类手动安装请见 [安装章节](#安装)。\n\n## 工作原理\n\n插件通过 CPA 的 C ABI 插件系统拦截配置列表中的模型（默认 `gpt-5.5` 和 `gpt-5.6-luna`）流式请求，内部以 codex 格式与上游通信，每当上游完成后检查 reasoning_tokens 是否匹配 518n−2 模式。若匹配且存在 encrypted_content，则触发续写\n\n续写轮重放原始 input 加上之前所有思考内容和一条提示消息使得模型从截断点继续而非重来\n\n默认情况设定为最多 3 轮续写, 以通过可能的更多耗时来换取相对提升的思考长度和时间进而提升模型智力\n\n### 异步流式与首字节延迟\n\ngpt-5.5 high reasoning effort 模式下，模型可能需要 25-30 秒才产生第一个 SSE 事件。很多客户端（包括 Codex CLI）会在 10 秒时超时。插件使用 CPA 的异步流式模式：响应头立即返回，由 goroutine 处理折叠逻辑。上游事件一到就转发给客户端，简单问题首字节实测低于 500ms，复杂推理也会在上游产出第一个事件后立即转发。\n\n## 接管范围\n\n插件只拦截**同时满足以下条件**的请求：\n\n- 模型位于 `models` 配置列表中（默认 `gpt-5.5` 和 `gpt-5.6-luna`）\n- 客户端协议为 `openai-response`（Responses API）、`openai`（Chat Completions API）或 `claude`（Anthropic Messages API）\n- 流式请求（`stream: true`）\n- 不含 `previous_response_id`\n- `input` 为数组格式（仅 `openai-response` 协议要求；字符串格式的 `input` 会直接透传，不触发续写）\n\n插件内部统一以 codex 格式与上游通信，由 CPA 的适配层自动完成客户端协议与 codex 之间的双向翻译，对下游客户端完全透明。其他请求全部透传给 CPA 正常处理。\n\n## 与 codexcomp \u002F CodexCont 的区别\n\n| | [CodexCont](https:\u002F\u002Fgithub.com\u002Fneteroster\u002FCodexCont) | [codexcomp](https:\u002F\u002Fgithub.com\u002Fdzshzx\u002Fcodexcomp) | 本插件 |\n|---|---|---|---|\n| **语言** | Python (Starlette\u002Fuvicorn) | Python (uv) | Go (C ABI 共享库) |\n| **部署** | 独立本地代理 (127.0.0.1:8787) | 独立本地代理 (127.0.0.1:8787) | CPA 插件（进程内加载） |\n| **集成** | 手动改 `openai_base_url` | 手动改 `openai_base_url` | CPA 自动路由，无需改配置 |\n| **传输** | HTTP\u002FSSE | WebSocket + SSE 回退 | CPA 宿主模型流（`host.model.execute_stream`） |\n| **递归规避** | 不适用（独立进程） | 不适用（独立进程） | `host_callback_id` 跳过自身路由\u002F拦截器 |\n| **并发** | 单进程 | 单进程 | CPA 管理，每请求一个 goroutine |\n| **配置** | `config.toml` | 零配置（uv tool） | 默认零配置，可选调试参数（C ABI 自注册） |\n| **折叠逻辑** | 最初的 `518n−2` 检测 + 续写 | 改进的折叠（传输无关） | codexcomp `fold.py` 的 Go 移植 |\n\nCodexCont 是最初的续写机制。codexcomp 将其改进为传输无关的折叠。本插件将折叠逻辑移植到 Go 并直接集成到 CPA 插件系统中，无需独立代理进程。\n\n## 安装\n\n### 方式一：CPA WebUI 安装（推荐）\n\n在 CPA 的 WebUI 中添加本仓库为插件源：\n\n1. 打开 **配置面板 → 可视化编辑 → 完整 → 高级与实验 → 插件**\n2. 确保插件系统为开启\n3. 在「第三方插件源」里点击「添加」，填入：\n\n```\nhttps:\u002F\u002Fraw.githubusercontent.com\u002Fuf-hy\u002Fcpa-plugin-codexcomp\u002Fmaster\u002Fregistry.json\n```\n\n4. 在最下方点击对钩保存更改即可热重载\n5. 随后在插件商店页面找到 CodexComp（可用搜索功能），点击安装即可\n\nCPA 会自动下载对应系统和架构的动态库、校验 SHA256、热重载，通常无需再次重启。Enjoy it!\n\n### 方式二：手动安装\n\n从 [Releases](https:\u002F\u002Fgithub.com\u002Fuf-hy\u002Fcpa-plugin-codexcomp\u002Freleases\u002Flatest) 下载对应平台的 zip 包，解压后放到 `plugins\u002F` 目录：\n\n```bash\n# Linux：确认架构后下载对应 zip，解压到 plugins 目录\nmkdir -p \u003CCPA_DIR>\u002Fplugins\nunzip -o codexcomp_\u003Cversion>_linux_\u003Camd64|arm64>.zip -d \u003CCPA_DIR>\u002Fplugins\u002F\n```\n\nWindows 原生部署目前提供 amd64 成品，可在 PowerShell 中解压：\n\n```powershell\nNew-Item -ItemType Directory -Force -Path '\u003CCPA_DIR>\\plugins' | Out-Null\nExpand-Archive -LiteralPath 'codexcomp_\u003Cversion>_windows_amd64.zip' -DestinationPath '\u003CCPA_DIR>\\plugins' -Force\n```\n\n在 `config.yaml` 中启用插件：\n\n```yaml\nplugins:\n  enabled: true\n  dir: plugins\n  configs:\n    codexcomp:\n      enabled: true\n      priority: 1\n```\n\n如果使用 Docker，在 `docker-compose.yml` 中挂载插件目录：\n\n```yaml\nvolumes:\n  - .\u002Fplugins:\u002FCLIProxyAPI\u002Fplugins\n```\n\n重启 CPA。\n\n更适合 AI agent（自动化代理）的完整步骤见 [SETUP.md](SETUP.md)。\n\n### 从源码编译\n\n`go.mod` 中有 `replace` 指令指向相邻目录的 CLIProxyAPI，需要先 clone 依赖仓库：\n\n```bash\ngit clone https:\u002F\u002Fgithub.com\u002Frouter-for-me\u002FCLIProxyAPI.git ..\u002FCLIProxyAPI\n# Linux\ngo build -buildmode=c-shared -o codexcomp.so .\n```\n\nWindows 原生编译需要启用 CGO 并安装 GCC：\n\n```powershell\ngit clone 'https:\u002F\u002Fgithub.com\u002Frouter-for-me\u002FCLIProxyAPI.git' '..\\CLIProxyAPI'\n$env:CGO_ENABLED = '1'\ngo build -buildmode=c-shared -o codexcomp.dll .\n```\n\n## 配置\n\n默认无需额外配置。插件通过 C ABI 自注册，自动路由。需要做 A\u002FB 测试或排障时，可以打开下面这些参数。\n\n```yaml\nplugins:\n  configs:\n    codexcomp:\n      models:\n        - gpt-5.5\n        - gpt-5.6-luna\n      marker_text: \"Continue thinking...\"\n      max_continue: 3\n      max_tier_n: 6\n      debug_log: false\n```\n\n`marker_text` 是检测到截断后插入的续写提示，默认值是 `Continue thinking...`。\n\n`max_continue` 是最多续写轮数，默认 3；设为 0 可以临时禁用续写，只保留接管和元数据路径，方便对比。\n\n`max_tier_n` 是允许续写的最大截断层级，默认 6；设为 0 表示不限制上限。\n\n`debug_log` 会通过 CPA 的 host log（宿主日志）输出配置和续写轮次信息，默认关闭，排障时再开。\n\n`models` 是要拦截的模型标识符列表，默认接管 `gpt-5.5` 和 `gpt-5.6-luna`。已观测到 Luna 仍存在 516 截断现象，因此加入默认列表；Terra 和 Sol 暂未观测到相同问题，不默认接管。如果后续遇到截断，可以自行加入配置：\n\n```yaml\nplugins:\n  configs:\n    codexcomp:\n      models:\n        - gpt-5.5\n        - gpt-5.6-luna\n        - gpt-5.6-terra\n        - gpt-5.6-sol\n```\n\n**实验功能，不建议使用：实际测试中效果不理想，续写轮经常只增加很少甚至 0 个推理 token。** `min_reasoning_tokens` 是可选的每模型最小推理 token 阈值配置，默认不启用、没有默认阈值。配置后会作用于该模型的所有被接管请求，不区分请求中的 reasoning effort（推理强度）；所有已折叠轮次的推理 token 总数低于配置值时，插件会尝试触发续写。键为精确模型标识符（如 `gpt-5.6-luna`），值为非负整数阈值。例如：\n\n```yaml\nplugins:\n  configs:\n    codexcomp:\n      min_reasoning_tokens:\n        gpt-5.6-luna: 1200\n```\n\n这会使 `gpt-5.6-luna` 在总推理 token 少于 1200 时尝试续写，直到达到阈值、达到 `max_continue` 限制，或上游不再返回 `encrypted_content`。它不能保证最终达到阈值；此触发条件与原有的 `518n-2` 截断触发条件是或（OR）关系。\n\n### 直连 CPA 的稳定缓存\n\n如果客户端直接调用 CPA 的 OpenAI 兼容接口（`\u002Fv1\u002Fchat\u002Fcompletions` 或 `\u002Fv1\u002Fresponses`），建议每个会话都带上稳定的 `X-CPA-Session-Id` 请求头：\n\n```http\nX-CPA-Session-Id: your-stable-session-id\n```\n\n这个值用于生成上游 `prompt_cache_key`，让同一会话里的多轮请求稳定命中提示缓存。插件也兼容 `X-CodexComp-Session-Id` 和旧的 `X-Claude-Code-Session-Id`，但新接入建议使用 `X-CPA-Session-Id`。\n\n如果想尝试更强的续写提示，可以参考 [openai\u002Fcodex#30364 的相关讨论](https:\u002F\u002Fgithub.com\u002Fopenai\u002Fcodex\u002Fissues\u002F30364#issuecomment-4828984707)，把 `marker_text` 换成 `Spend time on thinking; you do not need to use the commentary channel to report progress to me.`。它更明确地要求模型把时间花在 thinking（思考）上，不要把 commentary channel（进度汇报通道）用于报告进度。不同客户端和任务里的效果可能不同，建议按自己的场景测试后再启用。\n\n## Metadata 注入\n\n最终 `response.completed` 事件包含：\n\n- `metadata.proxy_rounds` — 每轮信息（轮次号、推理 token 数、截断层级 `n`；触发续写的轮次会额外包含 `continue_reason`，例如 `truncation` 或 `low_reasoning_tokens`）\n- `metadata.proxy_billed_usage` — 所有轮次的合计用量\n- `metadata.proxy_stopped_reason` — 非自然停止时非空（`no_encrypted_content`、`max_continue`、`tier_out_of_window`）\n\n> **注意**：以上 metadata 字段仅在 `openai-response`（Responses API）协议下保证可见。对于 `openai`（Chat Completions API）和 `claude`（Anthropic Messages API）协议，CPA 的协议翻译层不会传递 `response.completed` 中的 metadata，因此客户端无法获取 `proxy_rounds` 等字段。续写功能本身在所有协议下均正常工作，只是诊断信息仅在 Responses API 中可观测。\n\n## 基准测试\n\n使用 [codex-candy-eval](https:\u002F\u002Fgithub.com\u002Fhaowang02\u002Fcodex-candy-eval) 的糖果问题——一个触发 gpt-5.5 截断的推理深度测试。正确答案为 21。\n\n仓库内置了多协议测试脚本 `scripts\u002Fcandy_eval.py`，支持 `openai-response`、`openai-chat`、`anthropic` 三种客户端协议，可并发测试：\n\n**命令**：\n\n```bash\npython3 scripts\u002Fcandy_eval.py \\\n  --url http:\u002F\u002Fyour-cpa:port --key YOUR_KEY \\\n  --protocol openai-response --rounds 5 -r high\n```\n\n### 无插件（baseline）\n\n| 运行 | 推理 Token | 答案 | 正确 |\n|------|-----------|------|------|\n| 1    | 516       | 29   | ✗    |\n| 2    | 516       | 29   | ✗    |\n| 3    | 1552      | 21   | ✓    |\n| 4    | 516       | 29   | ✗    |\n| 5    | 3069      | 21   | ✓    |\n\n**准确率：2\u002F5 (40%)** — 5 个响应中有 3 个在 516 token 处截断（n=1），导致答错。\n\n### 有插件\n\n| 运行 | 推理 Token | 答案 | 正确 |\n|------|-----------|------|------|\n| 1    | 3641      | 21   | ✓    |\n| 2    | 2059      | 21   | ✓    |\n| 3    | 3273      | 21   | ✓    |\n| 4    | 4555      | 21   | ✓    |\n| 5    | 3100      | 21   | ✓    |\n\n**准确率：5\u002F5 (100%)** — 所有截断均被检测并续写，答案全部正确。\n\n## 免责声明\n\n本插件依赖非契约的模型行为（`518n−2` 截断模式和 `encrypted_content` 字段）。如果 OpenAI 改变截断模式或移除 `encrypted_content`，插件将不再触发，变为透明透传。续写轮消耗真实 token，总量记录在 `metadata.proxy_billed_usage` 中。\n\n## 致谢\n\n- **[CodexCont](https:\u002F\u002Fgithub.com\u002Fneteroster\u002FCodexCont)**（MIT）— 最初的续写机制，识别了 `518n−2` 截断模式并开创了 `encrypted_content` 重放方案\n- **[codexcomp](https:\u002F\u002Fgithub.com\u002Fdzshzx\u002Fcodexcomp)**（MIT）— 改进的传输无关折叠算法；本插件直接移植自其 `fold.py`\n- **[codex-candy-eval](https:\u002F\u002Fgithub.com\u002Fhaowang02\u002Fcodex-candy-eval)** — 本 README 使用的推理深度基准测试\n- **[CLIProxyAPI](https:\u002F\u002Fgithub.com\u002Frouter-for-me\u002FCLIProxyAPI)** — 插件宿主框架，使进程内拦截成为可能\n- **[LINUX DO](https:\u002F\u002Flinux.do)** 社区 — gpt-5.5 “516” 推理截断\u002F降智问题的主要讨论、定位与验证现场；感谢社区成员提供复现样例、部署反馈和测试验证\n\n## 许可证\n\nMIT。本插件包含来自 [CodexCont](https:\u002F\u002Fgithub.com\u002Fneteroster\u002FCodexCont) 和 [codexcomp](https:\u002F\u002Fgithub.com\u002Fdzshzx\u002Fcodexcomp) 的派生代码，详见 [THIRD_PARTY_NOTICES.md](THIRD_PARTY_NOTICES.md)。\n","这是一个专为CLIProxyAPI（CPA）设计的Go语言插件，用于检测并修复gpt-5.5等模型在流式推理中因token数精确停在518n−2模式（如516、1034）导致的推理截断问题。插件通过拦截符合特定条件的OpenAI\u002FAnthropic协议流式请求，利用encrypted_content重放机制自动续写，并将多轮响应折叠为单次透明输出，同时支持异步流式转发以规避首字节超时。适用于AI Agent、自动化代码生成、复杂推理任务等对模型思维连贯性要求较高的场景，无需修改客户端配置，直接集成于CPA进程内运行。",2,"2026-07-10 02:30:08","CREATED_QUERY"]