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.

Extract AI resource context policy owner

rcourtman06fe58144d44
diff --git a/docs/release-control/v6/internal/subsystems/ai-runtime.md b/docs/release-control/v6/internal/subsystems/ai-runtime.md
index 00b05ad358..adaaa9bf00 100644
--- a/docs/release-control/v6/internal/subsystems/ai-runtime.md
+++ b/docs/release-control/v6/internal/subsystems/ai-runtime.md
@@ -112,6 +112,10 @@ That governed context should also surface the canonical routing posture and
redaction hints that were derived from the shared policy model, so prompts
reflect the same sensitivity, routing, and scrub decisions that the runtime
uses for export boundaries instead of rebuilding privacy posture locally.
+That governed posture block and its export-routing inputs now also flow through
+the dedicated `internal/ai/resource_context_policy_model.go` owner, so
+`resource_context.go` stays on AI context composition instead of duplicating
+policy redaction sections or recomputing export metadata inline.
The same shared policy presenter also owns the routing-scope labels used in
the AI-facing policy surfaces, so the policy wording stays canonical instead
of being rendered inline by the consumer.
diff --git a/docs/release-control/v6/internal/subsystems/security-privacy.md b/docs/release-control/v6/internal/subsystems/security-privacy.md
index 5a6b1a7e62..886c51c632 100644
--- a/docs/release-control/v6/internal/subsystems/security-privacy.md
+++ b/docs/release-control/v6/internal/subsystems/security-privacy.md
@@ -238,3 +238,8 @@ on both sides: `frontend-modern/src/components/Settings/APITokenManager.tsx`,
continue to carry the direct `security-settings-surfaces` proof path together
with the API-contract token-management proof instead of borrowing coverage only
from broader settings-shell or API ownership.
+That same governed AI trust boundary also covers unified-resource context
+posture derivation: `internal/ai/resource_context_policy_model.go` is now the
+canonical owner for the policy-posture summary, local-only count, and
+redaction-hint inputs that drive outbound AI context export decisions, so
+`resource_context.go` does not duplicate trust-boundary policy assembly inline.
diff --git a/internal/ai/resource_context.go b/internal/ai/resource_context.go
index 71729f72de..d84ea7c669 100644
--- a/internal/ai/resource_context.go
+++ b/internal/ai/resource_context.go
@@ -101,39 +101,12 @@ func (s *Service) buildUnifiedResourceContextForModel(destinationModel string) s
infrastructure := unifiedresources.RefreshCanonicalMetadataSlice(urp.GetInfrastructure())
workloads := unifiedresources.RefreshCanonicalMetadataSlice(urp.GetWorkloads())
allResources := unifiedresources.RefreshCanonicalMetadataSlice(urp.GetAll())
- policyPosture := unifiedresources.SummarizePolicyPosture(allResources)
- sensitivityCounts := map[unifiedresources.ResourceSensitivity]int{}
- routingCounts := map[unifiedresources.ResourceRoutingScope]int{}
- var redactionHints []unifiedresources.ResourceRedactionHint
- localOnlyCount := 0
- if policyPosture != nil {
- sensitivityCounts = policyPosture.SensitivityCounts
- routingCounts = policyPosture.RoutingCounts
- localOnlyCount = routingCounts[unifiedresources.ResourceRoutingScopeLocalOnly]
- if len(policyPosture.RedactionCounts) > 0 {
- redactionHints = make([]unifiedresources.ResourceRedactionHint, 0, len(policyPosture.RedactionCounts))
- for hint := range policyPosture.RedactionCounts {
- redactionHints = append(redactionHints, hint)
- }
- }
- }
+ policyContext := buildUnifiedResourcePolicyContext(allResources)
byResourceID := make(map[string]unifiedresources.Resource, len(allResources))
for _, resource := range allResources {
byResourceID[resource.ID] = resource
}
- if policyPosture != nil && policyPosture.TotalResources > 0 {
- sections = append(sections, "\n### Data Governance")
- sensitivityParts := unifiedresources.ResourcePolicySensitivitySummaryFromCounts(sensitivityCounts)
- sections = append(sections, fmt.Sprintf("- Sensitivity: %s", strings.Join(sensitivityParts, ", ")))
-
- routingParts := unifiedresources.ResourcePolicyRoutingSummaryFromCounts(routingCounts)
- sections = append(sections, fmt.Sprintf("- Routing: %s", strings.Join(routingParts, ", ")))
- sections = append(sections, fmt.Sprintf("- Local-only resources: %d", localOnlyCount))
- if redactionLabels := unifiedresources.ResourcePolicyRedactionLabelsFromCounts(policyPosture.RedactionCounts); len(redactionLabels) > 0 {
- sections = append(sections, "\n### Policy Redaction Hints")
- sections = append(sections, fmt.Sprintf("- Redactions in use: %s", strings.Join(redactionLabels, ", ")))
- }
- }
+ sections = policyContext.appendSummarySections(sections)
if len(infrastructure) > 0 {
sections = append(sections, "\n### Infrastructure (Nodes & Hosts)")
@@ -511,19 +484,6 @@ func (s *Service) buildUnifiedResourceContextForModel(destinationModel string) s
}
}
- if len(redactionHints) > 0 {
- sections = append(sections, "\n### Policy Redaction Hints")
- labels := make([]string, 0, len(redactionHints))
- for _, hint := range redactionHints {
- if label := unifiedresources.ResourceRedactionHintLabel(hint); label != "" {
- labels = append(labels, label)
- }
- }
- if len(labels) > 0 {
- sections = append(sections, fmt.Sprintf("- Redactions in use: %s", strings.Join(labels, ", ")))
- }
- }
-
result := "\n\n" + strings.Join(sections, "\n")
const maxContextSize = 50000
@@ -536,7 +496,14 @@ func (s *Service) buildUnifiedResourceContextForModel(destinationModel string) s
}
if strings.TrimSpace(destinationModel) != "" {
- s.recordUnifiedResourceExport(destinationModel, result, stats, sensitivityCounts, localOnlyCount, redactionHints)
+ s.recordUnifiedResourceExport(
+ destinationModel,
+ result,
+ stats,
+ policyContext.sensitivityCounts,
+ policyContext.localOnlyCount,
+ policyContext.redactionHints,
+ )
}
log.Debug().Int("unified_resource_context_size", len(result)).Msg("built unified resource context")
diff --git a/internal/ai/resource_context_policy_model.go b/internal/ai/resource_context_policy_model.go
new file mode 100644
index 0000000000..585f36d3b2
--- /dev/null
+++ b/internal/ai/resource_context_policy_model.go
@@ -0,0 +1,106 @@
+package ai
+
+import (
+ "fmt"
+ "sort"
+ "strings"
+
+ unifiedresources "github.com/rcourtman/pulse-go-rewrite/internal/unifiedresources"
+)
+
+type unifiedResourcePolicyContext struct {
+ posture *unifiedresources.PolicyPostureSummary
+ sensitivityCounts map[unifiedresources.ResourceSensitivity]int
+ routingCounts map[unifiedresources.ResourceRoutingScope]int
+ localOnlyCount int
+ redactionHints []unifiedresources.ResourceRedactionHint
+ redactionLabels []string
+}
+
+func buildUnifiedResourcePolicyContext(resources []unifiedresources.Resource) unifiedResourcePolicyContext {
+ posture := unifiedresources.SummarizePolicyPosture(resources)
+ context := unifiedResourcePolicyContext{
+ posture: posture,
+ sensitivityCounts: map[unifiedresources.ResourceSensitivity]int{},
+ routingCounts: map[unifiedresources.ResourceRoutingScope]int{},
+ }
+ if posture == nil {
+ return context
+ }
+
+ if posture.SensitivityCounts != nil {
+ context.sensitivityCounts = posture.SensitivityCounts
+ }
+ if posture.RoutingCounts != nil {
+ context.routingCounts = posture.RoutingCounts
+ context.localOnlyCount = posture.RoutingCounts[unifiedresources.ResourceRoutingScopeLocalOnly]
+ }
+
+ context.redactionHints = resourcePolicyRedactionHintsFromCounts(posture.RedactionCounts)
+ context.redactionLabels = unifiedresources.ResourceRedactionLabelsFromHints(context.redactionHints)
+
+ return context
+}
+
+func (context unifiedResourcePolicyContext) hasGovernedResources() bool {
+ return context.posture != nil && context.posture.TotalResources > 0
+}
+
+func (context unifiedResourcePolicyContext) appendSummarySections(sections []string) []string {
+ if !context.hasGovernedResources() {
+ return sections
+ }
+
+ sections = append(sections, "\n### Data Governance")
+
+ sensitivityParts := unifiedresources.ResourcePolicySensitivitySummaryFromCounts(context.sensitivityCounts)
+ sections = append(sections, fmt.Sprintf("- Sensitivity: %s", strings.Join(sensitivityParts, ", ")))
+
+ routingParts := unifiedresources.ResourcePolicyRoutingSummaryFromCounts(context.routingCounts)
+ sections = append(sections, fmt.Sprintf("- Routing: %s", strings.Join(routingParts, ", ")))
+ sections = append(sections, fmt.Sprintf("- Local-only resources: %d", context.localOnlyCount))
+
+ if len(context.redactionLabels) > 0 {
+ sections = append(sections, "\n### Policy Redaction Hints")
+ sections = append(sections, fmt.Sprintf("- Redactions in use: %s", strings.Join(context.redactionLabels, ", ")))
+ }
+
+ return sections
+}
+
+func resourcePolicyRedactionHintsFromCounts(counts map[unifiedresources.ResourceRedactionHint]int) []unifiedresources.ResourceRedactionHint {
+ if len(counts) == 0 {
+ return nil
+ }
+
+ hints := make([]unifiedresources.ResourceRedactionHint, 0, len(counts))
+ seen := make(map[unifiedresources.ResourceRedactionHint]struct{}, len(counts))
+
+ for _, hint := range unifiedresources.ResourceRedactionHintOrder {
+ if counts[hint] <= 0 {
+ continue
+ }
+ hints = append(hints, hint)
+ seen[hint] = struct{}{}
+ }
+
+ remaining := make([]string, 0, len(counts))
+ remainingHints := make(map[string]unifiedresources.ResourceRedactionHint, len(counts))
+ for hint, count := range counts {
+ if count <= 0 {
+ continue
+ }
+ if _, ok := seen[hint]; ok {
+ continue
+ }
+ key := string(hint)
+ remaining = append(remaining, key)
+ remainingHints[key] = hint
+ }
+ sort.Strings(remaining)
+ for _, key := range remaining {
+ hints = append(hints, remainingHints[key])
+ }
+
+ return hints
+}
diff --git a/internal/ai/resource_context_policy_model_test.go b/internal/ai/resource_context_policy_model_test.go
new file mode 100644
index 0000000000..8587fcbf81
--- /dev/null
+++ b/internal/ai/resource_context_policy_model_test.go
@@ -0,0 +1,87 @@
+package ai
+
+import (
+ "reflect"
+ "strings"
+ "testing"
+
+ unifiedresources "github.com/rcourtman/pulse-go-rewrite/internal/unifiedresources"
+)
+
+func TestBuildUnifiedResourcePolicyContext(t *testing.T) {
+ resources := unifiedresources.RefreshCanonicalMetadataSlice([]unifiedresources.Resource{
+ {
+ ID: "public-1",
+ Name: "public-vm",
+ Type: unifiedresources.ResourceTypeVM,
+ Tags: []string{"public"},
+ },
+ {
+ ID: "sensitive-1",
+ Name: "db-1",
+ Type: unifiedresources.ResourceTypeVM,
+ Status: unifiedresources.StatusOnline,
+ Identity: unifiedresources.ResourceIdentity{
+ Hostnames: []string{"db.internal"},
+ IPAddresses: []string{"10.0.0.10"},
+ },
+ Canonical: &unifiedresources.CanonicalIdentity{
+ PlatformID: "db.internal",
+ Aliases: []string{"db-1"},
+ },
+ },
+ {
+ ID: "restricted-1",
+ Name: "mail-gw",
+ Type: unifiedresources.ResourceTypePMG,
+ Status: unifiedresources.StatusWarning,
+ PMG: &unifiedresources.PMGData{Hostname: "mail.internal"},
+ },
+ {
+ ID: "storage-1",
+ Name: "backup-volume",
+ Type: unifiedresources.ResourceTypeStorage,
+ Status: unifiedresources.StatusOnline,
+ Storage: &unifiedresources.StorageMeta{
+ Path: "/mnt/pve/backups",
+ },
+ },
+ })
+
+ context := buildUnifiedResourcePolicyContext(resources)
+
+ if !context.hasGovernedResources() {
+ t.Fatal("expected governed posture")
+ }
+ if context.localOnlyCount != 1 {
+ t.Fatalf("localOnlyCount = %d, want 1", context.localOnlyCount)
+ }
+
+ wantHints := []unifiedresources.ResourceRedactionHint{
+ unifiedresources.ResourceRedactionHostname,
+ unifiedresources.ResourceRedactionIPAddress,
+ unifiedresources.ResourceRedactionPlatformID,
+ unifiedresources.ResourceRedactionAlias,
+ unifiedresources.ResourceRedactionPath,
+ }
+ if !reflect.DeepEqual(context.redactionHints, wantHints) {
+ t.Fatalf("redactionHints = %#v, want %#v", context.redactionHints, wantHints)
+ }
+
+ joined := strings.Join(context.appendSummarySections(nil), "\n")
+ if !strings.Contains(joined, "### Data Governance") {
+ t.Fatalf("expected data governance section, got %q", joined)
+ }
+ if !strings.Contains(joined, "Sensitivity: 1 Public, 0 Internal, 2 Sensitive, 1 Restricted") {
+ t.Fatalf("expected canonical sensitivity summary, got %q", joined)
+ }
+ if !strings.Contains(joined, "Routing: 1 Cloud Summary, 2 Local First, 1 Local Only") {
+ t.Fatalf("expected canonical routing summary, got %q", joined)
+ }
+ if count := strings.Count(joined, "### Policy Redaction Hints"); count != 1 {
+ t.Fatalf("policy redaction hints count = %d, want 1 in %q", count, joined)
+ }
+ if !strings.Contains(joined, "Redactions in use: Hostname, IP Address, Platform ID, Alias, Path") {
+ t.Fatalf("expected canonical redaction labels, got %q", joined)
+ }
+}
diff --git a/internal/ai/resource_context_test.go b/internal/ai/resource_context_test.go
index ef3927324a..4c6c26ad6c 100644
--- a/internal/ai/resource_context_test.go
+++ b/internal/ai/resource_context_test.go
@@ -237,6 +237,9 @@ func TestBuildUnifiedResourceContext_FullContext(t *testing.T) {
assertContains("Routing: " + expectedRouting)
assertContains("Policy Redaction Hints")
assertContains("Redactions in use: Hostname, IP Address, Platform ID, Alias")
+ if count := strings.Count(got, "### Policy Redaction Hints"); count != 1 {
+ t.Fatalf("expected one policy redaction hints section, got %d in %q", count, got)
+ }
assertContains("Proxmox VE Nodes")
assertContains("HAS AGENT")
assertContains("NO AGENT")