Function-Aware Fill-in-the-Middle as Mid-Training for Coding Agent Foundation Models

Yubo Wang, Jiarong Liang, Yuxuan Zhang, Xuye Liu, Cong Wei, Yuyu Zhang, Ping Nie, Wenhu ChenarXiv 2026

TL;DR

This paper introduces a dedicated "mid-training" stage for code Large Language Models (LLMs) to bridge the gap between standard pre-training and agentic post-training. By exploiting a structural analogy between ordinary function calls and an agent's tool-use loop, the authors use function-aware Fill-in-the-Middle (FIM) training to significantly boost software engineering agent performance while preventing the typical capability erosion seen in specialized models.

What Problem Does This Paper Address?

AI coding agents typically operate in a loop: they analyze context, take an action (like calling a tool or running a command), receive an observation (the tool's output), and then continue reasoning. While standard left-to-right pre-training excels at predicting the next token, it poorly prepares models to reason bidirectionally about how downstream code should adapt to external, unpredictable tool returns.

To fix this, researchers usually rely heavily on agentic post-training (Supervised Fine-Tuning or Reinforcement Learning on agent trajectories). However, this post-training is data-hungry and frequently suffers from "catastrophic forgetting," severely degrading the model's performance on standard, non-agent coding benchmarks and out-of-domain tool-use tasks. While random-span Fill-in-the-Middle (FIM) pre-training exists, its arbitrary boundaries lack the semantic structure and intermediate reasoning steps required to mirror an agent's think-then-act workflow.

Methods

The authors propose a novel Function-Aware FIM Mid-Training stage positioned right before agentic post-training. The core insight is that a function call site is structurally isomorphic to a coding agent step:

Figure 1: Left: A function call site and a single step of a coding agent are structurally similar, decomposing into the same four stages: context, call/action, return/observation, continuation. Middle: We exploit this analogy via function-aware FIM mid-training. A function B is selected from the program dependency graph using complexity ( ˆH) and inferability (ˆI) scores; the model is then mid-trained to fill in B’s body together with a CoT rationale, given the surrounding file as an FIM- formatted prompt. Right: Mid-training yields consistent gains across both Qwen2.5-Coder-Instruct (7B, 14B) and Qwen3 (8B) on SWE-Bench-Verified (solid bars) and SWE-Bench-Lite (hatched bars).

To exploit this analogy at scale without relying on expensive synthetic agent trajectories, the method extracts this structure directly from raw source code using three key steps:

  1. Program Dependency Graph (PDG) Analysis: The pipeline parses code repositories into Abstract Syntax Trees (ASTs) to map out how functions call and interact with one another.
  2. Complexity-Inferability Double Criterion: Instead of masking random text spans, the model targets entire function bodies. It selects targets using a balanced score of Complexity ($\hat{H}$, ensuring the function is challenging enough to learn from) and Inferability ($\hat{I}$, ensuring the surrounding file contains enough context clues to make the function body guessable).
  3. Chain-of-Thought (CoT) Augmentation: Before filling in the masked function body, a teacher model (Gemini-3-Flash) generates a step-by-step natural language rationale explaining how to implement it based only on the surrounding context. The model is trained to generate this rationale before generating the actual code, perfectly mirroring an agent's reasoning process.
Figure 2: Function-aware FIM target selection on a small calculator example. (a) Program dependency graph parsed from the AST: solid arrows are call edges Ecall, dashed lines are sibling edges Esib between same-class methods. (b) Stacked bars decompose the complexity score ˆH =0.40 (Eq. 1; LoC, CC, depth) and the inferability score ˆI =0.48 (Eq. 2; five context signals) for Calculator.total, yielding FIM≈0.22≥τ =0.08 (a hyperparameter; see Appendix B for the full list).

Datasets

  • Mid-Training Corpus: The authors curated a decontaminated dataset of 968 GitHub Python repositories, yielding roughly 400K FIM samples (~2.6B tokens). Crucially, the repository commits were time-locked to precede the creation of evaluation benchmarks to eliminate data leakage.
  • Evaluation Benchmarks:
  • In-domain Agent tasks: SWE-Bench-Verified and SWE-Bench-Lite.
  • Non-agent Coding: LiveCodeBench, OJBench, and FullStackBench-EN.
  • Out-of-Domain Agent/Tool-Use: Terminal-Bench 2.0, $\tau$-bench, and the Berkeley Function Calling Leaderboard (BFCL).

Key Insights and Ideas

  • The Isomorphism "Aha Moment": Realizing that the internet is already full of "agent-like" data structures disguised as standard function calls allows models to learn agentic reasoning implicitly from massive, open-source codebases.
  • Mitigating Capability Erosion: Agentic post-training typically acts as a destructive force on general capabilities (e.g., dropping Qwen2.5-Coder-14B's LiveCodeBench score by over 13 points). Mid-training acts as a powerful structural regularizer, preserving or recovering these foundational capabilities.
  • Fixing the Stopping Policy: Trajectory analysis revealed that mid-training fundamentally alters agent behavior. Instead of giving up prematurely with empty patches ("no-patch" failures), mid-trained models adopt an "iterate-and-verify" strategy, actively using bash tools to run tests and refine their code edits.

Key Results

When evaluated on the rigorous SWE-Bench framework, adding the FIM mid-training stage yielded consistent, cross-model improvements:

  • Qwen2.5-Coder-7B-Instruct: Improved SWE-Bench-Verified by +2.8% and SWE-Bench-Lite by +3.7%.
  • Qwen2.5-Coder-14B-Instruct: Gained +3.0% on Verified and +4.0% on Lite.
  • Qwen3-8B (with SWE-Lego): Achieved the highest jumps, with +3.2% on Verified and +5.4% on Lite.

More impressively, the mid-training stage successfully arrested capability decay on non-agent tasks. For the 14B model, adding mid-training restored +11.1% on LiveCodeBench, +2.4% on BFCL, and +3.9% on $\tau$-bench compared to post-training alone—despite the mid-training corpus containing absolutely zero tool-use or non-Python data.

Limitations and Future Work

  • Language Constraints: The current mid-training corpus and evaluation focus exclusively on Python. The authors note that expanding this framework to multi-lingual repositories (Java, C++, Rust) remains future work.
  • Monolithic Code: The target selection algorithm assumes well-structured, modular code. It is inherently less effective on monolithic scripts or unstructured notebooks where clear function boundaries do not exist.
  • Teacher Dependency: Generating high-quality CoT rationales for the FIM blocks currently relies on a proprietary frontier model (Gemini-3-Flash), though ablations show that self-generated rationales can still recover a majority of the gains.

Summary updated

BibTeX
@misc{wang2026function,
  author = {Yubo Wang and Jiarong Liang and Yuxuan Zhang and Xuye Liu and Cong Wei and Yuyu Zhang and Ping Nie and Wenhu Chen},
  title = {{Function-Aware Fill-in-the-Middle as Mid-Training for Coding Agent Foundation Models}},
  year = {2026},
  eprint = {2607.12463},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2607.12463},
}
Figure