From c9a81ddde98900a27f28910fb90a92addce3010f Mon Sep 17 00:00:00 2001 From: Jonathan Shin Date: Sun, 25 Oct 2026 16:09:58 -0700 Subject: [PATCH 1/3] fix: preserve changed user instructions during reconciliation --- src/fold-reconcile.ts | 3 + .../issue2915-order-centric-reconcile.test.ts | 60 +++++++++++++++---- 2 files changed, 50 insertions(+), 13 deletions(-) diff --git a/src/fold-reconcile.ts b/src/fold-reconcile.ts index ea16e6c838..cc93ddd1a9 100644 --- a/src/fold-reconcile.ts +++ b/src/fold-reconcile.ts @@ -1315,6 +1315,8 @@ export function planReconciliation( const anchor = anchors[gap.oldId]; const message = byId.get(gap.newId); if (anchor === undefined || message === undefined) continue; + // Changed user instructions must reach the model, even between unchanged neighbors. + if (anchor.r === "user" && anchor.c !== "image") continue; if (anchor.r !== undefined && anchor.r !== message.role) continue; const len = message.text?.length ?? 0; if (Math.abs(len - anchor.b) > Math.max(256, anchor.b >> 2)) continue; @@ -1371,6 +1373,7 @@ export function planReconciliation( const residualMiddle = missingMiddle.filter((id) => !plan.claims.has(id)); if (residualMiddle.length >= SHAPE_MASS_MIN) { const looseProse = (anchor: FoldAnchor, m: CoreMessage): boolean => { + if (anchor.r === "user" && anchor.c !== "image") return false; if (anchor.wh === undefined || anchor.sh === undefined) return false; if (anchor.r !== undefined && anchor.r !== m.role) return false; const len = m.text?.length ?? 0; diff --git a/tests/issue2915-order-centric-reconcile.test.ts b/tests/issue2915-order-centric-reconcile.test.ts index 46c24a54ea..ded2be064e 100644 --- a/tests/issue2915-order-centric-reconcile.test.ts +++ b/tests/issue2915-order-centric-reconcile.test.ts @@ -28,6 +28,7 @@ // again (streak reset clears the latch) — upgrade-in-place recovery. import { test, describe } from "node:test"; import assert from "node:assert/strict"; +import { prune } from "acp-kernel"; import { simhashHex, hammingHex } from "../src/affinity-simhash.ts"; import { normalizedIdentity, @@ -42,7 +43,7 @@ import { } from "../src/fold-reconcile.ts"; import type { CoreMessage } from "acp-kernel"; import { SessionStore, _setStoreForTest } from "../src/persist.ts"; -import type { Session } from "../src/session.ts"; +import { createSession, type Session } from "../src/session.ts"; function msg(id: string, role: string, text: string, extra?: Partial): CoreMessage { return { id, role, contentType: "text", text, ...extra } as CoreMessage; @@ -116,6 +117,43 @@ function planFor(old: CoreMessage[], next: CoreMessage[], positions?: { ids: str const storedOf = (msgs: CoreMessage[]): { ids: string[]; canon: string[] } => ({ ids: msgs.map((m) => m.id as string), canon: msgs.map((m) => positionalFingerprint(m)) }); +for (const [name, editedIndexes, before, after] of [ + ["changed approval", [5], "Deployment is APPROVED.", "Deployment is FORBIDDEN."], + ["reordered instructions", [5], "Allow Alice; deny Bob.", "Deny Alice; allow Bob."], + ["several reordered instructions", [5, 7, 9, 11], "Allow Alice; deny Bob.", "Deny Alice; allow Bob."], +] satisfies Array<[string, number[], string, string]>) { + test(`outgoing history retains ${name} after message ids change`, () => { + const originals = Array.from({ length: 30 }, (_, i): CoreMessage => msg( + `original-${i}`, i % 2 === 0 ? "assistant" : "user", + `Historical context for step ${i}. `.repeat(100) + (editedIndexes.includes(i) ? before : ""), + )); + const covered = Array.from({ length: 10 }, (_, i) => `original-${i + 3}`); + const session = createSession(`instruction-edit-${name}`); + session.state.blocks.push({ + blockId: "instruction-block", runId: "instruction-run", tier: 1, + summary: before, directMessageIds: [...covered], effectiveMessageIds: [...covered], + directBlockIds: [], compressedTokens: 4000, createdAt: 1, + survivedCount: 0, generation: "young", active: true, + }); + reconcileFoldCoverage(session, originals, opts()); + const resent = originals.map((m, i): CoreMessage => ({ + ...m, id: `resent-${i}`, + text: editedIndexes.includes(i) ? (m.text ?? "").replace(before, after) : m.text, + })); + resent.push(msg("continue", "user", "Continue the current task.")); + reconcileFoldCoverage(session, resent, opts()); + const outgoing = prune(resent, session.state); + for (const i of editedIndexes) { + assert.ok(outgoing.some((m) => m.id === `resent-${i}` && m.text?.includes(after)), + `the corrected instruction at ${i} must reach the model`); + } + assert.ok(outgoing.some((m) => m.id === "acp_summary_instruction-block"), + "unchanged history remains compressed"); + assert.ok(!outgoing.some((m) => m.id === "resent-4"), + "an unchanged covered message stays folded"); + }); +} + describe("Pass 0 order-centric resync (#2915)", () => { test("#2821 replay: full id re-mint + changed preamble + appended turn — order carries, one resync, no mass loss", () => { const old = history(); @@ -130,21 +168,17 @@ describe("Pass 0 order-centric resync (#2915)", () => { assert.equal(plan.byTwin, 0, "positional coverage leaves nothing for the twin pass"); }); - test("replacement island mid-history: resync claims both flanks, the bracket tier claims the slot (owner rule)", () => { + test("replacement user message stays unmatched while resync preserves both flanks", () => { const old = history(); const next = remint(old, "g1"); next[5] = msg("g1_5", "user", "EDITED: content genuinely changed"); // index 5 island (m3, prose) const plan = planFor(old, next, storedOf(old)); - // the island content is neither normalized-equal nor simhash-close - // (JSON vs prose ≈ 30 bits), but it sits at the SAME INDEX between - // verified claims — owner rule: flanks match → the fold asserts the - // slot, not the stale bytes. assert.ok(plan.resyncs >= 1, `expected resyncs, got ${plan.resyncs}`); assert.equal(plan.byPos, 11); - assert.equal(plan.byOrder, 1, "the bracket tier claims the island slot"); - assert.deepEqual(plan.unmatched, [], "no island loss"); + assert.equal(plan.byOrder, 0, "neighboring messages cannot authorize hiding a user edit"); + assert.deepEqual(plan.unmatched, ["m3"], "the changed user message re-enters the wire"); assert.deepEqual(plan.claims.get("end"), ["g1_11"], "the tail span past the island keeps coverage"); - assert.deepEqual(plan.claims.get("m3"), ["g1_5"]); + assert.equal(plan.claims.has("m3"), false); }); test("insertion (net offset) still lands on the flank scans — resync never mispairs shifted cells", () => { @@ -323,15 +357,15 @@ describe("gap resolution tiers (owner rule 2026-10-12: any channel → flanks assert.deepEqual(plan.claims.get("m4"), ["g1_6"]); }); - test("delete + insert at the same index: the bracket claims the slot (owner-sanctioned stale-cell tradeoff)", () => { + test("delete + insert of a user message at the same index leaves the replacement visible", () => { const old = history(); const next = remint(old, "g1"); next[5] = msg("g1_5", "user", "a genuinely different replacement turn"); // m3 replaced in place const plan = planFor(old, next, storedOf(old)); assert.equal(plan.byPos, 11); - assert.equal(plan.byOrder, 1, "same index, verified claims on BOTH sides → assert the slot"); - assert.deepEqual(plan.unmatched, []); - assert.deepEqual(plan.claims.get("m3"), ["g1_5"], "coverage rides the slot — documented tradeoff, not a guess"); + assert.equal(plan.byOrder, 0); + assert.deepEqual(plan.unmatched, ["m3"]); + assert.equal(plan.claims.has("m3"), false, "the old summary does not cover a new user instruction"); }); }); From cd3510b584ffca02a23173b684795ad48045f11a Mon Sep 17 00:00:00 2001 From: Jonathan Shin Date: Sun, 25 Oct 2026 16:21:02 -0700 Subject: [PATCH 2/3] test: address instruction-preservation review findings --- src/fold-reconcile.ts | 8 ++++---- tests/issue2915-order-centric-reconcile.test.ts | 6 ++++-- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/fold-reconcile.ts b/src/fold-reconcile.ts index cc93ddd1a9..5dd4d4bf3f 100644 --- a/src/fold-reconcile.ts +++ b/src/fold-reconcile.ts @@ -1283,7 +1283,7 @@ export function planReconciliation( // byTwin / byNorm / byTurn / byImage) run BELOW Pass 0, so by the time // this tier sees a gap cell every exact channel has already had its // bite — what remains is tiered corroboration, in order of evidence - // strength: + // strength. User-role messages stay on the exact passes above: // (a) simhash: a re-rendered cell (formatting churn normalization // still misses — key reordering beyond canonical-Json's reach, // template re-stamps) keeps its word-shingle mass, so a 64-bit @@ -1316,7 +1316,7 @@ export function planReconciliation( const message = byId.get(gap.newId); if (anchor === undefined || message === undefined) continue; // Changed user instructions must reach the model, even between unchanged neighbors. - if (anchor.r === "user" && anchor.c !== "image") continue; + if (anchor.r === "user") continue; if (anchor.r !== undefined && anchor.r !== message.role) continue; const len = message.text?.length ?? 0; if (Math.abs(len - anchor.b) > Math.max(256, anchor.b >> 2)) continue; @@ -1362,7 +1362,7 @@ export function planReconciliation( // the words still on the wire. Doctrine (order carries, content gates, // the id is only a hint): when a MISSING MASSES of cells still carry // word evidence, pair them in order instead of diagnosing destruction. - // prose anchors: wordbag equality + simhash ≤ GAP bar (the same + // non-user prose anchors: wordbag equality + simhash ≤ GAP bar (the same // edit-honesty gate as the sim tier) — role-checked, length-guarded; // textless anchors (image families, no sh/wh by construction): k-th // family pairing, only when the anchor/candidate counts per @@ -1373,7 +1373,7 @@ export function planReconciliation( const residualMiddle = missingMiddle.filter((id) => !plan.claims.has(id)); if (residualMiddle.length >= SHAPE_MASS_MIN) { const looseProse = (anchor: FoldAnchor, m: CoreMessage): boolean => { - if (anchor.r === "user" && anchor.c !== "image") return false; + if (anchor.r === "user") return false; if (anchor.wh === undefined || anchor.sh === undefined) return false; if (anchor.r !== undefined && anchor.r !== m.role) return false; const len = m.text?.length ?? 0; diff --git a/tests/issue2915-order-centric-reconcile.test.ts b/tests/issue2915-order-centric-reconcile.test.ts index ded2be064e..45057cbff2 100644 --- a/tests/issue2915-order-centric-reconcile.test.ts +++ b/tests/issue2915-order-centric-reconcile.test.ts @@ -149,8 +149,10 @@ for (const [name, editedIndexes, before, after] of [ } assert.ok(outgoing.some((m) => m.id === "acp_summary_instruction-block"), "unchanged history remains compressed"); - assert.ok(!outgoing.some((m) => m.id === "resent-4"), - "an unchanged covered message stays folded"); + for (const i of [3, 4]) { + assert.ok(!outgoing.some((m) => m.id === `resent-${i}`), + "unchanged covered user and assistant messages stay folded"); + } }); } From c5279e632e6e83d4344d7d897138010080270640 Mon Sep 17 00:00:00 2001 From: Jonathan Shin Date: Tue, 27 Oct 2026 07:17:41 -0700 Subject: [PATCH 3/3] fix(reconcile): recover exact user text after line wrapping Restore formatting-only mass re-anchoring by requiring the unwrapped candidate to match the stored normalized identity. Keep user messages out of approximate matching and preserve changed punctuation and instruction order. Pin the non-user order-bracket path and quiet folded resends. Implementation drafted by Grok 4.6; design, review and verification by Codex. --- src/fold-reconcile.ts | 12 ++++-- .../issue2915-order-centric-reconcile.test.ts | 38 +++++++++++++++---- 2 files changed, 40 insertions(+), 10 deletions(-) diff --git a/src/fold-reconcile.ts b/src/fold-reconcile.ts index 5dd4d4bf3f..3789ca7f61 100644 --- a/src/fold-reconcile.ts +++ b/src/fold-reconcile.ts @@ -1362,7 +1362,9 @@ export function planReconciliation( // the words still on the wire. Doctrine (order carries, content gates, // the id is only a hint): when a MISSING MASSES of cells still carry // word evidence, pair them in order instead of diagnosing destruction. - // non-user prose anchors: wordbag equality + simhash ≤ GAP bar (the same + // text user anchors: exact normalizedIdentity after newline→space unwrap + // (line wrap that reconstructs the stored identity; never wordbag/simhash); + // other prose anchors: wordbag equality + simhash ≤ GAP bar (the same // edit-honesty gate as the sim tier) — role-checked, length-guarded; // textless anchors (image families, no sh/wh by construction): k-th // family pairing, only when the anchor/candidate counts per @@ -1373,11 +1375,15 @@ export function planReconciliation( const residualMiddle = missingMiddle.filter((id) => !plan.claims.has(id)); if (residualMiddle.length >= SHAPE_MASS_MIN) { const looseProse = (anchor: FoldAnchor, m: CoreMessage): boolean => { - if (anchor.r === "user") return false; - if (anchor.wh === undefined || anchor.sh === undefined) return false; if (anchor.r !== undefined && anchor.r !== m.role) return false; const len = m.text?.length ?? 0; if (Math.abs(len - anchor.b) > Math.max(256, anchor.b >> 2)) return false; + if (anchor.r === "user") { + if (anchor.c !== "text" || m.contentType !== "text") return false; + const unwrapped: CoreMessage = { ...m, text: (m.text ?? "").replace(/\r?\n/g, " ") }; + return normalizedIdentity(unwrapped) === anchor.n; + } + if (anchor.wh === undefined || anchor.sh === undefined) return false; const wh = wordBagDigest(canonicalTextOf(m.text ?? "")); if (wh !== anchor.wh) return false; const dist = hammingHex(anchor.sh, simhashHex(canonicalTextOf(m.text ?? ""))); diff --git a/tests/issue2915-order-centric-reconcile.test.ts b/tests/issue2915-order-centric-reconcile.test.ts index 45057cbff2..7b81cc5a3f 100644 --- a/tests/issue2915-order-centric-reconcile.test.ts +++ b/tests/issue2915-order-centric-reconcile.test.ts @@ -121,6 +121,7 @@ for (const [name, editedIndexes, before, after] of [ ["changed approval", [5], "Deployment is APPROVED.", "Deployment is FORBIDDEN."], ["reordered instructions", [5], "Allow Alice; deny Bob.", "Deny Alice; allow Bob."], ["several reordered instructions", [5, 7, 9, 11], "Allow Alice; deny Bob.", "Deny Alice; allow Bob."], + ["several punctuation edits", [5, 7, 9, 11], "Allow Alice; deny Bob.", "Allow Alice? deny Bob."], ] satisfies Array<[string, number[], string, string]>) { test(`outgoing history retains ${name} after message ids change`, () => { const originals = Array.from({ length: 30 }, (_, i): CoreMessage => msg( @@ -157,6 +158,34 @@ for (const [name, editedIndexes, before, after] of [ } describe("Pass 0 order-centric resync (#2915)", () => { + test("line-wrapped user history stays folded and the next resend is quiet", () => { + const originals = Array.from({ length: 30 }, (_, i): CoreMessage => msg( + `original-${i}`, i % 2 === 0 ? "assistant" : "user", + `Historical context for step ${i}. `.repeat(100), + )); + const covered = originals.slice(3, 13).map((m) => m.id!); + const session = createSession("instruction-line-wrap"); + session.state.blocks.push({ + blockId: "wrapped-block", runId: "wrapped-run", tier: 1, + summary: "Historical steps.", directMessageIds: [...covered], effectiveMessageIds: [...covered], + directBlockIds: [], compressedTokens: 4000, createdAt: 1, + survivedCount: 0, generation: "young", active: true, + }); + reconcileFoldCoverage(session, originals, opts()); + const reshaped = originals.map((m, i): CoreMessage => ({ + ...m, id: `wrapped-${i}`, text: (m.text ?? "").replace(/ /g, "\n"), + })); + reconcileFoldCoverage(session, reshaped, opts()); + const outgoing = prune(reshaped, session.state); + for (let i = 3; i < 13; i++) { + assert.ok(!outgoing.some((m) => m.id === `wrapped-${i}`), "unchanged user and assistant content stays folded"); + } + const logs: string[] = []; + reconcileFoldCoverage(session, reshaped, opts((_level, text) => logs.push(text))); + assert.ok(!logs.some((text) => text.includes("drifted") || text.includes("destroyed")), + "an unchanged resend must not keep reporting drift"); + }); + test("#2821 replay: full id re-mint + changed preamble + appended turn — order carries, one resync, no mass loss", () => { const old = history(); const next = remint(old, "g1", true, "brand new request turn"); @@ -337,13 +366,8 @@ describe("gap resolution tiers (owner rule 2026-10-12: any channel → flanks "The quick brown fox jumps over the lazy dog near the river bank at dawn while birds chirp"); const plan = planFor(old, next, storedOf(old)); assert.equal(plan.bySim, 0, "a word change is never swallowed by the simhash tier"); - // The bracket tier (same index, fenced between verified flanks — the - // owner-sanctioned delete+insert tradeoff, #2821) may still claim it: - // order evidence, not content similarity, is the carrier there. - const claimed = plan.byOrder === 1; - assert.ok(claimed || plan.unmatched.includes("m4"), - "falls through to the bracket tier or honest unmatched — never bySim"); - if (claimed) assert.deepEqual(plan.claims.get("m4"), ["g1_7"]); + assert.equal(plan.byOrder, 1, "verified flanks pin the non-user slot to the order bracket"); + assert.deepEqual(plan.claims.get("m4"), ["g1_7"]); }); test("mid-column deletion (#2915 review gap ③): flanks absorb the shift, the deleted id stays honestly unmatched", () => {