@@ -73,51 +73,91 @@ describe("update() must not drop a due every-job's pending run", () => {
7373 cron . stop ( ) ;
7474 } ) ;
7575
76- it ( "re-anchors an every-job to the edit time when the interval actually changes" , async ( ) => {
77- const store = await makeStorePath ( ) ;
78- const base = Date . parse ( "2025-12-13T00:00:00.000Z" ) ;
79-
80- const finished = createFinishedBarrier ( ) ;
81- const cron = new CronService ( {
82- storePath : store . storePath ,
83- cronEnabled : true ,
84- log : noopLogger ,
85- enqueueSystemEvent : vi . fn ( ) ,
86- requestHeartbeat : vi . fn ( ) ,
87- runIsolatedAgentJob : vi . fn ( async ( ) => ( { status : "ok" as const } ) ) ,
88- onEvent : finished . onEvent ,
89- } ) ;
90-
91- await cron . start ( ) ;
92-
93- const job = await cron . add ( {
94- name : "every 10s" ,
95- enabled : true ,
96- schedule : { kind : "every" , everyMs : 10_000 } ,
97- sessionTarget : "isolated" ,
98- wakeMode : "next-heartbeat" ,
99- payload : { kind : "agentTurn" , message : "tick" } ,
100- } ) ;
101- const jobId = job . id ;
102- expect ( job . schedule ) . toMatchObject ( { kind : "every" , anchorMs : base } ) ;
103-
104- // User edits the interval from 10s to 1h. The control UI omits the internal
105- // anchorMs, so the new cadence must start from the edit time rather than
106- // keeping the old phase; nextRunAtMs is one new interval from now.
107- const editTime = base + 3_000 ;
108- vi . setSystemTime ( new Date ( editTime ) ) ;
109- await cron . update ( jobId , { schedule : { kind : "every" , everyMs : 3_600_000 } } ) ;
110-
111- const current = ( await cron . list ( { includeDisabled : true } ) ) . find ( ( j ) => j . id === jobId ) ! ;
112- expect ( current . schedule ) . toMatchObject ( {
113- kind : "every" ,
114- everyMs : 3_600_000 ,
115- anchorMs : editTime ,
116- } ) ;
117- expect ( current . state . nextRunAtMs ) . toBe ( editTime + 3_600_000 ) ;
118-
119- cron . stop ( ) ;
120- } ) ;
76+ it . each ( [
77+ { name : "before its first run" , previousEveryMs : 10_000 , nextEveryMs : 3_600_000 } ,
78+ {
79+ name : "after a completed run when the interval increases" ,
80+ previousEveryMs : 10_000 ,
81+ nextEveryMs : 3_600_000 ,
82+ completedRun : true ,
83+ } ,
84+ {
85+ name : "after a completed run when the interval decreases" ,
86+ previousEveryMs : 60_000 ,
87+ nextEveryMs : 10_000 ,
88+ completedRun : true ,
89+ } ,
90+ {
91+ name : "at an explicit future anchor after a completed run" ,
92+ previousEveryMs : 10_000 ,
93+ nextEveryMs : 3_600_000 ,
94+ completedRun : true ,
95+ futureAnchorOffsetMs : 7_200_000 ,
96+ } ,
97+ ] ) (
98+ "re-anchors an every-job $name" ,
99+ async ( { previousEveryMs, nextEveryMs, completedRun, futureAnchorOffsetMs } ) => {
100+ const store = await makeStorePath ( ) ;
101+ const base = Date . parse ( "2025-12-13T00:00:00.000Z" ) ;
102+
103+ const finished = createFinishedBarrier ( ) ;
104+ const cron = new CronService ( {
105+ storePath : store . storePath ,
106+ cronEnabled : true ,
107+ log : noopLogger ,
108+ enqueueSystemEvent : vi . fn ( ) ,
109+ requestHeartbeat : vi . fn ( ) ,
110+ runIsolatedAgentJob : vi . fn ( async ( ) => ( { status : "ok" as const } ) ) ,
111+ onEvent : finished . onEvent ,
112+ } ) ;
113+
114+ await cron . start ( ) ;
115+
116+ const job = await cron . add ( {
117+ name : "every 10s" ,
118+ enabled : true ,
119+ schedule : { kind : "every" , everyMs : previousEveryMs } ,
120+ sessionTarget : "isolated" ,
121+ wakeMode : "next-heartbeat" ,
122+ payload : { kind : "agentTurn" , message : "tick" } ,
123+ } ) ;
124+ const jobId = job . id ;
125+ expect ( job . schedule ) . toMatchObject ( { kind : "every" , anchorMs : base } ) ;
126+
127+ let editTime = base + 3_000 ;
128+ if ( completedRun ) {
129+ vi . setSystemTime ( new Date ( base + previousEveryMs + 5 ) ) ;
130+ const firstRun = finished . waitForOk ( jobId ) ;
131+ await vi . runOnlyPendingTimersAsync ( ) ;
132+ await firstRun ;
133+ const completedJob = ( await cron . list ( { includeDisabled : true } ) ) . find (
134+ ( candidate ) => candidate . id === jobId ,
135+ ) ! ;
136+ editTime = completedJob . state . lastRunAtMs ! + 3_000 ;
137+ }
138+
139+ vi . setSystemTime ( new Date ( editTime ) ) ;
140+ const futureAnchorMs =
141+ futureAnchorOffsetMs === undefined ? undefined : editTime + futureAnchorOffsetMs ;
142+ await cron . update ( jobId , {
143+ schedule : {
144+ kind : "every" ,
145+ everyMs : nextEveryMs ,
146+ ...( futureAnchorMs === undefined ? { } : { anchorMs : futureAnchorMs } ) ,
147+ } ,
148+ } ) ;
149+
150+ const current = ( await cron . list ( { includeDisabled : true } ) ) . find ( ( j ) => j . id === jobId ) ! ;
151+ expect ( current . schedule ) . toMatchObject ( {
152+ kind : "every" ,
153+ everyMs : nextEveryMs ,
154+ anchorMs : futureAnchorMs ?? editTime ,
155+ } ) ;
156+ expect ( current . state . nextRunAtMs ) . toBe ( futureAnchorMs ?? editTime + nextEveryMs ) ;
157+
158+ cron . stop ( ) ;
159+ } ,
160+ ) ;
121161
122162 it ( "preserves a due cron-job nextRunAtMs on an idempotent schedule re-save" , async ( ) => {
123163 const store = await makeStorePath ( ) ;
0 commit comments