Skip to content

Commit 37c83f7

Browse files
steipeteTakhoffman
authored andcommitted
fix(cron): preserve cancellation reason after late finalization
Keep the generic task cancellation outcome and reason authoritative while storing the underlying cron execution, completion, and delivery outcomes in cron-owned history detail. Co-authored-by: Tak Hoffman <[email protected]>
1 parent 637da87 commit 37c83f7

5 files changed

Lines changed: 48 additions & 16 deletions

File tree

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

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ describe("cron task run terminal records", () => {
412412
);
413413
});
414414

415-
it("keeps operator cancellation while attaching terminal run history", async () => {
415+
it("keeps operator cancellation reason when required delivery is interrupted", async () => {
416416
await withOpenClawTestState(
417417
{ layout: "state-only", prefix: "openclaw-cron-cancelled-task-" },
418418
async () => {
@@ -448,8 +448,7 @@ describe("cron task run terminal records", () => {
448448
runtime: "cron",
449449
status: "cancelled",
450450
endedAt: startedAt + 50,
451-
error: "cancelled by operator",
452-
terminalSummary: "Cancelled by operator.",
451+
error: "Cancelled by operator.",
453452
});
454453

455454
tryFinishCronTaskRun(state, {
@@ -460,6 +459,11 @@ describe("cron task run terminal records", () => {
460459
action: "finished",
461460
job,
462461
status: "ok",
462+
completionStatus: "failed",
463+
summary: "payload complete",
464+
delivered: false,
465+
deliveryStatus: "not-delivered",
466+
deliveryError: "cron webhook delivery cancelled: Cancelled by operator.",
463467
runAtMs: startedAt,
464468
durationMs: 100,
465469
},
@@ -472,9 +476,14 @@ describe("cron task run terminal records", () => {
472476
expect(row).toMatchObject({
473477
status: "cancelled",
474478
endedAt: startedAt + 100,
475-
detail: { kind: "cron-run", status: "ok", durationMs: 100 },
479+
error: "Cancelled by operator.",
480+
detail: {
481+
kind: "cron-run",
482+
status: "ok",
483+
completionStatus: "failed",
484+
durationMs: 100,
485+
},
476486
});
477-
expect(row?.error).toBeUndefined();
478487
expect(row?.terminalSummary).toBeUndefined();
479488
expect(
480489
readCronTaskRunHistoryPage({
@@ -485,7 +494,12 @@ describe("cron task run terminal records", () => {
485494
expect.objectContaining({
486495
jobId: job.id,
487496
status: "ok",
497+
completionStatus: "failed",
488498
error: undefined,
499+
summary: "payload complete",
500+
delivered: false,
501+
deliveryStatus: "not-delivered",
502+
deliveryError: "cron webhook delivery cancelled: Cancelled by operator.",
489503
}),
490504
]);
491505
},

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -427,18 +427,22 @@ export function tryFinishCronTaskRun(
427427
status,
428428
endedAt: entry.ts,
429429
lastEventAt: entry.ts,
430-
error: entry.error,
431-
clearError: entry.error === undefined,
432-
terminalSummary: entry.summary ?? null,
433-
preserveTerminalSummary: true,
430+
...(status === "cancelled"
431+
? {}
432+
: {
433+
error: entry.error,
434+
clearError: entry.error === undefined,
435+
terminalSummary: entry.summary ?? null,
436+
preserveTerminalSummary: true,
437+
}),
434438
childSessionKey: entry.sessionKey ?? null,
435439
detail,
436440
});
437441
let updated = finalize(taskRunId);
438442
if (updated.length === 0) {
439443
const existing = findTaskByRunId(taskRunId);
440444
if (existing?.runtime === "cron" && existing.status === "cancelled") {
441-
// Operator cancellation owns task status, but its finished event still owns history detail.
445+
// Operator cancellation owns task status and reason; its finished event owns history detail.
442446
updated = finalize(taskRunId, "cancelled");
443447
} else if (
444448
existing?.runtime === "cron" &&

‎src/cron/service/timer-job-runner.interruption.test.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,13 @@ describe("resolveInterruptedRunProgress", () => {
5050
job: webhookJob,
5151
error: "cron webhook delivery cancelled: operator",
5252
});
53-
expect(resolved?.delivered).toBe(false);
54-
expect(resolved?.deliveryError).toBe("cron webhook delivery cancelled: operator");
53+
expect(resolved).toMatchObject({
54+
status: "ok",
55+
summary: "payload",
56+
delivered: false,
57+
deliveryError: "cron webhook delivery cancelled: operator",
58+
});
59+
expect(resolved?.error).toBeUndefined();
5560
});
5661

5762
it("returns undefined when no core result completed", () => {

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ export function parseCronRunLogEntryObject(
196196
return entry;
197197
}
198198

199-
/** Encodes cron-only outcome fields; generic lifecycle fields stay on TaskRecord. */
199+
/** Encodes cron-owned outcome fields; the generic lifecycle projection stays on TaskRecord. */
200200
export function cronRunLogEntryToTaskDetail(
201201
entry: CronRunLogEntry,
202202
options: {
@@ -209,6 +209,8 @@ export function cronRunLogEntryToTaskDetail(
209209
kind: CRON_TASK_DETAIL_KIND,
210210
status: entry.status,
211211
completionStatus: entry.completionStatus,
212+
error: entry.error ?? null,
213+
summary: entry.summary ?? null,
212214
storeKey: options.storeKey,
213215
errorReason: entry.errorReason,
214216
diagnostics: entry.diagnostics,
@@ -330,13 +332,15 @@ export function cronTaskRecordToRunLogEntry(task: TaskRecord): CronRunLogEntry |
330332
// Task detail is canonical write-time state; history reads do not rederive error reasons.
331333
const entry = parseCronRunLogEntryObject(
332334
{
335+
// Released rows stored these only on the generic task; current detail wins
336+
// when task cancellation and the underlying execution have different outcomes.
337+
error: task.error,
338+
summary: task.terminalSummary,
333339
...wireDetail,
334340
ts: resolveCronTaskRecordTimestamp(task),
335341
jobId: task.sourceId,
336342
action: "finished",
337343
status: isCronRunStatus(task.detail.status) ? task.detail.status : undefined,
338-
error: task.error,
339-
summary: task.terminalSummary,
340344
sessionKey: task.childSessionKey,
341345
runId: typeof task.detail.runId === "string" ? task.detail.runId : undefined,
342346
},

‎src/cron/task-run-history.test.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,8 +480,12 @@ describe("cron task run history", () => {
480480
1,
481481
storeKey,
482482
);
483+
task.error = "legacy error";
484+
task.terminalSummary = "legacy summary";
483485
task.detail = {
484-
...(task.detail as Record<string, TaskRecord["detail"]>),
486+
kind: "cron-run",
487+
status: "ok",
488+
storeKey,
485489
internalFutureField: "secret",
486490
triggerState: { secret: true },
487491
delivery: "malformed",
@@ -492,6 +496,7 @@ describe("cron task run history", () => {
492496
expect(Object.hasOwn(entry ?? {}, "storeKey")).toBe(false);
493497
expect(Object.hasOwn(entry ?? {}, "internalFutureField")).toBe(false);
494498
expect(Object.hasOwn(entry ?? {}, "triggerState")).toBe(false);
499+
expect(entry).toMatchObject({ error: "legacy error", summary: "legacy summary" });
495500
expect(entry?.delivery).toBeUndefined();
496501
expect(entry?.failureNotificationDelivery).toBeUndefined();
497502
});

0 commit comments

Comments
 (0)