一句话:提出 WIRL,用 LLM 智能体把粘贴代码中未解析变量按上下文自动替换,精确匹配精度达 91.7%。
复制-粘贴-修改是软件开发中普遍的实践,开发者常把来自 Stack Overflow、GitHub 或 LLM 的代码片段适配进本地代码库。其中的关键环节是代码“接线”(code wiring):即上下文感知地将粘贴代码中未解析的变量替换为周围上下文中合适的变量或表达式。现有方案依赖启发式规则或历史模板,难以有效利用上下文信息,而研究表明过半适配场景都依赖上下文。本文提出 WIRL,把代码接线建模为检索增强生成(RAG)式的填充任务,结合 LLM、定制工具包与编排模块来识别未解析变量、检索上下文并完成上下文感知替换;为兼顾效率与自主性,智能体采用混合策略:常见模式走确定性规则步骤,复杂情况由状态机引导的决策过程进行智能探索。在基于真实代码适配场景精心构建的高质量数据集上,WIRL 取得 91.7% 的精确匹配精度与 90.0% 的召回,分别比先进 LLM 高出 22.6 和 13.7 个百分点,比 IntelliJ IDEA 高出 54.3 和 49.9 个百分点,在变量依赖复杂或存在多个未解析变量时优势尤为明显。
Copy-paste-modify is a widespread and pragmatic practice in software development, where developers adapt reused code snippets, sourced from platforms such as Stack Overflow, GitHub, or LLM outputs, into their local codebase. A critical yet underexplored aspect of this adaptation is code wiring: the context-aware process of substituting unresolved variables in pasted code with suitable variables or expressions from the surrounding context. Existing solutions either rely on heuristic rules or historical templates, often failing to effectively utilize contextual information, despite studies showing that over half of adaptation cases are context-dependent. In this paper, we introduce WIRL, an LLM-based agent for code wiring framed as a Retrieval-Augmented Generation (RAG) infilling task. WIRL combines an LLM, a customized toolkit, and an orchestration module to identify unresolved variables, retrieve context, and perform context-aware substitutions. To balance efficiency and autonomy, the agent adopts a mixed strategy: deterministic rule-based steps for common patterns, and a state-machine-guided decision process for intelligent exploration. We evaluate WIRL on a carefully curated, high-quality dataset consisting of real-world code adaptation scenarios. Our approach achieves an exact match precision of 91.7% and a recall of 90.0%, outperforming advanced LLMs by 22.6 and 13.7 percentage points in precision and recall, respectively, and surpassing IntelliJ IDEA by 54.3 and 49.9 percentage points. These results underscore its practical utility, particularly in contexts with complex variable dependencies or multiple unresolved variables. We believe WIRL paves the way for more intelligent and context-aware developer assistance in modern IDEs.
DOI 原文 ·
@inproceedings{paperbot9315,
title = {Wired for Reuse: Automating Context-Aware Code Adaptation in IDEs via LLM-Based Agent},
author = {Taiming Wang and Yanjie Jiang and Chunhao Dong and Yuxia Zhang and Hui Liu},
booktitle = {ASE},
year = {2025},
doi = {10.1109/ase63991.2025.00023}
}