Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

47.4 KB
Raw Blame
// Package unified provides a unified alert/finding system that bridges
// threshold-based alerts and AI-generated findings into a single intelligence layer.
package unified

import (
	"encoding/json"
	"fmt"
	"strings"
	"sync"
	"time"

	"github.com/rs/zerolog/log"

	"github.com/rcourtman/pulse-go-rewrite/pkg/aicontracts"
)

// FindingSource identifies where a finding originated
type FindingSource string

const (
	// SourceThreshold indicates a finding created from a threshold alert
	SourceThreshold FindingSource = "threshold"
	// SourceAIPatrol indicates a finding created by AI patrol analysis
	SourceAIPatrol FindingSource = "ai-patrol"
	// SourceAIChat indicates a finding from interactive AI chat
	SourceAIChat FindingSource = "ai-chat"
	// SourceAnomaly indicates a finding from baseline anomaly detection
	SourceAnomaly FindingSource = "anomaly"
	// SourceCorrelation indicates a finding from root-cause correlation
	SourceCorrelation FindingSource = "correlation"
	// SourceForecast indicates a proactive finding from trend forecasting
	SourceForecast FindingSource = "forecast"
)

// UnifiedSeverity maps different severity systems to a common scale
type UnifiedSeverity string

const (
	SeverityInfo     UnifiedSeverity = "info"
	SeverityWatch    UnifiedSeverity = "watch"
	SeverityWarning  UnifiedSeverity = "warning"
	SeverityCritical UnifiedSeverity = "critical"
)

// UnifiedCategory groups findings by type
type UnifiedCategory string

const (
	CategoryPerformance   UnifiedCategory = "performance"
	CategoryCapacity      UnifiedCategory = "capacity"
	CategoryReliability   UnifiedCategory = "reliability"
	CategoryBackup        UnifiedCategory = "backup"
	CategorySecurity      UnifiedCategory = "security"
	CategoryConnectivity  UnifiedCategory = "connectivity"
	CategoryConfiguration UnifiedCategory = "configuration"
	CategoryGeneral       UnifiedCategory = "general"
)

type UnifiedFindingLifecycleEvent struct {
	At       time.Time         `json:"at"`
	Type     string            `json:"type"`
	Message  string            `json:"message,omitempty"`
	From     string            `json:"from,omitempty"`
	To       string            `json:"to,omitempty"`
	Metadata map[string]string `json:"metadata,omitempty"`
}

// UnifiedFinding represents a unified finding that can originate from
// threshold alerts, AI analysis, or other detection methods.
// unifiedFindingJSON deliberately mirrors this struct field-for-field
// (marshal-mirror pattern); merging them would change the public literal API.
// TestUnifiedFindingJSONMirrorStaysInSync enforces the mirror.
//
//nolint:dupl // deliberate marshal-mirror twin of unifiedFindingJSON
type UnifiedFinding struct {
	ID                         string          `json:"id"`
	Source                     FindingSource   `json:"source"`
	Severity                   UnifiedSeverity `json:"severity"`
	Category                   UnifiedCategory `json:"category"`
	ResourceID                 string          `json:"resource_id"`
	ResourceName               string          `json:"resource_name"`
	ResourceType               string          `json:"resource_type"`
	Node                       string          `json:"node,omitempty"`
	Title                      string          `json:"title"`
	Description                string          `json:"description"`
	Impact                     string          `json:"impact,omitempty"` // Operator-facing consequence-if-ignored statement
	PreviousResolvedFixSummary string          `json:"previous_resolved_fix_summary,omitempty"`
	Recommendation             string          `json:"recommendation,omitempty"`
	Evidence                   string          `json:"evidence,omitempty"`

	// Threshold-specific fields (when Source == "threshold")
	AlertIdentifier string  `json:"-"`
	AlertType       string  `json:"alert_type,omitempty"` // cpu, memory, disk, etc.
	Value           float64 `json:"value,omitempty"`
	Threshold       float64 `json:"threshold,omitempty"`
	IsThreshold     bool    `json:"is_threshold"`

	// AI enhancement fields
	AIContext     string     `json:"ai_context,omitempty"`     // AI-added context/explanation
	RootCauseID   string     `json:"root_cause_id,omitempty"`  // Linked root cause finding
	CorrelatedIDs []string   `json:"correlated_ids,omitempty"` // Related findings
	RemediationID string     `json:"remediation_id,omitempty"` // Linked remediation plan
	AIConfidence  float64    `json:"ai_confidence,omitempty"`  // AI confidence score (0-1)
	EnhancedByAI  bool       `json:"enhanced_by_ai"`           // Whether AI has analyzed this
	AIEnhancedAt  *time.Time `json:"ai_enhanced_at,omitempty"` // When AI analyzed

	// Investigation fields (autonomous patrol investigation)
	InvestigationSessionID string                           `json:"investigation_session_id,omitempty"`
	InvestigationStatus    string                           `json:"investigation_status,omitempty"`
	InvestigationOutcome   string                           `json:"investigation_outcome,omitempty"`
	LastInvestigatedAt     *time.Time                       `json:"last_investigated_at,omitempty"`
	InvestigationAttempts  int                              `json:"investigation_attempts,omitempty"`
	InvestigationRecord    *aicontracts.InvestigationRecord `json:"investigation_record,omitempty"`
	LoopState              string                           `json:"loop_state,omitempty"`
	Lifecycle              []UnifiedFindingLifecycleEvent   `json:"lifecycle,omitempty"`
	RegressionCount        int                              `json:"regression_count,omitempty"`
	LastRegressionAt       *time.Time                       `json:"last_regression_at,omitempty"`

	// Timestamps
	DetectedAt time.Time  `json:"detected_at"`
	LastSeenAt time.Time  `json:"last_seen_at"`
	ResolvedAt *time.Time `json:"resolved_at,omitempty"`
	// AutoResolved distinguishes Pulse's auto-detection of the condition
	// clearing from an operator-driven manual "Mark resolved" action. Used by
	// the surface to attribute who closed the loop ("Resolved by you" vs
	// "Pulse auto-resolved") so operational memory of operator actions is
	// preserved instead of being flattened into a single "resolved" state.
	AutoResolved bool `json:"auto_resolved"`

	// User feedback
	AcknowledgedAt  *time.Time `json:"acknowledged_at,omitempty"`
	SnoozedUntil    *time.Time `json:"snoozed_until,omitempty"`
	DismissedReason string     `json:"dismissed_reason,omitempty"`
	UserNote        string     `json:"user_note,omitempty"`
	Suppressed      bool       `json:"suppressed"`
	TimesRaised     int        `json:"times_raised"`
	// RemindAt is the will_fix_later wake-up deadline. When set, Patrol stays
	// quiet on re-detection until this time passes; once it passes, the next
	// re-detection clears the dismissal and emits a "reminded" lifecycle event.
	// See ai-runtime / patrol-intelligence subsystem contracts.
	RemindAt *time.Time `json:"remind_at,omitempty"`
}

