|
| 1 | +import fs from 'node:fs/promises'; |
| 2 | +import type { FileHandle } from 'node:fs/promises'; |
| 3 | +import { AppError, type AppErrorCode } from '../../utils/errors.ts'; |
| 4 | + |
| 5 | +const RUNNER_LOG_TAIL_BYTES = 64 * 1024; |
| 6 | + |
| 7 | +type RunnerFailureDiagnostic = { |
| 8 | + code?: AppErrorCode; |
| 9 | + reason: string; |
| 10 | + hint: string; |
| 11 | +}; |
| 12 | + |
| 13 | +const IOS_TARGET_AX_CRASH_HINT = |
| 14 | + 'The target iOS app appears to have crashed while XCTest/AXRuntime read accessibility attributes. This is usually a simulator/XCTest/runtime or app accessibility payload issue, not a text-entry failure. Reproduce on the latest stable simulator runtime, reinstall the app, and capture the app crash from Console.app or ~/Library/Logs/DiagnosticReports with the exact command, selector/ref, app build, Xcode, and simulator runtime.'; |
| 15 | + |
| 16 | +const IOS_TARGET_APP_CRASH_HINT = |
| 17 | + 'The target iOS app appears to have crashed while the runner was executing the command. Reopen or reinstall the app, retry on a fresh/latest stable simulator runtime, and capture the app crash from Console.app or ~/Library/Logs/DiagnosticReports with the exact command, selector/ref, app build, Xcode, and simulator runtime.'; |
| 18 | + |
| 19 | +export async function enrichRunnerFailureFromLog(params: { |
| 20 | + error: AppError; |
| 21 | + logPath?: string; |
| 22 | +}): Promise<AppError> { |
| 23 | + const diagnostic = await resolveRunnerFailureDiagnostic(params.logPath); |
| 24 | + if (!diagnostic) return params.error; |
| 25 | + |
| 26 | + return new AppError( |
| 27 | + diagnostic.code ?? params.error.code, |
| 28 | + params.error.message, |
| 29 | + { |
| 30 | + ...(params.error.details ?? {}), |
| 31 | + hint: |
| 32 | + typeof params.error.details?.hint === 'string' |
| 33 | + ? `${params.error.details.hint} ${diagnostic.hint}` |
| 34 | + : diagnostic.hint, |
| 35 | + runnerFailureReason: diagnostic.reason, |
| 36 | + }, |
| 37 | + params.error, |
| 38 | + ); |
| 39 | +} |
| 40 | + |
| 41 | +async function resolveRunnerFailureDiagnostic( |
| 42 | + logPath: string | undefined, |
| 43 | +): Promise<RunnerFailureDiagnostic | undefined> { |
| 44 | + if (!logPath) return undefined; |
| 45 | + const tail = await readFileTail(logPath, RUNNER_LOG_TAIL_BYTES); |
| 46 | + if (!tail) return undefined; |
| 47 | + return classifyRunnerFailureLog(tail); |
| 48 | +} |
| 49 | + |
| 50 | +function classifyRunnerFailureLog(logText: string): RunnerFailureDiagnostic | undefined { |
| 51 | + const normalized = logText.toLowerCase(); |
| 52 | + if (isAxRuntimeAccessibilityCrash(normalized)) { |
| 53 | + return { |
| 54 | + code: 'IOS_TARGET_APP_CRASH', |
| 55 | + reason: 'target_app_axruntime_coretext_crash', |
| 56 | + hint: IOS_TARGET_AX_CRASH_HINT, |
| 57 | + }; |
| 58 | + } |
| 59 | + if (isTargetAppCrash(normalized)) { |
| 60 | + return { |
| 61 | + code: 'IOS_TARGET_APP_CRASH', |
| 62 | + reason: 'target_app_crash', |
| 63 | + hint: IOS_TARGET_APP_CRASH_HINT, |
| 64 | + }; |
| 65 | + } |
| 66 | + return undefined; |
| 67 | +} |
| 68 | + |
| 69 | +function isAxRuntimeAccessibilityCrash(normalized: string): boolean { |
| 70 | + return ( |
| 71 | + normalized.includes('axruntime') && |
| 72 | + normalized.includes('coretext') && |
| 73 | + (normalized.includes('attributesforelement') || |
| 74 | + normalized.includes('axuielementcopymultipleattributevalues') || |
| 75 | + normalized.includes('reconstitutedsmuggledctfontfromdictionary') || |
| 76 | + normalized.includes('reconstitutedsmuggledattributedstringfromdictionary')) |
| 77 | + ); |
| 78 | +} |
| 79 | + |
| 80 | +function isTargetAppCrash(normalized: string): boolean { |
| 81 | + return ( |
| 82 | + normalized.includes('process crashed') || |
| 83 | + normalized.includes('the application under test') || |
| 84 | + normalized.includes('terminated unexpectedly') || |
| 85 | + (normalized.includes('exception type:') && normalized.includes('thread 0 crashed')) || |
| 86 | + (normalized.includes('crashed') && normalized.includes('xctest')) |
| 87 | + ); |
| 88 | +} |
| 89 | + |
| 90 | +async function readFileTail(filePath: string, maxBytes: number): Promise<string | undefined> { |
| 91 | + let handle: FileHandle | undefined; |
| 92 | + try { |
| 93 | + const stat = await fs.stat(filePath); |
| 94 | + const start = Math.max(0, stat.size - maxBytes); |
| 95 | + const length = stat.size - start; |
| 96 | + if (length <= 0) return undefined; |
| 97 | + |
| 98 | + handle = await fs.open(filePath, 'r'); |
| 99 | + const buffer = Buffer.alloc(length); |
| 100 | + await handle.read(buffer, 0, length, start); |
| 101 | + return buffer.toString('utf8'); |
| 102 | + } catch { |
| 103 | + return undefined; |
| 104 | + } finally { |
| 105 | + await handle?.close().catch(() => {}); |
| 106 | + } |
| 107 | +} |
0 commit comments