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.

29.4 KB
Raw Blame
package ai

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

	"github.com/rs/zerolog/log"
)

// SignalType identifies the kind of infrastructure problem signal detected.
type SignalType string

// Default signal detection thresholds for deterministic problem identification.
// These are used as fallbacks when no user-configured thresholds are provided.
const (
	signalStorageWarningPercent  = 75.0           // Pool usage above this triggers a warning signal
	signalStorageCriticalPercent = 95.0           // Pool usage above this triggers a critical signal
	signalHighCPUPercent         = 70.0           // Average CPU above this triggers a high-CPU signal
	signalHighMemoryPercent      = 80.0           // Average memory above this triggers a high-memory signal
	signalBackupStaleThreshold   = 48 * time.Hour // No backup in this window triggers a stale-backup signal
)

// SignalThresholds holds configurable thresholds for deterministic signal detection.
// When populated from user config (PatrolThresholds), signals align with user-defined
// alert thresholds instead of using hardcoded defaults.
type SignalThresholds struct {
	StorageWarningPercent  float64
	StorageCriticalPercent float64
	HighCPUPercent         float64
	HighMemoryPercent      float64
	BackupStaleThreshold   time.Duration
}

// DefaultSignalThresholds returns the hardcoded default thresholds.
func DefaultSignalThresholds() SignalThresholds {
	return SignalThresholds{
		StorageWarningPercent:  signalStorageWarningPercent,
		StorageCriticalPercent: signalStorageCriticalPercent,
		HighCPUPercent:         signalHighCPUPercent,
		HighMemoryPercent:      signalHighMemoryPercent,
		BackupStaleThreshold:   signalBackupStaleThreshold,
	}
}

// SignalThresholdsFromPatrol builds SignalThresholds from user-configured PatrolThresholds.
// Zero-value fields fall back to defaults.
func SignalThresholdsFromPatrol(pt PatrolThresholds) SignalThresholds {
	defaults := DefaultSignalThresholds()
	st := SignalThresholds{
		StorageWarningPercent:  pt.StorageWarning,
		StorageCriticalPercent: pt.StorageCritical,
		HighCPUPercent:         pt.NodeCPUWarning,
		HighMemoryPercent:      pt.NodeMemWarning,
		BackupStaleThreshold:   defaults.BackupStaleThreshold, // No user config for this
	}
	// Fall back to defaults for zero values
	if st.StorageWarningPercent == 0 {
		st.StorageWarningPercent = defaults.StorageWarningPercent
	}
	if st.StorageCriticalPercent == 0 {
		st.StorageCriticalPercent = defaults.StorageCriticalPercent
	}
	if st.HighCPUPercent == 0 {
		st.HighCPUPercent = defaults.HighCPUPercent
	}
	if st.HighMemoryPercent == 0 {
		st.HighMemoryPercent = defaults.HighMemoryPercent
	}
	return st
}

const (
	SignalSMARTFailure     SignalType = "smart_failure"
	SignalHighCPU          SignalType = "high_cpu"
	SignalHighMemory       SignalType = "high_memory"
	SignalHighDisk         SignalType = "high_disk"
	SignalBackupFailed     SignalType = "backup_failed"
	SignalBackupStale      SignalType = "backup_stale"
	SignalGuestUnreachable SignalType = "guest_unreachable"
)

// DetectedSignal represents a problem signal found in tool call results.
type DetectedSignal struct {
	SignalType        SignalType
	ResourceID        string
	ResourceName      string
	ResourceType      string
	SuggestedSeverity string // "critical", "warning", "watch"
	Category          string // matches FindingCategory values
	Summary           string // one-line description
	Evidence          string // truncated raw data (max 500 chars)
	ToolCallID        string
}

// DetectSignals scans completed tool calls for known problem signals.
// This is deterministic — it does not depend on LLM judgment.
// Thresholds control when signals fire; use DefaultSignalThresholds() for hardcoded defaults
// or SignalThresholdsFromPatrol() to derive from user-configured alert thresholds.
func DetectSignals(toolCalls []ToolCallRecord, thresholds SignalThresholds) []DetectedSignal {
	var signals []DetectedSignal

	for i := range toolCalls {
		tc := &toolCalls[i]
		if !tc.Success || tc.Output == "" {
			continue
		}

		detected := detectSignalsFromToolCall(tc, thresholds)
		signals = append(signals, detected...)
	}

	return deduplicateSignals(signals)
}

