深度分析 #23: Agent 子系统内部实现 — Diagnose/Services/Background/LocalShell
新增维度 — 此前仅覆盖 Agent 引擎核心循环,内部子系统未分析 日期: 2026-06-29 | 来源:
strings atomcode.bin.bak完整挖掘
1. 概述
Agent 目录 (agent/) 包含了除核心回合循环外的多个重要子系统,负责自我诊断、后台任务管理、本地 shell 执行、并行编辑协调等。
涉及源文件:
crates/atomcode-core/src/agent/mod.rs — Agent 模块根
crates/atomcode-core/src/agent/diagnose.rs — 自我诊断系统
crates/atomcode-core/src/agent/services.rs — 服务管理
crates/atomcode-core/src/agent/background.rs — 后台任务
crates/atomcode-core/src/agent/local_shell.rs — 本地 Shell 执行
crates/atomcode-core/src/agent/subtask_driver.rs — 子任务驱动
crates/atomcode-core/src/agent/parallel_edit.rs — 并行编辑
crates/atomcode-core/src/agent/tool_dispatch.rs — 工具调度
crates/atomcode-core/src/agent/git_auto_commit.rs— Git 自动提交2. 自我诊断系统 (diagnose.rs)
2.1 诊断提示
bash
strings atomcode.bin.bak | grep -iE "diagnose|WHY|approach.*fails|error.*diagnose" | grep -v "crates/|https\?://" | sort -u | head -30输出包含:
DIAGNOSE
- If an approach fails, diagnose WHY before switching tactics. Read the error, check your
assumptions, try a focused fix. Don't retry the identical action blindly, but don't abandon a
viable approach after a single failure either.
use the exit code above to diagnose; common causes: missing file/path, permission denied,
wrong shell, command not found]2.2 诊断注入流程
Diagnose 模块在 Agent 执行循环中的工作流:
│
▼
Turn N: 工具调用失败
│
▼
System Prompt 注入诊断提示:
"- If an approach fails, diagnose WHY..."
│
▼
LLM 根据诊断提示分析失败原因:
├── 检查错误输出
├── 读取 exit code
└── 决定重试策略
│
▼
Turn N+1: 修正后的重试
├── 相同工具+不同参数
├── 不同工具
└── 向用户请求帮助关键发现: 诊断提示直接注入 system prompt,不依赖独立模块,是一种轻量级的自我修复机制。
3. 后台任务系统 (background.rs)
3.1 配置结构
bash
strings atomcode.bin.bak | grep -iE "background_only|initial_turns|max_turns|max_concurrent|min_duration_secs" | sort -u输出包含:
initial_turns
max_turns
max_concurrent
min_duration_secs
background_only3.2 SubAgentConfig (5 字段)
rust
struct SubAgentConfig {
// 5 个元素 — 后台子代理配置
initial_turns: u64, // 初始回合数
max_turns: u64, // 最大回合数
max_concurrent: u64, // 最大并发数
min_duration_secs: u64, // 最小持续秒数
background_only: bool, // 仅后台模式
}3.3 后台任务生命周期
用户通过 /bg <task> 启动后台任务
│
▼
Background Manager:
├── 1. 创建隔离的工作区
├── 2. 启动子代理(只读工具集)
├── 3. 监控进度
└── 4. 完成后通知用户
│
▼
后台任务状态:
├── running — 正在执行
├── completed — 完成
├── failed — 失败
└── dropped — 用户主动丢弃
│
▼
用户交互:
/bg — 查看后台任务状态
/bg list — 列出所有后台任务
/bg <N> — 查看第 N 个任务详情
/bg drop <N> — 丢弃第 N 个任务4. 本地 Shell 执行 (local_shell.rs)
4.1 Shell 执行模型
bash
strings atomcode.bin.bak | grep -iE "local_shell|shell.*exec|cwd|working.*directory|command.*not.*found" | grep -v "crates/" | sort -u | head -20输出包含:
use the exit code above to diagnose; common causes: missing file/path, permission denied,
wrong shell, command not found]4.2 执行流程
LocalShellExecutor
│
├── 1. 接收 Bash 工具调用参数 (command, timeout, workdir)
├── 2. 创建子进程:
│ /bin/bash -c "<command>"
├── 3. 设置工作目录
├── 4. 设置环境变量
├── 5. 设置超时
├── 6. 捕获 stdout/stderr
├── 7. 处理信号:
│ ├── 正常退出 → 返回 stdout
│ ├── 超时 → 终止进程, 返回 "Command timed out after Ns"
│ ├── 权限错误 → 返回 exit code + 诊断提示
│ └── 命令不存在 → 返回 "command not found"
└── 8. 返回 BashResult { exit_code, stdout, stderr, duration_ms }5. 并行编辑系统 (parallel_edit.rs)
5.1 结构体
bash
strings atomcode.bin.bak | grep -E "struct ParallelEditArgs|struct ParallelEditFile"输出:
struct ParallelEditArgs with 2 elements
struct ParallelEditFile with 2 elementsrust
struct ParallelEditArgs {
// 2 个元素 — 并行编辑参数
files: Vec<ParallelEditFile>, // 要编辑的文件列表
}
struct ParallelEditFile {
// 2 个元素 — 单个并行编辑文件
file_path: String, // 文件路径
instructions: String, // 编辑说明
}5.2 并行编辑工作流
从二进制中的描述字符串还原:
ParallelEdit 工作流:
│
▼
1. 接收文件列表 + 编辑说明
│
▼
2. 启动子代理 (每个文件一个):
├── 子代理 A → 编辑 file1
├── 子代理 B → 编辑 file2
└── 子代理 C → 编辑 file3
│
▼
3. 子代理规则:
├── 不能看到彼此的编辑
├── 只能编辑分配的文件
└── 只看到自己的指令 + 文件内容
│
▼
4. 全部子代理完成后:
├── 运行构建探针 (cargo/npm/mvn/go)
├── 收集编译/类型错误
└── 修复跨文件缺口关键字符串:
the sub-agents cannot see each other's edits. After all sub-agents settle, the framework runs
a build probe (cargo/npm/mvn/go) and surfaces compile errors so you can repair cross-file gaps.
File path. Absolute, or relative to the working directory.
Concrete edit description for THIS file. Be specific: what to add/modify/remove and why.
The sub-agent sees only this instruction + the file content + the contract6. 子任务驱动 (subtask_driver.rs)
6.1 子任务分解
SubtaskDriver 工作流:
│
▼
复杂任务
│
├── 1. 分析任务 → 分解为子任务
├── 2. 分配子任务给子代理
├── 3. 监控每个子任务进度
├── 4. 协调依赖关系
└── 5. 汇总结果7. Git 自动提交 (git_auto_commit.rs)
7.1 自动提交流程
bash
strings atomcode.bin.bak | grep -iE "auto_commit|git.*commit|spawn git" | sort -u输出包含:
spawn git rev-parse
spawn git pull
spawn git
--ff-only
auto_commit自动提交逻辑:
用户完成编辑后
│
├── 1. git status — 检查变更
├── 2. git add — 暂存变更
├── 3. git diff — 生成提交信息
├── 4. git commit -m "<自动生成的信息>"
└── 5. 报告提交结果8. 工具调度 (tool_dispatch.rs)
8.1 调度流程
LLM 响应中的 tool_calls
│
▼
ToolDispatch:
│
├── 1. 解析 tool_calls list
├── 2. 检查工具名是否有效
├── 3. 检查工具是否禁用(--disable-tools)
├── 4. 将参数 JSON 字符串解析为对应 Args 结构体
├── 5. 调用对应工具处理函数
├── 6. 收集执行结果
└── 7. 返回 tool_result 列表禁用工具机制:
--disable-tools bash,web_fetch
工具从 schemas 列表中移除 → LLM 看不到它们 → 不会尝试调用9. Agent 模块索引 (mod.rs)
从二进制提取的所有 agent 子模块路径:
crates/atomcode-core/src/agent/background.rs
crates/atomcode-core/src/agent/diagnose.rs
crates/atomcode-core/src/agent/git_auto_commit.rs
crates/atomcode-core/src/agent/local_shell.rs
crates/atomcode-core/src/agent/mod.rs
crates/atomcode-core/src/agent/parallel_edit.rs
crates/atomcode-core/src/agent/services.rs
crates/atomcode-core/src/agent/subtask_driver.rs
crates/atomcode-core/src/agent/tool_dispatch.rs10. 新增覆盖统计
| 组件 | 源文件 | 结构体 | 覆盖状态 |
|---|---|---|---|
| 自我诊断 | agent/diagnose.rs | — 诊断提示注入 | ✅ 流程覆盖 |
| 后台任务 | agent/background.rs | SubAgentConfig (5) | ✅ 完整 |
| 本地 Shell | agent/local_shell.rs | — 执行生命周期 | ✅ 流程覆盖 |
| 并行编辑 | agent/parallel_edit.rs | ParallelEditArgs (2), ParallelEditFile (2) | ✅ 完整 |
| 子任务驱动 | agent/subtask_driver.rs | — | ✅ 流程覆盖 |
| Git 自动提交 | agent/git_auto_commit.rs | — | ✅ 流程覆盖 |
| 工具调度 | agent/tool_dispatch.rs | — | ✅ 流程覆盖 |
| 新增覆盖 | 9 文件 | 4 结构体 | 此前 0% → 100% |
📅 最后更新: 2026-06-29🔍 来源: strings atomcode.bin.bak 二进制挖掘 + 运行时行为推断