UNPKG

eve

Version:

Filesystem-first framework for durable backend AI agents that run anywhere.

1 lines • 2.9 kB
import{randomUUID}from"node:crypto";import{resolve}from"node:path";import{acquireOutputPublicationLock,resolveOutputPublicationLockPath,startPublicationJournalHeartbeat}from"#internal/application/output-publication-lock.js";import{assertStagedPublicationExists,backupOutputPublication,installOutputPublication,prepareOutputPublication,removeOutputPublicationBackups,rollbackOutputPublication}from"#internal/application/output-publication-artifacts.js";import{writeOutputPublicationJournal}from"#internal/application/output-publication-journal.js";var RecoverablePublicationError=class extends AggregateError{constructor(e,t,n){super(e,t,n),this.name=`RecoverablePublicationError`}};const DEFAULT_OBSERVER={async afterBackup(){},async afterOutputInstall(){},async onContention(){}};async function publishApplicationBuildArtifacts(e){await publishApplicationBuildArtifactsWithObserver(e,DEFAULT_OBSERVER)}async function publishApplicationBuildArtifactsWithObserver(e,t){let n=createOutputPublicationJournal(e);await assertStagedPublicationExists(n);let r=resolveOutputPublicationLockPath(e.appRoot),i=await acquireOutputPublicationLock(r,n,t.onContention),a=startPublicationJournalHeartbeat(r),o=!1;try{await prepareOutputPublication(n),n.phase=`prepared`,await writeOutputPublicationJournal(r,n),await backupOutputPublication(n),n.phase=`backed-up`,await writeOutputPublicationJournal(r,n),await t.afterBackup(),await installOutputPublication(n,t.afterOutputInstall),n.phase=`committed`,await writeOutputPublicationJournal(r,n),o=!0,await removeOutputPublicationBackups(n)}catch(e){o&&await throwRecoverablePublicationError({errors:[e],journal:n,lockPath:r,message:`Build output was committed but backup cleanup failed.`});try{await rollbackOutputPublication(n)}catch(t){await throwRecoverablePublicationError({errors:[e,t],journal:n,lockPath:r,message:`Build output publication failed and could not fully restore the previous output.`})}throw await i(),e}finally{a()}await i()}function createOutputPublicationJournal(e){let t=randomUUID(),n=resolve(e.finalOutputDir),r=resolve(e.finalSummaryPath);return{finalOutputDir:n,finalSummaryPath:r,hadOutput:!1,hadSummary:!1,liveness:`active`,outputBackupPath:`${n}.eve-backup-${t}`,phase:`acquired`,pid:process.pid,scratchDir:resolve(e.scratchDir),stagedOutputDir:resolve(e.stagedOutputDir),stagedSummaryPath:resolve(e.stagedSummaryPath),summaryBackupPath:`${r}.eve-backup-${t}`,token:t}}async function throwRecoverablePublicationError(e){e.journal.liveness=`recoverable`;try{await writeOutputPublicationJournal(e.lockPath,e.journal)}catch(t){throw new RecoverablePublicationError([...e.errors,t],e.message,{cause:e.errors[0]})}throw new RecoverablePublicationError(e.errors,e.message,{cause:e.errors[0]})}export{RecoverablePublicationError,publishApplicationBuildArtifacts,publishApplicationBuildArtifactsWithObserver,resolveOutputPublicationLockPath};