From 8895a2ca7a65b09970641aced443a5062aefd507 Mon Sep 17 00:00:00 2001 From: Saim Shafique Date: Sun, 15 Nov 2026 14:35:05 +0000 Subject: [PATCH 1/2] fix: recognize pi-web-access 0.46 shared proxy marker and keep inline traffic off real DNS pi-web-access 0.46 only sets __piWebAccessProxyFetch when the wrapped fetch already has it, and marks its own wrapper with __piWebAccessSharedProxyFetch instead. The fetch chain only knew the old name, so the #2435 coexistence branch was unreachable and every install fell into the generic re-chain. The #1662 escape then handed the rewritten INLINE_ORIGIN request to the oldest live anchor, a real DNS lookup that dies ENOTFOUND, and only the first occurrence is logged, so every later model call failed silently. Recognize both markers in the depth-1 branch, the install guard, and the marker the chain forwards, and never route pseudo-origin traffic to the anchor: send it down the chain instead, where the hop stamp lets the inline transport serve it in-process. --- src/agent/fetch-chain/index.ts | 51 +++++++++++++++-- tests/native-intercept-selfheal.test.ts | 31 ++++++++++ tests/pi-inline.test.ts | 76 +++++++++++++++++++++++++ 3 files changed, 153 insertions(+), 5 deletions(-) diff --git a/src/agent/fetch-chain/index.ts b/src/agent/fetch-chain/index.ts index b516b10e1b..8fcf07ecc7 100644 --- a/src/agent/fetch-chain/index.ts +++ b/src/agent/fetch-chain/index.ts @@ -15,6 +15,32 @@ 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"; + +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 @@ -147,9 +173,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 => { @@ -163,7 +190,15 @@ export function installFetchChain(makeDispatch: (send: typeof globalThis.fetch) } const depth = dispatchDepth.getStore(); if (depth === 1) { - if (Reflect.get(ds, "__piWebAccessProxyFetch") === true) return send(input, init); + if (isPiWebAccessFetch(ds)) return send(input, init); + // #6829: the #1662 escape must never hand pseudo-origin traffic + // to the oldest live anchor — billion-context.inline has no DNS + // record, so the request dies ENOTFOUND instead of reaching the + // inline engine. Route it down the chain instead: send() stamps + // the hop, so the inline transport link recognizes the descent + // (#4527) and serves it in-process. + const url = fetchUrlOf(input); + if (url !== undefined && url.startsWith(INLINE_ORIGIN)) return send(input, init); const anchor = observedFetches.find((f) => !knownDeadFetches.has(f)); if (anchor !== undefined && anchor !== ds) { if (!warnedReentry) { @@ -184,6 +219,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; }; @@ -223,7 +264,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 diff --git a/tests/native-intercept-selfheal.test.ts b/tests/native-intercept-selfheal.test.ts index 155ecc3652..f35732ba63 100644 --- a/tests/native-intercept-selfheal.test.ts +++ b/tests/native-intercept-selfheal.test.ts @@ -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 diff --git a/tests/pi-inline.test.ts b/tests/pi-inline.test.ts index 00b175a4ff..b4db57f582 100644 --- a/tests/pi-inline.test.ts +++ b/tests/pi-inline.test.ts @@ -204,6 +204,82 @@ test("#4527 composition: native intercept + inline link stacked like the real ar } }); +test("#6829: pi-web-access 0.46+ wrapper (shared marker only) keeps model traffic on the inline engine", async () => { + // The production arm (pi-native.ts) installs the inline transport FIRST, + // then the native fetch intercept ON TOP. From pi-web-access 0.46.0 a web + // tool call wraps the intercept link with a wrapper that carries ONLY the + // new `__piWebAccessSharedProxyFetch` marker (the old + // `__piWebAccessProxyFetch` propagates only when the wrapped fetch already + // has it, which bili's chain never sets from native). Pre-fix the wrapper + // fell into the generic re-chain, then the #1662 escape handed the + // rewritten INLINE_ORIGIN request to the oldest live anchor — a real DNS + // lookup of billion-context.inline that dies ENOTFOUND, and only the first + // occurrence is logged, so every later model call failed silently. + const ie = await import("../src/inline-engine.ts"); + const ni = await import("../src/agent/native-intercept.ts"); + const fc = await import("../src/agent/fetch-chain/index.ts"); + const piInline = await import("../src/agent/pi-inline.ts"); + const calls: string[] = []; + ie._setEngineFactoryForTest(async () => ({ + origin: ie.INLINE_ORIGIN, + dispatch: async (input: string | URL | Request): Promise => { + calls.push(String(input)); + return new Response("inline-ok"); + }, + dispatchHttp: async () => {}, + wsOrigin: Promise.resolve(undefined), + webOrigin: Promise.resolve(undefined), + })); + try { + ni._resetForTest(); + fc.resetFetchChainForTest(); + piInline._resetInlineTransportForTest(); + piInline.installInlineTransport(); + const state: import("../src/agent/native-intercept.ts").NativeInterceptState = { origin: ie.INLINE_ORIGIN, ready: Promise.resolve(ie.INLINE_ORIGIN) }; + assert.equal(ni.installNativeFetchIntercept(state), true); + // 0.46+ installGlobalProxyFetch shape: wraps the current top and marks + // ONLY the shared flag; passes everything else through to its base. + const seenByWrapper: string[] = []; + const base = globalThis.fetch; + const wrapper = (async (input: string | URL | Request, init?: RequestInit) => { + seenByWrapper.push(typeof input === "string" ? input : input instanceof URL ? input.href : (input as Request).url); + return base(input, init); + }) as typeof fetch; + Object.defineProperty(wrapper, "__piWebAccessSharedProxyFetch", { value: true }); + globalThis.fetch = wrapper; + // The shared marker must be visible through bili's re-chained top, so + // pi-web-access's own install guard sees a proxy is already in place + // and does not re-install on every web tool call. + assert.equal(Reflect.get(globalThis.fetch, "__piWebAccessSharedProxyFetch"), true); + const before = hits.length; + // A PLAIN model URL — what the host SDK actually calls. The intercept + // rewrites it to the pseudo-origin; the inline link must serve it. + const r = await withTimeout(fetch(`${upstreamOrigin}/v1/messages`, { method: "POST", body: "{}" }), 10_000, "model via chain + 0.46 wrapper"); + assert.equal(r.status, 200); + assert.equal(await r.text(), "inline-ok"); + assert.deepEqual(calls, [`${ie.INLINE_ORIGIN}/bili/${upstreamOrigin}/v1/messages`]); + // The real upstream must stay untouched — a DNS-bound pseudo-origin + // leak would never reach it anyway (ENOTFOUND). + assert.equal(hits.length, before); + // The wrapper's hook still ran on the rewritten URL (coexistence, not bypass). + assert.deepEqual(seenByWrapper, [`${ie.INLINE_ORIGIN}/bili/${upstreamOrigin}/v1/messages`]); + // Management URLs take the intercept's control-passthrough branch and + // must land on the inline engine too. + const m = await withTimeout(fetch(`${ie.INLINE_ORIGIN}/__bili/health`), 10_000, "mgmt via chain + 0.46 wrapper"); + assert.equal(m.status, 200); + assert.deepEqual(calls, [`${ie.INLINE_ORIGIN}/bili/${upstreamOrigin}/v1/messages`, `${ie.INLINE_ORIGIN}/__bili/health`]); + } finally { + const piInline2 = await import("../src/agent/pi-inline.ts"); + const fc2 = await import("../src/agent/fetch-chain/index.ts"); + const ni2 = await import("../src/agent/native-intercept.ts"); + piInline2._resetInlineTransportForTest(); + fc2.resetFetchChainForTest(); + ni2._resetForTest(); + ie._setEngineFactoryForTest(undefined); + ie._resetInlineEngineForTest(); + } +}); + test("#3278 transport: engine failure degrades model traffic direct, management fails loud", async () => { const ie = await import("../src/inline-engine.ts"); ie._setEngineFactoryForTest(async () => { From 2e97c3efd5ce1eb150e57e53e3968ddbab8a0854 Mon Sep 17 00:00:00 2001 From: Saim Shafique Date: Mon, 16 Nov 2026 13:48:18 +0000 Subject: [PATCH 2/2] fix: route inline-origin requests in-process even with a pi-web-access proxy scope on (#6829) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit With a pi-web-access proxy scope active, the wrapper sitting above the inline link curls INLINE_ORIGIN URLs itself, so the #1662 depth-1 fix never runs on that path and 0/2 inline-origin requests reach the engine (scope OFF: 2/2). A second leak exists for requests inheriting a stale dispatchDepth (#5614 shape), which skip dispatchBody entirely. Fix (routing-only, no default/budget change): - fetch-chain: exact-origin classifier isInlineOriginUrl (URL-parse; http only, exact hostname, default port, no credentials) and a classify-first check in patched — INLINE_ORIGIN requests run the link's own dispatch at a fresh depth 1 instead of being handed down the chain. The old depth-1 prefix route-down is removed. - pi-inline: the serve logic is extracted as exported serveInlineOrigin (undefined when not installed or not an inline-origin URL); the link dispatch delegates to it. - native-intercept: the dispatch's send is wrapped so every INLINE_ORIGIN send is served in-process via serveInlineOrigin; all other sends pass through untouched. Unconditional: bili cannot observe the wrapper's scope state from outside, so there is no signal to divert on. Tests: three new regression tests in tests/pi-inline.test.ts (proxy-scope ON/OFF, stale dispatchDepth, exact-origin classification) plus an updated #6829 expectation (the wrapper never sees pseudo-origin traffic now); 11/11 pi-inline, 29/29 self-heal + #5614 neighbours. Fixes #6829 --- src/agent/fetch-chain/index.ts | 45 +++++-- src/agent/native-intercept.ts | 23 +++- src/agent/pi-inline.ts | 88 ++++++++------ tests/pi-inline.test.ts | 215 ++++++++++++++++++++++++++++++++- 4 files changed, 325 insertions(+), 46 deletions(-) diff --git a/src/agent/fetch-chain/index.ts b/src/agent/fetch-chain/index.ts index 8fcf07ecc7..6680c34d97 100644 --- a/src/agent/fetch-chain/index.ts +++ b/src/agent/fetch-chain/index.ts @@ -31,6 +31,30 @@ function isPiWebAccessFetch(v: unknown): boolean { * 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; @@ -188,17 +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 (isPiWebAccessFetch(ds)) return send(input, init); - // #6829: the #1662 escape must never hand pseudo-origin traffic - // to the oldest live anchor — billion-context.inline has no DNS - // record, so the request dies ENOTFOUND instead of reaching the - // inline engine. Route it down the chain instead: send() stamps - // the hop, so the inline transport link recognizes the descent - // (#4527) and serves it in-process. - const url = fetchUrlOf(input); - if (url !== undefined && url.startsWith(INLINE_ORIGIN)) 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) { diff --git a/src/agent/native-intercept.ts b/src/agent/native-intercept.ts index 832ddaaa0a..49116a1434 100644 --- a/src/agent/native-intercept.ts +++ b/src/agent/native-intercept.ts @@ -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"; @@ -1568,7 +1569,25 @@ export function installNativeFetchIntercept(initialState: NativeInterceptState): // network again. Only ever contains origins we ourselves observed dying. const replacedOrigins = new Set(); - installFetchChain((send) => async (input: string | URL | Request, init?: RequestInit): Promise => { + installFetchChain((chainSend) => async (input: string | URL | Request, init?: RequestInit): Promise => { + // #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 => { + 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 diff --git a/src/agent/pi-inline.ts b/src/agent/pi-inline.ts index 7a5846b65f..876d2dc708 100644 --- a/src/agent/pi-inline.ts +++ b/src/agent/pi-inline.ts @@ -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"; @@ -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 { + 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 { @@ -34,40 +83,9 @@ export function installInlineTransport(): boolean { installed = true; rawSend = send; return async (input: string | URL | Request, init?: RequestInit): Promise => { - 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; diff --git a/tests/pi-inline.test.ts b/tests/pi-inline.test.ts index b4db57f582..0e25e15908 100644 --- a/tests/pi-inline.test.ts +++ b/tests/pi-inline.test.ts @@ -7,6 +7,7 @@ // mock upstream, makes exactly ONE upstream round trip per request (loop // safety), and tracks the session in-process. import { after, test } from "node:test"; +import { AsyncLocalStorage, AsyncResource } from "node:async_hooks"; import assert from "node:assert/strict"; import fs from "node:fs"; import os from "node:os"; @@ -112,6 +113,34 @@ async function freshChain() { return piInline; } +// #6841 rework: a minimal stand-in for the pi-web-access 0.46.0 shared proxy +// wrapper — the shape the real bundle installs when its proxy scope is on: +// with a scope active, any http(s) URL that is NOT on its bypass list is +// handled by the wrapper itself (it curls it) and never reaches the fetch +// below; with no scope active it passes through untouched. It marks itself +// with __piWebAccessSharedProxyFetch only (0.46+ behavior). `seenByWrapper` +// records what the wrapper actually observed, and `proxyCalls` records what +// its scope claimed — the assertions that prove inline traffic never reaches +// either. +function installScopeWrapper() { + const proxyScope = new AsyncLocalStorage(); + const proxyCalls: string[] = []; + const seenByWrapper: string[] = []; + const base = globalThis.fetch; + const wrapper = (async (input: string | URL | Request, init?: RequestInit): Promise => { + const url = typeof input === "string" ? input : input instanceof URL ? input.href : input.url; + seenByWrapper.push(url); + if (proxyScope.getStore() === true && !/^https?:\/\/(localhost|127\.0\.0\.1|\[::1\])([:/]|$)/.test(url)) { + proxyCalls.push(url); + return new Response("proxy-handled"); + } + return base(input, init); + }) as typeof fetch; + Object.defineProperty(wrapper, "__piWebAccessSharedProxyFetch", { value: true, configurable: true }); + globalThis.fetch = wrapper; + return { proxyScope, proxyCalls, seenByWrapper }; +} + test("#3278 transport: pseudo-origin URLs are served in-process, foreign URLs pass through", async () => { const ie = await import("../src/inline-engine.ts"); const calls: string[] = []; @@ -262,7 +291,10 @@ test("#6829: pi-web-access 0.46+ wrapper (shared marker only) keeps model traffi // leak would never reach it anyway (ENOTFOUND). assert.equal(hits.length, before); // The wrapper's hook still ran on the rewritten URL (coexistence, not bypass). - assert.deepEqual(seenByWrapper, [`${ie.INLINE_ORIGIN}/bili/${upstreamOrigin}/v1/messages`]); + // #6841 rework: the wrapper NEVER sees pseudo-origin traffic now — + // the intercept serves it in-process before any downstream runs, so + // coexistence is the marker handshake, not the hook running on it. + assert.deepEqual(seenByWrapper, []); // Management URLs take the intercept's control-passthrough branch and // must land on the inline engine too. const m = await withTimeout(fetch(`${ie.INLINE_ORIGIN}/__bili/health`), 10_000, "mgmt via chain + 0.46 wrapper"); @@ -280,6 +312,187 @@ test("#6829: pi-web-access 0.46+ wrapper (shared marker only) keeps model traffi } }); +test("#6841 rework: proxy-scope-ON pi-web-access wrapper cannot steal inline-origin traffic", async () => { + const ie = await import("../src/inline-engine.ts"); + const ni = await import("../src/agent/native-intercept.ts"); + const fc = await import("../src/agent/fetch-chain/index.ts"); + const piInline = await import("../src/agent/pi-inline.ts"); + ie._setEngineFactoryForTest(async () => ({ + origin: ie.INLINE_ORIGIN, + dispatch: async (): Promise => new Response("inline-ok"), + dispatchHttp: async () => {}, + wsOrigin: Promise.resolve(undefined), + webOrigin: Promise.resolve(undefined), + })); + try { + const model = `${ie.INLINE_ORIGIN}/bili/${upstreamOrigin}/v1/messages`; + const mgmt = `${ie.INLINE_ORIGIN}/__bili/health`; + const web = "http://example.test/web-search"; + // Intercept installed first (real arm order), wrapper above it. + ni._resetForTest(); + fc.resetFetchChainForTest(); + piInline._resetInlineTransportForTest(); + piInline.installInlineTransport(); + const state: import("../src/agent/native-intercept.ts").NativeInterceptState = { origin: ie.INLINE_ORIGIN, ready: Promise.resolve(ie.INLINE_ORIGIN) }; + assert.equal(ni.installNativeFetchIntercept(state), true); + const { proxyScope, proxyCalls, seenByWrapper } = installScopeWrapper(); + try { + // Scope ON: model + management traffic must reach the engine; + // the wrapper must see nothing at all. + await proxyScope.run(true, async () => { + assert.equal(await (await withTimeout(fetch(model, { method: "POST", body: "{}" }), 10_000, "model scope-on")).text(), "inline-ok"); + assert.equal(await (await withTimeout(fetch(mgmt), 10_000, "mgmt scope-on")).text(), "inline-ok"); + }); + assert.deepEqual(proxyCalls, []); + assert.deepEqual(seenByWrapper, []); + // Scope OFF: same outcome. + assert.equal(await (await withTimeout(fetch(model, { method: "POST", body: "{}" }), 10_000, "model scope-off")).text(), "inline-ok"); + assert.equal(await (await withTimeout(fetch(mgmt), 10_000, "mgmt scope-off")).text(), "inline-ok"); + assert.deepEqual(proxyCalls, []); + // Negative control: real web traffic with the scope ON still + // goes to the proxy stub, so the wrapper is genuinely active. + await proxyScope.run(true, async () => { + assert.equal(await (await withTimeout(fetch(web), 10_000, "web scope-on")).text(), "proxy-handled"); + }); + assert.deepEqual(proxyCalls, [web]); + } finally { + fc.resetFetchChainForTest(); + } + } finally { + const piInline2 = await import("../src/agent/pi-inline.ts"); + const fc2 = await import("../src/agent/fetch-chain/index.ts"); + const ni2 = await import("../src/agent/native-intercept.ts"); + piInline2._resetInlineTransportForTest(); + fc2.resetFetchChainForTest(); + ni2._resetForTest(); + ie._setEngineFactoryForTest(undefined); + ie._resetInlineEngineForTest(); + } +}); + +test("#6841 rework: inline-origin requests from a stale dispatchDepth context still reach the engine", async () => { + const ie = await import("../src/inline-engine.ts"); + const ni = await import("../src/agent/native-intercept.ts"); + const fc = await import("../src/agent/fetch-chain/index.ts"); + const piInline = await import("../src/agent/pi-inline.ts"); + // The dispatch mints an AsyncResource the #5614 way: created inside the + // fake engine dispatch, it keeps the dispatch's ALS (dispatchDepth 2) + // alive after the dispatch returns. + let stale: AsyncResource | undefined; + ie._setEngineFactoryForTest(async () => ({ + origin: ie.INLINE_ORIGIN, + dispatch: async (): Promise => { + stale = new AsyncResource("stale-scope"); + return new Response("inline-ok"); + }, + dispatchHttp: async () => {}, + wsOrigin: Promise.resolve(undefined), + webOrigin: Promise.resolve(undefined), + })); + try { + const model = `${ie.INLINE_ORIGIN}/bili/${upstreamOrigin}/v1/messages`; + ni._resetForTest(); + fc.resetFetchChainForTest(); + piInline._resetInlineTransportForTest(); + piInline.installInlineTransport(); + const state: import("../src/agent/native-intercept.ts").NativeInterceptState = { origin: ie.INLINE_ORIGIN, ready: Promise.resolve(ie.INLINE_ORIGIN) }; + assert.equal(ni.installNativeFetchIntercept(state), true); + const { proxyScope, proxyCalls, seenByWrapper } = installScopeWrapper(); + try { + await withTimeout(fetch(model, { method: "POST", body: "{}" }), 10_000, "seed"); + assert.notEqual(stale, undefined); + // Requests issued from that stale scope, with the proxy scope ON, + // must still reach the engine — not the wrapper, not the anchor. + await proxyScope.run(true, async () => { + await stale!.runInAsyncScope(async () => { + assert.equal(await (await withTimeout(fetch(model, { method: "POST", body: "{}" }), 10_000, "stale model")).text(), "inline-ok"); + assert.equal(await (await withTimeout(fetch(`${ie.INLINE_ORIGIN}/bili/${upstreamOrigin}/v1/messages`, { method: "POST", body: "{}" }), 10_000, "stale routed")).text(), "inline-ok"); + }); + }); + assert.deepEqual(proxyCalls, []); + assert.deepEqual(seenByWrapper, []); + } finally { + fc.resetFetchChainForTest(); + } + } finally { + const piInline2 = await import("../src/agent/pi-inline.ts"); + const fc2 = await import("../src/agent/fetch-chain/index.ts"); + const ni2 = await import("../src/agent/native-intercept.ts"); + piInline2._resetInlineTransportForTest(); + fc2.resetFetchChainForTest(); + ni2._resetForTest(); + ie._setEngineFactoryForTest(undefined); + ie._resetInlineEngineForTest(); + } +}); + +test("#6841 rework: inline-origin classification is the exact origin, not a prefix", async () => { + const ie = await import("../src/inline-engine.ts"); + const fc = await import("../src/agent/fetch-chain/index.ts"); + const piInline = await import("../src/agent/pi-inline.ts"); + const calls: string[] = []; + ie._setEngineFactoryForTest(async () => ({ + origin: ie.INLINE_ORIGIN, + dispatch: async (input: string | URL | Request): Promise => { + calls.push(String(input)); + return new Response("inline-ok"); + }, + dispatchHttp: async () => {}, + wsOrigin: Promise.resolve(undefined), + webOrigin: Promise.resolve(undefined), + })); + const originalFetch = globalThis.fetch; + try { + // Canned anchor: anything the link does NOT serve in-process lands + // here, so a misclassification is observable without real DNS. + const anchorCalls: string[] = []; + const canned = (async (input: string | URL | Request, _init?: RequestInit) => { + anchorCalls.push(typeof input === "string" ? input : input instanceof URL ? input.href : (input as Request).url); + return new Response("anchor-ok"); + }) as typeof fetch; + // Define the property DIRECTLY: a plain assignment would fire the + // stale guard accessor left by the previous test, re-chaining the + // canned sink as the downstream of the inherited top — every later + // test would then read "anchor-ok" instead of the real upstream. + // No { anchor } option: the canned sink is only the link's + // downstream (captured from globalThis.fetch at install); the + // module anchor stays the real fetch. + Object.defineProperty(globalThis, "fetch", { value: canned, writable: true, configurable: true, enumerable: true }); + fc.resetFetchChainForTest(); + piInline._resetInlineTransportForTest(); + piInline.installInlineTransport(); + const model = `${ie.INLINE_ORIGIN}/bili/${upstreamOrigin}/v1/messages`; + // Exact origin, case-insensitive host, :80 — served in-process. + assert.equal(await (await withTimeout(fetch(model), 10_000, "exact")).text(), "inline-ok"); + assert.equal(await (await withTimeout(fetch("HTTP://BILLION-CONTEXT.INLINE/bili/x"), 10_000, "case")).text(), "inline-ok"); + assert.equal(await (await withTimeout(fetch("http://billion-context.inline:80/bili/x"), 10_000, "port80")).text(), "inline-ok"); + // Lookalike host, https, :81, credentials — NOT the pseudo-origin: + // they pass through to the anchor instead of reaching the engine. + assert.equal(await (await withTimeout(fetch("http://billion-context.inline.evil.com/bili/x"), 10_000, "lookalike")).text(), "anchor-ok"); + assert.equal(await (await withTimeout(fetch("https://billion-context.inline/bili/x"), 10_000, "https")).text(), "anchor-ok"); + assert.equal(await (await withTimeout(fetch("http://billion-context.inline:81/bili/x"), 10_000, "port81")).text(), "anchor-ok"); + assert.equal(await (await withTimeout(fetch("http://user:pass@billion-context.inline/bili/x"), 10_000, "creds")).text(), "anchor-ok"); + assert.deepEqual(calls, [model, "HTTP://BILLION-CONTEXT.INLINE/bili/x", "http://billion-context.inline:80/bili/x"]); + assert.deepEqual(anchorCalls, [ + "http://billion-context.inline.evil.com/bili/x", + "https://billion-context.inline/bili/x", + "http://billion-context.inline:81/bili/x", + "http://user:pass@billion-context.inline/bili/x", + ]); + } finally { + const piInline2 = await import("../src/agent/pi-inline.ts"); + const fc2 = await import("../src/agent/fetch-chain/index.ts"); + piInline2._resetInlineTransportForTest(); + fc2.resetFetchChainForTest(); + // Reset restores the pre-install descriptor (the canned anchor); + // define the previous top back directly, so the guarded setter + // cannot swallow it as an own-chain write-back (see above). + Object.defineProperty(globalThis, "fetch", { value: originalFetch, writable: true, configurable: true, enumerable: true }); + ie._setEngineFactoryForTest(undefined); + ie._resetInlineEngineForTest(); + } +}); + test("#3278 transport: engine failure degrades model traffic direct, management fails loud", async () => { const ie = await import("../src/inline-engine.ts"); ie._setEngineFactoryForTest(async () => {