Foundational Compute

Test a change to a repo

Run the tests, apply a change, test again and get the diff.

Templatebase (2 vCPU, 512 MiB, $0.039 per hour)
Run timeAbout 15 seconds for this small repo
Cost of a runLess than $0.001. A 10-minute suite costs $0.0065.
CleanupThe 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.

Python · TypeScript

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()

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_file and run_shell on the same sandbox. Give an agent a code tool
  • Claude Code. Use the claude template and run claude -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 -A puts 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

Next