Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
cc9a3dd
fix(rebuild): restart the Hermes gateway after state restore
hunglp6d Aug 6, 2026
a19a17d
test(rebuild): stub post-restore gateway restart
apurvvkumaria Aug 6, 2026
7c73e9d
merge: resolve conflicts with main
github-actions[bot] Aug 6, 2026
b0669f9
Merge branch 'main' into fix/rebuild-gateway-restart-after-restore
hunglp6d Aug 6, 2026
005adee
Merge branch 'main' into fix/rebuild-gateway-restart-after-restore
hunglp6d Aug 6, 2026
46940cb
fix(rebuild): restore gateway recheck bound
apurvvkumaria Aug 6, 2026
ae6e940
Merge branch 'main' into fix/rebuild-gateway-restart-after-restore
hunglp6d Aug 6, 2026
3f22f53
Merge branch 'main' into fix/rebuild-gateway-restart-after-restore
hunglp6d Aug 7, 2026
c2c8063
Merge branch 'main' into fix/rebuild-gateway-restart-after-restore
cv Aug 7, 2026
a88afeb
Merge branch 'main' into fix/rebuild-gateway-restart-after-restore
hunglp6d Aug 7, 2026
5034182
Merge branch 'main' into fix/rebuild-gateway-restart-after-restore
hunglp6d Aug 7, 2026
fee1792
Merge branch 'main' into fix/rebuild-gateway-restart-after-restore
hunglp6d Aug 7, 2026
ea7d8a0
merge: resolve conflicts with main
github-actions[bot] Aug 7, 2026
450e278
merge: refresh main before cron gate fix
apurvvkumaria Aug 7, 2026
8ffba2d
fix(rebuild): hold cron gate through gateway replacement
apurvvkumaria Aug 7, 2026
64c4d45
fix(rebuild): bind cron release to verified gateway
apurvvkumaria Aug 7, 2026
2f8a6cc
merge: refresh main after cron gate fix
apurvvkumaria Aug 7, 2026
2f7c35b
fix(rebuild): order held-gate recovery guidance
apurvvkumaria Aug 7, 2026
54a3b19
fix(rebuild): report cron gate rollback uncertainty
apurvvkumaria Aug 7, 2026
15488b5
merge: refresh main after cron rollback fix
apurvvkumaria Aug 7, 2026
fdf6415
merge: refresh main after E2E recovery update
apurvvkumaria Aug 7, 2026
867cbe4
fix(hermes): refresh cron controller integrity hash
apurvvkumaria Aug 7, 2026
fa39efa
merge: refresh main after integrity hash fix
apurvvkumaria Aug 7, 2026
b4a011e
merge: refresh main after llama runtime cleanup
apurvvkumaria Aug 7, 2026
3e6ca26
fix(rebuild): type cron rollback failures
apurvvkumaria Aug 7, 2026
d588ed9
merge: refresh main for PR 8472
apurvvkumaria Aug 7, 2026
8b4ba8e
merge: refresh main for PR 8472
apurvvkumaria Aug 7, 2026
42bf3cc
test(hermes): preserve operator drain on restore failures
apurvvkumaria Aug 7, 2026
5f29a10
fix(rebuild): make Hermes cron recovery durable
apurvvkumaria Aug 7, 2026
9ed6cd7
merge(main): refresh PR #8472
apurvvkumaria Aug 7, 2026
78d68cc
Merge branch 'main' into fix/rebuild-gateway-restart-after-restore
hunglp6d Aug 8, 2026
ab17f4f
Merge branch 'main' into fix/rebuild-gateway-restart-after-restore
hunglp6d Aug 8, 2026
e0c75cc
Merge branch 'main' into fix/rebuild-gateway-restart-after-restore
hunglp6d Aug 9, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions docs/manage-sandboxes/recover-rebuild-sandboxes.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,10 @@ $$nemoclaw <sandbox-name> gateway-token --quiet
```

Before post-restore repairs, NemoClaw verifies that the recreated sandbox still identifies as Hermes and exits nonzero if its identity does not match the rebuild target.
After state restore, NemoClaw restores managed MCP configuration through the normal lifecycle, then re-proves or recovers gateway health and performs final MCP reconciliation.
After state restore, NemoClaw restores managed MCP configuration through the normal lifecycle, then restarts the Hermes gateway and verifies or recovers its health before performing final MCP reconciliation.
The gateway starts during recreation and reads its durable state before the restore replaces it, so the restart is what binds the running gateway to the restored state.
`rebuild` exits nonzero instead of reporting success when it cannot verify final gateway health or managed MCP state.
Follow the printed recovery guidance, using `$$nemoclaw <sandbox-name> recover` for gateway health and `$$nemoclaw <sandbox-name> mcp restart` for incomplete managed MCP restoration.
Follow the printed recovery guidance, using `$$nemoclaw <sandbox-name> gateway restart` first for gateway health, `$$nemoclaw <sandbox-name> recover` when the restart does not restore verified health, and `$$nemoclaw <sandbox-name> mcp restart` for incomplete managed MCP restoration.

When the rebuild backup contains active Hermes cron jobs that reference scripts, NemoClaw validates those script references before it deletes the existing sandbox.
The check covers the default profile and named profiles.
Expand Down
103 changes: 103 additions & 0 deletions src/lib/actions/sandbox/rebuild-hermes-post-restore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,84 @@ import {
} from "../../../../test/helpers/rebuild-flow-harness";
import { ensureHermesGatewayAfterStateRestore } from "./rebuild-hermes-post-restore";

const RESTART_SUCCEEDED = {
ok: true,
restarted: true,
healthPassed: true,
forwardRecovered: false,
} as const;
const RESTART_FAILED = {
ok: false,
failureLayer: "health timeout",
detail: "gateway did not become healthy",
} as const;

describe("binding the Hermes gateway to restored state", () => {
it("restarts the gateway before reading its health (#8184)", () => {
const order: string[] = [];
const state = ensureHermesGatewayAfterStateRestore("alpha", "hermes", {
restartSandboxGateway: () => {
order.push("restart");
return RESTART_SUCCEEDED;
},
checkAndRecoverSandboxProcesses: () => {
order.push("check");
return { checked: true, wasRunning: true, recovered: false };
},
});

expect(state).toBe("healthy");
expect(order).toEqual(["restart", "check"]);
});

// The bug this replaces: the gateway read its durable state at startup, the
// restore replaced that state afterwards, and a live process satisfied the
// old liveness check while still serving what it read before the restore.
it("refuses a gateway that stayed up through a failed restart (#8184)", () => {
const state = ensureHermesGatewayAfterStateRestore("alpha", "hermes", {
restartSandboxGateway: () => RESTART_FAILED,
checkAndRecoverSandboxProcesses: () => ({
checked: true,
wasRunning: true,
recovered: false,
}),
});

expect(state).toBe("unverified");
});

it("accepts a gateway the recovery check replaced after a failed restart (#8184)", () => {
const state = ensureHermesGatewayAfterStateRestore("alpha", "hermes", {
restartSandboxGateway: () => RESTART_FAILED,
checkAndRecoverSandboxProcesses: () => ({
checked: true,
wasRunning: false,
recovered: true,
}),
});

expect(state).toBe("recovered");
});

it("leaves a non-Hermes rebuild without a gateway restart (#8184)", () => {
const restartSandboxGateway = vi.fn(() => RESTART_SUCCEEDED);
const checkAndRecoverSandboxProcesses = vi.fn(() => ({
checked: true,
wasRunning: true,
recovered: false,
}));

const state = ensureHermesGatewayAfterStateRestore("alpha", "openclaw", {
restartSandboxGateway,
checkAndRecoverSandboxProcesses,
});

expect(state).toBe("not-applicable");
expect(restartSandboxGateway).not.toHaveBeenCalled();
expect(checkAndRecoverSandboxProcesses).not.toHaveBeenCalled();
});
});

describe("Hermes gateway post-restore recheck", () => {
it("accepts a gateway that becomes healthy after an inconclusive recovery check (#7084)", () => {
const checkAndRecoverSandboxProcesses = vi
Expand All @@ -26,6 +104,7 @@ describe("Hermes gateway post-restore recheck", () => {

expect(
ensureHermesGatewayAfterStateRestore("alpha", "hermes", {
restartSandboxGateway: () => RESTART_SUCCEEDED,
checkAndRecoverSandboxProcesses,
}),
).toBe("healthy");
Expand All @@ -47,6 +126,7 @@ describe("Hermes gateway post-restore recheck", () => {

expect(
ensureHermesGatewayAfterStateRestore("alpha", "hermes", {
restartSandboxGateway: () => RESTART_SUCCEEDED,
checkAndRecoverSandboxProcesses,
}),
).toBe("unverified");
Expand All @@ -63,6 +143,7 @@ describe("Hermes gateway post-restore recheck", () => {

expect(
ensureHermesGatewayAfterStateRestore("alpha", "hermes", {
restartSandboxGateway: () => RESTART_SUCCEEDED,
checkAndRecoverSandboxProcesses,
}),
).toBe("unverified");
Expand Down Expand Up @@ -264,6 +345,28 @@ describe("Hermes rebuild post-restore verification", () => {
);
});

it("restarts the gateway between the state restore and the health check (#8184)", async () => {
const harness = createRebuildFlowHarness({
agentName: "hermes",
sandboxEntry: { agent: "hermes" },
});

await expect(
harness.rebuildSandbox("alpha", ["--yes"], { throwOnError: true }),
).resolves.toBeUndefined();

expect(harness.restartSandboxGatewaySpy).toHaveBeenCalledWith("alpha", { quiet: true });
expect(harness.restoreSandboxStateSpy.mock.invocationCallOrder[0]).toBeLessThan(
harness.restartSandboxGatewaySpy.mock.invocationCallOrder[0],
);
expect(harness.restartSandboxGatewaySpy.mock.invocationCallOrder[0]).toBeLessThan(
harness.checkAndRecoverSandboxProcessesSpy.mock.invocationCallOrder[0],
);
expect(harness.logSpy).toHaveBeenCalledWith(
expect.stringContaining("Hermes gateway restarted and verified after state restore"),
);
});

it("fails before recovery when recreated Hermes identity mismatches (#7084)", async () => {
const harness = createRebuildFlowHarness({
agentName: "hermes",
Expand Down
33 changes: 27 additions & 6 deletions src/lib/actions/sandbox/rebuild-hermes-post-restore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import { CLI_NAME } from "../../cli/branding";
import { isDirectSandboxFallbackUnavailableError } from "../../sandbox/privileged-exec";
import type { GatewayRestartResult } from "./gateway-restart";
import * as processRecovery from "./process-recovery";

const HERMES_CRON_CONTROL = "/usr/local/lib/nemoclaw/hermes-cron-restore-control.py";
Expand Down Expand Up @@ -75,21 +76,37 @@ interface HermesPostRestoreGatewayDeps {
sandboxName: string,
options: { quiet: boolean },
) => GatewayRecoveryObservation;
restartSandboxGateway?: (
sandboxName: string,
options: { quiet: boolean },
) => GatewayRestartResult;
}

/**
* Re-prove Hermes gateway health after workspace state restoration.
* Bind the running Hermes gateway to the state this rebuild just restored.
*
* Recreation starts the gateway, and the restore replaces its durable state
* afterwards. An adapter that reads that state once at startup keeps the
* pre-restore result for the life of the process — the WhatsApp bridge reads
* its paired session that way — so the gateway can be alive and healthy while
* still serving the state the rebuild replaced. A liveness check cannot see
* that difference, so restart first and let the check report on the process
* that restart produced. `relaunchManagedSupervisorSession` already restarts
* after its own restore for the same reason.
*
* Inner onboarding verifies the fresh image before rebuild restores the prior
* state. That restore can still stop or wedge the gateway, so its earlier
* readiness message is not authoritative for rebuild completion.
* The restart belongs here rather than inside `runHermesCronRestoreTransaction`:
* that gate records the gateway `pid` and `start_time` when it drains dispatch
* and compares them again on validate and release, so a restart inside it would
* fail its own identity check.
*/
export function ensureHermesGatewayAfterStateRestore(
sandboxName: string,
agentName: string,
deps: HermesPostRestoreGatewayDeps = {},
): HermesPostRestoreGatewayState {
if (agentName !== "hermes") return "not-applicable";
const restart = deps.restartSandboxGateway ?? processRecovery.restartSandboxGateway;
const restarted = restart(sandboxName, { quiet: true }).ok;
const checkAndRecover =
deps.checkAndRecoverSandboxProcesses ?? processRecovery.checkAndRecoverSandboxProcesses;
for (let attempt = 1; attempt <= HERMES_GATEWAY_RECHECK_ATTEMPTS; attempt += 1) {
Expand All @@ -102,8 +119,12 @@ export function ensureHermesGatewayAfterStateRestore(
return "unverified";
}
if (!observation.checked) continue;
if (observation.wasRunning === true) return "healthy";
// Recovery replaces the process, so a recovered gateway reads the restored
// state whatever the restart reported. A gateway that stayed up through a
// failed restart is still serving what it read before the restore, which is
// the state this step exists to replace.
if (observation.recovered) return "recovered";
if (restarted && observation.wasRunning === true) return "healthy";
}
return "unverified";
}
Expand All @@ -115,7 +136,7 @@ export function printHermesGatewayRestoreRecovery(
): void {
if (state !== "unverified") return;
writeLine(
` Hermes gateway health was not verified after state restore — run \`${CLI_NAME} ${sandboxName} recover\` before relying on this sandbox`,
` Hermes gateway health was not verified after state restore — it can still be serving the state this rebuild replaced; run \`${CLI_NAME} ${sandboxName} gateway restart\`, then \`${CLI_NAME} ${sandboxName} recover\` if that fails`,
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib/actions/sandbox/rebuild-post-restore-phase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ export async function runRebuildPostRestorePhase(
);
const hermesGatewayRestoreUnverified = hermesGatewayRestoreState === "unverified";
if (hermesGatewayRestoreState === "healthy") {
console.log(` ${G}\u2713${R} Hermes gateway health verified after state restore`);
console.log(` ${G}\u2713${R} Hermes gateway restarted and verified after state restore`);
} else if (hermesGatewayRestoreState === "recovered") {
console.log(` ${G}\u2713${R} Hermes gateway recovered after state restore`);
}
Expand Down
7 changes: 7 additions & 0 deletions test/e2e/live/rebuild-hermes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1220,6 +1220,13 @@ test(STALE_BASE_REBUILD
expect(rebuildOutput).toContain(`Using Hermes Agent base image: ${phase1BaseResolution.ref}`);
expect(rebuildOutput).not.toContain("Rebuilding Hermes Agent base image");
expect(rebuildOutput).not.toMatch(/provider credential not found/i);
// The gateway starts during recreation and reads its durable state before the
// restore replaces it, so rebuild must hand back a process that started after
// the restore. Either post-restore path reports one; a live gateway that was
// only checked reports neither.
expect(rebuildOutput, "rebuild must report a Hermes gateway bound to the restored state").toMatch(
/Hermes gateway (?:restarted and verified|recovered) after state restore/u,
);
await waitForSandboxReady(host, apiKey, activeOpenshellBin, "phase-6-post-rebuild");

const backupPathText = rebuildOutput.match(/^\s*Backup:\s+(.+)$/mu)?.[1]?.trim();
Expand Down
15 changes: 15 additions & 0 deletions test/helpers/rebuild-flow-harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { createRequire } from "node:module";
import path from "node:path";

import { type MockInstance, vi } from "vitest";
import type { GatewayRestartResult } from "../../src/lib/actions/sandbox/gateway-restart";

type RebuildSandbox = typeof import("../../src/lib/actions/sandbox/rebuild")["rebuildSandbox"];

Expand Down Expand Up @@ -102,6 +103,7 @@ export type RebuildFlowOverrides = {
secretBoundaryRefused?: boolean;
mcpReconciliationRefused?: boolean;
};
restartSandboxGateway?: () => GatewayRestartResult;
onboard?: (session: RebuildFlowSession) => Promise<void> | void;
repairMutableConfigPerms?: () =>
| { applied: false; skipReason: "agent" | "locked" | "unreadable"; reason: string }
Expand Down Expand Up @@ -166,6 +168,7 @@ export type RebuildFlowHarness = {
restoreTrustedAgentRemoteBaseImageOverrideSpy: MockInstance;
executeSandboxCommandSpy: MockInstance;
checkAndRecoverSandboxProcessesSpy: MockInstance;
restartSandboxGatewaySpy: MockInstance;
ensureMessagingHostForwardAfterRebuildSpy: MockInstance;
logSpy: MockInstance;
finalizeIncompleteOnboardStepSpy: MockInstance;
Expand Down Expand Up @@ -777,6 +780,17 @@ export function createRebuildFlowHarness(overrides: RebuildFlowOverrides = {}):
forwardRecovered: false,
})),
);
const restartSandboxGatewaySpy = vi
.spyOn(processRecovery, "restartSandboxGateway")
.mockImplementation(
overrides.restartSandboxGateway ??
(() => ({
ok: true,
restarted: true,
healthPassed: true,
forwardRecovered: false,
})),
);
vi.spyOn(shields, "repairMutableConfigPerms").mockImplementation(
overrides.repairMutableConfigPerms ?? (() => ({ applied: true, verified: true, errors: [] })),
);
Expand Down Expand Up @@ -833,6 +847,7 @@ export function createRebuildFlowHarness(overrides: RebuildFlowOverrides = {}):
restoreTrustedAgentRemoteBaseImageOverrideSpy,
executeSandboxCommandSpy,
checkAndRecoverSandboxProcessesSpy,
restartSandboxGatewaySpy,
ensureMessagingHostForwardAfterRebuildSpy,
logSpy,
finalizeIncompleteOnboardStepSpy,
Expand Down
12 changes: 12 additions & 0 deletions test/helpers/rebuild-flow-test-harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,17 @@ export function createRebuildFlowHarness(overrides: RebuildFlowOverrides = {}):
forwardRecovered: false,
})),
);
const restartSandboxGatewaySpy = vi
.spyOn(processRecovery, "restartSandboxGateway")
.mockImplementation(
overrides.restartSandboxGateway ??
(() => ({
ok: true,
restarted: true,
healthPassed: true,
forwardRecovered: false,
})),
);
vi.spyOn(shields, "repairMutableConfigPerms").mockImplementation(
overrides.repairMutableConfigPerms ?? (() => ({ applied: true, verified: true, errors: [] })),
);
Expand Down Expand Up @@ -633,6 +644,7 @@ export function createRebuildFlowHarness(overrides: RebuildFlowOverrides = {}):
applyPresetSpy,
backupSandboxStateSpy,
checkAndRecoverSandboxProcessesSpy,
restartSandboxGatewaySpy,
errorSpy,
executeSandboxCommandSpy,
ensureMessagingHostForwardAfterRebuildSpy,
Expand Down
5 changes: 4 additions & 1 deletion test/helpers/rebuild-flow-test-support.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
// SPDX-License-Identifier: Apache-2.0

