beads/cmd/bd/defer_wake_proxied_integration_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

146 lines
4.7 KiB
Go

//go:build cgo
package main
import (
"encoding/json"
"strings"
"testing"
"github.com/steveyegge/beads/internal/types"
)
// TestProxiedServerDeferAutoWake is the proxied-mode parity subtest for the
// defer auto-wake sweep: a DATED defer whose date has passed returns to open
// on the next ready read, a DATELESS defer never does. Same contract, other
// execution mode — the sweep must behave identically in both.
func TestProxiedServerDeferAutoWake(t *testing.T) {
requireSharedProxiedServer(t)
t.Parallel()
bd := buildEmbeddedBD(t)
p := newSharedProxiedProject(t, bd, "dwk")
showState := func(t *testing.T, id string) (string, interface{}) {
t.Helper()
out, err := bdProxiedRun(t, bd, p.dir, "show", id, "--json")
if err != nil {
t.Fatalf("show %s: %v\n%s", id, err, out)
}
var arr []map[string]interface{}
if err := json.Unmarshal(out, &arr); err != nil {
t.Fatalf("unmarshal: %v\n%s", err, out)
}
if len(arr) == 0 {
t.Fatalf("show returned nothing for %s", id)
}
status, _ := arr[0]["status"].(string)
return status, arr[0]["defer_until"]
}
readySet := func(t *testing.T) map[string]bool {
t.Helper()
out, err := bdProxiedRun(t, bd, p.dir, "ready", "--json", "--limit", "0")
if err != nil {
t.Fatalf("ready: %v\n%s", err, out)
}
s := strings.TrimSpace(string(out))
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
}
t.Run("expired_dated_defer_wakes_on_ready", func(t *testing.T) {
a := bdProxiedCreate(t, bd, p.dir, "Expired snooze proxied", "--type", "task")
if out, err := bdProxiedRun(t, bd, p.dir, "defer", a.ID, "--until", "2020-01-01"); err != nil {
t.Fatalf("defer: %v\n%s", err, out)
}
status, _ := showState(t, a.ID)
if status != string(types.StatusDeferred) {
t.Fatalf("precondition: status = %s, want deferred", status)
}
ids := readySet(t)
if !ids[a.ID] {
t.Errorf("expected %s in bd ready after its defer date passed", a.ID)
}
status, deferUntil := showState(t, a.ID)
if status != string(types.StatusOpen) {
t.Errorf("status = %s, want open after wake", status)
}
if deferUntil != nil {
t.Errorf("defer_until should be cleared after wake, got %v", deferUntil)
}
})
t.Run("expired_dated_defer_wakes_wisp", func(t *testing.T) {
// The uow stack's wisp seam: wisp tables are dolt_ignored, so a
// wisp-only sweep mints no wake commit and must persist through the
// ephemeral plain-COMMIT form instead — the path that once rolled
// wisp wakes back, leaving expired wisp defers deferred forever in
// proxied/serve mode while embedded mode woke them (parity break).
id := bdProxiedCreateSilent(t, bd, p.dir, "Wisp snooze proxied", "--type", "task", "--ephemeral", "--wisp-type", "heartbeat")
if out, err := bdProxiedRun(t, bd, p.dir, "defer", id, "--until", "2020-01-01"); err != nil {
t.Fatalf("defer wisp: %v\n%s", err, out)
}
status, _ := showState(t, id)
if status != string(types.StatusDeferred) {
t.Fatalf("precondition: wisp status = %s, want deferred", status)
}
if out, err := bdProxiedRun(t, bd, p.dir, "ready", "--json"); err != nil {
t.Fatalf("ready: %v\n%s", err, out)
}
status, deferUntil := showState(t, id)
if status != string(types.StatusOpen) {
t.Errorf("wisp status = %s, want open after wake (parity break vs embedded)", status)
}
if deferUntil != nil {
t.Errorf("wisp defer_until should be cleared after wake, got %v", deferUntil)
}
})
t.Run("dateless_defer_never_wakes", func(t *testing.T) {
a := bdProxiedCreate(t, bd, p.dir, "Indefinite icebox proxied", "--type", "task")
if out, err := bdProxiedRun(t, bd, p.dir, "defer", a.ID); err != nil {
t.Fatalf("defer: %v\n%s", err, out)
}
ids := readySet(t)
if ids[a.ID] {
t.Errorf("dateless defer %s must not appear in bd ready", a.ID)
}
status, _ := showState(t, a.ID)
if status != string(types.StatusDeferred) {
t.Errorf("dateless defer must stay deferred, got %s", status)
}
})
t.Run("future_dated_defer_stays_hidden", func(t *testing.T) {
a := bdProxiedCreate(t, bd, p.dir, "Future snooze proxied", "--type", "task")
if out, err := bdProxiedRun(t, bd, p.dir, "defer", a.ID, "--until", "+8760h"); err != nil {
t.Fatalf("defer: %v\n%s", err, out)
}
ids := readySet(t)
if ids[a.ID] {
t.Errorf("future defer %s must not appear in bd ready", a.ID)
}
status, _ := showState(t, a.ID)
if status != string(types.StatusDeferred) {
t.Errorf("future defer must stay deferred, got %s", status)
}
})
}