From bf7c19ccc465f587e130ae3571b7c97cfe8e9c2d Mon Sep 17 00:00:00 2001 From: Rongjun GENG Date: Mon, 12 Oct 2026 05:37:54 +0000 Subject: [PATCH] fix(wisp): bd mol wisp gc --age 0s no longer misses a just-created wisp (#7246) * fix(wisp): --age 0s treats every wisp as abandoned, even one Dolt rounded into the future (#7236) findAbandonedWisps compared now.Sub(issue.UpdatedAt) > ageThreshold unconditionally. Dolt's DATETIME(0) column rounds fractional seconds half-up on write rather than truncating: a wisp created in the second half of a wall-clock second can read back with updated_at up to 0.5s in the future, making the comparison negative and failing the check for a wisp that is, by definition, not yet one second old. --age 0s's own documented contract ("hasn't been updated in --age duration") means every wisp qualifies trivially -- nothing has been updated in the last zero seconds. Extracted the comparison into wispIsAbandoned(now, updatedAt, ageThreshold), which short-circuits true whenever ageThreshold <= 0, bypassing the clock comparison (and its rounding exposure) entirely for that case. New TestWispIsAbandoned is a pure, deterministic unit test -- the original TestCLI_WispGCExcludeType needs a live Dolt test container and the reporter's own 60 real runs against one could not reliably reproduce the race, so a timing-dependent regression test would be exactly as unreliable. Confirmed red against the old inline comparison (the two age-0s cases failed) before extracting the fix, green after. go build/vet/gofmt clean. Co-Authored-By: Claude Sonnet 5 Claude-Session: https://claude.ai/code/session_0185qSpS74AbkmHoKRPf2yDD * chore: empty commit to retrigger mergeability recompute GitHub's mergeStateStatus has shown DIRTY/CONFLICTING since 2026-10-06, but a real `git merge --no-commit --no-ff` against current upstream/main auto-merges cleanly on all 3 touched files -- no actual conflict. This commit forces GitHub to recompute. Co-Authored-By: Claude Sonnet 5 * test(wisp): pin the gc call site and drop the 1ms age from the embedded test Folds the tests from #7472 into the #7236 fix. TestWispIsAbandoned pins the predicate. It cannot see whether findAbandonedWisps still decides age through it. TestFindAbandonedWisps AgesAtStoragePrecision drives findAbandonedWisps through a new wispGCNow clock seam with a wisp stored rounded into the future (written 12:00:00.7, stored 12:00:01): - --age 0s at 12:00:00.9 reclaims it. This fails against the old inline now.Sub(updated_at) > threshold. - --age 1h leaves it alone at that instant and reclaims it two hours later. - --age 1s at 12:00:01.5 leaves it alone: a positive threshold does not credit idle time that has not passed. TestWispGCProtectsActiveWisps ran gc with --age 1ms and relied on creation order to keep the most recently touched reclaimable wisp out of the rounding window. --age 0s states the intent ("every wisp is old enough") and is immune to the half-second rounding, so the ordering caveat goes. Co-authored-by: Julian Knutsen Co-Authored-By: Claude Opus 5.5 (1M context) --------- Co-authored-by: Claude Sonnet 5 Co-authored-by: Eddie the Engineer Co-authored-by: Julian Knutsen --- CHANGELOG.md | 8 ++ cmd/bd/BUILD.bazel | 1 + cmd/bd/wisp.go | 28 ++++- cmd/bd/wisp_abandoned_test.go | 138 +++++++++++++++++++++++++ cmd/bd/wisp_gc_active_embedded_test.go | 18 ++-- 5 files changed, 179 insertions(+), 14 deletions(-) create mode 100644 cmd/bd/wisp_abandoned_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 2a4cb8a05..986e0cb64 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -433,6 +433,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 role through (see *Added*). A missing id is a plain not-found after one `getIssue`, instead of a partial-id search refusal, and `bd update --claim` answers a not-found gc classifies. +- **`bd mol wisp gc --age 0s` can miss a just-created wisp.** Dolt's + `DATETIME(0)` column rounds fractional seconds half-up on write rather + than truncating, so a wisp created in the second half of a wall-clock + second can read back with `updated_at` up to 0.5s in the future. The GC's + age check (`now.Sub(updatedAt) > ageThreshold`) then compares against a + negative duration and fails, even though `--age 0s`'s own contract is + that every wisp qualifies. `ageThreshold <= 0` now bypasses the clock + comparison entirely (#7236). - **PRs based on `hotfix/**` branches now run full CI, not just cross-version historical smokes and triage labeling.** `pr.yml`, `pr-risk.yml`, `conformance.yml`, `cross-version-smoke.yml` and diff --git a/cmd/bd/BUILD.bazel b/cmd/bd/BUILD.bazel index 24364853d..6506f2bd3 100644 --- a/cmd/bd/BUILD.bazel +++ b/cmd/bd/BUILD.bazel @@ -1267,6 +1267,7 @@ go_test( "where_cgo_test.go", "where_embedded_test.go", "where_test.go", + "wisp_abandoned_test.go", "wisp_gc_active_embedded_test.go", "wisp_gc_purge_closed_embedded_test.go", "wisp_proxied_integration_test.go", diff --git a/cmd/bd/wisp.go b/cmd/bd/wisp.go index a1b7ac27e..82d0ee530 100644 --- a/cmd/bd/wisp.go +++ b/cmd/bd/wisp.go @@ -822,6 +822,30 @@ func runWispGC(cmd *cobra.Command, args []string) error { return nil } +// wispIsAbandoned reports whether a wisp counts as abandoned for GC +// purposes: it has not been updated within ageThreshold of now. +// +// ageThreshold <= 0 (--age 0s and below) always reports true, bypassing +// the clock comparison entirely — this matches the GC's own documented +// contract ("hasn't been updated in --age duration"): nothing has been +// updated in the last zero seconds. It also matters for a reason the +// comparison alone does not handle: Dolt's DATETIME(0) column rounds +// fractional seconds half-up on write rather than truncating (#7236), so +// a wisp created in the second half of a wall-clock second can read back +// with updatedAt up to 0.5s in the future. now.Sub(updatedAt) is then +// negative, failing a strict > comparison for a wisp that is, by +// definition, not yet one second old — exactly what --age 0s's "every +// wisp qualifies" contract exists to avoid depending on in the first +// place. +func wispIsAbandoned(now, updatedAt time.Time, ageThreshold time.Duration) bool { + return ageThreshold <= 0 || now.Sub(updatedAt) > ageThreshold +} + +// wispGCNow is the clock seam for findAbandonedWisps' age check, swappable in +// tests. Both gc routes read it, so a test that swaps it must not call +// t.Parallel(). +var wispGCNow = time.Now + func findAbandonedWisps(ctx context.Context, r molReader, cleanAll bool, ageThreshold time.Duration, excludeTypes []types.IssueType) ([]*types.Issue, error) { ephemeralFlag := true filter := types.IssueFilter{ @@ -848,7 +872,7 @@ func findAbandonedWisps(ctx context.Context, r molReader, cleanAll bool, ageThre return nil, err } - now := time.Now() + now := wispGCNow() var abandoned []*types.Issue for _, issue := range issues { if r.IsInfraTypeCtx(ctx, issue.IssueType) { @@ -860,7 +884,7 @@ func findAbandonedWisps(ctx context.Context, r molReader, cleanAll bool, ageThre if isProtectedWisp(issue, blockedSet, protectedStatuses) { continue } - if now.Sub(issue.UpdatedAt) > ageThreshold { + if wispIsAbandoned(now, issue.UpdatedAt, ageThreshold) { abandoned = append(abandoned, issue) } } diff --git a/cmd/bd/wisp_abandoned_test.go b/cmd/bd/wisp_abandoned_test.go new file mode 100644 index 000000000..e18c3397a --- /dev/null +++ b/cmd/bd/wisp_abandoned_test.go @@ -0,0 +1,138 @@ +package main + +import ( + "context" + "testing" + "time" + + "github.com/steveyegge/beads/internal/types" +) + +// TestWispIsAbandoned pins the #7236 fix: --age 0s (ageThreshold <= 0) must +// treat every wisp as abandoned regardless of its updatedAt, including a +// wisp whose stored updatedAt reads back AFTER now — the shape Dolt's +// DATETIME(0) half-up rounding on write produces for a wisp created in the +// second half of a wall-clock second (#7236). Before this fix, that case +// failed the strict now.Sub(updatedAt) > ageThreshold comparison because +// the difference is negative. +func TestWispIsAbandoned(t *testing.T) { + now := time.Date(2026, 1, 1, 12, 0, 0, 0, time.UTC) + + cases := []struct { + name string + updatedAt time.Time + age time.Duration + want bool + }{ + { + name: "age 0s: a wisp updated exactly now is abandoned", + updatedAt: now, + age: 0, + want: true, + }, + { + name: "age 0s: a wisp whose stored updatedAt rounded into the future is still abandoned", + updatedAt: now.Add(500 * time.Millisecond), + age: 0, + want: true, + }, + { + name: "a negative age behaves like zero", + updatedAt: now.Add(500 * time.Millisecond), + age: -time.Second, + want: true, + }, + { + name: "positive age: a fresh wisp is not abandoned", + updatedAt: now, + age: time.Hour, + want: false, + }, + { + name: "positive age: a wisp past the threshold is abandoned", + updatedAt: now.Add(-2 * time.Hour), + age: time.Hour, + want: true, + }, + { + name: "positive age: exactly at the threshold is not abandoned (strict >, unchanged)", + updatedAt: now.Add(-time.Hour), + age: time.Hour, + want: false, + }, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + if got := wispIsAbandoned(now, tc.updatedAt, tc.age); got != tc.want { + t.Errorf("wispIsAbandoned(%v, %v, %v) = %v, want %v", now, tc.updatedAt, tc.age, got, tc.want) + } + }) + } +} + +// fixedWispsMolReader serves findAbandonedWisps a fixed set of wisps with +// nothing blocked, no custom statuses and no dependents. It embeds the +// molReader INTERFACE, so a read the age scan does not make today panics +// rather than quietly answering zero. +type fixedWispsMolReader struct { + molReader + wisps []*types.Issue +} + +func (r fixedWispsMolReader) SearchIssues(context.Context, string, types.IssueFilter) ([]*types.Issue, error) { + return r.wisps, nil +} +func (fixedWispsMolReader) GetBlockedIssues(context.Context, types.WorkFilter) ([]*types.BlockedIssue, error) { + return nil, nil +} +func (fixedWispsMolReader) GetCustomStatusesDetailed(context.Context) ([]types.CustomStatus, error) { + return nil, nil +} +func (fixedWispsMolReader) IsInfraTypeCtx(context.Context, types.IssueType) bool { return false } +func (fixedWispsMolReader) FindWispDependentsRecursive(context.Context, []string) (map[string]bool, error) { + return nil, nil +} + +// TestFindAbandonedWispsAgesAtStoragePrecision pins the call site that +// TestWispIsAbandoned cannot see: findAbandonedWisps must decide age through +// wispIsAbandoned. A wisp written at 12:00:00.7 is stored as 12:00:01, and a +// gc at 12:00:00.9 with --age 0s must still reclaim it; the raw +// now.Sub(updated_at) > threshold that #7236 replaced reads -100ms and misses +// it. A one-hour threshold must leave the same wisp alone at that instant and +// reclaim it two hours later, so dropping the age check fails too. --age 1s +// at 12:00:01.5 must leave it alone: only half a second has passed since the +// stored second, so a positive threshold never credits idle time that has +// not elapsed. +func TestFindAbandonedWispsAgesAtStoragePrecision(t *testing.T) { + base := time.Date(2026, 10, 10, 12, 0, 0, 0, time.UTC) + wisp := &types.Issue{ + ID: "gc-idle", + Status: types.StatusOpen, + IssueType: types.TypeTask, + Ephemeral: true, + UpdatedAt: base.Add(700 * time.Millisecond).Round(time.Second), // 12:00:01 + } + old := wispGCNow + t.Cleanup(func() { wispGCNow = old }) + + for _, tc := range []struct { + now time.Time + threshold time.Duration + want int + }{ + {base.Add(900 * time.Millisecond), 0, 1}, + {base.Add(900 * time.Millisecond), time.Hour, 0}, + {base.Add(2 * time.Hour), time.Hour, 1}, + {base.Add(1500 * time.Millisecond), time.Second, 0}, + } { + wispGCNow = func() time.Time { return tc.now } + got, err := findAbandonedWisps(context.Background(), fixedWispsMolReader{wisps: []*types.Issue{wisp}}, false, tc.threshold, nil) + if err != nil { + t.Fatalf("findAbandonedWisps(--age %s): %v", tc.threshold, err) + } + if len(got) != tc.want { + t.Errorf("findAbandonedWisps(--age %s) at %s, updated_at 12:00:01: got %d wisps, want %d", + tc.threshold, tc.now.Format("15:04:05.000"), len(got), tc.want) + } + } +} diff --git a/cmd/bd/wisp_gc_active_embedded_test.go b/cmd/bd/wisp_gc_active_embedded_test.go index 3d62d9f93..7ab85a319 100644 --- a/cmd/bd/wisp_gc_active_embedded_test.go +++ b/cmd/bd/wisp_gc_active_embedded_test.go @@ -28,14 +28,6 @@ func TestWispGCProtectsActiveWisps(t *testing.T) { // category. "reviewing" is wip (protected); "triaging" is active, which // behaves like plain open and stays reclaimable — that pair also guards // 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. bdCommand(t, bd, dir, "config", "set", "status.custom", "reviewing:wip,triaging:active") // Genuinely abandoned: an idle open ephemeral wisp. Must be reclaimed. @@ -66,10 +58,12 @@ func TestWispGCProtectsActiveWisps(t *testing.T) { customWIP := bdCreate(t, bd, dir, "custom wip step", "--ephemeral").ID bdCommand(t, bd, dir, "update", customWIP, "--status", "reviewing") - // Every wisp above was created well over 1ms ago by the time gc runs, so a - // 1ms threshold makes them all "stale" by updated_at. Only the genuinely - // abandoned ones should be reclaimed. - out := bdCommand(t, bd, dir, "mol", "wisp", "gc", "--age", "1ms", "--dry-run", "--json") + // --age 0s makes every wisp above old enough to reclaim. Any positive --age + // can leave the most recently touched one reading as too young, because + // updated_at is stored at whole-second precision and Dolt rounds the write + // up to half a second into the future (#7236). Protection is checked + // before age, so only the genuinely abandoned ones should be reclaimed. + out := bdCommand(t, bd, dir, "mol", "wisp", "gc", "--age", "0s", "--dry-run", "--json") var res struct { CleanedIDs []string `json:"cleaned_ids"`