openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
1 lines • 13.9 kB
JavaScript
import{o as e}from"./openclaw-state-read-record-coerce-D1s3ReAs.mjs";import{a as t,i as n,r}from"./openclaw-state-read-kysely-sync-DQyYiAw5.mjs";import{a as i}from"./openclaw-state-read-sqlite-transaction-V1rK3HH2.mjs";import{P as a,c as o,i as s,n as c,t as l,u}from"./openclaw-state-read-workspace-path-exclusions-Cjo9Lmns.mjs";import{c as d,i as f,l as p,n as m,o as h,r as g,s as _,u as ee}from"./openclaw-state-read-local-workspace-cas-ZEBQPaPC.mjs";import{i as v,n as y,t as b}from"./openclaw-state-read-local-workspace-publication-C0wBpRSB.mjs";import{l as x,s as S}from"./openclaw-state-read-workspace-manifest-B5vKGqPY.mjs";import{t as C}from"./openclaw-state-read-local-workspace-fingerprints-fNlBRSJw.mjs";import{n as w}from"./openclaw-state-read-git-path-inventory-BbWvFXqI.mjs";import{r as T}from"./openclaw-state-read-workspace-actual-manifest-oEZtzR3V.mjs";import{o as te}from"./openclaw-state-read-workspace-manifest-comparison-BsGOV5Ve.mjs";import{createHash as E}from"node:crypto";import{isUtf8 as D}from"node:buffer";function shellEscape(e){return`'${e.replaceAll(`'`,`'"'"'`)}'`}function decodeLocalWorkspaceGitPaths(e){if(!D(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,t,n){let r=await u(e,[`--work-tree=${t}`,`ls-files`,`--full-name`,`--others`,`--ignored`,`--exclude-standard`,`--directory`,`-z`]);return{paths:new Set(decodeLocalWorkspaceGitPaths(r)),retainedPaths:n}}async function selectLocalWorkspaceCanonicalPaths(e){e.signal?.throwIfAborted(),e.assertCurrent();let t;if(e.preparedPaths)t=new Set(e.preparedPaths);else{let n=await u(e.root,[`ls-files`,`--cached`,`-z`],{signal:e.signal,beforeRun:e.assertCurrent});e.assertCurrent(),t=new Set(e.admittedPaths);for(let e of decodeLocalWorkspaceGitPaths(n))t.add(e)}if(e.baselinePaths)for(let n of e.baselinePaths)t.add(n);for(let e of t){let n=e.lastIndexOf(`/`);n!==-1&&t.add(e.slice(0,n))}return t}async function selectArchiveManifest(e){let{signal:t,assertCurrent:n}=e;if(!/^[a-f0-9]{40}(?:[a-f0-9]{24})?$/u.test(e.snapshotCommit))throw Error(`Invalid archive snapshot commit`);let r=await u(e.root,[`ls-tree`,`-r`,`-z`,`--name-only`,e.snapshotCommit],{signal:t,beforeRun:n,maxOutputBytes:67108864});n();let i=new Set(w(r).map(e=>e.toString(`hex`))),a=e.manifest.entries.filter(e=>i.has(Buffer.from(e.path).toString(`hex`))),s=/* @__PURE__ */ new Set;for(let e of a)for(let t of o(e.path))s.add(t);return a.length===e.manifest.entries.length&&s.size===(e.manifest.directories?.length??0)&&e.manifest.directories?.every(e=>s.has(e))!==!1?e.manifest:{...e.manifest,entries:a,directories:[...s].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=x(e,t);return{rawManifest:n,manifestRef:`sha256:`+E(`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 readLocalWorkspaceManifestRows(e,t){return i(e,()=>readManifestRowsInSnapshot(e,t))}function readManifestRowsInSnapshot(e,i){let a=t(e),o=i.action.kind===`scan`||i.action.kind===`probe`?i.action.target:i.action.kind===`archive`?`canonical`:void 0,s=o!==void 0||i.action.kind===`convert`||i.action.kind===`repair`,c=n(e,a.selectFrom(`local_workspace_projections`).select([`revision`,`projection_path`,`base_commit`,`baseline_json`,`baseline_ref`,`tree_root_digest`,`source_paths_digest`,`pending_ref`]).select(e=>[(s?e.ref(`source_paths_json`):e.val(`[]`)).as(`source_paths_json`),(o===`projection`||i.action.kind===`scan`?e.ref(`canonical_fingerprints`):e.val(null)).as(`canonical_fingerprints`),(o===`canonical`||i.action.kind===`scan`?e.ref(`projection_fingerprints`):e.val(null)).as(`projection_fingerprints`)]).$if(i.action.kind===`repair`,e=>e.select(`checkpoint_json`)).where(`worktree_id`,`=`,i.id));if(!c||c.revision!==i.revision)throw Error(`Local workspace binding changed`);if(s&&c.source_paths_digest&&(c.source_paths_json=y(e,c.source_paths_digest)),c.tree_root_digest&&i.knownTreeRoot===f(c.tree_root_digest)&&[`scan`,`probe`,`materialize`,`archive`].includes(i.action.kind))return{row:c,records:void 0,knownPacks:void 0};let l=[],u=i.action.kind===`replace`||i.action.kind===`repair`||i.action.kind===`prepare`&&i.action.manifest.replace;if(c.tree_root_digest&&(!u||i.action.kind===`repair`))l=v(e,c.tree_root_digest,{allowMissing:i.action.kind===`repair`});else if(!c.baseline_json&&!u)for(let{node_json:t}of r(e,a.selectFrom(`local_workspace_manifest_nodes`).select(`node_json`).where(`worktree_id`,`=`,i.id)).rows)l.push(t);let d=c.tree_root_digest&&i.action.kind===`replace`?v(e,c.tree_root_digest,{referencesOnly:!0,allowMissing:!0}):void 0;return{row:c,records:l,...d?{knownPacks:d}:{}}}async function executeLocalWorkspaceManifestRead(e,t,n){let{row:r}=e;if(!n&&!e.records)throw Error(`Local workspace prepared baseline is unavailable`);let i=n??m(e.records,!r.tree_root_digest);if(e.records=[],t.kind===`repair`){if(!r.baseline_ref)throw Error(`Local workspace repair requires its saved baseline reference`);let e=r.tree_root_digest?f(r.tree_root_digest):r.baseline_ref,n=c(parseLocalWorkspaceSourcePaths(r.source_paths_json)),a=r.checkpoint_json?JSON.parse(r.checkpoint_json):void 0,o=a?.kind===`rejected`?a.checkpoint?JSON.parse(a.checkpoint):void 0:a,s=o?[S(o.baseManifestRaw,o.baseManifestRef,[...n],`stored`),S(o.currentManifestRaw,o.currentManifestRef,[...n],`stored`)].map(e=>new Map([...e.entries.map(e=>[e.path,e]),...(e.directories??[]).map(e=>[e,{path:e,type:`directory`}])])):[],u=new Set(i.keys());u.delete(``);let m=new Set([...i.values()].filter(({node:e})=>e.type===`directory`&&e.path!==``).map(({node:e})=>e.path));for(let e of s[1]?.values()??[])e.type===`directory`&&m.add(e.path);let _,v;for(let i of l){let a,c;for(let l of[`projection`,`canonical`,`canonicalWithProjectionPaths`]){let f=l===`canonicalWithProjectionPaths`;if(f&&!a)continue;let y=l===`projection`?r.projection_path:t.canonicalRoot,x=[];try{let e,t=new Set(m);if(l!==`projection`){if(_??=await selectLocalWorkspaceCanonicalPaths({root:y,admittedPaths:parseLocalWorkspaceSourcePaths(r.source_paths_json),baselinePaths:u,assertCurrent:()=>void 0}),e=new Set(_),f){for(let t of a)e.add(t);for(let e of c)t.add(e)}for(let t of s)for(let n of t.keys())e.add(n)}await T({root:y,baseCommit:r.base_commit,includePaths:e,preserveDirectories:t,buildCacheInputs:n,legacyDerivedEligibilitySubset:i,onEntry:e=>x.push(e)})}catch(e){v=e;continue}l===`projection`&&(a=new Set(x.map(e=>e.path)),c=new Set(x.filter(e=>e.type===`directory`).map(e=>e.path)));let S=d(x);if(o&&S.get(``).treeRef===o.treeRoot&&e===o.previousTreeRoot){let[e,t]=s;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((r.tree_root_digest?C:r.baseline_json?localWorkspaceManifestDocument(w,r.base_commit,void 0,`stored`).manifestRef:p(S,r.base_commit))===e)return{kind:`replace`,update:b({kind:`update`,revision:r.revision,patch:{tree_root_digest:g(C),...!r.tree_root_digest&&!r.pending_ref?{baseline_json:null}:{},canonical_fingerprints:null,projection_fingerprints:null}},h(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 ${r.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(r.projection_path)}. Do not recreate or delete either workspace; retrying cannot safely choose between divergent edits.`,{cause:v})}if(t.kind===`replace`){let n=S(t.rawManifest,t.manifestRef,t.buildCacheInputs),i=d([...n.entries,...(n.directories??[]).map(e=>({path:e,type:`directory`}))]),a=p(i,n.baseCommit),o={kind:`update`,revision:r.revision,patch:t.patch,manifest:{previousRoot:r.baseline_ref,resultingRoot:a,records:[],removed:[],treeRoot:i.get(``).treeRef,previousTreeRoot:r.tree_root_digest?f(r.tree_root_digest):null,replace:!0}};return{kind:`replace`,update:b(o,h(i,void 0,new Set(e.knownPacks)))}}if(t.kind===`materialize`&&r.baseline_json&&r.baseline_ref)return{kind:`materialize`,rawManifest:r.baseline_json,manifestRef:r.baseline_ref};let u=i,v=t.kind===`prepare`&&t.manifest.replace;if(v&&(u=new Map(t.manifest.records.map(e=>[e.node.path,e]))),!v&&!r.tree_root_digest&&r.baseline_json&&r.baseline_ref){let e=S(r.baseline_json,r.baseline_ref,void 0,`stored`);u=d([...e.entries,...(e.directories??[]).map(e=>({path:e,type:`directory`}))])}else if(u.size===0){if(r.baseline_ref)throw Error(`Local workspace baseline nodes are missing`);u=d([])}if(t.kind===`convert`){let e=Array.from(u.values(),({node:e})=>e).filter(e=>e.path!==``);return r.baseline_json||(u=d(e)),(r.baseline_json?localWorkspaceManifestDocument(e,r.base_commit,void 0,`stored`).manifestRef:p(u,r.base_commit))===r.baseline_ref?{kind:`replace`,update:b({kind:`update`,revision:r.revision,patch:{tree_root_digest:g(u.get(``).treeRef),baseline_json:r.pending_ref?r.baseline_json:null,baseline_ref:r.baseline_ref,source_paths_json:r.source_paths_json}},h(u))}:{kind:`rescan`}}if(t.kind===`prepare`){let e=t.manifest;if(!e.replace){for(let t of e.removed)u.delete(t);for(let t of e.records)u.set(t.node.path,t)}return{kind:`replace`,update:b({kind:`update`,revision:r.revision,patch:t.patch,manifest:{...e,records:[],removed:[],treeRoot:u.get(``).treeRef,previousTreeRoot:r.tree_root_digest?f(r.tree_root_digest):null}},h(u))}}if(t.kind===`materialize`)return{kind:`materialize`,...localWorkspaceManifestDocument(Array.from(u.values(),({node:e})=>e).filter(e=>e.path!==``),r.base_commit,void 0,`stored`)};let y=t.kind===`archive`?`canonical`:t.target,x=await selectLocalWorkspaceCanonicalPaths({root:t.canonicalRoot,admittedPaths:parseLocalWorkspaceSourcePaths(r.source_paths_json),assertCurrent:()=>void 0}),w=[...c(x)],E=t.kind===`scan`?t.capture:void 0,D=E?.root??(y===`canonical`?r.projection_path:t.canonicalRoot),O=await readLocalWorkspaceGitExclusions(t.canonicalRoot,D,x),k=y===`projection`?new Set(x):void 0,A=/* @__PURE__ */ new Set,j=/* @__PURE__ */ new Set;for(let[e,{node:t}]of u)e!==``&&(!x.has(e)&&s(e,O.paths)?A.add(e):(t.type===`directory`&&j.add(e),k?.add(e)));let M=y===`canonical`?`projection`:`canonical`,N=M===`canonical`?r.canonical_fingerprints:r.projection_fingerprints,P=/* @__PURE__ */ new Set,F=[],I=r.tree_root_digest?f(r.tree_root_digest):null,L=E?.capturedAtMs??Date.now(),R=C(u,I??u.get(``).treeRef,I?N:null,L,E),z=/* @__PURE__ */ Error(`Workspace changed during reuse probe`);try{await T({root:D,baseCommit:r.base_commit,includePaths:k,gitIgnore:O,preserveDirectories:j,buildCacheInputs:new Set(w),reuseFile:R.reuseFile,unchangedProbe:t.kind===`probe`,btrfsSnapshot:E!==void 0,onEntry(e,n){P.add(e.path);let r=u.get(e.path);if(!te(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:u.size};throw e}for(let e of u.keys())if(e!==``&&!P.has(e)){if(t.kind===`probe`)return{kind:`rescan`,entryCount:u.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:u,baseCommit:r.base_commit,buildCacheInputs:w})}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=a(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=u.get(e);t&&!A.has(e)&&V.push(t.node)}let H=F.length&&I?C(u,I,M===`canonical`?r.projection_fingerprints:r.canonical_fingerprints,L):void 0,ne=_(u,F,B,I),U=[];for(let e of B){let t=u.get(e);t&&U.push(t.node)}let W=localWorkspaceManifestDocument(V,r.base_commit,w,`stored`),G=localWorkspaceManifestDocument(U,r.base_commit,w),re=r.tree_root_digest===null,K=u.get(``).treeRef,q=p(u,r.base_commit),J=R.encode(u,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(u,K,new Set(F.map(({path:e})=>e)));e&&(X.oppositeBlob=e.toString(`base64`))}let Z=w.length?parseLocalWorkspaceSourcePaths(r.source_paths_json):[],ie=new Set(Z),Q=w.filter(e=>!ie.has(e)),$=q===r.baseline_ref,ae=Q.length||$&&Y?b({kind:`update`,revision:r.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:r.baseline_ref,resultingRoot:q,treeRoot:K,previousTreeRoot:I,...X?{fingerprints:X}:{},preparations:ne,records:[],observation:ae,removed:[],...re?{replace:!0}:{},baseManifestRaw:W.rawManifest,baseManifestRef:W.manifestRef,currentManifestRaw:G.rawManifest,currentManifestRef:G.manifestRef,buildCacheInputs:w}}export{executeLocalWorkspaceManifestRead,readLocalWorkspaceManifestRows};