Other

Fix a Cross-File Bug with an AI-Powered Coding Assistant Without Breaking the Build

2026-11-29·7 min read·Updated 2026-11-29

What you'll fix and how you'll know it's done

You have a cross-file bug: a value is produced in one module, transformed in another, and consumed somewhere that assumes a different shape. The symptom appears far from the cause. Your goal is one verified outcome: the failing behavior is reproduced by a test or command, the assistant's proposed change passes that check, and the full build stays green. Anything less is a draft, not a fix.

This workflow treats the AI-powered coding assistant as a context assembler and diff proposer, not an autonomous committer. You supply the intent, the boundaries, and the verification gates. The assistant supplies the cross-file trace and a patch that you review.

To ground the mechanics, this guide uses the same end-to-end approach described in How to Use an AI Assistant to Fix a Cross-File Bug Without Breaking the Build and the editor-side steps in Run a Cross-File Bug Fix in an AI-Powered Code Editor.

Before you touch the assistant, write down three things in your issue or scratchpad:

  1. The failing observable. A test name, a CLI command with expected exit code, or a UI action with expected text.
  2. The suspected data path. The field or symbol name that travels across files (e.g., user.profile.tier).
  3. The blast radius. Which packages, services, or public interfaces must not change signatures.

If you cannot name the failing observable, stop. An assistant will happily produce a plausible patch for a bug you haven't defined.

A developer chooses between a cluttered multi-file path and a narrow single-file path marked with a check, illustrating a minimal-fix decision.

Starting material and settings to configure

Gather the input set

A cross-file trace fails when the assistant can see the producer but not the consumer. Assemble a minimal but complete input set:

  • Reproduction: the exact command or test you will run, e.g. npm test -- --grep "tier upgrade".
  • Entry point: the file and line where the bad value surfaces.
  • Producer and transformer: the files that create and mutate the value. Use your editor's "find references" to list them.
  • Type contracts: interfaces, schemas, or DTOs that define the shape at each boundary.
  • Recent diffs: the last change to any file on the path—regressions often live there.

Choose assistant settings deliberately

Settings differ by tool; use these as intent, not as a product claim.

SettingChooseWhy
Context scopeSelected files + references, not whole repoKeeps the trace focused; whole-repo context invites noise
Edit modePropose a diff for reviewYou need a gate before writes land
Test executionOff until after reviewPrevents the assistant from "fixing" tests to pass
Temperature / creativityLowCross-file patches reward determinism
Output formatUnified diff + a short rationale per fileMakes review and rollback cheap

If your assistant can run commands, allow read-only inspection first (grep, open files) and withhold write or shell execution until you approve the diff. The distinction between assistive and autonomous behavior matters here; Personal Agent vs AI Assistant: What’s the Difference? is a useful framing when deciding how much latitude to grant.

Step-by-step: trace, patch, verify

Step 1 — Reproduce before you prompt

Run the failing check and capture the output verbatim. Copy the stack trace or assertion diff into your prompt. A reproduction is the contract the assistant must satisfy.

Step 2 — Ask for a trace, not a fix

First prompt: "Here is the failing test and its output. Here are the files on the suspected data path. Trace where user.profile.tier is produced, transformed, and consumed. List each file, line, and the assumed shape at that point. Do not propose code yet."

Review the trace against the actual files. Correct wrong assumptions before asking for a patch. This one step prevents most broken builds.

Step 3 — Request a minimal diff

Second prompt: "Propose the smallest change that makes the failing test pass without altering public signatures. Return a unified diff, one paragraph of rationale per file, and a list of any call sites you did not verify."

The "did not verify" list is your review checklist. Treat it as a set of risks, not a formality.

Step 4 — Review the diff at the boundaries

Read every changed line at module edges: function signatures, exported types, default values, and serialization. Cross-file bugs usually break because one boundary silently changes shape. Confirm that nullability, units, and optional fields are unchanged unless the fix explicitly requires it.

Step 5 — Run the narrow check, then the full build

Apply the diff, run the reproduction, then run the project's standard test and type-check commands. If your assistant can execute tests, run them in that order and paste failures back into the same conversation so the context stays intact.

