Skip to content

fix: count responses input[] developer/system in preflight overhead estimate (#829) - #832

Merged
ranxianglei merged 4 commits into
masterfrom
2026-09-16_829-responses-overhead-devsystem
Sep 18, 2026
Merged

ranxianglei merged 4 commits into
masterfrom
2026-09-16_829-responses-overhead-devsystem

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Fixes #829

Problem

estimateWireOverhead (in src/server/budget.ts) only read top-level instructions for the responses protocol. codex 0.147.0 sends its whole system prompt as role=developer items inside input[] (top-level instructions stays empty). The kernel hoists those out of the counted message view (projection.systemParts), and the rebuild strips instructions while re-injecting the system text back into input[] as a developer message — so the entire system prompt was invisible to the preflight gate.

Result: codex-shaped sessions were systematically under-counted by ~6–9k tokens (the whole system prompt). When reported usage lagged real payload growth (first-turn unknown baseline, client history jumps, providers not reporting usage), the gate kept taking the stale-baseline escape ("trigger fired on a stale baseline but the payload fits; forwarding as-is") and deferred compression until payloadEstimate itself climbed past the window → context-overflow risk.

Fix

Count the developer/system items inside input[] for the responses branch (mirrors the existing openai chat branch). Handles both content shapes: plain string and array of input_text/output_text parts.

Note: this also now counts the always-injected compress/absorb prompts (they ride the wire in the rebuilt developer item) — correct, since they consume context, but it raises the calibrated overhead for all responses sessions.

Behavior change

Real codex users will now trigger compression earlier (the gate sees the true payload size). This is intended — it removes the systematic under-count. Flagging per the #829 note so it's evaluated as a deliberate behavior change before release.

Tests

  • Added 3 regression tests in tests/wire-overhead.test.ts: dev/system counted, user/assistant not; array-of-parts content joined; top-level instructions + input[] dev combined.
  • Recalibrated 4 test harnesses whose windows were tuned against the old under-count (10k→15k): codex-compact-e2e, compaction-trigger-finality, host-usage-postfold, and the two fix: Codex preflight summaries fail with unsupported max_output_tokens #663 tests in preflight-max-output-tokens. compaction-trigger-finality also moves the reported usage 9000→13500 to preserve its 90% ratio.

Pre-flight

  • typecheck: clean
  • test: 1652 tests / 1650 pass / 0 fail / 2 skipped (e2e-gated)
  • build: OK (dist/index.js 2.89MB)

…stimate (#829)

estimateWireOverhead only read top-level instructions for the responses protocol. codex sends its whole system prompt as role=developer items inside input[] (top-level instructions empty), and the kernel hoists those out of the counted message view while the rebuild strips instructions — so the entire system prompt was invisible to the preflight gate, systematically under-counting codex-shaped sessions by ~6-9k tokens and deferring compression.

Count the developer/system items in input[] (mirrors the openai branch). Recalibrated 5 test harnesses whose 10k windows were tuned against the old under-count to 15k, added regression tests. Pre-flight: typecheck clean, 1652 tests / 1650 pass / 0 fail / 2 skipped (e2e-gated), build OK.
@github-actions

github-actions Bot commented Sep 16, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-09-16_829-responses-overhead-devsystem (56db520)

Option A — Install from npm PR tag (recommended)

npm install -g billion-context@pr-832

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

Option B — Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pr832.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 review 流程处理:拉取分支核对 diff、验证 #829 的根因描述与代码路径一致、跑 typecheck/test/build、确认 CI 状态。结论稍后回帖。

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 Review complete — the fix is correct, I found and fixed one missed test calibration on the branch, CI is now fully green. Details:

Bug verification (#829) — confirmed at code level against acp-kernel 0.0.74:

  • responsesToCore hoists role=system|developer message items out of the counted message view into systemParts, so estimateCoreMessages never sees them.
  • Rebuild strips instructions (src/loop/adapter-responses.ts:300) and re-injects the system text back into input[] as a developer item — so the raw dev/system items are exactly what rides the wire, and counting them matches wire reality, including the always-injected compress/absorb prompts (the raised calibrated overhead you noted is correct).
  • No double count: payloadEstimate = estimateCoreMessages(processed) + overheadEstimate + imageTokens (src/server.ts:2813-2818), and dev/system items are excluded from processed.

The fix targets the root cause (the overhead estimator), not the symptom (the stale-baseline escape). The flagged behavior change — codex-shaped sessions triggering compression earlier — is agreed as intended; that's the whole point of #829.

Local verification: typecheck clean; 1652 tests / 1650 pass / 0 fail / 2 skipped (e2e-gated); build OK. Matches your pre-flight claims.

CI finding → fixed directly on the branch: your first push failed the real codex vs deterministic fake upstream (zero tokens) job at warmup: preflight fail-fast 502: context ~18346 tokens exceeds the model window 12000. Same miscalibration class you already handled for the four unit harnesses: once the system prompt is counted, the real-codex warmup floor (~18.4k) sits above the e2e-fake overflow test's 12k window, and turn 1 has nothing compressible, so preflight fails fast. That suite only runs on CI pull_request, so it wasn't visible locally. Fix: tests/e2e/e2e-codex-fake.test.ts overflow window 12_000 → 24_000 (clears the measured floor; per-turn filler still crosses it within two load turns; the 60k control test is untouched). Verified locally with real codex-cli 0.147.0 — both tests pass, real compression events occur, sentinels survive — then pushed 1c02193. All 7 CI checks are now green.

Also checked tests/e2e/e2e-codex.test.ts (manual-dispatch real-upstream suite): its windows are 32k/128k, above the new floor — unaffected.

The diff is clean: every hunk is either the overhead fix or its calibration fallout (including my one-line window bump). Since this miscalibration is the exact class #829 predicted ("~13 test windows calibrated on the old under-count"), fixing it inside this PR rather than a follow-up issue keeps the calibration complete in one shot.

Merge is human-only — everything is ready: #832

修复了 e2e-fake 测试窗口漏校准的问题(12k→24k,新计数下真实 codex 首轮下限约 18.4k 导致 CI warmup fail-fast),已在 PR 分支直接补上并用真实 codex 本地验证、CI 全绿,本 PR 可以合并。

@ranxianglei

Copy link
Copy Markdown
Owner Author

继续

@ranxianglei

Copy link
Copy Markdown
Owner Author

冲突

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

冲突已解决,分支已更新到当前 master(a8676a5)之上。

The conflict — one hunk, tests/e2e/e2e-codex-fake.test.ts: both sides changed the same window value in the overflow test.

Resolution: kept 24k. The two calibrations measure different things — preflight fail-fast compares the local payloadEstimate (core messages + overhead estimate), not upstream-reported usage. Master's 10k sits below the turn-1 estimate floor even after #815 shrinks it, so it would reproduce exactly the same warmup 502 that killed 12k in CI. 24k clears the measured floor with margin (the hermetic cwd can only lower it further), while per-turn filler (~6k tokens) still crosses the window within a couple of load turns, so the bounded-payload assertion keeps working. The comment now records both calibrations so this doesn't get re-"fixed" back down without re-measuring.

Everything else auto-merged cleanly — notably tests/preflight-max-output-tokens.test.ts, where our startProxy(..., limit = 10_000) param (with the two #663 tests at 15k) coexists with master's 8192→32768 cap change without semantic overlap.

Verification on the merged tree:

  • npm run typecheck ✅
  • npm test ✅ — 1769 pass / 0 fail (2 skipped = env-gated real-upstream e2e)
  • npm run build ✅
  • The conflicted suite itself, run locally against real codex 0.147.0 + deterministic fake upstream (zero tokens): both tests green — overflow: compression really happens in codex; bulk folded, sentinels retained and the under-window control.

Head is now 8a046c4 (merge commit); branch is 3 commits ahead of master with no conflicts remaining — GitHub should flip the PR back to mergeable once it finishes its post-push recompute. Ready for you to merge whenever CI is green.


中文一句话:解决了与 master 的合并冲突(e2e-fake 溢出测试的窗口值两边都改了),保留本分支的 24k——因为 preflight 快速失败比较的是本地估算值而非上游报告的 token 数,master 按报告值校准的 10k 会在第一轮回暖时复现同样的 502;合并后 typecheck/全量测试/构建全绿,并用真实 codex 跑通了该 e2e 套件,可以合并。

@ranxianglei

Copy link
Copy Markdown
Owner Author

ci失败

…#832)

CI failed with 'got 1' summary: the 24k window was calibrated on a
leaky local env (TMPDIR inside the repo tree -> codex discovered repo
AGENTS.md from its cwd, inflating every payload). In a clean env the
turn-1 estimate is ~15.3k and the crossing only happened on the last
load turn, leaving a single fold.

- window 24k -> 18k: clears the overhead-only floor (~14k est) so no
  warmup fail-fast 502, while preflight engages from turn 2
- filler 300 -> 600 lines: decisive crossing by turn 2, repeated folds
  inside the five load turns, forwarded payload stays flat (~57-58k)
- skip guard: if .git/AGENTS.md sits above the hermetic cwd, skip the
  calibrated test with an actionable reason instead of mis-calibrating
@ranxianglei
ranxianglei merged commit e2a2d79 into master Sep 18, 2026
8 checks passed
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

CI fixed — all checks green on 56db520, including real codex vs deterministic fake upstream (zero tokens) (run 35325506491).

What failed: the overflow test asserted ≥2 summarization calls but CI saw exactly 1 (the fold only happened on the last load turn).

Root cause: my merge-conflict resolution kept a 24k window calibrated on my local sandbox — which turned out to be leaky: its TMPDIR sits inside the repo tree, so codex discovered the repo's AGENTS.md from its spawn cwd (#815 mechanism) and injected it into every payload, inflating the local floor to ~24k est. In a clean CI env the turn-1 estimate is only ~15.3k, so with an 18.4k target the crossing didn't happen until turn 5 → single fold. Master's 10k wasn't viable either: under the #829-corrected estimate (dev/system items now counted), turn-1 ≈15.3k > 10k → warmup fail-fast 502 (same class as the original 12k CI failure).

Fix (56db520, test-only):

  • window 24k → 18k — clears the overhead-only floor (~14k est) so no warmup 502, while preflight engages from turn 2
  • filler 300 → 600 lines (~5k tokens/turn) — decisive crossing by turn 2, so repeated folds fit within the five load turns and the forwarded payload stays flat
  • hermetic-precondition guard — if .git/AGENTS.md sits above the test's hermetic cwd, the calibrated test now skips with an actionable reason ("point TMPDIR outside any repository") instead of silently mis-calibrating. This guard is what would have caught the local-vs-CI discrepancy immediately.

Evidence:

  • Local hermetic run (TMPDIR outside any repo = simulates CI): both tests pass; oracle log shows 5 mains + 4 summary calls, forwarded mains flat at 57–58k chars (peak/min ≈ 1.01 ≤ 1.3), sentinels survive into the final payload
  • Local leaky run: overflow test skips with the clear reason above; control still runs green
  • Gates: typecheck ✅ · npm test 1771 pass / 0 fail ✅ · build ✅ · CI 8/8 green ✅

PR remains open for you to merge — #832


中文摘要:CI 失败根因是冲突解决时保留的 24k 窗口按"仓库文档泄漏进载荷"的本地环境校准,干净 CI 环境下首轮估算仅 ~15.3k、直到最后一轮才触发压缩(仅 1 次);已改为 18k 窗口 + 加倍填充使压缩从第 2 轮起反复发生,并新增"cwd 上方存在 .git/AGENTS.md 则跳过并提示"的守卫防止再次错校准,CI 现已全绿,可以合并。

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.

[preflight] responses 协议 overhead 估算漏计 input[] 内的 developer/system 消息:codex 请求被系统性低估整个 system prompt

1 participant