diff --git a/kernel/package-lock.json b/kernel/package-lock.json index eddb848f06..d56ced4841 100644 --- a/kernel/package-lock.json +++ b/kernel/package-lock.json @@ -1,12 +1,12 @@ { "name": "acp-kernel", - "version": "0.0.182", + "version": "0.0.183", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "acp-kernel", - "version": "0.0.182", + "version": "0.0.183", "license": "MIT", "devDependencies": { "@types/node": "^22.0.0", diff --git a/kernel/package.json b/kernel/package.json index ab632703e5..45eb3f8d39 100644 --- a/kernel/package.json +++ b/kernel/package.json @@ -1,6 +1,6 @@ { "name": "acp-kernel", - "version": "0.0.182", + "version": "0.0.183", "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/unit-projection.ts b/kernel/src/unit-projection.ts index e5b820e25d..1bee9c05dd 100644 --- a/kernel/src/unit-projection.ts +++ b/kernel/src/unit-projection.ts @@ -83,6 +83,11 @@ export interface UnitProjectionInput { * {@link buildProjectionShared}. Omitted callers (the single-shot * apply/report paths) compute inline exactly as before. */ shared?: ProjectionShared; + /** #5789 recovery ladder (internal): nested block ids kept ACTIVE even + * though they sit inside the span — the merge proceeds without consuming + * them. Set only by projectUnit's own shrink retry; external callers must + * never pass it. */ + excludeNestedBlockIds?: ReadonlySet; } export interface UnitProjectionFatal { @@ -213,7 +218,18 @@ export function projectUnit(input: UnitProjectionInput): UnitProjection { ? (Math.min(3, targetTier + 1) as CompressionTier) : 1; + // #5789: blocks excluded by the recovery ladder stay ACTIVE and are simply + // not consumed here — their ids drop out of effectiveMessageIds below + // automatically (the union iterates consumedBlockIds), and because they + // remain active they are part of preExistingCoverage, so none of their ids + // re-enter as direct content either. + const excludedNestedBlockIds = input.excludeNestedBlockIds + ? resolved.nestedBlockIds.filter((id) => + input.excludeNestedBlockIds!.has(id), + ) + : []; const consumedBlockIds = resolved.nestedBlockIds.filter((id) => { + if (input.excludeNestedBlockIds?.has(id)) return false; const block = blockById(input.state, id); return block?.active && block.tier === targetTier; }); @@ -335,14 +351,27 @@ export function projectUnit(input: UnitProjectionInput): UnitProjection { if (filteredIds.length === 0 && consumedBlockIds.length === 0) { const recentN = input.config.preserveRecentMessages; + // #5789: an empty-after-exclusion span is NOT the protected-zone case — + // every nested block was excluded by the recovery ladder because its + // coverage strands a live conflict. Zone fatals are treated as no-op + // successes by applySingleRange (#4521); this one must stay a hard + // error with actionable guidance. + const excludedFatal = + excludedNestedBlockIds.length > 0 + ? new Error( + `Merging ${resolved.nestedBlockIds.join(", ")} cannot proceed: excluding ${excludedNestedBlockIds.join(", ")} (their coverage would strand a live turn-integrity conflict) leaves nothing left to fold in ${input.startRef}..${input.endRef}. Fold the stranded visible partner(s) first with a plain message-range compress so the turn closes, or retry once those block(s) can fold whole.`, + ) + : null; return finish({ fatal: { - kind: "zone", - error: new Error( - `Range is entirely within the protected zone (the last ${recentN} messages and/or the most recent user message): ${hitRefs.join( - ", ", - )}. Adjust startId/endId to older messages.`, - ), + kind: excludedFatal ? "split" : "zone", + error: + excludedFatal ?? + new Error( + `Range is entirely within the protected zone (the last ${recentN} messages and/or the most recent user message): ${hitRefs.join( + ", ", + )}. Adjust startId/endId to older messages.`, + ), }, }); } @@ -482,23 +511,86 @@ export function projectUnit(input: UnitProjectionInput): UnitProjection { ).withdrawn; } if (stuck || current.size > 0) { - const sample = [...consumedCoverage].slice(0, 5).join(", "); - return finish({ - fatal: { - kind: "split", - error: new Error( - `Merging ${consumedBlockIds.join(", ")} cannot satisfy turn integrity without unfolding already-folded message(s) owned by those blocks (${sample}) — doing so would drop their existing coverage. Align the range to whole turns (do not cut through a reasoning/tool-call/result group) or merge fewer blocks.`, - ), - }, - }); + // #5789: a conflict whose withdrawn ids are ENTIRELY outside this + // merge's fold set pre-dates it — the merge neither created nor + // worsened it (every id it touches stays covered, owner aside). + // Refusing over such conflicts kills EVERY merge in a session that + // carries one historical split (the reported permanent T2 death: + // survivingCoverage is global, so one poison block dooms all + // merges, even of two adjacent clean blocks). Tolerate: inherit + // the conflict as-is and warn honestly. + // A conflict only binds THIS merge when it touches content the merge + // newly folds (filteredIds) or re-owns (consumed coverage transfers + // to the new block). Ids merely inside the span geometry, or owned + // by surviving/excluded blocks, keep their covered status either way + // — their conflicts are inherited, not caused. #4484's invariant + // (a merge never drops existing coverage) holds: nothing here + // unfolds a covered id into the visible stream permanently. + const mergeOwned = new Set([ + ...filteredIds, + ...consumedCoverage, + ]); + const relevantUnresolved = [...current].filter((id) => + mergeOwned.has(id), + ); + if (relevantUnresolved.length === 0) { + // Merge-neutral: inherit and continue to the success path below + // (do NOT return — the fatal must be skipped). + warnings.push( + `Inherited a pre-existing turn-integrity conflict this merge does not touch (${[...current].slice(0, 6).map((id) => input.state.messageRefs.byRaw[id] ?? id).join(", ")}): the partner(s) are already covered by other blocks or still visible. Left as-is — fold the visible partner(s) into coverage to clear it.`, + ); + } else { + if ((input.excludeNestedBlockIds?.size ?? 0) < 64) { + const ownerByMessageId = new Map(); + for (const bid of consumedBlockIds) { + const block = blockById(input.state, bid); + if (!block) continue; + for (const id of block.effectiveMessageIds) + ownerByMessageId.set(id, bid); + } + const offending = [ + ...new Set( + relevantUnresolved + .filter((id) => !survivingCoverage.has(id)) + .map((id) => ownerByMessageId.get(id)) + .filter((bid): bid is string => bid !== undefined), + ), + ].sort(); + if (offending.length > 0) { + const retried = projectUnit({ + ...input, + excludeNestedBlockIds: new Set([ + ...(input.excludeNestedBlockIds ?? []), + ...offending, + ]), + }); + if (retried.fatal === null) { + warnings.push( + `Excluded ${offending.join(", ")} from this merge — their coverage cuts through a turn group whose partner(s) are still visible outside the span, and unfolding them would drop their only coverage. They stay ACTIVE (coverage intact) and can be distilled once the stranded partner(s) age into coverage; the merge proceeded around them.`, + ); + return retried; + } + } + } + return finish({ + fatal: { + kind: "split", + error: new Error( + actionableSplitError(consumedBlockIds, current, input), + ), + }, + }); + } } withdrawnIds = unfolded; for (const id of unfolded) effectiveMessageIds.delete(id); const beforeWithdraw = filteredIds.length; filteredIds = filteredIds.filter((id) => !unfolded.has(id)); - warnings.push( - `Withdrew ${beforeWithdraw - filteredIds.length} newly-folded message(s) from the merge to keep ${splitDesc} intact while preserving the existing coverage of ${consumedBlockIds.join(", ")}.`, - ); + if (beforeWithdraw !== filteredIds.length) { + warnings.push( + `Withdrew ${beforeWithdraw - filteredIds.length} newly-folded message(s) from the merge to keep ${splitDesc} intact while preserving the existing coverage of ${consumedBlockIds.join(", ")}.`, + ); + } } } } @@ -788,3 +880,45 @@ function resolveTargetTier( } return minTier; } + +/** #5789: actionable refusal for merges the exclusion ladder cannot complete + * — names the exact conflicted refs, who covers them, and the concrete next + * moves (fold the stranded partner first / sub-span around the offender). + * Keeps the "cannot satisfy turn integrity" phrase the host and the #4484 + * suite key on. */ +function actionableSplitError( + consumedBlockIds: string[], + unresolved: ReadonlySet, + input: UnitProjectionInput, +): string { + const refOf = (id: string): string => + input.state.messageRefs.byRaw[id] ?? id; + const consumedOwners = new Map(); + const survivingOwners = new Map(); + for (const block of activeBlocks(input.state)) { + const map = consumedBlockIds.includes(block.blockId) + ? consumedOwners + : survivingOwners; + for (const id of block.effectiveMessageIds) map.set(id, block.blockId); + } + const detail = [...unresolved] + .slice(0, 6) + .map((id) => { + const ref = refOf(id); + const owner = consumedOwners.get(id) ?? survivingOwners.get(id); + return owner ? `${ref} (covered by ${owner})` : `${ref} (visible)`; + }) + .join(", "); + const survivingOffenders = [ + ...new Set( + [...unresolved] + .map((id) => survivingOwners.get(id)) + .filter((bid): bid is string => bid !== undefined), + ), + ]; + const tail = + survivingOffenders.length > 0 + ? ` Block(s) ${survivingOffenders.join(", ")} survive this merge unchanged and keep the conflict alive — they can only distill once their partner(s) age into coverage.` + : ` Merge a sub-span that stops before or resumes after the offending block instead.`; + return `Merging ${consumedBlockIds.join(", ")} cannot satisfy turn integrity without dropping existing coverage: folding would strand ${detail}. Do not retry the same span — fold the stranded visible partner(s) first with a plain message-range compress so the turn closes.${tail}`; +} diff --git a/kernel/tests/issue4484-merge-coverage.test.ts b/kernel/tests/issue4484-merge-coverage.test.ts index f37aa187cb..bc50bec145 100644 --- a/kernel/tests/issue4484-merge-coverage.test.ts +++ b/kernel/tests/issue4484-merge-coverage.test.ts @@ -135,7 +135,8 @@ describe("#4484 merge preserves existing block coverage", () => { assert.equal(computeIntegrityWithdrawals(messages, after).withdrawn.size, 0); }); - it("impossible merge: fails fast up-front without touching existing coverage", () => { + // b2 covers ONLY the call half of the Read pair — its tail cut mid-tool-group. + function buildMidPairFixture() { const core = createCore(); const messages: CoreMessage[] = [ { id: "old-user", role: "user", contentType: "text", text: LONG }, @@ -160,9 +161,6 @@ describe("#4484 merge preserves existing block coverage", () => { if (seeded.result.blocksCreated !== 1 || seeded.result.errors.length !== 0) { throw new Error(`fixture seed failed: ${JSON.stringify(seeded.result)}`); } - // b2 covers ONLY the call half of the Read pair — its tail cut mid-tool-group. - // No consistent non-regressive candidate exists: folding the call splits its - // pair, unfolding it strands a message whose only covering block is consumed. seeded.state.blocks.push({ ...seeded.state.blocks[0]!, blockId: "b2", @@ -176,13 +174,45 @@ describe("#4484 merge preserves existing block coverage", () => { seeded.state.nextBlockId = 3; seeded.state.nextRunId = 3; seeded.state.stats.compressionCount = 2; - const before = coveredMessageIds(seeded.state); + return { core, messages, state: seeded.state }; + } + + it("mid-pair block inside a larger span: merge completes AROUND it, coverage untouched", () => { + const { core, messages, state } = buildMidPairFixture(); + const before = coveredMessageIds(state); const res = core.applyCompression({ messages, - state: seeded.state, + state, config, ranges: [{ startRef: "b1", endRef: "b2", summary: SUMMARY }], }); + // #5789 evolution: the old gate refused this span outright ("cannot + // satisfy turn integrity"); the recovery ladder now keeps the defective + // block ACTIVE and folds what can fold around it. The invariant this + // suite pins — a merge never drops existing coverage — is unchanged. + assert.equal(res.result.errors.length, 0, JSON.stringify(res.result)); + assert.equal(res.result.blocksCreated, 1); + assert.deepEqual(lostCoverage(before, res.state), [], "no existing coverage may be dropped"); + const byId = new Map(res.state.blocks.map((b) => [b.blockId, b])); + assert.equal(byId.get("b2")?.active, true, "defective block stays active with its coverage"); + assert.deepEqual(new Set(byId.get("b2")!.effectiveMessageIds), new Set(["read-call"])); + assert.equal(byId.get("b1")?.active, false, "b1 must be consumed"); + const merged = res.state.blocks.find((b) => b.active && b.blockId !== "b2"); + assert.ok(merged, "a merged block must exist"); + assert.ok(!merged.effectiveMessageIds.includes("read-call"), "the split call must not be re-claimed by the new block"); + assert.ok(merged.effectiveMessageIds.includes("old-user")); + assert.ok(res.result.warnings.some((w) => w.includes("b2")), JSON.stringify(res.result.warnings)); + }); + + it("impossible merge: a range whose only content is the defective block refuses actionably", () => { + const { core, messages, state } = buildMidPairFixture(); + const before = coveredMessageIds(state); + const res = core.applyCompression({ + messages, + state, + config, + ranges: [{ startRef: "b2", endRef: "b2", summary: SUMMARY }], + }); assert.equal(res.result.errors.length, 1, JSON.stringify(res.result)); assert.equal(res.result.blocksCreated, 0); assert.match(res.result.errors[0]!, /cannot satisfy turn integrity/); diff --git a/kernel/tests/issue5789-tier2-merge-ladder.test.ts b/kernel/tests/issue5789-tier2-merge-ladder.test.ts new file mode 100644 index 0000000000..7bed06d93a --- /dev/null +++ b/kernel/tests/issue5789-tier2-merge-ladder.test.ts @@ -0,0 +1,274 @@ +import { describe, it } from "node:test"; +import assert from "node:assert/strict"; +import { createCore } from "../src/compress.js"; +import { createInitialState, coveredMessageIds } from "../src/state.js"; +import { defaultConfig } from "../src/config.js"; +import { computeIntegrityWithdrawals } from "../src/turn-integrity.js"; +import type { CompressionState, CoreMessage } from "../src/types.js"; + +// #5789: in long sessions, Tier-2 distillation hard-failed PERMANENTLY. A +// historical block whose coverage cuts through a turn group (its call folded, +// the result still live OUTSIDE the merge span — legacy pre-widening shape; +// fresh folds self-align and never produce this) made every merge spanning it +// fail the turn-integrity gate: unfolding the covered half would drop its only +// coverage (the host pre-commit guard refuses such folds, #4484/#3322), and +// the advertised nudge range kept pointing at the same doomed span — breaker +// armed, context growing faster than before. +// +// Fixed behavior pinned here (apply-side only — the advertise-side screening +// of tier-distill runs lives in PR #5777, which owns compress.ts): +// 1. recovery ladder — the merge keeps the offending block(s) ACTIVE +// (coverage intact) and completes around them; +// 2. pre-existing merge-neutral conflicts are inherited with an honest +// warning instead of refusing every merge in the session; +// 3. unfixable conflicts refuse with ACTIONABLE text (stranded partner ref + +// owning block named), keeping the "cannot satisfy turn integrity" phrase +// the #4484 suite keys on. + +const isToolProtected = (t?: string) => + t === "mcp__bili__acp_rule" || t === "mcp__bili__compress"; +const isCompressTool = (t?: string) => + t === "mcp__bili__compress" || t === "compress"; + +const config = defaultConfig(255000, { + preserveRecentMessages: 0, + preserveRecentTokens: 0, + isToolProtected, + isCompressTool, +} as never); + +const SUMMARY = "Synthetic summary for this compression test. ".repeat(4); +const LONG = + "Synthetic historical content, safe for public reproduction. ".repeat(150); + +function lostCoverage(before: Set, state: CompressionState): string[] { + const after = coveredMessageIds(state); + return [...before].filter((id) => !after.has(id)); +} + +/** User-shape fixture (#5789): three clean T1 blocks plus a LAST block cut + * mid-tool-group — b4 covers reasoning+call while the result (l2) stays live + * AFTER the last block anchor. Refs: u0=m00001 a0=m00002 r1=m00003 + * k1=m00004 l1=m00005 u1=m00006 a1=m00007 r2=m00008 k2=m00009 l2=m00010 + * t2=m00011 new-user=m00012. */ +function buildUserShapeFixture() { + const core = createCore(); + const messages: CoreMessage[] = [ + { id: "u0", role: "user", contentType: "text", text: LONG }, + { id: "a0", role: "assistant", contentType: "text", text: "Previous work finished." }, + { id: "r1", role: "assistant", contentType: "reasoning", text: "Inspect the current state." }, + { id: "k1", role: "assistant", contentType: "tool-call", toolName: "Read", toolCallId: "c1", text: '{"path":"/tmp/a.txt"}' }, + { id: "l1", role: "tool", contentType: "tool-result", toolName: "Read", toolCallId: "c1", text: LONG }, + { id: "u1", role: "user", contentType: "text", text: LONG }, + { id: "a1", role: "assistant", contentType: "text", text: "More prior context." }, + { id: "r2", role: "assistant", contentType: "reasoning", text: "Check the tail." }, + { id: "k2", role: "assistant", contentType: "tool-call", toolName: "Read", toolCallId: "c2", text: '{"path":"/tmp/b.txt"}' }, + // b4 below covers r2,k2 ONLY — its tail cut mid-tool-group; l2 stays live. + { id: "l2", role: "tool", contentType: "tool-result", toolName: "Read", toolCallId: "c2", text: LONG }, + { id: "t2", role: "assistant", contentType: "text", text: "Tail turn done." }, + { id: "new-user", role: "user", contentType: "text", text: "Continue." }, + ]; + const state = createInitialState(); + let nextRef = 1; + for (const m of messages) { + const ref = `m${String(nextRef++).padStart(5, "0")}`; + state.messageRefs.byRaw[m.id] = ref; + state.messageRefs.byRef[ref] = m.id; + } + const seeded = core.applyCompression({ + messages, + state, + config, + ranges: [ + { + startRef: state.messageRefs.byRaw["u0"]!, + endRef: state.messageRefs.byRaw["a0"]!, + summary: SUMMARY, + }, + ], + }); + if (seeded.result.blocksCreated !== 1 || seeded.result.errors.length !== 0) { + throw new Error(`fixture seed failed: ${JSON.stringify(seeded.result)}`); + } + const push = (blockId: string, runId: string, ids: string[], tier = 1) => { + seeded.state.blocks.push({ + ...seeded.state.blocks[0]!, + blockId, + runId, + tier, + directMessageIds: [...ids], + effectiveMessageIds: [...ids], + compressedTokens: ids.length * 100, + startRef: state.messageRefs.byRaw[ids[0]!]!, + endRef: state.messageRefs.byRaw[ids[ids.length - 1]!]!, + }); + }; + push("b2", "r2", ["r1", "k1", "l1"]); + push("b3", "r3", ["u1", "a1"]); + push("b4", "r4", ["r2", "k2"]); + seeded.state.nextBlockId = 5; + seeded.state.nextRunId = 5; + seeded.state.stats.compressionCount = 4; + return { core, messages, state: seeded.state }; +} + +describe("#5789 tier-2 merge recovery ladder", () => { + it("tail-cut poison block: merging b1..b4 succeeds AROUND it (b4 stays active)", () => { + const { core, messages, state } = buildUserShapeFixture(); + const before = coveredMessageIds(state); + const res = core.applyCompression({ + messages, + state, + config, + ranges: [{ startRef: "b1", endRef: "b4", summary: SUMMARY }], + }); + assert.equal(res.result.errors.length, 0, JSON.stringify(res.result.errors)); + assert.equal(res.result.blocksCreated, 1, JSON.stringify(res.result)); + assert.deepEqual( + lostCoverage(before, res.state), + [], + "no existing coverage may be dropped", + ); + const byId = new Map(res.state.blocks.map((b) => [b.blockId, b])); + assert.equal(byId.get("b4")?.active, true, "poison block must stay active"); + for (const id of ["b1", "b2", "b3"]) { + assert.equal(byId.get(id)?.active, false, `${id} must be consumed`); + } + const merged = res.state.blocks.find((b) => b.active && b.blockId !== "b4"); + assert.ok(merged, "a new merged block must exist"); + assert.equal(merged.tier, 2); + assert.deepEqual( + new Set(merged.effectiveMessageIds), + new Set(["u0", "a0", "r1", "k1", "l1", "u1", "a1"]), + ); + assert.deepEqual([...merged.directBlockIds].sort(), ["b1", "b2", "b3"]); + assert.ok( + res.result.warnings.some((w) => w.includes("b4")), + `exclusion warning must name b4: ${JSON.stringify(res.result.warnings)}`, + ); + // The merge must not ADD integrity violations: the post-state withdrawn + // set is a subset of the pre-state one (b4's own split predates the merge). + const preW = computeIntegrityWithdrawals(messages, before).withdrawn; + const postW = computeIntegrityWithdrawals( + messages, + coveredMessageIds(res.state), + ).withdrawn; + for (const id of postW) { + assert.ok(preW.has(id), `merge introduced a violation for ${id}`); + } + }); + + it("block self-range b4..b4: idempotent no-op", () => { + // A poison block referenced against itself hard-failed on master with + // the same turn-integrity refusal as b1..b4 (its own coverage strands + // the live k2/l2/r2 conflict and nothing else is left to fold). With + // the recovery ladder the projection excludes b4 from consumption and + // folds nothing: an idempotent no-op success, never an error (a retry + // here must not arm the breaker or grow context). Pinned because #5789 + // touches the adjacent projection paths. + const { core, messages, state } = buildUserShapeFixture(); + const before = coveredMessageIds(state); + const res = core.applyCompression({ + messages, + state, + config, + ranges: [{ startRef: "b4", endRef: "b4", summary: SUMMARY }], + }); + assert.equal(res.result.errors.length, 0, JSON.stringify(res.result)); + assert.equal(res.result.blocksCreated, 0); + assert.ok( + res.result.warnings.some((w) => w.includes("folded nothing")), + JSON.stringify(res.result.warnings), + ); + assert.deepEqual(lostCoverage(before, res.state), [], "state must stay untouched"); + }); + + it("conflict owned by a SURVIVING higher-tier block: inherited with warning, clean blocks still distill", () => { + const core = createCore(); + const messages: CoreMessage[] = [ + { id: "u0", role: "user", contentType: "text", text: LONG }, + { id: "a0", role: "assistant", contentType: "text", text: "Previous work finished." }, + { id: "r2", role: "assistant", contentType: "reasoning", text: "Check the tail." }, + { id: "k2", role: "assistant", contentType: "tool-call", toolName: "Read", toolCallId: "c2", text: '{"path":"/tmp/b.txt"}' }, + { id: "u1", role: "user", contentType: "text", text: LONG }, + { id: "a1", role: "assistant", contentType: "text", text: "More prior context." }, + // Refs: u0=m00001 a0=m00002 r2=m00003 k2=m00004 u1=m00005 a1=m00006 + // l2=m00007 t2=m00008 new-user=m00009. + { id: "l2", role: "tool", contentType: "tool-result", toolName: "Read", toolCallId: "c2", text: LONG }, + { id: "t2", role: "assistant", contentType: "text", text: "Tail turn done." }, + { id: "new-user", role: "user", contentType: "text", text: "Continue." }, + ]; + const state = createInitialState(); + let nextRef = 1; + for (const m of messages) { + const ref = `m${String(nextRef++).padStart(5, "0")}`; + state.messageRefs.byRaw[m.id] = ref; + state.messageRefs.byRef[ref] = m.id; + } + const seeded = core.applyCompression({ + messages, + state, + config, + ranges: [ + { + startRef: state.messageRefs.byRaw["u0"]!, + endRef: state.messageRefs.byRaw["a0"]!, + summary: SUMMARY, + }, + ], + }); + if (seeded.result.blocksCreated !== 1 || seeded.result.errors.length !== 0) { + throw new Error(`fixture seed failed: ${JSON.stringify(seeded.result)}`); + } + const push = (blockId: string, runId: string, ids: string[], tier = 1) => { + seeded.state.blocks.push({ + ...seeded.state.blocks[0]!, + blockId, + runId, + tier, + directMessageIds: [...ids], + effectiveMessageIds: [...ids], + compressedTokens: ids.length * 100, + startRef: state.messageRefs.byRaw[ids[0]!]!, + endRef: state.messageRefs.byRaw[ids[ids.length - 1]!]!, + }); + }; + // b2 is a legacy TIER-2 block whose coverage cuts the c2 pair; it SURVIVES + // a T1 distill (not consumed), so no exclusion ladder can reach it. + push("b2", "r2", ["r2", "k2"], 2); + push("b3", "r3", ["u1", "a1"]); + seeded.state.nextBlockId = 4; + seeded.state.nextRunId = 4; + seeded.state.stats.compressionCount = 3; + const before = coveredMessageIds(seeded.state); + const res = core.applyCompression({ + messages, + state: seeded.state, + config, + ranges: [{ startRef: "b1", endRef: "b3", summary: SUMMARY }], + }); + // The conflict is PRE-EXISTING (b2 was already active with its mid-pair + // coverage) and merge-neutral: the clean blocks must still be able to + // distill — refusing here is what killed every merge in long sessions. + assert.equal(res.result.errors.length, 0, JSON.stringify(res.result)); + assert.equal(res.result.blocksCreated, 1); + assert.deepEqual(lostCoverage(before, res.state), []); + const byId = new Map(res.state.blocks.map((b) => [b.blockId, b])); + assert.equal(byId.get("b2")?.active, true, "surviving poison block untouched"); + assert.deepEqual(new Set(byId.get("b2")!.effectiveMessageIds), new Set(["r2", "k2"])); + const merged = res.state.blocks.find((b) => b.active && b.blockId !== "b2"); + assert.ok(merged, "a merged block must exist"); + assert.equal(merged.tier, 2); + assert.deepEqual(new Set(merged.effectiveMessageIds), new Set(["u0", "a0", "u1", "a1"])); + assert.deepEqual([...merged.directBlockIds].sort(), ["b1", "b3"]); + assert.ok( + res.result.warnings.some( + (w) => + w.includes("Inherited a pre-existing turn-integrity conflict") && + w.includes("m00007"), + ), + `inheritance warning must name the stranded partner (l2): ${JSON.stringify(res.result.warnings)}`, + ); + }); + +}); diff --git a/package-lock.json b/package-lock.json index 9cb99d16c3..0d5c56cca3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -50,7 +50,7 @@ }, "kernel": { "name": "acp-kernel", - "version": "0.0.182", + "version": "0.0.183", "dev": true, "license": "MIT", "devDependencies": { diff --git a/tests/msgid-lane-flow.test.ts b/tests/msgid-lane-flow.test.ts index 8fed5c7817..5042d009b2 100644 --- a/tests/msgid-lane-flow.test.ts +++ b/tests/msgid-lane-flow.test.ts @@ -106,9 +106,18 @@ async function harness(pin: boolean): Promise { // windows-latest runners (the row read saw the pre-turn snapshot — // PR #5644 win22-s2 red). Await the store's landing verdict instead; // a false verdict means a save genuinely failed — fail loud there, - // not on the missing row below. + // not on the missing row below. afterNextFlush resolves on the FIRST + // drain event, and under load a stale mid-turn write can still be in + // flight when the fresher one gets armed (persist.ts debounce overlap) + // — so one verdict can land on the pre-turn snapshot while the full + // save sits queued behind it (PR #5791 ubuntu-22 shard red). Every + // save for this turn was armed before the ack left, and nothing + // re-arms afterwards, so a second drain settles the remainder: an + // immediate no-op in the common single-write case. const ok = await store.afterNextFlush("conv-msgid-1"); assert.ok(ok, "debounced row save landed on disk before the row read"); + const settled = await store.afterNextFlush("conv-msgid-1"); + assert.ok(settled, "debounced row save landed on disk before the row read"); }; return { turn, forwarded, rows, settle, close: async () => { await new Promise((resolve) => proxy.close(() => resolve())); store.closeQuietly(); await new Promise((resolve) => upstream.close(() => resolve())); } }; }