@@ -2,14 +2,15 @@ import { join } from "node:path";
22import { afterEach , describe , expect , it , vi } from "vitest" ;
33import { validateCronAddParams } from "../../packages/gateway-protocol/src/index.js" ;
44import { useAutoCleanupTempDirTracker } from "../../test/helpers/temp-dir.js" ;
5+ import { normalizeCronJobCreate } from "../cron/normalize.js" ;
56import { closeOpenClawStateDatabaseForTest } from "../state/openclaw-state-db.js" ;
6- import { installClawCronJobs , readClawCronRefs } from "./cron.js" ;
7+ import { clawCronGatewayInput , installClawCronJobs , readClawCronRefs } from "./cron.js" ;
78import { buildClawAddPlan } from "./lifecycle.js" ;
89import { parseClawManifest } from "./schema.js" ;
910import type { ClawSourceIdentity } from "./types.js" ;
1011
11- afterEach ( ( ) => closeOpenClawStateDatabaseForTest ( ) ) ;
1212const tempDirs = useAutoCleanupTempDirTracker ( afterEach ) ;
13+ afterEach ( ( ) => closeOpenClawStateDatabaseForTest ( ) ) ;
1314
1415async function fixture ( ) {
1516 const root = tempDirs . make ( "openclaw-claw-cron-" ) ;
@@ -30,6 +31,7 @@ async function fixture() {
3031 if ( ! parsed . ok ) {
3132 throw new Error ( JSON . stringify ( parsed . diagnostics ) ) ;
3233 }
34+
3335 const source : ClawSourceIdentity = {
3436 kind : "package" ,
3537 name : "@acme/worker" ,
@@ -48,6 +50,24 @@ async function fixture() {
4850 return { root, plan, env : { OPENCLAW_STATE_DIR : join ( root , "state" ) } } ;
4951}
5052
53+ function listedCronJob (
54+ agentId : string ,
55+ ref : ReturnType < typeof readClawCronRefs > [ number ] ,
56+ id : string ,
57+ ) {
58+ const normalized = normalizeCronJobCreate ( clawCronGatewayInput ( agentId , ref ) ) ;
59+ if ( ! normalized ) {
60+ throw new Error ( "expected normalized cron job" ) ;
61+ }
62+ return {
63+ ...normalized ,
64+ id,
65+ createdAtMs : 1 ,
66+ updatedAtMs : 1 ,
67+ state : { } ,
68+ } ;
69+ }
70+
5171describe ( "installClawCronJobs" , ( ) => {
5272 it ( "pins declarations and execution to the final agent id" , async ( ) => {
5373 const current = await fixture ( ) ;
@@ -124,6 +144,73 @@ describe("installClawCronJobs", () => {
124144 expect ( add ) . not . toHaveBeenCalled ( ) ;
125145 } ) ;
126146
147+ it ( "fails closed when a complete scheduler job disappeared" , async ( ) => {
148+ const current = await fixture ( ) ;
149+ await installClawCronJobs ( current . plan , {
150+ env : current . env ,
151+ gateway : { add : vi . fn ( ) . mockResolvedValue ( { id : "scheduler-123" } ) } ,
152+ } ) ;
153+ const list = vi . fn ( ) . mockResolvedValue ( { jobs : [ ] } ) ;
154+ const add = vi . fn ( ) ;
155+
156+ await expect (
157+ installClawCronJobs ( current . plan , {
158+ env : current . env ,
159+ gateway : { add, list } ,
160+ } ) ,
161+ ) . rejects . toMatchObject ( { code : "cron_reconcile_conflict" } ) ;
162+
163+ expect ( list ) . toHaveBeenCalledOnce ( ) ;
164+ expect ( add ) . not . toHaveBeenCalled ( ) ;
165+ expect ( readClawCronRefs ( "worker-two" , { env : current . env } ) ) . toMatchObject ( [
166+ { schedulerJobId : "scheduler-123" , status : "complete" } ,
167+ ] ) ;
168+ } ) ;
169+
170+ it ( "rejects a same-key scheduler job whose declaration drifted" , async ( ) => {
171+ const current = await fixture ( ) ;
172+ await installClawCronJobs ( current . plan , {
173+ env : current . env ,
174+ gateway : { add : vi . fn ( ) . mockResolvedValue ( { id : "scheduler-123" } ) } ,
175+ } ) ;
176+ const [ ref ] = readClawCronRefs ( "worker-two" , { env : current . env } ) ;
177+ const drifted = listedCronJob ( "worker-two" , ref ! , "scheduler-123" ) ;
178+ drifted . payload = { kind : "agentTurn" , message : "Different declaration" } ;
179+ const add = vi . fn ( ) ;
180+
181+ await expect (
182+ installClawCronJobs ( current . plan , {
183+ env : current . env ,
184+ gateway : { add, list : vi . fn ( ) . mockResolvedValue ( { jobs : [ drifted ] } ) } ,
185+ } ) ,
186+ ) . rejects . toMatchObject ( { code : "cron_reconcile_conflict" } ) ;
187+ expect ( add ) . not . toHaveBeenCalled ( ) ;
188+ } ) ;
189+
190+ it ( "rejects a drifted complete job beyond the first gateway page" , async ( ) => {
191+ const current = await fixture ( ) ;
192+ await installClawCronJobs ( current . plan , {
193+ env : current . env ,
194+ gateway : { add : vi . fn ( ) . mockResolvedValue ( { id : "scheduler-123" } ) } ,
195+ } ) ;
196+ const [ ref ] = readClawCronRefs ( "worker-two" , { env : current . env } ) ;
197+ const drifted = listedCronJob ( "worker-two" , ref ! , "scheduler-123" ) ;
198+ drifted . payload = { kind : "agentTurn" , message : "Different declaration" } ;
199+ const fillers = Array . from ( { length : 200 } , ( _ , index ) => ( { id : `filler-${ index } ` } ) ) ;
200+ const add = vi . fn ( ) ;
201+
202+ await expect (
203+ installClawCronJobs ( current . plan , {
204+ env : current . env ,
205+ gateway : {
206+ add,
207+ list : vi . fn ( ) . mockResolvedValue ( { jobs : [ ...fillers , drifted ] } ) ,
208+ } ,
209+ } ) ,
210+ ) . rejects . toMatchObject ( { code : "cron_reconcile_conflict" } ) ;
211+ expect ( add ) . not . toHaveBeenCalled ( ) ;
212+ } ) ;
213+
127214 it ( "preserves an ambiguous pending reference when cron.add fails" , async ( ) => {
128215 const current = await fixture ( ) ;
129216
0 commit comments