Step 6 — Commit with a reversible message

Use a commit message that names the failing observable and the boundary changed. Keep the diff as one commit so a revert is one action.

Worked example: a tier field that arrives as a string

Symptom. A checkout test fails: expected pro, received undefined.

Reproduction. npm test -- --grep "tier upgrade" exits 1 with AssertionError: expected undefined to equal 'pro'.

Suspected path. api/session.ts builds the session object; lib/normalizeUser.ts maps the API response; components/PlanBadge.tsx reads user.profile.tier.

Trace result. The API returns { profile: { tier: "pro" } }. normalizeUser copies profile but strips unknown keys via a schema that omits tier. PlanBadge therefore reads undefined.

Minimal diff. Add tier to the schema in lib/normalizeUser.ts; no other file changes.

Prompt
// lib/normalizeUser.ts (before)
const ProfileSchema = z.object({ name: z.string() });

// after
const ProfileSchema = z.object({ name: z.string(), tier: z.string().optional() });

Verification. The grep test passes. The full suite passes. Type-check passes. The assistant's unverified list flagged two other consumers of profile; you open both and confirm they tolerate an optional tier.

What would have broken the build. Making tier required in the schema would have failed on legacy users with no tier. The optional marker is the boundary decision the assistant had to justify—and the reviewer had to check.

When it fails: checks and targeted changes

Cross-file fixes fail in predictable ways. Match the symptom to the correction rather than re-prompting broadly.

  • Assistant edits tests to pass. Symptom: diff touches spec files. Correction: revert test changes, re-prompt with "do not modify tests; the test is the specification."
  • Patch fixes the symptom in the wrong layer. Symptom: a default value added in the component instead of the normalizer. Correction: ask the assistant to name the layer that owns the shape, then constrain edits to that file.
  • Trace misses a dynamic call site. Symptom: build passes but a runtime path fails. Correction: search for the symbol by string, not just references; add dynamic imports or reflection targets to the input set.
  • Signature drift. Symptom: type-check errors in untouched files. Correction: reject the diff and require signature-preserving changes; widen types only at the boundary that truly needs it.
  • Over-broad refactor. Symptom: diff touches many files for a one-line bug. Correction: request a single-file minimal patch and explicitly list forbidden files.
  • Stale context. Symptom: the assistant references code you already changed. Correction: start a fresh conversation with the current files and the latest diff.

After each correction, re-run the narrow reproduction first. A patch that passes the full suite but fails the original test is not a fix.

Limits and when to stop prompting

An AI-powered coding assistant is strongest on local, traceable defects with a clear reproduction. It is weak where the bug depends on runtime state you haven't captured, concurrency, environment configuration, or behavior spread across services with no shared types. In those cases, spend the time building a better reproduction or a smaller failing test instead of iterating on prompts.

Stop and escalate when: the reproduction is flaky, the trace requires access you don't have, the minimal fix would change a public contract, or two review rounds produce diffs that touch unrelated files. At that point a human pairing session or a design discussion is cheaper than another prompt.

The repeatable core is simple: define the failing observable, supply the full data path, ask for a trace before a patch, require a minimal diff, and gate on the narrow test before the full build. That sequence—not any particular tool—is what keeps a cross-file fix from becoming a cross-file breakage.

Run the cross-file fix workflow

Act as a careful code-review assistant. Input: (1) the failing test command and its full output, (2) a list of files on the suspected data path with their current contents, (3) any type or schema contracts at module boundaries, (4) the public interfaces that must not change. Constraints: do not modify test files; do not change public signatures; prefer the smallest single-file change; if you cannot verify a call site, say so explicitly. Steps: first return a trace of where the symbol is produced, transformed, and consumed, with file and line references and the assumed shape at each point. Wait for my confirmation. Then return a unified diff of the minimal fix, one paragraph of rationale per changed file, and a bulleted list of unverified call sites. Output format: plain Markdown with a 'Trace' section, a 'Diff' code block, a 'Rationale' section, and an 'Unverified' section. Do not claim tests pass; I will run them and report back.

Continue in Studio

Further reading

Download desktop & mobile app

Access Ottermind anytime, anywhere.

Computer