beads/cmd/bd/count.go
Karel Bourgois e8f318316f
feat(count): --include-ephemeral, the plane knob bd count never had (#6240)
* feat(count): --include-ephemeral, the plane knob `bd count` never had

`bd count` can admit the wisps table only through `--include-infra`, which
makes four changes at once. This adds the flag that makes just the first one,
mirroring the `--include-ephemeral` that `bd list` gained in #6098.

## Why the plane is unreachable on its own

The write path routes on STORAGE CLASS, not type — dolt's `useWispsTable` is
`Ephemeral || NoHistory || WispType != "" || IsInfraType`. So a `no_history`
task lives in the wisps TABLE while remaining ordinary durable work, and a
default `bd count` never sees it.

Recovering it today means `--include-infra`, which this repo's own doc calls
"FOUR changes at once and not one": it admits the plane, drops the infra-type
exclusions, changes the template treatment, and reconciles cardinality with
`bd list --all`. Its template exclusion then silently drops template rows of
the named type — so a caller who wanted the plane trades one silent undercount
for another.

`ListRequest.IncludeEphemeral` has been the narrow answer on the listing side
since #5476, and #6098 gave it a CLI flag. `CountRequest` had no equivalent
field at all. This is the missing half of that pair.

## The change

- `issueops.CountRequest.IncludeEphemeral` — the plane knob alone: exactly the
  first of `IncludeInfra`'s four changes, with none of the other three.
- `internal/workapi/count.go` — one line, `} else if !in.IncludeEphemeral {`.
- `cmd/bd/count.go` — flag registration, matching `bd list`'s wording.
- `internal/httpapi` — `include_ephemeral` on `countIssues`, in the spec; the
  generated `types.gen.go` is `make api-gen` output, not hand-edited.

Unset, the count is durable-plane only: the historical answer, unchanged. Every
default is untouched.

It does NOT promise a count and a listing agree. They already differ on
TEMPLATES without this flag and still do with it — a count includes template
rows unless `IncludeInfra` excludes them, a listing excludes them unless
`IncludeTemplates` admits them. That predates this and is unchanged by it; only
`IncludeInfra` sets out to reconcile cardinality, and it pays with three
changes beyond the plane.

## Verification

    ok  github.com/steveyegge/beads/internal/workapi
    ok  github.com/steveyegge/beads/internal/httpapi
    ok  github.com/steveyegge/beads/issueops
    --- PASS: TestEmbeddedCountIncludeEphemeral (39.71s)
    --- PASS: TestCountIncludeInfraFlagShape
    --- PASS: TestParseCountRequestCarriesEveryFilterFlag

`TestEmbeddedCountIncludeEphemeral` was run with `BEADS_TEST_EMBEDDED_DOLT=1`
and really executed — 39.71s, not a skip. The default fast tier would have
skipped it and reported `ok`.

`make api-gen` reproduces `types.gen.go` byte-for-byte from the spec hunk, so
the two cannot drift apart in review. `go build ./...`, `go vet` and `gofmt`
clean.

Claude-Session: https://claude.ai/code/session_01X1jMLQTSrCwtzFRS1xeUC6

* count: reframe the comments for this repo, not the fork they came from

Review on #6240 (bee-ghosttrack): five comment blocks were written from the
fork's point of view and become false once merged here — "belongs upstream",
"the half that still has to go upstream", "the fork's old wide default",
and "upstream's default" meaning the current default.

Find-and-replace, as asked: the reasoning is kept, only the frame of reference
changes. "upstream's default" -> "the default before this change"; the bd
list sibling "landed in #6098"; the infra-type divergence is "pinned, not
fixed, here" with the reason; the embedded test's fork-maintenance history is
replaced by what it actually pins. No code changes.

* build(bazel): list the two test files this PR adds in their go_test srcs

Maintainer review fixes for #6240, review iteration 1. The synthesis and the
quality scorecard both returned approve with no required changes; this commit
takes the one minor whose subject is the PR's own added files.

Taken:

- Change Impact / Blast Radius (minor, gate impact minor). The PR adds
  cmd/bd/count_include_ephemeral_embedded_test.go and
  internal/workapi/count_skipwisps_test.go, but neither was listed in its
  package's explicit go_test srcs, so the PR's own assertions were invisible to
  `bazel test`. Both rows are now listed.

  Gazelle's verdict, not a hand-guess: `make bazel-sync` was run (bazel 9.2.0 is
  available here) and it produced exactly these two entries. Its full output
  also rewrote 12 other BUILD.bazel files -- pre-existing base drift from other
  PRs' files, plus dep/select churn -- which is out of scope for this PR, so the
  generated result was reverted and only the two rows this PR is responsible for
  were re-applied, at the slots gazelle chose. Both srcs lists remain sorted
  with no duplicates (cmd/bd bd_test 637 entries, internal/workapi workapi_test
  24 entries).

  The cgo build tag on count_include_ephemeral_embedded_test.go does not exempt
  it: the repo lists tag-gated *_embedded_test.go files anyway (99 of the 101 in
  cmd/bd are listed, and the sampled ones are simultaneously listed and tagged).

