openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
1 lines • 12.8 kB
JavaScript
import{a as e}from"./git-operation.worker-record-coerce-BUlpHiHI.mjs";import{i as t}from"./git-operation.worker-git-TpQcAIgG.mjs";import{l as n}from"./git-operation.worker-git-path-inventory-BTOjkdj9.mjs";import{a as r}from"./git-operation.worker-staged-inputs-DZiTgX91.mjs";import{c as i,f as a,g as o,l as s,s as c,u as l}from"./git-operation.worker-workspace-manifest-DsRMcvMP.mjs";import{i as u}from"./git-operation.worker-workspace-actual-manifest-Y3A72WaR.mjs";import{o as d}from"./git-operation.worker-workspace-manifest-comparison-E7MDmqwp.mjs";import{a as f,c as p,d as ee,i as m,l as h,n as g,o as _,r as v,s as y,t as b,u as x}from"./git-operation.worker-workspace-manifest-computation.runtime-DBgiwfnP.mjs";import{createHash as S}from"node:crypto";import{serialize as C}from"node:v8";import{isUtf8 as w}from"node:buffer";import{deflateRawSync as T}from"node:zlib";function shellEscape(e){return`'${e.replaceAll(`'`,`'"'"'`)}'`}function decodeLocalWorkspaceGitPaths(e){if(!w(e))throw Error(`Local sandbox source paths must be UTF-8; rename the invalid Git path before retrying`);return e.toString(`utf8`).split(`\0`).filter(Boolean)}function parseLocalWorkspaceSourcePaths(t){let n=JSON.parse(t);if(!e(n))throw Error(`Invalid local workspace source admission`);return n}async function readLocalWorkspaceGitExclusions(e,n,r){let i=await t(e,[`--work-tree=${n}`,`ls-files`,`--full-name`,`--others`,`--ignored`,`--exclude-standard`,`--directory`,`-z`]);return{paths:new Set(decodeLocalWorkspaceGitPaths(i)),retainedPaths:r}}async function selectLocalWorkspaceCanonicalPaths(e){e.signal?.throwIfAborted(),e.assertCurrent();let n;if(e.preparedPaths)n=new Set(e.preparedPaths);else{let r=await t(e.root,[`ls-files`,`--cached`,`-z`],{signal:e.signal,beforeRun:e.assertCurrent});e.assertCurrent(),n=new Set(e.admittedPaths);for(let e of decodeLocalWorkspaceGitPaths(r))n.add(e)}if(e.baselinePaths)for(let t of e.baselinePaths)n.add(t);for(let e of n){let t=e.lastIndexOf(`/`);t!==-1&&n.add(e.slice(0,t))}return n}async function selectArchiveManifest(e){let{signal:r,assertCurrent:i}=e;if(!/^[a-f0-9]{40}(?:[a-f0-9]{24})?$/u.test(e.snapshotCommit))throw Error(`Invalid archive snapshot commit`);let a=await t(e.root,[`ls-tree`,`-r`,`-z`,`--name-only`,e.snapshotCommit],{signal:r,beforeRun:i,maxOutputBytes:67108864});i();let s=new Set(n(a).map(e=>e.toString(`hex`))),c=e.manifest.entries.filter(e=>s.has(Buffer.from(e.path).toString(`hex`))),l=/* @__PURE__ */ new Set;for(let e of c)for(let t of o(e.path))l.add(t);return c.length===e.manifest.entries.length&&l.size===(e.manifest.directories?.length??0)&&e.manifest.directories?.every(e=>l.has(e))!==!1?e.manifest:{...e.manifest,entries:c,directories:[...l].toSorted()}}function localWorkspaceManifestDocument(e,t,n,r=`current`){let i={version:1,baseCommit:t,entries:[],directories:[],...n?{buildCacheInputs:n}:{}};for(let t of e)t.type===`directory`?i.directories.push(t.path):i.entries.push(t);return archiveDocument(i,r)}function archiveDocument(e,t=`current`){let n=i(e,t);return{rawManifest:n,manifestRef:`sha256:`+S(`sha256`).update(n).digest(`hex`)}}async function prepareLocalWorkspaceArchive(e){let t={version:1,baseCommit:e.baseCommit,buildCacheInputs:e.buildCacheInputs,entries:[],directories:[]};for(let{node:n}of e.records.values())n.type===`directory`?n.path!==``&&t.directories.push(n.path):t.entries.push(n);let n=await selectArchiveManifest({...e,manifest:t,assertCurrent:()=>void 0});return n===t?{kind:`archive`}:{kind:`archive`,overlay:{base:archiveDocument(n),accepted:archiveDocument(t)}}}function localWorkspaceFingerprintPatch(e){let t={};if(e){let{target:n,blob:r,oppositeBlob:i}=e;for(let[e,a]of[[n,r],[n===`canonical`?`projection`:`canonical`,i]])if(a){let n=Buffer.from(a,`base64`);t[`${e}_fingerprints`]=new Uint8Array(n.buffer,n.byteOffset,n.byteLength)}}return t}function prepareLocalWorkspaceUpdate(e,t=[]){let n=e.manifest,r={...e.patch,...n?{baseline_ref:n.resultingRoot,baseline_json:null}:{},...n?.treeRoot?{tree_root_digest:m(n.treeRoot)}:{},...localWorkspaceFingerprintPatch(n?.fingerprints)},i=r;if(typeof r.source_paths_json==`string`){let e=_(r.source_paths_json);t.push(T(C({paths:e}))),r.source_paths_digest=m(e.digest),i={...r,source_paths_json:`[]`}}return{revision:e.revision,patch:r,rejectedResult:e.rejectedResult,mutation:g().compress(C({...e,patch:i}),1),preparations:t}}async function executeLocalWorkspaceManifestRead(e,t,n){let{row:i}=e;if(!n&&!e.records)throw Error(`Local workspace prepared baseline is unavailable`);let g=n??v(e.records,!i.tree_root_digest);if(e.records=[],t.kind===`repair`){if(!i.baseline_ref)throw Error(`Local workspace repair requires its saved baseline reference`);let e=i.tree_root_digest?f(i.tree_root_digest):i.baseline_ref,n=l(parseLocalWorkspaceSourcePaths(i.source_paths_json)),r=i.checkpoint_json?JSON.parse(i.checkpoint_json):void 0,a=r?.kind===`rejected`?r.checkpoint?JSON.parse(r.checkpoint):void 0:r,o=a?[c(a.baseManifestRaw,a.baseManifestRef,[...n],`stored`),c(a.currentManifestRaw,a.currentManifestRef,[...n],`stored`)].map(e=>new Map([...e.entries.map(e=>[e.path,e]),...(e.directories??[]).map(e=>[e,{path:e,type:`directory`}])])):[],d=new Set(g.keys());d.delete(``);let p=new Set([...g.values()].filter(({node:e})=>e.type===`directory`&&e.path!==``).map(({node:e})=>e.path));for(let e of o[1]?.values()??[])e.type===`directory`&&p.add(e.path);let _,v;for(let r of s){let s,c;for(let l of[`projection`,`canonical`,`canonicalWithProjectionPaths`]){let f=l===`canonicalWithProjectionPaths`;if(f&&!s)continue;let g=l===`projection`?i.projection_path:t.canonicalRoot,b=[];try{let e,t=new Set(p);if(l!==`projection`){if(_??=await selectLocalWorkspaceCanonicalPaths({root:g,admittedPaths:parseLocalWorkspaceSourcePaths(i.source_paths_json),baselinePaths:d,assertCurrent:()=>void 0}),e=new Set(_),f){for(let t of s)e.add(t);for(let e of c)t.add(e)}for(let t of o)for(let n of t.keys())e.add(n)}await u({root:g,baseCommit:i.base_commit,includePaths:e,preserveDirectories:t,buildCacheInputs:n,legacyDerivedEligibilitySubset:r,onEntry:e=>b.push(e)})}catch(e){v=e;continue}l===`projection`&&(s=new Set(b.map(e=>e.path)),c=new Set(b.filter(e=>e.type===`directory`).map(e=>e.path)));let S=h(b);if(a&&S.get(``).treeRef===a.treeRoot&&e===a.previousTreeRoot){let[e,t]=o;ee(S,[.../* @__PURE__ */ new Set([...e.keys(),...t.keys()])].map(t=>({path:t,node:e.get(t)})))}let C=S.get(``).treeRef,w=[...S.values()].map(({node:e})=>e).filter(e=>e.path!==``);if((i.tree_root_digest?C:i.baseline_json?localWorkspaceManifestDocument(w,i.base_commit,void 0,`stored`).manifestRef:x(S,i.base_commit))===e)return{kind:`replace`,update:prepareLocalWorkspaceUpdate({kind:`update`,revision:i.revision,patch:{tree_root_digest:m(C),...!i.tree_root_digest&&!i.pending_ref?{baseline_json:null}:{},canonical_fingerprints:null,projection_fingerprints:null}},y(S))}}}throw Error(`Local workspace baseline nodes are missing and neither workspace matches the saved baseline or a verified checkpoint. Both workspaces were preserved: canonical ${t.canonicalRoot}; sandbox ${i.projection_path}. A Gateway administrator must recover the saved baseline inventory before retrying the session. Inspect the preserved changes with: diff -ru -- ${shellEscape(t.canonicalRoot)} ${shellEscape(i.projection_path)}. Do not recreate or delete either workspace; retrying cannot safely choose between divergent edits.`,{cause:v})}if(t.kind===`replace`){let n=c(t.rawManifest,t.manifestRef,t.buildCacheInputs),r=h([...n.entries,...(n.directories??[]).map(e=>({path:e,type:`directory`}))]),a=x(r,n.baseCommit);return{kind:`replace`,update:prepareLocalWorkspaceUpdate({kind:`update`,revision:i.revision,patch:t.patch,manifest:{previousRoot:i.baseline_ref,resultingRoot:a,records:[],removed:[],treeRoot:r.get(``).treeRef,previousTreeRoot:i.tree_root_digest?f(i.tree_root_digest):null,replace:!0}},y(r,void 0,new Set(e.knownPacks)))}}if(t.kind===`materialize`&&i.baseline_json&&i.baseline_ref)return{kind:`materialize`,rawManifest:i.baseline_json,manifestRef:i.baseline_ref};let _=g,S=t.kind===`prepare`&&t.manifest.replace;if(S&&(_=new Map(t.manifest.records.map(e=>[e.node.path,e]))),!S&&!i.tree_root_digest&&i.baseline_json&&i.baseline_ref){let e=c(i.baseline_json,i.baseline_ref,void 0,`stored`);_=h([...e.entries,...(e.directories??[]).map(e=>({path:e,type:`directory`}))])}else if(_.size===0){if(i.baseline_ref)throw Error(`Local workspace baseline nodes are missing`);_=h([])}if(t.kind===`convert`){let e=Array.from(_.values(),({node:e})=>e).filter(e=>e.path!==``);return i.baseline_json||(_=h(e)),(i.baseline_json?localWorkspaceManifestDocument(e,i.base_commit,void 0,`stored`).manifestRef:x(_,i.base_commit))===i.baseline_ref?{kind:`replace`,update:prepareLocalWorkspaceUpdate({kind:`update`,revision:i.revision,patch:{tree_root_digest:m(_.get(``).treeRef),baseline_json:i.pending_ref?i.baseline_json:null,baseline_ref:i.baseline_ref,source_paths_json:i.source_paths_json}},y(_))}:{kind:`rescan`}}if(t.kind===`prepare`){let e=t.manifest;if(!e.replace){for(let t of e.removed)_.delete(t);for(let t of e.records)_.set(t.node.path,t)}return{kind:`replace`,update:prepareLocalWorkspaceUpdate({kind:`update`,revision:i.revision,patch:t.patch,manifest:{...e,records:[],removed:[],treeRoot:_.get(``).treeRef,previousTreeRoot:i.tree_root_digest?f(i.tree_root_digest):null}},y(_))}}if(t.kind===`materialize`)return{kind:`materialize`,...localWorkspaceManifestDocument(Array.from(_.values(),({node:e})=>e).filter(e=>e.path!==``),i.base_commit,void 0,`stored`)};let C=t.kind===`archive`?`canonical`:t.target,w=await selectLocalWorkspaceCanonicalPaths({root:t.canonicalRoot,admittedPaths:parseLocalWorkspaceSourcePaths(i.source_paths_json),assertCurrent:()=>void 0}),T=[...l(w)],E=t.kind===`scan`?t.capture:void 0,D=E?.root??(C===`canonical`?i.projection_path:t.canonicalRoot),O=await readLocalWorkspaceGitExclusions(t.canonicalRoot,D,w),k=C===`projection`?new Set(w):void 0,A=/* @__PURE__ */ new Set,j=/* @__PURE__ */ new Set;for(let[e,{node:t}]of _)e!==``&&(!w.has(e)&&a(e,O.paths)?A.add(e):(t.type===`directory`&&j.add(e),k?.add(e)));let M=C===`canonical`?`projection`:`canonical`,N=M===`canonical`?i.canonical_fingerprints:i.projection_fingerprints,P=/* @__PURE__ */ new Set,F=[],I=i.tree_root_digest?f(i.tree_root_digest):null,L=E?.capturedAtMs??Date.now(),R=b(_,I??_.get(``).treeRef,I?N:null,L,E),z=/* @__PURE__ */ Error(`Workspace changed during reuse probe`);try{await u({root:D,baseCommit:i.base_commit,includePaths:k,gitIgnore:O,preserveDirectories:j,buildCacheInputs:new Set(T),reuseFile:R.reuseFile,unchangedProbe:t.kind===`probe`,btrfsSnapshot:E!==void 0,onEntry(e,n){P.add(e.path);let r=_.get(e.path);if(!d(r?.node,e)){if(t.kind===`probe`)throw z;F.push({path:e.path,node:e})}n!==void 0&&R.observe(e.path,n)}})}catch(e){if(e===z)return{kind:`rescan`,entryCount:_.size};throw e}for(let e of _.keys())if(e!==``&&!P.has(e)){if(t.kind===`probe`)return{kind:`rescan`,entryCount:_.size};F.push({path:e})}if(t.kind===`archive`){if(F.length)throw Error(`Local workspace changed before archive capture`);return await prepareLocalWorkspaceArchive({root:t.archiveRoot,snapshotCommit:t.snapshotCommit,records:_,baseCommit:i.base_commit,buildCacheInputs:T})}let B=/* @__PURE__ */ new Set;for(let e of F){B.add(e.path);for(let t of o(e.path))B.add(t);let t=r(e.path);if(t){B.add(`${t}/.gitignore`);for(let e of o(`${t}/.gitignore`))B.add(e)}}let V=[];for(let e of B){let t=_.get(e);t&&!A.has(e)&&V.push(t.node)}let H=F.length&&I?b(_,I,M===`canonical`?i.projection_fingerprints:i.canonical_fingerprints,L):void 0,te=p(_,F,B,I),U=[];for(let e of B){let t=_.get(e);t&&U.push(t.node)}let W=localWorkspaceManifestDocument(V,i.base_commit,T,`stored`),G=localWorkspaceManifestDocument(U,i.base_commit,T),ne=i.tree_root_digest===null,K=_.get(``).treeRef,q=x(_,i.base_commit),J=R.encode(_,K),Y=J!==null&&J!==N&&(!N||!J.equals(N)),X=J&&Y?{target:M,blob:J.toString(`base64`)}:void 0;if(X&&H){let e=H.encode(_,K,new Set(F.map(({path:e})=>e)));e&&(X.oppositeBlob=e.toString(`base64`))}let Z=T.length?parseLocalWorkspaceSourcePaths(i.source_paths_json):[],re=new Set(Z),Q=T.filter(e=>!re.has(e)),$=q===i.baseline_ref,ie=Q.length||$&&Y?prepareLocalWorkspaceUpdate({kind:`update`,revision:i.revision,patch:{...Q.length?{source_paths_json:JSON.stringify([...Z,...Q])}:{},...$&&Y?{[M===`canonical`?`canonical_fingerprints`:`projection_fingerprints`]:J}:{}}}):void 0;return{version:1,kind:`scan`,previousRoot:i.baseline_ref,resultingRoot:q,treeRoot:K,previousTreeRoot:I,...X?{fingerprints:X}:{},preparations:te,records:[],observation:ie,removed:[],...ne?{replace:!0}:{},baseManifestRaw:W.rawManifest,baseManifestRef:W.manifestRef,currentManifestRaw:G.rawManifest,currentManifestRef:G.manifestRef,buildCacheInputs:T}}export{executeLocalWorkspaceManifestRead};