feat(ready): put bd ready's published total behind the ReadyCounter role (bd-s10oa)
`bd ready --limit 100` prints "showing 100 of 412", and the 412 came from
nowhere in particular. The direct route computed it inline: unwrap past the
storage decorators, type-assert to storage.ReadyWorkCounter, and fall back to
an unbounded ready query when the assertion missed — twice, once in the --json
branch and once in the text one, with two different fallbacks. The proxied
route could not compute it at all and published no `total` key, so the same
script read a number under one workspace topology and nothing under another.
THE ROLE. issueops.ReadyCounter, reached through store.ReadyCounter() and the
provider's own accessor. It answers a question neither neighbour can: Reader
answers with a PAGE and its verdict is has-more ("did the limit hide
anything"), where this is "how many" — strictly larger, and already published
output, so collapsing the two would change what a shipped surface says. Counter
answers a predicate over one table, and the ready predicate is blocker-aware:
it reads the dependency graph and the wisp tier, which no CountRequest can
describe. Counter's own doc already said the count of ready work would be its
own role for that reason.
It takes issueops.ReadyRequest VERBATIM, the precedent ClaimNextRequest set, so
a listing, a claim and a count ask ONE question of ONE set. Limit and Offset are
ErrValidation for the reason a claim refuses them: a cardinality has no page, so
a limit would answer "how many of the first N" and an offset would subtract the
rows it skipped from the size of a set that still holds them.
THE CONTRACT IS AN IDENTITY, not a resemblance:
CountReady(r).Total == len(Reader.Ready(r with Limit=0).Items)
at every implementation. backend/conformance/ready_counter_contract.go holds
seven cases, each citing the leaf doc by line, run at all three backends with
ZERO parks. They are TWO independent bodies plus an engine check: dolt and
embeddeddolt share internal/workapi/storereadycounter (one indexed COUNT(*) per
plane minus their overlap), and the unit-of-work provider is genuinely separate
code that runs the unbounded ready query and takes its length — which is why
the wisp-tier case is written the way it is, since that is where a COUNT and
the page it sizes are assembled by different arithmetic.
A DEFECT THIS FOUND, and it is not the count's. `bd ready`'s directory-label
default (GH#541) never reached the ReadyClaimer role. claimNextRequest re-ran
the gatherer's own emptiness test against in.filter — and the gatherer had
already written the default INTO that filter, so the test saw a non-empty label
set and skipped. The branch could never fire. In a directory with
`directory.labels` configured, `bd ready --claim` therefore claimed from the
WHOLE ready front while `bd ready` beside it listed only the configured scope:
exactly the "hand an agent work the listing never offered it" that
ReadyClaimer's contract exists to forbid. The gatherer now RECORDS its decision
(readyInput.dirLabels) and both roles read it, so there is one answer instead of
two derivations of it. Caught by writing the count's own test for the same
line, and pinned by cmd/bd/ready_role_request_test.go.
WHAT ELSE CONVERGED. Both routes and both output modes now ask one question
through one accessor: the direct route's two inline epilogues become one call,
and the proxied route's `pagination.total` exists for the first time. The
"showing X of N" text a human reads and the total a script parses are the same
number by construction. cmd/bd no longer names storage.ReadyWorkCounter at all
— the capability is required by the store body's PARAMETER TYPE, so a backend
that cannot size the ready set fails to compile instead of falling back to a
mega-query at runtime.
THE HTTP OPERATION, spec first (per bd-jtp1h's A2 scope decision):
GET /v0/beads/ready:count, capability `ready.count`, answering
`ReadyCount {total}`. It publishes no `limit` and no `sort`: a cardinality has
no page and no order, and the handler sends the listing's own sort default so
the request stays the listing's request rather than one that resembles it. The
filter vocabulary is decoded by ONE function shared with GET /v0/beads/ready
(readyFilters), so the two operations cannot admit different parameters — which
is what the identity rests on over the wire.
`TestCountReadyAndListReadyAdmitTheSameFilters` compares the two predicates
built from one query string. No component here carries allOf and none is
x-go-type-pinned: the pinned seven are canonical Go structs whose JSON encoding
IS the contract, and there is no canonical struct for this number — the CLI
publishes it inside its own stdout envelope, which is not a wire type — so
pinning would weld the body to a CLI presentation type rather than prevent a
drift. The generated output was read to confirm it (a struct with `Total
int64`, not an alias), which is the v2.6.0 trap this document warns about.
httpapi.Config's ROLES database source grows a REQUIRED third member. That is
the sharp edge for the next role commit and is now written down in
engdocs/ADDING_AN_ISSUEOPS_ROLE.md as step 13: a Config carrying the old pair
would bind and then nil-dereference on the first request that reached the new
handler, so Listen refuses it with the same message a half-set pair gets.
USER-VISIBLE, for the wave's changelog pass: proxied `bd ready --json --limit N`
now publishes `pagination.total`; `bd ready --claim` now honours the
directory-label scope on both routes.
Verified: build, vet, gofmt clean; golangci-lint 0 issues; make api-check
(regenerate-and-diff plus the httpapi suite) green; TestReadyCounterContract
7/7 at dolt, embeddeddolt and uow; the httpapi package suite; the direct-route
`bd ready` suites under BEADS_TEST_EMBEDDED_DOLT=1 and the proxied ones under
BEADS_TEST_PROXIED_SERVER=1, including the pagination envelope that now asserts
the total; TestProxiedLookup*; and the ready protocol-corpus tests, which are
byte-identical.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
b6b319b981
commit
b1b253f35b
36 changed files with 2145 additions and 142 deletions
|
|
@ -165,6 +165,8 @@ linters:
|
|||
desc: "a command reaches the workspace-settings role through store.WorkspaceConfig(), never through the constructor: the accessor is where each storage decorator adds its layer - see internal/workapi/storeworkspaceconfig/workspaceconfig.go"
|
||||
- pkg: github.com/steveyegge/beads/internal/workapi/storestats
|
||||
desc: "a command reaches the summary role through store.StatsReporter(), never through the constructor: the accessor is where each storage decorator adds its layer - see internal/workapi/storestats/reporter.go"
|
||||
- pkg: github.com/steveyegge/beads/internal/workapi/storereadycounter
|
||||
desc: "a command reaches the ready-count role through store.ReadyCounter(), never through the constructor: the accessor is where each storage decorator adds its layer - see internal/workapi/storereadycounter/counter.go"
|
||||
# The CONSTRUCTION half of that same boundary, and the half that actually
|
||||
# matches the failure. The depguard rule above denies a handler IMPORTING
|
||||
# the builders; it does not deny a handler naming the filter types, which
|
||||
|
|
@ -273,9 +275,12 @@ linters:
|
|||
# than inlining a third copy of the default - bd-3fs.3. The blocked-issue
|
||||
# views in these files name the type directly for an empty filter or one
|
||||
# carrying only a --parent id, which is the absence of a filter rather
|
||||
# than one this command built. `bd ready` is on the shared BUILDER and,
|
||||
# on the proxied route, the shared EPILOGUE; it is not on the role, and
|
||||
# this rule does not pretend otherwise.
|
||||
# than one this command built. `bd ready`'s LISTING is on the shared
|
||||
# BUILDER and, on the proxied route, the shared EPILOGUE; it is not on a
|
||||
# role, and this rule does not pretend otherwise. Its other two questions
|
||||
# are: --claim goes to ReadyClaimer and the published total to
|
||||
# ReadyCounter, both over the request rather than the filter, on both
|
||||
# routes - which is why this hole keeps shrinking rather than growing.
|
||||
- path: '^cmd/bd/ready(_input|_proxied_server)?\.go$'
|
||||
linters:
|
||||
- forbidigo
|
||||
|
|
|
|||
434
backend/conformance/ready_counter_contract.go
Normal file
434
backend/conformance/ready_counter_contract.go
Normal file
|
|
@ -0,0 +1,434 @@
|
|||
package conformance
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/steveyegge/beads/internal/storage"
|
||||
"github.com/steveyegge/beads/internal/types"
|
||||
publicops "github.com/steveyegge/beads/issueops"
|
||||
)
|
||||
|
||||
// This file holds the semantic contract every implementation of
|
||||
// publicops.ReadyCounter must satisfy. Each case asserts what
|
||||
// issueops/readycounter.go PROMISES, cited by line, rather than what any one
|
||||
// backend happens to do; a backend that disagrees is parked at its own wiring
|
||||
// site with skipKnownDivergence so the case still runs on the ones that agree.
|
||||
//
|
||||
// THERE ARE TWO BODIES BEHIND THE THREE WIRINGS, and here they are further
|
||||
// apart than usual. dolt and embeddeddolt share internal/workapi/
|
||||
// storereadycounter, which is one indexed COUNT(*) per plane minus their
|
||||
// overlap; the unit-of-work provider runs the unbounded ready query and takes
|
||||
// its length. So the three wirings are one vote plus an engine check on the
|
||||
// cheap path, and a second, genuinely independent vote on the expensive one —
|
||||
// and the cases below are written to catch the arithmetic that only the first
|
||||
// of those does (the wisp merge, the overlap subtraction), which is exactly
|
||||
// where a COUNT can disagree with the page it claims to size.
|
||||
//
|
||||
// EVERY CASE IS SCOPED BY A LABEL, for the reason the ready-claim contract
|
||||
// gives: the ready front is a property of the whole database, not of an id
|
||||
// prefix, so a row seeded by one assertion is inside the next one's answer
|
||||
// unless each asks through Filter.Labels.
|
||||
//
|
||||
// EVERY CASE NAMES A SORT POLICY even though a count has no order, and that is
|
||||
// deliberate rather than copied: ReadyRequest.Sort's empty value resolves to
|
||||
// hybrid at the storage layer, which the leaf says no front door may rely on
|
||||
// (issueops/reader.go:79-83), and a count whose request is not the listing's
|
||||
// request is not the count this role promises.
|
||||
|
||||
// ReadyCounterFixture supplies adapter-specific storage access for the
|
||||
// ready-count assertions. Every field is named and typed exactly like the
|
||||
// per-backend roleFixtureKit hook it is filled from, so a wiring is kit plus
|
||||
// accessors plus prefix with no adapter in between.
|
||||
type ReadyCounterFixture struct {
|
||||
// IssuePrefix namespaces the ids each assertion seeds, so several of them
|
||||
// can share one database.
|
||||
IssuePrefix string
|
||||
// ReadyCounter is the surface under test.
|
||||
ReadyCounter publicops.ReadyCounter
|
||||
// Reader is the SAME backend's reader accessor, and this fixture cannot do
|
||||
// without it: the role's central promise is an IDENTITY with
|
||||
// len(Reader.Ready(r with Limit=0).Items) (issueops/readycounter.go:60-70),
|
||||
// and the only way to observe an identity is to ask both surfaces.
|
||||
Reader publicops.Reader
|
||||
// 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. It is a separate
|
||||
// field rather than an Ephemeral flag on CreateIssue because the three
|
||||
// adapters reach the two planes through different verbs.
|
||||
CreateWisp func(context.Context, *types.Issue, string) error
|
||||
// AddDependency seeds ONE edge, which is how a case takes a row OFF the
|
||||
// ready front without deleting or closing it.
|
||||
AddDependency func(context.Context, *types.Dependency, string) error
|
||||
// CountHistory reports how many history entries the fixture's branch has.
|
||||
// A nil hook means "this backend cannot observe history", and the case
|
||||
// that needs it SKIPS with that reason rather than passing quietly.
|
||||
CountHistory func(context.Context) (int, error)
|
||||
}
|
||||
|
||||
// RunReadyCounterEqualsTheUnboundedPage pins the identity that IS this role
|
||||
// (issueops/readycounter.go:60-70): CountReady(r).Total equals
|
||||
// len(Reader.Ready(r with Limit=0).Items), for every request the method
|
||||
// accepts.
|
||||
//
|
||||
// The case asks the SAME question three ways — an unbounded page, a page
|
||||
// bounded well below the seeded set, and the count — because the failure this
|
||||
// exists to prevent is a total that describes a different set than the page it
|
||||
// is printed beside. `bd ready` publishes both numbers together as "showing 2
|
||||
// of 5", and a reader takes the second as "how much work is left".
|
||||
//
|
||||
// The bounded page is the half that matters. A count computed by re-running
|
||||
// the listing would pass an unbounded comparison trivially; a count computed
|
||||
// from a COUNT(*) over a predicate assembled somewhere else is where the two
|
||||
// come apart, and it only shows up once a limit makes the numbers differ.
|
||||
//
|
||||
// The high-priority OUTSIDER is what makes the equality mean something: it is
|
||||
// ready, it would be counted by any request whose label predicate went missing
|
||||
// between the two surfaces, and it is outside the scope both are asked about.
|
||||
func RunReadyCounterEqualsTheUnboundedPage(t *testing.T, ctx context.Context, fixture ReadyCounterFixture) {
|
||||
t.Helper()
|
||||
label := fixture.IssuePrefix + "-rceq"
|
||||
for i, id := range readyCounterIDs(fixture, "rceq", 5) {
|
||||
seedReadyCounterIssue(t, ctx, fixture, readyCounterIssue(id, i%4, label))
|
||||
}
|
||||
outsider := fixture.IssuePrefix + "-rceq-outsider"
|
||||
seedReadyCounterIssue(t, ctx, fixture, readyCounterIssue(outsider, 0, label+"-other"))
|
||||
|
||||
request := publicops.ReadyRequest{Labels: []string{label}, Sort: readyCounterSort}
|
||||
unbounded := readyCounterPageIDs(t, ctx, fixture, request, 0)
|
||||
if len(unbounded) != 5 {
|
||||
t.Fatalf("Reader.Ready listed %d rows (%v) for the 5 this case seeded; "+
|
||||
"the identity below would then be an identity with the wrong set", len(unbounded), unbounded)
|
||||
}
|
||||
if page := readyCounterPageIDs(t, ctx, fixture, request, 2); len(page) != 2 {
|
||||
t.Fatalf("Reader.Ready with Limit=2 returned %d rows (%v), want 2", len(page), page)
|
||||
}
|
||||
|
||||
total := readyCounterTotal(t, ctx, fixture, request)
|
||||
if total != int64(len(unbounded)) {
|
||||
t.Errorf("CountReady = %d, len(Ready(Limit=0).Items) = %d (%v): the total and the page it sizes must describe one set",
|
||||
total, len(unbounded), unbounded)
|
||||
}
|
||||
}
|
||||
|
||||
// RunReadyCounterRejectsLimitAndOffset pins the deterministic
|
||||
// request-validation refusals (issueops/readycounter.go:72-82): a Limit — set
|
||||
// to any value, including an explicit zero — and a non-zero Offset are both
|
||||
// ErrValidation.
|
||||
//
|
||||
// A bounded count would answer "how many of the first N", which is not a
|
||||
// cardinality and would make the identity above false; an offset would
|
||||
// subtract the rows it skipped from the size of a set that still holds them.
|
||||
// The refusal is the promise that neither is quietly dropped.
|
||||
//
|
||||
// The explicit zero is here for the reason the claim contract states it:
|
||||
// ReadyRequest.Limit is a pointer so "unset" and "explicitly unlimited" stay
|
||||
// distinguishable (issueops/reader.go:85-89), and only the first is what this
|
||||
// request permits — an unlimited count is the only kind there is.
|
||||
//
|
||||
// A countable row is seeded first so each refusal runs against a set that
|
||||
// WOULD have answered, which is what makes a refusal a refusal rather than an
|
||||
// empty front reported the hard way.
|
||||
func RunReadyCounterRejectsLimitAndOffset(t *testing.T, ctx context.Context, fixture ReadyCounterFixture) {
|
||||
t.Helper()
|
||||
label := fixture.IssuePrefix + "-rcrej"
|
||||
seedReadyCounterIssue(t, ctx, fixture, readyCounterIssue(fixture.IssuePrefix+"-rcrej-a", 1, label))
|
||||
if got := readyCounterTotal(t, ctx, fixture, publicops.ReadyRequest{
|
||||
Labels: []string{label}, Sort: readyCounterSort,
|
||||
}); got != 1 {
|
||||
t.Fatalf("CountReady = %d for the one row this case seeded, want 1", got)
|
||||
}
|
||||
|
||||
limit := 5
|
||||
unlimited := 0
|
||||
for _, refusal := range []struct {
|
||||
name string
|
||||
request publicops.ReadyRequest
|
||||
}{
|
||||
{"limit", publicops.ReadyRequest{Labels: []string{label}, Sort: readyCounterSort, Limit: &limit}},
|
||||
{"explicitly unlimited limit", publicops.ReadyRequest{Labels: []string{label}, Sort: readyCounterSort, Limit: &unlimited}},
|
||||
{"offset", publicops.ReadyRequest{Labels: []string{label}, Sort: readyCounterSort, Offset: 1}},
|
||||
} {
|
||||
if _, err := fixture.ReadyCounter.CountReady(ctx, refusal.request); !errors.Is(err, storage.ErrValidation) {
|
||||
t.Errorf("CountReady with %s: error = %v, want ErrValidation", refusal.name, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// RunReadyCounterCountsTheBlockerAwareSet pins the sentence that makes this a
|
||||
// role of its own rather than a sixth thing Counter does
|
||||
// (issueops/readycounter.go:26-30): the predicate is BLOCKER-AWARE, so a row
|
||||
// with an open blocker is not in the count even though it is open, unclosed and
|
||||
// otherwise indistinguishable from the row beside it.
|
||||
//
|
||||
// It is asserted against the listing rather than against a literal alone, so
|
||||
// the case says the two agree ON THE SAME SET and not merely that both happen
|
||||
// to be 2 today. The literal is checked as well, because an identity between
|
||||
// two surfaces that both dropped the blocker predicate would still hold.
|
||||
func RunReadyCounterCountsTheBlockerAwareSet(t *testing.T, ctx context.Context, fixture ReadyCounterFixture) {
|
||||
t.Helper()
|
||||
label := fixture.IssuePrefix + "-rcblock"
|
||||
blocker := fixture.IssuePrefix + "-rcblock-blocker"
|
||||
blocked := fixture.IssuePrefix + "-rcblock-blocked"
|
||||
free := fixture.IssuePrefix + "-rcblock-free"
|
||||
for _, id := range []string{blocker, blocked, free} {
|
||||
seedReadyCounterIssue(t, ctx, fixture, readyCounterIssue(id, 1, label))
|
||||
}
|
||||
if err := fixture.AddDependency(ctx, &types.Dependency{
|
||||
IssueID: blocked, DependsOnID: blocker, Type: types.DepBlocks,
|
||||
}, "seed"); err != nil {
|
||||
t.Fatalf("seed the blocking edge: %v", err)
|
||||
}
|
||||
|
||||
request := publicops.ReadyRequest{Labels: []string{label}, Sort: readyCounterSort}
|
||||
listed := readyCounterPageIDs(t, ctx, fixture, request, 0)
|
||||
total := readyCounterTotal(t, ctx, fixture, request)
|
||||
|
||||
if total != 2 {
|
||||
t.Errorf("CountReady = %d over three open rows one of which has an open blocker, want 2", total)
|
||||
}
|
||||
if total != int64(len(listed)) {
|
||||
t.Errorf("CountReady = %d, Reader.Ready listed %d (%v)", total, len(listed), listed)
|
||||
}
|
||||
for _, id := range listed {
|
||||
if id == blocked {
|
||||
t.Errorf("Reader.Ready listed the blocked row %s, so the count above agrees with a listing that is itself wrong", blocked)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// RunReadyCounterEphemeralGateMatchesTheListing pins the wisp half of the
|
||||
// identity (issueops/readycounter.go:68-70): a request that admits ephemeral
|
||||
// rows counts them exactly as the listing lists them, and one that does not
|
||||
// counts neither.
|
||||
//
|
||||
// This is the case aimed at the cheap path specifically. The store-backed body
|
||||
// sizes the ready set as one COUNT(*) per plane MINUS their overlap
|
||||
// (internal/storage/issueops.CountReadyWorkInTx) while the listing merges the
|
||||
// two planes row by row, so the wisp tier is the one place where the count is
|
||||
// assembled by different arithmetic than the page. Both directions of the gate
|
||||
// are asserted because each fails on its own: a body that always merged the
|
||||
// wisp plane would over-count a default request, and one that never merged it
|
||||
// would under-count an opted-in one.
|
||||
func RunReadyCounterEphemeralGateMatchesTheListing(t *testing.T, ctx context.Context, fixture ReadyCounterFixture) {
|
||||
t.Helper()
|
||||
label := fixture.IssuePrefix + "-rcwisp"
|
||||
durable := fixture.IssuePrefix + "-rcwisp-d"
|
||||
wisp := fixture.IssuePrefix + "-rcwisp-w"
|
||||
seedReadyCounterIssue(t, ctx, fixture, readyCounterIssue(durable, 1, label))
|
||||
seedReadyCounterWisp(t, ctx, fixture, readyCounterIssue(wisp, 1, label))
|
||||
|
||||
byDefault := publicops.ReadyRequest{Labels: []string{label}, Sort: readyCounterSort}
|
||||
assertReadyCounterAgreesWithTheListing(t, ctx, fixture, byDefault, 1,
|
||||
"ephemeral rows are outside the default ready set")
|
||||
|
||||
admitted := byDefault
|
||||
admitted.IncludeEphemeral = true
|
||||
assertReadyCounterAgreesWithTheListing(t, ctx, fixture, admitted, 2,
|
||||
"IncludeEphemeral pulls the wisp tier into both surfaces")
|
||||
}
|
||||
|
||||
// RunReadyCounterEmptyFrontIsZeroAndNil pins the whole of this role's
|
||||
// "not found" story (issueops/readycounter.go:84-87): a predicate that matches
|
||||
// nothing is 0 with a NIL ERROR.
|
||||
//
|
||||
// The nil is the load-bearing half. An empty ready front is the steady state of
|
||||
// a drained queue, and a poller that had to classify an error to read a zero
|
||||
// would be pattern-matching prose. The decoy makes the zero about the FILTER
|
||||
// rather than about an empty database: a ready, countable row exists
|
||||
// throughout, it simply does not match.
|
||||
func RunReadyCounterEmptyFrontIsZeroAndNil(t *testing.T, ctx context.Context, fixture ReadyCounterFixture) {
|
||||
t.Helper()
|
||||
label := fixture.IssuePrefix + "-rcempty"
|
||||
decoy := fixture.IssuePrefix + "-rcempty-decoy"
|
||||
seedReadyCounterIssue(t, ctx, fixture, readyCounterIssue(decoy, 0, label+"-other"))
|
||||
|
||||
result, err := fixture.ReadyCounter.CountReady(ctx, publicops.ReadyRequest{
|
||||
Labels: []string{label}, Sort: readyCounterSort,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("CountReady over an empty ready front: error = %v, want nil — an empty front is a normal outcome, not an error", err)
|
||||
}
|
||||
if result.Total != 0 {
|
||||
t.Errorf("CountReady over an empty ready front = %d, want 0", result.Total)
|
||||
}
|
||||
if got := readyCounterTotal(t, ctx, fixture, publicops.ReadyRequest{
|
||||
Labels: []string{label + "-other"}, Sort: readyCounterSort,
|
||||
}); got != 1 {
|
||||
t.Errorf("CountReady for the decoy's own label = %d, want 1: the zero above must be the filter's doing, not an empty database", got)
|
||||
}
|
||||
}
|
||||
|
||||
// RunReadyCounterWritesNothing pins issueops/readycounter.go:45-48: counting is
|
||||
// a read. Nothing records a history entry, and a refused count does not either.
|
||||
//
|
||||
// It is asserted on the history log rather than on a row read-back because that
|
||||
// is the observable both an accidental commit and an accidental write would
|
||||
// move: every versioned unit of work in this tree ends in a Dolt commit, so a
|
||||
// count that took a write transaction would show up here even though it changed
|
||||
// no column. That is not a hypothetical for this role — the unit-of-work body
|
||||
// runs a query the listing runs, and the listing's own transaction wrapper is
|
||||
// two characters away from the writing one.
|
||||
func RunReadyCounterWritesNothing(t *testing.T, ctx context.Context, fixture ReadyCounterFixture) {
|
||||
t.Helper()
|
||||
if fixture.CountHistory == nil {
|
||||
t.Skip("fixture cannot observe history: CountHistory is nil, so the counting-is-a-read clause is unpinned on this backend")
|
||||
}
|
||||
label := fixture.IssuePrefix + "-rcread"
|
||||
seedReadyCounterIssue(t, ctx, fixture, readyCounterIssue(fixture.IssuePrefix+"-rcread-a", 1, label))
|
||||
|
||||
before, err := fixture.CountHistory(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("count history entries: %v", err)
|
||||
}
|
||||
if got := readyCounterTotal(t, ctx, fixture, publicops.ReadyRequest{
|
||||
Labels: []string{label}, Sort: readyCounterSort,
|
||||
}); got != 1 {
|
||||
t.Fatalf("CountReady = %d for the one row this case seeded, want 1", got)
|
||||
}
|
||||
refused := 1
|
||||
if _, err := fixture.ReadyCounter.CountReady(ctx, publicops.ReadyRequest{
|
||||
Labels: []string{label}, Sort: readyCounterSort, Limit: &refused,
|
||||
}); !errors.Is(err, storage.ErrValidation) {
|
||||
t.Fatalf("CountReady with a limit: error = %v, want ErrValidation", err)
|
||||
}
|
||||
|
||||
after, err := fixture.CountHistory(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("count history entries: %v", err)
|
||||
}
|
||||
if after != before {
|
||||
t.Errorf("history entries went %d -> %d across a count and a refusal, want no change: counting is a read", before, after)
|
||||
}
|
||||
}
|
||||
|
||||
// RunReadyCounterDoesNotMutateTheCallerRequest is the request-snapshot
|
||||
// tripwire the leaf promises in so many words (issueops/readycounter.go:39-43).
|
||||
//
|
||||
// The request is passed by value, so only its REFERENCE members can carry a
|
||||
// mutation back: the four label slices, ExcludeTypes, MetadataFields and the
|
||||
// Priority pointer. Every one is populated here with a value normalization
|
||||
// would want to touch — untrimmed, duplicated, comma-joined — and the
|
||||
// comparison is against a second copy built by the same function, so an
|
||||
// in-place trim, dedupe, sort, alias expansion or write-through fails.
|
||||
//
|
||||
// The filter matches nothing on purpose. The promise is about the request, not
|
||||
// about the answer, and an empty front is the one outcome that does not depend
|
||||
// on what the rest of the suite has seeded.
|
||||
func RunReadyCounterDoesNotMutateTheCallerRequest(t *testing.T, ctx context.Context, fixture ReadyCounterFixture) {
|
||||
t.Helper()
|
||||
build := func() publicops.ReadyRequest {
|
||||
priority := 2
|
||||
return publicops.ReadyRequest{
|
||||
IssueType: " Task ",
|
||||
Labels: []string{fixture.IssuePrefix + "-rcsnap ", fixture.IssuePrefix + "-rcsnap "},
|
||||
LabelsAny: []string{" " + fixture.IssuePrefix + "-rcsnap-any"},
|
||||
ExcludeLabels: []string{fixture.IssuePrefix + "-rcsnap-not "},
|
||||
ExcludeTypes: []string{"mr,epic", " chore "},
|
||||
MetadataFields: map[string]string{"team": "conformance"},
|
||||
HasMetadataKey: "team",
|
||||
Priority: &priority,
|
||||
Sort: readyCounterSort,
|
||||
}
|
||||
}
|
||||
request := build()
|
||||
want := build()
|
||||
|
||||
if _, err := fixture.ReadyCounter.CountReady(ctx, request); err != nil {
|
||||
t.Fatalf("CountReady with a fully populated request: %v", err)
|
||||
}
|
||||
if !reflect.DeepEqual(request, want) {
|
||||
t.Errorf("CountReady mutated the caller's request:\n got %+v\nwant %+v", request, want)
|
||||
}
|
||||
}
|
||||
|
||||
// readyCounterSort is the order every case names, for the reason
|
||||
// readyClaimerSort is named: it is the policy whose SQL is a plain "ORDER BY
|
||||
// priority, created_at, id", so no case depends on when the fixture was seeded.
|
||||
// A count has no order of its own — the policy is here because the request has
|
||||
// to be the listing's request for the identity to mean anything.
|
||||
const readyCounterSort = "priority"
|
||||
|
||||
func readyCounterIssue(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,
|
||||
}
|
||||
}
|
||||
|
||||
// readyCounterIDs mints n ids under one case's namespace.
|
||||
func readyCounterIDs(fixture ReadyCounterFixture, name string, n int) []string {
|
||||
ids := make([]string, 0, n)
|
||||
for i := 0; i < n; i++ {
|
||||
ids = append(ids, fmt.Sprintf("%s-%s-%d", fixture.IssuePrefix, name, i))
|
||||
}
|
||||
return ids
|
||||
}
|
||||
|
||||
func seedReadyCounterIssue(t *testing.T, ctx context.Context, fixture ReadyCounterFixture, issue *types.Issue) {
|
||||
t.Helper()
|
||||
if err := fixture.CreateIssue(ctx, issue, "seed"); err != nil {
|
||||
t.Fatalf("seed issue %s: %v", issue.ID, err)
|
||||
}
|
||||
}
|
||||
|
||||
func seedReadyCounterWisp(t *testing.T, ctx context.Context, fixture ReadyCounterFixture, issue *types.Issue) {
|
||||
t.Helper()
|
||||
issue.Ephemeral = true
|
||||
if err := fixture.CreateWisp(ctx, issue, "seed"); err != nil {
|
||||
t.Fatalf("seed wisp %s: %v", issue.ID, err)
|
||||
}
|
||||
}
|
||||
|
||||
// readyCounterTotal runs one count and fails the case on an error, because
|
||||
// every caller here has already established that the request is well-formed.
|
||||
func readyCounterTotal(t *testing.T, ctx context.Context, fixture ReadyCounterFixture, request publicops.ReadyRequest) int64 {
|
||||
t.Helper()
|
||||
result, err := fixture.ReadyCounter.CountReady(ctx, request)
|
||||
if err != nil {
|
||||
t.Fatalf("CountReady(labels=%v, includeEphemeral=%v): %v", request.Labels, request.IncludeEphemeral, err)
|
||||
}
|
||||
return result.Total
|
||||
}
|
||||
|
||||
// readyCounterPageIDs lists the same question at a given limit and returns the
|
||||
// ids, so a failure names the rows rather than two numbers that differ.
|
||||
func readyCounterPageIDs(t *testing.T, ctx context.Context, fixture ReadyCounterFixture, request publicops.ReadyRequest, limit int) []string {
|
||||
t.Helper()
|
||||
paged := request
|
||||
paged.Limit = &limit
|
||||
page, err := fixture.Reader.Ready(ctx, paged)
|
||||
if err != nil {
|
||||
t.Fatalf("Reader.Ready(labels=%v, limit=%d): %v", request.Labels, limit, err)
|
||||
}
|
||||
ids := make([]string, 0, len(page.Items))
|
||||
for _, item := range page.Items {
|
||||
if item != nil && item.Issue != nil {
|
||||
ids = append(ids, item.ID)
|
||||
}
|
||||
}
|
||||
return ids
|
||||
}
|
||||
|
||||
// assertReadyCounterAgreesWithTheListing checks the identity and the literal
|
||||
// together for one request. The literal is what says the two surfaces agree on
|
||||
// the RIGHT set; the identity is what says they agree at all.
|
||||
func assertReadyCounterAgreesWithTheListing(t *testing.T, ctx context.Context, fixture ReadyCounterFixture, request publicops.ReadyRequest, want int64, because string) {
|
||||
t.Helper()
|
||||
listed := readyCounterPageIDs(t, ctx, fixture, request, 0)
|
||||
total := readyCounterTotal(t, ctx, fixture, request)
|
||||
if total != want {
|
||||
t.Errorf("CountReady(includeEphemeral=%v) = %d, want %d: %s", request.IncludeEphemeral, total, want, because)
|
||||
}
|
||||
if total != int64(len(listed)) {
|
||||
t.Errorf("CountReady(includeEphemeral=%v) = %d, Reader.Ready listed %d (%v)",
|
||||
request.IncludeEphemeral, total, len(listed), listed)
|
||||
}
|
||||
}
|
||||
|
|
@ -179,26 +179,20 @@ This is useful for agents executing molecules to see which steps can run next.`,
|
|||
totalReady := len(results)
|
||||
truncated := false
|
||||
if filter.Limit > 0 && len(results) == filter.Limit {
|
||||
// The page is full, so there may be more ready work. Size the true
|
||||
// total N over the same ready predicate, zeroing the limit so the
|
||||
// count is the full ready set (byte-identical to
|
||||
// len(GetReadyWorkWithCounts(Limit=0))). Prefer the cheap COUNT(*)
|
||||
// capability; unwrap past decorators (e.g. HookFiringStore) so the
|
||||
// assertion reaches the concrete store. Fall back to the unbounded
|
||||
// mega-query only when a store predates ReadyWorkCounter.
|
||||
countFilter := filter
|
||||
countFilter.Limit = 0
|
||||
if counter, ok := storage.UnwrapStore(activeStore).(storage.ReadyWorkCounter); ok {
|
||||
if n, countErr := counter.CountReadyWork(ctx, countFilter); countErr == nil && n > len(results) {
|
||||
totalReady = n
|
||||
truncated = true
|
||||
}
|
||||
} else {
|
||||
all, countErr := activeStore.GetReadyWorkWithCounts(ctx, countFilter)
|
||||
if countErr == nil && len(all) > len(results) {
|
||||
totalReady = len(all)
|
||||
truncated = true
|
||||
}
|
||||
// The page is full, so there may be more ready work. The size of
|
||||
// the whole ready set is the ReadyCounter role's question, asked
|
||||
// through the store's own accessor: the role promises its answer
|
||||
// equals len(Reader.Ready(Limit=0).Items), which is what makes
|
||||
// this total describe the page above it.
|
||||
//
|
||||
// It used to be asked here, of the store: unwrap past the
|
||||
// decorators, type-assert to storage.ReadyWorkCounter, and fall
|
||||
// back to the unbounded mega-query when the assertion missed.
|
||||
// That is now the role's business and the assertion is a
|
||||
// compile-time one (internal/workapi/storereadycounter).
|
||||
if n, countErr := readyTotal(ctx, activeStore, in); countErr == nil && n > len(results) {
|
||||
totalReady = n
|
||||
truncated = true
|
||||
}
|
||||
}
|
||||
if results == nil {
|
||||
|
|
@ -231,12 +225,13 @@ This is useful for agents executing molecules to see which steps can run next.`,
|
|||
|
||||
totalReady := len(issues)
|
||||
truncated := false
|
||||
if !jsonOutput && filter.Limit > 0 && len(issues) == filter.Limit {
|
||||
countFilter := filter
|
||||
countFilter.Limit = 0
|
||||
allIssues, countErr := activeStore.GetReadyWork(ctx, countFilter)
|
||||
if countErr == nil && len(allIssues) > len(issues) {
|
||||
totalReady = len(allIssues)
|
||||
if filter.Limit > 0 && len(issues) == filter.Limit {
|
||||
// The same question the --json branch asks, through the same role,
|
||||
// so the "Showing X of N" a human reads and the total a script
|
||||
// parses are one number. This branch used to re-run the whole
|
||||
// unbounded ready query to size N.
|
||||
if n, countErr := readyTotal(ctx, activeStore, in); countErr == nil && n > len(issues) {
|
||||
totalReady = n
|
||||
truncated = true
|
||||
}
|
||||
}
|
||||
|
|
@ -341,6 +336,30 @@ var blockedCmd = &cobra.Command{
|
|||
},
|
||||
}
|
||||
|
||||
// readyTotal sizes the whole ready set for the request `bd ready` just listed
|
||||
// a page of, through the store's own ReadyCounter accessor.
|
||||
//
|
||||
// BOTH OUTPUT MODES CALL IT and only when the page came back full, which is
|
||||
// the one situation where the answer can differ from what is already on
|
||||
// screen. The role has no --max-rows field to honor and needs none: the cap is
|
||||
// a bound on a page this machine will materialize, and a count materializes no
|
||||
// rows — which is also why handleMaxRowsError has nothing to classify here.
|
||||
//
|
||||
// A failed count is not a failed command. The page is already correct; all
|
||||
// that is lost is the "of N" beside it, exactly as it was before this question
|
||||
// became a role.
|
||||
func readyTotal(ctx context.Context, activeStore storage.DoltStorage, in readyInput) (int, error) {
|
||||
counter, err := activeStore.ReadyCounter()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
result, err := counter.CountReady(ctx, readyRoleRequest(in))
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return int(result.Total), nil
|
||||
}
|
||||
|
||||
// buildParentEpicMap builds a map from child issue ID to parent epic title.
|
||||
// Only includes parents that are epics.
|
||||
func buildParentEpicMap(ctx context.Context, s storage.DoltStorage, issues []*types.Issue) map[string]string {
|
||||
|
|
|
|||
|
|
@ -21,6 +21,15 @@ type readyInput struct {
|
|||
|
||||
filter types.WorkFilter
|
||||
|
||||
// dirLabels is the directory-label default (GH#541) this listing applied,
|
||||
// or nil when it did not apply one. It is the gatherer's DECISION rather
|
||||
// than the configured value: whether the default applies depends on
|
||||
// whether the caller named a label, and that question is answerable only
|
||||
// before the default has been written onto the filter. See
|
||||
// readyRoleRequest, which read it back off the filter and got the wrong
|
||||
// answer for exactly that reason.
|
||||
dirLabels []string
|
||||
|
||||
claim bool
|
||||
gated bool
|
||||
molID string
|
||||
|
|
@ -194,6 +203,13 @@ func gatherReadyInput(cmd *cobra.Command, resolveCap func(*cobra.Command) (int,
|
|||
if len(filter.Labels) == 0 && len(filter.LabelsAny) == 0 {
|
||||
if dirLabels := config.GetDirectoryLabels(); len(dirLabels) > 0 {
|
||||
filter.LabelsAny = dirLabels
|
||||
// Recorded rather than recomputed downstream. readyRoleRequest
|
||||
// needs the same answer, and it cannot ask this question again:
|
||||
// the line above has just made the filter's label sets non-empty,
|
||||
// so a second evaluation of the same emptiness test reads its own
|
||||
// output and concludes the default does not apply. It did exactly
|
||||
// that until this field existed — see readyRoleRequest.
|
||||
in.dirLabels = dirLabels
|
||||
}
|
||||
}
|
||||
in.filter = filter
|
||||
|
|
@ -201,35 +217,55 @@ func gatherReadyInput(cmd *cobra.Command, resolveCap func(*cobra.Command) (int,
|
|||
return in, nil
|
||||
}
|
||||
|
||||
// claimNextRequest is `bd ready --claim`'s request to the ReadyClaimer role.
|
||||
// Both routes build it here, so the claim asks one question no matter which
|
||||
// door it came through — and it is the SAME issueops.ReadyRequest the listing
|
||||
// builds its filter from, so the claim's predicate cannot drift from the set
|
||||
// `bd ready` shows.
|
||||
// readyRoleRequest is the request `bd ready` hands the two roles that take a
|
||||
// whole ready question rather than a filter: ReadyClaimer, which claims one
|
||||
// row out of it, and ReadyCounter, which sizes it. Both routes of both
|
||||
// operations build it here, so a claim, a count and the listing beside them
|
||||
// ask ONE question no matter which door they came through — it is the same
|
||||
// issueops.ReadyRequest the listing builds its filter from.
|
||||
//
|
||||
// The two things the filter carries that the request does not:
|
||||
//
|
||||
// - The --max-rows cap. It never applied to a claim in the first place:
|
||||
// ClaimReadyIssueInTx clears MaxRows, MaxRowsSource and Limit before it
|
||||
// scans, because a cap sized for bulk reads must not make a single-row
|
||||
// claim report an empty front. Dropping it here is visible only in that
|
||||
// the role has no field to ignore.
|
||||
// - The directory-label default. The listing puts it on the built filter
|
||||
// verbatim; here it goes on the request, where it is normalized along with
|
||||
// every other label. That differs only for a directory.labels value
|
||||
// carrying stray whitespace. The emptiness test is unchanged — read off
|
||||
// the already-normalized filter, so `--label " "` is no label at all and
|
||||
// still does not suppress the default.
|
||||
func claimNextRequest(in readyInput) issueops.ClaimNextRequest {
|
||||
// - The --max-rows cap. It never applied to either role. A claim consumes
|
||||
// one row however large the pool it scanned (ClaimReadyIssueInTx clears
|
||||
// MaxRows, MaxRowsSource and Limit before it scans, because a cap sized
|
||||
// for bulk reads must not make a single-row claim report an empty front),
|
||||
// and a count materializes no rows at all. Dropping it here is visible
|
||||
// only in that neither role has a field to ignore.
|
||||
//
|
||||
// - The directory-label default (GH#541). The listing puts it on the built
|
||||
// filter verbatim; here it goes on the request, where it is normalized
|
||||
// along with every other label. That differs only for a directory.labels
|
||||
// value carrying stray whitespace.
|
||||
//
|
||||
// IT IS READ OFF readyInput.dirLabels, WHICH IS THE FIX. This used to
|
||||
// re-run the gatherer's own emptiness test against in.filter — and by
|
||||
// then the gatherer had already written the default INTO that filter, so
|
||||
// the test saw a non-empty label set and skipped. The branch could
|
||||
// therefore never fire: with no directory label configured there was
|
||||
// nothing to apply, and with one configured the filter it checked was the
|
||||
// one carrying it. `bd ready --claim` in a scoped directory consequently
|
||||
// claimed from the WHOLE ready front while `bd ready` beside it listed
|
||||
// only the configured scope, which is the one thing ReadyClaimer's
|
||||
// contract says a claim must never do (issueops/readyclaimer.go:9-14).
|
||||
// Recording the decision where it is made leaves one answer instead of
|
||||
// two derivations of it.
|
||||
//
|
||||
// Limit and Offset are ErrValidation on BOTH roles, so they are cleared here
|
||||
// once. Neither was ever live for the claim (--offset with --claim is already
|
||||
// a usage error above and the claim scan zeroes the limit itself); for the
|
||||
// count they are exactly what has to go — the page is the thing being sized.
|
||||
func readyRoleRequest(in readyInput) issueops.ReadyRequest {
|
||||
req := in.ReadyRequest
|
||||
// Limit and Offset are ErrValidation on a claim. Neither was ever live
|
||||
// here: --offset with --claim is already a usage error above, and the
|
||||
// claim scan zeroes the limit itself.
|
||||
req.Limit, req.Offset = nil, 0
|
||||
if len(in.filter.Labels) == 0 && len(in.filter.LabelsAny) == 0 {
|
||||
if dirLabels := config.GetDirectoryLabels(); len(dirLabels) > 0 {
|
||||
req.LabelsAny = dirLabels
|
||||
}
|
||||
if len(in.dirLabels) > 0 {
|
||||
req.LabelsAny = in.dirLabels
|
||||
}
|
||||
return issueops.ClaimNextRequest{Actor: actor, Filter: req}
|
||||
return req
|
||||
}
|
||||
|
||||
// claimNextRequest is `bd ready --claim`'s request to the ReadyClaimer role:
|
||||
// the shared ready question above, plus the claimant.
|
||||
func claimNextRequest(in readyInput) issueops.ClaimNextRequest {
|
||||
return issueops.ClaimNextRequest{Actor: actor, Filter: readyRoleRequest(in)}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -228,10 +228,14 @@ func TestProxiedServerReady(t *testing.T) {
|
|||
if envelope.Pagination.Returned != 2 {
|
||||
t.Errorf("pagination.returned = %d, want 2", envelope.Pagination.Returned)
|
||||
}
|
||||
// Total is unavailable on the proxied backend; omitempty must drop
|
||||
// the key rather than emit a false "total": 0.
|
||||
if strings.Contains(s[start:], `"total"`) {
|
||||
t.Errorf("unexpected 'total' key in proxied pagination (unavailable on this backend): %s", s[start:])
|
||||
// Total is PUBLISHED here, which it was not before the ReadyCounter
|
||||
// role: this route had no way to size the ready set, so the direct
|
||||
// route's "total" simply had no proxied counterpart and a script that
|
||||
// read it got nothing under a proxied workspace. Four rows were seeded
|
||||
// and two returned.
|
||||
if envelope.Pagination.Total != 4 {
|
||||
t.Errorf("pagination.total = %d, want the 4 rows this case seeded: %s",
|
||||
envelope.Pagination.Total, s[start:])
|
||||
}
|
||||
})
|
||||
|
||||
|
|
|
|||
|
|
@ -123,15 +123,27 @@ func runReadyProxiedList(ctx context.Context, uw uow.UnitOfWork, in readyInput)
|
|||
results, truncated := workapi.FinishPage(page.Items, "", false, in.filter.Limit, page.HasMore)
|
||||
truncated = truncated && in.filter.Limit > 0
|
||||
// Parity with the direct route: the pagination key is emitted only
|
||||
// when truncated. Total is unavailable from this backend's domain
|
||||
// page (no cheap COUNT(*) equivalent on the proxied path), so it is
|
||||
// left unset (0) unlike the direct route's fully-populated meta.
|
||||
// when truncated, and it now carries the same Total, from the same
|
||||
// role. This route published no total at all until ReadyCounter
|
||||
// existed — the domain page reports has-more and nothing else, and
|
||||
// there is no cheap COUNT(*) on this seam — so a script that read
|
||||
// `pagination.total` got one number under a direct-mode workspace and
|
||||
// no key at all under a proxied one.
|
||||
//
|
||||
// The guard is the direct route's guard: a total that does not exceed
|
||||
// the page says nothing this envelope does not already say, and the
|
||||
// two queries are not one snapshot (issueops.ReadyCounter.CountReady),
|
||||
// so a close landing between them must not publish a total smaller
|
||||
// than the page beside it.
|
||||
var pag *PaginationMeta
|
||||
if truncated {
|
||||
pag = &PaginationMeta{
|
||||
Returned: len(results),
|
||||
Truncated: true,
|
||||
}
|
||||
if n, countErr := proxiedReadyTotal(ctx, in); countErr == nil && n > len(results) {
|
||||
pag.Total = n
|
||||
}
|
||||
}
|
||||
_ = outputJSONWithPagination(results, pag)
|
||||
if truncated {
|
||||
|
|
@ -227,6 +239,33 @@ func runReadyProxiedClaim(ctx context.Context, in readyInput) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// proxiedReadyTotal sizes the whole ready set through the provider's own
|
||||
// ReadyCounter accessor — the same role the direct route reaches through the
|
||||
// store's accessor, over the request both build in readyRoleRequest.
|
||||
//
|
||||
// It opens NO unit of work of its own, and the caller's is not handed to it:
|
||||
// the role's request is the transaction, exactly as it is for the claim beside
|
||||
// it. So the count runs in its own read-only unit of work rather than the one
|
||||
// the page came from, which is why the two are not one snapshot.
|
||||
func proxiedReadyTotal(ctx context.Context, in readyInput) (int, error) {
|
||||
if uowProvider == nil {
|
||||
return 0, errors.New("proxied-server UOW provider not initialized")
|
||||
}
|
||||
src, ok := uowProvider.(uow.ReadyCounterSource)
|
||||
if !ok {
|
||||
return 0, fmt.Errorf("proxied-server provider %T does not offer the ready-count surface", uowProvider)
|
||||
}
|
||||
counter, err := src.ReadyCounter()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
result, err := counter.CountReady(ctx, readyRoleRequest(in))
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return int(result.Total), nil
|
||||
}
|
||||
|
||||
// proxiedReadyClaimer hands back the guarded claim surface for the
|
||||
// proxied-server provider, through the provider's OWN capability accessor —
|
||||
// the same two-step proxiedIssueReader performs, and for the same reason: the
|
||||
|
|
|
|||
95
cmd/bd/ready_role_request_test.go
Normal file
95
cmd/bd/ready_role_request_test.go
Normal file
|
|
@ -0,0 +1,95 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"slices"
|
||||
"testing"
|
||||
|
||||
"github.com/steveyegge/beads/internal/workapi"
|
||||
)
|
||||
|
||||
// TestReadyRoleRequestDropsThePage pins what both role-taking questions of
|
||||
// `bd ready` share: the claim and the count are asked over the LISTING's
|
||||
// request with the page removed.
|
||||
//
|
||||
// Both roles refuse a Limit and an Offset (ErrValidation), so a request that
|
||||
// carried either would fail at the seam rather than silently ignore it — and
|
||||
// for the count, which is published as "showing 2 of N", a limit that survived
|
||||
// would make N the size of the page instead of the size of the set.
|
||||
func TestReadyRoleRequestDropsThePage(t *testing.T) {
|
||||
got := runGatherReadyInput(t, newReadyFlagsCommand(t, "--limit", "5", "--label", "api"), nil)
|
||||
if got.err != nil {
|
||||
t.Fatalf("gatherReadyInput: %v", got.err)
|
||||
}
|
||||
// The listing itself is bounded, which is what makes the drop observable.
|
||||
if got.in.Limit == nil || *got.in.Limit != 5 {
|
||||
t.Fatalf("the listing request carries limit %v, want 5: the drop below would prove nothing", got.in.Limit)
|
||||
}
|
||||
|
||||
req := readyRoleRequest(got.in)
|
||||
if req.Limit != nil {
|
||||
t.Errorf("role request carries limit %d; both roles refuse one", *req.Limit)
|
||||
}
|
||||
if req.Offset != 0 {
|
||||
t.Errorf("role request carries offset %d; both roles refuse one", req.Offset)
|
||||
}
|
||||
if !slices.Equal(req.Labels, []string{"api"}) {
|
||||
t.Errorf("role request Labels = %q, want the listing's own %q", req.Labels, []string{"api"})
|
||||
}
|
||||
|
||||
// The claim asks the same question, plus the claimant: one builder, so the
|
||||
// count and the claim cannot drift apart from each other either.
|
||||
claim := claimNextRequest(got.in)
|
||||
if !reflect.DeepEqual(claim.Filter, req) {
|
||||
t.Errorf("the claim's filter and the shared role request differ:\nclaim: %+v\nrole: %+v", claim.Filter, req)
|
||||
}
|
||||
|
||||
// And the predicate the count actually runs is the listing's predicate,
|
||||
// unbounded — the identity issueops.ReadyCounter promises, checked here at
|
||||
// the front door rather than only at the three backends.
|
||||
counted, err := workapi.BuildReadyCountFilter(req)
|
||||
if err != nil {
|
||||
t.Fatalf("BuildReadyCountFilter: %v", err)
|
||||
}
|
||||
unlimited := 0
|
||||
listing := got.in.ReadyRequest
|
||||
listing.Limit = &unlimited
|
||||
want, err := workapi.BuildReadyFilter(listing)
|
||||
if err != nil {
|
||||
t.Fatalf("BuildReadyFilter: %v", err)
|
||||
}
|
||||
if !reflect.DeepEqual(counted, want) {
|
||||
t.Errorf("the count's filter and the unbounded listing filter differ:\ncount: %+v\nlisting: %+v", counted, want)
|
||||
}
|
||||
}
|
||||
|
||||
// TestReadyRoleRequestCarriesTheDirectoryLabelDefault pins the one thing the
|
||||
// role request adds that the raw flags do not: GH#541's configured label,
|
||||
// applied under the same gate the listing applies it under.
|
||||
//
|
||||
// It goes on the REQUEST here rather than on the built filter, so it is
|
||||
// normalized with every other label — which is exactly why it is asserted:
|
||||
// a count or a claim that missed the default would answer for a wider set than
|
||||
// the listing beside it shows.
|
||||
func TestReadyRoleRequestCarriesTheDirectoryLabelDefault(t *testing.T) {
|
||||
const configured = "scope:web"
|
||||
configureDirectoryLabel(t, configured)
|
||||
|
||||
got := runGatherReadyInput(t, newReadyFlagsCommand(t), nil)
|
||||
if got.err != nil {
|
||||
t.Fatalf("gatherReadyInput: %v", got.err)
|
||||
}
|
||||
if req := readyRoleRequest(got.in); !slices.Equal(req.LabelsAny, []string{configured}) {
|
||||
t.Errorf("role request LabelsAny = %q, want the configured %q", req.LabelsAny, []string{configured})
|
||||
}
|
||||
|
||||
// An explicit label suppresses it, the same way it suppresses the
|
||||
// listing's.
|
||||
explicit := runGatherReadyInput(t, newReadyFlagsCommand(t, "--label", "chosen"), nil)
|
||||
if explicit.err != nil {
|
||||
t.Fatalf("gatherReadyInput: %v", explicit.err)
|
||||
}
|
||||
if req := readyRoleRequest(explicit.in); len(req.LabelsAny) != 0 {
|
||||
t.Errorf("role request LabelsAny = %q with an explicit --label, want none", req.LabelsAny)
|
||||
}
|
||||
}
|
||||
|
|
@ -31,7 +31,8 @@ APPENDING later, not being born whole.
|
|||
|
||||
## The checklist
|
||||
|
||||
Twelve steps. Steps 1-9 are the role; 10-12 are what makes it the ONLY way in.
|
||||
Thirteen steps. Steps 1-9 are the role; 10-12 are what makes it the ONLY way
|
||||
in; 13 is the HTTP surface, which lands with the command rather than after it.
|
||||
|
||||
1. **The leaf contract.** `issueops/<role>.go`. Request and result types plus
|
||||
the interface, with the doc comment written as a SPECIFICATION — every
|
||||
|
|
@ -154,6 +155,23 @@ Twelve steps. Steps 1-9 are the role; 10-12 are what makes it the ONLY way in.
|
|||
number). Removing an entry there is how a role commit proves its front door
|
||||
actually reached the role.
|
||||
|
||||
13. **The HTTP half, when the role has one.** The spec is written FIRST —
|
||||
`internal/httpapi/spec/openapi.v0.yaml`, then `make api-gen` — and the
|
||||
handler is written against the generated types, never the other way round.
|
||||
An operation costs four more edits beside the handler: the `Op*` id and the
|
||||
`operationCodes` row in `problem.go`, the `routeTable` row and its
|
||||
`capability` token in `routes.go`, and that token in the document's
|
||||
`capabilities` vocabulary. `make api-check` is the gate.
|
||||
|
||||
**The ROLES database source is the part that surprises.** `httpapi.Config`
|
||||
carries either a unit-of-work provider or the roles, and the roles are
|
||||
required TOGETHER: a new operation served from a new role adds a required
|
||||
field, so `checkDatabaseSource` grows a term and every caller and test that
|
||||
built a Config grows a line. That is deliberate — a Config missing the new
|
||||
role would otherwise bind and nil-dereference on the first request that
|
||||
reached the new handler — but budget it, because it is the one edit in this
|
||||
list that touches code the role commit did not write.
|
||||
|
||||
## What the whole thing costs
|
||||
|
||||
`bd count` behind `issueops.Counter`, end to end, in one commit:
|
||||
|
|
|
|||
|
|
@ -81,7 +81,7 @@ type ContextResponse struct {
|
|||
// BeadsDir Absolute path of the served workspace's `.beads` directory. A host path, kept because it is the single-workspace server's only workspace-identity handshake; disclosing it to network peers is part of what an operator accepts when binding beyond loopback.
|
||||
BeadsDir string `json:"beads_dir"`
|
||||
|
||||
// Capabilities The operations this server actually implements, derived from its route table. v0's vocabulary is `ready.list`, `issues.list`, `issues.get`, `issues.claim`, `stats.get`, `config.list`, `config.get`, `dependencies.cycles`, `dependencies.list`; it grows additively, and an operation never appears here unless it is fully implemented. This is how a client checks for an operation — never the version string.
|
||||
// Capabilities The operations this server actually implements, derived from its route table. v0's vocabulary is `ready.list`, `ready.count`, `issues.list`, `issues.get`, `issues.claim`, `stats.get`, `config.list`, `config.get`, `dependencies.cycles`, `dependencies.list`; it grows additively, and an operation never appears here unless it is fully implemented. This is how a client checks for an operation — never the version string.
|
||||
Capabilities []string `json:"capabilities"`
|
||||
|
||||
// Database Logical database name (not a host or a DSN).
|
||||
|
|
@ -201,6 +201,14 @@ type Problem struct {
|
|||
Type *string `json:"type,omitempty"`
|
||||
}
|
||||
|
||||
// ReadyCount The size of a ready set. It carries no items, no `has_more` and no cursor: this is a number about a set, and the operation that returns rows is `GET /v0/beads/ready`.
|
||||
//
|
||||
// It is NOT `x-go-type`-pinned, unlike the seven schemas above, and that is a decision rather than an omission: those seven are pinned because a canonical Go struct's JSON encoding IS the contract and a second wire struct would let the CLI's `--json` drift from these bodies. There is no canonical struct here — the CLI publishes this number inside its own stdout envelope's `pagination` member, which is not a wire type — so pinning would weld this body to a CLI presentation type instead of preventing a drift.
|
||||
type ReadyCount struct {
|
||||
// Total How many items `GET /v0/beads/ready` would return for these filters with `limit=0`. Never negative; 0 when nothing is ready, which is a 200 rather than a 404 — a question about a set has an answer even when the set is empty.
|
||||
Total int64 `json:"total"`
|
||||
}
|
||||
|
||||
// ReadyPage defines model for ReadyPage.
|
||||
type ReadyPage struct {
|
||||
// HasMore True when the limit truncated the result. There is no cursor on this operation; narrow the filters or raise the limit.
|
||||
|
|
@ -426,6 +434,54 @@ type ListReadyWorkParams struct {
|
|||
// ListReadyWorkParamsSort defines parameters for ListReadyWork.
|
||||
type ListReadyWorkParamsSort string
|
||||
|
||||
// CountReadyWorkParams defines parameters for CountReadyWork.
|
||||
type CountReadyWorkParams struct {
|
||||
// Assignee Only issues assigned to this actor.
|
||||
Assignee *string `form:"assignee,omitempty" json:"assignee,omitempty"`
|
||||
|
||||
// Unassigned Only issues with no assignee.
|
||||
Unassigned *bool `form:"unassigned,omitempty" json:"unassigned,omitempty"`
|
||||
|
||||
// Type Issue type, with the shorthand alias expansion and the match-nothing-rather-than-fail treatment `GET /v0/beads/ready` documents. Setting it drops the default type exclusions and makes `exclude_type` ignored, there and here alike.
|
||||
Type *string `form:"type,omitempty" json:"type,omitempty"`
|
||||
|
||||
// ExcludeType Issue types to exclude. Repeat the parameter, or pass a comma-separated list. Ignored when `type` is set.
|
||||
ExcludeType *[]string `form:"exclude_type,omitempty" json:"exclude_type,omitempty"`
|
||||
|
||||
// Label Labels that must ALL be present (AND).
|
||||
Label *[]string `form:"label,omitempty" json:"label,omitempty"`
|
||||
|
||||
// LabelAny Labels of which at least one must be present (OR).
|
||||
LabelAny *[]string `form:"label_any,omitempty" json:"label_any,omitempty"`
|
||||
|
||||
// ExcludeLabel Labels that must not be present.
|
||||
ExcludeLabel *[]string `form:"exclude_label,omitempty" json:"exclude_label,omitempty"`
|
||||
|
||||
// LabelPattern Glob matched against labels.
|
||||
LabelPattern *string `form:"label_pattern,omitempty" json:"label_pattern,omitempty"`
|
||||
|
||||
// LabelRegex Regular expression matched against labels.
|
||||
LabelRegex *string `form:"label_regex,omitempty" json:"label_regex,omitempty"`
|
||||
|
||||
// Priority Exact priority (0 is a real value, not "unset").
|
||||
Priority *int `form:"priority,omitempty" json:"priority,omitempty"`
|
||||
|
||||
// Parent Restrict to recursive descendants of this issue.
|
||||
Parent *string `form:"parent,omitempty" json:"parent,omitempty"`
|
||||
|
||||
// MetadataField Top-level metadata equality filter as `key=value`, split on the first `=`. Repeatable. An invalid key is a 400.
|
||||
MetadataField *[]string `form:"metadata_field,omitempty" json:"metadata_field,omitempty"`
|
||||
|
||||
// HasMetadataKey Only issues carrying this top-level metadata key.
|
||||
HasMetadataKey *string `form:"has_metadata_key,omitempty" json:"has_metadata_key,omitempty"`
|
||||
|
||||
// IncludeEphemeral Include ephemeral (non-synced) rows, which the count merges exactly as the listing lists them.
|
||||
IncludeEphemeral *bool `form:"include_ephemeral,omitempty" json:"include_ephemeral,omitempty"`
|
||||
|
||||
// IncludeDeferred Include issues whose `defer_until` is still in the future.
|
||||
IncludeDeferred *bool `form:"include_deferred,omitempty" json:"include_deferred,omitempty"`
|
||||
}
|
||||
|
||||
// GetStatsParams defines parameters for GetStats.
|
||||
type GetStatsParams struct {
|
||||
// Assignee Answer for ONE ACTOR instead of the workspace. Used as written: no trimming, no case folding, no alias expansion. An empty value is a 400 — the workspace-wide question is asked by OMITTING the parameter, and an empty assignee would otherwise select the rows with no assignee and report them as one actor's workload.
|
||||
|
|
|
|||
|
|
@ -360,6 +360,9 @@ var (
|
|||
_ uow.IssueReaderSource = timedProvider{}
|
||||
_ uow.IssueClaimerSource = timedProvider{}
|
||||
_ uow.EdgeReaderSource = timedProvider{}
|
||||
_ uow.IssueReaderSource = timedProvider{}
|
||||
_ uow.IssueClaimerSource = timedProvider{}
|
||||
_ uow.ReadyCounterSource = timedProvider{}
|
||||
)
|
||||
|
||||
// IssueReader builds the reader OVER THIS WRAPPER rather than delegating to the
|
||||
|
|
@ -425,6 +428,13 @@ func (p timedProvider) EdgeReader() (issueops.EdgeReader, error) {
|
|||
return uow.NewEdgeReader(p)
|
||||
}
|
||||
|
||||
// ReadyCounter builds the ready counter OVER THIS WRAPPER, for the same reason
|
||||
// as the two above: the count opens a unit of work of its own, and it must land
|
||||
// in this request's uow_ms rather than in nobody's.
|
||||
func (p timedProvider) ReadyCounter() (issueops.ReadyCounter, error) {
|
||||
return uow.NewReadyCounter(p)
|
||||
}
|
||||
|
||||
func (p timedProvider) NewUOW(ctx context.Context) (uow.UnitOfWork, error) {
|
||||
start := time.Now()
|
||||
uw, err := p.inner.NewUOW(ctx)
|
||||
|
|
|
|||
|
|
@ -37,6 +37,7 @@ import (
|
|||
// status+code pair breaks the wire. Adding one does not, which is why clients
|
||||
// are told to default-branch on unknown codes within a status class. Keep it
|
||||
// at what the seven v0 operations actually need.
|
||||
// at what the v0 operations actually need.
|
||||
type Code string
|
||||
|
||||
// The v0 code vocabulary. Every value here is documented in the spec, and
|
||||
|
|
@ -158,6 +159,7 @@ const (
|
|||
// member because it answers per named issue, reports the ids that named
|
||||
// nothing, and returns edges whose target this database holds no row for.
|
||||
OpListDependencies = "listDependencies"
|
||||
OpCountReadyWork = "countReadyWork"
|
||||
)
|
||||
|
||||
// operationCodes is the per-operation problem vocabulary: exactly the codes
|
||||
|
|
@ -173,6 +175,8 @@ const (
|
|||
// documents them once at the document level rather than repeating them on all
|
||||
// nine operations; these rows carry what an operation produces beyond them.
|
||||
// seven operations; these rows carry what an operation produces beyond them.
|
||||
// documents them once at the document level rather than repeating them on
|
||||
// every operation; these rows carry what an operation produces beyond them.
|
||||
// Keep the two documents in step: a row here and the document-level prose are
|
||||
// the only two places that carve-out exists.
|
||||
var operationCodes = map[string][]Code{
|
||||
|
|
@ -205,6 +209,11 @@ var operationCodes = map[string][]Code{
|
|||
// the response's `missing` member, so a batch keeps the answers for the ids
|
||||
// that were found. A 404 would discard them.
|
||||
OpListDependencies: {CodeInvalidArgument, CodeBusy, CodeDBUnavailable, CodeInternal},
|
||||
// The same vocabulary as the listing it sizes: it takes the same filters
|
||||
// and can refuse them the same way. limit=0's mode-dependent refusal has no
|
||||
// analog here because there is no limit to pass — but that was a
|
||||
// CodeInvalidArgument too, so this row would not change if there were.
|
||||
OpCountReadyWork: {CodeInvalidArgument, CodeBusy, CodeDBUnavailable, CodeInternal},
|
||||
OpClaimIssue: {
|
||||
CodeInvalidArgument, CodeNotFound, CodeAlreadyClaimed, CodeNotClaimable,
|
||||
CodeBusy, CodeDBUnavailable, CodeInternal,
|
||||
|
|
|
|||
|
|
@ -50,38 +50,17 @@ import (
|
|||
func (s *Server) handleReady(w http.ResponseWriter, r *http.Request) {
|
||||
q := newQuery(r.URL.Query())
|
||||
|
||||
req := issueops.ReadyRequest{
|
||||
Assignee: q.str("assignee"),
|
||||
Unassigned: q.boolean("unassigned"),
|
||||
IssueType: q.str("type"),
|
||||
ExcludeTypes: q.list("exclude_type"),
|
||||
req := readyFilters(q)
|
||||
|
||||
Labels: q.list("label"),
|
||||
LabelsAny: q.list("label_any"),
|
||||
ExcludeLabels: q.list("exclude_label"),
|
||||
LabelPattern: q.str("label_pattern"),
|
||||
LabelRegex: q.str("label_regex"),
|
||||
|
||||
Priority: q.integer("priority"),
|
||||
ParentID: q.str("parent"),
|
||||
|
||||
MetadataFields: q.metadataFields("metadata_field"),
|
||||
HasMetadataKey: q.str("has_metadata_key"),
|
||||
|
||||
IncludeEphemeral: q.boolean("include_ephemeral"),
|
||||
IncludeDeferred: q.boolean("include_deferred"),
|
||||
|
||||
// EXPLICIT, always. The storage layer maps an EMPTY sort policy to
|
||||
// hybrid, and forwarding an absent `sort` as "" would silently adopt
|
||||
// that fallback while the document still read `default: priority`.
|
||||
// It is not a cosmetic difference: hybrid demotes older high-priority
|
||||
// work, so the item SET changes as soon as `limit` truncates — and
|
||||
// only for the clients this API exists to migrate off `bd ready`,
|
||||
// whose flag registers a concrete default and never sends empty.
|
||||
Sort: q.oneOf("sort", readySortDefault, "hybrid", "priority", "oldest"),
|
||||
|
||||
Limit: q.limit(),
|
||||
}
|
||||
// EXPLICIT, always. The storage layer maps an EMPTY sort policy to hybrid,
|
||||
// and forwarding an absent `sort` as "" would silently adopt that fallback
|
||||
// while the document still read `default: priority`. It is not a cosmetic
|
||||
// difference: hybrid demotes older high-priority work, so the item SET
|
||||
// changes as soon as `limit` truncates — and only for the clients this API
|
||||
// exists to migrate off `bd ready`, whose flag registers a concrete default
|
||||
// and never sends empty.
|
||||
req.Sort = q.oneOf("sort", readySortDefault, "hybrid", "priority", "oldest")
|
||||
req.Limit = q.limit()
|
||||
|
||||
if !s.acceptQuery(w, r, q) {
|
||||
return
|
||||
|
|
@ -106,6 +85,78 @@ func (s *Server) handleReady(w http.ResponseWriter, r *http.Request) {
|
|||
})
|
||||
}
|
||||
|
||||
// readyFilters decodes the ready-work filter vocabulary the two ready
|
||||
// operations share — everything except the PAGE, which only the listing has,
|
||||
// and the ORDER, which only the listing needs.
|
||||
//
|
||||
// It is one function because the two operations must admit exactly the same
|
||||
// filters: countReadyWork answers with the size of the set listReadyWork
|
||||
// returns, and a parameter one of them decoded and the other did not would
|
||||
// make that identity false for any client that sent it. Each parameter this
|
||||
// reads is also, by construction, one the operation accepts — the unknown-
|
||||
// parameter allowlist IS the set of names the handler asked for — so the two
|
||||
// parameter tables cannot drift apart either.
|
||||
func readyFilters(q *query) issueops.ReadyRequest {
|
||||
return issueops.ReadyRequest{
|
||||
Assignee: q.str("assignee"),
|
||||
Unassigned: q.boolean("unassigned"),
|
||||
IssueType: q.str("type"),
|
||||
ExcludeTypes: q.list("exclude_type"),
|
||||
|
||||
Labels: q.list("label"),
|
||||
LabelsAny: q.list("label_any"),
|
||||
ExcludeLabels: q.list("exclude_label"),
|
||||
LabelPattern: q.str("label_pattern"),
|
||||
LabelRegex: q.str("label_regex"),
|
||||
|
||||
Priority: q.integer("priority"),
|
||||
ParentID: q.str("parent"),
|
||||
|
||||
MetadataFields: q.metadataFields("metadata_field"),
|
||||
HasMetadataKey: q.str("has_metadata_key"),
|
||||
|
||||
IncludeEphemeral: q.boolean("include_ephemeral"),
|
||||
IncludeDeferred: q.boolean("include_deferred"),
|
||||
}
|
||||
}
|
||||
|
||||
// handleCountReady answers GET /v0/beads/ready:count.
|
||||
//
|
||||
// THE REQUEST IS THE LISTING'S REQUEST with the page taken off, which is what
|
||||
// makes the answer the size of the page the listing would return. It is not
|
||||
// assembled here beyond that: the role refuses a Limit and an Offset itself
|
||||
// (issueops.ReadyCounter.CountReady), so this handler could not smuggle a
|
||||
// bounded count past it even if a parameter for one existed.
|
||||
//
|
||||
// THE SORT IS SENT ANYWAY, and the operation publishes no parameter for it. A
|
||||
// count has no order, so there is nothing for a client to choose; but the
|
||||
// request still has to be a request the builder accepts, and sending the
|
||||
// listing's own default is how this stays the listing's question rather than a
|
||||
// second one that merely resembles it. Sending "" would adopt the storage
|
||||
// layer's hybrid fallback, which the document says no front door relies on.
|
||||
func (s *Server) handleCountReady(w http.ResponseWriter, r *http.Request) {
|
||||
q := newQuery(r.URL.Query())
|
||||
|
||||
req := readyFilters(q)
|
||||
req.Sort = readySortDefault
|
||||
|
||||
if !s.acceptQuery(w, r, q) {
|
||||
return
|
||||
}
|
||||
|
||||
counter, err := s.readyCounter(r)
|
||||
if err != nil {
|
||||
s.failErr(w, r, err)
|
||||
return
|
||||
}
|
||||
result, err := counter.CountReady(r.Context(), req)
|
||||
if err != nil {
|
||||
s.failReadErr(w, r, err)
|
||||
return
|
||||
}
|
||||
writeJSON(w, apigen.ReadyCount{Total: result.Total})
|
||||
}
|
||||
|
||||
// readySortDefault is the ordering this operation applies when `sort` is
|
||||
// absent. It is the same value `bd ready --sort` registers.
|
||||
//
|
||||
|
|
|
|||
112
internal/httpapi/ready_count_test.go
Normal file
112
internal/httpapi/ready_count_test.go
Normal file
|
|
@ -0,0 +1,112 @@
|
|||
package httpapi
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/steveyegge/beads/internal/types"
|
||||
)
|
||||
|
||||
// The provider-path pins for GET /v0/beads/ready:count. The roles-path twin
|
||||
// lives in roles_test.go, beside the other store-shaped-source cases.
|
||||
|
||||
// TestCountReadyRunsTheListingsQueryUnbounded asserts the property the
|
||||
// operation exists to deliver rather than the number it happened to return: the
|
||||
// count runs the SAME ready query the listing runs, with the page taken off.
|
||||
//
|
||||
// The recorded filter is the only place that is visible. A body carrying the
|
||||
// right total says nothing about which set was counted — every wrong predicate
|
||||
// also produces a number — so the assertions are on the filter: the label the
|
||||
// wire named, the listing's own sort policy, and Limit 0. Limit is the one that
|
||||
// would silently break the operation's promise, because the shared builder
|
||||
// defaults an UNSET limit to workapi.DefaultReadyLimit: a count assembled
|
||||
// without the role would answer "how many of the first 100" and be published as
|
||||
// a total.
|
||||
func TestCountReadyRunsTheListingsQueryUnbounded(t *testing.T) {
|
||||
rec := &recordingIssues{items: countedPage()}
|
||||
ts := newTestServer(t, Config{Provider: &fakeProvider{
|
||||
issues: &fakeIssues{},
|
||||
readIssues: rec,
|
||||
readConfig: emptyConfig{},
|
||||
}})
|
||||
|
||||
resp := ts.get(t, "/v0/beads/ready:count?label=api")
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
t.Fatalf("status = %d, want 200: %s", resp.StatusCode, readAll(t, resp))
|
||||
}
|
||||
if body := decodeBody(t, resp); body["total"] != float64(len(countedPage())) {
|
||||
t.Errorf("total = %v, want %d — the size of the set the reader answered with", body["total"], len(countedPage()))
|
||||
}
|
||||
|
||||
filters := rec.readyFilters()
|
||||
if len(filters) != 1 {
|
||||
t.Fatalf("%d ready queries, want 1", len(filters))
|
||||
}
|
||||
if got := filters[0].Limit; got != 0 {
|
||||
t.Errorf("Limit = %d, want 0: a bounded count answers \"how many of the first N\" and would be read as a total", got)
|
||||
}
|
||||
if got := filters[0].Offset; got != 0 {
|
||||
t.Errorf("Offset = %d, want 0", got)
|
||||
}
|
||||
if got := filters[0].SortPolicy; got != types.SortPolicy("priority") {
|
||||
t.Errorf("SortPolicy = %q, want the listing's own default: an empty policy is the storage layer's hybrid fallback", got)
|
||||
}
|
||||
if got := filters[0].Labels; len(got) != 1 || got[0] != "api" {
|
||||
t.Errorf("Labels = %v, want the label the wire named", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestCountReadyPublishesNoPageParameters: `limit`, `offset` and `sort` are the
|
||||
// listing's parameters and are absent from this operation's table, so sending
|
||||
// one is version skew rather than a bad value — the client is asking for
|
||||
// something this operation does not have, and `unknown_parameter` is the answer
|
||||
// that tells it so. Silently accepting `limit` would be the failure the role
|
||||
// itself refuses; silently accepting `sort` would advertise a knob that changes
|
||||
// nothing about a set with no order.
|
||||
func TestCountReadyPublishesNoPageParameters(t *testing.T) {
|
||||
for _, param := range []string{"limit=5", "limit=0", "offset=1", "sort=oldest"} {
|
||||
ts, _ := newReadServer(t, Config{})
|
||||
resp := ts.get(t, "/v0/beads/ready:count?"+param)
|
||||
if resp.StatusCode != http.StatusBadRequest {
|
||||
t.Errorf("GET ready:count?%s: status = %d, want 400", param, resp.StatusCode)
|
||||
continue
|
||||
}
|
||||
body := decodeBody(t, resp)
|
||||
if body["reason"] != string(ReasonUnknownParameter) {
|
||||
t.Errorf("GET ready:count?%s: body = %v, want reason=unknown_parameter", param, body)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestCountReadyAndListReadyAdmitTheSameFilters is the parity the identity
|
||||
// rests on: every filter the listing accepts, the count accepts too, and both
|
||||
// build the same predicate from it. A parameter one of them knew and the other
|
||||
// did not would make "the size of the page you would get" false for exactly the
|
||||
// clients that sent it — and would fail as a 400 on one path only, which is the
|
||||
// kind of asymmetry nobody notices until a script breaks.
|
||||
func TestCountReadyAndListReadyAdmitTheSameFilters(t *testing.T) {
|
||||
shared := "assignee=alice&unassigned=false&type=bug&exclude_type=epic&label=api&label_any=x" +
|
||||
"&exclude_label=wip&label_pattern=tech-*&label_regex=^tech-&priority=1&parent=bd-1" +
|
||||
"&metadata_field=team%3Dcore&has_metadata_key=team&include_ephemeral=true&include_deferred=true"
|
||||
|
||||
listing, listRec := newReadServer(t, Config{})
|
||||
if resp := listing.get(t, "/v0/beads/ready?"+shared); resp.StatusCode != http.StatusOK {
|
||||
t.Fatalf("GET ready: status = %d, want 200: %s", resp.StatusCode, readAll(t, resp))
|
||||
}
|
||||
counting, countRec := newReadServer(t, Config{})
|
||||
if resp := counting.get(t, "/v0/beads/ready:count?"+shared); resp.StatusCode != http.StatusOK {
|
||||
t.Fatalf("GET ready:count: status = %d, want 200: %s", resp.StatusCode, readAll(t, resp))
|
||||
}
|
||||
|
||||
listed, counted := listRec.readyFilters(), countRec.readyFilters()
|
||||
if len(listed) != 1 || len(counted) != 1 {
|
||||
t.Fatalf("%d listing queries and %d counting queries, want 1 each", len(listed), len(counted))
|
||||
}
|
||||
// The page is the only thing that may differ.
|
||||
want := listed[0]
|
||||
want.Limit = 0
|
||||
if !reflect.DeepEqual(counted[0], want) {
|
||||
t.Errorf("the count and the listing built different predicates from one query string:\ncount: %+v\nlisting: %+v", counted[0], want)
|
||||
}
|
||||
}
|
||||
|
|
@ -34,6 +34,11 @@ import (
|
|||
// uow.UnitOfWorkProvider, because "a backend that cannot produce a unit of work
|
||||
// is still servable" is the property this whole seam exists for. If any of them
|
||||
// ever grows a NewUOW method these tests stop proving it.
|
||||
// The fakes below implement one issueops role each and NOTHING else —
|
||||
// deliberately not uow.UnitOfWorkProvider, because "a backend that cannot
|
||||
// produce a unit of work is still servable" is the property this whole seam
|
||||
// exists for. If any of them ever grows a NewUOW method these tests stop
|
||||
// proving it.
|
||||
|
||||
type roleReader struct {
|
||||
page issueops.IssuePage
|
||||
|
|
@ -112,6 +117,35 @@ func (e *roleEdgeReader) edgeRequests() []issueops.EdgeReadRequest {
|
|||
return append([]issueops.EdgeReadRequest(nil), e.reads...)
|
||||
}
|
||||
|
||||
// roleReadyCounter is the third role a store-shaped source must supply. It is
|
||||
// its own fake rather than a method on roleReader for the reason the roles are
|
||||
// separate interfaces: a backend hands out one surface per question, and a test
|
||||
// double that answered two of them would be the shape this seam exists to rule
|
||||
// out.
|
||||
type roleReadyCounter struct {
|
||||
total int64
|
||||
err error
|
||||
|
||||
mu sync.Mutex
|
||||
counts []issueops.ReadyRequest
|
||||
}
|
||||
|
||||
func (c *roleReadyCounter) CountReady(_ context.Context, req issueops.ReadyRequest) (issueops.ReadyCountResult, error) {
|
||||
c.mu.Lock()
|
||||
c.counts = append(c.counts, req)
|
||||
c.mu.Unlock()
|
||||
if c.err != nil {
|
||||
return issueops.ReadyCountResult{}, c.err
|
||||
}
|
||||
return issueops.ReadyCountResult{Total: c.total}, nil
|
||||
}
|
||||
|
||||
func (c *roleReadyCounter) countRequests() []issueops.ReadyRequest {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
return append([]issueops.ReadyRequest(nil), c.counts...)
|
||||
}
|
||||
|
||||
type roleClaimer struct {
|
||||
result issueops.ClaimResult
|
||||
err error
|
||||
|
|
@ -245,6 +279,9 @@ func rolesConfig(cfg Config) Config {
|
|||
if cfg.EdgeReader == nil {
|
||||
cfg.EdgeReader = &roleEdgeReader{}
|
||||
}
|
||||
if cfg.ReadyCounter == nil {
|
||||
cfg.ReadyCounter = &roleReadyCounter{}
|
||||
}
|
||||
return cfg
|
||||
}
|
||||
|
||||
|
|
@ -299,6 +336,11 @@ func countedPage() []*types.IssueWithCounts {
|
|||
// no claimer would bind, answer every read, and fail the one write on this
|
||||
// surface with a nil dereference — at claim time, in a handler, on a live
|
||||
// server. Each role an operation reaches has a row here for the same reason.
|
||||
// PARTIAL set is the dangerous one: a Config carrying a reader and no claimer
|
||||
// would bind, answer every read, and fail the one write on this surface with a
|
||||
// nil dereference — at claim time, in a handler, on a live server. The set
|
||||
// grows with the surface, so the case that matters most is the newest role
|
||||
// missing: that is what a caller written against the previous release passes.
|
||||
func TestListenRequiresExactlyOneDatabaseSource(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
|
|
@ -354,11 +396,35 @@ func TestListenRequiresExactlyOneDatabaseSource(t *testing.T) {
|
|||
cfg: rolesConfigWithout(func(c *Config) { c.EdgeReader = nil }),
|
||||
wantErr: "no database source",
|
||||
},
|
||||
{
|
||||
name: "no ready counter",
|
||||
cfg: rolesConfigWithout(func(c *Config) { c.ReadyCounter = nil }),
|
||||
wantErr: "no database source",
|
||||
},
|
||||
{
|
||||
name: "a cycle detector alone",
|
||||
cfg: Config{CycleDetector: &roleCycleDetector{}},
|
||||
wantErr: "no database source",
|
||||
},
|
||||
{
|
||||
// The newest role, and the one a caller written against the
|
||||
// previous release would leave out: the pair alone used to be a
|
||||
// complete source, and a Config that stayed that way would bind and
|
||||
// then nil-dereference on the first ready:count request.
|
||||
name: "a reader and a claimer without a ready counter",
|
||||
cfg: Config{Reader: &roleReader{}, Claimer: &roleClaimer{}},
|
||||
wantErr: "no database source",
|
||||
},
|
||||
{
|
||||
name: "a ready counter alone",
|
||||
cfg: Config{ReadyCounter: &roleReadyCounter{}},
|
||||
wantErr: "no database source",
|
||||
},
|
||||
{
|
||||
name: "a provider and a ready counter",
|
||||
cfg: Config{Provider: &fakeProvider{}, ReadyCounter: &roleReadyCounter{}},
|
||||
wantErr: "exactly one database source",
|
||||
},
|
||||
{
|
||||
name: "a provider and a reader",
|
||||
cfg: Config{Provider: &fakeProvider{}, Reader: &roleReader{}},
|
||||
|
|
@ -445,6 +511,9 @@ func TestConfiguredRolesServeTheSameReadyBytesAsAProvider(t *testing.T) {
|
|||
// database-touching operations against a store-shaped source, which is the
|
||||
// whole point: none of them can reach a unit of work here, because there is no
|
||||
// provider to open one.
|
||||
// TestConfiguredRolesAnswerEveryDatabaseRoute drives every database-touching
|
||||
// operation against a store-shaped source, which is the whole point: none of
|
||||
// them can reach a unit of work here, because there is no provider to open one.
|
||||
func TestConfiguredRolesAnswerEveryDatabaseRoute(t *testing.T) {
|
||||
details := &issueops.IssueDetails{Issue: *seededIssue("bd-1", "alice", types.StatusOpen)}
|
||||
reader := &roleReader{page: issueops.IssuePage{Items: countedPage(), HasMore: true}, details: details}
|
||||
|
|
@ -462,8 +531,10 @@ func TestConfiguredRolesAnswerEveryDatabaseRoute(t *testing.T) {
|
|||
{ID: "bd-1", Edges: []*types.Dependency{{IssueID: "bd-1", DependsOnID: "bd-2", Type: types.DepBlocks}}},
|
||||
{ID: "bd-9", Missing: true},
|
||||
}}}
|
||||
counter := &roleReadyCounter{total: 41}
|
||||
ts := newTestServer(t, rolesConfig(Config{
|
||||
Reader: reader, Claimer: claimer, Settings: settings, Stats: reporter, EdgeReader: edges,
|
||||
Reader: reader, Claimer: claimer, Settings: settings, Stats: reporter,
|
||||
EdgeReader: edges, ReadyCounter: counter,
|
||||
}))
|
||||
|
||||
t.Run("ready", func(t *testing.T) {
|
||||
|
|
@ -485,6 +556,29 @@ func TestConfiguredRolesAnswerEveryDatabaseRoute(t *testing.T) {
|
|||
}
|
||||
})
|
||||
|
||||
t.Run("count ready", func(t *testing.T) {
|
||||
resp := ts.get(t, "/v0/beads/ready:count?label=api")
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
t.Fatalf("status = %d, want 200: %s", resp.StatusCode, readAll(t, resp))
|
||||
}
|
||||
if body := decodeBody(t, resp); body["total"] != float64(41) {
|
||||
t.Errorf("total = %v, want the 41 the role reported", body["total"])
|
||||
}
|
||||
// The role is handed the listing's request with the page taken off:
|
||||
// the filter the wire named, no Limit, no Offset, and the sort this
|
||||
// server sends on both ready operations.
|
||||
reqs := counter.countRequests()
|
||||
if len(reqs) != 1 {
|
||||
t.Fatalf("count requests = %+v, want exactly one", reqs)
|
||||
}
|
||||
if got := reqs[0]; got.Limit != nil || got.Offset != 0 {
|
||||
t.Errorf("count request carries a page (limit=%v offset=%d); the role refuses one", got.Limit, got.Offset)
|
||||
}
|
||||
if got := reqs[0]; len(got.Labels) != 1 || got.Labels[0] != "api" || got.Sort != readySortDefault {
|
||||
t.Errorf("count request = %+v, want the wire filter under sort %q", got, readySortDefault)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("list", func(t *testing.T) {
|
||||
resp := ts.get(t, "/v0/beads/issues")
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
|
|
@ -657,6 +751,7 @@ func TestConfiguredRolesKeepTheDocumentedRefusals(t *testing.T) {
|
|||
Stats: &roleStats{},
|
||||
CycleDetector: &roleCycleDetector{},
|
||||
EdgeReader: &roleEdgeReader{},
|
||||
ReadyCounter: &roleReadyCounter{},
|
||||
})
|
||||
resp := ts.get(t, "/v0/beads/issues/bd-404")
|
||||
if resp.StatusCode != http.StatusNotFound {
|
||||
|
|
@ -678,6 +773,7 @@ func TestConfiguredRolesKeepTheDocumentedRefusals(t *testing.T) {
|
|||
Stats: &roleStats{},
|
||||
CycleDetector: &roleCycleDetector{},
|
||||
EdgeReader: &roleEdgeReader{},
|
||||
ReadyCounter: &roleReadyCounter{},
|
||||
})
|
||||
resp := ts.get(t, "/v0/beads/issues?status=bogus")
|
||||
if resp.StatusCode != http.StatusBadRequest {
|
||||
|
|
@ -701,7 +797,8 @@ func TestConfiguredRolesKeepTheDocumentedRefusals(t *testing.T) {
|
|||
Status: types.StatusInProgress,
|
||||
Err: fmt.Errorf("claim bd-1: %w", storage.ErrAlreadyClaimed),
|
||||
}},
|
||||
Settings: &roleSettings{},
|
||||
Settings: &roleSettings{},
|
||||
ReadyCounter: &roleReadyCounter{},
|
||||
})
|
||||
resp := ts.claim(t, claimPath, `{"actor":"alice"}`)
|
||||
if resp.StatusCode != http.StatusConflict {
|
||||
|
|
@ -724,6 +821,7 @@ func TestConfiguredRolesKeepTheDocumentedRefusals(t *testing.T) {
|
|||
Stats: &roleStats{},
|
||||
CycleDetector: &roleCycleDetector{},
|
||||
EdgeReader: &roleEdgeReader{},
|
||||
ReadyCounter: &roleReadyCounter{},
|
||||
})
|
||||
resp := ts.get(t, "/v0/beads/ready")
|
||||
if resp.StatusCode != http.StatusInternalServerError {
|
||||
|
|
@ -828,6 +926,7 @@ func TestARoleThatAnswersWithNothingIsNotDereferenced(t *testing.T) {
|
|||
Stats: &roleStats{},
|
||||
CycleDetector: &roleCycleDetector{},
|
||||
EdgeReader: &roleEdgeReader{},
|
||||
ReadyCounter: &roleReadyCounter{},
|
||||
})
|
||||
|
||||
got := silent.get(t, "/v0/beads/issues/bd-1")
|
||||
|
|
@ -858,6 +957,7 @@ func TestARoleThatAnswersWithNothingIsNotDereferenced(t *testing.T) {
|
|||
Stats: &roleStats{},
|
||||
CycleDetector: &roleCycleDetector{},
|
||||
EdgeReader: &roleEdgeReader{},
|
||||
ReadyCounter: &roleReadyCounter{},
|
||||
})
|
||||
|
||||
resp := ts.claim(t, claimPath, `{"actor":"alice"}`)
|
||||
|
|
|
|||
|
|
@ -95,6 +95,18 @@ var routeTable = []route{
|
|||
implemented: true,
|
||||
handler: (*Server).handleDependencyCycles,
|
||||
},
|
||||
{
|
||||
op: OpCountReadyWork,
|
||||
method: http.MethodGet,
|
||||
// A collection-level custom method, spelled the way the claim route's
|
||||
// is. Unlike that one it needs no specPath declaration: the segment is
|
||||
// a LITERAL, and only a wildcard segment is inexpressible as a ServeMux
|
||||
// pattern, so the router registers the documented path itself.
|
||||
pattern: "/v0/beads/ready:count",
|
||||
capability: "ready.count",
|
||||
implemented: true,
|
||||
handler: (*Server).handleCountReady,
|
||||
},
|
||||
{
|
||||
op: OpListIssues,
|
||||
method: http.MethodGet,
|
||||
|
|
|
|||
|
|
@ -124,13 +124,16 @@ type Config struct {
|
|||
// from, for a backend whose facade is a STORE rather than a unit-of-work
|
||||
// provider. sourceRoles is the authoritative list.
|
||||
// Reader, Claimer and EdgeReader are the issue roles this server answers
|
||||
// Reader, Claimer and ReadyCounter are the issue roles this server answers
|
||||
// from, for a backend whose facade is a STORE rather than a unit-of-work
|
||||
// provider.
|
||||
//
|
||||
// Set them together, and only when Provider is nil: they are the other
|
||||
// Set them all together, and only when Provider is nil: they are the other
|
||||
// Set all three together, and only when Provider is nil: they are the other
|
||||
// complete database source, not an override of one. Listen refuses every
|
||||
// other combination, including a PARTIAL set — a reader without a claimer
|
||||
// other combination, including a partial set — a reader without a claimer
|
||||
// would bind, answer every read, and fail the one write on this surface with
|
||||
// a nil dereference inside a handler, and the same is true of every
|
||||
// operation whose role is missing.
|
||||
|
|
@ -144,6 +147,8 @@ type Config struct {
|
|||
// dereference inside a handler on a live server. The set grows by one each
|
||||
// time an operation reaches a role this source does not yet carry, and it
|
||||
// grows in the same change as the operation for exactly that reason.
|
||||
// a nil dereference inside a handler, and a pair without a ready counter
|
||||
// would do the same on the one read that is not the reader's.
|
||||
//
|
||||
// A caller with a store takes them off the store's own accessors, and WHICH
|
||||
// store value it takes them off is the whole question. Every decorator a
|
||||
|
|
@ -165,6 +170,8 @@ type Config struct {
|
|||
// httpapi.Listen(httpapi.Config{Reader: rd, Claimer: cl, Settings: wc, Stats: st, ...})
|
||||
// ed, err := src.EdgeReader()
|
||||
// httpapi.Listen(httpapi.Config{Reader: rd, Claimer: cl, EdgeReader: ed, ...})
|
||||
// rc, err := src.ReadyCounter()
|
||||
// httpapi.Listen(httpapi.Config{Reader: rd, Claimer: cl, ReadyCounter: rc, ...})
|
||||
//
|
||||
// Listen refuses a hook-firing role rather than trusting the paragraph
|
||||
// above — see checkDatabaseSource.
|
||||
|
|
@ -196,6 +203,7 @@ type Config struct {
|
|||
Stats issueops.StatsReporter
|
||||
CycleDetector issueops.CycleDetector
|
||||
EdgeReader issueops.EdgeReader
|
||||
ReadyCounter issueops.ReadyCounter
|
||||
// Workspace is the startup snapshot GET /v0/beads/context answers from.
|
||||
// Only the allowlisted fields are ever serialized — see contextResponse,
|
||||
// which names the whole set and the reasons for the exclusions.
|
||||
|
|
@ -231,12 +239,13 @@ type Server struct {
|
|||
// exactly when provider is nil. They are what reader(), claimer() and
|
||||
// edgeReader() hand back on the store-shaped source; the names differ from
|
||||
// those methods because a struct cannot carry both.
|
||||
issueReader issueops.Reader
|
||||
issueClaimer issueops.Claimer
|
||||
settings issueops.WorkspaceConfig
|
||||
issueStats issueops.StatsReporter
|
||||
issueCycles issueops.CycleDetector
|
||||
issueEdges issueops.EdgeReader
|
||||
issueReader issueops.Reader
|
||||
issueClaimer issueops.Claimer
|
||||
settings issueops.WorkspaceConfig
|
||||
issueStats issueops.StatsReporter
|
||||
issueCycles issueops.CycleDetector
|
||||
issueEdges issueops.EdgeReader
|
||||
issueReadyCounter issueops.ReadyCounter
|
||||
|
||||
listener net.Listener
|
||||
http *http.Server
|
||||
|
|
@ -331,14 +340,15 @@ func Listen(cfg Config) (*Server, error) {
|
|||
}
|
||||
|
||||
s := &Server{
|
||||
cfg: cfg,
|
||||
provider: cfg.Provider,
|
||||
issueReader: cfg.Reader,
|
||||
issueClaimer: cfg.Claimer,
|
||||
settings: cfg.Settings,
|
||||
issueStats: cfg.Stats,
|
||||
issueCycles: cfg.CycleDetector,
|
||||
issueEdges: cfg.EdgeReader,
|
||||
cfg: cfg,
|
||||
provider: cfg.Provider,
|
||||
issueReader: cfg.Reader,
|
||||
issueClaimer: cfg.Claimer,
|
||||
settings: cfg.Settings,
|
||||
issueStats: cfg.Stats,
|
||||
issueCycles: cfg.CycleDetector,
|
||||
issueEdges: cfg.EdgeReader,
|
||||
issueReadyCounter: cfg.ReadyCounter,
|
||||
|
||||
sem: make(chan struct{}, maxInflight),
|
||||
semTimeout: semAcquireTimeout,
|
||||
|
|
@ -405,6 +415,17 @@ func Listen(cfg Config) (*Server, error) {
|
|||
// it would otherwise produce is the worst shape available — a config missing
|
||||
// one role binds, answers every other route, and fails that one with a nil
|
||||
// dereference in a handler on a live server.
|
||||
// provider, or the issue roles. A PARTIAL set of roles is refused with the same
|
||||
// message as none at all, because it is the same mistake and the failure it
|
||||
// would otherwise produce is the worst shape available — a reader without a
|
||||
// claimer binds, answers every read, and fails the one write on this surface
|
||||
// with a nil dereference in a handler on a live server.
|
||||
//
|
||||
// The set GROWS as this surface grows, and that is deliberate rather than
|
||||
// unfortunate: every operation added here is an operation a roles-backed
|
||||
// deployment must be able to answer, so a role added to the set turns "this
|
||||
// build serves an operation your Config cannot answer" into a startup error
|
||||
// instead of a 500 on the first client that finds it.
|
||||
//
|
||||
// Both together is refused rather than resolved by precedence: a caller that
|
||||
// set both holds two different opinions about where this server reads from, and
|
||||
|
|
@ -431,12 +452,12 @@ func Listen(cfg Config) (*Server, error) {
|
|||
// as this check is concerned, exactly as it was when the check was a hand-
|
||||
// written boolean per role.
|
||||
func sourceRoles(cfg Config) []any {
|
||||
return []any{cfg.Reader, cfg.Claimer, cfg.Settings, cfg.Stats, cfg.CycleDetector, cfg.EdgeReader}
|
||||
return []any{cfg.Reader, cfg.Claimer, cfg.Settings, cfg.Stats, cfg.CycleDetector, cfg.EdgeReader, cfg.ReadyCounter}
|
||||
}
|
||||
|
||||
// roleSourceNames spells sourceRoles for the refusal message, in the same
|
||||
// order, so a caller reading the error learns the whole set it must pass.
|
||||
const roleSourceNames = "Reader, Claimer, Settings, Stats, CycleDetector and EdgeReader"
|
||||
const roleSourceNames = "Reader, Claimer, Settings, Stats, CycleDetector, EdgeReader and ReadyCounter"
|
||||
|
||||
func anyRoleSet(cfg Config) bool {
|
||||
return slices.ContainsFunc(sourceRoles(cfg), func(r any) bool { return r != nil })
|
||||
|
|
@ -657,6 +678,26 @@ func (s *Server) edgeReader(r *http.Request) (issueops.EdgeReader, error) {
|
|||
return src.EdgeReader()
|
||||
}
|
||||
|
||||
// readyCounter returns the ready-count surface for one request.
|
||||
//
|
||||
// It is the third role this server hands a handler, and the read-side twin of
|
||||
// the two above in every respect: the configured role on the roles source, one
|
||||
// built per request on the provider source so the unit of work the count opens
|
||||
// lands in THIS request's uow_ms, and held by INTERFACE so
|
||||
// uow.ReadyCounterSource is load-bearing rather than decorative.
|
||||
//
|
||||
// It goes out UNWRAPPED, and that is the difference worth stating. checkedReader
|
||||
// and checkedClaimer exist because their handlers dereference a POINTER a role
|
||||
// returned; CountReady answers with a value, so there is no dereference to make
|
||||
// safe and a wrapper would be ceremony that reads like a guarantee.
|
||||
func (s *Server) readyCounter(r *http.Request) (issueops.ReadyCounter, error) {
|
||||
if s.provider == nil {
|
||||
return s.issueReadyCounter, nil
|
||||
}
|
||||
var src uow.ReadyCounterSource = timedProvider{inner: s.provider, rec: requestInfo(r.Context())}
|
||||
return src.ReadyCounter()
|
||||
}
|
||||
|
||||
// WithUOW runs fn inside one unit of work and guarantees the rollback.
|
||||
//
|
||||
// The close context is DETACHED on purpose. Close sends ROLLBACK on the pinned
|
||||
|
|
|
|||
|
|
@ -578,7 +578,8 @@ func TestCapabilitiesAdvertiseEveryImplementedOperation(t *testing.T) {
|
|||
}
|
||||
want := []string{
|
||||
"config.get", "config.list", "dependencies.cycles", "dependencies.list",
|
||||
"issues.claim", "issues.get", "issues.list", "ready.list", "stats.get",
|
||||
"issues.claim", "issues.get", "issues.list", "ready.count", "ready.list",
|
||||
"stats.get",
|
||||
}
|
||||
if !slices.Equal(got, want) {
|
||||
t.Errorf("capabilities = %v, want %v", got, want)
|
||||
|
|
|
|||
|
|
@ -66,6 +66,7 @@ info:
|
|||
Both are uniform rules, reachable on EVERY route including `GET /healthz`,
|
||||
and are stated here instead of being repeated on all nine operations. The
|
||||
and are stated here instead of being repeated on all seven operations. The
|
||||
and are stated here instead of being repeated on every operation. The
|
||||
per-operation `responses` below therefore list what an operation produces
|
||||
BEYOND these two: a generated client must treat `400 invalid_argument` as
|
||||
possible everywhere, including on the operations whose documented statuses
|
||||
|
|
@ -554,6 +555,155 @@ paths:
|
|||
'503':
|
||||
$ref: '#/components/responses/Unavailable'
|
||||
|
||||
/v0/beads/ready:count:
|
||||
get:
|
||||
operationId: countReadyWork
|
||||
summary: Count ready work
|
||||
description: >-
|
||||
How many items `GET /v0/beads/ready` would return for the same filters
|
||||
with `limit=0`. That is an identity, not an estimate: the two
|
||||
operations answer from one role over one predicate
|
||||
(`issueops.ReadyCounter`), so a client may page the listing and print
|
||||
this number beside it — which is exactly what `bd ready` does when it
|
||||
reports "showing 100 of 412".
|
||||
|
||||
|
||||
THERE IS NO `limit` AND NO `sort` PARAMETER, and both absences are
|
||||
deliberate. A cardinality has no page: a limit here would answer "how
|
||||
many of the first N", which breaks the identity above, so the role
|
||||
refuses one rather than accepting it and dropping it. A cardinality has
|
||||
no order either — this operation counts the same set the listing
|
||||
returns under ANY sort policy — so there is nothing for a `sort`
|
||||
parameter to change and it is not published. Every other parameter of
|
||||
the listing is here and means exactly what it means there, including
|
||||
the default type exclusions and the way `type` drops them.
|
||||
|
||||
|
||||
It costs a query of its own. A client that only needs to know whether
|
||||
MORE work exists should read `has_more` on the listing instead; this
|
||||
operation is for the client that needs the number.
|
||||
parameters:
|
||||
- name: assignee
|
||||
in: query
|
||||
description: Only issues assigned to this actor.
|
||||
schema:
|
||||
type: string
|
||||
- name: unassigned
|
||||
in: query
|
||||
description: Only issues with no assignee.
|
||||
schema:
|
||||
type: boolean
|
||||
- name: type
|
||||
in: query
|
||||
description: >-
|
||||
Issue type, with the shorthand alias expansion and the
|
||||
match-nothing-rather-than-fail treatment `GET /v0/beads/ready`
|
||||
documents. Setting it drops the default type exclusions and makes
|
||||
`exclude_type` ignored, there and here alike.
|
||||
schema:
|
||||
type: string
|
||||
- name: exclude_type
|
||||
in: query
|
||||
description: >-
|
||||
Issue types to exclude. Repeat the parameter, or pass a
|
||||
comma-separated list. Ignored when `type` is set.
|
||||
style: form
|
||||
explode: true
|
||||
schema:
|
||||
type: array
|
||||
items:
|
||||
type: string
|
||||
- name: label
|
||||
in: query
|
||||
description: Labels that must ALL be present (AND).
|
||||
style: form
|
||||
explode: true
|
||||
schema:
|
||||
type: array
|
||||
items:
|
||||
type: string
|
||||
- name: label_any
|
||||
in: query
|
||||
description: Labels of which at least one must be present (OR).
|
||||
style: form
|
||||
explode: true
|
||||
schema:
|
||||
type: array
|
||||
items:
|
||||
type: string
|
||||
- name: exclude_label
|
||||
in: query
|
||||
description: Labels that must not be present.
|
||||
style: form
|
||||
explode: true
|
||||
schema:
|
||||
type: array
|
||||
items:
|
||||
type: string
|
||||
- name: label_pattern
|
||||
in: query
|
||||
description: Glob matched against labels.
|
||||
schema:
|
||||
type: string
|
||||
- name: label_regex
|
||||
in: query
|
||||
description: Regular expression matched against labels.
|
||||
schema:
|
||||
type: string
|
||||
- name: priority
|
||||
in: query
|
||||
description: Exact priority (0 is a real value, not "unset").
|
||||
schema:
|
||||
type: integer
|
||||
- name: parent
|
||||
in: query
|
||||
description: Restrict to recursive descendants of this issue.
|
||||
schema:
|
||||
type: string
|
||||
- name: metadata_field
|
||||
in: query
|
||||
description: >-
|
||||
Top-level metadata equality filter as `key=value`, split on the
|
||||
first `=`. Repeatable. An invalid key is a 400.
|
||||
style: form
|
||||
explode: true
|
||||
schema:
|
||||
type: array
|
||||
items:
|
||||
type: string
|
||||
- name: has_metadata_key
|
||||
in: query
|
||||
description: Only issues carrying this top-level metadata key.
|
||||
schema:
|
||||
type: string
|
||||
- name: include_ephemeral
|
||||
in: query
|
||||
description: >-
|
||||
Include ephemeral (non-synced) rows, which the count merges exactly
|
||||
as the listing lists them.
|
||||
schema:
|
||||
type: boolean
|
||||
default: false
|
||||
- name: include_deferred
|
||||
in: query
|
||||
description: Include issues whose `defer_until` is still in the future.
|
||||
schema:
|
||||
type: boolean
|
||||
default: false
|
||||
responses:
|
||||
'200':
|
||||
description: The size of the ready set.
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/ReadyCount'
|
||||
'400':
|
||||
$ref: '#/components/responses/InvalidArgument'
|
||||
'500':
|
||||
$ref: '#/components/responses/InternalError'
|
||||
'503':
|
||||
$ref: '#/components/responses/Unavailable'
|
||||
|
||||
/v0/beads/issues:
|
||||
get:
|
||||
operationId: listIssues
|
||||
|
|
@ -1908,15 +2058,42 @@ components:
|
|||
type: array
|
||||
description: >-
|
||||
The operations this server actually implements, derived from its
|
||||
route table. v0's vocabulary is `ready.list`, `issues.list`,
|
||||
`issues.get`, `issues.claim`, `stats.get`, `config.list`,
|
||||
`config.get`, `dependencies.cycles`, `dependencies.list`; it grows
|
||||
additively, and an operation never appears here unless it is fully
|
||||
implemented. This is how a client checks for an operation — never
|
||||
the version string.
|
||||
route table. v0's vocabulary is `ready.list`, `ready.count`,
|
||||
`issues.list`, `issues.get`, `issues.claim`, `stats.get`,
|
||||
`config.list`, `config.get`, `dependencies.cycles`,
|
||||
`dependencies.list`; it grows additively, and an operation never
|
||||
appears here unless it is fully implemented. This is how a client
|
||||
checks for an operation — never the version string.
|
||||
items:
|
||||
type: string
|
||||
|
||||
ReadyCount:
|
||||
type: object
|
||||
required: [total]
|
||||
properties:
|
||||
total:
|
||||
type: integer
|
||||
format: int64
|
||||
description: >-
|
||||
How many items `GET /v0/beads/ready` would return for these filters
|
||||
with `limit=0`. Never negative; 0 when nothing is ready, which is a
|
||||
200 rather than a 404 — a question about a set has an answer even
|
||||
when the set is empty.
|
||||
description: >-
|
||||
The size of a ready set. It carries no items, no `has_more` and no
|
||||
cursor: this is a number about a set, and the operation that returns
|
||||
rows is `GET /v0/beads/ready`.
|
||||
|
||||
|
||||
It is NOT `x-go-type`-pinned, unlike the seven schemas above, and that
|
||||
is a decision rather than an omission: those seven are pinned because a
|
||||
canonical Go struct's JSON encoding IS the contract and a second wire
|
||||
struct would let the CLI's `--json` drift from these bodies. There is
|
||||
no canonical struct here — the CLI publishes this number inside its own
|
||||
stdout envelope's `pagination` member, which is not a wire type — so
|
||||
pinning would weld this body to a CLI presentation type instead of
|
||||
preventing a drift.
|
||||
|
||||
ReadyPage:
|
||||
type: object
|
||||
required: [items, has_more]
|
||||
|
|
|
|||
|
|
@ -671,6 +671,10 @@ func (s *configStore) CycleDetector() (issueops.CycleDetector, error) {
|
|||
func (s *configStore) EdgeReader() (issueops.EdgeReader, error) {
|
||||
return nil, &storage.ErrUnsupported{Op: "EdgeReader", Backend: "jira-config-stub"}
|
||||
}
|
||||
|
||||
func (s *configStore) ReadyCounter() (issueops.ReadyCounter, error) {
|
||||
return nil, &storage.ErrUnsupported{Op: "ReadyCounter", Backend: "jira-config-stub"}
|
||||
}
|
||||
func (s *configStore) SetConfig(_ context.Context, _, _ string) error { return nil }
|
||||
func (s *configStore) SetLocalMetadata(_ context.Context, _, _ string) error { return nil }
|
||||
func (s *configStore) GetLocalMetadata(_ context.Context, _ string) (string, error) {
|
||||
|
|
|
|||
24
internal/storage/dolt/ready_counter.go
Normal file
24
internal/storage/dolt/ready_counter.go
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
package dolt
|
||||
|
||||
import (
|
||||
"github.com/steveyegge/beads/internal/storage"
|
||||
"github.com/steveyegge/beads/internal/workapi/storereadycounter"
|
||||
"github.com/steveyegge/beads/issueops"
|
||||
)
|
||||
|
||||
// ReadyCounter returns the guarded ready-count surface for this store.
|
||||
func (s *DoltStore) ReadyCounter() (issueops.ReadyCounter, error) {
|
||||
return newReadyCounter(s)
|
||||
}
|
||||
|
||||
// newReadyCounter returns guarded ready counts backed by store.
|
||||
//
|
||||
// The implementation is the shared one, for the reason newIssueReader gives:
|
||||
// the two Dolt-backed stores differ below storage.DoltStorage, not above it,
|
||||
// so a second copy here would be a copy of nothing but the constructor.
|
||||
func newReadyCounter(store *DoltStore) (issueops.ReadyCounter, error) {
|
||||
if store == nil {
|
||||
return nil, &storage.ErrUnsupported{Op: "newReadyCounter", Backend: "nil"}
|
||||
}
|
||||
return storereadycounter.New(store)
|
||||
}
|
||||
78
internal/storage/dolt/ready_counter_contract_test.go
Normal file
78
internal/storage/dolt/ready_counter_contract_test.go
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
package dolt
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/steveyegge/beads/backend/conformance"
|
||||
)
|
||||
|
||||
// TestReadyCounterContract runs the ReadyCounter contract against the
|
||||
// server-backed store, which reaches the cheap indexed COUNT(*) path
|
||||
// (internal/workapi/storereadycounter over CountReadyWorkInTx).
|
||||
//
|
||||
// The cases are subtests of one parent so the whole role suite shares one
|
||||
// store and one copy-on-write branch: each Run namespaces its ids and labels
|
||||
// under the fixture prefix and scopes its own request to them, and
|
||||
// WritesNothing takes a before/after history delta, which is only meaningful
|
||||
// while the subtests run sequentially. setupTestStore already marks the PARENT
|
||||
// parallel; no subtest here calls t.Parallel.
|
||||
func TestReadyCounterContract(t *testing.T) {
|
||||
fixture, ctx, cleanup := newDoltReadyCounterFixture(t, "rdc")
|
||||
defer cleanup()
|
||||
|
||||
t.Run("EqualsTheUnboundedPage", func(t *testing.T) {
|
||||
conformance.RunReadyCounterEqualsTheUnboundedPage(t, ctx, fixture)
|
||||
})
|
||||
t.Run("RejectsLimitAndOffset", func(t *testing.T) {
|
||||
conformance.RunReadyCounterRejectsLimitAndOffset(t, ctx, fixture)
|
||||
})
|
||||
t.Run("CountsTheBlockerAwareSet", func(t *testing.T) {
|
||||
conformance.RunReadyCounterCountsTheBlockerAwareSet(t, ctx, fixture)
|
||||
})
|
||||
t.Run("EphemeralGateMatchesTheListing", func(t *testing.T) {
|
||||
conformance.RunReadyCounterEphemeralGateMatchesTheListing(t, ctx, fixture)
|
||||
})
|
||||
t.Run("EmptyFrontIsZeroAndNil", func(t *testing.T) {
|
||||
conformance.RunReadyCounterEmptyFrontIsZeroAndNil(t, ctx, fixture)
|
||||
})
|
||||
t.Run("WritesNothing", func(t *testing.T) {
|
||||
conformance.RunReadyCounterWritesNothing(t, ctx, fixture)
|
||||
})
|
||||
t.Run("DoesNotMutateTheCallerRequest", func(t *testing.T) {
|
||||
conformance.RunReadyCounterDoesNotMutateTheCallerRequest(t, ctx, fixture)
|
||||
})
|
||||
}
|
||||
|
||||
// newDoltReadyCounterFixture composes the frozen role kit with this backend's
|
||||
// two accessors — the surface under test and the reader the identity case
|
||||
// compares it against.
|
||||
func newDoltReadyCounterFixture(t *testing.T, prefix string) (conformance.ReadyCounterFixture, context.Context, func()) {
|
||||
t.Helper()
|
||||
store, storeCleanup := setupTestStore(t)
|
||||
ctx, cancel := testContext(t)
|
||||
stop := func() {
|
||||
cancel()
|
||||
storeCleanup()
|
||||
}
|
||||
counter, err := store.ReadyCounter()
|
||||
if err != nil {
|
||||
stop()
|
||||
t.Fatalf("ReadyCounter(): %v", err)
|
||||
}
|
||||
reader, err := store.IssueReader()
|
||||
if err != nil {
|
||||
stop()
|
||||
t.Fatalf("IssueReader(): %v", err)
|
||||
}
|
||||
kit := newDoltRoleFixtureKit(store, prefix)
|
||||
return conformance.ReadyCounterFixture{
|
||||
IssuePrefix: kit.IssuePrefix,
|
||||
ReadyCounter: counter,
|
||||
Reader: reader,
|
||||
CreateIssue: kit.CreateIssue,
|
||||
CreateWisp: kit.CreateWisp,
|
||||
AddDependency: kit.AddDependency,
|
||||
CountHistory: kit.CountHistory,
|
||||
}, ctx, stop
|
||||
}
|
||||
26
internal/storage/embeddeddolt/ready_counter.go
Normal file
26
internal/storage/embeddeddolt/ready_counter.go
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
//go:build cgo
|
||||
|
||||
package embeddeddolt
|
||||
|
||||
import (
|
||||
"github.com/steveyegge/beads/internal/storage"
|
||||
"github.com/steveyegge/beads/internal/workapi/storereadycounter"
|
||||
"github.com/steveyegge/beads/issueops"
|
||||
)
|
||||
|
||||
// ReadyCounter returns the guarded ready-count surface for this store.
|
||||
func (s *EmbeddedDoltStore) ReadyCounter() (issueops.ReadyCounter, error) {
|
||||
return newReadyCounter(s)
|
||||
}
|
||||
|
||||
// newReadyCounter returns guarded ready counts backed by store.
|
||||
//
|
||||
// The implementation is the shared one: the two Dolt-backed stores differ
|
||||
// below storage.DoltStorage, not above it, so a second copy here would be a
|
||||
// copy of nothing but the constructor.
|
||||
func newReadyCounter(store *EmbeddedDoltStore) (issueops.ReadyCounter, error) {
|
||||
if store == nil {
|
||||
return nil, &storage.ErrUnsupported{Op: "newReadyCounter", Backend: "nil"}
|
||||
}
|
||||
return storereadycounter.New(store)
|
||||
}
|
||||
70
internal/storage/embeddeddolt/ready_counter_contract_test.go
Normal file
70
internal/storage/embeddeddolt/ready_counter_contract_test.go
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
//go:build cgo
|
||||
|
||||
package embeddeddolt_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/steveyegge/beads/backend/conformance"
|
||||
)
|
||||
|
||||
// TestReadyCounterContract runs the ReadyCounter contract against the embedded
|
||||
// store, which hands back the SAME body the server-backed store does
|
||||
// (internal/workapi/storereadycounter) and differs only in the engine
|
||||
// underneath. That is what this wiring catches; it is not an independent vote
|
||||
// on the body.
|
||||
//
|
||||
// One environment for the whole suite: booting an embedded engine per case
|
||||
// would dominate the runtime, the ids and labels are prefix-namespaced and
|
||||
// every request is scoped to them, and the history delta needs the subtests
|
||||
// sequential anyway.
|
||||
func TestReadyCounterContract(t *testing.T) {
|
||||
skipUnlessEmbeddedDolt(t)
|
||||
te := newTestEnv(t, "rdc")
|
||||
ctx := t.Context()
|
||||
fixture := newEmbeddedReadyCounterFixture(t, te, "rdc")
|
||||
|
||||
t.Run("EqualsTheUnboundedPage", func(t *testing.T) {
|
||||
conformance.RunReadyCounterEqualsTheUnboundedPage(t, ctx, fixture)
|
||||
})
|
||||
t.Run("RejectsLimitAndOffset", func(t *testing.T) {
|
||||
conformance.RunReadyCounterRejectsLimitAndOffset(t, ctx, fixture)
|
||||
})
|
||||
t.Run("CountsTheBlockerAwareSet", func(t *testing.T) {
|
||||
conformance.RunReadyCounterCountsTheBlockerAwareSet(t, ctx, fixture)
|
||||
})
|
||||
t.Run("EphemeralGateMatchesTheListing", func(t *testing.T) {
|
||||
conformance.RunReadyCounterEphemeralGateMatchesTheListing(t, ctx, fixture)
|
||||
})
|
||||
t.Run("EmptyFrontIsZeroAndNil", func(t *testing.T) {
|
||||
conformance.RunReadyCounterEmptyFrontIsZeroAndNil(t, ctx, fixture)
|
||||
})
|
||||
t.Run("WritesNothing", func(t *testing.T) {
|
||||
conformance.RunReadyCounterWritesNothing(t, ctx, fixture)
|
||||
})
|
||||
t.Run("DoesNotMutateTheCallerRequest", func(t *testing.T) {
|
||||
conformance.RunReadyCounterDoesNotMutateTheCallerRequest(t, ctx, fixture)
|
||||
})
|
||||
}
|
||||
|
||||
func newEmbeddedReadyCounterFixture(t *testing.T, te *testEnv, prefix string) conformance.ReadyCounterFixture {
|
||||
t.Helper()
|
||||
counter, err := te.store.ReadyCounter()
|
||||
if err != nil {
|
||||
t.Fatalf("ReadyCounter(): %v", err)
|
||||
}
|
||||
reader, err := te.store.IssueReader()
|
||||
if err != nil {
|
||||
t.Fatalf("IssueReader(): %v", err)
|
||||
}
|
||||
kit := newEmbeddedRoleFixtureKit(te, prefix)
|
||||
return conformance.ReadyCounterFixture{
|
||||
IssuePrefix: kit.IssuePrefix,
|
||||
ReadyCounter: counter,
|
||||
Reader: reader,
|
||||
CreateIssue: kit.CreateIssue,
|
||||
CreateWisp: kit.CreateWisp,
|
||||
AddDependency: kit.AddDependency,
|
||||
CountHistory: kit.CountHistory,
|
||||
}
|
||||
}
|
||||
17
internal/storage/hook_ready_counter.go
Normal file
17
internal/storage/hook_ready_counter.go
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
package storage
|
||||
|
||||
import (
|
||||
"github.com/steveyegge/beads/issueops"
|
||||
)
|
||||
|
||||
// ReadyCounter returns the inner store's ready-count surface.
|
||||
//
|
||||
// It recurses, like every other accessor on this decorator, rather than being
|
||||
// absent: the accessor set is uniform across the chain, so a caller never has
|
||||
// to know which decorators a store is wearing. What it does NOT do is wrap the
|
||||
// result — sizing the ready set fires no completion hooks, because nothing
|
||||
// completed. That is the same statement hook_counter.go and
|
||||
// hook_issue_reader.go make, and it is why this file is three lines.
|
||||
func (h *HookFiringStore) ReadyCounter() (issueops.ReadyCounter, error) {
|
||||
return h.inner.ReadyCounter()
|
||||
}
|
||||
|
|
@ -24,6 +24,7 @@ var roleAccessorNames = []string{
|
|||
"WorkspaceConfig",
|
||||
"StatsReporter",
|
||||
"CycleDetector",
|
||||
"ReadyCounter",
|
||||
"Commenter",
|
||||
"ReadyClaimer",
|
||||
"BatchCloser",
|
||||
|
|
@ -70,36 +71,38 @@ func assertRoleAccessorsAreDeclared(t *testing.T, decorator reflect.Type) {
|
|||
// a decorated surface from a passed-through one.
|
||||
type roleAccessorStore struct {
|
||||
DoltStorage
|
||||
lifecycle issueops.Lifecycle
|
||||
reader issueops.Reader
|
||||
relations issueops.Relations
|
||||
edges issueops.EdgeReader
|
||||
counter issueops.Counter
|
||||
settings issueops.WorkspaceConfig
|
||||
stats issueops.StatsReporter
|
||||
cycles issueops.CycleDetector
|
||||
commenter issueops.Commenter
|
||||
claimer issueops.ReadyClaimer
|
||||
closer issueops.BatchCloser
|
||||
editor issueops.DependencyEditor
|
||||
err error
|
||||
lifecycle issueops.Lifecycle
|
||||
reader issueops.Reader
|
||||
relations issueops.Relations
|
||||
edges issueops.EdgeReader
|
||||
counter issueops.Counter
|
||||
settings issueops.WorkspaceConfig
|
||||
stats issueops.StatsReporter
|
||||
cycles issueops.CycleDetector
|
||||
commenter issueops.Commenter
|
||||
claimer issueops.ReadyClaimer
|
||||
closer issueops.BatchCloser
|
||||
editor issueops.DependencyEditor
|
||||
readyCounter issueops.ReadyCounter
|
||||
err error
|
||||
}
|
||||
|
||||
func newRoleAccessorStore() *roleAccessorStore {
|
||||
sentinel := &roleAccessorSentinel{}
|
||||
return &roleAccessorStore{
|
||||
lifecycle: sentinel,
|
||||
reader: sentinel,
|
||||
relations: sentinel,
|
||||
edges: sentinel,
|
||||
counter: sentinel,
|
||||
settings: sentinel,
|
||||
stats: sentinel,
|
||||
cycles: sentinel,
|
||||
commenter: sentinel,
|
||||
claimer: sentinel,
|
||||
closer: sentinel,
|
||||
editor: sentinel,
|
||||
lifecycle: sentinel,
|
||||
reader: sentinel,
|
||||
relations: sentinel,
|
||||
edges: sentinel,
|
||||
counter: sentinel,
|
||||
settings: sentinel,
|
||||
stats: sentinel,
|
||||
cycles: sentinel,
|
||||
commenter: sentinel,
|
||||
claimer: sentinel,
|
||||
closer: sentinel,
|
||||
editor: sentinel,
|
||||
readyCounter: sentinel,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -119,6 +122,9 @@ func (s *roleAccessorStore) CycleDetector() (issueops.CycleDetector, error) {
|
|||
}
|
||||
func (s *roleAccessorStore) EdgeReader() (issueops.EdgeReader, error) { return s.edges, s.err }
|
||||
func (s *roleAccessorStore) Commenter() (issueops.Commenter, error) { return s.commenter, s.err }
|
||||
func (s *roleAccessorStore) ReadyCounter() (issueops.ReadyCounter, error) {
|
||||
return s.readyCounter, s.err
|
||||
}
|
||||
func (s *roleAccessorStore) ReadyClaimer() (issueops.ReadyClaimer, error) {
|
||||
return s.claimer, s.err
|
||||
}
|
||||
|
|
@ -186,6 +192,10 @@ func (*roleAccessorSentinel) AssigneeStats(context.Context, issueops.AssigneeSta
|
|||
func (*roleAccessorSentinel) DetectCycles(context.Context, issueops.DetectCyclesRequest) (issueops.CycleReport, error) {
|
||||
return issueops.CycleReport{}, nil
|
||||
}
|
||||
|
||||
func (*roleAccessorSentinel) CountReady(context.Context, issueops.ReadyRequest) (issueops.ReadyCountResult, error) {
|
||||
return issueops.ReadyCountResult{}, nil
|
||||
}
|
||||
func (*roleAccessorSentinel) AddComment(context.Context, issueops.AddCommentRequest) (issueops.AddCommentResult, error) {
|
||||
return issueops.AddCommentResult{}, nil
|
||||
}
|
||||
|
|
@ -229,6 +239,12 @@ func (*roleAccessorSentinel) RemoveDependency(context.Context, issueops.RemoveDe
|
|||
// hook_relations.go, hook_edgereader.go, hook_counter.go); this test pins that
|
||||
// as a decision rather than leaving it indistinguishable from the regression
|
||||
// above.
|
||||
// The four read roles are asserted the OTHER way round on purpose. Reads fire
|
||||
// no completion hooks, so IssueReader, IssueRelations, Counter and ReadyCounter
|
||||
// deliberately return the inner surface unwrapped (hook_issue_reader.go,
|
||||
// hook_relations.go, hook_counter.go, hook_ready_counter.go); this test pins
|
||||
// that as a decision rather than leaving it indistinguishable from the
|
||||
// regression above.
|
||||
func TestHookFiringStoreWrapsTheWriteRolesAndPassesTheReadsThrough(t *testing.T) {
|
||||
inner := newRoleAccessorStore()
|
||||
store := NewHookFiringStore(inner, nil)
|
||||
|
|
@ -251,6 +267,7 @@ func TestHookFiringStoreWrapsTheWriteRolesAndPassesTheReadsThrough(t *testing.T)
|
|||
{"WorkspaceConfig", func() (any, error) { return store.WorkspaceConfig() }, inner.settings, false},
|
||||
{"StatsReporter", func() (any, error) { return store.StatsReporter() }, inner.stats, false},
|
||||
{"CycleDetector", func() (any, error) { return store.CycleDetector() }, inner.cycles, false},
|
||||
{"ReadyCounter", func() (any, error) { return store.ReadyCounter() }, inner.readyCounter, false},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
surface, err := test.got()
|
||||
|
|
@ -293,6 +310,7 @@ func TestHookFiringStoreRoleAccessorsPropagateInnerErrors(t *testing.T) {
|
|||
{"WorkspaceConfig", func() (any, error) { return store.WorkspaceConfig() }},
|
||||
{"StatsReporter", func() (any, error) { return store.StatsReporter() }},
|
||||
{"CycleDetector", func() (any, error) { return store.CycleDetector() }},
|
||||
{"ReadyCounter", func() (any, error) { return store.ReadyCounter() }},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
if _, err := test.got(); !errors.Is(err, want) {
|
||||
|
|
|
|||
|
|
@ -192,6 +192,15 @@ type Storage interface {
|
|||
// refuses a missing anchor outright. Reads fire no hooks, as for
|
||||
// IssueReader.
|
||||
EdgeReader() (issueops.EdgeReader, error)
|
||||
// ReadyCounter returns the guarded ready-count surface for this store: the
|
||||
// size of the ready set, which is the number `bd ready`'s pagination
|
||||
// publishes and which no other role answers. IssueReader.Ready reports
|
||||
// only whether the limit hid anything, and Counter's predicate is a filter
|
||||
// over one table where the ready predicate is blocker-aware.
|
||||
//
|
||||
// Reads fire no hooks, so the hook decorator's answer is its inner store's
|
||||
// unchanged, as it is for IssueReader.
|
||||
ReadyCounter() (issueops.ReadyCounter, error)
|
||||
|
||||
// Issue CRUD
|
||||
CreateIssue(ctx context.Context, issue *types.Issue, actor string) error
|
||||
|
|
@ -623,10 +632,15 @@ type BackupStore interface {
|
|||
// ReadyWorkCounter sizes the total ready-work count for a filter without
|
||||
// materializing the counts mega-query. It is identical to
|
||||
// len(GetReadyWorkWithCounts(filter with Limit=0)) but computed with cheap
|
||||
// indexed COUNT(*)s over the ready predicate. `bd ready --json` type-asserts to
|
||||
// this (via UnwrapStore) to render the "Showing X of N" total when a page is
|
||||
// capped, and falls back to the unbounded GetReadyWorkWithCounts when a store
|
||||
// does not implement it.
|
||||
// indexed COUNT(*)s over the ready predicate.
|
||||
//
|
||||
// THAT IDENTITY IS THIS INTERFACE'S WHOLE CONTRACT, and it is now also a public
|
||||
// promise: issueops.ReadyCounter states it for every backend, and the
|
||||
// store-backed body behind ReadyCounter() (internal/workapi/storereadycounter)
|
||||
// is this method plus the shared filter builder. `bd ready`'s "Showing X of N"
|
||||
// reaches it that way on both routes; it no longer type-asserts for the
|
||||
// capability itself, because a store that cannot answer now fails to compile
|
||||
// rather than falling back to an unbounded query at runtime.
|
||||
type ReadyWorkCounter interface {
|
||||
CountReadyWork(ctx context.Context, filter types.WorkFilter) (int, error)
|
||||
}
|
||||
|
|
|
|||
71
internal/storage/uow/ready_counter.go
Normal file
71
internal/storage/uow/ready_counter.go
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
package uow
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/steveyegge/beads/internal/workapi"
|
||||
publicops "github.com/steveyegge/beads/issueops"
|
||||
)
|
||||
|
||||
// ReadyCounterSource is the capability accessor a unit-of-work provider offers
|
||||
// for the ready-count role, the sibling of IssueReaderSource and
|
||||
// ReadyClaimerSource. A consumer holding a provider by interface asks for the
|
||||
// role here instead of reaching for a constructor.
|
||||
type ReadyCounterSource interface {
|
||||
ReadyCounter() (publicops.ReadyCounter, error)
|
||||
}
|
||||
|
||||
// readyCounter sizes the ready set through a unit of work.
|
||||
type readyCounter struct {
|
||||
provider UnitOfWorkProvider
|
||||
}
|
||||
|
||||
// ReadyCounter returns the guarded ready-count surface for this provider.
|
||||
func (p *doltSQLProvider) ReadyCounter() (publicops.ReadyCounter, error) {
|
||||
return NewReadyCounter(p)
|
||||
}
|
||||
|
||||
// NewReadyCounter constructs a public ready counter backed by provider.
|
||||
func NewReadyCounter(provider UnitOfWorkProvider) (publicops.ReadyCounter, error) {
|
||||
if isNilUnitOfWorkProvider(provider) {
|
||||
return nil, fmt.Errorf("new ready counter: unit-of-work provider must not be nil")
|
||||
}
|
||||
return &readyCounter{provider: provider}, nil
|
||||
}
|
||||
|
||||
var _ publicops.ReadyCounter = (*readyCounter)(nil)
|
||||
|
||||
// CountReady answers one ready count inside one read-only unit of work.
|
||||
//
|
||||
// IT COUNTS THE PAGE, and that is a deliberate choice rather than a missing
|
||||
// optimization. This seam has no COUNT(*) over the ready predicate — the ready
|
||||
// set is a UNION of two tables whose overlap the id query resolves in Go
|
||||
// (domain/db/issue_search.go: an id present in both planes is a hard error
|
||||
// here, not a de-duplication) — so a hand-rolled COUNT would be a second
|
||||
// definition of the ready set on the one backend whose definition is already
|
||||
// separate code. Running the SAME query Reader.Ready runs, unbounded, and
|
||||
// taking its length makes the identity CountReady promises true BY
|
||||
// CONSTRUCTION on this backend, including the way it fails.
|
||||
//
|
||||
// What that costs is the unbounded ready query the store-backed sibling avoids
|
||||
// with an indexed COUNT(*). Both front doors ask for this number only when a
|
||||
// page came back full, so the cost is paid on truncated listings and nowhere
|
||||
// else; issueops.ReadyCounter promises the answer and says out loud that it
|
||||
// does not promise the cost.
|
||||
func (c *readyCounter) CountReady(ctx context.Context, req publicops.ReadyRequest) (publicops.ReadyCountResult, error) {
|
||||
// The same builder the store-backed sibling runs, which is also
|
||||
// BuildReadyFilter with the page removed — so the refusals of a Limit and
|
||||
// an Offset are one definition rather than one per backend.
|
||||
filter, err := workapi.BuildReadyCountFilter(req)
|
||||
if err != nil {
|
||||
return publicops.ReadyCountResult{}, err
|
||||
}
|
||||
return RunTxRead(ctx, c.provider, func(ctx context.Context, uw UnitOfWork) (publicops.ReadyCountResult, error) {
|
||||
page, err := uw.IssueUseCase().GetReadyWorkWithCounts(ctx, filter)
|
||||
if err != nil {
|
||||
return publicops.ReadyCountResult{}, err
|
||||
}
|
||||
return publicops.ReadyCountResult{Total: int64(len(page.Items))}, nil
|
||||
})
|
||||
}
|
||||
79
internal/storage/uow/ready_counter_contract_test.go
Normal file
79
internal/storage/uow/ready_counter_contract_test.go
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
package uow
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/steveyegge/beads/backend/conformance"
|
||||
)
|
||||
|
||||
// TestReadyCounterContract runs the ReadyCounter contract against the
|
||||
// unit-of-work provider — the one implementation that does not hand back
|
||||
// internal/workapi/storereadycounter, and the one that answers by counting the
|
||||
// unbounded page rather than with an indexed COUNT(*). It is the SECOND of two
|
||||
// votes, not the third: the two store backends share the other body.
|
||||
//
|
||||
// One provider for the whole suite (each newUOWRoleFixtureProvider boots a real
|
||||
// Dolt sql-server) and NO t.Parallel: this backend has no per-test
|
||||
// copy-on-write branch, so dolt_log and the issues table are database-global
|
||||
// and a parallel subtest would corrupt another subtest's history delta.
|
||||
func TestReadyCounterContract(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
fixture := newUOWReadyCounterFixture(t, ctx, "rdc")
|
||||
|
||||
t.Run("EqualsTheUnboundedPage", func(t *testing.T) {
|
||||
conformance.RunReadyCounterEqualsTheUnboundedPage(t, ctx, fixture)
|
||||
})
|
||||
t.Run("RejectsLimitAndOffset", func(t *testing.T) {
|
||||
conformance.RunReadyCounterRejectsLimitAndOffset(t, ctx, fixture)
|
||||
})
|
||||
t.Run("CountsTheBlockerAwareSet", func(t *testing.T) {
|
||||
conformance.RunReadyCounterCountsTheBlockerAwareSet(t, ctx, fixture)
|
||||
})
|
||||
t.Run("EphemeralGateMatchesTheListing", func(t *testing.T) {
|
||||
conformance.RunReadyCounterEphemeralGateMatchesTheListing(t, ctx, fixture)
|
||||
})
|
||||
t.Run("EmptyFrontIsZeroAndNil", func(t *testing.T) {
|
||||
conformance.RunReadyCounterEmptyFrontIsZeroAndNil(t, ctx, fixture)
|
||||
})
|
||||
t.Run("WritesNothing", func(t *testing.T) {
|
||||
conformance.RunReadyCounterWritesNothing(t, ctx, fixture)
|
||||
})
|
||||
t.Run("DoesNotMutateTheCallerRequest", func(t *testing.T) {
|
||||
conformance.RunReadyCounterDoesNotMutateTheCallerRequest(t, ctx, fixture)
|
||||
})
|
||||
}
|
||||
|
||||
func newUOWReadyCounterFixture(t *testing.T, ctx context.Context, prefix string) conformance.ReadyCounterFixture {
|
||||
t.Helper()
|
||||
provider := newUOWRoleFixtureProvider(t, ctx, prefix)
|
||||
// Through the capability accessors, not NewReadyCounter/NewIssueReader: a
|
||||
// provider that stopped offering either role is the regression, and a
|
||||
// constructor call would hide it.
|
||||
source, ok := provider.(ReadyCounterSource)
|
||||
if !ok {
|
||||
t.Fatalf("provider %T does not offer the ReadyCounter accessor", provider)
|
||||
}
|
||||
counter, err := source.ReadyCounter()
|
||||
if err != nil {
|
||||
t.Fatalf("ReadyCounter(): %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.ReadyCounterFixture{
|
||||
IssuePrefix: kit.IssuePrefix,
|
||||
ReadyCounter: counter,
|
||||
Reader: reader,
|
||||
CreateIssue: kit.CreateIssue,
|
||||
CreateWisp: kit.CreateWisp,
|
||||
AddDependency: kit.AddDependency,
|
||||
CountHistory: kit.CountHistory,
|
||||
}
|
||||
}
|
||||
36
internal/telemetry/ready_counter.go
Normal file
36
internal/telemetry/ready_counter.go
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
package telemetry
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/steveyegge/beads/issueops"
|
||||
)
|
||||
|
||||
// ReadyCounter returns the inner store's ready-count surface wrapped in this
|
||||
// layer's instrumentation. It recurses instead of delegating: a blind
|
||||
// delegation would return the inner counter unspanned and untimed.
|
||||
func (s *InstrumentedStorage) ReadyCounter() (issueops.ReadyCounter, error) {
|
||||
inner, err := s.Unwrap().ReadyCounter()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s.WrapReadyCounter(inner), nil
|
||||
}
|
||||
|
||||
// WrapReadyCounter instruments guarded ready counts with this storage layer's
|
||||
// existing telemetry meter and tracer.
|
||||
func (s *InstrumentedStorage) WrapReadyCounter(inner issueops.ReadyCounter) issueops.ReadyCounter {
|
||||
return &instrumentedReadyCounter{storage: s, inner: inner}
|
||||
}
|
||||
|
||||
type instrumentedReadyCounter struct {
|
||||
storage *InstrumentedStorage
|
||||
inner issueops.ReadyCounter
|
||||
}
|
||||
|
||||
func (c *instrumentedReadyCounter) CountReady(ctx context.Context, request issueops.ReadyRequest) (result issueops.ReadyCountResult, err error) {
|
||||
ctx, span, started := c.storage.op(ctx, "ReadyCounter.CountReady")
|
||||
result, err = c.inner.CountReady(ctx, request)
|
||||
c.storage.done(ctx, span, started, err)
|
||||
return result, err
|
||||
}
|
||||
|
|
@ -25,6 +25,7 @@ var roleAccessorNames = []string{
|
|||
"WorkspaceConfig",
|
||||
"StatsReporter",
|
||||
"CycleDetector",
|
||||
"ReadyCounter",
|
||||
"Commenter",
|
||||
"ReadyClaimer",
|
||||
"BatchCloser",
|
||||
|
|
@ -80,6 +81,9 @@ func (s *roleAccessorStore) CycleDetector() (issueops.CycleDetector, error) {
|
|||
}
|
||||
func (s *roleAccessorStore) EdgeReader() (issueops.EdgeReader, error) { return s.surface, s.err }
|
||||
func (s *roleAccessorStore) Commenter() (issueops.Commenter, error) { return s.surface, s.err }
|
||||
func (s *roleAccessorStore) ReadyCounter() (issueops.ReadyCounter, error) {
|
||||
return s.surface, s.err
|
||||
}
|
||||
func (s *roleAccessorStore) ReadyClaimer() (issueops.ReadyClaimer, error) {
|
||||
return s.surface, s.err
|
||||
}
|
||||
|
|
@ -147,6 +151,10 @@ func (*roleAccessorSentinel) AssigneeStats(context.Context, issueops.AssigneeSta
|
|||
func (*roleAccessorSentinel) DetectCycles(context.Context, issueops.DetectCyclesRequest) (issueops.CycleReport, error) {
|
||||
return issueops.CycleReport{}, nil
|
||||
}
|
||||
|
||||
func (*roleAccessorSentinel) CountReady(context.Context, issueops.ReadyRequest) (issueops.ReadyCountResult, error) {
|
||||
return issueops.ReadyCountResult{}, nil
|
||||
}
|
||||
func (*roleAccessorSentinel) AddComment(context.Context, issueops.AddCommentRequest) (issueops.AddCommentResult, error) {
|
||||
return issueops.AddCommentResult{}, nil
|
||||
}
|
||||
|
|
@ -193,6 +201,7 @@ func TestInstrumentedStorageInstrumentsEveryRoleAccessor(t *testing.T) {
|
|||
{"WorkspaceConfig", func() (any, error) { return wrapped.WorkspaceConfig() }},
|
||||
{"StatsReporter", func() (any, error) { return wrapped.StatsReporter() }},
|
||||
{"CycleDetector", func() (any, error) { return wrapped.CycleDetector() }},
|
||||
{"ReadyCounter", func() (any, error) { return wrapped.ReadyCounter() }},
|
||||
{"Commenter", func() (any, error) { return wrapped.Commenter() }},
|
||||
{"ReadyClaimer", func() (any, error) { return wrapped.ReadyClaimer() }},
|
||||
{"BatchCloser", func() (any, error) { return wrapped.BatchCloser() }},
|
||||
|
|
@ -233,6 +242,7 @@ func TestInstrumentedStorageRoleAccessorsPropagateInnerErrors(t *testing.T) {
|
|||
{"WorkspaceConfig", func() (any, error) { return wrapped.WorkspaceConfig() }},
|
||||
{"StatsReporter", func() (any, error) { return wrapped.StatsReporter() }},
|
||||
{"CycleDetector", func() (any, error) { return wrapped.CycleDetector() }},
|
||||
{"ReadyCounter", func() (any, error) { return wrapped.ReadyCounter() }},
|
||||
{"Commenter", func() (any, error) { return wrapped.Commenter() }},
|
||||
{"ReadyClaimer", func() (any, error) { return wrapped.ReadyClaimer() }},
|
||||
{"BatchCloser", func() (any, error) { return wrapped.BatchCloser() }},
|
||||
|
|
|
|||
|
|
@ -96,6 +96,34 @@ func BuildReadyFilter(in issueops.ReadyRequest) (types.WorkFilter, error) {
|
|||
return filter, nil
|
||||
}
|
||||
|
||||
// BuildReadyCountFilter turns a ready request into the storage-level filter a
|
||||
// COUNT of the ready set runs against: BuildReadyFilter's filter with the page
|
||||
// removed, and the single definition of what `bd ready`'s published total
|
||||
// means.
|
||||
//
|
||||
// It refuses a request carrying a page. issueops.ReadyCounter.CountReady
|
||||
// promises its answer equals len(Reader.Ready(r with Limit=0).Items), and a
|
||||
// Limit would make that "how many of the first N" while an Offset would
|
||||
// subtract the rows it skipped from the size of a set that still holds them —
|
||||
// so both are ErrValidation here rather than accepted and dropped, which is
|
||||
// the same call ValidateClaimNextRequest makes for the same reason.
|
||||
//
|
||||
// The zeroed limit is set on a LOCAL copy: a nil Limit means the shared ready
|
||||
// default at BuildReadyFilter, so an unlimited count has to say so explicitly,
|
||||
// and the caller's request is not the place to say it.
|
||||
func BuildReadyCountFilter(in issueops.ReadyRequest) (types.WorkFilter, error) {
|
||||
if in.Limit != nil {
|
||||
return types.WorkFilter{}, fmt.Errorf("%w: a ready count does not take a limit", issueops.ErrValidation)
|
||||
}
|
||||
if in.Offset != 0 {
|
||||
return types.WorkFilter{}, fmt.Errorf("%w: a ready count does not take an offset", issueops.ErrValidation)
|
||||
}
|
||||
unlimited := 0
|
||||
counted := in
|
||||
counted.Limit = &unlimited
|
||||
return BuildReadyFilter(counted)
|
||||
}
|
||||
|
||||
// normalizeExcludeTypes splits comma-separated exclusions and expands type
|
||||
// aliases, so --exclude-type=mr,epic and --exclude-type mr --exclude-type epic
|
||||
// mean the same thing.
|
||||
|
|
|
|||
110
internal/workapi/ready_count_test.go
Normal file
110
internal/workapi/ready_count_test.go
Normal file
|
|
@ -0,0 +1,110 @@
|
|||
package workapi
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/steveyegge/beads/issueops"
|
||||
)
|
||||
|
||||
// TestBuildReadyCountFilterIsTheUnboundedReadyFilter is the filter-level half
|
||||
// of the identity issueops.ReadyCounter promises: the predicate a count runs is
|
||||
// the predicate the listing runs, with the page taken off. Asserting it against
|
||||
// BuildReadyFilter rather than field by field is what makes it survive a new
|
||||
// ReadyRequest field — one the count builder forgot to carry would show up here
|
||||
// as a difference, where a field-by-field check would simply not mention it.
|
||||
func TestBuildReadyCountFilterIsTheUnboundedReadyFilter(t *testing.T) {
|
||||
priority := 1
|
||||
request := issueops.ReadyRequest{
|
||||
IssueType: "mr",
|
||||
Assignee: "alice",
|
||||
Labels: []string{" alpha ", "alpha"},
|
||||
LabelsAny: []string{"gamma"},
|
||||
ExcludeLabels: []string{" delta "},
|
||||
LabelPattern: "tech-*",
|
||||
LabelRegex: "^tech-",
|
||||
Priority: &priority,
|
||||
ParentID: "bd-parent",
|
||||
IncludeDeferred: true,
|
||||
IncludeEphemeral: true,
|
||||
ExcludeTypes: []string{"epic, mol"},
|
||||
MetadataFields: map[string]string{"team": "core"},
|
||||
HasMetadataKey: "team",
|
||||
Sort: "priority",
|
||||
}
|
||||
|
||||
counted, err := BuildReadyCountFilter(request)
|
||||
if err != nil {
|
||||
t.Fatalf("BuildReadyCountFilter() error = %v", err)
|
||||
}
|
||||
|
||||
unlimited := 0
|
||||
listing := request
|
||||
listing.Limit = &unlimited
|
||||
want, err := BuildReadyFilter(listing)
|
||||
if err != nil {
|
||||
t.Fatalf("BuildReadyFilter() error = %v", err)
|
||||
}
|
||||
if !reflect.DeepEqual(counted, want) {
|
||||
t.Errorf("BuildReadyCountFilter() = %+v\nBuildReadyFilter(Limit=0) = %+v\nthe count and the listing must run one predicate", counted, want)
|
||||
}
|
||||
if counted.Limit != 0 {
|
||||
t.Errorf("counted filter Limit = %d, want 0: a nil Limit takes the shared page default, so an unbounded count has to say so", counted.Limit)
|
||||
}
|
||||
if counted.Offset != 0 {
|
||||
t.Errorf("counted filter Offset = %d, want 0", counted.Offset)
|
||||
}
|
||||
}
|
||||
|
||||
// TestBuildReadyCountFilterRefusesAPage pins the two deterministic refusals the
|
||||
// role states (issueops/readycounter.go:74-86). The explicitly-unlimited limit
|
||||
// is refused with the rest: an unlimited count is the only kind there is, so
|
||||
// asking for one is asking for the default, and accepting the pointer here
|
||||
// would make "unset" and "explicitly unlimited" mean different things to a
|
||||
// count than they mean to a page.
|
||||
func TestBuildReadyCountFilterRefusesAPage(t *testing.T) {
|
||||
limit := 10
|
||||
unlimited := 0
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
request issueops.ReadyRequest
|
||||
}{
|
||||
{"limit", issueops.ReadyRequest{Sort: "priority", Limit: &limit}},
|
||||
{"explicitly unlimited limit", issueops.ReadyRequest{Sort: "priority", Limit: &unlimited}},
|
||||
{"offset", issueops.ReadyRequest{Sort: "priority", Offset: 5}},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
if _, err := BuildReadyCountFilter(test.request); !errors.Is(err, issueops.ErrValidation) {
|
||||
t.Fatalf("BuildReadyCountFilter() error = %v, want ErrValidation", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestBuildReadyCountFilterLeavesTheRequestAlone is the builder-level half of
|
||||
// the snapshot promise. The zeroed limit goes on a local copy, so a caller that
|
||||
// counts and then lists with the same request value still has the request it
|
||||
// wrote.
|
||||
func TestBuildReadyCountFilterLeavesTheRequestAlone(t *testing.T) {
|
||||
request := issueops.ReadyRequest{Sort: "priority", Labels: []string{"alpha"}}
|
||||
want := issueops.ReadyRequest{Sort: "priority", Labels: []string{"alpha"}}
|
||||
|
||||
if _, err := BuildReadyCountFilter(request); err != nil {
|
||||
t.Fatalf("BuildReadyCountFilter() error = %v", err)
|
||||
}
|
||||
if !reflect.DeepEqual(request, want) {
|
||||
t.Errorf("BuildReadyCountFilter mutated the caller's request: got %+v, want %+v", request, want)
|
||||
}
|
||||
}
|
||||
|
||||
// TestBuildReadyCountFilterRefusesAnInvalidSort keeps the count's refusals a
|
||||
// SUPERSET of the listing's rather than a replacement: a sort policy the
|
||||
// listing rejects is not quietly accepted here because a count has no order.
|
||||
// The request has to stay the listing's request for the identity to hold, and
|
||||
// that includes the ways it can be wrong.
|
||||
func TestBuildReadyCountFilterRefusesAnInvalidSort(t *testing.T) {
|
||||
if _, err := BuildReadyCountFilter(issueops.ReadyRequest{Sort: "bogus"}); !errors.Is(err, issueops.ErrValidation) {
|
||||
t.Fatalf("BuildReadyCountFilter(sort=bogus) error = %v, want ErrValidation", err)
|
||||
}
|
||||
}
|
||||
67
internal/workapi/storereadycounter/counter.go
Normal file
67
internal/workapi/storereadycounter/counter.go
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
// Package storereadycounter holds the store-backed implementation of
|
||||
// issueops.ReadyCounter: one shared body that every store-shaped backend's
|
||||
// ReadyCounter accessor hands back.
|
||||
//
|
||||
// It is a package of its own for the reason internal/workapi/storereader and
|
||||
// internal/workapi/storecounter are — see either package's doc. A constructor
|
||||
// sitting in internal/workapi would be a one-line drop-in for
|
||||
// store.ReadyCounter() from any front door, and one that silently skips the
|
||||
// decorators, because a decorator adds its layer in its own accessor. Down
|
||||
// here the only importers are the two Dolt store packages, and the
|
||||
// cmd-bd-role-constructors depguard rule in .golangci.yml makes a front door
|
||||
// importing it a lint failure rather than a review comment.
|
||||
//
|
||||
// The accessor is the door. This is the thing behind it.
|
||||
package storereadycounter
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/steveyegge/beads/internal/storage"
|
||||
"github.com/steveyegge/beads/internal/workapi"
|
||||
"github.com/steveyegge/beads/issueops"
|
||||
)
|
||||
|
||||
// New returns the ready-count surface backed by a store handle. *DoltStore and
|
||||
// *EmbeddedDoltStore answer identically because the difference between them is
|
||||
// below storage.DoltStorage, not above it.
|
||||
//
|
||||
// THE PARAMETER IS THE CHEAP SEAM ITSELF, not storage.DoltStorage, and that is
|
||||
// the one thing this body does differently from its siblings. `bd ready --json`
|
||||
// used to ask for that seam at RUNTIME — unwrap the decorators, type-assert to
|
||||
// storage.ReadyWorkCounter, and fall back to an unbounded ready query when the
|
||||
// assertion missed — because the value it held was a decorated interface that
|
||||
// could not answer for its own inner store. Reached through an accessor the
|
||||
// question is settled at COMPILE time: a store that cannot size the ready set
|
||||
// cannot construct this body, so there is no silent fallback to a mega-query
|
||||
// and no branch nothing exercises.
|
||||
func New(store storage.ReadyWorkCounter) (issueops.ReadyCounter, error) {
|
||||
if store == nil {
|
||||
return nil, &issueops.ErrUnsupported{Op: "storereadycounter.New", Backend: "nil"}
|
||||
}
|
||||
return &storeReadyCounter{store: store}, nil
|
||||
}
|
||||
|
||||
type storeReadyCounter struct{ store storage.ReadyWorkCounter }
|
||||
|
||||
var _ issueops.ReadyCounter = (*storeReadyCounter)(nil)
|
||||
|
||||
// CountReady sizes the ready set with the store's indexed COUNT(*) path.
|
||||
//
|
||||
// The filter comes from the shared builder, so the predicate counted here and
|
||||
// the predicate Reader.Ready lists are one predicate, and the identity
|
||||
// CountReady promises is a property of the query rather than of this file:
|
||||
// storage.ReadyWorkCounter is documented as identical to
|
||||
// len(GetReadyWorkWithCounts(filter with Limit=0)), which is the same filter
|
||||
// this builder produces for the same request.
|
||||
func (c *storeReadyCounter) CountReady(ctx context.Context, req issueops.ReadyRequest) (issueops.ReadyCountResult, error) {
|
||||
filter, err := workapi.BuildReadyCountFilter(req)
|
||||
if err != nil {
|
||||
return issueops.ReadyCountResult{}, err
|
||||
}
|
||||
total, err := c.store.CountReadyWork(ctx, filter)
|
||||
if err != nil {
|
||||
return issueops.ReadyCountResult{}, err
|
||||
}
|
||||
return issueops.ReadyCountResult{Total: int64(total)}, nil
|
||||
}
|
||||
|
|
@ -328,3 +328,34 @@ func TestCounterExposesTypedUnsupportedError(t *testing.T) {
|
|||
t.Fatalf("Counter() error = %v, want *beads.ErrUnsupported", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestReadyCounterKeepsTelemetryOutermost is the ready-count role's version of
|
||||
// the same pin, and it is the READ answer again: sizing the ready set fires no
|
||||
// completion hooks, so the hook decorator adds no layer and the outermost
|
||||
// surface a caller gets is the instrumented one.
|
||||
func TestReadyCounterKeepsTelemetryOutermost(t *testing.T) {
|
||||
t.Setenv("BD_OTEL_STDOUT", "true")
|
||||
instrumented, ok := telemetry.WrapStorage(&dolt.DoltStore{}).(*telemetry.InstrumentedStorage)
|
||||
if !ok {
|
||||
t.Fatal("WrapStorage() did not create InstrumentedStorage")
|
||||
}
|
||||
|
||||
counter, err := storage.NewHookFiringStore(instrumented, nil).ReadyCounter()
|
||||
if err != nil {
|
||||
t.Fatalf("ReadyCounter() error = %v", err)
|
||||
}
|
||||
if got := reflect.TypeOf(counter).String(); got != "*telemetry.instrumentedReadyCounter" {
|
||||
t.Fatalf("outer layer = %s, want the telemetry wrapper unwrapped by the hook decorator", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadyCounterExposesTypedUnsupportedError(t *testing.T) {
|
||||
counter, err := (*dolt.DoltStore)(nil).ReadyCounter()
|
||||
if counter != nil {
|
||||
t.Fatalf("ReadyCounter() counter = %T, want nil", counter)
|
||||
}
|
||||
var unsupported *beads.ErrUnsupported
|
||||
if !errors.As(err, &unsupported) {
|
||||
t.Fatalf("ReadyCounter() error = %v, want *beads.ErrUnsupported", err)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -374,7 +374,10 @@ type IssuePage struct {
|
|||
// cap, --claim, --gated, --explain, --mol, --watch, the hierarchical
|
||||
// --parent tree, and the text renderings that want []*types.Issue rather
|
||||
// than a counted page. Routing only their JSON paths through the role
|
||||
// would fork each command in two, which is more drift, not less.
|
||||
// would fork each command in two, which is more drift, not less. Two of
|
||||
// `bd ready`'s questions HAVE left the filter behind, each for the role
|
||||
// that owns it: --claim is ReadyClaimer's, and the published total is
|
||||
// ReadyCounter's, on both routes.
|
||||
//
|
||||
// WHAT THOSE TWO DO SHARE, stated exactly, because "not on the role" is not
|
||||
// the same as "unprotected":
|
||||
|
|
@ -392,10 +395,13 @@ type IssuePage struct {
|
|||
// --max-rows cap.
|
||||
// - EXECUTION, for `bd ready`, on the PROXIED route only. The direct route
|
||||
// keeps an epilogue of its own and cannot give it up: it answers the
|
||||
// strictly larger question "how many rows did the limit hide" with a
|
||||
// second counting query and publishes the total in its pagination meta,
|
||||
// where this role answers only "were any hidden". Collapsing them would
|
||||
// change one surface's published output.
|
||||
// strictly larger question "how many rows did the limit hide" and
|
||||
// publishes the total in its pagination meta, where this role answers only
|
||||
// "were any hidden". Collapsing them would change one surface's published
|
||||
// output. That second question is not off-role, though — it is
|
||||
// ReadyCounter's, which both routes now ask through their own accessor,
|
||||
// over this same ReadyRequest; what stays outside this role is the PAGE's
|
||||
// epilogue, not the count beside it.
|
||||
//
|
||||
// THE CLAIM, stated once and in full so it can be checked sentence by
|
||||
// sentence. SHARED: all three issue reads on the HTTP surface go through this
|
||||
|
|
|
|||
95
issueops/readycounter.go
Normal file
95
issueops/readycounter.go
Normal file
|
|
@ -0,0 +1,95 @@
|
|||
package issueops
|
||||
|
||||
import "context"
|
||||
|
||||
// ReadyCountResult is the size of a ready set.
|
||||
type ReadyCountResult struct {
|
||||
// Total is how many rows the request's ready predicate admits. It is an
|
||||
// int64 for the reason CountResult.Total is one — the counting seam counts
|
||||
// in one — and not because a ready front is expected to be large.
|
||||
Total int64
|
||||
}
|
||||
|
||||
// ReadyCounter describes sizing the ready set: the question `bd ready`'s
|
||||
// pagination answers when it reports "showing 100 of 412", and — like Reader,
|
||||
// ReadyClaimer and Counter — a role with its own accessor. A new capability
|
||||
// gets a new role interface and its own accessor; never append a method here.
|
||||
//
|
||||
// IT IS A DIFFERENT QUESTION FROM BOTH OF ITS NEIGHBORS, which is why it is
|
||||
// neither a Reader method nor a Counter one.
|
||||
//
|
||||
// - Reader.Ready answers with a PAGE, and the verdict it carries is
|
||||
// HasMore: "did the limit hide anything". This role answers "how many",
|
||||
// which is strictly larger — no page can be truncated into it — and it is
|
||||
// the number one surface has published for years, so collapsing the two
|
||||
// would change shipped output rather than tidy a duplicate.
|
||||
// - Counter answers a predicate over one table. The ready predicate is
|
||||
// BLOCKER-AWARE: it reads the dependency graph and the wisp tier, and no
|
||||
// CountRequest can describe it. Counter's own doc says so in as many words
|
||||
// ("The count of READY work is likewise a different question ... and is
|
||||
// its own role for the same reason").
|
||||
//
|
||||
// IT TAKES ReadyRequest VERBATIM — the same type Reader.Ready takes and the
|
||||
// same type ClaimNextRequest.Filter carries, not a parallel one shaped like
|
||||
// it. A listing, a claim and a count then ask ONE question of ONE set, and the
|
||||
// three cannot drift apart at the predicate: every implementation builds the
|
||||
// filter through internal/workapi.BuildReadyCountFilter, which is
|
||||
// BuildReadyFilter with the page removed.
|
||||
//
|
||||
// Implementations never mutate caller-owned request values, snapshot the
|
||||
// request at method entry, and apply normalization only to attempt-local
|
||||
// clones. That promise is load-bearing rather than conventional here:
|
||||
// ReadyRequest carries four label slices and a metadata map, and normalization
|
||||
// is the step that would otherwise write through them.
|
||||
//
|
||||
// COUNTING IS A READ. Nothing here records a history entry, fires a completion
|
||||
// hook or changes a row, and a refusal changes nothing either. Deterministic
|
||||
// request-validation failures match ErrValidation; result values are
|
||||
// unspecified when error is non-nil.
|
||||
//
|
||||
// WHAT COST THIS HAS IS NOT PROMISED, only what it answers. The store-backed
|
||||
// implementation sizes the set with indexed COUNT(*)s over the ready
|
||||
// predicate; the unit-of-work implementation has no such seam and materializes
|
||||
// the unbounded ready page to count it. Both answer the same number for the
|
||||
// same request — that IS promised, by CountReady — and a caller that must
|
||||
// bound the work asks a narrower question rather than a bounded one, because
|
||||
// there is no bounded count to ask for (see CountReady's refusals).
|
||||
type ReadyCounter interface {
|
||||
// CountReady returns how many issues the request's ready predicate admits.
|
||||
//
|
||||
// THE ANSWER IS AN IDENTITY, not a similar number:
|
||||
//
|
||||
// CountReady(r).Total == len(Reader.Ready(r with Limit=0).Items)
|
||||
//
|
||||
// for every request r this method accepts, at every implementation. That
|
||||
// is the whole contract of this role — a total the page it sizes does not
|
||||
// agree with is worse than no total at all, because it is read as "how
|
||||
// much work is left" — and it is why the two surfaces share a request type
|
||||
// rather than merely similar filters. It holds for the wisp tier too: a
|
||||
// request that admits ephemeral rows counts them exactly as the listing
|
||||
// lists them.
|
||||
//
|
||||
// LIMIT AND OFFSET MUST BE UNSET (ErrValidation), mirroring
|
||||
// ClaimNextRequest for a reason of the same shape. A cardinality has no
|
||||
// page: a Limit would answer "how many of the first N", which no caller
|
||||
// wants and which the identity above would stop being true of; an Offset
|
||||
// would silently subtract the rows it skipped from the size of a set that
|
||||
// still contains them. Rejecting both says that out loud rather than
|
||||
// accepting two knobs and dropping them. An explicit zero Limit is refused
|
||||
// with the rest: Limit is a pointer so "unset" and "explicitly unlimited"
|
||||
// stay distinguishable (see ReadyRequest.Limit), and only the first is
|
||||
// what this request permits — an unlimited count is the only kind there
|
||||
// is, so asking for one is asking for the default.
|
||||
//
|
||||
// A PREDICATE THAT MATCHES NOTHING IS 0 AND A NIL ERROR. That is the whole
|
||||
// of the "not found" story here, as it is for Counter: an empty ready
|
||||
// front is the steady state of a drained queue, and a poller that had to
|
||||
// classify an error to read a zero would be pattern-matching prose.
|
||||
//
|
||||
// IT IS NOT ONE SNAPSHOT WITH A PAGE. A caller that lists a page and then
|
||||
// counts has issued two queries, and a write between them can leave the
|
||||
// total disagreeing with the page by that write. Nothing here is
|
||||
// transactional across the two calls, and no front door reconciles them:
|
||||
// `bd ready --json` publishes both and says only what each is.
|
||||
CountReady(ctx context.Context, req ReadyRequest) (ReadyCountResult, error)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue