codex-516-guard 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,40 @@
1
+ name: release
2
+
3
+ # Build and publish to PyPI on a version tag, using PyPI Trusted Publishing
4
+ # (OIDC) — no API token stored anywhere. Configure the trusted publisher once
5
+ # at https://pypi.org/manage/project/codex-516-guard/settings/publishing/
6
+ # (or as a pending publisher before the first release):
7
+ # owner: dzshzx repo: codex-516-guard workflow: release.yml environment: pypi
8
+
9
+ on:
10
+ push:
11
+ tags:
12
+ - "v*"
13
+
14
+ jobs:
15
+ build:
16
+ runs-on: ubuntu-latest
17
+ steps:
18
+ - uses: actions/checkout@v4
19
+ - name: Install uv
20
+ uses: astral-sh/setup-uv@v5
21
+ - name: Build sdist + wheel
22
+ run: uv build
23
+ - uses: actions/upload-artifact@v4
24
+ with:
25
+ name: dist
26
+ path: dist/
27
+
28
+ publish:
29
+ needs: build
30
+ runs-on: ubuntu-latest
31
+ environment: pypi
32
+ permissions:
33
+ id-token: write # required for Trusted Publishing (OIDC)
34
+ steps:
35
+ - uses: actions/download-artifact@v4
36
+ with:
37
+ name: dist
38
+ path: dist/
39
+ - name: Publish to PyPI
40
+ uses: pypa/gh-action-pypi-publish@v1.14.0
@@ -0,0 +1,5 @@
1
+ .venv/
2
+ __pycache__/
3
+ *.pyc
4
+ *.log
5
+ dist/
@@ -0,0 +1,27 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 dzshzx
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
23
+ ---
24
+
25
+ Mechanism inspiration: the 518n-2 truncation detection + fold-continuation
26
+ approach originates from neteroster/CodexCont (MIT). This project is an
27
+ independent, from-scratch implementation.
@@ -0,0 +1,202 @@
1
+ Metadata-Version: 2.4
2
+ Name: codex-516-guard
3
+ Version: 0.1.0
4
+ Summary: Local Responses proxy for OpenAI Codex CLI: folds gpt-5.5 518n-2 reasoning truncation (516 degradation) via the official openai_base_url wiring — no provider change, WebSocket-first, no fallback noise.
5
+ Project-URL: Homepage, https://github.com/dzshzx/codex-516-guard
6
+ Project-URL: Repository, https://github.com/dzshzx/codex-516-guard
7
+ Project-URL: Issues, https://github.com/dzshzx/codex-516-guard/issues
8
+ Author: dzshzx
9
+ License-Expression: MIT
10
+ License-File: LICENSE
11
+ Keywords: 516,codex,gpt-5.5,openai,proxy,reasoning
12
+ Classifier: Development Status :: 4 - Beta
13
+ Classifier: Environment :: Console
14
+ Classifier: Intended Audience :: Developers
15
+ Classifier: Operating System :: OS Independent
16
+ Classifier: Programming Language :: Python :: 3.12
17
+ Classifier: Programming Language :: Python :: 3.13
18
+ Classifier: Programming Language :: Python :: 3.14
19
+ Classifier: Topic :: Internet :: Proxy Servers
20
+ Requires-Python: >=3.12
21
+ Requires-Dist: httpx>=0.27
22
+ Requires-Dist: starlette>=0.41
23
+ Requires-Dist: uvicorn[standard]>=0.32
24
+ Requires-Dist: zstandard>=0.23
25
+ Description-Content-Type: text/markdown
26
+
27
+ # codex-516-guard
28
+
29
+ > Local Responses proxy for OpenAI Codex CLI: detects the gpt-5.5 "516" reasoning-truncation
30
+ > fingerprint (`reasoning_tokens == 518*n - 2`), auto-continues the model's thinking, and folds
31
+ > all rounds into one response — **without changing `model_provider`**, so session grouping,
32
+ > remote compaction and remote-control stay intact. WebSocket-first: no
33
+ > "Falling back from WebSockets" retry noise.
34
+
35
+ 自研本地 Responses 代理,缓解 Codex gpt-5.5 的「516 降智」:思考在
36
+ `reasoning_tokens == 518*n - 2`(516、1034、1552…)处被截断,答案质量骤降
37
+ (上游 issue:[openai/codex#30364](https://github.com/openai/codex/issues/30364),无官方修复)。
38
+ 本代理检测该指纹后自动让模型继续思考,并把多轮续写**折叠为单个下游响应**。
39
+
40
+ 机制思路来自 [neteroster/CodexCont](https://github.com/neteroster/CodexCont)(MIT),
41
+ 实现为全新代码。与其关键差异:
42
+
43
+ | | codex-516-guard | CodexCont |
44
+ | --- | --- | --- |
45
+ | Codex 侧接线 | 顶层 `openai_base_url`(**不新建 provider**) | 新建 `[model_providers]`(会话按 provider 分组被隐藏、remote-control 不可用、丢远程压缩) |
46
+ | 下游传输 | **WebSocket 第一传输**(完整实现 `responses_websockets` 协议)+ SSE 兜底 | 仅 SSE(codex 先试 ws → 405 → 每会话约 5 次重连告警后回退) |
47
+ | zstd 请求压缩(0.142.x 内置 provider 默认开) | 原生解压,无需改 codex 配置 | 需 `[features] enable_request_compression = false` |
48
+ | `GET /v1/models` 模型目录刷新 | `/v1/*` 透传 | 未代理(静默失败,靠本地缓存) |
49
+ | 续写方法 | commentary 法(`phase:"commentary"` 消息 + encrypted reasoning 重放) | commentary + tool_pair legacy + 跨轮 repair 等更多可配置项 |
50
+
51
+ ## 原理
52
+
53
+ 1. 上游每轮结束时读取 `usage.output_tokens_details.reasoning_tokens`,命中 `518n-2`(n∈[1,6],最多续写 3 轮)即判定思考被截断;
54
+ 2. 丢弃该轮的**暂定输出**(message / tool calls——它们基于被截断的思考),把该轮 reasoning items(含 `encrypted_content`)+ 一条 `Continue thinking...` 的 `phase:"commentary"` 助手消息追加进 input 重放,开下一轮;
55
+ 3. 思考流实时透传给 agent,只有干净收尾那一轮的最终输出被放行;terminal 事件重建为单响应口径的 usage(input 取第 1 轮防止「假爆上下文」,reasoning 求和),真实累计成本记在 `metadata.proxy_billed_usage`。
56
+
57
+ ## 安装
58
+
59
+ 要求:[uv](https://docs.astral.sh/uv/)(自带 Python 管理)、Codex CLI(ChatGPT OAuth 登录,0.142.x 实测)。
60
+
61
+ ```bash
62
+ uv tool install codex-516-guard # 从 PyPI 安装
63
+ # 或直接从源码仓库:
64
+ # uv tool install git+https://github.com/dzshzx/codex-516-guard
65
+ ```
66
+
67
+ uv 会建一个隔离环境并把可执行文件放进 uv 的 bin 目录(Unix/macOS 默认 `~/.local/bin`,
68
+ Windows 用 `where.exe codex-516-guard` 查实际路径;`uv tool update-shell` 可把该目录加进 PATH)。
69
+ 之后:
70
+
71
+ ```bash
72
+ codex-516-guard # 前台跑起(默认 127.0.0.1:8787)
73
+ codex-516-guard --port 8790 --log-level debug # 可选参数:--host/--port/--upstream/--log-level
74
+ ```
75
+
76
+ 升级 / 卸载:`uv tool upgrade codex-516-guard` / `uv tool uninstall codex-516-guard`。
77
+
78
+ Codex 侧接线——`~/.codex/config.toml` 顶层(必须在第一个 `[table]` 之前)加一行:
79
+
80
+ ```toml
81
+ openai_base_url = "http://127.0.0.1:8787/v1"
82
+ ```
83
+
84
+ 这是覆盖内置 openai provider base_url 的**官方 config key**
85
+ ([#16719](https://github.com/openai/codex/issues/16719);同名 `[model_providers.openai]`
86
+ 覆盖被维护者拒绝,`OPENAI_BASE_URL` 环境变量已移除)。provider id 保持 `openai`,
87
+ 因此会话历史分组、远程压缩、remote-control 均不受影响。
88
+
89
+ **关闭**:注释掉 `openai_base_url` 行 + 停掉代理进程。代理停止而 key 在位时,Codex 会因上游不可达报错。
90
+
91
+ ## 开机自启动
92
+
93
+ 代理是**用户会话内**被 Codex 调用的回环服务,所以三平台都选「随用户登录启动、跑在用户上下文」的方式
94
+ (而不是系统级服务——系统服务跑在无用户环境的 session 里,够不到用户 profile 下的 uv 可执行文件与代理设置)。
95
+ 先用 `which codex-516-guard`(Unix/macOS)或 `where.exe codex-516-guard`(Windows)拿到绝对路径备用。
96
+
97
+ ### Linux / WSL — systemd user unit
98
+
99
+ 见 `systemd/codex-516-guard.service.example`:
100
+
101
+ ```bash
102
+ cp systemd/codex-516-guard.service.example ~/.config/systemd/user/codex-516-guard.service
103
+ systemctl --user daemon-reload && systemctl --user enable --now codex-516-guard
104
+ ```
105
+
106
+ ### macOS — launchd LaunchAgent
107
+
108
+ macOS 用 launchd 管理后台任务。放在 `~/Library/LaunchAgents/` 的是 **LaunchAgent**,随**用户登录**启动、
109
+ 跑在用户 GUI session 里(对回环代理正确的选择);`/Library/LaunchDaemons/` 里的 LaunchDaemon 开机即起但无用户会话,本场景不适用。
110
+
111
+ 把可执行文件绝对路径填进下面的 plist,存为 `~/Library/LaunchAgents/com.dzshzx.codex-516-guard.plist`:
112
+
113
+ ```xml
114
+ <?xml version="1.0" encoding="UTF-8"?>
115
+ <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
116
+ <plist version="1.0">
117
+ <dict>
118
+ <key>Label</key> <string>com.dzshzx.codex-516-guard</string>
119
+ <key>ProgramArguments</key>
120
+ <array>
121
+ <string>/Users/YOU/.local/bin/codex-516-guard</string>
122
+ </array>
123
+ <key>RunAtLoad</key> <true/>
124
+ <key>KeepAlive</key> <true/>
125
+ <key>StandardOutPath</key> <string>/tmp/codex-516-guard.log</string>
126
+ <key>StandardErrorPath</key><string>/tmp/codex-516-guard.log</string>
127
+ </dict>
128
+ </plist>
129
+ ```
130
+
131
+ 注意:launchd 不读 shell 配置,`ProgramArguments` 必须是**绝对路径**;崩溃后 `KeepAlive` 会重启(10 秒节流)。
132
+ 加载 / 停用(现代 `launchctl`,`load`/`unload` 已是 legacy):
133
+
134
+ ```bash
135
+ launchctl bootstrap gui/$(id -u) ~/Library/LaunchAgents/com.dzshzx.codex-516-guard.plist
136
+ launchctl enable gui/$(id -u)/com.dzshzx.codex-516-guard
137
+ launchctl kickstart -k gui/$(id -u)/com.dzshzx.codex-516-guard # 立即(重)启动
138
+ launchctl bootout gui/$(id -u)/com.dzshzx.codex-516-guard # 卸载
139
+ ```
140
+
141
+ ### Windows — 用「登录时触发」的计划任务(不是系统服务)
142
+
143
+ 用户常问能否做成 Windows **service**:可以,但原生 service 要求程序实现 SCM 控制协议,
144
+ `sc.exe create` 直接指向普通控制台程序会在启动时被判定超时(错误 1053),必须套 WinSW / NSSM 之类 wrapper。
145
+ 即便套上,service 默认跑在 **session 0 + SYSTEM 账户**,没有你的用户环境、代理设置,也不便访问用户 profile 下 uv 装的可执行文件。
146
+
147
+ 因此本场景**推荐计划任务(onlogon)**:随你登录启动、带完整用户环境和 PATH,正好匹配「用户会话内被调用的回环代理」。
148
+ 用 `where.exe codex-516-guard` 拿到路径后(PowerShell):
149
+
150
+ ```powershell
151
+ $exe = (Get-Command codex-516-guard).Source
152
+ schtasks /create /tn "codex-516-guard" /tr "`"$exe`"" /sc onlogon /rl limited /f
153
+ schtasks /run /tn "codex-516-guard" # 立即启动一次
154
+ # 删除:schtasks /delete /tn "codex-516-guard" /f
155
+ ```
156
+
157
+ 如确实要开机即起(未登录也运行)的系统服务形态,用 [WinSW](https://github.com/winswhq/winsw)(MIT)
158
+ 把本 exe 包成服务;注意需让服务以你的用户账户运行,否则够不到用户 profile 下的安装。
159
+
160
+ ## 验证
161
+
162
+ ```bash
163
+ curl -sS http://127.0.0.1:8787/healthz # {"ok":true,...}
164
+ journalctl --user -u codex-516-guard -f | grep -E 'round|done' # Linux/WSL;mac 看 plist 日志文件
165
+ ```
166
+
167
+ 命中折叠时的日志(实测样例,连环双 516 被击破、答案正确):
168
+
169
+ ```
170
+ round 1: in=21550 out=664 reason=516 total=22214 | n=1 buffered=['function_call'] -> continue
171
+ round 2: in=22078 out=652 reason=516 total=22730 | n=1 buffered=['function_call'] -> continue
172
+ round 3: in=22606 out=566 reason=291 total=23172 | n=None buffered=[...] -> clean
173
+ done: 3 round(s) | ... | status=completed stop=natural
174
+ ```
175
+
176
+ ## 开发
177
+
178
+ ```bash
179
+ git clone https://github.com/dzshzx/codex-516-guard && cd codex-516-guard
180
+ uv sync
181
+ uv run python test_fold.py # 折叠状态机自测,应输出 ALL PASS
182
+ uv run codex-516-guard # 本地跑
183
+ ```
184
+
185
+ 发布走 PyPI Trusted Publishing(`.github/workflows/release.yml`,OIDC,无 token):推 `v*` tag 即自动构建上传。
186
+
187
+ ## 结构
188
+
189
+ - `guard/fold.py` — 指纹检测 + 折叠状态机(传输无关;`test_fold.py` 覆盖丢弃/放行、重编号、双口径 usage)
190
+ - `guard/server.py` — starlette 传输层:ws / SSE 下游、SSE 上游、zstd/gzip 请求解压、`/v1/*` 透传
191
+ - `guard/cli.py` — CLI 入口(`codex-516-guard`;仅监听回环;auth passthrough,不存储任何凭据)
192
+
193
+ ## 安全与免责
194
+
195
+ - 代理只做 auth **passthrough**:转发 Codex 发来的 Authorization 头,不读取、不落盘任何凭据。
196
+ - 仅监听回环地址;不要暴露到非回环接口。
197
+ - 非官方项目,依赖上游未公开的行为(截断指纹、ws 帧格式),OpenAI 侧变更可能使其失效;使用风险自负。
198
+ - 续写会产生额外的真实 token 消耗(见 `metadata.proxy_billed_usage`),guard 以 n 窗口 + 3 轮上限约束。
199
+
200
+ ## License
201
+
202
+ MIT(见 LICENSE;机制思路 credit neteroster/CodexCont)。
@@ -0,0 +1,176 @@
1
+ # codex-516-guard
2
+
3
+ > Local Responses proxy for OpenAI Codex CLI: detects the gpt-5.5 "516" reasoning-truncation
4
+ > fingerprint (`reasoning_tokens == 518*n - 2`), auto-continues the model's thinking, and folds
5
+ > all rounds into one response — **without changing `model_provider`**, so session grouping,
6
+ > remote compaction and remote-control stay intact. WebSocket-first: no
7
+ > "Falling back from WebSockets" retry noise.
8
+
9
+ 自研本地 Responses 代理,缓解 Codex gpt-5.5 的「516 降智」:思考在
10
+ `reasoning_tokens == 518*n - 2`(516、1034、1552…)处被截断,答案质量骤降
11
+ (上游 issue:[openai/codex#30364](https://github.com/openai/codex/issues/30364),无官方修复)。
12
+ 本代理检测该指纹后自动让模型继续思考,并把多轮续写**折叠为单个下游响应**。
13
+
14
+ 机制思路来自 [neteroster/CodexCont](https://github.com/neteroster/CodexCont)(MIT),
15
+ 实现为全新代码。与其关键差异:
16
+
17
+ | | codex-516-guard | CodexCont |
18
+ | --- | --- | --- |
19
+ | Codex 侧接线 | 顶层 `openai_base_url`(**不新建 provider**) | 新建 `[model_providers]`(会话按 provider 分组被隐藏、remote-control 不可用、丢远程压缩) |
20
+ | 下游传输 | **WebSocket 第一传输**(完整实现 `responses_websockets` 协议)+ SSE 兜底 | 仅 SSE(codex 先试 ws → 405 → 每会话约 5 次重连告警后回退) |
21
+ | zstd 请求压缩(0.142.x 内置 provider 默认开) | 原生解压,无需改 codex 配置 | 需 `[features] enable_request_compression = false` |
22
+ | `GET /v1/models` 模型目录刷新 | `/v1/*` 透传 | 未代理(静默失败,靠本地缓存) |
23
+ | 续写方法 | commentary 法(`phase:"commentary"` 消息 + encrypted reasoning 重放) | commentary + tool_pair legacy + 跨轮 repair 等更多可配置项 |
24
+
25
+ ## 原理
26
+
27
+ 1. 上游每轮结束时读取 `usage.output_tokens_details.reasoning_tokens`,命中 `518n-2`(n∈[1,6],最多续写 3 轮)即判定思考被截断;
28
+ 2. 丢弃该轮的**暂定输出**(message / tool calls——它们基于被截断的思考),把该轮 reasoning items(含 `encrypted_content`)+ 一条 `Continue thinking...` 的 `phase:"commentary"` 助手消息追加进 input 重放,开下一轮;
29
+ 3. 思考流实时透传给 agent,只有干净收尾那一轮的最终输出被放行;terminal 事件重建为单响应口径的 usage(input 取第 1 轮防止「假爆上下文」,reasoning 求和),真实累计成本记在 `metadata.proxy_billed_usage`。
30
+
31
+ ## 安装
32
+
33
+ 要求:[uv](https://docs.astral.sh/uv/)(自带 Python 管理)、Codex CLI(ChatGPT OAuth 登录,0.142.x 实测)。
34
+
35
+ ```bash
36
+ uv tool install codex-516-guard # 从 PyPI 安装
37
+ # 或直接从源码仓库:
38
+ # uv tool install git+https://github.com/dzshzx/codex-516-guard
39
+ ```
40
+
41
+ uv 会建一个隔离环境并把可执行文件放进 uv 的 bin 目录(Unix/macOS 默认 `~/.local/bin`,
42
+ Windows 用 `where.exe codex-516-guard` 查实际路径;`uv tool update-shell` 可把该目录加进 PATH)。
43
+ 之后:
44
+
45
+ ```bash
46
+ codex-516-guard # 前台跑起(默认 127.0.0.1:8787)
47
+ codex-516-guard --port 8790 --log-level debug # 可选参数:--host/--port/--upstream/--log-level
48
+ ```
49
+
50
+ 升级 / 卸载:`uv tool upgrade codex-516-guard` / `uv tool uninstall codex-516-guard`。
51
+
52
+ Codex 侧接线——`~/.codex/config.toml` 顶层(必须在第一个 `[table]` 之前)加一行:
53
+
54
+ ```toml
55
+ openai_base_url = "http://127.0.0.1:8787/v1"
56
+ ```
57
+
58
+ 这是覆盖内置 openai provider base_url 的**官方 config key**
59
+ ([#16719](https://github.com/openai/codex/issues/16719);同名 `[model_providers.openai]`
60
+ 覆盖被维护者拒绝,`OPENAI_BASE_URL` 环境变量已移除)。provider id 保持 `openai`,
61
+ 因此会话历史分组、远程压缩、remote-control 均不受影响。
62
+
63
+ **关闭**:注释掉 `openai_base_url` 行 + 停掉代理进程。代理停止而 key 在位时,Codex 会因上游不可达报错。
64
+
65
+ ## 开机自启动
66
+
67
+ 代理是**用户会话内**被 Codex 调用的回环服务,所以三平台都选「随用户登录启动、跑在用户上下文」的方式
68
+ (而不是系统级服务——系统服务跑在无用户环境的 session 里,够不到用户 profile 下的 uv 可执行文件与代理设置)。
69
+ 先用 `which codex-516-guard`(Unix/macOS)或 `where.exe codex-516-guard`(Windows)拿到绝对路径备用。
70
+
71
+ ### Linux / WSL — systemd user unit
72
+
73
+ 见 `systemd/codex-516-guard.service.example`:
74
+
75
+ ```bash
76
+ cp systemd/codex-516-guard.service.example ~/.config/systemd/user/codex-516-guard.service
77
+ systemctl --user daemon-reload && systemctl --user enable --now codex-516-guard
78
+ ```
79
+
80
+ ### macOS — launchd LaunchAgent
81
+
82
+ macOS 用 launchd 管理后台任务。放在 `~/Library/LaunchAgents/` 的是 **LaunchAgent**,随**用户登录**启动、
83
+ 跑在用户 GUI session 里(对回环代理正确的选择);`/Library/LaunchDaemons/` 里的 LaunchDaemon 开机即起但无用户会话,本场景不适用。
84
+
85
+ 把可执行文件绝对路径填进下面的 plist,存为 `~/Library/LaunchAgents/com.dzshzx.codex-516-guard.plist`:
86
+
87
+ ```xml
88
+ <?xml version="1.0" encoding="UTF-8"?>
89
+ <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
90
+ <plist version="1.0">
91
+ <dict>
92
+ <key>Label</key> <string>com.dzshzx.codex-516-guard</string>
93
+ <key>ProgramArguments</key>
94
+ <array>
95
+ <string>/Users/YOU/.local/bin/codex-516-guard</string>
96
+ </array>
97
+ <key>RunAtLoad</key> <true/>
98
+ <key>KeepAlive</key> <true/>
99
+ <key>StandardOutPath</key> <string>/tmp/codex-516-guard.log</string>
100
+ <key>StandardErrorPath</key><string>/tmp/codex-516-guard.log</string>
101
+ </dict>
102
+ </plist>
103
+ ```
104
+
105
+ 注意:launchd 不读 shell 配置,`ProgramArguments` 必须是**绝对路径**;崩溃后 `KeepAlive` 会重启(10 秒节流)。
106
+ 加载 / 停用(现代 `launchctl`,`load`/`unload` 已是 legacy):
107
+
108
+ ```bash
109
+ launchctl bootstrap gui/$(id -u) ~/Library/LaunchAgents/com.dzshzx.codex-516-guard.plist
110
+ launchctl enable gui/$(id -u)/com.dzshzx.codex-516-guard
111
+ launchctl kickstart -k gui/$(id -u)/com.dzshzx.codex-516-guard # 立即(重)启动
112
+ launchctl bootout gui/$(id -u)/com.dzshzx.codex-516-guard # 卸载
113
+ ```
114
+
115
+ ### Windows — 用「登录时触发」的计划任务(不是系统服务)
116
+
117
+ 用户常问能否做成 Windows **service**:可以,但原生 service 要求程序实现 SCM 控制协议,
118
+ `sc.exe create` 直接指向普通控制台程序会在启动时被判定超时(错误 1053),必须套 WinSW / NSSM 之类 wrapper。
119
+ 即便套上,service 默认跑在 **session 0 + SYSTEM 账户**,没有你的用户环境、代理设置,也不便访问用户 profile 下 uv 装的可执行文件。
120
+
121
+ 因此本场景**推荐计划任务(onlogon)**:随你登录启动、带完整用户环境和 PATH,正好匹配「用户会话内被调用的回环代理」。
122
+ 用 `where.exe codex-516-guard` 拿到路径后(PowerShell):
123
+
124
+ ```powershell
125
+ $exe = (Get-Command codex-516-guard).Source
126
+ schtasks /create /tn "codex-516-guard" /tr "`"$exe`"" /sc onlogon /rl limited /f
127
+ schtasks /run /tn "codex-516-guard" # 立即启动一次
128
+ # 删除:schtasks /delete /tn "codex-516-guard" /f
129
+ ```
130
+
131
+ 如确实要开机即起(未登录也运行)的系统服务形态,用 [WinSW](https://github.com/winswhq/winsw)(MIT)
132
+ 把本 exe 包成服务;注意需让服务以你的用户账户运行,否则够不到用户 profile 下的安装。
133
+
134
+ ## 验证
135
+
136
+ ```bash
137
+ curl -sS http://127.0.0.1:8787/healthz # {"ok":true,...}
138
+ journalctl --user -u codex-516-guard -f | grep -E 'round|done' # Linux/WSL;mac 看 plist 日志文件
139
+ ```
140
+
141
+ 命中折叠时的日志(实测样例,连环双 516 被击破、答案正确):
142
+
143
+ ```
144
+ round 1: in=21550 out=664 reason=516 total=22214 | n=1 buffered=['function_call'] -> continue
145
+ round 2: in=22078 out=652 reason=516 total=22730 | n=1 buffered=['function_call'] -> continue
146
+ round 3: in=22606 out=566 reason=291 total=23172 | n=None buffered=[...] -> clean
147
+ done: 3 round(s) | ... | status=completed stop=natural
148
+ ```
149
+
150
+ ## 开发
151
+
152
+ ```bash
153
+ git clone https://github.com/dzshzx/codex-516-guard && cd codex-516-guard
154
+ uv sync
155
+ uv run python test_fold.py # 折叠状态机自测,应输出 ALL PASS
156
+ uv run codex-516-guard # 本地跑
157
+ ```
158
+
159
+ 发布走 PyPI Trusted Publishing(`.github/workflows/release.yml`,OIDC,无 token):推 `v*` tag 即自动构建上传。
160
+
161
+ ## 结构
162
+
163
+ - `guard/fold.py` — 指纹检测 + 折叠状态机(传输无关;`test_fold.py` 覆盖丢弃/放行、重编号、双口径 usage)
164
+ - `guard/server.py` — starlette 传输层:ws / SSE 下游、SSE 上游、zstd/gzip 请求解压、`/v1/*` 透传
165
+ - `guard/cli.py` — CLI 入口(`codex-516-guard`;仅监听回环;auth passthrough,不存储任何凭据)
166
+
167
+ ## 安全与免责
168
+
169
+ - 代理只做 auth **passthrough**:转发 Codex 发来的 Authorization 头,不读取、不落盘任何凭据。
170
+ - 仅监听回环地址;不要暴露到非回环接口。
171
+ - 非官方项目,依赖上游未公开的行为(截断指纹、ws 帧格式),OpenAI 侧变更可能使其失效;使用风险自负。
172
+ - 续写会产生额外的真实 token 消耗(见 `metadata.proxy_billed_usage`),guard 以 n 窗口 + 3 轮上限约束。
173
+
174
+ ## License
175
+
176
+ MIT(见 LICENSE;机制思路 credit neteroster/CodexCont)。
File without changes
@@ -0,0 +1,39 @@
1
+ """codex-516-guard CLI entry point (installed via [project.scripts])."""
2
+ from __future__ import annotations
3
+
4
+ import argparse
5
+ import logging
6
+ import os
7
+
8
+ import uvicorn
9
+
10
+
11
+ def main() -> None:
12
+ parser = argparse.ArgumentParser(
13
+ prog="codex-516-guard",
14
+ description=(
15
+ "Local Responses proxy for Codex CLI: detects the gpt-5.5 518n-2 "
16
+ "reasoning-truncation fingerprint, auto-continues thinking, and folds "
17
+ "all rounds into one response. Wire Codex to it with the top-level "
18
+ 'config key: openai_base_url = "http://127.0.0.1:8787/v1"'
19
+ ),
20
+ )
21
+ parser.add_argument("--host", default="127.0.0.1",
22
+ help="bind address (default: 127.0.0.1; keep it loopback)")
23
+ parser.add_argument("--port", type=int, default=8787, help="bind port (default: 8787)")
24
+ parser.add_argument("--upstream", default=None,
25
+ help="upstream base URL (default: https://chatgpt.com/backend-api/codex)")
26
+ parser.add_argument("--log-level", default="info",
27
+ choices=["critical", "error", "warning", "info", "debug"])
28
+ args = parser.parse_args()
29
+
30
+ if args.upstream:
31
+ os.environ["GUARD_UPSTREAM_BASE"] = args.upstream
32
+ logging.basicConfig(level=args.log_level.upper(),
33
+ format="%(levelname)s:%(name)s:%(message)s")
34
+ uvicorn.run("guard.server:app", host=args.host, port=args.port,
35
+ log_level=args.log_level)
36
+
37
+
38
+ if __name__ == "__main__":
39
+ main()