From e218d0692504fefda53a41326799201766603c6f Mon Sep 17 00:00:00 2001 From: Y-fyan <195829684+Y-fyan@users.noreply.github.com> Date: Wed, 21 Oct 2026 05:01:34 -0700 Subject: [PATCH] fix(opencode): exclude inline media from direct-refusal token floor (#4121) --- reference/architecture.md | 1 + src/agent/dsh-native.ts | 49 ++---------------------------- src/agent/media-bytes.ts | 46 ++++++++++++++++++++++++++++ src/agent/opencode-native.ts | 19 +++++++----- tests/opencode-native.test.ts | 56 +++++++++++++++++++++++++++++++++++ 5 files changed, 117 insertions(+), 54 deletions(-) create mode 100644 src/agent/media-bytes.ts diff --git a/reference/architecture.md b/reference/architecture.md index 1275d438c9..fcd3654dca 100644 --- a/reference/architecture.md +++ b/reference/architecture.md @@ -144,6 +144,7 @@ billion-context/ │ │ └── tag-echo-filter.ts # Streaming-safe stripper for model-echoed render tags (#206); tool args byte-exact │ ├── agent/ # Thin agent-side plugins (per-host, each with a -native variant) │ │ ├── shared.ts # Host plan/bootstrap shared helpers (BILLION_CONTEXT_PLUGIN gate) +│ │ ├── media-bytes.ts # Shared inline-media byte accounting for native direct-send refusal gates │ │ ├── native-bootstrap.ts # dist/agent/.js → dist/index.js resolution │ │ ├── native-intercept.ts # Model-API fetch routing, attribution and proxy recovery │ │ ├── fetch-chain/ diff --git a/src/agent/dsh-native.ts b/src/agent/dsh-native.ts index 9044d4081a..3a2c9e4bc8 100644 --- a/src/agent/dsh-native.ts +++ b/src/agent/dsh-native.ts @@ -60,8 +60,10 @@ import { createForkAdopter, sideShapedBody } from "./fork-adopt.js"; import { clearIncrementalProbe, createPrepareDirectSend, INCREMENTAL_TURN_MARKER_HEADER, INCREMENTAL_TURN_ID_HEADER, newIncrementalTurnId, noteIncrementalRejection, noteIncrementalAck, planIncrementalTurn, probeIncrementalTurns, rememberOriginalBody, sweepStale, type IncrementalWireMessage } from "./incremental-client.js"; import { detectCompactionOverlayGap, registerCompactionDisable } from "./dsh-compaction-disable.js"; import { maskUrlForDiagnostics } from "../log-mask.js"; +import { mediaBytesOfWireBody } from "./media-bytes.js"; import { conversationRecallStats, learnConversation, recallBoundaryOfMessages, recallConversation, recallHistoryFingerprints, recallLaneHasEntries, recallLaneKey } from "./conversation-recall.js"; export { sideShapedBody } from "./fork-adopt.js"; +export { mediaBytesOfWireBody } from "./media-bytes.js"; export const name = "bili-native"; export const inject = ["tools", "commands", "agents"]; @@ -557,53 +559,6 @@ function effectiveContextWindow(ctx: PluginContext): number | undefined { return modelInfo.byKey.get(modelInfoKey(target.provider, target.model))?.contextWindow; } -// #4111: the gate's oversize arms priced the RAW body at bytes/4 — a floor -// that only holds for text (real text density ≥ ~6.8 B/token, #2122) and -// wildly overbills inline media: a base64 screenshot is resolution-billed at -// ~2,000-4,000 B/token upstream, so a ~4MB image payload minted ~1M phantom -// tokens and tripped a "provably over window" refusal the real upstream never -// would have made. The relay's egress fuse already excludes media from its -// byte math (server/relay.ts — same caliber as preflight's doomed probe); -// this is the agent-lane twin: a recursive, protocol-agnostic walk of the -// wire shapes that carry inline media (anthropic {type:"base64",data} source -// blocks and redacted_thinking payloads, openai input_audio parts and data: -// URLs incl. file file_data, responses input_image whose image_url is a bare -// data-URL string (singular form), google inlineData), returning their byte -// mass so the doom math below bills core text only. Keyed structurally (field -// names/types), never by content: a base64 blob pasted as TEXT (a -// {type:"text"} block or plain string content) is NOT media on any wire and -// stays billed as text — the genuinely-huge-text direction keeps its teeth. -export function mediaBytesOfWireBody(raw: string): number { - let parsed: unknown; - try { - parsed = JSON.parse(raw); - } catch { - return 0; - } - let mediaBytes = 0; - const isObj = (v: unknown): v is Record => typeof v === "object" && v !== null && !Array.isArray(v); - const bytesOf = (s: string): number => Buffer.byteLength(s); - const dataUrlBytes = (s: string): number => (s.startsWith("data:") ? bytesOf(s) : 0); - const walk = (v: unknown): void => { - if (Array.isArray(v)) { - for (const item of v) walk(item); - return; - } - if (!isObj(v)) return; - if (typeof v.data === "string" && (v.type === "base64" || v.type === "redacted_thinking")) mediaBytes += bytesOf(v.data); - if (typeof v.url === "string") mediaBytes += dataUrlBytes(v.url); - if (typeof v.file_data === "string") mediaBytes += dataUrlBytes(v.file_data); - // responses-wire input_image carries the whole data URL as a STRING - // under image_url (singular form) — same shape family as the object form. - if (typeof v.image_url === "string") mediaBytes += dataUrlBytes(v.image_url); - if (isObj(v.input_audio) && typeof v.input_audio.data === "string") mediaBytes += bytesOf(v.input_audio.data); - if (isObj(v.inlineData) && typeof v.inlineData.data === "string") mediaBytes += bytesOf(v.inlineData.data); - for (const child of Object.values(v)) walk(child); - }; - walk(parsed); - return mediaBytes; -} - /** #4111: the shared two-stage doom predicate for the gate's oversize arms * (the `threw` arm and the unattributed arm). Stage 1 prices the RAW body at * bytes/4 — exactly the old math, cheap — and disarms when that is not even diff --git a/src/agent/media-bytes.ts b/src/agent/media-bytes.ts new file mode 100644 index 0000000000..8992b94061 --- /dev/null +++ b/src/agent/media-bytes.ts @@ -0,0 +1,46 @@ +// #4111: the gate's oversize arms priced the RAW body at bytes/4 — a floor +// that only holds for text (real text density ≥ ~6.8 B/token, #2122) and +// wildly overbills inline media: a base64 screenshot is resolution-billed at +// ~2,000-4,000 B/token upstream, so a ~4MB image payload minted ~1M phantom +// tokens and tripped a "provably over window" refusal the real upstream never +// would have made. The relay's egress fuse already excludes media from its +// byte math (server/relay.ts — same caliber as preflight's doomed probe); +// this is the agent-lane twin: a recursive, protocol-agnostic walk of the +// wire shapes that carry inline media (anthropic {type:"base64",data} source +// blocks and redacted_thinking payloads, openai input_audio parts and data: +// URLs incl. file file_data, responses input_image whose image_url is a bare +// data-URL string (singular form), google inlineData), returning their byte +// mass so native refusal gates bill core text only. Keyed structurally (field +// names/types), never by content: a base64 blob pasted as TEXT (a +// {type:"text"} block or plain string content) is NOT media on any wire and +// stays billed as text — the genuinely-huge-text direction keeps its teeth. +export function mediaBytesOfWireBody(raw: string): number { + let parsed: unknown; + try { + parsed = JSON.parse(raw); + } catch { + return 0; + } + let mediaBytes = 0; + const isObj = (v: unknown): v is Record => typeof v === "object" && v !== null && !Array.isArray(v); + const bytesOf = (s: string): number => Buffer.byteLength(s); + const dataUrlBytes = (s: string): number => (s.startsWith("data:") ? bytesOf(s) : 0); + const walk = (v: unknown): void => { + if (Array.isArray(v)) { + for (const item of v) walk(item); + return; + } + if (!isObj(v)) return; + if (typeof v.data === "string" && (v.type === "base64" || v.type === "redacted_thinking")) mediaBytes += bytesOf(v.data); + if (typeof v.url === "string") mediaBytes += dataUrlBytes(v.url); + if (typeof v.file_data === "string") mediaBytes += dataUrlBytes(v.file_data); + // responses-wire input_image carries the whole data URL as a STRING + // under image_url (singular form) — same shape family as the object form. + if (typeof v.image_url === "string") mediaBytes += dataUrlBytes(v.image_url); + if (isObj(v.input_audio) && typeof v.input_audio.data === "string") mediaBytes += bytesOf(v.input_audio.data); + if (isObj(v.inlineData) && typeof v.inlineData.data === "string") mediaBytes += bytesOf(v.inlineData.data); + for (const child of Object.values(v)) walk(child); + }; + walk(parsed); + return mediaBytes; +} diff --git a/src/agent/opencode-native.ts b/src/agent/opencode-native.ts index b98da7c455..11fd4d33f0 100644 --- a/src/agent/opencode-native.ts +++ b/src/agent/opencode-native.ts @@ -82,6 +82,7 @@ import { createOpencodeV2Setup, type V2HttpRequestEvent, type V2State } from "./ import { asciiHeaderValue, fetchProxyVersion, postIdentityRegister, reportRuntimeInfoOnChange, waitForProxyVersion, type ForwardedToolResult } from "./shared.js"; import { callLegacyAcpConfig, isLegacyAcpSession, loadLegacyAcp, type LegacyAcpModule } from "./opencode-legacy.js"; import { logHostEvent } from "./host-log.js"; +import { mediaBytesOfWireBody } from "./media-bytes.js"; /** Decides whether the native bootstrap should run in this process. */ export function shouldBootstrapNativeOpencode(env: NodeJS.ProcessEnv): boolean { @@ -111,8 +112,8 @@ 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 + * model's context window — the core-text floor (bytes/4) exceeds the + * window, and #2924's ledger evidence says real text runs 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 @@ -124,24 +125,28 @@ async function oversizeDirectRefusal(e: V2HttpRequestEvent, s: V2State, reason?: 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; + let body: ArrayBuffer; try { - bytes = (await req.clone().arrayBuffer()).byteLength; + body = await req.clone().arrayBuffer(); } catch { return undefined; } - const floorTokens = Math.ceil(bytes / 4); + const bytes = body.byteLength; + if (Math.ceil(bytes / 4) <= window) return undefined; + // #4121: only re-price would-refuse bodies; inline media is not text. + const coreBytes = Math.max(bytes - mediaBytesOfWireBody(new TextDecoder().decode(body)), 0); + const floorTokens = Math.ceil(coreBytes / 4); if (floorTokens <= window) return undefined; // #3654: a busy-exhausted refusal must NOT claim "proxy unavailable … // guaranteed upstream 400" — the proxy IS up and would serve the request // once its event loop drains; the honest fact is that we stopped waiting. if (reason === "busy-exhausted") { return new Error( - `bili: proxy alive but unresponsive — it accepted connections but never answered its health probe within the wait budget (event loop saturated, typically a large compression in flight); this request cannot be sent uncompressed (body ~${floorTokens} tok (floor estimate ${bytes}B/4) exceeds the ${model.providerID}/${model.id} context window (${window})), so it is refused locally instead of queued indefinitely. The proxy is likely to serve it once the fold finishes (#3654); retry shortly.`, + `bili: proxy alive but unresponsive — it accepted connections but never answered its health probe within the wait budget (event loop saturated, typically a large compression in flight); this request cannot be sent uncompressed (body ~${floorTokens} tok (floor estimate ${coreBytes}B/4) exceeds the ${model.providerID}/${model.id} context window (${window})), so it is refused locally instead of queued indefinitely. The proxy is likely to serve it once the fold finishes (#3654); retry shortly.`, ); } 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.`, + `bili: proxy unavailable and this request cannot be sent uncompressed — body ~${floorTokens} tok (floor estimate ${coreBytes}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.`, ); } diff --git a/tests/opencode-native.test.ts b/tests/opencode-native.test.ts index 23f51b4647..ff757d30f8 100644 --- a/tests/opencode-native.test.ts +++ b/tests/opencode-native.test.ts @@ -443,6 +443,62 @@ test("route: proxy down + body provably over the model window throws a local ref assert.equal(await (e.request as Request).text(), body, "measuring through the clone leaves the original body sendable"); }); +for (const unavailable of ["dead", "busy-exhausted"] as const) { + test(`#4121 route: ${unavailable} excludes inline images from the direct-refusal text floor`, async (t) => { + const b64 = "A".repeat(8_000); + const dataUrl = `data:image/png;base64,${b64}`; + const bodies = { + openai: { messages: [{ role: "user", content: [{ type: "text", text: "Describe this image." }, { type: "image_url", image_url: { url: dataUrl } }] }] }, + responses: { input: [{ role: "user", content: [{ type: "input_text", text: "Describe this image." }, { type: "input_image", image_url: dataUrl }] }] }, + anthropic: { messages: [{ role: "user", content: [{ type: "tool_result", tool_use_id: "shot", content: [{ type: "image", source: { type: "base64", media_type: "image/png", data: b64 } }] }] }] }, + }; + for (const [wire, payload] of Object.entries(bodies)) { + await t.test(wire, async () => { + const state: NativeInterceptState = { origin: "http://127.0.0.1:1", ready: Promise.resolve(undefined) }; + const route = createNativeRoute(state, { probe: async () => ({ state: unavailable === "busy-exhausted" ? "busy" : "dead" }), busyWaitCapMs: 0 }); + const s: V2State = { windows: new Map([["prov/model-x", 1_000]]) }; + const body = JSON.stringify(payload); + assert.ok(Buffer.byteLength(body) / 4 > 1_000, "the raw-byte floor would refuse this image-heavy request"); + const e: V2HttpRequestEvent = { + model: { providerID: "prov", id: "model-x" }, + request: new Request(MODEL_URL, { method: "POST", body }), + }; + await route(e, s); + assert.equal(state.lastUnavailableReason, unavailable); + assert.equal((e.request as Request).url, MODEL_URL); + assert.equal(s.proxyBase, undefined); + assert.equal(await (e.request as Request).text(), body, "direct fallback preserves the original body"); + }); + } + }); + + test(`#4121 route: ${unavailable} still refuses over-window text with or without media`, async (t) => { + const dataUrl = `data:image/png;base64,${"A".repeat(8_000)}`; + const text = "x".repeat(5_000); + const bodies = { + text: JSON.stringify({ messages: [{ role: "user", content: text }] }), + mixed: JSON.stringify({ messages: [{ role: "user", content: [{ type: "text", text }, { type: "image_url", image_url: { url: dataUrl } }] }] }), + pasted_data_url: JSON.stringify({ messages: [{ role: "user", content: [{ type: "text", text: dataUrl }] }] }), + malformed: `{"messages":[${text}`, + }; + for (const [name, body] of Object.entries(bodies)) { + await t.test(name, async () => { + const state: NativeInterceptState = { origin: "http://127.0.0.1:1", ready: Promise.resolve(undefined) }; + const route = createNativeRoute(state, { probe: async () => ({ state: unavailable === "busy-exhausted" ? "busy" : "dead" }), busyWaitCapMs: 0 }); + const e: V2HttpRequestEvent = { + model: { providerID: "prov", id: "model-x" }, + request: new Request(MODEL_URL, { method: "POST", body }), + }; + const s: V2State = { windows: new Map([["prov/model-x", 1_000]]) }; + await assert.rejects(route(e, s), unavailable === "busy-exhausted" ? /proxy alive but unresponsive/ : /proxy unavailable/); + assert.equal(state.lastUnavailableReason, unavailable); + assert.equal((e.request as Request).url, MODEL_URL); + assert.equal(await (e.request as Request).text(), body); + }); + } + }); +} + 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 });