用大模型注意力机制找出代码变更中高风险片段,辅助开发者高效审查。
A Preliminary Study on Explaining Risk of Code Changes using LLM-Based Prediction Models

- 基于大模型注意力权重定位代码变更中的高风险部分
- 顶2个代码块可覆盖53.85%的真实故障行,平均减少73.72%审查量
- 无需额外计算,可直接集成到代码评审界面,适合工程团队使用
机器学习和人工智能模型的预测结果常因缺乏可解释性而受到质疑。在即时缺陷预测场景中,尚未充分研究如何通过非规则式静态分析,精确定位代码变更(diff)中风险集中的小范围区域。本文利用基于大语言模型的差异风险评分(DRS)模型的注意力权重,识别模型关注的代码片段。我们将令牌级注意力聚合为可读的代码单元(行、补丁块、文件),并向开发者展示风险最高的前K个单元,作为代码审查中的轻量级引导。通过专家标注的真实故障变更数据评估,当突出显示前2个补丁块时,可覆盖53.85%的导致实际宕机的代码行,同时平均仅需审查26.28%的变更行。由于注意力机制在标准推理过程中自动生成,该方法可扩展至大规模开发流程,并以极低延迟集成到代码审查界面。
原文摘要 · Abstract (English)
Predictions by machine learning (ML) and artificial intelligence (AI) models are often received skeptically unless they are paired with intelligible explanations. In the context of just-in-time defect prediction, highlighting small portions of a software change (diff) -- beyond rule-based lints -- where risk may be concentrated has not yet been extensively investigated. In this work, we leverage attention weights from an LLM-based Diff Risk Score (DRS) model to highlight parts of a diff that the model focuses on when predicting risk. We aggregate token-level attention into interpretable code units (lines, hunks, and files), and present the top-K units to developers as a lightweight form of guidance during code review. We evaluate our approach using expert-labeled changes that have caused real outages. Results show that the highlighted snippets cover expert-labeled outage-causing change lines 53.85% of the time when highlighting the top-2 hunks, while requiring developers to review 26.28% of the changed lines on average. Because attention is produced during standard model inference, the approach is scalable for large development workflows and can be surfaced in the code review UI with low additional latency.
Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。