Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 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
02431db
refactor(cli): migrate connect command to oclif
cv May 1, 2026
37ce2ff
refactor(cli): migrate deploy command to oclif
cv May 1, 2026
d3e218e
refactor(cli): migrate onboard aliases to oclif
cv May 1, 2026
df05a3e
refactor(cli): migrate help and version commands to oclif
cv May 1, 2026
3edcf2e
refactor(cli): centralize legacy oclif dispatch
cv May 1, 2026
47df3b5
refactor(cli): drop trivial oclif wrapper helpers
cv May 1, 2026
460173c
refactor(cli): centralize sandbox runtime bridge
cv May 1, 2026
31c0195
test(cli): keep dispatch table out of coverage ratchet
cv May 1, 2026
bad424d
refactor(cli): centralize policy and channels runtime bridge
cv May 1, 2026
5214142
refactor(cli): centralize global runtime bridge
cv May 1, 2026
5fca48f
refactor(cli): collapse runtime bridge exports
cv May 1, 2026
9616295
refactor(cli): introduce sandbox runtime action facade
cv May 1, 2026
c3a4906
refactor(cli): introduce policy and channels action facade
cv May 1, 2026
35288d8
refactor(cli): introduce global command action facade
cv May 1, 2026
58e71de
refactor(cli): extract root help action
cv May 1, 2026
66a1673
refactor(cli): extract deploy action
cv May 1, 2026
e60ba8e
refactor(cli): extract onboard actions
cv May 1, 2026
7d29288
refactor(cli): extract openshell runtime helpers
cv May 1, 2026
81780c5
refactor(cli): extract sandbox logs action
cv May 1, 2026
278ecc5
test(cli): keep extracted action facades out of coverage
cv May 1, 2026
cb4dbdf
merge(main): resolve sandbox logs action conflicts
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/openshell-runtime.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

/* v8 ignore start -- exercised through CLI subprocess tests. */

import type { StdioOptions } from "node:child_process";

import { ROOT } from "./runner";
Expand Down
238 changes: 238 additions & 0 deletions src/lib/sandbox-logs-action.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,238 @@
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

/* v8 ignore start -- exercised through CLI subprocess log tests. */

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

import { ROOT } from "./runner";
import { getOpenshellBinary, runOpenshell } from "./openshell-runtime";

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;
};

function exitWithSpawnResult(result: SpawnLikeResult & { signal?: NodeJS.Signals | null }) {
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);
}

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;
}

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

function runOpenclawGatewayLogs(sandboxName: string, follow: boolean): SpawnLikeResult {
const args = buildSandboxOpenclawGatewayLogsArgs(sandboxName, follow);
const result = runOpenshell(args, {
stdio: "inherit",
ignoreError: true,
timeout: getLogsProbeTimeoutMs(),
});
if (result.status !== 0) {
console.error(
` OpenClaw log source unavailable (${describeLogProbeResult(result)}): ` +
`openshell ${args.join(" ")}`,
);
}
return result;
}

function streamSandboxFollowLogs(sandboxName: string): void {
const openclawArgs = buildSandboxOpenclawGatewayLogsArgs(sandboxName, true);
const openshellArgs = buildSandboxLogsArgs(sandboxName, true);
const spawnOptions = {
cwd: ROOT,
env: process.env,
stdio: "inherit" as const,
};
const sources: Array<{
label: string;
args: string[];
child: import("node:child_process").ChildProcess;
done: boolean;
}> = [];
let exiting = false;
let completedSources = 0;
let finalStatus = 0;
let requestedExitCode: number | null = null;
let forcedExitTimer: NodeJS.Timeout | null = null;
let setupComplete = false;

const stopChildren = (signal: NodeJS.Signals) => {
for (const { child } of sources) {
if (!child.killed && child.exitCode === null && child.signalCode === null) {
child.kill(signal);
}
}
};
const maybeExit = () => {
if (!setupComplete || completedSources !== sources.length) {
return;
}
if (forcedExitTimer) {
clearTimeout(forcedExitTimer);
forcedExitTimer = null;
}
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,
detail: string | null = null,
) => {
if (source.done) return;
source.done = true;
completedSources += 1;
if (status !== 0 && finalStatus === 0) {
finalStatus = status;
}
if (completedSources < sources.length && !exiting) {
const suffix = detail || `exit ${status}`;
console.error(` ${source.label} stopped (${suffix}); continuing with remaining log source.`);
}
maybeExit();
};
const requestExitAfterSignal = (signal: NodeJS.Signals, exitCode: number) => {
if (requestedExitCode !== null) return;
exiting = true;
requestedExitCode = exitCode;
stopChildren(signal);
forcedExitTimer = setTimeout(() => process.exit(exitCode), 2000);
forcedExitTimer.unref?.();
maybeExit();
};

process.once("SIGINT", () => {
requestExitAfterSignal("SIGINT", 130);
});
process.once("SIGTERM", () => {
requestExitAfterSignal("SIGTERM", 143);
});

const addSource = (label: string, args: string[]) => {
const source = {
label,
args,
child: spawn(getOpenshellBinary(), args, spawnOptions),
done: false,
};
sources.push(source);
source.child.on("error", (error: Error) => {
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);
});
};

