Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ vi.mock('../runner-xctestrun.ts', async () => {

import { prepareIosRunner, runAppleRunnerCommand } from '../index.ts';
import { resetRunnerRecycleLedgerForTests } from '../runner-recycle-ledger.ts';
import { RUNNER_REPLY_LOST_REASON } from '../runner-error-classification.ts';
import { RUNNER_EXCHANGE_REASONS } from '../runner-error-classification.ts';

const requestCancellation = createTestRequestCancellation();
const { markRequestCanceled, clearRequestCanceled, isRequestCanceled } = requestCancellation;
Expand Down Expand Up @@ -404,7 +404,7 @@ test('mutating commands keep invalidating when status recovery probe fails', asy
() => runAppleRunnerCommand(IOS_SIMULATOR, { command: 'tap', x: 120, y: 240 }),
(error: unknown) => {
assert.ok(error instanceof AppError);
assert.equal(error.details?.reason, RUNNER_REPLY_LOST_REASON);
assert.equal(error.details?.reason, RUNNER_EXCHANGE_REASONS.replyLost.reason);
assert.equal(error.details?.transportError, 'fetch failed');
return true;
},
Expand Down Expand Up @@ -434,7 +434,7 @@ test('a mutation lost reply keeps the hint its transport error already carries',
() => runAppleRunnerCommand(IOS_SIMULATOR, { command: 'tap', x: 120, y: 240 }),
(error: unknown) => {
assert.ok(error instanceof AppError);
assert.equal(error.details?.reason, RUNNER_REPLY_LOST_REASON);
assert.equal(error.details?.reason, RUNNER_EXCHANGE_REASONS.replyLost.reason);
assert.equal(error.details?.hint, 'Unlock the device and retry.');
return true;
},
Expand Down Expand Up @@ -920,7 +920,7 @@ test('sequence invalidates the session when the status probe fails', async () =>
}),
(error: unknown) => {
assert.ok(error instanceof AppError);
assert.equal(error.details?.reason, RUNNER_REPLY_LOST_REASON);
assert.equal(error.details?.reason, RUNNER_EXCHANGE_REASONS.replyLost.reason);
assert.equal(error.details?.transportError, 'fetch failed');
return true;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import type { RunnerCommand } from '../runner-contract.ts';
import {
isRetryableRunnerError,
isStructuredRunnerFailure,
RUNNER_REPLY_LOST_REASON,
RUNNER_EXCHANGE_REASONS,
} from '../runner-error-classification.ts';
import { runApplePressSeries } from '../runner-sequence.ts';
import { executeRunnerCommandWithSession, type RunnerSession } from '../runner-session.ts';
Expand Down Expand Up @@ -249,7 +249,10 @@ for (const row of ROWS) {
await assert.rejects(drive(), (error: unknown) => {
assert.ok(error instanceof AppError);
assert.equal(error.details?.dispatched, row.dispatched);
assert.equal(error.details?.reason === RUNNER_REPLY_LOST_REASON, REPLY_LOST_ROWS.has(row.id));
assert.equal(
error.details?.reason === RUNNER_EXCHANGE_REASONS.replyLost.reason,
REPLY_LOST_ROWS.has(row.id),
);
return true;
});
});
Expand All @@ -267,7 +270,7 @@ test.each([
async (_, status) => {
await assert.rejects(lostResponse([...status], SNAPSHOT), (error: unknown) => {
assert.ok(error instanceof AppError);
assert.notEqual(error.details?.reason, RUNNER_REPLY_LOST_REASON);
assert.notEqual(error.details?.reason, RUNNER_EXCHANGE_REASONS.replyLost.reason);
assert.equal(isRetryableRunnerError(error), true);
return true;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type { RunnerSession } from '../runner-session.ts';
import { appleRunnerTestHost } from '../test-host.ts';
import { withAppleRunnerProvider } from '../runner-provider.ts';
import { classifyRunnerReportedError, type RunnerCommand } from '../runner-contract.ts';
import { RUNNER_REPLY_LOST_REASON } from '../runner-error-classification.ts';
import { RUNNER_EXCHANGE_REASONS } from '../runner-error-classification.ts';
import { createRunnerPhaseBudget, requireRunnerPhaseRemainingMs } from '../runner-phase-budget.ts';
import {
captureDiagnostics,
Expand Down Expand Up @@ -185,7 +185,7 @@ test.each(Object.values(LOST_RESPONSE_MUTATION_ROWS))(
await assert.rejects(runAppleRunnerCommand(IOS_SIMULATOR, { ...request }), (error: unknown) => {
assert.ok(error instanceof AppError);
assert.equal(error.details?.dispatched, 'unknown');
assert.equal(error.details?.reason, RUNNER_REPLY_LOST_REASON);
assert.equal(error.details?.reason, RUNNER_EXCHANGE_REASONS.replyLost.reason);
return true;
});
assert.equal(
Expand Down Expand Up @@ -245,7 +245,7 @@ test.each(Object.values(LOST_RESPONSE_MUTATION_ROWS))(
await assert.rejects(runAppleRunnerCommand(IOS_SIMULATOR, { ...request }), (error: unknown) => {
assert.ok(error instanceof AppError);
assert.equal(error.details?.dispatched, 'unknown');
assert.equal(error.details?.reason, RUNNER_REPLY_LOST_REASON);
assert.equal(error.details?.reason, RUNNER_EXCHANGE_REASONS.replyLost.reason);
return true;
});
assert.equal(invalidateRunnerSessionMock.mock.calls.length, 1, 'the dead runner is dropped');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { IOS_SIMULATOR } from './device-fixtures.ts';
import type { ExecResult } from '@agent-device/host-kit/command';
import { handleRunnerTransportErrorAfterCommandSend } from '../runner-command-recovery.ts';
import type { RunnerCommand } from '../runner-contract.ts';
import { RUNNER_REPLY_LOST_REASON } from '../runner-error-classification.ts';
import { RUNNER_EXCHANGE_REASONS } from '../runner-error-classification.ts';
import type { RunnerSession } from '../runner-session.ts';
import {
startFakeRunnerServer,
Expand Down Expand Up @@ -131,7 +131,7 @@ test('notAccepted from a restarted runner fails the lost command as unknown, nam

await assert.rejects(result, (error: unknown) => {
assert.ok(error instanceof AppError);
assert.equal(error.details?.reason, RUNNER_REPLY_LOST_REASON);
assert.equal(error.details?.reason, RUNNER_EXCHANGE_REASONS.replyLost.reason);
assert.equal(error.details?.dispatched, 'unknown');
return true;
});
Expand All @@ -149,7 +149,7 @@ test('a failing status probe retains the invalidation and names the lost reply',
assert.notEqual(error.message, transportError.message);
assert.equal(error.details?.transportError, transportError.message);
assert.equal(error.cause, transportError);
assert.equal(error.details?.reason, RUNNER_REPLY_LOST_REASON);
assert.equal(error.details?.reason, RUNNER_EXCHANGE_REASONS.replyLost.reason);
assert.equal(error.details?.transportReason, 'socket_reset');
assert.equal(error.details?.recovery, 'status_probe_failed');
assert.equal(error.details?.dispatched, 'unknown');
Expand All @@ -167,7 +167,7 @@ test('a command without an id cannot be probed: invalidate and name the lost rep
await assert.rejects(result, (error: unknown) => {
assert.ok(error instanceof AppError);
assert.equal(error.cause, transportError);
assert.equal(error.details?.reason, RUNNER_REPLY_LOST_REASON);
assert.equal(error.details?.reason, RUNNER_EXCHANGE_REASONS.replyLost.reason);
assert.equal(error.details?.recovery, 'status_recovery_unavailable');
return true;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from '../runner-contract.ts';
import {
RUNNER_ERROR_RULES,
RUNNER_REPLY_LOST_REASON,
RUNNER_EXCHANGE_REASONS,
isRetryableRunnerError,
isRunnerBusyError,
resolveRunnerFatalErrorReason,
Expand Down Expand Up @@ -51,7 +51,7 @@ test('transport-shaped failures are retryable', () => {

test('a lost reply keys on its typed reason, never on the transport text it carries', () => {
for (const message of ['fetch failed', 'connect ECONNREFUSED 127.0.0.1:8100', 'socket hang up']) {
const lostReply = commandFailed(message, { reason: RUNNER_REPLY_LOST_REASON });
const lostReply = commandFailed(message, { reason: RUNNER_EXCHANGE_REASONS.replyLost.reason });
assert.equal(isRetryableRunnerError(lostReply), false, message);
assert.equal(shouldRetryRunnerConnectError(lostReply), false, message);
assert.equal(shouldRestartRunnerBeforeCommandSend(lostReply, TAP), false, message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,7 @@ vi.mock('../runner-session.ts', async () => {
});

import { runAppleRunnerCommand } from '../index.ts';
import {
isRetryableRunnerError,
RUNNER_REPLY_LOST_REASON,
} from '../runner-error-classification.ts';
import { isRetryableRunnerError, RUNNER_EXCHANGE_REASONS } from '../runner-error-classification.ts';
import type { RunnerCommand } from '../runner-contract.ts';
import { resetRunnerRecycleLedgerForTests } from '../runner-recycle-ledger.ts';
import { waitForRunner } from '../runner-startup-transport.ts';
Expand Down Expand Up @@ -177,7 +174,7 @@ const DRIVERS: Record<string, () => Promise<unknown>> = {
return await tap();
} catch (error) {
assert.ok(error instanceof AppError);
assert.equal(error.details?.reason, RUNNER_REPLY_LOST_REASON);
assert.equal(error.details?.reason, RUNNER_EXCHANGE_REASONS.replyLost.reason);
assert.equal(error.details?.recovery, 'status_probe_failed');
assert.equal(error.details?.runnerRestarted, undefined, 'the runner is not restarted');
throw error;
Expand Down Expand Up @@ -253,7 +250,7 @@ test('a mutation whose connect-loop POST timed out after writing is not restarte
assert.ok(error instanceof AppError);
assert.equal(error.details?.runnerRestarted, undefined);
assert.equal(error.details?.dispatched, 'unknown');
assert.equal(error.details?.reason, RUNNER_REPLY_LOST_REASON);
assert.equal(error.details?.reason, RUNNER_EXCHANGE_REASONS.replyLost.reason);
assert.equal(error.details?.recovery, 'status_probe_failed');
return true;
});
Expand All @@ -271,7 +268,7 @@ test('a mutation whose reply and status probe both fail on transport text is not
.mockRejectedValue(new AppError('COMMAND_FAILED', 'connect ECONNREFUSED 127.0.0.1:8100'));
await assert.rejects(tap(), (error: unknown) => {
assert.ok(error instanceof AppError);
assert.equal(error.details?.reason, RUNNER_REPLY_LOST_REASON);
assert.equal(error.details?.reason, RUNNER_EXCHANGE_REASONS.replyLost.reason);
assert.equal(error.details?.transportError, 'fetch failed');
assert.equal(isRetryableRunnerError(error), false);
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ test('prepareIosRunner invalidates rebuilt sessions when bad-cache recovery heal
assert.ok(error instanceof AppError);
assert.equal(error.message, 'artifact restored but runner did not connect');
assert.equal(error.details?.restoredFailureReason, 'Runner endpoint probe failed');
assert.equal(error.details?.reason, 'prepare_rebuilt_runner_unhealthy');
assert.equal(error.details?.rebuiltFailureMessage, 'Runner health timed out');
assert.equal(error.details?.xctestrunPath, '/tmp/rebuilt.xctestrun');
assert.equal(error.details?.artifact, 'rebuilt');
assert.equal(error.details?.cache, 'miss');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
RUNNER_ERROR_RULES,
classifyRunnerStartupFailure,
RUNNER_STARTUP_FAILURE_REASONS,
RUNNER_STARTUP_FAILURE_UNCLASSIFIED_REASON,
type RunnerStartupFailureReason,
} from '../runner-error-classification.ts';
import { assertDevToolsSecurityForIosRunner } from '../runner-dev-tools-security.ts';
Expand Down Expand Up @@ -151,7 +150,7 @@ test('the device speaking for a failure keeps the error that caused it', () => {
const caused = new AppError(
'COMMAND_FAILED',
'xcodebuild build-for-testing failed',
{ reason: RUNNER_STARTUP_FAILURE_UNCLASSIFIED_REASON },
{ reason: RUNNER_STARTUP_FAILURE_REASONS.buildFailedUnclassified.reason },
new Error('xcodebuild was killed by the host'),
);

Expand Down Expand Up @@ -218,11 +217,15 @@ function assertToolOutputReachable(
assert.match(String(nestedDetails?.stdout), /AgentDeviceRunner/);
}

const STARTUP_FAILURE_REASONS = Object.values(RUNNER_STARTUP_FAILURE_REASONS).map(
(row) => row.reason,
);

test('every startup failure reason has a recorded fixture', () => {
const reasonsWithFixtures = new Set(RUNNER_STARTUP_FAILURE_FIXTURES.map((f) => f.reason));

assert.equal(reasonsWithFixtures.size, RUNNER_STARTUP_FAILURE_REASONS.length);
for (const reason of RUNNER_STARTUP_FAILURE_REASONS) {
assert.equal(reasonsWithFixtures.size, STARTUP_FAILURE_REASONS.length);
for (const reason of STARTUP_FAILURE_REASONS) {
assert.ok(reasonsWithFixtures.has(reason), `no fixture records the ${reason} reason`);
}
});
Expand All @@ -232,9 +235,9 @@ test('every reason the classifier can name is produced by a rule row', () => {
RUNNER_ERROR_RULES.flatMap((rule) => (rule.buildFailure ? [rule.buildFailure.reason] : [])),
);

for (const reason of RUNNER_STARTUP_FAILURE_REASONS) {
for (const reason of STARTUP_FAILURE_REASONS) {
// The catch-all is the classifier's own answer when no row matched, so it names no row.
if (reason === RUNNER_STARTUP_FAILURE_UNCLASSIFIED_REASON) continue;
if (reason === RUNNER_STARTUP_FAILURE_REASONS.buildFailedUnclassified.reason) continue;
// The device-readiness members are named by the device's own states in
// `runner-device-readiness.ts`, not by a rule row: no amount of tool text establishes them,
// which is exactly why they are declared as a subset (#2683).
Expand All @@ -258,7 +261,10 @@ test('a scoped-set simulator xcodebuild cannot find names its set and the Xcode'
test('a default-set simulator xcodebuild cannot find names no simulator set', async () => {
const envelope = await driveBuildFailure(buildFixtureById('default-set-destination-not-found'));

assert.equal(envelope.details?.reason, RUNNER_STARTUP_FAILURE_UNCLASSIFIED_REASON);
assert.equal(
envelope.details?.reason,
RUNNER_STARTUP_FAILURE_REASONS.buildFailedUnclassified.reason,
);
assert.equal(envelope.details?.simulatorSetPath, undefined);
assert.equal(envelope.message, 'xcodebuild build-for-testing failed');
assert.doesNotMatch(String(envelope.hint), /DVTSimulatorSetLocation/);
Expand All @@ -277,7 +283,10 @@ test('an argv that names a provisioning profile is not evidence of a signing fai

const envelope = await driveBuildFailure(argvFixture);

assert.equal(envelope.details?.reason, RUNNER_STARTUP_FAILURE_UNCLASSIFIED_REASON);
assert.equal(
envelope.details?.reason,
RUNNER_STARTUP_FAILURE_REASONS.buildFailedUnclassified.reason,
);
assert.match(String(envelope.hint), CACHE_RECOVERY_HINT);
assert.doesNotMatch(String(envelope.hint), /AGENT_DEVICE_IOS_PROVISIONING_PROFILE/);
});
Expand All @@ -301,7 +310,7 @@ test('the failure the build catch publishes does not classify itself', async ()

const reclassified = classifyRunnerStartupFailure(published);

assert.equal(reclassified.reason, RUNNER_STARTUP_FAILURE_UNCLASSIFIED_REASON);
assert.equal(reclassified.reason, RUNNER_STARTUP_FAILURE_REASONS.buildFailedUnclassified.reason);
assert.match(reclassified.hint, CACHE_RECOVERY_HINT);
});

Expand All @@ -318,7 +327,10 @@ test('an identical message without the typed host fact is not read as a DevTools

const envelope = await driveBuildRejection(withoutFact);

assert.equal(envelope.details?.reason, RUNNER_STARTUP_FAILURE_UNCLASSIFIED_REASON);
assert.equal(
envelope.details?.reason,
RUNNER_STARTUP_FAILURE_REASONS.buildFailedUnclassified.reason,
);
assert.match(String(envelope.hint), CACHE_RECOVERY_HINT);
assert.doesNotMatch(String(envelope.hint), /DevToolsSecurity/);
});
Expand All @@ -332,7 +344,10 @@ test('an app identifier named without the availability fact is not read as a tak

const envelope = await driveBuildFailure(nearMiss);

assert.equal(envelope.details?.reason, RUNNER_STARTUP_FAILURE_UNCLASSIFIED_REASON);
assert.equal(
envelope.details?.reason,
RUNNER_STARTUP_FAILURE_REASONS.buildFailedUnclassified.reason,
);
assert.match(String(envelope.hint), CACHE_RECOVERY_HINT);
assert.doesNotMatch(String(envelope.hint), /AGENT_DEVICE_IOS_BUNDLE_ID/);
});
Expand All @@ -344,7 +359,10 @@ test('a conflicting-settings failure is not answered with missing-profile advice

const envelope = await driveBuildFailure(conflict);

assert.equal(envelope.details?.reason, RUNNER_STARTUP_FAILURE_UNCLASSIFIED_REASON);
assert.equal(
envelope.details?.reason,
RUNNER_STARTUP_FAILURE_REASONS.buildFailedUnclassified.reason,
);
assert.match(String(envelope.hint), CACHE_RECOVERY_HINT);
assert.doesNotMatch(String(envelope.hint), /AGENT_DEVICE_IOS_PROVISIONING_PROFILE/);
});
Expand Down
9 changes: 7 additions & 2 deletions packages/apple-runner/src/runner-cache-metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { isMacOs, type DeviceInfo } from '@agent-device/kernel/device';
import { AppError } from '@agent-device/kernel/errors';
import { AppError, type ReasonTable } from '@agent-device/kernel/errors';
import { hostEnvironment, isEnvTruthy, findProjectRoot, readVersion } from './host.ts';
import {
resolveRunnerBuildDestinationFamily,
Expand All @@ -19,6 +19,11 @@ import {
import type { RunnerPhaseBudget } from './runner-phase-budget.ts';
import { computeRunnerSourceFingerprint } from './runner-source.ts';

const RUNNER_BUILD_SETTINGS_REASONS = {
// The runner build used settings other than those its cache identity records.
buildSettingsMismatch: { reason: 'runner_build_settings_mismatch' },
} as const satisfies ReasonTable<'apple runner'>;

const DEFAULT_IOS_RUNNER_APP_BUNDLE_ID = 'com.callstack.agentdevice.runner';
const RUNNER_DERIVED_ROOT = path.join(os.homedir(), '.agent-device', 'apple-runner');
export const RUNNER_CACHE_METADATA_FILE = '.agent-device-runner-cache.json';
Expand Down Expand Up @@ -504,7 +509,7 @@ export function requireRunnerBuildSettingsMatchBuildLog(
'COMMAND_FAILED',
'The Apple runner build did not use the settings its cache identity records',
{
reason: 'runner_build_settings_mismatch',
reason: RUNNER_BUILD_SETTINGS_REASONS.buildSettingsMismatch.reason,
buildLogPath: logPath,
differences,
hint: 'Align the build invocation with the runner cache identity resolvers, or rebuild without the cache.',
Expand Down
9 changes: 7 additions & 2 deletions packages/apple-runner/src/runner-cache.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from 'node:fs';
import path from 'node:path';
import { AppError } from '@agent-device/kernel/errors';
import { AppError, type ReasonTable } from '@agent-device/kernel/errors';
import { resolveRunnerDerivedOverride } from './apple-runner-platform.ts';
import {
emitDiagnostic,
Expand All @@ -24,6 +24,11 @@ import type {
RunnerCacheArtifactMismatch,
RunnerCacheRefusal,
} from './runner-artifact-manifest.ts';

const RUNNER_CACHE_REASONS = {
// The runner build products cannot be certified for cache reuse; `details.refusalReason` says why.
cacheUncertifiable: { reason: 'runner_cache_uncertifiable' },
} as const satisfies ReasonTable<'apple runner'>;
export {
resolveExpectedRunnerCacheMetadata,
resolveRunnerArchBuildSettings,
Expand Down Expand Up @@ -203,7 +208,7 @@ export function requireCertifiedRunnerCacheArtifacts(
'COMMAND_FAILED',
'The Apple runner products cannot be certified for cache reuse',
{
reason: 'runner_cache_uncertifiable',
reason: RUNNER_CACHE_REASONS.cacheUncertifiable.reason,
refusalReason: refusal.reason,
refusingPath: refusal.path ?? derived,
...(refusal.reason === 'root_unusable' ? {} : { resolvesTo: refusal.target }),
Expand Down
6 changes: 3 additions & 3 deletions packages/apple-runner/src/runner-command-recovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
} from './runner-contract.ts';
import { isReadOnlyRunnerCommand } from './runner-command-traits.ts';
import {
RUNNER_REPLY_LOST_REASON,
RUNNER_EXCHANGE_REASONS,
shouldRetainRunnerSession,
shouldRecoverRunnerStatus,
} from './runner-error-classification.ts';
Expand Down Expand Up @@ -111,7 +111,7 @@ async function applyRunnerTransportRecovery(
}

/**
* A lost reply fails a mutation as {@link RUNNER_REPLY_LOST_REASON}: it is not resent. A read keeps
* A lost reply fails a mutation as `runner_reply_lost`: it is not resent. A read keeps
* the transport error, because `runAppleRunnerCommand` resends a read on exactly that error.
*/
function resolveRunnerRecoveryFailure(
Expand Down Expand Up @@ -146,7 +146,7 @@ function buildLostReplyError(
...(lostReply.lifecycleState === undefined
? {}
: { lifecycleState: lostReply.lifecycleState }),
reason: RUNNER_REPLY_LOST_REASON,
reason: RUNNER_EXCHANGE_REASONS.replyLost.reason,
...(transportReason === undefined ? {} : { transportReason }),
recovery: lostReply.recovery,
...readReadinessPreflightRecoveryDetails(transportError),
Expand Down
Loading
Loading