Skip to content

Commit aa53389

Browse files
thymikeeclaude
andcommitted
fix(ios): hold a canceled request's locks until its runner command ends
A request canceled through its signal released the session and device locks as soon as the runner fetch aborted, while the XCTest runner kept executing the command. A next request whose device work bypasses the runner (simctl launch, privacy, screenshots) could overtake it. The exchange now drains a canceled command it already sent: it polls the runner journal with `status`/`statusCommandId` until the entry is terminal, bounded by the command's own remaining deadline rather than the aborted request signal. Terminal evidence settles the abandoned charge through the shared journal settlement; a drain that runs out of deadline leaves it abandoned. A request canceled before the send releases at once without charging the runner. A caller's own deadline abort is unchanged. Closes #3383 Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 19203c6 commit aa53389

6 files changed

Lines changed: 363 additions & 40 deletions

File tree

‎packages/platform-apple/src/runner/__tests__/fake-runner-server.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ export type FakeRunnerResponse =
1717
/** Hangs up on this request and every later one for the command: the entry is never consumed. */
1818
| { kind: 'hangUpAlways' }
1919
/** Hangs up and stops listening, as a runner process that died mid-command. */
20-
| { kind: 'exit' };
20+
| { kind: 'exit' }
21+
/** Never answers, as a runner still executing the command when its caller gives up. */
22+
| { kind: 'hold' };
2123

2224
export type FakeRunnerRequest = {
2325
command: string;
@@ -79,9 +81,10 @@ export async function startFakeRunnerServer(
7981
requests,
8082
close: () =>
8183
stopped ??
82-
new Promise<void>((resolve, reject) =>
83-
server.close((error) => (error ? reject(error) : resolve())),
84-
),
84+
new Promise<void>((resolve, reject) => {
85+
server.close((error) => (error ? reject(error) : resolve()));
86+
server.closeAllConnections();
87+
}),
8588
};
8689
}
8790

