Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions reference/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/<entry>.js → dist/index.js resolution
│ │ ├── native-intercept.ts # Model-API fetch routing, attribution and proxy recovery
│ │ ├── fetch-chain/
Expand Down
49 changes: 2 additions & 47 deletions src/agent/dsh-native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"];
Expand Down Expand Up @@ -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<string, unknown> => 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
Expand Down
46 changes: 46 additions & 0 deletions src/agent/media-bytes.ts
Original file line number Diff line number Diff line change
@@ -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<string, unknown> => 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;
}
19 changes: 12 additions & 7 deletions src/agent/opencode-native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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.`,
);
}

Expand Down
56 changes: 56 additions & 0 deletions tests/opencode-native.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down
Loading