// detectSignalsFromToolCall dispatches to signal-specific detectors based on tool name and input.
func detectSignalsFromToolCall(tc *ToolCallRecord, thresholds SignalThresholds) []DetectedSignal {
	var signals []DetectedSignal

	if shouldSkipSignalParsing(tc.Output) {
		return nil
	}

	switch tc.ToolName {
	case "pulse_storage":
		signals = append(signals, detectStorageSignals(tc, thresholds)...)
	case "pulse_metrics":
		signals = append(signals, detectMetricsSignals(tc, thresholds)...)
		// pulse_alerts intentionally has no deterministic signal extraction.
		// Patrol's job is to find issues alerts cannot — duplicating the
		// alert list into Patrol findings was double-counting (alerts
		// already have their own lifecycle, surface, and acknowledgement
		// model) and produced regression cycles when the LLM resolved the
		// mirrored finding while the underlying alert kept firing. The
		// Alerts page is the canonical surface for currently-firing
		// alerts; Patrol should stay quiet here.
	}

	return signals
}

// --- Storage signals ---

type smartAttributeCounters struct {
	ReallocatedSectors   *int64 `json:"reallocatedSectors"`
	PendingSectors       *int64 `json:"pendingSectors"`
	OfflineUncorrectable *int64 `json:"offlineUncorrectable"`
	UDMACRCErrors        *int64 `json:"udmaCrcErrors"`
	MediaErrors          *int64 `json:"mediaErrors"`
}

type smartDiskSignalData struct {
	ID                   string                 `json:"id"`
	Device               string                 `json:"device"`
	DevPath              string                 `json:"dev_path"`
	Health               string                 `json:"health"`
	Model                string                 `json:"model"`
	Serial               string                 `json:"serial"`
	Node                 string                 `json:"node"`
	Attributes           smartAttributeCounters `json:"attributes"`
	SmartAttributes      smartAttributeCounters `json:"smart_attributes"`
	SmartAttributesCamel smartAttributeCounters `json:"smartAttributes"`
}

// storageDiskHealth is the minimal struct for parsing disk_health tool output.
type storageDiskHealth struct {
	Disks []smartDiskSignalData `json:"disks"`
	Hosts []struct {
		Hostname string                `json:"hostname"`
		SMART    []smartDiskSignalData `json:"smart"`
	} `json:"hosts"`
	Node string `json:"node"`
}

// storagePool is the minimal struct for parsing storage pool data.
type storagePool struct {
	Pools []struct {
		Name         string  `json:"name"`
		UsagePercent float64 `json:"usage_percent"`
		Node         string  `json:"node"`
		ID           string  `json:"id"`
	} `json:"pools"`
	Storage string `json:"storage"`
	Node    string `json:"node"`
}

// VMIDValue handles VMID fields that may be strings or numbers.
type VMIDValue string

func (v *VMIDValue) UnmarshalJSON(data []byte) error {
	if len(data) == 0 || string(data) == "null" {
		*v = ""
		return nil
	}

	var s string
	if err := json.Unmarshal(data, &s); err == nil {
		*v = VMIDValue(strings.TrimSpace(s))
		return nil
	}

	var n json.Number
	if err := json.Unmarshal(data, &n); err == nil {
		*v = VMIDValue(n.String())
		return nil
	}

	return fmt.Errorf("invalid VMID value: %s", string(data))
}

// backupTask is the minimal struct for parsing backup task data.
type backupTask struct {
	Tasks []struct {
		ID        string    `json:"id"`
		Status    string    `json:"status"`
		Error     string    `json:"error"`
		StartTime string    `json:"start_time"`
		EndTime   string    `json:"end_time"`
		VMID      VMIDValue `json:"vmid"`
		VMName    string    `json:"vm_name"`
		Node      string    `json:"node"`
		Instance  string    `json:"instance"`
		Type      string    `json:"type"`
	} `json:"tasks"`
	Node string `json:"node"`
}

