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.

Use canonical resource timeline in patrol intelligence

rcourtman942dd4ed405d
diff --git a/docs/release-control/v6/internal/subsystems/ai-runtime.md b/docs/release-control/v6/internal/subsystems/ai-runtime.md
index 94f8c1ae4b..1cbf120e66 100644
--- a/docs/release-control/v6/internal/subsystems/ai-runtime.md
+++ b/docs/release-control/v6/internal/subsystems/ai-runtime.md
@@ -152,3 +152,7 @@ store instead of leaving command execution state in memory-only tool helpers.
Those unified-resource action and export audit records are now also exposed
through the enterprise audit read surface so operators can inspect the
execution trail without reaching into storage internals.
+The Patrol intelligence seed context now also prefers the canonical
+unified-resource timeline before falling back to the patrol-local change
+detector, so deterministic patrol context and resource detail context share
+the same change source of truth.
diff --git a/docs/release-control/v6/internal/subsystems/patrol-intelligence.md b/docs/release-control/v6/internal/subsystems/patrol-intelligence.md
index ab90bcea3c..0a439021df 100644
--- a/docs/release-control/v6/internal/subsystems/patrol-intelligence.md
+++ b/docs/release-control/v6/internal/subsystems/patrol-intelligence.md
@@ -70,3 +70,7 @@ Patrol finding state must now also consume the canonical camelCase
Frontend Patrol helpers may not keep shadow `alert_identifier` fallbacks or
drop `expiresAt` when deciding whether queued investigation fixes still need
operator attention.
+Patrol intelligence seed context should also prefer the canonical
+unified-resource timeline before falling back to the patrol-local change
+detector so recent-change context stays aligned with the resource timeline
+that powers the shared resource API.
diff --git a/internal/ai/patrol_ai.go b/internal/ai/patrol_ai.go
index d2f0e746a2..0326d3d1ca 100644
--- a/internal/ai/patrol_ai.go
+++ b/internal/ai/patrol_ai.go
@@ -1606,7 +1606,9 @@ func (p *PatrolService) seedPrecomputeIntelligenceState(snap patrolRuntimeState,
}
// Recent changes
- if cd != nil {
+ if canonicalChanges, ok := p.loadCanonicalRecentChanges(scopedSet, now.Add(-24*time.Hour), 20); ok {
+ intel.recentChanges = append(intel.recentChanges, canonicalChanges...)
+ } else if cd != nil {
allChanges := cd.GetRecentChanges(20, now.Add(-24*time.Hour))
for _, c := range allChanges {
if seedIsInScope(scopedSet, c.ResourceID) {
@@ -1643,6 +1645,118 @@ func (p *PatrolService) seedPrecomputeIntelligenceState(snap patrolRuntimeState,
return intel
}
+func (p *PatrolService) loadCanonicalRecentChanges(scopedSet map[string]bool, since time.Time, limit int) ([]memory.Change, bool) {
+ if p == nil || p.aiService == nil {
+ return nil, false
+ }
+
+ p.aiService.mu.RLock()
+ store := p.aiService.resourceExportStore
+ orgID := strings.TrimSpace(p.aiService.orgID)
+ storeOrgID := strings.TrimSpace(p.aiService.resourceExportStoreOrgID)
+ p.aiService.mu.RUnlock()
+
+ if store == nil {
+ return nil, false
+ }
+ if storeOrgID != "" && storeOrgID != orgID {
+ return nil, false
+ }
+
+ changes, err := store.GetRecentChanges("", since, limit)
+ if err != nil {
+ log.Warn().
+ Err(err).
+ Msg("failed to load canonical patrol resource timeline")
+ return nil, false
+ }
+ if len(changes) == 0 {
+ return nil, false
+ }
+
+ result := make([]memory.Change, 0, len(changes))
+ for _, change := range changes {
+ if !seedIsInScope(scopedSet, change.ResourceID) {
+ continue
+ }
+ result = append(result, patrolRecentChangeFromUnified(change))
+ if len(result) >= limit {
+ break
+ }
+ }
+ if len(result) == 0 {
+ return nil, false
+ }
+ return result, true
+}
+
+func patrolRecentChangeFromUnified(change unifiedresources.ResourceChange) memory.Change {
+ return memory.Change{
+ ResourceID: strings.TrimSpace(change.ResourceID),
+ ResourceName: strings.TrimSpace(change.ResourceID),
+ ChangeType: memory.ChangeType(formatResourceChangeKindLabel(change.Kind)),
+ DetectedAt: patrolRecentChangeDetectedAt(change),
+ Description: patrolFormatCanonicalRecentChangeDescription(change),
+ }
+}
+
+func patrolRecentChangeDetectedAt(change unifiedresources.ResourceChange) time.Time {
+ if !change.ObservedAt.IsZero() {
+ return change.ObservedAt
+ }
+ if change.OccurredAt != nil && !change.OccurredAt.IsZero() {
+ return *change.OccurredAt
+ }
+ return time.Now()
+}
+
+func patrolFormatCanonicalRecentChangeDescription(change unifiedresources.ResourceChange) string {
+ parts := []string{formatResourceChangeKindLabel(change.Kind)}
+
+ if from := strings.TrimSpace(change.From); from != "" || strings.TrimSpace(change.To) != "" {
+ switch {
+ case from != "" && strings.TrimSpace(change.To) != "":
+ parts = append(parts, fmt.Sprintf("%s -> %s", from, strings.TrimSpace(change.To)))
+ case from != "":
+ parts = append(parts, fmt.Sprintf("from %s", from))
+ default:
+ parts = append(parts, fmt.Sprintf("to %s", strings.TrimSpace(change.To)))
+ }
+ }
+
+ if sourceType := strings.TrimSpace(string(change.SourceType)); sourceType != "" {
+ if sourceAdapter := strings.TrimSpace(string(change.SourceAdapter)); sourceAdapter != "" {
+ parts = append(parts, fmt.Sprintf("source: %s via %s", sourceType, sourceAdapter))
+ } else {
+ parts = append(parts, fmt.Sprintf("source: %s", sourceType))
+ }
+ } else if sourceAdapter := strings.TrimSpace(string(change.SourceAdapter)); sourceAdapter != "" {
+ parts = append(parts, fmt.Sprintf("source: %s", sourceAdapter))
+ }
+
+ if actor := strings.TrimSpace(change.Actor); actor != "" {
+ parts = append(parts, fmt.Sprintf("actor: %s", actor))
+ }
+
+ if reason := strings.TrimSpace(change.Reason); reason != "" {
+ parts = append(parts, reason)
+ }
+
+ if len(change.RelatedResources) > 0 {
+ related := make([]string, 0, len(change.RelatedResources))
+ for _, resourceID := range change.RelatedResources {
+ if trimmed := strings.TrimSpace(resourceID); trimmed != "" {
+ related = append(related, trimmed)
+ }
+ }
+ if len(related) > 0 {
+ parts = append(parts, fmt.Sprintf("related: %s", strings.Join(related, ", ")))
+ }
+ }
+
+ return strings.Join(parts, "; ")
+}
+
// seedResourceInventory builds the node, guest, docker, storage, ceph, and PBS sections.
type patrolNodeInventoryRow struct {
id, name, status string
@@ -3227,7 +3341,11 @@ func (p *PatrolService) seedIntelligenceContext(intel seedIntelligence, now time
name = c.ResourceID
}
ago := seedFormatTimeAgo(now, c.DetectedAt)
- sb.WriteString(fmt.Sprintf("- %s (%s): %s (%s)\n", name, c.ResourceType, c.Description, ago))
+ if strings.TrimSpace(c.ResourceType) != "" {
+ sb.WriteString(fmt.Sprintf("- %s (%s): %s (%s)\n", name, c.ResourceType, c.Description, ago))
+ } else {
+ sb.WriteString(fmt.Sprintf("- %s: %s (%s)\n", name, c.Description, ago))
+ }
}
sb.WriteString("\n")
}
diff --git a/internal/ai/patrol_ai_intelligence_test.go b/internal/ai/patrol_ai_intelligence_test.go
index 1b7af6529f..3e9182d1d4 100644
--- a/internal/ai/patrol_ai_intelligence_test.go
+++ b/internal/ai/patrol_ai_intelligence_test.go
@@ -389,6 +389,55 @@ func TestSeedPrecomputeIntelligenceState_UsesRuntimeReadState(t *testing.T) {
}
}
+func TestSeedPrecomputeIntelligenceState_UsesCanonicalResourceTimeline(t *testing.T) {
+ now := time.Now()
+ canonicalStore := ur.NewMemoryStore()
+ if err := canonicalStore.RecordChange(ur.ResourceChange{
+ ID: "change-1",
+ ObservedAt: now.Add(-time.Hour),
+ ResourceID: "res-1",
+ Kind: ur.ChangeConfigUpdate,
+ SourceType: ur.SourcePulseDiff,
+ SourceAdapter: ur.AdapterOpsAgent,
+ Actor: "agent:ops-helper",
+ RelatedResources: []string{"related-1"},
+ Reason: "Config refresh",
+ }); err != nil {
+ t.Fatalf("record canonical change: %v", err)
+ }
+
+ svc := &Service{
+ orgID: "org-1",
+ resourceExportStore: canonicalStore,
+ resourceExportStoreOrgID: "org-1",
+ }
+ ps := NewPatrolService(svc, nil)
+
+ intel := ps.seedPrecomputeIntelligenceState(
+ patrolRuntimeStateForTest(ps, models.StateSnapshot{}),
+ map[string]bool{"res-1": true},
+ now,
+ )
+
+ if len(intel.recentChanges) != 1 {
+ t.Fatalf("expected 1 canonical recent change, got %d", len(intel.recentChanges))
+ }
+ change := intel.recentChanges[0]
+ if change.ResourceID != "res-1" {
+ t.Fatalf("expected canonical resource ID res-1, got %q", change.ResourceID)
+ }
+ for _, part := range []string{
+ "Config update",
+ "source: pulse_diff via agent:ops-helper",
+ "actor: agent:ops-helper",
+ "related: related-1",
+ } {
+ if !strings.Contains(change.Description, part) {
+ t.Fatalf("expected canonical description to contain %q, got %q", part, change.Description)
+ }
+ }
+}
+
func TestSeedFindingsAndContext_ResolvesMissingAndAddsNotes(t *testing.T) {
now := time.Now()
ps := NewPatrolService(nil, nil)