* feat(cli): add --if-revision compare-and-swap guard to update/close/assign/delete Adds --if-revision <int64> to `bd update`, `bd close`, `bd assign` and `bd delete`, reusing the existing opaque row_lock token published as IssueDetails.Revision and the library's own ExpectedVersion compare-and-swap support. A mismatch writes nothing and exits 13 (ExitGuardMismatch); with --json the stderr envelope carries integer expected_revision/current_revision fields deployed callers decode directly, never a quoted string. An unsupported backend reports conditional_write_unsupported and exits 1 instead of silently dropping the guard. The guard composes with --if-assignee/--if-status (every guard must hold) and is refused alongside label or parent edits and alongside more than one id, matching the existing --if-assignee rules. `bd assign` is newly routed through the checked Lifecycle path to gain CAS support it previously had none of at all. Implements Item 4 (compare-and-swap through the CLI, gastownhall/beads#4682) from the BEADS-JOURNAL-PLAN. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> * chore(bazel): regenerate cmd/bd BUILD.bazel for if_revision/close_if_revision gazelle hadn't picked up if_revision.go, close_if_revision.go, if_revision_test.go, and if_revision_embedded_test.go, so Bazel's bd_lib and bd_test targets failed to build with "undefined: parseIfRevisionFlag" and friends while plain `go build`/`go test` worked fine. Ran `make bazel-sync` (bazel run //:gazelle; go_srcs.py; bazel mod tidy) to pick up the new files; only cmd/bd/BUILD.bazel's srcs lists changed. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> * test(cli): update update-guard parity strings for --if-revision TestParityUpdateGuardsRequireFieldUpdate and TestParityUpdateGuardsRejectClaim pinned the exact stderr text of update.go's pre-write guard short-circuits. Adding --if-revision to those guard messages (update.go/update_input.go) changed "--if-assignee/--if-status" to "--if-assignee/--if-status/--if-revision" in both, which this PR's own change to the guard text had not updated here — caught by CI's dolt-server tier, not by the targeted if_revision* test runs. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> --------- Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
193 lines
5.8 KiB
Go
193 lines
5.8 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/spf13/cobra"
|
|
"github.com/steveyegge/beads/internal/metrics"
|
|
"github.com/steveyegge/beads/internal/types"
|
|
"github.com/steveyegge/beads/internal/ui"
|
|
)
|
|
|
|
// CleanupEmptyResponse is returned when there are no closed issues to delete
|
|
type CleanupEmptyResponse struct {
|
|
DeletedCount int `json:"deleted_count"`
|
|
Message string `json:"message"`
|
|
Filter string `json:"filter,omitempty"`
|
|
Ephemeral bool `json:"ephemeral,omitempty"`
|
|
}
|
|
|
|
var cleanupCmd = &cobra.Command{
|
|
Use: "cleanup",
|
|
Short: "Delete closed issues to reduce database size",
|
|
Long: `Delete closed issues to reduce database size.
|
|
|
|
This command permanently removes closed issues from the database.
|
|
|
|
NOTE: This command only manages issue lifecycle (closed -> deleted). For general
|
|
health checks and automatic repairs, use 'bd doctor --fix' instead.
|
|
|
|
By default, deletes ALL closed issues. Use --older-than to only delete
|
|
issues closed before a certain date.
|
|
|
|
EXAMPLES:
|
|
bd admin cleanup --force # Delete all closed issues
|
|
bd admin cleanup --older-than 30 --force # Only issues closed 30+ days ago
|
|
bd admin cleanup --ephemeral --force # Only closed wisps (transient molecules)
|
|
bd admin cleanup --dry-run # Preview what would be deleted
|
|
|
|
SAFETY:
|
|
- Requires --force flag to actually delete (unless --dry-run)
|
|
- Supports --cascade to delete dependents
|
|
- Shows preview of what will be deleted
|
|
- Use --json for programmatic output
|
|
|
|
SEE ALSO:
|
|
bd doctor --fix Automatic health checks and repairs (recommended for routine maintenance)
|
|
bd admin compact Compact old closed issues to save space`,
|
|
SilenceUsage: true,
|
|
SilenceErrors: true,
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
if usesProxiedServer() {
|
|
return HandleErrorRespectJSON("admin cleanup is not supported in proxied-server mode")
|
|
}
|
|
evt := metrics.NewCommandEvent("admin-cleanup")
|
|
defer func() {
|
|
if c := metrics.Global(); c != nil {
|
|
c.CloseEventAndAdd(evt)
|
|
}
|
|
}()
|
|
|
|
if err := requireServerMode("cleanup"); err != nil {
|
|
return HandleError("%v", err)
|
|
}
|
|
force, _ := cmd.Flags().GetBool("force")
|
|
dryRun, _ := cmd.Flags().GetBool("dry-run")
|
|
cascade, _ := cmd.Flags().GetBool("cascade")
|
|
olderThanDays, _ := cmd.Flags().GetInt("older-than")
|
|
wispOnly, _ := cmd.Flags().GetBool("ephemeral")
|
|
|
|
if store == nil {
|
|
if err := ensureStoreActive(); err != nil {
|
|
return HandleError("%v", err)
|
|
}
|
|
}
|
|
|
|
ctx := rootCtx
|
|
|
|
// Build filter for closed issues. Cleanup is a scripted sweep — opt out
|
|
// of BEADS_MAX_ROWS (designer §4.1) so a misconfigured env doesn't abort
|
|
// cleanup mid-run and leave the database in an unswept state.
|
|
statusClosed := types.StatusClosed
|
|
filter := types.IssueFilter{
|
|
Status: &statusClosed,
|
|
MaxRows: 0,
|
|
MaxRowsSource: "",
|
|
}
|
|
|
|
if olderThanDays > 0 {
|
|
cutoffTime := time.Now().AddDate(0, 0, -olderThanDays)
|
|
filter.ClosedBefore = &cutoffTime
|
|
}
|
|
|
|
if wispOnly {
|
|
wispTrue := true
|
|
filter.Ephemeral = &wispTrue
|
|
}
|
|
|
|
closedIssues, err := store.SearchIssues(ctx, "", filter)
|
|
if err != nil {
|
|
return HandleError("listing issues: %v", err)
|
|
}
|
|
|
|
pinnedCount := 0
|
|
filteredIssues := make([]*types.Issue, 0, len(closedIssues))
|
|
for _, issue := range closedIssues {
|
|
if issue.Pinned {
|
|
pinnedCount++
|
|
continue
|
|
}
|
|
filteredIssues = append(filteredIssues, issue)
|
|
}
|
|
closedIssues = filteredIssues
|
|
|
|
if pinnedCount > 0 && !jsonOutput {
|
|
fmt.Printf("Skipping %d pinned issue(s) (protected from cleanup)\n", pinnedCount)
|
|
}
|
|
|
|
if len(closedIssues) == 0 {
|
|
if jsonOutput {
|
|
result := CleanupEmptyResponse{
|
|
DeletedCount: 0,
|
|
Message: "No closed issues to delete",
|
|
}
|
|
if olderThanDays > 0 {
|
|
result.Filter = fmt.Sprintf("older than %d days", olderThanDays)
|
|
}
|
|
if wispOnly {
|
|
result.Ephemeral = true
|
|
}
|
|
if err := outputJSON(result); err != nil {
|
|
return err
|
|
}
|
|
} else {
|
|
msg := "No closed issues to delete"
|
|
if wispOnly && olderThanDays > 0 {
|
|
msg = fmt.Sprintf("No closed wisps older than %d days to delete", olderThanDays)
|
|
} else if wispOnly {
|
|
msg = "No closed wisps to delete"
|
|
} else if olderThanDays > 0 {
|
|
msg = fmt.Sprintf("No closed issues older than %d days to delete", olderThanDays)
|
|
}
|
|
fmt.Println(msg)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
issueIDs := make([]string, len(closedIssues))
|
|
for i, issue := range closedIssues {
|
|
issueIDs[i] = issue.ID
|
|
}
|
|
|
|
if !force && !dryRun {
|
|
issueType := "closed"
|
|
if wispOnly {
|
|
issueType = "closed wisp"
|
|
}
|
|
return HandleErrorWithHint(
|
|
fmt.Sprintf("would delete %d %s issue(s)", len(issueIDs), issueType),
|
|
"Use --force to confirm or --dry-run to preview.")
|
|
}
|
|
|
|
if !jsonOutput {
|
|
issueType := "closed"
|
|
if wispOnly {
|
|
issueType = "closed wisp"
|
|
}
|
|
if olderThanDays > 0 {
|
|
fmt.Printf("Found %d %s issue(s) older than %d days\n", len(closedIssues), issueType, olderThanDays)
|
|
} else {
|
|
fmt.Printf("Found %d %s issue(s)\n", len(closedIssues), issueType)
|
|
}
|
|
if dryRun {
|
|
fmt.Println(ui.RenderWarn("DRY RUN - no changes will be made"))
|
|
}
|
|
fmt.Println()
|
|
}
|
|
|
|
if err := deleteBatch(cmd, issueIDs, force, dryRun, cascade, jsonOutput, false, nil, "cleanup"); err != nil {
|
|
return HandleError("%v", err)
|
|
}
|
|
return nil
|
|
},
|
|
}
|
|
|
|
func init() {
|
|
cleanupCmd.Flags().BoolP("force", "f", false, "Actually delete (without this flag, shows error)")
|
|
cleanupCmd.Flags().Bool("dry-run", false, "Preview what would be deleted without making changes")
|
|
cleanupCmd.Flags().Bool("cascade", false, "Recursively delete all dependent issues")
|
|
cleanupCmd.Flags().Int("older-than", 0, "Only delete issues closed more than N days ago (0 = all closed issues)")
|
|
cleanupCmd.Flags().Bool("ephemeral", false, "Only delete closed wisps (transient molecules)")
|
|
// Note: cleanupCmd is added to adminCmd in admin.go
|
|
}
|