// backupsResponse is the minimal struct for parsing pulse_storage type="backups" output.
type backupsResponse struct {
	PBS []struct {
		VMID       string `json:"vmid"`
		BackupTime string `json:"backup_time"`
		Instance   string `json:"instance"`
	} `json:"pbs"`
	PVE []struct {
		VMID       int    `json:"vmid"`
		BackupTime string `json:"backup_time"`
	} `json:"pve"`
	RecentTasks []struct {
		VMID      int    `json:"vmid"`
		Node      string `json:"node"`
		Status    string `json:"status"`
		StartTime string `json:"start_time"`
	} `json:"recent_tasks"`
}

func detectStorageSignals(tc *ToolCallRecord, thresholds SignalThresholds) []DetectedSignal {
	var signals []DetectedSignal

	inputType := extractInputType(tc.Input)

	switch inputType {
	case "disk_health":
		signals = append(signals, detectSMARTFailures(tc)...)
	case "pools":
		signals = append(signals, detectHighDiskUsage(tc, thresholds)...)
	case "backup_tasks":
		signals = append(signals, detectBackupIssues(tc, thresholds)...)
	case "backups":
		signals = append(signals, detectBackupIssuesFromBackups(tc, thresholds)...)
	case "":
		// Input type missing — attempt to infer from output structure.
		signals = append(signals, detectSMARTFailures(tc)...)
		signals = append(signals, detectHighDiskUsage(tc, thresholds)...)
		signals = append(signals, detectBackupIssues(tc, thresholds)...)
		signals = append(signals, detectBackupIssuesFromBackups(tc, thresholds)...)
	}

	return signals
}

func detectSMARTFailures(tc *ToolCallRecord) []DetectedSignal {
	var signals []DetectedSignal

	var data storageDiskHealth
	if err := json.Unmarshal([]byte(tc.Output), &data); err != nil {
		if !tryParseEmbeddedJSON(tc.Output, &data) {
			log.Debug().Err(err).Str("tool", tc.ToolName).Msg("patrol_signals: failed to parse disk_health output")
			return nil
		}
	}

	node := data.Node
	for _, disk := range data.Disks {
		if signal, ok := smartDiskSignal(tc, disk, node); ok {
			signals = append(signals, signal)
		}
	}
	for _, host := range data.Hosts {
		for _, disk := range host.SMART {
			if signal, ok := smartDiskSignal(tc, disk, host.Hostname); ok {
				signals = append(signals, signal)
			}
		}
	}

	return signals
}

func smartDiskSignal(tc *ToolCallRecord, disk smartDiskSignalData, fallbackNode string) (DetectedSignal, bool) {
	health := strings.ToUpper(strings.TrimSpace(disk.Health))
	healthIssue := health != "" && health != "UNKNOWN" && health != "PASSED" && health != "OK"

	attrParts, hasCriticalAttrs := smartAttributeIssueParts(disk.mergedAttributes())
	if !healthIssue && len(attrParts) == 0 {
		return DetectedSignal{}, false
	}

	resourceID, resourceName, resourceType := smartDiskResource(disk, fallbackNode)
	severity := "warning"
	if healthIssue || hasCriticalAttrs {
		severity = "critical"
	}

	var summary string
	switch {
	case healthIssue && len(attrParts) > 0:
		summary = fmt.Sprintf("SMART health check: %s for %s; counters: %s", health, resourceName, strings.Join(attrParts, ", "))
	case healthIssue:
		summary = fmt.Sprintf("SMART health check: %s for %s", health, resourceName)
	default:
		summary = fmt.Sprintf("SMART counters indicate disk risk for %s: %s", resourceName, strings.Join(attrParts, ", "))
	}

	return DetectedSignal{
		SignalType:        SignalSMARTFailure,
		ResourceID:        resourceID,
		ResourceName:      resourceName,
		ResourceType:      resourceType,
		SuggestedSeverity: severity,
		Category:          string(FindingCategoryReliability),
		Summary:           summary,
		Evidence:          truncateEvidence(tc.Output),
		ToolCallID:        tc.ID,
	}, true
}

