The morning after the test run: Letting an AI agent investigate failed tests overnight

Failed tests can hold up your release, but handing them to an AI agent feels risky. We found a middle ground. Our AI agent investigates failures overnight, and nothing merges without human approval.

A quick peek 

Nobody joins QA to spend mornings triaging overnight test failures. On one of our projects, that work took ~20 hours a month.

We built NightWatch AI to triage failures while the team sleeps. It categorizes each failure as a product bug, a broken test, or a flaky test, and drafts tested fixes for the last two.

The team reviews everything in the morning and makes the final call.


Automated tests are meant to save time. They run overnight, so nobody has to click through the application by hand. But on a bad night, when a run comes back with failures, automation stops helping. Someone still has to find out why the test failed. 

On one of our projects, the suite has more than 3,000 end-to-end test executions, and those questions eat up about 20 hours a month. It never feels urgent in the moment, but it is slow, unglamorous work that delays the engineering day and keeps deployments on hold.

We built NightWatch AI to handle that initial diagnostic phase overnight while the team is offline.

Three reasons a test turns red

A failing test is like a smoke alarm. It sounds the same for burnt toast and for a real fire. In end-to-end testing, red usually means one of three root causes:

Failure type

What's going on

What needs to happen next

Product bug

The application is broken.

Engineers fix the issue using clear reproduction steps.

Broken test

The product changed, and the test hasn't caught up.

Someone updates the test.

Flaky test

The test passes on some runs and fails on others.

Someone makes it more stable, usually by changing how long it waits or how it finds things on the page.


Flaky tests are the hardest to pin down. They usually work fine when you run them by hand.

Getting the type wrong is costly. A bug ticket for an outdated test wastes engineering time, and a “fix” that hides a real defect is worse. Until now, sorting this out always needed a person.

What happens overnight

The project already ran its build, deploy, and test stages in GitHub Actions. We added one more stage at the end. It only starts when end-to-end tests fail, so passing runs cost nothing.

Think of NightWatch as a triage nurse for failed tests. Before touching any code, it sorts each failure by type. The agent reads the failure report (error, stack trace, and the step-by-step trace) and the failing test's code, and classifies from that first. 

Only when code and trace are not enough does it use one of two tools: 

  • a headless browser capture that replays the test's navigation on the environment where it failed and records a screenshot, the accessibility tree, console and network errors; 

  • a re-run of the test up to three times to tell a flaky test from one that fails every time.


Then it acts on the verdict:

  • Bug: it records its verdict, the reason, and reproduction steps in the pipeline run's summary. A person decides whether to raise a bug ticket. We tried automatic Jira ticket creation in an experimental version and may add it later.

  • Broken test: it fixes the test and opens a draft pull request (PR).

  • Flaky test: it stabilizes the test and opens a draft PR.

Fixes are not just proposed; they are run. Before changing a broken test, NightWatch runs it as it is. If it passes, the test counts as flaky. 

After the fix, NightWatch compiles the test and runs it against the live application, with up to two tries. By morning, the QA team finds either a bug report with reproduction steps or a verified draft PR, ready to review.


None of the parts are new. The implementation uses components that already exist: GitHub Actions as the host, Claude Code run as a CI step with repository access, a headless Playwright script for on-demand reproduction, and Gradle for re-running and verifying tests. What we built is the way they work together.

Two real runs, up close

Diagrams are tidy. But production runs reveal how systems actually behave. Here are two real scenarios from our deployment pipeline.

On the first run, NightWatch called one failure flaky. Before fixing anything, it re-ran the test and changed its mind: the test was broken. It fixed it, opened a draft pull request, and the verification run came back green.

The fixes were tiny: one changed a true/false value, the other a method call's arguments. Finding where those parameters broke was the time-consuming step. A basic pattern-matching script would have accepted the initial verdict, but the agent re-verified before acting.

In the second run, two failures were classified as product bugs. NightWatch did not modify the application code or open a PR. Instead, it documented each failure in the GitHub Actions run summary, including the BUG classification and an explanation of what was broken. A team member then reviews the findings and decides whether to create a bug ticket.

Where we drew the lines

Letting an AI agent loose in a repository at night, with nobody watching, sounds like the start of a cautionary tale. It only feels comfortable when the limits come built in, so we set them up.

