Tutorial 5 — SWE-bench-style multi-Scenario Benches
Goal: evaluate an agent against N independent Scenarios, each with its own testdata, then aggregate results into a pass@1 score across the whole Bench. Replicate, in miniature, the structure of SWE-bench — the academic benchmark for LLM software-engineering ability.
You will learn: the scenarios: array with per-Scenario testdata, the --trials N flag for running N trials, the TrialsReport JSON schema (aggregate.totalPassed, aggregate.flakeRate, aggregate.perScenario), --concurrency for parallel execution, and how SWE-bench's vocabulary maps onto madbench primitives.
Prerequisites: Tutorials 1–4. You understand testdata, exec Checks, permission modes, and rubrics.
Cost: N Scenarios × K trials × ~$0.03 each. The example below — 3 Scenarios × 3 trials — is roughly $0.30.
What SWE-bench actually is, in one paragraph
SWE-bench is a benchmark of ~2,000 real GitHub issues from popular Python repos (Django, sympy, scikit-learn, etc.). Upstream SWE-bench calls each dataset row an instance. Madbench deliberately does not add an Instance entity; the same thing is just a Scenario with SWE-bench-shaped metadata: (repo, base_commit, problem_statement, FAIL_TO_PASS, PASS_TO_PASS). The harness clones the repo at base_commit, the agent reads problem_statement and edits the code, then the harness runs the test suite. The Scenario passes when every test in FAIL_TO_PASS now passes (the agent fixed the bug) and every test in PASS_TO_PASS still passes (the agent didn't regress anything). The headline metric is pass@1: the fraction of Scenarios that pass on a single attempt.
Madbench's primitives map cleanly onto every part of that:
| SWE-bench concept | Madbench primitive |
|---|---|
| SWE-bench dataset row | One entry in the scenarios: array, with its own testdata: (a Scenario) |
(repo, base_commit) | testdata/case-NNN/ — the source tree at the right state |
problem_statement | Per-Scenario prompt: |
FAIL_TO_PASS tests | exec [run-the-failing-tests] expected_exit: 0 |
PASS_TO_PASS tests | exec [run-the-rest] expected_exit: 0 (anti-regression) |
| Resolved Scenario (pass) | All Checks pass for this Scenario |
| pass@1 | aggregate.totalPassed / (aggregate.totalPassed + aggregate.totalFailed) |
| pass@k (run K times, count any-success) | --trials K then per-Scenario analysis on aggregate.perScenario |
That is the whole structural translation. The rest of this tutorial builds a working 3-Scenario Bench that demonstrates each piece.
Step 0 — Create the project
mkdir swe-mini
cd swe-mini
mkdir -p testdata/case-001 testdata/case-002 testdata/case-003
Layout you are about to build:
swe-mini/
├── madbench.yaml
└── testdata/
├── case-001/ # arithmetic bug
│ ├── go.mod
│ ├── add.go # ships with `return a - b`
│ └── add_test.go
├── case-002/ # off-by-one bug
│ ├── go.mod
│ ├── slice.go # ships with `s[1:n]` instead of `s[:n]`
│ └── slice_test.go
└── case-003/ # missing-ok-return bug
├── go.mod
├── lookup.go # ships with `return m[key]` (no ok)
└── lookup_test.go
Step 1 — Build the three cases
Each case has the same shape: a Go module, a buggy file, a test file that fails because of the bug. Three different bugs so the agent can't shortcut from one to the next.
testdata/case-001/go.mod
module example.com/swe-mini/case-001
go 1.22
testdata/case-001/add.go
package add
// Add returns the sum of a and b.
func Add(a, b int) int {
return a - b
}
testdata/case-001/add_test.go
package add
import "testing"
func TestAdd(t *testing.T) {
cases := []struct{ a, b, want int }{
{3, 4, 7},
{10, 20, 30},
{-5, 5, 0},
}
for _, c := range cases {
if got := Add(c.a, c.b); got != c.want {
t.Errorf("Add(%d, %d) = %d, want %d", c.a, c.b, got, c.want)
}
}
}
testdata/case-002/go.mod
module example.com/swe-mini/case-002
go 1.22
testdata/case-002/slice.go
package slice
// FirstN returns the first n elements of s. If n > len(s), returns s.
func FirstN(s []int, n int) []int {
if n >= len(s) {
return s
}
return s[1:n] // bug: should be s[:n]
}
testdata/case-002/slice_test.go
package slice
import (
"reflect"
"testing"
)
func TestFirstN(t *testing.T) {
s := []int{1, 2, 3, 4, 5}
if got := FirstN(s, 3); !reflect.DeepEqual(got, []int{1, 2, 3}) {
t.Errorf("FirstN(s, 3) = %v, want [1 2 3]", got)
}
}
testdata/case-003/go.mod
module example.com/swe-mini/case-003
go 1.22
testdata/case-003/lookup.go
package lookup
// Get returns the value for key in m, plus a boolean ok signalling
// whether the key was present.
func Get(m map[string]string, key string) (string, bool) {
return m[key], false // bug: ok is always false; should use the comma-ok idiom
}
testdata/case-003/lookup_test.go
package lookup
import "testing"
func TestGet(t *testing.T) {
m := map[string]string{"a": "1"}
if v, ok := Get(m, "a"); v != "1" || !ok {
t.Errorf("Get(m, \"a\") = (%q, %v), want (\"1\", true)", v, ok)
}
if _, ok := Get(nil, "x"); ok {
t.Errorf("Get(nil, \"x\") returned ok=true, want false")
}
}
Verify the red state of each Scenario before moving on:
for d in testdata/case-00{1,2,3}; do
echo "=== $d ==="
(cd "$d" && go test ./...)
done
Each should fail. If any Scenario is already green, the agent will trivially "pass" it and your Bench-level pass@1 will be inflated.
Three different bugs > three variations of the same bug. SWE-bench's discriminating power comes from Scenario diversity. If all three Scenarios were "wrong arithmetic operator" variants, an agent that solved one would trivially solve the others — not because it can fix bugs, but because the prompt template leaks the answer. Use distinct bug families: arithmetic, off-by-one, missing return value, nil-handling, signature mismatch, etc.
Verify the red state of every Scenario, not just one. This is the parameterized-Bench analog of Tutorial 3's red-state check.
Step 2 — Write madbench.yaml
In the project root (swe-mini/):
description: "swe-mini — 3-scenario bug-fixing bench (SWE-bench-style)"
harness:
type: claude-code
model: claude-haiku-4-5-20251001
args: ["--permission-mode", "bypassPermissions"]
defaults:
sandbox: home
timeout: 120s
capture: log
scenarios:
- name: case-001-arithmetic
testdata: ./testdata/case-001
prompt: |
In the Go module at the current working directory, the test in
add_test.go is failing. Find the bug, fix it, and verify with
'go test ./...'. Modify only the production code, not the tests.
checks:
- type: exec
cmd: go test ./...
expected_exit: 0
timeout: 30s
- type: exec
cmd: grep -c '{3, 4, 7}' add_test.go # PASS_TO_PASS analog
- type: latency
threshold: 60000
- type: cost
threshold: 0.10
- name: case-002-off-by-one
testdata: ./testdata/case-002
prompt: |
In the Go module at the current working directory, the test in
slice_test.go is failing. Find the bug, fix it, and verify with
'go test ./...'. Modify only the production code, not the tests.
checks:
- type: exec
cmd: go test ./...
expected_exit: 0
timeout: 30s
- type: exec
cmd: grep -F 'FirstN(s, 3)' slice_test.go
- type: latency
threshold: 60000
- type: cost
threshold: 0.10
- name: case-003-comma-ok
testdata: ./testdata/case-003
prompt: |
In the Go module at the current working directory, the test in
lookup_test.go is failing. Find the bug, fix it, and verify with
'go test ./...'. Modify only the production code, not the tests.
checks:
- type: exec
cmd: go test ./...
expected_exit: 0
timeout: 30s
- type: exec
cmd: grep -F 'Get(nil, "x")' lookup_test.go
- type: latency
threshold: 60000
- type: cost
threshold: 0.10
What is genuinely new here is per-Scenario testdata:. In every prior tutorial the Bench had a single Scenario pointing at one testdata directory; here, three Scenarios point at three different testdata directories. The harness copies each testdata directory into a fresh tmpdir before that Scenario runs. Scenarios are independent — Scenario 002's agent never sees Scenario 001's edits.
The PASS_TO_PASS pattern in YAML. The second
execin each Scenario (e.g.grep -c "{3, 4, 7}"checking the test cases are still there) is the madbench analog of SWE-bench's PASS_TO_PASS: it asserts the agent didn't regress something that was passing before. In SWE-bench this is a separate test suite the harness runs; in your YAML it is one extraexecper Scenario. Skip it and the agent can "pass" Scenario 001 by deleting test cases — same trap Tutorial 3 warned about, just at scale.The prompt template is identical across Scenarios. The variation lives in the testdata directory, not the prompt. This is the right way: it forces the Bench to grade the agent's capability (find a bug across diverse codebases), not its ability to follow Scenario-specific hints. SWE-bench does the same.
Step 3 — Run once
madbench --report-json out/swe-mini-1run.json
Expected (timings vary):
Bench: swe-mini — 3-scenario bug-fixing bench (SWE-bench-style)
✓ PASS case-001-arithmetic
✓ PASS case-002-off-by-one
✓ PASS case-003-comma-ok
3 passed, 0 failed
That is pass@1 = 3/3 = 100% on a single trial. Don't draw conclusions from one trial — agents are stochastic. Two trials from now you might see one Scenario fail. Onward to several trials.
Step 4 — --trials N for honest pass@1
A single trial is not a credible benchmark. SWE-bench papers report numbers averaged over multiple trials specifically because LLM agents are noisy. Madbench has the --trials N flag for this:
madbench --trials 3 --concurrency 3 --report-json out/swe-mini-3trials.json
--trials 3 runs the whole Bench three times, as three trials. --concurrency 3 lets madbench fan out Scenario execution in parallel within each trial (see Concurrency notes below for what this actually parallelizes).
Expected (the Console opens with one line per trial, then each Scenario's pass rate):
── TRIALS ── 3 × the same bench
trial 1/3 ✓ 3/3 pass ← kept: most typical, reported in full below
trial 2/3 ✗ 2/3 pass · 1 fail
trial 3/3 ✓ 3/3 pass
── RATES ── 3 trials · how often each scenario passed
That is the honest signal: pass@1 = 8/9 ≈ 89%, with one of three Scenarios showing flake.
Step 5 — The TrialsReport JSON schema
out/swe-mini-3trials.json has a different shape from single-trial JSON. The top-level object:
{
"schema_version": 3,
"trials": [
{
"index": 0,
"results": [ { /* ScenarioResult */ }, ... ],
"passed": 3,
"failed": 0,
"durationMs": 24501
},
{ "index": 1, "results": [...], "passed": 2, "failed": 1, ... },
{ "index": 2, "results": [...], "passed": 3, "failed": 0, ... }
],
"aggregate": {
"trialCount": 3,
"totalPassed": 8,
"totalFailed": 1,
"flakeRate": 0.333,
"chosenTrial": 0,
"perScenario": {
"case-001-arithmetic": { "passCount": 3, "failCount": 0, "flakeRate": 0.0 },
"case-002-off-by-one": { "passCount": 2, "failCount": 1, "flakeRate": 0.333 },
"case-003-comma-ok": { "passCount": 3, "failCount": 0, "flakeRate": 0.0 }
}
}
}
The two metrics you will publish:
# pass@1 across all Scenario×trial results
jq '.aggregate.totalPassed / (.aggregate.totalPassed + .aggregate.totalFailed)' \
out/swe-mini-3trials.json
# -> 0.8888888888888888
# Per-Scenario pass rate (which problems is the agent reliably solving?)
jq '.aggregate.perScenario | to_entries | map({
scenario: .key,
passRate: (.value.passCount / (.value.passCount + .value.failCount))
})' out/swe-mini-3trials.json
flakeRate is symmetric — it tops out at 0.5 for a perfect 50/50 split and drops to 0 for either all-pass or all-fail. A high flake rate on one specific Scenario is your prompt-design loop: the prompt is ambiguous, or the testdata directory is too small for the Model to disambiguate, or the Check is too strict. Iterate on that one Scenario, not the whole Bench.
flakeRatedistinguishes "hard" from "noisy." A Scenario that fails 3/3 is hard — the agent genuinely can't solve it at this Model tier. A Scenario that fails 1/3 is noisy — the agent can solve it but doesn't always. Different problems, different fixes: hard means switch Models or rewrite the prompt; noisy means run more trials and report the mean.
totalPassed / (totalPassed + totalFailed)is the mean per-trial pass rate. With--trials 1it is exactly pass@1. With higher K it is pass@1 averaged across K trials. To compute pass@k (any-success in K trials), iterate overaggregate.perScenarioand count Scenarios wherepassCount > 0.
Step 6 — Scaling beyond three Scenarios
A real Bench can have hundreds of Scenarios. Some practical considerations once you cross ~20:
- Testdata sizes matter. 200 Scenarios x 1 MB each is 200 MB of git history, and madbench copies each one into a tmpdir per run. Keep testdata directories lean — stripped repos with only the touched packages, not whole monorepo snapshots.
--concurrencylets Scenarios share a clock. With--concurrency 8, eight Scenarios run simultaneously, so a 200-Scenario Bench finishes in ~1/8 wall-clock time. LLM API rate limits become the bottleneck before CPU does — Anthropic's tier-1 limits are around 50 requests/minute on Haiku, which caps useful concurrency at roughly 10–15.- Cost ceilings per Scenario, not per Bench. Keep the
cost: 0.10ceiling on each Scenario as a per-Scenario circuit breaker. Aggregate Bench cost is the sum, but a runaway single Scenario is what you want to catch — and a per-Scenario ceiling does that. - Sandbox isolation matters more. With one Scenario, a bug in the testdata directory is annoying. With 200, a single mis-built testdata directory (e.g., one whose
go.modreferences a non-existent package) drags the aggregate down by 0.5%. Verify the red state of every Scenario, not just spot-check. - Don't aggregate
latencyacross Scenarios. Per-Scenario latency ceilings catch tool loops; Bench-wide aggregates blur the signal. Keep them per Scenario.
The
scenarios:array is a list, not a matrix. Madbench does not (yet) have a "for each testdata directory, expand into a Scenario" template feature — you write N entries by hand or generate the YAML with a script. For 3 Scenarios, hand-write. For 30, generate (a Go program that emits the YAML keeps generation reproducible). For 300, you are building infrastructure, not a Bench — talk to the harness maintainers before going further.
Concurrency notes
--concurrency N controls parallel Scenario execution within one trial. With --trials K --concurrency N, the Bench is run K times sequentially, and within each trial up to N Scenarios execute in parallel. So --trials 3 --concurrency 4 against a 3-Scenario Bench = 3 sequential trials x 3 parallel Scenarios each = ~3x wall-clock speedup vs. fully sequential, capped by the smaller of Scenario count and concurrency.
How this maps onto real SWE-bench
If you wanted to actually port a real SWE-bench-Lite dataset row (say, django__django-11099) into this exact YAML shape, the work is:
- Clone Django at the row's
base_commitintotestdata/django-11099/. - Copy the issue's
problem_statementinto the per-Scenarioprompt. - Write the
execCheck to run the FAIL_TO_PASS tests:cmd: [python, -m, pytest, "tests/auth_tests/test_basic.py::SpecificTest::test_method"]. - Add a second
execfor PASS_TO_PASS — the existing-tests-don't-regress check. - Switch the harness config to use a Python-aware permission mode and ensure
claudehas access topytest.
The shape of the YAML is identical to what you wrote in step 2 — just larger testdata and Python-flavored exec cmds. The harness primitives don't change.
What you now know
| Concept | Why it matters |
|---|---|
scenarios: array with per-Scenario testdata: | One Bench, N independent Scenarios |
--trials N | Honest pass@1 requires multiple trials; agents are stochastic |
--concurrency N | Parallel Scenario execution; rate-limited by API tier |
TrialsReport JSON schema | aggregate.totalPassed, aggregate.flakeRate, aggregate.perScenario |
| pass@1 / pass@k | The right metrics for stochastic agents |
| PASS_TO_PASS (anti-regression) | One extra exec per Scenario, not a separate Bench |
| Hard vs. noisy Scenarios | Distinguishable via perScenario.flakeRate |
What comes next
You now have every primitive needed to build a real domain-specific Bench — pick 30 bug-fix tasks from your team's bug tracker, parameterize them, run nightly, watch the dashboards. The remaining problems are not "how do I write a Bench" — they are sociotechnical:
- Data refresh — testdata directories drift as upstream changes.
- Statistical comparison — when is a Model A → B improvement "real"?
- Cost budgeting — running 300 Scenarios × 5 trials × $0.05 = $75 per full benchmark run.
- Session analysis at scale — when 30 Scenarios flake, which tool-call patterns recur?
For these, the SWE-bench papers and the Aider polyglot benchmark discuss what we (the field) have figured out so far. Madbench gives you the harness; the methodology is yours to choose.
Previous: Tutorial 4 — semantic judging with llm-rubric
Index: docs/tutorials/