Files
knowledge-base/4 - Resources/Claude-Code/Autonomous Agent Harness 自主代理框架.md
2026-04-06 23:27:39 +02:00

240 lines
7.6 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
created: "2026-04-06"
type: resource
tags: [resource, claude-code, AI-tools, autonomous-agent, agent-harness, ECC, windows-compatible]
source: "~/.claude/skills/autonomous-agent-harness/SKILL.md"
---
# Autonomous Agent Harness 自主代理框架
把 Claude Code 变成持久化、自驱动的 Agent 系统,替代 AutoGPT/Hermes。核心理念不需要额外框架用 Claude Code 原生能力crons + dispatch + MCP + memory就能构建自主 Agent。
相关笔记:[[Autonomous Loops 自主循环模式]]、[[dmux 多Agent并行编排]]、[[ECC 编排替代方案 (orchestrate 迁移)]]
## 架构
```
┌─────────────────────────────────────────────────────┐
│ Claude Code Runtime │
│ │
│ Crons (定时) Dispatch (远程) Memory Computer Use │
│ │ │ │ │ │
│ ▼ ▼ ▼ ▼ │
│ ┌───────────────────────────────────────────────┐ │
│ │ ECC Skill + Agent Layer │ │
│ │ autonomous-loops / eval-harness / santa... │ │
│ │ loop-operator / harness-optimizer agents │ │
│ └───────────────────────────────────────────────┘ │
│ │ │ │ │ │
│ ▼ ▼ ▼ ▼ │
│ ┌───────────────────────────────────────────────┐ │
│ │ MCP Server Layer │ │
│ │ memory github exa browser-use ... │ │
│ └───────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────┘
```
## 5 大核心组件
### 1. 三层记忆系统
| 层级 | 机制 | 生命周期 | 用途 |
|------|------|----------|------|
| 短期 | `TodoWrite` | 单次会话内 | 任务追踪 |
| 中期 | `~/.claude/projects/*/memory/*.md` | 跨会话 | 项目上下文 |
| 长期 | MCP Memory Server (知识图谱) | 永久 | 实体、关系、观察 |
### 2. 定时操作 (Crons)
```bash
# Claude Code 内置 cron 能力
# 例:每 30 分钟检查新 PR 并审查
Cron: every 30 min during work hours
1. Check for new PRs on watched repos
2. For each new PR: pull branch, run tests, review
3. Post review comments via GitHub MCP
4. Update memory with review status
```
### 3. 远程 Agent (Dispatch)
通过 `claude dispatch` 或 remote trigger 启动远程 Agent 实例。
### 4. Computer Use (MCP)
通过 MCP browser/desktop 服务器实现屏幕交互、浏览器操作。
### 5. 任务队列
基于 memory 的持久化任务队列,跨会话保持任务状态。
---
## 关键 Agent
### loop-operator
运行自主循环的安全操作员:
- 跟踪进度 checkpoint
- 检测停滞和重试风暴
- 失败重复时暂停并缩小范围
- 验证通过后才恢复
**升级条件**(任何一个为 true 则升级到人类):
- 连续 2 个 checkpoint 无进展
- 重复相同 stack trace 的失败
- 成本漂移超出预算窗口
- merge 冲突阻塞队列
### harness-optimizer
优化 Agent 框架配置的专家:
1. 运行 `/harness-audit` 收集基线分数
2. 识别 top 3 杠杆点hooks, evals, routing, context, safety
3. 提出最小可逆配置变更
4. 应用并验证
5. 报告前后对比
---
## 核心设计原则
### 1. Eval-First评估先行
执行前定义完成标准。Eval 是 "AI 开发的单元测试"。
```
目标 pass@3 > 90%
- 定义 capability eval (新功能能做什么)
- 定义 regression eval (不破坏已有功能)
```
### 2. De-Sloppify去粗糙化
**永远不要给生成器加负面约束**"不要做 X")。让它自由生成,然后加独立清理 agent。
> 核心洞察:两个聚焦的 Agent 优于一个被约束的 Agent。
### 3. Santa Method收敛循环
```
Generator 生成
→ Reviewer A安全+正确性)独立评估
→ Reviewer B架构+测试)独立评估
→ 两者都 PASS 才算收敛
→ FAIL 则修复后用全新 Agent 重跑两个 Reviewer
→ 最多 3 轮,超过则上报人类
```
关键Reviewer 从未看过 Generator 的推理过程,消除作者偏见。
### 4. 15 分钟单元规则
每个任务单元必须:
- 独立可验证
- 单一主要风险
- 明确的完成条件
- 约 15 分钟可完成
### 5. 分离上下文窗口
每个管道阶段在独立 Agent 进程中运行。不同阶段用不同模型:
| 阶段 | 模型 |
|------|------|
| Research | Sonnet |
| Plan | Opus |
| Implement | Sonnet / Codex |
| Review | Opus |
### 6. 循环安全
- **必须有退出条件**max-runs / max-cost / max-duration / completion signal
- **检测停滞和重试风暴**
- **质量门必须活跃**eval baseline 必须存在
- **回滚路径必须存在**
---
## Hermes 组件替代表
| Hermes 组件 | ECC 替代 |
|-------------|---------|
| Task Queue | MCP Memory + TodoWrite |
| Long-term Memory | MCP Memory Server (知识图谱) |
| Tool Execution | MCP Server Layer |
| Planning | /ecc:plan + /ecc:feature-dev |
| Scheduling | Claude Code Crons |
| Computer Use | MCP Playwright / Desktop |
| Web Browsing | MCP Browser + Exa Search |
---
## Windows 可用性
| 组件 | Windows | 说明 |
|------|---------|------|
| 三层记忆 | 可用 | 文件系统 + MCP |
| Crons | 可用 | Claude Code 原生 |
| Dispatch | 可用 | Claude Code 原生 |
| loop-operator agent | 可用 | Claude Code 内部 |
| 外部脚本auto-pilot.sh | 可用 | Git Bash |
| dmux 并行编排 | **不可用** | 需要 tmuxLinux/Mac |
Windows 上的并行替代Claude Code 内置 Agent/Task tool 实现进程内并行子 agent。
---
## 实际例子
### Sequential Pipeline最常用
```bash
#!/bin/bash
set -e
claude -p "读取 spec实现功能先写测试"
claude -p "审查改动,清理 slop运行测试"
claude -p "运行构建 + lint + 测试,修复失败"
claude -p "创建 conventional commit"
```
### Cron 定时 PR 审查
```
Cron: 工作时间每 30 分钟
1. gh pr list --state open
2. 对每个 PR: 拉分支、运行测试、code-reviewer 审查
3. GitHub MCP 发布评论
4. memory 更新审查状态
```
### 带成本控制的持续循环
```bash
continuous-claude --prompt "为未测试函数添加单元测试" --max-runs 10
continuous-claude --prompt "修复所有 linter 错误" --max-cost 5.00
continuous-claude --prompt "提升测试覆盖率" --max-duration 8h
```
---
## 反模式
| 反模式 | 问题 | 正确做法 |
|--------|------|---------|
| 无退出条件的循环 | 无限烧钱 | 始终设 max-runs/max-cost |
| 单 agent 自审自 | 作者偏见 | Santa Method 双独立 reviewer |
| 用否定指令约束生成 | 质量下降 | De-Sloppify 独立 pass |
| 迭代间无上下文桥 | 重复劳动 | SHARED_TASK_NOTES.md |
| 所有阶段同一上下文 | 偏见累积 | 每阶段独立进程 |
## Related
- [[Autonomous Loops 自主循环模式]]
- [[dmux 多Agent并行编排]]
- [[Ralphinho RFC-DAG 编排模式]]
- [[ECC 编排替代方案 (orchestrate 迁移)]]
- [[Everything Claude Code 完整指南]]