beads/cmd/bd/list_format.go
matt wilkie bb6b8f226b
Fix ready/list UX regressions (#3481)
* fix(ready): honor filter.ExcludeTypes in GetReadyWorkInTx (GH#3397)

The --exclude-type CLI flag was silently ignored by bd ready. The CLI
correctly populated filter.ExcludeTypes, and the type's godoc promised
that values are "Appended to the default exclusion list", but the
no-Type branch of GetReadyWorkInTx built the NOT IN clause from the
hardcoded defaults only.

Merge user-supplied ExcludeTypes into the default exclusion list (with
dedup against defaults and empties) before building the placeholders.
This matches the pattern already used by issueops/filters.go and
dolt/queries.go for bd list.

Fixes GH#3397.

* fix(ready): --parent returns transitive descendants, not direct children only (GH#3396)

The SQL clause in GetReadyWorkInTx's ParentID branch was a one-hop join
against the parent-child dependency table, so only direct children
surfaced. The --parent help text in cmd/bd/ready.go:672 and the
WorkFilter.ParentID godoc (internal/types/types.go:1295) both promise
"descendants (recursive)".

Add a BFS helper GetDescendantIDsInTx alongside GetChildrenOfIssuesInTx
in blocked.go — BFS rather than a recursive CTE because the existing
children helper already iterates across both dependencies and
wisp_dependencies tables, and composing that inside a single CTE gets
ugly. The visited set breaks cycles and a depth cap (default 100,
matching the cycle-detection CTE elsewhere in issueops) guards against
malformed data.

Rewrite the ParentID branch in GetReadyWorkInTx to resolve descendants
via the new helper and OR that with the prior dotted-ID prefix match
(for implicit-parent IDs with no explicit parent-child dep), preserving
the original semantics for dotted IDs.

MoleculeID is deliberately left as one-hop — its inline comment
explicitly scopes it to "direct children" unlike ParentID.

Fixes GH#3396.

* Improve ready and list UX regressions (bd-main-typ)

* fix(list): make --ready flag use blocker-aware GetReadyWork semantics (GH#3478)

bd list --ready previously only filtered by status=open, showing issues
that bd ready correctly excluded as blocked. Now both commands use the
same GetReadyWork path, ensuring consistent results.

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

* Add list ready blocker regression coverage (bd-qo1)

* Fix ready list review regressions

---------

Co-authored-by: Daniel Jasiński <daniel.jasinski@gmail.com>
Co-authored-by: kev <kglynn@pryoninc.com>
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-26 20:29:30 -07:00

284 lines
10 KiB
Go

package main
import (
"context"
"encoding/json"
"fmt"
"strings"
"time"
"github.com/steveyegge/beads/internal/storage"
"github.com/steveyegge/beads/internal/timeparsing"
"github.com/steveyegge/beads/internal/types"
"github.com/steveyegge/beads/internal/ui"
)
// parseTimeFlag parses time strings using the layered time parsing architecture.
// Supports compact durations (+6h, -1d), natural language (tomorrow, next monday),
// and absolute formats (2006-01-02, RFC3339).
func parseTimeFlag(s string) (time.Time, error) {
return timeparsing.ParseRelativeTime(s, time.Now())
}
// pinIndicator returns a pushpin emoji prefix for pinned issues
func pinIndicator(issue *types.Issue) string {
if issue.Pinned {
return "📌 "
}
return ""
}
// Priority tags for pretty output - simple text, semantic colors applied via ui package
// Design principle: only P0/P1 get color for attention, P2-P4 are neutral
func renderPriorityTag(priority int) string {
return ui.RenderPriority(priority)
}
// renderStatusIcon returns the status icon with semantic coloring applied
// Delegates to the shared ui.RenderStatusIcon for consistency across commands
func renderStatusIcon(status types.Status) string {
return ui.RenderStatusIcon(string(status))
}
// formatPrettyIssue formats a single issue for pretty output
// Uses semantic colors: status icon colored, priority P0/P1 colored, rest neutral
func formatPrettyIssue(issue *types.Issue) string {
// Use shared helpers from ui package
statusIcon := ui.RenderStatusIcon(string(issue.Status))
priorityTag := renderPriorityTag(issue.Priority)
// Type badge - only show for notable types
typeBadge := ""
switch issue.IssueType {
case "epic":
typeBadge = ui.TypeEpicStyle.Render("[epic]") + " "
case "bug":
typeBadge = ui.TypeBugStyle.Render("[bug]") + " "
}
// Format: STATUS_ICON ID PRIORITY [Type] Title
// Priority uses ● icon with color, no brackets needed
// Closed issues: entire line is muted
if issue.Status == types.StatusClosed {
return fmt.Sprintf("%s %s %s %s%s",
statusIcon,
ui.RenderMuted(issue.ID),
ui.RenderMuted(fmt.Sprintf("● P%d", issue.Priority)),
ui.RenderMuted(string(issue.IssueType)),
ui.RenderMuted(" "+issue.Title))
}
return fmt.Sprintf("%s %s %s %s%s", statusIcon, issue.ID, priorityTag, typeBadge, issue.Title)
}
// formatPrettyIssueWithContext formats an issue with optional parent epic annotation
func formatPrettyIssueWithContext(issue *types.Issue, parentEpic string) string {
base := formatPrettyIssue(issue)
if parentEpic == "" {
return base
}
return base + " " + ui.RenderMuted("← "+parentEpic)
}
// formatIssueLong formats a single issue in long format to a buffer
func formatIssueLong(buf *strings.Builder, issue *types.Issue, labels []string) {
status := string(issue.Status)
if status == "closed" {
line := fmt.Sprintf("%s%s [P%d] [%s] %s\n %s",
pinIndicator(issue), issue.ID, issue.Priority,
issue.IssueType, status, issue.Title)
buf.WriteString(ui.RenderClosedLine(line))
buf.WriteString("\n")
} else {
buf.WriteString(fmt.Sprintf("%s%s [%s] [%s] %s\n",
pinIndicator(issue),
ui.RenderID(issue.ID),
ui.RenderPriority(issue.Priority),
ui.RenderType(string(issue.IssueType)),
ui.RenderStatus(status)))
buf.WriteString(fmt.Sprintf(" %s\n", issue.Title))
}
if issue.Assignee != "" {
buf.WriteString(fmt.Sprintf(" Assignee: %s\n", issue.Assignee))
}
if desc := strings.TrimSpace(issue.Description); desc != "" {
buf.WriteString(" Description:\n")
for _, line := range strings.Split(desc, "\n") {
buf.WriteString(fmt.Sprintf(" %s\n", line))
}
}
if len(labels) > 0 {
buf.WriteString(fmt.Sprintf(" Labels: %v\n", labels))
}
if hasCustomMetadata(issue) {
if n := countMetadataKeys(issue); n > 0 {
buf.WriteString(fmt.Sprintf(" Metadata: %d keys\n", n))
} else {
buf.WriteString(" Metadata: set\n")
}
}
buf.WriteString("\n")
}
// formatAgentIssue formats a single issue in ultra-compact agent mode format
// Output: "ID: Title" with optional dependency info "(parent: X, blocked by: Y, blocks: Z)"
func formatAgentIssue(buf *strings.Builder, issue *types.Issue, blockedBy, blocks []string, parent string) {
depInfo := formatDependencyInfo(blockedBy, blocks, parent)
if depInfo != "" {
buf.WriteString(fmt.Sprintf("%s: %s %s\n", issue.ID, issue.Title, depInfo))
} else {
buf.WriteString(fmt.Sprintf("%s: %s\n", issue.ID, issue.Title))
}
}
// formatDependencyInfo formats dependency info for list output.
// Parent-child deps are shown as "parent: X" (structural), separate from "blocked by" (blocking). (bd-hcxu)
// Returns "(parent: X, blocked by: Y, blocks: Z)" or "" if no dependencies.
func formatDependencyInfo(blockedBy, blocks []string, parent string) string {
if len(blockedBy) == 0 && len(blocks) == 0 && parent == "" {
return ""
}
var parts []string
if parent != "" {
parts = append(parts, fmt.Sprintf("parent: %s", parent))
}
if len(blockedBy) > 0 {
parts = append(parts, fmt.Sprintf("blocked by: %s", strings.Join(blockedBy, ", ")))
}
if len(blocks) > 0 {
parts = append(parts, fmt.Sprintf("blocks: %s", strings.Join(blocks, ", ")))
}
return "(" + strings.Join(parts, ", ") + ")"
}
// buildBlockingMaps builds maps of blocking dependencies from dependency records.
// Returns three maps: blockedByMap[issueID] = []IDs that block this issue,
// blocksMap[issueID] = []IDs that this issue blocks (excluding parent-child),
// and childrenMap[issueID] = []IDs that are children of this issue.
// Only includes dependencies where AffectsReadyWork() is true (blocks, parent-child, etc.)
//
// closedIDs is an optional set of issue IDs known to be closed. When provided,
// closed blockers are excluded from blockedByMap so that "blocked by" annotations
// only show active (open) blockers. This prevents stale annotations like
// "(blocked by: X)" when X has already been closed.
func buildBlockingMaps(allDeps map[string][]*types.Dependency, closedIDs map[string]bool) (blockedByMap, blocksMap, childrenMap map[string][]string) {
blockedByMap = make(map[string][]string)
blocksMap = make(map[string][]string)
childrenMap = make(map[string][]string)
for issueID, deps := range allDeps {
for _, dep := range deps {
// Only include blocking dependencies
if !dep.Type.AffectsReadyWork() {
continue
}
// Skip closed blockers in "blocked by" annotations — the dependency
// record is preserved, but a closed blocker no longer blocks work.
isClosed := closedIDs != nil && closedIDs[dep.DependsOnID]
if !isClosed {
blockedByMap[issueID] = append(blockedByMap[issueID], dep.DependsOnID)
}
// Separate parent-child from blocking relationships
if dep.Type == types.DepParentChild {
childrenMap[dep.DependsOnID] = append(childrenMap[dep.DependsOnID], issueID)
} else if !isClosed {
blocksMap[dep.DependsOnID] = append(blocksMap[dep.DependsOnID], issueID)
}
}
}
return blockedByMap, blocksMap, childrenMap
}
// getClosedBlockerIDs collects all unique blocker IDs from dependency records
// and returns the subset that are closed or unreachable. This is used to filter
// stale "blocked by" annotations in bd list output.
func getClosedBlockerIDs(ctx context.Context, s storage.DoltStorage, allDeps map[string][]*types.Dependency) map[string]bool {
// Collect unique blocker IDs
blockerIDs := make(map[string]bool)
for _, deps := range allDeps {
for _, dep := range deps {
if dep.Type.AffectsReadyWork() {
blockerIDs[dep.DependsOnID] = true
}
}
}
closedIDs := make(map[string]bool)
for id := range blockerIDs {
issue, err := s.GetIssue(ctx, id)
if err != nil || issue == nil {
// Treat missing or unreachable blockers as resolved — a blocker
// that cannot be fetched cannot block work, matching the behavior
// of computeBlockedIDs which only considers active issues.
closedIDs[id] = true
continue
}
if issue.Status == types.StatusClosed {
closedIDs[id] = true
}
}
return closedIDs
}
// formatIssueCompact formats a single issue in compact format to a buffer
// Uses status icons for better scanability - consistent with bd graph
// Format: [icon] [pin] ID [Priority] [Type] @assignee [labels] - Title (parent: X, blocked by: Y, blocks: Z)
func formatIssueCompact(buf *strings.Builder, issue *types.Issue, labels []string, blockedBy, blocks []string, parent string) {
labelsStr := ""
if len(labels) > 0 {
labelsStr = fmt.Sprintf(" %v", labels)
}
assigneeStr := ""
if issue.Assignee != "" {
assigneeStr = fmt.Sprintf(" @%s", issue.Assignee)
}
// Format dependency info
depInfo := formatDependencyInfo(blockedBy, blocks, parent)
if depInfo != "" {
depInfo = " " + depInfo
}
// Get styled status icon — override to blocked when issue has open blockers (GH#2858)
statusIcon := renderStatusIcon(issue.Status)
if len(blockedBy) > 0 && issue.Status == types.StatusOpen {
statusIcon = renderStatusIcon(types.StatusBlocked)
}
if issue.Status == types.StatusClosed {
// Closed issues: entire line muted (fades visually)
line := fmt.Sprintf("%s %s%s [P%d] [%s]%s%s - %s%s",
statusIcon, pinIndicator(issue), issue.ID, issue.Priority,
issue.IssueType, assigneeStr, labelsStr, issue.Title, depInfo)
buf.WriteString(ui.RenderClosedLine(line))
buf.WriteString("\n")
} else {
// Active issues: status icon + semantic colors for priority/type
buf.WriteString(fmt.Sprintf("%s %s%s [%s] [%s]%s%s - %s%s\n",
statusIcon,
pinIndicator(issue),
ui.RenderID(issue.ID),
ui.RenderPriority(issue.Priority),
ui.RenderType(string(issue.IssueType)),
assigneeStr, labelsStr, issue.Title, depInfo))
}
}
// hasCustomMetadata returns true if the issue has non-empty custom metadata.
func hasCustomMetadata(issue *types.Issue) bool {
if len(issue.Metadata) == 0 {
return false
}
trimmed := strings.TrimSpace(string(issue.Metadata))
return trimmed != "{}" && trimmed != "null"
}
// countMetadataKeys returns the number of top-level keys in the issue's metadata JSON.
func countMetadataKeys(issue *types.Issue) int {
var data map[string]json.RawMessage
if err := json.Unmarshal(issue.Metadata, &data); err != nil {
return 0
}
return len(data)
}