Skip to content

fix: follow the live intercept state across WS hot-reimport (#3558) - #3580

Merged
ranxianglei merged 1 commit into
masterfrom
2026-10-17_ws-hot-replace-live-intercept
Oct 17, 2026
Merged

ranxianglei merged 1 commit into
masterfrom
2026-10-17_ws-hot-replace-live-intercept

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

The Codex Responses WebSocket interceptor (src/agent/native-ws-intercept.ts) still used the pre-#3395 pattern: globalThis.WebSocket was wrapped once per process, with the wrapper's routed() closure bound to the first module instance's state. When a host hot-reimports the plugin (fresh module instance + fresh state, no reset in between), the wrapper kept routing through the dead first instance → silent direct egress on any WS lane.

This is the WebSocket half of #3394/#3395 (which fixed the fetch half). It ports the exact shape of that fix onto the WS chain.

How

  • Extract bindLiveStateSlot(state) in native-intercept.ts: one shared, generation-bumped bind of the process-level active-state slot. The fetch and WS installers both call it at the top of every activation (fetch behavior unchanged — verified by its full 62-test suite).
  • Export ACTIVE_STATE / ACTIVE_STATE_SEQ; the WS routed() now resolves the active state per connection instead of using the install-time closure. An empty slot (no active plugin) passes through direct rather than hitting a disposed context.
  • _resetForTest drops the shared slot alongside the WS flag.
  • Port the three [BUG] DSH 插件热替换后 fetch 拦截仍绑在旧模块实例上 → 模型请求全部直连、压缩静默失效直到重启 #3394 fetch regression tests to the WS lane (hot-reimport rebind / same-instance late-dispose no-op / latest-activation teardown→direct); confirmed they fail pre-fix and pass post-fix.

Behavior change (old → new)

Normal single-install operation is unchanged (one install binds the slot once, same state as before). Two deltas, both the intended fix:

  1. After a host hot-reimport, WS routing follows the newest applied instance (was: stuck on the first instance's dead state → silent direct egress).
  2. A model-shaped WS connection while the active-state slot is empty now passes through direct/unrewritten (was: routed through whatever state was captured at install time) — matching the fetch lane's empty-slot semantics.

No new config / env var / wire-format change. No kernel/ or pi-subagents/ change. Local targeted suites green (WS 22/22, fetch 62/62); typecheck + build clean; full-suite verdict deferred to CI.

Closes #3558

The Codex Responses WebSocket interceptor (src/agent/native-ws-intercept.ts)
still wrapped globalThis.WebSocket once per process with a closure bound to the
first module instance's state. A host hot-reimport that re-applies with a fresh
state left the wrapper routing through the dead first instance — silent direct
egress on any WS lane. This mirrors the fetch fix (#3394/#3395): the wrapper now
rebinds the shared process-level active-state slot on every activation and
resolves it per connection, so routing follows the newest applied instance; an
empty slot passes through direct instead of hitting a disposed context.

- extract bindLiveStateSlot() so the fetch and WS installers share one
  generation-bumped bind (fetch behavior unchanged)
- export ACTIVE_STATE / ACTIVE_STATE_SEQ; the WS chain reads them per connection
- _resetForTest drops the shared slot alongside the WS flag
- port the three #3394 fetch regression tests to the WS lane (hot-reimport
  rebind, same-instance late-dispose no-op, latest-activation teardown->direct)

Closes #3558
@github-actions

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-10-17_ws-hot-replace-live-intercept (e4f4675)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.4230-pr.3580.fec9fe7

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-pr3580.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] 🏷 (ai: vllm-qwen/qwen3.8-27b)

查重:不重复。 这是 #3558(WS 半边边界记录)的实现 PR;#3558 本身是 #3394/#3395(fetch 半边,已合入 master d924789)明确「当时只记风险不动代码」留出的兄弟项。检索 open+closed issue/PR:无其他条目在修「WS 拦截 install 闭包绑死第一个实例」这一机制。相关但不重复:#3447(WS client lane 上游拒升级时回退 HTTP/SSE)——失败域不同(上游拒绝 vs 宿主热替换后 state 失效),不冲突。

PR 形态核对:单 commit e4f4675ee 落在当前 master tip(b10c52576)之上,无冲突(mergeable),改动面 3 文件 +132/-26。开始独立 review(原 issue 方向 → diff 逐段 → 既有行为回归清点 → 测试有效性 → CI)。CI 现状:静态门禁全绿(gate/static-gates/version-guard/lock-sync/lane-guard/unused-exports/test-import-graph/docs-sync/config-surface-guard/pi-subagents-guard),test 分片 ubuntu-22-1 已过、其余 in_progress。结论按结构化八问随后给出。

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 (ai: vllm-qwen/qwen3.8-27b)

独立复审结论(未复用 issue 侧结论,diff 逐段重读、回归逐项清点、CI 日志下钻)

1. 是什么问题:宿主热替换插件模块时,fetch 拦截已在 #3395 修好,但 Codex Responses WebSocket 拦截还是旧模式——globalThis.WebSocket 每进程只包一次,包装器闭包绑死第一个模块实例的 state。热替换后新实例+新 state 登场,包装器仍按旧实例路由 → WS 通道模型流量静默直连(不压缩、无 ACP 会话)。

2. 什么原因导致的:install 函数每进程只真正执行一次(Symbol.for 标志幂等),而路由闭包捕获的是安装时刻的 state,不是调用时刻读取。热替换在旧实例与新实例之间没有任何 reset,旧闭包引用的 state 已死。根因是「安装时绑定」这一模式本身,与 fetch 半边 #3394 同族,不是 WS 层特有缺陷。

3. 影响范围与严重程度:当前潜伏——需要同时满足「宿主支持插件热替换」+「该宿主走 Codex Responses WS 通道」。据 #3558 排查,今天没有已知宿主同时满足(pi 用 WS 但不热替换;dsh 热替换但走 HTTP)。一旦未来某宿主两者兼备:该通道全部模型请求静默直连外发,无报错、无压缩,严重度高且难察觉。本修复提前恢复这条路径的可用性。

4. 怎么修复的:对齐 fetch 侧已验证的机制,不发明新东西——抽出 bindLiveStateSlot(state)(共享槽位+代际递增),两个传输的 installer 在每次激活顶部都调用它;WS 的 routed() 从安装时闭包改为每连接读共享槽位;空槽位(无活动实例)= 直接透传,绝不触碰已 dispose 的上下文。dispose 沿用既有双守卫(代际号+对象身份),防止旧激活的迟到 teardown 误清新槽位。

5. 是否有回归问题:审过的既有行为及结论——

  • 正常单安装路由(无热替换):槽位绑定一次,每连接读回同一 state 对象,与修前字节级等价(既有 19 个 WS 测试全绿);
  • 非模型 WS / 已 /bili/ 路由 URL / 控制面路径:claim 谓词未动,直连透传逻辑未动;
  • 延迟壳(origin 未 settle 窗口):正常时序下捕获同一 state,行为不变;
  • 第三方构造器 adopt/拒绝逻辑:未触碰;
  • fetch 通道:纯等价重构(内联 bind 抽成函数,读取顺序与语义一致),62/62 含 [BUG] DSH 插件热替换后 fetch 拦截仍绑在旧模块实例上 → 模型请求全部直连、压缩静默失效直到重启 #3394 全部回归测试;
  • dsh 宿主(liveStateSlotEffect 唯一消费者):dsh 不装 WS 拦截器,SEQ 递增次数不变 → 零影响;pi 宿主不注册 dispose effect,多出的递增惰性无害;
  • 两个有意 delta(PR 正文已披露,我独立确认成立):a) 热替换后 WS 跟随最新实例(本修复本体);b) 空槽位时模型形 WS 连接从「用安装期 state」变「直接透传」(对齐 fetch 空槽语义,仅 dispose 后可达)。无其他语义漂移;无 fail-fast 变吞错、无阈值/时序/输出格式变化。

