Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,8 @@ 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).

**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.

Design record and threat model: [SESSION-IDENTITY.md](SESSION-IDENTITY.md). The
Expand Down
2 changes: 2 additions & 0 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,8 @@ 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)。

**无 header 客户端(pi 类):匿名前缀亲和。** 当客户端完全不发任何会话信号时,代理从重放的历史本身解析会话(`src/prefix-affinity.ts`,#309):只有当请求历史从第 0 条开始逐字节复现某已存会话的消息链时,才重新挂回该会话;否则获得一个确定性的新 `pfa-…` 会话。后果(#1262):**恢复(resume)** 的对话重新挂回自己的会话(包括代理重启后,#499);**开头相同的新任务不会继承**另一个会话的 block —— 它拿到全新会话,历史一旦分叉就彻底独立;完全没有任何可用信号时,请求会被显式 400 拒绝,而不是静默与他人状态碰撞。

设计记录与威胁模型:[SESSION-IDENTITY.md](SESSION-IDENTITY.md)。消息粒度的对偶文档(为什么身份由内容派生、而非指派 id)是 [MESSAGE-IDENTITY.zh-CN.md](MESSAGE-IDENTITY.zh-CN.md)。
Expand Down
2 changes: 2 additions & 0 deletions TECHNICAL-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ per-request headers via an `llm_request` middleware, max output captured by a
`pre_api_request` hook) — other client integrations should follow the same
protocol.

**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 `<proxy>/bili/<url>` 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.

The launcher env tier covers pure-proxy clients (no in-process plugin):
`bili <client>` reads the client's own model config at launch
(`model_context_window` / `model_max_output_tokens` for codex,
Expand Down
2 changes: 2 additions & 0 deletions TECHNICAL-NOTES.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ 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 捕获最大输出)—— 其他客户端接入请遵循同一协议。

**omp native:当 OMP 的 Cowork 传输接管出口时,不打 `prompt_cache_key`(#2583)。**native omp 模式下,打上的 `prompt_cache_key` 是唯一的逐请求会话信号,其唯一消费者是 bili 自己的 fetch 层拦截器(把模型 URL 改写成 `<proxy>/bili/<url>` 并在转发前剥掉该字段)。较新的 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。

launcher 环境变量这档覆盖纯代理客户端(无进程内插件):`bili <client>` 启动时读客户端自己的模型配置(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)。插件上报 —— 若存在 —— 永远优先于它。

首次模型请求之前会话尚不存在,`/acp` 面板会探测 `GET /__bili/plugin/status?conversationId=<agent>&fallback=latest`,代理从 runtime-info 表应答(`phase: "pre-first-request"`)而不是返回 404 —— 上报的配置立即可见,流量落地后由真实会话接管。
Expand Down
50 changes: 50 additions & 0 deletions src/agent/pi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,43 @@ export function replayedMessageCount(ctx: Ctx): number | undefined {
}
}

// #2583: OMP's Cowork transport (can1357/oh-my-pi 642892ae, proxy handling
// e22a5ff2) picks egress PER MODEL: default anthropic/anthropic-messages
// requests go out over node:https DIRECTLY — stable header order, HTTP/1.1,
// streaming decompression — and never touch globalThis.fetch, so the
// fetch-layer routing claim of the native mode does not hold for them. Only
// an explicit proxy setting (PI_PROXY / PI_PROXY_<PROVIDER>) pushes them back
// onto Bun's own fetch (which the patch then sees), as do exotic request
// shapes (Request input, non-record headers, non-string body, non-https).
// The stamped prompt_cache_key's sole consumer is the proxy itself
// (#1403/#1579); on the direct path the field rides verbatim into Anthropic's
// strict schema and 400s every request ("prompt_cache_key: Extra inputs are
// not permitted"). This mirrors the host's own selection predicate exactly
// (defaultFetchForModel + getProxyForProvider) — an evidence-permitlist entry
// with source-level proof, not a URL heuristic. Missing provider/api fields
// cannot conclude bypass, so they keep today's optimistic stamp (older hosts
// still route through globalThis.fetch — fail-safe toward #1579).
export function ompCoworkTransportBypassesFetch(
model: { provider?: string; api?: string } | undefined,
env: NodeJS.ProcessEnv = process.env,
): boolean {
if (model?.provider !== "anthropic" || model?.api !== "anthropic-messages") return false;
// 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;
}

