diff --git a/src/lib/destroy-cli-command.ts b/src/lib/destroy-cli-command.ts index 2a764e25700..9a8f23a977f 100644 --- a/src/lib/destroy-cli-command.ts +++ b/src/lib/destroy-cli-command.ts @@ -3,11 +3,12 @@ /* v8 ignore start -- thin oclif adapter covered through CLI integration tests. */ -import { Args, Command, Flags } from "@oclif/core"; +import { Args, Flags } from "@oclif/core"; +import { NemoClawCommand } from "./nemoclaw-oclif-command"; import { destroySandbox } from "./sandbox-runtime-actions"; -export default class DestroyCliCommand extends Command { +export default class DestroyCliCommand extends NemoClawCommand { static id = "sandbox:destroy"; static strict = true; static summary = "Stop NIM and delete sandbox"; @@ -18,16 +19,15 @@ export default class DestroyCliCommand extends Command { sandboxName: Args.string({ name: "sandbox", description: "Sandbox name", required: true }), }; static flags = { - help: Flags.help({ char: "h" }), yes: Flags.boolean({ char: "y", description: "Skip the confirmation prompt" }), force: Flags.boolean({ description: "Skip the confirmation prompt" }), }; public async run(): Promise { const { args, flags } = await this.parse(DestroyCliCommand); - const legacyArgs: string[] = []; - if (flags.yes) legacyArgs.push("--yes"); - if (flags.force) legacyArgs.push("--force"); - await destroySandbox(args.sandboxName, legacyArgs); + await destroySandbox(args.sandboxName, { + force: flags.force === true, + yes: flags.yes === true, + }); } } diff --git a/src/lib/global-cli-actions.ts b/src/lib/global-cli-actions.ts index e7f0c9e9492..eef7540fc7d 100644 --- a/src/lib/global-cli-actions.ts +++ b/src/lib/global-cli-actions.ts @@ -4,6 +4,10 @@ /* v8 ignore start -- transitional action facade until implementations leave src/nemoclaw.ts. */ import { runDeployAction as executeDeployAction } from "./deploy-action"; +import { + type GarbageCollectImagesOptions, + type UpgradeSandboxesOptions, +} from "./lifecycle-options"; import { backupAll as executeBackupAllAction, garbageCollectImages as executeGarbageCollectImagesAction, @@ -52,15 +56,19 @@ export function runBackupAllAction(): void { executeBackupAllAction(); } -export async function runUpgradeSandboxesAction(args: string[] = []): Promise { +export async function runUpgradeSandboxesAction( + options: string[] | UpgradeSandboxesOptions = {}, +): Promise { const { upgradeSandboxes } = require("./upgrade-sandboxes-action") as { - upgradeSandboxes: (args?: string[]) => Promise; + upgradeSandboxes: (options?: string[] | UpgradeSandboxesOptions) => Promise; }; - await upgradeSandboxes(args); + await upgradeSandboxes(options); } -export async function runGarbageCollectImagesAction(args: string[] = []): Promise { - await executeGarbageCollectImagesAction(args); +export async function runGarbageCollectImagesAction( + options: string[] | GarbageCollectImagesOptions = {}, +): Promise { + await executeGarbageCollectImagesAction(options); } export function showRootHelp(): void { diff --git a/src/lib/lifecycle-options.test.ts b/src/lib/lifecycle-options.test.ts new file mode 100644 index 00000000000..52e9cb63ff1 --- /dev/null +++ b/src/lib/lifecycle-options.test.ts @@ -0,0 +1,51 @@ +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +import { describe, expect, it } from "vitest"; + +import { + normalizeDestroySandboxOptions, + normalizeGarbageCollectImagesOptions, + normalizeRebuildSandboxOptions, + normalizeUpgradeSandboxesOptions, +} from "./lifecycle-options"; + +describe("lifecycle option normalization", () => { + it("preserves typed destroy options and still accepts compatibility argv", () => { + expect(normalizeDestroySandboxOptions({ yes: true })).toEqual({ yes: true }); + expect(normalizeDestroySandboxOptions(["--yes", "--force"])).toEqual({ + force: true, + yes: true, + }); + }); + + it("preserves typed rebuild options and still accepts compatibility argv", () => { + expect(normalizeRebuildSandboxOptions({ verbose: true, yes: true })).toEqual({ + verbose: true, + yes: true, + }); + expect(normalizeRebuildSandboxOptions(["-v", "--force"])).toEqual({ + force: true, + verbose: true, + yes: false, + }); + }); + + it("preserves typed maintenance options and still accepts compatibility argv", () => { + expect(normalizeUpgradeSandboxesOptions({ auto: true, yes: true })).toEqual({ + auto: true, + yes: true, + }); + expect(normalizeUpgradeSandboxesOptions(["--check", "--yes"])).toEqual({ + auto: false, + check: true, + yes: true, + }); + expect(normalizeGarbageCollectImagesOptions({ dryRun: true })).toEqual({ dryRun: true }); + expect(normalizeGarbageCollectImagesOptions(["--dry-run", "--force"])).toEqual({ + dryRun: true, + force: true, + yes: false, + }); + }); +}); diff --git a/src/lib/lifecycle-options.ts b/src/lib/lifecycle-options.ts new file mode 100644 index 00000000000..d3a84757a30 --- /dev/null +++ b/src/lib/lifecycle-options.ts @@ -0,0 +1,76 @@ +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +export interface DestroySandboxOptions { + force?: boolean; + yes?: boolean; +} + +export interface RebuildSandboxOptions { + force?: boolean; + verbose?: boolean; + yes?: boolean; +} + +export interface GarbageCollectImagesOptions { + dryRun?: boolean; + force?: boolean; + yes?: boolean; +} + +export interface UpgradeSandboxesOptions { + auto?: boolean; + check?: boolean; + yes?: boolean; +} + +export function normalizeDestroySandboxOptions( + options: string[] | DestroySandboxOptions = {}, +): DestroySandboxOptions { + if (Array.isArray(options)) { + return { + force: options.includes("--force"), + yes: options.includes("--yes"), + }; + } + return options; +} + +export function normalizeRebuildSandboxOptions( + options: string[] | RebuildSandboxOptions = {}, +): RebuildSandboxOptions { + if (Array.isArray(options)) { + return { + force: options.includes("--force"), + verbose: options.includes("--verbose") || options.includes("-v"), + yes: options.includes("--yes"), + }; + } + return options; +} + +export function normalizeGarbageCollectImagesOptions( + options: string[] | GarbageCollectImagesOptions = {}, +): GarbageCollectImagesOptions { + if (Array.isArray(options)) { + return { + dryRun: options.includes("--dry-run"), + force: options.includes("--force"), + yes: options.includes("--yes"), + }; + } + return options; +} + +export function normalizeUpgradeSandboxesOptions( + options: string[] | UpgradeSandboxesOptions = {}, +): UpgradeSandboxesOptions { + if (Array.isArray(options)) { + return { + auto: options.includes("--auto"), + check: options.includes("--check"), + yes: options.includes("--yes"), + }; + } + return options; +} diff --git a/src/lib/maintenance-actions.ts b/src/lib/maintenance-actions.ts index f86d588ac68..261039f7b2c 100644 --- a/src/lib/maintenance-actions.ts +++ b/src/lib/maintenance-actions.ts @@ -4,6 +4,10 @@ /* v8 ignore start -- exercised through CLI subprocess maintenance tests. */ import { prompt as askPrompt } from "./credentials"; +import { + type GarbageCollectImagesOptions, + normalizeGarbageCollectImagesOptions, +} from "./lifecycle-options"; import { dockerListImagesFormat, dockerRmi } from "./docker"; import { captureOpenshell } from "./openshell-runtime"; import * as registry from "./registry"; @@ -61,9 +65,12 @@ export function backupAll(): void { } } -export async function garbageCollectImages(args: string[] = []): Promise { - const dryRun = args.includes("--dry-run"); - const skipConfirm = args.includes("--yes") || args.includes("--force"); +export async function garbageCollectImages( + options: string[] | GarbageCollectImagesOptions = {}, +): Promise { + const normalized = normalizeGarbageCollectImagesOptions(options); + const dryRun = normalized.dryRun === true; + const skipConfirm = normalized.yes === true || normalized.force === true; let imagesOutput = ""; try { diff --git a/src/lib/maintenance-cli-commands.ts b/src/lib/maintenance-cli-commands.ts index d1d5d75224f..8d78963cf5b 100644 --- a/src/lib/maintenance-cli-commands.ts +++ b/src/lib/maintenance-cli-commands.ts @@ -45,11 +45,11 @@ export class UpgradeSandboxesCommand extends NemoClawCommand { public async run(): Promise { const { flags } = await this.parse(UpgradeSandboxesCommand); - const args: string[] = []; - if (flags.check) args.push("--check"); - if (flags.auto) args.push("--auto"); - if (flags.yes) args.push("--yes"); - await runUpgradeSandboxesAction(args); + await runUpgradeSandboxesAction({ + auto: flags.auto === true, + check: flags.check === true, + yes: flags.yes === true, + }); } } @@ -68,10 +68,10 @@ export class GarbageCollectImagesCommand extends NemoClawCommand { public async run(): Promise { const { flags } = await this.parse(GarbageCollectImagesCommand); - const args: string[] = []; - if (flags["dry-run"]) args.push("--dry-run"); - if (flags.yes) args.push("--yes"); - if (flags.force) args.push("--force"); - await runGarbageCollectImagesAction(args); + await runGarbageCollectImagesAction({ + dryRun: flags["dry-run"] === true, + force: flags.force === true, + yes: flags.yes === true, + }); } } diff --git a/src/lib/rebuild-cli-command.ts b/src/lib/rebuild-cli-command.ts index 582fe8a7ba0..86aec6449b1 100644 --- a/src/lib/rebuild-cli-command.ts +++ b/src/lib/rebuild-cli-command.ts @@ -3,11 +3,12 @@ /* v8 ignore start -- thin oclif adapter covered through CLI integration tests. */ -import { Args, Command, Flags } from "@oclif/core"; +import { Args, Flags } from "@oclif/core"; +import { NemoClawCommand } from "./nemoclaw-oclif-command"; import { rebuildSandbox } from "./sandbox-runtime-actions"; -export default class RebuildCliCommand extends Command { +export default class RebuildCliCommand extends NemoClawCommand { static id = "sandbox:rebuild"; static strict = true; static summary = "Upgrade sandbox to current agent version"; @@ -21,7 +22,6 @@ export default class RebuildCliCommand extends Command { sandboxName: Args.string({ name: "sandbox", description: "Sandbox name", required: true }), }; static flags = { - help: Flags.help({ char: "h" }), yes: Flags.boolean({ char: "y", description: "Skip the confirmation prompt" }), force: Flags.boolean({ description: "Skip the confirmation prompt" }), verbose: Flags.boolean({ char: "v", description: "Show verbose rebuild diagnostics" }), @@ -29,10 +29,10 @@ export default class RebuildCliCommand extends Command { public async run(): Promise { 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 rebuildSandbox(args.sandboxName, legacyArgs); + await rebuildSandbox(args.sandboxName, { + force: flags.force === true, + verbose: flags.verbose === true, + yes: flags.yes === true, + }); } } diff --git a/src/lib/sandbox-destroy-action.ts b/src/lib/sandbox-destroy-action.ts index 869cd8c19bd..4a249827375 100644 --- a/src/lib/sandbox-destroy-action.ts +++ b/src/lib/sandbox-destroy-action.ts @@ -7,6 +7,10 @@ import fs from "node:fs"; import { CLI_NAME } from "./branding"; import { prompt as askPrompt } from "./credentials"; +import { + type DestroySandboxOptions, + normalizeDestroySandboxOptions, +} from "./lifecycle-options"; import * as onboardSession from "./onboard-session"; import type { Session } from "./onboard-session"; import { OPENSHELL_PROBE_TIMEOUT_MS } from "./openshell-timeouts"; @@ -161,8 +165,12 @@ export function removeSandboxRegistryEntry( return removeSandbox(sandboxName); } -export async function destroySandbox(sandboxName: string, args: string[] = []): Promise { - const skipConfirm = args.includes("--yes") || args.includes("--force"); +export async function destroySandbox( + sandboxName: string, + options: string[] | DestroySandboxOptions = {}, +): Promise { + const normalized = normalizeDestroySandboxOptions(options); + const skipConfirm = normalized.yes === true || normalized.force === true; // Active session detection — enrich the confirmation prompt if sessions are active let activeSessionCount = 0; diff --git a/src/lib/sandbox-rebuild-action.ts b/src/lib/sandbox-rebuild-action.ts index 7ad13c456e8..f172e67ca56 100644 --- a/src/lib/sandbox-rebuild-action.ts +++ b/src/lib/sandbox-rebuild-action.ts @@ -5,6 +5,10 @@ import { CLI_NAME } from "./branding"; import { prompt as askPrompt } from "./credentials"; +import { + normalizeRebuildSandboxOptions, + type RebuildSandboxOptions, +} from "./lifecycle-options"; const { hydrateCredentialEnv } = require("./onboard") as { hydrateCredentialEnv: (name: string) => string | null; }; @@ -49,15 +53,13 @@ function getRebuildCredentialEnvFromRegistry(provider: string | null | undefined export async function rebuildSandbox( sandboxName: string, - args: string[] = [], + options: string[] | RebuildSandboxOptions = {}, opts: { throwOnError?: boolean } = {}, ): Promise { - const verbose = - args.includes("--verbose") || - args.includes("-v") || - process.env.NEMOCLAW_REBUILD_VERBOSE === "1"; + const normalized = normalizeRebuildSandboxOptions(options); + const verbose = normalized.verbose === true || process.env.NEMOCLAW_REBUILD_VERBOSE === "1"; const log: (msg: string) => void = verbose ? _rebuildLog : () => {}; - const skipConfirm = args.includes("--yes") || args.includes("--force"); + const skipConfirm = normalized.yes === true || normalized.force === true; // When called from upgradeSandboxes in a loop, throwOnError prevents // process.exit from aborting the entire batch on the first failure. const bail = opts.throwOnError diff --git a/src/lib/sandbox-runtime-actions.ts b/src/lib/sandbox-runtime-actions.ts index 81171c171e6..7e39c7f182e 100644 --- a/src/lib/sandbox-runtime-actions.ts +++ b/src/lib/sandbox-runtime-actions.ts @@ -3,6 +3,7 @@ /* v8 ignore start -- transitional action facade until implementations leave src/nemoclaw.ts. */ +import type { DestroySandboxOptions, RebuildSandboxOptions } from "./lifecycle-options"; import type { SandboxConnectOptions } from "./sandbox-connect-action"; import type { SandboxLogsOptions } from "./sandbox-logs-options"; @@ -30,18 +31,30 @@ export function showSandboxLogs(sandboxName: string, options: SandboxLogsOptions showSandboxLogsAction(sandboxName, options); } -export async function destroySandbox(sandboxName: string, args: string[] = []): Promise { +export async function destroySandbox( + sandboxName: string, + options: string[] | DestroySandboxOptions = {}, +): Promise { const { destroySandbox: destroyExtractedSandbox } = require("./sandbox-destroy-action") as { - destroySandbox: (sandboxName: string, args?: string[]) => Promise; + destroySandbox: ( + sandboxName: string, + options?: string[] | DestroySandboxOptions, + ) => Promise; }; - await destroyExtractedSandbox(sandboxName, args); + await destroyExtractedSandbox(sandboxName, options); } -export async function rebuildSandbox(sandboxName: string, args: string[] = []): Promise { +export async function rebuildSandbox( + sandboxName: string, + options: string[] | RebuildSandboxOptions = {}, +): Promise { const { rebuildSandbox: rebuildExtractedSandbox } = require("./sandbox-rebuild-action") as { - rebuildSandbox: (sandboxName: string, args?: string[]) => Promise; + rebuildSandbox: ( + sandboxName: string, + options?: string[] | RebuildSandboxOptions, + ) => Promise; }; - await rebuildExtractedSandbox(sandboxName, args); + await rebuildExtractedSandbox(sandboxName, options); } export async function installSandboxSkill( diff --git a/src/lib/upgrade-sandboxes-action.ts b/src/lib/upgrade-sandboxes-action.ts index 281487bd940..80868205170 100644 --- a/src/lib/upgrade-sandboxes-action.ts +++ b/src/lib/upgrade-sandboxes-action.ts @@ -5,6 +5,10 @@ import { CLI_NAME } from "./branding"; import { prompt as askPrompt } from "./credentials"; +import { + normalizeUpgradeSandboxesOptions, + type UpgradeSandboxesOptions, +} from "./lifecycle-options"; import { captureOpenshell } from "./openshell-runtime"; import * as registry from "./registry"; import { parseLiveSandboxNames } from "./runtime-recovery"; @@ -15,10 +19,13 @@ import { B, D, G, R, YW } from "./terminal-style"; // ── Upgrade sandboxes (#1904) ──────────────────────────────────── // Detect sandboxes running stale agent versions and offer to rebuild them. -export async function upgradeSandboxes(args: string[] = []): Promise { - const checkOnly = args.includes("--check"); - const auto = args.includes("--auto"); - const skipConfirm = auto || args.includes("--yes"); +export async function upgradeSandboxes( + options: string[] | UpgradeSandboxesOptions = {}, +): Promise { + const normalized = normalizeUpgradeSandboxesOptions(options); + const checkOnly = normalized.check === true; + const auto = normalized.auto === true; + const skipConfirm = auto || normalized.yes === true; const sandboxes = registry.listSandboxes().sandboxes; if (sandboxes.length === 0) { diff --git a/test/image-cleanup.test.ts b/test/image-cleanup.test.ts index 9714f9f83e6..1b939c9d647 100644 --- a/test/image-cleanup.test.ts +++ b/test/image-cleanup.test.ts @@ -13,6 +13,7 @@ import { removeSandboxImage, removeSandboxRegistryEntry, } from "../src/lib/sandbox-destroy-action"; +import { normalizeGarbageCollectImagesOptions } from "../src/lib/lifecycle-options"; import { help as renderRootHelp } from "../src/lib/root-help-action"; const ROOT = path.resolve(import.meta.dirname, ".."); @@ -129,26 +130,16 @@ describe("image cleanup: gc command exists (#2086)", () => { expect(registrySrc).toContain('"nemoclaw gc"'); }); - it("garbageCollectImages lists sandbox-from images and cross-references registry", () => { - const maintenanceSrc = fs.readFileSync( - path.join(ROOT, "src/lib/maintenance-actions.ts"), - "utf-8", - ); - const gcMatch = maintenanceSrc.match(/async function garbageCollectImages[\s\S]*?^}/m); - expect(gcMatch).toBeTruthy(); - if (!gcMatch) { - throw new Error("Expected garbageCollectImages() in src/lib/maintenance-actions.ts"); - } - const gcBody = gcMatch[0]; - - // Must query docker for sandbox-from images - expect(gcBody).toContain("openshell/sandbox-from"); - // Must consult the registry for in-use tags - expect(gcBody).toContain("registry.listSandboxes"); - // Must support --dry-run - expect(gcBody).toContain("dry-run"); - // Must support --yes - expect(gcBody).toContain("--yes"); + it("gc option normalization supports dry-run and confirmation aliases", () => { + expect(normalizeGarbageCollectImagesOptions(["--dry-run", "--yes"])).toEqual({ + dryRun: true, + force: false, + yes: true, + }); + expect(normalizeGarbageCollectImagesOptions({ dryRun: true, force: true })).toEqual({ + dryRun: true, + force: true, + }); }); it("gc appears in rendered help text", () => {