Three remedies, none of them free. Draw tests from the distribution you care about (Theorem 7.3(c) then applies). Add boundary and extreme cases deliberately, as property-based testing tools do (Claessen and Hughes, 2000). Measure the suite itself with mutation testing (DeMillo, Lipton and Sayward, 1978): in this pool the mutation score at 40 small examples is 0.498. That number is a measurement of the suite, and the program under test never enters it.
Tests that do not capture intent. The central failure. the companion book example gives a concrete case: a program for the task “return the maximum of over the entries, or 6 if that is larger or the list is empty” that adds 1 to the result when the list has at least six entries and equal first and last entries. It passes the first eight small examples, and all 40, and it fails 73 of 4,000 hidden inputs (error mass 0.0182); the first counterexample is [8, -9, 9, 7, 3, 2, 8]. Random differential testing against a reference finds such a program only after about tries: 54.8 expected for that one, and the measured mean was 54.8. For the program with the guard len(xs) >= 8 it was 9.2 against 9.0 expected, and for the guard on both extremes 25.7 against 26.5.
Three remedies, none of them free. Draw tests from the distribution you care about (Theorem 7.3(c) then applies). Add boundary and extreme cases deliberately, as property-based testing tools do (Claessen and Hughes, 2000). Measure the suite itself with mutation testing (DeMillo, Lipton and Sayward, 1978): in this pool the mutation score at 40 small examples is 0.498. That number is a measurement of the suite, and the program under test never enters it.
Flaky tests. A test that sometimes fails for reasons unrelated to the program (timing, ordering, shared state) is common in real projects (Luo, Hariri, Eloussi and Marinov, 2014). If a passing program fails each test run independently with probability and the suite has tests, the program is accepted with probability , and with up to reruns on failure with probability . In the listing (eight random tests) that is 0.8508 for with no rerun and 0.4305 for , and the measured acceptance matches (0.8478 and 0.4294). Spurious failures do not change the precision of the suite (the share of wrong returns stays between 0.3815 and 0.3899 in every row) but they raise the cost: expected samples to acceptance rise from 2.075 to 4.820 at (measured 4.854), and three reruns bring them to 2.545 (measured 2.531) at a cost of repeated test runs. The model here is spurious failure only. Flakiness in the other direction, a test that sometimes passes a wrong program, lowers the precision and is worse; Exercise 7.11 asks you to quantify it. In a learning loop, flaky rewards are noise on the reward, which the group baseline averages away only partly.
Benchmark contamination. If benchmark problems or their solutions are in a model’s training data, memorization may inflate pass@k. The score alone cannot separate memorization from generalization. The experiment in the companion book example is a hypothetical: 12 of 36 tasks are “leaked” and the odds of a correct program on them are multiplied by 8 (an assumed effect, not a measurement). True pass@1 on the leaked tasks goes from 0.3033 to 0.5718 while unleaked tasks stay at 0.4684. An unpaired comparison of the measured values (leaked 0.5833, unleaked 0.4521) gives a difference of 0.1313 plus or minus 0.2225, which does not exclude zero, because the two groups differ in difficulty. A paired comparison against fresh variants of the same 12 tasks (assumed to have the same difficulty and never to have been seen) gives 0.3000 plus or minus 0.1052. The comparison that isolates contamination is against a matched unseen version. An overlap detector (6-gram overlap of task text and reference with the training corpus) separates leaked from unleaked tasks with an AUROC of 0.958, but the unleaked tasks have a mean overlap of 0.724 because they share templates with the leaked ones (0.644 to 0.805 by template), and two of the 24 unleaked tasks score at least 0.99. A detector for templated benchmarks needs a null calibrated on unleaked items, as in the companion book.
Insecure code that passes. A functional suite says nothing about security properties. the companion book example gives four implementations of “sum the integers in a string like 12+5+7”. Two parse the string; two call eval on it. In an environment that allows eval, all four pass all 30 generated functional tests (30/30). A probe that a safe parser must reject, '2*3', is rejected by the two parsers and evaluated by the two eval versions (it returns 6). A static scan with a denylist flags the plain eval (name:eval) and misses the version that builds the name from two string pieces, which finds nothing. The restricted sandbox of the companion book example, which has no eval, fails both eval versions (0/30), which is the right outcome here and a surprise elsewhere: a program that works in a developer’s environment and fails in the test environment, or the reverse, has an environment-parity problem, and neither direction is evidence about security.
Generated code is a known source of vulnerabilities, and users of code assistants have been found to write less secure code than users without them, with more confidence in it (Perry, Srivastava, Kumar and Boneh, 2023). Unit tests rarely exercise the property that makes code unsafe, and static scanners rarely understand intent.
Over-trusting generated patches. A patch that passes the visible tests is plausible, not correct. The automated-program-repair literature documents that a large share of plausible patches overfit the tests (Qi, Long, Achour and Rinard, 2015; Smith, Barr, Le Goues and Brun, 2015). Here the wrong-return rate is 0.4722 with the weak suite and 0.3717 with the stronger one, even though every returned program passed its tests. Treat acceptance as a trigger for review and for stronger checks, never as the conclusion.
Three further traps, shorter. Hiding the hidden suite. The numbers in this article use a 4,000-input hidden suite to label programs correct; in practice the analogue is held-out tests, and once they are used to tune a prompt or an agent they are no longer held out. Counting only successes. Reporting the accepted-correct rate without the wrong-return rate rewards loops that always answer. Cost blindness. An agent that is 2 points better at three times the cost is not a better agent without a statement of what a point is worth (the companion book example).
Continue with the book
Explore the full treatment, worked examples, and exercises in Applied AI for Generation and Control.
Companion article for Applied AI for Generation and Control, published by Choral Software. Article text and original examples © 2026 Choral Software. All rights reserved.
