Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/server/fastpath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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<string, unknown> => {
const rest: Record<string, unknown> = { ...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). */
Expand Down
67 changes: 67 additions & 0 deletions tests/fastpath.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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<string, string>,
bodyText: string,
hostIds: string[],
): { out: ReturnType<typeof evaluateWatermarkFastpath>; headers: Record<string, string> } => {
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();
Expand Down Expand Up @@ -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);
});
Loading