Code Correctness Signals in LLM Hidden States: Pre-Generation Probing and Repair Geometry

📄 arXiv: 2606.14530v1 📥 PDF

作者: Carlo Di Cicco

分类: cs.LG

发布日期: 2026-06-12

备注: 12 pages, 8 tables. Code, data, and analysis scripts available at https://github.com/CarloDiCicco/ReasoningLab


💡 一句话要点

通过隐藏状态探测代码正确性并修复失败尝试

🎯 匹配领域: 支柱九:具身大模型 (Embodied Foundation Models)

关键词: 大型语言模型 代码生成 代码修复 隐藏状态 线性解码 统计分析 残差化技术

📋 核心要点

  1. 现有方法在识别大型语言模型生成代码的正确性方面存在挑战,尤其是在生成之前和修复失败尝试时的可解释性不足。
  2. 本研究提出了一种基于隐藏状态的探测方法,通过线性解码和残差化技术来分析代码的正确性和修复过程。
  3. 实验结果表明,模型首次尝试的代码正确性可以有效解码,且修复尝试的隐藏状态转变具有显著的统计特征,提供了新的理解视角。

📝 摘要(中文)

大型语言模型在其隐藏状态中编码了丰富的信息。本研究探讨了在生成代码之前及修复失败尝试时,Qwen3-4B-Instruct-2507的隐藏状态中是否可以识别代码的正确性,基于444个LiveCodeBench任务进行分析。研究发现,模型首次尝试代码的正确性可以从提示最终隐藏状态中线性解码,且在去除提示长度的影响后,仍然保持较高的解码性能。此外,在236个清理后的案例中,模型从失败尝试到修复的隐藏状态转变具有统计显著性,表明修复成功与修复上下文相关,而非单独的修复理解特征。该研究在方法论和实证上均有贡献,诚实地报告了负面结果与正面结果。

🔬 方法详解

问题定义:本研究旨在解决大型语言模型在生成代码时,如何有效识别代码的正确性及其修复过程中的可解释性问题。现有方法在这方面的表现不佳,尤其是在处理失败尝试时缺乏明确的信号。

核心思路:论文的核心思路是利用模型的隐藏状态,通过线性解码技术提取代码的正确性信息,并分析修复过程中的状态变化,以此来理解模型的行为和决策过程。

技术框架:整体架构包括两个主要模块:首先是对提示最终隐藏状态的线性解码,评估代码的正确性;其次是分析从失败尝试到修复的隐藏状态转变,利用统计方法验证其显著性。

关键创新:最重要的技术创新在于引入了残差化技术,以消除提示长度对隐藏状态的影响,从而更准确地评估代码的正确性和修复过程的有效性。这一方法与传统的直接分析方法有本质区别。

关键设计:在实验中,使用了嵌套交叉验证选择探测层,并在分析中采用了分割半数测试与标签洗牌的对比,确保结果的可靠性和有效性。

📊 实验亮点

实验结果显示,模型首次尝试代码的正确性可以从提示最终隐藏状态中线性解码,AUC达到0.931,去除提示长度影响后仍保持0.911,显著高于基线0.754。此外,修复尝试的隐藏状态转变具有统计显著性,表明修复成功与修复上下文相关。

🎯 应用场景

该研究的潜在应用领域包括自动代码生成、代码修复工具以及编程教育等。通过提高大型语言模型在代码生成和修复过程中的可解释性,可以帮助开发者更好地理解模型的决策过程,从而提升编程效率和代码质量。未来,该方法可能对智能编程助手和自动化开发工具的设计产生深远影响。

📄 摘要(原文)

Large language models encode rich information in their hidden states. This work asks whether code correctness is legible in the hidden states of Qwen3-4B-Instruct-2507, before it generates and as it repairs a failed attempt, studied on 444 LiveCodeBench tasks. It reports two findings connected by a single confound-control tool: residualization. First, the correctness of the model's first-attempt code is linearly decodable from the prompt-final hidden state, with a leakage-free held-out AUC of 0.931 +/- 0.008 across 50 outer splits. After the linear effect of prompt length is removed from each hidden state dimension, the probe still reaches 0.911 +/- 0.010, well above a prompt-length baseline of 0.754 +/- 0.014. Second, on 236 cleaned cases where the model attempts to repair a failed first attempt, the hidden state shift from the failing attempt to its repair carries a statistically detectable contrastive direction, significant on both a magnitude and a split-half test against label-shuffled nulls. This direction does not survive a conditional residualization against repair-context covariates that differ between successful and failed repairs, marking it as a correlate of repair success driven by the repair context rather than an isolated repair-comprehension feature. The probe layer is selected by nested cross-validation, and the same residualization approach that upholds the pre-generation correctness result overturns the repair-direction interpretation. The contribution is as much methodological as empirical: a diagnostic honest enough to report a negative result alongside a positive one.