fix #645: suppress #408 host-usage backfill for codex (report folded usage) - #647
fix #645: suppress #408 host-usage backfill for codex (report folded usage)#647ranxianglei wants to merge 3 commits into
Conversation
📦 Built Package ArtifactBranch: Option A — Install from npm PR tag (recommended)npm install -g billion-context@pr-647Each push to this PR publishes a new version under the Option B — Download artifact
tar xzf billion-context-pr647.tgz
npm install -g packageThis comment is automatically updated on each push. |
[bot] 🏷 Review: APPROVE ✅ Checked out the actual PR commit ( Pre-flight (verified on
Design — sound, single-point source fix:
One minor, non-blocking nit (PR description, not code): No code changes needed. Good fix. |
Logs the request User-Agent + isCodexClient result + pluginAgent per request so we can see exactly what UA the field codex sends and why the #645 exemption is not firing. Temporary; will be removed (with any matcher adjustment) in a follow-up commit before merge.
The field codex (issue #645) is the TypeScript SDK client, whose UA is 'codex_sdk_ts/<ver> ... (codex_exec; <ver>)' — not covered by the codex_cli_rs/ / codex_exec/ prefixes, so the #645 backfill exemption never fired and the >100% context display persisted. isCodexClient now also matches 'codex' anywhere in the UA (case-sensitive), so current and future codex client variants are all detected. Removes the temporary [codex-ua-debug] log block added to confirm the field UA. Adds regression tests for the SDK UA and the lenient fallback.
Problem (#645)
After v0.1.88 (which shipped the #408 host-usage backfill), Codex displays a
broken context metric:
1315/950k(138%) for a conversation whose realpost-fold usage is ~75k. The user's own proxy log shows the mechanism:
74,941 + 1,234,768 ≈ 1,311,663 ≈ the displayed 1315k.
Why the backfill is wrong for codex
request really is ~75k.
estimate of the folded-out tokens, so the metric can decrement without any
compress firing (reported in [Question] Previously, Codex displayed the context window size after compression, but it now appears to show an unexpected/incorrect value. #645).
total_tokens(codex-rs
session/context_window.rs→get_total_token_usage()), and thebackfill only touches
input_tokenson the responses wire — so the inflatednumber currently produces only the broken display, no compaction benefit.
Fix
Extend the #590 (pi) / #623 (omp) exemption to codex in
armHostUsageCredit: codex now reports the folded request's own usage.Detected by User-Agent because bili-launched codex sessions carry
pluginAgent "mcp"(shared with claude) and can't be gated on the header.Field-verified UA matcher (#645): the reporter's codex is the TypeScript
SDK client, whose UA is
codex_sdk_ts/<ver> … (codex_exec; <ver>)— notcovered by the original
codex_cli_rs//codex_exec/prefixes (thecodex_exectext is only inside a parenthetical). Codex ships multiple clientswith different UA prefixes, so
isCodexClientnow also matchescodexanywhere in the UA (case-sensitive) in addition to the known prefixes — current
and future codex client variants are all detected. (Confirmed via a temporary
[codex-ua-debug]log line in a diagnostic prerelease, since removed.)Safety nets are unchanged: ACP compression (nudge → preflight) runs on the
real post-fold number, and codex's native auto-compact remains the backstop at
90% of its window (#321 design). The uncompressed baseline is still visible in
the
/acppanel (hostContextTokens) and the proxy log.Tests
tests/host-usage-backfill.test.ts(responses wire, realkernel fold — sentinel in the folded assistant message, mirroring the pi plugin 模式宿主 usage 回填未压缩基线:footer 显示 302.7%/1M 与实际请求(折叠后 ~57K)不符 #590 pi
harness): codex UA → post-fold
input_tokens === 1000(no backfill);identical non-codex client →
input_tokens > 1000(代理压缩不上报改写量,宿主上下文会计塌缩至 ~0(omps recordAnchoredHistoryRewrite 零调用);且持久化 lastInputTokens 恢复无钳制,旧版负值升级后复活 #408 backfill intact).isCodexClientregression tests intests/codex-compact.test.ts+tests/codex-models.test.ts: the real SDK UA (codex_sdk_ts/0.153.4 …) →true, the lenient
codexsubstring fallback, and case-sensitivity(
Codex_CLI_RS/…→ false) all pinned.Pre-flight
npm run typecheck— cleannpm test— 1237/1238 pass; the 1 failure (launcher.test.ts: resolveClientCommand: codex/claude resolve to themselves) is pre-existing onclean master (this environment resolves
codexto/usr/bin/codex; the testexpects the bare name) and unrelated to this change
npm run build— success; published prerelease0.1.96-pr.647.472verifiedto contain the matcher and no diagnostic