ARTEX/db/planner_focus.go

65 lines
2.3 KiB
Go

package db
import (
"context"
"strings"
)
// PlanningGoals cannot let newly met goals evict an older unmet goal from the
// planner's view. History remains available after the open goals.
func (s *ExplorationStore) PlanningGoals(limit int) ([]*Node, error) {
return s.PlanningGoalsContext(context.Background(), limit)
}
func (s *ExplorationStore) PlanningGoalsContext(ctx context.Context, limit int) ([]*Node, error) {
if limit <= 0 || limit > 1000 {
limit = 100
}
rows, err := s.db.QueryContext(ctx, `SELECT `+nodeCols+` FROM exploration_nodes WHERE exploration_id=$1 AND kind='goal' ORDER BY (state='open') DESC,id DESC LIMIT $2`, s.expID, limit)
if err != nil {
return nil, err
}
defer rows.Close()
return scanNodes(rows)
}
func (s *ExplorationStore) PlanningGoalCounts(ctx context.Context) (total, open int, err error) {
err = s.db.QueryRowContext(ctx, `SELECT count(*),count(*) FILTER(WHERE state='open') FROM exploration_nodes WHERE exploration_id=$1 AND kind='goal'`, s.expID).Scan(&total, &open)
return
}
// PlanningContinuations searches all live fact nodes, independently of recent
// windows or cold-digest membership. Dismissed/superseded facts are excluded.
// These are candidates: only the planner can judge relevance to an open goal.
func (s *ExplorationStore) PlanningContinuations(ctx context.Context, before int64, limit int, q string) (nodes []*Node, total int, more bool, err error) {
if limit <= 0 {
limit = 10
}
if limit > 50 {
limit = 50
}
where := `exploration_id=$1 AND kind='fact' AND state='confirmed'
AND ((COALESCE(payload->>'outcome','') NOT IN ('verified','negative') AND length(btrim(COALESCE(payload->>'checkpoint','')))>0)
OR (payload->>'outcome'='blocked' AND length(btrim(COALESCE(payload->>'revisit_condition','')))>0))
AND ($2='' OR payload::text ILIKE '%'||$2||'%')`
q = strings.TrimSpace(q)
err = s.db.QueryRowContext(ctx, `SELECT count(*) FROM exploration_nodes WHERE `+where, s.expID, q).Scan(&total)
if err != nil {
return
}
rows, e := s.db.QueryContext(ctx, `SELECT `+nodeCols+` FROM exploration_nodes WHERE `+where+` AND ($3<=0 OR id<$3) ORDER BY id DESC LIMIT $4`, s.expID, q, before, limit+1)
if e != nil {
err = e
return
}
defer rows.Close()
nodes, err = scanNodes(rows)
if err != nil {
return
}
more = len(nodes) > limit
if more {
nodes = nodes[:limit]
}
return
}