feat(agent): list_facts 改为分页+关键词过滤,避免上下文膨胀

fact 多时旧的 list_facts 一次返回每任务至多 500 条且 summary 不截断,
一次调用就可能撑爆上下文。改为分页(默认 20,最新在前)+ q 关键词过滤,
返回 {facts, total, has_more, next_before},summary 超长按 rune 截断。

- db: listByKindPageFiltered/countByKindFiltered(payload->>'summary' ILIKE)
  + ListByKindPageWithSources 跨本任务/继承任务合并分页
- agent: list_facts 加 limit/before/q 参数,compactFact 截断 summary;
  worker/planner 提示词同步说明分页语义
- server: 旧库一次性迁移把 list_facts 新 schema 刷进 tools 目录表
  (SeedTool 首插入only,否则工具管理页显示「无参数」);flag bump 到 v7
This commit is contained in:
Autumn-27 committed 2026-08-27 12:15:02 -04:00
1 parent 6b6b56e0a7
commit 10f6a4fbc7
7 files changed
+139 -11

No files matched your search

+51 -6
View File
@@ -898,19 +898,64 @@ func (t *ToolSet) listFindings() actool.CoreTool {
})
}
// 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 readTool("list_facts", "列本任务及直接关联任务的【探索事实/结论】(紧凑:id+摘要+状态)。关联任务条目带 source_task_id/inherited=true 且只读。详情用 node_detail(id),漏洞看 list_findings。",
obj(map[string]any{}),
func(context.Context, json.RawMessage) (actool.Result, error) {
f, _ := t.ts.ListByKindWithSources(db.KindFact, 500)
return readTool("list_facts", "分页列本任务及直接关联任务的【探索事实/结论】,最新在前(紧凑:id+摘要+状态,摘要过长会截断,全文用 node_detail(id))。参数均可选:limit(默认 20,上限 100)、before(游标,传上一页返回的 next_before 取更旧的一页;省略/0=最新一页)、q(按摘要关键词过滤)。返回 {facts, total, has_more, next_before}:total 是过滤后的总数,has_more=true 时用 next_before 继续翻页。关联任务条目带 source_task_id/inherited=true 且只读。漏洞看 list_findings。",
obj(map[string]any{
"limit": intp("返回条数,默认 20,上限 100"),
"before": intp("分页游标:只返回 id 小于该值的更旧事实;省略或 0 = 最新一页"),
"q": str("按事实摘要关键词过滤(不区分大小写);省略 = 不过滤"),
}),
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, compactNode(n))
out = append(out, compactFact(n))
}
return jsonResult(out)
res := map[string]any{"facts": out, "total": total, "has_more": hasMore}
if hasMore && len(f) > 0 {
res["next_before"] = f[len(f)-1].ID // 传回它取下一页(更旧的)
}
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 readTool("node_detail", "按 id 取本任务或直接关联任务的【探索图节点】完整内容。继承节点带 source_task_id/inherited=true 且只读。仅限 list_facts/list_findings/graph_overview 返回的探索节点 id;资产请用 list_assets/asset_neighbors。",
obj(map[string]any{"id": idp("探索图节点 id(非资产 id)")}, "id"),