@@ -2,8 +2,9 @@ import path from 'node:path';
22import { resolveIosSimulatorDeviceSetPath } from '../../../../utils/device-isolation.ts' ;
33import { emitDiagnostic } from '../../../../utils/diagnostics.ts' ;
44import { isProcessAlive } from '../../../../utils/process-identity.ts' ;
5+ import { parseBooleanLiteral } from '../../../../utils/source-value.ts' ;
56import type { DeviceInfo } from '../../../../kernel/device.ts' ;
6- import type { ExecResult , ExecBackgroundResult } from '../../../../utils/exec.ts' ;
7+ import type { ExecResult } from '../../../../utils/exec.ts' ;
78import { sendRunnerCommandOnce } from './runner-transport.ts' ;
89import { withRunnerCommandId } from './runner-contract.ts' ;
910import {
@@ -17,17 +18,25 @@ import {
1718 resolveRunnerDerivedPath ,
1819 type RunnerXctestrunArtifact ,
1920} from './runner-xctestrun.ts' ;
20- import type { RunnerSession } from './runner-session-types.ts' ;
21+ import {
22+ buildRunnerSessionId ,
23+ normalizeRunnerStartupTimeoutMs ,
24+ type RunnerProcessHandle ,
25+ type RunnerSession ,
26+ } from './runner-session-types.ts' ;
2127
22- const RUNNER_ADOPTION_PROBE_TIMEOUT_MS = 2_000 ;
28+ // A healthy localhost runner answers uptime in tens of milliseconds and a dead
29+ // port refuses immediately; the timeout only bounds the wedged-runner case,
30+ // where giving up fast matters — the probe runs under the lease lock, in
31+ // series before the restart it would otherwise avoid.
32+ const RUNNER_ADOPTION_PROBE_TIMEOUT_MS = 500 ;
2333const RUNNER_ADOPTION_EXIT_POLL_INTERVAL_MS = 1_000 ;
2434
2535// Kill switch for the runner handoff across daemon restarts: disables both
2636// detaching healthy simulator runners on graceful shutdown and adopting them
2737// on the next startup.
2838export function isIosRunnerDetachEnabled ( env : NodeJS . ProcessEnv = process . env ) : boolean {
29- const value = env . AGENT_DEVICE_IOS_RUNNER_DETACH ?. trim ( ) . toLowerCase ( ) ;
30- return value !== '0' && value !== 'false' && value !== 'off' ;
39+ return parseBooleanLiteral ( env . AGENT_DEVICE_IOS_RUNNER_DETACH ?? '' ) !== false ;
3140}
3241
3342// Adopts a still-running runner left behind by a dead daemon (crash or
@@ -47,24 +56,27 @@ export async function tryAdoptRunnerSessionFromLease(
4756 if ( resolveIosSimulatorDeviceSetPath ( device . simulatorSetPath ) ) return null ;
4857 const lease = readStaleRunnerLease ( device . id ) ;
4958 if ( ! lease ) return null ;
50- const runnerPid = lease . runnerPid ;
51- const rejection = runnerPid
52- ? await probeAdoptableRunnerLease ( device , lease , runnerPid )
53- : 'runner_pid_missing' ;
54- if ( rejection || ! runnerPid ) {
59+
60+ const skip = ( reason : string ) : null => {
5561 emitDiagnostic ( {
5662 level : 'debug' ,
5763 phase : 'ios_runner_lease_adoption_skipped' ,
58- data : {
59- deviceId : device . id ,
60- runnerPid : lease . runnerPid ,
61- port : lease . port ,
62- reason : rejection ,
63- } ,
64+ data : { deviceId : device . id , runnerPid : lease . runnerPid , port : lease . port , reason } ,
6465 } ) ;
6566 return null ;
67+ } ;
68+
69+ const runnerPid = lease . runnerPid ;
70+ if ( ! runnerPid ) return skip ( 'runner_pid_missing' ) ;
71+ if ( ! isProcessAlive ( runnerPid ) ) return skip ( 'runner_process_dead' ) ;
72+ const expectedDerived = resolveExpectedDerivedPath ( device ) ;
73+ if ( ! expectedDerived ) return skip ( 'expected_derived_unresolved' ) ;
74+ if ( ! lease . xctestrunPath . startsWith ( `${ expectedDerived } ${ path . sep } ` ) ) {
75+ return skip ( 'artifact_fingerprint_mismatch' ) ;
6676 }
67- const session = buildAdoptedRunnerSession ( device , lease , runnerPid , options ) ;
77+ if ( ! ( await probeRunnerAnswersUptime ( device , lease . port ) ) ) return skip ( 'probe_failed' ) ;
78+
79+ const session = buildAdoptedRunnerSession ( device , lease , runnerPid , expectedDerived , options ) ;
6880 try {
6981 writeRunnerLease ( session . lease ) ;
7082 } catch {
@@ -76,38 +88,27 @@ export async function tryAdoptRunnerSessionFromLease(
7688 data : {
7789 deviceId : device . id ,
7890 sessionId : session . sessionId ,
79- runnerPid : lease . runnerPid ,
91+ runnerPid,
8092 port : lease . port ,
8193 previousOwnerPid : lease . ownerPid ,
8294 } ,
8395 } ) ;
8496 return session ;
8597}
8698
87- async function probeAdoptableRunnerLease (
88- device : DeviceInfo ,
89- lease : RunnerLease ,
90- runnerPid : number ,
91- ) : Promise < string | null > {
92- if ( ! isProcessAlive ( runnerPid ) ) return 'runner_process_dead' ;
93- const expectedDerived = resolveExpectedDerivedPath ( device ) ;
94- if ( ! expectedDerived ) return 'expected_derived_unresolved' ;
95- if ( ! lease . xctestrunPath . startsWith ( `${ expectedDerived } ${ path . sep } ` ) ) {
96- return 'artifact_fingerprint_mismatch' ;
97- }
99+ async function probeRunnerAnswersUptime ( device : DeviceInfo , port : number ) : Promise < boolean > {
98100 try {
99101 const response = await sendRunnerCommandOnce (
100102 device ,
101- lease . port ,
103+ port ,
102104 withRunnerCommandId ( { command : 'uptime' } ) ,
103105 RUNNER_ADOPTION_PROBE_TIMEOUT_MS ,
104106 ) ;
105107 const payload = JSON . parse ( await response . text ( ) ) as { ok ?: unknown } ;
106- if ( payload ?. ok !== true ) return 'probe_rejected' ;
108+ return payload ?. ok === true ;
107109 } catch {
108- return 'probe_failed' ;
110+ return false ;
109111 }
110- return null ;
111112}
112113
113114function resolveExpectedDerivedPath ( device : DeviceInfo ) : string | null {
@@ -122,12 +123,13 @@ function buildAdoptedRunnerSession(
122123 device : DeviceInfo ,
123124 lease : RunnerLease ,
124125 runnerPid : number ,
126+ expectedDerived : string ,
125127 options : { startupTimeoutMs ?: number } ,
126128) : RunnerSession & { lease : RunnerLease } {
127- const sessionId = ` ${ device . id } : ${ lease . port } : ${ Date . now ( ) } ` ;
129+ const sessionId = buildRunnerSessionId ( device . id , lease . port ) ;
128130 const artifact : RunnerXctestrunArtifact = {
129131 xctestrunPath : lease . xctestrunPath ,
130- derived : resolveExpectedDerivedPath ( device ) ?? path . dirname ( lease . xctestrunPath ) ,
132+ derived : expectedDerived ,
131133 cache : 'exact' ,
132134 artifact : 'valid' ,
133135 buildMs : 0 ,
@@ -147,7 +149,7 @@ function buildAdoptedRunnerSession(
147149 child,
148150 // The probe already proved the runner answers commands.
149151 ready : true ,
150- startupTimeoutMs : normalizeStartupTimeoutMs ( options . startupTimeoutMs ) ,
152+ startupTimeoutMs : normalizeRunnerStartupTimeoutMs ( options . startupTimeoutMs ) ,
151153 lease : buildRunnerLease ( {
152154 deviceId : device . id ,
153155 sessionId,
@@ -160,29 +162,23 @@ function buildAdoptedRunnerSession(
160162}
161163
162164// The adopted xcodebuild was spawned by a dead process, so there is no
163- // ChildProcess handle. Downstream code only reads pid/exitCode and kills by
164- // pid, so a pid-backed surrogate suffices; a low-frequency poll flips
165- // exitCode and settles testPromise when the process actually exits, which is
166- // what the transport's early-exit detection and disposal wait on.
165+ // ChildProcess to hold — just a pid-backed RunnerProcessHandle. A
166+ // low-frequency poll flips exitCode and settles testPromise when the process
167+ // actually exits, which is what the transport's early-exit detection and
168+ // disposal wait on.
167169function watchDetachedRunnerProcess ( pid : number ) : {
168- child : ExecBackgroundResult [ 'child' ] ;
170+ child : RunnerProcessHandle ;
169171 wait : Promise < ExecResult > ;
170172} {
171- const child = { pid, exitCode : null } as unknown as ExecBackgroundResult [ 'child' ] ;
173+ const child : RunnerProcessHandle = { pid, exitCode : null } ;
172174 const wait = new Promise < ExecResult > ( ( resolve ) => {
173175 const timer = setInterval ( ( ) => {
174176 if ( isProcessAlive ( pid ) ) return ;
175177 clearInterval ( timer ) ;
176- ( child as { exitCode : number | null } ) . exitCode = - 1 ;
178+ child . exitCode = - 1 ;
177179 resolve ( { stdout : '' , stderr : '' , exitCode : - 1 } ) ;
178180 } , RUNNER_ADOPTION_EXIT_POLL_INTERVAL_MS ) ;
179181 timer . unref ?.( ) ;
180182 } ) ;
181183 return { child, wait } ;
182184}
183-
184- function normalizeStartupTimeoutMs ( value : number | undefined ) : number | undefined {
185- return typeof value === 'number' && Number . isFinite ( value ) && value > 0
186- ? Math . floor ( value )
187- : undefined ;
188- }
0 commit comments