Skip to content

fix(#1958): bind V2 hook proxyBase to the baked origin on already-routed model requests - #1959

Closed
ranxianglei wants to merge 1 commit into
masterfrom
2026-10-03_opencode-v2-routed-proxybase
Closed

ranxianglei wants to merge 1 commit into
masterfrom
2026-10-03_opencode-v2-routed-proxybase

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Closes #1958

Problem

OpenCode V2 native entry (src/agent/opencode-native.ts, createNativeRoute): model requests that ALREADY carry a /bili/ prefix (origin baked into the URL by config/overlay) skip s.proxyBase initialization. The routed gate records the pinned origin via noteRoutedOrigin, then exits early because isModelApiUrl() is false for /bili/ paths — correct for avoiding a double URL rewrite, but it leaves the hook state unbound. The generic V2 http.request hook requires state.proxyBase before stamping (opencode-v2.ts httpRequestHook), so every request of such a session arrived at the proxy WITHOUT x-bili-plugin / x-bili-plugin-conversation / context-window / max-output markers.

Consequences on the server side:

  • pluginMode = pluginAgent !== undefined (server.ts) → the session ran as plain-client proxy mode (server-side compression, nudge injection, acp_summary carrier) while the ACP-native agent believed it owned compression locally (KDD "Two compression modes").
  • x-bili-plugin-conversation is ignored for identity resolution without the plugin marker (session-id.ts).
  • Host-reported window/output limits never reached the proxy (registry/configured fallback sizing).

The launcher lane (opencode.ts route) and the shared fetch patch both handle pre-routed URLs correctly; only this native V2 route had the gap.

Fix

In createNativeRoute, when the URL is already routed: after noteRoutedOrigin, bind the hook's stamping base to the origin baked into the request URL (new URL(url).origin) and return.

Deliberately NOT via resolveLive/rewrite:

Behavior change (intended)

Pre-routed OpenCode V2 native sessions: model requests previously carried NO plugin identity headers (proxy treated them as plain clients → proxy-mode compression binding) → now carry x-bili-plugin: opencode + conversation/window/output markers bound to the baked origin (plugin-mode binding, as designed). No URL rewrite, no respawn, no config surface change, no persistence/wire-format change beyond the intended headers.

Tests

New tests/issue1958-routed-session.test.ts (4 cases):

  1. core repro — routed URL kept byte-identical (URL/method/auth/body), all plugin headers stamped (fails on master: null !== 'opencode');
  2. baked origin wins over lifecycle state.origin; zero respawns triggered;
  3. raw model URL still rewritten exactly once; the routed form then passes through;
  4. non-model /bili/…/v1/models and /__bili/health stay unbound and unstamped.

Updated tests/opencode-native.test.ts ("route: non-model-API and already-routed URLs are left untouched") — its final assertion (s.proxyBase === undefined after a routed URL) encoded the bug; it now asserts the baked origin binds while the request reference stays untouched, and pins non-model URLs as unbound separately.

Verified locally: targeted suites green (issue1958 + opencode-native + opencode-v2: 33/33), typecheck clean, build OK. Full suite → CI.

…ted model requests

createNativeRoute recorded the pinned origin via noteRoutedOrigin but then
exited before initializing s.proxyBase (isModelApiUrl is false for /bili/
paths), so the generic V2 http.request hook never stamped x-bili-plugin /
conversation / window / output headers on pre-routed sessions — the proxy
ran them as plain-client proxy-mode traffic while the ACP-native agent
believed it owned compression (#1365 pinned-channel semantics kept the URL
untouched; this binds stamping without touching the URL).

Bind s.proxyBase to new URL(url).origin — the origin baked into the request
URL, NOT lifecycle state.origin (which may point elsewhere) — and skip
resolveLive/rewrite: a pinned channel must not follow a replacement instance
(#1365/#1370). Launcher lane (opencode.ts) and the shared fetch patch already
handled pre-routed URLs; only this native V2 route had the gap.
@github-actions

github-actions Bot commented Oct 3, 2026

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-10-03_opencode-v2-routed-proxybase (d6b61a9)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.1964-pr.1959.e9e7a80

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-pr1959.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] 🏷 PR Review — fix(#1958): bind V2 hook proxyBase to the baked origin on already-routed model requests

已按 review 规范完整审查(方向 → 回归清点 → 代码 → 测试有效性 → diff 整洁度),并本地做了定向验证。结论:可合并(待 CI 全绿;按 §7.4,wire/message-shape 变更必须人工合并)。

结构化七问

1. 是什么问题
OpenCode V2 native 入口(createNativeRoute)对已带 /bili/ 前缀的模型请求只记录了 pinned-origin 证据就提前退出,从未初始化钩子状态 s.proxyBase;而通用 V2 stamping(opencode-v2.ts httpRequestHook → stampHeaders)以 state.proxyBase 为前置门——于是这类会话的所有模型请求到达代理时不带任何 x-bili-plugin* 标记,代理把它当普通客户端(proxy-mode 压缩绑定),与 ACP-native agent 自认本地拥有压缩的设计(KDD「Two compression modes」)形成分裂。

2. 什么原因导致的
根因层在路由决策点:routedBiliModelUrl() 命中后,isModelApiUrl() 对 /bili/ 路径返回 false(这是为避免二次改写的正确设计),但退出路径漏掉了钩子状态的初始化——「URL 已经过 bili」被当成了「插件身份已绑定」。表面现象(缺 header、身份回退、窗口标记丢失)都是这一处未初始化的下游后果。修复打在决策点上(疏,非堵):同一根因换任何形状出现(别的 /bili/ 形态、别的 host)都仍被覆盖,不是对 payload 的过滤式遮罩。

3. 怎么修复的
在 routed 分支里把钩子的 stamping base 绑定到请求 URL 自身烘焙的 origin(new URL(url).origin)然后返回。三个关键取舍都验证过正确:不走 resolveLive(pinned channel 不得跟随替代实例,#1365/#1370 分裂脑约束,测试 2 断言 respawns === 0);不用生命周期 state.origin(请求 URL 是「本次流量实际骑在哪」的权威来源,测试 2 断言烘焙 origin 压过 lifecycle origin);不动 state.origin(只写钩子状态,不越权替换生命周期归属)。与两条兄弟路径的既有做法一致:launcher lane(opencode.ts route 对 routed URL 同样绑 s.proxyBase)和共享 fetch patch(native-intercept.ts dispatchBody 的 routed 分支经 headersFor stamping)本来就处理了这种输入——只有这条 native V2 路由有缺口,且 createNativeRoute 全仓仅 native 入口一处消费,无同款遗漏。

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

5. 是否破坏缓存
不涉及前缀缓存/persistence/dedup 键。这类会话的身份从指纹/匿名解析变为显式 conversation id(KDD #280 偏好的稳定原生标识),是稳定性提升而非漂移。

6. 是否改了设计方向
没有转向——恢复的是既有设计的本意(plugin mode 由 ACP-native agent 拥有压缩),消除的是实现缺口造成的模式分裂。

7. 是否引入了新的配置
不涉及。无新 env/config/CLI/默认值。

验证证据(本地定向,全量交 CI)

收工判定:通过,可合并(wire-shape 变更,按 §7.4 留给人工合并)。

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 CI 跟进:head d6b61a9 全部 14 个 check run 已绿——test(ubuntu/windows × node 22/24)、全部真实客户端 e2e lane(含 real opencode native lane)、registry e2e、build-artifact、version-guard,0 失败 0 待定。

review 结论维持:可合并(wire-shape 变更,按 §7.4 由人工合并:https://github.com/ranxianglei/billion-context/pull/1959)。

@ranxianglei

Copy link
Copy Markdown
Owner Author

关闭:维护者定调该组合为矛盾配置,要"报出来"而非静默兼容——盖章修复(s.proxyBase = baked)属于后者。方向已由 #2020 接替:运行时每 (session, origin) 一次警告 + 指引,请求保持纯代理路径、不盖章。感谢工作,根因分析与测试资产在 #2020 中被复用。

@ranxianglei ranxianglei closed this Oct 4, 2026
yingzhongshuoqi pushed a commit to yingzhongshuoqi/billion-context that referenced this pull request Oct 4, 2026
…e config — expose, don't absorb, don't gate install

Maintainer direction change on ranxianglei#1958: the 'native plugin + hand-written
/bili/ provider baseURL' combo is a conflicting configuration (two access
paths claiming routing ownership of the same requests). The correction is
a loud runtime warning — not silent compat (no stamping/rewrite, the
request stays on the plain-proxy path it encodes) and not an install-time
refusal (a scanner false positive would permanently block installs).

- src/agent/opencode-native.ts (createNativeRoute): on a pre-routed model
  URL, warn once per (session, origin) unless BILLION_CONTEXT_PROXY pins
  the SAME origin (supported attach posture — compared via URL.origin so
  trailing-slash/default-port spellings fold; env, not state.origin,
  because attach arming does not freeze state.origin synchronously).
  No behavior change to the request itself: ranxianglei#1365 pinned-channel
  recording (noteRoutedOrigin) and the isModelApiUrl fall-through are
  untouched; s.proxyBase stays unset for pre-routed URLs.
- tests/issue1958-routed-warn.test.ts: 4 cases — dedup per (session,
  origin) + no stamping; benign pin silent incl. port folding; mismatched
  pin warns; non-model /bili/ paths and /__bili/health never warn.
- CLIENTS(.zh-CN).md / CONFIGURATION(.zh-CN).md: the three OpenCode access
  paths are mutually exclusive; runtime warning + fix-it guide documented.

Supersedes ranxianglei#1959 (silent stamping) and ranxianglei#1972 (install refusal + stamping).
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.

[Bug][OpenCode V2] 已带 /bili/ 前缀的模型请求跳过 proxyBase 初始化,缺失插件会话标记

1 participant