diff --git a/CHANGELOG.md b/CHANGELOG.md index 14a7b76577..3c6375674f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,10 @@ Versions follow the merge of a `*_release-v*` branch; CI publishes to npm on tag ## [Unreleased] +### Fixed + +- **增量车道:拉取回执修复 ack 传输盲区 + 漂移回退取富侧(#4352/#4323)**。2026-10-22 事故双根因修复:① pi 的 after_provider_response 只在部分 provider 传输下暴露响应头,x-bili-applied-seq 丢失时 noteIncrementalAck 永不执行,后续每轮都整包 re-seed(sinceSeq=0 重发 21K 条历史)。现在 GET /__bili/plugin/turn?conversation= 返回热 canonical 的 {appliedSeq, boundary}(boundary=末条 wire 消息哈希,冷重建诚实返回 null),pi 扩展在规划下一轮前拉取回执,仅在 boundary 证明 canonical 吸收了我方末条消息时采纳——绝不按 seq 前进盲目采纳(外来的全量重发也会推进 canonical);② #4323 多实例写同一目录时,漂移回退无条件吃旧信封,把索引里更新的折叠世代(26,971 refs/16 active)换成旧世代(11,849/12),覆盖与 canonical 失配→0 可压区间→preflight 8.29M 假性死局 502。现在漂移两侧按 mapped ref 数取富侧(平局信封赢,平局语义=原子整文件)。新增 tests/incremental-ack-pull.test.ts 3 例(响应头 ack 回归钉/拉取采纳/冷 boundary null 拒采纳)+ storage-full-flip 4b(陈旧信封让位新索引世代)。 + ### Changes - **kernel: aged compress receipts fold (#4348).** `compress` exchanges previously enjoyed a hard-coded, age-independent folding exemption (`ALWAYS_PROTECTED_TOOLS`), so every successful compression receipt stayed on the wire forever — a long session accumulated hundreds of `[Compressed mX-mY ...]` stubs (measured: 148 receipts / ~170K chars riding every request) that no range could reclaim, and ranges over them died with "Range would split N turn(s)" once the receipts interleaved with re-anchored coverage. Now only the newest `COMPRESS_EXCHANGE_KEEP_RECENT = 3` compress exchanges keep the exemption (working memory: the model can still see what it just folded); older exchanges fold like ordinary tool messages, with call+receipt+reasoning kept whole by the existing turn-integrity gate. The summaries the model wrote live on verbatim in the rendered block anchors, so decompress/search lose nothing. Host `protectedTools`/`isToolProtected` patterns still win for non-compress tools; for compress-dialect exchanges the kernel aging policy outranks host pinning (same doctrine as #4289 — without it, MCP-lane forms pinned via the host `isToolProtected` hook could never age out). `acp_rule` keeps its blanket exemption. diff --git a/src/agent/incremental-client.ts b/src/agent/incremental-client.ts index 0a1ed6d72e..ef7b5de9fc 100644 --- a/src/agent/incremental-client.ts +++ b/src/agent/incremental-client.ts @@ -229,6 +229,47 @@ function wireHash(m: IncrementalWireMessage): string { return createHash("sha256").update(`${m.role}\u0000${JSON.stringify(m.content)}`).digest("hex").slice(0, 16); } +/** Public twin of the internal wire hash for boundary comparisons. The + * server-side hashWireBoundary (src/incremental-lane.ts) must stay + * byte-identical to this formula. */ +export function incrementalWireHash(m: IncrementalWireMessage): string { + return wireHash(m); +} + +/** Receipt served by GET /__bili/plugin/turn?conversation= — the warm + * canonical's appliedSeq plus the wire-hash of its last applied message. + * A null boundary means "cold, cannot vouch" and must never be adopted. */ +interface IncrementalReceipt { + appliedSeq: number; + boundary: string | null; +} + +/** Fresh (never cached) receipt pull for hosts whose transport hides the + * x-bili-applied-seq response header (#4352: pi's after_provider_response + * exposes headers only for some provider transports — the ack channel + * cannot depend on it). Any failure → null; the caller keeps its pending + * plan and the next turn retries. */ +export async function pullIncrementalReceipt( + proxyBase: string, + conversationId: string, + fetchImpl: typeof fetch = fetch, +): Promise { + try { + const url = `${proxyBase.replace(/\/$/, "")}/__bili/plugin/turn?conversation=${encodeURIComponent(conversationId)}`; + const res = await fetchImpl(url, { headers: { "x-bili-plugin": "pi" }, signal: AbortSignal.timeout(3000) }); + if (res.status !== 200) return null; + const body = (await res.json()) as { ok?: unknown; conversation?: { appliedSeq?: unknown; boundary?: unknown } }; + if (body.ok !== true || typeof body.conversation !== "object" || body.conversation === null) return null; + const seq = body.conversation.appliedSeq; + const boundary = body.conversation.boundary; + if (typeof seq !== "number" || !Number.isSafeInteger(seq) || seq < 0) return null; + if (boundary !== null && typeof boundary !== "string") return null; + return { appliedSeq: seq, boundary: boundary as string | null }; + } catch { + return null; + } +} + type IncrementalPlan = | { mode: "seed"; body: string } // full history, sinceSeq 0 (server may 409 → resync) | { mode: "delta"; body: string } // suffix only, acked sinceSeq diff --git a/src/agent/pi.ts b/src/agent/pi.ts index d6d6c931b0..ba90a8ca13 100644 --- a/src/agent/pi.ts +++ b/src/agent/pi.ts @@ -14,7 +14,7 @@ import { disposeSubagentSelfReg, selfRegisterForSession, type SubagentSelfRegSta import { isModelApiUrl, nativeInterceptInstalled } from "./native-intercept.js"; import { wirePiSubagents } from "./pi-subagents.js"; import { wireToolGuardrails } from "./tool-guardrails.js"; -import { clearIncrementalProbe, INCREMENTAL_TURN_MARKER_HEADER, incrementalEnvelopeConversationId, noteIncrementalRejection, noteIncrementalAck, planIncrementalTurn, probeIncrementalTurns, sweepStale, type IncrementalWireMessage } from "./incremental-client.js"; +import { clearIncrementalProbe, INCREMENTAL_TURN_MARKER_HEADER, incrementalEnvelopeConversationId, incrementalWireHash, noteIncrementalRejection, noteIncrementalAck, planIncrementalTurn, probeIncrementalTurns, pullIncrementalReceipt, sweepStale, type IncrementalWireMessage } from "./incremental-client.js"; import type { DirectSendPrep } from "./native-intercept.js"; import { asciiHeaderValue, detectProxyBase, destinationRoutedThroughProxy, fetchManifest, forwardTool, fetchStatus, fetchProxyVersion, postIdentityRegister, reportRuntimeInfoOnChange, armedIdleNotice, noSessionWarning, nonHttpProvidersFromEnv, type ForwardedToolResult, type ManifestTool } from "./shared.js"; import { createForkAdopter } from "./fork-adopt.js"; @@ -1339,6 +1339,25 @@ export function createBiliPlugin(agentOverride?: string, opts?: { retryIntervalM if (proxyBase === undefined) proxyBase = await awaitNativeProxyOrigin(); if (proxyBase === undefined) return undefined; if (!(await probeIncrementalTurns(proxyBase))) return undefined; + // #4352: the response-header ack channel is transport-dependent — + // pi's after_provider_response only exposes headers for some + // provider transports, and a missed ack makes EVERY next turn + // re-seed the full history (the 2026-10-22 incident: turn 2 sent + // sinceSeq=0 again). Before planning, reconcile any still-pending + // un-acked turn by PULLING the lane's receipt and adopting it only + // when the boundary hash proves the canonical absorbed OUR last + // message — never on mere seq progress (a foreign full-resend can + // advance the canonical too). + const pendingAck = pendingIncrementalAcks.get(sid); + if (pendingAck !== undefined && pendingAck.messages.length > 0) { + const receipt = await pullIncrementalReceipt(proxyBase, sid); + const expected = incrementalWireHash(pendingAck.messages[pendingAck.messages.length - 1]); + if (receipt !== null && receipt.appliedSeq > 0 && receipt.boundary === expected) { + pendingIncrementalAcks.delete(sid); + piIncrementalOriginals.delete(sid); + noteIncrementalAck(sid, receipt.appliedSeq, pendingAck.messages, pendingAck.system); + } + } const plan = planIncrementalTurn({ conversationId: sid, // The marker divert uses the ORIGINAL request URL server-side; diff --git a/src/incremental-lane.ts b/src/incremental-lane.ts index 7f60d49aff..c442575864 100644 --- a/src/incremental-lane.ts +++ b/src/incremental-lane.ts @@ -58,6 +58,7 @@ * (R1 message ids), durable acks. */ import http from "node:http"; +import { createHash } from "node:crypto"; import { Readable } from "node:stream"; import { anthropicToCore, coreToAnthropic, type BiliMessage } from "acp-kernel/wire"; import { getStore } from "./persist.js"; @@ -162,6 +163,12 @@ interface Canonical { marks: Map; system: unknown; systemSet: boolean; + /** #4352 ack pull: hash of the last WIRE message the canonical was + * advanced with (the client-side incrementalWireHash formula over the + * delta's final message). Warm-cache only — a cold rebuild has no wire + * bytes to hash and reports null; clients treat a null boundary as + * "cannot adopt, keep full resend" and self-heal on the next turn. */ + boundary: string | null; } const canonicalCache = new Map(); const inFlight = new Set(); @@ -386,6 +393,14 @@ function loadSystem(sessionId: string): unknown { } /** Seed/append/resync validation + application. Pure over the canonical. */ +/** Byte-identical to the client-side incrementalWireHash + * (src/agent/incremental-client.ts) over the raw wire message — the two + * formulas must never diverge or the pull-ack boundary check degrades into + * permanent false mismatches. */ +function hashWireBoundary(m: { role: string; content: unknown }): string { + return createHash("sha256").update(`${m.role}\u0000${JSON.stringify(m.content)}`).digest("hex").slice(0, 16); +} + function applyTurn( entry: Canonical, sinceSeq: number, @@ -410,6 +425,9 @@ function applyTurn( const base = resync ? [] : entry.msgs; entry.msgs = [...base, ...flat.msgs]; for (const [id, mark] of flat.cacheControls) entry.marks.set(id, mark); + entry.boundary = deltaWire.length > 0 + ? hashWireBoundary(deltaWire[deltaWire.length - 1]) + : (resync ? null : entry.boundary); return { ok: true, appliedSeq: entry.msgs.length, marks: flat.cacheControls }; } @@ -457,7 +475,7 @@ export function incrementalTurnsEnabled(): boolean { /** GET probe on the turn path: capabilities for native extensions. A 404 on * older servers means "no incremental support" — clients fall back to full * resend, which is the default everywhere. */ -export function handleIncrementalProbe(res: http.ServerResponse): void { +export function handleIncrementalProbe(res: http.ServerResponse, conversationId?: string): void { const sidecar = (() => { try { return getStore().replicasAvailable(); @@ -465,12 +483,26 @@ export function handleIncrementalProbe(res: http.ServerResponse): void { return false; } })(); + // #4352 ack pull: `?conversation=` upgrades the probe into a receipt + // poll. Served from the WARM canonical only — a cold rebuild here must + // not touchCache (a probe is not a turn), and a rebuilt entry has no wire + // boundary to hash, so an honest null forces the client to keep full + // resend for one more turn instead of adopting a guess. + let conversation: { appliedSeq: number; boundary: string | null } | undefined; + if (conversationId !== undefined && conversationId.length > 0) { + const entry = canonicalCache.get(conversationId); + conversation = { + appliedSeq: entry !== undefined && entry.sessionId === laneSessionId(conversationId) ? entry.msgs.length : 0, + boundary: entry !== undefined && entry.sessionId === laneSessionId(conversationId) ? entry.boundary : null, + }; + } jsonReply(res, 200, { ok: true, protocol: 1, feature: "pluginIncrementalTurns", enabled: featureEnabled("pluginIncrementalTurns") && sidecar, storage: sidecar ? "unified" : "legacy-only", + ...(conversation !== undefined ? { conversation } : {}), }); } @@ -554,7 +586,7 @@ export async function handleIncrementalTurn( log("warn", `[incremental] canonical rebuild failed for ${sessionId}: ${rebuilt.err} — requesting client resync`); return jsonReply(res, 409, { error: "seq-mismatch", serverSeq: 0, reason: rebuilt.err, conversationId }); } - entry = { sessionId, msgs: rebuilt.msgs, marks: loadMarks(sessionId), system: loadSystem(sessionId), systemSet: false }; + entry = { sessionId, msgs: rebuilt.msgs, marks: loadMarks(sessionId), system: loadSystem(sessionId), systemSet: false, boundary: null }; } const applied = applyTurn(entry, sinceSeq, resync, deltaWire as never, "system" in payload); if (!applied.ok) { diff --git a/src/persist.ts b/src/persist.ts index 3aead97047..e8759c8a30 100644 --- a/src/persist.ts +++ b/src/persist.ts @@ -1439,6 +1439,27 @@ export class SessionStore { const driftedEnvelope = this.loadEnvelope(id, meta); if (driftedEnvelope) { const drifted = buildSession(driftedEnvelope.payload); + // #4323: "drifted" only proves the two copies DIVERGED — + // not which side is ahead. With multiple writers sharing a + // sessions directory (the 2026-10-22 incident) the INDEX + // can hold the newer generation: serving the stale + // envelope resurrected an older fold state whose coverage + // no longer matched the canonical rows, the raw history + // replayed unfolded (0 compressible ranges) and preflight + // dead-ended at a phantom 8.29M tokens. Compare both + // sides' mapped-ref counts and serve the richer one; the + // envelope keeps winning ties (its bytes are whole-file + // atomic, the index's superiority must be visible). + const unifiedView = this.loadUnified(id); + if (unifiedView) { + const envRefs = Object.keys((drifted.state as { messageRefs?: { byRef?: Record } }).messageRefs?.byRef ?? {}).length; + const idxState = unifiedView.state as { messageRefs?: { byRef?: Record } }; + const idxRefs = Object.keys(idxState.messageRefs?.byRef ?? {}).length; + if (idxRefs > envRefs) { + this.log("warn", `[persist] drift on ${id}: unified index carries ${idxRefs} mapped ref(s) vs envelope ${envRefs} — serving the richer index side (multi-writer generation, #4323)`); + return this.afterLoad(id, unifiedView); + } + } if (hasNegativePersistedTokens(driftedEnvelope.payload)) { this.scheduleSave(drifted); this.log("info", `[persist] clamped negative token stats on reload for ${id} (#408)`); diff --git a/src/server/handle.ts b/src/server/handle.ts index 7f19954fe3..60229b7698 100644 --- a/src/server/handle.ts +++ b/src/server/handle.ts @@ -139,7 +139,8 @@ export async function handle( res.end(JSON.stringify({ error: denial })); return; } - handleIncrementalProbe(res); + const conv = new URL(req.url ?? "/", "http://localhost").searchParams.get("conversation"); + handleIncrementalProbe(res, conv !== null && conv.length > 0 ? conv : undefined); return; } // #3248 marker divert: a native extension may stamp diff --git a/tests/incremental-ack-pull.test.ts b/tests/incremental-ack-pull.test.ts new file mode 100644 index 0000000000..45f17878c3 --- /dev/null +++ b/tests/incremental-ack-pull.test.ts @@ -0,0 +1,180 @@ +// #4352: the ack channel must not depend on the response-header transport. +// +// The 2026-10-22 incident: pi's after_provider_response exposes response +// headers only for some provider transports — when the +// x-bili-applied-seq header never reaches the extension, noteIncrementalAck +// never runs, and EVERY subsequent turn re-seeds the full history +// (sinceSeq=0). Fix: before planning the next turn, reconcile any un-acked +// pending plan by PULLING GET /__bili/plugin/turn?conversation= and +// adopting the receipt only when its boundary hash proves the canonical +// absorbed OUR last message. +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 } from "../src/session.ts"; +import { _setForTest } from "../src/registry.ts"; +import { resetToolRingForTest } from "../src/tool-ring.ts"; +import { __resetIncrementalLaneForTests } from "../src/incremental-lane.ts"; +import { __resetIncrementalClientForTests } from "../src/agent/incremental-client.ts"; +import { createBiliPlugin } from "../src/agent/pi.ts"; + +process.env.NODE_ENV = "test"; +process.env.BILI_ROLLOUT_PLUGIN_INCREMENTAL_TURNS = "1"; +const testRoot = mkdtempSync(join(tmpdir(), "bili-ackpull-")); +test.after(() => rmSync(testRoot, { recursive: true, force: true })); + +function makeFakePi() { + const events = new Map unknown)[]>(); + const tools: unknown[] = []; + return { + events, + tools, + on: (event: string, handler: (event: never, ctx: never) => unknown) => { + const list = events.get(event) ?? []; + list.push(handler as (event: unknown, ctx: unknown) => unknown); + events.set(event, list); + }, + registerTool: (tool: unknown) => { tools.push(tool); }, + registerCommand: () => {}, + registerProvider: () => {}, + getSessionName: () => "test", + fire: (name: string, event: unknown, ctx: unknown) => { + const list = events.get(name) ?? []; + let last: unknown; + for (const h of list) last = h(event, ctx); + return last; + }, + fireAsync: async (name: string, event: unknown, ctx: unknown) => { + const list = events.get(name) ?? []; + let last: unknown; + for (const h of list) last = await h(event, ctx); + return last; + }, + }; +} + +type Harness = { + proxyUrl: string; + baseUrl: string; + pi: ReturnType; + ctx: { sessionManager: { getSessionId: () => string }; model: { id: string; contextWindow: number; api: string; baseUrl: string }; cwd: string }; + close: () => Promise; +}; + +async function withHarness(fn: (h: Harness) => Promise): 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 = "full"; + _resetSessionsForTest(); + _resetPluginStateForTest(); + resetToolRingForTest(); + __resetIncrementalLaneForTests(); + __resetIncrementalClientForTests(); + const store = new SessionStore({ dir: dir + "/sessions", enabled: true, debounceMs: 60000 }); + _setStoreForTest(store); + _setForTest({}); + const upstream = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (c: Buffer) => chunks.push(c)); + req.on("end", () => { + res.writeHead(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 } })); + }); + }); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const upstreamUrl = `http://127.0.0.1:${(upstream.address() as { port: number }).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 pi = makeFakePi(); + createBiliPlugin("pi")(pi as never); + const ctx = { + sessionManager: { getSessionId: () => "conv-pi-1" }, + model: { id: "claude-test", contextWindow: 400000, api: "anthropic", baseUrl: `${proxyUrl}/bili/${upstreamUrl}/v1` }, + cwd: dir, + }; + try { + await pi.fireAsync("session_start", {}, ctx); + const deadline = Date.now() + 15000; + while (pi.tools.length < 2 && Date.now() < deadline) await new Promise((r) => setTimeout(r, 25)); + assert.ok(pi.tools.length >= 2, `tools registered: ${pi.tools.length}`); + await fn({ proxyUrl, baseUrl: `${ctx.model.baseUrl}/messages`, pi, ctx, close: async () => undefined }); + } finally { + await new Promise((resolve) => proxy.close(() => resolve())); + store.closeQuietly(); + await new Promise((resolve) => upstream.close(() => resolve())); + } +} + +const PAYLOAD1 = { model: "claude-test", max_tokens: 1024, system: "be brief", messages: [{ role: "user", content: "hello".repeat(50) }] }; + +test("#4352 ack via response header keeps working (regression pin)", async () => { + await withHarness(async (h) => { + const headers1: Record = {}; + await h.pi.fireAsync("before_provider_headers", { headers: headers1 }, h.ctx); + const replaced1 = await h.pi.fireAsync("before_provider_request", { payload: PAYLOAD1 }, h.ctx) as Record; + const res1 = await fetch(h.baseUrl, { method: "POST", headers: { "content-type": "application/json", ...headers1 }, body: JSON.stringify(replaced1) }); + assert.equal(res1.status, 200); + assert.equal(res1.headers.get("x-bili-applied-seq"), "1", "the lane stamps the ack header on 2xx"); + const ackHeaders: Record = {}; + res1.headers.forEach((v, k) => { ackHeaders[k] = v; }); + await h.pi.fireAsync("after_provider_response", { status: res1.status, headers: ackHeaders }, h.ctx); + const payload2 = { ...PAYLOAD1, messages: [...PAYLOAD1.messages, { role: "assistant", content: "answer" }, { role: "user", content: "next".repeat(50) }] }; + const replaced2 = await h.pi.fireAsync("before_provider_request", { payload: payload2 }, h.ctx) as { sinceSeq?: number }; + assert.equal(replaced2.sinceSeq, 1, "acked turn 1 → turn 2 is a delta"); + }); +}); + +test("#4352 pull receipt: a transport that hides response headers must not cause a re-seed", async () => { + await withHarness(async (h) => { + const headers1: Record = {}; + await h.pi.fireAsync("before_provider_headers", { headers: headers1 }, h.ctx); + const replaced1 = await h.pi.fireAsync("before_provider_request", { payload: PAYLOAD1 }, h.ctx) as Record; + const res1 = await fetch(h.baseUrl, { method: "POST", headers: { "content-type": "application/json", ...headers1 }, body: JSON.stringify(replaced1) }); + assert.equal(res1.status, 200); + await res1.text(); + // The incident shape: the host transport never surfaces the response + // headers to after_provider_response — no ack is observed. + const probeRes = await fetch(`${h.proxyUrl}/__bili/plugin/turn?conversation=conv-pi-1`, { headers: { "x-bili-plugin": "pi" } }); + assert.equal(probeRes.status, 200); + const receipt = await probeRes.json() as { conversation?: { appliedSeq?: number; boundary?: string | null } }; + assert.equal(receipt.conversation?.appliedSeq, 1, "the receipt exposes the warm canonical's appliedSeq"); + assert.ok(typeof receipt.conversation?.boundary === "string" && receipt.conversation.boundary.length === 16, "boundary is the 16-hex wire hash"); + const payload2 = { ...PAYLOAD1, messages: [...PAYLOAD1.messages, { role: "assistant", content: "answer" }, { role: "user", content: "next".repeat(50) }] }; + const replaced2 = await h.pi.fireAsync("before_provider_request", { payload: payload2 }, h.ctx) as { sinceSeq?: number; resync?: boolean; messages?: unknown[] }; + assert.notEqual(replaced2.sinceSeq, 0, `pull reconciliation must adopt the receipt instead of re-seeding (sinceSeq=${replaced2.sinceSeq})`); + assert.notEqual(replaced2.resync, true, "turn 2 is a plain delta, not a resync"); + assert.ok((replaced2.messages ?? []).length < payload2.messages.length, "turn 2 ships only the delta suffix"); + }); +}); + +test("#4352 cold canonical reports boundary null — the client keeps its pending plan and does not adopt", async () => { + await withHarness(async (h) => { + // Warm the lane with one applied turn (no ack observation). + const headers1: Record = {}; + await h.pi.fireAsync("before_provider_headers", { headers: headers1 }, h.ctx); + const replaced1 = await h.pi.fireAsync("before_provider_request", { payload: PAYLOAD1 }, h.ctx) as Record; + const res1 = await fetch(h.baseUrl, { method: "POST", headers: { "content-type": "application/json", ...headers1 }, body: JSON.stringify(replaced1) }); + await res1.text(); + // Simulate a lane restart: warm canonical dropped. + __resetIncrementalLaneForTests(); + const probeRes = await fetch(`${h.proxyUrl}/__bili/plugin/turn?conversation=conv-pi-1`, { headers: { "x-bili-plugin": "pi" } }); + const receipt = await probeRes.json() as { conversation?: { appliedSeq?: number; boundary?: string | null } }; + assert.equal(receipt.conversation?.boundary, null, "a cold canonical cannot vouch for a boundary"); + // The client must NOT adopt a null boundary — turn 2 re-seeds with + // resync (the honest recovery), never a silent delta. + const payload2 = { ...PAYLOAD1, messages: [...PAYLOAD1.messages, { role: "assistant", content: "answer" }, { role: "user", content: "next".repeat(50) }] }; + const replaced2 = await h.pi.fireAsync("before_provider_request", { payload: payload2 }, h.ctx) as { sinceSeq?: number; resync?: boolean }; + assert.ok(replaced2.resync === true || replaced2.sinceSeq === 0, "unadoptable receipt → the client falls back to a full re-seed, not a guess"); + }); +}); diff --git a/tests/storage-full-flip.test.ts b/tests/storage-full-flip.test.ts index 915c826265..ad37183c21 100644 --- a/tests/storage-full-flip.test.ts +++ b/tests/storage-full-flip.test.ts @@ -238,6 +238,56 @@ test("#3818 ④ verifyDrift in the wire; drifted session reads the envelope whil } }); +// --------------------------------------------------------------------------- +// 4b. #4323: when the two sides drift, the RICHER side wins — a stale +// envelope (older multi-writer generation) must not resurrect an outdated +// fold state over a newer index generation. +// --------------------------------------------------------------------------- + +test("#4323 4b drifted session serves the richer side — a stale envelope loses to a newer index generation", async () => { + process.env.BILI_STORAGE_UNIFIED = "full"; + try { + const dir = tmpDir("drift-rich"); + const store = new SessionStore({ enabled: true, dir, debounceMs: 0 }); + const session = makeSession("drift-rich-session"); + const byRef: Record = {}; + const byRaw: Record = {}; + for (let i = 1; i <= 8; i++) { + const ref = "m" + String(i).padStart(5, "0"); + byRef[ref] = "raw-" + i; + byRaw["raw-" + i] = ref; + } + (session.state as { messageRefs: { byRef: Record; byRaw: Record } }).messageRefs = { byRaw, byRef }; + store.scheduleSave(session); + await settle(300); + const sh = (store as unknown as { shadow?: { record(r: unknown): void; driftedSessions?: Set } }).shadow; + assert.ok(sh, "full mode opens a sidecar"); + + // Freeze the index at the RICH v1, then let a stale writer commit a + // SMALLER v2 to the envelope only — the #4323 multi-writer shape. + const realRecord = sh.record.bind(sh); + sh.record = () => {}; + if (!sh.driftedSessions) sh.driftedSessions = new Set(); + sh.driftedSessions.add("drift-rich-session"); + const stale = makeSession("drift-rich-session"); + (stale.state as { messageRefs: { byRef: Record; byRaw: Record } }).messageRefs = { byRaw: { old1: "m00001" }, byRef: { m00001: "old1" } }; + (stale.stats as { requests: number }).requests = 42; + store.scheduleSave(stale); + assert.ok(store.flushSync(stale), "smaller (stale envelope) committed"); + await settle(); + sh.record = realRecord; + + const reloaded = store.loadSync("drift-rich-session"); + assert.ok(reloaded, "session loads"); + const refs = Object.keys((reloaded.state as { messageRefs?: { byRef?: Record } }).messageRefs?.byRef ?? {}).length; + assert.ok(refs >= 8, "the richer index side must win the drift (want >= 8 refs — the stale envelope had 1), got " + refs); + store.closeQuietly(); + rmrf(dir); + } finally { + delete process.env.BILI_STORAGE_UNIFIED; + } +}); + // --------------------------------------------------------------------------- // 5. bootBounded enumerates the index union // ---------------------------------------------------------------------------