diff --git a/CONFIGURATION.md b/CONFIGURATION.md index 21b0c7a9f4..190b86cdd8 100644 --- a/CONFIGURATION.md +++ b/CONFIGURATION.md @@ -616,6 +616,8 @@ Environment variables take precedence over the config file. They are useful for | `BILI_FORK_ADOPTION` | Set `1` to turn ON fork block-adoption (#629): when an anonymous (prefix-affinity) client forks its history mid-conversation (edit-and-resend / regenerate an earlier turn), the new session inherits the parent's compression blocks whose source content is fully present in the forked request — instead of restarting with zero compression state and re-folding the shared prefix from scratch. Default is OFF. `"forkAdoption": true` in the config file does the same; the env var wins. The adoptable inventory is logged on every anonymous fork even while disabled, so you can size the win before flipping it on. | | `BILI_STABLE_SYSTEM_ANCHOR` | Set `1` to enable stable-system anchoring (#1085) — a **best-effort wire-layer fallback** for prefix caching: the root fix belongs client-side (the client owns its history and decides how to present instruction changes), this only stops the proxy from letting a changed head invalidate the whole cached prefix. **Plain-proxy mode only**: plugin-mode agents (`x-bili-plugin`) own their context management and are never anchored, so clients that already inject cache-friendly updates (e.g. claude-code-style system reminders) are not double-processed. When enabled, bili remembers each session's first-seen head system/instructions block and keeps re-sending those exact bytes even when the client's system prompt later changes. A **localized** change (a file-style edit sharing ≥70% of lines with the version in effect so far) appends a trailing `[System context update] …` user note carrying a compact line diff (`-` removed / `+` added; each note composes sequentially onto the previous one). A **non-localized** change (structural reshuffle, tool-def churn, timestamped banners, heads over 400 lines) is adopted outright — one deliberate cache miss beats appending noise that would mislead the model about its instructions. Churn guard: more than 8 accumulated notes likewise replace the anchor with the newest text and clear the log. The anchor and note log persist with the session and survive compression/compaction (session metadata, not kernel state). Known residual limitation: client-placed `cache_control` breakpoints may still misalign after a head swap. Excluded from anchoring: title-gen micro-requests (OpenAI/Google), Responses compaction-trigger requests, auto-mode classifier requests. Clients implementing their own variant (stable prompt + in-history updates) get zero extra injection — such updates pass through as ordinary history. Default is OFF. `"stableSystemAnchor": true` in the config file does the same; the env var wins. | | `BILI_CHAIN_CONTENT` | Set `0` to turn OFF the ACP-artifact content fallback of bili→bili chain detection (#1086): when an inbound request carries compression artifacts (render tags / historical `acp_status`+`search_context` tool calls) but neither the `x-bili-hop` header nor local compression state for its session, it is passed through verbatim instead of being processed again (a middlebox may have stripped the hop header from a chained request). Default is ON. `"chainContentDetection": false` in the config file does the same; the env var wins. The `x-bili-hop` signal itself is unaffected by this switch. | +| `BILI_CHAIN_MAX_FUTURE_SKEW_MS` | Maximum future skew (ms) tolerated when validating a chain checkpoint's `issued-at` timestamp (#1395 step 2): a checkpoint stamped more than this far into the future is judged `stale` (replay / clock skew) even when its digest verifies. Default `120000` (2 min); non-numeric or non-positive values fall back to the default. Step 2 is shadow-only — these knobs tune verdict logging, never forwarding. | +| `BILI_CHAIN_RECENT_WINDOW_MS` | Recency window (ms) for chain-checkpoint validation (#1395 step 2): a checkpoint older than this is judged `stale`. Default `600000` (10 min); non-numeric or non-positive values fall back to the default. Step 2 is shadow-only — these knobs tune verdict logging, never forwarding. | | `BILI_CONFLICT_SCAN` | Set `0` to disable third-party compression plugin detection (#1206). On by default: bili scans the client's own plugin/extension registries — opencode global + project `plugin` arrays, pi global + project `.pi/settings.json` `packages`, omp `config.yml` `extensions`, claude settings `enabledPlugins`/`plugins` + its plugins dir, kimi `plugins/installed.json`, hermes plugins dir, dsh profile dependencies — for another compressor co-resident with bili. Two tiers: **known conflicts** (`opencode-acp`, legacy `billion-context-pi`) and **keyword-suspected** entries (names matching compress / compact / acp / summar* / context*; bili's own entries are always skipped, and non-compression tools like `context7` do not match). Findings surface as launcher stderr lines before client start, a one-time proxy warn log on each session's first request, and the session's conflict ledger — visible in `acp_status`'s `COMPRESSION CONFLICTS` section, aggregated at `GET /__bili/stats` → `conflicts`, and shown as a web-UI banner. Runtime interference evidence (unannounced history rewrites #1001, orphan-block deactivations) feeds the same ledger. The scan is read-only, best-effort, 5-minute cached, and never blocks or modifies client config. | | `BILI_UPSTREAM_PROXY` | Upstream proxy for the proxy's own outbound connections — highest priority, above per-URL/per-provider config. See the README *Upstream proxy* section. | | `BILI_INHERITED_HTTP_PROXY` / `BILI_INHERITED_HTTPS_PROXY` / `BILI_INHERITED_ALL_PROXY` / `BILI_INHERITED_NO_PROXY` | Not user-facing — set automatically by the launcher when it spawns the proxy (#1012). The launcher strips the shell's proxy vars from both the client and the proxy child (clients must send to bili; the proxy must not have its model egress hijacked by a shell proxy), but it forwards the user's pre-strip proxy under these names so the proxy's **auxiliary egress** (MITM blind tunnels — client-side MCP/web traffic) can still ride the user's VPN. They feed only the blind-tunnel fallback tier: explicit routes / global `proxy` / `BILI_UPSTREAM_PROXY` / explicit `"upstreamProxyMode": "direct"` all still win, and a value pointing at bili's own port is dropped. Model egress is unaffected (stays direct unless explicitly configured). | diff --git a/CONFIGURATION.zh-CN.md b/CONFIGURATION.zh-CN.md index 6eb0f05593..84d07ddcca 100644 --- a/CONFIGURATION.zh-CN.md +++ b/CONFIGURATION.zh-CN.md @@ -612,6 +612,8 @@ | `BILI_FORK_ADOPTION` | 设为 `1` 开启 fork 块继承(#629):匿名(prefix-affinity)客户端在会话中途分叉历史(编辑重发 / 从更早轮次重新生成)时,新会话直接继承父会话中"源内容在分叉请求里完整存在"的压缩块 —— 而不是从零开始、把共享前缀重新折叠一遍。默认关闭。配置文件中设 `"forkAdoption": true` 效果相同;环境变量优先。无论开关如何,匿名 fork 发生时日志都会记录可继承的块清单,便于先评估收益再开启。 | | `BILI_STABLE_SYSTEM_ANCHOR` | 设为 `1` 开启稳定 system 锚定(#1085)—— **wire 层兜底(best-effort)**:根治在客户端(会话历史与指令变更的呈现方式由客户端决定),本开关只是阻止代理因头部变化而使整个已缓存前缀失效。**仅限 plain-proxy 模式**:plugin-mode agent(`x-bili-plugin`)自管上下文、永不参与锚定,避免对已自带 cache-friendly 更新注入的客户端(如 claude-code 的 system-reminder)做双重处理。开启后,bili 按会话记住客户端首次发送的头部 system/instructions 块并持续原样重发。**局部变更**(文件式编辑,与当前生效版本共享 ≥70% 行)追加末尾 `[System context update] …` user 注记,内含紧凑行级 diff(`-` 删除 / `+` 新增;每条注记顺序叠加在前一条之上)。**非局部变更**(结构性重排、tool 定义增删、带时间戳的 banner、超 400 行的头部)直接采用新文本 —— 一次有意的缓存失效好过追加会误导模型的噪声 diff。防抖保护:累积超过 8 条注记同样直接替换锚点为最新文本并清空日志。锚点与注记日志随会话持久化,不受压缩/compaction 影响(session metadata 而非 kernel state)。已知残留限制:客户端自放的 `cache_control` 断点在换头后仍可能错位。不参与锚定的请求:标题生成微请求(OpenAI/Google)、Responses compaction-trigger 请求、auto-mode classifier 请求。客户端自身已实现同类机制(稳定 prompt + 历史内更新)时零额外注入 —— 这类更新作为普通历史透传。默认关闭。配置文件中设 `"stableSystemAnchor": true` 效果相同;环境变量优先。 | | `BILI_CHAIN_CONTENT` | 设为 `0` 关闭 bili→bili 链检测的 ACP 产物内容回退(#1086):当入站请求携带压缩产物(渲染标签 / 历史 `acp_status`+`search_context` 工具调用)但既无 `x-bili-hop` 头、本实例也无该会话的压缩状态时,请求被原样透传而不再次处理(中间设备可能已剥掉链路上的 hop 头)。默认开启。配置文件中设 `"chainContentDetection": false` 效果相同;环境变量优先。`x-bili-hop` 信号本身不受此开关影响。 | +| `BILI_CHAIN_MAX_FUTURE_SKEW_MS` | 校验链检查点 `issued-at` 时间戳时容忍的最大未来偏斜(毫秒)(#1395 step 2):戳在比当前时间未来超过此值的检查点会被判为 `stale`(重放 / 时钟偏斜),即使其摘要校验通过。默认 `120000`(2 分钟);非数字或非正值回退到默认值。Step 2 仅影子模式——这些旋钮只调判定日志,绝不影响转发。 | +| `BILI_CHAIN_RECENT_WINDOW_MS` | 链检查点校验的近期窗口(毫秒)(#1395 step 2):早于此窗口的检查点被判为 `stale`。默认 `600000`(10 分钟);非数字或非正值回退到默认值。Step 2 仅影子模式——这些旋钮只调判定日志,绝不影响转发。 | | `BILI_CONFLICT_SCAN` | 设为 `0` 关闭第三方压缩插件检测(#1206)。默认开启:bili 会扫描客户端自身的插件/扩展注册表 —— opencode 全局 + 项目配置的 `plugin` 数组、pi 全局 + 项目 `.pi/settings.json` 的 `packages`、omp `config.yml` 的 `extensions`、claude 设置的 `enabledPlugins`/`plugins` 及其插件目录、kimi `plugins/installed.json`、hermes 插件目录、dsh profile 依赖 —— 查找与 bili 并存的另一个压缩器。两个层级:**已知冲突**(`opencode-acp`、遗留 `billion-context-pi`,确定性判定)和**关键词疑似**条目(名称匹配 compress / compact / acp / summar* / context*;bili 自身条目永远跳过,`context7` 这类非压缩工具不会误报)。发现结果出现在:客户端启动前的 launcher stderr、每个会话首个请求的一次性代理 warn 日志、以及会话冲突台账 —— `acp_status` 的 `COMPRESSION CONFLICTS` 段、`GET /__bili/stats` → `conflicts`、Web UI 横幅。运行时干扰证据(未宣告的历史改写 #1001、孤儿块废弃)记入同一台账。「扫描只读、尽力而为、5 分钟缓存,绝不阻塞或改动客户端配置。」 | | `BILI_UPSTREAM_PROXY` | 代理自身出站连接的上游代理 —— 优先级最高,高于 per-URL/per-provider 配置。见 README「上游代理」一节。 | | `BILI_INHERITED_HTTP_PROXY` / `BILI_INHERITED_HTTPS_PROXY` / `BILI_INHERITED_ALL_PROXY` / `BILI_INHERITED_NO_PROXY` | 非用户直接使用 —— launcher 起代理子进程时自动设置(#1012)。launcher 会从客户端和代理子进程两侧剥掉 shell 的代理变量(客户端必须把流量发给 bili;代理的模型出网也不能被 shell 代理劫持),但会把用户剥离前的代理转发到这些变量里,让代理的**辅助出网**(MITM 盲隧道 —— 客户端侧的 MCP/web 流量)仍能走用户的 VPN。它们只作用于盲隧道的 fallback 层:显式路由 / 全局 `proxy` / `BILI_UPSTREAM_PROXY` / 显式 `"upstreamProxyMode": "direct"` 仍然优先,指向 bili 自身端口的值会被丢弃。模型出网不受影响(未显式配置则保持直连)。 | diff --git a/src/chain-checkpoint.ts b/src/chain-checkpoint.ts new file mode 100644 index 0000000000..c6a544500e --- /dev/null +++ b/src/chain-checkpoint.ts @@ -0,0 +1,358 @@ +import { createHash } from "node:crypto"; +import type { WireProtocol } from "./util.js"; + +// #1357/#1395 — Chain Checkpoint (first-processor-wins idempotent interop). +// A request-level checkpoint marks a request that already passed through a +// bili pipeline; downstream bili instances recognize it and forward silently +// instead of re-running kernel/injection. This module is STEP 2: recognition +// only — parser, per-wire carrier contract, JCS digest, shadow verdicts. +// Generation + enforcement land in step 3 (separate human-reviewed PR). +// +// Carrier contract (#1395 decision (a), ownership-based): a checkpoint lives +// ONLY in a bili-owned trailing control slot — the trailing run of user-role +// messages after the last non-user entry, whose ENTIRE content strictly +// equals the tag syntax below. Content the model or user can generate +// themselves is never a carrier; there is deliberately no global +// magic-string scanning anywhere else in the body. The strict full-content +// match (not the role) is what makes a slot eligible: if a client's own +// final message literally is a well-formed tag binding its own digest, open +// mode treats that as an explicit self-opt-out — accepted by design. +// +// Verdict semantics (shadow mode logs only; step 3 maps them to behavior): +// valid ≥1 known-version candidate whose digest matches and is fresh +// recent-mismatch no digest match, but a well-formed FRESH checkpoint (a +// different bili processed this body) → step 3 forwards + warns +// stale digest match with out-of-window/future timestamp (replay or +// clock skew → step 3 forwards + warns), OR no-match stale-only +// (→ step 3 strips and processes normally) +// invalid malformed-looking tag(s) in carrier slots, future-dated +// beyond skew only, or unknown version only → never trusted +// none no checkpoint signal at all +export const CHAIN_TAG = "bili-chain"; +export const SUPPORTED_CHECKPOINT_VERSION = 1; +export const DEFAULT_MAX_FUTURE_SKEW_MS = 2 * 60 * 1000; +export const DEFAULT_RECENT_CHECKPOINT_WINDOW_MS = 10 * 60 * 1000; +// Generous parser ceiling: the generator emits ≤~200 chars (design target +// ≤~120 with short ids); the cap only bounds malformed-tag scanning cost. +const MAX_CHECKPOINT_CHARS = 512; + +const TAG_OPEN = "\x3cbili-chain "; +const TAG_CLOSE = "/\x3e"; + +export interface ChainCheckpoint { + v: number; + processor: string; + issuedAt: number; + requestId: string; + digest: string; +} + +export type ChainVerdict = "none" | "valid" | "recent-mismatch" | "stale" | "invalid"; + +export interface ChainCheckpointContext { + candidates: ChainCheckpoint[]; + malformed: number; + selected?: ChainCheckpoint; + verdict: ChainVerdict; +} + +const ATTRS = ["v", "processor", "issued-at", "request-id", "digest"] as const; +type AttrName = (typeof ATTRS)[number]; + +function validAttrValue(name: AttrName, raw: string): boolean { + switch (name) { + case "v": + return /^\d+$/.test(raw) && Number(raw) >= 1; + case "issued-at": + return /^\d{1,16}$/.test(raw); + case "processor": + return /^[A-Za-z0-9._-]{1,64}$/.test(raw); + case "request-id": + return /^[A-Za-z0-9._:-]{1,64}$/.test(raw); + case "digest": + return /^sha256:[0-9a-f]{64}$/.test(raw); + } +} + +export function parseChainCheckpoint(text: unknown): ChainCheckpoint | null { + if (typeof text !== "string" || text.length === 0 || text.length > MAX_CHECKPOINT_CHARS) return null; + if (!text.startsWith(TAG_OPEN) || !text.endsWith(TAG_CLOSE)) return null; + const inner = text.slice(TAG_OPEN.length, -TAG_CLOSE.length); + const found: Partial> = {}; + for (const token of inner.split(/\s+/)) { + const m = /^([a-z][a-z0-9-]*)="([^"]*)"$/.exec(token); + if (!m) return null; + const name = m[1] as string; + if (!(ATTRS as readonly string[]).includes(name)) return null; + if (Object.hasOwn(found, name)) return null; + found[name as AttrName] = m[2]; + } + for (const attr of ATTRS) { + const raw = found[attr]; + if (raw === undefined || !validAttrValue(attr, raw)) return null; + } + return { + v: Number(found.v), + processor: found.processor as string, + issuedAt: Number(found["issued-at"]), + requestId: found["request-id"] as string, + digest: found.digest as string, + }; +} + +export function renderChainCheckpoint(cp: ChainCheckpoint): string { + return `${TAG_OPEN}v="${cp.v}" processor="${cp.processor}" issued-at="${cp.issuedAt}" request-id="${cp.requestId}" digest="${cp.digest}"${TAG_CLOSE}`; +} + +interface CarrierSlot { + messageIndex: number; + text: string; +} + +function singleText(content: unknown, partType: string): string | undefined { + if (typeof content === "string") return content; + if (Array.isArray(content) && content.length === 1) { + const only = content[0]; + if (only && typeof only === "object" && !Array.isArray(only)) { + const part = only as Record; + if (part.type === partType && typeof part.text === "string" && Object.keys(part).length === 2) { + return part.text; + } + } + } + return undefined; +} + +function trailingUserSlotsAnthropicLike(body: Record): CarrierSlot[] { + const messages = body.messages; + if (!Array.isArray(messages)) return []; + const slots: CarrierSlot[] = []; + for (let i = messages.length - 1; i >= 0; i--) { + const msg = messages[i]; + if (!msg || typeof msg !== "object" || Array.isArray(msg)) break; + const m = msg as Record; + if (m.role !== "user") break; + const text = singleText(m.content, "text"); + if (text !== undefined) slots.push({ messageIndex: i, text }); + } + return slots.reverse(); +} + +function trailingUserSlotsResponses(body: Record): CarrierSlot[] { + const input = body.input; + if (typeof input === "string") return input === "" ? [] : [{ messageIndex: -1, text: input }]; + if (!Array.isArray(input)) return []; + let end = input.length - 1; + const last = input[end]; + if (last && typeof last === "object" && !Array.isArray(last) && (last as Record).type === "compaction_trigger") { + end -= 1; + } + const slots: CarrierSlot[] = []; + for (let i = end; i >= 0; i--) { + const item = input[i]; + if (!item || typeof item !== "object" || Array.isArray(item)) break; + const it = item as Record; + if (it.type !== "message" || it.role !== "user") break; + const text = singleText(it.content, "input_text"); + if (text !== undefined) slots.push({ messageIndex: i, text }); + } + return slots.reverse(); +} + +function trailingUserSlotsGoogle(body: Record): CarrierSlot[] { + const contents = body.contents; + if (!Array.isArray(contents)) return []; + const slots: CarrierSlot[] = []; + for (let i = contents.length - 1; i >= 0; i--) { + const c = contents[i]; + if (!c || typeof c !== "object" || Array.isArray(c)) break; + const ct = c as Record; + if (ct.role !== "user") break; + const parts = ct.parts; + if (Array.isArray(parts) && parts.length === 1) { + const p = parts[0]; + if (p && typeof p === "object" && !Array.isArray(p) && typeof (p as Record).text === "string" && Object.keys(p).length === 1) { + slots.push({ messageIndex: i, text: (p as Record).text as string }); + } + } + } + return slots.reverse(); +} + +export interface ChainExtraction { + candidates: ChainCheckpoint[]; + malformed: number; + stripped: unknown; +} + +export function extractChainCarriers(parsed: unknown, wire: WireProtocol): ChainExtraction { + const result: ChainExtraction = { candidates: [], malformed: 0, stripped: parsed }; + if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) return result; + const body = parsed as Record; + let slots: CarrierSlot[] = []; + switch (wire) { + case "anthropic": + case "openai": + slots = trailingUserSlotsAnthropicLike(body); + break; + case "responses": + slots = trailingUserSlotsResponses(body); + break; + case "google": + slots = trailingUserSlotsGoogle(body); + break; + } + const hitIndexes = new Set(); + for (const slot of slots) { + if (!slot.text.startsWith("\x3c" + CHAIN_TAG)) continue; + const cp = parseChainCheckpoint(slot.text); + if (cp) { + result.candidates.push(cp); + hitIndexes.add(slot.messageIndex); + } else { + result.malformed += 1; + } + } + if (hitIndexes.size > 0) { + const strip = (arr: unknown[]): unknown[] => arr.filter((_, i) => !hitIndexes.has(i)); + if (wire === "responses" && typeof body.input === "string") { + result.stripped = { ...body, input: "" }; + } else { + const key = wire === "google" ? "contents" : wire === "responses" ? "input" : "messages"; + const arr = body[key]; + if (Array.isArray(arr)) result.stripped = { ...body, [key]: strip(arr) }; + } + } + return result; +} + +// RFC 8785 (JCS) canonicalization, dependency-free: recursive key sort by +// Unicode code point (NOT UTF-16 unit order — astral keys differ), arrays in +// order, JSON.stringify escaping (JCS-compatible: short control forms, lower +// \uXXXX hex), JS shortest-round-trip numbers per §3.2.2 (with the -0 fix). +function jcsKeyCompare(a: string, b: string): number { + // Numeric code-point comparison: string `<` compares UTF-16 units, which + // diverges from code-point order at the surrogate boundary (U+D800–U+DFFF + // vs U+10000+). RFC 8785 §2.3 requires code points. + const ca = [...a].map((ch) => ch.codePointAt(0)!); + const cb = [...b].map((ch) => ch.codePointAt(0)!); + const n = Math.min(ca.length, cb.length); + for (let i = 0; i < n; i++) { + const x = ca[i]!; + const y = cb[i]!; + if (x !== y) return x - y; + } + return ca.length - cb.length; +} + +export function jcsStringify(value: unknown): string { + if (value === null || typeof value === "boolean") return JSON.stringify(value); + if (typeof value === "number") return Object.is(value, -0) ? "-0" : JSON.stringify(value); + if (typeof value === "string") return JSON.stringify(value); + if (Array.isArray(value)) return "[" + value.map(jcsStringify).join(",") + "]"; + if (typeof value === "object") { + const obj = value as Record; + const keys = Object.keys(obj).sort(jcsKeyCompare); + return "{" + keys.map((k) => JSON.stringify(k) + ":" + jcsStringify(obj[k])).join(",") + "}"; + } + return "null"; +} + +export function computeRequestDigest(parsed: unknown, wire: WireProtocol): string { + const { stripped } = extractChainCarriers(parsed, wire); + return sha256Of(jcsStringify(stripped)); +} + +function sha256Of(canonical: string): string { + return "sha256:" + createHash("sha256").update(canonical, "utf8").digest("hex"); +} + +const ZERO_DIGEST = "sha256:" + "0".repeat(64); + +export function insertCheckpointCarrier(parsed: unknown, wire: WireProtocol, tag: string): unknown | null { + if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) return null; + const body = parsed as Record; + if (wire === "anthropic" || wire === "openai") { + const messages = body.messages; + if (!Array.isArray(messages)) return null; + return { ...body, messages: [...messages, { role: "user", content: tag }] }; + } + if (wire === "responses") { + const input = body.input; + if (typeof input === "string") { + // Two-part messages are not carrier slots (singleText needs exactly + // one part) — normalize to array form or the stamp is never recognized. + const orig = input === "" ? [] : [{ type: "message", role: "user", content: input }]; + return { ...body, input: [...orig, { type: "message", role: "user", content: tag }] }; + } + if (!Array.isArray(input)) return null; + let insertAt = input.length; + const last = input[insertAt - 1]; + if (last && typeof last === "object" && !Array.isArray(last) && (last as Record).type === "compaction_trigger") insertAt -= 1; + const next = [...input.slice(0, insertAt), { type: "message", role: "user", content: tag }, ...input.slice(insertAt)]; + return { ...body, input: next }; + } + const contents = body.contents; + if (!Array.isArray(contents)) return null; + return { ...body, contents: [...contents, { role: "user", parts: [{ text: tag }] }] }; +} + +// Generation-side digest (step 3 stamps with this): insert a zero-digest +// carrier, strip carriers, canonicalize + hash — so the rendered tag carries +// a digest that validates against the STAMPED body (round-trip property). +export function computeCheckpointDigest(parsed: unknown, wire: WireProtocol, fields: Pick): string | null { + const stamped = insertCheckpointCarrier(parsed, wire, renderChainCheckpoint({ ...fields, digest: ZERO_DIGEST })); + if (stamped === null) return null; + const { stripped } = extractChainCarriers(stamped, wire); + return sha256Of(jcsStringify(stripped)); +} + +export interface ChainEvaluationOptions { + nowMs?: number; + maxFutureSkewMs?: number; + recentWindowMs?: number; +} + +function envMs(name: string, fallback: number): number { + const raw = process.env[name]; + if (raw === undefined) return fallback; + const n = Number(raw); + return Number.isFinite(n) && n > 0 ? n : fallback; +} + +export function evaluateChain(parsed: unknown, wire: WireProtocol, opts: ChainEvaluationOptions = {}): ChainCheckpointContext { + const nowMs = opts.nowMs ?? Date.now(); + const maxFutureSkewMs = opts.maxFutureSkewMs ?? envMs("BILI_CHAIN_MAX_FUTURE_SKEW_MS", DEFAULT_MAX_FUTURE_SKEW_MS); + const recentWindowMs = opts.recentWindowMs ?? envMs("BILI_CHAIN_RECENT_WINDOW_MS", DEFAULT_RECENT_CHECKPOINT_WINDOW_MS); + const { candidates, malformed, stripped } = extractChainCarriers(parsed, wire); + if (candidates.length === 0) { + return { candidates, malformed, verdict: malformed > 0 ? "invalid" : "none" }; + } + const digest = sha256Of(jcsStringify(stripped)); + type TimeClass = "fresh" | "stale" | "future"; + interface Classified { + cp: ChainCheckpoint; + match: boolean; + time: TimeClass; + knownVersion: boolean; + } + const classified: Classified[] = candidates.map((cp) => { + const time: TimeClass = cp.issuedAt - nowMs > maxFutureSkewMs ? "future" : nowMs - cp.issuedAt > recentWindowMs ? "stale" : "fresh"; + return { cp, match: cp.digest === digest, time, knownVersion: cp.v === SUPPORTED_CHECKPOINT_VERSION }; + }); + const usable = classified.filter((c) => c.knownVersion); + const pickLatest = (arr: Classified[]): Classified | undefined => + arr.reduce((best, c) => (!best || c.cp.issuedAt > best.cp.issuedAt ? c : best), undefined); + // Classify first, then take latest: a digest MATCH proves identity and + // outranks any fresher mismatched candidate (never trust a newer forged + // timestamp over a verified digest). + const matchedFresh = pickLatest(usable.filter((c) => c.match && c.time === "fresh")); + if (matchedFresh) return { candidates, malformed, selected: matchedFresh.cp, verdict: "valid" }; + const matchedOther = pickLatest(usable.filter((c) => c.match)); + if (matchedOther) return { candidates, malformed, selected: matchedOther.cp, verdict: "stale" }; + const nomatchFresh = pickLatest(usable.filter((c) => !c.match && c.time === "fresh")); + if (nomatchFresh) return { candidates, malformed, selected: nomatchFresh.cp, verdict: "recent-mismatch" }; + const nomatchStale = pickLatest(usable.filter((c) => !c.match && c.time === "stale")); + if (nomatchStale) return { candidates, malformed, selected: nomatchStale.cp, verdict: "stale" }; + return { candidates, malformed, verdict: "invalid" }; +} diff --git a/src/server.ts b/src/server.ts index 30f108f28c..1f78809608 100644 --- a/src/server.ts +++ b/src/server.ts @@ -103,6 +103,7 @@ import { maybeAdoptForkBlocks } from "./fork-adoption.js"; import { flushPrefixAffinity, hydratePrefixAffinity, scheduleAffinityPersist } from "./affinity-persist.js"; import { consumePluginRegisterFor, flushConversations, handlePluginCompact, handlePluginManifest, handlePluginRegister, handlePluginRuntimeInfo, handlePluginStatus, handlePluginTool, loadConversations, pipePluginChatWithStrip, pipePluginJson, pipePluginResponsesWithStrip, pluginAgentHeader, pluginConversationHeader, pluginHeadersMatchModel, pluginReportedContextWindow, pluginReportedMaxOutput, pluginRuntimeInfoFor, recordChainVerdict, recordPluginSession, rememberPluginMessages, resolveConversation, takePendingPluginRegister } from "./plugin.js"; import { setupMitm, readMitmUpstream, getBlindTunnelStats } from "./mitm.js"; +import { evaluateChain } from "./chain-checkpoint.js"; import type { BiliMessage } from "acp-kernel/wire"; import { BILI_PASSTHROUGH_HEADER, BILI_PLUGIN_BYPASS_HEADER, hardenOpenaiAssistantContent, isLoopbackAddress, inspectContextOverflow, reserveOutputHeadroom, resolveOutputHeadroomCap, shouldReserveOutputHeadroom, systemToUser, usageOutputTotal, usageTotals, type ContextOverflowInfo, type WireProtocol } from "./util.js"; @@ -1239,6 +1240,21 @@ async function handle( if (Array.isArray(p.input)) return p.input.length; return null; })(); + // #1395 step 2 (shadow): chain-checkpoint recognition — log the verdict + // only, ZERO forwarding behavior change (enforcement lands in step 3). + // Gated like the legacy artifact fallback (chainContentDetection) and + // skipped when x-bili-hop is present (that path already decides). + if (protocol && hopMarker === undefined && opts.chainContentDetection !== false && parsed !== null && typeof parsed === "object") { + try { + const chainCtx = evaluateChain(parsed, protocol); + if (chainCtx.verdict !== "none") { + const sel = chainCtx.selected; + log(chainCtx.verdict === "valid" ? "info" : "warn", `[chain-shadow] inbound ${protocol} request carries ${chainCtx.candidates.length} chain checkpoint(s) — verdict=${chainCtx.verdict}${sel ? ` (v=${sel.v} processor=${sel.processor} issued-at=${sel.issuedAt} request-id=${sel.requestId})` : ""}${chainCtx.malformed > 0 ? ` malformed=${chainCtx.malformed}` : ""}; shadow mode: no forwarding decision made (#1395 step 3 enforces)`); + } + } catch (err) { + log("debug", `[chain-shadow] evaluation failed (${String(err)}); ignoring`); + } + } // #806: a parseable body missing the conversation field used to crash the // kernel's conversation-signal fingerprint (body.messages.find on undefined — // top-level arrays included) and surface as an opaque 502; #806 answered that diff --git a/tests/chain-checkpoint-step2.test.ts b/tests/chain-checkpoint-step2.test.ts new file mode 100644 index 0000000000..c3bf5789c1 --- /dev/null +++ b/tests/chain-checkpoint-step2.test.ts @@ -0,0 +1,410 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import { defaultConfig } from "acp-kernel"; +import { startServer } from "../src/server.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import type { ProxyOptions } from "../src/config.ts"; +import { _setForTest as setRegistryForTest } from "../src/registry.ts"; +import { setLogCapture } from "../src/logger.ts"; +import { + DEFAULT_MAX_FUTURE_SKEW_MS, + DEFAULT_RECENT_CHECKPOINT_WINDOW_MS, + computeCheckpointDigest, + computeRequestDigest, + evaluateChain, + extractChainCarriers, + insertCheckpointCarrier, + jcsStringify, + parseChainCheckpoint, + renderChainCheckpoint, + type ChainCheckpoint, +} from "../src/chain-checkpoint.ts"; + +// #1395 step 2: chain-checkpoint recognition (parser, per-wire carrier +// contract, JCS digest, shadow verdicts). Synthetic fixtures first; the three +// integration tests prove ZERO forwarding behavior change through a real proxy. + +const L = "\x3c"; +const R = "\x3e"; +const T0 = 1_758_864_000_000; +const MIN = 60 * 1000; + +const GOOD_DIGEST = "sha256:" + "ab".repeat(32); +const FIELDS: Pick = { + v: 1, + processor: "bili-a", + issuedAt: T0, + requestId: "req-1", +}; +const tag = (over: Partial = {}): string => + renderChainCheckpoint({ ...FIELDS, digest: GOOD_DIGEST, ...over }); + +test("parser: accepts canonical + reordered attrs, rejects every malformed shape", () => { + assert.deepEqual(parseChainCheckpoint(tag()), { v: 1, processor: "bili-a", issuedAt: T0, requestId: "req-1", digest: GOOD_DIGEST }); + assert.ok(parseChainCheckpoint(`${L}bili-chain digest="${GOOD_DIGEST}" request-id="r" issued-at="${T0}" processor="p" v="1"/${R}`), "attr order is irrelevant"); + assert.equal(parseChainCheckpoint(tag({ digest: undefined }) ?? "", "x") ?? null, null); + const noDigest = tag().replace(` digest="${GOOD_DIGEST}"`, ""); + assert.equal(parseChainCheckpoint(noDigest), null, "missing attr"); + assert.equal(parseChainCheckpoint(tag().replace("/" + R, ` v="1"/${R}`)), null, "duplicate attr"); + assert.equal(parseChainCheckpoint(tag().replace("/" + R, ` extra="x"/${R}`)), null, "unknown attr"); + assert.equal(parseChainCheckpoint(tag({ digest: "sha256:" + "AB".repeat(32) })), null, "uppercase hex rejected"); + assert.equal(parseChainCheckpoint(tag({ digest: "md5:" + "ab".repeat(16) })), null, "wrong algorithm prefix"); + assert.equal(parseChainCheckpoint(tag({ digest: "sha256:" + "a".repeat(63) })), null, "wrong digest length"); + assert.equal(parseChainCheckpoint(tag({ v: 0 })), null, "v=0"); + assert.equal(parseChainCheckpoint(tag({ v: -1 })), null, "negative v"); + assert.equal(parseChainCheckpoint(tag({ issuedAt: NaN })), null, "non-numeric issued-at"); + assert.equal(parseChainCheckpoint(tag({ processor: 'a"b' })), null, "embedded quote"); + assert.equal(parseChainCheckpoint(tag() + "\nmore"), null, "trailing newline"); + assert.equal(parseChainCheckpoint(" " + tag()), null, "leading space"); + assert.equal(parseChainCheckpoint(tag().slice(0, -1)), null, "missing close"); + assert.equal(parseChainCheckpoint(L + "bili-chain v=\"1\" " + "x".repeat(600) + R), null, "length cap"); + assert.equal(parseChainCheckpoint(42), null); + assert.equal(parseChainCheckpoint(null), null); + assert.equal(parseChainCheckpoint(undefined), null); +}); + +test("jcs: key-order invariant, array order preserved, RFC 8785 number/string forms, code-point key sort", () => { + assert.equal( + jcsStringify(JSON.parse('{"a":1,"b":{"d":2,"c":[3,1]}}')), + jcsStringify(JSON.parse('{"b":{"c":[3,1],"d":2},"a":1}')), + "key order must not matter (nested)", + ); + assert.notEqual(jcsStringify([1, 2]), jcsStringify([2, 1]), "array order matters"); + assert.equal(jcsStringify(-0), "-0"); + assert.equal(jcsStringify(1e21), "1e+21"); + assert.equal(jcsStringify(0.1), "0.1"); + assert.equal(jcsStringify('a"b\\c\nd\té'), JSON.stringify('a"b\\c\nd\té')); + // astral vs lone-surrogate keys: code-point order (U+D801 < U+10000), NOT + // UTF-16 unit order (0xD801 > 0xD800) — pins the [...str] spread sort. + const obj: Record = {}; + obj["\u{10000}"] = 2; + obj["\uD801"] = 1; + // V8 does not escape non-BMP characters in JSON.stringify, so build the + // expected string from the code point itself. + assert.equal(jcsStringify(obj), `{"\\ud801":1,"${String.fromCodePoint(0x10000)}":2}`); +}); + +test("carrier contract: strict whole-content match in the trailing user run only", () => { + const mkOpenai = (last: unknown, extra: unknown[] = []) => ({ + model: "m", + messages: [ + { role: "system", content: "sys" }, + { role: "user", content: tag() }, + { role: "assistant", content: "yo" }, + ...extra, + { role: "user", content: last }, + ], + }); + const found = extractChainCarriers(mkOpenai(tag()), "openai"); + assert.equal(found.candidates.length, 1, "trailing whole-content carrier is recognized"); + assert.equal((found.stripped as { messages: unknown[] }).messages.length, 3, "carrier message stripped (4 total → 3)"); + + assert.equal(extractChainCarriers(mkOpenai("plain"), "openai").candidates.length, 0, "mid-history tag is NOT a carrier (trailing run only)"); + assert.equal(extractChainCarriers({ model: "m", messages: [{ role: "assistant", content: tag() }] }, "openai").candidates.length, 0, "assistant content is never a carrier"); + assert.equal(extractChainCarriers(mkOpenai([{ type: "text", text: "hi" }, { type: "text", text: tag() }]), "openai").candidates.length, 0, "multi-block content is never a carrier"); + const singleBlock = extractChainCarriers(mkOpenai([{ type: "text", text: tag() }]), "openai"); + assert.equal(singleBlock.candidates.length, 1, "single text block IS the whole content"); + const malformed = extractChainCarriers(mkOpenai(L + "bili-chain broken" + R), "openai"); + assert.equal(malformed.candidates.length, 0); + assert.equal(malformed.malformed, 1, "tag-shaped garbage in a carrier slot is counted malformed"); + + const resp = { + model: "m", + input: [ + { type: "message", role: "user", content: "hi" }, + { type: "message", role: "assistant", content: "yo" }, + { type: "message", role: "user", content: tag() }, + { type: "compaction_trigger" }, + ], + }; + const respFound = extractChainCarriers(resp, "responses"); + assert.equal(respFound.candidates.length, 1, "responses carrier before trailing compaction_trigger"); + const respStripped = respFound.stripped as { input: { type?: string }[] }; + assert.equal(respStripped.input.length, 3); + assert.equal(respStripped.input[respStripped.input.length - 1].type, "compaction_trigger", "compaction_trigger stays last (#283/#209)"); + + const bareStr = extractChainCarriers({ model: "m", input: tag() }, "responses"); + assert.equal(bareStr.candidates.length, 1, "bare-string input equal to the tag is a carrier"); + assert.equal((bareStr.stripped as { input: string }).input, ""); + + const gOk = extractChainCarriers({ contents: [{ role: "user", parts: [{ text: tag() }] }] }, "google"); + assert.equal(gOk.candidates.length, 1); + assert.equal(extractChainCarriers({ contents: [{ role: "user", parts: [{ text: tag() }, { text: "x" }] }] }, "google").candidates.length, 0, "two-part google content is never a carrier"); + assert.equal(extractChainCarriers({ contents: [{ role: "user", parts: [{ text: tag(), thoughtSignature: "s" }] }] }, "google").candidates.length, 0, "extra part field disqualifies"); + assert.equal(extractChainCarriers(null, "openai").candidates.length, 0); + assert.equal(extractChainCarriers({ model: "m" }, "openai").candidates.length, 0); +}); + +test("digest: carrier-stripped body ≡ carrier-free body; key-order invariant; content-sensitive", () => { + const base = { model: "m", messages: [{ role: "user", content: "hello" }] }; + const stamped = insertCheckpointCarrier(base, "openai", tag())!; + assert.equal(computeRequestDigest(stamped, "openai"), computeRequestDigest(base, "openai"), "removing the carrier must restore the original digest"); + const a = JSON.parse('{"model":"m","messages":[{"role":"user","content":"x"}]}'); + const b = JSON.parse('{"messages":[{"content":"x","role":"user"}],"model":"m"}'); + assert.equal(computeRequestDigest(a, "openai"), computeRequestDigest(b, "openai")); + assert.notEqual(computeRequestDigest({ ...base, messages: [{ role: "user", content: "HELLO" }] }, "openai"), computeRequestDigest(base, "openai")); +}); + +test("zero-normalize round-trip: stamped body verifies as valid on every wire", () => { + const fields = { v: 1, processor: "bili-rt", issuedAt: T0 + 1000, requestId: "req-rt" }; + for (const wire of ["openai", "anthropic"] as const) { + const base = { model: "m", messages: [{ role: "user", content: "hello" }] }; + const digest = computeCheckpointDigest(base, wire, fields)!; + const stamped = insertCheckpointCarrier(base, wire, renderChainCheckpoint({ ...fields, digest }))!; + const ctx = evaluateChain(stamped, wire, { nowMs: T0 + 2000 }); + assert.equal(ctx.verdict, "valid", `${wire}: round-trip must verify`); + assert.equal(ctx.selected?.requestId, "req-rt", wire); + } + const baseR = { model: "m", input: [{ type: "message", role: "user", content: "hello" }, { type: "compaction_trigger" }] }; + const dR = computeCheckpointDigest(baseR, "responses", fields)!; + const sR = insertCheckpointCarrier(baseR, "responses", renderChainCheckpoint({ ...fields, digest: dR }))!; + const input = (sR as { input: { type?: string }[] }).input; + assert.equal(input[input.length - 1].type, "compaction_trigger", "stamp inserts BEFORE the trigger"); + assert.equal(input[input.length - 2]?.type === "message" ? (input[input.length - 2] as { content?: string }).content : "", renderChainCheckpoint({ ...fields, digest: dR }), "checkpoint lands in the trailing user slot"); + assert.equal(evaluateChain(sR, "responses", { nowMs: T0 + 2000 }).verdict, "valid"); +}); + +test("zero-normalize round-trip: responses bare-string input normalizes to message array and verifies", () => { + const fields = { v: 1, processor: "bili-rt-str", issuedAt: T0 + 1000, requestId: "req-rt-str" }; + const base = { model: "m", input: "hello" }; + const digest = computeCheckpointDigest(base, "responses", fields)!; + const rendered = renderChainCheckpoint({ ...fields, digest }); + const stamped = insertCheckpointCarrier(base, "responses", rendered)! as { input: { type?: string; content?: unknown }[] }; + assert.ok(Array.isArray(stamped.input), "string input must be normalized to the array form"); + assert.equal(stamped.input.length, 2); + assert.equal(stamped.input[0]!.type, "message"); + assert.equal(stamped.input[0]!.content, "hello", "original text survives as its own message"); + assert.equal(stamped.input[1]!.content, rendered, "carrier lands in the trailing user slot"); + assert.equal(evaluateChain(stamped, "responses", { nowMs: T0 + 2000 }).verdict, "valid", "stamped string-input body must verify"); + const baseEmpty = { model: "m", input: "" }; + const dE = computeCheckpointDigest(baseEmpty, "responses", fields)!; + const sE = insertCheckpointCarrier(baseEmpty, "responses", renderChainCheckpoint({ ...fields, digest: dE }))!; + assert.equal(evaluateChain(sE, "responses", { nowMs: T0 + 2000 }).verdict, "valid", "empty string input round-trips too"); +}); + +test("verdict matrix: digest×timestamp quadrants, selection priority, version gating", () => { + const base = { model: "m", messages: [{ role: "user", content: "hello" }] }; + const stamp = (fields: typeof FIELDS, digest: string): unknown => + insertCheckpointCarrier(base, "openai", renderChainCheckpoint({ ...fields, digest }))!; + const goodDigest = (fields: typeof FIELDS): string => computeCheckpointDigest(base, "openai", fields)!; + const evalAt = (body: unknown, nowMs: number) => evaluateChain(body, "openai", { nowMs }); + + assert.equal(evalAt(stamp(FIELDS, goodDigest(FIELDS)), T0 + MIN).verdict, "valid", "fresh + match"); + assert.equal(evalAt(stamp({ ...FIELDS, issuedAt: T0 - 11 * MIN }, goodDigest({ ...FIELDS, issuedAt: T0 - 11 * MIN })), T0).verdict, "stale", "match + out-of-window age"); + assert.equal(evalAt(stamp({ ...FIELDS, issuedAt: T0 + MIN }, goodDigest({ ...FIELDS, issuedAt: T0 + MIN })), T0).verdict, "valid", "match + future within skew is fresh"); + assert.equal(evalAt(stamp({ ...FIELDS, issuedAt: T0 + 3 * MIN }, goodDigest({ ...FIELDS, issuedAt: T0 + 3 * MIN })), T0).verdict, "stale", "MATCHED future beyond skew = replay/clock skew — verified identity, step 3 forwards + warns"); + assert.equal(evalAt(stamp({ ...FIELDS, issuedAt: T0 + 3 * MIN }, "sha256:" + "ee".repeat(32)), T0).verdict, "invalid", "future beyond skew with no digest match leaves nothing usable"); + assert.equal(evalAt(stamp(FIELDS, "sha256:" + "cd".repeat(32)), T0 + MIN).verdict, "recent-mismatch", "well-formed fresh, no digest match"); + assert.equal(evalAt(stamp({ ...FIELDS, issuedAt: T0 - 11 * MIN }, "sha256:" + "cd".repeat(32)), T0).verdict, "stale", "no-match stale only"); + + const staleMatch = { ...FIELDS, issuedAt: T0 - 11 * MIN }; + const freshNoMatch = { ...FIELDS, issuedAt: T0 + 90_000, requestId: "r2" }; + const both = stamp(staleMatch, goodDigest(staleMatch)) as { model: string; messages: unknown[] }; + both.messages.push({ role: "user", content: renderChainCheckpoint({ ...freshNoMatch, digest: "sha256:" + "ee".repeat(32) }) }); + const pref = evalAt(both, T0); + assert.equal(pref.verdict, "stale", "a verified digest outranks a fresher mismatched timestamp"); + assert.equal(pref.selected?.issuedAt, staleMatch.issuedAt); + + const a1 = { ...FIELDS, issuedAt: T0 + 40_000 }; + const a2 = { ...FIELDS, issuedAt: T0 + 80_000, requestId: "newer" }; + const multi = stamp(a1, goodDigest(a1)) as { model: string; messages: unknown[] }; + multi.messages.push({ role: "user", content: renderChainCheckpoint({ ...a2, digest: goodDigest(a2) }) }); + const finalBody = multi; + const dFinal = computeRequestDigest(finalBody, "openai"); + const rebuilt = stamp(a1, dFinal) as { model: string; messages: unknown[] }; + rebuilt.messages.push({ role: "user", content: renderChainCheckpoint({ ...a2, digest: dFinal }) }); + const latest = evalAt(rebuilt, T0 + MIN); + assert.equal(latest.verdict, "valid"); + assert.equal(latest.selected?.requestId, "newer", "latest issued-at wins within the matching-fresh class"); + + const v2 = { ...FIELDS, v: 2 }; + assert.equal(evalAt(stamp(v2, goodDigest(v2)), T0 + MIN).verdict, "invalid", "unknown version alone is never trusted"); + const mixedV = stamp(v2, goodDigest(v2)) as { model: string; messages: unknown[] }; + mixedV.messages.pop(); + const mixedBody = stamp(FIELDS, goodDigest(FIELDS)) as { model: string; messages: unknown[] }; + mixedBody.messages.push({ role: "user", content: renderChainCheckpoint({ ...v2, digest: GOOD_DIGEST }) }); + assert.equal(evalAt(mixedBody, T0 + MIN).verdict, "valid", "known-version candidate decides; unknown version ignored"); + + assert.equal(evalAt(base, T0).verdict, "none", "no signal at all"); + const malBody = insertCheckpointCarrier(base, "openai", L + "bili-chain nope" + R)!; + const malCtx = evalAt(malBody, T0); + assert.equal(malCtx.verdict, "invalid"); + assert.equal(malCtx.malformed, 1); +}); + +test("env overrides: BILI_CHAIN_MAX_FUTURE_SKEW_MS / BILI_CHAIN_RECENT_WINDOW_MS", () => { + const base = { model: "m", messages: [{ role: "user", content: "h" }] }; + const bogus = "sha256:" + "cd".repeat(32); + const futFields = { ...FIELDS, issuedAt: T0 + 20_000, requestId: "env-1" }; + const futBody = insertCheckpointCarrier(base, "openai", renderChainCheckpoint({ ...futFields, digest: bogus }))!; + try { + process.env.BILI_CHAIN_MAX_FUTURE_SKEW_MS = "15000"; + assert.equal(evaluateChain(futBody, "openai", { nowMs: T0 }).verdict, "invalid", "20s ahead exceeds tightened 15s skew — no usable candidate"); + } finally { + delete process.env.BILI_CHAIN_MAX_FUTURE_SKEW_MS; + } + assert.equal(evaluateChain(futBody, "openai", { nowMs: T0 }).verdict, "recent-mismatch", `default ${DEFAULT_MAX_FUTURE_SKEW_MS}ms skew keeps 20s ahead fresh`); + // 590s old: inside the default 600s window, outside a tightened 580s one. + const pastFields = { ...FIELDS, issuedAt: T0 - 9 * MIN - 50_000, requestId: "env-2" }; + const pastBody = insertCheckpointCarrier(base, "openai", renderChainCheckpoint({ ...pastFields, digest: computeCheckpointDigest(base, "openai", pastFields)! }))!; + try { + process.env.BILI_CHAIN_RECENT_WINDOW_MS = String(DEFAULT_RECENT_CHECKPOINT_WINDOW_MS - 20_000); + assert.equal(evaluateChain(pastBody, "openai", { nowMs: T0 }).verdict, "stale", "tightened window (580s) ages a 590s-old match"); + } finally { + delete process.env.BILI_CHAIN_RECENT_WINDOW_MS; + } + assert.equal(evaluateChain(pastBody, "openai", { nowMs: T0 }).verdict, "valid", `default ${DEFAULT_RECENT_CHECKPOINT_WINDOW_MS}ms window keeps a 590s-old match fresh`); +}); + +// ---------- integration: real proxy, ZERO forwarding behavior change ---------- + +function listen(server: http.Server): Promise { + if (server.listening) return Promise.resolve(); + return once(server, "listening").then(() => undefined); +} + +function close(server: http.Server): Promise { + return new Promise((resolve, reject) => server.close((error) => (error ? reject(error) : resolve()))); +} + +const MODEL = "gpt-test"; + +function makeOpts(port: number, upstream: string): ProxyOptions { + return { + port, + host: "127.0.0.1", + upstream, + routes: { [upstream]: { models: { [MODEL]: { context: 400_000 } } } }, + modelContextLimit: 400_000, + kernelConfig: defaultConfig(400_000), + compress: { injectTool: true, injectNudge: true }, + promptCache: { routing: "auto" }, + sessionHeader: "x-acp-session", + log: true, + debug: false, + passthrough: false, + autoUpdate: false, + logFile: "off", + mitm: { enabled: false, domains: [] }, + }; +} + +type Captured = { url: string; headers: http.IncomingHttpHeaders; body: string }; + +function makeUpstream(captured: Captured[]): http.Server { + return http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (c: Buffer) => chunks.push(c)); + req.on("end", () => { + const body = Buffer.concat(chunks).toString("utf8"); + captured.push({ url: req.url ?? "", headers: req.headers, body }); + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ + id: "chatcmpl-test", + object: "chat.completion", + model: MODEL, + choices: [{ index: 0, message: { role: "assistant", content: "ok" }, finish_reason: "stop" }], + usage: { prompt_tokens: 10, completion_tokens: 5, total_tokens: 15 }, + })); + }); + }); +} + +interface Rig { + port: number; + logs: { level: string; msg: string }[]; + captured: Captured[]; +} + +async function withRig(fn: (rig: Rig) => Promise): Promise { + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + const logs: { level: string; msg: string }[] = []; + setLogCapture((level, msg) => logs.push({ level, msg })); + const captured: Captured[] = []; + const upstream = makeUpstream(captured); + upstream.listen(0, "127.0.0.1"); + await listen(upstream); + const llmUrl = `http://127.0.0.1:${(upstream.address() as { port: number }).port}`; + const srv = await startServer(makeOpts(0, llmUrl)); + await listen(srv); + try { + await fn({ port: (srv.address() as { port: number }).port, logs, captured }); + } finally { + setLogCapture(null); + srv.closeAllConnections?.(); + await close(srv); + upstream.closeAllConnections?.(); + await close(upstream); + } +} + +const BASE_BODY = { + model: MODEL, + stream: false, + messages: [ + { role: "system", content: "You are a test assistant." }, + { role: "user", content: "hello world" }, + ], +}; + +test("#1395 step 2 T1: valid checkpoint → shadow info log only; request still fully processed (no passthrough)", async () => { + await withRig(async ({ port, logs, captured }) => { + const fields = { v: 1, processor: "bili-upstream-test", issuedAt: Date.now(), requestId: "req-step2-valid" }; + const sentJson = JSON.stringify(insertCheckpointCarrier(BASE_BODY, "openai", renderChainCheckpoint({ ...fields, digest: computeCheckpointDigest(BASE_BODY, "openai", fields)! }))!); + const resp = await fetch(`http://127.0.0.1:${port}/v1/chat/completions`, { + method: "POST", + headers: { "content-type": "application/json", "x-acp-session": "chain-step2-valid" }, + body: sentJson, + }); + assert.equal(resp.status, 200); + await resp.text(); + const shadows = logs.filter((l) => l.msg.includes("[chain-shadow]")); + assert.equal(shadows.length, 1, `expected exactly one [chain-shadow] log, got ${shadows.length}: ${JSON.stringify(shadows)}`); + assert.equal(shadows[0]!.level, "info"); + assert.ok(shadows[0]!.msg.includes("verdict=valid"), shadows[0]!.msg); + assert.ok(shadows[0]!.msg.includes("request-id=req-step2-valid"), shadows[0]!.msg); + assert.equal(captured.length, 1, "request reaches the LLM exactly once"); + assert.notEqual(captured[0]!.body, sentJson, "step 2 must NOT pass through verbatim (zero forwarding change)"); + assert.ok(captured[0]!.body.includes('"compress"'), "request must be fully processed (compress tool injected)"); + }); +}); + +test("#1395 step 2 T2: well-formed fresh checkpoint with wrong digest → recent-mismatch warn; still processed", async () => { + await withRig(async ({ port, logs, captured }) => { + const fields = { v: 1, processor: "bili-other", issuedAt: Date.now(), requestId: "req-step2-mismatch" }; + const sentJson = JSON.stringify(insertCheckpointCarrier(BASE_BODY, "openai", renderChainCheckpoint({ ...fields, digest: "sha256:" + "cd".repeat(32) }))!); + const resp = await fetch(`http://127.0.0.1:${port}/v1/chat/completions`, { + method: "POST", + headers: { "content-type": "application/json", "x-acp-session": "chain-step2-mismatch" }, + body: sentJson, + }); + assert.equal(resp.status, 200); + await resp.text(); + const shadows = logs.filter((l) => l.msg.includes("[chain-shadow]")); + assert.equal(shadows.length, 1, `expected exactly one [chain-shadow] log, got ${shadows.length}: ${JSON.stringify(shadows)}`); + assert.equal(shadows[0]!.level, "warn"); + assert.ok(shadows[0]!.msg.includes("verdict=recent-mismatch"), shadows[0]!.msg); + assert.equal(captured.length, 1); + assert.notEqual(captured[0]!.body, sentJson, "no forwarding behavior change in step 2"); + assert.ok(captured[0]!.body.includes('"compress"')); + }); +}); + +test("#1395 step 2 T3: plain request → no [chain-shadow] log at all", async () => { + await withRig(async ({ port, logs, captured }) => { + const sentJson = JSON.stringify(BASE_BODY); + const resp = await fetch(`http://127.0.0.1:${port}/v1/chat/completions`, { + method: "POST", + headers: { "content-type": "application/json", "x-acp-session": "chain-step2-plain" }, + body: sentJson, + }); + assert.equal(resp.status, 200); + await resp.text(); + assert.equal(logs.filter((l) => l.msg.includes("[chain-shadow]")).length, 0, "no checkpoint signal, no shadow log"); + assert.equal(captured.length, 1); + assert.ok(captured[0]!.body.includes('"compress"')); + }); +});