diff --git a/db/lessons.go b/db/lessons.go index c1261ff..9ea2519 100644 --- a/db/lessons.go +++ b/db/lessons.go @@ -114,13 +114,16 @@ func (d *DB) ExtractLessonsForTask(ctx context.Context, taskID int64) (int, erro // NULL company_id is not a scope. write := len(companyIDs) <= 1 companyID := lessonExtractCompanyID(companyIDs) + // Only a completed retest with a verdict is an outcome. A pending, running, + // failed or stopped retest can carry a staged verdict that never finished. rows, err := d.QueryContext(ctx, ` SELECT f.id, f.vulnclass, f.name, f.summary, f.severity, f.status, COALESCE(r.id,0), COALESCE(r.verdict,'') FROM findings f LEFT JOIN LATERAL ( SELECT id, verdict FROM finding_retests - WHERE finding_id=f.id ORDER BY id DESC LIMIT 1 + WHERE finding_id=f.id AND status='completed' AND verdict<>'' + ORDER BY id DESC LIMIT 1 ) r ON true WHERE f.task_id=$1 ORDER BY f.id`, taskID) diff --git a/db/lessons_test.go b/db/lessons_test.go index 7fcd291..3d616ca 100644 --- a/db/lessons_test.go +++ b/db/lessons_test.go @@ -1082,6 +1082,112 @@ func insertLessonRetest(t *testing.T, d *DB, findingID int64, verdict string) in return id } +func insertLessonRetestStatus(t *testing.T, d *DB, findingID int64, status, verdict string) int64 { + t.Helper() + var id int64 + if err := d.QueryRow(` + INSERT INTO finding_retests(finding_id,status,verdict,snapshot) + VALUES ($1,$2,$3,'{}'::jsonb) + RETURNING id`, findingID, status, verdict).Scan(&id); err != nil { + t.Fatal(err) + } + t.Cleanup(func() { cleanupExec(t, d, `DELETE FROM finding_retests WHERE id=$1`, id) }) + return id +} + +// retestEvidence counts every evidence row of this retest, live or withdrawn, +// in lessons of any kind. +func retestEvidence(t *testing.T, d *DB, retestID int64) int { + t.Helper() + var n int + if err := d.QueryRow(` + SELECT COUNT(*) FROM rsi_lesson_evidence + WHERE source_kind='retest' AND source_id=$1`, retestID).Scan(&n); err != nil { + t.Fatal(err) + } + return n +} + +// A staged verdict on a retest that did not complete is not an outcome: +// FinishFindingRetest turns completed-without-verdict into failed, and a +// stopped or failed retest keeps whatever verdict the agent staged. +func TestExtractLessonsIgnoresUnfinishedRetests(t *testing.T) { + d, err := Open(testDSN(t)) + if err != nil { + SkipPGUnavailable(t, err) + } + t.Cleanup(func() { d.Close() }) + + stamp := time.Now().UnixNano() + task, err := d.CreateTask(fmt.Sprintf("lesson unfinished retest %d", stamp), "goal", nil, 0, 0) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { cleanupErr(t, "task", d.DeleteTask(task.ID)) }) + cue := fmt.Sprintf("xss-unfinished-%d", stamp) + label := fmt.Sprintf("reflected in search %d", stamp) + t.Cleanup(func() { cleanupExec(t, d, `DELETE FROM rsi_lessons WHERE cue=$1`, cue) }) + var fid int64 + if err := d.QueryRow(` + INSERT INTO findings(task_id,vulnclass,name,summary,severity,status) + VALUES ($1,$2,'Reflected XSS',$3,'high','confirmed') + RETURNING id`, task.ID, cue, label).Scan(&fid); err != nil { + t.Fatal(err) + } + t.Cleanup(func() { cleanupExec(t, d, `DELETE FROM findings WHERE id=$1`, fid) }) + + for _, status := range []string{"pending", "running", "failed", "stopped"} { + for _, verdict := range []string{"reproduced", "fixed", "inconclusive"} { + rid := insertLessonRetestStatus(t, d, fid, status, verdict) + if _, err := d.ExtractLessonsForTask(t.Context(), task.ID); err != nil { + t.Fatal(err) + } + if n := retestEvidence(t, d, rid); n != 0 { + t.Fatalf("retest status=%s verdict=%s wrote %d evidence rows", status, verdict, n) + } + if status == "pending" || status == "running" { + // idx_finding_retests_active allows one active retest per finding. + cleanupExec(t, d, `DELETE FROM finding_retests WHERE id=$1`, rid) + } + } + } + var retestLessons int + if err := d.QueryRow(`SELECT COUNT(*) FROM rsi_lessons + WHERE cue=$1 AND (kind='caution' OR action LIKE 'Reproduced %')`, cue).Scan(&retestLessons); err != nil { + t.Fatal(err) + } + if retestLessons != 0 { + t.Fatalf("unfinished retests produced %d lessons", retestLessons) + } + + done := insertLessonRetestStatus(t, d, fid, "completed", "reproduced") + if _, err := d.ExtractLessonsForTask(t.Context(), task.ID); err != nil { + t.Fatal(err) + } + if n := positiveRetestEvidence(t, d, done); n != 1 { + t.Fatalf("completed reproduced retest: positive evidence=%d, want 1", n) + } + findLessonByAction(t, d, task.ID, "positive", "Reproduced", label) + + // A later unfinished retest does not hide the completed one. + insertLessonRetestStatus(t, d, fid, "running", "fixed") + if _, err := d.ExtractLessonsForTask(t.Context(), task.ID); err != nil { + t.Fatal(err) + } + if n := positiveRetestEvidence(t, d, done); n != 1 { + t.Fatalf("completed retest after a running one: positive evidence=%d, want 1", n) + } + + inconclusive := insertLessonRetestStatus(t, d, fid, "completed", "inconclusive") + if _, err := d.ExtractLessonsForTask(t.Context(), task.ID); err != nil { + t.Fatal(err) + } + if n := retestEvidence(t, d, inconclusive); n != 1 { + t.Fatalf("completed inconclusive retest: evidence=%d, want 1", n) + } + findLessonByAction(t, d, task.ID, "caution", "Recheck", cue) +} + func findLessonByAction(t *testing.T, d *DB, taskID int64, kind, actionPrefix, label string) *Lesson { t.Helper() lessons, err := d.TaskLessons(t.Context(), taskID, 8) diff --git a/docs/rsi-lessons.md b/docs/rsi-lessons.md index bb0cfc4..f7731e9 100644 --- a/docs/rsi-lessons.md +++ b/docs/rsi-lessons.md @@ -61,6 +61,10 @@ Rules: Pending and in-progress findings produce nothing: there is no outcome yet. Empty cue, action, or source is refused by the gate. +A retest counts only when it is `completed` with a verdict; extraction reads +the latest such retest. A verdict staged on a pending, running, failed, or +stopped retest produces nothing. + A task with more than one company in `task_scope` does not gain a new lesson. Retraction still runs, so a lesson written while the task had one company can be withdrawn after a second company is added. Per-finding company attribution