From 202e8116a91a30a3facc9a893544484c1c031d2b Mon Sep 17 00:00:00 2001 From: Sisyphus Date: Thu, 17 Sep 2026 13:39:56 +0800 Subject: [PATCH] fix(agent): preset BILLION_CONTEXT_PROXY attaches the opencode v2 native plugin instead of standing down MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The pseudo-attach hole: presetting BILLION_CONTEXT_PROXY and running bare opencode disarmed the native bootstrap entirely (no spawn, no attach) while tools still discovered the proxy through the env — model traffic silently went direct, the proxy never saw the session, and every tool forward 404'd ("unknown plugin conversation"). planNativeOpencode now treats the preset value as an external attach target (probe + rewrite + stamp, same semantics as BILLION_CONTEXT_ATTACH; explicit ATTACH wins when both are set). Kill switches (BILLION_CONTEXT_PLUGIN=0, BILI_NATIVE_OPENCODE=0) and /bili/ launches (BILI_PROVIDER_REWRITES) still stand down; a garbage preset falls back to spawn. Also: the opencode v2 plugin tool forward now returns guided text when the proxy reports the conversation was never registered, instead of surfacing the raw 404 that sent the model down a "refresh/restart" tangent. Verified in-container end-to-end (real qwen upstream) across all four postures: proxy (baseURL), launcher (bili opencode), native spawn (bare opencode + plugin), and preset-env attach — session registration, live conversation, acp_status real execution, /acp panel render. --- src/agent/native-bootstrap.ts | 16 ++++++++++++++++ src/agent/opencode-native.ts | 28 ++++++++++++++++++---------- src/agent/opencode-v2.ts | 6 +++++- tests/opencode-native.test.ts | 25 +++++++++++++++++++++---- 4 files changed, 60 insertions(+), 15 deletions(-) diff --git a/src/agent/native-bootstrap.ts b/src/agent/native-bootstrap.ts index d519b2ef17..6d0a456e5b 100644 --- a/src/agent/native-bootstrap.ts +++ b/src/agent/native-bootstrap.ts @@ -25,6 +25,22 @@ export function nativeBootstrapGate(env: NodeJS.ProcessEnv, optOutKey: string): return true; } +/** Normalize a preset BILLION_CONTEXT_PROXY value (http/https, trailing + * slashes stripped) for attach-style routing. Returns undefined when unset, + * blank, or not a valid http(s) origin. */ +export function proxyEnvOrigin(env: NodeJS.ProcessEnv): string | undefined { + const raw = env.BILLION_CONTEXT_PROXY; + if (raw === undefined) return undefined; + const url = raw.trim(); + if (url.length === 0 || !/^https?:\/\//i.test(url)) return undefined; + try { + new URL(url); + } catch { + return undefined; + } + return url.replace(/\/+$/, ""); +} + /** External-proxy attach signal (#809): when BILLION_CONTEXT_ATTACH holds a * valid http(s) origin, a host-native entry routes model traffic THROUGH that * pre-existing proxy instead of spawning its own — no ownership, no respawn, diff --git a/src/agent/opencode-native.ts b/src/agent/opencode-native.ts index 41903d7a64..b28ef4bbe4 100644 --- a/src/agent/opencode-native.ts +++ b/src/agent/opencode-native.ts @@ -30,9 +30,10 @@ // retries are rate-limited to one attempt per RESPAWN_COOLDOWN_MS so a // persistently failing spawn does not become a per-request spawn storm. // -// Skipped when a proxy is already managed (BILLION_CONTEXT_PROXY set by a -// `bili` launch, or BILI_PROVIDER_REWRITES set by a `bili` /bili/ launch) or -// opted out (BILI_NATIVE_OPENCODE=0 / BILLION_CONTEXT_PLUGIN=0). +// Skipped when opted out (BILI_NATIVE_OPENCODE=0 / BILLION_CONTEXT_PLUGIN=0) +// or when a `bili` /bili/ launch owns routing (BILI_PROVIDER_REWRITES). A +// preset BILLION_CONTEXT_PROXY is treated as an EXTERNAL attach target +// (probe + rewrite + stamp) instead of a stand-down — see planNativeOpencode. // // Deployment: OpenCode 2.x `plugin` entries must be DIRECTORIES whose index.js // is the entrypoint (bare file paths are rejected, #754 probe) — `bili plugin @@ -41,7 +42,7 @@ // src/launcher.ts prepareOpencodeHttpRewrite). import { ensureProxyRunning, LAUNCHER_DEFAULT_HOST } from "../launcher.js"; -import { markNativeHost, nativeAttachOrigin, nativeBootstrapGate, nativeProxyScriptPath, singleFlight } from "./native-bootstrap.js"; +import { markNativeHost, nativeAttachOrigin, nativeBootstrapGate, nativeProxyScriptPath, proxyEnvOrigin, singleFlight } from "./native-bootstrap.js"; import { isModelApiUrl, readyOrigin, type NativeInterceptState } from "./native-intercept.js"; import { createOpencodeV2Setup, type V2HttpRequestEvent, type V2State } from "./opencode-v2.js"; @@ -50,13 +51,20 @@ export function shouldBootstrapNativeOpencode(env: NodeJS.ProcessEnv): boolean { return nativeBootstrapGate(env, "BILI_NATIVE_OPENCODE"); } -/** Decide this process's native posture (#809). Precedence off > attach > - * spawn: a launcher-owned proxy or an opt-out stands us down entirely (even - * over an explicit attach); otherwise attach to BILLION_CONTEXT_ATTACH when - * given, else spawn our own watchdog proxy. */ +/** Decide this process's native posture (#809). Precedence kill-switch > + * /bili/ rewrite launch > attach > spawn. A preset BILLION_CONTEXT_PROXY is + * an ATTACH target, not a stand-down: the pseudo-attach hole (preset env + + * bare opencode) used to disarm routing entirely while tools still found the + * proxy through the env — model traffic went direct, the proxy never saw the + * session, and every tool forward 404'd. Attach keeps the user's intent + * ("route through THIS proxy") and matches BILLION_CONTEXT_ATTACH semantics + * (probe + rewrite + stamp); explicit BILLION_CONTEXT_ATTACH wins when both + * are set. A /bili/ launch (BILI_PROVIDER_REWRITES) still stands us down — + * its URLs are already proxy-shaped and isModelApiUrl skips them. */ export function planNativeOpencode(env: NodeJS.ProcessEnv): { mode: "off" | "attach" | "spawn"; attachOrigin?: string } { - if (!shouldBootstrapNativeOpencode(env)) return { mode: "off" }; - const attachOrigin = nativeAttachOrigin(env); + if (env.BILLION_CONTEXT_PLUGIN === "0" || env.BILI_NATIVE_OPENCODE === "0") return { mode: "off" }; + if (env.BILI_PROVIDER_REWRITES !== undefined) return { mode: "off" }; + const attachOrigin = nativeAttachOrigin(env) ?? proxyEnvOrigin(env); if (attachOrigin !== undefined) return { mode: "attach", attachOrigin }; return { mode: "spawn" }; } diff --git a/src/agent/opencode-v2.ts b/src/agent/opencode-v2.ts index 7ae0ed6bb8..3559b3ea95 100644 --- a/src/agent/opencode-v2.ts +++ b/src/agent/opencode-v2.ts @@ -205,7 +205,11 @@ export function createOpencodeV2Setup(options: OpencodeV2SetupOptions = {}): (ct const result = await forwardTool(base, tctx.sessionID, t.name, args); return { content: result }; } catch (err) { - return { content: err instanceof Error ? err.message : String(err) }; + const msg = err instanceof Error ? err.message : String(err); + if (msg.includes("no model request has arrived with this conversation id yet")) { + return { content: "bili: no ACP state for this session yet — no model request has been routed through the proxy. Tell the user to send one normal message first; ACP activates automatically once model traffic flows through the proxy (verify the provider baseURL goes through bili, or launch via `bili opencode` / the installed plugin)." }; + } + return { content: msg }; } }, }); diff --git a/tests/opencode-native.test.ts b/tests/opencode-native.test.ts index 74fb4e3ef7..df1641dd14 100644 --- a/tests/opencode-native.test.ts +++ b/tests/opencode-native.test.ts @@ -274,11 +274,28 @@ test("nativeAttachOrigin: normalizes a valid http(s) origin (trailing slash stri assert.equal(nativeAttachOrigin({ BILLION_CONTEXT_ATTACH: " https://proxy.example.com/ " }), "https://proxy.example.com"); }); -test("planNativeOpencode: default is spawn; opt-out and launcher-owned are off", () => { +test("planNativeOpencode: default is spawn; opt-out and /bili/ launches are off", () => { assert.deepEqual(planNativeOpencode({}), { mode: "spawn" }); assert.deepEqual(planNativeOpencode({ BILLION_CONTEXT_PLUGIN: "0" }), { mode: "off" }); assert.deepEqual(planNativeOpencode({ BILI_NATIVE_OPENCODE: "0" }), { mode: "off" }); - assert.deepEqual(planNativeOpencode({ BILLION_CONTEXT_PROXY: "http://127.0.0.1:36485" }), { mode: "off" }); + assert.deepEqual(planNativeOpencode({ BILI_PROVIDER_REWRITES: '{"vllm":"http://127.0.0.1:1/bili/http://x"}' }), { mode: "off" }); +}); + +test("planNativeOpencode: a preset BILLION_CONTEXT_PROXY is an attach target, not a stand-down", () => { + assert.deepEqual( + planNativeOpencode({ BILLION_CONTEXT_PROXY: "http://127.0.0.1:36485" }), + { mode: "attach", attachOrigin: "http://127.0.0.1:36485" }, + ); + assert.deepEqual( + planNativeOpencode({ BILLION_CONTEXT_PROXY: "http://127.0.0.1:36485/" }), + { mode: "attach", attachOrigin: "http://127.0.0.1:36485" }, + ); + // kill switches and a /bili/ launch still win over the preset + assert.deepEqual(planNativeOpencode({ BILLION_CONTEXT_PROXY: "http://127.0.0.1:36485", BILLION_CONTEXT_PLUGIN: "0" }), { mode: "off" }); + assert.deepEqual(planNativeOpencode({ BILLION_CONTEXT_PROXY: "http://127.0.0.1:36485", BILI_PROVIDER_REWRITES: "{}" }), { mode: "off" }); + // a garbage preset falls back to spawn (self-managed) rather than dead-off + assert.deepEqual(planNativeOpencode({ BILLION_CONTEXT_PROXY: "garbage" }), { mode: "spawn" }); + assert.deepEqual(planNativeOpencode({ BILLION_CONTEXT_PROXY: " " }), { mode: "spawn" }); }); test("planNativeOpencode: attach wins over spawn when no launcher owns the proxy", () => { @@ -289,10 +306,10 @@ test("planNativeOpencode: attach wins over spawn when no launcher owns the proxy assert.deepEqual(planNativeOpencode({ BILLION_CONTEXT_ATTACH: "garbage" }), { mode: "spawn" }); }); -test("planNativeOpencode: a launcher-owned proxy stands down even over an explicit attach", () => { +test("planNativeOpencode: explicit BILLION_CONTEXT_ATTACH wins over the env preset", () => { assert.deepEqual( planNativeOpencode({ BILLION_CONTEXT_PROXY: "http://127.0.0.1:36485", BILLION_CONTEXT_ATTACH: "http://10.0.0.5:9000" }), - { mode: "off" }, + { mode: "attach", attachOrigin: "http://10.0.0.5:9000" }, ); });