Skip to content

[PR] fix(opencode-v2): stop rewriting the shared http.request payload — route model traffic at fetch time (#4276) - #4280

Closed
ranxianglei wants to merge 4 commits into
masterfrom
wt-4276-ework-daemon
Closed

ranxianglei wants to merge 4 commits into
masterfrom
wt-4276-ework-daemon

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

What & why

Closes #4276 — opencode lane: native /bili/ URL rewrite breaks provider-scoped plugins (Kiro silent empty reply, Antigravity baseURL error).

Both opencode V2 entries (native src/agent/opencode-native.ts and launcher src/agent/opencode.ts) rewrote event.request.url IN PLACE inside their unscoped http.request hook. opencode dispatches ONE mutable event object to every matching hook — including provider-ID-scoped hooks from other plugins AND opencode's own built-in provider hooks (verified against the opencode 2.x binary: the host sends the post-hook request through a call-time-resolved globalThis.fetch; provider SDKs use lazy thunks () => globalThis.fetch, no node:undici). URL-validating provider plugins therefore saw the proxied <origin>/bili/<upstream> URL:

  • Kiro (@servoy/opencode-kiro-auth): response-hook gate q.*.amazonaws.com|runtime.*.kiro.dev failed → AWS bridge never engaged → raw OpenAI-shaped POST hit AWS → non-SSE 200 → retries → silent empty assistant message.
  • Antigravity (@cortexkit/opencode-antigravity-auth): request hook requires origin === generativelanguage.googleapis.com → threw on the proxied URL.

Root fix: the shared hook payload is no longer bili's scratch space. The routes keep only discovery/degrade (resolveLive → s.proxyBase, #1958 conflict warning, #3308/#3654 oversize local refusal); both entries' setup() now installs the existing shared native fetch intercept chain (installNativeFetchIntercept), which performs the <origin>/bili/<url> rewrite AT FETCH TIME. Every hook-visible payload keeps the original upstream URL. The WS handshake hook builds its tunnel itself now (raw-model-URL branch produces byte-identical output to the old rewrite+replace pipeline; already-routed URLs keep the old conversion verbatim).

Second half of the issue: providers.<url>.direct (documented ACTIVE client exemption, previously ZCode-lane only) is now honored in the opencode lanes too — the hook marks matching requests (biliDirect: no stamping, no incremental body swap, no ws tunneling) and the chain sends them direct before any proxy resolution (once-per-prefix log).

Config surface

No new fields. This completes an EXISTING documented field (providers.<url>.direct, CONFIGURATION.md) in a lane that ignored it. The doc section now lists the new lane coverage. No migration needed.

Behavior changes (old → new)

  1. Provider-scoped plugins + opencode built-in provider hooks now see the ORIGINAL upstream URL — the bug fix itself (previously they saw the proxied URL and misbehaved).
  2. Signed-body model requests (APIG scheme) in the opencode V2 lane now get the local bili 改写带 body 签名的模型请求,CodeArts(华为 APIG SDK-HMAC-SHA256)全部 401 #1884 refusal instead of riding unsigned through bili into an upstream 401 (aligns with the owner ruling applied in the other lanes; the V2 lane simply never ran the signed arm before because the chain wasn't installed there).
  3. When the proxy goes down, incremental rounds fall back to direct connection, sending the incremental payload as-is to upstream and causing a 400 (same mechanism across opencode/omp/dsh-native). | 代理挂掉时增量轮次降级直连会把增量载荷原样发给上游导致 400(opencode/omp/dsh-native 同一… #3304 prepareDirectSend body restore + Delta lane: native-mode clients send only deltas, server maintains full state + one-time fold, upstream receives snapsho | 增量车道:native 模式客户端只发增量,服务端全量维护+一次折叠,上游只收快照(#2926 的泛化) #3248 incremental acks (onResponse) activate in V2 processes — these wirings were dead code in V2 before (chain absent); they now cover the degrade/fallback paths only.
  4. Launcher dead-origin handling moves from preemptive unwrap in the hook to the chain's recover→retry→give-up-direct — strictly more recovery (may respawn where the old code went direct immediately); wire outcome identical when recovery succeeds.
  5. Known limitation (no regression — no exemption existed here before): a baseURL already rewritten by another hook earlier in the chain bypasses the direct match in the opencode lane (same boundary class as the ZCode store lane).

Verification

  • NEW tests/issue4276-opencode-v2-hook-payload.test.ts: hook payload immutability + fetch-time routing; Antigravity pool-endpoint passthrough (byte-identical, unproxied); direct exemption; install-gate directions via child-process probe (default installs, BILLION_CONTEXT_PLUGIN=0 fully inert).
  • tests/opencode-native.test.ts: 9 contract assertions flipped (hook leaves e.request same object + original URL; proxyBase carries the routed origin).
  • tests/opencode-launcher.test.ts: lifecycle tests now drive REAL fetches through the installed chain against recording servers (steady-state routing, native attach 模式(opencode 面)同样缺运行时 respawn:V2 route hook fail-closed + V1 launcher 插件 frozen origin #1135 death-respawn re-target, failed-respawn direct send via local upstream recorder — no real network).
  • Unchanged-and-green locally: opencode-v2 (32), responses-websocket (41, pins WS tunnel parity), native-intercept(+selfheal), opencode-incremental, zcode-json-edit, issue3304-incremental-direct-send — combined targeted run 186/186; typecheck + build clean. Full regression = remote CI.

@ranxianglei ranxianglei changed the title fix(opencode-v2): stop rewriting the shared http.request payload — route model traffic at fetch time (#4276) [PR] fix(opencode-v2): stop rewriting the shared http.request payload — route model traffic at fetch time (#4276) Oct 22, 2026
@github-actions

github-actions Bot commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: wt-4276-ework-daemon (b8e17d3)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.5096-pr.4280.abe84c2

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

Install instructions are refreshed on each push.

awork added 2 commits October 22, 2026 17:19
…ute model traffic at fetch time (#4276)

The V2 opencode entries (native + launcher) rewrote event.request.url in place inside their unscoped http.request hook. opencode hands that one mutable event to every matching hook — provider-ID-scoped plugins included — so URL-validating provider plugins saw the proxied <origin>/bili/<upstream> URL instead of the real upstream: Kiro's AWS bridge silently never engaged (empty assistant replies), Antigravity's request hook threw on the non-canonical origin.

Root fix: the routes keep only discovery/degrade (resolveLive -> proxyBase, oversize local refusal); both setup()s now install the shared native fetch intercept chain which performs the rewrite at fetch time, leaving every hook-visible payload untouched. The WS handshake hook builds its tunnel itself (byte-identical output for formerly-working shapes).

Also honors providers.<url>.direct in these lanes (previously ZCode-only): the hook marks exempt requests (no stamping/incremental/ws-tunnel) and the chain sends them direct before any proxy resolution. No new config surface — completes the documented field in the opencode lanes.

Behavior changes: provider plugins now see original upstream URLs (the fix); signed-body model requests in the V2 lane get the local #1884 refusal instead of unsigned-through-bili -> upstream 401; #3304 prepareDirectSend + #3248 onResponse acks activate in V2 processes (were dead code); launcher dead-origin handling moves from preemptive unwrap to chain recover/retry/give-up-direct.

Closes #4276
…fetch-time routing

The fetch-time routing change removed every call site of replaceRequestTarget in src/agent/opencode-native.ts and src/agent/opencode.ts but left the exported declaration in place, tripping the CI unused-exports gate (DEAD). Delete it.
… exemption

The shared state carries directPrefixes to BOTH chains each entry installs (V1 server() + V2 setup), so the exemption is honored on V1 hosts as well; the previous sentence named the V2 lanes only.
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

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

审核完成。结论先行:方向正确、机制成立;已顺手修掉两处小问题(死导出致 CI 红 + 文档少报覆盖面),新 head CI 绿后可合。

查重: 不重复——#4276 是此症状下唯一的 open 条目,本 PR 正是它的修复 PR;其余 Antigravity 相关条目(#3129/#3142/#3280/#3282)是代理侧 v1internal 信封拆包问题,机制与症状均不同。

标签: bug, opencode, opencodev2, scope/wiring, p2(已打)——opencode V2 车道真实缺陷、破 provider-scoped 插件,但有绕行(BILLION_CONTEXT_PLUGIN=0),p2。

验证证据

  • 全 diff 逐文件通读(10 文件 +574/-65),并读改动前代码清点既有行为。
  • 本地定向测试(rebased head,非全量):tests/issue4276-opencode-v2-hook-payload.test.ts 4/4(含子进程 install-gate 探针)、opencode-native 40/40、opencode-launcher 5/5(真实 fetch 穿过已装链到记录服务器)、native-intercept 64/64、native-intercept-selfheal 22/22、opencode-v2 32/32、opencode-native-v1 28/28、opencode-incremental / responses-websocket / zcode-json-edit / issue3304-incremental-direct-send 全绿;typecheck clean、build OK。
  • 旧 head af77390 的 CI 红定位:unused-exports gate 报 DEAD src/agent/native-intercept.ts: replaceRequestTarget(本 PR 删光了调用点却留下导出),连带整个 test lane 被 skip;已直接修掉(删除该死函数,独立标注 commit 968ab10),本地 gate 转 clean。
  • 分支落后 master 17 个提交 → 已 rebase 到当前 master(零文件重叠,clean)。另修一处文档少报:CONFIGURATION.md 原写「OpenCode V2 lanes」,但 directPrefixes 挂在共享 state 上,V1+V2 两条链都生效,已改为如实表述(commit 85a79a1)。
  • 旧 head 上真实客户端车道 e2e-native-opencode 已过(路由走链的端到端信号);其余 e2e 车道全绿。
  • 说明:审核窗口内另一会话曾向同一分支推过一个等价的死导出删除提交(58958af,未 rebase);我的 push 以 rebase 版覆盖之——两树除 master 侧内容外一致,无内容丢失。
  • 新 head 85a79a1 的 CI 在跑(主 CI lane queued,快车道已绿),完成后确认。

八问

  1. 是什么问题: opencode V2 两个入口的 http.request hook 把共享请求对象的 URL 就地改写成 <proxy>/bili/<upstream>;opencode 把这个同一对象递给每一个后续 hook,凡校验上游 URL 的第三方 provider 插件与内置 provider hook 全部失效——Kiro 响应桥不触发→静默空回复,Antigravity 请求 hook 直接抛错。附带:文档标注 ACTIVE 的 providers.<url>.direct 豁免在 opencode 车道完全没实现。
  2. 什么原因导致的: hook 把 e.request 当成 bili 私有 scratch space,而它实际是每个后续 hook 都会读的共享 wire payload、也是 host 最终发出的那个 Request。改写发生在错误的层——仓库里本就有 fetch-time 链(installNativeFetchIntercept)在发送时刻改写、不动任何 hook 可见状态,V2 入口只是从未安装它。
  3. 影响范围与严重程度: 装了 provider-scoped 鉴权/URL 校验插件(Kiro、Antigravity)的 opencode V2 用户:模型请求不可用(空回复或异常),绕行 = BILLION_CONTEXT_PLUGIN=0(失去压缩);普通 opencode 用户无感。p2。
  4. 怎么修复的: hook 只做发现+盖戳(设 state.proxyBase),URL 改写移入 setup() 为两个 V2 入口安装的共享 fetch-time 链;ws.handshake 自建隧道(baked-overlay 与 raw-URL 两种形状的输出逐字符核对过,与旧管线 byte-identical);providers.<url>.direct 由 hook 标 biliDirect(不盖戳、不走 incremental),链在 proxy 解析前直发。
  5. 是否有回归问题: 逐项审过既有行为——(a) 路由后请求的 wire 字节不变(<origin>/bili/<url>,盖戳顺序保持);(b) bug: dsh-native — attach 目标瞬时不可达 → 回退 spawn 第二实例:routed baseURL 下工具/模型通道必然脑裂(工具 404 → preflight 502 → 会话不可自愈) #1365/native attach 模式(opencode 面)同样缺运行时 respawn:V2 route hook fail-closed + V1 launcher 插件 frozen origin #1135 routed-origin 证据语义不变(旧 V2 无链,raw 流量本来就不产生 noteRoutedOrigin;baked URL 仍由 hook 在 opencode-native.ts:171 记录),launcher overlay 流量仍走 routed 分支 recover→retry→give-up;(c) V1 车道逻辑未动(仅新增豁免生效,见变更 5);(d) 有意行为变更见下表。未发现无意回归。
  6. 是否破坏缓存: 不涉及——wire 形状逐字节相同,x-bili-plugin-* 头集合不变;豁免的上游本就不经 bili;once-per-prefix 告警有界(Set cap 128)。
  7. 是否改了设计方向: 无转向——是把 V2 对齐到 V1/pi/dsh 已在用的 fetch-time 路由设计,去掉了一个分叉的 hook 层改写机制。
  8. 是否引入新配置: 无——补完既有文档字段 providers.<url>.direct 的实现(状态本就 ACTIVE),CONFIGURATION.md 更新为如实覆盖范围。

行为变更清单(旧 → 新 + 原因)

  1. provider-scoped 插件/内置 hook 看到的是原始上游 URL(原:/bili/ 代理 URL)——即修复本体。
  2. opencode V2 车道的签名体模型请求:旧=裸奔进代理(→上游 401 body-hash mismatch);新=本地 bili 改写带 body 签名的模型请求,CodeArts(华为 APIG SDK-HMAC-SHA256)全部 401 #1884 拒绝(或凭据可用时重签)——因为链现在装在 V2 进程里了。
  3. When the proxy goes down, incremental rounds fall back to direct connection, sending the incremental payload as-is to upstream and causing a 400 (same mechanism across opencode/omp/dsh-native). | 代理挂掉时增量轮次降级直连会把增量载荷原样发给上游导致 400(opencode/omp/dsh-native 同一… #3304 prepareDirectSend 恢复 + Delta lane: native-mode clients send only deltas, server maintains full state + one-time fold, upstream receives snapsho | 增量车道:native 模式客户端只发增量,服务端全量维护+一次折叠,上游只收快照(#2926 的泛化) #3248 incremental acks 在 V2 降级/回退路径生效(此前在那条进程里是死代码)——仅影响 proxy 中途死亡场景。
  4. launcher 死 origin 处理:旧=hook 内抢先 unwrap(e.request→upstream);新=链级 recover→retry→give-up-direct;恢复成功时 wire 结果相同,恢复机会严格更多。
  5. providers.<url>.direct 现在被所有 opencode 车道(V1+V2)遵守(原:opencode 车道完全忽略)——符合该字段文档意图("any lane can honor it identically")。

判定

代码与测试通过审查,发现的两个小问题已就地修掉并独立标注。等新 head CI 全绿即可合并。

The new tests/issue4276-opencode-v2-hook-payload.test.ts spawns a child process (spawnSync node --import tsx for the setup() install-gate probe), so the run-ci-tests.mjs #1248 tripwire requires it in the shared LIVE_FILES table; without that every shard die()s before running any test. Concurrency review: the probe is a short-lived SYNCHRONOUS child (120s cap) driving the real entry's setup({}) in attach mode against a local listen(0) probe server - no proxy fork, no fixed-port daemon; all ports in the file are ephemeral and XDG_STATE_HOME is mkdtemp-isolated, so its zone band goes unused harmlessly.
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

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

补充一条:新 head 的主 CI test lane 红了,根因是一个几行级遗漏——新增的 tests/issue4276-opencode-v2-hook-payload.test.ts 用 spawnSync 起子进程做 install-gate 探针,但没登记进 scripts/live-files.mjs 的 LIVE_FILES(#1248 tripwire),导致 6 个 shard 在跑任何测试之前就 die()。已就地修掉并推上分支(commit b8e17d3):并发面核对过——该探针是短命同步子进程(120s 上限)、attach 模式打本地 listen(0) 探针服务器、无 proxy fork、全文件端口皆临时、状态目录 mkdtemp 隔离,登记后其 zone band 闲置无害。等这轮 CI 结果。

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

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

CI 全绿确认:最终 head(b8e17d3,含 LIVE_FILES 登记修复)在 master 上的合并 ref 37810b5 全部 10 个 workflow 通过——主 CI(unused-exports gate + 6 个 test shard 全量回归)、e2e 全部车道(含真实客户端 e2e-native-opencode)、Windows codex overlay、build artifact。无红项。

分支最终形态(master + 4 commits):

  1. 5602c07db 主体修复:hook 不再改写共享 http.request payload,模型流量改由 fetch-time 链路由;providers..direct 豁免生效;
  2. 968ab1067 删除本 PR 遗留的死导出 replaceRequestTarget(CI unused-exports gate 红因);
  3. 85a79a11f CONFIGURATION.md 如实覆盖范围(V1+V2 全 opencode 车道);
  4. b8e17d363 新测试登记进 scripts/live-files.mjs LIVE_FILES(tests/claude-native.test.ts linux live-E2E subtests fail/hang in shared multi-agent sandboxes #1248 tripwire)。

审核结论维持首条回复(八问 + 行为变更清单):未发现无意回归,可合并。合并动作按规矩留给你:#4280

@ranxianglei

Copy link
Copy Markdown
Owner Author

(ai: zhipuai-lb/glm-5.3)

陈旧票清理(第三批动作①):目标 issue #4276 已关。为避免重复劳动关闭本 PR;如判断有误请直接 reopen 并留言。

@milesbuckton

Copy link
Copy Markdown

Re-tracked in follow-up issue #5625 to facilitate rework on current master against #4505.

ranxianglei pushed a commit that referenced this pull request Nov 3, 2026
…route model traffic at fetch time (#5625)

Rework of PR #4280 (reverted by c076614 over a master conflict; original
report #4276) on top of current master.

The opencode-lane http.request hook used to replace e.request with a new
Request pointed at `${proxyOrigin}/bili/<originalUrl>`. opencode hands that
one mutable event to every later http.request/http.response hook, including
provider-scoped hooks from other plugins, so provider auth plugins validating
the upstream URL broke: Kiro's bridge silently stopped engaging (empty
responses after ~10 retries) and Antigravity threw "pool requests need the
default Google base URL" before its own hook could run.

The routes now only DISCOVER the proxy (state.proxyBase; oversize refusal /
direct-degrade on death) and leave the hook-visible payload untouched. Both
setup()s install the shared native fetch intercept chain, which performs the
/bili/ rewrite at fetch time — the bytes other plugins see in their hooks are
unchanged. The WS handshake hook builds its own tunnel URL instead of relying
on the rewritten request. These lanes also honour providers.<url>.direct
(#1622), previously implemented only in the ZCode lane: matching requests are
marked biliDirect in the hook and sent direct by the chain.

Behavior changes (disclosed per policy):
- Provider plugins in opencode lanes see original upstream URLs again.
- Signed-body model requests (x-goog-api-key / SDK-HMAC-SHA256) in V2
  processes get the local #1884 refusal instead of unsigned-through-bili.
- #3304 prepareDirectSend and #3248 onResponse acks activate in V2 processes
  (dead code before the chain existed there).
- Launcher dead-origin handling moves from preemptive unwrap-to-upstream to
  the chain's recover/retry/give-up-direct path.
- v1internal pool-shaped URLs (e.g. daily-cloudcode-pa.googleapis.com,
  recognized since #4256 for DSH Desktop Code-Assist traffic) ride the chain
  like any other model request: byte-preserving forward through bili, no
  bili identity headers invented. Hosts wanting such traffic fully off bili
  opt out per upstream via providers.<url>.direct.

Tests: new tests/issue5625-opencode-v2-hook-payload.test.ts pins the hook
payload contract (untouched request, fetch-time routing, pool dialect,
direct exemption, install gate); launcher/native suites updated to the
fetch-time contract; the #4669 inline-composition test installs the chain
like its sibling and asserts the hook keeps the payload intact.

Closes #5625
ranxianglei pushed a commit that referenced this pull request Nov 4, 2026
…route model traffic at fetch time (#5625)

Rework of PR #4280 (reverted by c076614 over a master conflict; original
report #4276) on top of current master.

The opencode-lane http.request hook used to replace e.request with a new
Request pointed at `${proxyOrigin}/bili/<originalUrl>`. opencode hands that
one mutable event to every later http.request/http.response hook, including
provider-scoped hooks from other plugins, so provider auth plugins validating
the upstream URL broke: Kiro's bridge silently stopped engaging (empty
responses after ~10 retries) and Antigravity threw "pool requests need the
default Google base URL" before its own hook could run.

The routes now only DISCOVER the proxy (state.proxyBase; oversize refusal /
direct-degrade on death) and leave the hook-visible payload untouched. Both
setup()s install the shared native fetch intercept chain, which performs the
/bili/ rewrite at fetch time — the bytes other plugins see in their hooks are
unchanged. The WS handshake hook builds its own tunnel URL instead of relying
on the rewritten request. These lanes also honour providers.<url>.direct
(#1622), previously implemented only in the ZCode lane: matching requests are
marked biliDirect in the hook and sent direct by the chain.

Behavior changes (disclosed per policy):
- Provider plugins in opencode lanes see original upstream URLs again.
- Signed-body model requests (x-goog-api-key / SDK-HMAC-SHA256) in V2
  processes get the local #1884 refusal instead of unsigned-through-bili.
- #3304 prepareDirectSend and #3248 onResponse acks activate in V2 processes
  (dead code before the chain existed there).
- Launcher dead-origin handling moves from preemptive unwrap-to-upstream to
  the chain's recover/retry/give-up-direct path.
- v1internal pool-shaped URLs (e.g. daily-cloudcode-pa.googleapis.com,
  recognized since #4256 for DSH Desktop Code-Assist traffic) ride the chain
  like any other model request: byte-preserving forward through bili, no
  bili identity headers invented. Hosts wanting such traffic fully off bili
  opt out per upstream via providers.<url>.direct.

Tests: new tests/issue5625-opencode-v2-hook-payload.test.ts pins the hook
payload contract (untouched request, fetch-time routing, pool dialect,
direct exemption, install gate); launcher/native suites updated to the
fetch-time contract; the #4669 inline-composition test installs the chain
like its sibling and asserts the hook keeps the payload intact.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

opencode lane: native /bili/ URL rewrite breaks provider-scoped plugins (Kiro silent empty reply, Antigravity baseURL error)

2 participants