How to stop AI coding loops in a fragile codebase

When every AI fix causes another bug, reduce the failing case, trace state and business rules, and repair one boundary with a repeatable check.

Plainrelay · Updated

Recognize when another prompt is widening the problem

You ask for a login fix and receive a new auth provider, a rewritten component, and a changed database query. Sign-in works once, but refreshing breaks it. The next prompt adds another state variable. The code grows while the same customer journey remains unreliable.

Watch for repeated changes to unrelated files, competing versions of the same calculation, error handlers that discard failures, and a different explanation after each retry. These are reasons to investigate state ownership, contracts, and boundaries.

Capture a reproduction before changing code

Write down the starting state, exact actions, expected result, and observed result. Record the commit, runtime version, and relevant configuration names. Use test accounts and redact credentials and customer data from logs.

Starting state: a saved draft estimate with two line items
Action: edit quantity, save, then refresh
Expected: saved quantity and total remain consistent
Observed: quantity persists, total returns to its old value
Evidence: request payload, response, and stored record
Success check: repeat with two quantities and two separate users

If the problem is intermittent, add request IDs and timestamps around the affected operation. Establish whether two requests race or one response arrives late. Keep the reproduction small enough to run after every candidate change.

Trace the value through the system

Follow one field from input to request, validation, calculation, storage, response, and rendering. At each step, ask who is allowed to change it and which value is authoritative. Draw the sequence or list the functions involved before moving them.

Find duplicated business rules

In the estimate example, the client might calculate a total while a background task uses an older formula. Choose the owner of the calculation, make callers use its result, and test rounding and missing values at that boundary.

Find competing UI state

A component may store the same selection in props, local state, and a fetched object. Identify which state is necessary, derive values where possible, and check what happens when an older request resolves after a newer one. React’s guidance on avoiding redundant state is a useful review reference.

React guidance on state structure

Check the runtime boundary

A server module imported into browser code, an unchecked API response, or a build-time variable used as a runtime secret can create failures that repeated UI edits cannot resolve. Follow the actual request and inspect the deployed configuration.

Make one repair with a clear verification step

  1. Save a known baseline in version control and reproduce the failure.
  2. Add a regression check that fails for the observed bug.
  3. Change the smallest boundary that owns the incorrect behavior.
  4. Run the regression check, existing relevant tests, type checks, and the production build.
  5. Exercise the same journey in staging, including refresh, retries, and a second account where relevant.

A useful AI instruction names the reproduction, the files involved, the expected behavior, and the check to run. Ask for an explanation of the proposed change before accepting a broad diff. Review whether every changed file contributes to the observed failure.

Refactor around a working customer journey

Once the failure is covered, extract repeated rules, tighten the affected types, and simplify the state flow in separate changes. Maintain the public API and saved-data behavior that callers rely on. Where a contract must change, plan how old records and old application instances will behave during deployment.

For example, adding a calculated field may require a backfill and a compatibility window before old code can stop running. State the release order and recovery path before changing the schema.

The Scope2Quote project illustrates explicit boundaries between an AI-proposed scope, contractor review, deterministic pricing, and a saved estimate. These boundaries give each stage inputs and outputs that can be verified independently.

Know when to bring in an engineer

Get a focused review when you cannot reproduce the issue consistently, ownership rules are unclear, or changes affect payments and several customer accounts. Bring the reproduction, current repository, relevant logs, and release constraints. A useful audit should return evidence and ordered fixes, with a success check for each repair.

For hands-on help with duplicated logic or unstable Next.js state, see the Cursor codebase refactoring service.

Apply this to your app

Work through the production readiness checklist to identify the remaining gaps. For a written review of your code and a prioritized repair plan, explore the AI app production audit.

Discuss my audit