Bidirectional Context for Code Infilling and Repair

This direction is no longer present in the current build. Its last version stays readable and citable here.

Version 1 of 119 papers26 conceptsfrozen 2026-10-04

Standard Large Language Models (LLMs) are typically trained as auto-regressive predictors: they ingest a sequence of tokens and predict the next one, moving strictly from left to right. While this "sequential bottleneck" works for prose, it fundamentally clashes with how software is written and maintained. Developers rarely write code in a single linear pass; instead, they edit, refactor, and repair code within existing files where the "future" context (the code following the cursor) is just as important as the "past" (the code preceding it).

Research across program repair, code completion, and diffusion models suggests that moving beyond unidirectional generation toward bidirectional context infilling is essential for capturing the structural and logical constraints of software.

The Infilling Paradigm

Infilling treats code generation not as a completion task, but as a "fill-in-the-blank" problem. By providing a model with both a prefix (code before the target area) and a suffix (code after), the model can generate patches that are syntactically and logically consistent with the entire file.

As Chunqiu Steven Xia and Lingming Zhang demonstrate in Practical Program Repair in the Era of Large Pre-trained Language Models, this bidirectional approach significantly improves compilation rates and fix accuracy. When a model knows the structural boundaries of the code it is replacing, it is less likely to generate "hallucinated" variables or mismatched brackets that would break the trailing logic. This is particularly vital for syntax error correction; early work by Sahil Bhatia and Rishabh Singh on SYNFIX noted that ignoring the code following an error site discards valuable clues about the intended structure.

Capturing Structural Hierarchy

Code is inherently hierarchical, often represented as an Abstract Syntax Tree (AST). Unidirectional models struggle to maintain this hierarchy because they only see one branch of the tree at a time. Bidirectional context allows models to "see" the surrounding nodes, providing a clearer picture of the program's shape.

To exploit this, Linhai Li and colleagues proposed Contextual AST Serialization (CAS) in Using Context and Hierarchy Features to Enhance Code Completion in Code Modification Scenarios. By combining different traversals of the AST, they provide the model with both preceding and succeeding structural nodes. This prevents "prefix bias"—the tendency of models to over-rely on the code immediately before the cursor while ignoring essential constraints that appear later in the script.

Diffusion Models and Parallel Reasoning

While auto-regressive models can be adapted for infilling (e.g., by rearranging the input to put the suffix before the target gap), Discrete Diffusion Language Models (dLLMs) offer a more native solution. Diffusion models generate text by denoising a sequence of tokens in parallel, allowing them to resolve dependencies in any order.

Recent breakthroughs like LLaDA2.0 by Junbo Zhao and Stable-DiffCoder by Chenghao Fan show that this non-linear generation is particularly effective for code. Because diffusion models are not restricted to a left-to-right sequence, they can naturally leverage bidirectional context to solve complex reasoning tasks and resolve bugs where the necessary clues appear much later in the file. This makes them inherently better suited for "global" structural constraints than traditional sequential models.

Benchmarking Real-World Workflows

The shift toward bidirectional context has exposed limitations in how we evaluate AI coders. Standard benchmarks like HumanEval often focus on "interview-style" problems where a model writes a function from scratch. However, real-world development involves editing existing codebases.

Newer benchmarks are designed to capture this nuance:

  • DS-1000: Yuhang Lai and Luke Zettlemoyer created this benchmark to test models on realistic data science tasks. They found that models perform significantly better when they can utilize infilling, suggesting that bidirectional context is a more accurate reflection of how AI assistants are actually used.
  • SimCopilot: Abhinav Jain and Christopher Jermaine argue that models which dominate standard benchmarks often fail in "Copilot-style" scenarios. Their work highlights that generation success in complex codebases depends heavily on the model's ability to manage long-distance dependencies and integrate new code into a pre-existing suffix.

By prioritizing the surrounding environment over simple sequence completion, bidirectional context allows models to move from being "code writers" to "code editors," aligning AI capabilities with the iterative nature of software engineering.

Papers in this version

History & citationVersion 1 · 2026-10-04

No longer present in the current build · Permanent link

View frozen version 1

Cite this direction

Latest version (a living page):

@misc{lacuna_bidirectional_context_for_code_infilling_7d7bf0e0,
  title = {Bidirectional Context for Code Infilling and Repair},
  author = {{Lacuna}},
  howpublished = {Lacuna research direction},
  year = {2026},
  url = {https://lacuna.tiptreesystems.com/direction/bidirectional-context-for-code-infilling-and-repair/rdr_7d7bf0e0d88344f097c5dedf94d518e1},
  note = {Latest version of a living research direction; cite a version for a fixed text. Accessed 2026-11-25},
}

This exact version (version 1, frozen text):

@misc{lacuna_bidirectional_context_for_code_infilling_7d7bf0e0_v1,
  title = {Bidirectional Context for Code Infilling and Repair},
  author = {{Lacuna}},
  howpublished = {Lacuna research direction},
  year = {2026},
  url = {https://lacuna.tiptreesystems.com/direction/bidirectional-context-for-code-infilling-and-repair/rdr_7d7bf0e0d88344f097c5dedf94d518e1/v/1},
  note = {Version 1, frozen 2026-10-04. Accessed 2026-11-25},
}