@@ -136,6 +136,42 @@ const make = Effect.gen(function* () {
136136 ...( wake === undefined ? { } : { wake } ) ,
137137 } ) ;
138138
139+ const linkedOutcome = ( linked : PeerLink ) : LinkRequestOutcome => ( {
140+ linkStatus : "linked" ,
141+ linkedEnvironmentId : linked . environmentId ,
142+ linkedLabel : linked . label ,
143+ linkedAccess : linked . access ,
144+ } ) ;
145+
146+ // One answer per card at a time: two answers racing on one card must not
147+ // both link. Other cards go on while a slow peer holds one.
148+ const answerLocks = yield * KeyedLock . make < TurnItemId > ( ) ;
149+
150+ /**
151+ * Closes a card still waiting when its agent retries naming a machine linked
152+ * meanwhile. Nothing wakes the agent: the retry itself returns the link.
153+ */
154+ const closeAsLinked = ( threadId : ThreadId , turnItemId : TurnItemId , linked : PeerLink ) =>
155+ answerLocks . withLock (
156+ turnItemId ,
157+ Effect . gen ( function * ( ) {
158+ const card = yield * readCard ( threadId , turnItemId ) ;
159+ if (
160+ card === undefined ||
161+ card . linkStatus !== "pending" ||
162+ card . runId === null ||
163+ card . nodeId === null
164+ ) {
165+ return ;
166+ }
167+ yield * record (
168+ { ...card , runId : card . runId , nodeId : card . nodeId } ,
169+ linkedOutcome ( linked ) ,
170+ CommandId . make ( `link-request:${ card . id } :linked-on-retry` ) ,
171+ ) ;
172+ } ) ,
173+ ) ;
174+
139175 const request : PeerLinkRequests [ "Service" ] [ "request" ] = Effect . fn ( "PeerLinkRequests.request" ) (
140176 function * ( scope , input ) {
141177 // The card is shown in, and answered from, the caller's own thread.
@@ -171,6 +207,21 @@ const make = Effect.gen(function* () {
171207 ) ,
172208 ) ;
173209 if ( Option . isSome ( linked ) ) {
210+ if ( input . clientRequestId !== undefined ) {
211+ yield * closeAsLinked (
212+ threadId ,
213+ cardId ( threadId , scope . requestNamespace , input . clientRequestId ) ,
214+ linked . value ,
215+ ) . pipe (
216+ Effect . mapError (
217+ ( error ) =>
218+ new OrchestratorMcpFailure ( {
219+ code : "orchestration_error" ,
220+ message : `Could not close the link request: ${ errorMessage ( error ) } ` ,
221+ } ) ,
222+ ) ,
223+ ) ;
224+ }
174225 return {
175226 status : "linked" ,
176227 environmentId : linked . value . environmentId ,
@@ -180,12 +231,8 @@ const make = Effect.gen(function* () {
180231 }
181232 }
182233 const key = input . clientRequestId ?? ( yield * crypto . randomUUIDv4 ( ) . pipe ( Effect . orDie ) ) ;
183- // Turn item ids are global; scope the key to this thread and provider
184- // session, since a later session may reuse it. A retry with the same
185- // clientRequestId finds this card and records nothing.
186- const turnItemId = TurnItemId . make (
187- `turn-item:link-request:${ encodeURIComponent ( threadId ) } :${ encodeURIComponent ( scope . requestNamespace ) } :${ encodeURIComponent ( key ) } ` ,
188- ) ;
234+ // A retry with the same clientRequestId finds this card and records nothing.
235+ const turnItemId = cardId ( threadId , scope . requestNamespace , key ) ;
189236 if ( input . clientRequestId !== undefined ) {
190237 const existing = yield * readCard ( threadId , turnItemId ) . pipe ( Effect . mapError ( unreadable ) ) ;
191238 if ( existing !== undefined ) return resultOf ( existing ) ;
@@ -244,13 +291,6 @@ const make = Effect.gen(function* () {
244291 Effect . withSpan ( "PeerLinkRequests.request" ) ,
245292 ) ;
246293
247- const linkedOutcome = ( linked : PeerLink ) : LinkRequestOutcome => ( {
248- linkStatus : "linked" ,
249- linkedEnvironmentId : linked . environmentId ,
250- linkedLabel : linked . label ,
251- linkedAccess : linked . access ,
252- } ) ;
253-
254294 /** Links the machine the user picked, or finds the link they chose to keep. */
255295 const answerOutcome = (
256296 answer : PeerLinkRequestAnswerInput [ "answer" ] ,
@@ -300,9 +340,6 @@ const make = Effect.gen(function* () {
300340 }
301341 } ;
302342
303- // One answer per card at a time: two answers racing on one card must not
304- // both link. Other cards go on while a slow peer holds one.
305- const answerLocks = yield * KeyedLock . make < TurnItemId > ( ) ;
306343 const answer : PeerLinkRequests [ "Service" ] [ "answer" ] = Effect . fn ( "PeerLinkRequests.answer" ) (
307344 function * ( input ) {
308345 yield * Effect . annotateCurrentSpan ( {
@@ -345,6 +382,15 @@ const make = Effect.gen(function* () {
345382 return PeerLinkRequests . of ( { request, answer } ) ;
346383} ) ;
347384
385+ /**
386+ * Turn item ids are global; scope the key to this thread and provider session,
387+ * since a later session may reuse it.
388+ */
389+ const cardId = ( threadId : ThreadId , requestNamespace : string , key : string ) =>
390+ TurnItemId . make (
391+ `turn-item:link-request:${ encodeURIComponent ( threadId ) } :${ encodeURIComponent ( requestNamespace ) } :${ encodeURIComponent ( key ) } ` ,
392+ ) ;
393+
348394const pendingResult = ( turnItemId : TurnItemId ) : OrchestratorMcpEnvironmentLinkResult => ( {
349395 status : "pending" ,
350396 turnItemId,
0 commit comments