name: learning-systems description: 隐式反馈评分、置信度衰减和反模式检测。在理解swarm插件如何从结果中学习、实现学习循环或调试模式推广和废弃时使用。仅限opencode-swarm-plugin。
学习系统
swarm插件从任务结果中学习,随时间提高分解质量。三个互连的系统跟踪模式效果:隐式反馈评分、置信度衰减和模式成熟度进展。
隐式反馈评分
将任务结果转换为学习信号,无需显式用户反馈。
评分内容
时长信号:
- 快速(<5分钟)= 有帮助(1.0)
- 中等(5-30分钟)= 中性(0.6)
- 慢(>30分钟)= 有害(0.2)
错误信号:
- 0错误 = 有帮助(1.0)
- 1-2错误 = 中性(0.6)
- 3+错误 = 有害(0.2)
重试信号:
- 0重试 = 有帮助(1.0)
- 1重试 = 中性(0.7)
- 2+重试 = 有害(0.3)
成功信号:
- 成功 = 1.0(40%权重)
- 失败 = 0.0
加权分数计算
rawScore = success * 0.4 + duration * 0.2 + errors * 0.2 + retries * 0.2;
阈值:
- rawScore >= 0.7 → 有帮助
- rawScore <= 0.4 → 有害
- 0.4 < rawScore < 0.7 → 中性
记录结果
子任务完成后调用swarm_record_outcome:
swarm_record_outcome({
bead_id: "bd-123.1",
duration_ms: 180000, // 3分钟
error_count: 0,
retry_count: 0,
success: true,
files_touched: ["src/auth.ts"],
strategy: "file-based",
});
跟踪字段:
bead_id- 子任务标识符duration_ms- 从开始到完成的时间error_count- 遇到的错误(来自ErrorAccumulator)retry_count- 重试尝试次数success- 子任务是否成功完成files_touched- 修改的文件路径strategy- 使用的分解策略(可选)failure_mode- 如果success=false的分类(可选)failure_details- 错误上下文(可选)
置信度衰减
除非重新验证,否则评估标准权重衰减。防止过时模式主导未来分解。
半衰期公式
decayed_value = raw_value * 0.5^(age_days / 90)
衰减时间线:
- 第0天:100%权重
- 第90天:50%权重
- 第180天:25%权重
- 第270天:12.5%权重
标准权重计算
聚合衰减的反馈事件:
helpfulSum = sum(helpful_events.map((e) => e.raw_value * decay(e.timestamp)));
harmfulSum = sum(harmful_events.map((e) => e.raw_value * decay(e.timestamp)));
weight = max(0.1, helpfulSum / (helpfulSum + harmfulSum));
权重下限: 最小0.1防止完全归零
重新验证
记录新反馈重置该标准的衰减计时器:
{
criterion: "type_safe",
weight: 0.85,
helpful_count: 12,
harmful_count: 3,
last_validated: "2024-12-12T00:00:00Z", // 新反馈时重置
half_life_days: 90,
}
标准何时被废弃
total = helpful_count + harmful_count;
harmfulRatio = harmful_count / total;
if (total >= 3 && harmfulRatio > 0.3) {
// 废弃标准 - 减少影响至0
}
模式成熟度状态
模式根据反馈积累进展生命周期:
候选 → 已建立 → 已验证(或已废弃)
状态转换
候选(初始状态):
- 总反馈 < 3事件
- 数据不足判断
- 乘数:0.5x
已建立:
- 总反馈 >= 3事件
- 有跟踪记录但未验证
- 乘数:1.0x
已验证:
- 衰减有帮助 >= 5 且
- 有害比率 < 15%
- 乘数:1.5x
已废弃:
- 有害比率 > 30% 且
- 总反馈 >= 3事件
- 乘数:0x(排除)
状态计算中的衰减应用
状态确定使用衰减计数,非原始计数:
const { decayedHelpful, decayedHarmful } =
calculateDecayedCounts(feedbackEvents);
const total = decayedHelpful + decayedHarmful;
const harmfulRatio = decayedHarmful / total;
// 状态逻辑应用于衰减值
旧反馈影响较小。模式必须保持近期正面信号以保持已验证。
手动状态更改
提升至已验证:
promotePattern(maturity); // 外部验证确认有效性
废弃:
deprecatePattern(maturity, "在80%情况下导致文件冲突");
不能提升已废弃模式。必须重置。
分解中的乘数
应用成熟度乘数到模式分数:
const multipliers = {
candidate: 0.5,
established: 1.0,
proven: 1.5,
deprecated: 0,
};
pattern_score = base_score * multipliers[maturity.state];
已验证模式获得50%提升,已废弃模式完全排除。
反模式反转
失败模式在>60%失败率时自动转换为反模式。
反转阈值
const total = pattern.success_count + pattern.failure_count;
if (total >= 3 && pattern.failure_count / total >= 0.6) {
invertToAntiPattern(pattern, reason);
}
最小观察次数: 3次(防止草率反转) 失败比率: 60%(5次尝试中3+次失败)
反转过程
原始模式:
{
id: "pattern-123",
content: "按文件类型拆分",
kind: "pattern",
is_negative: false,
success_count: 2,
failure_count: 5,
}
反转的反模式:
{
id: "anti-pattern-123",
content: "避免:按文件类型拆分。失败5/7次(71%失败率)",
kind: "anti_pattern",
is_negative: true,
success_count: 2,
failure_count: 5,
reason: "失败5/7次(71%失败率)",
}
记录观察
跟踪模式结果以累积成功/失败计数:
recordPatternObservation(
pattern,
success: true, // 或false
beadId: "bd-123.1",
)
// 返回:
{
pattern: updatedPattern,
inversion?: {
original: pattern,
inverted: antiPattern,
reason: "失败5/7次(71%失败率)",
}
}
模式提取
从分解描述中自动检测策略:
extractPatternsFromDescription(
"我们将按文件类型拆分,每个子任务一个文件",
);
// 返回:["按文件类型拆分", "每个子任务一个文件"]
检测到的策略:
- 按文件类型拆分
- 按组件拆分
- 按层拆分(UI/逻辑/数据)
- 按功能拆分
- 每个子任务一个文件
- 先处理共享类型
- 分离API路由
- 测试与实现一起
- 测试在单独子任务中
- 最大化并行化
- 顺序执行顺序
- 尊重依赖链
在提示中使用反模式
为分解提示包含格式化:
formatAntiPatternsForPrompt(patterns);
输出:
## 要避免的反模式
基于过去失败,避免这些分解策略:
- 避免:按文件类型拆分。失败12/15次(80%失败率)
- 避免:每个子任务一个文件。失败8/10次(80%失败率)
错误累加器
跟踪子任务执行中的错误以用于重试提示和结果评分。
错误类型
type ErrorType =
| "validation" // 模式/类型错误
| "timeout" // 任务超时
| "conflict" // 文件保留冲突
| "tool_failure" // 工具调用失败
| "unknown"; // 未分类
记录错误
errorAccumulator.recordError(
beadId: "bd-123.1",
errorType: "validation",
message: "src/auth.ts中的类型错误",
options: {
stack_trace: "...",
tool_name: "typecheck",
context: "添加OAuth类型后",
}
)
生成错误上下文
为重试提示格式化累积错误:
const context = await errorAccumulator.getErrorContext(
beadId: "bd-123.1",
includeResolved: false,
)
输出:
## 先前错误
执行过程中遇到以下错误:
### validation(2个错误)
- **src/auth.ts中的类型错误**
- 上下文:添加OAuth类型后
- 工具:typecheck
- 时间:2024年12月12日,上午10:30
- **src/session.ts中缺失导入**
- 工具:typecheck
- 时间:2024年12月12日,上午10:35
**需要操作**:在继续之前解决这些错误。考虑:
- 每个错误的原因是什么?
- 如何防止类似错误?
- 错误类型间有模式吗?
解决错误
修复后标记错误已解决:
await errorAccumulator.resolveError(errorId);
已解决错误默认从重试上下文中排除。
错误统计
为结果跟踪获取错误计数:
const stats = await errorAccumulator.getErrorStats("bd-123.1")
// 返回:
{
total: 5,
unresolved: 2,
by_type: {
validation: 3,
timeout: 1,
tool_failure: 1,
}
}
使用total作为结果信号中的error_count。
使用学习系统
集成点
1. 分解期间(swarm_plan_prompt):
- 查询CASS获取类似任务
- 加载模式成熟度记录
- 在提示中包含已验证模式
- 排除已废弃模式
2. 执行期间:
- ErrorAccumulator跟踪错误
- 记录重试尝试
- 跟踪从开始到完成的时长
3. 完成后(swarm_complete):
- 记录结果信号
- 评分隐式反馈
- 更新模式观察
- 检查反模式反转
- 更新成熟度状态
完整工作流示例
// 1. 分解阶段
const cass_results = cass_search({ query: "用户认证", limit: 5 });
const patterns = loadPatterns(); // 获取成熟度记录
const prompt = swarm_plan_prompt({
task: "添加OAuth",
context: formatPatternsWithMaturityForPrompt(patterns),
query_cass: true,
});
// 2. 执行阶段
const errorAccumulator = new ErrorAccumulator();
const startTime = Date.now();
try {
// 工作发生...
await implement_subtask();
} catch (error) {
await errorAccumulator.recordError(
bead_id,
classifyError(error),
error.message,
);
retryCount++;
}
// 3. 完成阶段
const duration = Date.now() - startTime;
const errorStats = await errorAccumulator.getErrorStats(bead_id);
swarm_record_outcome({
bead_id,
duration_ms: duration,
error_count: errorStats.total,
retry_count: retryCount,
success: true,
files_touched: modifiedFiles,
strategy: "file-based",
});
// 4. 学习更新
const scored = scoreImplicitFeedback({
bead_id,
duration_ms: duration,
error_count: errorStats.total,
retry_count: retryCount,
success: true,
timestamp: new Date().toISOString(),
strategy: "file-based",
});
// 更新模式
for (const pattern of extractedPatterns) {
const { pattern: updated, inversion } = recordPatternObservation(
pattern,
scored.type === "helpful",
bead_id,
);
if (inversion) {
console.log(`模式反转:${inversion.reason}`);
storeAntiPattern(inversion.inverted);
}
}
配置调优
根据项目特征调整阈值:
const learningConfig = {
halfLifeDays: 90, // 衰减速度
minFeedbackForAdjustment: 3, // 权重调整的最小观察次数
maxHarmfulRatio: 0.3, // 废弃前的最大有害百分比
fastCompletionThresholdMs: 300000, // 5分钟=快速
slowCompletionThresholdMs: 1800000, // 30分钟=慢
maxErrorsForHelpful: 2, // 标记有害前的最大错误数
};
const antiPatternConfig = {
minObservations: 3, // 反转前的最小次数
failureRatioThreshold: 0.6, // 60%失败触发反转
antiPatternPrefix: "避免:",
};
const maturityConfig = {
minFeedback: 3, // 离开候选状态的最小反馈
minHelpful: 5, // 已验证的衰减有帮助阈值
maxHarmful: 0.15, // 已验证的最大15%有害
deprecationThreshold: 0.3, // 30%有害触发废弃
halfLifeDays: 90,
};
调试模式问题
为什么模式未验证?
检查衰减计数:
const feedback = await getFeedback(patternId);
const { decayedHelpful, decayedHarmful } = calculateDecayedCounts(feedback);
console.log({ decayedHelpful, decayedHarmful });
// 需要:decayedHelpful >= 5 且 harmfulRatio < 0.15
为什么模式被反转?
检查观察计数:
const total = pattern.success_count + pattern.failure_count;
const failureRatio = pattern.failure_count / total;
console.log({ total, failureRatio });
// 反转如果:total >= 3 且 failureRatio >= 0.6
为什么标准权重低?
检查反馈事件:
const events = await getFeedbackByCriterion("type_safe");
const weight = calculateCriterionWeight(events);
console.log(weight);
// 显示:有帮助与有害计数,last_validated日期
存储接口
FeedbackStorage
持久化反馈事件用于标准权重计算:
interface FeedbackStorage {
store(event: FeedbackEvent): Promise<void>;
getByCriterion(criterion: string): Promise<FeedbackEvent[]>;
getByBead(beadId: string): Promise<FeedbackEvent[]>;
getAll(): Promise<FeedbackEvent[]>;
}
ErrorStorage
持久化错误用于重试提示:
interface ErrorStorage {
store(entry: ErrorEntry): Promise<void>;
getByBead(beadId: string): Promise<ErrorEntry[]>;
getUnresolvedByBead(beadId: string): Promise<ErrorEntry[]>;
markResolved(id: string): Promise<void>;
getAll(): Promise<ErrorEntry[]>;
}
PatternStorage
持久化分解模式:
interface PatternStorage {
store(pattern: DecompositionPattern): Promise<void>;
get(id: string): Promise<DecompositionPattern | null>;
getAll(): Promise<DecompositionPattern[]>;
getAntiPatterns(): Promise<DecompositionPattern[]>;
getByTag(tag: string): Promise<DecompositionPattern[]>;
findByContent(content: string): Promise<DecompositionPattern[]>;
}
MaturityStorage
持久化模式成熟度记录:
interface MaturityStorage {
store(maturity: PatternMaturity): Promise<void>;
get(patternId: string): Promise<PatternMaturity | null>;
getAll(): Promise<PatternMaturity[]>;
getByState(state: MaturityState): Promise<PatternMaturity[]>;
storeFeedback(feedback: MaturityFeedback): Promise<void>;
getFeedback(patternId: string): Promise<MaturityFeedback[]>;
}
提供内存实现用于测试。生产应使用持久存储(基于文件的JSONL或SQLite)。