// unifiedFindingJSON is the marshal mirror of UnifiedFinding: identical
// fields, but AlertIdentifier round-trips as "alert_identifier" in
// persistence while the public struct hides it with `json:"-"`. The mirror is
// enforced by TestUnifiedFindingJSONMirrorStaysInSync.
//
//nolint:dupl // deliberate marshal-mirror twin of UnifiedFinding
type unifiedFindingJSON struct {
	ID                         string                           `json:"id"`
	Source                     FindingSource                    `json:"source"`
	Severity                   UnifiedSeverity                  `json:"severity"`
	Category                   UnifiedCategory                  `json:"category"`
	ResourceID                 string                           `json:"resource_id"`
	ResourceName               string                           `json:"resource_name"`
	ResourceType               string                           `json:"resource_type"`
	Node                       string                           `json:"node,omitempty"`
	Title                      string                           `json:"title"`
	Description                string                           `json:"description"`
	Impact                     string                           `json:"impact,omitempty"`
	PreviousResolvedFixSummary string                           `json:"previous_resolved_fix_summary,omitempty"`
	Recommendation             string                           `json:"recommendation,omitempty"`
	Evidence                   string                           `json:"evidence,omitempty"`
	AlertIdentifier            string                           `json:"alert_identifier,omitempty"`
	AlertType                  string                           `json:"alert_type,omitempty"`
	Value                      float64                          `json:"value,omitempty"`
	Threshold                  float64                          `json:"threshold,omitempty"`
	IsThreshold                bool                             `json:"is_threshold"`
	AIContext                  string                           `json:"ai_context,omitempty"`
	RootCauseID                string                           `json:"root_cause_id,omitempty"`
	CorrelatedIDs              []string                         `json:"correlated_ids,omitempty"`
	RemediationID              string                           `json:"remediation_id,omitempty"`
	AIConfidence               float64                          `json:"ai_confidence,omitempty"`
	EnhancedByAI               bool                             `json:"enhanced_by_ai"`
	AIEnhancedAt               *time.Time                       `json:"ai_enhanced_at,omitempty"`
	InvestigationSessionID     string                           `json:"investigation_session_id,omitempty"`
	InvestigationStatus        string                           `json:"investigation_status,omitempty"`
	InvestigationOutcome       string                           `json:"investigation_outcome,omitempty"`
	LastInvestigatedAt         *time.Time                       `json:"last_investigated_at,omitempty"`
	InvestigationAttempts      int                              `json:"investigation_attempts,omitempty"`
	InvestigationRecord        *aicontracts.InvestigationRecord `json:"investigation_record,omitempty"`
	LoopState                  string                           `json:"loop_state,omitempty"`
	Lifecycle                  []UnifiedFindingLifecycleEvent   `json:"lifecycle,omitempty"`
	RegressionCount            int                              `json:"regression_count,omitempty"`
	LastRegressionAt           *time.Time                       `json:"last_regression_at,omitempty"`
	DetectedAt                 time.Time                        `json:"detected_at"`
	LastSeenAt                 time.Time                        `json:"last_seen_at"`
	ResolvedAt                 *time.Time                       `json:"resolved_at,omitempty"`
	AutoResolved               bool                             `json:"auto_resolved"`
	AcknowledgedAt             *time.Time                       `json:"acknowledged_at,omitempty"`
	SnoozedUntil               *time.Time                       `json:"snoozed_until,omitempty"`
	DismissedReason            string                           `json:"dismissed_reason,omitempty"`
	UserNote                   string                           `json:"user_note,omitempty"`
	Suppressed                 bool                             `json:"suppressed"`
	TimesRaised                int                              `json:"times_raised"`
	RemindAt                   *time.Time                       `json:"remind_at,omitempty"`
}

func (f UnifiedFinding) MarshalJSON() ([]byte, error) {
	alertIdentifier := strings.TrimSpace(f.AlertIdentifier)
	return json.Marshal(unifiedFindingJSON{
		ID:                         f.ID,
		Source:                     f.Source,
		Severity:                   f.Severity,
		Category:                   f.Category,
		ResourceID:                 f.ResourceID,
		ResourceName:               f.ResourceName,
		ResourceType:               f.ResourceType,
		Node:                       f.Node,
		Title:                      f.Title,
		Description:                f.Description,
		Impact:                     f.Impact,
		PreviousResolvedFixSummary: f.PreviousResolvedFixSummary,
		Recommendation:             f.Recommendation,
		Evidence:                   f.Evidence,
		AlertIdentifier:            alertIdentifier,
		AlertType:                  f.AlertType,
		Value:                      f.Value,
		Threshold:                  f.Threshold,
		IsThreshold:                f.IsThreshold,
		AIContext:                  f.AIContext,
		RootCauseID:                f.RootCauseID,
		CorrelatedIDs:              f.CorrelatedIDs,
		RemediationID:              f.RemediationID,
		AIConfidence:               f.AIConfidence,
		EnhancedByAI:               f.EnhancedByAI,
		AIEnhancedAt:               f.AIEnhancedAt,
		InvestigationSessionID:     f.InvestigationSessionID,
		InvestigationStatus:        f.InvestigationStatus,
		InvestigationOutcome:       f.InvestigationOutcome,
		LastInvestigatedAt:         f.LastInvestigatedAt,
		InvestigationAttempts:      f.InvestigationAttempts,
		InvestigationRecord:        f.InvestigationRecord,
		LoopState:                  f.LoopState,
		Lifecycle:                  f.Lifecycle,
		RegressionCount:            f.RegressionCount,
		LastRegressionAt:           f.LastRegressionAt,
		DetectedAt:                 f.DetectedAt,
		LastSeenAt:                 f.LastSeenAt,
		ResolvedAt:                 f.ResolvedAt,
		AutoResolved:               f.AutoResolved,
		AcknowledgedAt:             f.AcknowledgedAt,
		SnoozedUntil:               f.SnoozedUntil,
		DismissedReason:            f.DismissedReason,
		UserNote:                   f.UserNote,
		Suppressed:                 f.Suppressed,
		TimesRaised:                f.TimesRaised,
		RemindAt:                   f.RemindAt,
	})
}

