diff --git a/src/cache-ledger.ts b/src/cache-ledger.ts index a21ee159dd..4910b999aa 100644 --- a/src/cache-ledger.ts +++ b/src/cache-ledger.ts @@ -1,4 +1,5 @@ import { createHash, randomUUID } from "node:crypto"; +import { AsyncLocalStorage } from "node:async_hooks"; import { computeFoldEconomics, decomposeSample, @@ -81,6 +82,9 @@ interface LedgerFold { } interface LedgerLine { + captureId?: string; + captureRound?: number; + evidence?: "out-of-order"; seq: number; at: number; input: number; @@ -418,6 +422,9 @@ const BODY_FULL_CAP = 8 * 1024 * 1024; * UTF-8 bytes). #2923: slots live under session.transient so the retained * copies release with the session. */ export interface SeamSlot { + order?: number; + requestId?: string; + round?: number; /** Capped head (≤ SEAM_BODY_CAP bytes) — seam forensics + JSON parse side. */ head: Buffer; /** Exact outbound body size in bytes — precise cap/full detection. */ @@ -460,13 +467,51 @@ export interface InboundSlot { meta: { role: string; len: number; cls: string }[]; } +type Capture = { + session: Session; + requestId: string; + round: number; + sentBody?: SeamSlot; + inBody?: InboundSlot; + imageFacts?: ForwardedImageFacts; +}; +const captures = new AsyncLocalStorage(); +let captureOrder = 0; + +/** Covers the entire forward, including awaited streaming/loop callbacks. + * Concurrent requests keep independent pending evidence; only settled pairs + * live on the session. It does not serialize upstream work or change the wire. */ +export async function withCacheCapture(session: Session | undefined, fn: () => Promise): Promise { + if (!session) return fn(); + return captures.run({ session, requestId: randomUUID(), round: 0 }, async () => { + try { return await fn(); } + finally { + const capture = captures.getStore(); + if (capture) { + delete capture.sentBody; + delete capture.inBody; + delete capture.imageFacts; + } + } + }); +} + +function pendingCapture(session: Session): Capture | NonNullable { + const capture = captures.getStore(); + return capture?.session === session ? capture : (session.transient ??= {}); +} + +export function lastForwardedBody(session: Session): SeamSlot | undefined { + return pendingCapture(session).sentBody; +} + /** #3023: capture side of the inbound pair — call at the send chokepoint * (src/server/relay.ts), next to noteForwardedBody, with the request's * original (pre-rebuild) message list. Cost is one canonical stringify per * message (≈ one extra body serialization, the outbound path already does * the same order of work hashing the wire body). */ export function noteInboundMessages(session: Session, msgs: CoreMessage[]): void { - const transient = session.transient ??= {}; + const transient = pendingCapture(session); if (msgs.length > INBOUND_MSG_CAP) { // Too many messages to fingerprint whole: keep the count (so the // settle-side guard can decline) but drop any stale slot from a @@ -629,10 +674,13 @@ export function noteForwardedBody(session: Session, body: string, msgs?: number // re-deriving them. const buf = Buffer.from(body, "utf8"); const digest = createHash("sha256").update(buf).digest("hex"); - const transient = session.transient ??= {}; + const transient = pendingCapture(session); const prior = transient.sentBody; const samePayload = prior !== undefined && prior.digest === digest; transient.sentBody = { + order: ++captureOrder, + requestId: "requestId" in transient ? transient.requestId : undefined, + round: "round" in transient ? ++transient.round : undefined, head: buf.byteLength > SEAM_BODY_CAP ? Buffer.from(buf.subarray(0, SEAM_BODY_CAP)) : buf, bytes: buf.byteLength, msgs: msgs ?? (samePayload ? prior.msgs : null), @@ -641,6 +689,10 @@ export function noteForwardedBody(session: Session, body: string, msgs?: number keyFp: keyFp ?? (samePayload ? prior.keyFp : undefined), effFp: effFp ?? (samePayload ? prior.effFp : undefined), }; + if ("round" in transient && transient.round > 1) { + delete transient.imageFacts; + delete transient.inBody; + } // prepare may reuse a measured baseline; this new payload is not measured yet. if (session.stats.contextTokensSource !== undefined) recordContextObservation(session, session.stats.contextTokens, "estimate"); else delete session.metadata?.publicContextObservation; @@ -681,13 +733,17 @@ const imageFactsLastSent = new WeakMap(); * with the image facts of the round about to be sent. The next settleUsageReport * consumes exactly this entry (same pairing guarantee as the seam forensics). */ export function noteForwardedImageFacts(session: Session, facts: ForwardedImageFacts): void { + const capture = captures.getStore(); + if (capture?.session === session) { capture.imageFacts = facts; return; } imageFactsLastSent.set(session, facts); } function settleImageLearning(session: Session, billedTotal: number): void { - const facts = imageFactsLastSent.get(session); + const capture = captures.getStore(); + const facts = capture?.session === session ? capture.imageFacts : imageFactsLastSent.get(session); if (!facts) return; - imageFactsLastSent.delete(session); + if (capture?.session === session) delete capture.imageFacts; + else imageFactsLastSent.delete(session); if (facts.nImages <= 0 || billedTotal <= 0) return; const observed = billedTotal - facts.textSide; if (observed <= 0) return; // text estimate overshot the bill — no signal @@ -852,8 +908,12 @@ function detectSeam(session: Session, led: CacheLedger): void { // Substantive unexplained residual only: a big ttlRepay slice of a big bill. if (!(line.tr > 8192 && line.tr > 0.3 * line.input)) return; const agg = led.agg; - const cur = session.transient?.sentBody; + const cur = pendingCapture(session).sentBody; const prev = session.transient?.settledBody; + if (cur?.order !== undefined && prev?.order !== undefined && cur.order <= prev.order) { + line.evidence = "out-of-order"; + return; + } // #2929: split the provider-side arms by idle gap — a byte-stable miss // after a long idle is normal TTL expiry; the same miss seconds after the // previous request is the abnormal provider-side failure. @@ -947,7 +1007,7 @@ function detectSeam(session: Session, led: CacheLedger): void { // a bili rebuild seam. Requires BOTH inbound slots captured whole // (msgs <= INBOUND_MSG_CAP — an above-cap list cannot prove where it // broke, and a missing lane capture must keep the legacy bucket). - const curIn = session.transient?.inBody; + const curIn = pendingCapture(session).inBody; const prevIn = session.transient?.settledInBody; if (curIn !== undefined && prevIn !== undefined && curIn.msgs <= INBOUND_MSG_CAP && prevIn.msgs <= INBOUND_MSG_CAP) { const k = firstDivergentInboundMsg(prevIn, curIn); @@ -1643,10 +1703,10 @@ export function settleUsageReport( // #2131: the credential fingerprint captured at the send chokepoint rides // the seam slot — read it BEFORE recordCacheSample so the line it pushes // already carries the identity (kp/kw) without a second slot lookup. - const curKeyFp = session.transient?.sentBody?.keyFp; + const curKeyFp = pendingCapture(session).sentBody?.keyFp; // #3803: the generation-parameter fingerprint noted for THIS request at the // send chokepoint — same transient-slot pairing as the credential one above. - const curEffFp = session.transient?.sentBody?.effFp; + const curEffFp = pendingCapture(session).sentBody?.effFp; // #2350: the host's system-prompt fingerprint as noted for THIS request by // fold-reconcile's wire sites (they precede this settle under the session // lock). Sparse: lanes that never noted one (title-gen / compaction-trigger @@ -1663,17 +1723,26 @@ export function settleUsageReport( const lastMode = s.protocol === "responses" ? (session.metadata?.[METADATA_LAST_RESPONSES_MODE] as unknown) : undefined; const led = getCacheLedger(session); recordCacheSample(session, { at: Date.now(), input: s.total, cached: s.reportedCached, output: s.output, protocol: s.protocol, upstream: s.upstream, keyFp: curKeyFp, sysFp: curSysFp, canonFp: curCanonFp, effFp: curEffFp, mode: lastMode === "plugin" || lastMode === "proxy" ? lastMode : undefined }); + const capture = captures.getStore(); + const sample = led.lines[led.lines.length - 1]; + if (capture?.session === session && sample) { + sample.captureId = capture.requestId; + sample.captureRound = capture.round; + } // #2131: per-call body-stability proof on the just-recorded line — the same // pairing detectSeam uses (this request's forwarded body vs the previous // settled one), so "prefix unchanged between calls" is stored data, not an // interpretation. Digest equality decides; the LCP offset localizes where // a divergence started when both full bodies were retained. - const curSlot = session.transient?.sentBody; + const curSlot = pendingCapture(session).sentBody; if (curSlot !== undefined) { const curLine = led.lines[led.lines.length - 1]; if (curLine !== undefined) { curLine.bd = curSlot.digest; - const prevSlot = session.transient?.settledBody; + const settled = session.transient?.settledBody; + const backwards = curSlot.order !== undefined && settled?.order !== undefined && curSlot.order <= settled.order; + const prevSlot = backwards ? undefined : settled; + if (backwards) curLine.evidence = "out-of-order"; if (prevSlot !== undefined) { if (curSlot.digest === prevSlot.digest) { curLine.be = 1; @@ -1718,14 +1787,17 @@ export function settleUsageReport( // actually sent (noteForwardedBody), then keep it as the next pair's // baseline. Lanes without body capture still get the aggregate flag. detectSeam(session, led); - const seamBody = session.transient?.sentBody; - if (seamBody !== undefined) (session.transient ??= {}).settledBody = seamBody; - delete session.transient?.sentBody; - // #3023: settle the inbound pair with the same lifecycle — the next - // settle's client arm compares against THIS request's client view. - const seamIn = session.transient?.inBody; - if (seamIn !== undefined) (session.transient ??= {}).settledInBody = seamIn; - delete session.transient?.inBody; + const pending = pendingCapture(session); + const settled = session.transient ??= {}; + const seamBody = pending.sentBody; + const backwards = seamBody?.order !== undefined && settled.settledBody?.order !== undefined && seamBody.order <= settled.settledBody.order; + if (seamBody !== undefined && !backwards) settled.settledBody = seamBody; + else delete settled.settledBody; + delete pending.sentBody; + const seamIn = pending.inBody; + if (seamBody !== undefined && seamIn !== undefined && !backwards) settled.settledInBody = seamIn; + else delete settled.settledInBody; + delete pending.inBody; } /** [#1279] Price profile stamped by the last request (server.ts runPrepare). @@ -1811,7 +1883,7 @@ interface BodyStability { interface BiliCacheReport extends CacheReport { stability: BodyStability; /** #2131: line set widened with the per-call body-stability fields. */ - lines: Array; + lines: Array; modelSwitches: ModelSwitchStats; /** #3803: host generation-parameter switches (reasoning effort / thinking config) — a provider cache-namespace flip that re-bills the whole stable prefix once, distinct from any byte change. */ effSwitches: ModelSwitchStats; @@ -2001,6 +2073,9 @@ export function buildSessionCacheReport(session: Session): BiliCacheReport { foldSeq: l.foldSeq, seam: l.seam, prov: l.prov, + captureId: l.captureId, + captureRound: l.captureRound, + evidence: l.evidence, bodyDigest: l.bd, keyFp: l.kp, sysFp: l.sp, diff --git a/src/loop/core.ts b/src/loop/core.ts index d03120c6c5..8ade4cf853 100644 --- a/src/loop/core.ts +++ b/src/loop/core.ts @@ -8,7 +8,7 @@ import { estimateCoreMessages } from "../preflight.js"; import { estimateWireOverhead } from "../server/budget.js"; import { handleAcpStatus } from "../acp-status.js"; import { STATUS_REPORT_CLOSE, STATUS_REPORT_OPEN } from "../acp-panel.js"; -import { credentialFingerprint, generationParamFp, handleAcpCache, noteForwardedBody, noteForwardedImageFacts, settleUsageReport } from "../cache-ledger.js"; +import { credentialFingerprint, generationParamFp, handleAcpCache, lastForwardedBody, noteForwardedBody, noteForwardedImageFacts, settleUsageReport } from "../cache-ledger.js"; import { maybeJudgeContinuity } from "../invariant-judge.js"; import { countImagesInRawBody } from "../image-tokens.js"; import { diagnoseSuccessWithoutUsage, lastCompressSuffix, withSessionLock, type Session } from "../session.js"; @@ -506,7 +506,7 @@ export async function* runCompressLoop( // overwrites the slot; clipped/oversized bodies retain no full copy and // the continuity check silently skips (no false positives from partial // forensics). Advisory; never blocks. - const prevSentFull = ctx.session.transient?.sentBody?.full ?? null; + const prevSentFull = lastForwardedBody(ctx.session)?.full ?? null; // #3803: generation-parameter fingerprint from the full wire object (never the capped head) — // a host reasoning-effort / thinking-config flip re-partitions the provider cache namespace. noteForwardedBody(ctx.session, wireBodyStr, wireArr !== undefined ? wireArr.length : null, credentialFingerprint(requestOptions.headers), generationParamFp(wireObj)); diff --git a/src/server/relay.ts b/src/server/relay.ts index b7ecbdfea3..ca914cb66a 100644 --- a/src/server/relay.ts +++ b/src/server/relay.ts @@ -10,7 +10,7 @@ import { type BiliMessage } from "acp-kernel/wire"; import { absorbToolName, applyAbsorbView } from "../absorb.js"; import { APIG_RESIGN_SCHEME, resignApig } from "../apig-resign.js"; import { noteSuccessfulForward } from "../cache-keepalive.js"; -import { credentialFingerprint, generationParamFp, noteClientAbort, noteForwardedBody, noteForwardedImageFacts, noteInboundMessages, settleOverflowCalibration, settleUsageReport } from "../cache-ledger.js"; +import { credentialFingerprint, generationParamFp, noteClientAbort, noteForwardedBody, noteForwardedImageFacts, noteInboundMessages, settleOverflowCalibration, settleUsageReport, withCacheCapture } from "../cache-ledger.js"; import { warnCacheCollapse } from "../cache-warn.js"; import { stampOutbound } from "../chain-checkpoint.js"; import { applyCompatDropFields, dropCompatFieldsJson, resolveCompatDropFields } from "../compat-drop.js"; @@ -243,7 +243,11 @@ export function armFirstByteWatchdog(o: { return timer; } -export async function forward( +export function forward(...args: Parameters): Promise { + return withCacheCapture(args[4]?.session, () => forwardCaptured(...args)); +} + +async function forwardCaptured( req: http.IncomingMessage, res: http.ServerResponse, opts: ProxyOptions, @@ -2034,11 +2038,7 @@ export async function forward( const reportedCached: number | null = typeof cached === "number" ? cached : null; const billed = typeof total === "number" ? total : 0; if (billed > 0 || reportedCached !== null) { - // wireBody — not prepared.body — is what actually went out - // (compat-role / steering / chain-stamp rewrites apply after - // prepare); the main chokepoint above already noted it, this - // keeps the pair byte-exact if that ever moves (#1891). - noteForwardedBody(prepared.session, typeof wireBody === "string" ? wireBody : wireBody.toString("utf8")); + // Use the send-time capture; settlement is not another send. settleUsageReport(prepared.session, { total: billed, reportedCached, output: out, protocol: prepared.protocol, upstream: targetOrigin }); if (reportedCached !== null) warnCacheCollapse(prepared.session, billed, reportedCached); const hitPct = reportedCached !== null && billed > 0 ? Math.round((100 * reportedCached) / billed) : undefined; diff --git a/tests/cache-capture-http.test.ts b/tests/cache-capture-http.test.ts new file mode 100644 index 0000000000..ed5fc42d3b --- /dev/null +++ b/tests/cache-capture-http.test.ts @@ -0,0 +1,120 @@ +import { after, test } from "node:test"; +import { mkdtempSync } from "node:fs"; +import { tmpdir } from "node:os"; +import path from "node:path"; +import { rmrf } from "./tmp-rm.js"; +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import { createHash } from "node:crypto"; +import { defaultConfig } from "acp-kernel"; +import { startServer } from "../src/server.ts"; +import type { ProxyOptions } from "../src/config.ts"; +import { getSession } from "../src/session.ts"; +import { credentialFingerprint, generationParamFp, getCacheLedger } from "../src/cache-ledger.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { _setForTest as setRegistryForTest } from "../src/registry.ts"; + +const testData = mkdtempSync(path.join(tmpdir(), "bili-capture-")); +const priorData = process.env.XDG_DATA_HOME; +const priorStorage = process.env.BILI_STORAGE_UNIFIED; +process.env.XDG_DATA_HOME = testData; +process.env.BILI_STORAGE_UNIFIED = "legacy"; +_setStoreForTest(new SessionStore({ enabled: false })); +after(() => { + if (priorData === undefined) delete process.env.XDG_DATA_HOME; + else process.env.XDG_DATA_HOME = priorData; + if (priorStorage === undefined) delete process.env.BILI_STORAGE_UNIFIED; + else process.env.BILI_STORAGE_UNIFIED = priorStorage; + rmrf(testData); +}); + +function gate() { + let open!: () => void; + const wait = new Promise((resolve) => { open = resolve; }); + return { wait, open }; +} +async function close(server: http.Server): Promise { + server.closeAllConnections(); + await new Promise((resolve, reject) => server.close((error) => error ? reject(error) : resolve())); +} + +for (const plugin of [false, true]) for (const stream of [false, true]) for (const reverse of [false, true]) { + test(`real proxy pairs overlapping same-session forwards; plugin=${plugin}, stream=${stream}, reverse=${reverse}`, { timeout: 30_000 }, async () => { + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + const ready = [gate(), gate()], release = [gate(), gate()]; + const sent: string[] = []; + const sockets = new Set(); + const upstream = http.createServer(async (req, res) => { + const chunks: Buffer[] = []; + for await (const chunk of req) chunks.push(Buffer.from(chunk)); + const index = sent.length; + assert.ok(index < 2, "no hidden upstream request"); + sent.push(Buffer.concat(chunks).toString("utf8")); + sockets.add(req.socket); + ready[index].open(); + await release[index].wait; + const usage = { prompt_tokens: 100_000 + index * 100, completion_tokens: 11 + index, total_tokens: 100_011 + index * 101, prompt_tokens_details: { cached_tokens: index === 0 ? 99_000 : 20_000 } }; + const message = { id: `chatcmpl-${index}`, object: "chat.completion", model: "gpt-test", choices: [{ index: 0, message: { role: "assistant", content: `answer ${index}` }, finish_reason: "stop" }], usage }; + res.writeHead(200, { "content-type": stream ? "text/event-stream" : "application/json" }); + if (stream) { + res.write(`data: ${JSON.stringify({ ...message, object: "chat.completion.chunk", choices: [{ index: 0, delta: { content: `answer ${index}` }, finish_reason: "stop" }] })}\n\n`); + res.end("data: [DONE]\n\n"); + } else res.end(JSON.stringify(message)); + }); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const origin = `http://127.0.0.1:${(upstream.address() as { port: number }).port}`; + const opts: ProxyOptions = { + port: 0, host: "127.0.0.1", upstream: origin, + routes: { [origin]: { models: { "gpt-test": { context: 400_000 } } } }, + modelContextLimit: 400_000, kernelConfig: defaultConfig(400_000), + compress: { injectTool: true, injectNudge: true }, promptCache: { routing: "auto" }, + sessionHeader: "x-acp-session", log: false, debug: false, + passthrough: false, passthroughSource: null, autoUpdate: false, autoRestartOnUpdate: false, + updateTag: "latest", advisoryCheck: false, releaseNotesCheck: false, + compat: { roles: {} }, streamErrorShape: "protocol", mitm: { enabled: false, domains: [] }, + }; + const proxy = await startServer(opts); + if (!proxy.listening) await once(proxy, "listening"); + const sessionId = `capture-http-${plugin}-${stream}-${reverse}`; + const url = `http://127.0.0.1:${(proxy.address() as { port: number }).port}/bili/${origin}/v1/chat/completions`; + const post = async (label: string): Promise => { + const response = await fetch(url, { + method: "POST", signal: AbortSignal.timeout(20_000), + headers: { ...(plugin ? { "x-bili-plugin": "test-agent" } : {}), "content-type": "application/json", "x-acp-session": sessionId, connection: "close", authorization: `Bearer synthetic-${label}` }, + body: JSON.stringify({ model: "gpt-test", stream, temperature: label === "request A" ? 0.1 : 0.9, messages: [{ role: "user", content: label }], tools: [{ type: "function", function: { name: "noop", parameters: { type: "object" } } }] }), + }); + assert.equal(response.status, 200); + assert.match(await response.text(), /answer/); + }; + try { + const a = post("request A"); + await ready[0].wait; + const b = post("request BBBB"); + await ready[1].wait; + assert.equal(sockets.size, 2, "two same-session socket requests overlap before either settles"); + if (reverse) { release[1].open(); await b; release[0].open(); await a; } + else { release[0].open(); await a; release[1].open(); await b; } + const lines = getCacheLedger(getSession(sessionId)).lines; + assert.equal(lines.length, 2); + for (let index = 0; index < 2; index++) { + const line = lines.find((sample) => sample.output === 11 + index)!; + assert.ok(line, "both usage records settled"); + assert.equal(line.bd, createHash("sha256").update(sent[index]).digest("hex")); + assert.equal(line.kp, credentialFingerprint({ authorization: `Bearer synthetic-${index === 0 ? "request A" : "request BBBB"}` })); + assert.equal(line.efp, generationParamFp(JSON.parse(sent[index]))); + assert.equal(line.captureRound, 1); + assert.ok(line.captureId); + } + assert.notEqual(lines[0].captureId, lines[1].captureId); + } finally { + for (const pending of release) pending.open(); + await close(proxy); + await close(upstream); + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + } + }); +} diff --git a/tests/cache-capture-pairing.test.ts b/tests/cache-capture-pairing.test.ts new file mode 100644 index 0000000000..eaaabdf4cc --- /dev/null +++ b/tests/cache-capture-pairing.test.ts @@ -0,0 +1,166 @@ +import { after, test } from "node:test"; +import { mkdtempSync } from "node:fs"; +import { tmpdir } from "node:os"; +import path from "node:path"; +import { rmrf } from "./tmp-rm.js"; +import assert from "node:assert/strict"; +import { createHash } from "node:crypto"; +import type { CoreMessage } from "acp-kernel"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import type { Session } from "../src/session.ts"; +import { buildSessionCacheReport, getCacheLedger, lastForwardedBody, noteForwardedBody, noteForwardedImageFacts, noteInboundMessages, settleUsageReport, withCacheCapture } from "../src/cache-ledger.ts"; + +const testData = mkdtempSync(path.join(tmpdir(), "bili-capture-")); +const priorData = process.env.XDG_DATA_HOME; +const priorStorage = process.env.BILI_STORAGE_UNIFIED; +process.env.XDG_DATA_HOME = testData; +process.env.BILI_STORAGE_UNIFIED = "legacy"; +_setStoreForTest(new SessionStore({ enabled: false })); +after(() => { + if (priorData === undefined) delete process.env.XDG_DATA_HOME; + else process.env.XDG_DATA_HOME = priorData; + if (priorStorage === undefined) delete process.env.BILI_STORAGE_UNIFIED; + else process.env.BILI_STORAGE_UNIFIED = priorStorage; + rmrf(testData); +}); + +let seq = 0; +const session = (): Session => ({ id: `capture-${++seq}`, metadata: {}, stats: {}, state: { blocks: [], messageRefs: { byRaw: {}, byRef: {} }, tokenSnapshot: {} } }) as unknown as Session; +const incoming = (id: string, text = "same"): CoreMessage[] => [{ id, role: "user", contentType: "text", text }]; +const body = (text: string): string => JSON.stringify({ messages: [{ role: "user", content: text }] }); +const digest = (text: string): string => createHash("sha256").update(text).digest("hex"); +const settle = (s: Session, total = 100_000, cached = 20_000): void => settleUsageReport(s, { total, reportedCached: cached, output: 1 }); +function gate() { + let open!: () => void; + const wait = new Promise((resolve) => { open = resolve; }); + return { wait, open }; +} + +for (const reverse of [false, true]) { + test(`overlapping captures keep body, inbound and image evidence paired; reverse=${reverse}`, async () => { + const s = session(); + const readyA = gate(), readyB = gate(), doneA = gate(), doneB = gate(); + const run = (label: string, ready: ReturnType, done: ReturnType) => withCacheCapture(s, async () => { + const wire = body(label); + noteForwardedBody(s, wire); + noteInboundMessages(s, incoming(label, label)); + noteForwardedImageFacts(s, { host: label, fp: label, nImages: 1, textSide: 99_000 }); + ready.open(); + await done.wait; + assert.equal(lastForwardedBody(s)?.digest, digest(wire)); + settle(s, 100_000, 99_000); + const line = getCacheLedger(s).lines.at(-1)!; + assert.equal(line.bd, digest(wire)); + assert.equal(line.captureRound, 1); + if (reverse && label === "A") assert.equal(s.transient?.settledInBody, undefined); + else assert.equal(s.transient?.settledInBody?.meta[0]?.len, JSON.stringify(incoming(label, label)[0]).length); + assert.ok(s.metadata.learnedImageCosts && typeof s.metadata.learnedImageCosts === "object" && label in s.metadata.learnedImageCosts); + }); + const a = run("A", readyA, doneA); + await readyA.wait; + const b = run("BBBB", readyB, doneB); + await readyB.wait; + if (reverse) { doneB.open(); await b; doneA.open(); await a; } + else { doneA.open(); await a; doneB.open(); await b; } + const lines = getCacheLedger(s).lines; + assert.notEqual(lines[0].captureId, lines[1].captureId); + assert.equal(s.transient?.sentBody, undefined); + }); +} + + +test("internal rounds keep request identity without stale inbound evidence", async () => { + const s = session(); + await withCacheCapture(s, async () => { + noteForwardedBody(s, body("first")); + noteInboundMessages(s, incoming("first")); + settle(s, 100_000, 99_000); + noteForwardedBody(s, body("second")); + settle(s, 100_000, 99_000); + }); + const [a, b] = getCacheLedger(s).lines; + assert.equal(a.captureId, b.captureId); + assert.equal(a.captureRound, 1); + assert.equal(b.captureRound, 2); + assert.equal(s.transient?.settledInBody, undefined); +}); + +test("aborted capture cannot leak body or image learning into the next forward", async () => { + const s = session(); + await assert.rejects(withCacheCapture(s, async () => { + noteForwardedBody(s, body("aborted")); + noteInboundMessages(s, incoming("aborted")); + noteForwardedImageFacts(s, { host: "aborted", fp: "a", nImages: 1, textSide: 99_000 }); + throw new Error("synthetic abort"); + }), /synthetic abort/); + await withCacheCapture(s, async () => { + assert.equal(lastForwardedBody(s), undefined); + settle(s, 100_000, 99_000); + }); + assert.equal(getCacheLedger(s).lines.at(-1)?.bd, undefined); + assert.equal(s.metadata.learnedImageCosts, undefined); + assert.equal(s.transient?.settledBody, undefined); +}); + +test("reversed completion clears comparison evidence instead of reporting a seam", async () => { + const s = session(), ready = gate(), release = gate(); + const a = withCacheCapture(s, async () => { + noteForwardedBody(s, body("older")); + noteInboundMessages(s, incoming("a")); + ready.open(); + await release.wait; + settle(s); + }); + await ready.wait; + await withCacheCapture(s, async () => { + noteForwardedBody(s, body("newer")); + noteInboundMessages(s, incoming("b")); + settle(s, 100_000, 99_000); + }); + release.open(); + await a; + const line = getCacheLedger(s).lines.at(-1)!; + assert.equal(line.evidence, "out-of-order"); + assert.equal(line.be, undefined); + assert.equal(line.seam, undefined); + assert.equal(line.prov, undefined); + assert.equal(buildSessionCacheReport(s).lines.at(-1)?.evidence, "out-of-order"); + assert.equal(s.transient?.settledBody, undefined); + assert.equal(s.transient?.settledInBody, undefined); +}); + +test("legacy callers and undefined sessions keep their capture behavior", async () => { + const s = session(); + await withCacheCapture(undefined, async () => { + noteForwardedBody(s, body("legacy"), 1, "credential", "params"); + noteInboundMessages(s, incoming("legacy")); + settle(s, 100_000, 99_000); + }); + const line = getCacheLedger(s).lines.at(-1)!; + assert.equal(line.bd, digest(body("legacy"))); + assert.equal(line.kp, "credential"); + assert.equal(line.efp, "params"); + assert.equal(line.captureId, undefined); + assert.ok(s.transient?.settledInBody); +}); + +test("an unbilled image round cannot teach the next image-free round", async () => { + const s = session(); + await withCacheCapture(s, async () => { + noteForwardedBody(s, body("image round")); + noteForwardedImageFacts(s, { host: "old", fp: "a", nImages: 1, textSide: 99_000 }); + noteForwardedBody(s, body("retry without images")); + settle(s, 100_000, 99_000); + }); + assert.equal(s.metadata.learnedImageCosts, undefined); +}); + +test("relay image facts captured before the first body survive until usage", async () => { + const s = session(); + await withCacheCapture(s, async () => { + noteForwardedImageFacts(s, { host: "relay", fp: "a", nImages: 1, textSide: 99_000 }); + noteForwardedBody(s, body("first")); + settle(s, 100_000, 99_000); + }); + assert.ok(s.metadata.learnedImageCosts && typeof s.metadata.learnedImageCosts === "object" && "relay" in s.metadata.learnedImageCosts); +});