diff --git a/packages/coding-agent/src/features/step-cron.ts b/packages/coding-agent/src/features/step-cron.ts index 5f7bfc36..3b847fe6 100644 --- a/packages/coding-agent/src/features/step-cron.ts +++ b/packages/coding-agent/src/features/step-cron.ts @@ -4,6 +4,7 @@ import { createHash, randomUUID } from "node:crypto"; import { closeSync, copyFileSync, + existsSync, mkdirSync, openSync, readFileSync, @@ -60,8 +61,11 @@ export type CronStatusReason = "session_missing"; export type CronRunStatus = "delivered" | "failed" | "missed" | "skipped" | "blocked"; /** - * A scheduled job. `schemaVersion: 1` is the original CLI record; v2 adds the - * optional desktop fields below. The CLI ignores fields it does not use. + * A scheduled job. `schemaVersion: 1` is the original record; v2 adds the + * optional host fields below (session binding, status, run accounting). Either + * host may write v2 now: the CLI binds a task to its session, which is one of + * those fields. v1 records still load, and are only ever written by an older + * build. */ export interface CronJob { schemaVersion: 1 | 2; @@ -80,6 +84,16 @@ export interface CronJob { autoExpireAt?: number; workspace?: string; sessionId?: string; + /** + * Absolute path of the bound session's file. + * + * This is the record's proof of which conversation it belongs to: a session id + * alone cannot say whether that conversation still exists, and guessing from a + * directory listing was wrong for `--session `, a custom session + * directory, and `--no-session`. When the file is gone the task can never run + * again, which is what `pauseMissingSessions` checks. + */ + sessionFile?: string; /** * Set when the host created the bound session for this task rather than * binding a session the user already had. The sidebar uses it to place the @@ -109,6 +123,7 @@ export interface CronCreateOptions { id?: string; workspace?: string; sessionId?: string; + sessionFile?: string; sessionCreated?: boolean; title?: string; status?: CronJobStatus; @@ -425,6 +440,11 @@ function parsePersistedJob(value: unknown): CronJob | undefined { return undefined; if (candidate.sessionId !== undefined && (typeof candidate.sessionId !== "string" || !candidate.sessionId.trim())) return undefined; + if ( + candidate.sessionFile !== undefined && + (typeof candidate.sessionFile !== "string" || !candidate.sessionFile.trim()) + ) + return undefined; if ( candidate.title !== undefined && (typeof candidate.title !== "string" || candidate.title.length > CRON_MAX_TITLE_LENGTH) @@ -518,8 +538,11 @@ export interface StepCronRuntimeOptions { * Which records this host delivers and may change. * * Durable records are shared by file path, so a project folder can hold a - * record another host owns. Unowned records are still read and written back - * unchanged; they are only never fired, listed or mutated here. + * record another host owns. An unowned record is never fired, listed or + * updated for its owner's sake. Two exceptions exist, both for records no + * session can ever claim again: `delete` may remove them, and + * `pauseMissingSessions` marks one whose bound conversation is gone so a + * reader can find and clear it. */ ownsJob?: (job: CronJob) => boolean; } @@ -541,6 +564,18 @@ export interface CronRuntimeLike { update?(id: string, patch: CronUpdatePatch): CronJob; setEnabled?(id: string, enabled: boolean): boolean; runNow?(id: string): CronJob; + /** + * Pause records whose bound session no longer exists, and make them deletable. + * + * A task belongs to one conversation. When that conversation is gone — the file + * was deleted, or the reader moved to another machine — nothing can ever claim + * the record again: it is never listed, never fired, and its expiry only runs + * for records this host owns. Without this it stayed in the file forever. + * + * `exists` answers whether a path is still there; it is given the record's + * recorded session file, not an id. + */ + pauseMissingSessions?(exists: (path: string) => boolean): void; onTurnEnd(): void; stop(): void; } @@ -662,7 +697,10 @@ export class StepCronRuntime { const nominalNext = (!recurring ? options.runAt : undefined) ?? (options.intervalHours ? createdAt + options.intervalHours * 60 * 60 * 1000 : expression.next(createdAt)); - const desktopFields = + // A record is v2 when it carries any of the fields v1 did not have. The + // session binding counts: that is what makes a task belong to one + // conversation. + const hasV2Fields = options.workspace !== undefined || options.sessionId !== undefined || options.title !== undefined || @@ -684,7 +722,13 @@ export class StepCronRuntime { ? createdAt + (options.autoExpireMs ?? AUTO_EXPIRE_MS) : undefined; const job = this.withLatestJobs(() => { - if (this.jobs.size >= this.maxJobs) + // Count only the records this host owns. The file is shared, so counting + // every row let another session's tasks — or records written before tasks + // were bound — fill the quota while remaining invisible and undeletable + // here: a reader could neither schedule a new task nor clear the ones in + // the way. + const owned = [...this.jobs.values()].filter((candidate) => this.ownsJob(candidate)).length; + if (owned >= this.maxJobs) throw new Error(`At most ${this.maxJobs} cron jobs can be scheduled; delete an existing job first`); const requestedId = options.id?.trim(); if (requestedId !== undefined && !/^[A-Za-z0-9_-]{1,64}$/u.test(requestedId)) @@ -692,7 +736,7 @@ export class StepCronRuntime { const id = requestedId || this.uniqueId(); if (this.jobs.has(id)) throw new Error(`Cron job id ${id} already exists`); const next: CronJob = { - schemaVersion: desktopFields ? 2 : 1, + schemaVersion: hasV2Fields ? 2 : 1, id, cron: normalizedCron, prompt: normalizedPrompt, @@ -704,6 +748,7 @@ export class StepCronRuntime { nextFireAt: this.jitter(nominalNext, createdAt, id, recurring, expression, options.jitter !== false), ...(options.workspace ? { workspace: path.resolve(options.workspace) } : {}), ...(options.sessionId ? { sessionId: options.sessionId } : {}), + ...(options.sessionFile ? { sessionFile: path.resolve(options.sessionFile) } : {}), ...(options.sessionCreated ? { sessionCreated: true } : {}), ...(options.title ? { title: options.title.slice(0, CRON_MAX_TITLE_LENGTH) } : {}), ...(options.status ? { status: options.status } : {}), @@ -887,7 +932,19 @@ export class StepCronRuntime { delete(id: string): boolean { const found = this.withLatestJobs(() => { - if (!this.jobs.has(id) || !this.ownsJob(this.jobs.get(id)!)) return false; + const job = this.jobs.get(id); + if (!job) return false; + // This session's own task, or one nothing can ever run again. A record + // bound to a live session stays: deleting it would remove a task that + // conversation is still going to run. + // + // Two kinds of record have no owner left and are accepted: no `sessionId` + // (written before tasks were bound), and `session_missing` (its + // conversation's file is gone). A record with an id but no file belongs to + // a host that keeps none (`--no-session`); its owner deletes it through + // `ownsJob`, and anyone else leaves it alone, because another host's + // records lack a file too. + if (!this.ownsJob(job) && job.sessionId !== undefined && job.statusReason !== "session_missing") return false; this.removeJob(id); return true; }); @@ -895,6 +952,44 @@ export class StepCronRuntime { return found; } + /** + * Pause records whose bound session file is gone, and let them be deleted. + * + * A task belongs to one conversation. Once that conversation's file no longer + * exists nothing can claim the record: it is never listed, never fired, and its + * expiry is only applied to records this host owns. Marking it + * `session_missing` makes `delete` accept it, so it does not accumulate + * forever. + * + * The test is the file the record names, not a directory listing: a session id + * is not a location, and `--session `, a custom session directory and + * `--no-session` all put (or keep) the file somewhere a scan of the default + * directory would miss. Guessing from a listing marked live sessions' own tasks + * as gone, which no host could undo. + * + * A record with no `sessionFile` cannot be verified — there is nothing to check + * — so it is left alone rather than guessed at. Such a record has no owner any + * host can identify either, which makes it permanently invisible and + * undeletable; `delete` accepts it for that reason. + */ + pauseMissingSessions(exists: (path: string) => boolean): void { + const missing = [...this.jobs.values()].filter( + (job) => job.durable && job.sessionFile !== undefined && !exists(job.sessionFile), + ); + if (missing.length === 0) return; + this.withLatestJobs(() => { + for (const job of missing) { + const current = this.jobs.get(job.id); + // A finished or expired record keeps its own outcome: it is not a lost + // session, and overwriting the status would hide why it stopped. + if (!current || current.statusReason === "session_missing") continue; + if (current.status === "expired" || current.status === "completed") continue; + current.status = "paused"; + current.statusReason = "session_missing"; + } + }); + } + tick(): void { try { if (this.recoveryPending) { @@ -941,7 +1036,7 @@ export class StepCronRuntime { const now = this.readNow(); if ( [...this.jobs.values()].some((job) => { - if (!job.durable) return false; + if (!job.durable || !this.ownsJob(job)) return false; const expires = this.expirationOf(job); return job.nextFireAt <= now || (expires !== undefined && expires <= now); }) @@ -950,6 +1045,11 @@ export class StepCronRuntime { // Import the complete file before any delivery can change persisted state. for (const job of [...this.jobs.values()]) { if (!job.durable) continue; + // A record another session owns is read and left untouched: its + // schedule, its expiry and its missed notice are that session's to + // advance. Recovering it here would deliver it into this + // conversation, which is the cross-session defect itself. + if (!this.ownsJob(job)) continue; if (job.status !== undefined && job.status !== "enabled") continue; const expires = this.expirationOf(job); if (expires !== undefined && expires <= now) { @@ -1048,7 +1148,12 @@ export class StepCronRuntime { * every one-second tick just to re-mark the same jobs deferred. */ private needsLockedDrain(now: number): boolean { - if (this.missedAllowed() && this.pendingMissed.size > 0) return true; + if (this.missedAllowed()) { + for (const id of this.pendingMissed) { + const job = this.jobs.get(id); + if (job && this.ownsJob(job)) return true; + } + } for (const job of this.jobs.values()) { if (!this.ownsJob(job)) continue; if (job.status !== undefined && job.status !== "enabled") continue; @@ -1069,6 +1174,13 @@ export class StepCronRuntime { private deliverMissed(): void { if (this.pendingMissed.size === 0 || !this.missedAllowed()) return; + // Drop the ids this session does not own instead of leaving them queued: a + // queue that can never drain made every tick take the storage lock to look + // again, and the lock is shared with every other session in the folder. + for (const id of [...this.pendingMissed]) { + const job = this.jobs.get(id); + if (!job || !this.ownsJob(job)) this.pendingMissed.delete(id); + } const jobs = [...this.pendingMissed].flatMap((id) => { const job = this.jobs.get(id); return job && this.ownsJob(job) ? [{ ...job }] : []; @@ -1433,12 +1545,29 @@ export function createStepCronExtension(options: StepCronExtensionOptions = {}): options.runtime ?? new StepCronRuntime({ telemetry: options.telemetry, - // A durable record bound to a session belongs to the host that owns - // that session. A CLI session loading the same project folder must not - // fire, list, or delete it: it has no way to see that session, and - // firing it would deliver a desktop task's prompt into this terminal - // conversation instead. - ownsJob: (job) => job.sessionId === undefined, + // A task belongs to the session that created it. + // + // Durable records live in the project folder, so every session opened + // there reads the same file. A job with a `sessionId` belongs to exactly + // that session; a CLI session opening the same folder must not fire, + // list or delete it — it has no way to see that conversation, and firing + // it would deliver the prompt into whichever terminal happens to be + // open. That was the behaviour for records with no `sessionId`, which + // is why one session's scheduled task used to fire in every other one. + // + // A record with no `sessionId` predates this rule (or was written by a + // host that does not bind tasks). Nothing claims it: it is never fired, + // and `cron_list` does not show it. Leaving it unlisted is the only + // outcome that cannot deliver it to a conversation it does not belong + // to. Such a record stays deletable, so it is not stranded. + ownsJob: (job) => { + // No readable session id means this host cannot attribute a task at + // all, so it claims none. Never `true`: that would fall back to + // "everything is mine", the cross-session defect itself. + const sessionId = withLiveContext(currentContext, (ctx) => ctx.sessionManager.getSessionId()); + if (!sessionId) return false; + return job.sessionId !== undefined && job.sessionId === sessionId; + }, isIdle: () => Boolean( withLiveContext( @@ -1496,6 +1625,22 @@ export function createStepCronExtension(options: StepCronExtensionOptions = {}): pi.on("session_start", (_event, ctx) => { setContext(_event, ctx); runtime.start(ctx.cwd, ctx.isProjectTrusted()); + // A task whose bound session file is gone can never run again. Marking + // it lets it be deleted instead of sitting in the shared file forever. + // The record names its own file, so this needs no directory scan and + // cannot mistake a live session's task for a lost one. + // + // A lock held by another process would throw here; startup recovery must + // not fail because of a housekeeping pass, so it is skipped and retried on + // the next start. + try { + runtime.pauseMissingSessions?.((file) => existsSync(file)); + } catch (error) { + currentContext?.ui.notify( + `Unable to check scheduled tasks: ${error instanceof Error ? error.message : String(error)}`, + "warning", + ); + } }); pi.on("turn_end", (_event, ctx) => { setContext(_event, ctx); @@ -1513,7 +1658,7 @@ export function createStepCronExtension(options: StepCronExtensionOptions = {}): pi.registerTool({ name: "cron_create", label: "Create cron job", - description: `Schedule a five-field local-time cron job (up to 50 jobs). Jobs fire only while StepCode is running and idle, after pending user input. Durable jobs (durable:true) persist under .stepcode/cron and survive session restarts; session jobs die with the process. Recurring jobs (recurring:true, default) expire after seven days as a runaway guard. Invalid expressions are rejected before the tool records anything. + description: `Schedule a five-field local-time cron job (up to 50 jobs). Jobs fire only while StepCode is running and idle, after pending user input. Durable jobs (durable:true) persist under .stepcode/cron and belong to the conversation that created them: they keep firing after a restart of that same conversation (--resume / --continue) and stay invisible to other conversations in the same folder. Session jobs die with the process. Recurring jobs (recurring:true, default) expire after seven days as a runaway guard. Invalid expressions are rejected before the tool records anything. When NOT to use: cron_create is for calendar or expression-based triggers that repeat or must survive a restart. For fanning work out to many subagents on schedule, cron_create only injects a follow-up prompt on trigger; the subsequent turn is where you would call workflow (with its own opt-in). For a multi-turn objective that should persist without a fixed calendar, use create_goal instead. @@ -1527,7 +1672,24 @@ Scheduling: recurring jobs get a stable task-ID offset of up to 30 minutes late, runtime.setContext(ctx.cwd); const recurring = params.recurring !== false; const durable = params.durable === true; - const result = runtime.create(params.cron, params.prompt, recurring, durable, ctx.isProjectTrusted()); + // Bind the job to this conversation. The record lives in the project + // folder, which every session opened there shares; without this the job + // fired in whichever one happened to be active. + // + // `origin: "agent"` records who created it. Nothing reads it today — + // the desktop host keeps a separate store and never sees this file — so + // it is a description, not a mechanism. A host that ever reads these + // records can use it to tell them from its own. + // The file is the record's proof of which conversation it belongs to; a + // later start checks it instead of guessing from an id. A host without + // one (`--no-session`) records only the id, and its tasks are simply + // not checked for a lost session. + const sessionFile = ctx.sessionManager.getSessionFile?.(); + const result = runtime.create(params.cron, params.prompt, recurring, durable, ctx.isProjectTrusted(), { + sessionId: ctx.sessionManager.getSessionId(), + ...(sessionFile ? { sessionFile } : {}), + origin: "agent", + }); return jsonResult({ ...result.job, nextFireAt: result.nextFireAt }); }, }); diff --git a/packages/coding-agent/src/step/system-prompt.ts b/packages/coding-agent/src/step/system-prompt.ts index b6eb00fc..8840d281 100644 --- a/packages/coding-agent/src/step/system-prompt.ts +++ b/packages/coding-agent/src/step/system-prompt.ts @@ -170,7 +170,7 @@ export function buildStepSystemPromptAppendix( ? [ "# Scheduled work", [ - "- Use cron_create for five-field local-time calendar work. Set durable:true when the job must survive a session restart; durable jobs are stored in the trusted project under .stepcode/cron.", + "- Use cron_create for five-field local-time calendar work. Set durable:true when the job must survive a session restart; durable jobs are stored in the trusted project under .stepcode/cron and belong to the conversation that created them, so another conversation in the same folder neither sees nor runs them.", "- A session can hold up to 50 jobs. Recurring jobs repeat and expire after seven days; recurring:false jobs fire once and are removed after delivery. Invalid expressions are rejected.", "- Use cron_list to inspect next fire times and cron_delete to remove a job. Jobs fire only while StepCode is running and idle, after pending user input. A missed durable one-shot is surfaced with its original prompt when the session returns.", "- Use create_goal for an explicitly requested multi-turn objective when that tool is available. Cron and goals have independent lifecycles.", diff --git a/packages/coding-agent/test/step-cron.test.ts b/packages/coding-agent/test/step-cron.test.ts index 52c48d35..b3304fc6 100644 --- a/packages/coding-agent/test/step-cron.test.ts +++ b/packages/coding-agent/test/step-cron.test.ts @@ -168,6 +168,8 @@ interface Harness { handlers: Map unknown>>; sent: Array<{ message: Record; options?: Record }>; ctx: ExtensionContext; + /** The session id `ctx.sessionManager.getSessionId()` reports; reassign to act as another session. */ + session: { id: string; file: string | undefined }; } function createHarness(): Harness { @@ -175,6 +177,12 @@ function createHarness(): Harness { const tools = new Map(); const handlers = new Map unknown>>(); const sent: Array<{ message: Record; options?: Record }> = []; + // A task records the absolute path of its session file, so the harness has to + // report one; `undefined` models `--no-session`. + const session: { id: string; file: string | undefined } = { + id: "cron-session", + file: path.join(makeDirectory(), "cron-session.jsonl"), + }; const ctx = { mode: "rpc", hasUI: true, @@ -184,7 +192,11 @@ function createHarness(): Harness { hasPendingUserDecision: () => false, isProjectTrusted: () => true, ui: { notify: vi.fn(), confirm: vi.fn().mockResolvedValue(true) }, - sessionManager: { getSessionId: () => "cron-session", getEntries: () => [] }, + sessionManager: { + getSessionId: () => session.id, + getSessionFile: () => session.file, + getEntries: () => [], + }, } as unknown as ExtensionContext; const api = { registerTool(tool: ToolDefinition) { @@ -207,7 +219,7 @@ function createHarness(): Harness { appendEntry: () => {}, getFlag: () => false, } as unknown as ExtensionAPI; - return { api, tools, handlers, sent, ctx, commands }; + return { api, tools, handlers, sent, ctx, commands, session }; } test("cron extension exposes create/list/update/delete and steer delivery", async () => { @@ -712,7 +724,11 @@ test("missed notices and /cron status include the original prompts", async () => storagePath: path.join(harness.ctx.cwd, ".stepcode", "cron", "tasks.json"), jitterRatio: 0, }); - const missed = first.create("* * * * *", "check the failed deployment", false, true).job; + // Bound to this session, as every task the CLI creates now is: a record with no + // session id is not claimed by any session and would never be reported. + const missed = first.create("* * * * *", "check the failed deployment", false, true, true, { + sessionId: harness.session.id, + }).job; createStepCronExtension({ enabled: true })(harness.api); await emitCron(harness, "session_start"); expect(harness.sent[0]?.message.content).toContain(missed.prompt); @@ -1141,3 +1157,470 @@ describe("desktop cron task fields", () => { expect(deliveries).toHaveLength(1); }); }); + +describe("a task belongs to the session that created it", () => { + /** Two sessions sharing one project folder, which is what makes the file shared. */ + function twoSessionsInOneFolder() { + const cwd = makeDirectory(); + const first = createHarness(); + Object.assign(first.ctx, { cwd }); + first.session.id = "session-first"; + const second = createHarness(); + Object.assign(second.ctx, { cwd }); + second.session.id = "session-second"; + return { first, second }; + } + + test("the record names the creating session", async () => { + vi.useFakeTimers(); + const { first } = twoSessionsInOneFolder(); + createStepCronExtension({ enabled: true })(first.api); + await emitCron(first, "session_start"); + const created = (await first.tools + .get("cron_create")! + .execute( + "create", + { cron: "* * * * *", prompt: "audit", durable: true } as never, + undefined, + undefined, + first.ctx, + )) as { details: CronJob }; + expect(created.details.sessionId).toBe("session-first"); + // The file is what a later start checks; recording only the id would leave + // nothing to verify, and an id is not a location. + expect(created.details.sessionFile).toBe(first.session.file); + await emitCron(first, "session_shutdown"); + }); + + test("a host with no session file records no file, and its tasks are not guessed at", async () => { + // `--no-session` keeps the transcript in memory. There is no path to check, + // so a later start must leave the record alone rather than call it missing. + vi.useFakeTimers(); + const harness = createHarness(); + harness.session.file = undefined; + createStepCronExtension({ enabled: true })(harness.api); + await emitCron(harness, "session_start"); + const created = (await harness.tools + .get("cron_create")! + .execute( + "create", + { cron: "* * * * *", prompt: "no file", durable: true } as never, + undefined, + undefined, + harness.ctx, + )) as { + details: CronJob; + }; + expect(created.details.sessionId).toBe("cron-session"); + expect("sessionFile" in created.details).toBe(false); + await emitCron(harness, "session_shutdown"); + }); + + test("another session in the same folder neither lists nor fires it", async () => { + // The defect: the record lived in the shared project folder with no + // `sessionId`, so every session opened there claimed it and the job's prompt + // fired into whichever terminal was active. + vi.useFakeTimers(); + const { first, second } = twoSessionsInOneFolder(); + createStepCronExtension({ enabled: true })(first.api); + await emitCron(first, "session_start"); + await first.tools + .get("cron_create")! + .execute( + "create", + { cron: "* * * * *", prompt: "audit", durable: true } as never, + undefined, + undefined, + first.ctx, + ); + await emitCron(first, "session_shutdown"); + + createStepCronExtension({ enabled: true })(second.api); + await emitCron(second, "session_start"); + const listed = (await second.tools + .get("cron_list")! + .execute("list", {} as never, undefined, undefined, second.ctx)) as { + details: CronJob[]; + }; + expect(listed.details).toEqual([]); + vi.advanceTimersByTime(120_000); + expect(second.sent).toEqual([]); + await emitCron(second, "session_shutdown"); + }); + + test("the creating session still lists it after a restart", async () => { + // `--resume` reads the id back from the session file's header, so the same + // conversation reclaims its task; a restart is a new extension over the same + // folder and the same session id. + vi.useFakeTimers(); + const cwd = makeDirectory(); + const before = createHarness(); + Object.assign(before.ctx, { cwd }); + before.session.id = "session-a"; + createStepCronExtension({ enabled: true })(before.api); + await emitCron(before, "session_start"); + await before.tools + .get("cron_create")! + .execute( + "create", + { cron: "* * * * *", prompt: "audit", durable: true } as never, + undefined, + undefined, + before.ctx, + ); + await emitCron(before, "session_shutdown"); + + const after = createHarness(); + Object.assign(after.ctx, { cwd }); + after.session.id = "session-a"; + createStepCronExtension({ enabled: true })(after.api); + await emitCron(after, "session_start"); + const listed = (await after.tools + .get("cron_list")! + .execute("list", {} as never, undefined, undefined, after.ctx)) as { + details: CronJob[]; + }; + expect(listed.details.map((job) => job.prompt)).toEqual(["audit"]); + await emitCron(after, "session_shutdown"); + }); + + test("session_start does not deliver another session's due task", async () => { + // Startup recovery used to walk every durable record without consulting + // ownership, so a task belonging to a session that had exited was delivered + // into whichever session opened the folder next — the reported defect, on + // the one path that runs before any tick. + // + // `autoExpireAt` is in the past on purpose: that is the branch that actually + // fires. A record that has merely come due without expiring takes the + // "advance and defer" path, which a version without the ownership check + // would also leave silent — the test would pass either way. + vi.useFakeTimers(); + const cwd = makeDirectory(); + const now = Date.now(); + new CronFileStore(path.join(cwd, ".stepcode", "cron", "tasks.json")).save([ + { + schemaVersion: 2, + id: "theirs", + cron: "* * * * *", + prompt: "THEIR SECRET TASK", + recurring: true, + durable: true, + createdAt: now - 120_000, + nextFireAt: now - 60_000, + autoExpireAt: now - 30_000, + sessionId: "session-theirs", + }, + ]); + const harness = createHarness(); + Object.assign(harness.ctx, { cwd }); + harness.session.id = "session-mine"; + createStepCronExtension({ enabled: true })(harness.api); + await emitCron(harness, "session_start"); + expect(harness.sent).toEqual([]); + // Untouched: its schedule is that session's to advance, not ours. + const stored = new CronFileStore(path.join(cwd, ".stepcode", "cron", "tasks.json")).load(); + expect(stored[0]?.nextFireAt).toBe(now - 60_000); + await emitCron(harness, "session_shutdown"); + }); + + test("unowned records neither fill the create quota nor become undeletable", async () => { + // Records written before tasks were bound have no owner. Counting them + // blocked new tasks, and refusing to delete them left the reader with no way + // to clear the ones in the way. + vi.useFakeTimers(); + const cwd = makeDirectory(); + const now = Date.now(); + const file = path.join(cwd, ".stepcode", "cron", "tasks.json"); + new CronFileStore(file).save( + Array.from({ length: 50 }, (_, index) => ({ + schemaVersion: 1 as const, + id: `legacy-${index}`, + cron: "0 9 * * *", + prompt: `old ${index}`, + recurring: true, + durable: true, + createdAt: now - 1_000, + nextFireAt: now + 3_600_000, + })), + ); + const harness = createHarness(); + Object.assign(harness.ctx, { cwd }); + harness.session.id = "session-mine"; + createStepCronExtension({ enabled: true })(harness.api); + await emitCron(harness, "session_start"); + // 50 unowned records must not fill the quota. + const created = (await harness.tools + .get("cron_create")! + .execute( + "create", + { cron: "0 10 * * *", prompt: "new", durable: true } as never, + undefined, + undefined, + harness.ctx, + )) as { + details: CronJob; + }; + expect(created.details.sessionId).toBe("session-mine"); + // A record no session owns can be removed, so it is not stranded. + const deleted = (await harness.tools + .get("cron_delete")! + .execute("delete", { id: "legacy-0" } as never, undefined, undefined, harness.ctx)) as { + details: { found: boolean }; + }; + expect(deleted.details.found).toBe(true); + await emitCron(harness, "session_shutdown"); + }); + + test("a task bound to a deleted session is paused and can be removed", async () => { + // Nothing can claim a task whose conversation is gone: it is never listed, + // never fired, and its expiry only runs for records this host owns. Without + // marking it, the record sat in the shared file forever with no way out. + vi.useFakeTimers(); + const cwd = makeDirectory(); + const now = Date.now(); + const file = path.join(cwd, ".stepcode", "cron", "tasks.json"); + new CronFileStore(file).save([ + { + schemaVersion: 2, + id: "gone", + cron: "0 9 * * *", + prompt: "bound to a deleted conversation", + recurring: true, + durable: true, + createdAt: now - 1_000, + nextFireAt: now + 3_600_000, + sessionId: "session-that-was-deleted", + // A path that does not exist: the record names its own proof, so no + // directory scan is involved. + sessionFile: path.join(cwd, "sessions", "20260101_session-that-was-deleted.jsonl"), + }, + ]); + const harness = createHarness(); + Object.assign(harness.ctx, { cwd }); + harness.session.id = "session-mine"; + createStepCronExtension({ enabled: true })(harness.api); + await emitCron(harness, "session_start"); + const stored = new CronFileStore(file).load(); + expect(stored[0]?.status).toBe("paused"); + expect(stored[0]?.statusReason).toBe("session_missing"); + // Its bound session is gone, so this is the only place that can clear it. + const deleted = (await harness.tools + .get("cron_delete")! + .execute("delete", { id: "gone" } as never, undefined, undefined, harness.ctx)) as { + details: { found: boolean }; + }; + expect(deleted.details.found).toBe(true); + await emitCron(harness, "session_shutdown"); + }); + + test("a session file outside the default directory still counts as alive", async () => { + // `--session ` keeps whatever path it was given, and a custom session + // directory puts the file elsewhere. A record names its own file, so neither + // looks lost — judging by an id and a directory listing marked live + // sessions' own tasks as gone, and nothing could undo that. + vi.useFakeTimers(); + const cwd = makeDirectory(); + const now = Date.now(); + const file = path.join(cwd, ".stepcode", "cron", "tasks.json"); + const elsewhere = path.join(makeDirectory(), "hand-picked-name.jsonl"); + writeFileSync(elsewhere, "{}\n"); + new CronFileStore(file).save([ + { + schemaVersion: 2, + id: "outside", + cron: "0 9 * * *", + prompt: "bound to a session file with a non-standard name", + recurring: true, + durable: true, + createdAt: now - 1_000, + nextFireAt: now + 3_600_000, + sessionId: "session-elsewhere", + sessionFile: elsewhere, + }, + ]); + const harness = createHarness(); + Object.assign(harness.ctx, { cwd }); + harness.session.id = "session-mine"; + createStepCronExtension({ enabled: true })(harness.api); + await emitCron(harness, "session_start"); + const stored = new CronFileStore(file).load(); + expect(stored[0]?.status).toBeUndefined(); + expect(stored[0]?.statusReason).toBeUndefined(); + await emitCron(harness, "session_shutdown"); + }); + + test("a record written before session files were recorded is left alone", async () => { + // Such a record carries only an id, and an id is not a location. There is + // nothing to verify, so nothing is guessed. + vi.useFakeTimers(); + const cwd = makeDirectory(); + const now = Date.now(); + const file = path.join(cwd, ".stepcode", "cron", "tasks.json"); + new CronFileStore(file).save([ + { + schemaVersion: 2, + id: "old-style", + cron: "0 9 * * *", + prompt: "only a session id", + recurring: true, + durable: true, + createdAt: now - 1_000, + nextFireAt: now + 3_600_000, + sessionId: "session-unknown", + }, + ]); + const harness = createHarness(); + Object.assign(harness.ctx, { cwd }); + harness.session.id = "session-mine"; + createStepCronExtension({ enabled: true })(harness.api); + await emitCron(harness, "session_start"); + const stored = new CronFileStore(file).load(); + expect(stored[0]?.status).toBeUndefined(); + expect(stored[0]?.statusReason).toBeUndefined(); + await emitCron(harness, "session_shutdown"); + }); + + test("a task from a host with no session file is still removable by its own session", async () => { + // `--no-session` records an id but no file, so no start can verify it. Its + // own conversation still owns it through the id, which is what keeps the + // record from being stranded. + vi.useFakeTimers(); + const harness = createHarness(); + harness.session.file = undefined; + harness.session.id = "session-no-file"; + createStepCronExtension({ enabled: true })(harness.api); + await emitCron(harness, "session_start"); + const created = (await harness.tools + .get("cron_create")! + .execute( + "create", + { cron: "0 9 * * *", prompt: "no file", durable: true } as never, + undefined, + undefined, + harness.ctx, + )) as { details: CronJob }; + expect("sessionFile" in created.details).toBe(false); + // Not paused: there is nothing to check. + const listed = (await harness.tools + .get("cron_list")! + .execute("list", {} as never, undefined, undefined, harness.ctx)) as { details: CronJob[] }; + expect(listed.details.map((job) => job.id)).toEqual([created.details.id]); + const deleted = (await harness.tools + .get("cron_delete")! + .execute("delete", { id: created.details.id } as never, undefined, undefined, harness.ctx)) as { + details: { found: boolean }; + }; + expect(deleted.details.found).toBe(true); + await emitCron(harness, "session_shutdown"); + }); + + test("a finished task keeps its outcome instead of being marked as a lost session", async () => { + // Overwriting it with `paused` would hide why it stopped, and the record is + // not a lost session in the first place. + vi.useFakeTimers(); + const cwd = makeDirectory(); + const now = Date.now(); + const file = path.join(cwd, ".stepcode", "cron", "tasks.json"); + const gone = path.join(cwd, "sessions", "gone.jsonl"); + new CronFileStore(file).save([ + { + schemaVersion: 2, + id: "done", + cron: "0 9 * * *", + prompt: "finished", + recurring: true, + durable: true, + createdAt: now - 10_000, + nextFireAt: now + 3_600_000, + sessionId: "session-gone", + sessionFile: gone, + status: "completed", + }, + ]); + const harness = createHarness(); + Object.assign(harness.ctx, { cwd }); + harness.session.id = "session-mine"; + createStepCronExtension({ enabled: true })(harness.api); + await emitCron(harness, "session_start"); + const stored = new CronFileStore(file).load(); + expect(stored[0]?.status).toBe("completed"); + expect(stored[0]?.statusReason).toBeUndefined(); + await emitCron(harness, "session_shutdown"); + }); + + test("a task whose bound session still exists is left alone", async () => { + vi.useFakeTimers(); + const cwd = makeDirectory(); + const now = Date.now(); + const file = path.join(cwd, ".stepcode", "cron", "tasks.json"); + new CronFileStore(file).save([ + { + schemaVersion: 2, + id: "alive", + cron: "0 9 * * *", + prompt: "still bound", + recurring: true, + durable: true, + createdAt: now - 1_000, + nextFireAt: now + 3_600_000, + sessionId: "session-other", + sessionFile: path.join(cwd, "sessions", "20260101_session-other.jsonl"), + }, + ]); + const harness = createHarness(); + Object.assign(harness.ctx, { cwd }); + harness.session.id = "session-mine"; + // The bound session's file is present, so its task stays live. + const sessionFile = path.join(cwd, "sessions", "20260101_session-other.jsonl"); + mkdirSync(path.dirname(sessionFile), { recursive: true }); + writeFileSync(sessionFile, "{}\n"); + createStepCronExtension({ enabled: true })(harness.api); + await emitCron(harness, "session_start"); + const stored = new CronFileStore(file).load(); + expect(stored[0]?.status).toBeUndefined(); + expect(stored[0]?.statusReason).toBeUndefined(); + // Still not ours to delete: that conversation is live. + const deleted = (await harness.tools + .get("cron_delete")! + .execute("delete", { id: "alive" } as never, undefined, undefined, harness.ctx)) as { + details: { found: boolean }; + }; + expect(deleted.details.found).toBe(false); + await emitCron(harness, "session_shutdown"); + }); + + test("a record with no session id is not claimed by any session", async () => { + // Records written before tasks were bound cannot be attributed, and firing + // one into an unrelated conversation is the defect this fixes. They stay on + // disk, unlisted, rather than being delivered somewhere arbitrary. + vi.useFakeTimers(); + const cwd = makeDirectory(); + const store = new CronFileStore(path.join(cwd, ".stepcode", "cron", "tasks.json")); + store.save([ + { + schemaVersion: 1, + id: "legacy", + cron: "* * * * *", + prompt: "old", + recurring: true, + durable: true, + createdAt: Date.now() - 1_000, + nextFireAt: Date.now() + 1_000, + }, + ]); + const harness = createHarness(); + Object.assign(harness.ctx, { cwd }); + harness.session.id = "session-any"; + createStepCronExtension({ enabled: true })(harness.api); + await emitCron(harness, "session_start"); + const listed = (await harness.tools + .get("cron_list")! + .execute("list", {} as never, undefined, undefined, harness.ctx)) as { + details: CronJob[]; + }; + expect(listed.details).toEqual([]); + vi.advanceTimersByTime(120_000); + expect(harness.sent).toEqual([]); + await emitCron(harness, "session_shutdown"); + }); +});