From 8d299895c468f51b4a211a0923a4fa3c4c3384d2 Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Wed, 21 Oct 2026 11:23:17 +0200 Subject: [PATCH 1/8] =?UTF-8?q?test:=20reproduce=20#778=20=E2=80=94=20snap?= =?UTF-8?q?shot=20boot=20estimate=20stays=20at=2070=20s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/drivers/android/index.test.ts | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/drivers/android/index.test.ts b/src/drivers/android/index.test.ts index 26db23124..54c4c4c28 100644 --- a/src/drivers/android/index.test.ts +++ b/src/drivers/android/index.test.ts @@ -799,7 +799,7 @@ describe("AndroidDriver", () => { }); }); - it("boots a shutdown device from its persisted clean baseline after a driver restart", async () => { + it("quotes a 4 s prepare boot for a persisted clean baseline and keeps unknown or recovery boots at 70 s", async () => { const harness = await provisionedHarness(); const booted = await harness.driver.makeReady(harness.device); @@ -835,6 +835,19 @@ describe("AndroidDriver", () => { markWriteExpectation("emulator-5586", "device-0"), ]); const restartedDriver = await createDriver(harness.filesystem, restartedRunner); + const spec = { + physical: false, + model: "Pixel 5", + osVersion: "34", + platform: "android", + } as const; + // why: Proposed per-device estimate input for #778: the current operation-only input + // cannot distinguish this persisted baseline from a cold boot of the same spec. + const prepareEstimate = { device: booted, operation: "boot", purpose: "prepare" } as const; + const recoveryEstimate = { ...prepareEstimate, purpose: "recover" } as const; + expect(await restartedDriver.estimate({ operation: "boot" }, spec)).toBe(70_000); + expect(await restartedDriver.estimate(recoveryEstimate, spec)).toBe(70_000); + const estimatedBootMs = await restartedDriver.estimate(prepareEstimate, spec); await expect(restartedDriver.makeReady(booted)).resolves.toMatchObject({ address: "emulator-5586", @@ -851,6 +864,7 @@ describe("AndroidDriver", () => { ], command: binaries.emulator, }); + expect(estimatedBootMs).toBe(4_000); }); describe("android.emulator launch options", () => { From 1497c5f2db71a23f28262e966848fafd8e865eef Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Sat, 24 Oct 2026 21:21:17 +0200 Subject: [PATCH 2/8] fix(android): quote a 4 s boot for a device with a matching clean baseline (#778) --- src/core/driver.ts | 11 +++- src/drivers/android/index.ts | 54 ++++++++++++++++++-- src/leasing/lease-acquisition-coordinator.ts | 17 ++++-- 3 files changed, 73 insertions(+), 9 deletions(-) diff --git a/src/core/driver.ts b/src/core/driver.ts index b317fdc29..8f668ec91 100644 --- a/src/core/driver.ts +++ b/src/core/driver.ts @@ -149,7 +149,12 @@ export type ReclaimStrategy = ReclaimResult["strategy"]; */ export type DriverEstimate = | { readonly operation: "provision" } - | { readonly operation: "boot" } + | { + readonly operation: "boot"; + /** why: With a `device` a driver may quote that device's own boot path, which it reads asynchronously. */ + readonly device?: DriverDevice; + readonly purpose?: "prepare" | "recover"; + } | { readonly operation: "reclaim"; readonly clean: "standard" | "full" }; export interface DriverCatalogEntry { @@ -351,6 +356,10 @@ export interface Driver { listPresentPhysical(enrolledIds: readonly string[]): Promise; /** Read-only: must never trigger a runtime / system-image download. */ listCatalog(): Promise; + estimate( + estimate: DriverEstimate & { readonly operation: "boot"; readonly device: DriverDevice }, + spec: DeviceSpec, + ): number | Promise; estimate(estimate: DriverEstimate, spec: DeviceSpec): number; leaseEnvironment( device: VirtualDriverDevice | PhysicalDriverDevice, diff --git a/src/drivers/android/index.ts b/src/drivers/android/index.ts index 9cd2f0220..f4909000f 100644 --- a/src/drivers/android/index.ts +++ b/src/drivers/android/index.ts @@ -123,6 +123,7 @@ const LICENSE_ACCEPT_ANSWERS = 100; const SIGKILL_REAP_TIMEOUT_MS = 5_000; const OWNERSHIP_PROBE_TIMEOUT_MS = 5_000; const SNAPSHOT_BOOT_ESTIMATE_MS = 4_000; +const ESTIMATED_HASH_FRESH_MS = 30_000; // why: Conservative estimate for a system-image download+install -- reserved by `ComponentInstaller` // before `sdkmanager --install` ever starts, so a full disk fails fast instead of filling up // mid-download. @@ -509,6 +510,10 @@ export class AndroidDriver implements Driver { readonly #legacyAvdHome: string; readonly #emulatorFlags: readonly string[]; readonly #baselineLaunchInputs: readonly string[]; + readonly #estimatedConfigHashes = new Map< + string, + { readonly at: number; readonly hash: string } + >(); readonly #locks = new Map>(); readonly #onDiagnostic: ((diagnostic: AndroidDriverDiagnostic) => void) | undefined; readonly #portAllocator: PortAllocator; @@ -1407,12 +1412,18 @@ export class AndroidDriver implements Driver { }; } - estimate(estimate: DriverEstimate, spec: DeviceSpec): number { + estimate( + estimate: DriverEstimate & { readonly operation: "boot"; readonly device: DriverDevice }, + spec: DeviceSpec, + ): Promise; + estimate(estimate: DriverEstimate, spec: DeviceSpec): number; + estimate(estimate: DriverEstimate, spec: DeviceSpec): number | Promise { switch (estimate.operation) { case "provision": return PROVISION_ESTIMATE_MS; case "boot": - return COLD_BOOT_ESTIMATE_MS; + if (estimate.device === undefined) return COLD_BOOT_ESTIMATE_MS; + return this.#bootEstimate(estimate.device, estimate.purpose, spec); case "reclaim": if (spec.physical) return PHYSICAL_RECLAIM_ESTIMATE_MS; // why: `standard` can still fall back to the wipe branch (about five times longer); `Doctor` @@ -2103,12 +2114,49 @@ export class AndroidDriver implements Driver { } } + /** + * why: Read-only twin of `#reconcileBaseline` and the `makeReady` branches: the snapshot figure + * is quoted only where `makeReady` would load the baseline, so an unknown, invalidated, wipe-pending + * or recovery boot keeps the cold figure. + */ + async #bootEstimate( + device: DriverDevice, + purpose: "prepare" | "recover" | undefined, + spec: DeviceSpec, + ): Promise { + if (spec.physical || purpose === "recover") return COLD_BOOT_ESTIMATE_MS; + const data = this.#dataFor(device); + const state = this.#devices.get(data.avdName); + if (state?.needsWipe === true) return COLD_BOOT_ESTIMATE_MS; + const baselineHash = await this.#baselineHash(data.avdName); + if (baselineHash === undefined) return COLD_BOOT_ESTIMATE_MS; + const currentHash = await this.#currentConfigHash( + data.avdName, + state?.imageIdentity ?? data.imageIdentity ?? "", + ); + this.#estimatedConfigHashes.set(data.avdName, { at: this.#clock.now(), hash: currentHash }); + return baselineHash === currentHash ? SNAPSHOT_BOOT_ESTIMATE_MS : COLD_BOOT_ESTIMATE_MS; + } + + /** + * why: The hash a boot estimate just computed answers the `makeReady` it quotes for, once: it saves + * a second `emulator -version` run and is dropped on use so no later decision reads a stale one. + */ + async #takeEstimatedConfigHash(avdName: string, imageIdentity: string): Promise { + const estimated = this.#estimatedConfigHashes.get(avdName); + this.#estimatedConfigHashes.delete(avdName); + if (estimated !== undefined && this.#clock.now() - estimated.at < ESTIMATED_HASH_FRESH_MS) { + return estimated.hash; + } + return this.#currentConfigHash(avdName, imageIdentity); + } + async #reconcileBaseline(data: AndroidDriverData, state: DeviceState): Promise { const baselineHash = await this.#baselineHash(data.avdName); if (state.needsWipe || baselineHash === undefined) { return; } - const currentHash = await this.#currentConfigHash(data.avdName, state.imageIdentity); + const currentHash = await this.#takeEstimatedConfigHash(data.avdName, state.imageIdentity); if (baselineHash === currentHash) { state.baselineCaptured = true; state.snapshotExpected = true; diff --git a/src/leasing/lease-acquisition-coordinator.ts b/src/leasing/lease-acquisition-coordinator.ts index 8dc966958..75ec17d74 100644 --- a/src/leasing/lease-acquisition-coordinator.ts +++ b/src/leasing/lease-acquisition-coordinator.ts @@ -884,9 +884,9 @@ export class LeaseAcquisitionCoordinator implements AcquisitionMaintenance { return; } if (action.kind === "boot-shutdown") { - waiter.timing = estimatedBootTiming( + waiter.timing = await estimatedBootTiming( this.options.drivers.get(action.device.spec.platform), - action.device.spec, + action.device, ); await this.#bootShutdown(waiter, action.device, action.capacityReservation, action.claim); return; @@ -1140,7 +1140,10 @@ export class LeaseAcquisitionCoordinator implements AcquisitionMaintenance { try { this.options.queue.notifyProgress(waiter, { stage: "booting", - etaMs: driver.estimate({ operation: "boot" }, device.spec), + etaMs: await driver.estimate( + { device: toDriverDevice(device), operation: "boot", purpose: "prepare" }, + device.spec, + ), }); const ready = await this.options.lifecycle.bootForLease(device, claim); if (ready === undefined) throw new Error(`Boot target is no longer safe: ${device.id}`); @@ -1362,8 +1365,12 @@ function estimatedTiming(driver: Driver, spec: DeviceSpec): LeaseTiming { }; } -function estimatedBootTiming(driver: Driver, spec: DeviceSpec): LeaseTiming { - const estimatedBootMs = driver.estimate({ operation: "boot" }, spec); +async function estimatedBootTiming(driver: Driver, device: DeviceRecord): Promise { + const spec = device.spec; + const estimatedBootMs = await driver.estimate( + { device: toDriverDevice(device), operation: "boot", purpose: "prepare" }, + spec, + ); return { estimatedBootMs, estimatedProvisionMs: 0, From 09cd7b7f5be0984ab8f4f9c336fd3fab9ff9f5b3 Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Sat, 24 Oct 2026 21:21:57 +0200 Subject: [PATCH 3/8] refactor(android): split the baseline match out of the boot estimate (#778) --- src/drivers/android/index.ts | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/drivers/android/index.ts b/src/drivers/android/index.ts index f4909000f..c6801e610 100644 --- a/src/drivers/android/index.ts +++ b/src/drivers/android/index.ts @@ -2128,14 +2128,19 @@ export class AndroidDriver implements Driver { const data = this.#dataFor(device); const state = this.#devices.get(data.avdName); if (state?.needsWipe === true) return COLD_BOOT_ESTIMATE_MS; + const matches = await this.#baselineMatches(data, state?.imageIdentity ?? data.imageIdentity); + return matches ? SNAPSHOT_BOOT_ESTIMATE_MS : COLD_BOOT_ESTIMATE_MS; + } + + async #baselineMatches( + data: AndroidDriverData, + imageIdentity: string | undefined, + ): Promise { const baselineHash = await this.#baselineHash(data.avdName); - if (baselineHash === undefined) return COLD_BOOT_ESTIMATE_MS; - const currentHash = await this.#currentConfigHash( - data.avdName, - state?.imageIdentity ?? data.imageIdentity ?? "", - ); + if (baselineHash === undefined) return false; + const currentHash = await this.#currentConfigHash(data.avdName, imageIdentity ?? ""); this.#estimatedConfigHashes.set(data.avdName, { at: this.#clock.now(), hash: currentHash }); - return baselineHash === currentHash ? SNAPSHOT_BOOT_ESTIMATE_MS : COLD_BOOT_ESTIMATE_MS; + return baselineHash === currentHash; } /** From beaa47fd1d4dbec6a4a931f9205836b0f89d6015 Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Sat, 24 Oct 2026 21:25:05 +0200 Subject: [PATCH 4/8] test(leasing): a shut-down device's boot progress and timing quote the device's own prepare figure (#778) --- src/leasing/create-leasing.test.ts | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/src/leasing/create-leasing.test.ts b/src/leasing/create-leasing.test.ts index 69fc483c5..5dd432b01 100644 --- a/src/leasing/create-leasing.test.ts +++ b/src/leasing/create-leasing.test.ts @@ -6,6 +6,8 @@ import { BootTimeoutError, type ComponentInstaller, type Config, + type DeviceSpec, + type DriverEstimate, DriverCrashError, type VirtualSpec, type LeaseProgress, @@ -983,9 +985,19 @@ describe("createLeasing", () => { }); }); - it("reports only the selected work as it begins", async () => { + it("reports only the selected work as it begins, quoting a shut-down device's own prepare-boot figure", async () => { const clock = new FakeClock(1_000); - const driver = new FakeDriver({ + // why: A real driver quotes a persisted-baseline boot below its cold figure; the fake does so only + // for an estimate that names the device and the prepare purpose. + class DeviceQuotingDriver extends FakeDriver { + override estimate(estimate: DriverEstimate, spec: DeviceSpec): number { + if (estimate.operation === "boot" && estimate.purpose === "prepare") { + return estimate.device === undefined ? 99 : 4; + } + return super.estimate(estimate, spec); + } + } + const driver = new DeviceQuotingDriver({ availableOsVersions: ["26.5"], clock, estimateMs: { boot: 20, provision: 10, reclaim: 15 }, @@ -1010,12 +1022,18 @@ describe("createLeasing", () => { payload: { deviceId: shutdown.id, initiator: "test" }, }); const booted: string[] = []; - await harness.engine.request(request, { - onProgress: (progress) => booted.push(progress.stage), + const bootEtas: (number | undefined)[] = []; + const bootedGrant = await harness.engine.request(request, { + onProgress: (progress) => { + booted.push(progress.stage); + bootEtas.push(progress.stage === "booting" ? progress.etaMs : undefined); + }, requesterId: "shutdown", ownerId: "shutdown", }); expect(booted).toEqual(["booting"]); + expect(bootEtas).toEqual([4]); + expect(bootedGrant.timing.estimatedBootMs).toBe(4); }); it("reports queue insertion without speculative work and isolates callback failures", async () => { From 962fd064fa1229640497eebc7335757130d046a9 Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Sat, 24 Oct 2026 21:29:00 +0200 Subject: [PATCH 5/8] test(android): a mismatched, wipe-pending or physical prepare boot keeps the 70 s estimate (#778) --- src/drivers/android/index.test.ts | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/drivers/android/index.test.ts b/src/drivers/android/index.test.ts index 54c4c4c28..88e22e709 100644 --- a/src/drivers/android/index.test.ts +++ b/src/drivers/android/index.test.ts @@ -799,7 +799,7 @@ describe("AndroidDriver", () => { }); }); - it("quotes a 4 s prepare boot for a persisted clean baseline and keeps unknown or recovery boots at 70 s", async () => { + it("quotes a 4 s prepare boot for a persisted clean baseline and keeps unknown, recovery, mismatched, wipe-pending and physical boots at 70 s", async () => { const harness = await provisionedHarness(); const booted = await harness.driver.makeReady(harness.device); @@ -865,6 +865,31 @@ describe("AndroidDriver", () => { command: binaries.emulator, }); expect(estimatedBootMs).toBe(4_000); + + // why: Each row is a prepare boot that `makeReady` would not load the baseline for, so it keeps the cold figure. + const mismatchedRunner = new ScriptedProcessRunner([ + processResult(binaries.emulator, ["-version"], "Android emulator version 37.0.0"), + ]); + const mismatchedDriver = await createDriver(harness.filesystem, mismatchedRunner); + // why: A full reclaim leaves the baseline hash matching but flags the next boot to wipe data. + const wipePendingRunner = new ScriptedProcessRunner([ + avdPathExpectation("emulator-5586"), + processResult(binaries.adb, ["-s", "emulator-5586", "emu", "kill"]), + processResult(binaries.emulator, ["-version"], "Android emulator version 36.1.9"), + ]); + const wipePendingDriver = await createDriver(harness.filesystem, wipePendingRunner); + await wipePendingDriver.reclaim(booted, { clean: "full" }); + expect({ + mismatchedBaselineHash: await mismatchedDriver.estimate(prepareEstimate, spec), + physicalSpec: await restartedDriver.estimate(prepareEstimate, { + class: "phone", + model: "Pixel 5", + osVersion: "34", + physical: true, + platform: "android", + }), + wipePending: await wipePendingDriver.estimate(prepareEstimate, spec), + }).toEqual({ mismatchedBaselineHash: 70_000, physicalSpec: 70_000, wipePending: 70_000 }); }); describe("android.emulator launch options", () => { From bc13bff08d7c09a72c0ec9f74989dbc1b307b503 Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Sat, 24 Oct 2026 21:36:09 +0200 Subject: [PATCH 6/8] fix(leasing): a rejecting device boot estimate falls back to the cold figure instead of stranding the waiter (#778) --- src/leasing/create-leasing.test.ts | 39 ++++++++++++++++++++ src/leasing/lease-acquisition-coordinator.ts | 19 +++++----- 2 files changed, 49 insertions(+), 9 deletions(-) diff --git a/src/leasing/create-leasing.test.ts b/src/leasing/create-leasing.test.ts index 5dd432b01..47cf3ebdb 100644 --- a/src/leasing/create-leasing.test.ts +++ b/src/leasing/create-leasing.test.ts @@ -1036,6 +1036,45 @@ describe("createLeasing", () => { expect(bootedGrant.timing.estimatedBootMs).toBe(4); }); + it("grants a shut-down device with the cold boot figure when its own boot estimate rejects", async () => { + const clock = new FakeClock(1_000); + class RejectingDeviceEstimateDriver extends FakeDriver { + override estimate(estimate: DriverEstimate, spec: DeviceSpec): number { + if (estimate.operation === "boot" && estimate.device !== undefined) { + return Promise.reject(new Error("emulator -version failed")) as unknown as number; + } + return super.estimate(estimate, spec); + } + } + const driver = new RejectingDeviceEstimateDriver({ + availableOsVersions: ["26.5"], + clock, + estimateMs: { boot: 20, provision: 10, reclaim: 15 }, + platform: "ios", + }); + const harness = await createHarness({ driver, idleShutdownAfterMs: 0 }); + const first = await harness.engine.request(request, { requesterId: "a", ownerId: "a" }); + await harness.engine.release(first.lease.id, "explicit"); + await harness.engine.settle(); + const shutdown = harness.registry.snapshot.devices[0]; + if (shutdown === undefined) throw new Error("Expected provisioned device"); + await harness.registry.transitionDevice(shutdown.id, "shutdown", { + event: "device.shutdown", + payload: { deviceId: shutdown.id, initiator: "test" }, + }); + + const granted = harness.engine + .request(request, { requesterId: "b", ownerId: "b" }) + .then((grant) => grant.timing.estimatedBootMs); + // why: Without the fallback the waiter hangs, so the race turns that hang into a named failure. + const outcome = await Promise.race([ + granted, + new Promise((resolve) => setTimeout(() => resolve("still pending"), 1_000)), + ]); + + expect(outcome).toBe(20); + }); + it("reports queue insertion without speculative work and isolates callback failures", async () => { const clock = new FakeClock(1_000); const driver = new FakeDriver({ availableOsVersions: ["26.5"], clock, platform: "ios" }); diff --git a/src/leasing/lease-acquisition-coordinator.ts b/src/leasing/lease-acquisition-coordinator.ts index 75ec17d74..90777aee2 100644 --- a/src/leasing/lease-acquisition-coordinator.ts +++ b/src/leasing/lease-acquisition-coordinator.ts @@ -1135,15 +1135,11 @@ export class LeaseAcquisitionCoordinator implements AcquisitionMaintenance { capacityReservation: CapacityReservation, claim: DeviceOperationClaim, ): Promise { - const driver = this.options.drivers.get(device.spec.platform); let handoff: ReadyDeviceHandoff; try { this.options.queue.notifyProgress(waiter, { stage: "booting", - etaMs: await driver.estimate( - { device: toDriverDevice(device), operation: "boot", purpose: "prepare" }, - device.spec, - ), + etaMs: waiter.timing.estimatedBootMs, }); const ready = await this.options.lifecycle.bootForLease(device, claim); if (ready === undefined) throw new Error(`Boot target is no longer safe: ${device.id}`); @@ -1367,10 +1363,15 @@ function estimatedTiming(driver: Driver, spec: DeviceSpec): LeaseTiming { async function estimatedBootTiming(driver: Driver, device: DeviceRecord): Promise { const spec = device.spec; - const estimatedBootMs = await driver.estimate( - { device: toDriverDevice(device), operation: "boot", purpose: "prepare" }, - spec, - ); + // why: The quote is advisory. A driver that cannot read the device's own boot path (a failed + // `emulator -version`, say) must not strand the claim and capacity reservation that the boot + // below releases, so the device-less figure stands in. + const estimatedBootMs = await Promise.resolve( + driver.estimate( + { device: toDriverDevice(device), operation: "boot", purpose: "prepare" }, + spec, + ), + ).catch(() => driver.estimate({ operation: "boot" }, spec)); return { estimatedBootMs, estimatedProvisionMs: 0, From 0195d48f2a0b8e0acbfd9d7a216ebdd09b79dded Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Sat, 24 Oct 2026 21:36:41 +0200 Subject: [PATCH 7/8] refactor(android): the boot estimate and makeReady read one baseline verdict (#778) --- src/drivers/android/index.test.ts | 1 + src/drivers/android/index.ts | 65 ++++++++++++------------------- 2 files changed, 25 insertions(+), 41 deletions(-) diff --git a/src/drivers/android/index.test.ts b/src/drivers/android/index.test.ts index 88e22e709..431a3a811 100644 --- a/src/drivers/android/index.test.ts +++ b/src/drivers/android/index.test.ts @@ -771,6 +771,7 @@ describe("AndroidDriver", () => { const ready = await harness.driver.makeReady(harness.device); const restartedRunner = new ScriptedProcessRunner([ + processResult(binaries.emulator, ["-version"], "Android emulator version 36.1.9"), processResult(binaries.emulator, ["-version"], "Android emulator version 36.1.9"), avdPathExpectation("emulator-5586"), processResult( diff --git a/src/drivers/android/index.ts b/src/drivers/android/index.ts index c6801e610..2de7c1687 100644 --- a/src/drivers/android/index.ts +++ b/src/drivers/android/index.ts @@ -123,7 +123,6 @@ const LICENSE_ACCEPT_ANSWERS = 100; const SIGKILL_REAP_TIMEOUT_MS = 5_000; const OWNERSHIP_PROBE_TIMEOUT_MS = 5_000; const SNAPSHOT_BOOT_ESTIMATE_MS = 4_000; -const ESTIMATED_HASH_FRESH_MS = 30_000; // why: Conservative estimate for a system-image download+install -- reserved by `ComponentInstaller` // before `sdkmanager --install` ever starts, so a full disk fails fast instead of filling up // mid-download. @@ -510,10 +509,6 @@ export class AndroidDriver implements Driver { readonly #legacyAvdHome: string; readonly #emulatorFlags: readonly string[]; readonly #baselineLaunchInputs: readonly string[]; - readonly #estimatedConfigHashes = new Map< - string, - { readonly at: number; readonly hash: string } - >(); readonly #locks = new Map>(); readonly #onDiagnostic: ((diagnostic: AndroidDriverDiagnostic) => void) | undefined; readonly #portAllocator: PortAllocator; @@ -2115,10 +2110,22 @@ export class AndroidDriver implements Driver { } /** - * why: Read-only twin of `#reconcileBaseline` and the `makeReady` branches: the snapshot figure - * is quoted only where `makeReady` would load the baseline, so an unknown, invalidated, wipe-pending - * or recovery boot keeps the cold figure. + * why: The one decision both `makeReady` (through `#reconcileBaseline`) and the boot estimate read, + * so the figure quoted is the boot `makeReady` then takes: `load` where the baseline is recorded + * and matches the current emulator and image, `stale` where it is recorded but no longer matches, + * and `none` where a wipe is pending or no baseline is recorded. It changes nothing. */ + async #baselineVerdict( + data: AndroidDriverData, + needsWipe: boolean, + imageIdentity: string, + ): Promise<"load" | "stale" | "none"> { + const baselineHash = await this.#baselineHash(data.avdName); + if (needsWipe || baselineHash === undefined) return "none"; + const currentHash = await this.#currentConfigHash(data.avdName, imageIdentity); + return baselineHash === currentHash ? "load" : "stale"; + } + async #bootEstimate( device: DriverDevice, purpose: "prepare" | "recover" | undefined, @@ -2127,42 +2134,18 @@ export class AndroidDriver implements Driver { if (spec.physical || purpose === "recover") return COLD_BOOT_ESTIMATE_MS; const data = this.#dataFor(device); const state = this.#devices.get(data.avdName); - if (state?.needsWipe === true) return COLD_BOOT_ESTIMATE_MS; - const matches = await this.#baselineMatches(data, state?.imageIdentity ?? data.imageIdentity); - return matches ? SNAPSHOT_BOOT_ESTIMATE_MS : COLD_BOOT_ESTIMATE_MS; - } - - async #baselineMatches( - data: AndroidDriverData, - imageIdentity: string | undefined, - ): Promise { - const baselineHash = await this.#baselineHash(data.avdName); - if (baselineHash === undefined) return false; - const currentHash = await this.#currentConfigHash(data.avdName, imageIdentity ?? ""); - this.#estimatedConfigHashes.set(data.avdName, { at: this.#clock.now(), hash: currentHash }); - return baselineHash === currentHash; - } - - /** - * why: The hash a boot estimate just computed answers the `makeReady` it quotes for, once: it saves - * a second `emulator -version` run and is dropped on use so no later decision reads a stale one. - */ - async #takeEstimatedConfigHash(avdName: string, imageIdentity: string): Promise { - const estimated = this.#estimatedConfigHashes.get(avdName); - this.#estimatedConfigHashes.delete(avdName); - if (estimated !== undefined && this.#clock.now() - estimated.at < ESTIMATED_HASH_FRESH_MS) { - return estimated.hash; - } - return this.#currentConfigHash(avdName, imageIdentity); + const verdict = await this.#baselineVerdict( + data, + state?.needsWipe === true, + state?.imageIdentity ?? data.imageIdentity ?? "", + ); + return verdict === "load" ? SNAPSHOT_BOOT_ESTIMATE_MS : COLD_BOOT_ESTIMATE_MS; } async #reconcileBaseline(data: AndroidDriverData, state: DeviceState): Promise { - const baselineHash = await this.#baselineHash(data.avdName); - if (state.needsWipe || baselineHash === undefined) { - return; - } - const currentHash = await this.#takeEstimatedConfigHash(data.avdName, state.imageIdentity); - if (baselineHash === currentHash) { + const verdict = await this.#baselineVerdict(data, state.needsWipe, state.imageIdentity); + if (verdict === "none") return; + if (verdict === "load") { state.baselineCaptured = true; state.snapshotExpected = true; return; From 83f93a4e0a23c9d631c184c354a7675b7e6e3144 Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Sat, 24 Oct 2026 21:37:32 +0200 Subject: [PATCH 8/8] test(android): the restart tests script the second emulator -version run (#778) --- src/drivers/android/index.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/drivers/android/index.test.ts b/src/drivers/android/index.test.ts index 431a3a811..acb7c516c 100644 --- a/src/drivers/android/index.test.ts +++ b/src/drivers/android/index.test.ts @@ -771,7 +771,6 @@ describe("AndroidDriver", () => { const ready = await harness.driver.makeReady(harness.device); const restartedRunner = new ScriptedProcessRunner([ - processResult(binaries.emulator, ["-version"], "Android emulator version 36.1.9"), processResult(binaries.emulator, ["-version"], "Android emulator version 36.1.9"), avdPathExpectation("emulator-5586"), processResult( @@ -805,6 +804,7 @@ describe("AndroidDriver", () => { const booted = await harness.driver.makeReady(harness.device); const restartedRunner = new ScriptedProcessRunner([ + processResult(binaries.emulator, ["-version"], "Android emulator version 36.1.9"), processResult(binaries.emulator, ["-version"], "Android emulator version 36.1.9"), processResult(binaries.adb, ["devices"], "List of devices attached\n"), { @@ -853,7 +853,7 @@ describe("AndroidDriver", () => { await expect(restartedDriver.makeReady(booted)).resolves.toMatchObject({ address: "emulator-5586", }); - expect(restartedRunner.calls[2]).toMatchObject({ + expect(restartedRunner.calls[3]).toMatchObject({ args: [ "-avd", "simlock_one",