UNPKG

eve

Version:

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

2 lines 3.94 kB
import{assertFullSha}from"./identifiers.js";import{gitOutput,runGitCommand}from"./git.js";import{createHash}from"node:crypto";import{shellQuote}from"#shared/shell-quote.js";const MAX_PROPOSAL_CHANGED_BYTES=1e6,MAX_PROPOSAL_CHANGED_FILES=100;async function captureSelfModificationProposal(e){let t=quote(e.workspace.repositoryPath);assertFullSha(e.workspace.baseSha,`proposal base revision`),await runGitCommand(e.sandbox,`git -C ${t} add -A -- .`);let n=await gitOutput(e.sandbox,`git -C ${t} write-tree`),r=await gitOutput(e.sandbox,`git -C ${t} rev-parse ${quote(`${e.workspace.baseSha}^{tree}`)}`);assertFullSha(n,`proposal tree`),assertFullSha(r,`proposal base tree`);let i=parseRawDiff(await gitOutput(e.sandbox,`git -C ${t} diff-tree -r --no-commit-id --raw -z --no-renames ${quote(e.workspace.baseSha)} ${quote(n)}`,!1));if(i.length===0)throw Error(`Self-modification proposal contains no changes.`);if(i.length>100)throw Error(`Self-modification proposal changes ${i.length} files; limit is 100.`);let a=[],o=0;for(let t of i){let n=t.status===`D`?null:proposalMode(t.newMode,t.path),r=t.status===`D`?null:t.newObjectId;assertAllowedChange({mode:n,objectId:r,path:t.path},e.workspace.directory);let i=r===null?0:await blobSize(e.sandbox,e.workspace.repositoryPath,r);if(o+=i,o>1e6)throw Error(`Self-modification proposal changes more than ${MAX_PROPOSAL_CHANGED_BYTES} bytes.`);a.push({bytes:i,kind:t.status===`A`?`add`:t.status===`D`?`delete`:`modify`,mode:n,objectId:r,path:t.path})}return{baseSha:e.workspace.baseSha,baseTreeSha:r,changedBytes:o,changes:a,proposedTreeSha:n}}async function readProposalBlob(e){if(e.change.objectId===null)throw Error(`Cannot read a deleted proposal blob.`);let t=await gitOutput(e.sandbox,`git -C ${quote(e.workspace.repositoryPath)} cat-file blob ${quote(e.change.objectId)} | base64 | tr -d '\\n'`),n=Buffer.from(t,`base64`),r=createHash(`sha1`).update(`blob ${n.byteLength}\0`).update(n).digest(`hex`);if(n.byteLength!==e.change.bytes||r!==e.change.objectId.toLowerCase())throw Error(`Self-modification proposal blob changed after validation: ${JSON.stringify(e.change.path)}.`);return t}function parseRawDiff(e){if(e.length===0)return[];let t=e.split(`\0`);if(t.at(-1)===``&&t.pop(),t.length%2!=0)throw Error(`Git returned malformed raw diff output.`);let n=[];for(let e=0;e<t.length;e+=2){let r=t[e],i=t[e+1],a=r===void 0?null:/^:[0-7]{6} ([0-7]{6}) [a-f0-9]{40} ([a-f0-9]{40}) ([ADM])$/u.exec(r);if(a===null||i===void 0||!isSafePath(i))throw Error(`Git returned malformed raw diff output.`);n.push({newMode:a[1],newObjectId:a[2],path:i,status:a[3]})}return n}function assertAllowedChange(e,t){let n=e.path,r=t===`.`?``:`${t}/`,i=`${r}agent/`,a=n.split(`/`),o=a.at(-1)??``;if(!isSafePath(n)||a.includes(`.git`)||o===`.env`||o===`.envrc`||o.startsWith(`.env.`)||a.some(e=>[`node_modules`,`dist`,`build`,`.next`,`.output`,`.turbo`,`.vercel`,`coverage`,`out`].includes(e))||n.startsWith(`.github/workflows/`)||n.startsWith(`${i}subagents/self-modification/`)||n===`${r}agent/extensions/selfmod.ts`||n===`${r}agent/self-modification.config.ts`)throw Error(`Self-modification proposal changes a protected path: ${JSON.stringify(n)}.`)}function isSafePath(e){return e.length>0&&!e.startsWith(`/`)&&!e.includes(`\\`)&&!e.includes(` `)&&!e.includes(`\r`)&&!e.split(`/`).some(e=>e===``||e===`.`||e===`..`)}function proposalMode(e,t){if(e===`100644`||e===`100755`)return e;throw Error(`Self-modification proposal uses unsafe Git mode ${e}: ${JSON.stringify(t)}.`)}async function blobSize(e,t,n){assertFullSha(n,`proposal blob`);let r=await gitOutput(e,`git -C ${quote(t)} cat-file -s ${quote(n)}`);if(!/^\d+$/u.test(r)||!Number.isSafeInteger(Number(r)))throw Error(`Git returned an invalid blob size.`);return Number(r)}function quote(e){return shellQuote(e)}export{MAX_PROPOSAL_CHANGED_BYTES,MAX_PROPOSAL_CHANGED_FILES,assertAllowedChange,captureSelfModificationProposal,parseRawDiff,readProposalBlob};