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
6 changes: 6 additions & 0 deletions docs/reference/commands.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,11 @@ $$nemoclaw deploy <instance-name>
### `$$nemoclaw <name> connect`

Connect to a sandbox by name.
Bare `$$nemoclaw connect` (no sandbox name) connects to the registry default.
NemoClaw uses the stored default when it names a non-pending registered sandbox, then falls back to the first non-pending registration.
If only pending registrations remain, the command exits non-zero and tells you to wait for onboarding or remove the incomplete sandbox.
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.

Expand Down Expand Up @@ -823,6 +828,7 @@ You no longer need to re-run `$$nemoclaw onboard` after a reboot in this case.

```bash
$$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.
Expand Down
81 changes: 81 additions & 0 deletions src/lib/cli/bare-connect-routing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

import type { NormalizedSandboxArgv } from "./argv-normalizer";
import { CLI_NAME } from "./branding";

type SandboxSummary = {
name: string;
pendingRouteReservation?: true;
};

export type BareConnectRoutingDeps = {
findRegisteredSandboxName: (tokens: string[]) => string | null;
getDefault: () => string | null;
getSandbox: (name: string) => unknown | null;
listSandboxes: () => { sandboxes: SandboxSummary[] };
printConnectOrderHint: (candidate: string | null) => void;
printSandboxConnectHelp: (sandboxName?: string) => void;
recoverRegistryEntries: () => Promise<unknown>;
};

/**
* Route a bare `connect` invocation to the default sandbox.
*
* Returns the (possibly rewritten) argv to dispatch, or `null` when the
* invocation was fully handled here (bare `connect --help`). A registered
* sandbox literally named `connect` keeps the historical name-first grammar.
*/
export async function resolveBareConnectArgv(
normalized: NormalizedSandboxArgv,
deps: BareConnectRoutingDeps,
): Promise<NormalizedSandboxArgv | null> {
if (normalized.sandboxName !== "connect" || deps.getSandbox("connect")) {
return normalized;
}

if (normalized.action !== "connect") {
const reorderedCandidate = deps.findRegisteredSandboxName([normalized.action]);
if (reorderedCandidate) {
deps.printConnectOrderHint(reorderedCandidate);
process.exit(1);
}
return normalized;
}

if (normalized.connectHelpRequested) {
deps.printSandboxConnectHelp(deps.getDefault() ?? undefined);
return null;
}

const defaultName = await resolveDefaultSandboxForBareConnect(deps);
if (!defaultName) printBareConnectWithoutDefault(deps);
return { ...normalized, sandboxName: defaultName };
}

async function resolveDefaultSandboxForBareConnect(
deps: BareConnectRoutingDeps,
): Promise<string | null> {
const currentDefault = deps.getDefault();
if (currentDefault) return currentDefault;

await deps.recoverRegistryEntries();
return deps.getDefault();
}

function printBareConnectWithoutDefault(deps: BareConnectRoutingDeps): never {
const pendingNames = deps
.listSandboxes()
.sandboxes.filter((sandbox) => sandbox.pendingRouteReservation === true)
.map((sandbox) => sandbox.name);

console.error(` '${CLI_NAME} connect' could not resolve a ready default sandbox.`);
console.error("");
if (pendingNames.length > 0) {
console.error(` Sandbox setup is still pending: ${pendingNames.join(", ")}`);
console.error(" Wait for onboarding to finish or remove the incomplete sandbox.");
} else {
console.error(` Run '${CLI_NAME} onboard' to create one.`);
}
process.exit(1);
}
21 changes: 16 additions & 5 deletions src/lib/cli/public-dispatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
normalizeArgv,
suggestCommand,
} from "./argv-normalizer";
import { resolveBareConnectArgv } from "./bare-connect-routing";
import { getRegisteredOclifCommandMetadata } from "./oclif-metadata";
import {
type PublicTranslationResult,
Expand Down Expand Up @@ -358,17 +359,27 @@ async function dispatchSandboxArgv(
normalized: NormalizedSandboxArgv,
argv: string[],
): Promise<void> {
const cmd = normalized.sandboxName;
const routed = await resolveBareConnectArgv(normalized, {
findRegisteredSandboxName,
getDefault: () => registry().getDefault(),
getSandbox: (name) => registry().getSandbox(name),
listSandboxes: () => registry().listSandboxes(),
printConnectOrderHint,
printSandboxConnectHelp: (sandboxName) => sandboxConnect().printSandboxConnectHelp(sandboxName),
recoverRegistryEntries: () => registryRecovery().recoverRegistryEntries(),
});
if (!routed) return;
const cmd = routed.sandboxName;
const rawArgsAfterCmd = argv.slice(1);
const requestedSandboxAction = normalized.action;
const requestedSandboxActionArgs = normalized.actionArgs;
if (handlePublicConnectHelp(normalized)) return;
const requestedSandboxAction = routed.action;
const requestedSandboxActionArgs = routed.actionArgs;
if (handlePublicConnectHelp(routed)) return;

// Help is parser metadata, not sandbox runtime behavior. Render sandbox-scoped
// public help before registry recovery so `nemoclaw missing channels start --help`
// stays side-effect free and never starts or repairs services.
if (
!normalized.connectHelpRequested &&
!routed.connectHelpRequested &&
isKnownSandboxAction(requestedSandboxAction) &&
hasPublicSandboxHelpFlag(requestedSandboxAction, requestedSandboxActionArgs)
) {
Expand Down
118 changes: 117 additions & 1 deletion test/cli/dispatch-basics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ describe("CLI dispatch", () => {
let code = 0;
let out = "";
try {
execSync(`node "${CLI}" alpha connect 2>&1`, {
execSync(`node "${CLI}" connect 2>&1`, {
encoding: "utf-8",
stdio: "pipe",
timeout: execTimeout(),
Expand Down Expand Up @@ -446,6 +446,122 @@ describe("CLI dispatch", () => {
);
});

it("connects to the default sandbox when connect is invoked without a sandbox name (#6627)", async () => {
await withDirectPublicDispatch(
async ({ dispatchCli, recoverRegistryEntries, runOclifCommandById }) => {
await dispatchCli(["connect"]);

expect(runOclifCommandById).toHaveBeenCalledWith(
"sandbox:connect",
["dcode-managed"],
expect.anything(),
);
expect(recoverRegistryEntries).not.toHaveBeenCalled();
},
{ sandboxNames: ["dcode-managed"], defaultSandbox: "dcode-managed" },
);
});

it("forwards connect flags to the default sandbox on a bare connect invocation (#6627)", async () => {
await withDirectPublicDispatch(
async ({ dispatchCli, runOclifCommandById }) => {
await dispatchCli(["connect", "--probe-only"]);

expect(runOclifCommandById).toHaveBeenCalledWith(
"sandbox:connect",
["dcode-managed", "--probe-only"],
expect.anything(),
);
},
{
sandboxNames: ["dcode-managed"],
defaultSandbox: "dcode-managed",
connectFlags: ["--probe-only"],
},
);
});

it("uses the first non-pending sandbox when no stored default is valid (#6627)", async () => {
await withDirectPublicDispatch(
async ({ dispatchCli, recoverRegistryEntries, runOclifCommandById }) => {
await dispatchCli(["connect"]);

expect(recoverRegistryEntries).not.toHaveBeenCalled();
expect(runOclifCommandById).toHaveBeenCalledWith(
"sandbox:connect",
["alpha"],
expect.anything(),
);
},
{
sandboxNames: ["pending", "alpha", "beta"],
defaultSandbox: "missing",
pendingSandboxNames: ["pending"],
},
);
});

it("waits for pending-only sandbox setup instead of suggesting it as a default (#6627)", async () => {
await withDirectPublicDispatch(
async ({ dispatchCli, exitSpy, recoverRegistryEntries, stderr }) => {
await expect(dispatchCli(["connect"])).rejects.toThrow("process.exit:1");

const output = stderr.join("\n");
expect(recoverRegistryEntries).toHaveBeenCalledTimes(1);
expect(output).toContain("'nemoclaw connect' could not resolve a ready default sandbox.");
expect(output).toContain("Sandbox setup is still pending: alpha");
expect(output).toContain("Wait for onboarding to finish or remove the incomplete sandbox.");
expect(output).not.toContain("nemoclaw use");
expect(exitSpy).toHaveBeenCalledWith(1);
},
{ sandboxNames: ["alpha"], pendingSandboxNames: ["alpha"] },
);
});

it("points bare connect at onboarding when no sandboxes are registered (#6627)", async () => {
await withDirectPublicDispatch(
async ({ dispatchCli, exitSpy, recoverRegistryEntries, stderr }) => {
await expect(dispatchCli(["connect"])).rejects.toThrow("process.exit:1");

const output = stderr.join("\n");
expect(recoverRegistryEntries).toHaveBeenCalledTimes(1);
expect(output).toContain("'nemoclaw connect' could not resolve a ready default sandbox.");
expect(output).toContain("Run 'nemoclaw onboard' to create one.");
expect(exitSpy).toHaveBeenCalledWith(1);
},
);
});

it("keeps the name-first grammar for a sandbox literally named connect (#6627)", async () => {
await withDirectPublicDispatch(
async ({ dispatchCli, getDefault, runOclifCommandById }) => {
await dispatchCli(["connect"]);

expect(runOclifCommandById).toHaveBeenCalledWith(
"sandbox:connect",
["connect"],
expect.anything(),
);
expect(getDefault).not.toHaveBeenCalled();
},
{ sandboxNames: ["connect"], defaultSandbox: null },
);
});

it("explains connect command order when only the sandbox name follows connect (#6627)", async () => {
await withDirectPublicDispatch(
async ({ dispatchCli, exitSpy, stderr }) => {
await expect(dispatchCli(["connect", "alpha"])).rejects.toThrow("process.exit:1");

const output = stderr.join("\n");
expect(output).toContain("Command order is: nemoclaw <sandbox-name> connect");
expect(output).toContain("Did you mean: nemoclaw alpha connect?");
expect(exitSpy).toHaveBeenCalledWith(1);
},
{ sandboxNames: ["alpha"] },
);
});

it("suggests the closest registered sandbox name for a mistyped sandbox action", async () => {
await withDirectPublicDispatch(
async ({ dispatchCli, exitSpy, stderr }) => {
Expand Down
23 changes: 9 additions & 14 deletions test/e2e/live/sandbox-rlimits-connect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,11 @@ function expectNoRlimitStartupDiagnostics(text: string): void {
);
}

function connectAcceptanceScript(cliPath: string, sandboxName: string): string {
function connectAcceptanceScript(cliPath: string): string {
const cli = JSON.stringify(cliPath);
const sandbox = JSON.stringify(sandboxName);
return [
"set -euo pipefail",
`cat <<'NEMOCLAW_CONNECT_RLIMITS' | ${cli} ${sandbox} connect`,
`cat <<'NEMOCLAW_CONNECT_RLIMITS' | ${cli} connect`,
"set -euo pipefail",
'printf "__NEMOCLAW_RLIMIT_CONNECT_BEGIN__\\n"',
'bash -lc \'printf "login_nproc=%s\\nlogin_nofile=%s\\n" "$(ulimit -u)" "$(ulimit -n)"; ulimit -a\'',
Expand Down Expand Up @@ -60,7 +59,7 @@ runConnectRlimitTest(
sandboxName: SANDBOX_NAME,
acceptancePath: [
"nemoclaw onboard --non-interactive --yes-i-accept-third-party-software",
`nemoclaw ${SANDBOX_NAME} connect`,
"nemoclaw connect (routes through the onboarded default sandbox)",
"bash -lc 'ulimit -u; ulimit -n'",
"bash -ic 'ulimit -u; ulimit -n'",
"ulimit -a",
Expand Down Expand Up @@ -109,16 +108,12 @@ runConnectRlimitTest(
);
expect(onboard.exitCode, resultText(onboard)).toBe(0);

const connect = await host.command(
"bash",
["-lc", connectAcceptanceScript(host.commandPath, SANDBOX_NAME)],
{
artifactName: "phase-2-connect-rlimits",
env: buildAvailabilityProbeEnv(),
redactionValues,
timeoutMs: 10 * 60_000,
},
);
const connect = await host.command("bash", ["-lc", connectAcceptanceScript(host.commandPath)], {
artifactName: "phase-2-connect-rlimits",
env: buildAvailabilityProbeEnv(),
redactionValues,
timeoutMs: 10 * 60_000,
});
const output = resultText(connect);
await artifacts.writeText("connect-rlimits-output.txt", output);
expect(connect.exitCode, output).toBe(0);
Expand Down
8 changes: 8 additions & 0 deletions test/e2e/mock-parity.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@
"src/lib/onboard/sandbox-create-plan-extra-providers.test.ts",
"test/onboard-extra-provider-reconciliation.test.ts"
]
},
{
"live": "test/e2e/live/sandbox-rlimits-connect.test.ts",
"fast": [
"test/cli/dispatch-basics.test.ts",
"test/e2e/support/rlimit-connect-workflow-boundary.test.ts",
"test/sandbox-rlimit-hooks.test.ts"
]
}
]
}
Loading
Loading