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
16 changes: 15 additions & 1 deletion src/agent/dsh-native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ import { ensureProxyRunning, LAUNCHER_DEFAULT_HOST } from "../launcher.js";
import { APIG_RESIGN_SCHEME, unresolvedRefusals } from "../apig-resign.js";
import { adminRelayEnabled, resolveResignSettings } from "../config.js";
import { markNativeHost, nativeAttachOrigin, nativeBootstrapGate, nativeProxyScriptPath, proxyEnvOrigin, singleFlight } from "./native-bootstrap.js";
import { installNativeFetchIntercept, liveStateSlotEffect, noteRoutedOrigin, observeRoutedOrigin, type GateVerdict, type NativeInterceptState } from "./native-intercept.js";
import { fetchMethodOf, installNativeFetchIntercept, liveStateSlotEffect, noteRoutedOrigin, observeRoutedOrigin, type GateVerdict, type NativeInterceptState } from "./native-intercept.js";
import { asciiHeaderValue, fetchManifest, fetchProxyVersion, fetchStatus, fetchStatusLatest, forwardTool, 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";
Expand Down Expand Up @@ -1628,6 +1628,9 @@ export function apply(ctx: PluginContext): void {
// pipeline — dropping it is a separate follow-up, not this PR.
const INC_PENDING_TTL_MS = 30_000; // matches omp-native / opencode-native
const incPendingAcks = new Map<string, { sid: string; url: string; messages: IncrementalWireMessage[]; system: unknown; base: string; at: number }>();
// #3304: original (body, headers) per swapped init — the give-up restore
// guard looks the init object back up by identity when the proxy dies.
const incOriginals = new WeakMap<object, { body: string; headers: Headers }>();
async function maybeIncrementalNative(url: string, init: RequestInit | undefined): Promise<void> {
if (!register.toolsReady || init === undefined) return;
const attr = attributionOf(ctx);
Expand Down Expand Up @@ -1664,6 +1667,7 @@ export function apply(ctx: PluginContext): void {
if (pending.url === url && Date.now() - pending.at < INC_PENDING_TTL_MS) { inflight = true; break; }
}
if (inflight) return;
incOriginals.set(init, { body, headers: new Headers(init.headers) });
init.body = (plan as { body: string }).body;
const turnId = newIncrementalTurnId();
const headers = new Headers(init.headers);
Expand All @@ -1672,6 +1676,16 @@ export function apply(ctx: PluginContext): void {
init.headers = headers;
incPendingAcks.set(turnId, { sid, url, messages: parsed.messages as IncrementalWireMessage[], system: "system" in parsed ? parsed.system : undefined, base, at: Date.now() });
}
// #3304: when the proxy dies and this request falls back to a direct
// send, restore the ORIGINAL payload instead of shipping the lane
// envelope a bare upstream cannot parse.
state.directGuard = (url, input, guardInit) => {
if (guardInit === undefined) return undefined;
const orig = incOriginals.get(guardInit);
if (orig === undefined) return undefined;
incOriginals.delete(guardInit);
return { input: url, init: { ...guardInit, method: fetchMethodOf(input, guardInit), body: orig.body, headers: orig.headers } };
};
state.onResponse = (_url, response, headers) => {
const turnId = headers?.[INCREMENTAL_TURN_ID_HEADER];
if (turnId === undefined) return; // not an incremental dispatch — nothing to attribute (#3329)
Expand Down
35 changes: 35 additions & 0 deletions src/agent/incremental-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,10 +291,45 @@ export function clearIncrementalProbe(proxyBase: string): void {
probeCache.delete(proxyBase);
}

// #3304: pi-lane original-payload ledger backing the give-up restore guard.
// The swap happens in the extension's before_provider_request handler while
// the guard runs in the bootstrap's fetch intercept — same bundle (the
// package.json "pi" manifest loads dist/agent/pi-native.js, which inlines
// pi.ts), so this module-level state is shared between them. Keyed per
// conversation id: pi sends strictly sequentially per session, and the
// writer clears the entry whenever a turn leaves WITHOUT a swap (or its
// response lands), so at fetch time an entry exists iff THE CURRENT request
// was swapped. TTL bounds memory for sessions whose response never comes.
const PI_ORIGINAL_TTL_MS = 30_000;
const piOriginals = new Map<string, { payload: unknown; at: number }>();

export function notePiIncrementalOriginal(conversationId: string, payload: unknown): void {
sweepStale(piOriginals, PI_ORIGINAL_TTL_MS);
piOriginals.set(conversationId, { payload, at: Date.now() });
}

export function clearPiIncrementalOriginal(conversationId: string): void {
piOriginals.delete(conversationId);
}

/** Consume (and forget) the original payload of the current swapped turn.
* Absent when nothing is pending or the entry expired. */
export function consumePiIncrementalOriginal(conversationId: string): unknown {
const entry = piOriginals.get(conversationId);
if (entry === undefined) return undefined;
if (Date.now() - entry.at >= PI_ORIGINAL_TTL_MS) {
piOriginals.delete(conversationId);
return undefined;
}
piOriginals.delete(conversationId);
return entry.payload;
}

export function __resetIncrementalClientForTests(): void {
convs.clear();
probeCache.clear();
rejectionCircuit.clear();
droppedParamWarned.clear();
droppedParamWarnPairs = 0;
piOriginals.clear();
}
95 changes: 92 additions & 3 deletions src/agent/native-intercept.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ import { APIG_RESIGN_HEADER, APIG_RESIGN_CREDENTIAL_HEADER, APIG_RESIGN_SCHEME,
* context window. */
export type GateVerdict = boolean | "refuse-oversized";

/** #3304: a (input, init) pair ready for a direct send — either the host's
* original pair or a lane-restored one carrying the ORIGINAL payload. */
type DirectSendPair = { input: string | URL | Request; init?: RequestInit };

/** #3304: directGuard verdicts — replace the outgoing pair, refuse locally,
* or leave it untouched (nothing was swapped). */
type DirectGuardVerdict = DirectSendPair | "refuse" | undefined;

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 @@ -63,6 +71,16 @@ export interface NativeInterceptState {
* (headersFor returns nothing) still pass through it — the hook itself
* gates on its own conditions. */
beforeSend?: (url: string, init: RequestInit | undefined) => Promise<void>;
/** #3304: consulted at EVERY exit that sends a model request DIRECT —
* proxy death (give-up), bootstrap failure, unattributed passthrough.
* Lane adapters arm it to restore the ORIGINAL payload when they
* swapped the outgoing body (incremental turns): a rewritten envelope
* must never leave the process toward a bare upstream. Return a
* replacement (input, init) pair, `"refuse"` for a local protocol-
* shaped refusal, or undefined when nothing was swapped. A throwing
* guard fails closed (local refusal) — it must never degrade to
* sending the possibly-rewritten body. */
directGuard?: (url: string, input: string | URL | Request, init?: RequestInit) => DirectGuardVerdict | Promise<DirectGuardVerdict>;
/** #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 @@ -276,8 +294,9 @@ function fetchUrlOf(input: string | URL | Request): string | undefined {

/** HTTP method of a fetch call, uppercased: init.method wins (spec: init
* overrides a Request-object input), then the Request object's own method,
* then the fetch default GET. */
function fetchMethodOf(input: string | URL | Request, init?: RequestInit): string {
* then the fetch default GET. Exported for #3304 directGuard restorers,
* which must rebuild the outgoing pair with the SAME effective method. */
export function fetchMethodOf(input: string | URL | Request, init?: RequestInit): string {
const m = init?.method;
if (typeof m === "string") return m.toUpperCase();
try {
Expand Down Expand Up @@ -409,6 +428,58 @@ function oversizeRefusal(url: string): Response {
return new Response(JSON.stringify({ error: { type: "server_error", code: "bili_gate_oversize_refusal", message, retryable: false } }), { status: 422, headers });
}

/** #3304: protocol-shaped local refusal for the give-up fallback when the
* outstanding request carries a lane payload that cannot be restored. 503
* retryable (transient infrastructure failure — the next turn re-plans
* against the respawned proxy), NOT 4xx (the request itself is fine; a 4xx
* would teach hosts the payload was wrong). */
function giveUpRestoreRefusal(url: string): Response {
const message =
"billion-context refused this request locally: the bili proxy died mid-session and this request carries an incremental payload that cannot be safely sent uncompressed — failing closed instead of shipping a rewritten body the upstream cannot parse (#3304). Retry the turn.";
const headers = { "content-type": "application/json", "x-bili-gate": "giveup-restore-refusal", "retry-after": "1" };
if (url.endsWith("/messages")) {
return new Response(JSON.stringify({ type: "error", error: { type: "api_error", message } }), { status: 503, headers });
}
return new Response(JSON.stringify({ error: { type: "server_error", code: "bili_gate_giveup_restore_refusal", message, retryable: true } }), { status: 503, headers });
}

/** #3304: the single choke point every exit that sends a model request
* DIRECT (proxy death, bootstrap failure, unattributed passthrough) must
* consult before sending. When a lane swapped the outgoing body
* (incremental turns), the ORIGINAL payload must travel instead of the
* rewritten envelope — or the request fails locally. Returns:
* - a Response: a local refusal (ship NOTHING);
* - a DirectSendPair: the caller must send exactly this pair;
* - undefined: nothing was swapped — the caller keeps its OWN default
* pair construction (each exit builds its target differently, e.g.
* makeTarget vs verbatim input), which stays byte-identical to the
* pre-fix behavior.
* A guard error fails closed too: an unknown state must never be resolved
* by shipping a possibly-rewritten body. */
/** "refuse" never leaves this helper — it becomes the local refusal
* Response — so callers only ever see pair / refusal / undefined. */
async function directGuardVerdict(
state: NativeInterceptState,
url: string,
input: string | URL | Request,
init?: RequestInit,
): Promise<Response | DirectSendPair | undefined> {
const guard = state.directGuard;
if (guard === undefined) return undefined;
let verdict: DirectGuardVerdict;
try {
verdict = await guard(url, input, init);
} catch (err) {
logHostEvent("error", "[native-client]", `bili-native: directGuard failed (${err instanceof Error ? err.message : String(err)}) — refusing instead of shipping a possibly-rewritten body (#3304)`);
verdict = "refuse";
}
if (verdict === "refuse") {
state.onDispatch?.(url, "refused");
return giveUpRestoreRefusal(url);
}
return verdict;
}

async function withTimeout<T>(p: Promise<T>, ms: number): Promise<T | undefined> {
let timer: ReturnType<typeof setTimeout> | undefined;
const timeout = new Promise<undefined>((resolve) => {
Expand Down Expand Up @@ -745,7 +816,10 @@ export function installNativeFetchIntercept(initialState: NativeInterceptState):
return oversizeRefusal(url);
}
if (verdict !== true) {
const stamped = withHeaders(input, init, { [BILI_PASSTHROUGH_HEADER]: "1" });
const gv = await directGuardVerdict(state, url, input, init);
if (gv instanceof Response) return gv;
const pair = gv ?? { input, init };
const stamped = withHeaders(pair.input, pair.init, { [BILI_PASSTHROUGH_HEADER]: "1" });
state.onDispatch?.(url, "direct");
return send(stamped.input, stamped.init);
}
Expand Down Expand Up @@ -830,7 +904,10 @@ export function installNativeFetchIntercept(initialState: NativeInterceptState):
warned = true;
logHostEvent("error", "[native-client]", `bili-native: no live proxy — model requests go direct (uncompressed): ${routedTarget}`);
}
const gv = await directGuardVerdict(state, routedTarget, input, init);
if (gv instanceof Response) return gv;
state.onDispatch?.(routedTarget, "direct");
if (gv !== undefined) return send(gv.input, gv.init);
return send(makeTarget(routedTarget), init);
}
}
Expand Down Expand Up @@ -887,8 +964,11 @@ export function installNativeFetchIntercept(initialState: NativeInterceptState):
return new Response(refusal.body, { status: refusal.status, headers: { "content-type": refusal.contentType, "x-bili-resign": "unavailable" } });
}
clearSignedRefusal(signedScheme);
const gv = await directGuardVerdict(state, url, input, init);
if (gv instanceof Response) return gv;
noteSignedDirect(url, signedScheme, "built-in passthrough opt-in (#1884) — byte-untouched, no compression");
state.onDispatch?.(url, "direct");
if (gv !== undefined) return send(gv.input, gv.init);
return send(input, init);
}
resignExtra = {
Expand Down Expand Up @@ -935,7 +1015,10 @@ export function installNativeFetchIntercept(initialState: NativeInterceptState):
state.onDispatch?.(url, "refused");
return oversizeRefusal(url);
}
const gv = await directGuardVerdict(state, url, input, init);
if (gv instanceof Response) return gv;
state.onDispatch?.(url, "direct");
if (gv !== undefined) return send(gv.input, gv.init);
return send(input, init);
}
}
Expand All @@ -948,7 +1031,10 @@ export function installNativeFetchIntercept(initialState: NativeInterceptState):
warned = true;
logHostEvent("error", "[native-client]", `bili-native: proxy not ready — model request goes direct (uncompressed): ${url}`);
}
const gv = await directGuardVerdict(state, url, input, init);
if (gv instanceof Response) return gv;
state.onDispatch?.(url, "direct");
if (gv !== undefined) return send(gv.input, gv.init);
return send(input, init);
}
// Attach mode rewrites exactly like spawn mode (#809 semantics —
Expand Down Expand Up @@ -1012,7 +1098,10 @@ export function installNativeFetchIntercept(initialState: NativeInterceptState):
warned = true;
logHostEvent("error", "[native-client]", `bili-native: proxy respawn failed — model requests go direct (uncompressed): ${url}`);
}
const gv = await directGuardVerdict(state, url, input, init);
if (gv instanceof Response) return gv;
state.onDispatch?.(url, "direct");
if (gv !== undefined) return send(gv.input, gv.init);
return send(input, init);
}
throw err;
Expand Down
23 changes: 22 additions & 1 deletion src/agent/omp-native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import { ensureProxyRunning, LAUNCHER_DEFAULT_HOST } from "../launcher.js";
import { createBiliPlugin } from "./pi.js";
import { applyOmpFirstEventTimeout, markNativeHost, nativeAttachOrigin, nativeBootstrapGate, nativeProxyScriptPath, proxyEnvOrigin, setNativeOriginWaiter, singleFlight } from "./native-bootstrap.js";
import { installNativeFetchIntercept, noteRoutedOrigin, observeRoutedOrigin, readyOrigin, type NativeInterceptState } from "./native-intercept.js";
import { fetchMethodOf, installNativeFetchIntercept, noteRoutedOrigin, observeRoutedOrigin, readyOrigin, type NativeInterceptState } from "./native-intercept.js";
import { clearIncrementalProbe, INCREMENTAL_TURN_MARKER_HEADER, INCREMENTAL_TURN_ID_HEADER, newIncrementalTurnId, noteIncrementalAck, noteIncrementalRejection, planIncrementalTurn, probeIncrementalTurns, sweepStale, wireMessagesLookAnthropic, type IncrementalWireMessage } from "./incremental-client.js";
import { fetchProxyVersion, waitForProxyVersion } from "./shared.js";
import { logHostEvent } from "./host-log.js";
Expand Down Expand Up @@ -104,6 +104,10 @@ const incPendingOmp = new Map<string, IncrementalPendingTurn>();
* (#3329): attribution is exact per turn id. Mirrors opencode. */
const INC_PENDING_TTL_MS = 30_000;

// #3304: original (body, headers) per swapped init — the give-up restore
// guard looks the init object back up by identity when the proxy dies.
const incOriginalsOmp = new WeakMap<object, { body: string; headers: Headers }>();

/** Test hook: drop all planned turns (module state across suites). */
export function __resetOmpIncrementalForTests(): void {
incPendingOmp.clear();
Expand Down Expand Up @@ -140,6 +144,7 @@ export async function ompIncrementalBeforeSend(url: string, init: RequestInit |
if (pending.url === url && Date.now() - pending.at < INC_PENDING_TTL_MS) { inflight = true; break; }
}
if (inflight) { return; }
incOriginalsOmp.set(init, { body: init.body, headers: new Headers(init.headers) });
init.body = plan.body;
const headers = new Headers(init.headers);
headers.set("x-bili-plugin", "omp");
Expand All @@ -156,6 +161,17 @@ export async function ompIncrementalBeforeSend(url: string, init: RequestInit |

state.beforeSend = ompIncrementalBeforeSend;

// #3304: when the proxy dies and this request falls back to a direct
// send, restore the ORIGINAL payload instead of shipping the lane
// envelope a bare upstream cannot parse.
state.directGuard = (url, input, guardInit) => {
if (guardInit === undefined) return undefined;
const orig = incOriginalsOmp.get(guardInit);
if (orig === undefined) return undefined;
incOriginalsOmp.delete(guardInit);
return { input: url, init: { ...guardInit, method: fetchMethodOf(input, guardInit), body: orig.body, headers: orig.headers } };
};

export function ompIncrementalOnResponse(_url: string, response: Response, headers?: Record<string, string>): void {
const turnId = headers?.[INCREMENTAL_TURN_ID_HEADER];
if (turnId === undefined) return; // not an incremental dispatch — nothing to attribute (#3329)
Expand Down Expand Up @@ -382,6 +398,11 @@ export function _stateRespawnForTest(): (() => Promise<string | undefined>) | un
return state.respawn;
}

/** Test hook (#3304): expose the armed give-up restore guard. */
export function _directGuardForTest(): NativeInterceptState["directGuard"] {
return state.directGuard;
}

/** Test hook (#1365): record routed-channel evidence without the fetch patch. */
export function _noteRoutedForTest(url: string): void {
noteRoutedOrigin(state, url);
Expand Down
Loading
Loading