From ba4d5a6646e4371a75ce7ab98c64ac1f2b78a810 Mon Sep 17 00:00:00 2001 From: ci-check Date: Fri, 20 Nov 2026 14:27:46 +0800 Subject: [PATCH 1/2] fix(fastpath): exclude id from watermark head check for pinned sessions (#7345) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The watermark fastpath head check compared a.id !== b.id between the fresh head cores (content-hash ids minted by openaiToCore, h_…) and the stashed seed cores. In a pinned session (pluginLaneServerHistory + pluginMsgIdIncremental — the 2x2 traffic the fastpath exists to accelerate) prepare-openai rebinds originalMessages to the POST-PIN cores before finalizeWatermarkTurn, so the seed carries host/ledger msgids while a fresh openaiToCore parse mints content-hash ids. The two id namespaces differ structurally on every turn, so the comparison could never pass: every turn of a pinned session logged fastpath miss (head-prefix-drift) and fell back to the full legacy parse. The id is not a content signal: exclude it from the head comparison (content and role inequality are still caught, and a remint with identical bytes is not a rewrite). Non-pinned traffic keeps content-hash ids on both sides, so when a.id === b.id the full-JSON equality holds iff the id-stripped equality holds — existing behavior is unchanged. Regression test plays the field sequence: seed with a host-id list, then a tail-only growth turn must HIT (was head-prefix-drift) and an in-place head edit must still drift. Fixes #7345 --- src/server/fastpath.ts | 17 ++++++++++- tests/fastpath.test.ts | 67 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 1 deletion(-) diff --git a/src/server/fastpath.ts b/src/server/fastpath.ts index 72970d4984..2de3ad9069 100644 --- a/src/server/fastpath.ts +++ b/src/server/fastpath.ts @@ -332,7 +332,7 @@ export function evaluateWatermarkFastpath( for (let hi = 0; hi < seedHead.length; hi++) { const a = headCores.msgs[hi]; const b = seedHead[hi]; - if (a.id !== b.id || JSON.stringify(a) !== JSON.stringify(b)) return miss("head-prefix-drift", false, tailIds); + if (headCoreContentDiffers(a, b)) return miss("head-prefix-drift", false, tailIds); } } const windowRaw = openaiToCore(windowFragment as OpenAIRequestBody); @@ -407,6 +407,21 @@ function baseOfRawId(id: string): string { return m === null ? id : id.slice(0, 18); } +/** In-place head-edit check, IGNORING the `id` field. In a pinned session + * (pluginLaneServerHistory) the seed cores carry host/ledger msgids while a + * fresh openaiToCore parse mints content-hash ids, so the id namespace differs + * structurally on every turn (#7347) — the id is not a content signal here. + * Content/role/shape inequality is still caught via the full JSON minus the + * id; a remint with identical bytes is not a rewrite. */ +function headCoreContentDiffers(a: CoreMessage, b: CoreMessage): boolean { + const strip = (c: CoreMessage): Record => { + const rest: Record = { ...c }; + delete rest.id; + return rest; + }; + return JSON.stringify(strip(a)) !== JSON.stringify(strip(b)); +} + /** openaiToCore materializes user content-array images as distinct cores * carrying an `imagePart`/`rawOpenaiContent` pair — used to detect * multimodal tails from already-converted cores (legacy-side seeding). */ diff --git a/tests/fastpath.test.ts b/tests/fastpath.test.ts index 8965a7b9f6..d44f4b6a6c 100644 --- a/tests/fastpath.test.ts +++ b/tests/fastpath.test.ts @@ -9,6 +9,7 @@ import http from "node:http"; import { openaiToCore, coreToOpenai } from "acp-kernel/wire"; import { evaluateWatermarkFastpath, finalizeWatermarkTurn, clearWatermarkSeed, watermarkDebugState, PF_COUNT_HEADER, PF_SEQ_HEADER, PF_TAIL_IDS_HEADER, PF_SLOT_UNKNOWN } from "../src/server/fastpath.ts"; import { stripRawStage } from "../src/marker/engine.ts"; +import { applyMsgIdPin } from "../src/msgid-pin.ts"; import { INCREMENTAL_TURN_MARKER_HEADER } from "../src/incremental-lane.ts"; import { _setStoreForTest, _resetStoreForTest, type SessionStore } from "../src/persist.ts"; import type { Session } from "../src/session.ts"; @@ -65,6 +66,33 @@ const runTurn = ( return { out, headers }; }; +/** Drive one turn the way prepareOpenai does for a PINNED session + * (pluginLaneServerHistory on, #4491): on a miss the seed is re-keyed to the + * host msgids via applyMsgIdPin (the id namespace the stash then carries), on + * a hit the pin is skipped (the seed's prefix was already pinned when it was + * materialized). This is the 2x2 traffic the watermark fastpath exists to + * accelerate — the seed cores carry host msgids while a fresh openaiToCore + * parse mints content-hash ids, so the id namespaces differ structurally + * (#7347). */ +const runPinnedTurn = ( + session: Session, + reqHeaders: Record, + bodyText: string, + hostIds: string[], +): { out: ReturnType; headers: Record } => { + const model = JSON.parse(bodyText).model as string; + const out = evaluateWatermarkFastpath(bodyText, headersOf(reqHeaders), session, { pluginMode: true, titleGen: false, model }, log); + const oracle = openaiToCore(JSON.parse(bodyText)); + const stripCounts = stripRawStage(JSON.parse(bodyText), "openai"); + const headers = finalizeWatermarkTurn(session, out, { + model, + legacyCores: out.hit !== null ? out.hit.view : applyMsgIdPin(oracle.msgs, hostIds), + rawSystemText: out.hit !== null ? out.hit.systemText : oracle.systemText, + turnStripCounts: stripCounts, + }); + return { out, headers }; +}; + test("gate off / title-gen / incremental-turn requests are untouched", () => { _resetStoreForTest(); makeStubStore(); @@ -464,3 +492,42 @@ test("clearWatermarkSeed forces the next turn back through verification", () => const { out } = runTurn(session, { [PF_COUNT_HEADER]: "3" }, b2); assert.equal(out.decision, "unseeded"); }); + +test("pinned seed (host msgids) + tail-only growth HITS; in-place head edit still drifts (#7347)", () => { + makeStubStore(); + const session = freshSession("pf-pin"); + // turn 1: seed. pluginLaneServerHistory pins the seed cores to host msgids + // (applyMsgIdPin), so the stash carries a DIFFERENT id namespace than the + // content-hash ids a fresh openaiToCore parse mints. + const b1 = bodyOf([SYS("s"), USR("a"), USR("b"), ASST("c"), USR("d")]); + const t1 = runPinnedTurn(session, {}, b1, ["host-a", "host-b", "host-c", "host-d"]); + assert.equal(t1.out.decision, "no-stamp", LOGS.join("\n")); + assert.equal(t1.out.measuredT, 5); + // turn 2: same prefix + one new tail element. The fresh head cores carry + // content-hash ids while the seed carries host msgids — the head check must + // ignore the id namespace and HIT on the unchanged content. Pre-fix this is + // a permanent head-prefix-drift miss (the field symptom: every turn of a + // 2x2 pinned session fell back to the full legacy parse). + const b2 = bodyOf([SYS("s"), USR("a"), USR("b"), ASST("c"), USR("d"), USR("e")]); + const t2 = runPinnedTurn(session, { [PF_COUNT_HEADER]: "5" }, b2, ["host-a", "host-b", "host-c", "host-d", "host-e"]); + assert.equal(t2.out.decision, "hit", LOGS.join("\n")); + assert.equal(t2.out.creditedMiss, false); + // the view must still be the legacy full-parse oracle on CONTENT. The + // prefix ids are host msgids by design in a pinned session, so assert + // text/role parity (not id parity — that is the namespace difference). + if (!t2.out.hit) throw new Error("expected hit"); + const oracle2 = openaiToCore(JSON.parse(b2)); + assert.equal(t2.out.hit.view.length, oracle2.msgs.length); + for (let i = 0; i < oracle2.msgs.length; i++) { + assert.equal(t2.out.hit.view[i]!.text, oracle2.msgs[i]!.text, `text @${i}`); + assert.equal(t2.out.hit.view[i]!.role, oracle2.msgs[i]!.role, `role @${i}`); + } + // turn 3: rewrite a HEAD element IN PLACE (prefix counts unchanged, system + // identical, stamp honest) + append one tail — the per-core content + // comparison (id excluded) must still catch the rewrite, so the fix does + // not mask a genuine in-place edit. + const b3 = bodyOf([SYS("s"), USR("a"), USR("b-EDITED"), ASST("c"), USR("d"), USR("e"), USR("f")]); + const t3 = runPinnedTurn(session, { [PF_COUNT_HEADER]: "6" }, b3, ["host-a", "host-b2", "host-c", "host-d", "host-e", "host-f"]); + assert.equal(t3.out.decision, "head-prefix-drift", LOGS.join("\n")); + assert.equal(t3.out.creditedMiss, false); +}); From ae774d7dea5cae75619ab2de407ca70659ac72ea Mon Sep 17 00:00:00 2001 From: ci-check Date: Fri, 20 Nov 2026 15:01:35 +0800 Subject: [PATCH 2/2] ci: re-trigger (initial runs mass-cancelled by concurrency race)