func (d smartDiskSignalData) mergedAttributes() smartAttributeCounters {
	merged := smartAttributeCounters{}
	merge := func(attrs smartAttributeCounters) {
		if merged.ReallocatedSectors == nil {
			merged.ReallocatedSectors = attrs.ReallocatedSectors
		}
		if merged.PendingSectors == nil {
			merged.PendingSectors = attrs.PendingSectors
		}
		if merged.OfflineUncorrectable == nil {
			merged.OfflineUncorrectable = attrs.OfflineUncorrectable
		}
		if merged.UDMACRCErrors == nil {
			merged.UDMACRCErrors = attrs.UDMACRCErrors
		}
		if merged.MediaErrors == nil {
			merged.MediaErrors = attrs.MediaErrors
		}
	}
	merge(d.Attributes)
	merge(d.SmartAttributes)
	merge(d.SmartAttributesCamel)
	return merged
}

func smartAttributeIssueParts(attrs smartAttributeCounters) ([]string, bool) {
	var parts []string
	hasCritical := false
	appendCounter := func(label string, value *int64, critical bool) {
		if value == nil || *value <= 0 {
			return
		}
		parts = append(parts, fmt.Sprintf("%s=%d", label, *value))
		if critical {
			hasCritical = true
		}
	}

	appendCounter("reallocated sectors", attrs.ReallocatedSectors, false)
	appendCounter("pending sectors", attrs.PendingSectors, true)
	appendCounter("offline uncorrectable", attrs.OfflineUncorrectable, true)
	appendCounter("UDMA CRC errors", attrs.UDMACRCErrors, false)
	appendCounter("media errors", attrs.MediaErrors, true)
	return parts, hasCritical
}

func smartDiskResource(disk smartDiskSignalData, fallbackNode string) (resourceID, resourceName, resourceType string) {
	resourceName = strings.TrimSpace(disk.Device)
	if resourceName == "" {
		resourceName = strings.TrimSpace(disk.DevPath)
	}
	if resourceName == "" {
		resourceName = strings.TrimSpace(disk.Model)
	}
	if resourceName == "" {
		resourceName = strings.TrimSpace(disk.ID)
	}
	if resourceName == "" {
		resourceName = "disk"
	}

	resourceID = strings.TrimSpace(disk.ID)
	resourceType = "disk"
	if resourceID == "" {
		node := strings.TrimSpace(disk.Node)
		if node == "" {
			node = strings.TrimSpace(fallbackNode)
		}
		resourceID = node
		resourceType = "node"
	}
	if resourceID == "" {
		resourceID = resourceName
	}
	return resourceID, resourceName, resourceType
}

func detectHighDiskUsage(tc *ToolCallRecord, thresholds SignalThresholds) []DetectedSignal {
	var signals []DetectedSignal

	var data storagePool
	if err := json.Unmarshal([]byte(tc.Output), &data); err != nil {
		if !tryParseEmbeddedJSON(tc.Output, &data) {
			log.Debug().Err(err).Str("tool", tc.ToolName).Msg("patrol_signals: failed to parse pools output")
			return nil
		}
	}

	for _, pool := range data.Pools {
		if pool.UsagePercent <= thresholds.StorageWarningPercent {
			continue
		}

		severity := "warning"
		if pool.UsagePercent > thresholds.StorageCriticalPercent {
			severity = "critical"
		}

		resourceID := pool.ID
		if resourceID == "" {
			resourceID = pool.Name
		}

		signals = append(signals, DetectedSignal{
			SignalType:        SignalHighDisk,
			ResourceID:        resourceID,
			ResourceName:      pool.Name,
			ResourceType:      "storage",
			SuggestedSeverity: severity,
			Category:          string(FindingCategoryCapacity),
			Summary:           fmt.Sprintf("Storage pool %s at %.1f%% usage", pool.Name, pool.UsagePercent),
			Evidence:          truncateEvidence(tc.Output),
			ToolCallID:        tc.ID,
		})
	}

	return signals
}

