Skip to content
Open
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
80 changes: 75 additions & 5 deletions src/agent/fetch-chain/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,56 @@ function isOwnChain(v: unknown): boolean {
return typeof v === "function" && Object.prototype.hasOwnProperty.call(v, CHAIN_MARKER);
}

/** #6829: pi-web-access's marker for "a proxy fetch is installed". 0.45.x set
* `__piWebAccessProxyFetch` unconditionally; from 0.46.0 it only propagates
* that flag when the wrapped fetch already has it, and marks its own wrapper
* with `__piWebAccessSharedProxyFetch` instead. Recognize BOTH names, or the
* #2435 coexistence branch is unreachable for 0.46+ installs. */
function isPiWebAccessFetch(v: unknown): boolean {
return typeof v === "function" && (Reflect.get(v, "__piWebAccessProxyFetch") === true || Reflect.get(v, "__piWebAccessSharedProxyFetch") === true);
}

/** #6829: pseudo-origin served in-process by the inline engine. Owned by
* src/inline-engine.ts (`INLINE_ORIGIN`); duplicated here because the chain
* module must stay free of the server graph (inline-engine pulls in
* server.js, which would cycle back through the agent lanes). If the origin
* ever changes, update both. */
const INLINE_ORIGIN = "http://billion-context.inline";

const INLINE_HOSTNAME = "billion-context.inline";

/** Exact-origin classifier for the inline pseudo-origin. A prefix match
* would also match lookalikes (billion-context.inline.evil.com), an https
* URL, a non-default port, or a URL carrying credentials — none of which is
* the inline engine. The hostname comparison is case-insensitive (URL
* normalises it), the scheme must be plain http, and only the default
* port (:80, or no port) counts. */
export function isInlineOriginUrl(url: string): boolean {
let parsed: URL;
try {
parsed = new URL(url);
} catch {
return false;
}
return (
parsed.protocol === "http:" &&
parsed.hostname === INLINE_HOSTNAME &&
(parsed.port === "" || parsed.port === "80") &&
parsed.username === "" &&
parsed.password === ""
);
}

function fetchUrlOf(input: string | URL | Request): string | undefined {
try {
if (typeof input === "string") return input;
if (input instanceof URL) return input.href;
return input.url;
} catch {
return undefined;
}
}

