SpecForge: Recursive Self-Improvement for Coding Agents via Behavioral Specifications

Yutong Cheng, Haifeng Chen, Peng Gao, Wei ChengNeurIPS 2026

TL;DR

How do you make a coding agent smarter without touching a single weight in its neural network? SpecForge teaches agents to iteratively rewrite their own natural-language documentation based on unit test failures—and uses a clever tree search to stop them from accidentally breaking working code while fixing bugs elsewhere. The result is crisp, reusable codebase knowledge that helps agents translate repositories into new languages and build new features better than human-written requirement docs.

What Problem Does This Paper Address?

Autonomous coding agents are only as good as their understanding of the codebase they inhabit. While we usually write documentation for human eyes, agents need behavioral specifications: precise, actionable natural-language descriptions of inputs, outputs, and edge cases.

Because these specifications are just text, an agent can theoretically improve itself in a loop: read the specs (without seeing the original source code), try to reconstruct the code, run unit tests, and rewrite the specs wherever tests fail. This is recursive self-improvement in context space—no expensive fine-tuning required.

There is just one catch: software isn't a bag of independent functions. When an agent edits the specification for Function A, it often breaks Function B. The authors call this output coupling, and it happens in two ways:

  1. Dependency coupling: If Function A calls Function B, trying to fix A's spec while B is still broken gives the agent misleading error signals.
  2. Generation coupling: Because the LLM reads the entire bundle of specifications at once to generate code, tweaking one description shifts the prompt context and subtly alters how completely unrelated functions get generated.

Left unchecked, iterative refinement turns into a frustrating game of whack-a-mole, oscillating endlessly instead of converging on a working codebase.

Methods

Instead of marching down a single, fragile chain of edits, SpecForge frames specification writing as a multi-frontier tree search, where each node is a complete bundle of specifications for the codebase.

Figure 2: Overview of SPECFORGE. Each iteration resolves three decisions: where to search, what to refine, and how to refine. ❶Pareto-guided state selection samples from an archive of non- dominated specification states with complementary strengths. ❷Dependency-constrained entity selection only refines entities whose callees are already solved. ❸Error-diversified expansion generates multiple children from different subsets of failing tests. Strong children are fed back into both the archive and the entity-selection policy.

Every iteration of the search answers three questions:

  • Where to search (Pareto-Guided State Selection): Rather than scoring specification bundles with a single average pass rate, SpecForge keeps an archive of Pareto-optimal states—bundles that aren't strictly worse than any other across all functions. One state might nail the low-level math utilities while another nails the high-level API logic. Keeping both alive prevents premature commitment to a dead-end trajectory.
  • What to refine (Dependency-Constrained Entity Selection): Using the program's call graph, SpecForge locks functions until all of their callees pass 100% of their tests (enforcing a strict bottom-up order). Among unlocked candidates, a discounted Upper Confidence Bound (D-UCB) bandit algorithm allocates compute to functions showing the highest "gap-closing rate"—separating genuinely fixable functions from stubborn ones.
  • How to refine (Error-Diversified Expansion): Dumping 20 failing unit tests into an LLM prompt often dilutes the diagnostic signal with cascading or redundant errors. SpecForge instead samples different subsets of failing tests to generate multiple candidate rewrites in parallel, hedging its bets so at least one branch gets a clean, noiseless clue.

Datasets

Because SpecForge keeps model weights frozen, there is no training dataset—optimization happens per codebase using execution-verified unit tests automatically generated from the reference code (with developer-written ground-truth tests held out strictly for final evaluation).

  • DevEval+: A revised Python benchmark of 20 modules, 247 functions/classes, and 760 tests (averaging a call-graph depth of 4.2), built by expanding DevEval with complex GitHub repos in finance, biology, and signal processing.
  • DevEval+-Java: A newly constructed 16-module JUnit 5 mirror of DevEval+ (208 entities, 680 tests) for testing Python-to-Java translation.
  • SWE-Dev: 27 feature-implementation tasks across 10 open-source Python packages, testing whether specs help implement brand-new functionality.

