From 907595ffc6ba8224a1aa57709c0735ea7296282d Mon Sep 17 00:00:00 2001 From: Julian Knutsen Date: Sat, 10 Oct 2026 10:23:13 +0000 Subject: [PATCH] fix(cmd/bd): measure wisp GC age at updated_at's storage precision updated_at is a whole-second DATETIME and Dolt rounds a fractional write to the nearest second (verified: '00:00:00.7' reads back as '00:00:01'), so a just-written wisp reads back up to 500ms in the future. findAbandonedWisps compared now.Sub(updated_at) > threshold at nanosecond precision, so for up to half a second after a write the age was negative, and `bd mol wisp gc --age 0s` missed the wisp it was asked to reclaim. This made TestCLI_WispGCExcludeType flake (13/300 iterations on the cmd/bd Dolt-server lane). wispIdleAtLeast now rounds now to the same one-second precision and compares with >=. round(write) <= round(now) for any write at or before now, so age is never negative and --age 0s means every wisp. Thresholds are as fine as the stored timestamp: a sub-second threshold behaves like "idle for at least the current second". Fixes ga-vnycm2.44. Co-Authored-By: Claude Opus 5.5 (1M context) --- cmd/bd/wisp.go | 22 +++++++++++++++- cmd/bd/wisp_gc_active_embedded_test.go | 12 ++++----- cmd/bd/wisp_test.go | 36 ++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 7 deletions(-) diff --git a/cmd/bd/wisp.go b/cmd/bd/wisp.go index a1b7ac27e..f92bf7f95 100644 --- a/cmd/bd/wisp.go +++ b/cmd/bd/wisp.go @@ -822,6 +822,26 @@ func runWispGC(cmd *cobra.Command, args []string) error { return nil } +// updatedAtPrecision is the precision issues.updated_at / wisps.updated_at are +// stored at: a DATETIME with no fractional seconds. +const updatedAtPrecision = time.Second + +// wispIdleAtLeast reports whether a wisp last updated at updatedAt (as read +// back from the store) has been idle for at least threshold at now. +// +// Age is measured at the precision updated_at is stored at, not at now's +// nanosecond precision. Dolt ROUNDS a fractional write to the nearest second, +// so a row written at hh:mm:00.7 reads back as hh:mm:01, up to 500ms in the +// future of the write; raw now.Sub(updatedAt) is then negative for up to half +// a second, and `--age 0s` (every wisp) missed the wisp it was asked to +// reclaim (ga-vnycm2.44). Rounding now the same way keeps the comparison +// monotone — round(write) <= round(now) for any write at or before now — so a +// wisp's age is never negative and `--age 0s` always includes it. A threshold +// below one second is therefore only as fine as the stored timestamp. +func wispIdleAtLeast(now, updatedAt time.Time, threshold time.Duration) bool { + return now.Round(updatedAtPrecision).Sub(updatedAt) >= threshold +} + func findAbandonedWisps(ctx context.Context, r molReader, cleanAll bool, ageThreshold time.Duration, excludeTypes []types.IssueType) ([]*types.Issue, error) { ephemeralFlag := true filter := types.IssueFilter{ @@ -860,7 +880,7 @@ func findAbandonedWisps(ctx context.Context, r molReader, cleanAll bool, ageThre if isProtectedWisp(issue, blockedSet, protectedStatuses) { continue } - if now.Sub(issue.UpdatedAt) > ageThreshold { + if wispIdleAtLeast(now, issue.UpdatedAt, ageThreshold) { abandoned = append(abandoned, issue) } } diff --git a/cmd/bd/wisp_gc_active_embedded_test.go b/cmd/bd/wisp_gc_active_embedded_test.go index 3d62d9f93..2b0dc4ce8 100644 --- a/cmd/bd/wisp_gc_active_embedded_test.go +++ b/cmd/bd/wisp_gc_active_embedded_test.go @@ -30,12 +30,12 @@ func TestWispGCProtectsActiveWisps(t *testing.T) { // against the predicate degenerating into "protect everything". // // Ordering matters: every wisp expected to be RECLAIMED is created and - // last-touched first, before the protected ones. The age predicate is - // now.Sub(updated_at) > threshold against a timestamp written by the - // database, so the most recently touched bead can briefly appear - // not-yet-stale if the DB clock runs ahead of the test process. Protected - // wisps are excluded regardless of age, so only the reclaimable - // assertions are sensitive to this. + // last-touched first, before the protected ones. updated_at is stored at + // whole-second precision and the age predicate (wispIdleAtLeast) compares + // at that precision, so with a sub-second --age the most recently touched + // bead can read as zero seconds idle. Protected wisps are excluded + // regardless of age, so only the reclaimable assertions are sensitive to + // this. bdCommand(t, bd, dir, "config", "set", "status.custom", "reviewing:wip,triaging:active") // Genuinely abandoned: an idle open ephemeral wisp. Must be reclaimed. diff --git a/cmd/bd/wisp_test.go b/cmd/bd/wisp_test.go index 5e0a5a53f..258f53cd7 100644 --- a/cmd/bd/wisp_test.go +++ b/cmd/bd/wisp_test.go @@ -8,6 +8,7 @@ import ( "path/filepath" "strings" "testing" + "time" "github.com/spf13/cobra" @@ -169,3 +170,38 @@ func TestWispCreateDryRunFanoutMessage(t *testing.T) { } }) } + +// TestWispIdleAtLeastAtStoragePrecision pins ga-vnycm2.44: updated_at is a +// whole-second DATETIME, and Dolt ROUNDS a fractional write to the nearest +// second, so a wisp written at hh:mm:00.7 reads back as hh:mm:01 — 300ms in +// the future of the write. A `bd mol wisp gc --age 0s` at hh:mm:00.9 used to +// compute a negative age (now.Sub(updated_at) = -100ms), fail `> 0`, and miss +// the wisp it was asked to reclaim (TestCLI_WispGCExcludeType's flake). Age is +// now measured at the precision updated_at is stored at. +func TestWispIdleAtLeastAtStoragePrecision(t *testing.T) { + base := time.Date(2026, 10, 10, 12, 0, 0, 0, time.UTC) + written := base.Add(700 * time.Millisecond) + stored := written.Round(time.Second) // what Dolt's DATETIME holds: 12:00:01 + + for _, tc := range []struct { + name string + now time.Time + threshold time.Duration + want bool + }{ + {"age 0 just after a rounded-up write", base.Add(900 * time.Millisecond), 0, true}, + {"age 0 in the same instant as the write", written, 0, true}, + {"1ms threshold just after a rounded-up write", base.Add(900 * time.Millisecond), time.Millisecond, false}, + {"1ms threshold a second later", base.Add(1900 * time.Millisecond), time.Millisecond, true}, + {"1h threshold, fresh", base.Add(30 * time.Minute), time.Hour, false}, + {"1h threshold, exactly an hour at storage precision", stored.Add(time.Hour), time.Hour, true}, + {"1h threshold, well past", base.Add(2 * time.Hour), time.Hour, true}, + } { + t.Run(tc.name, func(t *testing.T) { + if got := wispIdleAtLeast(tc.now, stored, tc.threshold); got != tc.want { + t.Errorf("wispIdleAtLeast(now=%s, updated_at=%s, %s) = %v, want %v", + tc.now.Format("15:04:05.000"), stored.Format("15:04:05"), tc.threshold, got, tc.want) + } + }) + } +}