import { type MockInstance, vi } from "vitest";
import type { GatewayRestartResult } from "../../src/lib/actions/sandbox/gateway-restart";
import type { SandboxGatewayState } from "../../src/lib/actions/sandbox/gateway-state";
import type {
finalizePreparedRebuildImageMessagingPlan,
RebuildImagePreflightResult,
} from "../../src/lib/actions/sandbox/rebuild-custom-image-preflight";
import type { PreservedEnvFile } from "../../src/lib/state/preserved-env";
import type { RebuildRecreateOnboardOpts } from "../../src/lib/actions/sandbox/rebuild-gpu-opt-out";
import type { VersionCheckResult } from "../../src/lib/sandbox/version";
import type { PreservedEnvFile } from "../../src/lib/state/preserved-env";
import type { SandboxRemovalReceipt } from "../../src/lib/state/registry";

export type RebuildSandbox =
Expand Down Expand Up @@ -51,6 +52,7 @@ export type RebuildFlowOverrides = {
secretBoundaryRefused?: boolean;
mcpReconciliationRefused?: boolean;
};
restartSandboxGateway?: () => GatewayRestartResult;
onboard?: (
session: RebuildFlowSession,
options: RebuildRecreateOnboardOpts,
Expand Down Expand Up @@ -132,6 +134,7 @@ export type RebuildFlowHarness = {
applyPresetSpy: MockInstance;
backupSandboxStateSpy: MockInstance;
checkAndRecoverSandboxProcessesSpy: MockInstance;
restartSandboxGatewaySpy: MockInstance;
errorSpy: MockInstance;
executeSandboxCommandSpy: MockInstance;
ensureMessagingHostForwardAfterRebuildSpy: MockInstance;
Expand Down
Loading