diff --git a/src/cache-ledger.ts b/src/cache-ledger.ts index 38f4fec163..21bf017396 100644 --- a/src/cache-ledger.ts +++ b/src/cache-ledger.ts @@ -129,6 +129,14 @@ interface LedgerLine { /** #1592 follow-up: 1 iff this sample settled within 30s of a client * mid-stream abort in the same session (abort/retry churn correlation). */ abortedNear?: 1; + /** #4397: 1 iff bili's render tags were active (render strategy != "none") + * when this request's body was sent out — stamped from the seam slot at + * settle time. A provider-side miss carrying this flag is the #2483 + * stateful-relay signature: a relay that validates echoed history against + * its own transcript rejects it when tags appear on assistant text, so + * the whole prefix re-bills every turn while bili's bytes stay stable. + * Sparse: omitted unless set. */ + rt?: 1; /** #2929: which attribution arm detectSeam booked this sample's residual * into — "ttl" (long-idle byte-stable miss, normal ageing), "side" * (short-idle byte-stable miss, abnormal provider failure), "rewind" @@ -301,6 +309,11 @@ interface CacheLedger { * cache (TTL expiry / eviction / relay node rotation). Not a rebuild seam. */ providerSideMisses: number; providerSideMissed: number; + /** #4397: subset of the provider-side misses sent out WITH bili's + * render tags active — the #2483 stateful-relay signature (the relay + * rejects tag-carrying echoed history against its own transcript). */ + taggedSideMisses: number; + taggedSideMissed: number; /** #3023: client-side mid-history rewrites — the inbound pair broke * mid-list (host-side insert/rewrite: another plugin, paste hoisting, * host re-rendering), so the residual belongs to the host, not to a @@ -463,6 +476,10 @@ export interface SeamSlot { * next loop round's continuity verdict reads them as entry-side ownership. * Preserved across byte-identical re-notes like keyFp. */ ownedSeparatorCores?: ReadonlySet; + /** #4397/#2483: 1 iff render tags were active when this body went out + * (render strategy != "none"). Preserved across byte-identical re-notes + * like keyFp/effFp. */ + rt?: 1; } // #3023: message-count ceiling for the inbound pair. The per-message @@ -759,8 +776,10 @@ export function generationParamFp(body: Record | null | undefin * settle re-noting the same wireBody), the earlier count is kept. `keyFp` is * the outbound credential fingerprint (#2131) — likewise inherited from the * prior slot on a byte-identical re-note without a fresh capture. `effFp` is - * the generation-parameter fingerprint (#3803) — same inheritance rule. */ -export function noteForwardedBody(session: Session, body: string, msgs?: number | null, keyFp?: string, effFp?: string): void { + * the generation-parameter fingerprint (#3803) — same inheritance rule. + * `renderTags` (#4397) stamps whether bili's render tags were active when the + * body went out — same inheritance rule. */ +export function noteForwardedBody(session: Session, body: string, msgs?: number | null, keyFp?: string, effFp?: string, renderTags?: boolean): void { // #2868: encode to UTF-8 ONCE here — digest, capped head and retained full // body all consume this same buffer, so no later comparison re-encodes. // Caps are BYTES (the old code-unit caps overshot them up to 3× on CJK). @@ -787,6 +806,7 @@ export function noteForwardedBody(session: Session, body: string, msgs?: number keyFp: keyFp ?? (samePayload ? prior.keyFp : undefined), effFp: effFp ?? (samePayload ? prior.effFp : undefined), ownedSeparatorCores: samePayload ? prior.ownedSeparatorCores : undefined, + rt: renderTags ? 1 : samePayload ? prior.rt : undefined, }; if ("round" in transient && transient.round > 1) { delete transient.imageFacts; @@ -1109,6 +1129,10 @@ function detectSeam(session: Session, led: CacheLedger): void { } else { agg.providerSideMisses += 1; agg.providerSideMissed += line.tr; + if (line.rt === 1) { + agg.taggedSideMisses += 1; + agg.taggedSideMissed += line.tr; + } } }; let f: ReturnType | undefined; @@ -1257,7 +1281,7 @@ export function getCacheLedger(session: Session): CacheLedger { "restartDropMissed", "attributedMissed", "unknownSamples", "unknownInput", "nbSamples", "nbInput", "seamSuspects", "seamMissed", - "providerSideMisses", "providerSideMissed", "providerTtlMisses", "providerTtlMissed", "rewinds", "rewindMissed", "abortCorrelated", "clientInserts", "clientInsertMissed", "orphanSweepSeams", "orphanSweepMissed", + "providerSideMisses", "providerSideMissed", "taggedSideMisses", "taggedSideMissed", "providerTtlMisses", "providerTtlMissed", "rewinds", "rewindMissed", "abortCorrelated", "clientInserts", "clientInsertMissed", "orphanSweepSeams", "orphanSweepMissed", ] as const) { if (typeof g[key] !== "number") g[key] = 0; } @@ -1274,7 +1298,7 @@ export function getCacheLedger(session: Session): CacheLedger { foldSeqCounter: 0, folds: [], lines: [], - agg: { requests: 0, input: 0, cached: 0, output: 0, nc: 0, cr: 0, tr: 0, switches: 0, switchMissed: 0, effSwitches: 0, effSwitchMissed: 0, keySwitches: 0, keySwitchMissed: 0, promptSwitches: 0, promptSwitchMissed: 0, projSwitches: 0, projSwitchMissed: 0, wireSwitches: 0, wireSwitchMissed: 0, upstreamSwitches: 0, upstreamSwitchMissed: 0, restartDrops: 0, restartDropMissed: 0, attributedMissed: 0, unknownSamples: 0, unknownInput: 0, nbSamples: 0, nbInput: 0, seamSuspects: 0, seamMissed: 0, providerSideMisses: 0, providerSideMissed: 0, providerTtlMisses: 0, providerTtlMissed: 0, rewinds: 0, rewindMissed: 0, abortCorrelated: 0, clientInserts: 0, clientInsertMissed: 0, orphanSweepSeams: 0, orphanSweepMissed: 0 }, + agg: { requests: 0, input: 0, cached: 0, output: 0, nc: 0, cr: 0, tr: 0, switches: 0, switchMissed: 0, effSwitches: 0, effSwitchMissed: 0, keySwitches: 0, keySwitchMissed: 0, promptSwitches: 0, promptSwitchMissed: 0, projSwitches: 0, projSwitchMissed: 0, wireSwitches: 0, wireSwitchMissed: 0, upstreamSwitches: 0, upstreamSwitchMissed: 0, restartDrops: 0, restartDropMissed: 0, attributedMissed: 0, unknownSamples: 0, unknownInput: 0, nbSamples: 0, nbInput: 0, seamSuspects: 0, seamMissed: 0, providerSideMisses: 0, providerSideMissed: 0, taggedSideMisses: 0, taggedSideMissed: 0, providerTtlMisses: 0, providerTtlMissed: 0, rewinds: 0, rewindMissed: 0, abortCorrelated: 0, clientInserts: 0, clientInsertMissed: 0, orphanSweepSeams: 0, orphanSweepMissed: 0 }, }; meta[LEDGER_KEY] = led; return led; @@ -1403,7 +1427,7 @@ function detectOrphanSweeps(session: Session, led: CacheLedger): void { * billed prefix as an unexplained ttlRepay residual (#1536). */ export function recordCacheSample( session: Session, - s: { at: number; input: number; cached: number | null; output?: number; protocol?: string; upstream?: string; keyFp?: string; sysFp?: string; canonFp?: string; mode?: "plugin" | "proxy"; effFp?: string }, + s: { at: number; input: number; cached: number | null; output?: number; protocol?: string; upstream?: string; keyFp?: string; sysFp?: string; canonFp?: string; mode?: "plugin" | "proxy"; effFp?: string; rt?: 1 }, ): void { const led = getCacheLedger(session); detectNewFolds(session, led); @@ -1678,6 +1702,7 @@ export function recordCacheSample( rs: restarted ? 1 : undefined, unk: known ? undefined : 1, nb: noBaseline ? 1 : undefined, + rt: s.rt, cause: known && cause !== null ? cause : undefined, }); led.lastBoot = BOOT_ID; @@ -2023,6 +2048,8 @@ export function settleUsageReport( // #3803: the generation-parameter fingerprint noted for THIS request at the // send chokepoint — same transient-slot pairing as the credential one above. const curEffFp = pendingCapture(session).sentBody?.effFp; + // #4397/#2483: render-tags-active stamp for THIS request — same pairing. + const curRt = pendingCapture(session).sentBody?.rt; // #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 @@ -2038,7 +2065,7 @@ export function settleUsageReport( // responses request's label into an inert-but-misleading line field. 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 }); + 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, rt: curRt, mode: lastMode === "plugin" || lastMode === "proxy" ? lastMode : undefined }); const capture = captures.getStore(); const sample = led.lines[led.lines.length - 1]; if (capture?.session === session && sample) { @@ -2246,7 +2273,7 @@ interface BiliCacheReport extends CacheReport { * is booked as new content, never as a prefix re-pay. */ initialBills: { samples: number; inputTokens: number }; invalidation: InvalidationTokenBreakdown; - seam: { suspects: number; missed: number; events: SeamEvent[]; providerSide: { count: number; missed: number }; providerTtl: { count: number; missed: number }; rewinds: { count: number; missed: number }; abortCorrelated: number; client: { count: number; missed: number }; orphanSweep: { count: number; missed: number } }; + seam: { suspects: number; missed: number; events: SeamEvent[]; providerSide: { count: number; missed: number }; taggedSide: { count: number; missed: number }; providerTtl: { count: number; missed: number }; rewinds: { count: number; missed: number }; abortCorrelated: number; client: { count: number; missed: number }; orphanSweep: { count: number; missed: number } }; } export function buildSessionCacheReport(session: Session): BiliCacheReport { @@ -2534,7 +2561,7 @@ export function buildSessionCacheReport(session: Session): BiliCacheReport { invalidation, stability, recovery, - seam: { suspects: a.seamSuspects, missed: a.seamMissed, events: led.seamEvents ?? [], providerSide: { count: a.providerSideMisses, missed: a.providerSideMissed }, providerTtl: { count: a.providerTtlMisses, missed: a.providerTtlMissed }, rewinds: { count: a.rewinds, missed: a.rewindMissed }, abortCorrelated: a.abortCorrelated, client: { count: a.clientInserts, missed: a.clientInsertMissed }, orphanSweep: { count: a.orphanSweepSeams, missed: a.orphanSweepMissed } }, + seam: { suspects: a.seamSuspects, missed: a.seamMissed, events: led.seamEvents ?? [], providerSide: { count: a.providerSideMisses, missed: a.providerSideMissed }, taggedSide: { count: a.taggedSideMisses, missed: a.taggedSideMissed }, providerTtl: { count: a.providerTtlMisses, missed: a.providerTtlMissed }, rewinds: { count: a.rewinds, missed: a.rewindMissed }, abortCorrelated: a.abortCorrelated, client: { count: a.clientInserts, missed: a.clientInsertMissed }, orphanSweep: { count: a.orphanSweepSeams, missed: a.orphanSweepMissed } }, }; } @@ -2712,6 +2739,9 @@ function formatSeam(r: BiliCacheReport): string { out.push("▲ PROVIDER-SIDE MISS (previous request's message list fully preserved)"); out.push(` ${r.seam.providerSide.count} sample(s) · ${fmtTok(r.seam.providerSide.missed)} tok — the previous request's message list is preserved in this one (byte-for-byte under the 512 KiB forensics cap; above it, proven by the stable recorded head plus exact message-count growth — any difference is only the appended tail), and the sample arrived within ${Math.round(PROVIDER_TTL_GAP_MS / 60_000)} min of the previous one, so this is an ABNORMAL provider miss (eviction / relay node rotation), not TTL ageing. Not a bili rebuild seam.`); out.push(` stateful-upstream check (#2483): if your upstream keeps a live session per conversation and validates the echoed history against its own transcript (claude-code-based relays such as magpie), bili's render tags on assistant text trip that check even though bili's own list is unchanged — set diagnostics.renderNone=true until per-route render-tags control ships; expect exactly ONE replay when flipping the flag mid-session`); + if (r.seam.taggedSide.count > 0) { + out.push(` of which ${r.seam.taggedSide.count} sample(s) · ${fmtTok(r.seam.taggedSide.missed)} tok were sent WITH bili render tags active (#4397): the #2483 signature measured in-session — flip diagnostics.renderNone=true and this bucket stops growing on the next turns`); + } } if (r.seam.client.count > 0) { out.push("✎ CLIENT MID-HISTORY REWRITE (host-side insert — NOT a bili seam)"); @@ -2745,14 +2775,36 @@ function formatInvalidation(r: BiliCacheReport): string { out.push(` wire switch: ${fmtTok(b.wire)} (${r.wireSwitches.count})`); out.push(` upstream switch: ${fmtTok(b.upstream)} (${r.upstreamSwitches.count})`); out.push(` restart/refork: ${fmtTok(b.restart)} (${r.restartDrops.count})`); + // #4397: split the opaque residual over the body-forensics buckets. Every + // bucket below is a SUBSET of b.remaining by construction: named-cause + // lines never reach detectSeam, and each forensics arm books line.tr once. + const forensics = [ + { label: "provider-side miss", count: r.seam.providerSide.count, missed: r.seam.providerSide.missed }, + { label: "provider TTL expiry", count: r.seam.providerTtl.count, missed: r.seam.providerTtl.missed }, + { label: "client mid-history rewrite", count: r.seam.client.count, missed: r.seam.client.missed }, + { label: "history rewind/trim", count: r.seam.rewinds.count, missed: r.seam.rewinds.missed }, + { label: "orphan cleanup sweep", count: r.seam.orphanSweep.count, missed: r.seam.orphanSweep.missed }, + { label: "suspected mid-history break", count: r.seam.suspects, missed: r.seam.missed }, + ].filter((f) => f.count > 0); + if (forensics.length > 0 && b.remaining > 0) { + const rest = Math.max(0, b.remaining - forensics.reduce((n, f) => n + f.missed, 0)); + out.push(` of which the unattributed ${fmtTok(b.remaining)} tok: body forensics booked ${forensics.map((f) => `${f.label} ${fmtTok(f.missed)} (${f.count})`).join(" · ")}` + (rest > 0 ? ` · ${fmtTok(rest)} below the forensics gate or lane without body capture` : "")); + } // #1847: a dominant unattributed residual with NO observed cause is the misleading case — the README // triage order would steer users to "③ bili bug". Name it explicitly as expected provider-side behavior. if (b.remaining > 0 && total > 0 && b.remaining / total >= 0.5) { const pct = Math.round((b.remaining / total) * 100); const noCauseEver = r.modelSwitches.count === 0 && r.effSwitches.count === 0 && r.keySwitches.count === 0 && r.promptSwitches.count === 0 && r.projSwitches.count === 0 && r.wireSwitches.count === 0 && r.upstreamSwitches.count === 0 && r.restartDrops.count === 0; - out.push(noCauseEver - ? ` ⚠ ${pct}% of your stable-prefix re-bill has no observable cause (no model/wire/upstream switch or restart seen) — expected provider-side behavior (cache TTL expiry / eviction / relay rotation), NOT a bili bug; if reproducible see #1195 coverage-mismatch` - : ` ⚠ ${pct}% of your stable-prefix re-bill is unnameable provider-side behavior (cache TTL expiry / eviction / relay rotation) beyond the causes listed above`); + // #4397: when body forensics actually suspect a mid-history break, the + // blanket "NOT a bili bug" line misdirects — report the split instead. + const susTok = r.seam.suspects > 0 ? r.seam.missed : 0; + if (susTok > 0 && susTok >= 0.1 * b.remaining) { + out.push(` ⚠ ${pct}% of your stable-prefix re-bill has no observable switch cause — body forensics split it: ${fmtTok(b.remaining - susTok)} tok non-bili (provider-side miss / TTL / host rewrite — breakdown above) · ${fmtTok(susTok)} tok still a suspected mid-history break (see CACHE SEAM section; attach raw dumps to attribute it, #4397)`); + } else { + out.push(noCauseEver + ? ` ⚠ ${pct}% of your stable-prefix re-bill has no observable cause (no model/wire/upstream switch or restart seen) — expected provider-side behavior (cache TTL expiry / eviction / relay rotation), NOT a bili bug; if reproducible see #1195 coverage-mismatch` + : ` ⚠ ${pct}% of your stable-prefix re-bill is unnameable provider-side behavior (cache TTL expiry / eviction / relay rotation) beyond the causes listed above`); + } } if (r.unmeasured.samples > 0) { out.push(` unmeasured (provider reported no cache tokens): ${r.unmeasured.samples} sample(s) · ${fmtTok(r.unmeasured.inputTokens)} tok — excluded from hit rate`); diff --git a/src/fold-reconcile.ts b/src/fold-reconcile.ts index 81bcea8277..f8692c5cbb 100644 --- a/src/fold-reconcile.ts +++ b/src/fold-reconcile.ts @@ -2542,7 +2542,7 @@ export function noteSystemPromptFingerprint(session: Session, system: unknown, o const prev = session.metadata[METADATA_SYSTEM_FP] as { fp: string; size: number } | undefined; if (prev !== undefined && prev.fp !== fp) { if (opts.log !== undefined) { - opts.log("info", `${opts.sessionId === undefined ? "" : `[${opts.sessionId}] `}[fold-reconcile] system prompt changed (${prev.size} → ${text.length} chars); fold state unaffected (#1921)`); + opts.log("info", `${opts.sessionId === undefined ? "" : `[${opts.sessionId}] `}[fold-reconcile] system prompt changed (${prev.size} → ${text.length} chars of the INBOUND client system/developer bundle, pre-injection — not the wire system); fold state unaffected (#1921)`); } } session.metadata[METADATA_SYSTEM_FP] = { fp, size: text.length }; diff --git a/src/loop/core.ts b/src/loop/core.ts index fa301f487c..509e522cba 100644 --- a/src/loop/core.ts +++ b/src/loop/core.ts @@ -42,7 +42,7 @@ import { warnCacheCollapse } from "../cache-warn.js"; import { dumpRejectedBody } from "../error-dump.js"; import { shedEgressIfOverBudget } from "../egress-shed.js"; import { DUMP_PLAINTEXT_BANNER } from "../dump-banner.js"; -import { bodyDumpEnabled as knobBodyDumpEnabled, imageRescaleMaxDim } from "../knobs.js"; +import { bodyDumpEnabled as knobBodyDumpEnabled, imageRescaleMaxDim, renderNone as knobRenderNone } from "../knobs.js"; import { dumpsDir } from "../paths.js"; import { isStrictReasoningEcho, modelIdOf, normalizeStrictEchoBody } from "../strict-echo.js"; import { log as loggerLog } from "../logger.js"; @@ -285,6 +285,10 @@ export interface LoopCtx { * host = upstream hostname, fp = `${billing}:${cap}` fingerprint. When * present, each sent round captures its image facts alongside the body. */ imageLearn?: { host: string; fp: string }; + /** #4397: the per-request render-tag strategy that produced this loop's outbound + * bodies (the global knob can diverge — compaction-trigger requests force "none"). + * Stamps noteForwardedBody's rt flag; undefined falls back to the knob. */ + renderTags?: "text-only" | "none"; } export interface RequestOptions { @@ -643,7 +647,9 @@ export async function* runCompressLoop( // noteForwardedBody is a string-only forensics sink; an opaque egress // envelope (Buffer) is lossy-converted — advisory pairing only, and the // provenance judge below already skips when the body was rewritten. - noteForwardedBody(ctx.session, typeof wireBodyStr === "string" ? wireBodyStr : wireBodyStr.toString("utf8"), wireArr !== undefined ? wireArr.length : null, credentialFingerprint(requestOptions.headers), generationParamFp(wireObj)); + // #4397: stamp whether bili's render tags were active on THIS body (the #2483 suspect flag) + // — per-request strategy via ctx (see LoopCtx.renderTags), knob fallback where absent. + noteForwardedBody(ctx.session, typeof wireBodyStr === "string" ? wireBodyStr : wireBodyStr.toString("utf8"), wireArr !== undefined ? wireArr.length : null, credentialFingerprint(requestOptions.headers), generationParamFp(wireObj), ctx.renderTags !== undefined ? ctx.renderTags !== "none" : !knobRenderNone()); // #564/#2627: provenance is resolved against the array that belongs to // `wireBodyStr`. Shedding rewrites the body wholesale, so the pre-shed // `wireArr` no longer describes it; rather than map stale indexes (which diff --git a/src/server/relay.ts b/src/server/relay.ts index 0bc270a470..d50402e883 100644 --- a/src/server/relay.ts +++ b/src/server/relay.ts @@ -38,7 +38,7 @@ import { currentFetchTransport } from "../fetch-transport.js"; import { refForItemBytes } from "../image-rescale.js"; import { countImagesInParsedBody, countImagesInRawBody, upstreamHost } from "../image-tokens.js"; import { log as loggerLog } from "../logger.js"; -import { dumpReqAllowed as knobDumpReqAllowed, limitsMaxMessages, limitsWsOutboundBytes, rawDumpDir as knobRawDumpDir } from "../knobs.js"; +import { dumpReqAllowed as knobDumpReqAllowed, limitsMaxMessages, limitsWsOutboundBytes, rawDumpDir as knobRawDumpDir, renderNone as knobRenderNone } from "../knobs.js"; import { gcDumpDirIfConfigured } from "../state-gc.js"; import { applyLaneCredential, laneCredential } from "../lane-credentials.js"; import { CREDENTIAL_HEADER_RE, maskHeaderForLog, maskHeadersForLog, maskHostPortForDisplay, maskUrlForLog } from "../log-mask.js"; @@ -1191,7 +1191,12 @@ const reply = prepared && !prepared.sidePassthrough ? createHandoffReply(prepare // #3803: the generation-parameter fingerprint rides the SAME full parse as the message count — // it is extracted from sentParsed (the complete object), never from the capped body head, so a // trailing-field flip is seen even when the body exceeds SEAM_BODY_CAP. - noteForwardedBody(prepared.session, sentBody, Array.isArray(sentArr) ? (sentArr as unknown[]).length : null, credentialFingerprint(headers), generationParamFp(sentParsed)); + // #4397: stamp whether bili's render tags were active on THIS body (the #2483 suspect flag). + // Per-request strategy, not the global knob: compaction-trigger requests force "none" + // even with the knob off (prepare-responses); lanes that carry no strategy (byte-faithful + // passthroughs) fall back to the knob — their bodies are client bytes, so tag presence ≈ + // session tag mode. + noteForwardedBody(prepared.session, sentBody, Array.isArray(sentArr) ? (sentArr as unknown[]).length : null, credentialFingerprint(headers), generationParamFp(sentParsed), prepared.renderTags !== undefined ? prepared.renderTags !== "none" : !knobRenderNone()); // #3023: the client's own resent view — the protocol conversion of the // inbound bytes BEFORE processTurn folded/replaced anything, captured // with the exact same pairing semantics so detectSeam can attribute a @@ -2816,7 +2821,7 @@ const reply = prepared && !prepared.sidePassthrough ? createHandoffReply(prepare const loopBillingUpstream = route?.rewrittenUrl ?? (/^https?:\/\//i.test(req.url ?? "") ? req.url ?? undefined : opts.upstream); const loop = runCompressLoop( responseBody, - { core, config, messages: prepared.processedMessages.length > 0 ? prepared.processedMessages : prepared.originalMessages, compressMessages: prepared.originalMessages, session: prepared.session, reply, log: ctx.log, proxyUrl, skippedProxy, upstreamOrigin: targetOrigin, protocol: prepared.protocol, textProtocol, debug: opts.debug, refreshFolded, separatorSlots: () => loopSeparatorSlots, visibilityMarkers, dumpSse: loopDumpDir ? (name, stream) => dumpStreamToFile(stream, loopDumpDir, name) : undefined, imageLearn: { host: upstreamHost(loopBillingUpstream), fp: `${imageBillingFor(opts, loopBillingUpstream)}:${imageTokenCapFor(opts, loopBillingUpstream)}` } }, + { core, config, messages: prepared.processedMessages.length > 0 ? prepared.processedMessages : prepared.originalMessages, compressMessages: prepared.originalMessages, session: prepared.session, reply, log: ctx.log, proxyUrl, skippedProxy, upstreamOrigin: targetOrigin, protocol: prepared.protocol, textProtocol, debug: opts.debug, renderTags: prepared.renderTags, refreshFolded, separatorSlots: () => loopSeparatorSlots, visibilityMarkers, dumpSse: loopDumpDir ? (name, stream) => dumpStreamToFile(stream, loopDumpDir, name) : undefined, imageLearn: { host: upstreamHost(loopBillingUpstream), fp: `${imageBillingFor(opts, loopBillingUpstream)}:${imageTokenCapFor(opts, loopBillingUpstream)}` } }, parsedReq, // #3280: egress goes out enveloped via wireTransform, so every // round-2+ response comes back enveloped too — unwrap at the loop's diff --git a/tests/codex-compact-e2e.test.ts b/tests/codex-compact-e2e.test.ts index c5762929b0..c7fe51f1ba 100644 --- a/tests/codex-compact-e2e.test.ts +++ b/tests/codex-compact-e2e.test.ts @@ -11,6 +11,7 @@ import type { ProxyOptions } from "../src/config.ts"; import { SessionStore, _setStoreForTest } from "../src/persist.ts"; import { _setForTest as setRegistryForTest } from "../src/registry.ts"; import { listSessions, _resetSessionsForTest } from "../src/session.ts"; +import { getCacheLedger } from "../src/cache-ledger.ts"; import { buildTriggerForgeBody } from "../src/codex-compact.ts"; // Issue #321 PR-E2: conditional interception + forgery of codex's native @@ -284,6 +285,45 @@ test("e2e E2 (trigger form): kill-switch off (BILI_CODEX_COMPACT=pass) → forwa }); }); +test("e2e E2 (trigger form): tag-free prepared body carries no rt flag even with the renderNone knob off (#4397 rt precision)", async () => { + const prevEnv = process.env.ACP_RENDER_NONE; + try { + delete process.env.ACP_RENDER_NONE; // knob OFF — a global-knob stamp would have set rt=1 here + await withHarness({ firstTurnTokens: 1000 }, async (h) => { + const afterSetup = await setupCompressedSession(h); + const s = listSessions().find((x) => x.meta.label === SESSION)!; + const linesBefore = getCacheLedger(s).lines.length; + // Non-codex UA: no codex early-dispatch (handle.ts forwards those + // verbatim with prepared:null, never touching the seam slot), so + // bili prepares the trigger body itself — renderTags forced "none" + // by prepare-responses regardless of the knob. The ONLY shape where + // per-request strategy diverges from the global knob. + const r2 = await fetch(h.url, { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ model: "gpt-resp", stream: true, session_id: SESSION, instructions: "You are the test coding agent.", input: [...conversation(), { type: "compaction_trigger" }] }), + }); + assert.equal(r2.status, 200); + await r2.text(); + assert.equal(h.bodies.length, afterSetup + 1, "compact request forwarded to upstream"); + // The stamp lives on the seam slot until settle consumes it into the + // ledger line; a trigger-form turn may end unsettled (stream-truncation + // frame), so accept EITHER carrier — neither may carry the #2483 flag. + const led = getCacheLedger(s); + const slotRt = s.transient?.sentBody?.rt; + // Attribute the line to THIS turn: an unsettled turn leaves the + // previous turn's (legitimately tagged) line as the last one. + const lineRt = led.lines.length > linesBefore ? led.lines[led.lines.length - 1]?.rt : undefined; + assert.ok(s.transient?.sentBody !== undefined || led.lines.length > linesBefore, "compact turn was seam-observed"); + assert.notEqual(slotRt, 1, "seam slot carries no rt flag on a tag-free body"); + assert.notEqual(lineRt, 1, "settled line carries no rt flag on a tag-free body"); + }); + } finally { + if (prevEnv === undefined) delete process.env.ACP_RENDER_NONE; + else process.env.ACP_RENDER_NONE = prevEnv; + } +}); + test("e2e E2 (trigger form): intercept + ACP not keeping up (≥90%) → forwarded (native backstop)", async () => { await withHarness({ mode: "intercept", firstTurnTokens: 15000 }, async (h) => { const afterSetup = await setupCompressedSession(h); diff --git a/tests/golden/storage-render/anthropic.json b/tests/golden/storage-render/anthropic.json index 558829d15d..a33efc86f6 100644 --- a/tests/golden/storage-render/anthropic.json +++ b/tests/golden/storage-render/anthropic.json @@ -95,13 +95,13 @@ "foldAt": 9, "editedAt": 12, "summaryFrom": 9, - "stateDigest": "1f528f4fee4f59026d3b005f99a0489cadc139e54698896b2e5a0dadbc95ee2b", - "stateDigestAligned": "a796f61ccd006f5fa1573e1ef33bf756dc24bf017154fab8bde4c0b369516537", + "stateDigest": "75a2e66e7bac21c5cebb67b2da3c0100e15636180340fec39c927574d6069487", + "stateDigestAligned": "faec77643fd5d0c65e64b3e6ec22f3586c210567643ee2a268633cd662655fea", "sectionDigests": { "state": "56e66f6b72c7f0dd", "blockContents": "dc7d13d250c94bb4", "stats": "e83198a55e6b2c29", - "metadata": "65894bb8839e9947" + "metadata": "2e41901e47fbc2f9" }, "stateKeyDigests": { "blocks": "3a454b9a16e4f0b9", diff --git a/tests/golden/storage-render/chat.json b/tests/golden/storage-render/chat.json index ae4923dd78..4ca06468a2 100644 --- a/tests/golden/storage-render/chat.json +++ b/tests/golden/storage-render/chat.json @@ -95,13 +95,13 @@ "foldAt": 9, "editedAt": 12, "summaryFrom": 9, - "stateDigest": "a7b5a67f54a2cbd7978e2a57ad78f522f7af8f94a4e2402898aee4d93479541a", - "stateDigestAligned": "76f701f5ab1631fe1615a3f1cd8fa0fcd0a3fa19c02658cc5d077ae565f4897a", + "stateDigest": "84aa5f49e6ba7bd9cc7dd7d495dade9e9981d678b98e7dc1093681d8e5eacfcb", + "stateDigestAligned": "4fced9f01ffc7194484a73caf007ae2eef99849cb47bec5518994a202c910d28", "sectionDigests": { "state": "90857fbf82001a2b", "blockContents": "94a58f96d7c5cfc3", "stats": "5c2f0fe203d710e8", - "metadata": "0c7d1819e0d4c0f6" + "metadata": "a97fd75dacfa237c" }, "stateKeyDigests": { "blocks": "d9770feee91dafc6", diff --git a/tests/golden/storage-render/google.json b/tests/golden/storage-render/google.json index 3dde0e8cdc..4a664307be 100644 --- a/tests/golden/storage-render/google.json +++ b/tests/golden/storage-render/google.json @@ -95,13 +95,13 @@ "foldAt": 9, "editedAt": 12, "summaryFrom": 9, - "stateDigest": "f8d07462fd8881896c5b52864e84ff9a1a79b0cdb73cd2ef87e16d361b5a07d3", - "stateDigestAligned": "219c42a0f17ed7c9061174627ca56e5b6733e7413d8af98953105bf5d60a40b7", + "stateDigest": "ba3777528ac0d87f32d1de0ea0232a3379aea5e82a359be04271a29315e3cdee", + "stateDigestAligned": "47cc6da83e5413dbc1880faad99a0fcc64b96dcca0f567ab1b041220cd66b877", "sectionDigests": { "state": "c5243ba17364d3ac", "blockContents": "473264d9df8ba338", "stats": "a3c84817f1d0c3dd", - "metadata": "615f125909ed135d" + "metadata": "d65143e223045acd" }, "stateKeyDigests": { "blocks": "0cfd0b9c0fc53073", diff --git a/tests/golden/storage-render/responses.json b/tests/golden/storage-render/responses.json index c9aa385f1a..6a95926f10 100644 --- a/tests/golden/storage-render/responses.json +++ b/tests/golden/storage-render/responses.json @@ -95,13 +95,13 @@ "foldAt": 9, "editedAt": 12, "summaryFrom": 9, - "stateDigest": "aa95de9ed8ddaff40f0b0e2deb1991c85582e33a17149c030687f46cfa34a9a3", - "stateDigestAligned": "5f40c3661d4c43338d855b5a5e79b2915da934affe33d6cb07680a1da39bb996", + "stateDigest": "e5d7a2740ce7772813bbcb981815d56f46dea8d27d24de2ce3a0661594f8f488", + "stateDigestAligned": "7be015bfd5f2889b6cf600c29c9712ecb62cbca1baef4f18660d9ae6e2d34eb5", "sectionDigests": { "state": "e686a14f31d69ce5", "blockContents": "b6701268a0bdcd8a", "stats": "099bde0a9cf64cad", - "metadata": "be12181116331419" + "metadata": "b9ced50a31ccde78" }, "stateKeyDigests": { "blocks": "34b0dc17f8c24595", diff --git a/tests/issue4397-invalidation-reconciliation.test.ts b/tests/issue4397-invalidation-reconciliation.test.ts new file mode 100644 index 0000000000..49d9a6a2f8 --- /dev/null +++ b/tests/issue4397-invalidation-reconciliation.test.ts @@ -0,0 +1,165 @@ +// #4397: the grand-ledger unattributed residual ("upstream TTL/eviction/wire +// rewrite") lumped every body-forensics outcome into one opaque bucket, and the +// dominant-residual warning declared "NOT a bili bug" even when forensics actually +// suspected a mid-history break. CACHE INVALIDATION now reconciles the residual +// over the seam buckets (provider-side / TTL / client / rewind / orphan-sweep / +// suspect), and the warning reports the split when suspects are material. Also: +// provider-side misses are stamped with whether bili's render tags were active on +// send (the #2483 stateful-relay signature) at both send chokepoints. Purely +// diagnostic — no wire change. +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { noteForwardedBody, noteInboundMessages, settleUsageReport, getCacheLedger, buildSessionCacheReport, handleAcpCache } from "../src/cache-ledger.ts"; +import type { Session } from "../src/session.ts"; + +let seq = 0; +function makeSession(): Session { + seq += 1; + return { + id: `i4397-${seq}`, + metadata: {}, + stats: {}, + state: { blocks: [], messageRefs: { byRaw: {}, byRef: {} }, tokenSnapshot: {} }, + } as unknown as Session; +} + +const T0 = Date.parse("2026-10-24T10:00:00Z"); +const CAP = 512 * 1024; + +function settle(session: Session, at: number, input: number, cached: number): void { + settleUsageReport(session, { total: input, reportedCached: cached, output: 0, protocol: "openai", upstream: "http://u" }); +} + +// 32 messages x 18 KiB ≈ 577 KiB: over the forensics cap (same fixture family as #2131). +const BIG = Array.from({ length: 32 }, (_, i) => `${i}${"x".repeat(17_983)}`); +const bigA = JSON.stringify({ model: "m", messages: BIG.map((c) => ({ role: "user", content: c })) }); +assert.ok(bigA.length > CAP, "fixture must exceed the forensics cap"); + +const appended = JSON.stringify({ model: "m", messages: [...BIG.map((c) => ({ role: "user", content: c })), { role: "user", content: "n1" }, { role: "user", content: "n2" }] }); +assert.ok(appended.length > CAP, "appended fixture must stay over the cap"); + +const inbound = (a: string, b: string, c: string) => [ + { role: "user", content: a }, + { role: "user", content: b }, + { role: "user", content: c }, +] as unknown as Parameters[1]; + +test("unattributed residual is reconciled over the forensics buckets", () => { + const s = makeSession(); + noteForwardedBody(s, bigA, 32); + settle(s, T0, 100_000, 99_000); + // (a) stable-head capped append → provider-side miss + noteForwardedBody(s, appended, 34); + settle(s, T0 + 1000, 100_000, 20_000); + // (b) mid-list break, no settled inbound pair yet → suspected seam + const msgsB = BIG.map((c) => ({ role: "user", content: c })); + msgsB[5] = { role: "user", content: `BROKEN${"w".repeat(17_977)}` }; + msgsB.push({ role: "user", content: "n1" }); + noteForwardedBody(s, JSON.stringify({ model: "m", messages: msgsB }), 33); + noteInboundMessages(s, inbound("A1", "B1", "C1")); + settle(s, T0 + 2000, 100_000, 20_000); + // (c) same outbound break shape, but the client's own resent list broke mid-list → host-owned + const msgsC = BIG.map((c) => ({ role: "user", content: c })); + msgsC[7] = { role: "user", content: `HOST${"z".repeat(17_981)}` }; + msgsC.push({ role: "user", content: "n1" }); + noteForwardedBody(s, JSON.stringify({ model: "m", messages: msgsC }), 33); + noteInboundMessages(s, inbound("A1", "B2", "C1")); + settle(s, T0 + 3000, 100_000, 20_000); + + const led = getCacheLedger(s); + assert.equal(led.agg.providerSideMisses, 1); + assert.equal(led.agg.seamSuspects, 1); + assert.equal(led.agg.clientInserts, 1); + const r = buildSessionCacheReport(s); + const booked = r.seam.providerSide.missed + r.seam.missed + r.seam.client.missed + r.seam.rewinds.missed + r.seam.orphanSweep.missed + r.seam.providerTtl.missed; + assert.ok(booked <= r.invalidation.remaining, "forensics buckets must stay inside the residual"); + const text = handleAcpCache(s).text; + assert.match(text, /of which the unattributed .*tok: body forensics booked/); + assert.match(text, /provider-side miss .*\(1\)/); + assert.match(text, /suspected mid-history break .*\(1\)/); + assert.match(text, /client mid-history rewrite .*\(1\)/); + // no render-tags stamp anywhere in this session + assert.equal(r.seam.taggedSide.count, 0); + assert.doesNotMatch(text, /sent WITH bili render tags active/); +}); + +test("provider-side miss sent with render tags active is tagged (#2483 signature)", () => { + const s = makeSession(); + noteForwardedBody(s, bigA, 32); + settle(s, T0, 100_000, 99_000); + noteForwardedBody(s, appended, 34, undefined, undefined, true); + settle(s, T0 + 1000, 100_000, 20_000); + const led = getCacheLedger(s); + assert.equal(led.agg.providerSideMisses, 1); + assert.equal(led.agg.taggedSideMisses, 1); + const line = led.lines[led.lines.length - 1]!; + assert.equal(line.rt, 1); + assert.equal(led.agg.taggedSideMissed, line.tr); + const r = buildSessionCacheReport(s); + assert.deepEqual(r.seam.taggedSide, { count: 1, missed: line.tr }); + assert.match(handleAcpCache(s).text, /sent WITH bili render tags active \(#4397\)/); +}); + +test("provider-side miss without render tags stays untagged", () => { + const s = makeSession(); + noteForwardedBody(s, bigA, 32); + settle(s, T0, 100_000, 99_000); + noteForwardedBody(s, appended, 34, undefined, undefined, false); + settle(s, T0 + 1000, 100_000, 20_000); + const led = getCacheLedger(s); + assert.equal(led.agg.providerSideMisses, 1); + assert.equal(led.agg.taggedSideMisses, 0); + assert.equal(led.agg.taggedSideMissed, 0); + const line = led.lines[led.lines.length - 1]!; + assert.equal(line.rt, undefined); + const r = buildSessionCacheReport(s); + assert.deepEqual(r.seam.taggedSide, { count: 0, missed: 0 }); + assert.doesNotMatch(handleAcpCache(s).text, /sent WITH bili render tags active/); +}); + +test("dominant residual with a suspected break reports the split, not 'NOT a bili bug'", () => { + const s = makeSession(); + const small = JSON.stringify({ model: "m", messages: [{ role: "user", content: "a".repeat(50_000) }] }); + noteForwardedBody(s, small, 1); + settle(s, T0, 100_000, 99_000); + const broken = JSON.stringify({ model: "m", messages: [{ role: "user", content: `BROKEN${"w".repeat(49_994)}` }, { role: "user", content: "n1" }] }); + noteForwardedBody(s, broken, 2); + settle(s, T0 + 1000, 100_000, 20_000); + const led = getCacheLedger(s); + assert.equal(led.agg.seamSuspects, 1); + const text = handleAcpCache(s).text; + assert.match(text, /body forensics split it:/); + assert.doesNotMatch(text, /NOT a bili bug/); +}); + +test("dominant residual with NO suspected break keeps the #1847 provider-side wording", () => { + const s = makeSession(); + noteForwardedBody(s, bigA, 32); + settle(s, T0, 100_000, 99_000); + noteForwardedBody(s, appended, 34); + settle(s, T0 + 1000, 100_000, 20_000); + const led = getCacheLedger(s); + assert.equal(led.agg.seamSuspects, 0); + assert.equal(led.agg.providerSideMisses, 1); + const text = handleAcpCache(s).text; + assert.match(text, /NOT a bili bug/); + assert.doesNotMatch(text, /body forensics split it/); + assert.match(text, /of which the unattributed .*tok: body forensics booked provider-side miss/); +}); + +test("pre-change persisted ledger without taggedSide counters normalizes on load", () => { + const s = makeSession(); + noteForwardedBody(s, bigA, 32); + settle(s, T0, 100_000, 99_000); + noteForwardedBody(s, appended, 34); + settle(s, T0 + 1000, 100_000, 20_000); + const raw = (s.metadata as unknown as { cacheLedger: { agg: Record } }).cacheLedger; + delete raw.agg.taggedSideMisses; + delete raw.agg.taggedSideMissed; + const again = getCacheLedger(s); + assert.equal(again.agg.taggedSideMisses, 0); + assert.equal(again.agg.taggedSideMissed, 0); + const r = buildSessionCacheReport(s); + assert.deepEqual(r.seam.taggedSide, { count: 0, missed: 0 }); + assert.equal(handleAcpCache(s).outcome, "success"); +});