UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

1 lines • 10.2 kB
import{a as e}from"./git-operation.worker-fs-safe-2XqdJR5K.mjs";import{Kt as t,Ln as n,Tn as r,Vt as i}from"./git-operation.worker.js";import{n as a}from"./git-operation.worker-exec-runner-D4RJRnE_.mjs";import{n as o,r as s}from"./git-operation.worker-exec-Cxdez5wI.mjs";import{a as c,i as l,n as u,r as d}from"./git-operation.worker-staged-inputs-DZiTgX91.mjs";import{C as f,g as p,i as m,r as h,u as g}from"./git-operation.worker-workspace-manifest-DsRMcvMP.mjs";import{o as _}from"./git-operation.worker-workspace-manifest-comparison-E7MDmqwp.mjs";import{prepareNonDirectoryTargets as v,reconciliationDirectories as y,reconciliationEntries as b}from"./git-operation.worker-workspace-reconcile-derived-paths-CuPCc3j9.mjs";import{a as x,directoryContainsOnlyWorkspaceEntries as S,i as C,localPath as w,localWorkspaceNode as T,n as E,r as D,removeEmptyWorkspaceDirectory as O,t as k}from"./git-operation.worker-workspace-reconcile-fs-2rlVLgX2.mjs";import{assertWorkspaceMatchesManifest as A}from"./git-operation.worker-workspace-reconcile-core-Ci1PG_NC.mjs";import j from"node:fs";import M from"node:fs/promises";import N from"node:path";import{createHash as P,randomBytes as F}from"node:crypto";const I=6e5;function workspaceRecoveryConflict(e){return/* @__PURE__ */ Error(`Gateway workspace changed while cloud recovery was pending: ${e}`)}async function requireGit(e,t,n,r,i){let o=await a([`git`,`-C`,e,...t],{timeoutMs:I,...n?{input:n}:{},...r?{env:r}:{},...i?{beforeInput:i}:{},maxOutputBytes:1048576});if(o.termination!==`exit`||o.code!==0)throw Error((o.stderr||o.stdout||`git ${t[0]} failed`).trim());return o.stdout.trim()}async function writeRawWorkspaceTree(e){let t=e.repositoryRoot,r=`refs/heads/openclaw-snapshot-${F(16).toString(`hex`)}`,i=N.join(t,`.git`,`snapshot-input`);try{try{var a=n();await D({...e,inputPath:i,ref:r});let o=a.a(await M.open(i,`r`)),{stdout:c}=await s(`git`,[`-C`,t,`fast-import`,`--quiet`],{stdinFileDescriptor:o.fd,timeoutMs:I,maxBuffer:1048576});return c.split(` `)[2].split(` `)[0]}catch(e){a.e=e}finally{await a.d()}}finally{await M.rm(i,{force:!0})}}async function readWorkspacePatch(e,n,r){let i=N.join(e,`.git`,`workspace.patch`),o=await M.open(i,`wx`,384),s=0,c=!1;try{let t=await a([`git`,`-C`,e,`diff`,`--binary`,`--full-index`,`--no-renames`,n,r,`--`],{timeoutMs:I,killProcessTree:!0,outputCapture:{stdout:`discard`,stderr:`head`},maxOutputBytes:{stdout:m,stderr:1048576},terminateOnOutputLimit:!0,onOutputChunk:(e,t)=>{if(t!==`stdout`)return!0;if(s+e.byteLength>805306368)return c=!0,!1;let n=0;for(;n<e.byteLength;){let t=j.writeSync(o.fd,e,n,e.byteLength-n);if(t===0)throw Error(`Cloud workspace patch write made no progress`);n+=t}return s+=e.byteLength,!0}});if(c)throw new f(`Cloud workspace patch exceeds its byte limit`);if(t.termination!==`exit`||t.code!==0)throw Error(t.stderr.trim()||`git diff failed`)}finally{await o.close()}return t(await M.readFile(i))}async function createWorkspacePatch(e){let n=await M.mkdtemp(N.join(r(),`openclaw-workspace-patch-`));try{await requireGit(n,[`init`,`--quiet`,`--object-format=sha1`]);let r=0;for(let t of e.baseEntries){if(t.type===`file`&&t.size>67108864)throw new f(`Cloud workspace rollback file is too large: ${t.path}`);if(r+=t.type===`file`?t.size:Buffer.byteLength(t.target),r>805306368)throw new f(`Cloud workspace rollback exceeds its byte limit`)}let i=await writeRawWorkspaceTree({repositoryRoot:n,entries:e.baseEntries,source:{root:e.root}}),a=await o([`git`,`-C`,n,`pack-objects`,`--stdout`,`--revs`],{input:Buffer.from(`${i}\n`),timeoutMs:I,maxOutputBytes:{stdout:h+1,stderr:1048576}});if(a.stdout.byteLength>268435456)throw new f(`Cloud workspace recovery snapshot exceeds its byte limit`);if(a.termination!==`exit`||a.code!==0)throw Error(a.stderr.toString(`utf8`).trim()||`git pack-objects failed`);let s={baseTree:i,basePack:t(a.stdout),publicationPack:e.publicationCopy?new Uint8Array(a.stdout):void 0,basePackSha256:P(`sha256`).update(a.stdout).digest(`hex`)};if(e.baseEntries.length===0&&e.appliedEntries.length===0)return{...s,patch:/* @__PURE__ */ new Uint8Array};let c=await readWorkspacePatch(n,i,await writeRawWorkspaceTree({repositoryRoot:n,entries:e.appliedEntries,source:{root:e.stagingRoot}}));return{...s,patch:c}}finally{await M.rm(n,{recursive:!0,force:!0})}}async function applyWorkspacePatch(e){if(e.patch.byteLength===0)return;let t=await M.mkdtemp(N.join(r(),`openclaw-no-git-`));try{await requireGit(e.root,[`-c`,`core.autocrlf=false`,`apply`,`--no-index`,`--binary`,`--whitespace=nowarn`],e.patch,{GIT_DIR:N.join(t,`.git`)},e.assertCurrent)}finally{await M.rm(t,{recursive:!0,force:!0})}}function validateJournalSnapshot(e){if(e.basePack.byteLength>268435456||!/^[a-f0-9]{40}$/u.test(e.baseTree)||P(`sha256`).update(e.basePack).digest(`hex`)!==e.basePackSha256)throw Error(`Cloud workspace reconciliation recovery snapshot is invalid`)}async function createWorkspaceRecoveryPatch(e){let t=await M.mkdtemp(N.join(r(),`openclaw-workspace-recovery-`));try{await requireGit(t,[`init`,`--quiet`,`--object-format=sha1`]),await requireGit(t,[`index-pack`,`--stdin`],e.journal.basePack),await requireGit(t,[`cat-file`,`-e`,`${e.journal.baseTree}^{tree}`]);let n=e.journal.baseEntries,r=e.journal.appliedEntries,i=new Map(n.map(e=>[e.path,e])),a=new Map(r.map(e=>[e.path,e])),o=/* @__PURE__ */ new Set([...i.keys(),...a.keys()]),s=/* @__PURE__ */ new Set;for(let e of o)for(let t of p(e))s.add(t);let c=[],l=await C(e.root,[...o]);for(let t of[...o].toSorted()){let n=l.get(t);if(!n){let e=i.get(t),n=a.get(t);if(e&&n)throw workspaceRecoveryConflict(t);continue}let r=i.get(t),u=a.get(t);if(r&&_(n,r))c.push(r);else if(u&&_(n,u))c.push(u);else if(!(n.type===`directory`&&(s.has(t)&&await S(e.root,t,e.isRetainedInput,{paths:o,directories:s},e.excludeBuildCaches)||await S(e.root,t,e.isRetainedInput,void 0,e.excludeBuildCaches))))throw workspaceRecoveryConflict(t)}let u=await writeRawWorkspaceTree({repositoryRoot:t,entries:c,source:{root:e.root}}),d=e.filteredBaseTree?await writeRawWorkspaceTree({repositoryRoot:t,entries:n,source:{root:t,tree:e.journal.baseTree}}):e.journal.baseTree;return await k({type:`workspace.reconcile.diff`,input:{root:t,baseTree:u,currentTree:d}})}finally{await M.rm(t,{recursive:!0,force:!0})}}async function assertWorkspaceRecoveryBase(e){let t=e.journal.baseEntries,n=e.journal.appliedEntries;await A({root:e.root,manifest:{version:1,baseCommit:null,entries:e.journal.baseEntries},entries:t});let r=new Set(e.journal.baseDirectories??[]),i=new Set(e.journal.appliedDirectories??[]);for(let t of r)if((await T(e.root,t))?.type!==`directory`)throw workspaceRecoveryConflict(t);let a=new Set(t.map(e=>e.path)),o=/* @__PURE__ */ new Set;for(let e of a)for(let t of p(e))o.add(t);for(let t of n){if(a.has(t.path))continue;let n=await M.lstat(w(e.root,t.path)).catch(()=>void 0);if(!(n?.isDirectory()&&!n.isSymbolicLink()&&o.has(t.path)&&await S(e.root,t.path,e.isRetainedInput,{paths:a,directories:o},e.excludeBuildCaches))&&n)throw workspaceRecoveryConflict(t.path)}for(let t of i){if(r.has(t)||a.has(t))continue;let n=await T(e.root,t);if(n&&!(n.type===`directory`&&await S(e.root,t,e.isRetainedInput,void 0,e.excludeBuildCaches)))throw workspaceRecoveryConflict(t)}}async function assertWorkspaceRecoveryDirectoriesRecoverable(e){let t=new Set(e.journal.baseDirectories??[]),n=new Set(e.journal.appliedDirectories??[]),r=new Map(e.journal.baseEntries.map(e=>[e.path,e])),i=new Map(e.journal.appliedEntries.map(e=>[e.path,e])),a=new Set(i.keys()),o=/* @__PURE__ */ new Set([...t,...n]);for(let s of o){let o=await T(e.root,s);if(o?.type===`directory`){if(r.has(s)&&n.has(s)&&!await S(e.root,s,e.isRetainedInput,{paths:a,directories:n},e.excludeBuildCaches))throw workspaceRecoveryConflict(s);continue}if(!o){if(t.has(s)&&n.has(s))throw workspaceRecoveryConflict(s);continue}let c=r.get(s),l=i.get(s);if(!(c&&_(o,c)||l&&_(o,l)))throw workspaceRecoveryConflict(s)}}async function restoreWorkspaceJournalDirectories(t){let n=await e(t.root,{mode:448,assertBeforeMutation:t.assertCurrent}),r=t.journal.baseDirectories??[],i=new Set(t.journal.appliedDirectories??[]);for(let e of r.toSorted())await n.mkdir(e);let a=new Set(r),o=new Set(t.journal.baseEntries.map(e=>e.path));for(let e of[...i].toSorted((e,t)=>t.localeCompare(e)))a.has(e)||o.has(e)||await O(n,e)}async function recoverWorkerWorkspaceReconciliation(t){if(t.journal.appliedManifestRef)throw Error(`Cloud workspace result is already applied and awaits fence acceptance`);if(!i()){let e=await E(`openclaw-workspace-recovery-input-`);try{let n=N.join(e,`base.pack`),{basePack:r,...i}=t.journal;return await M.writeFile(n,r),await x({type:`workspace.reconcile.recover`,input:{root:t.root,journal:i,basePackPath:n,buildCacheInputs:t.buildCacheInputs}},t)}finally{await M.rm(e,{recursive:!0,force:!0})}}let n=await M.realpath(t.root);validateJournalSnapshot(t.journal);let r=u(await e(n)),a=g(t.buildCacheInputs),o=[...t.journal.baseEntries,...t.journal.appliedEntries],s=l(o),f=[...o.map(e=>e.path),...t.journal.baseDirectories??[],...t.journal.appliedDirectories??[]];for(let e of f){let t=c(e);t&&!s.has(t)&&await r(e)&&s.add(t)}let p={...t.journal,baseEntries:b(t.journal.baseEntries,s,t.buildCacheInputs),appliedEntries:b(t.journal.appliedEntries,s,t.buildCacheInputs),baseDirectories:y(t.journal.baseDirectories,s,t.buildCacheInputs),appliedDirectories:y(t.journal.appliedDirectories,s,t.buildCacheInputs)},isRetainedInput=async e=>a.has(e)||d(e,s)||await r(e),m={root:n,journal:p,isRetainedInput,filteredBaseTree:p.baseEntries.length!==t.journal.baseEntries.length,excludeBuildCaches:t.buildCacheInputs!==void 0,assertCurrent:t.assertCurrent};try{await assertWorkspaceRecoveryBase(m);return}catch{}await assertWorkspaceRecoveryDirectoriesRecoverable(m);let h=await createWorkspaceRecoveryPatch(m);await v(n,p.baseEntries,isRetainedInput,t.assertCurrent,t.buildCacheInputs!==void 0),await applyWorkspacePatch({root:n,patch:h,assertCurrent:t.assertCurrent}),await restoreWorkspaceJournalDirectories(m),await assertWorkspaceRecoveryBase(m)}export{applyWorkspacePatch,createWorkspacePatch,readWorkspacePatch,recoverWorkerWorkspaceReconciliation};