Files
Pulse/internal/ai/attention_performance_test.go
rcourtman 1120502a1d Name Patrol inbox alert subjects by their unified resource
The Patrol inbox passed each alert's own resource reference through as the
item subject: docker:<host>/<container> for a Docker container,
<instance>:<node>:<vmid> for a guest, agent:<host>/disk:<label> for a
filesystem. Patrol findings, protection postures and resource actions carry
the unified resource ID instead, so "Browse Patrol findings for this
resource" opened an empty list, postures never attached, and the alert
detail said Patrol had raised no finding while it had one on that exact
container.

The attention projection now resolves each record's subject through the
org registry's alert reference resolver and names the unified resource. The
operational record keeps the alert's reference, and a reference inventory
cannot bind stays as before. The resolver gains Docker container references
with a complete ID, the key RecordChange already joins their history to, so
container alerts resolve like every other kind and alert-triggered Patrol
scopes for them keep the triggering alert. It also reads source targets
from the registry's view cache rather than scanning every mapping per
reference, which a page of alerts would otherwise multiply.

The alert-mirror reconcile compared the same mismatched strings and never
folded a finding under its alert; it now matches either identity. A
filesystem, sensor or ZFS pool alert resolves only to the owner its siblings
share, so the resolver marks it as a component and the reconcile does not
match on that owner. The inbox links a finding to the exact alert it
restates by the stamped alert ID, not by resource and kind, which several
alerts on one host share. The restart offer checks evidence against the
record's own reference, since the item subject is now the unified container
its registry lookup needs.

When no finding restates the alert but Patrol has open findings on the same
resource, the detail names them instead of saying Patrol raised nothing,
and the browse button appears only when there is something to browse.
2026-10-24 09:46:33 +01:00

71 lines
2.4 KiB
Go

package ai
import (
"fmt"
"strings"
"testing"
"time"
"github.com/rcourtman/pulse-go-rewrite/internal/alerts"
"github.com/rcourtman/pulse-go-rewrite/internal/models"
"github.com/rcourtman/pulse-go-rewrite/internal/operationaltrust"
"github.com/rcourtman/pulse-go-rewrite/internal/unifiedresources"
)
func TestAttentionProjectionLargeCardinalityKeepsResponseBounded(t *testing.T) {
const cardinality = 10_000
const guests = 2_000
now := time.Date(2026, 7, 19, 9, 0, 0, 0, time.UTC)
// Every alert names a guest by its Proxmox reference, so the projection
// resolves each subject against a populated inventory.
snapshot := models.StateSnapshot{VMs: make([]models.VM, guests)}
for index := range snapshot.VMs {
vmid := 100 + index
snapshot.VMs[index] = models.VM{ID: fmt.Sprintf("pve1:node1:%d", vmid), VMID: vmid, Name: fmt.Sprintf("guest-%d", vmid),
Node: "node1", Instance: "pve1", Status: "running", Type: "qemu", LastSeen: now}
}
registry := unifiedresources.NewRegistry(nil)
registry.IngestSnapshot(snapshot)
active := make([]alerts.Alert, cardinality)
for index := range active {
active[index] = attentionTestAlert(
fmt.Sprintf("record-%05d", index),
operationaltrust.OperationalOpen,
operationaltrust.SeverityWarning,
now.Add(-time.Duration(index)*time.Second),
now,
)
reference := snapshot.VMs[index%guests].ID
active[index].ResourceID = reference
active[index].OperationalRecord.SubjectResourceID = reference
}
startedAt := time.Now()
projection := ProjectAttentionItems(active, nil, nil, registry, now)
elapsed := time.Since(startedAt)
if projection.Summary.ActiveCount != cardinality {
t.Fatalf(
"ActiveCount = %d, want %d",
projection.Summary.ActiveCount,
cardinality,
)
}
for _, detail := range projection.Details {
if !strings.HasPrefix(detail.Item.SubjectResourceID, "vm-") {
t.Fatalf("subject %q was not resolved to its unified guest", detail.Item.SubjectResourceID)
}
}
page, err := PaginateAttentionDetails(projection.Details, 1, MaxAttentionPageSize)
if err != nil {
t.Fatalf("PaginateAttentionDetails(): %v", err)
}
if len(page) != MaxAttentionPageSize {
t.Fatalf("page size = %d, want %d", len(page), MaxAttentionPageSize)
}
// This is deliberately generous enough for shared CI while still catching
// accidental quadratic projection or an external request per item.
if elapsed > 5*time.Second {
t.Fatalf("10k attention projection took %s, want <= 5s", elapsed)
}
}