beads/cmd/bd/undefer.go
Athos Bernardes fc2351a51c
fix(defer): undefer clears a stray defer_until when status isn't deferred (ga-bq3w5) (#6390)
* fix(defer): undefer clears a stray defer_until when status isn't deferred (ga-bq3w5)

`bd undefer` gated entirely on issue.Status == StatusDeferred. But the
ready-work query hides any issue whose defer_until is in the future
regardless of status, so `bd update <id> --status open --defer <date>`
(an explicit --status wins over --defer's own status=deferred default)
leaves status=open with a live defer_until: permanently invisible to
`bd ready`, with no command able to clear it. `bd undefer` refused with a
technically-true, practically-misleading "is not deferred (status: open)"
and never touched the timestamp actually doing the hiding.

Gate on defer_until instead of status alone: only flip status to open
when the issue really was deferred (mirrors the guard GH#3233 already
gave `bd update --defer=""`), but always clear a stray defer_until so
the issue can return to `bd ready`.

Adds a regression test reproducing the exact shape (status=open + future
defer_until via a single `bd update --status open --defer ...` call).
Confirmed it fails against the pre-fix code and passes with the fix.

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

* fix(defer): mirror the undefer defer_until fix to the proxied-server route (ga-bq3w5)

PR review (bee-ghosttrack, round 1): the previous fix only patched the
embedded backend (undefer.go). runUndeferProxiedServer, used by
proxied-server/beads-team-server installs, still gated solely on
status == deferred and still overwrote status unconditionally — the
exact bug survives on that route, and CI treats it as first-class (15
green Proxied Dolt Cmd shards).

Mirror the same gate/write shape into defer_proxied_server.go: only
refuse when status isn't deferred AND there's no defer_until to clear;
only flip status to open when the issue really was deferred.

Also closes the reviewer's MINOR finding: the existing "open" fixture
can't distinguish the conditional status write from an unconditional
one (writing status=open to an already-open issue is a no-op). Adds
an in_progress case to both the embedded and the new proxied test —
confirmed by mutation that it fails against the unconditional-write
code and passes with the fix.

Shortens the undefer.go rationale comment and documents the
clear-without-clobbering behavior in the command's Long description,
per the reviewer's non-blocking nits.

Verified: go build/vet clean; gofmt clean; golangci-lint (native) 0
issues; BEADS_TEST_EMBEDDED_DOLT=1 go test -run TestEmbeddedUndefer
all 5 subtests PASS. The new proxied-server subtest could not be
executed in this sandbox (no Docker available for the shared Dolt
container), but go vet confirms it compiles and type-checks, and it
mirrors the embedded test byte-for-byte in structure. The windows
cross-lint step in `make ci-pr-lint` fails in this sandbox on files
untouched by this diff (confirmed byte-identical to origin/main) —
a pre-existing local environment gap, not a regression.

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

---------

Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-28 11:18:25 -07:00

124 lines
3.4 KiB
Go

package main
import (
"fmt"
"os"
"github.com/spf13/cobra"
"github.com/steveyegge/beads/internal/metrics"
"github.com/steveyegge/beads/internal/types"
"github.com/steveyegge/beads/internal/ui"
"github.com/steveyegge/beads/internal/utils"
)
var undeferCmd = &cobra.Command{
Use: "undefer [id...]",
Short: "Undefer one or more issues (restore to open)",
Long: `Undefer issues to restore them to open status.
This brings issues back from the icebox so they can be worked on again.
Issues will appear in 'bd ready' if they have no blockers.
If an issue carries a defer_until timestamp but its status isn't
"deferred" (e.g. after an explicit --status change), undefer clears
the stray timestamp without touching status.
Examples:
bd undefer bd-abc # Undefer a single issue
bd undefer bd-abc bd-def # Undefer multiple issues`,
Args: cobra.MinimumNArgs(1),
SilenceUsage: true,
SilenceErrors: true,
RunE: func(cmd *cobra.Command, args []string) error {
evt := metrics.NewCommandEvent("undefer")
defer func() {
if c := metrics.Global(); c != nil {
c.CloseEventAndAdd(evt)
}
}()
CheckReadonly("undefer")
if usesProxiedServer() {
return runUndeferProxiedServer(rootCtx, args)
}
ctx := rootCtx
_, err := utils.ResolvePartialIDs(ctx, store, args)
if err != nil {
return HandleError("%v", err)
}
undeferredIssues := []*types.Issue{}
if store == nil {
return HandleErrorWithHint("database not initialized", diagHint())
}
for _, id := range args {
fullID, err := utils.ResolvePartialID(ctx, store, id)
if err != nil {
fmt.Fprintf(os.Stderr, "Error resolving %s: %v\n", id, err)
continue
}
issue, err := store.GetIssue(ctx, fullID)
if err != nil {
fmt.Fprintf(os.Stderr, "Error getting %s: %v\n", fullID, err)
continue
}
// Gate on defer_until, not status alone (ga-bq3w5): bd ready hides
// any issue with a future defer_until regardless of status, so
// `bd update <id> --status open --defer <date>` leaves a status=open
// issue permanently invisible with no status-based signal anywhere.
// Mirrors GH#3233's `bd update --defer=""` gate (update.go): only
// flip status to open when it was actually "deferred" — other
// statuses shouldn't be clobbered just to clear a stray timestamp.
wasDeferred := issue.Status == types.StatusDeferred
if !wasDeferred && issue.DeferUntil == nil {
fmt.Fprintf(os.Stderr, "%s is not deferred (status: %s)\n", fullID, string(issue.Status))
continue
}
updates := map[string]interface{}{
"defer_until": nil,
}
if wasDeferred {
updates["status"] = string(types.StatusOpen)
}
if err := store.UpdateIssue(ctx, fullID, updates, actor); err != nil {
fmt.Fprintf(os.Stderr, "Error undeferring %s: %v\n", fullID, err)
continue
}
if jsonOutput {
issue, _ := store.GetIssue(ctx, fullID)
if issue != nil {
undeferredIssues = append(undeferredIssues, issue)
}
} else if wasDeferred {
fmt.Printf("%s Undeferred %s (now open)\n", ui.RenderPass("*"), fullID)
} else {
fmt.Printf("%s Cleared stale defer_until on %s (status unchanged: %s)\n", ui.RenderPass("*"), fullID, string(issue.Status))
}
}
if len(args) > 0 {
commandDidWrite.Store(true)
}
if jsonOutput && len(undeferredIssues) > 0 {
return outputJSON(undeferredIssues)
}
return nil
},
}
func init() {
undeferCmd.ValidArgsFunction = issueIDCompletion
rootCmd.AddCommand(undeferCmd)
}