worker 因 LLM 网络/连接等错误 model_error 收场、耗尽引擎层 2 次自动重试后意图标
blocked,此前无手动恢复入口。现支持一键重跑:置回 open→worker 重新认领、从头再跑
(已写回图谱的 fact/finding/asset 保留);任务若已终态/暂停会自动复活。
- db: ReopenIntent(blocked/exhausted/stopped→open)、ReopenBlockedIntents(批量 blocked→open)。
- server: resumeTaskForGoal 泛化为 reviveTask(set_goals 与重跑复用同一复活逻辑);
新增 POST /tasks/{id}/intents/{iid}/rerun 与 /tasks/{id}/intents/rerun-blocked,
reopen 后 reviveTask 确保 worker 循环存活并重认领。
- web: api.rerunIntent/rerunBlocked;概览页新增「被拦/出错意图」卡片,每条「重跑」+
顶部「全部重跑」,乐观更新 + 3s 轮询兜底。
138 lines
5.7 KiB
Go
138 lines
5.7 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/Autumn-27/artex/agent"
|
|
"github.com/Autumn-27/artex/db"
|
|
)
|
|
|
|
type goalSpec struct {
|
|
Text string
|
|
VulnClass string
|
|
}
|
|
|
|
// launchTask runs the shared post-creation sequence for a task created via ANY
|
|
// path (HTTP createTask 或 orchestration spawn_task),避免两处复制粘贴:
|
|
// 1. seed 根资产,喂给事件驱动 loop;
|
|
// 2. 可选种子意图,worker 免等首轮 planner 直接开跑;
|
|
// 3. 后台异步做目标分解(发「第 0 轮目标拆解」round + LLM 分解步骤 + 逐条 goal,页面可见),
|
|
// 分解完再 engine.Run —— 引擎在 goal 节点就绪后才启动,避免 planner 抢在 goal 之前跑的竞态。
|
|
//
|
|
// 异步(goroutine)所以调用方立即返回,两条路径行为一致:秒建任务、后台拆目标。
|
|
func (s *Server) launchTask(t *Task, seedText string, seedFirstIntent bool) {
|
|
s.seed(t, seedText)
|
|
if seedFirstIntent {
|
|
s.seedFirstIntent(t)
|
|
}
|
|
go func() {
|
|
s.engine.emitActivity(t, db.Activity{Worker: "planner", Kind: "round",
|
|
Summary: "第 0 轮目标拆解"})
|
|
goals := s.createGoals(s.ctx, t, func(r db.Activity) {
|
|
s.engine.emitActivity(t, r)
|
|
})
|
|
for _, g := range goals {
|
|
summary := g.Text
|
|
if g.VulnClass != "" {
|
|
summary = fmt.Sprintf("[%s] %s", g.VulnClass, g.Text)
|
|
}
|
|
s.engine.emitActivity(t, db.Activity{Worker: "planner", Kind: "text", Summary: summary})
|
|
}
|
|
s.engine.Run(s.ctx, t)
|
|
}()
|
|
}
|
|
|
|
// reviveTask 让一个已停下的任务重新跑起来:把终态(done/failed/timeout)拉回 running、
|
|
// 解除暂停,并(重)启动引擎循环 + 唤醒。已在 running 且未暂停的任务:只剩 Run 里的一次
|
|
// Notify,近乎无副作用。用于「主 agent set_goals 新增目标」和「重跑 blocked 意图」两处。
|
|
//
|
|
// 为什么必须显式复活:planner/worker 循环的终态门(engine.go)会吞掉普通 notify——光改
|
|
// 图 + Notify 唤不醒已判完成的任务;重启后终态任务的 goroutine 也可能已不在,故还要 Run。
|
|
func (s *Server) reviveTask(t *Task) {
|
|
if t == nil {
|
|
return
|
|
}
|
|
// 终态 → 拉回 running(SetTaskStatus 会清 completed_at 并同步内存 handle 的 Status,
|
|
// 使 planner/worker 循环的终态门放行)。
|
|
if isTerminalStatus(t.Status) {
|
|
if err := s.m.SetTaskStatus(t.ID, "running"); err != nil {
|
|
log.Printf("[revive] task %s 置 running 失败: %v", t.ID, err)
|
|
}
|
|
}
|
|
// 确保引擎循环存活:已 started 只会 Notify;未 started(重启后未恢复的终态任务)则重起循环。
|
|
s.engine.Run(s.ctx, t)
|
|
// 暂停中 → 解除暂停(清引擎内存态 + Notify),并持久化 paused=false(存活过重启)。
|
|
if t.Paused || s.engine.IsPaused(t.ID) {
|
|
s.engine.Resume(t)
|
|
t.Paused = false
|
|
if err := s.m.SetTaskPaused(t.ID, false); err != nil {
|
|
log.Printf("[revive] task %s 解除暂停失败: %v", t.ID, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
// createGoals materializes the goal node(s) under the task root (rel objective).
|
|
// Decomposition is done ENTIRELY by the LLM (the project requires an LLM). There is
|
|
// no rule-based fallback splitter — it only ever produced garbage (shredded URLs,
|
|
// meaningless 2-way splits). If the LLM yields nothing (an error), the raw task goal
|
|
// is used verbatim as a single goal so the task still has something to judge against.
|
|
// Returns the seeded specs so callers can emit activity records for them.
|
|
// emit, when non-nil, is forwarded to DecomposeGoals so LLM steps are visible in the UI.
|
|
func (s *Server) createGoals(ctx context.Context, t *Task, emit func(db.Activity)) []goalSpec {
|
|
// Use the SAME LLM the task runs on (its pinned profile, else the active profile),
|
|
// NOT agent.FromEnv() — the LLM is configured via the UI (DB profile), not env vars,
|
|
// so FromEnv returned empty and every task silently fell back to the crude rule
|
|
// splitter (which shredded URLs / made meaningless 2-way splits).
|
|
var specs []goalSpec
|
|
if cfg, ok := s.goalsLLMConfig(t); ok {
|
|
var as *db.AssetStore
|
|
if s.m != nil {
|
|
as = s.m.Assets()
|
|
}
|
|
taskID, _ := strconv.ParseInt(t.ID, 10, 64)
|
|
// DecomposeGoals persists each decomposed goal via the set_goals tool straight
|
|
// into t.Store; it returns the specs it wrote so we can emit activity + spot the
|
|
// empty (LLM error / no provider) case below. No write-back needed here anymore.
|
|
for _, g := range agent.DecomposeGoals(ctx, cfg, s.m.dir, t.Goal, t.Description, as, t.Store, taskID, emit) {
|
|
if strings.TrimSpace(g.Text) != "" {
|
|
specs = append(specs, goalSpec{Text: g.Text, VulnClass: g.VulnClass})
|
|
}
|
|
}
|
|
}
|
|
if len(specs) == 0 {
|
|
// No decomposed goals (LLM error / no provider): use the raw task goal verbatim
|
|
// as a single goal so the task still has something to judge against. This is the
|
|
// only path that writes here — decomposed goals are already persisted by the tool.
|
|
if g := strings.TrimSpace(t.Goal); g != "" {
|
|
log.Printf("[goals] task %s: LLM 目标拆解无产出,回退为「原始目标作为单目标」", t.ID)
|
|
origin, _ := t.Store.OriginFactID()
|
|
id, _ := t.Store.AddNode(db.KindGoal, map[string]any{"text": g}, 0, "open", "system", nil)
|
|
if origin > 0 && id > 0 {
|
|
_ = t.Store.Link(origin, db.RelSpawns, id) // goal descends from the task root (origin fact)
|
|
}
|
|
specs = []goalSpec{{Text: g}}
|
|
}
|
|
}
|
|
return specs
|
|
}
|
|
|
|
// goalsLLMConfig resolves the LLM config for goal decomposition by the standard
|
|
// precedence: the goals agent's own binding → the task's pinned profile → the global
|
|
// active profile (same source the engine runs on).
|
|
func (s *Server) goalsLLMConfig(t *Task) (agent.Config, bool) {
|
|
var pin *int64
|
|
if t != nil {
|
|
pin = t.LLMProfileID
|
|
}
|
|
if eff := s.effectiveProfileForAgent("goals", pin); eff != nil {
|
|
if cfg, ok := s.loadProfileConfig(*eff); ok {
|
|
return cfg, true
|
|
}
|
|
}
|
|
return s.loadLLMConfig()
|
|
}
|