openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
6 lines • 5.63 kB
JavaScript
import{c as sha256File}from"./directory-durability-o_aQCRBZ.mjs";import{r as resolveStateDir}from"./state-dir-BwIClCab.mjs";import{x as root}from"./fs-safe-Bxzu1j4h.mjs";import{t as hasErrnoCode}from"./errno-DUBt6SQQ.mjs";import{l as resolveAgentIdFromSessionKey}from"./session-key-BPgKfU2m.mjs";import{t as _usingCtx}from"./usingCtx-CUVyQ_FT.mjs";import{t as shellEscape}from"./shell-quote-BethZJ3T.mjs";import{f as parseWorkspaceManifestPair}from"./workspace-manifest-worker-C4V2H1Gh.mjs";import{c as isDerivedWorkspacePath}from"./workspace-path-exclusions-Cmi1311P.mjs";import{n as MAX_RECONCILIATION_FILE_BYTES,s as manifestPath}from"./workspace-manifest-BE9NudAI.mjs";import{l as workspaceSizeRecoveryPaths,t as OVERSIZED_WORKSPACE_TOTAL_ERROR_PREFIX}from"./workspace-manifest-comparison-DFPdvJq-.mjs";import{a as hasWorkerWorkspaceResultRef}from"./workspace-result-staging-BsmW1zVW.mjs";import{o as withLocalWorkspaceProjection,r as resolveLocalWorkspaceOwner}from"./local-workspace-projection-DBuPISyh.mjs";import path from"node:path";
//!src/gateway/worker-environments/local-workspace-recovery.ts
function recoveryPath(e){const t=manifestPath(e);if(t.split(`/`).some(e=>e.toLowerCase()===`.git`)||isDerivedWorkspacePath(t,false,true))throw new Error(`Select a retained workspace file and a UUID recovery ID`);return t}async function hasAggregateOverflow(e){const t=await e.baselineDocument();const n=await e.buildCacheInputs();const r=await e.capture(`canonical`);try{await parseWorkspaceManifestPair({baseRaw:t.rawManifest,baseRef:t.manifestRef,currentRaw:r.rawManifest,currentRef:r.manifestRef,buildCacheInputs:n})}catch(e){if(e instanceof Error&&e.message.startsWith(`Cloud workspace staged result exceeds its byte limit`))return true;throw e}return false}async function openRecoverableFile(e,t,n,r,i){const a=t.current();if(a.journal_json||!a.pending_ref||a.pending_target!==`canonical`||await hasWorkerWorkspaceResultRef({root:e.worktree.repoRoot,stagedResultRef:a.pending_ref}))throw new Error(`Recovery requires an unstaged guest checkpoint; existing receipts were preserved`);if((await t.canonicalPaths()).has(r))throw new Error(`Cannot relocate a source file already admitted to the canonical workspace`);const o=path.relative(n.rootReal,a.projection_path)+`/`+r;const s=await n.open(o);try{if(s.stat.size<=67108864&&(s.stat.size===0||!(i??await hasAggregateOverflow(t))))throw new Error(`Selected file does not exceed the 64 MiB limit and no aggregate overflow requires recovery`)}catch(e){await s[Symbol.asyncDispose]();throw e}return s}async function describeLocalWorkspaceRecovery(e,t,n){const r=workspaceSizeRecoveryPaths(e);for(const i of r)try{const r=recoveryPath(i);await(await openRecoverableFile(t,n,await root(resolveStateDir(t.env),{symlinks:`reject`,hardlinks:`reject`}),r,e.message.startsWith(OVERSIZED_WORKSPACE_TOTAL_ERROR_PREFIX)))[Symbol.asyncDispose]();n.current();return new Error(e.message+`
Pending workspace changes were preserved. Ask a Gateway administrator to run:
openclaw sandbox recover --session `+shellEscape(t.sessionKey)+` --path `+shellEscape(r)+`
Run this command in Bash or zsh on the Gateway host, then retry the session. The selected file is preserved outside workspace synchronization. Repeat for another selected file if the remaining payload is still too large.`,{cause:e})}catch{}return e}async function recoverLocalWorkspaceFile(e){try{var t=_usingCtx();const n=recoveryPath(e.path);if(!/^[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}$/u.test(e.recoveryId))throw new Error(`Select a retained workspace file and a UUID recovery ID`);const r=t.u(await resolveLocalWorkspaceOwner({cfg:e.cfg,agentId:resolveAgentIdFromSessionKey(e.sessionKey),sessionKey:e.sessionKey,env:e.env,assertCurrent:e.assertCurrent}));if(!r)throw new Error(`Session has no local managed workspace to recover`);return await withLocalWorkspaceProjection(r,async t=>{try{var i=_usingCtx();const current=()=>{e.signal?.throwIfAborted();return t.current()};const o=current();const s=await root(resolveStateDir(r.env),{symlinks:`reject`,mutationSymlinks:`reject`,hardlinks:`reject`,assertBeforeMutation:current});const c=path.relative(s.rootReal,o.projection_path)+`/`+n;const l=`workspace-recovery/`+r.worktree.id+`/`+e.recoveryId+`/`+n;const u=path.join(s.rootReal,l);const result=async()=>{try{var e=_usingCtx();const t=e.a(await s.open(l));current();return{path:u,bytes:t.stat.size}}catch(t){e.e=t}finally{await e.d()}};if(await s.exists(l)){if(await s.exists(c))throw new Error(`Both file names exist; inspect retained artifact at `+u);return await result()}const d=i.a(await openRecoverableFile(r,t,s,n));await s.mkdir(path.dirname(l),{private:true});current();try{try{await s.move(c,l)}catch(t){try{var a=_usingCtx();if(!hasErrnoCode(t,`EXDEV`))throw t;await s.copyIn(l,{root:s,relativePath:c},{overwrite:false,maxBytes:d.stat.size,preserveSourceMode:true,signal:e.signal});const n=a.a(await s.open(l));await n.handle.sync();const r=await sha256File(d.handle,{maxBytes:d.stat.size,signal:e.signal});const i=await sha256File(n.handle,{maxBytes:d.stat.size,signal:e.signal});if(r.bytes!==d.stat.size||i.bytes!==r.bytes||i.digest!==r.digest)throw new Error(`Recovery copy does not match the retained file`,{cause:t});await s.remove(c)}catch(e){a.e=e}finally{await a.d()}}}catch(e){throw new Error(`Recovery move did not finish; inspect `+u+` before retrying`,{cause:e})}current();return{path:u,bytes:d.stat.size}}catch(e){i.e=e}finally{await i.d()}},{signal:e.signal})}catch(e){t.e=e}finally{t.d()}}export{recoverLocalWorkspaceFile as n,describeLocalWorkspaceRecovery as t};