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 governed mention block formatting

rcourtman345a17c1be30
diff --git a/docs/release-control/v6/internal/subsystems/ai-runtime.md b/docs/release-control/v6/internal/subsystems/ai-runtime.md
index d629226228..803ed0af6c 100644
--- a/docs/release-control/v6/internal/subsystems/ai-runtime.md
+++ b/docs/release-control/v6/internal/subsystems/ai-runtime.md
@@ -125,6 +125,9 @@ the same routing and redaction rules as the rendered summary itself.
The governed mention preamble and footer text now also come from the shared
policy presenter, so the warning copy around the block does not drift from the
canonical policy wording.
+The complete governed mention block is also assembled by the shared policy
+presenter, so chat prefetch only decides when to render it and never rebuilds
+the summary layout locally.
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/unified-resources.md b/docs/release-control/v6/internal/subsystems/unified-resources.md
index 54dc54d2c7..191765d473 100644
--- a/docs/release-control/v6/internal/subsystems/unified-resources.md
+++ b/docs/release-control/v6/internal/subsystems/unified-resources.md
@@ -208,6 +208,8 @@ prefetching, so the decision to surface the block follows the same canonical
local-only and redaction rules as the rendered policy text.
The same helper also owns the governed-mention preamble and footer copy, so
the warning language around the block stays centralized with the policy model.
+The same helper now also assembles the complete governed mention block, so
+chat prefetch only routes the output instead of rebuilding the layout.
The same shared policy helper also owns the `aiSafeSummary` decision and
redaction predicates used by AI chat knowledge extraction and resource
context rendering, so governed labels and summary selection stay rooted in
diff --git a/internal/ai/chat/context_prefetch.go b/internal/ai/chat/context_prefetch.go
index 1f7530129c..b248539d57 100644
--- a/internal/ai/chat/context_prefetch.go
+++ b/internal/ai/chat/context_prefetch.go
@@ -744,7 +744,7 @@ func (p *ContextPrefetcher) formatContextSummary(mentions []ResourceMention, dis
for _, mention := range mentions {
if mention.requiresGovernedSummary() {
- formatGovernedMentionSummary(&sb, mention)
+ sb.WriteString(unifiedresources.FormatResourcePolicyGovernedSummary(mention.AISafeSummary, mention.Policy))
continue
}
@@ -909,20 +909,6 @@ func (m ResourceMention) requiresGovernedSummary() bool {
return unifiedresources.ResourcePolicyRequiresGovernedSummary(m.Policy)
}
-func formatGovernedMentionSummary(sb *strings.Builder, mention ResourceMention) {
- sb.WriteString("## Governed resource\n")
- if summary := strings.TrimSpace(mention.AISafeSummary); summary != "" {
- sb.WriteString(summary)
- sb.WriteString("\n")
- }
- for _, line := range unifiedresources.ResourcePolicySummaryLines(mention.Policy) {
- sb.WriteString(line)
- sb.WriteString("\n")
- }
- sb.WriteString(unifiedresources.ResourcePolicyGovernedSummaryFooter())
- sb.WriteString("\n\n")
-}
-
// extractWords extracts words (3+ characters) from a message for matching
func extractWords(message string) []string {
// Split on common delimiters and filter short words
diff --git a/internal/ai/chat/context_prefetch_additional_test.go b/internal/ai/chat/context_prefetch_additional_test.go
index f7d2a7ff9c..01bb489d75 100644
--- a/internal/ai/chat/context_prefetch_additional_test.go
+++ b/internal/ai/chat/context_prefetch_additional_test.go
@@ -495,6 +495,49 @@ func TestContextPrefetcher_FormatContextSummary_GovernedMention(t *testing.T) {
}
}
+func TestContextPrefetcher_FormatContextSummary_UsesSharedGovernedBlockFormatter(t *testing.T) {
+ prefetcher := NewContextPrefetcher(newTestReadState(models.StateSnapshot{}), nil)
+
+ summary := prefetcher.formatContextSummary([]ResourceMention{{
+ Name: "customer-db",
+ ResourceType: "system-container",
+ ResourceID: "201",
+ TargetID: "node1",
+ AISafeSummary: "system container resource; status online; local-only context",
+ Policy: &unifiedresources.ResourcePolicy{
+ Sensitivity: unifiedresources.ResourceSensitivityRestricted,
+ Routing: unifiedresources.ResourceRoutingPolicy{
+ Scope: unifiedresources.ResourceRoutingScopeLocalOnly,
+ AllowCloudSummary: false,
+ AllowCloudRawSignals: false,
+ Redact: []unifiedresources.ResourceRedactionHint{
+ unifiedresources.ResourceRedactionAlias,
+ unifiedresources.ResourceRedactionHostname,
+ },
+ },
+ },
+ }}, nil)
+
+ want := unifiedresources.FormatResourcePolicyGovernedSummary(
+ "system container resource; status online; local-only context",
+ &unifiedresources.ResourcePolicy{
+ Sensitivity: unifiedresources.ResourceSensitivityRestricted,
+ Routing: unifiedresources.ResourceRoutingPolicy{
+ Scope: unifiedresources.ResourceRoutingScopeLocalOnly,
+ AllowCloudSummary: false,
+ AllowCloudRawSignals: false,
+ Redact: []unifiedresources.ResourceRedactionHint{
+ unifiedresources.ResourceRedactionAlias,
+ unifiedresources.ResourceRedactionHostname,
+ },
+ },
+ },
+ )
+ if !strings.Contains(summary, want) {
+ t.Fatalf("expected governed summary block to match shared formatter, got %q", summary)
+ }
+}
+
func TestCanonicalMentionResourceType(t *testing.T) {
testCases := []struct {
in string
diff --git a/internal/unifiedresources/code_standards_test.go b/internal/unifiedresources/code_standards_test.go
index d229fab2f1..b6c4180fe7 100644
--- a/internal/unifiedresources/code_standards_test.go
+++ b/internal/unifiedresources/code_standards_test.go
@@ -363,8 +363,7 @@ func TestResourcePolicyLabelHelpersUsedByAIConsumers(t *testing.T) {
},
filepath.Join("..", "ai", "chat", "context_prefetch.go"): {
"unifiedresources.ResourcePolicyRequiresGovernedSummary(m.Policy)",
- "unifiedresources.ResourcePolicyGovernedSummaryPreamble()",
- "unifiedresources.ResourcePolicyGovernedSummaryFooter()",
+ "unifiedresources.FormatResourcePolicyGovernedSummary(mention.AISafeSummary, mention.Policy)",
},
filepath.Join("..", "ai", "resource_export.go"): {
"unifiedresources.ResourceRedactionLabelsFromHints(redactionHints)",
@@ -386,6 +385,7 @@ func TestResourcePolicyLabelHelpersUsedByAIConsumers(t *testing.T) {
"func ResourcePolicyRequiresGovernedSummary(policy *ResourcePolicy) bool",
"func ResourcePolicyGovernedSummaryPreamble() string",
"func ResourcePolicyGovernedSummaryFooter() string",
+ "func FormatResourcePolicyGovernedSummary(summary string, policy *ResourcePolicy) string",
"func ResourceDisplayName(resource Resource) string",
},
}
diff --git a/internal/unifiedresources/policy_metadata_test.go b/internal/unifiedresources/policy_metadata_test.go
index 55ba9da3a3..c256e0f9a5 100644
--- a/internal/unifiedresources/policy_metadata_test.go
+++ b/internal/unifiedresources/policy_metadata_test.go
@@ -188,6 +188,25 @@ func TestResourcePolicyGovernedSummaryGuidance(t *testing.T) {
}
}
+func TestFormatResourcePolicyGovernedSummary(t *testing.T) {
+ policy := &ResourcePolicy{
+ Sensitivity: ResourceSensitivityRestricted,
+ Routing: ResourceRoutingPolicy{
+ Scope: ResourceRoutingScopeLocalOnly,
+ Redact: []ResourceRedactionHint{
+ ResourceRedactionHostname,
+ ResourceRedactionAlias,
+ },
+ },
+ }
+
+ got := FormatResourcePolicyGovernedSummary("system container resource; status online; local-only context", policy)
+ want := "## Governed resource\nsystem container resource; status online; local-only context\nPolicy: sensitivity=Restricted, routing=Local Only, cloud_summary=false, cloud_raw_signals=false\nRedactions: Hostname, Alias\nRaw routing coordinates, bind mounts, hostnames, and discovery file paths withheld by canonical resource policy.\n\n"
+ if got != want {
+ t.Fatalf("FormatResourcePolicyGovernedSummary() = %q, want %q", got, want)
+ }
+}
+
func TestResourcePolicyRedactsAndUsesAISafeSummary(t *testing.T) {
policy := &ResourcePolicy{
Sensitivity: ResourceSensitivitySensitive,
diff --git a/internal/unifiedresources/policy_presentation.go b/internal/unifiedresources/policy_presentation.go
index 3672693948..601daf7bac 100644
--- a/internal/unifiedresources/policy_presentation.go
+++ b/internal/unifiedresources/policy_presentation.go
@@ -214,6 +214,19 @@ func ResourcePolicyGovernedSummaryFooter() string {
return "Raw routing coordinates, bind mounts, hostnames, and discovery file paths withheld by canonical resource policy."
}
+// FormatResourcePolicyGovernedSummary returns the canonical governed mention
+// block for AI chat prefetch and related policy-aware summaries.
+func FormatResourcePolicyGovernedSummary(summary string, policy *ResourcePolicy) string {
+ lines := make([]string, 0, 4)
+ lines = append(lines, "## Governed resource")
+ if summary = strings.TrimSpace(summary); summary != "" {
+ lines = append(lines, summary)
+ }
+ lines = append(lines, ResourcePolicySummaryLines(policy)...)
+ lines = append(lines, ResourcePolicyGovernedSummaryFooter())
+ return strings.Join(lines, "\n") + "\n\n"
+}
+
// ResourcePolicyRedacts reports whether the policy redacts any of the provided hints.
func ResourcePolicyRedacts(policy *ResourcePolicy, hints ...ResourceRedactionHint) bool {
if policy == nil {