Skip to content

Commit 775163b

Browse files
authored
fix(record): end the recorder a loaded host could not confirm (#2565)
* fix(record): end the recorder a loaded host could not confirm `record stop` refused to signal a recorder whose identity probe it could not afford, and the recording left in `cleanup-pending` then blocked `record start` until the daemon restarted. - Read the identity facts that decide who may be signaled off the event loop, with a budget the caller names, instead of one synchronous `ps` per field. - Keep "the host would not answer" apart from "the host named a different process": the first is unknown evidence, the second is a refusal. - End the recorder this owner spawned through its own handle while the host is not answering, and report any marker it cannot account for. - Re-drive a termination or a finish the host refused instead of replaying the rejection to every later stop. Closes #2549 * test(daemon): answer the ownership facts seam in the recovery fixture
1 parent 04052fd commit 775163b

10 files changed

Lines changed: 489 additions & 66 deletions

‎packages/capture-kit/src/screen-recording-live-handle.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,25 @@ test('failed finish permits one forced cleanup', async () => {
8080
assert.equal(cleanup.mock.calls.length, 1);
8181
});
8282

83+
test('retried finish re-drives a failed finish and replays a finished one', async () => {
84+
let attempts = 0;
85+
const finish = vi.fn(async () => {
86+
attempts += 1;
87+
if (attempts === 1) throw new Error('host could not confirm the recorder');
88+
return completed();
89+
});
90+
const handle = createScreenRecordingLiveHandle(snapshot(), {
91+
finish,
92+
forceCleanup: async () => ({ status: 'cleaned' }) as const,
93+
});
94+
95+
await assert.rejects(async () => await handle.finish(), /could not confirm the recorder/);
96+
assert.deepEqual(await handle.finish(), completed());
97+
assert.deepEqual(await handle.finish(), completed());
98+
99+
assert.equal(finish.mock.calls.length, 2);
100+
});
101+
83102
function snapshot() {
84103
return {
85104
backend: 'fixture',

‎packages/capture-kit/src/screen-recording-live-handle.ts‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,21 @@ export function createScreenRecordingLiveHandle(
2424
): ScreenRecordingLiveHandle {
2525
let snapshot = freezeSnapshot(initial);
2626
let finish: Promise<FinishOutcome<ScreenRecordingCompletion>> | undefined;
27+
let finishRequested = false;
2728
let cleanup: Promise<CleanupOutcome> | undefined;
2829
let disposal: Promise<void> | undefined;
29-
const finishRecording = () => (finish ??= implementation.finish(snapshot));
30+
// Once a finish or a cleanup has been asked for, the recording no longer accepts telemetry.
31+
const terminal = (): boolean => finishRequested || cleanup !== undefined;
32+
const finishRecording = () => {
33+
// Only a finished recording stays memoized. A finish the host refused has to be re-driven
34+
// by the next `record stop`, which is how a recording recovers without a daemon restart.
35+
finishRequested = true;
36+
finish ??= implementation.finish(snapshot).catch((error: unknown) => {
37+
finish = undefined;
38+
throw error;
39+
});
40+
return finish;
41+
};
3042
const forceCleanup = () =>
3143
(cleanup ??= finish
3244
? finish.then(
@@ -40,25 +52,25 @@ export function createScreenRecordingLiveHandle(
4052
return Object.freeze({
4153
inspect: () => snapshot,
4254
appendGestureEvents: (events: readonly RecordingGestureEvent[]) => {
43-
if (events.length === 0 || finish || cleanup) return;
55+
if (events.length === 0 || terminal()) return;
4456
snapshot = freezeSnapshot({
4557
...snapshot,
4658
gestureEvents: [...snapshot.gestureEvents, ...events],
4759
});
4860
},
4961
setTouchReferenceFrame: (touchReferenceFrame: GestureReferenceFrame | undefined) => {
50-
if (finish || cleanup) return;
62+
if (terminal()) return;
5163
snapshot = freezeSnapshot({
5264
...snapshot,
5365
...(touchReferenceFrame ? { touchReferenceFrame } : {}),
5466
});
5567
},
5668
setRunnerSessionId: (runnerSessionId: string) => {
57-
if (finish || cleanup || runnerSessionId.trim().length === 0) return;
69+
if (terminal() || runnerSessionId.trim().length === 0) return;
5870
snapshot = freezeSnapshot({ ...snapshot, runnerSessionId });
5971
},
6072
invalidate: (invalidatedReason: string) => {
61-
if (finish || cleanup || snapshot.invalidatedReason) return;
73+
if (terminal() || snapshot.invalidatedReason) return;
6274
snapshot = freezeSnapshot({ ...snapshot, invalidatedReason });
6375
},
6476
finish: finishRecording,

‎packages/host-kit/src/internal/host-process-liveness.test.ts‎

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,36 @@
11
import assert from 'node:assert/strict';
22
import { beforeEach, test, vi } from 'vitest';
33

4-
const { mockRunCmdSync } = vi.hoisted(() => ({ mockRunCmdSync: vi.fn() }));
4+
const { mockRunCmdSync, mockRunCmd } = vi.hoisted(() => ({
5+
mockRunCmdSync: vi.fn(),
6+
mockRunCmd: vi.fn(),
7+
}));
58

69
vi.mock('./exec.ts', async () => {
710
const actual = await vi.importActual<typeof import('./exec.ts')>('./exec.ts');
8-
return { ...actual, runCmdSync: mockRunCmdSync };
11+
return { ...actual, runCmd: mockRunCmd, runCmdSync: mockRunCmdSync };
912
});
1013

11-
import { isProcessZombie, readHostProcessIdentityObservations } from './host-process.ts';
14+
import {
15+
isProcessZombie,
16+
readHostProcessIdentityObservations,
17+
readProcessIdentityFacts,
18+
} from './host-process.ts';
1219

1320
function psReturns(stdout: string, exitCode = 0): void {
1421
mockRunCmdSync.mockReturnValue({ stdout, stderr: '', exitCode });
1522
}
1623

24+
function psAnswersEachField(fields: Record<string, string>): void {
25+
mockRunCmd.mockImplementation(async (_cmd: string, args: string[]) => {
26+
const value = fields[args[3]!];
27+
return { stdout: value ?? '', stderr: '', exitCode: value === undefined ? 1 : 0 };
28+
});
29+
}
30+
1731
beforeEach(() => {
1832
mockRunCmdSync.mockReset();
33+
mockRunCmd.mockReset();
1934
});
2035

2136
test('isProcessZombie detects the Z state code with trailing flags', () => {
@@ -62,3 +77,57 @@ test('reads many process identities from one ps snapshot', () => {
6277
'pid=,state=,lstart=',
6378
]);
6479
});
80+
81+
test('the ownership read spends the budget its caller names on every field', async () => {
82+
psAnswersEachField({
83+
'lstart=': 'Mon Aug 10 20:00:00 2026',
84+
'command=': '/bin/simctl io recordVideo out.mp4',
85+
'state=': 'Ss',
86+
});
87+
88+
const facts = await readProcessIdentityFacts(4242, 5_000);
89+
90+
assert.deepEqual(facts, {
91+
startTime: 'Mon Aug 10 20:00:00 2026',
92+
command: '/bin/simctl io recordVideo out.mp4',
93+
zombie: false,
94+
});
95+
assert.equal(mockRunCmd.mock.calls.length, 3);
96+
for (const call of mockRunCmd.mock.calls) {
97+
assert.equal(call[2]?.timeoutMs, 5_000);
98+
}
99+
});
100+
101+
test('the ownership read reports an unanswered host as unknown, not as absence', async () => {
102+
mockRunCmd.mockRejectedValue(new Error('/bin/ps timed out after 5000ms'));
103+
104+
assert.deepEqual(await readProcessIdentityFacts(4242, 5_000), {
105+
startTime: null,
106+
command: null,
107+
zombie: null,
108+
});
109+
});
110+
111+
test('the ownership read leaves the zombie question open when only the state is unreadable', async () => {
112+
psAnswersEachField({
113+
'lstart=': 'Mon Aug 10 20:00:00 2026',
114+
'command=': '/bin/simctl io recordVideo out.mp4',
115+
});
116+
117+
assert.deepEqual(await readProcessIdentityFacts(4242, 5_000), {
118+
startTime: 'Mon Aug 10 20:00:00 2026',
119+
command: '/bin/simctl io recordVideo out.mp4',
120+
zombie: null,
121+
});
122+
});
123+
124+
test('the ownership read answers the zombie question from the state field', async () => {
125+
psAnswersEachField({ 'lstart=': 'x', 'command=': 'y', 'state=': 'ZN' });
126+
127+
assert.deepEqual(await readProcessIdentityFacts(4242), {
128+
startTime: 'x',
129+
command: 'y',
130+
zombie: true,
131+
});
132+
assert.equal(mockRunCmd.mock.calls[0]?.[2]?.timeoutMs, 1_000);
133+
});

‎packages/host-kit/src/internal/host-process.ts‎

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,19 @@ export type HostProcessIdentityObservation = Readonly<{
1616
startTime: string;
1717
}>;
1818

19+
/**
20+
* What the host says about one pid when a caller is about to signal it. Each field is `null`
21+
* when the host did not answer, which is unknown evidence rather than absence: only a `ps`
22+
* that names a different process proves the pid changed hands. `zombie` is `null` together
23+
* with an unreadable process state, since a zombie passes `kill(pid, 0)` and still reports
24+
* its original start time, so nothing else exposes that it already terminated.
25+
*/
26+
export type HostProcessIdentityFacts = Readonly<{
27+
startTime: string | null;
28+
command: string | null;
29+
zombie: boolean | null;
30+
}>;
31+
1932
type HostProcessRunCommand = (
2033
cmd: string,
2134
args: string[],
@@ -85,6 +98,24 @@ export function isProcessGroupAlive(pid: number): boolean {
8598
}
8699
}
87100

101+
/**
102+
* Reads who owns a pid, off the event loop, with a budget the caller names. The three fields
103+
* are read concurrently so a loaded host pays one budget for the set rather than one per
104+
* field. Use this rather than the synchronous probes when the answer decides whether a
105+
* process is signaled: waiting is reversible and signaling the wrong process is not.
106+
*/
107+
export async function readProcessIdentityFacts(
108+
pid: number,
109+
timeoutMs = PS_TIMEOUT_MS,
110+
): Promise<HostProcessIdentityFacts> {
111+
const [startTime, command, state] = await Promise.all([
112+
readProcessFieldAsync(pid, 'lstart=', timeoutMs),
113+
readProcessFieldAsync(pid, 'command=', timeoutMs),
114+
readProcessFieldAsync(pid, 'state=', timeoutMs),
115+
]);
116+
return { startTime, command, zombie: state === null ? null : state.startsWith('Z') };
117+
}
118+
88119
export function readProcessStartTime(pid: number): string | null {
89120
return readProcessField(pid, 'lstart=');
90121
}
@@ -125,21 +156,44 @@ export function readHostProcessIdentityObservations(
125156
return observations;
126157
}
127158

128-
function readProcessField(pid: number, field: 'lstart=' | 'command=' | 'state='): string | null {
159+
type HostProcessField = 'lstart=' | 'command=' | 'state=';
160+
161+
function readProcessField(pid: number, field: HostProcessField): string | null {
129162
if (!Number.isInteger(pid) || pid <= 0) return null;
130163
try {
131164
const result = runCmdSync(HOST_PS_COMMAND, ['-p', String(pid), '-o', field], {
132165
allowFailure: true,
133166
timeoutMs: PS_TIMEOUT_MS,
134167
});
135-
if (result.exitCode !== 0) return null;
136-
const value = result.stdout.trim();
137-
return value.length > 0 ? value : null;
168+
return processFieldValue(result);
169+
} catch {
170+
return null;
171+
}
172+
}
173+
174+
async function readProcessFieldAsync(
175+
pid: number,
176+
field: HostProcessField,
177+
timeoutMs: number,
178+
): Promise<string | null> {
179+
if (!Number.isInteger(pid) || pid <= 0) return null;
180+
try {
181+
const result = await runCmd(HOST_PS_COMMAND, ['-p', String(pid), '-o', field], {
182+
allowFailure: true,
183+
timeoutMs,
184+
});
185+
return processFieldValue(result);
138186
} catch {
139187
return null;
140188
}
141189
}
142190

191+
function processFieldValue(result: ExecResult): string | null {
192+
if (result.exitCode !== 0) return null;
193+
const value = result.stdout.trim();
194+
return value.length > 0 ? value : null;
195+
}
196+
143197
export function parseHostProcessList(stdout: string): HostProcessInfo[] {
144198
const processes: HostProcessInfo[] = [];
145199
for (const line of stdout.split('\n')) {

‎packages/host-kit/src/process.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export {
66
hostNodeVersion,
77
hostPlatform,
88
hostProcessId,
9+
type HostProcessIdentityFacts,
910
type HostProcessIdentityObservation,
1011
type HostProcessInfo,
1112
isProcessAlive,
@@ -15,6 +16,7 @@ export {
1516
readHostEnvironmentVariable,
1617
readHostProcessIdentityObservations,
1718
readProcessCommand,
19+
readProcessIdentityFacts,
1820
readProcessStartTime,
1921
signalPidsBestEffort,
2022
signalProcessGroupBestEffort,

‎src/daemon/__tests__/request-router-open.test.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,18 @@ import { replayScriptSourceBundleFor } from '../../__tests__/test-utils/replay-s
1111
vi.mock('../device-ready.ts', () => ({ ensureDeviceReady: vi.fn(async () => {}) }));
1212
vi.mock('@agent-device/host-kit/process', async (importOriginal) => {
1313
const actual = await importOriginal<typeof import('@agent-device/host-kit/process')>();
14-
return { ...actual, readProcessStartTime: vi.fn(() => 'test-process-start') };
14+
const startTime = 'test-process-start';
15+
return {
16+
...actual,
17+
readProcessStartTime: vi.fn(() => startTime),
18+
// The ownership decision reads all three facts in one call, so it has to answer with the
19+
// same fabricated start time this fixture writes into its markers.
20+
readProcessIdentityFacts: vi.fn(async (pid: number) => ({
21+
startTime,
22+
command: actual.readProcessCommand(pid),
23+
zombie: false,
24+
})),
25+
};
1526
});
1627
vi.mock('@agent-device/platform-apple/runner/operations', async (importOriginal) => {
1728
const actual =

‎src/platform-runtime-screen-recording-apple-simulator-host.test.ts‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ vi.mock('@agent-device/host-kit/process', async (importOriginal) => ({
1919
isProcessZombie: () => false,
2020
readProcessStartTime: (pid: number) => processes.starts.get(pid) ?? null,
2121
readProcessCommand: (pid: number) => processes.commands.get(pid) ?? null,
22+
readProcessIdentityFacts: async (pid: number) => ({
23+
startTime: processes.starts.get(pid) ?? null,
24+
command: processes.commands.get(pid) ?? null,
25+
zombie: false,
26+
}),
2227
listHostProcesses: async () =>
2328
[...processes.alive.keys()].map((pid) => ({
2429
pid,
@@ -355,6 +360,46 @@ test.each([
355360
running.resolveWait({ stdout: '', stderr: '', exitCode: 0 });
356361
});
357362

363+
test('ends the recorder through its handle when the host cannot confirm its identity', async () => {
364+
const root = mkdtempForTestSync('agent-device-recording-unreadable-identity-');
365+
const outputPath = path.join(root, 'capture.mp4');
366+
fs.writeFileSync(outputPath, 'recording');
367+
const running = background(51, `xcrun simctl io ${simulator.id} recordVideo ${outputPath}`);
368+
const process = await withTransport(
369+
running.process,
370+
async () => await startAppleSimulatorRecording(simulator, outputPath),
371+
);
372+
373+
// A loaded host can fail the identity probe it would normally confirm ownership with.
374+
// That proves nothing about who owns the pid, and must not strand the recording.
375+
processes.starts.delete(51);
376+
await expect(process.terminate()).resolves.toBeUndefined();
377+
expect(running.kill).toHaveBeenCalledWith('SIGINT');
378+
await expect(process.wait).resolves.toMatchObject({ exitCode: 0 });
379+
});
380+
381+
test('a recorder whose stop was refused can be stopped by the next stop', async () => {
382+
const root = mkdtempForTestSync('agent-device-recording-refused-stop-');
383+
const outputPath = path.join(root, 'capture.mp4');
384+
fs.writeFileSync(outputPath, 'recording');
385+
const running = background(50, `xcrun simctl io ${simulator.id} recordVideo ${outputPath}`);
386+
const process = await withTransport(
387+
running.process,
388+
async () => await startAppleSimulatorRecording(simulator, outputPath),
389+
);
390+
391+
// A refused attempt stays refused for that attempt only. Whatever made this one refuse,
392+
// the next `record stop` must be able to end the same recorder rather than replay the
393+
// rejection for the rest of the session.
394+
processes.starts.set(50, 'start-of-a-different-process');
395+
await expect(process.terminate()).rejects.toThrow('process ownership changed');
396+
expect(running.kill).not.toHaveBeenCalled();
397+
398+
processes.starts.set(50, 'start-50');
399+
await expect(process.terminate()).resolves.toBeUndefined();
400+
expect(running.kill).toHaveBeenCalledWith('SIGINT');
401+
});
402+
358403
test('pidless provider process is killed and settled before start fails', async () => {
359404
const rejection = observeUnhandledRejections();
360405
const running = background(undefined);

0 commit comments

Comments
 (0)