// Once per process per model — the bypass is a property of the installed host
// build, not of individual requests, so repeating it per call would spam logs.
const coworkBypassNoted = new Set<string>();
function noteOmpCoworkBypass(modelId: string): void {
const key = modelId || "<unknown>";
if (coworkBypassNoted.has(key) || coworkBypassNoted.size >= 64) return;
coworkBypassNoted.add(key);
console.warn(`bili-plugin(omp): ${key} leaves this host through the host-native direct TLS transport (OMP Cowork transport, #2583) — those requests bypass bili's fetch interception and go DIRECT (uncompressed, invisible to the proxy). The prompt_cache_key identity stamp is withheld so the field can never leak into the upstream body.`);
}

// omp's chat-completions payloads carry NO conversation signal (no
// prompt_cache_key / session / user, and no session header — verified by dump),
// so the proxy's openai identity falls to a content fingerprint that never
Expand Down Expand Up @@ -432,6 +469,19 @@ 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).
if (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;
Expand Down
88 changes: 85 additions & 3 deletions tests/omp-native-identity.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import test from "node:test";
import assert from "node:assert/strict";
import { stampPromptCacheKey } from "../src/agent/pi.ts";
import { ompCoworkTransportBypassesFetch, stampPromptCacheKey } from "../src/agent/pi.ts";

// #1579: native omp mode routes at the fetch layer — ctx.model.baseUrl keeps
// the real upstream URL, so the launcher-shaped destination checks can never
Expand All @@ -14,6 +14,10 @@ import { stampPromptCacheKey } from "../src/agent/pi.ts";
// Review of #1586 added the live-claim requirement: the install flag alone
// persists after the interceptor degrades to direct sends, where a stamped
// body would ride verbatim into strict upstreams (#1403's exact 400).
// #2583 adds the transport carve-out: OMP's Cowork transport sends default
// anthropic/anthropic-messages traffic over node:https DIRECTLY (no proxy
// env) — the fetch-layer rewrite can never fire for those bytes, so the gate
// must mirror OMP's own per-model egress selection exactly.

const INTERCEPT_FLAG = Symbol.for("billion-context.native-fetch-intercept");
const PROXY_ORIGIN = "http://127.0.0.1:36009";
Expand Down Expand Up @@ -43,10 +47,33 @@ function withIntercept<T>(installed: boolean, fn: () => T): T {
}
}

function makeCtx(baseUrl: string): { model?: { baseUrl?: string }; sessionManager?: { getSessionId?: () => string } } {
return { model: { baseUrl }, sessionManager: { getSessionId: () => "sess-1579" } };
function withProxyEnv<T>(vars: Record<string, string | undefined>, fn: () => T): T {
const keys = Object.keys(vars);
const prev: Record<string, string | undefined> = {};
for (const k of keys) {
prev[k] = process.env[k];
const v = vars[k];
if (v === undefined) delete process.env[k];
else process.env[k] = v;
}
try {
return fn();
} finally {
for (const k of keys) {
const v = prev[k];
if (v === undefined) delete process.env[k];
else process.env[k] = v;
}
}
}

function makeCtx(baseUrl: string, extra?: Record<string, unknown>): { model?: { baseUrl?: string } & Record<string, unknown>; sessionManager?: { getSessionId?: () => string } } {
return { model: { baseUrl, ...extra }, sessionManager: { getSessionId: () => "sess-1579" } };
}

const COWORK_MODEL = { provider: "anthropic", api: "anthropic-messages" };
const NO_PROXY_ENV = { PI_PROXY: undefined, PI_PROXY_ANTHROPIC: undefined };

function makeEvent(body: Record<string, unknown>): unknown {
return { type: "before_provider_request", payload: { messages: [], ...body } };
}
Expand Down Expand Up @@ -113,3 +140,58 @@ test("a body without messages array is left alone", () => {
assert.equal(out, undefined);
}));
});

