Ensuring that code faithfully reflects its documented intent is a fundamental challenge in software engineering, but the informal natural language specifications in real repositories cannot be automatically compiled into executable checks and run against implementations. We introduce behavioral probes: executable postconditions generated from natural language documentation, injected into programs, and checked during test execution to reveal where documented intent and implementation behavior align or diverge. We present SpecProbe, a repository-aware pipeline that generates behavioral probes and classifies each as non-executable, matched, or mismatched. A structured four-field schema (import, pre-ghost, post-ghost, condition) makes generated postconditions injectable and compilable in real repositories; reasoning supervised fine-tuning on ∼1.5K automatically curated examples enables a compact 7B model to generate probes without continued API access. On Defects4J, SpecProbe's 7B model achieves GPT-4o-comparable semantic consistency (Sem@1) at 141× lower inference cost. The pipeline reveals 80 of 726 historical bugs, and for 95% of them the original failing-test stack trace omits the method checked by the probe. On Python icontract generation, SpecProbe surpasses GPT-5-mini and GPT-o3, demonstrating transfer to a different language and contract format. These results show that behavioral probes complement failing-test stack traces with diagnostic evidence grounded in the selected method's documented intent.
Figures & tables
Figure 2. Overall Workflow.
Figure 3. LLM Prompt Example.
Figure 4. Reasoning, Postcondition, and Injection Example.
Model
Size
#Ex.
Sem@1
Sem@5
Sem@10
Syn@1
Syn@5
Syn@10
rBugD
#Bugs
GPT-4o
≳ 200B
-
44.0%
77.7%
86.2%
82.1%
96.2%
96.8%
11.8%
86 /726
GPT-4*
≳ 200B
-
32.0%
57.0%
66.0%
65.0%
86.0%
89.0%
6.7%
35 /525
Qwen2.5-Coder
7B
-
12.5%
42.3%
60.8%
34.9%
80.5%
91.2%
5.0%
36 /726
StarChat*
16B
-
11.0%
38.0%
55.0%
25.0%
68.0%
83.0%
3.6%
19 /525
Qwen2.5-Coder
32B
-
32.6%
63.4%
73.5%
74.7%
94.5%
96.0%
10.6%
77 /726
DeepSeek-R1-Distill
32B
800K
39.7%
71.6%
80.2%
67.3%
94.0%
95.9%
12.7%
92 /726
Table 1. Model Performance Comparison. Bold font is used to mark the top open-source-based models. Ex. denotes the number of examples used for fine-tuning. Asterisk (*) models ( Endres et al., 2024 ) filtered 525 bugs from Defects4J while ours use 726.
Figure 5. Comparison of Sem@1 among initial Qwen model, non-reasoning SFT model from Qwen2.5-Coder-7B, and ours, in which "Qwen2.5-Coder-7B-SFT" and "ours" use the same number of training data.
Figure 6. Trend of average tokens and Sem@1 while the rank of reasoning context length increased, from rank 1 shortest to 10 longest.
Figure 7. An example of long reasoning.
Model
Sem@1
Syn@1
nl2postcond prompts
32.0%
65.0%
Our prompts
36.6%
70.5%
Table 2. Comparison on GPT-4 w/ different prompt construction schemes.
Figure 8. An example of GPT-4 generating post-condition with our prompt scheme ( Chart bug-21).
We introduce fidelity probes: natural-language questions generated from a reference artifact with code-derived ground-truth answers, answered from a candidate specification. The fraction of agreeing probes, which we call the fidelity, decomposes into contradiction and coverage-gap rates that drive targeted spec edits to convergence. On a 15-program, roughly 12k-line COBOL benchmark (AWS CardDemo), we raise frozen-test specification fidelity from 0.63 to 0.94 over eight iterations, with the plateau location predicted by a two-state Markov fixed point F† from just four iterations of rate data. Probes come from an LLM reading the code or from a static-analysis pipeline over its control-flow, data-flow, and system-dependence graphs, with a tunable mixture. A probe-resampling protocol with a frozen held-out set gives a Hoeffding-bounded overfitting discriminant; our measured train/test gap stays more than an order of magnitude below this envelope. Three graph-grounded mixtures lift fidelity by +16 to +30 points; cross-distribution evaluation shows the LLM and symbolic channels are empirically complementary. A cross-family generator sweep on five independent LLM lineages (Anthropic, DeepSeek, Google, Alibaba, OpenAI) confirms the convergence behaviour is not tied to any single model family: three of five non-Claude generators produce trajectories consistent with the Markov fixed-point prediction, and the frozen-test protocol actively falsifies the two generators whose probe distributions drift across iterations. The method applies to any pair of artifacts that are supposed to describe the same behaviour.
Large language models (LLMs) can generate code from natural language, but the extent to which they capture intended program behavior remains unclear. Executable behavioral specifications, defined via preconditions and postconditions, provide a concrete means to assess such understanding. However, existing work on specification generation is constrained in evaluation methodology, task settings, and specification expressiveness. We introduce CodeSpecBench, a benchmark for executable behavioral specification generation under an execution-based evaluation protocol. CodeSpecBench supports both function-level and repository-level tasks and encodes specifications as executable Python functions. Constructed from diverse real-world codebases, it enables a realistic assessment of both correctness (accepting valid behaviors) and completeness (rejecting invalid behaviors). Evaluating 15 state-of-the-art LLMs on CodeSpecBench, we observe a sharp performance degradation on repository-level tasks, where the best model attains only a 20.2% pass rate. We further find that specification generation is substantially more challenging than code generation, indicating that strong coding performance does not necessarily reflect deep understanding of intended program semantics. Our data and code are available at https://github.com/SparksofAGI/CodeSpecBench.
Zaoyu Chen, Jianbo Dai, Boyu Zhu +6
The Hong Kong Polytechnic University · University of Edinburgh · UCL +5
Automatically generating formal specifications could reduce the effort needed to improve program correctness, but in practice, this is still challenging. Many developers avoid writing contracts by hand, which limits the use of automated verification tools. Recent large language models (LLMs) can generate specifications from code, but these specifications often fail in terms of verification. The reason is syntax errors, overly strict constraints, or mismatches with program behavior. We present SpecPylot, a Python tool that synthesizes executable specifications for Python programs as icontract annotations and checks them using crosshair's symbolic execution. The tool relies on LLMs to propose candidate contracts and uses crosshair to validate them. When crosshair finds a concrete counterexample, SpecPylot updates only the generated contracts and leaves the program itself untouched. In addition, the tool can produce coverage-driven pytest stubs and keep detailed execution artifacts that are useful during debugging. Overall, the evaluation indicates that SpecPylot is able to generate crosshair-compatible contracts for most programs, but it also highlights the practical limits introduced by bounded symbolic exploration and differences in LLM behavior.