Skip to main content

Tutorial 3 — Code generation with exec Checks

Goal: let the agent write a new file from scratch, then run go test against it inside the sandbox to grade correctness.

You will learn: the exec Check (the workhorse of code-writing Benches), permission modes (default → acceptEdits → bypassPermissions), and anti-cheat Checks that prevent the agent from "passing" the test by deleting it.

Prerequisites: Tutorial 2 (you have run a live Bench and read its session). ANTHROPIC_API_KEY exported.

Cost: roughly $0.03–$0.07 per run on Haiku 4.5.

Why exec changes everything​

In Tutorial 2 you graded the agent's answer. That works for Q&A. For code generation, the answer is the code itself — and you do not want to grade prose like "I have written a FizzBuzz function". You want to know: does it pass go test?

exec runs a shell command inside the agent's sandboxed working directory after the run completes, and asserts on its exit code. It is the single most common Check in real-world Benches. Almost every code-writing Scenario you write will start with one exec running the test suite.

Step 0 — Create the project​

mkdir codegen-fizzbuzz
cd codegen-fizzbuzz

Layout you are about to build:

codegen-fizzbuzz/
├── madbench.yaml
└── code/
├── go.mod
└── fizzbuzz_test.go # references FizzBuzz, which doesn't exist yet

Step 1 — Build the testdata (the "red state")​

Fast path — you can skip the inline files below and curl the prebuilt testdata from madbench-examples:

curl -sL https://github.com/MadAppGang/madbench-examples/tarball/main \
| tar -xz --strip-components=2 \
'MadAppGang-madbench-examples-*/03-codegen-with-exec/code'

The command drops a code/ directory with the testdata files into your current directory. Then jump to Step 2. The inline walkthrough below is for readers who want to see the testdata structure as they type — and is still the place to read about red-state verification (which the download skips past).

mkdir -p code

code/go.mod

module example.com/fizzbuzz

go 1.22

code/fizzbuzz_test.go

package fizzbuzz

import "testing"

func TestFizzBuzz(t *testing.T) {
cases := []struct {
in int
want string
}{
{1, "1"},
{2, "2"},
{3, "Fizz"},
{4, "4"},
{5, "Buzz"},
{6, "Fizz"},
{9, "Fizz"},
{10, "Buzz"},
{15, "FizzBuzz"},
{30, "FizzBuzz"},
{45, "FizzBuzz"},
{7, "7"},
{11, "11"},
}
for _, c := range cases {
if got := FizzBuzz(c.in); got != c.want {
t.Errorf("FizzBuzz(%d) = %q, want %q", c.in, got, c.want)
}
}
}

This test references a FizzBuzz(n int) string function that does not exist yet. Verify the red state by hand before running the Bench:

cd code
go test ./...

You should see:

./fizzbuzz_test.go:35:14: undefined: FizzBuzz
FAIL example.com/fizzbuzz [build failed]

cd back to the project root afterwards.

If go test already passes, your Bench grades nothing. A green testdata means the agent can do nothing and still pass. Red-state verification is the cheapest sanity check you have when authoring a Bench — do it before you spend a single token on a live run.

A build failure counts as red. The agent fixes both compile errors and test-logic errors with the same exec [go test ./...] Check. You don't need a separate go build step.

Step 2 — Write madbench.yaml​

In the project root (codegen-fizzbuzz/):

description: "codegen — agent writes FizzBuzz, harness runs go test"
harness:
type: claude-code
model: claude-haiku-4-5-20251001
args: ["--permission-mode", "acceptEdits"] # NEW: see step 3
defaults:
sandbox: home
timeout: 120s
capture: log

scenarios:
- name: write-fizzbuzz
testdata: ./code
prompt: |
In the Go module at the current working directory there is a
failing test in fizzbuzz_test.go that calls a FizzBuzz(n int)
string function. That function does not exist yet.

Create fizzbuzz.go in this package with the FizzBuzz function
so that 'go test ./...' passes. Use idiomatic Go.
checks:
- type: exec
cmd: go test ./...
expected_exit: 0 # default 0; written here for clarity
timeout: 30s
- type: latency
threshold: 60000
- type: cost
threshold: 0.10

The new line is args: ["--permission-mode", "acceptEdits"]. Without it, the agent will silently no-op every Write call.

Step 3 — Permission modes (the decision tree)​

Claude Code in --print non-interactive mode silently skips any tool call that would normally prompt the user. Three modes, three use cases:

ModeUse whenTutorial reference
defaultRead-only Benches. Read, Grep, Glob never prompt.Tutorial 2 (codenav)
acceptEditsAgent writes/edits files but the harness runs commands.This tutorial
bypassPermissionsAgent edits and runs Bash itself (e.g. go test).Tutorial 4

This Bench uses acceptEdits because:

  • The agent must Write fizzbuzz.go (so default would silently no-op).
  • The harness runs go test, not the agent (so bypassPermissions is overkill).

Symptom of permission-mode mismatch: the agent's finalOutput reads "Please approve Write for fizzbuzz.go" instead of an actual answer. Bump from default to acceptEdits.

bypassPermissions sounds dangerous and isn't, here. The agent runs in a fresh tmpdir holding a copy of the testdata directory. There is nothing of yours for it to harm. The only reason not to default to bypassPermissions is hygiene: tighter modes make it impossible for the agent to do things you didn't intend, which makes Benches more reproducible.

