UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

7 lines (5 loc) • 6.25 kB
import"./git-operation.worker-path-guards-B3dt-SRh.mjs";import{lt as e}from"./git-operation.worker.js";import{c as t}from"./git-operation.worker-git-C2q2uQ5t.mjs";import{f as n,p as r}from"./git-operation.worker-schemas-OtWwONNf.mjs";import{a as i,c as a,n as o,s}from"./git-operation.worker-workspace-manifest-Zk1wvpHZ.mjs";import{i as c}from"./git-operation.worker-workspace-manifest-comparison-B0otBJpG.mjs";import{a as l,i as u,o as d,r as f,s as p}from"./git-operation.worker-workspace-result-inventory-DsLkZp06.mjs";import"node:fs/promises";import"node:path";import{createHash as m}from"node:crypto";function reconciliationEntries(e,t=r(e),i){let o=s(i);return e.filter(e=>!a(e.path,o.has(e.path)||n(e.path,t),i!==void 0))}async function loadStagedWorkerWorkspace(e,l,d){let m=u(l),h=await t(e,[`cat-file`,`commit`,m],{timeoutMs:p,maxOutputBytes:134221824});if(h.termination!==`exit`||h.code!==0)throw Error(h.stderr.toString(`utf8`).trim()||`git cat-file failed`);let g=h.stdout.indexOf(` `);if(g<0)throw Error(`Cloud workspace staged result metadata is invalid`);let _=h.stdout.subarray(g+2),v=_.indexOf(` `);if(v<0)throw Error(`Cloud workspace staged result metadata is invalid`);let y=_.subarray(0,v).toString(`utf8`).split(` `),b=y[1]===`version 1`?1:y[1]===`version 2`?2:y[1]===`version 3`?3:void 0,x=/^sha256:[a-f0-9]{64}$/u,S=y[2]?.slice(9)??``,C=y[3]?.slice(12)??``,w=Number(y[4]?.slice(11)),T=Number(y[5]?.slice(14));if(y[0]!==`OpenClaw worker workspace result`||b===void 0||!y[2]?.startsWith(`base-ref `)||!y[3]?.startsWith(`current-ref `)||!y[4]?.startsWith(`base-bytes `)||!y[5]?.startsWith(`current-bytes `)||y.length!==(b===3?7:6)||!x.test(S)||!x.test(C)||!Number.isSafeInteger(w)||w<0||!Number.isSafeInteger(T)||T<0)throw Error(`Cloud workspace staged result metadata is invalid`);let E=_.subarray(v+2);if(E.byteLength!==w+T)throw Error(`Cloud workspace staged result metadata is truncated`);let D=E.subarray(0,w).toString(`utf8`),O=E.subarray(w).toString(`utf8`),k=i(D,S,d),A=i(O,C,d),j=b===3&&y[6]?.startsWith(`git-history `)?f.parse(JSON.parse(y[6].slice(12))):void 0;if(b===3&&(!j||j.baseCommit!==k.baseCommit||j.descendsFromBase&&!j.unavailableReason&&!h.stdout.subarray(0,g).toString(`utf8`).split(` `).includes(`parent ${j.headCommit}`)))throw Error(`Cloud workspace staged Git history is invalid`);let M=c(k,A,b!==1),N=M.entries,P=b===1?reconciliationEntries(A.entries,void 0,d):N,F=await t(e,[`ls-tree`,`-r`,`-z`,`--full-tree`,m],{timeoutMs:p,maxOutputBytes:2*o});if(F.termination!==`exit`||F.code!==0)throw Error(F.stderr.toString(`utf8`).trim()||`git ls-tree failed`);let I=r(A.entries),L=s(d),R=/* @__PURE__ */ new Map;for(let e of F.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 r=t[3];a(r,L.has(r)||n(r,I),d!==void 0)||R.set(r,{mode:t[1],objectId:t[2]})}if(R.size!==P.length)throw Error(`Cloud workspace staged result tree does not match its manifest`);for(let e of P){let t=R.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{...j?{gitHistory:j}:{},baseManifestRaw:D,currentManifestRaw:O,baseManifestRef:S,currentManifestRef:C,base:k,current:A,changed:M.changed,changedEntries:N,objectsByPath:R}}function assertStagedEntryContent(e,t){if(!(e.type===`symlink`?t.toString(`utf8`)===e.target:t.byteLength===e.size&&m(`sha256`).update(t).digest(`hex`)===e.sha256))throw Error(`Cloud workspace staged result payload is invalid: ${e.path}`)}async function readStagedWorkerWorkspaceEntries(e){if(e.entries.length>256)throw Error(`Cloud workspace staged result batch exceeds its entry limit`);let n=0;for(let{object:t,entry:r}of e.entries){let e=d(r);if(!/^(?:[a-f0-9]{40}|[a-f0-9]{64})$/u.test(t.objectId)||!Number.isSafeInteger(e)||e<0||e>67108864)throw Error(`Cloud workspace staged result payload is invalid: ${r.path}`);n+=e}if(e.entries.length>1&&n>8388608)throw Error(`Cloud workspace staged result batch exceeds its byte limit`);if(e.entries.length===0)return Buffer.alloc(0);let r=await t(e.root,[`cat-file`,`--batch`],{input:Buffer.from(e.entries.map(({object:e})=>`${e.objectId}\n`).join(``)),timeoutMs:p,maxOutputBytes:n+e.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:t,entry:n}of e.entries){let e=r.stdout.indexOf(10,a),o=e>=a&&e-a<=128?/^([a-f0-9]{40}|[a-f0-9]{64}) blob (0|[1-9][0-9]*)$/u.exec(r.stdout.subarray(a,e).toString(`utf8`)):null,s=Number(o?.[2]),c=e+1,l=c+s;if(o?.[1]!==t.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 collectStagedWorkerArtifacts(t){let n=await loadStagedWorkerWorkspace(t.root,t.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(e=>e.path===t.previewPath),s=n.changedEntries.find(e=>e.path===t.downloadPath),c=o?.type===`file`&&o.size<=262144?await readStagedWorkerWorkspaceEntries({root:t.root,entries:[l(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:e(c)},...s?.type===`file`?{downloadObjectId:n.objectsByPath.get(s.path).objectId}:{}}}export{collectStagedWorkerArtifacts,loadStagedWorkerWorkspace,readStagedWorkerWorkspaceEntries};