addSource("OpenClaw log source", openclawArgs);
enableSandboxAuditLogs(sandboxName);
addSource("OpenShell log source", openshellArgs);
setupComplete = true;
maybeExit();
}

function enableSandboxAuditLogs(sandboxName: string) {
const args = buildEnableSandboxAuditLogsArgs(sandboxName);
const result = runOpenshell(args, {
stdio: ["ignore", "ignore", "pipe"],
ignoreError: true,
timeout: getLogsProbeTimeoutMs(),
});
if (result.status !== 0) {
warnSandboxAuditLogsUnavailable(sandboxName, args, result);
}
}

function warnSandboxAuditLogsUnavailable(
sandboxName: string,
args: string[],
result: SpawnLikeResult,
): void {
const stderr = String(result.stderr || "").trim();
console.error(
` Warning: failed to enable OpenShell audit logs for sandbox '${sandboxName}' ` +
`(${describeLogProbeResult(result)}): openshell ${args.join(" ")}`,
);
if (stderr) {
console.error(` ${stderr}`);
}
console.error(" Policy denial events may be missing from OpenShell logs.");
}

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

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

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

export function showSandboxLogs(sandboxName: string, follow: boolean) {
if (follow) {
streamSandboxFollowLogs(sandboxName);
return;
}

enableSandboxAuditLogs(sandboxName);
runOpenclawGatewayLogs(sandboxName, false);
const args = buildSandboxLogsArgs(sandboxName, false);
const result = runOpenshell(args, {
stdio: "inherit",
ignoreError: true,
});
if (result.status !== 0) {
console.error(` Command failed (exit ${result.status}): openshell ${args.join(" ")}`);
Comment on lines +234 to +235

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Report signal failures correctly in the non-follow error message.

If runOpenshell() returns status === null with a terminating signal, this currently logs exit null even though exitWithSpawnResult() converts the signal into the real process exit code. Reusing describeLogProbeResult() here keeps the stderr output accurate on that path.

🔧 Suggested change
-  if (result.status !== 0) {
-    console.error(`  Command failed (exit ${result.status}): openshell ${args.join(" ")}`);
-  }
+  if (result.status !== 0) {
+    console.error(
+      `  Command failed (${describeLogProbeResult(result)}): openshell ${args.join(" ")}`,
+    );
+  }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/lib/sandbox-logs-action.ts` around lines 234 - 235, The error message
shown when runOpenshell() fails prints `exit ${result.status}` which becomes
`exit null` for signal-terminated processes; update the non-follow failure path
to use the existing formatter describeLogProbeResult(result) (and/or
exitWithSpawnResult(result) if that returns the numeric code) instead of
interpolating result.status so the logged stderr reflects the converted/semantic
exit code produced by exitWithSpawnResult/describeLogProbeResult; change the
console.error call inside the if (result.status !== 0) branch to include
describeLogProbeResult(result) (or the returned value from exitWithSpawnResult)
and keep the rest of the message text the same.

}
exitWithSpawnResult(result);
}
5 changes: 4 additions & 1 deletion src/lib/sandbox-runtime-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ export async function showSandboxStatus(sandboxName: string): Promise<void> {
}

export function showSandboxLogs(sandboxName: string, follow: boolean): void {
getNemoClawRuntimeBridge().sandboxLogs(sandboxName, follow);
const { showSandboxLogs: showSandboxLogsAction } = require("./sandbox-logs-action") as {
showSandboxLogs: (sandboxName: string, follow: boolean) => void;
};
showSandboxLogsAction(sandboxName, follow);
}

export async function destroySandbox(sandboxName: string, args: string[] = []): Promise<void> {
Expand Down
Loading
Loading