Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
29 changes: 18 additions & 11 deletions src/lib/actions/sandbox/destroy-execution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,8 @@ export async function executeSandboxDestroy({
runtimeProviders,
);
const pendingCreateIdentity = sandbox?.pendingCreateIdentity;
const expectedSandboxIdentityFingerprint =
pendingCreateIdentity?.sandboxIdentityFingerprint ?? expectedContainerIdentityFingerprint;
const expectedContainerProof: DestroyContainerIdentityProof = expectedRuntimeProviderIdentity
? {
identities: undefined,
Expand All @@ -479,12 +481,15 @@ export async function executeSandboxDestroy({
if (verdict.status === "recovery") return { identities: verdict.identities };
return null;
};
const inspectPendingCreateVerificationContinuity = (): IdentityContinuity => {
if (!pendingCreateIdentity) return { status: "match" };
const inspectSandboxIdentityContinuity = (): IdentityContinuity => {
if (!expectedSandboxIdentityFingerprint) return { status: "match" };
const subject = pendingCreateIdentity
? "Pending create sandbox identity"
: "Retained sandbox identity";
if (!getSandbox) {
return {
status: "probe-failed",
subject: "Pending create sandbox identity",
subject,
detail: "an exact registry reader is unavailable",
};
}
Expand All @@ -495,8 +500,10 @@ export async function executeSandboxDestroy({
}
if (
sandboxConfirmedAbsent &&
expectedContainerIdentities !== undefined &&
expectedContainerIdentityFingerprint === pendingCreateIdentity.sandboxIdentityFingerprint
(!pendingCreateIdentity ||
(expectedContainerIdentities !== undefined &&
expectedContainerIdentityFingerprint ===
pendingCreateIdentity.sandboxIdentityFingerprint))
) {
return isDeepStrictEqual(readCurrentCheckpoint(), pendingCreateIdentity)
? { status: "match" }
Expand All @@ -507,30 +514,30 @@ export async function executeSandboxDestroy({
inspectOpenShellSandboxIdentityFingerprint;
const liveFingerprint = inspectIdentity({
sandboxName,
gatewayName: pendingCreateIdentity.gatewayName,
gatewayName: pendingCreateIdentity?.gatewayName ?? deleteGatewayName,
...(destroyRuntimeSelection ? { runtimeSelection: destroyRuntimeSelection } : {}),
});
if (
liveFingerprint !== pendingCreateIdentity.sandboxIdentityFingerprint ||
liveFingerprint !== expectedSandboxIdentityFingerprint ||
!isDeepStrictEqual(readCurrentCheckpoint(), pendingCreateIdentity)
) {
return {
status: "changed",
subject: "Pending create sandbox identity",
subject,
};
}
return { status: "match" };
} catch (error) {
return {
status: "probe-failed",
subject: "Pending create sandbox identity",
subject,
detail: redactDestroyError(error),
};
}
};
const inspectIdentityContinuity = (): IdentityContinuity => {
const pendingContinuity = inspectPendingCreateVerificationContinuity();
if (pendingContinuity.status !== "match") return pendingContinuity;
const sandboxContinuity = inspectSandboxIdentityContinuity();
if (sandboxContinuity.status !== "match") return sandboxContinuity;
if (portableContainerAuthority) {
try {
portableContainerAuthority.revalidate();
Expand Down
140 changes: 99 additions & 41 deletions src/lib/actions/sandbox/destroy-retained-recovery-flow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ describe("destroySandbox retained recovery flow", () => {
);

expect(harness.errorSpy).toHaveBeenCalledWith(
expect.stringContaining("without an atomic OpenShell delete-by-identity primitive"),
expect.stringContaining("Retained sandbox identity could not be inspected"),
);
expect(harness.runOpenshellSpy).not.toHaveBeenCalledWith(
["sandbox", "delete", "alpha"],
Expand Down Expand Up @@ -380,7 +380,7 @@ describe("destroySandbox retained recovery flow", () => {
);

it(
"does not delete a live retained sandbox when Docker identity is absent (#10547)",
"preserves live recovery when OpenShell identity cannot be inspected (#10863)",
{ timeout: 30_000 },
async () => {
const recovery = retainedRecoveryRecord();
Expand All @@ -398,7 +398,7 @@ describe("destroySandbox retained recovery flow", () => {
);

expect(harness.errorSpy).toHaveBeenCalledWith(
expect.stringContaining("cannot bind a mutable-name delete to the retained record"),
expect.stringContaining("sandbox identity could not be inspected"),
);
expect(harness.runOpenshellSpy).not.toHaveBeenCalledWith(
["sandbox", "delete", "alpha"],
Expand All @@ -410,12 +410,13 @@ describe("destroySandbox retained recovery flow", () => {
);

it(
"does not issue mutable-name deletion for a live retained sandbox with matching identity (#10547)",
"destroys a live retained sandbox with matching identity (#10863)",
{ timeout: 30_000 },
async () => {
const recovery = retainedRecoveryRecord();
const containerId = "a".repeat(64);
const harness = createDestroyHarness({
liveListOutput: "Id: sb-alpha",
dockerRunResult: {
status: 0,
stdout: `${containerId}\topenshell\tdefault\tsb-alpha`,
Expand All @@ -427,64 +428,42 @@ describe("destroySandbox retained recovery flow", () => {
retainedRecoveryRecords: [recovery],
});

await expect(harness.destroySandbox("alpha", { yes: true })).rejects.toThrow(
"process.exit(1)",
);
await expect(harness.destroySandbox("alpha", { yes: true })).resolves.toBeUndefined();

expect(harness.errorSpy).toHaveBeenCalledWith(
expect.stringContaining("cannot bind a mutable-name delete to the retained record"),
);
expect(harness.runOpenshellSpy).not.toHaveBeenCalledWith(
["sandbox", "delete", "alpha"],
expect(harness.runOpenshellSpy).toHaveBeenCalledWith(
["sandbox", "delete", "-g", recovery.gatewayName, "alpha"],
expect.anything(),
);
expect(harness.resolveRetainedSandboxRecoverySpy).not.toHaveBeenCalled();
expect(harness.removeSandboxSpy).not.toHaveBeenCalled();
expect(harness.resolveRetainedSandboxRecoverySpy).toHaveBeenCalledWith(recovery);
expect(harness.removeSandboxSpy).toHaveBeenCalledWith("alpha");
expect(exitSpy).not.toHaveBeenCalled();
},
);

it(
"still fails closed for a live retained sandbox even with a proven-matching OpenShell identity (#10863)",
"destroys a matching live retained sandbox without a host Docker container (#10863)",
{ timeout: 30_000 },
async () => {
// OpenShell exposes no atomic delete-by-identity primitive, so no
// amount of identity proof inside NemoClaw can close the window where
// another OpenShell client replaces the sandbox under the same name
// between the last read and OpenShell processing the delete. Automatic
// deletion of a live retained sandbox is therefore always fail-closed,
// even when the live OpenShell id matches the retained record.
const recovery = retainedRecoveryRecord("sandbox-alpha");
const containerId = "a".repeat(64);
const harness = createDestroyHarness({
dockerRunResult: {
status: 0,
stdout: `${containerId}\topenshell\tdefault\tsandbox-alpha`,
},
liveListOutput: "Id: sandbox-alpha",
dockerRunResult: { status: 0, stdout: "" },
registryEntryOverrides: {
lifecycleGeneration: recovery.lifecycleGeneration!,
lifecycleLiveIdentityFingerprint: recovery.sandboxIdentityFingerprint!,
},
retainedRecoveryRecords: [recovery],
});

await expect(harness.destroySandbox("alpha", { yes: true })).rejects.toThrow(
"process.exit(1)",
);
await expect(harness.destroySandbox("alpha", { yes: true })).resolves.toBeUndefined();

expect(harness.errorSpy).toHaveBeenCalledWith(
expect.stringContaining("cannot bind a mutable-name delete to the retained record"),
);
expect(harness.errorSpy.mock.calls.flat().join("\n")).not.toContain(
"openshell sandbox delete",
);
expect(harness.errorSpy).toHaveBeenCalledWith(
expect.stringContaining(`openshell sandbox list -g ${recovery.gatewayName} -o json`),
);
expect(harness.runOpenshellSpy).not.toHaveBeenCalledWith(
["sandbox", "delete", "alpha"],
expect(harness.runOpenshellSpy).toHaveBeenCalledWith(
["sandbox", "delete", "-g", recovery.gatewayName, "alpha"],
expect.anything(),
);
expect(harness.resolveRetainedSandboxRecoverySpy).not.toHaveBeenCalled();
expect(harness.resolveRetainedSandboxRecoverySpy).toHaveBeenCalledWith(recovery);
expect(harness.removeSandboxSpy).toHaveBeenCalledWith("alpha");
expect(exitSpy).not.toHaveBeenCalled();
},
);

Expand Down Expand Up @@ -514,6 +493,85 @@ describe("destroySandbox retained recovery flow", () => {
},
);

it.each([
["a replacement sandbox", "Id: sandbox-replacement", undefined],
["unknown presence", "Id: sb-alpha", { status: 1, stdout: "", stderr: "" }],
])(
"preserves retained identity when OpenShell reports %s (#10863)",
async (_scenario, liveListOutput, sandboxListResult) => {
const recovery = retainedRecoveryRecord();
const harness = createDestroyHarness({
liveListOutput,
dockerRunResult: { status: 0, stdout: "" },
registryEntryOverrides: {
lifecycleGeneration: recovery.lifecycleGeneration!,
lifecycleLiveIdentityFingerprint: recovery.sandboxIdentityFingerprint!,
},
retainedRecoveryRecords: [recovery],
...(sandboxListResult ? { sandboxListResult } : {}),
});

await expect(harness.destroySandbox("alpha", { yes: true })).rejects.toThrow(
"process.exit(1)",
);

expect(harness.events).toEqual([]);
expect(harness.removeSandboxSpy).not.toHaveBeenCalled();
expect(harness.resolveRetainedSandboxRecoverySpy).not.toHaveBeenCalled();
},
);

it("refuses retained identity drift at the delete boundary (#10863)", async () => {
const recovery = retainedRecoveryRecord();
const harness = createDestroyHarness({
dockerRunResult: { status: 0, stdout: "" },
registryEntryOverrides: {
lifecycleGeneration: recovery.lifecycleGeneration!,
lifecycleLiveIdentityFingerprint: recovery.sandboxIdentityFingerprint!,
},
retainedRecoveryRecords: [recovery],
});
let identityReads = 0;
harness.captureOpenshellSpy.mockImplementation((args: string[]) => {
identityReads += Number(args[0] === "sandbox" && args[1] === "get");
return {
status: 0,
output: `Id: ${identityReads < 5 ? "sb-alpha" : "sandbox-replacement"}`,
};
});

await expect(harness.destroySandbox("alpha", { yes: true })).rejects.toThrow("process.exit(1)");

expect(harness.events).toContain("detach");
expect(harness.events).not.toContain("delete");
expect(harness.errorSpy).toHaveBeenCalledWith(
expect.stringContaining("Retained sandbox identity changed at the delete boundary"),
);
expect(harness.removeSandboxSpy).not.toHaveBeenCalled();
expect(harness.resolveRetainedSandboxRecoverySpy).not.toHaveBeenCalled();
});

it("preserves matching retained recovery after deletion fails (#10863)", async () => {
const recovery = retainedRecoveryRecord();
const harness = createDestroyHarness({
liveListOutput: "Id: sb-alpha",
deleteStatus: 7,
deleteOutput: "delete failed",
dockerRunResult: { status: 0, stdout: "" },
registryEntryOverrides: {
lifecycleGeneration: recovery.lifecycleGeneration!,
lifecycleLiveIdentityFingerprint: recovery.sandboxIdentityFingerprint!,
},
retainedRecoveryRecords: [recovery],
});

await expect(harness.destroySandbox("alpha", { yes: true })).rejects.toThrow("process.exit(7)");

expect(harness.events).toContain("delete");
expect(harness.removeSandboxSpy).not.toHaveBeenCalled();
expect(harness.resolveRetainedSandboxRecoverySpy).not.toHaveBeenCalled();
});

it.each([
["present", undefined, "reports a sandbox present"],
["a failed list command", { status: 1, stdout: "", stderr: "" }, "could not determine"],
Expand Down
18 changes: 6 additions & 12 deletions src/lib/actions/sandbox/destroy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -897,23 +897,17 @@ async function destroySandboxUnlocked(
`Refusing to destroy sandbox '${sandboxName}': gateway authority changed during preflight.`,
);
}
if (retainedRecoveryAuthority && sandboxPresence !== "absent") {
// OpenShell has no atomic delete-by-identity primitive: it exposes no
// way to bind a mutable-name delete to the retained record's immutable
// sandbox id/resource version. Even a fresh identity read immediately
// before the delete command cannot close the window where another
// OpenShell client removes the retained sandbox and creates a
// replacement under the same name between that read and OpenShell
// processing the delete (#10863). Automatic deletion of a live retained
// sandbox is therefore always fail-closed. Inspection cannot authorize a
// later mutable-name delete, so the recovery record remains unresolved
// until OpenShell can prove absence through the owning gateway.
if (
retainedRecoveryAuthority &&
sandboxPresence !== "absent" &&
(sandboxPresence !== "present" || !retainedSandboxIdentityFingerprint)
) {
const presenceDetail =
sandboxPresence === "present"
? "OpenShell reports a sandbox present under this name."
: "OpenShell could not determine whether a sandbox is present under this name.";
console.error(
` Refusing to delete retained sandbox '${sandboxName}': ${presenceDetail} NemoClaw cannot bind a mutable-name delete to the retained record (create-attempt label '${retainedRecoveryAuthority.createAttemptNonce}') without an atomic OpenShell delete-by-identity primitive. No sandbox resources were removed. Preserve the recovery record. Inspect 'openshell sandbox list -g ${retainedRecoveryAuthority.gatewayName} -o json' for diagnosis only; do not run mutable-name deletion. Recovery remains blocked until the owning gateway reports the sandbox absent. Then rerun '${CLI_NAME} ${sandboxName} destroy --yes' to reconcile verified residual resources and the recovery record.`,
` Refusing to delete retained sandbox '${sandboxName}': ${presenceDetail} Its live identity cannot be verified. Inspect 'openshell sandbox list -g ${retainedRecoveryAuthority.gatewayName} -o json'. Preserve the recovery record; no sandbox resources were removed. Create-attempt label: '${retainedRecoveryAuthority.createAttemptNonce}'.`,
);
preparedManagedLlamaCppCleanup?.abort();
requestSandboxDestroyExit(1);
Expand Down
14 changes: 8 additions & 6 deletions src/lib/onboard/cancel-rollback.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ describe("createSandboxCancelRollback", () => {
expect(guidance).toContain(
`ai.nvidia.nemoclaw.create-attempt=${RECOVERY_CONTEXT.createAttemptNonce}`,
);
expect(guidance).toContain("did not run OpenShell's mutable-name deletion command");
expect(guidance).toContain("Do not delete the sandbox by mutable sandbox name");
expect(guidance).toContain("Shared inference providers are gateway configuration");
expect(guidance).toContain("not sandbox cleanup targets");
expect(guidance).toContain("nemoclaw new-sb destroy");
Expand Down Expand Up @@ -301,24 +299,28 @@ describe("makeOnboardCancelExit", () => {
});

describe("buildCancelRollbackMessage", () => {
it("preserves identity-bound recovery guidance", () => {
it("places supported destroy guidance before recovery diagnostics (#10863)", () => {
const message = buildCancelRollbackMessage("sb", SANDBOX_FINGERPRINT, RECOVERY_CONTEXT).join(
"\n",
);

expect(message).toContain("preserved incomplete sandbox 'sb'");
expect(message).toContain(SANDBOX_FINGERPRINT);
expect(message).toContain(RECOVERY_CONTEXT.createAttemptNonce);
expect(message).toContain("retained recovery evidence");
expect(message).toContain("does not authorize deletion by mutable name");
expect(message).toContain("nemoclaw sb destroy");
expect(message).toContain("verify and remove the retained sandbox");
expect(message.indexOf("nemoclaw sb destroy")).toBeLessThan(
message.indexOf(RECOVERY_CONTEXT.createAttemptNonce),
);
expect(message).not.toContain("openshell sandbox delete");
expect(message).not.toContain("cannot delete it by immutable identity");
});

it("does not refer to an undisplayed create-attempt label", () => {
const message = buildCancelRollbackMessage("sb", SANDBOX_FINGERPRINT).join("\n");

expect(message).toContain("preserve the displayed fingerprint");
expect(message).toContain("nemoclaw sb destroy");
expect(message).toContain(SANDBOX_FINGERPRINT);
expect(message).not.toContain("displayed create-attempt label");
});
});
Loading
Loading