func (f *UnifiedFinding) UnmarshalJSON(data []byte) error {
	var payload unifiedFindingJSON
	if err := json.Unmarshal(data, &payload); err != nil {
		return err
	}

	*f = UnifiedFinding{
		ID:                         payload.ID,
		Source:                     payload.Source,
		Severity:                   payload.Severity,
		Category:                   payload.Category,
		ResourceID:                 payload.ResourceID,
		ResourceName:               payload.ResourceName,
		ResourceType:               payload.ResourceType,
		Node:                       payload.Node,
		Title:                      payload.Title,
		Description:                payload.Description,
		Impact:                     payload.Impact,
		PreviousResolvedFixSummary: payload.PreviousResolvedFixSummary,
		Recommendation:             payload.Recommendation,
		Evidence:                   payload.Evidence,
		AlertIdentifier:            strings.TrimSpace(payload.AlertIdentifier),
		AlertType:                  payload.AlertType,
		Value:                      payload.Value,
		Threshold:                  payload.Threshold,
		IsThreshold:                payload.IsThreshold,
		AIContext:                  payload.AIContext,
		RootCauseID:                payload.RootCauseID,
		CorrelatedIDs:              payload.CorrelatedIDs,
		RemediationID:              payload.RemediationID,
		AIConfidence:               payload.AIConfidence,
		EnhancedByAI:               payload.EnhancedByAI,
		AIEnhancedAt:               payload.AIEnhancedAt,
		InvestigationSessionID:     payload.InvestigationSessionID,
		InvestigationStatus:        payload.InvestigationStatus,
		InvestigationOutcome:       payload.InvestigationOutcome,
		LastInvestigatedAt:         payload.LastInvestigatedAt,
		InvestigationAttempts:      payload.InvestigationAttempts,
		InvestigationRecord:        payload.InvestigationRecord,
		LoopState:                  payload.LoopState,
		Lifecycle:                  payload.Lifecycle,
		RegressionCount:            payload.RegressionCount,
		LastRegressionAt:           payload.LastRegressionAt,
		DetectedAt:                 payload.DetectedAt,
		LastSeenAt:                 payload.LastSeenAt,
		ResolvedAt:                 payload.ResolvedAt,
		AutoResolved:               payload.AutoResolved,
		AcknowledgedAt:             payload.AcknowledgedAt,
		SnoozedUntil:               payload.SnoozedUntil,
		DismissedReason:            payload.DismissedReason,
		UserNote:                   payload.UserNote,
		Suppressed:                 payload.Suppressed,
		TimesRaised:                payload.TimesRaised,
		RemindAt:                   payload.RemindAt,
	}
	return nil
}

// IsActive returns true if the finding is active (not resolved, snoozed, or suppressed)
func (f *UnifiedFinding) IsActive() bool {
	return f.ResolvedAt == nil && !f.IsSnoozed() && !f.Suppressed && f.DismissedReason == ""
}

// IsSnoozed returns true if the finding is currently snoozed
func (f *UnifiedFinding) IsSnoozed() bool {
	return f.SnoozedUntil != nil && time.Now().Before(*f.SnoozedUntil)
}

// AlertToFindingConfig configures how alerts are converted to findings
type AlertToFindingConfig struct {
	// DefaultCategory is used when alert type doesn't map to a specific category
	DefaultCategory UnifiedCategory
	// TypeCategoryMap maps alert types to categories
	TypeCategoryMap map[string]UnifiedCategory
}

// DefaultAlertToFindingConfig returns sensible defaults
func DefaultAlertToFindingConfig() AlertToFindingConfig {
	return AlertToFindingConfig{
		DefaultCategory: CategoryPerformance,
		TypeCategoryMap: map[string]UnifiedCategory{
			"cpu":                           CategoryPerformance,
			"memory":                        CategoryPerformance,
			"disk":                          CategoryCapacity,
			"diskRead":                      CategoryPerformance,
			"diskWrite":                     CategoryPerformance,
			"networkIn":                     CategoryPerformance,
			"networkOut":                    CategoryPerformance,
			"usage":                         CategoryCapacity,
			"storage":                       CategoryCapacity,
			"temperature":                   CategoryReliability,
			"diskTemperature":               CategoryReliability,
			"offline":                       CategoryConnectivity,
			"nodeOffline":                   CategoryConnectivity,
			"host-offline":                  CategoryConnectivity,
			"docker-host-offline":           CategoryConnectivity,
			"connectivity":                  CategoryConnectivity,
			"poweredOff":                    CategoryReliability,
			"powered-off":                   CategoryReliability,
			"backup":                        CategoryBackup,
			"backupMissing":                 CategoryBackup,
			"backupStale":                   CategoryBackup,
			"backup-age":                    CategoryBackup,
			"snapshot":                      CategoryBackup,
			"snapshotAge":                   CategoryBackup,
			"snapshot-age":                  CategoryBackup,
			"snapshotSize":                  CategoryCapacity,
			"restartLoop":                   CategoryReliability,
			"oom":                           CategoryReliability,
			"imageUpdateAvail":              CategoryConfiguration,
			"docker-container-update":       CategoryConfiguration,
			"zfs-pool-state":                CategoryReliability,
			"zfs-pool-errors":               CategoryReliability,
			"zfs-device":                    CategoryReliability,
			"storage-topology":              CategoryCapacity,
			"disk-health":                   CategoryReliability,
			"disk-wearout":                  CategoryReliability,
			"raid":                          CategoryReliability,
			"docker-container-health":       CategoryReliability,
			"docker-container-memory-limit": CategoryReliability,
			"docker-container-oom-kill":     CategoryReliability,
			"docker-container-restart-loop": CategoryReliability,
			"docker-container-state":        CategoryReliability,
			"docker-service-health":         CategoryReliability,
			"message-age":                   CategoryReliability,
		},
	}
}

