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 • 1.44 kB
import{_ as e,l as t}from"./host2.js";import{S as n}from"./runner-cache-metadata.js";import{D as r,S as i,c as a,v as o}from"./runner-lease.js";import s from"node:fs";import c from"node:path";const l=864e5,u=/^cache-[0-9a-f]{16}$/;function d(t=e().env){let n=t.AGENT_DEVICE_IOS_RUNNER_CACHE_KEEP?.trim();return!n||!/^\d+$/.test(n)?3:Number(n)}async function f(t,r=e().env,i=Date.now()){let a=d(r);if(a===0||n(r))return[];let o=m(c.dirname(t)).filter(e=>e.derived!==t).sort((e,t)=>t.lastUsedMs-e.lastUsedMs).slice(a-1).filter(e=>i-e.lastUsedMs>=l),s=[];for(let{derived:e}of o)try{await g(e,i)&&s.push(e)}catch(t){p(e,t)}return s}function p(e,n){t({level:`warn`,phase:`runner_xctestrun_cache_eviction_failed`,data:{derived:e,error:n instanceof Error?n.message:String(n)}})}function m(e){let t;try{t=s.readdirSync(e,{withFileTypes:!0}).filter(e=>e.isDirectory()&&u.test(e.name)).map(e=>e.name)}catch{return[]}return t.map(t=>{let n=c.join(e,t);return{derived:n,lastUsedMs:h(n)}})}function h(e){try{return s.statSync(r(e)).mtimeMs}catch{return 0}}async function g(e,t){let n;try{n=await o(e,0)}catch{return!1}try{if(t-h(e)<l)return!1;let n=c.basename(e);return!a().some(({xctestrunPath:t,cacheKey:r})=>r===n||t.startsWith(`${e}${c.sep}`))&&(await s.promises.rm(r(e),{force:!0}),await s.promises.rm(e,{recursive:!0,force:!0}),i(`clean`,`stale_cache_evicted`,{derived:e}),!0)}finally{await n().catch(()=>void 0)}}export{f as evictStaleRunnerCaches};