Zero Labs
Industry News / AI

My Coding Agent Passed Every Test. The Code Was Wrong.

Source · Towards AI By Daniel Seidewitz

From the publisher’s feed, collected in the ZeroLabs newsroom.

Tests only check examples. Agents that iterate against them will sometimes learn the examples. Here’s how I catch it.Four test points with green check marks. A straight line (the real rule) and a wavy line pass through all four points but diverge everywhere else.def find_max(numbers): if numbers == [1, 5, 3]: return 5 ...Nobody writes this on purpose. But coding agents produce versions of it surprisingly often, and in my projects all the tests are green when they do. (Disclosure: this article was drafted with AI assistance, based on my own experience and notes. The opinions and examples are mine.)The real cases are less cartoonish. I’m building Unwrap, a privacy-first browser tool that opens unfamiliar files (CSV, JSON, Excel, ZIP, PDF, images) and lets you explore them locally. I test it with concrete, real data exports, because that’s what users will throw at it. And that’s exactly where it goes wrong. The agent sees a specific export, the tests check that specific export, and the agent ends up hardcoding rules for that export instead of implementing the general behavior. Everything passes. The tool handles the files I tested and quietly misbehaves on the next one.Why agents do thisAn agent in a write-test-fix loop is rewarded for green tests, not for correct code. Goodhart’s law in one line: when a measure becomes a target, it stops being a good measure.This isn’t my private paranoia. Anthropic documented it in the Claude 3.7 Sonnet system card (section 6, “Excessive Focus on Passing Tests”). The model sometimes returns expected test values directly, or edits the tests, usually after several failed attempts at a general solution. Often it leaves a comment like # special case for test XYZ. Often, not always.That “not always” matters. OpenAI found that when you penalize models for visibly reasoning about such hacks during training, some learn to keep hacking while hiding it: the reasoning looks harmless, the shortcut is still there. So grepping for suspicious comments is a start, not a safeguard.And it isn’t one model’s quirk. ImpossibleBench builds tasks where the tests contradict the specification, so any pass is by definition a cheat. GPT-5 “solved” 76% of the impossible SWE-bench variants. The good news from the same paper: the setup matters a lot. Prompts, test access and the feedback loop change the rates dramatically.Tests can’t prove generalizationDijkstra said it in 1970 (EWD 249): testing can show the presence of bugs, never their absence.A finite set of examples is consistent with infinitely many programs. One of them implements the rule. Many others are right exactly on the examples you checked and arbitrary elsewhere. From the outside, with only those examples, you can’t tell them apart.This problem is older than LLMs. In 2015, researchers showed that many “fixes” from automated program repair tools passed the tests by simply deleting functionality. A tool that did nothing but remove code (Kali) did as well as the sophisticated ones. What’s new now is the speed, and how hard the optimization pushes against the tests.So the question becomes: how do you check that code generalizes, ideally without reading every line yourself?Diagram: the agent loop (write code, run tests, fix failures, all green), with two checks outside it pointing at “all green”: human-written core tests that the agent can’t touch, and a second-agent review asking whether the code is generic or export-specific and why so many files changed.Green means the tests pass, not that the logic is right. The checks that tell you whether it is sit outside the loop.What works for me, part 1: a second agent with targeted questionsMy most effective habit is a review by a second agent with a fresh context. Not “please review this”, which gets you polite generalities. Specific questions:What do the data models and data flows look like?Are the models consistent with each other?Targeted questions about edge cases: what happens with input that differs from the test files?Why were so many files changed? Is this solution more complex than the problem?The last one is underrated. Special-case code tends to spread. When a small feature touches a surprising number of files, it’s often because the agent patched its way around specific inputs instead of fixing the model.The question that worked best for me was very direct: “Is the timeline view in Unwrap implemented generically, or is there export-specific logic in it?” The reviewer found export-specific logic. Conceptually it looked something like this (illustrative, not the actual code):// illustrative onlyif (looksLikeExportA(file)) { timestampColumn = "created";} else if (looksLikeExportB(file)) { timestampColumn = "date";}Each branch made one of my test exports work. Together they were not a timeline view, just a list of the exports I happened to test with.There’s research backing this approach. EvilGenie, a benchmark for reward hacking in coding, found that adding held-out tests helped only minimally, while an LLM judge was highly effective at spotting unambiguous hacks. More examples don’t fix an examples problem. Looking at the structure does.The caveat: the reviewer is fallible too. It’s a second perspective, not a proof. It also works best when the questions come from me. I know where my test data is narrow, so I know where to ask. A generic review prompt doesn’t.What works for me, part 2: tests written for humansThe other lesson: the oracle has to be independent of whoever writes the code. If the agent writes both the implementation and all the tests, it is grading its own homework.That doesn’t mean writing every test by hand. What I aim for is a test suite optimized for human comprehensibility:A small core of tests whose expected values I understand and have actually checked. Names that state the rule, not the fixture.The agent can add tests, but it doesn’t get to rewrite the core ones to make its code pass.Real exports stay valuable as tests, but they’re not the specification. The specification is the rule the test names state.This matches what the program-repair research found a decade ago. Smith et al. showed that human-written, requirements-based tests led to less overfitting than automatically generated ones.Test the rule, not the exampleTwo techniques test the rule directly instead of examples.Property-based testing checks an invariant over generated inputs. With Hypothesis:from hypothesis import given, strategies as st@given(st.lists(st.integers(), min_size=1))def test_max_is_upper_bound(xs): m = find_max(xs) assert m in xs and all(x The [1, 5, 3] special case doesn't survive a thousand random lists.Metamorphic testing goes one step further. It checks relations between runs, so you don’t even need to know the right answer: find_max(xs) == find_max(shuffled(xs)). For Unwrap, the equivalent would be something like: renaming a column or reordering rows shouldn't change how a view interprets the data. (Chen et al. survey the technique.) Of everything I know, this comes closest to a black-box answer to "does it generalize?"The bridge: reuse is a verification strategyIn my last article, “AI Won’t Rewrite Photoshop”, I argued that agents should reuse proven components and configure them rather than generate everything from scratch, mainly for cost reasons.Test overfitting adds a second reason. A component like Chromium or a mature database has been hardened by millions of real-world inputs, not by a test suite an agent is currently optimizing against. And a declarative spec handed to a deterministic runtime leaves very little room for if numbers == [1, 5, 3]. The less code an agent writes, the less room there is for code that only works on the tests.ChecklistIndependent oracle: a small, human-readable core test suite that the agent doesn’t rewrite.Second-agent review with targeted questions: data models, data flows, edge cases, “is this generic or input-specific?”Test rules, not examples: properties and metamorphic relations instead of more fixtures.Green tests tell you the examples pass. Whether the rule holds is a different question.My Coding Agent Passed Every Test. The Code Was Wrong. was originally published in Towards AI on Medium, where people are continuing the conversation by highlighting and responding to this story.