From df5bd3b2ac8f4adc2ab1da664b27baa1bd320cb3 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 23 Nov 2026 09:44:22 +0000 Subject: [PATCH] fix: keep unavailable-clusters notice when list_events truncates boundAndSortEvents replaced the resource version with the truncation notice, dropping the unavailable-clusters notice set by the panel fan-out. Append it after the truncation notice instead. Co-Authored-By: Claude Opus 5.5 Signed-off-by: Claude --- main_test.go | 34 ++++++++++++++++++++++++++++++++++ tools.go | 13 ++++++++++++- 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/main_test.go b/main_test.go index e0af3b3..b58a05a 100644 --- a/main_test.go +++ b/main_test.go @@ -456,6 +456,40 @@ func TestBoundAndSortEvents_TruncatesAndNotifies(t *testing.T) { } } +func TestBoundAndSortEvents_TruncationKeepsUnavailableNotice(t *testing.T) { + now := time.Now() + build := func(rv string) *corev1.EventList { + list := &corev1.EventList{Items: make([]corev1.Event, 0, 150), ListMeta: metav1.ListMeta{ResourceVersion: rv}} + for i := 0; i < 150; i++ { + list.Items = append(list.Items, corev1.Event{ + ObjectMeta: metav1.ObjectMeta{Name: fmt.Sprintf("ev%d", i), Namespace: "games"}, + LastTimestamp: metav1.NewTime(now.Add(time.Duration(-i) * time.Second)), + Reason: "Test", + }) + } + return list + } + + t.Run("unavailable notice kept", func(t *testing.T) { + bounded := boundAndSortEvents(build("unavailable clusters: east, west")) + if len(bounded.Items) != 100 { + t.Fatalf("want 100 items after bounding, got %d", len(bounded.Items)) + } + want := "truncated (showing newest 100 of 150 events); unavailable clusters: east, west" + if bounded.ResourceVersion != want { + t.Errorf("want resourceVersion %q, got %q", want, bounded.ResourceVersion) + } + }) + + t.Run("plain resource version replaced", func(t *testing.T) { + bounded := boundAndSortEvents(build("12345")) + want := "truncated (showing newest 100 of 150 events)" + if bounded.ResourceVersion != want { + t.Errorf("want resourceVersion %q, got %q", want, bounded.ResourceVersion) + } + }) +} + func TestBoundAndSortEvents_NoTruncationWhenUnderLimit(t *testing.T) { now := time.Now() list := &corev1.EventList{ diff --git a/tools.go b/tools.go index 3e8a752..8f19648 100644 --- a/tools.go +++ b/tools.go @@ -13,6 +13,7 @@ import ( "encoding/json" "fmt" "sort" + "strings" "time" corev1 "k8s.io/api/core/v1" @@ -26,6 +27,11 @@ import ( // maxListEvents is the maximum number of events to include in list responses. const maxListEvents = 100 +// unavailableClustersPrefix starts the resource version the panel client sets +// when some clusters could not be read (internal/panel/fanout.go). Truncation +// keeps that notice so the warning is not lost. +const unavailableClustersPrefix = "unavailable clusters: " + // registeredToolNames lists every tool this server installs, in // registration order. main_test.go uses it to assert the read-only // invariant (no name/description implying a mutating verb) without needing @@ -280,6 +286,7 @@ func filterEventsByLabel(list *corev1.EventList, selector string) (*corev1.Event // bounds the list to maxListEvents. If the list is truncated, it mutates // the list metadata to include a truncation notice in the resourceVersion // field, since MCP text responses don't have a structured place for metadata. +// An existing unavailable-clusters notice is kept after the truncation notice. // Returns a DeepCopy to avoid mutating the input. func boundAndSortEvents(list *corev1.EventList) *corev1.EventList { out := list.DeepCopy() @@ -293,7 +300,11 @@ func boundAndSortEvents(list *corev1.EventList) *corev1.EventList { // Bound to maxListEvents and set truncation notice if needed if len(out.Items) > maxListEvents { out.Items = out.Items[:maxListEvents] - out.ResourceVersion = fmt.Sprintf("truncated (showing newest %d of %d events)", maxListEvents, len(list.Items)) + notice := fmt.Sprintf("truncated (showing newest %d of %d events)", maxListEvents, len(list.Items)) + if strings.HasPrefix(list.ResourceVersion, unavailableClustersPrefix) { + notice += "; " + list.ResourceVersion + } + out.ResourceVersion = notice } return out