// AlertAdapter provides the interface for reading alert data
type AlertAdapter interface {
	// GetAlertIdentifier returns the alert's unique identifier
	GetAlertIdentifier() string
	// GetAlertType returns the type of alert (cpu, memory, etc.)
	GetAlertType() string
	// GetAlertLevel returns the severity level
	GetAlertLevel() string
	// GetResourceID returns the affected resource ID
	GetResourceID() string
	// GetResourceName returns the human-readable resource name
	GetResourceName() string
	// GetNode returns the node name if applicable
	GetNode() string
	// GetMessage returns the alert message
	GetMessage() string
	// GetValue returns the current metric value
	GetValue() float64
	// GetThreshold returns the threshold that was exceeded
	GetThreshold() float64
	// GetStartTime returns when the alert started
	GetStartTime() time.Time
	// GetLastSeen returns when the alert was last seen
	GetLastSeen() time.Time
	// GetMetadata returns additional alert metadata
	GetMetadata() map[string]interface{}
}

// UnifiedStore manages unified findings
type UnifiedStore struct {
	mu sync.RWMutex

	findings          map[string]*UnifiedFinding
	byResource        map[string][]string // resource_id -> []finding_id
	byAlertIdentifier map[string]string   // alert_identifier -> finding_id
	bySource          map[FindingSource][]string

	config AlertToFindingConfig

	// Callbacks
	onNewFinding      func(f *UnifiedFinding)
	onFindingResolved func(f *UnifiedFinding)
	onFindingEnhanced func(f *UnifiedFinding)

	// Persistence
	persistence UnifiedPersistence
	saveTimer   *time.Timer
	savePending bool
}

// UnifiedPersistence interface for saving/loading unified findings
type UnifiedPersistence interface {
	SaveFindings(findings map[string]*UnifiedFinding) error
	LoadFindings() (map[string]*UnifiedFinding, error)
}

// NewUnifiedStore creates a new unified store
func NewUnifiedStore(config AlertToFindingConfig) *UnifiedStore {
	return &UnifiedStore{
		findings:          make(map[string]*UnifiedFinding),
		byResource:        make(map[string][]string),
		byAlertIdentifier: make(map[string]string),
		bySource:          make(map[FindingSource][]string),
		config:            config,
	}
}

// SetPersistence sets the persistence layer
func (s *UnifiedStore) SetPersistence(p UnifiedPersistence) error {
	s.mu.Lock()
	s.persistence = p
	s.mu.Unlock()

	if p != nil {
		findings, err := p.LoadFindings()
		if err != nil {
			return err
		}
		if len(findings) > 0 {
			s.mu.Lock()
			for id, f := range findings {
				s.findings[id] = f
				s.byResource[f.ResourceID] = append(s.byResource[f.ResourceID], id)
				if f.AlertIdentifier != "" {
					s.byAlertIdentifier[f.AlertIdentifier] = id
				}
				s.bySource[f.Source] = append(s.bySource[f.Source], id)
			}
			s.mu.Unlock()
		}
	}
	return nil
}

// SetOnNewFinding sets the callback for new findings
func (s *UnifiedStore) SetOnNewFinding(cb func(f *UnifiedFinding)) {
	s.mu.Lock()
	defer s.mu.Unlock()
	s.onNewFinding = cb
}

// SetOnFindingResolved sets the callback for resolved findings
func (s *UnifiedStore) SetOnFindingResolved(cb func(f *UnifiedFinding)) {
	s.mu.Lock()
	defer s.mu.Unlock()
	s.onFindingResolved = cb
}

// SetOnFindingEnhanced sets the callback for AI-enhanced findings
func (s *UnifiedStore) SetOnFindingEnhanced(cb func(f *UnifiedFinding)) {
	s.mu.Lock()
	defer s.mu.Unlock()
	s.onFindingEnhanced = cb
}

// ConvertAlert converts a threshold alert to a unified finding
func (s *UnifiedStore) ConvertAlert(alert AlertAdapter) *UnifiedFinding {
	alertIdentifier := alert.GetAlertIdentifier()
	alertType := alert.GetAlertType()

	// Determine category
	category := s.config.DefaultCategory
	if cat, ok := s.config.TypeCategoryMap[alertType]; ok {
		category = cat
	}

	// Map alert level to unified severity
	severity := SeverityWarning
	if strings.EqualFold(alert.GetAlertLevel(), "critical") {
		severity = SeverityCritical
	}

	// Determine resource type from alert type and metadata
	resourceType := determineResourceType(alertType, alert.GetMetadata())

	// Generate finding ID
	findingID := fmt.Sprintf("alert-%s-%d", alertIdentifier, time.Now().UnixNano()%1000000)

	// Create the finding
	finding := &UnifiedFinding{
		ID:              findingID,
		Source:          SourceThreshold,
		Severity:        severity,
		Category:        category,
		ResourceID:      alert.GetResourceID(),
		ResourceName:    alert.GetResourceName(),
		ResourceType:    resourceType,
		Node:            alert.GetNode(),
		Title:           generateTitle(alertType, alert.GetResourceName(), alert.GetValue(), alert.GetThreshold()),
		Description:     alert.GetMessage(),
		AlertIdentifier: alertIdentifier,
		AlertType:       alertType,
		Value:           alert.GetValue(),
		Threshold:       alert.GetThreshold(),
		IsThreshold:     true,
		DetectedAt:      alert.GetStartTime(),
		LastSeenAt:      alert.GetLastSeen(),
		TimesRaised:     1,
	}

	// Generate evidence
	finding.Evidence = fmt.Sprintf("Threshold alert triggered: %s = %.1f (threshold: %.1f)",
		alertType, alert.GetValue(), alert.GetThreshold())

	// Author detection-time impact text per alert type. The text describes the
	// operational consequence of operator inaction; it does not vary with
	// severity or threshold value because the consequence shape is intrinsic
	// to the alert type.
	finding.Impact = generateImpact(alertType)

	return finding
}

