diff --git a/.golangci.yml b/.golangci.yml index 9e1cf64a3..1ed2975a4 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -471,7 +471,13 @@ linters: # `search` LEFT this list when all three of its routes moved onto # issueops.Reader.List with issueops.SearchListRequest (cmd/bd/search.go); # it builds no filter any more. - - path: '^cmd/bd/(close|duplicates|find_duplicates|gate|gc|graph|info|label|orphans|purge|todo|wisp|human)(_proxied_server)?\.go$' + # + # `gate` LEFT it when `bd gate check` joined `bd gate list` on + # issueops.ListGates over the route's Querier: cmd/bd/gate.go and + # cmd/bd/gate_proxied_server.go name no filter type any more. + # `gate_discover` keeps its own hole below, since findPendingGates still + # asks SearchIssues for its gates. + - path: '^cmd/bd/(close|duplicates|find_duplicates|gc|graph|info|label|orphans|purge|todo|wisp|human)(_proxied_server)?\.go$' text: 'do not write a filter here' linters: - forbidigo diff --git a/CHANGELOG.md b/CHANGELOG.md index a5618ca17..db9794451 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -524,6 +524,21 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- **`bd gate check` works over a connected HTTP workspace.** Its open-gate + listing built the same raw `SearchIssues` filter `bd gate list` did before + S20, so the HTTP backend refused the command before reading a gate and gc's + core `gate-sweep` order (`bd gate check --type=timer --escalate`) failed on + every remote scope. It now lists through `issueops.ListGates` over the + route's Querier (direct, proxied and remote alike). Closing a resolved gate + was already served. Two writes the check can make still refuse over HTTP, + because the HTTP store has no raw `UpdateIssue` and the client refuses it + before sending a request: a bead gate's first sighting of its awaited bead + (`await_seen`, recorded when the check finds that bead, not yet closed, in + the gate's own store) and the gh:run discovery's `await_id` persist. Each + counts as that gate's error. A negative `--limit` is now an error; the + direct and proxied routes used to read it as no limit. The served corpus + gains `mc-gate-check-timer`, `mc-gate-check-gh` and `gate-check-all`. + - **`bd gate list` works over a connected HTTP workspace** (S20). The workspace-wide listing built a raw `SearchIssues` filter, which the HTTP backend refuses, so gc's `renudge-stale-human-gates` order failed on every diff --git a/backend/http/corpus/testdata/corpus.yaml b/backend/http/corpus/testdata/corpus.yaml index a0f1b5925..a3535ec71 100644 --- a/backend/http/corpus/testdata/corpus.yaml +++ b/backend/http/corpus/testdata/corpus.yaml @@ -441,6 +441,23 @@ shapes: - id: mc-gate-list-all source: core pack orders (gate list --all) args: [gate, list, --all, --json] + # gate check lists the open gates through the same ListGates entry. No + # seeded gate carries an await type (no wire create accepts await_*), so the + # typed sweeps find nothing of their type and the untyped one lists the open + # gates and evaluates none: the listing is what these shapes prove served, + # and nothing resolves, escalates (no gt exec) or writes. + - id: mc-gate-check-timer + source: gc core pack gate-sweep.sh (bd gate check --type=timer --escalate) + args: [gate, check, --type=timer, --escalate] + compare: text + - id: mc-gate-check-gh + source: gc core pack gate-sweep.sh (bd gate check --type=gh --escalate) + args: [gate, check, --type=gh, --escalate] + compare: text + - id: gate-check-all + source: bd gate check (every open gate) + args: [gate, check] + compare: text - id: gc-export-all source: gc core pack reaper (gc bd export --all -o F) diff --git a/cmd/bd/BUILD.bazel b/cmd/bd/BUILD.bazel index 2395a3b6f..2a6ac6e15 100644 --- a/cmd/bd/BUILD.bazel +++ b/cmd/bd/BUILD.bazel @@ -797,6 +797,7 @@ go_test( "formula_help_test.go", "formula_primitives_test.go", "formula_schema_test.go", + "gate_check_listing_test.go", "gate_check_proxied_integration_test.go", "gate_discover_test.go", "gate_embedded_test.go", diff --git a/cmd/bd/gate.go b/cmd/bd/gate.go index d42e2386d..3ce94cd1d 100644 --- a/cmd/bd/gate.go +++ b/cmd/bd/gate.go @@ -653,16 +653,9 @@ Examples: escalateFlag, _ := cmd.Flags().GetBool("escalate") limit, _ := cmd.Flags().GetInt("limit") - gateType := types.IssueType("gate") - filter := types.IssueFilter{ - IssueType: &gateType, - ExcludeStatus: []types.Status{types.StatusClosed}, - Limit: limit, - } - ctx := rootCtx - gates, err := store.SearchIssues(ctx, "", filter) + gates, err := listOpenGatesForCheck(ctx, limit) if err != nil { return HandleErrorRespectJSON("%v", err) } @@ -701,6 +694,28 @@ Examples: }, } +// listOpenGatesForCheck lists the open gates `bd gate check` evaluates through +// the same library entry `bd gate list` uses, issueops.ListGates over the +// route's Querier (openQuerier: the direct store, the proxied server's unit +// of work, or the remote http backend's queryIssues). It is the question the +// raw SearchIssues filter used to ask — the gate type, both planes, closed +// rows excluded, cut at limit — but a remote backend serves it, where the raw +// filter was refused before any gate was read. The per-type selection stays +// with filterCheckableGates. +func listOpenGatesForCheck(ctx context.Context, limit int) ([]*types.Issue, error) { + querier, err := openQuerier() + if err != nil { + return nil, err + } + return listOpenGatesWith(ctx, querier, limit) +} + +// listOpenGatesWith is listOpenGatesForCheck over a given Querier: the open +// gates only (gate check never evaluates a closed one), cut at limit. +func listOpenGatesWith(ctx context.Context, querier issueops.Querier, limit int) ([]*types.Issue, error) { + return issueops.ListGates(ctx, querier, issueops.GateListRequest{Limit: limit}) +} + type gateCheckResult struct { gate *types.Issue resolved bool diff --git a/cmd/bd/gate_check_listing_test.go b/cmd/bd/gate_check_listing_test.go new file mode 100644 index 000000000..b041713e8 --- /dev/null +++ b/cmd/bd/gate_check_listing_test.go @@ -0,0 +1,100 @@ +package main + +import ( + "context" + "errors" + "reflect" + "testing" + "time" + + "github.com/steveyegge/beads/internal/types" + "github.com/steveyegge/beads/issueops" +) + +// recordingGateQuerier records every Query and answers with fixed rows. +type recordingGateQuerier struct { + calls []issueops.QueryRequest + items []*types.IssueWithCounts + err error +} + +func (q *recordingGateQuerier) Query(_ context.Context, req issueops.QueryRequest) (issueops.IssuePage, error) { + q.calls = append(q.calls, req) + if q.err != nil { + return issueops.IssuePage{}, q.err + } + return issueops.IssuePage{Items: q.items}, nil +} + +// TestGateCheckListsOpenGatesThroughListGates pins the gate check listing to +// the Querier role (queryIssues on a remote backend) instead of the raw +// SearchIssues filter the http store refused before reading a gate: the gate +// type, closed excluded, the --limit carried as given (0 = unlimited). +func TestGateCheckListsOpenGatesThroughListGates(t *testing.T) { + for _, limit := range []int{0, 5, 100} { + q := &recordingGateQuerier{items: []*types.IssueWithCounts{ + {Issue: &types.Issue{ID: "g-timer", IssueType: "gate", AwaitType: "timer"}}, + {Issue: &types.Issue{ID: "g-pr", IssueType: "gate", AwaitType: "gh:pr"}}, + }} + got, err := listOpenGatesWith(context.Background(), q, limit) + if err != nil { + t.Fatalf("limit %d: listOpenGatesWith: %v", limit, err) + } + l := limit + want := issueops.QueryRequest{Expression: "type=gate", IncludeClosed: false, Limit: &l} + if len(q.calls) != 1 || !reflect.DeepEqual(q.calls[0], want) { + t.Fatalf("limit %d: Query calls = %#v, want one %#v", limit, q.calls, want) + } + if len(got) != 2 || got[0].ID != "g-timer" || got[1].ID != "g-pr" { + t.Fatalf("limit %d: gates = %v, want [g-timer g-pr] in the Querier's order", limit, got) + } + // The per-type selection still happens after the listing. + timers := filterCheckableGates(got, "timer") + if len(timers) != 1 || timers[0].ID != "g-timer" { + t.Fatalf("limit %d: timer gates = %v, want [g-timer]", limit, timers) + } + gh := filterCheckableGates(got, "gh") + if len(gh) != 1 || gh[0].ID != "g-pr" { + t.Fatalf("limit %d: gh gates = %v, want [g-pr]", limit, gh) + } + } +} + +// TestGateCheckListingFailureIsAnError keeps a refused or failed listing an +// error: gate-sweep must fail loudly, never report "No open gates found." +func TestGateCheckListingFailureIsAnError(t *testing.T) { + refusal := &issueops.ErrUnsupported{Op: "Querier.Query", Backend: "http"} + q := &recordingGateQuerier{err: refusal} + got, err := listOpenGatesWith(context.Background(), q, 0) + if !errors.Is(err, refusal) || got != nil { + t.Fatalf("listOpenGatesWith = (%v, %v), want (nil, the refusal)", got, err) + } +} + +// TestGateCheckTimerArmOverListedGates runs the evaluation over the listed +// rows: an expired timer gate resolves, an unexpired one stays pending, and a +// gate with no await type is not evaluated (the corpus seed's case). +func TestGateCheckTimerArmOverListedGates(t *testing.T) { + now := time.Date(2026, 10, 15, 12, 0, 0, 0, time.UTC) + q := &recordingGateQuerier{items: []*types.IssueWithCounts{ + {Issue: &types.Issue{ID: "g-expired", IssueType: "gate", AwaitType: "timer", CreatedAt: now.Add(-2 * time.Hour), Timeout: time.Hour}}, + {Issue: &types.Issue{ID: "g-pending", IssueType: "gate", AwaitType: "timer", CreatedAt: now.Add(-time.Minute), Timeout: time.Hour}}, + {Issue: &types.Issue{ID: "g-untyped", IssueType: "gate"}}, + }} + gates, err := listOpenGatesWith(context.Background(), q, 0) + if err != nil { + t.Fatalf("listOpenGatesWith: %v", err) + } + results := evaluateGates(context.Background(), filterCheckableGates(gates, ""), now, nil, nil, nil) + got := map[string]bool{} + for _, r := range results { + if r.err != nil { + t.Fatalf("%s: unexpected error %v", r.gate.ID, r.err) + } + got[r.gate.ID] = r.resolved + } + want := map[string]bool{"g-expired": true, "g-pending": false} + if !reflect.DeepEqual(got, want) { + t.Fatalf("evaluated = %v, want %v (g-untyped skipped)", got, want) + } +} diff --git a/cmd/bd/gate_proxied_server.go b/cmd/bd/gate_proxied_server.go index eef83fb30..15d5a133b 100644 --- a/cmd/bd/gate_proxied_server.go +++ b/cmd/bd/gate_proxied_server.go @@ -87,13 +87,6 @@ func runGateCheckProxiedServer(cmd *cobra.Command, ctx context.Context) error { return HandleErrorRespectJSON("proxied-server UOW provider not initialized") } - gateType := types.IssueType("gate") - filter := types.IssueFilter{ - IssueType: &gateType, - ExcludeStatus: []types.Status{types.StatusClosed}, - Limit: limit, - } - discovered := map[string]string{} var seen []*types.Issue var persistAwaitID func(gateID, runID string) error @@ -109,17 +102,12 @@ func runGateCheckProxiedServer(cmd *cobra.Command, ctx context.Context) error { } } - readUW, err := proxiedOpenReadUOW(ctx) + // The listing is the shared entry's, on this route too (gate list's S20). + gates, err := listOpenGatesForCheck(ctx, limit) if err != nil { - return err - } - page, err := readUW.IssueUseCase().SearchIssues(ctx, "", filter) - if err != nil { - readUW.Close(ctx) return HandleErrorRespectJSON("%v", err) } - filteredGates := filterCheckableGates(page.Items, gateTypeFilter) - readUW.Close(ctx) + filteredGates := filterCheckableGates(gates, gateTypeFilter) if len(filteredGates) == 0 { printNoOpenGates(gateTypeFilter) diff --git a/cmd/bd/gate_test.go b/cmd/bd/gate_test.go index b272d7f9e..d04a593af 100644 --- a/cmd/bd/gate_test.go +++ b/cmd/bd/gate_test.go @@ -19,6 +19,7 @@ import ( "github.com/steveyegge/beads/internal/storage" storageissueops "github.com/steveyegge/beads/internal/storage/issueops" "github.com/steveyegge/beads/internal/types" + "github.com/steveyegge/beads/issueops" ) var gateTestStdoutMu sync.Mutex @@ -33,15 +34,29 @@ type gateCloseCall struct { type fakeGateCheckStore struct { storage.DoltStorage issues []*types.Issue - searchFilter types.IssueFilter + queries []issueops.QueryRequest + searchCalled bool closeCalls []gateCloseCall } -func (f *fakeGateCheckStore) SearchIssues(_ context.Context, _ string, filter types.IssueFilter) ([]*types.Issue, error) { - f.searchFilter = filter +// SearchIssues records that the raw filter was used: gate check lists through +// the Querier role (ListGates), which a remote backend serves, and must not. +func (f *fakeGateCheckStore) SearchIssues(_ context.Context, _ string, _ types.IssueFilter) ([]*types.Issue, error) { + f.searchCalled = true return f.issues, nil } +func (f *fakeGateCheckStore) Querier() (issueops.Querier, error) { return f, nil } + +func (f *fakeGateCheckStore) Query(_ context.Context, req issueops.QueryRequest) (issueops.IssuePage, error) { + f.queries = append(f.queries, req) + items := make([]*types.IssueWithCounts, 0, len(f.issues)) + for _, issue := range f.issues { + items = append(items, &types.IssueWithCounts{Issue: issue}) + } + return issueops.IssuePage{Items: items}, nil +} + func (f *fakeGateCheckStore) CloseIssue(_ context.Context, id, reason, actor, session string) error { f.closeCalls = append(f.closeCalls, gateCloseCall{ id: id, @@ -1768,14 +1783,15 @@ func TestGateCheck_GHRunWorkflowDiscoveryPersistence(t *testing.T) { if !gateTestContains(output, "Checked 1 gates: 1 resolved, 0 escalated, 0 errors") { t.Fatalf("summary output missing expected counts: %q", output) } - if fakeStore.searchFilter.IssueType == nil || *fakeStore.searchFilter.IssueType != "gate" { - t.Fatalf("expected gate filter, got %+v", fakeStore.searchFilter) + if fakeStore.searchCalled { + t.Fatal("gate check listed through the raw SearchIssues filter; want issueops.ListGates") } - if len(fakeStore.searchFilter.ExcludeStatus) != 1 || fakeStore.searchFilter.ExcludeStatus[0] != types.StatusClosed { - t.Fatalf("expected closed-status exclusion, got %+v", fakeStore.searchFilter.ExcludeStatus) + if len(fakeStore.queries) != 1 { + t.Fatalf("Querier.Query calls = %d, want 1", len(fakeStore.queries)) } - if fakeStore.searchFilter.Limit != 100 { - t.Fatalf("expected limit 100, got %d", fakeStore.searchFilter.Limit) + q := fakeStore.queries[0] + if q.Expression != "type=gate" || q.IncludeClosed || q.Limit == nil || *q.Limit != 100 { + t.Fatalf("gate check query = %+v, want type=gate, closed excluded, limit 100", q) } if tt.wantCloseCalls == 1 { call := fakeStore.closeCalls[0]