Other

Use Coding AI to Fix a Cross-File Bug: A Repeatable Workflow

2026-11-25·6 min read·Updated 2026-11-25

Start with the answer

To fix a cross-file bug with coding AI, give the model a narrow, reproducible failure, the three to six files that participate in it, and one requested output: a minimal patch plus the exact test command you will run next. Then run the test yourself, inspect the diff, and send only new evidence back. That workflow turns a vague "fix my app" request into a controlled loop where the AI proposes and you verify.

This guide uses a concrete TypeScript example you can adapt. You will need a local repository, a failing test or reproduction script, and an AI assistant that can read multiple files. Full cross-file reasoning is still a review task, not an autopilot task; treat the patch as a draft.

For a broader look at how models handle cross-file defects, see GPT-6 coding review notes. When choosing the assistant itself, compare context handling and test integration in the 2026 developer assistant comparison.

A hand choosing between a small connected set of file cards and a sprawling pile, illustrating scoped versus unscoped debugging.

Starting material and settings

What to prepare

You need three things before the first prompt:

  1. A failing command. For example: npm test -- --runInBand billing.test.ts. It must fail for the bug you care about, not for an unrelated setup error.
  2. The participating files. In the worked example below: src/billing/invoice.ts, src/billing/tax.ts, src/orders/order.ts, src/orders/order.test.ts, plus the relevant package.json script. Keep the list short.
  3. One expected behavior sentence. Write it in plain language, such as: "A refunded order must not count tax twice when the invoice is regenerated." If you cannot state it in one sentence, the bug is not scoped enough for a useful AI pass.

Remove secrets, customer data, and any file the model does not need. If your assistant supports a context or file-picker window, select files explicitly rather than attaching the whole repository.

Settings that matter

SettingRecommended starting valueWhy
Context scope3–6 named files, not full repoKeeps the bug visible and reduces unrelated edits
TemperatureLow or 0.1–0.3Patch style should be repeatable
Output lengthShort, with a unified diffForces a reviewable proposal
ToolsRead and diff only; no auto-commit, no auto-deployYou keep control of the repository
Test commandProvided by you, not guessed by the modelPrevents invented test paths

If the assistant offers an "agent" mode that executes commands, start with it disabled. Run the test command yourself so you know the failure and the fix are real.

Steps: run the loop

  1. Confirm the failure locally. Run the test and save the exact output. If it passes, stop; you have a different problem.
  2. Ask for a diagnosis first, not a patch. Prompt: "Here is a failing test and five files. Identify the smallest set of files involved and name the faulty assumption. Do not write code yet." Review that answer against the actual code.
  3. Request a minimal diff. Name the files you will accept changes in and require a unified diff. Reject new dependencies or whole-file rewrites.
  4. Run the specified test. Use your own command. Do not accept a test the model claims to have run.
  5. Inspect the diff for cross-file side effects. Check call sites of any changed function signature and any shared state.
  6. Add one regression test. The AI may draft it, but the test must fail before the fix and pass after it.
  7. Repeat with only new evidence. Paste the new error or the diff you rejected. Do not re-paste the whole conversation.

Worked example

A simplified billing flow fails: regenerating an invoice after a refund doubles the tax line. The test asserts invoice.taxCents === 700 for a 10,000-cent order at 7%, but gets 1400.

The first prompt you send:

Prompt
Failing command: npm test -- --runInBand billing.test.ts
Expected: invoice.taxCents === 700
Actual: 1400
Relevant files: src/billing/invoice.ts, src/billing/tax.ts,
src/orders/order.ts, src/orders/order.test.ts
Request: name the faulty assumption and the minimum files to change.
Do not write code yet.

A plausible diagnosis: order.refunded is checked in invoice.ts, but tax.ts recomputes the tax from the original order total instead of the adjusted subtotal. The refund path calls regenerateInvoice, which calls calculateTax twice. The minimal fix is in tax.ts and the call site in invoice.ts; the order model is only read.

You then request a diff limited to those two files, with a regression test in billing.test.ts. Run the test command again. If the output moves from 1400 to 700, the fix is a candidate. If it moves to 350, you have found a second defect: the refund adjustment is being applied twice. Send that new evidence back and require the model to explain the order of operations before editing again.

What to check in the proposed patch

  • Every changed line is necessary. Cross-file bugs often tempt the model into refactors that hide the real cause.
  • Function signatures have all call sites updated. Search for the function name across the repository.
  • Shared state is not mutated in a new place. Check module-level caches, singletons, and default parameters.
  • The regression test fails on the original code. Stash the patch and run it.
  • The full test suite still passes. A narrow fix that breaks adjacent tests is not done.
  • No invented imports or paths. Verify every new file path exists.

When it fails, change one variable

FailureWhat to change
AI asks for more filesAdd the one file it names, not the whole folder
Patch touches unrelated modulesRestrict allowed files in the prompt and require a diff
Diagnosis is vagueAsk for the faulty line and the assumption behind it
Test still fails identicallyConfirm the test imports the changed module, not a built copy
Fix passes the new test but breaks othersAsk for the shared-state or call-site analysis, then rerun the full suite
Model claims it ran testsIgnore that claim; run the command yourself and paste the output

If two rounds produce no new information, stop. The bug may need a human trace through runtime logs or a debugger, which is outside what the model can see.

Limits and a sane stopping rule

The workflow does not guarantee a correct patch, and it does not replace code review. Models can misread call order, invent test output, or produce a diff that compiles but changes behavior elsewhere. Cross-file bugs are exactly where confident-sounding but wrong patches are most likely. Keep the patch small, keep the test command yours, and keep a human in the loop for anything touching payments, authentication, or data deletion.

A good stopping rule: if the failing test passes, the regression test fails on the original code, the full suite passes, and the diff touches only the named files, you have a reviewable candidate. If any of those is false, continue the loop or escalate to manual debugging. The value of coding AI here is not autonomy; it is a faster, better-documented diagnosis that you can verify.

Scope a cross-file bug fix

Act as a code review assistant. Inputs: a failing test command, the exact error output, one expected-behavior sentence, and up to six named source files. Constraints: do not invent file paths, test results, packages, or benchmarks; do not suggest new dependencies; limit any patch to the named files; do not claim to run commands. Task: first identify the faulty assumption and the minimum files involved, then propose a unified diff and one regression test. Output format: (1) Diagnosis in three bullets, (2) Unified diff in a fenced code block, (3) Regression test in a fenced code block, (4) The exact test command for the human to run, (5) Two risks to check in review.

Continue in Studio

Further reading

Download desktop & mobile app

Access Ottermind anytime, anywhere.

Computer