Declined:

- Test Evidence Quality (nit, gate impact none). The filing lane itself marked
  it optional and explicitly said not to gate on it, and its own reasoning holds:
  internal/storage/dolt/counts.go and internal/storage/embeddeddolt/counts.go
  both delegate to the same issueops.CountIssuesInTx, differing only in
  transaction acquisition.

Validation: go build ./... and CGO_ENABLED=1 go vet on cmd/bd, internal/workapi
and issueops all clean; go test ./internal/workapi/ ./issueops/ ok. No
production code changed -- the commit is two generated-list rows.

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

---------

Co-authored-by: Gas City Adopt-PR <adopt-pr@gascity.local>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
2026-09-29 16:56:23 -07:00

321 lines
12 KiB
Go

package main
import (
"cmp"
"context"
"fmt"
"slices"
"strings"
"time"
"github.com/spf13/cobra"
"github.com/steveyegge/beads/internal/metrics"
"github.com/steveyegge/beads/internal/storage"
"github.com/steveyegge/beads/issueops"
)
var countCmd = &cobra.Command{
Use: "count",
GroupID: "views",
Short: "Count issues matching filters",
Long: `Count issues matching the specified filters.
By default, returns the total count of issues matching the filters.
Use --by-* flags to group counts by different attributes.
Examples:
bd count # Count all issues
bd count --status open # Count open issues
bd count --by-status # Group count by status
bd count --by-priority # Group count by priority
bd count --by-type # Group count by issue type
bd count --by-assignee # Group count by assignee
bd count --by-label # Group count by label
bd count --assignee alice --by-status # Count alice's issues by status
bd count --include-infra # Count issues + wisps tier (matches 'bd list --include-infra --all' cardinality)
bd count --type task --include-ephemeral # Count tasks in the durable AND wisps tiers
`,
SilenceUsage: true,
SilenceErrors: true,
RunE: func(cmd *cobra.Command, args []string) error {
evt := metrics.NewCommandEvent("count")
defer func() {
if c := metrics.Global(); c != nil {
c.CloseEventAndAdd(evt)
}
}()
request, groupBy, err := parseCountRequest(cmd)
if err != nil {
return err
}
counter, err := openCounter()
if err != nil {
return HandleErrorRespectJSON("%v", err)
}
return executeCount(rootCtx, counter, request, groupBy)
},
}
// openCounter hands back the count role for whichever route this invocation is
// on, each through its own capability accessor. Neither branch builds a filter,
// loads config or opens a unit of work.
func openCounter() (issueops.Counter, error) {
if usesProxiedServer() {
return proxiedCounter()
}
return store.Counter()
}
// parseCountRequest turns the flag set into the role's request. Normalization
// of labels and ids, the wisp-tier policy and the workspace's infra vocabulary
// all live behind the role, so the two routes cannot come to disagree.
func parseCountRequest(cmd *cobra.Command) (issueops.CountRequest, issueops.CountGroup, error) {
groupBy, err := countGroupFlag(cmd)
if err != nil {
return issueops.CountRequest{}, "", err
}
status, _ := cmd.Flags().GetString("status")
assignee, _ := cmd.Flags().GetString("assignee")
issueType, _ := cmd.Flags().GetString("type")
labels, _ := cmd.Flags().GetStringSlice("label")
labelsAny, _ := cmd.Flags().GetStringSlice("label-any")
titleSearch, _ := cmd.Flags().GetString("title")
idFilter, _ := cmd.Flags().GetString("id")
titleContains, _ := cmd.Flags().GetString("title-contains")
descContains, _ := cmd.Flags().GetString("desc-contains")
notesContains, _ := cmd.Flags().GetString("notes-contains")
emptyDesc, _ := cmd.Flags().GetBool("empty-description")
noAssignee, _ := cmd.Flags().GetBool("no-assignee")
noLabels, _ := cmd.Flags().GetBool("no-labels")
includeInfra, _ := cmd.Flags().GetBool("include-infra")
metadataFieldFlags, _ := cmd.Flags().GetStringArray("metadata-field")
includeEphemeral, _ := cmd.Flags().GetBool("include-ephemeral")
request := issueops.CountRequest{
Status: status,
IssueType: issueType,
Assignee: assignee,
Labels: labels,
LabelsAny: labelsAny,
TitleSearch: titleSearch,
IDFilter: idFilter,
TitleContains: titleContains,
DescContains: descContains,
NotesContains: notesContains,
EmptyDesc: emptyDesc,
NoAssignee: noAssignee,
NoLabels: noLabels,
IncludeInfra: includeInfra,
IncludeEphemeral: includeEphemeral,
}
if len(metadataFieldFlags) > 0 {
request.MetadataFields = make(map[string]string, len(metadataFieldFlags))
for _, mf := range metadataFieldFlags {
k, v, ok := strings.Cut(mf, "=")
if !ok || k == "" {
return issueops.CountRequest{}, "", HandleErrorRespectJSON("invalid --metadata-field: expected key=value, got %q", mf)
}
if err := storage.ValidateMetadataKey(k); err != nil {
return issueops.CountRequest{}, "", HandleErrorRespectJSON("invalid --metadata-field key: %v", err)
}
request.MetadataFields[k] = v
}
}
if k, _ := cmd.Flags().GetString("has-metadata-key"); k != "" {
if err := storage.ValidateMetadataKey(k); err != nil {
return issueops.CountRequest{}, "", HandleErrorRespectJSON("invalid --has-metadata-key: %v", err)
}
request.HasMetadataKey = k
}
if cmd.Flags().Changed("priority") {
priority, _ := cmd.Flags().GetInt("priority")
request.Priority = &priority
}
if cmd.Flags().Changed("priority-min") {
priorityMin, _ := cmd.Flags().GetInt("priority-min")
request.PriorityMin = &priorityMin
}
if cmd.Flags().Changed("priority-max") {
priorityMax, _ := cmd.Flags().GetInt("priority-max")
request.PriorityMax = &priorityMax
}
for _, bound := range []struct {
flag string
dest **time.Time
}{
{"created-after", &request.CreatedAfter},
{"created-before", &request.CreatedBefore},
{"updated-after", &request.UpdatedAfter},
{"updated-before", &request.UpdatedBefore},
{"closed-after", &request.ClosedAfter},
{"closed-before", &request.ClosedBefore},
} {
raw, _ := cmd.Flags().GetString(bound.flag)
if raw == "" {
continue
}
parsed, err := parseTimeFlag(raw)
if err != nil {
return issueops.CountRequest{}, "", HandleErrorRespectJSON("parsing --%s: %v", bound.flag, err)
}
*bound.dest = &parsed
}
return request, groupBy, nil
}
// countGroupFlag resolves the five mutually exclusive --by-* flags to one
// dimension. The role refuses an unknown one, but it cannot refuse TWO — by
// the time a request reaches it only one dimension is left — so the exclusivity
// check stays here, with the flags it is about.
func countGroupFlag(cmd *cobra.Command) (issueops.CountGroup, error) {
var group issueops.CountGroup
set := 0
for _, candidate := range []struct {
flag string
group issueops.CountGroup
}{
{"by-status", issueops.CountGroupStatus},
{"by-priority", issueops.CountGroupPriority},
{"by-type", issueops.CountGroupType},
{"by-assignee", issueops.CountGroupAssignee},
{"by-label", issueops.CountGroupLabel},
} {
if on, _ := cmd.Flags().GetBool(candidate.flag); on {
group = candidate.group
set++
}
}
if set > 1 {
return "", HandleErrorRespectJSON("only one --by-* flag can be specified")
}
return group, nil
}
func executeCount(ctx context.Context, counter issueops.Counter, request issueops.CountRequest, groupBy issueops.CountGroup) error {
if groupBy == "" {
result, err := counter.Count(ctx, request)
if err != nil {
return HandleErrorRespectJSON("%v", err)
}
if jsonOutput {
return outputJSON(struct {
Count int64 `json:"count"`
}{Count: result.Total})
}
fmt.Println(result.Total)
return nil
}
result, err := counter.CountByGroup(ctx, issueops.CountByGroupRequest{Filter: request, GroupBy: groupBy})
if err != nil {
return HandleErrorRespectJSON("%v", err)
}
type GroupCount struct {
Group string `json:"group"`
Count int `json:"count"`
}
groups := make([]GroupCount, 0, len(result.Groups))
for group, count := range result.Groups {
groups = append(groups, GroupCount{Group: group, Count: count})
}
slices.SortFunc(groups, func(a, b GroupCount) int {
return cmp.Compare(a.Group, b.Group)
})
if jsonOutput {
return outputJSON(struct {
Total int64 `json:"total"`
Groups []GroupCount `json:"groups"`
}{
Total: result.Total,
Groups: groups,
})
}
// The total is the role's scalar count, not the sum of the buckets:
// --by-label buckets overlap, so a multi-label issue is one row in the
// total and one row in each of its buckets.
fmt.Printf("Total: %d\n\n", result.Total)
for _, g := range groups {
fmt.Printf("%s: %d\n", g.Group, g.Count)
}
return nil
}
func init() {
registerCountFlags(countCmd)
rootCmd.AddCommand(countCmd)
}
// registerCountFlags declares `bd count`'s flag set on cmd. It is a function
// rather than a block inside init so a test can stand up an INDEPENDENT
// command carrying the same flags: cobra's AddFlagSet shares the underlying
// *Flag values, so a test that set a flag on a copy would leak it into the
// real command and into the next test.
func registerCountFlags(cmd *cobra.Command) {
// Filter flags (same as list command)
cmd.Flags().StringP("status", "s", "", "Filter by stored status (open, in_progress, blocked, deferred, closed). Note: dependency-blocked issues use 'bd blocked'")
cmd.Flags().IntP("priority", "p", 0, "Filter by priority (0-4: 0=critical, 1=high, 2=medium, 3=low, 4=backlog)")
cmd.Flags().StringP("assignee", "a", "", "Filter by assignee")
cmd.Flags().StringP("type", "t", "", "Filter by type (bug, feature, task, epic, chore, decision, merge-request, molecule, gate)")
cmd.Flags().StringSliceP("label", "l", []string{}, "Filter by labels (AND: must have ALL)")
cmd.Flags().StringSlice("label-any", []string{}, "Filter by labels (OR: must have AT LEAST ONE)")
cmd.Flags().String("title", "", "Filter by title text (case-insensitive substring match)")
cmd.Flags().String("id", "", "Filter by specific issue IDs (comma-separated)")
// Pattern matching
cmd.Flags().String("title-contains", "", "Filter by title substring")
cmd.Flags().String("desc-contains", "", "Filter by description substring")
cmd.Flags().String("notes-contains", "", "Filter by notes substring")
// Date ranges
cmd.Flags().String("created-after", "", "Filter issues created after date (YYYY-MM-DD or RFC3339)")
cmd.Flags().String("created-before", "", "Filter issues created before date (YYYY-MM-DD or RFC3339)")
cmd.Flags().String("updated-after", "", "Filter issues updated after date (YYYY-MM-DD or RFC3339)")
cmd.Flags().String("updated-before", "", "Filter issues updated before date (YYYY-MM-DD or RFC3339)")
cmd.Flags().String("closed-after", "", "Filter issues closed after date (YYYY-MM-DD or RFC3339)")
cmd.Flags().String("closed-before", "", "Filter issues closed before date (YYYY-MM-DD or RFC3339)")
// Empty/null checks
cmd.Flags().Bool("empty-description", false, "Filter issues with empty description")
cmd.Flags().Bool("no-assignee", false, "Filter issues with no assignee")
cmd.Flags().Bool("no-labels", false, "Filter issues with no labels")
cmd.Flags().StringArray("metadata-field", nil, "Filter by metadata field (key=value, repeatable)")
cmd.Flags().String("has-metadata-key", "", "Filter issues that have this metadata key set")
// Priority ranges
cmd.Flags().Int("priority-min", 0, "Filter by minimum priority (inclusive)")
cmd.Flags().Int("priority-max", 0, "Filter by maximum priority (inclusive)")
// Wisps tier (GH#4387): mirrors bd list's flag of the same name so
// `bd count --include-infra <filters>` returns exactly the cardinality of
// `bd list --include-infra <filters> --all`.
cmd.Flags().Bool("include-infra", false, "Include infrastructure beads and the wisps tier (matches 'bd list --include-infra --all' cardinality)")
// The plane knob alone: admits the wisps tier WITHOUT lifting any type
// exclusion, which --include-infra cannot do (it bundles four changes, and
// its template exclusion silently drops template rows of a named type).
//
// The sibling on `bd list` landed in #6098 (cmd/bd/list.go); the name and
// the help text deliberately follow its shape so the two halves of the
// pair read alike. The trailing clause is the one addition: on `bd count`
// the wider `--include-infra` sits right beside this flag and advertises
// cardinality matching, so the contrast has to be legible in `--help` and
// not only in this comment.
cmd.Flags().Bool("include-ephemeral", false, "Include ephemeral wisp-plane rows in the count (normally hidden), without lifting type exclusions")
// Grouping flags
cmd.Flags().Bool("by-status", false, "Group count by status")
cmd.Flags().Bool("by-priority", false, "Group count by priority")
cmd.Flags().Bool("by-type", false, "Group count by issue type")
cmd.Flags().Bool("by-assignee", false, "Group count by assignee")
cmd.Flags().Bool("by-label", false, "Group count by label")
}