/** #1158 escape hatch: `BILI_RECLAIM_FETCH_PATCH=0` keeps the classic direct
* install — a third-party re-arm (dsh-http-proxy refresh) then wins and
* bili stops seeing model traffic (documented degradation, visible instead
Expand Down Expand Up @@ -147,9 +197,10 @@ export function installFetchChain(makeDispatch: (send: typeof globalThis.fetch)
// Windows). The depth check below is the proof of such nesting: an
// OUR link called from within our own dispatch can only be reached
// through a foreign wrapper that captured one of our older tops.
// A recognized pi-web-access link still owns proxy routing even after
// bili dispatched, so retain it at depth 1. Its older bili links must
// still be able to truncate unknown wrapper history.
// A recognized pi-web-access link (either marker, #6829) still owns
// proxy routing even after bili dispatched, so retain it at depth 1.
// Its older bili links must still be able to truncate unknown wrapper
// history.
// ponytail: unknown wrappers retain the #1662 cutoff; preserving their
// deeper hooks requires a shared chain protocol.
const patched = async (input: string | URL | Request, init?: RequestInit): Promise<Response> => {
Expand All @@ -161,9 +212,22 @@ export function installFetchChain(makeDispatch: (send: typeof globalThis.fetch)
if (chainHop.getStore() === patched) {
return dispatchDepth.run((dispatchDepth.getStore() ?? 0) + 1, () => dispatchBody(input, init));
}
// #6841 rework: classify FIRST. An INLINE_ORIGIN request is served
// in-process at a fresh depth 1 instead of being handed down the
// chain, so it can never reach a foreign wrapper sitting between
// this link and the inline link (the pi-web-access wrapper with a
// proxy scope on curls it before the #1662 depth-1 fix ever runs).
// It cannot loop: once served, the request is never sent down the
// chain again, so it terminates here.
if (isInlineOriginUrl(fetchUrlOf(input) ?? "")) {
return dispatchDepth.run(1, () => dispatchBody(input, init));
}
const depth = dispatchDepth.getStore();
if (depth === 1) {
if (Reflect.get(ds, "__piWebAccessProxyFetch") === true) return send(input, init);
if (isPiWebAccessFetch(ds)) return send(input, init);
// #6841 rework: INLINE_ORIGIN requests never reach this branch
// any more — the classify-first check above serves them
// in-process instead, so the old prefix route-down is gone.
const anchor = observedFetches.find((f) => !knownDeadFetches.has(f));
if (anchor !== undefined && anchor !== ds) {
if (!warnedReentry) {
Expand All @@ -184,6 +248,12 @@ export function installFetchChain(makeDispatch: (send: typeof globalThis.fetch)
Object.defineProperty(chain, "__piWebAccessProxyFetch", {
get: () => Reflect.get(ds, "__piWebAccessProxyFetch"),
});
// #6829: forward the 0.46+ shared marker too, so pi-web-access's own
// install guard can see through the chain that a proxy is already in
// place and does not re-install on every web tool call.
Object.defineProperty(chain, "__piWebAccessSharedProxyFetch", {
get: () => Reflect.get(ds, "__piWebAccessSharedProxyFetch"),
});
return chain;
};

Expand Down Expand Up @@ -223,7 +293,7 @@ export function installFetchChain(makeDispatch: (send: typeof globalThis.fetch)
// leaves; "evict" was the pre-self-heal framing and is dropped
// from the wording. Unknown wrappers and any LATER (repeat /
// abnormal) marked install keep the full diagnostic below.
const webAccess = Reflect.get(v, "__piWebAccessProxyFetch") === true;
const webAccess = isPiWebAccessFetch(v);
// Visibility (#1158): a third-party install used to be silent —
// log it so "un-routed by a third party" is diagnosable. Past
// REARM_LIMIT the WARNING stops (log spam), but the adoption
Expand Down
23 changes: 21 additions & 2 deletions src/agent/native-intercept.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
// at extension load always wins. The patch is surgical — it rewrites ONLY
// model-API shaped URLs and leaves every other request untouched.

import { installFetchChain, isDeadClosureError, resetFetchChainForTest } from "./fetch-chain/index.js";
import { installFetchChain, isDeadClosureError, isInlineOriginUrl, resetFetchChainForTest } from "./fetch-chain/index.js";
import { logHostEvent } from "./host-log.js";
import { serveInlineOrigin } from "./pi-inline.js";

import { envMillis } from "./native-bootstrap.js";
import { matchModelEndpoint, type ModelEndpointPattern } from "../model-endpoints.js";
Expand Down Expand Up @@ -1568,7 +1569,25 @@ export function installNativeFetchIntercept(initialState: NativeInterceptState):
// network again. Only ever contains origins we ourselves observed dying.
const replacedOrigins = new Set<string>();

installFetchChain((send) => async (input: string | URL | Request, init?: RequestInit): Promise<Response> => {
installFetchChain((chainSend) => async (input: string | URL | Request, init?: RequestInit): Promise<Response> => {
// #6841 rework: never hand pseudo-origin traffic to a downstream
// that may be a foreign wrapper. The pi-web-access wrapper sits
// ABOVE the inline link; with its proxy scope on it curls an
// INLINE_ORIGIN URL before bili's chain ever sees it, so the #1662
// depth-1 fix never runs on that path. Bili cannot observe the
// wrapper's scope state from outside, so the rule is unconditional:
// every INLINE_ORIGIN send from this dispatch is served in-process
// by the inline transport instead. Non-inline sends pass through
// untouched, and when the inline transport was never installed the
// serve returns undefined and the send goes down the chain as today.
const send = async (input: string | URL | Request, init?: RequestInit): Promise<Response> => {
const url = fetchUrlOf(input);
if (url !== undefined && isInlineOriginUrl(url)) {
const served = await serveInlineOrigin(input, init);
if (served !== undefined) return served;
}
return chainSend(input, init);
};
const url = fetchUrlOf(input);
// #3394: resolve the ACTIVE state per request instead of capturing the
// first installer's — a hot-reimport swapped in a fresh instance whose
Expand Down
88 changes: 53 additions & 35 deletions src/agent/pi-inline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// through its loopback-gated admin routes. The fetch captured below the link
// is the host's PRE-PATCH fetch — handed to the engine as its upstream
// transport so pipeline egress never re-enters the chain.
import { installFetchChain } from "./fetch-chain/index.js";
import { installFetchChain, isInlineOriginUrl } from "./fetch-chain/index.js";
import { getInlineEngine, INLINE_ORIGIN, type InlineEngine } from "../inline-engine.js";
import { logHostEvent } from "./host-log.js";

Expand All @@ -26,6 +26,55 @@ function urlOf(input: string | URL | Request): string | undefined {
}
}

/** Serve an INLINE_ORIGIN request in-process via the inline engine, using
* the pre-patch fetch captured at install as the engine's upstream
* transport. Returns undefined when the inline transport was never
* installed, or when the URL is not addressed to the inline origin — the
* caller then falls back to the chain send (today's behavior). The native
* intercept calls this for the INLINE_ORIGIN targets it produces, so a
* foreign wrapper sitting between the intercept and this link can never see
* (and, with a pi-web-access proxy scope active, curl) pseudo-origin
* traffic (#6841 rework). */
export async function serveInlineOrigin(input: string | URL | Request, init?: RequestInit): Promise<Response | undefined> {
if (!installed) return undefined;
const url = urlOf(input);
if (url === undefined || !isInlineOriginUrl(url)) return undefined;
const upstream = rawSend;
if (upstream === undefined) return undefined;
const signal = init?.signal !== undefined ? init.signal : input instanceof Request ? input.signal : undefined;
signal?.throwIfAborted();
try {
const engine = await getInlineEngine();
signal?.throwIfAborted();
return await engine.dispatch(input, init, upstream);
} catch (err) {
// Caller-side cancel is not an engine failure — propagate it:
// degrading would consume the one-shot warning and re-send a
// request the caller already abandoned.
signal?.throwIfAborted();
if (err instanceof Error && err.name === "AbortError") throw err;
const msg = err instanceof Error ? err.message : String(err);
if (url.startsWith(MODEL_PREFIX)) {
// Startup or dispatch failed: try the direct fallback by
// stripping the pseudo-origin prefix (the embedded
// upstream URL is absolute) and send direct, uncompressed.
if (!degradeWarned) {
degradeWarned = true;
logHostEvent("error", "[native-client]", `[bili-native] inline request failed — attempting direct model fallback (uncompressed): ${msg}`);
}
const target = url.slice(MODEL_PREFIX.length);
if (input instanceof Request) {
const direct = new Request(target, new Request(input, init));
// Keep transport-specific init fields without serializing the body twice
// (a second FormData serialization would choose a different boundary).
return await upstream(direct, init && { ...init, headers: undefined, body: undefined });
}
return await upstream(target, init);
}
throw err;
}
}

