kosa8 / Guides

Guide · Evals

How do I run SWE-bench-style evals in parallel sandbox forks?

Updated

Build the repository at its base commit into a kosa8 template once, start a base sandbox from it with the network closed, then fork the base into one live copy per task, in batches. In each copy apply the model's patch and the hidden test patch, run the tests that should now pass and the ones that must keep passing, record the result and delete the copy. Every task starts from an identical machine, nothing is reinstalled per task, and one task cannot affect another.

01Steps

  1. Install kosa8 and the Python SDK. Forks need a plan; your plan sets how many copies run at once, so size the batch to it.

  2. Describe the environment in a Dockerfile. One image per repository and base commit, with git, the project and its test dependencies installed.

    env/Dockerfile
    FROM python:3.12-slim
    ARG COMMIT
    RUN apt-get update && apt-get install -y --no-install-recommends git && rm -rf /var/lib/apt/lists/*
    RUN git clone https://github.com/pallets/click /repo && git -C /repo checkout "$COMMIT" \
        && pip install --no-cache-dir -e /repo pytest
  3. Build it into a template. The build runs in a throwaway sandbox with network; the sandboxes made from the template need none.

  4. Create the base with the network closed. --egress deny means nothing leaves, DNS included, so neither an agent nor a test can fetch an answer. Forks inherit it.

  5. Checkpoint the base. If anything ever changes the base, rewind puts it back.

  6. Write the tasks file. One JSON line per instance of this repository and commit, with SWE-bench-style fields: instance_id, model_patch, test_patch, FAIL_TO_PASS and PASS_TO_PASS (lists of test ids).

  7. Fork, score and clean up. The script forks one batch at a time, scores each task in its own copy in parallel, and deletes every copy even if scoring fails.

    eval.py
    import json
    from concurrent.futures import ThreadPoolExecutor
    from kosa8 import Kosa8Client
     
    client = Kosa8Client()
    BASE, BATCH = "base-click", 8
    tasks = [json.loads(line) for line in open("tasks.jsonl")]   # one repository and commit
     
    def score(sid, task):
        client.write_file(sid, "/workspace/model.diff", task["model_patch"])
        client.write_file(sid, "/workspace/tests.diff", task["test_patch"])
        applied = client.shell(sid, "cd /repo && git apply /workspace/model.diff && git apply /workspace/tests.diff")
        if not applied.ok:
            return task["instance_id"], False
        tests = " ".join(task["FAIL_TO_PASS"] + task["PASS_TO_PASS"])
        r = client.shell(sid, f"cd /repo && python -m pytest -q {tests}", timeout_s=900)
        return task["instance_id"], r.ok
     
    results = {}
    for i in range(0, len(tasks), BATCH):
        batch = tasks[i:i + BATCH]
        forks = [f["id"] for f in client.fork(BASE, len(batch), prefix=f"b{i}-")["forks"]]
        try:
            with ThreadPoolExecutor(len(forks)) as pool:
                results.update(pool.map(score, forks, batch))
        finally:
            for sid in forks:
                client.delete_sandbox(sid)
     
    print(f"resolved {sum(results.values())}/{len(results)}")
    json.dump(results, open("results.json", "w"), indent=2)

02Running the agent in the fork before scoring

To evaluate an agent rather than a precomputed patch, let it work in the fork first and score afterwards. With the OpenAI Agents SDK, for example, give it sandbox_tools(client, sid) from kosa8.integrations.openai_agents, tell it to fix the issue in /repo, and only then write the test patch in and run the tests. The agent never sees the hidden tests, and with the base's network closed it cannot fetch the upstream fix.

03Why fork instead of starting a fresh container per task?

A fork restores the whole machine, memory, processes and disk, from one snapshot of the prepared base, so the clone, the install and the warm shell session are paid for once instead of once per task. Every task starts from exactly the same state, which removes one source of flaky results.

kosa8 has not published timings for this pattern; measure on your own hardware before planning a large run.

04Caveats

  • Not the official harness. Published SWE-bench results come from its own Docker-image harness. Use this pattern for fast iteration; use the official harness for numbers you report.
  • One template per repository and commit. Tasks from different commits need different bases.
  • Resources. Every fork is a microVM with its own memory; set --memory on the base to what the tests need and keep the batch within what the machine holds.

05FAQ

How many tasks can run at once?

As many forks as your plan allows at once and your machine's memory holds; the script's BATCH sets it.

What if a patch does not apply?

The task is scored as unresolved and its fork is deleted like any other.

Can I run this on kosa8 Cloud?

The script uses the local Python client. kosa8 Cloud sandboxes are driven with Kosa8Cloud, which creates, runs in, sleeps and deletes sandboxes.

How do I see what happened in a task?

Every command run in every fork is in the audit log (kosa8 audit ls). To keep a fork for inspection, skip its delete_sandbox call.

06Get started

Install kosa8 on a Mac, Linux or Windows (WSL2) machine, or make an account and run the same sandboxes on kosa8 Cloud.

07Sources

Related

Updated 2026-11-14