agent-device 0.12.2 → 0.12.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/155.js +26 -26
- package/dist/src/android-apps.d.ts +12 -0
- package/dist/src/android-apps.js +1 -0
- package/dist/src/bin.js +46 -45
- package/dist/src/daemon.js +1 -1
- package/dist/src/index.d.ts +36 -2
- package/dist/src/index.js +3 -3
- package/dist/src/remote-config.d.ts +1 -0
- package/dist/src/remote-config.js +1 -1
- package/package.json +5 -1
- package/skills/agent-device/SKILL.md +1 -1
- package/skills/agent-device/references/remote-tenancy.md +60 -58
- package/skills/agent-device/references/verification.md +3 -1
- package/dist/src/924.js +0 -1
package/dist/src/daemon.js
CHANGED
|
@@ -2,7 +2,7 @@ let e,t;import r from"node:crypto";import a,{promises as i}from"node:fs";import
|
|
|
2
2
|
`}(e,this.buildOptimizedActions(e));a.writeFileSync(t,i)}catch{}}defaultTracePath(e){let t=tF.safeSessionName(e.name),r=new Date().toISOString().replace(/[:.]/g,"-");return n.join(this.sessionsDir,`${t}-${r}.trace.log`)}resolveAppLogPath(e){return n.join(this.sessionsDir,tF.safeSessionName(e),"app.log")}resolveAppLogPidPath(e){return n.join(this.sessionsDir,tF.safeSessionName(e),"app-log.pid")}static safeSessionName(e){return e.replace(/[^a-zA-Z0-9._-]/g,"_")}static expandHome(e,t){return e4(e,{cwd:t})}resolveScriptPath(e){if(e.saveScriptPath)return tF.expandHome(e.saveScriptPath);a.existsSync(this.sessionsDir)||a.mkdirSync(this.sessionsDir,{recursive:!0});let t=tF.safeSessionName(e.name),r=new Date(e.createdAt).toISOString().replace(/[:.]/g,"-");return n.join(this.sessionsDir,`${t}-${r}.ad`)}buildOptimizedActions(e){let t=[];for(let r of e.actions){if("snapshot"===r.command)continue;let a=Array.isArray(r.result?.selectorChain)&&r.result?.selectorChain.every(e=>"string"==typeof e)?r.result.selectorChain:[];if(a.length>0&&(t_(r.command)||"fill"===r.command||"get"===r.command)){let e=a.join(" || ");if(t_(r.command)){t.push({...r,positionals:[e]});continue}if("fill"===r.command){let a=eh(r);if(a.length>0){t.push({...r,positionals:[e,a]});continue}}if("get"===r.command){let a=r.positionals?.[0];if("text"===a||"attrs"===a){t.push({...r,positionals:[a,e]});continue}}}if(t_(r.command)||"fill"===r.command||"get"===r.command){let a=r.result?.refLabel;"string"==typeof a&&a.trim().length>0&&t.push({ts:r.ts,command:"snapshot",positionals:[],flags:{platform:e.device.platform,snapshotInteractiveOnly:!0,snapshotCompact:!0,snapshotScope:a.trim()},result:{scope:a.trim()}})}t.push(r)}return t}}let tU=["platform","device","udid","serial","out","verbose","metroHost","metroPort","bundleUrl","launchUrl","snapshotInteractiveOnly","snapshotCompact","snapshotDepth","snapshotScope","snapshotRaw","screenshotFullscreen","relaunch","saveScript","noRecord","fps","hideTouches","count","intervalMs","delayMs","holdMs","jitterPx","doubleTap","clickButton","pauseMs","pattern"],tG="app-log.pid";function tV(e){let t=e.trim();if(!t)return null;if(/^\d+$/.test(t))return{pid:Number.parseInt(t,10)};try{let e=JSON.parse(t);if(!Number.isInteger(e.pid)||e.pid<=0)return null;return e}catch{return null}}function tq(e,t){if(!e)return;let r=n.dirname(e);a.existsSync(r)||a.mkdirSync(r,{recursive:!0});let i={pid:t,startTime:Z(t)??void 0,command:es(t)??void 0};a.writeFileSync(e,`${JSON.stringify(i)}
|
|
3
3
|
`)}function tj(e){if(e&&a.existsSync(e))try{a.unlinkSync(e)}catch{}}async function tH(e,t=2e3){await Promise.race([e.then(()=>void 0).catch(()=>void 0),new Promise(e=>setTimeout(e,t))])}async function tB(e){await new Promise(t=>setTimeout(t,e))}function tK(e,t){let r=t.includeTokens?.filter(e=>e.length>0)??[],a="",i=a=>{(!(r.length>0)||r.some(e=>a.includes(e)))&&e.write(function(e,t){if(0===t.length)return e;let r=e;for(let e of t)r=r.replace(e,"[REDACTED]");return r}(a,t.redactionPatterns))};return{onChunk:e=>{let t=`${a}${e}`.split("\n");for(let e of(a=t.pop()??"",t))i(`${e}
|
|
4
4
|
`)},flush:()=>{a&&(i(a),a="")}}}function tz(e,t,r){let a=e.stdout,i=e.stderr;return a&&i?(a.setEncoding("utf8"),i.setEncoding("utf8"),a.on("data",r.writer.onChunk),i.on("data",r.writer.onChunk),t.on("error",()=>{e.killed||e.kill("SIGKILL")}),e.on("error",()=>t.destroy()),new Promise(a=>{e.on("close",e=>{r.writer.flush(),r.endStreamOnClose&&t.end(),a({stdout:"",stderr:"",exitCode:e??1})})})):Promise.resolve({stdout:"",stderr:"missing stdio pipes",exitCode:1})}function tW(e){if(!/^[a-zA-Z0-9._:-]+$/.test(e))throw new tv("INVALID_ARGS",`Invalid Android package name for logs: ${e}`)}async function tJ(e,t){let r=(await th("adb",["-s",e,"shell","pidof",t],{allowFailure:!0})).stdout.trim().split(/\s+/)[0];return r&&/^\d+$/.test(r)?r:null}async function tZ(e,t){var r,a;let i;tW(t);let n=await tJ(e,t),o=await th("adb",["-s",e,"logcat","-d","-v","time","-t","4000"],{allowFailure:!0,timeoutMs:3e3});if(0!==o.exitCode||0===o.stdout.trim().length)return null;let s=function(e,t,r){let a=new Set;for(let i of(r&&a.add(r),e.split("\n")))if(i.includes(t))for(let e of function(e,t){let r=t.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),a=[RegExp(`\\bStart proc\\s+(\\d+):${r}(?:\\b|/)`,"i"),RegExp(`\\b(\\d+):${r}(?:\\b|/)`,"i"),RegExp(`${r}.*?\\bpid\\s*[=:]?\\s*(\\d+)\\b`,"i"),RegExp(`\\bpid\\s*[=:]?\\s*(\\d+)\\b.*${r}`,"i")],i=[];for(let t of a){let r=t.exec(e),a=r?.[1];a&&/^\d+$/.test(a)&&i.push(a)}return i}(i,t))a.add(e);return[...a]}(o.stdout,t,n);if(0===s.length)return null;let l=(r=o.stdout,a=t,i=new Set(s),r.split("\n").filter(e=>{var t;let r;if(!e.trim())return!1;if(e.includes(a))return!0;let n=(t=e,r=/\(\s*(\d+)\)\s*:/.exec(t),r?.[1]??null);return!!n&&i.has(n)}).join("\n"));return 0===l.trim().length?null:{pid:n,text:l,recoveredPids:s}}async function tX(e,t,r,a,i){let n,s,l="recovering",d=!1,u=(async()=>{try{for(;!d;){let u=await tJ(e,t);if(!u){l="recovering",await tB(1e3);continue}let c=o("adb",["-s",e,"logcat","-v","time","--pid",u],{stdio:["ignore","pipe","pipe"]});n=c;let f=tK(r,{redactionPatterns:a});if(s=tz(c,r,{endStreamOnClose:!1,writer:f}),"number"==typeof c.pid&&(tq(i,c.pid),l="active"),await s,tj(i),n=void 0,s=void 0,d)break;l="recovering",await tB(500)}return{stdout:"",stderr:"",exitCode:0}}finally{r.end(),tj(i)}})();return{backend:"android",getState:()=>l,startedAt:Date.now(),wait:u,stop:async()=>{d=!0,n&&!n.killed&&n.kill("SIGINT"),s&&await tH(s),n&&!n.killed&&n.kill("SIGKILL"),await tH(u),tj(i)}}}function tY(e){return`subsystem == "${e}" OR processImagePath ENDSWITH[c] "/${e}" OR senderImagePath ENDSWITH[c] "/${e}"`}async function tQ(e){let{deviceId:t,appBundleId:r,startedAt:a,simulatorSetPath:i}=e,n=g(["spawn",t,"log","show","--style","compact","--info","--predicate",tY(r)],{simulatorSetPath:i});"number"==typeof a&&Number.isFinite(a)&&a>0?n.push("--start",`@${Math.floor(a/1e3)}`):n.push("--last","5m");let o=await th("xcrun",n,{allowFailure:!0,timeoutMs:4e3});if(0!==o.exitCode||0===o.stdout.trim().length)return null;let s=o.stdout.split("\n").map(e=>e.trimEnd()).filter(e=>{let t=e.trim();return t.length>0&&!t.startsWith("Timestamp Ty Process[PID:TID]")});return 0===s.length?null:{text:`${s.join("\n")}
|
|
5
|
-
`,recoveredLineCount:s.length}}async function t0(e,t,r,a,i,n){let s="active",l=o("xcrun",function(e){let{deviceId:t,appBundleId:r,simulatorSetPath:a}=e;return g(["spawn",t,"log","stream","--style","compact","--level","info","--predicate",tY(r)],{simulatorSetPath:a})}({deviceId:e,appBundleId:t,simulatorSetPath:i}),{stdio:["ignore","pipe","pipe"]}),d=tK(r,{redactionPatterns:a});"number"==typeof l.pid&&tq(n,l.pid);let u=tz(l,r,{endStreamOnClose:!0,writer:d}).then(e=>(0!==e.exitCode&&(s="failed"),tj(n),e));return{backend:"ios-simulator",getState:()=>s,startedAt:Date.now(),wait:u,stop:async()=>{l.killed||l.kill("SIGINT"),await tH(u),l.killed||l.kill("SIGKILL"),await tH(u),tj(n)}}}async function t1(e,t,r,a){let i="active",n=o("log",["stream","--style","compact","--predicate",tY(e)],{stdio:["ignore","pipe","pipe"]}),s=tK(t,{redactionPatterns:r});"number"==typeof n.pid&&tq(a,n.pid);let l=tz(n,t,{endStreamOnClose:!0,writer:s}).then(e=>(0!==e.exitCode&&(i="failed"),tj(a),e));return{backend:"macos",getState:()=>i,startedAt:Date.now(),wait:l,stop:async()=>{n.killed||n.kill("SIGINT"),await tH(l),n.killed||n.kill("SIGKILL"),await tH(l),tj(a)}}}async function t2(e,t,r,a){let i="active",n=o("xcrun",["devicectl","device","log","stream","--device",e],{stdio:["ignore","pipe","pipe"]}),s=tK(t,{redactionPatterns:r});"number"==typeof n.pid&&tq(a,n.pid);let l=tz(n,t,{endStreamOnClose:!0,writer:s}).then(e=>(0!==e.exitCode&&(i="failed"),tj(a),e));return{backend:"ios-device",getState:()=>i,startedAt:Date.now(),wait:l,stop:async()=>{n.killed||n.kill("SIGINT"),await tH(l),n.killed||n.kill("SIGKILL"),await tH(l),tj(a)}}}let t5=RegExp("\\b(GET|POST|PUT|PATCH|DELETE|HEAD|OPTIONS)\\b\\s+https?:\\/\\/","i"),t3=/https?:\/\/[^\s"'<>\])]+/i,t4=[/\bstatus(?:Code)?["'=: ]+([1-5]\d{2})\b/i,/\bresponse(?:\s+code)?["'=: ]+([1-5]\d{2})\b/i,/\bHTTP\/[0-9.]+\s+([1-5]\d{2})\b/i];function t8(e,t,r=e.limits.maxEntries){let a=[...e.entries],i=new Set(a.map(e=>t9(e)));for(let e of t.entries){let t=t9(e);if(!i.has(t)&&(i.add(t),a.push(e),a.length>=r))break}return{...e,matchedLines:a.length,entries:a}}function t6(e,t){let r=rl(t?.maxEntries,25,1,200),a=t?.backend,i=t?.include??"summary",n=rl(t?.maxPayloadChars,2048,64,16384),o=rl(t?.maxScanLines,4e3,100,2e4),s=e.split("\n"),l=Math.max(0,s.length-o),d=s.slice(l),u=[];for(let e=d.length-1;e>=0&&u.length<r;e-=1){let t=d[e];if(!t?.trim())continue;let r=function(e,t,r,a,i,n){let o=e[t]?.trim();if(!o)return null;let s=function(e){let t=e.indexOf("{");if(t<0)return null;let r=e.lastIndexOf("}");if(r<=t)return null;let a=e.slice(t,r+1);try{let e=JSON.parse(a);return e&&"object"==typeof e?e:null}catch{return null}}(o),l=ri(s,["method","httpMethod"]),d=ri(s,["url","requestUrl"]),u=function(e,t){if(!e)return null;for(let r of t){let t=e[r];if("number"==typeof t&&Number.isInteger(t))return t;if("string"==typeof t&&/^\d{3}$/.test(t.trim()))return Number.parseInt(t.trim(),10)}return null}(s,["status","statusCode","responseCode"]),c=t5.exec(o),f=/\bmethod["'=: ]+([A-Z]+)\b/i.exec(o),p=(l??f?.[1]??c?.[1])?.toUpperCase(),m=t3.exec(o),h=d??m?.[0];if(!h)return null;let g=u??re(o)??void 0;if(!(l||f?.[1]||c?.[1]||void 0!==g||/\bURL["'=: ]+https?:\/\//i.test(o)||/\bheaders?["'=: ]+/i.test(o)||/\b(?:requestBody|responseBody|payload|request|response)["'=: ]+/i.test(o)))return null;let w={method:p,url:h,status:g,timestamp:rt(o),packetId:rr(o)??void 0,durationMs:ra(o)??void 0,raw:rs(o,n),line:r};if("android"===a&&function(e,t,r){let a=t7(t,r,5),i=e.packetId??a.map(e=>rr(e)).find(e=>"string"==typeof e&&e.length>0);i&&(e.packetId=i);let n=i?t7(t,r,12).filter(e=>rr(e)===i):a;e.timestamp||(e.timestamp=n.map(e=>rt(e)).find(e=>"string"==typeof e&&e.length>0)),void 0===e.status&&(e.status=n.map(e=>re(e)).find(e=>"number"==typeof e)),void 0===e.durationMs&&(e.durationMs=n.map(e=>ra(e)).find(e=>"number"==typeof e))}(w,e,t),"headers"===i||"all"===i){let e=function(e,t){if(t){let e=t.headers??t.requestHeaders??t.responseHeaders;if(void 0!==e)return ro(e)}let r=/\bheaders?["'=: ]+(\{.*\})/i.exec(e);return r?.[1]?.trim()}(o,s);e&&(w.headers=rs(e,n))}if("body"===i||"all"===i){let e=rn(o,s,["requestBody","body","payload","request"]),t=rn(o,s,["responseBody","response"]);e&&(w.requestBody=rs(e,n)),t&&(w.responseBody=rs(t,n))}return w}(d,e,l+e+1,a,i,n);r&&u.push(r)}return{path:t?.path??"<memory>",exists:!0,scannedLines:d.length,matchedLines:u.length,entries:u,include:i,limits:{maxEntries:r,maxPayloadChars:n,maxScanLines:o}}}function t9(e){return`${e.timestamp??""}|${e.method??""}|${e.url}|${e.status??""}|${e.raw}`}function t7(e,t,r){let a=[],i=Math.max(0,t-r),n=Math.min(e.length-1,t+r);for(let t=i;t<=n;t+=1){let r=e[t]?.trim();r&&a.push(r)}return a}function re(e){for(let t of t4){let r=t.exec(e);if(!r)continue;let a=Number.parseInt(r[1]??"",10);if(Number.isInteger(a))return a}return null}function rt(e){let t=/\b\d{4}-\d{2}-\d{2}[ T]\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z)?\b/.exec(e);if(t)return t[0];let r=/\b\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d+\b/.exec(e);return r?.[0]}function rr(e){let t=/\bpacket id (\d+)\b/i.exec(e);return t?.[1]??null}function ra(e){let t=/\b(?:duration|elapsed request\/response time, ms)[:= ]+(\d+)\b/i.exec(e);if(!t)return null;let r=Number.parseInt(t[1]??"",10);return Number.isInteger(r)?r:null}function ri(e,t){if(e)for(let r of t){let t=e[r];if("string"==typeof t&&t.trim().length>0)return t.trim()}}function rn(e,t,r){if(t){for(let e of r)if(void 0!==t[e])return ro(t[e])}for(let t of r){let r=t.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),a=RegExp(`\\b${r}["'=: ]+(.+)$`,"i").exec(e);if(a?.[1])return a[1].trim()}}function ro(e){if("string"==typeof e)return e;try{return JSON.stringify(e)}catch{return String(e)}}function rs(e,t){return e.length<=t?e:`${e.slice(0,t)}...<truncated>`}function rl(e,t,r,a){return void 0!==e&&Number.isInteger(e)?Math.max(r,Math.min(a,e)):t}function rd(e,t){let r=process.env[e];if(!r)return t;let a=Number.parseInt(r,10);return Number.isInteger(a)&&a>0?a:t}function ru(e){let t=n.dirname(e);a.existsSync(t)||a.mkdirSync(t,{recursive:!0}),function(e,t){if(a.existsSync(e)&&!(a.statSync(e).size<t.maxBytes))for(let r=t.maxRotatedFiles;r>=1;r-=1){let t=1===r?e:`${e}.${r-1}`,i=`${e}.${r}`;a.existsSync(t)&&(a.existsSync(i)&&a.unlinkSync(i),a.renameSync(t,i))}}(e,{maxBytes:rd("AGENT_DEVICE_APP_LOG_MAX_BYTES",5242880),maxRotatedFiles:rd("AGENT_DEVICE_APP_LOG_MAX_FILES",1)})}async function rc(e){var t,r,i,n;let o,s,l,d,{device:u,appBundleId:c,appLogState:f,appLogStartedAt:p,appLogPath:m,maxEntries:h,include:g,maxPayloadChars:w,maxScanLines:v}=e,y="macos"===u.platform?"macos":"ios"===u.platform?"device"===u.kind?"ios-device":"ios-simulator":"android",I=(t={backend:y,maxEntries:h,include:g,maxPayloadChars:w,maxScanLines:v},o=rl(t?.maxEntries,25,1,200),s=t?.include??"summary",l=rl(t?.maxPayloadChars,2048,64,16384),d=rl(t?.maxScanLines,4e3,100,2e4),a.existsSync(m)?t6(a.readFileSync(m,"utf8"),{...t,path:m}):{path:m,exists:!1,scannedLines:0,matchedLines:0,entries:[],include:s,limits:{maxEntries:o,maxPayloadChars:l,maxScanLines:d}}),S=[],A=await rf({device:u,appBundleId:c,appLogPath:m,appLogState:f});if(A){let e=await tZ(u.id,c);if(e){let t=t6(e.text,{path:`${m} (adb logcat recovery)`,backend:"android",maxEntries:h,include:g,maxPayloadChars:w,maxScanLines:v});t.entries.length>0&&(I=t8(t,I,h),S.push((r=A,i=e.recoveredPids,"stale-active"===r.reason?`Session app log stream was still bound to prior Android PID ${r.trackedPid}. Recovered recent Android HTTP entries from adb logcat for PID set ${i.join(", ")}.`:`Session app log stream was inactive. Recovered recent Android HTTP entries from adb logcat for PID set ${i.join(", ")}.`)))}}if("ios"===u.platform&&"simulator"===u.kind&&c&&0===I.entries.length){let e=await rm({deviceId:u.id,appBundleId:c,startedAt:p,simulatorSetPath:u.simulatorSetPath,appLogPath:m,maxEntries:h,include:g,maxPayloadChars:w,maxScanLines:v});e&&(e.dump.entries.length>0?(I=t8(e.dump,I,h),S.push(`Recovered ${e.dump.entries.length} iOS simulator HTTP entr${1===e.dump.entries.length?"y":"ies"} from simctl log show (${e.recoveredLineCount} app log lines scanned).`)):e.recoveredLineCount>0&&S.push(`Recovered ${e.recoveredLineCount} recent iOS simulator app log lines from simctl log show, but none looked like HTTP traffic. This app may not emit request URLs, status, or timing into Unified Logging for this repro window.`))}return void 0===f?S.push("Capture uses the session app log file. For fresh traffic, run logs clear --restart before reproducing requests."):"active"!==f&&0===S.length&&("ios"===u.platform&&"simulator"===u.kind?S.push("Session app log stream is inactive. The iOS simulator recovery path scanned recent simctl log history, but a fresh logs clear --restart window is still the most reliable repro loop."):S.push("Session app log stream is inactive. Run logs clear --restart, reproduce the request window again, then rerun network dump.")),0===I.entries.length&&S.push("ios"===(n=u).platform&&"simulator"===n.kind?"No HTTP(s) entries were found in recent iOS simulator app logs. If the app only emits non-HTTP diagnostics, inspect logs path or add app-side URLSession/network logging for per-request timing and payload details.":"ios"===n.platform?"No HTTP(s) entries were found in recent iOS device app logs. iOS network dump only sees what the app emits into Unified Logging for this process.":"No HTTP(s) entries were found in recent session app logs."),{backend:y,dump:I,notes:S}}async function rf(e){let{device:t,appBundleId:r,appLogPath:i,appLogState:o}=e;if("android"!==t.platform||!r)return null;if(void 0!==o&&"active"!==o)return{reason:"inactive"};if("active"!==o)return null;let s=function(e){let t=function(e){if(!e||!a.existsSync(e))return null;try{return tV(a.readFileSync(e,"utf8"))}catch{return null}}(e)?.command;if(!t)return null;let r=/(?:^|\s)--pid\s+(\d+)(?:\s|$)/.exec(t);return r?.[1]??null}(n.join(n.dirname(i),tG));if(!s)return null;let l=await tJ(t.id,r);return l&&l!==s?{reason:"stale-active",trackedPid:s}:null}async function rp(e,t,r,i){ru(r);let n=a.createWriteStream(r,{flags:"a"}),o=function(){let e=process.env.AGENT_DEVICE_APP_LOG_REDACT_PATTERNS;if(!e)return[];let t=e.split(",").map(e=>e.trim()).filter(e=>e.length>0),r=[];for(let e of t)try{r.push(RegExp(e,"gi"))}catch{}return r}();if("ios"===e.platform)return"device"===e.kind?await t2(e.id,n,o,i):await t0(e.id,t,n,o,e.simulatorSetPath,i);if("android"===e.platform)return tW(t),await tX(e.id,t,n,o,i);if("macos"===e.platform)return await t1(t,n,o,i);throw n.end(),new tv("UNSUPPORTED_PLATFORM",`unsupported platform: ${e.platform}`)}async function rm(e){let t=await tQ({deviceId:e.deviceId,appBundleId:e.appBundleId,startedAt:e.startedAt,simulatorSetPath:e.simulatorSetPath});return t?{dump:t6(t.text,{path:`${e.appLogPath} (simctl log show recovery)`,backend:"ios-simulator",maxEntries:e.maxEntries,include:e.include,maxPayloadChars:e.maxPayloadChars,maxScanLines:e.maxScanLines}),recoveredLineCount:t.recoveredLineCount}:null}async function rh(e){await e.stop(),await tH(e.wait)}async function rg(e,t){let r={},a=[];if(t||a.push("No app bundle is tracked in this session. Run open <app> first for app-scoped logs."),"android"===e.platform){try{let e=await th("adb",["version"],{allowFailure:!0});r.adbAvailable=0===e.exitCode}catch{r.adbAvailable=!1}if(t)try{r.androidPidVisible=(await th("adb",["-s",e.id,"shell","pidof",t],{allowFailure:!0})).stdout.trim().length>0}catch{r.androidPidVisible=!1}}if("ios"===e.platform&&"simulator"===e.kind)try{let e=await th("xcrun",["simctl","help"],{allowFailure:!0});r.simctlAvailable=0===e.exitCode}catch{r.simctlAvailable=!1}if("ios"===e.platform&&"device"===e.kind)try{let e=await th("xcrun",["devicectl","--version"],{allowFailure:!0});r.devicectlAvailable=0===e.exitCode}catch{r.devicectlAvailable=!1}if("macos"===e.platform)try{let e=await th("log",["help"],{allowFailure:!0});r.logAvailable=0===e.exitCode}catch{r.logAvailable=!1}return{checks:r,notes:a}}function rw(e){let t=n.dirname(e),r=n.basename(e);a.existsSync(t)||a.mkdirSync(t,{recursive:!0}),a.existsSync(e)?a.truncateSync(e,0):a.writeFileSync(e,"","utf8");let i=0;for(let e of a.readdirSync(t)){if(!e.startsWith(`${r}.`))continue;let o=e.slice(r.length+1);if(/^\d+$/.test(o))try{a.unlinkSync(n.join(t,e)),i+=1}catch{}}return{path:e,cleared:!0,removedRotatedFiles:i}}let rv=new Map;function ry(e){let t=rv.get(e);if(t&&(clearTimeout(t.timer),rv.delete(e),t.deleteAfterDownload))try{a.rmSync(t.artifactPath,{force:!0})}catch{}}let rI=new Map;function rS(e,t){let r=rI.get(e);if(!r)throw new tv("INVALID_ARGS",`Uploaded artifact not found: ${e}`);if(r.tenantId&&r.tenantId!==t)throw new tv("UNAUTHORIZED","Uploaded artifact belongs to a different tenant");return clearTimeout(r.timer),r.artifactPath}function rA(e){let t=rI.get(e);t&&(clearTimeout(t.timer),rI.delete(e),a.rmSync(t.tempDir,{recursive:!0,force:!0}))}async function rb(e){let t=await r_(e);await th("tar",["xf",e.archivePath,"-C",e.tempDir]);let r=n.join(e.tempDir,t);if(!a.existsSync(r))throw new tv("INVALID_ARGS",`Expected extracted bundle "${t}" not found in archive`);return r}async function r_(e){let t=await th("tar",["-tf",e.archivePath],{allowFailure:!0});if(0!==t.exitCode)throw new tv("INVALID_ARGS","Artifact is not a valid tar archive",{archivePath:e.archivePath,stdout:t.stdout,stderr:t.stderr,exitCode:t.exitCode});let r=t.stdout.split(/\r?\n/).map(e=>e.trim()).filter(Boolean);if(0===r.length)throw new tv("INVALID_ARGS","Uploaded app bundle archive is empty");let a=r.map(rN),i=e.expectedRootName??function(e,t){let r=new Set;for(let t of e){let[e]=t.split("/");e&&r.add(e)}let a=[...r];if("ios"===t){let e=a.filter(e=>e.toLowerCase().endsWith(".app"));if(1===e.length)return e[0];if(0===e.length)throw new tv("INVALID_ARGS","iOS app bundle archives must contain a single top-level .app directory");throw new tv("INVALID_ARGS",`iOS app bundle archives must contain exactly one top-level .app directory, found: ${e.join(", ")}`)}if(1===a.length)return a[0];throw new tv("INVALID_ARGS",`Archive must contain a single top-level bundle, found: ${a.join(", ")}`)}(a,e.platform);if(!a.some(e=>e===i||e.startsWith(`${i}/`)))throw new tv("INVALID_ARGS",`Uploaded archive must contain a top-level "${i}" bundle`);for(let e of a){var n=e,o=i;if(n!==o&&!n.startsWith(`${o}/`))throw new tv("INVALID_ARGS",`Archive entry must stay inside top-level "${o}" bundle: ${n}`)}for(let t of(await th("tar",["-tvf",e.archivePath])).stdout.split(/\r?\n/).filter(Boolean))if("l"===t[0]||"h"===t[0])throw new tv("INVALID_ARGS","Uploaded app bundle archive cannot contain symlinks or hard links");return i}function rN(e){if(e.includes("\0"))throw new tv("INVALID_ARGS",`Invalid archive entry: ${e}`);if(n.posix.isAbsolute(e))throw new tv("INVALID_ARGS",`Archive entry must be relative: ${e}`);let t=n.posix.normalize(e).replace(/^(\.\/)+/,"");if(!t||"."===t||t.startsWith("../"))throw new tv("INVALID_ARGS",`Archive entry escapes bundle root: ${e}`);return t}let rx=ez(process.env.AGENT_DEVICE_ARTIFACT_IDLE_TIMEOUT_MS,6e4,1e3);function rD(e,t){return new Promise((r,i)=>{let n,o=a.createWriteStream(t),s=t=>{"destroy"in e&&"function"==typeof e.destroy&&e.destroy(t)},l=!1,d=0,u=e=>{if(!l){if(l=!0,n&&clearTimeout(n),e){o.destroy(),a.rmSync(t,{force:!0}),i(e);return}r()}},c=()=>{n&&clearTimeout(n),n=setTimeout(()=>{let e=new tv("COMMAND_FAILED","Artifact transfer timed out due to inactivity",{timeoutMs:rx});s(e),o.destroy(e),u(e)},rx)};e.on("data",e=>{c();let t=Buffer.isBuffer(e)?e.length:Buffer.byteLength(e);if((d+=t)>0x80000000){let e=new tv("INVALID_ARGS","Upload exceeds maximum size of 2147483648 bytes");s(e),o.destroy(e),u(e)}}),e.on("error",u),e.on("aborted",()=>{u(new tv("COMMAND_FAILED","Artifact transfer was interrupted"))}),o.on("error",u),o.on("finish",()=>u()),c(),e.pipe(o)})}async function rM(e){let t,r=e.headers["x-artifact-type"],i=e.headers["x-artifact-filename"];if(!r||!i)throw new tv("INVALID_ARGS","Missing required headers: x-artifact-type and x-artifact-filename");if("file"!==r&&"app-bundle"!==r)throw new tv("INVALID_ARGS",`Invalid x-artifact-type: ${r}. Must be "file" or "app-bundle".`);!function(e){if(void 0===e)return;let t=Number(e);if(Number.isFinite(t)&&t>0x80000000)throw new tv("INVALID_ARGS","Upload exceeds maximum size of 2147483648 bytes")}(e.headers["content-length"]);let o=function(e){let t=e.trim(),r=n.basename(t);if(!r||"."===r||".."===r)throw new tv("INVALID_ARGS",`Invalid artifact filename: ${e}`);return r}(i),s=(t=function(e){let t=e?.trim();if(!t)return"request";let r=t.replace(/[^a-zA-Z0-9._-]+/g,"-").replace(/^-+|-+$/g,"");return r.length>0?r.slice(0,48):"request"}("upload"),a.mkdtempSync(n.join(u.tmpdir(),`agent-device-artifact-${t}-`)));try{if("file"===r){let t=n.join(s,o);return await rD(e,t),{artifactPath:t,tempDir:s}}let t=n.join(s,"artifact.tar");await rD(e,t);let i=await rb({archivePath:t,tempDir:s,platform:"ios",expectedRootName:o});return a.rmSync(t,{force:!0}),{artifactPath:i,tempDir:s}}catch(e){throw a.rmSync(s,{recursive:!0,force:!0}),e}}let rk=new Set(["agent_device.command","agent-device.command"]),rP=new Set(["agent_device.install_from_source","agent-device.install_from_source"]),rL=new Set(["agent_device.release_materialized_paths","agent-device.release_materialized_paths"]),rR={"agent_device.lease.allocate":"lease_allocate","agent-device.lease.allocate":"lease_allocate","agent_device.lease.heartbeat":"lease_heartbeat","agent-device.lease.heartbeat":"lease_heartbeat","agent_device.lease.release":"lease_release","agent-device.lease.release":"lease_release"},rO=new Set([...rk,...rP,...rL,...Object.keys(rR)]);function rC(e,t,r,a){return{jsonrpc:"2.0",id:e,error:{code:t,message:r,data:a}}}function rE(e,t,r=200){e.statusCode=r,e.setHeader("content-type","application/json"),e.end(JSON.stringify(t))}function r$(e){switch(e){case"INVALID_ARGS":return 400;case"UNAUTHORIZED":return 401;case"SESSION_NOT_FOUND":return 404;default:return 500}}function rT(e,t){let r="string"==typeof t.authorization?t.authorization:"",a=r.toLowerCase().startsWith("bearer ")?r.slice(7):void 0,i="string"==typeof t["x-agent-device-token"]?t["x-agent-device-token"]:void 0;return("string"==typeof e.token?e.token:void 0)??i??a??""}function rF(e,t){let r=e[t];return"string"==typeof r?r:void 0}function rU(e,t){let r=e[t];return Number.isInteger(r)?Number(r):void 0}async function rG(e,t){if(!e)return{ok:!0};let r=await e(t);if(void 0===r||!0===r)return{ok:!0};if(!1===r){let e=tw(new tv("UNAUTHORIZED","Request rejected by auth hook"));return{ok:!1,statusCode:401,response:rC(t.rpcRequest.id??null,-32001,e.message,e)}}if(!1===r.ok){let e=tw(new tv(r.code??"UNAUTHORIZED",r.message??"Request rejected by auth hook",r.details));return{ok:!1,statusCode:401,response:rC(t.rpcRequest.id??null,-32001,e.message,e)}}if("string"==typeof r.tenantId&&r.tenantId.length>0){let e=eB(r.tenantId);if(!e){let e=tw(new tv("INVALID_ARGS","Auth hook returned invalid tenantId"));return{ok:!1,statusCode:500,response:rC(t.rpcRequest.id??null,-32e3,e.message,e)}}return{ok:!0,tenantId:e}}return{ok:!0}}async function rV(){let e,t=process.env.AGENT_DEVICE_HTTP_AUTH_HOOK;if(!t)return null;let r=process.env.AGENT_DEVICE_HTTP_AUTH_EXPORT||"default",a=n.isAbsolute(t)?t:n.resolve(t);try{e=await import(d(a).href)}catch(e){throw new tv("COMMAND_FAILED","Failed to load AGENT_DEVICE_HTTP_AUTH_HOOK module",{hookPath:a,error:e instanceof Error?e.message:String(e)})}let i=e[r];if("function"!=typeof i)throw new tv("INVALID_ARGS",`Auth hook export ${r} is not a function`,{hookPath:a,exportName:r});return i}async function rq(e){let t=await rV(),{handleRequest:r,token:a}=e;return s.createServer((e,i)=>{if("GET"===e.method&&"/health"===e.url){i.statusCode=200,i.setHeader("content-type","application/json"),i.end(JSON.stringify({ok:!0}));return}if("POST"===e.method&&"/upload"===e.url)return void rj(e,i,t,a);if("GET"===e.method&&e.url?.startsWith("/upload/"))return void rH(e,i,t,a);if("POST"!==e.method||"/rpc"!==e.url){i.statusCode=404,i.end("Not found");return}let n="";e.setEncoding("utf8"),e.on("data",t=>{(n+=t).length>1048576&&e.destroy(Error("request too large"))}),e.on("error",()=>{i.headersSent||rE(i,rC(null,-32700,"Parse error"),400)}),e.on("end",async()=>{let a,o;try{a=JSON.parse(n)}catch{rE(i,rC(null,-32700,"Parse error"),400);return}if("2.0"!==a.jsonrpc||"string"!=typeof a.method)return void rE(i,rC(a.id??null,-32600,"Invalid Request"),400);if(!rO.has(a.method))return void rE(i,rC(a.id??null,-32601,`Method not found: ${a.method}`),404);if(!a.params||"object"!=typeof a.params)return void rE(i,rC(a.id??null,-32602,"Invalid params"),400);try{var s;let n=a.params,l=function(e,t,r){if(rk.has(e))return{token:rT(t,r),session:t.session??"default",command:t.command??"",positionals:Array.isArray(t.positionals)?t.positionals:[],flags:t.flags,runtime:t.runtime,meta:t.meta};if(rP.has(e)){let e,a=rF(t,"platform");if("ios"!==a&&"android"!==a)throw new tv("INVALID_ARGS",'Invalid params: platform must be "ios" or "android"');return{token:rT(t,r),session:rF(t,"session")??"default",command:"install_source",positionals:[],flags:{platform:a},meta:{requestId:rF(t,"requestId"),installSource:function(e){let t=e.source;if(!t||"object"!=typeof t)throw new tv("INVALID_ARGS","Invalid params: source is required");if("url"===t.kind){let e="string"==typeof t.url?t.url.trim():"";if(!e)throw new tv("INVALID_ARGS","Invalid params: source.url is required for url sources");let r=t.headers,a={};if(void 0!==r){if(!r||"object"!=typeof r||Array.isArray(r))throw new tv("INVALID_ARGS","Invalid params: source.headers must be a string map");for(let[e,t]of Object.entries(r)){if("string"!=typeof t)throw new tv("INVALID_ARGS","Invalid params: source.headers values must be strings");a[e]=t}}return Object.keys(a).length>0?{kind:"url",url:e,headers:a}:{kind:"url",url:e}}if("path"===t.kind){let e="string"==typeof t.path?t.path.trim():"";if(!e)throw new tv("INVALID_ARGS","Invalid params: source.path is required for path sources");return{kind:"path",path:e}}throw new tv("INVALID_ARGS",'Invalid params: source.kind must be "url" or "path"')}(t),retainMaterializedPaths:(e=t.retainPaths,"boolean"==typeof e?e:void 0),materializedPathRetentionMs:rU(t,"retentionMs")}}}if(rL.has(e)){let e=rF(t,"materializationId")?.trim();if(!e)throw new tv("INVALID_ARGS","Invalid params: materializationId is required");return{token:rT(t,r),session:rF(t,"session")??"default",command:"release_materialized_paths",positionals:[],meta:{requestId:rF(t,"requestId"),materializationId:e}}}let a=rR[e];if(a)return{token:rT(t,r),session:rF(t,"session")??"default",command:a,positionals:[],meta:{tenantId:rF(t,"tenantId")??rF(t,"tenant"),runId:rF(t,"runId"),leaseId:rF(t,"leaseId"),leaseTtlMs:rU(t,"ttlMs"),leaseBackend:rF(t,"backend")}};throw new tv("INVALID_ARGS",`Method not found: ${e}`)}(a.method,n,e.headers);if(s=a.method,rk.has(s)&&("string"!=typeof l.command||0===l.command.length))return void rE(i,rC(a.id??null,-32602,"Invalid params: command is required"),400);o=U(l.meta?.requestId,a.id),l.meta={...l.meta,requestId:o},b(o);let d=()=>{i.writableFinished||e2(o)};e.on("aborted",d),i.on("close",d);let u=await rG(t,{headers:e.headers,rpcRequest:a,daemonRequest:l});if(!u.ok)return void rE(i,u.response,u.statusCode);u.tenantId&&(l.meta={...l.meta,tenantId:u.tenantId,sessionIsolation:l.meta?.sessionIsolation??l.flags?.sessionIsolation??"tenant"});let c=await r(l);if(c.ok)return void rE(i,{jsonrpc:"2.0",id:a.id??null,result:c});rE(i,rC(a.id??null,-32e3,c.error.message,c.error),r$(c.error.code))}catch(t){let e=tw(t);rE(i,rC(a.id??null,-32e3,e.message,e),r$(e.code))}finally{B(o)}})})}async function rj(e,t,a,i){try{var n;let o,s,l=rT({},e.headers),d=rB(l,i);if(d){t.statusCode=r$(d.code),t.setHeader("content-type","application/json"),t.end(JSON.stringify({ok:!1,error:d.message,code:d.code}));return}let u=await rG(a,{headers:e.headers,rpcRequest:{jsonrpc:"2.0",id:null,method:"agent_device.command"},daemonRequest:{token:l,session:"default",command:"upload",positionals:[]}});if(!u.ok){t.statusCode=u.statusCode,t.setHeader("content-type","application/json"),t.end(JSON.stringify({ok:!1,error:u.response.error?.data?.message??u.response.error?.message??"Unauthorized"}));return}let c=await rM(e),f=(n={artifactPath:c.artifactPath,tempDir:c.tempDir,tenantId:u.tenantId},o=r.randomUUID(),(s=setTimeout(()=>{rA(o)},3e5)).unref(),rI.set(o,{artifactPath:n.artifactPath,tempDir:n.tempDir,tenantId:n.tenantId,timer:s}),o);t.statusCode=200,t.setHeader("content-type","application/json"),t.end(JSON.stringify({ok:!0,uploadId:f}))}catch(r){let e=tw(r);t.statusCode=r$(e.code),t.setHeader("content-type","application/json"),t.end(JSON.stringify({ok:!1,error:e.message,code:e.code}))}}async function rH(e,t,r,i){let n=e.url?.slice("/upload/".length)??"";if(!n){t.statusCode=400,t.end("Missing artifact id");return}try{let o=rT({},e.headers),s=rB(o,i);if(s){t.statusCode=r$(s.code),t.setHeader("content-type","application/json"),t.end(JSON.stringify({ok:!1,error:s.message,code:s.code}));return}let l=await rG(r,{headers:e.headers,rpcRequest:{jsonrpc:"2.0",id:null,method:"agent_device.command"},daemonRequest:{token:o,session:"default",command:"download_artifact",positionals:[n]}});if(!l.ok){t.statusCode=l.statusCode,t.setHeader("content-type","application/json"),t.end(JSON.stringify({ok:!1,error:l.response.error?.data?.message??l.response.error?.message??"Unauthorized"}));return}let d=function(e,t){let r=rv.get(e);if(!r)throw new tv("INVALID_ARGS",`Artifact not found: ${e}`);if(r.tenantId&&r.tenantId!==t)throw new tv("UNAUTHORIZED","Artifact belongs to a different tenant");if(!a.existsSync(r.artifactPath))throw ry(e),new tv("COMMAND_FAILED",`Artifact file is missing: ${r.artifactPath}`);return{artifactPath:r.artifactPath,fileName:r.fileName,deleteAfterDownload:r.deleteAfterDownload}}(n,l.tenantId),u=a.createReadStream(d.artifactPath);t.statusCode=200,t.setHeader("content-type","application/octet-stream"),d.fileName&&t.setHeader("content-disposition",`attachment; filename="${d.fileName.replace(/"/g,"")}"`),u.on("error",e=>{if(t.headersSent)t.destroy(e);else{let r=tw(e);t.statusCode=r$(r.code),t.end(r.message)}}),t.on("close",()=>{t.writableFinished&&ry(n)}),u.pipe(t)}catch(r){let e=tw(r);t.statusCode=r$(e.code),t.setHeader("content-type","application/json"),t.end(JSON.stringify({ok:!1,error:e.message,code:e.code}))}}function rB(e,t){return t&&e!==t?tw(new tv("UNAUTHORIZED","Invalid token")):null}function rK(e){if(!e)return;let t=e.trim();if(t&&/^[a-zA-Z0-9._-]{1,128}$/.test(t))return t}function rz(e){if(!e)return;let t=e.trim();if(t&&/^[a-f0-9]{16,128}$/i.test(t))return t.toLowerCase()}function rW(e){let t=(e??"").trim().toLowerCase();if(!t||"ios-simulator"===t)return"ios-simulator";throw new tv("INVALID_ARGS",`Unsupported lease backend: ${e??""}`)}class rJ{leases=new Map;runBindings=new Map;maxActiveSimulatorLeases;defaultLeaseTtlMs;minLeaseTtlMs;maxLeaseTtlMs;now;constructor(e={}){this.maxActiveSimulatorLeases=Number.isInteger(e.maxActiveSimulatorLeases)?Math.max(0,Number(e.maxActiveSimulatorLeases)):0,this.defaultLeaseTtlMs=Number.isInteger(e.defaultLeaseTtlMs)?Math.max(1,Number(e.defaultLeaseTtlMs)):6e4,this.minLeaseTtlMs=Number.isInteger(e.minLeaseTtlMs)?Math.max(1,Number(e.minLeaseTtlMs)):5e3,this.maxLeaseTtlMs=Number.isInteger(e.maxLeaseTtlMs)?Math.max(this.minLeaseTtlMs,Number(e.maxLeaseTtlMs)):6e5,this.now=e.now??(()=>Date.now())}allocateLease(e){let t=rW(e.backend),a=eB(e.tenantId);if(!a)throw new tv("INVALID_ARGS","Invalid tenant id. Use 1-128 chars: letters, numbers, dot, underscore, hyphen.");let i=rK(e.runId);if(!i)throw new tv("INVALID_ARGS","Invalid run id. Use 1-128 chars: letters, numbers, dot, underscore, hyphen.");this.cleanupExpiredLeases();let n=this.resolveLeaseTtlMs(e.ttlMs),o=this.bindingKey(a,i,t),s=this.runBindings.get(o);if(s){let e=this.leases.get(s);if(e)return this.refreshLease(e,n);this.runBindings.delete(o)}this.enforceCapacity(t);let l=this.now(),d={leaseId:r.randomBytes(16).toString("hex"),tenantId:a,runId:i,backend:t,createdAt:l,heartbeatAt:l,expiresAt:l+n};return this.leases.set(d.leaseId,d),this.runBindings.set(o,d.leaseId),{...d}}heartbeatLease(e){let t=rz(e.leaseId);if(!t)throw new tv("INVALID_ARGS","Invalid lease id.");this.cleanupExpiredLeases();let r=this.leases.get(t);if(!r)throw new tv("UNAUTHORIZED","Lease is not active",{reason:"LEASE_NOT_FOUND"});this.assertOptionalScopeMatch(r,e.tenantId,e.runId);let a=this.resolveLeaseTtlMs(e.ttlMs);return this.refreshLease(r,a)}releaseLease(e){let t=rz(e.leaseId);if(!t)throw new tv("INVALID_ARGS","Invalid lease id.");this.cleanupExpiredLeases();let r=this.leases.get(t);return r?(this.assertOptionalScopeMatch(r,e.tenantId,e.runId),this.leases.delete(t),this.runBindings.delete(this.bindingKey(r.tenantId,r.runId,r.backend)),{released:!0}):{released:!1}}assertLeaseAdmission(e){let t=rW(e.backend),r=eB(e.tenantId);if(!r)throw new tv("INVALID_ARGS","tenant isolation requires tenant id.");let a=rK(e.runId);if(!a)throw new tv("INVALID_ARGS","tenant isolation requires run id.");let i=rz(e.leaseId);if(!i)throw new tv("INVALID_ARGS","tenant isolation requires lease id.");this.cleanupExpiredLeases();let n=this.leases.get(i);if(!n)throw new tv("UNAUTHORIZED","Lease is not active",{reason:"LEASE_NOT_FOUND"});if(n.backend!==t||n.tenantId!==r||n.runId!==a)throw new tv("UNAUTHORIZED","Lease does not match tenant/run scope",{reason:"LEASE_SCOPE_MISMATCH"})}listActiveLeases(){return this.cleanupExpiredLeases(),Array.from(this.leases.values()).map(e=>({...e}))}cleanupExpiredLeases(){let e=this.now();for(let t of this.leases.values())t.expiresAt>e||(this.leases.delete(t.leaseId),this.runBindings.delete(this.bindingKey(t.tenantId,t.runId,t.backend)))}enforceCapacity(e){if("ios-simulator"!==e||this.maxActiveSimulatorLeases<=0)return;let t=Array.from(this.leases.values()).filter(e=>"ios-simulator"===e.backend).length;if(!(t<this.maxActiveSimulatorLeases))throw new tv("COMMAND_FAILED","No simulator lease capacity available",{reason:"LEASE_CAPACITY_EXCEEDED",activeLeases:t,maxActiveLeases:this.maxActiveSimulatorLeases,backend:e,hint:"Retry after releasing another simulator lease."})}resolveLeaseTtlMs(e){if(!Number.isInteger(e))return this.defaultLeaseTtlMs;let t=Number(e);if(t<this.minLeaseTtlMs||t>this.maxLeaseTtlMs)throw new tv("INVALID_ARGS",`Lease ttlMs must be between ${this.minLeaseTtlMs} and ${this.maxLeaseTtlMs}.`);return t}refreshLease(e,t){let r=this.now(),a={...e,heartbeatAt:r,expiresAt:r+t};return this.leases.set(a.leaseId,a),this.runBindings.set(this.bindingKey(a.tenantId,a.runId,a.backend),a.leaseId),{...a}}bindingKey(e,t,r){return`${e}:${t}:${r}`}assertOptionalScopeMatch(e,t,r){let a=eB(t),i=rK(r);if(t&&!a)throw new tv("INVALID_ARGS","Invalid tenant id. Use 1-128 chars: letters, numbers, dot, underscore, hyphen.");if(r&&!i)throw new tv("INVALID_ARGS","Invalid run id. Use 1-128 chars: letters, numbers, dot, underscore, hyphen.");if(a&&e.tenantId!==a||i&&e.runId!==i)throw new tv("UNAUTHORIZED","Lease does not match tenant/run scope",{reason:"LEASE_SCOPE_MISMATCH"})}}let rZ=ez(process.env.AGENT_DEVICE_INSTALL_SOURCE_RETAIN_TTL_MS,9e5,5e3),rX=new Map;async function rY(e){let t=await c.mkdtemp(n.join(u.tmpdir(),"agent-device-materialized-"));try{let a=await r1(e.installablePath,n.join(t,"installable")),i=e.archivePath?await r1(e.archivePath,n.join(t,"archive")):void 0,o=r.randomUUID(),s=e.ttlMs??rZ,l=Date.now()+s,d=setTimeout(()=>{rQ(o)},s);return rX.set(o,{rootPath:t,installablePath:a,archivePath:i,tenantId:e.tenantId,sessionName:e.sessionName,expiresAt:l,timer:d}),{materializationId:o,installablePath:a,...i?{archivePath:i}:{},expiresAt:new Date(l).toISOString()}}catch(e){throw await c.rm(t,{recursive:!0,force:!0}),e}}async function rQ(e,t){let r=rX.get(e);if(!r)throw new tv("INVALID_ARGS",`Materialized paths not found: ${e}`);if(r.tenantId&&r.tenantId!==t)throw new tv("UNAUTHORIZED","Materialized paths belong to a different tenant");clearTimeout(r.timer),rX.delete(e),await c.rm(r.rootPath,{recursive:!0,force:!0})}async function r0(e){let t=Array.from(rX.entries()).filter(([,t])=>t.sessionName===e).map(([e])=>e);await Promise.all(t.map(async e=>{await rQ(e)}))}async function r1(e,t){let r=await c.stat(e);await c.mkdir(t,{recursive:!0});let a=n.join(t,n.basename(e));return r.isDirectory()?await c.cp(e,a,{recursive:!0}):await c.copyFile(e,a),a}async function r2(e){var t;let r="ios"===(t=e.flags?.platform)||"android"===t?t:void 0;if(e.session){if(r&&e.session.device.platform!==r)throw new tv("INVALID_ARGS",`install_from_source requested platform ${r}, but session is bound to ${e.session.device.platform}`);return await e8(e.session.device),e.session.device}if(!r)throw new tv("INVALID_ARGS",'install_from_source requires platform "ios" or "android" when no session is provided');let a=await eL(e.flags??{});return await e8(a),a}async function r5(e){let{req:t,sessionName:r,sessionStore:a}=e,i=a.get(r);try{let e,n,o,s=(n=function(e){let t=e.meta?.installSource;if(!t)throw new tv("INVALID_ARGS","install_from_source requires a source payload");if("url"===t.kind){if(!t.url||0===t.url.trim().length)throw new tv("INVALID_ARGS","install_from_source url source requires a non-empty url");return t}if(!t.path||0===t.path.trim().length)throw new tv("INVALID_ARGS","install_from_source path source requires a non-empty path");return t}(t),(o=t.meta?.uploadedArtifactId)&&"path"===n.kind?{source:{kind:"path",path:rS(o,t.meta?.tenantId)},cleanup:()=>{rA(o)}}:{source:n,cleanup:()=>{}}),l=function(e){let t=e.meta?.retainMaterializedPaths===!0,r=e.meta?.materializedPathRetentionMs;if(!t)return{enabled:!1};if(void 0!==r&&r<=0)throw new tv("INVALID_ARGS","install_from_source retentionMs must be a positive integer");return{enabled:!0,ttlMs:r}}(t),d=await r2({session:i,flags:t.flags});if(!to("install",d))return N("UNSUPPORTED_OPERATION","install_from_source is not supported on this device");let u=tp(t.meta?.requestId);if("ios"===d.platform){let e,{installIosInstallablePath:n}=await import("./155.js"),{prepareIosInstallArtifact:o}=await import("./155.js"),c=await o(s.source,{signal:u});try{if(l.enabled&&(e=await rY({archivePath:c.archivePath,installablePath:c.installablePath,tenantId:t.meta?.tenantId,sessionName:i?r:void 0,ttlMs:l.ttlMs})),await n(d,c.installablePath),!c.bundleId)throw new tv("COMMAND_FAILED","Installed iOS app identity could not be resolved from the artifact");let o={...e?.archivePath?{archivePath:e.archivePath}:{},...e?{installablePath:e.installablePath}:{},bundleId:c.bundleId,...c.appName?{appName:c.appName}:{},launchTarget:c.bundleId,...e?{materializationId:e.materializationId,materializationExpiresAt:e.expiresAt}:{}},s=ew(o,r3(o));return i&&a.recordAction(i,{command:"install_source",positionals:[],flags:t.flags??{},result:s}),{ok:!0,data:s}}catch(r){throw e&&await rQ(e.materializationId,t.meta?.tenantId).catch(()=>{}),r}finally{await c.cleanup(),s.cleanup()}}let{prepareAndroidInstallArtifact:c}=await import("./155.js"),{installAndroidInstallablePathAndResolvePackageName:f}=await import("./155.js"),p=await c(s.source,{signal:u});try{l.enabled&&(e=await rY({archivePath:p.archivePath,installablePath:p.installablePath,tenantId:t.meta?.tenantId,sessionName:i?r:void 0,ttlMs:l.ttlMs}));let n=await f(d,p.installablePath,p.packageName);if(!n)throw new tv("COMMAND_FAILED","Installed Android app identity could not be resolved from the artifact or device state");let{inferAndroidAppName:o}=await import("./155.js"),s=o(n),u={...e?.archivePath?{archivePath:e.archivePath}:{},...e?{installablePath:e.installablePath}:{},packageName:n,...s?{appName:s}:{},launchTarget:n,...e?{materializationId:e.materializationId,materializationExpiresAt:e.expiresAt}:{}},c=ew(u,r3(u));return i&&a.recordAction(i,{command:"install_source",positionals:[],flags:t.flags??{},result:c}),{ok:!0,data:c}}catch(r){throw e&&await rQ(e.materializationId,t.meta?.tenantId).catch(()=>{}),r}finally{await p.cleanup(),s.cleanup()}}catch(e){return{ok:!1,error:tw(e)}}}function r3(e){return`Installed: ${Q(e)}`}async function r4(e){let{req:t}=e;try{let e=t.meta?.materializationId?.trim();if(!e)throw new tv("INVALID_ARGS","release_materialized_paths requires a materializationId");return await rQ(e,t.meta?.tenantId),{ok:!0,data:{released:!0,materializationId:e}}}catch(e){return{ok:!1,error:tw(e)}}}let r8=ez(process.env.AGENT_DEVICE_IOS_SIMULATOR_POST_CLOSE_SETTLE_MS,300,0),r6=ez(process.env.AGENT_DEVICE_IOS_SIMULATOR_POST_OPEN_SETTLE_MS,300,0);function r9(e,t,r){return t||r7(r)?null:N("INVALID_ARGS",`${e} requires an active session or an explicit device selector (e.g. --platform ios).`)}function r7(e){return!!(e?.platform||e?.target||e?.device||e?.udid||e?.serial)}function ae(e){return"ios"===e.platform&&"simulator"===e.kind}async function at(e,t){ae(e)&&!(t<=0)&&await new Promise(e=>setTimeout(e,t))}async function ar(e){let t=r7(e.flags)||!e.session?await eL(e.flags??{}):await aa(e.session.device);return!1!==e.ensureReady&&await e8(t),t}async function aa(e){if("ios"!==e.platform||"simulator"!==e.kind||"darwin"!==process.platform)return e;let t={platform:"ios",target:e.target,udid:e.id,...e.simulatorSetPath?{iosSimulatorDeviceSet:e.simulatorSetPath}:{}};try{return await eL(t)}catch(e){if(!(e instanceof tv)||"DEVICE_NOT_FOUND"!==e.code)throw e}return await eL({platform:"ios",target:e.target,device:e.name,...e.simulatorSetPath?{iosSimulatorDeviceSet:e.simulatorSetPath}:{}})}function ai(e){let t=e.flags?.device?.trim();return t||(e.resolvedDevice?.platform==="android"&&"emulator"===e.resolvedDevice.kind?e.resolvedDevice.name:e.sessionDevice?.platform==="android"&&"emulator"===e.sessionDevice.kind?e.sessionDevice.name:void 0)}let an=["platform","metroHost","metroPort","bundleUrl","launchUrl"];function ao(e){return e?[e.metroHost,e.metroPort,e.bundleUrl,e.launchUrl].filter(e=>void 0!==e&&""!==e).length:0}function as(e){let t=e?.trim();return t&&t.length>0?t:void 0}function al(e,t){if(void 0!==e){if("string"!=typeof e)throw new tv("INVALID_ARGS",`Invalid open runtime ${t}: expected string.`);return as(e)}}function ad(e){if(void 0!==e){if(!Number.isInteger(e)||e<1||e>65535)throw new tv("INVALID_ARGS",`Invalid runtime metroPort: ${String(e)}. Use an integer between 1 and 65535.`);return e}}function au(e){if("ios"===e||"android"===e)return e}async function ac(e){let{replacedStoredRuntime:t,previousRuntime:r,runtime:a,session:i}=e;!t||!i?.appBundleId||!ep(r)||ep(a)||await eU({device:i.device,appId:i.appBundleId})}async function af(e){var t,r;let{req:a,sessionName:i,sessionStore:n}=e,o=(a.positionals?.[0]??"show").toLowerCase(),s=n.get(i),l=n.getRuntimeHints(i);if(!["set","show","clear"].includes(o))return N("INVALID_ARGS","runtime requires set, show, or clear");if("clear"===o){ep(l)&&s?.appBundleId&&await eU({device:s.device,appId:s.appBundleId});let e=n.clearRuntimeHints(i);return{ok:!0,data:{session:i,cleared:e}}}if("show"===o)return{ok:!0,data:{session:i,configured:!!l,runtime:l}};let d=au(tu(a.flags?.platform)??l?.platform??s?.device.platform);if(!d)return N("INVALID_ARGS","runtime set only supports iOS and Android sessions. Pass --platform ios|android or open an iOS/Android session first.");if(s&&s.device.platform!==d)return N("INVALID_ARGS",`runtime set targets ${d}, but session "${i}" is already bound to ${s.device.platform}.`);let u={platform:(t=a.flags,r={platform:d,metroHost:as(t?.metroHost),metroPort:ad(t?.metroPort),bundleUrl:as(t?.bundleUrl),launchUrl:as(t?.launchUrl)}).platform??l?.platform,metroHost:r.metroHost??l?.metroHost,metroPort:r.metroPort??l?.metroPort,bundleUrl:r.bundleUrl??l?.bundleUrl,launchUrl:r.launchUrl??l?.launchUrl};return 0===ao(u)?N("INVALID_ARGS","runtime set requires at least one hint such as --metro-host, --metro-port, --bundle-url, or --launch-url."):(n.setRuntimeHints(i,u),{ok:!0,data:{session:i,configured:!0,runtime:u}})}let ap="open-command-roundtrip",am="Not implemented for this platform in this release.",ah=new Set(["app","desktop","frontmost-app"]);async function ag(e){if("app"===e||"desktop"===e||"menubar"===e)return{};let t=await ta();return{appBundleId:t.bundleId,appName:t.appName}}async function aw(e,t,r){if(("ios"===e.platform||"macos"===e.platform)&&t)return eF(t)?"macos"===e.platform?void 0:"device"===e.kind?e5(r,t):void 0:await av(e,t)}async function av(e,t){try{let{resolveIosApp:r}=await import("./155.js");return await r(e,t)}catch{return}}async function ay(e,t){if(!("android"!==e.platform||!t||eF(t)))try{let{resolveAndroidApp:r}=await import("./155.js"),a=await r(e,t);return"package"===a.type?a.value:void 0}catch{return}}async function aI(e,t,r,a){return await aw(e,t,r)??await a(e,t)??("android"===e.platform&&t&&eF(t)?r:void 0)}function aS(e){return N("INVALID_ARGS",e)}function aA(e,t,r,a){try{return function(e){let{device:t,surfaceFlag:r,openTarget:a,existingSurface:i}=e;if(("macos"===t.platform||"linux"===t.platform)&&!r)return i??"app";if("linux"===t.platform){if(!r)return"app";let e=ed(r);if(!ah.has(e))throw new tv("INVALID_ARGS",`Linux supports --surface app, desktop, and frontmost-app (got "${r}")`);if("app"!==e&&a)throw new tv("INVALID_ARGS",`open --surface ${e} does not accept an app target`);return e}if("macos"!==t.platform){if(r)throw new tv("INVALID_ARGS","surface is only supported on macOS and Linux");return"app"}let n=r?ed(r):"app";if("app"!==n&&"menubar"!==n&&a)throw new tv("INVALID_ARGS",`open --surface ${n} does not accept an app target`);return n}({device:e,surfaceFlag:t,openTarget:r,existingSurface:a})}catch(e){return N(e instanceof tv?e.code:"INVALID_ARGS",String(e.message))}}function ab(e){let{shouldRelaunch:t,openTarget:r,surface:a,device:i}=e;return t?r&&eF(r)?aS("open --relaunch does not support URL targets."):"app"!==a?aS("open --relaunch is supported only for app surfaces."):"android"===i.platform&&r&&"binary"===eE(r)?aS(q(r)):null:null}async function a_(e){let{req:t,sessionName:r,sessionStore:a,device:i,surface:n,openTarget:o,existingSession:s}=e;await e8(i);let{appBundleId:l,appName:d}=await aN({device:i,surface:n,openTarget:o,existingAppBundleId:s?.appBundleId}),u=function(e){try{return{ok:!0,data:function(e){let{req:t,sessionStore:r,sessionName:a,device:i}=e,n=r.getRuntimeHints(a),o=function(e){let{runtime:t,sessionName:r,platform:a}=e;if(void 0===t)return;if(!t||"object"!=typeof t||Array.isArray(t))throw new tv("INVALID_ARGS","open runtime must be an object.");let i=Object.keys(t).find(e=>!an.includes(e));if(i)throw new tv("INVALID_ARGS",`Invalid open runtime field: ${i}. Supported fields are ${an.join(", ")}.`);return{platform:function(e,t,r){if(void 0===e)return r;if("ios"!==e&&"android"!==e)throw new tv("INVALID_ARGS",`Invalid open runtime platform: ${String(e)}. Use "ios" or "android".`);if(r&&e!==r)throw new tv("INVALID_ARGS",`open runtime targets ${e}, but session "${t}" is bound to ${r}.`);return e}(t.platform,r,a),metroHost:al(t.metroHost,"metroHost"),metroPort:function(e){if(void 0!==e){if("number"!=typeof e)throw new tv("INVALID_ARGS","Invalid open runtime metroPort: expected integer.");return ad(e)}}(t.metroPort),bundleUrl:al(t.bundleUrl,"bundleUrl"),launchUrl:al(t.launchUrl,"launchUrl")}}({runtime:t.runtime,sessionName:a,platform:au(i.platform)});return void 0===t.runtime?{runtime:function(e,t,r){let a=e.getRuntimeHints(t);if(!a)return;let i=r?.platform,n=au(i);if(a.platform&&r&&!n)throw new tv("INVALID_ARGS",`Session runtime hints are only supported on iOS and Android sessions, but session "${t}" is bound to ${i}.`);if(a.platform&&n&&a.platform!==n)throw new tv("INVALID_ARGS",`Session runtime hints target ${a.platform}, but session "${t}" is bound to ${i}. Clear the runtime hints or use a different session.`);return n&&a.platform!==n?{...a,platform:n}:a}(r,a,i),previousRuntime:n,replacedStoredRuntime:!1}:{runtime:o&&ao(o)>0?o:void 0,previousRuntime:n,replacedStoredRuntime:!0}}(e)}}catch(t){let e=tg(t);return N(e.code,e.message,e.details)}}({req:t,sessionStore:a,sessionName:r,device:i});if(!u.ok)return{type:"response",response:u};if(s){let{runtime:e,previousRuntime:t,replacedStoredRuntime:r}=u.data;await ac({replacedStoredRuntime:r,previousRuntime:t,runtime:e,session:s})}return{type:"details",details:{appBundleId:l,appName:d,runtime:u.data.runtime}}}async function aN(e){let{device:t,surface:r,openTarget:a,existingAppBundleId:i}=e,n=await ag(r);return{appBundleId:n.appBundleId??await aI(t,a,i,ay),appName:n.appName??a}}let ax=new Map;async function aD(e){let{device:t,closeTarget:r,outFlag:a,context:i}=e;"android"!==t.platform&&await ey(t.id),await w(t,"close",[r],a,i),await at(t,r8)}async function aM(e){let{runtime:t,device:r,req:a,logPath:i,appBundleId:n,traceLogPath:o,openPositionals:s}=e,l=t?.launchUrl;if(!l||0===s.length||s.length>1)return;let d=s[0]?.trim();!d||eF(d)||await w(r,"open",[l],a.flags?.out,{...eG(i,a.flags,n,o)})}async function ak(e){var t,r,a;let{req:i,sessionName:n,sessionStore:o,logPath:s,device:l,openTarget:d,openPositionals:u,appName:c,surface:f,appBundleId:p,runtime:m,existingSession:h}=e,g=i.flags?.relaunch===!0,v=h?.trace?.outPath;if(g&&d){let e=p??d;await aD({device:l,closeTarget:e,outFlag:i.flags?.out,context:{...eG(s,i.flags,p??h?.appBundleId,v)}})}await eP({device:l,appId:p,runtime:m});let y=Date.now();await w(l,"open",u,i.flags?.out,{...eG(s,i.flags,p)}),await aM({runtime:m,device:l,req:i,logPath:s,appBundleId:p,traceLogPath:v,openPositionals:u});let I=d?{durationMs:Math.max(0,Date.now()-y),measuredAt:new Date().toISOString(),method:ap,appTarget:d,appBundleId:p}:void 0;if(await at(l,r6),F(i.meta?.requestId)){let e=eS();return N(e.code,e.message,e.details)}h&&tf(h,"open",h.snapshot);let S=function(e){let{existingSession:t,sessionName:r,device:a,surface:i,appBundleId:n,appName:o,saveScript:s}=e;return t?{...t,device:a,surface:i,appBundleId:n,appName:o,recordSession:t.recordSession||s,snapshot:void 0}:{name:r,device:a,createdAt:Date.now(),surface:i,appBundleId:n,appName:o,recordSession:s,actions:[]}}({existingSession:h,sessionName:n,device:l,surface:f,appBundleId:p,appName:c,saveScript:!!i.flags?.saveScript});void 0!==i.runtime&&(t=o,r=n,(a=m)&&(0===ao(a)?t.clearRuntimeHints(r):t.setRuntimeHints(r,a)));let A=function(e){let{sessionName:t,appName:r,appBundleId:a,surface:i,startup:n,device:o,runtime:s,runtimeHintCount:l}=e,d={session:t,surface:i};return r&&(d.appName=r),a&&(d.appBundleId=a),n&&(d.startup=n),s&&l(s)>0&&(d.runtime=s),o&&(d.platform=o.platform,d.target=o.target??"mobile",d.device=o.name,d.id=o.id,d.kind=o.kind,"android"===o.platform&&(d.serial=o.id)),o?.platform==="ios"&&(d.device_udid=o.id,d.ios_simulator_device_set=o.simulatorSetPath??null),{...d,...D(`Opened: ${r??a??t}`)}}({sessionName:n,appName:c,appBundleId:p,surface:f,startup:I,device:l,runtime:m,runtimeHintCount:ao});return o.recordAction(S,{command:"open",positionals:u,flags:i.flags??{},runtime:void 0!==i.runtime?m:void 0,result:A}),o.set(n,S),{ok:!0,data:A}}async function aP(e){let{req:t,sessionName:r,logPath:a,sessionStore:i}=e;if(i.has(r)){let e=i.get(r);if(!e)return N("SESSION_NOT_FOUND",`Session "${r}" not found.`);let n=t.flags?.relaunch===!0,o=t.positionals?.[0],s=o??(n?e.appName:void 0),l=aA(e.device,t.flags?.surface,s,e.surface);if("string"!=typeof l)return l;if(!s&&"app"===l)return n?aS("open --relaunch requires an app name or an active session app."):aS("Session already active. Close it first or pass a new --session name.");let d=ab({shouldRelaunch:n,openTarget:s,surface:l,device:e.device});if(d)return d;let u=await aa(e.device),c=await a_({req:t,sessionName:r,sessionStore:i,device:u,surface:l,openTarget:s,existingSession:e});return"response"===c.type?c.response:await ak({req:t,sessionName:r,sessionStore:i,logPath:a,device:u,openTarget:s,openPositionals:o?t.positionals??[]:s?[s]:[],appBundleId:c.details.appBundleId,appName:c.details.appName,runtime:c.details.runtime,surface:l,existingSession:e})}let n=t.flags?.relaunch===!0,o=t.positionals?.[0];if(n&&!o)return aS("open --relaunch requires an app argument.");let s=function(e){let{shouldRelaunch:t,openTarget:r,platform:a}=e;return t?r&&eF(r)?aS("open --relaunch does not support URL targets."):"android"===a&&r&&"binary"===eE(r)?aS(q(r)):null:null}({shouldRelaunch:n,openTarget:o,platform:t.flags?.platform==="android"?"android":void 0});if(s)return s;let l=await eL(t.flags??{}),d=aA(l,t.flags?.surface,o);if("string"!=typeof d)return d;let u=ab({shouldRelaunch:n,openTarget:o,surface:d,device:l});return u||await k(ax,l.id,async()=>{let e=i.toArray().find(e=>e.device.id===l.id);if(e)return N("DEVICE_IN_USE",`Device is already in use by session "${e.name}".`,{session:e.name,deviceId:l.id,deviceName:l.name});let n=await a_({req:t,sessionName:r,sessionStore:i,device:l,surface:d,openTarget:o});return"response"===n.type?n.response:await ak({req:t,sessionName:r,sessionStore:i,logPath:a,device:l,openTarget:o,openPositionals:t.positionals??[],appBundleId:n.details.appBundleId,appName:n.details.appName,runtime:n.details.runtime,surface:d})})}async function aL(e){let t=await th("adb",["-s",e.id,"emu","kill"],{allowFailure:!0,timeoutMs:15e3});return{success:0===t.exitCode,exitCode:t.exitCode,stdout:String(t.stdout??""),stderr:String(t.stderr??"")}}async function aR(e){let{device:t,shutdownRequested:r}=e;if(r&&(ae(t)||"android"===t.platform&&"emulator"===t.kind))try{return ae(t)?await ev(t):await aL(t)}catch(t){let e=tw(t);return{success:!1,exitCode:-1,stdout:"",stderr:e.message,error:e}}}async function aO(e){if(await ey(e.device.id),"macos"!==e.device.platform)return;let t="frontmost-app"===e.surface?{surface:"frontmost-app"}:e.appBundleId?{bundleId:e.appBundleId}:{};await eT("dismiss",t).catch(t=>{tr({level:"debug",phase:"macos_close_alert_dismiss_failed",data:{session:e.name,error:t instanceof Error?t.message:String(t)}})})}async function aC(e){let{req:t,sessionName:r,logPath:a,sessionStore:i}=e,n=i.get(r);if(!n)return N("SESSION_NOT_FOUND","No active session");n.appLog&&await rh(n.appLog),t.positionals&&t.positionals.length>0&&(("ios"===n.device.platform||"macos"===n.device.platform)&&await aO(n),await w(n.device,"close",t.positionals,t.flags?.out,{...eG(a,t.flags,n.appBundleId,n.trace?.outPath)}),await at(n.device,r8)),("ios"===n.device.platform||"macos"===n.device.platform)&&await aO(n),ep(i.getRuntimeHints(r))&&n.appBundleId&&await eU({device:n.device,appId:n.appBundleId}).catch(()=>{}),i.recordAction(n,{command:"close",positionals:t.positionals??[],flags:t.flags??{},result:{session:r,...D(`Closed: ${r}`)}}),t.flags?.saveScript&&(n.recordSession=!0),i.writeSessionLog(n),await r0(r).catch(()=>{}),i.delete(r);let o=await aR({device:n.device,shutdownRequested:t.flags?.shutdown});return o?{ok:!0,data:ew({session:r,shutdown:o},`Closed: ${r}`)}:{ok:!0,data:{session:r,...D(`Closed: ${r}`)}}}let aE=["platform","target","device","udid","serial","verbose","out"];function a$(e,t){let r=e??{};for(let e of aE)void 0===t[e]&&void 0!==r[e]&&(t[e]=r[e]);return t}let aT={ios:async(e,t,r)=>{let{reinstallIosApp:a}=await import("./155.js");return await a(e,t,r)},android:async(e,t,r)=>{let{reinstallAndroidApp:a}=await import("./155.js");return await a(e,t,r)}},aF={ios:async(e,t,r)=>{let{installIosApp:a}=await import("./155.js"),i=await a(e,r,{appIdentifierHint:t});return{bundleId:i.bundleId,appName:i.appName,launchTarget:i.launchTarget}},android:async(e,t,r)=>{let{installAndroidApp:a}=await import("./155.js"),i=await a(e,r);return{package:i.packageName,appName:i.appName,launchTarget:i.launchTarget}}};async function aU(e){let{req:t,command:r,sessionName:i,sessionStore:n,deployOps:o}=e,s=n.get(i),l=t.flags??{},d=r9(r,s,l);if(d)return d;let u=t.positionals?.[0]?.trim(),c=t.positionals?.[1]?.trim();if(!u||!c)return N("INVALID_ARGS",`${r} requires: ${r} <app> <path-to-app-binary>`);let f=t.meta?.uploadedArtifactId;try{var p;let e,i=f?rS(f,t.meta?.tenantId):tF.expandHome(c);if(!a.existsSync(i))return N("INVALID_ARGS",`App binary not found: ${i}`);let d=await ar({session:s,flags:l,ensureReady:!1});if(!to(r,d))return N("UNSUPPORTED_OPERATION",`${r} is not supported on this device`);if("ios"===d.platform){let t=await o.ios(d,u,i),r=t.bundleId;e=r?{app:u,appPath:i,platform:"ios",appId:r,bundleId:r,appName:t.appName,launchTarget:t.launchTarget}:{app:u,appPath:i,platform:"ios",appName:t.appName,launchTarget:t.launchTarget}}else{let t=await o.android(d,u,i),r=t.package;e=r?{app:u,appPath:i,platform:"android",appId:r,package:r,packageName:r,appName:t.appName,launchTarget:t.launchTarget}:{app:u,appPath:i,platform:"android",appName:t.appName,launchTarget:t.launchTarget}}let m=ew(e,(p=e,`Installed: ${p.appName??em(p)}`));return s&&n.recordAction(s,{command:r,positionals:t.positionals??[],flags:t.flags??{},result:m??{}}),{ok:!0,data:m}}finally{f&&rA(f)}}async function aG(e,t,r){let a=e.flags?.batchOnError??"stop";if("stop"!==a)return N("INVALID_ARGS",`Unsupported batch on-error mode: ${a}.`);let i=e.flags?.batchMaxSteps??P;if(!Number.isInteger(i)||i<1||i>1e3)return N("INVALID_ARGS",`Invalid batch max-steps: ${String(e.flags?.batchMaxSteps)}`);try{let a=eu(e.flags?.batchSteps,i),n=Date.now(),o=[];for(let i=0;i<a.length;i+=1){let n=a[i],s=await aV(e,t,n,r,i+1);if(!s.ok)return{ok:!1,error:{code:s.error.code,message:`Batch failed at step ${s.step} (${n.command}): ${s.error.message}`,hint:s.error.hint,diagnosticId:s.error.diagnosticId,logPath:s.error.logPath,details:{...s.error.details??{},step:s.step,command:n.command,positionals:n.positionals,executed:i,total:a.length,partialResults:o}}};o.push(s.result)}return{ok:!0,data:{total:a.length,executed:a.length,totalDurationMs:Date.now()-n,results:o}}}catch(t){let e=tg(t);return N(e.code,e.message,e.details)}}async function aV(e,t,r,a,i){let n=Date.now(),o=function(e,t){let{batchSteps:r,batchOnError:a,batchMaxSteps:i,...n}=t??{};return a$(e,n)}(e.flags,r.flags);void 0===o.session&&(o.session=t);let s=await a({token:e.token,session:t,command:r.command,positionals:r.positionals,flags:o,runtime:r.runtime,meta:e.meta}),l=Date.now()-n;return s.ok?{ok:!0,step:i,result:{step:i,command:r.command,ok:!0,data:s.data??{},durationMs:l}}:{ok:!1,step:i,error:s.error}}async function aq(e){let t,r,a,{deviceName:i,runtime:n,simulatorSetPath:o,reuseExisting:s,boot:l,ensureReady:d}=e;if("darwin"!==process.platform)throw new tv("UNSUPPORTED_PLATFORM","ensure-simulator is only available on macOS");let u={simulatorSetPath:o??void 0};if(s){let e=await aj({deviceName:i,runtime:n,simctlOpts:u});e?(t=e.udid,r=e.runtime,a=!1):(t=(await aH({deviceName:i,runtime:n,simctlOpts:u})).udid,r=await aB(t,u),a=!0)}else t=(await aH({deviceName:i,runtime:n,simctlOpts:u})).udid,r=await aB(t,u),a=!0;let c=!1;if(l){let e={platform:"ios",id:t,name:i,kind:"simulator",target:"mobile",...o?{simulatorSetPath:o}:{}};await d(e),c=!0}return{udid:t,device:i,runtime:r,created:a,booted:c}}async function aj(e){let{deviceName:t,runtime:r,simctlOpts:a}=e,i=await th("xcrun",g(["list","devices","-j"],a),{allowFailure:!0,timeoutMs:W});if(0!==i.exitCode)return null;try{let e=JSON.parse(String(i.stdout??""));for(let[a,i]of Object.entries(e.devices??{}))if(!r||aK(a).includes(aK(r))){for(let e of i)if(e.isAvailable&&e.name.toLowerCase()===t.toLowerCase())return{udid:e.udid,runtime:a}}return null}catch{return null}}async function aH(e){let{deviceName:t,runtime:r,simctlOpts:a}=e,i=r?["create",t,t,r]:["create",t,t],n=await th("xcrun",g(i,a),{allowFailure:!0});if(0!==n.exitCode)throw new tv("COMMAND_FAILED","Failed to create iOS simulator",{deviceName:t,runtime:r,stdout:String(n.stdout??""),stderr:String(n.stderr??""),exitCode:n.exitCode,hint:"Ensure the device type and runtime identifiers are valid. Run `xcrun simctl list devicetypes` and `xcrun simctl list runtimes` to see available options."});let o=String(n.stdout??"").trim();if(!o)throw new tv("COMMAND_FAILED","simctl create returned no UDID",{deviceName:t,runtime:r,stdout:String(n.stdout??""),stderr:String(n.stderr??"")});return{udid:o}}async function aB(e,t){let r=await th("xcrun",g(["list","devices","-j"],t),{allowFailure:!0,timeoutMs:W});if(0!==r.exitCode)return"";try{let t=JSON.parse(String(r.stdout??""));for(let[r,a]of Object.entries(t.devices??{}))if(a.some(t=>t.udid===e))return r;return""}catch{return""}}function aK(e){return e.toLowerCase().replace(/[._-]/g,"")}async function az(e){let{req:t,sessionName:r,sessionStore:a}=e;if("session_list"===t.command)return{ok:!0,data:{sessions:a.toArray().map(e=>({name:e.name,platform:e.device.platform,target:e.device.target??"mobile",surface:e.surface??"app",device:e.device.name,id:e.device.id,device_id:e.device.id,createdAt:e.createdAt,..."ios"===e.device.platform&&{device_udid:e.device.id,ios_simulator_device_set:e.device.simulatorSetPath??null}}))}};if("ensure-simulator"===t.command)try{let e=t.flags??{},r=e.device,a=e.runtime,i=e1(e.iosSimulatorDeviceSet);if(!r)return N("INVALID_ARGS","ensure-simulator requires --device <name>");let n=await aq({deviceName:r,runtime:a,simulatorSetPath:i,reuseExisting:!1!==e.reuseExisting,boot:!0===e.boot,ensureReady:e8});return{ok:!0,data:{udid:n.udid,device:n.device,runtime:n.runtime,ios_simulator_device_set:i??null,created:n.created,booted:n.booted}}}catch(t){let e=tg(t);return N(e.code,e.message,e.details)}if("devices"===t.command)try{let e=[],r=et(t.flags?.androidDeviceAllowlist),a=tu(t.flags?.platform),i=h({simulatorSetPath:e1(t.flags?.iosSimulatorDeviceSet),platform:a,target:t.flags?.target});if("android"===a){let{listAndroidDevices:t}=await import("./155.js");e.push(...await t({serialAllowlist:r}))}else if("ios"===a||"macos"===a){let{listAppleDevices:t}=await import("./155.js");e.push(...await t({simulatorSetPath:i}))}else{if("apple"!==a){let{listAndroidDevices:t}=await import("./155.js");try{e.push(...await t({serialAllowlist:r}))}catch{}}let{listAppleDevices:t}=await import("./155.js");try{e.push(...await t({simulatorSetPath:i}))}catch{}}let n="ios"===a||"macos"===a?e.filter(e=>e.platform===a):e,o=(t.flags?.target?n.filter(e=>(e.target??"mobile")===t.flags?.target):n).map(({simulatorSetPath:e,...t})=>t);return{ok:!0,data:{devices:o}}}catch(t){let e=tg(t);return N(e.code,e.message,e.details)}if("apps"===t.command){let e=a.get(r),i=t.flags??{},n=r9(t.command,e,i);if(n)return n;let o=await ar({session:e,flags:i,ensureReady:!0});if(!to("apps",o))return N("UNSUPPORTED_OPERATION","apps is not supported on this device");let s=t.flags?.appsFilter??"all";if(Y(o.platform)){let{listIosApps:e}=await import("./155.js");return{ok:!0,data:{apps:(await e(o,s)).map(e=>e.name&&e.name!==e.bundleId?`${e.name} (${e.bundleId})`:e.bundleId)}}}let{listAndroidApps:l}=await import("./155.js");return{ok:!0,data:{apps:(await l(o,s)).map(e=>e.name&&e.name!==e.package?`${e.name} (${e.package})`:e.package)}}}return null}async function aW(e){let{ensureAndroidEmulatorBooted:t}=await import("./155.js");return await t(e)}let aJ='iOS appstate requires an active session on the target device. Run open first (for example: open --session sim --platform ios --device "<name>" <app>).',aZ='macOS appstate requires an active session on the target device. Run open first (for example: open --session macos --platform macos "System Settings").';async function aX(e){let{req:t,sessionName:r,sessionStore:a}=e,i=a.get(r),n=t.flags??{},o=tu(n.platform);if(!i&&"string"==typeof n?.session&&n.session.trim().length>0)return N("SESSION_NOT_FOUND","ios"===o?`No active session "${r}". Run open with --session ${r} first.`:`No active session "${r}". Run open with --session ${r} first, or omit --session to query by device selector.`);let s=r9("appstate",i,n);if(s)return s;let l=(i?.device.platform==="ios"||i?.device.platform==="macos")&&function(e,t){if(!t)return!1;if(!r7(e))return!0;let r=tu(e?.platform);return!(r&&!tn(t.device.platform,r)||e?.target&&e.target!==(t.device.target??"mobile")||e?.udid&&e.udid!==t.device.id||e?.serial&&e.serial!==t.device.id)&&(!e?.device||e.device.trim().toLowerCase()===t.device.name.trim().toLowerCase())}(n,i);if("ios"===o&&!l)return N("SESSION_NOT_FOUND",aJ);if("macos"===o&&!l)return N("SESSION_NOT_FOUND",aZ);if(l&&i){let e=i.appName??i.appBundleId;if(!i.appName&&!i.appBundleId){if("macos"===i.device.platform&&i.surface&&"app"!==i.surface&&"frontmost-app"!==i.surface)return{ok:!0,data:{platform:i.device.platform,appName:i.surface,appBundleId:i.appBundleId,source:"session",surface:i.surface}};let e="macos"===i.device.platform?"macOS":"iOS";return N("COMMAND_FAILED",`No foreground app is tracked for this ${e} session. Open an app in the session, then retry appstate.`)}return{ok:!0,data:{platform:i.device.platform,appName:e??"unknown",appBundleId:i.appBundleId,source:"session",surface:i.surface??"app",..."ios"===i.device.platform?{device_udid:i.device.id,ios_simulator_device_set:i.device.simulatorSetPath??null}:{}}}}let d=await ar({session:i,flags:n,ensureReady:!0});if("ios"===d.platform)return N("SESSION_NOT_FOUND",aJ);if("macos"===d.platform)return N("SESSION_NOT_FOUND",aZ);let{getAndroidAppState:u}=await import("./155.js"),c=await u(d);return{ok:!0,data:{platform:"android",package:c.package,activity:c.activity}}}async function aY(e){let{req:t,sessionName:r,sessionStore:a}=e;if("boot"===t.command){let e,i=a.get(r),n=t.flags??{},o=r9(t.command,i,n);if(o)return o;let s="android"===(tu(n.platform)??i?.device.platform),l=!0===n.headless;if(l&&!s)return N("INVALID_ARGS","boot --headless is supported only for Android emulators.");let d=ai({flags:n,sessionDevice:i?.device}),u=s&&!!d,c=!1;try{e=await ar({session:i,flags:n,ensureReady:!1})}catch(r){let t=tg(r);if(s&&l&&!d&&"DEVICE_NOT_FOUND"===t.code)return N("INVALID_ARGS","boot --headless requires --device <avd-name> (or an Android emulator session target).");if(!u||"DEVICE_NOT_FOUND"!==t.code||!d)throw r;e=await aW({avdName:d,serial:n.serial,headless:l}),c=!0}if(n.target&&(e.target??"mobile")!==n.target)return N("DEVICE_NOT_FOUND",`No ${e.platform} device found matching --target ${n.target}.`);if(s&&l){if("android"!==e.platform||"emulator"!==e.kind)return N("INVALID_ARGS","boot --headless is supported only for Android emulators.");if(!c){let t=ai({flags:n,sessionDevice:i?.device,resolvedDevice:e});if(!t)return N("INVALID_ARGS","boot --headless requires --device <avd-name> (or an Android emulator session target).");e=await aW({avdName:t,serial:n.serial,headless:!0})}await e8(e)}else("android"!==e.platform||!0!==e.booted)&&await e8(e);return to("boot",e)?{ok:!0,data:{platform:e.platform,target:e.target??"mobile",device:e.name,id:e.id,kind:e.kind,booted:!0}}:N("UNSUPPORTED_OPERATION","boot is not supported on this device")}return"appstate"===t.command?await aX({req:t,sessionName:r,sessionStore:a}):null}function aQ(e){return Math.round(10*e)/10}let a0="adb-shell-dumpsys-cpuinfo",a1="adb-shell-dumpsys-meminfo";async function a2(e,t){try{let r=await th("adb",eX(e,["shell","dumpsys","cpuinfo"]),{timeoutMs:15e3});return function(e,t,r){let a=new Set,i=0;for(let r of e.split("\n")){var n,o;let e=r.trim();if(0===e.length)continue;let s=e.match(/^([0-9]+(?:\.[0-9]+)?)%\s+\d+\/([^\s]+):\s/);if(!s)continue;let l=Number(s[1]),d=s[2];Number.isFinite(l)&&(n=d,o=t,n===o||n.startsWith(`${o}:`))&&(i+=l,a.add(d))}return{usagePercent:aQ(i),measuredAt:r,method:a0,matchedProcesses:[...a]}}(r.stdout,t,new Date().toISOString())}catch(e){throw a3("cpu",t,e)}}async function a5(e,t){try{let r=await th("adb",eX(e,["shell","dumpsys","meminfo",t]),{timeoutMs:15e3});return function(e,t,r){if(/no process found for:/i.test(e))throw new tv("COMMAND_FAILED",`Android meminfo did not find a running process for ${t}`,{metric:"memory",package:t,hint:"Run open <app> for this session again to ensure the Android app is active, then retry perf."});let a=a4(e,"TOTAL PSS")??function(e){for(let t of e.split("\n")){let e=t.trim();if(!/^TOTAL\b(?!\s+PSS:)/.test(e))continue;let r=e.split(/\s+/).slice(1).find(e=>null!==a8(e));if(!r)break;return a8(r)??void 0}}(e);if(void 0===a)throw new tv("COMMAND_FAILED",`Failed to parse Android meminfo output for ${t}`,{metric:"memory",package:t,hint:"Retry perf after reopening the app session. If the problem persists, capture adb shell dumpsys meminfo output for debugging."});return{totalPssKb:a,totalRssKb:a4(e,"TOTAL RSS"),measuredAt:r,method:a1}}(r.stdout,t,new Date().toISOString())}catch(e){throw a3("memory",t,e)}}function a3(e,t,r){return r instanceof tv&&("TOOL_MISSING"===r.code||"COMMAND_FAILED"===r.code)?new tv(r.code,r.message,{...r.details??{},metric:e,package:t},r):r instanceof tv?r:new tv("COMMAND_FAILED",`Failed to sample Android ${e} for ${t}`,{metric:e,package:t},r)}function a4(e,t){let r=t.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),a=e.match(RegExp(`${r}:\\s*([0-9][0-9,]*)`,"i"));if(a)return a8(a[1])??void 0}function a8(e){let t=e.replaceAll(",","").match(/^-?\d+(?:\.\d+)?/);if(!t)return null;let r=Number(t[0]);return Number.isFinite(r)?r:null}let a6="ps-process-snapshot",a9="ps-process-snapshot",a7="xctrace-activity-monitor",ie="xctrace-activity-monitor";async function it(e,t){if("ios"===e.platform&&"device"===e.kind)return await ii(e,t);let r=await ia(e,t),a=await ic(e,r);if(0===a.length)throw new tv("COMMAND_FAILED",`No running process found for ${t}`,{appBundleId:t,hint:"Run open <app> for this session again to ensure the Apple app is active, then retry perf."});let i=new Date().toISOString(),o=M(a.map(e=>n.basename(ip(e.command))));return im({usagePercent:a.reduce((e,t)=>e+t.cpuPercent,0),residentMemoryKb:a.reduce((e,t)=>e+t.rssKb,0),measuredAt:i,matchedProcesses:o,cpuMethod:a6,memoryMethod:a9})}async function ir(e){let t=K(e),r=ih(t,e=>"schema"===e.name&&"activity-monitor-process-live"===e.attributes.name);if(!r)throw new tv("COMMAND_FAILED","Failed to parse xctrace activity-monitor-process-live schema");let a=r.children.filter(e=>"col"===e.name).map(e=>{let t;return t=e.children.find(e=>"mnemonic"===e.name),t?.text??null??""}),i=a.indexOf("pid"),n=a.indexOf("process"),o=a.indexOf("cpu-total"),s=a.indexOf("memory-real");if(i<0||n<0||o<0||s<0)throw new tv("COMMAND_FAILED","xctrace activity-monitor-process-live export is missing expected columns");let l=function e(t,r){let a=[];for(let i of t)r(i)&&a.push(i),a.push(...e(i.children,r));return a}(t,e=>"row"===e.name),d=[],u=new Map;for(let e of l){var c,f;let t=e.children;if(0===t.length)continue;for(let e of t){let t=ih(e.children,e=>"pid"===e.name&&"string"==typeof e.attributes.id);if(t?.attributes.id){let e=Number(t.text);u.set(t.attributes.id,{numberValue:Number.isFinite(e)?e:null})}e.attributes.id&&u.set(e.attributes.id,{numberValue:ig(e),processName:iv(e)})}let r=iw(t[i],u),a=(c=t[n],f=u,c?c.attributes.ref?f.get(c.attributes.ref)?.processName??null:iv(c):null);null!==r&&Number.isFinite(r)&&a&&d.push({pid:r,processName:a,cpuTimeNs:iw(t[o],u),residentMemoryBytes:iw(t[s],u)})}return d}async function ia(e,t){let r="macos"===e.platform?await id(t):await iu(e,t),a="macos"===e.platform?n.join(r,"Contents","Info.plist"):n.join(r,"Info.plist"),i=await e9(a,"CFBundleExecutable");if(!i)throw new tv("COMMAND_FAILED",`Failed to resolve executable for ${t}`,{appBundleId:t,appPath:r});return{executableName:i,executablePath:"macos"===e.platform?n.join(r,"Contents","MacOS",i):void 0}}async function ii(e,t){let r=await io(e,t),a=await is(e,t),i=await is(e,t),n=il(await ir(a.xml),r,t,e),o=il(await ir(i.xml),r,t,e),s=i.capturedAtMs-a.capturedAtMs;if(s<=0)throw new tv("COMMAND_FAILED",`Invalid Activity Monitor sample window for ${t}`,{appBundleId:t,deviceId:e.id});if(null===n.cpuTimeNs||null===o.cpuTimeNs||null===o.residentMemoryBytes)throw new tv("COMMAND_FAILED",`Incomplete Activity Monitor sample for ${t}`,{appBundleId:t,deviceId:e.id,hint:"Keep the app running in the foreground while perf samples the device, then retry."});return im({usagePercent:Math.max(0,o.cpuTimeNs-n.cpuTimeNs)/(1e6*s)*100,residentMemoryKb:o.residentMemoryBytes/1024,measuredAt:new Date(i.capturedAtMs).toISOString(),matchedProcesses:o.matchedProcesses,cpuMethod:a7,memoryMethod:ie})}async function io(e,t){let r=(await J(e,"all")).find(e=>e.bundleId===t);if(!r)throw new tv("APP_NOT_INSTALLED",`No iOS device app found for ${t}`,{appBundleId:t,deviceId:e.id});if(!r.url)throw new tv("COMMAND_FAILED",`Missing app bundle URL for ${t}`,{appBundleId:t,deviceId:e.id});let a=r.url.replace(/\/$/,""),i=l(a),n=(await ei(e)).filter(e=>e.executable.startsWith(`${a}/`));if(0===n.length)throw new tv("COMMAND_FAILED",`No running process found for ${t}`,{appBundleId:t,deviceId:e.id,appBundlePath:i,hint:"Run open <app> for this session again to ensure the iOS app is active, then retry perf."});return n}async function is(e,t){let r=await i.mkdtemp(n.join(u.tmpdir(),"agent-device-ios-perf-")),a=n.join(r,"sample.trace"),o=n.join(r,"activity-monitor-process-live.xml");try{let r=["xctrace","record","--template","Activity Monitor","--device",e.id,"--all-processes","--time-limit","1s","--output",a,"--quiet","--no-prompt"],n=await th("xcrun",r,{allowFailure:!0,timeoutMs:6e4}),s=Date.now();if(0!==n.exitCode)throw new tv("COMMAND_FAILED",`Failed to record iOS device Activity Monitor sample for ${t}`,{cmd:"xcrun",args:r,exitCode:n.exitCode,stdout:n.stdout,stderr:n.stderr,appBundleId:t,deviceId:e.id,hint:iy(n.stdout,n.stderr)});let l=["xctrace","export","--input",a,"--xpath",'/trace-toc/run/data/table[@schema="activity-monitor-process-live"]',"--output",o],d=await th("xcrun",l,{allowFailure:!0,timeoutMs:15e3});if(0!==d.exitCode)throw new tv("COMMAND_FAILED",`Failed to export iOS device perf sample for ${t}`,{cmd:"xcrun",args:l,exitCode:d.exitCode,stdout:d.stdout,stderr:d.stderr,appBundleId:t,deviceId:e.id,hint:iy(d.stdout,d.stderr)});return{capturedAtMs:s,xml:await i.readFile(o,"utf8")}}finally{await i.rm(r,{recursive:!0,force:!0}).catch(()=>{})}}function il(e,t,r,a){let i=new Set(t.map(e=>e.pid)),o=new Set(t.map(e=>n.basename(l(e.executable)))),s=e.filter(e=>i.has(e.pid)||o.has(e.processName));if(0===s.length)throw new tv("COMMAND_FAILED",`No Activity Monitor sample found for ${r}`,{appBundleId:r,deviceId:a.id,hint:"Keep the app running in the foreground while perf samples the device, then retry."});let d=new Map;for(let e of s){let t=d.get(e.pid);if(!t){d.set(e.pid,e);continue}d.set(e.pid,{pid:e.pid,processName:e.processName||t.processName,cpuTimeNs:iI(t.cpuTimeNs,e.cpuTimeNs),residentMemoryBytes:iI(t.residentMemoryBytes,e.residentMemoryBytes)})}let u=[...d.values()],c=u.map(e=>e.cpuTimeNs).filter(e=>null!==e),f=u.map(e=>e.residentMemoryBytes).filter(e=>null!==e);return{cpuTimeNs:c.length>0?c.reduce((e,t)=>e+t,0):null,residentMemoryBytes:f.length>0?f.reduce((e,t)=>e+t,0):null,matchedProcesses:M(u.map(e=>e.processName))}}async function id(e){let t=`kMDItemCFBundleIdentifier == "${e.replaceAll('"','\\"')}"`,r=await th("mdfind",[t],{allowFailure:!0,timeoutMs:15e3});if(0!==r.exitCode)throw new tv("COMMAND_FAILED",`Failed to resolve macOS app bundle for ${e}`,{appBundleId:e,stdout:r.stdout,stderr:r.stderr,exitCode:r.exitCode});let a=r.stdout.split("\n").map(e=>e.trim()).find(e=>e.endsWith(".app"));if(!a)throw new tv("APP_NOT_INSTALLED",`No macOS app found for ${e}`,{appBundleId:e});return a}async function iu(e,t){let r=te(e,["get_app_container",e.id,t,"app"]),a=await th("xcrun",r,{allowFailure:!0,timeoutMs:15e3});if(0!==a.exitCode)throw new tv("COMMAND_FAILED",`Failed to resolve iOS simulator app container for ${t}`,{appBundleId:t,stdout:a.stdout,stderr:a.stderr,exitCode:a.exitCode,hint:"Ensure the iOS simulator app is installed and booted, then retry perf."});let i=a.stdout.trim();if(0===i.length)throw new tv("APP_NOT_INSTALLED",`No iOS simulator app container found for ${t}`,{appBundleId:t});return i}async function ic(e,t){let r="macos"===e.platform?["-axo","pid=,%cpu=,rss=,command="]:te(e,["spawn",e.id,"ps","-axo","pid=,%cpu=,rss=,command="]);return(function(e){let t=[];for(let r of e.split("\n")){let e=r.trim();if(0===e.length)continue;let a=e.match(/^(\d+)\s+([0-9]+(?:\.[0-9]+)?)\s+(\d+)\s+(.+)$/);if(!a)continue;let i=Number(a[1]),n=Number(a[2]),o=Number(a[3]),s=a[4].trim();Number.isFinite(i)&&Number.isFinite(n)&&Number.isFinite(o)&&t.push({pid:i,cpuPercent:n,rssKb:o,command:s})}return t})((await th("macos"===e.platform?"ps":"xcrun",r,{timeoutMs:15e3})).stdout).filter(e=>{var r,a;let i;return r=e.command,a=t,i=ip(r),!!(a.executablePath&&(i===a.executablePath||r.startsWith(`${a.executablePath} `)))||n.basename(i)===a.executableName})}function ip(e){let[t=""]=e.trim().split(/\s+/,1);return t}function im(e){return{cpu:{usagePercent:aQ(e.usagePercent),measuredAt:e.measuredAt,method:e.cpuMethod,matchedProcesses:e.matchedProcesses},memory:{residentMemoryKb:Math.round(e.residentMemoryKb),measuredAt:e.measuredAt,method:e.memoryMethod,matchedProcesses:e.matchedProcesses}}}function ih(e,t){for(let r of e){if(t(r))return r;let e=ih(r.children,t);if(e)return e}}function ig(e){if(!e||e.children.some(e=>"sentinel"===e.name)||!e.text)return null;let t=Number(e.text);return Number.isFinite(t)?t:null}function iw(e,t){return e?e.attributes.ref?t.get(e.attributes.ref)?.numberValue??null:ig(e):null}function iv(e){let t=e?.attributes.fmt?.trim()??"";return t?t.replace(/\s+\(\d+\)$/,"").trim():null}function iy(e,t){let r=eb(e,t);if(r)return r;let a=`${e}
|
|
5
|
+
`,recoveredLineCount:s.length}}async function t0(e,t,r,a,i,n){let s="active",l=o("xcrun",function(e){let{deviceId:t,appBundleId:r,simulatorSetPath:a}=e;return g(["spawn",t,"log","stream","--style","compact","--level","info","--predicate",tY(r)],{simulatorSetPath:a})}({deviceId:e,appBundleId:t,simulatorSetPath:i}),{stdio:["ignore","pipe","pipe"]}),d=tK(r,{redactionPatterns:a});"number"==typeof l.pid&&tq(n,l.pid);let u=tz(l,r,{endStreamOnClose:!0,writer:d}).then(e=>(0!==e.exitCode&&(s="failed"),tj(n),e));return{backend:"ios-simulator",getState:()=>s,startedAt:Date.now(),wait:u,stop:async()=>{l.killed||l.kill("SIGINT"),await tH(u),l.killed||l.kill("SIGKILL"),await tH(u),tj(n)}}}async function t1(e,t,r,a){let i="active",n=o("log",["stream","--style","compact","--predicate",tY(e)],{stdio:["ignore","pipe","pipe"]}),s=tK(t,{redactionPatterns:r});"number"==typeof n.pid&&tq(a,n.pid);let l=tz(n,t,{endStreamOnClose:!0,writer:s}).then(e=>(0!==e.exitCode&&(i="failed"),tj(a),e));return{backend:"macos",getState:()=>i,startedAt:Date.now(),wait:l,stop:async()=>{n.killed||n.kill("SIGINT"),await tH(l),n.killed||n.kill("SIGKILL"),await tH(l),tj(a)}}}async function t2(e,t,r,a){let i="active",n=o("xcrun",["devicectl","device","log","stream","--device",e],{stdio:["ignore","pipe","pipe"]}),s=tK(t,{redactionPatterns:r});"number"==typeof n.pid&&tq(a,n.pid);let l=tz(n,t,{endStreamOnClose:!0,writer:s}).then(e=>(0!==e.exitCode&&(i="failed"),tj(a),e));return{backend:"ios-device",getState:()=>i,startedAt:Date.now(),wait:l,stop:async()=>{n.killed||n.kill("SIGINT"),await tH(l),n.killed||n.kill("SIGKILL"),await tH(l),tj(a)}}}let t5=RegExp("\\b(GET|POST|PUT|PATCH|DELETE|HEAD|OPTIONS)\\b\\s+https?:\\/\\/","i"),t3=/https?:\/\/[^\s"'<>\])]+/i,t4=[/\bstatus(?:Code)?["'=: ]+([1-5]\d{2})\b/i,/\bresponse(?:\s+code)?["'=: ]+([1-5]\d{2})\b/i,/\bHTTP\/[0-9.]+\s+([1-5]\d{2})\b/i];function t8(e,t,r=e.limits.maxEntries){let a=[...e.entries],i=new Set(a.map(e=>t9(e)));for(let e of t.entries){let t=t9(e);if(!i.has(t)&&(i.add(t),a.push(e),a.length>=r))break}return{...e,matchedLines:a.length,entries:a}}function t6(e,t){let r=rl(t?.maxEntries,25,1,200),a=t?.backend,i=t?.include??"summary",n=rl(t?.maxPayloadChars,2048,64,16384),o=rl(t?.maxScanLines,4e3,100,2e4),s=e.split("\n"),l=Math.max(0,s.length-o),d=s.slice(l),u=[];for(let e=d.length-1;e>=0&&u.length<r;e-=1){let t=d[e];if(!t?.trim())continue;let r=function(e,t,r,a,i,n){let o=e[t]?.trim();if(!o)return null;let s=function(e){let t=e.indexOf("{");if(t<0)return null;let r=e.lastIndexOf("}");if(r<=t)return null;let a=e.slice(t,r+1);try{let e=JSON.parse(a);return e&&"object"==typeof e?e:null}catch{return null}}(o),l=ri(s,["method","httpMethod"]),d=ri(s,["url","requestUrl"]),u=function(e,t){if(!e)return null;for(let r of t){let t=e[r];if("number"==typeof t&&Number.isInteger(t))return t;if("string"==typeof t&&/^\d{3}$/.test(t.trim()))return Number.parseInt(t.trim(),10)}return null}(s,["status","statusCode","responseCode"]),c=t5.exec(o),f=/\bmethod["'=: ]+([A-Z]+)\b/i.exec(o),p=(l??f?.[1]??c?.[1])?.toUpperCase(),m=t3.exec(o),h=d??m?.[0];if(!h)return null;let g=u??re(o)??void 0;if(!(l||f?.[1]||c?.[1]||void 0!==g||/\bURL["'=: ]+https?:\/\//i.test(o)||/\bheaders?["'=: ]+/i.test(o)||/\b(?:requestBody|responseBody|payload|request|response)["'=: ]+/i.test(o)))return null;let w={method:p,url:h,status:g,timestamp:rt(o),packetId:rr(o)??void 0,durationMs:ra(o)??void 0,raw:rs(o,n),line:r};if("android"===a&&function(e,t,r){let a=t7(t,r,5),i=e.packetId??a.map(e=>rr(e)).find(e=>"string"==typeof e&&e.length>0);i&&(e.packetId=i);let n=i?t7(t,r,12).filter(e=>rr(e)===i):a;e.timestamp||(e.timestamp=n.map(e=>rt(e)).find(e=>"string"==typeof e&&e.length>0)),void 0===e.status&&(e.status=n.map(e=>re(e)).find(e=>"number"==typeof e)),void 0===e.durationMs&&(e.durationMs=n.map(e=>ra(e)).find(e=>"number"==typeof e))}(w,e,t),"headers"===i||"all"===i){let e=function(e,t){if(t){let e=t.headers??t.requestHeaders??t.responseHeaders;if(void 0!==e)return ro(e)}let r=/\bheaders?["'=: ]+(\{.*\})/i.exec(e);return r?.[1]?.trim()}(o,s);e&&(w.headers=rs(e,n))}if("body"===i||"all"===i){let e=rn(o,s,["requestBody","body","payload","request"]),t=rn(o,s,["responseBody","response"]);e&&(w.requestBody=rs(e,n)),t&&(w.responseBody=rs(t,n))}return w}(d,e,l+e+1,a,i,n);r&&u.push(r)}return{path:t?.path??"<memory>",exists:!0,scannedLines:d.length,matchedLines:u.length,entries:u,include:i,limits:{maxEntries:r,maxPayloadChars:n,maxScanLines:o}}}function t9(e){return`${e.timestamp??""}|${e.method??""}|${e.url}|${e.status??""}|${e.raw}`}function t7(e,t,r){let a=[],i=Math.max(0,t-r),n=Math.min(e.length-1,t+r);for(let t=i;t<=n;t+=1){let r=e[t]?.trim();r&&a.push(r)}return a}function re(e){for(let t of t4){let r=t.exec(e);if(!r)continue;let a=Number.parseInt(r[1]??"",10);if(Number.isInteger(a))return a}return null}function rt(e){let t=/\b\d{4}-\d{2}-\d{2}[ T]\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z)?\b/.exec(e);if(t)return t[0];let r=/\b\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d+\b/.exec(e);return r?.[0]}function rr(e){let t=/\bpacket id (\d+)\b/i.exec(e);return t?.[1]??null}function ra(e){let t=/\b(?:duration|elapsed request\/response time, ms)[:= ]+(\d+)\b/i.exec(e);if(!t)return null;let r=Number.parseInt(t[1]??"",10);return Number.isInteger(r)?r:null}function ri(e,t){if(e)for(let r of t){let t=e[r];if("string"==typeof t&&t.trim().length>0)return t.trim()}}function rn(e,t,r){if(t){for(let e of r)if(void 0!==t[e])return ro(t[e])}for(let t of r){let r=t.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),a=RegExp(`\\b${r}["'=: ]+(.+)$`,"i").exec(e);if(a?.[1])return a[1].trim()}}function ro(e){if("string"==typeof e)return e;try{return JSON.stringify(e)}catch{return String(e)}}function rs(e,t){return e.length<=t?e:`${e.slice(0,t)}...<truncated>`}function rl(e,t,r,a){return void 0!==e&&Number.isInteger(e)?Math.max(r,Math.min(a,e)):t}function rd(e,t){let r=process.env[e];if(!r)return t;let a=Number.parseInt(r,10);return Number.isInteger(a)&&a>0?a:t}function ru(e){let t=n.dirname(e);a.existsSync(t)||a.mkdirSync(t,{recursive:!0}),function(e,t){if(a.existsSync(e)&&!(a.statSync(e).size<t.maxBytes))for(let r=t.maxRotatedFiles;r>=1;r-=1){let t=1===r?e:`${e}.${r-1}`,i=`${e}.${r}`;a.existsSync(t)&&(a.existsSync(i)&&a.unlinkSync(i),a.renameSync(t,i))}}(e,{maxBytes:rd("AGENT_DEVICE_APP_LOG_MAX_BYTES",5242880),maxRotatedFiles:rd("AGENT_DEVICE_APP_LOG_MAX_FILES",1)})}async function rc(e){var t,r,i,n;let o,s,l,d,{device:u,appBundleId:c,appLogState:f,appLogStartedAt:p,appLogPath:m,maxEntries:h,include:g,maxPayloadChars:w,maxScanLines:v}=e,y="macos"===u.platform?"macos":"ios"===u.platform?"device"===u.kind?"ios-device":"ios-simulator":"android",I=(t={backend:y,maxEntries:h,include:g,maxPayloadChars:w,maxScanLines:v},o=rl(t?.maxEntries,25,1,200),s=t?.include??"summary",l=rl(t?.maxPayloadChars,2048,64,16384),d=rl(t?.maxScanLines,4e3,100,2e4),a.existsSync(m)?t6(a.readFileSync(m,"utf8"),{...t,path:m}):{path:m,exists:!1,scannedLines:0,matchedLines:0,entries:[],include:s,limits:{maxEntries:o,maxPayloadChars:l,maxScanLines:d}}),S=[],A=await rf({device:u,appBundleId:c,appLogPath:m,appLogState:f});if(A){let e=await tZ(u.id,c);if(e){let t=t6(e.text,{path:`${m} (adb logcat recovery)`,backend:"android",maxEntries:h,include:g,maxPayloadChars:w,maxScanLines:v});t.entries.length>0&&(I=t8(t,I,h),S.push((r=A,i=e.recoveredPids,"stale-active"===r.reason?`Session app log stream was still bound to prior Android PID ${r.trackedPid}. Recovered recent Android HTTP entries from adb logcat for PID set ${i.join(", ")}.`:`Session app log stream was inactive. Recovered recent Android HTTP entries from adb logcat for PID set ${i.join(", ")}.`)))}}if("ios"===u.platform&&"simulator"===u.kind&&c&&0===I.entries.length){let e=await rm({deviceId:u.id,appBundleId:c,startedAt:p,simulatorSetPath:u.simulatorSetPath,appLogPath:m,maxEntries:h,include:g,maxPayloadChars:w,maxScanLines:v});e&&(e.dump.entries.length>0?(I=t8(e.dump,I,h),S.push(`Recovered ${e.dump.entries.length} iOS simulator HTTP entr${1===e.dump.entries.length?"y":"ies"} from simctl log show (${e.recoveredLineCount} app log lines scanned).`)):e.recoveredLineCount>0&&S.push(`Recovered ${e.recoveredLineCount} recent iOS simulator app log lines from simctl log show, but none looked like HTTP traffic. This app may not emit request URLs, status, or timing into Unified Logging for this repro window.`))}return void 0===f?S.push("Capture uses the session app log file. For fresh traffic, run logs clear --restart before reproducing requests."):"active"!==f&&0===S.length&&("ios"===u.platform&&"simulator"===u.kind?S.push("Session app log stream is inactive. The iOS simulator recovery path scanned recent simctl log history, but a fresh logs clear --restart window is still the most reliable repro loop."):S.push("Session app log stream is inactive. Run logs clear --restart, reproduce the request window again, then rerun network dump.")),0===I.entries.length&&S.push("ios"===(n=u).platform&&"simulator"===n.kind?"No HTTP(s) entries were found in recent iOS simulator app logs. If the app only emits non-HTTP diagnostics, inspect logs path or add app-side URLSession/network logging for per-request timing and payload details.":"ios"===n.platform?"No HTTP(s) entries were found in recent iOS device app logs. iOS network dump only sees what the app emits into Unified Logging for this process.":"No HTTP(s) entries were found in recent session app logs."),{backend:y,dump:I,notes:S}}async function rf(e){let{device:t,appBundleId:r,appLogPath:i,appLogState:o}=e;if("android"!==t.platform||!r)return null;if(void 0!==o&&"active"!==o)return{reason:"inactive"};if("active"!==o)return null;let s=function(e){let t=function(e){if(!e||!a.existsSync(e))return null;try{return tV(a.readFileSync(e,"utf8"))}catch{return null}}(e)?.command;if(!t)return null;let r=/(?:^|\s)--pid\s+(\d+)(?:\s|$)/.exec(t);return r?.[1]??null}(n.join(n.dirname(i),tG));if(!s)return null;let l=await tJ(t.id,r);return l&&l!==s?{reason:"stale-active",trackedPid:s}:null}async function rp(e,t,r,i){ru(r);let n=a.createWriteStream(r,{flags:"a"}),o=function(){let e=process.env.AGENT_DEVICE_APP_LOG_REDACT_PATTERNS;if(!e)return[];let t=e.split(",").map(e=>e.trim()).filter(e=>e.length>0),r=[];for(let e of t)try{r.push(RegExp(e,"gi"))}catch{}return r}();if("ios"===e.platform)return"device"===e.kind?await t2(e.id,n,o,i):await t0(e.id,t,n,o,e.simulatorSetPath,i);if("android"===e.platform)return tW(t),await tX(e.id,t,n,o,i);if("macos"===e.platform)return await t1(t,n,o,i);throw n.end(),new tv("UNSUPPORTED_PLATFORM",`unsupported platform: ${e.platform}`)}async function rm(e){let t=await tQ({deviceId:e.deviceId,appBundleId:e.appBundleId,startedAt:e.startedAt,simulatorSetPath:e.simulatorSetPath});return t?{dump:t6(t.text,{path:`${e.appLogPath} (simctl log show recovery)`,backend:"ios-simulator",maxEntries:e.maxEntries,include:e.include,maxPayloadChars:e.maxPayloadChars,maxScanLines:e.maxScanLines}),recoveredLineCount:t.recoveredLineCount}:null}async function rh(e){await e.stop(),await tH(e.wait)}async function rg(e,t){let r={},a=[];if(t||a.push("No app bundle is tracked in this session. Run open <app> first for app-scoped logs."),"android"===e.platform){try{let e=await th("adb",["version"],{allowFailure:!0});r.adbAvailable=0===e.exitCode}catch{r.adbAvailable=!1}if(t)try{r.androidPidVisible=(await th("adb",["-s",e.id,"shell","pidof",t],{allowFailure:!0})).stdout.trim().length>0}catch{r.androidPidVisible=!1}}if("ios"===e.platform&&"simulator"===e.kind)try{let e=await th("xcrun",["simctl","help"],{allowFailure:!0});r.simctlAvailable=0===e.exitCode}catch{r.simctlAvailable=!1}if("ios"===e.platform&&"device"===e.kind)try{let e=await th("xcrun",["devicectl","--version"],{allowFailure:!0});r.devicectlAvailable=0===e.exitCode}catch{r.devicectlAvailable=!1}if("macos"===e.platform)try{let e=await th("log",["help"],{allowFailure:!0});r.logAvailable=0===e.exitCode}catch{r.logAvailable=!1}return{checks:r,notes:a}}function rw(e){let t=n.dirname(e),r=n.basename(e);a.existsSync(t)||a.mkdirSync(t,{recursive:!0}),a.existsSync(e)?a.truncateSync(e,0):a.writeFileSync(e,"","utf8");let i=0;for(let e of a.readdirSync(t)){if(!e.startsWith(`${r}.`))continue;let o=e.slice(r.length+1);if(/^\d+$/.test(o))try{a.unlinkSync(n.join(t,e)),i+=1}catch{}}return{path:e,cleared:!0,removedRotatedFiles:i}}let rv=new Map;function ry(e){let t=rv.get(e);if(t&&(clearTimeout(t.timer),rv.delete(e),t.deleteAfterDownload))try{a.rmSync(t.artifactPath,{force:!0})}catch{}}let rI=new Map;function rS(e,t){let r=rI.get(e);if(!r)throw new tv("INVALID_ARGS",`Uploaded artifact not found: ${e}`);if(r.tenantId&&r.tenantId!==t)throw new tv("UNAUTHORIZED","Uploaded artifact belongs to a different tenant");return clearTimeout(r.timer),r.artifactPath}function rA(e){let t=rI.get(e);t&&(clearTimeout(t.timer),rI.delete(e),a.rmSync(t.tempDir,{recursive:!0,force:!0}))}async function rb(e){let t=await r_(e);await th("tar",["xf",e.archivePath,"-C",e.tempDir]);let r=n.join(e.tempDir,t);if(!a.existsSync(r))throw new tv("INVALID_ARGS",`Expected extracted bundle "${t}" not found in archive`);return r}async function r_(e){let t=await th("tar",["-tf",e.archivePath],{allowFailure:!0});if(0!==t.exitCode)throw new tv("INVALID_ARGS","Artifact is not a valid tar archive",{archivePath:e.archivePath,stdout:t.stdout,stderr:t.stderr,exitCode:t.exitCode});let r=t.stdout.split(/\r?\n/).map(e=>e.trim()).filter(Boolean);if(0===r.length)throw new tv("INVALID_ARGS","Uploaded app bundle archive is empty");let a=r.map(rN),i=e.expectedRootName??function(e,t){let r=new Set;for(let t of e){let[e]=t.split("/");e&&r.add(e)}let a=[...r];if("ios"===t){let e=a.filter(e=>e.toLowerCase().endsWith(".app"));if(1===e.length)return e[0];if(0===e.length)throw new tv("INVALID_ARGS","iOS app bundle archives must contain a single top-level .app directory");throw new tv("INVALID_ARGS",`iOS app bundle archives must contain exactly one top-level .app directory, found: ${e.join(", ")}`)}if(1===a.length)return a[0];throw new tv("INVALID_ARGS",`Archive must contain a single top-level bundle, found: ${a.join(", ")}`)}(a,e.platform);if(!a.some(e=>e===i||e.startsWith(`${i}/`)))throw new tv("INVALID_ARGS",`Uploaded archive must contain a top-level "${i}" bundle`);for(let e of a){var n=e,o=i;if(n!==o&&!n.startsWith(`${o}/`))throw new tv("INVALID_ARGS",`Archive entry must stay inside top-level "${o}" bundle: ${n}`)}for(let t of(await th("tar",["-tvf",e.archivePath])).stdout.split(/\r?\n/).filter(Boolean))if("l"===t[0]||"h"===t[0])throw new tv("INVALID_ARGS","Uploaded app bundle archive cannot contain symlinks or hard links");return i}function rN(e){if(e.includes("\0"))throw new tv("INVALID_ARGS",`Invalid archive entry: ${e}`);if(n.posix.isAbsolute(e))throw new tv("INVALID_ARGS",`Archive entry must be relative: ${e}`);let t=n.posix.normalize(e).replace(/^(\.\/)+/,"");if(!t||"."===t||t.startsWith("../"))throw new tv("INVALID_ARGS",`Archive entry escapes bundle root: ${e}`);return t}let rx=ez(process.env.AGENT_DEVICE_ARTIFACT_IDLE_TIMEOUT_MS,6e4,1e3);function rD(e,t){return new Promise((r,i)=>{let n,o=a.createWriteStream(t),s=t=>{"destroy"in e&&"function"==typeof e.destroy&&e.destroy(t)},l=!1,d=0,u=e=>{if(!l){if(l=!0,n&&clearTimeout(n),e){o.destroy(),a.rmSync(t,{force:!0}),i(e);return}r()}},c=()=>{n&&clearTimeout(n),n=setTimeout(()=>{let e=new tv("COMMAND_FAILED","Artifact transfer timed out due to inactivity",{timeoutMs:rx});s(e),o.destroy(e),u(e)},rx)};e.on("data",e=>{c();let t=Buffer.isBuffer(e)?e.length:Buffer.byteLength(e);if((d+=t)>0x80000000){let e=new tv("INVALID_ARGS","Upload exceeds maximum size of 2147483648 bytes");s(e),o.destroy(e),u(e)}}),e.on("error",u),e.on("aborted",()=>{u(new tv("COMMAND_FAILED","Artifact transfer was interrupted"))}),o.on("error",u),o.on("finish",()=>u()),c(),e.pipe(o)})}async function rM(e){let t,r=e.headers["x-artifact-type"],i=e.headers["x-artifact-filename"];if(!r||!i)throw new tv("INVALID_ARGS","Missing required headers: x-artifact-type and x-artifact-filename");if("file"!==r&&"app-bundle"!==r)throw new tv("INVALID_ARGS",`Invalid x-artifact-type: ${r}. Must be "file" or "app-bundle".`);!function(e){if(void 0===e)return;let t=Number(e);if(Number.isFinite(t)&&t>0x80000000)throw new tv("INVALID_ARGS","Upload exceeds maximum size of 2147483648 bytes")}(e.headers["content-length"]);let o=function(e){let t=e.trim(),r=n.basename(t);if(!r||"."===r||".."===r)throw new tv("INVALID_ARGS",`Invalid artifact filename: ${e}`);return r}(i),s=(t=function(e){let t=e?.trim();if(!t)return"request";let r=t.replace(/[^a-zA-Z0-9._-]+/g,"-").replace(/^-+|-+$/g,"");return r.length>0?r.slice(0,48):"request"}("upload"),a.mkdtempSync(n.join(u.tmpdir(),`agent-device-artifact-${t}-`)));try{if("file"===r){let t=n.join(s,o);return await rD(e,t),{artifactPath:t,tempDir:s}}let t=n.join(s,"artifact.tar");await rD(e,t);let i=await rb({archivePath:t,tempDir:s,platform:"ios",expectedRootName:o});return a.rmSync(t,{force:!0}),{artifactPath:i,tempDir:s}}catch(e){throw a.rmSync(s,{recursive:!0,force:!0}),e}}let rk=new Set(["agent_device.command","agent-device.command"]),rP=new Set(["agent_device.install_from_source","agent-device.install_from_source"]),rL=new Set(["agent_device.release_materialized_paths","agent-device.release_materialized_paths"]),rR={"agent_device.lease.allocate":"lease_allocate","agent-device.lease.allocate":"lease_allocate","agent_device.lease.heartbeat":"lease_heartbeat","agent-device.lease.heartbeat":"lease_heartbeat","agent_device.lease.release":"lease_release","agent-device.lease.release":"lease_release"},rO=new Set([...rk,...rP,...rL,...Object.keys(rR)]);function rC(e,t,r,a){return{jsonrpc:"2.0",id:e,error:{code:t,message:r,data:a}}}function rE(e,t,r=200){e.statusCode=r,e.setHeader("content-type","application/json"),e.end(JSON.stringify(t))}function r$(e){switch(e){case"INVALID_ARGS":return 400;case"UNAUTHORIZED":return 401;case"SESSION_NOT_FOUND":return 404;default:return 500}}function rT(e,t){let r="string"==typeof t.authorization?t.authorization:"",a=r.toLowerCase().startsWith("bearer ")?r.slice(7):void 0,i="string"==typeof t["x-agent-device-token"]?t["x-agent-device-token"]:void 0;return("string"==typeof e.token?e.token:void 0)??i??a??""}function rF(e,t){let r=e[t];return"string"==typeof r?r:void 0}function rU(e,t){let r=e[t];return Number.isInteger(r)?Number(r):void 0}async function rG(e,t){if(!e)return{ok:!0};let r=await e(t);if(void 0===r||!0===r)return{ok:!0};if(!1===r){let e=tw(new tv("UNAUTHORIZED","Request rejected by auth hook"));return{ok:!1,statusCode:401,response:rC(t.rpcRequest.id??null,-32001,e.message,e)}}if(!1===r.ok){let e=tw(new tv(r.code??"UNAUTHORIZED",r.message??"Request rejected by auth hook",r.details));return{ok:!1,statusCode:401,response:rC(t.rpcRequest.id??null,-32001,e.message,e)}}if("string"==typeof r.tenantId&&r.tenantId.length>0){let e=eB(r.tenantId);if(!e){let e=tw(new tv("INVALID_ARGS","Auth hook returned invalid tenantId"));return{ok:!1,statusCode:500,response:rC(t.rpcRequest.id??null,-32e3,e.message,e)}}return{ok:!0,tenantId:e}}return{ok:!0}}async function rV(){let e,t=process.env.AGENT_DEVICE_HTTP_AUTH_HOOK;if(!t)return null;let r=process.env.AGENT_DEVICE_HTTP_AUTH_EXPORT||"default",a=n.isAbsolute(t)?t:n.resolve(t);try{e=await import(d(a).href)}catch(e){throw new tv("COMMAND_FAILED","Failed to load AGENT_DEVICE_HTTP_AUTH_HOOK module",{hookPath:a,error:e instanceof Error?e.message:String(e)})}let i=e[r];if("function"!=typeof i)throw new tv("INVALID_ARGS",`Auth hook export ${r} is not a function`,{hookPath:a,exportName:r});return i}async function rq(e){let t=await rV(),{handleRequest:r,token:a}=e;return s.createServer((e,i)=>{if("GET"===e.method&&"/health"===e.url){i.statusCode=200,i.setHeader("content-type","application/json"),i.end(JSON.stringify({ok:!0}));return}if("POST"===e.method&&"/upload"===e.url)return void rj(e,i,t,a);if("GET"===e.method&&e.url?.startsWith("/upload/"))return void rH(e,i,t,a);if("POST"!==e.method||"/rpc"!==e.url){i.statusCode=404,i.end("Not found");return}let n="";e.setEncoding("utf8"),e.on("data",t=>{(n+=t).length>1048576&&e.destroy(Error("request too large"))}),e.on("error",()=>{i.headersSent||rE(i,rC(null,-32700,"Parse error"),400)}),e.on("end",async()=>{let a,o;try{a=JSON.parse(n)}catch{rE(i,rC(null,-32700,"Parse error"),400);return}if("2.0"!==a.jsonrpc||"string"!=typeof a.method)return void rE(i,rC(a.id??null,-32600,"Invalid Request"),400);if(!rO.has(a.method))return void rE(i,rC(a.id??null,-32601,`Method not found: ${a.method}`),404);if(!a.params||"object"!=typeof a.params)return void rE(i,rC(a.id??null,-32602,"Invalid params"),400);try{var s;let n=a.params,l=function(e,t,r){if(rk.has(e))return{token:rT(t,r),session:t.session??"default",command:t.command??"",positionals:Array.isArray(t.positionals)?t.positionals:[],flags:t.flags,runtime:t.runtime,meta:t.meta};if(rP.has(e)){let e,a=rF(t,"platform");if("ios"!==a&&"android"!==a)throw new tv("INVALID_ARGS",'Invalid params: platform must be "ios" or "android"');return{token:rT(t,r),session:rF(t,"session")??"default",command:"install_source",positionals:[],flags:{platform:a},meta:{requestId:rF(t,"requestId"),installSource:function(e){let t=e.source;if(!t||"object"!=typeof t)throw new tv("INVALID_ARGS","Invalid params: source is required");if("url"===t.kind){let e="string"==typeof t.url?t.url.trim():"";if(!e)throw new tv("INVALID_ARGS","Invalid params: source.url is required for url sources");let r=t.headers,a={};if(void 0!==r){if(!r||"object"!=typeof r||Array.isArray(r))throw new tv("INVALID_ARGS","Invalid params: source.headers must be a string map");for(let[e,t]of Object.entries(r)){if("string"!=typeof t)throw new tv("INVALID_ARGS","Invalid params: source.headers values must be strings");a[e]=t}}return Object.keys(a).length>0?{kind:"url",url:e,headers:a}:{kind:"url",url:e}}if("path"===t.kind){let e="string"==typeof t.path?t.path.trim():"";if(!e)throw new tv("INVALID_ARGS","Invalid params: source.path is required for path sources");return{kind:"path",path:e}}throw new tv("INVALID_ARGS",'Invalid params: source.kind must be "url" or "path"')}(t),retainMaterializedPaths:(e=t.retainPaths,"boolean"==typeof e?e:void 0),materializedPathRetentionMs:rU(t,"retentionMs")}}}if(rL.has(e)){let e=rF(t,"materializationId")?.trim();if(!e)throw new tv("INVALID_ARGS","Invalid params: materializationId is required");return{token:rT(t,r),session:rF(t,"session")??"default",command:"release_materialized_paths",positionals:[],meta:{requestId:rF(t,"requestId"),materializationId:e}}}let a=rR[e];if(a)return{token:rT(t,r),session:rF(t,"session")??"default",command:a,positionals:[],meta:{tenantId:rF(t,"tenantId")??rF(t,"tenant"),runId:rF(t,"runId"),leaseId:rF(t,"leaseId"),leaseTtlMs:rU(t,"ttlMs"),leaseBackend:rF(t,"backend")}};throw new tv("INVALID_ARGS",`Method not found: ${e}`)}(a.method,n,e.headers);if(s=a.method,rk.has(s)&&("string"!=typeof l.command||0===l.command.length))return void rE(i,rC(a.id??null,-32602,"Invalid params: command is required"),400);o=U(l.meta?.requestId,a.id),l.meta={...l.meta,requestId:o},b(o);let d=()=>{i.writableFinished||e2(o)};e.on("aborted",d),i.on("close",d);let u=await rG(t,{headers:e.headers,rpcRequest:a,daemonRequest:l});if(!u.ok)return void rE(i,u.response,u.statusCode);u.tenantId&&(l.meta={...l.meta,tenantId:u.tenantId,sessionIsolation:l.meta?.sessionIsolation??l.flags?.sessionIsolation??"tenant"});let c=await r(l);if(c.ok)return void rE(i,{jsonrpc:"2.0",id:a.id??null,result:c});rE(i,rC(a.id??null,-32e3,c.error.message,c.error),r$(c.error.code))}catch(t){let e=tw(t);rE(i,rC(a.id??null,-32e3,e.message,e),r$(e.code))}finally{B(o)}})})}async function rj(e,t,a,i){try{var n;let o,s,l=rT({},e.headers),d=rB(l,i);if(d){t.statusCode=r$(d.code),t.setHeader("content-type","application/json"),t.end(JSON.stringify({ok:!1,error:d.message,code:d.code}));return}let u=await rG(a,{headers:e.headers,rpcRequest:{jsonrpc:"2.0",id:null,method:"agent_device.command"},daemonRequest:{token:l,session:"default",command:"upload",positionals:[]}});if(!u.ok){t.statusCode=u.statusCode,t.setHeader("content-type","application/json"),t.end(JSON.stringify({ok:!1,error:u.response.error?.data?.message??u.response.error?.message??"Unauthorized"}));return}let c=await rM(e),f=(n={artifactPath:c.artifactPath,tempDir:c.tempDir,tenantId:u.tenantId},o=r.randomUUID(),(s=setTimeout(()=>{rA(o)},3e5)).unref(),rI.set(o,{artifactPath:n.artifactPath,tempDir:n.tempDir,tenantId:n.tenantId,timer:s}),o);t.statusCode=200,t.setHeader("content-type","application/json"),t.end(JSON.stringify({ok:!0,uploadId:f}))}catch(r){let e=tw(r);t.statusCode=r$(e.code),t.setHeader("content-type","application/json"),t.end(JSON.stringify({ok:!1,error:e.message,code:e.code}))}}async function rH(e,t,r,i){let n=e.url?.slice("/upload/".length)??"";if(!n){t.statusCode=400,t.end("Missing artifact id");return}try{let o=rT({},e.headers),s=rB(o,i);if(s){t.statusCode=r$(s.code),t.setHeader("content-type","application/json"),t.end(JSON.stringify({ok:!1,error:s.message,code:s.code}));return}let l=await rG(r,{headers:e.headers,rpcRequest:{jsonrpc:"2.0",id:null,method:"agent_device.command"},daemonRequest:{token:o,session:"default",command:"download_artifact",positionals:[n]}});if(!l.ok){t.statusCode=l.statusCode,t.setHeader("content-type","application/json"),t.end(JSON.stringify({ok:!1,error:l.response.error?.data?.message??l.response.error?.message??"Unauthorized"}));return}let d=function(e,t){let r=rv.get(e);if(!r)throw new tv("INVALID_ARGS",`Artifact not found: ${e}`);if(r.tenantId&&r.tenantId!==t)throw new tv("UNAUTHORIZED","Artifact belongs to a different tenant");if(!a.existsSync(r.artifactPath))throw ry(e),new tv("COMMAND_FAILED",`Artifact file is missing: ${r.artifactPath}`);return{artifactPath:r.artifactPath,fileName:r.fileName,deleteAfterDownload:r.deleteAfterDownload}}(n,l.tenantId),u=a.createReadStream(d.artifactPath);t.statusCode=200,t.setHeader("content-type","application/octet-stream"),d.fileName&&t.setHeader("content-disposition",`attachment; filename="${d.fileName.replace(/"/g,"")}"`),u.on("error",e=>{if(t.headersSent)t.destroy(e);else{let r=tw(e);t.statusCode=r$(r.code),t.end(r.message)}}),t.on("close",()=>{t.writableFinished&&ry(n)}),u.pipe(t)}catch(r){let e=tw(r);t.statusCode=r$(e.code),t.setHeader("content-type","application/json"),t.end(JSON.stringify({ok:!1,error:e.message,code:e.code}))}}function rB(e,t){return t&&e!==t?tw(new tv("UNAUTHORIZED","Invalid token")):null}function rK(e){if(!e)return;let t=e.trim();if(t&&/^[a-zA-Z0-9._-]{1,128}$/.test(t))return t}function rz(e){if(!e)return;let t=e.trim();if(t&&/^[a-f0-9]{16,128}$/i.test(t))return t.toLowerCase()}function rW(e){let t=(e??"").trim().toLowerCase();if(!t||"ios-simulator"===t)return"ios-simulator";if("ios-instance"===t||"android-instance"===t)return t;throw new tv("INVALID_ARGS",`Unsupported lease backend: ${e??""}`)}class rJ{leases=new Map;runBindings=new Map;maxActiveSimulatorLeases;defaultLeaseTtlMs;minLeaseTtlMs;maxLeaseTtlMs;now;constructor(e={}){this.maxActiveSimulatorLeases=Number.isInteger(e.maxActiveSimulatorLeases)?Math.max(0,Number(e.maxActiveSimulatorLeases)):0,this.defaultLeaseTtlMs=Number.isInteger(e.defaultLeaseTtlMs)?Math.max(1,Number(e.defaultLeaseTtlMs)):6e4,this.minLeaseTtlMs=Number.isInteger(e.minLeaseTtlMs)?Math.max(1,Number(e.minLeaseTtlMs)):5e3,this.maxLeaseTtlMs=Number.isInteger(e.maxLeaseTtlMs)?Math.max(this.minLeaseTtlMs,Number(e.maxLeaseTtlMs)):6e5,this.now=e.now??(()=>Date.now())}allocateLease(e){let t=rW(e.backend),a=eB(e.tenantId);if(!a)throw new tv("INVALID_ARGS","Invalid tenant id. Use 1-128 chars: letters, numbers, dot, underscore, hyphen.");let i=rK(e.runId);if(!i)throw new tv("INVALID_ARGS","Invalid run id. Use 1-128 chars: letters, numbers, dot, underscore, hyphen.");this.cleanupExpiredLeases();let n=this.resolveLeaseTtlMs(e.ttlMs),o=this.bindingKey(a,i,t),s=this.runBindings.get(o);if(s){let e=this.leases.get(s);if(e)return this.refreshLease(e,n);this.runBindings.delete(o)}this.enforceCapacity(t);let l=this.now(),d={leaseId:r.randomBytes(16).toString("hex"),tenantId:a,runId:i,backend:t,createdAt:l,heartbeatAt:l,expiresAt:l+n};return this.leases.set(d.leaseId,d),this.runBindings.set(o,d.leaseId),{...d}}heartbeatLease(e){let t=rz(e.leaseId);if(!t)throw new tv("INVALID_ARGS","Invalid lease id.");this.cleanupExpiredLeases();let r=this.leases.get(t);if(!r)throw new tv("UNAUTHORIZED","Lease is not active",{reason:"LEASE_NOT_FOUND"});this.assertOptionalScopeMatch(r,e.tenantId,e.runId);let a=this.resolveLeaseTtlMs(e.ttlMs);return this.refreshLease(r,a)}releaseLease(e){let t=rz(e.leaseId);if(!t)throw new tv("INVALID_ARGS","Invalid lease id.");this.cleanupExpiredLeases();let r=this.leases.get(t);return r?(this.assertOptionalScopeMatch(r,e.tenantId,e.runId),this.leases.delete(t),this.runBindings.delete(this.bindingKey(r.tenantId,r.runId,r.backend)),{released:!0}):{released:!1}}assertLeaseAdmission(e){let t=rW(e.backend),r=eB(e.tenantId);if(!r)throw new tv("INVALID_ARGS","tenant isolation requires tenant id.");let a=rK(e.runId);if(!a)throw new tv("INVALID_ARGS","tenant isolation requires run id.");let i=rz(e.leaseId);if(!i)throw new tv("INVALID_ARGS","tenant isolation requires lease id.");this.cleanupExpiredLeases();let n=this.leases.get(i);if(!n)throw new tv("UNAUTHORIZED","Lease is not active",{reason:"LEASE_NOT_FOUND"});if(n.backend!==t||n.tenantId!==r||n.runId!==a)throw new tv("UNAUTHORIZED","Lease does not match tenant/run scope",{reason:"LEASE_SCOPE_MISMATCH"})}listActiveLeases(){return this.cleanupExpiredLeases(),Array.from(this.leases.values()).map(e=>({...e}))}cleanupExpiredLeases(){let e=this.now();for(let t of this.leases.values())t.expiresAt>e||(this.leases.delete(t.leaseId),this.runBindings.delete(this.bindingKey(t.tenantId,t.runId,t.backend)))}enforceCapacity(e){if("ios-simulator"!==e||this.maxActiveSimulatorLeases<=0)return;let t=Array.from(this.leases.values()).filter(e=>"ios-simulator"===e.backend).length;if(!(t<this.maxActiveSimulatorLeases))throw new tv("COMMAND_FAILED","No simulator lease capacity available",{reason:"LEASE_CAPACITY_EXCEEDED",activeLeases:t,maxActiveLeases:this.maxActiveSimulatorLeases,backend:e,hint:"Retry after releasing another simulator lease."})}resolveLeaseTtlMs(e){if(!Number.isInteger(e))return this.defaultLeaseTtlMs;let t=Number(e);if(t<this.minLeaseTtlMs||t>this.maxLeaseTtlMs)throw new tv("INVALID_ARGS",`Lease ttlMs must be between ${this.minLeaseTtlMs} and ${this.maxLeaseTtlMs}.`);return t}refreshLease(e,t){let r=this.now(),a={...e,heartbeatAt:r,expiresAt:r+t};return this.leases.set(a.leaseId,a),this.runBindings.set(this.bindingKey(a.tenantId,a.runId,a.backend),a.leaseId),{...a}}bindingKey(e,t,r){return`${e}:${t}:${r}`}assertOptionalScopeMatch(e,t,r){let a=eB(t),i=rK(r);if(t&&!a)throw new tv("INVALID_ARGS","Invalid tenant id. Use 1-128 chars: letters, numbers, dot, underscore, hyphen.");if(r&&!i)throw new tv("INVALID_ARGS","Invalid run id. Use 1-128 chars: letters, numbers, dot, underscore, hyphen.");if(a&&e.tenantId!==a||i&&e.runId!==i)throw new tv("UNAUTHORIZED","Lease does not match tenant/run scope",{reason:"LEASE_SCOPE_MISMATCH"})}}let rZ=ez(process.env.AGENT_DEVICE_INSTALL_SOURCE_RETAIN_TTL_MS,9e5,5e3),rX=new Map;async function rY(e){let t=await c.mkdtemp(n.join(u.tmpdir(),"agent-device-materialized-"));try{let a=await r1(e.installablePath,n.join(t,"installable")),i=e.archivePath?await r1(e.archivePath,n.join(t,"archive")):void 0,o=r.randomUUID(),s=e.ttlMs??rZ,l=Date.now()+s,d=setTimeout(()=>{rQ(o)},s);return rX.set(o,{rootPath:t,installablePath:a,archivePath:i,tenantId:e.tenantId,sessionName:e.sessionName,expiresAt:l,timer:d}),{materializationId:o,installablePath:a,...i?{archivePath:i}:{},expiresAt:new Date(l).toISOString()}}catch(e){throw await c.rm(t,{recursive:!0,force:!0}),e}}async function rQ(e,t){let r=rX.get(e);if(!r)throw new tv("INVALID_ARGS",`Materialized paths not found: ${e}`);if(r.tenantId&&r.tenantId!==t)throw new tv("UNAUTHORIZED","Materialized paths belong to a different tenant");clearTimeout(r.timer),rX.delete(e),await c.rm(r.rootPath,{recursive:!0,force:!0})}async function r0(e){let t=Array.from(rX.entries()).filter(([,t])=>t.sessionName===e).map(([e])=>e);await Promise.all(t.map(async e=>{await rQ(e)}))}async function r1(e,t){let r=await c.stat(e);await c.mkdir(t,{recursive:!0});let a=n.join(t,n.basename(e));return r.isDirectory()?await c.cp(e,a,{recursive:!0}):await c.copyFile(e,a),a}async function r2(e){var t;let r="ios"===(t=e.flags?.platform)||"android"===t?t:void 0;if(e.session){if(r&&e.session.device.platform!==r)throw new tv("INVALID_ARGS",`install_from_source requested platform ${r}, but session is bound to ${e.session.device.platform}`);return await e8(e.session.device),e.session.device}if(!r)throw new tv("INVALID_ARGS",'install_from_source requires platform "ios" or "android" when no session is provided');let a=await eL(e.flags??{});return await e8(a),a}async function r5(e){let{req:t,sessionName:r,sessionStore:a}=e,i=a.get(r);try{let e,n,o,s=(n=function(e){let t=e.meta?.installSource;if(!t)throw new tv("INVALID_ARGS","install_from_source requires a source payload");if("url"===t.kind){if(!t.url||0===t.url.trim().length)throw new tv("INVALID_ARGS","install_from_source url source requires a non-empty url");return t}if(!t.path||0===t.path.trim().length)throw new tv("INVALID_ARGS","install_from_source path source requires a non-empty path");return t}(t),(o=t.meta?.uploadedArtifactId)&&"path"===n.kind?{source:{kind:"path",path:rS(o,t.meta?.tenantId)},cleanup:()=>{rA(o)}}:{source:n,cleanup:()=>{}}),l=function(e){let t=e.meta?.retainMaterializedPaths===!0,r=e.meta?.materializedPathRetentionMs;if(!t)return{enabled:!1};if(void 0!==r&&r<=0)throw new tv("INVALID_ARGS","install_from_source retentionMs must be a positive integer");return{enabled:!0,ttlMs:r}}(t),d=await r2({session:i,flags:t.flags});if(!to("install",d))return N("UNSUPPORTED_OPERATION","install_from_source is not supported on this device");let u=tp(t.meta?.requestId);if("ios"===d.platform){let e,{installIosInstallablePath:n}=await import("./155.js"),{prepareIosInstallArtifact:o}=await import("./155.js"),c=await o(s.source,{signal:u});try{if(l.enabled&&(e=await rY({archivePath:c.archivePath,installablePath:c.installablePath,tenantId:t.meta?.tenantId,sessionName:i?r:void 0,ttlMs:l.ttlMs})),await n(d,c.installablePath),!c.bundleId)throw new tv("COMMAND_FAILED","Installed iOS app identity could not be resolved from the artifact");let o={...e?.archivePath?{archivePath:e.archivePath}:{},...e?{installablePath:e.installablePath}:{},bundleId:c.bundleId,...c.appName?{appName:c.appName}:{},launchTarget:c.bundleId,...e?{materializationId:e.materializationId,materializationExpiresAt:e.expiresAt}:{}},s=ew(o,r3(o));return i&&a.recordAction(i,{command:"install_source",positionals:[],flags:t.flags??{},result:s}),{ok:!0,data:s}}catch(r){throw e&&await rQ(e.materializationId,t.meta?.tenantId).catch(()=>{}),r}finally{await c.cleanup(),s.cleanup()}}let{prepareAndroidInstallArtifact:c}=await import("./155.js"),{installAndroidInstallablePathAndResolvePackageName:f}=await import("./155.js"),p=await c(s.source,{signal:u});try{l.enabled&&(e=await rY({archivePath:p.archivePath,installablePath:p.installablePath,tenantId:t.meta?.tenantId,sessionName:i?r:void 0,ttlMs:l.ttlMs}));let n=await f(d,p.installablePath,p.packageName);if(!n)throw new tv("COMMAND_FAILED","Installed Android app identity could not be resolved from the artifact or device state");let{inferAndroidAppName:o}=await import("./155.js"),s=o(n),u={...e?.archivePath?{archivePath:e.archivePath}:{},...e?{installablePath:e.installablePath}:{},packageName:n,...s?{appName:s}:{},launchTarget:n,...e?{materializationId:e.materializationId,materializationExpiresAt:e.expiresAt}:{}},c=ew(u,r3(u));return i&&a.recordAction(i,{command:"install_source",positionals:[],flags:t.flags??{},result:c}),{ok:!0,data:c}}catch(r){throw e&&await rQ(e.materializationId,t.meta?.tenantId).catch(()=>{}),r}finally{await p.cleanup(),s.cleanup()}}catch(e){return{ok:!1,error:tw(e)}}}function r3(e){return`Installed: ${Q(e)}`}async function r4(e){let{req:t}=e;try{let e=t.meta?.materializationId?.trim();if(!e)throw new tv("INVALID_ARGS","release_materialized_paths requires a materializationId");return await rQ(e,t.meta?.tenantId),{ok:!0,data:{released:!0,materializationId:e}}}catch(e){return{ok:!1,error:tw(e)}}}let r8=ez(process.env.AGENT_DEVICE_IOS_SIMULATOR_POST_CLOSE_SETTLE_MS,300,0),r6=ez(process.env.AGENT_DEVICE_IOS_SIMULATOR_POST_OPEN_SETTLE_MS,300,0);function r9(e,t,r){return t||r7(r)?null:N("INVALID_ARGS",`${e} requires an active session or an explicit device selector (e.g. --platform ios).`)}function r7(e){return!!(e?.platform||e?.target||e?.device||e?.udid||e?.serial)}function ae(e){return"ios"===e.platform&&"simulator"===e.kind}async function at(e,t){ae(e)&&!(t<=0)&&await new Promise(e=>setTimeout(e,t))}async function ar(e){let t=r7(e.flags)||!e.session?await eL(e.flags??{}):await aa(e.session.device);return!1!==e.ensureReady&&await e8(t),t}async function aa(e){if("ios"!==e.platform||"simulator"!==e.kind||"darwin"!==process.platform)return e;let t={platform:"ios",target:e.target,udid:e.id,...e.simulatorSetPath?{iosSimulatorDeviceSet:e.simulatorSetPath}:{}};try{return await eL(t)}catch(e){if(!(e instanceof tv)||"DEVICE_NOT_FOUND"!==e.code)throw e}return await eL({platform:"ios",target:e.target,device:e.name,...e.simulatorSetPath?{iosSimulatorDeviceSet:e.simulatorSetPath}:{}})}function ai(e){let t=e.flags?.device?.trim();return t||(e.resolvedDevice?.platform==="android"&&"emulator"===e.resolvedDevice.kind?e.resolvedDevice.name:e.sessionDevice?.platform==="android"&&"emulator"===e.sessionDevice.kind?e.sessionDevice.name:void 0)}let an=["platform","metroHost","metroPort","bundleUrl","launchUrl"];function ao(e){return e?[e.metroHost,e.metroPort,e.bundleUrl,e.launchUrl].filter(e=>void 0!==e&&""!==e).length:0}function as(e){let t=e?.trim();return t&&t.length>0?t:void 0}function al(e,t){if(void 0!==e){if("string"!=typeof e)throw new tv("INVALID_ARGS",`Invalid open runtime ${t}: expected string.`);return as(e)}}function ad(e){if(void 0!==e){if(!Number.isInteger(e)||e<1||e>65535)throw new tv("INVALID_ARGS",`Invalid runtime metroPort: ${String(e)}. Use an integer between 1 and 65535.`);return e}}function au(e){if("ios"===e||"android"===e)return e}async function ac(e){let{replacedStoredRuntime:t,previousRuntime:r,runtime:a,session:i}=e;!t||!i?.appBundleId||!ep(r)||ep(a)||await eU({device:i.device,appId:i.appBundleId})}async function af(e){var t,r;let{req:a,sessionName:i,sessionStore:n}=e,o=(a.positionals?.[0]??"show").toLowerCase(),s=n.get(i),l=n.getRuntimeHints(i);if(!["set","show","clear"].includes(o))return N("INVALID_ARGS","runtime requires set, show, or clear");if("clear"===o){ep(l)&&s?.appBundleId&&await eU({device:s.device,appId:s.appBundleId});let e=n.clearRuntimeHints(i);return{ok:!0,data:{session:i,cleared:e}}}if("show"===o)return{ok:!0,data:{session:i,configured:!!l,runtime:l}};let d=au(tu(a.flags?.platform)??l?.platform??s?.device.platform);if(!d)return N("INVALID_ARGS","runtime set only supports iOS and Android sessions. Pass --platform ios|android or open an iOS/Android session first.");if(s&&s.device.platform!==d)return N("INVALID_ARGS",`runtime set targets ${d}, but session "${i}" is already bound to ${s.device.platform}.`);let u={platform:(t=a.flags,r={platform:d,metroHost:as(t?.metroHost),metroPort:ad(t?.metroPort),bundleUrl:as(t?.bundleUrl),launchUrl:as(t?.launchUrl)}).platform??l?.platform,metroHost:r.metroHost??l?.metroHost,metroPort:r.metroPort??l?.metroPort,bundleUrl:r.bundleUrl??l?.bundleUrl,launchUrl:r.launchUrl??l?.launchUrl};return 0===ao(u)?N("INVALID_ARGS","runtime set requires at least one hint such as --metro-host, --metro-port, --bundle-url, or --launch-url."):(n.setRuntimeHints(i,u),{ok:!0,data:{session:i,configured:!0,runtime:u}})}let ap="open-command-roundtrip",am="Not implemented for this platform in this release.",ah=new Set(["app","desktop","frontmost-app"]);async function ag(e){if("app"===e||"desktop"===e||"menubar"===e)return{};let t=await ta();return{appBundleId:t.bundleId,appName:t.appName}}async function aw(e,t,r){if(("ios"===e.platform||"macos"===e.platform)&&t)return eF(t)?"macos"===e.platform?void 0:"device"===e.kind?e5(r,t):void 0:await av(e,t)}async function av(e,t){try{let{resolveIosApp:r}=await import("./155.js");return await r(e,t)}catch{return}}async function ay(e,t){if(!("android"!==e.platform||!t||eF(t)))try{let{resolveAndroidApp:r}=await import("./155.js"),a=await r(e,t);return"package"===a.type?a.value:void 0}catch{return}}async function aI(e,t,r,a){return await aw(e,t,r)??await a(e,t)??("android"===e.platform&&t&&eF(t)?r:void 0)}function aS(e){return N("INVALID_ARGS",e)}function aA(e,t,r,a){try{return function(e){let{device:t,surfaceFlag:r,openTarget:a,existingSurface:i}=e;if(("macos"===t.platform||"linux"===t.platform)&&!r)return i??"app";if("linux"===t.platform){if(!r)return"app";let e=ed(r);if(!ah.has(e))throw new tv("INVALID_ARGS",`Linux supports --surface app, desktop, and frontmost-app (got "${r}")`);if("app"!==e&&a)throw new tv("INVALID_ARGS",`open --surface ${e} does not accept an app target`);return e}if("macos"!==t.platform){if(r)throw new tv("INVALID_ARGS","surface is only supported on macOS and Linux");return"app"}let n=r?ed(r):"app";if("app"!==n&&"menubar"!==n&&a)throw new tv("INVALID_ARGS",`open --surface ${n} does not accept an app target`);return n}({device:e,surfaceFlag:t,openTarget:r,existingSurface:a})}catch(e){return N(e instanceof tv?e.code:"INVALID_ARGS",String(e.message))}}function ab(e){let{shouldRelaunch:t,openTarget:r,surface:a,device:i}=e;return t?r&&eF(r)?aS("open --relaunch does not support URL targets."):"app"!==a?aS("open --relaunch is supported only for app surfaces."):"android"===i.platform&&r&&"binary"===eE(r)?aS(q(r)):null:null}async function a_(e){let{req:t,sessionName:r,sessionStore:a,device:i,surface:n,openTarget:o,existingSession:s}=e;await e8(i);let{appBundleId:l,appName:d}=await aN({device:i,surface:n,openTarget:o,existingAppBundleId:s?.appBundleId}),u=function(e){try{return{ok:!0,data:function(e){let{req:t,sessionStore:r,sessionName:a,device:i}=e,n=r.getRuntimeHints(a),o=function(e){let{runtime:t,sessionName:r,platform:a}=e;if(void 0===t)return;if(!t||"object"!=typeof t||Array.isArray(t))throw new tv("INVALID_ARGS","open runtime must be an object.");let i=Object.keys(t).find(e=>!an.includes(e));if(i)throw new tv("INVALID_ARGS",`Invalid open runtime field: ${i}. Supported fields are ${an.join(", ")}.`);return{platform:function(e,t,r){if(void 0===e)return r;if("ios"!==e&&"android"!==e)throw new tv("INVALID_ARGS",`Invalid open runtime platform: ${String(e)}. Use "ios" or "android".`);if(r&&e!==r)throw new tv("INVALID_ARGS",`open runtime targets ${e}, but session "${t}" is bound to ${r}.`);return e}(t.platform,r,a),metroHost:al(t.metroHost,"metroHost"),metroPort:function(e){if(void 0!==e){if("number"!=typeof e)throw new tv("INVALID_ARGS","Invalid open runtime metroPort: expected integer.");return ad(e)}}(t.metroPort),bundleUrl:al(t.bundleUrl,"bundleUrl"),launchUrl:al(t.launchUrl,"launchUrl")}}({runtime:t.runtime,sessionName:a,platform:au(i.platform)});return void 0===t.runtime?{runtime:function(e,t,r){let a=e.getRuntimeHints(t);if(!a)return;let i=r?.platform,n=au(i);if(a.platform&&r&&!n)throw new tv("INVALID_ARGS",`Session runtime hints are only supported on iOS and Android sessions, but session "${t}" is bound to ${i}.`);if(a.platform&&n&&a.platform!==n)throw new tv("INVALID_ARGS",`Session runtime hints target ${a.platform}, but session "${t}" is bound to ${i}. Clear the runtime hints or use a different session.`);return n&&a.platform!==n?{...a,platform:n}:a}(r,a,i),previousRuntime:n,replacedStoredRuntime:!1}:{runtime:o&&ao(o)>0?o:void 0,previousRuntime:n,replacedStoredRuntime:!0}}(e)}}catch(t){let e=tg(t);return N(e.code,e.message,e.details)}}({req:t,sessionStore:a,sessionName:r,device:i});if(!u.ok)return{type:"response",response:u};if(s){let{runtime:e,previousRuntime:t,replacedStoredRuntime:r}=u.data;await ac({replacedStoredRuntime:r,previousRuntime:t,runtime:e,session:s})}return{type:"details",details:{appBundleId:l,appName:d,runtime:u.data.runtime}}}async function aN(e){let{device:t,surface:r,openTarget:a,existingAppBundleId:i}=e,n=await ag(r);return{appBundleId:n.appBundleId??await aI(t,a,i,ay),appName:n.appName??a}}let ax=new Map;async function aD(e){let{device:t,closeTarget:r,outFlag:a,context:i}=e;"android"!==t.platform&&await ey(t.id),await w(t,"close",[r],a,i),await at(t,r8)}async function aM(e){let{runtime:t,device:r,req:a,logPath:i,appBundleId:n,traceLogPath:o,openPositionals:s}=e,l=t?.launchUrl;if(!l||0===s.length||s.length>1)return;let d=s[0]?.trim();!d||eF(d)||await w(r,"open",[l],a.flags?.out,{...eG(i,a.flags,n,o)})}async function ak(e){var t,r,a;let{req:i,sessionName:n,sessionStore:o,logPath:s,device:l,openTarget:d,openPositionals:u,appName:c,surface:f,appBundleId:p,runtime:m,existingSession:h}=e,g=i.flags?.relaunch===!0,v=h?.trace?.outPath;if(g&&d){let e=p??d;await aD({device:l,closeTarget:e,outFlag:i.flags?.out,context:{...eG(s,i.flags,p??h?.appBundleId,v)}})}await eP({device:l,appId:p,runtime:m});let y=Date.now();await w(l,"open",u,i.flags?.out,{...eG(s,i.flags,p)}),await aM({runtime:m,device:l,req:i,logPath:s,appBundleId:p,traceLogPath:v,openPositionals:u});let I=d?{durationMs:Math.max(0,Date.now()-y),measuredAt:new Date().toISOString(),method:ap,appTarget:d,appBundleId:p}:void 0;if(await at(l,r6),F(i.meta?.requestId)){let e=eS();return N(e.code,e.message,e.details)}h&&tf(h,"open",h.snapshot);let S=function(e){let{existingSession:t,sessionName:r,device:a,surface:i,appBundleId:n,appName:o,saveScript:s}=e;return t?{...t,device:a,surface:i,appBundleId:n,appName:o,recordSession:t.recordSession||s,snapshot:void 0}:{name:r,device:a,createdAt:Date.now(),surface:i,appBundleId:n,appName:o,recordSession:s,actions:[]}}({existingSession:h,sessionName:n,device:l,surface:f,appBundleId:p,appName:c,saveScript:!!i.flags?.saveScript});void 0!==i.runtime&&(t=o,r=n,(a=m)&&(0===ao(a)?t.clearRuntimeHints(r):t.setRuntimeHints(r,a)));let A=function(e){let{sessionName:t,appName:r,appBundleId:a,surface:i,startup:n,device:o,runtime:s,runtimeHintCount:l}=e,d={session:t,surface:i};return r&&(d.appName=r),a&&(d.appBundleId=a),n&&(d.startup=n),s&&l(s)>0&&(d.runtime=s),o&&(d.platform=o.platform,d.target=o.target??"mobile",d.device=o.name,d.id=o.id,d.kind=o.kind,"android"===o.platform&&(d.serial=o.id)),o?.platform==="ios"&&(d.device_udid=o.id,d.ios_simulator_device_set=o.simulatorSetPath??null),{...d,...D(`Opened: ${r??a??t}`)}}({sessionName:n,appName:c,appBundleId:p,surface:f,startup:I,device:l,runtime:m,runtimeHintCount:ao});return o.recordAction(S,{command:"open",positionals:u,flags:i.flags??{},runtime:void 0!==i.runtime?m:void 0,result:A}),o.set(n,S),{ok:!0,data:A}}async function aP(e){let{req:t,sessionName:r,logPath:a,sessionStore:i}=e;if(i.has(r)){let e=i.get(r);if(!e)return N("SESSION_NOT_FOUND",`Session "${r}" not found.`);let n=t.flags?.relaunch===!0,o=t.positionals?.[0],s=o??(n?e.appName:void 0),l=aA(e.device,t.flags?.surface,s,e.surface);if("string"!=typeof l)return l;if(!s&&"app"===l)return n?aS("open --relaunch requires an app name or an active session app."):aS("Session already active. Close it first or pass a new --session name.");let d=ab({shouldRelaunch:n,openTarget:s,surface:l,device:e.device});if(d)return d;let u=await aa(e.device),c=await a_({req:t,sessionName:r,sessionStore:i,device:u,surface:l,openTarget:s,existingSession:e});return"response"===c.type?c.response:await ak({req:t,sessionName:r,sessionStore:i,logPath:a,device:u,openTarget:s,openPositionals:o?t.positionals??[]:s?[s]:[],appBundleId:c.details.appBundleId,appName:c.details.appName,runtime:c.details.runtime,surface:l,existingSession:e})}let n=t.flags?.relaunch===!0,o=t.positionals?.[0];if(n&&!o)return aS("open --relaunch requires an app argument.");let s=function(e){let{shouldRelaunch:t,openTarget:r,platform:a}=e;return t?r&&eF(r)?aS("open --relaunch does not support URL targets."):"android"===a&&r&&"binary"===eE(r)?aS(q(r)):null:null}({shouldRelaunch:n,openTarget:o,platform:t.flags?.platform==="android"?"android":void 0});if(s)return s;let l=await eL(t.flags??{}),d=aA(l,t.flags?.surface,o);if("string"!=typeof d)return d;let u=ab({shouldRelaunch:n,openTarget:o,surface:d,device:l});return u||await k(ax,l.id,async()=>{let e=i.toArray().find(e=>e.device.id===l.id);if(e)return N("DEVICE_IN_USE",`Device is already in use by session "${e.name}".`,{session:e.name,deviceId:l.id,deviceName:l.name});let n=await a_({req:t,sessionName:r,sessionStore:i,device:l,surface:d,openTarget:o});return"response"===n.type?n.response:await ak({req:t,sessionName:r,sessionStore:i,logPath:a,device:l,openTarget:o,openPositionals:t.positionals??[],appBundleId:n.details.appBundleId,appName:n.details.appName,runtime:n.details.runtime,surface:d})})}async function aL(e){let t=await th("adb",["-s",e.id,"emu","kill"],{allowFailure:!0,timeoutMs:15e3});return{success:0===t.exitCode,exitCode:t.exitCode,stdout:String(t.stdout??""),stderr:String(t.stderr??"")}}async function aR(e){let{device:t,shutdownRequested:r}=e;if(r&&(ae(t)||"android"===t.platform&&"emulator"===t.kind))try{return ae(t)?await ev(t):await aL(t)}catch(t){let e=tw(t);return{success:!1,exitCode:-1,stdout:"",stderr:e.message,error:e}}}async function aO(e){if(await ey(e.device.id),"macos"!==e.device.platform)return;let t="frontmost-app"===e.surface?{surface:"frontmost-app"}:e.appBundleId?{bundleId:e.appBundleId}:{};await eT("dismiss",t).catch(t=>{tr({level:"debug",phase:"macos_close_alert_dismiss_failed",data:{session:e.name,error:t instanceof Error?t.message:String(t)}})})}async function aC(e){let{req:t,sessionName:r,logPath:a,sessionStore:i}=e,n=i.get(r);if(!n)return N("SESSION_NOT_FOUND","No active session");n.appLog&&await rh(n.appLog),t.positionals&&t.positionals.length>0&&(("ios"===n.device.platform||"macos"===n.device.platform)&&await aO(n),await w(n.device,"close",t.positionals,t.flags?.out,{...eG(a,t.flags,n.appBundleId,n.trace?.outPath)}),await at(n.device,r8)),("ios"===n.device.platform||"macos"===n.device.platform)&&await aO(n),ep(i.getRuntimeHints(r))&&n.appBundleId&&await eU({device:n.device,appId:n.appBundleId}).catch(()=>{}),i.recordAction(n,{command:"close",positionals:t.positionals??[],flags:t.flags??{},result:{session:r,...D(`Closed: ${r}`)}}),t.flags?.saveScript&&(n.recordSession=!0),i.writeSessionLog(n),await r0(r).catch(()=>{}),i.delete(r);let o=await aR({device:n.device,shutdownRequested:t.flags?.shutdown});return o?{ok:!0,data:ew({session:r,shutdown:o},`Closed: ${r}`)}:{ok:!0,data:{session:r,...D(`Closed: ${r}`)}}}let aE=["platform","target","device","udid","serial","verbose","out"];function a$(e,t){let r=e??{};for(let e of aE)void 0===t[e]&&void 0!==r[e]&&(t[e]=r[e]);return t}let aT={ios:async(e,t,r)=>{let{reinstallIosApp:a}=await import("./155.js");return await a(e,t,r)},android:async(e,t,r)=>{let{reinstallAndroidApp:a}=await import("./155.js");return await a(e,t,r)}},aF={ios:async(e,t,r)=>{let{installIosApp:a}=await import("./155.js"),i=await a(e,r,{appIdentifierHint:t});return{bundleId:i.bundleId,appName:i.appName,launchTarget:i.launchTarget}},android:async(e,t,r)=>{let{installAndroidApp:a}=await import("./155.js"),i=await a(e,r);return{package:i.packageName,appName:i.appName,launchTarget:i.launchTarget}}};async function aU(e){let{req:t,command:r,sessionName:i,sessionStore:n,deployOps:o}=e,s=n.get(i),l=t.flags??{},d=r9(r,s,l);if(d)return d;let u=t.positionals?.[0]?.trim(),c=t.positionals?.[1]?.trim();if(!u||!c)return N("INVALID_ARGS",`${r} requires: ${r} <app> <path-to-app-binary>`);let f=t.meta?.uploadedArtifactId;try{var p;let e,i=f?rS(f,t.meta?.tenantId):tF.expandHome(c);if(!a.existsSync(i))return N("INVALID_ARGS",`App binary not found: ${i}`);let d=await ar({session:s,flags:l,ensureReady:!1});if(!to(r,d))return N("UNSUPPORTED_OPERATION",`${r} is not supported on this device`);if("ios"===d.platform){let t=await o.ios(d,u,i),r=t.bundleId;e=r?{app:u,appPath:i,platform:"ios",appId:r,bundleId:r,appName:t.appName,launchTarget:t.launchTarget}:{app:u,appPath:i,platform:"ios",appName:t.appName,launchTarget:t.launchTarget}}else{let t=await o.android(d,u,i),r=t.package;e=r?{app:u,appPath:i,platform:"android",appId:r,package:r,packageName:r,appName:t.appName,launchTarget:t.launchTarget}:{app:u,appPath:i,platform:"android",appName:t.appName,launchTarget:t.launchTarget}}let m=ew(e,(p=e,`Installed: ${p.appName??em(p)}`));return s&&n.recordAction(s,{command:r,positionals:t.positionals??[],flags:t.flags??{},result:m??{}}),{ok:!0,data:m}}finally{f&&rA(f)}}async function aG(e,t,r){let a=e.flags?.batchOnError??"stop";if("stop"!==a)return N("INVALID_ARGS",`Unsupported batch on-error mode: ${a}.`);let i=e.flags?.batchMaxSteps??P;if(!Number.isInteger(i)||i<1||i>1e3)return N("INVALID_ARGS",`Invalid batch max-steps: ${String(e.flags?.batchMaxSteps)}`);try{let a=eu(e.flags?.batchSteps,i),n=Date.now(),o=[];for(let i=0;i<a.length;i+=1){let n=a[i],s=await aV(e,t,n,r,i+1);if(!s.ok)return{ok:!1,error:{code:s.error.code,message:`Batch failed at step ${s.step} (${n.command}): ${s.error.message}`,hint:s.error.hint,diagnosticId:s.error.diagnosticId,logPath:s.error.logPath,details:{...s.error.details??{},step:s.step,command:n.command,positionals:n.positionals,executed:i,total:a.length,partialResults:o}}};o.push(s.result)}return{ok:!0,data:{total:a.length,executed:a.length,totalDurationMs:Date.now()-n,results:o}}}catch(t){let e=tg(t);return N(e.code,e.message,e.details)}}async function aV(e,t,r,a,i){let n=Date.now(),o=function(e,t){let{batchSteps:r,batchOnError:a,batchMaxSteps:i,...n}=t??{};return a$(e,n)}(e.flags,r.flags);void 0===o.session&&(o.session=t);let s=await a({token:e.token,session:t,command:r.command,positionals:r.positionals,flags:o,runtime:r.runtime,meta:e.meta}),l=Date.now()-n;return s.ok?{ok:!0,step:i,result:{step:i,command:r.command,ok:!0,data:s.data??{},durationMs:l}}:{ok:!1,step:i,error:s.error}}async function aq(e){let t,r,a,{deviceName:i,runtime:n,simulatorSetPath:o,reuseExisting:s,boot:l,ensureReady:d}=e;if("darwin"!==process.platform)throw new tv("UNSUPPORTED_PLATFORM","ensure-simulator is only available on macOS");let u={simulatorSetPath:o??void 0};if(s){let e=await aj({deviceName:i,runtime:n,simctlOpts:u});e?(t=e.udid,r=e.runtime,a=!1):(t=(await aH({deviceName:i,runtime:n,simctlOpts:u})).udid,r=await aB(t,u),a=!0)}else t=(await aH({deviceName:i,runtime:n,simctlOpts:u})).udid,r=await aB(t,u),a=!0;let c=!1;if(l){let e={platform:"ios",id:t,name:i,kind:"simulator",target:"mobile",...o?{simulatorSetPath:o}:{}};await d(e),c=!0}return{udid:t,device:i,runtime:r,created:a,booted:c}}async function aj(e){let{deviceName:t,runtime:r,simctlOpts:a}=e,i=await th("xcrun",g(["list","devices","-j"],a),{allowFailure:!0,timeoutMs:W});if(0!==i.exitCode)return null;try{let e=JSON.parse(String(i.stdout??""));for(let[a,i]of Object.entries(e.devices??{}))if(!r||aK(a).includes(aK(r))){for(let e of i)if(e.isAvailable&&e.name.toLowerCase()===t.toLowerCase())return{udid:e.udid,runtime:a}}return null}catch{return null}}async function aH(e){let{deviceName:t,runtime:r,simctlOpts:a}=e,i=r?["create",t,t,r]:["create",t,t],n=await th("xcrun",g(i,a),{allowFailure:!0});if(0!==n.exitCode)throw new tv("COMMAND_FAILED","Failed to create iOS simulator",{deviceName:t,runtime:r,stdout:String(n.stdout??""),stderr:String(n.stderr??""),exitCode:n.exitCode,hint:"Ensure the device type and runtime identifiers are valid. Run `xcrun simctl list devicetypes` and `xcrun simctl list runtimes` to see available options."});let o=String(n.stdout??"").trim();if(!o)throw new tv("COMMAND_FAILED","simctl create returned no UDID",{deviceName:t,runtime:r,stdout:String(n.stdout??""),stderr:String(n.stderr??"")});return{udid:o}}async function aB(e,t){let r=await th("xcrun",g(["list","devices","-j"],t),{allowFailure:!0,timeoutMs:W});if(0!==r.exitCode)return"";try{let t=JSON.parse(String(r.stdout??""));for(let[r,a]of Object.entries(t.devices??{}))if(a.some(t=>t.udid===e))return r;return""}catch{return""}}function aK(e){return e.toLowerCase().replace(/[._-]/g,"")}async function az(e){let{req:t,sessionName:r,sessionStore:a}=e;if("session_list"===t.command)return{ok:!0,data:{sessions:a.toArray().map(e=>({name:e.name,platform:e.device.platform,target:e.device.target??"mobile",surface:e.surface??"app",device:e.device.name,id:e.device.id,device_id:e.device.id,createdAt:e.createdAt,..."ios"===e.device.platform&&{device_udid:e.device.id,ios_simulator_device_set:e.device.simulatorSetPath??null}}))}};if("ensure-simulator"===t.command)try{let e=t.flags??{},r=e.device,a=e.runtime,i=e1(e.iosSimulatorDeviceSet);if(!r)return N("INVALID_ARGS","ensure-simulator requires --device <name>");let n=await aq({deviceName:r,runtime:a,simulatorSetPath:i,reuseExisting:!1!==e.reuseExisting,boot:!0===e.boot,ensureReady:e8});return{ok:!0,data:{udid:n.udid,device:n.device,runtime:n.runtime,ios_simulator_device_set:i??null,created:n.created,booted:n.booted}}}catch(t){let e=tg(t);return N(e.code,e.message,e.details)}if("devices"===t.command)try{let e=[],r=et(t.flags?.androidDeviceAllowlist),a=tu(t.flags?.platform),i=h({simulatorSetPath:e1(t.flags?.iosSimulatorDeviceSet),platform:a,target:t.flags?.target});if("android"===a){let{listAndroidDevices:t}=await import("./155.js");e.push(...await t({serialAllowlist:r}))}else if("ios"===a||"macos"===a){let{listAppleDevices:t}=await import("./155.js");e.push(...await t({simulatorSetPath:i}))}else{if("apple"!==a){let{listAndroidDevices:t}=await import("./155.js");try{e.push(...await t({serialAllowlist:r}))}catch{}}let{listAppleDevices:t}=await import("./155.js");try{e.push(...await t({simulatorSetPath:i}))}catch{}}let n="ios"===a||"macos"===a?e.filter(e=>e.platform===a):e,o=(t.flags?.target?n.filter(e=>(e.target??"mobile")===t.flags?.target):n).map(({simulatorSetPath:e,...t})=>t);return{ok:!0,data:{devices:o}}}catch(t){let e=tg(t);return N(e.code,e.message,e.details)}if("apps"===t.command){let e=a.get(r),i=t.flags??{},n=r9(t.command,e,i);if(n)return n;let o=await ar({session:e,flags:i,ensureReady:!0});if(!to("apps",o))return N("UNSUPPORTED_OPERATION","apps is not supported on this device");let s=t.flags?.appsFilter??"all";if(Y(o.platform)){let{listIosApps:e}=await import("./155.js");return{ok:!0,data:{apps:(await e(o,s)).map(e=>e.name&&e.name!==e.bundleId?`${e.name} (${e.bundleId})`:e.bundleId)}}}let{listAndroidApps:l}=await import("./155.js");return{ok:!0,data:{apps:(await l(o,s)).map(e=>e.name&&e.name!==e.package?`${e.name} (${e.package})`:e.package)}}}return null}async function aW(e){let{ensureAndroidEmulatorBooted:t}=await import("./155.js");return await t(e)}let aJ='iOS appstate requires an active session on the target device. Run open first (for example: open --session sim --platform ios --device "<name>" <app>).',aZ='macOS appstate requires an active session on the target device. Run open first (for example: open --session macos --platform macos "System Settings").';async function aX(e){let{req:t,sessionName:r,sessionStore:a}=e,i=a.get(r),n=t.flags??{},o=tu(n.platform);if(!i&&"string"==typeof n?.session&&n.session.trim().length>0)return N("SESSION_NOT_FOUND","ios"===o?`No active session "${r}". Run open with --session ${r} first.`:`No active session "${r}". Run open with --session ${r} first, or omit --session to query by device selector.`);let s=r9("appstate",i,n);if(s)return s;let l=(i?.device.platform==="ios"||i?.device.platform==="macos")&&function(e,t){if(!t)return!1;if(!r7(e))return!0;let r=tu(e?.platform);return!(r&&!tn(t.device.platform,r)||e?.target&&e.target!==(t.device.target??"mobile")||e?.udid&&e.udid!==t.device.id||e?.serial&&e.serial!==t.device.id)&&(!e?.device||e.device.trim().toLowerCase()===t.device.name.trim().toLowerCase())}(n,i);if("ios"===o&&!l)return N("SESSION_NOT_FOUND",aJ);if("macos"===o&&!l)return N("SESSION_NOT_FOUND",aZ);if(l&&i){let e=i.appName??i.appBundleId;if(!i.appName&&!i.appBundleId){if("macos"===i.device.platform&&i.surface&&"app"!==i.surface&&"frontmost-app"!==i.surface)return{ok:!0,data:{platform:i.device.platform,appName:i.surface,appBundleId:i.appBundleId,source:"session",surface:i.surface}};let e="macos"===i.device.platform?"macOS":"iOS";return N("COMMAND_FAILED",`No foreground app is tracked for this ${e} session. Open an app in the session, then retry appstate.`)}return{ok:!0,data:{platform:i.device.platform,appName:e??"unknown",appBundleId:i.appBundleId,source:"session",surface:i.surface??"app",..."ios"===i.device.platform?{device_udid:i.device.id,ios_simulator_device_set:i.device.simulatorSetPath??null}:{}}}}let d=await ar({session:i,flags:n,ensureReady:!0});if("ios"===d.platform)return N("SESSION_NOT_FOUND",aJ);if("macos"===d.platform)return N("SESSION_NOT_FOUND",aZ);let{getAndroidAppState:u}=await import("./155.js"),c=await u(d);return{ok:!0,data:{platform:"android",package:c.package,activity:c.activity}}}async function aY(e){let{req:t,sessionName:r,sessionStore:a}=e;if("boot"===t.command){let e,i=a.get(r),n=t.flags??{},o=r9(t.command,i,n);if(o)return o;let s="android"===(tu(n.platform)??i?.device.platform),l=!0===n.headless;if(l&&!s)return N("INVALID_ARGS","boot --headless is supported only for Android emulators.");let d=ai({flags:n,sessionDevice:i?.device}),u=s&&!!d,c=!1;try{e=await ar({session:i,flags:n,ensureReady:!1})}catch(r){let t=tg(r);if(s&&l&&!d&&"DEVICE_NOT_FOUND"===t.code)return N("INVALID_ARGS","boot --headless requires --device <avd-name> (or an Android emulator session target).");if(!u||"DEVICE_NOT_FOUND"!==t.code||!d)throw r;e=await aW({avdName:d,serial:n.serial,headless:l}),c=!0}if(n.target&&(e.target??"mobile")!==n.target)return N("DEVICE_NOT_FOUND",`No ${e.platform} device found matching --target ${n.target}.`);if(s&&l){if("android"!==e.platform||"emulator"!==e.kind)return N("INVALID_ARGS","boot --headless is supported only for Android emulators.");if(!c){let t=ai({flags:n,sessionDevice:i?.device,resolvedDevice:e});if(!t)return N("INVALID_ARGS","boot --headless requires --device <avd-name> (or an Android emulator session target).");e=await aW({avdName:t,serial:n.serial,headless:!0})}await e8(e)}else("android"!==e.platform||!0!==e.booted)&&await e8(e);return to("boot",e)?{ok:!0,data:{platform:e.platform,target:e.target??"mobile",device:e.name,id:e.id,kind:e.kind,booted:!0}}:N("UNSUPPORTED_OPERATION","boot is not supported on this device")}return"appstate"===t.command?await aX({req:t,sessionName:r,sessionStore:a}):null}function aQ(e){return Math.round(10*e)/10}let a0="adb-shell-dumpsys-cpuinfo",a1="adb-shell-dumpsys-meminfo";async function a2(e,t){try{let r=await th("adb",eX(e,["shell","dumpsys","cpuinfo"]),{timeoutMs:15e3});return function(e,t,r){let a=new Set,i=0;for(let r of e.split("\n")){var n,o;let e=r.trim();if(0===e.length)continue;let s=e.match(/^([0-9]+(?:\.[0-9]+)?)%\s+\d+\/([^\s]+):\s/);if(!s)continue;let l=Number(s[1]),d=s[2];Number.isFinite(l)&&(n=d,o=t,n===o||n.startsWith(`${o}:`))&&(i+=l,a.add(d))}return{usagePercent:aQ(i),measuredAt:r,method:a0,matchedProcesses:[...a]}}(r.stdout,t,new Date().toISOString())}catch(e){throw a3("cpu",t,e)}}async function a5(e,t){try{let r=await th("adb",eX(e,["shell","dumpsys","meminfo",t]),{timeoutMs:15e3});return function(e,t,r){if(/no process found for:/i.test(e))throw new tv("COMMAND_FAILED",`Android meminfo did not find a running process for ${t}`,{metric:"memory",package:t,hint:"Run open <app> for this session again to ensure the Android app is active, then retry perf."});let a=a4(e,"TOTAL PSS")??function(e){for(let t of e.split("\n")){let e=t.trim();if(!/^TOTAL\b(?!\s+PSS:)/.test(e))continue;let r=e.split(/\s+/).slice(1).find(e=>null!==a8(e));if(!r)break;return a8(r)??void 0}}(e);if(void 0===a)throw new tv("COMMAND_FAILED",`Failed to parse Android meminfo output for ${t}`,{metric:"memory",package:t,hint:"Retry perf after reopening the app session. If the problem persists, capture adb shell dumpsys meminfo output for debugging."});return{totalPssKb:a,totalRssKb:a4(e,"TOTAL RSS"),measuredAt:r,method:a1}}(r.stdout,t,new Date().toISOString())}catch(e){throw a3("memory",t,e)}}function a3(e,t,r){return r instanceof tv&&("TOOL_MISSING"===r.code||"COMMAND_FAILED"===r.code)?new tv(r.code,r.message,{...r.details??{},metric:e,package:t},r):r instanceof tv?r:new tv("COMMAND_FAILED",`Failed to sample Android ${e} for ${t}`,{metric:e,package:t},r)}function a4(e,t){let r=t.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),a=e.match(RegExp(`${r}:\\s*([0-9][0-9,]*)`,"i"));if(a)return a8(a[1])??void 0}function a8(e){let t=e.replaceAll(",","").match(/^-?\d+(?:\.\d+)?/);if(!t)return null;let r=Number(t[0]);return Number.isFinite(r)?r:null}let a6="ps-process-snapshot",a9="ps-process-snapshot",a7="xctrace-activity-monitor",ie="xctrace-activity-monitor";async function it(e,t){if("ios"===e.platform&&"device"===e.kind)return await ii(e,t);let r=await ia(e,t),a=await ic(e,r);if(0===a.length)throw new tv("COMMAND_FAILED",`No running process found for ${t}`,{appBundleId:t,hint:"Run open <app> for this session again to ensure the Apple app is active, then retry perf."});let i=new Date().toISOString(),o=M(a.map(e=>n.basename(ip(e.command))));return im({usagePercent:a.reduce((e,t)=>e+t.cpuPercent,0),residentMemoryKb:a.reduce((e,t)=>e+t.rssKb,0),measuredAt:i,matchedProcesses:o,cpuMethod:a6,memoryMethod:a9})}async function ir(e){let t=K(e),r=ih(t,e=>"schema"===e.name&&"activity-monitor-process-live"===e.attributes.name);if(!r)throw new tv("COMMAND_FAILED","Failed to parse xctrace activity-monitor-process-live schema");let a=r.children.filter(e=>"col"===e.name).map(e=>{let t;return t=e.children.find(e=>"mnemonic"===e.name),t?.text??null??""}),i=a.indexOf("pid"),n=a.indexOf("process"),o=a.indexOf("cpu-total"),s=a.indexOf("memory-real");if(i<0||n<0||o<0||s<0)throw new tv("COMMAND_FAILED","xctrace activity-monitor-process-live export is missing expected columns");let l=function e(t,r){let a=[];for(let i of t)r(i)&&a.push(i),a.push(...e(i.children,r));return a}(t,e=>"row"===e.name),d=[],u=new Map;for(let e of l){var c,f;let t=e.children;if(0===t.length)continue;for(let e of t){let t=ih(e.children,e=>"pid"===e.name&&"string"==typeof e.attributes.id);if(t?.attributes.id){let e=Number(t.text);u.set(t.attributes.id,{numberValue:Number.isFinite(e)?e:null})}e.attributes.id&&u.set(e.attributes.id,{numberValue:ig(e),processName:iv(e)})}let r=iw(t[i],u),a=(c=t[n],f=u,c?c.attributes.ref?f.get(c.attributes.ref)?.processName??null:iv(c):null);null!==r&&Number.isFinite(r)&&a&&d.push({pid:r,processName:a,cpuTimeNs:iw(t[o],u),residentMemoryBytes:iw(t[s],u)})}return d}async function ia(e,t){let r="macos"===e.platform?await id(t):await iu(e,t),a="macos"===e.platform?n.join(r,"Contents","Info.plist"):n.join(r,"Info.plist"),i=await e9(a,"CFBundleExecutable");if(!i)throw new tv("COMMAND_FAILED",`Failed to resolve executable for ${t}`,{appBundleId:t,appPath:r});return{executableName:i,executablePath:"macos"===e.platform?n.join(r,"Contents","MacOS",i):void 0}}async function ii(e,t){let r=await io(e,t),a=await is(e,t),i=await is(e,t),n=il(await ir(a.xml),r,t,e),o=il(await ir(i.xml),r,t,e),s=i.capturedAtMs-a.capturedAtMs;if(s<=0)throw new tv("COMMAND_FAILED",`Invalid Activity Monitor sample window for ${t}`,{appBundleId:t,deviceId:e.id});if(null===n.cpuTimeNs||null===o.cpuTimeNs||null===o.residentMemoryBytes)throw new tv("COMMAND_FAILED",`Incomplete Activity Monitor sample for ${t}`,{appBundleId:t,deviceId:e.id,hint:"Keep the app running in the foreground while perf samples the device, then retry."});return im({usagePercent:Math.max(0,o.cpuTimeNs-n.cpuTimeNs)/(1e6*s)*100,residentMemoryKb:o.residentMemoryBytes/1024,measuredAt:new Date(i.capturedAtMs).toISOString(),matchedProcesses:o.matchedProcesses,cpuMethod:a7,memoryMethod:ie})}async function io(e,t){let r=(await J(e,"all")).find(e=>e.bundleId===t);if(!r)throw new tv("APP_NOT_INSTALLED",`No iOS device app found for ${t}`,{appBundleId:t,deviceId:e.id});if(!r.url)throw new tv("COMMAND_FAILED",`Missing app bundle URL for ${t}`,{appBundleId:t,deviceId:e.id});let a=r.url.replace(/\/$/,""),i=l(a),n=(await ei(e)).filter(e=>e.executable.startsWith(`${a}/`));if(0===n.length)throw new tv("COMMAND_FAILED",`No running process found for ${t}`,{appBundleId:t,deviceId:e.id,appBundlePath:i,hint:"Run open <app> for this session again to ensure the iOS app is active, then retry perf."});return n}async function is(e,t){let r=await i.mkdtemp(n.join(u.tmpdir(),"agent-device-ios-perf-")),a=n.join(r,"sample.trace"),o=n.join(r,"activity-monitor-process-live.xml");try{let r=["xctrace","record","--template","Activity Monitor","--device",e.id,"--all-processes","--time-limit","1s","--output",a,"--quiet","--no-prompt"],n=await th("xcrun",r,{allowFailure:!0,timeoutMs:6e4}),s=Date.now();if(0!==n.exitCode)throw new tv("COMMAND_FAILED",`Failed to record iOS device Activity Monitor sample for ${t}`,{cmd:"xcrun",args:r,exitCode:n.exitCode,stdout:n.stdout,stderr:n.stderr,appBundleId:t,deviceId:e.id,hint:iy(n.stdout,n.stderr)});let l=["xctrace","export","--input",a,"--xpath",'/trace-toc/run/data/table[@schema="activity-monitor-process-live"]',"--output",o],d=await th("xcrun",l,{allowFailure:!0,timeoutMs:15e3});if(0!==d.exitCode)throw new tv("COMMAND_FAILED",`Failed to export iOS device perf sample for ${t}`,{cmd:"xcrun",args:l,exitCode:d.exitCode,stdout:d.stdout,stderr:d.stderr,appBundleId:t,deviceId:e.id,hint:iy(d.stdout,d.stderr)});return{capturedAtMs:s,xml:await i.readFile(o,"utf8")}}finally{await i.rm(r,{recursive:!0,force:!0}).catch(()=>{})}}function il(e,t,r,a){let i=new Set(t.map(e=>e.pid)),o=new Set(t.map(e=>n.basename(l(e.executable)))),s=e.filter(e=>i.has(e.pid)||o.has(e.processName));if(0===s.length)throw new tv("COMMAND_FAILED",`No Activity Monitor sample found for ${r}`,{appBundleId:r,deviceId:a.id,hint:"Keep the app running in the foreground while perf samples the device, then retry."});let d=new Map;for(let e of s){let t=d.get(e.pid);if(!t){d.set(e.pid,e);continue}d.set(e.pid,{pid:e.pid,processName:e.processName||t.processName,cpuTimeNs:iI(t.cpuTimeNs,e.cpuTimeNs),residentMemoryBytes:iI(t.residentMemoryBytes,e.residentMemoryBytes)})}let u=[...d.values()],c=u.map(e=>e.cpuTimeNs).filter(e=>null!==e),f=u.map(e=>e.residentMemoryBytes).filter(e=>null!==e);return{cpuTimeNs:c.length>0?c.reduce((e,t)=>e+t,0):null,residentMemoryBytes:f.length>0?f.reduce((e,t)=>e+t,0):null,matchedProcesses:M(u.map(e=>e.processName))}}async function id(e){let t=`kMDItemCFBundleIdentifier == "${e.replaceAll('"','\\"')}"`,r=await th("mdfind",[t],{allowFailure:!0,timeoutMs:15e3});if(0!==r.exitCode)throw new tv("COMMAND_FAILED",`Failed to resolve macOS app bundle for ${e}`,{appBundleId:e,stdout:r.stdout,stderr:r.stderr,exitCode:r.exitCode});let a=r.stdout.split("\n").map(e=>e.trim()).find(e=>e.endsWith(".app"));if(!a)throw new tv("APP_NOT_INSTALLED",`No macOS app found for ${e}`,{appBundleId:e});return a}async function iu(e,t){let r=te(e,["get_app_container",e.id,t,"app"]),a=await th("xcrun",r,{allowFailure:!0,timeoutMs:15e3});if(0!==a.exitCode)throw new tv("COMMAND_FAILED",`Failed to resolve iOS simulator app container for ${t}`,{appBundleId:t,stdout:a.stdout,stderr:a.stderr,exitCode:a.exitCode,hint:"Ensure the iOS simulator app is installed and booted, then retry perf."});let i=a.stdout.trim();if(0===i.length)throw new tv("APP_NOT_INSTALLED",`No iOS simulator app container found for ${t}`,{appBundleId:t});return i}async function ic(e,t){let r="macos"===e.platform?["-axo","pid=,%cpu=,rss=,command="]:te(e,["spawn",e.id,"ps","-axo","pid=,%cpu=,rss=,command="]);return(function(e){let t=[];for(let r of e.split("\n")){let e=r.trim();if(0===e.length)continue;let a=e.match(/^(\d+)\s+([0-9]+(?:\.[0-9]+)?)\s+(\d+)\s+(.+)$/);if(!a)continue;let i=Number(a[1]),n=Number(a[2]),o=Number(a[3]),s=a[4].trim();Number.isFinite(i)&&Number.isFinite(n)&&Number.isFinite(o)&&t.push({pid:i,cpuPercent:n,rssKb:o,command:s})}return t})((await th("macos"===e.platform?"ps":"xcrun",r,{timeoutMs:15e3})).stdout).filter(e=>{var r,a;let i;return r=e.command,a=t,i=ip(r),!!(a.executablePath&&(i===a.executablePath||r.startsWith(`${a.executablePath} `)))||n.basename(i)===a.executableName})}function ip(e){let[t=""]=e.trim().split(/\s+/,1);return t}function im(e){return{cpu:{usagePercent:aQ(e.usagePercent),measuredAt:e.measuredAt,method:e.cpuMethod,matchedProcesses:e.matchedProcesses},memory:{residentMemoryKb:Math.round(e.residentMemoryKb),measuredAt:e.measuredAt,method:e.memoryMethod,matchedProcesses:e.matchedProcesses}}}function ih(e,t){for(let r of e){if(t(r))return r;let e=ih(r.children,t);if(e)return e}}function ig(e){if(!e||e.children.some(e=>"sentinel"===e.name)||!e.text)return null;let t=Number(e.text);return Number.isFinite(t)?t:null}function iw(e,t){return e?e.attributes.ref?t.get(e.attributes.ref)?.numberValue??null:ig(e):null}function iv(e){let t=e?.attributes.fmt?.trim()??"";return t?t.replace(/\s+\(\d+\)$/,"").trim():null}function iy(e,t){let r=eb(e,t);if(r)return r;let a=`${e}
|
|
6
6
|
${t}`.toLowerCase();return a.includes("no device matched")||a.includes("failed to find device")?$:a.includes("timed out")?"Keep the iOS device unlocked and connected by cable, keep the app active, then retry perf.":"Ensure the iOS device is unlocked, trusted, visible to xctrace, and the target app stays active while perf samples it."}function iI(e,t){return null===e?t:null===t?e:Math.max(e,t)}async function iS(e){var t;let r=function(e){let t=[];for(let r of e){if("open"!==r.command)continue;let e=r.result?.startup;e&&"object"==typeof e&&"number"==typeof e.durationMs&&Number.isFinite(e.durationMs)&&"string"==typeof e.measuredAt&&0!==e.measuredAt.trim().length&&e.method===ap&&t.push({durationMs:Math.max(0,Math.round(e.durationMs)),measuredAt:e.measuredAt,method:ap,appTarget:"string"==typeof e.appTarget&&e.appTarget.length>0?e.appTarget:void 0,appBundleId:"string"==typeof e.appBundleId&&e.appBundleId.length>0?e.appBundleId:void 0})}return t.slice(-20)}(e.actions),a=r.at(-1),i=a?{available:!0,lastDurationMs:a.durationMs,lastMeasuredAt:a.measuredAt,method:ap,sampleCount:r.length,samples:r}:{available:!1,reason:"No startup sample captured yet. Run open <app|url> in this session first.",method:ap},n={session:e.name,platform:e.device.platform,device:e.device.name,deviceId:e.device.id,metrics:{startup:i,fps:{available:!1,reason:am},memory:{available:!1,reason:am},cpu:{available:!1,reason:am}},sampling:{startup:{method:ap,description:"Elapsed wall-clock time around dispatching the open command for the active session app target.",unit:"ms"},...function(e){if("android"===e.device.platform)return{memory:{method:a1,description:"Memory snapshot from adb shell dumpsys meminfo <package>. Values are reported in kilobytes.",unit:"kB"},cpu:{method:a0,description:"Aggregated CPU usage for app processes matched from adb shell dumpsys cpuinfo.",unit:"percent"}};var t=e.device;if("ios"===t.platform&&"device"===t.kind)return{memory:{method:ie,description:"Resident memory snapshot from a short xctrace Activity Monitor sample on the connected iOS device.",unit:"kB"},cpu:{method:a7,description:"Recent CPU usage snapshot from a short xctrace Activity Monitor sample on the connected iOS device.",unit:"percent"}};let r="macos"===t.platform?"host ps for the running macOS app executable resolved from the bundle ID.":"xcrun simctl spawn ps for the running iOS simulator app executable resolved from the bundle ID.";return{memory:{method:a9,description:`Resident memory snapshot from ${r}`,unit:"kB"},cpu:{method:a6,description:`Recent CPU usage snapshot from ${r}`,unit:"percent"}}}(e)}};if("android"!==(t=e).device.platform&&"ios"!==t.device.platform&&"macos"!==t.device.platform)return n;if(!e.appBundleId){let t="android"===e.device.platform?"No Android app package is associated with this session. Run open <app> first.":"No Apple app bundle ID is associated with this session. Run open <app> first.";return n.metrics.memory={available:!1,reason:t},n.metrics.cpu={available:!1,reason:t},n}let[o,s]=await iA(e);return n.metrics.memory=ib(o),n.metrics.cpu=ib(s),n}async function iA(e){let t=e.appBundleId;if("android"===e.device.platform){let[r,a]=await Promise.allSettled([a5(e.device,t),a2(e.device,t)]);return[r,a]}try{let r=await it(e.device,t);return[{status:"fulfilled",value:r.memory},{status:"fulfilled",value:r.cpu}]}catch(e){return[{status:"rejected",reason:e},{status:"rejected",reason:e}]}}function ib(e){if("fulfilled"===e.status)return{available:!0,...e.value};let t=tw(e.reason);return{available:!1,reason:t.message,error:t}}let i_=["path","start","stop","doctor","mark","clear"],iN=`logs requires ${i_.slice(0,-1).join(", ")}, or ${i_.at(-1)}`,ix=["dump","log"],iD=`network requires ${ix.join(" or ")}`,iM=["summary","headers","body","all"],ik=`network include mode must be one of: ${iM.join(", ")}`;async function iP(e){let{req:t}=e;return"perf"===t.command?iL(e):"logs"===t.command?iR(e):"network"===t.command?iT(e):null}async function iL(e){let{sessionName:t,sessionStore:r}=e,a=r.get(t);if(!a)return N("SESSION_NOT_FOUND","perf requires an active session. Run open first.");try{return{ok:!0,data:await iS(a)}}catch(e){return{ok:!1,error:tw(e)}}}async function iR(e){let{req:t,sessionName:r,sessionStore:i}=e,n=i.get(r);if(!n)return N("SESSION_NOT_FOUND","logs requires an active session");if(!to("logs",n.device))return N("UNSUPPORTED_OPERATION","logs is not supported on this device");let o=(t.positionals?.[0]??"path").toLowerCase(),s=!!t.flags?.restart;return i_.includes(o)?s&&"clear"!==o?N("INVALID_ARGS","logs --restart is only supported with logs clear"):"path"===o?function(e,t,r){let i=r.resolveAppLogPath(t),n=function(e){if(!a.existsSync(e))return{exists:!1,sizeBytes:0};let t=a.statSync(e);return{exists:!0,sizeBytes:t.size,modifiedAt:t.mtime.toISOString()}}(i);return{ok:!0,data:{path:i,active:!!e.appLog,state:e.appLog?.getState()??"inactive",backend:e.appLog?e.appLog.backend:"macos"===e.device.platform?"macos":"ios"===e.device.platform?"device"===e.device.kind?"ios-device":"ios-simulator":"android",sizeBytes:n.sizeBytes,modifiedAt:n.modifiedAt,startedAt:e.appLog?.startedAt?new Date(e.appLog.startedAt).toISOString():void 0,hint:'Grep the file for token-efficient debugging, e.g. grep -n "Error\\|Exception" <path>'}}}(n,r,i):"doctor"===o?iO(n,r,i):"mark"===o?function(e,t,r){let i,n=e.positionals?.slice(1).join(" ")??"",o=r.resolveAppLogPath(t);return ru(o),i=`[agent-device][mark][${new Date().toISOString()}] ${n.trim()||"marker"}
|
|
7
7
|
`,a.appendFileSync(o,i,"utf8"),{ok:!0,data:{path:o,marked:!0}}}(t,r,i):"clear"===o?iC(n,r,i,s):"start"===o?iE(n,r,i):"stop"===o?i$(n,r,i):N("INVALID_ARGS",iN):N("INVALID_ARGS",iN)}async function iO(e,t,r){let a=r.resolveAppLogPath(t),i=await rg(e.device,e.appBundleId);return{ok:!0,data:{path:a,active:!!e.appLog,state:e.appLog?.getState()??"inactive",checks:i.checks,notes:i.notes}}}async function iC(e,t,r,a){if(e.appLog&&!a)return N("INVALID_ARGS","logs clear requires logs to be stopped first; run logs stop");if(a&&!e.appBundleId)return N("INVALID_ARGS","logs clear --restart requires an app session; run open <app> first");let i=r.resolveAppLogPath(t);if(!a)return{ok:!0,data:rw(i)};e.appLog&&await rh(e.appLog);let n=rw(i),o=r.resolveAppLogPidPath(t);try{let a=await rp(e.device,e.appBundleId,i,o);return r.set(t,{...e,appLog:{platform:e.device.platform,backend:a.backend,outPath:i,startedAt:a.startedAt,getState:a.getState,stop:a.stop,wait:a.wait}}),{ok:!0,data:{...n,restarted:!0}}}catch(a){return r.set(t,{...e,appLog:void 0}),{ok:!1,error:tw(a)}}}async function iE(e,t,r){if(e.appLog)return N("INVALID_ARGS","app log already streaming; run logs stop first");if(!e.appBundleId)return N("INVALID_ARGS","logs start requires an app session; run open <app> first");let a=r.resolveAppLogPath(t),i=r.resolveAppLogPidPath(t);try{let n=await rp(e.device,e.appBundleId,a,i);return r.set(t,{...e,appLog:{platform:e.device.platform,backend:n.backend,outPath:a,startedAt:n.startedAt,getState:n.getState,stop:n.stop,wait:n.wait}}),{ok:!0,data:{path:a,started:!0}}}catch(e){return{ok:!1,error:tw(e)}}}async function i$(e,t,r){if(!e.appLog)return N("INVALID_ARGS","no app log stream active");let a=e.appLog.outPath;return await rh(e.appLog),r.set(t,{...e,appLog:void 0}),{ok:!0,data:{path:a,stopped:!0}}}async function iT(e){let{req:t,sessionName:r,sessionStore:a}=e,i=a.get(r);if(!i)return N("SESSION_NOT_FOUND","network requires an active session");if(!to("network",i.device))return N("UNSUPPORTED_OPERATION","network is not supported on this device");let n=(t.positionals?.[0]??"dump").toLowerCase();if(!ix.includes(n))return N("INVALID_ARGS",iD);let o=t.positionals?.[1]?Number.parseInt(t.positionals[1],10):25;if(!Number.isInteger(o)||o<1||o>200)return N("INVALID_ARGS","network dump limit must be an integer in range 1..200");let s=function(e){let t=e.positionals?.[2]?.toLowerCase(),r=e.flags?.networkInclude;if(t&&r&&t!==r)return N("INVALID_ARGS","network include mode was provided both positionally and via --include with different values");let a=(r??t??"summary").toLowerCase();return iM.includes(a)?{ok:!0,include:a}:N("INVALID_ARGS",ik)}(t);if(!s.ok)return s;let{include:l}=s,d=await rc({device:i.device,appBundleId:i.appBundleId,appLogState:i.appLog?.getState(),appLogStartedAt:i.appLog?.startedAt,appLogPath:a.resolveAppLogPath(r),maxEntries:o,include:l,maxPayloadChars:2048,maxScanLines:4e3});return{ok:!0,data:{...d.dump,active:!!i.appLog,state:i.appLog?.getState()??"inactive",backend:d.backend,notes:d.notes}}}let iF=new Set(["ios","android","macos","linux"]);function iU(e,t,r){let a=e[t];if(void 0!==a)throw new tv("INVALID_ARGS",a===r?`Duplicate replay test metadata "${t}" in context header.`:`Conflicting replay test metadata "${t}" in context header: ${String(a)} vs ${String(r)}.`);e[t]=r}function iG(e){return!!e&&!Number.isNaN(Number(e))}let iV=/[*?[\]{}]/;async function iq(e){let t,{filePath:r,sessionName:a,requestId:i,timeoutMs:n,platform:o,runReplay:s,cleanupSession:l}=e;b(i);let d=new Set,u=!1,c=s({filePath:r,sessionName:a,platform:o,requestId:i,artifactPaths:d}).catch(e=>{let t=tg(e);return N(t.code,t.message)}).finally(()=>{B(i)});try{return"number"==typeof n?await Promise.race([c,new Promise(e=>{t=setTimeout(()=>{u=!0,e2(i),e(function(e,t=[]){return{ok:!1,error:{code:"COMMAND_FAILED",message:`TIMEOUT after ${e}ms`,hint:"Replay test timeouts are cooperative; the active command may take a short grace period to stop.",details:{reason:"timeout",timeoutMs:e,timeoutMode:"cooperative",artifactPaths:t}}}}(n,[...d]))},n)})]):await c}finally{t&&clearTimeout(t),u&&(await ij(c)||tr({level:"warn",phase:"test_timeout_cleanup_race",data:{session:a,requestId:i,graceMs:2e3}}));try{await l(a)}catch(e){tr({level:"warn",phase:"test_cleanup_failed",data:{session:a,error:tg(e).message}})}}}async function ij(e){return await Promise.race([e.then(()=>!0),f(2e3).then(()=>!1)])}async function iH(e){let{req:t,sessionName:r,runReplay:i,cleanupSession:o}=e;if((t.positionals?.length??0)===0)return N("INVALID_ARGS","test requires at least one path or glob");try{var s,l,d,u,c,f;let e,p,m,h,g,w=function(e){let{inputs:t,cwd:r,platformFilter:i}=e,o=r??process.cwd(),s=[...new Set(t.flatMap(e=>(function(e,t){var r,i;let o=tF.expandHome(e,t);if(a.existsSync(o)){let t=a.statSync(o);if(t.isDirectory())return a.globSync("**/*.ad",{cwd:o}).map(e=>n.join(o,e));if(t.isFile()){if(".ad"!==n.extname(o))throw new tv("INVALID_ARGS",`test requires .ad files. Received: ${e}`);return[o]}return[]}if(r=e,!iV.test(r)&&(i=o,!iV.test(i)))throw new tv("INVALID_ARGS",`test input not found: ${e}`);let s=n.isAbsolute(o)?o:e;return a.globSync(s,{cwd:n.isAbsolute(o)?void 0:t}).map(e=>n.isAbsolute(e)?e:n.resolve(t,e)).filter(e=>".ad"===n.extname(e)&&function(e){try{return a.statSync(e).isFile()}catch{return!1}}(e))})(e,o)))].map(e=>n.normalize(e)).sort((e,t)=>e.localeCompare(t)),l=[];for(let e of s){var d,u;let t=function(e){let t=e.split(/\r?\n/),r={};for(let e of t){let t=e.trim();if(0===t.length||t.startsWith("#"))continue;if(!t.startsWith("context "))break;let a=t.match(/(?:^|\s)platform=([^\s]+)/);if(a){let e=a[1];e&&iF.has(e)&&iU(r,"platform",e)}let i=t.match(/(?:^|\s)timeout=(\d+)/);if(i){let e=Number(i[1]);Number.isFinite(e)&&e>=1&&iU(r,"timeoutMs",Math.floor(e))}let n=t.match(/(?:^|\s)retries=(\d+)/);if(n){let e=Number(n[1]);Number.isFinite(e)&&e>=0&&iU(r,"retries",Math.floor(e))}}return r}(a.readFileSync(e,"utf8"));if(!i){l.push({kind:"run",path:e,metadata:t});continue}if(!t.platform){l.push({kind:"skip",path:e,reason:"skipped-by-filter",message:`missing platform metadata for --platform ${i}`});continue}d=i,u=t.platform,("apple"===d?"apple"===u||"ios"===u||"macos"===u:u===d)&&l.push({kind:"run",path:e,metadata:t})}if(0===l.filter(e=>"run"===e.kind).length){let e=i?` for --platform ${i}`:"";throw new tv("INVALID_ARGS",`No .ad tests matched${e}.`)}return l}({inputs:t.positionals,cwd:t.meta?.cwd,platformFilter:t.flags?.platform}),v=(s=t.meta?.requestId,(s?.trim()||`${process.pid}-${Date.now().toString(36)}`).toLowerCase().replace(/[^a-z0-9]+/g,"-").replace(/^-+|-+$/g,"")||"suite"),y=function(e){let{artifactsDir:t,cwd:r,suiteInvocationId:a}=e,i=tF.expandHome(t??".agent-device/test-artifacts",r);return n.join(i,a)}({artifactsDir:"string"==typeof t.flags?.artifactsDir?t.flags.artifactsDir:void 0,cwd:t.meta?.cwd,suiteInvocationId:v}),I=[],S=Date.now(),A=0;for(let e of w){if("skip"===e.kind){I.push({file:e.path,status:"skipped",durationMs:0,reason:e.reason,message:e.message});continue}A+=1;let a=await iB({entry:e,sessionName:r,suiteInvocationId:v,caseIndex:A-1,cwd:t.meta?.cwd,requestId:t.meta?.requestId,retries:function(e,t){let r="number"==typeof e?e:t;return"number"!=typeof r?0:Math.max(0,Math.min(3,r))}(t.flags?.retries,e.metadata.retries),timeoutMs:(l=t.flags?.timeoutMs,d=e.metadata.timeoutMs,"number"==typeof l?l:d),suiteArtifactsDir:y,runReplay:i,cleanupSession:o});if(I.push(a),t.flags?.failFast===!0)break}let b=(u=w.length,c=I,f=Date.now()-S,e=c.filter(e=>"passed"===e.status).length,m=(p=c.filter(e=>"failed"===e.status)).length,h=c.filter(e=>"skipped"===e.status).length,g=e+m,{total:u,executed:g,passed:e,failed:m,skipped:h,notRun:Math.max(0,u-g-h),durationMs:f,failures:p,tests:c});return{ok:!0,data:b}}catch(t){let e=tg(t);return N(e.code,e.message)}}async function iB(e){var t,r;let i,o,{entry:s,sessionName:l,suiteInvocationId:d,caseIndex:u,cwd:c,requestId:f,retries:p,timeoutMs:m,suiteArtifactsDir:h,runReplay:g,cleanupSession:w}=e,v=Date.now(),y=n.join(h,(t=s.path,(0===(o=c?n.relative(c,t):n.basename(t)).length||o.startsWith("..")?n.basename(t):o).toLowerCase().replace(/[\\/]+/g,"__").replace(/[^a-z0-9._-]+/g,"-").replace(/^-+|-+$/g,"")||"test")),I="",S=0;for(let e=0;e<=p;e+=1){S=e+1;let t=function(e,t,r,a,i=0){let o=n.basename(r,n.extname(r)).toLowerCase().replace(/[^a-z0-9]+/g,"-").replace(/^-+|-+$/g,"");return`${e}:test:${t}:${a+1}${o?`-${o}`:""}:attempt-${i+1}`}(l,d,s.path,u,e),o=n.join(y,`attempt-${S}`);r=s.path,a.mkdirSync(o,{recursive:!0}),a.copyFileSync(r,n.join(o,"replay.ad"));let c=function(e){let{requestId:t,suiteInvocationId:r,filePath:a,caseIndex:i,attemptIndex:o}=e;return U(`${t??r}:test:${i+1}:${n.basename(a)}:attempt:${o+1}`,r)}({requestId:f,suiteInvocationId:d,filePath:s.path,caseIndex:u,attemptIndex:e}),h=await iq({filePath:s.path,sessionName:t,requestId:c,timeoutMs:m,platform:s.metadata.platform,runReplay:g,cleanupSession:w});if(!function(e){let{response:t,filePath:r,sessionName:i,attempts:o,maxAttempts:s,attemptArtifactsDir:l}=e,d=[...function(e){let t=e.ok?e.data?.artifactPaths:e.error.details?.artifactPaths;return Array.isArray(t)?[...new Set(t.filter(e=>"string"==typeof e))]:[]}(t)];t.ok||"string"!=typeof t.error.logPath||d.push(t.error.logPath);let u=function(e,t){let r=[],i=new Map;for(let o of e){if(!function(e){try{return a.statSync(e).isFile()}catch{return!1}}(o))continue;let e=function(e,t){let r=n.extname(e),a=r?e.slice(0,-r.length):e,i=t.get(e)??0;return(t.set(e,i+1),0===i)?e:`${a}-${i+1}${r}`}(n.basename(o),i),s=n.join(t,e);n.resolve(o)!==n.resolve(s)&&a.copyFileSync(o,s),r.push(s)}return r}(d,l),c=[`file: ${r}`,`session: ${i}`,`attempt: ${o}/${s}`,`status: ${t.ok?"passed":"failed"}`];if(t.ok){let e="number"==typeof t.data?.replayed?t.data.replayed:0,r="number"==typeof t.data?.healed?t.data.healed:0;c.push(`replayed: ${e}`,`healed: ${r}`)}else c.push(`code: ${t.error.code}`,`message: ${t.error.message}`),t.error.hint&&c.push(`hint: ${t.error.hint}`),t.error.diagnosticId&&c.push(`diagnosticId: ${t.error.diagnosticId}`),t.error.logPath&&c.push(`logPath: ${t.error.logPath}`),t.error.details?.reason==="timeout"&&c.push("timeoutMode: cooperative");u.length>0&&c.push(`copiedArtifacts: ${u.map(e=>n.basename(e)).join(", ")}`);let f=n.join(l,"result.txt"),p=`${c.join("\n")}
|
|
8
8
|
`;a.writeFileSync(f,p),t.ok||a.writeFileSync(n.join(l,"failure.txt"),p)}({response:h,filePath:s.path,sessionName:t,attempts:S,maxAttempts:p+1,attemptArtifactsDir:o}),i=h,I=t,h.ok)break}let A=Date.now()-v;if(i?.ok)return{file:s.path,session:I,status:"passed",durationMs:A,attempts:S,artifactsDir:y,replayed:"number"==typeof i.data?.replayed?i.data.replayed:0,healed:"number"==typeof i.data?.healed?i.data.healed:0};let b=i?.ok?{code:"COMMAND_FAILED",message:"Unknown replay test failure"}:i?.error??{code:"COMMAND_FAILED",message:"Unknown replay test failure"};return{file:s.path,session:I,status:"failed",durationMs:A,attempts:S,artifactsDir:y,error:b}}function iK(e){if(0===e.length)return{selectorExpression:null,selectorTimeout:null};let t=e[e.length-1],r=/^\d+$/.test(t??""),a=eo(r?e.slice(0,-1):e.slice());return!a||a.rest.length>0?{selectorExpression:null,selectorTimeout:null}:{selectorExpression:a.selectorExpression,selectorTimeout:r?t:null}}async function iz(e){let{action:t,sessionName:r,logPath:a,sessionStore:i}=e;if(!(t_(t.command)||["fill","get","is","wait"].includes(t.command)))return null;let n=i.get(r);if(!n)return null;let o=(function(e){let t=[],r=Array.isArray(e.result?.selectorChain)&&e.result?.selectorChain.every(e=>"string"==typeof e)?e.result.selectorChain:[];if(t.push(...r),t_(e.command)){let r=e.positionals?.[0]??"";r&&!r.startsWith("@")&&t.push(e.positionals.join(" "))}if("fill"===e.command){let r=e.positionals?.[0]??"";r&&!r.startsWith("@")&&Number.isNaN(Number(r))&&t.push(r)}if("get"===e.command){let r=e.positionals?.[1]??"";r&&!r.startsWith("@")&&t.push(e.positionals.slice(1).join(" "))}if("is"===e.command){let{split:r}=eZ(e.positionals);r&&t.push(r.selectorExpression)}if("wait"===e.command){let{selectorExpression:r}=iK(e.positionals??[]);r&&t.push(r)}return M(t).filter(e=>e.trim().length>0)})(t).map(e=>j(e)).filter(e=>null!==e);if(0===o.length)return null;let s=t_(t.command)||"fill"===t.command,l=t_(t.command)||"fill"===t.command||"get"===t.command&&t.positionals?.[0]==="text",d=await iW(n,t,a,s,i);for(let e of o){let r=A(d.nodes,e,{platform:n.device.platform,requireRect:s,requireUnique:!0,disambiguateAmbiguous:l});if(!r)continue;let a=eg(r.node,n.device.platform,{action:t_(t.command)?"click":"fill"===t.command?"fill":"get"}).join(" || ");if(t_(t.command))return{...t,positionals:[a]};if("fill"===t.command){let e=eh(t);if(!e)continue;return{...t,positionals:[a,e]}}if("get"===t.command){let e=t.positionals?.[0];if("text"!==e&&"attrs"!==e)continue;return{...t,positionals:[e,a]}}if("is"===t.command){let{predicate:e,split:r}=eZ(t.positionals);if(!e)continue;let i=r?.rest.join(" ").trim()??"",n=[e,a];return"text"===e&&i.length>0&&n.push(i),{...t,positionals:n}}if("wait"===t.command){let{selectorTimeout:e}=iK(t.positionals??[]),r=[a];return e&&r.push(e),{...t,positionals:r}}}return null}async function iW(e,t,r,a,i){let n=await w(e.device,"snapshot",[],t.flags?.out,{...eG(r,{...t.flags??{},snapshotInteractiveOnly:a,snapshotCompact:a},e.appBundleId,e.trace?.outPath)}),o=n?.nodes??[],s={nodes:ea(t.flags?.snapshotRaw?o:ek(o)),truncated:n?.truncated,createdAt:Date.now(),backend:n?.backend};return e.snapshot=s,i.set(e.name,e),s}async function iJ(e){let{req:t,sessionName:r,logPath:i,sessionStore:n,invoke:o}=e,s=t.positionals?.[0];if(!s)return N("INVALID_ARGS","replay requires a path");let l="",d=new Set;try{l=tF.expandHome(s,t.meta?.cwd);let e=a.readFileSync(l,"utf8"),u=e.trimStart()[0];if("{"===u||"["===u)return N("INVALID_ARGS","replay accepts .ad script files. JSON replay payloads are no longer supported.");let c=function(e){let t=[];for(let r of e.split(/\r?\n/)){let e=function(e){let t=e.trim();if(0===t.length||t.startsWith("#"))return null;let r=function(e){let t=[],r=0;for(;r<e.length;){for(;r<e.length&&/\s/.test(e[r]);)r+=1;if(r>=e.length)break;if('"'===e[r]){let a=r+1,i=!1;for(;a<e.length;){let t=e[a];if('"'===t&&!i)break;i="\\"===t&&!i,"\\"!==t&&(i=!1),a+=1}if(a>=e.length)throw new tv("INVALID_ARGS",`Invalid replay script line: ${e}`);let n=e.slice(r,a+1);t.push(JSON.parse(n)),r=a+1;continue}let a=r;for(;a<e.length&&!/\s/.test(e[a]);)a+=1;t.push(e.slice(r,a)),r=a}return t}(t);if(0===r.length)return null;let[a,...i]=r;if("context"===a)return null;let n={ts:Date.now(),command:a,positionals:[],flags:{}};if("snapshot"===a){n.positionals=[];for(let e=0;e<i.length;e+=1){let t=i[e];if("-i"===t){n.flags.snapshotInteractiveOnly=!0;continue}if("-c"===t){n.flags.snapshotCompact=!0;continue}if("--raw"===t){n.flags.snapshotRaw=!0;continue}if(("-d"===t||"--depth"===t)&&e+1<i.length){let t=Number(i[e+1]);Number.isFinite(t)&&t>=0&&(n.flags.snapshotDepth=Math.floor(t)),e+=1;continue}if(("-s"===t||"--scope"===t)&&e+1<i.length){n.flags.snapshotScope=i[e+1],e+=1;continue}if("--backend"===t&&e+1<i.length){e+=1;continue}}return n}if("open"===a){let e=function(e){var t;let r=[],a={};for(let t of e){if("--relaunch"===t){a.relaunch=!0;continue}r.push(t)}let i=tE(r);return{positionals:i.positionals,flags:a,runtime:(t=i.flags).platform||t.metroHost||void 0!==t.metroPort||t.bundleUrl||t.launchUrl?i.flags:void 0}}(i);return n.positionals=e.positionals,Object.assign(n.flags,e.flags),n.runtime=e.runtime,n}if("runtime"===a){let e=tE(i);return n.positionals=e.positionals,Object.assign(n.flags,e.flags),n}if(t_(a)){let e=tC(a,i);if(Object.assign(n.flags,e.flags),0===e.positionals.length)return n;let t=e.positionals[0];if(t.startsWith("@"))return n.positionals=[t],e.positionals[1]&&(n.result={refLabel:e.positionals[1]}),n;let r=e.positionals[0],o=e.positionals[1];return iG(r)&&iG(o)&&e.positionals.length>=2?n.positionals=[r,o]:n.positionals=[e.positionals.join(" ")],n}if("fill"===a){let e=tC(a,i);if(Object.assign(n.flags,e.flags),e.positionals.length<2)return n.positionals=e.positionals,n;let t=e.positionals[0];return t.startsWith("@")?(e.positionals.length>=3?(n.positionals=[t,e.positionals.slice(2).join(" ")],n.result={refLabel:e.positionals[1]}):n.positionals=[t,e.positionals[1]],n):(n.positionals=[t,e.positionals.slice(1).join(" ")],n)}if("get"===a){if(i.length<2)return n.positionals=i,n;let e=i[0],t=i[1];return t.startsWith("@")?(n.positionals=[e,t],i[2]&&(n.result={refLabel:i[2]})):n.positionals=[e,i.slice(1).join(" ")],n}if("swipe"===a||"type"===a){let e=tC(a,i);return Object.assign(n.flags,e.flags),n.positionals=e.positionals,n}if("record"===a){let e=[];for(let t=0;t<i.length;t+=1){let r=i[t];if("--hide-touches"===r){n.flags.hideTouches=!0;continue}if("--fps"===r&&t+1<i.length){let e=Number(i[t+1]);Number.isFinite(e)&&(n.flags.fps=Math.floor(e)),t+=1;continue}e.push(r)}return n.positionals=e,n}if("screenshot"===a){let e=[];for(let t of i){if("--fullscreen"===t){n.flags.screenshotFullscreen=!0;continue}e.push(t)}return n.positionals=e,n}return n.positionals=i,n}(r);e&&t.push(e)}return t}(e),f=t.flags?.replayUpdate===!0,p=0;for(let e=0;e<c.length;e+=1){let a=c[e];if(!a||"replay"===a.command)continue;let s=await iZ({req:t,sessionName:r,action:a,invoke:o});if(s.ok){iY(s).forEach(e=>d.add(e));continue}if(!f)return iX(s,a,e,l,[...d]);let u=await iz({action:a,sessionName:r,logPath:i,sessionStore:n});if(!u)return iX(s,a,e,l,[...d]);if(c[e]=u,!(s=await iZ({req:t,sessionName:r,action:u,invoke:o})).ok)return iX(s,u,e,l,[...d]);iY(s).forEach(e=>d.add(e)),p+=1}return f&&p>0&&function(e,t,r){let i=[];if(r){let e=r.device.kind?` kind=${r.device.kind}`:"",t=r.device.target?` target=${r.device.target}`:"";i.push(`context platform=${r.device.platform}${t} device=${tD(r.device.name)}${e} theme=unknown`)}for(let e of t)i.push(function(e){let t=[e.command];if("snapshot"===e.command)return e.flags?.snapshotInteractiveOnly&&t.push("-i"),e.flags?.snapshotCompact&&t.push("-c"),"number"==typeof e.flags?.snapshotDepth&&t.push("-d",String(e.flags.snapshotDepth)),e.flags?.snapshotScope&&t.push("-s",tx(e.flags.snapshotScope)),e.flags?.snapshotRaw&&t.push("--raw"),t.join(" ");if("open"===e.command)return tT(t,e),t.join(" ");if("runtime"===e.command){for(let r of e.positionals??[])t.push(tM(r,tP));return tR(t,e.flags),t.join(" ")}if("record"===e.command)return tO(t,e),t.join(" ");if("screenshot"===e.command){for(let r of e.positionals??[])t.push(tx(r));return e.flags?.screenshotFullscreen&&t.push("--fullscreen"),t.join(" ")}for(let r of e.positionals??[])t.push(tx(r));return tL(t,e),t.join(" ")}(e));let n=`${i.join("\n")}
|
package/dist/src/index.d.ts
CHANGED
|
@@ -26,6 +26,13 @@ export declare type AgentDeviceClient = {
|
|
|
26
26
|
materializations: {
|
|
27
27
|
release: (options: MaterializationReleaseOptions) => Promise<MaterializationReleaseResult>;
|
|
28
28
|
};
|
|
29
|
+
leases: {
|
|
30
|
+
allocate: (options: LeaseAllocateOptions) => Promise<Lease>;
|
|
31
|
+
heartbeat: (options: LeaseScopedOptions) => Promise<Lease>;
|
|
32
|
+
release: (options: LeaseScopedOptions) => Promise<{
|
|
33
|
+
released: boolean;
|
|
34
|
+
}>;
|
|
35
|
+
};
|
|
29
36
|
metro: {
|
|
30
37
|
prepare: (options: MetroPrepareOptions) => Promise<MetroPrepareResult>;
|
|
31
38
|
};
|
|
@@ -74,7 +81,6 @@ export declare type AgentDeviceClientConfig = {
|
|
|
74
81
|
lockPolicy?: DaemonLockPolicy;
|
|
75
82
|
lockPlatform?: PlatformSelector;
|
|
76
83
|
requestId?: string;
|
|
77
|
-
remoteConfig?: string;
|
|
78
84
|
stateDir?: string;
|
|
79
85
|
daemonBaseUrl?: string;
|
|
80
86
|
daemonAuthToken?: string;
|
|
@@ -84,6 +90,8 @@ export declare type AgentDeviceClientConfig = {
|
|
|
84
90
|
sessionIsolation?: SessionIsolationMode;
|
|
85
91
|
runId?: string;
|
|
86
92
|
leaseId?: string;
|
|
93
|
+
leaseBackend?: LeaseBackend;
|
|
94
|
+
runtime?: SessionRuntimeHints;
|
|
87
95
|
cwd?: string;
|
|
88
96
|
debug?: boolean;
|
|
89
97
|
};
|
|
@@ -129,7 +137,7 @@ export declare type AgentDeviceIdentifiers = {
|
|
|
129
137
|
package?: string;
|
|
130
138
|
};
|
|
131
139
|
|
|
132
|
-
export declare type AgentDeviceRequestOverrides = Pick<AgentDeviceClientConfig, 'session' | 'lockPolicy' | 'lockPlatform' | 'requestId' | 'tenant' | 'sessionIsolation' | 'runId' | 'leaseId' | 'cwd' | 'debug'>;
|
|
140
|
+
export declare type AgentDeviceRequestOverrides = Pick<AgentDeviceClientConfig, 'session' | 'lockPolicy' | 'lockPlatform' | 'requestId' | 'daemonBaseUrl' | 'daemonAuthToken' | 'daemonTransport' | 'daemonServerMode' | 'tenant' | 'sessionIsolation' | 'runId' | 'leaseId' | 'leaseBackend' | 'cwd' | 'debug'>;
|
|
133
141
|
|
|
134
142
|
export declare type AgentDeviceSelectionOptions = {
|
|
135
143
|
platform?: PlatformSelector;
|
|
@@ -549,8 +557,34 @@ export declare type KeyboardCommandResult = DaemonResponseData & {
|
|
|
549
557
|
attempts?: number;
|
|
550
558
|
};
|
|
551
559
|
|
|
560
|
+
declare type Lease = {
|
|
561
|
+
leaseId: string;
|
|
562
|
+
tenantId: string;
|
|
563
|
+
runId: string;
|
|
564
|
+
backend: LeaseBackend;
|
|
565
|
+
createdAt?: number;
|
|
566
|
+
heartbeatAt?: number;
|
|
567
|
+
expiresAt?: number;
|
|
568
|
+
};
|
|
569
|
+
|
|
570
|
+
declare type LeaseAllocateOptions = LeaseOptions & {
|
|
571
|
+
tenant: string;
|
|
572
|
+
runId: string;
|
|
573
|
+
leaseBackend?: LeaseBackend;
|
|
574
|
+
};
|
|
575
|
+
|
|
552
576
|
declare type LeaseBackend = 'ios-simulator' | 'ios-instance' | 'android-instance';
|
|
553
577
|
|
|
578
|
+
declare type LeaseOptions = AgentDeviceRequestOverrides & {
|
|
579
|
+
ttlMs?: number;
|
|
580
|
+
};
|
|
581
|
+
|
|
582
|
+
declare type LeaseScopedOptions = LeaseOptions & {
|
|
583
|
+
tenant?: string;
|
|
584
|
+
runId?: string;
|
|
585
|
+
leaseId: string;
|
|
586
|
+
};
|
|
587
|
+
|
|
554
588
|
export declare type LogsOptions = AgentDeviceRequestOverrides & {
|
|
555
589
|
action?: 'path' | 'start' | 'stop' | 'doctor' | 'mark' | 'clear';
|
|
556
590
|
message?: string;
|