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.

Move policy posture summary into unified resources

rcourtman116bce58e187
diff --git a/docs/release-control/v6/internal/subsystems/ai-runtime.md b/docs/release-control/v6/internal/subsystems/ai-runtime.md
index 6d883eacde..aa01502a79 100644
--- a/docs/release-control/v6/internal/subsystems/ai-runtime.md
+++ b/docs/release-control/v6/internal/subsystems/ai-runtime.md
@@ -167,6 +167,10 @@ redacted values stay consistent without AI-local presentation shims.
Those same paths also use the shared resource display-name helper, so the
name-or-ID fallback stays aligned across chat extraction, resource context,
and unified adapter presentation.
+The policy-posture aggregate itself now also comes from
+`internal/unifiedresources/policy_posture.go`, so AI summaries and resource
+context reuse the same canonical sensitivity, routing, and redaction counts
+instead of collecting governance posture in an AI-local helper.
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 270ce92396..7148f1e428 100644
--- a/docs/release-control/v6/internal/subsystems/unified-resources.md
+++ b/docs/release-control/v6/internal/subsystems/unified-resources.md
@@ -108,6 +108,10 @@ The patrol-local memory conversion helpers also live in
`internal/ai/memory/presentation.go`, so the canonical change timeline and
the Patrol memory fallback both translate through the same adapter boundary
instead of carrying duplicate mapping code.
+The canonical policy-posture aggregate now also lives in
+`internal/unifiedresources/policy_posture.go`, so AI summaries and resource
+context share the same sensitivity, routing, and redaction counts from the
+resource model instead of rebuilding governance posture in AI-local code.
The backend AI and Patrol graph context renderers now derive their canonical
relationship labels, direction, provenance, freshness, and metadata flags
from `internal/unifiedresources/relationship_presentation.go`, so the graph
diff --git a/internal/ai/intelligence.go b/internal/ai/intelligence.go
index c7788b754e..747ae2c693 100644
--- a/internal/ai/intelligence.go
+++ b/internal/ai/intelligence.go
@@ -57,22 +57,22 @@ type HealthScore struct {
// ResourceIntelligence aggregates all AI knowledge about a single resource
type ResourceIntelligence struct {
- ResourceID string `json:"resource_id"`
- ResourceName string `json:"resource_name,omitempty"`
- ResourceType string `json:"resource_type,omitempty"`
- Health HealthScore `json:"health"`
- ActiveFindings []*Finding `json:"active_findings,omitempty"`
- Predictions []patterns.FailurePrediction `json:"predictions,omitempty"`
- Dependencies []string `json:"dependencies,omitempty"` // Resources this depends on
- Dependents []string `json:"dependents,omitempty"` // Resources that depend on this
- Correlations []*correlation.Correlation `json:"correlations,omitempty"`
- Baselines map[string]*baseline.FlatBaseline `json:"baselines,omitempty"`
- Anomalies []AnomalyReport `json:"anomalies,omitempty"`
- RecentIncidents []*memory.Incident `json:"recent_incidents,omitempty"`
- RecentChanges []unifiedresources.ResourceChange `json:"recent_changes,omitempty"`
- PolicyPosture *PolicyPostureSummary `json:"policy_posture,omitempty"`
- Knowledge *knowledge.GuestKnowledge `json:"knowledge,omitempty"`
- NoteCount int `json:"note_count"`
+ ResourceID string `json:"resource_id"`
+ ResourceName string `json:"resource_name,omitempty"`
+ ResourceType string `json:"resource_type,omitempty"`
+ Health HealthScore `json:"health"`
+ ActiveFindings []*Finding `json:"active_findings,omitempty"`
+ Predictions []patterns.FailurePrediction `json:"predictions,omitempty"`
+ Dependencies []string `json:"dependencies,omitempty"` // Resources this depends on
+ Dependents []string `json:"dependents,omitempty"` // Resources that depend on this
+ Correlations []*correlation.Correlation `json:"correlations,omitempty"`
+ Baselines map[string]*baseline.FlatBaseline `json:"baselines,omitempty"`
+ Anomalies []AnomalyReport `json:"anomalies,omitempty"`
+ RecentIncidents []*memory.Incident `json:"recent_incidents,omitempty"`
+ RecentChanges []unifiedresources.ResourceChange `json:"recent_changes,omitempty"`
+ PolicyPosture *unifiedresources.PolicyPostureSummary `json:"policy_posture,omitempty"`
+ Knowledge *knowledge.GuestKnowledge `json:"knowledge,omitempty"`
+ NoteCount int `json:"note_count"`
}
// AnomalyReport describes a metric that's deviating from baseline
@@ -104,7 +104,7 @@ type IntelligenceSummary struct {
RecentRemediations []memory.RemediationRecord `json:"recent_remediations,omitempty"`
// Data governance posture
- PolicyPosture *PolicyPostureSummary `json:"policy_posture,omitempty"`
+ PolicyPosture *unifiedresources.PolicyPostureSummary `json:"policy_posture,omitempty"`
// Learning progress
Learning LearningStats `json:"learning"`
@@ -286,7 +286,7 @@ func (i *Intelligence) GetSummary() *IntelligenceSummary {
}
if unifiedResourceProvider != nil {
- summary.PolicyPosture = summarizePolicyPosture(
+ summary.PolicyPosture = unifiedresources.SummarizePolicyPosture(
normalizeUnifiedResourceContextSlice(unifiedResourceProvider.GetAll()),
)
}
@@ -362,7 +362,7 @@ func (i *Intelligence) GetResourceIntelligence(resourceID string) *ResourceIntel
}
if unifiedResourceProvider != nil {
- intel.PolicyPosture = summarizePolicyPosture(
+ intel.PolicyPosture = unifiedresources.SummarizePolicyPosture(
normalizeUnifiedResourceContextSlice(unifiedResourceProvider.GetAll()),
)
}
diff --git a/internal/ai/intelligence_coverage_test.go b/internal/ai/intelligence_coverage_test.go
index 0271534a46..b6e99344be 100644
--- a/internal/ai/intelligence_coverage_test.go
+++ b/internal/ai/intelligence_coverage_test.go
@@ -208,7 +208,7 @@ func TestIntelligence_getLearningStats(t *testing.T) {
}
}
-func TestIntelligence_summarizePolicyPosture(t *testing.T) {
+func TestIntelligence_SummarizePolicyPostureUsesSharedHelper(t *testing.T) {
resources := []ur.Resource{
{
ID: "public-1",
@@ -246,7 +246,7 @@ func TestIntelligence_summarizePolicyPosture(t *testing.T) {
},
}
- summary := summarizePolicyPosture(normalizeUnifiedResourceContextSlice(resources))
+ summary := ur.SummarizePolicyPosture(normalizeUnifiedResourceContextSlice(resources))
if summary == nil {
t.Fatal("expected posture summary")
}
diff --git a/internal/ai/resource_context.go b/internal/ai/resource_context.go
index afea102a57..9bca25a7b6 100644
--- a/internal/ai/resource_context.go
+++ b/internal/ai/resource_context.go
@@ -101,7 +101,7 @@ func (s *Service) buildUnifiedResourceContextForModel(destinationModel string) s
infrastructure := normalizeUnifiedResourceContextSlice(urp.GetInfrastructure())
workloads := normalizeUnifiedResourceContextSlice(urp.GetWorkloads())
allResources := normalizeUnifiedResourceContextSlice(urp.GetAll())
- policyPosture := summarizePolicyPosture(allResources)
+ policyPosture := unifiedresources.SummarizePolicyPosture(allResources)
sensitivityCounts := map[unifiedresources.ResourceSensitivity]int{}
routingCounts := map[unifiedresources.ResourceRoutingScope]int{}
var redactionHints []unifiedresources.ResourceRedactionHint
diff --git a/internal/unifiedresources/code_standards_test.go b/internal/unifiedresources/code_standards_test.go
index 2ba667435c..7a308bbc55 100644
--- a/internal/unifiedresources/code_standards_test.go
+++ b/internal/unifiedresources/code_standards_test.go
@@ -351,6 +351,34 @@ func TestResourcePolicyLabelHelpersUsedByAIConsumers(t *testing.T) {
}
}
+func TestPolicyPostureSummaryIsOwnedByUnifiedResources(t *testing.T) {
+ requiredSnippets := map[string][]string{
+ filepath.Join(".", "policy_posture.go"): {
+ "type PolicyPostureSummary struct {",
+ "func SummarizePolicyPosture(resources []Resource) *PolicyPostureSummary",
+ },
+ filepath.Join("..", "ai", "intelligence.go"): {
+ "unifiedresources.PolicyPostureSummary",
+ "unifiedresources.SummarizePolicyPosture(",
+ },
+ filepath.Join("..", "ai", "resource_context.go"): {
+ "unifiedresources.SummarizePolicyPosture(allResources)",
+ },
+ }
+
+ for name, snippets := range requiredSnippets {
+ data, err := os.ReadFile(name)
+ if err != nil {
+ t.Fatalf("failed to read %s: %v", name, err)
+ }
+ for _, snippet := range snippets {
+ if !strings.Contains(string(data), snippet) {
+ t.Fatalf("%s must contain %q", name, snippet)
+ }
+ }
+ }
+}
+
func TestExportDecisionHelpersUsedByAIConsumers(t *testing.T) {
data, err := os.ReadFile(filepath.Join("..", "ai", "resource_export.go"))
if err != nil {
diff --git a/internal/ai/policy_posture.go b/internal/unifiedresources/policy_posture.go
similarity index 50%
rename from internal/ai/policy_posture.go
rename to internal/unifiedresources/policy_posture.go
index 7930ef6cb3..a5f9a1a381 100644
--- a/internal/ai/policy_posture.go
+++ b/internal/unifiedresources/policy_posture.go
@@ -1,26 +1,26 @@
-package ai
-
-import unifiedresources "github.com/rcourtman/pulse-go-rewrite/internal/unifiedresources"
+package unifiedresources
// PolicyPostureSummary captures the canonical data-governance posture derived
// from the unified resource registry.
type PolicyPostureSummary struct {
- TotalResources int `json:"total_resources"`
- SensitivityCounts map[unifiedresources.ResourceSensitivity]int `json:"sensitivity_counts,omitempty"`
- RoutingCounts map[unifiedresources.ResourceRoutingScope]int `json:"routing_counts,omitempty"`
- RedactionCounts map[unifiedresources.ResourceRedactionHint]int `json:"redaction_counts,omitempty"`
+ TotalResources int `json:"total_resources"`
+ SensitivityCounts map[ResourceSensitivity]int `json:"sensitivity_counts,omitempty"`
+ RoutingCounts map[ResourceRoutingScope]int `json:"routing_counts,omitempty"`
+ RedactionCounts map[ResourceRedactionHint]int `json:"redaction_counts,omitempty"`
}
-func summarizePolicyPosture(resources []unifiedresources.Resource) *PolicyPostureSummary {
+// SummarizePolicyPosture aggregates canonical policy posture across the given
+// unified resources.
+func SummarizePolicyPosture(resources []Resource) *PolicyPostureSummary {
if len(resources) == 0 {
return &PolicyPostureSummary{}
}
summary := &PolicyPostureSummary{
TotalResources: len(resources),
- SensitivityCounts: make(map[unifiedresources.ResourceSensitivity]int),
- RoutingCounts: make(map[unifiedresources.ResourceRoutingScope]int),
- RedactionCounts: make(map[unifiedresources.ResourceRedactionHint]int),
+ SensitivityCounts: make(map[ResourceSensitivity]int),
+ RoutingCounts: make(map[ResourceRoutingScope]int),
+ RedactionCounts: make(map[ResourceRedactionHint]int),
}
for _, resource := range resources {
diff --git a/internal/unifiedresources/policy_posture_test.go b/internal/unifiedresources/policy_posture_test.go
new file mode 100644
index 0000000000..46cfcf772a
--- /dev/null
+++ b/internal/unifiedresources/policy_posture_test.go
@@ -0,0 +1,80 @@
+package unifiedresources
+
+import "testing"
+
+func TestSummarizePolicyPosture(t *testing.T) {
+ t.Parallel()
+
+ resources := []Resource{
+ {
+ ID: "public-1",
+ Name: "public-vm",
+ Type: ResourceTypeVM,
+ Tags: []string{"public"},
+ },
+ {
+ ID: "internal-1",
+ Name: "agent-1",
+ Type: ResourceTypeAgent,
+ Agent: &AgentData{Hostname: "agent-1"},
+ Status: StatusOnline,
+ },
+ {
+ ID: "sensitive-1",
+ Name: "db-1",
+ Type: ResourceTypeVM,
+ Status: StatusOnline,
+ Identity: ResourceIdentity{
+ Hostnames: []string{"db.internal"},
+ IPAddresses: []string{"10.0.0.10"},
+ },
+ Canonical: &CanonicalIdentity{
+ PlatformID: "db.internal",
+ Aliases: []string{"db-1"},
+ },
+ },
+ {
+ ID: "restricted-1",
+ Name: "mail-gw",
+ Type: ResourceTypePMG,
+ Status: StatusWarning,
+ PMG: &PMGData{Hostname: "mail.internal"},
+ },
+ }
+
+ for i := range resources {
+ RefreshPolicyMetadata(&resources[i])
+ }
+
+ summary := SummarizePolicyPosture(resources)
+ if summary == nil {
+ t.Fatal("expected posture summary")
+ }
+ if summary.TotalResources != 4 {
+ t.Fatalf("total resources = %d, want 4", summary.TotalResources)
+ }
+ if got := summary.SensitivityCounts[ResourceSensitivityPublic]; got != 1 {
+ t.Fatalf("public sensitivity count = %d, want 1", got)
+ }
+ if got := summary.SensitivityCounts[ResourceSensitivityInternal]; got != 1 {
+ t.Fatalf("internal sensitivity count = %d, want 1", got)
+ }
+ if got := summary.SensitivityCounts[ResourceSensitivitySensitive]; got != 1 {
+ t.Fatalf("sensitive sensitivity count = %d, want 1", got)
+ }
+ if got := summary.SensitivityCounts[ResourceSensitivityRestricted]; got != 1 {
+ t.Fatalf("restricted sensitivity count = %d, want 1", got)
+ }
+ if got := summary.RoutingCounts[ResourceRoutingScopeCloudSummary]; got != 2 {
+ t.Fatalf("cloud summary routing count = %d, want 2", got)
+ }
+ if got := summary.RoutingCounts[ResourceRoutingScopeLocalFirst]; got != 1 {
+ t.Fatalf("local first routing count = %d, want 1", got)
+ }
+ if got := summary.RoutingCounts[ResourceRoutingScopeLocalOnly]; got != 1 {
+ t.Fatalf("local only routing count = %d, want 1", got)
+ }
+ if got := summary.RedactionCounts[ResourceRedactionHostname]; got == 0 {
+ t.Fatal("expected hostname redaction count")
+ }
+}