import asyncio
from boxlite import CodeBox
from openai import OpenAI
client = OpenAI() # reads OPENAI_API_KEY (and OPENAI_BASE_URL if set)
# --- A simulated untrusted PR: the baseline is correct, the PR breaks the tax math ---
BASE_CODE = (
'def add_tax(amount, rate):\n'
' """Add tax to amount. rate is a fraction, e.g. 0.1 for 10%."""\n'
' return amount + amount * rate\n'
)
PR_CODE = (
'def add_tax(amount, rate):\n'
' """Add tax to amount. rate is a fraction, e.g. 0.1 for 10%."""\n'
' # PR change: an "optimization" that introduces a bug\n'
' return amount + rate\n'
)
TEST_CODE = (
'from billing import add_tax\n\n'
'def test_ten_percent():\n'
' assert add_tax(100, 0.1) == 110.0\n\n'
'def test_zero_rate():\n'
' assert add_tax(50, 0) == 50\n'
)
# Install git inside the box. DEBIAN_FRONTEND keeps apt from waiting on a prompt.
SETUP = r"""
set -e
export DEBIAN_FRONTEND=noninteractive
apt-get update -qq
apt-get install -y --no-install-recommends git ca-certificates >/dev/null
pip install -q pytest >/dev/null
git config --global user.email ci@example.com
git config --global user.name "ci-bot"
git config --global init.defaultBranch main
mkdir -p /work && cd /work && git init -q
"""
def _write(path: str, content: str) -> str:
"""Build a heredoc command that writes multi-line content to a file in the box."""
return f"cd /work && cat > {path} <<'BOXLITE_EOF'\n{content}BOXLITE_EOF\n"
async def review_pr() -> str:
# disk_size_gb=4: the default root filesystem is too small for git
async with CodeBox(disk_size_gb=4) as box:
# 1) Install git and pytest, initialize an isolated repository
setup = await box.exec("bash", "-lc", SETUP)
if setup.exit_code != 0:
raise RuntimeError(f"environment setup failed:\n{setup.stderr}")
# 2) Baseline commit
await box.exec("bash", "-lc",
_write("billing.py", BASE_CODE) +
"git add -A && git commit -q -m 'base'")
# 3) PR branch with the untrusted change and its tests
await box.exec("bash", "-lc", "cd /work && git checkout -q -b pr-42")
await box.exec("bash", "-lc",
_write("billing.py", PR_CODE) +
_write("test_billing.py", TEST_CODE) +
"git add -A && git commit -q -m 'PR #42'")
# 4) Extract the diff to review
diff = await box.exec("bash", "-lc",
"cd /work && git diff main...pr-42 -- billing.py")
# 5) Run the tests inside the microVM — the only place untrusted code executes.
# exec does not raise on failure; read exit_code to decide pass/fail.
test = await box.exec("bash", "-lc", "cd /work && python -m pytest -q")
tests_passed = test.exit_code == 0
# 6) Hand the diff and results to the model for a verdict
prompt = (
"You are a strict CI reviewer. A pull request modified billing.py.\n\n"
f"DIFF:\n{diff.stdout}\n\n"
f"Test output (exit_code={test.exit_code}):\n{test.stdout}\n\n"
"Reply with one line, APPROVE or REQUEST_CHANGES, then two sentences of reasoning."
)
response = client.chat.completions.create(
model="<YOUR_MODEL>", # e.g. "gpt-4o-mini", or any model id your endpoint serves
messages=[{"role": "user", "content": prompt}],
)
verdict = response.choices[0].message.content.strip()
return (
f"tests passed: {tests_passed} (pytest exit_code={test.exit_code})\n"
f"--- test output ---\n{test.stdout.strip()}\n"
f"--- review ---\n{verdict}"
)
async def main() -> None:
try:
print(await review_pr())
except RuntimeError as exc:
print(f"sandbox failed to start: {exc}")
asyncio.run(main())