-
Notifications
You must be signed in to change notification settings - Fork 3.1k
refactor(cli): extract sandbox logs action #2836
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
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 5e997cb
test(cli): relax uninstall helper timeouts
cv 88cd958
refactor(cli): migrate status and tunnel commands to oclif
cv 3f748d6
refactor(cli): migrate debug uninstall and gateway-token to oclif
cv 0bc877a
refactor(cli): migrate credentials commands to oclif
cv 3fd15fc
refactor(cli): migrate sandbox inspection commands to oclif
cv 81c62bb
refactor(cli): migrate maintenance commands to oclif
cv 7d6a6fb
refactor(cli): migrate sandbox logs command to oclif
cv 3f17a4f
refactor(cli): migrate skill install command to oclif
cv 0fb3c41
refactor(cli): migrate snapshot list and create to oclif
cv d11eeee
refactor(cli): migrate shields commands to oclif
cv 74806ce
refactor(cli): migrate channels mutation commands to oclif
cv e637e84
refactor(cli): migrate policy mutation commands to oclif
cv ee20dfb
refactor(cli): migrate snapshot restore command to oclif
cv 8d3a568
refactor(cli): migrate destroy command to oclif
cv b447f36
refactor(cli): migrate rebuild command to oclif
cv 02431db
refactor(cli): migrate connect command to oclif
cv 37ce2ff
refactor(cli): migrate deploy command to oclif
cv d3e218e
refactor(cli): migrate onboard aliases to oclif
cv df05a3e
refactor(cli): migrate help and version commands to oclif
cv 3edcf2e
refactor(cli): centralize legacy oclif dispatch
cv 47df3b5
refactor(cli): drop trivial oclif wrapper helpers
cv 460173c
refactor(cli): centralize sandbox runtime bridge
cv 31c0195
test(cli): keep dispatch table out of coverage ratchet
cv bad424d
refactor(cli): centralize policy and channels runtime bridge
cv 5214142
refactor(cli): centralize global runtime bridge
cv 5fca48f
refactor(cli): collapse runtime bridge exports
cv 9616295
refactor(cli): introduce sandbox runtime action facade
cv c3a4906
refactor(cli): introduce policy and channels action facade
cv 35288d8
refactor(cli): introduce global command action facade
cv 58e71de
refactor(cli): extract root help action
cv 66a1673
refactor(cli): extract deploy action
cv e60ba8e
refactor(cli): extract onboard actions
cv 7d29288
refactor(cli): extract openshell runtime helpers
cv 81780c5
refactor(cli): extract sandbox logs action
cv 278ecc5
test(cli): keep extracted action facades out of coverage
cv cb4dbdf
merge(main): resolve sandbox logs action conflicts
cv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(" ")}`); | ||
| } | ||
| exitWithSpawnResult(result); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Report signal failures correctly in the non-follow error message.
If
runOpenshell()returnsstatus === nullwith a terminating signal, this currently logsexit nulleven thoughexitWithSpawnResult()converts the signal into the real process exit code. ReusingdescribeLogProbeResult()here keeps the stderr output accurate on that path.🔧 Suggested change
🤖 Prompt for AI Agents