From ccd4a63775a5cc88239b30750013b07f7e23bbde Mon Sep 17 00:00:00 2001 From: Julie Yaunches Date: Tue, 18 Aug 2026 17:49:15 -0400 Subject: [PATCH 1/6] test(rebuild): expose invalid OpenClaw config hash Signed-off-by: Julie Yaunches --- .../sandbox/rebuild-config-hash.test.ts | 27 +++++++++++++++++++ .../rebuild-post-restore-phase.test.ts | 23 ++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/src/lib/actions/sandbox/rebuild-config-hash.test.ts b/src/lib/actions/sandbox/rebuild-config-hash.test.ts index 6b9978c3b2c..162306d64b0 100644 --- a/src/lib/actions/sandbox/rebuild-config-hash.test.ts +++ b/src/lib/actions/sandbox/rebuild-config-hash.test.ts @@ -47,6 +47,33 @@ describe.skipIf(process.platform !== "linux")("OpenClaw rebuild config hash refr } }); + it("rejects a stale hash when the config directory is root-owned (#9530)", () => { + const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "nemoclaw-rebuild-root-hash-")); + const configDir = path.join(tmpDir, ".openclaw"); + const binDir = path.join(tmpDir, "bin"); + const configPath = path.join(configDir, "openclaw.json"); + const hashPath = path.join(configDir, ".config-hash"); + const statCommand = path.join(binDir, "stat"); + try { + fs.mkdirSync(configDir, { recursive: true }); + fs.mkdirSync(binDir, { recursive: true }); + fs.writeFileSync(configPath, '{"gateway":{"auth":{"token":"fresh"}}}\n'); + fs.writeFileSync(hashPath, `${"0".repeat(64)} openclaw.json\n`); + fs.writeFileSync(statCommand, "#!/bin/sh\nprintf '%s\\n' root\n"); + fs.chmodSync(statCommand, 0o755); + + const result = runRefresh(configDir, { + ...process.env, + PATH: `${binDir}:${process.env.PATH ?? ""}`, + }); + + expect(result.status).not.toBe(0); + expect(fs.readFileSync(hashPath, "utf-8")).toBe(`${"0".repeat(64)} openclaw.json\n`); + } finally { + fs.rmSync(tmpDir, { recursive: true, force: true }); + } + }); + it("refuses to refresh through a symlinked config file", () => { const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "nemoclaw-rebuild-hash-symlink-")); const configDir = path.join(tmpDir, ".openclaw"); diff --git a/src/lib/actions/sandbox/rebuild-post-restore-phase.test.ts b/src/lib/actions/sandbox/rebuild-post-restore-phase.test.ts index 33eb2a60368..10c89cf87ca 100644 --- a/src/lib/actions/sandbox/rebuild-post-restore-phase.test.ts +++ b/src/lib/actions/sandbox/rebuild-post-restore-phase.test.ts @@ -84,6 +84,7 @@ describe("rebuild post-restore phase", () => { vi.spyOn(registry, "getSandbox").mockImplementation( () => ({ agent: agentName === "openclaw" ? null : agentName }) as never, ); + vi.spyOn(registry, "getBaselineExclusions").mockReturnValue([]); vi.spyOn(registry, "updateSandbox").mockReturnValue(true); vi.spyOn(messagingHostForward, "ensureMessagingHostForwardAfterRebuild").mockReturnValue(true); }); @@ -123,6 +124,28 @@ describe("rebuild post-restore phase", () => { expect(order).toEqual(["doctor", "reconcile", "messaging", "config-hash"]); }); + it("fails when doctor returns 255 and the final OpenClaw config hash is unverified (#9530)", async () => { + vi.mocked(processRecovery.executeSandboxCommand).mockReturnValue({ + status: 255, + stdout: "", + stderr: "", + }); + vi.mocked( + rebuildConfigHash.refreshMutableOpenClawConfigHashAfterPostRestoreWrites, + ).mockReturnValue(false); + const args = input(); + + await runRebuildPostRestorePhase(args); + + expect(args.relockShieldsIfNeeded).toHaveBeenCalledWith(true); + expect(args.bail).toHaveBeenCalledWith( + "OpenClaw config integrity verification failed after rebuild.", + ); + expect(vi.mocked(console.log).mock.calls.flat().join("\n")).not.toContain( + "rebuilt successfully", + ); + }); + it("does not run OpenClaw session reconciliation for another agent (#7102)", async () => { agentName = "hermes"; const args = input(); From 4e60f17bb3c0fc105ce908ad87697872ed76d3d8 Mon Sep 17 00:00:00 2001 From: Julie Yaunches Date: Tue, 18 Aug 2026 17:56:55 -0400 Subject: [PATCH 2/6] fix(rebuild): verify final OpenClaw config hash Signed-off-by: Julie Yaunches --- .../sandbox/rebuild-config-hash-command.ts | 2 +- .../sandbox/rebuild-config-hash.test.ts | 38 +++++++++++++++++-- .../sandbox/rebuild-post-restore-phase.ts | 4 ++ 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/src/lib/actions/sandbox/rebuild-config-hash-command.ts b/src/lib/actions/sandbox/rebuild-config-hash-command.ts index 1490fb18a09..44e7c6312f8 100644 --- a/src/lib/actions/sandbox/rebuild-config-hash-command.ts +++ b/src/lib/actions/sandbox/rebuild-config-hash-command.ts @@ -15,9 +15,9 @@ export function buildRefreshMutableOpenClawConfigHashCommand( '[ ! -L "$config_file" ] || { echo "refusing symlinked OpenClaw config file: $config_file" >&2; exit 11; }', '[ ! -L "$hash_file" ] || { echo "refusing symlinked OpenClaw config hash: $hash_file" >&2; exit 12; }', 'owner="$(stat -c "%U" "$config_dir" 2>/dev/null || echo unknown)"', - '[ "$owner" != "root" ] || exit 0', '[ -f "$config_file" ] || exit 0', 'cd "$config_dir" || exit 13', + '[ "$owner" != "root" ] || { sha256sum -c .config-hash --status || { echo "root-owned OpenClaw config hash does not match openclaw.json" >&2; exit 15; }; exit 0; }', "sha256sum openclaw.json > .config-hash || exit 14", "chmod 660 .config-hash 2>/dev/null || true", ].join("; "); diff --git a/src/lib/actions/sandbox/rebuild-config-hash.test.ts b/src/lib/actions/sandbox/rebuild-config-hash.test.ts index 162306d64b0..0926e15f04b 100644 --- a/src/lib/actions/sandbox/rebuild-config-hash.test.ts +++ b/src/lib/actions/sandbox/rebuild-config-hash.test.ts @@ -26,6 +26,13 @@ function runRefresh( }); } +function installRootOwnerStat(binDir: string): void { + const statCommand = path.join(binDir, "stat"); + fs.mkdirSync(binDir, { recursive: true }); + fs.writeFileSync(statCommand, "#!/bin/sh\nprintf '%s\\n' root\n"); + fs.chmodSync(statCommand, 0o755); +} + describe.skipIf(process.platform !== "linux")("OpenClaw rebuild config hash refresh", () => { it("refreshes .config-hash for the current openclaw.json", () => { const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "nemoclaw-rebuild-hash-")); @@ -53,14 +60,11 @@ describe.skipIf(process.platform !== "linux")("OpenClaw rebuild config hash refr const binDir = path.join(tmpDir, "bin"); const configPath = path.join(configDir, "openclaw.json"); const hashPath = path.join(configDir, ".config-hash"); - const statCommand = path.join(binDir, "stat"); try { fs.mkdirSync(configDir, { recursive: true }); - fs.mkdirSync(binDir, { recursive: true }); fs.writeFileSync(configPath, '{"gateway":{"auth":{"token":"fresh"}}}\n'); fs.writeFileSync(hashPath, `${"0".repeat(64)} openclaw.json\n`); - fs.writeFileSync(statCommand, "#!/bin/sh\nprintf '%s\\n' root\n"); - fs.chmodSync(statCommand, 0o755); + installRootOwnerStat(binDir); const result = runRefresh(configDir, { ...process.env, @@ -74,6 +78,32 @@ describe.skipIf(process.platform !== "linux")("OpenClaw rebuild config hash refr } }); + it("accepts a matching hash when the config directory is root-owned (#9530)", () => { + const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "nemoclaw-rebuild-valid-root-hash-")); + const configDir = path.join(tmpDir, ".openclaw"); + const binDir = path.join(tmpDir, "bin"); + const configPath = path.join(configDir, "openclaw.json"); + const hashPath = path.join(configDir, ".config-hash"); + try { + fs.mkdirSync(configDir, { recursive: true }); + fs.writeFileSync(configPath, '{"gateway":{"auth":{"token":"fresh"}}}\n'); + const expectedHash = `${sha256Hex(configPath)} openclaw.json\n`; + fs.writeFileSync(hashPath, expectedHash); + installRootOwnerStat(binDir); + + const result = runRefresh(configDir, { + ...process.env, + PATH: `${binDir}:${process.env.PATH ?? ""}`, + }); + + expect(result.stderr).toBe(""); + expect(result.status).toBe(0); + expect(fs.readFileSync(hashPath, "utf-8")).toBe(expectedHash); + } finally { + fs.rmSync(tmpDir, { recursive: true, force: true }); + } + }); + it("refuses to refresh through a symlinked config file", () => { const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "nemoclaw-rebuild-hash-symlink-")); const configDir = path.join(tmpDir, ".openclaw"); diff --git a/src/lib/actions/sandbox/rebuild-post-restore-phase.ts b/src/lib/actions/sandbox/rebuild-post-restore-phase.ts index 74c14dbe47f..82d196f3437 100644 --- a/src/lib/actions/sandbox/rebuild-post-restore-phase.ts +++ b/src/lib/actions/sandbox/rebuild-post-restore-phase.ts @@ -465,6 +465,10 @@ export async function runRebuildPostRestorePhase( bail(`Rebuild completed with unverified live policy reconciliation for '${sandboxName}'.`); return; } + if (targetAgentName === "openclaw" && mutableConfigHashRefreshUnverified) { + bail("OpenClaw config integrity verification failed after rebuild."); + return; + } if ( targetAgentName === "hermes" && (hermesGatewayRestoreUnverified || mcpBridgeRestoreUnverified) From 7483bbad00080dafb10da61e15ecc5f2513cd55e Mon Sep 17 00:00:00 2001 From: Julie Yaunches Date: Tue, 18 Aug 2026 18:04:42 -0400 Subject: [PATCH 3/6] test(rebuild): assert config hash failure contract Signed-off-by: Julie Yaunches --- src/lib/actions/sandbox/rebuild-config-hash.test.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/lib/actions/sandbox/rebuild-config-hash.test.ts b/src/lib/actions/sandbox/rebuild-config-hash.test.ts index 0926e15f04b..3b6f369841e 100644 --- a/src/lib/actions/sandbox/rebuild-config-hash.test.ts +++ b/src/lib/actions/sandbox/rebuild-config-hash.test.ts @@ -71,7 +71,10 @@ describe.skipIf(process.platform !== "linux")("OpenClaw rebuild config hash refr PATH: `${binDir}:${process.env.PATH ?? ""}`, }); - expect(result.status).not.toBe(0); + expect(result.status).toBe(15); + expect(result.stderr).toBe( + "root-owned OpenClaw config hash does not match openclaw.json\n", + ); expect(fs.readFileSync(hashPath, "utf-8")).toBe(`${"0".repeat(64)} openclaw.json\n`); } finally { fs.rmSync(tmpDir, { recursive: true, force: true }); From e8bd1b2476ce1b3e67e8f0d68a8b2b8edd008cb2 Mon Sep 17 00:00:00 2001 From: Prekshi Vyas Date: Tue, 18 Aug 2026 15:31:21 -0700 Subject: [PATCH 4/6] test(rebuild): deduplicate root hash cases Signed-off-by: Prekshi Vyas --- .../sandbox/rebuild-config-hash.test.ts | 48 +++++++------------ 1 file changed, 17 insertions(+), 31 deletions(-) diff --git a/src/lib/actions/sandbox/rebuild-config-hash.test.ts b/src/lib/actions/sandbox/rebuild-config-hash.test.ts index 3b6f369841e..68712942c0a 100644 --- a/src/lib/actions/sandbox/rebuild-config-hash.test.ts +++ b/src/lib/actions/sandbox/rebuild-config-hash.test.ts @@ -54,7 +54,20 @@ describe.skipIf(process.platform !== "linux")("OpenClaw rebuild config hash refr } }); - it("rejects a stale hash when the config directory is root-owned (#9530)", () => { + it.each([ + { + title: "rejects a stale hash when the config directory is root-owned (#9530)", + initialHash: () => `${"0".repeat(64)} openclaw.json\n`, + expectedStatus: 15, + expectedStderr: "root-owned OpenClaw config hash does not match openclaw.json\n", + }, + { + title: "accepts a matching hash when the config directory is root-owned (#9530)", + initialHash: (configPath: string) => `${sha256Hex(configPath)} openclaw.json\n`, + expectedStatus: 0, + expectedStderr: "", + }, + ])("$title", ({ initialHash, expectedStatus, expectedStderr }) => { const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "nemoclaw-rebuild-root-hash-")); const configDir = path.join(tmpDir, ".openclaw"); const binDir = path.join(tmpDir, "bin"); @@ -63,34 +76,7 @@ describe.skipIf(process.platform !== "linux")("OpenClaw rebuild config hash refr try { fs.mkdirSync(configDir, { recursive: true }); fs.writeFileSync(configPath, '{"gateway":{"auth":{"token":"fresh"}}}\n'); - fs.writeFileSync(hashPath, `${"0".repeat(64)} openclaw.json\n`); - installRootOwnerStat(binDir); - - const result = runRefresh(configDir, { - ...process.env, - PATH: `${binDir}:${process.env.PATH ?? ""}`, - }); - - expect(result.status).toBe(15); - expect(result.stderr).toBe( - "root-owned OpenClaw config hash does not match openclaw.json\n", - ); - expect(fs.readFileSync(hashPath, "utf-8")).toBe(`${"0".repeat(64)} openclaw.json\n`); - } finally { - fs.rmSync(tmpDir, { recursive: true, force: true }); - } - }); - - it("accepts a matching hash when the config directory is root-owned (#9530)", () => { - const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "nemoclaw-rebuild-valid-root-hash-")); - const configDir = path.join(tmpDir, ".openclaw"); - const binDir = path.join(tmpDir, "bin"); - const configPath = path.join(configDir, "openclaw.json"); - const hashPath = path.join(configDir, ".config-hash"); - try { - fs.mkdirSync(configDir, { recursive: true }); - fs.writeFileSync(configPath, '{"gateway":{"auth":{"token":"fresh"}}}\n'); - const expectedHash = `${sha256Hex(configPath)} openclaw.json\n`; + const expectedHash = initialHash(configPath); fs.writeFileSync(hashPath, expectedHash); installRootOwnerStat(binDir); @@ -99,8 +85,8 @@ describe.skipIf(process.platform !== "linux")("OpenClaw rebuild config hash refr PATH: `${binDir}:${process.env.PATH ?? ""}`, }); - expect(result.stderr).toBe(""); - expect(result.status).toBe(0); + expect(result.status).toBe(expectedStatus); + expect(result.stderr).toBe(expectedStderr); expect(fs.readFileSync(hashPath, "utf-8")).toBe(expectedHash); } finally { fs.rmSync(tmpDir, { recursive: true, force: true }); From 326916f793d48411841d546564104717baa580a5 Mon Sep 17 00:00:00 2001 From: Prekshi Vyas Date: Tue, 18 Aug 2026 15:34:56 -0700 Subject: [PATCH 5/6] fix(rebuild): bind locked hash to config Signed-off-by: Prekshi Vyas --- src/lib/actions/sandbox/rebuild-config-hash-command.ts | 2 +- src/lib/actions/sandbox/rebuild-config-hash.test.ts | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/lib/actions/sandbox/rebuild-config-hash-command.ts b/src/lib/actions/sandbox/rebuild-config-hash-command.ts index 44e7c6312f8..f1d87ad6826 100644 --- a/src/lib/actions/sandbox/rebuild-config-hash-command.ts +++ b/src/lib/actions/sandbox/rebuild-config-hash-command.ts @@ -17,7 +17,7 @@ export function buildRefreshMutableOpenClawConfigHashCommand( 'owner="$(stat -c "%U" "$config_dir" 2>/dev/null || echo unknown)"', '[ -f "$config_file" ] || exit 0', 'cd "$config_dir" || exit 13', - '[ "$owner" != "root" ] || { sha256sum -c .config-hash --status || { echo "root-owned OpenClaw config hash does not match openclaw.json" >&2; exit 15; }; exit 0; }', + '[ "$owner" != "root" ] || { expected_hash="$(sha256sum openclaw.json 2>/dev/null)" && actual_hash="$(cat .config-hash 2>/dev/null)" && [ "$actual_hash" = "$expected_hash" ] || { echo "root-owned OpenClaw config hash does not match openclaw.json" >&2; exit 15; }; exit 0; }', "sha256sum openclaw.json > .config-hash || exit 14", "chmod 660 .config-hash 2>/dev/null || true", ].join("; "); diff --git a/src/lib/actions/sandbox/rebuild-config-hash.test.ts b/src/lib/actions/sandbox/rebuild-config-hash.test.ts index 68712942c0a..d3b3421e55d 100644 --- a/src/lib/actions/sandbox/rebuild-config-hash.test.ts +++ b/src/lib/actions/sandbox/rebuild-config-hash.test.ts @@ -67,6 +67,16 @@ describe.skipIf(process.platform !== "linux")("OpenClaw rebuild config hash refr expectedStatus: 0, expectedStderr: "", }, + { + title: "rejects a valid hash that names another file in a root-owned directory (#9530)", + initialHash: (configPath: string) => { + const decoyPath = path.join(path.dirname(configPath), "decoy.json"); + fs.writeFileSync(decoyPath, '{"decoy":true}\n'); + return `${sha256Hex(decoyPath)} decoy.json\n`; + }, + expectedStatus: 15, + expectedStderr: "root-owned OpenClaw config hash does not match openclaw.json\n", + }, ])("$title", ({ initialHash, expectedStatus, expectedStderr }) => { const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "nemoclaw-rebuild-root-hash-")); const configDir = path.join(tmpDir, ".openclaw"); From 012f2a7e38f3115ed5c9caf238492af775a43f0a Mon Sep 17 00:00:00 2001 From: Prekshi Vyas Date: Tue, 18 Aug 2026 19:56:03 -0700 Subject: [PATCH 6/6] test(rebuild): expect config hash rejection Signed-off-by: Prekshi Vyas --- .../sandbox/rebuild-flow-recovery.test.ts | 171 +++++++++--------- 1 file changed, 85 insertions(+), 86 deletions(-) diff --git a/src/lib/actions/sandbox/rebuild-flow-recovery.test.ts b/src/lib/actions/sandbox/rebuild-flow-recovery.test.ts index 7cb6c275441..2d3fb270307 100644 --- a/src/lib/actions/sandbox/rebuild-flow-recovery.test.ts +++ b/src/lib/actions/sandbox/rebuild-flow-recovery.test.ts @@ -18,7 +18,6 @@ import { describe("rebuildSandbox flow: recovery", () => { installRebuildFlowTestHooks(); - it("uses marked manifest provenance when the custom-image registry baseline is missing (#6108)", async () => { const customDockerfile = path.join(process.cwd(), "Dockerfile"); const recoveryManifest = { @@ -55,12 +54,6 @@ describe("rebuildSandbox flow: recovery", () => { ); }); - - - - - - it("keeps an explicit default choice made while the replacement was in flight (#7734)", async () => { let harness!: ReturnType; harness = createRebuildFlowHarness({ @@ -167,32 +160,34 @@ describe("rebuildSandbox flow: recovery", () => { return restarted; } - it.each( - POST_DELETE_PHASES, - )("keeps a registered replacement when a rebuild restarts from '%s' (#7734)", async (phase) => { - const restarted = restartRebuild(REPLACEMENT_PROBE, await interruptAfterCreate(phase)); + it.each(POST_DELETE_PHASES)( + "keeps a registered replacement when a rebuild restarts from '%s' (#7734)", + async (phase) => { + const restarted = restartRebuild(REPLACEMENT_PROBE, await interruptAfterCreate(phase)); - await restarted.rebuildSandbox("alpha", ["--yes"]); + await restarted.rebuildSandbox("alpha", ["--yes"]); - expectNoSandboxDelete(restarted.runOpenshellSpy); - expect(restarted.onboardSpy).not.toHaveBeenCalled(); - expect( - (restarted.session.checkpoint as { sandboxRecreate: unknown }).sandboxRecreate, - ).toBeNull(); - }); + expectNoSandboxDelete(restarted.runOpenshellSpy); + expect(restarted.onboardSpy).not.toHaveBeenCalled(); + expect( + (restarted.session.checkpoint as { sandboxRecreate: unknown }).sandboxRecreate, + ).toBeNull(); + }, + ); - it.each( - POST_DELETE_PHASES, - )("refuses a foreign same-name sandbox when a rebuild restarts from '%s' (#7734)", async (phase) => { - const restarted = restartRebuild(FOREIGN_PROBE, await interruptAfterCreate(phase)); + it.each(POST_DELETE_PHASES)( + "refuses a foreign same-name sandbox when a rebuild restarts from '%s' (#7734)", + async (phase) => { + const restarted = restartRebuild(FOREIGN_PROBE, await interruptAfterCreate(phase)); - await expect(restarted.rebuildSandbox("alpha", ["--yes"])).rejects.toThrow( - /not the journaled replacement/, - ); + await expect(restarted.rebuildSandbox("alpha", ["--yes"])).rejects.toThrow( + /not the journaled replacement/, + ); - expectNoSandboxDelete(restarted.runOpenshellSpy); - expect(restarted.onboardSpy).not.toHaveBeenCalled(); - }); + expectNoSandboxDelete(restarted.runOpenshellSpy); + expect(restarted.onboardSpy).not.toHaveBeenCalled(); + }, + ); const SOURCE_PROBE = "Name: alpha\nId: sbx-source\nPhase: Ready\n"; const MISSING_SOURCE = { @@ -242,65 +237,69 @@ describe("rebuildSandbox flow: recovery", () => { return restarted; } - it.each( - LIVE_SOURCE_PHASES, - )("deletes the journaled source when a rebuild restarts from '%s' (#7734)", async (phase) => { - const restarted = restartFromJournaledSource( - [SOURCE_PROBE, SOURCE_PROBE, null], - await interruptBeforeCreate(phase), - ); - - await restarted.rebuildSandbox("alpha", ["--yes"]); - - expect(restarted.runOpenshellSpy).toHaveBeenCalledWith( - ["sandbox", "delete", "-g", "nemoclaw", "alpha"], - expect.objectContaining({ ignoreError: true }), - ); - expect(restarted.onboardSpy).toHaveBeenCalled(); - }); - - it.each( - PRE_CREATE_PHASES, - )("creates the replacement without a second delete when a rebuild restarts from '%s' with the source already absent (#7734)", async (phase) => { - const restarted = restartFromJournaledSource([null], await interruptBeforeCreate(phase)); - - await restarted.rebuildSandbox("alpha", ["--yes"]); - - expectNoSandboxDelete(restarted.runOpenshellSpy); - expect(restarted.onboardSpy).toHaveBeenCalled(); - }); - - it.each( - LIVE_SOURCE_PHASES, - )("stops before deletion when a same-name sandbox appears after a '%s' restart probe (#7734)", async (phase) => { - const restarted = restartFromJournaledSource( - [null, FOREIGN_PROBE], - await interruptBeforeCreate(phase), - ); - - await expect( - restarted.rebuildSandbox("alpha", ["--yes"], { throwOnError: true }), - ).rejects.toThrow(/the live same-name sandbox is not the journaled source/); - - expectNoSandboxDelete(restarted.runOpenshellSpy); - expect(restarted.onboardSpy).not.toHaveBeenCalled(); - }); + it.each(LIVE_SOURCE_PHASES)( + "deletes the journaled source when a rebuild restarts from '%s' (#7734)", + async (phase) => { + const restarted = restartFromJournaledSource( + [SOURCE_PROBE, SOURCE_PROBE, null], + await interruptBeforeCreate(phase), + ); + + await restarted.rebuildSandbox("alpha", ["--yes"]); + + expect(restarted.runOpenshellSpy).toHaveBeenCalledWith( + ["sandbox", "delete", "-g", "nemoclaw", "alpha"], + expect.objectContaining({ ignoreError: true }), + ); + expect(restarted.onboardSpy).toHaveBeenCalled(); + }, + ); - it.each( - LIVE_SOURCE_PHASES, - )("refuses a changed same-name sandbox when a rebuild restarts from '%s' (#7734)", async (phase) => { - const restarted = restartFromJournaledSource( - [FOREIGN_PROBE, null], - await interruptBeforeCreate(phase), - ); + it.each(PRE_CREATE_PHASES)( + "creates the replacement without a second delete when a rebuild restarts from '%s' with the source already absent (#7734)", + async (phase) => { + const restarted = restartFromJournaledSource([null], await interruptBeforeCreate(phase)); - await expect(restarted.rebuildSandbox("alpha", ["--yes"])).rejects.toThrow( - /no longer has the journaled source identity/, - ); + await restarted.rebuildSandbox("alpha", ["--yes"]); - expectNoSandboxDelete(restarted.runOpenshellSpy); - expect(restarted.onboardSpy).not.toHaveBeenCalled(); - }); + expectNoSandboxDelete(restarted.runOpenshellSpy); + expect(restarted.onboardSpy).toHaveBeenCalled(); + }, + ); + + it.each(LIVE_SOURCE_PHASES)( + "stops before deletion when a same-name sandbox appears after a '%s' restart probe (#7734)", + async (phase) => { + const restarted = restartFromJournaledSource( + [null, FOREIGN_PROBE], + await interruptBeforeCreate(phase), + ); + + await expect( + restarted.rebuildSandbox("alpha", ["--yes"], { throwOnError: true }), + ).rejects.toThrow(/the live same-name sandbox is not the journaled source/); + + expectNoSandboxDelete(restarted.runOpenshellSpy); + expect(restarted.onboardSpy).not.toHaveBeenCalled(); + }, + ); + + it.each(LIVE_SOURCE_PHASES)( + "refuses a changed same-name sandbox when a rebuild restarts from '%s' (#7734)", + async (phase) => { + const restarted = restartFromJournaledSource( + [FOREIGN_PROBE, null], + await interruptBeforeCreate(phase), + ); + + await expect(restarted.rebuildSandbox("alpha", ["--yes"])).rejects.toThrow( + /no longer has the journaled source identity/, + ); + + expectNoSandboxDelete(restarted.runOpenshellSpy); + expect(restarted.onboardSpy).not.toHaveBeenCalled(); + }, + ); it("refuses a live same-name sandbox when a rebuild restarts from 'deleted' (#7734)", async () => { const restarted = restartFromJournaledSource( @@ -542,7 +541,7 @@ describe("rebuildSandbox flow: recovery", () => { ).toBeGreaterThan(harness.onboardSpy.mock.invocationCallOrder[0]); }); - it("finishes the rebuild while surfacing incomplete post-restore work", async () => { + it("fails the rebuild while surfacing incomplete OpenClaw post-restore work", async () => { const harness = createRebuildFlowHarness({ sandboxEntry: { policyPresetsFinalized: true, policyTier: "balanced" }, executeSandboxCommand: () => ({ status: 1, stdout: "", stderr: "hash refresh failed" }), @@ -562,7 +561,7 @@ describe("rebuildSandbox flow: recovery", () => { await expect( harness.rebuildSandbox("alpha", ["--yes"], { throwOnError: true }), - ).resolves.toBeUndefined(); + ).rejects.toThrow("OpenClaw config integrity verification failed after rebuild"); const output = harness.logSpy.mock.calls.map((call) => String(call[0])).join("\n"); expect(output).toContain("rebuilt but some post-restore steps were incomplete");