import asyncio
from contree_sdk import ContreeAsyncSession, create_sandbox_session
TEST_SOURCE = """
from calc import add
assert add(2, 3) == 5
assert add(-1, 1) == 0
print("PASS")
"""
BROKEN_SOURCE = """
def add(a, b):
return a - b
"""
MULTIPLY_SOURCE = """
def add(a, b):
return a * b
"""
ADDITION_SOURCE = """
def add(a, b):
return a + b
"""
FOLLOW_UP_SOURCE = """
from calc import add
assert add(10, 7) == 17
print("FOLLOW-UP PASS")
"""
async def main() -> None:
async with create_sandbox_session(
image="tag:python:3.12-alpine",
session_type=ContreeAsyncSession,
) as setup:
prepared = await setup.run(
shell="true",
files={"/repo/calc.py": BROKEN_SOURCE.encode(), "/repo/test_calc.py": TEST_SOURCE.encode()},
timeout=60,
)
if prepared.state.exit_code != 0 or not setup.image_uuid:
raise RuntimeError("Preparation failed or returned no saved image")
baseline_uuid = setup.image_uuid
baseline_test = await setup.run(shell="python test_calc.py", cwd="/repo", disposable=True, timeout=30)
if baseline_test.state.exit_code != 1:
raise RuntimeError("The baseline should fail before either patch is applied")
print(f"Prepared test image: {baseline_uuid}")
print("Baseline fails as expected; trying both fixes")
multiply, addition = await asyncio.gather(
try_patch(baseline_uuid, MULTIPLY_SOURCE),
try_patch(baseline_uuid, ADDITION_SOURCE),
)
multiply_result, _ = multiply
addition_result, selected_uuid = addition
outcomes = {"multiply": multiply_result.state.exit_code, "add": addition_result.state.exit_code}
if outcomes != {"multiply": 1, "add": 0}:
raise RuntimeError("Expected the multiplication patch to fail and the addition patch to pass")
if addition_result.stdout.as_text() != "PASS\n" or not selected_uuid:
raise RuntimeError("Passing patch did not return PASS output and a saved image")
async with create_sandbox_session(
image=selected_uuid,
session_type=ContreeAsyncSession,
) as selected:
if await selected.read_file("/repo/calc.py") != ADDITION_SOURCE.encode():
raise RuntimeError("Saved source does not contain the selected addition fix")
print(f"Patch results: {outcomes}; continuing from {selected_uuid}")
continued = await selected.run(
shell="python test_calc.py && python follow_up.py",
files={"/repo/follow_up.py": FOLLOW_UP_SOURCE.encode()},
cwd="/repo",
disposable=True,
timeout=30,
)
if continued.state.exit_code != 0 or continued.stdout.as_text() != "PASS\nFOLLOW-UP PASS\n":
raise RuntimeError("The selected image failed the follow-up checks")
print(continued.stdout.as_text(), end="")
print(f"baseline={baseline_uuid} chosen={selected_uuid} outcomes={outcomes}")
async def try_patch(image: str, source: str):
async with create_sandbox_session(
image=image,
session_type=ContreeAsyncSession,
) as branch:
result = await branch.run(
shell="python test_calc.py",
files={"/repo/calc.py": source.encode()},
cwd="/repo",
disposable=False,
timeout=30,
)
if result.stdout.truncated is True or result.stderr.truncated is True:
raise RuntimeError("Patch output was truncated")
return result, branch.image_uuid
asyncio.run(main())