@@ -373,19 +373,10 @@ export async function runCronIsolatedAgentTurn(params: {
373373 } ) ;
374374 }
375375 } finally {
376- // Release runtime references after the run completes (success or failure) .
377- // The session entry has already been persisted to disk by this point,
378- // so the in-memory store and run context can be safely dropped .
376+ // Release runtime and admission ownership before deleting the exact-run alias .
377+ // Deleting while this turn still owns session work deterministically reports
378+ // "competing work is in flight" and leaks every otherwise-unused continuation .
379379 try {
380- if ( prepared . context . runContinuationSession ) {
381- try {
382- await removeCronRunContinuationSessionIfIdle ( prepared . context . runSessionKey ) ;
383- } catch ( error ) {
384- logWarn (
385- `[cron:${ params . job . id } ] Failed to remove unused run continuation: ${ String ( error ) } ` ,
386- ) ;
387- }
388- }
389380 await disposeCronRunContext ( {
390381 sessionId : initialSessionId ,
391382 cronSession : prepared . context . cronSession ,
@@ -394,14 +385,26 @@ export async function runCronIsolatedAgentTurn(params: {
394385 } ) ;
395386 } finally {
396387 prepared . context . sessionWorkAdmission . release ( ) ;
397- // Only run-scoped browser identities end with this invocation.
398- // Persistent cron targets keep the session and its tracked tabs alive.
399- if ( prepared . context . runSessionKey !== prepared . context . agentSessionKey ) {
400- await cleanupBrowserSessionsForLifecycleEnd ( {
401- cfg : prepared . context . cfgWithAgentDefaults ,
402- sessionKeys : [ prepared . context . runSessionKey ] ,
403- onWarn : ( message ) => logWarn ( `[cron:${ params . job . id } ] ${ message } ` ) ,
404- } ) ;
388+ try {
389+ if ( prepared . context . runContinuationSession ) {
390+ try {
391+ await removeCronRunContinuationSessionIfIdle ( prepared . context . runSessionKey ) ;
392+ } catch ( error ) {
393+ logWarn (
394+ `[cron:${ params . job . id } ] Failed to remove unused run continuation: ${ String ( error ) } ` ,
395+ ) ;
396+ }
397+ }
398+ } finally {
399+ // Only run-scoped browser identities end with this invocation.
400+ // Persistent cron targets keep the session and its tracked tabs alive.
401+ if ( prepared . context . runSessionKey !== prepared . context . agentSessionKey ) {
402+ await cleanupBrowserSessionsForLifecycleEnd ( {
403+ cfg : prepared . context . cfgWithAgentDefaults ,
404+ sessionKeys : [ prepared . context . runSessionKey ] ,
405+ onWarn : ( message ) => logWarn ( `[cron:${ params . job . id } ] ${ message } ` ) ,
406+ } ) ;
407+ }
405408 }
406409 }
407410 }
0 commit comments