metrics: disk trend uses Theil-Sen so a burst is not growth
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
internal/metrics/forecast.go+50 −35internal/metrics/forecast_test.go+23 −11
CI failed — run details
Intent
<task-notification> <task-id>aa9707fd2c6c1a2ea</task-id> <tool-use-id>toolu_01CrtgFDB6uskkeaZsGb7GfX</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/6112a34e-476e-4b5e-8e07-f93a46fc6d50/tasks/aa9707fd2c6c1a2ea.output</output-file> <status>completed</status> <summary>Agent "Implement Task 1: disk forecast rule" finished</summary> <note>A task-notification fires each time this agent stops with no live background children of its own. The user can send it another message and resume it, so the same task-id may notify more than once.</note> <result>**Status:** DONE_WITH_CONCERNS **Commit:** 059354bf "metrics: disk_eta forecast rule". It landed despite a lock warning from git. **Tests:** The new tests and the whole `internal/metrics` package pass. RED was a build failure on the undefined symbols. `gofmt`, `go vet` and `go build ./...` are clean. **Concerns:** 1. A plain least-squares fit on a large early step in disk use still forecasts a fill. A 20 GiB step 5 h ago on a 100 GiB disk (50 used) gave an ETA of 16 h. The brief's "NoGrowth, or ETA over 108" only holds for a modest burst, so `TestDiskTrendBurstThenFlat` uses a 4 GiB step. I kept the algorithm as specified; a reviewer ruling on outlier handling may be worth getting. 2. `env.md` asks for the trailer `Claude Opus 5.5`, but the session attribution says `Claude Sonnet 5.5`, which is the real model. I used Sonnet 5.5. 3. `go test` needed the sandbox disabled because `~/.cache/go-build` is read-only inside it. `env.md` allows that. **Report:** `/home/paulca/amadan.net/carlos/platform/.claude/worktrees/design-fleet-alerts/.superpowers/sdd/2026-10-30-fleet-alerts-1-engine-connectors/task-1-report.md`</result> <usage><subagent_tokens>64180</subagent_tokens><tool_uses>11</tool_uses><duration_ms>133803</duration_ms></usage> </task-notification>
Attributed to this commit: 19.3 g CO₂e (2,433,210 tokens). This is an attribution, not a measurement — usage counted from this commit's own marker forward to the next one on design-fleet-alerts, never backward from it. A turn that produced more than one commit books all of its tokens to the last one, and a concurrent session on this branch can add to the same figure. Some of it was priced using the shared default rate, because no model-specific factor row exists yet for at least one model involved. The Carbon view has the repo total, which is never derived from this attribution.
diff --git a/internal/metrics/forecast.go b/internal/metrics/forecast.go| index efbef3035..fc820762e 100644 |
| --- a/internal/metrics/forecast.go |
| +++ b/internal/metrics/forecast.go |
| @@ -1,6 +1,9 @@ |
| package metrics |
| -import "time" |
| +import ( |
| + "sort" |
| + "time" |
| +) |
| // DiskPoint is one disk reading for the forecast: bytes used and the |
| // filesystem size at a moment. Samples carry only a fraction (sample.go), so |
| @@ -36,17 +39,35 @@ const ( |
| trendMinPoints = 30 |
| ) |
| -// DiskTrend fits an ordinary least-squares line to used bytes against time |
| -// over the points at most 6 hours old, and projects when the disk reaches |
| +// DiskTrend estimates the growth rate of used bytes with the Theil-Sen |
| +// slope, the median of the pairwise slopes (y_j - y_i)/(t_j - t_i) over every |
| +// pair of points at most 6 hours old, and projects when the disk reaches |
| // TotalBytes. It returns TrendUnknown below 2 hours of span or 30 points, |
| -// TrendNoGrowth when the slope is zero or negative (so a deletion reads as |
| -// no growth, spec §7.2), and TrendGrowing with the hours to full otherwise. |
| -// The ETA is measured from the fitted value at now, not the last raw point, |
| -// so one noisy reading doesn't move it; it is never negative and never |
| -// infinite (a flat line is TrendNoGrowth, not an infinite ETA). The points |
| -// need not be sorted. TotalBytes comes from the newest point. |
| +// TrendNoGrowth when the median slope is zero or negative (so a deletion |
| +// reads as no growth, spec §7.2), and TrendGrowing with the hours to full |
| +// otherwise. |
| +// |
| +// Why the median and not a least-squares fit: a burst (a log dump, a restore) |
| +// is a step, and a step puts a large positive slope into a least-squares line |
| +// for hours afterwards, which would forecast a fill that is not coming |
| +// (spec §6.3). In a series with one step, only the pairs that straddle it |
| +// are positive, and that is at most about half of them; pairs on the same |
| +// side of it are flat. So the median stays at zero, which reads as |
| +// TrendNoGrowth. Steady growth shows in nearly every pair, so the median |
| +// tracks it. The pairs are computed directly: at most 360 points is about |
| +// 64k pairs. |
| +// |
| +// One edge: with n points, a step within about 10 minutes of the exact middle of the |
| +// window straddles slightly more than half the pairs (n/(2(n-1)) at the |
| +// centre), so the median is then a small positive slope. A step crosses that |
| +// band in about 20 minutes, well inside the 2 hour hold on the rule, which |
| +// resets on the first tick that reads NoGrowth. |
| +// The ETA is measured from the latest observed used bytes (no intercept is |
| +// fitted), is never negative, and is never infinite (a flat series is |
| +// TrendNoGrowth, not an infinite ETA). The points need not be sorted. |
| +// TotalBytes comes from the newest point. |
| // |
| -// Pure, and the same fit can serve other used-vs-time series later. |
| +// Pure, and the same estimator can serve other used-vs-time series later. |
| func DiskTrend(points []DiskPoint, now time.Time) (v TrendVerdict, etaHours float64) { |
| var in []DiskPoint |
| for _, p := range points { |
| @@ -57,47 +78,41 @@ func DiskTrend(points []DiskPoint, now time.Time) (v TrendVerdict, etaHours floa |
| if len(in) < trendMinPoints { |
| return TrendUnknown, 0 |
| } |
| - first, last := in[0].At, in[0].At |
| + first := in[0].At |
| newest := in[0] |
| for _, p := range in { |
| if p.At.Before(first) { |
| first = p.At |
| } |
| - if p.At.After(last) { |
| - last = p.At |
| + if p.At.After(newest.At) { |
| newest = p |
| } |
| } |
| - if last.Sub(first) < trendMinSpan { |
| + if newest.At.Sub(first) < trendMinSpan { |
| return TrendUnknown, 0 |
| } |
| - // x is minutes since the first point and y is used bytes. The sums |
| - // below are of deviations from the means, which keeps float64 precise |
| - // on multi-terabyte disks. |
| - n := float64(len(in)) |
| - var sx, sy float64 |
| - xs := make([]float64, len(in)) |
| - ys := make([]float64, len(in)) |
| - for i, p := range in { |
| - xs[i] = p.At.Sub(first).Minutes() |
| - ys[i] = float64(p.UsedBytes) |
| - sx += xs[i] |
| - sy += ys[i] |
| - } |
| - mx, my := sx/n, sy/n |
| - var sxx, sxy float64 |
| + slopes := make([]float64, 0, len(in)*(len(in)-1)/2) |
| for i := range in { |
| - dx := xs[i] - mx |
| - sxx += dx * dx |
| - sxy += dx * (ys[i] - my) |
| + for j := range in { |
| + dt := in[j].At.Sub(in[i].At) |
| + if dt <= 0 { |
| + continue |
| + } |
| + dy := float64(in[j].UsedBytes) - float64(in[i].UsedBytes) |
| + slopes = append(slopes, dy/dt.Minutes()) // bytes per minute |
| + } |
| + } |
| + sort.Float64s(slopes) |
| + mid := len(slopes) / 2 |
| + slope := slopes[mid] |
| + if len(slopes)%2 == 0 { |
| + slope = (slopes[mid-1] + slopes[mid]) / 2 |
| } |
| - slope := sxy / sxx // bytes per minute; sxx > 0 because span >= 2 h |
| if !(slope > 0) { |
| return TrendNoGrowth, 0 |
| } |
| - fittedNow := my + slope*(now.Sub(first).Minutes()-mx) |
| - remaining := float64(newest.TotalBytes) - fittedNow |
| + remaining := float64(newest.TotalBytes) - float64(newest.UsedBytes) |
| if remaining < 0 { |
| remaining = 0 |
| } |
diff --git a/internal/metrics/forecast_test.go b/internal/metrics/forecast_test.go| index a4c6881d4..f9d27be07 100644 |
| --- a/internal/metrics/forecast_test.go |
| +++ b/internal/metrics/forecast_test.go |
| @@ -34,22 +34,34 @@ func TestDiskTrendSteadyFill(t *testing.T) { |
| func TestDiskTrendBurstThenFlat(t *testing.T) { |
| now := time.Date(2026, 10, 30, 12, 0, 0, 0, time.UTC) |
| - // A 4 GiB jump 5.5 h ago (a log dump, say), flat since. A least-squares |
| - // line through a step early in the window leans only slightly upward, so |
| - // it must not read as an imminent fill. (A much larger step does tilt |
| - // the line enough to forecast; that is inherent to the plain fit.) |
| + // A 20 GiB jump 5 h ago, flat since, on 100 GiB with 50 used. A burst is |
| + // not growth: it must not forecast a fill (spec §6.3). |
| pts := diskSeries(now, 360, 100*gib, func(m int) uint64 { |
| - if m > 330 { |
| - return 46 * gib |
| + if m > 300 { |
| + return 30 * gib |
| } |
| return 50 * gib |
| }) |
| - v, eta := DiskTrend(pts, now) |
| - if v == TrendGrowing && eta <= 108 { |
| - t.Fatalf("burst then flat must not forecast under 108 h, got growing eta=%v", eta) |
| + if v, eta := DiskTrend(pts, now); v != TrendNoGrowth { |
| + t.Fatalf("want TrendNoGrowth, got %v (eta %v)", v, eta) |
| } |
| - if v == TrendUnknown { |
| - t.Fatal("360 points over 6 h is enough to forecast") |
| +} |
| + |
| +func TestDiskTrendBurstMidWindow(t *testing.T) { |
| + now := time.Date(2026, 10, 30, 12, 0, 0, 0, time.UTC) |
| + // The same 20 GiB jump in the middle of the window, where the most pairs |
| + // straddle it: 4 h ago and 2 h ago. (Within about 20 minutes of the exact |
| + // centre the median turns slightly positive; see DiskTrend.) |
| + for _, minsAgo := range []int{240, 120} { |
| + pts := diskSeries(now, 360, 100*gib, func(m int) uint64 { |
| + if m > minsAgo { |
| + return 30 * gib |
| + } |
| + return 50 * gib |
| + }) |
| + if v, eta := DiskTrend(pts, now); v != TrendNoGrowth { |
| + t.Fatalf("step %d min ago: want TrendNoGrowth, got %v (eta %v)", minsAgo, v, eta) |
| + } |
| } |
| } |