func detectBackupIssues(tc *ToolCallRecord, thresholds SignalThresholds) []DetectedSignal {
	var signals []DetectedSignal

	var data backupTask
	if err := json.Unmarshal([]byte(tc.Output), &data); err != nil {
		if !tryParseEmbeddedJSON(tc.Output, &data) {
			log.Debug().Err(err).Str("tool", tc.ToolName).Msg("patrol_signals: failed to parse backup_tasks output")
			return nil
		}
	}

	now := time.Now()
	type latestBackup struct {
		time          time.Time
		isFailure     bool
		statusSummary string
		resourceID    string
		resourceName  string
	}
	latestByResource := make(map[string]latestBackup)

	for _, task := range data.Tasks {
		statusLower := strings.ToLower(strings.TrimSpace(task.Status))
		errorLower := strings.ToLower(strings.TrimSpace(task.Error))
		vmID := strings.TrimSpace(string(task.VMID))
		if vmID == "0" {
			vmID = ""
		}
		var taskEnd time.Time
		if task.EndTime != "" {
			if parsed, err := tryParseTime(task.EndTime); err == nil {
				taskEnd = parsed
			}
		}
		if taskEnd.IsZero() && task.StartTime != "" {
			if parsed, err := tryParseTime(task.StartTime); err == nil {
				taskEnd = parsed
			}
		}
		if taskEnd.IsZero() {
			taskEnd = now
		}
		if now.Sub(taskEnd) > thresholds.BackupStaleThreshold {
			continue
		}

		// Check for failed backups. Treat any non-OK terminal status as failure.
		isSuccess := statusLower == "ok" || strings.Contains(statusLower, "success")
		isFailure := statusLower != "" && !isSuccess
		if strings.Contains(statusLower, "error") || strings.Contains(statusLower, "fail") {
			isFailure = true
		}
		if strings.Contains(statusLower, "running") || strings.Contains(statusLower, "active") {
			isFailure = false
		}
		if errorLower != "" && errorLower != "null" && errorLower != "0" {
			isFailure = true
		}

		node := task.Node
		if node == "" {
			node = data.Node
		}
		resourceID := node
		if vmID != "" {
			resourceID = vmID
		}
		resourceName := vmID
		if task.VMName != "" {
			resourceName = task.VMName
		}
		if resourceName == "" {
			resourceName = resourceID
		}
		statusSummary := task.Status
		if strings.TrimSpace(statusSummary) == "" {
			statusSummary = task.Error
		}

		if prev, ok := latestByResource[resourceID]; !ok || taskEnd.After(prev.time) {
			latestByResource[resourceID] = latestBackup{
				time:          taskEnd,
				isFailure:     isFailure,
				statusSummary: statusSummary,
				resourceID:    resourceID,
				resourceName:  resourceName,
			}
		}
	}

	for _, entry := range latestByResource {
		if !entry.isFailure {
			continue
		}
		signals = append(signals, DetectedSignal{
			SignalType:        SignalBackupFailed,
			ResourceID:        entry.resourceID,
			ResourceName:      entry.resourceName,
			ResourceType:      "backup",
			SuggestedSeverity: "warning",
			Category:          string(FindingCategoryBackup),
			Summary:           "Backup task failed: " + entry.statusSummary,
			Evidence:          truncateEvidence(tc.Output),
			ToolCallID:        tc.ID,
		})
	}

	return signals
}