NightWatch is not an unrestricted agent. It runs automatically after scheduled weekday nightly runs, and after manual runs only when someone opts in. It analyzes an area only when at most two distinct tests fail there, and at most three analyses start per nightly run, which caps cost on a bad night.

A few more rules sit on top of that:

  • Proof before review. Every fix must compile and pass against the live application.

  • Draft pull requests only. That's as far as the agent can go on its own.

  • A human signs off. A person approves before anything is merged.

  • A clock on every job. Each job has a 45-minute limit, with budgets of 30 minutes for analysis and 20 for fixing.

NightWatch handles the diagnostics and proposes fixes; our engineers make all final merge decisions. The system is designed around continuous human oversight.

A straight answer on cost

Here's how the cost picture looks so far:


Manual triage

NightWatch AI

What we measured

~20 hours of engineering time monthly ($25 blended hourly rate)

$1.50–$3.00 per run

Monthly cost

Roughly $500

Roughly $18–$50


The numbers look good side by side, but we haven't measured hours before and after yet. So the 20 hours show the size of the problem, and we'll report real savings once we have the data.

What's next for NightWatch

NightWatch is still young, and we're treating it that way. The current version covers the workflow described above. The team is also tracking which AI-generated draft pull requests are merged as they are, which require changes, and which are rejected. 

Reviewing these outcomes helps the team understand where the agent’s reasoning differs from an experienced QA engineer’s judgment and provides feedback for refining how NightWatch analyzes and fixes broken and flaky tests.

Next, we plan to teach NightWatch to fix application bugs too. We also want to build a knowledge base of past fixes and product context and optimize execution time and cost.

Portability remains an open question. NightWatch was built into this project's pipeline. The team sees potential for a reusable approach, but its application to other projects and CI environments has not yet been demonstrated.

The broader lesson

Automation has run tests for years. What it couldn't do was the step after a failure, and that's the step NightWatch takes on while the team is offline.

We think this works well beyond testing. An AI agent with clear limits can take on a lot of repetitive investigation, as long as five things are in place:

  • Context: test code, the failure report and step trace, and the live application.

  • Tools: a browser capture, the repository, and test re-runs.

  • Decision boundaries: a small set of categories, each tied to a specific allowed action.

  • Verification: compilation, live runs, and re-runs before anything is proposed.

  • Human oversight: a person who approves before changes land.

Key takeaways

For our QA team, the primary benefit is an improved morning workflow. Instead of spending hours investigating ambiguous logs, they start the day reviewing documented bug summaries or verified pull requests. 

Chances are your company has a job like failed-test triage tucked away somewhere in its delivery process, the kind that eats a few hours every week and never feels urgent enough to fix. We'd start with an AI agent. Pick one job, keep a person approving every change, and once the agent has proved itself there, you'll have a much clearer idea of what else to hand over.

If you're weighing up where an agent could fit into your own pipeline, we'd be happy to give you a hand.

FAQ

Can an AI agent fix bugs in the application, or only in tests?

For now, only tests. When NightWatch finds a product bug, it leaves the application code alone and writes up the bug in the run summary. A person decides whether to open a ticket.

How does an AI agent tell a flaky test from a broken one?

It reads the failure report and the test's code first. If that's not enough, it re-runs the test up to three times to see whether it fails every time. In one of our first runs, that re-run turned a “flaky” verdict into “broken.”

Could an AI “fix” end up hiding a real bug?

That's the risk our limits are built for. A bug verdict never leads to a code change. Test fixes must pass against the live application, and a person approves every merge.

What happens when a lot of tests fail on the same night?

NightWatch backs off. It skips areas with more than two failing tests and runs at most three analyses a night. The rest waits for the team, which also keeps costs in check.

Can we use NightWatch AI in our own pipeline?

Not as a ready-made product yet. It was built for one project, and hasn't been tested elsewhere. But you can borrow the approach: if your setup covers the five things above, it's worth a try.

Let’s get in touch!

Fill out the form below, and we’ll get in touch soon

Geo Marquez

SVP of Growth

Share a few details about your project,

and Geo will personally review your request.

Trusted by

Let’s get in touch!

Fill out the form below, and we’ll get in touch soon

Geo Marquez

SVP of Growth

Share a few details about your project,

and Geo will personally review your request.

Trusted by

Let’s get in touch!

Fill out the form below, and we’ll get in touch soon

Geo Marquez

SVP of Growth

Share a few details about your project,

and Geo will personally review your request.

Trusted by