diff --git a/src/agent/opencode-native.ts b/src/agent/opencode-native.ts index 03bbd87558..e9e9f5c349 100644 --- a/src/agent/opencode-native.ts +++ b/src/agent/opencode-native.ts @@ -28,10 +28,16 @@ // and re-probe only after HEALTH_PROBE_TTL_MS, so a healthy proxy costs no // per-request RTT and a dead one is found within at most one TTL. If no origin // can be made healthy the request goes DIRECT (uncompressed) and a one-time -// stderr warning fires — recovery is automatic once a proxy is healthy again -// (no permanent latch; re-checked at least every TTL). Bootstrap retries are -// rate-limited to one attempt per RESPAWN_COOLDOWN_MS so a persistently -// failing spawn does not become a per-request spawn storm. +// diagnostic lands in bili.log — EXCEPT when the body provably exceeds the +// model's context window: per the #2924 ruling (always compress OR always +// refuse) such a request is refused locally with a host-visible retryable +// error instead of riding the direct fallback into a guaranteed upstream 400 +// (#3308 — on an over-window session history that 400 then repeats on every +// later turn until restart). Unknown window/body keeps the fail-open. +// Recovery is automatic once a proxy is healthy again (no permanent latch; +// re-checked at least every TTL). Bootstrap 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 opted out (BILI_NATIVE_OPENCODE=0 / BILLION_CONTEXT_PLUGIN=0) // or when a `bili` /bili/ launch owns routing (BILI_PROVIDER_REWRITES). A @@ -102,6 +108,35 @@ export function planNativeOpencode(env: NodeJS.ProcessEnv): { mode: "off" | "att export type OpencodeNativeRouteDeps = LiveOriginResolverDeps; +/** #3308: client-side mirror of #2924's refuse-oversized arm for this lane + * (the V2 route has no takeover gate to carry it). Returns a host-visible + * refusal when the proxy is unavailable AND the body provably cannot fit the + * model's context window — the FLOOR estimate (bytes/4) already exceeds the + * window, and #2924's ledger evidence says real bodies run ABOVE the floor + * (calibratedEstimate ~1.14), so the comparison cannot false-refuse. Unknown + * model, unknown window, unreadable or absent body all return undefined and + * keep the documented fail-open direct send. The size is measured through a + * CLONE — the original Request must stay sendable (#2496 discipline). */ +async function oversizeDirectRefusal(e: V2HttpRequestEvent, s: V2State): Promise { + const model = e.model; + if (model === undefined || typeof model.providerID !== "string" || typeof model.id !== "string") return undefined; + const window = s.windows?.get(`${model.providerID}/${model.id}`); + if (window === undefined || !Number.isFinite(window) || window <= 0) return undefined; + const req = e.request as Request | undefined; + if (req === null || typeof req !== "object" || typeof req.clone !== "function") return undefined; + let bytes: number; + try { + bytes = (await req.clone().arrayBuffer()).byteLength; + } catch { + return undefined; + } + const floorTokens = Math.ceil(bytes / 4); + if (floorTokens <= window) return undefined; + return new Error( + `bili: proxy unavailable and this request cannot be sent uncompressed — body ~${floorTokens} tok (floor estimate ${bytes}B/4) exceeds the ${model.providerID}/${model.id} context window (${window}); upstream would reject it. Recovery is automatic (#3308); retry shortly, or start a fresh session if the local history has outgrown the window.`, + ); +} + /** Build the native-mode route callback consumed by createOpencodeV2Setup. * #1135: spawn and attach share ONE liveness gate — attach arms * state.respawn (verifyAttachAndRecover), so a dead external proxy is @@ -110,6 +145,7 @@ export type OpencodeNativeRouteDeps = LiveOriginResolverDeps; export function createNativeRoute(state: NativeInterceptState, deps: OpencodeNativeRouteDeps = {}): (e: V2HttpRequestEvent, s: V2State) => Promise { const resolveLive = createLiveOriginResolver(state, deps); let warned = false; + let refusedNoticed = false; // #1958: conflicting pre-routed config is exposed once per (session, origin). const conflictWarned = new Set(); return async (e, s) => { @@ -151,6 +187,18 @@ export function createNativeRoute(state: NativeInterceptState, deps: OpencodeNat const target = await resolveLive(); if (target === undefined) { + // #3308: a direct send that provably cannot fit the window is not + // "availability" — refuse it locally (#2924 ruling) instead of + // handing upstream a request that can only 400. + const refusal = await oversizeDirectRefusal(e, s); + if (refusal !== undefined) { + if (!refusedNoticed) { + refusedNoticed = true; + logHostEvent("error", "[native-client]", `bili-native-opencode: proxy unavailable — model request refused locally instead of going direct into a guaranteed upstream 400 (#3308): ${refusal.message}`); + } + s.proxyBase = undefined; + throw refusal; + } if (!warned) { warned = true; logHostEvent("error", "[native-client]", "bili-native-opencode: proxy unavailable — model requests go direct (uncompressed)"); diff --git a/src/agent/opencode-v2.ts b/src/agent/opencode-v2.ts index 8ee186ac1d..6462c3b7ec 100644 --- a/src/agent/opencode-v2.ts +++ b/src/agent/opencode-v2.ts @@ -344,6 +344,17 @@ export function createOpencodeV2Setup(options: OpencodeV2SetupOptions = {}): (ct const url = e.request?.url; if (typeof url !== "string") return; if (options.route !== undefined) { + // #3308: prime the window map BEFORE the route decides — its + // degrade branch needs the model's context window to refuse a + // provably-oversized body locally instead of sending it direct + // into a guaranteed upstream 400. The harvest commits on + // microtasks, so with a slow catalog the first request still + // sees an empty map and keeps the documented fail-open; from + // the second request on the guard is armed even when bootstrap + // never landed. Launcher mode keeps today's ordering: harvesting + // without a detected proxy would only warn about headers bili + // never stamps there. + refreshWindows(ctx, state); // Native mode: route owns discovery + rewriting (it is called // per request even once state.proxyBase is known, because the // URL rewrite must happen per request and env detection alone diff --git a/tests/opencode-native.test.ts b/tests/opencode-native.test.ts index bf38d242cc..c69c2aa4c6 100644 --- a/tests/opencode-native.test.ts +++ b/tests/opencode-native.test.ts @@ -423,6 +423,135 @@ test("native route: attach mode respawns on runtime death and routes to the repl assert.equal((e2.request as Request).url, `${spawned}/bili/${MODEL_URL}`); }); +// #3308: the degrade branch must apply the #2924 ruling — a body that +// provably cannot fit the model's window is refused locally instead of riding +// the direct fallback into a guaranteed upstream 400 (which on an over-window +// session history then repeats on every later turn until restart). +test("route: proxy down + body provably over the model window throws a local refusal", async () => { + const state: NativeInterceptState = { origin: undefined, ready: Promise.resolve(undefined) }; + const route = createNativeRoute(state, { probe: async () => false }); + const s: V2State = { windows: new Map([["prov/model-x", 1_000]]) }; + const body = JSON.stringify({ messages: [{ role: "user", content: "x".repeat(5_000) }] }); + const e: V2HttpRequestEvent = { + sessionID: "ses_ow", + model: { providerID: "prov", id: "model-x" }, + request: new Request(MODEL_URL, { method: "POST", headers: { "content-type": "application/json" }, body }), + }; + await assert.rejects(route(e, s), /proxy unavailable/); + assert.equal((e.request as Request).url, MODEL_URL, "refused request is not rewritten"); + assert.equal(s.proxyBase, undefined); + assert.equal(await (e.request as Request).text(), body, "measuring through the clone leaves the original body sendable"); +}); + +test("route: proxy down + under-window body keeps the fail-open direct send", async () => { + const state: NativeInterceptState = { origin: undefined, ready: Promise.resolve(undefined) }; + const route = createNativeRoute(state, { probe: async () => false }); + const s: V2State = { windows: new Map([["prov/model-x", 1_000_000]]) }; + const body = JSON.stringify({ messages: [{ role: "user", content: "hi" }] }); + const e: V2HttpRequestEvent = { + sessionID: "ses_uw", + model: { providerID: "prov", id: "model-x" }, + request: new Request(MODEL_URL, { method: "POST", headers: { "content-type": "application/json" }, body }), + }; + await route(e, s); + assert.equal((e.request as Request).url, MODEL_URL); + assert.equal(s.proxyBase, undefined); +}); + +test("route: proxy down + unknown or mismatched window keeps the fail-open direct send (no false refusal)", async () => { + const state: NativeInterceptState = { origin: undefined, ready: Promise.resolve(undefined) }; + const route = createNativeRoute(state, { probe: async () => false }); + const bigBody = JSON.stringify({ messages: [{ role: "user", content: "x".repeat(5_000) }] }); + // no window map at all (bootstrap never landed, catalog never harvested) + const s1: V2State = {}; + const e1: V2HttpRequestEvent = { + sessionID: "ses_nw", + model: { providerID: "prov", id: "model-x" }, + request: new Request(MODEL_URL, { method: "POST", headers: { "content-type": "application/json" }, body: bigBody }), + }; + await route(e1, s1); + assert.equal((e1.request as Request).url, MODEL_URL); + // window known for a DIFFERENT model — this model's window is still unknown + const s2: V2State = { windows: new Map([["other/model-y", 1_000]]) }; + const e2: V2HttpRequestEvent = { + sessionID: "ses_mm", + model: { providerID: "prov", id: "model-x" }, + request: new Request(MODEL_URL, { method: "POST", headers: { "content-type": "application/json" }, body: bigBody }), + }; + await route(e2, s2); + assert.equal((e2.request as Request).url, MODEL_URL); + // no model info on the event — cannot attribute a window + const s3: V2State = { windows: new Map([["prov/model-x", 1_000]]) }; + const e3: V2HttpRequestEvent = { + sessionID: "ses_nm", + request: new Request(MODEL_URL, { method: "POST", headers: { "content-type": "application/json" }, body: bigBody }), + }; + await route(e3, s3); + assert.equal((e3.request as Request).url, MODEL_URL); +}); + +test("setup(route): window priming before the route arms the refusal on a fresh degraded session", async () => { + const state: NativeInterceptState = { origin: undefined, ready: Promise.resolve(undefined) }; + const hooks: Array<{ name: string; cb: (e: V2HttpRequestEvent) => void | Promise }> = []; + const ctx: V2PluginContext = { + session: { + hook: (name, cb) => { + hooks.push({ name, cb }); + return Promise.resolve({ dispose() {} }); + }, + }, + model: { + list: () => Promise.resolve({ data: [{ providerID: "prov", id: "model-x", limit: { context: 1_000 } }] }), + }, + }; + const setup = createOpencodeV2Setup({ route: createNativeRoute(state, { probe: async () => false }) }); + const cleanup = await setup(ctx); + try { + const body = JSON.stringify({ messages: [{ role: "user", content: "x".repeat(5_000) }] }); + const e: V2HttpRequestEvent = { + sessionID: "ses_prim", + model: { providerID: "prov", id: "model-x" }, + request: new Request(MODEL_URL, { method: "POST", headers: { "content-type": "application/json" }, body }), + }; + // The harvest commits on microtasks ahead of the route's own awaits, + // so even the FIRST request of a never-bootstrapped session is refused + // once the catalog answers fast (#3308 priming order). + await assert.rejects(async () => { await hooks[0].cb(e); }, /proxy unavailable/); + assert.equal((e.request as Request).url, MODEL_URL); + } finally { + cleanup(); + } +}); + +test("setup(route): empty catalog keeps the fail-open direct send (no false refusal)", async () => { + const state: NativeInterceptState = { origin: undefined, ready: Promise.resolve(undefined) }; + const hooks: Array<{ name: string; cb: (e: V2HttpRequestEvent) => void | Promise }> = []; + const ctx: V2PluginContext = { + session: { + hook: (name, cb) => { + hooks.push({ name, cb }); + return Promise.resolve({ dispose() {} }); + }, + }, + model: { list: () => Promise.resolve({ data: [] }) }, + }; + const setup = createOpencodeV2Setup({ route: createNativeRoute(state, { probe: async () => false }) }); + const cleanup = await setup(ctx); + try { + const body = JSON.stringify({ messages: [{ role: "user", content: "x".repeat(5_000) }] }); + const e: V2HttpRequestEvent = { + sessionID: "ses_empty", + model: { providerID: "prov", id: "model-x" }, + request: new Request(MODEL_URL, { method: "POST", headers: { "content-type": "application/json" }, body }), + }; + await hooks[0].cb(e); + assert.equal((e.request as Request).url, MODEL_URL); + assert.equal((e.request as Request).headers.get("x-bili-plugin"), null, "degraded request stays unstamped"); + } finally { + cleanup(); + } +}); + test("native route: attach mode transient blip re-attaches to the SAME origin (no migration, #1135)", async () => { const ext = "http://10.0.0.5:9000"; let up = false;