func detectBackupIssuesFromBackups(tc *ToolCallRecord, thresholds SignalThresholds) []DetectedSignal {
	var signals []DetectedSignal

	var data backupsResponse
	if err := json.Unmarshal([]byte(tc.Output), &data); err != nil {
		if !tryParseEmbeddedJSON(tc.Output, &data) {
			log.Debug().Err(err).Str("tool", tc.ToolName).Msg("patrol_signals: failed to parse backups output")
			return nil
		}
	}

	now := time.Now()
	knownVMIDs := make(map[string]bool)
	for _, task := range data.RecentTasks {
		if task.VMID == 0 {
			continue
		}
		knownVMIDs[fmt.Sprintf("%d", task.VMID)] = true
	}

	// Recent task failures (from backups response) - only the latest per resource
	type latestBackup struct {
		time          time.Time
		isFailure     bool
		statusSummary string
		resourceID    string
	}
	latestByResource := make(map[string]latestBackup)
	for _, task := range data.RecentTasks {
		statusLower := strings.ToLower(strings.TrimSpace(task.Status))
		if statusLower == "" {
			continue
		}
		isSuccess := statusLower == "ok" || strings.Contains(statusLower, "success")
		isFailure := statusLower != "" && !isSuccess
		if strings.Contains(statusLower, "error") || strings.Contains(statusLower, "fail") {
			isFailure = true
		}
		if strings.Contains(statusLower, "running") || strings.Contains(statusLower, "active") {
			isFailure = false
		}

		var taskTime time.Time
		if task.StartTime != "" {
			if parsed, err := tryParseTime(task.StartTime); err == nil {
				taskTime = parsed
			}
		}
		if taskTime.IsZero() {
			taskTime = now
		}
		if now.Sub(taskTime) > thresholds.BackupStaleThreshold {
			continue
		}

		vmID := strings.TrimSpace(fmt.Sprintf("%d", task.VMID))
		if vmID == "0" {
			vmID = ""
		}
		resourceID := vmID
		if resourceID == "" {
			resourceID = task.Node
		}
		if resourceID == "" {
			resourceID = "backup"
		}

		if prev, ok := latestByResource[resourceID]; !ok || taskTime.After(prev.time) {
			latestByResource[resourceID] = latestBackup{
				time:          taskTime,
				isFailure:     isFailure,
				statusSummary: task.Status,
				resourceID:    resourceID,
			}
		}
	}

	for _, entry := range latestByResource {
		if !entry.isFailure {
			continue
		}
		signals = append(signals, DetectedSignal{
			SignalType:        SignalBackupFailed,
			ResourceID:        entry.resourceID,
			ResourceName:      entry.resourceID,
			ResourceType:      "backup",
			SuggestedSeverity: "warning",
			Category:          string(FindingCategoryBackup),
			Summary:           "Backup task failed: " + entry.statusSummary,
			Evidence:          truncateEvidence(tc.Output),
			ToolCallID:        tc.ID,
		})
	}

	// Stale backup detection from backup summaries
	if len(knownVMIDs) == 0 {
		return signals
	}
	latestByVM := make(map[string]time.Time)
	for _, backup := range data.PBS {
		vmID := strings.TrimSpace(backup.VMID)
		if vmID == "" || vmID == "0" {
			continue
		}
		if !knownVMIDs[vmID] {
			continue
		}
		if t, err := tryParseTime(backup.BackupTime); err == nil {
			if prev, ok := latestByVM[vmID]; !ok || t.After(prev) {
				latestByVM[vmID] = t
			}
		}
	}
	for _, backup := range data.PVE {
		if backup.VMID == 0 {
			continue
		}
		vmID := fmt.Sprintf("%d", backup.VMID)
		if !knownVMIDs[vmID] {
			continue
		}
		if t, err := tryParseTime(backup.BackupTime); err == nil {
			if prev, ok := latestByVM[vmID]; !ok || t.After(prev) {
				latestByVM[vmID] = t
			}
		}
	}

	for vmID, newestEnd := range latestByVM {
		if now.Sub(newestEnd) > thresholds.BackupStaleThreshold {
			signals = append(signals, DetectedSignal{
				SignalType:        SignalBackupStale,
				ResourceID:        vmID,
				ResourceName:      vmID,
				ResourceType:      "backup",
				SuggestedSeverity: "warning",
				Category:          string(FindingCategoryBackup),
				Summary:           "No backup completed in 48+ hours for VM/CT " + vmID,
				Evidence:          truncateEvidence(tc.Output),
				ToolCallID:        tc.ID,
			})
		}
	}

	return signals
}

// --- Metrics signals ---

// metricsPerformance is the minimal struct for parsing performance metrics.
type metricsPerformance struct {
	Resources []struct {
		ID         string  `json:"id"`
		Name       string  `json:"name"`
		Type       string  `json:"type"`
		Node       string  `json:"node"`
		AvgCPU     float64 `json:"avg_cpu"`
		AvgMemory  float64 `json:"avg_memory"`
		MaxCPU     float64 `json:"max_cpu"`
		MaxMemory  float64 `json:"max_memory"`
		CPUPercent float64 `json:"cpu_percent"`
		MemPercent float64 `json:"mem_percent"`
	} `json:"resources"`
	// Also handle flat response (single resource)
	ID         string  `json:"id"`
	Name       string  `json:"name"`
	Type       string  `json:"type"`
	Node       string  `json:"node"`
	AvgCPU     float64 `json:"avg_cpu"`
	AvgMemory  float64 `json:"avg_memory"`
	MaxCPU     float64 `json:"max_cpu"`
	MaxMemory  float64 `json:"max_memory"`
	CPUPercent float64 `json:"cpu_percent"`
	MemPercent float64 `json:"mem_percent"`
}

