NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

From Completion to Editing: Unlocking Context-Aware Code Infilling via Search-and-Replace Instruction Tuning

Jiajun Zhang, Zeyu Cui, Jiaxi Yang, Lei Zhang, Yuheng Jing, Zeyao Ma, Tianyi Bai, Zilei Wang, Qiang Liu, Liang Wang, Binyuan Hui, Junyang Lin

TL;DR

Traditional code infilling (Fill-in-the-Middle) relies on insecure base models and fails completely when the surrounding code contains bugs. This paper introduces Search-and-Replace Infilling (SRI), a single-pass framework that brings the safety and error-correcting flexibility of chat agents to real-time code completion without the prohibitive latency overhead. By fine-tuning chat models on structured diffs, the resulting SRI-Coder series matches or exceeds the completion performance of base models while maintaining robust safety alignments.

What Problem Does This Paper Address?

Modern code autocomplete tools (like GitHub Copilot) rely heavily on the Fill-in-the-Middle (FIM) paradigm. FIM works by taking the code before the cursor (prefix) and after the cursor (suffix) to predict the missing code in between. While highly effective, FIM suffers from two major flaws:

  1. The Optimal Context Assumption: FIM assumes the surrounding code is completely error-free. If a developer has a typo or a logical bug in the prefix, a standard FIM model cannot fix it; it blindly builds on top of the flaw, generating broken code.
  2. Inherent Security Risks: FIM typically runs on unaligned "Base" LLMs because directly prompting safety-aligned "Chat" models for raw text continuation tasks causes massive performance degradation—up to a 59% drop in completion capabilities. This leaves autocomplete tools highly vulnerable to context-injection attacks.

While multi-step agentic workflows can dynamically edit and verify code, they are far too slow for the instantaneous "Tab-to-complete" experience developers expect. The authors set out to bridge this gap, creating a fast, secure, and context-aware infilling method.

Performance degradation in Chat LLMs

Methods

The authors propose Search-and-Replace Infilling (SRI), which reframes static code completion as an intelligent micro-editing task.

Instead of performing raw text continuation, the SRI framework inserts a unique identifier (/* MIDDLE CODE TO COMPLETE */) at the cursor location. The model is then trained to generate a structured block inspired by conflict resolution markers:

  • SEARCH Block: The model replicates the target code region that needs modification. This acts as a visual Chain-of-Thought (CoT), forcing the model to explicitly identify and validate the context before writing new code.
  • REPLACE Block: The model provides the corrected and completed code segment.

This structured format allows the model to simultaneously fill in the missing code and dynamically correct any pre-existing bugs in the surrounding context. The final output can easily be parsed and applied directly via IDE tools or standard patches (e.g., git apply).

SRI Workflow Overview

Datasets

To train and evaluate their approach, the authors curated several key datasets:

  • SRI-200K: A high-quality dataset synthesized from The Stack v2. The authors used tree-sitter to parse abstract syntax trees (ASTs) and extract structural logic blocks (functions, loops, if-else branches) across 50+ programming languages to serve as infilling targets. A 20K subset was used for primary fine-tuning.
  • Glaive-Code-Assistant: 60K instruction pairs were mixed into the training data to ensure the model retained general conversational and instruction-following capabilities.
  • CrossCodeEval-Flex: A new benchmark introduced by the authors to test a model's resilience to messy code. It modifies the standard CrossCodeEval dataset by intentionally injecting syntactical or logical noise into the 5-line window surrounding the missing code.

Key Insights and Ideas

  • Overcoming Conversational Distribution Shift: Chat models perform poorly on standard FIM because conversational templates conflict with raw text continuation. SRI harmonizes completion tasks with the instruction-following priors of Chat LLMs by using a structured diff format.
  • Search as a Grounding Mechanism: The explicit SEARCH phase acts as a lightweight reasoning step. By forcing the model to copy the context first, it prevents the model from hallucinating or blindly building on top of broken code.
  • Preserving General Coding Competencies: Traditional FIM fine-tuning often degrades a model's broader problem-solving capabilities. Because SRI aligns closely with the diff-formatted data naturally found in pre-training corpora (like Git commits), it preserves the model's general coding skills.
  • Scaling Laws Favor SRI: The performance advantage of SRI over traditional FIM grows as the model size increases. Larger models are significantly better at mastering the structured search-and-replace format.

Key Results

  • Surpassing Base Models: With minimal fine-tuning (just 20k samples), SRI-Coder-32B completely recovered the chat-induced performance drop, outperforming its base counterpart (Qwen2.5-Coder-32B) across standard benchmarks.
  • Robustness to Contextual Noise: On the noisy CrossCodeEval-Flex benchmark, standard FIM models scored a flat 0.0 Exact Match (EM) because they are architecturally frozen and cannot alter the noisy context. In contrast, SRI-Coder-32B achieved a 33.0 EM score by successfully fixing the surrounding bugs while completing the code.
  • Plugging the Security Gap: On the SAL benchmark, commercial baselines like GitHub Copilot exhibit attack success rates exceeding 98%. SRI-enabled models leverage their chat backbones to slash this vulnerability drastically, with DeepSeek-V3-Instruct (SRI) dropping the attack success rate to just 5.5%.
  • Zero Latency Overhead: Despite generating structured search/replace blocks, the inference speed of SRI remains virtually identical to standard FIM, making it perfectly viable for real-time IDE deployment.
Ablation study comparing SRI against NL-FIM variants

Limitations and Future Work

Despite the strong results, the authors highlight a few limitations:

  • Offline Evaluation Constraints: The current evaluation relies entirely on static offline benchmarks, which cannot fully capture the interactive, multi-turn nature of real-world software development.
  • Capacity Threshold: Smaller models (e.g., 0.5B parameters) struggle with the structural complexity of the SRI task, showing less pronounced improvements than their larger counterparts.

Future Directions: The team plans to explore knowledge distillation techniques to effectively transfer SRI capabilities into highly compact models. They also aim to integrate the SRI-Coder series into functional IDE extensions to gather real-world developer telemetry and qualitative feedback.

Summary updated

BibTeX
@inproceedings{zhang2026from,
  author = {Jiajun Zhang and Zeyu Cui and Jiaxi Yang and Lei Zhang and Yuheng Jing and Zeyao Ma and Tianyi Bai and Zilei Wang and Qiang Liu and Liang Wang and Binyuan Hui and Junyang Lin},
  title = {{From Completion to Editing: Unlocking Context-Aware Code Infilling via Search-and-Replace Instruction Tuning}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2026},
  doi = {10.18653/v1/2026.acl-long.361},
}
Figure