Files
ARTEX/server/goals.go
T
Autumn-27andClaude Opus 4.8 e841bc9332 feat(goals): 目标设定 agent 提取并登记测试资产范围
- DecomposeGoals 新增 as/taskID 参数,有效时复用 add_task_scope 工具挂入本次调用
- 追加 code-owned tail(仅工具挂上时):从目标/描述提取明确的
  root_domain/subdomain/ip/cidr 并登记为 task_scope;只登记明确写出的、
  拿不准选更精确的;不处理 company(任务刚建公司通常未入库、名字解析不上,
  留给 plan 阶段)
- MaxTurns 4→6;server createGoals 传入 AssetStore + 数字 taskID

Co-Authored-By: Claude Opus 4.8 <[email protected]>
2026-08-08 12:30:51 -04:00

75 lines
2.7 KiB
Go

package server
import (
"context"
"log"
"strconv"
"strings"
"github.com/Autumn-27/artex/agent"
"github.com/Autumn-27/artex/db"
)
type goalSpec struct {
Text string
VulnClass string
}
// 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)
for _, g := range agent.DecomposeGoals(ctx, cfg, t.Goal, t.Description, as, taskID, emit) {
if strings.TrimSpace(g.Text) != "" {
specs = append(specs, goalSpec{Text: g.Text, VulnClass: g.VulnClass})
}
}
}
if len(specs) == 0 {
// No rule-based splitting: use the raw task goal verbatim as a single goal
// (only reached on an LLM error — normal runs get real decomposed goals).
if g := strings.TrimSpace(t.Goal); g != "" {
log.Printf("[goals] task %s: LLM 目标拆解无产出,回退为「原始目标作为单目标」", t.ID)
specs = []goalSpec{{Text: g}}
}
}
origin, _ := t.Store.OriginFactID()
for _, g := range specs {
payload := map[string]any{"text": g.Text}
if g.VulnClass != "" {
payload["vulnclass"] = g.VulnClass
}
id, _ := t.Store.AddNode(db.KindGoal, payload, 0, "open", "system", nil)
if origin > 0 && id > 0 {
_ = t.Store.Link(origin, db.RelSpawns, id) // goals descend from the task root (origin fact)
}
}
return specs
}
// goalsLLMConfig resolves the LLM config for goal decomposition: the task's pinned
// profile if any, else the global active profile (same source the engine runs on).
func (s *Server) goalsLLMConfig(t *Task) (agent.Config, bool) {
if t != nil && t.LLMProfileID != nil {
if cfg, ok := s.loadProfileConfig(*t.LLMProfileID); ok {
return cfg, true
}
}
return s.loadLLMConfig()
}