旧代码片段会误导模型生成过时代码,影响代码补全质量。
When Retrieval Hurts Code Completion: A Diagnostic Study of Stale Repository Context
- 通过控制实验对比新旧代码上下文的影响。
- 旧上下文使模型生成过时代码比例上升88.2%和76.5%。
- 适合关注代码生成鲁棒性的开发者与研究者。
检索增强的代码生成依赖跨文件项目上下文,但检索到的代码片段可能来自过时的项目状态。本文针对五个Python仓库中17个生产级辅助函数签名变更的样本,开展受控诊断研究。在隐藏提交新鲜度和预期当前签名的前提下,比较仅当前、仅旧版、无检索及混合新旧检索四种条件下的表现。结果表明:在中性提示下,仅使用旧上下文时,Qwen2.5-Coder-7B-Instruct模型在15/17样本中引入旧版辅助函数引用,比仅用当前上下文提升88.2个百分点;gpt-4.1-mini在13/17样本中出现类似问题,提升76.5个百分点。无检索条件下虽无旧引用,但仅1/17样本生成正确代码。两模型在触发旧引用的样本上具有75.0%的Jaccard重合率。混合条件下加入有效当前证据可显著缓解旧上下文带来的错误。结论:检索上下文的时间有效性是代码RAG鲁棒性的重要诊断变量——过时上下文会主动诱导模型偏向旧项目状态,而非仅损失有效信息。
原文摘要 · Abstract (English)
Context: Retrieval-augmented code generation relies on cross-file repository context, but retrieved snippets may come from obsolete project states. Objectives: We study whether temporally stale repository snippets act as harmless noise or actively induce current-state-incompatible code. Methods: We conduct a controlled diagnostic study on a curated 17-sample set of production-helper signature changes from five Python repositories. For each sample, we compare current-only, stale-only, no-retrieval, and mixed current/stale retrieval conditions under prompts that hide commit freshness and expected current signatures. Results: Under neutralized prompts, stale-only retrieval induces stale helper references on 15/17 Qwen2.5-Coder-7B-Instruct samples and 13/17 gpt-4.1-mini samples, corresponding to 88.2 and 76.5 percentage-point increases over current-only retrieval. No retrieval produces zero stale references but only 1/17 passing completions. The two models share 75.0% Jaccard overlap among stale-triggering samples, and mixed conditions show that adding valid current evidence largely rescues stale-only failures. Conclusion: Temporal validity of retrieved repository context is a distinct diagnostic variable for Code RAG robustness: stale context can actively bias models toward obsolete repository state rather than merely removing useful evidence.
Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。