From 3ba08dba1cd3e23e55f70109a2b3c703cb1e76d4 Mon Sep 17 00:00:00 2001 From: Vishnu J Date: Sat, 26 Sep 2026 13:58:55 -0700 Subject: [PATCH] fix(storage): cascade wisp aux rows in the shared wisp delete path (#6487) (#6627) deleteIssueRowInTx, behind Transaction.DeleteIssue (bd mol burn) and DeleteResolvedSetInTx, removed only the wisps row and the dependencies edges, so on stores lacking the wisp aux FKs every deleted wisp orphaned its wisp_labels, wisp_events, wisp_comments and wisp_child_counters rows. #5343 fixed only the DoltStore wisp GC paths. Move the aux table list and delete into issueops.DeleteWispAuxRowsInTx, call it from deleteIssueRowInTx, and have the DoltStore paths use the same helper so the table set cannot drift. Forward-fix only: cleaning existing orphans needs a batched backfill and is left to a follow-up. Sibling of #6563 (wisp_dependencies). Co-authored-by: Claude Sonnet 5 --- ...wisp_delete_cascade_tx_integration_test.go | 61 +++++++++++++++++++ internal/storage/dolt/wisps.go | 38 +----------- internal/storage/issueops/delete.go | 37 +++++++++++ .../issueops/journal_completeness_test.go | 1 + 4 files changed, 101 insertions(+), 36 deletions(-) create mode 100644 internal/storage/dolt/wisp_delete_cascade_tx_integration_test.go diff --git a/internal/storage/dolt/wisp_delete_cascade_tx_integration_test.go b/internal/storage/dolt/wisp_delete_cascade_tx_integration_test.go new file mode 100644 index 000000000..6a51bf8ae --- /dev/null +++ b/internal/storage/dolt/wisp_delete_cascade_tx_integration_test.go @@ -0,0 +1,61 @@ +//go:build integration && !windows + +package dolt + +import ( + "context" + "database/sql" + "fmt" + "testing" + "time" + + "github.com/steveyegge/beads/internal/storage" +) + +// TestWispDeleteCascade_TransactionDeleteIssue covers the delete path #5343 +// left uncovered: Transaction.DeleteIssue (what `bd mol burn` uses) goes +// through issueops.DeleteIssueInTx -> deleteIssueRowInTx rather than +// deleteWisp/deleteWispBatchTx, and removed only the wisps row and the +// dependencies edges. On stores without the wisp aux FKs (dropped by +// setupWispCascadeStore) every burned wisp orphaned its wisp_labels, +// wisp_events, wisp_comments and wisp_child_counters rows (#6487). +func TestWispDeleteCascade_TransactionDeleteIssue(t *testing.T) { + store := setupWispCascadeStore(t) + ctx, cancel := context.WithTimeout(context.Background(), time.Minute) + defer cancel() + + wisp := createTestWisp(t, ctx, store, "tx-delete wisp") + other := createTestWisp(t, ctx, store, "tx-delete bystander wisp") + seedWispAuxRows(t, ctx, store.db, wisp.ID) + seedWispAuxRows(t, ctx, store.db, other.ID) + bystanderBefore := make([]int, len(wispAuxTables)) + for i, tc := range wispAuxTables { + bystanderBefore[i] = countWispAuxRows(t, ctx, store.db, tc.table, tc.column, other.ID) + } + + if err := store.RunInTransaction(ctx, "test: burn wisp", func(tx storage.Transaction) error { + return tx.DeleteIssue(ctx, wisp.ID) + }); err != nil { + t.Fatalf("Transaction.DeleteIssue: %v", err) + } + + assertWispAuxRowsGone(t, ctx, store.db, wisp.ID) + + // The delete must be scoped to the deleted wisp: a bystander's rows stay. + for i, tc := range wispAuxTables { + if got := countWispAuxRows(t, ctx, store.db, tc.table, tc.column, other.ID); got != bystanderBefore[i] { + t.Errorf("bystander wisp %s: %s rows changed from %d to %d", other.ID, tc.table, bystanderBefore[i], got) + } + } +} + +func countWispAuxRows(t *testing.T, ctx context.Context, db *sql.DB, table, column, id string) int { + t.Helper() + var count int + //nolint:gosec // G201: table/column come from the fixed wispAuxTables literal, not input. + q := fmt.Sprintf("SELECT COUNT(*) FROM %s WHERE %s = ?", table, column) + if err := db.QueryRowContext(ctx, q, id).Scan(&count); err != nil { + t.Fatalf("count %s: %v", table, err) + } + return count +} diff --git a/internal/storage/dolt/wisps.go b/internal/storage/dolt/wisps.go index 3cd08ab0a..d49f21725 100644 --- a/internal/storage/dolt/wisps.go +++ b/internal/storage/dolt/wisps.go @@ -294,40 +294,6 @@ func (s *DoltStore) closeWispChecked(ctx context.Context, id string, actor strin return storage.CloseIssueResult{Unchanged: res.AlreadyClosed, OpenChildren: res.OpenChildren}, nil } -// wispAuxCascadeTables lists the wisp auxiliary tables a wisp delete must -// also clean up, mirroring internal/storage/schema/cli_migrations.go:300-315. -// wisp_child_counters is keyed on parent_id (a wisp can be a parent whose -// children hold the counter row); the other three are keyed on issue_id. -// Some deployed stores enforce this via FK ON DELETE CASCADE and some do not -// (be-zdqyl: the migration adding those FKs was never promoted out of -// migrations/ignored/), so the delete paths below must not rely on the -// database to do it for them. -var wispAuxCascadeTables = []struct{ table, column string }{ - {"wisp_labels", "issue_id"}, - {"wisp_events", "issue_id"}, - {"wisp_comments", "issue_id"}, - {"wisp_child_counters", "parent_id"}, -} - -// deleteWispAuxRowsInTx removes every row the given wisp ids own across -// wispAuxCascadeTables. Shared by deleteWisp and deleteWispBatchTx so the -// table set cannot drift between the two paths. -func deleteWispAuxRowsInTx(ctx context.Context, tx *sql.Tx, ids []string) error { - if len(ids) == 0 { - return nil - } - inClause, args := doltBuildSQLInClause(ids) - for _, aux := range wispAuxCascadeTables { - //nolint:gosec // G201: aux.table/aux.column come from the fixed wispAuxCascadeTables literal; inClause contains only ? markers - if _, err := tx.ExecContext(ctx, - fmt.Sprintf("DELETE FROM %s WHERE %s IN (%s)", aux.table, aux.column, inClause), - args...); err != nil { - return fmt.Errorf("delete wisp aux rows from %s: %w", aux.table, err) - } - } - return nil -} - // deleteWisp permanently removes a wisp and its related data. func (s *DoltStore) deleteWisp(ctx context.Context, id string) error { tx, err := s.db.BeginTx(ctx, nil) @@ -374,7 +340,7 @@ func (s *DoltStore) deleteWisp(ctx context.Context, id string) error { return err } - if err := deleteWispAuxRowsInTx(ctx, tx, []string{id}); err != nil { + if err := issueops.DeleteWispAuxRowsInTx(ctx, tx, []string{id}); err != nil { return fmt.Errorf("delete wisp aux rows for %s: %w", id, err) } @@ -475,7 +441,7 @@ func (s *DoltStore) deleteWispBatchTx(ctx context.Context, ids []string) (int, e return 0, err } - if err := deleteWispAuxRowsInTx(ctx, tx, ids); err != nil { + if err := issueops.DeleteWispAuxRowsInTx(ctx, tx, ids); err != nil { return 0, fmt.Errorf("delete wisp aux rows: %w", err) } diff --git a/internal/storage/issueops/delete.go b/internal/storage/issueops/delete.go index f8ed23ad5..09105789c 100644 --- a/internal/storage/issueops/delete.go +++ b/internal/storage/issueops/delete.go @@ -84,6 +84,9 @@ func deleteIssueRowInTx(ctx context.Context, tx *sql.Tx, id string, isWisp bool, if err := DeleteWispFromDependenciesInTx(ctx, tx, id); err != nil { return err } + if err := DeleteWispAuxRowsInTx(ctx, tx, []string{id}); err != nil { + return err + } } else if err := DeleteLeaseInTx(ctx, tx, id); err != nil { // A deleted issue holds no lease. return err @@ -91,6 +94,40 @@ func deleteIssueRowInTx(ctx context.Context, tx *sql.Tx, id string, isWisp bool, return nil } +// wispAuxCascadeTables lists the wisp auxiliary tables a wisp delete must also +// clean up, mirroring internal/storage/schema/cli_migrations.go:300-315. +// wisp_child_counters is keyed on parent_id (a wisp can be a parent whose +// children hold the counter row); the other three are keyed on issue_id. +// Some deployed stores enforce this via FK ON DELETE CASCADE and some do not +// (be-zdqyl: the migration adding those FKs was never promoted out of +// migrations/ignored/), so the delete paths must not rely on the database to +// do it for them. wisp_dependencies is cleaned by DeleteWisp(s)FromDependenciesInTx. +var wispAuxCascadeTables = []struct{ table, column string }{ + {"wisp_labels", "issue_id"}, + {"wisp_events", "issue_id"}, + {"wisp_comments", "issue_id"}, + {"wisp_child_counters", "parent_id"}, +} + +// DeleteWispAuxRowsInTx removes every row the given wisp ids own across +// wispAuxCascadeTables. Shared by every wisp delete path (the DoltStore wisp +// GC paths and deleteIssueRowInTx) so the table set cannot drift between them. +func DeleteWispAuxRowsInTx(ctx context.Context, tx *sql.Tx, wispIDs []string) error { + if len(wispIDs) == 0 { + return nil + } + inClause, args := buildSQLInClause(wispIDs) + for _, aux := range wispAuxCascadeTables { + //nolint:gosec // G201: aux.table/aux.column come from the fixed wispAuxCascadeTables literal; inClause contains only ? placeholders + if _, err := tx.ExecContext(ctx, + fmt.Sprintf("DELETE FROM %s WHERE %s IN (%s)", aux.table, aux.column, inClause), + args...); err != nil { + return fmt.Errorf("delete wisp aux rows from %s: %w", aux.table, err) + } + } + return nil +} + // DeletionSet is the EXACT set of rows one delete removes, split by the plane // each row lives in. // diff --git a/internal/storage/issueops/journal_completeness_test.go b/internal/storage/issueops/journal_completeness_test.go index 9e28558cd..d14458524 100644 --- a/internal/storage/issueops/journal_completeness_test.go +++ b/internal/storage/issueops/journal_completeness_test.go @@ -170,6 +170,7 @@ var beadDMLExemptions = map[string]string{ "RetargetInboundDependenciesToIssueInTx": "rewrites dep rows during promote; PromoteFromEphemeralInTx journals it", "DeleteWispFromDependenciesInTx": "cleans up dep rows during a delete that journals the delete", "DeleteWispsFromDependenciesInTx": "cleans up dep rows during a delete that journals the delete", + "DeleteWispAuxRowsInTx": "cleans up wisp label/event/comment/counter rows during a delete that journals the delete", // (4) compaction maintenance — a lossy content rewrite outside the // create/update/close/delete/dep/label op vocabulary. Carried from the