db: pick the newest activity across all evidence lines

ActivityContainingEvidence queried one line at a time and returned on the
first match, so an older activity matching line 0 beat a newer one matching
line 1. A single query over unnest() orders by id and keeps the global newest,
and a regression test covers the reversed-order case.

🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>
This commit is contained in:
dach-a
2026-11-21 10:20:45 +00:00
parent c92c784987
commit 42ce6ea5c8
2 changed files with 35 additions and 12 deletions
+22
View File
@@ -366,6 +366,28 @@ func TestFactProvenanceTiedToRecordedActivity(t *testing.T) {
}
}
// The tie picks the newest matching activity across all lines, not the newest
// match for whichever line the model wrote first.
func TestActivityContainingEvidenceNewestAcrossLines(t *testing.T) {
s := guardTaskStore(t)
intent := addGuardNode(t, s, db.KindIntent, map[string]any{"text": "collect banners"}, "running")
older, err := s.AppendActivity(db.Activity{NodeID: &intent, Kind: "tool_result", Tool: "Bash", Detail: "Server: nginx/1.25.3"})
if err != nil {
t.Fatal(err)
}
newer, err := s.AppendActivity(db.Activity{NodeID: &intent, Kind: "tool_result", Tool: "Bash", Detail: "X-Powered-By: Express"})
if err != nil {
t.Fatal(err)
}
id, _, ok, err := s.ActivityContainingEvidence(intent, []string{"Server: nginx/1.25.3", "X-Powered-By: Express"})
if err != nil || !ok || id != newer || newer <= older {
t.Fatalf("ActivityContainingEvidence = %d, %v, %v; want newest %d", id, ok, err, newer)
}
if id, _, ok, err := s.ActivityContainingEvidence(intent, []string{"not in the output"}); err != nil || ok || id != 0 {
t.Fatalf("no-match = %d, %v, %v; want 0/false/nil", id, ok, err)
}
}
// fakeFactTraffic answers like the traffic store and records the window asked
// for, so the test can check both the classification and the bounds.
type fakeFactTraffic struct {
+13 -12
View File
@@ -1779,20 +1779,21 @@ func (s *ExplorationStore) ActivityContainingEvidence(nodeID int64, lines []stri
if nodeID <= 0 || len(lines) == 0 {
return 0, time.Time{}, false, nil
}
for _, line := range lines {
err := s.db.QueryRow(`SELECT id, created_at FROM activity
// One query across all lines: the newest matching activity wins, not the
// newest match for whichever line came first.
err = s.db.QueryRow(`SELECT id, created_at FROM activity a
WHERE exploration_id=$1 AND node_id=$2
AND (strpos(COALESCE(detail,''), $3) > 0 OR strpos(COALESCE(summary,''), $3) > 0)
ORDER BY id DESC LIMIT 1`, s.expID, nodeID, line).Scan(&id, &createdAt)
if err == sql.ErrNoRows {
continue
}
if err != nil {
return 0, time.Time{}, false, err
}
return id, createdAt, true, nil
AND EXISTS (SELECT 1 FROM unnest($3::text[]) AS line
WHERE strpos(COALESCE(a.detail,''), line) > 0
OR strpos(COALESCE(a.summary,''), line) > 0)
ORDER BY id DESC LIMIT 1`, s.expID, nodeID, lines).Scan(&id, &createdAt)
if err == sql.ErrNoRows {
return 0, time.Time{}, false, nil
}
return 0, time.Time{}, false, nil
if err != nil {
return 0, time.Time{}, false, err
}
return id, createdAt, true, nil
}
// ActivityListForTerminalIntent is the inherited, incremental-read boundary.