Skip to content

[PR] feat(#1926): WS bridge idle-socket observability + WS test hardening (post-#1968 follow-up) - #2004

Closed
ranxianglei wants to merge 8 commits into
masterfrom
2026-10-03_ws-observability-1926
Closed

ranxianglei wants to merge 8 commits into
masterfrom
2026-10-03_ws-observability-1926

Conversation

@ranxianglei

@ranxianglei ranxianglei commented Oct 3, 2026 •

Copy link
Copy Markdown
Owner

Follow-up to the merged #1968 — it carries the two commits that landed on the branch after the merge point (the merge took 163cfc54; these rode later pushes). Rebased onto current master (33d58e5e) during review; see the stale-base note under item 3.

  1. feat(Responses WS lane: idle client sockets pin live upstream connections and checkpoints with no reclamation #1926): WS bridge observability — option (c) from the issue's suggested sequence

    • GET /__bili/stats gains wsBridge: per live connection — codec, connection id, idleMs, retainedBytes (client-leg + upstream-leg checkpoints, from ResponsesWsHistory.bytes()), inFlight.
    • One-shot idle-retention warn per idle episode (60s scan, unref'd, default threshold 30min, BILI_WS_IDLE_WARN_SECONDS overrides, 0 disables), tagged (#1926), reset by client activity. Pure observability — nothing is closed.
    • Shell-level: any codec gets both surfaces via the optional WsBridgeSession.stats() hook; the Responses session implements it.
  2. test(OpenCode V2.0.20 OpenAI default WebSocket bypasses bili native HTTP interception; only title request creates empty ACP session #1844): poll the side-request log line in the title-persona WS test — the seventh site of the log-race family that flaked the windows-24 CI leg (completed frame arrives before the side request (agent=title) line flushes on slow runners); migrated to readLogUntil like the six earlier sites.

  3. test: satisfy the widened typecheck — the typecheck script moved to tsconfig.json (src+tests) after feat: ACP-aware OpenCode V2 Responses WebSocket proxy + generic WS bridge shell (#1844/#1467) #1968 merged, which exposed latent type errors in the WS test files, and one import in fix(preflight): log+dump rejected summary bodies and shrink-retry hard 400/413 #1994's new preflight test (ProxyOptions lives in src/config.ts, not re-exported by server.ts). Stale-base note: this PR branched at 129200db; fix: preflight transport resilience + shipped-payload baseline anchor (#1987) #1989 then merged over the now-widened gate carrying its own red test file (tests/issue1987-preflight-baseline-anchor.test.ts), so master's npm run typecheck was red before this PR landed. The rebased head fixes all four affected files; master's typecheck is green again at this head.

Review additions (pushed to this branch during review)

  • fix(ws-bridge): non-numeric or negative BILI_WS_IDLE_WARN_SECONDS now falls back to the 30-min default instead of silently disabling the warn scan (garbage → NaN → disabled under the original parse). Behavior change for degenerate input only: invalid value previously = scan off, now = default on — matches the sibling-knob convention (BILI_MODEL_INFO_RETRY_MS, BILI_UPDATE_CHECK_INTERVAL_MS).
  • docs: BILI_WS_IDLE_WARN_SECONDS mirrored into the CONFIGURATION.md env-var tables (en/zh); it was previously documented only in docs/opencode-responses-websocket.md.

Config surface

New env var BILI_WS_IDLE_WARN_SECONDS (no config-file field):

Verification (final head 55d43e80, rebased on 33d58e5e)

  • npm run typecheck (tsconfig.json, src+tests) clean — including tests/issue1987-preflight-baseline-anchor.test.ts from fix: preflight transport resilience + shipped-payload baseline anchor (#1987) #1989
  • Targeted suites 60/60 pass: tests/ws-bridge.test.ts 4/4 (incl. injected-clock scanner test: threshold crossing, exactly-once per episode, reset on activity, stats lifecycle), tests/codex-responses-ws.test.ts 5/5, tests/responses-websocket.test.ts 36/36, tests/issue1993-preflight-400.test.ts 1/1 (import fix only), plus tests/issue1987-preflight-baseline-anchor.test.ts + tests/preflight-transport-recovery.test.ts 10/10; full regression left to CI

Refs #1926 (option (c) done; (a)/(b) idle-close pending verified host recovery — issue stays open), #1968, #1844.

@github-actions

github-actions Bot commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-10-03_ws-observability-1926 (7fafcbd)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.2046-pr.2004.2a8d71b

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

Install instructions are refreshed on each push.

…one-shot idle-retention warns

GET /__bili/stats gains wsBridge: per live connection (codec, connection id,
idleMs, retainedBytes across both checkpoints, inFlight). A 60s scan logs one
warn per idle episode once idle age crosses 30min (BILI_WS_IDLE_WARN_SECONDS
overrides, 0 disables), reset by client activity — option (c) from #1926, zero
contract risk; idle-close (a)/(b) stays pending verified host recovery.
Seventh site of the log-race family (windows-24 CI leg): the completed frame
can arrive before the side-request line is flushed on slow runners. Switch the
last bare readFileSync+match to readLogUntil, same as the six migrated sites.
- codex-responses-ws / ws-bridge: explicit Item casts, typed dispatch promise
- issue1993 + issue1987 (master's own new tests): import ProxyOptions from
  config.ts (server.ts only re-imports the type); AddressInfo cast for
  .address().port
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 Review 完成。结论先行:可合并(等 55d43e80 的 CI 转绿即可)。方向正确、无回归、diff 干净;review 中发现的两个小问题已按「小问题直接修」直接推上本分支(见下),PR 描述已补上缺失的 Config surface 章节并更新 Verification。

① 是什么问题:Responses WS 通道没有任何回收机制——空闲的客户端 socket 会一直钉住一条 live 上游连接 + 两个 checkpoint,直到客户端自己关闭(#1926 观测:38 条无 close 连接,其中 30 条 30min+ 无上游请求)。本 PR 实现 issue 建议顺序里的 option (c):纯可观测性,不关闭任何东西,(a)/(b) 保留给验证过宿主恢复之后再做。

② 什么原因导致的:桥接 shell 只在 peer close / server shutdown 时回收,没有定时器或 idle 扫描;真正的风险面是上游 WS 连接槽位耗尽(bili 已有 websocket_connection_limit_reached 轮换逻辑)。而 idle-close 的正确性依赖宿主对 previous_response_not_found 的恢复行为——尚未 E2E 验证(anomalyco/opencode 侧),所以先做零契约风险的 (c) 是正确分层,不是绕开根因:(a)/(b) 仍挂在 #1926 里等验证。

③ 怎么修复的:shell 层加可选 WsBridgeSession.stats() hook(任意 codec 可实现),Responses session 返回 {retainedBytes, inFlight};retainedBytes 与 commit() 的 MAX_REQUEST_BYTES 计量完全同源(client-leg + upstream-leg 的 JSON 字节和)。60s unref'd 扫描器对每条连接在每个空闲 episode 打一次 warn(默认阈值 30min,客户端活动重置,tagged (#1926));GET /__bili/stats 增加加性顶层键 wsBridge。附带:title-persona WS 测试迁移到 readLogUntil(#1844 log-race 家族第 7 处),以及 widened typecheck 的补齐。

④ 是否有回归问题:触碰路径的既有行为逐项清点——

  • /__bili/stats 输出格式:纯加性新增 wsBridge 键,既有字段不动 ✓
  • upgrade 准入语义:不变(仅返回值扩展为 fn-with-stats,唯一调用点 server.ts 同步更新)✓
  • server.close / 'close' 路径:新增幂等 scanner?.close(),不改变原有关闭时序 ✓
  • wire/message shape:零改动(纯诊断旁路),plugin/proxy 两种压缩模式均推理过,都不受影响 ✓
  • 高危模式逐项:fal-fast 变吞错(无)、阈值/时序漂移(扫描器 unref'd,不拖进程退出)、输出格式变化(仅加键)、缓存/重试策略静默失效(无)— 均未命中
    非阻塞观察(未改代码):扫描器不看 inFlight,单次请求跑超 30min 可能被标 idle warn(定义站得住:idle=无客户端活动,"钉住上游连接"陈述依然为真);wsBridgeStats 是模块级全局(后装者胜),生产单进程单 server,与既有模块级统计(blindTunnels 等)一致,可接受。

⑤ 是否破坏缓存:不涉及——prefix cache、重试、增量游标、去重键全部未触碰。

⑥ 是否改了设计方向:没有——纯加性可观测 hook + stats 键,架构/边界/数据流不变;idle-close (a)/(b) 继续等宿主恢复的 E2E 验证,#1926 保持 open。

⑦ 是否引入新配置:是——新增 env var BILI_WS_IDLE_WARN_SECONDS(默认 1800 秒;0 禁用 warn 扫描但 stats 仍在;非数字/负值回退 1800;扫描周期固定 60s)。为何现有 surface 表达不了、考虑过哪些机制(providers 表 / compress 三级 / launcher↔extension 均不适用,WS lane 无 config-file block,HTTP-lane 同类旋钮都关 socket 而这个只 warn)、兼容/迁移说明——已按 Configuration Surface Discipline 补进 PR 描述的 Config surface 章节;CONFIGURATION.md en/zh 镜像行也已补。

Review 中直接修掉的小问题(两个 commit 已推上本分支):

  1. fix(ws-bridge)(483e8d5b):原解析 Number(env ?? 1800) * 1000 在垃圾输入下 → NaN → 静默禁用整个 warn 扫描。改为非有限/负值回退默认 30min,与兄弟旋钮约定一致(BILI_MODEL_INFO_RETRY_MS、BILI_UPDATE_CHECK_INTERVAL_MS)。行为变更披露:无效值从"扫描禁用"变为"回退默认启用"(仅影响退化输入;显式 0 仍是禁用)。
  2. docs(55d43e80):BILI_WS_IDLE_WARN_SECONDS 镜像进 CONFIGURATION.md en/zh(此前只在 docs/opencode-responses-websocket.md 有)。

Stale-base 与协调事项(需 owner 拍板):

验证证据(final head 55d43e80,本地定向运行):typecheck clean(src+tests);ws-bridge 4/4(injected-clock 扫描器测试覆盖阈值穿越 / exactly-once / 活动重置 / stats 生命周期)、codex-responses-ws 5/5、responses-websocket 36/36、issue1993-preflight-400 1/1、issue1987 + preflight-transport-recovery 10/10,共 60/60;全量回归交给 CI。

判定:可合并(human 合并)。合并后 #1926 保持 open,等 (a)/(b) 的宿主恢复验证。

…one-shot idle-retention warns

GET /__bili/stats gains wsBridge: per live connection (codec, connection id,
idleMs, retainedBytes across both checkpoints, inFlight). A 60s scan logs one
warn per idle episode once idle age crosses 30min (BILI_WS_IDLE_WARN_SECONDS
overrides, 0 disables), reset by client activity — option (c) from #1926, zero
contract risk; idle-close (a)/(b) stays pending verified host recovery.
# Conflicts:
#	tests/codex-responses-ws.test.ts
#	tests/issue1987-preflight-baseline-anchor.test.ts
#	tests/ws-bridge.test.ts
@ranxianglei

Copy link
Copy Markdown
Owner Author

(ai: zhipuai-lb/glm-5.3)

孤儿票清理:标题指向的 issue 已关闭,修复已由其他路径落地。关单;误判请 reopen。

@ranxianglei ranxianglei changed the title feat(#1926): WS bridge idle-socket observability + WS test hardening (post-#1968 follow-up) [PR] feat(#1926): WS bridge idle-socket observability + WS test hardening (post-#1968 follow-up) Oct 24, 2026
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