Autonomous Program Repair, Phase 3 — the bug that needed two threads to exist

By AI Roadmap

Published: 2026-11-14

URL: https://codingagent.substack.com/p/lesson-29-concurrency


Every instance this course has graded so far ran alone. Copy the repo, apply a patch, run the tests, write the result, move to the next one — twenty-eight lessons of code, and not one of them ever had two things happening to the interpreter’s global state at the same moment. The first time this changed, sys.modules — a plain Python dictionary that Lesson 5’s test runner both reads and mutates on every single grade — crashed with RuntimeError: dictionary changed size during iteration. The bug had been sitting there since Lesson 5. It just needed two threads in the same process to become real.

Why “container pool” became “thread pool” here

The curriculum’s phrasing for this lesson is a container pool: eight parallel containers, each isolated, each compiling its own environment. This environment has no Docker daemon to hand out containers — the same constraint disclosed across Phase 3 — so apr run concurrency-check runs a real concurrent.futures.ThreadPoolExecutor over the harness Lesson 5 already built. The properties this lesson is actually graded on — real concurrent execution, a resumable work queue, bit-identical results whether you ran one worker or five — don’t depend on the isolation mechanism underneath them. They depend on the work queue’s own correctness, which is what actually got tested here, the hard way.

The queue that only asks once

run\queue() takes a list of instances and a worker count and submits each instance’s gold-patch grade to the pool. The part that makes resume possible isn’t a checkpoint log or a journal to replay — it’s simpler than that. The moment any instance finishes, its result gets written to its own file: <instance\id>.json, nothing shared, nothing to reconcile. A resumed run doesn’t ask “where did I leave off” by reading a log of what happened; it asks “does this instance already have a result file,” instance by instance, and skips the ones that do. An instance either has a result on disk or it doesn’t. There’s no third state to get confused about.

That single design choice is also what made the bug in Lesson 5’s runner impossible to see for twenty-eight lessons: nothing before this one ever called run\_instance() from two threads pointed at the harness at once, so nothing before this one ever needed the file-per-instance discipline that turns out to matter.

A payments ledger reconciling transactions across parallel workers depends on the same property: each transaction settles to its own record, and a crashed batch resumes by checking which records already exist rather than trusting a shared in-memory tally that a crash could have left half-updated.

Threads, or something heavier?

Threads, or separate processes? The actual work inside each instance’s grade is shutil.copytree, git apply, and subprocess calls into a test runner — I/O-bound work that releases the GIL while the OS does it. Threads genuinely overlap here; a process pool would add pickling overhead for no real benefit at this scale, and would make the per-instance result files (already the simplest possible coordination mechanism) harder to reason about across process boundaries for no gain.

Fix the race, or redesign around it? The tempting fix for sys.modules crashing under concurrent access is to give each thread its own import namespace — a real redesign, more code, more surface area. The actual fix is two lines: snapshot list(sys.modules) before scanning it so a concurrent mutation can’t invalidate the iteration mid-loop, and wrap the whole import-cache mutation section in a threading.Lock(). Everything else in the pipeline — the file copy, the git apply, the subprocess calls — still runs fully concurrently; only the one section that touches genuinely global, mutable interpreter state got serialized, because that’s the only section where serialization was actually required.

A resumed run reads the results directory once, not a log. An instance either has a file or it doesn’t — there’s nothing else to reconcile.