@@ -36,6 +36,7 @@ import { getSubagentSessionStartedAt } from "./subagent-session-metrics.js";
3636
3737const MAX_RECOVERY_ATTEMPTS = 2 ;
3838const RECOVERY_ATTEMPT_WINDOW_MS = 2 * 60_000 ;
39+ type SubagentRunManager = ReturnType < typeof createSubagentRunManager > ;
3940
4041export type RestartRecoveryResult =
4142 | { status : "ignored" }
@@ -56,33 +57,17 @@ export type RestartRecoveryParams = {
5657 entry : SubagentRunRecord ;
5758 now : number ;
5859 gatewayRuntime : GatewayRecoveryRuntime | undefined ;
59- isCurrent : ( ) => boolean ;
60- abandonLaunch : ReturnType <
61- typeof createSubagentRunManager
62- > [ "abandonSubagentRestartRecoveryLaunch" ] ;
63- clearAcceptedRecovery : ReturnType <
64- typeof createSubagentRunManager
65- > [ "clearAcceptedSubagentRestartRecovery" ] ;
60+ isCurrent : ( runId : string , entry : SubagentRunRecord ) => boolean ;
61+ abandonLaunch : SubagentRunManager [ "abandonSubagentRestartRecoveryLaunch" ] ;
62+ clearAcceptedRecovery : SubagentRunManager [ "clearAcceptedSubagentRestartRecovery" ] ;
6663 getRun : ( runId : string ) => SubagentRunRecord | undefined ;
67- replaceRun : ReturnType < typeof createSubagentRunManager > [ "replaceSubagentRunAfterSteer" ] ;
68- markLaunchAttempted : ReturnType <
69- typeof createSubagentRunManager
70- > [ "markSubagentRestartRecoveryLaunchAttempted" ] ;
71- markLaunchAccepted : ReturnType <
72- typeof createSubagentRunManager
73- > [ "markSubagentRestartRecoveryLaunchAccepted" ] ;
74- markLaunchConsumed : ReturnType <
75- typeof createSubagentRunManager
76- > [ "markSubagentRestartRecoveryLaunchConsumed" ] ;
77- resetLaunchAttempt : ReturnType <
78- typeof createSubagentRunManager
79- > [ "resetSubagentRestartRecoveryLaunchAttempt" ] ;
80- reserveLaunch : ReturnType <
81- typeof createSubagentRunManager
82- > [ "reserveSubagentRestartRecoveryLaunch" ] ;
83- resumeAcceptedRecovery : ReturnType <
84- typeof createSubagentRunManager
85- > [ "resumeSettledSubagentRestartRecovery" ] ;
64+ replaceRun : SubagentRunManager [ "replaceSubagentRunAfterSteer" ] ;
65+ markLaunchAttempted : SubagentRunManager [ "markSubagentRestartRecoveryLaunchAttempted" ] ;
66+ markLaunchAccepted : SubagentRunManager [ "markSubagentRestartRecoveryLaunchAccepted" ] ;
67+ markLaunchConsumed : SubagentRunManager [ "markSubagentRestartRecoveryLaunchConsumed" ] ;
68+ resetLaunchAttempt : SubagentRunManager [ "resetSubagentRestartRecoveryLaunchAttempt" ] ;
69+ reserveLaunch : SubagentRunManager [ "reserveSubagentRestartRecoveryLaunch" ] ;
70+ resumeAcceptedRecovery : SubagentRunManager [ "resumeSettledSubagentRestartRecovery" ] ;
8671 warn : ( message : string , meta ?: Record < string , unknown > ) => void ;
8772} ;
8873
@@ -95,7 +80,7 @@ async function reconcileAcceptedRecovery(params: {
9580 clearAcceptedRecovery : RestartRecoveryParams [ "clearAcceptedRecovery" ] ;
9681 entry : SubagentRunRecord ;
9782 getRun : RestartRecoveryParams [ "getRun" ] ;
98- isCurrent : ( ) => boolean ;
83+ isCurrent : RestartRecoveryParams [ "isCurrent" ] ;
9984 now : number ;
10085 receipt : SubagentRestartRecoveryReceipt ;
10186 replaceRun : RestartRecoveryParams [ "replaceRun" ] ;
@@ -117,7 +102,7 @@ async function reconcileAcceptedRecovery(params: {
117102 let remapped = false ;
118103 try {
119104 remapped =
120- params . isCurrent ( ) &&
105+ params . isCurrent ( params . runId , params . entry ) &&
121106 params . replaceRun ( {
122107 previousRunId : params . runId ,
123108 nextRunId : params . receipt . idempotencyKey ,
@@ -143,7 +128,11 @@ async function reconcileAcceptedRecovery(params: {
143128 } ;
144129 }
145130 const successor = params . getRun ( params . receipt . idempotencyKey ) ;
146- if ( ! successor || successor . execution . restartRecovery !== params . receipt ) {
131+ if (
132+ ! successor ||
133+ successor . execution . restartRecovery !== params . receipt ||
134+ ! params . isCurrent ( successor . runId , successor )
135+ ) {
147136 params . warn ( "accepted subagent restart recovery lost its remapped owner" , {
148137 runId : params . runId ,
149138 childSessionKey : params . childSessionKey ,
@@ -152,6 +141,10 @@ async function reconcileAcceptedRecovery(params: {
152141 }
153142 owner = successor ;
154143 }
144+ const ownsAcceptedTarget = ( ) =>
145+ params . isCurrent ( owner . runId , owner ) &&
146+ owner . execution . restartRecovery === params . receipt &&
147+ isRestartRecoveryLifecycleCurrent ( params . receipt ) ;
155148
156149 if (
157150 ! params . currentSessionId ||
@@ -173,10 +166,7 @@ async function reconcileAcceptedRecovery(params: {
173166 ! ( await settleAcceptedRecoverySession ( {
174167 attempts : params . attempts ,
175168 childSessionKey : params . childSessionKey ,
176- isOwnerCurrent : ( ) =>
177- params . getRun ( owner . runId ) === owner &&
178- owner . execution . restartRecovery === params . receipt &&
179- isRestartRecoveryLifecycleCurrent ( params . receipt ) ,
169+ isOwnerCurrent : ownsAcceptedTarget ,
180170 sessionId : params . receipt . sessionId ,
181171 sessionLifecycleRevision : params . receipt . sessionLifecycleRevision ,
182172 now : params . now ,
@@ -222,9 +212,9 @@ async function reconcileAcceptedRecovery(params: {
222212 target : { runId : owner . runId , entry : owner } ,
223213 } ;
224214 }
225-
226215 try {
227216 if (
217+ ! ownsAcceptedTarget ( ) ||
228218 ! params . clearAcceptedRecovery ( {
229219 runId : owner . runId ,
230220 expected : owner ,
@@ -247,6 +237,7 @@ async function reconcileAcceptedRecovery(params: {
247237 return { status : "deferred" } ;
248238 }
249239 if (
240+ ! params . isCurrent ( owner . runId , owner ) ||
250241 ! params . resumeAcceptedRecovery ( {
251242 runId : owner . runId ,
252243 expected : owner ,
@@ -272,10 +263,10 @@ export async function recoverInterruptedSubagentRow(
272263 params . entry . execution . outcome ?. status === "timeout" &&
273264 typeof params . entry . execution . endedAt === "number" ;
274265 const acceptedRecoveryCurrent =
275- initialRecoveryReceipt ?. phase === "accepted" && params . isCurrent ( ) ;
266+ initialRecoveryReceipt ?. phase === "accepted" && params . isCurrent ( params . runId , params . entry ) ;
276267 const isRecoverySourceCurrent = ( ) =>
277268 isRecoveryAttemptLifecycleCurrent ( ) &&
278- params . isCurrent ( ) &&
269+ params . isCurrent ( params . runId , params . entry ) &&
279270 params . entry . pauseReason !== "sessions_yield" &&
280271 params . entry . suppressAnnounceReason !== "steer-restart" &&
281272 params . entry . killReconciliation === undefined &&
0 commit comments