type metricsPhysicalDisks struct {
	Disks []smartDiskSignalData `json:"disks"`
}

func detectMetricsSignals(tc *ToolCallRecord, thresholds SignalThresholds) []DetectedSignal {
	inputType := extractInputType(tc.Input)
	if inputType == "disks" {
		return detectPhysicalDiskMetricSignals(tc)
	}
	if inputType != "performance" && inputType != "" {
		return nil
	}

	var signals []DetectedSignal
	var data metricsPerformance
	if err := json.Unmarshal([]byte(tc.Output), &data); err != nil {
		if !tryParseEmbeddedJSON(tc.Output, &data) {
			log.Debug().Err(err).Str("tool", tc.ToolName).Msg("patrol_signals: failed to parse performance output")
			return nil
		}
	}

	// Collect resources to check (handle both list and single)
	type resource struct {
		id, name, rtype, node string
		avgCPU, avgMemory     float64
	}
	var resources []resource

	if len(data.Resources) > 0 {
		for _, r := range data.Resources {
			resources = append(resources, resource{
				id: r.ID, name: r.Name, rtype: r.Type, node: r.Node,
				avgCPU:    selectNonZero(r.AvgCPU, r.CPUPercent),
				avgMemory: selectNonZero(r.AvgMemory, r.MemPercent),
			})
		}
	} else if data.ID != "" || data.Name != "" {
		resources = append(resources, resource{
			id: data.ID, name: data.Name, rtype: data.Type, node: data.Node,
			avgCPU:    selectNonZero(data.AvgCPU, data.CPUPercent),
			avgMemory: selectNonZero(data.AvgMemory, data.MemPercent),
		})
	}

	for _, r := range resources {
		if r.avgCPU > thresholds.HighCPUPercent {
			signals = append(signals, DetectedSignal{
				SignalType:        SignalHighCPU,
				ResourceID:        r.id,
				ResourceName:      r.name,
				ResourceType:      r.rtype,
				SuggestedSeverity: "warning",
				Category:          string(FindingCategoryPerformance),
				Summary:           fmt.Sprintf("High CPU usage on %s: %.1f%%", r.name, r.avgCPU),
				Evidence:          truncateEvidence(tc.Output),
				ToolCallID:        tc.ID,
			})
		}
		if r.avgMemory > thresholds.HighMemoryPercent {
			signals = append(signals, DetectedSignal{
				SignalType:        SignalHighMemory,
				ResourceID:        r.id,
				ResourceName:      r.name,
				ResourceType:      r.rtype,
				SuggestedSeverity: "warning",
				Category:          string(FindingCategoryPerformance),
				Summary:           fmt.Sprintf("High memory usage on %s: %.1f%%", r.name, r.avgMemory),
				Evidence:          truncateEvidence(tc.Output),
				ToolCallID:        tc.ID,
			})
		}
	}

	if inputType == "" {
		signals = append(signals, detectPhysicalDiskMetricSignals(tc)...)
	}

	return signals
}

func detectPhysicalDiskMetricSignals(tc *ToolCallRecord) []DetectedSignal {
	var data metricsPhysicalDisks
	if err := json.Unmarshal([]byte(tc.Output), &data); err != nil {
		if !tryParseEmbeddedJSON(tc.Output, &data) {
			log.Debug().Err(err).Str("tool", tc.ToolName).Msg("patrol_signals: failed to parse physical disks output")
			return nil
		}
	}

	var signals []DetectedSignal
	for _, disk := range data.Disks {
		if signal, ok := smartDiskSignal(tc, disk, disk.Node); ok {
			signals = append(signals, signal)
		}
	}
	return signals
}

// --- Deduplication and matching ---