Key Insights and Ideas

  • Readable docs make terrible specs: Static documentation tools written for humans (like RepoAgent) actually hurt cross-language code translation compared to just handing the LLM raw source code (33.5% vs. 39.7% solve rate). Human-friendly prose leaves ambiguities that code generators interpret all too literally.
  • Less error context can be more: Mixing noisy or cascading test failures with informative ones actively corrupts an LLM's diagnostic reasoning. Subsampling errors across tree branches reliably unlocks better fixes.
  • Self-improved specs beat human PRDs: On SWE-Dev, giving an agent SpecForge's optimized specifications of the existing codebase helped it build new features better than giving it the benchmark's official Product Requirements Documents (PRDs). The loop distills genuinely reusable architectural knowledge rather than overfitting to tests.

Key Results

  • Code Reconstruction (DevEval+): SpecForge outperforms static docs, monolithic prompt optimizers (MIPROv2, GEPA, ACE), commercial agents (Cursor, Claude Code), and prior spec-search baselines across all five tested LLMs. It hits a 94.9% entity solve rate with Claude-4.5-Sonnet (90.9% on GPT-4o), beating the strongest baseline (DocSearch) by 5.7 percentage points on average.
  • Ablations: Removing reverse-topological ordering causes the steepest drop (-18.6% solve rate), followed by removing the D-UCB bandit (-10.4%), error diversity (-9.5%), and the Pareto archive (-5.2%).
  • Downstream Transfer: In Python-to-Java translation, SpecForge specs boost GPT-4o's solve rate from 39.7% (source-only) to 65.7%, and reach 76.2% when combined with the Python source. On SWE-Dev new-feature tasks, SpecForge achieves 74.5% Pass@1 on easy and 51.7% on hard tasks, beating the reference PRDs by +18.8 and +7.1 points.

Limitations and Future Work

The main text of the paper does not include a dedicated limitations section. However, a few scope boundaries are apparent from the setup: SpecForge relies on having an existing reference implementation from which passing unit tests can be automatically generated and executed to ground the search loop, and its cross-language evaluation excludes modules tied to libraries without direct target-language equivalents.

Summary updated

Abstract

Coding agents based on frozen language models can improve their performance by refining the external context they rely on, without updating model weights. In this paper, we study recursive self-improvement (RSI) through behavioral specifications: natural-language code documentation that describes the behavior of program entities and guides agents in generating correct code. We formulate a self-improvement loop in which an agent generates code from its current specifications, evaluates the generated code using execution-verified tests, and refines the specifications based on observed failures. The improved specifications then guide subsequent code generation, allowing the agent to progressively improve its understanding of the codebase. However, recursive refinement is challenging because program entities are interdependent. Improving the specification of one entity can inadvertently degrade the correctness of others, a phenomenon we call output coupling. This creates a whack-a-mole effect in which local improvements cause regressions elsewhere, preventing consistent progress. We introduce SpecForge, a multi-frontier tree search framework that addresses this challenge through three mechanisms: (i) Pareto-guided state selection preserves complementary specification states and prevents premature commitment; (ii) dependency-constrained entity selection prioritizes callee-before-caller refinement; and (iii) error-diversified expansion explores multiple refinements using different subsets of test failures. On DevEval+, SpecForge achieves the best performance across five backbone models, including a 94.9% solve rate with Claude-4.5-Sonnet and a 5.7-percentage-point average improvement over the strongest baseline. The improved specifications also make the agent better at tasks outside the loop: they transfer to cross-language translation and to new-feature implementation, where they outperform the benchmark-provided requirement documents. The code and data are available at https://github.com/ccsnow127/SpecForge.
BibTeX
@inproceedings{cheng2026specforge,
  author = {Yutong Cheng and Haifeng Chen and Peng Gao and Wei Cheng},
  title = {{SpecForge: Recursive Self-Improvement for Coding Agents via Behavioral Specifications}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2026},
  url = {https://openreview.net/forum?id=yMLqe1DovZ},
}
Figure