Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
116111c
refactor(cli): harden oclif bridge
cv Apr 30, 2026
5e997cb
test(cli): relax uninstall helper timeouts
cv Apr 30, 2026
88cd958
refactor(cli): migrate status and tunnel commands to oclif
cv Apr 30, 2026
3f748d6
refactor(cli): migrate debug uninstall and gateway-token to oclif
cv Apr 30, 2026
0bc877a
refactor(cli): migrate credentials commands to oclif
cv Apr 30, 2026
3fd15fc
refactor(cli): migrate sandbox inspection commands to oclif
cv Apr 30, 2026
81c62bb
refactor(cli): migrate maintenance commands to oclif
cv Apr 30, 2026
7d6a6fb
refactor(cli): migrate sandbox logs command to oclif
cv May 1, 2026
3f17a4f
refactor(cli): migrate skill install command to oclif
cv May 1, 2026
0fb3c41
refactor(cli): migrate snapshot list and create to oclif
cv May 1, 2026
d11eeee
refactor(cli): migrate shields commands to oclif
cv May 1, 2026
74806ce
refactor(cli): migrate channels mutation commands to oclif
cv May 1, 2026
e637e84
refactor(cli): migrate policy mutation commands to oclif
cv May 1, 2026
ee20dfb
refactor(cli): migrate snapshot restore command to oclif
cv May 1, 2026
8d3a568
refactor(cli): migrate destroy command to oclif
cv May 1, 2026
b447f36
refactor(cli): migrate rebuild command to oclif
cv May 1, 2026
ea850ea
merge(main): resolve snapshot restore oclif conflicts
cv May 2, 2026
876a404
Merge branch 'refactor/oclif-snapshot-restore' into refactor/oclif-de…
cv May 2, 2026
fd59cd0
merge(main): update destroy oclif branch
cv May 2, 2026
e3102e7
merge(cli): update rebuild oclif branch
cv May 2, 2026
50c3d98
merge(main): resolve rebuild oclif conflicts
cv May 2, 2026
9e16f8b
test(cli): deflake logs follow signal assertions
cv May 2, 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
2 changes: 2 additions & 0 deletions src/lib/oclif-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
UpgradeSandboxesCommand,
} from "./maintenance-cli-commands";
import { PolicyAddCommand, PolicyRemoveCommand } from "./policy-mutate-cli-commands";
import RebuildCliCommand from "./rebuild-cli-command";
import {
SandboxChannelsListCommand,
SandboxConfigGetCommand,
Expand Down Expand Up @@ -68,6 +69,7 @@ export default {
"sandbox:policy-add": PolicyAddCommand,
"sandbox:policy-list": SandboxPolicyListCommand,
"sandbox:policy-remove": PolicyRemoveCommand,
"sandbox:rebuild": RebuildCliCommand,
"sandbox:shields:down": ShieldsDownCommand,
"sandbox:shields:status": ShieldsStatusCommand,
"sandbox:shields:up": ShieldsUpCommand,
Expand Down
40 changes: 40 additions & 0 deletions src/lib/rebuild-cli-command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

/* v8 ignore start -- thin oclif adapter covered through CLI integration tests. */

import { Args, Command, Flags } from "@oclif/core";

type RuntimeBridge = {
sandboxRebuild: (sandboxName: string, args?: string[]) => Promise<void>;
};

function getRuntimeBridge(): RuntimeBridge {
return require("../nemoclaw") as RuntimeBridge;
}

export default class RebuildCliCommand extends Command {
static id = "sandbox:rebuild";
static strict = true;
static summary = "Upgrade sandbox to current agent version";
static description = "Back up, recreate, and restore a sandbox using the current agent image.";
static usage = ["<name> rebuild [--yes|--force] [--verbose|-v]"];
static args = {
sandboxName: Args.string({ name: "sandbox", description: "Sandbox name", required: true }),
};
static flags = {
help: Flags.help({ char: "h" }),
yes: Flags.boolean({ description: "Skip the confirmation prompt" }),
force: Flags.boolean({ description: "Skip the confirmation prompt" }),
verbose: Flags.boolean({ char: "v", description: "Show verbose rebuild diagnostics" }),
};

public async run(): Promise<void> {
const { args, flags } = await this.parse(RebuildCliCommand);
const legacyArgs: string[] = [];
if (flags.yes) legacyArgs.push("--yes");
if (flags.force) legacyArgs.push("--force");
if (flags.verbose) legacyArgs.push("--verbose");
await getRuntimeBridge().sandboxRebuild(args.sandboxName, legacyArgs);
}
}
7 changes: 6 additions & 1 deletion src/nemoclaw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,7 @@ exports.sandboxLogs = sandboxLogs;
exports.sandboxPolicyAdd = sandboxPolicyAdd;
exports.sandboxPolicyList = sandboxPolicyList;
exports.sandboxPolicyRemove = sandboxPolicyRemove;
exports.sandboxRebuild = sandboxRebuild;
exports.sandboxSkillInstall = sandboxSkillInstall;
exports.sandboxSnapshot = sandboxSnapshot;
exports.sandboxStatus = sandboxStatus;
Expand Down Expand Up @@ -4974,7 +4975,11 @@ const mainPromise = (async () => {
break;
}
case "rebuild":
await sandboxRebuild(cmd, actionArgs);
if (hasHelpFlag(actionArgs)) {
printSandboxActionUsage("rebuild [--yes|--force] [--verbose|-v]");
break;
}
await runOclif("sandbox:rebuild", [cmd, ...actionArgs]);
break;
case "snapshot": {
const snapshotSub = actionArgs[0];
Expand Down
63 changes: 49 additions & 14 deletions test/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import { describe, it, expect } from "vitest";
import { execSync, spawn, spawnSync } from "node:child_process";
import type { ChildProcess } from "node:child_process";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
Expand Down Expand Up @@ -55,6 +56,23 @@ function readCliErrorOutput(error: CliErrorShape | string | null | undefined): C
};
}

function normalizeChildExit(code: number | null, signal: NodeJS.Signals | null): number | null {
if (code !== null) return code;
if (signal === "SIGTERM") return 143;
if (signal === "SIGINT") return 130;
return null;
}

function waitForChildExit(child: ChildProcess): Promise<number | null> {
return new Promise((resolve) => {
child.once("exit", (code, signal) => resolve(normalizeChildExit(code, signal)));
});
}

function isChildRunning(child: ChildProcess): boolean {
return child.exitCode === null && child.signalCode === null;
}

function run(args: string): CliRunResult {
return runWithEnv(args);
}
Expand Down Expand Up @@ -1073,6 +1091,11 @@ describe("CLI dispatch", () => {
expect(destroy.out).toContain("<name> destroy [--yes|--force]");
expect(destroy.out).not.toContain("sandbox:destroy");

const rebuild = runWithEnv("alpha rebuild --help", { HOME: home });
expect(rebuild.code).toBe(0);
expect(rebuild.out).toContain("<name> rebuild [--yes|--force] [--verbose|-v]");
expect(rebuild.out).not.toContain("sandbox:rebuild");

for (const action of ["policy-add", "policy-remove", "policy-list"]) {
const policy = runWithEnv(`alpha ${action} --help`, { HOME: home });
expect(policy.code).toBe(0);
Expand Down Expand Up @@ -1352,9 +1375,7 @@ describe("CLI dispatch", () => {
env: { ...process.env, HOME: home, PATH: `${localBin}:${process.env.PATH || ""}` },
stdio: "ignore",
});
const exitPromise = new Promise<number | null>((resolve) => {
child.once("exit", (code) => resolve(code));
});
const exitPromise = waitForChildExit(child);
const readCalls = () =>
fs.existsSync(markerFile) ? fs.readFileSync(markerFile, "utf8").trim().split(/\n/) : [];

Expand All @@ -1372,11 +1393,11 @@ describe("CLI dispatch", () => {
}
await new Promise((resolve) => setTimeout(resolve, 100));
}
expect(child.exitCode).toBeNull();
expect(isChildRunning(child)).toBe(true);
expect(calls).toContain("logs alpha -n 200 --source all --tail");
expect(calls).toContain("sandbox exec -n alpha -- tail -n 200 -f /tmp/gateway.log");
} finally {
if (child.exitCode === null) {
if (isChildRunning(child)) {
child.kill("SIGTERM");
}
expect(await exitPromise).toBe(143);
Expand All @@ -1387,19 +1408,21 @@ describe("CLI dispatch", () => {
const home = fs.mkdtempSync(path.join(os.tmpdir(), "nemoclaw-cli-logs-follow-sigterm-wait-"));
const localBin = path.join(home, "bin");
const markerFile = path.join(home, "logs-follow-sigterm-wait-args");
const releaseFile = path.join(home, "release-log-children");
fs.mkdirSync(localBin, { recursive: true });
writeSandboxRegistry(home);
fs.writeFileSync(
path.join(localBin, "openshell"),
[
"#!/usr/bin/env bash",
`marker_file=${JSON.stringify(markerFile)}`,
`release_file=${JSON.stringify(releaseFile)}`,
'printf \'%s\\n\' "$*" >> "$marker_file"',
'if [ "$1" = "settings" ]; then',
" exit 0",
"fi",
'if [ "$1" = "logs" ] || [ "$1" = "sandbox" ]; then',
" trap 'sleep 0.3; exit 0' TERM INT",
" trap 'printf \"%s term-start\\n\" \"$*\" >> \"$marker_file\"; while [ ! -f \"$release_file\" ]; do sleep 0.05; done; printf \"%s term-end\\n\" \"$*\" >> \"$marker_file\"; exit 0' TERM INT",
" while true; do sleep 1; done",
"fi",
"exit 0",
Expand All @@ -1412,8 +1435,10 @@ describe("CLI dispatch", () => {
env: { ...process.env, HOME: home, PATH: `${localBin}:${process.env.PATH || ""}` },
stdio: "ignore",
});
const exitPromise = new Promise<number | null>((resolve) => {
child.once("exit", (code) => resolve(code));
let hasExited = false;
const exitPromise = waitForChildExit(child).then((code) => {
hasExited = true;
return code;
});
const readCalls = () =>
fs.existsSync(markerFile) ? fs.readFileSync(markerFile, "utf8").trim().split(/\n/) : [];
Expand All @@ -1435,14 +1460,24 @@ describe("CLI dispatch", () => {
expect(calls).toContain("logs alpha -n 200 --source all --tail");
expect(calls).toContain("sandbox exec -n alpha -- tail -n 200 -f /tmp/gateway.log");
child.kill("SIGTERM");
const exitedEarly = await Promise.race([
exitPromise.then(() => true),
new Promise<boolean>((resolve) => setTimeout(() => resolve(false), 100)),
]);
expect(exitedEarly).toBe(false);

let callsAfterTerm: string[] = [];
const termDeadline = Date.now() + Math.min(testTimeout(5_000), Math.max(1_000, testTimeout() - 5_000));
while (Date.now() < termDeadline) {
callsAfterTerm = readCalls();
if (callsAfterTerm.some((call) => call.endsWith("term-start")) || hasExited) {
break;
}
await new Promise((resolve) => setTimeout(resolve, 50));
}

expect(callsAfterTerm.some((call) => call.endsWith("term-start"))).toBe(true);
expect(hasExited).toBe(false);
fs.writeFileSync(releaseFile, "1");
expect(await exitPromise).toBe(143);
} finally {
if (child.exitCode === null) {
fs.writeFileSync(releaseFile, "1");
if (isChildRunning(child)) {
child.kill("SIGKILL");
}
}
Expand Down
Loading