Function-Aware Fill-in-the-Middle as Mid-Training for Coding Agent Foundation Models
arXiv · HuggingFace · ▲104
Abstract (verbatim)
Coding agents must integrate external tool returns into ongoing reasoning - a capability that standard left-to-right pretraining on code exposes only in its forward direction. We observe that the action-observation-continuation loop of a coding agent is structurally isomorphic to a function call site, where a caller binds arguments, a callee returns a value computed elsewhere, and downstream code consumes that value. This conditioning structure exists at internet scale in ordinary code. We exploit it through function-aware fill-in-the-middle (FIM) mid-training: a self-supervised objective that masks functions selected via program dependency graph analysis and a complexity-inferability double criterion. We mid-train Qwen2.5-Coder-Instruct (7B/14B) and Qwen3-8B on a 2.6B-token decontaminated corpus drawn from 968 GitHub repositories, then apply existing agentic post-training pipelines. Mid-training improves SWE-Bench-Verified by +2.8/+3.0 at 7B/14B and by +3.2 on Qwen3-8B; SWE-Bench-Lite gains are +3.7/+4.0/+5.4 on the same models. The improvement holds across two post-training pipelines (R2E-Gym, SWE-Smith) and on a non-Qwen2.5 base (Qwen3-8B with SWE-Lego). Beyond in-domain gains, mid-training also mitigates the capability erosion that agentic post-training otherwise inflicts on non-agent coding (e.g., LiveCodeBench) and non-coding tool-use benchmarks (tau-bench, BFCL): although the mid-training corpus contains Python code only, the function-call inductive bias survives post-training and yields consistent gains.
Background
Background Analysis
1. Technical Context
Coding agents (e.g., AI tools for software engineering) have transitioned from research demos to real-world deployment (e.g., fixing bugs or automating development). A core challenge for these systems is integrating external tool outputs (e.g., API responses) into subsequent reasoning. For example, after querying a code repository, the agent must generate code based on that result. However, current methods rely heavily on "post-training" with synthetic trajectories (simulating human problem-solving), while base models (e.g., Qwen) are not optimized for this "conditional structure" (history-action-observation-continuation loop) during initial pretraining.
2. Previous Limitations
Traditional pretraining uses left-to-right token prediction or random span filling (FIM) but suffers from three flaws:
- Arbitrary Boundaries: Randomly masked code snippets often truncate expressions or statements, failing to capture function-level dependencies (e.g., a function call might involve multiple files).
- Lack of Reasoning Supervision: Models fill masks directly without intermediate reasoning (e.g., "think-then-act" logic), mismatching how agents operate.
- Diluted Signals: Random FIM, as part of pretraining, gets overwhelmed by unrelated data, making its structural knowledge ineffective for downstream tasks.
3. Proposed Solution
The paper introduces "function-aware fill-in-the-middle (FIM) mid-training":
- Target Alignment: Treats function calls as analogues to an agent’s "action-observation-continuation" loop (e.g., function arguments = action, return values = observation, downstream code = continuation).
- Precise Masking: Selects function-level targets via program dependency graph analysis, combined with a "complexity-inferability" criterion (ensuring masks are neither too simple nor incomprehensible).
- Embedded Reasoning: Inserts chain-of-thought (CoT) rationales into masked spans, training models to "reason before generating code."
- Timing Focus: Applies this stage between pretraining and post-training to preserve structured knowledge for agent tasks.
4. Key Differences
Compared to prior work, this approach:
- Maps Functions to Agents: Uses function calls (ubiquitous in code) as training targets, directly aligning with agent needs.
- Dual Criteria for Selection: Chooses masks via program analysis and complexity checks, avoiding randomness.
- Preserves Reasoning: Embeds CoT into masks, teaching "think-then-act" logic rather than direct code generation.
- Cross-Task Validation: Demonstrates gains not only in coding tasks (e.g., SWE-Bench) but also in non-coding tasks (e.g., LiveCodeBench), proving generalization.
This method improves agent performance and robustness by leveraging existing structural patterns in code without extra data.






