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
52 changes: 24 additions & 28 deletions src/lib/actions/sandbox/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
import * as agentRuntime from "../../agent/runtime";
import { CLI_DISPLAY_NAME, CLI_NAME } from "../../cli/branding";
import { D, G, R, RD, YW } from "../../cli/terminal-style";
import { getNamedGatewayLifecycleState } from "../../gateway-runtime-action";
import { parseGatewayInference } from "../../inference/config";
import {
type ProviderHealthProbeOptions,
Expand All @@ -25,8 +24,6 @@ import * as nim from "../../inference/nim";
import * as sandboxVersion from "../../sandbox/version";
import * as shields from "../../shields";
import { parseSandboxPhase } from "../../state/gateway";
import type { Session } from "../../state/onboard-session";
import * as onboardSession from "../../state/onboard-session";
import * as registry from "../../state/registry";
import {
createSystemDeps as createSessionDeps,
Expand Down Expand Up @@ -249,6 +246,29 @@ async function printGatewayFailureLayerHeader(sandboxName: string): Promise<void
console.log(` ${getLayerHeader(failure.layer)}`);
}

function printMissingLiveSandboxStatusGuidance(
sandboxName: string,
lookup: SandboxGatewayState,
): void {
console.log("");
console.log(
` Sandbox '${sandboxName}' is registered locally, but is not present in the live OpenShell gateway.`,
);
if (lookup.recoveredGateway) {
const via = lookup.recoveryVia ? ` via ${lookup.recoveryVia}` : "";
console.log(
` The ${CLI_DISPLAY_NAME} gateway was just recovered${via}; it may still be reconciling post-restart sandbox state.`,
);
}
console.log(" No local registry entry was removed by this status check.");
console.log(
` Retry \`${CLI_NAME} ${sandboxName} status\` after the gateway finishes reconnecting.`,
);
console.log(
` If the sandbox was intentionally deleted, run \`${CLI_NAME} list\` to inspect the remaining sandboxes or \`${CLI_NAME} onboard\` to create a new one.`,
);
}

// eslint-disable-next-line complexity
export async function showSandboxStatus(sandboxName: string): Promise<void> {
// #2666: never let an unexpected throw from the gateway probe (e.g. openshell
Expand Down Expand Up @@ -385,31 +405,7 @@ export async function showSandboxStatus(sandboxName: string): Promise<void> {
console.log(lookup.output);
process.exit(1);
} else if (lookup.state === "missing") {
// Belt-and-suspenders: only destroy registry state if the nemoclaw gateway
// is demonstrably the healthy active gateway. Guards against regressions
// in the reconciler.
const guard = getNamedGatewayLifecycleState();
if (guard.state !== "healthy_named") {
console.log("");
if (guard.state === "connected_other") {
printWrongGatewayActiveGuidance(sandboxName, guard.activeGateway, console.log);
} else {
await printGatewayFailureLayerHeader(sandboxName);
printGatewayLifecycleHint(guard.status || "", sandboxName, console.log);
}
} else {
registry.removeSandbox(sandboxName);
const session = onboardSession.loadSession();
if (session && session.sandboxName === sandboxName) {
onboardSession.updateSession((s: Session) => {
s.sandboxName = null;
return s;
});
}
console.log("");
console.log(` Sandbox '${sandboxName}' is not present in the live OpenShell gateway.`);
console.log(" Removed stale local registry entry.");
}
printMissingLiveSandboxStatusGuidance(sandboxName, lookup);
process.exit(1);
} else if (lookup.state === "identity_drift") {
console.log("");
Expand Down
13 changes: 8 additions & 5 deletions test/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6146,7 +6146,7 @@ describe("CLI dispatch", () => {
testTimeout(10_000),
);

it("auto-cleans an orphan registry entry on status when the named gateway is healthy", () => {
it("preserves an orphan registry entry on passive status when the named gateway is healthy", () => {
const home = fs.mkdtempSync(path.join(os.tmpdir(), "nemoclaw-cli-status-orphan-"));
const localBin = path.join(home, "bin");
const registryDir = path.join(home, ".nemoclaw");
Expand Down Expand Up @@ -6200,12 +6200,15 @@ describe("CLI dispatch", () => {

expect(statusResult.code).toBe(1);
expect(statusResult.out).not.toContain("Inference: healthy");
expect(statusResult.out).toContain("is not present in the live OpenShell gateway");
expect(statusResult.out).toContain("Removed stale local registry entry");
expect(statusResult.out).toContain(
"registered locally, but is not present in the live OpenShell gateway",
);
expect(statusResult.out).toContain("No local registry entry was removed");
expect(statusResult.out).not.toContain("Removed stale local registry entry");

const saved = JSON.parse(fs.readFileSync(path.join(registryDir, "sandboxes.json"), "utf8"));
expect(saved.sandboxes.alpha).toBeUndefined();
expect(saved.defaultSandbox).toBeNull();
expect(saved.sandboxes.alpha).toBeDefined();
expect(saved.defaultSandbox).toBe("alpha");
});
});

Expand Down
18 changes: 8 additions & 10 deletions test/gateway-state-reconcile-2276.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,10 +315,10 @@ describe("Scenario 1: connect — healthy nemoclaw active + sandbox NotFound tru
});
});

// ─── Scenario 2 ─── destructive path preserved for `status` ────────────────
// ─── Scenario 2 ─── passive `status` must preserve registry state ─────────
describe("Scenario 2: status — healthy nemoclaw active + sandbox NotFound truly gone", () => {
it(
"removes the registry entry, clears session, and logs removal (no exit 1)",
"reports the missing live sandbox without removing local registry state",
{ timeout: TIMEOUT_MS },
() => {
writeStubOpenshell({
Expand All @@ -331,18 +331,16 @@ describe("Scenario 2: status — healthy nemoclaw active + sandbox NotFound trul

const r = runCli("status");

// status doesn't exit non-zero on a missing sandbox — it just logs.
assert.equal(r.status, 1, `expected exit 1, got ${r.status}`);
assert.equal(
registrySandboxPresent(r),
false,
`expected registry entry removed, got: ${JSON.stringify(r.registry)}`,
);
assert.equal(
r.sessionSandboxName === null || r.sessionSandboxName === undefined,
true,
`expected session sandboxName cleared, got: ${r.sessionSandboxName}`,
`expected registry entry preserved, got: ${JSON.stringify(r.registry)}`,
);
assert.match(r.stdout, /Removed stale local registry entry/);
assert.equal(r.sessionSandboxName, SANDBOX_NAME, "expected session sandboxName preserved");
assert.match(r.stdout, /registered locally, but is not present/);
assert.match(r.stdout, /No local registry entry was removed/);
assert.doesNotMatch(r.stdout, /Removed stale local registry entry/);
},
);
});
Expand Down
Loading