@@ -37,6 +37,7 @@ import { createRevylInteractor } from './interactor.ts';
3737import { providerRuntimeOwner } from '@agent-device/contracts/platform-runtime' ;
3838import { resolveRevylRuntimeInstance } from './runtime-instance.ts' ;
3939import { revylSessionIdForLease } from './session-identity.ts' ;
40+ import type { RevylStartRequest } from './session-allocation.ts' ;
4041
4142export type RevylRuntimeOptions = Readonly < {
4243 apiKey : string ;
@@ -81,6 +82,8 @@ type RevylStopOutcome = Readonly<{
8182 * session that start produced, and the `/stop` that has not been confirmed yet.
8283 */
8384class RevylLeaseEntry {
85+ /** What the start was asked for, which a joining allocation has to ask for too. */
86+ readonly request : RevylStartRequest ;
8487 /**
8588 * Settles once this lease's `start_device` has answered and the stop that a failed start owed has
8689 * settled with it, so a reader that awaits it cannot miss a rented device.
@@ -92,15 +95,26 @@ class RevylLeaseEntry {
9295 stop : RevylRentedSession | undefined ;
9396 /** Whether Revyl confirmed `/stop` for {@link stop}. Only this lets the entry leave the map. */
9497 stopConfirmed : boolean ;
98+ /**
99+ * The `/stop` in flight for {@link stop}, shared by every release, recovery and shutdown that arrives
100+ * while it runs, so Revyl sees one stop per attempt. Cleared once it settles, so a failed stop is
101+ * retried by the next caller.
102+ */
103+ stopping : Promise < string | undefined > | undefined ;
95104
96105 /**
97106 * `start` fills this entry in as it goes; it may write the entry's own fields, but it must never wait
98107 * on them, because the assignment below has not happened while its body first runs.
99108 */
100- constructor ( start : ( entry : RevylLeaseEntry ) => Promise < RevylStartOutcome > ) {
109+ constructor (
110+ request : RevylStartRequest ,
111+ start : ( entry : RevylLeaseEntry ) => Promise < RevylStartOutcome > ,
112+ ) {
113+ this . request = request ;
101114 this . session = undefined ;
102115 this . stop = undefined ;
103116 this . stopConfirmed = false ;
117+ this . stopping = undefined ;
104118 this . start = start ( this ) ;
105119 }
106120}
@@ -251,16 +265,25 @@ class RevylRuntimeImplementation implements ProviderDeviceRuntime {
251265 ? platformForRevylLeaseBackend ( lease . backend )
252266 : undefined ;
253267 if ( platform === undefined ) return undefined ;
254- const { rejectRefusedRevylProfileFields } = await import ( './session-allocation.ts' ) ;
268+ const {
269+ rejectRefusedRevylProfileFields,
270+ requireSameRevylStartRequest,
271+ resolveRevylStartRequest,
272+ } = await import ( './session-allocation.ts' ) ;
255273 // Before the join below: an allocation that reuses a live lease still carries flags of its own,
256274 // and Revyl reads none of them — whether it starts a device or joins one that is starting.
257275 rejectRefusedRevylProfileFields ( context ) ;
276+ const request = resolveRevylStartRequest ( context , {
277+ ...( this . options . deviceModel === undefined ? { } : { deviceModel : this . options . deviceModel } ) ,
278+ ...( this . options . osVersion === undefined ? { } : { osVersion : this . options . osVersion } ) ,
279+ } ) ;
258280 // Nothing between here and the entry's registration awaits, so one caller can never miss another's.
259281 if ( this . shuttingDown ) throw shuttingDownDuring ( lease . leaseId , 'allocate' ) ;
260282 const existing = this . leases . get ( lease . leaseId ) ;
283+ if ( existing ) requireSameRevylStartRequest ( lease . leaseId , existing . request , request ) ;
261284 // The caller that started the device waits on it the same way a joiner does: one entry, one awaitable,
262285 // each caller bounded by its own patience.
263- const entry = existing ?? this . rent ( lease , platform , context ) ;
286+ const entry = existing ?? this . rent ( lease , platform , request , context ) ;
264287 const outcome = await this . join ( entry , platform , context ) ;
265288 if ( this . shuttingDown ) {
266289 // The device this start rented is already an entry shutdown is ending, so this caller only reports
@@ -276,8 +299,15 @@ class RevylRuntimeImplementation implements ProviderDeviceRuntime {
276299 * map before the first `await`, so an allocation that arrives mid-start joins it instead of renting a
277300 * second device, and a release that arrives then has somewhere to find the session.
278301 */
279- private rent ( lease : DeviceLease , platform : RevylPlatform , context ?: LeaseLifecycleContext ) {
280- const entry = new RevylLeaseEntry ( ( target ) => this . runStart ( target , lease , platform , context ) ) ;
302+ private rent (
303+ lease : DeviceLease ,
304+ platform : RevylPlatform ,
305+ request : RevylStartRequest ,
306+ context ?: LeaseLifecycleContext ,
307+ ) {
308+ const entry = new RevylLeaseEntry ( request , ( target ) =>
309+ this . runStart ( target , lease , platform , context ) ,
310+ ) ;
281311 this . leases . set ( lease . leaseId , entry ) ;
282312 return entry ;
283313 }
@@ -297,7 +327,13 @@ class RevylRuntimeImplementation implements ProviderDeviceRuntime {
297327 const leaseId = lease . leaseId ;
298328 let started : RevylStart ;
299329 try {
300- started = await this . startLeaseDevice ( lease , platform , context ) ;
330+ started = await startRevylSession ( this . client , lease , {
331+ platform,
332+ appUrl : entry . request . appUrl ,
333+ deviceModel : entry . request . deviceModel ,
334+ osVersion : entry . request . osVersion ,
335+ ...revylCallerBudget ( context ) ,
336+ } ) ;
301337 } catch ( error ) {
302338 // The start failed somewhere this module does not model, and the request may already have gone out,
303339 // so the id it carried is the handle the entry has to end.
@@ -323,7 +359,7 @@ class RevylRuntimeImplementation implements ProviderDeviceRuntime {
323359 entry . stop = rented ;
324360 // The entry ends what a failed start rented rather than waiting for a release to ask: the device is
325361 // billed from the moment Revyl made it, and this is the first moment its id is known.
326- const failure = await this . stopRented ( rented ) ;
362+ const failure = await this . stopOwed ( entry , rented ) ;
327363 if ( failure === undefined ) {
328364 entry . stopConfirmed = true ;
329365 this . closeLease ( leaseId , entry ) ;
@@ -383,7 +419,7 @@ class RevylRuntimeImplementation implements ProviderDeviceRuntime {
383419 const session = entry . session ;
384420 // A failed start has already ended its own session before this point, and a caller that releases such
385421 // a lease must not make Revyl see a second stop for a session already gone.
386- const failure = entry . stopConfirmed ? undefined : await this . stopRented ( target ) ;
422+ const failure = entry . stopConfirmed ? undefined : await this . stopOwed ( entry , target ) ;
387423 if ( failure === undefined ) {
388424 entry . stopConfirmed = true ;
389425 this . closeLease ( leaseId , entry ) ;
@@ -418,6 +454,17 @@ class RevylRuntimeImplementation implements ProviderDeviceRuntime {
418454 return outcome . data ?? { providerSessionId : outcome . providerSessionId } ;
419455 }
420456
457+ /** The entry's one in-flight `/stop`, started by the first caller and joined by the rest. */
458+ private stopOwed (
459+ entry : RevylLeaseEntry ,
460+ target : RevylRentedSession ,
461+ ) : Promise < string | undefined > {
462+ entry . stopping ??= this . stopRented ( target ) . finally ( ( ) => {
463+ entry . stopping = undefined ;
464+ } ) ;
465+ return entry . stopping ;
466+ }
467+
421468 /**
422469 * One `/stop`, with Revyl's own "no such session" read as released: that device is not rented. The
423470 * failure's message is what the caller reports; `undefined` is the confirmation the entry needs.
@@ -431,38 +478,6 @@ class RevylRuntimeImplementation implements ProviderDeviceRuntime {
431478 }
432479 }
433480
434- /**
435- * One start. The caller's own budget rides along, so a start that runs past it fails as that caller's
436- * expired patience rather than as a device nobody asked for; the request itself keeps the client's
437- * longer budget, because its answer is the only thing that names what was rented.
438- */
439- private async startLeaseDevice (
440- lease : DeviceLease ,
441- platform : RevylPlatform ,
442- context ?: LeaseLifecycleContext ,
443- ) : Promise < RevylStart > {
444- const { resolveRevylAppUrl, resolveRevylDeviceSelection } =
445- await import ( './session-allocation.ts' ) ;
446- const selection = resolveRevylDeviceSelection (
447- context ,
448- this . options . deviceModel === undefined && this . options . osVersion === undefined
449- ? { }
450- : {
451- ...( this . options . deviceModel === undefined
452- ? { }
453- : { deviceModel : this . options . deviceModel } ) ,
454- ...( this . options . osVersion === undefined ? { } : { osVersion : this . options . osVersion } ) ,
455- } ,
456- ) ;
457- return await startRevylSession ( this . client , lease , {
458- platform,
459- appUrl : resolveRevylAppUrl ( context ) ,
460- deviceModel : selection . deviceModel ,
461- osVersion : selection . osVersion ,
462- ...revylCallerBudget ( context ) ,
463- } ) ;
464- }
465-
466481 private sessionForDevice ( device : DeviceInfo ) : RevylSession | undefined {
467482 const parsed = parseRevylDeviceId ( device . id ) ;
468483 if ( ! parsed ) return undefined ;
0 commit comments