Buried Conventions: Depth-Robust Code Quality for Compact Repository Models

Code models are usually graded on whether their output passes unit tests. Real software teams also care whether the code follows the repository's conventions: its naming style, its error-handling idiom, how it splits work into helper functions, and its memory and speed budgets. Most of these conventions are never written down next to the task. They are shown by the code in other files, and in a long repository prompt those files sit in the middle of the context. Long-context models reliably under-use the middle of their input; this is the "lost-in-the-middle" effect. We propose to measure how much of the quality gap in repository-level code generation comes from this positional loss. We then train a compact 7B code model so that its adherence to conventions stays flat no matter where in the context the convention-bearing evidence appears, without giving up functional correctness.

The bet is specific. For conventions that are stated explicitly, simply restating them right before generation should recover most of the lost adherence, and we expect a cheap prompt fix to win there. For conventions that are only demonstrated implicitly across many files, or that must be satisfied several at once, restating requires first extracting them, and the extraction step itself is a long-context task. We hypothesize that this is where preference training of a specialized model pays off. The proposal is designed so that either outcome is informative.

Core Claim

Our claim is that a large share of non-functional failures in repository-scale generation is position-dependent. Adherence to a convention falls when the files that establish it are placed at middle depths of a long prompt, even when the model can answer questions about that convention correctly. We further claim that iso-functional, depth-randomized preference training removes most of this dependence for implicit conventions, where prompt-level restating does not. Iso-functional pairs are two completions that both pass the tests but differ in convention adherence. Depth-randomized means the evidence for the convention appears at varying positions in the context.

Three lines of evidence motivate this claim. RACE (Beyond Correctness) scores code on readability, maintainability, correctness and efficiency. It finds that instruction following "approaches zero when five or more simultaneous requirements are imposed," and that models degrade sharply when forced into non-idiomatic conventions such as camelCase in Python. All of RACE's constraints sit in short, local prompts. The long-context literature (Lost in the Middle; Serial Position Effects of Large Language Models) charts a U-shaped use of context, but only for factual retrieval and question answering. The AGENTS.md study (Evaluating AGENTS.md) finds that repository context files do not improve resolution rates and raise cost by 20 to 23%. It never varies where those files sit, so it cannot tell a positional failure from a content failure. No paper in our search crosses these: none asks whether code quality, as opposed to fact recall, decays with depth.

Research Program

Benchmark construction. The first component is a controlled benchmark, which we call DepthQ. Each item takes a function- or class-level task with tests from the pool RACE builds on (HumanEval+, MBPP+, ClassEval) and embeds it in a real repository context. The repositories are drawn from CrossCodeEval (CrossCodeEval) and COLA-132K (aiXcoder-7B-v2), padded to 16k, 32k and 64k tokens.

We rewrite a subset of the context files to carry a convention. That convention is either stated explicitly in a style-guide file or demonstrated implicitly by consistent usage across three to six files. The conventions are chosen to fight the model's priors, following RACE's finding that such conventions expose memorization. Examples include camelCase in Python, a mandated result-object error pattern, a required decomposition into named helpers, and an explicit memory bound. We then place the convention-bearing files at controlled relative depths from 0 to 1, and vary the number of simultaneous conventions from one to six.

Adherence is checked automatically:

  • AST-based checks cover naming and decomposition, in the style of RACE.
  • Maintainability Index and cyclomatic complexity are measured against a target.
  • RACE's normalized execution-time and memory index covers efficiency.
  • A style distance in the spirit of HCCS's Style Adherence Score (Beyond Pass@k) uses a profile computed from the repository rather than from a developer's history.

Correctness is pass@1 on the original tests.

Separating retrieval from application. For every item we also pose a probe question, such as "what naming convention does this repository use for helper functions?", asked at the same depth. This separates failure to retrieve the convention from failure to apply a convention the model did retrieve. NoFunEval's "generative paradox" (NoFunEval) warns that generating and discriminating can come apart in code models, and the probe is how we test for that split here.

Training. The second component trains a compact model. We start from a 7B code model, and run separately from aiXcoder-7B-v2, which was already trained to use distant cross-file context for API calls. For each training context we sample several completions, keep only those that pass the tests, and form iso-functional preference pairs that differ in convention adherence. This pairing is borrowed from HCCS-SFT, which used it to raise quality on GPT-4o while losing only about 1% correctness. The convention-bearing files are placed at depths drawn uniformly at random.

The model is trained with direct preference optimization plus a supervised regularization term, as in CodeLutra (CodeLutra). Direct preference optimization is a method that raises the likelihood of the preferred completion relative to the rejected one. The supervised term guards against probability collapse.

How this differs from prior work. aiXcoder-7B-v2 pairs completions that used distant API definitions against ones that ignored them, and scores only exact match. Our pairs hold correctness fixed and vary only distant non-functional adherence. HCCS-SFT works on single files with no notion of position. CodeDPO (CodeDPO) ranks correct solutions by runtime, but only on short algorithmic prompts.

Outcome measure. Our primary outcome is depth sensitivity: the gap between the best and the worst adherence rate across the depth bins, computed at a fixed number of conventions and a fixed context length. We report it alongside mean adherence and pass@1, because a model that is uniformly bad is flat but useless.

Baselines

Base model as-is. The naive reference is the base 7B model run with the full repository context and no changes. This gives the raw U-curve, if one exists, and the floor for every other comparison.

