From 774f8376be863815fe79ea9d83e90a17a9833db4 Mon Sep 17 00:00:00 2001 From: Erik Darling <2136037+erikdarlingdata@users.noreply.github.com> Date: Fri, 23 Oct 2026 15:39:38 -0400 Subject: [PATCH] Say how many events were read when the health parser is truncated (#6650) --- ...ngMcpHealthParserToolsWindowNoticeTests.cs | 30 +++++++++++++++++ .../Mcp/DarlingMcpHealthParserTools.cs | 2 +- .../McpWindowNoticeSystemHealthToolTests.cs | 27 ++++++++++++++++ Lite.Tests/RingBufferTruncationSignalTests.cs | 19 +++++++++++ Lite/Mcp/McpHealthParserTools.cs | 2 +- .../RingBufferTruncationNote.cs | 32 +++++++++++++++---- 6 files changed, 104 insertions(+), 8 deletions(-) diff --git a/Darling/Darling.Tests/DarlingMcpHealthParserToolsWindowNoticeTests.cs b/Darling/Darling.Tests/DarlingMcpHealthParserToolsWindowNoticeTests.cs index cf9585341a..3b4f8e4c6b 100644 --- a/Darling/Darling.Tests/DarlingMcpHealthParserToolsWindowNoticeTests.cs +++ b/Darling/Darling.Tests/DarlingMcpHealthParserToolsWindowNoticeTests.cs @@ -311,6 +311,36 @@ await DarlingMcpTestData.ExecAsync(c, TestContext.Current.CancellationToken, Assert.DoesNotContain("quiet", message, StringComparison.Ordinal); }); + /* #6650: events of the tool's type WERE read in the window and its gate dropped them. The truncation answer must say + how many were read, not "No ... events were found" over a count of one or more. */ + [Fact] + public async Task ATruncatedRingBufferRun_WithEventsReadButGatedOut_SaysHowManyWereRead_AgainstDevPostgres() => + await RunHealthAsync("rbtrunc-read", async (c, ds, end) => + { + await SeedHealthServerAsync(c, "rbtrunc-read", end.AddDays(-30), end.AddDays(-8), end.AddMinutes(-30)); + var name = HealthName("rbtrunc-read"); + var (eventType, fixture, _) = SourceOf(HealthTool.MemoryBroker); + var xml = System.IO.File.ReadAllText(System.IO.Path.Combine(AppContext.BaseDirectory, "Fixtures", "SystemHealth", fixture)); + await DarlingMcpTestData.ExecAsync(c, TestContext.Current.CancellationToken, + @"INSERT INTO system_health_events (system_health_event_id, collection_time, server_id, server_name, event_time, event_type, event_xml) +VALUES ($1,$2,$3,$4,$5,$6,$7)", + CollectionIdGenerator.Next(), DarlingMcpTestData.Naive(end.AddDays(-1)), ServerIdHelper.GetDeterministicHashCode(name), name, + DarlingMcpTestData.Naive(end.AddDays(-1)), eventType, xml); + await DarlingMcpTestData.ExecAsync(c, TestContext.Current.CancellationToken, + @"INSERT INTO collection_log (log_id, server_id, server_name, collector_name, collection_time, duration_ms, status, rows_collected, error_message) +VALUES ($1, $2, $3, $4, $5, 12, 'SUCCESS', 0, 'ring_buffer_truncated=1 ring_buffer_events_hidden=773 newest_visible_event_age_s=19380')", + CollectionIdGenerator.Next(), ServerIdHelper.GetDeterministicHashCode(name), name, Collector, DarlingMcpTestData.Naive(end)); + + var root = WindowFloorLiveHarness.Parse(await CallHealthAsync(HealthTool.MemoryBroker, ds, "rbtrunc-read", 168, end)); + + Assert.Equal("unavailable", root.GetProperty("status").GetString()); + var message = root.GetProperty("message").GetString()!; + Assert.StartsWith($"1 {eventType} event(s) were read for {name}", message, StringComparison.Ordinal); + Assert.DoesNotContain("events were found", message, StringComparison.Ordinal); + Assert.Contains("NOT an all-clear", message, StringComparison.Ordinal); + Assert.Contains("773 event(s) are hidden", message, StringComparison.Ordinal); + }); + /* #6453: nothing of any type ever stored. The newest run read the live session through its event_file target and found nothing: a true negative, `empty`. A fallback to the ring buffer, a ring buffer read with no note and a newest run that was an ERROR keep `unavailable`, because the ring buffer path also records SUCCESS for a dead session. */ diff --git a/Darling/PerformanceMonitor.Darling.Service/Mcp/DarlingMcpHealthParserTools.cs b/Darling/PerformanceMonitor.Darling.Service/Mcp/DarlingMcpHealthParserTools.cs index 1ae6ceb26e..276245c2e9 100644 --- a/Darling/PerformanceMonitor.Darling.Service/Mcp/DarlingMcpHealthParserTools.cs +++ b/Darling/PerformanceMonitor.Darling.Service/Mcp/DarlingMcpHealthParserTools.cs @@ -896,7 +896,7 @@ private static async Task EmptyAsync( { return WitnessStatus( "unavailable", - RingBufferTruncationNote.HealthParserMessage(eventType, c.DisplayName, hoursBack, truncation), + RingBufferTruncationNote.HealthParserMessage(eventType, c.DisplayName, hoursBack, truncation, c.RawEventCount, noneQualifiedBecause), sourceObserved: true, c.LastCapturedAt, lastCapturedOfTypeAt: lastOfType, eventsInWindow: c.RawEventCount); } diff --git a/Lite.Tests/McpWindowNoticeSystemHealthToolTests.cs b/Lite.Tests/McpWindowNoticeSystemHealthToolTests.cs index e60eab8170..e29a776f39 100644 --- a/Lite.Tests/McpWindowNoticeSystemHealthToolTests.cs +++ b/Lite.Tests/McpWindowNoticeSystemHealthToolTests.cs @@ -658,6 +658,33 @@ INSERT INTO collection_log (log_id, server_id, server_name, collector_name, coll Assert.DoesNotContain("quiet", message, StringComparison.Ordinal); } + /* #6650: events of the tool's type WERE read in the window and its gate dropped them. The truncation answer must say + how many were read, not "No ... events were found" over a count of one or more. */ + [Fact] + public async Task ATruncatedRingBuffer_WithEventsReadButGatedOut_SaysHowManyWereRead_NotThatNoneWereFound() + { + await _duckDb.InitializeFromTemplateAsync(); + await SeedLogRunsAsync(Anchor.AddDays(-2), Anchor.AddMinutes(-30), everyMinutes: 30); + var (eventType, fixture, _) = SourceOf(Tool.MemoryBroker); + await ExecuteAsync(@" +INSERT INTO system_health_events (system_health_event_id, collection_time, server_id, server_name, event_time, event_type, event_xml) +VALUES ($1, $2, $3, $4, $2, $5, $6)", + _nextId++, Naive(Anchor.AddDays(-1)), _serverId, ServerName, eventType, LoadFixture(fixture)); + await ExecuteAsync(@" +INSERT INTO collection_log (log_id, server_id, server_name, collector_name, collection_time, duration_ms, status, rows_collected, error_message) +VALUES ($1, $2, $3, 'system_health_events', $4, 12, 'SUCCESS', 0, 'ring_buffer_truncated=1 ring_buffer_events_hidden=773 newest_visible_event_age_s=19380')", + _nextId++, _serverId, ServerName, Naive(Anchor)); + + var root = Root(await CallAsync(Tool.MemoryBroker)); + + Assert.Equal("unavailable", root.GetProperty("status").GetString()); + var message = root.GetProperty("message").GetString()!; + Assert.StartsWith("1 " + eventType + " event(s) were read for " + ServerName, message, StringComparison.Ordinal); + Assert.DoesNotContain("events were found", message, StringComparison.Ordinal); + Assert.Contains("NOT an all-clear", message, StringComparison.Ordinal); + Assert.Contains("773 event(s) are hidden", message, StringComparison.Ordinal); + } + /* #6441: a clean newest run after a truncated one clears the warning; the read answers as it did before. */ [Fact] public async Task ATruncatedOlderRun_ThenACleanNewestRun_KeepsTheQuietAnswer() diff --git a/Lite.Tests/RingBufferTruncationSignalTests.cs b/Lite.Tests/RingBufferTruncationSignalTests.cs index c0dcf97d2a..64af70653c 100644 --- a/Lite.Tests/RingBufferTruncationSignalTests.cs +++ b/Lite.Tests/RingBufferTruncationSignalTests.cs @@ -157,6 +157,25 @@ public void FormatCollectionNote_ATruncatedNote_SaysSoInPlainWords() Assert.Contains("773 event(s) are hidden", some, StringComparison.Ordinal); } + /* #6650: the health-parser truncation answer says how many events the read counted when it counted some, and keeps + "No ... events were found" only when it counted none. */ + [Fact] + public void HealthParserMessage_WithEventsRead_SaysHowManyAndWhyNoneQualified_NotThatNoneWereFound() + { + var truncation = RingBufferTruncationNote.Describe(TruncatedNote)!; + + var counted = RingBufferTruncationNote.HealthParserMessage( + "memory_broker", "srv", 24, truncation, eventsRead: 412, noneQualifiedBecause: "none passed the significance gate"); + + Assert.StartsWith("412 memory_broker event(s) were read for srv in the last 24 hour(s), and none passed the significance gate, and this is NOT an all-clear", counted, StringComparison.Ordinal); + Assert.DoesNotContain("were found", counted, StringComparison.Ordinal); + Assert.Contains("773 event(s) are hidden", counted, StringComparison.Ordinal); + + var none = RingBufferTruncationNote.HealthParserMessage("memory_broker", "srv", 24, truncation); + Assert.StartsWith("No memory_broker events were found for srv in the last 24 hour(s), and this is NOT an all-clear", none, StringComparison.Ordinal); + Assert.Contains("773 event(s) are hidden", none, StringComparison.Ordinal); + } + [Theory] [InlineData(45, "45 s")] [InlineData(1200, "20 min")] diff --git a/Lite/Mcp/McpHealthParserTools.cs b/Lite/Mcp/McpHealthParserTools.cs index b0ebea78cf..8f0d6e399f 100644 --- a/Lite/Mcp/McpHealthParserTools.cs +++ b/Lite/Mcp/McpHealthParserTools.cs @@ -686,7 +686,7 @@ private static async Task EmptyAsync( { return WitnessStatus( "unavailable", - RingBufferTruncationNote.HealthParserMessage(eventType, serverName, hoursBack, truncation), + RingBufferTruncationNote.HealthParserMessage(eventType, serverName, hoursBack, truncation, captured, noneQualifiedBecause), sourceObserved: true, lastCapturedAt, lastCapturedOfTypeAt: lastOfType, eventsInWindow: captured); } diff --git a/PerformanceMonitor.Common/RingBufferTruncationNote.cs b/PerformanceMonitor.Common/RingBufferTruncationNote.cs index 5a55ca0bc7..a92cc20de3 100644 --- a/PerformanceMonitor.Common/RingBufferTruncationNote.cs +++ b/PerformanceMonitor.Common/RingBufferTruncationNote.cs @@ -108,15 +108,35 @@ public static bool TryParse(string? note, out long eventsHidden, out long newest /// The health-parser reads' answer (Lite and Darling, one string) when a category came back empty and the newest /// system_health collection run says the ring buffer was truncated. Without it those reads called the window /// "genuinely quiet" or "the healthy answer" while the newest events sat on the server unread (#6441). + /// #6650: when the read did count events of the type ( above 0) the first clause + /// says so, with the filter that left none (), instead of "No ... events were + /// found" over a count of hundreds. At 0 it keeps that wording. /// - public static string HealthParserMessage(string eventType, string serverName, int hoursBack, string truncation) => - string.Format( + public static string HealthParserMessage( + string eventType, string serverName, int hoursBack, string truncation, + int eventsRead = 0, string? noneQualifiedBecause = null) + { + var first = eventsRead > 0 + ? string.Format( + CultureInfo.InvariantCulture, + "{0} {1} event(s) were read for {2} in the last {3} hour(s), and {4}", + eventsRead, + eventType, + serverName, + hoursBack, + noneQualifiedBecause ?? "none qualified") + : string.Format( + CultureInfo.InvariantCulture, + "No {0} events were found for {1} in the last {2} hour(s)", + eventType, + serverName, + hoursBack); + + return first + string.Format( CultureInfo.InvariantCulture, - "No {0} events were found for {1} in the last {2} hour(s), and this is NOT an all-clear: the newest system_health collection run reported that {3}. Events of this type may exist on the server and not be stored here. Restarting the system_health session empties its ring buffer, and collection resumes until the buffer fills again.", - eventType, - serverName, - hoursBack, + ", and this is NOT an all-clear: the newest system_health collection run reported that {0}. Events of this type may exist on the server and not be stored here. Restarting the system_health session empties its ring buffer, and collection resumes until the buffer fills again.", truncation); + } /// An age in seconds as 5 h 23 min, 20 min or 45 s. public static string FormatAge(long seconds)