You cannot fix a mode mismatch by tweaking the prompt. "Please write the file" reaches the model and the model tries to call Write, which the harness silently drops. The fix is always the YAML flag.

Step 4 — The exec Check shape​

- type: exec
cmd: go test ./...
expected_exit: 0 # default 0; written here for clarity
timeout: 30s # default 60s

The shape:

FieldMeaning
cmdThe command as you would type it. A string runs in bash, so pipes and && work, and stops at its first failing command; a list is the exact argv, with no shell (how a command runs).
shellOptional. The shell a string cmd runs in: bash (default), zsh or sh (how a command runs).
expected_exitThe exit code that means pass. Default 0. Use a non-zero value when you are asserting something should fail (rare).
cwdOptional subdirectory under Session.WorkDir. Default is the workdir root.
timeoutPer-command wall-clock cap.

When exec fails, the Check's reason field carries the last 2 KB of combined stdout+stderr — usually the exact compile error or test diff. That is your debugger. A passing exec keeps the same tail in evidence.output, so you can still read what go test printed when it went green.

Step 5 — Add anti-cheat Checks​

A shockingly common failure mode: the agent makes the test "pass" by deleting the test cases that fail. Or by mutating fizzbuzz_test.go to assert against Foo instead of FizzBuzz. Or by hardcoding the inputs the test happens to check.

Append three more Checks to defend against these. Final checks: list:

checks:
# 1) Primary: tests pass.
- type: exec
cmd: go test ./...
expected_exit: 0
timeout: 30s

# 2) Did the agent actually write a FizzBuzz function?
- type: exec
cmd: grep -q 'func FizzBuzz' fizzbuzz.go
expected_exit: 0

# 3) Did the agent use a write/edit tool?
- type: any-of
checks:
- type: session:tool-used
value: "Write"
- type: session:tool-used
value: "Edit"

# 4) Are at least 4 "FizzBuzz" string literals still in the test file?
# (catches the cheat where the agent fixed tests by deleting cases)
- type: exec
cmd: test "$(grep -c "\"FizzBuzz\"" fizzbuzz_test.go)" -ge 4
expected_exit: 0

- type: latency
threshold: 60000
- type: cost
threshold: 0.10

The pattern is general: for every "smart" way the agent could pass without doing the work, write one exec that catches it. This is not paranoia — it is the difference between a Bench that measures capability and a Bench that measures specification-gaming.

Anti-cheat Checks are about the Check shape, not the agent's intent. The agent isn't trying to cheat. It is doing whatever the gradient pulled it toward. If your Bench's only signal is go test ./... exit 0, the agent's policy treats "delete failing cases" as a valid solution, because from the agent's view it is. The fix is to grade more dimensions, not to lecture the agent in the prompt.

Step 6 — Run it and inspect​

madbench --report-json out/fizzbuzz.json

Expected (timings vary):

Bench: codegen — agent writes FizzBuzz, harness runs go test
✓ PASS write-fizzbuzz
✓ exec: exec [go test ./...]: exit 0
✓ exec: exec [grep -q func FizzBuzz fizzbuzz.go]: exit 0
✓ any-of: any-of: at least one child passed
✓ exec: exec [sh -c ...]: exit 0
✓ latency: 12483ms (threshold: 60000ms)
✓ cost: $0.0341 (threshold: $0.10)
1 passed, 0 failed

What changed in the report from Tutorial 2:

# The agent wrote a file. See it in the session:
jq '.results[0].session.calls[] | select(.name == "Write")' out/fizzbuzz.json

# Cost went up ~15× from Tutorial 2. Codegen is more expensive than nav.
jq '.results[0].session.metrics.cost' out/fizzbuzz.json

Step 7 — Force a permission-mode mismatch​

Edit the YAML, change the permission mode:

args: ["--permission-mode", "default"] # was: acceptEdits

Re-run. You will see exec [go test ./...] fail because no fizzbuzz.go was written, and finalOutput will be a polite refusal asking you to approve the Write. This is what the symptom looks like in the wild — internalize it, then restore the value.

Step 8 — Cost variance is real​

Run the Bench three times. You will see different costs each run — sometimes the agent reads go.mod first, sometimes it doesn't, sometimes it runs Bash to verify (and is silently denied because we're in acceptEdits). Across three observed runs you might see $0.031, $0.045, $0.062. That is 2× variance on identical inputs, and it is normal.

The implication for the cost: 0.10 ceiling: it is set high on purpose. The point is to catch runaway behavior (tool loops, infinite reads), not to fit the median sample. If you tighten it to $0.04 because run #1 was cheap, run #4 will flake and you will have a flaky CI gate.

What you now know​

ConceptWhy it matters
exec CheckThe workhorse of code-writing Benches
cmd: go test ./...A string is a shell line; a list is the exact argv
--permission-mode acceptEditsRequired for Write/Edit in non-interactive mode
Permission-mode decision treedefault → acceptEdits → bypassPermissions
Anti-cheat patternsOne exec per failure-mode you can think of
Cost variance2× run-to-run is normal; set ceilings for pathology, not median

What you do not yet know​

exec is great when the answer is binary (test passes / doesn't). But what about the prose the agent produces — its explanation of the bug, its rationale for a design choice, the quality of its commit message? You cannot grade that with grep. Tutorial 4 introduces llm-rubric, where a judge model grades prose against a rubric you write.


Previous: Tutorial 2 — your first live LLM Bench Next: Tutorial 4 — semantic judging with llm-rubric