|
| 1 | +import { beforeEach, test, vi } from 'vitest'; |
| 2 | +import assert from 'node:assert/strict'; |
| 3 | +import { IOS_SIMULATOR } from './device-fixtures.ts'; |
| 4 | +import { createTestRequestCancellation } from './runner-session-fixtures.ts'; |
| 5 | +import { AppError, isRequestCanceledError } from '@agent-device/kernel/errors'; |
| 6 | +import { appleRunnerTestHost } from '../test-host.ts'; |
| 7 | +import type { RunnerSession } from '../runner-session-types.ts'; |
| 8 | + |
| 9 | +// The read-only resend policy around a `RUNNER_BUSY` refusal (#2804). The runner refuses fast |
| 10 | +// while watchdog-abandoned XCTest work drains (1.2s median on the Bluesky bench), so the daemon's |
| 11 | +// resend has to outlast that drain, keep the old three-attempt budget for transport failures, and |
| 12 | +// stay cancellable across the seconds it may now wait. |
| 13 | + |
| 14 | +const { |
| 15 | + mockEnsureRunnerSession, |
| 16 | + mockExecuteRunnerCommandWithSession, |
| 17 | + mockReadRunnerSessionLiveness, |
| 18 | + mockInvalidateRunnerSession, |
| 19 | +} = vi.hoisted(() => ({ |
| 20 | + mockEnsureRunnerSession: vi.fn(), |
| 21 | + mockExecuteRunnerCommandWithSession: vi.fn(), |
| 22 | + mockReadRunnerSessionLiveness: vi.fn(), |
| 23 | + mockInvalidateRunnerSession: vi.fn(), |
| 24 | +})); |
| 25 | + |
| 26 | +vi.mock('../runner-session.ts', async () => { |
| 27 | + const actual = |
| 28 | + await vi.importActual<typeof import('../runner-session.ts')>('../runner-session.ts'); |
| 29 | + return { |
| 30 | + ...actual, |
| 31 | + ensureRunnerSession: mockEnsureRunnerSession, |
| 32 | + executeRunnerCommandWithSession: mockExecuteRunnerCommandWithSession, |
| 33 | + readRunnerSessionLiveness: mockReadRunnerSessionLiveness, |
| 34 | + invalidateRunnerSession: mockInvalidateRunnerSession, |
| 35 | + }; |
| 36 | +}); |
| 37 | + |
| 38 | +import { runAppleRunnerCommand } from '../runner-client.ts'; |
| 39 | +import { buildRunnerResponseError } from '../runner-contract.ts'; |
| 40 | +import { resetRunnerRecycleLedgerForTests } from '../runner-recycle-ledger.ts'; |
| 41 | + |
| 42 | +const requestCancellation = createTestRequestCancellation(); |
| 43 | + |
| 44 | +beforeEach(() => { |
| 45 | + vi.resetAllMocks(); |
| 46 | + vi.useRealTimers(); |
| 47 | + resetRunnerRecycleLedgerForTests(); |
| 48 | + requestCancellation.reset(); |
| 49 | + appleRunnerTestHost.update({ |
| 50 | + emitDiagnostic: vi.fn(), |
| 51 | + isRequestCanceled: requestCancellation.isRequestCanceled, |
| 52 | + getRequestSignal: () => undefined, |
| 53 | + }); |
| 54 | + const session = readySession(); |
| 55 | + mockEnsureRunnerSession.mockResolvedValue(session); |
| 56 | + // A live session, or `executeRunnerCommand` reads the resend as a recycle boot and charges the |
| 57 | + // per-request recycle budget instead of resending. |
| 58 | + mockReadRunnerSessionLiveness.mockReturnValue({ |
| 59 | + sessionId: session.sessionId, |
| 60 | + liveness: 'ready', |
| 61 | + }); |
| 62 | +}); |
| 63 | + |
| 64 | +function readySession(): RunnerSession { |
| 65 | + return { |
| 66 | + sessionId: 'session-8100', |
| 67 | + device: IOS_SIMULATOR, |
| 68 | + deviceId: IOS_SIMULATOR.id, |
| 69 | + port: 8100, |
| 70 | + xctestrunPath: '/tmp/runner.xctestrun', |
| 71 | + jsonPath: '/tmp/runner.json', |
| 72 | + testPromise: Promise.resolve({ exitCode: 0, stdout: '', stderr: '' }), |
| 73 | + child: { pid: 1234, exitCode: null }, |
| 74 | + state: 'ready', |
| 75 | + } as RunnerSession; |
| 76 | +} |
| 77 | + |
| 78 | +/** The runner's live refusal, exactly as the transport decodes it. */ |
| 79 | +function busyRefusal(): AppError { |
| 80 | + return buildRunnerResponseError({ |
| 81 | + ok: false, |
| 82 | + error: { code: 'RUNNER_BUSY', message: 'The iOS runner is still finishing a previous command' }, |
| 83 | + }); |
| 84 | +} |
| 85 | + |
| 86 | +/** What the status probe reads back for the same refusal after each resend. */ |
| 87 | +const busyJournal = { |
| 88 | + lifecycleState: 'failed', |
| 89 | + lifecycleErrorCode: 'RUNNER_BUSY', |
| 90 | + lifecycleErrorMessage: 'The iOS runner is still finishing a previous command', |
| 91 | +}; |
| 92 | + |
| 93 | +function sentCommands(): string[] { |
| 94 | + return mockExecuteRunnerCommandWithSession.mock.calls.map((call) => call[2].command); |
| 95 | +} |
| 96 | + |
| 97 | +test('read-only commands wait out RUNNER_BUSY past the transport resend backoff', async () => { |
| 98 | + // The logged shape: three busy answers spaced 200/400/800ms apart, then the runner answers. |
| 99 | + // The default three attempts gave up at ~0.6s, 60ms before the abandoned work drained. |
| 100 | + vi.useFakeTimers(); |
| 101 | + mockExecuteRunnerCommandWithSession |
| 102 | + .mockRejectedValueOnce(busyRefusal()) |
| 103 | + .mockResolvedValueOnce(busyJournal) |
| 104 | + .mockRejectedValueOnce(busyRefusal()) |
| 105 | + .mockResolvedValueOnce(busyJournal) |
| 106 | + .mockRejectedValueOnce(busyRefusal()) |
| 107 | + .mockResolvedValueOnce(busyJournal) |
| 108 | + .mockResolvedValueOnce({ nodes: [], truncated: false }); |
| 109 | + |
| 110 | + const pending = runAppleRunnerCommand(IOS_SIMULATOR, { command: 'snapshot' }); |
| 111 | + await vi.advanceTimersByTimeAsync(6_000); |
| 112 | + const result = await pending; |
| 113 | + |
| 114 | + assert.deepEqual(result, { nodes: [], truncated: false }); |
| 115 | + assert.equal(mockInvalidateRunnerSession.mock.calls.length, 0); |
| 116 | + assert.deepEqual(sentCommands(), [ |
| 117 | + 'snapshot', |
| 118 | + 'status', |
| 119 | + 'snapshot', |
| 120 | + 'status', |
| 121 | + 'snapshot', |
| 122 | + 'status', |
| 123 | + 'snapshot', |
| 124 | + ]); |
| 125 | +}); |
| 126 | + |
| 127 | +test('a RUNNER_BUSY resend still ends when the refusals outlast the window', async () => { |
| 128 | + vi.useFakeTimers(); |
| 129 | + mockExecuteRunnerCommandWithSession.mockImplementation(async (_device, _session, command) => |
| 130 | + command.command === 'status' ? busyJournal : Promise.reject(busyRefusal()), |
| 131 | + ); |
| 132 | + |
| 133 | + const pending = runAppleRunnerCommand(IOS_SIMULATOR, { command: 'snapshot' }); |
| 134 | + const settled = pending.then( |
| 135 | + () => undefined, |
| 136 | + (error: unknown) => error, |
| 137 | + ); |
| 138 | + await vi.advanceTimersByTimeAsync(20_000); |
| 139 | + const error = await settled; |
| 140 | + |
| 141 | + assert.ok(error instanceof AppError); |
| 142 | + assert.equal(error.details?.runnerErrorCode, 'RUNNER_BUSY'); |
| 143 | + assert.equal(sentCommands().filter((command) => command === 'snapshot').length, 8); |
| 144 | +}); |
| 145 | + |
| 146 | +test('read-only transport failures keep the three-attempt resend budget', async () => { |
| 147 | + mockExecuteRunnerCommandWithSession |
| 148 | + .mockRejectedValueOnce(new AppError('COMMAND_FAILED', 'fetch failed')) |
| 149 | + .mockResolvedValueOnce({ lifecycleState: 'started' }) |
| 150 | + .mockRejectedValueOnce(new AppError('COMMAND_FAILED', 'fetch failed')) |
| 151 | + .mockResolvedValueOnce({ lifecycleState: 'started' }) |
| 152 | + .mockRejectedValueOnce(new AppError('COMMAND_FAILED', 'fetch failed')) |
| 153 | + .mockResolvedValueOnce({ lifecycleState: 'started' }) |
| 154 | + .mockResolvedValueOnce({ nodes: [], truncated: false }); |
| 155 | + |
| 156 | + await assert.rejects( |
| 157 | + () => runAppleRunnerCommand(IOS_SIMULATOR, { command: 'snapshot' }), |
| 158 | + (error: unknown) => { |
| 159 | + assert.ok(error instanceof AppError); |
| 160 | + assert.equal(error.message, 'fetch failed'); |
| 161 | + return true; |
| 162 | + }, |
| 163 | + ); |
| 164 | + |
| 165 | + assert.equal(sentCommands().filter((command) => command === 'snapshot').length, 3); |
| 166 | +}); |
| 167 | + |
| 168 | +test('a cancelled request wakes the RUNNER_BUSY delay instead of sleeping it out', async () => { |
| 169 | + vi.useFakeTimers(); |
| 170 | + const controller = new AbortController(); |
| 171 | + mockExecuteRunnerCommandWithSession |
| 172 | + .mockRejectedValueOnce(busyRefusal()) |
| 173 | + .mockResolvedValueOnce(busyJournal) |
| 174 | + .mockResolvedValue({ nodes: [], truncated: false }); |
| 175 | + |
| 176 | + const pending = runAppleRunnerCommand( |
| 177 | + IOS_SIMULATOR, |
| 178 | + { command: 'snapshot' }, |
| 179 | + { signal: controller.signal }, |
| 180 | + ); |
| 181 | + const settled = pending.then( |
| 182 | + () => undefined, |
| 183 | + (error: unknown) => error, |
| 184 | + ); |
| 185 | + // Inside the first 200ms delay; the next attempt would otherwise succeed at the timer. |
| 186 | + await vi.advanceTimersByTimeAsync(50); |
| 187 | + controller.abort(); |
| 188 | + await vi.advanceTimersByTimeAsync(0); |
| 189 | + const error = await settled; |
| 190 | + |
| 191 | + assert.ok(isRequestCanceledError(error), `expected a canceled request, got ${String(error)}`); |
| 192 | + assert.deepEqual(sentCommands(), ['snapshot', 'status']); |
| 193 | +}); |
0 commit comments