跪拜 Guibai
← All articles
Programmers · Artificial Intelligence · AI Programming

Five Checks Before Merging AI-Generated Code: From Semantic Drift to Operational Readiness

By 挖掘狂人 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

AI's speed makes plausible but wrong code easy to merge, and the acceptance workflow determines whether it survives production. This concrete checklist catches semantic drift, tautological tests, and operational gaps before they turn into late-night incidents.

Summary

AI coding assistants now generate compilable, test-passing code in minutes, but the acceptance review decides whether it works in production. A common failure mode is semantic drift: the model understands a requirement word slightly differently and writes confident code that runs, yet does the wrong thing. The fix is to start not with the implementation but with the business rules, checking interfaces and parameters line by line before touching the diff.

The second step is reading the diff before running any commands, using `git diff --stat` and `--check` to scope changes and catch whitespace or conflict markers, and scrutinizing conditionals, new imports, deleted code, and test files. Boundary cases and security are where AI fudges: null/undefined handling, SQL injection, hardcoded secrets, and N+1 queries often pass local tests but fail under production load. AI-written tests are particularly misleading because they assert what the code does, not what the requirement demands; designers must devise independent acceptance cases, including reverse checks that tamper with a return field to expose tautological tests.

Finally, maintainability and operational readiness matter: naming, single-responsibility methods, logging, monitoring, rollback paths, and data consistency. The author keeps a running ledger of acceptance mistakes, feeding them back into prompts to reduce recurrence; after six months, 27 logged patterns have cut repeated failures noticeably.

Takeaways
— AI's largest failure mode is semantic drift: it writes confident code that runs but interprets a requirement word differently than intended.
— The first review pass should match interface signatures, parameters, and return values against business rules, not admire the implementation.
— Read the diff before running tests; `git diff --stat` shows scope and `git diff --check` catches trailing whitespace and conflict markers.
— Four diff areas deserve closest inspection: conditionals, new imports, deleted code, and test files.
— Using `--word-diff` on dense changes gives clearer output than line-by-line comparison.
— Null/undefined handling, array bounds, division by zero, timeouts, concurrency, and third-party API failures are boundary cases AI avoids addressing.
— Security weaknesses like SQL string concatenation, hardcoded secrets, and logging tokens pass functional checks but fail in production.
— AI-generated tests are reverse-engineered from the implementation, so green tests verify that code works as written, not as required.
— Independent acceptance cases for normal, boundary, empty, and invalid values, plus reverse checks that tamper with a return field to expose fake tests, keep tests honest.
— N+1 queries, full-table loads, lock granularity, and thread pool settings don't show up in local testing; evaluate against production data volume.
— Before launch, confirm logging, monitoring, alerts, rollback speed, and data consistency after rollback.
— Keeping a ledger of acceptance failures and feeding them into prompt descriptions reduces recurrence; the author had 27 entries after six months.
— AI picks odd names like `processData` or `handleStuff` and tends to over-engineer with unnecessary abstraction layers, complicating long-term maintenance.
Conclusions

The gap between AI's confidence and its handling of null/undefined suggests that polished, self-consistent code is exactly where semantic drift hides, so review effort should concentrate on the most fluent-looking sections.

AI-generated tests that assert the implementation's behavior make a green test run indistinguishable from bug certification; using a tampered return field to see if tests fail is a cheap way to expose this without rewriting the suite.

The article's claim that prompt quality determines the starting point while acceptance quality determines the finish line implies that review discipline compounds better than prompt tweaking alone.

The author's ledger of 27 distinct failure patterns over six months shows that AI coding assistants produce a long tail of subtle, non-syntax errors that no single fix or style rule eliminates.

Concepts & terms
Semantic drift
When an AI model interprets a requirement's wording differently from the human, producing code that runs and passes type checks but behaves incorrectly relative to the business rule.
Reverse validation
A test technique where you deliberately alter a return field or expected output to see if the test fails; if the test stays green, it's asserting existing implementation behavior rather than required behavior.
From the discussion
Featured comments
shushenghong

That line "tests are written by AI, code is also modified by AI, neither counts as original evidence; the diff is what matters" is spot on. Before we look at the diff, we take one extra step: each AI task goes on its own branch, and on delivery we attach a note (what was changed, how it was verified, where we're uncertain). First we check against the task description whether it touched any files outside the task. The retry logic you mentioned being silently deleted would most likely surface at this step. In practice, the "where we're uncertain" field in that note is usually more worth reading than a row of all-green tests.

See top comments, translated →
Source: juejin.cn ↗ Google Translate ↗ Backup ↗