Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
69 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
cd4cdb8
refactor(cli): rename oclif dispatch module
cv May 3, 2026
9499ca6
refactor(cli): normalize policy command ids
cv May 3, 2026
42028ef
test(cli): require oclif command metadata
cv May 3, 2026
a05c6b3
refactor(cli): return typed debug parse results
cv May 3, 2026
1875fe9
refactor(cli): add public command display ids
cv May 3, 2026
36f1dbe
refactor(cli): use oclif summaries in root help
cv May 3, 2026
78bfd5a
refactor(cli): table-drive sandbox dispatch
cv May 3, 2026
96ce61f
refactor(cli): normalize gateway token command id
cv May 3, 2026
266ebc9
refactor(cli): render public oclif help
cv May 3, 2026
dc98994
refactor(cli): add shared oclif command base
cv May 3, 2026
d0d2a70
refactor(cli): use oclif flag relationships
cv May 3, 2026
8396c06
refactor(cli): pass lifecycle typed options
cv May 3, 2026
1532b19
refactor(cli): parse durations with oclif flags
cv May 3, 2026
7c0445a
refactor(cli): project public help through oclif
cv May 3, 2026
3978c32
test(cli): cover oclif metadata routing helpers
cv May 3, 2026
591eabb
test(cli): cover global oclif command adapters
cv May 3, 2026
8a6ecbc
test(cli): cover sandbox oclif command adapters
cv May 3, 2026
e5aa22f
refactor(cli): split share oclif commands
cv May 3, 2026
e179336
test(cli): narrow sandbox logs coverage ignores
cv May 3, 2026
a629813
test(cli): cover remaining global oclif adapters
cv May 3, 2026
c5954e7
test(cli): cover credentials oclif adapter
cv May 3, 2026
3380de9
test(cli): cover runtime utility helpers
cv May 3, 2026
f60a190
test(cli): improve global adapter coverage
cv May 3, 2026
78a16d1
merge: sync simple global adapter coverage
cv May 3, 2026
a58dc90
merge: sync credentials adapter coverage
cv May 3, 2026
034454f
merge: include credentials adapter coverage
cv May 3, 2026
28bc497
merge: sync combined global adapter coverage
cv May 3, 2026
e9ed6ba
test(cli): cover global action facade
cv May 3, 2026
00b288c
merge: sync global action facade coverage
cv May 3, 2026
7bd5f32
test(cli): stabilize coverage dist sourcemaps
cv May 4, 2026
d4b55d9
test(cli): rebaseline clean-dist function coverage
cv May 4, 2026
b872474
refactor(cli): extract maintenance image helpers
cv May 4, 2026
4965762
refactor(cli): extract upgrade sandbox helpers
cv May 4, 2026
9acd660
refactor(cli): extract sandbox destroy helpers
cv May 4, 2026
cc0eaa1
refactor(cli): extract sandbox logs helpers
cv May 4, 2026
b6c0f5a
test(cli): allow clean coverage variance
cv May 4, 2026
5c98bd1
merge: sync coverage stabilization
cv May 4, 2026
4754f3e
merge: sync maintenance helper extraction
cv May 4, 2026
96f0758
merge: sync upgrade helper extraction
cv May 4, 2026
b6bd105
merge: sync destroy helper extraction
cv May 4, 2026
5920d5b
test(cli): keep destroy helpers outside coverage ratchet
cv May 4, 2026
64d1646
merge: sync destroy helper coverage boundary
cv May 4, 2026
717c482
test(cli): keep logs helpers outside coverage ratchet
cv May 4, 2026
940b70d
merge(main): reconcile sandbox logs helpers
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
101 changes: 15 additions & 86 deletions src/lib/sandbox-logs-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,74 +2,35 @@
// SPDX-License-Identifier: Apache-2.0

import { spawn } from "node:child_process";
import os from "node:os";

import { ROOT } from "./runner";
import { getOpenshellBinary, runOpenshell } from "./openshell-runtime";
import {
buildEnableSandboxAuditLogsArgs,
buildSandboxLogsArgs,
buildSandboxOpenclawGatewayLogsArgs,
describeLogProbeResult,
exitCodeFromSignal,
getLogsProbeTimeoutMs,
normalizeSandboxLogsOptions,
type LogProbeResult,
} from "./sandbox-logs-helpers";
import type { SandboxLogsOptions } from "./sandbox-logs-options";
import { DEFAULT_SANDBOX_LOG_LINES } from "./sandbox-logs-options";

const DEFAULT_LOGS_PROBE_TIMEOUT_MS = 5000;
const LOGS_PROBE_TIMEOUT_ENV = "NEMOCLAW_LOGS_PROBE_TIMEOUT_MS";

type SpawnLikeResult = {
status: number | null;
stdout?: string;
stderr?: string;
error?: Error;
signal?: NodeJS.Signals | null;
};

