AI Code Review: Finding Defects Without Creating More Work


Greetings, traveler!

A navigation change passes its unit tests and the app builds. The pull request replaces several screen-specific transitions with a SwiftUI NavigationStack. The implementation agent reports that the change is ready, but an independent review finds a navigation defect.

When a user opens a notification while already viewing an order, the app adds the new destination on top of the old path. The requirement was to show the order list followed by the selected order. Pressing Back first shows the newly added list; pressing Back again reveals the previously viewed order, which should no longer be in the stack.

The defect is small enough to fit in one line of code, but finding it requires more than reading that line. A reviewer needs the expected navigation behavior, the current route state, and the entry points that can change it. An AI reviewer can help trace those relationships and identify possible regressions. Its comments may also describe missing requirements or scenarios the application never supports.

The examples in this article describe a hypothetical iOS application. The review comments and test results are illustrative; they are not the outcome of an actual project review.

What should an AI reviewer examine?

A useful review starts with a question about a specific change. “Find problems in this code” leaves the agent to guess which behavior is important and how the system is supposed to work. That often produces comments about naming, hypothetical edge cases, and preferred patterns while missing a requirement outside the diff.

For a navigation change, I would separate the investigation into three related questions:

  • Requirements: Does the change preserve the expected behavior for in-app navigation, notifications, and deep links? Is any requested behavior missing entirely?
  • Architecture: Who owns the navigation state? Do modules remain responsible for the routes they can actually handle? Does the new design fit the existing lifecycle and deployment targets?
  • Implementation: Can a particular sequence of events produce an incorrect path, lose state, or show the wrong screen? Do tests exercise those sequences?

These are lenses, not necessarily separate agent runs. A small change may need one focused review; a navigation migration affecting multiple modules may benefit from separate passes. In either case, the reviewer needs access to relevant surrounding code. A diff shows where a change occurred, but a route handler, a caller, or a test outside the diff can determine whether that change is correct.

It also helps to distinguish the code’s current behavior from the desired behavior. Tests describe some of the current contract, but they can be incomplete or encode an old decision. Requirements describe intent, but they may omit interactions with existing features. When they disagree, the review should surface the disagreement instead of choosing whichever source supports the implementation.

Give the reviewer a stable target

A second agent can provide a useful fresh perspective after an implementation is ready. Start it in a separate session or context so that it receives the requirements and the actual changes without inheriting the implementer’s reasoning and assumptions. This does not make its judgment independent in a statistical sense: agents may share the same model, training, or blind spots. It does remove one obvious source of bias, the implementation conversation itself.

For a committed change, identify the comparison explicitly:

BASE=$(git merge-base origin/main HEAD)
HEAD=$(git rev-parse HEAD)
git diff --stat "$BASE" "$HEAD"
git diff "$BASE" "$HEAD"

Fetch or otherwise verify the target branch reference first. In a stacked pull request, use the actual parent branch as the comparison target; comparing every branch to main can repeat changes already reviewed. If the parent branch changes or is rebased, update the reference, recalculate the merge base, and inspect the diff to confirm it contains the intended changes. These commands compare commits and do not include uncommitted work. Commit or otherwise capture the working tree before review, and include relevant untracked files if the tool supports them.

The request should include the exact revisions, acceptance criteria, project constraints, and where to find relevant code. For example:

Review the navigation change between BASE_SHA and HEAD_SHA.

Requirements:
- Opening an order from the order list pushes its detail screen.
- Opening an order from a notification replaces the current path with
  the order list followed by that order.
- The system Back action must update the same navigation state used by
  programmatic navigation.
- This change does not add navigation state restoration.

Inspect the diff and follow relevant entry points, route definitions,
state ownership, and tests. Do not modify files.

Report actionable issues with a file and line, a concrete event sequence,
the expected and actual behavior, and any assumptions you could not verify.
Separate confirmed defects from questions. Ignore style preferences unless
there is a documented project rule or a concrete maintenance cost.
State what you examined and which checks you did not run.

Replace the placeholders with actual commit IDs before sending the request. If a reviewer has only a patch and no repository access, say so. It can still flag a suspicious change, but it cannot verify an unseen caller or determine that no other path resets the state.

The author can supply a brief change summary, but a summary is a guide to inspection, not evidence that the code works. Avoid passing the implementer’s conclusion as a constraint such as “this correctly handles all deep links.” Give the reviewer enough information to check that claim.

A navigation defect in the diff

Suppose the implementation represents the stack with a typed route array. The example uses NavigationStack, which requires iOS 16 or later. Projects supporting older versions need a separate navigation approach for those devices.

