Reliable AI Coding for Cross-File and Long-Horizon Tasks
This article explains how to make AI coding tools complete cross-file and long-horizon tasks reliably. It covers defining acceptance criteria, managing context, making small reviewable changes, verifying results, evaluating real tasks, and setting permission and review levels based on risk.
1. Why Long-Horizon Coding Is Difficult: Keeping Multi-Turn Changes Aligned with One Goal
Completing a single function and making a requirement change in an existing codebase are two different kinds of work. The former can often be done from a function signature, local code, and input/output examples. The latter requires understanding call relationships, data constraints, existing behavior, and delivery requirements, while keeping that information consistent across modification, verification, and correction.
The “long” in a long-horizon task is not only about processing time or context length. It also means that an earlier decision can change the conditions for later steps.
Take “change the default sorting of an order list” as an example. The requirement may look like a single sorting rule, but it can involve server-side queries, pagination boundaries, front-end default options, and existing callers. If only the query statement is changed, the first page may look correct, but later pages may contain duplicates or omissions when the sorting key is not unique. If the pagination interface is also refactored along the way, a local requirement may turn into a compatibility change.
The key measure of such a delivery is not how much code was changed, but whether the change boundary can be explained and whether common paths and boundary behavior can be verified.
2. Define Acceptance Conditions Before Asking the Model to Make a Plan
The task description given to a model should separate the goal, constraints, and acceptance evidence. For the sorting change, the goal could be: “When no sorting method is specified, display results in descending order by creation time.” Constraints could include: “Do not change the behavior of explicit sorting” and “Do not add response fields.” Acceptance conditions should cover cases such as: “The result order is stable when creation times are identical,” “Pagination does not return the same record twice,” and “Existing callers do not need to modify their requests.”
Do not treat vague statements such as “the implementation should be reasonable” or “avoid affecting other features” as directly executable requirements. They may help communicate intent, but they cannot replace concrete checks.
Before making changes, ask the model to list questions that need confirmation and where evidence for each question should come from. For example: Is the default sorting passed in by the page, or filled in by the server? Does the existing pagination rely on offsets, or on the last record of the previous page? If the repository does not contain enough evidence to determine whether the old behavior is part of a compatibility contract, it should be treated as an open question rather than decided by the model on its own. The team can first approve the behavior change and then approve the implementation scope, avoiding a situation where requirements are discussed again only during code review.
3. Provide Context as Needed, Not as a Long-Context Dump
Being able to read more content does not mean the model will correctly find the key constraints. Putting a large number of repository files into the context at once can cause outdated documentation, similar implementations, and unrelated modules to compete for attention.
A safer approach is to start from the entry point, core logic, and existing tests, then add material as questions arise. If you need to confirm where a parameter is produced, trace the callers. If you need to confirm whether the order is stable, read the sorting implementation and pagination tests. Every newly added piece of material should answer a specific question.
When a task spans multiple turns, it is useful to maintain a short, checkable record. It should include confirmed behavior, assumptions still to be verified, the scope of changes, and check results. In particular, distinguish between “what was seen in the code,” “what was inferred from it,” and “what was actually confirmed by running something.” For example, seeing a secondary sorting key in the implementation does not mean all query paths use it. Only by checking the specific call chain and corresponding tests can you confirm which path the verification covers. The next turn should continue from the latest code and verification results, rather than treating the previous turn’s text summary as the source of truth.
4. Make Small Changes So Each Stage Produces a Reviewable Result
Cross-file tasks can be sliced by behavior rather than mechanically split by file. Using the sorting example again, first add a test that fixes the expected behavior, then modify the default sorting logic, and finally check whether pagination and explicit sorting are affected.
A behavior slice may span multiple files, but it should have a clear review boundary. Reviewers should be able to identify which part implements the new behavior, which part proves that the old behavior still holds, and which changes are only cleanup that can be deferred.
During verification, first run the checks most directly related to the change to locate problems quickly. Then run regression tests and static checks according to the impact scope. Front-end changes should also be checked through actual interaction, such as page behavior during loading, empty results, and request failures; a static preview cannot replace this.
If a test fails, first determine whether the problem is in the implementation, the test data, or the environment. Do not loosen assertions just to obtain a passing result. If a dependency service is unavailable, the delivery record should clearly state which checks were not run, what prerequisites are missing, and who needs to complete the follow-up verification. “Not run” and “passed” must be recorded separately.
5. Evaluate Capabilities with Real Tasks, Not a Single Demo
Comprehensive coding evaluations, front-end blind tests, and long-context tests do not measure the same thing. Comprehensive evaluations help provide an initial understanding of coding and reasoning ability. Front-end blind tests can reflect visible results or interaction preferences. Long-context tests focus on whether a model can find and use relevant information from a large amount of material.
None of them directly answers the question a team really cares about: whether the model can complete maintainable changes in its own codebase with an acceptable review cost.
Teams can draw samples from historical tasks that have clear acceptance criteria. These samples should cover local fixes, cross-module changes, interaction adjustments, and refactoring with compatibility constraints. During evaluation, use the same code starting point, permissions, and verification conditions. Provide only the requirement material that was available at the time, and do not provide historical patches in advance.
In addition to the acceptance pass rate, record the number of human interventions, review time, reasons for rework, and types of failed checks. A solution generated quickly but requiring multiple rounds of correction may not save time compared with a slower solution that makes fewer changes and provides clearer evidence.
6. Set Permissions and Human Review Depth Based on Task Risk
Tasks that are good candidates for early adoption of AI coding usually meet three conditions: the change boundary is clear, verification can be repeated, and failure is easy to reverse. Examples include adding tests for existing business logic or adjusting page display within existing interface constraints.
Tasks involving data migration, permission rules, public interface compatibility, or production releases should retain independent approval. Even if the model can generate complete code and explanations, business judgments that cannot be confirmed by automated checks should not be left for the model to decide on its own.
In practice, start by allowing the model to perform read-only analysis. Then allow it to generate patches in an isolated environment and run agreed checks. Maintainers review the diff and decide whether to merge it.
The delivery material does not need to be long, but it should answer four questions: what was changed, why it was changed this way, what evidence was used for verification, and what risks remain unresolved. When a team can consistently obtain such delivery results, gradually expanding the task scope is easier to evaluate and easier to control rework costs than pursuing full automation from the beginning.
