File size: 6,448 Bytes
3144483 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | import { finiteSecondsToTimerSafeMilliseconds } from "@openclaw/normalization-core/number-coercion";
import { runCommandWithTimeout } from "../process/exec.js";
import {
buildCronCommandSummary,
isCronCommandActionCriticalLine,
} from "./command-output-summary.js";
import type { CronRunDiagnostics, CronRunOutcome, CronRunStatus, CronJob } from "./types.js";
const DEFAULT_COMMAND_TIMEOUT_MS = 10 * 60_000;
const EFFECTIVELY_UNBOUNDED_TIMEOUT_MS = 2_147_483_647;
function secondsToMs(value: number | undefined): number | undefined {
if (typeof value !== "number") {
return undefined;
}
if (value <= 0) {
return EFFECTIVELY_UNBOUNDED_TIMEOUT_MS;
}
return finiteSecondsToTimerSafeMilliseconds(value) ?? undefined;
}
function formatCommand(argv: string[]): string {
return argv.map((arg) => JSON.stringify(arg)).join(" ");
}
function commandErrorMessage(params: {
code: number | null;
signal: NodeJS.Signals | null;
termination: string;
}): string {
if (params.termination === "timeout") {
return "command timed out";
}
if (params.termination === "no-output-timeout") {
return "command produced no output before noOutputTimeoutSeconds";
}
if (params.termination === "signal") {
return params.signal ? `command stopped by signal ${params.signal}` : "command stopped";
}
if (typeof params.code === "number") {
return `command exited with code ${params.code}`;
}
return "command failed";
}
function buildDiagnostics(params: {
command: string;
status: CronRunStatus;
summary?: string;
code: number | null;
signal: NodeJS.Signals | null;
stdoutTruncatedBytes?: number;
stderrTruncatedBytes?: number;
nowMs: () => number;
}): CronRunDiagnostics {
const truncated =
Boolean(params.stdoutTruncatedBytes && params.stdoutTruncatedBytes > 0) ||
Boolean(params.stderrTruncatedBytes && params.stderrTruncatedBytes > 0);
return {
...(params.summary ? { summary: params.summary } : {}),
entries: [
{
ts: params.nowMs(),
source: "exec",
severity: params.status === "ok" ? "info" : "error",
message: params.summary
? `command ${params.status}: ${params.command}`
: `command ${params.status} with no output: ${params.command}`,
exitCode: params.code,
truncated,
...(params.signal ? { toolName: `signal:${params.signal}` } : {}),
},
],
};
}
/** Executes a cron command payload without starting an agent/model run. */
export async function runCronCommandJob(params: {
job: CronJob;
abortSignal?: AbortSignal;
nowMs?: () => number;
}): Promise<CronRunOutcome> {
const nowMs = params.nowMs ?? Date.now;
const { payload } = params.job;
if (payload.kind !== "command") {
return {
status: "skipped",
error: 'command runner requires payload.kind="command"',
};
}
if (!Array.isArray(payload.argv) || payload.argv.length === 0) {
return {
status: "skipped",
error: 'command payload requires non-empty "argv"',
};
}
const command = formatCommand(payload.argv);
const noOutputTimeoutMs = secondsToMs(payload.noOutputTimeoutSeconds);
try {
const result = await runCommandWithTimeout(payload.argv, {
timeoutMs: secondsToMs(payload.timeoutSeconds) ?? DEFAULT_COMMAND_TIMEOUT_MS,
...(payload.cwd ? { cwd: payload.cwd } : {}),
...(payload.input !== undefined ? { input: payload.input } : {}),
...(payload.env ? { env: payload.env } : {}),
...(noOutputTimeoutMs !== undefined ? { noOutputTimeoutMs } : {}),
...(payload.outputMaxBytes !== undefined ? { maxOutputBytes: payload.outputMaxBytes } : {}),
preserveOutputLine: isCronCommandActionCriticalLine,
...(params.abortSignal ? { signal: params.abortSignal } : {}),
killProcessTree: true,
});
const termination =
result.termination === "signal" &&
params.abortSignal?.reason instanceof Error &&
params.abortSignal.reason.name === "TimeoutError"
? "timeout"
: result.termination;
const ok =
result.code === 0 &&
!result.killed &&
termination !== "timeout" &&
termination !== "no-output-timeout" &&
termination !== "signal";
const status: CronRunStatus = ok ? "ok" : "error";
const summary = buildCronCommandSummary({
stdout: result.stdout,
stderr: result.stderr,
preservedStdoutLines: result.preservedStdoutLines,
preservedStderrLines: result.preservedStderrLines,
});
const error = ok
? undefined
: commandErrorMessage({
code: result.code,
signal: result.signal,
termination,
});
const failureNotificationDetail =
termination === "timeout"
? ({ kind: "command-timeout", mode: "wall-clock" } as const)
: termination === "no-output-timeout"
? ({ kind: "command-timeout", mode: "no-output" } as const)
: termination === "exit" && typeof result.code === "number" && result.code !== 0
? ({ kind: "command-exit", exitCode: result.code } as const)
: undefined;
return {
status,
...(error ? { error } : {}),
...(failureNotificationDetail
? {
failureNotificationDetail,
errorClassification:
failureNotificationDetail.kind === "command-timeout"
? ({ kind: "reason", reason: "timeout" } as const)
: ({ kind: "permanent" } as const),
}
: {}),
...(summary ? { summary } : {}),
diagnostics: buildDiagnostics({
command,
status,
summary,
code: result.code,
signal: result.signal,
stdoutTruncatedBytes: result.stdoutTruncatedBytes,
stderrTruncatedBytes: result.stderrTruncatedBytes,
nowMs,
}),
};
} catch (err) {
const error = err instanceof Error ? err.message : String(err);
return {
status: "error",
error,
...(err instanceof Error && "code" in err && err.code === "ENOENT"
? { errorClassification: { kind: "permanent" as const } }
: {}),
diagnostics: {
summary: error,
entries: [
{
ts: nowMs(),
source: "exec",
severity: "error",
message: `command failed to start: ${command}: ${error}`,
exitCode: null,
},
],
},
};
}
}
|