import SwiftUI

The router publishes its path:

enum Route: Hashable {
    case orders
    case order(id: UUID)
}

@MainActor
final class OrderRouter: ObservableObject {
    @Published var path: [Route] = []

    func openOrderFromNotification(id: UUID) {
        path.append(.orders)
        path.append(.order(id: id))
    }
}

The SwiftUI container uses the same path for display and programmatic navigation:

struct OrderNavigationView: View {
    @ObservedObject var router: OrderRouter

    var body: some View {
        NavigationStack(path: $router.path) {
            HomeView()
                .navigationDestination(for: Route.self) { route in
                    switch route {
                    case .orders:
                        OrderListView()
                    case .order(let id):
                        OrderDetailView(orderID: id)
                    }
                }
        }
    }
}

The feature owner creates and retains the router, then passes it to OrderNavigationView. The screen implementations are omitted because they are project-specific.

If the path is empty when a notification arrives, the resulting path is [.orders, .order(id: newID)]. A test that starts with an empty path and checks only the resulting route array would pass. Now consider a user who is already viewing an order. Given an existing path of [.orders, .order(id: oldID)], the handler appends another pair of routes. The first Back action shows the newly appended list; the next reveals the previous order. The required path would contain only the list and the newly selected order.

A useful finding would make that sequence explicit:

Severity: High
Location: OrderRouter.openOrderFromNotification(id:)

When the path already contains an order, opening a notification appends
another list and detail to the existing stack. Pressing Back twice reveals
the previous order. The requirement says notification navigation should
replace the current path.

Reproduce: set the path to [.orders, .order(id: oldID)], then call the
handler with newID. The resulting path contains both orders.

Suggested check: add a test starting with a non-empty path and assert that
the final path is [.orders, .order(id: newID)].

The severity is a team decision. In an application where order details contain sensitive information, a misleading navigation history may warrant blocking the change. Elsewhere, the impact may be lower. The event sequence and violated requirement give the team enough information to make that decision.

Given the stated contract and a single shared path, the targeted change is:

func openOrderFromNotification(id: UUID) {
    path = [.orders, .order(id: id)]
}

That assignment is not a universal deep-link solution. If the app has separate stacks per tab, authentication gates, multiple scenes, or asynchronous route resolution, those owners and transitions need review too. The small fix is appropriate only after confirming that this router owns the relevant path and that replacing it is the intended behavior.

Verify the finding and the fix

Before changing code, inspect the complete handler and its callers. Another layer might already clear the path before calling this method. If it does so for every notification entry point, the reviewer’s proposed failure may be a false positive. If only some entry points clear it, the behavior depends on the caller and the contract between the two layers deserves attention.

A focused regression test for the hypothetical router could look like this:

@MainActor
func testNotificationReplacesExistingNavigationPath() {
    let router = OrderRouter()
    let oldID = UUID()
    let newID = UUID()
    router.path = [.orders, .order(id: oldID)]

    router.openOrderFromNotification(id: newID)

    XCTAssertEqual(router.path, [.orders, .order(id: newID)])
}

The test belongs in an XCTestCase with XCTest and the app module imported, typically using @testable import when the declarations are internal. It checks the router’s state transition; it does not prove that a real notification reaches this handler or that the Back button behaves correctly in the running app. Those questions need an integration or UI scenario using the application’s actual notification routing and SwiftUI container.

For this change, a sensible verification set includes the focused test, existing navigation tests, and a build for the supported target. Exercise the notification while already on an order detail screen and verify the Back sequence. If the environment cannot deliver a notification or run the simulator, record that limitation. A skipped scenario remains unverified even when every executed test passes.

After a fix, review the new diff and rerun checks against the resulting revision. A test report from before the fix says nothing about whether the fix introduced another problem. The final review should name what was checked and what remains unknown.

When a review comment adds unnecessary complexity

Imagine the reviewer also proposes a persistence layer for every route so that the stack survives an application restart. That could be useful for some products, but the stated scope explicitly excludes restoration. Adding storage, migration handling, and recovery behavior here would introduce a new feature and new failure modes. The appropriate response is to decline the suggestion for this change, not to silently implement it.

Another comment suggests wrapping path in an actor because notifications may arrive on different threads. The router in the example is isolated to MainActor, and its caller must respect that isolation. An additional actor is not justified by the snippet alone. The reviewer should inspect the caller: if it is not already isolated to the main actor, it must arrange a safe transition before accessing the router. The right approach depends on the callback and its isolation. If the caller cannot be inspected, the concern remains a question.

