Can Large Language Models Detect Their Own Code-Generation Errors? A Study of Self-Detection and Self-Correction
Abstract
Large Language Models (LLMs) have shown remarkable code generation skills, but creating software that is accurate and consistently executable is still a major difficulty. Pre-trained models frequently produce code with minor logical flaws, inappropriate library or API usage, and syntax problems. A possible substitute for human-driven debugging is the new concept of self-correction, in which an LLM iteratively improves its generated code via natural-language feedback. However, models frequently have trouble accurately determining whether their outputs are valid in the absence of an external oracle, compiler, or execution-based feedback. This can lead to either a total inability to make significant progress or circular or harmful repair procedures, such as hyper-correction, where initially correct code is needlessly altered and worsened. We provide an approach that separates self-detection from correction in order to lay the groundwork for autonomous code debugging. We look into two important pillars: (i) Mechanistic assessment of internal signals, examining the model's implicit "knowledge" of its faults using a second-order confidence model from decision neuroscience. We examine the Post-Answer Newline (PANL) token, which stores internal activations that increase confidence without regard to token log-probabilities. (ii) Using algorithmic reinforcement learning (RL) to coordinate self-correction in 1B-scale models with limited resources. We use CoCoS (Confident Collaborative Self-correction), which uses an accumulated reward trajectory and unit test pass ratios to optimise a multi-turn Markov Decision Process (MDP) with a progressive, fine-grained reward function. Our empirical analysis produces a number of important discoveries. First, we verify that second-order PANL signals outperform explicit verbalised reflection and token log-probabilities in terms of code soundness prediction. Second, even in cases where behavioural signals are unsuccessful, PANL activations are able to accurately anticipate restoreability—that is, whatever code faults the model has the implicit knowledge to correct in later rounds. Third, training with the CoCoS RL aim yields significant improvements, but small language models (SLMs) struggle to self-correct out-of-the-box because of short edit distance on future rounds. In particular, 1B-scale models trained with CoCoS show a relatively low hyper-correction rate (1.8% change from correct to incorrect) and improve by 35.8% on the MBPP and 27.7% on HumanEval.