From b6e2926e1928d9850ad14bb87bad74bfdd4222e3 Mon Sep 17 00:00:00 2001 From: Kevin Rajan <7121943+kvnloo@users.noreply.github.com> Date: Sat, 17 Oct 2026 04:53:31 -0500 Subject: [PATCH 1/9] refactor(runner): separate reuse revocation from worker retirement --- packages/e2e/src/run/scheduler.ts | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/packages/e2e/src/run/scheduler.ts b/packages/e2e/src/run/scheduler.ts index 5d93615330..615c97664d 100644 --- a/packages/e2e/src/run/scheduler.ts +++ b/packages/e2e/src/run/scheduler.ts @@ -244,6 +244,11 @@ class SchedulerWorker { this.killAfter(graceMs); } + /** Revokes a retired worker's reuse permission without restarting its kill deadline. */ + revokeReuse(): void { + if (this.runner.alive) this.runner.send({ type: 'shutdown' }); + } + /** * Asks for immediate engine disposal and force-kills once the budget is * spent, replacing the interrupt grace a busy worker was given. @@ -499,6 +504,12 @@ class Scheduler { } } + /** + * The forced interrupt: no worker gets to finish its unit any more. Each + * busy one disposes its engine now and is killed after the force budget, + * however long the unit's own grace still had to run. A live retired + * worker also receives it to revoke resource reuse during disposal. + */ private broadcastForce(): void { if (!this.options.forceSignal.aborted || this.forceBroadcast) return; this.forceBroadcast = true; @@ -701,7 +712,12 @@ class Scheduler { * it first, and the others only ever retire idle workers. */ private retire(worker: SchedulerWorker, preserveResources: boolean): void { - if (worker.state === 'retired' && preserveResources) return; + if (worker.state === 'retired') { + // A final interrupt can withdraw reuse permission while the earlier + // replacement retirement is still disposing its engine. + if (!preserveResources) worker.revokeReuse(); + return; + } worker.state = 'retired'; worker.shutdown(SHUTDOWN_GRACE_MS, preserveResources); } From 9393aed53bc2ff705939dc23ef1cbc985fb41704 Mon Sep 17 00:00:00 2001 From: Kevin Rajan <7121943+kvnloo@users.noreply.github.com> Date: Sat, 17 Oct 2026 04:53:51 -0500 Subject: [PATCH 2/9] docs(mobile): restore resource-lifetime invariants for reviewers --- packages/e2e/src/run/worker/protocol.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/e2e/src/run/worker/protocol.ts b/packages/e2e/src/run/worker/protocol.ts index efda941752..c590c602ed 100644 --- a/packages/e2e/src/run/worker/protocol.ts +++ b/packages/e2e/src/run/worker/protocol.ts @@ -108,6 +108,7 @@ export interface InterruptMessage { export interface ShutdownMessage { readonly type: 'shutdown'; + /** Permission to retain run-owned resources for a replacement. Missing means final cleanup. */ readonly preserveResources?: true; } From b9c53d21cb06aefdf410ef3d1f6dfca8742a0d1d Mon Sep 17 00:00:00 2001 From: Kevin Rajan <7121943+kvnloo@users.noreply.github.com> Date: Sat, 17 Oct 2026 04:53:53 -0500 Subject: [PATCH 3/9] docs(mobile): restore resource-lifetime invariants for reviewers --- packages/mobile/src/bindings.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/mobile/src/bindings.ts b/packages/mobile/src/bindings.ts index 1e38b6e8c4..71b2e53b11 100644 --- a/packages/mobile/src/bindings.ts +++ b/packages/mobile/src/bindings.ts @@ -46,6 +46,10 @@ export interface SlotBinding { readonly client?: DeviceClientConfig | undefined; /** App a provider already installed from `appPath`, so the worker installs nothing and opens this. */ readonly installedApp?: string | undefined; + /** + * The slot's provider lease id. The engine uses it to retain healthy + * sessions across worker replacement and to find the lease for `record`. + */ readonly leaseId?: string | undefined; } From 09b3d3bd4f3a4e3dc889497ed02e67fa35088b9e Mon Sep 17 00:00:00 2001 From: Kevin Rajan <7121943+kvnloo@users.noreply.github.com> Date: Sat, 17 Oct 2026 04:53:56 -0500 Subject: [PATCH 4/9] docs(mobile): restore resource-lifetime invariants for reviewers --- packages/mobile/src/pool.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/packages/mobile/src/pool.ts b/packages/mobile/src/pool.ts index 5fd278db27..afd4f0dcdd 100644 --- a/packages/mobile/src/pool.ts +++ b/packages/mobile/src/pool.ts @@ -205,6 +205,13 @@ export class DevicePool { return { workers: bindings.length, env: { [bindingsVariable(info.targetName)]: encodeBindings(bindings) } }; } + /** + * Closes every engine session `warm` opened for the target and releases + * what the source acquired. This is the normal run-end close for leased + * slots and a fallback for local sessions or a failed `prepare`. Session + * closes and source release run concurrently under the hook's cleanup + * budget, so a hung close does not delay lease release. + */ async finish(info: EngineFinishInfo): Promise { const clients = this.warmed.get(info.targetName) ?? []; this.warmed.delete(info.targetName); From dc75665a681cc09d4df47012bd2c22f7e12aa60c Mon Sep 17 00:00:00 2001 From: Kevin Rajan <7121943+kvnloo@users.noreply.github.com> Date: Sat, 17 Oct 2026 04:53:58 -0500 Subject: [PATCH 5/9] docs(mobile): restore resource-lifetime invariants for reviewers --- packages/mobile/src/surface.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/mobile/src/surface.ts b/packages/mobile/src/surface.ts index 507a529615..257490201e 100644 --- a/packages/mobile/src/surface.ts +++ b/packages/mobile/src/surface.ts @@ -363,6 +363,7 @@ function tellHarness(call: () => void): void { export class AgentDeviceSurface { private client: AgentDeviceClient | undefined; private attempt: Attempt | undefined; + /** Retained across attempt cleanup until a stop or native session reset is confirmed. */ private recording: Recording | undefined; /** The attempt's `EngineAttemptContext.screen`, which locate captures feed; undefined between attempts. */ private screenSink: ((snapshot: EngineSnapshot) => void) | undefined; From d2c00ab67e6872859d5c07b298b4eb3d63696e74 Mon Sep 17 00:00:00 2001 From: Kevin Rajan <7121943+kvnloo@users.noreply.github.com> Date: Sat, 17 Oct 2026 04:54:01 -0500 Subject: [PATCH 6/9] docs(mobile): restore resource-lifetime invariants for reviewers --- packages/testmu/src/provider.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/testmu/src/provider.ts b/packages/testmu/src/provider.ts index 599ba1da82..81710b8d38 100644 --- a/packages/testmu/src/provider.ts +++ b/packages/testmu/src/provider.ts @@ -193,6 +193,8 @@ export function testmu(options: TestmuOptions): DeviceProvider { leaseBackend, leaseProvider: PROVIDER, ttlMs: LEASE_TTL_MS, + // Retain the provider lease when closeApp or cleanup closes the engine session. + // The provider releases the lease at run end. retainOnClose: true, ...selectors, }, From 7269ba5cd1ec9781acd55c8f472e7d3d18c5f888 Mon Sep 17 00:00:00 2001 From: Kevin Rajan <7121943+kvnloo@users.noreply.github.com> Date: Sat, 17 Oct 2026 04:54:37 -0500 Subject: [PATCH 7/9] refactor(mobile): name the leased-session retention decision --- packages/mobile/src/surface.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/mobile/src/surface.ts b/packages/mobile/src/surface.ts index 257490201e..05ad69ee08 100644 --- a/packages/mobile/src/surface.ts +++ b/packages/mobile/src/surface.ts @@ -705,8 +705,12 @@ export class AgentDeviceSurface { }).catch(() => undefined); }; const reuse = context.reuseSignal; + const canKeepSession = + this.device?.leaseId !== undefined && recording?.kind !== 'screen' && reuse !== undefined && !reuse.aborted; + // Retain only a leased, healthy session while a replacement is allowed. + // Native recording reset and final/forced cleanup must close it. reuse?.addEventListener('abort', close, { once: true }); - if (this.device?.leaseId === undefined || recording?.kind === 'screen' || reuse === undefined || reuse.aborted) close(); + if (!canKeepSession) close(); try { const stopping = recording?.kind === 'lease' ? this.stopLeaseRecording(recording, context.signal).catch(() => undefined) : undefined; await withinCleanupBudget((async () => { From 5f278894c88d03960fe3301c6b740390ed8b1a1d Mon Sep 17 00:00:00 2001 From: Kevin Rajan <7121943+kvnloo@users.noreply.github.com> Date: Sat, 17 Oct 2026 04:54:51 -0500 Subject: [PATCH 8/9] docs(mobile): document native recording reset and lease claim behavior --- docs/reference/mobile.mdx | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/reference/mobile.mdx b/docs/reference/mobile.mdx index 8c1206f1a4..a2d78b5395 100644 --- a/docs/reference/mobile.mdx +++ b/docs/reference/mobile.mdx @@ -709,6 +709,7 @@ await device.openApp('com.example.app'); // launches fresh, on its first screen ``` - A screen query before the next `openApp` is `APP_NOT_OPEN` too. +- If a previous attempt left a native screen recording unfinished, the next attempt resets it by closing the agent-device session. On a leased device, that also drops the session's device claim, even when worker reuse was otherwise permitted. The provider lease itself is released at run end. #### `clearKeychain` From 9d8b0ff12f099fa353e1ab035dd3eb12659875d4 Mon Sep 17 00:00:00 2001 From: Kevin Rajan <7121943+kvnloo@users.noreply.github.com> Date: Sat, 17 Oct 2026 04:54:54 -0500 Subject: [PATCH 9/9] docs(mobile): document native recording reset and lease claim behavior --- .changeset/tame-dogs-listen.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/tame-dogs-listen.md b/.changeset/tame-dogs-listen.md index 2174193f61..e64236fd18 100644 --- a/.changeset/tame-dogs-listen.md +++ b/.changeset/tame-dogs-listen.md @@ -4,6 +4,6 @@ Keep healthy engine sessions open on leased devices across worker replacement. After a failed test, a retired worker could drop the device claim. A competing session could then take the device and make later tests fail with "Device is already in use". -Forced worker shutdown and unconfirmed agent-device recording stops still close the engine session. At run end, the engine closes its sessions and the provider releases its device leases. +Forced worker shutdown and unconfirmed agent-device recording stops still close the engine session. A later attempt also resets a leftover native screen recording by closing that session, dropping its device claim even on a leased slot. At run end, the engine closes its sessions and the provider releases its device leases. Before another attempt on the same worker, retry a provider recording whose stop failed. Wait for that stop to finish before starting the attempt, and keep its original artifact directory. The worker also retries cleanup at attempt end and disposal while it owns the recording handle. Providers remain responsible for any external recording cleanup when releasing a lease at run end. Closing an agent-device session cannot guarantee that a cloud recording stops.