// deduplicateSignals removes duplicate signals with the same SignalType:ResourceID.
func deduplicateSignals(signals []DetectedSignal) []DetectedSignal {
	if len(signals) == 0 {
		return signals
	}

	seen := make(map[string]bool)
	var result []DetectedSignal

	for _, s := range signals {
		key := string(s.SignalType) + ":" + s.ResourceID
		if seen[key] {
			continue
		}
		seen[key] = true
		result = append(result, s)
	}

	return result
}

// DetectReachabilitySignals returns signals for running guests that are unreachable.
// This is separate from tool-call-based detection since reachability comes from
// pre-patrol probing, not from LLM tool calls.
func DetectReachabilitySignals(guestIntel map[string]*GuestIntelligence) []DetectedSignal {
	var signals []DetectedSignal
	for guestID, gi := range guestIntel {
		if gi == nil || gi.Reachable == nil || *gi.Reachable {
			continue
		}
		name := gi.Name
		if name == "" {
			name = guestID
		}
		summary := fmt.Sprintf("Guest %s is running but not responding to ping", name)
		signals = append(signals, DetectedSignal{
			SignalType:        SignalGuestUnreachable,
			ResourceID:        guestID,
			ResourceName:      name,
			ResourceType:      gi.GuestType,
			SuggestedSeverity: "warning",
			Category:          "connectivity",
			Summary:           summary,
			Evidence:          "ICMP ping from host node returned no response",
		})
	}
	return signals
}

// UnmatchedSignals returns signals that don't have a corresponding finding already reported.
// A signal is considered matched if any finding shares the same ResourceID AND Category.
func UnmatchedSignals(signals []DetectedSignal, findings []*Finding) []DetectedSignal {
	if len(signals) == 0 {
		return nil
	}

	// Build a set of resourceID:category from existing findings
	matched := make(map[string]bool)
	for _, f := range findings {
		key := f.ResourceID + ":" + string(f.Category)
		matched[key] = true
	}

	var unmatched []DetectedSignal
	for _, s := range signals {
		key := s.ResourceID + ":" + s.Category
		if !matched[key] {
			unmatched = append(unmatched, s)
		}
	}

	return unmatched
}

// --- Helpers ---

// extractInputType extracts the "type" field from a tool call's input JSON string.
func extractInputType(input string) string {
	return extractInputField(input, "type")
}

// extractInputField extracts a named field from a tool call's input JSON string.
func extractInputField(input string, field string) string {
	if input == "" {
		return ""
	}
	var m map[string]interface{}
	if err := json.Unmarshal([]byte(input), &m); err != nil {
		return ""
	}
	if v, ok := m[field].(string); ok {
		return v
	}
	return ""
}

func shouldSkipSignalParsing(output string) bool {
	trimmed := strings.TrimSpace(output)
	if trimmed == "" {
		return true
	}
	lower := strings.ToLower(trimmed)
	if strings.HasPrefix(lower, "no ") {
		return true
	}
	if strings.HasPrefix(lower, "state provider not available") {
		return true
	}
	return false
}

// tryParseEmbeddedJSON attempts to find and parse a JSON object embedded in a larger string.
func tryParseEmbeddedJSON(s string, v interface{}) bool {
	start := strings.Index(s, "{")
	if start == -1 {
		return false
	}
	// Find the matching closing brace by trying progressively from the end
	for end := len(s); end > start; end-- {
		if s[end-1] == '}' {
			if json.Unmarshal([]byte(s[start:end]), v) == nil {
				return true
			}
		}
	}
	return false
}

// truncateEvidence truncates evidence to a maximum of 500 characters.
func truncateEvidence(s string) string {
	if len(s) <= 500 {
		return s
	}
	return s[:500]
}

// selectNonZero returns the first non-zero value, preferring the first argument.
func selectNonZero(a, b float64) float64 {
	if a > 0 {
		return a
	}
	return b
}

// tryParseTime tries common time formats.
func tryParseTime(s string) (time.Time, error) {
	formats := []string{
		time.RFC3339,
		"2006-01-02T15:04:05",
		"2006-01-02 15:04:05",
		time.RFC3339Nano,
	}
	for _, f := range formats {
		if t, err := time.Parse(f, s); err == nil {
			return t, nil
		}
	}
	return time.Time{}, fmt.Errorf("unable to parse time: %s", s)
}