beads/cmd/bd/prune.go
Jim Wordelman 8b691beaea
feat(prune): add bd prune for deleting closed non-ephemeral beads (#3353)
* feat(prune): add `bd prune` for deleting closed non-ephemeral beads

`bd purge` is scoped to closed ephemeral beads (wisps, transient
molecules). That leaves no built-in way to trim the usual long-lived
cruft: closed tasks/features/bugs/chores that stick around forever,
bloat auto-export, and make `bd list` and other queries slower over
time. Users who don't care about closed history eventually want a way
to drop it.

Add `bd prune` with the same surface as `bd purge` but the opposite
scope — closed non-ephemeral beads.

- Extracted runPurgeOrPrune from the purgeCmd closure. Both commands
  now share filter plumbing, preview/dry-run/force semantics, stats
  printing, and embedded-mode commit-flush. The two cobra commands
  differ only in name, scope, messaging, and safety gate.
- purge's filter stays `Ephemeral: &true`; prune's is `Ephemeral:
  &false`. Scopes are deliberately disjoint: prune never touches what
  purge would, and vice versa.
- Safety gate: `bd prune` requires `--older-than` or `--pattern`. A
  muscle-memory `bd prune --force` would otherwise wipe every closed
  bead in the repo. `--older-than 1d` is the documented escape hatch
  for "sweep everything closed for at least a day".
- Shared flags: --force, --dry-run, --older-than, --pattern. Same
  dependency/label/event reporting, pinned-bead skipping, and
  commandDidWrite tracking as purge.

`bd purge --help` now cross-references `bd prune` (and vice versa) so
users find the right command for their scope.

Tests (cmd/bd/prune_embedded_test.go, gated by BEADS_TEST_EMBEDDED_DOLT=1
like the existing purge tests):

- prune_requires_older_than_or_pattern — safety gate rejects bare
  `--force` and leaves data intact.
- prune_deletes_closed_non_ephemeral — happy path: scoped prune
  removes the target.
- prune_leaves_closed_ephemeral_for_purge — cross-scope invariant:
  prune must not touch wisps; they remain visible to `bd purge`.
- prune_leaves_open_beads_alone — open beads survive even under a
  matching pattern.
- prune_dry_run_reports_without_deleting — dry-run prints preview
  and keeps data intact.
- prune_preview_fails_without_force — preview mode (no --force)
  aborts with a preview message, no data lost.
- prune_older_than_skips_recent — `--older-than 30d` excludes
  just-closed beads.

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

* Fix prune test and polish output

Replace the broken list --ephemeral assertion with a purge dry-run check, and make prune's JSON and help text match its scope.

Co-authored-by: Jim Wordelman <91582+quad341@users.noreply.github.com>

* Tighten prune ephemeral assertion

Assert prune skips closed wisps by scope without relying on a later purge process that auto-imports from issues.jsonl.

Co-authored-by: Jim Wordelman <91582+quad341@users.noreply.github.com>

---------

Co-authored-by: Jim Wordelman <jim@wordelman.name>
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Co-authored-by: matt wilkie <maphew@gmail.com>
Co-authored-by: Jim Wordelman <91582+quad341@users.noreply.github.com>
2026-04-24 06:44:43 -07:00

55 lines
2.2 KiB
Go

package main
import (
"github.com/spf13/cobra"
)
var pruneCmd = &cobra.Command{
Use: "prune",
GroupID: "maint",
Short: "Delete old closed beads to reclaim space and shrink exports",
Long: `Permanently delete closed non-ephemeral beads and their associated data.
Use this to trim closed regular beads (tasks, features, bugs, chores, etc.)
that are no longer useful. The common case is a long-lived repo where
closed work has piled up and is bloating auto-export or slowing queries.
Requires --older-than or --pattern. The flag is a safety gate — without
it, a muscle-memory ` + "`--force`" + ` could wipe every closed bead in the repo.
Use ` + "`--pattern '*'`" + ` if you really do want to sweep everything closed.
Deletes: issues, dependencies, labels, events, and comments for matching beads.
Skips: pinned beads (protected), open/in-progress beads, and ephemeral beads.
To delete closed ephemeral beads (wisps, transient molecules) use
` + "`bd purge`" + ` instead.
For full Dolt storage reclaim after deleting many rows, follow with ` + "`bd flatten`" + `
so history can be collapsed and old chunks can be garbage-collected.
EXAMPLES:
bd prune --older-than 30d # Preview closed beads >30d old
bd prune --older-than 30d --force # Delete them
bd prune --older-than 90d --dry-run # Detailed preview with stats
bd prune --pattern "*" --force # Delete all closed regular beads
bd prune --pattern "gm-temp-*" --force # Scope to a pattern`,
Run: func(cmd *cobra.Command, _ []string) {
runPurgeOrPrune(cmd, purgeScope{
cmdName: "prune",
pastTense: "pruned",
countKey: "pruned_count",
dryRunCountKey: "prune_count",
subjectNoun: "closed bead",
ephemeralOnly: false,
requireFilter: true,
})
},
}
func init() {
pruneCmd.Flags().BoolP("force", "f", false, "Actually prune (without this, shows preview)")
pruneCmd.Flags().Bool("dry-run", false, "Preview what would be pruned with stats")
pruneCmd.Flags().String("older-than", "", "Only prune beads closed more than N ago (e.g., 30d, 2w, 60)")
pruneCmd.Flags().String("pattern", "", "Only prune beads matching ID glob pattern (e.g., 'gm-old-*')")
rootCmd.AddCommand(pruneCmd)
}