Skip to content

Commit a744280

Browse files
committed
🪲 Answer from a finished holder's publication when elected late
A recovery attempt waiting behind a live ticket polls the published tree only between election passes, while a holder marks its ticket done only after publishing. When the publication and the release both landed in the window between one poll and the next election pass, the waiter was elected and entered the recovery cleanup a second time — harmless to durable state, because the in-lock re-check removed nothing, but a violation of the contract that an attempt which did not do the recovery answers from what the holder published, and the intermittent failure AM19 caught under Bun (#664). An elected attempt now checks the published tree before entering the cleanup: ticket order guarantees a finished holder's publication is visible by election time, so being elected behind one means the answer is already on disk. AM20 produces the exact window deterministically through a new election-pass observer and fails without the fix. Closes #664 Claude-Session: https://claude.ai/code/session_015PmPc8yC6AJKQiu9F9d2ru
1 parent 879c4ff commit a744280

2 files changed

Lines changed: 67 additions & 1 deletion

File tree

‎packages/acp/src/adapter-snapshots.ts‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,8 @@ function* install(
349349
export interface EmbeddedAdapterObservers {
350350
/** Runs after a recovery takes its lock and before it removes anything. */
351351
readonly beforeRecoveryCleanup?: () => Operation<void>;
352+
/** Runs before each pass of the recovery election. */
353+
readonly beforeRecoveryElection?: () => Operation<void>;
352354
}
353355

354356
/**
@@ -524,7 +526,9 @@ export function createEmbeddedAdapters(
524526
* resolved and might already be running.
525527
*
526528
* An attempt that does not hold the claim never removes anything. It waits
527-
* and answers from what the holder published.
529+
* and answers from what the holder published — including when the wait ends
530+
* with this attempt elected, because a holder finishes its ticket only after
531+
* publishing.
528532
*/
529533
function* recover(
530534
snapshot: EmbeddedAdapterSnapshot,
@@ -544,6 +548,10 @@ export function createEmbeddedAdapters(
544548
yield* ensure(() => releaseTicket(claims, mine));
545549

546550
for (let attempt = 0; attempt < RECOVERY_ATTEMPTS; attempt += 1) {
551+
const observe = observers.beforeRecoveryElection;
552+
if (observe !== undefined) {
553+
yield* observe();
554+
}
547555
const status = yield* electTicket(claims, mine);
548556
if (status === "unreadable") {
549557
throw new AdapterSnapshotError(
@@ -552,6 +560,14 @@ export function createEmbeddedAdapters(
552560
);
553561
}
554562
if (status === "held") {
563+
// A ticket is marked finished only after its holder's publication is
564+
// on disk, so an election won behind a finished holder can mean the
565+
// work is already done. Answer from it rather than entering a
566+
// cleanup with nothing left to recover.
567+
if (yield* published(snapshot)) {
568+
settled = true;
569+
return;
570+
}
555571
held = true;
556572
break;
557573
}

‎packages/acp/tests/adapter-materialization.test.ts‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,56 @@ describe("Tier AM — embedded adapter materialization", () => {
597597
expect(yield* exists(join(target, "node_modules", "stale.txt"))).toBe(false);
598598
});
599599

600+
it("AM20: an attempt elected behind a finished holder answers from its publication", function* () {
601+
const root = yield* useRoot();
602+
const target = join(root, digestOf("codex"));
603+
const claims = `${target}.claims`;
604+
605+
// A valid published tree, built elsewhere, ready to drop in.
606+
const donorRoot = yield* useRoot();
607+
const donor = createEmbeddedAdapters(donorRoot);
608+
yield* donor.materialize("codex");
609+
const donorTree = join(donorRoot, digestOf("codex"));
610+
611+
// Abandoned work at the target, so the attempt takes the recovery path.
612+
yield* ensureDir(join(target, "node_modules"));
613+
yield* writeTextFile(join(target, "node_modules", "stale.txt"), "half an install\n");
614+
615+
// A live ticket ahead, so the attempt has to wait at least one pass.
616+
yield* plantTicket(claims, 0, { pid: process.pid, host: hostname() });
617+
618+
// The window AM19 can only reach by scheduling luck, produced exactly: the
619+
// holder ahead publishes and finishes its ticket after this attempt last
620+
// read the target and before its next election pass. The next election
621+
// elects this attempt — and being elected must mean reading what the
622+
// finished holder published, never a second recovery over it.
623+
let cleanups = 0;
624+
let elections = 0;
625+
const adapters = createEmbeddedAdapters(root, {
626+
*beforeRecoveryCleanup(): Operation<void> {
627+
cleanups += 1;
628+
},
629+
*beforeRecoveryElection(): Operation<void> {
630+
elections += 1;
631+
if (elections === 2) {
632+
yield* rm(target, { recursive: true, force: true });
633+
yield* until(cp(donorTree, target, { recursive: true }));
634+
yield* until(writeFile(join(claims, "0", "done"), ""));
635+
}
636+
},
637+
});
638+
639+
const entry = adapters.executablePath("codex");
640+
yield* adapters.materialize("codex");
641+
642+
expect(elections).toBeGreaterThanOrEqual(2);
643+
expect(cleanups).toBe(0);
644+
expect(yield* exists(entry)).toBe(true);
645+
expect(yield* until(readFile(entry))).toEqual(
646+
yield* until(readFile(join(donorTree, "node_modules", CODEX_PACKAGE, "dist", "index.js"))),
647+
);
648+
});
649+
600650
it("AM9: edited adapter bytes are refused rather than executed", function* () {
601651
const root = yield* useRoot();
602652
const adapters = createEmbeddedAdapters(root);

0 commit comments

Comments
 (0)