eve
Version:
Filesystem-first framework for durable backend AI agents that run anywhere.
1 lines • 2.44 kB
JavaScript
import{assertFullSha,assertGitRef}from"./identifiers.js";import{gitOutput,runGitCommand,withBrokeredGitHubCredential}from"./git.js";import{shellQuote}from"#shared/shell-quote.js";import{gitHubRemoteUrl}from"#shared/git.js";const SELF_MODIFICATION_CONFIG_PATH=`agent/extensions/self-modification/extension.ts`,WORKSPACE_PATH=`/workspace`,REPOSITORY_PATH=`${WORKSPACE_PATH}/repository`,BASE_REF=`refs/eve-self-modification/base`;async function prepareSelfModificationWorkspace(e){assertGitRef(e.targetBranch,`target branch`);let t=gitHubRemoteUrl(e.repository);await withBrokeredGitHubCredential(e.sandbox,e.token,async()=>{await run(e.sandbox,`rm -rf ${quote(REPOSITORY_PATH)} && mkdir -p ${quote(REPOSITORY_PATH)}`),await run(e.sandbox,`git -C ${quote(REPOSITORY_PATH)} init`),await run(e.sandbox,`git -C ${quote(REPOSITORY_PATH)} remote add origin ${quote(t)}`),await run(e.sandbox,`GIT_CONFIG_NOSYSTEM=1 GIT_CONFIG_GLOBAL=/dev/null GIT_LFS_SKIP_SMUDGE=1 GIT_TERMINAL_PROMPT=0 git -C ${quote(REPOSITORY_PATH)} fetch --no-tags origin ${quote(`refs/heads/${e.targetBranch}`)}`),await run(e.sandbox,`git -C ${quote(REPOSITORY_PATH)} update-ref ${BASE_REF} FETCH_HEAD`),await run(e.sandbox,`git -C ${quote(REPOSITORY_PATH)} checkout --detach ${BASE_REF}`)});let n=await gitOutput(e.sandbox,`git -C ${quote(REPOSITORY_PATH)} rev-parse ${BASE_REF}`);return assertFullSha(n,`target branch revision`),await verifyApplicationRoot(e.sandbox,e.directory),{baseSha:n,directory:e.directory,repository:e.repository,repositoryPath:REPOSITORY_PATH,targetBranch:e.targetBranch}}async function readPreparedSelfModificationWorkspace(e){let t=await gitOutput(e.sandbox,`git -C ${quote(REPOSITORY_PATH)} rev-parse ${BASE_REF}`);return assertFullSha(t,`target branch revision`),await verifyApplicationRoot(e.sandbox,e.directory),{baseSha:t,directory:e.directory,repository:e.repository,repositoryPath:REPOSITORY_PATH,targetBranch:e.targetBranch}}async function verifyApplicationRoot(e,t){let n=t===`.`?REPOSITORY_PATH:`${REPOSITORY_PATH}/${t}`;if((await e.run({command:`test -d ${quote(`${n}/agent`)}`})).exitCode!==0)throw Error(`Self-modification source directory does not contain agent/: ${t}`)}function quote(e){return shellQuote(e)}async function run(e,t){await runGitCommand(e,t)}export{BASE_REF,REPOSITORY_PATH,SELF_MODIFICATION_CONFIG_PATH,WORKSPACE_PATH,prepareSelfModificationWorkspace,readPreparedSelfModificationWorkspace,verifyApplicationRoot};