From ddc4d0ab60175eec92085da27eb123001db0a959 Mon Sep 17 00:00:00 2001 From: ams Date: Sat, 17 Oct 2026 16:54:23 +0700 Subject: [PATCH 1/5] fix(#2645/#2649/#2583): wire-conformant prompt_cache_key on every egress lane MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - session-id: drop the route-rewritten leg of shouldStampRelayAffinityPck (#2649: a rewritten/embedded URL is not relay evidence — the /bili/ tunnel rewrites the user's own remote destination too); loopback is the only destination-shape proof. Export relayAffinityDestined for the egress site. - prepare-openai: chat wire no longer passes a client prompt_cache_key through verbatim to non-relay upstreams — strict APIs 400 unknown body params (#2645). Client value still wins on loopback relay destinations. - pi (stampPromptCacheKey): hoist the #2583 Cowork withhold above the routed-check so it gates ALL stamp paths. Pre-hoist, the launcher cert-MITM lane (BILI_MITM_HOSTS) made destinationRoutedThroughProxy() true and skipped the withhold entirely — stamped bodies reached Anthropic's strict schema verbatim (#1403 400s). - pi (ompCoworkTransportBypassesFetch): a LOOPBACK HTTPS_PROXY is bili itself and Cowork's node:https honors it — treat as routing evidence (stamp); a foreign/non-loopback HTTPS_PROXY is a bypass (withhold). Tests: flip #2649 matrix row; 4 new cases (MITM withhold, loopback HTTPS_PROXY stamp, foreign HTTPS_PROXY withhold, predicate matrix). --- src/agent/pi.ts | 47 ++++++++++++------- src/server/prepare-openai.ts | 19 +++++--- src/session-id.ts | 37 ++++++++++----- tests/issue2218-relay-affinity-signal.test.ts | 7 ++- tests/omp-native-identity.test.ts | 31 ++++++++++++ 5 files changed, 104 insertions(+), 37 deletions(-) diff --git a/src/agent/pi.ts b/src/agent/pi.ts index 2b3a56059f..1ddb0fbc1a 100644 --- a/src/agent/pi.ts +++ b/src/agent/pi.ts @@ -408,7 +408,24 @@ export function ompCoworkTransportBypassesFetch( // getProxyForProvider("anthropic"): PI_PROXY_ANTHROPIC (raw truthy) || PI_PROXY (trimmed). const hasProviderProxy = (env.PI_PROXY_ANTHROPIC ?? "").length > 0; const hasGlobalProxy = (env.PI_PROXY ?? "").trim().length > 0; - return !hasProviderProxy && !hasGlobalProxy; + if (hasProviderProxy || hasGlobalProxy) return false; + // Launcher lanes (cert-MITM): a LOOPBACK HTTPS_PROXY is bili itself, and + // the Cowork transport's node:https stack honors it — the request + // transits the proxy, which consumes and strips the stamped pck, so it + // is routing evidence exactly like PI_PROXY. A non-loopback (foreign/ + // corporate) HTTPS_PROXY is no bili consumer: treat it as a bypass so + // the stamp can never ride it verbatim into a strict-schema upstream + // (#1403/#2583). + const httpsProxy = (env.HTTPS_PROXY ?? env.https_proxy ?? "").trim(); + if (httpsProxy.length > 0) { + try { + const hostname = new URL(httpsProxy).hostname.toLowerCase(); + if (hostname === "localhost" || hostname === "::1" || hostname === "[::1]" || /^127(?:\.\d{1,3}){0,3}$/.test(hostname)) return false; + } catch { + // unparseable proxy URL — no routing evidence, keep the bypass + } + } + return true; } // Once per process per model — the bypass is a property of the installed host @@ -452,6 +469,19 @@ export function stampPromptCacheKey(event: unknown, ctx: Ctx, agent: string): Re // name gate. const profile = hostProfileFor(agent); if (!profile.stampsBodyPromptCacheKey) return undefined; + // #2583 (hoisted): the Cowork withhold must gate EVERY stamp path, not + // just the unrouted fallback below. On the launcher cert-MITM lane + // (HTTPS_PROXY + BILI_MITM_HOSTS) destinationRoutedThroughProxy() is + // true, which skipped this check entirely — yet Cowork traffic never + // rides globalThis.fetch on that lane either, so the stamp could fire + // on a transport the fetch intercept cannot consume and ride verbatim + // into Anthropic's strict schema (#1403's 400 on every delegation). + // #3482 site 2: whether that transport is even POSSIBLE is a registry + // property; the env observation below is the per-request truth. + if (profile.coworkTransportPossible && ompCoworkTransportBypassesFetch(ctx.model, process.env)) { + noteOmpCoworkBypass(ctx.model?.id ?? (ctx.model?.baseUrl ?? "")); + return undefined; + } if (!destinationRoutedThroughProxy(ctx.model?.baseUrl)) { // #1579: native omp mode routes at the FETCH layer — the model // baseUrl stays the real upstream, so the launcher-shaped checks @@ -480,21 +510,6 @@ export function stampPromptCacheKey(event: unknown, ctx: Ctx, agent: string): Re const base = ctx.model?.baseUrl ?? ""; const expanded = /\/v\d+\/?$/.test(base) ? `${base.replace(/\/+$/, "")}/chat/completions` : base; if (!isModelApiUrl(base) && !isModelApiUrl(expanded)) return undefined; - // #2583: the gates above prove the interceptor WOULD rewrite this URL - // at the fetch layer — but OMP's Cowork transport never puts default - // anthropic/anthropic-messages traffic ON globalThis.fetch in the - // first place (node:https direct unless a proxy env is set), so the - // rewrite can never fire and the stamped pck would ride verbatim into - // Anthropic's strict schema (400 on every delegation). Withhold the - // stamp where the transport is proven direct: those requests go - // direct anyway (uncompressed, anonymous to the proxy) — soft - // identity loss instead of a guaranteed hard failure (#1403). - // #3482 site 2: whether that transport is even POSSIBLE is a registry - // property; the env-observation below is the per-request truth. - if (profile.coworkTransportPossible && ompCoworkTransportBypassesFetch(ctx.model, process.env)) { - noteOmpCoworkBypass(ctx.model?.id ?? base); - return undefined; - } } const payload = (event as { payload?: unknown } | undefined)?.payload; if (payload === null || typeof payload !== "object" || Array.isArray(payload)) return undefined; diff --git a/src/server/prepare-openai.ts b/src/server/prepare-openai.ts index 2fb81e6499..2cef745cde 100644 --- a/src/server/prepare-openai.ts +++ b/src/server/prepare-openai.ts @@ -5,7 +5,7 @@ import { coreToOpenai, injectOpenaiSystem, openaiToCore, type BiliMessage, type import type { CompressReasoningConfig } from "../reasoning-drop.js"; import type { ProxyOptions } from "../config.js"; import { currentCalibrationFactor, hardenOpenaiAssistantContent, systemToUser } from "../util.js"; -import { conversationHeaderSource, shouldStampRelayAffinityPck } from "../session-id.js"; +import { conversationHeaderSource, relayAffinityDestined, shouldStampRelayAffinityPck } from "../session-id.js"; import { applyReasoningDrop, diagNudge, diagTagSummary, deriveTitle, effectiveTokenCount, imageBillingFor, imageReserveFor, imageTokenCapFor, isAutoInjectedNotification, planReasoningDrop, reapOrphansLogged, runNudgeDecision, stripKernelSummaries, warnDroppedOpenaiParts, warnReasoningPairs, type Prepared } from "../server.js"; import { buildDecisionPrompt, buildDirectiveText, consumeFallback, ladderMode, resolveDecisionRange, stripDecisionReasoningFields, type DecideConfig } from "../nudge-decide.js"; import { excludeDeadRefRanges } from "../acp-status.js"; @@ -402,18 +402,25 @@ export async function prepareOpenai( // launcher forces PI_CACHE_RETENTION=long (for the session-id // prompt_cache_key) which makes the client also emit it. Third-party // OpenAI-compatible upstreams may reject unknown fields, and cache policy - // is the upstream's business — strip it. prompt_cache_key itself passes - // through: upstreams that ignore it lose nothing, upstreams that use it - // get a per-conversation routing hint. + // is the upstream's business — strip it. prompt_cache_key is not + // schema-defined on the chat wire either: strict upstreams 400 unknown + // body params (#2645), so it is kept only where a relay can live and + // stripped from all other egress below. delete (rebuilt as Record).prompt_cache_retention; // #2218: body-only relay affinity for plugin hosts. dsh desktop's only // identity signal is the bili-internal x-bili-plugin-conversation header // (already forwarded as x-session-id by buildForwardTarget); body-keyed // relays (workbuddy panel) can't read headers, so stamp the conversation - // id into prompt_cache_key too. Gated to admin-rewritten or loopback - // destinations — strict direct APIs can 400 unknown body params (#1403). + // id into prompt_cache_key too. Gated to loopback relay destinations + // (#2649: a rewritten route is not relay evidence) — strict direct APIs + // can 400 unknown body params (#1403/#2645), so a client-supplied + // prompt_cache_key survives verbatim ONLY on relay destinations + // ("client wins" there, the body-keyed relay family may be keyed by it) + // and is stripped on all other egress. if (shouldStampRelayAffinityPck(conversationHeaderSource(req.headers)?.name, parsed.prompt_cache_key, billingUpstream, upstreamOrigin)) { rebuilt.prompt_cache_key = session.id; + } else if (!relayAffinityDestined(upstreamOrigin)) { + delete (rebuilt as Record).prompt_cache_key; } // OpenAI Chat Completions only emits a usage object in the final stream // chunk when the client sets stream_options.include_usage=true. Without diff --git a/src/session-id.ts b/src/session-id.ts index cd993ad54d..c342529595 100644 --- a/src/session-id.ts +++ b/src/session-id.ts @@ -236,20 +236,20 @@ export function affinityToken(identity: ConversationIdentity): string | undefine * - the plugin lane owns the identity (the winning conversation header is * the bili-internal one, not a header the upstream could read itself); * - the client sent no prompt_cache_key of its own (client value wins); - * - the destination is admin-configured (provider-rewritten route) or - * loopback — deliberate relay/local deployments. Strict direct APIs can - * 400 unknown body params (#1403), so unrouted non-loopback forwards - * stay unstamped. + * - the destination is loopback — deliberate relay/local deployments. + * #2649: a rewritten/embedded route URL is NOT relay evidence (the /bili/ + * tunnel lane rewrites the user's own remote destination too, which + * stamped strict remote APIs and 400'd them — the #2645 class), so the + * admin-rewritten leg is gone; loopback is the only destination-shape + * proof of a relay. Strict direct APIs can 400 unknown body params + * (#1403/#2645), so non-loopback forwards stay unstamped. */ -export function shouldStampRelayAffinityPck( - identityHeaderName: string | undefined, - clientPck: unknown, - routeRewrittenUrl: string | undefined, - upstreamOrigin: string, -): boolean { - if (identityHeaderName !== "x-bili-plugin-conversation") return false; - if (clientPck !== undefined) return false; - if (routeRewrittenUrl !== undefined) return true; + +/** #2218/#2649: destination-shape half of the gate — is this origin a + * deliberate local relay? Shared with the chat egress site so a + * client-supplied prompt_cache_key is kept exactly where relays may read + * it and stripped everywhere else. */ +export function relayAffinityDestined(upstreamOrigin: string): boolean { try { const hostname = new URL(upstreamOrigin).hostname; // "localhost" is loopback but not matched by isLoopbackAddress (127.x/::1 @@ -260,6 +260,17 @@ export function shouldStampRelayAffinityPck( } } +export function shouldStampRelayAffinityPck( + identityHeaderName: string | undefined, + clientPck: unknown, + _routeRewrittenUrl: string | undefined, + upstreamOrigin: string, +): boolean { + if (identityHeaderName !== "x-bili-plugin-conversation") return false; + if (clientPck !== undefined) return false; + return relayAffinityDestined(upstreamOrigin); +} + /** * Promote a Responses body's `prompt_cache_key` over a content-fingerprint * identity. Clients that replay full history statelessly (omp, some codex diff --git a/tests/issue2218-relay-affinity-signal.test.ts b/tests/issue2218-relay-affinity-signal.test.ts index 8cb909a440..e55c775658 100644 --- a/tests/issue2218-relay-affinity-signal.test.ts +++ b/tests/issue2218-relay-affinity-signal.test.ts @@ -103,8 +103,11 @@ function chatBody(extra: Record = {}): Record test("#2218 unit: shouldStampRelayAffinityPck gate matrix", () => { const loop = "http://127.0.0.1:7864/v1"; const remote = "https://relay.example.com/v1"; - // plugin lane + no client pck + admin-rewritten route → stamp - assert.equal(shouldStampRelayAffinityPck("x-bili-plugin-conversation", undefined, loop, remote), true); + // #2649: plugin lane + no client pck + rewritten route to a REMOTE + // destination → NO stamp (a rewritten/embedded URL is not relay + // evidence — the /bili/ tunnel rewrites the user's own remote + // destination too; verbatim stamps 400'd strict upstreams, #2645) + assert.equal(shouldStampRelayAffinityPck("x-bili-plugin-conversation", undefined, loop, remote), false); // plugin lane + no client pck + loopback destination without a route → stamp assert.equal(shouldStampRelayAffinityPck("x-bili-plugin-conversation", undefined, undefined, loop), true); // plugin lane + no client pck + unrouted non-loopback (strict direct API risk, #1403) → no stamp diff --git a/tests/omp-native-identity.test.ts b/tests/omp-native-identity.test.ts index 21e3b81883..d07ecccd10 100644 --- a/tests/omp-native-identity.test.ts +++ b/tests/omp-native-identity.test.ts @@ -195,3 +195,34 @@ test("#2583: ompCoworkTransportBypassesFetch mirrors OMP's own egress selection assert.equal(ompCoworkTransportBypassesFetch(COWORK_MODEL, { PI_PROXY_ANTHROPIC: "" }), true); assert.equal(ompCoworkTransportBypassesFetch(COWORK_MODEL, { PI_PROXY_ANTHROPIC: "http://127.0.0.1:9999" }), false); }); + +test("#2583 hoist: launcher MITM lane (BILI_MITM_HOSTS) withholds the stamp when no proxy env routes Cowork", () => { + // destinationRoutedThroughProxy() is TRUE here (MITM whitelist) — the + // pre-hoist bug skipped the Cowork withhold entirely on this branch. + withIntercept(true, () => withEnv(PROXY_ORIGIN, () => withProxyEnv({ ...NO_PROXY_ENV, BILI_MITM_HOSTS: "api.anthropic.com" }, () => { + const out = stampPromptCacheKey(makeEvent({}), makeCtx("https://api.anthropic.com/v1", COWORK_MODEL), "omp"); + assert.equal(out, undefined); + }))); +}); + +test("#2583 hoist: launcher lane with a LOOPBACK HTTPS_PROXY stamps (cert-MITM lane consumes it)", () => { + withIntercept(true, () => withEnv(PROXY_ORIGIN, () => withProxyEnv({ ...NO_PROXY_ENV, HTTPS_PROXY: "http://127.0.0.1:18787" }, () => { + const out = stampPromptCacheKey(makeEvent({}), makeCtx("https://api.anthropic.com/v1", COWORK_MODEL), "omp"); + assert.deepEqual(out?.prompt_cache_key, "sess-1579"); + }))); +}); + +test("#2583 hoist: a FOREIGN (non-loopback) HTTPS_PROXY withholds (no bili consumer on that path)", () => { + withIntercept(true, () => withEnv(PROXY_ORIGIN, () => withProxyEnv({ ...NO_PROXY_ENV, HTTPS_PROXY: "http://corp-proxy.example.com:3128" }, () => { + const out = stampPromptCacheKey(makeEvent({}), makeCtx("https://api.anthropic.com/v1", COWORK_MODEL), "omp"); + assert.equal(out, undefined); + }))); +}); + +test("#2583: ompCoworkTransportBypassesFetch treats a loopback HTTPS_PROXY as routing evidence (launcher cert-MITM)", () => { + assert.equal(ompCoworkTransportBypassesFetch(COWORK_MODEL, { HTTPS_PROXY: "http://127.0.0.1:18787" }), false); + assert.equal(ompCoworkTransportBypassesFetch(COWORK_MODEL, { https_proxy: "http://localhost:18787" }), false); + assert.equal(ompCoworkTransportBypassesFetch(COWORK_MODEL, { HTTPS_PROXY: "http://[::1]:18787" }), false); + assert.equal(ompCoworkTransportBypassesFetch(COWORK_MODEL, { HTTPS_PROXY: "http://corp-proxy.example.com:3128" }), true); + assert.equal(ompCoworkTransportBypassesFetch(COWORK_MODEL, { HTTPS_PROXY: "not-a-url" }), true); +}); From 925e6e65d4d4493ff0f305b2cef65cd9b1e8b284 Mon Sep 17 00:00:00 2001 From: ams Date: Sat, 17 Oct 2026 17:10:57 +0700 Subject: [PATCH 2/5] launcher(omp): route the Cowork anthropic lane through the proxy (PI_PROXY_ANTHROPIC) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The #2583 withhold (hoisted earlier in this branch) correctly stops the identity stamp from leaking, but Cowork traffic still left the host direct — uncompressed and invisible to the proxy — because omp's Cowork transport sends node:https and ignores HTTPS_PROXY entirely (proven live, Anthropic req_011Cg7cujfgv4xLjuRvLMqvg). PI_PROXY_ANTHROPIC pushes Cowork onto Bun's own fetch, where the interceptor's /bili/ rewrite carries it through the proxy as plain loopback HTTP (no MITM CA involved). Verified live: forward POST → api.anthropic.com/v1/messages, session-visible, 200, pck scrubbed. Also reverts the loopback-HTTPS_PROXY clause in ompCoworkTransportBypassesFetch (same live proof: not routing evidence for this transport) and flips its tests to the proven posture. --- src/agent/pi.ts | 22 ++++++---------------- src/launcher.ts | 12 ++++++++++++ tests/omp-native-identity.test.ts | 18 ++++++++++++------ 3 files changed, 30 insertions(+), 22 deletions(-) diff --git a/src/agent/pi.ts b/src/agent/pi.ts index 1ddb0fbc1a..bdcb9939f0 100644 --- a/src/agent/pi.ts +++ b/src/agent/pi.ts @@ -409,22 +409,12 @@ export function ompCoworkTransportBypassesFetch( const hasProviderProxy = (env.PI_PROXY_ANTHROPIC ?? "").length > 0; const hasGlobalProxy = (env.PI_PROXY ?? "").trim().length > 0; if (hasProviderProxy || hasGlobalProxy) return false; - // Launcher lanes (cert-MITM): a LOOPBACK HTTPS_PROXY is bili itself, and - // the Cowork transport's node:https stack honors it — the request - // transits the proxy, which consumes and strips the stamped pck, so it - // is routing evidence exactly like PI_PROXY. A non-loopback (foreign/ - // corporate) HTTPS_PROXY is no bili consumer: treat it as a bypass so - // the stamp can never ride it verbatim into a strict-schema upstream - // (#1403/#2583). - const httpsProxy = (env.HTTPS_PROXY ?? env.https_proxy ?? "").trim(); - if (httpsProxy.length > 0) { - try { - const hostname = new URL(httpsProxy).hostname.toLowerCase(); - if (hostname === "localhost" || hostname === "::1" || hostname === "[::1]" || /^127(?:\.\d{1,3}){0,3}$/.test(hostname)) return false; - } catch { - // unparseable proxy URL — no routing evidence, keep the bypass - } - } + // HTTPS_PROXY is deliberately NOT routing evidence here: the Cowork + // transport's node:https stack ignores env proxies entirely (proven + // live 2026-10-17, Anthropic req_011Cg7cujfgv4xLjuRvLMqvg — a loopback + // HTTPS_PROXY was set and the stamped body still reached the upstream + // direct, 400). Only PI_PROXY / PI_PROXY_ANTHROPIC push Cowork onto + // Bun's fetch, where the patch can see and the proxy can consume it. return true; } diff --git a/src/launcher.ts b/src/launcher.ts index 9464d0f398..2c559e5a24 100644 --- a/src/launcher.ts +++ b/src/launcher.ts @@ -5029,6 +5029,18 @@ export async function runLaunch(params: RunLaunchParams, deps: LauncherDeps = {} // the proxy on session_compact. https upstreams ride cert-MITM like pi. env = buildPiEnv(origin, ca, stripInheritedProxy(process.env), routes.httpRewrites, [], extMitmHosts, extNonHttpProviders); delete env.PI_CODING_AGENT_DIR; + // #2583 completion: omp's Cowork transport (anthropic OAuth lane) + // sends node:https DIRECT and ignores HTTPS_PROXY entirely — those + // bytes bypassed bili (uncompressed, invisible) even on this lane. + // PI_PROXY_ANTHROPIC pushes Cowork onto Bun's own fetch, where the + // interceptor's /bili/ rewrite routes it through the proxy (plain + // loopback HTTP — no MITM CA involved; the proxy re-originates TLS). + // Verified live: Claude then rides the compressible lane and the + // anthropic pck scrub keeps the strict schema clean. Only fill the + // gap — never override a user-set proxy posture. + if (env.PI_PROXY_ANTHROPIC === undefined && env.PI_PROXY === undefined) { + env.PI_PROXY_ANTHROPIC = origin; + } // #1774: the child always rides bili's proxy here, so a long preflight can // hold the first SSE event well past OMP's default 300s first-parsed-event // watchdog — export a wider first-event budget unless the user pinned one. diff --git a/tests/omp-native-identity.test.ts b/tests/omp-native-identity.test.ts index d07ecccd10..2824626c18 100644 --- a/tests/omp-native-identity.test.ts +++ b/tests/omp-native-identity.test.ts @@ -205,10 +205,16 @@ test("#2583 hoist: launcher MITM lane (BILI_MITM_HOSTS) withholds the stamp when }))); }); -test("#2583 hoist: launcher lane with a LOOPBACK HTTPS_PROXY stamps (cert-MITM lane consumes it)", () => { +test("#2583 hoist: launcher lane with a LOOPBACK HTTPS_PROXY still withholds (Cowork node:https ignores env proxies)", () => { + // Proven live 2026-10-17 (Anthropic req_011Cg7cujfgv4xLjuRvLMqvg): the + // launcher sets a loopback HTTPS_PROXY for its cert-MITM lane, but the + // Cowork transport's node:https stack never honors env proxies — the + // stamped body reached the upstream direct and 400'd. Env-proxy shape is + // therefore NOT routing evidence for this transport; only PI_PROXY / + // PI_PROXY_ANTHROPIC (which push Cowork onto Bun's fetch) are. withIntercept(true, () => withEnv(PROXY_ORIGIN, () => withProxyEnv({ ...NO_PROXY_ENV, HTTPS_PROXY: "http://127.0.0.1:18787" }, () => { const out = stampPromptCacheKey(makeEvent({}), makeCtx("https://api.anthropic.com/v1", COWORK_MODEL), "omp"); - assert.deepEqual(out?.prompt_cache_key, "sess-1579"); + assert.equal(out, undefined); }))); }); @@ -219,10 +225,10 @@ test("#2583 hoist: a FOREIGN (non-loopback) HTTPS_PROXY withholds (no bili consu }))); }); -test("#2583: ompCoworkTransportBypassesFetch treats a loopback HTTPS_PROXY as routing evidence (launcher cert-MITM)", () => { - assert.equal(ompCoworkTransportBypassesFetch(COWORK_MODEL, { HTTPS_PROXY: "http://127.0.0.1:18787" }), false); - assert.equal(ompCoworkTransportBypassesFetch(COWORK_MODEL, { https_proxy: "http://localhost:18787" }), false); - assert.equal(ompCoworkTransportBypassesFetch(COWORK_MODEL, { HTTPS_PROXY: "http://[::1]:18787" }), false); +test("#2583: ompCoworkTransportBypassesFetch ignores HTTPS_PROXY entirely (Cowork node:https never reads it)", () => { + assert.equal(ompCoworkTransportBypassesFetch(COWORK_MODEL, { HTTPS_PROXY: "http://127.0.0.1:18787" }), true); + assert.equal(ompCoworkTransportBypassesFetch(COWORK_MODEL, { https_proxy: "http://localhost:18787" }), true); + assert.equal(ompCoworkTransportBypassesFetch(COWORK_MODEL, { HTTPS_PROXY: "http://[::1]:18787" }), true); assert.equal(ompCoworkTransportBypassesFetch(COWORK_MODEL, { HTTPS_PROXY: "http://corp-proxy.example.com:3128" }), true); assert.equal(ompCoworkTransportBypassesFetch(COWORK_MODEL, { HTTPS_PROXY: "not-a-url" }), true); }); From eb6f4174a41e203b17c69a37ab9589f576ba8961 Mon Sep 17 00:00:00 2001 From: awork Date: Sat, 17 Oct 2026 20:44:06 +0800 Subject: [PATCH 3/5] refactor(session-id): drop dead routeRewrittenUrl param, restore gate doc placement; add #2645/#3556 remote-destination pck regressions Review of #3556: this PR already dropped the rewritten-URL leg of shouldStampRelayAffinityPck, which left the third parameter dead and the full-gate JSDoc stranded above relayAffinityDestined. Aligns the signature with #2649's 3-arg form and moves the doc back onto the function it describes. Also ports the #2649 strict-remote regression (mock upstream that 400s on prompt_cache_key, bound to a non-loopback IPv4 so bili sees a REMOTE destination) and adds the missing regression for the new client-pck strip: verbatim client prompt_cache_key must not reach non-relay chat egress (plain-client and dsh plugin lanes), while x-session-id identity survives. --- src/server/prepare-openai.ts | 2 +- src/session-id.ts | 32 ++--- tests/issue2218-relay-affinity-signal.test.ts | 129 +++++++++++++++--- 3 files changed, 125 insertions(+), 38 deletions(-) diff --git a/src/server/prepare-openai.ts b/src/server/prepare-openai.ts index 2cef745cde..711e8030c6 100644 --- a/src/server/prepare-openai.ts +++ b/src/server/prepare-openai.ts @@ -417,7 +417,7 @@ export async function prepareOpenai( // prompt_cache_key survives verbatim ONLY on relay destinations // ("client wins" there, the body-keyed relay family may be keyed by it) // and is stripped on all other egress. - if (shouldStampRelayAffinityPck(conversationHeaderSource(req.headers)?.name, parsed.prompt_cache_key, billingUpstream, upstreamOrigin)) { + if (shouldStampRelayAffinityPck(conversationHeaderSource(req.headers)?.name, parsed.prompt_cache_key, upstreamOrigin)) { rebuilt.prompt_cache_key = session.id; } else if (!relayAffinityDestined(upstreamOrigin)) { delete (rebuilt as Record).prompt_cache_key; diff --git a/src/session-id.ts b/src/session-id.ts index c342529595..e1fc418e85 100644 --- a/src/session-id.ts +++ b/src/session-id.ts @@ -220,6 +220,21 @@ export function affinityToken(identity: ConversationIdentity): string | undefine return identity.clientProvided ? identity.value : undefined; } +/** #2218/#2649: destination-shape half of the gate — is this origin a + * deliberate local relay? Shared with the chat egress site so a + * client-supplied prompt_cache_key is kept exactly where relays may read + * it and stripped everywhere else. */ +export function relayAffinityDestined(upstreamOrigin: string): boolean { + try { + const hostname = new URL(upstreamOrigin).hostname; + // "localhost" is loopback but not matched by isLoopbackAddress (127.x/::1 + // only); a silent miss here would reproduce the zero-signal bug (#2218). + return hostname === "localhost" || isLoopbackAddress(hostname); + } catch { + return false; + } +} + /** * #2218: decide whether the proxy stamps `prompt_cache_key` into an outbound * OpenAI chat body on behalf of a plugin host (dsh desktop) whose ONLY @@ -244,26 +259,9 @@ export function affinityToken(identity: ConversationIdentity): string | undefine * proof of a relay. Strict direct APIs can 400 unknown body params * (#1403/#2645), so non-loopback forwards stay unstamped. */ - -/** #2218/#2649: destination-shape half of the gate — is this origin a - * deliberate local relay? Shared with the chat egress site so a - * client-supplied prompt_cache_key is kept exactly where relays may read - * it and stripped everywhere else. */ -export function relayAffinityDestined(upstreamOrigin: string): boolean { - try { - const hostname = new URL(upstreamOrigin).hostname; - // "localhost" is loopback but not matched by isLoopbackAddress (127.x/::1 - // only); a silent miss here would reproduce the zero-signal bug (#2218). - return hostname === "localhost" || isLoopbackAddress(hostname); - } catch { - return false; - } -} - export function shouldStampRelayAffinityPck( identityHeaderName: string | undefined, clientPck: unknown, - _routeRewrittenUrl: string | undefined, upstreamOrigin: string, ): boolean { if (identityHeaderName !== "x-bili-plugin-conversation") return false; diff --git a/tests/issue2218-relay-affinity-signal.test.ts b/tests/issue2218-relay-affinity-signal.test.ts index e55c775658..87bea3a796 100644 --- a/tests/issue2218-relay-affinity-signal.test.ts +++ b/tests/issue2218-relay-affinity-signal.test.ts @@ -1,6 +1,7 @@ import { test } from "node:test"; import assert from "node:assert/strict"; import http from "node:http"; +import os from "node:os"; import { once } from "node:events"; import { startServer } from "../src/server.ts"; import { defaultConfig } from "acp-kernel"; @@ -31,7 +32,19 @@ interface Harness { close(): Promise; } -async function startHarness(routes: ProxyOptions["routes"] | undefined): Promise { +interface HarnessOpts { + /** Bind address of the mock upstream. Non-loopback addresses make bili see a + * REMOTE destination (the #2645/#2649 shape) while the socket still lands on + * this host. */ + bindAddress?: string; + /** Strict-schema mode (#2645): answer 400 on any prompt_cache_key in the body, + * mirroring NVIDIA NIM's `Validation: Unsupported parameter(s)` rejection, so + * a leaked/stamped field reproduces as a real 400 instead of a field check. */ + rejectPck?: boolean; +} + +async function startHarness(routes: ProxyOptions["routes"] | undefined, opts: HarnessOpts = {}): Promise { + const { bindAddress = "127.0.0.1", rejectPck = false } = opts; const captured: Captured[] = []; const upstream = http.createServer((req, res) => { const chunks: Buffer[] = []; @@ -40,11 +53,16 @@ async function startHarness(routes: ProxyOptions["routes"] | undefined): Promise let body: Record = {}; try { body = JSON.parse(Buffer.concat(chunks).toString("utf8")); } catch { /* keep {} */ } captured.push({ headers: req.headers, body }); + if (rejectPck && "prompt_cache_key" in body) { + res.writeHead(400, { "content-type": "application/json" }); + res.end(JSON.stringify({ error: { message: "Validation: Unsupported parameter(s): `prompt_cache_key`", type: "Bad Request", code: 400 } })); + return; + } res.writeHead(200, { "content-type": "text/event-stream", "cache-control": "no-cache" }); res.end(chatSse()); }); }); - upstream.listen(0, "127.0.0.1"); + upstream.listen(0, bindAddress); await once(upstream, "listening"); const upstreamPort = (upstream.address() as { port: number }).port; @@ -103,23 +121,22 @@ function chatBody(extra: Record = {}): Record test("#2218 unit: shouldStampRelayAffinityPck gate matrix", () => { const loop = "http://127.0.0.1:7864/v1"; const remote = "https://relay.example.com/v1"; - // #2649: plugin lane + no client pck + rewritten route to a REMOTE - // destination → NO stamp (a rewritten/embedded URL is not relay - // evidence — the /bili/ tunnel rewrites the user's own remote - // destination too; verbatim stamps 400'd strict upstreams, #2645) - assert.equal(shouldStampRelayAffinityPck("x-bili-plugin-conversation", undefined, loop, remote), false); - // plugin lane + no client pck + loopback destination without a route → stamp - assert.equal(shouldStampRelayAffinityPck("x-bili-plugin-conversation", undefined, undefined, loop), true); - // plugin lane + no client pck + unrouted non-loopback (strict direct API risk, #1403) → no stamp - assert.equal(shouldStampRelayAffinityPck("x-bili-plugin-conversation", undefined, undefined, remote), false); + // #2649: plugin lane + no client pck + non-loopback destination → NO + // stamp. A rewritten/embedded route URL is deliberately NOT an input to + // this gate (the /bili/ tunnel rewrites the user's own remote + // destination too; verbatim stamps 400'd strict upstreams, #2645), so + // loopback is the only destination-shape proof of a relay. + assert.equal(shouldStampRelayAffinityPck("x-bili-plugin-conversation", undefined, remote), false); + // plugin lane + no client pck + loopback destination → stamp + assert.equal(shouldStampRelayAffinityPck("x-bili-plugin-conversation", undefined, loop), true); // client's own prompt_cache_key always wins - assert.equal(shouldStampRelayAffinityPck("x-bili-plugin-conversation", "pck-own", loop, loop), false); + assert.equal(shouldStampRelayAffinityPck("x-bili-plugin-conversation", "pck-own", loop), false); // non-plugin identity sources (codex session-id header, plain clients) stay unstamped - assert.equal(shouldStampRelayAffinityPck("session-id", undefined, loop, loop), false); - assert.equal(shouldStampRelayAffinityPck(undefined, undefined, loop, loop), false); - assert.equal(shouldStampRelayAffinityPck("x-bili-plugin-conversation", undefined, undefined, "http://localhost:7864/v1"), true); + assert.equal(shouldStampRelayAffinityPck("session-id", undefined, loop), false); + assert.equal(shouldStampRelayAffinityPck(undefined, undefined, loop), false); + assert.equal(shouldStampRelayAffinityPck("x-bili-plugin-conversation", undefined, "http://localhost:7864/v1"), true); // unparseable origin is not a deliberate deployment → no stamp - assert.equal(shouldStampRelayAffinityPck("x-bili-plugin-conversation", undefined, undefined, "not-a-url"), false); + assert.equal(shouldStampRelayAffinityPck("x-bili-plugin-conversation", undefined, "not-a-url"), false); }); test("#2218 regression: plain client body pck still forwards x-session-id + body pck", async () => { @@ -160,10 +177,10 @@ test("#2218 fix: dsh plugin lane reaches relays with BOTH header and body sessio }); test("#2218 fix: dsh plugin lane stamps pck on a loopback destination even without a configured route", async () => { - // The /bili/-embedded-URL form always resolves a route (rewrittenUrl set), - // so this case pins the gate's route-rewrite leg with an EMPTY admin - // provider table; the bare loopback leg (route undefined + loopback - // origin) is pinned by the unit matrix above. + // Pins the /bili/-tunnel lane end-to-end with an EMPTY admin provider + // table: the tunnel rewrites the user's own destination too (#2645), so + // the stamp here comes from the LOOPBACK destination shape, not from a + // rewritten route; the bare loopback leg is pinned by the unit matrix. const h = await startHarness(undefined); try { const base = `http://127.0.0.1:${h.proxyPort}/bili/http://127.0.0.1:${h.upstreamPort}/v1/chat/completions`; @@ -220,3 +237,75 @@ test("#2218 regression: readable client conversation headers still suppress the await h.close(); } }); + +// #2645: a dsh plugin-lane request tunneled to a REMOTE (non-loopback) +// destination must NOT carry a stamped prompt_cache_key — strict-schema +// upstreams (NVIDIA NIM) answer 400 on the unknown field. A machine +// non-loopback IPv4 address stands in for the remote host: bili sees a +// remote destination while the socket still lands on this host. +function nonLoopbackIPv4(): string | undefined { + const ifaces = os.networkInterfaces(); + for (const name of Object.keys(ifaces)) { + for (const i of ifaces[name] ?? []) { + if (i.family === "IPv4" && !i.internal) return i.address; + } + } + return undefined; +} + +test("#2645 regression: dsh plugin lane to a strict REMOTE upstream is not stamped (no 400)", async (t) => { + const destIp = nonLoopbackIPv4(); + if (!destIp) { t.skip("no non-loopback IPv4 interface available in this environment"); return; } + const h = await startHarness(undefined, { bindAddress: destIp, rejectPck: true }); + try { + const base = `http://127.0.0.1:${h.proxyPort}/bili/http://${destIp}:${h.upstreamPort}/v1/chat/completions`; + const r = await fetch(base, { + method: "POST", + headers: { "content-type": "application/json", "x-bili-plugin": "dsh", "x-bili-plugin-conversation": "dsh-nvidia-1" }, + body: JSON.stringify(chatBody()), + }); + // Before the fix bili stamped prompt_cache_key → the strict upstream 400'd. + assert.equal(r.status, 200); + await r.text(); + const [b] = h.captured; + assert.ok(b, "request reached the strict upstream"); + assert.ok(!("prompt_cache_key" in b.body), "prompt_cache_key must NOT be stamped onto a remote destination"); + // header relay still works (workbuddy hub / sub2api read x-session-id) + assert.equal(b.headers["x-session-id"], "dsh-nvidia-1"); + } finally { + await h.close(); + } +}); + +test("#2645/#3556 regression: client-supplied prompt_cache_key is stripped on non-relay chat egress", async (t) => { + const destIp = nonLoopbackIPv4(); + if (!destIp) { t.skip("no non-loopback IPv4 interface available in this environment"); return; } + const h = await startHarness(undefined, { bindAddress: destIp }); + try { + const base = `http://127.0.0.1:${h.proxyPort}/bili/http://${destIp}:${h.upstreamPort}/v1/chat/completions`; + // plain client: the key was forwarded verbatim before this fix; now it + // is stripped from the body while the x-session-id relay survives + const r1 = await fetch(base, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(chatBody({ prompt_cache_key: "pck-client-remote" })) }); + assert.equal(r1.status, 200); + await r1.text(); + const [a] = h.captured; + assert.ok(!("prompt_cache_key" in a.body), "client prompt_cache_key must be stripped from non-relay chat egress"); + assert.equal(a.headers["x-session-id"], "pck-client-remote"); + // dsh plugin lane: "client value wins" means no STAMP — it does not mean + // the client's key rides verbatim onto a strict remote API either + const r2 = await fetch(base, { + method: "POST", + headers: { "content-type": "application/json", "x-bili-plugin": "dsh", "x-bili-plugin-conversation": "dsh-sid-remote" }, + body: JSON.stringify(chatBody({ prompt_cache_key: "pck-dsh-remote" })), + }); + assert.equal(r2.status, 200); + await r2.text(); + const c = h.captured[1]; + assert.ok(!("prompt_cache_key" in c.body), "client prompt_cache_key on the dsh lane must also be stripped from non-relay egress"); + // the plugin conversation header outranks the body pck for identity — + // the point here is the STRIP, not which value wins the session id + assert.equal(c.headers["x-session-id"], "dsh-sid-remote"); + } finally { + await h.close(); + } +}); From 0d185808bab73931bf2da645cd8709473a86a76d Mon Sep 17 00:00:00 2001 From: awork Date: Sat, 17 Oct 2026 20:44:06 +0800 Subject: [PATCH 4/5] fix(launcher): mirror OMP proxy truthiness in PI_PROXY_ANTHROPIC fill guard Review nit on #3556: OMP's getProxyForProvider treats an empty-string PI_PROXY_ANTHROPIC and a whitespace-only PI_PROXY as unset, but the === undefined guard counted them as user-set, leaving Cowork on node:https direct (the stamped-body 400 class this PR exists to kill). The guard now mirrors the src/agent/pi.ts predicate exactly. --- src/launcher.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/launcher.ts b/src/launcher.ts index 2c559e5a24..6215226590 100644 --- a/src/launcher.ts +++ b/src/launcher.ts @@ -5037,8 +5037,11 @@ export async function runLaunch(params: RunLaunchParams, deps: LauncherDeps = {} // loopback HTTP — no MITM CA involved; the proxy re-originates TLS). // Verified live: Claude then rides the compressible lane and the // anthropic pck scrub keeps the strict schema clean. Only fill the - // gap — never override a user-set proxy posture. - if (env.PI_PROXY_ANTHROPIC === undefined && env.PI_PROXY === undefined) { + // gap — never override a user-set proxy posture. Truthiness mirrors + // OMP's own getProxyForProvider exactly (raw for PI_PROXY_ANTHROPIC, + // trimmed for PI_PROXY), so an empty/whitespace user value counts as + // unset and gets the routing, same as OMP would treat it. + if ((env.PI_PROXY_ANTHROPIC ?? "").length === 0 && (env.PI_PROXY ?? "").trim().length === 0) { env.PI_PROXY_ANTHROPIC = origin; } // #1774: the child always rides bili's proxy here, so a long preflight can From 36e073ec2b3843df4e175d4cb6a37643918ef577 Mon Sep 17 00:00:00 2001 From: awork Date: Sat, 17 Oct 2026 20:44:06 +0800 Subject: [PATCH 5/5] docs: sync omp Cowork caveat with launcher routing (#2583/#3556) README en/zh caveat paragraph, TECHNICAL-NOTES en/zh paragraph (heading omp native -> omp; launcher completion sentence; cert-MITM hoist), CONFIGURATION en/zh (client-table omp row + env appendix PI_PROXY_ANTHROPIC row): the default bili omp launcher lane now routes Cowork through the proxy via PI_PROXY_ANTHROPIC; direct-and-anonymous remains only for native mode and user-pinned proxy postures. --- CONFIGURATION.md | 3 ++- CONFIGURATION.zh-CN.md | 3 ++- README.md | 2 +- README.zh-CN.md | 2 +- TECHNICAL-NOTES.md | 2 +- TECHNICAL-NOTES.zh-CN.md | 2 +- 6 files changed, 8 insertions(+), 6 deletions(-) diff --git a/CONFIGURATION.md b/CONFIGURATION.md index e91e6d2da7..6aee878ab1 100644 --- a/CONFIGURATION.md +++ b/CONFIGURATION.md @@ -1847,6 +1847,7 @@ File keys resolve only when the matching env var is unset. Defaults in parenthes | `BILI_SESSION_GC_INTERVAL_MS` | Background GC sweep interval in ms (default `3600000` = 1h). A sweep also runs once at boot. | | `BILI_ENCRYPTION_KEY` | Encrypt session files at rest (#708) for deployments on untrusted nodes. Exactly 32 bytes, hex (64 chars) or base64; unset = unencrypted plain-JSON files (or `BILIZSTD1` when `BILI_PERSIST_ZSTD=1` — see `BILI_PERSIST_ZSTD`). When set: every session file is written as `BILIENC1` AES-256-GCM over JSON that is zstd-compressed only when `BILI_PERSIST_ZSTD=1` (zstd on Node ≥ 22.15, raw body otherwise) — enabling compression also shrinks the file ~5–10×. Encryption and compression are now independent knobs (#1080). The key comes ONLY from this environment variable — never written to disk or logs — so keep it out of the same filesystem's reach. Invalid values abort startup (fail fast, never silently unencrypted). Booting with the wrong key skips the affected sessions as corrupt (logged, no crash). Losing the key makes encrypted sessions permanently unreadable. Symmetric by design (the same process encrypts and decrypts). Existing unencoded files are never rewritten at boot — they encrypt organically on their next save (downgrade safety; see `BILI_PERSIST_ZSTD`). Threat model (#708, owner-confirmed): it defends against **offline/mechanical** file acquisition — provider disk swaps, offline disk snapshots after node-image drift, stolen volumes, leaked backups, cloud-synced state dirs — where an offline third party cannot read the content without the key. It does NOT defend against targeted adversaries with live access to the node; for that tier move the trust root out of the proxy (KMS / TEE / confidential VMs, hardened permissions) instead of solving it inside the proxy — at that level far more than the key is exposed, so proxy-level key handling is not the boundary to hold (`BILI_PERSIST=0` disables persistence entirely). Double-encrypting the key with a second key from the same process/environment adds nothing: in every offline-theft scenario the attacker lacks exactly one artifact — your off-disk secret — whether it is called the data key or the wrapping key; only a wrapping key in a different trust domain (KMS/TPM/TEE) raises the bar, and that is scenario 2 above. | | `BILLION_CONTEXT_PROXY` | Exported by the launcher; client-side bili plugins/extensions detect it and self-disable their own compression (no double compression). | +| `PI_PROXY_ANTHROPIC` | Exported by the `bili omp` launcher (only when neither `PI_PROXY` nor `PI_PROXY_ANTHROPIC` comes from the user's environment): points OMP's Cowork anthropic transport at the proxy so those requests ride the compressible lane instead of node:https direct (#2583/#3556). User-set values always win. | | `BILLION_CONTEXT_PLUGIN` | Set `0` to disable plugin mode entirely (wire-level tool injection resumes). | | `BILI_LAUNCHER_MODEL_WINDOWS` | Internal: the launcher hands the client's own per-model context windows (pi `models.json`, omp `models.yml`, opencode `models..limit`, codex `model_context_window`) to the spawned proxy as JSON, so the nudge denominator matches the real window for self-hosted models. Only the launcher sets it — no user configuration. | | `BILI_LAUNCHER_LANE` | Internal: the launcher hands its client's lane name (pi / codex / claude / …) to the spawned proxy, which records it in the instance file (#1225). Reuse is identity-based: two instances with *different declared* lanes never attach to each other; an instance without a declared lane is a manually-started user-zone daemon — attachable by default (#1660). Only the launcher sets it — no user configuration. | @@ -2076,7 +2077,7 @@ How each client is pointed at the proxy (set automatically in the child env): | Client | Redirect | CA trust | |---|---|---| | pi | `HTTPS_PROXY` + `BILI_PROVIDER_REWRITES` env manifest (extension `registerProvider`) | `NODE_EXTRA_CA_CERTS` | -| omp | `HTTPS_PROXY` + `BILI_PROVIDER_REWRITES` env manifest (extension `registerProvider`) | `NODE_EXTRA_CA_CERTS` | +| omp | `HTTPS_PROXY` + `BILI_PROVIDER_REWRITES` env manifest (extension `registerProvider`) + `PI_PROXY_ANTHROPIC=` when neither `PI_PROXY` nor `PI_PROXY_ANTHROPIC` is user-set (routes OMP's Cowork anthropic lane through the proxy instead of node:https direct, #2583/#3556) | `NODE_EXTRA_CA_CERTS` | | codex | `HTTPS_PROXY` + `-c key=value` overrides | `SSL_CERT_FILE` → `combined-ca.pem` | | claude | `ANTHROPIC_BASE_URL` = `/bili/` URL (Vertex: `ANTHROPIC_VERTEX_BASE_URL`, when `CLAUDE_CODE_USE_VERTEX=1`) | none needed | | opencode | `HTTPS_PROXY` + isolated `OPENCODE_CONFIG` | `NODE_EXTRA_CA_CERTS` | diff --git a/CONFIGURATION.zh-CN.md b/CONFIGURATION.zh-CN.md index f45422c0ca..ba06e9ca51 100644 --- a/CONFIGURATION.zh-CN.md +++ b/CONFIGURATION.zh-CN.md @@ -1843,6 +1843,7 @@ ACP 原生 agent(当前为 `pi` 扩展)会在每个进程内向代理上报 | `BILI_SESSION_GC_INTERVAL_MS` | 后台清理扫描间隔(毫秒,默认 `3600000` = 1 小时)。启动时会先扫一次。 | | `BILI_ENCRYPTION_KEY` | 会话文件静态加密(#708),适用于部署在不可信节点的场景。密钥必须恰好 32 字节,hex(64 字符)或 base64;未设置 = 不加密的纯 JSON 文件(设 `BILI_PERSIST_ZSTD=1` 时为 `BILIZSTD1`——参见 `BILI_PERSIST_ZSTD`)。设置后:每个会话文件均以 `BILIENC1` 格式写入,即对 JSON 施加 AES-256-GCM 加密,JSON 仅在 `BILI_PERSIST_ZSTD=1` 时以 zstd 压缩(Node ≥ 22.15 使用 zstd,其余情况写入原始数据)——启用压缩还可将文件体积缩小约 5–10 倍。加密与压缩现为独立的配置项(#1080)。密钥只从该环境变量读取——永不落盘、永不进日志——请确保它不受同一文件系统上的其他进程触及。非法值会导致启动中止(快速失败,绝不静默明文运行)。用错误的密钥启动时,受影响的会话按损坏文件跳过(有日志,不崩溃)。丢失密钥将使已加密的会话永久不可读。对称加密为刻意设计(同一进程既加密又解密)。已有的未编码文件从不在启动时改写——在其下一次保存时自然加密(降级安全;参见 `BILI_PERSIST_ZSTD`)。威胁模型(#708,owner 确认):防的是**离线/机械性**的文件获取——云厂商换盘、节点镜像漂移后的离线磁盘快照、磁盘镜像失窃、备份泄露、被云同步的状态目录——离线第三方拿不到密钥即无法读取内容。不防御对活节点有访问权的定向攻击者;那一档应把信任根移出 proxy(KMS / TEE / 机密虚拟机 + 强化权限体系),而不是在 proxy 本身想办法——到了那个程度暴露的远不止密钥,proxy 层不是该守的边界(`BILI_PERSIST=0` 可彻底关闭持久化)。用同一进程/环境中的第二把密钥对密钥做二次加密不增加任何安全性:所有离线失窃场景里攻击者缺的始终只有一个工件——你的非落盘秘密——无论它叫数据密钥还是包裹密钥;只有把包裹密钥放进不同信任域(KMS/TPM/TEE)才能提高门槛,而那属于上面的场景 2。 | | `BILLION_CONTEXT_PROXY` | launcher 会导出它;客户端侧 bili 插件/扩展检测到后自禁用自身压缩(避免双重压缩)。 | +| `PI_PROXY_ANTHROPIC` | 由 `bili omp` launcher 导出(仅当 `PI_PROXY` 与 `PI_PROXY_ANTHROPIC` 均未来自用户环境时):把 OMP 的 Cowork anthropic 传输指向代理,让这些请求走可压缩通道而非 node:https 直连(#2583/#3556)。用户设置的值始终优先。 | | `BILLION_CONTEXT_PLUGIN` | 设 `0` 彻底关闭插件模式(恢复 wire 层工具注入)。 | | `BILI_LAUNCHER_MODEL_WINDOWS` | 内部使用:launcher 把客户端自身配置里的逐模型上下文窗口(pi `models.json`、omp `models.yml`、opencode `models..limit`、codex `model_context_window`)以 JSON 传给自己拉起的代理,让 nudge 分母对自托管模型也用真实窗口。只有 launcher 会设置,无需用户配置。 | | `BILI_LAUNCHER_PLUGIN` | 设 `0` 关闭 launcher 为 claude/codex 注入 bili MCP 服务器(退回纯 wire 模式);设 `1` 强制插件模式。默认注入——但 codex 上游为本地/私网地址时自动退回 wire 模式(sglang/vllm/ollama 不解析 codex 的 namespace 工具类型)。见[启动器参考](#启动器参考)。 | @@ -2051,7 +2052,7 @@ MITM 只对一份**白名单**中的模型域名生效(`open.bigmodel.cn`、`a | 客户端 | 重定向方式 | CA 信任 | |---|---|---| | pi | `HTTPS_PROXY` + `BILI_PROVIDER_REWRITES` env 清单(扩展 `registerProvider`) | `NODE_EXTRA_CA_CERTS` | -| omp | `HTTPS_PROXY` + `BILI_PROVIDER_REWRITES` env 清单(扩展 `registerProvider`) | `NODE_EXTRA_CA_CERTS` | +| omp | `HTTPS_PROXY` + `BILI_PROVIDER_REWRITES` env 清单(扩展 `registerProvider`)+ 用户未设置 `PI_PROXY` / `PI_PROXY_ANTHROPIC` 时的 `PI_PROXY_ANTHROPIC=`(将 OMP 的 Cowork anthropic 通道经代理路由而非 node:https 直连,#2583/#3556) | `NODE_EXTRA_CA_CERTS` | | codex | `HTTPS_PROXY` + `-c key=value` 覆盖 | `SSL_CERT_FILE` → `combined-ca.pem` | | claude | `ANTHROPIC_BASE_URL` = `/bili/` URL(Vertex:`ANTHROPIC_VERTEX_BASE_URL`,当 `CLAUDE_CODE_USE_VERTEX=1`) | 无需 | | opencode | `HTTPS_PROXY` + 隔离 `OPENCODE_CONFIG` | `NODE_EXTRA_CA_CERTS` | diff --git a/README.md b/README.md index e022fa67b9..f51e709446 100644 --- a/README.md +++ b/README.md @@ -520,7 +520,7 @@ Responses/OpenAI/Anthropic wires. | **omp** (via plugin) | ✅ yes | `prompt_cache_key` promoted over any fingerprint (#268) | | **pi** (bare) | ❌ no | nothing → anonymous prefix affinity below | -**omp caveat (#2583):** on recent OMP builds, default Anthropic (`anthropic-messages`) requests leave the host through OMP's Cowork transport over node:https directly — they never reach bili's fetch layer, so the `prompt_cache_key` stamp is *withheld* there (stamping would leak the field into Anthropic's strict schema → HTTP 400). Those requests run direct: uncompressed and anonymous to the proxy; all other omp traffic keeps its stamped identity. Details: [TECHNICAL-NOTES.md](TECHNICAL-NOTES.md). +**omp caveat (#2583/#3556):** on recent OMP builds, default Anthropic (`anthropic-messages`) requests leave the host through OMP's Cowork transport over node:https directly — they never reach bili's fetch layer, so the `prompt_cache_key` stamp is *withheld* there (stamping would leak the field into Anthropic's strict schema → HTTP 400). Since #3556 the `bili omp` launcher lane no longer leaves that hole open by default: unless you set `PI_PROXY` / `PI_PROXY_ANTHROPIC` yourself, the launcher exports `PI_PROXY_ANTHROPIC=`, which pushes Cowork onto Bun's fetch and routes it through the proxy like every other lane (compressed and session-visible; the stamped key is consumed and stripped by the proxy). Direct-and-anonymous remains only for native omp mode and for launcher lanes where you pin your own proxy posture; all other omp traffic keeps its stamped identity. Details: [TECHNICAL-NOTES.md](TECHNICAL-NOTES.md). **Header-less clients (pi-like): anonymous prefix affinity.** With no conversation signal at all, the proxy resolves the session from the replayed history itself (`src/prefix-affinity.ts`, #309): a request reattaches to a stored session only when its history reproduces that session's message chain byte-exactly from position 0; otherwise it gets its own deterministic `pfa-…` session. Consequences (#1262): a **resumed** conversation reattaches to its own session (even after a proxy restart, #499); a **new task with an identical opener does NOT inherit** another conversation's blocks — it mints a fresh, fully separate session; a request with no usable signal at all gets an explicit 400 instead of silently colliding. diff --git a/README.zh-CN.md b/README.zh-CN.md index da870da7a6..52e5cb1486 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -402,7 +402,7 @@ bili --no-auto-update # 本次启动禁用自动更新 | **omp**(经插件)| ✅ 发 | `prompt_cache_key` 提升为稳定身份(#268)| | **pi**(裸跑)| ❌ 不发 | 无 → 见下方匿名前缀亲和 | -**omp 注意(#2583):** 较新的 OMP 构建中,默认 Anthropic(`anthropic-messages`)请求经 OMP 的 Cowork 传输以 node:https **直发** —— 到不了 bili 的 fetch 层,因此那条路径上 `prompt_cache_key` 打戳被*主动放弃*(打了戳反而会把字段泄漏进 Anthropic 严格 schema → HTTP 400)。这些请求走直连:不经压缩、对代理匿名;其余 omp 流量保持打戳身份。详见 [TECHNICAL-NOTES.zh-CN.md](TECHNICAL-NOTES.zh-CN.md)。 +**omp 注意(#2583/#3556):** 较新的 OMP 构建中,默认 Anthropic(`anthropic-messages`)请求经 OMP 的 Cowork 传输以 node:https **直发** —— 到不了 bili 的 fetch 层,因此那条路径上 `prompt_cache_key` 打戳被*主动放弃*(打了戳反而会把字段泄漏进 Anthropic 严格 schema → HTTP 400)。自 #3556 起,`bili omp` launcher lane 默认不再留这个洞:除非你自己设置了 `PI_PROXY` / `PI_PROXY_ANTHROPIC`,launcher 会导出 `PI_PROXY_ANTHROPIC=`,把 Cowork 推回 Bun 的 fetch、像其余 lane 一样经代理路由(可压缩、会话可见;打上的 key 由代理消费并剥离)。直连且匿名的行为只保留在 native omp 模式、以及你自己钉死代理姿态的 launcher lane;其余 omp 流量保持打戳身份。详见 [TECHNICAL-NOTES.zh-CN.md](TECHNICAL-NOTES.zh-CN.md)。 **无 header 客户端(pi 类):匿名前缀亲和。** 当客户端完全不发任何会话信号时,代理从重放的历史本身解析会话(`src/prefix-affinity.ts`,#309):只有当请求历史从第 0 条开始逐字节复现某已存会话的消息链时,才重新挂回该会话;否则获得一个确定性的新 `pfa-…` 会话。后果(#1262):**恢复(resume)** 的对话重新挂回自己的会话(包括代理重启后,#499);**开头相同的新任务不会继承**另一个会话的 block —— 它拿到全新会话,历史一旦分叉就彻底独立;完全没有任何可用信号时,请求会被显式 400 拒绝,而不是静默与他人状态碰撞。 diff --git a/TECHNICAL-NOTES.md b/TECHNICAL-NOTES.md index 78f7909dc0..c3a61a9a13 100644 --- a/TECHNICAL-NOTES.md +++ b/TECHNICAL-NOTES.md @@ -105,7 +105,7 @@ follow the same protocol. **DSH `maxOutput` reporting is declaration-gated — an undeclared custom model empties the whole ceiling chain (#2904).** `src/agent/dsh-native.ts` forwards `maxOutput` **only** when the host's `resolveModelInfo(provider, model)` returns a positive `defaultMaxTokens`, and DSH populates that **only** when the model item explicitly declares `maxTokens` — provider-level output defaults are never reported. For a user-added custom model with no declared `maxTokens`, `defaultMaxTokens` is `undefined`, so BOTH runtime-info channels come back empty. With no models.dev entry and no bili-configured `output` either, the four-source ceiling chain (#1840) resolves to nothing, and `restoreOutputBudget` lifts a client-starved budget to the session high-water mark — which on a long session has decayed through small positives to a poisoned low value. Every turn then runs reasoning-only and stops at `length`/`max-tokens`; switching to a model WITH a known ceiling masks it (the high-water is floored up to the ceiling), so the switch is a trigger, not a cause. The real fix home is **DSH-side**: report its configured provider/model output default instead of `undefined` (the immediate user workaround is setting `maxTokens` on the model item). bili deliberately does not guess a quota floor (#1842); instead #2904 surfaces the degraded state persistently on the `/acp` panel, the Web UI session page (data source `/__bili/sessions//detail`), and `/__bili/plugin/status?conversationId=` (`outputBudgetDegraded`) — clearing automatically once a healthy value or ceiling arrives — and extends `degenerateTurnWarning` to `length`/max-output terminations so the truncation correlates with its cause in the log. -**omp native: the `prompt_cache_key` stamp is withheld when OMP's Cowork transport owns egress (#2583).** In native omp mode the stamped `prompt_cache_key` is the only per-request session signal, and its sole consumer is bili's own fetch-layer interceptor (it rewrites model URLs to `/bili/` and strips the field before forwarding). Recent OMP builds ship a "Cowork transport" (introduced 2026-07-30, can1357/oh-my-pi `642892ae`; proxy handling `e22a5ff2`) that sends default `anthropic`/`anthropic-messages` requests over node:https DIRECTLY — they never touch `globalThis.fetch` unless an explicit proxy env var (`PI_PROXY` / `PI_PROXY_ANTHROPIC`) pushes them back onto Bun's fetch. Stamping there would ride the field verbatim into Anthropic's strict schema (HTTP 400 `prompt_cache_key: Extra inputs are not permitted`). The stamp gate (`stampPromptCacheKey` → `ompCoworkTransportBypassesFetch` in `src/agent/pi.ts`) mirrors OMP's own per-model egress selection: proven-direct traffic is unstamped, with one loud warning per process per model. Consequence: those direct requests go uncompressed and anonymous to the proxy (identity/panel/billing degrade — the same trade as #1403); every other lane (launcher `/bili/` rewrite, MITM whitelist, other providers/apis, proxy-configured hosts) is unchanged. Known residual: a `NO_PROXY`/`no_proxy` rule matching the model host silences OMP's proxy handling even when `PI_PROXY` / `PI_PROXY_ANTHROPIC` are set — the request then dials direct while still stamped (the same 400 class); remove such hosts from `NO_PROXY` instead. The reverse asymmetry is fail-safe: OMP's shape-based fetch fallbacks (Request-object input, non-record headers, unsupported body) route back onto `globalThis.fetch` even with no proxy envs, where a withheld stamp only degrades identity and never hard-fails. Full coverage would require a host-side change (route Cowork egress through `globalThis.fetch`) — boundary note in #2583. +**omp: the `prompt_cache_key` stamp is withheld when OMP's Cowork transport owns egress (#2583/#3556).** In native omp mode the stamped `prompt_cache_key` is the only per-request session signal, and its sole consumer is bili's own fetch-layer interceptor (it rewrites model URLs to `/bili/` and strips the field before forwarding). Recent OMP builds ship a "Cowork transport" (introduced 2026-07-30, can1357/oh-my-pi `642892ae`; proxy handling `e22a5ff2`) that sends default `anthropic`/`anthropic-messages` requests over node:https DIRECTLY — they never touch `globalThis.fetch` unless an explicit proxy env var (`PI_PROXY` / `PI_PROXY_ANTHROPIC`) pushes them back onto Bun's fetch. Since #3556 the `bili omp` launcher closes this gap itself: unless the user has set `PI_PROXY` / `PI_PROXY_ANTHROPIC`, it exports `PI_PROXY_ANTHROPIC=`, so on the default launcher lane Cowork rides the compressible lane (the stamped key consumed and stripped by the proxy) instead of dialing direct. The withhold also gates the cert-MITM lane — #3556 hoisted it above the destination-routed check, where a nested placement skipped it entirely. Stamping there would ride the field verbatim into Anthropic's strict schema (HTTP 400 `prompt_cache_key: Extra inputs are not permitted`). The stamp gate (`stampPromptCacheKey` → `ompCoworkTransportBypassesFetch` in `src/agent/pi.ts`) mirrors OMP's own per-model egress selection: proven-direct traffic is unstamped, with one loud warning per process per model. Consequence: those direct requests go uncompressed and anonymous to the proxy (identity/panel/billing degrade — the same trade as #1403); every other lane (launcher `/bili/` rewrite, MITM whitelist, other providers/apis, proxy-configured hosts) is unchanged. Known residual: a `NO_PROXY`/`no_proxy` rule matching the model host silences OMP's proxy handling even when `PI_PROXY` / `PI_PROXY_ANTHROPIC` are set — the request then dials direct while still stamped (the same 400 class); remove such hosts from `NO_PROXY` instead. The reverse asymmetry is fail-safe: OMP's shape-based fetch fallbacks (Request-object input, non-record headers, unsupported body) route back onto `globalThis.fetch` even with no proxy envs, where a withheld stamp only degrades identity and never hard-fails. Full coverage would require a host-side change (route Cowork egress through `globalThis.fetch`) — boundary note in #2583. The launcher env tier covers pure-proxy clients (no in-process plugin): `bili ` reads the client's own model config at launch diff --git a/TECHNICAL-NOTES.zh-CN.md b/TECHNICAL-NOTES.zh-CN.md index 7293c350b6..7ba7a7bf40 100644 --- a/TECHNICAL-NOTES.zh-CN.md +++ b/TECHNICAL-NOTES.zh-CN.md @@ -41,7 +41,7 @@ hook 附着前先探测候选者的 `/__bili/health`:armed → 附着并注册 w 窗口解析顺序:`anthropic-beta` 协商 > 逐请求 plugin 头 > runtime-info > launcher 环境变量 > 路由配置 > models.dev 注册表 > 内置表。runtime-info 这一步:带 `x-bili-plugin` 头的请求读**按 agent 的条目**(agent+model 必须匹配);不带该头的请求解析以 `conversationId` 记录的**会话级条目**,键与会话绑定的同一会话信号一致(客户端会话头、自定义 session 头或请求体的 `prompt_cache_key`)—— 无论哪种,model 都必须匹配(#1531:omp 打 `prompt_cache_key` 但不打 plugin 头,且主/子代理会话共用 agent 名却跑不同模型)。上报的 `maxOutput` 仅在请求体自带输出预算缺席时兜底。现有实现:`src/agent/pi.ts`(覆盖 pi 与 omp)、`src/agent/opencode-native.ts`(v1)、`src/agent/opencode-v2.ts`、`src/agent/dsh-native.ts`、`src/kimi/native-mcp.ts`(仅自举时上报 —— kimi 的 provider `custom_headers` 是静态的,逐请求头会在模型切换后过期)、`hermes-plugin/__init__.py`(Python 插件:经 `llm_request` 中间件打逐请求头,`pre_api_request` hook 捕获最大输出与宿主 per-model 上下文窗口 —— 窗口读自 hermes 自己的 `HERMES_HOME/context_length_cache.yaml`,任何缺失/歧义 fail-open(#3261))—— 其他客户端接入请遵循同一协议。 -**omp native:当 OMP 的 Cowork 传输接管出口时,不打 `prompt_cache_key`(#2583)。**native omp 模式下,打上的 `prompt_cache_key` 是唯一的逐请求会话信号,其唯一消费者是 bili 自己的 fetch 层拦截器(把模型 URL 改写成 `/bili/` 并在转发前剥掉该字段)。较新的 OMP 构建带有 "Cowork 传输"(2026-07-30 引入,can1357/oh-my-pi `642892ae`;代理处理 `e22a5ff2`):默认 `anthropic`/`anthropic-messages` 请求经 node:https **直发** —— 除非显式代理环境变量(`PI_PROXY` / `PI_PROXY_ANTHROPIC`)把它们推回 Bun 的 fetch,否则根本不经过 `globalThis.fetch`。在那条路径上打戳会让字段原样进入 Anthropic 严格 schema(HTTP 400 `prompt_cache_key: Extra inputs are not permitted`)。打戳闸门(`src/agent/pi.ts` 的 `stampPromptCacheKey` → `ompCoworkTransportBypassesFetch`)镜像 OMP 自己的按模型出口选择:证明直发时不打戳,并每进程每模型警告一次。后果:这些直发请求不经压缩、对代理匿名(身份/面板/账单退化 —— 与 #1403 同一取舍);其余 lane(launcher `/bili/` 改写、MITM 白名单、其他 provider/api、配置了代理的宿主)不受影响。已知残留:`NO_PROXY`/`no_proxy` 规则命中模型 host 时,即使设置了 `PI_PROXY` / `PI_PROXY_ANTHROPIC`,OMP 的代理处理也会被静默 —— 请求直发但已打戳(同类 400);这类 host 应从 `NO_PROXY` 中移除而非依赖打戳闸门。反向不对称是 fail-safe 的:OMP 的按形状 fetch 回退(Request 对象入参、非 record headers、不支持的 body)即使没有任何代理环境变量也会走回 `globalThis.fetch`,那里放弃打戳只退化身份、不会硬失败。完整覆盖需要宿主侧变更(Cowork 出口改走 `globalThis.fetch`)—— 作为边界注记记录在 #2583。 +**omp:当 OMP 的 Cowork 传输接管出口时,不打 `prompt_cache_key`(#2583/#3556)。**native omp 模式下,打上的 `prompt_cache_key` 是唯一的逐请求会话信号,其唯一消费者是 bili 自己的 fetch 层拦截器(把模型 URL 改写成 `/bili/` 并在转发前剥掉该字段)。较新的 OMP 构建带有 "Cowork 传输"(2026-07-30 引入,can1357/oh-my-pi `642892ae`;代理处理 `e22a5ff2`):默认 `anthropic`/`anthropic-messages` 请求经 node:https **直发** —— 除非显式代理环境变量(`PI_PROXY` / `PI_PROXY_ANTHROPIC`)把它们推回 Bun 的 fetch,否则根本不经过 `globalThis.fetch`。自 #3556 起,`bili omp` 启动器自行关闭了这一缺口:除非用户已设置 `PI_PROXY` / `PI_PROXY_ANTHROPIC`,否则它会导出 `PI_PROXY_ANTHROPIC=`,因此在默认启动器通道上,Cowork 走可压缩通道(打上的 key 由代理消费并剥离),而非直发。打戳放弃同时门控 cert-MITM 通道 —— #3556 把它提到了目的地路由检查之上,嵌套放置会完全跳过它。在那条路径上打戳会让字段原样进入 Anthropic 严格 schema(HTTP 400 `prompt_cache_key: Extra inputs are not permitted`)。打戳闸门(`src/agent/pi.ts` 的 `stampPromptCacheKey` → `ompCoworkTransportBypassesFetch`)镜像 OMP 自己的按模型出口选择:证明直发时不打戳,并每进程每模型警告一次。后果:这些直发请求不经压缩、对代理匿名(身份/面板/账单退化 —— 与 #1403 同一取舍);其余 lane(launcher `/bili/` 改写、MITM 白名单、其他 provider/api、配置了代理的宿主)不受影响。已知残留:`NO_PROXY`/`no_proxy` 规则命中模型 host 时,即使设置了 `PI_PROXY` / `PI_PROXY_ANTHROPIC`,OMP 的代理处理也会被静默 —— 请求直发但已打戳(同类 400);这类 host 应从 `NO_PROXY` 中移除而非依赖打戳闸门。反向不对称是 fail-safe 的:OMP 的按形状 fetch 回退(Request 对象入参、非 record headers、不支持的 body)即使没有任何代理环境变量也会走回 `globalThis.fetch`,那里放弃打戳只退化身份、不会硬失败。完整覆盖需要宿主侧变更(Cowork 出口改走 `globalThis.fetch`)—— 作为边界注记记录在 #2583。 launcher 环境变量这档覆盖纯代理客户端(无进程内插件):`bili ` 启动时读客户端自己的模型配置(codex 的 `model_context_window` / `model_max_output_tokens`,pi / omp 的 `contextWindow` / `maxTokens`,opencode 的 `limit.context` / `limit.output`,codebuddy 的 `maxInputTokens` / `maxOutputTokens`),经 `BILI_LAUNCHER_MODEL_WINDOWS` / `BILI_LAUNCHER_MODEL_MAX_OUTPUTS` 交给代理(#971)。插件上报 —— 若存在 —— 永远优先于它。