// AddFromAlert creates a unified finding from an alert
func (s *UnifiedStore) AddFromAlert(alert AlertAdapter) (*UnifiedFinding, bool) {
	alertIdentifier := alert.GetAlertIdentifier()

	s.mu.Lock()

	// Check if we already have a finding for this alert
	if existingID, ok := s.byAlertIdentifier[alertIdentifier]; ok {
		if existing := s.findings[existingID]; existing != nil {
			// Update existing finding
			existing.LastSeenAt = alert.GetLastSeen()
			existing.Value = alert.GetValue()
			existing.TimesRaised++

			// Backfill detection-time impact for findings that pre-date the
			// Impact contract: if the persisted finding has no impact and the
			// alert type has a curated impact line, adopt it on re-fire.
			// Description and Recommendation intentionally remain non-refreshed
			// so this addition does not change historical alert wording.
			if existing.Impact == "" {
				if impact := generateImpact(alert.GetAlertType()); impact != "" {
					existing.Impact = impact
				}
			}

			// Re-open if the finding was resolved (alert fired again)
			if existing.ResolvedAt != nil {
				existing.ResolvedAt = nil
				log.Debug().
					Str("finding_id", existing.ID).
					Str("alert_identifier", alertIdentifier).
					Msg("Re-opened resolved finding due to alert re-firing")
			}

			// Update severity if alert level changed
			newSeverity := SeverityWarning
			if strings.EqualFold(alert.GetAlertLevel(), "critical") {
				newSeverity = SeverityCritical
			}
			if severityOrder(newSeverity) > severityOrder(existing.Severity) {
				existing.Severity = newSeverity
			}

			s.mu.Unlock()
			s.scheduleSave()
			return existing, false
		}
	}

	// Convert and add new finding
	finding := s.ConvertAlert(alert)
	s.findings[finding.ID] = finding
	s.byResource[finding.ResourceID] = append(s.byResource[finding.ResourceID], finding.ID)
	s.byAlertIdentifier[alertIdentifier] = finding.ID
	s.bySource[SourceThreshold] = append(s.bySource[SourceThreshold], finding.ID)

	callback := s.onNewFinding
	s.mu.Unlock()

	s.scheduleSave()

	// Fire callback
	if callback != nil {
		go callback(finding)
	}

	log.Debug().
		Str("finding_id", finding.ID).
		Str("alert_identifier", alertIdentifier).
		Str("resource", finding.ResourceName).
		Str("category", string(finding.Category)).
		Msg("Created unified finding from alert")

	return finding, true
}

// AddFromAI creates a unified finding from AI analysis
func (s *UnifiedStore) AddFromAI(finding *UnifiedFinding) (*UnifiedFinding, bool) {
	if finding == nil {
		return nil, false
	}

	now := time.Now()
	if finding.Source == "" {
		finding.Source = SourceAIPatrol
	}
	if finding.DetectedAt.IsZero() {
		finding.DetectedAt = now
	}
	if finding.LastSeenAt.IsZero() {
		finding.LastSeenAt = now
	}

	s.mu.Lock()

	// For AI patrol findings, the ID is stable and should be treated as the canonical key.
	// Always merge into the existing record to avoid duplicating index entries.
	if existing, exists := s.findings[finding.ID]; exists && existing != nil {
		// Basic fields
		existing.Source = finding.Source
		existing.ResourceID = finding.ResourceID
		existing.ResourceName = finding.ResourceName
		existing.ResourceType = finding.ResourceType
		existing.Node = finding.Node
		existing.Title = finding.Title
		if severityOrder(finding.Severity) > severityOrder(existing.Severity) {
			existing.Severity = finding.Severity
		}
		existing.Category = finding.Category

		if finding.Description != "" {
			existing.Description = finding.Description
		}
		if finding.Recommendation != "" {
			existing.Recommendation = finding.Recommendation
		}
		if finding.Impact != "" {
			existing.Impact = finding.Impact
		}
		if finding.PreviousResolvedFixSummary != "" {
			existing.PreviousResolvedFixSummary = finding.PreviousResolvedFixSummary
		}
		if finding.Evidence != "" {
			existing.Evidence = finding.Evidence
		}

		// Timestamps and counters
		if finding.LastSeenAt.After(existing.LastSeenAt) {
			existing.LastSeenAt = finding.LastSeenAt
		}
		if finding.TimesRaised > 0 {
			existing.TimesRaised = finding.TimesRaised
		} else {
			existing.TimesRaised++
		}
		// Allow reopening by clearing ResolvedAt if the incoming finding is active again.
		existing.ResolvedAt = finding.ResolvedAt
		existing.AutoResolved = finding.AutoResolved

		// Investigation fields (allow clearing)
		existing.InvestigationSessionID = finding.InvestigationSessionID
		existing.InvestigationStatus = finding.InvestigationStatus
		existing.InvestigationOutcome = finding.InvestigationOutcome
		existing.LastInvestigatedAt = finding.LastInvestigatedAt
		existing.InvestigationAttempts = finding.InvestigationAttempts
		existing.InvestigationRecord = finding.InvestigationRecord
		existing.LoopState = finding.LoopState
		existing.Lifecycle = finding.Lifecycle
		existing.RegressionCount = finding.RegressionCount
		existing.LastRegressionAt = finding.LastRegressionAt

		// User feedback (allow clearing)
		existing.AcknowledgedAt = finding.AcknowledgedAt
		existing.SnoozedUntil = finding.SnoozedUntil
		existing.DismissedReason = finding.DismissedReason
		existing.UserNote = finding.UserNote
		existing.Suppressed = finding.Suppressed
		// RemindAt mirrors the will_fix_later commitment from the canonical
		// findings store; allow clearing on remind-at wake or undismiss.
		existing.RemindAt = finding.RemindAt

		// AI enhancement fields (best-effort merge)
		if finding.AIContext != "" {
			existing.AIContext = finding.AIContext
		}
		if finding.RootCauseID != "" {
			existing.RootCauseID = finding.RootCauseID
		}
		if len(finding.CorrelatedIDs) > 0 {
			existing.CorrelatedIDs = finding.CorrelatedIDs
		}
		if finding.RemediationID != "" {
			existing.RemediationID = finding.RemediationID
		}
		if finding.AIConfidence != 0 {
			existing.AIConfidence = finding.AIConfidence
		}
		if finding.EnhancedByAI {
			existing.EnhancedByAI = true
			existing.AIEnhancedAt = finding.AIEnhancedAt
		}

		s.mu.Unlock()
		s.scheduleSave()
		return existing, false
	}

	// New finding
	if finding.TimesRaised <= 0 {
		finding.TimesRaised = 1
	}
	s.findings[finding.ID] = finding
	s.byResource[finding.ResourceID] = append(s.byResource[finding.ResourceID], finding.ID)
	s.bySource[finding.Source] = append(s.bySource[finding.Source], finding.ID)

	callback := s.onNewFinding
	s.mu.Unlock()

	s.scheduleSave()

	if callback != nil {
		go callback(finding)
	}

	return finding, true
}

