diff --git a/src/invariant-judge.ts b/src/invariant-judge.ts index ad2e2a5267..302856e05b 100644 --- a/src/invariant-judge.ts +++ b/src/invariant-judge.ts @@ -75,6 +75,8 @@ export interface JudgeVerdict { exempt?: { carrier: number; reasoning: number; crush: number }; /** Egress lane the verdict came from — main first-send vs loop round-2+. */ lane?: "main" | "loop"; + /** #4241: true when judged in render-lane tail-fidelity mode (outbound prefix was server-rendered). */ + renderLane?: boolean; } interface JudgeOptions { @@ -87,6 +89,13 @@ interface JudgeOptions { // steady folded session stays far under it (≈ one carrier per live block + // one reasoning item per reasoning turn). maxExempt?: number; + // #4241: when set (a finite number >= 0), THIS turn's outbound prefix was + // server-rendered (snapshot-render lane) — the raw inbound prefix is NOT a + // valid baseline (the engine intentionally replaced the covered region with + // transcript rows + summary carriers). Validate only the client tail + // entry[renderTailStart..] against the outbound; the rendered prefix is exempt + // from the frozen-prefix requirement. Absent ⇒ legacy full inbound-vs-outbound. + renderTailStart?: number; } function normText(m: CoreMessage): string { @@ -167,60 +176,117 @@ export function judgeInboundOutbound( const freezeEnd = folded ? sumIdx : exit.length; let foldStart = -1; + // #4241 render-lane tail-fidelity: when THIS turn's outbound prefix was + // server-rendered (snapshot render active), the raw inbound prefix is NOT a + // valid baseline — the engine intentionally replaced the covered region with + // transcript rows + summary carriers (defined behavior, not a rewrite). Only + // the client tail (entry[rts..]) is promised byte-intact, so validate exactly + // that, in order; the rendered prefix is exempt from the frozen-prefix rule. + // A real tail corruption/drop/reorder still goes red — I2 stays armed on the + // part the render owns verbatim. Cross-turn prefix stability is seamLcp's / + // shadow's job (outbound-vs-previous-outbound), not this inbound-vs-outbound. + const rts = opts.renderTailStart; + const renderLane = typeof rts === "number" && Number.isFinite(rts) && rts >= 0; + let j = 0; - for (let i = 0; i < entry.length; i++) { - const e = entry[i]; - if (isReasoning(e)) continue; - if (folded && j >= freezeEnd) { foldStart = i; break; } // remainder is the folded region - total++; - - // Stateful hosts re-send previously-injected carriers in their inbound - // history; such an entry must match its own twin by identity — skipping - // exit carriers for it would false-positive every steady-state post-fold - // turn. The gap budget counts NON-carrier elements only: a stack of - // injected carriers between stable messages is exempt volume, not drift. - const eCarrier = isCarrier(e); - let found = -1; - let nearTwin = false; - let scanned = 0; - for (let k = j; k < freezeEnd; k++) { - const x = exit[k]; - if (isReasoning(x)) continue; - if (!eCarrier && isCarrier(x)) continue; - scanned++; - if (scanned > maxGap) break; - if (stableEqual(e, x)) { found = k; break; } - if (x.role === e.role && x.contentType === e.contentType && (!e.toolCallId || !x.toolCallId || x.toolCallId === e.toolCallId)) nearTwin = true; + if (renderLane) { + let located = false; // have we matched at least one tail message yet? + for (let i = rts!; i < entry.length; i++) { + const e = entry[i]; + if (isReasoning(e)) continue; + total++; + const eCarrier = isCarrier(e); + let found = -1; + let nearTwin = false; + let scanned = 0; + // Until the tail is located, the scan spans the whole outbound (it must + // cross the entire server-rendered prefix); once located, tail messages + // are contiguous and the normal carrier-gap budget applies again. + const cap = located ? maxGap : Number.POSITIVE_INFINITY; + for (let k = j; k < exit.length; k++) { + const x = exit[k]; + if (isReasoning(x)) continue; + if (!eCarrier && isCarrier(x)) continue; + scanned++; + if (scanned > cap) break; + if (stableEqual(e, x)) { found = k; break; } + if (x.role === e.role && x.contentType === e.contentType && (!e.toolCallId || !x.toolCallId || x.toolCallId === e.toolCallId)) nearTwin = true; + } + if (found < 0) { + violations.push({ kind: nearTwin ? "mid-history-rewrite" : "mid-history-drop", index: i, detail: `tail inbound[${i}] ${fp(e)} absent from outbound` }); + j++; // soft resync: one bad cell must not cascade into a total unfold (#2821) + continue; + } + // Insertions count only AFTER the tail is located — the server prefix + // before the first match is legitimately present, never an insertion. + if (located) { + for (let k = j; k < found; k++) { + const x = exit[k]; + if (!isCarrier(x) && !isReasoning(x)) { + violations.push({ kind: "mid-insertion", index: i, detail: `unexplained outbound element before tail inbound[${i}]: ${fp(x)}` }); + } + } + } + matched++; + j = found + 1; + located = true; } + foldStart = rts! > 0 ? 0 : -1; + } else { + for (let i = 0; i < entry.length; i++) { + const e = entry[i]; + if (isReasoning(e)) continue; + if (folded && j >= freezeEnd) { foldStart = i; break; } // remainder is the folded region + total++; - if (found < 0) { - violations.push({ - kind: nearTwin ? "mid-history-rewrite" : "mid-history-drop", - index: i, - detail: `inbound[${i}] ${fp(e)} absent from outbound frozen prefix`, - }); - j++; // soft resync: one bad cell must not cascade into a total unfold (#2821) - continue; - } + // Stateful hosts re-send previously-injected carriers in their inbound + // history; such an entry must match its own twin by identity — skipping + // exit carriers for it would false-positive every steady-state post-fold + // turn. The gap budget counts NON-carrier elements only: a stack of + // injected carriers between stable messages is exempt volume, not drift. + const eCarrier = isCarrier(e); + let found = -1; + let nearTwin = false; + let scanned = 0; + for (let k = j; k < freezeEnd; k++) { + const x = exit[k]; + if (isReasoning(x)) continue; + if (!eCarrier && isCarrier(x)) continue; + scanned++; + if (scanned > maxGap) break; + if (stableEqual(e, x)) { found = k; break; } + if (x.role === e.role && x.contentType === e.contentType && (!e.toolCallId || !x.toolCallId || x.toolCallId === e.toolCallId)) nearTwin = true; + } + + if (found < 0) { + violations.push({ + kind: nearTwin ? "mid-history-rewrite" : "mid-history-drop", + index: i, + detail: `inbound[${i}] ${fp(e)} absent from outbound frozen prefix`, + }); + j++; // soft resync: one bad cell must not cascade into a total unfold (#2821) + continue; + } - for (let k = j; k < found; k++) { - const x = exit[k]; - if (!isCarrier(x) && !isReasoning(x)) { - violations.push({ kind: "mid-insertion", index: i, detail: `unexplained outbound element before inbound[${i}]: ${fp(x)}` }); + for (let k = j; k < found; k++) { + const x = exit[k]; + if (!isCarrier(x) && !isReasoning(x)) { + violations.push({ kind: "mid-insertion", index: i, detail: `unexplained outbound element before inbound[${i}]: ${fp(x)}` }); + } } + matched++; + j = found + 1; } - matched++; - j = found + 1; - } - // Carrier pinning (I2): a non-carrier element sitting before the fold carrier - // that exceeds the matched frozen prefix means the anchor drifted. - if (folded && sumIdx > matched) { - for (let k = matched; k < sumIdx; k++) { - const x = exit[k]; - if (!isCarrier(x) && !isReasoning(x)) { - violations.push({ kind: "carrier-drift", index: -1, detail: `element before fold carrier at outbound[${k}]: ${fp(x)}` }); - break; + // Carrier pinning (I2): a non-carrier element sitting before the fold carrier + // that exceeds the matched frozen prefix means the anchor drifted. + if (folded && sumIdx > matched) { + for (let k = matched; k < sumIdx; k++) { + const x = exit[k]; + if (!isCarrier(x) && !isReasoning(x)) { + violations.push({ kind: "carrier-drift", index: -1, detail: `element before fold carrier at outbound[${k}]: ${fp(x)}` }); + break; + } } } } @@ -249,6 +315,7 @@ export function judgeInboundOutbound( total, violations, exempt: { carrier: carrierExempt, reasoning: reasoningExempt, crush: crushExempt }, + ...(renderLane ? { renderLane: true } : {}), }; } @@ -266,6 +333,10 @@ interface InvariantJudgeInput { body?: string | Buffer; protocol?: string; sessionId?: string; + /** #4241: set when prepare's snapshot render served this turn's outbound prefix — + * the first client index of the live tail; the judge then validates only that + * suffix against the outbound (the rendered prefix is exempt). Absent ⇒ legacy. */ + renderTailStart?: number; } // Re-parse the ACTUAL outbound body through its own wire codec so the exit side @@ -332,7 +403,7 @@ export function maybeRunInvariantJudge(input: InvariantJudgeInput): void { const exitCores0 = parseExitCores(input.body, input.protocol); if (!exitCores0) return; // unparseable/unknown wire → skip, never false-positive const exitCores = fault ? fault([...exitCores0]) : exitCores0; - const v = judgeInboundOutbound(entry, exitCores); + const v = judgeInboundOutbound(entry, exitCores, { renderTailStart: input.renderTailStart }); // Every judged request reports its verdict (ok or not): consumers must be // able to see folded-but-clean traffic too, not just violations — that is // how the live I2 path proves a real fold stayed green (#3842). diff --git a/src/server/relay.ts b/src/server/relay.ts index 594cd98736..fd1f1ec368 100644 --- a/src/server/relay.ts +++ b/src/server/relay.ts @@ -717,11 +717,19 @@ export async function forward( // steering / chain-stamp / shed rewrites, so I1/I2 see the wire truth. // Advisory; never blocks. Replaces the pre-send hook (handle.ts) which // judged a pre-rewrite snapshot. + // #4241: on a snapshot-render turn the outbound prefix is server-rendered + // (transcript rows + summary carriers), not the raw inbound bytes — the + // judge must exempt that region and validate only the client tail. The + // boundary was recorded by prepare's maybeSnapshotView on the same session. + const renderTailStart = typeof prepared.session.metadata["snapshotRenderTailStart"] === "number" + ? (prepared.session.metadata["snapshotRenderTailStart"] as number) + : undefined; maybeRunInvariantJudge({ originalMessages: prepared.originalMessages, body: sentBody, protocol: prepared.protocol, sessionId: prepared.session.id, + ...(renderTailStart !== undefined ? { renderTailStart } : {}), }); } // #3148: round-level breaker — while the endpoint is provably dead, fail diff --git a/src/server/snapshot-render.ts b/src/server/snapshot-render.ts index ed8f4c85b4..0068b9a1c5 100644 --- a/src/server/snapshot-render.ts +++ b/src/server/snapshot-render.ts @@ -121,7 +121,11 @@ function parseRow(content: string, seq: number): BiliMessage | null { * are two observability fields on `session.metadata` (the render latch and the * last post-arm degrade reason) — neither touches outbound bytes. */ -type ViewResult = { ok: true; view: BiliMessage[] } | { ok: false; reason: string }; +// tailStart (#4241): the first CLIENT index of the live tail (clientJoin) — +// everything in clientMsgs at indices >= tailStart rides the outbound verbatim, +// everything below is server-rendered territory. Exposed so the egress judge can +// exempt the rendered prefix and validate only the tail it promises byte-intact. +type ViewResult = { ok: true; view: BiliMessage[]; tailStart: number } | { ok: false; reason: string }; function buildView(session: Session, clientMsgs: BiliMessage[]): ViewResult { const activeBlocks = session.state.blocks.filter((b) => b.active); @@ -300,10 +304,15 @@ function buildView(session: Session, clientMsgs: BiliMessage[]): ViewResult { while (ai < anchors.length) merged.push(anchors[ai++].msg); for (let k = clientJoin; k < clientMsgs.length; k++) merged.push(clientMsgs[k]); - return { ok: true, view: merged }; + return { ok: true, view: merged, tailStart: clientJoin }; } export function maybeSnapshotView(session: Session, clientMsgs: BiliMessage[], deps: SnapshotRenderDeps): BiliMessage[] { + // #4241: the judge reads this to know whether THIS turn's outbound prefix is + // server-rendered. Clear unconditionally so presence ⟺ this call rendered a + // live view — every early-return / degrade / shadow path leaves it unset, + // which is exactly when the outbound is raw client bytes (legacy I1/I2 applies). + delete session.metadata["snapshotRenderTailStart"]; if (deps.pluginMode) return clientMsgs; // plugin lane: client-authoritative by design, not a degradation const live = featureEnabled("snapshotRender"); const shadow = !live && shadowLaneEnabled(); @@ -314,6 +323,7 @@ export function maybeSnapshotView(session: Session, clientMsgs: BiliMessage[], d if (live) { if (!r.ok) return legacyFor(session, deps, clientMsgs, r.reason); delete session.metadata["snapshotRenderDegradeReason"]; + session.metadata["snapshotRenderTailStart"] = r.tailStart; noteRender(session, deps); return r.view; } diff --git a/tests/invariant-judge.test.ts b/tests/invariant-judge.test.ts index e6f42eb3a5..75fff8eec9 100644 --- a/tests/invariant-judge.test.ts +++ b/tests/invariant-judge.test.ts @@ -317,3 +317,84 @@ test("PR-D closure: a REAL mid-history rewrite still flags through the echo norm assert.equal(v.ok, false); assert.ok(v.violations.some((x) => x.kind === "mid-history-rewrite")); }); + +// #4241 — snapshot-render lane tail-fidelity. On a turn where prepare's snapshot +// render served the outbound prefix, the raw inbound prefix was INTENTIONALLY +// replaced by server rows + summary anchors; only the client tail (>= renderTailStart) +// is promised byte-intact. The judge validates exactly that suffix; the rendered +// prefix is exempt from the frozen-prefix rule while I2 stays armed on the tail. +const renderCarrier = () => mk("system", "text", "[Compressed conversation section] b5", { summaryOfBlockId: "b5" }); + +test("#4241 POS bug-repro: covered region replaced by server view + intact tail ⇒ green (was false-positive red)", () => { + const entry = [mk("user", "text", "old-a"), mk("assistant", "text", "old-b"), mk("user", "text", "old-c"), mk("user", "text", "new question")]; + const exit = [mk("user", "text", "canon-x"), mk("assistant", "text", "canon-y"), renderCarrier(), mk("user", "text", "new question")]; + // Without render-lane awareness this turn reads as a mid-history rewrite — the reported false positive: + const legacy = judgeInboundOutbound(entry, exit); + assert.equal(legacy.ok, false); + assert.ok(legacy.violations.some((x) => x.kind === "mid-history-rewrite")); + // With renderTailStart=3 (first client index of the live tail) only the tail is validated: + const v = judgeInboundOutbound(entry, exit, { renderTailStart: 3 }); + assert.equal(v.ok, true); + assert.equal(v.renderLane, true); + assert.equal(v.matched, 1); + assert.equal(v.total, 1); + assert.equal(v.folded, true); +}); + +test("#4241 NEG control: a corrupted tail message still goes red on the render lane (teeth remain)", () => { + const entry = [mk("user", "text", "old"), mk("user", "text", "original words")]; + const exit = [mk("user", "text", "canon"), renderCarrier(), mk("user", "text", "rewritten words")]; + const v = judgeInboundOutbound(entry, exit, { renderTailStart: 1 }); + assert.equal(v.ok, false); + assert.ok(v.violations.some((x) => x.kind === "mid-history-rewrite")); +}); + +test("#4241 NEG control: a dropped tail message still goes red on the render lane", () => { + const entry = [mk("user", "text", "old"), mk("user", "text", "keep me"), mk("assistant", "text", "and me")]; + const exit = [mk("user", "text", "canon"), renderCarrier(), mk("user", "text", "keep me")]; + const v = judgeInboundOutbound(entry, exit, { renderTailStart: 1 }); + assert.equal(v.ok, false); + assert.ok(v.violations.some((x) => x.kind === "mid-history-drop")); +}); + +test("#4241 POS: multiple intact tail messages all match in order across the server prefix", () => { + const entry = [mk("user", "text", "c0"), mk("assistant", "text", "c1"), mk("user", "text", "q"), mk("assistant", "text", "a"), mk("user", "text", "q2")]; + const exit = [mk("user", "text", "canon"), renderCarrier(), mk("user", "text", "q"), mk("assistant", "text", "a"), mk("user", "text", "q2")]; + const v = judgeInboundOutbound(entry, exit, { renderTailStart: 2 }); + assert.equal(v.ok, true); + assert.equal(v.matched, 3); + assert.equal(v.total, 3); + assert.equal(v.renderLane, true); +}); + +test("#4241 POS edge: fully-covered turn (empty tail) is green with nothing to validate", () => { + const entry = [mk("user", "text", "covered-0"), mk("assistant", "text", "covered-1")]; + const exit = [mk("user", "text", "canon-0"), renderCarrier()]; + const v = judgeInboundOutbound(entry, exit, { renderTailStart: 2 }); + assert.equal(v.ok, true); + assert.equal(v.matched, 0); + assert.equal(v.total, 0); + assert.equal(v.renderLane, true); +}); + +test("#4241 wiring: maybeRunInvariantJudge honors renderTailStart (green on rendered shape, red without)", () => { + const seen: JudgeVerdict[] = []; + _setInvariantJudgeForTest(true, (v) => seen.push(v)); + try { + const body = JSON.stringify({ model: "m", messages: [ + { role: "user", content: "canon" }, + { role: "user", content: "new question" }, + ] }); + const entry = [mk("user", "text", "old"), mk("user", "text", "new question")]; + maybeRunInvariantJudge({ originalMessages: entry, body, protocol: "openai", sessionId: "s-r" }); + assert.equal(seen.at(-1)?.ok, false); + assert.equal(seen.at(-1)?.renderLane, undefined); + assert.ok(seen.at(-1)!.violations.some((x) => x.kind === "mid-history-rewrite")); + seen.length = 0; + maybeRunInvariantJudge({ originalMessages: entry, body, protocol: "openai", sessionId: "s-r", renderTailStart: 1 }); + assert.equal(seen.at(-1)?.ok, true); + assert.equal(seen.at(-1)?.renderLane, true); + } finally { + _setInvariantJudgeForTest(false); + } +});