Skip to content

Commit 33d48a1

Browse files
authored
refactor(ios-runner): give runner sessions a lifecycle state (#2673)
The session ready boolean mixed the runner answer with process existence, and three readers combined them three ways. A session now records whether it has answered and whether disposal has begun, while the process probe remains a separate fact. Both feed one liveness answer and one idempotence guard. (#2662)
1 parent 3dfd413 commit 33d48a1

34 files changed

Lines changed: 1430 additions & 583 deletions

‎packages/platform-apple/src/__tests__/session-observation.test.ts‎

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const { snapshot, runningApp } = vi.hoisted(() => ({
66
snapshot: vi.fn(),
77
runningApp: vi.fn(),
88
}));
9-
vi.mock('../core/runner-client.ts', () => ({ getRunnerSessionSnapshot: snapshot }));
9+
vi.mock('../core/runner-client.ts', () => ({ readRunnerSessionLiveness: snapshot }));
1010
vi.mock('../core/app-resolution.ts', () => ({ detectSoleRunningIosSimulatorApp: runningApp }));
1111

1212
const host = {
@@ -19,22 +19,19 @@ beforeEach(() => {
1919
vi.resetAllMocks();
2020
});
2121

22-
test.each([true, false])(
23-
'runner observation preserves liveness %s and session identity',
24-
async (alive) => {
25-
snapshot.mockResolvedValue({ sessionId: 'runner-1', alive, ready: false });
22+
test('runner observation reports a registered runner, whatever lifecycle state it is in', async () => {
23+
snapshot.mockReturnValue({ sessionId: 'runner-1', liveness: 'starting' });
2624

27-
await expect(observation.observeRunnerSession('sim-1')).resolves.toEqual({
28-
alive,
29-
sessionId: 'runner-1',
30-
});
31-
expect(snapshot).toHaveBeenCalledWith('sim-1');
32-
expect(host.listLocalDevices).not.toHaveBeenCalled();
33-
},
34-
);
25+
await expect(observation.observeRunnerSession('sim-1')).resolves.toEqual({
26+
alive: true,
27+
sessionId: 'runner-1',
28+
});
29+
expect(snapshot).toHaveBeenCalledWith('sim-1');
30+
expect(host.listLocalDevices).not.toHaveBeenCalled();
31+
});
3532

3633
test('a missing runner session has no observation', async () => {
37-
snapshot.mockResolvedValue(null);
34+
snapshot.mockReturnValue(null);
3835
await expect(observation.observeRunnerSession('sim-1')).resolves.toBeUndefined();
3936
});
4037

‎packages/platform-apple/src/core/runner-client.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ export const hasCachedAppleRunnerArtifact: AppleRunnerClient['hasCachedAppleRunn
3535
client.hasCachedAppleRunnerArtifact;
3636
export const detachIosSimulatorRunnerSessionsForShutdown: AppleRunnerClient['detachIosSimulatorRunnerSessionsForShutdown'] =
3737
client.detachIosSimulatorRunnerSessionsForShutdown;
38-
export const getRunnerSessionSnapshot: AppleRunnerClient['getRunnerSessionSnapshot'] =
39-
client.getRunnerSessionSnapshot;
38+
export const readRunnerSessionLiveness: AppleRunnerClient['readRunnerSessionLiveness'] =
39+
client.readRunnerSessionLiveness;
4040
export const releaseIosRunnerOnClose: AppleRunnerClient['releaseIosRunnerOnClose'] =
4141
client.releaseIosRunnerOnClose;
4242
export const stopIosRunnerSession: AppleRunnerClient['stopIosRunnerSession'] =

‎packages/platform-apple/src/runner-operations-facade.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
export {
22
applyXctestRunnerAppIconFromDerivedPath,
33
detachIosSimulatorRunnerSessionsForShutdown,
4-
getRunnerSessionSnapshot,
54
hasLiveIosRunnerSession,
65
notifyIosRunnerAppRelaunched,
76
prepareIosRunner,
87
prewarmAppleRunnerCache,
98
prewarmIosRunnerSession,
9+
readRunnerSessionLiveness,
1010
readStaleRunnerLease,
1111
releaseIosRunnerOnClose,
1212
releaseSpeculativeIosRunnerSessionFor,

‎packages/platform-apple/src/runner/__tests__/runner-adoption.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ test('adoption succeeds for a live, matching, probe-healthy runner', async () =>
140140

141141
expect(session).not.toBeNull();
142142
expect(session?.port).toBe(lease.port);
143-
expect(session?.ready).toBe(true);
143+
expect(session?.state).toBe('ready');
144144
expect(session?.child.pid).toBe(424242);
145145
expect(session?.sessionId).toBe(lease.sessionId);
146146
expect(session?.xctestrunArtifact?.reason).toBe('adopted_from_lease');
@@ -252,7 +252,7 @@ test('adoption accepts a legacy lease whose live pid is runner-shaped', async ()
252252

253253
const session = await tryAdoptRunnerSessionFromLease(simulator, {});
254254

255-
expect(session?.ready).toBe(true);
255+
expect(session?.state).toBe('ready');
256256
expect(session?.child.pid).toBe(424242);
257257
});
258258

‎packages/platform-apple/src/runner/__tests__/runner-client.test.ts‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ test('parseRunnerResponse preserves runner unsupported-operation codes', async (
497497
},
498498
}),
499499
);
500-
const session = { ready: false };
500+
const session = { state: 'starting' } as const;
501501

502502
await assert.rejects(
503503
() => parseRunnerResponse(response, session, '/tmp/runner.log'),
@@ -523,7 +523,7 @@ test('parseRunnerResponse surfaces the keyboard-dismiss hint naming the occlusio
523523
},
524524
}),
525525
);
526-
const session = { ready: false };
526+
const session = { state: 'starting' } as const;
527527

528528
await assert.rejects(
529529
() => parseRunnerResponse(response, session, '/tmp/runner.log'),
@@ -552,7 +552,7 @@ test('parseRunnerResponse preserves iOS AX snapshot failure code and hint', asyn
552552
},
553553
}),
554554
);
555-
const session = { ready: true };
555+
const session = { state: 'ready' } as const;
556556