// ResolveByAlertIdentifier resolves a finding by its canonical alert identifier.
func (s *UnifiedStore) ResolveByAlertIdentifier(alertID string) bool {
	s.mu.Lock()

	findingID, ok := s.byAlertIdentifier[alertID]
	if !ok {
		s.mu.Unlock()
		return false
	}

	finding, ok := s.findings[findingID]
	if !ok || finding.ResolvedAt != nil {
		s.mu.Unlock()
		return false
	}

	now := time.Now()
	finding.ResolvedAt = &now

	callback := s.onFindingResolved
	s.mu.Unlock()

	s.scheduleSave()

	if callback != nil {
		go callback(finding)
	}

	log.Debug().
		Str("finding_id", findingID).
		Str("alert_identifier", alertID).
		Msg("Resolved unified finding from alert")

	return true
}

// EnhanceWithAI adds AI context to a finding
func (s *UnifiedStore) EnhanceWithAI(findingID string, context string, confidence float64, rootCauseID string, correlatedIDs []string) bool {
	s.mu.Lock()

	finding, ok := s.findings[findingID]
	if !ok {
		s.mu.Unlock()
		return false
	}

	finding.AIContext = context
	finding.AIConfidence = confidence
	finding.EnhancedByAI = true
	now := time.Now()
	finding.AIEnhancedAt = &now

	if rootCauseID != "" {
		finding.RootCauseID = rootCauseID
	}
	if len(correlatedIDs) > 0 {
		finding.CorrelatedIDs = correlatedIDs
	}

	callback := s.onFindingEnhanced
	s.mu.Unlock()

	s.scheduleSave()

	if callback != nil {
		go callback(finding)
	}

	return true
}

// LinkRemediation links a remediation plan to a finding
func (s *UnifiedStore) LinkRemediation(findingID, remediationID string) bool {
	s.mu.Lock()
	finding, ok := s.findings[findingID]
	if !ok {
		s.mu.Unlock()
		return false
	}

	finding.RemediationID = remediationID
	s.mu.Unlock()
	s.scheduleSave()
	return true
}

// Get returns a finding by ID
func (s *UnifiedStore) Get(findingID string) *UnifiedFinding {
	s.mu.RLock()
	defer s.mu.RUnlock()

	if f, ok := s.findings[findingID]; ok {
		copy := *f
		return &copy
	}
	return nil
}

// GetByAlertIdentifier returns a finding by its canonical alert identifier.
func (s *UnifiedStore) GetByAlertIdentifier(alertID string) *UnifiedFinding {
	s.mu.RLock()
	defer s.mu.RUnlock()

	if findingID, ok := s.byAlertIdentifier[alertID]; ok {
		if f, ok := s.findings[findingID]; ok {
			copy := *f
			return &copy
		}
	}
	return nil
}

// GetByResource returns all active findings for a resource
func (s *UnifiedStore) GetByResource(resourceID string) []*UnifiedFinding {
	s.mu.RLock()
	defer s.mu.RUnlock()

	var result []*UnifiedFinding
	for _, id := range s.byResource[resourceID] {
		if f, ok := s.findings[id]; ok && f.IsActive() {
			copy := *f
			result = append(result, &copy)
		}
	}
	return result
}

// GetBySource returns all active findings from a specific source
func (s *UnifiedStore) GetBySource(source FindingSource) []*UnifiedFinding {
	s.mu.RLock()
	defer s.mu.RUnlock()

	var result []*UnifiedFinding
	for _, id := range s.bySource[source] {
		if f, ok := s.findings[id]; ok && f.IsActive() {
			copy := *f
			result = append(result, &copy)
		}
	}
	return result
}

// GetActive returns all active findings
func (s *UnifiedStore) GetActive() []*UnifiedFinding {
	s.mu.RLock()
	defer s.mu.RUnlock()

	var result []*UnifiedFinding
	for _, f := range s.findings {
		if f.IsActive() {
			copy := *f
			result = append(result, &copy)
		}
	}
	return result
}

// GetAll returns all findings regardless of status.
func (s *UnifiedStore) GetAll() []*UnifiedFinding {
	s.mu.RLock()
	defer s.mu.RUnlock()

	var result []*UnifiedFinding
	for _, f := range s.findings {
		copy := *f
		result = append(result, &copy)
	}
	return result
}

// GetThresholdFindings returns all active threshold-based findings
func (s *UnifiedStore) GetThresholdFindings() []*UnifiedFinding {
	return s.GetBySource(SourceThreshold)
}

// GetAIFindings returns all active AI-generated findings
func (s *UnifiedStore) GetAIFindings() []*UnifiedFinding {
	s.mu.RLock()
	defer s.mu.RUnlock()

	var result []*UnifiedFinding
	for _, f := range s.findings {
		if f.IsActive() && f.Source != SourceThreshold {
			copy := *f
			result = append(result, &copy)
		}
	}
	return result
}

// GetUnenhanсedThresholdFindings returns threshold findings not yet enhanced by AI
func (s *UnifiedStore) GetUnenhancedThresholdFindings() []*UnifiedFinding {
	s.mu.RLock()
	defer s.mu.RUnlock()

	var result []*UnifiedFinding
	for _, f := range s.findings {
		if f.IsActive() && f.Source == SourceThreshold && !f.EnhancedByAI {
			copy := *f
			result = append(result, &copy)
		}
	}
	return result
}

// Dismiss dismisses a finding with a reason
func (s *UnifiedStore) Dismiss(findingID, reason, note string) bool {
	s.mu.Lock()

	f, ok := s.findings[findingID]
	if !ok {
		s.mu.Unlock()
		return false
	}

	f.DismissedReason = reason
	if note != "" {
		f.UserNote = note
	}
	now := time.Now()
	f.AcknowledgedAt = &now

	if reason == "not_an_issue" {
		f.Suppressed = true
	}

	s.mu.Unlock()
	s.scheduleSave()
	return true
}

