UNPKG

eve

Version:

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

1 lines 3.25 kB
import{randomUUID}from"node:crypto";import{EVE_DEVELOPMENT_SANDBOX_METADATA_PATH_TAG,EVE_DEVELOPMENT_SANDBOX_RUN_ID_TAG}from"#execution/sandbox/development-run.js";import{toErrorMessage}from"#shared/errors.js";import{DockerUnavailableError,createDockerCli}from"#execution/sandbox/bindings/docker-cli.js";import{DOCKER_SANDBOX_LABEL,stopDockerContainerIfRunning}from"#execution/sandbox/bindings/docker-container.js";import{MICROSANDBOX_METADATA_VERSION,readSessionMetadata,writeSessionMetadata}from"#execution/sandbox/bindings/microsandbox-metadata.js";import{createProviderName,loadMicrosandboxWithoutInstall,removeSnapshotIfExists,stopAndSnapshotMicrosandboxSandbox}from"#execution/sandbox/bindings/microsandbox-runtime.js";async function stopDevelopmentSandboxResources(e){let t=await Promise.allSettled([stopDevelopmentDockerResources(e.devRunId),stopDevelopmentMicrosandboxResources(e.appRoot,e.devRunId,e.log)]);for(let n of t)n.status===`rejected`&&e.log?.(`failed to stop development sandbox resources: ${toErrorMessage(n.reason)}`)}async function stopDevelopmentDockerResources(e){let t=createDockerCli(),n=[`label=${DOCKER_SANDBOX_LABEL}=1`,`label=${DOCKER_SANDBOX_LABEL}.tag.${EVE_DEVELOPMENT_SANDBOX_RUN_ID_TAG}=${e}`],r;try{r=await t.run([`ps`,`-q`,...n.flatMap(e=>[`--filter`,e])])}catch(e){if(e instanceof DockerUnavailableError)return;throw e}if(r.exitCode!==0)return;let i=r.stdout.trim().split(/\s+/u).filter(Boolean);await Promise.all(i.map(e=>stopDockerContainerIfRunning(t,e)))}async function stopDevelopmentMicrosandboxResources(e,t,n){let r=await loadMicrosandboxWithoutInstall(e);if(r===null)return;let i=await r.Sandbox.listWith({labels:{"eve.backend":`microsandbox`,[EVE_DEVELOPMENT_SANDBOX_RUN_ID_TAG]:t}});await Promise.all(i.filter(e=>e.status===`running`||e.status===`draining`).map(async e=>{let t=getMicrosandboxLabel(e.configJson,EVE_DEVELOPMENT_SANDBOX_METADATA_PATH_TAG);if(t===void 0){await(await r.Sandbox.get(e.name)).stopWithTimeout(1e4).catch(()=>{});return}let i=await readSessionMetadata(t);if(i===null||i.sandboxName!==e.name){await(await r.Sandbox.get(e.name)).stopWithTimeout(1e4).catch(()=>{});return}n?.(`snapshotting development microsandbox session "${e.name}" before shutdown`);let a=i.stateSnapshotName,o=createProviderName(`eve-sbx-state`,`${i.sandboxName}:${randomUUID()}`);await stopAndSnapshotMicrosandboxSandbox(r,e.name,o),await writeSessionMetadata(t,{networkPolicy:i.networkPolicy,optionsHash:i.optionsHash,sandboxName:i.sandboxName,stateSnapshotName:o,version:MICROSANDBOX_METADATA_VERSION}),a!==void 0&&await removeSnapshotIfExists(r,a)}))}function getMicrosandboxLabel(e,t){let n;try{n=JSON.parse(e)}catch{return}return findLabel(n,t)}function findLabel(e,t){if(!isRecord(e))return;let n=e[t];if(typeof n==`string`)return n;let r=e.labels;if(isRecord(r)&&typeof r[t]==`string`)return r[t];if(typeof e.labelsJson==`string`){let n=parseJsonRecord(e.labelsJson);if(n!==null&&typeof n[t]==`string`)return n[t]}for(let n of Object.values(e)){let e=findLabel(n,t);if(e!==void 0)return e}}function parseJsonRecord(e){try{let t=JSON.parse(e);return isRecord(t)?t:null}catch{return null}}function isRecord(e){return typeof e==`object`&&!!e&&!Array.isArray(e)}export{stopDevelopmentSandboxResources};