Defensive code can be necessary. An external URL is untrusted input, so parsing, validation, authentication, and authorization may be required at the boundary. The decision depends on the supported inputs and the consequences of failure. A guard that silently discards a valid deep link can be a regression; a guard that rejects malformed data before it reaches a router may be essential.

For each proposed safeguard, ask what input reaches it, what would happen without it, and whether its failure behavior is visible to the user or caller. If the reviewer cannot trace a plausible input to the relevant boundary, it should say so. If the consequence is severe, a low-frequency scenario may still deserve investigation.

The same standard applies to abstractions. A protocol introduced for several real implementations can make a boundary easier to test or change. A protocol, factory, and registry around one local function can make the call path harder to follow without providing a current benefit. Review the cost and purpose of the abstraction in the context of the codebase rather than treating either abstraction or simplicity as a rule by itself.

Triage findings without hiding uncertainty

Severity and confidence answer different questions. Severity describes the potential impact if a finding is correct. Confidence describes how well the evidence supports it. An unverified report of a serious authorization bypass may have high potential impact and low confidence; it still deserves prompt investigation.

A practical way to process a review is to assign each finding one of these dispositions:

  • Confirmed defect: the event sequence or a reliable test demonstrates the problem. Fix it, or document the decision and owner if the team accepts the risk.
  • Plausible concern: the reviewer identified a credible failure but could not establish an assumption. Investigate the missing caller, requirement, or runtime behavior.
  • Suggestion: the behavior appears correct, but an alternative could improve the design. Evaluate its benefits and cost without treating it as a blocker by default.
  • Unsupported or incorrect: the cited scenario cannot occur under the verified contract, or the suggested change would break intended behavior. Record the reason when it helps the author or another reviewer understand the decision.

A reviewer can be wrong in both directions. A false positive consumes attention and can lead to unnecessary changes. A false negative leaves a defect unreported. Filtering verbose comments may improve the signal the engineer sees, but it can also discard a real issue. Ask an agent to explain its filtering decisions and retain access to the original findings, especially for security-sensitive or high-impact changes.

Do not measure review quality by the number of comments or the percentage accepted. A reviewer that emits one hundred speculative suggestions may appear thorough while making an important defect harder to notice. Conversely, a quiet review may reflect excellent code or a missed regression. Track recurring rejected comments, defects found after review, and the time needed to triage findings, then use that feedback to refine the scope and instructions.

Respond to review feedback as engineering input

A second AI agent can help group duplicate comments, locate the code involved, or check whether a suggested behavior is already covered. Ask it to preserve the original findings and produce a short rationale for each recommendation. A blanket request to “fix all review comments” gives it authority to turn every suggestion into a code change, including contradictory or unsupported ones.

When a finding is valid, make a targeted change and check the relevant behavior. When it is unclear, investigate the missing context or ask the person who owns the requirement. When it is incorrect, respond with the evidence: a caller that guarantees the state, a documented constraint, or a test that demonstrates the intended behavior. A passing test is useful evidence only for the behavior it actually exercises.

Review comments can also reveal gaps outside the patch. If a notification flow was already broken before the change, distinguish that existing defect from a regression introduced by the pull request. Depending on the risk and the scope, the team can fix it now, create a separately owned task, or explicitly accept it. Avoid making unrelated changes without a decision about their scope.

For a change with multiple findings, agree which ones block merging. A reasonable policy is to resolve confirmed critical and high-impact defects before merge, investigate serious unresolved concerns, and record the owner and rationale for any accepted risk. Teams may use different severity labels; the important part is that the decision reflects the consequence and the evidence.

Keep the human review focused

The engineer accepting a change should still inspect the behavior and design, especially where the code crosses important boundaries. For the navigation example, that means understanding who owns the path, how notifications reach it, and what happens when the user goes back. An AI report can point to the relevant files, but it cannot give the team ownership of a design it has not examined.

Use automated tools for checks they can perform reliably, such as compilation, formatting, and deterministic tests. Use AI to investigate relationships and surface questions that benefit from contextual analysis. Neither a passing build nor an empty AI review establishes that every requirement is satisfied.

The amount of review should match the risk. A copy change may need a quick diff inspection and a visual check. A navigation rewrite that touches authentication, payments, or multi-scene behavior needs deeper investigation and people familiar with those flows. Multiple review agents can help on a large change, but they also add coordination, cost, and duplicate findings. Start with the questions that need answers, then add reviewers when a separate perspective is likely to help.

Before accepting the change, the engineer can use the review record to check unresolved risks and decide what still needs investigation.