Skip to content
Merged
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
11 changes: 9 additions & 2 deletions src/agent/dsh-native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ import { markNativeHost, nativeAttachOrigin, nativeBootstrapGate, nativeProxyScr
import { installNativeFetchIntercept, liveStateSlotEffect, noteRoutedOrigin, observeRoutedOrigin, type GateVerdict, type NativeInterceptState } from "./native-intercept.js";
import { asciiHeaderValue, fetchManifest, fetchProxyVersion, fetchStatus, fetchStatusLatest, forwardTool, reportAttributionHealth, reportRuntimeInfo, waitForProxyVersion, type ManifestTool } from "./shared.js";
import { createForkAdopter, sideShapedBody } from "./fork-adopt.js";
import { clearIncrementalProbe, INCREMENTAL_TURN_MARKER_HEADER, INCREMENTAL_TURN_ID_HEADER, newIncrementalTurnId, noteIncrementalRejection, noteIncrementalAck, planIncrementalTurn, probeIncrementalTurns, sweepStale, type IncrementalWireMessage } from "./incremental-client.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 { registerCompactionDisable } from "./dsh-compaction-disable.js";
export { sideShapedBody } from "./fork-adopt.js";

Expand Down Expand Up @@ -1762,6 +1762,9 @@ export function apply(ctx: PluginContext): void {
await maybeForkAdoptBeforeSend(ctx, init);
await maybeIncrementalNative(url, init);
};
// #3304: the dsh swap stamps the turn id atomically with the body, so the
// strict shared guard applies — mutated + unrestorable ⇒ refuse locally.
state.prepareDirectSend = createPrepareDirectSend();

// #3248 incremental turns (#3370 hardened): reference wiring for the
// native fetch lanes; opencode/omp-native follow the same recipe:
Expand Down Expand Up @@ -1826,8 +1829,12 @@ export function apply(ctx: PluginContext): void {
if (pending.url === url && Date.now() - pending.at < INC_PENDING_TTL_MS) { inflight = true; break; }
}
if (inflight) return;
init.body = (plan as { body: string }).body;
const turnId = newIncrementalTurnId();
// #3304: stash the pre-swap body under this turn's id — if the proxy
// dies before the send lands, the direct-send guard restores these
// bytes instead of forwarding the lane-only payload upstream.
rememberOriginalBody(turnId, body);
init.body = (plan as { body: string }).body;
const headers = new Headers(init.headers);
headers.set(INCREMENTAL_TURN_MARKER_HEADER, "1");
headers.set(INCREMENTAL_TURN_ID_HEADER, turnId);
Expand Down
75 changes: 75 additions & 0 deletions src/agent/incremental-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
// content ids server-side, so hosts without their own message ids work.

import { createHash } from "node:crypto";
import type { DirectSendPrep } from "./native-intercept.js";

/** Marker header the lane's divert recognizes. Client-side copy of
* INCREMENTAL_TURN_MARKER_HEADER in src/incremental-lane.ts — the host-side
Expand All @@ -42,6 +43,79 @@ export function newIncrementalTurnId(): string {
return `${Date.now().toString(36)}-${(turnIdCounter++).toString(36)}-${Math.random().toString(36).slice(2, 10)}`;
}

// #3304: originals stash for the direct-send guard. When an adapter swaps a
// request's body for the incremental payload, it stashes the ORIGINAL body
// string under that turn's id. If the proxy dies before the send lands,
// native-intercept's fall-back consults createPrepareDirectSend below and
// either restores these bytes (availability) or refuses the direct send
// (safety) — a lane-only payload never reaches a provider that cannot parse
// it. Entries age out by TTL (a single turn never outlives minutes) and are
// capped so a leak cannot grow the map.
const ORIGINAL_TTL_MS = 5 * 60_000;
const ORIGINAL_MAX_ENTRIES = 256;
const originalBodies = new Map<string, { body: string; at: number }>();

/** Stash the pre-swap body of one planned turn under its turn id. */
export function rememberOriginalBody(turnId: string, body: string): void {
const now = Date.now();
for (const [key, entry] of originalBodies) if (now - entry.at >= ORIGINAL_TTL_MS) originalBodies.delete(key);
if (originalBodies.size >= ORIGINAL_MAX_ENTRIES) {
let oldestKey: string | undefined;
let oldestAt = Number.POSITIVE_INFINITY;
for (const [key, entry] of originalBodies) {
if (entry.at < oldestAt) { oldestAt = entry.at; oldestKey = key; }
}
if (oldestKey !== undefined) originalBodies.delete(oldestKey);
}
originalBodies.set(turnId, { body, at: now });
}

/** Non-consuming lookup: a host-level retry re-dispatches the SAME request
* (same turn id) and must get the original back again, so entries are kept
* until TTL instead of being deleted on read. */
export function originalBodyForTurn(turnId: string): string | null {
const entry = originalBodies.get(turnId);
if (entry === undefined || Date.now() - entry.at >= ORIGINAL_TTL_MS) return null;
return entry.body;
}

/** #3304: prepareDirectSend implementation for lanes that stamp the turn id
* ATOMICALLY with the body swap (dsh / omp / opencode-native): presence of
* x-bili-turn-id on the outgoing request proves the body was replaced, so
* a missing/expired original means "mutated but unrecoverable" → refuse
* rather than risk sending lane-only bytes upstream. Lanes without the hook
* (every non-incremental traffic) keep today's byte-identical behavior. */
export function createPrepareDirectSend(): (url: string, input: string | URL | Request, init: RequestInit | undefined) => Promise<DirectSendPrep> {
return async (_url, input, init) => {
let turnId: string | null = null;
try {
const src = (init?.headers ?? (input instanceof Request ? input.headers : undefined)) as ConstructorParameters<typeof Headers>[0] | undefined;
if (src !== undefined) turnId = new Headers(src).get(INCREMENTAL_TURN_ID_HEADER);
} catch {
turnId = null;
}
if (turnId === null) return { outcome: "as-is" };
const body = originalBodyForTurn(turnId);
return body === null ? { outcome: "unrestorable" } : { outcome: "restored", body };
};
}

/** #3304: is this parsed body an incremental envelope — i.e. WOULD the
* lane's marker divert claim it? Mirrors incrementalTurnBodyShape in
* src/incremental-lane.ts (keep in sync): such a body is lane-only, so it
* must never be forwarded to the real upstream as-is. Returns the
* conversationId when it matches (pi correlates its stash through it),
* undefined otherwise. */
export function incrementalEnvelopeConversationId(body: unknown): string | undefined {
if (body === null || typeof body !== "object" || Array.isArray(body)) return undefined;
const b = body as Record<string, unknown>;
if (typeof b.conversationId !== "string" || b.conversationId.length === 0) return undefined;
if (typeof b.sinceSeq !== "number" || !Number.isInteger(b.sinceSeq) || b.sinceSeq < 0) return undefined;
if (!Array.isArray(b.messages)) return undefined;
if (b.model !== undefined) return undefined;
return b.conversationId;
}

/** #3329: drop pending entries older than `ttlMs` — a planned turn whose
* response never comes (network-dead request) must not pin its slot forever.
* Memory hygiene only since attribution is exact per request: correctness no
Expand Down Expand Up @@ -297,4 +371,5 @@ export function __resetIncrementalClientForTests(): void {
rejectionCircuit.clear();
droppedParamWarned.clear();
droppedParamWarnPairs = 0;
originalBodies.clear();
}
103 changes: 90 additions & 13 deletions src/agent/native-intercept.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ import { APIG_RESIGN_HEADER, APIG_RESIGN_CREDENTIAL_HEADER, APIG_RESIGN_SCHEME,
* context window. */
export type GateVerdict = boolean | "refuse-oversized";

/** #3304: prepareDirectSend's verdict for one fall-back send to the real
* upstream (see the state field). */
export type DirectSendPrep =
| { outcome: "as-is" }
| { outcome: "restored"; body: string }
| { outcome: "unrestorable" };

export interface NativeInterceptState {
/** Proxy origin ("http://127.0.0.1:PORT") once the bootstrap resolved.
* Written by the owner (pi-native.ts); read synchronously on each call. */
Expand Down Expand Up @@ -62,7 +69,22 @@ export interface NativeInterceptState {
* the request init (body) and may spend async time; wire-mode requests
* (headersFor returns nothing) still pass through it — the hook itself
* gates on its own conditions. */
beforeSend?: (url: string, init: RequestInit | undefined) => Promise<void>;
beforeSend?: (url: string, init: RequestInit | undefined) => Promise<void>;
/** #3304: consulted immediately before ANY fall-back send to the REAL
* upstream (proxy-death give-up, bootstrap-fail, unattributed direct,
* signed passthrough). Lanes that mutate the outgoing payload (the
* incremental-turn adapters) arm it; every other lane leaves it
* undefined and each fall-back behaves byte-identically to pre-#3304.
* The hook sees the SAME (input, init) pair the fall-back would send
* (headers/body already mutated by the lane's own swap) and answers:
* - "as-is" — the payload is a standalone request → send it;
* - "restored" — send `body` INSTEAD (the pre-mutation original);
* - "unrestorable" — the payload was mutated and cannot be undone →
* the direct send is refused locally (TypeError) so
* a lane-only body never reaches a provider that
* cannot parse it; the host's retry re-runs the full
* decision (re-probe, re-plan) from scratch. */
prepareDirectSend?: (url: string, input: string | URL | Request, init: RequestInit | undefined) => DirectSendPrep | Promise<DirectSendPrep>;
/** #3248 incremental turns: response hook — fired with the routed
* Response BEFORE it is returned to the host, so response headers are
* readable without consuming the stream. Native hosts use it to read
Expand Down Expand Up @@ -646,6 +668,26 @@ export function replaceRequestPayload(e: { request?: unknown }, body: string, ex
}
}

/** #3304: rebuild a fall-back dispatch carrying `body` INSTEAD of whatever
* `init`/the Request carried. The restored body rides INIT so fetch-spec
* precedence (init over Request fields) stays exactly what the original
* dispatch had; a string body needs no duplex. */
export function withRestoredBody(input: string | URL | Request, init: RequestInit | undefined, body: string): { input: string | URL | Request; init: RequestInit | undefined } {
if (input instanceof Request) {
try {
const plain = new Request(input.url, { method: input.method, headers: input.headers });
return { input: plain, init: { ...(init ?? {}), body } };
} catch {
const headers: Record<string, string> = {};
try {
for (const [k, v] of input.headers) headers[k] = v;
} catch {}
return { input: input.url, init: { ...(init ?? {}), method: input.method, headers, body } };
}
}
return { input, init: { ...(init ?? {}), body } };
}

/** Re-point the process-level live-state slot at the activating instance and
* advance its generation, so every transport (fetch + Codex Responses WS)
* follows the newest applied module instance (#3394/#3558). Called at the TOP
Expand Down Expand Up @@ -737,6 +779,47 @@ export function installNativeFetchIntercept(initialState: NativeInterceptState):
return again;
};

// #3304: ask the lane whether this fall-back payload may go to the
// real upstream AS-IS. Fail-safe direction: an armed hook that
// throws is treated as "unrestorable" — never risk sending bytes the
// lane may have mutated.
const prepareForDirectSend = async (url: string, input: string | URL | Request, init: RequestInit | undefined): Promise<DirectSendPrep> => {
const hook = state.prepareDirectSend;
if (hook === undefined) return { outcome: "as-is" };
try {
return await hook(url, input, init);
} catch (err) {
logHostEvent("error", "[native-client]", `bili-native: prepareDirectSend hook failed (${err instanceof Error ? err.message : String(err)}) — refusing the direct send (#3304)`);
return { outcome: "unrestorable" };
}
};

// #3304: the guarded fall-back send. `defaultInput` is what the site
// would send without the guard (usually `input`; makeTarget(routedTarget)
// on the routed give-up). Restored bodies ride INIT so fetch-spec
// precedence (init over Request) stays exactly what the original
// dispatch had.
const directSendOrRefuse = async (dispatchUrl: string, defaultInput: string | URL | Request, init: RequestInit | undefined): Promise<Response> => {
const prep = await prepareForDirectSend(dispatchUrl, defaultInput, init);
if (prep.outcome === "as-is") {
state.onDispatch?.(dispatchUrl, "direct");
return send(defaultInput, init);
}
if (prep.outcome === "unrestorable") {
// A lane-only payload (e.g. an incremental delta) cannot be
// forwarded to a provider that cannot parse it — refusing
// locally keeps the host's retry loop driving the decision
// again (re-probe / re-plan) instead of burning the turn on
// an upstream 400.
state.onDispatch?.(dispatchUrl, "refused");
logHostEvent("error", "[native-client]", `bili-native: no usable proxy and the request carries a lane-only payload — failing locally instead of sending it to the provider; retry the turn (#3304): ${dispatchUrl}`);
throw new TypeError("billion-context: proxy unavailable — the request payload is lane-specific (incremental turn) and cannot be forwarded to the provider directly; retry the turn (#3304)");
}
const restored = withRestoredBody(defaultInput, init, prep.body);
state.onDispatch?.(dispatchUrl, "direct");
return send(restored.input, restored.init);
};

// Already-routed `/bili/` model requests (launcher settings overlay):
// routing is done, but plugin headers still decide wire vs plugin
// mode — stamp and pass through untouched otherwise.
Expand All @@ -762,8 +845,7 @@ export function installNativeFetchIntercept(initialState: NativeInterceptState):
}
if (verdict !== true) {
const stamped = withHeaders(input, init, { [BILI_PASSTHROUGH_HEADER]: "1" });
state.onDispatch?.(url, "direct");
return send(stamped.input, stamped.init);
return directSendOrRefuse(url, stamped.input, stamped.init);
}
}
// #1365: attributed routed traffic pins this process's model channel
Expand Down Expand Up @@ -846,8 +928,7 @@ export function installNativeFetchIntercept(initialState: NativeInterceptState):
warned = true;
logHostEvent("error", "[native-client]", `bili-native: no live proxy — model requests go direct (uncompressed): ${routedTarget}`);
}
state.onDispatch?.(routedTarget, "direct");
return send(makeTarget(routedTarget), init);
return directSendOrRefuse(routedTarget, makeTarget(routedTarget), init);
}
}
if (!isModelApiUrl(url)) {
Expand Down Expand Up @@ -904,8 +985,7 @@ export function installNativeFetchIntercept(initialState: NativeInterceptState):
}
clearSignedRefusal(signedScheme);
noteSignedDirect(url, signedScheme, "built-in passthrough opt-in (#1884) — byte-untouched, no compression");
state.onDispatch?.(url, "direct");
return send(input, init);
return directSendOrRefuse(url, input, init);
}
resignExtra = {
[APIG_RESIGN_HEADER]: APIG_RESIGN_SCHEME,
Expand Down Expand Up @@ -951,8 +1031,7 @@ export function installNativeFetchIntercept(initialState: NativeInterceptState):
state.onDispatch?.(url, "refused");
return oversizeRefusal(url);
}
state.onDispatch?.(url, "direct");
return send(input, init);
return directSendOrRefuse(url, input, init);
}
}

Expand All @@ -964,8 +1043,7 @@ export function installNativeFetchIntercept(initialState: NativeInterceptState):
warned = true;
logHostEvent("error", "[native-client]", `bili-native: proxy not ready — model request goes direct (uncompressed): ${url}`);
}
state.onDispatch?.(url, "direct");
return send(input, init);
return directSendOrRefuse(url, input, init);
}
// Attach mode rewrites exactly like spawn mode (#809 semantics —
// opencode's attach probe+rewrite; the V1 fetch patch relies on it to
Expand Down Expand Up @@ -1028,8 +1106,7 @@ export function installNativeFetchIntercept(initialState: NativeInterceptState):
warned = true;
logHostEvent("error", "[native-client]", `bili-native: proxy respawn failed — model requests go direct (uncompressed): ${url}`);
}
state.onDispatch?.(url, "direct");
return send(input, init);
return directSendOrRefuse(url, input, init);
}
throw err;
}
Expand Down
Loading
Loading