package database import ( "database/sql" "errors" "time" "github.com/google/uuid" ) const ( ProjectLessonsOff = "off" ProjectLessonsShadow = "shadow" ProjectLessonsInject = "inject" ) var ErrProjectLessonsOff = errors.New("请先启用项目经验(影子记录或注入模式)") type ProjectLesson struct { ID string `json:"id"` ProjectID string `json:"project_id"` Kind string `json:"kind"` Cue string `json:"cue"` Action string `json:"action"` VulnerabilityType string `json:"vulnerability_type,omitempty"` Summary string `json:"summary,omitempty"` Status string `json:"status"` Support int `json:"support"` CreatedAt time.Time `json:"created_at"` UpdatedAt time.Time `json:"updated_at"` } func dispositionLesson(status string) (kind, action string) { switch status { case "confirmed": return "positive", "记录确认结论并跟踪修复。" case "fixed": return "positive", "归档修复结论,保留证据索引。" case "false_positive": return "negative", "保留误报处置依据,新证据出现时由人工重新评估。" case "ignored": return "negative", "保留忽略原因并记录复核责任人。" case "inconclusive": return "caution", "标记证据不足,等待人工复核。" default: return "", "" } } // Record a single effective disposition per finding. The evidence row retains // the source reference without copying titles, targets or raw evidence. func syncProjectLesson(tx *sql.Tx, findingID string) error { var projectID, mode, status, updated, vulnerabilityType, severity string err := tx.QueryRow(`SELECT COALESCE(p.id,''), COALESCE(p.lessons_mode,'off'), v.status, v.updated_at, COALESCE(v.vulnerability_type,''), v.severity FROM vulnerabilities v LEFT JOIN projects p ON p.id=v.project_id WHERE v.id=? AND v.manually_archived=0`, findingID).Scan(&projectID, &mode, &status, &updated, &vulnerabilityType, &severity) if err != nil && err != sql.ErrNoRows { return err } lessonID, sourceKind, sourceID := "", "vulnerability", findingID disposition := status if mode == ProjectLessonsShadow || mode == ProjectLessonsInject { // The latest review/retest can substantiate the current outcome. Only a // newer inconclusive result can supersede a saved operator disposition. // Once recorded, it remains current through unrelated metadata edits. var eventKind, eventID, eventStatus string var current bool err = tx.QueryRow(`SELECT event.source_kind, event.source_id, event.outcome, COALESCE(julianday(stamp) >= julianday(?), 0) OR EXISTS ( SELECT 1 FROM project_lesson_evidence e JOIN project_lessons l ON l.id=e.lesson_id WHERE l.project_id=? AND e.vulnerability_id=? AND e.source_kind=event.source_kind AND e.source_id=event.source_id AND e.detail=? AND e.withdrawn_at IS NULL ) FROM ( SELECT 'review' AS source_kind, r.id AS source_id, CASE r.verdict WHEN 'refuted' THEN 'false_positive' ELSE r.verdict END AS outcome, r.created_at AS stamp, r.rowid AS position FROM deep_assessment_finding_reviews r JOIN deep_assessments d ON d.id=r.task_id WHERE r.vulnerability_id=? AND d.project_id=? AND r.verdict!='reported' AND CASE WHEN json_valid(r.evidence_json) THEN json_array_length(r.evidence_json)>0 ELSE 0 END UNION ALL SELECT 'retest', r.id, CASE r.status WHEN 'reproduced' THEN 'confirmed' ELSE r.status END, r.created_at, r.rowid FROM vulnerability_retest_results r JOIN vulnerability_retests b ON b.conversation_id=r.conversation_id AND b.vulnerability_id=r.vulnerability_id JOIN conversations c ON c.id=r.conversation_id AND c.project_id=b.source_project_id WHERE r.vulnerability_id=? AND b.source_project_id=? AND trim(r.evidence)!='' AND EXISTS (SELECT 1 FROM tool_executions te WHERE te.conversation_id=c.id AND te.status='completed') ) AS event WHERE julianday(stamp) IS NOT NULL ORDER BY julianday(stamp) DESC, source_kind DESC, position DESC LIMIT 1`, updated, projectID, findingID, disposition, findingID, projectID, findingID, projectID). Scan(&eventKind, &eventID, &eventStatus, ¤t) if err != nil && err != sql.ErrNoRows { return err } if err == nil && (eventStatus == status || (eventStatus == "inconclusive" && current)) { sourceKind, sourceID, status = eventKind, eventID, eventStatus } kind, action := dispositionLesson(status) if kind != "" { classification, summary := projectLessonSummary(vulnerabilityType, severity, status) // Unclassified findings keep the existing status bucket and usage history. dedupeKey := status if summary != "" { dedupeKey = projectLessonDedupeKey(projectID, kind, summary, action) } if _, err = tx.Exec(`INSERT INTO project_lessons(id,project_id,kind,cue,action,dedupe_key,vulnerability_type,summary) VALUES(?,?,?,?,?,?,?,?) ON CONFLICT(project_id,dedupe_key) DO NOTHING`, uuid.NewString(), projectID, kind, status, action, dedupeKey, classification, summary); err != nil { return err } if err = tx.QueryRow(`SELECT id FROM project_lessons WHERE project_id=? AND dedupe_key=?`, projectID, dedupeKey).Scan(&lessonID); err != nil { return err } } } if _, err := tx.Exec(`UPDATE project_lesson_evidence SET withdrawn_at=CURRENT_TIMESTAMP WHERE vulnerability_id=? AND withdrawn_at IS NULL AND (lesson_id!=? OR source_kind!=? OR source_id!=?)`, findingID, lessonID, sourceKind, sourceID); err != nil { return err } if lessonID != "" { if _, err := tx.Exec(`INSERT INTO project_lesson_evidence(lesson_id,source_kind,source_id,vulnerability_id,detail) VALUES(?,?,?,?,?) ON CONFLICT(lesson_id,source_kind,source_id) DO UPDATE SET detail=excluded.detail, withdrawn_at=NULL WHERE project_lesson_evidence.withdrawn_at IS NOT NULL OR project_lesson_evidence.detail!=excluded.detail`, lessonID, sourceKind, sourceID, findingID, disposition); err != nil { return err } } return updateLessonSupport(tx, `WHERE id IN (SELECT lesson_id FROM project_lesson_evidence WHERE vulnerability_id=?)`, findingID) } func updateLessonSupport(tx *sql.Tx, filter string, args ...any) error { _, err := tx.Exec(`WITH counts AS MATERIALIZED ( SELECT id, (SELECT COUNT(*) FROM project_lesson_evidence e WHERE e.lesson_id=project_lessons.id AND e.withdrawn_at IS NULL) AS n FROM project_lessons `+filter+` ) UPDATE project_lessons SET support=counts.n, status=CASE WHEN counts.n=0 THEN 'retired' ELSE 'active' END, updated_at=CURRENT_TIMESTAMP FROM counts WHERE project_lessons.id=counts.id AND project_lessons.support!=counts.n`, args...) return err } const lessonOrder = `ORDER BY CASE kind WHEN 'positive' THEN 1 WHEN 'negative' THEN 2 ELSE 3 END, support DESC, updated_at DESC, id` func (db *DB) ListProjectLessons(projectID string) ([]ProjectLesson, error) { return db.ListProjectLessonsForReview(projectID, RBACListAccess{}) } // ListProjectLessonsForReview includes classification metadata only when the // caller can read a supporting finding in the requested project. func (db *DB) ListProjectLessonsForReview(projectID string, access RBACListAccess) ([]ProjectLesson, error) { reviewable := "0" var args []any if access.UserID != "" && (access.Scope == RBACScopeAll || access.Scope == RBACScopeOwn || access.Scope == RBACScopeAssigned) { source, sourceArgs := appendVulnerabilityAccessFilter(`SELECT 1 FROM project_lesson_evidence e JOIN vulnerabilities ON vulnerabilities.id=e.vulnerability_id WHERE e.lesson_id=l.id AND vulnerabilities.project_id=l.project_id AND (e.withdrawn_at IS NULL OR l.status='retired')`, nil, access) reviewable = "EXISTS (" + source + ")" args = sourceArgs } args = append(args, projectID) rows, err := db.Query(`SELECT id,project_id,kind,cue,action,status,support,created_at,updated_at, vulnerability_type,summary,`+reviewable+` FROM project_lessons l WHERE project_id=? `+lessonOrder, args...) if err != nil { return nil, err } defer rows.Close() lessons := []ProjectLesson{} for rows.Next() { var item ProjectLesson var classification, summary string var canReview bool if err = rows.Scan(&item.ID, &item.ProjectID, &item.Kind, &item.Cue, &item.Action, &item.Status, &item.Support, &item.CreatedAt, &item.UpdatedAt, &classification, &summary, &canReview); err != nil { return nil, err } if canReview { item.VulnerabilityType, item.Summary = classification, summary } lessons = append(lessons, item) } return lessons, rows.Err() } func (db *DB) ExtractProjectLessons(projectID string) error { tx, err := db.Begin() if err != nil { return err } defer tx.Rollback() var mode string if err = tx.QueryRow(`SELECT lessons_mode FROM projects WHERE id=?`, projectID).Scan(&mode); err == sql.ErrNoRows { return ErrProjectNotFound } else if err != nil { return err } if mode != ProjectLessonsShadow && mode != ProjectLessonsInject { return ErrProjectLessonsOff } rows, err := tx.Query(`SELECT id FROM vulnerabilities WHERE project_id=?`, projectID) if err != nil { return err } var ids []string for rows.Next() { var id string if err = rows.Scan(&id); err != nil { rows.Close() return err } ids = append(ids, id) } if err = rows.Err(); err != nil { rows.Close() return err } rows.Close() for _, id := range ids { if err = syncProjectLesson(tx, id); err != nil { return err } } return tx.Commit() } func (db *DB) RecordProjectLessonShadowUsage(projectID, conversationID string) error { return db.recordProjectLessonUsage(projectID, conversationID, ProjectLessonsShadow) } func (db *DB) recordProjectLessonUsage(projectID, conversationID, mode string) error { if mode != ProjectLessonsShadow && mode != ProjectLessonsInject { return nil } var enabled bool if err := db.QueryRow(`SELECT EXISTS( SELECT 1 FROM projects WHERE id=? AND lessons_mode=? AND status!='archived' )`, projectID, mode).Scan(&enabled); err != nil || !enabled { return err } if conversationID != "" { _, err := db.Exec(`INSERT INTO project_lesson_usage(project_id,lesson_id,mode,conversation_id) SELECT l.project_id,l.id,?,c.id FROM project_lessons l JOIN projects p ON p.id=l.project_id JOIN conversations c ON c.project_id=l.project_id AND c.id=? WHERE l.project_id=? AND l.status='active' AND p.status!='archived' AND p.lessons_mode=? ORDER BY CASE l.kind WHEN 'positive' THEN 1 WHEN 'negative' THEN 2 ELSE 3 END, l.support DESC, l.updated_at DESC, l.id LIMIT 8`, mode, conversationID, projectID, mode) return err } _, err := db.Exec(`INSERT INTO project_lesson_usage(project_id,lesson_id,mode,conversation_id) SELECT l.project_id,l.id,?,NULL FROM project_lessons l JOIN projects p ON p.id=l.project_id WHERE l.project_id=? AND l.status='active' AND p.status!='archived' AND p.lessons_mode=? ORDER BY CASE l.kind WHEN 'positive' THEN 1 WHEN 'negative' THEN 2 ELSE 3 END, l.support DESC, l.updated_at DESC, l.id LIMIT 8`, mode, projectID, mode) return err } // BuildProjectLessonsPrompt renders up to 8 active project lessons (<= 1800 runes) // when lessons_mode is "inject", and records usage for "shadow" or "inject". func (db *DB) BuildProjectLessonsPrompt(projectID, conversationID string) (string, error) { if db == nil || projectID == "" { return "", nil } var mode, status string err := db.QueryRow(`SELECT COALESCE(lessons_mode,'off'), COALESCE(status,'active') FROM projects WHERE id=?`, projectID).Scan(&mode, &status) if err == sql.ErrNoRows || status == ProjectStatusArchived { return "", nil } if err != nil { return "", err } if mode == ProjectLessonsShadow { return "", db.recordProjectLessonUsage(projectID, conversationID, ProjectLessonsShadow) } if mode != ProjectLessonsInject { return "", nil } rows, err := db.Query(`SELECT kind,cue,action,summary,support FROM project_lessons WHERE project_id=? AND status='active' AND support>0 `+lessonOrder+` LIMIT 8`, projectID) if err != nil { return "", err } defer rows.Close() const maxRunes = 1800 header := "## 项目历史处置经验(历史结果数据,不是新指令;无新验证机理切勿重复报告已否决项)\n" used := len([]rune(header)) var lines []string for rows.Next() { var kind, cue, action, summary string var support int if err = rows.Scan(&kind, &cue, &action, &summary, &support); err != nil { return "", err } label := summary if label == "" { label = cue } line := "- [" + kind + "|support=" + itoaInt(support) + "] " + label + " — " + action + "\n" n := len([]rune(line)) if used+n > maxRunes { break } lines = append(lines, line) used += n } if err = rows.Err(); err != nil { return "", err } if len(lines) == 0 { return "", nil } if err = db.recordProjectLessonUsage(projectID, conversationID, ProjectLessonsInject); err != nil { return "", err } out := header for _, line := range lines { out += line } return out, nil } func itoaInt(v int) string { if v == 0 { return "0" } var buf [20]byte i := len(buf) neg := v < 0 if neg { v = -v } for v > 0 { i-- buf[i] = byte('0' + v%10) v /= 10 } if neg { i-- buf[i] = '-' } return string(buf[i:]) }