用多智能体框架让大模型生成更安全的异常处理代码
Towards Exception Safety Code Generation with Intermediate Representation Agents Framework
- 五类智能体协作分析代码,精准定位异常漏洞
- 异常处理准确率提升37%,代码健壮性提高38%
- 适合关注代码安全性的开发者和自动化测试团队
大型语言模型在生成代码时常缺乏稳健的异常处理,导致程序易出错。本文提出Seeker,一种基于中间表示(IR)的多智能体框架,通过分解异常处理为扫描、检测、猎手、排序和处理五个专业智能体,协同分析代码、识别脆弱段落、检索最佳实践并注入鲁棒的异常处理逻辑。我们还构建了来自官方文档、技术实践和真实代码的通用异常枚举(CEE)知识库,统一异常处理策略。框架引入深度检索增强生成(Deep RAG),高效遍历异常继承层级,将搜索开销降低93%且提升相关异常识别准确率。在15个开源Java项目及多个基准测试上评估,Seeker优于现有基线,异常处理精度最高提升37%,整体代码健壮性提升38%(经专家评审)。其在真实问题修复任务(SWE bench)中达到28%成功率,显著缩小与人类开发者差距(前代方法仅19%)。该框架在保持功能正确性的同时主动处理错误,为生成更安全的代码提供了可推广的实用方案。
原文摘要 · Abstract (English)
Large Language Models (LLMs) often struggle with robust exception handling in generated code, leading to fragile programs that are prone to runtime errors. We propose Seeker, a novel multi-agent framework that enforces exception safety in LLM generated code through an Intermediate Representation (IR) approach. Seeker decomposes exception handling into five specialized agents: Scanner, Detector, Predator, Ranker, and Handler that collaboratively analyze code, detect fragile segments, retrieve best practice exception strategies, and inject robust handling code. We also introduce Common Exception Enumeration (CEE), a comprehensive knowledge base derived from official documentation, technical practices, and real world code, to standardize exception handling strategies. Seeker also incorporates a Deep Retrieval-Augmented Generation (Deep RAG) algorithm to efficiently navigate the exception inheritance hierarchy, cutting down search overhead by 93% while improving accuracy in identifying relevant exceptions. We evaluate Seeker on 15 open source Java projects and multiple benchmarks. Seeker outperforms state of the art baselines, improving exception handling precision by up to 37% and overall code robustness by 38% as measured by expert code review. It significantly closes the gap between LLM and human developers in exception management, achieving a 28% success rate on real world issue fixes (SWE bench) versus 19% by prior methods. Our framework preserves functional correctness of code while proactively handling errors, demonstrating a practical, generalizable solution for safer code generation. In this paper, we discuss the novelty of using intermediate representation and multi-agent collaboration for exception handling, and outline how Seeker can be extended to other programming languages and complex software engineering tasks, aligning LLM-generated code with industrial standard.
Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。