Skip to content

Commit 8ac01c3

Browse files
committed
fix(cron): preserve cancellation reason after late finalization
1 parent 248966d commit 8ac01c3

5 files changed

Lines changed: 39 additions & 17 deletions

File tree

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { afterEach, describe, expect, it, vi } from "vitest";
22
import { runOpenClawStateWriteTransaction } from "../../state/openclaw-state-db.js";
33
import { getDetachedTaskLifecycleRuntime } from "../../tasks/detached-task-runtime.js";
44
import * as taskExecutor from "../../tasks/task-executor.js";
5-
import { finalizeTaskRunByRunIdCore } from "../../tasks/task-executor.js";
65
import * as taskRegistry from "../../tasks/task-registry.js";
76
import { markTaskLostById } from "../../tasks/task-registry.js";
87
import { listTaskRegistryRecordsByRuntimeSourceIdFromSqlite } from "../../tasks/task-registry.store.sqlite.js";
@@ -412,7 +411,7 @@ describe("cron task run terminal records", () => {
412411
);
413412
});
414413

415-
it("keeps operator cancellation while attaching terminal run history", async () => {
414+
it("keeps operator cancellation reason when webhook delivery is interrupted", async () => {
416415
await withOpenClawTestState(
417416
{ layout: "state-only", prefix: "openclaw-cron-cancelled-task-" },
418417
async () => {
@@ -443,13 +442,12 @@ describe("cron task run terminal records", () => {
443442
if (!taskRunId) {
444443
throw new Error("expected cron task run id");
445444
}
446-
finalizeTaskRunByRunIdCore({
445+
taskExecutor.finalizeTaskRunByRunIdCore({
447446
runId: taskRunId,
448447
runtime: "cron",
449448
status: "cancelled",
450449
endedAt: startedAt + 50,
451-
error: "cancelled by operator",
452-
terminalSummary: "Cancelled by operator.",
450+
error: "Cancelled by operator.",
453451
});
454452

455453
tryFinishCronTaskRun(state, {
@@ -460,6 +458,9 @@ describe("cron task run terminal records", () => {
460458
action: "finished",
461459
job,
462460
status: "ok",
461+
summary: "payload complete",
462+
delivered: false,
463+
deliveryError: "cron webhook delivery cancelled: Cancelled by operator.",
463464
runAtMs: startedAt,
464465
durationMs: 100,
465466
},
@@ -472,9 +473,9 @@ describe("cron task run terminal records", () => {
472473
expect(row).toMatchObject({
473474
status: "cancelled",
474475
endedAt: startedAt + 100,
476+
error: "Cancelled by operator.",
475477
detail: { kind: "cron-run", status: "ok", durationMs: 100 },
476478
});
477-
expect(row?.error).toBeUndefined();
478479
expect(row?.terminalSummary).toBeUndefined();
479480
expect(
480481
readCronTaskRunHistoryPage({
@@ -486,6 +487,9 @@ describe("cron task run terminal records", () => {
486487
jobId: job.id,
487488
status: "ok",
488489
error: undefined,
490+
summary: "payload complete",
491+
delivered: false,
492+
deliveryError: "cron webhook delivery cancelled: Cancelled by operator.",
489493
}),
490494
]);
491495
},
@@ -810,7 +814,7 @@ describe("cron task run terminal records", () => {
810814
}),
811815
);
812816
expect(recovery.taskRunId).toBe(legacyRunId);
813-
finalizeTaskRunByRunIdCore({
817+
taskExecutor.finalizeTaskRunByRunIdCore({
814818
runId: legacyRunId,
815819
runtime: "cron",
816820
status: "timed_out",

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -365,18 +365,22 @@ export function tryFinishCronTaskRun(
365365
status,
366366
endedAt: entry.ts,
367367
lastEventAt: entry.ts,
368-
error: entry.error,
369-
clearError: entry.error === undefined,
370-
terminalSummary: entry.summary ?? null,
371-
preserveTerminalSummary: true,
368+
...(status === "cancelled"
369+
? {}
370+
: {
371+
error: entry.error,
372+
clearError: entry.error === undefined,
373+
terminalSummary: entry.summary ?? null,
374+
preserveTerminalSummary: true,
375+
}),
372376
childSessionKey: entry.sessionKey ?? null,
373377
detail,
374378
});
375379
let updated = finalize(taskRunId);
376380
if (updated.length === 0) {
377381
const existing = findTaskByRunId(taskRunId);
378382
if (existing?.runtime === "cron" && existing.status === "cancelled") {
379-
// Operator cancellation owns task status, but its finished event still owns history detail.
383+
// Operator cancellation owns task status and reason; its finished event owns history detail.
380384
updated = finalize(taskRunId, "cancelled");
381385
} else if (
382386
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: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ export function cronRunLogEntryToTaskDetail(
198198
const detail = toJsonValue({
199199
kind: CRON_TASK_DETAIL_KIND,
200200
status: entry.status,
201+
error: entry.error ?? null,
202+
summary: entry.summary ?? null,
201203
storeKey: options.storeKey,
202204
errorReason: entry.errorReason,
203205
diagnostics: entry.diagnostics,
@@ -312,13 +314,15 @@ export function cronTaskRecordToRunLogEntry(task: TaskRecord): CronRunLogEntry |
312314
// Task detail is canonical write-time state; history reads do not rederive error reasons.
313315
const entry = parseCronRunLogEntryObject(
314316
{
317+
// Released rows stored these only on the generic task; current detail wins
318+
// when task cancellation and the underlying execution have different outcomes.
319+
error: task.error,
320+
summary: task.terminalSummary,
315321
...wireDetail,
316322
ts: resolveCronTaskRecordTimestamp(task),
317323
jobId: task.sourceId,
318324
action: "finished",
319325
status: isCronRunStatus(task.detail.status) ? task.detail.status : undefined,
320-
error: task.error,
321-
summary: task.terminalSummary,
322326
sessionKey: task.childSessionKey,
323327
runId: typeof task.detail.runId === "string" ? task.detail.runId : undefined,
324328
},

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,8 +451,12 @@ describe("cron task run history", () => {
451451
1,
452452
storeKey,
453453
);
454+
task.error = "legacy error";
455+
task.terminalSummary = "legacy summary";
454456
task.detail = {
455-
...(task.detail as Record<string, TaskRecord["detail"]>),
457+
kind: "cron-run",
458+
status: "ok",
459+
storeKey,
456460
internalFutureField: "secret",
457461
triggerState: { secret: true },
458462
delivery: "malformed",
@@ -463,6 +467,7 @@ describe("cron task run history", () => {
463467
expect(Object.hasOwn(entry ?? {}, "storeKey")).toBe(false);
464468
expect(Object.hasOwn(entry ?? {}, "internalFutureField")).toBe(false);
465469
expect(Object.hasOwn(entry ?? {}, "triggerState")).toBe(false);
470+
expect(entry).toMatchObject({ error: "legacy error", summary: "legacy summary" });
466471
expect(entry?.delivery).toBeUndefined();
467472
expect(entry?.failureNotificationDelivery).toBeUndefined();
468473
});

0 commit comments

Comments
 (0)