UNPKG

eve

Version:

Filesystem-first framework for durable backend AI agents that run anywhere.

1 lines • 1.18 kB
import{mkdir,readFile,readdir,rm,writeFile}from"node:fs/promises";import{join}from"node:path";import{stopDevelopmentSandboxResources}from"#execution/sandbox/bindings/local.js";const PREFIX=`dev-cleanup-intent.`,SUFFIX=`.json`;async function recordCleanupIntent(e,t){let n=join(e,`.eve`),r=join(n,`${PREFIX}${t.runId}${SUFFIX}`);return await mkdir(n,{recursive:!0}),await writeFile(r,`${JSON.stringify(t)}\n`,`utf8`),async()=>await rm(r,{force:!0})}async function reconcileCleanupIntents(e){let t=join(e,`.eve`),n;try{n=await readdir(t)}catch(e){if(e instanceof Error&&`code`in e&&e.code===`ENOENT`)return;throw e}await Promise.all(n.filter(e=>e.startsWith(PREFIX)&&e.endsWith(SUFFIX)).map(async n=>{try{let r=join(t,n),i=JSON.parse(await readFile(r,`utf8`));if(typeof i.runId!=`string`||typeof i.ownerPid!=`number`||!Number.isInteger(i.ownerPid)||i.ownerPid<=0||isProcessAlive(i.ownerPid))return;await stopDevelopmentSandboxResources({appRoot:e,devRunId:i.runId}),await rm(r,{force:!0})}catch{}}))}function isProcessAlive(e){try{return process.kill(e,0),!0}catch(e){return e instanceof Error&&`code`in e&&e.code===`EPERM`}}export{reconcileCleanupIntents,recordCleanupIntent};