Skip to content

Commit 4c4e37c

Browse files
committed
fix(cron): keep armed on-exit jobs enabled after non-delete force runs
1 parent 2357a0f commit 4c4e37c

3 files changed

Lines changed: 94 additions & 8 deletions

File tree

‎docs/automation/cron-jobs.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ Cron is the Gateway's built-in scheduler. It persists jobs, wakes the agent at t
7272
| `cron` | `--cron` | 5-field or 6-field cron expression with optional `--tz` |
7373
| `on-exit` | `--on-exit` | Fire once when a watched command exits (event trigger; survives turn teardown; optional `--on-exit-cwd`) |
7474

75-
`on-exit` jobs honor `--delete-after-run` / `--keep-after-run` the same way as successful one-shot `--at` jobs: after a successful payload run, delete when requested; otherwise keep the job disabled (the Gateway exit watcher disables the job before the payload runs so a restart cannot re-arm the watched command). Failed payload runs are never auto-deleted.
75+
`on-exit` jobs honor successful `--delete-after-run` the same way as one-shot `--at` jobs: after a successful payload run, the job is removed when deletion was requested. The Gateway exit watcher still disables the job **before** the payload runs so a restart cannot re-arm the watched command; failed payloads and keep-after-run outcomes stay in the store (watcher-disabled when fired by the watcher). Operator-triggered force runs do not terminalize an armed `on-exit` job.
7676

7777
Timestamps without a timezone are treated as UTC. Add `--tz America/New_York` to interpret an offset-less `--at` datetime, or to evaluate a cron expression, in that IANA timezone. Cron expressions without `--tz` use the Gateway host timezone. `--tz` is not valid with `--every` or `--on-exit`.
7878

‎src/cron/service.removal-postcommit.test.ts‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,23 @@ function createCompletedOnExitJob(id: string, nowMs: number, deleteAfterRun: boo
5252
};
5353
}
5454

55+
/** Armed on-exit job as an operator might force-run before the watched process exits. */
56+
function createArmedOnExitJob(id: string, nowMs: number, deleteAfterRun: boolean): CronJob {
57+
return {
58+
id,
59+
name: `on-exit armed ${id}`,
60+
enabled: true,
61+
deleteAfterRun,
62+
createdAtMs: nowMs - 60_000,
63+
updatedAtMs: nowMs - 1_000,
64+
schedule: { kind: "on-exit", command: "sh -c 'exit 0'" },
65+
sessionTarget: "isolated",
66+
wakeMode: "next-heartbeat",
67+
payload: { kind: "agentTurn", message: "do work" },
68+
state: {},
69+
};
70+
}
71+
5572
function createState(params: {
5673
storePath: string;
5774
nowMs: number;
@@ -349,4 +366,76 @@ describe("cron on-exit deleteAfterRun finalization (#104518)", () => {
349366
clearStateTimer(state);
350367
}
351368
});
369+
370+
it("keeps an armed on-exit job enabled after manual keep-after-run success", async () => {
371+
const { storePath } = await makeStorePath();
372+
const nowMs = Date.parse("2026-07-10T12:00:00.000Z");
373+
const job = createArmedOnExitJob("on-exit-armed-keep-ok", nowMs, false);
374+
await saveCronStore(storePath, { version: 1, jobs: [job] });
375+
376+
const events: CronEvent[] = [];
377+
const state = createState({
378+
storePath,
379+
nowMs,
380+
onEvent: (event) => events.push(structuredClone(event)),
381+
});
382+
383+
try {
384+
await run(state, job.id, "force");
385+
386+
expect(events.map((event) => event.action)).toEqual(["started", "finished"]);
387+
expect(events.some((event) => event.action === "removed")).toBe(false);
388+
const listed = await list(state, { includeDisabled: true });
389+
expect(listed).toEqual([
390+
expect.objectContaining({
391+
id: job.id,
392+
enabled: true,
393+
deleteAfterRun: false,
394+
state: expect.objectContaining({ lastRunStatus: "ok" }),
395+
}),
396+
]);
397+
} finally {
398+
clearStateTimer(state);
399+
}
400+
});
401+
402+
it("keeps an armed on-exit job enabled after manual force-run failure", async () => {
403+
const { storePath } = await makeStorePath();
404+
const nowMs = Date.parse("2026-07-10T12:00:00.000Z");
405+
const job = createArmedOnExitJob("on-exit-armed-fail", nowMs, true);
406+
await saveCronStore(storePath, { version: 1, jobs: [job] });
407+
408+
const events: CronEvent[] = [];
409+
const state = createCronServiceState({
410+
storePath,
411+
cronEnabled: true,
412+
log: logger,
413+
nowMs: () => nowMs,
414+
enqueueSystemEvent: vi.fn(),
415+
requestHeartbeat: vi.fn(),
416+
runIsolatedAgentJob: vi.fn(async () => ({
417+
status: "error" as const,
418+
error: "payload failed",
419+
summary: "fail",
420+
})),
421+
onEvent: (event) => events.push(structuredClone(event)),
422+
});
423+
424+
try {
425+
await run(state, job.id, "force");
426+
427+
expect(events.some((event) => event.action === "removed")).toBe(false);
428+
const listed = await list(state, { includeDisabled: true });
429+
expect(listed).toEqual([
430+
expect.objectContaining({
431+
id: job.id,
432+
enabled: true,
433+
deleteAfterRun: true,
434+
state: expect.objectContaining({ lastRunStatus: "error" }),
435+
}),
436+
]);
437+
} finally {
438+
clearStateTimer(state);
439+
}
440+
});
352441
});

‎src/cron/service/timer.ts‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -891,13 +891,10 @@ export function applyJobResult(
891891
);
892892
}
893893
}
894-
} else if (job.schedule.kind === "on-exit") {
895-
// Fire-once event schedule: after a payload run, never re-arm via timer.
896-
// Exit watchers also disable pre-fire so a Gateway restart cannot re-run
897-
// the watched command; keep that terminal retention when deletion is off
898-
// or the payload did not succeed.
899-
job.enabled = false;
900-
job.state.nextRunAtMs = undefined;
894+
// on-exit is intentionally omitted: the Gateway exit watcher already
895+
// disables before payload fire. Operator force runs share this finalizer
896+
// without a watcher-origin fact, so non-delete on-exit results must not
897+
// cancel still-armed watches. Successful deleteAfterRun uses shouldDelete.
901898
} else if (result.status === "error" && isJobEnabled(job)) {
902899
const retryDecision = resolveTransientCronRetryDecision({
903900
cronConfig: state.deps.cronConfig,

0 commit comments

Comments
 (0)