test("#2583: native omp anthropic-messages with no proxy env → NOT stamped (Cowork node:https path bypasses the fetch patch)", () => {
withIntercept(true, () => withEnv(PROXY_ORIGIN, () => withProxyEnv(NO_PROXY_ENV, () => {
const out = stampPromptCacheKey(makeEvent({}), makeCtx("https://api.anthropic.com/v1", { ...COWORK_MODEL, id: "claude-haiku-5-5" }), "omp");
assert.equal(out, undefined);
})));
});

test("#2583: same model with PI_PROXY set → stamped (the proxied push-off lands on globalThis.fetch)", () => {
withIntercept(true, () => withEnv(PROXY_ORIGIN, () => withProxyEnv({ ...NO_PROXY_ENV, PI_PROXY: "http://127.0.0.1:9999" }, () => {
const out = stampPromptCacheKey(makeEvent({}), makeCtx("https://api.anthropic.com/v1", COWORK_MODEL), "omp");
assert.deepEqual(out?.prompt_cache_key, "sess-1579");
})));
});

test("#2583: same model with PI_PROXY_ANTHROPIC set → stamped (per-provider proxy wins)", () => {
withIntercept(true, () => withEnv(PROXY_ORIGIN, () => withProxyEnv({ ...NO_PROXY_ENV, PI_PROXY_ANTHROPIC: "http://127.0.0.1:9999" }, () => {
const out = stampPromptCacheKey(makeEvent({}), makeCtx("https://api.anthropic.com/v1", COWORK_MODEL), "omp");
assert.deepEqual(out?.prompt_cache_key, "sess-1579");
})));
});

test("#2583: third-party relay speaking anthropic-messages → stamped (Cowork selection is provider-scoped)", () => {
withIntercept(true, () => withEnv(PROXY_ORIGIN, () => withProxyEnv(NO_PROXY_ENV, () => {
const out = stampPromptCacheKey(makeEvent({}), makeCtx("https://openrouter.ai/api/v1", { provider: "openrouter", api: "anthropic-messages" }), "omp");
assert.deepEqual(out?.prompt_cache_key, "sess-1579");
})));
});

test("#2583: provider anthropic on a non-anthropic-messages api → stamped", () => {
withIntercept(true, () => withEnv(PROXY_ORIGIN, () => withProxyEnv(NO_PROXY_ENV, () => {
const out = stampPromptCacheKey(makeEvent({}), makeCtx("http://10.0.0.8:8199/v1", { provider: "anthropic", api: "responses" }), "omp");
assert.deepEqual(out?.prompt_cache_key, "sess-1579");
})));
});

test("#2583: missing provider/api fields keep the optimistic stamp (fail-safe toward #1579 for older hosts)", () => {
withIntercept(true, () => withEnv(PROXY_ORIGIN, () => withProxyEnv(NO_PROXY_ENV, () => {
const out = stampPromptCacheKey(makeEvent({}), makeCtx("https://api.anthropic.com/v1"), "omp");
assert.deepEqual(out?.prompt_cache_key, "sess-1579");
})));
});

test("#2583: ompCoworkTransportBypassesFetch mirrors OMP's own egress selection predicate", () => {
assert.equal(ompCoworkTransportBypassesFetch(undefined), false);
assert.equal(ompCoworkTransportBypassesFetch({ provider: "anthropic" }), false);
assert.equal(ompCoworkTransportBypassesFetch({ api: "anthropic-messages" }), false);
assert.equal(ompCoworkTransportBypassesFetch({ provider: "openai", api: "anthropic-messages" }), false);
assert.equal(ompCoworkTransportBypassesFetch(COWORK_MODEL, {}), true);
assert.equal(ompCoworkTransportBypassesFetch(COWORK_MODEL, { PI_PROXY: "" }), true);
assert.equal(ompCoworkTransportBypassesFetch(COWORK_MODEL, { PI_PROXY: " " }), true);
assert.equal(ompCoworkTransportBypassesFetch(COWORK_MODEL, { PI_PROXY: "http://127.0.0.1:9999" }), false);
assert.equal(ompCoworkTransportBypassesFetch(COWORK_MODEL, { PI_PROXY_ANTHROPIC: "" }), true);
assert.equal(ompCoworkTransportBypassesFetch(COWORK_MODEL, { PI_PROXY_ANTHROPIC: "http://127.0.0.1:9999" }), false);
});
Loading