@@ -730,6 +730,25 @@ export function abortEmbeddedAgentRun(
730730 return false ;
731731}
732732
733+ type EmbeddedHeartbeatPreemptionResult = "not-heartbeat" | "drained" | "timed-out" ;
734+
735+ export async function preemptAndDrainEmbeddedHeartbeatRun (
736+ sessionId : string ,
737+ timeoutMs : number ,
738+ ) : Promise < EmbeddedHeartbeatPreemptionResult > {
739+ const handle = ACTIVE_EMBEDDED_RUNS . get ( sessionId ) ;
740+ if ( ! handle ?. preemptByVisibleTurn ) {
741+ return "not-heartbeat" ;
742+ }
743+ const drainPromise = waitForCurrentEmbeddedAgentRunEnd ( sessionId , timeoutMs , handle ) ;
744+ try {
745+ handle . preemptByVisibleTurn ( ) ;
746+ } catch ( err ) {
747+ diag . warn ( `heartbeat preemption failed: sessionId=${ sessionId } err=${ String ( err ) } ` ) ;
748+ }
749+ return ( await drainPromise ) ? "drained" : "timed-out" ;
750+ }
751+
733752export function isEmbeddedAgentRunActive ( sessionId : string ) : boolean {
734753 const active = ACTIVE_EMBEDDED_RUNS . has ( sessionId ) || isReplyRunActiveForSessionId ( sessionId ) ;
735754 if ( active ) {
@@ -866,8 +885,14 @@ export async function waitForActiveEmbeddedRuns(
866885function waitForCurrentEmbeddedAgentRunEnd (
867886 sessionId : string ,
868887 timeoutMs : number | null ,
888+ handle ?: EmbeddedAgentQueueHandle ,
869889) : Promise < boolean > {
870- if ( ! ACTIVE_EMBEDDED_RUNS . has ( sessionId ) ) {
890+ const isHandleActive = ( ) =>
891+ handle ? ACTIVE_EMBEDDED_RUNS . get ( sessionId ) === handle : ACTIVE_EMBEDDED_RUNS . has ( sessionId ) ;
892+ if ( ! isHandleActive ( ) ) {
893+ if ( handle ) {
894+ return Promise . resolve ( true ) ;
895+ }
871896 return waitForReplyRunEndBySessionId ( sessionId , timeoutMs ) ;
872897 }
873898 const timeoutLabel = timeoutMs === null ? "none" : String ( timeoutMs ) ;
@@ -876,6 +901,7 @@ function waitForCurrentEmbeddedAgentRunEnd(
876901 const waiters = EMBEDDED_RUN_WAITERS . get ( sessionId ) ?? new Set ( ) ;
877902 const waiter : EmbeddedRunWaiter = {
878903 resolve,
904+ handle,
879905 } ;
880906 if ( timeoutMs !== null ) {
881907 waiter . timer = setTimeout (
@@ -892,7 +918,7 @@ function waitForCurrentEmbeddedAgentRunEnd(
892918 }
893919 waiters . add ( waiter ) ;
894920 EMBEDDED_RUN_WAITERS . set ( sessionId , waiters ) ;
895- if ( ! ACTIVE_EMBEDDED_RUNS . has ( sessionId ) ) {
921+ if ( ! isHandleActive ( ) ) {
896922 waiters . delete ( waiter ) ;
897923 if ( waiters . size === 0 ) {
898924 EMBEDDED_RUN_WAITERS . delete ( sessionId ) ;
@@ -1111,19 +1137,26 @@ async function persistForceClearedEmbeddedRunTerminalState(params: {
11111137 }
11121138}
11131139
1114- function notifyEmbeddedRunEnded ( sessionId : string ) {
1140+ function notifyEmbeddedRunEnded ( sessionId : string , endedHandle : EmbeddedAgentQueueHandle ) {
11151141 const waiters = EMBEDDED_RUN_WAITERS . get ( sessionId ) ;
11161142 if ( ! waiters || waiters . size === 0 ) {
11171143 return ;
11181144 }
1119- EMBEDDED_RUN_WAITERS . delete ( sessionId ) ;
1145+ const sessionIdle = ! ACTIVE_EMBEDDED_RUNS . has ( sessionId ) ;
11201146 diag . debug ( `notifying waiters: sessionId=${ sessionId } waiterCount=${ waiters . size } ` ) ;
11211147 for ( const waiter of waiters ) {
1148+ if ( waiter . handle ? waiter . handle !== endedHandle : ! sessionIdle ) {
1149+ continue ;
1150+ }
1151+ waiters . delete ( waiter ) ;
11221152 if ( waiter . timer ) {
11231153 clearTimeout ( waiter . timer ) ;
11241154 }
11251155 waiter . resolve ( true ) ;
11261156 }
1157+ if ( waiters . size === 0 ) {
1158+ EMBEDDED_RUN_WAITERS . delete ( sessionId ) ;
1159+ }
11271160}
11281161
11291162export function setActiveEmbeddedRun (
@@ -1193,9 +1226,6 @@ export function clearActiveEmbeddedRun(
11931226 reason = "run_completed" ,
11941227) {
11951228 const activeHandle = ACTIVE_EMBEDDED_RUNS . get ( sessionId ) ;
1196- if ( activeHandle === undefined ) {
1197- return ;
1198- }
11991229 if ( activeHandle === handle ) {
12001230 ACTIVE_EMBEDDED_RUNS . delete ( sessionId ) ;
12011231 clearEmbeddedRunAbortability ( handle , { retainFinalizing : true } ) ;
@@ -1213,10 +1243,11 @@ export function clearActiveEmbeddedRun(
12131243 if ( ! sessionId . startsWith ( "probe-" ) ) {
12141244 diag . debug ( `run cleared: sessionId=${ sessionId } totalActive=${ ACTIVE_EMBEDDED_RUNS . size } ` ) ;
12151245 }
1216- notifyEmbeddedRunEnded ( sessionId ) ;
1217- } else {
1246+ } else if ( activeHandle !== undefined ) {
12181247 diag . debug ( `run clear skipped: sessionId=${ sessionId } reason=handle_mismatch` ) ;
12191248 }
1249+ // Exact-handle waiters own teardown even after another run takes the session slot.
1250+ notifyEmbeddedRunEnded ( sessionId , handle ) ;
12201251}
12211252
12221253function forceClearEmbeddedAgentRun (
@@ -1236,7 +1267,7 @@ function forceClearEmbeddedAgentRun(
12361267 clearActiveRunSessionFiles ( sessionId ) ;
12371268 logSessionStateChange ( { sessionId, sessionKey, state : "idle" , reason } ) ;
12381269 markDiagnosticEmbeddedRunEnded ( { sessionId, sessionKey } ) ;
1239- notifyEmbeddedRunEnded ( sessionId ) ;
1270+ notifyEmbeddedRunEnded ( sessionId , handle ) ;
12401271 cleared = true ;
12411272 }
12421273 const cause = new Error ( `Embedded run force-cleared by ${ reason } ` ) ;
@@ -1254,7 +1285,7 @@ const testing = {
12541285 if ( waiter . timer ) {
12551286 clearTimeout ( waiter . timer ) ;
12561287 }
1257- waiter . resolve ( true ) ;
1288+ waiter . resolve ( ! waiter . handle ) ;
12581289 }
12591290 }
12601291 EMBEDDED_RUN_WAITERS . clear ( ) ;
0 commit comments