Skip to content

Commit 80e046d

Browse files
authored
fix(cron): show skipped automation tasks as failed (#123787)
* fix(cron): report skipped runs as failed tasks * test(cron): align skipped task expectations * fix(cron): keep skipped tasks failed
1 parent 986be45 commit 80e046d

6 files changed

Lines changed: 86 additions & 18 deletions

File tree

‎src/cron/service/ops.test.ts‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1450,9 +1450,7 @@ describe("cron service ops seam coverage", () => {
14501450
expect(restored?.state.lastRunAtMs).toBe(startedAt);
14511451
expect(restored?.state.lastRunStatus).toBe(status);
14521452
expect(runIsolatedAgentJob).not.toHaveBeenCalled();
1453-
expect(findTaskByRunId(taskRunId)?.status).toBe(
1454-
status === "error" ? "failed" : "succeeded",
1455-
);
1453+
expect(findTaskByRunId(taskRunId)?.status).toBe(status === "ok" ? "succeeded" : "failed");
14561454
} finally {
14571455
stop(state);
14581456
}

‎src/cron/service/task-runs.test.ts‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,21 @@ describe("cron task run terminal records", () => {
164164
tryFinishCronTaskRunWithoutHistory(state, {
165165
taskRunId: runIds[0],
166166
status: "skipped",
167+
error: "cron: job execution timed out",
167168
endedAt: 1_501,
168169
});
169170
expect(childSessionKey(systemEventJob)).toBeUndefined();
171+
expect(
172+
listTaskRegistryRecordsByRuntimeSourceIdFromSqlite({
173+
runtime: "cron",
174+
sourceId: systemEventJob.id,
175+
}),
176+
).toEqual([
177+
expect.objectContaining({
178+
status: "failed",
179+
error: "cron: job execution timed out",
180+
}),
181+
]);
170182
},
171183
);
172184
});
@@ -429,7 +441,7 @@ describe("cron task run terminal records", () => {
429441
action: "finished",
430442
job,
431443
status: "skipped",
432-
error: "trigger condition not met",
444+
error: "cron: job execution timed out",
433445
runId: "manual:skipped-job:1",
434446
runAtMs: startedAt,
435447
durationMs: 0,
@@ -446,10 +458,10 @@ describe("cron task run terminal records", () => {
446458
runtime: "cron",
447459
sourceId: job.id,
448460
agentId: "finn",
449-
status: "succeeded",
461+
status: "failed",
450462
startedAt,
451463
endedAt: startedAt,
452-
error: "trigger condition not met",
464+
error: "cron: job execution timed out",
453465
detail: {
454466
kind: "cron-run",
455467
status: "skipped",

‎src/cron/service/task-runs.ts‎

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import type { DatabaseSync } from "node:sqlite";
55
import { normalizeOptionalLowercaseString } from "@openclaw/normalization-core/string-coerce";
66
import { normalizeAgentId, resolveAgentIdFromSessionKey } from "../../routing/session-key.js";
77
import { resolveCronJobEffectiveAgentId } from "../agent-id.js";
8-
import { isCronTimeoutErrorText } from "../execution-error-constants.js";
98

109
function requireCronAgentId(agentId: string | undefined): string {
1110
if (!agentId?.trim()) {
@@ -321,7 +320,10 @@ export function tryFinishCronTaskRunWithoutHistory(
321320
if (!result.taskRunId) {
322321
return;
323322
}
324-
const error = result.status === "error" ? normalizeCronRunErrorText(result.error) : undefined;
323+
const error =
324+
result.status !== "ok" && result.error !== undefined
325+
? normalizeCronRunErrorText(result.error)
326+
: undefined;
325327
const quietTriggerEval =
326328
result.triggerEval?.fired === false
327329
? { ...result.triggerEval, fired: false as const }
@@ -330,12 +332,7 @@ export function tryFinishCronTaskRunWithoutHistory(
330332
finalizeTaskRunByRunIdCore({
331333
runId: result.taskRunId,
332334
runtime: "cron",
333-
status:
334-
result.status === "ok" || result.status === "skipped"
335-
? "succeeded"
336-
: isCronTimeoutErrorText(error)
337-
? "timed_out"
338-
: "failed",
335+
status: cronRunStatusToTaskStatus({ status: result.status, error }),
339336
endedAt: result.endedAt,
340337
lastEventAt: result.endedAt,
341338
error,

‎src/cron/service/timer.regression.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2822,7 +2822,7 @@ describe("cron service timer regressions", () => {
28222822
outcome: "skip",
28232823
status: "skipped",
28242824
error: "agent skipped after removal",
2825-
taskStatus: "succeeded",
2825+
taskStatus: "failed",
28262826
},
28272827
] as const)(
28282828
"finalizes a removed job's $outcome outcome in operator history",

‎src/cron/task-run-detail.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,12 +262,12 @@ export function cronTaskRecordToScriptRunResult(
262262

263263
/** Maps the cron outcome vocabulary onto generic task terminal states. */
264264
export function cronRunStatusToTaskStatus(
265-
entry: CronRunLogEntry,
265+
entry: Pick<CronRunLogEntry, "status" | "error"> & Partial<CronRunLogEntry>,
266266
): Extract<TaskStatus, "succeeded" | "failed" | "timed_out"> {
267-
if (entry.status === "ok" || entry.status === "skipped") {
267+
if (entry.status === "ok") {
268268
return "succeeded";
269269
}
270-
return isCronTimeoutErrorText(entry.error) ? "timed_out" : "failed";
270+
return entry.status === "error" && isCronTimeoutErrorText(entry.error) ? "timed_out" : "failed";
271271
}
272272

273273
/** Reconstructs the unchanged CronRunLogEntry wire shape from a cron task row. */

‎src/gateway/server.cron.test.ts‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1632,6 +1632,67 @@ describe("gateway server cron", () => {
16321632
}
16331633
});
16341634

1635+
test("reports skipped isolated cron runs as failed tasks", async () => {
1636+
const { prevSkipCron } = await setupCronTestRun({
1637+
tempPrefix: "openclaw-gw-cron-run-skipped-task-",
1638+
cronEnabled: false,
1639+
});
1640+
cronIsolatedRun.mockResolvedValueOnce({
1641+
status: "skipped",
1642+
error: "model endpoint unavailable",
1643+
});
1644+
const { server, ws } = await startServerWithClient();
1645+
await connectOk(ws);
1646+
1647+
try {
1648+
const addRes = await rpcReq(ws, "cron.add", {
1649+
name: "skipped task projection",
1650+
enabled: true,
1651+
schedule: { kind: "every", everyMs: 60_000 },
1652+
sessionTarget: "isolated",
1653+
wakeMode: "next-heartbeat",
1654+
payload: { kind: "agentTurn", message: "do work" },
1655+
delivery: { mode: "none" },
1656+
});
1657+
const jobId = expectCronJobIdFromResponse(addRes);
1658+
const finished = waitForCronEvent(
1659+
ws,
1660+
(payload) => payload?.jobId === jobId && payload?.action === "finished",
1661+
);
1662+
1663+
await runCronJobForce(ws, jobId);
1664+
expect(await finished).toMatchObject({
1665+
jobId,
1666+
status: "skipped",
1667+
error: "model endpoint unavailable",
1668+
});
1669+
1670+
const history = await rpcReq(ws, "cron.runs", { id: jobId, limit: 1 });
1671+
expect(history.ok).toBe(true);
1672+
expect(history.payload).toMatchObject({
1673+
entries: [
1674+
expect.objectContaining({
1675+
jobId,
1676+
status: "skipped",
1677+
error: "model endpoint unavailable",
1678+
}),
1679+
],
1680+
});
1681+
1682+
const taskList = await rpcReq(ws, "tasks.list", {});
1683+
expect(taskList.ok).toBe(true);
1684+
const tasks = (taskList.payload as { tasks?: Array<Record<string, unknown>> } | undefined)
1685+
?.tasks;
1686+
expect(tasks?.find((task) => task.sourceId === jobId)).toMatchObject({
1687+
runtime: "cron",
1688+
status: "failed",
1689+
error: "model endpoint unavailable",
1690+
});
1691+
} finally {
1692+
await cleanupCronTestRun({ ws, server, prevSkipCron });
1693+
}
1694+
});
1695+
16351696
test("returns already-running without starting background work", async () => {
16361697
const now = Date.now();
16371698
let resolveRun: ((result: { status: "ok"; summary: string }) => void) | undefined;

0 commit comments

Comments
 (0)