arXiv:2608.29841cs.CLAug 2026

Manuscript Offprint

arXiv:2608.29841

SkillForge: Compositional Skill Synthesis with Verification-in-the-Loop for Generating Formally Verified Dafny Programs

Yanming Liu, Xinyue Peng, Jiannan Cao, Xinyi Wang, Jinbo Su

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.

Abstract

Generating formally verified programs from natural language remains challenging: existing approaches either produce code in a single pass without recourse when verification fails, or rely on open-ended agentic reasoning that is non-deterministic and opaque. We introduce SKILLFORGE, a framework that decomposes formal code synthesis into a library of atomic, reusable skills—each targeting a specific subtask such as specification inference, body synthesis, invariant generation, error diagnosis, or targeted repair—defined by a prompt template, tool binding, and decidable success criterion. A verification-driven harness orchestrates these skills: it submits candidates to the Dafny verifier, diagnoses failures into structured categories, deterministically routes to the appropriate repair skill, and iterates until formal correctness is proved or a budget is exhausted. On a curated benchmark of natural language–Dafny specification pairs, SKILLFORGE substantially outperforms both state-of-the-art agentic approaches (including ReAct-style agents, MCTS-based repair, and RL-guided verification) and traditional iterative baselines, while requiring fewer tokens and lower latency. Ablation studies confirm that every skill contributes measurably, and the harness converges rapidly with the majority of programs verified on the first attempt.

1 Introduction

Formal verification offers the strongest correctness guarantee available in software engineering: a proof, checked by machine, that a program meets its specification for all possible inputs (Wen et al., 2024). The Dafny language (Loughridge et al., 2024) exemplifies this paradigm by integrating preconditions, postconditions, loop invariants, and termination measures directly into the source code, enabling an automated verifier backed by an SMT solver to certify correctness at compile time. However, writing verification-grade code is prohibitively difficult even for experts: one must simultaneously produce an implementation that is algorithmically correct, a specification that is tight enough for the verifier to discharge, and auxiliary annotations (loop invariants, ghost state) that bridge the gap. Recent large language models (LLMs) can generate syntactically valid Dafny (Rasheed et al., 2025), yet the verification pass rate of naive generation remains low because these subtasks place fundamentally different demands on the model:

  • One-shot generation treats the problem as a single inference step. When verification fails, there is no mechanism for diagnosis or repair.

  • Naive iterative repair feeds the error message back without structured analysis, conflating specification gaps, missing invariants, and logic errors into one undifferentiated retry—leading to oscillation and wasted budget.

  • Agent-based approaches (Yang et al., 2024; Wang et al., 2024b; Yao et al., 2023) give the LLM open-ended control over its next action. While powerful for exploratory tasks, they suffer from non-determinism, opacity, and unbounded computation when applied to formal verification—a domain where the error space is finite and well-categorized.

  • Tree-search methods (Ren et al., 2024; Zhang et al., 2024) explore multiple repair candidates simultaneously but scale poorly with program complexity and lack the targeted repair strategies that formal verification errors afford.

We propose a different paradigm: skill-based harness engineering. Inspired by compositional skill systems in open-ended learning (Wang et al., 2024a; Qian et al., 2024; Chen et al., 2024) and recent advances in agent-designed skill libraries

(Zhou et al., 2026), we decompose the NL-to-verified-Dafny task into a library of atomic skills, each targeting a single well-defined subtask (specification inference, body synthesis, invariant generation, error diagnosis, code repair, spec strengthening). A harness—not an agent—orchestrates their execution with deterministic routing, bounded iteration, and full observability.

The key insight is that the Dafny verifier provides a formal oracle: its structured error output can be parsed into categories that deterministically select the next skill to invoke. This converts the open-ended “what should I do next?” problem into a closed, auditable routing decision. Unlike agentic systems that must reason about what tool to call at each step, or tree-search methods that explore exponentially many candidates, our harness exploits the structure of verification errors to make optimal routing decisions with zero reasoning overhead.

Consider a concrete example: given the instruction “compute the factorial of n using a loop,” a one-shot generator may produce a correct loop body but omit the loop invariant invariant r == fact(i) required by the verifier. A ReAct-style agent would reason about the error, potentially try multiple repair strategies, and waste turns on inapplicable fixes. SKILLFORGE instead: (1) runs the verifier, (2) diagnoses the specific failure as invariant_missing via DIAGNOSEERROR, (3) routes to the specialized GENINVARIANT skill, which synthesizes only the missing invariant. This targeted approach succeeds in 2 iterations where agentic methods require 3–5 turns with lower reliability.