Skip to content

Commit ba8d349

Browse files
committed
fix(cron): reserve migrated heartbeat-task namespace
1 parent 2bc1699 commit ba8d349

5 files changed

Lines changed: 42 additions & 17 deletions

File tree

‎src/commands/doctor-heartbeat-task-migration.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ async function createExistingInboxJob(fixture: Awaited<ReturnType<typeof createF
140140
wakeMode: "next-heartbeat",
141141
state: { lastRunAtMs: fixture.nowMs - 10_000 },
142142
},
143-
{ enabledExplicit: true, matchesExisting: isHeartbeatTaskCronJob },
143+
{ enabledExplicit: true, systemOwned: true, matchesExisting: isHeartbeatTaskCronJob },
144144
);
145145
return structuredClone("job" in result ? result.job : result);
146146
}

‎src/cron/heartbeat-task.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ import type { CronJob } from "./types.js";
44

55
const HEARTBEAT_TASK_DECLARATION_PREFIX = "heartbeat-task:";
66

7+
/** Whether a declaration key belongs to the doctor-owned heartbeat-task namespace. */
8+
export function isHeartbeatTaskDeclarationKey(declarationKey: string | undefined): boolean {
9+
return declarationKey?.startsWith(HEARTBEAT_TASK_DECLARATION_PREFIX) === true;
10+
}
11+
712
/** Stable declaration identity; duplicate names add their deterministic occurrence ordinal. */
813
export function heartbeatTaskDeclarationKey(
914
agentId: string,
@@ -27,7 +32,7 @@ export function isHeartbeatTaskCronJob(job: CronJob): job is CronJob & {
2732
sessionTarget: "main";
2833
} {
2934
return (
30-
job.declarationKey?.startsWith(HEARTBEAT_TASK_DECLARATION_PREFIX) === true &&
35+
isHeartbeatTaskDeclarationKey(job.declarationKey) &&
3136
job.payload.kind === "systemEvent" &&
3237
job.sessionTarget === "main"
3338
);

‎src/cron/service.heartbeat-payload.test.ts‎

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,16 +89,33 @@ describe("heartbeat payload execution", () => {
8989
createStartedCronServiceWithFinishedBarrier({ storePath, logger: noopLogger });
9090
try {
9191
await cron.start();
92-
const added = await cron.add({
93-
declarationKey: heartbeatTaskDeclarationKey("main", "inbox"),
94-
name: "inbox",
95-
agentId: "main",
96-
enabled: true,
97-
schedule: { kind: "every", everyMs: 60_000 },
98-
payload: { kind: "systemEvent", text: "Check urgent inbox items" },
99-
sessionTarget: "main",
100-
wakeMode: "next-heartbeat",
101-
});
92+
const declarationKey = heartbeatTaskDeclarationKey("main", "inbox");
93+
await expect(
94+
cron.add({
95+
declarationKey,
96+
name: "ordinary automation",
97+
agentId: "main",
98+
enabled: true,
99+
schedule: { kind: "every", everyMs: 60_000 },
100+
payload: { kind: "systemEvent", text: "This must use normal cron delivery" },
101+
sessionTarget: "main",
102+
wakeMode: "next-heartbeat",
103+
}),
104+
).rejects.toThrow(/system-owned/);
105+
106+
const added = await cron.add(
107+
{
108+
declarationKey,
109+
name: "inbox",
110+
agentId: "main",
111+
enabled: true,
112+
schedule: { kind: "every", everyMs: 60_000 },
113+
payload: { kind: "systemEvent", text: "Check urgent inbox items" },
114+
sessionTarget: "main",
115+
wakeMode: "next-heartbeat",
116+
},
117+
{ systemOwned: true },
118+
);
102119
const job = "job" in added ? added.job : added;
103120

104121
await expect(cron.run(job.id, "force")).resolves.toMatchObject({ ok: true });

‎src/cron/service/ops-mutations.ts‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
onCronJobInactive,
1515
requestActiveCronJobCancellation,
1616
} from "../active-jobs.js";
17+
import { isHeartbeatTaskDeclarationKey } from "../heartbeat-task.js";
1718
import { cloneCronRuntimeAuthority, type CronRuntimeAuthority } from "../runtime-authority.js";
1819
import { cronSchedulingInputsEqual } from "../schedule-identity.js";
1920
import { removeCronJobBaseSession } from "../session-reaper.js";
@@ -303,12 +304,15 @@ export async function add(
303304
let pendingSessionCleanup: Promise<void> | undefined;
304305
return await locked(state, async () => {
305306
warnIfDisabled(state, "add");
306-
// Heartbeat monitors are gateway-converged system jobs; without this
307-
// boundary any internal caller could upsert the declaration key and
308-
// hijack the monitor despite the transport schemas excluding the kind.
307+
const declarationKey = normalizeOptionalString(input.declarationKey);
309308
if (input.payload?.kind === "heartbeat" && opts?.systemOwned !== true) {
310309
throw new Error("heartbeat payloads are system-owned; jobs cannot be created with them");
311310
}
311+
if (isHeartbeatTaskDeclarationKey(declarationKey) && opts?.systemOwned !== true) {
312+
throw new Error(
313+
'cron declarationKey namespace "heartbeat-task:" is system-owned; jobs cannot be created with it',
314+
);
315+
}
312316
await ensureLoaded(state, { skipRecompute: true });
313317
const agentId = resolveEffectiveJobAgentId(input, resolveCurrentDefaultAgentId(state));
314318
if (state.deps.isAgentAvailable?.(agentId) === false) {
@@ -326,7 +330,6 @@ export async function add(
326330
}
327331
}
328332
const normalizedInput = normalizedId ? { ...input, id: normalizedId } : input;
329-
const declarationKey = normalizeOptionalString(input.declarationKey);
330333
const matches = declarationKey
331334
? (state.store?.jobs.filter(
332335
(job) => job.declarationKey === declarationKey && (opts?.matchesExisting?.(job) ?? true),

‎src/cron/service/state.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ export type CronAddInput = CronJobCreate;
389389
export type CronAddOptions = {
390390
matchesExisting?: (job: CronJob) => boolean;
391391
enabledExplicit?: boolean;
392-
/** Gateway-owned system payloads (heartbeat monitors) require this opt-in. */
392+
/** Gateway/doctor-owned heartbeat jobs require this opt-in at service creation. */
393393
systemOwned?: boolean;
394394
/** Authenticated caller provenance stamped by the service, never public input. */
395395
scheduledToolPolicy?: CronScheduledToolPolicy;

0 commit comments

Comments
 (0)