From 5877ce00c3cb5960d6ee44a3a85c434f4dcda155 Mon Sep 17 00:00:00 2001 From: rcourtman Date: Sun, 21 Dec 2025 22:19:40 +0000 Subject: [PATCH] fix: use 24h window for MetricSamples (matches in-memory retention) The in-memory MetricsHistory only retains 24 hours of data, not 7 days. Changed computeGuestMetricSamples to use trendWindow24h instead of trendWindow7d, and reduced sample count from 24 to 12 points. This ensures the LLM actually receives metric samples in the context, which wasn't happening before because the 7-day query returned empty data. --- internal/ai/context/builder.go | 11 ++++++----- internal/ai/context/formatter.go | 2 +- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/internal/ai/context/builder.go b/internal/ai/context/builder.go index 26b19873e..5944486b8 100644 --- a/internal/ai/context/builder.go +++ b/internal/ai/context/builder.go @@ -478,16 +478,17 @@ func (b *Builder) computeGuestMetricSamples(guestID string) map[string][]MetricP return samples } - // Get 7 days of data - enough to see weekly patterns and determine normalcy - allMetrics := b.metricsHistory.GetAllGuestMetrics(guestID, b.trendWindow7d) + // Get 24 hours of data (matches in-memory retention) + // This lets the LLM see recent patterns and changes + allMetrics := b.metricsHistory.GetAllGuestMetrics(guestID, b.trendWindow24h) for metric, points := range allMetrics { if len(points) < 3 { continue } - // Downsample to ~24 points (roughly 3 per day over 7 days) - // This lets the LLM see: daily patterns, weekly cycles, and recent changes - sampled := DownsampleMetrics(points, 24) + // Downsample to ~12 points (roughly every 2 hours over 24h) + // This gives the LLM a compact view of recent trends + sampled := DownsampleMetrics(points, 12) if len(sampled) >= 3 { samples[metric] = sampled } diff --git a/internal/ai/context/formatter.go b/internal/ai/context/formatter.go index ccd9f19f7..18d189e0e 100644 --- a/internal/ai/context/formatter.go +++ b/internal/ai/context/formatter.go @@ -64,7 +64,7 @@ func FormatResourceContext(ctx ResourceContext) string { // Raw metric samples - let the LLM interpret patterns directly // This is more reliable than pre-computed trends for edge cases if len(ctx.MetricSamples) > 0 { - sb.WriteString("**History (7d sampled, oldest→newest)**: ") + sb.WriteString("**History (24h sampled, oldest→newest)**: ") var sampleLines []string for metric, points := range ctx.MetricSamples { if len(points) >= 3 {