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.

refactor(ai): canonicalize truenas host resource types

rcourtmane58d7df39870
diff --git a/docs/release-control/v6/internal/subsystems/ai-runtime.md b/docs/release-control/v6/internal/subsystems/ai-runtime.md
index 6c7a9512b1..7e18dc0b52 100644
--- a/docs/release-control/v6/internal/subsystems/ai-runtime.md
+++ b/docs/release-control/v6/internal/subsystems/ai-runtime.md
@@ -297,6 +297,11 @@ Compatibility-only top-level TrueNAS mention types must also collapse to the
canonical `agent` host type at that same handler boundary, so the AI runtime
does not carry a parallel raw `truenas` mention contract once transport input
has been normalized.
+That same compatibility-collapse rule also applies to alert, finding, and
+Patrol scope payloads. API-backed TrueNAS systems may still keep `truenas`
+platform metadata and separate run-history coverage counts, but AI resource
+type fields must normalize to canonical `agent` once they cross the governed
+runtime boundary.
The same governed-context rule also applies to the main unified AI resource
overview: infrastructure, workload, alert-label, and top-consumer summaries
must not leak raw resource names, cluster labels, IP addresses, or unresolved
diff --git a/docs/release-control/v6/internal/subsystems/patrol-intelligence.md b/docs/release-control/v6/internal/subsystems/patrol-intelligence.md
index fb9b55b845..827fb1a537 100644
--- a/docs/release-control/v6/internal/subsystems/patrol-intelligence.md
+++ b/docs/release-control/v6/internal/subsystems/patrol-intelligence.md
@@ -222,6 +222,10 @@ counts canonical too. When the backend distinguishes API-backed TrueNAS
systems from unified-agent hosts in Patrol run history, the run-history chips
and selected-run breakdown must render `TrueNAS` separately instead of
collapsing those systems back into the generic `agent` count.
+That run-history distinction must not reopen a parallel raw `truenas`
+resource-type contract in Patrol findings, scoped-run filters, or alert-backed
+resource state. Those payloads stay canonical `agent` plus platform context,
+while the separate `TrueNAS` count remains a run-history coverage detail.
That summary-card/metrics-strip split also applies to findings counts. The
primary assessment card may keep health as supporting context, but active
findings, warning counts, and critical counts belong to the supporting metric
diff --git a/internal/ai/alert_adapter.go b/internal/ai/alert_adapter.go
index b6d170b97a..a8a5cd4d35 100644
--- a/internal/ai/alert_adapter.go
+++ b/internal/ai/alert_adapter.go
@@ -2,7 +2,6 @@ package ai
import (
"fmt"
- "strings"
"time"
"github.com/rcourtman/pulse-go-rewrite/internal/alerts"
@@ -178,7 +177,7 @@ func inferResourceType(alertType string, metadata map[string]interface{}) string
}
func normalizeAlertResourceType(raw string) string {
- resourceType := strings.ToLower(strings.TrimSpace(raw))
+ resourceType := canonicalizeAICompatibilityResourceType(raw)
if resourceType == "" {
return ""
}
@@ -200,6 +199,8 @@ func normalizeAlertResourceType(raw string) string {
return "node"
case "k8s-cluster":
return "k8s-cluster"
+ case "physical_disk":
+ return "physical_disk"
default:
return resourceType
}
diff --git a/internal/ai/alert_adapter_test.go b/internal/ai/alert_adapter_test.go
index ca1a0bb8ca..ce4f540bad 100644
--- a/internal/ai/alert_adapter_test.go
+++ b/internal/ai/alert_adapter_test.go
@@ -177,6 +177,7 @@ func TestNormalizeAlertResourceType(t *testing.T) {
{name: "legacy docker_container alias rejected", input: "docker_container", expected: ""},
{name: "legacy docker_service alias rejected", input: "docker_service", expected: ""},
{name: "legacy kubernetes_cluster alias rejected", input: "kubernetes_cluster", expected: ""},
+ {name: "truenas canonicalized to agent", input: "truenas", expected: "agent"},
{name: "trim and case normalize rejected", input: " DOCKER-SERVICE ", expected: ""},
{name: "unknown passthrough", input: "storage", expected: "storage"},
}
diff --git a/internal/ai/findings.go b/internal/ai/findings.go
index ceafd53ec1..cd9285e9cf 100644
--- a/internal/ai/findings.go
+++ b/internal/ai/findings.go
@@ -438,7 +438,7 @@ func inferFindingResourceType(resourceID, resourceName string) string {
}
func canonicalFindingResourceType(resourceType string) string {
- normalized := strings.ToLower(strings.TrimSpace(resourceType))
+ normalized := canonicalizeAICompatibilityResourceType(resourceType)
if normalized == "" {
return ""
}
@@ -460,6 +460,8 @@ func canonicalFindingResourceType(resourceType string) string {
return "agent"
case "node":
return "node"
+ case "physical_disk":
+ return "physical_disk"
default:
return normalized
}
diff --git a/internal/ai/findings_resource_type_test.go b/internal/ai/findings_resource_type_test.go
index 013e4df93f..5e7d9013cc 100644
--- a/internal/ai/findings_resource_type_test.go
+++ b/internal/ai/findings_resource_type_test.go
@@ -113,6 +113,7 @@ func TestCanonicalFindingResourceType(t *testing.T) {
{in: "docker_host", want: ""},
{in: "kubernetes_cluster", want: ""},
{in: "host", want: ""},
+ {in: "truenas", want: "agent"},
{in: "storage", want: "storage"},
}
diff --git a/internal/ai/patrol_resource_state_test.go b/internal/ai/patrol_resource_state_test.go
index cd47bf4371..0ce9763c50 100644
--- a/internal/ai/patrol_resource_state_test.go
+++ b/internal/ai/patrol_resource_state_test.go
@@ -37,7 +37,7 @@ func TestGetResourceCurrentState(t *testing.T) {
{alert: AlertInfo{ResourceType: "usage", Type: "usage", ResourceID: "storage-1", ResourceName: "local"}, assert: "Storage 'local'"},
{alert: AlertInfo{ResourceType: "node", ResourceID: "node-1", ResourceName: "node-1"}, assert: "Node 'node-1'"},
{alert: AlertInfo{ResourceType: "agent", ResourceID: "host-1", ResourceName: "host-1"}, assert: "Agent host 'host-1'"},
- {alert: AlertInfo{ResourceType: "truenas", ResourceID: "truenas-1", ResourceName: "TrueNAS Main"}, assert: "TrueNAS 'truenas-1'"},
+ {alert: AlertInfo{ResourceType: "truenas", ResourceID: "truenas-1", ResourceName: "TrueNAS Main"}, assert: "TrueNAS system 'truenas-1'"},
{alert: AlertInfo{ResourceType: "vm", ResourceID: "vm-1", ResourceName: "vm-1"}, assert: "VM 'vm-1'"},
{alert: AlertInfo{ResourceType: "system-container", ResourceID: "ct-1", ResourceName: "ct-1"}, assert: "Container 'ct-1'"},
{alert: AlertInfo{ResourceType: "app-container", ResourceID: "dc-1", ResourceName: "web"}, assert: "Docker container 'web'"},
diff --git a/internal/ai/patrol_run.go b/internal/ai/patrol_run.go
index b4205ec496..0b4d6e4c4a 100644
--- a/internal/ai/patrol_run.go
+++ b/internal/ai/patrol_run.go
@@ -2038,6 +2038,7 @@ func (p *PatrolService) shouldResolveAlertState(ctx context.Context, alert Alert
// getCurrentMetricValue gets the current value of the metric that triggered the alert
type patrolAlertResourceState struct {
resourceType string
+ platform string
name string
status string
cpu float64
@@ -2059,7 +2060,7 @@ func patrolAlertNameMatches(alert AlertInfo, ids ...string) bool {
}
func patrolAlertLookupType(alert AlertInfo) string {
- resourceType := strings.TrimSpace(alert.ResourceType)
+ resourceType := canonicalizeAICompatibilityResourceType(alert.ResourceType)
if alert.Type == "usage" && (resourceType == "" || strings.EqualFold(resourceType, "usage")) {
return "storage"
}
@@ -2094,10 +2095,6 @@ func lookupPatrolAlertResourceState(alert AlertInfo, snap patrolRuntimeState) pa
if resource, ok := patrolLookupHostAlertResourceState(alert, snap); ok {
return resource
}
- case "truenas":
- if resource, ok := patrolLookupHostAlertResourceState(alert, snap); ok {
- return resource
- }
}
return patrolAlertResourceState{}
}
@@ -2170,8 +2167,8 @@ func patrolLookupGuestAlertResourceState(alert AlertInfo, snap patrolRuntimeStat
}
type patrolHostAlertRow struct {
- id, name, hostname, status, platform, resourceType string
- cpu, memory float64
+ id, name, hostname, status, platform string
+ cpu, memory float64
}
func patrolHostAlertRows(snap patrolRuntimeState) []patrolHostAlertRow {
@@ -2180,14 +2177,13 @@ func patrolHostAlertRows(snap patrolRuntimeState) []patrolHostAlertRow {
rows := make([]patrolHostAlertRow, 0, len(hosts))
for _, host := range hosts {
rows = append(rows, patrolHostAlertRow{
- id: host.ID(),
- name: host.Name(),
- hostname: host.Hostname(),
- status: string(host.Status()),
- platform: host.Platform(),
- resourceType: patrolHostResourceType(host.Platform()),
- cpu: host.CPUPercent(),
- memory: host.MemoryPercent(),
+ id: host.ID(),
+ name: host.Name(),
+ hostname: host.Hostname(),
+ status: string(host.Status()),
+ platform: host.Platform(),
+ cpu: host.CPUPercent(),
+ memory: host.MemoryPercent(),
})
}
return rows
@@ -2196,23 +2192,19 @@ func patrolHostAlertRows(snap patrolRuntimeState) []patrolHostAlertRow {
rows := make([]patrolHostAlertRow, 0, len(snap.Hosts))
for _, host := range snap.Hosts {
rows = append(rows, patrolHostAlertRow{
- id: host.ID,
- name: host.DisplayName,
- hostname: host.Hostname,
- status: host.Status,
- platform: host.Platform,
- resourceType: patrolHostResourceType(host.Platform),
- cpu: host.CPUUsage,
- memory: host.Memory.Usage,
+ id: host.ID,
+ name: host.DisplayName,
+ hostname: host.Hostname,
+ status: host.Status,
+ platform: host.Platform,
+ cpu: host.CPUUsage,
+ memory: host.Memory.Usage,
})
}
return rows
}
func patrolHostResourceType(platform string) string {
- if strings.EqualFold(strings.TrimSpace(platform), "truenas") {
- return "truenas"
- }
return "agent"
}
@@ -2226,7 +2218,8 @@ func patrolLookupHostAlertResourceState(alert AlertInfo, snap patrolRuntimeState
name = host.name
}
return patrolAlertResourceState{
- resourceType: host.resourceType,
+ resourceType: patrolHostResourceType(host.platform),
+ platform: host.platform,
name: name,
status: host.status,
cpu: host.cpu,
@@ -2262,7 +2255,7 @@ func (p *PatrolService) isResourceOnlineState(alert AlertInfo, snap patrolRuntim
return false
}
switch resource.resourceType {
- case "node", "agent", "truenas":
+ case "node", "agent":
return resource.status == "online"
case "vm", "system-container", "app-container":
return resource.status == "running" || resource.status == string(unifiedresources.StatusOnline)
@@ -2325,8 +2318,6 @@ func (p *PatrolService) getResourceCurrentStateState(alert AlertInfo, snap patro
return "Storage not found in current state (may have been removed)"
case "node":
return "Node not found in current state"
- case "truenas":
- return "TrueNAS system not found in current state"
case "agent":
return "Agent host not found in current state"
case "vm":
@@ -2345,10 +2336,11 @@ func (p *PatrolService) getResourceCurrentStateState(alert AlertInfo, snap patro
case "node":
return fmt.Sprintf("Node '%s': CPU %.1f%%, Memory %.1f%%, Status: %s",
resource.name, resource.cpu, resource.memory, resource.status)
- case "truenas":
- return fmt.Sprintf("TrueNAS '%s': CPU %.1f%%, Memory %.1f%%, Status: %s",
- resource.name, resource.cpu, resource.memory, resource.status)
case "agent":
+ if strings.EqualFold(strings.TrimSpace(resource.platform), "truenas") {
+ return fmt.Sprintf("TrueNAS system '%s': CPU %.1f%%, Memory %.1f%%, Status: %s",
+ resource.name, resource.cpu, resource.memory, resource.status)
+ }
return fmt.Sprintf("Agent host '%s': CPU %.1f%%, Memory %.1f%%, Status: %s",
resource.name, resource.cpu, resource.memory, resource.status)
case "vm":
diff --git a/internal/ai/patrol_triage.go b/internal/ai/patrol_triage.go
index 62f2929fe1..9040255359 100644
--- a/internal/ai/patrol_triage.go
+++ b/internal/ai/patrol_triage.go
@@ -743,7 +743,7 @@ func triageRuntimeResourceType(snap patrolRuntimeState, knownType, resourceID st
for _, host := range patrolHostAlertRows(snap) {
if strings.EqualFold(strings.TrimSpace(host.id), normalizedID) ||
strings.EqualFold(strings.TrimSpace(host.hostname), normalizedID) {
- return host.resourceType
+ return patrolHostResourceType(host.platform)
}
}
@@ -767,11 +767,8 @@ func triageGuestResourceType(knownType, resourceID string) string {
}
func triageCanonicalResourceType(resourceType string) string {
- switch normalized := strings.ToLower(strings.TrimSpace(resourceType)); normalized {
- case "vm", "system-container", "app-container", "node", "storage", "docker-host", "pbs", "pmg", "agent", "truenas", "physical_disk", "physical-disk":
- if normalized == "physical-disk" {
- return "physical_disk"
- }
+ switch normalized := canonicalizeAICompatibilityResourceType(resourceType); normalized {
+ case "vm", "system-container", "app-container", "node", "storage", "docker-host", "pbs", "pmg", "agent", "physical_disk":
return normalized
default:
return ""
@@ -792,7 +789,7 @@ func triageConnectionResourceType(resourceID string) string {
case strings.HasPrefix(id, "docker-host:"), strings.HasPrefix(id, "docker-host/"), strings.HasPrefix(id, "app-container:"), strings.HasPrefix(id, "app-container/"), strings.HasPrefix(id, "docker-"), strings.Contains(id, "docker"):
return "docker-host"
case strings.HasPrefix(id, "truenas-"), strings.HasPrefix(id, "truenas:"), strings.HasPrefix(id, "truenas/"), strings.Contains(id, "truenas"):
- return "truenas"
+ return "agent"
case strings.HasPrefix(id, "pbs-"), strings.HasPrefix(id, "pbs/"), strings.Contains(id, "pbs"):
return "pbs"
case strings.HasPrefix(id, "pmg-"), strings.HasPrefix(id, "pmg/"), strings.Contains(id, "pmg"):
diff --git a/internal/ai/patrol_triage_test.go b/internal/ai/patrol_triage_test.go
index 046cb3bf47..e95631031c 100644
--- a/internal/ai/patrol_triage_test.go
+++ b/internal/ai/patrol_triage_test.go
@@ -821,8 +821,14 @@ func TestTriageAlertChecksState_ResolvesTrueNASHostsToCanonicalResourceType(t *t
if len(flags) != 1 {
t.Fatalf("expected 1 alert-backed triage flag, got %d", len(flags))
}
- if flags[0].ResourceType != "truenas" {
- t.Fatalf("expected TrueNAS resource type, got %#v", flags[0])
+ if flags[0].ResourceType != "agent" {
+ t.Fatalf("expected canonical agent resource type, got %#v", flags[0])
+ }
+}
+
+func TestTriageRuntimeResourceType_CanonicalizesTrueNASConnectionIDsToAgent(t *testing.T) {
+ if got := triageRuntimeResourceType(patrolRuntimeState{}, "", "truenas-main"); got != "agent" {
+ t.Fatalf("expected truenas connection IDs to canonicalize to agent, got %q", got)
}
}
diff --git a/internal/ai/patrol_triggers.go b/internal/ai/patrol_triggers.go
index e8e20c19b0..72468215c7 100644
--- a/internal/ai/patrol_triggers.go
+++ b/internal/ai/patrol_triggers.go
@@ -3,7 +3,6 @@ package ai
import (
"context"
"sort"
- "strings"
"sync"
"time"
@@ -538,7 +537,7 @@ func slicesEqual(a, b []string) bool {
}
func canonicalPatrolScopeResourceType(resourceType string) string {
- normalized := strings.ToLower(strings.TrimSpace(resourceType))
+ normalized := canonicalizeAICompatibilityResourceType(resourceType)
if normalized == "" {
return ""
}
@@ -560,6 +559,8 @@ func canonicalPatrolScopeResourceType(resourceType string) string {
return "agent"
case "node":
return "node"
+ case "physical_disk":
+ return "physical_disk"
default:
return normalized
}
diff --git a/internal/ai/patrol_triggers_test.go b/internal/ai/patrol_triggers_test.go
index 851c8de1aa..bcc80255c8 100644
--- a/internal/ai/patrol_triggers_test.go
+++ b/internal/ai/patrol_triggers_test.go
@@ -494,6 +494,13 @@ func TestPatrolScopeFactories_IgnoreLegacyResourceTypes(t *testing.T) {
}
}
+func TestPatrolScopeFactories_CanonicalizeTrueNASToAgent(t *testing.T) {
+ scope := AlertTriggeredPatrolScope("alert-truenas", "res-truenas", "truenas", "offline")
+ if len(scope.ResourceTypes) != 1 || scope.ResourceTypes[0] != "agent" {
+ t.Fatalf("expected canonical agent scope for truenas host, got %v", scope.ResourceTypes)
+ }
+}
+
func TestAlertClearedPatrolScope(t *testing.T) {
scope := AlertClearedPatrolScope("alert-2", "res-2", "vm")
diff --git a/internal/ai/resource_type_legacy.go b/internal/ai/resource_type_legacy.go
index 51bd5e3541..4ebff0d336 100644
--- a/internal/ai/resource_type_legacy.go
+++ b/internal/ai/resource_type_legacy.go
@@ -6,10 +6,24 @@ import (
"github.com/rcourtman/pulse-go-rewrite/internal/unifiedresources"
)
+// canonicalizeAICompatibilityResourceType collapses compatibility-only runtime
+// tokens onto the canonical v6 AI resource type contract.
+func canonicalizeAICompatibilityResourceType(value string) string {
+ normalized := strings.ToLower(strings.TrimSpace(value))
+ switch normalized {
+ case "truenas":
+ return "agent"
+ case "physical-disk":
+ return "physical_disk"
+ default:
+ return normalized
+ }
+}
+
// isUnsupportedLegacyAIResourceTypeToken reports legacy/v5 type tokens that are
// explicitly rejected by strict v6 AI resource-type normalization paths.
func isUnsupportedLegacyAIResourceTypeToken(value string) bool {
- normalized := strings.ToLower(strings.TrimSpace(value))
+ normalized := canonicalizeAICompatibilityResourceType(value)
if normalized == "" {
return false
}