// Undismiss reopens a dismissed finding while preserving its operator note.
// The Patrol findings store remains authoritative for lifecycle history; this
// mirror keeps the unified findings projection immediately consistent.
func (s *UnifiedStore) Undismiss(findingID string) bool {
	s.mu.Lock()

	f, ok := s.findings[findingID]
	if !ok || (f.DismissedReason == "" && !f.Suppressed) {
		s.mu.Unlock()
		return false
	}

	f.DismissedReason = ""
	f.Suppressed = false
	f.AcknowledgedAt = nil
	f.RemindAt = nil

	s.mu.Unlock()
	s.scheduleSave()
	return true
}

// Snooze snoozes a finding for the specified duration
func (s *UnifiedStore) Snooze(findingID string, duration time.Duration) bool {
	s.mu.Lock()

	f, ok := s.findings[findingID]
	if !ok {
		s.mu.Unlock()
		return false
	}

	until := time.Now().Add(duration)
	f.SnoozedUntil = &until

	s.mu.Unlock()
	s.scheduleSave()
	return true
}

// Acknowledge acknowledges a finding
func (s *UnifiedStore) Acknowledge(findingID string) bool {
	s.mu.Lock()

	f, ok := s.findings[findingID]
	if !ok {
		s.mu.Unlock()
		return false
	}

	now := time.Now()
	f.AcknowledgedAt = &now

	s.mu.Unlock()
	s.scheduleSave()
	return true
}

// SetUserNote sets or updates the user note on a finding
func (s *UnifiedStore) SetUserNote(findingID string, note string) bool {
	s.mu.Lock()

	f, ok := s.findings[findingID]
	if !ok {
		s.mu.Unlock()
		return false
	}

	f.UserNote = note

	s.mu.Unlock()
	s.scheduleSave()
	return true
}

// Resolve marks a finding as resolved
func (s *UnifiedStore) Resolve(findingID string) bool {
	s.mu.Lock()

	f, ok := s.findings[findingID]
	if !ok {
		s.mu.Unlock()
		return false
	}

	// A replayed Patrol resolution already carries its canonical timestamp.
	// Do not turn synchronization into another recovery event.
	if f.ResolvedAt == nil {
		now := time.Now()
		f.ResolvedAt = &now
	}
	f.SnoozedUntil = nil

	s.mu.Unlock()
	s.scheduleSave()
	return true
}

// GetSummary returns a summary of active findings
func (s *UnifiedStore) GetSummary() UnifiedSummary {
	s.mu.RLock()
	defer s.mu.RUnlock()

	summary := UnifiedSummary{
		BySource:   make(map[FindingSource]int),
		ByCategory: make(map[UnifiedCategory]int),
	}

	for _, f := range s.findings {
		summary.Total++
		if f.IsActive() {
			summary.Active++
			summary.BySource[f.Source]++
			summary.ByCategory[f.Category]++

			switch f.Severity {
			case SeverityCritical:
				summary.Critical++
			case SeverityWarning:
				summary.Warning++
			case SeverityWatch:
				summary.Watch++
			case SeverityInfo:
				summary.Info++
			}

			if f.EnhancedByAI {
				summary.EnhancedByAI++
			}
		}
	}

	return summary
}

// UnifiedSummary provides statistics about unified findings
type UnifiedSummary struct {
	Total        int                     `json:"total"`
	Active       int                     `json:"active"`
	Critical     int                     `json:"critical"`
	Warning      int                     `json:"warning"`
	Watch        int                     `json:"watch"`
	Info         int                     `json:"info"`
	EnhancedByAI int                     `json:"enhanced_by_ai"`
	BySource     map[FindingSource]int   `json:"by_source"`
	ByCategory   map[UnifiedCategory]int `json:"by_category"`
}

// FormatForContext formats unified findings for AI prompt injection
func (s *UnifiedStore) FormatForContext() string {
	active := s.GetActive()
	if len(active) == 0 {
		return ""
	}

	var result strings.Builder
	result.WriteString("\n## Active Issues (Unified View)\n\n")

	// Group by source
	bySource := make(map[FindingSource][]*UnifiedFinding)
	for _, f := range active {
		bySource[f.Source] = append(bySource[f.Source], f)
	}

	// Threshold alerts first
	if findings := bySource[SourceThreshold]; len(findings) > 0 {
		result.WriteString("### Threshold Alerts\n")
		for _, f := range findings {
			enhanced := ""
			if f.EnhancedByAI {
				enhanced = " [AI-analyzed]"
			}
			result.WriteString(fmt.Sprintf("- [%s] %s: %s%s\n",
				f.Severity, f.ResourceName, f.Title, enhanced))
			if f.AIContext != "" {
				result.WriteString(fmt.Sprintf("  AI context: %s\n", f.AIContext))
			}
		}
		result.WriteString("\n")
	}

	// AI findings
	for source, findings := range bySource {
		if source == SourceThreshold || len(findings) == 0 {
			continue
		}
		result.WriteString(fmt.Sprintf("### %s Findings\n", formatSourceName(source)))
		for _, f := range findings {
			result.WriteString(fmt.Sprintf("- [%s] %s: %s\n",
				f.Severity, f.ResourceName, f.Title))
			if f.RootCauseID != "" {
				result.WriteString(fmt.Sprintf("  Root cause linked: %s\n", f.RootCauseID))
			}
		}
		result.WriteString("\n")
	}

	return result.String()
}

// scheduleSave schedules a debounced save operation
func (s *UnifiedStore) scheduleSave() {
	s.mu.Lock()
	if s.persistence == nil {
		s.mu.Unlock()
		return
	}

	if s.savePending {
		s.mu.Unlock()
		return
	}

	s.savePending = true
	s.saveTimer = time.AfterFunc(5*time.Second, func() {
		s.mu.Lock()
		s.savePending = false
		findingsCopy := make(map[string]*UnifiedFinding, len(s.findings))
		for id, f := range s.findings {
			copy := *f
			findingsCopy[id] = &copy
		}
		persistence := s.persistence
		s.mu.Unlock()

		if persistence != nil {
			if err := persistence.SaveFindings(findingsCopy); err != nil {
				log.Error().Err(err).Msg("failed to save unified findings")
			}
		}
	})
	s.mu.Unlock()
}