557557
await assert.rejects(
558558
() => parseRunnerResponse(response, session, '/tmp/runner.log'),
@@ -581,7 +581,7 @@ test('parseRunnerResponse preserves XCTest recorded failure code and hint', asyn
581581
},
582582
}),
583583
);
584-
const session = { ready: true };
584+
const session = { state: 'ready' } as const;
585585

586586
await assert.rejects(
587587
() => parseRunnerResponse(response, session, '/tmp/runner.log'),
@@ -608,7 +608,7 @@ test('parseRunnerResponse maps RUNNER_BUSY to retriable command failure', async
608608
},
609609
}),
610610
);
611-
const session = { ready: true };
611+
const session = { state: 'ready' } as const;
612612

613613
await assert.rejects(
614614
() => parseRunnerResponse(response, session, '/tmp/runner.log'),
@@ -635,7 +635,7 @@ test('parseRunnerResponse preserves RUNNER_WEDGED as a fatal runner code', async
635635
},
636636
}),
637637
);
638-
const session = { ready: true };
638+
const session = { state: 'ready' } as const;
639639

640640
await assert.rejects(
641641
() => parseRunnerResponse(response, session, '/tmp/runner.log'),
@@ -667,7 +667,7 @@ Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
667667
},
668668
}),
669669
);
670-
const session = { ready: true };
670+
const session = { state: 'ready' } as const;
671671

672672
await assert.rejects(
673673
() => parseRunnerResponse(response, session, logPath),
@@ -697,7 +697,7 @@ The application under test terminated unexpectedly.
697697
},
698698
}),
699699
);
700-
const session = { ready: true };
700+
const session = { state: 'ready' } as const;
701701

702702
await assert.rejects(
703703
() => parseRunnerResponse(response, session, logPath),
@@ -726,7 +726,7 @@ AGENT_DEVICE_RUNNER_COMMAND_FAILED command=snapshot error=fetch failed
726726
},
727727
}),
728728
);
729-
const session = { ready: true };
729+
const session = { state: 'ready' } as const;
730730

731731
await assert.rejects(
732732
() => parseRunnerResponse(response, session, logPath),
@@ -754,7 +754,7 @@ test('parseRunnerResponse hints when XCTest main-thread execution times out', as
754754
},
755755
}),
756756
);
757-
const session = { ready: true };
757+
const session = { state: 'ready' } as const;
758758

759759
await assert.rejects(
760760
() => parseRunnerResponse(response, session, logPath),
@@ -782,14 +782,14 @@ test('parseRunnerResponse emits diagnostics for runner gesture fallbacks', async
782782
},
783783
}),
784784
);
785-
const session = { ready: false };
785+
const session = { state: 'starting' } as const;
786786
const diagnosticEvents: DiagnosticEventInput[] = [];
787787
appleRunnerTestHost.update({ emitDiagnostic: (event) => diagnosticEvents.push(event) });
788788

789789
const data = await parseRunnerResponse(response, session, '/tmp/runner.log');
790790
assert.equal(data.gestureFallback, 'xctest-coordinate-drag');
791791

792-
assert.equal(session.ready, true);
792+
assert.equal(session.state, 'ready');
793793
const diagnostics = JSON.stringify(diagnosticEvents);
794794
assert.match(diagnostics, /ios_runner_gesture_fallback/);
795795
assert.match(diagnostics, /xctest-coordinate-drag/);

