Balanced First, Real Second: Staged Training for the Rare Edits Code Models Get Wrong

Code language models are good at writing programs from scratch and much worse at changing programs that already exist. The worst failures are the large, structural changes: rewriting a function to meet a new requirement, reorganizing a block for efficiency, restructuring logic across many lines. This note argues that part of this failure is a class-imbalance problem hidden inside instruction tuning. Real commit histories are dominated by small, local edits, while deep refactorings are rare. A model tuned on that stream learns that "edit" usually means "change a little." We propose to borrow a two-phase recipe from imbalanced learning. First, train the model on a synthetic, execution-verified corpus in which every kind and size of edit appears equally often. Then adapt it briefly, with constrained updates, on real commits at their natural skewed frequencies.

The specific bet is about ordering, not only data. From Fake to Real showed that mixing synthetic samples into real data to rebalance it teaches the model to associate rare concepts with "looks synthetic." Staging the two sources removed that shortcut and improved worst-group accuracy by up to 20%. The strongest open code-editing recipe, NextCoder, does exactly the mixing that result warns against. We test whether staging, rather than mixing, is what lets a code model carry rare edit skills over to terse, real developer instructions.

Core Claim

Our first claim is that, for a fixed pool of synthetic and real editing examples and a fixed training budget, one ordering beats two alternatives. The ordering is a balanced synthetic phase followed by a short, update-constrained phase on naturally distributed real commits. The alternatives are single-stage mixing of the same examples and single-stage balanced resampling of the same examples. The staged model should do better on the rare, structurally large edit categories, and especially on terse "lazy" instructions phrased the way developers actually write them. Our second claim is about mechanism. The mixed model's weakness should show up as a measurable source shortcut: it performs rare edits well when the instruction resembles the synthetic generator's phrasing and falls back to minimal edits or verbatim copying when the instruction resembles a real commit message. The staged model should show a much smaller gap between the two phrasings.

The motivating evidence comes from four sources. Across 19 models, CodeEditorBench reports that the requirement-switch category averages an 11.18% pass rate, far below debugging (about 20%) and translation (about 30%). Can It Edit? shows that models degrade when instructions are lazy rather than descriptive. CoditT5 reports "naive copying," meaning the model returns its input unchanged, in up to 34% of comment updates for a baseline model. Exploring the Capabilities of LLMs for Code Change Related Tasks documents "change-blindness" and notes that LoRA-tuned models still struggle with high-level refactoring.

We read all four as symptoms of a single cause. The model has learned a strong prior toward small edits, and that prior overrides what the instruction actually asks for. In imbalanced classification this is the familiar collapse toward the majority class. Here the "classes" are kinds and sizes of edits.

Research Program

Edit strata. We first make the imbalance explicit by defining edit strata along two axes. The first axis is intent, following the four CodeEditorBench categories: debug, translate, polish, and requirement switch. The second axis is structural scope, measured by the syntax-tree difference between the code before and after the edit. Scope runs from a single-token change, to a single line, to a multi-line block, to a change spanning several functions. We then label a large, lightly filtered commit corpus with these strata. The corpus is built from CommitPackFT together with the EditPackFT and Commits2023FT corpora released with Can It Edit?. Intent labels come from a classifier we check on a hand-labeled sample, and scope labels are computed directly from the syntax trees. Measuring the natural stratum distribution is itself a contribution. The file contains no reported frequencies, and the claim that structural refactors form the tail is currently an assumption that this measurement will confirm or refute.

Balanced synthetic corpus. Next we build a synthetic corpus balanced across strata. Following the flaw-injection idea in NextCoder, we start from clean seed functions and have a strong model produce an instruction, a target edit, and unit tests for each stratum. We keep only pairs where the edited code passes the tests and, for polish, is measurably faster. Seeds are drawn from repositories disjoint from the sources behind CodeEditorBench, CanItEdit, and EditEval, and we deduplicate against those benchmarks.

Phase 1. We train on the balanced synthetic corpus with dense updates. The balanced-pretraining literature argues that this is when representations for rare concepts must form. Examples include the two-stage bug detector in On Distribution Shift in Learning-based Bug Detectors, synthetic balancing for glaucoma in DiffuPT, and balanced storm pretraining in USFP. The bug-detector paper plays two roles. It motivates the design: the two phases lifted real-world variable-misuse precision from 3.4% to 64.8%. It also constrains it: synthetic warm-up helped least on structurally complex bugs. That limitation is precisely our target regime, so our synthetic generator must produce multi-line, semantically meaningful edits rather than token-level mutations.

Phase 2. We then adapt on real commits at their natural distribution for a short budget. Updates in this phase are constrained with NextCoder's selective sparse update method (SeleKT), which updates only the most important ~5% of weights and pulls the rest back toward the starting point. The intent is that phase 2 teaches the model how real developers phrase requests and how often each kind of edit occurs, without erasing what phase 1 built. This division of labor answers an open tension in the file. NextCoder restricts updates everywhere to prevent forgetting, whereas the balanced-pretraining view says rare-class representations need deep updates. We allow deep updates only where they are useful, in phase 1, and restrict them in phase 2. The loss follows Can It Edit?: next-token prediction on the post-edit code only.

Backbone and budget. One open 7B-scale code model serves as the main backbone, with a 33B replication of the headline comparison if compute allows. Every variant sees the same set of examples and the same number of optimizer steps.

Baselines

