UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 6.8 kB
import{r as isErrno,t as hasErrnoCode}from"./errno-DUBt6SQQ.mjs";import{m as statIfExists}from"./path-existence-BR9MXlgL.mjs";import{t as assertionSucceeds}from"./assertion-BDJqrEL5.mjs";import{l as gitNullConfigPath}from"./git-exec-BRQo7oSy.mjs";import{l as GitHubPublicationWorkspaceChangedError}from"./github-publication-failure-CZb5rwTV.mjs";import path from"node:path";import{createHash}from"node:crypto";import fs from"node:fs/promises";import os from"node:os"; //!src/gateway/github-publication-git-index.ts const HARDENED_GIT=[`git`,`-c`,`core.hooksPath=${os.devNull}`,`-c`,`core.fsmonitor=false`];var GitHubPublicationRefCasRejectedError=class extends GitHubPublicationWorkspaceChangedError{};var GitHubPublicationRecoveryPendingError=class extends Error{};function assertGitHubPublicationRefCasCompleted(e){if(e.code===0)return;if(e.signal===null&&!e.killed)throw new GitHubPublicationRefCasRejectedError(`GitHub publication workspace branch changed before commit.`);throw new Error(`GitHub publication workspace branch update outcome is unknown.`)}async function syncDirectory(e){let t;try{t=await fs.open(e,`r`);await t.sync()}catch(e){const t=isErrno(e)?e.code:void 0;if(process.platform!==`win32`||t!==`EINVAL`&&t!==`EPERM`)throw e}finally{await t?.close().catch(()=>void 0)}}async function sameFile(e,t){try{const[n,r]=await Promise.all([fs.stat(e),fs.stat(t)]);return n.nlink>=2&&r.nlink>=2&&n.dev===r.dev&&n.ino===r.ino}catch(e){if(hasErrnoCode(e,`ENOENT`))return false;throw e}}async function writeDurableFile(e,t){await fs.writeFile(e,t,{flag:`w`,mode:384});const n=await fs.open(e,`r+`);try{await n.sync()}finally{await n.close()}}function publicationRecoveryPath(e,t){return`${e}.openclaw-${createHash(`sha256`).update(t).digest(`hex`)}`}async function recoverGitHubPublicationBranchAndIndex(e){e.assertCustody();const mutate=async t=>{e.assertCustody();return await t()};const t=await e.run([`git`,`rev-parse`,`--git-path`,`index`],{cwd:e.cwd});const n=path.resolve(e.cwd,t);const r=`${n}.lock`;const i=publicationRecoveryPath(n,e.requestId);if(await statIfExists(i)===void 0)return;const a=await sameFile(i,r);if(!a&&await statIfExists(r)!==void 0)throw new GitHubPublicationRecoveryPendingError(`GitHub publication workspace recovery is waiting for another Git operation.`);const o=await e.run([`git`,`rev-parse`,`--verify`,`refs/heads/${e.branch}`],{cwd:e.cwd});if(!a){const t=await e.run([...HARDENED_GIT,`write-tree`],{cwd:e.cwd});if(o===e.sourceHeadCommit||t===e.workspaceTree&&await publicationCommitMatches(e,o)){await mutate(async()=>await fs.rm(i,{force:true}));return}throw new GitHubPublicationRecoveryPendingError(`GitHub publication workspace recovery is pending.`)}if(o===e.sourceHeadCommit){await mutate(async()=>await fs.rm(r,{force:true}));await mutate(async()=>await fs.rm(i,{force:true}));await mutate(async()=>await syncDirectory(path.dirname(n)));return}if(!await publicationCommitMatches(e,o))throw new GitHubPublicationRecoveryPendingError(`GitHub publication workspace branch recovery is pending.`);await mutate(async()=>await fs.rename(r,n));await mutate(async()=>await syncDirectory(path.dirname(n)));await mutate(async()=>await fs.rm(i,{force:true}))}async function publicationCommitMatches(e,t){const[n,r,i]=await Promise.all([e.run([`git`,`show`,`-s`,`--format=%B`,t],{cwd:e.cwd}),e.run([`git`,`rev-parse`,`${t}^`],{cwd:e.cwd}),e.run([`git`,`rev-parse`,`${t}^{tree}`],{cwd:e.cwd})]);return n.split(/\r?\n/u).includes(`OpenClaw-Publication: ${e.requestId}`)&&r===e.sourceHeadCommit&&i===e.workspaceTree}async function updateGitHubPublicationBranchAndIndex(e){e.assertCurrent();const t=await fs.mkdtemp(path.join(os.tmpdir(),`openclaw-github-index-`));const n=path.join(t,`replacement-index`);const r=path.join(t,`observed-index`);try{const t=await e.run([`git`,`rev-parse`,`--git-path`,`index`],{cwd:e.cwd});const i=path.resolve(e.cwd,t);const a=`${i}.lock`;const o=publicationRecoveryPath(i,e.requestId);let s=false;let c=false;let l=false;let u=false;const installIndex=async t=>{t();await fs.rename(a,i);s=false;u=true;e.assertCustody();await syncDirectory(path.dirname(i));e.assertCustody();await fs.rm(o,{force:true})};try{const t={...e.env,GIT_CONFIG_GLOBAL:gitNullConfigPath(),GIT_CONFIG_SYSTEM:gitNullConfigPath()};await e.run([...HARDENED_GIT,`read-tree`,e.headCommit],{cwd:e.cwd,env:{...t,GIT_INDEX_FILE:n}});const u=await fs.readFile(n);let d;try{d=await fs.readFile(o)}catch(e){if(!hasErrnoCode(e,`ENOENT`))throw e}if(d&&!d.equals(u)){const t=await e.run([`git`,`rev-parse`,`--verify`,`refs/heads/${e.branch}`],{cwd:e.cwd});if(await sameFile(o,a)||t!==e.previousHead)throw new GitHubPublicationRecoveryPendingError(`GitHub publication workspace recovery data changed.`);d=void 0}if(!d){e.assertCurrent();c=true;await writeDurableFile(o,u);await syncDirectory(path.dirname(i))}if(await sameFile(o,a)){const t=await e.run([`git`,`rev-parse`,`--verify`,`refs/heads/${e.branch}`],{cwd:e.cwd});if(t===e.headCommit){l=true;s=true;try{await installIndex(e.assertCustody);return}catch(e){throw new GitHubPublicationRecoveryPendingError(`GitHub publication workspace index recovery is pending.`,{cause:e})}}if(t!==e.previousHead)throw new GitHubPublicationRecoveryPendingError(`GitHub publication workspace branch recovery is pending.`);e.assertCustody();c=true;await fs.rm(a);e.assertCustody();await syncDirectory(path.dirname(i))}else if(await statIfExists(a)!==void 0)throw new Error(`GitHub publication workspace index is locked by another operation.`);e.assertCurrent();try{await fs.link(o,a);s=true;c=true}catch(e){throw new Error(`GitHub publication workspace index changed before commit.`,{cause:e})}await fs.copyFile(i,r);const f=await e.run([...HARDENED_GIT,`write-tree`],{cwd:e.cwd,env:{...t,GIT_INDEX_FILE:r}});if(f!==e.sourceIndexTree&&f!==e.workspaceTree)throw new GitHubPublicationWorkspaceChangedError(`GitHub publication workspace index changed after its accepted snapshot.`);e.assertCurrent();await syncDirectory(path.dirname(i));e.assertCurrent();if(e.updateRef){l=true;try{await e.updateRef()}catch(e){if(e instanceof GitHubPublicationRefCasRejectedError)l=false;throw e}}await installIndex(e.updateRef?e.assertCustody:e.assertCurrent)}catch(e){if(!u&&l&&s)throw new GitHubPublicationRecoveryPendingError(`GitHub publication workspace recovery is pending.`,{cause:e});throw e}finally{const hasCustody=()=>assertionSucceeds(()=>e.assertCustody());if(!u&&!l&&s&&hasCustody()&&await sameFile(o,a)&&hasCustody())await fs.rm(a,{force:true});if((u||!l&&c)&&hasCustody())await fs.rm(o,{force:true})}}finally{await fs.rm(t,{recursive:true,force:true})}}export{updateGitHubPublicationBranchAndIndex as i,assertGitHubPublicationRefCasCompleted as n,recoverGitHubPublicationBranchAndIndex as r,GitHubPublicationRecoveryPendingError as t};