Skip to content

fix(#1169): match codex UA case-insensitively so Codex Desktop is detected - #1170

Merged
ranxianglei merged 1 commit into
masterfrom
2026-09-23_codex-desktop-ua-case
Sep 23, 2026
Merged

ranxianglei merged 1 commit into
masterfrom
2026-09-23_codex-desktop-ua-case

Conversation

@ranxianglei

@ranxianglei ranxianglei commented Sep 23, 2026 •

Copy link
Copy Markdown
Owner

What changed

isCodexClient (src/codex-compact.ts) now matches CODEX_UA_PREFIXES case-insensitively (UA lowercased once before the prefix check), and "codex desktop/" is registered as a known prefix. The lenient codex substring fallback stays case-sensitive. Tests: two assertions that had pinned the old behavior are updated, plus a regression test for the exact Codex Desktop/0.155.0-alpha.9 UA from the issue and a boundary pin (CodeXchange/1.0 → not codex, #1106).

Why

Codex Desktop sends User-Agent: Codex Desktop/x.y.z… with an initial capital. The old code missed it entirely, so none of the codex paths engaged: BILI_CODEX_COMPACT=intercept fell through to native compact, no host-usage exemption, no codex window clamp. This contradicts #645's own goal — "a new client variant must not silently fall out of detection".

Review revision (rebased onto current master): the original version of this PR made the entire match case-insensitive, including the lenient substring fallback. Re-running pre-flight on current master surfaced a semantic conflict: commit a5d9173 (merged after this PR's base) pins in tests/responses-instructions-drift.test.ts that mixed-case "Codex"-shaped UAs (CodeXchange/1.0) must NOT be treated as codex — a case-insensitive fallback would pull non-codex relays with "Codex"-shaped UAs back into the instructions fingerprint and re-fork them mid-conversation, exactly what #1106 removes. The fix is therefore narrowed to its root cause: register the Codex Desktop variant in the known-prefix table (the designed extension point) and make prefix matching casing-tolerant; the catch-all stays strict.

Behavior change (disclosed)

  • Old: only UAs containing lowercase codex anywhere, or starting with codex_cli_rs/ / codex_exec/, matched.
  • New: known prefixes match case-insensitively — Codex Desktop/…, Codex_CLI_RS/…, CODEX_EXEC/…, etc. are now recognized; the lenient lowercase-substring fallback is unchanged.
  • Why: the real-world Codex Desktop variant uses an initial capital and must be detected everywhere. Non-codex clients whose UA merely contains "codex" in mixed case (e.g. CodeXchange) are still NOT treated as codex — preserving the Responses instructions fingerprint should be an allowlist (codex/claude only), not a default — generic relay clients fork on every system-prompt change #1106 boundary pinned in tests/responses-instructions-drift.test.ts.
  • Call sites affected consistently (all treat such clients as codex, which is the desired outcome): src/server.ts compaction_trigger dispatch, forge gate, compact pre-gate, compression target; src/codex-models.ts:84 window clamp; src/session-id.ts:117 instructions-fingerprint allowlist (Codex Desktop traffic now gets the persona-splitting fingerprint, consistent with other codex clients).

Pre-flight (on rebased head, current master)

Fixes #1169

@github-actions

github-actions Bot commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-09-23_codex-desktop-ua-case (40f02fd)

Option A — Install from npm PR tag (recommended)

npm install -g billion-context@pr-1170

Each push to this PR publishes a new version under the pr-1170 npm tag.

Option B — Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pr1170.tgz
npm install -g package

Install instructions are refreshed on each push.

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 收到,开始对 PR #1170 做正式 review(分支 2026-09-23_codex-desktop-ua-case):拉取分支核对 diff、对照当前 master、重跑 typecheck/test/build、查 CI 状态。结论稍后回复。

…esktop)

isCodexClient now lowercases the UA before the known-prefix check and registers "codex desktop/" in CODEX_UA_PREFIXES, so Codex Desktop ("Codex Desktop/x.y.z", initial capital) is detected on every codex path. The lenient lowercase-substring fallback stays CASE-SENSITIVE: a case-insensitive fallback would pull non-codex relays with "Codex"-shaped UAs back into codex treatment and re-fork them mid-conversation (#1106).

Fixes #1169
@ranxianglei
ranxianglei force-pushed the 2026-09-23_codex-desktop-ua-case branch from 4494e0c to 40f02fd Compare September 23, 2026 04:18
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 Review 结论:原方案不可合并 → 已在 PR 分支直接修正,现在可以合并。

审查过程

发现的问题(拦截原因)
Rebase 后测试出现 1 个失败:not ok 1856 - instructionsFingerprintApplies: everyone else keys verbatim (#1106)(tests/responses-instructions-drift.test.ts:44:CodeXchange/1.0 不得判为 codex,原方案下被判为 codex)。
根因:在本 PR base 之后合入的 commit a5d9173 明确钉死了这条边界——isCodexClient 的宽松子串回退保持大小写敏感,是因为整体大小写不敏感会把 UA 形如 "Codex" 的非 codex relay 重新拉进 instructions fingerprint、导致会话中途 re-fork(正是 #1106 修掉的问题)。原"整体大小写不敏感"方案违反了这条 incident-pinned 不变量;且 PR 描述列了 5 个调用点,漏了第 6 个(src/session-id.ts:117)。

修正(小问题直接在 PR 分支修掉)
按根因收敛为两层方案:

效果:Codex Desktop/0.155.0-alpha.9 被所有 codex 路径识别(#1169 修复),CodeXchange/1.0 等仍非 codex(#1106 保留)。新 head 40f02fd,单一线性 commit。

行为变更披露(旧 → 新)

  • 旧:UA 含小写 codex(任意位置)或以 codex_cli_rs/ / codex_exec/ 开头才识别;
  • 新:已知前缀大小写不敏感识别(Codex Desktop/…、Codex_CLI_RS/…、CODEX_EXEC/… 等新增识别);宽松回退不变;混合大小写 "Codex" 形状的非 codex 客户端仍不当作 codex。
  • 全部 6 个调用点一致地将其视为 codex,均为期望结果:server.ts compaction_trigger dispatch / forge gate / compact pre-gate / compression target、codex-models.ts:84 window clamp、session-id.ts:117 fingerprint allowlist(Codex Desktop 流量从此获得与其他 codex 客户端一致的 persona-splitting fingerprint)。

Pre-flight(rebase 后新 head 重跑)

合并是人工操作:#1170

一句话:审查发现原方案(整体大小写不敏感)与 master 上 #1106 钉死的边界冲突(CodeXchange 回归测试失败),已改为「已知前缀大小写不敏感 + 注册 codex desktop/ 前缀,宽松回退保持大小写敏感」,重跑全绿,可以合并。

@ranxianglei

Copy link
Copy Markdown
Owner Author

Independent review (second pass, on top of the ework bot's):

Design — approve, and it is strictly better than the issue's suggested one-liner. Registering codex desktop/ as a known prefix with case-insensitive prefix matching, while keeping the generic s.includes("codex") fallback case-sensitive, threads the needle between #1169 (Desktop must be detected) and #1106 (mixed-case "Codex"-shaped relay UAs must NOT be pulled into codex fingerprint handling). A blanket toLowerCase().includes() would have reopened #1106.

Completeness — verified. src/codex-compact.ts:26 is the only UA consumer in src/ (grep -rn '"user-agent"' src/), so no other parser can mishandle the Desktop UA. All five isCodexClient call sites (intercept dispatch, forge gate, compact pre-gate, compression target ratio, window clamp) plus instructionsFingerprintApplies pick up Desktop uniformly — intended.

Evidence — re-verified locally. Rebased onto current master 41c7f6a (clean, no conflicts — the behind state is trivial), targeted suites 31/31 pass (codex-compact + codex-models + responses-instructions-drift), tsc --noEmit clean. CI on the PR head: 7/7 success.

Note for the record: the flipped assertions (Codex_CLI_RS/… now matches) are correct — that test pin came from #645's implementation description, not a deliberate exclusion decision, and it directly contradicted #645's own goal ("new client variants must not silently fall out of detection").

Ready to merge (human).

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 收到第二轮独立审查,结论与我一致。补充一个交叉验证:我 rebase 的基线(53c584e)之后,master 新增的提交(release v0.1.139、#1163/#1115、#1172、#1149)与本 PR 的 5 个文件(src/codex-compact.ts、src/session-id.ts 及 3 个测试文件)零重叠,所以你本地在 41c7f6a 上验证的干净 rebase 成立,我也无需再推任何东西。PR head 40f02fd 上 CI 7/7 全绿。

关于 flipped assertions 的备注也同意:Codex_CLI_RS/… 的大小写 pin 来自 #645 的实现描述而非刻意排除决策,翻转后与 #645 的初衷("new client variant must not silently fall out of detection")一致。

合并由人工执行(human-only):#1170

一句话:第二轮独立审查通过,新增 master 提交与本 PR 文件零重叠、无需改动,等待人工合并。

@ranxianglei
ranxianglei merged commit b2f28c6 into master Sep 23, 2026
8 checks passed
@ranxianglei
ranxianglei deleted the 2026-09-23_codex-desktop-ua-case branch October 15, 2026 05:32
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.

Codex Desktop 无法触发 BILI_CODEX_COMPACT=intercept

1 participant