Skip to content

armgit / ARTEX

Public Imported from GitHub
0

ARTEX - AI autonomous penetration testing system (English translation; upstream: Autumn-27/ARTEX, AGPL-3.0)

db: pick the newest activity across all evidence lines

dach-a42ce6ea5c854
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
+ }
+ if err != nil {
+ return 0, time.Time{}, false, err
}
- return 0, time.Time{}, false, nil
+ return id, createdAt, true, nil
}
// ActivityListForTerminalIntent is the inherited, incremental-read boundary.