@@ -101,6 +104,7 @@ function writeFakeRunnerResponse(
101104
res.end(JSON.stringify({ ok: false, error: { message: 'fake runner script exhausted' } }));
102105
return;
103106
}
107+
if (next.kind === 'hold') return;
104108
if (next.kind === 'hangUp' || next.kind === 'hangUpAlways') {
105109
res.destroy();
106110
return;

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

Lines changed: 206 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
import assert from 'node:assert/strict';
22
import type { ExecResult } from '@agent-device/host-kit/command';
3+
import { createRequestCanceledError, isRequestCanceledError } from '@agent-device/kernel/errors';
4+
import { withKeyedLock } from '@agent-device/kernel/keyed-lock';
35
import { afterEach, test } from 'vitest';
46
import { executeRunnerExchange } from '../runner-exchange.ts';
57
import { RunnerCommandAccounting, type RunnerSession } from '../runner-session-types.ts';
68
import { IOS_SIMULATOR } from './device-fixtures.ts';
7-
import { startFakeRunnerServer, type FakeRunnerServer } from './fake-runner-server.ts';
9+
import {
10+
startFakeRunnerServer,
11+
type FakeRunnerResponse,
12+
type FakeRunnerServer,
13+
} from './fake-runner-server.ts';
814

915
let server: FakeRunnerServer | undefined;
1016

@@ -184,3 +190,202 @@ test('the exchange awaits owner invalidation before throwing a fatal runner erro
184190
}
185191
await assert.rejects(exchange);
186192
});
193+
194+
const LONG_PRESS = {
195+
command: 'longPress',
196+
x: 10,
197+
y: 10,
198+
durationMs: 5_000,
199+
appBundleId: 'com.example.app',
200+
} as const;
201+
202+
function journalState(lifecycleState: string): FakeRunnerResponse {
203+
return { kind: 'ok', data: { lifecycleState } };
204+
}
205+
206+
async function abortOnceReceived(
207+
runner: FakeRunnerServer,
208+
command: string,
209+
controller: AbortController,
210+
reason: unknown = createRequestCanceledError(),
211+
): Promise<void> {
212+
while (!runner.requests.some((request) => request.command === command)) {
213+
await new Promise<void>((resolve) => setTimeout(resolve, 1));
214+
}
215+
controller.abort(reason);
216+
}
217+
218+
function statusPolls(runner: FakeRunnerServer): unknown[] {
219+
return runner.requests
220+
.filter((request) => request.command === 'status')
221+
.map((request) => request.body.statusCommandId);
222+
}
223+
224+
function sentCommandId(runner: FakeRunnerServer, command: string): unknown {
225+
return runner.requests.find((request) => request.command === command)?.body.commandId;
226+
}
227+
228+
test('a canceled exchange whose command reached the runner settles only once its journal entry is terminal', async () => {
229+
server = await startFakeRunnerServer({
230+
longPress: [{ kind: 'hold' }],
231+
status: [journalState('started'), journalState('completed')],
232+
});
233+
const runner = server;
234+
const session = sessionFor(runner.port);
235+
const controller = new AbortController();
236+
let requestsWhenSettled = -1;
237+
238+
const exchange = executeRunnerExchange(
239+
IOS_SIMULATOR,
240+
session,
241+
LONG_PRESS,
242+
undefined,
243+
10_000,
244+
async () => {},
245+
controller.signal,
246+
).finally(() => {
247+
requestsWhenSettled = runner.requests.length;
248+
});
249+
await abortOnceReceived(runner, 'longPress', controller);
250+
251+
await assert.rejects(exchange, isRequestCanceledError);
252+
const commandId = sentCommandId(runner, 'longPress');
253+
assert.deepEqual(statusPolls(runner), [commandId, commandId]);
254+
assert.equal(requestsWhenSettled, runner.requests.length);
255+
assert.equal(session.commandCharges.hasOutstandingCharges, false);
256+
});
257+
258+
test('a request on the same session lock starts only after the canceled command is terminal', async () => {
259+
server = await startFakeRunnerServer({
260+
longPress: [{ kind: 'hold' }],
261+
status: [journalState('accepted'), journalState('started'), journalState('failed')],
262+
});
263+
const runner = server;
264+
const session = sessionFor(runner.port);
265+
const locks = new Map<string, Promise<unknown>>();
266+
const controller = new AbortController();
267+
268+
const canceled = withKeyedLock(locks, 'session:default', async () => {
269+
await executeRunnerExchange(
270+
IOS_SIMULATOR,
271+
session,
272+
LONG_PRESS,
273+
undefined,
274+
10_000,
275+
async () => {},
276+
controller.signal,
277+
);
278+
});
279+
await abortOnceReceived(runner, 'longPress', controller);
280+
const nextRequestSawPolls = withKeyedLock(locks, 'session:default', async () =>
281+
statusPolls(runner),
282+
);
283+
284+
await assert.rejects(canceled, isRequestCanceledError);
285+
assert.equal((await nextRequestSawPolls).length, 3);
286+
assert.equal(session.commandCharges.hasOutstandingCharges, false);
287+
});
288+
289+
test('a request canceled before its command is sent releases without reading the journal', async () => {
290+
server = await startFakeRunnerServer({});
291+
const session = sessionFor(server.port);
292+
session.lastHealthyMutation = { atMs: Date.now(), appBundleId: LONG_PRESS.appBundleId };
293+
const controller = new AbortController();
294+
controller.abort(createRequestCanceledError());
295+
296+
await assert.rejects(
297+
executeRunnerExchange(
298+
IOS_SIMULATOR,
299+
session,
300+
LONG_PRESS,
301+
undefined,
302+
10_000,
303+
async () => {},
304+
controller.signal,
305+
),
306+
isRequestCanceledError,
307+
);
308+
309+
assert.deepEqual(server.requests, []);
310+
assert.equal(session.commandCharges.hasOutstandingCharges, false);
311+
});
312+
313+
test('a canceled command the journal never accepted releases after one read and stays abandoned', async () => {
314+
server = await startFakeRunnerServer({
315+
longPress: [{ kind: 'hold' }],
316+
status: [journalState('notAccepted')],
317+
});
318+
const session = sessionFor(server.port);
319+
const controller = new AbortController();
320+
321+
const exchange = executeRunnerExchange(
322+
IOS_SIMULATOR,
323+
session,
324+
LONG_PRESS,
325+
undefined,
326+
10_000,
327+
async () => {},
328+
controller.signal,
329+
);
330+
await abortOnceReceived(server, 'longPress', controller);
331+
332+
await assert.rejects(exchange, isRequestCanceledError);
333+
assert.equal(statusPolls(server).length, 1);
334+
assert.equal(session.commandCharges.hasAbandonedCharges, true);
335+
});
336+
337+
test('a drain that outlives the command deadline settles and leaves the charge abandoned', async () => {
338+
server = await startFakeRunnerServer({
339+
longPress: [{ kind: 'hold' }],
340+
status: Array.from({ length: 50 }, () => journalState('started')),
341+
});
342+
const session = sessionFor(server.port);
343+
const controller = new AbortController();
344+
const timeoutMs = 300;
345+
const startedAtMs = Date.now();
346+
347+
const exchange = executeRunnerExchange(
348+
IOS_SIMULATOR,
349+
session,
350+
LONG_PRESS,
351+
undefined,
352+
timeoutMs,
353+
async () => {},
354+
controller.signal,
355+
);
356+
await abortOnceReceived(server, 'longPress', controller);
357+
358+
await assert.rejects(exchange, isRequestCanceledError);
359+
assert.ok(statusPolls(server).length >= 1);
360+
assert.ok(Date.now() - startedAtMs < timeoutMs + 1_000);
361+
assert.equal(session.commandCharges.hasAbandonedCharges, true);
362+
});
363+
364+
test("a caller's own deadline abort does not wait on the runner journal", async () => {
365+
server = await startFakeRunnerServer({
366+
longPress: [{ kind: 'hold' }],
367+
status: [journalState('started'), journalState('completed')],
368+
});
369+
const session = sessionFor(server.port);
370+
const controller = new AbortController();
371+
372+
const exchange = executeRunnerExchange(
373+
IOS_SIMULATOR,
374+
session,
375+
LONG_PRESS,
376+
undefined,
377+
10_000,
378+
async () => {},
379+
controller.signal,
380+
);
381+
await abortOnceReceived(
382+
server,
383+
'longPress',
384+
controller,
385+
new DOMException('Wait deadline exceeded', 'TimeoutError'),
386+
);
387+
388+
await assert.rejects(exchange);
389+
assert.deepEqual(statusPolls(server), []);
390+
assert.equal(session.commandCharges.hasAbandonedCharges, true);
391+
});
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import type { RunnerCommand } from './runner-contract.ts';
2+
import type { RunnerCommandAccounting } from './runner-session-types.ts';
3+
4+
/**
5+
* The runner journal vocabulary, so charge settlement, status recovery, and the drain of a canceled
6+
* command cannot disagree about what a state means. `runner-swift-settlement-fixtures.ts` pins these
7+
* names to `RunnerCommandLifecycleState`, and the recovery wiring rows are derived from that same
8+
* declaration, so a state the runner gains has to be ruled here before it can decide a handoff.
9+
*
10+
* `completed` and `failed` close an entry — from the response's `ok` in `finish`, or a thrown error in
11+
* `fail` — so execution ended. `accepted` and `started` are written as execution opens, so they share
12+
* one in-flight verdict. `notAccepted` is what `status` reports for an id the journal never held,
13+
* which this daemon cannot read as terminal.
14+
*/
15+
const RUNNER_TERMINAL_LIFECYCLE_STATES: ReadonlySet<string> = new Set(['completed', 'failed']);
16+
export const RUNNER_IN_FLIGHT_LIFECYCLE_STATES: ReadonlySet<string> = new Set([
17+
'accepted',
18+
'started',
19+
]);
20+
21+
/**
22+
* Discharges the abandoned charge terminal status proves landed (#2965). A status reply is served
23+
* inline, so it is no evidence that queued work finished; this is the only place a status answer may
24+
* settle a charge, and only the one its `statusCommandId` names.
25+
*
26+
* @returns whether the evidence paid a debt, or `undefined` when the state is not terminal and no
27+
* settlement was attempted.
28+
*/
29+
export function settleRunnerChargeForTerminalStatus(
30+
session: { commandCharges: RunnerCommandAccounting },
31+
command: Pick<RunnerCommand, 'commandId'>,
32+
lifecycleState: string,
33+
): boolean | undefined {
34+
if (!RUNNER_TERMINAL_LIFECYCLE_STATES.has(lifecycleState)) return undefined;
35+
return session.commandCharges.settleTerminalEvidence(command.commandId);
36+
}

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

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ import {
1212
} from './runner-contract.ts';
1313
import { isReadOnlyRunnerCommand } from './runner-command-traits.ts';
1414
import { RUNNER_REPLY_LOST_REASON } from './runner-error-classification.ts';
15+
import {
16+
RUNNER_IN_FLIGHT_LIFECYCLE_STATES,
17+
settleRunnerChargeForTerminalStatus,
18+
} from './runner-command-journal.ts';
1519
import type { AppleRunnerCommandOptions } from './runner-provider.ts';
1620
import { executeRunnerCommandWithSession, type RunnerSession } from './runner-session.ts';
1721

