diff --git a/kernel/package-lock.json b/kernel/package-lock.json index 0ee2818fbc..030f548bd9 100644 --- a/kernel/package-lock.json +++ b/kernel/package-lock.json @@ -1,12 +1,12 @@ { "name": "acp-kernel", - "version": "0.0.147", + "version": "0.0.148", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "acp-kernel", - "version": "0.0.147", + "version": "0.0.148", "license": "MIT", "devDependencies": { "@types/node": "^22.0.0", diff --git a/kernel/package.json b/kernel/package.json index 54b48a750d..abf00497c5 100644 --- a/kernel/package.json +++ b/kernel/package.json @@ -1,6 +1,6 @@ { "name": "acp-kernel", - "version": "0.0.147", + "version": "0.0.148", "description": "Framework-agnostic context-compression engine (model-driven, 3-tier LSM). Pure core: no host dependency.", "license": "MIT", "author": "ranxianglei", diff --git a/kernel/src/wire/responses.ts b/kernel/src/wire/responses.ts index 946754ce4a..05509271b9 100644 --- a/kernel/src/wire/responses.ts +++ b/kernel/src/wire/responses.ts @@ -153,6 +153,18 @@ function userDisplayText( return !text || (hasImage && !hasTextPart) ? "[image]" : text; } +/** Parse-side twin of the split gate in responsesToCore: does this part + * produce exactly one user core (non-empty text part, or image part with a + * string URL)? Rebuild-side full coverage counts parts with THIS predicate + * so its count matches the cores the parse actually emitted. */ +function producesUserCore(part: ResponseContentPart): boolean { + if (typeof part !== "object" || part === null) return false; + if (part.type === "input_text" || part.type === "output_text") + return typeof part.text === "string" && part.text.length > 0; + if (part.type === "input_image") return typeof part.image_url === "string"; + return false; +} + /** Extract the reasoning text from a responses reasoning item. The host * carries it in `content` (reasoning_text parts); the primeFold mirror * carries it in `summary` (summary_text parts). Both must yield the same @@ -747,91 +759,155 @@ export function coreToResponses( customToolCallIds: Set = new Set(), ): ResponseInputItem[] { const out: ResponseInputItem[] = []; - // #3642: canonical granularity splits one user item into one core per - // part, so consecutive user cores are a RUN that rebuilds as ONE wire - // item. Full coverage (every part's core present, run length == part - // count) rebuilds the original item VERBATIM — byte-exact for the - // untouched case. A partial run (some parts' cores folded away) rebuilds - // a FILTERED view: only the surviving parts, never resurrecting folded - // content from the raw sidecar. - const flushUserRun = (run: CoreMessage[]) => { - if (run.length === 0) return; - if (run.length === 1) { - const message = run[0]!; - const raw = (message as BiliMessage) - .rawResponsesItem as ResponseInputItem | undefined; - const rawMessage = - raw?.type === "message" ? (raw as ResponseInputMessage) : undefined; - if ( - rawMessage && - userDisplayText( - rawMessage.content, - messageContent(rawMessage.content), - ) === (message.text ?? "") - ) - out.push(rawMessage); - else if ( - ((message as BiliMessage).imagePart || - (message as BiliMessage).text === CANONICAL_IMAGE_TEXT) && - rawMessage && - Array.isArray(rawMessage.content) - ) - out.push({ - type: "message", - role: "user", - content: rawMessage.content.filter( - (part) => part.type === "input_image", - ), - }); - else - out.push({ - type: "message", - role: "user", - content: message.text ?? "", - }); - return; - } - const rawCarrier = run.find( - (m) => (m as BiliMessage).rawResponsesItem !== undefined, - ) as BiliMessage | undefined; - const rawItem = rawCarrier?.rawResponsesItem as - | ResponseInputItem - | undefined; + // Single-core rendering (historical single-core path, byte-identical): + // verbatim when the core's text matches the item's display text, image + // items re-emit their input_image parts, otherwise plain text. + const flushUserCore = (message: CoreMessage) => { + const raw = (message as BiliMessage) + .rawResponsesItem as ResponseInputItem | undefined; const rawMessage = - rawItem?.type === "message" - ? (rawItem as ResponseInputMessage) - : undefined; - const fullCover = + raw?.type === "message" ? (raw as ResponseInputMessage) : undefined; + if ( rawMessage && - Array.isArray(rawMessage.content) && - rawMessage.content.length === run.length; - if (fullCover && rawMessage) { + userDisplayText( + rawMessage.content, + messageContent(rawMessage.content), + ) === (message.text ?? "") + ) out.push(rawMessage); + else if ( + ((message as BiliMessage).imagePart || + (message as BiliMessage).text === CANONICAL_IMAGE_TEXT) && + rawMessage && + Array.isArray(rawMessage.content) + ) + out.push({ + type: "message", + role: "user", + content: rawMessage.content.filter( + (part) => part.type === "input_image", + ), + }); + else + out.push({ + type: "message", + role: "user", + content: message.text ?? "", + }); + }; + // #3642/#4150: canonical granularity splits one user item into one core + // per part, so consecutive user cores form a RUN that spans ITEMS — each + // source item contributes its own contiguous slice of the run. The run is + // therefore rebuilt PER SOURCE ITEM: groups are keyed by wireIndex (the + // item's position in body.input), falling back to sidecar identity for + // foreign cores without wireIndex, and standalone for cores with neither. + // #4150 was exactly the conflation of this span: the FIRST sidecar core + // was taken as THE carrier for the whole run, so a string-content item + // ahead of a multi-part item starved the image cores of their parts (the + // image silently dropped) and the items merged into one. Per group: + // full coverage + unchanged bytes ⇒ the original item VERBATIM + // (byte-exact untouched case, cross-lane consistent with + // patchResponsesInput's slot.original fast path); + // partial survival ⇒ a FILTERED view of that item's surviving parts — + // images resolved from the group's OWN item, never resurrecting + // folded content from the raw sidecar (#3642 contract); + // no raw sidecar (text-only split item) ⇒ synthesize from the cores. + // Single-core groups keep the historical rendering exactly. + const flushUserRun = (run: CoreMessage[]) => { + if (run.length === 0) return; + if (run.length === 1) { + flushUserCore(run[0]!); return; } - const imageParts = - rawMessage && Array.isArray(rawMessage.content) - ? rawMessage.content.filter((part) => part.type === "input_image") - : []; - let img = 0; - const parts: ResponseContentPart[] = []; + type RunGroup = { + wire?: number; + rawKey?: unknown; + raw?: ResponseInputMessage; + cores: CoreMessage[]; + }; + const groups: RunGroup[] = []; for (const message of run) { const bili = message as BiliMessage; - // Image-ness is structural (parse-time flag), never text equality: - // render/ref-tag injection rewrites the marker, and image parts have - // no text slot to carry a tag. - if (bili.imagePart && bili.rawResponsesItem) { - const part = imageParts[img++]; - if (part) parts.push(part); - } else if (bili.text) { - parts.push({ type: "input_text", text: bili.text }); + const rawItem = bili.rawResponsesItem as ResponseInputItem | undefined; + let group = groups[groups.length - 1]; + const joinsCurrent = + group !== undefined && + (bili.wireIndex !== undefined + ? bili.wireIndex === group.wire + : group.rawKey !== undefined && + group.rawKey === bili.rawResponsesItem); + if (group === undefined || !joinsCurrent) { + group = { + ...(bili.wireIndex !== undefined + ? { wire: bili.wireIndex } + : { rawKey: rawItem }), + cores: [], + }; + groups.push(group); } + if (!group.raw && rawItem?.type === "message") + group.raw = rawItem as ResponseInputMessage; + group.cores.push(message); + } + for (const group of groups) { + if (group.cores.length === 1) { + flushUserCore(group.cores[0]!); + continue; + } + const raw = group.raw; + if (raw && Array.isArray(raw.content)) { + const producing = raw.content.filter(producesUserCore); + const full = producing.length === group.cores.length; + const unchanged = + full && + group.cores.every((m, k) => { + const part = producing[k]; + const bili = m as BiliMessage; + if (part?.type === "input_image") + return ( + bili.imagePart === true || + bili.text === CANONICAL_IMAGE_TEXT + ); + return ( + (bili.text ?? "") === + (typeof part?.text === "string" ? part.text : "") + ); + }); + if (full && unchanged) { + out.push(raw); + continue; + } + const imageParts = raw.content.filter( + (part) => part.type === "input_image", + ); + let img = 0; + const parts: ResponseContentPart[] = []; + for (const m of group.cores) { + const bili = m as BiliMessage; + // Structural flag only, no text-equality fallback here (unlike + // flushUserCore): every split image core carries imagePart, while + // a user text part may legitimately read "[image]" — equality + // would misclassify it and steal an image slot. + if (bili.imagePart === true) { + const part = imageParts[img++]; + if (part) parts.push(part); + } else if (bili.text) { + parts.push({ type: "input_text", text: bili.text }); + } + } + if (parts.length > 0) + out.push({ ...raw, content: parts } as ResponseInputItem); + continue; + } + const parts = group.cores.flatMap((m) => { + const bili = m as BiliMessage; + return bili.text + ? [{ type: "input_text" as const, text: bili.text }] + : []; + }); + if (parts.length > 0) + out.push({ type: "message", role: "user", content: parts }); } - out.push({ - type: "message", - role: "user", - content: parts.length > 0 ? parts : "", - }); }; let userRun: CoreMessage[] = []; for (const message of messages) { @@ -851,11 +927,30 @@ export function coreToResponses( }); } else if (message.role === "assistant") { if (message.contentType === "text") { - out.push({ - type: "message", - role: "assistant", - content: message.text ?? "", - }); + // #4150 observation 2: the assistant lane gets the same raw-sidecar + // verbatim fast path the user lane has — an array-content item whose + // text survived unrewritten round-trips BYTE-EXACT (shape included); + // stringified output diverged from patchResponsesInput's + // slot.original on the same state (prefix-cache seam, #3837 family). + // Rewritten text (ref-tag/render/demoted-thinking split) fails the + // equality and keeps the synthesized string form below. + const rawMessage = + raw?.type === "message" + ? (raw as ResponseInputMessage) + : undefined; + if ( + rawMessage && + rawMessage.role === "assistant" && + Array.isArray(rawMessage.content) && + messageContent(rawMessage.content) === (message.text ?? "") + ) + out.push(rawMessage); + else + out.push({ + type: "message", + role: "assistant", + content: message.text ?? "", + }); } else if (message.contentType === "tool-call") { const callId = message.toolCallId ?? `call_${message.id}`; if (customToolCallIds.has(callId)) { diff --git a/kernel/tests/issue4150-responses-split-rebuild.test.ts b/kernel/tests/issue4150-responses-split-rebuild.test.ts new file mode 100644 index 0000000000..ed916e39aa --- /dev/null +++ b/kernel/tests/issue4150-responses-split-rebuild.test.ts @@ -0,0 +1,222 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { + coreToResponses, + patchResponsesInput, + responsesToCore, + type ResponseInputItem, +} from "../src/wire/responses.js"; + +const IMG_A = "data:image/png;base64,AAAA"; +const IMG_B = "data:image/png;base64,BBBB"; + +type MessageItem = { + type: "message"; + role: string; + content: unknown; + [key: string]: unknown; +}; + +const userText = (content: string): MessageItem => ({ + type: "message", + role: "user", + content, +}); + +const multipart = (extra?: Record): MessageItem => ({ + type: "message", + role: "user", + content: [ + { type: "input_text", text: "look at this" }, + { type: "input_image", image_url: IMG_A }, + ], + ...extra, +}); + +const clone = (value: T): T => JSON.parse(JSON.stringify(value)); + +test("#4150 issue matrix: adjacent plain-text + multi-part rebuilds verbatim", () => { + const shapes: ResponseInputItem[][] = [ + // the reported bug: text BEFORE multipart dropped the image + [userText("plain question"), multipart()], + [userText("one"), userText("two"), multipart()], + // these two already survived pre-fix — must stay verbatim + [multipart(), userText("after")], + [multipart()], + // adjacent plain-text items used to merge into one array item + [userText("one"), userText("two")], + ]; + for (const input of shapes) { + const proj = responsesToCore({ model: "m", input }); + const out = coreToResponses(proj.msgs); + assert.deepEqual( + out, + clone(input), + `shape ${JSON.stringify(input)} not rebuilt verbatim`, + ); + } +}); + +test("#4150: each image resolves from its OWN item (no cross-item assignment)", () => { + const input: ResponseInputItem[] = [ + { + type: "message", + role: "user", + content: [{ type: "input_image", image_url: IMG_A }], + }, + { + type: "message", + role: "user", + content: [ + { type: "input_text", text: "second" }, + { type: "input_image", image_url: IMG_B }, + ], + }, + ]; + const proj = responsesToCore({ model: "m", input }); + assert.deepEqual(coreToResponses(proj.msgs), clone(input)); + // fold item 0 entirely: item 1 keeps its own image, not item 0's + const survivors = proj.msgs.filter((m) => m.wireIndex !== 0); + const out = coreToResponses(survivors); + assert.deepEqual(out, [clone(input[1])]); + assert.ok(!JSON.stringify(out).includes(IMG_A), "foreign image resurrected"); +}); + +test("#4150: merged image-only item after a text item stays verbatim (no marker leak)", () => { + const input: ResponseInputItem[] = [ + userText("q"), + { + type: "message", + role: "user", + content: [{ type: "input_image", image_url: IMG_A }], + }, + ]; + const proj = responsesToCore({ model: "m", input }); + const out = coreToResponses(proj.msgs); + assert.deepEqual(out, clone(input)); + assert.ok( + !JSON.stringify(out).includes("[image]"), + "canonical marker leaked into the wire", + ); +}); + +test("#4150: partial folds rebuild per-item views, never stale bytes", () => { + const input: ResponseInputItem[] = [ + userText("lead"), + multipart({ status: "in_progress" }), + userText("trail"), + ]; + const proj = responsesToCore({ model: "m", input }); + const slot = proj.layout[1]; + const [textCoreId, imageCoreId] = slot.coreIds ?? []; + assert.ok(textCoreId && imageCoreId, "multi-part item must split (#3642)"); + const textCore = proj.msgs.find((m) => m.id === textCoreId); + assert.ok(textCore); + + // fold the TEXT part: the image survives, resolved from its own item + let out = coreToResponses(proj.msgs.filter((m) => m.id !== textCoreId)); + assert.deepEqual(out, [ + clone(input[0]), + { + type: "message", + role: "user", + content: [{ type: "input_image", image_url: IMG_A }], + }, + clone(input[2]), + ]); + + // fold the IMAGE part: the surviving text core renders exactly as it does + // alone — a core's rendering must not depend on neighbouring runs + out = coreToResponses(proj.msgs.filter((m) => m.id !== imageCoreId)); + assert.deepEqual(out, [ + clone(input[0]), + { type: "message", role: "user", content: "look at this" }, + clone(input[2]), + ]); + assert.deepEqual(coreToResponses([textCore]), [out[1]]); + + // fold BOTH parts: the item drops entirely, neighbours intact + out = coreToResponses( + proj.msgs.filter((m) => m.id !== textCoreId && m.id !== imageCoreId), + ); + assert.deepEqual(out, [clone(input[0]), clone(input[2])]); + + // rewritten text core (ref-tag/render injection): the filtered view carries + // the NEW bytes plus the image — never the stale verbatim item + const rewritten = proj.msgs.map((m) => + m.id === textCoreId ? { ...m, text: "look at this (ref)" } : m, + ); + out = coreToResponses(rewritten); + assert.deepEqual(out, [ + clone(input[0]), + { + type: "message", + role: "user", + status: "in_progress", + content: [ + { type: "input_text", text: "look at this (ref)" }, + { type: "input_image", image_url: IMG_A }, + ], + }, + clone(input[2]), + ]); +}); + +test("#4150 obs 2: assistant array-content item round-trips verbatim", () => { + const input: ResponseInputItem[] = [ + { type: "message", role: "user", content: "weather?" }, + { type: "function_call", call_id: "call_1", name: "get_weather", arguments: "{}" }, + { type: "function_call_output", call_id: "call_1", output: "18C rainy" }, + { + type: "message", + role: "assistant", + content: [{ type: "output_text", text: "It is 18C and rainy." }], + }, + ]; + const proj = responsesToCore({ model: "m", input }); + const out = coreToResponses(proj.msgs); + assert.deepEqual(out, clone(input)); +}); + +test("#4150 obs 2: rewritten assistant text keeps the synthesized string form", () => { + const input: ResponseInputItem[] = [ + { type: "message", role: "user", content: "weather?" }, + { type: "function_call", call_id: "call_1", name: "get_weather", arguments: "{}" }, + { type: "function_call_output", call_id: "call_1", output: "18C rainy" }, + { + type: "message", + role: "assistant", + content: [{ type: "output_text", text: "It is 18C and rainy." }], + }, + ]; + const proj = responsesToCore({ model: "m", input }); + const assistantCore = proj.msgs.find( + (m) => m.role === "assistant" && m.contentType === "text", + ); + assert.ok(assistantCore); + const edited = proj.msgs.map((m) => + m.id === assistantCore!.id ? { ...m, text: `${m.text} (edited)` } : m, + ); + const out = coreToResponses(edited); + const last = out[out.length - 1] as { content: unknown }; + assert.equal(last.content, "It is 18C and rainy. (edited)"); +}); + +test("#4150: full state — patch lane and full-rebuild lane agree byte-for-byte", () => { + const input: ResponseInputItem[] = [ + { role: "user", content: "first question" } as unknown as ResponseInputItem, + multipart(), + { type: "function_call", call_id: "call_1", name: "get_weather", arguments: "{}" }, + { type: "function_call_output", call_id: "call_1", output: "18C rainy" }, + { + type: "message", + role: "assistant", + content: [{ type: "output_text", text: "It is 18C and rainy." }], + }, + userText("thanks"), + ]; + const proj = responsesToCore({ model: "m", input }); + const patched = patchResponsesInput(proj, proj.msgs) as ResponseInputItem[]; + const rebuilt = coreToResponses(proj.msgs); + assert.deepEqual(patched, rebuilt, "lanes diverge on the untouched state"); +}); diff --git a/package-lock.json b/package-lock.json index ebfaf66b7e..ea804d8867 100644 --- a/package-lock.json +++ b/package-lock.json @@ -50,7 +50,7 @@ }, "kernel": { "name": "acp-kernel", - "version": "0.0.147", + "version": "0.0.148", "dev": true, "license": "MIT", "devDependencies": { diff --git a/tests/verif-wire-responses-stability.test.ts b/tests/verif-wire-responses-stability.test.ts new file mode 100644 index 0000000000..5840e118c9 --- /dev/null +++ b/tests/verif-wire-responses-stability.test.ts @@ -0,0 +1,209 @@ +/** + * wire/responses stability judge (direction 1). + * + * Targets: kernel/src/wire/responses.ts — responsesToCore (projection), + * coreToResponses (outbound rebuild), patchResponsesInput (fold/edit patch). + * This is the responses lane's OUTBOUND path and the concrete outlet of the + * #3837 family on that lane (stamped-item recount on abort/retry) plus the + * #3642 split-item semantics. + * + * Replay judge (real kernel functions, no solver): + * P1 verbatim round-trip — full-coverage rebuild is byte-identical to the + * original wire input (#3642 verbatim contract, incl. type-less Easy + * shorthand staying type-less). + * P2 fold locality — folding a whole tool round removes exactly its items; + * every surviving item is verbatim. + * P3 patch idempotence. + * P4 stamped consistency — unchanged type-less survivors stay type-less + * across folds/patches (the #3837 "stamped count changed" dimension). + * P5 append locality — a fresh user core appends at the tail; older + * survivors verbatim. + * P6 #3642 partial survival — folding one part of a split multi-part item + * rebuilds a filtered view (surviving parts only, images in order); + * folding all parts drops the item. + * P7 text-edit locality — editing one message rewrites exactly its item. + */ + +import assert from "node:assert/strict"; +import { describe, it } from "node:test"; + +import { + responsesToCore, + coreToResponses, + patchResponsesInput, + type ResponseInputItem, +} from "../kernel/src/wire/responses.js"; +import type { BiliMessage } from "../kernel/src/wire/bili-message.js"; + +// --- fixture: a realistic Codex-style responses input --------------------- + +const INPUT: ResponseInputItem[] = [ + // Easy shorthand, TYPE-LESS (legal per normalizeEasyInputMessage) + { role: "user", content: "first question" } as unknown as ResponseInputItem, + { + type: "message", + role: "user", + content: [ + { type: "input_text", text: "look at this" }, + { type: "input_image", image_url: "data:image/png;base64,AAAA" }, + ], + }, + { type: "function_call", call_id: "call_1", name: "get_weather", arguments: '{"city":"Tokyo"}' }, + { type: "function_call_output", call_id: "call_1", output: "18C, rain" }, + { type: "message", role: "assistant", content: [{ type: "output_text", text: "It is 18C and rainy." }] }, + { type: "message", role: "user", content: "thanks" }, +]; + +// The system's verbatim contract is "verbatim modulo the Easy-stamp": +// normalizeEasyInputItem stamps {type:"message"} onto type-less shorthand +// INSIDE responsesToCore, and every outbound (turn 1 included) is rebuilt +// from that stamped form — so the byte-stable baseline is the STAMPED +// input, not the raw shorthand. (Consistent from turn 1 = cache-stable; +// the stamp itself never reaches the upstream unstamped.) +const STAMPED: ResponseInputItem[] = INPUT.map((item, i) => + i === 0 ? { ...(item as { role: string; content: string }), type: "message" } : item, +); + +const proj = responsesToCore({ model: "gpt-5", input: INPUT }); + +/** layout tells us which cores belong to which wire item */ +const coreIdsOf = (wireIndex: number): string[] => { + const slot = proj.layout[wireIndex]; + assert.ok(slot, `layout slot ${wireIndex} missing`); + return slot.coreIds ?? (slot.coreId ? [slot.coreId] : []); +}; + +const dropCores = (msgs: BiliMessage[], folded: string[]): BiliMessage[] => { + const gone = new Set(folded); + return msgs.filter((m) => !gone.has(m.id)); +}; + +const verbatim = (items: ResponseInputItem[], what: string): void => { + // every item in `items` must appear (deep-equal, order preserved) in the + // STAMPED baseline — the verbatim-survival contract + let cursor = 0; + for (const item of items) { + let found = -1; + for (let i = cursor; i < STAMPED.length; i++) { + if (JSON.stringify(STAMPED[i]) === JSON.stringify(item)) { found = i; break; } + } + assert.ok(found >= 0, `${what}: item is not a verbatim survivor: ${JSON.stringify(item)}`); + cursor = found + 1; + } +}; + +// --- judge ---------------------------------------------------------------- + +describe("verif: wire responses stability (responsesToCore/coreToResponses/patchResponsesInput)", () => { + it("P0 non-vacuity: projection covers every item with cores", () => { + assert.equal(proj.layout.length, INPUT.length); + const allCores = proj.layout.flatMap((s) => s.coreIds ?? (s.coreId ? [s.coreId] : [])); + assert.equal(allCores.length, proj.msgs.length); + assert.equal(coreIdsOf(1).length, 2, "multi-part item must split into 2 cores (#3642)"); + }); + + it("P1 #4150 FIXED: text-before-multipart rebuild is verbatim (image survives)", () => { + const out = coreToResponses(proj.msgs); + // #4150 fixed: flushUserRun groups the run PER SOURCE ITEM instead of + // taking the first sidecar core as THE carrier, so a string-content + // item ahead of a multi-part item no longer starves the image cores — + // every item round-trips byte-exact, image included. + assert.deepEqual(out, STAMPED, "#4150 regressed? — rebuild must be verbatim"); + assert.ok(JSON.stringify(out).includes("input_image"), "image lost in the #4150 shape"); + }); + + it("P1b round-trip WITHOUT the #4150 shape (all items verbatim)", () => { + const input = INPUT.slice(1); // drop the leading plain-text user item + const p = responsesToCore({ model: "gpt-5", input }); + const out = coreToResponses(p.msgs); + // #4150 fixed: the assistant array-content item round-trips byte-exact + // too (same raw-sidecar fast path as the user lane) — the cross-lane + // inconsistency with patchResponsesInput's slot.original is resolved. + assert.deepEqual(out, JSON.parse(JSON.stringify(input))); + assert.ok(JSON.stringify(out).includes("input_image"), "image lost without the #4150 shape?"); + }); + + it("P2 fold locality: folding the tool round removes exactly its items", () => { + const folded = [...coreIdsOf(2), ...coreIdsOf(3)]; + const remaining = dropCores(proj.msgs, folded); + const out = patchResponsesInput(proj, remaining) as ResponseInputItem[]; + assert.equal(out.length, INPUT.length - 2, "exactly the tool round's items drop"); + verbatim(out, "P2"); + // and the rebuilt output for the survivors equals the untouched subset + const expect = STAMPED.filter((_, i) => i !== 2 && i !== 3); + assert.deepEqual(out, expect); + }); + + it("P3 patch idempotence", () => { + const folded = [...coreIdsOf(2), ...coreIdsOf(3)]; + const remaining = dropCores(proj.msgs, folded); + const a = patchResponsesInput(proj, remaining); + const b = patchResponsesInput(proj, remaining); + assert.deepEqual(a, b); + }); + + it("P4 stamped consistency: a survivor's bytes are IDENTICAL between full and folded patches (#3837 recount family)", () => { + const folded = [...coreIdsOf(2), ...coreIdsOf(3), ...coreIdsOf(4)]; + const full = patchResponsesInput(proj, proj.msgs); + const afterFold = patchResponsesInput(proj, dropCores(proj.msgs, folded)); + const first = afterFold[0] as { role?: string; type?: string }; + // Easy type-less shorthand is stamped CONSISTENTLY (turn 1 included), + // and the stamp does not flip across folds: + assert.equal(first.type, "message"); + assert.equal(first.role, "user"); + assert.deepEqual(first, full[0], "first item's bytes changed across a fold of OTHER items"); + }); + + it("P5 append locality: a fresh user core appends at the tail, survivors verbatim", () => { + const fresh: BiliMessage = { id: "fresh-1", role: "user", contentType: "text", text: "one more thing" }; + const out = patchResponsesInput(proj, [...proj.msgs, fresh]) as ResponseInputItem[]; + assert.equal(out.length, INPUT.length + 1); + verbatim(out.slice(0, -1), "P5 prefix"); + const tail = out.at(-1) as { type?: string; role?: string; content?: unknown }; + assert.equal(tail.role, "user"); + const text = typeof tail.content === "string" ? tail.content : JSON.stringify(tail.content); + assert.ok(text.includes("one more thing"), "fresh message not appended: " + text); + }); + + it("P6 #3642 partial survival: filtered view keeps surviving parts in order", () => { + const [textCore, imageCore] = coreIdsOf(1); + assert.ok(textCore && imageCore); + + // fold the IMAGE part → only input_text survives + const outA = patchResponsesInput(proj, dropCores(proj.msgs, [imageCore])) as ResponseInputItem[]; + const itemA = outA.find((i: ResponseInputItem) => JSON.stringify(i).includes("look at this")) as { content: unknown }; + const partsA = itemA.content as { type: string }[]; + assert.equal(partsA.length, 1, "folded image part still present?"); + assert.equal(partsA[0]!.type, "input_text"); + + // fold the TEXT part → only the image survives + const outB = patchResponsesInput(proj, dropCores(proj.msgs, [textCore])) as ResponseInputItem[]; + const itemB = outB.find((i: ResponseInputItem) => JSON.stringify(i).includes("input_image")) as { content: unknown }; + assert.ok(itemB, "image part dropped with text part"); + const partsB = itemB.content as { type: string }[]; + assert.equal(partsB.length, 1); + assert.equal(partsB[0]!.type, "input_image"); + assert.ok(!JSON.stringify(outB).includes("look at this"), "folded text resurrected from sidecar"); + + // fold BOTH → item gone entirely + const outC = patchResponsesInput(proj, dropCores(proj.msgs, [textCore, imageCore])) as ResponseInputItem[]; + assert.ok(!JSON.stringify(outC).includes("look at this")); + assert.ok(!JSON.stringify(outC).includes("input_image")); + verbatim(outC, "P6 both-folded survivors"); + }); + + it("P7 text-edit locality: editing one message rewrites exactly its item", () => { + const edited = proj.msgs.map((m) => + m.text === "thanks" ? { ...m, text: "thanks!!" } : m, + ); + const out = patchResponsesInput(proj, edited); + assert.equal(out.length, INPUT.length); + for (let i = 0; i < INPUT.length; i++) { + if (i === 5) continue; + assert.deepEqual(out[i], STAMPED[i], `item ${i} changed by an unrelated edit`); + } + const last = out[5] as { content: unknown }; + const text = typeof last.content === "string" ? last.content : JSON.stringify(last.content); + assert.ok(String(text).includes("thanks!!"), "edit did not land: " + text); + }); +}); diff --git a/verif/README.md b/verif/README.md index 16484fae15..42aa012524 100644 --- a/verif/README.md +++ b/verif/README.md @@ -39,6 +39,7 @@ VERIF_PYTHON=/path/to/python-with-cvc5 node --import tsx --test tests/verif-bug* | `verif-bug3805-durable-ref-migration` | 定向不变量 | 保护解除时 BLOCKED→数字引用迁移给已发送字节中途加标签;非 durable 追加稳定(UNSAT) | | `verif-generic-append-stability` | **任意输入** | 消息全符号化、唯一不变量"只增不删的字节稳定":Q1 SAT(#3805 形状泛化重现)、Q2 SAT(**求解器自己发现的新 bug #3945**:id 撞车重打标,零保护解除)、Q3 UNSAT(排除两个已知形状后界内无第三种) | | `verif-induction-append-stability` | **归纳判官** | 任意消息数的 append-only 稳定性:前缀抽象成一个符号绑定,归纳步 UNSAT(id 不重复时任何前缀状态都动不了);撤掉约束 SAT = #3945;真代码随机抽查 60 轮 | +| `verif-wire-responses-stability` | 回放判官(无求解器) | responses 编码三件套(responsesToCore/coreToResponses/patchResponsesInput):verbatim 往返(钉住 #4150 文本前邻丢图)、折叠局部性、patch 幂等、Easy-stamp 一致性(#3837 计数维度)、追加局部性、#3642 部分幸存、文本编辑局部性 | | `verif-adapter-wire-stability` | 回放判官(无求解器) | 三个 adapter 的 buildRequest 出站重建:已完成轮次字节跨轮稳定(P1/P3,轮合并感知——开轮可长,完成轮不可动)、幂等+输入不可变、notes 只动尾部、工具轮全量保全。#1637/#1876/#2499 的字节前缀性质推广到三家并钉住 | | `verif-view-latch-discipline` | **锁存纪律判官**(#3989) | 安静轮 latch 字节逐字回放(Q1 UNSAT)+ 新鲜消息直通不受污染(Q2 UNSAT,任意 pre/post/latch 文本)+ 真代码行为矩阵(quiet 回放 / 到达锁存 / 折叠重置 / 稳态零churn) | | `verif-conflict-watch` / `verif-snapshot-render` / `verif-wire-anthropic-roundtrip` / `verif-wire-google-rebuild-stability` | 定向不变量 | conflict-watch 降落门限纪律(#2709/#3806 形状,边界 749/1000 vs 750/1000);snapshot-render 尾保真(发现 **#4052**:新消息与已压缩消息同文本被整条丢弃,钉为翻转断言);anthropic 同类对序+载荷保真;google 重建 append-only 稳定 |