Test a change to a repo
Run the tests, apply a change, test again and get the diff.
| Template | base (2 vCPU, 512 MiB, $0.039 per hour) |
|---|---|
| Run time | About 15 seconds for this small repo |
| Cost of a run | Less than $0.001. A 10-minute suite costs $0.0065. |
| Cleanup | The finally block kills the sandbox. Copy out the diff and reports first. The 15-minute lifetime ends it if your process stops first. |
1The code
The code uses the public colorama repo. In step 3, a fixed file write stands in for your agent.
repo_agent.py
from fdtn import Sandbox, CommandExitException REPO = "https://github.com/tartley/colorama" WORK = "/home/user/repo" def sh(sbx, cmd, timeout=300): """Run a command in the repo. Return (exit code, output). Do not raise.""" try: r = sbx.commands.run(cmd, cwd=WORK, timeout=timeout) return 0, r.stdout + r.stderr except CommandExitException as e: return e.exit_code, e.stdout + e.stderr sbx = Sandbox.create( "base", timeout=900, # the sandbox lives 15 minutes at most metadata={"example": "repo-agent"}, ) try: # 1. Get the code and the test tools. sbx.commands.run(f"git clone --depth 1 {REPO} {WORK}", timeout=120) sbx.commands.run("pip install --quiet pytest", timeout=180) # 2. Run the tests before the change. code, out = sh(sbx, "python3 -m pytest -q") print("before:", out.strip().splitlines()[-1]) # 3. The agent changes the code. Here a fixed edit stands in for the agent. # Put your agent call here, for example `claude -p "..."` in the claude template. sbx.files.write(f"{WORK}/colorama/tests/version_test.py", "import colorama\n\n" "def test_version_is_set():\n" " assert colorama.__version__.count('.') >= 2\n") # 4. Run the tests again and get the diff. code, out = sh(sbx, "python3 -m pytest -q") print("after: ", out.strip().splitlines()[-1], f"(exit {code})") sh(sbx, "git add -A") _, diff = sh(sbx, "git diff --cached --stat && git diff --cached") print(diff) finally: sbx.kill()
import { Sandbox, CommandExitError } from '@fdtn/sdk' const REPO = 'https://github.com/tartley/colorama' const WORK = '/home/user/repo' // Run a command in the repo. Return [exit code, output]. Do not throw. async function sh(sbx, cmd, timeoutMs = 300_000) { try { const r = await sbx.commands.run(cmd, { cwd: WORK, timeoutMs }) return [0, r.stdout + r.stderr] } catch (e) { if (e instanceof CommandExitError) return [e.exitCode, e.stdout + e.stderr] throw e } } const last = (s) => s.trim().split('\n').at(-1) const sbx = await Sandbox.create('base', { timeoutMs: 900_000, // the sandbox lives 15 minutes at most metadata: { example: 'repo-agent' }, }) try { // 1. Get the code and the test tools. await sbx.commands.run(`git clone --depth 1 ${REPO} ${WORK}`, { timeoutMs: 120_000 }) await sbx.commands.run('pip install --quiet pytest', { timeoutMs: 180_000 }) // 2. Run the tests before the change. let [code, out] = await sh(sbx, 'python3 -m pytest -q') console.log('before:', last(out)) // 3. The agent changes the code. Here a fixed edit stands in for the agent. await sbx.files.write(`${WORK}/colorama/tests/version_test.py`, 'import colorama\n\n' + 'def test_version_is_set():\n' + " assert colorama.__version__.count('.') >= 2\n") // 4. Run the tests again and get the diff. ;[code, out] = await sh(sbx, 'python3 -m pytest -q') console.log('after: ', last(out), `(exit ${code})`) await sh(sbx, 'git add -A') const [, diff] = await sh(sbx, 'git diff --cached --stat && git diff --cached') console.log(diff) } finally { await sbx.kill() }
2Run it
$ python repo_agent.py # or: node repo_agent.mjs before: 38 passed, 14 skipped in 0.12s after: 39 passed, 14 skipped in 0.06s (exit 0) colorama/tests/version_test.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/colorama/tests/version_test.py b/colorama/tests/version_test.py new file mode 100644 index 0000000..cae2c1f --- /dev/null +++ b/colorama/tests/version_test.py @@ -0,0 +1,4 @@ +import colorama + +def test_version_is_set(): + assert colorama.__version__.count('.') >= 2
3Put your agent in
Replace the fixed write in step 3 with one of these:
- A patch. Write it to a file and run
git apply /home/user/change.patch. A bad patch exits with code 1. - Tools. Give the agent
read_file,write_fileandrun_shellon the same sandbox. Give an agent a code tool - Claude Code. Use the
claudetemplate and runclaude -p "your task"in the repo. Run Claude Code from code
Notes
sh()does not raise: a failed test run is a result, not an error.git add -Aputs new files in the diff.- For a private repo, use a read-only token for that one repo. Security
- For a large repo, build a template with the repo and its packages. Templates