Skip to content
Merged
13 changes: 13 additions & 0 deletions docs/manage-sandboxes/recover-rebuild-sandboxes.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,19 @@ Use `nemo-deepagents <sandbox-name> status`, `logs`, `connect`, and `rebuild` fo
If the terminal runtime reports degraded health, rebuild the sandbox instead of using `recover` or `gateway restart`.
</AgentOnly>

<AgentOnly variant="openclaw,hermes">
### Host OpenShell Gateway Versus In-Sandbox Agent Recovery

`recover` and `start` repair the in-sandbox agent gateway and the host-side port forwards for one named sandbox.
`connect --probe-only` waits for that sandbox to become ready, rechecks it on its recorded gateway, and then verifies or repairs the same sandbox-scoped processes and forwards.
These commands do not restart, replace, or reap the shared host OpenShell gateway process.
If the host gateway RPC returns an error while these sandbox-scoped commands run, they surface the error with explicit next-step guidance and exit rather than swapping the shared gateway out from under other sandboxes.
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

To repair the host gateway itself, use `openshell status`, `openshell gateway start --name <gateway>`, or run `$$nemoclaw onboard` again.
Other workflows, including onboarding, rebuild, and `doctor --fix`, can explicitly recover the named host gateway when their operation requires it.
`$$nemoclaw <name> gateway restart` instead restarts only the supported agent gateway inside the named sandbox.
</AgentOnly>

## Rebuild While Preserving State

<AgentOnly variant="openclaw">
Expand Down
5 changes: 3 additions & 2 deletions docs/reference/commands.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,7 @@ If only pending registrations remain, the command exits non-zero and tells you t
If the registry remains empty after recovery, it tells you to run `$$nemoclaw onboard`.
A registered sandbox literally named `connect` keeps the name-first reading.
If the sandbox is not yet in the `Ready` phase, `connect` polls `openshell sandbox list` every few seconds and prints the current phase. This gives you progress output right after onboarding, when the 2.4 GB image is still pulling, instead of a silent hang.
Control the wait budget with `NEMOCLAW_CONNECT_TIMEOUT` (integer seconds, default `120`). When the deadline expires, `connect` exits non-zero with the last-seen phase.
Control the wait budget with `NEMOCLAW_CONNECT_TIMEOUT` in integer seconds. An interactive connection defaults to `120` seconds, while `--probe-only` defaults to `300` seconds so a scripted health check can wait through a cold sandbox start. When the deadline expires, `connect` exits non-zero with the last-seen phase.

