openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
5 lines • 8.64 kB
JavaScript
import{r as resolveStateDir}from"./state-dir-BwIClCab.mjs";import{u as observeDiagnosticsTimelinePhases}from"./diagnostics-timeline-Wd4cnAkV.mjs";import{t as _usingCtx}from"./usingCtx-CUVyQ_FT.mjs";import{a as withGitProcessOperation}from"./spawn-diagnostics-C-LTFiNq.mjs";import{s as composeSessionSourceAssertion,u as prepareSessionSourceEffect}from"./session-source-authority-Cun3s-rH.mjs";import{n as runCommandWithTimeout}from"./exec-runner-BiCq-T1F.mjs";import"./exec-c5tqm8tb.mjs";import{l as gitNullConfigPath,r as createGitCommandError}from"./git-exec-BRQo7oSy.mjs";import{d as runGit,n as gitEnvironment,o as requireGit}from"./git-2P0YMMKQ.mjs";import{o as timeWorktreePreparationPhase}from"./preparation-timing-CC2_vlen.mjs";import{t as withWorktreeGitConfig}from"./checkout-git-config-DsLh_M7l.mjs";import{n as prepareWorktreeFilesystemMutation}from"./filesystem-native-BvDS5nVZ.mjs";import{t as parseGitHubRemoteUrl}from"./github-remote-Cv6zDIpK.mjs";import{t as captureInitialLocalWorkspaceManifest}from"./workspace-manifest-worker-C4V2H1Gh.mjs";import{n as prepareWorkerWorkspaceGitPack}from"./workspace-git-base-0Ecej9Zn.mjs";import path from"node:path";import fs from"node:fs/promises";
//!src/gateway/worker-environments/local-workspace-checkout.ts
const RECONCILE_PHASE=`attempt.setup.sandbox.workspace_selection.prepare.reconcile`;function checkoutCurrent(e,t){const assertCurrent=()=>{t.signal.throwIfAborted();e.assertCurrent()};const n=[.../* @__PURE__ */ new Set([...e.workspaceEvidence??[],...t.workerAuthority?.workspaceEvidence??[]])];return Object.assign(assertCurrent,{prepareSessionSourceEffect:async()=>({...e,release:void 0,assertCurrent,workspaceEvidence:n})})}function checkoutGitEnvironment(e){return{PATH:process.env.PATH,HOME:e,GIT_CONFIG_NOSYSTEM:`1`,GIT_CONFIG_GLOBAL:gitNullConfigPath(),GIT_CONFIG_SYSTEM:gitNullConfigPath(),GIT_NO_REPLACE_OBJECTS:`1`,GIT_TERMINAL_PROMPT:`0`}}async function configureProjectionFetch(e,t){const n={baseEnv:t,env:t,signal:e.signal,beforeRun:e.assertCurrent,stateOwnerPath:e.workerAuthority?.stateOwnerPath};await requireGit(e.destination,[`config`,`--local`,`--replace-all`,`openclaw.projection`,`true`],n);const r=await runGit(e.source,[`config`,`--get`,`remote.origin.url`],n);if(r.code!==0&&r.code!==1)throw createGitCommandError(`git config remote.origin.url`,r);const i=parseGitHubRemoteUrl(r.stdout);if(!i)return;const a=`https://github.com/${i.owner}/${i.repo}`;const o={"remote.origin.url":`${a}.git`};for(const e of[`origin`,a,`${a}.git`]){o[`remote.${e}.promisor`]=`true`;o[`remote.${e}.partialclonefilter`]=`blob:none`}for(const[t,r]of Object.entries(o))await requireGit(e.destination,[`config`,`--local`,`--replace-all`,t,r],n)}async function localWorkspaceProjectionPath(e){if(!/^[a-f0-9-]{36}$/u.test(e.worktree.id))throw new Error(`Invalid managed worktree identity`);return path.join(await fs.realpath(resolveStateDir(e.env)),`worktree-projections`,e.worktree.id,`workspace`)}async function prepareLocalWorkspaceCheckout(e){try{var t=_usingCtx();const n=t.u(observeDiagnosticsTimelinePhases(`${RECONCILE_PHASE}.checkout.pack`));const{signal:r,destination:i}=e;const a=await prepareSessionSourceEffect(composeSessionSourceAssertion([e.assertCurrent,e.workerAuthority?.assertCurrent]));t.a({[Symbol.asyncDispose]:async()=>await a.release?.()});const o=checkoutCurrent(a,e);const s={commitGuard:o,signal:r,workerAuthority:e.workerAuthority};const prepare=()=>prepareWorktreeFilesystemMutation(s,a);await prepare();o();const c=await fs.mkdtemp(path.join(e.temporaryRoot,`git-`));try{const t=await withWorktreeGitConfig(e.source,true,{signal:r,beforeRun:o,stateOwnerPath:e.workerAuthority?.stateOwnerPath},t=>t.withContentEnvironment(t=>prepareWorkerWorkspaceGitPack({root:e.source,baseCommit:e.baseCommit,retainedCommit:e.retainedCommit,temporaryRoot:c,signal:r,baseEnv:t})));await prepare();o();n.mark(`${RECONCILE_PHASE}.checkout.index`);await fs.mkdir(i,{recursive:true,mode:448});const a=checkoutGitEnvironment(c);const git=(t,n)=>requireGit(i,t,{baseEnv:a,env:a,input:n,signal:r,beforeRun:o,stateOwnerPath:e.workerAuthority?.stateOwnerPath});if(!e.retainedCommit)await git([`init`,`--quiet`,`--template=`,`--object-format=`+(e.baseCommit.length===40?`sha1`:`sha256`)]);const s=await fs.open(t,`r`);try{await prepare();o();const e=await withGitProcessOperation(`workspace.inventory`,()=>runCommandWithTimeout([`git`,`-C`,i,`index-pack`,`--stdin`,`--fix-thin`],{stdinFileDescriptor:s.fd,baseEnv:gitEnvironment(a),signal:r,timeoutMs:3e5,maxOutputBytes:4096,terminateOnOutputLimit:true,killProcessTree:true,requireProcessTreeExtinction:true}),`default`);if(e.code!==0||e.termination!==`exit`)throw createGitCommandError(`git index-pack`,e)}finally{await s.close()}await prepare();o();n.mark(`${RECONCILE_PHASE}.checkout.materialize`);await fs.appendFile(path.join(i,`.git`,`shallow`),e.baseCommit+`
`,{mode:384});await git([`checkout`,`--quiet`,e.retainedCommit?`-B`:`-b`,e.branch,e.baseCommit]);if(e.retainedCommit){n.mark(`${RECONCILE_PHASE}.checkout.repack`);await git([`reflog`,`expire`,`--expire=all`,`--all`]);await git([`repack`,`-ad`,`--window=0`]);await prepare();o();await fs.writeFile(path.join(i,`.git`,`shallow`),e.baseCommit+`
`)}n.mark(`${RECONCILE_PHASE}.checkout.configure`);await configureProjectionFetch({...e,assertCurrent:o},a)}finally{n.mark(`${RECONCILE_PHASE}.checkout.cleanup`);await fs.rm(c,{recursive:true,force:true})}}catch(e){t.e=e}finally{await t.d()}}async function assertLocalWorkspaceDirectory(e){const t=await fs.lstat(e);if(!t.isDirectory()||t.isSymbolicLink()||await fs.realpath(e)!==e)throw new Error(`Local sandbox workspace directory changed; preserved for recovery`)}async function initializeLocalWorkspaceProjection(e){try{var t=_usingCtx();const n=t.u(observeDiagnosticsTimelinePhases(`${RECONCILE_PHASE}.initialize`));const{signal:r,dependencies:i}=e;const a={...e.owner.workerAuthority,...e.workerAuthority,workspaceEvidence:[.../* @__PURE__ */ new Set([...e.owner.workerAuthority?.workspaceEvidence??[],...e.workerAuthority?.workspaceEvidence??[]])]};const o=await prepareSessionSourceEffect(composeSessionSourceAssertion([e.assertCurrent,e.owner.assertCurrent,e.owner.workerAuthority?.assertCurrent,e.workerAuthority?.assertCurrent]));t.a({[Symbol.asyncDispose]:async()=>await o.release?.()});const s=checkoutCurrent(o,{...e,workerAuthority:a});const c={commitGuard:s,signal:r,workerAuthority:a};const prepare=()=>prepareWorktreeFilesystemMutation(c,o);const l=path.dirname(e.projectionPath);await prepare();s();await fs.mkdir(l,{recursive:true,mode:448});await assertLocalWorkspaceDirectory(l);await prepare();s();await fs.rm(e.projectionPath,{recursive:true,force:true});await prepare();s();const u=await fs.mkdtemp(path.join(l,`.prepare-`));try{const t=path.join(u,`workspace`);const o={source:e.owner.worktree.path,destination:t,temporaryRoot:u,baseCommit:e.baseCommit,branch:e.owner.worktree.branch,signal:r,assertCurrent:s,workerAuthority:a};const c=await e.buildCacheInputs();n.mark(`${RECONCILE_PHASE}.template`);const d=i&&await(await import(`./local-workspace-template-SJhQwIsz.mjs`)).cloneLocalWorkspaceTemplate({...o,buildCacheInputs:c,repoRoot:e.owner.worktree.repoRoot,templateRoot:path.dirname(l),env:e.owner.env??process.env,sandbox:i.sandbox,guard:{...i.allocation,signal:i.allocation.signal?AbortSignal.any([r,i.allocation.signal]):r,commitGuard:composeSessionSourceAssertion([i.allocation.commitGuard,s]),workerAuthority:{...i.allocation.workerAuthority,assertCurrent:composeSessionSourceAssertion([i.allocation.workerAuthority.assertCurrent,a.assertCurrent,s]),stateOwnerPath:a.stateOwnerPath??i.allocation.workerAuthority.stateOwnerPath,workspaceEvidence:[.../* @__PURE__ */ new Set([...i.allocation.workerAuthority.workspaceEvidence??[],...a.workspaceEvidence??[]])]}}});if(!d){n.mark(`${RECONCILE_PHASE}.checkout`);await timeWorktreePreparationPhase(`checkout`,()=>prepareLocalWorkspaceCheckout(o))}else await configureProjectionFetch(o,checkoutGitEnvironment(u));n.mark(`${RECONCILE_PHASE}.manifest_scan`);const f=d||await timeWorktreePreparationPhase(`snapshot`,async()=>captureInitialLocalWorkspaceManifest({root:t,baseCommit:e.baseCommit,buildCacheInputs:new Set(c),signal:r}));await prepare();s();n.mark(`${RECONCILE_PHASE}.publish`);await fs.rename(t,e.projectionPath);return{...f,buildCacheInputs:c}}finally{n.mark(`${RECONCILE_PHASE}.cleanup`);await fs.rm(u,{recursive:true,force:true})}}catch(e){t.e=e}finally{await t.d()}}export{prepareLocalWorkspaceCheckout as i,initializeLocalWorkspaceProjection as n,localWorkspaceProjectionPath as r,assertLocalWorkspaceDirectory as t};