How To
How to Use an AI Assistant to Fix a Cross-File Bug Without Breaking the Build
The outcome and what you need before you start
By the end of this workflow you will have a merged fix for a bug that touches at least two files, with a passing test that would have failed before the change, and a short note explaining why the fix belongs where it does. The workflow is deliberately boring: collect context, constrain the assistant, patch one seam at a time, then verify with the same tests a reviewer would run.
You need five things before the first prompt:
- A reproducible failure. A single command or click path that fails every time, with the exact error text.
- A test harness that runs offline. If your suite needs a live database or a third-party key, stand up a local fixture first; otherwise you cannot tell a real fix from a lucky guess.
- A clean working tree, or a branch you can discard. Ask the assistant to work on one branch, and keep your own stash of unrelated edits out of it.
- A small dependency map. List the files you already suspect, plus one layer above and below them. This is the boundary you will police later.
- A short failure statement in plain language, such as: “Saving a draft with an empty tag list throws
TypeError: undefined is not iterablein the serializer, but only when the draft was created before the last schema change.”
If any of these are missing, stop and build them first. An assistant given a vague symptom and a whole repository will produce a plausible-looking patch that moves the error rather than removing it.

Choose the assistant mode and settings that fit a cross-file bug
Most assistants offer at least three modes: inline completion, chat with selected context, and an agent that can read files and run commands. For a cross-file bug, use the agent mode but keep its permissions narrow. Inline completion is the wrong tool because it sees only the current file; chat without file access forces you to paste code and lose the import graph.
Settings to set before you start:
- Read scope: the package containing the bug, its tests, and the shared types it imports. Exclude generated files, vendored code, and build output. A wide read scope is cheap; a wide write scope is not.
- Write scope: one directory. If the fix genuinely needs a change outside it, the assistant should propose the diff and stop, not apply it.
- Command allowlist: your test runner, your type checker or linter, and read-only git commands. Do not allow package installs, migrations, or network calls in the first pass.
- Diff size: cap it. A reasonable ceiling for a bug fix is a few dozen changed lines across two or three files. If the assistant wants to touch ten files, it has found a refactor, not a bug.
- Autonomy: require approval for every write. You want to see each patch before it lands, especially in the first hour.
One more setting matters more than people expect: turn off automatic test generation for the first attempt. You want the assistant to reproduce the existing failure before it writes anything. Generated tests on a broken baseline often encode the bug as expected behavior.
If you are still choosing a tool, the comparison of AI assistants for developers is a useful starting point, and the AI assistant versus agent versus copilot distinction explains why the agent mode is the right default here.
Step-by-step: trace, isolate, patch, verify
Step 1: Make the assistant reproduce the failure in its own words
Give it the failure statement, the exact command that fails, and the error text. Ask for a numbered trace from the user action to the throwing line, naming every file and function on the path. Do not ask for a fix yet. The trace is the artifact you will check; if it skips a file or invents a function, you have found the limit of its context before it has written a line of code.
Step 2: Ask it to name the seam
A seam is the boundary where correct data becomes incorrect. Common seams are a serializer that assumes a list, a cache key that omits a tenant, or a default that was correct under an old schema. Ask: “Which single function is the earliest point where the wrong value exists?” Then ask it to prove that claim by showing the caller and the callee. If it cannot, it is guessing.
Step 3: Patch the seam, not the symptom
Instruct it to change the earliest responsible function and to leave every downstream workaround alone in this pass. Downstream guards often hide the real bug; removing them in the same commit makes the diff impossible to review. One seam, one patch.
Step 4: Verify with the failing path first
Run the original reproduction, then the surrounding test file, then the type checker. Ask for the raw output, not a summary. A patch that passes the new test but changes the output of an existing snapshot test has not been verified; it has been re-baselined.
Step 5: Write the note
Have the assistant write three sentences: what the wrong value was, where it was created, and why the fix belongs there rather than at the call site. This note goes in the pull request description. It is also the fastest way to catch a fix that works by accident.
A worked example: empty tag list breaks the draft serializer
Assume a small content app. Drafts created before a schema change have no tags field. The serializer iterates draft.tags, and saving such a draft throws TypeError: undefined is not iterable.
The first prompt gives the assistant the reproduction command, the stack trace, and the write scope of the drafts package. It returns a trace: the save handler reads the draft, passes it to normalizeDraft, which calls the serializer, which spreads draft.tags. It names normalizeDraft as the seam, because that is the first function that emits a draft object without a tags list.
The proposed patch is four lines in normalizeDraft: default tags to an empty list when the field is absent, and leave the serializer untouched. That is the right shape. A serializer-level ?? [] would fix this caller and every future one individually; the normalizer fixes every caller at once.
The second prompt asks for a regression test at the normalizer level using a fixture draft with no tags field, and for the original reproduction to be run before and after. The raw output shows the new test failing on the unpatched branch and passing after. The existing serializer tests still pass, which is the check that the patch did not quietly change output for well-formed drafts.
What to check before merging: the diff stays inside the drafts package; no schema migration is included; the fixture matches the real legacy shape, not a simplified guess; and the commit message names the seam. If the assistant changed a snapshot file to make tests pass, treat that as a failure and start again from Step 2.
When the first patch fails, change one variable at a time
Failure has a few common shapes, and each has a specific response.
The trace is wrong. The assistant named a function that does not exist or skipped the file that actually throws. Narrow the read scope to the files in the stack trace and ask for the trace again with line numbers. If it still invents code, paste the two or three relevant functions directly and work in chat mode.
The patch fixes the symptom. The error disappears but a related path still fails. Ask for the caller list of the patched function and run each caller's tests. If the seam was misidentified, revert and return to Step 2 with the caller list as new evidence.
The patch is too large. Ten files changed means the assistant drifted into a refactor. Revert, restate the write scope, and add the instruction: “Do not rename, reformat, or move code in this pass.”
Tests fail for unrelated reasons. Check the baseline first. Run the suite on the untouched branch; if it already fails, the assistant's patch is not the cause and your verification is meaningless until the baseline is green.
The fix requires a schema change. Stop. A cross-file bug that needs a migration is a design decision, not a patch. Ask the assistant to write the options and their trade-offs, then decide yourself. For a longer treatment of this workflow, including how to keep patches small and reviewable, see the repeatable cross-file bug workflow.
What to check at every iteration: the reproduction still fails on the base branch, the diff stays inside the write scope, the test that proves the fix fails without it, and the note names the seam. If all four hold, you have a fix you can defend in review.
Limits worth stating plainly
An assistant cannot tell you what your users meant, whether a legacy field was intentionally absent, or which of two correct fixes your team prefers. It can trace, propose, patch, and run commands; it cannot own the decision. Keep the write scope tight, require approval on every change, and treat any patch that alters a snapshot or skips a test as unverified until you have checked the baseline yourself.
Turn your cross-file bug into a traced, verified patch
Act as a coding assistant helping me fix a cross-file bug. Inputs: my failure statement, the exact reproduction command, the full error text, the list of files I allow you to read, and the single directory I allow you to write to. Constraints: do not rename, reformat, move or install anything; do not edit tests or snapshots to make them pass; require my approval before any write; keep the diff to a few dozen lines in at most three files. Steps: first give a numbered trace from the failing user action to the throwing line, naming every file and function; second name the single earliest function where the wrong value exists and show its caller and callee; third propose the smallest patch at that seam; fourth tell me the exact commands to run to verify the failure before and after, including the surrounding test file and the type checker. Output format: four headed sections (Trace, Seam, Proposed patch as a unified diff, Verification commands) followed by three sentences stating what the wrong value was, where it was created, and why the fix belongs there. Do not claim tests pass unless I paste the output.
Continue in Studio