‎packages/platform-apple/src/runner/__tests__/runner-close-finalization.test.ts‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import {
2424
abortAllIosRunnerSessions,
2525
ensureRunnerSession,
2626
executeRunnerCommandWithSession,
27-
getRunnerSessionSnapshot,
27+
readRunnerSessionLiveness,
2828
releaseIosRunnerOnClose,
2929
stopIosRunnerSession,
3030
type RunnerSession,
@@ -195,7 +195,7 @@ test('a retained close over an idle runner keeps that runner warm for the next o
195195
assert.deepEqual(events, ['release', 'released', 'alerts']);
196196
assert.match(diagnostics, /"phase":"ios_runner_idle_stop_scheduled"/);
197197
assert.doesNotMatch(diagnostics, /ios_runner_retain_skipped_busy/);
198-
assert.equal(getRunnerSessionSnapshot(device.id)?.sessionId, session.sessionId);
198+
assert.equal(readRunnerSessionLiveness(device.id)?.sessionId, session.sessionId);
199199
assert.ok(runnerLeaseExists(device.id));
200200
// The next open gets the same runner and launches nothing.
201201
assert.equal((await ensureRunnerSession(device, {})).sessionId, session.sessionId);
@@ -216,7 +216,7 @@ test('a retained close disposes a runner still draining so the next open boots a
216216
assert.deepEqual(events, ['release', 'released', 'alerts']);
217217
assert.match(diagnostics, /"phase":"ios_runner_retain_skipped_busy"/);
218218
assert.doesNotMatch(diagnostics, /"phase":"ios_runner_idle_stop_scheduled"/);
219-
assert.equal(getRunnerSessionSnapshot(device.id), null);
219+
assert.equal(readRunnerSessionLiveness(device.id), null);
220220
assert.equal(runnerLeaseExists(device.id), false);
221221
assert.notEqual((await ensureRunnerSession(device, {})).sessionId, session.sessionId);
222222
assert.equal(mockRunCmdBackground.mock.calls.length, 2);
@@ -238,7 +238,7 @@ test('a non-retained close stops the runner and cancels the idle stop a retained
238238

239239
assert.match(diagnostics, /"phase":"ios_runner_idle_stop_scheduled"/);
240240
assert.doesNotMatch(diagnostics, /"phase":"ios_runner_idle_stop"/);
241-
assert.equal(getRunnerSessionSnapshot(device.id), null);
241+
assert.equal(readRunnerSessionLiveness(device.id), null);
242242
assert.equal(runnerLeaseExists(device.id), false);
243243
});
244244

@@ -267,7 +267,7 @@ test('a retained close with no runner in memory starts none and claims no lease
267267
await lifecycle.finalizeApplicationClose(closeInput(true));
268268

269269
assert.deepEqual(events, ['release', 'released', 'alerts']);
270-
assert.equal(getRunnerSessionSnapshot(device.id), null);
270+
assert.equal(readRunnerSessionLiveness(device.id), null);
271271
assert.equal(mockRunCmdBackground.mock.calls.length, 0);
272272
assert.equal(runnerLeaseExists(device.id), false);
273273
});
@@ -281,7 +281,7 @@ test('a daemon-shutdown close never issues the ordinary close release (#2615)',
281281
await lifecycle.finalizeApplicationClose({ ...closeInput(true), daemonShutdown: true });
282282

283283
assert.deepEqual(events, ['alerts']);
284-
assert.equal(getRunnerSessionSnapshot(device.id)?.sessionId, session.sessionId);
284+
assert.equal(readRunnerSessionLiveness(device.id)?.sessionId, session.sessionId);
285285
assert.ok(runnerLeaseExists(device.id));
286286
});
287287

@@ -331,7 +331,7 @@ test('a drain that lands after close is issued cannot return a busy runner to re
331331
assert.equal(session.runnerMainThreadBusy, false);
332332
assert.match(diagnostics, /"phase":"ios_runner_retain_skipped_busy"/);
333333
assert.doesNotMatch(diagnostics, /"phase":"ios_runner_idle_stop_scheduled"/);
334-
assert.equal(getRunnerSessionSnapshot(device.id), null);
334+
assert.equal(readRunnerSessionLiveness(device.id), null);
335335
});
336336

337337
/** The close path's Apple tools, composed exactly as the root's lazy tools compose them. */

‎packages/platform-apple/src/runner/__tests__/runner-command-recovery.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function makeRunnerSession(port: number): RunnerSession {
3434
jsonPath: '/tmp/fake.json',
3535
testPromise: new Promise<ExecResult>(() => {}),
3636
child: { pid: process.pid, exitCode: null },
37-
ready: true,
37+
state: 'ready',
3838
};
3939
}
4040

0 commit comments

Comments
 (0)