From 42ce6ea5c85430dff2208c498d0e9fe201448e32 Mon Sep 17 00:00:00 2001 From: dach-a Date: Sat, 21 Nov 2026 10:20:45 +0000 Subject: [PATCH] db: pick the newest activity across all evidence lines MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- agent/fact_provenance_test.go | 22 ++++++++++++++++++++++ db/exploration.go | 25 +++++++++++++------------ 2 files changed, 35 insertions(+), 12 deletions(-) diff --git a/agent/fact_provenance_test.go b/agent/fact_provenance_test.go index 1940c77..0c4d4f0 100644 --- a/agent/fact_provenance_test.go +++ b/agent/fact_provenance_test.go @@ -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 { diff --git a/db/exploration.go b/db/exploration.go index 3a30135..aa63910 100644 --- a/db/exploration.go +++ b/db/exploration.go @@ -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.