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.
Go aialertsdashboarddockerdocker-monitoringgohomelabinfrastructure-monitoringkubernetesmonitoringmspproxmoxproxmox-backup-serverproxmox-mail-gatewayproxmox-veself-hostedtruenastypescriptvspherewebhooks
Reduce repeated work in connected dashboard broadcasts
pulse-triage[bot]ee8b88df697d
diff --git a/internal/unifiedresources/registry.go b/internal/unifiedresources/registry.goindex da148d3ef5..e1507e031b 100644--- a/internal/unifiedresources/registry.go+++ b/internal/unifiedresources/registry.go@@ -5827,9 +5827,14 @@ func exclusionKey(a, b string) string {// Stable ordering helper for deterministic output.func sortResourcesByName(resources []Resource) {- sort.SliceStable(resources, func(i, j int) bool {- return CompareResourcesByCanonicalName(resources[i], resources[j]) < 0- })+ if len(resources) < 2 {+ return+ }+ keys := make([]resourceNameSortKey, len(resources))+ for i := range resources {+ keys[i] = resourceNameSortKey{canonicalResourceNameKey(resources[i].Name), resources[i].Type, resources[i].ID}+ }+ sort.Stable(resourceNameSort{resources, keys})}type namedResourceView interface {@@ -5838,9 +5843,14 @@ type namedResourceView interface {}func sortNamedResourceViewsByName[T namedResourceView](views []T) {- sort.SliceStable(views, func(i, j int) bool {- return compareResourceNameIdentity(views[i].Name(), "", views[i].ID(), views[j].Name(), "", views[j].ID()) < 0- })+ if len(views) < 2 {+ return+ }+ keys := make([]resourceNameSortKey, len(views))+ for i := range views {+ keys[i] = resourceNameSortKey{name: canonicalResourceNameKey(views[i].Name()), id: views[i].ID()}+ }+ sort.Stable(namedResourceSort[T]{views, keys})}// ---------------------------------------------------------------------------diff --git a/internal/unifiedresources/sort_keys_test.go b/internal/unifiedresources/sort_keys_test.gonew file mode 100644index 0000000000..df16775e0b--- /dev/null+++ b/internal/unifiedresources/sort_keys_test.go@@ -0,0 +1,69 @@+package unifiedresources++import (+ "fmt"+ "math/rand"+ "reflect"+ "sort"+ "testing"+)++func referenceSortResources(rows []Resource) {+ sort.SliceStable(rows, func(i, j int) bool { return CompareResourcesByCanonicalName(rows[i], rows[j]) < 0 })+}+func sortKeyFixture(count int) []Resource {+ r := rand.New(rand.NewSource(2199))+ rows := make([]Resource, count)+ names := []string{" Mixed-Case ", "mixed-case", "Σίσυφος", " İSTANBUL ", "", "ZZZ", "aaa"}+ for i := range rows {+ rows[i] = Resource{ID: fmt.Sprintf("%d", r.Intn(count/2+1)), Name: names[r.Intn(len(names))], Type: ResourceType(fmt.Sprintf("kind-%d", r.Intn(4))), Uptime: int64(i)}+ }+ return rows+}+func TestPrecomputedResourceSortMatchesCanonicalStableOrder(t *testing.T) {+ for _, count := range []int{0, 1, 2, 17, 1236} {+ rows := sortKeyFixture(count)+ want := append([]Resource(nil), rows...)+ referenceSortResources(want)+ sortResourcesByName(rows)+ if !reflect.DeepEqual(rows, want) && len(rows) > 0 {+ t.Fatalf("canonical/stable order changed at size %d", count)+ }+ }+}++type sortTestView struct {+ name, id string+ sequence int+}++func (v sortTestView) Name() string { return v.name }+func (v sortTestView) ID() string { return v.id }+func TestPrecomputedViewSortMatchesCanonicalStableOrder(t *testing.T) {+ rows := []sortTestView{{" B ", "z", 0}, {"b", "a", 1}, {"b", "a", 2}, {"A", "c", 3}, {"", "d", 4}, {"İ", "e", 5}}+ want := append([]sortTestView(nil), rows...)+ sort.SliceStable(want, func(i, j int) bool {+ return compareResourceNameIdentity(want[i].Name(), "", want[i].ID(), want[j].Name(), "", want[j].ID()) < 0+ })+ sortNamedResourceViewsByName(rows)+ if !reflect.DeepEqual(rows, want) {+ t.Fatalf("view order changed: %v", rows)+ }+}+func BenchmarkCanonicalResourceSort(b *testing.B) {+ seed := sortKeyFixture(1236)+ for _, impl := range []struct {+ name string+ sort func([]Resource)+ }{{"reference", referenceSortResources}, {"precomputed", sortResourcesByName}} {+ b.Run(impl.name, func(b *testing.B) {+ rows := make([]Resource, len(seed))+ b.ReportAllocs()+ b.ResetTimer()+ for i := 0; i < b.N; i++ {+ copy(rows, seed)+ impl.sort(rows)+ }+ })+ }+}diff --git a/internal/unifiedresources/sort_order.go b/internal/unifiedresources/sort_order.goindex afc1bc050e..21727d3e77 100644--- a/internal/unifiedresources/sort_order.go+++ b/internal/unifiedresources/sort_order.go@@ -28,3 +28,45 @@ func compareResourceNameIdentity(func CompareResourcesByCanonicalName(a, b Resource) int {return compareResourceNameIdentity(a.Name, a.Type, a.ID, b.Name, b.Type, b.ID)}++// Per-sort keys avoid normalising mixed-case names O(n log n) times and+// copying a large Resource into every comparison. Keys move with their rows.+type resourceNameSortKey struct {+ name string+ kind ResourceType+ id string+}++func (k resourceNameSortKey) less(other resourceNameSortKey) bool {+ if k.name != other.name {+ return k.name < other.name+ }+ if k.kind != other.kind {+ return k.kind < other.kind+ }+ return k.id < other.id+}++type resourceNameSort struct {+ rows []Resource+ keys []resourceNameSortKey+}++func (s resourceNameSort) Len() int { return len(s.rows) }+func (s resourceNameSort) Less(i, j int) bool { return s.keys[i].less(s.keys[j]) }+func (s resourceNameSort) Swap(i, j int) {+ s.rows[i], s.rows[j] = s.rows[j], s.rows[i]+ s.keys[i], s.keys[j] = s.keys[j], s.keys[i]+}++type namedResourceSort[T namedResourceView] struct {+ rows []T+ keys []resourceNameSortKey+}++func (s namedResourceSort[T]) Len() int { return len(s.rows) }+func (s namedResourceSort[T]) Less(i, j int) bool { return s.keys[i].less(s.keys[j]) }+func (s namedResourceSort[T]) Swap(i, j int) {+ s.rows[i], s.rows[j] = s.rows[j], s.rows[i]+ s.keys[i], s.keys[j] = s.keys[j], s.keys[i]+}diff --git a/internal/websocket/broadcast_projection_export_test.go b/internal/websocket/broadcast_projection_export_test.goindex 8bb71f947e..9869b296d6 100644--- a/internal/websocket/broadcast_projection_export_test.go+++ b/internal/websocket/broadcast_projection_export_test.go@@ -33,6 +33,35 @@ func NewBroadcastProjectionProbeForTest(getState func(string) interface{}, recip}, nil}+// NewQuietBroadcastProjectionProbeForTest also admits an actually quiet delta.+func NewQuietBroadcastProjectionProbeForTest(getState func(string) interface{}, recipients int) (func() (int, error), error) {+ hub := NewHub(getState)+ clients := make([]*Client, recipients)+ for i := range clients {+ client := &Client{hub: hub, orgID: "default", send: make(chan []byte, 1)}+ if _, sent, err := client.queueFullState("initialState", getState("default")); err != nil || !sent {+ return nil, fmt.Errorf("initial state: sent=%v error=%v", sent, err)+ }+ <-client.send+ clients[i] = client+ hub.clients[client] = true+ }+ return func() (int, error) {+ hub.dispatchStateBroadcast(&Message{Type: "rawData", Data: stateBroadcastRequest{}}, "")+ bytes := 0+ for _, client := range clients {+ select {+ case data := <-client.send:+ bytes += len(data)+ default:+ // An unchanged snapshot queues no frame.+ continue+ }+ }+ return bytes, nil+ }, nil+}+// NewBroadcastProjectionCaptureForTest retains the actual per-client frames,// including a nil frame for a quiet or unrelated tenant. It runs the production// current-state getter, projection, delta baseline and queue path without timersdiff --git a/internal/websocket/connected_cost_test.go b/internal/websocket/connected_cost_test.gonew file mode 100644index 0000000000..485160259c--- /dev/null+++ b/internal/websocket/connected_cost_test.go@@ -0,0 +1,90 @@+package websocket_test++import (+ "fmt"+ "testing"+ "time"++ "github.com/rcourtman/pulse-go-rewrite/internal/models"+ "github.com/rcourtman/pulse-go-rewrite/internal/monitoring"+ "github.com/rcourtman/pulse-go-rewrite/internal/unifiedresources"+ "github.com/rcourtman/pulse-go-rewrite/internal/websocket"+)++// Source-native mixed estate, close to #2199's reported resource/Kubernetes+// scale. Completed ingest is outside the read measurement, as in the existing+// broadcast probe. Transport, compression and installed CPU are not measured.+func connectedCostMonitor() *monitoring.Monitor {+ now := time.Unix(1791283200, 0).UTC()+ seed := models.StateSnapshot{LastUpdate: now}+ cluster := models.KubernetesCluster{ID: "cost-cluster", Name: "cost-cluster", Status: "online", LastSeen: now}+ for i := 0; i < 1000; i++ {+ cluster.Pods = append(cluster.Pods, models.KubernetesPod{UID: fmt.Sprintf("pod-%04d", i), Name: fmt.Sprintf(" API-Workload-%04d ", 1000-i), Namespace: "workloads", Phase: "Running", UsageCPUPercent: 12.5, UsageMemoryBytes: 9007199254740993, Labels: map[string]string{"app": "api", "team": "ops", "env": "prod", "description": "unchanged source metadata<&>"}, Containers: []models.KubernetesPodContainer{{Name: "api", Ready: true}}})+ }+ seed.KubernetesClusters = []models.KubernetesCluster{cluster}+ for i := 0; i < 235; i++ {+ seed.VMs = append(seed.VMs, models.VM{ID: fmt.Sprintf("lab:pve-%d:%d", i%8, i+100), VMID: i + 100, Name: fmt.Sprintf(" MixedCase-VM-%04d ", 235-i), Node: fmt.Sprintf("pve-%d", i%8), Instance: "lab", Type: "qemu", Status: "running", CPU: 0.1, Memory: models.Memory{Total: 4 << 30, Used: 1 << 30}, LastSeen: now})+ }+ registry := unifiedresources.NewRegistry(nil)+ registry.IngestSnapshot(seed)+ monitor := &monitoring.Monitor{}+ monitor.SetResourceStore(&broadcastReadStore{unifiedresources.NewMonitorAdapter(registry)})+ return monitor+}+func BenchmarkConnectedDashboardCost(b *testing.B) {+ for _, mode := range []string{"cold", "quiet", "metric", "heartbeat", "churn"} {+ for _, viewers := range []int{0, 1, 4, 17} {+ b.Run(fmt.Sprintf("%s/viewers-%d", mode, viewers), func(b *testing.B) {+ monitor := connectedCostMonitor()+ sequence := int64(0)+ getter := func(string) interface{} {+ state := monitor.BuildBroadcastFrontendState()+ sequence+++ switch mode {+ case "metric":+ state.LastUpdate = sequence+ state.Resources[0].CPU = &models.ResourceMetricFrontend{Current: float64(sequence % 100)}+ case "heartbeat":+ state.LastUpdate = sequence+ for i := range state.Resources {+ state.Resources[i].LastSeen = sequence+ }+ case "churn":+ state.LastUpdate = sequence+ state.Resources = state.Resources[:len(state.Resources)-int(sequence%2)]+ state.Resources[0].Labels = map[string]string{"changed": fmt.Sprint(sequence)}+ state.Resources[0].DisplayName = fmt.Sprint(sequence)+ state.Resources[0], state.Resources[len(state.Resources)-1] = state.Resources[len(state.Resources)-1], state.Resources[0]+ }+ return state+ }+ run, err := websocket.NewQuietBroadcastProjectionProbeForTest(getter, viewers)+ if err != nil {+ b.Fatal(err)+ }+ // Shared accepted baselines, not independent initial-state snapshots.+ if mode != "quiet" {+ if _, err = run(); err != nil {+ b.Fatal(err)+ }+ }+ b.ReportAllocs()+ b.ResetTimer()+ wire := 0+ for i := 0; i < b.N; i++ {+ if mode == "cold" {+ run, err = websocket.NewQuietBroadcastProjectionProbeForTest(getter, viewers)+ if err != nil {+ b.Fatal(err)+ }+ }+ wire, err = run()+ if err != nil {+ b.Fatal(err)+ }+ }+ b.ReportMetric(float64(wire), "wire-B/op")+ })+ }+ }+}diff --git a/internal/websocket/delta_dispatch_test.go b/internal/websocket/delta_dispatch_test.gonew file mode 100644index 0000000000..bab482c409--- /dev/null+++ b/internal/websocket/delta_dispatch_test.go@@ -0,0 +1,64 @@+package websocket++import (+ "bytes"+ "encoding/json"+ "testing"++ "github.com/rcourtman/pulse-go-rewrite/internal/models"+)++func TestStateDeltaDispatchSharesOnlyAcceptedBaseline(t *testing.T) {+ before, err := buildClientStateSnapshot(benchmarkFrontendState(2))+ if err != nil {+ t.Fatal(err)+ }+ state := benchmarkFrontendState(2)+ state.Resources[0].LastSeen = 9007199254740993+ current, err := buildClientStateSnapshot(state)+ if err != nil {+ t.Fatal(err)+ }+ cache := make(stateDeltaDispatch)+ first := &Client{stateSnapshot: before, send: make(chan []byte, 1)}+ second := &Client{stateSnapshot: before, send: make(chan []byte, 1)}+ blocked := &Client{stateSnapshot: before, send: make(chan []byte, 1)}+ blocked.send <- []byte("already queued")+ separate, err := buildClientStateSnapshot(benchmarkFrontendState(2))+ if err != nil {+ t.Fatal(err)+ }+ reconnect := &Client{stateSnapshot: separate, send: make(chan []byte, 1)}+ waiting := &Client{send: make(chan []byte, 1)}+ for _, c := range []*Client{first, second, reconnect} {+ if _, attempted, sent, err := c.queueStateDeltaForDispatch(current, cache); err != nil || !attempted || !sent {+ t.Fatalf("queue: %v %v %v", attempted, sent, err)+ }+ }+ a, b := <-first.send, <-second.send+ if &a[0] != &b[0] || !bytes.Contains(a, []byte(`"lastSeen":9007199254740993`)) {+ t.Fatal("accepted baseline did not share exact immutable frame")+ }+ if len(cache) != 2 {+ t.Fatalf("dispatch cached %d baselines, want shared and rehydrated only", len(cache))+ }+ if _, attempted, sent, err := blocked.queueStateDeltaForDispatch(current, cache); err != nil || !attempted || sent || blocked.stateSnapshot != before {+ t.Fatal("failed send advanced baseline")+ }+ if _, attempted, _, err := waiting.queueStateDeltaForDispatch(current, cache); err != nil || attempted || waiting.stateSnapshot != nil {+ t.Fatal("unhydrated client adopted another baseline")+ }+ if _, attempted, sent, err := first.queueStateDeltaForDispatch(current, make(stateDeltaDispatch)); err != nil || attempted || !sent {+ t.Fatal("quiet dispatch re-sent stale frame")+ }+ // Each client still applies its own frame limit before send/adoption.+ limited := &Client{stateSnapshot: before, send: make(chan []byte, 1)}+ limited.maxInboundBytes.Store(1)+ if _, attempted, sent, err := limited.queueStateDeltaForDispatch(current, cache); err != nil || !attempted || !sent {+ t.Fatalf("limit: %v %v %v", attempted, sent, err)+ }+ var marker Message+ if err := json.Unmarshal(<-limited.send, &marker); err != nil || marker.Type != stateTooLargeMessageType || limited.stateSnapshot != nil || !limited.restRecovery {+ t.Fatal("shared frame bypassed REST recovery limit")+ }+}diff --git a/internal/websocket/hub.go b/internal/websocket/hub.goindex 0b2dd1a396..04efdaa2e8 100644--- a/internal/websocket/hub.go+++ b/internal/websocket/hub.go@@ -416,7 +416,34 @@ func (c *Client) queueStateTooLargeLocked(return payloadBytes, true, nil}+type preparedStateDelta struct {+ data []byte+ err error+}++func prepareStateDelta(previous, current *clientStateSnapshot) preparedStateDelta {+ delta, err := buildClientStateDelta(previous, current)+ if err != nil {+ return preparedStateDelta{err: err}+ }+ if len(delta) == 0 {+ return preparedStateDelta{}+ }+ data, err := json.Marshal(Message{Type: "rawData", Data: delta})+ return preparedStateDelta{data: data, err: err}+}++// This map belongs to one serial dispatch, never the Hub or a tenant cache.+// Pointer identity identifies an actually accepted immutable snapshot, not a+// hash or resource ID. A reconnect/re-hydration gets its own baseline.+type stateDeltaDispatch map[*clientStateSnapshot]preparedStateDelta+func (c *Client) queueStateDelta(current *clientStateSnapshot) (int, bool, bool, error) {+ return c.queueStateDeltaForDispatch(current, nil)+}++func (c *Client) queueStateDeltaForDispatch(current *clientStateSnapshot, dispatch stateDeltaDispatch) (int, bool, bool, error) {+if current == nil {return 0, false, false, fmt.Errorf("current state snapshot is nil")}@@ -438,18 +465,21 @@ func (c *Client) queueStateDelta(current *clientStateSnapshot) (int, bool, bool,return 0, false, false, nil}- delta, err := buildClientStateDelta(c.stateSnapshot, current)- if err != nil {- return 0, true, false, err+ prepared, found := dispatch[c.stateSnapshot]+ if !found {+ prepared = prepareStateDelta(c.stateSnapshot, current)+ if dispatch != nil {+ dispatch[c.stateSnapshot] = prepared+ }}- if len(delta) == 0 {+ if prepared.err != nil {+ return 0, true, false, prepared.err+ }+ if len(prepared.data) == 0 {c.stateSnapshot = currentreturn 0, false, true, nil}- data, err := json.Marshal(Message{Type: "rawData", Data: delta})- if err != nil {- return 0, true, false, err- }+ data := prepared.dataif limit := c.inboundLimitBytes(); limit > 0 && int64(len(data)) > limit {dataLen, sent, markerErr := c.queueStateTooLargeLocked("rawData", current, len(data), limit)return dataLen, true, sent, markerErr@@ -1322,8 +1352,9 @@ func (h *Hub) dispatchCurrentStateSnapshot(state interface{}, orgID string) {}h.mu.RUnlock()+ dispatch := make(stateDeltaDispatch)for _, client := range clients {- dataLen, attempted, sent, queueErr := client.queueStateDelta(snapshot)+ dataLen, attempted, sent, queueErr := client.queueStateDeltaForDispatch(snapshot, dispatch)if queueErr != nil {log.Error().Err(queueErr).Str("client", client.id).Str("org_id", client.orgID).Msg("Failed to marshal WebSocket state delta")continuediff --git a/internal/websocket/merge_patch_reference_test.go b/internal/websocket/merge_patch_reference_test.gonew file mode 100644index 0000000000..4653be438b--- /dev/null+++ b/internal/websocket/merge_patch_reference_test.go@@ -0,0 +1,91 @@+package websocket++import (+ "bytes"+ "encoding/json"+ "fmt"+ "io"+ "reflect"+)++// Exact d14a3891 pre-change implementation, independent of the new raw-object path.+func referenceJSONMergePatch(previous, current json.RawMessage) (json.RawMessage, error) {+ previousValue, err := referenceDecodeJSONMergeValue(previous)+ if err != nil {+ return nil, err+ }+ currentValue, err := referenceDecodeJSONMergeValue(current)+ if err != nil {+ return nil, err+ }++ patchValue, changed := referenceDiffJSONMergeValue(previousValue, currentValue)+ if !changed {+ return json.RawMessage(`{}`), nil+ }+ if patchObject, ok := patchValue.(map[string]interface{}); ok {+ if currentObject, ok := currentValue.(map[string]interface{}); ok {+ if id, ok := currentObject["id"]; ok {+ patchObject["id"] = id+ }+ }+ }+ patch, err := json.Marshal(patchValue)+ if err != nil {+ return nil, err+ }+ return patch, nil+}++// A delta must preserve the authoritative encoded counters, not round them+// through float64. Otherwise adjacent int64/uint64 readings above 2^53 can+// compare equal, and a changed nested value/array can be sent with altered+// numbers. Keep number tokens exact while retaining the existing merge rules.+func referenceDecodeJSONMergeValue(encoded json.RawMessage) (interface{}, error) {+ decoder := json.NewDecoder(bytes.NewReader(encoded))+ decoder.UseNumber()+ var value interface{}+ if err := decoder.Decode(&value); err != nil {+ return nil, err+ }+ // Decode accepts a stream by default. Preserve Unmarshal's one-value+ // contract, including rejection of a valid prefix followed by more JSON.+ var trailing interface{}+ if err := decoder.Decode(&trailing); err != io.EOF {+ if err == nil {+ err = fmt.Errorf("multiple JSON merge values")+ }+ return nil, err+ }+ return value, nil+}++func referenceDiffJSONMergeValue(previous, current interface{}) (interface{}, bool) {+ if reflect.DeepEqual(previous, current) {+ return nil, false+ }++ previousObject, previousIsObject := previous.(map[string]interface{})+ currentObject, currentIsObject := current.(map[string]interface{})+ if !previousIsObject || !currentIsObject {+ return current, true+ }++ patch := make(map[string]interface{})+ for key := range previousObject {+ if _, exists := currentObject[key]; !exists {+ patch[key] = nil+ }+ }+ for key, currentValue := range currentObject {+ previousValue, exists := previousObject[key]+ if !exists {+ patch[key] = currentValue+ continue+ }+ if nestedPatch, changed := referenceDiffJSONMergeValue(previousValue, currentValue); changed {+ patch[key] = nestedPatch+ }+ }+ return patch, len(patch) > 0+}diff --git a/internal/websocket/raw_merge_patch_test.go b/internal/websocket/raw_merge_patch_test.gonew file mode 100644index 0000000000..39a6f5931e--- /dev/null+++ b/internal/websocket/raw_merge_patch_test.go@@ -0,0 +1,62 @@+package websocket++import (+ "encoding/json"+ "fmt"+ "math/rand"+ "reflect"+ "testing"+)++func assertRawMergeMatchesReference(t testing.TB, previous, current []byte) {+ t.Helper()+ want, wantErr := referenceJSONMergePatch(previous, current)+ got, gotErr := createJSONMergePatch(previous, current)+ if (wantErr == nil) != (gotErr == nil) {+ t.Fatalf("error mismatch: before=%q after=%q reference=%v raw=%v", previous, current, wantErr, gotErr)+ }+ if wantErr == nil && !reflect.DeepEqual(decodeExactDeltaJSON(t, want), decodeExactDeltaJSON(t, got)) {+ t.Fatalf("patch mismatch: before=%s after=%s reference=%s raw=%s", previous, current, want, got)+ }+}++func TestRawMergePatchSemanticEquivalence(t *testing.T) {+ cases := []string{`null`, `true`, `false`, `0`, `1.0`, `1e0`, `9007199254740993`, `18446744073709551615`, `"<&>"`, `"\u003c\u0026\u003e"`, `[]`, `[null,1.0,{"z":2,"a":1}]`, `[null,1.0,{"a":1,"z":2}]`, `{}`, `{"id":"r","nested":{"id":"child","n":9007199254740993}}`, `{"nested":{"n":9007199254740993,"id":"child"},"id":"r"}`, `{"id":"r","nested":{"id":"changed","n":9007199254740994}}`, `{"id":"old","id":"r","nested":{"a":1,"a":2}}`, `{"id":"r","nested":{"a":2}}`, `{"id":null,"optional":null}`, `{"id":"r","optional":[]}`, `{"id":"r","optional":{}}`}+ for i, a := range cases {+ for j, b := range cases {+ t.Run(fmt.Sprintf("%d-%d", i, j), func(t *testing.T) { assertRawMergeMatchesReference(t, []byte(a), []byte(b)) })+ }+ }+ // Identical invalid bytes may not bypass validation.+ for _, invalid := range []string{``, `{"a":`, `{"a":1} {}`, `[1,]`, `NaN`} {+ assertRawMergeMatchesReference(t, []byte(invalid), []byte(invalid))+ }+}++func randomMergeValue(r *rand.Rand, depth int) any {+ if depth == 0 {+ return []any{nil, true, false, "same<&>", "changed", json.Number("9007199254740993"), json.Number("1.0"), json.Number("1e0")}[r.Intn(8)]+ }+ switch r.Intn(3) {+ case 0:+ return randomMergeValue(r, 0)+ case 1:+ return []any{randomMergeValue(r, depth-1), randomMergeValue(r, depth-1)}+ default:+ return map[string]any{"id": "r", "a": randomMergeValue(r, depth-1), "b": randomMergeValue(r, depth-1)}+ }+}+func TestRawMergePatchRandomEquivalence(t *testing.T) {+ r := rand.New(rand.NewSource(2199))+ for i := 0; i < 1000; i++ {+ a, _ := json.Marshal(randomMergeValue(r, 3))+ b, _ := json.Marshal(randomMergeValue(r, 3))+ assertRawMergeMatchesReference(t, a, b)+ }+}+func FuzzRawMergePatchEquivalence(f *testing.F) {+ f.Add([]byte(`{"id":"r","a":{"b":1},"c":null}`), []byte(`{"id":"r","a":{"b":2},"c":[]}`))+ f.Add([]byte(`{"id":"r","x":18446744073709551615}`), []byte(`{"id":"r","x":9007199254740993}`))+ f.Add([]byte(`{"id":"a","id":"r"}`), []byte(`{"id":"r"}`))+ f.Fuzz(func(t *testing.T, a, b []byte) { assertRawMergeMatchesReference(t, a, b) })+}diff --git a/internal/websocket/state_delta.go b/internal/websocket/state_delta.goindex 592f7cb529..8d638fc928 100644--- a/internal/websocket/state_delta.go+++ b/internal/websocket/state_delta.go@@ -300,34 +300,101 @@ func buildClientStateDelta(previous, current *clientStateSnapshot) (map[string]ireturn delta, nil}+// Work on encoded object fields first. Most dashboard changes are a heartbeat+// or one metric beside a large unchanged metadata object; decoding all nested+// values just to discover equality costs far more than comparing owned bytes.+// Non-identical leaves still use the exact-number semantic comparison below.func createJSONMergePatch(previous, current json.RawMessage) (json.RawMessage, error) {- previousValue, err := decodeJSONMergeValue(previous)- if err != nil {- return nil, err+ // Even equal inputs must be valid single JSON values. Snapshot producers+ // validate their encodings, but this helper also has standalone callers.+ if !json.Valid(previous) || !json.Valid(current) {+ return nil, fmt.Errorf("invalid JSON merge value")}- currentValue, err := decodeJSONMergeValue(current)+ patch, changed, err := diffRawJSONMergeValue(previous, current)if err != nil {return nil, err}-- patchValue, changed := diffJSONMergeValue(previousValue, currentValue)if !changed {return json.RawMessage(`{}`), nil}- if patchObject, ok := patchValue.(map[string]interface{}); ok {- if currentObject, ok := currentValue.(map[string]interface{}); ok {- if id, ok := currentObject["id"]; ok {- patchObject["id"] = id+ // Preserve the existing identity anchor for changed object entries.+ if rawJSONObject(current) && rawJSONObject(patch) {+ var object, patchObject map[string]json.RawMessage+ if err := json.Unmarshal(current, &object); err != nil {+ return nil, err+ }+ if id, ok := object["id"]; ok {+ if err := json.Unmarshal(patch, &patchObject); err != nil {+ return nil, err}+ patchObject["id"] = id+ return json.Marshal(patchObject)}}- patch, err := json.Marshal(patchValue)- if err != nil {- return nil, err- }return patch, nil}+func rawJSONObject(value json.RawMessage) bool {+ trimmed := bytes.TrimSpace(value)+ return len(trimmed) > 0 && trimmed[0] == '{'+}++// Inputs here are validated by createJSONMergePatch. Raw equality is only a+// fast positive match, never a content hash, source-ID hint or freshness test.+func diffRawJSONMergeValue(previous, current json.RawMessage) (json.RawMessage, bool, error) {+ if bytes.Equal(previous, current) {+ return nil, false, nil+ }+ if !rawJSONObject(previous) || !rawJSONObject(current) {+ before, err := decodeJSONMergeValue(previous)+ if err != nil {+ return nil, false, err+ }+ after, err := decodeJSONMergeValue(current)+ if err != nil {+ return nil, false, err+ }+ patch, changed := diffJSONMergeValue(before, after)+ if !changed {+ return nil, false, nil+ }+ encoded, err := json.Marshal(patch)+ return encoded, true, err+ }+ var before, after map[string]json.RawMessage+ if err := json.Unmarshal(previous, &before); err != nil {+ return nil, false, err+ }+ if err := json.Unmarshal(current, &after); err != nil {+ return nil, false, err+ }+ patch := make(map[string]json.RawMessage)+ for key := range before {+ if _, exists := after[key]; !exists {+ patch[key] = json.RawMessage(`null`)+ }+ }+ for key, value := range after {+ old, exists := before[key]+ if !exists {+ patch[key] = value+ continue+ }+ nested, changed, err := diffRawJSONMergeValue(old, value)+ if err != nil {+ return nil, false, err+ }+ if changed {+ patch[key] = nested+ }+ }+ if len(patch) == 0 {+ return nil, false, nil+ }+ encoded, err := json.Marshal(patch)+ return encoded, true, err+}+// A delta must preserve the authoritative encoded counters, not round them// through float64. Otherwise adjacent int64/uint64 readings above 2^53 can// compare equal, and a changed nested value/array can be sent with altered