@@ -333,6 +333,72 @@ describe("CronService - session reaper runs in finally block (#31946)", () => {
333333 } ) ;
334334 } ) ;
335335
336+ it . each ( [
337+ { name : "deleted persisted owner" , includeStaleJob : false } ,
338+ { name : "blocked-delete owner with unfinished job cleanup" , includeStaleJob : true } ,
339+ ] ) ( "skips an unavailable $name without hiding a live owner" , async ( { includeStaleJob } ) => {
340+ const store = await makeStorePath ( ) ;
341+ const now = Date . parse ( "2026-02-10T10:00:00.000Z" ) ;
342+ const liveAgentId = "live" ;
343+ const unavailableAgentId = includeStaleJob ? "blocked" : "deleted" ;
344+ const sessionStorePath = path . join ( path . dirname ( store . storePath ) , "sessions" , "sessions.json" ) ;
345+ const jobs = includeStaleJob
346+ ? [
347+ {
348+ ...createDueIsolatedJob ( { id : "unfinished-cleanup" , nowMs : now } ) ,
349+ agentId : unavailableAgentId ,
350+ enabled : false ,
351+ } ,
352+ ]
353+ : [ ] ;
354+ await saveCronStore ( store . storePath , { version : 1 , jobs } ) ;
355+ await replaceSessionEntry (
356+ {
357+ agentId : liveAgentId ,
358+ storePath : sessionStorePath ,
359+ sessionKey : `agent:${ liveAgentId } :cron:old-job:run:expired` ,
360+ } ,
361+ { sessionId : "live-expired" , updatedAt : now - 25 * 3_600_000 } ,
362+ ) ;
363+ const resolveSessionStorePath = vi . fn ( ( agentId ?: string ) => {
364+ if ( agentId === unavailableAgentId ) {
365+ throw new Error (
366+ `OpenClaw agent database is unavailable while agent ${ unavailableAgentId } is deleted.` ,
367+ ) ;
368+ }
369+ return sessionStorePath ;
370+ } ) ;
371+ const state = createCronServiceState ( {
372+ storePath : store . storePath ,
373+ cronEnabled : true ,
374+ log : noopLogger ,
375+ nowMs : ( ) => now ,
376+ enqueueSystemEvent : vi . fn ( ) ,
377+ requestHeartbeat : vi . fn ( ) ,
378+ runIsolatedAgentJob : vi . fn ( ) ,
379+ resolveDefaultAgentId : ( ) => undefined ,
380+ resolveSessionStoreAgentIds : ( ) => [ liveAgentId , unavailableAgentId ] ,
381+ isAgentAvailable : ( agentId ) => agentId === liveAgentId ,
382+ resolveSessionStorePath,
383+ } ) ;
384+
385+ await withCronServiceStateForTest ( state , async ( ) => {
386+ await onTimer ( state ) ;
387+ await onTimer ( state ) ;
388+
389+ expect ( resolveSessionStorePath . mock . calls ) . toEqual ( [ [ liveAgentId ] , [ liveAgentId ] ] ) ;
390+ expect ( listSessionEntriesCore ( { agentId : liveAgentId , storePath : sessionStorePath } ) ) . toEqual (
391+ [ ] ,
392+ ) ;
393+ expect ( noopLogger . warn ) . not . toHaveBeenCalled ( ) ;
394+ expect (
395+ noopLogger . debug . mock . calls . filter (
396+ ( [ , message ] ) => message === "cron-reaper: skipped unavailable agent" ,
397+ ) ,
398+ ) . toEqual ( [ [ { agentId : unavailableAgentId } , "cron-reaper: skipped unavailable agent" ] ] ) ;
399+ } ) ;
400+ } ) ;
401+
336402 it ( "sweeps a persisted owner after it leaves the roster and cron store" , async ( ) => {
337403 const store = await makeStorePath ( ) ;
338404 const now = Date . parse ( "2026-02-10T10:00:00.000Z" ) ;
0 commit comments