Skip to content

fix(#1800): fold compressible text when images alone exceed the window estimate - #1801

Merged
ranxianglei merged 1 commit into
masterfrom
2026-09-30_issue1800-image-arbitration-compress
Oct 1, 2026
Merged

ranxianglei merged 1 commit into
masterfrom
2026-09-30_issue1800-image-arbitration-compress

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

What & why

Fixes #1800 — on bytes-billing upstreams (unknown hosts), the conservative base64/4
image estimate can push the payload estimate over the context window while the TEXT
portion still fits. In that case the #496 arbitration branch returned prepared
unconditionally, skipping preflightCompress entirely and recording no compression
state. Net effect: the compressible text was never folded even though it was foldable,
so auto-compression stayed permanently disabled for image sessions — the reporter's
session logged preflightCompress running 0 times across the whole session while
every request was forwarded.

Root cause (layered)

  • Symptom: images billed by the bytes-mode estimate → over-window estimate → requests forwarded and compression never engages.
  • Mechanism: the #496 branch's early return conflated "nothing to fold" with "images dominate" — it exited before preflightCompress regardless of whether any compressible ranges existed.
  • Fix at the decision point (not a mask): the branch now short-circuits ONLY when there are genuinely no compressible ranges. Otherwise it runs preflightCompress to fold the text portion, then — if images alone still keep the residual over the window with no upstream overflow evidence — forwards the folded payload for the upstream to arbitrate billing. The image rides along byte-exact either way.

Behavior change (old → new)

  • Old: image-dominated, over-window-by-estimate payloads with foldable text → forwarded unfolded; compression permanently off for the session.
  • New: same inputs → the compressible text portion is folded first; if images alone still exceed the window it is forwarded (folded). Auto-compression is no longer permanently disabled.
  • This is the intended fix, disclosed per policy. No thresholds/defaults/env changed; no new config surface.

Evidence

  • typecheck clean on the rebased head (deps synced to acp-kernel 0.0.99).
  • New regression tests/issue1800-image-arbitration-compress.test.ts: fails without the fix (got 0 summarization calls) / passes with it.
  • Affected-path tests green: issue1800, issue496-forward-once-learn, issue767-pixel-billing, fix-488-multimodal = 19/19.
  • Full suite: 3450/3470 pass. The single failure (tests/proxy-persist.test.ts "EPERM detector") passes in isolation → a full-suite ordering flake in the persistence layer, untouched by this diff; not a regression from this change.
  • Diff confined to src/server.ts (the #496 arbitration branch) + the new test. Rebased onto current master.

Review notes

  • Hot-file territory (src/server.ts preflight) → human review, not auto-merge.
  • Both compression modes considered: the change is wire-agnostic (estimate/session-stats based), so plugin and proxy modes are treated consistently.
  • CI runs the full suite + e2e-codex (auto-triggered by the src/server.ts touch).

Closes #1800

…w estimate

On bytes-billing upstreams (unknown hosts) the conservative base64/4 image
estimate can push the payload estimate over the context window while the TEXT
portion still fits. The #496 arbitration branch returned `prepared`
unconditionally in that case, skipping preflightCompress entirely and
recording no compression state — so auto-compression stayed permanently
disabled for image sessions (issue #1800: preflightCompress ran 0 times across
the whole session while every request was forwarded).

The branch now short-circuits ONLY when there is genuinely nothing to fold
(compressibleRanges empty). Otherwise it remembers the arbitration, runs
preflightCompress to fold the compressible text portion, and — if images alone
still keep the residual estimate over the window with no upstream overflow
evidence — forwards the folded payload for the upstream to arbitrate billing.
The image rides along byte-exact either way. No config/env surface added.

Adds tests/issue1800-image-arbitration-compress.test.ts pinning the regression
(verified: fails without the fix with 0 summarization calls, passes with it).
@github-actions

github-actions Bot commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-09-30_issue1800-image-arbitration-compress (6ede410)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.1827-pr.1801.2bc0c1e

This exact version is immutable and reproducible. The rolling pr tag tracks the newest test build across all PRs.

Option B — Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pr1801.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 规范审查本 PR(对应原 issue #1800)。步骤:核对原 issue 方向 → 拉分支审代码 → 回归审查(#496/#767/#488 既有保证)→ 测试有效性 → diff 整洁度 + CI。结论会用结构化七问给出,稍等。

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 ## Review: fix(#1800) — fold compressible text when images alone exceed the window estimate

Reviewed against current master (branch is rebased, 0 commits behind at review time; single linear commit, diff confined to src/server.ts + new test — clean). Full control-flow read of preflightCompressIfNeeded, cross-checked against src/preflight.ts internals, then local verification on the exact pushed head.

Verification performed (not just reading the diff)

  • Negative control: ran the new regression test against unpatched master in a clean worktree → fails with got 0 summarization calls. The test genuinely reproduces 疑似#767后续:图片计价高估会使preflight自动压缩永久失效,同时请求仍被转发 #1800. ✅
  • Positive: typecheck clean; affected set (issue1800 / issue496-forward-once-learn / issue767-pixel-billing / fix-488-multimodal / issue857-image-baseline) 22/22; full suite locally: 3470 tests, 3451 pass, 0 fail (the proxy-persist EPERM flake did not reproduce — consistent with your ordering-flake assessment).
  • CI on first head 6ede4109: 13/13 green, including the src/server.ts-triggered e2e-codex run.

⚠️ One real defect found — in the PR's own regression test (fixed & pushed)

While strengthening the test I caught that the fixture's mock summary "PREFLIGHT SUMMARY: folded segment." is 35 chars < MIN_SUMMARY_CHARS = 50 (src/preflight.ts). Consequence: every summarization call was rejected "summary too short", compressedRanges stayed 0, and the fix's post-compress arbitration path forwarded the unfolded prepared payload. The test still passed (summaryCalls >= 1) while proving only that preflight RAN — not that it FOLDED, which is what the PR title claims. I proved this empirically (upstream mock saw 60/60 filler markers).

Pushed commit dc633e14 to the PR branch:

  • fixture now returns the repo's canonical ≥50-char summary so folds actually apply;
  • new assertion: strictly fewer filler markers reach the mock than were sent — pins that the forwarded body is the rebuilt/folded one (catches a future "folds but forwards stale prepared" regression).

With the fixed fixture the test passes on the patched tree and still fails without the fix. New-head CI is queued (push-triggered); please let it go green before merging.

Regression audit (existing behaviors touched, item by item)


结构化七问(中文结论)

  1. 是什么问题:bytes 计费上游上,图片的保守估算(base64/4)单独就把 payload 估算推过窗口时,PR #489 后续: 默认图片计费 b64/4 在像素 tile 计费上游(官方 Anthropic/OpenAI)会硬 502 误杀多模态会话 #496 仲裁分支无条件提前返回,preflightCompress 整段被跳过——图片会话的自动压缩被永久禁用(报告者全会话 0 次 preflight)。
  2. 什么原因导致的:该分支把「图片主导了估算」与「无事可做」混为一谈,early return 放在任何可压缩性判断之前,导致明明可折叠的文本也永远不被折叠。修在决策点(删除无条件返回),不是遮症状。
  3. 怎么修复的:分支现在只在确实没有可压缩 range 时立即转发;否则先跑 preflightCompress 折叠文本部分,压缩后重新应用同一仲裁逻辑——若图片单独仍使残差超窗且无上游溢出证据,则转发已折叠的 payload(rebuilt outbound)交给上游裁决计费,而非 fail-fast。
  4. 是否有回归问题:审过 PR #489 后续: 默认图片计费 b64/4 在像素 tile 计费上游(官方 Anthropic/OpenAI)会硬 502 误杀多模态会话 #496(空 range 路径与旧代码逐字节一致)、[Question] 非白名单上游(如 DeepSeek)图片按 b64/4 估算,实测高估约 100×;且该估算被写回 lastInputTokens 并自愈成 confirmed 窗口,使 #496 逃生门永久失效 #857(preflight 只写 estimate-source stats,无法翻转证据门)、fix: honor anthropic-beta context-1m to size the context window #301(非图片部分自撑满窗口仍 fail-fast)、preflight fail-fast × soft-protection livelock: over-window payload with nothing foldable outside the protected zone 502s forever #330(非图片路径不变;图片+空 range 保持旧行为=parity 而非回归)、[Bug] Codex 额度中断后恢复长会话,preflight SSE 摘要为空并持续 502 #726(新转发路径在死端标记之前返回,不引入冷却陷阱)、双压缩模式(wire-agnostic,一致)。未发现回归。
  5. 是否破坏缓存:不涉及。折叠轮次的 prefix-cache miss 是任何压缩的固有代价;去重键、死端键、持久化格式均未动。
  6. 是否改了设计方向:不涉及。仍在 preflight 决策流内部,wire-agnostic,无新模块/边界。
  7. 是否引入了新的配置:无。无 env/config/默认值变更(PR 声明属实,diff 核实)。

判定:可合并——代码改动本身正确、最小、理由充分;我推送到 PR 分支的 dc633e14(测试夹具修复+断言补牙)让回归测试真正钉住「折叠」这一核心行为。等 dc633e14 的 CI 全绿后即可由人工合并。合并动作按规范留给人工。

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 CI 状态更新(新 head dc633e14)

我推送到 PR 分支的 commit 没有自动触发 CI——本环境的 push 凭据属于 app token,GitHub 对其 push 事件不投递(与 release-manual 流程文档记录的 GITHUB_TOKEN 行为一致),所以 push 后 check-runs 一直为空。已按同样的回退模式手动 dispatch 了全部可触发的 workflow(ref=pr1801),结果:

Workflow 结果
CI(ubuntu 22/24 + windows 22/24 全矩阵 + version-guard skipped) ✅ success
E2E (real codex) ✅ success
e2e-fake-codex ✅ success
e2e-local-registry(verdaccio hermetic) ✅ success
e2e-native-pi / e2e-native-opencode / e2e-native-dsh ✅ success ×3

未在新 head 上跑的仅剩 PR Build Artifact 和 CI Windows codex overlay 两个——它们是纯 pull_request 触发,等人工下一次 push(或 GitHub 补投 synchronize 事件)即会跑;两者均为辅助项(artifact 上传 / codex overlay 注册),不影响合并判定。

结论不变:可合并。 代码改动经回归审计无回归;我补的测试 commit(dc633e14)让回归测试真正钉住「折叠」行为,且全量 CI(含真实 codex e2e)已在全新 head 上全绿。合并动作留给人工。

@ranxianglei

Copy link
Copy Markdown
Owner Author

Superseded by #1818 — folded in (outbound-forward kept alongside the #1820 meter anchor). Tracking issue #1800 merged back into #1812.

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.

疑似#767后续:图片计价高估会使preflight自动压缩永久失效,同时请求仍被转发

1 participant