过去一年,AI 辅助编码已经从 "尝鲜" 变成我们团队的默认工作方式:需求拆给模型,实现和单元测试几分钟就能出来。但在这段时间的落地实践里,我们发现一个被严重低估的事实 —— 当 AI 负责生成之后,交付的主要矛盾已经从代码生产转移到了代码验证。
一个典型的坑:让 AI 同时写实现和对应的单元测试,如果两者来自同一个错误的理解,测试会一致地通过,业务也会一致地偏离。流水线全绿,上线后逻辑却是歪的。
我们复盘过一批 AI 辅助代码(约 31 万行),生成速度确实提升,但分层模糊、实践风格不一、依赖耦合加深等问题同步放大;行业数据也显示,相当一部分 AI 生成代码即使通过了 QA 和预发,上线后仍需人工调试,不少团队要经历两到三轮重新部署才能确认修复生效。
这篇文章把我们踩坑之后沉淀下来的三层验收体系分享出来:架构门禁、独立契约 + 变异测试、运行时验证。目标只有一句话 —— 让 AI 的生成结果可验证、可阻断、可回滚。
第一层:用架构契约和分层门禁,约束 AI 的随意发挥
AI 有一类最典型的问题:它总能 "把事办成",但手段往往越界 ——Controller 直连数据库、依赖穿透模块边界、敏感路径被替换成非标准实现。这类问题单看代码 review 很容易漏,因为每一处都 "能跑"。
我们的做法是把架构契约固化为机器可读的硬约束:
- 分层规则、依赖白名单、禁止模式(如禁止跨层直查)、关键接口的 DTO 签名,全部写入仓库规则和门禁脚本;
- CI 流水线执行格式、编译、架构边界(如 ArchUnit)、API 兼容性等确定性检查,任意一项失败直接阻断合入。
这一层不碰业务逻辑,消灭的是 AI 代码中最常见、最隐蔽的结构性风险。它回答的问题是:这份代码有没有越出系统该有的形状。
第二层:切断 "同源共错",让验收依据独立于实现
这是整个体系里最关键的一条纪律。如果测试也是 AI 按自己的实现生成的,它只是在陪实现演戏:边界条件改错了,测试可能照样通过 —— 因为测试和实现共享了同一个错误前提。
解决办法是规格、验证、实现三者分离:
- 冻结只读契约 动手之前,先把业务契约写下来 —— 关键不变量、接口语义、边界条件,以及至少一个反例 —— 放在 AI 无权修改的目录或仓库区域,作为唯一的正确性依据。契约本身可以由人写、也可以让 AI 起草后由人审定,但一经冻结就不再跟随实现变动。
- 用变异测试做独立裁判 向实现注入小故障:改比较符、删空值校验、放宽退款上限。如果 AI 写的测试杀不死这些变异体,说明测试根本没有覆盖到契约,不可信。PIT(Pitest)可以直接接入 Java 流水线,把 "变异分数" 变成卡合并的硬指标。
这一层切掉的是 "测试通过 = 业务正确" 的质量幻觉。测试由 AI 生成并不可怕,可怕的是验收依据和实现来自同一个 "主体"—— 哪怕这个主体是个模型。
第三层:用运行时验证,装上生产环境的行为保险丝
静态门禁拦得住结构越界,变异测试拦得住测试陪跑,但它们都拦不住那些只有真实流量才暴露的问题:空指针、边界状态漂移、超时与重试风暴。
所以验收必须延伸到运行时可观测性:
- 把 AI 生成的关键路径纳入指标、日志和分布式追踪;
- 在灰度阶段设置行为断言 —— 不是返回 200 就算通过,而是业务副作用、幂等条件、状态迁移必须在真实流量下被观测到符合契约,达标后再逐步放量。
这一层的信任观只有一句话:不信任 "看起来对",只信任 "行为证据"。灰度阶段观察到的行为证据,比合并前的绿灯更值得信任。
AI 写代码的速度会越来越快,但验证的责任永远不会自动交给模型。我们这套三层验收体系,本质不是为了限制 AI,而是给高速的代码生产装上刹车与标尺。不要被流水线的绿灯、漂亮的单元测试报告迷惑,同源共错的陷阱藏在每一次 AI 生成里。把架构边界守住、把验证依据独立、把信任交给真实运行时的行为证据,才能安稳接住 AI 编码带来的效率红利。AI 可以帮我们产出代码,但判断代码是否正确、是否符合系统长期设计的决策权,依然要握在人手里。
本文由 AI图鉴自动抓取并翻译,版权归原作者所有;如需查看原文请点下方链接。
Over the past year, AI's auxiliary coding has moved from a taste to a default way of working for our team: demand to remove to the model, realization and unit testing can come out in a few minutes. But in this time of landing, we found a fact that was seriously underestimated -- when AI was responsible for the generation, the main contradiction of delivery had shifted from code production to code validation.
A typical pit: allow AI to write both the achievement and the corresponding unit tests, and if both are derived from the same erroneous understanding, the tests will be passed consistently and the business will deviate consistently. The current line is green, but the logic behind it is crooked.
We have reset a series of AI support codes (approximately 310,000 lines) and the speed of generation has indeed increased, but the layers are blurred, the style of practice varies, the reliance on convergence deepens, etc.; industry data also show that a significant proportion of AI generation codes, even if passed QA and pre-issuance, need to be manually debugged when they go online, and many teams have to undergo two to three rounds of redeployment to confirm recovery.
This article shared the three layers of acceptance check that we sunk after stepping on the pit: the architecture door, the independent contract + the mutation test, and the operating verification. The target has only one sentence - to make AI's generation verifiable, blockable, rolling back.
First level: use of architecture contracts and layers to limit the free play of AI
AI has one of the most typical kinds of problems: it can always "get things done," but the means are often crossed across borders — the Controller direct database, reliance on penetrating modular boundaries, and the replacement of sensitive paths with non-standards. This kind of questionnaire looks at the code and it's easy to slip because every place is "run."
Our approach is to anchor the architecture contract into machine-readable hard bounds:
- tiered rules, reliance on white lists, prohibited modes (e.g., prohibition of direct cross-layer checks), signatures of DTOs for key interfaces, all written in warehouse rules and closed scripts;
- Certain checks such as the implementation format, compilation, structural boundaries (e.g. ArchUnit), API compatibility, etc. of the flow line by CI, and any failure directly hinders integration.
This layer, free of business logic, eliminates the most common and hidden structural risks in the AI code. It answers the question: does the code cross the shape that the system should have.
Level two: Cut off the "one-source fault" and let acceptance check be independent.
This is the most critical discipline in the system. If the test is also AI based on its own achievement, it's just accompanying the performance: the boundary conditions are wrong and the test may pass -- because the test and the error is shared.
The solution is specification, validation, separation:
- Freezing read-only contracts Before moving on, write down the business contract - Key non-variant, interface semantics, border conditions, and at least one counter-exact - in the AI directory or warehouse area that is not entitled to modification as the only basis for correctness. The contract itself can be written or written by AI, but once frozen, it will not follow the change.
- An independent decision based on a mutation test. If the tests written by AI do not kill these variants, this means that they do not cover the contract at all and are not credible. PIT (Pitest) has direct access to the Java stream line, turning the "variant fractions" into a hard indicator for card consolidation.
This layer cuts off the "test through = business correct" quality illusion. It's not terrible that the tests were created by AI, and it's terrible that acceptance check was based on and realized from the same "subject" -- even if the subject was a model.
Level III: Operation-validation, installation of behavioural fuses in the production environment
Static doors can stop structures across borders and mutation tests can stop testing to accompany them, but they can't stop problems that are exposed only to real flows: empty pointers, border movement, timeout and retry storms.
So acceptance check has to extend to operational observability:
- Integration of key AI-generated pathways into indicators, logs and distributed tracking;
- Set up behavioral assertions during the greyscale phase - not return 200, even if passed, but business side effects, thorium, etc., and state migration must be observed in real flow to conform to the contract and then progressively release after meeting the standards.
There's only one word of confidence on this level: no trust in "looks right," only in "act evidence." The evidence of behaviour observed during the greyscale phase is more trustworthy than the green light before the merger.
AI will write codes faster, but the burden of proof will never automatically be assigned to the model. Our three-storey acceptance check system is not meant to limit AI, but to place brakes and rulers on high-speed code production. Don't be confused by the green light of the current line, the beautiful unit test report, and the co-involved trap is hidden in every AI generation. The efficiency dividend of the AI code can only be securely captured by securing the boundaries of the structure, providing an independent basis for certification and handing over trust to evidence of behaviour when it is actually operating. AI can help us produce the code, but the decision-making power to judge whether the code is correct or consistent with the long-term design of the system remains in the hands of people.
Auto-fetched and translated by AI Atlas. All rights belong to the original author.