File size: 9,917 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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 | import type { ChildProcess } from "node:child_process";
import { existsSync } from "node:fs";
import path from "node:path";
import { describe, expect, it, vi } from "vitest";
import * as execSpawn from "../process/exec-spawn.js";
import * as processExecution from "../process/exec.js";
import { isPidAlive } from "../shared/pid-alive.js";
import { readPidFile, waitForPidToExit } from "../test-utils/process-tree.js";
import { withTempDir } from "../test-utils/temp-dir.js";
import { runCronCommandJob } from "./command-runner.js";
import type { CronJob } from "./types.js";
function makeCommandJob(payload: Extract<CronJob["payload"], { kind: "command" }>): CronJob {
const now = Date.now();
return {
id: "command-job",
name: "Command job",
enabled: true,
createdAtMs: now,
updatedAtMs: now,
schedule: { kind: "every", everyMs: 60_000 },
sessionTarget: "isolated",
wakeMode: "now",
payload,
state: {},
};
}
describe("runCronCommandJob", () => {
it("runs command argv and returns stdout as the deliverable summary", async () => {
const result = await runCronCommandJob({
job: makeCommandJob({
kind: "command",
argv: [process.execPath, "-e", "process.stdout.write('hello from cron')"],
timeoutSeconds: 5,
}),
nowMs: () => 123,
});
expect(result.status).toBe("ok");
expect(result.errorClassification).toBeUndefined();
expect(result.summary).toBe("hello from cron");
expect(result.diagnostics?.entries[0]).toMatchObject({
ts: 123,
source: "exec",
severity: "info",
exitCode: 0,
});
});
it("preserves exact NO_REPLY stdout for outbound suppression", async () => {
const result = await runCronCommandJob({
job: makeCommandJob({
kind: "command",
argv: [process.execPath, "-e", "process.stdout.write('NO_REPLY\\n')"],
timeoutSeconds: 5,
}),
});
expect(result.status).toBe("ok");
expect(result.summary).toBe("NO_REPLY");
});
it("marks non-zero exit codes as cron errors and keeps stderr as summary", async () => {
const result = await runCronCommandJob({
job: makeCommandJob({
kind: "command",
argv: [process.execPath, "-e", "process.stderr.write('bad thing'); process.exit(7)"],
timeoutSeconds: 5,
}),
});
expect(result.status).toBe("error");
expect(result.error).toBe("command exited with code 7");
expect(result.errorClassification).toEqual({ kind: "permanent" });
expect(result.failureNotificationDetail).toEqual({ kind: "command-exit", exitCode: 7 });
expect(result.summary).toBe("bad thing");
expect(result.diagnostics?.entries[0]).toMatchObject({
source: "exec",
severity: "error",
exitCode: 7,
});
});
it("preserves early action-required command output when the captured tail is truncated", async () => {
const result = await runCronCommandJob({
job: makeCommandJob({
kind: "command",
argv: [
process.execPath,
"-e",
[
"process.stdout.write('Visit https://example.com/device and enter code ABCD-EFGH\\n')",
"process.stdout.write('x'.repeat(200))",
].join(";"),
],
timeoutSeconds: 5,
outputMaxBytes: 24,
}),
});
expect(result.status).toBe("ok");
expect(result.summary).toBe(
`action-required output preserved:\nVisit https://example.com/device and enter code ABCD-EFGH\n\n${"x".repeat(24)}`,
);
expect(result.diagnostics?.summary).toBe(result.summary);
expect(result.diagnostics?.entries[0]).toMatchObject({ truncated: true });
});
it("marks command timeouts as cron errors", async () => {
const result = await runCronCommandJob({
job: makeCommandJob({
kind: "command",
argv: [process.execPath, "-e", "setInterval(() => {}, 1000)"],
timeoutSeconds: 0.05,
}),
nowMs: () => 456,
});
expect(result.status).toBe("error");
expect(result.error).toBe("command timed out");
expect(result.errorClassification).toEqual({ kind: "reason", reason: "timeout" });
expect(result.failureNotificationDetail).toEqual({
kind: "command-timeout",
mode: "wall-clock",
});
expect(result.diagnostics?.entries[0]).toMatchObject({
ts: 456,
source: "exec",
severity: "error",
});
});
it.skipIf(process.platform === "win32")("kills shell process groups on timeout", async () =>
withTempDir("openclaw-cron-command-", async (tempDir) => {
const childPidPath = path.join(tempDir, "child.pid");
const shellCommand = [
"sleep 60 &",
"child_pid=$!",
`printf '%s' "$child_pid" > ${JSON.stringify(childPidPath)}`,
'wait "$child_pid"',
].join("\n");
const controller = new AbortController();
const realSetTimeout = setTimeout;
const spawnSpy = vi.spyOn(execSpawn, "spawnCommandWithInvocation");
let parent: ChildProcess | undefined;
let command: ReturnType<typeof runCronCommandJob> | undefined;
try {
// Freeze the deadline until the real shell has published a live child;
// startup time must not consume the behavior this test is exercising.
vi.useFakeTimers({ toFake: ["Date", "setTimeout", "clearTimeout"] });
command = runCronCommandJob({
job: makeCommandJob({
kind: "command",
argv: ["sh", "-lc", shellCommand],
timeoutSeconds: 0.5,
}),
abortSignal: controller.signal,
});
const spawnResult = spawnSpy.mock.results[0];
if (spawnResult?.type !== "return") {
throw new Error("command did not spawn");
}
parent = spawnResult.value.child.nodeChildProcess;
let childPid = 0;
for (let attempt = 0; attempt < 80; attempt += 1) {
if (existsSync(childPidPath)) {
childPid = await readPidFile(childPidPath);
if (Number.isSafeInteger(childPid) && isPidAlive(childPid)) {
break;
}
}
await new Promise<void>((resolve) => {
realSetTimeout(resolve, 25);
});
}
expect(Number.isSafeInteger(childPid)).toBe(true);
expect(isPidAlive(childPid)).toBe(true);
await vi.advanceTimersByTimeAsync(500);
await vi.advanceTimersByTimeAsync(execSpawn.COMMAND_PROCESS_TREE_KILL_GRACE_MS);
// Force delivery now has a separate bounded exit-observation phase.
await vi.advanceTimersByTimeAsync(execSpawn.COMMAND_PROCESS_TREE_KILL_GRACE_MS);
const result = await command;
expect(result.status).toBe("error");
expect(result.error).toBe("command timed out");
vi.useRealTimers();
expect(await waitForPidToExit(childPid)).toBe(true);
} finally {
try {
controller.abort();
if (parent?.pid) {
try {
process.kill(-parent.pid, "SIGKILL");
} catch {
// The command may already have reaped its process group.
}
}
if (vi.isFakeTimers()) {
await vi.runAllTimersAsync();
}
} finally {
vi.useRealTimers();
spawnSpy.mockRestore();
await command;
}
}
}),
);
it("marks no-output timeouts as cron errors", async () => {
const result = await runCronCommandJob({
job: makeCommandJob({
kind: "command",
argv: [process.execPath, "-e", "setInterval(() => {}, 1000)"],
timeoutSeconds: 5,
noOutputTimeoutSeconds: 0.05,
}),
});
expect(result.status).toBe("error");
expect(result.error).toBe("command produced no output before noOutputTimeoutSeconds");
expect(result.errorClassification).toEqual({ kind: "reason", reason: "timeout" });
expect(result.failureNotificationDetail).toEqual({
kind: "command-timeout",
mode: "no-output",
});
expect(result.diagnostics?.entries[0]).toMatchObject({
source: "exec",
severity: "error",
});
});
it("marks aborted command runs as cron errors", async () => {
const controller = new AbortController();
controller.abort();
const result = await runCronCommandJob({
job: makeCommandJob({
kind: "command",
argv: [process.execPath, "-e", "process.stdout.write('should not run')"],
timeoutSeconds: 5,
}),
abortSignal: controller.signal,
});
expect(result.status).toBe("error");
expect(result.error).toBe("command stopped");
expect(result.errorClassification).toBeUndefined();
expect(result.summary).toBeUndefined();
expect(result.failureNotificationDetail).toBeUndefined();
});
it("keeps command start failures generic", async () => {
const result = await runCronCommandJob({
job: makeCommandJob({
kind: "command",
argv: ["openclaw-command-that-does-not-exist"],
timeoutSeconds: 5,
}),
});
expect(result.status).toBe("error");
expect(result.failureNotificationDetail).toBeUndefined();
expect(result.errorClassification).toEqual({ kind: "permanent" });
});
it("leaves transient command start errors unclassified", async () => {
const spawnError = Object.assign(new Error("spawn EAGAIN"), { code: "EAGAIN" });
const runCommand = vi
.spyOn(processExecution, "runCommandWithTimeout")
.mockRejectedValueOnce(spawnError);
try {
const result = await runCronCommandJob({
job: makeCommandJob({ kind: "command", argv: [process.execPath] }),
});
expect(result.status).toBe("error");
expect(result.error).toBe("spawn EAGAIN");
expect(result.errorClassification).toBeUndefined();
} finally {
runCommand.mockRestore();
}
});
});
|