agent-device 0.18.1 → 0.18.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/android-multitouch-helper/dist/{agent-device-android-multitouch-helper-0.18.1.apk → agent-device-android-multitouch-helper-0.18.3.apk} +0 -0
- package/android-multitouch-helper/dist/agent-device-android-multitouch-helper-0.18.3.apk.sha256 +1 -0
- package/android-multitouch-helper/dist/{agent-device-android-multitouch-helper-0.18.1.manifest.json → agent-device-android-multitouch-helper-0.18.3.manifest.json} +4 -4
- package/android-snapshot-helper/dist/{agent-device-android-snapshot-helper-0.18.1.apk → agent-device-android-snapshot-helper-0.18.3.apk} +0 -0
- package/android-snapshot-helper/dist/agent-device-android-snapshot-helper-0.18.3.apk.sha256 +1 -0
- package/android-snapshot-helper/dist/{agent-device-android-snapshot-helper-0.18.1.manifest.json → agent-device-android-snapshot-helper-0.18.3.manifest.json} +6 -6
- package/apple-runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests.swift +2 -1
- package/dist/src/1986.js +1 -1
- package/dist/src/2948.js +16 -16
- package/dist/src/3340.js +8 -8
- package/dist/src/4428.js +3 -3
- package/dist/src/495.js +5 -5
- package/dist/src/6849.js +1 -1
- package/dist/src/7871.js +1 -1
- package/dist/src/8184.js +1 -1
- package/dist/src/8414.js +2 -2
- package/dist/src/8875.js +4 -1
- package/dist/src/9581.js +3 -3
- package/dist/src/9624.js +1 -1
- package/dist/src/9722.js +38 -37
- package/dist/src/android.js +1 -1
- package/dist/src/apps.js +9 -9
- package/dist/src/audio-probe.js +1 -1
- package/dist/src/batch.d.ts +1 -1
- package/dist/src/cli-help.js +15 -8
- package/dist/src/cli.js +23 -23
- package/dist/src/contracts.d.ts +1 -1
- package/dist/src/devices.js +1 -1
- package/dist/src/devices~1.js +1 -1
- package/dist/src/index.d.ts +61 -16
- package/dist/src/input-actions.js +1 -1
- package/dist/src/interaction.js +1 -1
- package/dist/src/interactor.js +1 -1
- package/dist/src/internal/bin.js +1 -1
- package/dist/src/lease.js +1 -1
- package/dist/src/linux.js +1 -1
- package/dist/src/record-trace-recording.js +3 -3
- package/dist/src/remote-config.d.ts +1 -1
- package/dist/src/selector-runtime.js +1 -1
- package/dist/src/selectors.d.ts +8 -4
- package/dist/src/session.js +11 -11
- package/dist/src/simulator.js +2 -2
- package/dist/src/snapshot.js +2 -2
- package/dist/src/web.js +1 -1
- package/package.json +1 -1
- package/server.json +2 -2
- package/skills/agent-device/SKILL.md +2 -0
- package/android-multitouch-helper/dist/agent-device-android-multitouch-helper-0.18.1.apk.sha256 +0 -1
- package/android-snapshot-helper/dist/agent-device-android-snapshot-helper-0.18.1.apk.sha256 +0 -1
- package/dist/src/432.js +0 -4
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
let e,t,r,a;import i from"node:fs";import o from"node:path";import{createHash as n}from"node:crypto";import s from"node:os";import{fileURLToPath as c}from"node:url";import{AppError as d}from"./485.js";import{runCmd as
|
|
1
|
+
let e,t,r,a;import i from"node:fs";import o from"node:path";import{createHash as n}from"node:crypto";import s from"node:os";import{fileURLToPath as c}from"node:url";import{AppError as d}from"./485.js";import{withDiagnosticTimer as u,emitDiagnostic as l,runCmd as p}from"./8875.js";import{acquireProcessLock as m}from"./8391.js";import{readProcessStartTime as h}from"./9414.js";import{sleep as f}from"./5560.js";import{androidDeviceForSerial as w,runAndroidAdb as g,pullAndroidAdbFile as y}from"./3340.js";import{runXcrun as _,tryGetPlugin as v,SessionStore as P,IOS_RUNNER_CONTAINER_BUNDLE_IDS as S,requireCommandSupported as A,errorResponse as b,resolveImplicitSessionScope as M,resolvePublicSessionName as C,registerBuiltinPlatformPlugins as x,resolveTargetDevice as R,runAppleRunnerCommand as I}from"./9722.js";import{isIosFamily as D}from"./6849.js";import{buildAppleRunnerRequestOptions as N,ensureDeviceReady as E}from"./8414.js";import{defaultRecordingPath as $,appendRecordingExtensionWhenMissing as O}from"./5643.js";import{resolveWebProvider as k}from"./provider.js";import{resolveRecordingProvider as F}from"./recording-provider.js";import{recordingQualityInputToExportQuality as T,RECORDING_EXPORT_QUALITIES as L}from"./2948.js";import{recordSessionAction as U}from"./handler-utils.js";function z(e=process.env){let t=V(),r=o.join(t,"home"),a=o.join(t,"module-cache");return i.mkdirSync(r,{recursive:!0}),i.mkdirSync(a,{recursive:!0}),{...e,HOME:r,CLANG_MODULE_CACHE_PATH:a}}async function K(e){let t=i.statSync(e.sourcePath),r=i.readFileSync(e.sourcePath),a=W(e.cacheName??o.basename(e.sourcePath,o.extname(e.sourcePath))),n=B(["2",process.platform,process.arch,o.resolve(e.sourcePath),t.size,r]),s=o.join(V(),"bin",`${a}-${n}`);return await H({sourcePath:e.sourcePath,executablePath:s,timeoutMs:e.timeoutMs}),s}async function G(e){let t=W(e.cacheName),r=B(["2",process.platform,process.arch,e.source]),a=o.join(V(),"sources",`${t}-${r}.swift`),i=o.join(V(),"bin",`${t}-${r}`);return await H({sourcePath:a,executablePath:i,sourceText:e.source,timeoutMs:e.timeoutMs}),i}function V(){let e=process.env.AGENT_DEVICE_SWIFT_CACHE_DIR?.trim();return e?o.resolve(e):o.join(s.tmpdir(),"agent-device-swift-cache")}async function H(e){if(q(e.executablePath))return;let t=o.dirname(e.executablePath);i.mkdirSync(t,{recursive:!0});let r=`${e.executablePath}.lock`,a=await j(r,e.executablePath,e.timeoutMs??12e4);if(!a)return;let n=i.mkdtempSync(o.join(t,`.${o.basename(e.executablePath)}.${process.pid}.`)),s=o.join(n,o.basename(e.executablePath));try{if(q(e.executablePath))return;void 0===e.sourceText||i.existsSync(e.sourcePath)||(i.mkdirSync(o.dirname(e.sourcePath),{recursive:!0}),i.writeFileSync(e.sourcePath,e.sourceText)),await p("xcrun",["swiftc",e.sourcePath,"-o",s],{timeoutMs:e.timeoutMs??12e4,env:z()}),i.renameSync(s,e.executablePath)}finally{i.rmSync(n,{recursive:!0,force:!0}),await a()}}async function j(e,t,r){if(q(t))return null;try{return await m({lockDirPath:e,owner:{pid:process.pid,startTime:h(process.pid),acquiredAtMs:Date.now()},timeoutMs:r,pollMs:25,ownerGraceMs:r,description:`Swift cache lock: ${e} (${r}ms)`})}catch(t){if(t instanceof d&&t.message.startsWith("Timed out waiting for Swift cache lock:"))throw new d("COMMAND_FAILED",t.message,{...t.details,lockDir:e,timeoutMs:r,hint:`Another agent-device process may still be compiling this Swift helper. Retry shortly; if no agent-device process is active, remove "${e}" and retry.`});throw t}}function q(e){try{return i.accessSync(e,i.constants.X_OK),i.statSync(e).isFile()}catch{return!1}}function W(e){return e.replaceAll(/[^A-Za-z0-9._-]/g,"-").replaceAll(/^-+|-+$/g,"")||"swift-helper"}function B(e){let t=n("sha256");for(let r of e)t.update(Buffer.isBuffer(r)?r:String(r)),t.update("\0");return t.digest("hex").slice(0,16)}let Q=`
|
|
2
2
|
import Foundation
|
|
3
3
|
import AVFoundation
|
|
4
4
|
|
|
@@ -22,5 +22,5 @@ Task {
|
|
|
22
22
|
|
|
23
23
|
semaphore.wait()
|
|
24
24
|
exit(exitCode)
|
|
25
|
-
`.trim();async function
|
|
26
|
-
${t}`;return/\b(no such module ['"]AVFoundation['"]|unable to find utility ["']swiftc?["']|xcrun: error: unable to find utility ["']swiftc?["'])\b/i.test(r)}function Z(e){try{let t=i.statSync(e);if(!t.isFile()||t.size<=0)return!1}catch{return!1}let t=function(e){try{let t=i.openSync(e,"r");try{let e=i.fstatSync(t).size,r=0,a=[];for(;r+8<=e&&a.length<16;){let e=Buffer.alloc(8);if(8>i.readSync(t,e,0,8,r))break;let o=e.readUInt32BE(0),n=e.toString("latin1",4,8);if(a.push(n),1===o){let e=Buffer.alloc(8);if(8>i.readSync(t,e,0,8,r+8))break;o=Number(e.readBigUInt64BE(0))}if(!Number.isFinite(o)||o<=0)break;r+=o}return a}finally{i.closeSync(t)}}catch{return[]}}(e);return t.includes("ftyp")&&t.includes("moov")}function Y(e){let t=o.parse(e);return o.join(t.dir,`${t.name}.gesture-telemetry.json`)}function ee(e){return[...e].sort((e,t)=>e.tMs-t.tMs)}function et(e){var t,r,a;let o,n,{recording:s,trimStartMs:c}=e,d=(o=Y((t={videoPath:s.outPath,events:s.gestureEvents,trimStartMs:c}).videoPath),n={version:1,generatedAt:new Date().toISOString(),events:(r=t.events,(a=t.trimStartMs??0)>0?ee(r.flatMap(e=>{let t=e.tMs-a,r="durationMs"in e?e.durationMs:void 0;return("number"==typeof r?t+r:t)<=0?[]:[{...e,tMs:Math.max(0,t)}]})):ee(r))},i.writeFileSync(o,JSON.stringify(n,null,2)),o);return s.telemetryPath=d,d}function er(e){let t=o.dirname(c(import.meta.url)),r=[c(new URL(`./${e}`,import.meta.url)),o.resolve(t,`../../apple-runner/AgentDeviceRunner/RecordingScripts/${e}`),o.resolve(t,`../../../apple-runner/AgentDeviceRunner/RecordingScripts/${e}`),o.resolve(process.cwd(),`apple-runner/AgentDeviceRunner/RecordingScripts/${e}`)];for(let e of r)if(i.existsSync(e))return e;throw new d("COMMAND_FAILED",`Missing recording helper script: ${e}`,{hint:"Ensure apple-runner/AgentDeviceRunner/RecordingScripts is present in this checkout or bundled with the package.",scriptName:e,searchedPaths:r})}async function ea(e){var t;let r,a,{videoPath:n,scriptPath:s,scriptArgs:c,commandDescription:u}=e;await W(n),await J(n);let p=(t=n,r=o.parse(t),a=`${process.pid}-${Date.now()}-${Math.random().toString(16).slice(2)}`,o.join(r.dir,`.${r.name}.agent-device-${a}${r.ext||".mp4"}`));try{let e=await L({sourcePath:s});await l(e,["--input",n,"--output",p,...c],{timeoutMs:12e4,env:T()}),await J(p),i.renameSync(p,n)}catch(t){let e=t instanceof d?t:new d("COMMAND_FAILED",String(t),void 0,t instanceof Error?t:void 0);throw new d("COMMAND_FAILED",u,{...e.details,videoPath:n,script:s},e)}finally{i.rmSync(p,{force:!0})}}async function ei(e){let{videoPath:t,trimStartMs:a}=e;a>0&&await ea({videoPath:t,scriptPath:r??=er("recording-trim.swift"),scriptArgs:["--trim-start-ms",String(a)],commandDescription:"Failed to trim the start of the iOS recording"})}async function eo(e){let{videoPath:r,telemetryPath:a,exportQuality:i="medium",targetLabel:o="recording"}=e;await ea({videoPath:r,scriptPath:t??=er("recording-overlay.swift"),scriptArgs:["--events",a,"--quality",i],commandDescription:`Failed to add touch overlays to the ${o}`})}async function en(e){let{videoPath:t,maxSize:r,exportQuality:i="medium",targetLabel:o="recording"}=e;await ea({videoPath:t,scriptPath:a??=er("recording-resize.swift"),scriptArgs:["--max-size",String(r),"--quality",i],commandDescription:`Failed to resize the ${o}`})}function es(e){return e instanceof Error?e.message:String(e)}function ec(e,t){return e.stderr.trim()||e.stdout.trim()||`${t} exited with code ${e.exitCode}`}function ed(e,t,r=Date.now()){let a=Math.max(0,r-t.startedAt);return a>=1e3?{message:e,tooShort:!1}:{message:`${e}. Recording stopped after ${Math.round(a)}ms; wait at least 1000ms between record start and record stop so the recorder can finalize a playable MP4`,tooShort:!0}}async function el(e){let{recording:t,deps:r,trimStartMs:a,targetLabel:i}=e,o=et({recording:t,trimStartMs:a});if(!t.showTouches)return void f({level:"debug",phase:"record_stop_overlay_skipped",data:{reason:"hide_touches"}});if(0===t.gestureEvents.length)return void f({level:"debug",phase:"record_stop_overlay_skipped",data:{reason:"no_gesture_events"}});let n=function(e=process.platform){if("darwin"!==e)return"touch overlay burn-in is only available on macOS hosts; returning raw video plus gesture telemetry"}();if(n){t.overlayWarning??=n;return}try{await h("record_stop_overlay_export",()=>r.overlayRecordingTouches({videoPath:t.outPath,telemetryPath:o,exportQuality:t.exportQuality,targetLabel:i}),{targetLabel:i,gestureEventCount:t.gestureEvents.length})}catch(e){t.overlayWarning??=`failed to overlay recording touches: ${es(e)}`}}function eu(e,t){if(1===t)return e;let r=o.parse(e),a=r.ext||".mp4";return o.join(r.dir,`${r.name}.part-${String(t).padStart(3,"0")}${a}`)}function ep(e){return e.chunks??=[{index:1,path:e.outPath,remotePath:e.remotePath}],e.chunks}async function em(e){let{recording:t,startNextChunk:r,finishCurrentChunk:a}=e;if(t.stopping)return;let i=await a();if(i)throw Error(i);if(t.stopping)return;let n=ep(t),s=n.length+1,c=await r(o.posix.dirname(t.remotePath));t.remotePath=c.remotePath,t.remotePid=c.remotePid,n.push({index:s,path:eu(t.outPath,s),remotePath:c.remotePath}),t.warning??="Android adb screenrecord is capped at 180s, so this recording was split into multiple MP4 chunks."}async function eh(e){let{recording:t,deps:r}=e;ep(t).length<=1?await el({recording:t,deps:r,targetLabel:"Android recording"}):(et({recording:t}),t.showTouches&&t.gestureEvents.length>0&&(t.overlayWarning??="touch overlay burn-in is skipped for chunked Android recordings; returning raw chunks plus gesture telemetry"))}async function ef(e){for(let t of e.chunks){let r=await ew({deps:e.deps,deviceId:e.deviceId,remotePath:t.remotePath,outPath:t.path});if(r)return`failed to copy recording chunk ${t.index}: ${r}`}}async function ew(e){let t,{deps:r,deviceId:a,remotePath:o,outPath:n}=e;for(let e=0;e<2;e+=1){eg(n);let s=w(a),c=await y(o,n,{allowFailure:!0,device:s});if(0!==c.exitCode)t=ec(c,"adb pull");else{await r.waitForStableFile(n,{pollMs:250,attempts:20});let t=await r.isPlayableVideo(n);if(f({level:"debug",phase:"record_stop_android_pull_validation",data:{deviceId:a,remotePath:o,outPath:n,attempt:e+1,fileSize:function(e){try{return i.statSync(e).size}catch{return 0}}(n),playable:t}}),t)return;f({level:"warn",phase:"record_stop_android_invalid_video_retry",data:{deviceId:a,remotePath:o,outPath:n,attempt:e+1}})}e<1&&await m(750)}return t?`failed to copy recording from device: ${t}`:(eg(n),"failed to copy recording from device: pulled file is not a playable MP4")}function eg(e){try{i.rmSync(e,{force:!0})}catch{}}let ey={medium:8e6,high:2e7};async function e_(e,t,r){return await g(w(e),t,r)}async function ev(e,t){let r=await e_(e,["shell","ps","-o","pid=","-p",t],{allowFailure:!0});return 0===r.exitCode&&r.stdout.split(/\s+/).map(e=>e.trim()).includes(t)}async function eP(e,t){for(let r=0;r<40;r+=1){if(!await ev(e,t))return!0;await m(250)}return!await ev(e,t)}async function eS(e,t){let r,a=0;for(let i=0;i<20;i+=1){let i=await e_(e,["shell","stat","-c","%s",t],{allowFailure:!0}),o=0===i.exitCode?i.stdout.trim():"";if(o.length>0&&o===r){if((a+=1)>=4)return}else a=0;r=o,await m(250)}}async function eA(e,t,r){for(let a=0;a<8;a+=1){let i=await e_(e,["shell","stat","-c","%s",t],{allowFailure:!0}),o=0===i.exitCode?Number(i.stdout.trim()):NaN;if(Number.isFinite(o)&&o>0)return!0;if(!await ev(e,r))break;if(a+1>=2)return!0;await m(250)}return!1}async function eb(e){let{deviceId:t,maxSize:r}=e;if(void 0===r)return;let a=await e_(t,["shell","wm","size"],{allowFailure:!0}),i=a.stdout.match(/Override size:\s*(\d+)x(\d+)/)??a.stdout.match(/Physical size:\s*(\d+)x(\d+)/);if(0!==a.exitCode||!i)throw Error(`failed to resolve Android screen size for recording max-size: ${ec(a,"adb shell wm size")}`);var o={width:Number(i[1]),height:Number(i[2]),maxSize:r};let n=Math.max(o.width,o.height);if(n<=o.maxSize)return{width:o.width,height:o.height};let s=o.maxSize/n;return{width:eM(o.width,s),height:eM(o.height,s)}}function eM(e,t){return Math.max(2,2*Math.round(e*t/2))}async function eC(e,t){await e_(e,["shell","rm","-f",t],{allowFailure:!0})}async function ex(e,t){let r=await e_(e,["shell","kill","-9",t],{allowFailure:!0});return f({level:"warn",phase:"record_stop_android_force_signal",data:{deviceId:e,remotePid:t,exitCode:r.exitCode,stdout:r.stdout.trim(),stderr:r.stderr.trim()}}),!(0!==r.exitCode&&await ev(e,t))&&await eP(e,t)}async function eR(e){var t;let r,a,{device:i,recordingSize:o,quality:n,preferredRemoteDir:s}=e,c="failed to start recording: Android screenrecord did not begin producing frames";for(let e of(t=Date.now(),r=`agent-device-recording-${t}.mp4`,a=["/sdcard","/data/local/tmp"],(s&&a.includes(s)?[s,...a.filter(e=>e!==s)]:a).map(e=>`${e}/${r}`))){let t=await e_(i.id,["shell",function(e,t,r){let a=["screenrecord"];return t&&a.push("--size",`${t.width}x${t.height}`),a.push("--bit-rate",String(ey[r])),a.push(e),`${a.join(" ")} >/dev/null 2>&1 & echo $!`}(e,o,n)],{allowFailure:!0});if(0!==t.exitCode){c=`failed to start recording: ${ec(t,"adb shell screenrecord")}`;continue}let r=t.stdout.split(/\r?\n/).map(e=>e.trim()).filter(e=>/^\d+$/.test(e)).at(-1);if(!r){c="failed to start recording: adb did not return a valid Android screenrecord pid",await eC(i.id,e);continue}if(f({level:"debug",phase:"record_start_android_started",data:{deviceId:i.id,remotePath:e,remotePid:r}}),await eA(i.id,e,r))return{remotePath:e,remotePid:r,startedAt:Date.now()};c="failed to start recording: Android screenrecord did not begin producing frames",await ex(i.id,r),await eC(i.id,e)}return{error:A("COMMAND_FAILED",c)}}async function eI(e){let t,{device:r,recordingBase:a}=e;try{t=await eb({deviceId:r.id,maxSize:a.maxSize})}catch(e){return A("COMMAND_FAILED",e instanceof Error?e.message:String(e))}let i=a.exportQuality??"medium",o=await eR({device:r,recordingSize:t,quality:i});if("error"in o)return o.error;let n={platform:"android",remotePath:o.remotePath,remotePid:o.remotePid,chunks:[{index:1,path:a.outPath,remotePath:o.remotePath}],...a,startedAt:o.startedAt};return!function e(t){let{recording:r,startNextChunk:a,finishCurrentChunk:i}=t,o=setTimeout(()=>{r.rotationPromise=em({recording:r,startNextChunk:a,finishCurrentChunk:i}).catch(e=>{r.rotationFailedReason=e instanceof Error?e.message:String(e)}).finally(()=>{r.rotationPromise=void 0,r.stopping||r.rotationFailedReason||e({recording:r,startNextChunk:a,finishCurrentChunk:i})})},17e4);o.unref?.(),r.rotationTimer=o}({recording:n,finishCurrentChunk:async()=>await eD({device:r,recording:n,waitForRemoteFileStability:!1}),startNextChunk:async e=>{let a=await eR({device:r,recordingSize:t,quality:i,preferredRemoteDir:e});if("error"in a)throw Error(a.error.ok?"failed to start next Android recording chunk":a.error.error.message);return a}}),n}async function eD(e){let{device:t,recording:r,waitForRemoteFileStability:a=!0}=e;await ev(t.id,r.remotePid)||(r.warning??=function(e){if(!(Date.now()-e.startedAt<178e3))return"Android adb screenrecord stopped before record stop, likely after reaching the 180s platform limit. The MP4 may be truncated; final interactions after the limit are not in the video."}(r));let i=await e_(t.id,["shell","kill","-2",r.remotePid],{allowFailure:!0});if(f({level:"debug",phase:"record_stop_android_signal",data:{deviceId:t.id,remotePath:r.remotePath,remotePid:r.remotePid,exitCode:i.exitCode,stdout:i.stdout.trim(),stderr:i.stderr.trim()}}),0!==i.exitCode)return await eN(t.id,r.remotePid,i);let o=await eE(t.id,r.remotePid);if(o)return o;a&&await eS(t.id,r.remotePath)}async function eN(e,t,r){if(await ev(e,t)&&!await ex(e,t))return`failed to stop recording: ${ec(r,"adb shell kill")}`}async function eE(e,t){if(!await eP(e,t)&&!await ex(e,t))return`failed to stop recording: Android screenrecord pid ${t} did not exit`}async function e$(e){let t,{deps:r,device:a,recording:i,stopRequestedAt:o}=e;f({level:"debug",phase:"record_stop_android_enter",data:{deviceId:a.id,remotePath:i.remotePath,remotePid:i.remotePid}}),i.stopping=!0,i.rotationTimer&&(clearTimeout(i.rotationTimer),i.rotationTimer=void 0),await i.rotationPromise;let n=await eD({device:a,recording:i});if(i.rotationFailedReason&&!n&&(i.warning??=`Android recording chunk rotation failed: ${i.rotationFailedReason}`),!n){let e=await ef({deps:r,deviceId:a.id,chunks:ep(i)});if(e)return await s(),A("COMMAND_FAILED",eO(e,i,o));await eh({recording:i,deps:r})}if(await s(),n)return A("COMMAND_FAILED",eO(n,i,o));if(t)return A("COMMAND_FAILED",t);return null;async function s(){for(let e of ep(i)){let r=await e_(a.id,["shell","rm","-f",e.remotePath],{allowFailure:!0});f({level:"debug",phase:"record_stop_android_cleanup",data:{deviceId:a.id,remotePath:e.remotePath,exitCode:r.exitCode,stdout:r.stdout.trim(),stderr:r.stderr.trim()}}),0===r.exitCode||n||(t=`failed to clean up remote recording: ${ec(r,"adb shell rm")}`)}}}function eO(e,t,r){return ed(e,t,r).message}function ek(e){let t=e.appBundleId?.trim();return t&&t.length>0?t:void 0}function eF(e,t,r){return R({req:e,logPath:t,traceLogPath:r.trace?.outPath})}async function eT(e){let{req:t,activeSession:r,device:a,logPath:i,deps:o}=e,n=ek(r);try{await o.runAppleRunnerCommand(a,{command:"recordStop",appBundleId:n},eF(t,i,r))}catch(e){f({level:"warn",phase:"record_stop_runner_failed",data:{platform:a.platform,kind:a.kind,deviceId:a.id,session:r.name,error:es(e)}})}}async function eL(e){let{req:t,activeSession:r,device:a,logPath:i,deps:o}=e,n=ek(r);if(n)try{let e=await o.runAppleRunnerCommand(a,{command:"snapshot",appBundleId:n,interactiveOnly:!0,depth:1},eF(t,i,r)),s=Date.now();if("number"==typeof e.currentUptimeMs&&Number.isFinite(e.currentUptimeMs)&&e.currentUptimeMs>0)return f({level:"debug",phase:"record_start_gesture_clock_anchor",data:{source:"warm_snapshot"}}),{gestureClockOriginAtMs:s,gestureClockOriginUptimeMs:e.currentUptimeMs};return}catch(e){f({level:"warn",phase:"record_start_simulator_runner_warm_failed",data:{deviceId:a.id,session:r.name,appBundleId:n,error:es(e)}});return}}async function eU(e){let t,r,{req:a,activeSession:i,sessionStore:o,device:n,logPath:s,deps:c,fpsFlag:d,recordingBase:l,appBundleId:u}=e,p=`agent-device-recording-${Date.now()}.mp4`,m=`tmp/${p}`,h=eF(a,s,i),w=async()=>c.runAppleRunnerCommand(n,{command:"recordStart",outPath:p,fps:d,maxSize:l.maxSize,appBundleId:u},h);try{let e=await w();t="number"==typeof e.recorderStartUptimeMs?e.recorderStartUptimeMs:void 0,r="number"==typeof e.targetAppReadyUptimeMs?e.targetAppReadyUptimeMs:void 0}catch(a){var g,y;if(!es(a).toLowerCase().includes("recording already in progress"))return A("COMMAND_FAILED",`failed to start recording: ${es(a)}`);f({level:"warn",phase:"record_start_runner_desynced",data:{platform:n.platform,kind:n.kind,deviceId:n.id,session:i.name,error:es(a)}});let e=(g=n.id,y=i.name,o.toArray().find(e=>e.name!==y&&"ios"===e.device.platform&&"device"===e.device.kind&&e.device.id===g&&e.recording?.platform==="ios-device-runner"));if(e)return A("COMMAND_FAILED",`failed to start recording: recording already in progress in session '${e.name}'`);try{await c.runAppleRunnerCommand(n,{command:"recordStop",appBundleId:u},h)}catch{}try{let e=await w();t="number"==typeof e.recorderStartUptimeMs?e.recorderStartUptimeMs:void 0,r="number"==typeof e.targetAppReadyUptimeMs?e.targetAppReadyUptimeMs:void 0}catch(e){return A("COMMAND_FAILED",`failed to start recording: ${es(e)}`)}}return{platform:"ios-device-runner",remotePath:m,runnerStartedAtUptimeMs:t,targetAppReadyUptimeMs:r,...l}}async function ez(e){let{req:t,activeSession:r,device:a,logPath:i,deps:o,fpsFlag:n,recordingBase:s,appBundleId:c}=e;try{await o.runAppleRunnerCommand(a,{command:"recordStart",outPath:s.outPath,fps:n,maxSize:s.maxSize,appBundleId:c},eF(t,i,r))}catch(e){return A("COMMAND_FAILED",`failed to start recording: ${es(e)}`)}return{platform:"macos-runner",...s}}async function eK(e){let{req:t,activeSession:r,device:a,logPath:i,deps:o,recording:n}=e;await eT({req:t,activeSession:r,device:a,logPath:i,deps:o});let s={stdout:"",stderr:"",exitCode:1};for(let e of P)if(0===(s=await o.runCmd("xcrun",["devicectl","device","copy","from","--device",a.id,"--source",n.remotePath,"--destination",n.outPath,"--domain-type","appDataContainer","--domain-identifier",e],{allowFailure:!0})).exitCode)break;if(0!==s.exitCode){let e=s.stderr.trim()||s.stdout.trim()||`devicectl exited with code ${s.exitCode}`;return A("COMMAND_FAILED",`failed to copy recording from device: ${e}`)}let c="number"!=typeof n.runnerStartedAtUptimeMs||"number"!=typeof n.targetAppReadyUptimeMs?0:Math.max(0,n.targetAppReadyUptimeMs-n.runnerStartedAtUptimeMs);return c>0&&await o.trimRecordingStart({videoPath:n.outPath,trimStartMs:c}),await el({recording:n,deps:o,trimStartMs:c,targetLabel:"iOS recording"}),null}async function eG(e){let{req:t,activeSession:r,device:a,logPath:i,deps:o,recording:n}=e;return await eT({req:t,activeSession:r,device:a,logPath:i,deps:o}),await el({recording:n,deps:o,targetLabel:"macOS recording"}),null}async function eV(e){let{deps:t,recording:r}=e;r.child.kill("SIGINT");let a=await eH(r.wait,5e3);return a||(await ej(t,r,"SIGINT"),(a=await eH(r.wait,2e3))||(r.child.kill("SIGTERM"),await ej(t,r,"SIGTERM"),a=await eH(r.wait,2e3)))?a:(r.child.kill("SIGKILL"),await ej(t,r,"SIGKILL"),await eH(r.wait,2e3))}async function eH(e,t){return await Promise.race([e,m(t).then(()=>null)])}async function ej(e,t,r){await eW(e,t,r)||await eq(e,t.outPath,r)}async function eq(e,t,r){let a,i=`simctl.*recordVideo.*${t.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}`;try{a=await e.runCmd("pgrep",["-f",i],{allowFailure:!0})}catch(e){f({level:"warn",phase:"record_stop_ios_simulator_pgrep_failed",data:{outPath:t,signal:r,error:es(e)}});return}let o=eQ(eX(a.stdout)),n=eJ(o,r);f({level:n>0?"warn":"debug",phase:"record_stop_ios_simulator_signal_recorders",data:{outPath:t,signal:r,matchedPidCount:o.length,signaled:n,pgrepExitCode:a.exitCode}})}async function eW(e,t,r){let a=t.recorderPid??t.child.pid;if("number"!=typeof a||!Number.isInteger(a)||a<=0)return f({level:"debug",phase:"record_stop_ios_simulator_owned_recorder_unavailable",data:{outPath:t.outPath,signal:r,reason:"missing_recorder_pid"}}),!1;let i=await eB(e,a,t.outPath,r),o=eQ([a,...i.pids]),n=eJ(o,r);return f({level:n>0?"warn":"debug",phase:"record_stop_ios_simulator_signal_owned_recorder",data:{outPath:t.outPath,signal:r,recorderPid:a,childPidCount:i.pids.length,matchedPidCount:o.length,signaled:n,pgrepExitCode:i.exitCode}}),n>0}async function eB(e,t,r,a){let i;try{i=await e.runCmd("pgrep",["-P",String(t)],{allowFailure:!0})}catch(e){return f({level:"warn",phase:"record_stop_ios_simulator_owned_pgrep_failed",data:{outPath:r,signal:a,parentPid:t,error:es(e)}}),{pids:[]}}return{pids:eX(i.stdout),exitCode:i.exitCode}}function eQ(e){return Array.from(new Set(e)).filter(e=>Number.isInteger(e)&&e>0&&e!==process.pid)}function eJ(e,t){let r=0;for(let a of e)try{process.kill(a,t),r+=1}catch{}return r}function eX(e){return e.split(/\s+/).map(e=>Number(e)).filter(e=>Number.isInteger(e)&&e>0)}async function eZ(e){let t,r,{req:a,activeSession:i,device:o,logPath:n,deps:s,recordingBase:c,resolvedOut:d}=e,l=c.showTouches?await eL({req:a,activeSession:i,device:o,logPath:n,deps:s}):void 0,{child:u,wait:p}=s.startIosSimulatorRecording({device:o,outPath:d}),m=await e0(d);if(l)t=l.gestureClockOriginAtMs,r=l.gestureClockOriginUptimeMs;else if(c.showTouches)try{let e=Date.now(),c=await s.runAppleRunnerCommand(o,{command:"uptime",appBundleId:ek(i)},eF(a,n,i)),d=Date.now();t=Math.round((e+d)/2),r="number"==typeof c.currentUptimeMs?c.currentUptimeMs:void 0}catch{}return{platform:"ios",child:u,wait:p,...c,recorderPid:u.pid,startedAt:m,gestureClockOriginAtMs:void 0===r?void 0:t,gestureClockOriginUptimeMs:r}}async function eY(e){let{deps:t,recording:r,stopRequestedAt:a}=e;await h("record_stop_tail_settle",()=>t.waitForRecordingTail(r),{platform:r.platform,gestureEventCount:r.gestureEvents.length});let i=await h("record_stop_ios_simulator_process",()=>eV({deps:t,recording:r}),{outPath:r.outPath});if(!i)return e1("failed to stop recording: simctl recordVideo did not exit after 5000ms and forced cleanup",r,a);if(0!==i.exitCode)return e1(`failed to stop recording: ${ec(i,"simctl recordVideo")}`,r,a);if(await h("record_stop_video_stable",()=>t.waitForStableFile(r.outPath,{pollMs:150,attempts:12}),{outPath:r.outPath}),!await h("record_stop_video_playable_check",()=>t.isPlayableVideo(r.outPath),{outPath:r.outPath}))return e1(`failed to stop recording: ${r.outPath} was not finalized into a playable video`,r,a);if(void 0!==r.maxSize)try{await h("record_stop_resize",()=>t.resizeRecording({videoPath:r.outPath,maxSize:r.maxSize,exportQuality:r.exportQuality,targetLabel:"iOS recording"}),{outPath:r.outPath,maxSize:r.maxSize})}catch(e){r.overlayWarning=`failed to resize recording: ${es(e)}`}return await h("record_stop_finalize_overlay",()=>el({recording:r,deps:t,targetLabel:"iOS recording"}),{outPath:r.outPath,showTouches:r.showTouches,gestureEventCount:r.gestureEvents.length}),null}async function e0(e){for(let t=0;t<2;t+=1){try{if(i.statSync(e).size>0)return Date.now()}catch{}if(t+1>=2)break;await m(250)}return Date.now()}function e1(e,t,r){let a=ed(e,t,r);return function(e){try{i.rmSync(e,{force:!0})}catch{}}(t.outPath),A("COMMAND_FAILED",a.message)}function e2(e){return"web"===e.platform?e5:"android"===e.platform?e6:"macos"===e.platform?e3:"ios"===e.platform&&"device"===e.kind?e8:"ios"===e.platform?e7:e9}function e4({req:e}){return e.positionals?.[1]??D(void 0)}let e5={validateStart:e=>(function(e){let t,r=(t=[],e.flags?.fps!==void 0&&t.push("--fps"),e.flags?.quality!==void 0&&t.push("--quality"),e.flags?.screenshotMaxSize!==void 0&&t.push("--max-size"),e.flags?.hideTouches!==void 0&&t.push("--hide-touches"),t);return r.length>0?A("INVALID_ARGS",`web recordings do not support ${r.join(", ")}; agent-browser records WebM directly`):null})(e),resolveOutputPath:function({req:e}){let t=e.positionals?.[1];return void 0===t?D("web"):N(t,".webm")},start:async({activeSession:e,recordingBase:t,resolvedOut:r})=>{var a;let i=(a=r,".webm"!==o.extname(a).toLowerCase()?A("INVALID_ARGS","web recordings must use a .webm output path"):null);if(i)return i;if(e.recordOnlySession)return A("INVALID_ARGS","record on web requires an active browser session; run open <url> --platform web first");let n=E();return n.startRecording?(await n.startRecording(r),{...t,outPath:r,startedAt:Date.now(),platform:"web",showTouches:!1}):A("UNSUPPORTED_OPERATION","record is not supported by this web provider")},stop:async({recording:e})=>await te({recording:e}),cleanupRecordOnlySession:async()=>{try{await E().close()}catch{}}},e8={resolveOutputPath:e4,start:async({req:e,activeSession:t,sessionStore:r,device:a,logPath:i,deps:o,fpsFlag:n,recordingBase:s})=>{let c=ek(t);return c?await eU({req:e,activeSession:t,sessionStore:r,device:a,logPath:i,deps:o,fpsFlag:n,recordingBase:s,appBundleId:c}):A("INVALID_ARGS","record on physical iOS devices requires an active app session; run open <app> first")},stop:async({req:e,activeSession:t,device:r,logPath:a,deps:i,recording:o})=>await eK({req:e,activeSession:t,device:r,logPath:a,deps:i,recording:o})},e3={resolveOutputPath:e4,start:async({req:e,activeSession:t,device:r,logPath:a,deps:i,fpsFlag:o,recordingBase:n})=>{let s=ek(t);return s?await ez({req:e,activeSession:t,device:r,logPath:a,deps:i,fpsFlag:o,recordingBase:n,appBundleId:s}):A("INVALID_ARGS","record on macOS requires an active app session; run open <app> first")},stop:async({req:e,activeSession:t,device:r,logPath:a,deps:i,recording:o})=>await eG({req:e,activeSession:t,device:r,logPath:a,deps:i,recording:o})},e7={resolveOutputPath:e4,start:async({req:e,activeSession:t,device:r,logPath:a,deps:i,recordingBase:o,resolvedOut:n})=>await eZ({req:e,activeSession:t,device:r,logPath:a,deps:i,recordingBase:o,resolvedOut:n}),stop:async({deps:e,recording:t,stopRequestedAt:r})=>await eY({deps:e,recording:t,stopRequestedAt:r})},e6={resolveOutputPath:e4,start:async({device:e,recordingBase:t})=>await eI({device:e,recordingBase:t}),stop:async({deps:e,device:t,recording:r,stopRequestedAt:a})=>await e$({deps:e,device:t,recording:r,stopRequestedAt:a})},e9={resolveOutputPath:e4,start:async()=>A("UNSUPPORTED_OPERATION","record is not supported on this device"),stop:async()=>A("UNSUPPORTED_OPERATION","record is not supported on this device")};async function te(e){let{recording:t}=e,r=E();if(!r.stopRecording)return A("UNSUPPORTED_OPERATION","record is not supported by this web provider");if(await r.stopRecording(),!function(e){try{return i.statSync(e).size>0}catch{return!1}}(t.outPath)){var a=t.outPath;try{i.rmSync(a,{force:!0})}catch{}return A("COMMAND_FAILED",`failed to stop recording: ${t.outPath} was not finalized into a WebM video`)}return null}async function tt(e){"ios"!==e.platform||0!==e.gestureEvents.length&&await m(350)}async function tr(e){let t,{req:r,sessionName:a,sessionStore:n,activeSession:s,device:c,logPath:d,deps:l}=e;if(s.recording)return A("INVALID_ARGS","recording already in progress");let u=r.flags?.fps,p=r.flags?.quality,m=r.flags?.screenshotMaxSize,h=e2(c),f=h.validateStart?.(r)??null;if(f)return f;if(void 0!==u&&(!Number.isInteger(u)||u<1||u>120))return A("INVALID_ARGS","fps must be an integer between 1 and 120");if(void 0!==p&&void 0===O(p))return A("INVALID_ARGS",`quality must be one of: ${k.join(", ")} (legacy numeric values 5-10 are accepted)`);if(void 0!==m&&(!Number.isInteger(m)||m<1))return A("INVALID_ARGS","max-size must be a positive integer");let w=S("record",c);if(w)return w;let g=h.resolveOutputPath({req:r}),y=v.expandHome(g,r.meta?.cwd),_=(t=O(r.flags?.quality),{outPath:y,clientOutPath:r.meta?.clientArtifactPaths?.outPath,startedAt:Date.now(),maxSize:r.flags?.screenshotMaxSize,exportQuality:t??"medium",showTouches:r.flags?.hideTouches!==!0,gestureEvents:[]});i.mkdirSync(o.dirname(y),{recursive:!0}),i.rmSync(y,{force:!0});let P=await h.start({req:r,activeSession:s,sessionStore:n,device:c,logPath:d,deps:l,fpsFlag:u,recordingBase:_,resolvedOut:y});if("ok"in P)return P;s.recording=P,n.set(a,s);let b=n.ensureSessionDir(a);return F(n,s,r,r.command,{action:"start",showTouches:P.showTouches}),{ok:!0,data:{recording:"started",outPath:P.clientOutPath??g,sessionStateDir:b,showTouches:P.showTouches}}}async function ta(e){var t;let r,a,{req:i,activeSession:n,device:s,logPath:c,deps:d}=e;if(!n.recording)return A("INVALID_ARGS","no active recording");let l=n.recording,u=Date.now(),p=l.invalidatedReason;n.recording=void 0;let m=await function(e){let{recording:t}=e;switch(t.platform){case"android":return e6.stop({...e,recording:t});case"ios":return e7.stop({...e,recording:t});case"ios-device-runner":return e8.stop({...e,recording:t});case"macos-runner":return e3.stop({...e,recording:t});case"web":return e5.stop({...e,recording:t})}return t}({req:i,activeSession:n,device:s,logPath:c,deps:d,recording:l,stopRequestedAt:u});if(m)return m;if(p&&"ios"===l.platform&&l.showTouches)l.overlayWarning??=`overlay unavailable: ${p}`;else if(p)return A("COMMAND_FAILED",p);return r="android"===(t=l).platform?t.chunks:void 0,a=[{field:"outPath",path:t.outPath,localPath:t.clientOutPath,fileName:o.basename(t.clientOutPath??t.outPath)}],r&&r.length>1&&a.push(...r.slice(1).map(e=>({field:"chunkPath",path:e.path,localPath:ti(t,e.index),fileName:o.basename(ti(t,e.index)??e.path)}))),t.telemetryPath&&a.push({field:"telemetryPath",path:t.telemetryPath,localPath:function(e){if(e.clientOutPath)return Y(e.clientOutPath)}(t),fileName:o.basename(t.telemetryPath)}),{ok:!0,data:{recording:"stopped",outPath:t.outPath,telemetryPath:t.telemetryPath,artifacts:a,showTouches:t.showTouches,warning:t.warning,overlayWarning:t.overlayWarning,chunks:r?.map(e=>({index:e.index,path:ti(t,e.index)??e.path}))}}}function ti(e,t){if("android"===e.platform&&e.clientOutPath)return eu(e.clientOutPath,t)}async function to(e,t,r,a={}){if(!r.recordOnlySession)return;let i=e2(r.device);await i.cleanupRecordOnlySession?.(r),a.writeLog&&e.writeSessionLog(r),e.delete(t)}async function tn(e){let{req:t,sessionName:r,sessionStore:a,logPath:i}=e,o={runCmd:async(e,t,r)=>"xcrun"===e?await _(t,r):await l(e,t,r),startIosSimulatorRecording:e=>$().startIosSimulatorRecording(e),runAppleRunnerCommand:x,waitForRecordingTail:tt,waitForStableFile:W,isPlayableVideo:B,trimRecordingStart:ei,resizeRecording:en,overlayRecordingTouches:eo},n=a.get(r),s=n?.device??await C(t.flags??{});n||await I(s);let c=n??{name:M(t),sessionScope:b(t),device:s,createdAt:Date.now(),recordOnlySession:!0,actions:[]},d=(t.positionals?.[0]??"").toLowerCase();if(!["start","stop"].includes(d))return A("INVALID_ARGS","record requires start|stop");if("start"===d)return tr({req:t,sessionName:r,sessionStore:a,activeSession:c,device:s,logPath:i,deps:o});let u=await ta({req:t,activeSession:c,device:s,logPath:i,deps:o});return u.ok?(F(a,c,t,t.command,{action:"stop",outPath:u.data?.outPath,showTouches:u.data?.showTouches}),await to(a,r,c,{writeLog:!0})):await to(a,r,c),u}export{tn as handleRecordCommand};
|
|
25
|
+
`.trim();async function J(e,t={}){let r,a=t.pollMs??150,o=t.attempts??12,n=0;for(let t=0;t<o;t+=1){let t=0;try{t=i.statSync(e).size}catch{t=0}if(t>0&&t===r){if((n+=1)>=2)return}else n=0;r=t,await f(a)}}async function X(e){try{let t=await Z(),r=await p(t,[e],{allowFailure:!0,timeoutMs:1e4,env:z()});if(0===r.exitCode)return!0;if(ee(r.stderr,r.stdout))return et(e);return!1}catch(r){var t;if((t=r)instanceof d&&("TOOL_MISSING"===t.code||ee(String(t.details?.stderr??""),String(t.details?.stdout??""))))return et(e);throw r}}async function Z(){e??=G({source:Q,cacheName:"video-validator",timeoutMs:3e4});try{return await e}catch(t){throw e=void 0,t}}async function Y(e,t={}){let r=t.pollMs??150,a=t.attempts??12;for(let t=0;t<a;t+=1){if(await X(e))return;await f(r)}}function ee(e,t){let r=`${e}
|
|
26
|
+
${t}`;return/\b(no such module ['"]AVFoundation['"]|unable to find utility ["']swiftc?["']|xcrun: error: unable to find utility ["']swiftc?["'])\b/i.test(r)}function et(e){try{let t=i.statSync(e);if(!t.isFile()||t.size<=0)return!1}catch{return!1}let t=function(e){try{let t=i.openSync(e,"r");try{let e=i.fstatSync(t).size,r=0,a=[];for(;r+8<=e&&a.length<16;){let e=Buffer.alloc(8);if(8>i.readSync(t,e,0,8,r))break;let o=e.readUInt32BE(0),n=e.toString("latin1",4,8);if(a.push(n),1===o){let e=Buffer.alloc(8);if(8>i.readSync(t,e,0,8,r+8))break;o=Number(e.readBigUInt64BE(0))}if(!Number.isFinite(o)||o<=0)break;r+=o}return a}finally{i.closeSync(t)}}catch{return[]}}(e);return t.includes("ftyp")&&t.includes("moov")}function er(e){let t=o.parse(e);return o.join(t.dir,`${t.name}.gesture-telemetry.json`)}function ea(e){return[...e].sort((e,t)=>e.tMs-t.tMs)}function ei(e){var t,r,a;let o,n,{recording:s,trimStartMs:c}=e,d=(o=er((t={videoPath:s.outPath,events:s.gestureEvents,trimStartMs:c}).videoPath),n={version:1,generatedAt:new Date().toISOString(),events:(r=t.events,(a=t.trimStartMs??0)>0?ea(r.flatMap(e=>{let t=e.tMs-a,r="durationMs"in e?e.durationMs:void 0;return("number"==typeof r?t+r:t)<=0?[]:[{...e,tMs:Math.max(0,t)}]})):ea(r))},i.writeFileSync(o,JSON.stringify(n,null,2)),o);return s.telemetryPath=d,d}function eo(e){let t=o.dirname(c(import.meta.url)),r=[c(new URL(`./${e}`,import.meta.url)),o.resolve(t,`../../apple-runner/AgentDeviceRunner/RecordingScripts/${e}`),o.resolve(t,`../../../apple-runner/AgentDeviceRunner/RecordingScripts/${e}`),o.resolve(process.cwd(),`apple-runner/AgentDeviceRunner/RecordingScripts/${e}`)];for(let e of r)if(i.existsSync(e))return e;throw new d("COMMAND_FAILED",`Missing recording helper script: ${e}`,{hint:"Ensure apple-runner/AgentDeviceRunner/RecordingScripts is present in this checkout or bundled with the package.",scriptName:e,searchedPaths:r})}async function en(e){var t;let r,a,{videoPath:n,scriptPath:s,scriptArgs:c,commandDescription:u}=e;await J(n),await Y(n);let l=(t=n,r=o.parse(t),a=`${process.pid}-${Date.now()}-${Math.random().toString(16).slice(2)}`,o.join(r.dir,`.${r.name}.agent-device-${a}${r.ext||".mp4"}`));try{let e=await K({sourcePath:s});await p(e,["--input",n,"--output",l,...c],{timeoutMs:12e4,env:z()}),await Y(l),i.renameSync(l,n)}catch(t){let e=t instanceof d?t:new d("COMMAND_FAILED",String(t),void 0,t instanceof Error?t:void 0);throw new d("COMMAND_FAILED",u,{...e.details,videoPath:n,script:s},e)}finally{i.rmSync(l,{force:!0})}}async function es(e){let{videoPath:t,trimStartMs:a}=e;a>0&&await en({videoPath:t,scriptPath:r??=eo("recording-trim.swift"),scriptArgs:["--trim-start-ms",String(a)],commandDescription:"Failed to trim the start of the iOS recording"})}async function ec(e){let{videoPath:r,telemetryPath:a,exportQuality:i="medium",targetLabel:o="recording"}=e;await en({videoPath:r,scriptPath:t??=eo("recording-overlay.swift"),scriptArgs:["--events",a,"--quality",i],commandDescription:`Failed to add touch overlays to the ${o}`})}async function ed(e){let{videoPath:t,maxSize:r,exportQuality:i="medium",targetLabel:o="recording"}=e;await en({videoPath:t,scriptPath:a??=eo("recording-resize.swift"),scriptArgs:["--max-size",String(r),"--quality",i],commandDescription:`Failed to resize the ${o}`})}function eu(e){return e instanceof Error?e.message:String(e)}function el(e,t){return e.stderr.trim()||e.stdout.trim()||`${t} exited with code ${e.exitCode}`}function ep(e,t,r=Date.now()){let a=Math.max(0,r-t.startedAt);return a>=1e3?{message:e,tooShort:!1}:{message:`${e}. Recording stopped after ${Math.round(a)}ms; wait at least 1000ms between record start and record stop so the recorder can finalize a playable MP4`,tooShort:!0}}async function em(e){let{recording:t,deps:r,trimStartMs:a,targetLabel:i}=e,o=ei({recording:t,trimStartMs:a});if(!t.showTouches)return void l({level:"debug",phase:"record_stop_overlay_skipped",data:{reason:"hide_touches"}});if(0===t.gestureEvents.length)return void l({level:"debug",phase:"record_stop_overlay_skipped",data:{reason:"no_gesture_events"}});let n=function(e=process.platform){if("darwin"!==e)return"touch overlay burn-in is only available on macOS hosts; returning raw video plus gesture telemetry"}();if(n){t.overlayWarning??=n;return}try{await u("record_stop_overlay_export",()=>r.overlayRecordingTouches({videoPath:t.outPath,telemetryPath:o,exportQuality:t.exportQuality,targetLabel:i}),{targetLabel:i,gestureEventCount:t.gestureEvents.length})}catch(e){t.overlayWarning??=`failed to overlay recording touches: ${eu(e)}`}}function eh(e,t){if(1===t)return e;let r=o.parse(e),a=r.ext||".mp4";return o.join(r.dir,`${r.name}.part-${String(t).padStart(3,"0")}${a}`)}function ef(e){return e.chunks??=[{index:1,path:e.outPath,remotePath:e.remotePath}],e.chunks}async function ew(e){let{recording:t,startNextChunk:r,finishCurrentChunk:a}=e;if(t.stopping)return;let i=await a();if(i)throw Error(i);if(t.stopping)return;let n=ef(t),s=n.length+1,c=await r(o.posix.dirname(t.remotePath));t.remotePath=c.remotePath,t.remotePid=c.remotePid,n.push({index:s,path:eh(t.outPath,s),remotePath:c.remotePath}),t.warning??="Android adb screenrecord is capped at 180s, so this recording was split into multiple MP4 chunks."}async function eg(e){let{recording:t,deps:r}=e;ef(t).length<=1?await em({recording:t,deps:r,targetLabel:"Android recording"}):(ei({recording:t}),t.showTouches&&t.gestureEvents.length>0&&(t.overlayWarning??="touch overlay burn-in is skipped for chunked Android recordings; returning raw chunks plus gesture telemetry"))}async function ey(e){for(let t of e.chunks){let r=await e_({deps:e.deps,deviceId:e.deviceId,remotePath:t.remotePath,outPath:t.path});if(r)return`failed to copy recording chunk ${t.index}: ${r}`}}async function e_(e){let t,{deps:r,deviceId:a,remotePath:o,outPath:n}=e;for(let e=0;e<2;e+=1){ev(n);let s=w(a),c=await y(o,n,{allowFailure:!0,device:s});if(0!==c.exitCode)t=el(c,"adb pull");else{await r.waitForStableFile(n,{pollMs:250,attempts:20});let t=await r.isPlayableVideo(n);if(l({level:"debug",phase:"record_stop_android_pull_validation",data:{deviceId:a,remotePath:o,outPath:n,attempt:e+1,fileSize:function(e){try{return i.statSync(e).size}catch{return 0}}(n),playable:t}}),t)return;l({level:"warn",phase:"record_stop_android_invalid_video_retry",data:{deviceId:a,remotePath:o,outPath:n,attempt:e+1}})}e<1&&await f(750)}return t?`failed to copy recording from device: ${t}`:(ev(n),"failed to copy recording from device: pulled file is not a playable MP4")}function ev(e){try{i.rmSync(e,{force:!0})}catch{}}let eP={medium:8e6,high:2e7};async function eS(e,t,r){return await g(w(e),t,r)}async function eA(e,t){let r=await eS(e,["shell","ps","-o","pid=","-p",t],{allowFailure:!0});return 0===r.exitCode&&r.stdout.split(/\s+/).map(e=>e.trim()).includes(t)}async function eb(e,t){for(let r=0;r<40;r+=1){if(!await eA(e,t))return!0;await f(250)}return!await eA(e,t)}async function eM(e,t){let r,a=0;for(let i=0;i<20;i+=1){let i=await eS(e,["shell","stat","-c","%s",t],{allowFailure:!0}),o=0===i.exitCode?i.stdout.trim():"";if(o.length>0&&o===r){if((a+=1)>=4)return}else a=0;r=o,await f(250)}}async function eC(e,t,r){for(let a=0;a<8;a+=1){let i=await eS(e,["shell","stat","-c","%s",t],{allowFailure:!0}),o=0===i.exitCode?Number(i.stdout.trim()):NaN;if(Number.isFinite(o)&&o>0)return!0;if(!await eA(e,r))break;if(a+1>=2)return!0;await f(250)}return!1}async function ex(e){let{deviceId:t,maxSize:r}=e;if(void 0===r)return;let a=await eS(t,["shell","wm","size"],{allowFailure:!0}),i=a.stdout.match(/Override size:\s*(\d+)x(\d+)/)??a.stdout.match(/Physical size:\s*(\d+)x(\d+)/);if(0!==a.exitCode||!i)throw Error(`failed to resolve Android screen size for recording max-size: ${el(a,"adb shell wm size")}`);var o={width:Number(i[1]),height:Number(i[2]),maxSize:r};let n=Math.max(o.width,o.height);if(n<=o.maxSize)return{width:o.width,height:o.height};let s=o.maxSize/n;return{width:eR(o.width,s),height:eR(o.height,s)}}function eR(e,t){return Math.max(2,2*Math.round(e*t/2))}async function eI(e,t){await eS(e,["shell","rm","-f",t],{allowFailure:!0})}async function eD(e,t){let r=await eS(e,["shell","kill","-9",t],{allowFailure:!0});return l({level:"warn",phase:"record_stop_android_force_signal",data:{deviceId:e,remotePid:t,exitCode:r.exitCode,stdout:r.stdout.trim(),stderr:r.stderr.trim()}}),!(0!==r.exitCode&&await eA(e,t))&&await eb(e,t)}async function eN(e){var t;let r,a,{device:i,recordingSize:o,quality:n,preferredRemoteDir:s}=e,c="failed to start recording: Android screenrecord did not begin producing frames";for(let e of(t=Date.now(),r=`agent-device-recording-${t}.mp4`,a=["/sdcard","/data/local/tmp"],(s&&a.includes(s)?[s,...a.filter(e=>e!==s)]:a).map(e=>`${e}/${r}`))){let t=await eS(i.id,["shell",function(e,t,r){let a=["screenrecord"];return t&&a.push("--size",`${t.width}x${t.height}`),a.push("--bit-rate",String(eP[r])),a.push(e),`${a.join(" ")} >/dev/null 2>&1 & echo $!`}(e,o,n)],{allowFailure:!0});if(0!==t.exitCode){c=`failed to start recording: ${el(t,"adb shell screenrecord")}`;continue}let r=t.stdout.split(/\r?\n/).map(e=>e.trim()).filter(e=>/^\d+$/.test(e)).at(-1);if(!r){c="failed to start recording: adb did not return a valid Android screenrecord pid",await eI(i.id,e);continue}if(l({level:"debug",phase:"record_start_android_started",data:{deviceId:i.id,remotePath:e,remotePid:r}}),await eC(i.id,e,r))return{remotePath:e,remotePid:r,startedAt:Date.now()};c="failed to start recording: Android screenrecord did not begin producing frames",await eD(i.id,r),await eI(i.id,e)}return{error:b("COMMAND_FAILED",c)}}async function eE(e){let t,{device:r,recordingBase:a}=e;try{t=await ex({deviceId:r.id,maxSize:a.maxSize})}catch(e){return b("COMMAND_FAILED",e instanceof Error?e.message:String(e))}let i=a.exportQuality??"medium",o=await eN({device:r,recordingSize:t,quality:i});if("error"in o)return o.error;let n={platform:"android",remotePath:o.remotePath,remotePid:o.remotePid,chunks:[{index:1,path:a.outPath,remotePath:o.remotePath}],...a,startedAt:o.startedAt};return!function e(t){let{recording:r,startNextChunk:a,finishCurrentChunk:i}=t,o=setTimeout(()=>{r.rotationPromise=ew({recording:r,startNextChunk:a,finishCurrentChunk:i}).catch(e=>{r.rotationFailedReason=e instanceof Error?e.message:String(e)}).finally(()=>{r.rotationPromise=void 0,r.stopping||r.rotationFailedReason||e({recording:r,startNextChunk:a,finishCurrentChunk:i})})},17e4);o.unref?.(),r.rotationTimer=o}({recording:n,finishCurrentChunk:async()=>await e$({device:r,recording:n,waitForRemoteFileStability:!1}),startNextChunk:async e=>{let a=await eN({device:r,recordingSize:t,quality:i,preferredRemoteDir:e});if("error"in a)throw Error(a.error.ok?"failed to start next Android recording chunk":a.error.error.message);return a}}),n}async function e$(e){let{device:t,recording:r,waitForRemoteFileStability:a=!0}=e;await eA(t.id,r.remotePid)||(r.warning??=function(e){if(!(Date.now()-e.startedAt<178e3))return"Android adb screenrecord stopped before record stop, likely after reaching the 180s platform limit. The MP4 may be truncated; final interactions after the limit are not in the video."}(r));let i=await eS(t.id,["shell","kill","-2",r.remotePid],{allowFailure:!0});if(l({level:"debug",phase:"record_stop_android_signal",data:{deviceId:t.id,remotePath:r.remotePath,remotePid:r.remotePid,exitCode:i.exitCode,stdout:i.stdout.trim(),stderr:i.stderr.trim()}}),0!==i.exitCode)return await eO(t.id,r.remotePid,i);let o=await ek(t.id,r.remotePid);if(o)return o;a&&await eM(t.id,r.remotePath)}async function eO(e,t,r){if(await eA(e,t)&&!await eD(e,t))return`failed to stop recording: ${el(r,"adb shell kill")}`}async function ek(e,t){if(!await eb(e,t)&&!await eD(e,t))return`failed to stop recording: Android screenrecord pid ${t} did not exit`}async function eF(e){let t,{deps:r,device:a,recording:i,stopRequestedAt:o}=e;l({level:"debug",phase:"record_stop_android_enter",data:{deviceId:a.id,remotePath:i.remotePath,remotePid:i.remotePid}}),i.stopping=!0,i.rotationTimer&&(clearTimeout(i.rotationTimer),i.rotationTimer=void 0),await i.rotationPromise;let n=await e$({device:a,recording:i});if(i.rotationFailedReason&&!n&&(i.warning??=`Android recording chunk rotation failed: ${i.rotationFailedReason}`),!n){let e=await ey({deps:r,deviceId:a.id,chunks:ef(i)});if(e)return await s(),b("COMMAND_FAILED",eT(e,i,o));await eg({recording:i,deps:r})}if(await s(),n)return b("COMMAND_FAILED",eT(n,i,o));if(t)return b("COMMAND_FAILED",t);return null;async function s(){for(let e of ef(i)){let r=await eS(a.id,["shell","rm","-f",e.remotePath],{allowFailure:!0});l({level:"debug",phase:"record_stop_android_cleanup",data:{deviceId:a.id,remotePath:e.remotePath,exitCode:r.exitCode,stdout:r.stdout.trim(),stderr:r.stderr.trim()}}),0===r.exitCode||n||(t=`failed to clean up remote recording: ${el(r,"adb shell rm")}`)}}}function eT(e,t,r){return ep(e,t,r).message}function eL(e){let t=e.appBundleId?.trim();return t&&t.length>0?t:void 0}function eU(e,t,r){return N({req:e,logPath:t,traceLogPath:r.trace?.outPath})}async function ez(e){let{req:t,activeSession:r,device:a,logPath:i,deps:o}=e,n=eL(r);try{await o.runAppleRunnerCommand(a,{command:"recordStop",appBundleId:n},eU(t,i,r))}catch(e){l({level:"warn",phase:"record_stop_runner_failed",data:{platform:a.platform,kind:a.kind,deviceId:a.id,session:r.name,error:eu(e)}})}}async function eK(e){let{req:t,activeSession:r,device:a,logPath:i,deps:o}=e,n=eL(r);if(n)try{let e=await o.runAppleRunnerCommand(a,{command:"snapshot",appBundleId:n,interactiveOnly:!0,depth:1},eU(t,i,r)),s=Date.now();if("number"==typeof e.currentUptimeMs&&Number.isFinite(e.currentUptimeMs)&&e.currentUptimeMs>0)return l({level:"debug",phase:"record_start_gesture_clock_anchor",data:{source:"warm_snapshot"}}),{gestureClockOriginAtMs:s,gestureClockOriginUptimeMs:e.currentUptimeMs};return}catch(e){l({level:"warn",phase:"record_start_simulator_runner_warm_failed",data:{deviceId:a.id,session:r.name,appBundleId:n,error:eu(e)}});return}}async function eG(e){let t,r,{req:a,activeSession:i,sessionStore:o,device:n,logPath:s,deps:c,fpsFlag:d,recordingBase:u,appBundleId:p}=e,m=`agent-device-recording-${Date.now()}.mp4`,h=`tmp/${m}`,f=eU(a,s,i),w=async()=>c.runAppleRunnerCommand(n,{command:"recordStart",outPath:m,fps:d,maxSize:u.maxSize,appBundleId:p},f);try{let e=await w();t="number"==typeof e.recorderStartUptimeMs?e.recorderStartUptimeMs:void 0,r="number"==typeof e.targetAppReadyUptimeMs?e.targetAppReadyUptimeMs:void 0}catch(a){var g,y;if(!eu(a).toLowerCase().includes("recording already in progress"))return b("COMMAND_FAILED",`failed to start recording: ${eu(a)}`);l({level:"warn",phase:"record_start_runner_desynced",data:{platform:n.platform,kind:n.kind,deviceId:n.id,session:i.name,error:eu(a)}});let e=(g=n.id,y=i.name,o.toArray().find(e=>e.name!==y&&D(e.device)&&"device"===e.device.kind&&e.device.id===g&&e.recording?.platform==="ios-device-runner"));if(e)return b("COMMAND_FAILED",`failed to start recording: recording already in progress in session '${e.name}'`);try{await c.runAppleRunnerCommand(n,{command:"recordStop",appBundleId:p},f)}catch{}try{let e=await w();t="number"==typeof e.recorderStartUptimeMs?e.recorderStartUptimeMs:void 0,r="number"==typeof e.targetAppReadyUptimeMs?e.targetAppReadyUptimeMs:void 0}catch(e){return b("COMMAND_FAILED",`failed to start recording: ${eu(e)}`)}}return{platform:"ios-device-runner",remotePath:h,runnerStartedAtUptimeMs:t,targetAppReadyUptimeMs:r,...u}}async function eV(e){let{req:t,activeSession:r,device:a,logPath:i,deps:o,fpsFlag:n,recordingBase:s,appBundleId:c}=e;try{await o.runAppleRunnerCommand(a,{command:"recordStart",outPath:s.outPath,fps:n,maxSize:s.maxSize,appBundleId:c},eU(t,i,r))}catch(e){return b("COMMAND_FAILED",`failed to start recording: ${eu(e)}`)}return{platform:"macos-runner",...s}}async function eH(e){let{req:t,activeSession:r,device:a,logPath:i,deps:o,recording:n}=e;await ez({req:t,activeSession:r,device:a,logPath:i,deps:o});let s={stdout:"",stderr:"",exitCode:1};for(let e of S)if(0===(s=await o.runCmd("xcrun",["devicectl","device","copy","from","--device",a.id,"--source",n.remotePath,"--destination",n.outPath,"--domain-type","appDataContainer","--domain-identifier",e],{allowFailure:!0})).exitCode)break;if(0!==s.exitCode){let e=s.stderr.trim()||s.stdout.trim()||`devicectl exited with code ${s.exitCode}`;return b("COMMAND_FAILED",`failed to copy recording from device: ${e}`)}let c="number"!=typeof n.runnerStartedAtUptimeMs||"number"!=typeof n.targetAppReadyUptimeMs?0:Math.max(0,n.targetAppReadyUptimeMs-n.runnerStartedAtUptimeMs);return c>0&&await o.trimRecordingStart({videoPath:n.outPath,trimStartMs:c}),await em({recording:n,deps:o,trimStartMs:c,targetLabel:"iOS recording"}),null}async function ej(e){let{req:t,activeSession:r,device:a,logPath:i,deps:o,recording:n}=e;return await ez({req:t,activeSession:r,device:a,logPath:i,deps:o}),await em({recording:n,deps:o,targetLabel:"macOS recording"}),null}async function eq(e){let{deps:t,recording:r}=e;r.child.kill("SIGINT");let a=await eW(r.wait,5e3);return a||(await eB(t,r,"SIGINT"),(a=await eW(r.wait,2e3))||(r.child.kill("SIGTERM"),await eB(t,r,"SIGTERM"),a=await eW(r.wait,2e3)))?a:(r.child.kill("SIGKILL"),await eB(t,r,"SIGKILL"),await eW(r.wait,2e3))}async function eW(e,t){return await Promise.race([e,f(t).then(()=>null)])}async function eB(e,t,r){await eJ(e,t,r)||await eQ(e,t.outPath,r)}async function eQ(e,t,r){let a,i=`simctl.*recordVideo.*${t.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}`;try{a=await e.runCmd("pgrep",["-f",i],{allowFailure:!0})}catch(e){l({level:"warn",phase:"record_stop_ios_simulator_pgrep_failed",data:{outPath:t,signal:r,error:eu(e)}});return}let o=eZ(e0(a.stdout)),n=eY(o,r);l({level:n>0?"warn":"debug",phase:"record_stop_ios_simulator_signal_recorders",data:{outPath:t,signal:r,matchedPidCount:o.length,signaled:n,pgrepExitCode:a.exitCode}})}async function eJ(e,t,r){let a=t.recorderPid??t.child.pid;if("number"!=typeof a||!Number.isInteger(a)||a<=0)return l({level:"debug",phase:"record_stop_ios_simulator_owned_recorder_unavailable",data:{outPath:t.outPath,signal:r,reason:"missing_recorder_pid"}}),!1;let i=await eX(e,a,t.outPath,r),o=eZ([a,...i.pids]),n=eY(o,r);return l({level:n>0?"warn":"debug",phase:"record_stop_ios_simulator_signal_owned_recorder",data:{outPath:t.outPath,signal:r,recorderPid:a,childPidCount:i.pids.length,matchedPidCount:o.length,signaled:n,pgrepExitCode:i.exitCode}}),n>0}async function eX(e,t,r,a){let i;try{i=await e.runCmd("pgrep",["-P",String(t)],{allowFailure:!0})}catch(e){return l({level:"warn",phase:"record_stop_ios_simulator_owned_pgrep_failed",data:{outPath:r,signal:a,parentPid:t,error:eu(e)}}),{pids:[]}}return{pids:e0(i.stdout),exitCode:i.exitCode}}function eZ(e){return Array.from(new Set(e)).filter(e=>Number.isInteger(e)&&e>0&&e!==process.pid)}function eY(e,t){let r=0;for(let a of e)try{process.kill(a,t),r+=1}catch{}return r}function e0(e){return e.split(/\s+/).map(e=>Number(e)).filter(e=>Number.isInteger(e)&&e>0)}async function e1(e){let t,r,{req:a,activeSession:i,device:o,logPath:n,deps:s,recordingBase:c,resolvedOut:d}=e,u=c.showTouches?await eK({req:a,activeSession:i,device:o,logPath:n,deps:s}):void 0,{child:l,wait:p}=s.startIosSimulatorRecording({device:o,outPath:d}),m=await e4(d);if(u)t=u.gestureClockOriginAtMs,r=u.gestureClockOriginUptimeMs;else if(c.showTouches)try{let e=Date.now(),c=await s.runAppleRunnerCommand(o,{command:"uptime",appBundleId:eL(i)},eU(a,n,i)),d=Date.now();t=Math.round((e+d)/2),r="number"==typeof c.currentUptimeMs?c.currentUptimeMs:void 0}catch{}return{platform:"ios",child:l,wait:p,...c,recorderPid:l.pid,startedAt:m,gestureClockOriginAtMs:void 0===r?void 0:t,gestureClockOriginUptimeMs:r}}async function e2(e){let{deps:t,recording:r,stopRequestedAt:a}=e;await u("record_stop_tail_settle",()=>t.waitForRecordingTail(r),{platform:r.platform,gestureEventCount:r.gestureEvents.length});let i=await u("record_stop_ios_simulator_process",()=>eq({deps:t,recording:r}),{outPath:r.outPath});if(!i)return e5("failed to stop recording: simctl recordVideo did not exit after 5000ms and forced cleanup",r,a);if(0!==i.exitCode)return e5(`failed to stop recording: ${el(i,"simctl recordVideo")}`,r,a);if(await u("record_stop_video_stable",()=>t.waitForStableFile(r.outPath,{pollMs:150,attempts:12}),{outPath:r.outPath}),!await u("record_stop_video_playable_check",()=>t.isPlayableVideo(r.outPath),{outPath:r.outPath}))return e5(`failed to stop recording: ${r.outPath} was not finalized into a playable video`,r,a);if(void 0!==r.maxSize)try{await u("record_stop_resize",()=>t.resizeRecording({videoPath:r.outPath,maxSize:r.maxSize,exportQuality:r.exportQuality,targetLabel:"iOS recording"}),{outPath:r.outPath,maxSize:r.maxSize})}catch(e){r.overlayWarning=`failed to resize recording: ${eu(e)}`}return await u("record_stop_finalize_overlay",()=>em({recording:r,deps:t,targetLabel:"iOS recording"}),{outPath:r.outPath,showTouches:r.showTouches,gestureEventCount:r.gestureEvents.length}),null}async function e4(e){for(let t=0;t<2;t+=1){try{if(i.statSync(e).size>0)return Date.now()}catch{}if(t+1>=2)break;await f(250)}return Date.now()}function e5(e,t,r){let a=ep(e,t,r);return function(e){try{i.rmSync(e,{force:!0})}catch{}}(t.outPath),b("COMMAND_FAILED",a.message)}function e8(e){return tr[v(e.platform)?.recording?.resolveBackendTag(e)??"unsupported"]}function e3({req:e}){return e.positionals?.[1]??$(void 0)}x();let e6={validateStart:e=>(function(e){let t,r=(t=[],e.flags?.fps!==void 0&&t.push("--fps"),e.flags?.quality!==void 0&&t.push("--quality"),e.flags?.screenshotMaxSize!==void 0&&t.push("--max-size"),e.flags?.hideTouches!==void 0&&t.push("--hide-touches"),t);return r.length>0?b("INVALID_ARGS",`web recordings do not support ${r.join(", ")}; agent-browser records WebM directly`):null})(e),resolveOutputPath:function({req:e}){let t=e.positionals?.[1];return void 0===t?$("web"):O(t,".webm")},start:async({activeSession:e,recordingBase:t,resolvedOut:r})=>{var a;let i=(a=r,".webm"!==o.extname(a).toLowerCase()?b("INVALID_ARGS","web recordings must use a .webm output path"):null);if(i)return i;if(e.recordOnlySession)return b("INVALID_ARGS","record on web requires an active browser session; run open <url> --platform web first");let n=k();return n.startRecording?(await n.startRecording(r),{...t,outPath:r,startedAt:Date.now(),platform:"web",showTouches:!1}):b("UNSUPPORTED_OPERATION","record is not supported by this web provider")},stop:async({recording:e})=>await ta({recording:e}),cleanupRecordOnlySession:async()=>{try{await k().close()}catch{}}},e7={resolveOutputPath:e3,start:async({req:e,activeSession:t,sessionStore:r,device:a,logPath:i,deps:o,fpsFlag:n,recordingBase:s})=>{let c=eL(t);return c?await eG({req:e,activeSession:t,sessionStore:r,device:a,logPath:i,deps:o,fpsFlag:n,recordingBase:s,appBundleId:c}):b("INVALID_ARGS","record on physical iOS devices requires an active app session; run open <app> first")},stop:async({req:e,activeSession:t,device:r,logPath:a,deps:i,recording:o})=>await eH({req:e,activeSession:t,device:r,logPath:a,deps:i,recording:o})},e9={resolveOutputPath:e3,start:async({req:e,activeSession:t,device:r,logPath:a,deps:i,fpsFlag:o,recordingBase:n})=>{let s=eL(t);return s?await eV({req:e,activeSession:t,device:r,logPath:a,deps:i,fpsFlag:o,recordingBase:n,appBundleId:s}):b("INVALID_ARGS","record on macOS requires an active app session; run open <app> first")},stop:async({req:e,activeSession:t,device:r,logPath:a,deps:i,recording:o})=>await ej({req:e,activeSession:t,device:r,logPath:a,deps:i,recording:o})},te={resolveOutputPath:e3,start:async({req:e,activeSession:t,device:r,logPath:a,deps:i,recordingBase:o,resolvedOut:n})=>await e1({req:e,activeSession:t,device:r,logPath:a,deps:i,recordingBase:o,resolvedOut:n}),stop:async({deps:e,recording:t,stopRequestedAt:r})=>await e2({deps:e,recording:t,stopRequestedAt:r})},tt={resolveOutputPath:e3,start:async({device:e,recordingBase:t})=>await eE({device:e,recordingBase:t}),stop:async({deps:e,device:t,recording:r,stopRequestedAt:a})=>await eF({deps:e,device:t,recording:r,stopRequestedAt:a})},tr={web:e6,android:tt,macos:e9,"ios-device":e7,"ios-simulator":te,unsupported:{resolveOutputPath:e3,start:async()=>b("UNSUPPORTED_OPERATION","record is not supported on this device"),stop:async()=>b("UNSUPPORTED_OPERATION","record is not supported on this device")}};async function ta(e){let{recording:t}=e,r=k();if(!r.stopRecording)return b("UNSUPPORTED_OPERATION","record is not supported by this web provider");if(await r.stopRecording(),!function(e){try{return i.statSync(e).size>0}catch{return!1}}(t.outPath)){var a=t.outPath;try{i.rmSync(a,{force:!0})}catch{}return b("COMMAND_FAILED",`failed to stop recording: ${t.outPath} was not finalized into a WebM video`)}return null}async function ti(e){"ios"!==e.platform||0!==e.gestureEvents.length&&await f(350)}async function to(e){let t,{req:r,sessionName:a,sessionStore:n,activeSession:s,device:c,logPath:d,deps:u}=e;if(s.recording)return b("INVALID_ARGS","recording already in progress");let l=r.flags?.fps,p=r.flags?.quality,m=r.flags?.screenshotMaxSize,h=e8(c),f=h.validateStart?.(r)??null;if(f)return f;if(void 0!==l&&(!Number.isInteger(l)||l<1||l>120))return b("INVALID_ARGS","fps must be an integer between 1 and 120");if(void 0!==p&&void 0===T(p))return b("INVALID_ARGS",`quality must be one of: ${L.join(", ")} (legacy numeric values 5-10 are accepted)`);if(void 0!==m&&(!Number.isInteger(m)||m<1))return b("INVALID_ARGS","max-size must be a positive integer");let w=A("record",c);if(w)return w;let g=h.resolveOutputPath({req:r}),y=P.expandHome(g,r.meta?.cwd),_=(t=T(r.flags?.quality),{outPath:y,clientOutPath:r.meta?.clientArtifactPaths?.outPath,startedAt:Date.now(),maxSize:r.flags?.screenshotMaxSize,exportQuality:t??"medium",showTouches:r.flags?.hideTouches!==!0,gestureEvents:[]});i.mkdirSync(o.dirname(y),{recursive:!0}),i.rmSync(y,{force:!0});let v=await h.start({req:r,activeSession:s,sessionStore:n,device:c,logPath:d,deps:u,fpsFlag:l,recordingBase:_,resolvedOut:y});if("ok"in v)return v;s.recording=v,n.set(a,s);let S=n.ensureSessionDir(a);return U(n,s,r,r.command,{action:"start",showTouches:v.showTouches}),{ok:!0,data:{recording:"started",outPath:v.clientOutPath??g,sessionStateDir:S,showTouches:v.showTouches}}}async function tn(e){var t;let r,a,{req:i,activeSession:n,device:s,logPath:c,deps:d}=e;if(!n.recording)return b("INVALID_ARGS","no active recording");let u=n.recording,l=Date.now(),p=u.invalidatedReason;n.recording=void 0;let m=await function(e){let{recording:t}=e;switch(t.platform){case"android":return tt.stop({...e,recording:t});case"ios":return te.stop({...e,recording:t});case"ios-device-runner":return e7.stop({...e,recording:t});case"macos-runner":return e9.stop({...e,recording:t});case"web":return e6.stop({...e,recording:t})}return t}({req:i,activeSession:n,device:s,logPath:c,deps:d,recording:u,stopRequestedAt:l});if(m)return m;if(p&&"ios"===u.platform&&u.showTouches)u.overlayWarning??=`overlay unavailable: ${p}`;else if(p)return b("COMMAND_FAILED",p);return r="android"===(t=u).platform?t.chunks:void 0,a=[{field:"outPath",path:t.outPath,localPath:t.clientOutPath,fileName:o.basename(t.clientOutPath??t.outPath)}],r&&r.length>1&&a.push(...r.slice(1).map(e=>({field:"chunkPath",path:e.path,localPath:ts(t,e.index),fileName:o.basename(ts(t,e.index)??e.path)}))),t.telemetryPath&&a.push({field:"telemetryPath",path:t.telemetryPath,localPath:function(e){if(e.clientOutPath)return er(e.clientOutPath)}(t),fileName:o.basename(t.telemetryPath)}),{ok:!0,data:{recording:"stopped",outPath:t.outPath,telemetryPath:t.telemetryPath,artifacts:a,showTouches:t.showTouches,warning:t.warning,overlayWarning:t.overlayWarning,chunks:r?.map(e=>({index:e.index,path:ts(t,e.index)??e.path}))}}}function ts(e,t){if("android"===e.platform&&e.clientOutPath)return eh(e.clientOutPath,t)}async function tc(e,t,r,a={}){if(!r.recordOnlySession)return;let i=e8(r.device);await i.cleanupRecordOnlySession?.(r),a.writeLog&&e.writeSessionLog(r),e.delete(t)}async function td(e){let{req:t,sessionName:r,sessionStore:a,logPath:i}=e,o={runCmd:async(e,t,r)=>"xcrun"===e?await _(t,r):await p(e,t,r),startIosSimulatorRecording:e=>F().startIosSimulatorRecording(e),runAppleRunnerCommand:I,waitForRecordingTail:ti,waitForStableFile:J,isPlayableVideo:X,trimRecordingStart:es,resizeRecording:ed,overlayRecordingTouches:ec},n=a.get(r),s=n?.device??await R(t.flags??{});n||await E(s);let c=n??{name:C(t),sessionScope:M(t),device:s,createdAt:Date.now(),recordOnlySession:!0,actions:[]},d=(t.positionals?.[0]??"").toLowerCase();if(!["start","stop"].includes(d))return b("INVALID_ARGS","record requires start|stop");if("start"===d)return to({req:t,sessionName:r,sessionStore:a,activeSession:c,device:s,logPath:i,deps:o});let u=await tn({req:t,activeSession:c,device:s,logPath:i,deps:o});return u.ok?(U(a,c,t,t.command,{action:"stop",outPath:u.data?.outPath,showTouches:u.data?.showTouches}),await tc(a,r,c,{writeLog:!0})):await tc(a,r,c),u}export{td as handleRecordCommand};
|
|
@@ -29,7 +29,7 @@ declare type LeaseBackend = (typeof LEASE_BACKENDS)[number];
|
|
|
29
29
|
|
|
30
30
|
declare type MetroPrepareKind = 'auto' | 'react-native' | 'expo';
|
|
31
31
|
|
|
32
|
-
declare const PLATFORM_SELECTORS: readonly ["
|
|
32
|
+
declare const PLATFORM_SELECTORS: readonly ["apple", "android", "linux", "web", "ios", "macos"];
|
|
33
33
|
|
|
34
34
|
declare type PlatformSelector = (typeof PLATFORM_SELECTORS)[number];
|
|
35
35
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{setSessionSnapshot as e,createDaemonRuntimeSessionStore as t,context_contextFromFlags as r,captureSnapshot as n,dispatchCommand as s,requireCommandSupported as o,stopIosRunnerSession as i,errorResponse as a,resolveRunnerAppBundleId as l,getActiveAndroidSnapshotFreshness as c,noActiveSessionError as u,resolveTargetDevice as f,runAppleRunnerCommand as d}from"./9722.js";import{closeIosApp as p}from"./apps.js";import{emitDiagnostic as m}from"./432.js";import{ensureDeviceReady as g,buildAppleRunnerRequestOptions as h}from"./8414.js";import{asAppError as y,normalizeError as w,AppError as S}from"./485.js";import{evaluateIsPredicate as _,isSupportedPredicate as v,localCommandPolicy as x,createAgentDevice as k}from"./7871.js";import{getAndroidAppState as I}from"./3340.js";import{splitIsSelectorArgs as A,normalizeType as P,prefersValueForReadableText as N,findNodeByLabel as R,extractNodeReadText as b}from"./893.js";import{resolveRectCenter as q,isSparseSnapshotQualityVerdict as C,parseWaitPositionals as M}from"./2948.js";import{buildSnapshotPresentationKey as E,snapshotPresentationOptionsFromFlags as D,isApplePlatform as $}from"./6849.js";import{parseFindArgs as L,tryParseSelectorChain as O}from"./587.js";async function K(e,t,r){let n=e.get(t),s=n?.device??await f(r??{});return n||await g(s),{session:n,device:s}}async function F(e,t,r){let n=!e&&"ios"===t.platform;try{return await r()}finally{n&&(await i(t.id),await G(t))}}async function G(e){let t=l();await p(e,t).catch(r=>{m({level:"debug",phase:"ios_sessionless_runner_host_close_failed",data:{deviceId:e.id,bundleId:t,error:r instanceof Error?r.message:String(r)}})})}function V(e,t,r,n){t&&e.recordAction(t,{command:r.command,positionals:r.positionals??[],flags:r.flags??{},result:n})}function U(e){let{session:t,sessionName:r,device:n,snapshot:s,appBundleId:o}=e;return t?{...t,snapshot:s,lastComparisonSafeSnapshot:s?.comparisonSafe===!0?s:t.lastComparisonSafeSnapshot}:{name:r,device:n,createdAt:Date.now(),appBundleId:o,snapshot:s,...s?.comparisonSafe===!0?{lastComparisonSafeSnapshot:s}:{},actions:[]}}function T(e,t={}){return{artifacts:function(e,t={}){let r=!0===t.plural?"do":"does";return{resolveInput:async()=>{throw new S("UNSUPPORTED_OPERATION",`${e} ${r} not resolve input artifacts`)},reserveOutput:async()=>{throw new S("UNSUPPORTED_OPERATION",`${e} ${r} not reserve output artifacts`)},createTempFile:async()=>{throw new S("UNSUPPORTED_OPERATION",`${e} ${r} not create temporary files`)}}}(e,t),policy:x()}}let B=[["snapshotDepth","--depth"],["snapshotScope","--scope"],["snapshotRaw","--raw"]];function H(e,t){let r=function(e){if(!e)return[];let t=[];for(let[r,n]of B)void 0!==e[r]&&t.push(n);return t}(t);return 0===r.length?null:a("INVALID_ARGS",`${e} @ref does not support ${r.join(", ")}.`)}async function j(e,t){let r=await z(e);if(r)throw new S("COMMAND_FAILED",`press ${t} left ${e.appBundleId} and foregrounded ${r.foregroundPackage}. The tap likely escaped the app.`,r)}async function z(e){var t;if("android"!==e.device.platform||!e.appBundleId)return null;let r=await I(e.device),n=r.package?.trim();return n&&n!==e.appBundleId&&("com.android.settings"===(t=n)||"com.android.systemui"===t||"com.google.android.permissioncontroller"===t||t.includes("launcher"))?{expectedPackage:e.appBundleId,foregroundPackage:n,activity:r.activity,hint:"com.google.android.permissioncontroller"===n?"Dismiss or allow the permission prompt, then retry the smoke assertion.":"Use screenshot as visual truth, then take a fresh snapshot -i before retrying."}:null}function Q(e){return"COMMAND_FAILED"===e.code&&"string"==typeof e.details?.expectedPackage&&"string"==typeof e.details?.foregroundPackage}function W(e,t){let r=Array.isArray(e.selectorChain)?e.selectorChain:void 0,n=Z(e),s=n?.kind==="ref"?ee(n.ref):void 0,o=n?.kind==="selector"?n.selector:void 0,i={...s?{ref:s}:{},...o?{selector:o}:{},...r?{selectorChain:r}:{}};if("attrs"===t)return i;let a="string"==typeof e.text?e.text:"";return{...i,text:a,refLabel:function(e){let t=e.trim();if(!(!t||t.length>80||/[\r\n]/.test(t)))return t}(a)}}function J(e){let t=Z(e);return{...t?.kind==="ref"?{ref:ee(t.ref)}:{},...t?.kind==="selector"?{selector:t.selector}:{},..."string"==typeof e.text?{text:e.text}:{},...e.node&&"object"==typeof e.node?{node:e.node}:{}}}function X(e){let{selectorChain:t,...r}=e;return r}function Y(e,t,r,n){let s=e.get(t);s&&e.recordAction(s,{command:r.command,positionals:r.positionals??[],flags:r.flags??{},result:n})}function Z(e){let t=e.target;return t&&"object"==typeof t?"ref"===t.kind&&"string"==typeof t.ref?{kind:"ref",ref:t.ref}:"selector"===t.kind&&"string"==typeof t.selector?{kind:"selector",selector:t.selector}:void 0:void 0}function ee(e){return e.startsWith("@")?e.slice(1):e}let et=["application","window","tabbar","scrollbar","image"],er=new Set(["tab bar"]);async function en(e,t){var r;if(t.ok||(r=t.error.message,!/^wait timed out for (?:selector|text): /i.test(r)))return t;let n=await es(e).catch(()=>null);return n?a(t.error.code,`${t.error.message}. Current surface: ${n.summary}.`,{...t.error.details??{},currentSurface:n.details}):t}async function es(e){let t=[...(await n({device:e.device,session:e.session,flags:{...e.req.flags,snapshotInteractiveOnly:!0},logPath:e.logPath??""})).snapshot.nodes].sort(ei),r=eo(t,6,{includeIdentifiers:!0});if(0===r.length)return null;let s=eo(t.filter(e=>!ec(e)),4,{includeIdentifiers:!1}),o=eo(t.filter(eu),4,{includeIdentifiers:!0});return{summary:(s.length>0?s:r.slice(0,4)).join(", "),details:{labels:r,...o.length>0?{buttons:o}:{}}}}function eo(e,t,r){let n=new Set,s=[];for(let o of e){let e=el(o,r);if(!(!e||n.has(e))&&(n.add(e),s.push(e),s.length>=t))break}return s}function ei(e,t){var r,n;return ea(e)-ea(t)||(r=e,n=t,r.rect&&n.rect?r.rect.y-n.rect.y||r.rect.x-n.rect.x:r.rect?-1:n.rect?1:(r.depth??0)-(n.depth??0)||r.index-n.index)}function ea(e){let t=!!el(e,{includeIdentifiers:!1});return 2*!!ec(e)+ +!t}function el(e,t){let r=(t.includeIdentifiers?[e.label,e.value,e.identifier]:[e.label,e.value]).map(e=>"string"==typeof e?e.trim():"").find(e=>e.length>0);return r?r.replace(/\s+/g," ").slice(0,80):""}function ec(e){let t=P(`${e.type??""} ${e.role??""} ${e.subrole??""}`),r=`${e.label??""} ${e.value??""}`.trim().toLowerCase();return et.some(e=>t.includes(e))||er.has(r)||r.endsWith(".fill")}function eu(e){return P(`${e.type??""} ${e.role??""} ${e.subrole??""}`).includes("button")}function ef(e){var t;let{session:r,selectorExpression:n}=e;if(!r||"ios"!==r.device.platform||r.postGestureStabilization)return null;let s=O(n);if(!s||1!==s.selectors.length)return null;let o=s.selectors[0];if(!o||1!==o.terms.length)return null;let i=o.terms[0];return i&&"string"==typeof i.value&&("id"===(t=i.key)||"label"===t||"text"===t||"value"===t)?{key:i.key,value:i.value,raw:o.raw}:null}function ed(e,t={}){let r=y(e);if("ELEMENT_NOT_FOUND"===r.code)return!0===t.allowElementNotFound;if("COMMAND_FAILED"!==r.code)return!1;let n=r.message.toLowerCase();return n.includes("fetch failed")||n.includes("timed out")||n.includes("timeout")||n.includes("runner did not accept connection")||n.includes("invalid runner response")}async function ep(e){let{device:t,node:r,flags:n,appBundleId:o,traceOutPath:i,surface:a,contextFromFlags:l}=e,c=b(r),u=q(r.rect);if(!u||"ios"===t.platform&&c&&!N(r.type??""))return c;try{let e=await s(t,"read",[String(u.x),String(u.y)],void 0,{...l(n,o,i),surface:a}),f=e&&"object"==typeof e?e:void 0,d="string"==typeof f?.text?f.text:"";if(d.trim())return d;return m({level:"warn",phase:"interaction_read_fallback",data:{reason:"empty_backend_text",nodeRef:r.ref,surface:a,platform:t.platform}}),c}catch(e){return m({level:"warn",phase:"interaction_read_fallback",data:{reason:"backend_read_failed",nodeRef:r.ref,surface:a,platform:t.platform,error:e instanceof Error?e.message:String(e)}}),c}}function em(t){let r,n,{session:s,sessionStore:o,sessionName:i}=t,a=0;return{capture:async l=>{var u,f;let d=Date.now(),p=(u=l,f=t.req.flags?.out,JSON.stringify({presentationKey:E(D(e_(u))),includeRects:!0===u.includeRects,outPath:u.outPath??f??null})),m=function(e){let{timestamp:t,lastSnapshotAt:r,lastSnapshotResult:n,lastSnapshotCacheKey:s,session:o,request:i,cacheKey:a}=e;if(!(i.cache?.forceFresh===!0||!n||s!==a||t-r>=750||c(o))&&!eS(o,i))return n}({timestamp:d,lastSnapshotAt:a,lastSnapshotResult:r,lastSnapshotCacheKey:n,session:s,request:l,cacheKey:p});if(m)return m;let g=function(e){var t,r;let{session:n,timestamp:s,request:o}=e,i=n?.snapshot;if(i&&(t=n,r=o,!(r.cache?.forceFresh===!0||r.cache?.useSessionSnapshot!==!0||c(t)||eS(t,r)))&&s-i.createdAt<750&&i.presentationKey===function(e){return E(D(e_(e)))}(o))return i}({session:s,timestamp:d,request:l});if(g)return a=g.createdAt,r={snapshot:g},n=p,r;let h=await eg({params:t,request:l}),y={snapshot:h};return function(t){let{session:r,sessionStore:n,sessionName:s,snapshot:o}=t;!r||C(o.snapshotQuality)||(e(r,o),n.set(s,r))}({session:s,sessionStore:o,sessionName:i,snapshot:h}),a=d,r=y,n=p,y}}}async function eg(e){var t;let{params:r,request:n}=e,s=await ew(r,n,n.snapshotScope);return n.recovery?.legacyIosSparse&&!(t=s).snapshotQuality&&"xctest"===t.backend&&1===t.nodes.length&&t.nodes[0]?.type==="Application"?await eh({runtimeParams:r,request:n,policy:n.recovery.legacyIosSparse}):n.recovery?.sparseVerdictQueryScope?.shouldScope&&C(s.snapshotQuality)?await ey({runtimeParams:r,request:n,policy:n.recovery.sparseVerdictQueryScope,snapshot:s}):s}async function eh(e){let{runtimeParams:t,request:r,policy:n}=e;try{return await ew(t,r,n.scope,!1)}catch(e){if(!n.shouldScope)throw e;return await ew(t,r,n.query,!1)}}async function ey(e){let{runtimeParams:t,request:r,policy:n,snapshot:s}=e;try{return await ew(t,r,n.query,!1)}catch{return s}}async function ew(e,t,r,s=t.flags?.snapshotInteractiveOnly){return(await n({device:e.device,session:e.session,flags:{...t.flags,snapshotInteractiveOnly:s},outPath:t.outPath??e.req.flags?.out,logPath:e.logPath??"",snapshotScope:r,includeRects:t.includeRects})).snapshot}function eS(e,t){return t.cache?.bypassForPostGestureStabilization===!0&&!!e?.postGestureStabilization}function e_(e){return void 0===e.snapshotScope?e.flags:{...e.flags,snapshotScope:e.snapshotScope}}function ev(n){return k({backend:function(e){let{req:t,session:n,device:s,logPath:o,sessionName:i,sessionStore:a}=e,l=em({device:s,session:n,sessionStore:a,sessionName:i,req:t,logPath:o});return{platform:s.platform,captureSnapshot:async(e,r)=>{var n;let o,i={...t.flags,...(n=r,o={},n?.interactiveOnly!==void 0&&(o.snapshotInteractiveOnly=n.interactiveOnly),n?.scope!==void 0&&(o.snapshotScope=n.scope),n?.depth!==void 0&&(o.snapshotDepth=n.depth),n?.raw!==void 0&&(o.snapshotRaw=n.raw),o)},a=r?.includeRects===!0,c=r?.scope??t.flags?.snapshotScope,u="wait"===t.command||"find"===t.command||a&&"web"===s.platform;return await l.capture({flags:i,snapshotScope:c,includeRects:a,cache:{forceFresh:u,useSessionSnapshot:!0,bypassForPostGestureStabilization:!0}})},readText:async(i,a)=>({text:await ep({device:s,node:a,flags:t.flags,appBundleId:n?.appBundleId,traceOutPath:n?.trace?.outPath,surface:n?.surface,contextFromFlags:e.contextFromFlags??((e,t,n)=>r(o??"",e,t,n))})}),findText:async(t,r)=>({found:await ek(e,r)})}}(n),...T("selector commands",{plural:!0}),sessions:t({sessionName:n.sessionName,getSession:()=>n.session,recordOptions:{includeSnapshot:!0},setRecord:t=>{n.session&&t.snapshot&&(e(n.session,t.snapshot),n.sessionStore.set(n.sessionName,n.session))}})})}async function ex(e,t){let r=e.sessionStore.get(e.sessionName);if(!r&&t.requireSession)return{ok:!1,response:u()};let n=r?.device??await f(e.req.flags??{});r||await g(n);let s=o(t.capability,n);return s?{ok:!1,response:s}:{ok:!0,runtime:ev({...e,session:r,device:n})}}async function ek(e,t){let r=await eI(e,t);if(null!==r)return r;let n=await eA(e,t);return null!==n?n:await eP(e,t)}async function eI(e,t){return"macos"===e.device.platform&&e.session?.surface&&"app"!==e.session.surface?await eP(e,t):null}async function eA(e,t){var r,n,s;let o=$((r=e).device.platform)&&r.session?.appBundleId?{device:r.device,appBundleId:r.session.appBundleId,traceLogPath:r.session.trace?.outPath}:null;if(!o)return null;let i=await d(o.device,{command:"findText",text:t,appBundleId:o.appBundleId},(n=e,s=o,h({req:n.req,logPath:n.logPath,traceLogPath:s.traceLogPath})));return i?.found===!0}async function eP(e,t){return!!R((await eN(e)).nodes,t)}async function eN(e){let t=em({device:e.device,session:e.session,sessionStore:e.sessionStore,sessionName:e.sessionName,req:e.req,logPath:e.logPath}),{snapshot:r}=await t.capture({flags:{...e.req.flags,snapshotInteractiveOnly:!1},cache:{forceFresh:!0,bypassForPostGestureStabilization:!0}});return r}async function eR(e){var t;let{req:r}=e;if("find"!==r.command)return null;let n=r.positionals??[];if(0===n.length)return a("INVALID_ARGS","find requires a locator or text");let s=L(n);if(!s.query)return a("INVALID_ARGS","find requires a value");if(r.flags?.findFirst&&r.flags?.findLast)return a("INVALID_ARGS","find accepts only one of --first or --last");let o=s.action;if("exists"!==(t=o)&&"wait"!==t&&"get_text"!==t&&"get_attrs"!==t)return null;let i=await ex(e,{requireSession:!1,capability:"find"});return i.ok?await eF(async()=>{let t=await i.runtime.selectors.find({session:e.sessionName,requestId:r.meta?.requestId,locator:s.locator,query:s.query,action:o,timeoutMs:s.timeoutMs});return Y(e.sessionStore,e.sessionName,r,function(e,t){if("exists"===t)return{found:!0};if("wait"===t)return{found:!0,waitedMs:e.waitedMs};let r="string"==typeof e.ref?e.ref:void 0;return"get_attrs"===t?{ref:r,action:"get attrs"}:{ref:r,action:"get text",text:"string"==typeof e.text?e.text:""}}(t,o)),"found"===t.kind?{found:!0,..."number"==typeof t.waitedMs?{waitedMs:t.waitedMs}:{}}:{..."string"==typeof t.ref?{ref:t.ref}:{},..."string"==typeof t.text?{text:t.text}:{},...t.node&&"object"==typeof t.node?{node:t.node}:{}}}):i.response}async function eb(e){let{req:t}=e;if("get"!==t.command)return null;let r=t.positionals?.[0];if("text"!==r&&"attrs"!==r)return a("INVALID_ARGS","get only supports text or attrs");let n=function(e){let t=e.positionals?.[1]??"";if(t.startsWith("@"))return{ok:!0,target:{kind:"ref",ref:t,fallbackLabel:e.positionals.length>2?e.positionals.slice(2).join(" ").trim():""}};let r=e.positionals?.slice(1).join(" ").trim()??"";return r?{ok:!0,target:{kind:"selector",selector:r}}:{ok:!1,response:a("INVALID_ARGS","get requires @ref or selector expression")}}(t);if(!n.ok)return n.response;if("ref"===n.target.kind){let e=H("get",t.flags);if(e)return e}if("selector"===n.target.kind){let t=await eM(e,r,n.target.selector);if(t)return t}let s=await ex(e,{requireSession:!0,capability:"get"});return s.ok?await eF(async()=>{let o=await s.runtime.selectors.get({session:e.sessionName,requestId:t.meta?.requestId,property:r,target:n.target});return Y(e.sessionStore,e.sessionName,t,W(o,r)),J(o)}):s.response}async function eq(e){let{req:t}=e;if("is"!==t.command)return null;let r=(t.positionals?.[0]??"").toLowerCase();if(!v(r))return a("INVALID_ARGS","is requires predicate: visible|hidden|exists|editable|selected|text");let{split:n}=A(t.positionals??[]);if(!n)return a("INVALID_ARGS","is requires a selector expression");let s=n.rest.join(" ").trim();if("text"===r&&!s)return a("INVALID_ARGS","is text requires expected text value");if("text"!==r&&n.rest.length>0)return a("INVALID_ARGS",`is ${r} does not accept trailing values`);let o=await eE(e,r,n.selectorExpression,s);if(o)return o;let i=await ex(e,{requireSession:!0,capability:"is"});if(!i.ok)return i.response;let l=await eF(async()=>{let o=await i.runtime.selectors.is({session:e.sessionName,requestId:t.meta?.requestId,predicate:r,selector:n.selectorExpression,expectedText:s});return Y(e.sessionStore,e.sessionName,t,o),X(o)});return await eG(e,l,`is ${r}`)}async function eC(e){let{req:t,sessionName:r,sessionStore:n}=e,s=M(t.positionals??[]);if(!s)return a("INVALID_ARGS","wait requires a duration or text");let{session:i,device:l}=await K(n,r,t.flags);if("sleep"!==s.kind){let e=o("wait",l);if(e)return e}if("selector"===s.kind){let t=await eD({...e,session:i,device:l,selectorExpression:s.selectorExpression,timeoutMs:s.timeoutMs});if(t)return t}let c=async()=>{let o=ev({...e,session:i,device:l}),a=await eF(async()=>{let e=await o.selectors.wait({session:r,requestId:t.meta?.requestId,target:function(e,t){if("sleep"===e.kind)return{kind:"sleep",durationMs:e.durationMs};if("selector"===e.kind)return{kind:"selector",selector:e.selectorExpression,timeoutMs:e.timeoutMs};if("ref"===e.kind){if(!t?.snapshot)throw new S("INVALID_ARGS","Ref wait requires an existing snapshot in session.");return{kind:"ref",ref:e.rawRef,timeoutMs:e.timeoutMs}}if(!e.text)throw new S("INVALID_ARGS","wait requires text");return{kind:"text",text:e.text,timeoutMs:e.timeoutMs}}(s,i)});return Y(n,r,t,e),{waitedMs:e.waitedMs,..."string"==typeof e.text?{text:e.text}:{},..."string"==typeof e.selector?{selector:e.selector}:{}}}),c=await en({req:t,logPath:e.logPath,session:i,device:l},a);return await eG(e,c,"wait")};return"sleep"===s.kind?await c():await F(i,l,c)}async function eM(e,t,r){let n=e.sessionStore.get(e.sessionName),s=ef({session:n,selectorExpression:r});if(!n||!s||"text"===t&&"id"!==s.key)return null;let o=await eO(e,n,s);if(eK(o))return o.response;if(!o)return null;let i=function(e,t,r){if(!r.found||!r.node)return null;let n={target:{kind:"selector",selector:t},node:r.node,selectorChain:[t]};return"attrs"===e?{kind:"attrs",...n}:"string"!=typeof r.text?null:{kind:"text",...n,text:r.text}}(t,s.raw,o);return i?(Y(e.sessionStore,e.sessionName,e.req,W(i,t)),{ok:!0,data:J(i)}):null}async function eE(e,t,r,n){var s,o,i,a,l;let c;if("hidden"===t)return null;let u=await e$(e,r);if(eK(u))return u.response;if(!u?.result.found||!u.result.node)return null;let f="exists"===t?{predicate:t,pass:!0,selector:u.selector.raw,matches:1,selectorChain:[u.selector.raw]}:(s=t,o=n,i=u.selector.raw,a=u.session,c=_({predicate:s,node:l=u.result.node,nodes:[l],expectedText:o,platform:a.device.platform}),{predicate:s,pass:c.pass,selector:i,..."text"===s?{text:c.actualText}:{},selectorChain:[i]});return f?(Y(e.sessionStore,e.sessionName,e.req,f),{ok:!0,data:X(f)}):null}async function eD(e){let t=ef({session:e.session,selectorExpression:e.selectorExpression});if(!e.session||!t)return null;let r=Date.now(),n=await eO(e,e.session,t);if(eK(n))return n.response;if(!n?.found)return null;let s={kind:"selector",selector:t.raw,waitedMs:Date.now()-r,selectorChain:[t.raw]};return Y(e.sessionStore,e.sessionName,e.req,s),await en({req:e.req,logPath:e.logPath,session:e.session,device:e.device},{ok:!0,data:s})}async function e$(e,t){let r=e.sessionStore.get(e.sessionName),n=ef({session:r,selectorExpression:t});if(!r||!n)return null;let s=await eO(e,r,n);return eK(s)?s:s?{session:r,selector:n,result:s}:null}async function eL(e,t,r){let n=await d(t.device,{command:"querySelector",selectorKey:r.key,selectorValue:r.value,appBundleId:t.appBundleId},h({req:e.req,logPath:e.logPath,traceLogPath:t.trace?.outPath})),s=!0===n.found,o=function(e){let t=e.nodes;if(!Array.isArray(t))return;let r=t[0];if(r&&"object"==typeof r)return r}(n);return{found:s,..."string"==typeof n.text?{text:n.text}:{},...o?{node:o}:{}}}async function eO(e,t,r){try{return await eL(e,t,r)}catch(e){if(ed(e,{allowElementNotFound:!0}))return null;return{kind:"error",response:{ok:!1,error:w(e)}}}}function eK(e){return null!==e&&"kind"in e&&"error"===e.kind}async function eF(e){try{return{ok:!0,data:await e()}}catch(t){let e=y(t);return a(e.code,e.message,e.details)}}async function eG(e,t,r){var n;let s;if(t.ok)return t;let o=e.sessionStore.get(e.sessionName);if(!o)return t;try{s=await z(o)}catch{return t}return s?a(t.error.code,`${r} failed because ${"com.google.android.permissioncontroller"===(n=s).foregroundPackage?`Android permission dialog is blocking ${n.expectedPackage}`:`${n.foregroundPackage} is foreground instead of ${n.expectedPackage}`}.`,{...t.error.details??{},...s,blockedBy:"android_foreground_surface",originalMessage:t.error.message}):t}export{j as assertAndroidPressStayedInApp,U as buildSnapshotSession,T as createDaemonRuntimePolicy,em as createSelectorCaptureRuntime,eR as dispatchFindReadOnlyViaRuntime,eb as dispatchGetViaRuntime,eq as dispatchIsViaRuntime,eC as dispatchWaitViaRuntime,Q as isAndroidEscapeError,ed as isDirectIosSelectorFallbackError,ef as readSimpleIosSelectorTarget,ep as readTextForNode,V as recordIfSession,H as refSnapshotFlagGuardResponse,K as resolveSessionDevice,F as withSessionlessRunnerCleanup};
|
|
1
|
+
import{snapshotPresentationOptionsFromFlags as e,isMacOs as t,isApplePlatform as r,publicPlatformString as n,isIosFamily as s,buildSnapshotPresentationKey as o}from"./6849.js";import{setSessionSnapshot as i,createDaemonRuntimeSessionStore as a,context_contextFromFlags as l,captureSnapshot as c,dispatchCommand as u,requireCommandSupported as d,stopIosRunnerSession as f,errorResponse as p,resolveRunnerAppBundleId as m,getActiveAndroidSnapshotFreshness as g,noActiveSessionError as h,resolveTargetDevice as y,runAppleRunnerCommand as w}from"./9722.js";import{closeIosApp as S}from"./apps.js";import{emitDiagnostic as _}from"./8875.js";import{ensureDeviceReady as v,buildAppleRunnerRequestOptions as x}from"./8414.js";import{asAppError as k,normalizeError as I,AppError as A}from"./485.js";import{evaluateIsPredicate as P,isSupportedPredicate as N,localCommandPolicy as R,createAgentDevice as b}from"./7871.js";import{getAndroidAppState as q}from"./3340.js";import{splitIsSelectorArgs as C,normalizeType as M,prefersValueForReadableText as E,findNodeByLabel as D,extractNodeReadText as $}from"./893.js";import{resolveRectCenter as L,isSparseSnapshotQualityVerdict as O,parseWaitPositionals as K}from"./2948.js";import{parseFindArgs as F,tryParseSelectorChain as G}from"./587.js";async function V(e,t,r){let n=e.get(t),s=n?.device??await y(r??{});return n||await v(s),{session:n,device:s}}async function U(e,t,r){let n=!e&&s(t);try{return await r()}finally{n&&(await f(t.id),await T(t))}}async function T(e){let t=m();await S(e,t).catch(r=>{_({level:"debug",phase:"ios_sessionless_runner_host_close_failed",data:{deviceId:e.id,bundleId:t,error:r instanceof Error?r.message:String(r)}})})}function B(e,t,r,n){t&&e.recordAction(t,{command:r.command,positionals:r.positionals??[],flags:r.flags??{},result:n})}function H(e){let{session:t,sessionName:r,device:n,snapshot:s,appBundleId:o}=e;return t?{...t,snapshot:s,lastComparisonSafeSnapshot:s?.comparisonSafe===!0?s:t.lastComparisonSafeSnapshot}:{name:r,device:n,createdAt:Date.now(),appBundleId:o,snapshot:s,...s?.comparisonSafe===!0?{lastComparisonSafeSnapshot:s}:{},actions:[]}}function j(e,t={}){return{artifacts:function(e,t={}){let r=!0===t.plural?"do":"does";return{resolveInput:async()=>{throw new A("UNSUPPORTED_OPERATION",`${e} ${r} not resolve input artifacts`)},reserveOutput:async()=>{throw new A("UNSUPPORTED_OPERATION",`${e} ${r} not reserve output artifacts`)},createTempFile:async()=>{throw new A("UNSUPPORTED_OPERATION",`${e} ${r} not create temporary files`)}}}(e,t),policy:R()}}let z=[["snapshotDepth","--depth"],["snapshotScope","--scope"],["snapshotRaw","--raw"]];function Q(e,t){let r=function(e){if(!e)return[];let t=[];for(let[r,n]of z)void 0!==e[r]&&t.push(n);return t}(t);return 0===r.length?null:p("INVALID_ARGS",`${e} @ref does not support ${r.join(", ")}.`)}async function W(e,t){let r=await J(e);if(r)throw new A("COMMAND_FAILED",`press ${t} left ${e.appBundleId} and foregrounded ${r.foregroundPackage}. The tap likely escaped the app.`,r)}async function J(e){var t;if("android"!==e.device.platform||!e.appBundleId)return null;let r=await q(e.device),n=r.package?.trim();return n&&n!==e.appBundleId&&("com.android.settings"===(t=n)||"com.android.systemui"===t||"com.google.android.permissioncontroller"===t||t.includes("launcher"))?{expectedPackage:e.appBundleId,foregroundPackage:n,activity:r.activity,hint:"com.google.android.permissioncontroller"===n?"Dismiss or allow the permission prompt, then retry the smoke assertion.":"Use screenshot as visual truth, then take a fresh snapshot -i before retrying."}:null}function X(e){return"COMMAND_FAILED"===e.code&&"string"==typeof e.details?.expectedPackage&&"string"==typeof e.details?.foregroundPackage}function Y(e,t){let r=Array.isArray(e.selectorChain)?e.selectorChain:void 0,n=er(e),s=n?.kind==="ref"?en(n.ref):void 0,o=n?.kind==="selector"?n.selector:void 0,i={...s?{ref:s}:{},...o?{selector:o}:{},...r?{selectorChain:r}:{}};if("attrs"===t)return i;let a="string"==typeof e.text?e.text:"";return{...i,text:a,refLabel:function(e){let t=e.trim();if(!(!t||t.length>80||/[\r\n]/.test(t)))return t}(a)}}function Z(e){let t=er(e);return{...t?.kind==="ref"?{ref:en(t.ref)}:{},...t?.kind==="selector"?{selector:t.selector}:{},..."string"==typeof e.text?{text:e.text}:{},...e.node&&"object"==typeof e.node?{node:e.node}:{}}}function ee(e){let{selectorChain:t,...r}=e;return r}function et(e,t,r,n){let s=e.get(t);s&&e.recordAction(s,{command:r.command,positionals:r.positionals??[],flags:r.flags??{},result:n})}function er(e){let t=e.target;return t&&"object"==typeof t?"ref"===t.kind&&"string"==typeof t.ref?{kind:"ref",ref:t.ref}:"selector"===t.kind&&"string"==typeof t.selector?{kind:"selector",selector:t.selector}:void 0:void 0}function en(e){return e.startsWith("@")?e.slice(1):e}let es=["application","window","tabbar","scrollbar","image"],eo=new Set(["tab bar"]);async function ei(e,t){var r;if(t.ok||(r=t.error.message,!/^wait timed out for (?:selector|text): /i.test(r)))return t;let n=await ea(e).catch(()=>null);return n?p(t.error.code,`${t.error.message}. Current surface: ${n.summary}.`,{...t.error.details??{},currentSurface:n.details}):t}async function ea(e){let t=[...(await c({device:e.device,session:e.session,flags:{...e.req.flags,snapshotInteractiveOnly:!0},logPath:e.logPath??""})).snapshot.nodes].sort(ec),r=el(t,6,{includeIdentifiers:!0});if(0===r.length)return null;let n=el(t.filter(e=>!ef(e)),4,{includeIdentifiers:!1}),s=el(t.filter(ep),4,{includeIdentifiers:!0});return{summary:(n.length>0?n:r.slice(0,4)).join(", "),details:{labels:r,...s.length>0?{buttons:s}:{}}}}function el(e,t,r){let n=new Set,s=[];for(let o of e){let e=ed(o,r);if(!(!e||n.has(e))&&(n.add(e),s.push(e),s.length>=t))break}return s}function ec(e,t){var r,n;return eu(e)-eu(t)||(r=e,n=t,r.rect&&n.rect?r.rect.y-n.rect.y||r.rect.x-n.rect.x:r.rect?-1:n.rect?1:(r.depth??0)-(n.depth??0)||r.index-n.index)}function eu(e){let t=!!ed(e,{includeIdentifiers:!1});return 2*!!ef(e)+ +!t}function ed(e,t){let r=(t.includeIdentifiers?[e.label,e.value,e.identifier]:[e.label,e.value]).map(e=>"string"==typeof e?e.trim():"").find(e=>e.length>0);return r?r.replace(/\s+/g," ").slice(0,80):""}function ef(e){let t=M(`${e.type??""} ${e.role??""} ${e.subrole??""}`),r=`${e.label??""} ${e.value??""}`.trim().toLowerCase();return es.some(e=>t.includes(e))||eo.has(r)||r.endsWith(".fill")}function ep(e){return M(`${e.type??""} ${e.role??""} ${e.subrole??""}`).includes("button")}function em(e){var t;let{session:r,selectorExpression:n}=e;if(!r||!s(r.device)||r.postGestureStabilization)return null;let o=G(n);if(!o||1!==o.selectors.length)return null;let i=o.selectors[0];if(!i||1!==i.terms.length)return null;let a=i.terms[0];return a&&"string"==typeof a.value&&("id"===(t=a.key)||"label"===t||"text"===t||"value"===t)?{key:a.key,value:a.value,raw:i.raw}:null}function eg(e,t={}){let r=k(e);if("ELEMENT_NOT_FOUND"===r.code)return!0===t.allowElementNotFound;if("COMMAND_FAILED"!==r.code)return!1;let n=r.message.toLowerCase();return n.includes("fetch failed")||n.includes("timed out")||n.includes("timeout")||n.includes("runner did not accept connection")||n.includes("invalid runner response")}async function eh(e){let{device:t,node:r,flags:n,appBundleId:o,traceOutPath:i,surface:a,contextFromFlags:l}=e,c=$(r),d=L(r.rect);if(!d||s(t)&&c&&!E(r.type??""))return c;try{let e=await u(t,"read",[String(d.x),String(d.y)],void 0,{...l(n,o,i),surface:a}),s=e&&"object"==typeof e?e:void 0,f="string"==typeof s?.text?s.text:"";if(f.trim())return f;return _({level:"warn",phase:"interaction_read_fallback",data:{reason:"empty_backend_text",nodeRef:r.ref,surface:a,platform:t.platform}}),c}catch(e){return _({level:"warn",phase:"interaction_read_fallback",data:{reason:"backend_read_failed",nodeRef:r.ref,surface:a,platform:t.platform,error:e instanceof Error?e.message:String(e)}}),c}}function ey(t){let r,n,{session:s,sessionStore:a,sessionName:l}=t,c=0;return{capture:async u=>{var d,f;let p=Date.now(),m=(d=u,f=t.req.flags?.out,JSON.stringify({presentationKey:o(e(ek(d))),includeRects:!0===d.includeRects,outPath:d.outPath??f??null})),h=function(e){let{timestamp:t,lastSnapshotAt:r,lastSnapshotResult:n,lastSnapshotCacheKey:s,session:o,request:i,cacheKey:a}=e;if(!(i.cache?.forceFresh===!0||!n||s!==a||t-r>=750||g(o))&&!ex(o,i))return n}({timestamp:p,lastSnapshotAt:c,lastSnapshotResult:r,lastSnapshotCacheKey:n,session:s,request:u,cacheKey:m});if(h)return h;let y=function(t){var r,n;let{session:s,timestamp:i,request:a}=t,l=s?.snapshot;if(l&&(r=s,n=a,!(n.cache?.forceFresh===!0||n.cache?.useSessionSnapshot!==!0||g(r)||ex(r,n)))&&i-l.createdAt<750&&l.presentationKey===function(t){return o(e(ek(t)))}(a))return l}({session:s,timestamp:p,request:u});if(y)return c=y.createdAt,r={snapshot:y},n=m,r;let w=await ew({params:t,request:u}),S={snapshot:w};return function(e){let{session:t,sessionStore:r,sessionName:n,snapshot:s}=e;!t||O(s.snapshotQuality)||(i(t,s),r.set(n,t))}({session:s,sessionStore:a,sessionName:l,snapshot:w}),c=p,r=S,n=m,S}}}async function ew(e){var t;let{params:r,request:n}=e,s=await ev(r,n,n.snapshotScope);return n.recovery?.legacyIosSparse&&!(t=s).snapshotQuality&&"xctest"===t.backend&&1===t.nodes.length&&t.nodes[0]?.type==="Application"?await eS({runtimeParams:r,request:n,policy:n.recovery.legacyIosSparse}):n.recovery?.sparseVerdictQueryScope?.shouldScope&&O(s.snapshotQuality)?await e_({runtimeParams:r,request:n,policy:n.recovery.sparseVerdictQueryScope,snapshot:s}):s}async function eS(e){let{runtimeParams:t,request:r,policy:n}=e;try{return await ev(t,r,n.scope,!1)}catch(e){if(!n.shouldScope)throw e;return await ev(t,r,n.query,!1)}}async function e_(e){let{runtimeParams:t,request:r,policy:n,snapshot:s}=e;try{return await ev(t,r,n.query,!1)}catch{return s}}async function ev(e,t,r,n=t.flags?.snapshotInteractiveOnly){return(await c({device:e.device,session:e.session,flags:{...t.flags,snapshotInteractiveOnly:n},outPath:t.outPath??e.req.flags?.out,logPath:e.logPath??"",snapshotScope:r,includeRects:t.includeRects})).snapshot}function ex(e,t){return t.cache?.bypassForPostGestureStabilization===!0&&!!e?.postGestureStabilization}function ek(e){return void 0===e.snapshotScope?e.flags:{...e.flags,snapshotScope:e.snapshotScope}}function eI(e){return b({backend:function(e){let{req:t,session:r,device:s,logPath:o,sessionName:i,sessionStore:a}=e,c=ey({device:s,session:r,sessionStore:a,sessionName:i,req:t,logPath:o});return{platform:n(s),captureSnapshot:async(e,r)=>{var n;let o,i={...t.flags,...(n=r,o={},n?.interactiveOnly!==void 0&&(o.snapshotInteractiveOnly=n.interactiveOnly),n?.scope!==void 0&&(o.snapshotScope=n.scope),n?.depth!==void 0&&(o.snapshotDepth=n.depth),n?.raw!==void 0&&(o.snapshotRaw=n.raw),o)},a=r?.includeRects===!0,l=r?.scope??t.flags?.snapshotScope,u="wait"===t.command||"find"===t.command||a&&"web"===s.platform;return await c.capture({flags:i,snapshotScope:l,includeRects:a,cache:{forceFresh:u,useSessionSnapshot:!0,bypassForPostGestureStabilization:!0}})},readText:async(n,i)=>({text:await eh({device:s,node:i,flags:t.flags,appBundleId:r?.appBundleId,traceOutPath:r?.trace?.outPath,surface:r?.surface,contextFromFlags:e.contextFromFlags??((e,t,r)=>l(o??"",e,t,r))})}),findText:async(t,r)=>({found:await eP(e,r)})}}(e),...j("selector commands",{plural:!0}),sessions:a({sessionName:e.sessionName,getSession:()=>e.session,recordOptions:{includeSnapshot:!0},setRecord:t=>{e.session&&t.snapshot&&(i(e.session,t.snapshot),e.sessionStore.set(e.sessionName,e.session))}})})}async function eA(e,t){let r=e.sessionStore.get(e.sessionName);if(!r&&t.requireSession)return{ok:!1,response:h()};let n=r?.device??await y(e.req.flags??{});r||await v(n);let s=d(t.capability,n);return s?{ok:!1,response:s}:{ok:!0,runtime:eI({...e,session:r,device:n})}}async function eP(e,t){let r=await eN(e,t);if(null!==r)return r;let n=await eR(e,t);return null!==n?n:await eb(e,t)}async function eN(e,r){return t(e.device)&&e.session?.surface&&"app"!==e.session.surface?await eb(e,r):null}async function eR(e,t){var n,s,o;let i=r((n=e).device.platform)&&n.session?.appBundleId?{device:n.device,appBundleId:n.session.appBundleId,traceLogPath:n.session.trace?.outPath}:null;if(!i)return null;let a=await w(i.device,{command:"findText",text:t,appBundleId:i.appBundleId},(s=e,o=i,x({req:s.req,logPath:s.logPath,traceLogPath:o.traceLogPath})));return a?.found===!0}async function eb(e,t){return!!D((await eq(e)).nodes,t)}async function eq(e){let t=ey({device:e.device,session:e.session,sessionStore:e.sessionStore,sessionName:e.sessionName,req:e.req,logPath:e.logPath}),{snapshot:r}=await t.capture({flags:{...e.req.flags,snapshotInteractiveOnly:!1},cache:{forceFresh:!0,bypassForPostGestureStabilization:!0}});return r}async function eC(e){var t;let{req:r}=e;if("find"!==r.command)return null;let n=r.positionals??[];if(0===n.length)return p("INVALID_ARGS","find requires a locator or text");let s=F(n);if(!s.query)return p("INVALID_ARGS","find requires a value");if(r.flags?.findFirst&&r.flags?.findLast)return p("INVALID_ARGS","find accepts only one of --first or --last");let o=s.action;if("exists"!==(t=o)&&"wait"!==t&&"get_text"!==t&&"get_attrs"!==t)return null;let i=await eA(e,{requireSession:!1,capability:"find"});return i.ok?await eU(async()=>{let t=await i.runtime.selectors.find({session:e.sessionName,requestId:r.meta?.requestId,locator:s.locator,query:s.query,action:o,timeoutMs:s.timeoutMs});return et(e.sessionStore,e.sessionName,r,function(e,t){if("exists"===t)return{found:!0};if("wait"===t)return{found:!0,waitedMs:e.waitedMs};let r="string"==typeof e.ref?e.ref:void 0;return"get_attrs"===t?{ref:r,action:"get attrs"}:{ref:r,action:"get text",text:"string"==typeof e.text?e.text:""}}(t,o)),"found"===t.kind?{found:!0,..."number"==typeof t.waitedMs?{waitedMs:t.waitedMs}:{}}:{..."string"==typeof t.ref?{ref:t.ref}:{},..."string"==typeof t.text?{text:t.text}:{},...t.node&&"object"==typeof t.node?{node:t.node}:{}}}):i.response}async function eM(e){let{req:t}=e;if("get"!==t.command)return null;let r=t.positionals?.[0];if("text"!==r&&"attrs"!==r)return p("INVALID_ARGS","get only supports text or attrs");let n=function(e){let t=e.positionals?.[1]??"";if(t.startsWith("@"))return{ok:!0,target:{kind:"ref",ref:t,fallbackLabel:e.positionals.length>2?e.positionals.slice(2).join(" ").trim():""}};let r=e.positionals?.slice(1).join(" ").trim()??"";return r?{ok:!0,target:{kind:"selector",selector:r}}:{ok:!1,response:p("INVALID_ARGS","get requires @ref or selector expression")}}(t);if(!n.ok)return n.response;if("ref"===n.target.kind){let e=Q("get",t.flags);if(e)return e}if("selector"===n.target.kind){let t=await e$(e,r,n.target.selector);if(t)return t}let s=await eA(e,{requireSession:!0,capability:"get"});return s.ok?await eU(async()=>{let o=await s.runtime.selectors.get({session:e.sessionName,requestId:t.meta?.requestId,property:r,target:n.target});return et(e.sessionStore,e.sessionName,t,Y(o,r)),Z(o)}):s.response}async function eE(e){let{req:t}=e;if("is"!==t.command)return null;let r=(t.positionals?.[0]??"").toLowerCase();if(!N(r))return p("INVALID_ARGS","is requires predicate: visible|hidden|exists|editable|selected|text");let{split:n}=C(t.positionals??[]);if(!n)return p("INVALID_ARGS","is requires a selector expression");let s=n.rest.join(" ").trim();if("text"===r&&!s)return p("INVALID_ARGS","is text requires expected text value");if("text"!==r&&n.rest.length>0)return p("INVALID_ARGS",`is ${r} does not accept trailing values`);let o=await eL(e,r,n.selectorExpression,s);if(o)return o;let i=await eA(e,{requireSession:!0,capability:"is"});if(!i.ok)return i.response;let a=await eU(async()=>{let o=await i.runtime.selectors.is({session:e.sessionName,requestId:t.meta?.requestId,predicate:r,selector:n.selectorExpression,expectedText:s});return et(e.sessionStore,e.sessionName,t,o),ee(o)});return await eT(e,a,`is ${r}`)}async function eD(e){let{req:t,sessionName:r,sessionStore:n}=e,s=K(t.positionals??[]);if(!s)return p("INVALID_ARGS","wait requires a duration or text");let{session:o,device:i}=await V(n,r,t.flags);if("sleep"!==s.kind){let e=d("wait",i);if(e)return e}if("selector"===s.kind){let t=await eO({...e,session:o,device:i,selectorExpression:s.selectorExpression,timeoutMs:s.timeoutMs});if(t)return t}let a=async()=>{let a=eI({...e,session:o,device:i}),l=await eU(async()=>{let e=await a.selectors.wait({session:r,requestId:t.meta?.requestId,target:function(e,t){if("sleep"===e.kind)return{kind:"sleep",durationMs:e.durationMs};if("selector"===e.kind)return{kind:"selector",selector:e.selectorExpression,timeoutMs:e.timeoutMs};if("ref"===e.kind){if(!t?.snapshot)throw new A("INVALID_ARGS","Ref wait requires an existing snapshot in session.");return{kind:"ref",ref:e.rawRef,timeoutMs:e.timeoutMs}}if(!e.text)throw new A("INVALID_ARGS","wait requires text");return{kind:"text",text:e.text,timeoutMs:e.timeoutMs}}(s,o)});return et(n,r,t,e),{waitedMs:e.waitedMs,..."string"==typeof e.text?{text:e.text}:{},..."string"==typeof e.selector?{selector:e.selector}:{}}}),c=await ei({req:t,logPath:e.logPath,session:o,device:i},l);return await eT(e,c,"wait")};return"sleep"===s.kind?await a():await U(o,i,a)}async function e$(e,t,r){let n=e.sessionStore.get(e.sessionName),s=em({session:n,selectorExpression:r});if(!n||!s||"text"===t&&"id"!==s.key)return null;let o=await eG(e,n,s);if(eV(o))return o.response;if(!o)return null;let i=function(e,t,r){if(!r.found||!r.node)return null;let n={target:{kind:"selector",selector:t},node:r.node,selectorChain:[t]};return"attrs"===e?{kind:"attrs",...n}:"string"!=typeof r.text?null:{kind:"text",...n,text:r.text}}(t,s.raw,o);return i?(et(e.sessionStore,e.sessionName,e.req,Y(i,t)),{ok:!0,data:Z(i)}):null}async function eL(e,t,r,n){var s,o,i,a,l;let c;if("hidden"===t)return null;let u=await eK(e,r);if(eV(u))return u.response;if(!u?.result.found||!u.result.node)return null;let d="exists"===t?{predicate:t,pass:!0,selector:u.selector.raw,matches:1,selectorChain:[u.selector.raw]}:(s=t,o=n,i=u.selector.raw,a=u.session,c=P({predicate:s,node:l=u.result.node,nodes:[l],expectedText:o,platform:a.device.platform}),{predicate:s,pass:c.pass,selector:i,..."text"===s?{text:c.actualText}:{},selectorChain:[i]});return d?(et(e.sessionStore,e.sessionName,e.req,d),{ok:!0,data:ee(d)}):null}async function eO(e){let t=em({session:e.session,selectorExpression:e.selectorExpression});if(!e.session||!t)return null;let r=Date.now(),n=await eG(e,e.session,t);if(eV(n))return n.response;if(!n?.found)return null;let s={kind:"selector",selector:t.raw,waitedMs:Date.now()-r,selectorChain:[t.raw]};return et(e.sessionStore,e.sessionName,e.req,s),await ei({req:e.req,logPath:e.logPath,session:e.session,device:e.device},{ok:!0,data:s})}async function eK(e,t){let r=e.sessionStore.get(e.sessionName),n=em({session:r,selectorExpression:t});if(!r||!n)return null;let s=await eG(e,r,n);return eV(s)?s:s?{session:r,selector:n,result:s}:null}async function eF(e,t,r){let n=await w(t.device,{command:"querySelector",selectorKey:r.key,selectorValue:r.value,appBundleId:t.appBundleId},x({req:e.req,logPath:e.logPath,traceLogPath:t.trace?.outPath})),s=!0===n.found,o=function(e){let t=e.nodes;if(!Array.isArray(t))return;let r=t[0];if(r&&"object"==typeof r)return r}(n);return{found:s,..."string"==typeof n.text?{text:n.text}:{},...o?{node:o}:{}}}async function eG(e,t,r){try{return await eF(e,t,r)}catch(e){if(eg(e,{allowElementNotFound:!0}))return null;return{kind:"error",response:{ok:!1,error:I(e)}}}}function eV(e){return null!==e&&"kind"in e&&"error"===e.kind}async function eU(e){try{return{ok:!0,data:await e()}}catch(t){let e=k(t);return p(e.code,e.message,e.details)}}async function eT(e,t,r){var n;let s;if(t.ok)return t;let o=e.sessionStore.get(e.sessionName);if(!o)return t;try{s=await J(o)}catch{return t}return s?p(t.error.code,`${r} failed because ${"com.google.android.permissioncontroller"===(n=s).foregroundPackage?`Android permission dialog is blocking ${n.expectedPackage}`:`${n.foregroundPackage} is foreground instead of ${n.expectedPackage}`}.`,{...t.error.details??{},...s,blockedBy:"android_foreground_surface",originalMessage:t.error.message}):t}export{W as assertAndroidPressStayedInApp,H as buildSnapshotSession,j as createDaemonRuntimePolicy,ey as createSelectorCaptureRuntime,eC as dispatchFindReadOnlyViaRuntime,eM as dispatchGetViaRuntime,eE as dispatchIsViaRuntime,eD as dispatchWaitViaRuntime,X as isAndroidEscapeError,eg as isDirectIosSelectorFallbackError,em as readSimpleIosSelectorTarget,eh as readTextForNode,B as recordIfSession,Q as refSnapshotFlagGuardResponse,V as resolveSessionDevice,U as withSessionlessRunnerCleanup};
|
package/dist/src/selectors.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export declare function findSelectorChainMatch(nodes: SnapshotState['nodes'], chain: SelectorChain, options: {
|
|
2
|
-
platform: Platform;
|
|
2
|
+
platform: Platform | PublicPlatform;
|
|
3
3
|
requireRect?: boolean;
|
|
4
4
|
}): {
|
|
5
5
|
selectorIndex: number;
|
|
@@ -12,7 +12,7 @@ export declare function formatSelectorFailure(chain: SelectorChain, diagnostics:
|
|
|
12
12
|
unique?: boolean;
|
|
13
13
|
}): string;
|
|
14
14
|
|
|
15
|
-
export declare function isNodeEditable(node: SnapshotNode, platform: Platform): boolean;
|
|
15
|
+
export declare function isNodeEditable(node: SnapshotNode, platform: Platform | PublicPlatform): boolean;
|
|
16
16
|
|
|
17
17
|
export declare function isNodeVisible(node: SnapshotNode): boolean;
|
|
18
18
|
|
|
@@ -22,7 +22,11 @@ export declare function parseSelectorChain(expression: string): SelectorChain;
|
|
|
22
22
|
|
|
23
23
|
declare type Platform = (typeof PLATFORMS)[number];
|
|
24
24
|
|
|
25
|
-
declare const PLATFORMS: readonly ['
|
|
25
|
+
declare const PLATFORMS: readonly ['apple', 'android', 'linux', 'web'];
|
|
26
|
+
|
|
27
|
+
declare const PUBLIC_PLATFORMS: readonly ['ios', 'macos', 'android', 'linux', 'web'];
|
|
28
|
+
|
|
29
|
+
declare type PublicPlatform = (typeof PUBLIC_PLATFORMS)[number];
|
|
26
30
|
|
|
27
31
|
declare type RawSnapshotNode = {
|
|
28
32
|
index: number;
|
|
@@ -59,7 +63,7 @@ declare type Rect = {
|
|
|
59
63
|
};
|
|
60
64
|
|
|
61
65
|
export declare function resolveSelectorChain(nodes: SnapshotState['nodes'], chain: SelectorChain, options: {
|
|
62
|
-
platform: Platform;
|
|
66
|
+
platform: Platform | PublicPlatform;
|
|
63
67
|
requireRect?: boolean;
|
|
64
68
|
requireUnique?: boolean;
|
|
65
69
|
disambiguateAmbiguous?: boolean;
|