@@ -277,38 +281,6 @@ async function tryRecoverRunnerCommandAfterTransportError(
277281
);
278282
}
279283

280-
/**
281-
* The runner journal vocabulary, so the charge settlement below and the recovery verdict in
282-
* {@link handleRunnerCommandStatusRecovery} cannot disagree about what a state means.
283-
* `runner-swift-settlement-fixtures.ts` pins these names to `RunnerCommandLifecycleState`, and the
284-
* recovery wiring rows are derived from that same declaration, so a state the runner gains has to be
285-
* ruled here before it can decide a handoff.
286-
*
287-
* `completed` and `failed` close an entry — from the response's `ok` in `finish`, or a thrown error in
288-
* `fail` — so execution ended, and each gets its own recovery verdict below. `accepted` and `started`
289-
* are written as execution opens, so they share one in-flight verdict. `notAccepted` is what `status`
290-
* reports for an id the journal never held, which this daemon cannot read as terminal.
291-
*/
292-
const RUNNER_TERMINAL_LIFECYCLE_STATES: ReadonlySet<string> = new Set(['completed', 'failed']);
293-
const RUNNER_IN_FLIGHT_LIFECYCLE_STATES: ReadonlySet<string> = new Set(['accepted', 'started']);
294-
295-
/**
296-
* Discharges the abandoned charge terminal status proves landed (#2965). A status reply is served
297-
* inline, so it is no evidence that queued work finished; this is the only place a status answer may
298-
* settle a charge, and only the one its `statusCommandId` names.
299-
*
300-
* @returns whether the evidence paid a debt, or `undefined` when the state is not terminal and no
301-
* settlement was attempted.
302-
*/
303-
function settleRunnerChargeForTerminalStatus(
304-
session: RunnerSession,
305-
command: RunnerCommand,
306-
lifecycleState: string,
307-
): boolean | undefined {
308-
if (!RUNNER_TERMINAL_LIFECYCLE_STATES.has(lifecycleState)) return undefined;
309-
return session.commandCharges.settleTerminalEvidence(command.commandId);
310-
}
311-
312284
function handleRunnerCommandStatusRecovery(
313285
status: Record<string, unknown>,
314286
lifecycleState: string,

0 commit comments

Comments
 (0)