On a TTY, a one-shot hint prints before dropping into the sandbox shell.
The hint is agent-aware. It names the correct TUI command for the sandbox's agent and reminds you to use `/exit` to leave the chat before `exit` returns you to the host shell.
Expand Down Expand Up @@ -853,7 +853,8 @@ $$nemoclaw my-assistant connect [--probe-only]
$$nemoclaw connect
```

The `--probe-only` flag verifies the sandbox is reachable over SSH and exits without opening a shell.
The `--probe-only` flag waits for the sandbox to become ready, verifies or repairs its in-sandbox agent process and host-side forward, and exits without opening a shell.
It rechecks the sandbox on its recorded OpenShell gateway after the readiness wait and never restarts the shared host gateway.
Use it for health checks and scripted readiness probes.

### `$$nemoclaw <name> exec`
Expand Down
2 changes: 1 addition & 1 deletion src/lib/actions/sandbox/connect-flow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe("connectSandbox flow", () => {
await expect(harness.connectSandbox("alpha")).rejects.toThrow("process.exit(0)");

expect(harness.captureOpenshellSpy).toHaveBeenCalledWith(
["sandbox", "list"],
["sandbox", "list", "-g", "nemoclaw"],
expect.objectContaining({ ignoreError: true }),
);
expect(harness.checkAndRecoverSpy).toHaveBeenCalledWith("alpha");
Expand Down
123 changes: 123 additions & 0 deletions src/lib/actions/sandbox/connect-probe-observe.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

import { afterEach, beforeEach, describe, expect, it, type MockInstance, vi } from "vitest";

import {
connectModulePath,
createConnectHarness,
requireDist,
} from "../../../../test/support/connect-flow-test-harness";

describe("connectSandbox probe-only observe mode", () => {
let exitSpy: MockInstance;

beforeEach(() => {
process.env.NEMOCLAW_TEST_NO_SLEEP = "1";
exitSpy = vi.spyOn(process, "exit").mockImplementation(((code?: number | string | null) => {
throw new Error(`process.exit(${code ?? 0})`);
}) as never);
});

afterEach(() => {
vi.restoreAllMocks();
vi.unstubAllEnvs();
delete process.env.NEMOCLAW_TEST_NO_SLEEP;
delete process.env.NEMOCLAW_CONNECT_TIMEOUT;
delete require.cache[requireDist.resolve(connectModulePath)];
});

it("passes gatewayRecovery=observe to ensureLiveSandboxOrExit on probeOnly", async () => {
const harness = createConnectHarness();

await expect(harness.connectSandbox("alpha", { probeOnly: true })).resolves.toBeUndefined();

expect(harness.ensureLiveSandboxSpy).toHaveBeenCalledWith(
"alpha",
expect.objectContaining({ allowNonReadyPhase: true, gatewayRecovery: "observe" }),
);
expect(exitSpy).not.toHaveBeenCalled();
});

it("uses gatewayRecovery=recover on the full connect path", async () => {
const harness = createConnectHarness();

await expect(harness.connectSandbox("alpha")).rejects.toThrow("process.exit(0)");

expect(harness.ensureLiveSandboxSpy).toHaveBeenCalledWith(
"alpha",
expect.objectContaining({ allowNonReadyPhase: true, gatewayRecovery: "recover" }),
);
});

it("re-observes the live sandbox after delayed readiness before process or forward recovery (#7173)", async () => {
const harness = createConnectHarness({
registryEntry: { gatewayPort: 8091 },
listOutputs: ["alpha Starting", "alpha Ready"],
processCheck: {
checked: true,
wasRunning: true,
recovered: false,
forwardRecovered: true,
},
});

await expect(harness.connectSandbox("alpha", { probeOnly: true })).resolves.toBeUndefined();

const listInvocations = harness.captureOpenshellSpy.mock.invocationCallOrder.filter(
(_, index) => {
const call = harness.captureOpenshellSpy.mock.calls[index];
return (
Array.isArray(call?.[0]) &&
(call[0] as string[])[0] === "sandbox" &&
(call[0] as string[])[1] === "list"
);
},
);
expect(listInvocations).toHaveLength(2);
const listArgs = harness.captureOpenshellSpy.mock.calls
.map((call) => call[0])
.filter(
(args): args is string[] =>
Array.isArray(args) && args[0] === "sandbox" && args[1] === "list",
);
expect(listArgs).toEqual([
["sandbox", "list", "-g", "nemoclaw-8091"],
["sandbox", "list", "-g", "nemoclaw-8091"],
]);
const liveLookupOrder = harness.ensureLiveSandboxSpy.mock.invocationCallOrder;
expect(liveLookupOrder).toHaveLength(2);
const recoveryOrder = harness.checkAndRecoverSpy.mock.invocationCallOrder;
expect(recoveryOrder).toHaveLength(1);
expect(listInvocations[1]).toBeLessThan(liveLookupOrder[1]);
expect(liveLookupOrder[1]).toBeLessThan(recoveryOrder[0]);
expect(harness.logSpy).toHaveBeenCalledWith(
expect.stringContaining("restored dashboard port forward"),
);
});

it("does not run process or forward recovery for a terminal sandbox phase (#7173)", async () => {
const harness = createConnectHarness({ listOutput: "alpha Error" });

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

expect(harness.ensureLiveSandboxSpy).toHaveBeenCalledOnce();
expect(harness.checkAndRecoverSpy).not.toHaveBeenCalled();
});

it("suggests a longer equivalent retry when probe-only readiness times out", async () => {
vi.spyOn(Date, "now").mockReturnValueOnce(0).mockReturnValue(300_001);
const harness = createConnectHarness({ listOutput: "alpha Starting" });

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

const errors = harness.errorSpy.mock.calls.map((call) => String(call[0])).join("\n");
expect(errors).toContain("Timed out after 300s waiting for sandbox 'alpha'");
expect(errors).toContain("NEMOCLAW_CONNECT_TIMEOUT=600 nemoclaw alpha connect --probe-only");
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
expect(harness.checkAndRecoverSpy).not.toHaveBeenCalled();
});
});
8 changes: 6 additions & 2 deletions src/lib/actions/sandbox/connect-route-containment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ describe("connect route containment", () => {

await expect(harness.connectSandbox("alpha", { probeOnly: true })).resolves.toBeUndefined();

expect(harness.ensureLiveSandboxSpy).toHaveBeenCalledOnce();
expect(harness.ensureLiveSandboxSpy).toHaveBeenCalledTimes(2);
expect(harness.checkAndRecoverSpy).toHaveBeenCalled();
expect(harness.captureOpenshellSpy).toHaveBeenCalled();
expect(harness.runOpenshellSpy).toHaveBeenCalledWith(
Expand Down Expand Up @@ -275,7 +275,11 @@ describe("connect route containment", () => {
releaseLock();

await expect(connect).rejects.toThrow("process.exit(1)");
expect(harness.captureOpenshellSpy).not.toHaveBeenCalled();
const routeReadCalls = harness.captureOpenshellSpy.mock.calls.filter((call) => {
const argv = Array.isArray(call?.[0]) ? (call[0] as string[]) : [];
return argv[0] === "sandbox" && argv[1] !== "list";
});
expect(routeReadCalls).toHaveLength(0);
expect(harness.runOpenshellSpy).not.toHaveBeenCalled();
expect(harness.applyVmDnsMonkeypatchSpy).not.toHaveBeenCalled();
expect(harness.runSetupDnsProxySpy).not.toHaveBeenCalled();
Expand Down
4 changes: 2 additions & 2 deletions src/lib/actions/sandbox/connect-route-lifecycle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ describe("connectSandbox route lifecycle", () => {
});

it("shell-quotes hostile route values in drift recovery commands (#3726)", async () => {
const sandboxName = "alpha's box";
const sandboxName = "alpha's-box";
const harness = createConnectHarness({
inferenceGetOutput:
"Gateway inference:\n Provider: openai; touch /tmp/pwn\n Model: $(id) model\n",
Expand All @@ -100,7 +100,7 @@ describe("connectSandbox route lifecycle", () => {

const errorOutput = harness.errorSpy.mock.calls.map((call) => String(call[0] ?? "")).join("\n");
expect(errorOutput).toContain(
"nemoclaw inference set --provider 'openai; touch /tmp/pwn' --model '$(id) model' --sandbox 'alpha'\\''s box'",
"nemoclaw inference set --provider 'openai; touch /tmp/pwn' --model '$(id) model' --sandbox 'alpha'\\''s-box'",
);
});

Expand Down
Loading
Loading