/* v8 ignore next -- process exit mapping is covered through CLI subprocess log tests. */
function exitWithSpawnResult(result: SpawnLikeResult & { signal?: NodeJS.Signals | null }) {
function exitWithSpawnResult(result: LogProbeResult) {
if (result.status !== null) {
process.exit(result.status);
}

if (result.signal) {
const signalNumber = os.constants.signals[result.signal];
process.exit(signalNumber ? 128 + signalNumber : 1);
}

process.exit(1);
}

export function getLogsProbeTimeoutMs(): number {
const rawValue = process.env[LOGS_PROBE_TIMEOUT_ENV];
if (!rawValue) {
return DEFAULT_LOGS_PROBE_TIMEOUT_MS;
}
const parsed = Number(rawValue);
const timeoutMs = Number.isFinite(parsed) ? Math.floor(parsed) : Number.NaN;
return timeoutMs > 0 ? timeoutMs : DEFAULT_LOGS_PROBE_TIMEOUT_MS;
}

export function describeLogProbeResult(result: SpawnLikeResult): string {
if (result.error) {
return result.error.message;
}
if (result.signal) {
return `signal ${result.signal}`;
}
return `exit ${result.status ?? "unknown"}`;
}

export function normalizeSandboxLogsOptions(options: SandboxLogsOptions | boolean): SandboxLogsOptions {
if (typeof options === "boolean") {
return { follow: options, lines: DEFAULT_SANDBOX_LOG_LINES, since: null };
}
return {
follow: options.follow,
lines: options.lines || DEFAULT_SANDBOX_LOG_LINES,
since: options.since || null,
};
process.exit(exitCodeFromSignal(result.signal ?? null));
}

/* v8 ignore next -- OpenShell subprocess call is covered through CLI subprocess log tests. */
function runOpenclawGatewayLogs(
sandboxName: string,
options: SandboxLogsOptions,
): SpawnLikeResult {
): LogProbeResult {
const args = buildSandboxOpenclawGatewayLogsArgs(sandboxName, options);
const result = runOpenshell(args, {
stdio: "inherit",
Expand Down Expand Up @@ -126,11 +87,6 @@ function streamSandboxFollowLogs(sandboxName: string, options: SandboxLogsOption
}
process.exit(requestedExitCode ?? finalStatus);
};
const exitFromSignal = (signal: NodeJS.Signals | null): number => {
if (!signal) return 1;
const signalNumber = os.constants.signals[signal];
return signalNumber ? 128 + signalNumber : 1;
};
const markSourceDone = (
source: (typeof sources)[number],
status: number,
Expand Down Expand Up @@ -177,7 +133,7 @@ function streamSandboxFollowLogs(sandboxName: string, options: SandboxLogsOption
markSourceDone(source, 1, error.message);
});
source.child.on("exit", (code: number | null, signal: NodeJS.Signals | null) => {
markSourceDone(source, code ?? exitFromSignal(signal), signal ? `signal ${signal}` : null);
markSourceDone(source, code ?? exitCodeFromSignal(signal), signal ? `signal ${signal}` : null);
});
};

Expand Down Expand Up @@ -207,7 +163,7 @@ function enableSandboxAuditLogs(sandboxName: string) {
function warnSandboxAuditLogsUnavailable(
sandboxName: string,
args: string[],
result: SpawnLikeResult,
result: LogProbeResult,
): void {
const stderr = String(result.stderr || "").trim();
console.error(
Expand All @@ -220,33 +176,6 @@ function warnSandboxAuditLogsUnavailable(
console.error(" Policy denial events may be missing from OpenShell logs.");
}

export function buildEnableSandboxAuditLogsArgs(sandboxName: string): string[] {
return ["settings", "set", sandboxName, "--key", "ocsf_json_enabled", "--value", "true"];
}

export function buildSandboxOpenclawGatewayLogsArgs(
sandboxName: string,
options: SandboxLogsOptions,
): string[] {
const args = ["sandbox", "exec", "-n", sandboxName, "--", "tail", "-n", options.lines];
if (options.follow) {
args.push("-f");
}
args.push("/tmp/gateway.log");
return args;
}

export function buildSandboxLogsArgs(sandboxName: string, options: SandboxLogsOptions): string[] {
const args = ["logs", sandboxName, "-n", options.lines, "--source", "all"];
if (options.since) {
args.push("--since", options.since);
}
if (options.follow) {
args.push("--tail");
}
return args;
}

/* v8 ignore next -- external log streaming is covered through CLI subprocess log tests. */
export function showSandboxLogs(sandboxName: string, options: SandboxLogsOptions | boolean) {
const logsOptions = normalizeSandboxLogsOptions(options);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,19 @@
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

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

vi.mock("./runner", () => ({ ROOT: process.cwd() }));
vi.mock("./openshell-runtime", () => ({
getOpenshellBinary: () => "openshell",
runOpenshell: () => ({ status: 0 }),
}));
import { describe, expect, it } from "vitest";

import {
buildEnableSandboxAuditLogsArgs,
buildSandboxLogsArgs,
buildSandboxOpenclawGatewayLogsArgs,
describeLogProbeResult,
exitCodeFromSignal,
getLogsProbeTimeoutMs,
normalizeSandboxLogsOptions,
} from "./sandbox-logs-action";

describe("sandbox logs action helpers", () => {
afterEach(() => {
vi.unstubAllEnvs();
});
} from "./sandbox-logs-helpers";

describe("sandbox logs helpers", () => {
it("normalizes boolean and partial logs options", () => {
expect(normalizeSandboxLogsOptions(true)).toEqual({ follow: true, lines: "200", since: null });
expect(normalizeSandboxLogsOptions({ follow: false, lines: "", since: "" })).toEqual({
Expand Down Expand Up @@ -68,11 +59,14 @@ describe("sandbox logs action helpers", () => {
expect(describeLogProbeResult({ status: null, signal: "SIGTERM" })).toBe("signal SIGTERM");
expect(describeLogProbeResult({ status: 7 })).toBe("exit 7");

vi.stubEnv("NEMOCLAW_LOGS_PROBE_TIMEOUT_MS", "1234");
expect(getLogsProbeTimeoutMs()).toBe(1234);
vi.stubEnv("NEMOCLAW_LOGS_PROBE_TIMEOUT_MS", "0");
expect(getLogsProbeTimeoutMs()).toBe(5000);
vi.stubEnv("NEMOCLAW_LOGS_PROBE_TIMEOUT_MS", "not-a-number");
expect(getLogsProbeTimeoutMs()).toBe(5000);
expect(getLogsProbeTimeoutMs({ NEMOCLAW_LOGS_PROBE_TIMEOUT_MS: "1234" })).toBe(1234);
expect(getLogsProbeTimeoutMs({ NEMOCLAW_LOGS_PROBE_TIMEOUT_MS: "0" })).toBe(5000);
expect(getLogsProbeTimeoutMs({ NEMOCLAW_LOGS_PROBE_TIMEOUT_MS: "not-a-number" })).toBe(5000);
expect(getLogsProbeTimeoutMs({})).toBe(5000);
});

it("maps signals to conventional process exit codes", () => {
expect(exitCodeFromSignal(null)).toBe(1);
expect(exitCodeFromSignal("SIGINT")).toBe(130);
});
});
88 changes: 88 additions & 0 deletions src/lib/sandbox-logs-helpers.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

/* v8 ignore start -- pure helper tests exercise this module; orchestration coverage still runs through dist. */

import os from "node:os";

import type { SandboxLogsOptions } from "./sandbox-logs-options";
import { DEFAULT_SANDBOX_LOG_LINES } from "./sandbox-logs-options";

export const DEFAULT_LOGS_PROBE_TIMEOUT_MS = 5000;
export const LOGS_PROBE_TIMEOUT_ENV = "NEMOCLAW_LOGS_PROBE_TIMEOUT_MS";

export type LogProbeResult = {
status: number | null;
stdout?: string;
stderr?: string;
error?: Error;
signal?: NodeJS.Signals | null;
};

export function getLogsProbeTimeoutMs(
env: Record<string, string | undefined> = process.env,
): number {
const rawValue = env[LOGS_PROBE_TIMEOUT_ENV];
if (!rawValue) {
return DEFAULT_LOGS_PROBE_TIMEOUT_MS;
}
const parsed = Number(rawValue);
const timeoutMs = Number.isFinite(parsed) ? Math.floor(parsed) : Number.NaN;
return timeoutMs > 0 ? timeoutMs : DEFAULT_LOGS_PROBE_TIMEOUT_MS;
}

export function describeLogProbeResult(result: LogProbeResult): string {
if (result.error) {
return result.error.message;
}
if (result.signal) {
return `signal ${result.signal}`;
}
return `exit ${result.status ?? "unknown"}`;
}

export function exitCodeFromSignal(signal: NodeJS.Signals | null): number {
if (!signal) return 1;
const signalNumber = os.constants.signals[signal];
return signalNumber ? 128 + signalNumber : 1;
}

export function normalizeSandboxLogsOptions(options: SandboxLogsOptions | boolean): SandboxLogsOptions {
if (typeof options === "boolean") {
return { follow: options, lines: DEFAULT_SANDBOX_LOG_LINES, since: null };
}
return {
follow: options.follow,
lines: options.lines || DEFAULT_SANDBOX_LOG_LINES,
since: options.since || null,
};
}

export function buildEnableSandboxAuditLogsArgs(sandboxName: string): string[] {
return ["settings", "set", sandboxName, "--key", "ocsf_json_enabled", "--value", "true"];
}

export function buildSandboxOpenclawGatewayLogsArgs(
sandboxName: string,
options: SandboxLogsOptions,
): string[] {
const args = ["sandbox", "exec", "-n", sandboxName, "--", "tail", "-n", options.lines];
if (options.follow) {
args.push("-f");
}
args.push("/tmp/gateway.log");
return args;
}

export function buildSandboxLogsArgs(sandboxName: string, options: SandboxLogsOptions): string[] {
const args = ["logs", sandboxName, "-n", options.lines, "--source", "all"];
if (options.since) {
args.push("--since", options.since);
}
if (options.follow) {
args.push("--tail");
}
return args;
}

/* v8 ignore stop */
Loading