Skip to content

feat: ACP-aware OpenCode V2 Responses WebSocket proxy - #1859

Closed
darkness-A wants to merge 3 commits into
ranxianglei:masterfrom
darkness-A:2026-10-01_opencode-responses-websocket
Closed

darkness-A wants to merge 3 commits into
ranxianglei:masterfrom
darkness-A:2026-10-01_opencode-responses-websocket

Conversation

@darkness-A

@darkness-A darkness-A commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

Outcome

Fixes #1844. Fixes #1866.

Implements OpenCode V2 <-> WebSocket <-> bili ACP <-> WebSocket <-> OpenAI Responses, including the ChatGPT OAuth/Codex endpoint. Both model network legs use WS while the existing ACP pipeline still processes the conversation.

The user authorized this integration on 2026-10-01 and subsequently authorized the sparse-terminal follow-up after local OAuth acceptance found repeated tool calls. This updates the existing contribution rather than replacing it.

Implementation

  • Route supported OpenAI Responses handshakes through the existing explicit-protocol tunnel, stamping native session/model metadata. Admit only loopback cooperative OpenCode clients and reuse destination guards; unrelated upgrades retain immediate 426.
  • Reuse ACP through an in-process HTTP-shaped envelope, with an async-scoped upstream WS transport. There is no network HTTP bridge for model requests.
  • Keep independent client-visible and processed-upstream continuation checkpoints. Expand incoming deltas before ACP; send upstream deltas only when processed history extends its checkpoint exactly. Folds/history edits reset the response chain with full processed input.
  • Preserve tool argument strings, opaque reasoning, usage accounting, error status, cancellation, reconnect, proxy egress, verified TLS, bounded buffering, and shutdown.
  • Bundle pinned ws as a build-time dependency. Add WS wire contract WC-013 and isolated real OpenCode V2.0.20 WS CI coverage; retain the existing V2.0.3 Chat Completions job.

