@@ -29,6 +29,7 @@ import {
2929} from "@t3tools/contracts" ;
3030import * as Context from "effect/Context" ;
3131import type * as Cause from "effect/Cause" ;
32+ import * as Clock from "effect/Clock" ;
3233import * as Crypto from "effect/Crypto" ;
3334import * as Deferred from "effect/Deferred" ;
3435import * as Effect from "effect/Effect" ;
@@ -42,6 +43,11 @@ import * as SynchronizedRef from "effect/SynchronizedRef";
4243import * as McpInvocationContext from "./McpInvocationContext.ts" ;
4344
4445const HOST_RESPONSE_GRACE_MS = 1_000 ;
46+ /**
47+ * A host evicted for a slow response is still running and re-registers within
48+ * about a second. Its tabs survive, so its agents keep their current tab.
49+ */
50+ const EVICTED_LEASE_GRACE_MS = 5_000 ;
4551
4652export interface PreviewAutomationInvokeInput {
4753 readonly scope : McpInvocationContext . McpThreadInvocationScope ;
@@ -93,7 +99,10 @@ interface PendingRequest {
9399 readonly context : PreviewAutomationRequestErrorContext ;
94100}
95101
96- /** A lease belongs to this exact connectionId/queue; replacing the host invalidates it. */
102+ /**
103+ * A lease lives as long as the connection it names; a host evicted for a timeout
104+ * hands its leases to its own re-registration (see `EVICTED_LEASE_GRACE_MS`).
105+ */
97106interface HostAssignment {
98107 readonly clientId : ClientConnection [ "clientId" ] ;
99108 readonly connectionId : ClientConnection [ "connectionId" ] ;
@@ -102,6 +111,13 @@ interface HostAssignment {
102111 readonly tabSequence ?: number ;
103112}
104113
114+ interface ReleasedLease {
115+ readonly clientId : ClientConnection [ "clientId" ] ;
116+ readonly tabId ?: PreviewTabId ;
117+ readonly tabSequence ?: number ;
118+ readonly releasedAt : number ;
119+ }
120+
105121interface PreviewAutomationRequestErrorContext {
106122 readonly operation : PreviewAutomationOperation ;
107123 readonly environmentId : McpInvocationContext . McpInvocationScope [ "environmentId" ] ;
@@ -120,6 +136,8 @@ interface PreviewAutomationRequestErrorContext {
120136interface BrokerState {
121137 readonly clients : ReadonlyMap < string , ClientConnection > ;
122138 readonly assignments : ReadonlyMap < string , HostAssignment > ;
139+ /** Leases of hosts evicted for a timeout, kept until they re-register or the grace ends. */
140+ readonly released : ReadonlyMap < string , ReleasedLease > ;
123141 readonly pending : ReadonlyMap < string , PendingRequest > ;
124142 readonly requestSequence : number ;
125143 readonly focusSequence : number ;
@@ -129,22 +147,32 @@ const removeConnectionFromState = (
129147 current : BrokerState ,
130148 clientId : string ,
131149 queue : ClientConnection [ "queue" ] ,
150+ releasedAt ?: number ,
132151) : { readonly state : BrokerState ; readonly disconnected : ReadonlyArray < PendingRequest > } => {
133152 const clients = new Map ( current . clients ) ;
134153 const assignments = new Map ( current . assignments ) ;
154+ const released = new Map ( current . released ) ;
135155 const pending = new Map ( current . pending ) ;
136156 const disconnected : PendingRequest [ ] = [ ] ;
137157 if ( current . clients . get ( clientId ) ?. queue === queue ) clients . delete ( clientId ) ;
138158 for ( const [ assignmentKey , assignment ] of assignments ) {
139- if ( assignment . queue === queue ) assignments . delete ( assignmentKey ) ;
159+ if ( assignment . queue !== queue ) continue ;
160+ assignments . delete ( assignmentKey ) ;
161+ if ( releasedAt === undefined ) continue ;
162+ released . set ( assignmentKey , {
163+ clientId : assignment . clientId ,
164+ ...( assignment . tabId === undefined ? { } : { tabId : assignment . tabId } ) ,
165+ ...( assignment . tabSequence === undefined ? { } : { tabSequence : assignment . tabSequence } ) ,
166+ releasedAt,
167+ } ) ;
140168 }
141169 for ( const [ requestId , entry ] of pending ) {
142170 if ( entry . queue !== queue ) continue ;
143171 pending . delete ( requestId ) ;
144172 disconnected . push ( entry ) ;
145173 }
146174 return {
147- state : { ...current , clients, assignments, pending } ,
175+ state : { ...current , clients, assignments, released , pending } ,
148176 disconnected,
149177 } ;
150178} ;
@@ -351,6 +379,7 @@ export const make = Effect.gen(function* PreviewAutomationBrokerMake() {
351379 const state = yield * SynchronizedRef . make < BrokerState > ( {
352380 clients : new Map ( ) ,
353381 assignments : new Map ( ) ,
382+ released : new Map ( ) ,
354383 pending : new Map ( ) ,
355384 requestSequence : 0 ,
356385 focusSequence : 0 ,
@@ -382,11 +411,14 @@ export const make = Effect.gen(function* PreviewAutomationBrokerMake() {
382411 queue : ClientConnection [ "queue" ] ,
383412 completeStream = false ,
384413 ) {
414+ // Only a timeout keeps the host's leases: a closed or replaced stream may
415+ // belong to a runtime whose tabs are gone.
416+ const releasedAt = completeStream ? yield * Clock . currentTimeMillis : undefined ;
385417 yield * SynchronizedRef . modifyEffect ( state , ( current ) => {
386418 if ( current . clients . get ( clientId ) ?. queue !== queue ) {
387419 return Effect . succeed ( [ undefined , current ] as const ) ;
388420 }
389- const removed = removeConnectionFromState ( current , clientId , queue ) ;
421+ const removed = removeConnectionFromState ( current , clientId , queue , releasedAt ) ;
390422 return closeConnection ( queue , removed . disconnected , completeStream ) . pipe (
391423 Effect . as ( [ undefined , removed . state ] as const ) ,
392424 ) ;
@@ -412,12 +444,28 @@ export const make = Effect.gen(function* PreviewAutomationBrokerMake() {
412444 focusOrder : 0 ,
413445 queue,
414446 } ;
447+ const now = yield * Clock . currentTimeMillis ;
415448 const registration = yield * SynchronizedRef . modify ( state , ( current ) => {
416449 const previousConnection = current . clients . get ( clientId ) ;
417450 const removed = previousConnection
418451 ? removeConnectionFromState ( current , clientId , previousConnection . queue )
419452 : { state : current , disconnected : [ ] } ;
420453 const clients = new Map ( removed . state . clients ) ;
454+ const assignments = new Map ( removed . state . assignments ) ;
455+ const released = new Map ( removed . state . released ) ;
456+ for ( const [ assignmentKey , lease ] of released ) {
457+ const expired = now - lease . releasedAt > EVICTED_LEASE_GRACE_MS ;
458+ if ( ! expired && lease . clientId !== clientId ) continue ;
459+ released . delete ( assignmentKey ) ;
460+ if ( expired || assignments . has ( assignmentKey ) ) continue ;
461+ assignments . set ( assignmentKey , {
462+ clientId,
463+ connectionId,
464+ queue,
465+ ...( lease . tabId === undefined ? { } : { tabId : lease . tabId } ) ,
466+ ...( lease . tabSequence === undefined ? { } : { tabSequence : lease . tabSequence } ) ,
467+ } ) ;
468+ }
421469 const focusSequence = removed . state . focusSequence + 1 ;
422470 const registeredConnection = { ...connection , focusOrder : focusSequence } ;
423471 clients . set ( clientId , registeredConnection ) ;
@@ -427,7 +475,7 @@ export const make = Effect.gen(function* PreviewAutomationBrokerMake() {
427475 disconnected : removed . disconnected ,
428476 registeredConnection,
429477 } ,
430- { ...removed . state , clients, focusSequence } ,
478+ { ...removed . state , clients, assignments , released , focusSequence } ,
431479 ] as const ;
432480 } ) ;
433481 if ( registration . previousConnection ) {
0 commit comments