Skip to content

[PR] fix: preflight one-shot gate must not block a replaced epoch's first fold (#7545) - #7558

Merged
ranxianglei merged 1 commit into
masterfrom
2026-11-23_preflight-gate-epoch-exception
Nov 23, 2026
Merged

ranxianglei merged 1 commit into
masterfrom
2026-11-23_preflight-gate-epoch-exception

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Follow-up to #7546 (merged 2026-11-22, commit 9ad612e). Post-merge CI went red on all 3 test shards (ubuntu-24/1, ubuntu-22/1, windows-22/2) on the same test: e2e #6069: history replaced under the same session id, then grown past the window: the turn is folded and forwarded (tests/issue6069-epoch-pause-over-window.test.ts:231, 'preflight summarized the new history' — expected true, actual false). Not one of the two declared pre-existing failures.

Root cause

The one-shot gate blocks preflight whenever the session has committed folds (compressionCount > 0) — but committed folds belong to the epoch that produced them. When the host wholesale-replaces the history under the same session id, fold-reconcile latches the epoch confirmation (#3641, foldEpochConfirmPending) and preflight itself suspends on that same latch (src/preflight.ts:1873: fits → verbatim pause, over-window → fold anyway). The gate did not read the latch, so the new epoch's first over-window turn was blocked on the REPLACED epoch's committed count and forwarded unfolded — the exact #6069 contract ("the paused preflight folds once the payload exceeds the window") regressed.

Empirical trace (pre-merge baseline, instrumented): old 24-msg history folds (5 blocks) → unrelated 12-msg history resent 3× → old blocks deactivated, drift streak 3 → latch set → new history grows over-window → gate (merged code) blocks on compressionCount>0 → no summary → assertion fails. With the fix: the gate sees the pending latch, the new epoch's one-shot fires, the fold commits, the in-request re-prepare reconcile clears the latch on the first clean pass, and the gate blocks again on the fresh committed count.

Changes

  • src/server.ts (gate): skip the committed-fold block while epochConfirmPending(session) — the new epoch gets its own one-shot attempt. The preflightRuns (spent-attempt) branch is NOT epoch-exempted here; it is re-armed at the latch site instead, so an aborted attempt on the new epoch still kills its retry (livelock protection holds per-epoch).
  • src/fold-reconcile.ts (re-arm): at the latch-set site, if (session.stats) delete session.stats.preflightRuns; — an aborted attempt on the REPLACED epoch must not starve the new one. Successful folds never set the latch, so the one-shot still holds within any single epoch. (Runtime guard: unit-test session fixtures omit stats.)
  • tests/preflight-one-shot-gate.test.ts: new case E (POSITIVE, paired against B2's NEGATIVE): old epoch folds → 3 replacement passes latch the epoch → grown over-window turn folds again (new summary call, summary in forwarded payload, newest turn verbatim, latch clears) → next over-window turn is forwarded as-is (the exception is scoped to the latch window; the new epoch's one-shot is still ONE shot).

Behavior change (disclosed)

Preflight one-shot gate: old (post-#7546) — a session with any committed fold is blocked from proxy-side preflight folding for the rest of its lifecycle, including after a wholesale history replacement under the same session id. new — while the #3641 epoch-confirm latch is pending (replacement confirmed, old substrate suspended), the committed-fold block is suspended and the new epoch gets its own single attempt; once the new fold commits and the latch clears on the first clean pass, the gate blocks again on the fresh committed count. The livelock fix for #7545 (abort → no retry; managed session → no re-fold) is unchanged for non-replaced sessions.

Verification (targeted, local)

Closes #7545

…fold (#7545)

The merged one-shot gate (#7546) blocks preflight whenever the session has
committed folds — but committed folds belong to the epoch that produced
them. When the host wholesale-replaces the history under the same session
id, fold-reconcile latches the epoch confirmation (#3641) and preflight
itself suspends on the same latch (src/preflight.ts); the gate must suspend
with it, or the new epoch's first over-window turn is forwarded unfolded
(regression: the e2e in tests/issue6069-epoch-pause-over-window.test.ts,
red on all 3 test shards of #7546 after merge).

- gate (src/server.ts): skip the committed-fold block while
  epochConfirmPending(session) — the new epoch gets its own one-shot
- re-arm (src/fold-reconcile.ts): an aborted attempt on the REPLACED epoch
  must not starve the new one — clear preflightRuns at the latch site
  (successful folds never set the latch, so the one-shot still holds
  within any single epoch)
- test: case E in preflight-one-shot-gate (old epoch folds -> 3 replacement
  passes latch -> grown turn folds again -> next over-window turn blocked:
  the exception is scoped to the latch window)
@ranxianglei ranxianglei changed the title fix: preflight one-shot gate must not block a replaced epoch's first fold (#7545) [PR] fix: preflight one-shot gate must not block a replaced epoch's first fold (#7545) Nov 22, 2026
@github-actions

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-11-23_preflight-gate-epoch-exception (434bf68)

Latest build — install this exact version

npm install -g billion-context@0.0.11474-pr.7558.dd12eed

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

Recent builds (commit → version)

commit version build
434bf68 billion-context@0.0.11474-pr.7558.dd12eed build

Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pr7558.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] 🏷

Review of PR #7558 — follow-up to #7546, closes #7545

I independently reproduced the bug, reviewed the diff, and ran the targeted suites. Verdict: mergeable.

Bug is real (reproduced on pre-fix). Checked out the PR's parent (9ad612e = #7546 merge) and ran tests/issue6069-epoch-pause-over-window.test.ts: it fails with exactly preflight summarized the new history — actual: false — the post-#7546 CI red on all 3 shards. The merged one-shot gate blocked the new epoch's first over-window turn on the REPLACED epoch's committed compressionCount.

Fix is correct and scoped. Two coordinated changes:

The exception is correctly scoped to the latch window: once the new fold commits and the latch clears on the first clean pass, the gate blocks again on the fresh committed count. Case E's final assertion pins this (the new epoch's one-shot is still ONE shot).

No regression for non-replaced sessions. The change only alters behavior while the latch is pending (a confirmed wholesale replacement — 3 consecutive drift passes, no anchor match). Cases B/B2/C (managed session NOT replaced, aborted attempt) all pass unchanged. The #7545 livelock fix (abort → no retry; managed → no re-fold) is preserved.

Verification (targeted, local):

Minor observations (non-blocking, pre-existing):

  1. preflightGateBlockedLogged (src/server.ts:2253) is a module-level Set never cleared. With the epoch exception, a session can now block → unblock (replacement) → block again, but the warn log only fires on the first block. Pre-existing (never cleared even across compression resets), observability-only. A clean fix would risk a server↔fold-reconcile import cycle, so I left it for the owner to weigh.
  2. The if (session.stats) guard at src/fold-reconcile.ts:2521 is for unit-test fixtures; in production session.stats is always zeroStats() (and the gate at src/server.ts:4180 reads it unguarded, so it must be defined). Deliberate defensive guard, no production effect.

结构化八问(中文):

  1. 是什么问题: [PR] Add a one-time gate to preflight collapse-before-forwarding: only triggers on first use for new conversations / old sess | preflight 转发前折叠加一次性门槛:只对新对话/未用过 bili 的老会话首次触发,掐断死循环从机制上不可能 (#7545) #7546 的一次性门槛把「会话有已提交折叠」当成终身封禁,但已提交折叠属于产生它的那个 epoch;宿主在同一 session id 下整体替换历史后,新 epoch 的第一个超窗轮次被旧 epoch 的提交计数挡住、原样转发不折叠 —— 正是 Context limit exceeded. | 上下文超了 #6069 契约「暂停的 preflight 在 payload 超窗后折一次」的回归。
  2. 什么原因导致的: 门槛只读 compressionCount > 0,不读 Snapshot as session reconstruction: identity claim order takes priority, message hash demoted to reference signal (root- | 快照即会话重构:身份认领顺序优先,消息哈希降级为参考信号(根治 #3588/#3640 一类误判) #3641 的 epoch 确认闩(latch)。整体替换时 fold-reconcile 置闩、preflight 自身也在同一闩上暂停,但门槛没读这个闩,于是新 epoch 被旧 epoch 的提交计数卡死。
  3. 影响范围与严重程度: 仅「同一 session id 下整体替换历史且新历史超窗」这一窄路径;触发 = 宿主原生压缩/全新对话复用同一 id + 3 连漂 + 新历史超窗。严重度:该路径下 preflight 对新 epoch 永久失能(新 epoch 永远不折),但 使用pi 压缩后就提示prompt is too long #1195 in-request refold + egress fuse 仍在兜底、nudge/compress 工具路径仍可折 —— 属「特定条件下功能失能」,非完全不可用。
  4. 怎么修复的: 让门槛 epoch 感知 —— 闩 pending 期间挂起「已提交折叠」封禁(新 epoch 拿到自己的一次机会),闩置位处重置 preflightRuns(被替换 epoch 的中止尝试不饿死新 epoch);闩在首个干净 pass 清除后,门槛按新的提交计数重新封禁。一次性语义在单个 epoch 内不变。
  5. 是否有回归问题: 无。改动只在闩 pending(确认的整体替换)时改变行为;未替换会话(B/B2/C 三例)行为逐字不变;Preflight pre-forward collapse triggers infinitely on sessions already under compression management, forming a 300s time | preflight 转发前折叠对已被压缩管理的会话无限重复触发,慢摘要下形成 300s 掐断死循环(用户 25 分钟零产出) #7545 活锁修复(中止→不重试、已管理→不再折)保留;新 epoch 仍是一次(用例 E 末段断言钉住)。高危模式逐项过:语义漂移仅限闩窗口(已披露旧→新);fail-fast 未变吞错;阈值/时序/输出格式未动;缓存/重试未触及。
  6. 是否破坏缓存: 不涉及。preflight 门槛不触碰 prompt cache、重试、增量游标、去重键。
  7. 是否改了设计方向: 不涉及。仍是 proxy 侧一次性 preflight 折叠 + 使用pi 压缩后就提示prompt is too long #1195/egress fuse 兜底;只是把「生命周期一次性」细化为「epoch 一次性」。
  8. 是否引入了新的配置: 不涉及。无新 env/config/默认值。

判定:可合。 单模块修复、回归测试复现原 bug 且现过、CI 全绿、不触碰 kernel/update/wire/config/持久化。建议 owner 合并。

@github-actions

Copy link
Copy Markdown
Contributor

📦 Merged Build Published

This merge to master was published as 0.0.1281-master.73844f5 (rolling master tag).

npm install -g billion-context@0.0.1281-master.73844f5  # 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.

ranxianglei pushed a commit to werifu/billion-context that referenced this pull request Nov 23, 2026
…ght epoch-exception fix

The branch was cut from 9ad612e, before the ranxianglei#7558 fix (434bf68)
landed on master. The e2e in tests/issue6069-epoch-pause-over-window.test.ts
went red on all 3 test shards (windows 22/4, ubuntu 22/2, ubuntu 24/2)
because the branch carried the ranxianglei#7546 one-shot gate without the ranxianglei#7558
epoch-exception fix. This merge brings in the fix; the contributor's
commit is unchanged and the PR diff still shows only that commit.
ranxianglei pushed a commit that referenced this pull request Nov 23, 2026
…lict, round 7)

Master moved 0b68ade -> 15f06c6 (kernel 0.0.205, +preflight
emergency-digest default-off #7549, +preflight 413 passthrough #7548,
+sessions reattach #7512, +opencode user-fork detection #5511,
+acp-status uncompressed nudge-align #7536, +preflight gate epoch
exception #7558, +413 egress fuse pins #7488, +Korean README #7551,
+many host/test changes).

Conflicts: 3 version-only files (kernel/package.json,
kernel/package-lock.json, root package-lock.json). Resolved to kernel
0.0.206 (merged kernel = superset of master's 0.0.205 + PR #6720/#6762
changes; kernel/src changed vs master => must bump above master's 0.0.205).

Auto-merged clean: kernel/src/compress.ts (PR markers 13x + master
changes), src/stream.ts (PR markers 3x), src/server.ts, master's new
test files, docs.

Local verification: typecheck ✓, build ✓, kernel 1671 pass / 0 fail,
host nudge/receipt 7 pass / 0 fail, kernel-guard ✓ (SRC_CHANGED=2
BUMPED=1 0.0.205->0.0.206), version-guard ✓ (root 0.1.216 untouched),
lock-sync ✓.
ranxianglei pushed a commit that referenced this pull request Nov 23, 2026
The 3 red shards (ubuntu-22/1, ubuntu-24/1, windows-22/1) all fail the
same inherited master-level test (e2e #6069, tests/issue6069-epoch-pause-
over-window.test.ts:231) — the #7546 one-shot-gate regression of the #6069
contract. That fix landed on master via PR #7558 (73844f5). Re-merging
master brings it in; no branch-side code change.

Verified locally: #6069 suite 3/3 green, PR's own suites 27/27 green,
typecheck clean.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

1 participant