Token-level defect feedback and alternating optimization for coupled code generation and defect detection with large language models
Abstract Large language models write code fluently, yet the programs they produce carry defect rates that ordinary functional evaluation seldom exposes, and detection systems that could catch these faults act too late to shape the generation that created them. This paper argues that generation and detection should be optimized together rather than in sequence, and it makes that argument concrete through one specific coupling: the detector’s node-level risk estimates are mapped back onto decoder positions and used to reweight the generation loss token by token, while a composite reward built from test outcomes and defect probability supplies a second, sampled gradient. Generator and detector are updated in alternation under a bounded schedule; failed candidates re-enter the detector’s training stream, and twelve mutation operators widen its exposure to underrepresented fault families. Using Qwen2.5-Coder-7B-Instruct as the generator and a gated graph neural network as the detector, and averaging five independent seeds, the coupled method lowered the defect rate of test-passing code from 0.216 ± 0.008 to 0.128 ± 0.008 while raising pass@10 from 0.724 ± 0.008 to 0.761 ± 0.008. Every defect rate reported here is scored by a reference detector frozen outside the loop, so the improvement cannot be an artefact of a critic grading its own pupil. Detection F1 rose from 0.627 ± 0.011 to 0.681 ± 0.011, although that gain confounds the coupling with the larger training stream the coupled detector receives. Ablation isolates token-level blame assignment as the component doing most of the work, and a controlled error-injection study quantifies how far detector mistakes propagate into generation. The evidence is confined to function-level Python and C, and concurrency faults remain resistant, so the claims made here are deliberately bounded to that setting; code, configurations and per-seed records accompany the submission so the boundary can be tested directly.
Authors
- Wenzeng Shan
Institutions
- Jiangxi College of Applied Technology (CN)
Publication Details
- Journal
- Scientific Reports
- Published
- 2026-09-19
- DOI
- https://doi.org/10.1038/s41598-026-71524-z
- Primary Topic
- Software Testing and Debugging Techniques
- Type
- article
- Field-Weighted Citation Impact
- 0.00