Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
4b7a4a2
refactor(cli): extract sandbox live state helpers
cv May 2, 2026
2ce87dd
refactor(cli): extract sandbox skill install action
cv May 2, 2026
4cd3cf3
refactor(cli): extract sandbox connect action
cv May 3, 2026
e9dd46e
refactor(cli): extract sandbox status action
cv May 3, 2026
aab6c86
refactor(cli): extract sandbox doctor action
cv May 3, 2026
8908521
refactor(cli): extract sandbox destroy action
cv May 3, 2026
56e4f05
refactor(cli): extract sandbox rebuild action
cv May 3, 2026
8bf1958
refactor(cli): extract upgrade sandboxes action
cv May 3, 2026
38eb84d
refactor(cli): remove runtime bridge
cv May 3, 2026
b2ad5da
refactor(cli): remove legacy dispatch fallbacks
cv May 3, 2026
edd2650
refactor(cli): expose explicit main entrypoint
cv May 3, 2026
a15da95
refactor(cli): add oclif examples for utility commands
cv May 3, 2026
4f57ebb
refactor(cli): validate logs flags with oclif
cv May 3, 2026
8b2d077
refactor(cli): improve sandbox diagnostic command metadata
cv May 3, 2026
a09cc51
refactor(cli): tighten policy and channel parser validation
cv May 3, 2026
75857dc
refactor(cli): improve snapshot command metadata
cv May 3, 2026
11c0676
refactor(cli): require skill install path in oclif
cv May 3, 2026
05f9eca
refactor(cli): add lifecycle confirmation flag aliases
cv May 3, 2026
4ebeae4
refactor(cli): split share into oclif subcommands
cv May 3, 2026
7d72437
Revert "refactor(cli): split share into oclif subcommands"
cv May 3, 2026
0ee5ca5
refactor(cli): split share into oclif subcommands
cv May 3, 2026
928219c
refactor(cli): model debug flags with oclif
cv May 3, 2026
36cce5e
refactor(cli): model onboard flags with oclif
cv May 3, 2026
702afb1
docs: sync oclif UX command reference
cv May 3, 2026
3224350
refactor(cli): extract public argv normalizer
cv May 3, 2026
4efce12
merge(main): reconcile argv normalizer
cv May 5, 2026
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
93 changes: 93 additions & 0 deletions src/lib/cli-argv-normalizer.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

import { describe, expect, it } from "vitest";

import { normalizeArgv, suggestCommand } from "./cli-argv-normalizer";

const globalCommands = new Set(["list", "status", "onboard", "--version"]);
const isConnectFlag = (arg: string | undefined) => arg === "--probe-only" || arg === "--help";

describe("normalizeArgv", () => {
it("normalizes root help aliases", () => {
expect(normalizeArgv([], { globalCommands, isSandboxConnectFlag: isConnectFlag })).toEqual({
kind: "rootHelp",
});
expect(normalizeArgv(["--help"], { globalCommands, isSandboxConnectFlag: isConnectFlag })).toEqual({
kind: "rootHelp",
});
});

it("normalizes internal dump commands", () => {
expect(
normalizeArgv(["--dump-commands"], { globalCommands, isSandboxConnectFlag: isConnectFlag }),
).toEqual({ kind: "dumpCommands" });
});

it("normalizes global commands", () => {
expect(
normalizeArgv(["list", "--json"], { globalCommands, isSandboxConnectFlag: isConnectFlag }),
).toEqual({ kind: "global", command: "list", args: ["--json"] });
});

it("normalizes explicit sandbox actions", () => {
expect(
normalizeArgv(["alpha", "status"], { globalCommands, isSandboxConnectFlag: isConnectFlag }),
).toEqual({
kind: "sandbox",
sandboxName: "alpha",
action: "status",
actionArgs: [],
connectHelpRequested: false,
});
});

it("normalizes bare and implicit connect invocations", () => {
expect(
normalizeArgv(["alpha"], { globalCommands, isSandboxConnectFlag: isConnectFlag }),
).toEqual({
kind: "sandbox",
sandboxName: "alpha",
action: "connect",
actionArgs: [],
connectHelpRequested: false,
});
expect(
normalizeArgv(["alpha", "--probe-only"], {
globalCommands,
isSandboxConnectFlag: isConnectFlag,
}),
).toEqual({
kind: "sandbox",
sandboxName: "alpha",
action: "connect",
actionArgs: ["--probe-only"],
connectHelpRequested: false,
});
});

it("tracks connect help requests", () => {
expect(
normalizeArgv(["alpha", "connect", "--help"], {
globalCommands,
isSandboxConnectFlag: isConnectFlag,
}),
).toMatchObject({
kind: "sandbox",
sandboxName: "alpha",
action: "connect",
actionArgs: ["--help"],
connectHelpRequested: true,
});
});
});

