eve
Version:
Filesystem-first framework for durable backend AI agents that run anywhere.
1 lines • 2.39 kB
JavaScript
import{existsSync}from"node:fs";import{join,resolve}from"node:path";import{readFile,readdir,rm,stat,writeFile}from"node:fs/promises";const DEVELOPMENT_RUNTIME_ARTIFACTS_ACTIVATED_MARKER=`activated`,DEVELOPMENT_RUNTIME_ARTIFACTS_RETIRED_METADATA=`retired.json`;async function recordRetiredDevelopmentRuntimeArtifactsSnapshot(e,n=Date.now()){await writeFile(join(e,DEVELOPMENT_RUNTIME_ARTIFACTS_RETIRED_METADATA),`${JSON.stringify({retiredAt:n})}\n`)}async function pruneDevelopmentRuntimeArtifactsSnapshotDirectory(r){if(r.protectAll)return;let o=r.now??Date.now(),s=Math.max(0,r.gracePeriodMs??18e5),c=Math.max(0,Math.trunc(r.retainCount??5)),l;try{l=await readdir(r.snapshotsDirectory,{withFileTypes:!0})}catch(e){if(e instanceof Error&&`code`in e&&e.code===`ENOENT`)return;throw e}let u=r.activeSnapshotRoot===void 0?void 0:resolve(r.activeSnapshotRoot),d=await Promise.all(l.filter(e=>e.isDirectory()).map(async i=>{let a=join(r.snapshotsDirectory,i.name),s=u===resolve(a),c=existsSync(join(a,DEVELOPMENT_RUNTIME_ARTIFACTS_ACTIVATED_MARKER)),l;if(c&&!s)try{l=await readRetiredAt(a),l===void 0&&(await recordRetiredDevelopmentRuntimeArtifactsSnapshot(a,o),l=o)}catch(e){console.warn(`[eve:dev] failed to read or initialize runtime generation retirement metadata for "${a}": ${String(e)}`)}return{activated:c,active:s,path:a,retiredAt:l,mtimeMs:(await stat(a)).mtimeMs}})),f=new Set(d.flatMap(e=>e.retiredAt===void 0?[]:[{path:e.path,retiredAt:e.retiredAt}]).sort((e,t)=>t.retiredAt-e.retiredAt).slice(0,c).map(e=>e.path)),p=new Set(d.filter(e=>!e.activated).sort((e,t)=>t.mtimeMs-e.mtimeMs).slice(0,c).map(e=>e.path));await Promise.all(d.map(async e=>{e.active||e.activated&&e.retiredAt===void 0||f.has(e.path)||p.has(e.path)||e.retiredAt!==void 0&&o-e.retiredAt<=s||!e.activated&&o-e.mtimeMs<=s||await rm(e.path,{force:!0,recursive:!0})}))}async function readRetiredAt(e){let n;try{n=await readFile(join(e,DEVELOPMENT_RUNTIME_ARTIFACTS_RETIRED_METADATA),`utf8`)}catch(e){if(e instanceof Error&&`code`in e&&e.code===`ENOENT`)return;throw e}let i;try{i=JSON.parse(n)}catch{return}if(typeof i==`object`&&i&&!Array.isArray(i)&&`retiredAt`in i&&typeof i.retiredAt==`number`&&Number.isFinite(i.retiredAt)&&i.retiredAt>=0)return i.retiredAt}export{DEVELOPMENT_RUNTIME_ARTIFACTS_ACTIVATED_MARKER,pruneDevelopmentRuntimeArtifactsSnapshotDirectory,recordRetiredDevelopmentRuntimeArtifactsSnapshot};