@@ -22,7 +22,14 @@ import {
2222 type RevylPlatform ,
2323} from './device.ts' ;
2424import { createRevylClient , isRevylSessionGone , type RevylClient } from './revyl-client.ts' ;
25- import { startRevylSession , stopRevylSession , type RevylSession } from './revyl-session.ts' ;
25+ import {
26+ revylCallerGaveUp ,
27+ revylCallerWait ,
28+ startRevylSession ,
29+ stopRevylSession ,
30+ type RevylCallerBudget ,
31+ type RevylSession ,
32+ } from './revyl-session.ts' ;
2633import { createRevylInteractor } from './interactor.ts' ;
2734import { providerRuntimeOwner } from '@agent-device/contracts/platform-runtime' ;
2835import { resolveRevylRuntimeInstance } from './runtime-instance.ts' ;
@@ -115,7 +122,7 @@ class RevylRuntimeImplementation implements ProviderDeviceRuntime {
115122 * One entry per lease whose device is being rented, kept until the session reaches the table. A second
116123 * allocation of that lease joins it rather than renting a device nobody will stop.
117124 */
118- private readonly pendingAllocations = new Map < string , Promise < RevylAllocation | undefined > > ( ) ;
125+ private readonly pendingAllocations = new Map < string , Promise < RevylAllocation > > ( ) ;
119126 private shuttingDown = false ;
120127 readonly provider = REVYL_PROVIDER ;
121128
@@ -260,28 +267,65 @@ class RevylRuntimeImplementation implements ProviderDeviceRuntime {
260267 * nothing pointing at it; the second call joins the first instead. The session reaches the table while
261268 * the start is still registered, so a third allocation cannot arrive in the gap between one finishing
262269 * and the other learning about it and rent a device of its own.
270+ *
271+ * A caller joins only after its own flags are refused and its own budget is installed, so joining
272+ * neither accepts a profile Revyl would ignore nor inherits the patience of the caller that started
273+ * first.
263274 */
264275 private async allocateOncePerLease (
265276 lease : DeviceLease ,
266277 context ?: LeaseLifecycleContext ,
267278 ) : Promise < RevylAllocation | undefined > {
279+ const platform =
280+ lease . leaseProvider === this . provider
281+ ? platformForRevylLeaseBackend ( lease . backend )
282+ : undefined ;
283+ if ( platform === undefined ) return undefined ;
284+ const { rejectRefusedRevylProfileFields } = await import ( './session-allocation.ts' ) ;
285+ // Before the join below: an allocation that reuses a live lease still carries flags of its own,
286+ // and Revyl reads none of them — whether it starts a device or joins one that is starting.
287+ rejectRefusedRevylProfileFields ( context ) ;
268288 const pending = this . pendingAllocations . get ( lease . leaseId ) ;
269- if ( pending ) {
270- // The device was taken by the call already running, so this one is not the one to end it.
271- const joined = await pending ;
272- return joined === undefined ? undefined : { ...joined , rentedNow : false } ;
273- }
274- const starting = this . allocate ( lease , context ) ;
289+ if ( pending ) return await this . joinAllocation ( pending , platform , context ) ;
290+ const starting = this . allocate ( lease , platform , context ) ;
275291 this . pendingAllocations . set ( lease . leaseId , starting ) ;
276292 try {
277293 const allocated = await starting ;
278- if ( allocated && ! this . shuttingDown ) this . sessions . set ( lease . leaseId , allocated . session ) ;
294+ // Recorded while the start is still registered, so no allocation can arrive in the gap after it
295+ // and rent a device of its own.
296+ if ( ! this . shuttingDown ) this . sessions . set ( lease . leaseId , allocated . session ) ;
279297 return allocated ;
280298 } finally {
281299 this . pendingAllocations . delete ( lease . leaseId ) ;
282300 }
283301 }
284302
303+ /**
304+ * Waits for the start another caller already has running for this lease, within this caller's own
305+ * budget. Hanging up here never stops that start: the device belongs to every caller that joined it, so
306+ * this one reports its own cancellation or its own expired deadline and the start runs on for the
307+ * callers still waiting. A joiner never took the device, so it never ends it either way.
308+ */
309+ private async joinAllocation (
310+ pending : Promise < RevylAllocation > ,
311+ platform : RevylPlatform ,
312+ context ?: LeaseLifecycleContext ,
313+ ) : Promise < RevylAllocation > {
314+ const budget = revylCallerBudget ( context ) ;
315+ // The number the report names is the wait this caller was granted, so it is measured from the moment
316+ // the join started rather than from the deadline's own epoch instant.
317+ const joinedAt = Date . now ( ) ;
318+ const gaveUp = revylCallerWait < never > ( budget . deadline ?? Number . POSITIVE_INFINITY , budget , ( ) =>
319+ revylCallerGaveUp ( platform , budget , Math . max ( 0 , ( budget . deadline ?? joinedAt ) - joinedAt ) ) ,
320+ ) ;
321+ if ( gaveUp === undefined ) return await joinOf ( pending ) ;
322+ try {
323+ return await joinOf ( Promise . race ( [ pending , gaveUp . promise ] ) ) ;
324+ } finally {
325+ gaveUp . dispose ( ) ;
326+ }
327+ }
328+
285329 /** Resolves once no allocation is in flight, so shutdown stops everything that exists. */
286330 private async awaitInFlightAllocations ( ) : Promise < void > {
287331 const pending = [ ...this . inFlightAllocations ] ;
@@ -291,21 +335,14 @@ class RevylRuntimeImplementation implements ProviderDeviceRuntime {
291335 }
292336
293337 /**
294- * One allocation's session, with `rentedNow` naming whether this call is the one that took the
295- * device. A lease of another provider, or of a backend Revyl has no device for, is answered with
296- * nothing, which is how the core reads "this provider has no allocation to report".
338+ * One allocation's session, with `rentedNow` naming whether this call is the one that took the device.
339+ * The lease is already known to be Revyl's, and its context already refused, by the caller above.
297340 */
298341 private async allocate (
299342 lease : DeviceLease ,
343+ platform : RevylPlatform ,
300344 context ?: LeaseLifecycleContext ,
301- ) : Promise < RevylAllocation | undefined > {
302- if ( lease . leaseProvider !== this . provider ) return undefined ;
303- const platform = platformForRevylLeaseBackend ( lease . backend ) ;
304- if ( ! platform ) return undefined ;
305- const { rejectRefusedRevylProfileFields } = await import ( './session-allocation.ts' ) ;
306- // Before the reuse below: a repeat allocation of a live lease carries flags of its own, and
307- // Revyl reads none of them a second time.
308- rejectRefusedRevylProfileFields ( context ) ;
345+ ) : Promise < RevylAllocation > {
309346 if ( this . shuttingDown ) throw shuttingDownDuring ( lease . leaseId , 'allocate' ) ;
310347 // A repeat allocation of a live lease is that same device, and Revyl is asked for nothing.
311348 const existing = this . sessions . get ( lease . leaseId ) ;
@@ -340,8 +377,7 @@ class RevylRuntimeImplementation implements ProviderDeviceRuntime {
340377 appUrl : resolveRevylAppUrl ( context ) ,
341378 deviceModel : selection . deviceModel ,
342379 osVersion : selection . osVersion ,
343- ...( context ?. signal === undefined ? { } : { signal : context . signal } ) ,
344- ...( context ?. deadline === undefined ? { } : { deadline : context . deadline } ) ,
380+ ...revylCallerBudget ( context ) ,
345381 } ) ;
346382 }
347383
@@ -422,6 +458,25 @@ class RevylRuntimeImplementation implements ProviderDeviceRuntime {
422458 }
423459}
424460
461+ /**
462+ * The caller's own patience, read from the lease context it arrived with. A caller that names neither
463+ * waits for whatever the work takes.
464+ */
465+ function revylCallerBudget ( context ?: LeaseLifecycleContext ) : RevylCallerBudget {
466+ return {
467+ ...( context ?. signal === undefined ? { } : { signal : context . signal } ) ,
468+ ...( context ?. deadline === undefined ? { } : { deadline : context . deadline } ) ,
469+ } ;
470+ }
471+
472+ /**
473+ * What a joined allocation reports: the device the caller that started it rented, which this call did
474+ * not take and therefore does not owe ending.
475+ */
476+ async function joinOf ( pending : Promise < RevylAllocation > ) : Promise < RevylAllocation > {
477+ return { ...( await pending ) , rentedNow : false } ;
478+ }
479+
425480/** Recovery and lifecycle calls arrive for one provider's lease; anything else is a routing bug. */
426481function requireRevylLease ( lease : DeviceLease , operation : 'recover' | 'release' ) : void {
427482 if ( lease . leaseProvider === REVYL_PROVIDER && platformForRevylLeaseBackend ( lease . backend ) ) return ;
0 commit comments