describe("suggestCommand", () => {
it("suggests close global command typos", () => {
expect(suggestCommand("liost", globalCommands)).toBe("list");
});

it("ignores flag-like commands", () => {
expect(suggestCommand("version", globalCommands)).toBeNull();
});
});
88 changes: 88 additions & 0 deletions src/lib/cli-argv-normalizer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

export type NormalizedRootHelpArgv = { kind: "rootHelp" };
export type NormalizedDumpCommandsArgv = { kind: "dumpCommands" };
export type NormalizedGlobalArgv = { kind: "global"; command: string; args: string[] };
export type NormalizedSandboxArgv = {
kind: "sandbox";
sandboxName: string;
action: string;
actionArgs: string[];
connectHelpRequested: boolean;
};

export type NormalizedArgv =
| NormalizedRootHelpArgv
| NormalizedDumpCommandsArgv
| NormalizedGlobalArgv
| NormalizedSandboxArgv;

export type NormalizeArgvOptions = {
globalCommands: ReadonlySet<string>;
isSandboxConnectFlag: (arg: string | undefined) => boolean;
};

export function normalizeArgv(argv: readonly string[], opts: NormalizeArgvOptions): NormalizedArgv {
const [cmd, ...args] = argv;

if (!cmd || cmd === "help" || cmd === "--help" || cmd === "-h") {
return { kind: "rootHelp" };
}

if (cmd === "--dump-commands") {
return { kind: "dumpCommands" };
}

if (opts.globalCommands.has(cmd)) {
return { kind: "global", command: cmd, args };
}

const firstSandboxArg = args[0];
const implicitConnectArg = opts.isSandboxConnectFlag(firstSandboxArg);
const action = !firstSandboxArg || implicitConnectArg ? "connect" : firstSandboxArg;
const actionArgs = !firstSandboxArg || implicitConnectArg ? args : args.slice(1);

return {
kind: "sandbox",
sandboxName: cmd,
action,
actionArgs,
connectHelpRequested:
action === "connect" && actionArgs.some((arg) => arg === "--help" || arg === "-h"),
};
}

function editDistance(left: string, right: string): number {
const rows = left.length + 1;
const cols = right.length + 1;
const matrix: number[][] = Array.from({ length: rows }, () => Array(cols).fill(0));
for (let i = 0; i < rows; i++) matrix[i][0] = i;
for (let j = 0; j < cols; j++) matrix[0][j] = j;
for (let i = 1; i < rows; i++) {
for (let j = 1; j < cols; j++) {
const cost = left[i - 1] === right[j - 1] ? 0 : 1;
matrix[i][j] = Math.min(
matrix[i - 1][j] + 1,
matrix[i][j - 1] + 1,
matrix[i - 1][j - 1] + cost,
);
}
}
return matrix[left.length][right.length];
}

