From 4450fe99bb71524f8e6f1d00d8567b0c4303b0ef Mon Sep 17 00:00:00 2001 From: Julian Knutsen Date: Sat, 17 Oct 2026 12:29:13 +0000 Subject: [PATCH] feat(http): bd blocked over bd serve (blocked.list), plus bd blocked --flags [--raw] (#7649) * feat(http): bd blocked over bd serve (blocked.list) through issueops.BlockedLister `bd blocked` refused against an HTTP workspace: it called the legacy GetBlockedIssues, which the HTTP backend stubs as unsupported. It now runs one role on every route, and bd serve publishes that role. - Role: issueops.BlockedLister.ListBlocked(BlockedRequest{ParentID, Labels, LabelsAny, ExcludeLabels}) -> BlockedResult{Items}. One body, internal/storage/issueops.ExecuteListBlockedInTx over the unchanged GetBlockedIssuesInTx; labels normalized in the role (BlockedFilter); Items never nil and in a total order (priority, created_at desc, id). - Legs: DoltStore (read tx), EmbeddedDoltStore (read conn), uow (read unit of work through the use case's GetBlockedIssues, so the external-dependency uow decorator applies). Census: storage.DoltStorage gains BlockedLister(); HookFiringStore passes it through (a read), telemetry wraps it, uow.notifyingProvider, externaldeps.uowProvider and httpapi.timedProvider build it. - externaldeps.Store.BlockedLister is built over its own GetBlockedIssues, so local `bd blocked` output is unchanged. Over a remote store it takes the server's listing when the server advertises policy.external_dependencies, else decorates the server's listing here through EdgeReader and BatchGetter (decorateBlocked, factored out of GetBlockedIssues). - Wire: GET /v0/beads/blocked (listBlockedIssues; parent, label, label_any, exclude_label) answering BlockedPage {items, has_more} with the new x-go-type-pinned BlockedIssue schema, behind the new blocked.list token. apigen and the wire-shape golden regenerated; the golden diff is additive only, so wire_revision stays 2. Config.BlockedLister is a required role, wired in bd serve. - Client: httpclient.Store.BlockedLister dials through Store.dispatch, so an older server is refused BEFORE the dial with *wire.CapabilityError naming blocked.list. The legacy Store.GetBlockedIssues rides the role (page and row cap applied to its answer) and leaves the unsupported shell. Divergence ledger row F-blocked retired. - CLI: `bd blocked` takes BlockedLister() from the store on the direct routes and from the provider's BlockedListerSource on the proxied route; runBlockedProxiedServer is deleted. --json output is the same rows in the same order, [] when empty. Tests: backend/conformance RunBlockedLister (10 cases: direct blocker, child attributed to its blocked parent, closed blocker, wisp plane, pinned status, parent filter, label filters with normalization, order, empty slice, request not mutated) on dolt, embedded, uow and the served HTTP client; httpapi handler tests (mapping, unknown/repeated params, projection, capability); httpclient stub tests (pre-dial refusal naming the token, encoding, projection, legacy door paging); skew and vocabulary counts; TestE2E_BlockedOverHTTP (served `bd blocked --json` equals the local command's after mapping ids and timestamps, member order included, external blocker named, older-server refusal with no dial); corpus gc-blocked re-marked and gc-blocked-parent added, both pending until the reference server advertises blocked.list. Co-Authored-By: Claude Opus 5.5 (1M context) * fix(blocked): pin server-side external decoration, refuse has_more, serve the legacy door over http Review fixes for the blocked.list slice. - Server-side external decoration is now pinned on the compositions bd serve answers from. TestWrapUOWProviderBlockedListerNamesExternalBlockers lists through the wrapped unit-of-work provider's own BlockedLister accessor (the bd serve provider path) and requires an external-only row and a mixed row to carry their external: refs; building that lister over the inner provider fails it. TestServedBlockedListerCarriesExternalRefs- WhenTheServerEnforces serves the store-shaped composition (externaldeps.Store.BlockedLister, policy advertised) and requires the operation's own answer, through the undecorated client role, to carry the refs, and the client's decorated chain to return it unchanged; serving the inner store's lister fails it. - The client refuses an answer reporting has_more with the new typed httpclient.TruncatedListingError instead of returning a short listing: the operation has no cursor, and a caller reads a missing row as unblocked. The legacy door inherits it. - externaldeps.Store.GetBlockedIssues over an http store reads the extra sources through the served roles (EdgeReader, BatchGetter) instead of the raw GetIssuesByIDs / GetDependencyRecordsForIssues the http backend refuses, and the decoration skips the source read when there is nothing to load. The legacy door on the CLI chain (bd mol stale, the wisp commands) now answers over http in both policy modes; TestServedLegacyGetBlockedIssuesThroughTheDecorator pins it, paging included. bd ready --explain still refuses on its other raw reads; the CHANGELOG says so. - Docs: the spec and the retired F-blocked ledger row state that the listing is unpaged, bounded by the client's 32 MiB response limit (refused, never read in part), and that has_more is refused. - Contract: new cases for waits-for and conditional-blocks blockers, no type exclusions (gate, epic), and a stale flag with no nameable blocker (on the legs that can write the column: dolt, embedded; skipped elsewhere with the reason). Order seeds explicit created_at values with five rows tied on priority and created_at, two of them wisps, so only the id tiebreak orders them; dropping the tiebreak now fails every run. Co-Authored-By: Claude Opus 5.5 (1M context) * feat(blocked): bd blocked --flags, the blocked flag per id from stdin `bd blocked --flags [--json]` reads ids from stdin, one per line (blank lines and repeats ignored), and prints the blocked flag bd holds for each: with --json, [{"id","is_blocked"}] in the order the ids were first given, omitting an id that names no row on either plane, [] for no input. It is the readiness column a consumer otherwise had to read with `bd sql`, so no such consumer needs a local-only command. It is the FLAG, not the listing: `bd blocked` narrows the stored flag to rows it can name a blocker for, and --flags does not, so a row whose flag is set with no nameable blocker reads true here and is absent from the listing. Where the external-dependency policy applies, an unsatisfied external: blocker sets the flag, as it keeps the row out of `bd ready`. One library entry on every route: issueops.BlockedFlags over the route's BlockingAnnotator (the store's accessor on dolt, embedded and HTTP, the provider's on the proxied route). Over HTTP that is listBlockingAnnotations with is_blocked behind dependencies.blocking.isBlocked; a server without that token is refused before the dial, naming it. No wire change. The listing's filter flags are refused with --flags rather than ignored. Tests: TestE2E_BlockedFlagsOverHTTP (served JSON equals the local command's after mapping ids to titles, unknown id omitted, repeat collapsed, [] on empty stdin, filters refused; a stale-flagged row with no blocker reads true and is absent from `bd blocked --json`, which fails if --flags is derived from the listing; an older server's handshake refused naming the token with no dial); TestProxiedServerBlocked/flags_reads_ids_from_stdin on the proxied route; corpus gc-blocked-flags, pending until the reference server advertises the token. CHANGELOG entry. Co-Authored-By: Claude Opus 5.5 (1M context) * feat(blocked): bd blocked --flags --raw, the stored is_blocked column `bd blocked --flags --raw [--json]` answers the stored is_blocked column for each id read from stdin, exactly what reading is_blocked from the issues and wisps tables returns, without the external-dependency policy: a row blocked only by an unsatisfied external: ref reads false (the policy-aware --flags reads true), and a stale flag reads true either way. Same output shape as --flags. This lets a consumer move off a `bd sql` read of the column with no change in what it reads. The default --flags is unchanged. The role is taken from beneath every decorator: storage.UnwrapStore on the direct routes, uow.UnwrapProvider on the proxied route. Over HTTP the innermost store is the http client, whose annotation read is the server's answer: against a server that does not advertise policy.external_dependencies that answer is the column, and --raw is served. A server that advertises it decorates the flag it answers, the v0 wire has no member asking it for the column, and the decorated flag cannot be turned back into the column client-side, so --raw against such a server is refused before the annotation read, naming the token, rather than answered with the decorated flag. --raw without --flags is refused. Tests: TestE2E_BlockedFlagsOverHTTP/raw_is_the_stored_column (served --raw equals local --raw; the externally-blocked-only row reads true with --flags and false with --raw on both; locally blocked rows agree; a stale-flagged row reads true in both modes; --raw without --flags exits 1; a server advertising the policy refuses --raw with no annotation request); TestProxiedServerBlocked/flags_raw_is_the_stored_column; corpus gc-blocked-flags-raw (pending until the reference server advertises dependencies.blocking.isBlocked). Routing --raw through the decorated annotator fails the e2e. CHANGELOG entry. Co-Authored-By: Claude Opus 5.5 (1M context) * docs(changelog): bd blocked --flags --raw is for the local and proxied routes Co-Authored-By: Claude Opus 5.5 (1M context) * test(blocked): give the e2e gate-kid its own priority; document --flags answers closed ids TestE2E_BlockedOverHTTP seeded gate-parent and gate-kid at one priority, so when both landed in the same second their order fell to the generated ids, which differ between the local and the served workspace: the comparison failed intermittently. gate-kid now has its own priority. Ten runs on the embedded lane pass. bd blocked --flags answers a closed id too, with its stored flag; a SQL read of the column filtered on status <> 'closed' leaves it out. Said in the command's doc and the CHANGELOG. Co-Authored-By: Claude Opus 5.5 (1M context) * fix(blocked): apply label filters to external-only blocked rows The external-dependency policy appends a row blocked only by an unsatisfied external: ref after the label query has run, so --label, --label-any and --exclude-label never judged it: bd blocked and GET /v0/beads/blocked listed it whatever its labels, on every route. Both decorators now filter those rows by the request's labels. The uow decorator reads them through GetMany, chunked at MaxGetManyIDs, so their labels are hydrated; GetIssuesByIDs left them empty, which also printed them unlabelled on the proxied route. Review fixes (adopt-pr apply-fixes, iteration 1): - major #1: label filters on external-only rows in the store and uow decorators, with absence-asserting arms in the unit, served httpclient (server- and client-enforced policy), e2e and proxied tiers; the spec and the role doc say such rows are narrowed like every other row. - minor #2: the policy.external_dependencies token prose names the blocked listing (spec, routes.go, regenerated types.gen.go). - minor #6: SERVE_RUNBOOK and the allowUnlimited comment record the blocked listing's unbounded whole-set answer and why. - minor #10: BlockedLister telemetry-outermost and typed-unsupported pins beside the ReadyCounter pair, plus the externaldeps wrap pin. - nit #9: F-blocked names --flags --raw's refusal against a server that applies the policy (ledger.go and the regenerated ledger doc). - nit #11: the remote BlockedLister no longer builds a local lister it then discards. Review fixes (adopt-pr apply-fixes, iteration 2; the review approved at 940 with one minor and two nits, none gating): - minor (the --flags refusal list was unpinned): the new TestBlockedFlagsRefusesEveryListingFilter pins blockedFlagFilterFlags to the flags blockedCmd registers, in both directions. Every registered flag except --flags and --raw must be refused, and every entry must name a registered flag. The test walks Flags() rather than LocalFlags(), so it does not merge the root's persistent flags into blockedCmd, and it skips inherited root flags and cobra's --help. Mutants: - four fail it: a new filter flag left off the list, a dropped entry, a misspelled entry, and --raw added to the list; - a simulated earlier in-process run (merged root flags plus --help) stays green, and fails once either skip is removed. - nit (the remote branch's span): the remote BlockedLister comment now says why that lister is not wrapped again. The inner lister already carries telemetry's span, so wrapping would nest a second span of the same name. The comment also says what runs outside that span. - nit (duplicated order comparator): left as is. finishBlockedIssues predates this PR, so merging the two sorts would refactor existing code with no review pass left after this one. Co-Authored-By: Claude Opus 5.5 Agent-Signature: claude-code-claude-opus-5-5-max on behalf of Gas City Adopt-PR --------- Co-authored-by: Claude Opus 5.5 (1M context) Co-authored-by: Eddie the Engineer <338826946+gascity-eddie[bot]@users.noreply.github.com> --- .../scripts/embedded-storage-test-shards.txt | 1 + CHANGELOG.md | 56 +++ backend/conformance/BUILD.bazel | 1 + .../conformance/blocked_lister_contract.go | 426 ++++++++++++++++++ backend/http/BUILD.bazel | 2 + backend/http/corpus/harness_test.go | 2 + backend/http/corpus/testdata/corpus.yaml | 18 +- backend/http/e2e_blocked_flags_test.go | 287 ++++++++++++ backend/http/e2e_blocked_test.go | 229 ++++++++++ backend/http/e2e_cli_test.go | 2 + cmd/bd/BUILD.bazel | 2 + cmd/bd/blocked_embedded_test.go | 2 +- cmd/bd/blocked_flags.go | 167 +++++++ cmd/bd/blocked_flags_test.go | 57 +++ cmd/bd/blocked_proxied_integration_test.go | 102 +++++ cmd/bd/ready.go | 78 ++-- cmd/bd/ready_proxied_server.go | 49 +- cmd/bd/serve.go | 4 + cmd/bd/serve_source_test.go | 1 + cmd/bd/serve_store_identity_test.go | 6 + cmd/bd/stale.go | 2 +- engdocs/SERVE_RUNBOOK.md | 8 + engdocs/design/http-divergence-ledger.md | 4 +- internal/httpapi/BUILD.bazel | 2 + internal/httpapi/apigen/types.gen.go | 31 +- internal/httpapi/blocked.go | 72 +++ internal/httpapi/blocked_test.go | 150 ++++++ internal/httpapi/claim.go | 7 + internal/httpapi/pinning.go | 4 + internal/httpapi/problem.go | 7 + internal/httpapi/reads.go | 5 + internal/httpapi/roles_test.go | 29 ++ internal/httpapi/routes.go | 18 +- internal/httpapi/server.go | 24 +- internal/httpapi/spec/openapi.v0.yaml | 226 +++++++++- internal/httpapi/wire_bijection_test.go | 3 +- .../httpapi/wireshape/testdata/golden.json | 418 +++++++++++++++++ internal/httpclient/BUILD.bazel | 4 + internal/httpclient/accessors.go | 8 + internal/httpclient/blockedlister.go | 130 ++++++ internal/httpclient/blockedlister_test.go | 230 ++++++++++ internal/httpclient/completeness_test.go | 1 - internal/httpclient/encode/ledger.go | 12 +- internal/httpclient/encode/oracle_test.go | 2 + .../served_blocked_external_test.go | 283 ++++++++++++ .../httpclient/served_blocked_lister_test.go | 24 + internal/httpclient/served_harness_test.go | 2 + internal/httpclient/served_test.go | 3 + internal/httpclient/unsupported_gen.go | 7 +- internal/httpclient/wire/handshake.go | 5 + internal/httpclient/wire/paths.go | 2 + internal/httpclient/wire/skew_test.go | 11 +- internal/httpclient/wire/vocabulary_test.go | 1 + internal/storage/BUILD.bazel | 1 + internal/storage/dolt/BUILD.bazel | 2 + internal/storage/dolt/blocked_lister.go | 38 ++ .../dolt/blocked_lister_contract_test.go | 40 ++ internal/storage/embeddeddolt/BUILD.bazel | 2 + .../storage/embeddeddolt/blocked_lister.go | 40 ++ .../blocked_lister_contract_test.go | 46 ++ internal/storage/externaldeps/BUILD.bazel | 2 + .../storage/externaldeps/blocked_lister.go | 114 +++++ .../externaldeps/blocked_lister_test.go | 176 ++++++++ internal/storage/externaldeps/decorator.go | 67 ++- .../storage/externaldeps/uow_decorator.go | 22 +- .../externaldeps/uow_decorator_test.go | 33 +- internal/storage/hook_blocked_lister.go | 14 + internal/storage/issueops/BUILD.bazel | 2 + internal/storage/issueops/blocked_role.go | 114 +++++ .../storage/role_accessor_decorator_test.go | 18 +- internal/storage/storage.go | 6 + internal/storage/uow/BUILD.bazel | 2 + internal/storage/uow/blocked_lister.go | 50 ++ .../uow/blocked_lister_contract_test.go | 31 ++ internal/storage/uow/notifying.go | 7 + internal/telemetry/BUILD.bazel | 1 + internal/telemetry/blocked_lister.go | 38 ++ .../telemetry/role_accessor_decorator_test.go | 42 +- issue_roles_external_test.go | 30 ++ issueops/BUILD.bazel | 1 + issueops/blockedlister.go | 64 +++ 81 files changed, 4084 insertions(+), 146 deletions(-) create mode 100644 backend/conformance/blocked_lister_contract.go create mode 100644 backend/http/e2e_blocked_flags_test.go create mode 100644 backend/http/e2e_blocked_test.go create mode 100644 cmd/bd/blocked_flags.go create mode 100644 cmd/bd/blocked_flags_test.go create mode 100644 internal/httpapi/blocked.go create mode 100644 internal/httpapi/blocked_test.go create mode 100644 internal/httpclient/blockedlister.go create mode 100644 internal/httpclient/blockedlister_test.go create mode 100644 internal/httpclient/served_blocked_external_test.go create mode 100644 internal/httpclient/served_blocked_lister_test.go create mode 100644 internal/storage/dolt/blocked_lister.go create mode 100644 internal/storage/dolt/blocked_lister_contract_test.go create mode 100644 internal/storage/embeddeddolt/blocked_lister.go create mode 100644 internal/storage/embeddeddolt/blocked_lister_contract_test.go create mode 100644 internal/storage/externaldeps/blocked_lister.go create mode 100644 internal/storage/externaldeps/blocked_lister_test.go create mode 100644 internal/storage/hook_blocked_lister.go create mode 100644 internal/storage/issueops/blocked_role.go create mode 100644 internal/storage/uow/blocked_lister.go create mode 100644 internal/storage/uow/blocked_lister_contract_test.go create mode 100644 internal/telemetry/blocked_lister.go create mode 100644 issueops/blockedlister.go diff --git a/.github/scripts/embedded-storage-test-shards.txt b/.github/scripts/embedded-storage-test-shards.txt index c48bdeb79..8cc9f773e 100644 --- a/.github/scripts/embedded-storage-test-shards.txt +++ b/.github/scripts/embedded-storage-test-shards.txt @@ -176,6 +176,7 @@ 40 3 TestTransactionCleanupBeforeSQLCommitRemainsDefinite 40 3 TestUnknownProvenanceLeaseStaysReclaimable +40 4 TestBlockedListerContract 40 4 TestCreateBatchFastPathsMatchPerRowSmallDepAdd_Embedded 40 4 TestEmbeddedIssueOperationsUpdateLabelPatchOrdering 40 4 TestEmbeddedReaderGetMissIsNotFoundAndBackendFailureDoesNotDecay diff --git a/CHANGELOG.md b/CHANGELOG.md index 99789418d..c9bb95944 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,56 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added +- `bd blocked --flags` prints the blocked flag for each id read from stdin + (one per line; blank lines and repeats ignored), with `--json` as + `[{"id","is_blocked"}]` in the order the ids were given, omitting an id that + names no row on either plane. A closed id is answered too, with its stored + flag (a `status <> 'closed'` SQL read of the column leaves it out). It is the flag `bd ready` filters on, not the + listing: a row whose stored flag is set with no nameable blocker reads true + here and is absent from `bd blocked`. It runs `issueops.BlockedFlags` on + every route; over HTTP that is `listBlockingAnnotations` with `is_blocked`, + behind `dependencies.blocking.isBlocked`, refused before the dial against a + server that does not advertise it. The listing's filter flags are refused + with `--flags`. +- `bd blocked --flags --raw` answers the stored `is_blocked` column itself, + without the external-dependency policy: a row blocked only by an + unsatisfied `external:` ref reads `false` (the policy-aware `--flags` + reads `true`); a stale flag reads `true` either way. `--raw` is for the + local and proxied routes, where it reads the column directly and the two + answer the same. Over HTTP it is answered only by a server that does not + advertise `policy.external_dependencies` (whose flag is the column); a + server advertising it answers only the policy-aware flag, so `--raw` + against it is refused before the dial rather than answered with the + decorated flag. `--raw` without `--flags` is refused. +- `bd blocked` works over a connected HTTP workspace. The blocked listing is + a role, `issueops.BlockedLister` (`ListBlocked(BlockedRequest{ParentID, + Labels, LabelsAny, ExcludeLabels}) -> BlockedResult{Items}`), on every + backend leg over the same body `GetBlockedIssues` runs, and `bd serve` + publishes it as `GET /v0/beads/blocked` (`parent`, and the repeatable + `label`, `label_any` and `exclude_label`), answering `{items, has_more}` + with `BlockedIssue` rows (the element `bd blocked --json` prints), behind + the new `blocked.list` capability token (additive; no wire_revision bump). + The listing names unsatisfied `external:` blockers wherever the + external-dependency policy applies: on the server when it advertises + `policy.external_dependencies`, else on the client, as every other policy + read does. The HTTP backend's `BlockedLister()` and legacy + `GetBlockedIssues` dial it; against a server that does not advertise the + token they refuse before dialing with a typed capability error naming + `blocked.list`, and never compose the listing from other reads. The + listing is unpaged: a server answer that reports `has_more` is refused + with the new `httpclient.TruncatedListingError` rather than returned + short, and an answer over the client's 32 MiB response limit is refused + as any oversized response is. The external-dependency decorator's legacy + `GetBlockedIssues` over an HTTP store now reads the extra sources through + the served roles (it failed on a raw multi-id read before), so callers of + that door no longer fail on it; commands that also make other raw reads + the HTTP backend does not serve (`bd ready --explain`) still refuse on + those. `bd + blocked` runs the role on every route (the proxied-server route through + the provider's accessor) with unchanged output: `--json` prints the same + rows in the same order (priority, then newest first, then id, which now + also breaks a tie the order used to leave to chance), and `[]`, never + `null`, when nothing is blocked. - `bd list`'s remaining filters work over a connected HTTP workspace. `GET /v0/beads/issues` accepts `updated_after`/`updated_before`, `closed_after`/`closed_before`, `defer_after`/`defer_before`, @@ -678,6 +728,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- **`bd blocked --label`, `--label-any` and `--exclude-label` apply to a row + blocked only by an unsatisfied `external:` dependency.** The + external-dependency policy lists such a row itself, after the label query + has run, and it was listed whatever its labels; it is now narrowed by the + label filters like every other row, on every route. On the proxied-server + route such a row also printed with no labels; it now prints them. - **`bd gate add-waiter` survives contention the store gave up replaying.** It retried only a version miss, so a convoy adder failed when its guarded write ended in a serialization failure or deadlock the backend stopped diff --git a/backend/conformance/BUILD.bazel b/backend/conformance/BUILD.bazel index d491cd407..abf0ea1ed 100644 --- a/backend/conformance/BUILD.bazel +++ b/backend/conformance/BUILD.bazel @@ -15,6 +15,7 @@ go_library( "batch_closer_contract.go", "batch_creator_contract.go", "batch_getter_contract.go", + "blocked_lister_contract.go", "blocked_state.go", "blocking_annotator_contract.go", "bootstrapper_contract.go", diff --git a/backend/conformance/blocked_lister_contract.go b/backend/conformance/blocked_lister_contract.go new file mode 100644 index 000000000..9313e3e76 --- /dev/null +++ b/backend/conformance/blocked_lister_contract.go @@ -0,0 +1,426 @@ +package conformance + +import ( + "context" + "fmt" + "reflect" + "slices" + "testing" + "time" + + "github.com/steveyegge/beads/internal/types" + publicops "github.com/steveyegge/beads/issueops" +) + +// This file holds the semantic contract every implementation of +// publicops.BlockedLister must satisfy: what issueops/blockedlister.go +// promises, asserted on every leg (dolt, embedded dolt, unit of work, and the +// served HTTP client) so `bd blocked` answers one listing whichever route it +// takes. +// +// EVERY CASE IS SCOPED BY A LABEL: the blocked set is a property of the whole +// database, not of an id prefix, so a row seeded by one case is inside the +// next one's answer unless each asks through Labels. + +// BlockedListerFixture supplies adapter-specific storage access for the +// blocked-listing assertions. +type BlockedListerFixture struct { + // IssuePrefix namespaces the ids each case seeds, so several cases can + // share one database. + IssuePrefix string + // BlockedLister is the surface under test. + BlockedLister publicops.BlockedLister + // CreateIssue seeds a durable issue in the issues plane. + CreateIssue func(context.Context, *types.Issue, string) error + // CreateWisp seeds an ephemeral issue in the wisps plane. + CreateWisp func(context.Context, *types.Issue, string) error + // AddDependency seeds ONE edge, recomputing the stored blocked flag the + // way every dependency write does. + AddDependency func(context.Context, *types.Dependency, string) error + // MarkBlocked sets the stored is_blocked flag on one durable issue + // directly, without the edge that would justify it: the stale flag `bd + // recompute-blocked` exists to repair. It must fail unless exactly one row + // changed. A nil hook means this leg cannot write the column, and the case + // that needs it skips with that reason. + MarkBlocked func(context.Context, string) error +} + +// RunBlockedLister runs every blocked-listing case as a subtest of t. +func RunBlockedLister(t *testing.T, ctx context.Context, fixture BlockedListerFixture) { + t.Helper() + t.Run("NamesADirectBlocker", func(t *testing.T) { runBlockedNamesADirectBlocker(t, ctx, fixture) }) + t.Run("AttributesAChildToItsBlockedParent", func(t *testing.T) { runBlockedAttributesAChildToItsParent(t, ctx, fixture) }) + t.Run("ClosedBlockerDoesNotBlock", func(t *testing.T) { runBlockedClosedBlockerDoesNotBlock(t, ctx, fixture) }) + t.Run("IncludesTheWispPlane", func(t *testing.T) { runBlockedIncludesTheWispPlane(t, ctx, fixture) }) + t.Run("ExcludesThePinnedStatus", func(t *testing.T) { runBlockedExcludesThePinnedStatus(t, ctx, fixture) }) + t.Run("ParentFilter", func(t *testing.T) { runBlockedParentFilter(t, ctx, fixture) }) + t.Run("LabelFilters", func(t *testing.T) { runBlockedLabelFilters(t, ctx, fixture) }) + t.Run("Order", func(t *testing.T) { runBlockedOrder(t, ctx, fixture) }) + t.Run("NamesWaitsForAndConditionalBlockers", func(t *testing.T) { runBlockedNamesWaitsForAndConditionalBlockers(t, ctx, fixture) }) + t.Run("OmitsAStaleFlagWithNoNameableBlocker", func(t *testing.T) { runBlockedOmitsAStaleFlag(t, ctx, fixture) }) + t.Run("HasNoTypeExclusions", func(t *testing.T) { runBlockedHasNoTypeExclusions(t, ctx, fixture) }) + t.Run("EmptyIsAnEmptySlice", func(t *testing.T) { runBlockedEmptyIsAnEmptySlice(t, ctx, fixture) }) + t.Run("DoesNotMutateTheCallerRequest", func(t *testing.T) { runBlockedDoesNotMutateTheRequest(t, ctx, fixture) }) +} + +// runBlockedNamesADirectBlocker: an issue with an open `blocks` target is +// listed with that target as its one blocker; the target itself is not. +func runBlockedNamesADirectBlocker(t *testing.T, ctx context.Context, f BlockedListerFixture) { + label := f.IssuePrefix + "-bl-direct" + blocked, blocker := blListerID(f, "direct-src"), blListerID(f, "direct-dst") + blListerSeedIssue(t, ctx, f, blListerIssue(blocked, 1, label)) + blListerSeedIssue(t, ctx, f, blListerIssue(blocker, 1, label)) + blListerSeedEdge(t, ctx, f, blocked, blocker, types.DepBlocks) + + items := blListerList(t, ctx, f, publicops.BlockedRequest{Labels: []string{label}}) + blListerAssertRows(t, items, map[string][]string{blocked: {blocker}}) +} + +// runBlockedAttributesAChildToItsParent: a child of a blocked parent, with no +// blocker of its own, is listed with its parent named as the blocker. +func runBlockedAttributesAChildToItsParent(t *testing.T, ctx context.Context, f BlockedListerFixture) { + label := f.IssuePrefix + "-bl-child" + parent, child, blocker := blListerID(f, "child-parent"), blListerID(f, "child-child"), blListerID(f, "child-blocker") + blListerSeedIssue(t, ctx, f, blListerIssue(parent, 1, label)) + blListerSeedIssue(t, ctx, f, blListerIssue(blocker, 1, label+"-other")) + blListerSeedIssue(t, ctx, f, blListerIssue(child, 1, label)) + blListerSeedEdge(t, ctx, f, child, parent, types.DepParentChild) + blListerSeedEdge(t, ctx, f, parent, blocker, types.DepBlocks) + + items := blListerList(t, ctx, f, publicops.BlockedRequest{Labels: []string{label}}) + blListerAssertRows(t, items, map[string][]string{parent: {blocker}, child: {parent}}) +} + +// runBlockedClosedBlockerDoesNotBlock: a `blocks` edge onto a closed target +// names no blocker, so the source is not listed. +func runBlockedClosedBlockerDoesNotBlock(t *testing.T, ctx context.Context, f BlockedListerFixture) { + label := f.IssuePrefix + "-bl-closed" + src, dst := blListerID(f, "closed-src"), blListerID(f, "closed-dst") + blListerSeedIssue(t, ctx, f, blListerIssue(src, 1, label)) + closed := blListerIssue(dst, 1, label) + closed.Status = types.StatusClosed + blListerSeedIssue(t, ctx, f, closed) + blListerSeedEdge(t, ctx, f, src, dst, types.DepBlocks) + + items := blListerList(t, ctx, f, publicops.BlockedRequest{Labels: []string{label}}) + blListerAssertRows(t, items, map[string][]string{}) +} + +// runBlockedIncludesTheWispPlane: a blocked wisp is listed beside the issues. +func runBlockedIncludesTheWispPlane(t *testing.T, ctx context.Context, f BlockedListerFixture) { + label := f.IssuePrefix + "-bl-wisp" + wisp, blocker := blListerID(f, "wisp-src"), blListerID(f, "wisp-dst") + w := blListerIssue(wisp, 1, label) + w.Ephemeral = true + if err := f.CreateWisp(ctx, w, "seed"); err != nil { + t.Fatalf("seed wisp %s: %v", wisp, err) + } + b := blListerIssue(blocker, 1, label) + b.Ephemeral = true + if err := f.CreateWisp(ctx, b, "seed"); err != nil { + t.Fatalf("seed wisp %s: %v", blocker, err) + } + blListerSeedEdge(t, ctx, f, wisp, blocker, types.DepBlocks) + + items := blListerList(t, ctx, f, publicops.BlockedRequest{Labels: []string{label}}) + blListerAssertRows(t, items, map[string][]string{wisp: {blocker}}) +} + +// runBlockedExcludesThePinnedStatus: a row whose status is pinned is never +// listed, whatever blocks it. +func runBlockedExcludesThePinnedStatus(t *testing.T, ctx context.Context, f BlockedListerFixture) { + label := f.IssuePrefix + "-bl-pinned" + pinned, open, blocker := blListerID(f, "pinned-src"), blListerID(f, "pinned-open"), blListerID(f, "pinned-dst") + p := blListerIssue(pinned, 1, label) + p.Status = types.StatusPinned + blListerSeedIssue(t, ctx, f, p) + blListerSeedIssue(t, ctx, f, blListerIssue(open, 1, label)) + blListerSeedIssue(t, ctx, f, blListerIssue(blocker, 1, label+"-other")) + blListerSeedEdge(t, ctx, f, pinned, blocker, types.DepBlocks) + blListerSeedEdge(t, ctx, f, open, blocker, types.DepBlocks) + + items := blListerList(t, ctx, f, publicops.BlockedRequest{Labels: []string{label}}) + blListerAssertRows(t, items, map[string][]string{open: {blocker}}) +} + +// runBlockedParentFilter: ParentID narrows to the parent's direct children. +func runBlockedParentFilter(t *testing.T, ctx context.Context, f BlockedListerFixture) { + label := f.IssuePrefix + "-bl-parent" + parent, kid, stranger, blocker := blListerID(f, "pf-parent"), blListerID(f, "pf-kid"), blListerID(f, "pf-stranger"), blListerID(f, "pf-blocker") + blListerSeedIssue(t, ctx, f, blListerIssue(parent, 1, label+"-other")) + blListerSeedIssue(t, ctx, f, blListerIssue(blocker, 1, label+"-other")) + blListerSeedIssue(t, ctx, f, blListerIssue(kid, 1, label)) + blListerSeedIssue(t, ctx, f, blListerIssue(stranger, 1, label)) + blListerSeedEdge(t, ctx, f, kid, parent, types.DepParentChild) + blListerSeedEdge(t, ctx, f, kid, blocker, types.DepBlocks) + blListerSeedEdge(t, ctx, f, stranger, blocker, types.DepBlocks) + + all := blListerList(t, ctx, f, publicops.BlockedRequest{Labels: []string{label}}) + blListerAssertRows(t, all, map[string][]string{kid: {blocker}, stranger: {blocker}}) + narrowed := blListerList(t, ctx, f, publicops.BlockedRequest{ParentID: parent, Labels: []string{label}}) + blListerAssertRows(t, narrowed, map[string][]string{kid: {blocker}}) +} + +// runBlockedLabelFilters: Labels is AND, LabelsAny is OR, ExcludeLabels +// removes, and every value is trimmed with blanks dropped. +func runBlockedLabelFilters(t *testing.T, ctx context.Context, f BlockedListerFixture) { + base := f.IssuePrefix + "-bl-lab" + red, blue := base+"-red", base+"-blue" + both, onlyRed, onlyBlue, blocker := blListerID(f, "lab-both"), blListerID(f, "lab-red"), blListerID(f, "lab-blue"), blListerID(f, "lab-blocker") + blListerSeedIssue(t, ctx, f, blListerIssue(both, 1, base, red, blue)) + blListerSeedIssue(t, ctx, f, blListerIssue(onlyRed, 1, base, red)) + blListerSeedIssue(t, ctx, f, blListerIssue(onlyBlue, 1, base, blue)) + blListerSeedIssue(t, ctx, f, blListerIssue(blocker, 1, base+"-other")) + for _, id := range []string{both, onlyRed, onlyBlue} { + blListerSeedEdge(t, ctx, f, id, blocker, types.DepBlocks) + } + want := func(ids ...string) map[string][]string { + m := map[string][]string{} + for _, id := range ids { + m[id] = []string{blocker} + } + return m + } + + blListerAssertRows(t, blListerList(t, ctx, f, publicops.BlockedRequest{Labels: []string{" " + red + " ", "", blue}}), want(both)) + blListerAssertRows(t, blListerList(t, ctx, f, publicops.BlockedRequest{Labels: []string{base}, LabelsAny: []string{red, blue}}), want(both, onlyRed, onlyBlue)) + blListerAssertRows(t, blListerList(t, ctx, f, publicops.BlockedRequest{Labels: []string{base}, ExcludeLabels: []string{" " + blue}}), want(onlyRed)) +} + +// runBlockedOrder: priority ascending, then created_at descending, then id. +// +// The created_at values are seeded explicitly, so every key decides at least +// one pair: two rows share a priority and differ in created_at (newest first), +// and five share both priority and created_at (the id breaks the tie). The +// expected order is written out rather than recomputed from the answer. +func runBlockedOrder(t *testing.T, ctx context.Context, f BlockedListerFixture) { + label := f.IssuePrefix + "-bl-order" + blocker := blListerID(f, "order-blocker") + blListerSeedIssue(t, ctx, f, blListerIssue(blocker, 1, label+"-other")) + older := time.Date(2026, 1, 1, 10, 0, 0, 0, time.UTC) + newer := older.Add(time.Hour) + // Two of the five tied rows are wisps. The store body assembles rows + // issues-plane first, each plane in id order, so the ids alone would put + // the tied run in the wrong order (b, d, e, then a, c); only the id + // tiebreak puts it right. + rows := []struct { + name string + priority int + created time.Time + wisp bool + }{ + {"order-p3", 3, newer, false}, + {"order-p0-older", 0, older, false}, + {"order-p0-newer-e", 0, newer, false}, + {"order-p0-newer-c", 0, newer, true}, + {"order-p0-newer-a", 0, newer, true}, + {"order-p0-newer-d", 0, newer, false}, + {"order-p0-newer-b", 0, newer, false}, + {"order-p1", 1, older, false}, + } + for _, row := range rows { + issue := blListerIssue(blListerID(f, row.name), row.priority, label) + issue.CreatedAt = row.created + issue.UpdatedAt = row.created + if row.wisp { + issue.Ephemeral = true + if err := f.CreateWisp(ctx, issue, "seed"); err != nil { + t.Fatalf("seed wisp %s: %v", issue.ID, err) + } + } else { + blListerSeedIssue(t, ctx, f, issue) + } + blListerSeedEdge(t, ctx, f, issue.ID, blocker, types.DepBlocks) + } + want := []string{ + blListerID(f, "order-p0-newer-a"), + blListerID(f, "order-p0-newer-b"), + blListerID(f, "order-p0-newer-c"), + blListerID(f, "order-p0-newer-d"), + blListerID(f, "order-p0-newer-e"), + blListerID(f, "order-p0-older"), + blListerID(f, "order-p1"), + blListerID(f, "order-p3"), + } + got := blListerIDs(blListerList(t, ctx, f, publicops.BlockedRequest{Labels: []string{label}})) + if !slices.Equal(got, want) { + t.Errorf("listing order = %v, want %v (priority ascending, created_at descending, id)", got, want) + } +} + +// runBlockedNamesWaitsForAndConditionalBlockers: a waits-for onto a spawner +// whose child is still open, and a conditional-blocks onto an open target, +// each name their target, as a blocks edge does. +func runBlockedNamesWaitsForAndConditionalBlockers(t *testing.T, ctx context.Context, f BlockedListerFixture) { + label := f.IssuePrefix + "-bl-wf" + spawner, spawned := blListerID(f, "wf-spawner"), blListerID(f, "wf-spawned") + waiter := blListerID(f, "wf-waiter") + target, conditional := blListerID(f, "cb-target"), blListerID(f, "cb-src") + blListerSeedIssue(t, ctx, f, blListerIssue(spawner, 1, label+"-other")) + blListerSeedIssue(t, ctx, f, blListerIssue(spawned, 1, label+"-other")) + blListerSeedIssue(t, ctx, f, blListerIssue(target, 1, label+"-other")) + blListerSeedIssue(t, ctx, f, blListerIssue(waiter, 1, label)) + blListerSeedIssue(t, ctx, f, blListerIssue(conditional, 1, label)) + blListerSeedEdge(t, ctx, f, spawned, spawner, types.DepParentChild) + blListerSeedEdge(t, ctx, f, waiter, spawner, types.DepWaitsFor) + blListerSeedEdge(t, ctx, f, conditional, target, types.DepConditionalBlocks) + + items := blListerList(t, ctx, f, publicops.BlockedRequest{Labels: []string{label}}) + blListerAssertRows(t, items, map[string][]string{waiter: {spawner}, conditional: {target}}) +} + +// runBlockedOmitsAStaleFlag: a row whose stored flag says blocked but which +// has no active blocker and no parent names no blocker, so it is not listed. +// This is the narrowing gc's readiness projection documents relying on. +func runBlockedOmitsAStaleFlag(t *testing.T, ctx context.Context, f BlockedListerFixture) { + if f.MarkBlocked == nil { + t.Skip("this leg cannot write the stored is_blocked flag directly") + } + label := f.IssuePrefix + "-bl-stale" + stale, real, blocker := blListerID(f, "stale-src"), blListerID(f, "stale-real"), blListerID(f, "stale-blocker") + blListerSeedIssue(t, ctx, f, blListerIssue(stale, 1, label)) + blListerSeedIssue(t, ctx, f, blListerIssue(real, 1, label)) + blListerSeedIssue(t, ctx, f, blListerIssue(blocker, 1, label+"-other")) + blListerSeedEdge(t, ctx, f, real, blocker, types.DepBlocks) + if err := f.MarkBlocked(ctx, stale); err != nil { + t.Fatalf("mark %s blocked: %v", stale, err) + } + + items := blListerList(t, ctx, f, publicops.BlockedRequest{Labels: []string{label}}) + blListerAssertRows(t, items, map[string][]string{real: {blocker}}) +} + +// runBlockedHasNoTypeExclusions: a gate is listed like any other row; the +// blocked door excludes no issue type (unlike the ready front). +func runBlockedHasNoTypeExclusions(t *testing.T, ctx context.Context, f BlockedListerFixture) { + label := f.IssuePrefix + "-bl-type" + gate, epic, blocker := blListerID(f, "type-gate"), blListerID(f, "type-epic"), blListerID(f, "type-blocker") + g := blListerIssue(gate, 1, label) + g.IssueType = types.TypeGate + g.AwaitType = "human" + blListerSeedIssue(t, ctx, f, g) + e := blListerIssue(epic, 1, label) + e.IssueType = types.TypeEpic + blListerSeedIssue(t, ctx, f, e) + blListerSeedIssue(t, ctx, f, blListerIssue(blocker, 1, label+"-other")) + blListerSeedEdge(t, ctx, f, gate, blocker, types.DepBlocks) + blListerSeedEdge(t, ctx, f, epic, blocker, types.DepBlocks) + + items := blListerList(t, ctx, f, publicops.BlockedRequest{Labels: []string{label}}) + blListerAssertRows(t, items, map[string][]string{gate: {blocker}, epic: {blocker}}) +} + +// runBlockedEmptyIsAnEmptySlice: nothing blocked is an empty, non-nil slice +// and a nil error. +func runBlockedEmptyIsAnEmptySlice(t *testing.T, ctx context.Context, f BlockedListerFixture) { + result, err := f.BlockedLister.ListBlocked(ctx, publicops.BlockedRequest{Labels: []string{f.IssuePrefix + "-bl-nothing"}}) + if err != nil { + t.Fatalf("ListBlocked: %v", err) + } + if result.Items == nil || len(result.Items) != 0 { + t.Fatalf("Items = %#v, want an empty non-nil slice", result.Items) + } +} + +// runBlockedDoesNotMutateTheRequest: the caller's label slices come back as +// they were sent, normalization notwithstanding. +func runBlockedDoesNotMutateTheRequest(t *testing.T, ctx context.Context, f BlockedListerFixture) { + req := publicops.BlockedRequest{ + ParentID: " ", + Labels: []string{" a ", "", "a"}, + LabelsAny: []string{" b"}, + ExcludeLabels: []string{"c "}, + } + before := publicops.BlockedRequest{ + ParentID: req.ParentID, + Labels: slices.Clone(req.Labels), + LabelsAny: slices.Clone(req.LabelsAny), + ExcludeLabels: slices.Clone(req.ExcludeLabels), + } + if _, err := f.BlockedLister.ListBlocked(ctx, req); err != nil { + t.Fatalf("ListBlocked: %v", err) + } + if !reflect.DeepEqual(req, before) { + t.Errorf("request after = %#v, before = %#v", req, before) + } +} + +func blListerID(f BlockedListerFixture, name string) string { + return fmt.Sprintf("%s-%s", f.IssuePrefix, name) +} + +func blListerIssue(id string, priority int, labels ...string) *types.Issue { + return &types.Issue{ + ID: id, + Title: id, + Status: types.StatusOpen, + Priority: priority, + IssueType: types.TypeTask, + Labels: labels, + } +} + +func blListerSeedIssue(t *testing.T, ctx context.Context, f BlockedListerFixture, issue *types.Issue) { + t.Helper() + if err := f.CreateIssue(ctx, issue, "seed"); err != nil { + t.Fatalf("seed issue %s: %v", issue.ID, err) + } +} + +func blListerSeedEdge(t *testing.T, ctx context.Context, f BlockedListerFixture, from, to string, kind types.DependencyType) { + t.Helper() + if err := f.AddDependency(ctx, &types.Dependency{IssueID: from, DependsOnID: to, Type: kind}, "seed"); err != nil { + t.Fatalf("seed %s edge %s -> %s: %v", kind, from, to, err) + } +} + +func blListerList(t *testing.T, ctx context.Context, f BlockedListerFixture, req publicops.BlockedRequest) []*types.BlockedIssue { + t.Helper() + result, err := f.BlockedLister.ListBlocked(ctx, req) + if err != nil { + t.Fatalf("ListBlocked(%+v): %v", req, err) + } + if result.Items == nil { + t.Fatalf("ListBlocked(%+v) Items is nil; the role promises a non-nil slice", req) + } + return result.Items +} + +func blListerIDs(items []*types.BlockedIssue) []string { + ids := make([]string, 0, len(items)) + for _, item := range items { + ids = append(ids, item.ID) + } + return ids +} + +// assertBlockedRows requires the listing to be exactly want: the same ids, +// each naming exactly the blockers want gives it (in any order), with +// BlockedByCount equal to their number. +func blListerAssertRows(t *testing.T, items []*types.BlockedIssue, want map[string][]string) { + t.Helper() + got := map[string][]string{} + for _, item := range items { + if item == nil { + t.Fatalf("listing carries a nil row") + } + if _, dup := got[item.ID]; dup { + t.Errorf("row %s listed twice", item.ID) + } + blockers := slices.Clone(item.BlockedBy) + slices.Sort(blockers) + got[item.ID] = blockers + if item.BlockedByCount != len(item.BlockedBy) { + t.Errorf("row %s: BlockedByCount = %d, len(BlockedBy) = %d", item.ID, item.BlockedByCount, len(item.BlockedBy)) + } + } + normalized := map[string][]string{} + for id, blockers := range want { + b := slices.Clone(blockers) + slices.Sort(b) + normalized[id] = b + } + if !reflect.DeepEqual(got, normalized) { + t.Errorf("listing = %v, want %v", got, normalized) + } +} diff --git a/backend/http/BUILD.bazel b/backend/http/BUILD.bazel index da2d2454d..1e622733c 100644 --- a/backend/http/BUILD.bazel +++ b/backend/http/BUILD.bazel @@ -31,6 +31,8 @@ go_test( timeout = "long", srcs = [ "attach_test.go", + "e2e_blocked_flags_test.go", + "e2e_blocked_test.go", "e2e_cli_test.go", "e2e_export_test.go", "e2e_gates_test.go", diff --git a/backend/http/corpus/harness_test.go b/backend/http/corpus/harness_test.go index ca00eea59..d2a2c4d02 100644 --- a/backend/http/corpus/harness_test.go +++ b/backend/http/corpus/harness_test.go @@ -144,6 +144,8 @@ func corpusServeConfig(t *testing.T, s storage.DoltStorage) httpapi.Config { must("LeaseReclaimer") cfg.Heartbeater, err = s.Heartbeater() must("Heartbeater") + cfg.BlockedLister, err = s.BlockedLister() + must("BlockedLister") cfg.Relations, err = s.IssueRelations() must("IssueRelations") cfg.Commenter, err = s.Commenter() diff --git a/backend/http/corpus/testdata/corpus.yaml b/backend/http/corpus/testdata/corpus.yaml index 788ea038f..925d1db64 100644 --- a/backend/http/corpus/testdata/corpus.yaml +++ b/backend/http/corpus/testdata/corpus.yaml @@ -404,7 +404,23 @@ shapes: - id: gc-blocked source: gc bdstore_ready_projection.go fetchReadyProjectionViaBlocked args: [blocked, --json] - pending: {slice: "S18", reason: "bd blocked hits the GetBlockedIssues stub (B3 #10)"} + pending: {slice: "SRV-1", reason: "bd blocked dials listBlockedIssues behind blocked.list, which the reference server does not advertise, so the client refuses at its capability preflight (pinned passing against the current server by backend/http TestE2E_BlockedOverHTTP); remove the mark when reference_server.json is upgraded"} + - id: gc-blocked-flags + source: gc bdstore_ready_projection.go readiness door (bd blocked --flags --json, ids on stdin) + args: [blocked, --flags, --json] + stdin: "e2e-mol\ne2e-mol.1\ne2e-mol.2\ne2e-no-such-row\n" + compare: json + pending: {slice: "SRV-1", reason: "bd blocked --flags reads listBlockingAnnotations is_blocked behind dependencies.blocking.isBlocked, which the reference server does not advertise, so the client refuses at its capability preflight (pinned passing against the current server by backend/http TestE2E_BlockedFlagsOverHTTP); remove the mark when reference_server.json is upgraded"} + - id: gc-blocked-flags-raw + source: gc bdstore_ready_projection.go readiness door (the stored is_blocked column, bd blocked --flags --raw --json, ids on stdin) + args: [blocked, --flags, --raw, --json] + stdin: "e2e-mol\ne2e-mol.1\ne2e-mol.2\ne2e-no-such-row\n" + compare: json + pending: {slice: "SRV-1", reason: "bd blocked --flags reads listBlockingAnnotations is_blocked behind dependencies.blocking.isBlocked, which the reference server does not advertise, so the client refuses at its capability preflight (pinned passing against the current server by backend/http TestE2E_BlockedFlagsOverHTTP); remove the mark when reference_server.json is upgraded"} + - id: gc-blocked-parent + source: bd blocked --parent (the molecule-scoped blocked listing) + args: [blocked, --json, --parent, e2e-mol] + pending: {slice: "SRV-1", reason: "bd blocked dials listBlockedIssues behind blocked.list, which the reference server does not advertise, so the client refuses at its capability preflight (pinned passing against the current server by backend/http TestE2E_BlockedOverHTTP); remove the mark when reference_server.json is upgraded"} # ---------------------------------------------------------------- molecules - id: mc-mol-current diff --git a/backend/http/e2e_blocked_flags_test.go b/backend/http/e2e_blocked_flags_test.go new file mode 100644 index 000000000..1943bd676 --- /dev/null +++ b/backend/http/e2e_blocked_flags_test.go @@ -0,0 +1,287 @@ +//go:build cgo + +package bdhttp_test + +import ( + "context" + "encoding/json" + "errors" + "os/exec" + "reflect" + "sort" + "strings" + "testing" + + "github.com/steveyegge/beads/internal/storage/issueops" +) + +// runBDStdin is runBD with stdin. +func runBDStdin(t *testing.T, f *remoteRolesFixture, stdin string, args ...string) bdResult { + t.Helper() + cmd := exec.Command(f.bin, args...) + cmd.Dir = f.dir + cmd.Env = minimalBDEnv(f.dir) + cmd.Stdin = strings.NewReader(stdin) + var o, e strings.Builder + cmd.Stdout, cmd.Stderr = &o, &e + err := cmd.Run() + code := 0 + var exitErr *exec.ExitError + if errors.As(err, &exitErr) { + code = exitErr.ExitCode() + } else if err != nil { + t.Fatalf("run bd %v: %v", args, err) + } + return bdResult{stdout: o.String(), stderr: e.String(), code: code} +} + +type blockedFlagRow struct { + ID string `json:"id"` + IsBlocked bool `json:"is_blocked"` +} + +func parseBlockedFlags(t *testing.T, out string) []blockedFlagRow { + t.Helper() + var rows []blockedFlagRow + if err := json.Unmarshal([]byte(strings.TrimSpace(out)), &rows); err != nil { + t.Fatalf("parse bd blocked --flags --json: %v\n%s", err, out) + } + if rows == nil { + t.Fatalf("bd blocked --flags --json printed null, want an array: %s", out) + } + return rows +} + +// flagStdin is every id the fixture created, in title order, plus an id that +// names no row, a blank line and a repeat: the omitted, ignored and collapsed +// cases. +func flagStdin(f *remoteRolesFixture) string { + titles := make([]string, 0, len(f.ids)) + for title := range f.ids { + titles = append(titles, title) + } + sort.Strings(titles) + var b strings.Builder + for _, title := range titles { + b.WriteString(f.ids[title] + "\n") + } + b.WriteString("\n " + f.ids[titles[0]] + " \ne2e-no-such-row\n") + return b.String() +} + +// TestE2E_BlockedFlagsOverHTTP runs `bd blocked --flags --json` (the blocked +// flag per id read from stdin, the readiness column gc projects) through the +// real `bd` binary against an in-process `bd serve` and against a local +// embedded workspace holding the same graph, and requires the same JSON once +// each generated id is mapped to its title. Over HTTP the read is +// listBlockingAnnotations with is_blocked; an older server's handshake is +// refused before the dial, naming dependencies.blocking.isBlocked. +// +// It also pins that this is the FLAG and not the listing: a row whose stored +// flag is set with no nameable blocker reads true here and is absent from +// `bd blocked --json`. +func TestE2E_BlockedFlagsOverHTTP(t *testing.T) { + skipUnlessEmbeddedDolt(t) + bin := buildBD(t) + + seed := func(f *remoteRolesFixture) { + f.seed() + blocker := f.create("blocker", "-t", "task") + gp := f.create("gate-parent", "-t", "epic") + kid := f.create("gate-kid", "-t", "task") + f.mustRun(nil, "dep", "add", kid, gp, "--type", "parent-child") + f.mustRun(nil, "dep", "add", gp, blocker) + } + + t.Run("served equals local", func(t *testing.T) { + addr := startE2EServer(t) + local := newEmbeddedFixture(t, bin) + seed(local) + served := newHTTPFixture(t, bin, addr) + seed(served) + + lr := runBDStdin(t, local, flagStdin(local), "blocked", "--flags", "--json") + sr := runBDStdin(t, served, flagStdin(served), "blocked", "--flags", "--json") + if lr.code != 0 || sr.code != 0 { + t.Fatalf("exit local=%d served=%d\nlocal: %s %s\nserved: %s %s", lr.code, sr.code, lr.stdout, lr.stderr, sr.stdout, sr.stderr) + } + normalize := func(f *remoteRolesFixture, rows []blockedFlagRow) []blockedFlagRow { + titleOf := map[string]string{} + for title, id := range f.ids { + titleOf[id] = title + } + out := make([]blockedFlagRow, 0, len(rows)) + for _, row := range rows { + if title, ok := titleOf[row.ID]; ok { + row.ID = title + } + out = append(out, row) + } + return out + } + want := normalize(local, parseBlockedFlags(t, lr.stdout)) + got := normalize(served, parseBlockedFlags(t, sr.stdout)) + if !reflect.DeepEqual(want, got) { + t.Errorf("served bd blocked --flags --json differs from the local command:\nlocal: %v\nserved: %v", want, got) + } + if len(want) != len(local.ids) { + t.Errorf("local answered %d rows for %d ids (the unknown id omitted, the repeat collapsed): %v", len(want), len(local.ids), want) + } + blocked := map[string]bool{} + for _, row := range got { + blocked[row.ID] = row.IsBlocked + } + for title, wantBlocked := range map[string]bool{ + "loner": true, "child-x": true, "gate-parent": true, "gate-kid": true, + "blocker": false, "child-b": false, "parent": false, + } { + if blocked[title] != wantBlocked { + t.Errorf("%s is_blocked = %v, want %v (rows %v)", title, blocked[title], wantBlocked, got) + } + } + // Byte-for-byte the same members in the same order. + if strings.Count(lr.stdout, "\n") != strings.Count(sr.stdout, "\n") { + t.Errorf("line counts differ:\nlocal:\n%s\nserved:\n%s", lr.stdout, sr.stdout) + } + // Nothing piped is an empty array. + if r := runBDStdin(t, served, "", "blocked", "--flags", "--json"); r.code != 0 || strings.TrimSpace(r.stdout) != "[]" { + t.Errorf("empty stdin: exit %d stdout %q, want [] and exit 0", r.code, r.stdout) + } + // The listing's filters are refused, not silently ignored. + if r := runBDStdin(t, served, "x\n", "blocked", "--flags", "--json", "--label", "a"); r.code != 1 { + t.Errorf("--flags --label exited %d, want 1: %s", r.code, r.stdout) + } + }) + + t.Run("the flag, not the listing", func(t *testing.T) { + addr, reference := startE2EServerWithStore(t, e2eProjectID, e2eDatabase) + served := newHTTPFixture(t, bin, addr) + stale := served.create("stale") + if err := reference.RunEventsMaintenanceTx(context.Background(), func(ctx context.Context, tx issueops.DBTX) error { + _, err := tx.ExecContext(ctx, "UPDATE issues SET is_blocked = 1 WHERE id = ?", stale) + return err + }); err != nil { + t.Fatalf("set the stale flag: %v", err) + } + rows := parseBlockedFlags(t, runBDStdin(t, served, stale+"\n", "blocked", "--flags", "--json").stdout) + if len(rows) != 1 || rows[0].ID != stale || !rows[0].IsBlocked { + t.Errorf("--flags for a stale-flagged row = %v, want [{%s true}]", rows, stale) + } + listing := served.mustRun(nil, "blocked", "--json").stdout + if strings.Contains(listing, stale) { + t.Errorf("bd blocked --json lists %s, which has no nameable blocker: %s", stale, listing) + } + }) + + t.Run("raw is the stored column", func(t *testing.T) { + addr, reference := startE2EServerWithStore(t, e2eProjectID, e2eDatabase) + local := newEmbeddedFixture(t, bin) + seed(local) + served := newHTTPFixture(t, bin, addr) + seed(served) + + flagsOf := func(f *remoteRolesFixture, stdin string, args ...string) map[string]bool { + t.Helper() + r := runBDStdin(t, f, stdin, append([]string{"blocked", "--flags", "--json"}, args...)...) + if r.code != 0 { + t.Fatalf("bd blocked --flags --json %v exited %d: %s %s", args, r.code, r.stdout, r.stderr) + } + titleOf := map[string]string{} + for title, id := range f.ids { + titleOf[id] = title + } + out := map[string]bool{} + for _, row := range parseBlockedFlags(t, r.stdout) { + key := row.ID + if title, ok := titleOf[row.ID]; ok { + key = title + } + out[key] = row.IsBlocked + } + return out + } + + localRaw := flagsOf(local, flagStdin(local), "--raw") + servedRaw := flagsOf(served, flagStdin(served), "--raw") + if !reflect.DeepEqual(localRaw, servedRaw) { + t.Errorf("served --raw differs from local --raw:\nlocal: %v\nserved: %v", localRaw, servedRaw) + } + lr := runBDStdin(t, local, flagStdin(local), "blocked", "--flags", "--json", "--raw") + sr := runBDStdin(t, served, flagStdin(served), "blocked", "--flags", "--json", "--raw") + if strings.Count(lr.stdout, "\n") != strings.Count(sr.stdout, "\n") { + t.Errorf("--raw line counts differ:\nlocal:\n%s\nserved:\n%s", lr.stdout, sr.stdout) + } + + // child-x is blocked ONLY by an unsatisfied external ref: the policy + // flag says blocked, the stored column does not. + decorated := flagsOf(served, flagStdin(served)) + if !decorated["child-x"] || servedRaw["child-x"] || localRaw["child-x"] { + t.Errorf("child-x (externally blocked only): --flags %v, --raw served %v local %v; want true, false, false", + decorated["child-x"], servedRaw["child-x"], localRaw["child-x"]) + } + // Locally blocked rows read the same either way. + for _, title := range []string{"loner", "gate-parent", "gate-kid", "blocker"} { + if decorated[title] != servedRaw[title] { + t.Errorf("%s: --flags %v, --raw %v; a local blocker is the column", title, decorated[title], servedRaw[title]) + } + } + + // A stale flag (set, no blocker) is the column, so both read true. + stale := served.create("stale") + if err := reference.RunEventsMaintenanceTx(context.Background(), func(ctx context.Context, tx issueops.DBTX) error { + _, err := tx.ExecContext(ctx, "UPDATE issues SET is_blocked = 1 WHERE id = ?", stale) + return err + }); err != nil { + t.Fatalf("set the stale flag: %v", err) + } + if got := flagsOf(served, stale+"\n", "--raw"); !got["stale"] { + t.Errorf("--raw for the stale-flagged row = %v, want true", got) + } + if got := flagsOf(served, stale+"\n"); !got["stale"] { + t.Errorf("--flags for the stale-flagged row = %v, want true", got) + } + + // --raw qualifies --flags only. + if r := runBDStdin(t, served, "", "blocked", "--json", "--raw"); r.code != 1 || !strings.Contains(r.stdout+r.stderr, "--raw") { + t.Errorf("bd blocked --raw without --flags: exit %d %s %s, want exit 1 naming --raw", r.code, r.stdout, r.stderr) + } + + // A server that applies the policy answers the decorated flag and the + // wire cannot ask it for the column: --raw is refused there, before + // the annotation read, rather than answered with the policy flag. + proxyAddr, counter := startMaskingProxy(t, addr, func(body map[string]any) { + caps, _ := body["capabilities"].([]any) + body["capabilities"] = append(caps, "policy.external_dependencies") + }) + enforced := newHTTPFixture(t, bin, proxyAddr) + counter.reset() + r := runBDStdin(t, enforced, served.ids["child-x"]+"\n", "blocked", "--flags", "--json", "--raw") + if r.code != 1 || !strings.Contains(r.stdout+r.stderr, "policy.external_dependencies") { + t.Errorf("--raw against a policy server: exit %d %s %s, want exit 1 naming the policy token", r.code, r.stdout, r.stderr) + } + for _, p := range counter.snapshot() { + if strings.Contains(p, "/v0/beads/dependencies/blocking") { + t.Errorf("a refused --raw dialed %s", p) + } + } + }) + + t.Run("older server mask", func(t *testing.T) { + addr := startE2EServer(t) + proxyAddr, counter := startMaskingProxy(t, addr, mcContextMask) + f := newHTTPFixture(t, bin, proxyAddr) + counter.reset() + r := runBDStdin(t, f, "e2e-anything\n", "blocked", "--flags", "--json") + if r.code != 1 { + t.Fatalf("bd blocked --flags against a server without the token exited %d, want 1: %s", r.code, r.stdout) + } + if !strings.Contains(r.stdout+r.stderr, "dependencies.blocking.isBlocked") { + t.Errorf("refusal does not name the missing capability: stdout=%s stderr=%s", r.stdout, r.stderr) + } + for _, p := range counter.snapshot() { + if strings.Contains(p, "/v0/beads/dependencies/blocking") { + t.Errorf("a refused bd blocked --flags dialed %s", p) + } + } + }) +} diff --git a/backend/http/e2e_blocked_test.go b/backend/http/e2e_blocked_test.go new file mode 100644 index 000000000..786379fba --- /dev/null +++ b/backend/http/e2e_blocked_test.go @@ -0,0 +1,229 @@ +//go:build cgo + +package bdhttp_test + +import ( + "encoding/json" + "reflect" + "slices" + "strings" + "testing" +) + +// TestE2E_BlockedOverHTTP runs `bd blocked --json` (the blocked door gc's +// readiness projection reads) through the real `bd` binary against an +// in-process `bd serve`, and against a local embedded workspace holding the +// same graph. +// +// Over HTTP the CLI takes the store's BlockedLister, which dials +// GET /v0/beads/blocked behind blocked.list. The two answers must be the same +// JSON once the values that cannot agree between two workspaces (each row's +// generated id, and its timestamps) are mapped out: the same rows, the same +// members, the same blockers (local, parent-attributed and external), in the +// same order. Against a server that does not advertise blocked.list (an older +// server's handshake) the client refuses before the dial, naming the token. +func TestE2E_BlockedOverHTTP(t *testing.T) { + skipUnlessEmbeddedDolt(t) + bin := buildBD(t) + addr := startE2EServer(t) + + seedBlocked := func(f *remoteRolesFixture) { + f.seed() + // A plain blocks edge, a blocked parent's child attributed to the + // parent, and a labelled blocked row for the filter arms. + blocker := f.create("blocker", "-t", "task", "-p", "1") + gp := f.create("gate-parent", "-t", "epic", "-p", "2") + // Its own priority: rows tied on priority and created_at fall to the + // id order, and ids are generated per workspace, so a tie would order + // the two workspaces differently. + kid := f.create("gate-kid", "-t", "task", "-p", "3") + tagged := f.create("tagged", "-t", "task", "-p", "1", "-l", "lane:a") + // Blocked by nothing but an unsatisfied external ref, as child-x is, + // and labelled: the decorator lists such rows itself, after the label + // query ran, so the filter arms must judge them too. + extTagged := f.create("ext-tagged", "-t", "task", "-p", "4", "-l", "lane:a") + f.mustRun(nil, "dep", "add", kid, gp, "--type", "parent-child") + f.mustRun(nil, "dep", "add", gp, blocker) + f.mustRun(nil, "dep", "add", tagged, blocker) + f.mustRun(nil, "dep", "add", extTagged, "external:other:cap") + } + + local := newEmbeddedFixture(t, bin) + seedBlocked(local) + served := newHTTPFixture(t, bin, addr) + seedBlocked(served) + + for _, args := range [][]string{ + {"blocked", "--json"}, + {"blocked", "--json", "--label", "lane:a"}, + {"blocked", "--json", "--label-any", "lane:a"}, + {"blocked", "--json", "--exclude-label", "lane:a"}, + {"blocked", "--json", "--parent", "{gate-parent}"}, + {"blocked", "--json", "--label", "no-such-label"}, + } { + t.Run(strings.Join(args[1:], " "), func(t *testing.T) { + want := local.mustRun(nil, local.expand(args)...).stdout + got := served.mustRun(nil, served.expand(args)...).stdout + if strings.TrimSpace(want) == "null" || strings.TrimSpace(got) == "null" { + t.Fatalf("bd blocked --json printed null: local=%s served=%s", want, got) + } + if w, g := normalizeBlocked(t, local, want), normalizeBlocked(t, served, got); !reflect.DeepEqual(w, g) { + t.Errorf("served bd blocked --json differs from the local command:\nlocal: %v\nserved: %v", w, g) + } + if w, g := blockedKeyOrder(t, want), blockedKeyOrder(t, got); !reflect.DeepEqual(w, g) { + t.Errorf("served member order %v differs from the local command's %v", g, w) + } + }) + } + + t.Run("names the external blocker", func(t *testing.T) { + got := normalizeBlocked(t, served, served.mustRun(nil, "blocked", "--json").stdout) + found := false + for _, row := range got { + if row["id"] == "child-x" { + found = reflect.DeepEqual(row["blocked_by"], []any{"external:other:cap"}) + } + } + if !found { + t.Errorf("served bd blocked --json does not list child-x blocked by its external ref: %v", got) + } + }) + + // The arms above compare the two answers, which agree just as well when + // both list a row the filter rules out. These pin which rows each filter + // leaves, the external-only ones (child-x unlabelled, ext-tagged + // labelled) among them. + for _, tc := range []struct { + args []string + want []string // the whole listing, when set + listed, ruled []string + }{ + {args: []string{"--label", "lane:a"}, want: []string{"ext-tagged", "tagged"}}, + {args: []string{"--label-any", "lane:a"}, want: []string{"ext-tagged", "tagged"}}, + {args: []string{"--label", "no-such-label"}, want: []string{}}, + {args: []string{"--exclude-label", "lane:a"}, listed: []string{"child-x"}, ruled: []string{"ext-tagged", "tagged"}}, + } { + t.Run("filters external-only rows "+strings.Join(tc.args, " "), func(t *testing.T) { + for _, f := range []struct { + name string + f *remoteRolesFixture + }{{"local", local}, {"served", served}} { + got := f.f.titlesOf(f.f.mustRun(nil, append([]string{"blocked", "--json"}, tc.args...)...).stdout) + if tc.want != nil && !slices.Equal(got, tc.want) { + t.Errorf("%s bd blocked %s listed %v, want %v", f.name, strings.Join(tc.args, " "), got, tc.want) + } + for _, title := range tc.listed { + if !slices.Contains(got, title) { + t.Errorf("%s bd blocked %s does not list %s: %v", f.name, strings.Join(tc.args, " "), title, got) + } + } + for _, title := range tc.ruled { + if slices.Contains(got, title) { + t.Errorf("%s bd blocked %s lists %s, which the filter rules out: %v", f.name, strings.Join(tc.args, " "), title, got) + } + } + } + }) + } + + t.Run("older server mask", func(t *testing.T) { + proxyAddr, counter := startMaskingProxy(t, addr, mcContextMask) + f := newHTTPFixture(t, bin, proxyAddr) + counter.reset() + r := f.run(nil, "blocked", "--json") + if r.code != 1 { + t.Fatalf("bd blocked against a server without blocked.list exited %d, want 1: %s", r.code, r.stdout) + } + if !strings.Contains(r.stdout+r.stderr, "blocked.list") { + t.Errorf("refusal does not name the missing capability: stdout=%s stderr=%s", r.stdout, r.stderr) + } + for _, p := range counter.snapshot() { + if strings.Contains(p, "/v0/beads/blocked") { + t.Errorf("a refused bd blocked dialed %s; the refusal must come before the dial", p) + } + } + }) +} + +// normalizeBlocked parses `bd blocked --json` and maps out what two +// workspaces cannot share: each id (and each blocker id) becomes the title the +// fixture created it under, and every timestamp member is blanked. +func normalizeBlocked(t *testing.T, f *remoteRolesFixture, out string) []map[string]any { + t.Helper() + var rows []map[string]any + if err := json.Unmarshal([]byte(strings.TrimSpace(out)), &rows); err != nil { + t.Fatalf("parse bd blocked --json: %v\n%s", err, out) + } + titleOf := map[string]string{} + for title, id := range f.ids { + titleOf[id] = title + } + var walk func(key string, v any) any + walk = func(key string, v any) any { + switch v := v.(type) { + case map[string]any: + for k, inner := range v { + v[k] = walk(k, inner) + } + return v + case []any: + for i := range v { + v[i] = walk("", v[i]) + } + return v + case string: + if strings.HasSuffix(key, "_at") { + return "