diff --git a/.github/scripts/embedded-storage-test-shards.txt b/.github/scripts/embedded-storage-test-shards.txt index 8723a463d..1502ba993 100644 --- a/.github/scripts/embedded-storage-test-shards.txt +++ b/.github/scripts/embedded-storage-test-shards.txt @@ -211,6 +211,7 @@ 15 3 TestUpdateMergeOps_DoltEngine_TwoIssues 15 3 TestWithPeerAuth_RestoresAbsentEnv +15 4 TestCounterExcludeStatusAcceptsAWorkspaceCustomStatusWithoutIncludeInfra 15 4 TestEmbeddedBatchCloserSettlesTheDependersOfWhatItClosed 15 4 TestEmbeddedBlockedRecheckSettlesWhatTheWriteRecorded 15 4 TestEmbeddedClaimIssueWrapperClaimsTheWispPlane diff --git a/backend/conformance/counter_contract.go b/backend/conformance/counter_contract.go index e70e2e0f2..b18f5fd3f 100644 --- a/backend/conformance/counter_contract.go +++ b/backend/conformance/counter_contract.go @@ -57,6 +57,19 @@ type CounterFixture struct { // A nil hook means "this backend cannot observe history", and the case // that needs it SKIPS rather than passing quietly. CountHistory func(context.Context) (int, error) + // AddDependency seeds one dependency edge, the same hook + // ReaderFixture/DependencyEditorFixture take it from (role_fixture_kit's + // shared AddDependency). It exists here only for the ParentID/NoParent + // scope cases (S8): NoParent excludes by PARENT-CHILD EDGE alone + // (sqlbuild/filter.go), unlike ParentID's dotted-id-prefix fallback, so + // pinning it needs a real edge and not just a naming convention. + AddDependency func(context.Context, *types.Dependency, string) error + // List runs the List role's query directly (the store's IssueReader), so + // the storage-level parity cases below (S8 review, follow-up #5) can + // compare Count's answer against len(List's rows) for the same scope + // without going through a second builder. A nil hook skips those cases + // rather than failing: not every backend wiring has a Reader to hand. + List func(context.Context, publicops.ListRequest) (publicops.IssuePage, error) } // RunCounterCountsTheDurablePlaneByDefault pins counter.go:123-126 from the @@ -176,6 +189,323 @@ func RunCounterAnUnknownStatusMatchesNothing(t *testing.T, ctx context.Context, assertCounterTotal(t, ctx, fixture, scope, 0) } +// RunCounterParentIDScopesToChildren pins CountRequest.ParentID (counter.go, +// S8): restricted to ONE issue's children, reached two ways at once, exactly +// as ListRequest.ParentID is (sqlbuild/filter.go) — a parent-child dependency +// edge, OR (for a row with no such edge) a dotted-id prefix match. The parent +// itself, an unrelated row and a same-prefix DECOY (sibling id with no +// separator, e.g. "parent2") are all seeded so neither arm can pass by +// over-matching. +func RunCounterParentIDScopesToChildren(t *testing.T, ctx context.Context, fixture CounterFixture) { + t.Helper() + parent := fixture.IssuePrefix + "-parentid-parent" + byEdge := fixture.IssuePrefix + "-parentid-byedge" + byDotted := parent + ".1" + decoy := parent + "x" + unrelated := fixture.IssuePrefix + "-parentid-unrelated" + + for _, id := range []string{parent, byEdge, byDotted, decoy, unrelated} { + seedCounterIssue(t, ctx, fixture, counterSeed(id)) + } + requireCounterAddDependency(t, fixture) + if err := fixture.AddDependency(ctx, &types.Dependency{ + IssueID: byEdge, DependsOnID: parent, Type: types.DepParentChild, + }, "seed"); err != nil { + t.Fatalf("seed edge %s -> %s: %v", byEdge, parent, err) + } + + scope := counterScope(parent, byEdge, byDotted, decoy, unrelated) + scope.ParentID = parent + // Both children count; the parent, the decoy and the unrelated row do not. + assertCounterTotal(t, ctx, fixture, scope, 2) +} + +// RunCounterNoParentExcludesChildren pins CountRequest.NoParent +// (counter.go, S8): restricted to rows with NO parent-child edge. +// +// Unlike ParentID, NoParent's SQL clause (sqlbuild/filter.go) checks ONLY the +// parent-child dependency table, not the dotted-id convention — a row that is +// a dotted-id child with no edge at all still passes NoParent. That asymmetry +// is exactly what the "byDotted" row below pins: it is excluded from +// ParentID's answer by nothing (RunCounterParentIDScopesToChildren already +// admits it), but NoParent admits it too, because it carries no edge. +func RunCounterNoParentExcludesChildren(t *testing.T, ctx context.Context, fixture CounterFixture) { + t.Helper() + parent := fixture.IssuePrefix + "-noparent-parent" + child := fixture.IssuePrefix + "-noparent-child" + byDotted := parent + ".1" + + for _, id := range []string{parent, child, byDotted} { + seedCounterIssue(t, ctx, fixture, counterSeed(id)) + } + requireCounterAddDependency(t, fixture) + if err := fixture.AddDependency(ctx, &types.Dependency{ + IssueID: child, DependsOnID: parent, Type: types.DepParentChild, + }, "seed"); err != nil { + t.Fatalf("seed edge %s -> %s: %v", child, parent, err) + } + + scope := counterScope(parent, child, byDotted) + assertCounterTotal(t, ctx, fixture, scope, 3) + + scope.NoParent = true + // child is excluded (has an edge); parent and byDotted remain (neither + // carries a parent-child edge, and NoParent does not consult the dotted + // naming convention at all). + assertCounterTotal(t, ctx, fixture, scope, 2) +} + +// RunCounterExcludeTypesNarrowsThePredicate pins CountRequest.ExcludeTypes +// (counter.go, S8): named types are excluded from the count, composing with +// (rather than replacing) IncludeInfra's own "gate" exclusion. +func RunCounterExcludeTypesNarrowsThePredicate(t *testing.T, ctx context.Context, fixture CounterFixture) { + t.Helper() + task := fixture.IssuePrefix + "-extype-task" + chore := fixture.IssuePrefix + "-extype-chore" + gate := fixture.IssuePrefix + "-extype-gate" + seedCounterIssue(t, ctx, fixture, counterSeed(task)) + choreSeed := counterSeed(chore) + choreSeed.IssueType = types.TypeChore + seedCounterIssue(t, ctx, fixture, choreSeed) + gateSeed := counterSeed(gate) + gateSeed.IssueType = types.IssueType("gate") + seedCounterIssue(t, ctx, fixture, gateSeed) + + scope := counterScope(task, chore, gate) + assertCounterTotal(t, ctx, fixture, scope, 3) + + scope.ExcludeTypes = []string{"chore"} + assertCounterTotal(t, ctx, fixture, scope, 2) + + // Composes with IncludeInfra's own default gate exclusion: both the + // explicit chore exclusion and the include-infra gate exclusion apply at + // once, leaving only task. + scope.IncludeInfra = true + assertCounterTotal(t, ctx, fixture, scope, 1) +} + +// RunCounterExcludeStatusNarrowsThePredicate pins CountRequest.ExcludeStatus +// (counter.go, S8): a Count-only field with no List counterpart. Named +// statuses are excluded, and — UNLIKE Status — an unrecognized name is +// refused as ErrValidation rather than excluding nothing, because a typo +// would silently overcount (counter.go). +func RunCounterExcludeStatusNarrowsThePredicate(t *testing.T, ctx context.Context, fixture CounterFixture) { + t.Helper() + open := fixture.IssuePrefix + "-exstatus-open" + closed := fixture.IssuePrefix + "-exstatus-closed" + seedCounterIssue(t, ctx, fixture, counterSeed(open)) + closedSeed := counterSeed(closed) + closedSeed.Status = types.StatusClosed + seedCounterIssue(t, ctx, fixture, closedSeed) + + scope := counterScope(open, closed) + assertCounterTotal(t, ctx, fixture, scope, 2) + + scope.ExcludeStatus = []string{"closed"} + assertCounterTotal(t, ctx, fixture, scope, 1) + + // Unlike Status and IssueType, ExcludeStatus IS validated (S8 review, + // follow-up #3): a misspelled entry would silently exclude nothing and + // OVERCOUNT, so an unrecognized name is refused as ErrValidation instead + // of treated as a no-op. + scope.ExcludeStatus = []string{"no-such-status"} + if _, err := fixture.Counter.Count(ctx, scope); !errors.Is(err, publicops.ErrValidation) { + t.Errorf("Count with an unrecognized exclude-status = %v, want ErrValidation", err) + } +} + +// requireCounterAddDependency skips a case that needs a real parent-child +// edge when the fixture offers no way to create one, rather than failing with +// a nil-pointer call — mirroring counterHistoryCount's optional-hook pattern. +func requireCounterAddDependency(t *testing.T, fixture CounterFixture) { + t.Helper() + if fixture.AddDependency == nil { + t.Skip("fixture cannot seed a dependency edge: AddDependency is nil") + } +} + +// requireCounterList skips a case that needs the List role when the fixture +// offers no way to reach it, mirroring requireCounterAddDependency. +func requireCounterList(t *testing.T, fixture CounterFixture) { + t.Helper() + if fixture.List == nil { + t.Skip("fixture cannot run the List role: List is nil") + } +} + +// assertCounterMatchesList compares Count's Total against len(List's rows) +// for the same scope, with List's AllFlag and IncludeAllTypes both set so +// List's own default exclusions (status, pinned, template, gate, infra-plane) +// are lifted exactly as far as Count's own default lifts none of them +// (workapi.BuildCountFilter's doc, count.go) — the one combination that +// makes the two cardinalities comparable at all for an arbitrary scope. +// +// This is a DIFFERENT check than +// TestBuildCountFilterParentFieldsMatchListFilter +// (internal/workapi/count_test.go), which compares the two FILTERS a shared +// request builds without touching a store; this one runs both roles against +// a real backend and compares what they actually return (S8 review, follow-up +// #5). +func assertCounterMatchesList(t *testing.T, ctx context.Context, fixture CounterFixture, idFilter, parentID string, noParent bool, excludeTypes []string) { + t.Helper() + countReq := publicops.CountRequest{ + IDFilter: idFilter, + ParentID: parentID, + NoParent: noParent, + ExcludeTypes: excludeTypes, + } + total := counterTotal(t, ctx, fixture, countReq) + + listReq := publicops.ListRequest{ + IDFilter: idFilter, + ParentID: parentID, + NoParent: noParent, + ExcludeTypes: excludeTypes, + AllFlag: true, + IncludeAllTypes: true, + } + page, err := fixture.List(ctx, listReq) + if err != nil { + t.Fatalf("List(%+v): %v", listReq, err) + } + if got := int64(len(page.Items)); got != total { + t.Errorf("Count(%+v) = %d, len(List(%+v).Items) = %d, want equal", countReq, total, listReq, got) + } +} + +// RunCounterParentIDMatchesListCardinality pins the storage-level half of +// CountRequest.ParentID's promise (S8 review, follow-up #5a): for one scope, +// Count's Total equals len(List's rows) for the equivalent ParentID scope. +func RunCounterParentIDMatchesListCardinality(t *testing.T, ctx context.Context, fixture CounterFixture) { + t.Helper() + requireCounterList(t, fixture) + requireCounterAddDependency(t, fixture) + + parent := fixture.IssuePrefix + "-parity-parentid-parent" + byEdge := fixture.IssuePrefix + "-parity-parentid-byedge" + byDotted := parent + ".1" + unrelated := fixture.IssuePrefix + "-parity-parentid-unrelated" + + for _, id := range []string{parent, byEdge, byDotted, unrelated} { + seedCounterIssue(t, ctx, fixture, counterSeed(id)) + } + if err := fixture.AddDependency(ctx, &types.Dependency{ + IssueID: byEdge, DependsOnID: parent, Type: types.DepParentChild, + }, "seed"); err != nil { + t.Fatalf("seed edge %s -> %s: %v", byEdge, parent, err) + } + + idFilter := counterScope(parent, byEdge, byDotted, unrelated).IDFilter + assertCounterMatchesList(t, ctx, fixture, idFilter, parent, false, nil) +} + +// RunCounterNoParentMatchesListCardinality pins the storage-level half of +// CountRequest.NoParent's promise (S8 review, follow-up #5a). +func RunCounterNoParentMatchesListCardinality(t *testing.T, ctx context.Context, fixture CounterFixture) { + t.Helper() + requireCounterList(t, fixture) + requireCounterAddDependency(t, fixture) + + parent := fixture.IssuePrefix + "-parity-noparent-parent" + child := fixture.IssuePrefix + "-parity-noparent-child" + byDotted := parent + ".1" + + for _, id := range []string{parent, child, byDotted} { + seedCounterIssue(t, ctx, fixture, counterSeed(id)) + } + if err := fixture.AddDependency(ctx, &types.Dependency{ + IssueID: child, DependsOnID: parent, Type: types.DepParentChild, + }, "seed"); err != nil { + t.Fatalf("seed edge %s -> %s: %v", child, parent, err) + } + + idFilter := counterScope(parent, child, byDotted).IDFilter + assertCounterMatchesList(t, ctx, fixture, idFilter, "", true, nil) +} + +// RunCounterExcludeTypesMatchesListCardinality pins the storage-level half of +// CountRequest.ExcludeTypes' promise (S8 review, follow-up #5a). ExcludeStatus +// has no List counterpart (counter.go) so it is not part of this parity +// family; RunCounterExcludeStatusNarrowsThePredicate above is its only +// coverage. +func RunCounterExcludeTypesMatchesListCardinality(t *testing.T, ctx context.Context, fixture CounterFixture) { + t.Helper() + requireCounterList(t, fixture) + + task := fixture.IssuePrefix + "-parity-extype-task" + chore := fixture.IssuePrefix + "-parity-extype-chore" + seedCounterIssue(t, ctx, fixture, counterSeed(task)) + choreSeed := counterSeed(chore) + choreSeed.IssueType = types.TypeChore + seedCounterIssue(t, ctx, fixture, choreSeed) + + idFilter := counterScope(task, chore).IDFilter + assertCounterMatchesList(t, ctx, fixture, idFilter, "", false, []string{"chore"}) +} + +// RunCounterParentIDIncludesAWispChild pins that ParentID's edge match reaches +// into the wisp tier exactly as IncludeInfra's merge does (S8 review, +// follow-up #5b): a child seeded in the ephemeral/wisp tier, linked to its +// parent by the same parent-child edge ParentID matches on, is invisible to +// Count's durable-only default and counted once IncludeInfra merges the wisps +// table in. +func RunCounterParentIDIncludesAWispChild(t *testing.T, ctx context.Context, fixture CounterFixture) { + t.Helper() + requireCounterAddDependency(t, fixture) + + parent := fixture.IssuePrefix + "-wispchild-parent" + wispChild := fixture.IssuePrefix + "-wispchild-child" + seedCounterIssue(t, ctx, fixture, counterSeed(parent)) + seedCounterWisp(t, ctx, fixture, counterSeed(wispChild)) + if err := fixture.AddDependency(ctx, &types.Dependency{ + IssueID: wispChild, DependsOnID: parent, Type: types.DepParentChild, + }, "seed"); err != nil { + t.Fatalf("seed edge %s -> %s: %v", wispChild, parent, err) + } + + scope := counterScope(parent, wispChild) + scope.ParentID = parent + // Durable-only default (counter.go, RunCounterCountsTheDurablePlaneByDefault): + // the wisp child is invisible without IncludeInfra. + assertCounterTotal(t, ctx, fixture, scope, 0) + + scope.IncludeInfra = true + assertCounterTotal(t, ctx, fixture, scope, 1) +} + +// RunCounterParentIDAndExcludeStatusComposeOnAClosedChild pins that ParentID +// and ExcludeStatus apply as a conjunction, not alternatives (S8 review, +// follow-up #5b): a closed child under its parent is counted by ParentID +// alone (Count hides nothing by default), and excluded once ExcludeStatus +// names its status, leaving only the open child. +func RunCounterParentIDAndExcludeStatusComposeOnAClosedChild(t *testing.T, ctx context.Context, fixture CounterFixture) { + t.Helper() + requireCounterAddDependency(t, fixture) + + parent := fixture.IssuePrefix + "-closedchild-parent" + openChild := fixture.IssuePrefix + "-closedchild-open" + closedChild := fixture.IssuePrefix + "-closedchild-closed" + seedCounterIssue(t, ctx, fixture, counterSeed(parent)) + seedCounterIssue(t, ctx, fixture, counterSeed(openChild)) + closedSeed := counterSeed(closedChild) + closedSeed.Status = types.StatusClosed + seedCounterIssue(t, ctx, fixture, closedSeed) + for _, child := range []string{openChild, closedChild} { + if err := fixture.AddDependency(ctx, &types.Dependency{ + IssueID: child, DependsOnID: parent, Type: types.DepParentChild, + }, "seed"); err != nil { + t.Fatalf("seed edge %s -> %s: %v", child, parent, err) + } + } + + scope := counterScope(parent, openChild, closedChild) + scope.ParentID = parent + assertCounterTotal(t, ctx, fixture, scope, 2) + + scope.ExcludeStatus = []string{"closed"} + assertCounterTotal(t, ctx, fixture, scope, 1) +} + // RunCounterGroupsPartitionTheScalarSet pins counter.go:168-169 and :244-245 // for a dimension whose buckets do NOT overlap: the grouped Total is the same // number a scalar Count returns, and the buckets partition it exactly. diff --git a/backend/conformance/role_bundle_cases.go b/backend/conformance/role_bundle_cases.go index bbbb1fa29..22bf1173a 100644 --- a/backend/conformance/role_bundle_cases.go +++ b/backend/conformance/role_bundle_cases.go @@ -186,6 +186,15 @@ var roleContractCases = []roleContract{ RunCounterIncludeInfraExcludesGates, RunCounterCountsClosedRows, RunCounterAnUnknownStatusMatchesNothing, + RunCounterParentIDScopesToChildren, + RunCounterNoParentExcludesChildren, + RunCounterExcludeTypesNarrowsThePredicate, + RunCounterExcludeStatusNarrowsThePredicate, + RunCounterParentIDMatchesListCardinality, + RunCounterNoParentMatchesListCardinality, + RunCounterExcludeTypesMatchesListCardinality, + RunCounterParentIDIncludesAWispChild, + RunCounterParentIDAndExcludeStatusComposeOnAClosedChild, RunCounterGroupsPartitionTheScalarSet, RunCounterLabelBucketsOverlapSoTotalIsNotTheirSum, RunCounterNamesTheEmptyBuckets, diff --git a/cmd/bd/count.go b/cmd/bd/count.go index 01f3d7d04..46ea46845 100644 --- a/cmd/bd/count.go +++ b/cmd/bd/count.go @@ -93,6 +93,13 @@ func parseCountRequest(cmd *cobra.Command) (issueops.CountRequest, issueops.Coun includeInfra, _ := cmd.Flags().GetBool("include-infra") metadataFieldFlags, _ := cmd.Flags().GetStringArray("metadata-field") includeEphemeral, _ := cmd.Flags().GetBool("include-ephemeral") + parentID, _ := cmd.Flags().GetString("parent") + noParent, _ := cmd.Flags().GetBool("no-parent") + if parentID != "" && noParent { + return issueops.CountRequest{}, "", HandleErrorRespectJSON("--parent and --no-parent are mutually exclusive") + } + excludeTypes, _ := cmd.Flags().GetStringSlice("exclude-type") + excludeStatus, _ := cmd.Flags().GetStringSlice("exclude-status") request := issueops.CountRequest{ Status: status, @@ -111,6 +118,10 @@ func parseCountRequest(cmd *cobra.Command) (issueops.CountRequest, issueops.Coun IncludeInfra: includeInfra, IncludeEphemeral: includeEphemeral, + ParentID: parentID, + NoParent: noParent, + ExcludeTypes: excludeTypes, + ExcludeStatus: excludeStatus, } if len(metadataFieldFlags) > 0 { request.MetadataFields = make(map[string]string, len(metadataFieldFlags)) @@ -271,6 +282,18 @@ func registerCountFlags(cmd *cobra.Command) { cmd.Flags().String("title", "", "Filter by title text (case-insensitive substring match)") cmd.Flags().String("id", "", "Filter by specific issue IDs (comma-separated)") + // Hierarchy and exclusions (same spelling as `bd list`'s flags of the same + // name, GH#4387's count/list parity): --parent and --no-parent restrict to + // a parent's children or to rows with no parent-child edge (an edge-less + // dotted-id child still counts as top-level, as in `bd list --no-parent`); + // --exclude-type and --exclude-status narrow the predicate by type and by + // status, composing with --include-infra's own exclusions rather than + // replacing them. + cmd.Flags().String("parent", "", "Filter by parent issue ID (count children of specified issue)") + cmd.Flags().Bool("no-parent", false, "Count only top-level issues (exclude child issues)") + cmd.Flags().StringSlice("exclude-type", nil, "Exclude issue types from the count (comma-separated or repeatable)") + cmd.Flags().StringSlice("exclude-status", nil, "Exclude statuses from the count (comma-separated or repeatable)") + // Pattern matching cmd.Flags().String("title-contains", "", "Filter by title substring") cmd.Flags().String("desc-contains", "", "Filter by description substring") diff --git a/cmd/bd/count_filter_test.go b/cmd/bd/count_filter_test.go index da97b2831..cfac118d1 100644 --- a/cmd/bd/count_filter_test.go +++ b/cmd/bd/count_filter_test.go @@ -76,10 +76,71 @@ func TestCountIncludeEphemeralFlagShape(t *testing.T) { } } +// TestCountScopeFlagsShape pins --parent, --no-parent, --exclude-type and +// --exclude-status: their existence, their defaults, and that setting one +// does not leak into another. These are the Counter-scope fields (S8): the +// behavior token issues.count.scope on the HTTP front door exists because of +// exactly these four flags. +func TestCountScopeFlagsShape(t *testing.T) { + for flag, defValue := range map[string]string{ + "parent": "", + "no-parent": "false", + "exclude-type": "[]", + "exclude-status": "[]", + } { + got := countCmd.Flags().Lookup(flag) + if got == nil { + t.Fatalf("bd count must expose a --%s flag", flag) + } + if got.DefValue != defValue { + t.Fatalf("--%s must default to %q, got %q", flag, defValue, got.DefValue) + } + } + + request, _, err := parseCountRequest(newCountFlagSet(t)) + if err != nil { + t.Fatalf("parseCountRequest with no flags set: %v", err) + } + if request.ParentID != "" || request.NoParent || len(request.ExcludeTypes) != 0 || len(request.ExcludeStatus) != 0 { + t.Errorf("parseCountRequest with no flags set = %#v, want all four scope fields at their zero value", request) + } + + flags := newCountFlagSet(t) + if err := flags.Flags().Set("parent", "bd-1"); err != nil { + t.Fatalf("set --parent: %v", err) + } + if err := flags.Flags().Set("exclude-type", "wisp"); err != nil { + t.Fatalf("set --exclude-type: %v", err) + } + request, _, err = parseCountRequest(flags) + if err != nil { + t.Fatalf("parseCountRequest --parent --exclude-type: %v", err) + } + if request.ParentID != "bd-1" { + t.Errorf("ParentID = %q, want %q", request.ParentID, "bd-1") + } + if request.NoParent { + t.Error("NoParent = true with only --parent set; the two flags must not leak into each other") + } + if !reflect.DeepEqual(request.ExcludeTypes, []string{"wisp"}) { + t.Errorf("ExcludeTypes = %v, want [wisp]", request.ExcludeTypes) + } + if len(request.ExcludeStatus) != 0 { + t.Errorf("ExcludeStatus = %v, want empty; only --exclude-type was set", request.ExcludeStatus) + } +} + // TestParseCountRequestCarriesEveryFilterFlag is the tripwire for a flag that // is registered, documented and silently dropped on the way into the request. // Every filter flag is set to a value distinguishable from its zero and read // back off the request. +// +// --no-parent is NOT in this matrix: review S8 follow-up #1 made --parent and +// --no-parent mutually exclusive, and this test already sets --parent to +// cover that field, so setting --no-parent alongside it would refuse the +// whole request instead of pinning a mapping. --no-parent's carriage into the +// request is pinned on its own in TestParseCountRequestRefusesParentAndNoParentTogether's +// sibling cases below and in TestCountScopeFlagsShape above. func TestParseCountRequestCarriesEveryFilterFlag(t *testing.T) { flags := newCountFlagSet(t) for flag, value := range map[string]string{ @@ -109,6 +170,9 @@ func TestParseCountRequestCarriesEveryFilterFlag(t *testing.T) { "priority-max": "4", "include-infra": "true", "include-ephemeral": "true", + "parent": "bd-9", + "exclude-type": "wisp,gate", + "exclude-status": "closed,archived", } { if err := flags.Flags().Set(flag, value); err != nil { t.Fatalf("set --%s=%s: %v", flag, value, err) @@ -161,12 +225,59 @@ func TestParseCountRequestCarriesEveryFilterFlag(t *testing.T) { HasMetadataKey: "audit_ref", IncludeInfra: true, IncludeEphemeral: true, + ParentID: "bd-9", + ExcludeTypes: []string{"wisp", "gate"}, + ExcludeStatus: []string{"closed", "archived"}, } if !reflect.DeepEqual(request, want) { t.Errorf("parseCountRequest built\n %#v\nwant\n %#v", request, want) } } +// TestParseCountRequestRefusesParentAndNoParentTogether pins review S8 +// follow-up #1: `bd count --parent X --no-parent` is refused at the CLI flag +// layer, before a request is even built — the same early-refusal shape every +// other mutually-exclusive pair on this CLI gets (e.g. --pinned/--no-pinned). +// +// HandleErrorRespectJSON prints "--parent and --no-parent are mutually +// exclusive" (`bd list`'s own wording, cmd/bd/list_input.go) to stderr/JSON +// and returns an opaque exit error, so — as every other parseCountRequest +// refusal test in this file does — only the refusal itself is asserted here, +// not the string inside the returned error. +func TestParseCountRequestRefusesParentAndNoParentTogether(t *testing.T) { + flags := newCountFlagSet(t) + if err := flags.Flags().Set("parent", "bd-1"); err != nil { + t.Fatalf("set --parent: %v", err) + } + if err := flags.Flags().Set("no-parent", "true"); err != nil { + t.Fatalf("set --no-parent: %v", err) + } + if _, _, err := parseCountRequest(flags); err == nil { + t.Fatal("parseCountRequest accepted --parent with --no-parent, want a refusal") + } +} + +// TestParseCountRequestCarriesNoParentAlone pins --no-parent's own mapping, +// split out of TestParseCountRequestCarriesEveryFilterFlag because that test's +// --parent case cannot also set --no-parent now that the two refuse each +// other. +func TestParseCountRequestCarriesNoParentAlone(t *testing.T) { + flags := newCountFlagSet(t) + if err := flags.Flags().Set("no-parent", "true"); err != nil { + t.Fatalf("set --no-parent: %v", err) + } + request, _, err := parseCountRequest(flags) + if err != nil { + t.Fatalf("parseCountRequest --no-parent: %v", err) + } + if !request.NoParent { + t.Error("NoParent = false, want true") + } + if request.ParentID != "" { + t.Errorf("ParentID = %q, want empty: --parent was not set", request.ParentID) + } +} + func TestParseCountRequestRejectsInvalidMetadataField(t *testing.T) { for _, value := range []string{"team", "bad$key=value"} { t.Run(value, func(t *testing.T) { diff --git a/cmd/bd/serve_count_proxied_integration_test.go b/cmd/bd/serve_count_proxied_integration_test.go index 577a09d7e..9747f40c4 100644 --- a/cmd/bd/serve_count_proxied_integration_test.go +++ b/cmd/bd/serve_count_proxied_integration_test.go @@ -255,6 +255,51 @@ func TestProxiedServerServeCount(t *testing.T) { } }) + // PR #7199 REVIEW: exclude_status against a WORKSPACE CUSTOM STATUS, over + // the wire and with include_infra left unset. The fake-role tests in + // internal/httpapi cannot write this case — a fake cannot be asked whether a + // custom status is in its own vocabulary — and the bug this pins lived below + // the HTTP layer anyway (storecounter and uow's countFilter only loaded the + // workspace's list configuration when include_infra was set, so + // BuildCountFilter's validation refused a status the workspace itself + // defined unless the caller also happened to pass the unrelated flag). + t.Run("exclude_status accepts a workspace custom status without include_infra", func(t *testing.T) { + label := countSliceLabel + "-customstatus" + scopedCustom := "?label=" + label + + if out, err := bdProxiedRun(t, bd, p.dir, "config", "set", "status.custom", "review,qa"); err != nil { + t.Fatalf("bd config set status.custom: %v\n%s", err, out) + } + + bdProxiedCreate(t, bd, p.dir, "custom status open row", "-p", "2", "-l", label) + reviewRow := bdProxiedCreate(t, bd, p.dir, "custom status review row", "-p", "2", "-l", label) + if out, err := bdProxiedRun(t, bd, p.dir, "update", reviewRow.ID, "--status", "review"); err != nil { + t.Fatalf("bd update --status review: %v\n%s", err, out) + } + + _, total, _ := sp.countIssues(t, scopedCustom) + if total != 2 { + t.Fatalf("total = %d, want 2", total) + } + + // include_infra is NOT in this query string. Before the fix this 400'd + // with an "invalid exclude-status" refusal because the config load that + // feeds BuildCountFilter's vocabulary check was gated on include_infra + // alone. + status, narrowed, _ := sp.countIssues(t, scopedCustom+"&exclude_status=review") + if status != http.StatusOK { + t.Fatalf("exclude_status=review (a workspace custom status, include_infra unset): status = %d, want 200", status) + } + if narrowed != 1 { + t.Fatalf("exclude_status=review counted %d, want 1", narrowed) + } + + // The parity oracle, same as the sibling cases above. + if cli := bdProxiedCountTotal(t, bd, p.dir, "--label", label, "--exclude-status", "review"); cli != narrowed { + t.Errorf("bd count --exclude-status review = %d, the server said %d", cli, narrowed) + } + }) + // The refusals, over the wire: a closed vocabulary and a typed parameter // set, both refused before any query runs. t.Run("the document's refusals are refused", func(t *testing.T) { diff --git a/internal/httpapi/apigen/types.gen.go b/internal/httpapi/apigen/types.gen.go index 0e414bbc2..1edee6f7b 100644 --- a/internal/httpapi/apigen/types.gen.go +++ b/internal/httpapi/apigen/types.gen.go @@ -1010,7 +1010,7 @@ type ContextResponse struct { // OPTIONAL, and absent means only that this server does not disclose its filesystem layout — never that it has no workspace. A client MUST NOT require it, MUST NOT treat absence as an error, and has no use for the value beyond display: it is a path on the SERVER's filesystem, which the client cannot open. Identify the workspace by `project_id` and `database`, which are required. BeadsDir *string `json:"beads_dir,omitempty"` - // Capabilities The tokens this server advertises: the OPERATIONS it implements, derived from its route table, and the server-wide BEHAVIORS it enforces. v0's operation vocabulary is `ready.list`, `ready.count`, `issues.list`, `issues.query`, `issues.count`, `issues.get`, `issues.related`, `issues.create`, `issues.addComment`, `issues.batchClose`, `issues.claim`, `issues.claimNext`, `issues.release`, `issues.close`, `issues.reopen`, `issues.update`, `issues.sweep`, `issues.delete`, `issues.batchCreate`, `issues.batchApply`, `stats.get`, `config.list`, `config.get`, `config.set`, `config.unset`, `dependencies.cycles`, `dependencies.list`, `dependencies.count`, `dependencies.blocking`, `dependencies.tree`, `dependencies.add`, `dependencies.remove`, `memories.list`, `memories.get`, `memories.remember`, `memories.forget`, `events.list`, `events.watch`, `issues.casMetadata`; the behavior tokens are `project.enforce`, which announces that a `Bd-Project-Id` stamp for the wrong workspace is refused here rather than silently ignored, `issues.batchApplyLarge`, which announces that `POST /v0/beads/issues:batchApply` accepts the RAISED envelope — up to 1000 items (`ApplyBatchRequest.items.maxItems`), a 16 MiB body, and (for a request over 100 items) a run budget that EXTENDS to an operator-configured ceiling (`bd serve --large-apply-ceiling`, 5 minutes by default) — rather than the original 100-item, 4 MiB bound. The ceiling is a flat whole-run budget, not a value scaled by item count: a 101-item request and a 1000-item request both get the same extended budget. A request at or under 100 items is unaffected either way and keeps the server's ordinary deadline exactly as before. A client that checks for this token before sending a plan over 100 items learns whether THIS server accepts it without paying for a round trip an older server would refuse anyway, and `issues.list.sort`, which announces that `GET /v0/beads/issues` accepts the `sort` query parameter (two closed, pageable total orders — see that parameter) rather than silently answering `unknown_parameter`. The list grows additively, and an operation never appears here unless it is fully implemented. This is how a client checks for an operation or a behavior — never the version string. + // Capabilities The tokens this server advertises: the OPERATIONS it implements, derived from its route table, and the server-wide BEHAVIORS it enforces. v0's operation vocabulary is `ready.list`, `ready.count`, `issues.list`, `issues.query`, `issues.count`, `issues.get`, `issues.related`, `issues.create`, `issues.addComment`, `issues.batchClose`, `issues.claim`, `issues.claimNext`, `issues.release`, `issues.close`, `issues.reopen`, `issues.update`, `issues.sweep`, `issues.delete`, `issues.batchCreate`, `issues.batchApply`, `stats.get`, `config.list`, `config.get`, `config.set`, `config.unset`, `dependencies.cycles`, `dependencies.list`, `dependencies.count`, `dependencies.blocking`, `dependencies.tree`, `dependencies.add`, `dependencies.remove`, `memories.list`, `memories.get`, `memories.remember`, `memories.forget`, `events.list`, `events.watch`, `issues.casMetadata`; the behavior tokens are `project.enforce`, which announces that a `Bd-Project-Id` stamp for the wrong workspace is refused here rather than silently ignored, `issues.batchApplyLarge`, which announces that `POST /v0/beads/issues:batchApply` accepts the RAISED envelope — up to 1000 items (`ApplyBatchRequest.items.maxItems`), a 16 MiB body, and (for a request over 100 items) a run budget that EXTENDS to an operator-configured ceiling (`bd serve --large-apply-ceiling`, 5 minutes by default) — rather than the original 100-item, 4 MiB bound. The ceiling is a flat whole-run budget, not a value scaled by item count: a 101-item request and a 1000-item request both get the same extended budget. A request at or under 100 items is unaffected either way and keeps the server's ordinary deadline exactly as before. A client that checks for this token before sending a plan over 100 items learns whether THIS server accepts it without paying for a round trip an older server would refuse anyway, and `issues.list.sort`, which announces that `GET /v0/beads/issues` accepts the `sort` query parameter (two closed, pageable total orders — see that parameter) rather than silently answering `unknown_parameter`, and `issues.count.scope`, which announces that `GET /v0/beads/issues:count` accepts `parent`, `no_parent`, `exclude_type`, and `exclude_status` (see those parameters) rather than silently answering `unknown_parameter` for all four. The list grows additively, and an operation never appears here unless it is fully implemented. This is how a client checks for an operation or a behavior — never the version string. // // THIS LIST IS BUILD-LEVEL, NOT WORKSPACE-LEVEL. It says which operations this binary serves, and for every entry but two that is the whole answer. `events.list` and `events.watch` are the exceptions: the durable events journal is a per-workspace setting that is OFF by default, so a server that advertises them may still refuse every request to both with 409 `events_journal_disabled` — correctly, because the operations exist and the workspace has no journal. A consumer of either MUST treat the capability as "this server speaks it" and the 409 as "not on this workspace", and must not read the capability as a promise that records will arrive. Capabilities []string `json:"capabilities"` @@ -2464,6 +2464,30 @@ type CountIssuesParams struct { // Unset — and with `include_ephemeral` also unset — the count is durable-plane only and applies none of the four: the historical `bd count` answer, kept exactly so a scripted caller reads the same number it read yesterday. IncludeInfra *bool `form:"include_infra,omitempty" json:"include_infra,omitempty"` + // Parent Restrict to one issue's children through one predicate with two arms: a parent-child dependency edge onto `parent`, OR — for an issue carrying no parent-child edge at all — a dotted-id prefix match (`parent.anything`). NEITHER ARM WALKS. The edge arm stops at one level, so a grandchild with an edge of its own is not counted, while the prefix arm reaches every edge-less dotted id at any depth (`X.1.1` as well as `X.1`). It is the same predicate `GET /v0/beads/issues`'s own `parent` applies, so `bd count --parent X` agrees in cardinality with `bd list --parent X --flat --all` (or `--json`) for the shared part of the predicate — NOT with a bare `bd list --parent X --all`, whose default tree mode walks every descendant reachable through either arm (grandchildren and below) by re-querying this same predicate once per level client-side (cmd/bd/list_show_filter_modes.go). `--flat` and `--json` both skip that walk and return this field's answer directly. + // + // Sending it beside `no_parent` IS refused: `invalid_argument` on `no_parent` with `reason: invalid_value`, matching `bd list`'s CLI refusal of the same combination. + // + // Behind the `issues.count.scope` behavior token: an older server answers `unknown_parameter` for this name. + Parent *string `form:"parent,omitempty" json:"parent,omitempty"` + + // NoParent Only issues carrying NO parent-child dependency edge. Unlike `parent`, this does NOT consult the dotted-id convention — an issue named `X.1` with no edge to `X` is still counted here, which is the one place `parent` and `no_parent` are not exact opposites of each other. + // + // Sending it beside `parent` IS refused: `invalid_argument` on this parameter with `reason: invalid_value`, matching `bd list`'s CLI refusal of the same combination (cmd/bd/list_input.go) rather than the empty-intersection treatment `assignee`/`no_assignee` get. + // + // Behind the `issues.count.scope` behavior token: an older server answers `unknown_parameter` for this name. + NoParent *bool `form:"no_parent,omitempty" json:"no_parent,omitempty"` + + // ExcludeType Issue types to exclude from the count. Repeat the parameter, or pass a comma-separated list; entries are split and trimmed inside the role and are NOT validated against this workspace's vocabulary — an unrecognized name excludes nothing rather than failing, `status`'s own treatment. It is NOT ignored when `type` is set (unlike `GET /v0/beads/ready`'s `exclude_type`): the two compose, and it also composes with the exclusions `include_infra` applies on its own rather than replacing them. + // + // Behind the `issues.count.scope` behavior token: an older server answers `unknown_parameter` for this name. + ExcludeType *[]string `form:"exclude_type,omitempty" json:"exclude_type,omitempty"` + + // ExcludeStatus Statuses to exclude from the count. Repeat the parameter, or pass a comma-separated list; entries are taken as written (no normalization) but — UNLIKE `status` and `exclude_type` above — each name IS validated against this workspace's vocabulary (built-in statuses plus this workspace's own custom ones): an unrecognized name is `invalid_argument` on this parameter with `reason: invalid_value`, rather than silently excluding nothing and overcounting. There is no listing counterpart to this parameter: `GET /v0/beads/issues` computes its default status exclusions internally from workspace configuration and exposes no equivalent knob, so this is a Count-only capability. + // + // Behind the `issues.count.scope` behavior token: an older server answers `unknown_parameter` for this name. + ExcludeStatus *[]string `form:"exclude_status,omitempty" json:"exclude_status,omitempty"` + // GroupBy Bucket the count by one dimension and return `groups` beside `total`. Absent, the response carries `total` alone. // // The set is CLOSED, and a value outside it is a `400` rather than an empty answer: a caller that misspelled a dimension and got zero buckets back has no way to tell that from a workspace with nothing in it. That is the role's own rule, applied at the edge here so the refusal names the parameter. diff --git a/internal/httpapi/count_test.go b/internal/httpapi/count_test.go index 986455704..77ab524e9 100644 --- a/internal/httpapi/count_test.go +++ b/internal/httpapi/count_test.go @@ -117,6 +117,10 @@ func TestCountForwardsEveryDocumentedParameter(t *testing.T) { "has_metadata_key": {"audit_ref"}, "include_infra": {"true"}, "include_ephemeral": {"true"}, + "parent": {"bd-9"}, + "no_parent": {"true"}, + "exclude_type": {"wisp", "gate"}, + "exclude_status": {"closed", "archived"}, }.Encode()) if resp.StatusCode != http.StatusOK { t.Fatalf("status = %d, want 200: %s", resp.StatusCode, readAll(t, resp)) @@ -169,6 +173,11 @@ func TestCountForwardsEveryDocumentedParameter(t *testing.T) { IncludeInfra: true, IncludeEphemeral: true, + + ParentID: "bd-9", + NoParent: true, + ExcludeTypes: []string{"wisp", "gate"}, + ExcludeStatus: []string{"closed", "archived"}, } if !reflect.DeepEqual(got[0], want) { t.Errorf("request = %+v\nwant %+v", got[0], want) @@ -645,6 +654,10 @@ var countFieldForParameter = map[string]string{ "has_metadata_key": "HasMetadataKey", "include_infra": "IncludeInfra", "include_ephemeral": "IncludeEphemeral", + "parent": "ParentID", + "no_parent": "NoParent", + "exclude_type": "ExcludeTypes", + "exclude_status": "ExcludeStatus", } // TestEveryCountRequestFieldIsPublished: the wire publishes every filter the diff --git a/internal/httpapi/problem.go b/internal/httpapi/problem.go index eeee88f25..31e336eac 100644 --- a/internal/httpapi/problem.go +++ b/internal/httpapi/problem.go @@ -741,11 +741,17 @@ var operationCodes = map[string][]Code{ // Its 400s come from both the transport and the ROLE. The transport refuses // malformed values, repeated single-valued parameters and a `group_by` // outside the closed set. countGroupOf stops that last case at the edge. - // The role has exactly one reachable refusal: BuildCountFilter rejects an - // invalid metadata key, from `metadata_field` or `has_metadata_key`. - // failReadErr classifies it through invalidFilterParam as a 400 naming the - // parameter it came from. An unrecognized status or type is not a refusal; - // the role promises it matches nothing and answers 0. + // The role has three reachable refusals, all from BuildCountFilter: an + // invalid metadata key (`metadata_field` or `has_metadata_key`), an + // unrecognized `exclude_status` name, and `parent` set together with + // `no_parent` (named `no_parent`). The last two carry ErrValidation; the + // metadata-key refusal does not, being a plain error from + // ValidateMetadataFilters, which the list and ready builders share. + // failReadErr classifies all three through invalidFilterParam by message + // prefix, not by sentinel, as a 400 naming the parameter each came from. + // An unrecognized `status` or `type` is not a refusal; the role promises + // it matches nothing and answers 0. Nor is an unrecognized `exclude_type`, + // which excludes nothing. OpCountIssues: {CodeInvalidArgument, CodeUnauthenticated, CodeBusy, CodeDBUnavailable, CodeInternal}, // The listing's vocabulary minus the cursor: this operation has none, so // invalid_cursor cannot arise. An unparseable EXPRESSION is an diff --git a/internal/httpapi/reads.go b/internal/httpapi/reads.go index 1ab132ed8..988f6298e 100644 --- a/internal/httpapi/reads.go +++ b/internal/httpapi/reads.go @@ -229,6 +229,19 @@ func countFilters(q *query) issueops.CountRequest { // it reaches the role as its own field and never as IncludeInfra: folded // into it, a plane-only count would also lose its templates and gates. IncludeEphemeral: q.boolean("include_ephemeral"), + + // The four scope fields behind `issues.count.scope` (S8): ParentID and + // ExcludeTypes are read exactly as the listing reads them (q.str("parent"), + // q.list("exclude_type")), because the role documents its own ParentID and + // ExcludeTypes as spelling ListRequest's fields identically. NoParent and + // ExcludeStatus have no listing counterpart to mirror — NoParent is a new + // boolean switch and ExcludeStatus is a Count-only capability the listing + // never exposed — so both are read with this file's own conventions for + // their kind (q.boolean, q.list) rather than a listing precedent. + ParentID: q.str("parent"), + NoParent: q.boolean("no_parent"), + ExcludeTypes: q.list("exclude_type"), + ExcludeStatus: q.list("exclude_status"), } } @@ -598,6 +611,15 @@ func invalidFilterParam(err error) (string, bool) { switch { case strings.HasPrefix(msg, "invalid status "): return "status", true + // The count role's own refusals (S8 review fix): a misspelled + // --exclude-status entry, and --parent set together with --no-parent. + // Both are the ROLE's ErrValidation. They are named here the same way as + // the `status` row above and the metadata-key rows below, so the client + // learns which parameter to fix rather than reading an unclassified 500. + case strings.HasPrefix(msg, "invalid exclude-status "): + return "exclude_status", true + case strings.HasPrefix(msg, "--parent and --no-parent are mutually exclusive"): + return "no_parent", true case strings.HasPrefix(msg, "invalid issue type "): return "type", true case strings.HasPrefix(msg, "invalid sort policy "): diff --git a/internal/httpapi/reads_test.go b/internal/httpapi/reads_test.go index 672f5147e..51cbb88ab 100644 --- a/internal/httpapi/reads_test.go +++ b/internal/httpapi/reads_test.go @@ -161,6 +161,8 @@ func TestABuilderRefusalIsTheDocumentedBadRequest(t *testing.T) { {"a has-metadata key on the ready surface", "/v0/beads/ready?has_metadata_key=1bad", "has_metadata_key"}, {"a metadata field key on the count surface", "/v0/beads/issues:count?metadata_field=1bad=x", "metadata_field"}, {"a has-metadata key on the count surface", "/v0/beads/issues:count?has_metadata_key=1bad", "has_metadata_key"}, + {"an unknown exclude-status name on the count surface", "/v0/beads/issues:count?exclude_status=bogus", "exclude_status"}, + {"parent and no_parent together on the count surface", "/v0/beads/issues:count?parent=bd-1&no_parent=true", "no_parent"}, } { t.Run(tc.name, func(t *testing.T) { ts, _ := newReadServer(t, Config{}) diff --git a/internal/httpapi/routes.go b/internal/httpapi/routes.go index cb7a84b76..1fe26aab0 100644 --- a/internal/httpapi/routes.go +++ b/internal/httpapi/routes.go @@ -76,6 +76,49 @@ const CapBatchApplyLarge = "issues.batchApplyLarge" // baseline is built from. const CapIssuesListSort = "issues.list.sort" +// CapIssuesCountScope is the behavior capability that advertises GET +// /v0/beads/issues:count's `parent`, `no_parent`, `exclude_type`, and +// `exclude_status` parameters (reads.go countFilters, spec OpCountIssues). +// Like CapIssuesListSort it names FOUR PARAMETERS added to an existing +// operation rather than a route of its own — `issues.count` is already the +// per-operation token for the route itself — so it rides the same +// behaviorCapabilities list and one token covers all four (internal/beads +// design doc "Count scope", S8): they shipped together, through the one +// shared workapi.BuildCountFilter builder, so there is no partial-support +// state a finer-grained token would need to distinguish. +// +// This is the TestNewParameterOnExistingOperationHasABehaviorToken gate +// CapIssuesListSort's doc describes: `issues:count` is a pretoken-baseline +// operation (internal/httpapi/testdata/pretoken_operations.json), so these +// four NEW parameters on it must each cite a served token in their own +// OpenAPI description, and this is that token. +// +// CLIENT-SKEW NOTE FOR S3/S4 (no OSS HTTP client exists yet to wire this +// into): a server predating this token answers any of the four parameters +// with `400 invalid_argument`/`reason: "unknown_parameter"` naming the +// parameter — the per-parameter capability probe every behavior token on +// this operation already relies on (see CapIssuesListSort). When the HTTP +// client lands, it MUST check `issues.count.scope` against the cached +// `ContextResponse.capabilities` from the handshake and refuse LOCALLY with +// a typed `ErrUnsupported{Capability: "issues.count.scope"}` (or an +// equivalent typed error carrying the token) before sending a request that +// sets `ParentID`, `NoParent`, `ExcludeTypes` or `ExcludeStatus` to an older +// server — never let the caller pay for a round trip that 400s anyway, and +// never silently drop the fields and return a wider count than asked for. +// +// THE DOWNSTREAM FALLBACK THIS PROTECTS (S8 review, follow-up #6): gc's own +// client maps that local `ErrUnsupported{Capability: "issues.count.scope"}` +// refusal to its own `ErrCountUnsupported` and falls back to the List role for +// the same predicate (beads-design DESIGN.txt §3.2, "gc maps this to +// ErrCountUnsupported and falls back to List") rather than surfacing the +// refusal to its own caller or guessing at a count. S3/S4, when they build +// that client, MUST add a skew test that masks this token out of a handshake +// response and asserts the fallback actually fires — not merely that the +// local refusal is raised — because a fallback that compiles but never runs +// in CI is indistinguishable from one that silently regressed to a wrong +// count. +const CapIssuesCountScope = "issues.count.scope" + // customMethodTarget splits the custom method off the segment the router // matched, and reports the row that claims it. // @@ -810,7 +853,7 @@ func (r route) specPathOf() string { // route. project.enforce announces per-request Bd-Project-Id enforcement // (checkProjectStamp): a stamped client reads it to know the refusal is available // rather than silently dropped by an older server. -var behaviorCapabilities = []string{CapProjectEnforce, CapBatchApplyLarge, CapIssuesListSort} +var behaviorCapabilities = []string{CapProjectEnforce, CapBatchApplyLarge, CapIssuesListSort, CapIssuesCountScope} // Capabilities lists what this build advertises in ContextResponse.capabilities: // the operations it actually implements, gated on `implemented` so a stub can diff --git a/internal/httpapi/server_test.go b/internal/httpapi/server_test.go index 0b5799395..291c7fdf2 100644 --- a/internal/httpapi/server_test.go +++ b/internal/httpapi/server_test.go @@ -635,7 +635,7 @@ func TestCapabilitiesAdvertiseEveryImplementedOperation(t *testing.T) { // them; project.enforce and issues.batchApplyLarge are advertised in the // same list. Both are spelled literally here to keep this an independent // oracle rather than a second call to the code under test. - want = append(want, "project.enforce", "issues.batchApplyLarge", "issues.list.sort") + want = append(want, "project.enforce", "issues.batchApplyLarge", "issues.list.sort", "issues.count.scope") slices.Sort(want) if !slices.Equal(got, want) { t.Errorf("capabilities = %v, want %v", got, want) diff --git a/internal/httpapi/spec/openapi.v0.yaml b/internal/httpapi/spec/openapi.v0.yaml index 356032b1f..4b36dee2d 100644 --- a/internal/httpapi/spec/openapi.v0.yaml +++ b/internal/httpapi/spec/openapi.v0.yaml @@ -2068,6 +2068,102 @@ paths: schema: type: boolean default: false + - name: parent + in: query + description: >- + Restrict to one issue's children through one predicate with two + arms: a parent-child dependency edge onto `parent`, OR — for an + issue carrying no parent-child edge at all — a dotted-id prefix + match (`parent.anything`). NEITHER ARM WALKS. The edge arm stops + at one level, so a grandchild with an edge of its own is not + counted, while the prefix arm reaches every edge-less dotted id at + any depth (`X.1.1` as well as `X.1`). It is the same predicate + `GET /v0/beads/issues`'s own `parent` applies, so `bd count + --parent X` agrees in cardinality with `bd list --parent X --flat + --all` (or `--json`) for the shared part of the predicate — NOT + with a bare `bd list --parent X --all`, whose default tree mode + walks every descendant reachable through either arm (grandchildren + and below) by re-querying this same predicate once per level + client-side + (cmd/bd/list_show_filter_modes.go). `--flat` and `--json` both + skip that walk and return this field's answer directly. + + + Sending it beside `no_parent` IS refused: `invalid_argument` on + `no_parent` with `reason: invalid_value`, matching `bd list`'s CLI + refusal of the same combination. + + + Behind the `issues.count.scope` behavior token: an older server + answers `unknown_parameter` for this name. + schema: + type: string + - name: no_parent + in: query + description: >- + Only issues carrying NO parent-child dependency edge. Unlike + `parent`, this does NOT consult the dotted-id convention — an + issue named `X.1` with no edge to `X` is still counted here, which + is the one place `parent` and `no_parent` are not exact opposites + of each other. + + + Sending it beside `parent` IS refused: `invalid_argument` on this + parameter with `reason: invalid_value`, matching `bd list`'s CLI + refusal of the same combination (cmd/bd/list_input.go) rather than + the empty-intersection treatment `assignee`/`no_assignee` get. + + + Behind the `issues.count.scope` behavior token: an older server + answers `unknown_parameter` for this name. + schema: + type: boolean + default: false + - name: exclude_type + in: query + description: >- + Issue types to exclude from the count. Repeat the parameter, or + pass a comma-separated list; entries are split and trimmed inside + the role and are NOT validated against this workspace's + vocabulary — an unrecognized name excludes nothing rather than + failing, `status`'s own treatment. It is NOT ignored when `type` + is set (unlike `GET /v0/beads/ready`'s `exclude_type`): the two + compose, and it also composes with the exclusions `include_infra` + applies on its own rather than replacing them. + + + Behind the `issues.count.scope` behavior token: an older server + answers `unknown_parameter` for this name. + style: form + explode: true + schema: + type: array + items: + type: string + - name: exclude_status + in: query + description: >- + Statuses to exclude from the count. Repeat the parameter, or pass + a comma-separated list; entries are taken as written (no + normalization) but — UNLIKE `status` and `exclude_type` above — + each name IS validated against this workspace's vocabulary + (built-in statuses plus this workspace's own custom ones): an + unrecognized name is `invalid_argument` on this parameter with + `reason: invalid_value`, rather than silently excluding nothing + and overcounting. There is no listing counterpart to this + parameter: `GET /v0/beads/issues` computes its default status + exclusions internally from workspace configuration and exposes no + equivalent knob, so this is a Count-only capability. + + + Behind the `issues.count.scope` behavior token: an older server + answers `unknown_parameter` for this name. + style: form + explode: true + schema: + type: array + items: + type: string - name: group_by in: query description: >- @@ -7514,7 +7610,11 @@ components: older server would refuse anyway, and `issues.list.sort`, which announces that `GET /v0/beads/issues` accepts the `sort` query parameter (two closed, pageable total orders — see that - parameter) rather than silently answering `unknown_parameter`. The + parameter) rather than silently answering `unknown_parameter`, and + `issues.count.scope`, which announces that + `GET /v0/beads/issues:count` accepts `parent`, `no_parent`, + `exclude_type`, and `exclude_status` (see those parameters) rather + than silently answering `unknown_parameter` for all four. The list grows additively, and an operation never appears here unless it is fully implemented. This is how a client checks for an operation or a behavior — never the version string. diff --git a/internal/httpapi/wireshape/testdata/golden.json b/internal/httpapi/wireshape/testdata/golden.json index 608ede82a..30ff61ebe 100644 --- a/internal/httpapi/wireshape/testdata/golden.json +++ b/internal/httpapi/wireshape/testdata/golden.json @@ -4594,6 +4594,24 @@ "explode": true, "default": "false" }, + { + "schema": "param:countIssues", + "member": "query:exclude_status", + "type": "array", + "required": false, + "item_type": "string", + "style": "form", + "explode": true + }, + { + "schema": "param:countIssues", + "member": "query:exclude_type", + "type": "array", + "required": false, + "item_type": "string", + "style": "form", + "explode": true + }, { "schema": "param:countIssues", "member": "query:group_by", @@ -4688,6 +4706,15 @@ "explode": true, "default": "false" }, + { + "schema": "param:countIssues", + "member": "query:no_parent", + "type": "boolean", + "required": false, + "style": "form", + "explode": true, + "default": "false" + }, { "schema": "param:countIssues", "member": "query:notes_contains", @@ -4696,6 +4723,14 @@ "style": "form", "explode": true }, + { + "schema": "param:countIssues", + "member": "query:parent", + "type": "string", + "required": false, + "style": "form", + "explode": true + }, { "schema": "param:countIssues", "member": "query:priority", diff --git a/internal/storage/dolt/counter_contract_test.go b/internal/storage/dolt/counter_contract_test.go index 9047ed925..1166eef66 100644 --- a/internal/storage/dolt/counter_contract_test.go +++ b/internal/storage/dolt/counter_contract_test.go @@ -65,6 +65,33 @@ func TestCounterContract(t *testing.T) { t.Run("WritesNothing", func(t *testing.T) { conformance.RunCounterWritesNothing(t, ctx, fixture) }) + t.Run("ParentIDScopesToChildren", func(t *testing.T) { + conformance.RunCounterParentIDScopesToChildren(t, ctx, fixture) + }) + t.Run("NoParentExcludesChildren", func(t *testing.T) { + conformance.RunCounterNoParentExcludesChildren(t, ctx, fixture) + }) + t.Run("ExcludeTypesNarrowsThePredicate", func(t *testing.T) { + conformance.RunCounterExcludeTypesNarrowsThePredicate(t, ctx, fixture) + }) + t.Run("ExcludeStatusNarrowsThePredicate", func(t *testing.T) { + conformance.RunCounterExcludeStatusNarrowsThePredicate(t, ctx, fixture) + }) + t.Run("ParentIDMatchesListCardinality", func(t *testing.T) { + conformance.RunCounterParentIDMatchesListCardinality(t, ctx, fixture) + }) + t.Run("NoParentMatchesListCardinality", func(t *testing.T) { + conformance.RunCounterNoParentMatchesListCardinality(t, ctx, fixture) + }) + t.Run("ExcludeTypesMatchesListCardinality", func(t *testing.T) { + conformance.RunCounterExcludeTypesMatchesListCardinality(t, ctx, fixture) + }) + t.Run("ParentIDIncludesAWispChild", func(t *testing.T) { + conformance.RunCounterParentIDIncludesAWispChild(t, ctx, fixture) + }) + t.Run("ParentIDAndExcludeStatusComposeOnAClosedChild", func(t *testing.T) { + conformance.RunCounterParentIDAndExcludeStatusComposeOnAClosedChild(t, ctx, fixture) + }) } func newDoltCounterFixture(t *testing.T, prefix string) (conformance.CounterFixture, context.Context, func()) { @@ -78,12 +105,20 @@ func newDoltCounterFixture(t *testing.T, prefix string) (conformance.CounterFixt t.Fatalf("Counter(): %v", err) } kit := newDoltRoleFixtureKit(store, prefix) + reader, err := store.IssueReader() + if err != nil { + cancel() + storeCleanup() + t.Fatalf("IssueReader(): %v", err) + } fixture := conformance.CounterFixture{ - IssuePrefix: kit.IssuePrefix, - Counter: counter, - CreateIssue: kit.CreateIssue, - CreateWisp: kit.CreateWisp, - CountHistory: kit.CountHistory, + IssuePrefix: kit.IssuePrefix, + Counter: counter, + CreateIssue: kit.CreateIssue, + CreateWisp: kit.CreateWisp, + CountHistory: kit.CountHistory, + AddDependency: kit.AddDependency, + List: reader.List, } return fixture, ctx, func() { cancel() diff --git a/internal/storage/embeddeddolt/counter_contract_test.go b/internal/storage/embeddeddolt/counter_contract_test.go index dd6e3f167..b39570cb3 100644 --- a/internal/storage/embeddeddolt/counter_contract_test.go +++ b/internal/storage/embeddeddolt/counter_contract_test.go @@ -6,6 +6,8 @@ import ( "testing" "github.com/steveyegge/beads/backend/conformance" + "github.com/steveyegge/beads/internal/types" + publicops "github.com/steveyegge/beads/issueops" ) // TestCounterContract runs the Counter contract against the embedded store, @@ -68,6 +70,33 @@ func TestCounterContract(t *testing.T) { t.Run("WritesNothing", func(t *testing.T) { conformance.RunCounterWritesNothing(t, ctx, fixture) }) + t.Run("ParentIDScopesToChildren", func(t *testing.T) { + conformance.RunCounterParentIDScopesToChildren(t, ctx, fixture) + }) + t.Run("NoParentExcludesChildren", func(t *testing.T) { + conformance.RunCounterNoParentExcludesChildren(t, ctx, fixture) + }) + t.Run("ExcludeTypesNarrowsThePredicate", func(t *testing.T) { + conformance.RunCounterExcludeTypesNarrowsThePredicate(t, ctx, fixture) + }) + t.Run("ExcludeStatusNarrowsThePredicate", func(t *testing.T) { + conformance.RunCounterExcludeStatusNarrowsThePredicate(t, ctx, fixture) + }) + t.Run("ParentIDMatchesListCardinality", func(t *testing.T) { + conformance.RunCounterParentIDMatchesListCardinality(t, ctx, fixture) + }) + t.Run("NoParentMatchesListCardinality", func(t *testing.T) { + conformance.RunCounterNoParentMatchesListCardinality(t, ctx, fixture) + }) + t.Run("ExcludeTypesMatchesListCardinality", func(t *testing.T) { + conformance.RunCounterExcludeTypesMatchesListCardinality(t, ctx, fixture) + }) + t.Run("ParentIDIncludesAWispChild", func(t *testing.T) { + conformance.RunCounterParentIDIncludesAWispChild(t, ctx, fixture) + }) + t.Run("ParentIDAndExcludeStatusComposeOnAClosedChild", func(t *testing.T) { + conformance.RunCounterParentIDAndExcludeStatusComposeOnAClosedChild(t, ctx, fixture) + }) } func newEmbeddedCounterFixture(t *testing.T, te *testEnv, prefix string) conformance.CounterFixture { @@ -76,12 +105,82 @@ func newEmbeddedCounterFixture(t *testing.T, te *testEnv, prefix string) conform if err != nil { t.Fatalf("Counter(): %v", err) } + reader, err := te.store.IssueReader() + if err != nil { + t.Fatalf("IssueReader(): %v", err) + } kit := newEmbeddedRoleFixtureKit(te, prefix) return conformance.CounterFixture{ - IssuePrefix: kit.IssuePrefix, - Counter: counter, - CreateIssue: kit.CreateIssue, - CreateWisp: kit.CreateWisp, - CountHistory: kit.CountHistory, + IssuePrefix: kit.IssuePrefix, + Counter: counter, + CreateIssue: kit.CreateIssue, + CreateWisp: kit.CreateWisp, + CountHistory: kit.CountHistory, + AddDependency: kit.AddDependency, + List: reader.List, + } +} + +// TestCounterExcludeStatusAcceptsAWorkspaceCustomStatusWithoutIncludeInfra +// pins the PR #7199 review's blocker fix: the store-backed Counter must load +// the workspace's list configuration when ExcludeStatus is non-empty, not +// only when IncludeInfra is set, so BuildCountFilter's validation sees the +// workspace's own custom statuses. A leg-level test, not a conformance case, +// because CounterFixture cannot drive a workspace-configured custom status +// (counter_contract.go's CounterFixture doc) — this needs a real store with +// status.custom set. +// +// Reproduces the reviewer's CLI repro directly against the store: with +// status.custom="review,qa", an issue moved to "review", and IncludeInfra +// left unset, `--exclude-status review` must narrow the count instead of +// being refused as an unrecognized status. +func TestCounterExcludeStatusAcceptsAWorkspaceCustomStatusWithoutIncludeInfra(t *testing.T) { + skipUnlessEmbeddedDolt(t) + te := newTestEnv(t, "cntcs") + ctx := t.Context() + + if err := te.store.SetConfig(ctx, "status.custom", "review,qa"); err != nil { + t.Fatalf("SetConfig(status.custom): %v", err) + } + + const openID = "cntcs-open" + const reviewID = "cntcs-review" + for _, id := range []string{openID, reviewID} { + if err := te.store.CreateIssue(ctx, &types.Issue{ + ID: id, Title: id, Status: types.StatusOpen, Priority: 2, IssueType: types.TypeTask, + }, "tester"); err != nil { + t.Fatalf("CreateIssue(%s): %v", id, err) + } + } + if err := te.store.UpdateIssue(ctx, reviewID, map[string]interface{}{ + "status": types.Status("review"), + }, "tester"); err != nil { + t.Fatalf("UpdateIssue(%s) to review: %v", reviewID, err) + } + + counter, err := te.store.Counter() + if err != nil { + t.Fatalf("Counter(): %v", err) + } + + scope := publicops.CountRequest{IDFilter: openID + "," + reviewID} + total, err := counter.Count(ctx, scope) + if err != nil { + t.Fatalf("Count(unfiltered): %v", err) + } + if total.Total != 2 { + t.Fatalf("Count(unfiltered).Total = %d, want 2", total.Total) + } + + // IncludeInfra left UNSET: before the fix this refused with "invalid + // exclude-status \"review\"" because the config load was gated on + // IncludeInfra alone. + scope.ExcludeStatus = []string{"review"} + result, err := counter.Count(ctx, scope) + if err != nil { + t.Fatalf("Count(exclude-status=review, IncludeInfra unset): %v, want the custom status accepted", err) + } + if result.Total != 1 { + t.Fatalf("Count(exclude-status=review).Total = %d, want 1", result.Total) } } diff --git a/internal/storage/uow/counter.go b/internal/storage/uow/counter.go index 03a155788..467712fa2 100644 --- a/internal/storage/uow/counter.go +++ b/internal/storage/uow/counter.go @@ -85,11 +85,14 @@ func (c *counter) CountByGroup(ctx context.Context, req publicops.CountByGroupRe } // countFilter builds the storage filter from the unit of work the call already -// holds, loading configuration only when IncludeInfra can read it — the same two -// decisions the store-backed body makes, through the same builder. +// holds, loading configuration only when IncludeInfra or ExcludeStatus can +// read it — the same two decisions the store-backed body makes, through the +// same builder. ExcludeStatus needs the workspace's custom status names so +// BuildCountFilter's validation does not refuse a status the workspace itself +// defines (PR #7199 review). func countFilter(ctx context.Context, uw UnitOfWork, req publicops.CountRequest) (types.IssueFilter, error) { var cfg workapi.ListConfig - if req.IncludeInfra { + if req.IncludeInfra || len(req.ExcludeStatus) > 0 { loaded, err := workapi.LoadUOWListConfig(ctx, uw) if err != nil { return types.IssueFilter{}, err diff --git a/internal/storage/uow/counter_contract_test.go b/internal/storage/uow/counter_contract_test.go index 45e4de73b..9936e77e1 100644 --- a/internal/storage/uow/counter_contract_test.go +++ b/internal/storage/uow/counter_contract_test.go @@ -5,6 +5,9 @@ import ( "testing" "github.com/steveyegge/beads/backend/conformance" + "github.com/steveyegge/beads/internal/storage/domain" + "github.com/steveyegge/beads/internal/types" + publicops "github.com/steveyegge/beads/issueops" ) // TestCounterContract runs the Counter contract against the unit-of-work @@ -65,6 +68,33 @@ func TestCounterContract(t *testing.T) { t.Run("WritesNothing", func(t *testing.T) { conformance.RunCounterWritesNothing(t, ctx, fixture) }) + t.Run("ParentIDScopesToChildren", func(t *testing.T) { + conformance.RunCounterParentIDScopesToChildren(t, ctx, fixture) + }) + t.Run("NoParentExcludesChildren", func(t *testing.T) { + conformance.RunCounterNoParentExcludesChildren(t, ctx, fixture) + }) + t.Run("ExcludeTypesNarrowsThePredicate", func(t *testing.T) { + conformance.RunCounterExcludeTypesNarrowsThePredicate(t, ctx, fixture) + }) + t.Run("ExcludeStatusNarrowsThePredicate", func(t *testing.T) { + conformance.RunCounterExcludeStatusNarrowsThePredicate(t, ctx, fixture) + }) + t.Run("ParentIDMatchesListCardinality", func(t *testing.T) { + conformance.RunCounterParentIDMatchesListCardinality(t, ctx, fixture) + }) + t.Run("NoParentMatchesListCardinality", func(t *testing.T) { + conformance.RunCounterNoParentMatchesListCardinality(t, ctx, fixture) + }) + t.Run("ExcludeTypesMatchesListCardinality", func(t *testing.T) { + conformance.RunCounterExcludeTypesMatchesListCardinality(t, ctx, fixture) + }) + t.Run("ParentIDIncludesAWispChild", func(t *testing.T) { + conformance.RunCounterParentIDIncludesAWispChild(t, ctx, fixture) + }) + t.Run("ParentIDAndExcludeStatusComposeOnAClosedChild", func(t *testing.T) { + conformance.RunCounterParentIDAndExcludeStatusComposeOnAClosedChild(t, ctx, fixture) + }) } func newUOWCounterFixture(t *testing.T, ctx context.Context, prefix string) conformance.CounterFixture { @@ -80,12 +110,99 @@ func newUOWCounterFixture(t *testing.T, ctx context.Context, prefix string) conf if err != nil { t.Fatalf("Counter(): %v", err) } + readerSource, ok := provider.(IssueReaderSource) + if !ok { + t.Fatalf("provider %T does not offer the IssueReader accessor", provider) + } + reader, err := readerSource.IssueReader() + if err != nil { + t.Fatalf("IssueReader(): %v", err) + } kit := newUOWRoleFixtureKit(provider, prefix) return conformance.CounterFixture{ - IssuePrefix: kit.IssuePrefix, - Counter: counter, - CreateIssue: kit.CreateIssue, - CreateWisp: kit.CreateWisp, - CountHistory: kit.CountHistory, + IssuePrefix: kit.IssuePrefix, + Counter: counter, + CreateIssue: kit.CreateIssue, + CreateWisp: kit.CreateWisp, + CountHistory: kit.CountHistory, + AddDependency: kit.AddDependency, + List: reader.List, + } +} + +// TestCounterExcludeStatusAcceptsAWorkspaceCustomStatusWithoutIncludeInfra +// pins the PR #7199 review's blocker fix on the unit-of-work leg: countFilter +// must load the workspace's list configuration when ExcludeStatus is +// non-empty, not only when IncludeInfra is set, so BuildCountFilter's +// validation sees the workspace's own custom statuses. A leg-level test, not +// a conformance case, because CounterFixture cannot drive a +// workspace-configured custom status (counter_contract.go's CounterFixture +// doc) — this needs a real provider with status.custom set. +// +// Its own provider (not newUOWCounterFixture's), so status.custom does not +// leak into the other subtests sharing that fixture's one database. +func TestCounterExcludeStatusAcceptsAWorkspaceCustomStatusWithoutIncludeInfra(t *testing.T) { + ctx := context.Background() + provider := newUOWRoleFixtureProvider(t, ctx, "cntcs") + + if err := RunTx(ctx, provider, func(ctx context.Context, uw UnitOfWork) (string, error) { + return "set status.custom", uw.ConfigUseCase().SetConfig(ctx, "status.custom", "review,qa") + }); err != nil { + t.Fatalf("SetConfig(status.custom): %v", err) + } + + const openID = "cntcs-open" + const reviewID = "cntcs-review" + for _, id := range []string{openID, reviewID} { + id := id + if err := RunTx(ctx, provider, func(ctx context.Context, uw UnitOfWork) (string, error) { + _, err := uw.IssueUseCase().CreateIssue(ctx, domain.CreateIssueParams{ + Issue: &types.Issue{ + ID: id, Title: id, Status: types.StatusOpen, Priority: 2, IssueType: types.TypeTask, + }, + ExplicitID: id, + CreateOnly: true, + }, "tester") + return "seed " + id, err + }); err != nil { + t.Fatalf("CreateIssue(%s): %v", id, err) + } + } + if err := RunTx(ctx, provider, func(ctx context.Context, uw UnitOfWork) (string, error) { + return "move to review", uw.IssueUseCase().UpdateIssue(ctx, reviewID, map[string]any{ + "status": types.Status("review"), + }, "tester") + }); err != nil { + t.Fatalf("UpdateIssue(%s) to review: %v", reviewID, err) + } + + source, ok := provider.(CounterSource) + if !ok { + t.Fatalf("provider %T does not offer the Counter accessor", provider) + } + counter, err := source.Counter() + if err != nil { + t.Fatalf("Counter(): %v", err) + } + + scope := publicops.CountRequest{IDFilter: openID + "," + reviewID} + total, err := counter.Count(ctx, scope) + if err != nil { + t.Fatalf("Count(unfiltered): %v", err) + } + if total.Total != 2 { + t.Fatalf("Count(unfiltered).Total = %d, want 2", total.Total) + } + + // IncludeInfra left UNSET: before the fix this refused with "invalid + // exclude-status \"review\"" because the config load was gated on + // IncludeInfra alone. + scope.ExcludeStatus = []string{"review"} + result, err := counter.Count(ctx, scope) + if err != nil { + t.Fatalf("Count(exclude-status=review, IncludeInfra unset): %v, want the custom status accepted", err) + } + if result.Total != 1 { + t.Fatalf("Count(exclude-status=review).Total = %d, want 1", result.Total) } } diff --git a/internal/workapi/count.go b/internal/workapi/count.go index 2aef4adc4..66c1f509b 100644 --- a/internal/workapi/count.go +++ b/internal/workapi/count.go @@ -43,8 +43,11 @@ func ValidateCountGroup(group issueops.CountGroup) (string, error) { // pinned by a golden-style test comparing this builder's output against // BuildListFilter's for the same request (count_test.go, GH#4387). // -// cfg supplies the workspace's infra vocabulary and is only read under -// IncludeInfra; a zero ListConfig falls back to the default infra set. +// cfg supplies the workspace's infra vocabulary and its custom status names. +// It is read under IncludeInfra AND whenever ExcludeStatus is non-empty (the +// exclusion is validated against the workspace's statuses), so a caller must +// load it in both cases; a zero ListConfig falls back to the default infra set +// and the built-in statuses only. func BuildCountFilter(in issueops.CountRequest, cfg ListConfig) (types.IssueFilter, error) { filter := types.IssueFilter{ TitleSearch: in.TitleSearch, @@ -103,6 +106,59 @@ func BuildCountFilter(in issueops.CountRequest, cfg ListConfig) (types.IssueFilt filter.IDs = ids } + // ParentID and NoParent are refused together, matching `bd list`'s CLI + // refusal of the same combination (cmd/bd/list_input.go) with the same + // wording — the role raises it as ErrValidation so a caller reaching this + // builder from any front door (HTTP, proxied CLI, a future client) gets + // the same refusal the primary CLI's flag parser gives, rather than the + // role silently answering the empty intersection. + if in.ParentID != "" && in.NoParent { + return types.IssueFilter{}, fmt.Errorf("--parent and --no-parent are mutually exclusive%.0w", issueops.ErrValidation) + } + if in.ParentID != "" { + parentID := in.ParentID + filter.ParentID = &parentID + } + if in.NoParent { + filter.NoParent = true + } + + // ExcludeTypes is appended to, not assigned: applyCountIncludeInfra (below) + // contributes its own "gate" exclusion, and the two sets must compose + // rather than one silently discarding the other, exactly as + // BuildListFilter's ExcludeTypes and applyTypeSuppressions compose. + for _, raw := range in.ExcludeTypes { + for _, t := range strings.Split(raw, ",") { + t = strings.TrimSpace(t) + if t != "" { + filter.ExcludeTypes = append(filter.ExcludeTypes, types.IssueType(utils.NormalizeIssueType(t))) + } + } + } + + // ExcludeStatus takes names as written but — unlike Status and IssueType + // above — IS validated against the workspace vocabulary (built-in statuses + // plus cfg's custom ones, the same set ApplyStatusFilter checks in + // list.go). An exclusion list is built by hand from a status name, so a + // misspelled entry here would silently exclude nothing and OVERCOUNT + // rather than undercount, which is a worse failure mode than the + // match-nothing treatment Status and IssueType accept above — so a typo + // is ErrValidation instead. + for _, raw := range in.ExcludeStatus { + for _, s := range strings.Split(raw, ",") { + s = strings.TrimSpace(s) + if s == "" { + continue + } + status := types.Status(s) + if !status.IsValidWithCustom(cfg.CustomStatusNames()) { + return types.IssueFilter{}, fmt.Errorf("invalid exclude-status %q (valid: %s)%.0w", + s, ValidStatusList(cfg.CustomStatusNames()), issueops.ErrValidation) + } + filter.ExcludeStatus = append(filter.ExcludeStatus, status) + } + } + if in.IncludeInfra { applyCountIncludeInfra(&filter, in.IssueType, cfg) } else if !in.IncludeEphemeral { diff --git a/internal/workapi/count_test.go b/internal/workapi/count_test.go index 481e035c2..2d8b102fe 100644 --- a/internal/workapi/count_test.go +++ b/internal/workapi/count_test.go @@ -158,6 +158,204 @@ func TestBuildCountFilterNormalizesLabelsAndIDs(t *testing.T) { } } +// TestBuildCountFilterMapsParentAndNoParent pins ParentID/NoParent's mapping +// into types.IssueFilter, mirroring BuildListFilter's ParentID/NoParent +// mapping (internal/workapi/list.go) field for field. +func TestBuildCountFilterMapsParentAndNoParent(t *testing.T) { + got, err := BuildCountFilter(issueops.CountRequest{ParentID: "bd-1"}, ListConfig{}) + if err != nil { + t.Fatalf("BuildCountFilter: %v", err) + } + if got.ParentID == nil || *got.ParentID != "bd-1" { + t.Errorf("ParentID = %v, want \"bd-1\"", countPtrStr(got.ParentID)) + } + if got.NoParent { + t.Errorf("NoParent = true, want false: request did not set it") + } + + got, err = BuildCountFilter(issueops.CountRequest{NoParent: true}, ListConfig{}) + if err != nil { + t.Fatalf("BuildCountFilter: %v", err) + } + if got.ParentID != nil { + t.Errorf("ParentID = %v, want nil: request did not set it", countPtrStr(got.ParentID)) + } + if !got.NoParent { + t.Error("NoParent = false, want true") + } +} + +// TestBuildCountFilterExcludeTypesSplitsAndNormalizes pins ExcludeTypes' +// mapping: comma-separated entries split, trim and normalize exactly as +// BuildListFilter's ExcludeTypes does, and the result APPENDS to (rather than +// replaces) whatever applyCountIncludeInfra later contributes. +func TestBuildCountFilterExcludeTypesSplitsAndNormalizes(t *testing.T) { + got, err := BuildCountFilter(issueops.CountRequest{ + // "feat" is an alias (-> "feature", internal/utils/strings.go); + // "bug" is not, so it passes through unchanged. + ExcludeTypes: []string{" bug , feat", "", "chore"}, + }, ListConfig{}) + if err != nil { + t.Fatalf("BuildCountFilter: %v", err) + } + want := []types.IssueType{"bug", "feature", "chore"} + if !reflect.DeepEqual(got.ExcludeTypes, want) { + t.Errorf("ExcludeTypes = %v, want %v", got.ExcludeTypes, want) + } + + // Composes with IncludeInfra's own "gate" exclusion rather than + // discarding it. + got, err = BuildCountFilter(issueops.CountRequest{ + ExcludeTypes: []string{"chore"}, + IncludeInfra: true, + }, ListConfig{}) + if err != nil { + t.Fatalf("BuildCountFilter: %v", err) + } + want = []types.IssueType{"chore", "gate"} + if !reflect.DeepEqual(got.ExcludeTypes, want) { + t.Errorf("ExcludeTypes = %v, want %v (explicit exclusions composed with IncludeInfra's)", got.ExcludeTypes, want) + } +} + +// TestBuildCountFilterParentFieldsMatchListFilter extends the GH#4387 +// count/list cardinality-parity pin to the three new Count fields that DO +// have a List counterpart: ParentID, NoParent and ExcludeTypes must produce +// the identical types.IssueFilter contribution BuildListFilter does for the +// same request, since a filter contract test (backend/conformance) can only +// show the two AGREE on one backend's rows — this shows WHY, at the one place +// both builders share. +func TestBuildCountFilterParentFieldsMatchListFilter(t *testing.T) { + cfg := ListConfig{} + + countGot, err := BuildCountFilter(issueops.CountRequest{ParentID: "bd-1"}, cfg) + if err != nil { + t.Fatalf("BuildCountFilter: %v", err) + } + listGot, err := BuildListFilter(issueops.ListRequest{ParentID: "bd-1", AllFlag: true}, cfg) + if err != nil { + t.Fatalf("BuildListFilter: %v", err) + } + if !reflect.DeepEqual(countGot.ParentID, listGot.ParentID) { + t.Errorf("Count ParentID = %v, List ParentID = %v, want equal", countPtrStr(countGot.ParentID), countPtrStr(listGot.ParentID)) + } + + countGot, err = BuildCountFilter(issueops.CountRequest{NoParent: true}, cfg) + if err != nil { + t.Fatalf("BuildCountFilter: %v", err) + } + listGot, err = BuildListFilter(issueops.ListRequest{NoParent: true, AllFlag: true}, cfg) + if err != nil { + t.Fatalf("BuildListFilter: %v", err) + } + if countGot.NoParent != listGot.NoParent { + t.Errorf("Count NoParent = %v, List NoParent = %v, want equal", countGot.NoParent, listGot.NoParent) + } + + countGot, err = BuildCountFilter(issueops.CountRequest{ExcludeTypes: []string{"wisp,feat"}}, cfg) + if err != nil { + t.Fatalf("BuildCountFilter: %v", err) + } + // IncludeAllTypes on the list side skips applyTypeSuppressions' OWN + // exclusions (template/gate/infra defaults), isolating the one thing this + // case compares: the explicit --exclude-type mapping. + listGot, err = BuildListFilter(issueops.ListRequest{ExcludeTypes: []string{"wisp,feat"}, AllFlag: true, IncludeAllTypes: true}, cfg) + if err != nil { + t.Fatalf("BuildListFilter: %v", err) + } + if !reflect.DeepEqual(countGot.ExcludeTypes, listGot.ExcludeTypes) { + t.Errorf("Count ExcludeTypes = %v, List ExcludeTypes = %v, want equal", countGot.ExcludeTypes, listGot.ExcludeTypes) + } +} + +// TestBuildCountFilterExcludeStatusTakesNamesAsWritten pins ExcludeStatus: a +// Count-only field with no List counterpart (ListRequest computes its default +// exclusions internally and exposes no caller-facing knob for them). Entries +// split and trim like ExcludeTypes. UNLIKE Status and ExcludeTypes, each name +// IS validated against the workspace vocabulary (review S8 follow-up #3): a +// built-in name passes as written, and an unrecognized one is ErrValidation +// rather than silently excluding nothing. +func TestBuildCountFilterExcludeStatusTakesNamesAsWritten(t *testing.T) { + got, err := BuildCountFilter(issueops.CountRequest{ + ExcludeStatus: []string{" closed , pinned", ""}, + }, ListConfig{}) + if err != nil { + t.Fatalf("BuildCountFilter: %v", err) + } + want := []types.Status{"closed", "pinned"} + if !reflect.DeepEqual(got.ExcludeStatus, want) { + t.Errorf("ExcludeStatus = %v, want %v", got.ExcludeStatus, want) + } +} + +// TestBuildCountFilterExcludeStatusRejectsAnUnknownName pins the ErrValidation +// refusal (review S8 follow-up #3): a typo'd status name in --exclude-status +// must fail loudly rather than silently excluding nothing and overcounting. +func TestBuildCountFilterExcludeStatusRejectsAnUnknownName(t *testing.T) { + _, err := BuildCountFilter(issueops.CountRequest{ + ExcludeStatus: []string{"no-such-status"}, + }, ListConfig{}) + if err == nil { + t.Fatal("BuildCountFilter accepted an unknown exclude-status name, want ErrValidation") + } + if !errors.Is(err, issueops.ErrValidation) { + t.Errorf("err = %v, want errors.Is(err, issueops.ErrValidation)", err) + } +} + +// TestBuildCountFilterExcludeStatusAllowsAWorkspaceCustomStatus pins the other +// half of #3: a workspace-defined custom status is NOT a typo and must be +// accepted, following ApplyStatusFilter's own IsValidWithCustom precedent in +// list.go. +func TestBuildCountFilterExcludeStatusAllowsAWorkspaceCustomStatus(t *testing.T) { + cfg := ListConfig{CustomStatuses: []types.CustomStatus{{Name: "triaged"}}} + got, err := BuildCountFilter(issueops.CountRequest{ + ExcludeStatus: []string{"triaged"}, + }, cfg) + if err != nil { + t.Fatalf("BuildCountFilter rejected the workspace's own custom status: %v", err) + } + want := []types.Status{"triaged"} + if !reflect.DeepEqual(got.ExcludeStatus, want) { + t.Errorf("ExcludeStatus = %v, want %v", got.ExcludeStatus, want) + } +} + +// TestBuildCountFilterRefusesParentAndNoParentTogether pins review S8 +// follow-up #1: the role refuses the combination as ErrValidation, with the +// same wording `bd list`'s CLI already uses for the same combination +// (cmd/bd/list_input.go), so a caller reaching this role from any front door +// gets the identical refusal. +func TestBuildCountFilterRefusesParentAndNoParentTogether(t *testing.T) { + _, err := BuildCountFilter(issueops.CountRequest{ParentID: "bd-1", NoParent: true}, ListConfig{}) + if err == nil { + t.Fatal("BuildCountFilter accepted --parent with --no-parent, want ErrValidation") + } + if !errors.Is(err, issueops.ErrValidation) { + t.Errorf("err = %v, want errors.Is(err, issueops.ErrValidation)", err) + } + const want = "--parent and --no-parent are mutually exclusive" + if err.Error() != want { + t.Errorf("err = %q, want %q", err.Error(), want) + } +} + +// TestBuildCountFilterStatusAllIsNotValidation pins DESIGN's requirement that +// Status "all" have well-defined semantics: it is the no-filter spelling +// (same as empty), NOT ErrValidation, because BuildListFilter's own "all" +// selector means the same thing and a count must stay able to answer the +// cardinality of `bd list --status all --all` for the shared predicate +// (GH#4387 count/list parity). +func TestBuildCountFilterStatusAllIsNotValidation(t *testing.T) { + got, err := BuildCountFilter(issueops.CountRequest{Status: "all"}, ListConfig{}) + if err != nil { + t.Fatalf(`BuildCountFilter(status="all") returned an error, want nil: %v`, err) + } + if got.Status != nil { + t.Errorf(`status "all" produced Status = %v, want no status predicate`, countPtrStr(got.Status)) + } +} + func TestBuildCountFilterCarriesMetadataFields(t *testing.T) { fields := map[string]string{"team": "platform", "env": "prod"} got, err := BuildCountFilter(issueops.CountRequest{MetadataFields: fields}, ListConfig{}) diff --git a/internal/workapi/storecounter/counter.go b/internal/workapi/storecounter/counter.go index f873d5900..0c9fa1b29 100644 --- a/internal/workapi/storecounter/counter.go +++ b/internal/workapi/storecounter/counter.go @@ -75,15 +75,18 @@ func (c *storeCounter) CountByGroup(ctx context.Context, req issueops.CountByGro // filter builds the storage filter, loading the workspace configuration only // when the request can read it. // -// TWO THINGS ARE DELIBERATE. The load is SKIPPED unless IncludeInfra is set, -// because that flag is the only thing in a count request the configuration -// reaches. And when it does run it runs PER CALL rather than once at -// construction: the infra vocabulary is workspace state a caller can change -// between two counts, and a counter that cached it would answer for the older -// workspace. +// TWO THINGS ARE DELIBERATE. The load is SKIPPED unless IncludeInfra is set OR +// ExcludeStatus is non-empty — those are the two things in a count request the +// configuration reaches: IncludeInfra needs the infra vocabulary, and +// ExcludeStatus needs the workspace's custom status names so +// BuildCountFilter's validation does not refuse a status the workspace itself +// defines (PR #7199 review). And when it does run it runs PER CALL rather +// than once at construction: the infra vocabulary and custom statuses are +// workspace state a caller can change between two counts, and a counter that +// cached it would answer for the older workspace. func (c *storeCounter) filter(ctx context.Context, req issueops.CountRequest) (types.IssueFilter, error) { var cfg workapi.ListConfig - if req.IncludeInfra { + if req.IncludeInfra || len(req.ExcludeStatus) > 0 { loaded, err := workapi.LoadStoreListConfig(ctx, c.store) if err != nil { return types.IssueFilter{}, err diff --git a/issueops/counter.go b/issueops/counter.go index 594ec4add..1f97939f4 100644 --- a/issueops/counter.go +++ b/issueops/counter.go @@ -54,6 +54,17 @@ type CountRequest struct { // than quietly tightened, because a scripted caller that counts a status // its workspace has since dropped currently reads 0 and would start reading // an error. + // + // "all" is this field's one well-defined non-literal spelling, and it is + // NOT ErrValidation: it is equivalent to leaving Status empty, and + // BuildListFilter treats its own "all" selector identically (the no-filter + // spelling), so `bd count --status all ` and `bd list --status + // all --all` already agree on cardinality for the shared part + // of the predicate. Rejecting it here would break that count/list parity + // (the whole point of this role, GH#4387) for every caller already + // spelling it, and there is no caller-visible ambiguity it would resolve: + // "no status" is a filter that excludes every row, which "all" has never + // meant on either front door. Status string // IssueType restricts the type, with the same match-nothing-rather-than-fail // treatment Status gets and no alias expansion at all. @@ -66,6 +77,24 @@ type CountRequest struct { // written and answer with the empty intersection. Assignee string + // ParentID restricts to one issue's children, spelled as + // ListRequest.ParentID spells it: a parent-child dependency edge, OR (for + // an issue with no such edge) a dotted-id prefix match. NoParent restricts + // to rows with no parent-child dependency edge. It does not consult the + // dotted-id convention, so an edge-less dotted-id child (X.1) still counts + // as top-level — it is counted by BOTH ParentID X and NoParent, as it is + // listed by both `bd list --parent X --flat` and `bd list --no-parent`. + // + // Setting both IS refused, as ErrValidation, UNLIKE Assignee/NoAssignee + // above: `bd list`'s CLI already refuses `--parent` with `--no-parent` + // (cmd/bd/list_input.go) with this same wording, and a caller reaching + // this role directly — over HTTP, or through a future client that skips + // the primary CLI's flag parser — gets the identical refusal rather than + // a silent empty-intersection answer the primary CLI would never have let + // it ask for. + ParentID string + NoParent bool + // Priority is an exact priority; PriorityMin and PriorityMax bound a range // inclusively. All three are pointers because 0 is a real priority, for the // reason ReadyRequest.Priority gives. @@ -86,6 +115,25 @@ type CountRequest struct { // rather than a slice it had to prepare. IDFilter string + // ExcludeTypes names types to exclude, spelled as ListRequest.ExcludeTypes + // spells it: entries may be comma-separated, splitting and normalization + // happen inside, and an entry is NOT validated against the workspace + // vocabulary — an unrecognized type excludes nothing rather than failing. + // It composes with IncludeInfra's own "gate" exclusion rather than + // replacing it: both contribute to the same exclusion set. + ExcludeTypes []string + // ExcludeStatus names statuses to exclude. Unlike Status, this has no + // List counterpart — ListRequest computes its default status exclusions + // internally from workspace configuration and exposes no caller-facing + // knob for them. Here it is a new, Count-only capability (behavior token + // `issues.count.scope`): entries may be comma-separated, and — UNLIKE + // Status and ExcludeTypes above — each name IS validated against the + // workspace vocabulary (built-in statuses plus the workspace's own custom + // ones). An unrecognized name is ErrValidation rather than excluding + // nothing: a caller building an exclusion list by hand gets a loud failure + // on a typo instead of a silently wider, overcounted answer. + ExcludeStatus []string + // TitleContains, DescContains and NotesContains are substring matches on // the three long fields, spelled as ListRequest spells them so the two // requests cannot drift apart in naming while meaning the same thing. diff --git a/issueops/reader.go b/issueops/reader.go index 0a0219f52..3a7d3b0ce 100644 --- a/issueops/reader.go +++ b/issueops/reader.go @@ -54,7 +54,15 @@ type ReadyRequest struct { // the other, and P0 has already been lost that way once. Priority *int - // ParentID restricts to recursive descendants of one issue. + // ParentID restricts to ALL TRANSITIVE DESCENDANTS of one issue, not just + // its direct children: internal/storage/issueops/ready_work.go calls + // GetDescendantIDsInTx(ctx, tx, *filter.ParentID, 0), the recursive walk. + // GH#3396: this used to be a one-hop subquery against the dependency + // table, so grandchildren were silently dropped despite this comment and + // the help text both promising "descendants (recursive)" — do not + // mistake this for ListRequest.ParentID's non-walking clause, which is a + // DIFFERENT query (PR #7199 review: an earlier pass here wrongly copied + // that one-level wording onto this field). ParentID string // MolType restricts to one molecule type. MolType *MolType @@ -339,6 +347,29 @@ type ListRequest struct { // ExcludeTypes entries may be comma-separated; splitting happens inside. ExcludeTypes []string + // ParentID restricts to one issue's children through one SQL clause, the + // same one CountRequest.ParentID compiles to + // (internal/storage/sqlbuild/filter.go): a parent-child dependency edge + // onto ParentID, OR, for a row with no parent-child edge at all, an id + // under ParentID's dotted prefix ("X.1", "X.1.1"). NEITHER ARM WALKS. The + // edge arm stops at one level, so a grandchild with an edge of its own is + // not in the answer, while the prefix arm reaches every edge-less dotted + // id at any depth (backend/conformance/reader_contract.go pins that arm). + // It is NOT the recursive descendants walk — that is ReadyRequest.ParentID + // above, a DIFFERENT field on a different request, backed by + // GetDescendantIDsInTx (GH#3396); on the ReadyFlag arm this field crosses + // to that query and resolves recursively there. + // The recursive tree that `bd list --parent X` renders by DEFAULT comes + // from cmd/bd/list_show_filter_modes.go's findAllDescendants re-running + // this clause against the store once per level and accumulating the + // results client-side, not from this field answering a deeper question. + // --flat, --json and --format skip that walk and return exactly this + // field's answer, which is why the flat listing is the invocation that + // agrees with `bd count --parent X`'s cardinality. + // + // NoParent restricts to rows with no parent-child dependency edge. It has + // no dotted-id arm, so an edge-less "X.1" matches BOTH ParentID X and + // NoParent. The ReadyFlag arm refuses it; see that field. ParentID string NoParent bool MolType *MolType