Follow-up: sparse terminal output (#1866)

The original PR saved only response.completed.response.output. A response may already have delivered a completed tool call in response.output_item.done while terminal output is empty, omitted, or partial. Losing that call orphaned the next tool result during ACP processing and could cause repeated tool execution.

Both checkpoint boundaries now accumulate completed output items by output_index and merge terminal items by ID. Internal non-streaming responses receive the complete assembled output; streaming frames and tool arguments stay unchanged. Only completed responses establish checkpoints. Diagnostics report restored item counts without payload contents.

Regression tests cover full, empty, omitted, partial-prefix, and partial-suffix terminal output, indexed ordering/opaque reasoning, actual folds, and three successive tool-result continuations. The real OpenCode E2E asserts exactly one post-fold status call, its matching result, retained summary, removed bulky history, and no primary-request HTTP fallback.

Failure-combination hardening

The user requested abnormal-combination coverage and explicitly selected minimal diagnostics. This follow-up adds eight cases (34 dedicated WS tests in total) and extends the sparse-output real-fold case with a sequence of missing checkpoints, idle disconnects, and connection-limit rotation.

  • A tool delivered before disconnect, failed completion, or incomplete completion does not create a successful checkpoint or trigger silent automatic replay.
  • Cancellation followed immediately by a fresh socket with rotated synthetic credentials requires full history; subsequent continuation works and abort timers are released.
  • An overlapping create is rejected without corrupting the active response/checkpoint.
  • Repeated continuation and connection-limit errors stop after one applicable transport recovery; the next explicit request can recover.
  • A rejected handshake can be followed by a successful connection.
  • Completed call/result pairs remain present exactly once, folded bulky history stays absent, and the summary survives combined recovery paths.

Minimal WS diagnostics include connection/session labels, failure phase, numeric close code, full/delta mode, and checkpoint-reset/recovery reasons. New diagnostic messages exclude credentials, URLs, raw close reasons, payloads, and upstream error bodies; assertions check that exclusion. Normal lifecycle details are debug, transport failures are warn. Existing HTTP error logging is outside this new diagnostic surface.

No new production defect was established by these cases. This is regression coverage and observability, not an exactly-once guarantee for arbitrary host-tool side effects or validation of the real OAuth refresh lifecycle.

Config surface

No new or renamed runtime config fields, BILI_* variables, CLI flags, or config semantics. Existing activation, routing, compression hierarchy, and tunnel mechanisms express this feature. ACP_TEST_E2E_OC_WS is a test-only gate following the existing convention. The implementation does not rewrite the user's transport choice.

No manual package-version bump, acp-kernel pin change, or persistence-format change. Version 0.1.179 is inherited from current master; socket checkpoints remain connection-local.

Final verification

Rebased the existing feature and sparse-terminal fix onto master d4746c4ecd211adc5ff12fc0079d138337b50933. git range-diff reports both earlier patches unchanged; the hardening is an additive follow-up commit on the same PR.

  • Typecheck: PASS.
  • Build, including dist-import-annotations: PASS.
  • git diff --check: PASS.
  • Dedicated WS regression coverage: 34/34 PASS, included in the full suite.
  • Real OpenCode V2.0.20 with isolated loopback WS upstream: 2/2 PASS (full/sparse terminal, real ACP fold and post-fold result).
  • Real Codex with deterministic fake Responses upstream: 3/3 PASS (overflow fold, under-window control, hostile .env routing).
  • Clean-environment full Windows suite: 3625 total, 3600 PASS, 10 FAIL, 15 skipped.
  • Fresh master control at d4746c4e, same environment: 3590 total, 3565 PASS, 10 FAIL, 15 skipped. All ten failing test locations match exactly: two in tests/codex-overlay-env.test.ts, six in tests/launcher.test.ts, one in tests/update-dsh-self-refresh.test.ts, and one in tests/update-pi-self-refresh.test.ts. These are existing Windows symlink/platform failures; the full local suite is not green. No failing assertions were weakened or skipped.
  • The previously unavailable real-upstream Codex lifecycle gate at 127.0.0.1:8199/v1 remains unverified locally. The earlier user-run live ChatGPT OAuth acceptance below remains the live evidence, distinct from these isolated fake-upstream regressions.

The new logs take effect when the local proxy next starts from the rebuilt artifact. The user's live services were not restarted during this work.

User-run live ChatGPT OAuth acceptance

After installing the fixed local build, the user reported that repeated calls stopped. Their log records a real plugin compression with upstream input 335615 -> 172429, then continuing WS requests at 174342 and 174855 input tokens. Restoration diagnostics recovered 1, 5, 2, and 4 streamed output items absent from terminal output. This confirms the sparse-terminal fix was exercised on the live OAuth route; it is bounded acceptance, not a guarantee for every upstream shape or attribution of every historical repetition.

Review boundaries

  • Initial scope is local cooperative OpenCode OpenAI Responses, not generic Realtime or transparent WS relay.
  • Existing HTTP proxy-mode carrier/loop behavior remains covered by wire-contract and real Codex tests; admitted WS uses plugin-mode tool ownership.
  • view=ws-expanded sizes describe reconstructed processing envelopes, not incremental wire bytes. Socket reuse does not guarantee incremental input after every history edit.
  • Existing compression cadence/70% emergency escalation is unchanged by this fix.
  • Related feat: support wss:// (WebSocket) traffic — opt-in transparent passthrough now, protocol-aware compression later #1467/[PR] feat: opt-in transparent WebSocket passthrough (#1467) #1472 is a separate transparent WS proposal touching the upgrade decision point; coordinate integration deliberately.
  • Transport/security-sensitive changes require human review. Keep draft pending updated CI/review and the unavailable real-upstream gate. No merge or publish performed.

@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 收到。开始按仓库 PR Review 规范审这个 PR(外部贡献者 PR — 只审不代改)。当前步骤:读原 issue #1844 核对方向、拉分支核对代码与 diff、看 CI 状态,然后给出结构化七问结论。稍后回复完整审查结果。

@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 Review complete — PR #1859(外部贡献者 PR,按仓库规范仅做 review + 验证,未向作者分支推送任何提交)

查重 / 方向关:本 PR 是 #1844(OpenCode v2.0.20 默认 WS transport 导致主流量绕过 ACP)的实现,非重复立项;与 #1467/#1472(透明非压缩 WS relay)机制不同——那是"relay",这是"ACP-aware processing",单独立项合理。方向经 owner 在 #1844 comment 3 明确批准(WS-native + 边界锁定),实现与批准边界逐项一致。PR 基于当前 master(bcef839)单 commit、无 stale base;无 ework-agent-pr 标记 → 外部贡献,保持 draft、人工合并。

验证证据(本地定向验证 + CI):

  • 本地(worktree @ 45ff200):typecheck PASS;定向测试 63/63 PASS(responses-websocket 18 例 + wire-contract + opencode-v2 + opencode-native);build PASS;dist 自包含扫描通过——dist/index.js 仅剩 node builtin 导入,ws@8.22.0(devDependency 精确钉版)经 tsup noExternal 内联,dist 零运行时依赖,符合 Key Design Decision fix: keep system prompt byte-stable for prefix cache #1。
  • CI:sha 45ff200 上 14 项 check 全绿,含新增 real OpenCode V2.0.20 WS job(主请求全程留 WS、真实 compress fold、usage accounting)、real codex fake-upstream lane、hermetic registry e2e、ubuntu/windows × 4 平台 test matrix。作者本地缺口(real-upstream Codex lifecycle gate、Windows 本地 9 fail)由 CI green 覆盖,无需本地复跑(本地禁跑全量回归)。

测试有效性(④):断言有牙。零 HTTP fallback(httpRequests===0)、精确 wire shape(逐行核对 previous_response_id)、tool arguments 字节级一致(含字面 <acp> tag payload——#1039 不变式在新 transport 上的回归保护)、timer 泄漏检查(_liveUpstreamTimersForTest()===0)、跨会话 checkpoint 隔离、cache miss 恰好一次 full-input 重试、connection limit 恰好一次轮换、匿名 upgrade 保留 426。E2E 驱动真实 OpenCode V2 二进制(hermetic env),关键断言:tests/e2e/e2e-opencode-responses-ws.test.ts:130 所有 HTTP 请求必须为 title side-channel(x-bili-plugin-agent==="title")——直接钉住 #1844 的回归面(主流量不得回落 HTTP)。非恒真、非 mock-only。

结构化七问:

  1. 是什么问题:OpenCode v2.0.20 把 OpenAI provider 默认 transport 切到 websocket(ChatGPT OAuth 走 chatgpt.com/backend-api/codex),而 bili V2 插件只 hook 了 http.request → 主模型流量完全绕过 ACP,只有 HTTP title 请求建了空 session,压缩/status/usage 全部失效(OpenCode V2.0.20 OpenAI default WebSocket bypasses bili native HTTP interception; only title request creates empty ACP session #1844)。
  2. 什么原因导致的:上游 feat(plugin): add experimental WebSocket handshake hook anomalyco/opencode#49131 移除 force-HTTP gate,WS executor 与 HTTP hook 并存;缺口在 bili 缺 WS 拦截层这一 transport hook 层面。"强制回 HTTP"是治标(堵),WS-native 处理才是根修(疏)——owner 已拍板。
  3. 怎么修复的:双端 WS native processing,既非透明转发也非强制 HTTP。客户端:V2 插件注册 experimental.ws.handshake,复用既有 httpRequestHook 的路由/stamping,把 socket 改写进 /bili/responses/<upstream> 隧道。服务端:只放行 loopback + 协作身份(x-bili-plugin: opencode + conversation header)的升级,其余 upgrade 照旧 426;目的地复用既有 tunnel-guard 检查([SECURITY] /bili/<绝对URL> 零配置隧道无目标准入:可回环访问自家管理面、触达 link-local 元数据;0.0.0.0 部署下 LAN 可经 PUT 劫持全部流量凭据(架空自述的 loopback-only 防线) #409 语义);每个 response.create 帧包成进程内 HTTP-shaped envelope(无内部网络桥)走完整 ACP pipeline,管线本身零改动;上游腿用 AsyncLocalStorage 作用域的 undici WebSocket transport(尊重既有 proxy egress、TLS 校验、cancel/reconnect/lifetime rotation)。checkpoint 设计:客户端 delta→full 展开;上游仅在处理后历史精确前缀扩展上一 response 时发增量;fold/历史编辑后自动改发不带 previous_response_id 的全量输入(同 socket 新链);上游 cache miss 全量重试一次;连接上限轮换一次。
  4. 是否有回归问题:逐项审了触碰路径的既有行为——HTTP 主管线(dispatch 改名语义逐字节不变)、fetchWithTimeout(无 transport 作用域时回落 global fetch,既有路径不变)、resolveUpstream(未改,/bili/responses/ 复用 master 既有 explicit-protocol 前缀解析)、426 契约(未认领 upgrade 保留,有专门回归测试)、wire-contract ledger(WC-013 只增不删,无 golden snapshot 变更)、既有 V2.0.3 Chat Completions CI lane(未动)、session/persistence/kernel pin(未触碰)、config surface(零新增字段)。高危模式(fail-fast 变吞错、阈值/时序漂移、输出格式漂移)逐项过:错误显式呈现(426/403/409/413/error frame 语义完整),日志格式仅为新流量类新增 forward WS 变体。结论:无回归。
  5. 是否破坏缓存:否。上游 prompt cache 反而被保护——增量轮次发 exact-prefix delta,保 prefix affinity;fold/历史编辑主动切全量新链(压缩语义固有,非缓存破坏)。客户端 checkpoint 连接局部,断开即清,跨会话隔离有专门测试,无跨会话陈旧状态复用。
  6. 是否改了设计方向:无转向。实现的是 owner 在 OpenCode V2.0.20 OpenAI default WebSocket bypasses bili native HTTP interception; only title request creates empty ACP session #1844 批准的 WS-native 方向,边界锁定(进程内 envelope / async-scoped upstream WS / separate checkpoints / loopback 协作准入 / 无新配置)。两种压缩模式均已考虑:WS 路径结构上仅 plugin mode(准入要求 x-bili-plugin 头),proxy mode 客户端不受影响仍走原 HTTP 路径。范围边界明确写入文档:首发仅本地 native/launcher OpenCode OpenAI Responses;generic Realtime、远程 WS 客户端、多路复用 lane 不在范围内;旧 host 无 experimental hook 时回退 transport: "http"(CLIENTS.md 已记录)。
  7. 是否引入新配置:无。零新增 config field/env var/CLI flag(owner-gated config surface 未动);ACP_TEST_E2E_OC_WS / E2E_OC_BIN 是测试门控变量,沿用既有 ACP_TEST_* / E2E_*_BIN 约定。新依赖 ws@8.22.0 为 devDependency 精确钉版 + 构建期内联,运行时依赖不变。

小问题(均 non-blocking,外部贡献者分支不便代改,列出供参考):

  • src/server.ts upgrade listener 上方两段注释碎片合并得略生硬(纯 cosmetic)。
  • 协议边界说明:若客户端违反协议在带 previous_response_id 的同时发全量 input,expand() 会重复历史——OpenCode 遵循协议(delta+id 或 full-without-id),属协议假设而非缺陷;设计文档已注明 ref tagging 等历史编辑会强制全量输入。
  • 新增 CI job 随 workflow 级 pull_request trigger 在每个 PR 上跑——与仓库其他 e2e lane 一致,CI 分钟数小幅增加,可接受。

判定:审查通过,达到合并标准(代码质量、测试质量、回归审计、CI 全绿、owner 批准的方向与边界全部落实)。建议 un-draft 后由人工合并。

@darkness-A

Copy link
Copy Markdown
Contributor Author

The sparse-terminal follow-up for #1866 is now included in the existing PR #1859, rebased onto master 78e72f0d.

Fix commit: a83a7f1d531bb38ee04adff606d349f95f24b27a.

The original implementation dropped completed streamed tool calls when terminal output was sparse. Both client/upstream checkpoints now reconstruct indexed output independently; streaming frames and tool arguments remain unchanged. Coverage includes empty/omitted/partial-prefix/partial-suffix output, real ACP folds, and successive tool-result pairing.

Final checks: typecheck/build/diff-check PASS; 26 dedicated WS tests PASS; real OpenCode V2.0.20 local WS E2E 2/2 PASS; real Codex fake-upstream E2E 3/3 PASS. Full Windows suite is 3548/3572 passing with 9 failures and 15 skips; a fresh master control has the exact same nine failing test locations (3521/3545 passing). No new failing tests relative to master. The real-upstream Codex lifecycle remains blocked locally because port 8199 is unavailable.

User-run ChatGPT OAuth validation exercised checkpoint restoration and continued after an actual fold (335615 -> 172429 input tokens); the user reports the tool-repeat loop stopped. A later observation of frequent compression correlates with the pre-existing 70% emergency nudge, not frozen tool-result history; this patch does not change compression policy.

The PR body contains updated evidence and limitations. The existing PR remains draft/open for updated CI and human review; it has not been merged or published.

@darkness-A
darkness-A force-pushed the 2026-10-01_opencode-responses-websocket branch from a83a7f1 to 1e282e9 Compare October 1, 2026 15:57
@darkness-A

Copy link
Copy Markdown
Contributor Author

Follow-up hardening pushed in 1e282e9: eight abnormal-combination cases plus scoped WS diagnostics (failure phase, numeric close code, checkpoint reset/recovery reason; no credential/payload/URL logging in the new diagnostics). The sparse-output fold regression now chains checkpoint loss, idle disconnect, and connection-limit rotation.

Reviewed/rebased on master d4746c4; range-diff confirms the two prior patches are unchanged. Typecheck/build/diff-check pass; WS 34/34, real OpenCode fake-WS E2E 2/2, and real Codex fake-upstream E2E 3/3 pass. Full Windows suite: 3625 total / 3600 pass / 10 fail / 15 skipped; fresh master control: 3590 / 3565 / the same 10 failing locations / 15. Full suite is not green; baseline failures are documented in the updated PR body.

No new production defect found in this hardening pass. Existing real-upstream gate still needs maintainer/CI infrastructure. PR remains draft for human review; no live service restart, merge, or publish.

@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 第二轮审查完成(head 1e282e91,#1866 sparse-terminal 跟进)

按仓库 PR Review 规范对新增的两个 commit 做了完整审查(feat 层已验证与首轮批准的 45ff2002 逐文件一致,审查范围 = 46ee9929 fix + 1e282e91 test/diagnostics)。本地定向验证 + CI 结果如下。

验证证据

  • rebase 核实:pr-1859 恰为当前 master(d4746c4e)+3 commit,无 stale-base;feat 层 diff 与首轮批准版本逐文件、逐行数一致(22 files, +1256/−16)。
  • 本地(独立 worktree):typecheck PASS;定向套件 81/81 PASS(responses-websocket 参数化展开后 23 个顶层测试 + wire-contract + opencode-v2 + opencode-native);build PASS(dists-import-annotations OK);dist 自包含性复验——ws 已内联,bundle 中仅剩 node builtin,bufferutil/utf-8-validate 是 ws 标准的 try/catch 可选原生加速件,非硬依赖。
  • CI(1e282e91):14/14 全绿,含新增 OpenCode V2.0.20 WS ACP job、real codex fake-upstream、real opencode/pi/dsh native lanes、四平台全量矩阵(build-artifact、version-guard、hermetic registry e2e)。你本地 Windows 的 9 个失败在 CI 不复现,与「与 master 对照同 9 处」的说法一致。

结构化七问

  1. 是什么问题:首轮实现的 socket 检查点只提交终态 response.output;当该字段为空/缺失/部分时,流式已交付的 function_call 从历史中消失,下一轮 ACP 收到孤儿 tool result、配对校验丢弃,模型重发同一工具(fix: sparse Responses WS terminal output loses tool calls after ACP fold (#1859) #1866)。
  2. 什么原因导致的:检查点内容的生产者只信终态快照,而 OpenAI WS 协议下 item 的权威交付记录是流式 output_item.done,终态 output 只是(可能稀疏的)汇总——生产者选错了事实来源。
  3. 怎么修复的:新增 ResponsesWsOutput 逐 exchange 按 output_index 累积 output_item.done(末次覆盖、字节账本含替换调整、超 MAX_REQUEST_BYTES 抛错),response.completed 时以 item ID 合并终态覆盖流式项,重建输出同时提交到客户端侧与上游侧两个检查点边界;非流式路径给内部 pipeline 的重建 JSON 也走同一重建。疏而非堵:改的是检查点内容生产者,任何稀疏形状都收敛到同一重建历史;流式帧与 tool arguments 路径零改动(byte-exact 不变量保持)。
  4. 是否有回归问题:无破坏性回归,逐项过过:① 终态输出完整的常规会话——ID 匹配槽位保序,重建结果与旧行为逐字节一致,原有 18 个测试(含 tool args byte-exact、continuation、usage accounting)全部仍绿;② 一处有意的行为收紧(披露):非流式终态路径现在先 clear 再 commit——旧代码在非流式响应 failed/incomplete 时会保留上一个检查点可继续增量,新代码清空 → 下一轮 delta 得到显式 previous_response_not_found → 一次性 full resend(前缀缓存对共享前缀仍命中)。这是把「只有成功 completed 才推进检查点」的不变量统一到了非流式路径,fail-loud 优于陈旧复用,属有意变更;③ 诊断日志为纯新增行,既有日志行语义不变;④ 准入安全面(loopback+协作头+tunnel guard)、配置面、持久化格式、kernel pin、update.ts 两个新 commit 均未触碰(stat 范围核对);WC-013 账本未动。两种压缩模式:WS 路径结构性仅 plugin mode(准入要求 x-bili-plugin:opencode),proxy mode 不受影响——与首轮结论一致。
  5. 是否破坏缓存:上游 prefix cache 不受影响(正常续传仍是 exact-prefix delta;稀疏终态场景下旧行为是丢工具调用,新行为是正确历史+最多一次 full resend,前缀缓存照命中)。fix: sparse Responses WS terminal output loses tool calls after ACP fold (#1859) #1866 floor 4 记录的已知残留(混合 assistant 轮 function_call+message 触发 pipeline 重建插入 turn separator → 退化为 full resend,正确性无损、实测缓存 ~99%)已在测试中钉住并注释,属既有 Compression breaks Responses assistant history ordering, causing repeated invalid_assistant_history 400s #564 排序修复的交互,不阻塞本 PR;独立的 fold 应用异常已单列 上下文中总是首条用户消息被保留 #1869。
  6. 是否改了设计方向:没有。OpenCode V2.0.20 OpenAI default WebSocket bypasses bili native HTTP interception; only title request creates empty ACP session #1844 owner 签核的边界(进程内信封共享 ACP pipeline、双侧独立检查点、exact-prefix 增量+fold 重置、loopback 协作准入、零配置面)全部保持;本 PR 仅在该设计内补上检查点重建这一环。
  7. 是否引入了新的配置:无。无新 config/env/CLI flag/默认值;诊断走既有 logger 的 debug/warn 分级,且已核对隐私纪律(不含 auth 头、URL、close reason 文本、payload、上游错误消息;恢复计数只记数量不记内容)。

小问题(不阻塞,记录在案):1e282e91 以 test: 为标题但含 src/responses-ws.ts 的诊断增强(101 行)。按 commit convention 诊断部分更宜归入 fix commit 或单独 commit——但这需要重写贡献者的提交结构,按 #1765 教训不动你的历史,留作后续 release squash/整理时的参考即可。测试质量本身过硬:故障组合(交付后断连、failed/incomplete 终态、cancel+轮换凭据立即重连、重叠 create、重复 continuation/connection-limit 有界重试、握手拒绝后恢复)+ 稀疏终态参数化真实 fold(断言每个完成的 call/result 在上游恰好出现一次),是真断言不是恒真测试;#1866 floor 4 的独立实现交叉验证(21/21 + 44/44)进一步佐证。

判定:两个新 commit 审查通过。 feat 层(首轮)+ sparse-terminal 修复 + 诊断覆盖均达标,CI 14/14 绿,本地定向验证全绿,行为变更已披露。PR 达到合并标准,转 ready 与合并由你决定(我不代合)。#1866 的修复即在本 PR 内,合并后可关闭。

@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 确认:无新增变更,维持第二轮审查结论

刚重新 fetch 核对:分支 head 仍为我已完整审查的 1e282e91(merge-base = 当前 master d4746c4e,3 commits),range-diff 意义上的「两个 prior patch 未变」与我的记录一致——这条消息描述的内容(八个异常组合用例、scoped WS 诊断、sparse-output fold 链式回归)正是第二轮已批准 commit 1e282e91 的全部内容,没有新 push。因此不重复全套七问,直接给核实结果:

  • CI(刚重新拉取):1e282e91 上 14/14 全绿,含 OpenCode V2 Responses WebSocket ACP job、real codex/opencode/pi/dsh lanes、四平台全量矩阵、build-artifact、version-guard。
  • 本地独立验证(该 sha 上执行过,证据沿用):typecheck PASS;定向套件 81/81(responses-websocket 参数化展开 + wire-contract + opencode-v2 + opencode-native);build PASS,dist 自包含复验通过。你注释里的计数口径(WS 34/34)与我的运行(同文件参数化展开后 23 个顶层测试,四文件合计 81)差异来自展开计数方式,结论一致:相对 master 对照零新增失败。
  • real-upstream gate:维持已知限制——真实 Codex lifecycle 依赖可用上游基础设施,本地与 CI 均无;fake-upstream e2e(real codex 3/3)+ OpenCode V2.0.20 真二进制 WS E2E 已覆盖 pipeline 行为,PR body 已如实披露,不构成阻塞。

状态不变:审查通过、达到合并标准,转 ready / 合并由你决定(我不代合)。若后续有新 commit 推上来,我会按新 diff 走完整审查轮次。

@darkness-A

Copy link
Copy Markdown
Contributor Author

补充一次与当前 master 的兼容性验证(关联 #1844):我在独立分支上以 8ae9225a(0.1.180 / acp-kernel 0.0.100)为基线重放了本 PR 的三个提交,保留官方已合入的父子会话修复。

发现一处需要随合入处理的兼容点:stripLeakedBiliTools 将仅含 bili 工具的辅助请求转入 passthrough,该路径传给 transport 的 body 是 Buffer;原 WS transport 仅接受 string,因此报 Unsupported request in Responses WebSocket transport。补充修复让 WS 同时接受 string/Buffer,再统一解析 JSON。

同时调整主请求测试夹具,使其包含真实宿主 action tool;新增单独回归验证辅助请求的工具被剥离、仍走 WS、主会话 usage/refs 不被改变,并能继续正常主请求。补充提交:
darkness-A@323f37bb

验证结果:

  • WS + native binding 定向测试 44/44。
  • 真实 OpenCode V2.0.21 + 隔离本地 WS upstream:2/2(完整和稀疏 terminal output,压缩及后续工具链)。
  • 真实 Codex + 隔离 fake upstream:3/3。
  • typecheck / build / diff check 通过。
  • master 基线:3675 total / 3650 pass / 10 fail / 15 skip;集成版:3711 total / 3686 pass / 10 fail / 15 skip,10 处失败位置完全一致(Windows 基线问题)。不是全量全绿;本机真实 upstream 仍不可达,未声称完成真实 upstream 生命周期验收。

集成验证分支为 darkness-A:2026-10-02_local-ws-v180,用于本地维护和提供补充提交,没有改写本 PR 原分支或另开替代 PR。合入时请一并考虑这个 body 类型兼容点。

@darkness-A

darkness-A commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor Author

Reporter clarification: #1923 has been withdrawn and closed. These runtime observations came only from the unmerged WS build; no official-build/HTTP comparison has established an upstream defect. The notes below are observations for investigation of this WS patch's resource lifecycle, not a confirmed official bili bug.


Resource-lifecycle follow-up for the WS patch: investigating idle subagent rows also found long-idle client sockets/checkpoints retained. Current-session logs recorded 38 connections opened without a subsequent client-close record, 30 with no upstream request for >30 minutes; a contemporaneous OS snapshot had 41 established inbound connections to bili (that TCP count also includes ordinary HTTP, so it is not a per-session attribution). Only 1 actual request was inFlight. Sampled idle session request/usage counters did not increase.

installResponsesWebSocket keeps a per-peer ResponsesWsHistory plus ResponsesWsUpstream until the client closes or the server closes; there is currently no idle-age reclamation. Thus retained sockets/history can consume resources without generating model requests. This is not evidence of continued model billing or a measured memory leak; ownership/idle cleanup deserves review if the host keeps completed subagent sockets alive. Any future cleanup must respect the explicit retry-full contract for a lost previous_response_id, rather than silently discarding continuation context.

The misleading Web UI running badge is separately reported at #1923: it tests memory residency, not WS liveness or inFlight. These are distinct concerns.

@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 第四轮审查:master 兼容性(Buffer body)核实完成 + PR 状态变化提醒

① 缺陷核实(代码级证据,master 8ae9225a):确认存在,且范围比你报告的略宽。当前 master 的 side-passthrough 路径(src/server.ts:2577)对所有辅助请求都向 forward() 传 Buffer:demotedSide ? Buffer.from(JSON.stringify(parsed)) : bodyBuffer——即不仅 #1897 demoted 请求,persona 头与 max_tokens 启发式触发的辅助请求同样走 Buffer(forward 签名与 Prepared.body 契约本就是 string | Buffer)。而 WS transport 原守卫只接受 string → Unsupported request in Responses WebSocket transport。你只观察到 demoted 路径是因为真实会话里先撞上了它。

② 修复评审(commit 323f37bb):改在正确的层——transport 是 Prepared.body 契约的消费者,把输入边界放宽到完整契约(string|Buffer,统一一次 JSON.parse)而不是在 server.ts 侧 stringify(那只治一个生产者)。diff 最小、对非 string/非 Buffer 体仍严格拒绝;string 路径解析行为不变。新增回归测试有牙:demoted 辅助轮断言 httpRequests===0(仍走 WS)、上游收到 tools 已剥离而 side 预算保留、主会话 stats/refs 前后逐字节不变、主链恢复且不含辅助内容;fixture 加宿主 action tool 也正确(防 stripLeakedBiliTools 误伤主请求)。

③ 集成态本地验证(codeload 拉取你的集成分支 2026-10-02_local-ws-v180,0.1.180/kernel 0.0.100 + 修复):npm ci OK,typecheck PASS,定向套件 61/61(WS 全套含新回归 + master 自己的 fix-1897-leaked-tools-side 全套 + wire-contract WC-013)。与你给出的对照数据一致。

结构化七问(针对本增量):1) 问题:WS transport 输入契约比 pipeline 输出契约窄,辅助请求(Buffer body)在 WS 通道被拒。2) 原因:设计时假定 body 为 JSON.stringify 产物(主 pipeline 路径如此),未覆盖 side-passthrough 这一既有机制的 Buffer 产出(#388/#1897)。3) 修法:在消费边界放宽到完整契约,不动任何生产者。4) 回归:string 路径逐字节不变;主请求流不受影响;master #1897 自带套件在集成树全绿;两种压缩模式无交互(side passthrough 按定义不触 kernel 状态)。5) 缓存:不涉及(本 commit 不碰检查点/continuation 逻辑)。6) 设计方向:不涉及。7) 新配置:无。

⚠️ PR 状态变化(需要动作):master 已从 d4746c4e 推进 59 个提交至 8ae9225a(含 src/server.ts +197/−49 与 #1897 整套),PR head 仍是 1e282e91,GitHub 现报 mergeable=false / dirty(server.ts 文本冲突)。合并路径:请把你的集成分支内容(rebase 后的三个提交 + 323f37bb)更新到 PR 原分支——这是你的分支,按外部贡献者规则我不代推;push 后 CI 会在集成态上全量验证(含四平台矩阵),我随后按 range-diff 对新基线做增量复审(已批准的 feat 层 + 本修复 commit)。Windows 对照法(基线/集成同失败位点)方法学成立,CI 矩阵会再确认一遍。

@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 资源生命周期发现(idle socket/checkpoint 保留)已核实并立项

按你在线程里给出的数据对代码做了逐点核实(src/responses-ws.ts,集成 head):

  • 无 idle 回收确认:installResponsesWebSocket 的清理只发生在两个时机——客户端 peer close(transport.close("client-close") + history.clear() + transports.delete)与 server 关闭(override + close 事件)。模块内没有任何 timer/idle 超时/年龄扫描。你观察到的「38 开、0 关、30 个 >30min 无请求」与此结构完全对应:宿主(OpenCode V2)保留已完成的 subagent socket,bili 侧就永久保留到进程重启。
  • 每个 idle peer 的实际持有量:① loopback 客户端 TCP+WS 连接;② 一条存活的上游 WS 连接——上游腿是刻意的跨轮复用(终帧后 clean() 只摘监听器,socket 保持 OPEN,下次 connect() 同 key+OPEN 直接复用),所以 idle peer 不止占内存,还占上游侧连接槽;③ 双侧检查点(client-leg + upstream-leg history,各自上限 MAX_REQUEST_BYTES=100MB,常态 KB–MB)。单 peer 有界,非经典泄漏;无计费影响(与你计数器采样一致)。死掉的上游侧会自愈(下次 connect 见 readyState≠OPEN 即重连)。
  • 我认为更尖锐的风险是上游连接槽而非内存:bili 已有 websocket_connection_limit_reached 轮换逻辑,正是因为提供方限制每客户端 WS 连接数;N 个 idle peer 各占一槽,长驻宿主不断开旧 socket 时,新活跃会话可能提前撞连接上限。
  • 你的约束成立且是设计前提:任何回收必须尊重 lost previous_response_id 的显式 retry-full 契约。两个候选方案(idle 关闭客户端 socket / 仅清检查点保 socket)都依赖一个尚未验证的行为——OpenCode 在收到 previous_response_not_found 后是否会以 full input 重发。这是 anomalyco/opencode 侧行为,可用真二进制 E2E harness 验证后再定。

处置:已立 #1926(带来源标记,机制独立于本 PR):含完整数据、代码验证、三个候选方案(a 关客户端 socket / b 清检查点 / c 纯可观测)及建议次序——先做 c(暴露 live WS peer 数/idle 时长/保留字节,零契约风险,替代 OS 快照这种土法观测),再在 E2E 验证宿主恢复行为后做 a/b。不建议纳入本 PR:不同失败域,且本 PR 体量已大。#1923(UI running badge 测的是内存驻留而非 WS liveness/inFlight)你分得对,保持独立。

@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 两点说明:

① 本条与线程中 13:18 的兼容性验证是同一内容(重复转发),已在上方「第四轮审查」回复中完整处理,不重复展开:缺陷已按代码核实(src/server.ts:2577,且范围覆盖全部 side-request 类型而非仅 demoted)、修复 323f37bb 评审通过、集成树本地验证 typecheck PASS + 定向 61/61。当前唯一待办仍是该回复末尾所述:请把集成分支 2026-10-02_local-ws-v180 的内容(rebase 后的三个提交 + 323f37bb)更新到 PR 原分支——master 已推进至 8ae9225a,PR 现处于 mergeable=false/dirty;push 后 CI 全量验证,我按 range-diff 做增量复审。

② 收到你对 #1923 的撤回澄清:相关运行时观察仅来自未合入的 WS 构建,不构成对官方构建缺陷的认定。我已同步更新 #1926 正文:标注 #1923 已撤回关闭,并明确该 issue 的保留行为发现独立成立于对 WS lane 代码本身的验证(installResponsesWebSocket 无 idle 回收、上游腿跨轮复用),与官方构建无关——与你「这是本 WS patch 资源生命周期的调查观察」的定位一致。

@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 状态更新(来自对叠在其上的 #1936 的复审):master 已从 d4746c4e 推进到 9c608fc3(zcode MCP handshake #1892、fold-reconcile #1924、image e2e lane #1843、codex sqlite overlay #1918/#1919、ACP tag hardening #1882 等),本 PR 现报 mergeable=False,冲突预期集中在 src/server.ts(master 侧 +40/−3:import 区 ~51/~68 与 prepare*/injectSystem 区 3516–4771)。

请作者 rebase 时以 9c608fc3 为目标,并一并把集成分支 darkness-A:2026-10-02_local-ws-v180 的内容(三个提交 + buffered-auxiliary Buffer-body 修复 323f37bb)推上 PR 分支——floor 14/17 的请求仍然有效。#1936(WS lane observability,步骤 c of #1926)已独立复审通过(CI 16/16 全绿),其合并顺序依赖本 PR 先落地;若最终合入树不含 aux 修复,#1936 声明的堆叠基点会发生语义漂移,rebase 前需先确认。

@ranxianglei

Copy link
Copy Markdown
Owner

Follow-up refactor (discussion in #1467, one-step generalization): extracted the transport-agnostic half of this PR into a reusable WebSocket bridge shell, leaving the Responses-specific half as the first entry of a codec table.

src/ws-bridge.ts (new, protocol-independent):

  • admission: loopback source + x-bili-plugin lane marker + x-bili-plugin-conversation header + existing tunnel-destination guard
  • upgrade handshake, per-connection labeled logging ([codec] [conn=N] [session=…]), exactly-once shutdown teardown (server.close patched; close event kept as backstop)
  • driven by a readonly WsBridgeCodec[] table; unclaimed upgrades return false so the existing 426 path is untouched

src/responses-ws.ts: everything below installResponsesWebSocket moves into ResponsesWsSession implements WsBridgeSession, exported as responsesCodec (name responses-ws, marker opencode, URL claim for /bili/responses/<upstream>). Frame handling, previous_response_id history contract, upstream transport — all unchanged in behavior.

src/server.ts: single wiring site — installWebSocketBridge(server, dispatch, log, [responsesCodec]).

tests/ws-bridge.test.ts (new): drives the shell with a synthetic second codec (fake protocol on /bili/fake/<upstream>/echo) to pin the codec-table abstraction — second codec claims its own path+marker; foreign marker / missing conversation / unknown path fall through; server.close shuts sessions down exactly once and terminates peers.

Verification: tests/ws-bridge.test.ts 3/3, tests/responses-websocket.test.ts 34/34, adjacent Responses suites green, typecheck clean. Unknown protocols stay on #1472 transparent passthrough (folding requires knowing where history lives in the wire format, so "arbitrary WS compression" is out of scope by construction).

The branch is a pure fast-forward on top of this PR's head (1e282e91 + one refactor commit):

git remote add upstream https://github.com/ranxianglei/billion-context
git fetch upstream 2026-10-02_opencode-responses-ws-generic
git merge --ff-only FETCH_HEAD   # on this PR's branch

Happy to rework if you'd rather keep the PR single-protocol and land the shell separately — this shape just makes the second protocol (whatever it is) a codec file + one table entry.

@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 第五轮审查:ws-bridge 重构 — 行为保持已验证;一个基线谱系的关键发现

本轮范围:commit 85cf11db(分支 2026-10-02_opencode-responses-ws-generic,已在主仓),5 文件 +352/−109,纯 fast-forward 于 1e282e91 之上(compare API 核实:ahead_by=1, behind_by=0)。

① 行为等价性(重构审查核心) — 逐行比对旧 installResponsesWebSocket vs 新 shell+codec:

  • 准入语义字节等价:loopback 来源 + 非空 conversation + lane marker 精确匹配 + path regex;非 string conversation → "" → 拒绝,与旧代码 typeof !== "string" 检查等价;regex group 1 不可能为空,故 matchUpgrade?.[1] ≡ 旧 match[1]。安全门无任何放宽。
  • tunnel guard 调用参数、403/502 响应原始字节、日志行([responses-ws] upgrade failed)与旧实现完全一致。
  • 帧校验 / previous_response_id 历史契约 / 上游 transport / SSE sink 逐字移入 ResponsesWsSession;onClose ≡ 旧 peer-close handler(set 簿记移交 shell);shutdown() 为显式新增,其后 peer close 触发的重复清理是幂等的(transport.close/history.clear/destroy 均幂等)。
  • teardown exactly-once:close override 先 sessions.clear() 再拆除,close-event backstop 不会重跑——比旧实现(依赖 terminate→close 级联)更干净。
  • server.ts 单一接线点,426 fallback 路径未动。无 wire 变更 / 无配置面 / 无持久化 / 无 kernel pin / 无 update.ts。

② 本地验证(独立树 @85cf11db):typecheck PASS;定向 83/83(ws-bridge 3 + responses-websocket 34 + wire-contract + opencode-v2);build PASS,dist 自包含保持(ws 内联;bufferutil/utf-8-validate 仍为 ws 标准可选 addon)。CI on 85cf11db:审查时 4 个 matrix job 运行中(version-guard skipped)——注意它们验证的是 STALE base,见下。

③ ⚠️ 关键发现(阻塞合并):基线谱系。85cf11db 立在 1e282e91(base d4746c4e / v0.1.179)上,不是 v180 集成线(master 8ae9225a + Buffer fix 323f37bb)。按注释原样 ff 合并,PR 将同时丢失 master 的 59 个提交和第四轮认定的必需 Buffer body 修复。v180 分支谱系已核实:8ae9225a→a82f165b→8ded9894→4964465c→323f37bb。正确顺序:把 85cf11db replay/cherry-pick 到 323f37bb(v180 tip)→ 更新 PR 分支 → 全量 CI。refactor footprint 与 v180 变更的重叠面小(responses-ws.ts fetch guard 区 + server.ts 接线点),replay 风险低。

④ 两个小问题(补丁就绪;第一条已本地应用并验证通过):
a. tests/ws-bridge.test.ts test 2 隔离性弱:new net.Socket() 的 remoteAddress 为 undefined → isLoopbackAddress(undefined)===false(src/util.ts:55-57:!!addr && …)→ 三个负例全部在 loopback 门就被拒,从未到达 marker/path 逻辑——断言恒真但不钉住其声称的机制。修复(本地已跑 3/3 PASS):stub remoteAddress="127.0.0.1",并补一个期望 true 的正向对照:

function stubLoopbackSocket(): net.Socket {
    const socket = new net.Socket();
    Object.defineProperty(socket, "remoteAddress", { value: "127.0.0.1" });
    return socket;
}
// test 2 内:
const upgrade = (headers: Record<string, string>, duplex?: Duplex) => handler(
    Object.assign(new http.IncomingMessage(stubLoopbackSocket()), { url: "/bili/fake/http://127.0.0.1:9/echo", headers }),
    duplex ?? new Duplex(),
    Buffer.alloc(0),
);
assert.equal(upgrade({ "x-bili-plugin": "opencode", "x-bili-plugin-conversation": "ses" }), false); // wrong marker
assert.equal(upgrade({ "x-bili-plugin-conversation": "ses" }), false); // missing marker
assert.equal(upgrade({ "x-bili-plugin": "fakehost" }), false); // missing conversation
const claimed = new Duplex({ read: () => {}, write: (c, e, cb) => cb(), final: (cb) => cb() });
claimed.on("error", () => {});
assert.equal(upgrade({ "x-bili-plugin": "fakehost", "x-bili-plugin-conversation": "ses" }, claimed), true); // positive control

b. zh-CN 镜像缺失:docs/opencode-responses-websocket.zh-CN.md 存在但未动,新增 Architecture 章节只进了英文版。建议中文文本(可直接并入):

架构:通用 WebSocket 桥(#1467 第二阶段外壳)

WebSocket 拦截被拆分为协议无关的外壳与按协议的编解码器(codec):

  • src/ws-bridge.ts(外壳)负责准入(loopback 来源、x-bili-plugin 车道标记、x-bili-plugin-conversation 会话头、隧道目标守卫)、upgrade 握手、每连接簿记与关停拆除。它不感知任何线上协议。
  • src/responses-ws.ts(codec)负责一切 Responses 专属逻辑:帧校验、previous_response_id 客户端历史契约、带续传检查点的上游 WebSocket 传输,以及 SSE 响应汇聚。它在唯一接线点以 responsesCodec 注册进 codec 表(src/server.ts 中的 installWebSocketBridge(server, dispatch, log, [responsesCodec]))。

第二种线上协议 = 一个新 codec 文件 + 一条表项——外壳零改动。外壳契约是 WsBridgeCodec(name、plugin 标记、URL 认领、会话工厂)与 WsBridgeSession(message/close/shutdown);会话获得带标签的 logger、升级后的 peer、upgrade 请求、被认领的上游 URL 与 ACP 管线入口(dispatch),并预期在进程内重建协议形状的 envelope。tests/ws-bridge.test.ts 用一个合成的第二 codec 驱动外壳,保持 codec 表的诚实性。

未知协议不可压缩(folding 需要知道历史在 wire 格式中的位置);它们留在 #1472 透明透传车道上,不受本桥影响。

这两点我没有直接 push 到 generic 分支:该分支反正要 replay 到 v180 线(见 ③),随 replay 一并落入可避免在 stale lineage 上的额外往返。如希望直接应用到当前 generic 分支,我可以执行。

七问结论:

  1. 是什么问题:非缺陷——一步泛化重构(feat: support wss:// (WebSocket) traffic — opt-in transparent passthrough now, protocol-aware compression later #1467 phase-2),把 WS 拦截中协议无关的一半抽成可复用 shell。
  2. 什么原因导致的:N/A;动机 = 让未来协议成为「一个 codec 文件 + 一条表项」而非再次手术外壳。
  3. 怎么修复的:shell/codec 拆分——准入/握手/簿记/teardown 移入 ws-bridge.ts、由 codec 表驱动;Responses 成为第一个 codec;行为保持的移动(逐行验证)。
  4. 是否有回归问题:未发现——准入/拒绝路径/日志行字节等价,teardown 等价或更严(exactly-once shutdown),帧/历史/transport 未变;本地定向 83/83;CI 待正确基线上全量。
  5. 是否破坏缓存:否——checkpoint 逻辑(ResponsesWsHistory)未触碰,retry-full 契约完整。
  6. 是否改了设计方向:是——引入 WsBridgeCodec 扩展点(表驱动多协议)。边界守住:未知协议仍走 [PR] feat: opt-in transparent WebSocket passthrough (#1467) #1472 透明透传;压缩保持 protocol-aware by construction。此方向由 owner 发起,我建议接受;范围建议 = 并入本 PR(shell 只为服务本 PR 的代码而存在,拆开则需先落死代码或重复抽取两次)。
  7. 是否引入新配置:无(env/config/CLI flag 均无)。

判定:重构本身审查通过(行为保持,证据如上)。合入前必须先解决基线谱系(replay 至 v180 tip,含 Buffer fix)+ 落实上述两个小修,最终 head 全量 CI 绿 → human merge。

@ranxianglei

Copy link
Copy Markdown
Owner

Huge thanks @darkness-A — this draft is the feature. The ACP-aware Responses-over-WebSocket design (dual-leg checkpoints, envelope dispatch into the existing pipeline, the diagnostics conventions, and the sparse-terminal reconstruction from #1866) all come from your work, and it's carried forward verbatim in the stack below.

What we extended on top, and why:

  1. Generic bridge shell (src/ws-bridge.ts): the OpenCode-specific upgrade handling became a codec table (WsBridgeCodec/WsBridgeSession) with admission (loopback + conversation header + plugin marker when present), tunnel guard, labeled logging, and exactly-once teardown. Reason: a second wire protocol already needed the same seat —
  2. codex Responses-over-WebSocket codec (codexResponsesCodec, prefix lane, conversation header session-id): codex CLI ships Responses-over-WS behind a feature flag (verified by binary strings in 0.147.0); with the shell in place it's one file + one table entry instead of a parallel implementation. This is the feat: support wss:// (WebSocket) traffic — opt-in transparent passthrough now, protocol-aware compression later #1467 phase-2 shape.
  3. Smaller hardening from review: pipeline Buffer bodies accepted in the transport, per-codec allowStreamFlag policy (codex always sends stream:true; OpenCode keeps the strict absent-flag rule), log-race guards in tests (poll instead of racing the writer on slow runners), deterministic close-frame waits.

All of your commits are inside PR #1968 (2026-10-02_opencode-responses-ws-generic, currently CI-green on all four legs, mergeStateStatus=CLEAN) — merging it merges your stack plus the extensions; attribution is preserved in history. Closing this PR in favor of #1968 to keep one review thread. Follow-up tracking for the lane's idle-socket retention stays in #1926.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants