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 AI memory change conversion

rcourtman43664a3f27ec
diff --git a/docs/release-control/v6/internal/subsystems/ai-runtime.md b/docs/release-control/v6/internal/subsystems/ai-runtime.md
index 4c2bd382bd..6d883eacde 100644
--- a/docs/release-control/v6/internal/subsystems/ai-runtime.md
+++ b/docs/release-control/v6/internal/subsystems/ai-runtime.md
@@ -204,6 +204,10 @@ The canonical recent-change section wrapper also lives in
`internal/unifiedresources.FormatResourceRecentChangesContext`, so the AI
summary and resource-specific context share the same heading and prefix rules
instead of rebuilding that section layout locally.
+The canonical memory conversion helpers also live in
+`internal/ai/memory/presentation.go`, so the Patrol fallback feed and the
+AI summary path translate between unified-resource changes and memory.Change
+through one shared adapter boundary instead of keeping local shims.
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
diff --git a/docs/release-control/v6/internal/subsystems/unified-resources.md b/docs/release-control/v6/internal/subsystems/unified-resources.md
index b37d72bb7e..270ce92396 100644
--- a/docs/release-control/v6/internal/subsystems/unified-resources.md
+++ b/docs/release-control/v6/internal/subsystems/unified-resources.md
@@ -104,6 +104,10 @@ section-specific headings are applied.
The same helper also owns `FormatResourceRecentChangesContext`, so AI runtime
callers share the canonical recent-change section heading and resource
prefixing instead of rebuilding that wrapper locally.
+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 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 6dd7c61621..c7788b754e 100644
--- a/internal/ai/intelligence.go
+++ b/internal/ai/intelligence.go
@@ -274,7 +274,7 @@ func (i *Intelligence) GetSummary() *IntelligenceSummary {
if len(recent) > 0 {
converted := make([]unifiedresources.ResourceChange, 0, len(recent))
for _, change := range recent {
- converted = append(converted, convertMemoryChangeToResourceChange(change))
+ converted = append(converted, memory.ResourceChangeFromMemoryChange(change))
}
summary.RecentChanges = append([]unifiedresources.ResourceChange{}, converted[:min(len(converted), 5)]...)
}
@@ -1049,49 +1049,11 @@ func (i *Intelligence) getRecentChangesForResource(resourceID string, limit int)
converted := make([]unifiedresources.ResourceChange, 0, len(recent))
for _, change := range recent {
- converted = append(converted, convertMemoryChangeToResourceChange(change))
+ converted = append(converted, memory.ResourceChangeFromMemoryChange(change))
}
return converted
}
-func convertMemoryChangeToResourceChange(change memory.Change) unifiedresources.ResourceChange {
- return unifiedresources.ResourceChange{
- ID: change.ID,
- ObservedAt: change.DetectedAt,
- ResourceID: change.ResourceID,
- Kind: memoryChangeTypeToUnifiedChangeKind(change.ChangeType),
- SourceType: unifiedresources.SourceHeuristic,
- Confidence: unifiedresources.ConfidenceMedium,
- RelatedResources: func() []string {
- if change.ResourceName == "" {
- return nil
- }
- return []string{change.ResourceName}
- }(),
- Reason: change.Description,
- Metadata: map[string]any{
- "memoryChangeType": string(change.ChangeType),
- },
- }
-}
-
-func memoryChangeTypeToUnifiedChangeKind(changeType memory.ChangeType) unifiedresources.ChangeKind {
- switch changeType {
- case memory.ChangeCreated, memory.ChangeDeleted, memory.ChangeStatus:
- return unifiedresources.ChangeStateTransition
- case memory.ChangeConfig:
- return unifiedresources.ChangeConfigUpdate
- case memory.ChangeMigrated:
- return unifiedresources.ChangeRelationship
- case memory.ChangeRestarted:
- return unifiedresources.ChangeRestart
- case memory.ChangeBackedUp:
- return unifiedresources.ChangeConfigUpdate
- default:
- return unifiedresources.ChangeAnomaly
- }
-}
-
func (i *Intelligence) formatBaselinesForContext(resourceID string) string {
i.mu.RLock()
store := i.baselines
diff --git a/internal/ai/memory/presentation.go b/internal/ai/memory/presentation.go
index a6246f4229..bc40461e64 100644
--- a/internal/ai/memory/presentation.go
+++ b/internal/ai/memory/presentation.go
@@ -5,6 +5,7 @@ import (
"strings"
"time"
+ "github.com/rcourtman/pulse-go-rewrite/internal/unifiedresources"
"github.com/rcourtman/pulse-go-rewrite/internal/utils"
)
@@ -73,3 +74,64 @@ func FormatRecentChangesContext(changes []Change, includeResourcePrefix bool, he
}
return strings.Join(lines, "\n")
}
+
+// ChangeFromUnifiedResourceChange converts a canonical unified-resource change
+// into the patrol-local memory representation.
+func ChangeFromUnifiedResourceChange(change unifiedresources.ResourceChange) Change {
+ detectedAt := change.ObservedAt
+ if detectedAt.IsZero() && change.OccurredAt != nil {
+ detectedAt = *change.OccurredAt
+ }
+ if detectedAt.IsZero() {
+ detectedAt = time.Now()
+ }
+
+ return Change{
+ ID: strings.TrimSpace(change.ID),
+ ResourceID: strings.TrimSpace(change.ResourceID),
+ ResourceName: strings.TrimSpace(change.ResourceID),
+ ChangeType: ChangeType(unifiedresources.ChangeKindLabel(change.Kind)),
+ DetectedAt: detectedAt,
+ Description: unifiedresources.FormatResourceChangeSummary(change),
+ }
+}
+
+// ResourceChangeFromMemoryChange converts a patrol-local memory change back
+// into the canonical unified-resource change shape.
+func ResourceChangeFromMemoryChange(change Change) unifiedresources.ResourceChange {
+ var related []string
+ if resourceName := strings.TrimSpace(change.ResourceName); resourceName != "" {
+ related = []string{resourceName}
+ }
+
+ return unifiedresources.ResourceChange{
+ ID: strings.TrimSpace(change.ID),
+ ObservedAt: change.DetectedAt,
+ ResourceID: strings.TrimSpace(change.ResourceID),
+ Kind: memoryChangeTypeToUnifiedChangeKind(change.ChangeType),
+ SourceType: unifiedresources.SourceHeuristic,
+ Confidence: unifiedresources.ConfidenceMedium,
+ RelatedResources: related,
+ Reason: change.Description,
+ Metadata: map[string]any{
+ "memoryChangeType": string(change.ChangeType),
+ },
+ }
+}
+
+func memoryChangeTypeToUnifiedChangeKind(changeType ChangeType) unifiedresources.ChangeKind {
+ switch changeType {
+ case ChangeCreated, ChangeDeleted, ChangeStatus:
+ return unifiedresources.ChangeStateTransition
+ case ChangeConfig:
+ return unifiedresources.ChangeConfigUpdate
+ case ChangeMigrated:
+ return unifiedresources.ChangeRelationship
+ case ChangeRestarted:
+ return unifiedresources.ChangeRestart
+ case ChangeBackedUp:
+ return unifiedresources.ChangeConfigUpdate
+ default:
+ return unifiedresources.ChangeAnomaly
+ }
+}
diff --git a/internal/ai/memory/presentation_test.go b/internal/ai/memory/presentation_test.go
index 71604b5c6d..96a9708bcd 100644
--- a/internal/ai/memory/presentation_test.go
+++ b/internal/ai/memory/presentation_test.go
@@ -4,6 +4,8 @@ import (
"strings"
"testing"
"time"
+
+ "github.com/rcourtman/pulse-go-rewrite/internal/unifiedresources"
)
func TestChangeTypeLabel(t *testing.T) {
@@ -73,3 +75,87 @@ func TestFormatRecentChangesContext(t *testing.T) {
}
}
}
+
+func TestChangeFromUnifiedResourceChange(t *testing.T) {
+ t.Parallel()
+
+ observedAt := time.Date(2026, time.March, 19, 12, 30, 0, 0, time.UTC)
+ occurredAt := observedAt.Add(-5 * time.Minute)
+ change := unifiedresources.ResourceChange{
+ ID: "chg-1",
+ ObservedAt: observedAt,
+ OccurredAt: &occurredAt,
+ ResourceID: "res-1",
+ Kind: unifiedresources.ChangeRestart,
+ From: " old-state ",
+ To: " new-state ",
+ SourceType: unifiedresources.SourcePulseDiff,
+ SourceAdapter: unifiedresources.AdapterOpsAgent,
+ Actor: " agent:ops-helper ",
+ Reason: " restarted after maintenance ",
+ RelatedResources: []string{"", " related-1 "},
+ }
+
+ got := ChangeFromUnifiedResourceChange(change)
+ if got.ID != "chg-1" {
+ t.Fatalf("ChangeFromUnifiedResourceChange ID = %q, want chg-1", got.ID)
+ }
+ if got.ResourceID != "res-1" {
+ t.Fatalf("ChangeFromUnifiedResourceChange ResourceID = %q, want res-1", got.ResourceID)
+ }
+ if got.ResourceName != "res-1" {
+ t.Fatalf("ChangeFromUnifiedResourceChange ResourceName = %q, want res-1", got.ResourceName)
+ }
+ if got.ChangeType != ChangeType("Restart") {
+ t.Fatalf("ChangeFromUnifiedResourceChange ChangeType = %q, want Restart", got.ChangeType)
+ }
+ if !got.DetectedAt.Equal(observedAt) {
+ t.Fatalf("ChangeFromUnifiedResourceChange DetectedAt = %v, want %v", got.DetectedAt, observedAt)
+ }
+ if got.Description != unifiedresources.FormatResourceChangeSummary(change) {
+ t.Fatalf("ChangeFromUnifiedResourceChange Description = %q, want canonical summary", got.Description)
+ }
+}
+
+func TestResourceChangeFromMemoryChange(t *testing.T) {
+ t.Parallel()
+
+ detectedAt := time.Date(2026, time.March, 19, 13, 15, 0, 0, time.UTC)
+ change := Change{
+ ID: "mem-1",
+ ResourceID: "res-2",
+ ResourceName: "rel-2",
+ ChangeType: ChangeMigrated,
+ DetectedAt: detectedAt,
+ Description: "relocated",
+ }
+
+ got := ResourceChangeFromMemoryChange(change)
+ if got.ID != "mem-1" {
+ t.Fatalf("ResourceChangeFromMemoryChange ID = %q, want mem-1", got.ID)
+ }
+ if got.ObservedAt != detectedAt {
+ t.Fatalf("ResourceChangeFromMemoryChange ObservedAt = %v, want %v", got.ObservedAt, detectedAt)
+ }
+ if got.ResourceID != "res-2" {
+ t.Fatalf("ResourceChangeFromMemoryChange ResourceID = %q, want res-2", got.ResourceID)
+ }
+ if got.Kind != unifiedresources.ChangeRelationship {
+ t.Fatalf("ResourceChangeFromMemoryChange Kind = %q, want %q", got.Kind, unifiedresources.ChangeRelationship)
+ }
+ if got.SourceType != unifiedresources.SourceHeuristic {
+ t.Fatalf("ResourceChangeFromMemoryChange SourceType = %q, want %q", got.SourceType, unifiedresources.SourceHeuristic)
+ }
+ if got.Confidence != unifiedresources.ConfidenceMedium {
+ t.Fatalf("ResourceChangeFromMemoryChange Confidence = %q, want %q", got.Confidence, unifiedresources.ConfidenceMedium)
+ }
+ if len(got.RelatedResources) != 1 || got.RelatedResources[0] != "rel-2" {
+ t.Fatalf("ResourceChangeFromMemoryChange RelatedResources = %#v, want [rel-2]", got.RelatedResources)
+ }
+ if got.Reason != "relocated" {
+ t.Fatalf("ResourceChangeFromMemoryChange Reason = %q, want relocated", got.Reason)
+ }
+ if got.Metadata["memoryChangeType"] != string(change.ChangeType) {
+ t.Fatalf("ResourceChangeFromMemoryChange Metadata = %#v, want memoryChangeType=%q", got.Metadata, change.ChangeType)
+ }
+}
diff --git a/internal/ai/patrol_ai.go b/internal/ai/patrol_ai.go
index 8cf27f1788..6fc95444f4 100644
--- a/internal/ai/patrol_ai.go
+++ b/internal/ai/patrol_ai.go
@@ -1680,7 +1680,7 @@ func (p *PatrolService) loadCanonicalRecentChanges(scopedSet map[string]bool, si
if !seedIsInScope(scopedSet, change.ResourceID) {
continue
}
- result = append(result, patrolRecentChangeFromUnified(change))
+ result = append(result, memory.ChangeFromUnifiedResourceChange(change))
if len(result) >= limit {
break
}
@@ -1691,26 +1691,6 @@ func (p *PatrolService) loadCanonicalRecentChanges(scopedSet map[string]bool, si
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(unifiedresources.ChangeKindLabel(change.Kind)),
- DetectedAt: patrolRecentChangeDetectedAt(change),
- Description: unifiedresources.FormatResourceChangeSummary(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()
-}
-
// seedResourceInventory builds the node, guest, docker, storage, ceph, and PBS sections.
type patrolNodeInventoryRow struct {
id, name, status string
diff --git a/internal/ai/patrol_ai_intelligence_test.go b/internal/ai/patrol_ai_intelligence_test.go
index 5b39dca9ae..2574c48916 100644
--- a/internal/ai/patrol_ai_intelligence_test.go
+++ b/internal/ai/patrol_ai_intelligence_test.go
@@ -8,6 +8,7 @@ import (
"github.com/rcourtman/pulse-go-rewrite/internal/ai/baseline"
"github.com/rcourtman/pulse-go-rewrite/internal/ai/knowledge"
+ "github.com/rcourtman/pulse-go-rewrite/internal/ai/memory"
"github.com/rcourtman/pulse-go-rewrite/internal/models"
"github.com/rcourtman/pulse-go-rewrite/internal/servicediscovery"
ur "github.com/rcourtman/pulse-go-rewrite/internal/unifiedresources"
@@ -452,6 +453,9 @@ func TestSeedPrecomputeIntelligenceState_UsesCanonicalResourceTimeline(t *testin
func TestPatrolRecentChangeFromUnified_UsesCanonicalChangePresentation(t *testing.T) {
change := ur.ResourceChange{
+ ID: "change-1",
+ ObservedAt: time.Date(2026, time.March, 19, 12, 30, 0, 0, time.UTC),
+ ResourceID: "res-1",
Kind: ur.ChangeAnomaly,
From: " old-state ",
To: " new-state ",
@@ -462,7 +466,16 @@ func TestPatrolRecentChangeFromUnified_UsesCanonicalChangePresentation(t *testin
RelatedResources: []string{"", " related-1 "},
}
- memoryChange := patrolRecentChangeFromUnified(change)
+ memoryChange := memory.ChangeFromUnifiedResourceChange(change)
+ if memoryChange.ID != "change-1" {
+ t.Fatalf("unexpected memory change ID: %q", memoryChange.ID)
+ }
+ if memoryChange.ResourceName != "res-1" {
+ t.Fatalf("unexpected memory change resource name: %q", memoryChange.ResourceName)
+ }
+ if !memoryChange.DetectedAt.Equal(change.ObservedAt) {
+ t.Fatalf("unexpected memory change detectedAt: %v", memoryChange.DetectedAt)
+ }
if memoryChange.ChangeType != "Metric anomaly" {
t.Fatalf("unexpected change type: %q", memoryChange.ChangeType)
}
diff --git a/internal/unifiedresources/code_standards_test.go b/internal/unifiedresources/code_standards_test.go
index 71c4c1d2a3..2ba667435c 100644
--- a/internal/unifiedresources/code_standards_test.go
+++ b/internal/unifiedresources/code_standards_test.go
@@ -528,7 +528,7 @@ func TestPatrolSeedCorrelationContextUsesCanonicalSummaryFormatter(t *testing.T)
requiredSnippets := []string{
"# Known Resource Correlations",
"correlation.FormatCorrelationSummary(c)",
- "unifiedresources.FormatResourceChangeSummary(change)",
+ "memory.ChangeFromUnifiedResourceChange(change)",
}
for _, snippet := range requiredSnippets {
if !strings.Contains(source, snippet) {
@@ -553,6 +553,34 @@ func TestIntelligenceRecentChangesUseCanonicalSummaryFormatter(t *testing.T) {
}
}
+func TestMemoryChangeConversionHelpersAreSharedAcrossAIConsumers(t *testing.T) {
+ requiredFiles := map[string][]string{
+ filepath.Join("..", "ai", "patrol_ai.go"): {
+ "memory.ChangeFromUnifiedResourceChange(change)",
+ },
+ filepath.Join("..", "ai", "intelligence.go"): {
+ "memory.ResourceChangeFromMemoryChange(change)",
+ },
+ filepath.Join("..", "ai", "memory", "presentation.go"): {
+ "func ChangeFromUnifiedResourceChange(change unifiedresources.ResourceChange) Change",
+ "func ResourceChangeFromMemoryChange(change Change) unifiedresources.ResourceChange",
+ },
+ }
+
+ for path, snippets := range requiredFiles {
+ data, err := os.ReadFile(path)
+ if err != nil {
+ t.Fatalf("failed to read %s: %v", path, err)
+ }
+ source := string(data)
+ for _, snippet := range snippets {
+ if !strings.Contains(source, snippet) {
+ t.Fatalf("%s must pin canonical memory conversion snippet %q", path, snippet)
+ }
+ }
+ }
+}
+
func TestResourcePresentationsUseSharedDurationHelper(t *testing.T) {
requiredFiles := []string{
"change_presentation.go",