mirror of
https://github.com/skilfoy/ARTEX-English.git
synced 2026-12-04 05:25:52 +00:00
2142 lines
97 KiB
Go
2142 lines
97 KiB
Go
package agent
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
|
|
acperm "github.com/Autumn-27/norma/permission"
|
|
actool "github.com/Autumn-27/norma/tool"
|
|
"github.com/skilfoy/ARTEX-English/db"
|
|
)
|
|
|
|
// compactIntents distills intents to {id, summary, state, asset_ids, parents,
|
|
// yields} so the planner sees both the direction and its LINEAGE — parents (the
|
|
// upstream nodes it derived from: facts/intents/findings) and yields (the facts/
|
|
// findings it produced) — without pulling full payloads. parentsOf/yieldsOf are
|
|
// built from the exploration edges in graph_overview.
|
|
func compactIntents(ns []*db.Node, parentsOf, yieldsOf map[int64][]int64) []map[string]any {
|
|
out := make([]map[string]any, 0, len(ns))
|
|
for _, n := range ns {
|
|
var p map[string]any
|
|
_ = json.Unmarshal(n.Payload, &p)
|
|
m := map[string]any{"id": n.ID, "summary": p["summary"], "state": n.State}
|
|
if n.Inherited {
|
|
m["source_task_id"] = n.SourceTaskID
|
|
m["inherited"] = true
|
|
}
|
|
// asset_ids is the structured "which assets this direction covers" signal for
|
|
// dedup; fall back to legacy payload keys (target_ids plural, then target_id
|
|
// single) so intents stored before the rename still surface their anchors.
|
|
if tg, ok := p["asset_ids"]; ok && tg != nil {
|
|
m["asset_ids"] = tg
|
|
} else if tg, ok := p["target_ids"]; ok && tg != nil {
|
|
m["asset_ids"] = tg
|
|
} else if tg, ok := p["target_id"]; ok && tg != nil && tg != "" {
|
|
m["asset_ids"] = []any{tg}
|
|
}
|
|
if ps := parentsOf[n.ID]; len(ps) > 0 {
|
|
m["parents"] = ps // Upstream: from which nodes the intent is derived (multiple facts may produce an intention together))
|
|
}
|
|
if ys := yieldsOf[n.ID]; len(ys) > 0 {
|
|
m["yields"] = ys // Downstream: What was the intent to produce?/Discover
|
|
}
|
|
out = append(out, m)
|
|
}
|
|
return out
|
|
}
|
|
|
|
// ToolSet exposes the PG-backed dual graph (asset + exploration) to an LLM agent.
|
|
// One ToolSet is created per planner/worker run; per-run signals live here.
|
|
type ToolSet struct {
|
|
findingRecorder FindingRecorder
|
|
as *db.AssetStore // asset store (optional; nil = asset tools not available)
|
|
cs *db.CompanyStore // company store (optional)
|
|
ts *db.ExplorationStore
|
|
worker string
|
|
taskID int64 // PG tasks.id; 0 when unknown (tests / orchestrator cross-task reads)
|
|
// coverageDisabled mirrors tasks.coverage_enabled=false. Stored inverted so the
|
|
// zero value (all existing ToolSet constructions) means ENABLED — matching the
|
|
// DB default (true). When true: graphOverviewData drops the coverage block, the
|
|
// auto-scope hook (insertAssets) is skipped, and add_task_scope/list_untested_assets
|
|
// are filtered out of the agent's tool list. The scope field stays regardless.
|
|
coverageDisabled bool
|
|
// ownerNode is the exploration node that writes attach to: assets this run
|
|
// touches get anchored to it as lineage/provenance (NOT visibility — the asset
|
|
// graph is global and shared). Worker = its claimed intent; planner = begin root.
|
|
ownerNode int64
|
|
GoalMet bool
|
|
Reason string
|
|
writes WriteCounts
|
|
// killWork, if set, terminates a running work by intent id (engine callback,
|
|
// wired by the planner). nil = the kill_work tool reports unavailable.
|
|
killWork func(intentID int64) error
|
|
// steerWork, if set, queues a mid-run course-correction for the work running an
|
|
// intent id (engine callback, wired by the planner): the worker injects it before
|
|
// its next tool call and re-plans, without being killed. nil = tool unavailable.
|
|
steerWork func(intentID int64, msg string) error
|
|
// enrich, if set, receives async auto-completion triggers (DNS resolve for a
|
|
// domain, HTTP probe for a site). nil = no engine enrichment.
|
|
enrich EnrichTrigger
|
|
// notify, if set, wakes the task's planner after a graph change that should be
|
|
// re-planned promptly (currently: a new hint). nil = no wake (the hint is still
|
|
// stored and read on the next round triggered by other events). debounced.
|
|
notify func()
|
|
// notifyFinding, if set, wakes the task's planner when this run reports a finding,
|
|
// carrying (intentID, summary) so the round can spell out which intent found what.
|
|
// Wired for workers; nil elsewhere → falls back to notify (bare wake).
|
|
notifyFinding func(intentID int64, summary string)
|
|
// resumeTask, if set, revives the task after a graph change that should make a
|
|
// stopped task run again (currently: set_goals adds a goal). It flips a terminal/
|
|
// paused task back to running and (re)starts the engine loops — a plain notify()
|
|
// can't, because the planner's terminal gate swallows wakes. Wired ONLY for the
|
|
// main agent (human steering); nil for the goals decomposer and workers.
|
|
resumeTask func()
|
|
// notifyGoal, if set, wakes the planner AND records ONE "the operator added N goals: …" trigger
|
|
// for a whole set_goals call (batch-aware — one call, one trigger, not one per goal)
|
|
// so the next round spells out the added goals (instead of the planner having to
|
|
// spot new open goals in the overview). Wired ONLY for the main agent; nil for the
|
|
// goals decomposer (round-0 has no running planner to inform) and workers → those
|
|
// fall back to the bare notify.
|
|
notifyGoal func(texts []string)
|
|
// notifyHint, if set, wakes the planner AND records ONE "the operator added N strategic hints: …"
|
|
// trigger for a whole add_hint call (batch-aware — one call, one trigger) so the next
|
|
// round is told the round was fired by a new hint and spells the hint out, instead of
|
|
// the planner having to spot it folded into the graph overview. Wired for the main
|
|
// agent + cross-task orchestration; nil elsewhere → falls back to the bare notify.
|
|
notifyHint func(texts []string)
|
|
}
|
|
|
|
// SetNotifyGoal wires the goal-add trigger callback (see ToolSet.notifyGoal). Set only
|
|
// by the main-agent chat, so runtime-added goals are announced to the planner by name.
|
|
func (t *ToolSet) SetNotifyGoal(fn func([]string)) { t.notifyGoal = fn }
|
|
|
|
// SetNotifyHint wires the hint-add trigger callback (see ToolSet.notifyHint). Set by
|
|
// the main-agent chat and cross-task orchestration, so a runtime-added hint fires a
|
|
// planner round announced by name instead of a bare wake.
|
|
func (t *ToolSet) SetNotifyHint(fn func([]string)) { t.notifyHint = fn }
|
|
|
|
// SetResumeTask wires the task-revive callback (see ToolSet.resumeTask). Set only by
|
|
// the main-agent chat, so runtime-added goals can pull a finished task back to running.
|
|
func (t *ToolSet) SetResumeTask(fn func()) { t.resumeTask = fn }
|
|
|
|
// SetNotify wires the planner-wake callback (see ToolSet.notify). Set by callers
|
|
// that hold the task handle (main-agent chat, cross-task orchestration).
|
|
func (t *ToolSet) SetNotify(fn func()) { t.notify = fn }
|
|
|
|
// SetNotifyFinding wires the finding-wake callback (see ToolSet.notifyFinding).
|
|
func (t *ToolSet) SetNotifyFinding(fn func(int64, string)) { t.notifyFinding = fn }
|
|
|
|
// EnrichTrigger is the enrichment engine seen from the tool layer (see package
|
|
// enrich). Kept as an interface here to avoid coupling agent → enrich.
|
|
type EnrichTrigger interface {
|
|
ResolveDomain(id int64, host string)
|
|
ProbeSite(id int64, url string)
|
|
}
|
|
|
|
// WriteCounts breaks down what a worker persisted this run, by node kind, so the
|
|
// engine can log an accurate "wrote back" summary instead of lumping assets and
|
|
// findings under "facts" (record_fact → Facts, insert_assets → Assets,
|
|
// report_finding → Findings; each element of a batch counts once).
|
|
type WriteCounts struct {
|
|
Facts int
|
|
Assets int
|
|
Findings int
|
|
}
|
|
|
|
// Total is every node persisted this run, regardless of kind — the
|
|
// "explored but persisted nothing" signal (Total == 0).
|
|
func (w WriteCounts) Total() int { return w.Facts + w.Assets + w.Findings }
|
|
|
|
// String renders the per-kind breakdown for logs, e.g. "facts 1, assets 25, findings 0".
|
|
func (w WriteCounts) String() string {
|
|
return fmt.Sprintf("facts %d, assets %d, findings %d", w.Facts, w.Assets, w.Findings)
|
|
}
|
|
|
|
// Writes reports what this run wrote back, split by node kind (so the engine can
|
|
// tell "explored but persisted nothing" apart from a completed intent, and log an
|
|
// honest breakdown instead of calling assets/findings "facts").
|
|
func (t *ToolSet) Writes() WriteCounts { return t.writes }
|
|
|
|
func NewToolSet(ts *db.ExplorationStore, worker string) *ToolSet {
|
|
return &ToolSet{ts: ts, worker: worker}
|
|
}
|
|
|
|
// SetTaskID sets the PG task id on this ToolSet so that report_finding can
|
|
// dual-write to the standalone findings table (which survives task deletion).
|
|
func (t *ToolSet) SetTaskID(id int64) { t.taskID = id }
|
|
|
|
// SetCoverageEnabled records whether this task has the asset-coverage feature on
|
|
// (default enabled). Passing false makes graphOverviewData omit the coverage block
|
|
// and DropCoverageTools filter the two coverage-only tools out of the agent's tool
|
|
// list. It does NOT stop scope accumulation: insertAssets' auto-scope hook runs
|
|
// either way, because task_scope is the task's range boundary (the filter basis for
|
|
// asset queries), not merely a coverage denominator.
|
|
func (t *ToolSet) SetCoverageEnabled(enabled bool) { t.coverageDisabled = !enabled }
|
|
|
|
// CoverageDisabled reports whether the coverage feature is off for this task.
|
|
func (t *ToolSet) CoverageDisabled() bool { return t.coverageDisabled }
|
|
|
|
// coverageOnlyTools are the LLM tools that only make sense when asset coverage is
|
|
// on. When the feature is off they are filtered out of the agent's tool list so
|
|
// they neither pollute the prompt nor let the model build a disabled denominator.
|
|
// add_task_scope is deliberately NOT here: task_scope is the task's range boundary
|
|
// (the filter basis for asset queries), not merely a coverage denominator, so the
|
|
// agents that own the scope definition keep it either way — in lockstep with insertAssets'
|
|
// auto-scope hook, which also runs regardless of the switch.
|
|
var coverageOnlyTools = map[string]bool{"list_untested_assets": true}
|
|
|
|
// DropCoverageTools returns tools with the coverage-only ones removed when this
|
|
// task has the feature disabled; otherwise it returns tools unchanged.
|
|
func (t *ToolSet) DropCoverageTools(tools []actool.CoreTool) []actool.CoreTool {
|
|
if !t.coverageDisabled {
|
|
return tools
|
|
}
|
|
out := tools[:0:0]
|
|
for _, tool := range tools {
|
|
if coverageOnlyTools[tool.Name()] {
|
|
continue
|
|
}
|
|
out = append(out, tool)
|
|
}
|
|
return out
|
|
}
|
|
|
|
// Cross-task reuse: exported accessors returning the per-task tool logic bound to
|
|
// THIS ToolSet's store. Host-side orchestration tools build a ToolSet for an
|
|
// arbitrary task, then Call these — so cross-task reads/hint reuse the exact
|
|
// same logic as the in-task tools. (readTool ignores ToolContext, so Call(…,nil)
|
|
// is safe; add_hint is a writeTool but also doesn't deref the context here.)
|
|
func (t *ToolSet) GraphOverviewTool() actool.CoreTool { return t.graphOverview() }
|
|
func (t *ToolSet) ListFindingsTool() actool.CoreTool { return t.listFindings() }
|
|
func (t *ToolSet) GetWorkerTraceTool() actool.CoreTool { return t.getWorkerTrace() }
|
|
func (t *ToolSet) ListWorkerTracesTool() actool.CoreTool { return t.listWorkerTraces() }
|
|
func (t *ToolSet) SearchWorkerTracesTool() actool.CoreTool { return t.searchAllWorkerTraces() }
|
|
func (t *ToolSet) NodeDetailTool() actool.CoreTool { return t.nodeDetail() }
|
|
func (t *ToolSet) AddHintTool() actool.CoreTool { return t.addHint() }
|
|
|
|
// SetEnrich wires the async enrichment engine (DNS/HTTP auto-completion).
|
|
func (t *ToolSet) SetEnrich(e EnrichTrigger) { t.enrich = e }
|
|
|
|
// SetOwnerNode sets the exploration node that writes anchor to (worker: its
|
|
// intent node; planner/main: the begin root). Assets created/referenced while
|
|
// ownerNode is set are anchored to it as lineage (not visibility).
|
|
func (t *ToolSet) SetOwnerNode(id int64) { t.ownerNode = id }
|
|
|
|
// anchorOwner records a lineage edge from this run's owner node to an asset
|
|
// (no-op if unset). Provenance only — the asset graph is global and shared, so
|
|
// this no longer affects which assets a task can read.
|
|
func (t *ToolSet) anchorOwner(assetID int64) {
|
|
if t.ts != nil && t.ownerNode > 0 && assetID > 0 {
|
|
_ = t.ts.Anchor(t.ownerNode, assetID)
|
|
}
|
|
}
|
|
|
|
// pid parses an id that may arrive as a JSON number or string ("" / 0 → 0).
|
|
func pid(raw json.RawMessage) int64 {
|
|
if len(raw) == 0 {
|
|
return 0
|
|
}
|
|
var n int64
|
|
if json.Unmarshal(raw, &n) == nil {
|
|
return n
|
|
}
|
|
var s string
|
|
if json.Unmarshal(raw, &s) == nil {
|
|
v, _ := strconv.ParseInt(strings.TrimSpace(s), 10, 64)
|
|
return v
|
|
}
|
|
return 0
|
|
}
|
|
|
|
// pidList parses a list of ids (number|string), dropping zeros/invalids.
|
|
func pidList(raw []json.RawMessage) []int64 {
|
|
var out []int64
|
|
for _, r := range raw {
|
|
if v := pid(r); v > 0 {
|
|
out = append(out, v)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
func obj(props map[string]any, required ...string) map[string]any {
|
|
m := map[string]any{"type": "object", "properties": props}
|
|
if len(required) > 0 {
|
|
m["required"] = required
|
|
}
|
|
return m
|
|
}
|
|
func str(desc string) map[string]any { return map[string]any{"type": "string", "description": desc} }
|
|
func intp(desc string) map[string]any {
|
|
return map[string]any{"type": "integer", "description": desc}
|
|
}
|
|
func idp(desc string) map[string]any {
|
|
return map[string]any{"type": "integer", "description": desc}
|
|
}
|
|
|
|
func readTool(name, desc string, schema map[string]any, run func(context.Context, json.RawMessage) (actool.Result, error)) actool.CoreTool {
|
|
return actool.Build(actool.Spec{
|
|
Name: name, Description: desc, Schema: schema,
|
|
ReadOnly: func(json.RawMessage) bool { return true },
|
|
Concurrent: func(json.RawMessage) bool { return true },
|
|
Permissions: func(context.Context, json.RawMessage, acperm.Context) acperm.Decision { return acperm.Allowed() },
|
|
Run: func(ctx context.Context, in json.RawMessage, _ *actool.ToolContext) (actool.Result, error) {
|
|
return run(ctx, in)
|
|
},
|
|
})
|
|
}
|
|
|
|
func writeTool(name, desc string, schema map[string]any, run func(context.Context, json.RawMessage) (actool.Result, error)) actool.CoreTool {
|
|
return actool.Build(actool.Spec{
|
|
Name: name, Description: desc, Schema: schema,
|
|
Permissions: func(context.Context, json.RawMessage, acperm.Context) acperm.Decision { return acperm.Allowed() },
|
|
Run: func(ctx context.Context, in json.RawMessage, _ *actool.ToolContext) (actool.Result, error) {
|
|
return run(ctx, in)
|
|
},
|
|
})
|
|
}
|
|
|
|
// readExpTool / writeExpTool build a domain tool whose handler dereferences the
|
|
// task-bound ExplorationStore. Two ToolSets carry a nil store: the catalog's
|
|
// seed-only shell (never called) and the server-level one behind buildDomainReg,
|
|
// which the tools table can bind to ANY agent — including ones that never run
|
|
// inside a task (auto/pentest/reporter/Customized agent/Bypass question). Refusing there
|
|
// keeps a mis-bound tool a bad tool call; without the guard it was a nil deref,
|
|
// and tool handlers run on the harness's own goroutine, so the panic is out of
|
|
// reach of every recover() in the server and kills the whole process.
|
|
func (t *ToolSet) readExpTool(name, desc string, schema map[string]any, run func(context.Context, json.RawMessage) (actool.Result, error)) actool.CoreTool {
|
|
return readTool(name, desc, schema, t.needExploration(name, run))
|
|
}
|
|
|
|
func (t *ToolSet) writeExpTool(name, desc string, schema map[string]any, run func(context.Context, json.RawMessage) (actool.Result, error)) actool.CoreTool {
|
|
return writeTool(name, desc, schema, t.needExploration(name, run))
|
|
}
|
|
|
|
// needExploration wraps a handler so it only runs with an exploration store.
|
|
// Tools that degrade more usefully than "unavailable" (report_finding points at
|
|
// add_task_hint, set_goals/set_constraints at the task itself) keep their own
|
|
// bespoke guard instead.
|
|
func (t *ToolSet) needExploration(name string, run func(context.Context, json.RawMessage) (actool.Result, error)) func(context.Context, json.RawMessage) (actool.Result, error) {
|
|
return func(ctx context.Context, in json.RawMessage) (actool.Result, error) {
|
|
if t.ts == nil {
|
|
return actool.Errorf(name + ": Task Context is required (the exploration graph). This agent is not running inside a task, so the task graph is unavailable and this tool cannot be used. Use it inside a task, or use a cross-task read tool that takes task_id (get_task_node_detail, list_task_findings, get_task_graph, and similar)."), nil
|
|
}
|
|
return run(ctx, in)
|
|
}
|
|
}
|
|
|
|
func jsonResult(v any) (actool.Result, error) {
|
|
b, err := json.Marshal(v)
|
|
if err != nil {
|
|
return actool.Errorf(err.Error()), nil
|
|
}
|
|
return actool.Text(string(b)), nil
|
|
}
|
|
|
|
// --- read tools (planner + worker) ---
|
|
|
|
func (t *ToolSet) graphOverview() actool.CoreTool {
|
|
return t.readExpTool("graph_overview",
|
|
"(Exploration graph) Distilled snapshot of the exploration: asset counts, sites with no interface, the frontier, findings, and hints (strategic notes from the operator or the main agent, which must be taken into account when generating intents). Call this first when planning.",
|
|
obj(map[string]any{}),
|
|
func(context.Context, json.RawMessage) (actool.Result, error) {
|
|
return jsonResult(t.graphOverviewData())
|
|
})
|
|
}
|
|
|
|
// graphOverviewData computes the distilled situational snapshot shared by the
|
|
// graph_overview tool and the planner's wake-up prompt (which pre-injects it so
|
|
// the model needn't spend a turn calling the tool — every plan round starts with
|
|
// an empty context and always needs this first).
|
|
func (t *ToolSet) graphOverviewData() map[string]any {
|
|
out := map[string]any{}
|
|
// goals summary folded in so the planner needn't call list_goals each round.
|
|
goals, _ := t.ts.ListByKind(db.KindGoal, 100)
|
|
gsum := make([]map[string]any, 0, len(goals))
|
|
for _, g := range goals {
|
|
var p map[string]any
|
|
_ = json.Unmarshal(g.Payload, &p)
|
|
gsum = append(gsum, map[string]any{"id": g.ID, "state": g.State, "text": p["text"]})
|
|
}
|
|
out["goals"] = gsum
|
|
// hints: strategic notes the operator or the main agent attached with add_hint;
|
|
// folded in so the planner reads them every round when generating intents
|
|
// (otherwise they are written and never read).
|
|
hints, _ := t.ts.ListByKind(db.KindHint, 50)
|
|
hsum := make([]map[string]any, 0, len(hints))
|
|
for _, h := range hints {
|
|
var p map[string]any
|
|
_ = json.Unmarshal(h.Payload, &p)
|
|
hint := map[string]any{"id": h.ID, "state": h.State, "text": p["text"]}
|
|
if findingTrafficBindingEnabled() && p["traffic_refs"] != nil {
|
|
hint["traffic_refs"] = p["traffic_refs"]
|
|
}
|
|
hsum = append(hsum, hint)
|
|
}
|
|
out["hints"] = hsum
|
|
// lineage from the exploration edges: an intent's parents (what it
|
|
// derived_from — possibly several facts combined) and its yields (the
|
|
// facts/findings it produced). factFrom maps a fact → the intent that
|
|
// produced it. This is the relationship layer the flat lists lacked.
|
|
edges, _ := t.ts.Edges(5000)
|
|
parentsOf := map[int64][]int64{}
|
|
yieldsOf := map[int64][]int64{}
|
|
factFrom := map[int64]int64{}
|
|
for _, e := range edges {
|
|
switch e.Rel {
|
|
case db.RelDerivedFrom, db.RelSpawns: // upstream: derived_from (fact/finding/intent→intent) or spawns (origin fact→goal, legacy begin→intent)
|
|
parentsOf[e.To] = append(parentsOf[e.To], e.From)
|
|
case db.RelYields: // intent --yields--> fact/finding
|
|
yieldsOf[e.From] = append(yieldsOf[e.From], e.To)
|
|
factFrom[e.To] = e.From
|
|
}
|
|
}
|
|
// cold-digest §6: members folded into an active digest are shown via cold_digests
|
|
// (below), not the flat recent_* lists. `covered` maps member id → its digest id.
|
|
// §6 render-time revival check: a covered member that has become hot again (a new
|
|
// intent derived from it) must reappear this round — so `hidden` folds a member out
|
|
// only when it is covered AND still cold.
|
|
covered, _ := t.ts.CoveredMembers()
|
|
// Render-time hot set (ancestor of a live intent / fact under a live intent).
|
|
// §6 revival check: a covered member that revived (now hot) must NOT stay folded
|
|
// — hidden() only folds a member out when it is covered AND still cold. Computed
|
|
// every round (cheap for real graph sizes); nil map degrades safely.
|
|
var hotAtRender map[int64]bool
|
|
if cg, _, err := loadColdGraph(t.ts); err == nil {
|
|
hotAtRender = cg.hotSet()
|
|
}
|
|
hidden := func(id int64) bool { _, c := covered[id]; return c && !hotAtRender[id] }
|
|
const openIntentsCap = 30
|
|
fr, _ := t.ts.Frontier(openIntentsCap) // priority DESC, id ASC — the N highest-priority rows; the true total is frontier_open
|
|
out["open_intents"] = compactIntents(fr, parentsOf, yieldsOf)
|
|
all, _ := t.ts.ListByKind(db.KindIntent, 300)
|
|
var running, recentDone []*db.Node
|
|
for _, n := range all {
|
|
switch n.State {
|
|
case "running":
|
|
running = append(running, n)
|
|
case "done", "blocked", "exhausted":
|
|
if hidden(n.ID) {
|
|
continue // in a cold_digest and still cold — shown via cold_digests (§6.2)
|
|
}
|
|
recentDone = append(recentDone, n) // newest first (all is id descending); folded ones are already dropped, then capped to the latest N
|
|
}
|
|
}
|
|
out["running_intents"] = compactIntents(running, parentsOf, yieldsOf)
|
|
// done_intents_total: count of finished intents (done/blocked/exhausted), named
|
|
// in parallel with recent_done_intents — the latter is only the newest window.
|
|
// Side by side they read as "what you see is N of the total", so the planner
|
|
// does not treat "not shown" as "never dispatched". No extra prompt is needed.
|
|
if dt, err := t.ts.CountFinishedIntents(); err == nil {
|
|
out["done_intents_total"] = dt
|
|
}
|
|
// frontier_open: the real count of open intents (open_intents is only the
|
|
// highest-priority N, a truncated view).
|
|
if fo, err := t.ts.CountOpenIntents(); err == nil {
|
|
out["frontier_open"] = fo
|
|
} else {
|
|
out["frontier_open"] = len(fr)
|
|
}
|
|
// findings (confirmed vulns) and facts (worker exploration results) are
|
|
// now distinct node kinds. recent_facts surfaces fact summaries (esp.
|
|
// negative results) so the planner sees them in one call; full content
|
|
// via node_detail(id).
|
|
vulnNodes, _ := t.ts.ListByKind(db.KindFinding, 1000)
|
|
factNodes, _ := t.ts.ListByKind(db.KindFact, 1000) // newest first
|
|
out["findings_total"] = len(vulnNodes) // confirmed-finding count (goal judgment reads this); details are in finding_list (newest window)
|
|
out["facts"] = len(factNodes) // exploration facts and conclusions, including negative ones
|
|
// Findings are the highest-value product of the task, so the overview carries
|
|
// the newest window (at most 10; vulnNodes are already id-descending, newest first).
|
|
// The planner sees recently confirmed findings each round; the full set and older
|
|
// ones come from list_findings. Each row is only {id, summary, from_intent?}:
|
|
// from_intent is the intent that produced this finding. evidence, assets,
|
|
// vulnclass, severity, and state are still available from list_findings or node_detail(id).
|
|
const findingListCap = 10
|
|
findingList := make([]map[string]any, 0, findingListCap)
|
|
for _, n := range vulnNodes {
|
|
if len(findingList) >= findingListCap {
|
|
break
|
|
}
|
|
var fp map[string]any
|
|
_ = json.Unmarshal(n.Payload, &fp)
|
|
m := map[string]any{"id": n.ID, "summary": fp["summary"]}
|
|
if from := factFrom[n.ID]; from > 0 {
|
|
m["from_intent"] = from // which intent produced this finding
|
|
}
|
|
findingList = append(findingList, m)
|
|
}
|
|
out["finding_list"] = findingList
|
|
// recent_facts: the newest window of facts that are not folded (at most N;
|
|
// factNodes are id-descending, newest first). Facts folded into a digest and
|
|
// still cold (hidden) are shown via cold_digests, not repeated here. Each row is
|
|
// {id, summary, from_intent?, confidence?}; evidence and other detail use
|
|
// node_detail(id). Older facts are paged with list_facts.
|
|
const recentFactsCap = 20
|
|
recentFacts := make([]map[string]any, 0, recentFactsCap)
|
|
for _, n := range factNodes {
|
|
if len(recentFacts) >= recentFactsCap {
|
|
break
|
|
}
|
|
if hidden(n.ID) {
|
|
continue // folded into a digest and still cold — see cold_digests
|
|
}
|
|
m := compactNode(n)
|
|
if from := factFrom[n.ID]; from > 0 {
|
|
m["from_intent"] = from // which intent produced this fact
|
|
}
|
|
// confidence is included so the planner can see which conclusion is only
|
|
// inferred (especially a negative one, which must not be treated as settled).
|
|
// evidence is longer and stays in node_detail(id).
|
|
var fp map[string]any
|
|
if json.Unmarshal(n.Payload, &fp) == nil {
|
|
if c, ok := fp["confidence"].(string); ok && c != "" {
|
|
m["confidence"] = c
|
|
}
|
|
}
|
|
recentFacts = append(recentFacts, m)
|
|
}
|
|
out["recent_facts"] = recentFacts
|
|
// recent_done_intents: the newest window of finished intents that are not folded
|
|
// (at most N; recentDone is already id-descending). Older ones are the
|
|
// done_intents_total count plus node_detail(id).
|
|
const recentDoneCap = 12
|
|
if len(recentDone) > recentDoneCap {
|
|
recentDone = recentDone[:recentDoneCap]
|
|
}
|
|
out["recent_done_intents"] = compactIntents(recentDone, parentsOf, yieldsOf)
|
|
// cold-digest §6.1: digest bodies for the folded cold region, the N whose newest
|
|
// member is most recent. Older digests that were cut off are given as bare ids
|
|
// (still expandable with expand_digest), so the only exit from the cold region
|
|
// does not grow without bound.
|
|
const coldDigestsCap = 15
|
|
if cds, more := coldDigestsRecent(t.ts, coldDigestsCap); len(cds) > 0 {
|
|
out["cold_digests"] = cds // [{id, body, member_count}] — read body directly (§6.1)
|
|
if len(more) > 0 {
|
|
out["cold_digests_more"] = more // ids of older digests that were cut off; expand with expand_digest(id)
|
|
}
|
|
}
|
|
// the original task (root) so the planner always has it, not just the
|
|
// decomposed goals.
|
|
if description, goal, err := t.ts.Root(); err == nil {
|
|
out["task"] = map[string]any{"description": description, "goal": goal}
|
|
}
|
|
// Direct source tasks are a live, read-only blackboard view. Keep their
|
|
// summaries in a separate field so their intents never enter this task's
|
|
// frontier or get mistaken for locally claimable work.
|
|
out["related_tasks"] = t.relatedTaskOverviews()
|
|
// coverage: a rough reference for how much of the in-scope assets (task_scope)
|
|
// a fact has touched, plus by_type (total and tested per type). The agent
|
|
// decides whether to look up specific untested assets with list_untested_assets.
|
|
// Present only in a task context. When coverage is disabled (coverageDisabled),
|
|
// keep only host_count (so the model still knows how many target hosts there are)
|
|
// and drop denominator/tested/pct/by_type/note so those metrics do not pollute
|
|
// the context or tempt use of the hidden add_task_scope and list_untested_assets.
|
|
if t.as != nil && t.ts != nil && t.taskID > 0 {
|
|
{
|
|
m := map[string]any{}
|
|
if rows, err := t.as.ListTaskScopeWithSources(t.taskID); err == nil {
|
|
const maxScopeRows = 30
|
|
if len(rows) > maxScopeRows {
|
|
rows = rows[:maxScopeRows]
|
|
}
|
|
scope := make([]map[string]any, 0, len(rows))
|
|
for _, row := range rows {
|
|
entry := map[string]any{"kind": row.Kind, "source": row.Source, "task_id": row.TaskID}
|
|
switch {
|
|
case row.Domain != "":
|
|
entry["value"] = row.Domain
|
|
case row.Net != "":
|
|
entry["value"] = row.Net
|
|
case row.Value != "":
|
|
entry["value"] = row.Value
|
|
}
|
|
if row.CompanyID != nil {
|
|
entry["company_id"] = *row.CompanyID
|
|
entry["company_name"] = row.CompanyName
|
|
if t.cs != nil {
|
|
if rules, err := t.cs.GetScope(*row.CompanyID); err == nil {
|
|
companyScope := make([]map[string]any, 0, len(rules))
|
|
keywords := []string{}
|
|
for _, rule := range rules {
|
|
value := rule.Domain
|
|
if value == "" {
|
|
value = rule.Net
|
|
}
|
|
if value == "" {
|
|
value = rule.Value
|
|
}
|
|
if rule.Raw != "" {
|
|
value = rule.Raw
|
|
}
|
|
companyScope = append(companyScope, map[string]any{"kind": rule.Kind, "value": value})
|
|
if rule.Kind == "keyword" {
|
|
keywords = append(keywords, value)
|
|
}
|
|
}
|
|
entry["company_scope"] = companyScope
|
|
entry["company_keywords"] = keywords
|
|
}
|
|
}
|
|
}
|
|
scope = append(scope, entry)
|
|
}
|
|
m["scope"] = scope
|
|
}
|
|
if !t.coverageDisabled {
|
|
if cov, err := t.as.TaskCoverageWithSources(t.taskID); err == nil {
|
|
m["denominator"] = cov.Denominator
|
|
m["tested"] = cov.Tested
|
|
m["by_type"] = cov.ByType
|
|
m["note"] = "Asset-test coverage, including related assets such as interfaces. This is a rough estimate for reference only. It includes the current task and directly related tasks' scope and fact anchors; related scope is read-only. Container assets or a large enumeration make the number look low — do not treat that as 'already tested'. add_task_scope can widen this task's scope, and list_untested_assets lists untested assets. Usually do not call list_untested_assets; just keep the task moving."
|
|
if cov.Denominator == 0 {
|
|
m["pct"] = nil
|
|
m["status"] = "scope not anchored"
|
|
} else {
|
|
m["pct"] = cov.Pct
|
|
}
|
|
}
|
|
}
|
|
if hosts, err := t.as.HostsByTaskWithSources(t.taskID); err == nil {
|
|
// Host count only. Do not flatten the host list into graph_overview (on a wide
|
|
// task that is a large string repeated every round, and it rarely changes a
|
|
// planning decision). Look up specific hosts with list_assets when needed.
|
|
m["host_count"] = len(hosts)
|
|
}
|
|
if len(m) > 0 {
|
|
out["coverage"] = m
|
|
}
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
func inheritedMap(m map[string]any, sourceTaskID int64) map[string]any {
|
|
m["source_task_id"] = sourceTaskID
|
|
m["inherited"] = true
|
|
return m
|
|
}
|
|
|
|
const (
|
|
relatedOverviewTotalTextRunes = 48_000
|
|
relatedOverviewMaxTextPerSource = 8_000
|
|
relatedOverviewMaxGoalsPerSource = 8
|
|
relatedOverviewMaxHintsPerSource = 6
|
|
relatedOverviewMaxFactsPerSource = 12
|
|
relatedOverviewMaxFindingsPerTask = 6
|
|
relatedOverviewMaxIntentsPerTask = 8
|
|
relatedOverviewMaxScopePerSource = 12
|
|
relatedOverviewMaxDigestsPerSource = 6
|
|
)
|
|
|
|
// overviewTextBudget bounds inherited prompt text while preserving a fair slice
|
|
// for every direct source. Full evidence remains available through the on-demand
|
|
// read tools, so truncation here does not discard persisted blackboard data.
|
|
type overviewTextBudget struct {
|
|
remaining int
|
|
truncated bool
|
|
}
|
|
|
|
func relatedOverviewBudgetForSources(sourceCount int) int {
|
|
if sourceCount <= 0 {
|
|
return 0
|
|
}
|
|
if sourceCount > db.MaxTaskSourceCount {
|
|
sourceCount = db.MaxTaskSourceCount
|
|
}
|
|
perSource := relatedOverviewTotalTextRunes / sourceCount
|
|
if perSource > relatedOverviewMaxTextPerSource {
|
|
perSource = relatedOverviewMaxTextPerSource
|
|
}
|
|
return perSource
|
|
}
|
|
|
|
func (b *overviewTextBudget) take(value any, fieldLimit int) string {
|
|
var text string
|
|
switch value := value.(type) {
|
|
case string:
|
|
text = strings.TrimSpace(value)
|
|
case nil:
|
|
return ""
|
|
default:
|
|
text = strings.TrimSpace(fmt.Sprint(value))
|
|
}
|
|
if text == "" {
|
|
return ""
|
|
}
|
|
if b.remaining <= 0 || fieldLimit <= 0 {
|
|
b.truncated = true
|
|
return ""
|
|
}
|
|
runes := []rune(text)
|
|
limit := fieldLimit
|
|
if limit > b.remaining {
|
|
limit = b.remaining
|
|
}
|
|
if len(runes) > limit {
|
|
b.truncated = true
|
|
if limit == 1 {
|
|
text = "…"
|
|
} else {
|
|
text = string(runes[:limit-1]) + "…"
|
|
}
|
|
runes = []rune(text)
|
|
}
|
|
b.remaining -= len(runes)
|
|
return text
|
|
}
|
|
|
|
func recentTerminalIntents(store *db.ExplorationStore, limit int) []*db.Node {
|
|
if limit <= 0 {
|
|
return []*db.Node{}
|
|
}
|
|
const batch = 300
|
|
cursor := int64(0)
|
|
out := make([]*db.Node, 0, limit)
|
|
for len(out) < limit {
|
|
page, more, err := store.ListByKindPage(db.KindIntent, cursor, batch)
|
|
if err != nil || len(page) == 0 {
|
|
break
|
|
}
|
|
for _, intent := range page {
|
|
switch intent.State {
|
|
case "done", "blocked", "exhausted", "stopped":
|
|
out = append(out, intent)
|
|
}
|
|
if len(out) >= limit {
|
|
break
|
|
}
|
|
}
|
|
if !more {
|
|
break
|
|
}
|
|
cursor = page[len(page)-1].ID
|
|
}
|
|
return out
|
|
}
|
|
|
|
// relatedTaskOverviews distills persistent blackboard state from direct source
|
|
// tasks. It intentionally reads each source's local store methods, never its own
|
|
// related sources, so inheritance is one level only.
|
|
func (t *ToolSet) relatedTaskOverviews() []map[string]any {
|
|
sources, err := t.ts.DirectSourceStores()
|
|
if err != nil {
|
|
return []map[string]any{}
|
|
}
|
|
if len(sources) > db.MaxTaskSourceCount {
|
|
sources = sources[:db.MaxTaskSourceCount]
|
|
}
|
|
perSourceTextBudget := relatedOverviewBudgetForSources(len(sources))
|
|
out := make([]map[string]any, 0, len(sources))
|
|
for _, source := range sources {
|
|
ts := source.Store
|
|
// §2 cross-task: render the source task's OWN folded view — fold out the
|
|
// members it has already folded, and surface its cold_digests read-only.
|
|
hidden := hiddenMembersFor(ts)
|
|
budget := overviewTextBudget{remaining: perSourceTextBudget}
|
|
item := map[string]any{
|
|
"source_task_id": source.Task.TaskID,
|
|
"inherited": true,
|
|
"task": map[string]any{
|
|
"description": budget.take(source.Task.Description, 800),
|
|
"goal": budget.take(source.Task.Goal, 800),
|
|
"status": source.Task.Status,
|
|
},
|
|
}
|
|
stats, statsErr := ts.Stats()
|
|
|
|
edges, _ := ts.Edges(5000)
|
|
parentsOf := map[int64][]int64{}
|
|
yieldsOf := map[int64][]int64{}
|
|
factFrom := map[int64]int64{}
|
|
for _, edge := range edges {
|
|
switch edge.Rel {
|
|
case db.RelDerivedFrom, db.RelSpawns:
|
|
parentsOf[edge.To] = append(parentsOf[edge.To], edge.From)
|
|
case db.RelYields:
|
|
yieldsOf[edge.From] = append(yieldsOf[edge.From], edge.To)
|
|
factFrom[edge.To] = edge.From
|
|
}
|
|
}
|
|
|
|
goals, _ := ts.ListByKind(db.KindGoal, relatedOverviewMaxGoalsPerSource)
|
|
goalSummary := make([]map[string]any, 0, len(goals))
|
|
for _, goal := range goals {
|
|
var payload map[string]any
|
|
_ = json.Unmarshal(goal.Payload, &payload)
|
|
goalSummary = append(goalSummary, inheritedMap(map[string]any{
|
|
"id": goal.ID, "state": goal.State, "text": budget.take(payload["text"], 400),
|
|
}, source.Task.TaskID))
|
|
}
|
|
item["goals"] = goalSummary
|
|
|
|
hints, _ := ts.ListByKind(db.KindHint, relatedOverviewMaxHintsPerSource)
|
|
hintSummary := make([]map[string]any, 0, len(hints))
|
|
for _, hint := range hints {
|
|
var payload map[string]any
|
|
_ = json.Unmarshal(hint.Payload, &payload)
|
|
hintSummary = append(hintSummary, inheritedMap(map[string]any{
|
|
"id": hint.ID, "state": hint.State, "text": budget.take(payload["text"], 400),
|
|
}, source.Task.TaskID))
|
|
}
|
|
item["hints"] = hintSummary
|
|
|
|
facts, _ := ts.ListByKind(db.KindFact, relatedOverviewMaxFactsPerSource)
|
|
findings, _ := ts.ListByKind(db.KindFinding, relatedOverviewMaxFindingsPerTask)
|
|
intentNodes, _ := ts.ListByKind(db.KindIntent, 300)
|
|
terminalIntent := make(map[int64]bool, len(intentNodes))
|
|
for _, intent := range intentNodes {
|
|
terminalIntent[intent.ID] = inheritedIntentSummaryState(intent.State)
|
|
}
|
|
item["facts"] = len(facts)
|
|
item["findings"] = len(findings)
|
|
if statsErr == nil {
|
|
item["facts"] = stats[db.KindFact]
|
|
item["findings"] = stats[db.KindFinding]
|
|
if stats[db.KindGoal] > len(goals) || stats[db.KindHint] > len(hints) ||
|
|
stats[db.KindFact] > len(facts) || stats[db.KindFinding] > len(findings) {
|
|
budget.truncated = true
|
|
}
|
|
}
|
|
recentFindings := make([]map[string]any, 0, len(findings))
|
|
for _, finding := range findings {
|
|
entry := inheritedMap(compactFinding(finding), source.Task.TaskID)
|
|
entry["summary"] = budget.take(entry["summary"], 400)
|
|
recentFindings = append(recentFindings, entry)
|
|
}
|
|
item["recent_findings"] = recentFindings
|
|
recentFacts := make([]map[string]any, 0, len(facts))
|
|
for _, fact := range facts {
|
|
if hidden(fact.ID) {
|
|
continue // folded into this source's cold_digests — shown there (§2/§6.2)
|
|
}
|
|
m := inheritedMap(compactNode(fact), source.Task.TaskID)
|
|
m["summary"] = budget.take(m["summary"], 400)
|
|
if from := factFrom[fact.ID]; from > 0 && terminalIntent[from] {
|
|
m["from_intent"] = from
|
|
}
|
|
var payload map[string]any
|
|
if json.Unmarshal(fact.Payload, &payload) == nil {
|
|
if confidence, ok := payload["confidence"].(string); ok && confidence != "" {
|
|
m["confidence"] = confidence
|
|
}
|
|
}
|
|
recentFacts = append(recentFacts, m)
|
|
}
|
|
item["recent_facts"] = recentFacts
|
|
|
|
recentDoneRaw := recentTerminalIntents(ts, relatedOverviewMaxIntentsPerTask)
|
|
recentDone := recentDoneRaw[:0] // in-place filter: drop this source's folded intents (§2)
|
|
for _, intent := range recentDoneRaw {
|
|
if hidden(intent.ID) {
|
|
continue
|
|
}
|
|
recentDone = append(recentDone, intent)
|
|
}
|
|
for _, intent := range recentDone {
|
|
intent.Inherited = true
|
|
intent.SourceTaskID = source.Task.TaskID
|
|
}
|
|
intentResults := compactIntents(recentDone, parentsOf, yieldsOf)
|
|
for i, intent := range recentDone {
|
|
intentResults[i]["summary"] = budget.take(intentResults[i]["summary"], 400)
|
|
acts, _, err := ts.ActivityPageForTerminalIntent(intent.ID, 0, 20)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
var resultSummary, textFallback string
|
|
for _, activity := range acts {
|
|
switch activity.Kind {
|
|
case "result":
|
|
resultSummary = activity.Summary
|
|
case "text":
|
|
textFallback = activity.Summary
|
|
}
|
|
}
|
|
if resultSummary == "" {
|
|
resultSummary = textFallback
|
|
}
|
|
if resultSummary != "" {
|
|
intentResults[i]["result_summary"] = budget.take(resultSummary, 800)
|
|
}
|
|
}
|
|
item["recent_intent_results"] = intentResults
|
|
// §2 cross-task: the source task's folded cold region, read-only, newest-member
|
|
// first & capped like the current task's. Members (and overflow digests) are
|
|
// resolvable via expand_digest(id)/node_detail(id), which search source tasks.
|
|
if cds, more := coldDigestsRecent(ts, relatedOverviewMaxDigestsPerSource); len(cds) > 0 {
|
|
for _, cd := range cds {
|
|
cd["inherited"] = true
|
|
cd["source_task_id"] = source.Task.TaskID
|
|
}
|
|
item["cold_digests"] = cds
|
|
if len(more) > 0 {
|
|
item["cold_digests_more"] = more // The cut is older. digest of id;expand_digest(id) Expand
|
|
}
|
|
}
|
|
if statsErr == nil {
|
|
item["node_stats"] = stats
|
|
}
|
|
|
|
if t.as != nil {
|
|
if scopeRows, err := t.as.ListTaskScope(source.Task.TaskID); err == nil && len(scopeRows) > 0 {
|
|
scopeCount := len(scopeRows)
|
|
if len(scopeRows) > relatedOverviewMaxScopePerSource {
|
|
scopeRows = scopeRows[:relatedOverviewMaxScopePerSource]
|
|
budget.truncated = true
|
|
}
|
|
scope := make([]map[string]any, 0, len(scopeRows))
|
|
for _, row := range scopeRows {
|
|
entry := map[string]any{"kind": row.Kind, "source": budget.take(row.Source, 300)}
|
|
switch {
|
|
case row.Domain != "":
|
|
entry["value"] = budget.take(row.Domain, 400)
|
|
case row.Net != "":
|
|
entry["value"] = budget.take(row.Net, 400)
|
|
case row.Value != "":
|
|
entry["value"] = budget.take(row.Value, 400)
|
|
case row.CompanyID != nil:
|
|
entry["company_id"] = *row.CompanyID
|
|
}
|
|
scope = append(scope, entry)
|
|
}
|
|
item["asset_scope"] = scope
|
|
item["asset_scope_count"] = scopeCount
|
|
}
|
|
if coverage, err := t.as.TaskCoverage(source.Task.TaskID, source.Task.ExplorationID); err == nil {
|
|
item["asset_coverage"] = map[string]any{
|
|
"denominator": coverage.Denominator,
|
|
"tested": coverage.Tested,
|
|
"pct": coverage.Pct,
|
|
"by_type": coverage.ByType,
|
|
}
|
|
}
|
|
}
|
|
if budget.truncated {
|
|
item["summary_truncated"] = true
|
|
}
|
|
out = append(out, item)
|
|
}
|
|
return out
|
|
}
|
|
|
|
func inheritedIntentSummaryState(state string) bool {
|
|
switch state {
|
|
case "done", "blocked", "exhausted", "stopped":
|
|
return true
|
|
default:
|
|
return false
|
|
}
|
|
}
|
|
|
|
// compactNode distills any exploration node to id + summary + state, dropping the
|
|
// big detail/evidence (fetch that on demand via node_detail).
|
|
func compactNode(n *db.Node) map[string]any {
|
|
var p map[string]any
|
|
_ = json.Unmarshal(n.Payload, &p)
|
|
m := map[string]any{"id": n.ID, "state": n.State, "summary": p["summary"]}
|
|
if n.Inherited {
|
|
inheritedMap(m, n.SourceTaskID)
|
|
}
|
|
return m
|
|
}
|
|
|
|
// compactFinding is compactNode plus the vuln-specific vulnclass/severity.
|
|
func compactFinding(n *db.Node) map[string]any {
|
|
var p map[string]any
|
|
_ = json.Unmarshal(n.Payload, &p)
|
|
m := map[string]any{"id": n.ID, "state": n.State, "summary": p["summary"]}
|
|
if n.Inherited {
|
|
inheritedMap(m, n.SourceTaskID)
|
|
}
|
|
if vc, ok := p["vulnclass"]; ok && vc != nil && vc != "" {
|
|
m["vulnclass"] = vc
|
|
}
|
|
if sv, ok := p["severity"]; ok && sv != nil && sv != "" {
|
|
m["severity"] = sv
|
|
}
|
|
return m
|
|
}
|
|
|
|
func (t *ToolSet) listFindings() actool.CoreTool {
|
|
return t.readExpTool("list_findings", "List confirmed findings for this task and directly related tasks (compact: id, task_id, intent_id, vulnclass, severity, summary, and state). Related-task rows carry source_task_id and inherited=true and are read-only. This list contains only findings. Ordinary exploration facts use list_facts, and full detail uses node_detail(id).",
|
|
obj(map[string]any{}),
|
|
func(context.Context, json.RawMessage) (actool.Result, error) {
|
|
f, _ := t.ts.ListByKindWithSources(db.KindFinding, 500)
|
|
if err := t.ts.PopulateFindingTrafficIDs(f); err != nil {
|
|
return actool.Errorf(err.Error()), nil
|
|
}
|
|
intentOf, _ := t.ts.FindingIntentsWithSources() // finding id -> the intent that produced it
|
|
taskID := t.taskID
|
|
if taskID <= 0 {
|
|
taskID, _ = t.ts.TaskID()
|
|
}
|
|
out := make([]map[string]any, 0, len(f))
|
|
for _, n := range f {
|
|
m := compactFinding(n)
|
|
if n.FindingID > 0 {
|
|
m["finding_id"], m["finding_node_id"], m["traffic_count"] = n.FindingID, n.ID, n.TrafficCount
|
|
}
|
|
if n.Inherited {
|
|
m["task_id"] = n.SourceTaskID
|
|
} else {
|
|
m["task_id"] = taskID
|
|
}
|
|
if iid, ok := intentOf[n.ID]; ok {
|
|
m["intent_id"] = iid
|
|
}
|
|
out = append(out, m)
|
|
}
|
|
return jsonResult(out)
|
|
})
|
|
}
|
|
|
|
// factsPageSize is the default page size for list_facts. Facts pile up on long
|
|
// tasks; returning all of them at once (the old behaviour) could blow up the
|
|
// context, so default to the newest page and let the agent page/filter for more.
|
|
const factsPageSize = 20
|
|
|
|
func (t *ToolSet) listFacts() actool.CoreTool {
|
|
return t.readExpTool("list_facts", "Page through exploration facts and conclusions for this task and directly related tasks, newest first (compact: id, summary, and state; a long summary is truncated, and the full text is node_detail(id)). All parameters are optional: limit (default 20, max 100), before (cursor: pass the previous page's next_before to get an older page; omit or 0 for the newest page), and q (filter by a keyword in the summary). Returns {facts, total, has_more, next_before}: total is the count after filtering, and when has_more is true pass next_before to continue. Related-task rows carry source_task_id and inherited=true and are read-only. Findings are listed with list_findings.",
|
|
obj(map[string]any{
|
|
"limit": intp("How many rows to return. Default 20, maximum 100."),
|
|
"before": intp("Page cursor: return only older facts whose id is less than this value. Omit or 0 for the newest page."),
|
|
"q": str("Filter by a keyword in the fact summary (case-insensitive). Omit to disable the filter."),
|
|
}),
|
|
func(_ context.Context, in json.RawMessage) (actool.Result, error) {
|
|
var a struct {
|
|
Limit int `json:"limit"`
|
|
Before int64 `json:"before"`
|
|
Q string `json:"q"`
|
|
}
|
|
_ = json.Unmarshal(in, &a)
|
|
limit := a.Limit
|
|
if limit <= 0 {
|
|
limit = factsPageSize
|
|
}
|
|
if limit > 100 {
|
|
limit = 100
|
|
}
|
|
f, hasMore, total, err := t.ts.ListByKindPageWithSources(db.KindFact, a.Before, limit, strings.TrimSpace(a.Q))
|
|
if err != nil {
|
|
return actool.Result{}, err
|
|
}
|
|
out := make([]map[string]any, 0, len(f))
|
|
for _, n := range f {
|
|
out = append(out, compactFact(n))
|
|
}
|
|
res := map[string]any{"facts": out, "total": total, "has_more": hasMore}
|
|
if hasMore && len(f) > 0 {
|
|
res["next_before"] = f[len(f)-1].ID // pass this back to fetch the next, older page
|
|
}
|
|
return jsonResult(res)
|
|
})
|
|
}
|
|
|
|
// factSummaryMax caps a fact summary in list_facts output. Facts carry one-line
|
|
// conclusions, but nothing enforces brevity; a runaway summary must not bloat a
|
|
// whole page. Full text stays available via node_detail(id).
|
|
const factSummaryMax = 160
|
|
|
|
// compactFact is compactNode with the summary rune-capped for list_facts, so a
|
|
// page of facts stays bounded regardless of how long any single summary grew.
|
|
func compactFact(n *db.Node) map[string]any {
|
|
m := compactNode(n)
|
|
if s, ok := m["summary"].(string); ok && len([]rune(s)) > factSummaryMax {
|
|
m["summary"] = string([]rune(s)[:factSummaryMax]) + "…"
|
|
m["summary_truncated"] = true
|
|
}
|
|
return m
|
|
}
|
|
|
|
func (t *ToolSet) nodeDetail() actool.CoreTool {
|
|
return t.readExpTool("node_detail", "Fetch the full content of one exploration-graph node in this task or a directly related task by id. An inherited node carries source_task_id and inherited=true and is read-only. Only exploration-node ids returned by list_facts, list_findings, or graph_overview are valid. For assets use list_assets or asset_neighbors.",
|
|
obj(map[string]any{"id": idp("Exploration-node id (not an asset id)")}, "id"),
|
|
func(_ context.Context, in json.RawMessage) (actool.Result, error) {
|
|
var a struct {
|
|
ID json.RawMessage `json:"id"`
|
|
}
|
|
_ = json.Unmarshal(in, &a)
|
|
id := pid(a.ID)
|
|
if id <= 0 {
|
|
return actool.Errorf("id is required"), nil
|
|
}
|
|
n, err := t.ts.GetNodeWithSources(id)
|
|
if err != nil {
|
|
return actool.Errorf(err.Error()), nil
|
|
}
|
|
if n == nil {
|
|
return actool.Errorf(fmt.Sprintf("No exploration node %d was found. If you wanted an asset, use list_assets or asset_neighbors (assets and exploration nodes are different id spaces; an asset id cannot be passed to node_detail).", id)), nil
|
|
}
|
|
if err := t.ts.PopulateFindingTrafficIDs([]*db.Node{n}); err != nil {
|
|
return actool.Errorf(err.Error()), nil
|
|
}
|
|
return jsonResult(n) // full payload incl. detail / evidence, plus explicit finding IDs
|
|
})
|
|
}
|
|
|
|
// --- planner write tools ---
|
|
|
|
// intentItem is one exploration direction for add_intent, whether batched or single.
|
|
type intentItem struct {
|
|
Summary string `json:"summary"`
|
|
AssetIDs []json.RawMessage `json:"asset_ids"`
|
|
ParentIDs []json.RawMessage `json:"parent_ids"`
|
|
Priority int `json:"priority"`
|
|
}
|
|
|
|
// addOneIntent creates one intent node, links its upstream lineage, and returns the id.
|
|
// Constraint: an intent may be anchored only on confirmed knowledge — every parent_id
|
|
// must be an existing fact or finding node (not another intent, a goal, or a hint).
|
|
// A brand-new top-level direction leaves parent_ids empty and falls back to the origin
|
|
// fact. That forces "every intent connects to a fact node, and planning is driven by
|
|
// findings rather than invented from nothing" on the creation path.
|
|
func (t *ToolSet) addOneIntent(it intentItem) (int64, error) {
|
|
if strings.TrimSpace(it.Summary) == "" {
|
|
return 0, fmt.Errorf("summary cannot be empty")
|
|
}
|
|
// Validate anchors before the node is created.
|
|
parents := pidList(it.ParentIDs)
|
|
for _, pidv := range parents {
|
|
n, err := t.ts.GetNodeWithSources(pidv)
|
|
if err != nil || n == nil {
|
|
return 0, fmt.Errorf("parent_id %d is not in this task or a directly related task: parent_ids must be existing fact or finding node ids. Leave parent_ids empty for a brand-new top-level direction", pidv)
|
|
}
|
|
if n.Kind != db.KindFact && n.Kind != db.KindFinding {
|
|
return 0, fmt.Errorf("parent_id %d is a %q node and cannot anchor an intent: an intent can be anchored only on a confirmed fact or finding, not on an intent, goal, or hint. Leave parent_ids empty for a brand-new top-level direction", pidv, n.Kind)
|
|
}
|
|
}
|
|
priority := it.Priority
|
|
if priority == 0 {
|
|
priority = 5
|
|
}
|
|
anchors := pidList(it.AssetIDs)
|
|
// Asset interception: if an asset this intent would bind hits a system asset-intercept
|
|
// rule, the intent must not be dispatched.
|
|
if t.as != nil && len(anchors) > 0 {
|
|
hits, err := t.as.CheckAssetsIntercept(t.taskID, anchors)
|
|
if err != nil {
|
|
return 0, fmt.Errorf("Asset interception check failed:%w", err)
|
|
}
|
|
if len(hits) > 0 {
|
|
var b strings.Builder
|
|
fmt.Fprintf(&b, "Intent [%s] was not dispatched. Stop testing these assets; they are outside the allowed test scope:", it.Summary)
|
|
for _, h := range hits {
|
|
fmt.Fprintf(&b, "\n - %s", h.Describe())
|
|
}
|
|
return 0, fmt.Errorf("%s", b.String())
|
|
}
|
|
}
|
|
payload := map[string]any{"summary": it.Summary}
|
|
if len(anchors) > 0 {
|
|
payload["asset_ids"] = anchors
|
|
}
|
|
id, err := t.ts.AddIntent(payload, priority, anchors, "planner")
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
// upstream lineage: link each (validated) fact/finding parent → this intent, so
|
|
// "multiple facts combine into one new intent" is expressible.
|
|
for _, parent := range parents {
|
|
_ = t.ts.Link(parent, db.RelDerivedFrom, id)
|
|
}
|
|
// a top-level intent (no explicit parent) connects to the origin fact, so every
|
|
// intent still traces back to a fact node — at task start the only fact is the
|
|
// origin, and the first intents derive from it.
|
|
if len(parents) == 0 {
|
|
if origin, _ := t.ts.OriginFactID(); origin > 0 {
|
|
_ = t.ts.Link(origin, db.RelDerivedFrom, id)
|
|
}
|
|
}
|
|
return id, nil
|
|
}
|
|
|
|
func (t *ToolSet) addIntent() actool.CoreTool {
|
|
return t.writeExpTool("add_intent", "Add an exploration direction to the frontier and link it into the exploration graph. An intent is an open direction, not a fixed type — describe in summary, in one sentence, what to explore, verify, or test.\n"+
|
|
"Prefer a batch: put several new directions from this round into the intents array and submit them once (fewer round trips than one call each). The returned ids array has the same length and order as intents (a failed item has id 0; see errors). For a single direction, omit intents and pass the top-level summary.",
|
|
obj(map[string]any{
|
|
"intents": map[string]any{"type": "array", "description": "Preferred. Exploration directions to add, processed in order. Each item uses the same fields as the top level (summary, asset_ids, parent_ids, priority). Returned ids match this array in length and order.", "items": map[string]any{"type": "object"}},
|
|
"summary": str("Single direction: one sentence saying what to do and why. The direction can stand on its own without an asset id."),
|
|
"asset_ids": map[string]any{"type": "array", "items": map[string]any{"type": "integer"}, "description": "Target asset ids this direction will test (prefer to pass them; zero, one, or many). These are asset ids from list_assets, not exploration-node ids: the sites, interfaces, parameters, or hosts this direction is aimed at. Pass them whenever the direction is about specific assets — they mark which targets this exploration hits, for coverage de-duplication and for linking the intent into the asset graph. Leave empty only for pure global reconnaissance that truly has no specific target asset."},
|
|
"parent_ids": map[string]any{"type": "array", "items": map[string]any{"type": "integer"}, "description": "Optional upstream anchors (zero, one, or many): which confirmed fact or finding nodes this direction was derived from. Only existing fact or finding node ids are allowed, not intents, goals, or hints — an intent must be anchored on confirmed knowledge, driven by findings rather than planned from nothing. Pass several when several facts together produce one new intent. Leave empty for a brand-new top-level reconnaissance direction (it is attached to the task origin fact automatically)."},
|
|
"priority": intp("Priority from 0 to 10; defaults to 5."),
|
|
}),
|
|
func(_ context.Context, in json.RawMessage) (actool.Result, error) {
|
|
var a struct {
|
|
Intents []intentItem `json:"intents"`
|
|
intentItem // single mode: top-level summary, asset_ids, parent_ids, priority
|
|
}
|
|
_ = json.Unmarshal(in, &a)
|
|
batch := len(a.Intents) > 0
|
|
items := a.Intents
|
|
if !batch {
|
|
items = []intentItem{a.intentItem}
|
|
}
|
|
|
|
ids := make([]int64, len(items))
|
|
errs := map[string]string{}
|
|
createdAny := false
|
|
for i, it := range items {
|
|
id, err := t.addOneIntent(it)
|
|
if err != nil {
|
|
errs[strconv.Itoa(i)] = err.Error()
|
|
continue
|
|
}
|
|
ids[i] = id
|
|
createdAny = true
|
|
}
|
|
|
|
// The operator, through the main agent, injects an intent directly. If the task
|
|
// is already done (the goalless branch with no open goal), pull it back to
|
|
// running so a worker can claim this intent. resumeTask is wired only by the
|
|
// main agent's Chat (SetResumeTask). The planner's ToolSet leaves it nil, so
|
|
// when the planner itself calls add_intent this is a no-op and does not
|
|
// disturb normal intent creation. The intent node is already created above
|
|
// (open), so reviving the task will not be mistaken for a drained queue.
|
|
if createdAny && t.resumeTask != nil {
|
|
t.resumeTask()
|
|
}
|
|
|
|
if !batch { // single item: keep the original return shape
|
|
if e, bad := errs["0"]; bad {
|
|
return actool.Errorf(e), nil
|
|
}
|
|
return actool.Text(fmt.Sprintf("intent created: %d", ids[0])), nil
|
|
}
|
|
out := map[string]any{"ids": ids}
|
|
if len(errs) > 0 {
|
|
out["errors"] = errs
|
|
}
|
|
return jsonResult(out)
|
|
})
|
|
}
|
|
|
|
func (t *ToolSet) listGoals() actool.CoreTool {
|
|
return t.readExpTool("list_goals", "List this task's goal nodes and their state (open or met), so you can judge whether they have been achieved.",
|
|
obj(map[string]any{}),
|
|
func(context.Context, json.RawMessage) (actool.Result, error) {
|
|
g, _ := t.ts.ListByKind(db.KindGoal, 100)
|
|
return jsonResult(g)
|
|
})
|
|
}
|
|
|
|
func (t *ToolSet) proveGoal() actool.CoreTool {
|
|
return t.writeExpTool("prove_goal", "Call this when you judge that a finding or fact proves a goal is achieved: link the evidence node to the goal node and mark the goal met.",
|
|
obj(map[string]any{
|
|
"goal_id": idp("Goal node id"),
|
|
"evidence_id": idp("Id of the finding or fact node that proves it"),
|
|
"reason": str("Why this evidence satisfies the goal"),
|
|
}, "goal_id", "evidence_id"),
|
|
func(_ context.Context, in json.RawMessage) (actool.Result, error) {
|
|
var a struct {
|
|
GoalID json.RawMessage `json:"goal_id"`
|
|
EvidenceID json.RawMessage `json:"evidence_id"`
|
|
Reason string `json:"reason"`
|
|
}
|
|
_ = json.Unmarshal(in, &a)
|
|
goal, ev := pid(a.GoalID), pid(a.EvidenceID)
|
|
if goal == 0 || ev == 0 {
|
|
return actool.Errorf("goal_id and evidence_id are required"), nil
|
|
}
|
|
goalNode, err := t.ts.GetNode(goal)
|
|
if err != nil || goalNode == nil || goalNode.Kind != db.KindGoal {
|
|
return actool.Errorf("goal_id must be a goal node of this task (goals from a related task are read-only)"), nil
|
|
}
|
|
evidenceNode, err := t.ts.GetNodeWithSources(ev)
|
|
if err != nil || evidenceNode == nil || (evidenceNode.Kind != db.KindFact && evidenceNode.Kind != db.KindFinding) {
|
|
return actool.Errorf("evidence_id must be a fact or finding node in this task or a directly related task"), nil
|
|
}
|
|
_ = t.ts.Link(ev, db.RelProves, goal)
|
|
_ = t.ts.SetNodeState(goal, "met")
|
|
// Each time a goal is marked met, check whether every goal of this task is met.
|
|
// If so, mark the task complete (set GoalMet). The model does not also need
|
|
// to call goal_met explicitly.
|
|
if goals, err := t.ts.ListByKind(db.KindGoal, 1000); err == nil && len(goals) > 0 {
|
|
allMet := true
|
|
for _, g := range goals {
|
|
if g.State != "met" {
|
|
allMet = false
|
|
break
|
|
}
|
|
}
|
|
if allMet {
|
|
t.GoalMet = true
|
|
t.Reason = fmt.Sprintf("all %d goals are met (last triggered by goal %d)", len(goals), goal)
|
|
return actool.Text(fmt.Sprintf("goal %d marked met; every goal of this task is achieved and the task is automatically complete", goal)), nil
|
|
}
|
|
}
|
|
return actool.Text(fmt.Sprintf("goal %d marked met", goal)), nil
|
|
})
|
|
}
|
|
|
|
func (t *ToolSet) goalMet() actool.CoreTool {
|
|
return writeTool("goal_met", "End the whole task immediately. Call this only when you have confirmed that every goal of the task is truly achieved and the task as a whole is finished. Achieving only one goal, one flag, or one vulnerability does not count — use prove_goal to mark that goal. This tool is not for ending the current planning round: if this round has no new intent to dispatch, or you are waiting on worker output, just end the round and do not call this tool (zero intents is completely normal). Prefer prove_goal to prove goals one by one. goal_met is only the way to close the whole task without proving them one by one.",
|
|
obj(map[string]any{"reason": str("Why the task is achieved. This must be evidence that the goals were actually achieved, not a reason for ending the round such as \"there is no new direction this round\".")}, "reason"),
|
|
func(_ context.Context, in json.RawMessage) (actool.Result, error) {
|
|
var a struct{ Reason string }
|
|
_ = json.Unmarshal(in, &a)
|
|
t.GoalMet = true
|
|
t.Reason = a.Reason
|
|
return actool.Text("acknowledged: goal marked met"), nil
|
|
})
|
|
}
|
|
|
|
// --- worker write tools ---
|
|
|
|
func (t *ToolSet) addFinding() actool.CoreTool {
|
|
return writeTool("report_finding", "Record a confirmed vulnerability. Use evidence for verifiable proof such as command output or logs. In a task context, pass the current intent_id. The returned finding_id is the standalone finding-record id; finding_node_id is the exploration-node id (the first line of the result keeps that node id).", obj(map[string]any{
|
|
"vulnclass": str("Vulnerability class"), "name": str("Vulnerability name"), "severity": str("critical|high|medium|low"), "summary": str("Finding summary"),
|
|
"intent_id": idp("Intent id of the current task"), "asset_ids": map[string]any{"type": "array", "items": map[string]any{"type": "integer"}, "description": "Ids of affected assets"},
|
|
"evidence": str("Evidence or proof-of-concept text"),
|
|
"evidence_hint_id": idp("Optional: id of the hint node in this task that corresponds to this finding. Its structured traffic_refs are carried automatically. Do not cite an inherited hint or a hint for a different finding."),
|
|
"traffic_refs": map[string]any{"type": "array", "description": "Optional. For an HTTP or HTTPS finding, search first and check each request and response really supports the finding, then fill real ids in reproduction order. For TCP and other non-HTTP findings, or when nothing was captured or no exact record exists, omit this or pass []. That does not block the report. You may explain why in evidence and give other verifiable evidence. Do not guess ids, infer a link from a domain or a timestamp, or probe again only to fill in a packet. Roles: baseline is a normal control, proof demonstrates the vulnerability, verification is extra confirmation, and supporting is auxiliary evidence.",
|
|
"items": obj(map[string]any{"traffic_id": str("Real traffic id returned by traffic_search"), "role": map[string]any{"type": "string", "enum": []string{"baseline", "proof", "verification", "supporting"}}, "note": str("How this flow supports the finding")}, "traffic_id")},
|
|
}, "vulnclass", "severity", "summary"), func(ctx context.Context, in json.RawMessage) (actool.Result, error) {
|
|
var a struct {
|
|
VulnClass, Name, Severity, Summary, Evidence string
|
|
IntentID json.RawMessage `json:"intent_id"`
|
|
AssetIDs []json.RawMessage `json:"asset_ids"`
|
|
TrafficRefs []db.TrafficRef `json:"traffic_refs"`
|
|
EvidenceHintID json.RawMessage `json:"evidence_hint_id"`
|
|
}
|
|
if err := json.Unmarshal(in, &a); err != nil {
|
|
return actool.Errorf(err.Error()), nil
|
|
}
|
|
if t.ts == nil {
|
|
return actool.Errorf("report_finding needs a task context. In a platform conversation, hand the finding to the matching task with add_task_hint and keep any existing traffic_refs in the hint so the task agent can record it. A finding that is already recorded can be bound later with bind_finding_traffic."), nil
|
|
}
|
|
// Auto-binding off: ignore the evidence params instead of rejecting the call.
|
|
// stripTrafficParameters already removes them from the advertised schema, but
|
|
// models routinely emit fields anyway — failing here would discard a confirmed
|
|
// finding over a stray parameter. The success path below reports evidence_status
|
|
// "not_bound" with the "disabled; link it manually on the page" note, which is what the caller needs.
|
|
if !findingTrafficBindingEnabled() {
|
|
a.TrafficRefs, a.EvidenceHintID = nil, nil
|
|
}
|
|
if len(a.EvidenceHintID) > 0 && pid(a.EvidenceHintID) <= 0 {
|
|
return actool.Errorf("evidence_hint_id must be a valid hint-node id; omit it when there is no handoff hint"), nil
|
|
}
|
|
refs, err := t.findingRefsFromHint(pid(a.EvidenceHintID), a.TrafficRefs)
|
|
if err != nil {
|
|
return actool.Errorf(err.Error()), nil
|
|
}
|
|
input := db.RecordFindingInput{TaskID: t.taskID, ExplorationID: t.ts.ID(), IntentID: pid(a.IntentID), VulnClass: a.VulnClass, Name: a.Name, Severity: a.Severity, Summary: a.Summary, Evidence: a.Evidence, Worker: t.worker, AssetIDs: pidList(a.AssetIDs)}
|
|
var recorded *db.RecordedFinding
|
|
if t.findingRecorder != nil {
|
|
recorded, err = t.findingRecorder.Record(ctx, input, refs)
|
|
} else if len(refs) > 0 {
|
|
return actool.Errorf("Unregistered traffic references require an evidence store; the finding was not recorded"), nil
|
|
} else {
|
|
recorded, err = t.ts.RecordFinding(ctx, input)
|
|
}
|
|
if err != nil {
|
|
return actool.Errorf(err.Error()), nil
|
|
}
|
|
if t.notifyFinding != nil {
|
|
iid := input.IntentID
|
|
if iid <= 0 {
|
|
iid = t.ownerNode
|
|
}
|
|
t.notifyFinding(iid, a.Summary)
|
|
} else if t.notify != nil {
|
|
t.notify()
|
|
}
|
|
t.writes.Findings++
|
|
// Keep the first line's node-ID contract for existing reporter triggers.
|
|
for i := range recorded.Traffic.Bindings {
|
|
recorded.Traffic.Bindings[i].Snapshot.ReqHead = ""
|
|
recorded.Traffic.Bindings[i].Snapshot.RespHead = ""
|
|
}
|
|
result := struct {
|
|
*db.RecordedFinding
|
|
EvidenceStatus string `json:"evidence_status"`
|
|
EvidenceNote string `json:"evidence_note,omitempty"`
|
|
}{RecordedFinding: recorded, EvidenceStatus: "bound"}
|
|
if len(recorded.Traffic.Bindings) == 0 {
|
|
result.EvidenceStatus = "not_bound"
|
|
result.EvidenceNote = "Finding saved without bound traffic. For a verified HTTP finding, bind available traffic through bind_finding_traffic or the finding page. Keep other verified evidence for TCP findings."
|
|
if !findingTrafficBindingEnabled() {
|
|
result.EvidenceNote = "Finding saved. Automatic traffic binding is disabled; verified traffic may be linked manually on the finding page."
|
|
}
|
|
}
|
|
raw, _ := json.Marshal(result)
|
|
return actool.Text(fmt.Sprintf("finding recorded: %d\n%s", recorded.NodeID, raw)), nil
|
|
})
|
|
}
|
|
|
|
// recordFact writes a general exploration RESULT/conclusion (not a vuln, not a
|
|
// new asset) into the EXPLORATION graph, chained to the intent that produced it.
|
|
// This is the home for observations and — importantly — negative results
|
|
// ("port closed", "param not injectable", "no login found"). Such conclusions
|
|
// must NOT be stuffed into the asset graph via upsert_asset.
|
|
// factItem is one fact for record_fact, whether batched or single.
|
|
type factItem struct {
|
|
Summary string `json:"summary"`
|
|
Detail string `json:"detail"`
|
|
Evidence string `json:"evidence"` // one line of key evidence (command plus the key output line) supporting the conclusion, so it can be checked later
|
|
Confidence string `json:"confidence"` // observed (seen directly) | inferred (inferred from what was observed)
|
|
IntentID json.RawMessage `json:"intent_id"`
|
|
AssetIDs []json.RawMessage `json:"asset_ids"`
|
|
}
|
|
|
|
// recordOneFact writes one fact node and links it to the intent (intent→yields→fact).
|
|
// defaultIntent is the batch default, used when this item omits intent_id.
|
|
func (t *ToolSet) recordOneFact(it factItem, defaultIntent int64) (int64, error) {
|
|
if strings.TrimSpace(it.Summary) == "" {
|
|
return 0, fmt.Errorf("summary cannot be empty")
|
|
}
|
|
payload := map[string]any{"summary": it.Summary}
|
|
if it.Detail != "" {
|
|
payload["detail"] = it.Detail
|
|
}
|
|
if e := strings.TrimSpace(it.Evidence); e != "" {
|
|
payload["evidence"] = e
|
|
}
|
|
if c := strings.TrimSpace(it.Confidence); c != "" {
|
|
payload["confidence"] = c
|
|
}
|
|
intent := pid(it.IntentID)
|
|
if intent <= 0 {
|
|
intent = defaultIntent
|
|
}
|
|
if intent > 0 {
|
|
node, err := t.ts.GetNode(intent)
|
|
if err != nil || node == nil || node.Kind != db.KindIntent {
|
|
return 0, fmt.Errorf("intent_id must be an intent of this task (intents from a related task are read-only)")
|
|
}
|
|
}
|
|
// a fact is its OWN node kind (distinct from a vuln finding).
|
|
id, err := t.ts.AddNode(db.KindFact, payload, 5, "confirmed", t.worker, pidList(it.AssetIDs))
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
if intent > 0 {
|
|
_ = t.ts.Link(intent, db.RelYields, id) // chain: intent -> fact
|
|
}
|
|
t.writes.Facts++
|
|
return id, nil
|
|
}
|
|
|
|
func (t *ToolSet) recordFact() actool.CoreTool {
|
|
return t.writeExpTool("record_fact", "Write an exploration fact or conclusion into the exploration graph, linked to the intent that produced it (intent_id). Use it to record exploration results, including positive conclusions such as fingerprints or enumeration, and negative conclusions such as 'port closed', 'parameter not injectable', or 'no login entry found'.\n"+
|
|
"Combine the observations from one exploration into one fact. Do not split them. When they can be one fact, use one: summary is one sentence summarizing this conclusion, and detail holds the related specifics (it may include several items). Example: a fingerprint intent becomes one fact {summary:'Identified the technology stack and response characteristics of site X', detail:'nginx 1.25 / Vue3 / 200 / title=.. / body_len=..'}, not one fact each for status code, fingerprint, and title. One intent usually produces only one fact. Splitting too finely makes the graph grow without bound.\n"+
|
|
"The facts array writes several conclusions that differ from each other in one call (each item may omit intent_id and then uses the top-level intent_id). The returned ids array has the same length and order as facts.\n"+
|
|
"Write only conclusions you actually saw in tool output. Do not invent them. evidence and confidence keep inaccurate conclusions from polluting the graph:\n"+
|
|
" · evidence is one line of key evidence for this conclusion (the command plus the one or two output lines that prove it most). Keep it short. The detail already lives in detail; do not paste a large output block here.\n"+
|
|
" · confidence is observed (seen directly in the output) or inferred (inferred from what you observed).\n"+
|
|
" · A negative conclusion (not injectable, port closed, no entry found, and similar) should state only what you observed plus a tentative reading. Say what you actually saw. Whether to abandon the direction is for the planner to decide from the whole picture. Always give evidence. If the methods are not exhausted or the evidence is weak (including a single probe, or something that only looks like a result), mark inferred. Mark observed only when the approach is exhausted and you saw the result directly.",
|
|
obj(map[string]any{
|
|
"facts": map[string]any{"type": "array", "description": "Use this when there are several different conclusions. Each element uses the same fields as the top level (summary, detail, evidence, confidence, intent_id, asset_ids). An omitted intent_id uses the top-level intent_id. Returned ids match this array in length and order.", "items": map[string]any{"type": "object"}},
|
|
"summary": str("One sentence summarizing this exploration's conclusion (a summary of detail)"),
|
|
"intent_id": idp("Id of the intent that produced this fact (the intent you were given; in a batch this is the default for each item)"),
|
|
"detail": str("Related detail for this fact: put the observations from this exploration here"),
|
|
"evidence": str("One line of key evidence: the command plus the one or two output lines that best support the conclusion. Keep it short. Do not paste a large output block (put detail in detail)."),
|
|
"confidence": str("observed (seen directly in the output) or inferred (inferred from what you observed). Mark negative conclusions honestly."),
|
|
"asset_ids": map[string]any{"type": "array", "items": map[string]any{"type": "integer"}, "description": "Related asset ids (optional; zero, one, or many): which assets this fact concerns"},
|
|
}),
|
|
func(_ context.Context, in json.RawMessage) (actool.Result, error) {
|
|
var a struct {
|
|
Facts []factItem `json:"facts"`
|
|
factItem // single mode, plus the batch default intent_id
|
|
}
|
|
_ = json.Unmarshal(in, &a)
|
|
batch := len(a.Facts) > 0
|
|
items := a.Facts
|
|
if !batch {
|
|
items = []factItem{a.factItem}
|
|
}
|
|
defaultIntent := pid(a.factItem.IntentID) // Top intent_id = Batch Default
|
|
|
|
ids := make([]int64, len(items))
|
|
errs := map[string]string{}
|
|
for i, it := range items {
|
|
id, err := t.recordOneFact(it, defaultIntent)
|
|
if err != nil {
|
|
errs[strconv.Itoa(i)] = err.Error()
|
|
continue
|
|
}
|
|
ids[i] = id
|
|
}
|
|
|
|
if !batch { // single item: keep the original return shape
|
|
if e, bad := errs["0"]; bad {
|
|
return actool.Errorf(e), nil
|
|
}
|
|
return actool.Text(fmt.Sprintf("fact recorded: %d", ids[0])), nil
|
|
}
|
|
out := map[string]any{"ids": ids}
|
|
if len(errs) > 0 {
|
|
out["errors"] = errs
|
|
}
|
|
return jsonResult(out)
|
|
})
|
|
}
|
|
|
|
type hintItem struct {
|
|
Text string `json:"text"`
|
|
AssetIDs []json.RawMessage `json:"asset_ids"`
|
|
TrafficRefs []db.TrafficRef `json:"traffic_refs"`
|
|
}
|
|
|
|
// addOneHint attaches one hint node (active/human) to the exploration graph. It may
|
|
// anchor assets and returns the id.
|
|
func (t *ToolSet) addOneHint(it hintItem) (int64, error) {
|
|
if len(it.TrafficRefs) > 0 && !findingTrafficBindingEnabled() {
|
|
return 0, fmt.Errorf("automatic traffic binding is off, so a hint that carries traffic_refs was not saved. Turn it on in system settings, or hand over text only")
|
|
}
|
|
if strings.TrimSpace(it.Text) == "" {
|
|
return 0, fmt.Errorf("text cannot be empty")
|
|
}
|
|
var anchors []int64
|
|
for _, raw := range it.AssetIDs {
|
|
if tid := pid(raw); tid > 0 {
|
|
anchors = append(anchors, tid)
|
|
}
|
|
}
|
|
refs, err := db.NormalizeTrafficRefs(it.TrafficRefs)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
payload := map[string]any{"text": it.Text}
|
|
if len(refs) > 0 {
|
|
payload["traffic_refs"] = refs
|
|
}
|
|
// The planner is not woken here, one hint at a time. addHint fires once after the
|
|
// whole batch is written (and includes the hint text), so one add_hint call with
|
|
// several hints does not flood the planner's trigger line.
|
|
return t.ts.AddNode(db.KindHint, payload, 0, "active", "human", anchors)
|
|
}
|
|
|
|
type goalItem struct {
|
|
Text string `json:"text"`
|
|
VulnClass string `json:"vulnclass"`
|
|
}
|
|
|
|
// addOneGoal attaches one goal node (open) to the exploration graph, linked to the
|
|
// task root (origin fact, rel spawns). origin is t.worker (default "system"): the
|
|
// goals decomposer writes "goals", and the main agent at runtime writes "human".
|
|
// Waking the planner is done once by setGoals after the whole batch (see below).
|
|
// This function only persists the node.
|
|
func (t *ToolSet) addOneGoal(it goalItem) (int64, error) {
|
|
text := strings.TrimSpace(it.Text)
|
|
if text == "" {
|
|
return 0, fmt.Errorf("text cannot be empty")
|
|
}
|
|
payload := map[string]any{"text": text}
|
|
if vc := strings.TrimSpace(it.VulnClass); vc != "" {
|
|
payload["vulnclass"] = vc
|
|
}
|
|
origin := t.worker
|
|
if origin == "" {
|
|
origin = "system"
|
|
}
|
|
id, err := t.ts.AddNode(db.KindGoal, payload, 0, "open", origin, nil)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
if of, _ := t.ts.OriginFactID(); of > 0 && id > 0 {
|
|
_ = t.ts.Link(of, db.RelSpawns, id) // goals descend from the task root (origin fact)
|
|
}
|
|
return id, nil
|
|
}
|
|
|
|
// setGoals adds exploration goals (goal nodes) to this task. It is both the goal
|
|
// decomposer's submit tool and the tool the main agent uses to add goals at
|
|
// runtime — the same managed tool, whose description and schema can be edited on
|
|
// the web and bound per agent.
|
|
func (t *ToolSet) setGoals() actool.CoreTool {
|
|
return writeTool("set_goals",
|
|
"Add exploration goals (goal) to this task. A goal is a final deliverable or verifiable outcome, not an attack step and not a reconnaissance action.\n"+
|
|
"Prefer a batch: put several goals in the goals array and submit them once. Returned ids match that array in length and order (a failed item has id 0; see errors). For a single goal, omit goals and pass the top-level text.\n"+
|
|
"vulnclass is optional: the vulnerability class, such as SQLi or IDOR. Leave it empty for a business-logic goal. Whether a goal is achieved is marked met by the system. This tool only adds goals.",
|
|
obj(map[string]any{
|
|
"goals": map[string]any{"type": "array", "description": "Preferred. Goals to add, processed in order. Each element is text (required: one independently verifiable final outcome) plus vulnclass (optional). Returned ids match this array in length and order.", "items": map[string]any{"type": "object"}},
|
|
"text": str("Single goal: one independently verifiable final outcome"),
|
|
"vulnclass": str("Single goal: the vulnerability class when it is clear, such as SQLi or IDOR. A business-logic goal may be left empty."),
|
|
}),
|
|
func(_ context.Context, in json.RawMessage) (actool.Result, error) {
|
|
if t.ts == nil {
|
|
return actool.Errorf("set_goals is not enabled: ExplorationStore is not initialized"), nil
|
|
}
|
|
var a struct {
|
|
Goals []goalItem `json:"goals"`
|
|
goalItem // single mode: top-level text and vulnclass
|
|
}
|
|
_ = json.Unmarshal(in, &a)
|
|
batch := len(a.Goals) > 0
|
|
items := a.Goals
|
|
if !batch {
|
|
items = []goalItem{a.goalItem}
|
|
}
|
|
|
|
ids := make([]int64, len(items))
|
|
errs := map[string]string{}
|
|
var addedTexts []string
|
|
for i, it := range items {
|
|
id, err := t.addOneGoal(it)
|
|
if err != nil {
|
|
errs[strconv.Itoa(i)] = err.Error()
|
|
continue
|
|
}
|
|
ids[i] = id
|
|
addedTexts = append(addedTexts, strings.TrimSpace(it.Text))
|
|
}
|
|
if len(addedTexts) > 0 {
|
|
// Wake the planner once for the whole batch. Prefer notifyGoal: one set_goals
|
|
// call records one "the operator added N goals: …" trigger, instead of
|
|
// one line per goal. The decomposer and workers have no such callback
|
|
// and fall back to a bare notify (the decomposer's round-0 does not
|
|
// even wire notify, which is a no-op, because the planner has not
|
|
// started yet).
|
|
switch {
|
|
case t.notifyGoal != nil:
|
|
t.notifyGoal(addedTexts)
|
|
case t.notify != nil:
|
|
t.notify()
|
|
}
|
|
// The main agent adding a goal at runtime pulls a finished or paused task back
|
|
// to running so work continues (the terminal gate swallows a plain notify,
|
|
// so this must revive the task explicitly). Only mainagent wires this
|
|
// callback; the decomposer and workers leave it nil.
|
|
if t.resumeTask != nil {
|
|
t.resumeTask()
|
|
}
|
|
}
|
|
|
|
if !batch { // single item: keep the original return shape
|
|
if e, bad := errs["0"]; bad {
|
|
return actool.Errorf(e), nil
|
|
}
|
|
return actool.Text(fmt.Sprintf("goal added: %d", ids[0])), nil
|
|
}
|
|
out := map[string]any{"ids": ids}
|
|
if len(errs) > 0 {
|
|
out["errors"] = errs
|
|
}
|
|
return jsonResult(out)
|
|
})
|
|
}
|
|
|
|
type constraintItem struct {
|
|
Text string `json:"text"`
|
|
Type string `json:"type"` // allow | deny
|
|
}
|
|
|
|
// addOneConstraint writes one operation constraint to task_constraints. origin is
|
|
// t.worker (default "system"): the decomposer writes "goals" and the main agent
|
|
// writes "human".
|
|
func (t *ToolSet) addOneConstraint(it constraintItem) (int64, error) {
|
|
text := strings.TrimSpace(it.Text)
|
|
if text == "" {
|
|
return 0, fmt.Errorf("text cannot be empty")
|
|
}
|
|
kind := strings.TrimSpace(strings.ToLower(it.Type))
|
|
if kind == "" {
|
|
kind = "deny" // default to deny: more conservative when the type was not stated
|
|
}
|
|
if kind != "allow" && kind != "deny" {
|
|
return 0, fmt.Errorf("type must be allow or deny")
|
|
}
|
|
return t.ts.AddConstraint(kind, text, t.worker)
|
|
}
|
|
|
|
// setConstraints adds operation constraints to this task (allow = what is permitted,
|
|
// deny = what is forbidden). It is both the goal decomposer's round-0 submit tool
|
|
// for extracted constraints and the tool the main agent uses to add constraints at
|
|
// runtime — the same managed tool, whose description and schema can be edited on
|
|
// the web and bound per agent. Constraints are injected into the planner and worker
|
|
// system prompts to bound exploration.
|
|
func (t *ToolSet) setConstraints() actool.CoreTool {
|
|
return writeTool("set_constraints",
|
|
"Add operation constraints to this task, to bound exploration: type is allow (an operation that is permitted) or deny (an operation that is forbidden).\n"+
|
|
"A constraint states what may or may not be done (for example \"test only the current port; do not scan other ports\", \"do not perform write operations against the production database\", or \"passive reconnaissance only\"). It is not a goal and it is not an attack step.\n"+
|
|
"Prefer a batch: put several constraints in the constraints array and submit them once. Returned ids match that array in length and order (a failed item has id 0; see errors). For a single constraint, omit constraints and pass the top-level text and type.\n"+
|
|
"Register only constraints the task goal or description states explicitly. Do not invent them. If the type is uncertain, use deny (the more conservative choice).",
|
|
obj(map[string]any{
|
|
"constraints": map[string]any{"type": "array", "description": "Preferred. Constraints to add, processed in order. Each element is text (required: one constraint) plus type (allow or deny). Returned ids match this array in length and order.", "items": map[string]any{"type": "object"}},
|
|
"text": str("Single constraint: the text of one operation constraint"),
|
|
"type": str("Single constraint: allow (permitted) or deny (forbidden). Defaults to deny."),
|
|
}),
|
|
func(_ context.Context, in json.RawMessage) (actool.Result, error) {
|
|
if t.ts == nil {
|
|
return actool.Errorf("set_constraints is not enabled: ExplorationStore is not initialized"), nil
|
|
}
|
|
var a struct {
|
|
Constraints []constraintItem `json:"constraints"`
|
|
constraintItem // single mode: top-level text and type
|
|
}
|
|
_ = json.Unmarshal(in, &a)
|
|
batch := len(a.Constraints) > 0
|
|
items := a.Constraints
|
|
if !batch {
|
|
items = []constraintItem{a.constraintItem}
|
|
}
|
|
ids := make([]int64, len(items))
|
|
errs := map[string]string{}
|
|
for i, it := range items {
|
|
id, err := t.addOneConstraint(it)
|
|
if err != nil {
|
|
errs[strconv.Itoa(i)] = err.Error()
|
|
continue
|
|
}
|
|
ids[i] = id
|
|
}
|
|
if !batch { // single item: keep the simple return
|
|
if e, bad := errs["0"]; bad {
|
|
return actool.Errorf(e), nil
|
|
}
|
|
return actool.Text(fmt.Sprintf("constraint added: %d", ids[0])), nil
|
|
}
|
|
out := map[string]any{"ids": ids}
|
|
if len(errs) > 0 {
|
|
out["errors"] = errs
|
|
}
|
|
return jsonResult(out)
|
|
})
|
|
}
|
|
|
|
func (t *ToolSet) addHint() actool.CoreTool {
|
|
return t.writeExpTool("add_hint", "Attach a strategic hint from the operator or the main agent to the exploration graph. The planner reads it the next time it generates intents.\n"+
|
|
"Prefer a batch: put several hints in the hints array and submit them once (fewer round trips than one call each). The returned ids array has the same length and order as hints (a failed item has id 0; see errors). For a single hint, omit hints and pass the top-level text.",
|
|
obj(map[string]any{
|
|
"hints": map[string]any{"type": "array", "description": "Preferred. Hints to add, processed in order. Each element uses the same fields as the top level (text, asset_ids, traffic_refs). Returned ids match this array in length and order.", "items": obj(map[string]any{"text": str("Hint text"), "asset_ids": map[string]any{"type": "array", "items": map[string]any{"type": "integer"}}, "traffic_refs": HintTrafficSchema()})},
|
|
"text": str("Single hint, for example 'focus on authenticated interfaces'"),
|
|
"traffic_refs": HintTrafficSchema(),
|
|
"asset_ids": map[string]any{"type": "array", "items": map[string]any{"type": "integer"}, "description": "Anchored asset ids (optional; zero, one, or many)"},
|
|
}),
|
|
func(_ context.Context, in json.RawMessage) (actool.Result, error) {
|
|
var a struct {
|
|
Hints []hintItem `json:"hints"`
|
|
hintItem // single mode: top-level text and asset_ids
|
|
}
|
|
_ = json.Unmarshal(in, &a)
|
|
batch := len(a.Hints) > 0
|
|
items := a.Hints
|
|
if !batch {
|
|
items = []hintItem{a.hintItem}
|
|
}
|
|
|
|
ids := make([]int64, len(items))
|
|
errs := map[string]string{}
|
|
var addedTexts []string
|
|
for i, it := range items {
|
|
id, err := t.addOneHint(it)
|
|
if err != nil {
|
|
errs[strconv.Itoa(i)] = err.Error()
|
|
continue
|
|
}
|
|
ids[i] = id
|
|
addedTexts = append(addedTexts, strings.TrimSpace(it.Text))
|
|
}
|
|
if len(addedTexts) > 0 {
|
|
// Wake the planner once for the whole batch. Prefer notifyHint: one add_hint
|
|
// call records one "the operator added N strategic hints: …" trigger, so
|
|
// the planner knows this round was fired by a new hint and sees the
|
|
// text. If that callback is not wired, fall back to a bare notify (the
|
|
// hint is still folded into the graph for the planner to read).
|
|
switch {
|
|
case t.notifyHint != nil:
|
|
t.notifyHint(addedTexts)
|
|
case t.notify != nil:
|
|
t.notify()
|
|
}
|
|
}
|
|
|
|
if !batch { // single item: keep the original return shape
|
|
if e, bad := errs["0"]; bad {
|
|
return actool.Errorf(e), nil
|
|
}
|
|
return actool.Text(fmt.Sprintf("hint added: %d", ids[0])), nil
|
|
}
|
|
out := map[string]any{"ids": ids}
|
|
if len(errs) > 0 {
|
|
out["errors"] = errs
|
|
}
|
|
return jsonResult(out)
|
|
})
|
|
}
|
|
|
|
// killWorkTool lets the planner terminate a single running work (by intent id).
|
|
func (t *ToolSet) killWorkTool() actool.CoreTool {
|
|
return t.writeExpTool("kill_work", "Stop one running intent (work). Use it to halt exploration that has drifted or is pointless. The stopped intent is marked stopped and will not be claimed again automatically. Look at get_worker_output first to see what it is doing.",
|
|
obj(map[string]any{"intent_id": idp("Id of the intent to stop (the work handle)")}, "intent_id"),
|
|
func(_ context.Context, in json.RawMessage) (actool.Result, error) {
|
|
if t.killWork == nil {
|
|
return actool.Errorf("kill_work is not available"), nil
|
|
}
|
|
var a struct {
|
|
IntentID json.RawMessage `json:"intent_id"`
|
|
}
|
|
_ = json.Unmarshal(in, &a)
|
|
id := pid(a.IntentID)
|
|
if id <= 0 {
|
|
return actool.Errorf("intent_id is required"), nil
|
|
}
|
|
node, err := t.ts.GetNode(id)
|
|
if err != nil || node == nil || node.Kind != db.KindIntent {
|
|
return actool.Errorf("intent_id It must be the intent of this mission.)"), nil
|
|
}
|
|
if err := t.killWork(id); err != nil {
|
|
return actool.Errorf(err.Error()), nil
|
|
}
|
|
return actool.Text(fmt.Sprintf("sent a stop signal to the work for intent %d", id)), nil
|
|
})
|
|
}
|
|
|
|
// steerWorkTool lets the planner inject a mid-run course-correction into a running
|
|
// work WITHOUT killing it: the message reaches the worker before its next tool call,
|
|
// which re-plans its next step (already-gathered context is kept). For in-intent
|
|
// nudges ("stop doing X and focus on Y"); if the whole direction is wrong use kill_work + a new intent.
|
|
func (t *ToolSet) steerWorkTool() actool.CoreTool {
|
|
return t.writeExpTool("steer_work", "Inject a course correction into a running intent (work) without interrupting it or discarding progress. The worker receives your instruction before its next action and adjusts. Use it for an in-intent correction such as 'stop doing X and focus on Y'. If the whole direction is wrong, use kill_work and then a new intent. Look at get_worker_output first to see what it is doing.",
|
|
obj(map[string]any{
|
|
"intent_id": idp("Id of the intent to correct (the work handle)"),
|
|
"message": str("Instruction for the worker: say clearly what to stop and what to turn to"),
|
|
}, "intent_id", "message"),
|
|
func(_ context.Context, in json.RawMessage) (actool.Result, error) {
|
|
if t.steerWork == nil {
|
|
return actool.Errorf("steer_work is not available"), nil
|
|
}
|
|
var a struct {
|
|
IntentID json.RawMessage `json:"intent_id"`
|
|
Message string `json:"message"`
|
|
}
|
|
_ = json.Unmarshal(in, &a)
|
|
id := pid(a.IntentID)
|
|
if id <= 0 {
|
|
return actool.Errorf("intent_id is required"), nil
|
|
}
|
|
node, err := t.ts.GetNode(id)
|
|
if err != nil || node == nil || node.Kind != db.KindIntent {
|
|
return actool.Errorf("intent_id It must be the intent of this mission.)"), nil
|
|
}
|
|
if strings.TrimSpace(a.Message) == "" {
|
|
return actool.Errorf("message is required"), nil
|
|
}
|
|
if err := t.steerWork(id, a.Message); err != nil {
|
|
return actool.Errorf(err.Error()), nil
|
|
}
|
|
return actool.Text(fmt.Sprintf("injected a course correction into the work for intent %d (takes effect on the next step)", id)), nil
|
|
})
|
|
}
|
|
|
|
// getWorkerOutput returns a work's final conclusion text by intent id, or the last
|
|
// output as of the moment it was stopped.
|
|
func (t *ToolSet) getWorkerOutput() actool.CoreTool {
|
|
return t.readExpTool("get_worker_output", "Fetch the final output of one intent (work) in this task or a directly related task. A related-task result carries source_task_id and inherited=true and is read-only. A normal finish returns its summary. A stopped or failed work returns the last output as of the moment it ended.",
|
|
obj(map[string]any{"intent_id": idp("Intent id (the work handle)")}, "intent_id"),
|
|
func(_ context.Context, in json.RawMessage) (actool.Result, error) {
|
|
var a struct {
|
|
IntentID json.RawMessage `json:"intent_id"`
|
|
}
|
|
_ = json.Unmarshal(in, &a)
|
|
id := pid(a.IntentID)
|
|
if id <= 0 {
|
|
return actool.Errorf("intent_id is required"), nil
|
|
}
|
|
intentNode, err := t.ts.GetNodeWithSources(id)
|
|
if err != nil {
|
|
return actool.Errorf(err.Error()), nil
|
|
}
|
|
if intentNode == nil || intentNode.Kind != db.KindIntent {
|
|
return actool.Errorf("intent_id does not belong to this task or a directly related task"), nil
|
|
}
|
|
acts, _, err := t.ts.ActivityListWithSources(id, 0, 1000)
|
|
if err != nil {
|
|
return actool.Errorf(err.Error()), nil
|
|
}
|
|
var chosen, fallback *db.Activity
|
|
for i := range acts {
|
|
switch acts[i].Kind {
|
|
case "result":
|
|
chosen = &acts[i]
|
|
fallback = &acts[i]
|
|
case "text":
|
|
fallback = &acts[i]
|
|
}
|
|
}
|
|
pick := chosen
|
|
if pick == nil {
|
|
pick = fallback
|
|
}
|
|
if pick == nil {
|
|
if intentNode.Inherited {
|
|
return jsonResult(inheritedMap(map[string]any{
|
|
"intent_id": id, "final_text": "(this work has no output yet)",
|
|
}, intentNode.SourceTaskID))
|
|
}
|
|
return actool.Text("(this work has no output yet)"), nil
|
|
}
|
|
detail, _ := t.ts.ActivityDetailWithSources(pick.ID)
|
|
if detail == "" {
|
|
detail = pick.Summary
|
|
}
|
|
result := map[string]any{
|
|
"intent_id": id, "final_text": detail,
|
|
"summary": pick.Summary, "is_error": pick.IsError,
|
|
}
|
|
if intentNode.Inherited {
|
|
inheritedMap(result, intentNode.SourceTaskID)
|
|
}
|
|
return jsonResult(result)
|
|
})
|
|
}
|
|
|
|
// traceSteps renders summary-only trace rows, re-truncating each summary to 100
|
|
// chars — the stored summary is capped at 200 for the UI transcript; the trace
|
|
// tools want it tighter since a whole work's step list is many rows.
|
|
func traceSteps(acts []db.Activity) []map[string]any {
|
|
steps := make([]map[string]any, 0, len(acts))
|
|
for i := range acts {
|
|
step := map[string]any{
|
|
"step_id": acts[i].ID, "kind": acts[i].Kind, "tool": acts[i].Tool,
|
|
"is_error": acts[i].IsError, "summary": firstLine(acts[i].Summary, 100),
|
|
}
|
|
if acts[i].Inherited {
|
|
inheritedMap(step, acts[i].SourceTaskID)
|
|
}
|
|
steps = append(steps, step)
|
|
}
|
|
return steps
|
|
}
|
|
|
|
// getWorkerTrace exposes a work's execution PROCESS (not just its final output):
|
|
// list step summaries, keyword-search within one work, or pull full detail of a
|
|
// few specific steps. Thinking steps are excluded everywhere.
|
|
func (t *ToolSet) getWorkerTrace() actool.CoreTool {
|
|
return t.readExpTool("get_worker_trace",
|
|
"Read the execution trace of one intent (work). This is different from get_worker_output, which returns only the final conclusion. Three uses:\n"+
|
|
"1. intent_id only: return a summary stream of each step (summary at most 100 characters, including step_id). This is only the outline of the action, not the full output.\n"+
|
|
"2. intent_id plus q: return only the step summaries that match the keyword (searched in both the summary and the full output). Still only the summary; use 3 to read the content.\n"+
|
|
"3. intent_id plus step_ids: return the full content (detail) of those steps. At most 5 at a time. If you pass more, only the first 5 are returned and notice and omitted_step_ids name the ones that were not fetched.\n"+
|
|
"Typical flow: use 1 or 2 to find the step_id of a suspicious step, then use 3 for its full output. Thinking steps are excluded. Historical traces of directly related tasks are supported; those results carry source_task_id and inherited=true and are read-only.",
|
|
obj(map[string]any{
|
|
"intent_id": idp("Intent id (the work handle)"),
|
|
"q": str("Keyword: return only steps whose summary or full output matches it (optional; mutually exclusive with step_ids)"),
|
|
"step_ids": map[string]any{"type": "array", "items": map[string]any{"type": "integer"}, "description": "step_ids whose full content to fetch (from a type-1 or type-2 result). At most 5 at a time. Extra ids are listed in omitted_step_ids."},
|
|
"limit": intp("Cap on the summary stream or the search (optional)"),
|
|
}, "intent_id"),
|
|
func(_ context.Context, in json.RawMessage) (actool.Result, error) {
|
|
var a struct {
|
|
IntentID json.RawMessage `json:"intent_id"`
|
|
Q string `json:"q"`
|
|
StepIDs []json.RawMessage `json:"step_ids"`
|
|
Limit int `json:"limit"`
|
|
}
|
|
_ = json.Unmarshal(in, &a)
|
|
id := pid(a.IntentID)
|
|
if id <= 0 {
|
|
return actool.Errorf("intent_id is required"), nil
|
|
}
|
|
intentNode, nodeErr := t.ts.GetNodeWithSources(id)
|
|
if nodeErr != nil {
|
|
return actool.Errorf(nodeErr.Error()), nil
|
|
}
|
|
if intentNode == nil || intentNode.Kind != db.KindIntent {
|
|
return actool.Errorf("intent_id does not belong to this task or a directly related task"), nil
|
|
}
|
|
// ③ detail drill-down by step ids, thinking excluded by the store.
|
|
if len(a.StepIDs) > 0 {
|
|
// Dedup + drop invalid ids first so garbage/duplicates don't eat into
|
|
// the per-call cap. detail is returned in full (untruncated), so the
|
|
// cap bounds one tool result; over the cap we serve the first N and
|
|
// tell the model exactly which ids were deferred, instead of erroring
|
|
// and forcing it to re-plan the call.
|
|
const maxStepIDs = 5
|
|
var ids []int64
|
|
seen := make(map[int64]bool)
|
|
for _, raw := range a.StepIDs {
|
|
if v := pid(raw); v > 0 && !seen[v] {
|
|
seen[v] = true
|
|
ids = append(ids, v)
|
|
}
|
|
}
|
|
var omitted []int64
|
|
if len(ids) > maxStepIDs {
|
|
omitted = append(omitted, ids[maxStepIDs:]...)
|
|
ids = ids[:maxStepIDs]
|
|
}
|
|
acts, err := t.ts.ActivityByIDsWithSources(ids)
|
|
if err != nil {
|
|
return actool.Errorf(err.Error()), nil
|
|
}
|
|
steps := make([]map[string]any, 0, len(acts))
|
|
for i := range acts {
|
|
if acts[i].NodeID == nil || *acts[i].NodeID != id || acts[i].Inherited != intentNode.Inherited ||
|
|
(acts[i].Inherited && acts[i].SourceTaskID != intentNode.SourceTaskID) {
|
|
continue
|
|
}
|
|
step := map[string]any{
|
|
"step_id": acts[i].ID, "kind": acts[i].Kind, "tool": acts[i].Tool,
|
|
"is_error": acts[i].IsError, "detail": acts[i].Detail,
|
|
}
|
|
if acts[i].Inherited {
|
|
inheritedMap(step, acts[i].SourceTaskID)
|
|
}
|
|
steps = append(steps, step)
|
|
}
|
|
result := map[string]any{"intent_id": id, "steps": steps, "returned_step_ids": ids}
|
|
if len(omitted) > 0 {
|
|
// returned_step_ids/omitted_step_ids let the model decide programmatically
|
|
// whether another call is worth it; the notice states the same in prose.
|
|
result["omitted_step_ids"] = omitted
|
|
result["notice"] = fmt.Sprintf(
|
|
"At most %d steps' full content can be fetched at once. This call returned the first %d (%v). The %d not fetched are %v. "+
|
|
"If what you have is enough to locate the issue, do not fetch the rest. If you still need them, call again with those step_ids.",
|
|
maxStepIDs, len(ids), ids, len(omitted), omitted)
|
|
}
|
|
if intentNode.Inherited {
|
|
inheritedMap(result, intentNode.SourceTaskID)
|
|
}
|
|
return jsonResult(result)
|
|
}
|
|
// ①/② summary stream, optionally keyword-filtered; 100-char summaries.
|
|
var acts []db.Activity
|
|
var err error
|
|
if strings.TrimSpace(a.Q) != "" {
|
|
acts, err = t.ts.ActivityTraceSearchWithSources(id, a.Q, a.Limit)
|
|
} else {
|
|
acts, err = t.ts.ActivityTraceWithSources(id, a.Limit)
|
|
}
|
|
if err != nil {
|
|
return actool.Errorf(err.Error()), nil
|
|
}
|
|
result := map[string]any{"intent_id": id, "steps": traceSteps(acts)}
|
|
if intentNode.Inherited {
|
|
inheritedMap(result, intentNode.SourceTaskID)
|
|
}
|
|
return jsonResult(result)
|
|
})
|
|
}
|
|
|
|
// searchAllWorkerTraces keyword-searches EVERY work's process in this task — for
|
|
// finding what a worker saw but never wrote back as a fact. Returns only matching
|
|
// summaries (≤100 chars), each tagged with its intent_id for follow-up drill-down.
|
|
func (t *ToolSet) searchAllWorkerTraces() actool.CoreTool {
|
|
return t.readExpTool("search_all_worker_traces",
|
|
"Usually not recommended, because the system already provides most of this information. Search other works' execution traces in this task by keyword (q), to recover something a worker saw but did not write into a fact (a path, a token, an error, and similar). "+
|
|
"Steps of your own intent are excluded automatically (those are already in your context). "+
|
|
"Only matching step summaries are returned (summary at most 100 characters), each with an intent_id. Then use get_worker_trace(intent_id, step_ids=[...]) for the full content.",
|
|
obj(map[string]any{
|
|
"q": str("Keyword, searched in every work step's summary and full output"),
|
|
"limit": intp("Return cap. Default 100 (optional)."),
|
|
}, "q"),
|
|
func(_ context.Context, in json.RawMessage) (actool.Result, error) {
|
|
var a struct {
|
|
Q string `json:"q"`
|
|
Limit int `json:"limit"`
|
|
}
|
|
_ = json.Unmarshal(in, &a)
|
|
if strings.TrimSpace(a.Q) == "" {
|
|
return actool.Errorf("q is required"), nil
|
|
}
|
|
// Exclude the caller's own intent. A worker's own trace is already in its context.
|
|
acts, err := t.ts.ActivityTraceSearchAllWithSources(t.ownerNode, a.Q, a.Limit)
|
|
if err != nil {
|
|
return actool.Errorf(err.Error()), nil
|
|
}
|
|
hits := make([]map[string]any, 0, len(acts))
|
|
for i := range acts {
|
|
var intent int64
|
|
if acts[i].NodeID != nil {
|
|
intent = *acts[i].NodeID
|
|
}
|
|
hit := map[string]any{
|
|
"intent_id": intent, "step_id": acts[i].ID, "worker": acts[i].Worker,
|
|
"kind": acts[i].Kind, "tool": acts[i].Tool, "is_error": acts[i].IsError,
|
|
"summary": firstLine(acts[i].Summary, 100),
|
|
}
|
|
if acts[i].Inherited {
|
|
inheritedMap(hit, acts[i].SourceTaskID)
|
|
}
|
|
hits = append(hits, hit)
|
|
}
|
|
return jsonResult(map[string]any{"query": a.Q, "hits": hits})
|
|
})
|
|
}
|
|
|
|
// listWorkerTraces gives a worker (which has no graph_overview and can't see the
|
|
// intent graph) a lightweight index of the works in this task — intent_id +
|
|
// one-line summary + state — so it can DISCOVER which works to inspect via
|
|
// get_worker_trace. Without this a worker only knows intent_ids that come back
|
|
// from search_all_worker_traces hits. Excludes still-open intents (not yet run →
|
|
// no process to inspect).
|
|
func (t *ToolSet) listWorkerTraces() actool.CoreTool {
|
|
return t.readExpTool("list_worker_traces",
|
|
"Usually not recommended, because the system already provides most of this information. List an index of works (intents) that have already run in this task: intent_id, a one-line direction (summary), and state. "+
|
|
"You (the worker) cannot see the graph. Use this to discover which works are worth reading, then get_worker_trace(intent_id) for the steps and get_worker_trace(intent_id, step_ids=[...]) for detail. "+
|
|
"Only intents that have run are listed (running, done, exhausted, blocked, stopped), not open intents that have not run. Your task boundary is still the intent you were given. Reading other work is only to reuse observations or avoid repeating effort.",
|
|
obj(map[string]any{
|
|
"q": str("Filter by a keyword in summary (optional)"),
|
|
"limit": intp("Return cap. Default 50 (optional)."),
|
|
}),
|
|
func(_ context.Context, in json.RawMessage) (actool.Result, error) {
|
|
var a struct {
|
|
Q string `json:"q"`
|
|
Limit int `json:"limit"`
|
|
}
|
|
_ = json.Unmarshal(in, &a)
|
|
limit := a.Limit
|
|
if limit <= 0 {
|
|
limit = 50
|
|
}
|
|
all, err := t.ts.ListByKindWithSources(db.KindIntent, 500)
|
|
if err != nil {
|
|
return actool.Errorf(err.Error()), nil
|
|
}
|
|
q := strings.ToLower(strings.TrimSpace(a.Q))
|
|
out := make([]map[string]any, 0, limit)
|
|
for _, n := range all {
|
|
if n.Inherited && n.State == "running" {
|
|
continue
|
|
}
|
|
switch n.State {
|
|
case "running", "done", "exhausted", "blocked", "stopped": // has run → has a process
|
|
default:
|
|
continue
|
|
}
|
|
var p map[string]any
|
|
_ = json.Unmarshal(n.Payload, &p)
|
|
summary, _ := p["summary"].(string)
|
|
if q != "" && !strings.Contains(strings.ToLower(summary), q) {
|
|
continue
|
|
}
|
|
item := map[string]any{"intent_id": n.ID, "summary": summary, "state": n.State}
|
|
if n.Inherited {
|
|
inheritedMap(item, n.SourceTaskID)
|
|
}
|
|
out = append(out, item)
|
|
if len(out) >= limit {
|
|
break
|
|
}
|
|
}
|
|
return jsonResult(map[string]any{"works": out})
|
|
})
|
|
}
|
|
|
|
// PlannerTools is the read + intent-generation + goal-judgement tool set.
|
|
func (t *ToolSet) PlannerTools() []actool.CoreTool {
|
|
return []actool.CoreTool{
|
|
t.graphOverview(), t.listFindings(), t.listFacts(), t.nodeDetail(),
|
|
// cold-digest §6.1: restore folded cold nodes (digest body → members → detail).
|
|
t.expandDigest(),
|
|
t.getWorkerOutput(), t.getWorkerTrace(), t.searchAllWorkerTraces(), t.listGoals(), t.addIntent(), t.proveGoal(), t.goalMet(),
|
|
t.killWorkTool(), t.steerWorkTool(),
|
|
// report_finding: while judging the situation, the planner may record a vulnerability it has already confirmed (the same tool the worker uses).
|
|
t.addFinding(),
|
|
// list_companies: company list, scope, and asset counts (to obtain company_id and understand ownership).
|
|
t.listCompanies(),
|
|
// list_assets: during planning, search the whole asset store with a DSL. Together with
|
|
// list_untested_assets (untested assets inside scope), this adds "search the whole
|
|
// store by domain, fingerprint, port, status code, and similar".
|
|
t.listAssets(),
|
|
// add_company_scope: during planning, add a domain, IP, CIDR, ICP record, or keyword to a company's asset scope (matching assets are claimed automatically).
|
|
t.addCompanyScope(),
|
|
// add_task_scope: pull a whole root domain, a whole company, a subdomain, or an IP into this task's test scope (the coverage denominator).
|
|
t.addTaskScope(),
|
|
// list_untested_assets: on demand, list untested assets inside this task's scope (by type, paged) and decide whether to cover them.
|
|
t.listUntestedAssets(),
|
|
}
|
|
}
|