-
-
Notifications
You must be signed in to change notification settings - Fork 313
fix(ios-runner): resend read-only commands across the RUNNER_BUSY drain window #2804
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
thymikee
merged 3 commits into
callstack:main
from
okwasniewski:oskar/runner-busy-resend-window
Sep 24, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
0c35703
fix(ios-runner): resend read-only commands across the RUNNER_BUSY dra…
okwasniewski 86b7b99
fix(ios-runner): keep the RUNNER_BUSY refusal through a mid-window wa…
okwasniewski 61404c2
fix(ios-runner): read the caller deadline from the abort reason, not …
okwasniewski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
243 changes: 243 additions & 0 deletions
243
packages/platform-apple/src/runner/__tests__/runner-command-busy-resend.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,243 @@ | ||
| import { beforeEach, test, vi } from 'vitest'; | ||
| import assert from 'node:assert/strict'; | ||
| import { IOS_SIMULATOR } from './device-fixtures.ts'; | ||
| import { createTestRequestCancellation, makeRunnerSession } from './runner-session-fixtures.ts'; | ||
| import { AppError, isRequestCanceledError } from '@agent-device/kernel/errors'; | ||
| import { appleRunnerTestHost } from '../test-host.ts'; | ||
|
|
||
| // The read-only resend policy around a `RUNNER_BUSY` refusal. The runner refuses fast while | ||
| // watchdog-abandoned XCTest work drains, so the daemon's resend has to outlast that drain, keep | ||
| // the old three-attempt budget for transport failures, and stay cancellable across the seconds it | ||
| // may now wait. | ||
|
|
||
| const { | ||
| mockEnsureRunnerSession, | ||
| mockExecuteRunnerCommandWithSession, | ||
| mockReadRunnerSessionLiveness, | ||
| mockInvalidateRunnerSession, | ||
| } = vi.hoisted(() => ({ | ||
| mockEnsureRunnerSession: vi.fn(), | ||
| mockExecuteRunnerCommandWithSession: vi.fn(), | ||
| mockReadRunnerSessionLiveness: vi.fn(), | ||
| mockInvalidateRunnerSession: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock('../runner-session.ts', async () => { | ||
| const actual = | ||
| await vi.importActual<typeof import('../runner-session.ts')>('../runner-session.ts'); | ||
| return { | ||
| ...actual, | ||
| ensureRunnerSession: mockEnsureRunnerSession, | ||
| executeRunnerCommandWithSession: mockExecuteRunnerCommandWithSession, | ||
| readRunnerSessionLiveness: mockReadRunnerSessionLiveness, | ||
| invalidateRunnerSession: mockInvalidateRunnerSession, | ||
| }; | ||
| }); | ||
|
|
||
| import { runAppleRunnerCommand } from '../runner-client.ts'; | ||
| import { buildRunnerResponseError } from '../runner-contract.ts'; | ||
| import { resetRunnerRecycleLedgerForTests } from '../runner-recycle-ledger.ts'; | ||
|
|
||
| const requestCancellation = createTestRequestCancellation(); | ||
|
|
||
| beforeEach(() => { | ||
| vi.resetAllMocks(); | ||
| vi.useRealTimers(); | ||
| resetRunnerRecycleLedgerForTests(); | ||
| requestCancellation.reset(); | ||
| appleRunnerTestHost.update({ | ||
| emitDiagnostic: vi.fn(), | ||
| isRequestCanceled: requestCancellation.isRequestCanceled, | ||
| getRequestSignal: () => undefined, | ||
| }); | ||
| const session = makeRunnerSession({ state: 'ready' }); | ||
| mockEnsureRunnerSession.mockResolvedValue(session); | ||
| // A live session, or `executeRunnerCommand` reads the resend as a recycle boot and charges the | ||
| // per-request recycle budget instead of resending. | ||
| mockReadRunnerSessionLiveness.mockReturnValue({ | ||
| sessionId: session.sessionId, | ||
| liveness: 'ready', | ||
| }); | ||
| }); | ||
|
|
||
| /** The runner's live refusal, exactly as the transport decodes it. */ | ||
| function busyRefusal(): AppError { | ||
| return buildRunnerResponseError({ | ||
| ok: false, | ||
| error: { code: 'RUNNER_BUSY', message: 'The iOS runner is still finishing a previous command' }, | ||
| }); | ||
| } | ||
|
|
||
| function sentCommands(): string[] { | ||
| return mockExecuteRunnerCommandWithSession.mock.calls.map((call) => call[2].command); | ||
| } | ||
|
|
||
| test('read-only commands wait out RUNNER_BUSY past the transport resend backoff', async () => { | ||
| // Three busy answers spaced 200/400/800ms apart, then the runner answers. The default three | ||
| // attempts stopped after the second delay, before the abandoned work had drained. | ||
| vi.useFakeTimers(); | ||
| mockExecuteRunnerCommandWithSession | ||
| .mockRejectedValueOnce(busyRefusal()) | ||
| .mockRejectedValueOnce(busyRefusal()) | ||
| .mockRejectedValueOnce(busyRefusal()) | ||
| .mockResolvedValueOnce({ nodes: [], truncated: false }); | ||
|
|
||
| const pending = runAppleRunnerCommand(IOS_SIMULATOR, { command: 'snapshot' }); | ||
| await vi.advanceTimersByTimeAsync(6_000); | ||
| const result = await pending; | ||
|
|
||
| assert.deepEqual(result, { nodes: [], truncated: false }); | ||
| assert.equal(mockInvalidateRunnerSession.mock.calls.length, 0); | ||
| // A structured refusal already says the command did not run, so no status probe sits between | ||
| // the resends. | ||
| assert.deepEqual(sentCommands(), ['snapshot', 'snapshot', 'snapshot', 'snapshot']); | ||
| }); | ||
|
|
||
| test('a RUNNER_BUSY resend still ends when the refusals outlast the window', async () => { | ||
| vi.useFakeTimers(); | ||
| mockExecuteRunnerCommandWithSession.mockImplementation(async () => { | ||
| throw busyRefusal(); | ||
| }); | ||
|
|
||
| const pending = runAppleRunnerCommand(IOS_SIMULATOR, { command: 'snapshot' }); | ||
| const settled = pending.then( | ||
| () => undefined, | ||
| (error: unknown) => error, | ||
| ); | ||
| await vi.advanceTimersByTimeAsync(20_000); | ||
| const error = await settled; | ||
|
|
||
| assert.ok(error instanceof AppError); | ||
| assert.equal(error.details?.runnerErrorCode, 'RUNNER_BUSY'); | ||
| assert.equal(error.details?.recovery, undefined, 'the raw refusal reaches the caller unwrapped'); | ||
| assert.deepEqual(sentCommands(), Array<string>(8).fill('snapshot')); | ||
| }); | ||
|
|
||
| test('read-only transport failures keep the three-attempt resend budget', async () => { | ||
| mockExecuteRunnerCommandWithSession | ||
| .mockRejectedValueOnce(new AppError('COMMAND_FAILED', 'fetch failed')) | ||
| .mockResolvedValueOnce({ lifecycleState: 'started' }) | ||
| .mockRejectedValueOnce(new AppError('COMMAND_FAILED', 'fetch failed')) | ||
| .mockResolvedValueOnce({ lifecycleState: 'started' }) | ||
| .mockRejectedValueOnce(new AppError('COMMAND_FAILED', 'fetch failed')) | ||
| .mockResolvedValueOnce({ lifecycleState: 'started' }) | ||
| .mockResolvedValueOnce({ nodes: [], truncated: false }); | ||
|
|
||
| await assert.rejects( | ||
| () => runAppleRunnerCommand(IOS_SIMULATOR, { command: 'snapshot' }), | ||
| (error: unknown) => { | ||
| assert.ok(error instanceof AppError); | ||
| assert.equal(error.message, 'fetch failed'); | ||
| return true; | ||
| }, | ||
| ); | ||
|
|
||
| assert.equal(sentCommands().filter((command) => command === 'snapshot').length, 3); | ||
| }); | ||
|
|
||
| test('a wait deadline landing mid-window rethrows the last RUNNER_BUSY refusal, not a cancel', async () => { | ||
| // `wait` bounds each poll with its own abort signal (runWithinWaitDeadline) and keeps the last | ||
| // typed refusal as the wait's cause. The refusal must therefore survive the deadline: a bare | ||
| // cancellation would make the wait report a stalled capture and drop the runner's own code. | ||
| vi.useFakeTimers(); | ||
| const deadline = new AbortController(); | ||
| mockExecuteRunnerCommandWithSession | ||
| .mockRejectedValueOnce(busyRefusal()) | ||
| .mockResolvedValue({ nodes: [], truncated: false }); | ||
|
|
||
| const pending = runAppleRunnerCommand( | ||
| IOS_SIMULATOR, | ||
| { command: 'snapshot' }, | ||
| { requestId: 'req-wait', signal: deadline.signal }, | ||
| ); | ||
| const settled = pending.then( | ||
| () => undefined, | ||
| (error: unknown) => error, | ||
| ); | ||
| await vi.advanceTimersByTimeAsync(50); | ||
| deadline.abort(new DOMException('Wait deadline exceeded', 'TimeoutError')); | ||
| await vi.advanceTimersByTimeAsync(0); | ||
| const error = await settled; | ||
|
|
||
| assert.ok(error instanceof AppError, `expected the refusal, got ${String(error)}`); | ||
| assert.equal(error.details?.runnerErrorCode, 'RUNNER_BUSY'); | ||
| assert.equal(error.details?.retriable, true); | ||
| assert.deepEqual(sentCommands(), ['snapshot']); | ||
| }); | ||
|
|
||
| test('a wait deadline landing mid-fetch during a resend still rethrows the RUNNER_BUSY refusal', async () => { | ||
| vi.useFakeTimers(); | ||
| const deadline = new AbortController(); | ||
| mockExecuteRunnerCommandWithSession.mockRejectedValueOnce(busyRefusal()).mockImplementationOnce( | ||
| (_device, _session, _command, _logPath, _timeoutMs, signal: AbortSignal | undefined) => | ||
| new Promise((_resolve, reject) => { | ||
| signal?.addEventListener('abort', () => reject(signal.reason), { once: true }); | ||
| }), | ||
| ); | ||
|
|
||
| const pending = runAppleRunnerCommand( | ||
| IOS_SIMULATOR, | ||
| { command: 'snapshot' }, | ||
| { requestId: 'req-wait-mid-fetch', signal: deadline.signal }, | ||
| ); | ||
| const settled = pending.then( | ||
| () => undefined, | ||
| (error: unknown) => error, | ||
| ); | ||
| // Past the first 200ms delay: the second send is in flight when the deadline lands. | ||
| await vi.advanceTimersByTimeAsync(250); | ||
| deadline.abort(new DOMException('Wait deadline exceeded', 'TimeoutError')); | ||
| await vi.advanceTimersByTimeAsync(0); | ||
| const error = await settled; | ||
|
|
||
| assert.ok(error instanceof AppError, `expected the refusal, got ${String(error)}`); | ||
| assert.equal(error.details?.runnerErrorCode, 'RUNNER_BUSY'); | ||
| assert.deepEqual(sentCommands(), ['snapshot', 'snapshot']); | ||
| }); | ||
|
|
||
| test('a cancelled request wakes the RUNNER_BUSY delay and reports the cancellation', async () => { | ||
| vi.useFakeTimers(); | ||
| const request = new AbortController(); | ||
| appleRunnerTestHost.update({ getRequestSignal: () => request.signal }); | ||
| mockExecuteRunnerCommandWithSession | ||
| .mockRejectedValueOnce(busyRefusal()) | ||
| .mockResolvedValue({ nodes: [], truncated: false }); | ||
|
|
||
| const pending = runAppleRunnerCommand( | ||
| IOS_SIMULATOR, | ||
| { command: 'snapshot' }, | ||
| { requestId: 'req-cancelled' }, | ||
| ); | ||
| const settled = pending.then( | ||
| () => undefined, | ||
| (error: unknown) => error, | ||
| ); | ||
| // Inside the first 200ms delay; the next attempt would otherwise succeed at the timer. | ||
| await vi.advanceTimersByTimeAsync(50); | ||
| requestCancellation.markRequestCanceled('req-cancelled'); | ||
| request.abort(); | ||
| await vi.advanceTimersByTimeAsync(0); | ||
| const error = await settled; | ||
|
|
||
| assert.ok(isRequestCanceledError(error), `expected a canceled request, got ${String(error)}`); | ||
| assert.deepEqual(sentCommands(), ['snapshot']); | ||
| }); | ||
|
|
||
| test('a mutating command meets RUNNER_BUSY once, with no status probe and no resend', async () => { | ||
| // The status-recovery bypass for structured replies reaches mutating commands too: the refusal | ||
| // says the tap did not run, so there is nothing to recover and nothing is replayed. | ||
| mockExecuteRunnerCommandWithSession.mockRejectedValueOnce(busyRefusal()); | ||
|
|
||
| await assert.rejects( | ||
| () => runAppleRunnerCommand(IOS_SIMULATOR, { command: 'tap', x: 120, y: 240 }), | ||
| (error: unknown) => { | ||
| assert.ok(error instanceof AppError); | ||
| assert.equal(error.details?.runnerErrorCode, 'RUNNER_BUSY'); | ||
| assert.equal(error.details?.recovery, undefined); | ||
| return true; | ||
| }, | ||
| ); | ||
|
|
||
| assert.deepEqual(sentCommands(), ['tap']); | ||
| assert.equal(mockInvalidateRunnerSession.mock.calls.length, 0); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.