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)

fix(db): retract lesson evidence when a disposition reverses

Coff0xcc0ce7a9494d3
diff --git a/db/lessons.go b/db/lessons.go
index a4639f3..c3a43e2 100644
--- a/db/lessons.go
+++ b/db/lessons.go
@@ -93,10 +93,13 @@ func lessonExtractCompanyID(companyIDs []int64) int64 {
}
// ExtractLessonsForTask derives candidate lessons from one task's findings and
-// retests. It is idempotent: the evidence primary key means a second run adds
-// no support. A task with more than one company is skipped so a generic
-// (NULL company_id) lesson cannot leak across companies. Returns the number of
-// distinct lessons touched.
+// retests. It is idempotent for the same disposition: the evidence primary key
+// means a second run adds no support. A later run after a reversed disposition
+// retracts that finding's evidence from the opposite-kind lesson and retires
+// the lesson when nothing else supports it. Confirming again restores the
+// evidence and un-retires. A task with more than one company is skipped so a
+// generic (NULL company_id) lesson cannot leak across companies. Returns the
+// number of distinct lessons written.
func (d *DB) ExtractLessonsForTask(ctx context.Context, taskID int64) (int, error) {
categoryID, companyIDs, err := taskLessonScope(ctx, d, taskID)
if err != nil {
@@ -175,11 +178,22 @@ func (d *DB) ExtractLessonsForTask(ctx context.Context, taskID int64) (int, erro
return 0, err
}
}
+ if err := d.retractLessonEvidence(ctx, "finding", findingID, "negative"); err != nil {
+ return 0, err
+ }
case "false_positive", "duplicate", "ignored", "risk_accepted":
if err := add("negative", "Do not re-report without new mechanics: "+label+" (prior "+status+")",
"finding", findingID, sourceDetail); err != nil {
return 0, err
}
+ if err := d.retractLessonEvidence(ctx, "finding", findingID, "positive"); err != nil {
+ return 0, err
+ }
+ if retestID > 0 {
+ if err := d.retractLessonEvidence(ctx, "retest", retestID, "positive"); err != nil {
+ return 0, err
+ }
+ }
}
if retestID > 0 && (verdict == "fixed" || verdict == "inconclusive") {
if err := add("caution", "Recheck before reporting "+cue+": a prior retest was "+verdict,
@@ -196,11 +210,59 @@ func (d *DB) upsertLesson(ctx context.Context, key, kind, cue, action string, ca
err := d.QueryRowContext(ctx, `
INSERT INTO rsi_lessons(dedupe_key,kind,cue,action,category_id,company_id)
VALUES($1,$2,$3,$4,NULLIF($5,0),NULLIF($6,0))
- ON CONFLICT (dedupe_key) DO UPDATE SET updated_at=now()
+ ON CONFLICT (dedupe_key) DO UPDATE SET
+ updated_at=now(),
+ status=CASE WHEN rsi_lessons.status='retired' THEN 'candidate' ELSE rsi_lessons.status END
RETURNING id`, key, kind, cue, action, categoryID, companyID).Scan(&id)
return id, err
}
+// retractLessonEvidence removes this source from lessons of fromKind and
+// retires any of those lessons that now have zero evidence. Shared lessons
+// that still have other evidence stay active.
+func (d *DB) retractLessonEvidence(ctx context.Context, sourceKind string, sourceID int64, fromKind string) error {
+ rows, err := d.QueryContext(ctx, `
+ DELETE FROM rsi_lesson_evidence e
+ USING rsi_lessons l
+ WHERE e.lesson_id=l.id
+ AND l.kind=$1
+ AND e.source_kind=$2
+ AND e.source_id=$3
+ RETURNING e.lesson_id`, fromKind, sourceKind, sourceID)
+ if err != nil {
+ return err
+ }
+ defer rows.Close()
+ seen := map[int64]bool{}
+ var ids []int64
+ for rows.Next() {
+ var id int64
+ if err := rows.Scan(&id); err != nil {
+ return err
+ }
+ if !seen[id] {
+ seen[id] = true
+ ids = append(ids, id)
+ }
+ }
+ if err := rows.Err(); err != nil {
+ return err
+ }
+ if err := rows.Close(); err != nil {
+ return err
+ }
+ if len(ids) == 0 {
+ return nil
+ }
+ _, err = d.ExecContext(ctx, `
+ UPDATE rsi_lessons l
+ SET status='retired', updated_at=now()
+ WHERE l.id=ANY($1::bigint[])
+ AND l.status<>'retired'
+ AND NOT EXISTS (SELECT 1 FROM rsi_lesson_evidence e WHERE e.lesson_id=l.id)`, ids)
+ return err
+}
+
// TaskLessons selects lessons visible to one task: generic lessons plus those
// scoped to the task's company and category. Scope matches rank first, then
// positive before negative before caution, then support.
diff --git a/db/lessons_test.go b/db/lessons_test.go
index 2fc87b1..8106196 100644
--- a/db/lessons_test.go
+++ b/db/lessons_test.go
@@ -453,3 +453,206 @@ func insertLessonFinding(t *testing.T, d *DB, taskID int64, status string) int64
t.Cleanup(func() { cleanupExec(t, d, `DELETE FROM findings WHERE id=$1`, id) })
return id
}
+
+func TestExtractLessonsRetractsReversedDisposition(t *testing.T) {
+ d, err := Open(testDSN(t))
+ if err != nil {
+ t.Skipf("postgres unavailable (%v) — skipping", err)
+ }
+ t.Cleanup(func() { d.Close() })
+
+ stamp := time.Now().UnixNano()
+ task, err := d.CreateTask(fmt.Sprintf("lesson retract %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-retract-%d", stamp)
+ label := fmt.Sprintf("reflected in search %d", stamp)
+ 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)
+ }
+
+ n, err := d.ExtractLessonsForTask(t.Context(), task.ID)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if n != 1 {
+ t.Fatalf("first extract=%d, want 1", n)
+ }
+ first, err := d.TaskLessons(t.Context(), task.ID, 8)
+ if err != nil {
+ t.Fatal(err)
+ }
+ var pos *Lesson
+ for i := range first {
+ if first[i].Kind == "positive" && strings.Contains(first[i].Action, label) {
+ pos = &first[i]
+ break
+ }
+ }
+ if pos == nil {
+ t.Fatalf("no positive lesson: %+v", first)
+ }
+ t.Cleanup(func() { cleanupExec(t, d, `DELETE FROM rsi_lessons WHERE id=$1`, pos.ID) })
+ posID := pos.ID
+
+ if _, err := d.SetFindingStatus(fid, FindingFalsePositive); err != nil {
+ t.Fatal(err)
+ }
+ if _, err := d.ExtractLessonsForTask(t.Context(), task.ID); err != nil {
+ t.Fatal(err)
+ }
+
+ var posStatus string
+ var posSupport int
+ if err := d.QueryRow(`SELECT status,
+ (SELECT COUNT(*) FROM rsi_lesson_evidence e WHERE e.lesson_id=rsi_lessons.id)
+ FROM rsi_lessons WHERE id=$1`, posID).Scan(&posStatus, &posSupport); err != nil {
+ t.Fatal(err)
+ }
+ if posStatus != "retired" || posSupport != 0 {
+ t.Fatalf("positive after FP: status=%s support=%d", posStatus, posSupport)
+ }
+
+ afterFP, err := d.TaskLessons(t.Context(), task.ID, 8)
+ if err != nil {
+ t.Fatal(err)
+ }
+ var neg *Lesson
+ for i := range afterFP {
+ if afterFP[i].ID == posID {
+ t.Fatal("retired positive still selected")
+ }
+ if afterFP[i].Kind == "negative" && strings.Contains(afterFP[i].Action, label) {
+ neg = &afterFP[i]
+ }
+ }
+ if neg == nil {
+ t.Fatalf("no negative lesson after FP: %+v", afterFP)
+ }
+ t.Cleanup(func() { cleanupExec(t, d, `DELETE FROM rsi_lessons WHERE id=$1`, neg.ID) })
+
+ if _, err := d.SetFindingStatus(fid, FindingConfirmed); err != nil {
+ t.Fatal(err)
+ }
+ if _, err := d.ExtractLessonsForTask(t.Context(), task.ID); err != nil {
+ t.Fatal(err)
+ }
+
+ if err := d.QueryRow(`SELECT status,
+ (SELECT COUNT(*) FROM rsi_lesson_evidence e WHERE e.lesson_id=rsi_lessons.id)
+ FROM rsi_lessons WHERE id=$1`, posID).Scan(&posStatus, &posSupport); err != nil {
+ t.Fatal(err)
+ }
+ if posStatus != "candidate" || posSupport != 1 {
+ t.Fatalf("positive after re-confirm: status=%s support=%d", posStatus, posSupport)
+ }
+
+ var negStatus string
+ var negSupport int
+ if err := d.QueryRow(`SELECT status,
+ (SELECT COUNT(*) FROM rsi_lesson_evidence e WHERE e.lesson_id=rsi_lessons.id)
+ FROM rsi_lessons WHERE id=$1`, neg.ID).Scan(&negStatus, &negSupport); err != nil {
+ t.Fatal(err)
+ }
+ if negStatus != "retired" || negSupport != 0 {
+ t.Fatalf("negative after re-confirm: status=%s support=%d", negStatus, negSupport)
+ }
+}
+
+func TestExtractLessonsRetireOnlyWhenLastEvidence(t *testing.T) {
+ d, err := Open(testDSN(t))
+ if err != nil {
+ t.Skipf("postgres unavailable (%v) — skipping", err)
+ }
+ t.Cleanup(func() { d.Close() })
+
+ stamp := time.Now().UnixNano()
+ task, err := d.CreateTask(fmt.Sprintf("lesson shared retract %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-shared-%d", stamp)
+ label := fmt.Sprintf("reflected in search %d", stamp)
+ var aID, bID 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(&aID); err != nil {
+ t.Fatal(err)
+ }
+ 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(&bID); err != nil {
+ t.Fatal(err)
+ }
+
+ if _, err := d.ExtractLessonsForTask(t.Context(), task.ID); err != nil {
+ t.Fatal(err)
+ }
+ lessons, err := d.TaskLessons(t.Context(), task.ID, 8)
+ if err != nil {
+ t.Fatal(err)
+ }
+ var pos *Lesson
+ for i := range lessons {
+ if lessons[i].Kind == "positive" && strings.Contains(lessons[i].Action, label) {
+ pos = &lessons[i]
+ break
+ }
+ }
+ if pos == nil {
+ t.Fatalf("no shared positive lesson: %+v", lessons)
+ }
+ t.Cleanup(func() { cleanupExec(t, d, `DELETE FROM rsi_lessons WHERE id=$1`, pos.ID) })
+ if pos.Support != 2 {
+ t.Fatalf("shared support=%d, want 2", pos.Support)
+ }
+
+ if _, err := d.SetFindingStatus(aID, FindingFalsePositive); err != nil {
+ t.Fatal(err)
+ }
+ if _, err := d.ExtractLessonsForTask(t.Context(), task.ID); err != nil {
+ t.Fatal(err)
+ }
+
+ var status string
+ var support int
+ if err := d.QueryRow(`SELECT status,
+ (SELECT COUNT(*) FROM rsi_lesson_evidence e WHERE e.lesson_id=rsi_lessons.id)
+ FROM rsi_lessons WHERE id=$1`, pos.ID).Scan(&status, &support); err != nil {
+ t.Fatal(err)
+ }
+ if status == "retired" || support != 1 {
+ t.Fatalf("shared positive should stay active with support 1, got status=%s support=%d", status, support)
+ }
+
+ after, err := d.TaskLessons(t.Context(), task.ID, 8)
+ if err != nil {
+ t.Fatal(err)
+ }
+ var sawNeg bool
+ for i := range after {
+ if after[i].ID == pos.ID && after[i].Kind == "positive" {
+ if after[i].Support != 1 {
+ t.Fatalf("selected support=%d, want 1", after[i].Support)
+ }
+ }
+ if after[i].Kind == "negative" && strings.Contains(after[i].Action, label) {
+ sawNeg = true
+ t.Cleanup(func() { cleanupExec(t, d, `DELETE FROM rsi_lessons WHERE id=$1`, after[i].ID) })
+ }
+ }
+ if !sawNeg {
+ t.Fatalf("expected a negative lesson for the flipped finding: %+v", after)
+ }
+}
diff --git a/docs/rsi-lessons.md b/docs/rsi-lessons.md
index b17bf59..6d75a25 100644
--- a/docs/rsi-lessons.md
+++ b/docs/rsi-lessons.md
@@ -32,9 +32,13 @@ Extraction runs automatically when a task reaches a terminal status
POST /api/tasks/{id}/lessons/extract
```
-It is idempotent. `rsi_lesson_evidence` is keyed by `(lesson, source_kind,
-source_id)`, so a second run for the same task adds no evidence and cannot
-inflate support.
+It is idempotent for the same disposition. `rsi_lesson_evidence` is keyed by
+`(lesson, source_kind, source_id)`, so a second run for the same finding adds
+no evidence and cannot inflate support. A later extract after a reversed
+disposition retracts that finding's evidence from the opposite-kind lesson
+and retires the lesson when its support hits zero. Confirming again restores
+the evidence and un-retires. Extraction does not run on status PATCH; call
+the endpoint after triage.
Rules:
@@ -87,7 +91,8 @@ What this layer deliberately does not do:
- no adoption: `status` is `candidate`; `adopted` and `retired` are reserved for
a later, owner-approved phase;
- no prompt mutation, no benchmark probing, no lesson editing through the UI;
-- no decay or deletion; the evidence table is the audit trail.
+- no time-based decay; reversed dispositions retract that finding's evidence
+ from the opposite-kind lesson on the next extract.
## Measuring it