beads/cmd/bd/defer_wake_embedded_test.go
Steve Yegge 01f9166810
fix(defer): auto-wake expired dated defers on ready-front reads and claims (bd-i8qx8) (#5386)
* fix(defer): auto-wake expired dated defers on ready-front reads and claims (bd-i8qx8)

A dated defer (bd defer --until / bd update --defer) sets status=deferred
plus defer_until, but nothing ever flipped the status back — the ready
WHERE clause's defer_until predicate only helps rows already 'open', so an
expired defer stayed invisible to bd ready forever until a human ran bd
undefer. One deployment measured 241 issues (24 P1s in the first census)
silently dark this way, regenerating ~11/day from correct-looking
automation ('needs-design cooldown' call sites that defer +7d).

Fix: a lazy wake sweep (issueops.WakeExpiredDefersInTx, one shared DBTX
body serving both storage stacks, shaped after ReclaimExpiredLeasesInTx)
runs at the top of every ready-front read and claim:

- classic dolt server store: GetReadyWork/WithCounts/CountReadyWork and
  readyClaimer.ClaimNext, via runIssueOperationTxWithMessage in a write tx
  of its own (the read paths' withReadTx always rolls back)
- embedded store: same three seams plus its readyClaimer
- domain/uow stack (proxied CLI + bd serve): uow.WakeExpiredDefers in a
  committing UOW before Reader.Ready, Reader.List(--ready), the proxied
  CLI ready route, and uow readyClaimer.ClaimNext

Waking writes status=open, defer_until=NULL — byte-identical to bd
undefer, so a later dateless re-defer cannot inherit a stale past date —
and records a status_changed event (actor bd-defer-wake). A DATELESS
defer never auto-wakes: it is the indefinite icebox, and bd undefer stays
its only exit. The sweep is RowsAffected-gated (zero matches ⇒ no dolt
commit), advisory by contract (a ready listing never fails because the
sweep could not run; strict --readonly skips it), and covers wisps
(dolt_ignored, so wisp wakes mint no version commit by construction).

Tests: TestEmbeddedDeferAutoWake (wake/icebox/future/claimable) and
TestProxiedServerDeferAutoWake (parity subtest), green alongside the
existing defer/undefer/ready embedded and proxied suites.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* docs: auto-regenerate CLI reference

Applied from the cli-docs-freshness-patch artifact of https://github.com/gastownhall/beads/actions/runs/31145176188
(generated with CI's canonical pinned build). See
scripts/check-cli-docs-drift.sh for how drift is attributed.

* fix(defer): persist wisp-only wakes in the uow stack; warn-once on access-denied (bd-i8qx8)

The uow sweep discarded wisp-only wakes: the repo returned only the
permanent-issue count, a wisp-only sweep produced commitMsg == "",
RunTxResult skipped Commit, and Close rolled the transaction back —
expired wisp defers stayed deferred forever in proxied/serve mode while
embedded mode woke them, and every ready read redid and re-discarded the
same writes. A wake commit message would not have fixed it: wisp tables
are dolt_ignored, so DOLT_COMMIT errors nothing-to-commit and the tx
still rolls back.

WakeExpiredDefers now reports (issues, wisps) through the repo and use
case; the uow sweep commits with the wake message when permanent issues
woke, and persists a wisp-only wake via uw.Commit(ctx, "") — the
ephemeral plain-COMMIT form RunTxEphemeral keeps for dolt_ignored state.
Wisp parity subtests added to both the embedded and proxied defer
auto-wake tests.

Also: dberrors.IsAccessDenied classifier + warn-once handling in both
defer-wake advisory handlers, so a read-only-privileged client gets one
warning per process instead of one per bd ready.

* ci: trigger runs for 0a94d44f4 (empty)

* test(defer): teach the ready-surface test fixtures about the wake sweep (bd-i8qx8)

Three fixtures pinned the pre-sweep ready path and broke against it:

- readerIssues and recordingIssues embed a nil domain.IssueUseCase and
  stub only the read methods, so the sweep's promoted WakeExpiredDefers
  call segfaulted (surfacing as a 500 on every httpapi ready-surface
  test). Both now stub a no-op sweep.
- The reader-parity fixture's provider pool held one unit of work; the
  ready arm now opens two (sweep, then read span), and past the pool the
  mock provider hands out zero-valued units. The pool holds both.
- TestAReadRouteTimesTheUnitsOfWorkItsReaderOpens counted exactly one
  unit of work; the count is now two, and both still land in uow_ms,
  which is the property the test exists to pin.

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
2026-08-07 04:34:38 -07:00

184 lines
6.2 KiB
Go

//go:build cgo
package main
import (
"encoding/json"
"os"
"os/exec"
"strings"
"testing"
)
// showDeferState returns (status, defer_until) for an issue via bd show --json.
func showDeferState(t *testing.T, bd, dir, id string) (string, interface{}) {
t.Helper()
cmd := exec.Command(bd, "show", id, "--json")
cmd.Dir = dir
cmd.Env = bdEnv(dir)
stdout, stderr, err := runCommandBuffers(t, cmd)
if err != nil {
t.Fatalf("bd show %s --json failed: %v\nstdout:\n%s\nstderr:\n%s", id, err, stdout.String(), stderr.String())
}
s := strings.TrimSpace(stdout.String())
start := strings.IndexAny(s, "[{")
if start < 0 {
t.Fatalf("no JSON in show output: %s", s)
}
var m map[string]interface{}
if s[start] == '[' {
var arr []map[string]interface{}
if err := json.Unmarshal([]byte(s[start:]), &arr); err != nil {
t.Fatalf("parse show JSON array: %v\n%s", err, s)
}
if len(arr) == 0 {
t.Fatalf("empty JSON array in show output")
}
m = arr[0]
} else {
if err := json.Unmarshal([]byte(s[start:]), &m); err != nil {
t.Fatalf("parse show JSON: %v\n%s", err, s)
}
}
status, _ := m["status"].(string)
return status, m["defer_until"]
}
// wakeReadyIDs runs bd ready --json (plus extra args) and returns the listed ids.
func wakeReadyIDs(t *testing.T, bd, dir string, args ...string) map[string]bool {
t.Helper()
fullArgs := append([]string{"ready", "--json"}, args...)
cmd := exec.Command(bd, fullArgs...)
cmd.Dir = dir
cmd.Env = bdEnv(dir)
stdout, stderr, err := runCommandBuffers(t, cmd)
if err != nil {
t.Fatalf("bd ready --json failed: %v\nstdout:\n%s\nstderr:\n%s", err, stdout.String(), stderr.String())
}
s := strings.TrimSpace(stdout.String())
start := strings.Index(s, "[")
if start < 0 {
return map[string]bool{}
}
var arr []map[string]interface{}
if err := json.Unmarshal([]byte(s[start:]), &arr); err != nil {
t.Fatalf("parse ready JSON: %v\n%s", err, s)
}
ids := map[string]bool{}
for _, row := range arr {
if id, ok := row["id"].(string); ok {
ids[id] = true
}
}
return ids
}
// TestEmbeddedDeferAutoWake documents the defer contract: a DATED defer is a
// snooze — once defer_until passes, the next ready-front read returns the
// issue to open (status open, defer_until cleared, same shape bd undefer
// writes). A DATELESS defer is the indefinite icebox and never auto-wakes.
func TestEmbeddedDeferAutoWake(t *testing.T) {
if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" {
t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt integration tests")
}
t.Parallel()
bd := buildEmbeddedBD(t)
dir, _, _ := bdInit(t, bd, "--prefix", "dw")
t.Run("expired_dated_defer_wakes_on_ready", func(t *testing.T) {
issue := bdCreate(t, bd, dir, "Expired snooze", "--type", "task")
// defer.go warns about a past date but proceeds: status=deferred with
// an already-expired defer_until — exactly the tomb state a defer that
// expired while nobody was looking leaves behind.
bdDefer(t, bd, dir, issue.ID, "--until", "2020-01-01")
status, _ := showDeferState(t, bd, dir, issue.ID)
if status != "deferred" {
t.Fatalf("precondition: expected status=deferred, got %q", status)
}
ids := wakeReadyIDs(t, bd, dir)
if !ids[issue.ID] {
t.Errorf("expected %s in bd ready after its defer date passed", issue.ID)
}
status, deferUntil := showDeferState(t, bd, dir, issue.ID)
if status != "open" {
t.Errorf("expected status=open after wake, got %q", status)
}
if deferUntil != nil {
t.Errorf("expected defer_until cleared after wake (undefer's shape), got %v", deferUntil)
}
})
t.Run("dateless_defer_never_wakes", func(t *testing.T) {
issue := bdCreate(t, bd, dir, "Indefinite icebox", "--type", "task")
bdDefer(t, bd, dir, issue.ID)
ids := wakeReadyIDs(t, bd, dir)
if ids[issue.ID] {
t.Errorf("dateless defer %s must not appear in bd ready", issue.ID)
}
status, _ := showDeferState(t, bd, dir, issue.ID)
if status != "deferred" {
t.Errorf("dateless defer must stay deferred, got %q", status)
}
})
t.Run("future_dated_defer_stays_hidden", func(t *testing.T) {
issue := bdCreate(t, bd, dir, "Future snooze", "--type", "task")
bdDefer(t, bd, dir, issue.ID, "--until", "+8760h")
ids := wakeReadyIDs(t, bd, dir)
if ids[issue.ID] {
t.Errorf("future defer %s must not appear in bd ready", issue.ID)
}
status, _ := showDeferState(t, bd, dir, issue.ID)
if status != "deferred" {
t.Errorf("future defer must stay deferred, got %q", status)
}
})
t.Run("expired_dated_defer_wakes_wisp", func(t *testing.T) {
// Wisps ride the same defer/wake contract through dolt_ignored tables,
// which take a different persistence path (SQL commit, no version
// commit) — the seam where the uow stack once rolled wisp wakes back.
issue := bdCreate(t, bd, dir, "Wisp snooze", "--type", "task", "--ephemeral", "--wisp-type", "heartbeat")
bdDefer(t, bd, dir, issue.ID, "--until", "2020-01-01")
status, _ := showDeferState(t, bd, dir, issue.ID)
if status != "deferred" {
t.Fatalf("precondition: expected status=deferred, got %q", status)
}
_ = wakeReadyIDs(t, bd, dir) // trigger the sweep
status, deferUntil := showDeferState(t, bd, dir, issue.ID)
if status != "open" {
t.Errorf("expected wisp status=open after wake, got %q", status)
}
if deferUntil != nil {
t.Errorf("expected wisp defer_until cleared after wake, got %v", deferUntil)
}
})
t.Run("expired_dated_defer_claimable", func(t *testing.T) {
issue := bdCreate(t, bd, dir, "Claimable after snooze", "--type", "task", "--labels", "wake-claim-test")
bdDefer(t, bd, dir, issue.ID, "--until", "2020-01-01")
// The claim path wakes before selecting, so the freshly-expired bead
// is claimable without any listing having run first.
cmd := exec.Command(bd, "ready", "--claim", "--json", "-l", "wake-claim-test")
cmd.Dir = dir
cmd.Env = bdEnv(dir)
stdout, stderr, err := runCommandBuffers(t, cmd)
if err != nil {
t.Fatalf("bd ready --claim failed: %v\nstdout:\n%s\nstderr:\n%s", err, stdout.String(), stderr.String())
}
if !strings.Contains(stdout.String(), issue.ID) {
t.Errorf("expected claim to win %s, got:\n%s", issue.ID, stdout.String())
}
status, _ := showDeferState(t, bd, dir, issue.ID)
if status != "in_progress" {
t.Errorf("expected status=in_progress after claim, got %q", status)
}
})
}