// ForceSave immediately saves findings
func (s *UnifiedStore) ForceSave() error {
	s.mu.Lock()
	if s.saveTimer != nil {
		s.saveTimer.Stop()
	}
	s.savePending = false

	findingsCopy := make(map[string]*UnifiedFinding, len(s.findings))
	for id, f := range s.findings {
		copy := *f
		findingsCopy[id] = &copy
	}
	persistence := s.persistence
	s.mu.Unlock()

	if persistence != nil {
		return persistence.SaveFindings(findingsCopy)
	}
	return nil
}

// Helper functions

func severityOrder(s UnifiedSeverity) int {
	switch s {
	case SeverityInfo:
		return 0
	case SeverityWatch:
		return 1
	case SeverityWarning:
		return 2
	case SeverityCritical:
		return 3
	default:
		return 0
	}
}

func determineResourceType(alertType string, metadata map[string]interface{}) string {
	// Check metadata for explicit type
	if metadata != nil {
		if t, ok := metadata["resourceType"].(string); ok && t != "" {
			return t
		}
	}

	// Infer from alert type
	switch alertType {
	case "nodeOffline", "temperature":
		return "node"
	case "usage", "storage":
		return "storage"
	case "backup", "backupMissing", "backupStale":
		return "backup"
	case "snapshot", "snapshotAge", "snapshotSize":
		return "snapshot"
	case "restartLoop", "oom", "imageUpdateAvail":
		return "app-container"
	default:
		return "agent"
	}
}

func generateTitle(alertType, resourceName string, value, threshold float64) string {
	switch alertType {
	case "cpu":
		return fmt.Sprintf("High CPU usage on %s (%.0f%%)", resourceName, value)
	case "memory":
		return fmt.Sprintf("High memory usage on %s (%.0f%%)", resourceName, value)
	case "disk":
		return fmt.Sprintf("High disk usage on %s (%.0f%%)", resourceName, value)
	case "usage", "storage":
		return fmt.Sprintf("High storage usage on %s (%.0f%%)", resourceName, value)
	case "temperature":
		return fmt.Sprintf("High temperature on %s (%.0f°C)", resourceName, value)
	case "offline", "nodeOffline":
		return fmt.Sprintf("%s is offline", resourceName)
	case "poweredOff":
		return fmt.Sprintf("%s is powered off", resourceName)
	default:
		return fmt.Sprintf("%s alert on %s", alertType, resourceName)
	}
}

// generateImpact returns the operator-facing consequence-if-ignored statement
// for a threshold alert, keyed on alert type. The text is hand-authored per
// alert kind so the operator sees the same operational consequence regardless
// of which AI runtime (or none) is active.
func generateImpact(alertType string) string {
	switch alertType {
	case "cpu":
		return "Workloads on this resource slow down or queue requests; sustained pressure can stall services and propagate latency to dependent workloads."
	case "memory", "docker-container-memory-limit":
		return "Memory pressure can trigger OOM kills or swap thrashing, terminating processes or stalling the host until pressure clears."
	case "disk":
		return "Writes will start failing when the disk fills, which can corrupt running workloads and stop logging, monitoring, and backup jobs."
	case "diskRead", "diskWrite":
		return "Sustained I/O pressure causes workloads to queue and stall, propagating latency to dependent services."
	case "networkIn", "networkOut":
		return "Network saturation causes packet loss and latency, slowing or timing out dependent connections."
	case "usage", "storage":
		return "Storage writes will start failing when capacity is exhausted, blocking new data, snapshots, and backup retention."
	case "temperature", "diskTemperature":
		return "Sustained heat throttles performance and shortens hardware life; in critical ranges the system can shut down or fail unexpectedly."
	case "offline", "nodeOffline", "host-offline", "docker-host-offline", "connectivity":
		return "Workloads on this resource are unreachable; backups, scheduled jobs, and dependent services on this host stop until it returns."
	case "poweredOff", "powered-off":
		return "Nothing on this resource is running, so any workload, backup, or dependent service it provides is unavailable."
	case "zfs-pool-state", "zfs-pool-errors":
		return "Pool errors indicate data integrity risk; uncorrected errors can lead to silent data corruption or total pool failure."
	case "zfs-device":
		return "A faulted or degraded device risks pool failure; if redundancy is already lost, data on this pool is at risk."
	case "storage-topology":
		return "Misconfigured storage topology can cause unexpected capacity exhaustion, unbalanced wear, or lost redundancy."
	case "disk-health", "disk-wearout":
		return "Failing or worn disks risk data loss; if the disk fails before replacement, data on it may be unrecoverable."
	case "raid":
		return "A degraded RAID array has reduced or no redundancy; a second disk failure before rebuild completes can cause permanent data loss."
	case "backup", "backupMissing", "backupStale", "backup-age":
		return "Without recent backups, data lost to failure or corruption cannot be recovered to a known-good state."
	case "snapshot", "snapshotAge", "snapshot-age":
		return "Old snapshots lose restore value as they drift further from current data; recovery points become stale."
	case "snapshotSize":
		return "Large snapshots consume pool capacity and can block new writes when space is exhausted."
	case "oom", "docker-container-oom-kill":
		return "Out-of-memory kills terminate processes without warning, causing data loss or service disruption."
	case "restartLoop", "docker-container-restart-loop":
		return "A restart loop means the workload cannot reach a stable state; dependent services cycle in and out of availability."
	case "imageUpdateAvail", "docker-container-update":
		return "Pending updates may include security patches; unpatched images accumulate known vulnerabilities over time."
	case "docker-container-health", "docker-service-health", "docker-container-state":
		return "An unhealthy container or service cannot serve its function; dependent workloads may fail or degrade."
	case "message-age":
		return "Stale monitoring data means current status is unknown; issues may have developed without detection."
	default:
		return ""
	}
}

func formatSourceName(source FindingSource) string {
	switch source {
	case SourceThreshold:
		return "Threshold Alert"
	case SourceAIPatrol:
		return "Pulse Patrol"
	case SourceAIChat:
		return "Pulse Assistant"
	case SourceAnomaly:
		return "Anomaly Detection"
	case SourceCorrelation:
		return "Root Cause Analysis"
	case SourceForecast:
		return "Proactive Forecast"
	default:
		return string(source)
	}
}