Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
31 changes: 30 additions & 1 deletion src/lib/cli/public-dispatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,17 +267,46 @@ function printGlobalStatusScopeHint(sandboxName: string, args: readonly string[]
process.exit(2);
}

/**
* Report the sandbox-first grammar for a first token that names a sandbox action.
*
* `nemoclaw doctor` and `nemoclaw policy list` name an action, not a sandbox.
* Reporting a missing sandbox sends the reader to `onboard` for a sandbox they
* never asked for (#10212).
*/
function printSandboxScopeHint(action: string, remainingArgs: readonly string[]): never {
const argSuffix = remainingArgs.length > 0 ? ` ${remainingArgs.join(" ")}` : "";
console.error(` '${action}' is a sandbox command. It needs a sandbox name.`);
console.error("");
console.error(` Run: ${CLI_NAME} <name> ${action}${argSuffix}`);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
const allNames = registeredSandboxNames();
if (allNames.length > 0) {
console.error(` Registered sandboxes: ${allNames.join(", ")}`);
console.error(` Run '${CLI_NAME} list' to see all sandboxes.`);
} else {
console.error(` Run '${CLI_NAME} onboard' to create one.`);
}
process.exit(1);
}

async function recoverRequestedSandboxIfNeeded(
sandboxName: string,
action: string,
rawArgsAfterSandboxName: string[],
): Promise<void> {
if (registry().getSandbox(sandboxName) || !isKnownSandboxAction(action)) return;
if (registry().getSandbox(sandboxName)) return;
const namesSandboxAction = isKnownSandboxAction(sandboxName);
if (!namesSandboxAction && !isKnownSandboxAction(action)) return;

validateName(sandboxName, "sandbox name");
await registryRecovery().recoverRegistryEntries({ requestedSandboxName: sandboxName });
if (registry().getSandbox(sandboxName)) return;

// Recovery runs first so a live sandbox named after an action stays reachable
// through the name-first grammar. A token that recovery cannot resolve is a
// scope error, not a missing sandbox.
if (namesSandboxAction) printSandboxScopeHint(sandboxName, rawArgsAfterSandboxName);

if (rawArgsAfterSandboxName.length === 0) {
const suggestion = suggestGlobalCommand(sandboxName);
if (suggestion) {
Expand Down
100 changes: 100 additions & 0 deletions test/cli/dispatch-basics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,106 @@ describe("CLI dispatch", () => {
);
});

it("reports the sandbox-first grammar for a bare sandbox action (#10212)", async () => {
await withDirectPublicDispatch(async ({ dispatchCli, exitSpy, stderr }) => {
await expect(dispatchCli(["doctor"])).rejects.toThrow("process.exit:1");

const output = stderr.join("\n");
expect(output).toContain("'doctor' is a sandbox command. It needs a sandbox name.");
expect(output).toContain("Run: nemoclaw <name> doctor");
expect(output).toContain("Run 'nemoclaw onboard' to create one.");
expect(output).not.toContain("Sandbox 'doctor' does not exist.");
expect(exitSpy).toHaveBeenCalledWith(1);
});
});

it("carries trailing arguments into the sandbox-first grammar hint (#10212)", async () => {
await withDirectPublicDispatch(async ({ dispatchCli, stderr }) => {
await expect(dispatchCli(["doctor", "--json"])).rejects.toThrow("process.exit:1");

expect(stderr.join("\n")).toContain("Run: nemoclaw <name> doctor --json");
});
});

it("reports the sandbox-first grammar for a two-token sandbox action (#10212)", async () => {
await withDirectPublicDispatch(async ({ dispatchCli, stderr }) => {
await expect(dispatchCli(["policy", "list"])).rejects.toThrow("process.exit:1");

const output = stderr.join("\n");
expect(output).toContain("'policy' is a sandbox command. It needs a sandbox name.");
expect(output).toContain("Run: nemoclaw <name> policy list");
expect(output).not.toContain("Unknown command: policy");
});
});

it("lists registered sandboxes in the sandbox-first grammar hint (#10212)", async () => {
await withDirectPublicDispatch(
async ({ dispatchCli, stderr }) => {
await expect(dispatchCli(["doctor"])).rejects.toThrow("process.exit:1");

const output = stderr.join("\n");
expect(output).toContain("Registered sandboxes: alpha, beta");
expect(output).not.toContain("Run 'nemoclaw onboard' to create one.");
},
{ sandboxNames: ["alpha", "beta"] },
);
});

it("does not suggest an unrelated global command for a sandbox action (#10212)", async () => {
await withDirectPublicDispatch(async ({ dispatchCli, stderr }) => {
await expect(dispatchCli(["share"])).rejects.toThrow("process.exit:1");

const output = stderr.join("\n");
expect(output).toContain("'share' is a sandbox command. It needs a sandbox name.");
expect(output).not.toContain("Did you mean: nemoclaw start?");
});
});

it("prefers the sandbox-action report over a near-match global suggestion (#10212)", async () => {
await withDirectPublicDispatch(async ({ dispatchCli, stderr }) => {
await expect(dispatchCli(["agent"])).rejects.toThrow("process.exit:1");

// `agent` names a sandbox action and `agents` is a separate global
// command. An exact action-token match is more accurate than an
// edit-distance guess, so the scope report replaces the suggestion.
const output = stderr.join("\n");
expect(output).toContain("'agent' is a sandbox command. It needs a sandbox name.");
expect(output).not.toContain("Did you mean: nemoclaw agents?");
});
});

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

expect(runOclifCommandById).toHaveBeenCalledWith(
"sandbox:status",
["doctor"],
expect.anything(),
);
expect(stderr.join("\n")).not.toContain("is a sandbox command");
},
{ sandboxNames: ["doctor"] },
);
});

it("recovers a live sandbox named after an action before reporting scope (#10212)", async () => {
await withDirectPublicDispatch(
async ({ dispatchCli, recoverRegistryEntries, sandboxes, stderr }) => {
recoverRegistryEntries.mockImplementation(async () => {
sandboxes.set("doctor", { name: "doctor" });
return { sandboxes: [...sandboxes.values()], defaultSandbox: null };
});

await dispatchCli(["doctor", "status"]);

expect(recoverRegistryEntries).toHaveBeenCalledTimes(1);
expect(stderr.join("\n")).not.toContain("is a sandbox command");
},
);
});

it("does not suggest a route-only reservation in the connect command-order hint (#8801)", async () => {
await withDirectPublicDispatch(
async ({ dispatchCli, exitSpy, stderr }) => {
Expand Down
Loading