From cba9c438666ee3457acf30eeed396bfb75a6c8ab Mon Sep 17 00:00:00 2001 From: bueryi <98598692+yanhuojiyusheng@users.noreply.github.com> Date: Mon, 19 Oct 2026 15:27:34 +0800 Subject: [PATCH 1/4] fix: propagate system removal in native incremental turns --- src/agent/incremental-client.ts | 748 +++++++++++----------- tests/incremental-lane.test.ts | 1066 ++++++++++++++++--------------- 2 files changed, 929 insertions(+), 885 deletions(-) diff --git a/src/agent/incremental-client.ts b/src/agent/incremental-client.ts index dc1451d52b..66b3c095d4 100644 --- a/src/agent/incremental-client.ts +++ b/src/agent/incremental-client.ts @@ -1,375 +1,373 @@ -// Shared incremental-turn client for native extensions (#3248). -// -// Lives in the HOST process (bundled with each native adapter: pi / omp / -// fetch-patched native lanes) and owns the per-conversation ack state: -// 1. probe the lane once per proxy base (GET /__bili/plugin/turn, TTL -// cache; 404/failed → stay full-resend, byte-identical to today); -// 2. diff the outgoing wire messages against the last acknowledged set — -// matching prefix → send only the delta; mismatch (edit / branch) → -// resync with the full history; the server's ack header is the only -// authority for sinceSeq; -// 3. any lane-level failure (409 storm, 503) falls back to the original -// full-resend request with the marker removed — behavior equals the -// pre-incremental world for that turn. -// -// Wire contract: see PLUGIN.md "Incremental turns". Messages are raw wire -// messages ({role, content: string | blocks[]}) — the lane derives stable -// content ids server-side, so hosts without their own message ids work. - -import { createHash } from "node:crypto"; -import type { DirectSendPrep } from "./native-intercept.js"; - -/** Marker header the lane's divert recognizes. Client-side copy of - * INCREMENTAL_TURN_MARKER_HEADER in src/incremental-lane.ts — the host-side - * helper must not import server modules. Keep the two in sync. */ -export const INCREMENTAL_TURN_MARKER_HEADER = "x-bili-plugin-turn"; - -/** #3329: per-request correlation id stamped on each planned incremental - * turn. The ack channel (native-intercept's onResponse) only carries the URL, - * and several conversations share one provider URL — keying the pending-ack - * table by URL mis-attributes a late response to whichever conversation - * planned last on that URL. The id rides the outgoing request and is read - * back from THAT dispatch's own headers when its response lands, so - * attribution is exact per request. It never travels to the proxy for any - * purpose (the lane ignores it) — pure in-process correlation. */ -export const INCREMENTAL_TURN_ID_HEADER = "x-bili-turn-id"; - -let turnIdCounter = 0; - -/** Fresh per-request turn id: time base + process counter + random tail. - * Uniqueness only needs to hold inside this process (the id never crosses a - * wire boundary for correlation purposes). */ -export function newIncrementalTurnId(): string { - return `${Date.now().toString(36)}-${(turnIdCounter++).toString(36)}-${Math.random().toString(36).slice(2, 10)}`; -} - -// #3304: originals stash for the direct-send guard. When an adapter swaps a -// request's body for the incremental payload, it stashes the ORIGINAL body -// string under that turn's id. If the proxy dies before the send lands, -// native-intercept's fall-back consults createPrepareDirectSend below and -// either restores these bytes (availability) or refuses the direct send -// (safety) — a lane-only payload never reaches a provider that cannot parse -// it. Entries age out by TTL (a single turn never outlives minutes) and are -// capped so a leak cannot grow the map. -const ORIGINAL_TTL_MS = 5 * 60_000; -const ORIGINAL_MAX_ENTRIES = 256; -const originalBodies = new Map(); - -/** Stash the pre-swap body of one planned turn under its turn id. */ -export function rememberOriginalBody(turnId: string, body: string): void { - const now = Date.now(); - for (const [key, entry] of originalBodies) if (now - entry.at >= ORIGINAL_TTL_MS) originalBodies.delete(key); - if (originalBodies.size >= ORIGINAL_MAX_ENTRIES) { - let oldestKey: string | undefined; - let oldestAt = Number.POSITIVE_INFINITY; - for (const [key, entry] of originalBodies) { - if (entry.at < oldestAt) { oldestAt = entry.at; oldestKey = key; } - } - if (oldestKey !== undefined) originalBodies.delete(oldestKey); - } - originalBodies.set(turnId, { body, at: now }); -} - -/** Non-consuming lookup: a host-level retry re-dispatches the SAME request - * (same turn id) and must get the original back again, so entries are kept - * until TTL instead of being deleted on read. */ -export function originalBodyForTurn(turnId: string): string | null { - const entry = originalBodies.get(turnId); - if (entry === undefined || Date.now() - entry.at >= ORIGINAL_TTL_MS) return null; - return entry.body; -} - -/** #3304: prepareDirectSend implementation for lanes that stamp the turn id - * ATOMICALLY with the body swap (dsh / omp / opencode-native): presence of - * x-bili-turn-id on the outgoing request proves the body was replaced, so - * a missing/expired original means "mutated but unrecoverable" → refuse - * rather than risk sending lane-only bytes upstream. Lanes without the hook - * (every non-incremental traffic) keep today's byte-identical behavior. */ -export function createPrepareDirectSend(): (url: string, input: string | URL | Request, init: RequestInit | undefined) => Promise { - return async (_url, input, init) => { - let turnId: string | null = null; - try { - const src = (init?.headers ?? (input instanceof Request ? input.headers : undefined)) as ConstructorParameters[0] | undefined; - if (src !== undefined) turnId = new Headers(src).get(INCREMENTAL_TURN_ID_HEADER); - } catch { - turnId = null; - } - if (turnId === null) return { outcome: "as-is" }; - const body = originalBodyForTurn(turnId); - return body === null ? { outcome: "unrestorable" } : { outcome: "restored", body }; - }; -} - -/** #3304: is this parsed body an incremental envelope — i.e. WOULD the - * lane's marker divert claim it? Mirrors incrementalTurnBodyShape in - * src/incremental-lane.ts (keep in sync): such a body is lane-only, so it - * must never be forwarded to the real upstream as-is. Returns the - * conversationId when it matches (pi correlates its stash through it), - * undefined otherwise. */ -export function incrementalEnvelopeConversationId(body: unknown): string | undefined { - if (body === null || typeof body !== "object" || Array.isArray(body)) return undefined; - const b = body as Record; - if (typeof b.conversationId !== "string" || b.conversationId.length === 0) return undefined; - if (typeof b.sinceSeq !== "number" || !Number.isInteger(b.sinceSeq) || b.sinceSeq < 0) return undefined; - if (!Array.isArray(b.messages)) return undefined; - if (b.model !== undefined) return undefined; - return b.conversationId; -} - -/** #3329: drop pending entries older than `ttlMs` — a planned turn whose - * response never comes (network-dead request) must not pin its slot forever. - * Memory hygiene only since attribution is exact per request: correctness no - * longer depends on how long an entry survives. */ -export function sweepStale(map: Map, ttlMs: number): void { - const now = Date.now(); - for (const [key, entry] of map) if (now - entry.at >= ttlMs) map.delete(key); -} - -/** Same allowlist the lane accepts in `request` (mirror of - * REQUEST_PARAM_KEYS in src/incremental-lane.ts — keep in sync). */ -const REQUEST_PARAM_KEYS = [ - "model", "max_tokens", "temperature", "top_p", "top_k", "stop_sequences", - "stream", "tools", "tool_choice", "metadata", "thinking", "service_tier", - "parallel_tool_calls", - // Anthropic-on-Vertex has no `model` in the body — `anthropic_version` - // carries the wire revision instead (live capture 2026-10-15). Must stay - // identical to the lane's copy in src/incremental-lane.ts. - "anthropic_version", -] as const; -const REQUEST_PARAM_KEY_SET = new Set(REQUEST_PARAM_KEYS); - -/** Keys the lane assembles from the wire's top level — hosts pass the full - * anthropic body as `request`, so these are by-design, not dropped. */ -const LANE_OWNED_REQUEST_KEYS = new Set(["messages", "system"]); - -/** #3406 item 6: params dropped here never reach the lane (its own warn - * cannot fire for helper-planned turns), so surface them once per - * conversation+key. Bounded: past the pair budget the map resets and the - * next epoch warns again. */ -const droppedParamWarned = new Map>(); -let droppedParamWarnPairs = 0; -const DROPPED_PARAM_WARN_BUDGET = 4096; - -function warnDroppedRequestParams(conversationId: string, request: Record): void { - const fresh = Object.keys(request).filter((key) => !REQUEST_PARAM_KEY_SET.has(key) && !LANE_OWNED_REQUEST_KEYS.has(key)); - if (fresh.length === 0) return; - const seen = droppedParamWarned.get(conversationId) ?? new Set(); - const unwarned = fresh.filter((key) => !seen.has(key)); - if (unwarned.length === 0) return; - if (droppedParamWarnPairs + unwarned.length > DROPPED_PARAM_WARN_BUDGET) { - droppedParamWarned.clear(); - droppedParamWarnPairs = 0; - seen.clear(); - } - droppedParamWarnPairs += unwarned.length; - for (const key of unwarned) seen.add(key); - droppedParamWarned.set(conversationId, seen); - console.error(`[bili-incremental] ${conversationId}: request parameter(s) outside the incremental allowlist are not sent: ${unwarned.join(", ")} — extend REQUEST_PARAM_KEYS (src/agent/incremental-client.ts) if they must reach the upstream (#3406)`); -} - -export interface IncrementalWireMessage { - role: string; - content: unknown; -} - -interface ConvState { - /** Last server ack (x-bili-applied-seq) — flat message count. */ - sinceSeq: number; - /** Wire hashes of everything the server has acknowledged, in order. */ - sent: string[]; - /** Sticky system (JSON) we told the lane; resend only on change. */ - systemJson: string | undefined; - /** Per-conversation kill switch after unrecoverable failures. */ - dead: boolean; -} - -const convs = new Map(); - -const PROBE_TTL_MS = 30_000; -const probeCache = new Map(); - -/** Probe the lane's incremental support. Cached per proxy base for 30s; any - * error or non-200 → not enabled (older servers 404 the GET). */ -export async function probeIncrementalTurns(proxyBase: string, fetchImpl: typeof fetch = fetch): Promise { - const now = Date.now(); - const cached = probeCache.get(proxyBase); - if (cached && now - cached.at < PROBE_TTL_MS) return cached.ok; - let ok = false; - try { - const res = await fetchImpl(`${proxyBase}/__bili/plugin/turn`, { method: "GET", signal: AbortSignal.timeout(3000) }); - if (res.ok) { - const body = await res.json() as { enabled?: unknown }; - ok = body.enabled === true; - } - } catch { - ok = false; - } - probeCache.set(proxyBase, { ok, at: now }); - return ok; -} - -/** Does this message list look like the Anthropic wire shape the lane's - * anthropicToCore can ingest? OpenAI-tool shapes (role:"tool"/"function", - * assistant.tool_calls, tool_call_id) are NOT convertible — the adapter - * falls back to a full resend for those conversations (correctness over - * savings). Content may be a string or a block array, both fine. */ -export function wireMessagesLookAnthropic(messages: unknown): messages is IncrementalWireMessage[] { - if (!Array.isArray(messages) || messages.length === 0) return false; - for (const m of messages) { - if (m === null || typeof m !== "object") return false; - const msg = m as { role?: unknown; content?: unknown; tool_calls?: unknown; tool_call_id?: unknown }; - if (typeof msg.role !== "string" || msg.role === "tool" || msg.role === "function") return false; - if (msg.tool_calls !== undefined || msg.tool_call_id !== undefined) return false; - if (typeof msg.content !== "string" && !Array.isArray(msg.content)) return false; - } - return true; -} - -function wireHash(m: IncrementalWireMessage): string { - return createHash("sha256").update(`${m.role}\u0000${JSON.stringify(m.content)}`).digest("hex").slice(0, 16); -} - -type IncrementalPlan = - | { mode: "seed"; body: string } // full history, sinceSeq 0 (server may 409 → resync) - | { mode: "delta"; body: string } // suffix only, acked sinceSeq - | { mode: "resync"; body: string } // full history, resync: true (recovery) - | { mode: "skip" }; // nothing to gain / state dead → full resend - -interface PlanArgs { - conversationId: string; - /** Forward path of the ORIGINAL request (the marker divert uses it); - * optional — the lane defaults a bare `path` to /v1/messages. */ - path?: string; - messages: IncrementalWireMessage[]; - /** Raw wire system field; omit key entirely when the host sent none. */ - system: unknown; - /** Anthropic request params from the original body (model, max_tokens…); - * optional — missing params ride the lane's defaults. */ - request?: Record; -} - -function buildBody(args: PlanArgs, sinceSeq: number, messages: IncrementalWireMessage[], resync: boolean, includeSystem: boolean): string { - // `request` is optional at the call sites (a host may plan with only - // messages+system); missing params simply ride their lane defaults. - const src = args.request ?? {}; - const request: Record = {}; - for (const key of REQUEST_PARAM_KEYS) if (key in src) request[key] = src[key]; - warnDroppedRequestParams(args.conversationId, src); - const body: Record = { - conversationId: args.conversationId, - sinceSeq, - path: args.path ?? "/v1/messages", - request, - messages, - }; - if (resync) body.resync = true; - if (includeSystem) body.system = args.system; - return JSON.stringify(body); -} - -/** Plan the incremental form of one outgoing model request. `forceResync` - * replans after a 409 (seed hit a live canonical, or an edit diverged). */ -export function planIncrementalTurn(args: PlanArgs, forceResync = false): IncrementalPlan { - const state = convs.get(args.conversationId); - if ((state !== undefined && state.dead) || args.messages.length === 0) return { mode: "skip" }; - if (state === undefined || forceResync) { - const systemJson = args.system === undefined ? undefined : JSON.stringify(args.system); - const includeSystem = state === undefined || systemJson !== state.systemJson; - // First attempt (and every forced replan) sends the FULL set with - // resync:true — a wholesale canonical replacement never 409s, so the - // first incremental turn works whether the server-side history is - // empty (fresh conversation) or already live (interop with - // full-resend traffic: the client's view is authoritative and - // identical-or-newer). Only DELTA turns can 409. - return { mode: forceResync ? "resync" : "seed", body: buildBody(args, 0, args.messages, true, includeSystem) }; - } - const hashes = args.messages.map(wireHash); - // Prefix alignment: everything previously acked must still lead the list. - const n = Math.min(state.sent.length, hashes.length); - let aligned = true; - for (let i = 0; i < n; i++) { - if (state.sent[i] !== hashes[i]) { aligned = false; break; } - } - if (!aligned || hashes.length < state.sent.length) { - return { mode: "resync", body: buildBody(args, 0, args.messages, true, args.system === undefined ? false : JSON.stringify(args.system) !== state.systemJson) }; - } - if (hashes.length === state.sent.length) { - // Equal message set: normally a retry of the same turn → normal path. - // But a SYSTEM change is still a real outgoing turn — send an empty - // delta so the lane forwards the assembled request with the new - // sticky system (the canonical itself is unchanged). - const sysJson = args.system === undefined ? undefined : JSON.stringify(args.system); - if (sysJson !== undefined && sysJson !== state.systemJson) { - return { mode: "delta", body: buildBody(args, state.sinceSeq, [], false, true) }; - } - return { mode: "skip" }; // retry of the same turn → normal path - } - const systemJson = args.system === undefined ? undefined : JSON.stringify(args.system); - return { - mode: "delta", - body: buildBody(args, state.sinceSeq, args.messages.slice(state.sent.length), false, systemJson !== undefined && systemJson !== state.systemJson), - }; -} - -/** Record a 200 ack: `appliedSeq` is the response's x-bili-applied-seq and - * `messages` the wire set the server just acknowledged. An ack clears the - * rejection circuit — the conversation is healthy again. */ -export function noteIncrementalAck(conversationId: string, appliedSeq: number, messages: IncrementalWireMessage[], system: unknown): void { - rejectionCircuit.delete(conversationId); - convs.set(conversationId, { - sinceSeq: appliedSeq, - sent: messages.map(wireHash), - systemJson: system === undefined ? undefined : JSON.stringify(system), - dead: false, - }); -} - -/** Consecutive 409s (no intervening ack) that trip the sticky kill switch. - * A SINGLE 409 just resets the conversation state — the next turn re-seeds - * with a full resync:true body, which cannot 409 — so transient drift (sidecar - * degraded at boot, one upstream 5xx between apply and forward, a sibling host - * re-seeding) self-heals on the very next turn instead of permanently - * downgrading the conversation (#3369). */ -const REJECTION_CIRCUIT = 3; - -/** Consecutive rejections per conversation, OUTSIDE `convs` — a reset deletes - * the wire state, but the circuit counter must survive it. */ -const rejectionCircuit = new Map(); - -/** Record a lane rejection (409 seq drift). Returns "reset" when the next - * turn re-seeds (transient drift — the common case) and "dead" when the - * sticky kill switch tripped (persistent drift: full resend for the rest of - * the process lifetime, exactly the pre-#3369 behavior). */ -export function noteIncrementalRejection(conversationId: string): "reset" | "dead" { - const strikes = (rejectionCircuit.get(conversationId) ?? 0) + 1; - if (strikes >= REJECTION_CIRCUIT) { - rejectionCircuit.delete(conversationId); - convs.set(conversationId, { sinceSeq: 0, sent: [], systemJson: undefined, dead: true }); - return "dead"; - } - rejectionCircuit.set(conversationId, strikes); - convs.delete(conversationId); // next turn re-seeds: full set, resync:true, sinceSeq 0 - return "reset"; -} - -/** Sticky per-conversation fallback after an unrecoverable lane failure — - * the conversation keeps working via plain full resend. */ -export function markIncrementalDead(conversationId: string): void { - const state = convs.get(conversationId); - if (state) state.dead = true; -} - -/** Drop the cached probe for one proxy base (e.g. after a 503 — the lane's - * rollout flag or sidecar liveness changed). The next turn re-probes. */ -export function clearIncrementalProbe(proxyBase: string): void { - probeCache.delete(proxyBase); -} - -export function __resetIncrementalClientForTests(): void { - convs.clear(); - probeCache.clear(); - rejectionCircuit.clear(); - droppedParamWarned.clear(); - droppedParamWarnPairs = 0; - originalBodies.clear(); -} +// Shared incremental-turn client for native extensions (#3248). +// +// Lives in the HOST process (bundled with each native adapter: pi / omp / +// fetch-patched native lanes) and owns the per-conversation ack state: +// 1. probe the lane once per proxy base (GET /__bili/plugin/turn, TTL +// cache; 404/failed → stay full-resend, byte-identical to today); +// 2. diff the outgoing wire messages against the last acknowledged set — +// matching prefix → send only the delta; mismatch (edit / branch) → +// resync with the full history; the server's ack header is the only +// authority for sinceSeq; +// 3. any lane-level failure (409 storm, 503) falls back to the original +// full-resend request with the marker removed — behavior equals the +// pre-incremental world for that turn. +// +// Wire contract: see PLUGIN.md "Incremental turns". Messages are raw wire +// messages ({role, content: string | blocks[]}) — the lane derives stable +// content ids server-side, so hosts without their own message ids work. + +import { createHash } from "node:crypto"; +import type { DirectSendPrep } from "./native-intercept.js"; + +/** Marker header the lane's divert recognizes. Client-side copy of + * INCREMENTAL_TURN_MARKER_HEADER in src/incremental-lane.ts — the host-side + * helper must not import server modules. Keep the two in sync. */ +export const INCREMENTAL_TURN_MARKER_HEADER = "x-bili-plugin-turn"; + +/** #3329: per-request correlation id stamped on each planned incremental + * turn. The ack channel (native-intercept's onResponse) only carries the URL, + * and several conversations share one provider URL — keying the pending-ack + * table by URL mis-attributes a late response to whichever conversation + * planned last on that URL. The id rides the outgoing request and is read + * back from THAT dispatch's own headers when its response lands, so + * attribution is exact per request. It never travels to the proxy for any + * purpose (the lane ignores it) — pure in-process correlation. */ +export const INCREMENTAL_TURN_ID_HEADER = "x-bili-turn-id"; + +let turnIdCounter = 0; + +/** Fresh per-request turn id: time base + process counter + random tail. + * Uniqueness only needs to hold inside this process (the id never crosses a + * wire boundary for correlation purposes). */ +export function newIncrementalTurnId(): string { + return `${Date.now().toString(36)}-${(turnIdCounter++).toString(36)}-${Math.random().toString(36).slice(2, 10)}`; +} + +// #3304: originals stash for the direct-send guard. When an adapter swaps a +// request's body for the incremental payload, it stashes the ORIGINAL body +// string under that turn's id. If the proxy dies before the send lands, +// native-intercept's fall-back consults createPrepareDirectSend below and +// either restores these bytes (availability) or refuses the direct send +// (safety) — a lane-only payload never reaches a provider that cannot parse +// it. Entries age out by TTL (a single turn never outlives minutes) and are +// capped so a leak cannot grow the map. +const ORIGINAL_TTL_MS = 5 * 60_000; +const ORIGINAL_MAX_ENTRIES = 256; +const originalBodies = new Map(); + +/** Stash the pre-swap body of one planned turn under its turn id. */ +export function rememberOriginalBody(turnId: string, body: string): void { + const now = Date.now(); + for (const [key, entry] of originalBodies) if (now - entry.at >= ORIGINAL_TTL_MS) originalBodies.delete(key); + if (originalBodies.size >= ORIGINAL_MAX_ENTRIES) { + let oldestKey: string | undefined; + let oldestAt = Number.POSITIVE_INFINITY; + for (const [key, entry] of originalBodies) { + if (entry.at < oldestAt) { oldestAt = entry.at; oldestKey = key; } + } + if (oldestKey !== undefined) originalBodies.delete(oldestKey); + } + originalBodies.set(turnId, { body, at: now }); +} + +/** Non-consuming lookup: a host-level retry re-dispatches the SAME request + * (same turn id) and must get the original back again, so entries are kept + * until TTL instead of being deleted on read. */ +export function originalBodyForTurn(turnId: string): string | null { + const entry = originalBodies.get(turnId); + if (entry === undefined || Date.now() - entry.at >= ORIGINAL_TTL_MS) return null; + return entry.body; +} + +/** #3304: prepareDirectSend implementation for lanes that stamp the turn id + * ATOMICALLY with the body swap (dsh / omp / opencode-native): presence of + * x-bili-turn-id on the outgoing request proves the body was replaced, so + * a missing/expired original means "mutated but unrecoverable" → refuse + * rather than risk sending lane-only bytes upstream. Lanes without the hook + * (every non-incremental traffic) keep today's byte-identical behavior. */ +export function createPrepareDirectSend(): (url: string, input: string | URL | Request, init: RequestInit | undefined) => Promise { + return async (_url, input, init) => { + let turnId: string | null = null; + try { + const src = (init?.headers ?? (input instanceof Request ? input.headers : undefined)) as ConstructorParameters[0] | undefined; + if (src !== undefined) turnId = new Headers(src).get(INCREMENTAL_TURN_ID_HEADER); + } catch { + turnId = null; + } + if (turnId === null) return { outcome: "as-is" }; + const body = originalBodyForTurn(turnId); + return body === null ? { outcome: "unrestorable" } : { outcome: "restored", body }; + }; +} + +/** #3304: is this parsed body an incremental envelope — i.e. WOULD the + * lane's marker divert claim it? Mirrors incrementalTurnBodyShape in + * src/incremental-lane.ts (keep in sync): such a body is lane-only, so it + * must never be forwarded to the real upstream as-is. Returns the + * conversationId when it matches (pi correlates its stash through it), + * undefined otherwise. */ +export function incrementalEnvelopeConversationId(body: unknown): string | undefined { + if (body === null || typeof body !== "object" || Array.isArray(body)) return undefined; + const b = body as Record; + if (typeof b.conversationId !== "string" || b.conversationId.length === 0) return undefined; + if (typeof b.sinceSeq !== "number" || !Number.isInteger(b.sinceSeq) || b.sinceSeq < 0) return undefined; + if (!Array.isArray(b.messages)) return undefined; + if (b.model !== undefined) return undefined; + return b.conversationId; +} + +/** #3329: drop pending entries older than `ttlMs` — a planned turn whose + * response never comes (network-dead request) must not pin its slot forever. + * Memory hygiene only since attribution is exact per request: correctness no + * longer depends on how long an entry survives. */ +export function sweepStale(map: Map, ttlMs: number): void { + const now = Date.now(); + for (const [key, entry] of map) if (now - entry.at >= ttlMs) map.delete(key); +} + +/** Same allowlist the lane accepts in `request` (mirror of + * REQUEST_PARAM_KEYS in src/incremental-lane.ts — keep in sync). */ +const REQUEST_PARAM_KEYS = [ + "model", "max_tokens", "temperature", "top_p", "top_k", "stop_sequences", + "stream", "tools", "tool_choice", "metadata", "thinking", "service_tier", + "parallel_tool_calls", + // Anthropic-on-Vertex has no `model` in the body — `anthropic_version` + // carries the wire revision instead (live capture 2026-10-15). Must stay + // identical to the lane's copy in src/incremental-lane.ts. + "anthropic_version", +] as const; +const REQUEST_PARAM_KEY_SET = new Set(REQUEST_PARAM_KEYS); + +/** Keys the lane assembles from the wire's top level — hosts pass the full + * anthropic body as `request`, so these are by-design, not dropped. */ +const LANE_OWNED_REQUEST_KEYS = new Set(["messages", "system"]); + +/** #3406 item 6: params dropped here never reach the lane (its own warn + * cannot fire for helper-planned turns), so surface them once per + * conversation+key. Bounded: past the pair budget the map resets and the + * next epoch warns again. */ +const droppedParamWarned = new Map>(); +let droppedParamWarnPairs = 0; +const DROPPED_PARAM_WARN_BUDGET = 4096; + +function warnDroppedRequestParams(conversationId: string, request: Record): void { + const fresh = Object.keys(request).filter((key) => !REQUEST_PARAM_KEY_SET.has(key) && !LANE_OWNED_REQUEST_KEYS.has(key)); + if (fresh.length === 0) return; + const seen = droppedParamWarned.get(conversationId) ?? new Set(); + const unwarned = fresh.filter((key) => !seen.has(key)); + if (unwarned.length === 0) return; + if (droppedParamWarnPairs + unwarned.length > DROPPED_PARAM_WARN_BUDGET) { + droppedParamWarned.clear(); + droppedParamWarnPairs = 0; + seen.clear(); + } + droppedParamWarnPairs += unwarned.length; + for (const key of unwarned) seen.add(key); + droppedParamWarned.set(conversationId, seen); + console.error(`[bili-incremental] ${conversationId}: request parameter(s) outside the incremental allowlist are not sent: ${unwarned.join(", ")} — extend REQUEST_PARAM_KEYS (src/agent/incremental-client.ts) if they must reach the upstream (#3406)`); +} + +export interface IncrementalWireMessage { + role: string; + content: unknown; +} + +interface ConvState { + /** Last server ack (x-bili-applied-seq) — flat message count. */ + sinceSeq: number; + /** Wire hashes of everything the server has acknowledged, in order. */ + sent: string[]; + /** Sticky system (JSON) we told the lane; resend only on change. */ + systemJson: string | undefined; + /** Per-conversation kill switch after unrecoverable failures. */ + dead: boolean; +} + +const convs = new Map(); + +const PROBE_TTL_MS = 30_000; +const probeCache = new Map(); + +/** Probe the lane's incremental support. Cached per proxy base for 30s; any + * error or non-200 → not enabled (older servers 404 the GET). */ +export async function probeIncrementalTurns(proxyBase: string, fetchImpl: typeof fetch = fetch): Promise { + const now = Date.now(); + const cached = probeCache.get(proxyBase); + if (cached && now - cached.at < PROBE_TTL_MS) return cached.ok; + let ok = false; + try { + const res = await fetchImpl(`${proxyBase}/__bili/plugin/turn`, { method: "GET", signal: AbortSignal.timeout(3000) }); + if (res.ok) { + const body = await res.json() as { enabled?: unknown }; + ok = body.enabled === true; + } + } catch { + ok = false; + } + probeCache.set(proxyBase, { ok, at: now }); + return ok; +} + +/** Does this message list look like the Anthropic wire shape the lane's + * anthropicToCore can ingest? OpenAI-tool shapes (role:"tool"/"function", + * assistant.tool_calls, tool_call_id) are NOT convertible — the adapter + * falls back to a full resend for those conversations (correctness over + * savings). Content may be a string or a block array, both fine. */ +export function wireMessagesLookAnthropic(messages: unknown): messages is IncrementalWireMessage[] { + if (!Array.isArray(messages) || messages.length === 0) return false; + for (const m of messages) { + if (m === null || typeof m !== "object") return false; + const msg = m as { role?: unknown; content?: unknown; tool_calls?: unknown; tool_call_id?: unknown }; + if (typeof msg.role !== "string" || msg.role === "tool" || msg.role === "function") return false; + if (msg.tool_calls !== undefined || msg.tool_call_id !== undefined) return false; + if (typeof msg.content !== "string" && !Array.isArray(msg.content)) return false; + } + return true; +} + +function wireHash(m: IncrementalWireMessage): string { + return createHash("sha256").update(`${m.role}\u0000${JSON.stringify(m.content)}`).digest("hex").slice(0, 16); +} + +type IncrementalPlan = + | { mode: "seed"; body: string } // full history, sinceSeq 0 (server may 409 → resync) + | { mode: "delta"; body: string } // suffix only, acked sinceSeq + | { mode: "resync"; body: string } // full history, resync: true (recovery) + | { mode: "skip" }; // nothing to gain / state dead → full resend + +interface PlanArgs { + conversationId: string; + /** Forward path of the ORIGINAL request (the marker divert uses it); + * optional — the lane defaults a bare `path` to /v1/messages. */ + path?: string; + messages: IncrementalWireMessage[]; + /** System from the complete host request; undefined means the host sent none. */ + system: unknown; + /** Anthropic request params from the original body (model, max_tokens…); + * optional — missing params ride the lane's defaults. */ + request?: Record; +} + +function buildBody(args: PlanArgs, sinceSeq: number, messages: IncrementalWireMessage[], resync: boolean, includeSystem: boolean): string { + // `request` is optional at the call sites (a host may plan with only + // messages+system); missing params simply ride their lane defaults. + const src = args.request ?? {}; + const request: Record = {}; + for (const key of REQUEST_PARAM_KEYS) if (key in src) request[key] = src[key]; + warnDroppedRequestParams(args.conversationId, src); + const body: Record = { + conversationId: args.conversationId, + sinceSeq, + path: args.path ?? "/v1/messages", + request, + messages, + }; + if (resync) body.resync = true; + if (includeSystem) body.system = args.system === undefined ? null : args.system; + return JSON.stringify(body); +} + +/** Plan the incremental form of one outgoing model request. `forceResync` + * replans after a 409 (seed hit a live canonical, or an edit diverged). */ +export function planIncrementalTurn(args: PlanArgs, forceResync = false): IncrementalPlan { + const state = convs.get(args.conversationId); + if ((state !== undefined && state.dead) || args.messages.length === 0) return { mode: "skip" }; + const systemJson = args.system === undefined ? undefined : JSON.stringify(args.system); + if (state === undefined || forceResync) { + const includeSystem = state === undefined || systemJson !== state.systemJson; + // First attempt (and every forced replan) sends the FULL set with + // resync:true — a wholesale canonical replacement never 409s, so the + // first incremental turn works whether the server-side history is + // empty (fresh conversation) or already live (interop with + // full-resend traffic: the client's view is authoritative and + // identical-or-newer). Only DELTA turns can 409. + return { mode: forceResync ? "resync" : "seed", body: buildBody(args, 0, args.messages, true, includeSystem) }; + } + const hashes = args.messages.map(wireHash); + // Prefix alignment: everything previously acked must still lead the list. + const n = Math.min(state.sent.length, hashes.length); + let aligned = true; + for (let i = 0; i < n; i++) { + if (state.sent[i] !== hashes[i]) { aligned = false; break; } + } + if (!aligned || hashes.length < state.sent.length) { + return { mode: "resync", body: buildBody(args, 0, args.messages, true, systemJson !== state.systemJson) }; + } + if (hashes.length === state.sent.length) { + // Equal message set: normally a retry of the same turn → normal path. + // But a SYSTEM change is still a real outgoing turn — send an empty + // delta so the lane forwards the assembled request with the new + // sticky system (the canonical itself is unchanged). + if (systemJson !== state.systemJson) { + return { mode: "delta", body: buildBody(args, state.sinceSeq, [], false, true) }; + } + return { mode: "skip" }; // retry of the same turn → normal path + } + return { + mode: "delta", + body: buildBody(args, state.sinceSeq, args.messages.slice(state.sent.length), false, systemJson !== state.systemJson), + }; +} + +/** Record a 200 ack: `appliedSeq` is the response's x-bili-applied-seq and + * `messages` the wire set the server just acknowledged. An ack clears the + * rejection circuit — the conversation is healthy again. */ +export function noteIncrementalAck(conversationId: string, appliedSeq: number, messages: IncrementalWireMessage[], system: unknown): void { + rejectionCircuit.delete(conversationId); + convs.set(conversationId, { + sinceSeq: appliedSeq, + sent: messages.map(wireHash), + systemJson: system === undefined ? undefined : JSON.stringify(system), + dead: false, + }); +} + +/** Consecutive 409s (no intervening ack) that trip the sticky kill switch. + * A SINGLE 409 just resets the conversation state — the next turn re-seeds + * with a full resync:true body, which cannot 409 — so transient drift (sidecar + * degraded at boot, one upstream 5xx between apply and forward, a sibling host + * re-seeding) self-heals on the very next turn instead of permanently + * downgrading the conversation (#3369). */ +const REJECTION_CIRCUIT = 3; + +/** Consecutive rejections per conversation, OUTSIDE `convs` — a reset deletes + * the wire state, but the circuit counter must survive it. */ +const rejectionCircuit = new Map(); + +/** Record a lane rejection (409 seq drift). Returns "reset" when the next + * turn re-seeds (transient drift — the common case) and "dead" when the + * sticky kill switch tripped (persistent drift: full resend for the rest of + * the process lifetime, exactly the pre-#3369 behavior). */ +export function noteIncrementalRejection(conversationId: string): "reset" | "dead" { + const strikes = (rejectionCircuit.get(conversationId) ?? 0) + 1; + if (strikes >= REJECTION_CIRCUIT) { + rejectionCircuit.delete(conversationId); + convs.set(conversationId, { sinceSeq: 0, sent: [], systemJson: undefined, dead: true }); + return "dead"; + } + rejectionCircuit.set(conversationId, strikes); + convs.delete(conversationId); // next turn re-seeds: full set, resync:true, sinceSeq 0 + return "reset"; +} + +/** Sticky per-conversation fallback after an unrecoverable lane failure — + * the conversation keeps working via plain full resend. */ +export function markIncrementalDead(conversationId: string): void { + const state = convs.get(conversationId); + if (state) state.dead = true; +} + +/** Drop the cached probe for one proxy base (e.g. after a 503 — the lane's + * rollout flag or sidecar liveness changed). The next turn re-probes. */ +export function clearIncrementalProbe(proxyBase: string): void { + probeCache.delete(proxyBase); +} + +export function __resetIncrementalClientForTests(): void { + convs.clear(); + probeCache.clear(); + rejectionCircuit.clear(); + droppedParamWarned.clear(); + droppedParamWarnPairs = 0; + originalBodies.clear(); +} diff --git a/tests/incremental-lane.test.ts b/tests/incremental-lane.test.ts index cf71b9edf2..d2bb69e626 100644 --- a/tests/incremental-lane.test.ts +++ b/tests/incremental-lane.test.ts @@ -1,510 +1,556 @@ -import assert from "node:assert/strict"; -import http from "node:http"; -import { once } from "node:events"; -import test from "node:test"; -import { mkdtempSync, rmSync } from "node:fs"; -import { tmpdir } from "node:os"; -import { join } from "node:path"; -import { defaultConfig } from "acp-kernel"; -import { startServer } from "../src/server.ts"; -import type { ProxyOptions } from "../src/config.ts"; -import { SessionStore, _setStoreForTest } from "../src/persist.ts"; -import { _resetPluginStateForTest } from "../src/plugin.ts"; -import { _resetSessionsForTest, peekSession } from "../src/session.ts"; -import { _setForTest } from "../src/registry.ts"; -import { resetToolRingForTest } from "../src/tool-ring.ts"; -import { __resetIncrementalLaneForTests } from "../src/incremental-lane.ts"; - -process.env.NODE_ENV = "test"; -const testRoot = mkdtempSync(join(tmpdir(), "bili-incr-")); -test.after(() => rmSync(testRoot, { recursive: true, force: true })); - -interface Turn { - sinceSeq: number; - messages: unknown[]; - resync?: boolean; - system?: string; - request?: Record; -} - -interface Harness { - proxyUrl: string; - path: string; - turn: (t: Turn) => Promise<{ status: number; headers: Headers; body: unknown }>; - probe: () => Promise<{ status: number; body: unknown }>; - markerTurn: (reqPath: string, payload: { conversationId: string; sinceSeq: number; resync?: boolean; system?: unknown; request?: Record; messages: unknown[] }) => Promise<{ status: number; body: unknown; appliedSeq?: number }>; - markerRaw: (reqPath: string, anthropicBody: Record) => Promise<{ status: number; body: unknown }>; - forwarded: Record[]; - dir: string; - close: () => Promise; -} - -async function harness(storageMode: "full" | "legacy", opts: { upstreamDelayMs?: number; upstreamStatus?: number } = {}): Promise { - const dir = mkdtempSync(join(testRoot, "run-")); - for (const key of ["XDG_STATE_HOME", "XDG_DATA_HOME", "XDG_CACHE_HOME", "XDG_CONFIG_HOME"]) process.env[key] = dir; - process.env.BILI_STORAGE_UNIFIED = storageMode === "full" ? "full" : "legacy"; - _resetSessionsForTest(); - _resetPluginStateForTest(); - resetToolRingForTest(); - __resetIncrementalLaneForTests(); - const store = new SessionStore({ dir: dir + "/sessions", enabled: true, debounceMs: 60000 }); - _setStoreForTest(store); - _setForTest({}); - const forwarded: Record[] = []; - const upstream = http.createServer((req, res) => { - const chunks: Buffer[] = []; - req.on("data", (chunk: Buffer) => chunks.push(chunk)); - req.on("end", () => { - forwarded.push(JSON.parse(Buffer.concat(chunks).toString("utf8")) as Record); - const reply = (): void => { - res.writeHead(opts.upstreamStatus ?? 200, { "content-type": "application/json" }); - res.end(JSON.stringify({ id: "msg_test", role: "assistant", content: [{ type: "text", text: "answer" }], usage: { input_tokens: 10000, output_tokens: 10 } })); - }; - if (opts.upstreamDelayMs) setTimeout(reply, opts.upstreamDelayMs); - else reply(); - }); - }); - upstream.listen(0, "127.0.0.1"); - await once(upstream, "listening"); - const addr = upstream.address(); - assert(addr && typeof addr === "object"); - const upstreamUrl = `http://127.0.0.1:${addr.port}`; - const proxy = await startServer({ port: 0, host: "127.0.0.1", upstream: upstreamUrl, routes: { [upstreamUrl]: { models: { "claude-test": { context: 400000 } } } }, modelContextLimit: 400000, kernelConfig: defaultConfig(400000), compress: { injectTool: true, injectNudge: false }, promptCache: { routing: "auto" }, sessionHeader: "x-acp-session", log: false, debug: false, passthrough: false, autoUpdate: false, mitm: { enabled: false, domains: [] } } as ProxyOptions); - if (!(proxy as unknown as { listening?: boolean }).listening) await once(proxy, "listening"); - const proxyUrl = `http://127.0.0.1:${(proxy.address() as { port: number }).port}`; - const path = "/v1/messages"; - const turn = async (t: Turn) => { - const response = await fetch(`${proxyUrl}/__bili/plugin/turn`, { - method: "POST", - headers: { "content-type": "application/json", "x-bili-plugin": "dsh", "x-bili-plugin-conversation": "conv-inc-1" }, - body: JSON.stringify({ conversationId: "conv-inc-1", sinceSeq: t.sinceSeq, ...(t.resync ? { resync: true } : {}), ...(t.system !== undefined ? { system: t.system } : {}), path, request: { model: "claude-test", max_tokens: 1024, ...(t.request ?? {}) }, messages: t.messages }), - }); - const body: unknown = await response.json().catch(() => null); - return { status: response.status, headers: response.headers, body }; - }; - // store handle must be released before the teardown rm -rf: Windows cannot - // unlink an open index.db (EBUSY) — same seam the storage-unified suite uses. - const probe = async () => { - const response = await fetch(`${proxyUrl}/__bili/plugin/turn`, { method: "GET", headers: { "x-bili-plugin": "dsh" } }); - const body: unknown = await response.json().catch(() => null); - return { status: response.status, body }; - }; - // #3248 marker divert: shaped incremental body posted on the ORDINARY model path - const markerTurn = async (reqPath: string, payload: { conversationId: string; sinceSeq: number; resync?: boolean; system?: unknown; request?: Record; messages: unknown[] }) => { - const response = await fetch(`${proxyUrl}${reqPath}`, { - method: "POST", - headers: { "content-type": "application/json", "x-bili-plugin": "dsh", "x-bili-plugin-conversation": payload.conversationId, "x-acp-session": payload.conversationId, "x-bili-plugin-turn": "1" }, - body: JSON.stringify({ conversationId: payload.conversationId, sinceSeq: payload.sinceSeq, ...(payload.resync ? { resync: true } : {}), ...(payload.system !== undefined ? { system: payload.system } : {}), request: { model: "claude-test", max_tokens: 1024, ...(payload.request ?? {}) }, messages: payload.messages }), - }); - const body: unknown = await response.json().catch(() => null); - const applied = response.headers.get("x-bili-applied-seq"); - return { status: response.status, body, appliedSeq: applied !== null && /^\d+$/.test(applied) ? Number(applied) : undefined }; - }; - // marker on an ordinary anthropic body: must ride the normal pipeline unchanged - const markerRaw = async (reqPath: string, anthropicBody: Record) => { - const response = await fetch(`${proxyUrl}${reqPath}`, { method: "POST", headers: { "content-type": "application/json", "x-bili-plugin": "dsh", "x-acp-session": "conv-marker-plain", "x-bili-plugin-turn": "1" }, body: JSON.stringify(anthropicBody) }); - const body: unknown = await response.json().catch(() => null); - return { status: response.status, body }; - }; - return { proxyUrl, path, turn, probe, markerTurn, markerRaw, forwarded, dir, close: async () => { await new Promise((resolve) => proxy.close(() => resolve())); store.closeQuietly(); await new Promise((resolve) => upstream.close(() => resolve())); } }; -} - -const userTurn = (n: number, text = "hello") => ({ id: `u${n}`, role: "user", contentType: "text", content: text.repeat(400) }); -const assistantTurn = (n: number) => ({ id: `a${n}`, role: "assistant", contentType: "text", content: `model answer ${n}` }); - -test("incremental lane: seed → delta → upstream receives the assembled full history; ack advances", async () => { - const h = await harness("full"); - try { - const seed = await h.turn({ sinceSeq: 0, messages: [userTurn(1)] }); - assert.equal(seed.status, 200); - assert.equal(seed.headers.get("x-bili-applied-seq"), "1"); - const upstream1 = h.forwarded.at(-1) as { system?: unknown; messages: { role: string; content: string }[] }; - assert.ok(Array.isArray(upstream1.messages) && upstream1.messages.length === 1); - assert.equal(upstream1.messages[0].role, "user"); - - // Delta: everything after ack — the previous assistant reply + the new user turn. - const delta = await h.turn({ sinceSeq: 1, messages: [assistantTurn(1), userTurn(2, "second question")] }); - assert.equal(delta.status, 200); - assert.equal(delta.headers.get("x-bili-applied-seq"), "3"); - const upstream2 = h.forwarded.at(-1) as { messages: { role: string }[] }; - assert.equal(upstream2.messages.length, 3, "assembled history = seed + delta"); - assert.deepEqual(upstream2.messages.map((m) => m.role), ["user", "assistant", "user"]); - } finally { - await h.close(); - } -}); - -test("incremental lane: a failed turn (non-2xx) carries NO x-bili-applied-seq (#3344)", async () => { - const h = await harness("full", { upstreamStatus: 500 }); - try { - const failed = await h.turn({ sinceSeq: 0, messages: [userTurn(1)] }); - assert.ok(failed.status >= 400, `expected a non-2xx failure from the failing upstream, got ${failed.status}`); - // The ack must not ride a failure: a host trusting it unconditionally - // would skip its resend and lose the user's message (#3344). - assert.equal(failed.headers.get("x-bili-applied-seq"), null); - } finally { - await h.close(); - } -}); - -test("incremental lane: seq mismatch answers 409 with the server's seq; resync recovers", async () => { - const h = await harness("full"); - try { - const seed = await h.turn({ sinceSeq: 0, messages: [userTurn(1)] }); - assert.equal(seed.status, 200); - // Client guesses a stale seq. - const stale = await h.turn({ sinceSeq: 0, messages: [assistantTurn(1), userTurn(2)] }); - assert.equal(stale.status, 409); - assert.equal((stale.body as { error?: string }).error, "seq-mismatch"); - assert.equal((stale.body as { serverSeq?: number }).serverSeq, 1); - // Resync with the full history re-seeds the canonical. - const recovered = await h.turn({ sinceSeq: 0, resync: true, messages: [userTurn(1), assistantTurn(1), userTurn(2)] }); - assert.equal(recovered.status, 200); - assert.equal(recovered.headers.get("x-bili-applied-seq"), "3"); - const upstream = h.forwarded.at(-1) as { messages: { role: string }[] }; - assert.deepEqual(upstream.messages.map((m) => m.role), ["user", "assistant", "user"]); - } finally { - await h.close(); - } -}); - -test("incremental lane: cold rebuild after restart serves the next delta from the unified table", async () => { - const h = await harness("full"); - try { - const seed = await h.turn({ sinceSeq: 0, messages: [userTurn(1)] }); - assert.equal(seed.status, 200); - // Simulate a process restart: session pool + warm caches dropped. The - // canonical prefix must rebuild from the unified messages table. - _resetSessionsForTest(); - __resetIncrementalLaneForTests(); - const delta = await h.turn({ sinceSeq: 1, messages: [assistantTurn(1), userTurn(2, "after restart")] }); - assert.equal(delta.status, 200); - const upstream = h.forwarded.at(-1) as { messages: { role: string }[] }; - assert.deepEqual(upstream.messages.map((m) => m.role), ["user", "assistant", "user"], "table-rebuilt canonical + delta"); - } finally { - await h.close(); - } -}); - -test("incremental lane: divergent resync survives a cold restart without a 409 loop", async () => { - const h = await harness("full"); - try { - const seed = await h.turn({ sinceSeq: 0, messages: [userTurn(1, "old branch one"), assistantTurn(1), userTurn(2, "old branch two")] }); - assert.equal(seed.status, 200); - // The client rewinds to an edited branch (divergent content, ids and all). - const resync = await h.turn({ sinceSeq: 0, resync: true, messages: [userTurn(1, "edited branch")] }); - assert.equal(resync.status, 200); - assert.equal(resync.headers.get("x-bili-applied-seq"), "1"); - // Cold restart: the canonical must rebuild from the CURRENT branch - // only — the rejected branch's rows stay in the table (append-only) - // but the served history slices past them (baseSeq epoch). - _resetSessionsForTest(); - __resetIncrementalLaneForTests(); - const delta = await h.turn({ sinceSeq: 1, messages: [assistantTurn(1), userTurn(2, "new branch tail")] }); - assert.equal(delta.status, 200); - assert.equal(delta.headers.get("x-bili-applied-seq"), "3"); - const upstream = h.forwarded.at(-1) as { messages: { role: string; content: unknown }[] }; - assert.equal(upstream.messages.length, 3, "served history is the edited branch, not the row union"); - const first = JSON.stringify(upstream.messages[0].content); - assert.ok(first.includes("edited branch"), `first message is the resynced one: ${first.slice(0, 80)}`); - assert.ok(!JSON.stringify(upstream.messages).includes("old branch"), "old branch rows do not leak into the canonical"); - } finally { - await h.close(); - } -}); - -test("incremental lane: sessions share the plugin conversation id — incremental and normal traffic interop", async () => { - const h = await harness("full"); - try { - const seed = await h.turn({ sinceSeq: 0, messages: [userTurn(1)] }); - assert.equal(seed.status, 200); - const session = peekSession("conv-inc-1"); - assert.ok(session, "session id = the plugin conversation id (same as normal plugin traffic)"); - assert.equal(session.stats.requests, 1); - } finally { - await h.close(); - } -}); - -test("incremental lane: sticky system — omitted system keeps the last one, a new value replaces it", async () => { - const h = await harness("full"); - // The assembled body rides the normal pipeline, which renders the system - // prompt as anthropic blocks (with its cache marks) — compare by text. - const sysText = (v: unknown): string => typeof v === "string" ? v : Array.isArray(v) ? v.map((b) => (b as { text?: string })?.text ?? "").join("") : String(v); - try { - await h.turn({ sinceSeq: 0, system: "SYSTEM-A", messages: [userTurn(1)] }); - const upstream1 = sysText((h.forwarded.at(-1) as { system?: unknown }).system); - assert.ok(upstream1.startsWith("SYSTEM-A"), `turn 1 carries the client system: ${upstream1.slice(0, 40)}`); - // Omitted → sticky. - await h.turn({ sinceSeq: 1, messages: [assistantTurn(1), userTurn(2)] }); - const upstream2 = sysText((h.forwarded.at(-1) as { system?: unknown }).system); - assert.ok(upstream2.startsWith("SYSTEM-A"), "omitted system keeps the last one"); - // Explicit new value replaces. - await h.turn({ sinceSeq: 3, system: "SYSTEM-B", messages: [assistantTurn(2), userTurn(3)] }); - const upstream3 = sysText((h.forwarded.at(-1) as { system?: unknown }).system); - assert.ok(upstream3.startsWith("SYSTEM-B"), "explicit system replaces"); - assert.ok(!upstream3.includes("SYSTEM-A"), "the old system is fully replaced"); - } finally { - await h.close(); - } -}); - -test("incremental lane: a second concurrent turn on the same conversation answers 409 turn-in-flight", async () => { - // Margins sized for loaded runners: the settle sleep must beat the first - // request's path into inFlight, and the upstream delay must outlive it — - // 40ms/150ms flaked (review of #3253). - const h = await harness("full", { upstreamDelayMs: 800 }); - try { - const first = h.turn({ sinceSeq: 0, messages: [userTurn(1)] }); - await new Promise((r) => setTimeout(r, 120)); // let the first enter forward - const second = await h.turn({ sinceSeq: 1, messages: [assistantTurn(1), userTurn(2)] }); - assert.equal(second.status, 409); - assert.equal((second.body as { error?: string }).error, "turn-in-flight"); - assert.equal((await first).status, 200); - } finally { - await h.close(); - } -}); - -test("incremental lane: request params (tools/max_tokens) ride the assembled body to the upstream", async () => { - const h = await harness("full"); - try { - const tools = [{ name: "get_weather", description: "w", input_schema: { type: "object", properties: {} } }]; - await h.turn({ sinceSeq: 0, request: { max_tokens: 777, tools }, messages: [userTurn(1)] }); - const upstream = h.forwarded.at(-1) as { max_tokens?: number; tools?: unknown; model?: string }; - assert.equal(upstream.max_tokens, 777); - assert.equal(upstream.model, "claude-test"); - assert.ok(Array.isArray(upstream.tools)); - // Disallowed request params never ride. - await h.turn({ sinceSeq: 1, request: { max_tokens: 512, messages: [{ role: "user", content: "smuggled" }] } as never, messages: [assistantTurn(1), userTurn(2)] }); - const upstream2 = h.forwarded.at(-1) as { messages: { role: string }[] }; - assert.equal(upstream2.messages.length, 3, "request.messages is ignored — the lane owns the message list"); - } finally { - await h.close(); - } -}); - -test("incremental lane: a Vertex turn keeps anthropic_version and still drops an unlisted field", async () => { - const h = await harness("full"); - try { - // Anthropic-on-Vertex has no `model` in the body — `anthropic_version` - // carries the wire revision instead (live capture 2026-10-15). The - // allowlist is the only thing that could drop it on the way out. - await h.turn({ sinceSeq: 0, request: { anthropic_version: "vertex-2023-10-16" }, messages: [userTurn(1)] }); - const upstream = h.forwarded.at(-1) as { anthropic_version?: string; smuggled?: unknown }; - assert.equal(upstream.anthropic_version, "vertex-2023-10-16"); - // The allowlist is still a whitelist: an unlisted key must not ride. - await h.turn({ sinceSeq: 1, request: { anthropic_version: "vertex-2023-10-16", smuggled: true } as never, messages: [assistantTurn(1), userTurn(2)] }); - const upstream2 = h.forwarded.at(-1) as { anthropic_version?: string; smuggled?: unknown }; - assert.equal(upstream2.anthropic_version, "vertex-2023-10-16"); - assert.equal(upstream2.smuggled, undefined); - } finally { - await h.close(); - } -}); - -test("incremental lane: legacy-only storage answers 503 with the enablement hint", async () => { - const h = await harness("legacy"); - try { - const seed = await h.turn({ sinceSeq: 0, messages: [userTurn(1)] }); - assert.equal(seed.status, 503); - assert.match(String((seed.body as { error?: string }).error), /storage\.unified/); - } finally { - await h.close(); - } -}); - -// --- marker divert (#3248 phase 2) ------------------------------------------------- -// The host keeps its ordinary provider URL and stamps `x-bili-plugin-turn: 1`; -// the body shape is the final word (non-incremental bodies ride the normal -// pipeline byte-identically). - -test("incremental marker divert: probe reports the rollout state", async () => { - const h = await harness("full"); - try { - const off = await h.probe(); - assert.equal(off.status, 200); - const offBody = off.body as { ok: boolean; enabled: boolean; feature: string; protocol: number }; - assert.equal(offBody.ok, true); - assert.equal(offBody.feature, "pluginIncrementalTurns"); - assert.equal(offBody.protocol, 1); - assert.equal(offBody.enabled, false, "default rollout percent is 0 — probe must report disabled"); - process.env.BILI_ROLLOUT_PLUGIN_INCREMENTAL_TURNS = "1"; - try { - const on = await h.probe(); - assert.equal(on.status, 200); - assert.equal((on.body as { enabled: boolean }).enabled, true, "env force flips the probe"); - } finally { - delete process.env.BILI_ROLLOUT_PLUGIN_INCREMENTAL_TURNS; - } - } finally { - await h.close(); - } -}); - -test("incremental marker divert: shaped body on the ordinary model path is diverted and assembled", async () => { - process.env.BILI_ROLLOUT_PLUGIN_INCREMENTAL_TURNS = "1"; - let h: Awaited> | undefined; - try { - h = await harness("full"); - const seed = await h.markerTurn("/v1/messages", { conversationId: "conv-marker-1", sinceSeq: 0, request: { max_tokens: 512 }, messages: [userTurn(1)] }); - assert.equal(seed.status, 200); - assert.ok(seed.appliedSeq !== undefined && seed.appliedSeq >= 1, "diverted turn carries x-bili-applied-seq"); - const upstream = h.forwarded.at(-1) as { model?: string; max_tokens?: number; messages: { role: string }[] }; - assert.equal(upstream.model, "claude-test", "assembled body is a real anthropic request"); - assert.equal(upstream.max_tokens, 512); - assert.equal(upstream.messages.length, 1); - // delta through the same ordinary path - const delta = await h.markerTurn("/v1/messages", { conversationId: "conv-marker-1", sinceSeq: seed.appliedSeq ?? 1, request: { max_tokens: 512 }, messages: [assistantTurn(1), userTurn(2)] }); - assert.equal(delta.status, 200); - const upstream2 = h.forwarded.at(-1) as { messages: { role: string }[] }; - assert.equal(upstream2.messages.length, 3, "delta was assembled onto the canonical prefix"); - assert.deepEqual(upstream2.messages.map((m: { role: string }) => m.role), ["user", "assistant", "user"]); - } finally { - delete process.env.BILI_ROLLOUT_PLUGIN_INCREMENTAL_TURNS; - await h?.close(); - } -}); - -test("incremental marker divert: marker on a non-incremental body rides the normal pipeline unchanged", async () => { - process.env.BILI_ROLLOUT_PLUGIN_INCREMENTAL_TURNS = "1"; - let h: Awaited> | undefined; - try { - h = await harness("full"); - // an ordinary anthropic body WITH the marker: shape is the final word. - // The normal pipeline re-serializes anthropic bodies (content may come - // back as blocks), so assert semantics, not byte identity: model rides, - // one message, text preserved, and no incremental artifacts leak - // upstream (conversationId/sinceSeq must be absent). - const res = await h.markerRaw("/v1/messages", { model: "claude-test", max_tokens: 512, messages: [{ role: "user", content: "plain turn with marker" }] }); - assert.equal(res.status, 200); - const upstream = h.forwarded.at(-1) as { model?: string; conversationId?: string; sinceSeq?: number; messages: { role: string; content: unknown }[] }; - assert.equal(upstream.model, "claude-test"); - assert.equal(upstream.messages.length, 1); - assert.equal("conversationId" in upstream, false, "not diverted"); - assert.equal("sinceSeq" in upstream, false, "not diverted"); - const c = upstream.messages[0].content; - const text = typeof c === "string" ? c : Array.isArray(c) ? c.map((p) => (p && typeof p === "object" && "text" in p ? String((p as { text: unknown }).text) : "")).join("") : String(c); - // The ordinary pipeline stamps the hidden ref on the first user - // turn — exactly what a marker-less request would carry too. - assert.ok(text.endsWith("plain turn with marker"), `text rides the normal pipeline (got: ${JSON.stringify(text.slice(0, 60))}…)`); - } finally { - delete process.env.BILI_ROLLOUT_PLUGIN_INCREMENTAL_TURNS; - await h?.close(); - } -}); - -test("incremental marker divert: rollout off answers 503 on shaped turns only", async () => { - const h = await harness("full"); - try { - const shaped = await h.markerTurn("/v1/messages", { conversationId: "conv-marker-off", sinceSeq: 0, request: { max_tokens: 512 }, messages: [userTurn(1)] }); - assert.equal(shaped.status, 503); - assert.match(String((shaped.body as { error?: string }).error), /pluginIncrementalTurns/); - // a plain body with the marker still rides the normal pipeline - const plain = await h.markerRaw("/v1/messages", { model: "claude-test", max_tokens: 512, messages: [{ role: "user", content: "plain while off" }] }); - assert.equal(plain.status, 200); - } finally { - await h.close(); - } -}); - -test("incremental marker divert: system-only change rides an empty delta — canonical unchanged, sticky system updates", async () => { - process.env.BILI_ROLLOUT_PLUGIN_INCREMENTAL_TURNS = "1"; - let h: Awaited> | undefined; - const sysText = (v: unknown): string => typeof v === "string" ? v : Array.isArray(v) ? v.map((b) => (b as { text?: string })?.text ?? "").join("") : String(v); - try { - h = await harness("full"); - const seed = await h.markerTurn("/v1/messages", { conversationId: "conv-marker-sys", sinceSeq: 0, system: "SYSTEM-A", request: { max_tokens: 512 }, messages: [userTurn(1)] }); - assert.equal(seed.status, 200); - assert.equal(seed.appliedSeq, 1); - // Same message set, different system → empty delta: the canonical must - // not extend, and the assembled request carries the new sticky system. - const sysOnly = await h.markerTurn("/v1/messages", { conversationId: "conv-marker-sys", sinceSeq: seed.appliedSeq ?? 1, system: "SYSTEM-B", request: { max_tokens: 512 }, messages: [] }); - assert.equal(sysOnly.status, 200); - assert.equal(sysOnly.appliedSeq, seed.appliedSeq, "a pure system update does not extend the canonical"); - const upstream = h.forwarded.at(-1) as { system?: unknown; messages: { role: string }[] }; - assert.equal(upstream.messages.length, 1, "history unchanged by the system-only turn"); - const sys = sysText(upstream.system); - assert.ok(sys.startsWith("SYSTEM-B"), `assembled request carries the new system: ${sys.slice(0, 40)}`); - assert.ok(!sys.includes("SYSTEM-A"), "the old system is fully replaced"); - // An empty delta WITHOUT a system update stays a client error. - const bareEmpty = await h.markerTurn("/v1/messages", { conversationId: "conv-marker-sys", sinceSeq: seed.appliedSeq ?? 1, request: { max_tokens: 512 }, messages: [] }); - assert.equal(bareEmpty.status, 400); - } finally { - delete process.env.BILI_ROLLOUT_PLUGIN_INCREMENTAL_TURNS; - await h?.close(); - } -}); - -// --- client helper state machine (src/agent/incremental-client.ts) ------------------ - -test("incremental client helper: seed implies resync, delta slices, edits resync, sticky system", async () => { - const { __resetIncrementalClientForTests, planIncrementalTurn, noteIncrementalAck, markIncrementalDead } = await import("../src/agent/incremental-client.js"); - __resetIncrementalClientForTests(); - const conv = "conv-helper-1"; - const msgs1 = [{ role: "user", content: "hello there" }]; - const seed = planIncrementalTurn({ conversationId: conv, messages: msgs1, system: "be terse" }); - assert.equal(seed.mode, "seed"); - const seedBody = JSON.parse(seed.body) as { sinceSeq: number; resync: boolean; messages: unknown[]; system?: string }; - assert.equal(seedBody.sinceSeq, 0); - assert.equal(seedBody.resync, true, "seed never 409s — replacement semantics"); - assert.equal(seedBody.system, "be terse"); - - // ack seq 1 → next turn sends only the new tail - noteIncrementalAck(conv, 1, msgs1, "be terse"); - const msgs2 = [...msgs1, { role: "assistant", content: "hi" }, { role: "user", content: "and now?" }]; - const delta = planIncrementalTurn({ conversationId: conv, messages: msgs2, system: "be terse" }); - assert.equal(delta.mode, "delta"); - const deltaBody = JSON.parse(delta.body) as { sinceSeq: number; messages: unknown[]; system?: string }; - assert.equal(deltaBody.sinceSeq, 1); - assert.equal(deltaBody.messages.length, 2, "only the messages added since the ack"); - assert.equal(deltaBody.system, undefined, "sticky system omitted when unchanged"); - - // edited prefix → resync with the full history - const edited = [{ role: "user", content: "hello there (edited)" }, ...msgs2.slice(1)]; - const rs = planIncrementalTurn({ conversationId: conv, messages: edited, system: "be terse" }); - assert.equal(rs.mode, "resync"); - const rsBody = JSON.parse(rs.body) as { sinceSeq: number; resync: boolean; messages: unknown[] }; - assert.equal(rsBody.resync, true); - assert.equal(rsBody.messages.length, edited.length); - - // system change with an equal message set rides an EMPTY delta — the - // canonical is unchanged, the sticky system updates server-side - noteIncrementalAck(conv, edited.length, edited, "be terse"); - const sysChange = planIncrementalTurn({ conversationId: conv, messages: edited, system: "be verbose" }); - assert.equal(sysChange.mode, "delta"); - const sysBody = JSON.parse(sysChange.body) as { sinceSeq: number; messages: unknown[]; system?: string }; - assert.equal(sysBody.messages.length, 0, "pure sticky-system update — empty delta"); - assert.equal(sysBody.system, "be verbose"); - - // dead conversation falls back to skip until re-probed/reset - markIncrementalDead(conv); - const dead = planIncrementalTurn({ conversationId: conv, messages: edited, system: "be verbose" }); - assert.equal(dead.mode, "skip", "dead conversation rides full resend (host's own path)"); - __resetIncrementalClientForTests(); -}); - -test("wireMessagesLookAnthropic: anthropic shapes pass, OpenAI-tool shapes fall back to full resend", async () => { - const { wireMessagesLookAnthropic } = await import("../src/agent/incremental-client.js"); - assert.equal(wireMessagesLookAnthropic([{ role: "user", content: "hi" }]), true, "string content"); - assert.equal(wireMessagesLookAnthropic([{ role: "user", content: [{ type: "text", text: "hi" }] }]), true, "block content"); - assert.equal(wireMessagesLookAnthropic([{ role: "assistant", content: [{ type: "tool_use", id: "t", name: "x", input: {} }] }, { role: "user", content: [{ type: "tool_result", tool_use_id: "t", content: "ok" }] }]), true, "anthropic tool blocks are native wire shape"); - assert.equal(wireMessagesLookAnthropic([]), false, "empty list is not a conversation"); - assert.equal(wireMessagesLookAnthropic("nope"), false); - assert.equal(wireMessagesLookAnthropic([{ role: "user", content: "hi" }, { role: "tool", content: "out" }]), false, "OpenAI tool role is not convertible"); - assert.equal(wireMessagesLookAnthropic([{ role: "assistant", content: "x", tool_calls: [{ id: "c", type: "function", function: { name: "f" } }] }]), false, "tool_calls cannot ride anthropicToCore"); - assert.equal(wireMessagesLookAnthropic([{ role: "user", content: "hi", tool_call_id: "c" }]), false); - assert.equal(wireMessagesLookAnthropic([{ role: "user", content: 42 }]), false, "content must be string or array"); -}); - -test("incrementalTurnBodyShape accepts the incremental payload and rejects anthropic/garbage", async () => { - const { incrementalTurnBodyShape } = await import("../src/incremental-lane.js"); - const inc = Buffer.from(JSON.stringify({ conversationId: "c1", sinceSeq: 0, request: {}, messages: [] })); - assert.equal(incrementalTurnBodyShape(inc), true); - const anthropic = Buffer.from(JSON.stringify({ model: "claude-test", max_tokens: 64, messages: [] })); - assert.equal(incrementalTurnBodyShape(anthropic), false, "top-level model = ordinary request"); - const garbage = Buffer.from("not json at all"); - assert.equal(incrementalTurnBodyShape(garbage), false); - const badSeq = Buffer.from(JSON.stringify({ conversationId: "c1", sinceSeq: "3", messages: [] })); - assert.equal(incrementalTurnBodyShape(badSeq), false, "sinceSeq must be an integer"); -}); +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import test from "node:test"; +import { mkdtempSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { defaultConfig } from "acp-kernel"; +import { startServer } from "../src/server.ts"; +import type { ProxyOptions } from "../src/config.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { _resetPluginStateForTest } from "../src/plugin.ts"; +import { _resetSessionsForTest, peekSession } from "../src/session.ts"; +import { _setForTest } from "../src/registry.ts"; +import { resetToolRingForTest } from "../src/tool-ring.ts"; +import { __resetIncrementalLaneForTests } from "../src/incremental-lane.ts"; + +process.env.NODE_ENV = "test"; +const testRoot = mkdtempSync(join(tmpdir(), "bili-incr-")); +test.after(() => rmSync(testRoot, { recursive: true, force: true })); + +interface Turn { + sinceSeq: number; + messages: unknown[]; + resync?: boolean; + system?: string; + request?: Record; +} + +interface Harness { + proxyUrl: string; + path: string; + turn: (t: Turn) => Promise<{ status: number; headers: Headers; body: unknown }>; + probe: () => Promise<{ status: number; body: unknown }>; + markerTurn: (reqPath: string, payload: { conversationId: string; sinceSeq: number; resync?: boolean; system?: unknown; request?: Record; messages: unknown[] }) => Promise<{ status: number; body: unknown; appliedSeq?: number }>; + markerRaw: (reqPath: string, anthropicBody: Record) => Promise<{ status: number; body: unknown }>; + forwarded: Record[]; + dir: string; + close: () => Promise; +} + +async function harness(storageMode: "full" | "legacy", opts: { upstreamDelayMs?: number; upstreamStatus?: number } = {}): Promise { + const dir = mkdtempSync(join(testRoot, "run-")); + for (const key of ["XDG_STATE_HOME", "XDG_DATA_HOME", "XDG_CACHE_HOME", "XDG_CONFIG_HOME"]) process.env[key] = dir; + process.env.BILI_STORAGE_UNIFIED = storageMode === "full" ? "full" : "legacy"; + _resetSessionsForTest(); + _resetPluginStateForTest(); + resetToolRingForTest(); + __resetIncrementalLaneForTests(); + const store = new SessionStore({ dir: dir + "/sessions", enabled: true, debounceMs: 60000 }); + _setStoreForTest(store); + _setForTest({}); + const forwarded: Record[] = []; + const upstream = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (chunk: Buffer) => chunks.push(chunk)); + req.on("end", () => { + forwarded.push(JSON.parse(Buffer.concat(chunks).toString("utf8")) as Record); + const reply = (): void => { + res.writeHead(opts.upstreamStatus ?? 200, { "content-type": "application/json" }); + res.end(JSON.stringify({ id: "msg_test", role: "assistant", content: [{ type: "text", text: "answer" }], usage: { input_tokens: 10000, output_tokens: 10 } })); + }; + if (opts.upstreamDelayMs) setTimeout(reply, opts.upstreamDelayMs); + else reply(); + }); + }); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const addr = upstream.address(); + assert(addr && typeof addr === "object"); + const upstreamUrl = `http://127.0.0.1:${addr.port}`; + const proxy = await startServer({ port: 0, host: "127.0.0.1", upstream: upstreamUrl, routes: { [upstreamUrl]: { models: { "claude-test": { context: 400000 } } } }, modelContextLimit: 400000, kernelConfig: defaultConfig(400000), compress: { injectTool: true, injectNudge: false }, promptCache: { routing: "auto" }, sessionHeader: "x-acp-session", log: false, debug: false, passthrough: false, autoUpdate: false, mitm: { enabled: false, domains: [] } } as ProxyOptions); + if (!(proxy as unknown as { listening?: boolean }).listening) await once(proxy, "listening"); + const proxyUrl = `http://127.0.0.1:${(proxy.address() as { port: number }).port}`; + const path = "/v1/messages"; + const turn = async (t: Turn) => { + const response = await fetch(`${proxyUrl}/__bili/plugin/turn`, { + method: "POST", + headers: { "content-type": "application/json", "x-bili-plugin": "dsh", "x-bili-plugin-conversation": "conv-inc-1" }, + body: JSON.stringify({ conversationId: "conv-inc-1", sinceSeq: t.sinceSeq, ...(t.resync ? { resync: true } : {}), ...(t.system !== undefined ? { system: t.system } : {}), path, request: { model: "claude-test", max_tokens: 1024, ...(t.request ?? {}) }, messages: t.messages }), + }); + const body: unknown = await response.json().catch(() => null); + return { status: response.status, headers: response.headers, body }; + }; + // store handle must be released before the teardown rm -rf: Windows cannot + // unlink an open index.db (EBUSY) — same seam the storage-unified suite uses. + const probe = async () => { + const response = await fetch(`${proxyUrl}/__bili/plugin/turn`, { method: "GET", headers: { "x-bili-plugin": "dsh" } }); + const body: unknown = await response.json().catch(() => null); + return { status: response.status, body }; + }; + // #3248 marker divert: shaped incremental body posted on the ORDINARY model path + const markerTurn = async (reqPath: string, payload: { conversationId: string; sinceSeq: number; resync?: boolean; system?: unknown; request?: Record; messages: unknown[] }) => { + const response = await fetch(`${proxyUrl}${reqPath}`, { + method: "POST", + headers: { "content-type": "application/json", "x-bili-plugin": "dsh", "x-bili-plugin-conversation": payload.conversationId, "x-acp-session": payload.conversationId, "x-bili-plugin-turn": "1" }, + body: JSON.stringify({ conversationId: payload.conversationId, sinceSeq: payload.sinceSeq, ...(payload.resync ? { resync: true } : {}), ...(payload.system !== undefined ? { system: payload.system } : {}), request: { model: "claude-test", max_tokens: 1024, ...(payload.request ?? {}) }, messages: payload.messages }), + }); + const body: unknown = await response.json().catch(() => null); + const applied = response.headers.get("x-bili-applied-seq"); + return { status: response.status, body, appliedSeq: applied !== null && /^\d+$/.test(applied) ? Number(applied) : undefined }; + }; + // marker on an ordinary anthropic body: must ride the normal pipeline unchanged + const markerRaw = async (reqPath: string, anthropicBody: Record) => { + const response = await fetch(`${proxyUrl}${reqPath}`, { method: "POST", headers: { "content-type": "application/json", "x-bili-plugin": "dsh", "x-acp-session": "conv-marker-plain", "x-bili-plugin-turn": "1" }, body: JSON.stringify(anthropicBody) }); + const body: unknown = await response.json().catch(() => null); + return { status: response.status, body }; + }; + return { proxyUrl, path, turn, probe, markerTurn, markerRaw, forwarded, dir, close: async () => { await new Promise((resolve) => proxy.close(() => resolve())); store.closeQuietly(); await new Promise((resolve) => upstream.close(() => resolve())); } }; +} + +const userTurn = (n: number, text = "hello") => ({ id: `u${n}`, role: "user", contentType: "text", content: text.repeat(400) }); +const assistantTurn = (n: number) => ({ id: `a${n}`, role: "assistant", contentType: "text", content: `model answer ${n}` }); + +test("incremental lane: seed → delta → upstream receives the assembled full history; ack advances", async () => { + const h = await harness("full"); + try { + const seed = await h.turn({ sinceSeq: 0, messages: [userTurn(1)] }); + assert.equal(seed.status, 200); + assert.equal(seed.headers.get("x-bili-applied-seq"), "1"); + const upstream1 = h.forwarded.at(-1) as { system?: unknown; messages: { role: string; content: string }[] }; + assert.ok(Array.isArray(upstream1.messages) && upstream1.messages.length === 1); + assert.equal(upstream1.messages[0].role, "user"); + + // Delta: everything after ack — the previous assistant reply + the new user turn. + const delta = await h.turn({ sinceSeq: 1, messages: [assistantTurn(1), userTurn(2, "second question")] }); + assert.equal(delta.status, 200); + assert.equal(delta.headers.get("x-bili-applied-seq"), "3"); + const upstream2 = h.forwarded.at(-1) as { messages: { role: string }[] }; + assert.equal(upstream2.messages.length, 3, "assembled history = seed + delta"); + assert.deepEqual(upstream2.messages.map((m) => m.role), ["user", "assistant", "user"]); + } finally { + await h.close(); + } +}); + +test("incremental lane: a failed turn (non-2xx) carries NO x-bili-applied-seq (#3344)", async () => { + const h = await harness("full", { upstreamStatus: 500 }); + try { + const failed = await h.turn({ sinceSeq: 0, messages: [userTurn(1)] }); + assert.ok(failed.status >= 400, `expected a non-2xx failure from the failing upstream, got ${failed.status}`); + // The ack must not ride a failure: a host trusting it unconditionally + // would skip its resend and lose the user's message (#3344). + assert.equal(failed.headers.get("x-bili-applied-seq"), null); + } finally { + await h.close(); + } +}); + +test("incremental lane: seq mismatch answers 409 with the server's seq; resync recovers", async () => { + const h = await harness("full"); + try { + const seed = await h.turn({ sinceSeq: 0, messages: [userTurn(1)] }); + assert.equal(seed.status, 200); + // Client guesses a stale seq. + const stale = await h.turn({ sinceSeq: 0, messages: [assistantTurn(1), userTurn(2)] }); + assert.equal(stale.status, 409); + assert.equal((stale.body as { error?: string }).error, "seq-mismatch"); + assert.equal((stale.body as { serverSeq?: number }).serverSeq, 1); + // Resync with the full history re-seeds the canonical. + const recovered = await h.turn({ sinceSeq: 0, resync: true, messages: [userTurn(1), assistantTurn(1), userTurn(2)] }); + assert.equal(recovered.status, 200); + assert.equal(recovered.headers.get("x-bili-applied-seq"), "3"); + const upstream = h.forwarded.at(-1) as { messages: { role: string }[] }; + assert.deepEqual(upstream.messages.map((m) => m.role), ["user", "assistant", "user"]); + } finally { + await h.close(); + } +}); + +test("incremental lane: cold rebuild after restart serves the next delta from the unified table", async () => { + const h = await harness("full"); + try { + const seed = await h.turn({ sinceSeq: 0, messages: [userTurn(1)] }); + assert.equal(seed.status, 200); + // Simulate a process restart: session pool + warm caches dropped. The + // canonical prefix must rebuild from the unified messages table. + _resetSessionsForTest(); + __resetIncrementalLaneForTests(); + const delta = await h.turn({ sinceSeq: 1, messages: [assistantTurn(1), userTurn(2, "after restart")] }); + assert.equal(delta.status, 200); + const upstream = h.forwarded.at(-1) as { messages: { role: string }[] }; + assert.deepEqual(upstream.messages.map((m) => m.role), ["user", "assistant", "user"], "table-rebuilt canonical + delta"); + } finally { + await h.close(); + } +}); + +test("incremental lane: divergent resync survives a cold restart without a 409 loop", async () => { + const h = await harness("full"); + try { + const seed = await h.turn({ sinceSeq: 0, messages: [userTurn(1, "old branch one"), assistantTurn(1), userTurn(2, "old branch two")] }); + assert.equal(seed.status, 200); + // The client rewinds to an edited branch (divergent content, ids and all). + const resync = await h.turn({ sinceSeq: 0, resync: true, messages: [userTurn(1, "edited branch")] }); + assert.equal(resync.status, 200); + assert.equal(resync.headers.get("x-bili-applied-seq"), "1"); + // Cold restart: the canonical must rebuild from the CURRENT branch + // only — the rejected branch's rows stay in the table (append-only) + // but the served history slices past them (baseSeq epoch). + _resetSessionsForTest(); + __resetIncrementalLaneForTests(); + const delta = await h.turn({ sinceSeq: 1, messages: [assistantTurn(1), userTurn(2, "new branch tail")] }); + assert.equal(delta.status, 200); + assert.equal(delta.headers.get("x-bili-applied-seq"), "3"); + const upstream = h.forwarded.at(-1) as { messages: { role: string; content: unknown }[] }; + assert.equal(upstream.messages.length, 3, "served history is the edited branch, not the row union"); + const first = JSON.stringify(upstream.messages[0].content); + assert.ok(first.includes("edited branch"), `first message is the resynced one: ${first.slice(0, 80)}`); + assert.ok(!JSON.stringify(upstream.messages).includes("old branch"), "old branch rows do not leak into the canonical"); + } finally { + await h.close(); + } +}); + +test("incremental lane: sessions share the plugin conversation id — incremental and normal traffic interop", async () => { + const h = await harness("full"); + try { + const seed = await h.turn({ sinceSeq: 0, messages: [userTurn(1)] }); + assert.equal(seed.status, 200); + const session = peekSession("conv-inc-1"); + assert.ok(session, "session id = the plugin conversation id (same as normal plugin traffic)"); + assert.equal(session.stats.requests, 1); + } finally { + await h.close(); + } +}); + +test("incremental lane: sticky system — omitted system keeps the last one, a new value replaces it", async () => { + const h = await harness("full"); + // The assembled body rides the normal pipeline, which renders the system + // prompt as anthropic blocks (with its cache marks) — compare by text. + const sysText = (v: unknown): string => typeof v === "string" ? v : Array.isArray(v) ? v.map((b) => (b as { text?: string })?.text ?? "").join("") : String(v); + try { + await h.turn({ sinceSeq: 0, system: "SYSTEM-A", messages: [userTurn(1)] }); + const upstream1 = sysText((h.forwarded.at(-1) as { system?: unknown }).system); + assert.ok(upstream1.startsWith("SYSTEM-A"), `turn 1 carries the client system: ${upstream1.slice(0, 40)}`); + // Omitted → sticky. + await h.turn({ sinceSeq: 1, messages: [assistantTurn(1), userTurn(2)] }); + const upstream2 = sysText((h.forwarded.at(-1) as { system?: unknown }).system); + assert.ok(upstream2.startsWith("SYSTEM-A"), "omitted system keeps the last one"); + // Explicit new value replaces. + await h.turn({ sinceSeq: 3, system: "SYSTEM-B", messages: [assistantTurn(2), userTurn(3)] }); + const upstream3 = sysText((h.forwarded.at(-1) as { system?: unknown }).system); + assert.ok(upstream3.startsWith("SYSTEM-B"), "explicit system replaces"); + assert.ok(!upstream3.includes("SYSTEM-A"), "the old system is fully replaced"); + } finally { + await h.close(); + } +}); + +test("incremental lane: a second concurrent turn on the same conversation answers 409 turn-in-flight", async () => { + // Margins sized for loaded runners: the settle sleep must beat the first + // request's path into inFlight, and the upstream delay must outlive it — + // 40ms/150ms flaked (review of #3253). + const h = await harness("full", { upstreamDelayMs: 800 }); + try { + const first = h.turn({ sinceSeq: 0, messages: [userTurn(1)] }); + await new Promise((r) => setTimeout(r, 120)); // let the first enter forward + const second = await h.turn({ sinceSeq: 1, messages: [assistantTurn(1), userTurn(2)] }); + assert.equal(second.status, 409); + assert.equal((second.body as { error?: string }).error, "turn-in-flight"); + assert.equal((await first).status, 200); + } finally { + await h.close(); + } +}); + +test("incremental lane: request params (tools/max_tokens) ride the assembled body to the upstream", async () => { + const h = await harness("full"); + try { + const tools = [{ name: "get_weather", description: "w", input_schema: { type: "object", properties: {} } }]; + await h.turn({ sinceSeq: 0, request: { max_tokens: 777, tools }, messages: [userTurn(1)] }); + const upstream = h.forwarded.at(-1) as { max_tokens?: number; tools?: unknown; model?: string }; + assert.equal(upstream.max_tokens, 777); + assert.equal(upstream.model, "claude-test"); + assert.ok(Array.isArray(upstream.tools)); + // Disallowed request params never ride. + await h.turn({ sinceSeq: 1, request: { max_tokens: 512, messages: [{ role: "user", content: "smuggled" }] } as never, messages: [assistantTurn(1), userTurn(2)] }); + const upstream2 = h.forwarded.at(-1) as { messages: { role: string }[] }; + assert.equal(upstream2.messages.length, 3, "request.messages is ignored — the lane owns the message list"); + } finally { + await h.close(); + } +}); + +test("incremental lane: a Vertex turn keeps anthropic_version and still drops an unlisted field", async () => { + const h = await harness("full"); + try { + // Anthropic-on-Vertex has no `model` in the body — `anthropic_version` + // carries the wire revision instead (live capture 2026-10-15). The + // allowlist is the only thing that could drop it on the way out. + await h.turn({ sinceSeq: 0, request: { anthropic_version: "vertex-2023-10-16" }, messages: [userTurn(1)] }); + const upstream = h.forwarded.at(-1) as { anthropic_version?: string; smuggled?: unknown }; + assert.equal(upstream.anthropic_version, "vertex-2023-10-16"); + // The allowlist is still a whitelist: an unlisted key must not ride. + await h.turn({ sinceSeq: 1, request: { anthropic_version: "vertex-2023-10-16", smuggled: true } as never, messages: [assistantTurn(1), userTurn(2)] }); + const upstream2 = h.forwarded.at(-1) as { anthropic_version?: string; smuggled?: unknown }; + assert.equal(upstream2.anthropic_version, "vertex-2023-10-16"); + assert.equal(upstream2.smuggled, undefined); + } finally { + await h.close(); + } +}); + +test("incremental lane: legacy-only storage answers 503 with the enablement hint", async () => { + const h = await harness("legacy"); + try { + const seed = await h.turn({ sinceSeq: 0, messages: [userTurn(1)] }); + assert.equal(seed.status, 503); + assert.match(String((seed.body as { error?: string }).error), /storage\.unified/); + } finally { + await h.close(); + } +}); + +// --- marker divert (#3248 phase 2) ------------------------------------------------- +// The host keeps its ordinary provider URL and stamps `x-bili-plugin-turn: 1`; +// the body shape is the final word (non-incremental bodies ride the normal +// pipeline byte-identically). + +test("incremental marker divert: probe reports the rollout state", async () => { + const h = await harness("full"); + try { + const off = await h.probe(); + assert.equal(off.status, 200); + const offBody = off.body as { ok: boolean; enabled: boolean; feature: string; protocol: number }; + assert.equal(offBody.ok, true); + assert.equal(offBody.feature, "pluginIncrementalTurns"); + assert.equal(offBody.protocol, 1); + assert.equal(offBody.enabled, false, "default rollout percent is 0 — probe must report disabled"); + process.env.BILI_ROLLOUT_PLUGIN_INCREMENTAL_TURNS = "1"; + try { + const on = await h.probe(); + assert.equal(on.status, 200); + assert.equal((on.body as { enabled: boolean }).enabled, true, "env force flips the probe"); + } finally { + delete process.env.BILI_ROLLOUT_PLUGIN_INCREMENTAL_TURNS; + } + } finally { + await h.close(); + } +}); + +test("incremental marker divert: shaped body on the ordinary model path is diverted and assembled", async () => { + process.env.BILI_ROLLOUT_PLUGIN_INCREMENTAL_TURNS = "1"; + let h: Awaited> | undefined; + try { + h = await harness("full"); + const seed = await h.markerTurn("/v1/messages", { conversationId: "conv-marker-1", sinceSeq: 0, request: { max_tokens: 512 }, messages: [userTurn(1)] }); + assert.equal(seed.status, 200); + assert.ok(seed.appliedSeq !== undefined && seed.appliedSeq >= 1, "diverted turn carries x-bili-applied-seq"); + const upstream = h.forwarded.at(-1) as { model?: string; max_tokens?: number; messages: { role: string }[] }; + assert.equal(upstream.model, "claude-test", "assembled body is a real anthropic request"); + assert.equal(upstream.max_tokens, 512); + assert.equal(upstream.messages.length, 1); + // delta through the same ordinary path + const delta = await h.markerTurn("/v1/messages", { conversationId: "conv-marker-1", sinceSeq: seed.appliedSeq ?? 1, request: { max_tokens: 512 }, messages: [assistantTurn(1), userTurn(2)] }); + assert.equal(delta.status, 200); + const upstream2 = h.forwarded.at(-1) as { messages: { role: string }[] }; + assert.equal(upstream2.messages.length, 3, "delta was assembled onto the canonical prefix"); + assert.deepEqual(upstream2.messages.map((m: { role: string }) => m.role), ["user", "assistant", "user"]); + } finally { + delete process.env.BILI_ROLLOUT_PLUGIN_INCREMENTAL_TURNS; + await h?.close(); + } +}); + +test("incremental marker divert: marker on a non-incremental body rides the normal pipeline unchanged", async () => { + process.env.BILI_ROLLOUT_PLUGIN_INCREMENTAL_TURNS = "1"; + let h: Awaited> | undefined; + try { + h = await harness("full"); + // an ordinary anthropic body WITH the marker: shape is the final word. + // The normal pipeline re-serializes anthropic bodies (content may come + // back as blocks), so assert semantics, not byte identity: model rides, + // one message, text preserved, and no incremental artifacts leak + // upstream (conversationId/sinceSeq must be absent). + const res = await h.markerRaw("/v1/messages", { model: "claude-test", max_tokens: 512, messages: [{ role: "user", content: "plain turn with marker" }] }); + assert.equal(res.status, 200); + const upstream = h.forwarded.at(-1) as { model?: string; conversationId?: string; sinceSeq?: number; messages: { role: string; content: unknown }[] }; + assert.equal(upstream.model, "claude-test"); + assert.equal(upstream.messages.length, 1); + assert.equal("conversationId" in upstream, false, "not diverted"); + assert.equal("sinceSeq" in upstream, false, "not diverted"); + const c = upstream.messages[0].content; + const text = typeof c === "string" ? c : Array.isArray(c) ? c.map((p) => (p && typeof p === "object" && "text" in p ? String((p as { text: unknown }).text) : "")).join("") : String(c); + // The ordinary pipeline stamps the hidden ref on the first user + // turn — exactly what a marker-less request would carry too. + assert.ok(text.endsWith("plain turn with marker"), `text rides the normal pipeline (got: ${JSON.stringify(text.slice(0, 60))}…)`); + } finally { + delete process.env.BILI_ROLLOUT_PLUGIN_INCREMENTAL_TURNS; + await h?.close(); + } +}); + +test("incremental marker divert: rollout off answers 503 on shaped turns only", async () => { + const h = await harness("full"); + try { + const shaped = await h.markerTurn("/v1/messages", { conversationId: "conv-marker-off", sinceSeq: 0, request: { max_tokens: 512 }, messages: [userTurn(1)] }); + assert.equal(shaped.status, 503); + assert.match(String((shaped.body as { error?: string }).error), /pluginIncrementalTurns/); + // a plain body with the marker still rides the normal pipeline + const plain = await h.markerRaw("/v1/messages", { model: "claude-test", max_tokens: 512, messages: [{ role: "user", content: "plain while off" }] }); + assert.equal(plain.status, 200); + } finally { + await h.close(); + } +}); + +test("incremental marker divert: system-only change rides an empty delta — canonical unchanged, sticky system updates", async () => { + process.env.BILI_ROLLOUT_PLUGIN_INCREMENTAL_TURNS = "1"; + let h: Awaited> | undefined; + const sysText = (v: unknown): string => typeof v === "string" ? v : Array.isArray(v) ? v.map((b) => (b as { text?: string })?.text ?? "").join("") : String(v); + try { + h = await harness("full"); + const seed = await h.markerTurn("/v1/messages", { conversationId: "conv-marker-sys", sinceSeq: 0, system: "SYSTEM-A", request: { max_tokens: 512 }, messages: [userTurn(1)] }); + assert.equal(seed.status, 200); + assert.equal(seed.appliedSeq, 1); + // Same message set, different system → empty delta: the canonical must + // not extend, and the assembled request carries the new sticky system. + const sysOnly = await h.markerTurn("/v1/messages", { conversationId: "conv-marker-sys", sinceSeq: seed.appliedSeq ?? 1, system: "SYSTEM-B", request: { max_tokens: 512 }, messages: [] }); + assert.equal(sysOnly.status, 200); + assert.equal(sysOnly.appliedSeq, seed.appliedSeq, "a pure system update does not extend the canonical"); + const upstream = h.forwarded.at(-1) as { system?: unknown; messages: { role: string }[] }; + assert.equal(upstream.messages.length, 1, "history unchanged by the system-only turn"); + const sys = sysText(upstream.system); + assert.ok(sys.startsWith("SYSTEM-B"), `assembled request carries the new system: ${sys.slice(0, 40)}`); + assert.ok(!sys.includes("SYSTEM-A"), "the old system is fully replaced"); + // An empty delta WITHOUT a system update stays a client error. + const bareEmpty = await h.markerTurn("/v1/messages", { conversationId: "conv-marker-sys", sinceSeq: seed.appliedSeq ?? 1, request: { max_tokens: 512 }, messages: [] }); + assert.equal(bareEmpty.status, 400); + } finally { + delete process.env.BILI_ROLLOUT_PLUGIN_INCREMENTAL_TURNS; + await h?.close(); + } +}); + +for (const scenario of ["append", "system-only", "edit", "forced resync", "fresh client"] as const) { + test(`incremental client: removed system clears the upstream on ${scenario}`, async () => { + const { __resetIncrementalClientForTests, planIncrementalTurn, noteIncrementalAck } = await import("../src/agent/incremental-client.js"); + const priorRollout = process.env.BILI_ROLLOUT_PLUGIN_INCREMENTAL_TURNS; + process.env.BILI_ROLLOUT_PLUGIN_INCREMENTAL_TURNS = "1"; + __resetIncrementalClientForTests(); + let h: Harness | undefined; + try { + h = await harness("full"); + const conversationId = "conv-system-clear"; + const original = [{ role: "user", content: "original request" }]; + const seed = planIncrementalTurn({ conversationId, messages: original, system: "OLD-SYSTEM-INSTRUCTION" }); + assert.notEqual(seed.mode, "skip"); + if (seed.mode === "skip") return; + const seeded = await h.markerTurn(h.path, JSON.parse(seed.body)); + assert.equal(seeded.status, 200); + assert.equal(seeded.appliedSeq, original.length); + assert.ok(JSON.stringify(h.forwarded.at(-1)?.system).includes("OLD-SYSTEM-INSTRUCTION")); + noteIncrementalAck(conversationId, original.length, original, "OLD-SYSTEM-INSTRUCTION"); + if (scenario === "fresh client") __resetIncrementalClientForTests(); + const messages = scenario === "append" + ? [...original, { role: "assistant", content: "answer" }, { role: "user", content: "next request" }] + : scenario === "edit" ? [{ role: "user", content: "edited request" }] : original; + const plan = planIncrementalTurn({ conversationId, messages, system: undefined }, scenario === "forced resync"); + assert.equal(plan.mode, scenario === "fresh client" ? "seed" : scenario === "edit" || scenario === "forced resync" ? "resync" : "delta"); + const cleared = await h.markerTurn(h.path, JSON.parse(plan.body)); + assert.equal(cleared.status, 200); + assert.equal(cleared.appliedSeq, messages.length); + assert.equal(JSON.stringify(h.forwarded.at(-1)?.system ?? "").includes("OLD-SYSTEM-INSTRUCTION"), false, "removed original system must not be forwarded"); + noteIncrementalAck(conversationId, messages.length, messages, undefined); + const next = planIncrementalTurn({ conversationId, messages: [...messages, { role: "user", content: "another request" }], system: undefined }); + assert.equal(next.mode, "delta"); + if (next.mode !== "delta") return; + assert.equal(Object.hasOwn(JSON.parse(next.body), "system"), false, "acknowledged absence stays omitted on subsequent deltas"); + const followed = await h.markerTurn(h.path, JSON.parse(next.body)); + assert.equal(followed.status, 200); + assert.equal(JSON.stringify(h.forwarded.at(-1)?.system ?? "").includes("OLD-SYSTEM-INSTRUCTION"), false); + } finally { + if (priorRollout === undefined) delete process.env.BILI_ROLLOUT_PLUGIN_INCREMENTAL_TURNS; + else process.env.BILI_ROLLOUT_PLUGIN_INCREMENTAL_TURNS = priorRollout; + __resetIncrementalClientForTests(); + await h?.close(); + } + }); +} + +// --- client helper state machine (src/agent/incremental-client.ts) ------------------ + +test("incremental client helper: seed implies resync, delta slices, edits resync, sticky system", async () => { + const { __resetIncrementalClientForTests, planIncrementalTurn, noteIncrementalAck, markIncrementalDead } = await import("../src/agent/incremental-client.js"); + __resetIncrementalClientForTests(); + const conv = "conv-helper-1"; + const msgs1 = [{ role: "user", content: "hello there" }]; + const seed = planIncrementalTurn({ conversationId: conv, messages: msgs1, system: "be terse" }); + assert.equal(seed.mode, "seed"); + const seedBody = JSON.parse(seed.body) as { sinceSeq: number; resync: boolean; messages: unknown[]; system?: string }; + assert.equal(seedBody.sinceSeq, 0); + assert.equal(seedBody.resync, true, "seed never 409s — replacement semantics"); + assert.equal(seedBody.system, "be terse"); + + // ack seq 1 → next turn sends only the new tail + noteIncrementalAck(conv, 1, msgs1, "be terse"); + const msgs2 = [...msgs1, { role: "assistant", content: "hi" }, { role: "user", content: "and now?" }]; + const delta = planIncrementalTurn({ conversationId: conv, messages: msgs2, system: "be terse" }); + assert.equal(delta.mode, "delta"); + const deltaBody = JSON.parse(delta.body) as { sinceSeq: number; messages: unknown[]; system?: string }; + assert.equal(deltaBody.sinceSeq, 1); + assert.equal(deltaBody.messages.length, 2, "only the messages added since the ack"); + assert.equal(deltaBody.system, undefined, "sticky system omitted when unchanged"); + + // edited prefix → resync with the full history + const edited = [{ role: "user", content: "hello there (edited)" }, ...msgs2.slice(1)]; + const rs = planIncrementalTurn({ conversationId: conv, messages: edited, system: "be terse" }); + assert.equal(rs.mode, "resync"); + const rsBody = JSON.parse(rs.body) as { sinceSeq: number; resync: boolean; messages: unknown[] }; + assert.equal(rsBody.resync, true); + assert.equal(rsBody.messages.length, edited.length); + + // system change with an equal message set rides an EMPTY delta — the + // canonical is unchanged, the sticky system updates server-side + noteIncrementalAck(conv, edited.length, edited, "be terse"); + const sysChange = planIncrementalTurn({ conversationId: conv, messages: edited, system: "be verbose" }); + assert.equal(sysChange.mode, "delta"); + const sysBody = JSON.parse(sysChange.body) as { sinceSeq: number; messages: unknown[]; system?: string }; + assert.equal(sysBody.messages.length, 0, "pure sticky-system update — empty delta"); + assert.equal(sysBody.system, "be verbose"); + + // dead conversation falls back to skip until re-probed/reset + markIncrementalDead(conv); + const dead = planIncrementalTurn({ conversationId: conv, messages: edited, system: "be verbose" }); + assert.equal(dead.mode, "skip", "dead conversation rides full resend (host's own path)"); + __resetIncrementalClientForTests(); +}); + +test("wireMessagesLookAnthropic: anthropic shapes pass, OpenAI-tool shapes fall back to full resend", async () => { + const { wireMessagesLookAnthropic } = await import("../src/agent/incremental-client.js"); + assert.equal(wireMessagesLookAnthropic([{ role: "user", content: "hi" }]), true, "string content"); + assert.equal(wireMessagesLookAnthropic([{ role: "user", content: [{ type: "text", text: "hi" }] }]), true, "block content"); + assert.equal(wireMessagesLookAnthropic([{ role: "assistant", content: [{ type: "tool_use", id: "t", name: "x", input: {} }] }, { role: "user", content: [{ type: "tool_result", tool_use_id: "t", content: "ok" }] }]), true, "anthropic tool blocks are native wire shape"); + assert.equal(wireMessagesLookAnthropic([]), false, "empty list is not a conversation"); + assert.equal(wireMessagesLookAnthropic("nope"), false); + assert.equal(wireMessagesLookAnthropic([{ role: "user", content: "hi" }, { role: "tool", content: "out" }]), false, "OpenAI tool role is not convertible"); + assert.equal(wireMessagesLookAnthropic([{ role: "assistant", content: "x", tool_calls: [{ id: "c", type: "function", function: { name: "f" } }] }]), false, "tool_calls cannot ride anthropicToCore"); + assert.equal(wireMessagesLookAnthropic([{ role: "user", content: "hi", tool_call_id: "c" }]), false); + assert.equal(wireMessagesLookAnthropic([{ role: "user", content: 42 }]), false, "content must be string or array"); +}); + +test("incrementalTurnBodyShape accepts the incremental payload and rejects anthropic/garbage", async () => { + const { incrementalTurnBodyShape } = await import("../src/incremental-lane.js"); + const inc = Buffer.from(JSON.stringify({ conversationId: "c1", sinceSeq: 0, request: {}, messages: [] })); + assert.equal(incrementalTurnBodyShape(inc), true); + const anthropic = Buffer.from(JSON.stringify({ model: "claude-test", max_tokens: 64, messages: [] })); + assert.equal(incrementalTurnBodyShape(anthropic), false, "top-level model = ordinary request"); + const garbage = Buffer.from("not json at all"); + assert.equal(incrementalTurnBodyShape(garbage), false); + const badSeq = Buffer.from(JSON.stringify({ conversationId: "c1", sinceSeq: "3", messages: [] })); + assert.equal(incrementalTurnBodyShape(badSeq), false, "sinceSeq must be an integer"); +}); From c8155a8a077ae4ed7ab2b9782a3600b63f0fc179 Mon Sep 17 00:00:00 2001 From: awork Date: Tue, 20 Oct 2026 01:21:22 +0800 Subject: [PATCH 2/4] chore: normalize line endings to LF (autocrlf artifact from contributor machine) --- src/agent/incremental-client.ts | 746 ++++++++++----------- tests/incremental-lane.test.ts | 1112 +++++++++++++++---------------- 2 files changed, 929 insertions(+), 929 deletions(-) diff --git a/src/agent/incremental-client.ts b/src/agent/incremental-client.ts index 66b3c095d4..0a1ed6d72e 100644 --- a/src/agent/incremental-client.ts +++ b/src/agent/incremental-client.ts @@ -1,373 +1,373 @@ -// Shared incremental-turn client for native extensions (#3248). -// -// Lives in the HOST process (bundled with each native adapter: pi / omp / -// fetch-patched native lanes) and owns the per-conversation ack state: -// 1. probe the lane once per proxy base (GET /__bili/plugin/turn, TTL -// cache; 404/failed → stay full-resend, byte-identical to today); -// 2. diff the outgoing wire messages against the last acknowledged set — -// matching prefix → send only the delta; mismatch (edit / branch) → -// resync with the full history; the server's ack header is the only -// authority for sinceSeq; -// 3. any lane-level failure (409 storm, 503) falls back to the original -// full-resend request with the marker removed — behavior equals the -// pre-incremental world for that turn. -// -// Wire contract: see PLUGIN.md "Incremental turns". Messages are raw wire -// messages ({role, content: string | blocks[]}) — the lane derives stable -// content ids server-side, so hosts without their own message ids work. - -import { createHash } from "node:crypto"; -import type { DirectSendPrep } from "./native-intercept.js"; - -/** Marker header the lane's divert recognizes. Client-side copy of - * INCREMENTAL_TURN_MARKER_HEADER in src/incremental-lane.ts — the host-side - * helper must not import server modules. Keep the two in sync. */ -export const INCREMENTAL_TURN_MARKER_HEADER = "x-bili-plugin-turn"; - -/** #3329: per-request correlation id stamped on each planned incremental - * turn. The ack channel (native-intercept's onResponse) only carries the URL, - * and several conversations share one provider URL — keying the pending-ack - * table by URL mis-attributes a late response to whichever conversation - * planned last on that URL. The id rides the outgoing request and is read - * back from THAT dispatch's own headers when its response lands, so - * attribution is exact per request. It never travels to the proxy for any - * purpose (the lane ignores it) — pure in-process correlation. */ -export const INCREMENTAL_TURN_ID_HEADER = "x-bili-turn-id"; - -let turnIdCounter = 0; - -/** Fresh per-request turn id: time base + process counter + random tail. - * Uniqueness only needs to hold inside this process (the id never crosses a - * wire boundary for correlation purposes). */ -export function newIncrementalTurnId(): string { - return `${Date.now().toString(36)}-${(turnIdCounter++).toString(36)}-${Math.random().toString(36).slice(2, 10)}`; -} - -// #3304: originals stash for the direct-send guard. When an adapter swaps a -// request's body for the incremental payload, it stashes the ORIGINAL body -// string under that turn's id. If the proxy dies before the send lands, -// native-intercept's fall-back consults createPrepareDirectSend below and -// either restores these bytes (availability) or refuses the direct send -// (safety) — a lane-only payload never reaches a provider that cannot parse -// it. Entries age out by TTL (a single turn never outlives minutes) and are -// capped so a leak cannot grow the map. -const ORIGINAL_TTL_MS = 5 * 60_000; -const ORIGINAL_MAX_ENTRIES = 256; -const originalBodies = new Map(); - -/** Stash the pre-swap body of one planned turn under its turn id. */ -export function rememberOriginalBody(turnId: string, body: string): void { - const now = Date.now(); - for (const [key, entry] of originalBodies) if (now - entry.at >= ORIGINAL_TTL_MS) originalBodies.delete(key); - if (originalBodies.size >= ORIGINAL_MAX_ENTRIES) { - let oldestKey: string | undefined; - let oldestAt = Number.POSITIVE_INFINITY; - for (const [key, entry] of originalBodies) { - if (entry.at < oldestAt) { oldestAt = entry.at; oldestKey = key; } - } - if (oldestKey !== undefined) originalBodies.delete(oldestKey); - } - originalBodies.set(turnId, { body, at: now }); -} - -/** Non-consuming lookup: a host-level retry re-dispatches the SAME request - * (same turn id) and must get the original back again, so entries are kept - * until TTL instead of being deleted on read. */ -export function originalBodyForTurn(turnId: string): string | null { - const entry = originalBodies.get(turnId); - if (entry === undefined || Date.now() - entry.at >= ORIGINAL_TTL_MS) return null; - return entry.body; -} - -/** #3304: prepareDirectSend implementation for lanes that stamp the turn id - * ATOMICALLY with the body swap (dsh / omp / opencode-native): presence of - * x-bili-turn-id on the outgoing request proves the body was replaced, so - * a missing/expired original means "mutated but unrecoverable" → refuse - * rather than risk sending lane-only bytes upstream. Lanes without the hook - * (every non-incremental traffic) keep today's byte-identical behavior. */ -export function createPrepareDirectSend(): (url: string, input: string | URL | Request, init: RequestInit | undefined) => Promise { - return async (_url, input, init) => { - let turnId: string | null = null; - try { - const src = (init?.headers ?? (input instanceof Request ? input.headers : undefined)) as ConstructorParameters[0] | undefined; - if (src !== undefined) turnId = new Headers(src).get(INCREMENTAL_TURN_ID_HEADER); - } catch { - turnId = null; - } - if (turnId === null) return { outcome: "as-is" }; - const body = originalBodyForTurn(turnId); - return body === null ? { outcome: "unrestorable" } : { outcome: "restored", body }; - }; -} - -/** #3304: is this parsed body an incremental envelope — i.e. WOULD the - * lane's marker divert claim it? Mirrors incrementalTurnBodyShape in - * src/incremental-lane.ts (keep in sync): such a body is lane-only, so it - * must never be forwarded to the real upstream as-is. Returns the - * conversationId when it matches (pi correlates its stash through it), - * undefined otherwise. */ -export function incrementalEnvelopeConversationId(body: unknown): string | undefined { - if (body === null || typeof body !== "object" || Array.isArray(body)) return undefined; - const b = body as Record; - if (typeof b.conversationId !== "string" || b.conversationId.length === 0) return undefined; - if (typeof b.sinceSeq !== "number" || !Number.isInteger(b.sinceSeq) || b.sinceSeq < 0) return undefined; - if (!Array.isArray(b.messages)) return undefined; - if (b.model !== undefined) return undefined; - return b.conversationId; -} - -/** #3329: drop pending entries older than `ttlMs` — a planned turn whose - * response never comes (network-dead request) must not pin its slot forever. - * Memory hygiene only since attribution is exact per request: correctness no - * longer depends on how long an entry survives. */ -export function sweepStale(map: Map, ttlMs: number): void { - const now = Date.now(); - for (const [key, entry] of map) if (now - entry.at >= ttlMs) map.delete(key); -} - -/** Same allowlist the lane accepts in `request` (mirror of - * REQUEST_PARAM_KEYS in src/incremental-lane.ts — keep in sync). */ -const REQUEST_PARAM_KEYS = [ - "model", "max_tokens", "temperature", "top_p", "top_k", "stop_sequences", - "stream", "tools", "tool_choice", "metadata", "thinking", "service_tier", - "parallel_tool_calls", - // Anthropic-on-Vertex has no `model` in the body — `anthropic_version` - // carries the wire revision instead (live capture 2026-10-15). Must stay - // identical to the lane's copy in src/incremental-lane.ts. - "anthropic_version", -] as const; -const REQUEST_PARAM_KEY_SET = new Set(REQUEST_PARAM_KEYS); - -/** Keys the lane assembles from the wire's top level — hosts pass the full - * anthropic body as `request`, so these are by-design, not dropped. */ -const LANE_OWNED_REQUEST_KEYS = new Set(["messages", "system"]); - -/** #3406 item 6: params dropped here never reach the lane (its own warn - * cannot fire for helper-planned turns), so surface them once per - * conversation+key. Bounded: past the pair budget the map resets and the - * next epoch warns again. */ -const droppedParamWarned = new Map>(); -let droppedParamWarnPairs = 0; -const DROPPED_PARAM_WARN_BUDGET = 4096; - -function warnDroppedRequestParams(conversationId: string, request: Record): void { - const fresh = Object.keys(request).filter((key) => !REQUEST_PARAM_KEY_SET.has(key) && !LANE_OWNED_REQUEST_KEYS.has(key)); - if (fresh.length === 0) return; - const seen = droppedParamWarned.get(conversationId) ?? new Set(); - const unwarned = fresh.filter((key) => !seen.has(key)); - if (unwarned.length === 0) return; - if (droppedParamWarnPairs + unwarned.length > DROPPED_PARAM_WARN_BUDGET) { - droppedParamWarned.clear(); - droppedParamWarnPairs = 0; - seen.clear(); - } - droppedParamWarnPairs += unwarned.length; - for (const key of unwarned) seen.add(key); - droppedParamWarned.set(conversationId, seen); - console.error(`[bili-incremental] ${conversationId}: request parameter(s) outside the incremental allowlist are not sent: ${unwarned.join(", ")} — extend REQUEST_PARAM_KEYS (src/agent/incremental-client.ts) if they must reach the upstream (#3406)`); -} - -export interface IncrementalWireMessage { - role: string; - content: unknown; -} - -interface ConvState { - /** Last server ack (x-bili-applied-seq) — flat message count. */ - sinceSeq: number; - /** Wire hashes of everything the server has acknowledged, in order. */ - sent: string[]; - /** Sticky system (JSON) we told the lane; resend only on change. */ - systemJson: string | undefined; - /** Per-conversation kill switch after unrecoverable failures. */ - dead: boolean; -} - -const convs = new Map(); - -const PROBE_TTL_MS = 30_000; -const probeCache = new Map(); - -/** Probe the lane's incremental support. Cached per proxy base for 30s; any - * error or non-200 → not enabled (older servers 404 the GET). */ -export async function probeIncrementalTurns(proxyBase: string, fetchImpl: typeof fetch = fetch): Promise { - const now = Date.now(); - const cached = probeCache.get(proxyBase); - if (cached && now - cached.at < PROBE_TTL_MS) return cached.ok; - let ok = false; - try { - const res = await fetchImpl(`${proxyBase}/__bili/plugin/turn`, { method: "GET", signal: AbortSignal.timeout(3000) }); - if (res.ok) { - const body = await res.json() as { enabled?: unknown }; - ok = body.enabled === true; - } - } catch { - ok = false; - } - probeCache.set(proxyBase, { ok, at: now }); - return ok; -} - -/** Does this message list look like the Anthropic wire shape the lane's - * anthropicToCore can ingest? OpenAI-tool shapes (role:"tool"/"function", - * assistant.tool_calls, tool_call_id) are NOT convertible — the adapter - * falls back to a full resend for those conversations (correctness over - * savings). Content may be a string or a block array, both fine. */ -export function wireMessagesLookAnthropic(messages: unknown): messages is IncrementalWireMessage[] { - if (!Array.isArray(messages) || messages.length === 0) return false; - for (const m of messages) { - if (m === null || typeof m !== "object") return false; - const msg = m as { role?: unknown; content?: unknown; tool_calls?: unknown; tool_call_id?: unknown }; - if (typeof msg.role !== "string" || msg.role === "tool" || msg.role === "function") return false; - if (msg.tool_calls !== undefined || msg.tool_call_id !== undefined) return false; - if (typeof msg.content !== "string" && !Array.isArray(msg.content)) return false; - } - return true; -} - -function wireHash(m: IncrementalWireMessage): string { - return createHash("sha256").update(`${m.role}\u0000${JSON.stringify(m.content)}`).digest("hex").slice(0, 16); -} - -type IncrementalPlan = - | { mode: "seed"; body: string } // full history, sinceSeq 0 (server may 409 → resync) - | { mode: "delta"; body: string } // suffix only, acked sinceSeq - | { mode: "resync"; body: string } // full history, resync: true (recovery) - | { mode: "skip" }; // nothing to gain / state dead → full resend - -interface PlanArgs { - conversationId: string; - /** Forward path of the ORIGINAL request (the marker divert uses it); - * optional — the lane defaults a bare `path` to /v1/messages. */ - path?: string; - messages: IncrementalWireMessage[]; - /** System from the complete host request; undefined means the host sent none. */ - system: unknown; - /** Anthropic request params from the original body (model, max_tokens…); - * optional — missing params ride the lane's defaults. */ - request?: Record; -} - -function buildBody(args: PlanArgs, sinceSeq: number, messages: IncrementalWireMessage[], resync: boolean, includeSystem: boolean): string { - // `request` is optional at the call sites (a host may plan with only - // messages+system); missing params simply ride their lane defaults. - const src = args.request ?? {}; - const request: Record = {}; - for (const key of REQUEST_PARAM_KEYS) if (key in src) request[key] = src[key]; - warnDroppedRequestParams(args.conversationId, src); - const body: Record = { - conversationId: args.conversationId, - sinceSeq, - path: args.path ?? "/v1/messages", - request, - messages, - }; - if (resync) body.resync = true; - if (includeSystem) body.system = args.system === undefined ? null : args.system; - return JSON.stringify(body); -} - -/** Plan the incremental form of one outgoing model request. `forceResync` - * replans after a 409 (seed hit a live canonical, or an edit diverged). */ -export function planIncrementalTurn(args: PlanArgs, forceResync = false): IncrementalPlan { - const state = convs.get(args.conversationId); - if ((state !== undefined && state.dead) || args.messages.length === 0) return { mode: "skip" }; - const systemJson = args.system === undefined ? undefined : JSON.stringify(args.system); - if (state === undefined || forceResync) { - const includeSystem = state === undefined || systemJson !== state.systemJson; - // First attempt (and every forced replan) sends the FULL set with - // resync:true — a wholesale canonical replacement never 409s, so the - // first incremental turn works whether the server-side history is - // empty (fresh conversation) or already live (interop with - // full-resend traffic: the client's view is authoritative and - // identical-or-newer). Only DELTA turns can 409. - return { mode: forceResync ? "resync" : "seed", body: buildBody(args, 0, args.messages, true, includeSystem) }; - } - const hashes = args.messages.map(wireHash); - // Prefix alignment: everything previously acked must still lead the list. - const n = Math.min(state.sent.length, hashes.length); - let aligned = true; - for (let i = 0; i < n; i++) { - if (state.sent[i] !== hashes[i]) { aligned = false; break; } - } - if (!aligned || hashes.length < state.sent.length) { - return { mode: "resync", body: buildBody(args, 0, args.messages, true, systemJson !== state.systemJson) }; - } - if (hashes.length === state.sent.length) { - // Equal message set: normally a retry of the same turn → normal path. - // But a SYSTEM change is still a real outgoing turn — send an empty - // delta so the lane forwards the assembled request with the new - // sticky system (the canonical itself is unchanged). - if (systemJson !== state.systemJson) { - return { mode: "delta", body: buildBody(args, state.sinceSeq, [], false, true) }; - } - return { mode: "skip" }; // retry of the same turn → normal path - } - return { - mode: "delta", - body: buildBody(args, state.sinceSeq, args.messages.slice(state.sent.length), false, systemJson !== state.systemJson), - }; -} - -/** Record a 200 ack: `appliedSeq` is the response's x-bili-applied-seq and - * `messages` the wire set the server just acknowledged. An ack clears the - * rejection circuit — the conversation is healthy again. */ -export function noteIncrementalAck(conversationId: string, appliedSeq: number, messages: IncrementalWireMessage[], system: unknown): void { - rejectionCircuit.delete(conversationId); - convs.set(conversationId, { - sinceSeq: appliedSeq, - sent: messages.map(wireHash), - systemJson: system === undefined ? undefined : JSON.stringify(system), - dead: false, - }); -} - -/** Consecutive 409s (no intervening ack) that trip the sticky kill switch. - * A SINGLE 409 just resets the conversation state — the next turn re-seeds - * with a full resync:true body, which cannot 409 — so transient drift (sidecar - * degraded at boot, one upstream 5xx between apply and forward, a sibling host - * re-seeding) self-heals on the very next turn instead of permanently - * downgrading the conversation (#3369). */ -const REJECTION_CIRCUIT = 3; - -/** Consecutive rejections per conversation, OUTSIDE `convs` — a reset deletes - * the wire state, but the circuit counter must survive it. */ -const rejectionCircuit = new Map(); - -/** Record a lane rejection (409 seq drift). Returns "reset" when the next - * turn re-seeds (transient drift — the common case) and "dead" when the - * sticky kill switch tripped (persistent drift: full resend for the rest of - * the process lifetime, exactly the pre-#3369 behavior). */ -export function noteIncrementalRejection(conversationId: string): "reset" | "dead" { - const strikes = (rejectionCircuit.get(conversationId) ?? 0) + 1; - if (strikes >= REJECTION_CIRCUIT) { - rejectionCircuit.delete(conversationId); - convs.set(conversationId, { sinceSeq: 0, sent: [], systemJson: undefined, dead: true }); - return "dead"; - } - rejectionCircuit.set(conversationId, strikes); - convs.delete(conversationId); // next turn re-seeds: full set, resync:true, sinceSeq 0 - return "reset"; -} - -/** Sticky per-conversation fallback after an unrecoverable lane failure — - * the conversation keeps working via plain full resend. */ -export function markIncrementalDead(conversationId: string): void { - const state = convs.get(conversationId); - if (state) state.dead = true; -} - -/** Drop the cached probe for one proxy base (e.g. after a 503 — the lane's - * rollout flag or sidecar liveness changed). The next turn re-probes. */ -export function clearIncrementalProbe(proxyBase: string): void { - probeCache.delete(proxyBase); -} - -export function __resetIncrementalClientForTests(): void { - convs.clear(); - probeCache.clear(); - rejectionCircuit.clear(); - droppedParamWarned.clear(); - droppedParamWarnPairs = 0; - originalBodies.clear(); -} +// Shared incremental-turn client for native extensions (#3248). +// +// Lives in the HOST process (bundled with each native adapter: pi / omp / +// fetch-patched native lanes) and owns the per-conversation ack state: +// 1. probe the lane once per proxy base (GET /__bili/plugin/turn, TTL +// cache; 404/failed → stay full-resend, byte-identical to today); +// 2. diff the outgoing wire messages against the last acknowledged set — +// matching prefix → send only the delta; mismatch (edit / branch) → +// resync with the full history; the server's ack header is the only +// authority for sinceSeq; +// 3. any lane-level failure (409 storm, 503) falls back to the original +// full-resend request with the marker removed — behavior equals the +// pre-incremental world for that turn. +// +// Wire contract: see PLUGIN.md "Incremental turns". Messages are raw wire +// messages ({role, content: string | blocks[]}) — the lane derives stable +// content ids server-side, so hosts without their own message ids work. + +import { createHash } from "node:crypto"; +import type { DirectSendPrep } from "./native-intercept.js"; + +/** Marker header the lane's divert recognizes. Client-side copy of + * INCREMENTAL_TURN_MARKER_HEADER in src/incremental-lane.ts — the host-side + * helper must not import server modules. Keep the two in sync. */ +export const INCREMENTAL_TURN_MARKER_HEADER = "x-bili-plugin-turn"; + +/** #3329: per-request correlation id stamped on each planned incremental + * turn. The ack channel (native-intercept's onResponse) only carries the URL, + * and several conversations share one provider URL — keying the pending-ack + * table by URL mis-attributes a late response to whichever conversation + * planned last on that URL. The id rides the outgoing request and is read + * back from THAT dispatch's own headers when its response lands, so + * attribution is exact per request. It never travels to the proxy for any + * purpose (the lane ignores it) — pure in-process correlation. */ +export const INCREMENTAL_TURN_ID_HEADER = "x-bili-turn-id"; + +let turnIdCounter = 0; + +/** Fresh per-request turn id: time base + process counter + random tail. + * Uniqueness only needs to hold inside this process (the id never crosses a + * wire boundary for correlation purposes). */ +export function newIncrementalTurnId(): string { + return `${Date.now().toString(36)}-${(turnIdCounter++).toString(36)}-${Math.random().toString(36).slice(2, 10)}`; +} + +// #3304: originals stash for the direct-send guard. When an adapter swaps a +// request's body for the incremental payload, it stashes the ORIGINAL body +// string under that turn's id. If the proxy dies before the send lands, +// native-intercept's fall-back consults createPrepareDirectSend below and +// either restores these bytes (availability) or refuses the direct send +// (safety) — a lane-only payload never reaches a provider that cannot parse +// it. Entries age out by TTL (a single turn never outlives minutes) and are +// capped so a leak cannot grow the map. +const ORIGINAL_TTL_MS = 5 * 60_000; +const ORIGINAL_MAX_ENTRIES = 256; +const originalBodies = new Map(); + +/** Stash the pre-swap body of one planned turn under its turn id. */ +export function rememberOriginalBody(turnId: string, body: string): void { + const now = Date.now(); + for (const [key, entry] of originalBodies) if (now - entry.at >= ORIGINAL_TTL_MS) originalBodies.delete(key); + if (originalBodies.size >= ORIGINAL_MAX_ENTRIES) { + let oldestKey: string | undefined; + let oldestAt = Number.POSITIVE_INFINITY; + for (const [key, entry] of originalBodies) { + if (entry.at < oldestAt) { oldestAt = entry.at; oldestKey = key; } + } + if (oldestKey !== undefined) originalBodies.delete(oldestKey); + } + originalBodies.set(turnId, { body, at: now }); +} + +/** Non-consuming lookup: a host-level retry re-dispatches the SAME request + * (same turn id) and must get the original back again, so entries are kept + * until TTL instead of being deleted on read. */ +export function originalBodyForTurn(turnId: string): string | null { + const entry = originalBodies.get(turnId); + if (entry === undefined || Date.now() - entry.at >= ORIGINAL_TTL_MS) return null; + return entry.body; +} + +/** #3304: prepareDirectSend implementation for lanes that stamp the turn id + * ATOMICALLY with the body swap (dsh / omp / opencode-native): presence of + * x-bili-turn-id on the outgoing request proves the body was replaced, so + * a missing/expired original means "mutated but unrecoverable" → refuse + * rather than risk sending lane-only bytes upstream. Lanes without the hook + * (every non-incremental traffic) keep today's byte-identical behavior. */ +export function createPrepareDirectSend(): (url: string, input: string | URL | Request, init: RequestInit | undefined) => Promise { + return async (_url, input, init) => { + let turnId: string | null = null; + try { + const src = (init?.headers ?? (input instanceof Request ? input.headers : undefined)) as ConstructorParameters[0] | undefined; + if (src !== undefined) turnId = new Headers(src).get(INCREMENTAL_TURN_ID_HEADER); + } catch { + turnId = null; + } + if (turnId === null) return { outcome: "as-is" }; + const body = originalBodyForTurn(turnId); + return body === null ? { outcome: "unrestorable" } : { outcome: "restored", body }; + }; +} + +/** #3304: is this parsed body an incremental envelope — i.e. WOULD the + * lane's marker divert claim it? Mirrors incrementalTurnBodyShape in + * src/incremental-lane.ts (keep in sync): such a body is lane-only, so it + * must never be forwarded to the real upstream as-is. Returns the + * conversationId when it matches (pi correlates its stash through it), + * undefined otherwise. */ +export function incrementalEnvelopeConversationId(body: unknown): string | undefined { + if (body === null || typeof body !== "object" || Array.isArray(body)) return undefined; + const b = body as Record; + if (typeof b.conversationId !== "string" || b.conversationId.length === 0) return undefined; + if (typeof b.sinceSeq !== "number" || !Number.isInteger(b.sinceSeq) || b.sinceSeq < 0) return undefined; + if (!Array.isArray(b.messages)) return undefined; + if (b.model !== undefined) return undefined; + return b.conversationId; +} + +/** #3329: drop pending entries older than `ttlMs` — a planned turn whose + * response never comes (network-dead request) must not pin its slot forever. + * Memory hygiene only since attribution is exact per request: correctness no + * longer depends on how long an entry survives. */ +export function sweepStale(map: Map, ttlMs: number): void { + const now = Date.now(); + for (const [key, entry] of map) if (now - entry.at >= ttlMs) map.delete(key); +} + +/** Same allowlist the lane accepts in `request` (mirror of + * REQUEST_PARAM_KEYS in src/incremental-lane.ts — keep in sync). */ +const REQUEST_PARAM_KEYS = [ + "model", "max_tokens", "temperature", "top_p", "top_k", "stop_sequences", + "stream", "tools", "tool_choice", "metadata", "thinking", "service_tier", + "parallel_tool_calls", + // Anthropic-on-Vertex has no `model` in the body — `anthropic_version` + // carries the wire revision instead (live capture 2026-10-15). Must stay + // identical to the lane's copy in src/incremental-lane.ts. + "anthropic_version", +] as const; +const REQUEST_PARAM_KEY_SET = new Set(REQUEST_PARAM_KEYS); + +/** Keys the lane assembles from the wire's top level — hosts pass the full + * anthropic body as `request`, so these are by-design, not dropped. */ +const LANE_OWNED_REQUEST_KEYS = new Set(["messages", "system"]); + +/** #3406 item 6: params dropped here never reach the lane (its own warn + * cannot fire for helper-planned turns), so surface them once per + * conversation+key. Bounded: past the pair budget the map resets and the + * next epoch warns again. */ +const droppedParamWarned = new Map>(); +let droppedParamWarnPairs = 0; +const DROPPED_PARAM_WARN_BUDGET = 4096; + +function warnDroppedRequestParams(conversationId: string, request: Record): void { + const fresh = Object.keys(request).filter((key) => !REQUEST_PARAM_KEY_SET.has(key) && !LANE_OWNED_REQUEST_KEYS.has(key)); + if (fresh.length === 0) return; + const seen = droppedParamWarned.get(conversationId) ?? new Set(); + const unwarned = fresh.filter((key) => !seen.has(key)); + if (unwarned.length === 0) return; + if (droppedParamWarnPairs + unwarned.length > DROPPED_PARAM_WARN_BUDGET) { + droppedParamWarned.clear(); + droppedParamWarnPairs = 0; + seen.clear(); + } + droppedParamWarnPairs += unwarned.length; + for (const key of unwarned) seen.add(key); + droppedParamWarned.set(conversationId, seen); + console.error(`[bili-incremental] ${conversationId}: request parameter(s) outside the incremental allowlist are not sent: ${unwarned.join(", ")} — extend REQUEST_PARAM_KEYS (src/agent/incremental-client.ts) if they must reach the upstream (#3406)`); +} + +export interface IncrementalWireMessage { + role: string; + content: unknown; +} + +interface ConvState { + /** Last server ack (x-bili-applied-seq) — flat message count. */ + sinceSeq: number; + /** Wire hashes of everything the server has acknowledged, in order. */ + sent: string[]; + /** Sticky system (JSON) we told the lane; resend only on change. */ + systemJson: string | undefined; + /** Per-conversation kill switch after unrecoverable failures. */ + dead: boolean; +} + +const convs = new Map(); + +const PROBE_TTL_MS = 30_000; +const probeCache = new Map(); + +/** Probe the lane's incremental support. Cached per proxy base for 30s; any + * error or non-200 → not enabled (older servers 404 the GET). */ +export async function probeIncrementalTurns(proxyBase: string, fetchImpl: typeof fetch = fetch): Promise { + const now = Date.now(); + const cached = probeCache.get(proxyBase); + if (cached && now - cached.at < PROBE_TTL_MS) return cached.ok; + let ok = false; + try { + const res = await fetchImpl(`${proxyBase}/__bili/plugin/turn`, { method: "GET", signal: AbortSignal.timeout(3000) }); + if (res.ok) { + const body = await res.json() as { enabled?: unknown }; + ok = body.enabled === true; + } + } catch { + ok = false; + } + probeCache.set(proxyBase, { ok, at: now }); + return ok; +} + +/** Does this message list look like the Anthropic wire shape the lane's + * anthropicToCore can ingest? OpenAI-tool shapes (role:"tool"/"function", + * assistant.tool_calls, tool_call_id) are NOT convertible — the adapter + * falls back to a full resend for those conversations (correctness over + * savings). Content may be a string or a block array, both fine. */ +export function wireMessagesLookAnthropic(messages: unknown): messages is IncrementalWireMessage[] { + if (!Array.isArray(messages) || messages.length === 0) return false; + for (const m of messages) { + if (m === null || typeof m !== "object") return false; + const msg = m as { role?: unknown; content?: unknown; tool_calls?: unknown; tool_call_id?: unknown }; + if (typeof msg.role !== "string" || msg.role === "tool" || msg.role === "function") return false; + if (msg.tool_calls !== undefined || msg.tool_call_id !== undefined) return false; + if (typeof msg.content !== "string" && !Array.isArray(msg.content)) return false; + } + return true; +} + +function wireHash(m: IncrementalWireMessage): string { + return createHash("sha256").update(`${m.role}\u0000${JSON.stringify(m.content)}`).digest("hex").slice(0, 16); +} + +type IncrementalPlan = + | { mode: "seed"; body: string } // full history, sinceSeq 0 (server may 409 → resync) + | { mode: "delta"; body: string } // suffix only, acked sinceSeq + | { mode: "resync"; body: string } // full history, resync: true (recovery) + | { mode: "skip" }; // nothing to gain / state dead → full resend + +interface PlanArgs { + conversationId: string; + /** Forward path of the ORIGINAL request (the marker divert uses it); + * optional — the lane defaults a bare `path` to /v1/messages. */ + path?: string; + messages: IncrementalWireMessage[]; + /** System from the complete host request; undefined means the host sent none. */ + system: unknown; + /** Anthropic request params from the original body (model, max_tokens…); + * optional — missing params ride the lane's defaults. */ + request?: Record; +} + +function buildBody(args: PlanArgs, sinceSeq: number, messages: IncrementalWireMessage[], resync: boolean, includeSystem: boolean): string { + // `request` is optional at the call sites (a host may plan with only + // messages+system); missing params simply ride their lane defaults. + const src = args.request ?? {}; + const request: Record = {}; + for (const key of REQUEST_PARAM_KEYS) if (key in src) request[key] = src[key]; + warnDroppedRequestParams(args.conversationId, src); + const body: Record = { + conversationId: args.conversationId, + sinceSeq, + path: args.path ?? "/v1/messages", + request, + messages, + }; + if (resync) body.resync = true; + if (includeSystem) body.system = args.system === undefined ? null : args.system; + return JSON.stringify(body); +} + +/** Plan the incremental form of one outgoing model request. `forceResync` + * replans after a 409 (seed hit a live canonical, or an edit diverged). */ +export function planIncrementalTurn(args: PlanArgs, forceResync = false): IncrementalPlan { + const state = convs.get(args.conversationId); + if ((state !== undefined && state.dead) || args.messages.length === 0) return { mode: "skip" }; + const systemJson = args.system === undefined ? undefined : JSON.stringify(args.system); + if (state === undefined || forceResync) { + const includeSystem = state === undefined || systemJson !== state.systemJson; + // First attempt (and every forced replan) sends the FULL set with + // resync:true — a wholesale canonical replacement never 409s, so the + // first incremental turn works whether the server-side history is + // empty (fresh conversation) or already live (interop with + // full-resend traffic: the client's view is authoritative and + // identical-or-newer). Only DELTA turns can 409. + return { mode: forceResync ? "resync" : "seed", body: buildBody(args, 0, args.messages, true, includeSystem) }; + } + const hashes = args.messages.map(wireHash); + // Prefix alignment: everything previously acked must still lead the list. + const n = Math.min(state.sent.length, hashes.length); + let aligned = true; + for (let i = 0; i < n; i++) { + if (state.sent[i] !== hashes[i]) { aligned = false; break; } + } + if (!aligned || hashes.length < state.sent.length) { + return { mode: "resync", body: buildBody(args, 0, args.messages, true, systemJson !== state.systemJson) }; + } + if (hashes.length === state.sent.length) { + // Equal message set: normally a retry of the same turn → normal path. + // But a SYSTEM change is still a real outgoing turn — send an empty + // delta so the lane forwards the assembled request with the new + // sticky system (the canonical itself is unchanged). + if (systemJson !== state.systemJson) { + return { mode: "delta", body: buildBody(args, state.sinceSeq, [], false, true) }; + } + return { mode: "skip" }; // retry of the same turn → normal path + } + return { + mode: "delta", + body: buildBody(args, state.sinceSeq, args.messages.slice(state.sent.length), false, systemJson !== state.systemJson), + }; +} + +/** Record a 200 ack: `appliedSeq` is the response's x-bili-applied-seq and + * `messages` the wire set the server just acknowledged. An ack clears the + * rejection circuit — the conversation is healthy again. */ +export function noteIncrementalAck(conversationId: string, appliedSeq: number, messages: IncrementalWireMessage[], system: unknown): void { + rejectionCircuit.delete(conversationId); + convs.set(conversationId, { + sinceSeq: appliedSeq, + sent: messages.map(wireHash), + systemJson: system === undefined ? undefined : JSON.stringify(system), + dead: false, + }); +} + +/** Consecutive 409s (no intervening ack) that trip the sticky kill switch. + * A SINGLE 409 just resets the conversation state — the next turn re-seeds + * with a full resync:true body, which cannot 409 — so transient drift (sidecar + * degraded at boot, one upstream 5xx between apply and forward, a sibling host + * re-seeding) self-heals on the very next turn instead of permanently + * downgrading the conversation (#3369). */ +const REJECTION_CIRCUIT = 3; + +/** Consecutive rejections per conversation, OUTSIDE `convs` — a reset deletes + * the wire state, but the circuit counter must survive it. */ +const rejectionCircuit = new Map(); + +/** Record a lane rejection (409 seq drift). Returns "reset" when the next + * turn re-seeds (transient drift — the common case) and "dead" when the + * sticky kill switch tripped (persistent drift: full resend for the rest of + * the process lifetime, exactly the pre-#3369 behavior). */ +export function noteIncrementalRejection(conversationId: string): "reset" | "dead" { + const strikes = (rejectionCircuit.get(conversationId) ?? 0) + 1; + if (strikes >= REJECTION_CIRCUIT) { + rejectionCircuit.delete(conversationId); + convs.set(conversationId, { sinceSeq: 0, sent: [], systemJson: undefined, dead: true }); + return "dead"; + } + rejectionCircuit.set(conversationId, strikes); + convs.delete(conversationId); // next turn re-seeds: full set, resync:true, sinceSeq 0 + return "reset"; +} + +/** Sticky per-conversation fallback after an unrecoverable lane failure — + * the conversation keeps working via plain full resend. */ +export function markIncrementalDead(conversationId: string): void { + const state = convs.get(conversationId); + if (state) state.dead = true; +} + +/** Drop the cached probe for one proxy base (e.g. after a 503 — the lane's + * rollout flag or sidecar liveness changed). The next turn re-probes. */ +export function clearIncrementalProbe(proxyBase: string): void { + probeCache.delete(proxyBase); +} + +export function __resetIncrementalClientForTests(): void { + convs.clear(); + probeCache.clear(); + rejectionCircuit.clear(); + droppedParamWarned.clear(); + droppedParamWarnPairs = 0; + originalBodies.clear(); +} diff --git a/tests/incremental-lane.test.ts b/tests/incremental-lane.test.ts index d2bb69e626..34a7d48be7 100644 --- a/tests/incremental-lane.test.ts +++ b/tests/incremental-lane.test.ts @@ -1,556 +1,556 @@ -import assert from "node:assert/strict"; -import http from "node:http"; -import { once } from "node:events"; -import test from "node:test"; -import { mkdtempSync, rmSync } from "node:fs"; -import { tmpdir } from "node:os"; -import { join } from "node:path"; -import { defaultConfig } from "acp-kernel"; -import { startServer } from "../src/server.ts"; -import type { ProxyOptions } from "../src/config.ts"; -import { SessionStore, _setStoreForTest } from "../src/persist.ts"; -import { _resetPluginStateForTest } from "../src/plugin.ts"; -import { _resetSessionsForTest, peekSession } from "../src/session.ts"; -import { _setForTest } from "../src/registry.ts"; -import { resetToolRingForTest } from "../src/tool-ring.ts"; -import { __resetIncrementalLaneForTests } from "../src/incremental-lane.ts"; - -process.env.NODE_ENV = "test"; -const testRoot = mkdtempSync(join(tmpdir(), "bili-incr-")); -test.after(() => rmSync(testRoot, { recursive: true, force: true })); - -interface Turn { - sinceSeq: number; - messages: unknown[]; - resync?: boolean; - system?: string; - request?: Record; -} - -interface Harness { - proxyUrl: string; - path: string; - turn: (t: Turn) => Promise<{ status: number; headers: Headers; body: unknown }>; - probe: () => Promise<{ status: number; body: unknown }>; - markerTurn: (reqPath: string, payload: { conversationId: string; sinceSeq: number; resync?: boolean; system?: unknown; request?: Record; messages: unknown[] }) => Promise<{ status: number; body: unknown; appliedSeq?: number }>; - markerRaw: (reqPath: string, anthropicBody: Record) => Promise<{ status: number; body: unknown }>; - forwarded: Record[]; - dir: string; - close: () => Promise; -} - -async function harness(storageMode: "full" | "legacy", opts: { upstreamDelayMs?: number; upstreamStatus?: number } = {}): Promise { - const dir = mkdtempSync(join(testRoot, "run-")); - for (const key of ["XDG_STATE_HOME", "XDG_DATA_HOME", "XDG_CACHE_HOME", "XDG_CONFIG_HOME"]) process.env[key] = dir; - process.env.BILI_STORAGE_UNIFIED = storageMode === "full" ? "full" : "legacy"; - _resetSessionsForTest(); - _resetPluginStateForTest(); - resetToolRingForTest(); - __resetIncrementalLaneForTests(); - const store = new SessionStore({ dir: dir + "/sessions", enabled: true, debounceMs: 60000 }); - _setStoreForTest(store); - _setForTest({}); - const forwarded: Record[] = []; - const upstream = http.createServer((req, res) => { - const chunks: Buffer[] = []; - req.on("data", (chunk: Buffer) => chunks.push(chunk)); - req.on("end", () => { - forwarded.push(JSON.parse(Buffer.concat(chunks).toString("utf8")) as Record); - const reply = (): void => { - res.writeHead(opts.upstreamStatus ?? 200, { "content-type": "application/json" }); - res.end(JSON.stringify({ id: "msg_test", role: "assistant", content: [{ type: "text", text: "answer" }], usage: { input_tokens: 10000, output_tokens: 10 } })); - }; - if (opts.upstreamDelayMs) setTimeout(reply, opts.upstreamDelayMs); - else reply(); - }); - }); - upstream.listen(0, "127.0.0.1"); - await once(upstream, "listening"); - const addr = upstream.address(); - assert(addr && typeof addr === "object"); - const upstreamUrl = `http://127.0.0.1:${addr.port}`; - const proxy = await startServer({ port: 0, host: "127.0.0.1", upstream: upstreamUrl, routes: { [upstreamUrl]: { models: { "claude-test": { context: 400000 } } } }, modelContextLimit: 400000, kernelConfig: defaultConfig(400000), compress: { injectTool: true, injectNudge: false }, promptCache: { routing: "auto" }, sessionHeader: "x-acp-session", log: false, debug: false, passthrough: false, autoUpdate: false, mitm: { enabled: false, domains: [] } } as ProxyOptions); - if (!(proxy as unknown as { listening?: boolean }).listening) await once(proxy, "listening"); - const proxyUrl = `http://127.0.0.1:${(proxy.address() as { port: number }).port}`; - const path = "/v1/messages"; - const turn = async (t: Turn) => { - const response = await fetch(`${proxyUrl}/__bili/plugin/turn`, { - method: "POST", - headers: { "content-type": "application/json", "x-bili-plugin": "dsh", "x-bili-plugin-conversation": "conv-inc-1" }, - body: JSON.stringify({ conversationId: "conv-inc-1", sinceSeq: t.sinceSeq, ...(t.resync ? { resync: true } : {}), ...(t.system !== undefined ? { system: t.system } : {}), path, request: { model: "claude-test", max_tokens: 1024, ...(t.request ?? {}) }, messages: t.messages }), - }); - const body: unknown = await response.json().catch(() => null); - return { status: response.status, headers: response.headers, body }; - }; - // store handle must be released before the teardown rm -rf: Windows cannot - // unlink an open index.db (EBUSY) — same seam the storage-unified suite uses. - const probe = async () => { - const response = await fetch(`${proxyUrl}/__bili/plugin/turn`, { method: "GET", headers: { "x-bili-plugin": "dsh" } }); - const body: unknown = await response.json().catch(() => null); - return { status: response.status, body }; - }; - // #3248 marker divert: shaped incremental body posted on the ORDINARY model path - const markerTurn = async (reqPath: string, payload: { conversationId: string; sinceSeq: number; resync?: boolean; system?: unknown; request?: Record; messages: unknown[] }) => { - const response = await fetch(`${proxyUrl}${reqPath}`, { - method: "POST", - headers: { "content-type": "application/json", "x-bili-plugin": "dsh", "x-bili-plugin-conversation": payload.conversationId, "x-acp-session": payload.conversationId, "x-bili-plugin-turn": "1" }, - body: JSON.stringify({ conversationId: payload.conversationId, sinceSeq: payload.sinceSeq, ...(payload.resync ? { resync: true } : {}), ...(payload.system !== undefined ? { system: payload.system } : {}), request: { model: "claude-test", max_tokens: 1024, ...(payload.request ?? {}) }, messages: payload.messages }), - }); - const body: unknown = await response.json().catch(() => null); - const applied = response.headers.get("x-bili-applied-seq"); - return { status: response.status, body, appliedSeq: applied !== null && /^\d+$/.test(applied) ? Number(applied) : undefined }; - }; - // marker on an ordinary anthropic body: must ride the normal pipeline unchanged - const markerRaw = async (reqPath: string, anthropicBody: Record) => { - const response = await fetch(`${proxyUrl}${reqPath}`, { method: "POST", headers: { "content-type": "application/json", "x-bili-plugin": "dsh", "x-acp-session": "conv-marker-plain", "x-bili-plugin-turn": "1" }, body: JSON.stringify(anthropicBody) }); - const body: unknown = await response.json().catch(() => null); - return { status: response.status, body }; - }; - return { proxyUrl, path, turn, probe, markerTurn, markerRaw, forwarded, dir, close: async () => { await new Promise((resolve) => proxy.close(() => resolve())); store.closeQuietly(); await new Promise((resolve) => upstream.close(() => resolve())); } }; -} - -const userTurn = (n: number, text = "hello") => ({ id: `u${n}`, role: "user", contentType: "text", content: text.repeat(400) }); -const assistantTurn = (n: number) => ({ id: `a${n}`, role: "assistant", contentType: "text", content: `model answer ${n}` }); - -test("incremental lane: seed → delta → upstream receives the assembled full history; ack advances", async () => { - const h = await harness("full"); - try { - const seed = await h.turn({ sinceSeq: 0, messages: [userTurn(1)] }); - assert.equal(seed.status, 200); - assert.equal(seed.headers.get("x-bili-applied-seq"), "1"); - const upstream1 = h.forwarded.at(-1) as { system?: unknown; messages: { role: string; content: string }[] }; - assert.ok(Array.isArray(upstream1.messages) && upstream1.messages.length === 1); - assert.equal(upstream1.messages[0].role, "user"); - - // Delta: everything after ack — the previous assistant reply + the new user turn. - const delta = await h.turn({ sinceSeq: 1, messages: [assistantTurn(1), userTurn(2, "second question")] }); - assert.equal(delta.status, 200); - assert.equal(delta.headers.get("x-bili-applied-seq"), "3"); - const upstream2 = h.forwarded.at(-1) as { messages: { role: string }[] }; - assert.equal(upstream2.messages.length, 3, "assembled history = seed + delta"); - assert.deepEqual(upstream2.messages.map((m) => m.role), ["user", "assistant", "user"]); - } finally { - await h.close(); - } -}); - -test("incremental lane: a failed turn (non-2xx) carries NO x-bili-applied-seq (#3344)", async () => { - const h = await harness("full", { upstreamStatus: 500 }); - try { - const failed = await h.turn({ sinceSeq: 0, messages: [userTurn(1)] }); - assert.ok(failed.status >= 400, `expected a non-2xx failure from the failing upstream, got ${failed.status}`); - // The ack must not ride a failure: a host trusting it unconditionally - // would skip its resend and lose the user's message (#3344). - assert.equal(failed.headers.get("x-bili-applied-seq"), null); - } finally { - await h.close(); - } -}); - -test("incremental lane: seq mismatch answers 409 with the server's seq; resync recovers", async () => { - const h = await harness("full"); - try { - const seed = await h.turn({ sinceSeq: 0, messages: [userTurn(1)] }); - assert.equal(seed.status, 200); - // Client guesses a stale seq. - const stale = await h.turn({ sinceSeq: 0, messages: [assistantTurn(1), userTurn(2)] }); - assert.equal(stale.status, 409); - assert.equal((stale.body as { error?: string }).error, "seq-mismatch"); - assert.equal((stale.body as { serverSeq?: number }).serverSeq, 1); - // Resync with the full history re-seeds the canonical. - const recovered = await h.turn({ sinceSeq: 0, resync: true, messages: [userTurn(1), assistantTurn(1), userTurn(2)] }); - assert.equal(recovered.status, 200); - assert.equal(recovered.headers.get("x-bili-applied-seq"), "3"); - const upstream = h.forwarded.at(-1) as { messages: { role: string }[] }; - assert.deepEqual(upstream.messages.map((m) => m.role), ["user", "assistant", "user"]); - } finally { - await h.close(); - } -}); - -test("incremental lane: cold rebuild after restart serves the next delta from the unified table", async () => { - const h = await harness("full"); - try { - const seed = await h.turn({ sinceSeq: 0, messages: [userTurn(1)] }); - assert.equal(seed.status, 200); - // Simulate a process restart: session pool + warm caches dropped. The - // canonical prefix must rebuild from the unified messages table. - _resetSessionsForTest(); - __resetIncrementalLaneForTests(); - const delta = await h.turn({ sinceSeq: 1, messages: [assistantTurn(1), userTurn(2, "after restart")] }); - assert.equal(delta.status, 200); - const upstream = h.forwarded.at(-1) as { messages: { role: string }[] }; - assert.deepEqual(upstream.messages.map((m) => m.role), ["user", "assistant", "user"], "table-rebuilt canonical + delta"); - } finally { - await h.close(); - } -}); - -test("incremental lane: divergent resync survives a cold restart without a 409 loop", async () => { - const h = await harness("full"); - try { - const seed = await h.turn({ sinceSeq: 0, messages: [userTurn(1, "old branch one"), assistantTurn(1), userTurn(2, "old branch two")] }); - assert.equal(seed.status, 200); - // The client rewinds to an edited branch (divergent content, ids and all). - const resync = await h.turn({ sinceSeq: 0, resync: true, messages: [userTurn(1, "edited branch")] }); - assert.equal(resync.status, 200); - assert.equal(resync.headers.get("x-bili-applied-seq"), "1"); - // Cold restart: the canonical must rebuild from the CURRENT branch - // only — the rejected branch's rows stay in the table (append-only) - // but the served history slices past them (baseSeq epoch). - _resetSessionsForTest(); - __resetIncrementalLaneForTests(); - const delta = await h.turn({ sinceSeq: 1, messages: [assistantTurn(1), userTurn(2, "new branch tail")] }); - assert.equal(delta.status, 200); - assert.equal(delta.headers.get("x-bili-applied-seq"), "3"); - const upstream = h.forwarded.at(-1) as { messages: { role: string; content: unknown }[] }; - assert.equal(upstream.messages.length, 3, "served history is the edited branch, not the row union"); - const first = JSON.stringify(upstream.messages[0].content); - assert.ok(first.includes("edited branch"), `first message is the resynced one: ${first.slice(0, 80)}`); - assert.ok(!JSON.stringify(upstream.messages).includes("old branch"), "old branch rows do not leak into the canonical"); - } finally { - await h.close(); - } -}); - -test("incremental lane: sessions share the plugin conversation id — incremental and normal traffic interop", async () => { - const h = await harness("full"); - try { - const seed = await h.turn({ sinceSeq: 0, messages: [userTurn(1)] }); - assert.equal(seed.status, 200); - const session = peekSession("conv-inc-1"); - assert.ok(session, "session id = the plugin conversation id (same as normal plugin traffic)"); - assert.equal(session.stats.requests, 1); - } finally { - await h.close(); - } -}); - -test("incremental lane: sticky system — omitted system keeps the last one, a new value replaces it", async () => { - const h = await harness("full"); - // The assembled body rides the normal pipeline, which renders the system - // prompt as anthropic blocks (with its cache marks) — compare by text. - const sysText = (v: unknown): string => typeof v === "string" ? v : Array.isArray(v) ? v.map((b) => (b as { text?: string })?.text ?? "").join("") : String(v); - try { - await h.turn({ sinceSeq: 0, system: "SYSTEM-A", messages: [userTurn(1)] }); - const upstream1 = sysText((h.forwarded.at(-1) as { system?: unknown }).system); - assert.ok(upstream1.startsWith("SYSTEM-A"), `turn 1 carries the client system: ${upstream1.slice(0, 40)}`); - // Omitted → sticky. - await h.turn({ sinceSeq: 1, messages: [assistantTurn(1), userTurn(2)] }); - const upstream2 = sysText((h.forwarded.at(-1) as { system?: unknown }).system); - assert.ok(upstream2.startsWith("SYSTEM-A"), "omitted system keeps the last one"); - // Explicit new value replaces. - await h.turn({ sinceSeq: 3, system: "SYSTEM-B", messages: [assistantTurn(2), userTurn(3)] }); - const upstream3 = sysText((h.forwarded.at(-1) as { system?: unknown }).system); - assert.ok(upstream3.startsWith("SYSTEM-B"), "explicit system replaces"); - assert.ok(!upstream3.includes("SYSTEM-A"), "the old system is fully replaced"); - } finally { - await h.close(); - } -}); - -test("incremental lane: a second concurrent turn on the same conversation answers 409 turn-in-flight", async () => { - // Margins sized for loaded runners: the settle sleep must beat the first - // request's path into inFlight, and the upstream delay must outlive it — - // 40ms/150ms flaked (review of #3253). - const h = await harness("full", { upstreamDelayMs: 800 }); - try { - const first = h.turn({ sinceSeq: 0, messages: [userTurn(1)] }); - await new Promise((r) => setTimeout(r, 120)); // let the first enter forward - const second = await h.turn({ sinceSeq: 1, messages: [assistantTurn(1), userTurn(2)] }); - assert.equal(second.status, 409); - assert.equal((second.body as { error?: string }).error, "turn-in-flight"); - assert.equal((await first).status, 200); - } finally { - await h.close(); - } -}); - -test("incremental lane: request params (tools/max_tokens) ride the assembled body to the upstream", async () => { - const h = await harness("full"); - try { - const tools = [{ name: "get_weather", description: "w", input_schema: { type: "object", properties: {} } }]; - await h.turn({ sinceSeq: 0, request: { max_tokens: 777, tools }, messages: [userTurn(1)] }); - const upstream = h.forwarded.at(-1) as { max_tokens?: number; tools?: unknown; model?: string }; - assert.equal(upstream.max_tokens, 777); - assert.equal(upstream.model, "claude-test"); - assert.ok(Array.isArray(upstream.tools)); - // Disallowed request params never ride. - await h.turn({ sinceSeq: 1, request: { max_tokens: 512, messages: [{ role: "user", content: "smuggled" }] } as never, messages: [assistantTurn(1), userTurn(2)] }); - const upstream2 = h.forwarded.at(-1) as { messages: { role: string }[] }; - assert.equal(upstream2.messages.length, 3, "request.messages is ignored — the lane owns the message list"); - } finally { - await h.close(); - } -}); - -test("incremental lane: a Vertex turn keeps anthropic_version and still drops an unlisted field", async () => { - const h = await harness("full"); - try { - // Anthropic-on-Vertex has no `model` in the body — `anthropic_version` - // carries the wire revision instead (live capture 2026-10-15). The - // allowlist is the only thing that could drop it on the way out. - await h.turn({ sinceSeq: 0, request: { anthropic_version: "vertex-2023-10-16" }, messages: [userTurn(1)] }); - const upstream = h.forwarded.at(-1) as { anthropic_version?: string; smuggled?: unknown }; - assert.equal(upstream.anthropic_version, "vertex-2023-10-16"); - // The allowlist is still a whitelist: an unlisted key must not ride. - await h.turn({ sinceSeq: 1, request: { anthropic_version: "vertex-2023-10-16", smuggled: true } as never, messages: [assistantTurn(1), userTurn(2)] }); - const upstream2 = h.forwarded.at(-1) as { anthropic_version?: string; smuggled?: unknown }; - assert.equal(upstream2.anthropic_version, "vertex-2023-10-16"); - assert.equal(upstream2.smuggled, undefined); - } finally { - await h.close(); - } -}); - -test("incremental lane: legacy-only storage answers 503 with the enablement hint", async () => { - const h = await harness("legacy"); - try { - const seed = await h.turn({ sinceSeq: 0, messages: [userTurn(1)] }); - assert.equal(seed.status, 503); - assert.match(String((seed.body as { error?: string }).error), /storage\.unified/); - } finally { - await h.close(); - } -}); - -// --- marker divert (#3248 phase 2) ------------------------------------------------- -// The host keeps its ordinary provider URL and stamps `x-bili-plugin-turn: 1`; -// the body shape is the final word (non-incremental bodies ride the normal -// pipeline byte-identically). - -test("incremental marker divert: probe reports the rollout state", async () => { - const h = await harness("full"); - try { - const off = await h.probe(); - assert.equal(off.status, 200); - const offBody = off.body as { ok: boolean; enabled: boolean; feature: string; protocol: number }; - assert.equal(offBody.ok, true); - assert.equal(offBody.feature, "pluginIncrementalTurns"); - assert.equal(offBody.protocol, 1); - assert.equal(offBody.enabled, false, "default rollout percent is 0 — probe must report disabled"); - process.env.BILI_ROLLOUT_PLUGIN_INCREMENTAL_TURNS = "1"; - try { - const on = await h.probe(); - assert.equal(on.status, 200); - assert.equal((on.body as { enabled: boolean }).enabled, true, "env force flips the probe"); - } finally { - delete process.env.BILI_ROLLOUT_PLUGIN_INCREMENTAL_TURNS; - } - } finally { - await h.close(); - } -}); - -test("incremental marker divert: shaped body on the ordinary model path is diverted and assembled", async () => { - process.env.BILI_ROLLOUT_PLUGIN_INCREMENTAL_TURNS = "1"; - let h: Awaited> | undefined; - try { - h = await harness("full"); - const seed = await h.markerTurn("/v1/messages", { conversationId: "conv-marker-1", sinceSeq: 0, request: { max_tokens: 512 }, messages: [userTurn(1)] }); - assert.equal(seed.status, 200); - assert.ok(seed.appliedSeq !== undefined && seed.appliedSeq >= 1, "diverted turn carries x-bili-applied-seq"); - const upstream = h.forwarded.at(-1) as { model?: string; max_tokens?: number; messages: { role: string }[] }; - assert.equal(upstream.model, "claude-test", "assembled body is a real anthropic request"); - assert.equal(upstream.max_tokens, 512); - assert.equal(upstream.messages.length, 1); - // delta through the same ordinary path - const delta = await h.markerTurn("/v1/messages", { conversationId: "conv-marker-1", sinceSeq: seed.appliedSeq ?? 1, request: { max_tokens: 512 }, messages: [assistantTurn(1), userTurn(2)] }); - assert.equal(delta.status, 200); - const upstream2 = h.forwarded.at(-1) as { messages: { role: string }[] }; - assert.equal(upstream2.messages.length, 3, "delta was assembled onto the canonical prefix"); - assert.deepEqual(upstream2.messages.map((m: { role: string }) => m.role), ["user", "assistant", "user"]); - } finally { - delete process.env.BILI_ROLLOUT_PLUGIN_INCREMENTAL_TURNS; - await h?.close(); - } -}); - -test("incremental marker divert: marker on a non-incremental body rides the normal pipeline unchanged", async () => { - process.env.BILI_ROLLOUT_PLUGIN_INCREMENTAL_TURNS = "1"; - let h: Awaited> | undefined; - try { - h = await harness("full"); - // an ordinary anthropic body WITH the marker: shape is the final word. - // The normal pipeline re-serializes anthropic bodies (content may come - // back as blocks), so assert semantics, not byte identity: model rides, - // one message, text preserved, and no incremental artifacts leak - // upstream (conversationId/sinceSeq must be absent). - const res = await h.markerRaw("/v1/messages", { model: "claude-test", max_tokens: 512, messages: [{ role: "user", content: "plain turn with marker" }] }); - assert.equal(res.status, 200); - const upstream = h.forwarded.at(-1) as { model?: string; conversationId?: string; sinceSeq?: number; messages: { role: string; content: unknown }[] }; - assert.equal(upstream.model, "claude-test"); - assert.equal(upstream.messages.length, 1); - assert.equal("conversationId" in upstream, false, "not diverted"); - assert.equal("sinceSeq" in upstream, false, "not diverted"); - const c = upstream.messages[0].content; - const text = typeof c === "string" ? c : Array.isArray(c) ? c.map((p) => (p && typeof p === "object" && "text" in p ? String((p as { text: unknown }).text) : "")).join("") : String(c); - // The ordinary pipeline stamps the hidden ref on the first user - // turn — exactly what a marker-less request would carry too. - assert.ok(text.endsWith("plain turn with marker"), `text rides the normal pipeline (got: ${JSON.stringify(text.slice(0, 60))}…)`); - } finally { - delete process.env.BILI_ROLLOUT_PLUGIN_INCREMENTAL_TURNS; - await h?.close(); - } -}); - -test("incremental marker divert: rollout off answers 503 on shaped turns only", async () => { - const h = await harness("full"); - try { - const shaped = await h.markerTurn("/v1/messages", { conversationId: "conv-marker-off", sinceSeq: 0, request: { max_tokens: 512 }, messages: [userTurn(1)] }); - assert.equal(shaped.status, 503); - assert.match(String((shaped.body as { error?: string }).error), /pluginIncrementalTurns/); - // a plain body with the marker still rides the normal pipeline - const plain = await h.markerRaw("/v1/messages", { model: "claude-test", max_tokens: 512, messages: [{ role: "user", content: "plain while off" }] }); - assert.equal(plain.status, 200); - } finally { - await h.close(); - } -}); - -test("incremental marker divert: system-only change rides an empty delta — canonical unchanged, sticky system updates", async () => { - process.env.BILI_ROLLOUT_PLUGIN_INCREMENTAL_TURNS = "1"; - let h: Awaited> | undefined; - const sysText = (v: unknown): string => typeof v === "string" ? v : Array.isArray(v) ? v.map((b) => (b as { text?: string })?.text ?? "").join("") : String(v); - try { - h = await harness("full"); - const seed = await h.markerTurn("/v1/messages", { conversationId: "conv-marker-sys", sinceSeq: 0, system: "SYSTEM-A", request: { max_tokens: 512 }, messages: [userTurn(1)] }); - assert.equal(seed.status, 200); - assert.equal(seed.appliedSeq, 1); - // Same message set, different system → empty delta: the canonical must - // not extend, and the assembled request carries the new sticky system. - const sysOnly = await h.markerTurn("/v1/messages", { conversationId: "conv-marker-sys", sinceSeq: seed.appliedSeq ?? 1, system: "SYSTEM-B", request: { max_tokens: 512 }, messages: [] }); - assert.equal(sysOnly.status, 200); - assert.equal(sysOnly.appliedSeq, seed.appliedSeq, "a pure system update does not extend the canonical"); - const upstream = h.forwarded.at(-1) as { system?: unknown; messages: { role: string }[] }; - assert.equal(upstream.messages.length, 1, "history unchanged by the system-only turn"); - const sys = sysText(upstream.system); - assert.ok(sys.startsWith("SYSTEM-B"), `assembled request carries the new system: ${sys.slice(0, 40)}`); - assert.ok(!sys.includes("SYSTEM-A"), "the old system is fully replaced"); - // An empty delta WITHOUT a system update stays a client error. - const bareEmpty = await h.markerTurn("/v1/messages", { conversationId: "conv-marker-sys", sinceSeq: seed.appliedSeq ?? 1, request: { max_tokens: 512 }, messages: [] }); - assert.equal(bareEmpty.status, 400); - } finally { - delete process.env.BILI_ROLLOUT_PLUGIN_INCREMENTAL_TURNS; - await h?.close(); - } -}); - -for (const scenario of ["append", "system-only", "edit", "forced resync", "fresh client"] as const) { - test(`incremental client: removed system clears the upstream on ${scenario}`, async () => { - const { __resetIncrementalClientForTests, planIncrementalTurn, noteIncrementalAck } = await import("../src/agent/incremental-client.js"); - const priorRollout = process.env.BILI_ROLLOUT_PLUGIN_INCREMENTAL_TURNS; - process.env.BILI_ROLLOUT_PLUGIN_INCREMENTAL_TURNS = "1"; - __resetIncrementalClientForTests(); - let h: Harness | undefined; - try { - h = await harness("full"); - const conversationId = "conv-system-clear"; - const original = [{ role: "user", content: "original request" }]; - const seed = planIncrementalTurn({ conversationId, messages: original, system: "OLD-SYSTEM-INSTRUCTION" }); - assert.notEqual(seed.mode, "skip"); - if (seed.mode === "skip") return; - const seeded = await h.markerTurn(h.path, JSON.parse(seed.body)); - assert.equal(seeded.status, 200); - assert.equal(seeded.appliedSeq, original.length); - assert.ok(JSON.stringify(h.forwarded.at(-1)?.system).includes("OLD-SYSTEM-INSTRUCTION")); - noteIncrementalAck(conversationId, original.length, original, "OLD-SYSTEM-INSTRUCTION"); - if (scenario === "fresh client") __resetIncrementalClientForTests(); - const messages = scenario === "append" - ? [...original, { role: "assistant", content: "answer" }, { role: "user", content: "next request" }] - : scenario === "edit" ? [{ role: "user", content: "edited request" }] : original; - const plan = planIncrementalTurn({ conversationId, messages, system: undefined }, scenario === "forced resync"); - assert.equal(plan.mode, scenario === "fresh client" ? "seed" : scenario === "edit" || scenario === "forced resync" ? "resync" : "delta"); - const cleared = await h.markerTurn(h.path, JSON.parse(plan.body)); - assert.equal(cleared.status, 200); - assert.equal(cleared.appliedSeq, messages.length); - assert.equal(JSON.stringify(h.forwarded.at(-1)?.system ?? "").includes("OLD-SYSTEM-INSTRUCTION"), false, "removed original system must not be forwarded"); - noteIncrementalAck(conversationId, messages.length, messages, undefined); - const next = planIncrementalTurn({ conversationId, messages: [...messages, { role: "user", content: "another request" }], system: undefined }); - assert.equal(next.mode, "delta"); - if (next.mode !== "delta") return; - assert.equal(Object.hasOwn(JSON.parse(next.body), "system"), false, "acknowledged absence stays omitted on subsequent deltas"); - const followed = await h.markerTurn(h.path, JSON.parse(next.body)); - assert.equal(followed.status, 200); - assert.equal(JSON.stringify(h.forwarded.at(-1)?.system ?? "").includes("OLD-SYSTEM-INSTRUCTION"), false); - } finally { - if (priorRollout === undefined) delete process.env.BILI_ROLLOUT_PLUGIN_INCREMENTAL_TURNS; - else process.env.BILI_ROLLOUT_PLUGIN_INCREMENTAL_TURNS = priorRollout; - __resetIncrementalClientForTests(); - await h?.close(); - } - }); -} - -// --- client helper state machine (src/agent/incremental-client.ts) ------------------ - -test("incremental client helper: seed implies resync, delta slices, edits resync, sticky system", async () => { - const { __resetIncrementalClientForTests, planIncrementalTurn, noteIncrementalAck, markIncrementalDead } = await import("../src/agent/incremental-client.js"); - __resetIncrementalClientForTests(); - const conv = "conv-helper-1"; - const msgs1 = [{ role: "user", content: "hello there" }]; - const seed = planIncrementalTurn({ conversationId: conv, messages: msgs1, system: "be terse" }); - assert.equal(seed.mode, "seed"); - const seedBody = JSON.parse(seed.body) as { sinceSeq: number; resync: boolean; messages: unknown[]; system?: string }; - assert.equal(seedBody.sinceSeq, 0); - assert.equal(seedBody.resync, true, "seed never 409s — replacement semantics"); - assert.equal(seedBody.system, "be terse"); - - // ack seq 1 → next turn sends only the new tail - noteIncrementalAck(conv, 1, msgs1, "be terse"); - const msgs2 = [...msgs1, { role: "assistant", content: "hi" }, { role: "user", content: "and now?" }]; - const delta = planIncrementalTurn({ conversationId: conv, messages: msgs2, system: "be terse" }); - assert.equal(delta.mode, "delta"); - const deltaBody = JSON.parse(delta.body) as { sinceSeq: number; messages: unknown[]; system?: string }; - assert.equal(deltaBody.sinceSeq, 1); - assert.equal(deltaBody.messages.length, 2, "only the messages added since the ack"); - assert.equal(deltaBody.system, undefined, "sticky system omitted when unchanged"); - - // edited prefix → resync with the full history - const edited = [{ role: "user", content: "hello there (edited)" }, ...msgs2.slice(1)]; - const rs = planIncrementalTurn({ conversationId: conv, messages: edited, system: "be terse" }); - assert.equal(rs.mode, "resync"); - const rsBody = JSON.parse(rs.body) as { sinceSeq: number; resync: boolean; messages: unknown[] }; - assert.equal(rsBody.resync, true); - assert.equal(rsBody.messages.length, edited.length); - - // system change with an equal message set rides an EMPTY delta — the - // canonical is unchanged, the sticky system updates server-side - noteIncrementalAck(conv, edited.length, edited, "be terse"); - const sysChange = planIncrementalTurn({ conversationId: conv, messages: edited, system: "be verbose" }); - assert.equal(sysChange.mode, "delta"); - const sysBody = JSON.parse(sysChange.body) as { sinceSeq: number; messages: unknown[]; system?: string }; - assert.equal(sysBody.messages.length, 0, "pure sticky-system update — empty delta"); - assert.equal(sysBody.system, "be verbose"); - - // dead conversation falls back to skip until re-probed/reset - markIncrementalDead(conv); - const dead = planIncrementalTurn({ conversationId: conv, messages: edited, system: "be verbose" }); - assert.equal(dead.mode, "skip", "dead conversation rides full resend (host's own path)"); - __resetIncrementalClientForTests(); -}); - -test("wireMessagesLookAnthropic: anthropic shapes pass, OpenAI-tool shapes fall back to full resend", async () => { - const { wireMessagesLookAnthropic } = await import("../src/agent/incremental-client.js"); - assert.equal(wireMessagesLookAnthropic([{ role: "user", content: "hi" }]), true, "string content"); - assert.equal(wireMessagesLookAnthropic([{ role: "user", content: [{ type: "text", text: "hi" }] }]), true, "block content"); - assert.equal(wireMessagesLookAnthropic([{ role: "assistant", content: [{ type: "tool_use", id: "t", name: "x", input: {} }] }, { role: "user", content: [{ type: "tool_result", tool_use_id: "t", content: "ok" }] }]), true, "anthropic tool blocks are native wire shape"); - assert.equal(wireMessagesLookAnthropic([]), false, "empty list is not a conversation"); - assert.equal(wireMessagesLookAnthropic("nope"), false); - assert.equal(wireMessagesLookAnthropic([{ role: "user", content: "hi" }, { role: "tool", content: "out" }]), false, "OpenAI tool role is not convertible"); - assert.equal(wireMessagesLookAnthropic([{ role: "assistant", content: "x", tool_calls: [{ id: "c", type: "function", function: { name: "f" } }] }]), false, "tool_calls cannot ride anthropicToCore"); - assert.equal(wireMessagesLookAnthropic([{ role: "user", content: "hi", tool_call_id: "c" }]), false); - assert.equal(wireMessagesLookAnthropic([{ role: "user", content: 42 }]), false, "content must be string or array"); -}); - -test("incrementalTurnBodyShape accepts the incremental payload and rejects anthropic/garbage", async () => { - const { incrementalTurnBodyShape } = await import("../src/incremental-lane.js"); - const inc = Buffer.from(JSON.stringify({ conversationId: "c1", sinceSeq: 0, request: {}, messages: [] })); - assert.equal(incrementalTurnBodyShape(inc), true); - const anthropic = Buffer.from(JSON.stringify({ model: "claude-test", max_tokens: 64, messages: [] })); - assert.equal(incrementalTurnBodyShape(anthropic), false, "top-level model = ordinary request"); - const garbage = Buffer.from("not json at all"); - assert.equal(incrementalTurnBodyShape(garbage), false); - const badSeq = Buffer.from(JSON.stringify({ conversationId: "c1", sinceSeq: "3", messages: [] })); - assert.equal(incrementalTurnBodyShape(badSeq), false, "sinceSeq must be an integer"); -}); +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import test from "node:test"; +import { mkdtempSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { defaultConfig } from "acp-kernel"; +import { startServer } from "../src/server.ts"; +import type { ProxyOptions } from "../src/config.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { _resetPluginStateForTest } from "../src/plugin.ts"; +import { _resetSessionsForTest, peekSession } from "../src/session.ts"; +import { _setForTest } from "../src/registry.ts"; +import { resetToolRingForTest } from "../src/tool-ring.ts"; +import { __resetIncrementalLaneForTests } from "../src/incremental-lane.ts"; + +process.env.NODE_ENV = "test"; +const testRoot = mkdtempSync(join(tmpdir(), "bili-incr-")); +test.after(() => rmSync(testRoot, { recursive: true, force: true })); + +interface Turn { + sinceSeq: number; + messages: unknown[]; + resync?: boolean; + system?: string; + request?: Record; +} + +interface Harness { + proxyUrl: string; + path: string; + turn: (t: Turn) => Promise<{ status: number; headers: Headers; body: unknown }>; + probe: () => Promise<{ status: number; body: unknown }>; + markerTurn: (reqPath: string, payload: { conversationId: string; sinceSeq: number; resync?: boolean; system?: unknown; request?: Record; messages: unknown[] }) => Promise<{ status: number; body: unknown; appliedSeq?: number }>; + markerRaw: (reqPath: string, anthropicBody: Record) => Promise<{ status: number; body: unknown }>; + forwarded: Record[]; + dir: string; + close: () => Promise; +} + +async function harness(storageMode: "full" | "legacy", opts: { upstreamDelayMs?: number; upstreamStatus?: number } = {}): Promise { + const dir = mkdtempSync(join(testRoot, "run-")); + for (const key of ["XDG_STATE_HOME", "XDG_DATA_HOME", "XDG_CACHE_HOME", "XDG_CONFIG_HOME"]) process.env[key] = dir; + process.env.BILI_STORAGE_UNIFIED = storageMode === "full" ? "full" : "legacy"; + _resetSessionsForTest(); + _resetPluginStateForTest(); + resetToolRingForTest(); + __resetIncrementalLaneForTests(); + const store = new SessionStore({ dir: dir + "/sessions", enabled: true, debounceMs: 60000 }); + _setStoreForTest(store); + _setForTest({}); + const forwarded: Record[] = []; + const upstream = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (chunk: Buffer) => chunks.push(chunk)); + req.on("end", () => { + forwarded.push(JSON.parse(Buffer.concat(chunks).toString("utf8")) as Record); + const reply = (): void => { + res.writeHead(opts.upstreamStatus ?? 200, { "content-type": "application/json" }); + res.end(JSON.stringify({ id: "msg_test", role: "assistant", content: [{ type: "text", text: "answer" }], usage: { input_tokens: 10000, output_tokens: 10 } })); + }; + if (opts.upstreamDelayMs) setTimeout(reply, opts.upstreamDelayMs); + else reply(); + }); + }); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const addr = upstream.address(); + assert(addr && typeof addr === "object"); + const upstreamUrl = `http://127.0.0.1:${addr.port}`; + const proxy = await startServer({ port: 0, host: "127.0.0.1", upstream: upstreamUrl, routes: { [upstreamUrl]: { models: { "claude-test": { context: 400000 } } } }, modelContextLimit: 400000, kernelConfig: defaultConfig(400000), compress: { injectTool: true, injectNudge: false }, promptCache: { routing: "auto" }, sessionHeader: "x-acp-session", log: false, debug: false, passthrough: false, autoUpdate: false, mitm: { enabled: false, domains: [] } } as ProxyOptions); + if (!(proxy as unknown as { listening?: boolean }).listening) await once(proxy, "listening"); + const proxyUrl = `http://127.0.0.1:${(proxy.address() as { port: number }).port}`; + const path = "/v1/messages"; + const turn = async (t: Turn) => { + const response = await fetch(`${proxyUrl}/__bili/plugin/turn`, { + method: "POST", + headers: { "content-type": "application/json", "x-bili-plugin": "dsh", "x-bili-plugin-conversation": "conv-inc-1" }, + body: JSON.stringify({ conversationId: "conv-inc-1", sinceSeq: t.sinceSeq, ...(t.resync ? { resync: true } : {}), ...(t.system !== undefined ? { system: t.system } : {}), path, request: { model: "claude-test", max_tokens: 1024, ...(t.request ?? {}) }, messages: t.messages }), + }); + const body: unknown = await response.json().catch(() => null); + return { status: response.status, headers: response.headers, body }; + }; + // store handle must be released before the teardown rm -rf: Windows cannot + // unlink an open index.db (EBUSY) — same seam the storage-unified suite uses. + const probe = async () => { + const response = await fetch(`${proxyUrl}/__bili/plugin/turn`, { method: "GET", headers: { "x-bili-plugin": "dsh" } }); + const body: unknown = await response.json().catch(() => null); + return { status: response.status, body }; + }; + // #3248 marker divert: shaped incremental body posted on the ORDINARY model path + const markerTurn = async (reqPath: string, payload: { conversationId: string; sinceSeq: number; resync?: boolean; system?: unknown; request?: Record; messages: unknown[] }) => { + const response = await fetch(`${proxyUrl}${reqPath}`, { + method: "POST", + headers: { "content-type": "application/json", "x-bili-plugin": "dsh", "x-bili-plugin-conversation": payload.conversationId, "x-acp-session": payload.conversationId, "x-bili-plugin-turn": "1" }, + body: JSON.stringify({ conversationId: payload.conversationId, sinceSeq: payload.sinceSeq, ...(payload.resync ? { resync: true } : {}), ...(payload.system !== undefined ? { system: payload.system } : {}), request: { model: "claude-test", max_tokens: 1024, ...(payload.request ?? {}) }, messages: payload.messages }), + }); + const body: unknown = await response.json().catch(() => null); + const applied = response.headers.get("x-bili-applied-seq"); + return { status: response.status, body, appliedSeq: applied !== null && /^\d+$/.test(applied) ? Number(applied) : undefined }; + }; + // marker on an ordinary anthropic body: must ride the normal pipeline unchanged + const markerRaw = async (reqPath: string, anthropicBody: Record) => { + const response = await fetch(`${proxyUrl}${reqPath}`, { method: "POST", headers: { "content-type": "application/json", "x-bili-plugin": "dsh", "x-acp-session": "conv-marker-plain", "x-bili-plugin-turn": "1" }, body: JSON.stringify(anthropicBody) }); + const body: unknown = await response.json().catch(() => null); + return { status: response.status, body }; + }; + return { proxyUrl, path, turn, probe, markerTurn, markerRaw, forwarded, dir, close: async () => { await new Promise((resolve) => proxy.close(() => resolve())); store.closeQuietly(); await new Promise((resolve) => upstream.close(() => resolve())); } }; +} + +const userTurn = (n: number, text = "hello") => ({ id: `u${n}`, role: "user", contentType: "text", content: text.repeat(400) }); +const assistantTurn = (n: number) => ({ id: `a${n}`, role: "assistant", contentType: "text", content: `model answer ${n}` }); + +test("incremental lane: seed → delta → upstream receives the assembled full history; ack advances", async () => { + const h = await harness("full"); + try { + const seed = await h.turn({ sinceSeq: 0, messages: [userTurn(1)] }); + assert.equal(seed.status, 200); + assert.equal(seed.headers.get("x-bili-applied-seq"), "1"); + const upstream1 = h.forwarded.at(-1) as { system?: unknown; messages: { role: string; content: string }[] }; + assert.ok(Array.isArray(upstream1.messages) && upstream1.messages.length === 1); + assert.equal(upstream1.messages[0].role, "user"); + + // Delta: everything after ack — the previous assistant reply + the new user turn. + const delta = await h.turn({ sinceSeq: 1, messages: [assistantTurn(1), userTurn(2, "second question")] }); + assert.equal(delta.status, 200); + assert.equal(delta.headers.get("x-bili-applied-seq"), "3"); + const upstream2 = h.forwarded.at(-1) as { messages: { role: string }[] }; + assert.equal(upstream2.messages.length, 3, "assembled history = seed + delta"); + assert.deepEqual(upstream2.messages.map((m) => m.role), ["user", "assistant", "user"]); + } finally { + await h.close(); + } +}); + +test("incremental lane: a failed turn (non-2xx) carries NO x-bili-applied-seq (#3344)", async () => { + const h = await harness("full", { upstreamStatus: 500 }); + try { + const failed = await h.turn({ sinceSeq: 0, messages: [userTurn(1)] }); + assert.ok(failed.status >= 400, `expected a non-2xx failure from the failing upstream, got ${failed.status}`); + // The ack must not ride a failure: a host trusting it unconditionally + // would skip its resend and lose the user's message (#3344). + assert.equal(failed.headers.get("x-bili-applied-seq"), null); + } finally { + await h.close(); + } +}); + +test("incremental lane: seq mismatch answers 409 with the server's seq; resync recovers", async () => { + const h = await harness("full"); + try { + const seed = await h.turn({ sinceSeq: 0, messages: [userTurn(1)] }); + assert.equal(seed.status, 200); + // Client guesses a stale seq. + const stale = await h.turn({ sinceSeq: 0, messages: [assistantTurn(1), userTurn(2)] }); + assert.equal(stale.status, 409); + assert.equal((stale.body as { error?: string }).error, "seq-mismatch"); + assert.equal((stale.body as { serverSeq?: number }).serverSeq, 1); + // Resync with the full history re-seeds the canonical. + const recovered = await h.turn({ sinceSeq: 0, resync: true, messages: [userTurn(1), assistantTurn(1), userTurn(2)] }); + assert.equal(recovered.status, 200); + assert.equal(recovered.headers.get("x-bili-applied-seq"), "3"); + const upstream = h.forwarded.at(-1) as { messages: { role: string }[] }; + assert.deepEqual(upstream.messages.map((m) => m.role), ["user", "assistant", "user"]); + } finally { + await h.close(); + } +}); + +test("incremental lane: cold rebuild after restart serves the next delta from the unified table", async () => { + const h = await harness("full"); + try { + const seed = await h.turn({ sinceSeq: 0, messages: [userTurn(1)] }); + assert.equal(seed.status, 200); + // Simulate a process restart: session pool + warm caches dropped. The + // canonical prefix must rebuild from the unified messages table. + _resetSessionsForTest(); + __resetIncrementalLaneForTests(); + const delta = await h.turn({ sinceSeq: 1, messages: [assistantTurn(1), userTurn(2, "after restart")] }); + assert.equal(delta.status, 200); + const upstream = h.forwarded.at(-1) as { messages: { role: string }[] }; + assert.deepEqual(upstream.messages.map((m) => m.role), ["user", "assistant", "user"], "table-rebuilt canonical + delta"); + } finally { + await h.close(); + } +}); + +test("incremental lane: divergent resync survives a cold restart without a 409 loop", async () => { + const h = await harness("full"); + try { + const seed = await h.turn({ sinceSeq: 0, messages: [userTurn(1, "old branch one"), assistantTurn(1), userTurn(2, "old branch two")] }); + assert.equal(seed.status, 200); + // The client rewinds to an edited branch (divergent content, ids and all). + const resync = await h.turn({ sinceSeq: 0, resync: true, messages: [userTurn(1, "edited branch")] }); + assert.equal(resync.status, 200); + assert.equal(resync.headers.get("x-bili-applied-seq"), "1"); + // Cold restart: the canonical must rebuild from the CURRENT branch + // only — the rejected branch's rows stay in the table (append-only) + // but the served history slices past them (baseSeq epoch). + _resetSessionsForTest(); + __resetIncrementalLaneForTests(); + const delta = await h.turn({ sinceSeq: 1, messages: [assistantTurn(1), userTurn(2, "new branch tail")] }); + assert.equal(delta.status, 200); + assert.equal(delta.headers.get("x-bili-applied-seq"), "3"); + const upstream = h.forwarded.at(-1) as { messages: { role: string; content: unknown }[] }; + assert.equal(upstream.messages.length, 3, "served history is the edited branch, not the row union"); + const first = JSON.stringify(upstream.messages[0].content); + assert.ok(first.includes("edited branch"), `first message is the resynced one: ${first.slice(0, 80)}`); + assert.ok(!JSON.stringify(upstream.messages).includes("old branch"), "old branch rows do not leak into the canonical"); + } finally { + await h.close(); + } +}); + +test("incremental lane: sessions share the plugin conversation id — incremental and normal traffic interop", async () => { + const h = await harness("full"); + try { + const seed = await h.turn({ sinceSeq: 0, messages: [userTurn(1)] }); + assert.equal(seed.status, 200); + const session = peekSession("conv-inc-1"); + assert.ok(session, "session id = the plugin conversation id (same as normal plugin traffic)"); + assert.equal(session.stats.requests, 1); + } finally { + await h.close(); + } +}); + +test("incremental lane: sticky system — omitted system keeps the last one, a new value replaces it", async () => { + const h = await harness("full"); + // The assembled body rides the normal pipeline, which renders the system + // prompt as anthropic blocks (with its cache marks) — compare by text. + const sysText = (v: unknown): string => typeof v === "string" ? v : Array.isArray(v) ? v.map((b) => (b as { text?: string })?.text ?? "").join("") : String(v); + try { + await h.turn({ sinceSeq: 0, system: "SYSTEM-A", messages: [userTurn(1)] }); + const upstream1 = sysText((h.forwarded.at(-1) as { system?: unknown }).system); + assert.ok(upstream1.startsWith("SYSTEM-A"), `turn 1 carries the client system: ${upstream1.slice(0, 40)}`); + // Omitted → sticky. + await h.turn({ sinceSeq: 1, messages: [assistantTurn(1), userTurn(2)] }); + const upstream2 = sysText((h.forwarded.at(-1) as { system?: unknown }).system); + assert.ok(upstream2.startsWith("SYSTEM-A"), "omitted system keeps the last one"); + // Explicit new value replaces. + await h.turn({ sinceSeq: 3, system: "SYSTEM-B", messages: [assistantTurn(2), userTurn(3)] }); + const upstream3 = sysText((h.forwarded.at(-1) as { system?: unknown }).system); + assert.ok(upstream3.startsWith("SYSTEM-B"), "explicit system replaces"); + assert.ok(!upstream3.includes("SYSTEM-A"), "the old system is fully replaced"); + } finally { + await h.close(); + } +}); + +test("incremental lane: a second concurrent turn on the same conversation answers 409 turn-in-flight", async () => { + // Margins sized for loaded runners: the settle sleep must beat the first + // request's path into inFlight, and the upstream delay must outlive it — + // 40ms/150ms flaked (review of #3253). + const h = await harness("full", { upstreamDelayMs: 800 }); + try { + const first = h.turn({ sinceSeq: 0, messages: [userTurn(1)] }); + await new Promise((r) => setTimeout(r, 120)); // let the first enter forward + const second = await h.turn({ sinceSeq: 1, messages: [assistantTurn(1), userTurn(2)] }); + assert.equal(second.status, 409); + assert.equal((second.body as { error?: string }).error, "turn-in-flight"); + assert.equal((await first).status, 200); + } finally { + await h.close(); + } +}); + +test("incremental lane: request params (tools/max_tokens) ride the assembled body to the upstream", async () => { + const h = await harness("full"); + try { + const tools = [{ name: "get_weather", description: "w", input_schema: { type: "object", properties: {} } }]; + await h.turn({ sinceSeq: 0, request: { max_tokens: 777, tools }, messages: [userTurn(1)] }); + const upstream = h.forwarded.at(-1) as { max_tokens?: number; tools?: unknown; model?: string }; + assert.equal(upstream.max_tokens, 777); + assert.equal(upstream.model, "claude-test"); + assert.ok(Array.isArray(upstream.tools)); + // Disallowed request params never ride. + await h.turn({ sinceSeq: 1, request: { max_tokens: 512, messages: [{ role: "user", content: "smuggled" }] } as never, messages: [assistantTurn(1), userTurn(2)] }); + const upstream2 = h.forwarded.at(-1) as { messages: { role: string }[] }; + assert.equal(upstream2.messages.length, 3, "request.messages is ignored — the lane owns the message list"); + } finally { + await h.close(); + } +}); + +test("incremental lane: a Vertex turn keeps anthropic_version and still drops an unlisted field", async () => { + const h = await harness("full"); + try { + // Anthropic-on-Vertex has no `model` in the body — `anthropic_version` + // carries the wire revision instead (live capture 2026-10-15). The + // allowlist is the only thing that could drop it on the way out. + await h.turn({ sinceSeq: 0, request: { anthropic_version: "vertex-2023-10-16" }, messages: [userTurn(1)] }); + const upstream = h.forwarded.at(-1) as { anthropic_version?: string; smuggled?: unknown }; + assert.equal(upstream.anthropic_version, "vertex-2023-10-16"); + // The allowlist is still a whitelist: an unlisted key must not ride. + await h.turn({ sinceSeq: 1, request: { anthropic_version: "vertex-2023-10-16", smuggled: true } as never, messages: [assistantTurn(1), userTurn(2)] }); + const upstream2 = h.forwarded.at(-1) as { anthropic_version?: string; smuggled?: unknown }; + assert.equal(upstream2.anthropic_version, "vertex-2023-10-16"); + assert.equal(upstream2.smuggled, undefined); + } finally { + await h.close(); + } +}); + +test("incremental lane: legacy-only storage answers 503 with the enablement hint", async () => { + const h = await harness("legacy"); + try { + const seed = await h.turn({ sinceSeq: 0, messages: [userTurn(1)] }); + assert.equal(seed.status, 503); + assert.match(String((seed.body as { error?: string }).error), /storage\.unified/); + } finally { + await h.close(); + } +}); + +// --- marker divert (#3248 phase 2) ------------------------------------------------- +// The host keeps its ordinary provider URL and stamps `x-bili-plugin-turn: 1`; +// the body shape is the final word (non-incremental bodies ride the normal +// pipeline byte-identically). + +test("incremental marker divert: probe reports the rollout state", async () => { + const h = await harness("full"); + try { + const off = await h.probe(); + assert.equal(off.status, 200); + const offBody = off.body as { ok: boolean; enabled: boolean; feature: string; protocol: number }; + assert.equal(offBody.ok, true); + assert.equal(offBody.feature, "pluginIncrementalTurns"); + assert.equal(offBody.protocol, 1); + assert.equal(offBody.enabled, false, "default rollout percent is 0 — probe must report disabled"); + process.env.BILI_ROLLOUT_PLUGIN_INCREMENTAL_TURNS = "1"; + try { + const on = await h.probe(); + assert.equal(on.status, 200); + assert.equal((on.body as { enabled: boolean }).enabled, true, "env force flips the probe"); + } finally { + delete process.env.BILI_ROLLOUT_PLUGIN_INCREMENTAL_TURNS; + } + } finally { + await h.close(); + } +}); + +test("incremental marker divert: shaped body on the ordinary model path is diverted and assembled", async () => { + process.env.BILI_ROLLOUT_PLUGIN_INCREMENTAL_TURNS = "1"; + let h: Awaited> | undefined; + try { + h = await harness("full"); + const seed = await h.markerTurn("/v1/messages", { conversationId: "conv-marker-1", sinceSeq: 0, request: { max_tokens: 512 }, messages: [userTurn(1)] }); + assert.equal(seed.status, 200); + assert.ok(seed.appliedSeq !== undefined && seed.appliedSeq >= 1, "diverted turn carries x-bili-applied-seq"); + const upstream = h.forwarded.at(-1) as { model?: string; max_tokens?: number; messages: { role: string }[] }; + assert.equal(upstream.model, "claude-test", "assembled body is a real anthropic request"); + assert.equal(upstream.max_tokens, 512); + assert.equal(upstream.messages.length, 1); + // delta through the same ordinary path + const delta = await h.markerTurn("/v1/messages", { conversationId: "conv-marker-1", sinceSeq: seed.appliedSeq ?? 1, request: { max_tokens: 512 }, messages: [assistantTurn(1), userTurn(2)] }); + assert.equal(delta.status, 200); + const upstream2 = h.forwarded.at(-1) as { messages: { role: string }[] }; + assert.equal(upstream2.messages.length, 3, "delta was assembled onto the canonical prefix"); + assert.deepEqual(upstream2.messages.map((m: { role: string }) => m.role), ["user", "assistant", "user"]); + } finally { + delete process.env.BILI_ROLLOUT_PLUGIN_INCREMENTAL_TURNS; + await h?.close(); + } +}); + +test("incremental marker divert: marker on a non-incremental body rides the normal pipeline unchanged", async () => { + process.env.BILI_ROLLOUT_PLUGIN_INCREMENTAL_TURNS = "1"; + let h: Awaited> | undefined; + try { + h = await harness("full"); + // an ordinary anthropic body WITH the marker: shape is the final word. + // The normal pipeline re-serializes anthropic bodies (content may come + // back as blocks), so assert semantics, not byte identity: model rides, + // one message, text preserved, and no incremental artifacts leak + // upstream (conversationId/sinceSeq must be absent). + const res = await h.markerRaw("/v1/messages", { model: "claude-test", max_tokens: 512, messages: [{ role: "user", content: "plain turn with marker" }] }); + assert.equal(res.status, 200); + const upstream = h.forwarded.at(-1) as { model?: string; conversationId?: string; sinceSeq?: number; messages: { role: string; content: unknown }[] }; + assert.equal(upstream.model, "claude-test"); + assert.equal(upstream.messages.length, 1); + assert.equal("conversationId" in upstream, false, "not diverted"); + assert.equal("sinceSeq" in upstream, false, "not diverted"); + const c = upstream.messages[0].content; + const text = typeof c === "string" ? c : Array.isArray(c) ? c.map((p) => (p && typeof p === "object" && "text" in p ? String((p as { text: unknown }).text) : "")).join("") : String(c); + // The ordinary pipeline stamps the hidden ref on the first user + // turn — exactly what a marker-less request would carry too. + assert.ok(text.endsWith("plain turn with marker"), `text rides the normal pipeline (got: ${JSON.stringify(text.slice(0, 60))}…)`); + } finally { + delete process.env.BILI_ROLLOUT_PLUGIN_INCREMENTAL_TURNS; + await h?.close(); + } +}); + +test("incremental marker divert: rollout off answers 503 on shaped turns only", async () => { + const h = await harness("full"); + try { + const shaped = await h.markerTurn("/v1/messages", { conversationId: "conv-marker-off", sinceSeq: 0, request: { max_tokens: 512 }, messages: [userTurn(1)] }); + assert.equal(shaped.status, 503); + assert.match(String((shaped.body as { error?: string }).error), /pluginIncrementalTurns/); + // a plain body with the marker still rides the normal pipeline + const plain = await h.markerRaw("/v1/messages", { model: "claude-test", max_tokens: 512, messages: [{ role: "user", content: "plain while off" }] }); + assert.equal(plain.status, 200); + } finally { + await h.close(); + } +}); + +test("incremental marker divert: system-only change rides an empty delta — canonical unchanged, sticky system updates", async () => { + process.env.BILI_ROLLOUT_PLUGIN_INCREMENTAL_TURNS = "1"; + let h: Awaited> | undefined; + const sysText = (v: unknown): string => typeof v === "string" ? v : Array.isArray(v) ? v.map((b) => (b as { text?: string })?.text ?? "").join("") : String(v); + try { + h = await harness("full"); + const seed = await h.markerTurn("/v1/messages", { conversationId: "conv-marker-sys", sinceSeq: 0, system: "SYSTEM-A", request: { max_tokens: 512 }, messages: [userTurn(1)] }); + assert.equal(seed.status, 200); + assert.equal(seed.appliedSeq, 1); + // Same message set, different system → empty delta: the canonical must + // not extend, and the assembled request carries the new sticky system. + const sysOnly = await h.markerTurn("/v1/messages", { conversationId: "conv-marker-sys", sinceSeq: seed.appliedSeq ?? 1, system: "SYSTEM-B", request: { max_tokens: 512 }, messages: [] }); + assert.equal(sysOnly.status, 200); + assert.equal(sysOnly.appliedSeq, seed.appliedSeq, "a pure system update does not extend the canonical"); + const upstream = h.forwarded.at(-1) as { system?: unknown; messages: { role: string }[] }; + assert.equal(upstream.messages.length, 1, "history unchanged by the system-only turn"); + const sys = sysText(upstream.system); + assert.ok(sys.startsWith("SYSTEM-B"), `assembled request carries the new system: ${sys.slice(0, 40)}`); + assert.ok(!sys.includes("SYSTEM-A"), "the old system is fully replaced"); + // An empty delta WITHOUT a system update stays a client error. + const bareEmpty = await h.markerTurn("/v1/messages", { conversationId: "conv-marker-sys", sinceSeq: seed.appliedSeq ?? 1, request: { max_tokens: 512 }, messages: [] }); + assert.equal(bareEmpty.status, 400); + } finally { + delete process.env.BILI_ROLLOUT_PLUGIN_INCREMENTAL_TURNS; + await h?.close(); + } +}); + +for (const scenario of ["append", "system-only", "edit", "forced resync", "fresh client"] as const) { + test(`incremental client: removed system clears the upstream on ${scenario}`, async () => { + const { __resetIncrementalClientForTests, planIncrementalTurn, noteIncrementalAck } = await import("../src/agent/incremental-client.js"); + const priorRollout = process.env.BILI_ROLLOUT_PLUGIN_INCREMENTAL_TURNS; + process.env.BILI_ROLLOUT_PLUGIN_INCREMENTAL_TURNS = "1"; + __resetIncrementalClientForTests(); + let h: Harness | undefined; + try { + h = await harness("full"); + const conversationId = "conv-system-clear"; + const original = [{ role: "user", content: "original request" }]; + const seed = planIncrementalTurn({ conversationId, messages: original, system: "OLD-SYSTEM-INSTRUCTION" }); + assert.notEqual(seed.mode, "skip"); + if (seed.mode === "skip") return; + const seeded = await h.markerTurn(h.path, JSON.parse(seed.body)); + assert.equal(seeded.status, 200); + assert.equal(seeded.appliedSeq, original.length); + assert.ok(JSON.stringify(h.forwarded.at(-1)?.system).includes("OLD-SYSTEM-INSTRUCTION")); + noteIncrementalAck(conversationId, original.length, original, "OLD-SYSTEM-INSTRUCTION"); + if (scenario === "fresh client") __resetIncrementalClientForTests(); + const messages = scenario === "append" + ? [...original, { role: "assistant", content: "answer" }, { role: "user", content: "next request" }] + : scenario === "edit" ? [{ role: "user", content: "edited request" }] : original; + const plan = planIncrementalTurn({ conversationId, messages, system: undefined }, scenario === "forced resync"); + assert.equal(plan.mode, scenario === "fresh client" ? "seed" : scenario === "edit" || scenario === "forced resync" ? "resync" : "delta"); + const cleared = await h.markerTurn(h.path, JSON.parse(plan.body)); + assert.equal(cleared.status, 200); + assert.equal(cleared.appliedSeq, messages.length); + assert.equal(JSON.stringify(h.forwarded.at(-1)?.system ?? "").includes("OLD-SYSTEM-INSTRUCTION"), false, "removed original system must not be forwarded"); + noteIncrementalAck(conversationId, messages.length, messages, undefined); + const next = planIncrementalTurn({ conversationId, messages: [...messages, { role: "user", content: "another request" }], system: undefined }); + assert.equal(next.mode, "delta"); + if (next.mode !== "delta") return; + assert.equal(Object.hasOwn(JSON.parse(next.body), "system"), false, "acknowledged absence stays omitted on subsequent deltas"); + const followed = await h.markerTurn(h.path, JSON.parse(next.body)); + assert.equal(followed.status, 200); + assert.equal(JSON.stringify(h.forwarded.at(-1)?.system ?? "").includes("OLD-SYSTEM-INSTRUCTION"), false); + } finally { + if (priorRollout === undefined) delete process.env.BILI_ROLLOUT_PLUGIN_INCREMENTAL_TURNS; + else process.env.BILI_ROLLOUT_PLUGIN_INCREMENTAL_TURNS = priorRollout; + __resetIncrementalClientForTests(); + await h?.close(); + } + }); +} + +// --- client helper state machine (src/agent/incremental-client.ts) ------------------ + +test("incremental client helper: seed implies resync, delta slices, edits resync, sticky system", async () => { + const { __resetIncrementalClientForTests, planIncrementalTurn, noteIncrementalAck, markIncrementalDead } = await import("../src/agent/incremental-client.js"); + __resetIncrementalClientForTests(); + const conv = "conv-helper-1"; + const msgs1 = [{ role: "user", content: "hello there" }]; + const seed = planIncrementalTurn({ conversationId: conv, messages: msgs1, system: "be terse" }); + assert.equal(seed.mode, "seed"); + const seedBody = JSON.parse(seed.body) as { sinceSeq: number; resync: boolean; messages: unknown[]; system?: string }; + assert.equal(seedBody.sinceSeq, 0); + assert.equal(seedBody.resync, true, "seed never 409s — replacement semantics"); + assert.equal(seedBody.system, "be terse"); + + // ack seq 1 → next turn sends only the new tail + noteIncrementalAck(conv, 1, msgs1, "be terse"); + const msgs2 = [...msgs1, { role: "assistant", content: "hi" }, { role: "user", content: "and now?" }]; + const delta = planIncrementalTurn({ conversationId: conv, messages: msgs2, system: "be terse" }); + assert.equal(delta.mode, "delta"); + const deltaBody = JSON.parse(delta.body) as { sinceSeq: number; messages: unknown[]; system?: string }; + assert.equal(deltaBody.sinceSeq, 1); + assert.equal(deltaBody.messages.length, 2, "only the messages added since the ack"); + assert.equal(deltaBody.system, undefined, "sticky system omitted when unchanged"); + + // edited prefix → resync with the full history + const edited = [{ role: "user", content: "hello there (edited)" }, ...msgs2.slice(1)]; + const rs = planIncrementalTurn({ conversationId: conv, messages: edited, system: "be terse" }); + assert.equal(rs.mode, "resync"); + const rsBody = JSON.parse(rs.body) as { sinceSeq: number; resync: boolean; messages: unknown[] }; + assert.equal(rsBody.resync, true); + assert.equal(rsBody.messages.length, edited.length); + + // system change with an equal message set rides an EMPTY delta — the + // canonical is unchanged, the sticky system updates server-side + noteIncrementalAck(conv, edited.length, edited, "be terse"); + const sysChange = planIncrementalTurn({ conversationId: conv, messages: edited, system: "be verbose" }); + assert.equal(sysChange.mode, "delta"); + const sysBody = JSON.parse(sysChange.body) as { sinceSeq: number; messages: unknown[]; system?: string }; + assert.equal(sysBody.messages.length, 0, "pure sticky-system update — empty delta"); + assert.equal(sysBody.system, "be verbose"); + + // dead conversation falls back to skip until re-probed/reset + markIncrementalDead(conv); + const dead = planIncrementalTurn({ conversationId: conv, messages: edited, system: "be verbose" }); + assert.equal(dead.mode, "skip", "dead conversation rides full resend (host's own path)"); + __resetIncrementalClientForTests(); +}); + +test("wireMessagesLookAnthropic: anthropic shapes pass, OpenAI-tool shapes fall back to full resend", async () => { + const { wireMessagesLookAnthropic } = await import("../src/agent/incremental-client.js"); + assert.equal(wireMessagesLookAnthropic([{ role: "user", content: "hi" }]), true, "string content"); + assert.equal(wireMessagesLookAnthropic([{ role: "user", content: [{ type: "text", text: "hi" }] }]), true, "block content"); + assert.equal(wireMessagesLookAnthropic([{ role: "assistant", content: [{ type: "tool_use", id: "t", name: "x", input: {} }] }, { role: "user", content: [{ type: "tool_result", tool_use_id: "t", content: "ok" }] }]), true, "anthropic tool blocks are native wire shape"); + assert.equal(wireMessagesLookAnthropic([]), false, "empty list is not a conversation"); + assert.equal(wireMessagesLookAnthropic("nope"), false); + assert.equal(wireMessagesLookAnthropic([{ role: "user", content: "hi" }, { role: "tool", content: "out" }]), false, "OpenAI tool role is not convertible"); + assert.equal(wireMessagesLookAnthropic([{ role: "assistant", content: "x", tool_calls: [{ id: "c", type: "function", function: { name: "f" } }] }]), false, "tool_calls cannot ride anthropicToCore"); + assert.equal(wireMessagesLookAnthropic([{ role: "user", content: "hi", tool_call_id: "c" }]), false); + assert.equal(wireMessagesLookAnthropic([{ role: "user", content: 42 }]), false, "content must be string or array"); +}); + +test("incrementalTurnBodyShape accepts the incremental payload and rejects anthropic/garbage", async () => { + const { incrementalTurnBodyShape } = await import("../src/incremental-lane.js"); + const inc = Buffer.from(JSON.stringify({ conversationId: "c1", sinceSeq: 0, request: {}, messages: [] })); + assert.equal(incrementalTurnBodyShape(inc), true); + const anthropic = Buffer.from(JSON.stringify({ model: "claude-test", max_tokens: 64, messages: [] })); + assert.equal(incrementalTurnBodyShape(anthropic), false, "top-level model = ordinary request"); + const garbage = Buffer.from("not json at all"); + assert.equal(incrementalTurnBodyShape(garbage), false); + const badSeq = Buffer.from(JSON.stringify({ conversationId: "c1", sinceSeq: "3", messages: [] })); + assert.equal(incrementalTurnBodyShape(badSeq), false, "sinceSeq must be an integer"); +}); From 32d2fffa5b855b7b851e29bd01671227f4c4972c Mon Sep 17 00:00:00 2001 From: awork Date: Tue, 20 Oct 2026 01:21:22 +0800 Subject: [PATCH 3/4] fix(ci): drop unused export on maskUrlForPublish (unused-exports gate red on master) The function is only referenced inside log-mask.ts itself (maskUrlsForPublish); the leading export tripped scripts/check-unused-exports.mjs on every PR since it landed. --- src/log-mask.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/log-mask.ts b/src/log-mask.ts index ac2440b279..d34a619b1b 100644 --- a/src/log-mask.ts +++ b/src/log-mask.ts @@ -118,7 +118,7 @@ export function maskUrlsInText(text: string): string { * preference must not weaken the publish boundary (same stance as * redactSecretsInText, which is never gated by the switch). Userinfo, query * and hash are dropped exactly as in maskUrlForLog; the path is kept. */ -export function maskUrlForPublish(url: string): string { +function maskUrlForPublish(url: string): string { let u: URL; try { u = new URL(url); From d11dd3fb618e1e025c8230e3c10673402b952a4f Mon Sep 17 00:00:00 2001 From: awork Date: Tue, 20 Oct 2026 01:21:41 +0800 Subject: [PATCH 4/4] fix(ci): regenerate config docs to drop stale duplicate rows (docs-sync gate red on master) CONFIGURATION.md/zh-CN.md still carried a pre-#3818 storage.unified row (shadow default) and an older sqliteAutoDownload row missing the #3788 cooldown text; the website/config-reference seed is already deduped, so the committed docs were simply stale relative to it. --- CONFIGURATION.md | 2 -- CONFIGURATION.zh-CN.md | 2 -- 2 files changed, 4 deletions(-) diff --git a/CONFIGURATION.md b/CONFIGURATION.md index 09e953c4c5..d59b7e9cc6 100644 --- a/CONFIGURATION.md +++ b/CONFIGURATION.md @@ -174,8 +174,6 @@ This index is generated from `website/config-reference/*.yaml` — edit the seed | `persist.epermAlertRepeatMs` | number | 0 (no repeats) | BILI_PERSIST_EPERM_ALERT_REPEAT_MS | Repeat interval for the EPERM alert; 0 logs it once. | | `storage` | object | {} (all defaults) | — | Unified SQLite storage sidecar settings (#2671). | | `storage.unified` | legacy\|shadow\|full | unset (full) | BILI_STORAGE_UNIFIED | Tiered storage-migration switch (#2671): shadow = unified SQLite double-write dry-run with digest reconciliation; full = drop-in read path (unified-first loads with organic import, legacy fallback). Absent everywhere now defaults to FULL (#3818 flip - the shadow dry-run reconciled cleanly since #2815, so the index is the load path; JSON envelopes keep being written, and legacy remains the one-line escape hatch). File tier is strict legacy/shadow/full; the env tier also accepts 1/true (=shadow) and unified (=full) and wins over the file. Resolved once per lane boot - hot edits need a restart. Encryption (BILI_ENCRYPTION_KEY) hard-blocks the sidecar. | -| `storage.sqliteAutoDownload` | boolean | true | BILI_SQLITE_AUTO_DOWNLOAD | Gate for the one-time SQLite engine self-download (#2833): when no engine is available in-process (no built-in node:sqlite, no host-installed better-sqlite3) and the unified sidecar is wanted on this platform, bili downloads the pinned better-sqlite3 prebuilt tarball from the npm registry once (~11 MB, sha512-verified) into the cache dir and reuses it on every later start. Engine resolution order: built-in node:sqlite (Node >= 22.5) -> host-provided better-sqlite3 -> cached self-download -> self-download (gated by this switch) -> none (legacy-only, the #2825 degrade contract). The switch gates only the network fetch - a cached copy is used even when it is false; unsupported platforms never download. The tarball download walks a builtin mirror list (#3338): the configured registry (BILI_UPDATE_REGISTRY / update.registry) is tried first, then https://registry.npmjs.org, then https://registry.npmmirror.com - the first mirror that yields a sha512-verified tarball wins; a Node < 22.5 with no engine gets an explicit Node upgrade hint in the degrade reason (#3385). | -| `storage.unified` | legacy\|shadow\|full | unset (shadow) | BILI_STORAGE_UNIFIED | Tiered storage-migration switch (#2671): shadow = unified SQLite double-write dry-run with digest reconciliation; full = drop-in read path (unified-first loads with organic import, legacy fallback). Absent everywhere now defaults to shadow (JSON stays the sole source of truth; the index is digest-reconciled telemetry - set legacy to opt out). File tier is strict legacy/shadow/full; the env tier also accepts 1/true (=shadow) and unified (=full) and wins over the file. Resolved once per lane boot - hot edits need a restart. Encryption (BILI_ENCRYPTION_KEY) hard-blocks the sidecar. | | `storage.sqliteAutoDownload` | boolean | true | BILI_SQLITE_AUTO_DOWNLOAD | Gate for the one-time SQLite engine self-download (#2833): when no engine is available in-process (no built-in node:sqlite, no host-installed better-sqlite3) and the unified sidecar is wanted on this platform, bili downloads the pinned better-sqlite3 prebuilt tarball from the npm registry once (~11 MB, sha512-verified) into the cache dir and reuses it on every later start. Engine resolution order: built-in node:sqlite (Node >= 22.5) -> host-provided better-sqlite3 -> cached self-download -> self-download (gated by this switch) -> none (legacy-only, the #2825 degrade contract). The switch gates only the network fetch - a cached copy is used even when it is false; unsupported platforms never download. The tarball download walks a builtin mirror list (#3338): the configured registry (BILI_UPDATE_REGISTRY / update.registry) is tried first, then https://registry.npmjs.org, then https://registry.npmmirror.com - the first mirror that yields a sha512-verified tarball wins; a Node < 22.5 with no engine gets an explicit Node upgrade hint in the degrade reason (#3385). A failed download arms a 6-hour retry cooldown persisted as a marker file under the cache dir (billion-context/sqlite/.bootstrap-failed.json); the storage diagnostics blob shows the marker's absolute expiry time and path - delete that file and restart bili to force an immediate retry instead of waiting out the whole window (#3788). | | `storage.jsonWriter` | on\|off | on | BILI_STORAGE_JSON_WRITER | Phase 3 envelope switch (#2960): off stops writing the legacy JSON session envelope while storage.unified is full - the SQLite index becomes the only persistent copy (reads are already unified-first; boot enumerates the index union, so index-only sessions load normally). Only honored in effective-full mode: under shadow/legacy (or with encryption active) the request is ignored with a one-time warning because the JSON file remains the sole source of truth. Env tier 0/false/off = off and 1/true/on = on wins over the strict file spellings. Fail-safe: if the sidecar disables itself mid-run, envelope writes resume on the next save. Rollback: unset the knob - writing resumes, index-only sessions keep loading via the boot union, and replicas/restore re-materialize portable JSON envelopes. Downgrading the binary (or losing the SQLite engine) makes index-only sessions invisible to v0.1.196 and older until a >=v0.1.197 binary runs again - the data stays intact in index.db, it is just not read by the older code. The per-session content-store sidecar keeps being written either way. A jsonWriter=off lane must be the SOLE writer of its sessions directory: an off lane reads unified-first and never falls back to the file, so any peer instance running legacy/shadow against the same directory diverges silently (its JSON copy stops receiving updates, and the off lane never sees the peer's writes). | | `storage.walJournalSizeLimitBytes` | number | 8388608 | BILI_STORAGE_WAL_JOURNAL_SIZE_LIMIT_BYTES | WAL reclamation cap for index.db-wal (#3250), in bytes: every completed checkpoint truncates the WAL file back to this size, bounding disk use and crash-recovery replay in long-lived processes. NOT a per-session data quota - data lands in index.db as it is written and the WAL only stages frames between checkpoints, so session count/size does not scale this; a large burst can overshoot temporarily until the next completed checkpoint truncates back. Higher = rarer truncate IO but more steady disk use and a longer crash-replay tail. Env tier (positive integer) wins over the file tier; a garbage or non-positive env value falls through to the file/default tier instead of failing boot. Applied when the store opens at lane boot - hot edits need a restart. The effective value appears on the unified-enable log line and in the storage diagnostics blob. | diff --git a/CONFIGURATION.zh-CN.md b/CONFIGURATION.zh-CN.md index 758505a848..457bc4cbb3 100644 --- a/CONFIGURATION.zh-CN.md +++ b/CONFIGURATION.zh-CN.md @@ -174,8 +174,6 @@ | `persist.epermAlertRepeatMs` | number | 0 (no repeats) | BILI_PERSIST_EPERM_ALERT_REPEAT_MS | EPERM 告警的重复间隔;0 只记录一次。 | | `storage` | object | {} (all defaults) | — | 统一 SQLite 存储边车设置(#2671)。 | | `storage.unified` | legacy\|shadow\|full | unset (full) | BILI_STORAGE_UNIFIED | 分层存储迁移开关(#2671):shadow = 统一 SQLite 双写干跑 + digest 对账;full = 平替读路径(unified-first 加载 + 有机导入 + legacy 兜底)。两处均未设置时默认 full(#3818 翻转 —— shadow 干跑自 #2815 起对账一直干净,索引因此成为默认加载路径;JSON 信封照写,legacy 仍是一行逃生口)。文件层只接受 legacy/shadow/full;环境变量层还接受 1/true(=shadow)与 unified(=full)并优先于文件。每个 lane 启动时解析一次 —— 热改动需重启生效。加密(BILI_ENCRYPTION_KEY)会硬阻断边车。 | -| `storage.sqliteAutoDownload` | boolean | true | BILI_SQLITE_AUTO_DOWNLOAD | SQLite 引擎一次性自下载的闸门(#2833):进程内没有可用引擎(无内置 node:sqlite、宿主未装 better-sqlite3)且本平台希望启用统一边车时,bili 从 npm registry 一次性下载钉死的 better-sqlite3 预编译 tarball(约 11 MB,sha512 校验)到缓存目录,之后每次启动复用。引擎解析顺序:内置 node:sqlite(Node >= 22.5)-> 宿主机已装的 better-sqlite3 -> 缓存的自下载副本 -> 自下载(本开关控制)-> 无引擎(仅 legacy,即 #2825 降级契约)。开关只控制网络下载 —— 为 false 时缓存副本仍会使用;不支持的平台永不下载。 tarball 下载内置镜像列表兜底(#3338):先试配置的 registry(BILI_UPDATE_REGISTRY / update.registry),再试 https://registry.npmjs.org、https://registry.npmmirror.com —— 第一个通过 sha512 校验的镜像生效;Node < 22.5 且无引擎时,降级原因会给出明确的 Node 升级提示(#3385)。 | -| `storage.unified` | legacy\|shadow\|full | unset (shadow) | BILI_STORAGE_UNIFIED | 分层存储迁移开关(#2671):shadow = 统一 SQLite 双写干跑 + digest 对账;full = 平替读路径(unified-first 加载 + 有机导入 + legacy 兜底)。两处均未设置时默认 shadow(JSON 仍是唯一事实源,索引只做对账遥测 —— 设 legacy 可退出)。文件层只接受 legacy/shadow/full;环境变量层还接受 1/true(=shadow)与 unified(=full)并优先于文件。每个 lane 启动时解析一次 —— 热改动需重启生效。加密(BILI_ENCRYPTION_KEY)会硬阻断边车。 | | `storage.sqliteAutoDownload` | boolean | true | BILI_SQLITE_AUTO_DOWNLOAD | SQLite 引擎一次性自下载的闸门(#2833):进程内没有可用引擎(无内置 node:sqlite、宿主未装 better-sqlite3)且本平台希望启用统一边车时,bili 从 npm registry 一次性下载钉死的 better-sqlite3 预编译 tarball(约 11 MB,sha512 校验)到缓存目录,之后每次启动复用。引擎解析顺序:内置 node:sqlite(Node >= 22.5)-> 宿主机已装的 better-sqlite3 -> 缓存的自下载副本 -> 自下载(本开关控制)-> 无引擎(仅 legacy,即 #2825 降级契约)。开关只控制网络下载 —— 为 false 时缓存副本仍会使用;不支持的平台永不下载。 tarball 下载内置镜像列表兜底(#3338):先试配置的 registry(BILI_UPDATE_REGISTRY / update.registry),再试 https://registry.npmjs.org、https://registry.npmmirror.com —— 第一个通过 sha512 校验的镜像生效;Node < 22.5 且无引擎时,降级原因会给出明确的 Node 升级提示(#3385)。下载失败会置一个 6 小时的重试冷却,以标记文件形式持久化在缓存目录下(billion-context/sqlite/.bootstrap-failed.json);storage 诊断块会显示该标记的绝对到期时间与路径 —— 删除该文件并重启 bili 即可强制立即重试,不必等完整个冷却窗口(#3788)。 | | `storage.jsonWriter` | on\|off | on | BILI_STORAGE_JSON_WRITER | Phase 3 信封开关(#2960):off 时若 storage.unified 为 full 则不再写 legacy JSON 会话信封 —— SQLite 索引成为唯一持久化副本(读路径本就 unified-first;boot 枚举索引并集,纯索引会话可正常加载)。仅在有效 full 模式下生效:shadow/legacy(或加密激活)下请求会被忽略并警告一次,因为 JSON 文件仍是唯一事实源。环境变量层 0/false/off = off、1/true/on = on,优先于严格拼写的文件层。失败安全:若边车运行中自禁用,下次保存即恢复信封写入。回滚:去掉该开关 —— 写入恢复,纯索引会话经 boot 并集继续可加载,replicas/restore 可重新物化可移植的 JSON 信封。降级二进制(或丢失 SQLite 引擎)会让纯索引会话在 v0.1.196 及更早版本上不可见,直到重新运行 >=v0.1.197 —— 数据在 index.db 中完好无损,只是旧代码不读它。每会话的 content-store 边车无论开关如何都照写。jsonWriter=off 的 lane 必须是其会话目录的唯一写者:off lane 读路径 unified-first、永不回退到文件,因此同一目录上跑 legacy/shadow 的对等实例会静默分叉(它的 JSON 副本收不到更新,off lane 也看不到对等实例的写入)。 | | `storage.walJournalSizeLimitBytes` | number | 8388608 | BILI_STORAGE_WAL_JOURNAL_SIZE_LIMIT_BYTES | index.db-wal 的 WAL 回收上限(#3250),单位字节:每次完成的 checkpoint 把 WAL 文件截回该大小,约束长驻进程的磁盘占用与崩溃恢复重放。不是每会话数据配额 —— 数据写入时就已落进 index.db,WAL 只是两次 checkpoint 之间的帧暂存区,会话数量/大小不会放大它;大 burst 可能暂时超出,直到下一次完成的 checkpoint 截回。调大 = truncate IO 更稀疏,但稳态磁盘占用更多、崩溃恢复重放尾更长。环境变量层(正整数)优先于配置文件层;env 值非法或非正数时静默落到文件/默认层,boot 不崩。lane 启动、store open 时生效 —— 热改动需重启。实际取值会出现在 unified-enable 日志行与 storage 诊断块中。 |