6. 是否破坏缓存:不涉及——客户端路由层改动,代理侧缓存/重试/游标/去重键零触碰;URL 改写规则(线格式)不变。

7. 是否改了设计方向:没有——把 #3394/#3395 既有的 active-state 槽机制从 1 个传输扩到 2 个;单槽原子性(热替换时两传输整体切换)是该机制的自然推广,非转向。

8. 是否引入新配置:无。无新 env/config/CLI 面(config-surface-guard CI 绿)。

测试有效性:三个移植的回归测试断言有牙——热替换 rebind 测试断言精确改写 URL + dispatch 证据落到新实例(pre-fix 闭包绑旧实例必挂);teardown→direct 测试断言空槽直连(pre-fix 必挂);late-dispose no-op 测试钉双守卫语义(pre-fix 也过,属守卫钉住而非 bug 复现——PR 正文「三者均 pre-fix 失败」此处略过,不影响实质)。均走真实 globalThis.WebSocket 包装路径,FakeWs 桩构造,无网络无端口,确定性成立。

本地定向验证:WS 22/22 + fetch 62/62 = 84/84 绿(node 22,Linux);typecheck 干净;kernel/pi-subagents 构建通过。全量判定按纪律交给 CI。

CI 状态与处置:e4f4675ee 上静态门禁全绿、e2e 各道全绿/按设计 skip;test 分片 ubuntu 22×3、ubuntu 24、windows 22-2 全绿,仅 windows-22-1 红。下钻该 job 日志(4192 测试仅 1 败):失败的是 tests/persist-guard-ownership.test.ts:243(#3406 持久化守卫「foreign write between DEBOUNCED saves」,断言 full-read 计数 1 !== 2),该用例耗时 10489ms 而同文件兄弟用例 300–978ms、整分片跑满 660s——高负载 runner 上事件循环饥饿的典型签名(debounce 写未及时落地,计数没涨)。该文件属持久化层,与本 PR diff(客户端拦截三文件)无任何导入关系,不存在本 PR 能影响它的机制路径。判定:环境性 flake,与本 PR 无关。我已对该 run 触发 rerun-failed-jobs(HTTP 201),等重跑结果;若复现将单独排查那个持久化测试,不混入本 PR。

判定:可合(待 windows flake 分片重跑转绿确认;该失败与本 PR 无关,如再次复现按独立问题处理)。

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 (ai: vllm-qwen/qwen3.8-27b)

CI 重跑确认:rerun-failed-jobs 后 test (windows-latest, 22, 1) 转绿(117413455199 success)——坐实上一条的判定:该失败是 runner 高负载下 persist-guard-ownership(#3406)时序敏感用例的事件循环饥饿 flake(同用例 pre-fix/post-fix 代码完全相同,diff 未触碰持久化层),与本 PR 无关。

至此 e4f4675 上全部检查通过:静态门禁 11 项绿、test 分片 ubuntu 22×3 / ubuntu 24 / windows 22×2 全绿、e2e 各道按设计全绿/skip。

最终判定维持:可合。 八问结论见上一条回复。合并动作按规矩留给你:#3580

@ranxianglei
ranxianglei merged commit c33ab30 into master Oct 17, 2026
61 of 62 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

📦 Merged Build Published

This merge to master was published as 0.0.542-master.c33ab30 (rolling master tag).

npm install -g billion-context@0.0.542-master.c33ab30  # 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

Development

Successfully merging this pull request may close these issues.

[plugin] WS 拦截还是「装一次绑死第一个实例」的老模式 —— #3394 修了 fetch,WebSocket 这半边没修

1 participant