Skip to content

Commit cce5e64

Browse files
committed
test(cron): prove durable on-exit finalization
1 parent b79929d commit cce5e64

2 files changed

Lines changed: 65 additions & 64 deletions

File tree

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

Lines changed: 63 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
tryBeginGatewayRootWorkAdmission,
2323
} from "../../process/gateway-work-admission.js";
2424
import { CommandLane } from "../../process/lanes.js";
25-
import { saveCronStore } from "../store.js";
25+
import { loadCronStore, saveCronStore } from "../store.js";
2626
import { enqueueRun, remove, run, start } from "./ops.js";
2727
import type { CronEvent } from "./state.js";
2828
import { createCronServiceState } from "./state.js";
@@ -795,70 +795,74 @@ describe("cron service ops regressions", () => {
795795

796796
clearCommandLane(CommandLane.Cron);
797797
});
798-
it("#104518: deletes a successful on-exit deleteAfterRun job instead of retaining it disabled", async () => {
799-
const runScenario = async (params: {
800-
id: string;
801-
deleteAfterRun: boolean;
802-
runStatus: "ok" | "error";
803-
}) => {
804-
const store = opsRegressionFixtures.makeStorePath();
805-
const nowMs = Date.now();
806-
const job = createIsolatedRegressionJob({
807-
id: params.id,
808-
name: params.id,
809-
scheduledAt: nowMs,
810-
schedule: { kind: "on-exit", command: 'sh -c "exit 0"' },
811-
payload: { kind: "agentTurn", message: "post-exit payload" },
812-
state: {},
813-
});
814-
job.deleteAfterRun = params.deleteAfterRun;
815-
// The gateway exit watcher durably disables the job BEFORE firing
816-
// (replay protection); the fire itself is a force run on the disabled
817-
// job, mirrored here.
818-
job.enabled = false;
819-
await saveCronStore(store.storePath, { version: 1, jobs: [job] });
820-
821-
const state = createCronServiceState({
822-
cronEnabled: false,
823-
storePath: store.storePath,
824-
log: noopLogger,
825-
enqueueSystemEvent: vi.fn(),
826-
requestHeartbeat: vi.fn(),
827-
runIsolatedAgentJob:
828-
params.runStatus === "ok"
829-
? vi.fn().mockResolvedValue({ status: "ok", summary: "ok" })
830-
: vi.fn().mockResolvedValue({ status: "error", error: "boom" }),
831-
});
832-
const result = await run(state, params.id, "force");
833-
expect(result).toEqual({ ok: true, ran: true });
834-
return state.store?.jobs.find((entry) => entry.id === params.id);
835-
};
836-
837-
// Successful deleteAfterRun on-exit job: deleted, like one-shot "at" jobs.
838-
const deleted = await runScenario({
798+
it.each([
799+
{
839800
id: "onexit-delete-ok",
840801
deleteAfterRun: true,
841-
runStatus: "ok",
842-
});
843-
expect(deleted).toBeUndefined();
844-
845-
// Without deleteAfterRun the job stays, durably disabled by the pre-fire
846-
// transition (persistence-order contract unchanged).
847-
const retained = await runScenario({
802+
runStatus: "ok" as const,
803+
expectedJob: undefined,
804+
expectedActions: ["started", "finished", "removed"],
805+
},
806+
{
848807
id: "onexit-keep-ok",
849808
deleteAfterRun: false,
850-
runStatus: "ok",
851-
});
852-
expect(retained?.enabled).toBe(false);
853-
expect(retained?.state.lastStatus).toBe("ok");
854-
855-
// Failed runs are retained for inspection even with deleteAfterRun.
856-
const failed = await runScenario({
809+
runStatus: "ok" as const,
810+
expectedJob: { enabled: false, lastStatus: "ok" },
811+
expectedActions: ["started", "finished"],
812+
},
813+
{
857814
id: "onexit-delete-error",
858815
deleteAfterRun: true,
859-
runStatus: "error",
816+
runStatus: "error" as const,
817+
expectedJob: { enabled: false, lastStatus: "error" },
818+
expectedActions: ["started", "finished"],
819+
},
820+
])("#104518 finalizes watcher-fired on-exit job: $id", async (params) => {
821+
const store = opsRegressionFixtures.makeStorePath();
822+
const nowMs = Date.now();
823+
const job = createIsolatedRegressionJob({
824+
id: params.id,
825+
name: params.id,
826+
scheduledAt: nowMs,
827+
schedule: { kind: "on-exit", command: 'sh -c "exit 0"' },
828+
payload: { kind: "agentTurn", message: "post-exit payload" },
829+
state: {},
830+
});
831+
job.deleteAfterRun = params.deleteAfterRun;
832+
// The gateway watcher persists this disable before force-running the payload.
833+
job.enabled = false;
834+
await saveCronStore(store.storePath, { version: 1, jobs: [job] });
835+
836+
const events: CronEvent[] = [];
837+
const state = createCronServiceState({
838+
cronEnabled: false,
839+
storePath: store.storePath,
840+
log: noopLogger,
841+
enqueueSystemEvent: vi.fn(),
842+
requestHeartbeat: vi.fn(),
843+
runIsolatedAgentJob:
844+
params.runStatus === "ok"
845+
? vi.fn().mockResolvedValue({ status: "ok", summary: "ok" })
846+
: vi.fn().mockResolvedValue({ status: "error", error: "boom" }),
847+
onEvent: (event) => events.push(event),
860848
});
861-
expect(failed?.enabled).toBe(false);
862-
expect(failed?.state.lastStatus).toBe("error");
849+
await expect(run(state, params.id, "force")).resolves.toEqual({ ok: true, ran: true });
850+
851+
const memoryJob = state.store?.jobs.find((entry) => entry.id === params.id);
852+
const durableJob = (await loadCronStore(store.storePath)).jobs.find(
853+
(entry) => entry.id === params.id,
854+
);
855+
if (params.expectedJob) {
856+
for (const persistedJob of [memoryJob, durableJob]) {
857+
expect(persistedJob).toMatchObject({
858+
enabled: params.expectedJob.enabled,
859+
state: { lastStatus: params.expectedJob.lastStatus },
860+
});
861+
}
862+
} else {
863+
expect(memoryJob).toBeUndefined();
864+
expect(durableJob).toBeUndefined();
865+
}
866+
expect(events.map((event) => event.action)).toEqual(params.expectedActions);
863867
});
864868
});

‎src/cron/service/timer.ts‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -803,11 +803,8 @@ export function applyJobResult(
803803
job.state.lastFailureAlertAtMs = undefined;
804804
}
805805

806-
// deleteAfterRun finalizes every one-shot schedule kind. On-exit jobs are
807-
// durably disabled BEFORE their payload fires (gateway watcher replay
808-
// protection), so successful deletion here is the terminal transition that
809-
// actually removes them; restricting it to "at" left them behind as
810-
// disabled jobs (#104518).
806+
// The gateway watcher disables on-exit jobs before firing; successful removal here
807+
// completes the same deleteAfterRun contract as a one-shot at schedule.
811808
const isOneShotSchedule = job.schedule.kind === "at" || job.schedule.kind === "on-exit";
812809
const shouldDelete = isOneShotSchedule && job.deleteAfterRun === true && result.status === "ok";
813810
const retryDisabledHeartbeatOneShot = shouldRetryDisabledHeartbeatOneShot(job, result);

0 commit comments

Comments
 (0)