Skip to content

Commit eef2967

Browse files
committed
fix(tasks): preserve canonical cron cancellation state
1 parent 404adc8 commit eef2967

5 files changed

Lines changed: 25 additions & 5 deletions

File tree

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ function requireCronAgentId(agentId: string | undefined): string {
1515
function resolveCurrentDefaultAgentId(state: CronServiceState): string | undefined {
1616
return state.deps.resolveDefaultAgentId?.() ?? state.deps.defaultAgentId;
1717
}
18+
import { CRON_TASK_KIND } from "../../tasks/cron-task-contract.js";
1819
import {
1920
createRunningTaskRun,
2021
finalizeTaskRunById,
@@ -237,6 +238,7 @@ function tryCreateCronTaskRunRecord(params: {
237238
: undefined;
238239
const task = createRunningTaskRun({
239240
runtime: "cron",
241+
taskKind: CRON_TASK_KIND,
240242
sourceId: params.jobId,
241243
ownerKey: "",
242244
scopeKind: "system",

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
} from "../../../test/helpers/cron/service-regression-fixtures.js";
1313
import { DEFAULT_CRON_MAX_CONCURRENT_RUNS } from "../../config/cron-limits.js";
1414
import { HEARTBEAT_SKIP_LANES_BUSY, type HeartbeatRunResult } from "../../infra/heartbeat-wake.js";
15+
import { CRON_TASK_KIND } from "../../tasks/cron-task-contract.js";
1516
import { cancelTaskById, listTaskRecords } from "../../tasks/task-registry.js";
1617
import {
1718
resetTaskRegistryControlRuntimeForTests,
@@ -751,6 +752,7 @@ describe("cron service timer regressions", () => {
751752
throw new Error("Expected timed-out cron task row");
752753
}
753754
expect(task.status).toBe("running");
755+
expect(task.taskKind).toBe(CRON_TASK_KIND);
754756

755757
installCronCancellationControlRuntime();
756758
const cancelResult = await cancelTaskById({

‎src/tasks/cron-task-contract.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/** Durable task kind stamped by the current cron task-ledger owner. */
2+
export const CRON_TASK_KIND = "automation_run";

‎src/tasks/task-registry-cancel.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { OpenClawConfig } from "../config/types.openclaw.js";
22
import { formatErrorMessage } from "../infra/errors.js";
33
import { isBackgroundExecTask } from "./background-exec-task-contract.js";
4+
import { CRON_TASK_KIND } from "./cron-task-contract.js";
45
import { SUBAGENT_KILL_TASK_ERROR } from "./detached-task-runtime-contract.js";
56
import { isHarnessOwnedSubagentTask } from "./harness-owned-subagent-task.js";
67
import { isProvisionalSubagentKillTask } from "./task-cancellation-state.js";
@@ -92,16 +93,16 @@ export async function cancelTaskById(params: {
9293
reason: params.reason?.trim() || "Cancelled by operator.",
9394
})
9495
) {
95-
if (childSessionKey) {
96+
if (task.taskKind === CRON_TASK_KIND || childSessionKey) {
9697
return {
9798
found: true,
9899
cancelled: false,
99100
reason: "Cron task has no active cancellation handle.",
100101
task: cloneTaskRecord(task),
101102
};
102103
}
103-
// Childless cron rows are stale legacy ledger records; with no live
104-
// runner handle and no child session to cancel, clear the task row.
104+
// Current rows carry taskKind before their runner publishes a child
105+
// session. Only an unmarked childless row is legacy cleanup state.
105106
}
106107
} else if (!childSessionKey) {
107108
if (!isHarnessOwnedSubagentTask(task)) {

‎src/tasks/task-registry.test.ts‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
import type { ParsedAgentSessionKey } from "../routing/session-key.js";
2727
import { withTempDir } from "../test-helpers/temp-dir.js";
2828
import { withEnvAsync } from "../test-utils/env.js";
29+
import { CRON_TASK_KIND } from "./cron-task-contract.js";
2930
import { SUBAGENT_KILL_TASK_ERROR } from "./detached-task-runtime-contract.js";
3031
import { ensureTaskRuntimeStateReady } from "./runtime-internal.js";
3132
import {
@@ -4775,25 +4776,37 @@ describe("task-registry", () => {
47754776

47764777
it.each([
47774778
{
4778-
name: "cancels stale cron tasks without an active runtime abort handle",
4779+
name: "cancels stale legacy childless cron tasks without an active runtime abort handle",
4780+
taskKind: undefined,
47794781
childSessionKey: undefined,
47804782
cancelled: true,
47814783
reason: undefined,
47824784
status: "cancelled",
47834785
error: "Cancelled by operator.",
47844786
},
4787+
{
4788+
name: "does not cancel canonical childless cron tasks without an active runtime abort handle",
4789+
taskKind: CRON_TASK_KIND,
4790+
childSessionKey: undefined,
4791+
cancelled: false,
4792+
reason: "Cron task has no active cancellation handle.",
4793+
status: "running",
4794+
error: undefined,
4795+
},
47854796
{
47864797
name: "does not mark session-backed cron tasks cancelled without an active runtime abort handle",
4798+
taskKind: undefined,
47874799
childSessionKey: "agent:main:cron:daily-repost",
47884800
cancelled: false,
47894801
reason: "Cron task has no active cancellation handle.",
47904802
status: "running",
47914803
error: undefined,
47924804
},
4793-
])("$name", async ({ childSessionKey, cancelled, reason, status, error }) => {
4805+
])("$name", async ({ taskKind, childSessionKey, cancelled, reason, status, error }) => {
47944806
await withTaskRegistryTempDir(async () => {
47954807
const task = createTaskFixture("cron", {
47964808
sourceId: "daily-repost",
4809+
taskKind,
47974810
ownerKey: "",
47984811
scopeKind: "system",
47994812
childSessionKey,

0 commit comments

Comments
 (0)