Skip to content

fix: preflight relaxes the soft-protected recent zone under overflow (#330) - #337

Merged
ranxianglei merged 1 commit into
masterfrom
2026-08-29_preflight-soft-zone-relax
Sep 6, 2026
Merged

ranxianglei merged 1 commit into
masterfrom
2026-08-29_preflight-soft-zone-relax

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

What

Fixes the #330 livelock: an over-window payload whose only foldable content sits inside the soft-protected recent zone (preserveRecentMessages / preserveRecentTokens / most-recent user message) used to 502 forever with no recovery path.

Two things bricked it:

  1. The server pre-check failed fast on the normal-config compressibleRanges (which exclude the soft zone) before preflightCompress ever ran.
  2. Even when preflight ran, it had no recovery once nothing was foldable outside the zone — it just set an exhausted failure.

Changes

  • src/preflight.ts
    • When no range is foldable outside the soft zone and the payload's own estimate still overflows, relax the soft zone to zero (oldest-first within it) and retry. The hard protectedTools exclusion is computed independently and still applies. Only when truly nothing is foldable does it fail fast — now with an actionable error (raise the model context window / restart the session).
    • The relaxation is gated on the payload's own estimate (result.payloadEstimate), not currentTokens — the latter is floored by a possibly-stale lastInputTokens from a prior model, which would fold protected content even when the real payload already fits. (This regression was caught by the existing bili 模式切换模型会报错上下文满 #247 model-switch test.)
    • The per-round pipeline now runs with the kernel's emergency-truncate node suppressed (window inflated so usage < truncate.threshold; compressible ranges derive from the protected zone, not usage). Without this, the node trims the very tool output overflowing the window, making the fit check undershoot and break before the soft zone is relaxed.
  • src/server.ts
    • Decide forward/fail on the payload actually forwarded (re-measured after the rebuild), not on result.payloadEstimate — the relaxed-zone processTurn trims that estimate more than the normal-config prepare does, which could turn a guaranteed-400 forward into a false "fits".

Tests

  • New e2e #330: over-window payload whose only foldable content is in the protected recent zone → soft zone relaxed, folded, forwarded (200).
  • The "truly incompressible" e2e is now a hard-protected tool result (protectedTools: ["bash"]): 502 + actionable remedy, no summarization call, no forward.
  • Full suite 746/746, npm run typecheck clean, npm run build clean.

Follow-up (not in this PR)

The issue also suggested the kernel's truncateLargeToolOutputs as a second lever. It's stateless/non-persistent and only touches tool-results (the repro is a large user message), so it's largely redundant with the soft-zone relaxation here — left as a possible follow-up.

@github-actions

github-actions Bot commented Aug 29, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-08-29_preflight-soft-zone-relax (bfe9e4c)

Option A — Install from npm PR tag (recommended)

npm install -g billion-context@pr-337

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

Option B — Download artifact

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

This comment is automatically updated on each push.

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 ework agent · vllm-qwen/qwen3.8-27b

[bot] Reviewed 2026-08-29_preflight-soft-zone-relax (74fe46d) — checked out the branch and verified every claim locally. Verdict: approve. The fix is correct, well-reasoned, and genuinely regression-tested.

Verified clean:

  • npm run typecheck — no errors
  • npm run build — success
  • npm test — 746/746 pass

The tests actually catch the bug (not vacuous): I ran the PR's test file against master's src/preflight.ts + src/server.ts (keeping the PR's tests). Both new/modified e2e cases fail on master and pass on the branch:

  • #330 → 502 on master (old server pre-check bails on normal-config compressibleRanges before preflightCompress runs), 200 on the branch.
  • Modified #301 (hard-protected bash tool result) → also fails on master.

Design claims validated against acp-kernel 0.0.46 source:

  • noEmergencyTruncate (inflate modelContextLimit ×100) is safe: buildCompressibleRanges derives ranges structurally (excludes protectedZoneRefs + protectedTools), not from usage — so inflating the window suppresses only the usage-gated emergency-truncate node, never the range set. Exactly as the comment claims.
  • relaxedConfig is complete, no gap: in computeProtectedRefs, the most-recent-user-message protection is gated on preserveN > 0, so zeroing preserveRecentMessages/preserveRecentTokens removes the entire soft zone (including the last user message).
  • The noEmergencyTruncate change is load-bearing and tested: with it disabled, the #301 case's emergency-truncate node trims the overflowing tool output → preflight loop breaks early via currentTokens < limit → result.failure stays undefined → the actionable "Raise the model context window" remedy is lost (falls back to the generic message). Status is still 502 either way, so this is a message-quality fix that the modified #301 test now pins down.
  • server.ts correctly re-measures the rebuilt (normal-config) payload instead of trusting result.payloadEstimate — the relaxed-zone processTurn trims that estimate more, which would otherwise turn a guaranteed-400 forward into a false "fits". Right call.

Code quality (per AGENTS.md): no as any, no @ts-ignore, no console.error in server modules (uses log/deps.log), no <acp> XML introduced, config objects are spread (no mutation of the caller's config). Comments are explanatory and match the file's existing #NNN style.

Two non-blocking notes:

  1. The "e2e" tests live in tests/preflight-fail-fast.test.ts (mock-upstream e2e via startServer), not the tests/e2e/ real-client suite from Model: glm-5.3 — test: e2e suite — real codex through bili (warmup/load/compress/purity/forge) #329. The "e2e" label in the PR body is accurate for what they exercise (real HTTP through the proxy) — just flagging the location differs from the tests/e2e/ dir in case that was the intent.
  2. The ×100 inflation factor in noEmergencyTruncate is a magic number; it's documented by the adjacent comment and is safe (only pushes usage below the truncate/nudge thresholds), so no action needed — just noting it.

The truncateLargeToolOutputs follow-up you scoped out looks right to leave for later (stateless, tool-results-only, redundant with the soft-zone relax for this repro). No changes requested.

@ranxianglei

Copy link
Copy Markdown
Owner Author

Re-reviewed on current master (2cbbeab, 252 commits ahead of the branch base) and pushed a verified merge head f78a63d.

The merge applied cleanly, but the post-flight restructure regressed #496: deciding forward/fail on estimateCoreMessages(...) alone drops the imageFloor (images ride the payload verbatim, #488), so an image-dominated payload with overflow evidence looked "fitting" on its text estimate and was forwarded again — the exact 400 loop #496 forbids. Fixed in f78a63d: both post-flight fit gates now measure estimateCoreMessages(...) + imageTokens < limit.

Verified on f78a63d: typecheck ✓ · full suite 1047/1049 (the 2 fails are the known plugin-agent.test.ts env flakes, pre-existing) · build ✓ · ACP_TEST_E2E=1 codex 4-phase PASS (242 s). The #330 e2e (soft zone relaxed → folded → forwarded) passes with kernel 0.0.52, so the noEmergencyTruncate window-inflation trick is still compatible with the pressure-band gate.

Ready to merge. PR#334 is the weaker alternative (forward-as-is) for the same livelock and is being closed as superseded — this PR implements the issue's preferred fix (relax soft protection, keep hard protectedTools).

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 ework agent · qwen3.8-27b

[bot] Independently verified the merge head f78a63d (checked out the branch, npm ci → acp-kernel 0.0.52). Green light.

Pre-flight (my run):

  • npm run typecheck — clean
  • npm run build — success
  • Targeted #496 / #488 / #301 / #330 e2e — 12/12 pass
  • Full suite — 1048/1049 on my runner; the single failure is not from this PR (below)

The #496 fix is real and load-bearing. I confirmed it empirically: stripping just the + imageTokens term from the two post-flight fit gates makes tests/issue496-forward-once-learn.test.ts fail (AssertionError — request 2 gets forwarded again, reintroducing the 400 loop); restoring it makes it pass. Traced the logic too — all four forward/fail sites now measure text + image cost consistently:

  • early-forward gate uses payloadEstimate < limit, where payloadEstimate = estimateCoreMessages(...) + imageTokens (src/server.ts:2528)
  • rebuild gate: estimateCoreMessages(rebuilt.processedMessages) + imageTokens < limit (src/server.ts:2618)
  • no-progress gate: estimateCoreMessages(prepared.processedMessages) + imageTokens < limit (src/server.ts:2619)

The rebuild path reuses the original imageTokens constant, which is correct — images ride the payload verbatim (#488) and are never folded, so their cost is invariant across the rebuild. So yes: the rebase dropped these terms when it rewrote the gates around estimateCoreMessages(...), and f78a63d puts them back. Matches your description exactly.

On the count (my 1048/1049 vs your 1047/1049): the one failure in my run is tests/launcher.test.ts → resolveClientCommand: codex/claude resolve to themselves. It asserts the resolver returns the bare command name when the binary isn't on PATH; in this sandbox a codex/claude executable is resolvable on the runner's PATH, so the resolver correctly returns the resolved path and the deepEqual trips. I confirmed it's pre-existing and environmental, not a rebase artifact — it fails identically on master (2cbbeab) and this PR never touches launcher.ts. Your 2-fail (plugin-agent flakes) is the same category surfacing in your cleaner env. Net: zero failures attributable to this change.

Not re-run: the ACP_TEST_E2E=1 codex 4-phase needs a live local LLM upstream I don't have in this sandbox, so I'm taking your 242 s PASS on that. Everything I could execute independently confirms the head.

#334 supersession — agreed. Forward-as-is (let the upstream arbitrate) is strictly weaker than relaxing the soft zone and actually folding the content back under the window; the two are semantically exclusive, so keeping both alongside #337 would conflict. It's already closed (your floor 6) with the right rationale.

Ready to merge on my end — per repo rules the merge itself stays yours.

…regression in post-flight fit gates
@ranxianglei
ranxianglei force-pushed the 2026-08-29_preflight-soft-zone-relax branch from f78a63d to bfe9e4c Compare September 6, 2026 06:25
@ranxianglei

Copy link
Copy Markdown
Owner Author

已修复 PR 显示的巨大 diff 问题。

为什么之前显示 251 commits / 143 files: 该分支此前用 merge 方式同步 master(head f78a63d 是个 merge commit),GitHub 把 diff 锚在 08-29 的老 base 上,于是把 master 一周的演进全部计入了 Files changed。

修复方式: 从当前 master(a184ff9, v0.1.87)rebase,cherry-pick -m 1 f78a63d 单提交落地。现在显示恢复为真实改动:3 files, +163/-29。

改动内容

文件 内容
src/preflight.ts (+52) 溢出时软区放宽:relaxedConfig() 将 preserveRecentMessages/Tokens 归零重试(硬 protectedTools 仍独立生效);noEmergencyTruncate() 让 fit 检查反映持久 payload 而非 per-turn 截断副作用;失败信息改为可操作文案(提窗口/重开会话)
src/server.ts (+45/-) 删除溢出时提前 502 "nothing left to fold"(#330 livelock 元凶);压缩后 fit 判定改用重建 payload 估计 + imageTokens(修 #496 图主导 payload 的假 fit 回归);stale-baseline 转发仅在 payload 确实装得下时
tests/preflight-fail-fast.test.ts (+95) e2e #330 场景:唯一可折叠内容都在软保护区内 → 放宽、折叠、转发 + upstream 捕获断言

Pre-flight

  • typecheck ✓
  • tests/preflight-fail-fast.test.ts 4/4 ✓
  • 全量 1063/1065(2 个已知 plugin-agent 环境失败,与 master 基线一致)✓
  • mergeable ✓(GitHub 显示 clean,无冲突)

Closes #330

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 ework agent · qwen3.8-27b

[bot] Verified the rebased head bfe9e4c. The display fix is correct and the change is green on the new base. Green light.

Display fix confirmed: bfe9e4c is a single commit with a single parent = a184ff9 (current master tip), so GitHub now anchors the diff correctly → clean 3 files / +163/−29 instead of the old 251-commit / 143-file view. That parent structure is what fixed it.

Content unchanged from what I validated last round: git diff f78a63d bfe9e4c -- src/preflight.ts src/server.ts tests/preflight-fail-fast.test.ts is empty — the rebase moved the base but didn't touch the PR's code, so all prior logic/code-quality review carries over verbatim.

Re-validated on the newer base (a184ff9, v0.1.87; note acp-kernel bumped 0.0.52 → 0.0.54, so I re-ran rather than assumed):

Closes #330 is accurate. Ready to merge on my end — the merge itself stays yours per repo rules.

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.

1 participant