🐛 fix(agent/schedulerefine): 修复复合微调分支链路问题,并将 MinContextSwitch 重构为固定坑位重排语义 - 🔧 修复 `schedulerefine` 复合路由中参数透传不完整、缺少 deterministic objective 时错误降级,以及“复合工具执行成功”与“终审通过”语义混淆的问题 - ✅ 保证新的独立复合分支能够正确执行、正确出站,并统一交由 `hard_check` 裁决最终结果 - 🔍 排查时发现 `MinContextSwitch` 上游 `context_tag` 存在整体退化为 `General` 的风险,影响MinContextSwitch - 🛡️ 为 `MinContextSwitch` 增加兜底策略:当标签整体退化时,按任务名关键词推断学科分组,避免分组能力失效 - ♻️ 将 `MinContextSwitch` 从“整周重新寻找新坑位”调整为“坑位不变,任务顺序改变” - 🎯 将落地方式从顺序 `BatchMove` 改为固定坑位原子重写,避免出现远距离跳位、跨天错迁、异常嵌入课位及循环换位冲突 - 🧹 修复 `hard_check` 在 `MinContextSwitch` 成功后仍执行 `origin_rank` 顺序归位、并导致逆序终审误判的问题 - 🚦 命中该分支后跳过顺序归位与顺序硬校验,避免 `summary` / `hard_check` 将有效重排结果误判为失败 📈 当前连续微调规划涉及的全部功能已可以稳定运行;下一步将继续扩展能力边界,并进一步优化 `schedule_plan` 流程 ♻️ refactor: 重整 agent2 架构,并迁移 quicknote/chat 新链路,目前还剩3个模块未迁移,后续迁移完成后会删除原agent并将此目录命名为agent - 🏗️ 明确 `agent2` 采用“统一分层目录 + 文件分层 + 依赖注入”的重构方案,不再沿用模块目录多层嵌套结构 - 🧩 完善 `agent2` 基础骨架,统一收口 `entrance` / `router` / `llm` / `stream` / `shared` / `model` / `prompt` / `node` / `graph` 等层级职责 - 🚚 将通用路由能力迁移至 `agent2/router`,沉淀统一的 `Action`、`RoutingDecision`、控制码解析,以及 `Dispatcher` / `Resolver` 抽象 - 💬 将普通聊天链路迁移至 `agent2/chat`,复用 `stream` 的 OpenAI 兼容输出协议与 LLM usage 聚合能力 - 📝 将 `quicknote` 链路迁移到 `agent2` 新结构,拆分为 `model` / `prompt` / `llm` / `node` / `graph` 多层实现,替换对旧 `agent/quicknote` 的直接依赖 - 🔌 调整 `agentsvc` 对 `agent2` 的引用,普通聊天、通用分流与 `quicknote` 全部切换到新链路 - ✂️ 去除 graph 内部 `runner` 转接层,改为由 node 层直接持有请求级依赖,并向 graph 暴露节点方法 - 🧹 合并 `graph/quicknote` 与 `graph/quicknote_run`,删除冗余骨架文件,收敛为单一 `quicknote graph` 文件 - 📚 新增 `agent2`《通用能力接入文档》,明确公共能力边界、接入方式以及 graph/node 协作约定 - 📝 更新 `AGENTS.md`,要求后续扩展 `agent2` 通用能力时必须同步维护接入文档 ♻️ refactor: 删除了现Agent目录内Chat模块的两条冗余Prompt
86 lines
2.4 KiB
Go
86 lines
2.4 KiB
Go
package agentshared
|
||
|
||
import (
|
||
"context"
|
||
"time"
|
||
)
|
||
|
||
// RetryOptions 描述公共重试策略。
|
||
//
|
||
// 职责边界:
|
||
// 1. 这里只定义“是否重试、最多几次、间隔多久”;
|
||
// 2. 不关心具体业务是工具调用失败、模型 JSON 失败还是 DB 暂时不可用;
|
||
// 3. 真正的业务兜底文案仍应由上层 node 决定。
|
||
type RetryOptions struct {
|
||
MaxAttempts int
|
||
Interval time.Duration
|
||
ShouldRetry func(err error) bool
|
||
OnRetry func(attempt int, err error)
|
||
}
|
||
|
||
// Do 执行一个只返回 error 的重试任务。
|
||
//
|
||
// 执行规则:
|
||
// 1. 第一次执行也算一次 attempt;
|
||
// 2. 任意一次成功即立即返回;
|
||
// 3. 上下文取消、达到最大次数、或 ShouldRetry=false 时立即停止。
|
||
func Do(ctx context.Context, options RetryOptions, fn func(attempt int) error) error {
|
||
_, err := DoValue[struct{}](ctx, options, func(attempt int) (struct{}, error) {
|
||
return struct{}{}, fn(attempt)
|
||
})
|
||
return err
|
||
}
|
||
|
||
// DoValue 执行一个带返回值的通用重试任务。
|
||
//
|
||
// 设计说明:
|
||
// 1. 旧 agent 里后续很多地方都会出现“失败重试 2~3 次”的模式;
|
||
// 2. 这里先把循环骨架统一,避免每个 skill 自己写 for + sleep + ctx.Done;
|
||
// 3. 上层只需关心“本轮失败要不要继续”,而不是重复造轮子。
|
||
func DoValue[T any](ctx context.Context, options RetryOptions, fn func(attempt int) (T, error)) (T, error) {
|
||
var zero T
|
||
|
||
maxAttempts := options.MaxAttempts
|
||
if maxAttempts <= 0 {
|
||
maxAttempts = 1
|
||
}
|
||
|
||
for attempt := 1; attempt <= maxAttempts; attempt++ {
|
||
if err := ctx.Err(); err != nil {
|
||
return zero, err
|
||
}
|
||
|
||
value, err := fn(attempt)
|
||
if err == nil {
|
||
return value, nil
|
||
}
|
||
|
||
// 1. 到最后一次了,直接返回原错误,避免无意义等待。
|
||
if attempt >= maxAttempts {
|
||
return zero, err
|
||
}
|
||
// 2. 业务显式声明“不值得重试”时,立刻停止。
|
||
if options.ShouldRetry != nil && !options.ShouldRetry(err) {
|
||
return zero, err
|
||
}
|
||
// 3. 把重试钩子留给上层,用于打点或阶段提示。
|
||
if options.OnRetry != nil {
|
||
options.OnRetry(attempt, err)
|
||
}
|
||
// 4. 没有配置间隔则马上下一轮;配置了则等待,同时尊重 ctx 取消。
|
||
if options.Interval <= 0 {
|
||
continue
|
||
}
|
||
|
||
timer := time.NewTimer(options.Interval)
|
||
select {
|
||
case <-ctx.Done():
|
||
timer.Stop()
|
||
return zero, ctx.Err()
|
||
case <-timer.C:
|
||
}
|
||
}
|
||
|
||
return zero, nil
|
||
}
|