|
| 1 | +import { promises as fs } from 'node:fs'; |
| 2 | +import os from 'node:os'; |
| 3 | +import path from 'node:path'; |
| 4 | +import type { DaemonResponse, SessionState } from './types.ts'; |
| 5 | +import { dispatchCommand } from '../core/dispatch.ts'; |
| 6 | +import { emitDiagnostic } from '../utils/diagnostics.ts'; |
| 7 | +import { normalizeError, type NormalizedError } from '../utils/errors.ts'; |
| 8 | +import type { ScreenshotOverlayRef } from '../utils/snapshot.ts'; |
| 9 | +import { contextFromFlags } from './context.ts'; |
| 10 | +import { annotateScreenshotWithRefs } from './screenshot-overlay.ts'; |
| 11 | + |
| 12 | +type CapturedAndroidSnapshotTimeoutEvidenceBase = { |
| 13 | + path: string; |
| 14 | + overlayRefsRequested: true; |
| 15 | +}; |
| 16 | + |
| 17 | +type AndroidSnapshotTimeoutEvidence = |
| 18 | + | { |
| 19 | + captureFailed: true; |
| 20 | + error: string; |
| 21 | + } |
| 22 | + | (CapturedAndroidSnapshotTimeoutEvidenceBase & { |
| 23 | + overlayRefSource: 'unavailable'; |
| 24 | + overlayRefsAnnotated: false; |
| 25 | + overlayRefCount: 0; |
| 26 | + }) |
| 27 | + | (CapturedAndroidSnapshotTimeoutEvidenceBase & { |
| 28 | + overlayRefSource: 'session-snapshot'; |
| 29 | + overlayRefsAnnotated: boolean; |
| 30 | + overlayRefCount: number; |
| 31 | + overlayRefs: ScreenshotOverlayRef[]; |
| 32 | + }) |
| 33 | + | (CapturedAndroidSnapshotTimeoutEvidenceBase & { |
| 34 | + overlayRefSource: 'session-snapshot'; |
| 35 | + overlayRefsAnnotated: false; |
| 36 | + overlayRefCount: 0; |
| 37 | + overlayAnnotationError: string; |
| 38 | + }); |
| 39 | + |
| 40 | +export async function maybeBuildAndroidSnapshotTimeoutFailure(params: { |
| 41 | + error: unknown; |
| 42 | + command: 'snapshot' | 'diff'; |
| 43 | + logPath: string; |
| 44 | + session: SessionState | undefined; |
| 45 | + device: SessionState['device']; |
| 46 | +}): Promise<Extract<DaemonResponse, { ok: false }> | undefined> { |
| 47 | + if (params.command !== 'snapshot') return undefined; |
| 48 | + if (params.device.platform !== 'android') return undefined; |
| 49 | + |
| 50 | + const normalized = normalizeError(params.error); |
| 51 | + if (!isAndroidSnapshotTimeoutError(normalized)) return undefined; |
| 52 | + |
| 53 | + return { |
| 54 | + ok: false, |
| 55 | + error: { |
| 56 | + ...normalized, |
| 57 | + details: { |
| 58 | + ...(normalized.details ?? {}), |
| 59 | + androidSnapshotTimeoutScreenshot: await captureAndroidSnapshotTimeoutEvidence(params), |
| 60 | + }, |
| 61 | + }, |
| 62 | + }; |
| 63 | +} |
| 64 | + |
| 65 | +async function captureAndroidSnapshotTimeoutEvidence(params: { |
| 66 | + logPath: string; |
| 67 | + session: SessionState | undefined; |
| 68 | + device: SessionState['device']; |
| 69 | +}): Promise<AndroidSnapshotTimeoutEvidence> { |
| 70 | + try { |
| 71 | + const tempDir = await fs.mkdtemp( |
| 72 | + path.join(os.tmpdir(), 'agent-device-android-snapshot-timeout-'), |
| 73 | + ); |
| 74 | + const screenshotPath = path.join(tempDir, 'snapshot-timeout-overlay-refs.png'); |
| 75 | + const data = await dispatchCommand(params.device, 'screenshot', [screenshotPath], undefined, { |
| 76 | + ...contextFromFlags( |
| 77 | + params.logPath, |
| 78 | + // Use a fresh unstabilized screenshot context; inheriting snapshot flags could repeat the |
| 79 | + // accessibility stabilization timeout that this fallback is trying to avoid. |
| 80 | + { screenshotNoStabilize: true }, |
| 81 | + params.session?.appBundleId, |
| 82 | + params.session?.trace?.outPath, |
| 83 | + ), |
| 84 | + surface: params.session?.surface, |
| 85 | + }); |
| 86 | + const resolvedPath = resolveCapturedScreenshotPath(data, screenshotPath); |
| 87 | + await fs.access(resolvedPath); |
| 88 | + const evidence = await annotateAndroidSnapshotTimeoutEvidence(resolvedPath, params.session); |
| 89 | + |
| 90 | + emitDiagnostic({ |
| 91 | + level: 'warn', |
| 92 | + phase: 'android_snapshot_timeout_screenshot_captured', |
| 93 | + data: { |
| 94 | + path: resolvedPath, |
| 95 | + overlayRefCount: 'overlayRefCount' in evidence ? evidence.overlayRefCount : undefined, |
| 96 | + overlayRefsAnnotated: |
| 97 | + 'overlayRefsAnnotated' in evidence ? evidence.overlayRefsAnnotated : undefined, |
| 98 | + }, |
| 99 | + }); |
| 100 | + return evidence; |
| 101 | + } catch (error) { |
| 102 | + const normalized = normalizeError(error); |
| 103 | + emitDiagnostic({ |
| 104 | + level: 'warn', |
| 105 | + phase: 'android_snapshot_timeout_screenshot_failed', |
| 106 | + data: { error: normalized.message }, |
| 107 | + }); |
| 108 | + return { |
| 109 | + captureFailed: true, |
| 110 | + error: normalized.message, |
| 111 | + }; |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +async function annotateAndroidSnapshotTimeoutEvidence( |
| 116 | + screenshotPath: string, |
| 117 | + session: SessionState | undefined, |
| 118 | +): Promise<AndroidSnapshotTimeoutEvidence> { |
| 119 | + if (!session?.snapshot) { |
| 120 | + return { |
| 121 | + path: screenshotPath, |
| 122 | + overlayRefsRequested: true, |
| 123 | + overlayRefsAnnotated: false, |
| 124 | + overlayRefSource: 'unavailable', |
| 125 | + overlayRefCount: 0, |
| 126 | + }; |
| 127 | + } |
| 128 | + |
| 129 | + try { |
| 130 | + const overlayRefs = await annotateScreenshotWithRefs({ |
| 131 | + screenshotPath, |
| 132 | + snapshot: session.snapshot, |
| 133 | + }); |
| 134 | + return { |
| 135 | + path: screenshotPath, |
| 136 | + overlayRefsRequested: true, |
| 137 | + overlayRefsAnnotated: overlayRefs.length > 0, |
| 138 | + overlayRefCount: overlayRefs.length, |
| 139 | + overlayRefSource: 'session-snapshot', |
| 140 | + overlayRefs, |
| 141 | + }; |
| 142 | + } catch (error) { |
| 143 | + const normalized = normalizeError(error); |
| 144 | + emitDiagnostic({ |
| 145 | + level: 'warn', |
| 146 | + phase: 'android_snapshot_timeout_screenshot_overlay_failed', |
| 147 | + data: { path: screenshotPath, error: normalized.message }, |
| 148 | + }); |
| 149 | + return { |
| 150 | + path: screenshotPath, |
| 151 | + overlayRefsRequested: true, |
| 152 | + overlayRefsAnnotated: false, |
| 153 | + overlayRefSource: 'session-snapshot', |
| 154 | + overlayRefCount: 0, |
| 155 | + overlayAnnotationError: normalized.message, |
| 156 | + }; |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +function resolveCapturedScreenshotPath(data: unknown, fallbackPath: string): string { |
| 161 | + return hasStringPath(data) ? data.path : fallbackPath; |
| 162 | +} |
| 163 | + |
| 164 | +function hasStringPath(value: unknown): value is { path: string } { |
| 165 | + return ( |
| 166 | + typeof value === 'object' && value !== null && 'path' in value && typeof value.path === 'string' |
| 167 | + ); |
| 168 | +} |
| 169 | + |
| 170 | +function isAndroidSnapshotTimeoutError(error: NormalizedError): boolean { |
| 171 | + if (error.code !== 'COMMAND_FAILED') return false; |
| 172 | + return ( |
| 173 | + hasKnownAndroidSnapshotTimeoutMessage(error) || |
| 174 | + hasHelperTimeoutDetails(error.details?.helper) || |
| 175 | + hasUiAutomatorDumpTimeoutDetails(error.details) |
| 176 | + ); |
| 177 | +} |
| 178 | + |
| 179 | +function hasKnownAndroidSnapshotTimeoutMessage(error: NormalizedError): boolean { |
| 180 | + const text = `${error.message}\n${error.hint ?? ''}`; |
| 181 | + if (/Android UI hierarchy dump timed out/i.test(text)) return true; |
| 182 | + if (/Stock UIAutomator fallback was skipped/i.test(text)) return true; |
| 183 | + return /Android accessibility snapshots can be blocked/i.test(text); |
| 184 | +} |
| 185 | + |
| 186 | +function hasHelperTimeoutDetails(helper: unknown): boolean { |
| 187 | + if (!helper || typeof helper !== 'object') return false; |
| 188 | + const helperRecord = helper as Record<string, unknown>; |
| 189 | + const errorType = String(helperRecord.errorType ?? ''); |
| 190 | + const message = String(helperRecord.message ?? ''); |
| 191 | + return /TimeoutException/i.test(errorType) || /timed out/i.test(message); |
| 192 | +} |
| 193 | + |
| 194 | +function hasUiAutomatorDumpTimeoutDetails(details: Record<string, unknown> | undefined): boolean { |
| 195 | + if (!details) return false; |
| 196 | + const timeoutMs = details?.timeoutMs; |
| 197 | + const cmd = details?.cmd; |
| 198 | + const args = normalizeArgList(details?.args); |
| 199 | + return ( |
| 200 | + typeof timeoutMs === 'number' && |
| 201 | + cmd === 'adb' && |
| 202 | + args.includes('uiautomator') && |
| 203 | + args.includes('dump') |
| 204 | + ); |
| 205 | +} |
| 206 | + |
| 207 | +function normalizeArgList(rawArgs: unknown): string[] { |
| 208 | + if (Array.isArray(rawArgs)) return rawArgs.map(String); |
| 209 | + return typeof rawArgs === 'string' ? rawArgs.split(/\s+/) : []; |
| 210 | +} |
0 commit comments