Skip to content

fix: dispatch preflight folds by payoff, keep the recent zone last-resort (#2682) - #2721

Merged
ranxianglei merged 2 commits into
masterfrom
2026-10-11_preflight-payoff-order
Oct 15, 2026
Merged

ranxianglei merged 2 commits into
masterfrom
2026-10-11_preflight-payoff-order

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Closes #2682

What

Preflight walked compressible ranges oldest-first and folded one range per round. A summarization call costs the same wall clock whether it folds 6K or 64K tokens (measured 25-44s/call), and the head range re-grows after each fold (its block + newer messages form one bigger range again) — so a pass re-folded an almost-exhausted head every round while the largest ranges never got dispatched within the budget. Exactly what was measured on v0.1.191: five head re-folds over a 180s pass; two ranges worth 108K tokens untouched.

Fix (src/preflight.ts only)

Two-tier dispatch order:

  1. Tier 1 — ranges entirely outside the soft-protected recent zone: largest span first; age order breaks ties of equal mass (legacy behavior for equal-mass pools).
  2. Tier 2 — ranges entering the zone (present only after a preflight fail-fast × soft-protection livelock: over-window payload with nothing foldable outside the protected zone 502s forever #330/模型压缩输出是输入的数倍 #1841 relax): oldest-first within it, and never outranking old content — preserving preflight fail-fast × soft-protection livelock: over-window payload with nothing foldable outside the protected zone 502s forever #330's stated policy ("relax the soft zone (oldest-first within it)"). A naive pure-mass sort violated this: it dispatched a giant relaxed tail range before moderate old content and broke tests/preflight-compress-responses.test.ts (Codex 长会话在 macOS 休眠唤醒后 Bili 未触发压缩,导致上下文窗口超限 #280) and tests/preflight-hold.test.ts (Preflight compression holds the request with zero response bytes; client undici headersTimeout (default 300s) kills it -> repeated [preflight] summarization aborted: client disconnected 5-minute death loop, payload never forwarded #568).

The tier boundary comes from a CPU-only processTurn probe under the original protection config (the kernel keeps computeProtectedRefs internal to acp-kernel, so no kernel change is needed); it is re-derived each round because a zone fold slides the last-N window forward.

Also logs the chosen fold order once per pass ([preflight] fold order (largest first outside the recent zone, #2682): ..., info level, capped at 8 entries + overflow count) so ordering is diagnosable from logs without reading source (issue proposal 4).

Behavior change disclosure (old -> new)

  • Preflight fold dispatch order: ref-number ascending (oldest-first) -> token-mass descending OUTSIDE the protected zone, ties broken by ref-number ascending. Inside a relaxed zone the effective order is unchanged (ref-number ascending).
  • New info log line once per pass; no existing log format changed.
  • No wire shape, config surface, persistence, budget, futility-gate, or kernel changes. The 模型压缩输出是输入的数倍 #1841 futility gates are order-independent set sums (verified against their code paths).

Tests

New tests/issue2682-preflight-payoff-order.test.ts (hermetic: real kernel core + mock upstream):

  1. largest-range dispatch — fails on pre-fix code (verified by stashing the fix: 1 fail / 2 pass);
  2. equal-mass tie keeps the legacy oldest-first tie-break;
  3. after a 模型压缩输出是输入的数倍 #1841 up-front relax, zone content stays last-resort (guards against a future regression to pure-mass ordering).

Targeted family run locally: tests/preflight-*.test.ts + tests/issue1372-*.test.ts + tests/issue2682-*.test.ts -> 145/145 pass (four of these — #1372 skip diagnostics, #280 responses e2e, covered-ranges, #568 hold — failed under the intermediate pure-mass iteration and are green under the two-tier order). Typecheck clean, build clean. Full suite + e2e lanes delegated to CI.

Notes

…sort (#2682)

Preflight walked compressible ranges oldest-first and folded one range per round. A summarization call costs the same wall clock whether it folds 6K or 64K tokens (measured 25-44s/call), and the head range re-grows after each fold (block + newer messages form one bigger range again), so a pass re-folded an almost-exhausted head every round while the largest ranges never got dispatched within the budget (#2682: five head re-folds over a 180s pass; two ranges worth 108K tokens untouched).

Two-tier dispatch order: tier 1 (ranges outside the soft-protected recent zone) goes largest-span first with age order breaking ties of equal mass; tier 2 (ranges entering the zone, present only after a #330/#1841 relax) stays oldest-first within it and never outranks old content, preserving #330's stated policy. The boundary comes from a CPU-only processTurn probe under the ORIGINAL protection config (the kernel keeps computeProtectedRefs internal), re-derived each round because a zone fold slides the last-N window forward.

Behavior change: preflight fold dispatch changes from oldest-first to payoff-first OUTSIDE the protected zone (old value: ref-number ascending; new value: token mass descending, ties age-ascending); inside a relaxed zone the effective order is unchanged (ref-number ascending). No wire shape, config surface, persistence, or kernel change. New info log line reports the chosen fold order once per pass.
@github-actions

github-actions Bot commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-10-11_preflight-payoff-order (f503e86)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.3817-pr.2721.7daa4a7

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-pr2721.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(src/preflight.ts +60/-4、新测试 +188,单 commit),本地跑了定向验证,核对了 CI。这是对本 PR 的独立 review(#2682 楼 3 是实现报告)。

审原 issue(方向关):#2682 成立——测量证据完整(180s pass 内 5 次 head 重折、savings 递减、108K 大 range 从未派发;summary 调用 25-44s 与大小无关),根因定位准确(ref 号升序派发 + 调用成本与大小无关 + head 折后再生长 = 大 range 预算饥饿)。修法对齐 issue 提案 1(payoff 序)+ 提案 4(日志);提案 2/3 有意不实现的理由成立(既有出口已给 pass 上界,阈值启发式只添调参常数)。

回归审查(逐项既有行为清点):

  1. fold 派发顺序——有意变更,已披露(zone 外 oldest-first → largest-first,tie 按 ref 升序;zone 内不变)。✓
  2. preflight fail-fast × soft-protection livelock: over-window payload with nothing foldable outside the protected zone 502s forever #330 zone 政策("relax the soft zone (oldest-first within it)")——保持。tier 2 保持 oldest-first 且永不压过 tier 1;空间上也必然如此(kernel range 互不相交、zone 是 ref 后缀,任何进入 zone 的 range 都位于所有非 zone range 之后),且危险方向的误分类不可能:受保护 ref 全部 > 边界 B,而 tier 1 的 endRef ≤ B。✓
  3. 模型压缩输出是输入的数倍 #1841 两个 futility 门(pre-walk resolvableMass(ranges)、mid-walk remaining 求和)——均为顺序无关的集合和,已对代码路径核实;payoff 序下 mid-walk bail 会在大 mass 消费后更早触发,但判定基于真实剩余质量,语义无损(反而更省调用)。✓
  4. preflight fail-fast × soft-protection livelock: over-window payload with nothing foldable outside the protected zone 502s forever #330/模型压缩输出是输入的数倍 #1841 relax 触发路径(空列表 relax、round-end relax、up-front relax 探测)——未触碰。✓
  5. 预算机制(summaryCalls/summaryBudget/transientRetryBudget、per-regime reset)——未触碰。✓
  6. DSH 分叉会话后触发大量 preflight overflow compress #2383 emergency-fold 体制——同一循环,deterministic digest 无 LLM 成本;largest-first 在该体制下同样严格更优(round cap 内更多收缩量),无回归。✓
  7. wire 形状 / 配置面 / 持久化 / kernel——均未触碰(CI 的 config-surface-guard / kernel-guard / lock-sync / version-guard 全绿佐证)。✓
  8. 日志——新增一条 info 级每 pass 一行(≤8 条 + overflow 计数),经 deps.log(即 loggerLog 通道),既有格式未改。✓

代码审查要点:sort comparator 三种 tier 组合逐一验证正确(tie-break 显式,不依赖 sort 稳定性);边界探测与 #1841 既有探测惯例完全同构(原始保护配置、CPU-only、state 丢弃不采纳)、每轮重推以跟随 last-N 窗口前移;探测返回空时 boundary=-1 → 全量落入 tier 2 → 纯 oldest-first = legacy 行为,兜底方向安全;每个 relaxed round 多一次 processTurn,CPU 成本与既有探测同量级,可接受。无 as any/console.error,注释密度符合本仓风格。

测试有效性:3 个测试均为真 kernel core + mock upstream 的行为测试(listen(0) 取端口,确定性达标)。我在本地独立复验了咬合:把 master 版 src/preflight.ts 换回后跑新测试文件,恰好 1 fail(largest-range 用例)/ 2 pass,与 PR 声明一致;恢复后全绿。test 2 钉住等质量 tie-break(防静默翻成 newest-first),test 3 钉住 zone last-resort(防未来退回纯 mass 序)。本地定向家族(preflight-* + issue1372 + issue2682 + issue1841)= 148/148 绿,typecheck 干净。CI 15/15 全绿(linux/windows × node22/24 全矩阵 + 全部 guard + hermetic billing e2e)。

八问结论:

  1. 是什么问题:preflight 按最旧优先、每轮折一个派发 fold,head range 折后再生长,导致整个 pass 反复整价重折几乎耗尽的 head,最大的可折 range 在预算内从未被派发。
  2. 什么原因导致的:派发键是 ref 号升序而非收益;叠加 summary 调用墙钟成本与范围大小无关(实测 25-44s/call)和 head 再生长机制,oldest-first 退化为对同一块地的重复付费。
  3. 影响范围与严重程度:所有触发 preflight 且存在多个候选 range 的溢出会话(proxy 模式为主)。性能退化级:分钟级墙钟阻塞 + 预算燃尽仍不收敛;无数据丢失、无 wire 损坏。
  4. 怎么修复的:两级派发——zone 外最大 span 优先(等质量按 age),zone 内保持 oldest-first 且永不压过旧内容;zone 边界用原始保护配置下的 CPU-only processTurn 探测获得,每轮重推。
  5. 是否有回归问题:无。上述 8 项既有行为逐项过,唯一变更即已披露的派发顺序;模型压缩输出是输入的数倍 #1841 futility 门为顺序无关集合和,relax/budget/emergency/wire/kernel 均未动。
  6. 是否破坏缓存:不影响。fold 本身从折叠点起失效前缀缓存,本改动只决定「先折哪个」,不引入新的改写/去重/游标变化;message ref/id 不可复用契约未触碰。
  7. 是否改了设计方向:无架构转向,preflightCompress 内部派发策略调整;issue 提案 2/3 有意不采纳(理由见上)。
  8. 是否引入新配置:无。无新 env/config/CLI;边界探测与两级序均为内部实现。

diff 整洁度:单 commit、2 文件、每一行都与本 PR 目的相关,无空白/锁文件噪音。未发现需要顺手修的小问题。

判定:可合并,请 owner 合并。

…t.ts)

Only conflict: the #2682 dispatch rework and #2952 tier-distillation
recovery both touched the same declaration spot. Kept both sides
(union): the fold-order log flag plus the whole #2952 recovery block —
they are independent (dispatch ordering vs exhaustion-path recovery).

Verified on the merged tree: tsc --noEmit clean after kernel rebuild;
preflight family 189/189 green including issue2682 payoff-order (3),
issue2952 tier-recovery (5).
@ranxianglei
ranxianglei merged commit 44eea7f into master Oct 15, 2026
36 of 40 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

📦 Merged Build Published

This merge to master was published as 0.0.459-master.44eea7f (rolling master tag).

npm install -g billion-context@0.0.459-master.44eea7f  # this exact build
npm install -g billion-context@master  # always the newest merged build

npm view billion-context@master prints the current version at any time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant