Skip to content

fix: 一次瞬时 409/503 不再把增量压缩会话永久打回全量重发 - #3377

Merged
ranxianglei merged 3 commits into
masterfrom
fix/incremental-transient-recovery
Oct 16, 2026
Merged

ranxianglei merged 3 commits into
masterfrom
fix/incremental-transient-recovery

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

ai: zhipuai-lb/glm-5.3

什么坏了(#3369)

增量会话碰上第一次 lane 抖动就永久死亡:所有 adapter 对 409 和 503 一律 markIncrementalDead,而这个 dead 状态没有任何复活路径——planIncrementalTurn 直接短路 skip,ack 永远等不到,会话在宿主进程剩余生命周期里全部走全量重发。sidecar 开机抖一下、上游在本地 apply 和转发之间回一个 5xx、或兄弟进程 re-seed 一次,就够触发。而客户端自己的恢复钩子(forceResync)一直存在,只是没有任何 adapter 调过它。

新语义

场景 旧行为 新行为
一次 409(seq 漂移) 永久 dead noteIncrementalRejection():删除 wire 状态 → 下一轮用全量 resync:true 重播种(它不可能再 409)——下一轮就自愈
连续 3 次 409(中间无 ack) 第一次就 dead 熔断(REJECTION_CIRCUIT = 3)→ 保持旧的永久回退,日志单列
503(lane 暂不可用) 永久 dead 会话 ack 状态保留,只清探测缓存 → 下一轮重新探测恢复
任何 200 ack — 清零熔断计数

四个 adapter(pi/omp/opencode/dsh)同步改;dsh 顺带修了 200 无 ack 头时误消费 pending 条目的问题(那是单飞行跳过的并发请求的响应)。

测试

  • 新 tests/incremental-client-recovery.test.ts:一次 409 → re-seed 形状(resync:true/sinceSeq:0/全量);连续 3 次熔断;ack 清计数;无状态 rejection 无副作用。
  • opencode seam 测试从「一次 409 就死」重写为「一次 409 重播种;三次才死」。
  • incremental-lane 17/17,native-intercept 58/58,omp/opencode 增量套件全绿,tsc 干净。

Closes #3369。来自 #3331 区间审查(B-H1)。

Incremental conversations died forever on their first lane hiccup
(#3369): every adapter answered 409 AND 503 with markIncrementalDead,
which has no revive path — planIncrementalTurn short-circuits to skip,
so no ack can ever arrive and the conversation spends the rest of the
host process lifetime on full resend. One sidecar blip at boot, one
upstream 5xx between the server's local apply and the forward, or one
sibling re-seed was enough. The client's own recovery hook
(planIncrementalTurn's forceResync) existed but no adapter ever used it.

New semantics (#3369):
- 409 (seq drift): noteIncrementalRejection() — one rejection DELETES the
  wire state, so the next turn re-seeds with a wholesale resync:true body
  (which cannot 409). Transient drift self-heals on the very next turn.
  Three CONSECUTIVE rejections with no ack between trip a sticky kill
  switch (REJECTION_CIRCUIT = 3) — persistent drift keeps the old
  permanent-fallback behavior, now with a distinct log line.
- 503 (lane unavailable): adapters keep the conversation's ack state and
  only drop the probe cache; the next turn re-probes and resumes. A boot
  blip no longer costs the conversation its incremental lane.
- noteIncrementalAck clears the rejection circuit (recovery restarts it).

Adapters updated: pi.ts, omp-native.ts, opencode-native.ts, dsh-native.ts
(dsh also stops consuming its pending entry on a 200 without an ack
header — that response belongs to a request single-flight skipped).

Tests: tests/incremental-client-recovery.test.ts (reset re-seed shape,
circuit trip, ack clears circuit, stateless rejection no-op) and the
opencode seam test rewritten from 'one 409 kills' to 'one 409 re-seeds;
three kill'. incremental-lane 17/17, native-intercept 58/58,
omp/opencode incremental suites green, tsc clean.

Refs #3369
@github-actions

github-actions Bot commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: fix/incremental-transient-recovery (08bb9c5)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.3985-pr.3377.a457ed8

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-pr3377.tgz
npm install -g package

Install instructions are refreshed on each push.

… header (#3369)

Review finding on #3377: the PR description claims dsh stops consuming its
pending entry on a 200 without an ack header (the response belongs to a
concurrent request single-flight skipped), but an unconditional
incPendingAcks.delete(url) at the top of the handler still consumed it —
the added comment described behavior the code did not perform.

Move the delete down so only responses that settle the turn consume the
entry; a 200 without the ack header now keeps the entry for the marker'd
response still in flight. Every other path (200+ack, 409, 503, other
statuses) behaves exactly as before.
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

(ai: vllm-qwen/qwen3.8-27b)

查重: 不重复。 #3377 是 #3369 的唯一修复项,检索近期 open/closed issue 与 PR,无同签名(409/503 → 永久全量回退)的其他在途条目。

审核说明:独立通读了全部 7 个文件的 diff、master 上的改动前代码与服务端 lane 契约(src/incremental-lane.ts),未复用 issue 侧结论。

① 方向关:方向正确。修的是根因层——客户端状态机「dead 无复活路径」(dead 后 plan 恒短路 skip,ack 永远到不了),不是症状层打补丁;issue 讨论的选项 A(reset + 熔断)被采纳。独立验证了自愈论证的两半:resync:true 确实绕开服务端 seq 检查(incremental-lane.ts:255-262),所以一次 409 后下一轮全量重播种在结构上不会再撞 seq-mismatch;但 turn-in-flight(line 368,对所有请求含 resync 生效)与冷重建失败(line 384-385)仍可对 resync 回 409——因此 3 连击熔断是必要且合理的兜底,持久漂移保持旧的永久回退行为。

② 回归清点(diff 触碰路径的既有行为逐项判定):

  • 409 处理:旧=立即永久 dead → 新=首次 reset(下一轮重播种)、连续 3 次才 dead。有意变更,PR 表格已披露(旧行为=新语义下熔断立即触发的特例)。
  • 503 处理:旧=dead+清探测缓存 → 新=只清探测缓存、会话状态保留。有意变更,已披露。
  • 200+ack:逻辑不变,新增清零熔断计数(纯增量,安全)。
  • 其他状态(500/413 等):不变(消费 pending、不动会话状态),与 issue 里商定的「5xx 什么都不动、由下一次 409 驱动恢复」一致。
  • seed/delta/resync 规划逻辑、探测 TTL(30s)、markIncrementalDead 原语(仍被 tests/incremental-lane.test.ts 使用,非死代码):不变。
  • 服务端:inFlight 释放在 finally 内(incremental-lane.ts:464-467),forward 抛错也不漏锁;ack 头注入点未动。
    未发现无意回归。

③ 代码关 — 发现一处注释与代码不符,已在分支上直接修掉:PR 描述称「dsh 顺带修了 200 无 ack 头时误消费 pending 条目的问题」,但 diff 里 handler 开头的 incPendingAcks.delete(url) 仍然无条件先消费条目,实际只加了注释和 return——注释声称「keep the pending entry」,代码并没有做到。已重排:只有结算该回合的响应(200+ack / 409 / 503 / 其他终态)才消费条目,200 无 ack 头保留给仍在途的 marker'd 响应;其余路径行为与之前完全一致。commit fc60e8c55(标注 fix(dsh)),本地 tsc 干净 + 相关套件 100/100 绿,已推送,等 CI 对新 head 复跑。

④ 测试有效性:真测试。新 tests/incremental-client-recovery.test.ts 4 例断言都有牙(重播种体形状 resync:true/sinceSeq:0/全量、第 3 次熔断、ack 后计数归零、无状态 rejection 无副作用);opencode seam 测试重写为完整闭环(seed→ack→409→断言重播种形状→ack 清零→3×409→dead→全量重发)。本地定向套件(recovery / opencode / omp / incremental-lane / native-intercept×2)100/100,tsc 干净;远端 CI 旧 head 全绿(test shards ×7、static-gates、kernel-guard、config-surface-guard、lock-sync、unused-exports 等)。

两个非阻塞观察(不扩本 PR 范围,建议挂 #3369 后续清单):

  1. omp/opencode/pi 有同样的「无条件先删 pending 条目」模式(omp/opencode 按 URL 键、pi 按 sid 键)。新语义下影响有界——丢一次 ack 至多多花一轮 409+resync 即自愈——所以本 PR 不顺手改另外三条线(需要各宿主的并发证据和一轮独立测试);需要的话每处几行即可对齐。
  2. planIncrementalTurn 的 forceResync 参数仍无任何调用点(issue 分析也指出这是历史遗留);本 PR 之后恢复走 reset 路径,该参数的文档用途已过时。删留由作者定(删除会波及数处测试的 mode 断言,不算几行级小修)。

八问:

  1. 是什么问题:增量压缩会话碰上第一次瞬时车道抖动(409 seq 漂移或 503 车道暂不可用),就被永久降级为全量重发,直到宿主进程结束,静默丢失后续所有增量收益。
  2. 什么原因导致的:四个 adapter 对 409 和 503 一律调 markIncrementalDead,而 dead 状态没有任何复活路径——plan 见 dead 恒短路 skip,ack 到不了,于是永久 dead。协议本来就定义了「409 后用全量 resync 恢复」,只是客户端从未实现这个恢复步骤(forceResync 钩子一直无人调用)。
  3. 影响范围与严重程度:开启了增量功能的用户(默认 0% rollout,opt-in)且会话遭遇瞬时抖动(sidecar 开机抖动、上游在 apply 与转发之间回 5xx、兄弟进程 re-seed 等)。严重度:正确性无损(全量重发字节等价),代价是永久失去带宽节省 + 日志噪音,属「特定条件下有问题」。
  4. 怎么修复的:客户端状态机引入「拒绝熔断」——单次 409 只删 wire 状态,下一轮以全量 resync:true 重播种(结构上不会再撞 seq-mismatch),下一轮即自愈;连续 3 次 409(中间无 ack)才触发粘性 kill switch,保持旧的永久回退并单列日志;503 不动会话状态,只清探测缓存让下一轮重新探测;任何 200 ack 把熔断计数清零。
  5. 是否有回归问题:无。受影响的既有行为全部是有意变更且 PR 表格已披露旧值→新值(409:立即死→重置后三连击才死;503:死→保状态);其余路径(200+ack、其他状态码、规划逻辑、探测缓存、服务端 inFlight 释放)逐项核对不变。
  6. 是否破坏缓存:不涉及。前缀缓存/重试/增量游标策略均未变;lane 的前缀稳定性依赖服务端 canonical,本 PR 未触碰。
  7. 是否改了设计方向:不涉及。仍是「服务端 seq 为唯一权威 + 客户端 diff 发 delta」,本 PR 只是补上协议悬空的那一步恢复。
  8. 是否引入了新的配置:不涉及。REJECTION_CIRCUIT = 3 是内部常量,未暴露为配置/env。

结论:可合。 上述小修 commit fc60e8c55 推送后,等新 head 的 CI 转绿即可合并(分支落后 master 9 个提交但不触碰本 PR 文件,无冲突,无需 rebase)。

ranxianglei pushed a commit that referenced this pull request Oct 16, 2026
Two statements (the 200-no-ack '}' close and the 'if (409)' open) landed on
one line during the #3377 onResponse rewire — valid JS but inconsistent with
the file's one-statement-per-line style. No behavior change.
…other three ack channels (#3369)

The same top-of-handler delete that fc60e8c fixed for dsh existed in the
omp, opencode and pi handlers: any response matching a pending key consumed
the slot, so a concurrent full-resend request whose headerless 200 arrived
first dropped the marker'd turn's real ack (lost ack → spurious 409 →
re-seed cycle). Same mechanism family — one uniform rule across all four
channels: consume the pending slot only when the response settles it (valid
200+ack / 409 / 503 / other), keep it on a 200 without a valid ack header.

Lingering entries are bounded: omp/opencode staleness TTL (30s) or the next
plan's unconditional set (pi/dsh) clears them.

Also refreshes pi's stale ack-channel comment block (still described the
pre-#3369 dead-on-409 behavior) and pins the seam with a regression test:
an interleaved headerless 200 must not drop the real ack (verified red
without the fix, green with it).
ranxianglei pushed a commit that referenced this pull request Oct 16, 2026
…ending incremental entry (#3378 align)

The PR's own comment promised 'keep the pending entry for the marker'd response still in flight', but all four ack collectors deleted the entry unconditionally right after the lookup. A concurrent full-resend request's plain 200 (no seq header) arriving first would consume the entry, so the marker'd response's ack was lost: the client's sinceSeq lagged, the next turn replanned already-applied messages into the delta, the server answered 409, and #3377's recovery reset to a full resync — self-healing degradation, but it defeats the single-flight design this PR adds.

Invariant relied on: src/incremental-lane.ts installs a writeHead wrap that stamps x-bili-applied-seq on every diverted response that reaches forward(); every lane-written rejection (400/409/503) is emitted before that wrap and therefore never carries the header. So 200-without-seq is provably a foreign response: keep the entry (bounded by the existing 30s TTL); every other status keeps its previous consumption semantics. Regression test: dsh seam test step 2b feeds a foreign 200 before the marker'd response and asserts the next turn still plans a clean delta.
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

(ai: vllm-qwen/qwen3.8-27b)

追加审核:新 commit 08bb9c5d0(keep-pending 规则扩展到 omp/opencode/pi)— 通过。

上一条回复里留的观察 1(另外三条 ack 通道同样的「无条件先删 pending」模式)已被这个 commit 一并修掉,我独立核对了:

  • 规则形状正确:三条通道与 dsh(fc60e8c55)完全同构——只有结算该回合的响应(有效 200+ack / 409 / 503 / 其他终态)才消费 pending 槽位,200 无有效 ack 头保留给在途的 marker'd 响应。pi 顺手把仍描述旧「409 即死」行为的注释块刷新了(注释与代码事实对齐,好)。
  • 有界性声明属实(逐项查了代码):omp/opencode 的 pending 槽有 INC_PENDING_TTL_MS = 30_000 的 beforeSend 守卫(omp-native.ts:104-105、opencode-native.ts:846-847)——滞留条目最多让后续 30s 内的回合走全量重发(行为正确、只是少省一轮),到期后下一轮 plan 覆盖;pi/dsh 由下一轮 plan 的无条件 set() 覆盖。无泄漏路径。
  • 新 seam 测试有牙:a 200 without an ack header does not steal the pending slot 断言交错无头 200 之后下一轮是 delta(sinceSeq=1、2 条消息)。我推演过红路径:不修的话无头 200 偷走槽位 → 真 ack 到不了 → 状态不前进 → 下一轮重播种(sinceSeq=0/resync:true),断言必挂。本地实跑通过(opencode-incremental 5/5)。
  • 本地验证:build OK、tsc 干净、定向套件(recovery / opencode / omp / incremental-lane / native-intercept×2)全绿。
  • 远端 CI @ 08bb9c5d0:40/40 完成 — 28 success + 12 skipped(默认门控的 e2e 真客户端线)+ 0 失败(之前 fc60e8c head 上的 3 个 cancelled 是被这次 push 顶替,非故障)。

结论:可合。 三个 commit(原始修复 + dsh 小修 + 三通道扩展)合起来是一个闭环,CI 全绿,行为变更均已在 PR 描述披露。请人工合并:#3377

@ranxianglei
ranxianglei merged commit 83197e9 into master Oct 16, 2026
40 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

📦 Merged Build Published

This merge to master was published as 0.0.492-master.83197e9 (rolling master tag).

npm install -g billion-context@0.0.492-master.83197e9  # 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 added a commit that referenced this pull request Oct 16, 2026
…flight + TTL on the ack stash

native-intercept.ts:698-700) and marker stamping (state.headersFor, called
at :705) across a single closure-level boolean shared by EVERY session in
the process. Two concurrent model requests crossing that microtask
boundary interleaved: R1.beforeSend armed the flag -> R2.beforeSend ->
R2.headersFor consumed it -> R1.headersFor found it disarmed and R1's
delta body shipped with NO marker. The proxy forwarded the delta-only
messages verbatim (incrementalTurnBodyShape is only consulted under the
marker, handle.ts:146-152), so the model answered with no conversation
history — silent wrong answers, not an error.

- The marker now rides init.headers from the conversion itself (omp
  pattern): new Headers(init.headers).set(x-bili-plugin-turn, "1") right
  where init.body is swapped, atomic in the same hook invocation. withHeaders
  merges headersFor's own stamps on top, so the marker and the body can
  never part ways. headersFor's armed-flag branch is gone.
- Single-flight guard (omp-native.ts:104-106 pattern): while a marker'd
  request for the same URL awaits its ack, later requests ride the normal
  pipeline — their acks could otherwise collide with the pending entry
  keyed by the shared model endpoint URL. Pending entries carry at:
  Date.now() and expire after 30s (matches omp/opencode).
- Test seams: _stateBeforeSendForTest/_stateOnResponseForTest exposed
  alongside _stateHeadersForTest; the dsh mock proxy serves the
  /__bili/plugin/turn feature probe (turnEnabled flag).

Tests: new #3370 dsh test drives the seams end-to-end — marker+body
atomic on the same init, headersFor carries no marker, single-flight
leaves an overlapping request untouched, ack advances the next plan to a
real delta. Existing suites re-run green: native-intercept 58/58,
incremental-lane 17/17, opencode/omp incremental + recovery suites, tsc
clean. The dsh test nulls BILI_PROVIDER_REWRITES/BILLION_CONTEXT_PLUGIN
(hermetic pattern from the #1772 suite) so it runs on hosts whose shell
is itself behind a bili proxy.

Stacked on #3377 (409/503 recovery rewired the onResponse arms this test
also drives).

Closes #3370
ranxianglei pushed a commit that referenced this pull request Oct 16, 2026
…ending incremental entry (#3378 align)

The PR's own comment promised 'keep the pending entry for the marker'd response still in flight', but all four ack collectors deleted the entry unconditionally right after the lookup. A concurrent full-resend request's plain 200 (no seq header) arriving first would consume the entry, so the marker'd response's ack was lost: the client's sinceSeq lagged, the next turn replanned already-applied messages into the delta, the server answered 409, and #3377's recovery reset to a full resync — self-healing degradation, but it defeats the single-flight design this PR adds.

Invariant relied on: src/incremental-lane.ts installs a writeHead wrap that stamps x-bili-applied-seq on every diverted response that reaches forward(); every lane-written rejection (400/409/503) is emitted before that wrap and therefore never carries the header. So 200-without-seq is provably a foreign response: keep the entry (bounded by the existing 30s TTL); every other status keeps its previous consumption semantics. Regression test: dsh seam test step 2b feeds a foreign 200 before the marker'd response and asserts the next turn still plans a clean delta.
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