@@ -3,6 +3,7 @@ import path from 'node:path';
33import { afterEach , beforeEach , expect , test , vi } from 'vitest' ;
44import type { DeviceInfo } from '@agent-device/kernel/device' ;
55import { createRequestCanceledError , isRequestCanceledError } from '@agent-device/kernel/errors' ;
6+ import { resetAllProcessMemosForTests } from '@agent-device/kernel/ttl-memo' ;
67import {
78 buildDetachedRunnerLease ,
89 buildRunnerLease ,
@@ -14,6 +15,7 @@ import { isIosRunnerDetachEnabled, tryAdoptRunnerSessionFromLease } from '../run
1415import { sendRunnerCommandOnce } from '../runner-transport.ts' ;
1516import { resolveExpectedRunnerCacheMetadata } from '../runner-xctestrun.ts' ;
1617import { appleRunnerTestHost } from '../test-host.ts' ;
18+ import { appleToolchainProbeResult } from './apple-toolchain-fixtures.ts' ;
1719import { mkdtempForTestSync } from './tmp-dir.ts' ;
1820
1921vi . mock ( '../runner-transport.ts' , async ( importOriginal ) => {
@@ -143,12 +145,12 @@ test('adoption succeeds for a live, matching, probe-healthy runner', async () =>
143145 expect ( readStaleRunnerLease ( simulator . id ) ) . toBeNull ( ) ;
144146} ) ;
145147
146- test ( 'a request canceled during the fingerprint probe fails adoption instead of skipping it' , async ( ) => {
148+ test ( 'a request canceled during the fingerprint probe fails adoption instead of skipping it, cold or with the fingerprint cache warm ' , async ( ) => {
147149 // The fingerprint check runs the same blocking toolchain probes a fresh
148150 // startup would, and it is handed the request's signal. A cancellation from
149151 // there is not an unresolvable derived path: swallowing it would let startup
150152 // walk on past a client that is already gone (#2422).
151- writeStaleLease ( ) ;
153+ const lease = writeStaleLease ( ) ;
152154 mockIsProcessAlive . mockReturnValue ( true ) ;
153155 const request = new AbortController ( ) ;
154156 request . abort ( ) ;
@@ -162,6 +164,33 @@ test('a request canceled during the fingerprint probe fails adoption instead of
162164 tryAdoptRunnerSessionFromLease ( simulator , { signal : request . signal } ) ,
163165 ) . rejects . toSatisfy ( isRequestCanceledError ) ;
164166 expect ( mockSendRunnerCommandOnce ) . not . toHaveBeenCalled ( ) ;
167+
168+ // The mock above only proves adoption propagates whatever the fingerprint
169+ // call throws. Exercise the real production code path too: warm the actual
170+ // toolchain fingerprint memo (the module-level cache in
171+ // runner-cache-metadata.ts is a singleton the mock above never touches),
172+ // then adopt again with an already-aborted signal. Before the fix,
173+ // requireRunnerToolchainFingerprint returned the memoized value without
174+ // checking the signal, so a cache hit let adoption go on to probe uptime
175+ // and write the lease for an already-canceled request (#2422 round 4).
176+ resetAllProcessMemosForTests ( ) ;
177+ const { resolveExpectedRunnerCacheMetadata : actualResolveExpectedRunnerCacheMetadata } =
178+ await vi . importActual < typeof import ( '../runner-xctestrun.ts' ) > ( '../runner-xctestrun.ts' ) ;
179+ appleRunnerTestHost . update ( { runCmdSync : vi . fn ( appleToolchainProbeResult ) } ) ;
180+ actualResolveExpectedRunnerCacheMetadata ( simulator ) ;
181+
182+ const warmRequest = new AbortController ( ) ;
183+ warmRequest . abort ( ) ;
184+ mockResolveExpectedRunnerCacheMetadata . mockImplementationOnce (
185+ actualResolveExpectedRunnerCacheMetadata ,
186+ ) ;
187+
188+ await expect (
189+ tryAdoptRunnerSessionFromLease ( simulator , { signal : warmRequest . signal } ) ,
190+ ) . rejects . toSatisfy ( isRequestCanceledError ) ;
191+ expect ( mockSendRunnerCommandOnce ) . not . toHaveBeenCalled ( ) ;
192+ // Ownership was never transferred: the stale lease is untouched.
193+ expect ( readStaleRunnerLease ( simulator . id ) ?. ownerToken ) . toBe ( lease . ownerToken ) ;
165194} ) ;
166195
167196test ( 'adoption is skipped for a recycled runner pid (start time mismatch)' , async ( ) => {
0 commit comments