AI-generated code often passes every test and still breaks the first time a retry, a double-click, or a parallel worker hits it. The cause is temporal coupling: the code secretly assumes operations arrive in exactly one order, and nobody wrote that assumption down anywhere a reviewer or test could catch it.
The Details:
When you ask an AI to write code for a scenario, it writes code that handles that one scenario correctly. Initialize, fetch, process, save, in that exact sequence, becomes an unwritten contract. Nothing prompts the model to ask what happens if step two fires twice, or if step three starts before step one finishes.
Tests rarely expose this because tests are written in the same narrative order as the code. A test that checks token refresh runs setup, then refresh, then verification, confirming the one sequence nobody doubted. The tests that would catch the real bug run steps twice, interrupt mid-operation, or fire the same event three times fast, which feels unnatural to write on purpose.
A classic failure: a webhook handler marks an order paid and emails a receipt. A payment provider retries the same webhook three times during a network blip. Nothing crashes, no component has a bug, but the customer gets three emails and a downstream loyalty service credits points three times. The hidden assumption was that the webhook fires exactly once.
The fix is prompting for the operating environment, not just the feature. Telling the model to assume at-least-once delivery, concurrent callers, or retries on failure pushes it toward idempotency keys, conditional writes, and state checks that close the gap between checking and acting. A short instruction like "assume this runs twice" changes the design before a single line of code exists.
Bottom Line: Passing tests only prove one sequence works; safety comes from naming every order the system might actually face and building the code, or the prompt, to survive all of them.
Enjoy this article?
Listen to the Claude Code Conversations radio show or join the community.