export function suggestCommand(token: string, commands: Iterable<string>): string | null {
let best: { command: string; distance: number } | null = null;
for (const command of commands) {
if (command.startsWith("-")) continue;
const distance = editDistance(token, command);
if (!best || distance < best.distance) {
best = { command, distance };
}
}
if (!best) return null;
if (best.distance <= 1) return best.command;
if (token.length >= 5 && best.distance <= 2) return best.command;
return null;
}
64 changes: 15 additions & 49 deletions src/nemoclaw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ const {
globalCommandTokens,
sandboxActionTokens,
} = require("./lib/command-registry");
import { normalizeArgv, suggestCommand } from "./lib/cli-argv-normalizer";
import { OPENSHELL_PROBE_TIMEOUT_MS } from "./lib/openshell-timeouts";
import {
resolveGlobalOclifDispatch,
Expand Down Expand Up @@ -148,38 +149,8 @@ function printSandboxActionUsage(action: string): void {

// ── Dispatch helpers ─────────────────────────────────────────────

function editDistance(left: string, right: string): number {
const rows = left.length + 1;
const cols = right.length + 1;
const matrix: number[][] = Array.from({ length: rows }, () => Array(cols).fill(0));
for (let i = 0; i < rows; i++) matrix[i][0] = i;
for (let j = 0; j < cols; j++) matrix[0][j] = j;
for (let i = 1; i < rows; i++) {
for (let j = 1; j < cols; j++) {
const cost = left[i - 1] === right[j - 1] ? 0 : 1;
matrix[i][j] = Math.min(
matrix[i - 1][j] + 1,
matrix[i][j - 1] + 1,
matrix[i - 1][j - 1] + cost,
);
}
}
return matrix[left.length][right.length];
}

function suggestGlobalCommand(token: string): string | null {
let best: { command: string; distance: number } | null = null;
for (const command of GLOBAL_COMMANDS) {
if (command.startsWith("-")) continue;
const distance = editDistance(token, command);
if (!best || distance < best.distance) {
best = { command, distance };
}
}
if (!best) return null;
if (best.distance <= 1) return best.command;
if (token.length >= 5 && best.distance <= 2) return best.command;
return null;
return suggestCommand(token, GLOBAL_COMMANDS);
}

function findRegisteredSandboxName(tokens: string[]): string | null {
Expand Down Expand Up @@ -256,36 +227,31 @@ async function runDispatchResult(

// eslint-disable-next-line complexity
async function main(argv: string[] = process.argv.slice(2)): Promise<void> {
const [cmd, ...args] = argv;
const normalized = normalizeArgv(argv, {
globalCommands: GLOBAL_COMMANDS,
isSandboxConnectFlag,
});

// No command → help
if (!cmd || cmd === "help" || cmd === "--help" || cmd === "-h") {
if (normalized.kind === "rootHelp") {
await runOclif("root:help", []);
return;
}

// Internal developer flag — dump canonical command list for check-docs.sh parity checks
if (cmd === "--dump-commands") {
if (normalized.kind === "dumpCommands") {
canonicalUsageList().forEach((c: string) => console.log(c));
return;
}

// Global commands
if (GLOBAL_COMMANDS.has(cmd)) {
await runDispatchResult(resolveGlobalOclifDispatch(cmd, args));
if (normalized.kind === "global") {
await runDispatchResult(resolveGlobalOclifDispatch(normalized.command, normalized.args));
return;
}

// Sandbox-scoped commands: nemoclaw <name> <action>
const firstSandboxArg = args[0];
const implicitConnectArg = isSandboxConnectFlag(firstSandboxArg);
const requestedSandboxAction =
!firstSandboxArg || implicitConnectArg ? "connect" : firstSandboxArg;
const requestedSandboxActionArgs = !firstSandboxArg || implicitConnectArg ? args : args.slice(1);
if (
requestedSandboxAction === "connect" &&
requestedSandboxActionArgs.some((arg) => arg === "--help" || arg === "-h")
) {
const cmd = normalized.sandboxName;
const args = argv.slice(1);
const requestedSandboxAction = normalized.action;
const requestedSandboxActionArgs = normalized.actionArgs;
if (normalized.connectHelpRequested) {
validateName(cmd, "sandbox name");
printSandboxConnectHelp(cmd);
return;
Expand Down
Loading