/** Install the inline transport as the innermost link of the fetch chain.
* Must run before installNativeFetchIntercept (chain order = install order). */
export function installInlineTransport(): boolean {
Expand All @@ -34,40 +83,9 @@ export function installInlineTransport(): boolean {
installed = true;
rawSend = send;
return async (input: string | URL | Request, init?: RequestInit): Promise<Response> => {
const url = urlOf(input);
if (url === undefined || !url.startsWith(INLINE_ORIGIN)) return send(input, init);
const signal = init?.signal !== undefined ? init.signal : input instanceof Request ? input.signal : undefined;
signal?.throwIfAborted();
try {
const engine = await getInlineEngine();
signal?.throwIfAborted();
return await engine.dispatch(input, init, rawSend);
} catch (err) {
// Caller-side cancel is not an engine failure — propagate it:
// degrading would consume the one-shot warning and re-send a
// request the caller already abandoned.
signal?.throwIfAborted();
if (err instanceof Error && err.name === "AbortError") throw err;
const msg = err instanceof Error ? err.message : String(err);
if (url.startsWith(MODEL_PREFIX)) {
// Startup or dispatch failed: try the direct fallback by
// stripping the pseudo-origin prefix (the embedded
// upstream URL is absolute) and send direct, uncompressed.
if (!degradeWarned) {
degradeWarned = true;
logHostEvent("error", "[native-client]", `[bili-native] inline request failed — attempting direct model fallback (uncompressed): ${msg}`);
}
const target = url.slice(MODEL_PREFIX.length);
if (input instanceof Request) {
const direct = new Request(target, new Request(input, init));
// Keep transport-specific init fields without serializing the body twice
// (a second FormData serialization would choose a different boundary).
return await send(direct, init && { ...init, headers: undefined, body: undefined });
}
return await send(target, init);
}
throw err;
}
const served = await serveInlineOrigin(input, init);
if (served !== undefined) return served;
return send(input, init);
};
});
return true;
Expand Down
31 changes: 31 additions & 0 deletions tests/native-intercept-selfheal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,37 @@ test("#2827: first web-access adoption writes nothing to the host terminal and r
assert.deepEqual(nativeSink, ["http://127.0.0.1:40001/bili/https://api.example.invalid/v1/messages"]);
});

