UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 3.21 kB
import{i as listAgentIds}from"./agent-roster-4XrgUgPK.mjs";import"./agent-scope-config-CG9CFpGX.mjs";import{t as _usingCtx}from"./usingCtx-CUVyQ_FT.mjs";import{t as createSubsystemLogger}from"./subsystem-DLCDFvCH.mjs";import{i as resolveSandboxConfigForAgent}from"./config-CNA3Oz0X.mjs";import{n as worktreeTemplateMutationKey}from"./template-registry-CUQpl5qv.mjs";import{t as IDLE_GC_MS}from"./retention-Br7rnRK1.mjs";import fs from"node:fs/promises"; //!src/gateway/worker-environments/local-workspace-prewarm.ts const log=createSubsystemLogger(`agents/worktrees`);async function prewarmLocalWorkspaceTemplates(e){const t=e.getConfig();if(t.worktreeAcceleration===false)return;const{signal:n}=e;const commitGuard=()=>{n.throwIfAborted();if(e.getConfig()!==t)throw new Error(`Sandbox dependency prewarm configuration was replaced`)};const r=process.env;const{listTemplatesAsync:i}=await import(`./template-registry-async-CjMojYPG.mjs`);const a=Date.now()-IDLE_GC_MS;const o=(await i(r,commitGuard)).filter(e=>e.backend.startsWith(`sandbox-`)&&e.lastUsedAt>a);if(!o.length)return;const s=/* @__PURE__ */ new Set;for(const i of listAgentIds(t)){const a=resolveSandboxConfigForAgent(t,i);if(a.backend!==`docker`&&a.backend!==`podman`)continue;try{commitGuard();const[{withWorktreeMutationLease:t},{resolveWorktreeBase:c,observeWorktreeBase:l},{resolveRepository:u},{withWorktreeRunEnd:d},{withWorktreeSources:f},{prepareLocalWorkspaceTemplate:p,sandboxTemplateCacheKey:m},{resolveSandboxDependencyTemplateIdentity:h}]=await Promise.all([import(`./allocation-DNN7sp8b.mjs`),import(`./base-ref-CTcUr7RL.mjs`),import(`./repository-paths-Db3gfXMq.mjs`),import(`./run-end-lifecycle-YMXEG6cP.mjs`),import(`./service-preparation-zUVtwD8_.mjs`),import(`./local-workspace-template-SJhQwIsz.mjs`),import(`./dependency-template-D_koSig-.mjs`)]);commitGuard();const g=await h(a,{signal:n,assertCurrent:commitGuard});if(!g)continue;const _=o.filter(e=>worktreeTemplateMutationKey(e.cacheKey)===m(e.commonDir,e.worktreeRoot,g.key)).toReversed();for(const o of _){if(await e.shouldDefer?.())return;const m=JSON.stringify([o.repoRoot,o.worktreeRoot,g.key]);if(s.has(m))continue;s.add(m);try{const e=await u(o.repoRoot);await d(r,()=>t({env:r,signal:n,commitGuard,id:`template-prewarm:${i}`},t=>f(r,async i=>{try{var s=_usingCtx();await i({...t,repository:e});const u=await c(e.repoRoot,void 0,n,t.commitGuard);const d=o.worktreeRoot;const f=new AbortController;s.u(l((t,n)=>{if(t===e.repoRoot&&n!==u.commit)f.abort(/* @__PURE__ */ new Error(`Sandbox dependency prewarm base was superseded`))}));t.commitGuard();await fs.mkdir(d,{recursive:true,mode:448});await(await p({source:e.repoRoot,repoRoot:e.repoRoot,baseCommit:u.commit,temporaryRoot:d,templateRoot:d,env:r,sandbox:a,identity:g,backgroundUsedAt:o.lastUsedAt,buildCacheInputs:[],guard:{...t,signal:AbortSignal.any([t.signal??n,f.signal])}}))?.record.release()}catch(e){s.e=e}finally{s.d()}})))}catch{commitGuard();log.debug(`sandbox dependency template prewarm unavailable for repository ${o.repoRoot}`)}}}catch{if(n.aborted||e.getConfig()!==t)return;log.debug(`sandbox dependency template prewarm unavailable for agent ${i}`)}}}export{prewarmLocalWorkspaceTemplates};