DEV Community

Cover image for Continuous Integration for AI-Generated Code: Gating Pull Requests on What the Code Actually Does
Marx Jenes
Marx Jenes

Posted on

Continuous Integration for AI-Generated Code: Gating Pull Requests on What the Code Actually Does

Pull requests look different than they did a couple of years ago. An AI assistant can produce a new endpoint, a refactor across thirty files and a matching set of tests in the time it used to take to write the first function. The pipeline runs, everything passes, and the change is ready to merge before anyone has fully read it.

That speed is useful, but it shifts where mistakes get caught. When a person writes a few hundred lines a day, code review does a lot of the checking. When an assistant writes thousands, review can't keep up, and the pipeline becomes the main line of defense. Continuous integration was always meant to catch problems early. For AI-generated code, it has to catch a specific kind of problem: changes that look correct, pass their own tests and still behave differently from what clients expect.

Why AI-written tests aren't enough on their own

When an assistant writes both the code and its tests, both come from the same understanding of the task. If that understanding is wrong, the tests are wrong in the same way, and they pass. A misread business rule, a renamed response field or a changed error format will sail through a test suite that was written to match the new code.

That doesn't make generated tests useless. They're a fast way to cover obvious cases. But they can't serve as an independent check on the change that produced them. A pull request gate needs at least some tests whose expectations come from somewhere else.

What the pipeline should actually check

For most backend changes, the most reliable independent check is the API's behavior from the outside. Clients don't care how the code is structured. They care that the same request still returns the same status code, the same fields and the same error format, unless the change was meant to alter them.

Tests built at that boundary stay meaningful no matter who wrote the code. Their expected results come from requirements, API contracts or recorded real behavior, not from the assistant that wrote the change. When an AI-generated refactor quietly changes a response, those tests are the ones that notice.

This is where the broader practice of continuous integration earns its keep. Running behavior checks on every pull request means a problem shows up minutes after the change is proposed, while it's still small and easy to trace, rather than weeks later in production.

Building a baseline the assistant didn't write

The baseline is the record of how the API is supposed to behave, and its value depends on its source. Hand-written expectations work for critical rules but take time to build. API specifications help where they're accurate and current. Recorded traffic is often the most practical source, because it captures what clients actually send and receive, including the unusual cases nobody would think to write down.

Whatever the source, the baseline should live in the repository and change only on purpose. When a pull request intentionally changes behavior, the updated expectations should appear in the same pull request, so reviewers can see exactly what's meant to be different.

Making the check a real gate

A check that can be ignored isn't a gate. On GitHub, branch protection rules can mark specific status checks as required, so a pull request can't merge until they pass. GitLab offers a similar setting that requires the pipeline to succeed before a merge request can be merged. Either way, the behavior tests should be required, not optional.

It's also worth deciding who can override a failed check. Allowing anyone to bypass required checks quietly defeats the purpose, especially when large AI-generated changes are coming in quickly and the temptation to merge anyway is strong.

Failure reports reviewers can actually use

When an AI-generated pull request touches dozens of files, a red build that only says "tests failed" doesn't help much. Reviewers need to know what behavior changed. The most useful failure reports show the request, the expected response and the actual response side by side, ideally posted directly on the pull request.

That changes how review works. Instead of reading every line the assistant wrote, reviewers look at the handful of behavior differences the pipeline found and decide whether each one is intended. It's a much better use of their time.

Keeping the gate fast and trustworthy

A gate that's slow or flaky gets bypassed. Two things matter most. Tests shouldn't depend on live databases or third-party services, since those cause failures unrelated to the code. Recorded or mocked dependencies keep results consistent from one run to the next. And values that change on every request, like timestamps and generated IDs, should be excluded from comparisons field by field, so they don't trigger false failures.

Where AI testing tools fit

AI testing tools can help build the baseline itself, as long as their output is checked against the real system rather than trusted blindly. Keploy is one example built for this kind of pipeline. It generates API tests from real traffic or from specs like OpenAPI, validates them against the running API, and runs them in CI on every pull request as a required check, posting a diff of any changed responses on the pull request. Since the tests reflect how the API actually behaves, they stay independent of whatever assistant wrote the change being tested.

People still make the final call

None of this removes the need for human judgment. A pipeline can show that a response changed. It can't always tell whether the change is right. That's still a decision for someone who understands the product and its clients. What a good gate does is narrow that decision down to the few changes that matter, so reviewers spend their attention where it counts.

Conclusion

AI-generated code moves faster than review can, which puts more weight on the pipeline than ever before. Continuous integration that checks behavior at the API boundary, against a baseline the assistant didn't write, catches the mistakes that self-written tests miss. Making those checks required and their failures readable turns the pipeline into a real gate rather than a formality.

A practical first step is to pick the API endpoints your clients depend on most, build a behavior baseline for them, and make that check required on every pull request. Once it's in place, the next large AI-generated change will have to prove it still does what the old code did before it can merge.

Top comments (0)