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 • 6.05 kB
JavaScript
import e from"node:crypto";import t from"node:fs";import n from"node:path";const r=134217728,i=4095;function a(e,t,n){let r=_(e);if(r===null)return{ok:!1,refusal:{reason:`root_unusable`,path:e}};let i=o(e,t,r);if(`reason`in i)return{ok:!1,refusal:i};let a=c(n,e,r);if(!a.ok)return{ok:!1,refusal:a.refusal};let s=[];for(let e of a.roots){let t=h(r,e,a.roots);if(!t.ok)return{ok:!1,refusal:t.refusal};s.push(...t.entries)}return s.length===0?{ok:!1,refusal:{reason:`root_unusable`,path:n.join(`, `)}}:(s.sort((e,t)=>e.path.localeCompare(t.path)),{ok:!0,artifacts:{xctestrunPath:t,xctestrunSize:i.size,xctestrunDigest:i.digest,productPaths:[...n],entries:s}})}function o(e,t,n){let r=g(e,t)??t,i=_(t);return i===null?{reason:`root_unusable`,path:r}:N(i,n)?s(t)?v(t)??{reason:`root_unusable`,path:r}:{reason:`root_unusable`,path:r}:{reason:`root_escapes_cache`,path:r,target:i}}function s(e){try{return t.lstatSync(e).isFile()}catch{return!1}}function c(e,t,n){let r=[];for(let i of e){let e=_(i);if(e===null)return{ok:!1,refusal:C(t,i)};if(!N(e,n))return{ok:!1,refusal:{reason:`root_escapes_cache`,path:g(t,i)??i,target:e}};r.push(e)}let i=l(r);return i.length>0?{ok:!0,roots:i}:{ok:!1,refusal:{reason:`root_unusable`,path:e.join(`, `)}}}function l(e){let t=[];for(let n of new Set([...e].sort()))t.some(e=>N(n,e))||t.push(n);return t}function u(e,r,i){let a=[],o=[r];for(;o.length>0;){let r=o.pop(),s;try{s=t.readdirSync(r)}catch{return{ok:!1,refusal:{reason:`root_unusable`,path:g(e,r)??void 0}}}for(let c of s){let s=n.join(r,c),l=g(e,s);if(l===null)return{ok:!1,refusal:{reason:`root_unusable`,path:s}};let u;try{u=t.lstatSync(s)}catch{return{ok:!1,refusal:{reason:`root_unusable`,path:l}}}if(!m(u))return{ok:!1,refusal:{reason:`root_unusable`,path:l}};if(u.isSymbolicLink()&&d(s,i)===null)return{ok:!1,refusal:{reason:`symlink_escapes_cache`,path:l,target:f(s)??`(unreadable)`}};u.isDirectory()?o.push(s):a.push({relativePath:l,fullPath:s,stat:u})}}return{ok:!0,leaves:a}}function d(e,t){let n=f(e);if(n===null)return null;let r=p(e,n);return r===null?null:t.some(e=>N(r,e))?n:null}function f(e){try{return t.readlinkSync(e)}catch{return null}}function p(e,t){let r=_(n.dirname(e));if(r===null)return null;let i=n.isAbsolute(t)?n.resolve(t):n.resolve(r,t);return _(i)??i}function m(e){return e.isDirectory()||e.isFile()||e.isSymbolicLink()}function h(e,t,n){let a=u(e,t,n);if(!a.ok)return{ok:!1,refusal:a.refusal};let o=[];for(let e of a.leaves){if(e.stat.isSymbolicLink()){let t=f(e.fullPath);if(t===null)return{ok:!1,refusal:{reason:`root_unusable`,path:e.relativePath}};o.push({path:e.relativePath,symlink:t});continue}if(e.stat.size>r)return{ok:!1,refusal:{reason:`root_unusable`,path:e.relativePath}};let t=v(e.fullPath);if(!t)return{ok:!1,refusal:{reason:`root_unusable`,path:e.relativePath}};o.push({path:e.relativePath,size:t.size,mode:e.stat.mode&i,digest:t.digest})}return{ok:!0,entries:o}}function g(e,t){let r=n.relative(n.resolve(e),n.resolve(t));return r===``||r.startsWith(`..`)||n.isAbsolute(r)?null:r}function _(e){try{return t.realpathSync(e)}catch{return null}}function v(n){try{let i=t.statSync(n);if(!i.isFile()||i.size>r)return null;let a=e.createHash(`sha256`);return a.update(t.readFileSync(n)),{digest:a.digest(`hex`),size:i.size}}catch{return null}}function y(e,t){let n=t.artifacts,r=n?_(e):null;if(!n||!D(n)||r===null||!N(n.xctestrunPath,e))return{ok:!1,mismatch:null};let i=x(n,r);if(i)return{ok:!1,mismatch:i};let a=b(n,r,c(n.productPaths,e,r));return a?{ok:!1,mismatch:a}:{ok:!0,xctestrunPath:n.xctestrunPath,productPaths:[...n.productPaths]}}function b(e,t,n){if(!n.ok)return S(n.refusal);let r=new Map(e.entries.map(e=>[e.path,e]));for(let e of n.roots){let i=w(t,e,n.roots,r);if(i)return i}let i=r.keys().next();return i.done?null:{path:i.value,reason:`missing`}}function x(e,t){let n=E(e.xctestrunPath,e.xctestrunSize,e.xctestrunDigest);if(n)return n;let r=_(e.xctestrunPath);return r!==null&&N(r,t)?null:{path:e.xctestrunPath,reason:`root_escapes_cache`,target:r??`(?)`}}function S(e){return e.reason===`root_unusable`?{path:e.path??`(cache root)`,reason:`root_unusable`}:{...e}}function C(e,t){return{reason:`root_unusable`,path:g(e,t)??void 0}}function w(e,t,n,r){let i=u(e,t,n);if(!i.ok)return i.refusal?S(i.refusal):{path:t,reason:`root_unusable`};for(let e of i.leaves){let t=r.get(e.relativePath);if(!t)return{path:e.relativePath,reason:`undeclared_entry`};let n=T(e.fullPath,e.relativePath,t);if(n)return n;r.delete(e.relativePath)}return null}function T(e,t,n){if(`symlink`in n){let r=f(e);return r===null?{path:t,reason:`missing`}:r===n.symlink?null:{path:t,reason:`symlink_target_changed`,expected:n.symlink,actual:r}}return E(e,n.size,n.digest,n.mode,t)}function E(e,n,r,a,o){let s=o??e,c;try{c=t.lstatSync(e)}catch{return{path:s,reason:`missing`}}if(!c.isFile())return{path:s,reason:`kind_changed`};if(c.size!==n)return{path:s,reason:`size_changed`,expected:n,actual:c.size};if(a!==void 0&&(c.mode&i)!==a)return{path:s,reason:`mode_changed`,expected:a,actual:c.mode&i};let l=v(e);return l?l.digest===r?null:{path:s,reason:`digest_mismatch`}:{path:s,reason:`file_too_large`,size:c.size}}function D(e){if(!e||typeof e!=`object`||Array.isArray(e))return!1;let t=e;return typeof t.xctestrunPath==`string`&&A(t.xctestrunSize)&&typeof t.xctestrunDigest==`string`&&j(t.productPaths)&&M(t.entries)&&t.entries.every(O)}function O(e){if(!e||typeof e!=`object`||Array.isArray(e))return!1;let t=e;return typeof t.path!=`string`||!k(t.path)?!1:typeof t.symlink==`string`?t.digest===void 0:typeof t.digest==`string`&&A(t.size)&&typeof t.mode==`number`}function k(e){return!e.startsWith(`/`)&&!e.startsWith(`..`)&&!n.isAbsolute(e)}function A(e){return Number.isInteger(e)&&e>=0}function j(e){return M(e)&&e.every(e=>typeof e==`string`)}function M(e){return Array.isArray(e)&&e.length>0}function N(e,t){let r=n.relative(n.resolve(t),n.resolve(e));return r!==``&&!r.startsWith(`..`)&&!n.isAbsolute(r)}export{a as buildRunnerCacheArtifactManifest,N as isPathInsideDirectory,y as validateRunnerCacheArtifactManifest};