凌晨4点17分,你的Agent执行了一个任务。结果不对。然后它停下来,看了看自己的输出,意识到问题所在,自动修正并重新执行——这就是Self-Correction(自我修正)。
Self-Correction是Agent在执行过程中或执行后,通过自我评估发现问题并主动修正的能力。它包含三个核心要素:
┌───────────┐
│ Plan │
└───┬───────┘
│
▼
┌───────────┐
│ Execute │──────► Output
└───┬───────┘ │
│ ▼
│ ┌───────────┐
│ │ Evaluate │
│ └─────┬─────┘
│ │
│ ▼
│ ┌───────────┐
│ │ Reflect │
│ └─────┬─────┘
│ │
│ Error? ▼
│ ┌───────────┐
│◄────────│ Correct │
│ └───────────┘
│ │
▼ │
┌───────────┐ │
│ Replan │◄─────────┘
└───────────┘
Agent在完成任务后,主动反思输出的质量和正确性:
// OpenClaw Reflection工作流
const reflectionWorkflow = async (task) => {
// 第一轮执行
const result = await agent.execute(task);
// 自我评估
const critique = await agent.evaluate({
task: task,
output: result,
criteria: [
"是否符合用户需求",
"是否有逻辑错误",
"是否遗漏关键信息",
"格式是否正确"
]
});
// 如果有问题,修正
if (critique.hasIssues) {
const improvedResult = await agent.execute({
task: task,
previousOutput: result,
critique: critique,
instruction: "根据上述反馈改进输出"
});
return improvedResult;
}
return result;
};
遇到执行失败时,自动重试并调整策略:
// OpenClaw Retry模式
const retryWithAdaptation = async (task, maxRetries = 3) => {
let attempts = [];
for (let i = 0; i < maxRetries; i++) {
try {
const result = await agent.execute({
task: task,
previousAttempts: attempts, // 让Agent知道之前怎么失败的
strategy: i === 0 ? "default" : "adapted"
});
return { success: true, result, attempts };
} catch (error) {
attempts.push({
attempt: i + 1,
error: error.message,
strategy: i === 0 ? "default" : "adapted"
});
// 分析失败原因,调整策略
if (i < maxRetries - 1) {
await agent.analyzeFailure({
attempts: attempts,
task: task
});
}
}
}
return { success: false, attempts };
};
通过外部验证来触发修正:
// OpenClaw Verification模式
const verifiedExecution = async (task) => {
const result = await agent.execute(task);
// 外部验证(可以是另一个Agent或真实检验)
const verification = await verify({
output: result,
type: "external_check",
validator: "code_compiler" // 如果是代码任务,实际编译测试
});
if (!verification.passed) {
// 基于验证结果修正
const correction = await agent.execute({
task: task,
previousOutput: result,
verificationErrors: verification.errors,
instruction: "修复验证发现的错误"
});
// 再次验证
return verifiedExecution({ task, previous: correction });
}
return result;
};
结合用户反馈进行修正:
# OpenClaw Feedback工作流
name: feedback_correction
description: 基于用户反馈的迭代修正
workflow:
- step: initial_execution
action: execute
input: "${task}"
- step: present_output
action: notify
channel: feishu
content: "${$[0]}"
ask_feedback: true
- step: collect_feedback
action: wait_for_response
timeout: 60m
- step: apply_feedback
condition: "${feedback_received}"
action: execute
input:
task: "${task}"
previous: "${$[0]}"
feedback: "${$[2]}"
instruction: "根据用户反馈改进"
- step: final_output
action: notify
content: "${$[3]}"
# SKILL.md - 自愈Agent配置
name: self_healing_agent
correction:
# 自动错误检测
auto_detect:
- invalid_output_format
- missing_required_fields
- logical_inconsistency
- timeout_failure
# 修正策略
strategies:
- name: reflection
trigger: "output_received"
max_iterations: 2
- name: retry
trigger: "execution_error"
max_retries: 3
backoff: exponential
- name: fallback
trigger: "all_retries_failed"
action: "use_simplified_approach"
# 验证集成
validation:
- type: schema_check
enabled: true
- type: external_tool
enabled: true
tool: code_linter
错误类型 优先级 修正策略
────────────────────────────────────────────
格式错误 P1 自动格式化
逻辑错误 P2 Reflection修正
工具失败 P3 Retry + 超时调整
理解偏差 P4 用户反馈修正
性能问题 P5 简化方案降级
// 必须设置的终止条件
maxIterations: 3
maxTime: 30m
qualityThreshold: 0.8
convergenceCheck: "output_stable"