fix(cmd/bd): purge/prune reject negative --older-than overflow, carry --limit in confirm hint (#7540)
daysAge only checked the upper bound (maxOlderThanDays) before multiplying by 24h. A day count negative enough overflows the int64-nanosecond multiplication into a small positive duration, so positiveAge's own check never sees the sign flip: `bd purge --older-than=-213503d --force` purged every closed row older than a day instead of being refused. Reject non-positive counts before the multiplication can run. Fixes gastownhall/beads#7524. Separately: `bd purge ... --limit N` (no --force) previews N deletions, but the confirmation's suggested --force command dropped --limit, so following it literally deleted every eligible row instead of the N the preview showed. Threaded limit through to the hint builder (now its own function, sweepConfirmHint, for a direct unit test) so the suggested command always matches the selection it previewed. Fixes gastownhall/beads#7534. Verified the overflow fix catches the regression directly: temporarily reverted just daysAge and confirmed the new -213503d/-213503 test rows fail without it, pass with it. go build/vet clean, golangci-lint's two findings are pre-existing in main.go, untouched by this change. Claude-Session: https://claude.ai/code/session_01VN2SgXAHW1eTB1izwWwmyr Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
parent
74a67c3c27
commit
d82c82d965
2 changed files with 62 additions and 5 deletions
|
|
@ -210,7 +210,7 @@ func runPurgeOrPrune(cmd *cobra.Command, scope purgeScope) error {
|
|||
case dryRun:
|
||||
return emitSweepDryRun(scope, limit, result)
|
||||
case !force:
|
||||
return emitSweepConfirm(scope, olderThan, pattern, result)
|
||||
return emitSweepConfirm(scope, olderThan, pattern, limit, result)
|
||||
}
|
||||
|
||||
commandDidWrite.Store(true)
|
||||
|
|
@ -354,7 +354,7 @@ func emitSweepDryRun(scope purgeScope, limit int, result issueops.SweepResult) e
|
|||
return nil
|
||||
}
|
||||
|
||||
func emitSweepConfirm(scope purgeScope, olderThan, pattern string, result issueops.SweepResult) error {
|
||||
func emitSweepConfirm(scope purgeScope, olderThan, pattern string, limit int, result issueops.SweepResult) error {
|
||||
fmt.Printf("Found %d %s(s) to %s\n", result.Swept, scope.subjectNoun, scope.cmdName)
|
||||
if result.Skipped.Pinned > 0 {
|
||||
fmt.Printf("Skipping %d pinned bead(s)\n", result.Skipped.Pinned)
|
||||
|
|
@ -365,6 +365,16 @@ func emitSweepConfirm(scope purgeScope, olderThan, pattern string, result issueo
|
|||
if result.Skipped.Referenced > 0 {
|
||||
fmt.Println(ui.MutedStyle.Render(fmt.Sprintf("Skipping %d referenced bead(s)", result.Skipped.Referenced)))
|
||||
}
|
||||
return HandleErrorWithHint(
|
||||
fmt.Sprintf("would %s %d bead(s)", scope.cmdName, result.Swept),
|
||||
fmt.Sprintf("Use --force to confirm or --dry-run to preview.\n %s", sweepConfirmHint(scope, olderThan, pattern, limit)))
|
||||
}
|
||||
|
||||
// sweepConfirmHint is the command the confirmation suggests: the caller's own
|
||||
// selection with --force added. Every selector the caller gave is carried,
|
||||
// --limit among them — a hint that dropped the limit previewed one deletion
|
||||
// and, followed, deleted every eligible row, no-history beads included.
|
||||
func sweepConfirmHint(scope purgeScope, olderThan, pattern string, limit int) string {
|
||||
hint := fmt.Sprintf("bd %s --force", scope.cmdName)
|
||||
if scope.tier == issueops.SweepWispsPlane {
|
||||
hint += " --wisps-plane"
|
||||
|
|
@ -375,9 +385,10 @@ func emitSweepConfirm(scope purgeScope, olderThan, pattern string, result issueo
|
|||
if pattern != "" {
|
||||
hint += " --pattern " + pattern
|
||||
}
|
||||
return HandleErrorWithHint(
|
||||
fmt.Sprintf("would %s %d bead(s)", scope.cmdName, result.Swept),
|
||||
fmt.Sprintf("Use --force to confirm or --dry-run to preview.\n %s", hint))
|
||||
if limit > 0 {
|
||||
hint += fmt.Sprintf(" --limit %d", limit)
|
||||
}
|
||||
return hint
|
||||
}
|
||||
|
||||
func emitSweepResult(scope purgeScope, limit int, result issueops.SweepResult) error {
|
||||
|
|
@ -472,6 +483,13 @@ const maxOlderThanDays = math.MaxInt64 / int64(24*time.Hour)
|
|||
var errOlderThanRange = fmt.Errorf("duration out of range (at most %d days)", maxOlderThanDays)
|
||||
|
||||
func daysAge(days int64) (time.Duration, error) {
|
||||
// Only the upper bound was checked here; a day count negative enough
|
||||
// overflows the int64-nanosecond multiplication into a small positive
|
||||
// duration, so positiveAge's own check below never sees the sign flip.
|
||||
// Reject non-positive counts before the multiplication can run.
|
||||
if days <= 0 {
|
||||
return 0, fmt.Errorf("duration must be positive")
|
||||
}
|
||||
if days > maxOlderThanDays {
|
||||
return 0, errOlderThanRange
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,8 @@ package main
|
|||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/steveyegge/beads/issueops"
|
||||
)
|
||||
|
||||
// TestParseOlderThan pins --older-than on both `bd purge` and `bd prune`.
|
||||
|
|
@ -51,6 +53,13 @@ func TestParseOlderThan(t *testing.T) {
|
|||
{"106752d", 0, true},
|
||||
{"9223372036854775807d", 0, true},
|
||||
{"99999999999h", 0, true},
|
||||
// A negative count far enough past the floor must be refused too,
|
||||
// never wrapped: -213503d used to overflow into a small positive
|
||||
// duration (~23h35m) and purge almost everything instead of being
|
||||
// rejected outright.
|
||||
{"-213503d", 0, true},
|
||||
{"-213503", 0, true},
|
||||
{"-9223372036854775808", 0, true},
|
||||
// The largest representable day count still parses.
|
||||
{"106751d", 106751 * day, false},
|
||||
}
|
||||
|
|
@ -67,3 +76,33 @@ func TestParseOlderThan(t *testing.T) {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestSweepConfirmHintCarriesEverySelector pins the no-force confirmation's
|
||||
// suggested command against dropping a selector the caller gave: a hint that
|
||||
// dropped --limit previewed one deletion and, followed literally, deleted
|
||||
// every eligible row instead of the one the preview showed.
|
||||
func TestSweepConfirmHintCarriesEverySelector(t *testing.T) {
|
||||
purge := purgeScope{cmdName: "purge", tier: issueops.SweepWispsPlane}
|
||||
prune := purgeScope{cmdName: "prune", tier: issueops.SweepDurable}
|
||||
tests := []struct {
|
||||
name string
|
||||
scope purgeScope
|
||||
olderThan string
|
||||
pattern string
|
||||
limit int
|
||||
want string
|
||||
}{
|
||||
{"the gate's command", purge, "168h", "", 1, "bd purge --force --wisps-plane --older-than 168h --limit 1"},
|
||||
{"a larger limit", purge, "7d", "", 500, "bd purge --force --wisps-plane --older-than 7d --limit 500"},
|
||||
{"no limit asked", purge, "168h", "", 0, "bd purge --force --wisps-plane --older-than 168h"},
|
||||
{"pattern and limit", purge, "", "wi-*", 3, "bd purge --force --wisps-plane --pattern wi-* --limit 3"},
|
||||
{"prune defines no limit", prune, "30d", "", 0, "bd prune --force --older-than 30d"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := sweepConfirmHint(tt.scope, tt.olderThan, tt.pattern, tt.limit); got != tt.want {
|
||||
t.Errorf("sweepConfirmHint() = %q, want %q", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue