@@ -394,6 +394,52 @@ describe("startGatewayMaintenanceTimers", () => {
394394 stopMaintenanceTimers ( timers ) ;
395395 } ) ;
396396
397+ it ( "evicts pending accepted agent dedupe entries with invalid run expiry" , async ( ) => {
398+ vi . useFakeTimers ( ) ;
399+ vi . setSystemTime ( new Date ( "2026-03-22T00:00:00Z" ) ) ;
400+ const { startGatewayMaintenanceTimers } = await import ( "./server-maintenance.js" ) ;
401+ const deps = createMaintenanceTimerDeps ( ) ;
402+ const now = Date . now ( ) ;
403+ deps . dedupe . set ( "agent:invalid-expiry-pending-agent" , {
404+ ts : now - DEDUPE_TTL_MS - 1 ,
405+ ok : true ,
406+ payload : {
407+ runId : "invalid-expiry-pending-agent" ,
408+ sessionKey : "agent:main:main" ,
409+ status : "accepted" ,
410+ expiresAtMs : Number . POSITIVE_INFINITY ,
411+ } ,
412+ } ) ;
413+
414+ const timers = startGatewayMaintenanceTimers ( deps ) ;
415+
416+ await vi . advanceTimersByTimeAsync ( 60_000 ) ;
417+
418+ expect ( deps . dedupe . has ( "agent:invalid-expiry-pending-agent" ) ) . toBe ( false ) ;
419+
420+ stopMaintenanceTimers ( timers ) ;
421+ } ) ;
422+
423+ it ( "aborts active runs with invalid expiry timestamps" , async ( ) => {
424+ vi . useFakeTimers ( ) ;
425+ vi . setSystemTime ( new Date ( "2026-03-22T00:00:00Z" ) ) ;
426+ const { startGatewayMaintenanceTimers } = await import ( "./server-maintenance.js" ) ;
427+ const deps = createMaintenanceTimerDeps ( ) ;
428+ const runId = "run-invalid-expiry" ;
429+ const activeRun = createActiveRun ( "main" ) ;
430+ activeRun . expiresAtMs = Number . POSITIVE_INFINITY ;
431+ deps . chatAbortControllers . set ( runId , activeRun ) ;
432+
433+ const timers = startGatewayMaintenanceTimers ( deps ) ;
434+
435+ await vi . advanceTimersByTimeAsync ( 60_000 ) ;
436+
437+ expect ( activeRun . controller . signal . aborted ) . toBe ( true ) ;
438+ expect ( deps . chatAbortControllers . has ( runId ) ) . toBe ( false ) ;
439+
440+ stopMaintenanceTimers ( timers ) ;
441+ } ) ;
442+
397443 it ( "keeps active exec approval dedupe aliases past the normal ttl" , async ( ) => {
398444 vi . useFakeTimers ( ) ;
399445 vi . setSystemTime ( new Date ( "2026-03-22T00:00:00Z" ) ) ;
0 commit comments