// #6829: pi-web-access 0.46+ marks its wrapper with the SHARED flag only
// (`__piWebAccessSharedProxyFetch`) — the old flag propagates only when the
// wrapped fetch already has it. The guard must recognize the shared marker
// as the same supported coexistence handshake (#2435), not an unknown
// third-party install, and the marker must be visible through the re-chained
// top so the wrapper's own install guard does not re-install.
test("#6829: a shared-marker pi-web-access install is quietly adopted as coexistence", async () => {
const nativeSink: string[] = [];
const { warns, logs, errors, out, err, lines } = await withDiagnosticsCapture(async () => {
await withScope(fakeFetch(nativeSink), async () => {
installNativeFetchIntercept({ origin: "http://127.0.0.1:40001", ready: Promise.resolve("http://127.0.0.1:40001") });
const base = globalThis.fetch;
const wrapper = (async (input: string | URL | Request, init?: RequestInit) => base(input, init)) as typeof fetch;
Object.defineProperty(wrapper, "__piWebAccessSharedProxyFetch", { value: true });
globalThis.fetch = wrapper;
// Marker visible through the re-chained top (forwarded, not lost).
assert.equal(Reflect.get(globalThis.fetch, "__piWebAccessSharedProxyFetch"), true);
const res = await globalThis.fetch("https://api.example.invalid/v1/messages", { method: "POST" });
assert.equal(res.status, 200);
});
});
assert.equal(warns.length, 0);
assert.equal(logs.length, 0);
assert.equal(errors.length, 0);
assertNoTerminalBili(out, err);
const diags = lines.filter(l => l.includes("[native-client]"));
assert.equal(diags.length, 1);
assert.match(diags[0], /\[info\] \[v=[^\]]+\] \[native-client\] \[bili-native\] adopted pi-web-access proxy fetch as downstream \(supported coexistence, #2435\)/);
assert.deepEqual(nativeSink, ["http://127.0.0.1:40001/bili/https://api.example.invalid/v1/messages"]);
});

// #5907: the no-guard postures (env escape hatch / non-configurable host
// property) are exactly where a third-party replacement silently un-routes
// model traffic — pin that each one names the degraded posture in bili.log
Expand Down
Loading
Loading