@@ -1370,37 +1370,49 @@ export const layerWithOptions = (
13701370 }
13711371 }
13721372 } ) . pipe (
1373- Effect . onError ( ( ) =>
1374- attachedTo === undefined
1375- ? Effect . void
1376- : removeThreadAttachment ( { ...input , runtime : attachedTo } ) . pipe (
1377- Effect . andThen (
1378- Effect . suspend ( ( ) => {
1379- dropReservation ( ) ;
1380- // Other live processes hold reused MCP credentials; revocation would break their clients.
1381- if ( preparedForCleanup ?. issued !== true ) return Effect . void ;
1382- const mcpCredentialId = preparedForCleanup . mcpCredentialId ;
1383- const attachedRuntime = attachedTo ;
1384- return Ref . get ( sessions ) . pipe (
1385- Effect . flatMap ( ( current ) =>
1386- ( mcpCredentialId !== undefined &&
1387- isMcpCredentialReserved ( input . threadId , mcpCredentialId ) ) ||
1388- Array . from ( current . values ( ) ) . some (
1389- ( other ) =>
1390- other . runtime !== attachedRuntime &&
1391- ( other . attachedThreadIds . has ( input . threadId ) ||
1392- ( mcpCredentialId !== undefined &&
1393- other . mcpCredentialIdByThread . get ( input . threadId ) ===
1394- mcpCredentialId ) ) ,
1395- )
1396- ? Effect . void
1397- : clearMcpSession ( input . threadId , mcpCredentialId ) ,
1398- ) ,
1399- ) ;
1373+ Effect . onError ( ( ) => {
1374+ // A reclaim (thread already attached, credential rotated) has
1375+ // attachedTo === undefined but still issued a credential that must
1376+ // be cleaned up on failure. Only skip when nothing was prepared.
1377+ const isReclaim = attachedTo === undefined && preparedForCleanup !== undefined ;
1378+ if ( attachedTo === undefined && ! isReclaim ) return Effect . void ;
1379+ const removeAttachment =
1380+ attachedTo === undefined
1381+ ? Effect . void
1382+ : removeThreadAttachment ( { ...input , runtime : attachedTo } ) ;
1383+ return removeAttachment . pipe (
1384+ Effect . andThen (
1385+ Effect . suspend ( ( ) => {
1386+ dropReservation ( ) ;
1387+ // Other live processes hold reused MCP credentials; revocation would break their clients.
1388+ if ( preparedForCleanup ?. issued !== true ) return Effect . void ;
1389+ const mcpCredentialId = preparedForCleanup . mcpCredentialId ;
1390+ // For a reclaim, exclude the session being reused (not torn
1391+ // down) from the holder check, mirroring new attachments.
1392+ return Ref . get ( sessions ) . pipe (
1393+ Effect . flatMap ( ( current ) => {
1394+ const attachedRuntime =
1395+ attachedTo ?? current . get ( sessionKey ( input . providerSessionId ) ) ?. runtime ;
1396+ const held =
1397+ ( mcpCredentialId !== undefined &&
1398+ isMcpCredentialReserved ( input . threadId , mcpCredentialId ) ) ||
1399+ Array . from ( current . values ( ) ) . some (
1400+ ( other ) =>
1401+ other . runtime !== attachedRuntime &&
1402+ ( other . attachedThreadIds . has ( input . threadId ) ||
1403+ ( mcpCredentialId !== undefined &&
1404+ other . mcpCredentialIdByThread . get ( input . threadId ) ===
1405+ mcpCredentialId ) ) ,
1406+ ) ;
1407+ return held
1408+ ? Effect . void
1409+ : clearMcpSession ( input . threadId , mcpCredentialId ) ;
14001410 } ) ,
1401- ) ,
1402- ) ,
1403- ) ,
1411+ ) ;
1412+ } ) ,
1413+ ) ,
1414+ ) ;
1415+ } ) ,
14041416 ) ;
14051417 return threadAttachment
14061418 . withLock ( threadAttachmentKey ( input ) , attach )
0 commit comments