UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

7 lines (5 loc) • 7.21 kB
import{l as e}from"./git-operation.worker-git-TpQcAIgG.mjs";import{Kt as t,ir as n}from"./git-operation.worker.js";import{i as r,r as i}from"./git-operation.worker-staged-inputs-DZiTgX91.mjs";import{C as a,d as o,n as s,s as c,u as l}from"./git-operation.worker-workspace-manifest-DsRMcvMP.mjs";import{i as u,r as d}from"./git-operation.worker-workspace-manifest-comparison-E7MDmqwp.mjs";import{t as f}from"./git-operation.worker-workspace-result-git-DNTVSnX7.mjs";import{reconciliationEntries as p}from"./git-operation.worker-workspace-reconcile-derived-paths-CuPCc3j9.mjs";import{a as m,o as h,r as g,s as _}from"./git-operation.worker-workspace-result-inventory-BcwQoY1w.mjs";import v from"node:fs/promises";import y from"node:path";import{createHash as b}from"node:crypto";async function loadStagedWorkerWorkspace(t,n,h,_){let v=m(n),y=await e(t,[`cat-file`,`commit`,v],{timeoutMs:f,maxOutputBytes:134221824});if(y.termination===`output-limit`)throw new a(`Worker workspace result metadata exceeds its 128 MiB safety limit.`);if(y.termination!==`exit`||y.code!==0)throw Error(y.stderr.toString(`utf8`).trim()||`git cat-file failed`);let b=y.stdout.indexOf(` `);if(b<0)throw Error(`Cloud workspace staged result metadata is invalid`);let x=y.stdout.subarray(b+2),S=x.indexOf(` `);if(S<0)throw Error(`Cloud workspace staged result metadata is invalid`);let C=x.subarray(0,S).toString(`utf8`).split(` `),w=C[1]===`version 1`?1:C[1]===`version 2`?2:C[1]===`version 3`?3:void 0,T=/^sha256:[a-f0-9]{64}$/u,E=C[2]?.slice(9)??``,D=C[3]?.slice(12)??``,O=Number(C[4]?.slice(11)),k=Number(C[5]?.slice(14));if(C[0]!==`OpenClaw worker workspace result`||w===void 0||!C[2]?.startsWith(`base-ref `)||!C[3]?.startsWith(`current-ref `)||!C[4]?.startsWith(`base-bytes `)||!C[5]?.startsWith(`current-bytes `)||C.length!==(w===3?7:6)||!T.test(E)||!T.test(D)||!Number.isSafeInteger(O)||O<0||!Number.isSafeInteger(k)||k<0)throw Error(`Cloud workspace staged result metadata is invalid`);let A=x.subarray(S+2);if(A.byteLength!==O+k)throw Error(`Cloud workspace staged result metadata is truncated`);let j=A.subarray(0,O).toString(`utf8`),M=A.subarray(O).toString(`utf8`),N=c(j,E,h),P=c(M,D,h),F=w===3&&C[6]?.startsWith(`git-history `)?g.parse(JSON.parse(C[6].slice(12))):void 0;if(w===3&&(!F||F.baseCommit!==N.baseCommit||F.descendsFromBase&&!F.unavailableReason&&!y.stdout.subarray(0,b).toString(`utf8`).split(` `).includes(`parent ${F.headCommit}`)))throw Error(`Cloud workspace staged Git history is invalid`);let I={baseNodes:d(N),currentNodes:d(P)},L=u(N,P,!(!_||_.alreadyAccepted||_.baseManifestRef!==E),I),R=L.entries,z=w===1?p(P.entries,void 0,h):R,B=await e(t,[`ls-tree`,`-r`,`-z`,`--full-tree`,v],{timeoutMs:f,maxOutputBytes:2*s});if(B.termination!==`exit`||B.code!==0)throw Error(B.stderr.toString(`utf8`).trim()||`git ls-tree failed`);let V=r(P.entries),H=l(h),U=/* @__PURE__ */ new Map;for(let e of B.stdout.toString(`utf8`).split(`\0`).filter(Boolean)){let t=/^(100644|100755|120000) blob ([a-f0-9]{40}|[a-f0-9]{64})\t([\s\S]+)$/u.exec(e);if(!t)throw Error(`Cloud workspace staged result tree is invalid`);let n=t[3];o(n,H.has(n)||i(n,V),h!==void 0)||U.set(n,{mode:t[1],objectId:t[2]})}if(U.size!==z.length)throw Error(`Cloud workspace staged result tree does not match its manifest`);for(let e of z){let t=U.get(e.path),n=e.type===`symlink`?`120000`:e.mode&73?`100755`:`100644`;if(!t||t.mode!==n)throw Error(`Cloud workspace staged result tree is invalid: ${e.path}`)}return{...F?{gitHistory:F}:{},baseManifestRaw:j,currentManifestRaw:M,baseManifestRef:E,currentManifestRef:D,base:N,current:P,nodes:I,changed:L.changed,changedEntries:R,objectsByPath:U}}function assertStagedEntryContent(e,t){if(!(e.type===`symlink`?t.toString(`utf8`)===e.target:t.byteLength===e.size&&b(`sha256`).update(t).digest(`hex`)===e.sha256))throw Error(`Cloud workspace staged result payload is invalid: ${e.path}`)}async function readStagedWorkerWorkspaceEntries(t){if(t.entries.length>256)throw Error(`Cloud workspace staged result batch exceeds its entry limit`);let n=0;for(let{object:e,entry:r}of t.entries){let t=_(r);if(!/^(?:[a-f0-9]{40}|[a-f0-9]{64})$/u.test(e.objectId)||!Number.isSafeInteger(t)||t<0||t>67108864)throw Error(`Cloud workspace staged result payload is invalid: ${r.path}`);n+=t}if(n>67108864)throw Error(`Cloud workspace staged result batch exceeds its byte limit`);if(t.entries.length===0)return Buffer.alloc(0);let r=await e(t.root,[`cat-file`,`--batch`],{input:Buffer.from(t.entries.map(({object:e})=>`${e.objectId}\n`).join(``)),timeoutMs:f,maxOutputBytes:n+t.entries.length*128+1});if(r.termination!==`exit`||r.code!==0)throw Error(r.stderr.toString(`utf8`).trim()||`git cat-file failed`);let i=[],a=0;for(let{object:e,entry:n}of t.entries){let t=r.stdout.indexOf(10,a),o=t>=a&&t-a<=128?/^([a-f0-9]{40}|[a-f0-9]{64}) blob (0|[1-9][0-9]*)$/u.exec(r.stdout.subarray(a,t).toString(`utf8`)):null,s=Number(o?.[2]),c=t+1,l=c+s;if(o?.[1]!==e.objectId||!Number.isSafeInteger(s)||s>67108864||n.type===`file`&&s!==n.size||l>=r.stdout.byteLength||r.stdout[l]!==10)throw Error(`Cloud workspace staged result payload is invalid: ${n.path}`);let u=r.stdout.subarray(c,l);assertStagedEntryContent(n,u),n.type===`file`&&i.push(u),a=l+1}if(a!==r.stdout.byteLength)throw Error(`Cloud workspace staged result contains unexpected payload bytes`);return i.length===1?i[0]:Buffer.concat(i)}async function materializeStagedWorkerWorkspace(e,t,r){let i=0;for(;i<r.changedEntries.length;){let a=[],o=0;for(;i<r.changedEntries.length;){let e=r.changedEntries[i],t=_(e);if(a.length===256||a.length>0&&o+t>67108864)break;a.push(h(r.objectsByPath,e)),o+=t,i++}let s=await readStagedWorkerWorkspaceEntries({root:e,entries:a});if(!t)continue;let c=0,l=a.map(({entry:e})=>{let n=e.type===`file`?s.subarray(c,c+e.size):void 0;return c+=e.type===`file`?e.size:0,async()=>{let r=y.join(t,e.path);await v.mkdir(y.dirname(r),{recursive:!0,mode:448}),e.type===`symlink`?await v.symlink(e.target,r):(await v.writeFile(r,n,{mode:e.mode,flag:`wx`}),await v.chmod(r,e.mode))}}),u=await n({tasks:l,limit:4,errorMode:`stop`});if(u.hasError)throw u.firstError}}async function collectStagedWorkerArtifacts(e){let n=await loadStagedWorkerWorkspace(e.root,e.ref),r=new Set(n.current.entries.map(e=>e.path)),i=new Set(n.base.entries.map(e=>e.path)),a=[...n.changedEntries.map(e=>({path:e.path,status:i.has(e.path)?`modified`:`added`,additions:0,deletions:0})),...n.base.entries.filter(e=>!r.has(e.path)).map(e=>({path:e.path,status:`deleted`,additions:0,deletions:0}))].toSorted((e,t)=>e.path.localeCompare(t.path)),o=n.changedEntries.find(t=>t.path===e.previewPath),s=n.changedEntries.find(t=>t.path===e.downloadPath),c=o?.type===`file`&&o.size<=262144?await readStagedWorkerWorkspaceEntries({root:e.root,entries:[h(n.objectsByPath,o)]}):void 0;return{baseManifestRef:n.baseManifestRef,currentManifestRef:n.currentManifestRef,base:{baseCommit:n.base.baseCommit},current:{baseCommit:n.current.baseCommit},changedEntries:n.changedEntries,changes:a,...c===void 0?{}:{preview:t(c)},...s?.type===`file`?{downloadObjectId:n.objectsByPath.get(s.path).objectId}:{}}}export{collectStagedWorkerArtifacts,loadStagedWorkerWorkspace,materializeStagedWorkerWorkspace};