Skip to content

fix: preflight tries all viable ranges before declaring exhaustion (#569) - #574

Closed
ranxianglei wants to merge 1 commit into
masterfrom
2026-09-06_preflight-try-all-ranges
Closed

ranxianglei wants to merge 1 commit into
masterfrom
2026-09-06_preflight-try-all-ranges

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

What

Preflight fail-fast returned 502 retryable=false with "no range could be compressed" while later spans of the same history were compressible — minutes later the session owner's in-band compress shrank it 88% (#569).

Root cause

preflightCompress (src/preflight.ts) picked exactly ONE range per round — the oldest viable startRef — and when that single range yielded no applied chunk (sub-minCompressRange chunk, empty content, unusable summary, or kernel apply rejection), it declared global exhaustion and broke out of the round loop. Remaining viable ranges were never tried, so one bad leading edge produced a false terminal verdict on a still-recoverable session.

In block-dense histories this is likely: the kernel's min-gate sums only non-summary message text per spec, while preflight's char precheck counts all view text including block summaries — so a leading range can pass preflight's check yet be rejected at apply time, or simply sit below minCompressRange after its blocks were folded.

Fix (src/preflight.ts)

  • Iterate ALL viable ranges (oldest-first, prefix-cache-stable) within a round until one yields an applied chunk; declare exhausted only after every range has been tried and nothing applied.
  • A range whose summary was unusable / apply failed / refs unresolvable is added to a skip-set (refs are stable across folds) so it is never retried within the invocation — no wasted upstream calls, no infinite churn.
  • New cost cap MAX_SUMMARY_CALLS_PER_PREFLIGHT = 8 bounds summarization calls per preflight invocation (legacy worst case was also 8×1); hitting it reports a distinct budget-exhausted detail instead of the generic one.
  • Exhaustion detail now names how many ranges were tried, making the 502 truthful and diagnosable.
  • Upstream-error (429→503 retryable), abort, and credit math semantics unchanged. No wire-shape change: mode-agnostic core only (both plugin and proxy modes affected identically, per AGENTS.md §6).

Tests (tests/preflight-multi-range.test.ts, new)

E2E through the real proxy with a mock Anthropic upstream:

  1. Regression: 3-segment over-window conversation where the oldest segment's summary is unusable → now 200 with the next range folded (legacy code: 502, verified RED via stash).
  2. Truthful exhaustion: every summary unusable → 502 only AFTER all ranges tried (summaryCalls ≥ 2; legacy made exactly 1), no forward, no blocks.
  3. Budget cap: 12 segments all unusable → exactly MAX_SUMMARY_CALLS_PER_PREFLIGHT summarization calls, budget variant in the error.

Pre-flight

  • npm run typecheck: pass
  • New suite: 3/3 pass; RED confirmed on legacy code (test 1: expected 200, actual 502)
  • Full npm test: 1050/1051 — the single failure (launcher.test.ts resolveClientCommand codex) is pre-existing and environment-specific (sandbox resolves codex to /usr/bin/codex); fails identically on pristine master, unrelated to this change
  • npm run build: success

@github-actions

github-actions Bot commented Sep 6, 2026

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-09-06_preflight-try-all-ranges (363974f)

Option A — Install from npm PR tag (recommended)

npm install -g billion-context@pr-574

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

Option B — Download artifact

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

This comment is automatically updated on each push.

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 ework agent · qwen3.8-27b

Implemented, verified, and opened a PR: #575

Triage (validated before writing code). Reproduced the defect against the actual code, not just the report: in src/preflight.ts the round loop picked exactly one range per round (the oldest) and, when it yielded no applied chunk, set a global exhaustion verdict and broke out — remaining viable ranges were never tried. One bad leading edge ⇒ false terminal 502 on a recoverable session. Your root-cause analysis holds; I additionally confirmed the skip-set keying is sound before relying on it — refs are content-fingerprinted (byRaw[m.id]), so they stay stable across folds and a range found unusable remains correctly skipped in later rounds.

Fix (src/preflight.ts).

  • Walk all viable ranges oldest-first until one folds; declare exhausted only after every range has been tried and nothing applied.
  • Skip-set for ranges whose summary was unusable / apply failed / refs unresolvable — never retried within an invocation (no wasted upstream calls, no infinite churn).
  • New cost cap MAX_SUMMARY_CALLS_PER_PREFLIGHT = 8; hitting it reports a distinct budget-exhausted detail instead of the generic one.
  • Exhaustion detail now names how many ranges were tried (truthful, diagnosable 502).
  • Upstream-error (429→503 retryable), abort, and credit-math semantics unchanged. No wire-shape change: mode-agnostic core only, so both plugin and proxy modes are affected identically (§6).

Tests (tests/preflight-multi-range.test.ts, new). E2E through the real proxy with a mock Anthropic upstream whose summarization responses are usable/unusable per call:

  1. Regression — oldest range's summary unusable → now 200 with a later range folded (legacy: 502).
  2. Truthful exhaustion — every summary unusable → 502 only after all ranges tried (summaryCalls ≥ 2; legacy made exactly 1), no forward, no blocks.
  3. Budget cap — 48-message history, all unusable → exactly MAX_SUMMARY_CALLS_PER_PREFLIGHT summary calls, budget variant in the error.

Verification. RED confirmed on legacy by stashing the fix: test 1 expected 200 got 502, test 2 failed on summaryCalls ≥ 2, test 3 failed on the budget variant + exact count. On the fix: new suite 3/3 green · npm run typecheck pass · npm run build success · full npm test 1050/1051 — the single failure is a pre-existing, environment-specific case in launcher.test.ts (resolveClientCommand), caused by how this sandbox resolves the codex binary; it fails identically on pristine master and is unrelated to this change.

Content branch only — version untouched. Merge is yours (agents don't merge).

ranxianglei pushed a commit that referenced this pull request Sep 6, 2026
)

Preflight fail-fast returned a global exhaustion verdict when its single
picked range (the oldest) yielded no applied chunk — one bad leading edge
(unusable summary / sub-min chunk / apply rejection) produced a false 502 on
a still-recoverable session while later spans were compressible (#569).

- Walk every viable range oldest-first until one folds; declare exhausted only
  after all have been tried.
- Skip-set for ranges found unusable (refs are content-fingerprinted, stable
  across folds) so they are never retried within an invocation.
- New cost cap MAX_SUMMARY_CALLS_PER_PREFLIGHT=8 bounds summarization calls;
  hitting it reports a distinct budget-exhausted detail.
- Exhaustion detail names how many ranges were tried. Upstream/abort/credit
  semantics unchanged; mode-agnostic core only.
@ranxianglei

Copy link
Copy Markdown
Owner Author

Closing as duplicate of #575 — superseded.

Same fix (preflight walks every viable range before declaring exhaustion), but #575 ships the refined implementation: skip-set keyed on startRef:endRef (refs are assigned exactly once per message id in acp-kernel's assignRefs, so keys stay stable across folds), no wasted summarization calls on the remaining chunks of an already-failed range, non-exported budget constant, and the rewritten E2E suite.

#575 was just rebased onto current master (8ecca23) and re-verified there: typecheck pass, new suite 3/3 (RED confirmed against legacy code), full suite green apart from the known sandbox-specific launcher/codex case, build success. Merge into master is textually clean (test-merged locally).

If you'd rather keep this iteration, reopen it and say which variant you prefer.

@ranxianglei ranxianglei added the duplicate This issue or pull request already exists label Sep 6, 2026
@ranxianglei ranxianglei closed this Sep 6, 2026
ranxianglei pushed a commit that referenced this pull request Oct 1, 2026
… all four platforms

The #1841 pre-walk gate correctly skips scenario C's round (image floor
~1.1M alone exceeds the ~999K window; the ~4.4K foldable filler pool
cannot close the ~110K gap), so the old 'ranges were folded' assertion
no longer holds — the same contract the two #574 assertions were already
migrated to. Keep the 502/hatch-closed pins, pin the new zero-call
behavior + 'futile round' verdict wording, and assert the baseline stays
exactly untouched (it was never re-poisonable here: the armed 1,072,519
usage baseline sat above any text+overhead write-back value, so the old
write-back assertion passed trivially even before this PR).
ranxianglei pushed a commit that referenced this pull request Oct 6, 2026
… all four platforms

The #1841 pre-walk gate correctly skips scenario C's round (image floor
~1.1M alone exceeds the ~999K window; the ~4.4K foldable filler pool
cannot close the ~110K gap), so the old 'ranges were folded' assertion
no longer holds — the same contract the two #574 assertions were already
migrated to. Keep the 502/hatch-closed pins, pin the new zero-call
behavior + 'futile round' verdict wording, and assert the baseline stays
exactly untouched (it was never re-poisonable here: the armed 1,072,519
usage baseline sat above any text+overhead write-back value, so the old
write-back assertion passed trivially even before this PR).
LoveDoLove pushed a commit to LoveDoLove-Forked-Projects/billion-context that referenced this pull request Oct 12, 2026
…istic (ranxianglei#2832)

- diagnoseEmptySummary gains per-wire block fingerprints (text_chars / text_blocks / thinking_blocks / reasoning_chars / text_parts) so a thinking-only answer is distinguishable from relay-stripped text in logs and fail-fast details
- new isDeterministicEmptyCompletion: a normal stop (end_turn / stop / completed) carrying zero answer text is deterministic for that (model, host) pair — it earns neither ranxianglei#1767's same-span retries nor ranxianglei#726's halving recovery, because re-sending the identical payload returns the identical empty answer and a normal stop is not a size signal
- three consecutive deterministic empties on the same (host, model) arm a session-persisted dead-probe verdict (session.metadata deterministicEmptyProbe); while dead, at most ONE canary probe per preflight invocation bills the wall, and a successful canary clears the verdict so folding resumes on a self-healed relay
- fail-fast detail names the actionable exits: configure compress.externalSummary, switch the session's model, or fix the relay's thinking/text handling
- tests: classifier matrix + fingerprint pins (unit), systemic e2e (bounded strikes, sticky verdict, exactly-one-canary contract, nothing forwarded), recovery e2e (canary clears, fold proceeds, 200); ranxianglei#574 multi-range fixture moves to max_tokens so that file keeps pinning the walk/budget mechanics on the size-driven path independent of this reclassification
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

duplicate This issue or pull request already exists

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant