beads/cmd/bd/wisp_proxied_integration_test.go
gascity-eddie[bot] b17005ad14
fix(wisp gc): apply the --age test to cascade children (follow-up to #5219) (#7599)
* fix(wisp gc): apply the --age test to cascade children

`bd mol wisp gc --age` promised, in its own help text, that a wisp counts as
abandoned when it "has not been updated within --age AND is not closed".
Parents were held to that contract; children reached through the cascade were
not. A child was deleted purely because something it transitively depended on
was old, so `--age` meant nothing for children at all.

Raising the threshold is not a workaround: with `--age 720h`, a step created a
moment ago is still destroyed when its parent is older than 720 hours.

Observed blast radius: a molecule slung seconds earlier went down with all nine
of its steps — 43 beads in a single run — and the agent's hook emptied
instantly. Two rigs of three were hit; the third survived only because a stop
order reached it first.

Steps already picked up have been covered by `isProtectedWisp` since #4430, but
a freshly created step is still `open`, and `open` maps to CategoryActive, which
that guard deliberately does not protect. This closes the remaining path.

Tests: cascade deletion of a stale parent no longer takes a fresh child, and
raising --age does not save one either.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* fix(wisp gc): stop re-cascading at delete time; pin the age contract in the proxied test

The integration test update Steve asked for surfaced that the selection fix
alone was incomplete: findAbandonedWisps now respects --age for cascade
children, but both delete paths re-derived the cascade at delete time and
swept fresh children past that filter anyway:

- embedded path: deleteBatch was called with cascade=true
- proxied-server path: DeleteIssuesParams{Cascade: true} re-expanded via
  FindAllDependents in the domain layer

Both now delete exactly the set findAbandonedWisps selected — the selection
is the single place the --age contract lives.

Test cascade_sweeps_dependent_step_wisps is reshaped per review into
cascade_respects_age_for_dependent_step_wisps: a second child backdated past
the threshold IS swept (cascade still collects genuinely abandoned children),
the fresh child is absent from deleted AND still present in the wisps table
(the table-level check is what caught the delete-time re-cascade).

Verified against the shared dolt container (BEADS_TEST_PROXIED_SERVER=1):
TestProxiedServerWispGC all four subtests pass; TestWispGC* unit tests pass.

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

* fix(wisp gc): age cascade children with wispIsAbandoned

Adopting #5219 onto main, which landed two changes after this PR was
written:

- #7246 (fixes #7236) moved the parents' age check onto
  wispIsAbandoned, so --age 0s still collects a wisp whose DATETIME(0)
  updated_at rounded up to half a second into the future. The PR's child
  check kept the raw now.Sub(updated_at) <= threshold skip, which
  re-opens #7236 for cascade children: under --age 0s a step closed in
  the same second as the gc was left behind, orphaned, while its
  abandoned parent was deleted. Children now take the parents'
  predicate, so "the same --age contract as parents" holds; positive
  thresholds behave exactly as before.
  TestWispGCCascadeChildAgesAtStoragePrecision pins it at storage
  precision and fails on the raw comparison.
- #5735 had already turned the delete-time cascade off on both gc
  routes (GH#4394), so the PR's second commit now carries only its
  proxied-test reshape. Main's delete-site comments gain that commit's
  reason alongside GH#4394: a cascade there would also sweep fresh
  children past the --age filter.

Also lists the PR's new wisp_gc_cascade_age_test.go in cmd/bd/BUILD.bazel,
which gazelle requires.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: mayor <6847071@gmail.com>
Co-authored-by: AB <6847071@gmail.com>

* docs(wisp gc): document the cascade's age policy; test it with a closed child

Review fixes for #5219, apply-fixes iteration 1.

- The 'bd mol wisp gc' help and the CHANGELOG now say what happens to
  the dependents of an abandoned wisp. They are deleted with it only if
  they meet --age themselves and are not live work, closed ones included
  even without --all. A dependent that is too young is spared, and both
  delete routes drop its edge to the deleted wisp, so an open one is
  collected once it ages out and a closed one is left for --all,
  --closed or bd purge: without --all the parent scan skips closed
  wisps.
- A comment by the child loop in findAbandonedWisps says dependents are
  exempt from the parent scan's not-closed clause, so a change that
  aligns the two loops does not quietly leave an abandoned molecule's
  finished steps behind.
- TestWispGCCascadeRespectsAge and the proxied subtest
  cascade_respects_age_for_dependent_step_wisps close their stale child.
  An open one was collected by the parent scan, so both passed with the
  cascade disabled. With the child closed, a mutant that skips every
  cascade child fails both.

docs/CLI_REFERENCE.md and docs/cli-reference/ are not regenerated: they
are built from the v1.2.2 pin in docs/cli-docs.pin.

Agent-Signature: claude-code-claude-opus-5-5-max on behalf of Gas City Adopt-PR
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: mayor <6847071@gmail.com>
Co-authored-by: AB <6847071@gmail.com>

---------

Co-authored-by: mayor <6847071@gmail.com>
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-authored-by: Eddie the Engineer <adopt-pr@gascity.com>
Co-authored-by: Eddie the Engineer <julianknutsen@users.noreply.github.com>
2026-10-16 09:49:28 +00:00

392 lines
13 KiB
Go

//go:build cgo
package main
import (
"encoding/json"
"strings"
"testing"
"time"
)
func TestProxiedServerWispCreate(t *testing.T) {
requireSharedProxiedServer(t)
t.Parallel()
bd := buildEmbeddedBD(t)
t.Run("bare_wisp_and_wisp_create_materialize_same_dag", func(t *testing.T) {
t.Parallel()
p := newSharedProxiedProject(t, bd, "wcd")
proto := bdProxiedCreate(t, bd, p.dir, "DAG proto", "--type", "epic", "--label", "template")
bdProxiedCreate(t, bd, p.dir, "DAG step one", "--type", "task", "--parent", proto.ID)
bdProxiedCreate(t, bd, p.dir, "DAG step two", "--type", "task", "--parent", proto.ID)
out, err := bdProxiedRun(t, bd, p.dir, "mol", "wisp", proto.ID, "--json")
if err != nil {
t.Fatalf("bd mol wisp --json: %v\n%s", err, out)
}
var got struct {
Created int `json:"created"`
Phase string `json:"phase"`
}
if err := json.Unmarshal(out, &got); err != nil {
t.Fatalf("unmarshal: %v\n%s", err, out)
}
if got.Created != 3 {
t.Errorf("created = %d, want 3 (root + 2 steps)", got.Created)
}
if got.Phase != "vapor" {
t.Errorf("phase = %s, want vapor", got.Phase)
}
out2, err := bdProxiedRun(t, bd, p.dir, "mol", "wisp", "create", proto.ID, "--json")
if err != nil {
t.Fatalf("bd mol wisp create --json: %v\n%s", err, out2)
}
var got2 struct {
Created int `json:"created"`
}
if err := json.Unmarshal(out2, &got2); err != nil {
t.Fatalf("unmarshal: %v\n%s", err, out2)
}
if got2.Created != got.Created {
t.Errorf("bd mol wisp create --json created %d, want %d (parity with bare wisp)", got2.Created, got.Created)
}
})
t.Run("root_only_skips_children", func(t *testing.T) {
t.Parallel()
p := newSharedProxiedProject(t, bd, "wcr")
proto := bdProxiedCreate(t, bd, p.dir, "Root-only proto", "--type", "epic", "--label", "template")
bdProxiedCreate(t, bd, p.dir, "Skipped step", "--type", "task", "--parent", proto.ID)
out, err := bdProxiedRun(t, bd, p.dir, "mol", "wisp", "create", proto.ID, "--root-only", "--json")
if err != nil {
t.Fatalf("bd mol wisp create --root-only --json: %v\n%s", err, out)
}
var got struct {
Created int `json:"created"`
}
if err := json.Unmarshal(out, &got); err != nil {
t.Fatalf("unmarshal: %v\n%s", err, out)
}
if got.Created != 1 {
t.Errorf("created = %d, want 1 (root only)", got.Created)
}
})
t.Run("dry_run_creates_nothing", func(t *testing.T) {
t.Parallel()
p := newSharedProxiedProject(t, bd, "wcdr")
proto := bdProxiedCreate(t, bd, p.dir, "Dry wisp proto", "--type", "epic", "--label", "template")
bdProxiedCreate(t, bd, p.dir, "Dry wisp step", "--type", "task", "--parent", proto.ID)
stdout, _, err := bdProxiedRunBuffers(t, bd, p.dir, "mol", "wisp", "create", proto.ID, "--dry-run")
if err != nil {
t.Fatalf("bd mol wisp create --dry-run: %v\n%s", err, stdout)
}
if !strings.Contains(stdout, "Dry run") {
t.Errorf("expected dry-run message, got: %s", stdout)
}
db := openProxiedDB(t, p)
var count int
if err := db.QueryRow("SELECT COUNT(*) FROM wisps").Scan(&count); err != nil {
t.Fatalf("count wisps: %v", err)
}
if count != 0 {
t.Errorf("expected no wisps created by --dry-run, found %d", count)
}
})
}
func TestProxiedServerWispList(t *testing.T) {
requireSharedProxiedServer(t)
t.Parallel()
bd := buildEmbeddedBD(t)
t.Run("default_excludes_closed_all_includes", func(t *testing.T) {
t.Parallel()
p := newSharedProxiedProject(t, bd, "wl")
openWisp := bdProxiedCreate(t, bd, p.dir, "Open wisp", "--ephemeral")
closedWisp := bdProxiedCreate(t, bd, p.dir, "Closed wisp", "--ephemeral")
if out, err := bdProxiedRun(t, bd, p.dir, "close", closedWisp.ID); err != nil {
t.Fatalf("bd close: %v\n%s", err, out)
}
out, err := bdProxiedRun(t, bd, p.dir, "mol", "wisp", "list", "--json")
if err != nil {
t.Fatalf("bd mol wisp list --json: %v\n%s", err, out)
}
var got struct {
Wisps []struct {
ID string `json:"id"`
} `json:"wisps"`
}
if err := json.Unmarshal(out, &got); err != nil {
t.Fatalf("unmarshal: %v\n%s", err, out)
}
ids := map[string]bool{}
for _, w := range got.Wisps {
ids[w.ID] = true
}
if !ids[openWisp.ID] {
t.Errorf("expected open wisp %s in default list", openWisp.ID)
}
if ids[closedWisp.ID] {
t.Errorf("closed wisp %s should be excluded by default", closedWisp.ID)
}
outAll, err := bdProxiedRun(t, bd, p.dir, "mol", "wisp", "list", "--all", "--json")
if err != nil {
t.Fatalf("bd mol wisp list --all --json: %v\n%s", err, outAll)
}
var gotAll struct {
Wisps []struct {
ID string `json:"id"`
} `json:"wisps"`
}
if err := json.Unmarshal(outAll, &gotAll); err != nil {
t.Fatalf("unmarshal: %v\n%s", err, outAll)
}
idsAll := map[string]bool{}
for _, w := range gotAll.Wisps {
idsAll[w.ID] = true
}
if !idsAll[closedWisp.ID] {
t.Errorf("expected closed wisp %s in --all list", closedWisp.ID)
}
})
t.Run("type_filter", func(t *testing.T) {
t.Parallel()
p := newSharedProxiedProject(t, bd, "wlt")
taskWisp := bdProxiedCreate(t, bd, p.dir, "Task wisp", "--ephemeral", "--type", "task")
bdProxiedCreate(t, bd, p.dir, "Chore wisp", "--ephemeral", "--type", "chore")
out, err := bdProxiedRun(t, bd, p.dir, "mol", "wisp", "list", "--type", "task", "--json")
if err != nil {
t.Fatalf("bd mol wisp list --type task --json: %v\n%s", err, out)
}
var got struct {
Wisps []struct {
ID string `json:"id"`
Type string `json:"type"`
} `json:"wisps"`
}
if err := json.Unmarshal(out, &got); err != nil {
t.Fatalf("unmarshal: %v\n%s", err, out)
}
if len(got.Wisps) != 1 || got.Wisps[0].ID != taskWisp.ID {
t.Fatalf("expected only task wisp %s, got %+v", taskWisp.ID, got.Wisps)
}
})
t.Run("old_wisp_flagged", func(t *testing.T) {
t.Parallel()
p := newSharedProxiedProject(t, bd, "wlo")
oldWisp := bdProxiedCreate(t, bd, p.dir, "Old wisp", "--ephemeral")
db := openProxiedDB(t, p)
if _, err := db.Exec("UPDATE wisps SET updated_at = ? WHERE id = ?",
time.Now().UTC().Add(-48*time.Hour), oldWisp.ID); err != nil {
t.Fatalf("backdate wisp: %v", err)
}
out, err := bdProxiedRun(t, bd, p.dir, "mol", "wisp", "list", "--json")
if err != nil {
t.Fatalf("bd mol wisp list --json: %v\n%s", err, out)
}
var got struct {
Wisps []struct {
ID string `json:"id"`
Old bool `json:"old"`
} `json:"wisps"`
OldCount int `json:"old_count"`
}
if err := json.Unmarshal(out, &got); err != nil {
t.Fatalf("unmarshal: %v\n%s", err, out)
}
if got.OldCount != 1 {
t.Errorf("old_count = %d, want 1", got.OldCount)
}
found := false
for _, w := range got.Wisps {
if w.ID == oldWisp.ID && w.Old {
found = true
}
}
if !found {
t.Errorf("expected wisp %s flagged old, got %+v", oldWisp.ID, got.Wisps)
}
})
}
func TestProxiedServerWispGC(t *testing.T) {
requireSharedProxiedServer(t)
t.Parallel()
bd := buildEmbeddedBD(t)
t.Run("age_threshold_cleans_abandoned_wisps", func(t *testing.T) {
t.Parallel()
p := newSharedProxiedProject(t, bd, "wg")
fresh := bdProxiedCreate(t, bd, p.dir, "Fresh wisp", "--ephemeral")
abandoned := bdProxiedCreate(t, bd, p.dir, "Abandoned wisp", "--ephemeral")
db := openProxiedDB(t, p)
if _, err := db.Exec("UPDATE wisps SET updated_at = ? WHERE id = ?",
time.Now().UTC().Add(-2*time.Hour), abandoned.ID); err != nil {
t.Fatalf("backdate wisp: %v", err)
}
out, err := bdProxiedRun(t, bd, p.dir, "mol", "wisp", "gc", "--age", "1h", "--json")
if err != nil {
t.Fatalf("bd mol wisp gc --age 1h --json: %v\n%s", err, out)
}
var got struct {
Deleted []string `json:"deleted"`
DeletedCount int `json:"deleted_count"`
}
if err := json.Unmarshal(out, &got); err != nil {
t.Fatalf("unmarshal: %v\n%s", err, out)
}
if got.DeletedCount != 1 || got.Deleted[0] != abandoned.ID {
t.Fatalf("expected only %s cleaned, got %+v", abandoned.ID, got)
}
var count int
if err := db.QueryRow("SELECT COUNT(*) FROM wisps WHERE id = ?", fresh.ID).Scan(&count); err != nil {
t.Fatalf("query fresh wisp: %v", err)
}
if count != 1 {
t.Errorf("expected fresh wisp %s to survive gc", fresh.ID)
}
})
t.Run("cascade_respects_age_for_dependent_step_wisps", func(t *testing.T) {
t.Parallel()
p := newSharedProxiedProject(t, bd, "wgc")
parentWisp := bdProxiedCreate(t, bd, p.dir, "Abandoned parent wisp", "--ephemeral")
staleChild := bdProxiedCreate(t, bd, p.dir, "Stale child wisp", "--ephemeral")
freshChild := bdProxiedCreate(t, bd, p.dir, "Fresh child wisp", "--ephemeral")
// Closed so that only the cascade can collect it: without --all the
// parent scan passes over closed wisps.
if out, err := bdProxiedRun(t, bd, p.dir, "close", staleChild.ID); err != nil {
t.Fatalf("bd close: %v\n%s", err, out)
}
db := openProxiedDB(t, p)
for _, childID := range []string{staleChild.ID, freshChild.ID} {
if _, err := db.Exec(
"INSERT INTO wisp_dependencies (id, issue_id, depends_on_wisp_id, type, created_at, created_by) VALUES (UUID(), ?, ?, ?, NOW(), 'test')",
childID, parentWisp.ID, "blocks"); err != nil {
t.Fatalf("plant child->parent wisp edge: %v", err)
}
}
if _, err := db.Exec("UPDATE wisps SET updated_at = ? WHERE id IN (?, ?)",
time.Now().UTC().Add(-2*time.Hour), parentWisp.ID, staleChild.ID); err != nil {
t.Fatalf("backdate parent and stale child wisps: %v", err)
}
out, err := bdProxiedRun(t, bd, p.dir, "mol", "wisp", "gc", "--age", "1h", "--json")
if err != nil {
t.Fatalf("bd mol wisp gc --age 1h --json: %v\n%s", err, out)
}
var got struct {
Deleted []string `json:"deleted"`
}
if err := json.Unmarshal(out, &got); err != nil {
t.Fatalf("unmarshal: %v\n%s", err, out)
}
cleaned := map[string]bool{}
for _, id := range got.Deleted {
cleaned[id] = true
}
if !cleaned[parentWisp.ID] {
t.Errorf("expected abandoned parent %s cleaned", parentWisp.ID)
}
if !cleaned[staleChild.ID] {
t.Errorf("expected stale dependent child %s cascaded", staleChild.ID)
}
if cleaned[freshChild.ID] {
t.Errorf("fresh child %s must survive gc despite stale parent, deleted: %v", freshChild.ID, got.Deleted)
}
var count int
if err := db.QueryRow("SELECT COUNT(*) FROM wisps WHERE id = ?", freshChild.ID).Scan(&count); err != nil {
t.Fatalf("query fresh child wisp: %v", err)
}
if count != 1 {
t.Errorf("expected fresh child %s to remain in wisps table", freshChild.ID)
}
})
t.Run("exclude_type_protects_wisps", func(t *testing.T) {
t.Parallel()
p := newSharedProxiedProject(t, bd, "wge")
if out, err := bdProxiedRun(t, bd, p.dir, "config", "set", "types.custom", "agent"); err != nil {
t.Fatalf("bd config set types.custom agent: %v\n%s", err, out)
}
protected := bdProxiedCreate(t, bd, p.dir, "Protected agent wisp", "--ephemeral", "--type", "agent")
unprotected := bdProxiedCreate(t, bd, p.dir, "Unprotected wisp", "--ephemeral")
db := openProxiedDB(t, p)
old := time.Now().UTC().Add(-2 * time.Hour)
if _, err := db.Exec("UPDATE wisps SET updated_at = ? WHERE id IN (?, ?)", old, protected.ID, unprotected.ID); err != nil {
t.Fatalf("backdate wisps: %v", err)
}
out, err := bdProxiedRun(t, bd, p.dir, "mol", "wisp", "gc", "--age", "1h", "--exclude-type", "agent", "--json")
if err != nil {
t.Fatalf("bd mol wisp gc --exclude-type agent --json: %v\n%s", err, out)
}
var got struct {
Deleted []string `json:"deleted"`
}
if err := json.Unmarshal(out, &got); err != nil {
t.Fatalf("unmarshal: %v\n%s", err, out)
}
cleaned := map[string]bool{}
for _, id := range got.Deleted {
cleaned[id] = true
}
if cleaned[protected.ID] {
t.Errorf("expected agent-type wisp %s to be protected", protected.ID)
}
if !cleaned[unprotected.ID] {
t.Errorf("expected unprotected wisp %s to be cleaned", unprotected.ID)
}
})
t.Run("closed_force_purges_regardless_of_age", func(t *testing.T) {
t.Parallel()
p := newSharedProxiedProject(t, bd, "wgcl")
closedWisp := bdProxiedCreate(t, bd, p.dir, "Just-closed wisp", "--ephemeral")
if out, err := bdProxiedRun(t, bd, p.dir, "close", closedWisp.ID); err != nil {
t.Fatalf("bd close: %v\n%s", err, out)
}
out, err := bdProxiedRun(t, bd, p.dir, "mol", "wisp", "gc", "--closed", "--force", "--json")
if err != nil {
t.Fatalf("bd mol wisp gc --closed --force --json: %v\n%s", err, out)
}
var got struct {
DeletedCount int `json:"deleted_count"`
}
if err := json.Unmarshal(out, &got); err != nil {
t.Fatalf("unmarshal: %v\n%s", err, out)
}
if got.DeletedCount != 1 {
t.Errorf("deleted_count = %d, want 1", got.DeletedCount)
}
db := openProxiedDB(t, p)
var count int
if err := db.QueryRow("SELECT COUNT(*) FROM wisps WHERE id = ?", closedWisp.ID).Scan(&count); err != nil {
t.Fatalf("query wisps: %v", err)
}
if count != 0 {
t.Errorf("expected closed wisp %s purged", closedWisp.ID)
}
})
}