agent-device 0.12.8 → 0.13.0
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/README.md +20 -1
- package/dist/src/1974.js +2 -2
- package/dist/src/3883.js +1 -1
- package/dist/src/3918.js +28 -28
- package/dist/src/4829.js +1 -0
- package/dist/src/7847.js +1 -1
- package/dist/src/9076.js +1 -1
- package/dist/src/9152.js +1 -1
- package/dist/src/9323.js +5 -5
- package/dist/src/9542.js +2 -2
- package/dist/src/989.js +1 -1
- package/dist/src/bin.js +33 -33
- package/dist/src/commands/index.d.ts +4 -1
- package/dist/src/contracts.d.ts +3 -1
- package/dist/src/daemon.js +13 -13
- package/dist/src/index.d.ts +22 -11
- package/dist/src/metro.d.ts +9 -11
- package/package.json +20 -3
- package/skills/agent-device/SKILL.md +2 -1
- package/skills/agent-device/references/debugging.md +3 -1
- package/skills/agent-device/references/exploration.md +5 -2
- package/skills/agent-device/references/remote-tenancy.md +9 -6
- package/skills/agent-device/references/verification.md +6 -2
- package/skills/dogfood/SKILL.md +1 -0
- package/skills/react-devtools/SKILL.md +53 -0
- package/skills/react-devtools/references/commands.md +91 -0
- package/skills/react-devtools/references/profiling.md +74 -0
package/dist/src/9323.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import{URL as t}from"node:url";import{whichCmd as e,runCmd as n}from"./9818.js";import{asAppError as a,AppError as r}from"./9152.js";import{ensureAndroidSdkPathConfigured as i}from"./8164.js";function o(t,e){return["-s",t.id,...e]}async function s(){if(await i(),!await e("adb"))throw new r("TOOL_MISSING","adb not found in PATH")}function l(t,e){let n=`${t}
|
|
2
|
-
${e}`.toLowerCase();return n.includes("no shell command implementation")||n.includes("unknown command")}
|
|
3
|
-
`:
|
|
4
|
-
</map>`)}function
|
|
5
|
-
${e}`.toLowerCase();return["run-as: package not debuggable","run-as: permission denied","run-as: package is unknown","run-as: unknown package","is unknown","is not an application","could not set capabilities"].some(t=>n.includes(t))}export{o as adbArgs,
|
|
1
|
+
import{URL as t}from"node:url";import{whichCmd as e,runCmd as n}from"./9818.js";import{asAppError as a,AppError as r}from"./9152.js";import{ensureAndroidSdkPathConfigured as i}from"./8164.js";import"./4829.js";function o(t,e){return["-s",t.id,...e]}async function s(){if(await i(),!await e("adb"))throw new r("TOOL_MISSING","adb not found in PATH")}function l(t,e){let n=`${t}
|
|
2
|
+
${e}`.toLowerCase();return n.includes("no shell command implementation")||n.includes("unknown command")}let u=/\.(?:apk|aab)$/i,d=/^[A-Za-z_][\w]*(\.[A-Za-z_][\w]*)+$/;function c(t){var e,n;let a=t.trim();return 0===a.length?"other":u.test(a)?a.includes("/")||a.includes("\\")||a.startsWith(".")||a.startsWith("~")||(e=a,!d.test(e))?"binary":"package":(n=a,d.test(n))?"package":"other"}function p(t){return`Android runtime hints require an installed package name, not "${t}". Install or reinstall the app first, then relaunch by package.`}let m=["AGENT_DEVICE_IOS_SIMULATOR_DEVICE_SET","IOS_SIMULATOR_DEVICE_SET"],f=["AGENT_DEVICE_ANDROID_DEVICE_ALLOWLIST","ANDROID_DEVICE_ALLOWLIST"];function h(t){return t?.trim()||void 0}function w(t,e){for(let n of t){let t=h(e[n]);if(t)return t}}function _(t,e=process.env){return h(t)??w(m,e)}function A(t){return new Set(t.split(/[\s,]+/).map(t=>t.trim()).filter(Boolean))}function g(t,e=process.env){let n=h(t)??w(f,e);if(n)return A(n)}function b(t,e={}){let n=_(e.simulatorSetPath);return n?["simctl","--set",n,...t]:["simctl",...t]}function S(t,e){return"ios"!==t.platform||"simulator"!==t.kind?["simctl",...e]:b(e,{simulatorSetPath:t.simulatorSetPath})}let I="shared_prefs/ReactNativeDevPrefs.xml",v="debug_http_host",C="dev_server_https",$="RCT_jsLocation",E="RCT_packager_scheme",R="React Native runtime hints require adb run-as access to the app sandbox. Verify the app is debuggable and the selected package/device are correct.",k='<?xml version="1.0" encoding="utf-8" standalone="yes" ?>\n<map>\n</map>\n';function y(t){return void 0!==D(t)}function D(e){if(!e)return;let n=K(e.metroHost),a=q(e.metroPort),i="http",o=K(e.bundleUrl);if(o){var s;let e;try{e=new t(o)}catch(t){throw new r("INVALID_ARGS",`Invalid runtime bundle URL: ${o}`,{},t)}("http:"===e.protocol||"https:"===e.protocol)&&(n??=K(e.hostname),a??=q(e.port.length>0?Number(e.port):"https:"===(s=e.protocol)?443:"http:"===s?80:void 0),i="https:"===e.protocol?"https":"http")}if(n&&a)return{host:n,port:a,scheme:i}}async function N(t){let{device:e,appId:n,runtime:a}=t;if(!n)return;let r=D(a);if(r){if("android"===e.platform)return void await L(e,n,r);"ios"===e.platform&&"simulator"===e.kind&&await P(e,n,r)}}async function x(t){let{device:e,appId:n}=t;if(n){if("android"===e.platform)return void await T(e,n);"ios"===e.platform&&"simulator"===e.kind&&await F(e,n)}}async function L(t,e,n){var a,r,i,o,s,l;let u,d;G(e);let c=(a=await O(t,e),r=v,i=`${n.host}:${n.port}`,u=` <string name="${W(r)}">${W(i)}</string>`,V(U(a,r),u));o=c,s=C,l="https"===n.scheme,d=` <boolean name="${W(s)}" value="${l?"true":"false"}" />`,c=V(U(o,s),d),await M(t,e,c)}async function T(t,e){G(e);let n=await O(t,e),a=U(n,v),r=U(a,C);r!==n&&await M(t,e,r)}async function O(t,e){let a=await n("adb",o(t,["shell","run-as",e,"cat",I]),{allowFailure:!0});return 0!==a.exitCode?k:H(a.stdout)}async function M(t,e,i){let s=o(t,["shell","run-as",e,"id"]),l=await n("adb",s,{allowFailure:!0});if(0!==l.exitCode){let t=z(l.stdout,l.stderr);throw new r("COMMAND_FAILED",t?`Failed to access Android app sandbox for ${e}`:`Failed to probe Android app sandbox for ${e}`,{package:e,cmd:"adb",args:s,stdout:l.stdout,stderr:l.stderr,exitCode:l.exitCode,hint:t?R:"adb shell run-as probe failed. Check adb connectivity and that the device is reachable. Inspect stderr/details for more information."})}try{await n("adb",o(t,["shell","run-as",e,"mkdir","-p","shared_prefs"])),await n("adb",o(t,["shell","run-as",e,"tee",I]),{stdin:i.trimEnd()})}catch(i){let t=a(i);if("TOOL_MISSING"===t.code)throw t;let n=z("string"==typeof t.details?.stdout?t.details.stdout:"","string"==typeof t.details?.stderr?t.details.stderr:"");throw new r("COMMAND_FAILED",n?`Failed to access Android app sandbox for ${e}`:`Failed to write Android runtime hints for ${e}`,{...t.details??{},package:e,cmd:"adb",phase:"write-runtime-hints",hint:n?R:"adb run-as succeeded, but writing ReactNativeDevPrefs.xml failed. Inspect stderr/details for the failing shell command."},t)}}async function P(t,e,a){await n("xcrun",S(t,["spawn",t.id,"defaults","write",e,$,"-string",`${a.host}:${a.port}`])),await n("xcrun",S(t,["spawn",t.id,"defaults","write",e,E,"-string",a.scheme]))}async function F(t,e){await n("xcrun",S(t,["spawn",t.id,"defaults","delete",e,$]),{allowFailure:!0}),await n("xcrun",S(t,["spawn",t.id,"defaults","delete",e,E]),{allowFailure:!0})}function H(t){let e=t.trim();return e.includes("<map")&&e.includes("</map>")?`${e}
|
|
3
|
+
`:k}function V(t,e){return H(t).replace("</map>",`${e}
|
|
4
|
+
</map>`)}function U(t,e){let n=e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&");return H(t).replace(RegExp(`^\\s*<string name="${n}">[\\s\\S]*?<\\/string>\\n?`,"m"),"").replace(RegExp(`^\\s*<boolean name="${n}" value="(?:true|false)"\\s*\\/?>\\n?`,"m"),"")}function K(t){let e=t?.trim();return e&&e.length>0?e:void 0}function G(t){if("binary"!==c(t))return;let e=p(t);throw new r("INVALID_ARGS",e,{package:t,hint:e})}function q(t){if(Number.isInteger(t)&&!(t<=0)&&!(t>65535))return t}function W(t){return t.replaceAll("&","&").replaceAll("<","<").replaceAll(">",">").replaceAll('"',""").replaceAll("'","'")}function z(t,e){let n=`${t}
|
|
5
|
+
${e}`.toLowerCase();return["run-as: package not debuggable","run-as: permission denied","run-as: package is unknown","run-as: unknown package","is unknown","is not an application","could not set capabilities"].some(t=>n.includes(t))}export{o as adbArgs,N as applyRuntimeHintsToApp,b as buildSimctlArgs,S as buildSimctlArgsForDevice,c as classifyAndroidAppTarget,x as clearRuntimeHintsFromApp,s as ensureAdb,p as formatAndroidInstalledPackageRequiredMessage,y as hasRuntimeTransportHints,l as isClipboardShellUnsupported,A as parseSerialAllowlist,g as resolveAndroidSerialAllowlist,_ as resolveIosSimulatorDeviceSetPath,D as resolveRuntimeTransportHints,K as trimRuntimeValue};
|
package/dist/src/9542.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import e from"node:net";import t from"node:http";import a from"node:https";import r from"node:fs";import o from"node:path";import n from"node:os";import{createHash as i,randomUUID as s}from"node:crypto";import{AppError as l}from"./9152.js";import{runCmdSync as d,runCmd as c,runCmdDetached as u}from"./9818.js";import{createRequestId as p,buildDeviceIdentifiers as m,resolveDaemonPaths as f,readVersion as h,findProjectRoot as y,resolveDaemonServerMode as w,computeDaemonCodeSignature as v,withDiagnosticTimer as g,resolveDaemonTransportPreference as I,buildAppIdentifiers as b,emitDiagnostic as A}from"./8564.js";import{isAgentDeviceDaemonProcess as M,stopProcessForTakeover as _}from"./3883.js";import{tryParseSelectorChain as k}from"./7847.js";import{prepareMetroRuntime as S}from"./1974.js";let D="sha256";async function P(e){let t=await E(e.localPath,e.platform),a=e.baseUrl.endsWith("/")?e.baseUrl:`${e.baseUrl}/`;try{let r=await x({normalizedBase:a,token:e.token,artifact:t});if(r?.kind==="cache-hit")return r.uploadId;if(r?.kind==="direct-upload")try{return await C(t.payloadPath,r),await O({normalizedBase:a,token:e.token,uploadId:r.uploadId})}catch{}return await U({normalizedBase:a,token:e.token,artifact:t})}finally{t.cleanup()}}async function E(e,t){var a,n,i;let s,l=r.statSync(e),d=o.basename(e),c=l.isDirectory(),u=("ios"===(a=t)||"android"===a?a:void 0)??(n=e,i=l,s=n.toLowerCase(),i.isDirectory()&&s.endsWith(".app")||s.endsWith(".ipa")?"ios":s.endsWith(".apk")||s.endsWith(".aab")?"android":void 0),p=[];try{let t=c?await T(e,p):e,a=r.statSync(t);return{payloadPath:t,fileName:d,artifactType:c?"app-bundle":"file",platform:u,contentType:c?"application/gzip":"application/octet-stream",sha256:await L(t),sizeBytes:a.size,cleanup:()=>N(p)}}catch(e){throw N(p),e}}async function T(e,t){let a=r.mkdtempSync(o.join(n.tmpdir(),`agent-device-upload-${s()}-`));t.push(a);let i=o.join(a,`${o.basename(e)}.tar.gz`);return await c("tar",["czf",i,"-C",o.dirname(e),o.basename(e)]),i}function N(e){for(let t of e)r.rmSync(t,{recursive:!0,force:!0})}async function U(e){let{normalizedBase:t,token:a,artifact:r}=e,o=new URL("upload",t),n={"content-type":r.contentType,"x-artifact-type":r.artifactType,"x-artifact-filename":r.fileName,"x-artifact-hash":r.sha256,"x-artifact-hash-algorithm":D,"transfer-encoding":"chunked"};a&&(n.authorization=`Bearer ${a}`,n["x-agent-device-token"]=a);let i=await R({url:o,method:"POST",headers:n,payloadPath:r.payloadPath,timeoutMessage:"Artifact upload timed out",timeoutHint:"The upload to the remote daemon exceeded the 5-minute timeout.",errorMessage:"Failed to upload artifact to remote daemon",errorHint:"Verify the remote daemon is reachable and supports artifact uploads."});try{let e=JSON.parse(i.body);if(!e.ok||!e.uploadId)throw new l("COMMAND_FAILED",`Upload failed: ${i.body}`);return e.uploadId}catch(e){if(e instanceof l)throw e;throw new l("COMMAND_FAILED",`Invalid upload response: ${i.body}`)}}async function x(e){let t=new URL("upload/preflight",e.normalizedBase),a={"content-type":"application/json"};e.token&&(a.authorization=`Bearer ${e.token}`,a["x-agent-device-token"]=e.token);let r=await fetch(t,{method:"POST",headers:a,signal:AbortSignal.timeout(3e4),body:JSON.stringify({sha256:e.artifact.sha256,fileName:e.artifact.fileName,sizeBytes:e.artifact.sizeBytes,artifactType:e.artifact.artifactType,...e.artifact.platform?{platform:e.artifact.platform}:{},contentType:e.artifact.contentType})}).catch(()=>void 0);if(r?.ok)return function(e){var t;if(!e||"object"!=typeof e||!0!==e.ok||"string"!=typeof e.uploadId)return;if(!0===e.cacheHit)return{kind:"cache-hit",uploadId:e.uploadId};let a=e.upload;if(!a||"string"!=typeof a.url)return;let r=a.headers??{};if(!(!(t=r)||"object"!=typeof t||Array.isArray(t))&&Object.values(t).every(e=>"string"==typeof e))return{kind:"direct-upload",uploadId:e.uploadId,url:a.url,headers:r}}(await r.json().catch(()=>void 0))}async function C(e,t){let a=await R({url:new URL(t.url),method:"PUT",headers:t.headers,payloadPath:e,timeoutMessage:"Direct artifact upload timed out",timeoutHint:"The direct upload ticket did not accept the artifact within the timeout.",errorMessage:"Failed to upload artifact with direct upload ticket"});if(a.statusCode<200||a.statusCode>=300)throw new l("COMMAND_FAILED","Direct artifact upload failed",{statusCode:a.statusCode,statusMessage:a.statusMessage})}async function R(e){let o="https:"===e.url.protocol?a:t;return await new Promise((t,a)=>{let n=o.request({protocol:e.url.protocol,host:e.url.hostname,port:e.url.port,method:e.method,path:e.url.pathname+e.url.search,headers:e.headers},e=>{let a="";e.setEncoding("utf8"),e.on("data",e=>{a+=e}),e.on("end",()=>{clearTimeout(i),t({statusCode:e.statusCode??500,statusMessage:e.statusMessage,body:a})})}),i=setTimeout(()=>{n.destroy(),a(new l("COMMAND_FAILED",e.timeoutMessage,{timeoutMs:3e5,...e.timeoutHint?{hint:e.timeoutHint}:{}}))},3e5);n.on("error",t=>{clearTimeout(i),a(new l("COMMAND_FAILED",e.errorMessage,e.errorHint?{hint:e.errorHint}:{},t))}),n.on("close",()=>clearTimeout(i));let s=r.createReadStream(e.payloadPath);s.pipe(n),s.on("error",e=>{n.destroy(),a(new l("COMMAND_FAILED","Failed to read local artifact",{},e))})})}async function O(e){let t=new URL("upload/finalize",e.normalizedBase),a={"content-type":"application/json"};e.token&&(a.authorization=`Bearer ${e.token}`,a["x-agent-device-token"]=e.token);let r=await fetch(t,{method:"POST",headers:a,signal:AbortSignal.timeout(3e4),body:JSON.stringify({uploadId:e.uploadId})}).catch(e=>{throw new l("COMMAND_FAILED","Failed to finalize direct artifact upload",{},e)});if(!r.ok)throw new l("COMMAND_FAILED","Direct artifact upload finalize failed",{status:r.status,statusText:r.statusText});let o=await r.json().catch(()=>void 0);if(!o?.ok||!o.uploadId)throw new l("COMMAND_FAILED","Invalid upload finalize response");return o.uploadId}async function L(e){let t=i(D);return await new Promise((a,o)=>{r.createReadStream(e).on("data",e=>t.update(e)).on("error",e=>{o(new l("COMMAND_FAILED","Failed to read local artifact",{},e))}).on("end",a)}),t.digest("hex")}let F=ev(),B=function(e=process.env.AGENT_DEVICE_DAEMON_STARTUP_TIMEOUT_MS){if(!e)return 15e3;let t=Number(e);return Number.isFinite(t)?Math.max(1e3,Math.floor(t)):15e3}(),q=function(e=process.env.AGENT_DEVICE_DAEMON_STARTUP_ATTEMPTS){if(!e)return 2;let t=Number(e);return Number.isFinite(t)?Math.min(5,Math.max(1,Math.floor(t))):2}(),$=["xcodebuild .*AgentDeviceRunnerUITests/RunnerTests/testCommand","xcodebuild .*AgentDeviceRunner\\.env\\.session-","xcodebuild build-for-testing .*ios-runner/AgentDeviceRunner/AgentDeviceRunner\\.xcodeproj"],j=new e.BlockList;async function z(t){let a=t.meta?.requestId??p(),r=!!(t.meta?.debug||t.flags?.verbose),o=function(t){let a,r=t.flags?.stateDir??process.env.AGENT_DEVICE_STATE_DIR,o=function(e){let t;if(e){try{t=new URL(e)}catch(t){throw new l("INVALID_ARGS","Invalid daemon base URL",{daemonBaseUrl:e},t instanceof Error?t:void 0)}if("http:"!==t.protocol&&"https:"!==t.protocol)throw new l("INVALID_ARGS","Daemon base URL must use http or https",{daemonBaseUrl:e});return t.toString().replace(/\/+$/,"")}}(t.flags?.daemonBaseUrl??process.env.AGENT_DEVICE_DAEMON_BASE_URL),n=t.flags?.daemonAuthToken??process.env.AGENT_DEVICE_DAEMON_AUTH_TOKEN;var i=o,s=n;if(!(!i||"localhost"===(a=new URL(i).hostname.trim().toLowerCase().replace(/^\[(.*)\]$/,"$1"))||(e.isIPv4(a)?j.check(a,"ipv4"):!!e.isIPv6(a)&&j.check(a,"ipv6")))&&("string"!=typeof s||!(s.trim().length>0)))throw new l("INVALID_ARGS","Remote daemon base URL for non-loopback hosts requires daemon authentication",{daemonBaseUrl:i,hint:"Provide --daemon-auth-token or AGENT_DEVICE_DAEMON_AUTH_TOKEN when using a non-loopback remote daemon URL."});let d=t.flags?.daemonTransport??process.env.AGENT_DEVICE_DAEMON_TRANSPORT,c=I(d);if(o&&"socket"===c)throw new l("INVALID_ARGS","Remote daemon base URL only supports HTTP transport. Remove --daemon-transport socket.",{daemonBaseUrl:o});let u=w(t.flags?.daemonServerMode??process.env.AGENT_DEVICE_DAEMON_SERVER_MODE??("dual"===d?"dual":void 0));return{paths:f(r),transportPreference:c,serverMode:u,remoteBaseUrl:o,remoteAuthToken:n}}(t),n=function(e,t=process.env.AGENT_DEVICE_DAEMON_TIMEOUT_MS){if("test"!==e)return ev(t)}(t.command),i=await g("daemon_startup",async()=>await J(o),{requestId:a,session:t.session}),s=await H(t,i),d={...t,positionals:s.positionals,flags:s.flags,token:i.token,meta:{...t.meta??{},requestId:a,debug:r,cwd:t.meta?.cwd,tenantId:t.meta?.tenantId??t.flags?.tenant,runId:t.meta?.runId??t.flags?.runId,leaseId:t.meta?.leaseId??t.flags?.leaseId,sessionIsolation:t.meta?.sessionIsolation??t.flags?.sessionIsolation,lockPolicy:t.meta?.lockPolicy,lockPlatform:t.meta?.lockPlatform,...s.uploadedArtifactId?{uploadedArtifactId:s.uploadedArtifactId}:{},...s.clientArtifactPaths?{clientArtifactPaths:s.clientArtifactPaths}:{},...s.installSource?{installSource:s.installSource}:{}}};return A({level:"info",phase:"daemon_request_prepare",data:{requestId:a,command:t.command,session:t.session}}),await g("daemon_request",async()=>await es(i,d,o.transportPreference,n),{requestId:a,command:t.command})}async function H(e,t){let a,n=[...e.positionals??[]],i=e.flags?{...e.flags}:void 0,s=e.meta?.installSource,l={};if(ef(t)){let r=function(e,t){if("screenshot"===e.command){let a=V(e,"path",".png");return t[0]?{field:"path",localPath:a,positionalIndex:0,positionalPath:K("screenshot",".png")}:{field:"path",localPath:a,positionalIndex:0,flagPath:K("screenshot",".png")}}if("record"===e.command&&"start"===(t[0]??"").toLowerCase()){let t=V(e,"outPath",".mp4",1);return{field:"outPath",localPath:t,positionalIndex:1,positionalPath:K("recording",o.extname(t)||".mp4")}}return null}(e,n);r&&(void 0!==r.positionalPath&&(n[r.positionalIndex]=r.positionalPath),void 0!==r.flagPath&&((i??={}).out=r.flagPath),l[r.field]=r.localPath);let d=await G(e,t);d&&(s=d.installSource,a=d.uploadedArtifactId??a)}if(!ef(t)||"install"!==e.command&&"reinstall"!==e.command||n.length<2)return{positionals:n,flags:i,installSource:s,uploadedArtifactId:a,...Object.keys(l).length>0?{clientArtifactPaths:l}:{}};let d=n[1];if(d.startsWith("remote:"))return n[1]=d.slice(7),{positionals:n,flags:i,...Object.keys(l).length>0?{clientArtifactPaths:l}:{}};let c=o.isAbsolute(d)?d:o.resolve(e.meta?.cwd??process.cwd(),d);return r.existsSync(c)?{positionals:n,flags:i,installSource:s,uploadedArtifactId:a=await P({localPath:c,baseUrl:t.baseUrl,token:t.token,platform:e.flags?.platform}),...Object.keys(l).length>0?{clientArtifactPaths:l}:{}}:{positionals:n,flags:i,...Object.keys(l).length>0?{clientArtifactPaths:l}:{}}}async function G(e,t){let a=e.meta?.installSource;if("install_source"!==e.command||!a||"path"!==a.kind)return null;let n=a.path.trim();if(!n)return{installSource:a};if(n.startsWith("remote:"))return{installSource:{...a,path:n.slice(7)}};let i=o.isAbsolute(n)?n:o.resolve(e.meta?.cwd??process.cwd(),n);if(!r.existsSync(i))return{installSource:{...a,path:i}};let s=await P({localPath:i,baseUrl:t.baseUrl,token:t.token,platform:e.flags?.platform});return{installSource:{...a,path:i},uploadedArtifactId:s}}function V(e,t,a,r=0){let n=e.positionals?.[r]??e.flags?.out,i=`${"path"===t?"screenshot":"recording"}-${Date.now()}${a}`,s=n&&n.trim().length>0?n:i;return o.isAbsolute(s)?s:o.resolve(e.meta?.cwd??process.cwd(),s)}function K(e,t){let a=t.startsWith(".")?t:`.${t}`;return o.posix.join("/tmp",`agent-device-${e}-${Date.now()}-${Math.random().toString(36).slice(2,8)}${a}`)}async function J(e){let t;if(e.remoteBaseUrl){let t={transport:"http",token:e.remoteAuthToken??"",pid:0,baseUrl:e.remoteBaseUrl};if(await eo(t,"http"))return t;throw new l("COMMAND_FAILED","Remote daemon is unavailable",{daemonBaseUrl:e.remoteBaseUrl,hint:"Verify AGENT_DEVICE_DAEMON_BASE_URL points to a reachable daemon with GET /health and POST /rpc."})}let a=Z(e.paths.infoPath),r=h(),o=v((t=ei()).useSrc?t.srcPath:t.distPath,t.root),n=!!a&&await eo(a,e.transportPreference);if(a&&a.version===r&&a.codeSignature===o&&n)return a;a&&(a.version!==r||a.codeSignature!==o||!n)&&(await Y(a),er(e.paths.infoPath)),function(e){let t=et(e);if(!t.hasLock||t.hasInfo)return;let a=ee(e.lockPath);if(!a)return er(e.lockPath);M(a.pid,a.processStartTime)||er(e.lockPath)}(e.paths);let i=0;for(let t=1;t<=q;t+=1){await en(e);let a=await W(B,e);if(a)return a;if(await X(e.paths)){i+=1;continue}let r=et(e.paths);if(!(t<q))break;if(!r.hasInfo&&!r.hasLock){await Q(150);continue}}let s=et(e.paths);throw new l("COMMAND_FAILED","Failed to start daemon",{kind:"daemon_startup_failed",infoPath:e.paths.infoPath,lockPath:e.paths.lockPath,startupTimeoutMs:B,startupAttempts:q,lockRecoveryCount:i,metadataState:s,hint:function(e,t=f(process.env.AGENT_DEVICE_STATE_DIR)){return e.hasLock&&!e.hasInfo?`Detected ${t.lockPath} without ${t.infoPath}. If no agent-device daemon process is running, delete ${t.lockPath} and retry.`:e.hasLock&&e.hasInfo?`Daemon metadata may be stale. If no agent-device daemon process is running, delete ${t.infoPath} and ${t.lockPath}, then retry.`:`Daemon metadata is missing or stale. Delete ${t.infoPath} if present and retry.`}(s,e.paths)})}async function W(e,t){let a=Date.now();for(;Date.now()-a<e;){let e=Z(t.paths.infoPath);if(e&&await eo(e,t.transportPreference))return e;await new Promise(e=>setTimeout(e,100))}return null}async function Q(e){await new Promise(t=>setTimeout(t,e))}async function X(e){let t=et(e);if(!t.hasLock||t.hasInfo)return!1;let a=ee(e.lockPath);return a&&M(a.pid,a.processStartTime)&&await _(a.pid,{termTimeoutMs:3e3,killTimeoutMs:1e3,expectedStartTime:a.processStartTime}),er(e.lockPath),!0}async function Y(e){await _(e.pid,{termTimeoutMs:3e3,killTimeoutMs:1e3,expectedStartTime:e.processStartTime})}function Z(e){let t=ea(e);if(!t||"object"!=typeof t)return null;let a="string"==typeof t.token&&t.token.length>0?t.token:null;if(!a)return null;let r=Number.isInteger(t.port)&&Number(t.port)>0,o=Number.isInteger(t.httpPort)&&Number(t.httpPort)>0;if(!r&&!o)return null;let n=t.transport,i="string"==typeof t.version?t.version:void 0,s="string"==typeof t.codeSignature?t.codeSignature:void 0,l="string"==typeof t.processStartTime?t.processStartTime:void 0,d=Number.isInteger(t.pid)&&Number(t.pid)>0;return{token:a,port:r?Number(t.port):void 0,httpPort:o?Number(t.httpPort):void 0,transport:"socket"===n||"http"===n||"dual"===n?n:void 0,pid:d?Number(t.pid):0,version:i,codeSignature:s,processStartTime:l}}function ee(e){let t=ea(e);return t&&"object"==typeof t&&Number.isInteger(t.pid)&&Number(t.pid)>0?{pid:Number(t.pid),processStartTime:"string"==typeof t.processStartTime?t.processStartTime:void 0,startedAt:"number"==typeof t.startedAt?t.startedAt:void 0}:null}j.addSubnet("127.0.0.0",8,"ipv4"),j.addAddress("::1","ipv6"),j.addSubnet("::ffff:127.0.0.0",104,"ipv6");function et(e){return{hasInfo:r.existsSync(e.infoPath),hasLock:r.existsSync(e.lockPath)}}function ea(e){if(!r.existsSync(e))return null;try{return JSON.parse(r.readFileSync(e,"utf8"))}catch{return null}}function er(e){try{r.existsSync(e)&&r.unlinkSync(e)}catch{}}async function eo(r,o){var n;return"http"===el(r,o)?await function(e){let r=e.baseUrl?eh(e.baseUrl,"health"):e.httpPort?`http://127.0.0.1:${e.httpPort}/health`:null;if(!r)return Promise.resolve(!1);let o=new URL(r),n="https:"===o.protocol?a:t,i=e.baseUrl?3e3:500;return new Promise(e=>{let t=n.request({protocol:o.protocol,host:o.hostname,port:o.port,path:o.pathname+o.search,method:"GET",timeout:i},t=>{t.resume(),e((t.statusCode??500)<500)});t.on("timeout",()=>{t.destroy(),e(!1)}),t.on("error",()=>{e(!1)}),t.end()})}(r):await ((n=r.port)?new Promise(t=>{let a=e.createConnection({host:"127.0.0.1",port:n},()=>{a.destroy(),t(!0)});a.on("error",()=>{t(!1)})}):Promise.resolve(!1))}async function en(e){let t=ei(),a=t.useSrc?["--experimental-strip-types",t.srcPath]:[t.distPath],r={...process.env,AGENT_DEVICE_STATE_DIR:e.paths.baseDir,AGENT_DEVICE_DAEMON_SERVER_MODE:e.serverMode};u(process.execPath,a,{env:r})}function ei(){let e=y(),t=o.join(e,"dist","src","daemon.js"),a=o.join(e,"src","daemon.ts"),n=r.existsSync(t),i=r.existsSync(a);if(!n&&!i)throw new l("COMMAND_FAILED","Daemon entry not found",{distPath:t,srcPath:a});return{root:e,distPath:t,srcPath:a,useSrc:process.execArgv.includes("--experimental-strip-types")?i:!n&&i}}async function es(e,t,a,r){return"http"===el(e,a)?await em(e,t,r):await ep(e,t,r)}function el(e,t){if(e.baseUrl){if("socket"===t)throw new l("COMMAND_FAILED","Remote daemon endpoint only supports HTTP transport",{daemonBaseUrl:e.baseUrl});return"http"}if("http"===t||"socket"===t){var a=e,r=t;if(ed(a,r))return r;throw new l("COMMAND_FAILED","http"===r?"Daemon HTTP endpoint is unavailable":"Daemon socket endpoint is unavailable")}let o=("socket"===e.transport||"dual"===e.transport?["socket","http"]:["http","socket"]).find(t=>ed(e,t));if(o)return o;throw new l("COMMAND_FAILED","Daemon metadata has no reachable transport")}function ed(e,t){return"http"===t?!!e.httpPort:!!e.port}function ec(e,t,a,r,o,n){let i=o?{terminated:0}:function(){let e=0;try{for(let t of $){let a=d("pkill",["-f",t],{allowFailure:!0});0===a.exitCode&&(e+=1)}return{terminated:e}}catch(t){return{terminated:e,error:t instanceof Error?t.message:String(t)}}}(),s=o?{forcedKill:!1}:function(e,t){let a=!1;try{M(e.pid,e.processStartTime)&&(process.kill(e.pid,"SIGKILL"),a=!0)}catch{_(e.pid,{termTimeoutMs:3e3,killTimeoutMs:1e3,expectedStartTime:e.processStartTime})}finally{er(t.infoPath),er(t.lockPath)}return{forcedKill:a}}(e,t);return A({level:"error",phase:"daemon_request_timeout",data:{timeoutMs:n,requestId:a,command:r,timedOutRunnerPidsTerminated:i.terminated,timedOutRunnerCleanupError:i.error,daemonPidReset:o?void 0:e.pid,daemonPidForceKilled:o?void 0:s.forcedKill,daemonBaseUrl:e.baseUrl}}),new l("COMMAND_FAILED","Daemon request timed out",{timeoutMs:n,requestId:a,hint:o?"Retry with --debug and verify the remote daemon URL, auth token, and remote host logs.":"Retry with --debug and check daemon diagnostics logs. Timed-out iOS runner xcodebuild processes were terminated when detected."})}function eu(e,t,a){return A({level:"error",phase:"daemon_request_socket_error",data:{requestId:t,message:e instanceof Error?e.message:String(e)}}),new l("COMMAND_FAILED","Failed to communicate with daemon",{requestId:t,hint:a?"Retry command. If this persists, verify the remote daemon URL, auth token, and remote host reachability.":"Retry command. If this persists, clean stale daemon metadata and start a fresh session."},e instanceof Error?e:void 0)}async function ep(t,a,r){let o=t.port;if(!o)throw new l("COMMAND_FAILED","Daemon socket endpoint is unavailable");return new Promise((n,i)=>{let s=e.createConnection({host:"127.0.0.1",port:o},()=>{s.write(`${JSON.stringify(a)}
|
|
2
|
-
`)}),d=f(a.flags?.stateDir??process.env.AGENT_DEVICE_STATE_DIR),c="number"==typeof r?setTimeout(()=>{s.destroy(),i(ec(t,d,a.meta?.requestId,a.command,!1,r))},r):void 0,u="";s.setEncoding("utf8"),s.on("data",e=>{let t=(u+=e).indexOf("\n");if(-1===t)return;let r=u.slice(0,t).trim();if(r)try{let e=JSON.parse(r);s.end(),c&&clearTimeout(c),n(e)}catch(e){c&&clearTimeout(c),i(new l("COMMAND_FAILED","Invalid daemon response",{requestId:a.meta?.requestId,line:r},e instanceof Error?e:void 0))}}),s.on("error",e=>{c&&clearTimeout(c),i(eu(e,a.meta?.requestId,!1))})})}async function em(e,r,o){var n,i,s;let d,c=e.baseUrl?new URL(eh(e.baseUrl,"rpc")):e.httpPort?new URL(`http://127.0.0.1:${e.httpPort}/rpc`):null;if(!c)throw new l("COMMAND_FAILED","Daemon HTTP endpoint is unavailable");let u=JSON.stringify((n=r,i={includeTokenParam:!e.baseUrl},d=n.meta?.requestId??p(),"lease_allocate"!==(s=n.command)&&"lease_heartbeat"!==s&&"lease_release"!==s?{jsonrpc:"2.0",id:d,method:"agent_device.command",params:n}:{jsonrpc:"2.0",id:d,method:function(e){switch(e){case"lease_allocate":return"agent_device.lease.allocate";case"lease_heartbeat":return"agent_device.lease.heartbeat";case"lease_release":return"agent_device.lease.release"}}(n.command),params:function(e,t,a){let r={...a.includeTokenParam?{token:e.token}:{},session:e.session,tenantId:e.meta?.tenantId,runId:e.meta?.runId};switch(t){case"lease_allocate":return{...r,ttlMs:e.meta?.leaseTtlMs,backend:e.meta?.leaseBackend};case"lease_heartbeat":return{...r,leaseId:e.meta?.leaseId,ttlMs:e.meta?.leaseTtlMs};case"lease_release":return{...r,leaseId:e.meta?.leaseId}}}(n,n.command,i)})),m={"content-type":"application/json","content-length":Buffer.byteLength(u)};return e.baseUrl&&e.token&&(m.authorization=`Bearer ${e.token}`,m["x-agent-device-token"]=e.token),await new Promise((n,i)=>{let s=f(r.flags?.stateDir??process.env.AGENT_DEVICE_STATE_DIR),d=("https:"===c.protocol?a:t).request({protocol:c.protocol,host:c.hostname,port:c.port,method:"POST",path:c.pathname+c.search,headers:m},t=>{let a="";t.setEncoding("utf8"),t.on("data",e=>{a+=e}),t.on("end",()=>{h&&clearTimeout(h);try{let t=JSON.parse(a);if(t.error){let e=t.error.data??{};i(new l(String(e.code??"COMMAND_FAILED"),String(e.message??t.error.message??"Daemon RPC request failed"),{..."object"==typeof e.details&&e.details?e.details:{},hint:"string"==typeof e.hint?e.hint:void 0,diagnosticId:"string"==typeof e.diagnosticId?e.diagnosticId:void 0,logPath:"string"==typeof e.logPath?e.logPath:void 0,requestId:r.meta?.requestId}));return}if(!t.result||"object"!=typeof t.result)return void i(new l("COMMAND_FAILED","Invalid daemon RPC response",{requestId:r.meta?.requestId}));if(e.baseUrl&&t.result.ok)return void ey(e,r,t.result).then(n).catch(i);n(t.result)}catch(e){h&&clearTimeout(h),i(new l("COMMAND_FAILED","Invalid daemon response",{requestId:r.meta?.requestId,line:a},e instanceof Error?e:void 0))}})}),p=ef(e),h="number"==typeof o?setTimeout(()=>{d.destroy(),i(ec(e,s,r.meta?.requestId,r.command,p,o))},o):void 0;d.on("error",e=>{h&&clearTimeout(h),i(eu(e,r.meta?.requestId,p))}),d.write(u),d.end()})}function ef(e){return"string"==typeof e.baseUrl&&e.baseUrl.length>0}function eh(e,t){return new URL(t,e.endsWith("/")?e:`${e}/`).toString()}async function ey(e,t,a){let r=Array.isArray(a.data?.artifacts)?a.data.artifacts:[];if(0===r.length||!e.baseUrl)return a;let n=a.data?{...a.data}:{},i=[];for(let a of r){if(!a||"object"!=typeof a||"string"!=typeof a.artifactId){i.push(a);continue}let r=function(e,t){if(e.localPath&&e.localPath.trim().length>0)return e.localPath;let a=e.fileName?.trim()||`${e.field}-${Date.now()}`;return o.resolve(t.meta?.cwd??process.cwd(),a)}(a,t);await ew({baseUrl:e.baseUrl,token:e.token,artifactId:a.artifactId,destinationPath:r,requestId:t.meta?.requestId}),n[a.field]=r,i.push({...a,localPath:r})}return n.artifacts=i,{ok:!0,data:n}}async function ew(e){var n,i;let s,d=new URL((n=e.baseUrl,i=e.artifactId,s=n.endsWith("/")?n:`${n}/`,new URL(`upload/${encodeURIComponent(i)}`,s).toString())),c="https:"===d.protocol?a:t;await r.promises.mkdir(o.dirname(e.destinationPath),{recursive:!0}),await new Promise((t,a)=>{let o=!1,n=e.timeoutMs??F,i=n=>{if(!o){if(o=!0,clearTimeout(u),n)return void r.promises.rm(e.destinationPath,{force:!0}).finally(()=>a(n));t()}},s=c.request({protocol:d.protocol,host:d.hostname,port:d.port,method:"GET",path:d.pathname+d.search,headers:e.token?{authorization:`Bearer ${e.token}`,"x-agent-device-token":e.token}:void 0},t=>{if((t.statusCode??500)>=400){let a="";t.setEncoding("utf8"),t.on("data",e=>{a+=e}),t.on("end",()=>{i(new l("COMMAND_FAILED","Failed to download remote artifact",{artifactId:e.artifactId,statusCode:t.statusCode,requestId:e.requestId,body:a}))});return}let a=r.createWriteStream(e.destinationPath);a.on("error",e=>{i(e instanceof Error?e:Error(String(e)))}),t.on("error",e=>{i(e instanceof Error?e:Error(String(e)))}),t.on("aborted",()=>{i(new l("COMMAND_FAILED","Remote artifact download was interrupted",{artifactId:e.artifactId,requestId:e.requestId}))}),a.on("finish",()=>{a.close(()=>i())}),t.pipe(a)}),u=setTimeout(()=>{s.destroy(new l("COMMAND_FAILED","Remote artifact download timed out",{artifactId:e.artifactId,requestId:e.requestId,timeoutMs:n}))},n);s.on("error",t=>{t instanceof l?i(t):i(new l("COMMAND_FAILED","Failed to download remote artifact",{artifactId:e.artifactId,requestId:e.requestId,timeoutMs:n},t instanceof Error?t:void 0))}),s.end()})}function ev(e=process.env.AGENT_DEVICE_DAEMON_TIMEOUT_MS){if(!e)return 9e4;let t=Number(e);return Number.isFinite(t)?Math.max(1e3,Math.floor(t)):9e4}let eg={alert:"alert",appState:"appstate",appSwitcher:"app-switcher",apps:"apps",back:"back",batch:"batch",boot:"boot",click:"click",clipboard:"clipboard",devices:"devices",diff:"diff",fill:"fill",find:"find",focus:"focus",get:"get",home:"home",is:"is",keyboard:"keyboard",logs:"logs",longPress:"longpress",network:"network",perf:"perf",pinch:"pinch",press:"press",push:"push",record:"record",replay:"replay",rotate:"rotate",scroll:"scroll",screenshot:"screenshot",settings:"settings",snapshot:"snapshot",swipe:"swipe",test:"test",trace:"trace",triggerAppEvent:"trigger-app-event",type:"type",wait:"wait"};function eI(e,t,a,r){let o=a(e[t]);if(void 0===o)throw new l("COMMAND_FAILED",r,{response:e});return o}function eb(e,t){return eI(e,t,eD,`Daemon response is missing "${t}".`)}function eA(e,t){return eD(e[t])}function eM(e,t){var a;let r;return a=eD,null===(r=e[t])?null:a(r)}function e_(e,t){return eI(e,t,eE,`Daemon response has invalid "${t}".`)}function ek(e,t){return function(e){return"tv"===e||"mobile"===e||"desktop"===e?e:void 0}(e[t])??"mobile"}function eS(e,t){let a=e[t];if(!eU(a))return;let r="number"==typeof a.x?a.x:void 0,o="number"==typeof a.y?a.y:void 0,n="number"==typeof a.width?a.width:void 0,i="number"==typeof a.height?a.height:void 0;if(void 0!==r&&void 0!==o&&void 0!==n&&void 0!==i)return{x:r,y:o,width:n,height:i}}function eD(e){return"string"==typeof e&&e.length>0?e:void 0}function eP(e){return"number"==typeof e&&Number.isFinite(e)?e:void 0}function eE(e){return"ios"===e||"macos"===e||"android"===e?e:void 0}function eT(e){return"simulator"===e||"emulator"===e||"device"===e?e:void 0}function eN(e){if(!eU(e))throw new l("COMMAND_FAILED","Daemon returned an unexpected response shape.",{value:e});return e}function eU(e){return"object"==typeof e&&null!==e}function ex(e){let t={};for(let[a,r]of Object.entries(e))void 0!==r&&(t[a]=r);return t}function eC(e,t){let a=eA(e,"bundleId"),r=eA(e,"package");return{app:eb(e,"app"),appPath:eb(e,"appPath"),platform:e_(e,"platform"),appId:a??r,bundleId:a,package:r,identifiers:b({session:t,bundleId:a,packageName:r})}}function eR(e){let t=eN(e),a=e_(t,"platform"),r=eb(t,"id"),o=eb(t,"name");return{platform:a,target:ek(t,"target"),kind:eI(t,"kind",eT,'Daemon response has invalid "kind".'),id:r,name:o,booted:"boolean"==typeof t.booted?t.booted:void 0,identifiers:m(a,r,o),ios:"ios"===a?{udid:r}:void 0,android:"android"===a?{serial:r}:void 0}}function eO(e){let t=eN(e),a=e_(t,"platform"),r=eb(t,"id"),o=eb(t,"name"),n=ek(t,"target"),i=eb(t,"device"),s={session:o,...m(a,r,i)};return{name:o,createdAt:eI(t,"createdAt",eP,'Daemon response is missing numeric "createdAt".'),device:{platform:a,target:n,id:r,name:i,identifiers:s,ios:"ios"===a?{udid:r,simulatorSetPath:eM(t,"ios_simulator_device_set")}:void 0,android:"android"===a?{serial:r}:void 0},identifiers:s}}function eL(e){return e??"default"}function eF(e={},t={}){var a;let r,o=t.transport??z,n=async(t,a=[],r={})=>{var n,i;let s=(n=e,i=r,{...n,...i}),d=await o({session:eL(s.session),command:t,positionals:a,flags:ex({stateDir:s.stateDir,daemonBaseUrl:s.daemonBaseUrl,daemonAuthToken:s.daemonAuthToken,daemonTransport:s.daemonTransport,daemonServerMode:s.daemonServerMode,tenant:s.tenant,sessionIsolation:s.sessionIsolation,runId:s.runId,leaseId:s.leaseId,leaseBackend:s.leaseBackend,platform:s.platform,target:s.target,device:s.device,udid:s.udid,serial:s.serial,iosSimulatorDeviceSet:s.iosSimulatorDeviceSet,androidDeviceAllowlist:s.androidDeviceAllowlist,runtime:s.simulatorRuntimeId,boot:s.boot,reuseExisting:s.reuseExisting,surface:s.surface,activity:s.activity,relaunch:s.relaunch,shutdown:s.shutdown,saveScript:s.saveScript,noRecord:s.noRecord,backMode:s.backMode,metroHost:s.metroHost,metroPort:s.metroPort,bundleUrl:s.bundleUrl,launchUrl:s.launchUrl,snapshotInteractiveOnly:s.interactiveOnly,snapshotCompact:s.compact,snapshotDepth:s.depth,snapshotScope:s.scope,snapshotRaw:s.raw,screenshotFullscreen:s.screenshotFullscreen,overlayRefs:s.overlayRefs,appsFilter:s.appsFilter,out:s.out,count:s.count,fps:s.fps,quality:s.quality,hideTouches:s.hideTouches,intervalMs:s.intervalMs,delayMs:s.delayMs,holdMs:s.holdMs,jitterPx:s.jitterPx,pixels:s.pixels,doubleTap:s.doubleTap,clickButton:s.clickButton,pauseMs:s.pauseMs,pattern:s.pattern,headless:s.headless,restart:s.restart,replayUpdate:s.replayUpdate,failFast:s.failFast,timeoutMs:s.timeoutMs,retries:s.retries,artifactsDir:s.artifactsDir,reportJunit:s.reportJunit,findFirst:s.findFirst,findLast:s.findLast,networkInclude:s.networkInclude,batchOnError:s.batchOnError,batchMaxSteps:s.batchMaxSteps,batchSteps:s.batchSteps,verbose:s.debug}),runtime:s.runtime,meta:ex({requestId:s.requestId,cwd:s.cwd,debug:s.debug,lockPolicy:s.lockPolicy,lockPlatform:s.lockPlatform,tenantId:s.tenant,runId:s.runId,leaseId:s.leaseId,leaseBackend:s.leaseBackend,leaseTtlMs:s.leaseTtlMs,sessionIsolation:s.sessionIsolation,installSource:s.installSource,retainMaterializedPaths:s.retainMaterializedPaths,materializedPathRetentionMs:s.materializedPathRetentionMs,materializationId:s.materializationId})});return d.ok||function(e){throw new l(e.code,e.message,{...e.details??{},hint:e.hint,diagnosticId:e.diagnosticId,logPath:e.logPath})}(d.error),d.data??{}},i=async(e={})=>{let t=await n("session_list",[],e);return(Array.isArray(t.sessions)?t.sessions:[]).map(eO)},s=async(e,t=[],a={})=>await n(e,t,a),d=(t={})=>{var a,r;return eL((a=e,r=t,{...a,...r}).session)};return{command:(a=async e=>await n(e.command,e.positionals,e.options),r=async e=>await a(e),{wait:async e=>await r(function(e){if(1!==[void 0!==e.durationMs?"durationMs":void 0,void 0!==e.text?"text":void 0,void 0!==e.ref?"ref":void 0,void 0!==e.selector?"selector":void 0].filter(Boolean).length)throw new l("INVALID_ARGS","wait command requires exactly one of durationMs, text, ref, or selector.");if(void 0!==e.durationMs)return{command:eg.wait,positionals:[String(e.durationMs)],options:e};let t=void 0!==e.timeoutMs?[String(e.timeoutMs)]:[];if(void 0!==e.text)return{command:eg.wait,positionals:["text",e.text,...t],options:e};if(void 0!==e.ref)return{command:eg.wait,positionals:[e.ref,...t],options:e};let a=e.selector;return function(e){if(!k(e))throw new l("INVALID_ARGS",`Invalid wait selector: ${e}`)}(a),{command:eg.wait,positionals:[a,...t],options:e}}(e)),alert:async(e={})=>{var t;let a;return await r((a=(t=e).action??"get",{command:eg.alert,positionals:[a,...void 0!==t.timeoutMs?[String(t.timeoutMs)]:[]],options:t}))},appState:async(e={})=>await r({command:eg.appState,positionals:[],options:e}),back:async(e={})=>await r({command:eg.back,positionals:[],options:{...e,backMode:e.mode}}),home:async(e={})=>await r({command:eg.home,positionals:[],options:e}),rotate:async e=>await r({command:eg.rotate,positionals:[e.orientation],options:e}),appSwitcher:async(e={})=>await r({command:eg.appSwitcher,positionals:[],options:e}),keyboard:async(e={})=>await r({command:eg.keyboard,positionals:e.action?[e.action]:[],options:e}),clipboard:async e=>{var t;return await r("read"===(t=e).action?{command:eg.clipboard,positionals:["read"],options:t}:{command:eg.clipboard,positionals:["write",t.text],options:t})}}),devices:{list:async(e={})=>{let t=await n(eg.devices,[],e);return(Array.isArray(t.devices)?t.devices:[]).map(eR)},boot:async(e={})=>await s(eg.boot,[],e)},sessions:{list:async(e={})=>await i(e),close:async(e={})=>{let t=d(e),a=(await n("close",[],e)).shutdown;return{session:t,shutdown:"object"==typeof a&&null!==a?a:void 0,identifiers:{session:t}}}},simulators:{ensure:async e=>{let{runtime:t,...a}=e,r=await n("ensure-simulator",[],{...a,simulatorRuntimeId:t}),o=eb(r,"udid"),i=eb(r,"device");return{udid:o,device:i,runtime:eb(r,"runtime"),created:!0===r.created,booted:!0===r.booted,iosSimulatorDeviceSet:eM(r,"ios_simulator_device_set"),identifiers:{deviceId:o,deviceName:i,udid:o}}}},apps:{install:async e=>eC(await n("install",[e.app,e.appPath],e),d(e)),reinstall:async e=>eC(await n("reinstall",[e.app,e.appPath],e),d(e)),installFromSource:async e=>(function(e,t){let a=eA(e,"bundleId"),r=eA(e,"packageName"),o=a??r??eA(e,"appId"),n=eA(e,"launchTarget")??r??a??o;if(!n)throw new l("COMMAND_FAILED",'Daemon response is missing "launchTarget".',{response:e});return{appName:eA(e,"appName"),appId:o,bundleId:a,packageName:r,launchTarget:n,installablePath:eA(e,"installablePath"),archivePath:eA(e,"archivePath"),materializationId:eA(e,"materializationId"),materializationExpiresAt:eA(e,"materializationExpiresAt"),identifiers:b({session:t,bundleId:a,packageName:r,appId:o})}})(await n("install_source",[],{...e,installSource:e.source,retainMaterializedPaths:e.retainPaths,materializedPathRetentionMs:e.retentionMs}),d(e)),list:async(e={})=>{let t=await n(eg.apps,[],e);return Array.isArray(t.apps)?t.apps.filter(e=>"string"==typeof e):[]},open:async e=>{let t=d(e),a=e.app?e.url?[e.app,e.url]:[e.app]:[],r=await n("open",a,e),o=function(e){let t=e.platform,a=eA(e,"id"),r=eA(e,"device");if("ios"!==t&&"macos"!==t&&"android"!==t||!a||!r)return;let o=ek(e,"target"),n=m(t,a,r);return{platform:t,target:o,id:a,name:r,identifiers:n,ios:"ios"===t?{udid:eA(e,"device_udid")??a,simulatorSetPath:eM(e,"ios_simulator_device_set")}:void 0,android:"android"===t?{serial:eA(e,"serial")??a}:void 0}}(r),i=eA(r,"appBundleId");return{session:t,appName:eA(r,"appName"),appBundleId:i,appId:i,startup:function(e){if(eU(e)&&"number"==typeof e.durationMs&&"string"==typeof e.measuredAt&&"string"==typeof e.method)return{durationMs:e.durationMs,measuredAt:e.measuredAt,method:e.method,appTarget:eA(e,"appTarget"),appBundleId:eA(e,"appBundleId")}}(r.startup),runtime:function(e){if(!eU(e))return;let t=e.platform,a=eA(e,"metroHost"),r="number"==typeof e.metroPort?e.metroPort:void 0;return{platform:"ios"===t||"android"===t?t:void 0,metroHost:a,metroPort:r,bundleUrl:eA(e,"bundleUrl"),launchUrl:eA(e,"launchUrl")}}(r.runtime),device:o,identifiers:{session:t,deviceId:o?.id,deviceName:o?.name,udid:o?.ios?.udid,serial:o?.android?.serial,appId:i,appBundleId:i}}},close:async(e={})=>{let t=d(e),a=(await n("close",e.app?[e.app]:[],e)).shutdown;return{session:t,closedApp:e.app,shutdown:"object"==typeof a&&null!==a?a:void 0,identifiers:{session:t}}},push:async e=>{var t;return await s(eg.push,[e.app,"string"==typeof(t=e.payload)?t:JSON.stringify(t)],e)},triggerEvent:async e=>{var t;return await s(eg.triggerAppEvent,[(t=e).event,...t.payload?[JSON.stringify(t.payload)]:[]],e)}},materializations:{release:async e=>{var t;return{released:!0===(t=await n("release_materialized_paths",[],{...e,materializationId:e.materializationId})).released,materializationId:eb(t,"materializationId"),identifiers:{}}}},leases:{allocate:async e=>ej(await n("lease_allocate",[],{...e,leaseId:void 0,leaseTtlMs:e.ttlMs})),heartbeat:async e=>ej(await n("lease_heartbeat",[],{...e,leaseTtlMs:e.ttlMs})),release:async e=>({released:!0===(await n("lease_release",[],e)).released})},metro:{prepare:async t=>await S({projectRoot:t.projectRoot??e.cwd,kind:t.kind,publicBaseUrl:t.publicBaseUrl,proxyBaseUrl:t.proxyBaseUrl,proxyBearerToken:t.bearerToken,bridgeScope:t.bridgeScope,launchUrl:t.launchUrl,companionProfileKey:t.companionProfileKey,companionConsumerKey:t.companionConsumerKey,metroPort:t.port,listenHost:t.listenHost,statusHost:t.statusHost,startupTimeoutMs:t.startupTimeoutMs,probeTimeoutMs:t.probeTimeoutMs,reuseExisting:t.reuseExisting,installDependenciesIfNeeded:t.installDependenciesIfNeeded,runtimeFilePath:t.runtimeFilePath,logPath:t.logPath})},capture:{snapshot:async(e={})=>{var t;let a=d(e),r=await n(eg.snapshot,[],e),o=eA(r,"appBundleId"),i="object"==typeof r.visibility&&null!==r.visibility?r.visibility:void 0;return{nodes:Array.isArray(t=r.nodes)?t:[],truncated:!0===r.truncated,appName:eA(r,"appName"),appBundleId:o,...i?{visibility:i}:{},warnings:Array.isArray(r.warnings)?r.warnings.filter(e=>"string"==typeof e):void 0,identifiers:{session:a,appId:o,appBundleId:o}}},screenshot:async(e={})=>{let t=d(e),a=await n(eg.screenshot,e.path?[e.path]:[],{...e,screenshotFullscreen:e.fullscreen});return{path:eb(a,"path"),overlayRefs:function(e){let t=e.overlayRefs;if(!Array.isArray(t))return;let a=[];for(let e of t){if(!eU(e))continue;let t=eA(e,"ref"),r=eS(e,"rect"),o=eS(e,"overlayRect"),n=function(e,t){let a=e[t];if(!eU(a))return;let r="number"==typeof a.x?a.x:void 0,o="number"==typeof a.y?a.y:void 0;if(void 0!==r&&void 0!==o)return{x:r,y:o}}(e,"center");t&&r&&o&&n&&a.push({ref:t,label:eA(e,"label"),rect:r,overlayRect:o,center:n})}return a}(a),identifiers:{session:t}}},diff:async e=>await s(eg.diff,[e.kind],{...e,interactiveOnly:e.interactiveOnly,compact:e.compact,depth:e.depth,scope:e.scope,raw:e.raw})},interactions:{click:async e=>await s(eg.click,eB(e),{...e,clickButton:e.button}),press:async e=>await s(eg.press,eB(e),e),longPress:async e=>await s(eg.longPress,[String(e.x),String(e.y),...e$(e.durationMs)],e),swipe:async e=>await s(eg.swipe,[String(e.from.x),String(e.from.y),String(e.to.x),String(e.to.y),...e$(e.durationMs)],e),focus:async e=>await s(eg.focus,[String(e.x),String(e.y)],e),type:async e=>await s(eg.type,[e.text],e),fill:async e=>await s(eg.fill,[...eB(e),e.text],e),scroll:async e=>await s(eg.scroll,[e.direction,...e$(e.amount)],e),pinch:async e=>await s(eg.pinch,[String(e.scale),...e$(e.x),...e$(e.y)],e),get:async e=>{var t;return await s(eg.get,[e.format,...void 0!==(t=e).ref?[t.ref,...eq(t.label)]:[t.selector]],e)},is:async e=>await s(eg.is,[e.predicate,e.selector,..."text"===e.predicate?[e.value]:[]],e),find:async e=>await s(eg.find,function(e){let t=e.locator&&"any"!==e.locator?[e.locator,e.query]:[e.query];switch(e.action){case void 0:case"click":case"focus":case"exists":return e.action?[...t,e.action]:t;case"getText":return[...t,"get","text"];case"getAttrs":return[...t,"get","attrs"];case"wait":return[...t,"wait",...e$(e.timeoutMs)];case"fill":case"type":return[...t,e.action,e.value]}}(e),{...e,findFirst:e.first,findLast:e.last})},replay:{run:async e=>await s(eg.replay,[e.path],{...e,replayUpdate:e.update}),test:async e=>await s(eg.test,e.paths,{...e,replayUpdate:e.update})},batch:{run:async e=>await s(eg.batch,[],{...e,batchSteps:e.steps,batchOnError:e.onError,batchMaxSteps:e.maxSteps})},observability:{perf:async(e={})=>await s(eg.perf,[],e),logs:async(e={})=>{var t;return await s(eg.logs,[(t=e).action??"path",...eq(t.message)],e)},network:async(e={})=>{var t;return await s(eg.network,[...(t=e).action?[t.action]:[],...e$(t.limit)],{...e,networkInclude:e.include})}},recording:{record:async e=>await s(eg.record,[e.action,...eq(e.path)],e),trace:async e=>await s(eg.trace,[e.action,...eq(e.path)],e)},settings:{update:async e=>await s(eg.settings,[e.setting,e.state,..."permission"in e?[e.permission]:[],..."mode"in e&&e.mode?[e.mode]:[]],e)}}}function eB(e){return void 0!==e.ref?[e.ref,...eq(e.label)]:void 0!==e.selector?[e.selector]:[String(e.x),String(e.y)]}function eq(e){return void 0===e?[]:[e]}function e$(e){return void 0===e?[]:[String(e)]}function ej(e){let t=e.lease;if(!t||"object"!=typeof t||Array.isArray(t))throw Error("Invalid lease response from daemon");return{leaseId:eb(t,"leaseId"),tenantId:eb(t,"tenantId"),runId:eb(t,"runId"),backend:eb(t,"backend"),createdAt:"number"==typeof t.createdAt?t.createdAt:void 0,heartbeatAt:"number"==typeof t.heartbeatAt?t.heartbeatAt:void 0,expiresAt:"number"==typeof t.expiresAt?t.expiresAt:void 0}}export{eg as CLIENT_COMMANDS,eF as createAgentDeviceClient,z as sendToDaemon};
|
|
1
|
+
import e from"node:net";import t from"node:http";import a from"node:https";import r from"node:fs";import o from"node:path";import n from"node:os";import{createHash as i,randomUUID as s}from"node:crypto";import{toAppErrorCode as l,AppError as d}from"./9152.js";import{runCmdSync as c,runCmd as u,runCmdDetached as p}from"./9818.js";import{createRequestId as m,buildDeviceIdentifiers as f,resolveDaemonPaths as h,readVersion as y,findProjectRoot as w,resolveDaemonServerMode as v,computeDaemonCodeSignature as g,withDiagnosticTimer as I,resolveDaemonTransportPreference as b,buildAppIdentifiers as A,emitDiagnostic as M}from"./8564.js";import{isAgentDeviceDaemonProcess as _,stopProcessForTakeover as S}from"./3883.js";import{sleep as k}from"./4829.js";import{tryParseSelectorChain as E}from"./7847.js";import{prepareMetroRuntime as D}from"./1974.js";let P="sha256";async function T(e){let t=await N(e.localPath,e.platform),a=e.baseUrl.endsWith("/")?e.baseUrl:`${e.baseUrl}/`;try{let r=await R({normalizedBase:a,token:e.token,artifact:t});if(r?.kind==="cache-hit")return r.uploadId;if(r?.kind==="direct-upload")try{return await O(t.payloadPath,r),await F({normalizedBase:a,token:e.token,uploadId:r.uploadId})}catch{}return await C({normalizedBase:a,token:e.token,artifact:t})}finally{t.cleanup()}}async function N(e,t){var a,n,i;let s,l=r.statSync(e),d=o.basename(e),c=l.isDirectory(),u=("ios"===(a=t)||"android"===a?a:void 0)??(n=e,i=l,s=n.toLowerCase(),i.isDirectory()&&s.endsWith(".app")||s.endsWith(".ipa")?"ios":s.endsWith(".apk")||s.endsWith(".aab")?"android":void 0),p=[];try{let t=c?await U(e,p):e,a=r.statSync(t);return{payloadPath:t,fileName:d,artifactType:c?"app-bundle":"file",platform:u,contentType:c?"application/gzip":"application/octet-stream",sha256:await B(t),sizeBytes:a.size,cleanup:()=>x(p)}}catch(e){throw x(p),e}}async function U(e,t){let a=r.mkdtempSync(o.join(n.tmpdir(),`agent-device-upload-${s()}-`));t.push(a);let i=o.join(a,`${o.basename(e)}.tar.gz`);return await u("tar",["czf",i,"-C",o.dirname(e),o.basename(e)]),i}function x(e){for(let t of e)r.rmSync(t,{recursive:!0,force:!0})}async function C(e){let{normalizedBase:t,token:a,artifact:r}=e,o=new URL("upload",t),n={"content-type":r.contentType,"x-artifact-type":r.artifactType,"x-artifact-filename":r.fileName,"x-artifact-hash":r.sha256,"x-artifact-hash-algorithm":P,"transfer-encoding":"chunked"};a&&(n.authorization=`Bearer ${a}`,n["x-agent-device-token"]=a);let i=await L({url:o,method:"POST",headers:n,payloadPath:r.payloadPath,timeoutMessage:"Artifact upload timed out",timeoutHint:"The upload to the remote daemon exceeded the 5-minute timeout.",errorMessage:"Failed to upload artifact to remote daemon",errorHint:"Verify the remote daemon is reachable and supports artifact uploads."});try{let e=JSON.parse(i.body);if(!e.ok||!e.uploadId)throw new d("COMMAND_FAILED",`Upload failed: ${i.body}`);return e.uploadId}catch(e){if(e instanceof d)throw e;throw new d("COMMAND_FAILED",`Invalid upload response: ${i.body}`)}}async function R(e){let t=new URL("upload/preflight",e.normalizedBase),a={"content-type":"application/json"};e.token&&(a.authorization=`Bearer ${e.token}`,a["x-agent-device-token"]=e.token);let r=await fetch(t,{method:"POST",headers:a,signal:AbortSignal.timeout(3e4),body:JSON.stringify({sha256:e.artifact.sha256,fileName:e.artifact.fileName,sizeBytes:e.artifact.sizeBytes,artifactType:e.artifact.artifactType,...e.artifact.platform?{platform:e.artifact.platform}:{},contentType:e.artifact.contentType})}).catch(()=>void 0);if(r?.ok)return function(e){var t;if(!e||"object"!=typeof e||!0!==e.ok||"string"!=typeof e.uploadId)return;if(!0===e.cacheHit)return{kind:"cache-hit",uploadId:e.uploadId};let a=e.upload;if(!a||"string"!=typeof a.url)return;let r=a.headers??{};if(!(!(t=r)||"object"!=typeof t||Array.isArray(t))&&Object.values(t).every(e=>"string"==typeof e))return{kind:"direct-upload",uploadId:e.uploadId,url:a.url,headers:r}}(await r.json().catch(()=>void 0))}async function O(e,t){let a=await L({url:new URL(t.url),method:"PUT",headers:t.headers,payloadPath:e,timeoutMessage:"Direct artifact upload timed out",timeoutHint:"The direct upload ticket did not accept the artifact within the timeout.",errorMessage:"Failed to upload artifact with direct upload ticket"});if(a.statusCode<200||a.statusCode>=300)throw new d("COMMAND_FAILED","Direct artifact upload failed",{statusCode:a.statusCode,statusMessage:a.statusMessage})}async function L(e){let o="https:"===e.url.protocol?a:t;return await new Promise((t,a)=>{let n=o.request({protocol:e.url.protocol,host:e.url.hostname,port:e.url.port,method:e.method,path:e.url.pathname+e.url.search,headers:e.headers},e=>{let a="";e.setEncoding("utf8"),e.on("data",e=>{a+=e}),e.on("end",()=>{clearTimeout(i),t({statusCode:e.statusCode??500,statusMessage:e.statusMessage,body:a})})}),i=setTimeout(()=>{n.destroy(),a(new d("COMMAND_FAILED",e.timeoutMessage,{timeoutMs:3e5,...e.timeoutHint?{hint:e.timeoutHint}:{}}))},3e5);n.on("error",t=>{clearTimeout(i),a(new d("COMMAND_FAILED",e.errorMessage,e.errorHint?{hint:e.errorHint}:{},t))}),n.on("close",()=>clearTimeout(i));let s=r.createReadStream(e.payloadPath);s.pipe(n),s.on("error",e=>{n.destroy(),a(new d("COMMAND_FAILED","Failed to read local artifact",{},e))})})}async function F(e){let t=new URL("upload/finalize",e.normalizedBase),a={"content-type":"application/json"};e.token&&(a.authorization=`Bearer ${e.token}`,a["x-agent-device-token"]=e.token);let r=await fetch(t,{method:"POST",headers:a,signal:AbortSignal.timeout(3e4),body:JSON.stringify({uploadId:e.uploadId})}).catch(e=>{throw new d("COMMAND_FAILED","Failed to finalize direct artifact upload",{},e)});if(!r.ok)throw new d("COMMAND_FAILED","Direct artifact upload finalize failed",{status:r.status,statusText:r.statusText});let o=await r.json().catch(()=>void 0);if(!o?.ok||!o.uploadId)throw new d("COMMAND_FAILED","Invalid upload finalize response");return o.uploadId}async function B(e){let t=i(P);return await new Promise((a,o)=>{r.createReadStream(e).on("data",e=>t.update(e)).on("error",e=>{o(new d("COMMAND_FAILED","Failed to read local artifact",{},e))}).on("end",a)}),t.digest("hex")}let q=eg(),z=function(e=process.env.AGENT_DEVICE_DAEMON_STARTUP_TIMEOUT_MS){if(!e)return 15e3;let t=Number(e);return Number.isFinite(t)?Math.max(1e3,Math.floor(t)):15e3}(),$=function(e=process.env.AGENT_DEVICE_DAEMON_STARTUP_ATTEMPTS){if(!e)return 2;let t=Number(e);return Number.isFinite(t)?Math.min(5,Math.max(1,Math.floor(t))):2}(),j=["xcodebuild .*AgentDeviceRunnerUITests/RunnerTests/testCommand","xcodebuild .*AgentDeviceRunner\\.env\\.session-","xcodebuild build-for-testing .*ios-runner/AgentDeviceRunner/AgentDeviceRunner\\.xcodeproj"],H=new e.BlockList;async function V(t){let a=t.meta?.requestId??m(),r=!!(t.meta?.debug||t.flags?.verbose),o=function(t){let a,r=t.flags?.stateDir??process.env.AGENT_DEVICE_STATE_DIR,o=function(e){let t;if(e){try{t=new URL(e)}catch(t){throw new d("INVALID_ARGS","Invalid daemon base URL",{daemonBaseUrl:e},t instanceof Error?t:void 0)}if("http:"!==t.protocol&&"https:"!==t.protocol)throw new d("INVALID_ARGS","Daemon base URL must use http or https",{daemonBaseUrl:e});return t.toString().replace(/\/+$/,"")}}(t.flags?.daemonBaseUrl??process.env.AGENT_DEVICE_DAEMON_BASE_URL),n=t.flags?.daemonAuthToken??process.env.AGENT_DEVICE_DAEMON_AUTH_TOKEN;var i=o,s=n;if(!(!i||"localhost"===(a=new URL(i).hostname.trim().toLowerCase().replace(/^\[(.*)\]$/,"$1"))||(e.isIPv4(a)?H.check(a,"ipv4"):!!e.isIPv6(a)&&H.check(a,"ipv6")))&&("string"!=typeof s||!(s.trim().length>0)))throw new d("INVALID_ARGS","Remote daemon base URL for non-loopback hosts requires daemon authentication",{daemonBaseUrl:i,hint:"Provide --daemon-auth-token or AGENT_DEVICE_DAEMON_AUTH_TOKEN when using a non-loopback remote daemon URL."});let l=t.flags?.daemonTransport??process.env.AGENT_DEVICE_DAEMON_TRANSPORT,c=b(l);if(o&&"socket"===c)throw new d("INVALID_ARGS","Remote daemon base URL only supports HTTP transport. Remove --daemon-transport socket.",{daemonBaseUrl:o});let u=v(t.flags?.daemonServerMode??process.env.AGENT_DEVICE_DAEMON_SERVER_MODE??("dual"===l?"dual":void 0));return{paths:h(r),transportPreference:c,serverMode:u,remoteBaseUrl:o,remoteAuthToken:n}}(t),n=function(e,t=process.env.AGENT_DEVICE_DAEMON_TIMEOUT_MS){if("test"!==e)return eg(t)}(t.command),i=await I("daemon_startup",async()=>await Q(o),{requestId:a,session:t.session}),s=await G(t,i),l={...t,positionals:s.positionals,flags:s.flags,token:i.token,meta:{...t.meta??{},requestId:a,debug:r,cwd:t.meta?.cwd,tenantId:t.meta?.tenantId??t.flags?.tenant,runId:t.meta?.runId??t.flags?.runId,leaseId:t.meta?.leaseId??t.flags?.leaseId,sessionIsolation:t.meta?.sessionIsolation??t.flags?.sessionIsolation,lockPolicy:t.meta?.lockPolicy,lockPlatform:t.meta?.lockPlatform,...s.uploadedArtifactId?{uploadedArtifactId:s.uploadedArtifactId}:{},...s.clientArtifactPaths?{clientArtifactPaths:s.clientArtifactPaths}:{},...s.installSource?{installSource:s.installSource}:{}}};return M({level:"info",phase:"daemon_request_prepare",data:{requestId:a,command:t.command,session:t.session}}),await I("daemon_request",async()=>await el(i,l,o.transportPreference,n),{requestId:a,command:t.command})}async function G(e,t){let a,n=[...e.positionals??[]],i=e.flags?{...e.flags}:void 0,s=e.meta?.installSource,l={};if(eh(t)){let r=function(e,t){if("screenshot"===e.command){let a=J(e,"path",".png");return t[0]?{field:"path",localPath:a,positionalIndex:0,positionalPath:W("screenshot",".png")}:{field:"path",localPath:a,positionalIndex:0,flagPath:W("screenshot",".png")}}if("record"===e.command&&"start"===(t[0]??"").toLowerCase()){let t=J(e,"outPath",".mp4",1);return{field:"outPath",localPath:t,positionalIndex:1,positionalPath:W("recording",o.extname(t)||".mp4")}}return null}(e,n);r&&(void 0!==r.positionalPath&&(n[r.positionalIndex]=r.positionalPath),void 0!==r.flagPath&&((i??={}).out=r.flagPath),l[r.field]=r.localPath);let d=await K(e,t);d&&(s=d.installSource,a=d.uploadedArtifactId??a)}if(!eh(t)||"install"!==e.command&&"reinstall"!==e.command||n.length<2)return{positionals:n,flags:i,installSource:s,uploadedArtifactId:a,...Object.keys(l).length>0?{clientArtifactPaths:l}:{}};let d=n[1];if(d.startsWith("remote:"))return n[1]=d.slice(7),{positionals:n,flags:i,...Object.keys(l).length>0?{clientArtifactPaths:l}:{}};let c=o.isAbsolute(d)?d:o.resolve(e.meta?.cwd??process.cwd(),d);return r.existsSync(c)?{positionals:n,flags:i,installSource:s,uploadedArtifactId:a=await T({localPath:c,baseUrl:t.baseUrl,token:t.token,platform:e.flags?.platform}),...Object.keys(l).length>0?{clientArtifactPaths:l}:{}}:{positionals:n,flags:i,...Object.keys(l).length>0?{clientArtifactPaths:l}:{}}}async function K(e,t){let a=e.meta?.installSource;if("install_source"!==e.command||!a||"path"!==a.kind)return null;let n=a.path.trim();if(!n)return{installSource:a};if(n.startsWith("remote:"))return{installSource:{...a,path:n.slice(7)}};let i=o.isAbsolute(n)?n:o.resolve(e.meta?.cwd??process.cwd(),n);if(!r.existsSync(i))return{installSource:{...a,path:i}};let s=await T({localPath:i,baseUrl:t.baseUrl,token:t.token,platform:e.flags?.platform});return{installSource:{...a,path:i},uploadedArtifactId:s}}function J(e,t,a,r=0){let n=e.positionals?.[r]??e.flags?.out,i=`${"path"===t?"screenshot":"recording"}-${Date.now()}${a}`,s=n&&n.trim().length>0?n:i;return o.isAbsolute(s)?s:o.resolve(e.meta?.cwd??process.cwd(),s)}function W(e,t){let a=t.startsWith(".")?t:`.${t}`;return o.posix.join("/tmp",`agent-device-${e}-${Date.now()}-${Math.random().toString(36).slice(2,8)}${a}`)}async function Q(e){let t;if(e.remoteBaseUrl){let t={transport:"http",token:e.remoteAuthToken??"",pid:0,baseUrl:e.remoteBaseUrl};if(await en(t,"http"))return t;throw new d("COMMAND_FAILED","Remote daemon is unavailable",{daemonBaseUrl:e.remoteBaseUrl,hint:"Verify AGENT_DEVICE_DAEMON_BASE_URL points to a reachable daemon with GET /health and POST /rpc."})}let a=ee(e.paths.infoPath),r=y(),o=g((t=es()).useSrc?t.srcPath:t.distPath,t.root),n=!!a&&await en(a,e.transportPreference);if(a&&a.version===r&&a.codeSignature===o&&n)return a;a&&(a.version!==r||a.codeSignature!==o||!n)&&(await Z(a),eo(e.paths.infoPath)),function(e){let t=ea(e);if(!t.hasLock||t.hasInfo)return;let a=et(e.lockPath);if(!a)return eo(e.lockPath);_(a.pid,a.processStartTime)||eo(e.lockPath)}(e.paths);let i=0;for(let t=1;t<=$;t+=1){await ei(e);let a=await X(z,e);if(a)return a;if(await Y(e.paths)){i+=1;continue}let r=ea(e.paths);if(!(t<$))break;if(!r.hasInfo&&!r.hasLock){await k(150);continue}}let s=ea(e.paths);throw new d("COMMAND_FAILED","Failed to start daemon",{kind:"daemon_startup_failed",infoPath:e.paths.infoPath,lockPath:e.paths.lockPath,startupTimeoutMs:z,startupAttempts:$,lockRecoveryCount:i,metadataState:s,hint:function(e,t=h(process.env.AGENT_DEVICE_STATE_DIR)){return e.hasLock&&!e.hasInfo?`Detected ${t.lockPath} without ${t.infoPath}. If no agent-device daemon process is running, delete ${t.lockPath} and retry.`:e.hasLock&&e.hasInfo?`Daemon metadata may be stale. If no agent-device daemon process is running, delete ${t.infoPath} and ${t.lockPath}, then retry.`:`Daemon metadata is missing or stale. Delete ${t.infoPath} if present and retry.`}(s,e.paths)})}async function X(e,t){let a=Date.now();for(;Date.now()-a<e;){let e=ee(t.paths.infoPath);if(e&&await en(e,t.transportPreference))return e;await k(100)}return null}async function Y(e){let t=ea(e);if(!t.hasLock||t.hasInfo)return!1;let a=et(e.lockPath);return a&&_(a.pid,a.processStartTime)&&await S(a.pid,{termTimeoutMs:3e3,killTimeoutMs:1e3,expectedStartTime:a.processStartTime}),eo(e.lockPath),!0}async function Z(e){await S(e.pid,{termTimeoutMs:3e3,killTimeoutMs:1e3,expectedStartTime:e.processStartTime})}function ee(e){let t=er(e);if(!t||"object"!=typeof t)return null;let a="string"==typeof t.token&&t.token.length>0?t.token:null;if(!a)return null;let r=Number.isInteger(t.port)&&Number(t.port)>0,o=Number.isInteger(t.httpPort)&&Number(t.httpPort)>0;if(!r&&!o)return null;let n=t.transport,i="string"==typeof t.version?t.version:void 0,s="string"==typeof t.codeSignature?t.codeSignature:void 0,l="string"==typeof t.processStartTime?t.processStartTime:void 0,d=Number.isInteger(t.pid)&&Number(t.pid)>0;return{token:a,port:r?Number(t.port):void 0,httpPort:o?Number(t.httpPort):void 0,transport:"socket"===n||"http"===n||"dual"===n?n:void 0,pid:d?Number(t.pid):0,version:i,codeSignature:s,processStartTime:l}}function et(e){let t=er(e);return t&&"object"==typeof t&&Number.isInteger(t.pid)&&Number(t.pid)>0?{pid:Number(t.pid),processStartTime:"string"==typeof t.processStartTime?t.processStartTime:void 0,startedAt:"number"==typeof t.startedAt?t.startedAt:void 0}:null}H.addSubnet("127.0.0.0",8,"ipv4"),H.addAddress("::1","ipv6"),H.addSubnet("::ffff:127.0.0.0",104,"ipv6");function ea(e){return{hasInfo:r.existsSync(e.infoPath),hasLock:r.existsSync(e.lockPath)}}function er(e){if(!r.existsSync(e))return null;try{return JSON.parse(r.readFileSync(e,"utf8"))}catch{return null}}function eo(e){try{r.existsSync(e)&&r.unlinkSync(e)}catch{}}async function en(r,o){var n;return"http"===ed(r,o)?await function(e){let r=e.baseUrl?ey(e.baseUrl,"health"):e.httpPort?`http://127.0.0.1:${e.httpPort}/health`:null;if(!r)return Promise.resolve(!1);let o=new URL(r),n="https:"===o.protocol?a:t,i=e.baseUrl?3e3:500;return new Promise(e=>{let t=n.request({protocol:o.protocol,host:o.hostname,port:o.port,path:o.pathname+o.search,method:"GET",timeout:i},t=>{t.resume(),e((t.statusCode??500)<500)});t.on("timeout",()=>{t.destroy(),e(!1)}),t.on("error",()=>{e(!1)}),t.end()})}(r):await ((n=r.port)?new Promise(t=>{let a=e.createConnection({host:"127.0.0.1",port:n},()=>{a.destroy(),t(!0)});a.on("error",()=>{t(!1)})}):Promise.resolve(!1))}async function ei(e){let t=es(),a=t.useSrc?["--experimental-strip-types",t.srcPath]:[t.distPath],r={...process.env,AGENT_DEVICE_STATE_DIR:e.paths.baseDir,AGENT_DEVICE_DAEMON_SERVER_MODE:e.serverMode};p(process.execPath,a,{env:r})}function es(){let e=w(),t=o.join(e,"dist","src","daemon.js"),a=o.join(e,"src","daemon.ts"),n=r.existsSync(t),i=r.existsSync(a);if(!n&&!i)throw new d("COMMAND_FAILED","Daemon entry not found",{distPath:t,srcPath:a});return{root:e,distPath:t,srcPath:a,useSrc:process.execArgv.includes("--experimental-strip-types")?i:!n&&i}}async function el(e,t,a,r){return"http"===ed(e,a)?await ef(e,t,r):await em(e,t,r)}function ed(e,t){if(e.baseUrl){if("socket"===t)throw new d("COMMAND_FAILED","Remote daemon endpoint only supports HTTP transport",{daemonBaseUrl:e.baseUrl});return"http"}if("http"===t||"socket"===t){var a=e,r=t;if(ec(a,r))return r;throw new d("COMMAND_FAILED","http"===r?"Daemon HTTP endpoint is unavailable":"Daemon socket endpoint is unavailable")}let o=("socket"===e.transport||"dual"===e.transport?["socket","http"]:["http","socket"]).find(t=>ec(e,t));if(o)return o;throw new d("COMMAND_FAILED","Daemon metadata has no reachable transport")}function ec(e,t){return"http"===t?!!e.httpPort:!!e.port}function eu(e,t,a,r,o,n){let i=o?{terminated:0}:function(){let e=0;try{for(let t of j){let a=c("pkill",["-f",t],{allowFailure:!0});0===a.exitCode&&(e+=1)}return{terminated:e}}catch(t){return{terminated:e,error:t instanceof Error?t.message:String(t)}}}(),s=o?{forcedKill:!1}:function(e,t){let a=!1;try{_(e.pid,e.processStartTime)&&(process.kill(e.pid,"SIGKILL"),a=!0)}catch{S(e.pid,{termTimeoutMs:3e3,killTimeoutMs:1e3,expectedStartTime:e.processStartTime})}finally{eo(t.infoPath),eo(t.lockPath)}return{forcedKill:a}}(e,t);return M({level:"error",phase:"daemon_request_timeout",data:{timeoutMs:n,requestId:a,command:r,timedOutRunnerPidsTerminated:i.terminated,timedOutRunnerCleanupError:i.error,daemonPidReset:o?void 0:e.pid,daemonPidForceKilled:o?void 0:s.forcedKill,daemonBaseUrl:e.baseUrl}}),new d("COMMAND_FAILED","Daemon request timed out",{timeoutMs:n,requestId:a,hint:o?"Retry with --debug and verify the remote daemon URL, auth token, and remote host logs.":"Retry with --debug and check daemon diagnostics logs. Timed-out iOS runner xcodebuild processes were terminated when detected."})}function ep(e,t,a){return M({level:"error",phase:"daemon_request_socket_error",data:{requestId:t,message:e instanceof Error?e.message:String(e)}}),new d("COMMAND_FAILED","Failed to communicate with daemon",{requestId:t,hint:a?"Retry command. If this persists, verify the remote daemon URL, auth token, and remote host reachability.":"Retry command. If this persists, clean stale daemon metadata and start a fresh session."},e instanceof Error?e:void 0)}async function em(t,a,r){let o=t.port;if(!o)throw new d("COMMAND_FAILED","Daemon socket endpoint is unavailable");return new Promise((n,i)=>{let s=e.createConnection({host:"127.0.0.1",port:o},()=>{s.write(`${JSON.stringify(a)}
|
|
2
|
+
`)}),l=h(a.flags?.stateDir??process.env.AGENT_DEVICE_STATE_DIR),c="number"==typeof r?setTimeout(()=>{s.destroy(),i(eu(t,l,a.meta?.requestId,a.command,!1,r))},r):void 0,u="";s.setEncoding("utf8"),s.on("data",e=>{let t=(u+=e).indexOf("\n");if(-1===t)return;let r=u.slice(0,t).trim();if(r)try{let e=JSON.parse(r);s.end(),c&&clearTimeout(c),n(e)}catch(e){c&&clearTimeout(c),i(new d("COMMAND_FAILED","Invalid daemon response",{requestId:a.meta?.requestId,line:r},e instanceof Error?e:void 0))}}),s.on("error",e=>{c&&clearTimeout(c),i(ep(e,a.meta?.requestId,!1))})})}async function ef(e,r,o){var n,i,s;let c,u=e.baseUrl?new URL(ey(e.baseUrl,"rpc")):e.httpPort?new URL(`http://127.0.0.1:${e.httpPort}/rpc`):null;if(!u)throw new d("COMMAND_FAILED","Daemon HTTP endpoint is unavailable");let p=JSON.stringify((n=r,i={includeTokenParam:!e.baseUrl},c=n.meta?.requestId??m(),"lease_allocate"!==(s=n.command)&&"lease_heartbeat"!==s&&"lease_release"!==s?{jsonrpc:"2.0",id:c,method:"agent_device.command",params:n}:{jsonrpc:"2.0",id:c,method:function(e){switch(e){case"lease_allocate":return"agent_device.lease.allocate";case"lease_heartbeat":return"agent_device.lease.heartbeat";case"lease_release":return"agent_device.lease.release"}}(n.command),params:function(e,t,a){let r={...a.includeTokenParam?{token:e.token}:{},session:e.session,tenantId:e.meta?.tenantId,runId:e.meta?.runId};switch(t){case"lease_allocate":return{...r,ttlMs:e.meta?.leaseTtlMs,backend:e.meta?.leaseBackend};case"lease_heartbeat":return{...r,leaseId:e.meta?.leaseId,ttlMs:e.meta?.leaseTtlMs};case"lease_release":return{...r,leaseId:e.meta?.leaseId}}}(n,n.command,i)})),f={"content-type":"application/json","content-length":Buffer.byteLength(p)};return e.baseUrl&&e.token&&(f.authorization=`Bearer ${e.token}`,f["x-agent-device-token"]=e.token),await new Promise((n,i)=>{let s=h(r.flags?.stateDir??process.env.AGENT_DEVICE_STATE_DIR),c=("https:"===u.protocol?a:t).request({protocol:u.protocol,host:u.hostname,port:u.port,method:"POST",path:u.pathname+u.search,headers:f},t=>{let a="";t.setEncoding("utf8"),t.on("data",e=>{a+=e}),t.on("end",()=>{y&&clearTimeout(y);try{let t=JSON.parse(a);if(t.error){let e=t.error.data??{};i(new d(l(null!=e.code?String(e.code):void 0,"COMMAND_FAILED"),String(e.message??t.error.message??"Daemon RPC request failed"),{..."object"==typeof e.details&&e.details?e.details:{},hint:"string"==typeof e.hint?e.hint:void 0,diagnosticId:"string"==typeof e.diagnosticId?e.diagnosticId:void 0,logPath:"string"==typeof e.logPath?e.logPath:void 0,requestId:r.meta?.requestId}));return}if(!t.result||"object"!=typeof t.result)return void i(new d("COMMAND_FAILED","Invalid daemon RPC response",{requestId:r.meta?.requestId}));if(e.baseUrl&&t.result.ok)return void ew(e,r,t.result).then(n).catch(i);n(t.result)}catch(e){y&&clearTimeout(y),i(new d("COMMAND_FAILED","Invalid daemon response",{requestId:r.meta?.requestId,line:a},e instanceof Error?e:void 0))}})}),m=eh(e),y="number"==typeof o?setTimeout(()=>{c.destroy(),i(eu(e,s,r.meta?.requestId,r.command,m,o))},o):void 0;c.on("error",e=>{y&&clearTimeout(y),i(ep(e,r.meta?.requestId,m))}),c.write(p),c.end()})}function eh(e){return"string"==typeof e.baseUrl&&e.baseUrl.length>0}function ey(e,t){return new URL(t,e.endsWith("/")?e:`${e}/`).toString()}async function ew(e,t,a){let r=Array.isArray(a.data?.artifacts)?a.data.artifacts:[];if(0===r.length||!e.baseUrl)return a;let n=a.data?{...a.data}:{},i=[];for(let a of r){if(!a||"object"!=typeof a||"string"!=typeof a.artifactId){i.push(a);continue}let r=function(e,t){if(e.localPath&&e.localPath.trim().length>0)return e.localPath;let a=e.fileName?.trim()||`${e.field}-${Date.now()}`;return o.resolve(t.meta?.cwd??process.cwd(),a)}(a,t);await ev({baseUrl:e.baseUrl,token:e.token,artifactId:a.artifactId,destinationPath:r,requestId:t.meta?.requestId}),n[a.field]=r,i.push({...a,localPath:r})}return n.artifacts=i,{ok:!0,data:n}}async function ev(e){var n,i;let s,l=new URL((n=e.baseUrl,i=e.artifactId,s=n.endsWith("/")?n:`${n}/`,new URL(`artifacts/${encodeURIComponent(i)}`,s).toString())),c="https:"===l.protocol?a:t;await r.promises.mkdir(o.dirname(e.destinationPath),{recursive:!0}),await new Promise((t,a)=>{let o=!1,n=e.timeoutMs??q,i=n=>{if(!o){if(o=!0,clearTimeout(u),n)return void r.promises.rm(e.destinationPath,{force:!0}).finally(()=>a(n));t()}},s=c.request({protocol:l.protocol,host:l.hostname,port:l.port,method:"GET",path:l.pathname+l.search,headers:e.token?{authorization:`Bearer ${e.token}`,"x-agent-device-token":e.token}:void 0},t=>{if((t.statusCode??500)>=400){let a="";t.setEncoding("utf8"),t.on("data",e=>{a+=e}),t.on("end",()=>{i(new d("COMMAND_FAILED","Failed to download remote artifact",{artifactId:e.artifactId,statusCode:t.statusCode,requestId:e.requestId,body:a}))});return}let a=r.createWriteStream(e.destinationPath);a.on("error",e=>{i(e instanceof Error?e:Error(String(e)))}),t.on("error",e=>{i(e instanceof Error?e:Error(String(e)))}),t.on("aborted",()=>{i(new d("COMMAND_FAILED","Remote artifact download was interrupted",{artifactId:e.artifactId,requestId:e.requestId}))}),a.on("finish",()=>{a.close(()=>i())}),t.pipe(a)}),u=setTimeout(()=>{s.destroy(new d("COMMAND_FAILED","Remote artifact download timed out",{artifactId:e.artifactId,requestId:e.requestId,timeoutMs:n}))},n);s.on("error",t=>{t instanceof d?i(t):i(new d("COMMAND_FAILED","Failed to download remote artifact",{artifactId:e.artifactId,requestId:e.requestId,timeoutMs:n},t instanceof Error?t:void 0))}),s.end()})}function eg(e=process.env.AGENT_DEVICE_DAEMON_TIMEOUT_MS){if(!e)return 9e4;let t=Number(e);return Number.isFinite(t)?Math.max(1e3,Math.floor(t)):9e4}let eI={alert:"alert",appState:"appstate",appSwitcher:"app-switcher",apps:"apps",back:"back",batch:"batch",boot:"boot",click:"click",clipboard:"clipboard",devices:"devices",diff:"diff",fill:"fill",find:"find",focus:"focus",get:"get",home:"home",is:"is",keyboard:"keyboard",logs:"logs",longPress:"longpress",network:"network",perf:"perf",pinch:"pinch",press:"press",push:"push",record:"record",replay:"replay",rotate:"rotate",scroll:"scroll",screenshot:"screenshot",settings:"settings",snapshot:"snapshot",swipe:"swipe",test:"test",trace:"trace",triggerAppEvent:"trigger-app-event",type:"type",wait:"wait"};function eb(e,t,a,r){let o=a(e[t]);if(void 0===o)throw new d("COMMAND_FAILED",r,{response:e});return o}function eA(e,t){return eb(e,t,eD,`Daemon response is missing "${t}".`)}function eM(e,t){return eD(e[t])}function e_(e,t){var a;let r;return a=eD,null===(r=e[t])?null:a(r)}function eS(e,t){return eb(e,t,eT,`Daemon response has invalid "${t}".`)}function ek(e,t){return function(e){return"tv"===e||"mobile"===e||"desktop"===e?e:void 0}(e[t])??"mobile"}function eE(e,t){let a=e[t];if(!ex(a))return;let r="number"==typeof a.x?a.x:void 0,o="number"==typeof a.y?a.y:void 0,n="number"==typeof a.width?a.width:void 0,i="number"==typeof a.height?a.height:void 0;if(void 0!==r&&void 0!==o&&void 0!==n&&void 0!==i)return{x:r,y:o,width:n,height:i}}function eD(e){return"string"==typeof e&&e.length>0?e:void 0}function eP(e){return"number"==typeof e&&Number.isFinite(e)?e:void 0}function eT(e){return"ios"===e||"macos"===e||"android"===e?e:void 0}function eN(e){return"simulator"===e||"emulator"===e||"device"===e?e:void 0}function eU(e){if(!ex(e))throw new d("COMMAND_FAILED","Daemon returned an unexpected response shape.",{value:e});return e}function ex(e){return"object"==typeof e&&null!==e}function eC(e){let t={};for(let[a,r]of Object.entries(e))void 0!==r&&(t[a]=r);return t}function eR(e,t){let a=eM(e,"bundleId"),r=eM(e,"package");return{app:eA(e,"app"),appPath:eA(e,"appPath"),platform:eS(e,"platform"),appId:a??r,bundleId:a,package:r,identifiers:A({session:t,bundleId:a,packageName:r})}}function eO(e){let t=eU(e),a=eS(t,"platform"),r=eA(t,"id"),o=eA(t,"name");return{platform:a,target:ek(t,"target"),kind:eb(t,"kind",eN,'Daemon response has invalid "kind".'),id:r,name:o,booted:"boolean"==typeof t.booted?t.booted:void 0,identifiers:f(a,r,o),ios:"ios"===a?{udid:r}:void 0,android:"android"===a?{serial:r}:void 0}}function eL(e){let t=eU(e),a=eS(t,"platform"),r=eA(t,"id"),o=eA(t,"name"),n=ek(t,"target"),i=eA(t,"device"),s={session:o,...f(a,r,i)};return{name:o,createdAt:eb(t,"createdAt",eP,'Daemon response is missing numeric "createdAt".'),device:{platform:a,target:n,id:r,name:i,identifiers:s,ios:"ios"===a?{udid:r,simulatorSetPath:e_(t,"ios_simulator_device_set")}:void 0,android:"android"===a?{serial:r}:void 0},identifiers:s}}function eF(e){return e??"default"}function eB(e={},t={}){var a;let r,o=t.transport??V,n=async(t,a=[],r={})=>{var n,i;let s=(n=e,i=r,{...n,...i}),c=await o({session:eF(s.session),command:t,positionals:a,flags:eC({stateDir:s.stateDir,daemonBaseUrl:s.daemonBaseUrl,daemonAuthToken:s.daemonAuthToken,daemonTransport:s.daemonTransport,daemonServerMode:s.daemonServerMode,tenant:s.tenant,sessionIsolation:s.sessionIsolation,runId:s.runId,leaseId:s.leaseId,leaseBackend:s.leaseBackend,platform:s.platform,target:s.target,device:s.device,udid:s.udid,serial:s.serial,iosSimulatorDeviceSet:s.iosSimulatorDeviceSet,androidDeviceAllowlist:s.androidDeviceAllowlist,runtime:s.simulatorRuntimeId,boot:s.boot,reuseExisting:s.reuseExisting,surface:s.surface,activity:s.activity,relaunch:s.relaunch,shutdown:s.shutdown,saveScript:s.saveScript,noRecord:s.noRecord,backMode:s.backMode,metroHost:s.metroHost,metroPort:s.metroPort,bundleUrl:s.bundleUrl,launchUrl:s.launchUrl,snapshotInteractiveOnly:s.interactiveOnly,snapshotCompact:s.compact,snapshotDepth:s.depth,snapshotScope:s.scope,snapshotRaw:s.raw,screenshotFullscreen:s.screenshotFullscreen,screenshotMaxSize:s.screenshotMaxSize,overlayRefs:s.overlayRefs,appsFilter:s.appsFilter,out:s.out,count:s.count,fps:s.fps,quality:s.quality,hideTouches:s.hideTouches,intervalMs:s.intervalMs,delayMs:s.delayMs,holdMs:s.holdMs,jitterPx:s.jitterPx,pixels:s.pixels,doubleTap:s.doubleTap,clickButton:s.clickButton,pauseMs:s.pauseMs,pattern:s.pattern,headless:s.headless,restart:s.restart,replayUpdate:s.replayUpdate,replayEnv:s.replayEnv,replayShellEnv:s.replayShellEnv,failFast:s.failFast,timeoutMs:s.timeoutMs,retries:s.retries,artifactsDir:s.artifactsDir,reportJunit:s.reportJunit,findFirst:s.findFirst,findLast:s.findLast,networkInclude:s.networkInclude,batchOnError:s.batchOnError,batchMaxSteps:s.batchMaxSteps,batchSteps:s.batchSteps,verbose:s.debug}),runtime:s.runtime,meta:eC({requestId:s.requestId,cwd:s.cwd,debug:s.debug,lockPolicy:s.lockPolicy,lockPlatform:s.lockPlatform,tenantId:s.tenant,runId:s.runId,leaseId:s.leaseId,leaseBackend:s.leaseBackend,leaseTtlMs:s.leaseTtlMs,sessionIsolation:s.sessionIsolation,installSource:s.installSource,retainMaterializedPaths:s.retainMaterializedPaths,materializedPathRetentionMs:s.materializedPathRetentionMs,materializationId:s.materializationId})});return c.ok||function(e){throw new d(l(e.code),e.message,{...e.details??{},hint:e.hint,diagnosticId:e.diagnosticId,logPath:e.logPath})}(c.error),c.data??{}},i=async(e={})=>{let t=await n("session_list",[],e);return(Array.isArray(t.sessions)?t.sessions:[]).map(eL)},s=async(e,t=[],a={})=>await n(e,t,a),c=(t={})=>{var a,r;return eF((a=e,r=t,{...a,...r}).session)};return{command:(a=async e=>await n(e.command,e.positionals,e.options),r=async e=>await a(e),{wait:async e=>await r(function(e){if(1!==[void 0!==e.durationMs?"durationMs":void 0,void 0!==e.text?"text":void 0,void 0!==e.ref?"ref":void 0,void 0!==e.selector?"selector":void 0].filter(Boolean).length)throw new d("INVALID_ARGS","wait command requires exactly one of durationMs, text, ref, or selector.");if(void 0!==e.durationMs)return{command:eI.wait,positionals:[String(e.durationMs)],options:e};let t=void 0!==e.timeoutMs?[String(e.timeoutMs)]:[];if(void 0!==e.text)return{command:eI.wait,positionals:["text",e.text,...t],options:e};if(void 0!==e.ref)return{command:eI.wait,positionals:[e.ref,...t],options:e};let a=e.selector;return function(e){if(!E(e))throw new d("INVALID_ARGS",`Invalid wait selector: ${e}`)}(a),{command:eI.wait,positionals:[a,...t],options:e}}(e)),alert:async(e={})=>{var t;let a;return await r((a=(t=e).action??"get",{command:eI.alert,positionals:[a,...void 0!==t.timeoutMs?[String(t.timeoutMs)]:[]],options:t}))},appState:async(e={})=>await r({command:eI.appState,positionals:[],options:e}),back:async(e={})=>await r({command:eI.back,positionals:[],options:{...e,backMode:e.mode}}),home:async(e={})=>await r({command:eI.home,positionals:[],options:e}),rotate:async e=>await r({command:eI.rotate,positionals:[e.orientation],options:e}),appSwitcher:async(e={})=>await r({command:eI.appSwitcher,positionals:[],options:e}),keyboard:async(e={})=>await r({command:eI.keyboard,positionals:e.action?[e.action]:[],options:e}),clipboard:async e=>{var t;return await r("read"===(t=e).action?{command:eI.clipboard,positionals:["read"],options:t}:{command:eI.clipboard,positionals:["write",t.text],options:t})}}),devices:{list:async(e={})=>{let t=await n(eI.devices,[],e);return(Array.isArray(t.devices)?t.devices:[]).map(eO)},boot:async(e={})=>await s(eI.boot,[],e)},sessions:{list:async(e={})=>await i(e),close:async(e={})=>{let t=c(e),a=(await n("close",[],e)).shutdown;return{session:t,shutdown:"object"==typeof a&&null!==a?a:void 0,identifiers:{session:t}}}},simulators:{ensure:async e=>{let{runtime:t,...a}=e,r=await n("ensure-simulator",[],{...a,simulatorRuntimeId:t}),o=eA(r,"udid"),i=eA(r,"device");return{udid:o,device:i,runtime:eA(r,"runtime"),created:!0===r.created,booted:!0===r.booted,iosSimulatorDeviceSet:e_(r,"ios_simulator_device_set"),identifiers:{deviceId:o,deviceName:i,udid:o}}}},apps:{install:async e=>eR(await n("install",[e.app,e.appPath],e),c(e)),reinstall:async e=>eR(await n("reinstall",[e.app,e.appPath],e),c(e)),installFromSource:async e=>(function(e,t){let a=eM(e,"bundleId"),r=eM(e,"packageName"),o=a??r??eM(e,"appId"),n=eM(e,"launchTarget")??r??a??o;if(!n)throw new d("COMMAND_FAILED",'Daemon response is missing "launchTarget".',{response:e});return{appName:eM(e,"appName"),appId:o,bundleId:a,packageName:r,launchTarget:n,installablePath:eM(e,"installablePath"),archivePath:eM(e,"archivePath"),materializationId:eM(e,"materializationId"),materializationExpiresAt:eM(e,"materializationExpiresAt"),identifiers:A({session:t,bundleId:a,packageName:r,appId:o})}})(await n("install_source",[],{...e,installSource:e.source,retainMaterializedPaths:e.retainPaths,materializedPathRetentionMs:e.retentionMs}),c(e)),list:async(e={})=>{let t=await n(eI.apps,[],e);return Array.isArray(t.apps)?t.apps.filter(e=>"string"==typeof e):[]},open:async e=>{let t=c(e),a=e.app?e.url?[e.app,e.url]:[e.app]:[],r=await n("open",a,e),o=function(e){let t=e.platform,a=eM(e,"id"),r=eM(e,"device");if("ios"!==t&&"macos"!==t&&"android"!==t||!a||!r)return;let o=ek(e,"target"),n=f(t,a,r);return{platform:t,target:o,id:a,name:r,identifiers:n,ios:"ios"===t?{udid:eM(e,"device_udid")??a,simulatorSetPath:e_(e,"ios_simulator_device_set")}:void 0,android:"android"===t?{serial:eM(e,"serial")??a}:void 0}}(r),i=eM(r,"appBundleId");return{session:t,appName:eM(r,"appName"),appBundleId:i,appId:i,startup:function(e){if(ex(e)&&"number"==typeof e.durationMs&&"string"==typeof e.measuredAt&&"string"==typeof e.method)return{durationMs:e.durationMs,measuredAt:e.measuredAt,method:e.method,appTarget:eM(e,"appTarget"),appBundleId:eM(e,"appBundleId")}}(r.startup),runtime:function(e){if(!ex(e))return;let t=e.platform,a=eM(e,"metroHost"),r="number"==typeof e.metroPort?e.metroPort:void 0;return{platform:"ios"===t||"android"===t?t:void 0,metroHost:a,metroPort:r,bundleUrl:eM(e,"bundleUrl"),launchUrl:eM(e,"launchUrl")}}(r.runtime),device:o,identifiers:{session:t,deviceId:o?.id,deviceName:o?.name,udid:o?.ios?.udid,serial:o?.android?.serial,appId:i,appBundleId:i}}},close:async(e={})=>{let t=c(e),a=(await n("close",e.app?[e.app]:[],e)).shutdown;return{session:t,closedApp:e.app,shutdown:"object"==typeof a&&null!==a?a:void 0,identifiers:{session:t}}},push:async e=>{var t;return await s(eI.push,[e.app,"string"==typeof(t=e.payload)?t:JSON.stringify(t)],e)},triggerEvent:async e=>{var t;return await s(eI.triggerAppEvent,[(t=e).event,...t.payload?[JSON.stringify(t.payload)]:[]],e)}},materializations:{release:async e=>{var t;return{released:!0===(t=await n("release_materialized_paths",[],{...e,materializationId:e.materializationId})).released,materializationId:eA(t,"materializationId"),identifiers:{}}}},leases:{allocate:async e=>eH(await n("lease_allocate",[],{...e,leaseId:void 0,leaseTtlMs:e.ttlMs})),heartbeat:async e=>eH(await n("lease_heartbeat",[],{...e,leaseTtlMs:e.ttlMs})),release:async e=>({released:!0===(await n("lease_release",[],e)).released})},metro:{prepare:async t=>await D({projectRoot:t.projectRoot??e.cwd,kind:t.kind,publicBaseUrl:t.publicBaseUrl,proxyBaseUrl:t.proxyBaseUrl,proxyBearerToken:t.bearerToken,bridgeScope:t.bridgeScope,launchUrl:t.launchUrl,companionProfileKey:t.companionProfileKey,companionConsumerKey:t.companionConsumerKey,metroPort:t.port,listenHost:t.listenHost,statusHost:t.statusHost,startupTimeoutMs:t.startupTimeoutMs,probeTimeoutMs:t.probeTimeoutMs,reuseExisting:t.reuseExisting,installDependenciesIfNeeded:t.installDependenciesIfNeeded,runtimeFilePath:t.runtimeFilePath,logPath:t.logPath})},capture:{snapshot:async(e={})=>{var t;let a=c(e),r=await n(eI.snapshot,[],e),o=eM(r,"appBundleId"),i="object"==typeof r.visibility&&null!==r.visibility?r.visibility:void 0;return{nodes:Array.isArray(t=r.nodes)?t:[],truncated:!0===r.truncated,appName:eM(r,"appName"),appBundleId:o,...i?{visibility:i}:{},warnings:Array.isArray(r.warnings)?r.warnings.filter(e=>"string"==typeof e):void 0,identifiers:{session:a,appId:o,appBundleId:o}}},screenshot:async(e={})=>{let t=c(e),a=await n(eI.screenshot,e.path?[e.path]:[],{...e,screenshotFullscreen:e.fullscreen,screenshotMaxSize:e.maxSize});return{path:eA(a,"path"),overlayRefs:function(e){let t=e.overlayRefs;if(!Array.isArray(t))return;let a=[];for(let e of t){if(!ex(e))continue;let t=eM(e,"ref"),r=eE(e,"rect"),o=eE(e,"overlayRect"),n=function(e,t){let a=e[t];if(!ex(a))return;let r="number"==typeof a.x?a.x:void 0,o="number"==typeof a.y?a.y:void 0;if(void 0!==r&&void 0!==o)return{x:r,y:o}}(e,"center");t&&r&&o&&n&&a.push({ref:t,label:eM(e,"label"),rect:r,overlayRect:o,center:n})}return a}(a),identifiers:{session:t}}},diff:async e=>await s(eI.diff,[e.kind],{...e,interactiveOnly:e.interactiveOnly,compact:e.compact,depth:e.depth,scope:e.scope,raw:e.raw})},interactions:{click:async e=>await s(eI.click,eq(e),{...e,clickButton:e.button}),press:async e=>await s(eI.press,eq(e),e),longPress:async e=>await s(eI.longPress,[String(e.x),String(e.y),...e$(e.durationMs)],e),swipe:async e=>await s(eI.swipe,[String(e.from.x),String(e.from.y),String(e.to.x),String(e.to.y),...e$(e.durationMs)],e),focus:async e=>await s(eI.focus,[String(e.x),String(e.y)],e),type:async e=>await s(eI.type,[e.text],e),fill:async e=>await s(eI.fill,[...eq(e),e.text],e),scroll:async e=>await s(eI.scroll,[e.direction,...e$(e.amount)],e),pinch:async e=>await s(eI.pinch,[String(e.scale),...e$(e.x),...e$(e.y)],e),get:async e=>{var t;return await s(eI.get,[e.format,...void 0!==(t=e).ref?[t.ref,...ez(t.label)]:[t.selector]],e)},is:async e=>await s(eI.is,[e.predicate,e.selector,..."text"===e.predicate?[e.value]:[]],e),find:async e=>await s(eI.find,function(e){let t=e.locator&&"any"!==e.locator?[e.locator,e.query]:[e.query];switch(e.action){case void 0:case"click":case"focus":case"exists":return e.action?[...t,e.action]:t;case"getText":return[...t,"get","text"];case"getAttrs":return[...t,"get","attrs"];case"wait":return[...t,"wait",...e$(e.timeoutMs)];case"fill":case"type":return[...t,e.action,e.value]}}(e),{...e,findFirst:e.first,findLast:e.last})},replay:{run:async e=>await s(eI.replay,[e.path],{...e,replayUpdate:e.update,replayEnv:e.env,replayShellEnv:ej(process.env)}),test:async e=>await s(eI.test,e.paths,{...e,replayUpdate:e.update,replayEnv:e.env,replayShellEnv:ej(process.env)})},batch:{run:async e=>await s(eI.batch,[],{...e,batchSteps:e.steps,batchOnError:e.onError,batchMaxSteps:e.maxSteps})},observability:{perf:async(e={})=>await s(eI.perf,[],e),logs:async(e={})=>{var t;return await s(eI.logs,[(t=e).action??"path",...ez(t.message)],e)},network:async(e={})=>{var t;return await s(eI.network,[...(t=e).action?[t.action]:[],...e$(t.limit)],{...e,networkInclude:e.include})}},recording:{record:async e=>await s(eI.record,[e.action,...ez(e.path)],e),trace:async e=>await s(eI.trace,[e.action,...ez(e.path)],e)},settings:{update:async e=>await s(eI.settings,[e.setting,e.state,..."permission"in e?[e.permission]:[],..."mode"in e&&e.mode?[e.mode]:[]],e)}}}function eq(e){return void 0!==e.ref?[e.ref,...ez(e.label)]:void 0!==e.selector?[e.selector]:[String(e.x),String(e.y)]}function ez(e){return void 0===e?[]:[e]}function e$(e){return void 0===e?[]:[String(e)]}function ej(e){let t={};for(let[a,r]of Object.entries(e))"string"==typeof r&&a.startsWith("AD_VAR_")&&(t[a]=r);return t}function eH(e){let t=e.lease;if(!t||"object"!=typeof t||Array.isArray(t))throw Error("Invalid lease response from daemon");return{leaseId:eA(t,"leaseId"),tenantId:eA(t,"tenantId"),runId:eA(t,"runId"),backend:eA(t,"backend"),createdAt:"number"==typeof t.createdAt?t.createdAt:void 0,heartbeatAt:"number"==typeof t.heartbeatAt?t.heartbeatAt:void 0,expiresAt:"number"==typeof t.expiresAt?t.expiresAt:void 0}}export{eI as CLIENT_COMMANDS,eB as createAgentDeviceClient,V as sendToDaemon};
|
package/dist/src/989.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import t from"node:dns/promises";import e from"node:net";import{promises as a}from"node:fs";import r from"node:os";import o from"node:path";import{AppError as i}from"./9152.js";import{runCmd as n}from"./9818.js";import{expandUserHomePath as s}from"./3267.js";
|
|
1
|
+
import t from"node:dns/promises";import e from"node:net";import{promises as a}from"node:fs";import r from"node:os";import o from"node:path";import{AppError as i}from"./9152.js";import{runCmd as n}from"./9818.js";import{expandUserHomePath as s}from"./3267.js";import{resolveTimeoutMs as l}from"./4829.js";let c=[".zip",".tar",".tar.gz",".tgz"],h=Object.freeze([...c]),u=l(process.env.AGENT_DEVICE_SOURCE_DOWNLOAD_TIMEOUT_MS,12e4,1e3),d=["1","true","yes","on"];async function w(t){let e=[];try{let a=await p(t.source,{signal:t.signal,downloadTimeoutMs:t.downloadTimeoutMs});e.push(a.cleanup);let r=await b(a.localPath,{archivePath:void 0,isInstallablePath:t.isInstallablePath,installableLabel:t.installableLabel,allowArchiveExtraction:!1!==t.allowArchiveExtraction,registerCleanup:t=>{e.push(t)}});return{archivePath:r.archivePath,installablePath:r.installablePath,cleanup:async()=>{await R(e)}}}catch(t){throw await R(e),t}}async function p(t,e){if("path"===t.kind)return{localPath:s(t.path),cleanup:async()=>{}};let i=await a.mkdtemp(o.join(r.tmpdir(),"agent-device-source-"));try{return{localPath:await f(i,t.url,t.headers,e),cleanup:async()=>{await a.rm(i,{recursive:!0,force:!0})}}}catch(t){throw await a.rm(i,{recursive:!0,force:!0}),t}}async function f(t,e,r,n){let s;try{s=new URL(e)}catch{throw new i("INVALID_ARGS",`Invalid source URL: ${e}`)}await m(s);let l=n?.signal;if(l?.aborted)throw new i("COMMAND_FAILED","request canceled",{reason:"request_canceled"});let c=new AbortController,h=()=>{c.abort(l?.reason)};l?.addEventListener("abort",h,{once:!0});let d=n?.downloadTimeoutMs??u,w=setTimeout(()=>{c.abort(Error("download timeout"))},d);try{let e=await fetch(s,{headers:r,redirect:"follow",signal:c.signal});if(!e.ok)throw new i("COMMAND_FAILED",`Failed to download app source: ${e.status} ${e.statusText}`,{status:e.status,statusText:e.statusText,url:s.toString()});let n=function(t,e){let a=t.headers.get("content-disposition"),r=a?.match(/filename\*?=(?:UTF-8'')?"?([^";]+)"?/i),i=r?.[1]?.trim();if(i)return o.basename(i);let n=o.basename(e.pathname);return n||"downloaded-artifact.bin"}(e,s),l=o.join(t,n),h=e.body;if(!h)throw new i("COMMAND_FAILED","Download response body was empty",{url:s.toString()});let u=await a.open(l,"w");try{for await(let t of h)await u.write(t)}finally{await u.close()}return l}catch(t){if(l?.aborted)throw new i("COMMAND_FAILED","request canceled",{reason:"request_canceled"},t);if(c.signal.aborted)throw new i("COMMAND_FAILED",`App source download timed out after ${d}ms`,{timeoutMs:d,url:s.toString()},t);throw t}finally{l?.removeEventListener("abort",h),clearTimeout(w)}}async function m(e){if("http:"!==e.protocol&&"https:"!==e.protocol)throw new i("INVALID_ARGS",`Unsupported source URL protocol: ${e.protocol}`);if(!d.includes((process.env.AGENT_DEVICE_ALLOW_PRIVATE_SOURCE_URLS??"").toLowerCase())){if(A(e.hostname.toLowerCase()))throw new i("INVALID_ARGS",`Source URL host is not allowed: ${e.hostname}`,{hint:"Use a public artifact URL, or set AGENT_DEVICE_ALLOW_PRIVATE_SOURCE_URLS=1 for trusted private-network daemons."});if((await t.lookup(e.hostname,{all:!0,verbatim:!0}).catch(()=>[])).some(t=>L(t.address)))throw new i("INVALID_ARGS",`Source URL host resolved to a private or loopback address: ${e.hostname}`,{hint:"Use a public artifact URL, or set AGENT_DEVICE_ALLOW_PRIVATE_SOURCE_URLS=1 for trusted private-network daemons."})}}function _(t){var e,a,r,o;let i=t instanceof URL?t:new URL(t),n=i.hostname.toLowerCase();if(!n)return!1;let s=i.pathname;return e=n,a=s,("api.github.com"===e?/^\/repos\/[^/]+\/[^/]+\/actions\/artifacts\/\d+\/zip$/i.test(a):"github.com"===e&&/^\/[^/]+\/[^/]+\/(?:actions\/runs\/\d+\/artifacts\/\d+|suites\/\d+\/artifacts\/\d+)$/i.test(a))||(r=n,o=s,("expo.dev"===r||!!r.endsWith(".expo.dev"))&&/^\/(?:artifacts\/eas\/|accounts\/[^/]+\/projects\/[^/]+\/builds\/)/i.test(o))}function A(t){return!!(!t||"localhost"===t||t.endsWith(".localhost"))||L(t)}function L(t){let a,r=e.isIP(t);return 4===r?function(t){let e=t.split(".").map(t=>Number.parseInt(t,10));if(4!==e.length||e.some(t=>Number.isNaN(t)||t<0||t>255))return!1;let[a,r]=e;return 10===a||127===a||169===a&&254===r||172===a&&!!(r>=16)&&!!(r<=31)||192===a&&168===r}(t):6===r&&!!("::1"===(a=t.toLowerCase())||a.startsWith("fc")||a.startsWith("fd")||a.startsWith("fe80:"))}async function b(t,e){let r=await a.stat(t).catch(()=>null);if(!r)throw new i("INVALID_ARGS",`App source not found: ${t}`);if(e.isInstallablePath(t,r))return{archivePath:e.archivePath,installablePath:t};if(r.isFile()&&C(t)){if(!e.allowArchiveExtraction)throw new i("INVALID_ARGS",`URL sources must point directly to a ${e.installableLabel}; archive extraction is not allowed`,{path:t});let a=await v(t);return e.registerCleanup(a.cleanup),await b(a.outputPath,{...e,archivePath:e.archivePath??t})}if(r.isDirectory()){let a=await I(t,e.isInstallablePath);if(1===a.length)return{archivePath:e.archivePath,installablePath:a[0]};if(a.length>1)throw new i("INVALID_ARGS",`Found multiple ${e.installableLabel} candidates under ${t}`,{matches:a});let r=await I(t,(t,e)=>e.isFile()&&C(t));if(1===r.length){if(!e.allowArchiveExtraction)throw new i("INVALID_ARGS",`URL sources must point directly to a ${e.installableLabel}; nested archives are not allowed`,{path:r[0]});let t=await v(r[0]);return e.registerCleanup(t.cleanup),await b(t.outputPath,{...e,archivePath:e.archivePath??r[0]})}if(r.length>1)throw new i("INVALID_ARGS",`Found multiple nested archives under ${t}; expected one ${e.installableLabel} source`,{matches:r})}throw new i("INVALID_ARGS",`Expected ${e.installableLabel} source, but got ${t}`)}async function I(t,e){let r=[],i=[{path:t,depth:0}];for(;i.length>0;){let t=i.shift();if(!t)continue;let n=await a.readdir(t.path,{withFileTypes:!0});for(let a of(n.sort((t,e)=>t.name.localeCompare(e.name)),n)){let n=o.join(t.path,a.name);if(e(n,a)){r.push(n);continue}a.isDirectory()&&t.depth<5&&i.push({path:n,depth:t.depth+1})}}return r}async function v(t){let e=await a.mkdtemp(o.join(r.tmpdir(),"agent-device-archive-"));try{return t.toLowerCase().endsWith(".zip")?await E(t,e):t.toLowerCase().endsWith(".tar.gz")||t.toLowerCase().endsWith(".tgz")?await n("tar",["-xzf",t,"-C",e]):await n("tar",["-xf",t,"-C",e]),{outputPath:e,cleanup:async()=>{await a.rm(e,{recursive:!0,force:!0})}}}catch(t){throw await a.rm(e,{recursive:!0,force:!0}),t}}async function E(t,e){await n("unzip",["-q",t,"-d",e])}function C(t){let e=t.toLowerCase();return c.some(t=>e.endsWith(t))}async function R(t){for(let e=t.length-1;e>=0;e-=1)await t[e]()}export{h as ARCHIVE_EXTENSIONS,L as isBlockedIpAddress,A as isBlockedSourceHostname,_ as isTrustedInstallSourceUrl,w as materializeInstallablePath,m as validateDownloadSourceUrl};
|