From 933bf2a1d9b0bc6824abd6e03cbddd6bba037916 Mon Sep 17 00:00:00 2001 From: Aaron Erickson Date: Sun, 2 Aug 2026 08:28:23 -0700 Subject: [PATCH 1/5] fix(onboard): preserve shared-state commit authority Accept only the exact historical schema-v1 manifest for legacy null-identity recovery. Fence Docker rollback with the existing host-visible durable receipt probe. Signed-off-by: Aaron Erickson --- src/lib/onboard/managed-bootstrap/README.md | 7 + .../docker-shared-state.test.ts | 152 ++++++++++++++++++ .../managed-bootstrap/docker-shared-state.ts | 27 ++-- ...d-startup-shared-state-transaction.test.ts | 89 +++++++++- .../shared-state-transaction.ts | 49 ++++-- 5 files changed, 296 insertions(+), 28 deletions(-) create mode 100644 src/lib/onboard/managed-bootstrap/docker-shared-state.test.ts diff --git a/src/lib/onboard/managed-bootstrap/README.md b/src/lib/onboard/managed-bootstrap/README.md index dec1ec6010e..e63b1d9e2c6 100644 --- a/src/lib/onboard/managed-bootstrap/README.md +++ b/src/lib/onboard/managed-bootstrap/README.md @@ -54,6 +54,13 @@ commit atomically moves its pending manifest and backups into a durable receipt namespace, compacts that state to an exact commit receipt, and rejects rollback after a restart. The provider may retire that receipt only after it proves the external rollback backup is gone, leaving the next bootstrap attempt unblocked. +The parser accepts the exact canonical schema-v1 manifest written before +`bootstrapIdentity` was added only for the legacy null-identity path. It rejects +additional fields, missing historical fields, and legacy state presented as +identity-bound authority. Before rollback, the Docker adapter stops the +replacement and copies its writable-layer commit receipt to a protected host +path for verification. The immutable helper cannot obtain that receipt through +`--volumes-from`, which exposes volumes but not the replacement writable layer. At managed create-lifecycle startup, the driver-neutral coordinator asks the selected provider to reconcile every unfinished record before a new sandbox create begins. The Docker provider then resumes the durable phase monotonically: diff --git a/src/lib/onboard/managed-bootstrap/docker-shared-state.test.ts b/src/lib/onboard/managed-bootstrap/docker-shared-state.test.ts new file mode 100644 index 00000000000..4f043aaeb71 --- /dev/null +++ b/src/lib/onboard/managed-bootstrap/docker-shared-state.test.ts @@ -0,0 +1,152 @@ +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +import fs from "node:fs"; +import path from "node:path"; + +import { afterEach, describe, expect, it, vi } from "vitest"; + +import type { DockerGpuPatchDeps } from "../docker-gpu-patch-types"; +import { + MANAGED_STARTUP_SHARED_COMMIT_RECEIPT_DIRECTORY, + MANAGED_STARTUP_SHARED_ROLLBACK_RECEIPT_DIRECTORY, + MANAGED_STARTUP_SHARED_TRANSACTION_DIRECTORY, +} from "../managed-startup/shared-state-transaction"; +import { + type DockerManagedBootstrapSharedStateTransaction, + finalizeDockerManagedStartupSharedState, +} from "./docker-shared-state"; + +const CONTAINER_ID = "c".repeat(64); +const TRANSACTION: DockerManagedBootstrapSharedStateTransaction = { + agent: "openclaw", + bootstrapIdentity: "b".repeat(64), + containerId: CONTAINER_ID, + image: `sha256:${"a".repeat(64)}`, + profileFingerprint: "d".repeat(64), +}; + +interface SharedStateFixture { + readonly commands: readonly (readonly string[])[]; + readonly deps: DockerGpuPatchDeps; + readonly events: readonly string[]; + readonly state: () => "committed" | "none" | "pending"; +} + +const copiedReceiptPaths: string[] = []; + +afterEach(() => { + vi.restoreAllMocks(); + for (const receiptPath of copiedReceiptPaths.splice(0)) { + fs.rmSync(path.dirname(receiptPath), { force: true, recursive: true }); + } +}); + +function fixture(initialState: "committed" | "none" | "pending"): SharedStateFixture { + let state = initialState; + const commands: string[][] = []; + const events: string[] = []; + const dockerRun = vi.fn((args: readonly string[]) => { + commands.push([...args]); + if (args[0] === "cp") { + const source = String(args[2] ?? ""); + const destination = String(args[3] ?? ""); + const sourcePath = source.slice(`${CONTAINER_ID}:`.length); + const present = + (sourcePath === MANAGED_STARTUP_SHARED_COMMIT_RECEIPT_DIRECTORY && state === "committed") || + (sourcePath === MANAGED_STARTUP_SHARED_TRANSACTION_DIRECTORY && state === "pending"); + events.push(`copy:${path.basename(sourcePath)}:${present ? "present" : "absent"}`); + if (!present) { + return { + status: 1, + stderr: `Error response from daemon: Could not find the file ${sourcePath} in container ${CONTAINER_ID}`, + }; + } + fs.mkdirSync(destination, { recursive: true }); + copiedReceiptPaths.push(destination); + return { status: 0 }; + } + if (args[0] === "run" && args.includes("--shared-state-transaction-status")) { + events.push(`status:${state}`); + return { status: 0, stdout: `${state}\n` }; + } + if (args[0] === "run" && args.includes("--rollback-shared-state-transaction")) { + events.push("rollback"); + state = "none"; + return { status: 0 }; + } + throw new Error(`Unexpected Docker command: ${args.join(" ")}`); + }); + return { + commands, + deps: { + dockerRm: vi.fn(() => ({ status: 0 })), + dockerRun, + dockerStop: vi.fn(() => { + events.push("stop"); + return { status: 0 }; + }), + }, + events, + state: () => state, + }; +} + +describe("Docker managed-bootstrap shared-state rollback authority", () => { + it("copies and verifies writable-layer commit authority before rollback", () => { + const fake = fixture("committed"); + + expect(() => + finalizeDockerManagedStartupSharedState( + { transaction: TRANSACTION, supervisorReady: false }, + fake.deps, + ), + ).toThrow(/durably committed and cannot be rolled back/u); + + expect(fake.events).toEqual([ + "stop", + `copy:${path.basename(MANAGED_STARTUP_SHARED_COMMIT_RECEIPT_DIRECTORY)}:present`, + "status:committed", + ]); + const statusCommand = fake.commands.find((args) => + args.includes("--shared-state-transaction-status"), + ); + expect(statusCommand).toContainEqual( + expect.stringMatching( + new RegExp( + `^type=bind,src=.+,dst=${MANAGED_STARTUP_SHARED_COMMIT_RECEIPT_DIRECTORY},readonly$`, + "u", + ), + ), + ); + expect(fake.commands.some((args) => args.includes("--rollback-shared-state-transaction"))).toBe( + false, + ); + }); + + it("proves pending authority after quiescence before starting the rollback helper", () => { + const fake = fixture("pending"); + + expect( + finalizeDockerManagedStartupSharedState( + { transaction: TRANSACTION, supervisorReady: false }, + fake.deps, + ), + ).toEqual({ supervisorReady: false, failure: null }); + + expect(fake.events[0]).toBe("stop"); + expect(fake.events.indexOf("status:pending")).toBeLessThan(fake.events.indexOf("rollback")); + expect(fake.state()).toBe("none"); + const rollbackCommand = fake.commands.find((args) => + args.includes("--rollback-shared-state-transaction"), + ); + expect(rollbackCommand).toContainEqual( + expect.stringMatching( + new RegExp( + `^type=bind,src=.+,dst=${MANAGED_STARTUP_SHARED_ROLLBACK_RECEIPT_DIRECTORY},readonly$`, + "u", + ), + ), + ); + }); +}); diff --git a/src/lib/onboard/managed-bootstrap/docker-shared-state.ts b/src/lib/onboard/managed-bootstrap/docker-shared-state.ts index e86214f31b2..39a1d1d0265 100644 --- a/src/lib/onboard/managed-bootstrap/docker-shared-state.ts +++ b/src/lib/onboard/managed-bootstrap/docker-shared-state.ts @@ -465,10 +465,21 @@ function copyManagedStartupReceipt( function rollbackManagedStartupSharedState( transaction: DockerManagedBootstrapSharedStateTransaction, - receiptPath: string, deps: DockerGpuPatchDeps, -): void { + preservedReceiptPath?: string, +): boolean { const dockerRun = deps.dockerRun ?? defaultDockerRun; + const status = probeDockerManagedStartupSharedState( + { transaction, profileFingerprint: transaction.profileFingerprint }, + deps, + ); + if (status === "committed") { + throw new Error("Managed-startup shared state is durably committed and cannot be rolled back."); + } + const receiptPath = + preservedReceiptPath ?? + (status === "pending" ? copyManagedStartupReceipt(transaction, deps) : null); + if (!receiptPath) return false; let restored = false; try { const helper = dockerRun( @@ -517,6 +528,7 @@ function rollbackManagedStartupSharedState( cleanupReceiptBestEffort(receiptPath); } } + return true; } function removeFailedUnbackedContainer( @@ -605,7 +617,7 @@ export function finalizeDockerManagedStartupSharedState( `OpenShell supervisor reconnected, but managed shared-state logical commit validation failed: ${commitFailure.message}`, ); quiesceManagedStartupContainer(transaction, deps); - rollbackManagedStartupSharedState(transaction, receiptPath, deps); + rollbackManagedStartupSharedState(transaction, deps, receiptPath); if (!input.patchResult && !input.retainContainerAfterRollback) { removeFailedUnbackedContainer(transaction, deps); } @@ -613,14 +625,7 @@ export function finalizeDockerManagedStartupSharedState( } quiesceManagedStartupContainer(transaction, deps); - const receiptPath = copyManagedStartupReceipt(transaction, deps, true); - if (!receiptPath) { - if (!input.patchResult && !input.retainContainerAfterRollback) { - removeFailedUnbackedContainer(transaction, deps); - } - return { supervisorReady: false, failure: null }; - } - rollbackManagedStartupSharedState(transaction, receiptPath, deps); + rollbackManagedStartupSharedState(transaction, deps); if (!input.patchResult && !input.retainContainerAfterRollback) { removeFailedUnbackedContainer(transaction, deps); } diff --git a/src/lib/onboard/managed-startup-shared-state-transaction.test.ts b/src/lib/onboard/managed-startup-shared-state-transaction.test.ts index b913d21983e..8904be2198e 100644 --- a/src/lib/onboard/managed-startup-shared-state-transaction.test.ts +++ b/src/lib/onboard/managed-startup-shared-state-transaction.test.ts @@ -78,6 +78,20 @@ describe("managed startup shared-state transaction", () => { ); } + function rewriteManifest( + rewrite: (manifest: Record) => Record = (manifest) => + manifest, + ): void { + const manifestFile = path.join(transactionDirectory, "manifest.json"); + const manifest = JSON.parse(fs.readFileSync(manifestFile, "utf8")) as Record; + expect(manifest.bootstrapIdentity).toBeNull(); + delete manifest.bootstrapIdentity; + const rewritten = rewrite(manifest); + fs.chmodSync(manifestFile, 0o600); + fs.writeFileSync(manifestFile, `${JSON.stringify(rewritten, null, 2)}\n`); + fs.chmodSync(manifestFile, 0o400); + } + it.each([ "openclaw", "hermes", @@ -257,6 +271,70 @@ describe("managed startup shared-state transaction", () => { expect(commitManagedStartupSharedStateTransaction("openclaw", options)).toBe(false); }); + it.each([ + ["commits", "commit"], + ["rolls back", "rollback"], + ] as const)("%s an exact historical schema-v1 manifest without bootstrap identity", (_description, action) => { + const root = agentRoot("openclaw"); + fs.mkdirSync(root); + const config = path.join(root, "openclaw.json"); + fs.writeFileSync(config, "before\n"); + beginManagedStartupSharedStateTransaction(managedStartupE2eProfile("openclaw"), options); + rewriteManifest(); + fs.writeFileSync(config, "after\n"); + + const result = + action === "commit" + ? commitManagedStartupSharedStateTransaction("openclaw", options) + : rollbackManagedStartupSharedStateTransaction("openclaw", options); + + expect(result).toBe(true); + expect(fs.readFileSync(config, "utf8")).toBe(action === "commit" ? "after\n" : "before\n"); + expect(fs.existsSync(transactionDirectory)).toBe(false); + expect(fs.existsSync(commitReceiptDirectory())).toBe(false); + }); + + it.each([ + ["an extra field", (manifest: Record) => ({ ...manifest, extra: true })], + [ + "a missing historical field", + (manifest: Record) => { + delete manifest.directories; + return manifest; + }, + ], + ] as const)("rejects a schema-v1 legacy manifest with %s", (_case, rewrite) => { + const root = agentRoot("openclaw"); + fs.mkdirSync(root); + fs.writeFileSync(path.join(root, "openclaw.json"), "before\n"); + beginManagedStartupSharedStateTransaction(managedStartupE2eProfile("openclaw"), options); + rewriteManifest(rewrite); + + expect(() => commitManagedStartupSharedStateTransaction("openclaw", options)).toThrow( + /unexpected fields/u, + ); + expect(fs.existsSync(transactionDirectory)).toBe(true); + }); + + it("does not treat a legacy manifest as authority for an identity-bound bootstrap", () => { + const root = agentRoot("openclaw"); + fs.mkdirSync(root); + fs.writeFileSync(path.join(root, "openclaw.json"), "before\n"); + const boundOptions = { ...options, bootstrapIdentity: "b".repeat(64) }; + beginManagedStartupSharedStateTransaction(managedStartupE2eProfile("openclaw"), boundOptions); + const manifestFile = path.join(transactionDirectory, "manifest.json"); + const manifest = JSON.parse(fs.readFileSync(manifestFile, "utf8")) as Record; + delete manifest.bootstrapIdentity; + fs.chmodSync(manifestFile, 0o600); + fs.writeFileSync(manifestFile, `${JSON.stringify(manifest, null, 2)}\n`); + fs.chmodSync(manifestFile, 0o400); + + expect(() => rollbackManagedStartupSharedStateTransaction("openclaw", boundOptions)).toThrow( + /different bootstrap attempt/u, + ); + expect(fs.existsSync(transactionDirectory)).toBe(true); + }); + it("fsyncs every transaction namespace before exposing a pending receipt", () => { const root = agentRoot("openclaw"); fs.mkdirSync(root); @@ -393,10 +471,13 @@ describe("managed startup shared-state transaction", () => { throw new Error("injected post-rename cleanup interruption"); })() : originalRmSync(target, removeOptions)) as typeof fs.rmSync); - expect(() => commitManagedStartupSharedStateTransaction("openclaw", boundOptions)).toThrow( - /injected post-rename cleanup interruption/u, - ); - rm.mockRestore(); + try { + expect(() => commitManagedStartupSharedStateTransaction("openclaw", boundOptions)).toThrow( + /injected post-rename cleanup interruption/u, + ); + } finally { + rm.mockRestore(); + } expect(fs.existsSync(transactionDirectory)).toBe(false); const committedDirectory = commitReceiptDirectory(); diff --git a/src/lib/onboard/managed-startup/shared-state-transaction.ts b/src/lib/onboard/managed-startup/shared-state-transaction.ts index 68ac40bd7e6..7fcb5f38086 100644 --- a/src/lib/onboard/managed-startup/shared-state-transaction.ts +++ b/src/lib/onboard/managed-startup/shared-state-transaction.ts @@ -538,6 +538,20 @@ function canonicalManifest(manifest: TransactionManifest): string { return `${JSON.stringify(manifest, null, 2)}\n`; } +function canonicalLegacyManifest(manifest: TransactionManifest): string { + return `${JSON.stringify( + { + schemaVersion: manifest.schemaVersion, + agent: manifest.agent, + profileFingerprint: manifest.profileFingerprint, + files: manifest.files, + directories: manifest.directories, + }, + null, + 2, + )}\n`; +} + function canonicalCommitReceipt(receipt: CommitReceipt): string { return `${JSON.stringify(receipt, null, 2)}\n`; } @@ -597,23 +611,29 @@ function parseManifest(text: string): TransactionManifest { fail("transaction manifest must be an object"); } const record = parsed as Record; - requireExactKeys(record, [ - "agent", - "bootstrapIdentity", - "directories", - "files", - "profileFingerprint", - "schemaVersion", - ]); + const hasBootstrapIdentity = Object.hasOwn(record, "bootstrapIdentity"); + requireExactKeys( + record, + hasBootstrapIdentity + ? [ + "agent", + "bootstrapIdentity", + "directories", + "files", + "profileFingerprint", + "schemaVersion", + ] + : ["agent", "directories", "files", "profileFingerprint", "schemaVersion"], + ); + const bootstrapIdentity = hasBootstrapIdentity ? record.bootstrapIdentity : null; if ( record.schemaVersion !== TRANSACTION_SCHEMA_VERSION || !["openclaw", "hermes", "langchain-deepagents-code"].includes(String(record.agent)) || typeof record.profileFingerprint !== "string" || !/^[a-f0-9]{64}$/u.test(record.profileFingerprint) || !( - record.bootstrapIdentity === null || - (typeof record.bootstrapIdentity === "string" && - /^[a-f0-9]{64}$/u.test(record.bootstrapIdentity)) + bootstrapIdentity === null || + (typeof bootstrapIdentity === "string" && /^[a-f0-9]{64}$/u.test(bootstrapIdentity)) ) || !Array.isArray(record.files) || !Array.isArray(record.directories) || @@ -709,11 +729,14 @@ function parseManifest(text: string): TransactionManifest { schemaVersion: TRANSACTION_SCHEMA_VERSION, agent: record.agent as ManagedStartupAgent, profileFingerprint: record.profileFingerprint, - bootstrapIdentity: record.bootstrapIdentity as string | null, + bootstrapIdentity, files, directories, }; - if (canonicalManifest(manifest) !== text) { + const canonical = hasBootstrapIdentity + ? canonicalManifest(manifest) + : canonicalLegacyManifest(manifest); + if (canonical !== text) { fail("transaction manifest is not canonical"); } return manifest; From 1fef6bde6aea742e7f54d0589692e841186ce1a4 Mon Sep 17 00:00:00 2001 From: Aaron Erickson Date: Sun, 2 Aug 2026 08:48:59 -0700 Subject: [PATCH 2/5] test(onboard): keep shared-state fixture linear Signed-off-by: Aaron Erickson --- .../docker-shared-state.test.ts | 66 +++++++++++-------- 1 file changed, 40 insertions(+), 26 deletions(-) diff --git a/src/lib/onboard/managed-bootstrap/docker-shared-state.test.ts b/src/lib/onboard/managed-bootstrap/docker-shared-state.test.ts index 4f043aaeb71..c87ecfe086f 100644 --- a/src/lib/onboard/managed-bootstrap/docker-shared-state.test.ts +++ b/src/lib/onboard/managed-bootstrap/docker-shared-state.test.ts @@ -46,36 +46,50 @@ function fixture(initialState: "committed" | "none" | "pending"): SharedStateFix let state = initialState; const commands: string[][] = []; const events: string[] = []; + const copyPresentReceipt = (destination: string) => { + fs.mkdirSync(destination, { recursive: true }); + copiedReceiptPaths.push(destination); + return { status: 0 }; + }; + const copyMissingReceipt = (sourcePath: string) => ({ + status: 1, + stderr: `Error response from daemon: Could not find the file ${sourcePath} in container ${CONTAINER_ID}`, + }); const dockerRun = vi.fn((args: readonly string[]) => { commands.push([...args]); - if (args[0] === "cp") { - const source = String(args[2] ?? ""); - const destination = String(args[3] ?? ""); - const sourcePath = source.slice(`${CONTAINER_ID}:`.length); - const present = - (sourcePath === MANAGED_STARTUP_SHARED_COMMIT_RECEIPT_DIRECTORY && state === "committed") || - (sourcePath === MANAGED_STARTUP_SHARED_TRANSACTION_DIRECTORY && state === "pending"); - events.push(`copy:${path.basename(sourcePath)}:${present ? "present" : "absent"}`); - if (!present) { - return { - status: 1, - stderr: `Error response from daemon: Could not find the file ${sourcePath} in container ${CONTAINER_ID}`, - }; + switch (args[0]) { + case "cp": { + const source = String(args[2] ?? ""); + const destination = String(args[3] ?? ""); + const sourcePath = source.slice(`${CONTAINER_ID}:`.length); + const present = + (sourcePath === MANAGED_STARTUP_SHARED_COMMIT_RECEIPT_DIRECTORY && + state === "committed") || + (sourcePath === MANAGED_STARTUP_SHARED_TRANSACTION_DIRECTORY && state === "pending"); + events.push(`copy:${path.basename(sourcePath)}:${present ? "present" : "absent"}`); + return present ? copyPresentReceipt(destination) : copyMissingReceipt(sourcePath); } - fs.mkdirSync(destination, { recursive: true }); - copiedReceiptPaths.push(destination); - return { status: 0 }; - } - if (args[0] === "run" && args.includes("--shared-state-transaction-status")) { - events.push(`status:${state}`); - return { status: 0, stdout: `${state}\n` }; - } - if (args[0] === "run" && args.includes("--rollback-shared-state-transaction")) { - events.push("rollback"); - state = "none"; - return { status: 0 }; + case "run": { + const action = args.includes("--shared-state-transaction-status") + ? "status" + : args.includes("--rollback-shared-state-transaction") + ? "rollback" + : "unexpected"; + switch (action) { + case "status": + events.push(`status:${state}`); + return { status: 0, stdout: `${state}\n` }; + case "rollback": + events.push("rollback"); + state = "none"; + return { status: 0 }; + default: + throw new Error(`Unexpected Docker command: ${args.join(" ")}`); + } + } + default: + throw new Error(`Unexpected Docker command: ${args.join(" ")}`); } - throw new Error(`Unexpected Docker command: ${args.join(" ")}`); }); return { commands, From f2ac7866573e4d199915b92ddb917fffc1bb110b Mon Sep 17 00:00:00 2001 From: Aaron Erickson Date: Sun, 2 Aug 2026 09:52:27 -0700 Subject: [PATCH 3/5] test(onboard): cover absent shared-state receipts Signed-off-by: Aaron Erickson --- .../docker-shared-state.test.ts | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/lib/onboard/managed-bootstrap/docker-shared-state.test.ts b/src/lib/onboard/managed-bootstrap/docker-shared-state.test.ts index c87ecfe086f..dc8792d04aa 100644 --- a/src/lib/onboard/managed-bootstrap/docker-shared-state.test.ts +++ b/src/lib/onboard/managed-bootstrap/docker-shared-state.test.ts @@ -163,4 +163,27 @@ describe("Docker managed-bootstrap shared-state rollback authority", () => { ), ); }); + + it("removes the exact failed container without rollback when both receipts are absent", () => { + const fake = fixture("none"); + + expect( + finalizeDockerManagedStartupSharedState( + { transaction: TRANSACTION, supervisorReady: false }, + fake.deps, + ), + ).toEqual({ supervisorReady: false, failure: null }); + + expect(fake.events).toEqual([ + "stop", + `copy:${path.basename(MANAGED_STARTUP_SHARED_COMMIT_RECEIPT_DIRECTORY)}:absent`, + `copy:${path.basename(MANAGED_STARTUP_SHARED_TRANSACTION_DIRECTORY)}:absent`, + ]); + expect(fake.commands.some((args) => args.includes("--rollback-shared-state-transaction"))).toBe( + false, + ); + expect(fake.deps.dockerRm).toHaveBeenCalledTimes(1); + expect(fake.deps.dockerRm).toHaveBeenCalledWith(CONTAINER_ID, expect.any(Object)); + expect(fake.state()).toBe("none"); + }); }); From 088ede4ad10e80197d8be4e42c050b6cb41e098d Mon Sep 17 00:00:00 2001 From: Aaron Erickson Date: Sun, 2 Aug 2026 11:04:13 -0700 Subject: [PATCH 4/5] test(onboard): cover shared-state commit rollback fence Signed-off-by: Aaron Erickson --- .../docker-shared-state.test.ts | 90 ++++++++++++++++++- 1 file changed, 89 insertions(+), 1 deletion(-) diff --git a/src/lib/onboard/managed-bootstrap/docker-shared-state.test.ts b/src/lib/onboard/managed-bootstrap/docker-shared-state.test.ts index dc8792d04aa..053ffec8725 100644 --- a/src/lib/onboard/managed-bootstrap/docker-shared-state.test.ts +++ b/src/lib/onboard/managed-bootstrap/docker-shared-state.test.ts @@ -33,6 +33,11 @@ interface SharedStateFixture { readonly state: () => "committed" | "none" | "pending"; } +interface SharedStateFixtureOptions { + readonly stateAfterCommitFailure?: "committed" | "none" | "pending"; + readonly stateAfterStop?: "committed" | "none" | "pending"; +} + const copiedReceiptPaths: string[] = []; afterEach(() => { @@ -42,7 +47,10 @@ afterEach(() => { } }); -function fixture(initialState: "committed" | "none" | "pending"): SharedStateFixture { +function fixture( + initialState: "committed" | "none" | "pending", + options: SharedStateFixtureOptions = {}, +): SharedStateFixture { let state = initialState; const commands: string[][] = []; const events: string[] = []; @@ -87,6 +95,13 @@ function fixture(initialState: "committed" | "none" | "pending"): SharedStateFix throw new Error(`Unexpected Docker command: ${args.join(" ")}`); } } + case "exec": + if (!args.includes("--commit-shared-state-transaction")) { + throw new Error("Unexpected Docker commit command"); + } + events.push("commit:failed"); + state = options.stateAfterCommitFailure ?? state; + return { status: 1, stderr: "commit helper failed" }; default: throw new Error(`Unexpected Docker command: ${args.join(" ")}`); } @@ -98,6 +113,7 @@ function fixture(initialState: "committed" | "none" | "pending"): SharedStateFix dockerRun, dockerStop: vi.fn(() => { events.push("stop"); + state = options.stateAfterStop ?? state; return { status: 0 }; }), }, @@ -186,4 +202,76 @@ describe("Docker managed-bootstrap shared-state rollback authority", () => { expect(fake.deps.dockerRm).toHaveBeenCalledWith(CONTAINER_ID, expect.any(Object)); expect(fake.state()).toBe("none"); }); + + it("reuses one preserved pending receipt when commit validation fails", () => { + const fake = fixture("pending", { stateAfterCommitFailure: "none" }); + + const outcome = finalizeDockerManagedStartupSharedState( + { + retainContainerAfterRollback: true, + transaction: TRANSACTION, + supervisorReady: true, + }, + fake.deps, + ); + + expect(outcome.supervisorReady).toBe(false); + expect(outcome.failure).toEqual( + expect.objectContaining({ + message: expect.stringContaining("commit helper failed"), + }), + ); + const pendingSource = CONTAINER_ID + ":" + MANAGED_STARTUP_SHARED_TRANSACTION_DIRECTORY; + const pendingCopies = fake.commands.filter( + (args) => args[0] === "cp" && args[2] === pendingSource, + ); + expect( + fake.events.filter( + (event) => + event === + "copy:" + path.basename(MANAGED_STARTUP_SHARED_TRANSACTION_DIRECTORY) + ":present", + ), + ).toHaveLength(1); + const preservedReceiptPath = String(pendingCopies[0]?.[3] ?? ""); + const rollbackCommand = fake.commands.find((args) => + args.includes("--rollback-shared-state-transaction"), + ); + expect(rollbackCommand).toContain( + "type=bind,src=" + + preservedReceiptPath + + ",dst=" + + MANAGED_STARTUP_SHARED_ROLLBACK_RECEIPT_DIRECTORY + + ",readonly", + ); + expect(fake.deps.dockerRm).not.toHaveBeenCalled(); + }); + + it("rejects rollback when a failed commit becomes durable during quiescence", () => { + const fake = fixture("pending", { + stateAfterCommitFailure: "none", + stateAfterStop: "committed", + }); + + expect(() => + finalizeDockerManagedStartupSharedState( + { transaction: TRANSACTION, supervisorReady: true }, + fake.deps, + ), + ).toThrow(/durably committed and cannot be rolled back/u); + + expect( + fake.events.filter( + (event) => + event === + "copy:" + path.basename(MANAGED_STARTUP_SHARED_TRANSACTION_DIRECTORY) + ":present", + ), + ).toHaveLength(1); + expect(fake.events).toContain("commit:failed"); + expect(fake.events).toContain("status:committed"); + expect(fake.events).not.toContain("rollback"); + expect(fake.commands.some((args) => args.includes("--rollback-shared-state-transaction"))).toBe( + false, + ); + expect(fake.deps.dockerRm).not.toHaveBeenCalled(); + }); }); From 49694623d2a4e45b4f215dd0ed0821517536c4f9 Mon Sep 17 00:00:00 2001 From: Aaron Erickson Date: Sun, 2 Aug 2026 11:12:18 -0700 Subject: [PATCH 5/5] test(onboard): keep commit fixture linear Signed-off-by: Aaron Erickson --- .../managed-bootstrap/docker-shared-state.test.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/lib/onboard/managed-bootstrap/docker-shared-state.test.ts b/src/lib/onboard/managed-bootstrap/docker-shared-state.test.ts index 053ffec8725..34eca762bcb 100644 --- a/src/lib/onboard/managed-bootstrap/docker-shared-state.test.ts +++ b/src/lib/onboard/managed-bootstrap/docker-shared-state.test.ts @@ -96,12 +96,14 @@ function fixture( } } case "exec": - if (!args.includes("--commit-shared-state-transaction")) { - throw new Error("Unexpected Docker commit command"); + switch (args.includes("--commit-shared-state-transaction")) { + case true: + events.push("commit:failed"); + state = options.stateAfterCommitFailure ?? state; + return { status: 1, stderr: "commit helper failed" }; + default: + throw new Error("Unexpected Docker commit command"); } - events.push("commit:failed"); - state = options.stateAfterCommitFailure ?? state; - return { status: 1, stderr: "commit helper failed" }; default: throw new Error(`Unexpected Docker command: ${args.join(" ")}`); }