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.

Centralize discovery canonicalization helpers

rcourtmanaabbd85350ec
diff --git a/docs/release-control/v6/internal/subsystems/ai-runtime.md b/docs/release-control/v6/internal/subsystems/ai-runtime.md
index ffe655d038..3f2fa7eed0 100644
--- a/docs/release-control/v6/internal/subsystems/ai-runtime.md
+++ b/docs/release-control/v6/internal/subsystems/ai-runtime.md
@@ -131,6 +131,10 @@ the summary layout locally.
The chat prefetch path now also calls the shared governed-summary predicate
directly at each mention site, so it no longer carries a local wrapper around
the canonical policy decision or a separate mention-summary trim helper.
+Structured mention resolution also uses the shared AI tools discovery
+canonicalization helpers now, so chat prefetch and discovery responses agree
+on resource-type and target-ID formatting instead of maintaining chat-local
+copies.
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/internal/ai/chat/context_prefetch.go b/internal/ai/chat/context_prefetch.go
index 563977585b..29ec037d91 100644
--- a/internal/ai/chat/context_prefetch.go
+++ b/internal/ai/chat/context_prefetch.go
@@ -466,7 +466,7 @@ func (p *ContextPrefetcher) resolveStructuredMentions(structured []StructuredMen
Msg("[ContextPrefetch] Ignoring unsupported legacy structured mention type")
continue
}
- resourceType := canonicalMentionResourceType(sm.Type)
+ resourceType := tools.CanonicalDiscoveryResourceType(sm.Type)
// Use the canonical unified-resource resolver for routing plus policy metadata.
resolved := unifiedresources.ResolveResourceContext(rs, sm.Name)
@@ -655,46 +655,10 @@ func parseStructuredDockerMentionID(mentionID string, rs unifiedresources.ReadSt
return parts[0], parts[1]
}
-func canonicalMentionResourceType(raw string) string {
- resourceType := strings.ToLower(strings.TrimSpace(raw))
- switch resourceType {
- case "docker":
- return "app-container"
- case "docker-host":
- return "docker-host"
- case "k8s-cluster":
- return "k8s-cluster"
- case "k8s-pod":
- return "k8s-pod"
- case "k8s-deployment":
- return "k8s-deployment"
- default:
- return resourceType
- }
-}
-
-func discoveryResourceType(resourceType string) string {
- switch canonicalMentionResourceType(resourceType) {
- case "app-container":
- // Discovery backend still stores Docker containers under "docker".
- return "docker"
- default:
- return canonicalMentionResourceType(resourceType)
- }
-}
-
-func canonicalDiscoveryTargetID(discovery *tools.ResourceDiscoveryInfo) string {
- if discovery == nil {
- return ""
- }
- targetID := strings.TrimSpace(discovery.TargetID)
- return targetID
-}
-
// getOrTriggerDiscovery gets existing discovery or triggers a new one
func (p *ContextPrefetcher) getOrTriggerDiscovery(ctx context.Context, mention ResourceMention) (*tools.ResourceDiscoveryInfo, error) {
- discoveryType := discoveryResourceType(mention.ResourceType)
- canonicalType := canonicalMentionResourceType(mention.ResourceType)
+ discoveryType := tools.DiscoveryProviderResourceType(mention.ResourceType)
+ canonicalType := tools.CanonicalDiscoveryResourceType(mention.ResourceType)
// First try to get existing discovery
discovery, err := p.discoveryProvider.GetDiscoveryByResource(discoveryType, mention.TargetID, mention.ResourceID)
@@ -737,8 +701,8 @@ func (p *ContextPrefetcher) formatContextSummary(mentions []ResourceMention, dis
// Create a map for quick discovery lookup
discoveryMap := make(map[string]*tools.ResourceDiscoveryInfo)
for _, d := range discoveries {
- discoveryTargetID := canonicalDiscoveryTargetID(d)
- key := fmt.Sprintf("%s:%s:%s", canonicalMentionResourceType(d.ResourceType), discoveryTargetID, d.ResourceID)
+ discoveryTargetID := tools.CanonicalDiscoveryTargetID(d, "")
+ key := fmt.Sprintf("%s:%s:%s", tools.CanonicalDiscoveryResourceType(d.ResourceType), discoveryTargetID, d.ResourceID)
discoveryMap[key] = d
}
@@ -748,11 +712,11 @@ func (p *ContextPrefetcher) formatContextSummary(mentions []ResourceMention, dis
continue
}
- key := fmt.Sprintf("%s:%s:%s", canonicalMentionResourceType(mention.ResourceType), mention.TargetID, mention.ResourceID)
+ key := fmt.Sprintf("%s:%s:%s", tools.CanonicalDiscoveryResourceType(mention.ResourceType), mention.TargetID, mention.ResourceID)
discovery, hasDiscovery := discoveryMap[key]
// Docker containers get special treatment - show the full routing chain
- if canonicalMentionResourceType(mention.ResourceType) == "app-container" {
+ if tools.CanonicalDiscoveryResourceType(mention.ResourceType) == "app-container" {
sb.WriteString(fmt.Sprintf("## %s (Docker container)\n", mention.Name))
// Show the full routing chain unambiguously
diff --git a/internal/ai/chat/context_prefetch_additional_test.go b/internal/ai/chat/context_prefetch_additional_test.go
index 12a9758132..c5405be5b3 100644
--- a/internal/ai/chat/context_prefetch_additional_test.go
+++ b/internal/ai/chat/context_prefetch_additional_test.go
@@ -510,25 +510,3 @@ func TestContextPrefetcher_FormatContextSummary_UsesSharedGovernedBlockFormatter
t.Fatalf("expected governed summary block to match shared formatter, got %q", summary)
}
}
-
-func TestCanonicalMentionResourceType(t *testing.T) {
- testCases := []struct {
- in string
- want string
- }{
- {in: "docker", want: "app-container"},
- {in: "docker-container", want: "docker-container"},
- {in: "app_container", want: "app_container"},
- {in: "dockerhost", want: "dockerhost"},
- {in: "k8s_cluster", want: "k8s_cluster"},
- {in: "k8s_pod", want: "k8s_pod"},
- {in: "k8s_deployment", want: "k8s_deployment"},
- {in: "system-container", want: "system-container"},
- }
-
- for _, tc := range testCases {
- if got := canonicalMentionResourceType(tc.in); got != tc.want {
- t.Fatalf("canonicalMentionResourceType(%q) = %q, want %q", tc.in, got, tc.want)
- }
- }
-}
diff --git a/internal/ai/tools/tools_discovery.go b/internal/ai/tools/tools_discovery.go
index 646b9ce5bf..e691f8a100 100644
--- a/internal/ai/tools/tools_discovery.go
+++ b/internal/ai/tools/tools_discovery.go
@@ -209,7 +209,9 @@ func getCommonServicePaths(serviceType string) (logPaths, configPaths, dataPaths
return nil, nil, nil
}
-func canonicalDiscoveryTargetID(discovery *ResourceDiscoveryInfo, fallbackTargetID string) string {
+// CanonicalDiscoveryTargetID returns the canonical target ID for a discovery,
+// falling back to the provided target ID when the discovery payload omits one.
+func CanonicalDiscoveryTargetID(discovery *ResourceDiscoveryInfo, fallbackTargetID string) string {
if discovery == nil {
return strings.TrimSpace(fallbackTargetID)
}
@@ -231,8 +233,8 @@ func (e *PulseToolExecutor) executeGetDiscovery(ctx context.Context, args map[st
if isUnsupportedDiscoveryLegacyResourceTypeToken(resourceTypeRaw) {
return NewErrorResult(fmt.Errorf("unsupported resource_type %q", strings.TrimSpace(resourceTypeRaw))), nil
}
- resourceType := canonicalDiscoveryResourceType(resourceTypeRaw)
- providerResourceType := discoveryProviderResourceType(resourceType)
+ resourceType := CanonicalDiscoveryResourceType(resourceTypeRaw)
+ providerResourceType := DiscoveryProviderResourceType(resourceType)
targetID = strings.TrimSpace(targetID)
if resourceType == "" {
@@ -357,10 +359,10 @@ func (e *PulseToolExecutor) executeGetDiscovery(ctx context.Context, args map[st
}
}
- discoveryTargetID := canonicalDiscoveryTargetID(discovery, targetID)
+ discoveryTargetID := CanonicalDiscoveryTargetID(discovery, targetID)
// Return the discovery information
- responseResourceType := canonicalDiscoveryResourceType(discovery.ResourceType)
+ responseResourceType := CanonicalDiscoveryResourceType(discovery.ResourceType)
if responseResourceType == "" {
responseResourceType = resourceType
}
@@ -510,7 +512,9 @@ func isUnsupportedDiscoveryLegacyResourceTypeToken(value string) bool {
}
}
-func canonicalDiscoveryResourceType(raw string) string {
+// CanonicalDiscoveryResourceType returns the canonical discovery resource type
+// used by the AI tools layer.
+func CanonicalDiscoveryResourceType(raw string) string {
resourceType := strings.ToLower(strings.TrimSpace(raw))
switch resourceType {
case "docker", "app-container":
@@ -520,13 +524,15 @@ func canonicalDiscoveryResourceType(raw string) string {
}
}
-func discoveryProviderResourceType(canonical string) string {
- switch canonicalDiscoveryResourceType(canonical) {
+// DiscoveryProviderResourceType returns the discovery-provider resource type
+// used for the canonical discovery representation.
+func DiscoveryProviderResourceType(canonical string) string {
+ switch CanonicalDiscoveryResourceType(canonical) {
case "app-container":
// Discovery storage still keys Docker/container runtime discoveries as "docker".
return "docker"
default:
- return canonicalDiscoveryResourceType(canonical)
+ return CanonicalDiscoveryResourceType(canonical)
}
}
@@ -542,8 +548,8 @@ func (e *PulseToolExecutor) executeListDiscoveries(_ context.Context, args map[s
if isUnsupportedDiscoveryLegacyResourceTypeToken(filterType) {
return NewErrorResult(fmt.Errorf("unsupported type %q", strings.TrimSpace(filterType))), nil
}
- filterType = canonicalDiscoveryResourceType(filterType)
- providerFilterType := discoveryProviderResourceType(filterType)
+ filterType = CanonicalDiscoveryResourceType(filterType)
+ providerFilterType := DiscoveryProviderResourceType(filterType)
filterTargetID = strings.TrimSpace(filterTargetID)
if filterType != "" && !isSupportedDiscoveryResourceType(filterType) {
@@ -587,10 +593,10 @@ func (e *PulseToolExecutor) executeListDiscoveries(_ context.Context, args map[s
// Build response
results := make([]map[string]interface{}, 0, len(discoveries))
for _, d := range discoveries {
- discoveryTargetID := canonicalDiscoveryTargetID(d, "")
+ discoveryTargetID := CanonicalDiscoveryTargetID(d, "")
result := map[string]interface{}{
"id": d.ID,
- "resource_type": canonicalDiscoveryResourceType(d.ResourceType),
+ "resource_type": CanonicalDiscoveryResourceType(d.ResourceType),
"resource_id": d.ResourceID,
"target_id": discoveryTargetID,
"agent_id": d.AgentID,
diff --git a/internal/ai/tools/tools_discovery_test.go b/internal/ai/tools/tools_discovery_test.go
index 5ccc8a2829..61131b9481 100644
--- a/internal/ai/tools/tools_discovery_test.go
+++ b/internal/ai/tools/tools_discovery_test.go
@@ -135,6 +135,80 @@ func TestExecuteGetDiscovery_UsesTargetID(t *testing.T) {
assert.NotContains(t, payload, "host_id")
}
+func TestCanonicalDiscoveryResourceType(t *testing.T) {
+ tests := []struct {
+ in string
+ want string
+ }{
+ {in: "docker", want: "app-container"},
+ {in: "app-container", want: "app-container"},
+ {in: "k8s-cluster", want: "k8s-cluster"},
+ {in: "k8s-pod", want: "k8s-pod"},
+ {in: "docker-host", want: "docker-host"},
+ }
+
+ for _, tc := range tests {
+ t.Run(tc.in, func(t *testing.T) {
+ assert.Equal(t, tc.want, CanonicalDiscoveryResourceType(tc.in))
+ })
+ }
+}
+
+func TestDiscoveryProviderResourceType(t *testing.T) {
+ tests := []struct {
+ in string
+ want string
+ }{
+ {in: "app-container", want: "docker"},
+ {in: "docker", want: "docker"},
+ {in: "vm", want: "vm"},
+ }
+
+ for _, tc := range tests {
+ t.Run(tc.in, func(t *testing.T) {
+ assert.Equal(t, tc.want, DiscoveryProviderResourceType(tc.in))
+ })
+ }
+}
+
+func TestCanonicalDiscoveryTargetID(t *testing.T) {
+ tests := []struct {
+ name string
+ in *ResourceDiscoveryInfo
+ fallback string
+ want string
+ }{
+ {
+ name: "uses discovery target",
+ in: &ResourceDiscoveryInfo{
+ TargetID: "node1",
+ },
+ fallback: "fallback",
+ want: "node1",
+ },
+ {
+ name: "falls back when target missing",
+ in: &ResourceDiscoveryInfo{
+ TargetID: " ",
+ },
+ fallback: "fallback",
+ want: "fallback",
+ },
+ {
+ name: "nil discovery falls back",
+ in: nil,
+ fallback: "fallback",
+ want: "fallback",
+ },
+ }
+
+ for _, tc := range tests {
+ t.Run(tc.name, func(t *testing.T) {
+ assert.Equal(t, tc.want, CanonicalDiscoveryTargetID(tc.in, tc.fallback))
+ })
+ }
+}
+
func TestExecuteGetDiscovery_TargetIDRequired(t *testing.T) {
exec := NewPulseToolExecutor(ExecutorConfig{DiscoveryProvider: &stubDiscoveryProvider{}})
diff --git a/internal/unifiedresources/code_standards_test.go b/internal/unifiedresources/code_standards_test.go
index 513104f3b5..fd29a5cfae 100644
--- a/internal/unifiedresources/code_standards_test.go
+++ b/internal/unifiedresources/code_standards_test.go
@@ -399,9 +399,17 @@ func TestResourcePolicyLabelHelpersUsedByAIConsumers(t *testing.T) {
"unifiedresources.ResourcePolicyRedactedValue(",
},
filepath.Join("..", "ai", "chat", "context_prefetch.go"): {
- "unifiedresources.ResourcePolicyRequiresGovernedSummary(m.Policy)",
+ "tools.CanonicalDiscoveryResourceType(",
+ "tools.DiscoveryProviderResourceType(",
+ "tools.CanonicalDiscoveryTargetID(",
+ "unifiedresources.ResourcePolicyRequiresGovernedSummary(mention.Policy)",
"unifiedresources.FormatResourcePolicyGovernedSummary(mention.AISafeSummary, mention.Policy)",
},
+ filepath.Join("..", "ai", "tools", "tools_discovery.go"): {
+ "func CanonicalDiscoveryResourceType(raw string) string",
+ "func DiscoveryProviderResourceType(canonical string) string",
+ "func CanonicalDiscoveryTargetID(discovery *ResourceDiscoveryInfo, fallbackTargetID string) string",
+ },
filepath.Join("..", "ai", "resource_export.go"): {
"unifiedresources.ResourceRedactionLabelsFromHints(redactionHints)",
},