All Concept Bites
26 views
Software Engineeringintermediate

Why AI-Repaired Code Passes Tests and Is Still Wrong

A green test suite on familiar inputs proves the code runs, not that it's right.

Reviewed by Abhijith N M · 24 October 2026 · Editorial standards

The Simple VersionNo jargon

Here is a hypothetical, separate from the study below. A library function changes. The old call `read()` now needs a required `size` argument. An AI assistant rewrites your function. You run the tests. Everything passes. You ship it. Weeks later, a user reports wrong numbers. The repaired function still calls the old API somewhere. The old call still works in the common case. It returns a value that looks right. Your tests only checked that case, so nothing failed. A preregistered study examined this pattern. Preregistered means the analysis plan was fixed before data collection. In the study, repairs were picked from documented code fragments, not written free-form. The setup used 73 tasks built from 60 documented API changes in CPython, Node.js and SQLite. A gate that ran every assertion on typical inputs still released about 41% of wrong repairs. These numbers come from a constrained selection task. Treat them as a warning, not a measurement of every AI rewrite. The fix is small, and it is not a guarantee. Write one assertion that exercises the changed contract on an edge input. In the study's main stage, one edge input written from the release note cut the fraction of wrong repairs released to about 6.5%. It reached about 6.1% on 15 unseen clusters. A blind low-cost commercial writer (GPT-5.6-luna) using three inputs reached about 7.0%. Two later frozen stages enlarged the fragment menu to 1491. They also added 233 hard distractors that match legacy output on typical inputs. There, the edge input and the writer each released 5.8% of wrong repairs. This matters because API migrations are routine. A repair that runs cleanly is not evidence of correctness. Your test has to check the new behavior, not just confirm the code still executes. The habit the study points to is still sound. After an API change, add an assertion that would fail if the repair were wrong.

Why AI-Repaired Code Passes Tests and Is Still Wrong — Visual Breakdown

Click any step below to inspect
API change lands
Repair chosen from fragments
Run tests on typical inputs
Green suite ships wrong output
Add edge-input assertion
Wrong repair caught
Sequence of Events
Why It Matters in Production
API migrations are routine, and AI assistants make the rewrite cheap. That speed hides a risk. A repaired function can run without errors and still return the wrong value. In a controlled study, repairs were chosen from documented code fragments. An all-assertions gate on typical inputs released about 41% of wrong repairs. One edge input written from the release note cut that to about 6.5% in the main stage. It held at 5.8% in later frozen stages with a larger fragment menu and hard distractors. The practical habit is narrow. After any API change, write at least one assertion that exercises the changed contract on an edge input. Do not trust a green suite that only covers the happy path the repair already matched. Reviewers should ask which assertion would fail if the repair were wrong.
Implementation & Syntax
🐍Python
import sqlite3

# DB-API rule: params must be a sequence of values.
# The correct repair passes a one-element tuple.
def get_user(conn, user_id):
    return conn.execute(
        "SELECT name FROM users WHERE id = ?", (user_id,)
    ).fetchone()

# A wrong repair builds the SQL with string formatting.
# It looks fine for plain integer ids and breaks on other input.
def get_user_wrong(conn, user_id):
    return conn.execute(
        f"SELECT name FROM users WHERE id = {user_id}"
    ).fetchone()

conn = sqlite3.connect(":memory:")
conn.execute("CREATE TABLE users (id INTEGER, name TEXT)")
conn.execute("INSERT INTO users VALUES (1, 'ada')")
conn.execute("INSERT INTO users VALUES (10, 'grace')")

# Happy path: both versions pass on a typical single-digit id.
assert get_user(conn, 1) == ('ada',)
assert get_user_wrong(conn, 1) == ('ada',)

# Edge input: a value that changes the SQL semantics.
# The bound parameter is the literal string "10 OR 1=1".
# It matches no integer id, so the correct repair returns None.
assert get_user(conn, "10 OR 1=1") is None

# The formatted query becomes "WHERE id = 10 OR 1=1", which matches
# the first row, so the wrong repair returns ('ada',) instead of None.
assert get_user_wrong(conn, "10 OR 1=1") == ('ada',)  # wrong repair exposed

Quick Concept Check

Test your understanding in 15 seconds

Your AI assistant repairs a function after an API change. All existing tests pass. What is the strongest next step?

Bite Progression32 / 36

Related Concepts

Complementary topics

Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.