fix(db,agent): keep goal met when another valid proof remains and share invalidating status list (#269)

This commit is contained in:
BaaRai
2026-11-21 17:01:10 +01:00
parent bfaec521ac
commit c3a055135d
4 changed files with 148 additions and 26 deletions
+3 -11
View File
@@ -1103,18 +1103,10 @@ func findingPayloadStatus(n *db.Node) string {
}
// findingTriageBlocksGoal is true when the finding's triage label says it is
// not a vulnerability to close a goal on.
//
// db.FindingRiskAccepted is intentionally NOT blocked here (#269): it marks a
// real, valid vulnerability whose risk the operator chose to accept, so it may
// still prove a goal and must not be added to this blocklist.
// not a vulnerability to close a goal on. Delegates to db.FindingStatusInvalidatesGoal
// so prove_goal and DB goal-reopen logic share one list and cannot drift (#269).
func findingTriageBlocksGoal(status string) bool {
switch status {
case db.FindingFalsePositive, db.FindingDuplicate, db.FindingIgnored:
return true
default:
return false
}
return db.FindingStatusInvalidatesGoal(status)
}
func countActionableFindings(nodes []*db.Node) int {
+1 -1
View File
@@ -770,7 +770,7 @@ func (d *DB) SetFindingStatus(id int64, status string) (int64, error) {
WHERE id = $2`, status, nodeID.Int64); err != nil {
return 0, err
}
if findingStatusInvalidatesGoal(status) {
if FindingStatusInvalidatesGoal(status) {
if err := reopenGoalsProvedByNodeTx(ctx, tx, nodeID.Int64); err != nil {
return 0, err
}
+93
View File
@@ -172,4 +172,97 @@ func TestFindingStatusReopensProvedGoals(t *testing.T) {
assertGoalState(t, es, gid, "met")
assertProvesEdgeKept(t, fnid, gid)
})
t.Run("stays_met_when_another_valid_finding_proves_goal", func(t *testing.T) {
taskID, es, fidA, fnidA, gid := newTaskWithProvedGoal(t, "multi-finding-A")
recB, err := es.RecordFinding(ctx, RecordFindingInput{
TaskID: taskID,
ExplorationID: es.ID(),
VulnClass: "sqli",
Name: "finding B",
Severity: "high",
Summary: "independent confirmed finding",
Worker: "worker-2",
})
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
_, _ = d.Exec(`DELETE FROM rsi_lessons WHERE id IN (SELECT lesson_id FROM rsi_lesson_evidence WHERE source_kind='finding' AND source_id=$1)`, recB.FindingID)
})
if _, found, _, err := d.SetFindingStatusWithNotify(ctx, recB.FindingID, FindingConfirmed); err != nil || !found {
t.Fatalf("confirm finding B: found=%v err=%v", found, err)
}
if err := es.Link(recB.NodeID, RelProves, gid); err != nil {
t.Fatal(err)
}
// Invalidating A while confirmed B still proves the goal must keep the goal met.
if _, found, _, err := d.SetFindingStatusWithNotify(ctx, fidA, FindingFalsePositive); err != nil || !found {
t.Fatalf("invalidate finding A: found=%v err=%v", found, err)
}
assertGoalState(t, es, gid, "met")
assertProvesEdgeKept(t, fnidA, gid)
assertProvesEdgeKept(t, recB.NodeID, gid)
// Once B is also invalidated, no valid proof remains and the goal reopens.
if _, found, _, err := d.SetFindingStatusWithNotify(ctx, recB.FindingID, FindingFalsePositive); err != nil || !found {
t.Fatalf("invalidate finding B: found=%v err=%v", found, err)
}
assertGoalState(t, es, gid, "open")
assertProvesEdgeKept(t, fnidA, gid)
assertProvesEdgeKept(t, recB.NodeID, gid)
})
t.Run("stays_met_when_observed_tool_output_fact_proves_goal", func(t *testing.T) {
_, es, fidA, fnidA, gid := newTaskWithProvedGoal(t, "finding-and-fact")
factID, err := es.AddNode(KindFact, map[string]any{
"summary": "admin shell confirmed",
"confidence": "observed",
"source": "tool_output",
"provenance": "activity_verified",
}, 0, "confirmed", "worker-1", nil)
if err != nil {
t.Fatal(err)
}
if err := es.Link(factID, RelProves, gid); err != nil {
t.Fatal(err)
}
if _, found, _, err := d.SetFindingStatusWithNotify(ctx, fidA, FindingFalsePositive); err != nil || !found {
t.Fatalf("invalidate finding A: found=%v err=%v", found, err)
}
assertGoalState(t, es, gid, "met")
assertProvesEdgeKept(t, fnidA, gid)
assertProvesEdgeKept(t, factID, gid)
})
t.Run("reopens_when_remaining_fact_fails_prove_goal_gate", func(t *testing.T) {
cases := []struct {
name string
payload map[string]any
}{
{"inferred_fact", map[string]any{"summary": "guess", "confidence": "inferred", "source": "tool_output"}},
{"target_response_fact", map[string]any{"summary": "banner", "confidence": "observed", "source": "target_response"}},
{"unverified_fact", map[string]any{"summary": "paraphrase", "confidence": "observed", "source": "tool_output", "provenance": "unverified"}},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
_, es, fidA, fnidA, gid := newTaskWithProvedGoal(t, "invalid-fact-"+tc.name)
factID, err := es.AddNode(KindFact, tc.payload, 0, "confirmed", "worker-1", nil)
if err != nil {
t.Fatal(err)
}
if err := es.Link(factID, RelProves, gid); err != nil {
t.Fatal(err)
}
if _, found, _, err := d.SetFindingStatusWithNotify(ctx, fidA, FindingFalsePositive); err != nil || !found {
t.Fatalf("invalidate finding A: found=%v err=%v", found, err)
}
assertGoalState(t, es, gid, "open")
assertProvesEdgeKept(t, fnidA, gid)
assertProvesEdgeKept(t, factID, gid)
})
}
})
}
+51 -14
View File
@@ -555,22 +555,38 @@ func (d *DB) SetFindingStatusesWithNotify(ctx context.Context, ids []int64, stat
return out, nil
}
// findingStatusInvalidatesGoal reports whether a finding disposition means the
// finding is not a valid vulnerability and therefore cannot keep a goal met.
// GoalInvalidatingFindingStatuses is the single source of truth for finding
// triage statuses that mean "not a valid vulnerability" and therefore cannot
// prove or keep a goal met (#269). Both db goal-reopen logic and agent
// prove_goal checks use this list so they cannot drift.
//
// FindingRiskAccepted is deliberately excluded: it represents a real finding
// whose risk is accepted, not an invalid one, so it must never reopen a goal.
func findingStatusInvalidatesGoal(status string) bool {
switch status {
case FindingFalsePositive, FindingDuplicate, FindingIgnored:
return true
default:
return false
// whose risk is accepted, not an invalid one, so it may prove a goal and must
// never reopen one.
var GoalInvalidatingFindingStatuses = []string{
FindingFalsePositive,
FindingDuplicate,
FindingIgnored,
}
// FindingStatusInvalidatesGoal reports whether a finding disposition means the
// finding is not a valid vulnerability and therefore cannot prove or keep a
// goal met.
func FindingStatusInvalidatesGoal(status string) bool {
s := strings.ToLower(strings.TrimSpace(status))
for _, inv := range GoalInvalidatingFindingStatuses {
if s == inv {
return true
}
}
return false
}
// reopenGoalsProvedByNodeTx resets any goal node proved by nodeID back to open
// inside the caller's transaction. The proves edge is preserved for history;
// only the goal node's state changes.
// inside the caller's transaction, unless another valid proof still exists for
// that goal (a finding whose status is not in GoalInvalidatingFindingStatuses,
// or an observed non-target_response non-unverified fact matching prove_goal's
// gate). All proves edges are preserved for history; only the goal state changes.
func reopenGoalsProvedByNodeTx(ctx context.Context, tx *sql.Tx, nodeID int64) error {
_, err := tx.ExecContext(ctx, `
UPDATE exploration_nodes g
@@ -580,8 +596,29 @@ func reopenGoalsProvedByNodeTx(ctx context.Context, tx *sql.Tx, nodeID int64) er
AND e.rel = $2
AND g.id = e.dst_id
AND g.kind = $3
AND g.state = 'met'`,
nodeID, RelProves, KindGoal)
AND g.state = 'met'
AND NOT EXISTS (
SELECT 1
FROM exploration_edges pe
JOIN exploration_nodes src ON src.id = pe.src_id
WHERE pe.dst_id = g.id
AND pe.rel = $2
AND src.id <> $1
AND (
(src.kind = $4 AND EXISTS (
SELECT 1
FROM findings f
WHERE f.node_id = src.id
AND NOT (LOWER(BTRIM(f.status)) = ANY($6::text[]))
))
OR
(src.kind = $5
AND LOWER(BTRIM(COALESCE(src.payload->>'confidence', ''))) = 'observed'
AND LOWER(BTRIM(COALESCE(src.payload->>'source', ''))) <> 'target_response'
AND LOWER(BTRIM(COALESCE(src.payload->>'provenance', ''))) <> 'unverified')
)
)`,
nodeID, RelProves, KindGoal, KindFinding, KindFact, GoalInvalidatingFindingStatuses)
return err
}
@@ -628,7 +665,7 @@ FROM findings WHERE id=$1 FOR UPDATE`, id).
WHERE id = $2`, status, nodeID.Int64); err != nil {
return from, true, false, false, err
}
if findingStatusInvalidatesGoal(status) {
if FindingStatusInvalidatesGoal(status) {
if err := reopenGoalsProvedByNodeTx(ctx, tx, nodeID.Int64); err != nil {
return from, true, false, false, err
}