Restate-at-end. This is the strongest training-free competitor, and the one we take most seriously. The convention-bearing content is repeated immediately before the task. For explicit conventions this means copying the style guide; for implicit ones it means copying the convention-bearing files verbatim, within the token budget. It is the coding analogue of RAL-WRITER's retrieve-and-restate (Lost-in-the-Middle in Long-Text Generation), except that we give it oracle knowledge of which files carry the convention. A non-oracle variant instead selects files with BM25 against the task, the method CrossCodeEval found competitive for cross-file retrieval. No result has been reported on code non-functional adherence for either variant; the closest number is RAL-WRITER's consistency score of 55.28 on Qwen2.5-14B for text synthesis. We count the trained model as beating this baseline only if it matches or exceeds the non-oracle restate variant in mean adherence on implicit conventions, while also having lower depth sensitivity.

Extract-then-generate. The model first writes a short list of the repository's conventions and then generates conditioned on that list. This follows the extraction-generation alignment of Aligning Extraction and Generation for Robust Retrieval-Augmented Generation. It is the natural fix for implicit conventions, and it directly tests our claim that the extraction step itself loses middle content.

Positional contrastive decoding. This decoding method (Mitigating Posterior Salience Attenuation) subtracts a "near-sighted" set of next-token scores from the normal ones. The near-sighted scores come from over-rotating the low-frequency parts of the rotary position encoding, the scheme that encodes each token's position by rotating its query and key vectors. The authors report raising Llama-3-8B-262k from 72.0% to 79.0% on InfiniteBench 8k retrieval, and RULER 16k variable-tracking F1 from 64.40 to 69.04. We apply it to the same base model with its mixing weight β tuned on a held-out split. It is the strongest decoding-level answer to positional decay.

Other trained models. We include aiXcoder-7B-v2 unchanged. Its reported up-to-19.7% exact-match gain on COLA-132K tells us whether long-context API training already carries over to conventions. We also include one frontier API model, to test whether scale alone flattens the curve.

Ablations. Two ablations isolate the key ingredient:

  • The same preference pairs with conventions always placed at the end (no depth randomization) isolates the role of position.
  • The same depth-randomized data with pass/fail pairs instead of iso-functional pairs isolates the correctness-preserving design.

Fairness controls. All trained variants receive the same number of preference pairs, drawn from the same repositories, with the same optimizer budget and three seeds. All training-free baselines get the same hyperparameter-search budget on the same held-out split. Repositories used in training never appear at evaluation time.

Why not just…?

Why not just restate the conventions at the end of the prompt? For explicit conventions we expect this to work. If it brings mean adherence in the worst depth bin to within a few points of the best bin, we will report it as the practical answer for that case. The open question is implicit conventions. Restating them requires knowing which files carry them; the oracle variant shows the ceiling and the BM25 variant shows the realistic case. RACE's complexity wall also exists in short prompts, so for four or more joint conventions restating may fix position but not application. The retrieval probe will reveal this, as items where the probe is correct but adherence is wrong. If non-oracle restating matches the trained model on implicit conventions, the training claim fails and we will say so.

Why not just use positional contrastive decoding? It needs no training and has demonstrated gains on retrieval tasks. However, its gains are reported on single-token retrieval, while convention adherence is a property spread over a whole generated function. Its contrast also amplifies distant tokens at every step, which may harm local syntax. We will measure both its adherence and its compile and pass rate. A model with flat adherence but dropping pass@1 does not count as a win.

Why not just run a linter or formatter after generation? A formatter can rename identifiers, but it cannot decompose a function into the repository's helper structure, choose its error-handling idiom, or meet a memory bound. We will report what share of each convention family a post-hoc tool fixes. Convention families it fully fixes will be excluded from the headline claim.

Why not just use a bigger model? RACE shows that frontier models also collapse on multi-constraint following. AutoTriton (AutoTriton) and CodeLutra show 8B specialized models beating GPT-4-class models on narrow domains. Still, positional robustness has never been compared across scales, so the frontier run is a genuine test rather than a strawman.

Decisive Experiments

Is there a positional effect? The first check is the depth curve on DepthQ, broken out by explicit versus implicit conventions. If adherence is flat across depth for the base model, the positional hypothesis is wrong. The project then becomes a multi-constraint training study, and positional framing is dropped.

Is the failure retrieval or application? The second check is the joint distribution of probe accuracy and adherence at middle depths. A large share of items where the probe is right but adherence is wrong would mean application is the bottleneck, which favors training over prompt fixes.

Does training flatten the curve? The third check compares the trained model against non-oracle restate-at-end and extract-then-generate on implicit conventions with three or more joint conventions. The bar is lower depth sensitivity and higher mean adherence at a pass@1 loss of at most about 1%, matching what HCCS-SFT reports at short context. The no-randomization ablation should then show that depth randomization, not just more preference data, produces the flattening.

Does it transfer to real repositories? We evaluate on two external sets:

  • CTXBENCH, using its developer-committed AGENTS.md files, where developer context files gave a non-significant +2.4%.
  • The refactoring split of SWE Atlas (SWE Atlas), using its maintainability and dead-artifact rubrics.

We also check NoFunEval and HumanEval+ pass rates to make sure specialization has not degraded general coding ability. For depth-dependent non-functional adherence itself, we found no reported result on Lacuna, so DepthQ numbers will be the first.