编程 5 个 Agent 修 C++ 构建错误:Fixer 一次给 3 个候选,Verifier 编译不过就回滚

2026-09-21 00:04:30

5 个 Agent 修 C++ 构建错误:Fixer 一次给 3 个候选,Verifier 编译不过就回滚

大型 C++ 项目的构建错误修复不是单步任务:错误检测与分类 → 代码上下文分析 → 修复策略制定 → 代码修改 → 验证迭代。让一个 Agent 同时掌握全部技能,上下文会迅速膨胀到难以维护。更实际的做法是拆成五个专业化角色,职责边界清晰,共用一个 Shared State(Build Log / File Cache / Fix History)。这套系统基于 Claude Agent SDK 搭建,实践数据是 LLM 驱动方案可自动解决约 63% 的编译错误。

五个角色各管一段

Orchestrator(总指挥):解析构建日志、按相似度把错误分组、分配任务、协调工作流、决定继续迭代还是放弃。约束是 max_iterations=3timeout_per_error=300s。出口决策三条:error_count==0 → SUCCESS;iteration>=max → PARTIAL_SUCCESS;new_errors_introduced → ROLLBACK。核心循环:

解析日志 → 分组 → 对每组错误迭代:
Error Analyzer 分析 (is_fatal 直接 FAILED)
Context Analyzer 收集上下文
Fixer 生成修复
apply_fixes → Verifier 验证
verification.success → SUCCESS
new_errors → rollback → REGRESSION
否则 iteration += 1,用 remaining_errors 继续

is_fatal 是一次快速失败:命中就直接 FAILED,不必再走后面的上下文收集与修复流程。

Error Analyzer:解析 gcc/clang/msvc 输出,提取 file_pathline_numbercolumnerror_codeerror_typeseveritycontext_snippetsuggested_fixes。分类维度为 syntax(missing_semicolon / unmatched_braces / invalid_tokens)、linker(undefined_reference / multiple_definition)、type(implicit_conversion / incompatible_types / missing_include)、template(template_instantiation_failed / deduction_failed)。解析用 Claude 的结构化 JSON 输出保证一致性,同时做可修复性评估,提前把不可自动修复的错误挑出来。实现用 model=claude-3-5-sonnet-20241022max_tokens=2000,产出 fixableis_fatal 字段。

Context Analyzer:分层收集上下文。primary 是 error_line 前后各 10 行、完整函数体、完整类定义;secondary 是所有 #include、相关类型/函数定义、调用点;tertiary 是 CMakeLists.txt / Makefile 与编译器 flag。analysis_depth 分 shallow / medium / deep,deep 会追传递依赖,代价是慢。文件读取走 file_cache 避免重复 IO,语义分析交给 Claude:代码意图、最佳实践(比如这里应该用 std::make_unique)、约束和相关模式。

Fixer Agent:基于错误分析与上下文生成修复,一次给多个候选(num_candidates=3),按 likelihood_of_success / code_quality / minimal_changes 排序,返回前 3 个。

fix_strategies 按错误类型分级:

syntax_errors  → add_missing_semicolon / balance_braces / fix_typos
type_errors    → add_explicit_cast / include_header / fix_template_arguments
linker_errors  → add_implementation / fix_linkage_specification / add_library_dependency

code_style 约束为 follow_project_conventions、use_modern_cpp(C++17/20)、preserve_formatting、add_comments minimal。

关键设计是用 tools 做结构化工具调用,而不是让模型吐自由文本:

apply_code_change(file_path, start_line, end_line, replacement, explanation)
add_include(file_path, header, position="top" | "after_includes")

每个修复附带 riskadditional_suggestionsmodel=claude-3-5-sonnet-20241022max_tokens=4000

Verifier Agent:应用修复 → 重建 → 分析 → 决定是否接受。步骤是 apply_fixgit apply --cachedbackup=true)→ 增量编译(只编译受影响文件)→ 分析(check_new_errors / check_warnings / check_unchanged_errors)→ 决策。接受条件 accept_if: original_error_fixed and no_new_errors and no_regression;拒绝条件 reject_if: new_errors_introduced or compilation_time_exceededrollback_strategyautomatic=true,触发条件是 new_errors 或 test_failures。返回 VerificationResult(success, remaining_errors, reason)

协作协议与共享状态

Agent 之间用 AgentMessage dataclass 通信:from_agent / to_agent / message_type / payload / correlation_id / timestampSharedState 维护 build_logerrorsfixesfile_cacheerror_fix_mapadd_fix 记录修复并建立 error→fix 映射,get_fix_history 可以查某个错误的全部修复尝试。

两个实际场景

场景一:src/parser.cpp:42std::unique_ptr 不是 std 成员。Error Analyzer 判为 type_error、可修复,根因是缺 ``;Context Analyzer 给出 context_window、includes、missing: 和语义意图;Fixer 候选 1 加 #include (无风险),候选 2 改用 std::make_unique;Verifier 执行 git apply 后增量编译 parser.cpp 成功,accept_fix。

场景二:src/utils.hpp:120,no matching function for call to 'transform'。Error Analyzer 识别为模板推导失败;Context Analyzer 发现用的是自定义 transform 而非 std::transform,且少了第四个参数;Fixer 给两个方案——补一个转换函数 lambda,或者 include `` 改用 std::transform;Verifier 验证后选最佳。

学习、并行与增量过滤

学习机制在 LearningModule 里,fix_database 存「错误模式 → 成功修复」,record_success 累积记录,suggest_fix 取 success_rate 最高的一条。

并行处理由 parallel_fix 负责,用 asyncio.create_task + asyncio.gather 并行处理相互独立的错误组。增量修复用 IncrementalFixer.filter_new_errors,以 (file_path, line_number, error_msg) 集合做差集,只处理新出现的错误。缓存层 CacheManagerLRUCache(maxsize=100) 缓存文件内容。

参考

  • 博客园 iTech《实战:用 Claude Agent SDK 构建多 Agent 系统自动修复 C++ 构建错误》:https://www.cnblogs.com/itech/p/19823071
  • Claude Agent SDK 文档:https://docs.claude.com/en/api/agent-sdk/overview

Keywords: Fixer Agent, Claude Agent SDK, C++ 构建错误, 多 Agent 系统, Verifier Agent

Tags: Fixer Agent, Claude Agent SDK, C++ 构建错误, 多 Agent 系统

推荐文章

程序员茄子在线接单