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 resource policy cloning

rcourtman699a81f7a25e
diff --git a/docs/release-control/v6/internal/subsystems/ai-runtime.md b/docs/release-control/v6/internal/subsystems/ai-runtime.md
index 02e1a84858..8616b2bd40 100644
--- a/docs/release-control/v6/internal/subsystems/ai-runtime.md
+++ b/docs/release-control/v6/internal/subsystems/ai-runtime.md
@@ -149,6 +149,9 @@ detectors only serving as fallback coverage when the canonical store is not
available. When that patrol-local fallback is used, it must render through the
shared memory change presentation helper so the same heading, scope prefix, and
change-type labels are reused instead of being rebuilt ad hoc in AI-local code.
+AI-facing policy metadata must also be cloned through the shared unified-
+resource policy helper so chat and tools consumers do not maintain their own
+policy copy logic.
That shared presentation layer also owns the elapsed-time and "ago" wording
utilities, so the same "time ago" phrasing stays consistent across resource,
incident, and fallback memory summaries instead of being reformatted
diff --git a/docs/release-control/v6/internal/subsystems/unified-resources.md b/docs/release-control/v6/internal/subsystems/unified-resources.md
index 27bc1dc8e1..bd49085cc2 100644
--- a/docs/release-control/v6/internal/subsystems/unified-resources.md
+++ b/docs/release-control/v6/internal/subsystems/unified-resources.md
@@ -131,6 +131,9 @@ The canonical unified-resource change and relationship presenters now also
share the same elapsed-time and "ago" wording utilities, so `observed`,
`last seen`, and `ago` fragments stay consistent without each formatter
maintaining its own "time ago" implementation.
+The canonical resource policy model also owns a shared clone helper, so AI
+chat and tools consumers preserve policy metadata by copying through the same
+unified-resource contract instead of maintaining their own deep-copy logic.
The change emitter now also classifies canonical restart changes for Docker
and Kubernetes resources when restart counters increase or uptime resets, so
the timeline can distinguish restarts from generic state transitions instead
diff --git a/internal/ai/chat/context_prefetch.go b/internal/ai/chat/context_prefetch.go
index 581465c683..f684eaea13 100644
--- a/internal/ai/chat/context_prefetch.go
+++ b/internal/ai/chat/context_prefetch.go
@@ -901,10 +901,7 @@ func cloneMentionPolicy(resource *unifiedresources.Resource) *unifiedresources.R
if resource == nil || resource.Policy == nil {
return nil
}
-
- policy := *resource.Policy
- policy.Routing.Redact = append([]unifiedresources.ResourceRedactionHint(nil), resource.Policy.Routing.Redact...)
- return &policy
+ return unifiedresources.CloneResourcePolicy(resource.Policy)
}
func mentionAISafeSummary(resource *unifiedresources.Resource) string {
diff --git a/internal/ai/chat/context_prefetch_additional_test.go b/internal/ai/chat/context_prefetch_additional_test.go
index b97039180e..90abd90e57 100644
--- a/internal/ai/chat/context_prefetch_additional_test.go
+++ b/internal/ai/chat/context_prefetch_additional_test.go
@@ -462,6 +462,39 @@ func TestContextPrefetcher_FormatContextSummary_GovernedMention(t *testing.T) {
}
}
+func TestCloneMentionPolicyUsesSharedHelper(t *testing.T) {
+ resource := &unifiedresources.Resource{
+ Policy: &unifiedresources.ResourcePolicy{
+ Sensitivity: unifiedresources.ResourceSensitivityRestricted,
+ Routing: unifiedresources.ResourceRoutingPolicy{
+ Scope: unifiedresources.ResourceRoutingScopeLocalOnly,
+ Redact: []unifiedresources.ResourceRedactionHint{
+ unifiedresources.ResourceRedactionHostname,
+ unifiedresources.ResourceRedactionIPAddress,
+ },
+ },
+ },
+ }
+
+ clone := cloneMentionPolicy(resource)
+ if clone == nil {
+ t.Fatal("expected clone")
+ }
+ if clone == resource.Policy {
+ t.Fatal("expected clone to allocate a new policy")
+ }
+ clone.Routing.Redact[0] = unifiedresources.ResourceRedactionAlias
+ if resource.Policy.Routing.Redact[0] != unifiedresources.ResourceRedactionHostname {
+ t.Fatalf("expected clone to deep copy redactions, original mutated to %#v", resource.Policy.Routing.Redact)
+ }
+ if clone.Sensitivity != resource.Policy.Sensitivity {
+ t.Fatalf("clone sensitivity = %q, want %q", clone.Sensitivity, resource.Policy.Sensitivity)
+ }
+ if clone.Routing.Scope != unifiedresources.ResourceRoutingScopeLocalOnly {
+ t.Fatalf("clone routing scope = %q, want %q", clone.Routing.Scope, unifiedresources.ResourceRoutingScopeLocalOnly)
+ }
+}
+
func TestCanonicalMentionResourceType(t *testing.T) {
testCases := []struct {
in string
diff --git a/internal/ai/tools/tools_query.go b/internal/ai/tools/tools_query.go
index 09dce96c99..892b41ad00 100644
--- a/internal/ai/tools/tools_query.go
+++ b/internal/ai/tools/tools_query.go
@@ -2205,21 +2205,11 @@ func governedQueryMetadataFromResolvedResource(resource *unifiedresources.Resour
resourceCopy := *resource
unifiedresources.RefreshPolicyMetadata(&resourceCopy)
return GovernedResourceMetadata{
- Policy: cloneGovernedQueryPolicy(resourceCopy.Policy),
+ Policy: unifiedresources.CloneResourcePolicy(resourceCopy.Policy),
AISafeSummary: strings.TrimSpace(resourceCopy.AISafeSummary),
}
}
-func cloneGovernedQueryPolicy(policy *unifiedresources.ResourcePolicy) *unifiedresources.ResourcePolicy {
- if policy == nil {
- return nil
- }
-
- copyPolicy := *policy
- copyPolicy.Routing.Redact = append([]unifiedresources.ResourceRedactionHint(nil), policy.Routing.Redact...)
- return &copyPolicy
-}
-
// executeQuery routes to the appropriate query handler based on action
func (e *PulseToolExecutor) executeQuery(ctx context.Context, args map[string]interface{}) (CallToolResult, error) {
action, _ := args["action"].(string)
diff --git a/internal/ai/tools/tools_query_test.go b/internal/ai/tools/tools_query_test.go
index c332436471..a9bedb0f5c 100644
--- a/internal/ai/tools/tools_query_test.go
+++ b/internal/ai/tools/tools_query_test.go
@@ -8,6 +8,7 @@ import (
"github.com/rcourtman/pulse-go-rewrite/internal/agentexec"
"github.com/rcourtman/pulse-go-rewrite/internal/models"
+ "github.com/rcourtman/pulse-go-rewrite/internal/unifiedresources"
)
func TestQueryResponsesUseCanonicalEmptyCollections(t *testing.T) {
@@ -770,6 +771,51 @@ func TestExecuteQuerySurfacesIncludeGovernedMetadata(t *testing.T) {
}
}
+func TestGovernedQueryMetadataFromResolvedResourceClonesPolicy(t *testing.T) {
+ resource := &unifiedresources.Resource{
+ ID: "vm-1",
+ Name: "customer-payments",
+ Tags: []string{"customer-data"},
+ Identity: unifiedresources.ResourceIdentity{
+ Hostnames: []string{"payments.internal"},
+ IPAddresses: []string{"10.0.0.44"},
+ },
+ Canonical: &unifiedresources.CanonicalIdentity{
+ PlatformID: "payments.internal",
+ PrimaryID: "vm:100",
+ Aliases: []string{"vm-100"},
+ },
+ }
+
+ metadata := governedQueryMetadataFromResolvedResource(resource)
+ if metadata.Policy == nil {
+ t.Fatal("expected policy metadata")
+ }
+ if metadata.Policy == resource.Policy {
+ t.Fatal("expected metadata policy to be a clone")
+ }
+ if len(metadata.Policy.Routing.Redact) == 0 {
+ t.Fatal("expected redactions on governed metadata policy")
+ }
+ baselinePolicy := unifiedresources.CloneResourcePolicy(metadata.Policy)
+ metadata.Policy.Routing.Redact[0] = unifiedresources.ResourceRedactionAlias
+ if baselinePolicy.Routing.Redact[0] != unifiedresources.ResourceRedactionHostname {
+ t.Fatalf("expected clone to deep copy redactions, baseline mutated to %#v", baselinePolicy.Routing.Redact)
+ }
+ if metadata.AISafeSummary == "" {
+ t.Fatal("expected ai safe summary")
+ }
+ if metadata.AISafeSummary == resource.Name {
+ t.Fatalf("expected safe summary to avoid raw resource name: %q", metadata.AISafeSummary)
+ }
+ if metadata.Policy.Sensitivity != unifiedresources.ResourceSensitivityRestricted {
+ t.Fatalf("expected restricted sensitivity, got %q", metadata.Policy.Sensitivity)
+ }
+ if metadata.Policy.Routing.Scope != unifiedresources.ResourceRoutingScopeLocalOnly {
+ t.Fatalf("expected local-only routing, got %q", metadata.Policy.Routing.Scope)
+ }
+}
+
func TestExecuteGetResource_DockerDetails(t *testing.T) {
state := models.StateSnapshot{
DockerHosts: []models.DockerHost{{
diff --git a/internal/unifiedresources/code_standards_test.go b/internal/unifiedresources/code_standards_test.go
index 8a72dbd8c1..96f5e1138e 100644
--- a/internal/unifiedresources/code_standards_test.go
+++ b/internal/unifiedresources/code_standards_test.go
@@ -293,6 +293,29 @@ func TestResourcePolicyPresentationUsesCanonicalLabels(t *testing.T) {
}
}
+func TestResourcePolicyCloneHelperUsedByAIConsumers(t *testing.T) {
+ requiredFiles := []string{
+ filepath.Join("..", "ai", "chat", "context_prefetch.go"),
+ filepath.Join("..", "ai", "tools", "tools_query.go"),
+ filepath.Join(".", "policy_metadata.go"),
+ }
+ requiredSnippets := map[string]string{
+ filepath.Join("..", "ai", "chat", "context_prefetch.go"): "unifiedresources.CloneResourcePolicy(resource.Policy)",
+ filepath.Join("..", "ai", "tools", "tools_query.go"): "unifiedresources.CloneResourcePolicy(resourceCopy.Policy)",
+ filepath.Join(".", "policy_metadata.go"): "func CloneResourcePolicy(policy *ResourcePolicy) *ResourcePolicy",
+ }
+ for _, name := range requiredFiles {
+ data, err := os.ReadFile(name)
+ if err != nil {
+ t.Fatalf("failed to read %s: %v", name, err)
+ }
+ snippet := requiredSnippets[name]
+ if !strings.Contains(string(data), snippet) {
+ t.Fatalf("%s must contain %q", name, snippet)
+ }
+ }
+}
+
func TestResourceTimelineStoreIndexesSupportFilteredReads(t *testing.T) {
data, err := os.ReadFile(filepath.Join("store.go"))
if err != nil {
diff --git a/internal/unifiedresources/policy_metadata.go b/internal/unifiedresources/policy_metadata.go
index bea8807f56..8cd46941d8 100644
--- a/internal/unifiedresources/policy_metadata.go
+++ b/internal/unifiedresources/policy_metadata.go
@@ -50,6 +50,16 @@ type ResourceRoutingPolicy struct {
Redact []ResourceRedactionHint `json:"redact,omitempty"`
}
+// CloneResourcePolicy returns a deep copy of the canonical resource policy.
+func CloneResourcePolicy(policy *ResourcePolicy) *ResourcePolicy {
+ if policy == nil {
+ return nil
+ }
+ copyPolicy := *policy
+ copyPolicy.Routing.Redact = append([]ResourceRedactionHint(nil), policy.Routing.Redact...)
+ return &copyPolicy
+}
+
// RefreshPolicyMetadata derives canonical sensitivity, routing, and AI-safe summary data.
func RefreshPolicyMetadata(resource *Resource) {
if resource == nil {
diff --git a/internal/unifiedresources/policy_metadata_test.go b/internal/unifiedresources/policy_metadata_test.go
index 8623c07384..fad3788c28 100644
--- a/internal/unifiedresources/policy_metadata_test.go
+++ b/internal/unifiedresources/policy_metadata_test.go
@@ -210,6 +210,44 @@ func TestResourcePolicyRedactsAndUsesAISafeSummary(t *testing.T) {
}
}
+func TestCloneResourcePolicy(t *testing.T) {
+ t.Parallel()
+
+ original := &ResourcePolicy{
+ Sensitivity: ResourceSensitivityRestricted,
+ Routing: ResourceRoutingPolicy{
+ Scope: ResourceRoutingScopeLocalOnly,
+ AllowCloudSummary: false,
+ AllowCloudRawSignals: false,
+ Redact: []ResourceRedactionHint{
+ ResourceRedactionHostname,
+ ResourceRedactionIPAddress,
+ },
+ },
+ }
+
+ clone := CloneResourcePolicy(original)
+ if clone == nil {
+ t.Fatal("expected clone")
+ }
+ if clone == original {
+ t.Fatal("expected clone to allocate a new policy")
+ }
+ if clone.Sensitivity != original.Sensitivity || clone.Routing.Scope != original.Routing.Scope {
+ t.Fatalf("clone does not match original: %#v vs %#v", clone, original)
+ }
+ if len(clone.Routing.Redact) != len(original.Routing.Redact) {
+ t.Fatalf("clone redactions = %#v, want %#v", clone.Routing.Redact, original.Routing.Redact)
+ }
+ clone.Routing.Redact[0] = ResourceRedactionPath
+ if original.Routing.Redact[0] != ResourceRedactionHostname {
+ t.Fatalf("expected clone to deep copy redactions, original mutated to %#v", original.Routing.Redact)
+ }
+ if CloneResourcePolicy(nil) != nil {
+ t.Fatal("expected nil clone for nil policy")
+ }
+}
+
func containsRedactionHint(hints []ResourceRedactionHint, want ResourceRedactionHint) bool {
for _, hint := range hints {
if hint == want {