The naive reference points are the base instruction-tuned model with no editing fine-tuning, and a synthetic-only model trained on the balanced corpus and never shown real commits. The synthetic-only model is the direct analogue of the bug detectors that collapsed to 3.4% precision in the real world. If it matches the staged model, real-data adaptation is unnecessary and the premise of the proposal fails.

The strongest existing method is NextCoder's recipe. It mixes synthetic and real commits one-to-one in a single stage, trained with SeleKT. We reimplement it on our backbone, using our synthetic corpus and the same real commits, the same step count, and the same hyperparameter-search budget. That way the only difference from our method is ordering and stratum balance. The file does not quote NextCoder's scores on CanItEdit, HumanEvalFix, or Aider; no reported result found on Lacuna for NextCoder's numeric scores. We therefore compare against our reimplementation, and against the paper's radar-chart figures only as a sanity check. For the single-stage commit tuning recipe EditCoder, the file reports only that EditCoder-33b outperforms GPT-3.5-Turbo on descriptive CanItEdit instructions and matches it on lazy ones, with no pass@1 value. On EditEval, the only reported numbers are GPT-4 at 68.56%, ChatGPT at 57.73%, and InstructCoder-tuned Code LLaMA 13B at 57.22% accuracy, all from InstructCoder. The 57.22% figure is the concrete open-model bar we must clear on that benchmark.

Ablations. The key ablation isolates ordering. We take the identical pooled examples and train in a single stage twice: once with the natural mixture, and once with the whole pool resampled so each stratum is equally frequent. A second ablation isolates the phase-2 constraint by running phase 2 with dense updates, standard LoRA, and SeleKT. A third ablation removes stratum balance from phase 1 while keeping the same synthetic volume. We count the proposal as beating NextCoder-style mixing only if it meets two conditions. It must improve pass@1 on CodeEditorBench_Plus requirement switch and on CanItEdit lazy instructions by more than the seed-to-seed spread across three seeds. It must also do so without losing ground on MMLU and GSM8K relative to the mixed model.

Why not just…?

Why not just prompt a frontier model with retrieved examples? CodeEditorBench already ran zero-shot, few-shot, and chain-of-thought prompting across 19 models, including GPT-4 and Gemini-Ultra, and requirement switch still averaged 11.18%. Exploring the Capabilities of LLMs for Code Change Related Tasks found that up to eight retrieved examples lost to LoRA fine-tuning. DivoT5 shows that a 220M edit-trained model beats ChatGPT prompted with few-shot examples. We still report GPT-4-class prompting as a ceiling reference, not as a competitor under matched compute.

Why not just mix synthetic and real data, as NextCoder does? This is the main threat, and we do not argue it away. The argument for staging is transferred from vision From Fake to Real, and an autoregressive code model conditioned on instructions may not form the same shortcut. The matched-data ordering ablation and the phrasing-swap probe described below settle the question directly. If mixing ties staging and shows no phrasing gap, we will report that the vision result does not carry over. The surviving contribution would then be the stratified synthetic data and the long-tail measurement.

Why not just oversample rare commits or use focal loss in one stage? Oversampling real data repeats the few tail commits that exist, so the model memorizes them rather than learning the category. Reweighting the loss during a single phase has the same problem and destabilizes training. More fundamentally, neither technique creates coverage of large structural edits that the real stream barely contains. The balanced-resampling ablation above is the fair version of this alternative, because it uses the same synthetic pool that staging uses. The two differ only in whether the imbalanced real phase comes last.

Why not just use LoRA to avoid forgetting? NextCoder reports that SeleKT beats standard LoRA, and LoRA-tuned models still struggle with high-level refactoring. We include LoRA as a phase-2 option precisely to test whether a cheaper constraint suffices once phase 1 has done the representational work. If it does, that addresses SeleKT's dense-gradient cost, which the file lists as an open limitation.

Proposed Experiments and Decisive Checks

Main evaluation. We evaluate on CodeEditorBench_Plus, the split built to reduce data leakage, reporting per-category pass rates. We also evaluate on CanItEdit, reporting pass@1 and ExcessCode separately for descriptive and lazy instructions, and on EditEval accuracy. For editing breadth we add HumanEvalFix and Aider, and for forgetting we add MMLU and GSM8K, mirroring NextCoder's own checks.

Stratum-level diagnostics. On our held-out real-commit test set, labeled by stratum, we report pass rates per stratum and naive-copy rate as defined in CoditT5. We also report an under-editing ratio: the syntax-tree diff size of the model's output divided by that of the reference edit, averaged within each stratum. If a small-edit prior is dragging the model toward minimal changes, this ratio will sit well below one on the large-scope strata. It should be near one if that prior has been corrected.

First decisive check: phrasing swap. For each held-out synthetic task, we write a second version of the instruction in the terse style of real commit messages, while keeping the tests the same. We then compare how much each model's pass rate drops between the two phrasings. The staged model should show a clearly smaller drop than the mixed model on the rare strata. If the drops are equal, the proposed mechanism is wrong regardless of headline scores.

Second decisive check: gains concentrated in the tail. Improvements from staging should appear mainly on requirement-switch and multi-line strata, not on token-level debugging. A uniform lift across all strata would suggest generic extra training rather than a correction of the small-edit bias.

Third check: interaction with the phase-2 constraint. Dense updates in phase 2 should raise head-stratum scores but erode tail-stratum gains and MMLU/GSM8K scores. SeleKT or LoRA should preserve both. This would give the first direct evidence on whether sparse updates and balanced representation learning conflict, or complement each other when assigned to different phases.