UNPKG

agent-device

Version:

Open source device layer for AI coding agents: a CLI, MCP server, and typed Node.js API to control mobile, web, TV, and desktop apps on devices and simulators, locally or in the cloud.

1 lines • 2.58 kB
import{j as e}from"./sdk-contracts.js";import{B as t,C as n,E as r,F as i,L as a,P as o,St as s,T as c,v as l}from"./sdk-io.js";import"./diagnostics.js";const u=2e3,d=5e3,f=(e,t)=>e===t;async function p(e){if(e!==void 0)return m(e,await i(e,d))}function m(e,t){let n=t?.startTime,r=t?.command;return n&&r?Object.freeze({pid:e,startTime:n,command:r}):void 0}async function h(e){let t=await g(e.pid),n=l([e.pid],t).map(({pid:e})=>e).filter(t=>t!==e.pid),r=await o(n,d),i=n.flatMap(e=>{let t=m(e,r.get(e));return t?[t]:[]});return Object.freeze([e,...i])}async function g(t){try{return await r({timeoutMs:5e3})}catch(n){return s({level:`warn`,phase:`screen_recording_process_tree_unreadable`,data:{rootPid:t,reason:e(n)}}),[]}}async function _(e,t=f){let n=await v(e,t);return n===`gone`?`missing`:n===`ours`?`owned-alive`:`ownership-lost`}async function v(e,t){if(!n(e.pid))return`gone`;let r=await i(e.pid,d),a=c({startTime:e.startTime,matches:n=>t(e.command,n)},r);return a===`exited`?`gone`:e.startTime.length===0||e.command.length===0||a===`reassigned`?`not-ours`:a===`ours`?`ours`:r.startTime!==null&&r.command!==null?`not-ours`:`unprovable`}async function y(e,t,n=f){let r=t?.child.pid,i=async e=>({marker:e,evidence:await v(e,n)}),a=({marker:e,evidence:t})=>t===`ours`||t===`unprovable`&&e.pid===r,o=await Promise.all(e.map(i)),s=o[0],c=new Set(e.map(({pid:e})=>e)),l=s!==void 0&&a(s)?await h(s.marker):[],d=await Promise.all(l.filter(({pid:e})=>!c.has(e)).map(async e=>await i(e))),p=[...o,...d],m=p.filter(a).map(({marker:e})=>e);if(m.length===0)return b(p.map(({evidence:e})=>e));let g=new Set(m.map(({pid:e})=>e)),_=p.filter(({marker:e,evidence:t})=>!g.has(e.pid)&&t!==`gone`),y=m,C=_.length>0;for(let[e,r]of[[`SIGINT`,5e3],[`SIGTERM`,u],[`SIGKILL`,u]]){x(y,e,t);let i=await S(y,r,t,n);if(C||=i.lost,y=i.running,y.length===0)return C?`ownership-lost`:`terminated`}return`ownership-lost`}function b(e){return e.every(e=>e===`gone`)?`already-missing`:`ownership-lost`}function x(e,t,n){let r=n?.child.pid;a(e.map(({pid:e})=>e).filter(e=>e!==r),t),r!==void 0&&e.some(({pid:e})=>e===r)&&n?.child.kill(t)}async function S(e,n,r,i){let a=r?.child.pid,o=r?await C(r.wait,n):!1,s=e.filter(({pid:e})=>!o||e!==a),c=await Promise.all(s.map(async e=>(await t(e.pid,n),{marker:e,evidence:await v(e,i)}))),l=[],u=!1;for(let e of c)e.evidence!==`gone`&&(e.evidence===`ours`||e.marker.pid===a?l.push(e.marker):u=!0);return{running:l,lost:u}}async function C(e,t){return await Promise.race([e.then(()=>!0,()=>!0),new Promise(e=>setTimeout(()=>e(!1),t))])}export{y as i,p as n,h as r,_ as t};