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 correlation summary formatting

rcourtman8f499b13d348
diff --git a/docs/release-control/v6/internal/subsystems/ai-runtime.md b/docs/release-control/v6/internal/subsystems/ai-runtime.md
index 2559aea42a..7cc8c95950 100644
--- a/docs/release-control/v6/internal/subsystems/ai-runtime.md
+++ b/docs/release-control/v6/internal/subsystems/ai-runtime.md
@@ -164,6 +164,10 @@ AI resource and incident context now also surfaces a canonical resource-graph
section from unified-resource relationships, so relationship wording and edge
provenance stay aligned with the same shared resource model instead of being
reconstructed from the drawer or prompt helpers.
+The related-resource correlation section now also comes from the shared
+correlation formatter in `internal/ai/correlation`, so resource chat and
+incident prompts reuse the same learned-edge wording instead of rebuilding a
+second patrol-local bullet format.
The Patrol intelligence page now also fetches the learned correlation list
from the canonical AI correlations endpoint, so the global AI surface and the
resource drawer both expose the same learned edge evidence instead of only
diff --git a/docs/release-control/v6/internal/subsystems/patrol-intelligence.md b/docs/release-control/v6/internal/subsystems/patrol-intelligence.md
index 53c899a12d..5dedabdef0 100644
--- a/docs/release-control/v6/internal/subsystems/patrol-intelligence.md
+++ b/docs/release-control/v6/internal/subsystems/patrol-intelligence.md
@@ -85,6 +85,10 @@ Those same Patrol-owned prompt contexts now also surface a canonical
resource-graph section from unified-resource relationships, so edge labels,
directionality, and provenance stay aligned with the shared graph model
instead of being reconstructed locally.
+The Patrol seed context and AI runtime prompt path now also share the same
+correlation summary formatter from `internal/ai/correlation`, so learned-edge
+wording and confidence/count annotations stay canonical across the prompt
+surface instead of being rebuilt in each caller.
The Patrol page also now renders the canonical intelligence summary card
through the governed AI client and store, so the visible page summary and the
resource/timeline sections stay aligned on the same shared backend slice.
diff --git a/internal/ai/correlation/detector.go b/internal/ai/correlation/detector.go
index 822886071b..43e9f95c41 100644
--- a/internal/ai/correlation/detector.go
+++ b/internal/ai/correlation/detector.go
@@ -183,7 +183,7 @@ func (d *Detector) detectCorrelations(newEvent Event) {
existing.Description = d.formatCorrelationDescription(existing)
} else {
// Create new correlation
- d.correlations[key] = &Correlation{
+ correlation := &Correlation{
SourceID: oldEvent.ResourceID,
SourceName: oldEvent.ResourceName,
SourceType: oldEvent.ResourceType,
@@ -196,6 +196,8 @@ func (d *Detector) detectCorrelations(newEvent Event) {
Confidence: 0.1, // Low initial confidence
LastSeen: newEvent.Timestamp,
}
+ correlation.Description = d.formatCorrelationDescription(correlation)
+ d.correlations[key] = correlation
}
}
}
@@ -235,6 +237,51 @@ func (d *Detector) formatCorrelationDescription(c *Correlation) string {
", " + targetName + " often follows within " + delayStr
}
+// FormatCorrelationSummary returns the canonical human-readable correlation
+// summary used by AI context formatting and seed prompts.
+func FormatCorrelationSummary(c *Correlation) string {
+ if c == nil {
+ return ""
+ }
+
+ summary := strings.TrimSpace(c.Description)
+ if summary == "" {
+ sourceName := strings.TrimSpace(c.SourceName)
+ if sourceName == "" {
+ sourceName = strings.TrimSpace(c.SourceID)
+ }
+ targetName := strings.TrimSpace(c.TargetName)
+ if targetName == "" {
+ targetName = strings.TrimSpace(c.TargetID)
+ }
+ sourceEvent := strings.TrimSpace(c.EventPattern)
+ if parts := strings.Split(c.EventPattern, " -> "); len(parts) == 2 {
+ sourceEvent = strings.TrimSpace(parts[0])
+ }
+ if sourceName != "" || targetName != "" || sourceEvent != "" {
+ summary = "When " + sourceName + " experiences " + sourceEvent + ", " + targetName + " usually follows within " + formatDuration(c.AvgDelay)
+ }
+ }
+ if summary == "" {
+ summary = strings.TrimSpace(c.EventPattern)
+ }
+ if summary == "" {
+ return ""
+ }
+
+ details := make([]string, 0, 2)
+ if c.Confidence > 0 {
+ details = append(details, "confidence: "+formatConfidence(c.Confidence))
+ }
+ if c.Occurrences > 0 {
+ details = append(details, fmt.Sprintf("seen %dx", c.Occurrences))
+ }
+ if len(details) > 0 {
+ summary += " (" + strings.Join(details, ", ") + ")"
+ }
+ return summary
+}
+
// GetCorrelations returns all detected correlations above minimum confidence
func (d *Detector) GetCorrelations() []*Correlation {
d.mu.RLock()
@@ -375,10 +422,8 @@ func (d *Detector) FormatForContext(resourceID string) string {
result += "\n... and more\n"
break
}
- if c.Description != "" {
- result += "- " + c.Description + "\n"
- } else {
- result += "- " + c.EventPattern + " (" + formatConfidence(c.Confidence) + " confidence)\n"
+ if summary := FormatCorrelationSummary(c); summary != "" {
+ result += "- " + summary + "\n"
}
}
diff --git a/internal/ai/correlation/detector_coverage_test.go b/internal/ai/correlation/detector_coverage_test.go
index e6581d8289..62ff924690 100644
--- a/internal/ai/correlation/detector_coverage_test.go
+++ b/internal/ai/correlation/detector_coverage_test.go
@@ -182,6 +182,39 @@ func TestFormatForContext_LimitsAndFallback(t *testing.T) {
}
}
+func TestFormatCorrelationSummary(t *testing.T) {
+ summary := FormatCorrelationSummary(&Correlation{
+ SourceID: "node-1",
+ TargetID: "vm-1",
+ EventPattern: "high_cpu -> restart",
+ Occurrences: 4,
+ AvgDelay: 10 * time.Minute,
+ Confidence: 0.8,
+ })
+
+ if !strings.Contains(summary, "When node-1 experiences high_cpu, vm-1 usually follows within 10 minutes") {
+ t.Fatalf("expected canonical fallback summary, got %q", summary)
+ }
+ if !strings.Contains(summary, "confidence: 80%") {
+ t.Fatalf("expected confidence annotation, got %q", summary)
+ }
+ if !strings.Contains(summary, "seen 4x") {
+ t.Fatalf("expected occurrence annotation, got %q", summary)
+ }
+
+ described := FormatCorrelationSummary(&Correlation{
+ Description: "Observed pattern",
+ Occurrences: 2,
+ Confidence: 0.5,
+ })
+ if !strings.Contains(described, "Observed pattern") {
+ t.Fatalf("expected provided description to be preserved, got %q", described)
+ }
+ if !strings.Contains(described, "confidence: 50%") {
+ t.Fatalf("expected confidence annotation on described summary, got %q", described)
+ }
+}
+
func TestSaveToDisk_EmptyDataDir(t *testing.T) {
d := NewDetector(DefaultConfig())
if err := d.saveToDisk(); err != nil {
diff --git a/internal/ai/coverage_increase_test.go b/internal/ai/coverage_increase_test.go
index 52ad49d6b0..4e57868e21 100644
--- a/internal/ai/coverage_increase_test.go
+++ b/internal/ai/coverage_increase_test.go
@@ -6,6 +6,7 @@ import (
"time"
"github.com/rcourtman/pulse-go-rewrite/internal/ai/baseline"
+ "github.com/rcourtman/pulse-go-rewrite/internal/ai/correlation"
"github.com/rcourtman/pulse-go-rewrite/internal/ai/memory"
"github.com/rcourtman/pulse-go-rewrite/internal/models"
unifiedresources "github.com/rcourtman/pulse-go-rewrite/internal/unifiedresources"
@@ -502,6 +503,26 @@ func TestService_BuildResourceGraphContext_UsesCanonicalReadState(t *testing.T)
s.readState = readState
s.mu.Unlock()
+ corrCfg := DefaultCorrelationConfig()
+ corrCfg.MinOccurrences = 1
+ corrCfg.CorrelationWindow = time.Hour
+ corr := NewCorrelationDetector(corrCfg)
+ corr.RecordEvent(correlation.Event{
+ ResourceID: resourceID,
+ ResourceName: resourceID,
+ ResourceType: "vm",
+ EventType: CorrelationEventHighCPU,
+ Timestamp: now.Add(-30 * time.Minute),
+ })
+ corr.RecordEvent(correlation.Event{
+ ResourceID: "vm-graph-2",
+ ResourceName: "vm-graph-2",
+ ResourceType: "vm",
+ EventType: CorrelationEventRestart,
+ Timestamp: now.Add(-25 * time.Minute),
+ })
+ ps.SetCorrelationDetector(corr)
+
resourceCtx := s.buildEnrichedResourceContext(resourceID, "", nil)
if !strings.Contains(resourceCtx, "Resource Graph") {
t.Fatalf("expected enriched resource context to include graph section, got %q", resourceCtx)
@@ -512,6 +533,12 @@ func TestService_BuildResourceGraphContext_UsesCanonicalReadState(t *testing.T)
if !strings.Contains(resourceCtx, "discoverer proxmox_adapter") {
t.Fatalf("expected enriched resource context to include provenance, got %q", resourceCtx)
}
+ if !strings.Contains(resourceCtx, "Resource Correlations") {
+ t.Fatalf("expected enriched resource context to include correlation section, got %q", resourceCtx)
+ }
+ if !strings.Contains(resourceCtx, "seen 1x") {
+ t.Fatalf("expected enriched resource context to include shared correlation summary, got %q", resourceCtx)
+ }
incidentCtx := s.buildIncidentContext(resourceID, "")
if !strings.Contains(incidentCtx, "Resource Graph") {
diff --git a/internal/ai/patrol_ai.go b/internal/ai/patrol_ai.go
index 23c65c2587..eba70311d2 100644
--- a/internal/ai/patrol_ai.go
+++ b/internal/ai/patrol_ai.go
@@ -15,6 +15,7 @@ import (
"github.com/rcourtman/pulse-go-rewrite/internal/ai/baseline"
"github.com/rcourtman/pulse-go-rewrite/internal/ai/chat"
+ "github.com/rcourtman/pulse-go-rewrite/internal/ai/correlation"
"github.com/rcourtman/pulse-go-rewrite/internal/ai/cost"
"github.com/rcourtman/pulse-go-rewrite/internal/ai/memory"
"github.com/rcourtman/pulse-go-rewrite/internal/ai/providers"
@@ -3355,20 +3356,9 @@ func (p *PatrolService) seedIntelligenceContext(intel seedIntelligence, now time
if len(intel.correlations) > 0 {
sb.WriteString("# Known Resource Correlations\n")
for _, c := range intel.correlations {
- sourceEvent := c.EventPattern
- if parts := strings.SplitN(c.EventPattern, " -> ", 2); len(parts) == 2 {
- sourceEvent = parts[0]
+ if summary := correlation.FormatCorrelationSummary(c); summary != "" {
+ sb.WriteString("- " + summary + "\n")
}
- sourceName := c.SourceName
- if sourceName == "" {
- sourceName = c.SourceID
- }
- targetName := c.TargetName
- if targetName == "" {
- targetName = c.TargetID
- }
- sb.WriteString(fmt.Sprintf("- When %s experiences %s, %s usually follows within %s (confidence: %.0f%%, seen %dx)\n",
- sourceName, sourceEvent, targetName, seedFormatDuration(c.AvgDelay), c.Confidence*100, c.Occurrences))
}
sb.WriteString("\n")
}
diff --git a/internal/ai/service.go b/internal/ai/service.go
index 769a36cf71..431ac39a99 100644
--- a/internal/ai/service.go
+++ b/internal/ai/service.go
@@ -4921,22 +4921,11 @@ func (s *Service) buildEnrichedResourceContext(resourceID, _ string, currentMetr
sections = append(sections, graphContext)
}
- // Get correlation detector for related resources
+ // Get shared correlation detector context for learned related resources.
correlationDetector := patrol.GetCorrelationDetector()
if correlationDetector != nil {
- correlations := correlationDetector.GetCorrelationsForResource(resourceID)
- if len(correlations) > 0 {
- var corrInfo []string
- limit := 2
- if len(correlations) < limit {
- limit = len(correlations)
- }
- for _, corr := range correlations[:limit] {
- corrInfo = append(corrInfo, fmt.Sprintf("Correlated with **%s**: %s", corr.TargetName, corr.Description))
- }
- if len(corrInfo) > 0 {
- sections = append(sections, "### Related Resources\n"+strings.Join(corrInfo, "\n"))
- }
+ if ctx := correlationDetector.FormatForContext(resourceID); ctx != "" {
+ sections = append(sections, ctx)
}
}
diff --git a/internal/unifiedresources/code_standards_test.go b/internal/unifiedresources/code_standards_test.go
index 5aa8356f11..10ccf60b43 100644
--- a/internal/unifiedresources/code_standards_test.go
+++ b/internal/unifiedresources/code_standards_test.go
@@ -375,6 +375,7 @@ func TestResourceGraphContextUsesCanonicalRelationshipPresentation(t *testing.T)
"unifiedresources.DescribeRelationship(rel)",
"### Resource Graph",
"type canonicalResourceGetter interface {",
+ "correlationDetector.FormatForContext(resourceID)",
}
for _, snippet := range requiredSnippets {
if !strings.Contains(source, snippet) {
@@ -383,6 +384,23 @@ func TestResourceGraphContextUsesCanonicalRelationshipPresentation(t *testing.T)
}
}
+func TestPatrolSeedCorrelationContextUsesCanonicalSummaryFormatter(t *testing.T) {
+ data, err := os.ReadFile(filepath.Join("..", "ai", "patrol_ai.go"))
+ if err != nil {
+ t.Fatalf("failed to read patrol_ai.go: %v", err)
+ }
+ source := string(data)
+ requiredSnippets := []string{
+ "# Known Resource Correlations",
+ "correlation.FormatCorrelationSummary(c)",
+ }
+ for _, snippet := range requiredSnippets {
+ if !strings.Contains(source, snippet) {
+ t.Fatalf("internal/ai/patrol_ai.go must pin canonical correlation presentation snippet %q", snippet)
+ }
+ }
+}
+
func TestResourceFacetCountsAreCanonicalResourceFields(t *testing.T) {
typesData, err := os.ReadFile(filepath.Join("types.go"))
if err != nil {