The role landed facade-only (#5485) with neither front door, and its own doc said why: the numbers it answers are already printed through internal/workapi's detail seam, and the wire operation "lands in the graph-counts wire slice". This is that slice. What it buys is the question the stored-edge read cannot ask — how many edges, in a direction the caller names, without materializing a row. Spec first, then `make api-gen`, then the handler. IT IS NOT THE LISTING COUNTED, and the operationId says so. `listDependencies` is OUTGOING ONLY and takes no direction; this one REQUIRES a direction and answers about either end, so the two agree on a number at `direction=out` and nowhere else. `countDependencies` would have promised they always agree, so it is `countDependencyEdges` — and the path is `/v0/beads/dependencies:count`, the collection-level custom method `ready:count` and `issues:count` already spell, on the collection whose listing it sits beside. Both segments are literal, so pattern and specPath agree; it collides with nothing, because /cycles, /blocking and /tree all carry a separating slash and ServeMux matches whole segments. THE ANSWER IS PER ANCHOR, which is the shape difference from the listing beside it. An edge row carries its own `issue_id`, so that operation flattens; a number does not, so folding these would produce a total nobody asked for and lose the per-issue answer every caller wants. `anchors` is an ARRAY in first-mention order — the role's doc says a map would be the one result shape a wire operation cannot carry without inventing a key vocabulary, and that the request's order is part of the answer. `count` and `missing` are both emitted on every entry: 0 is the COMMON answer here and `missing` is the only thing that tells a typo from an issue that genuinely has none. THE IDS CAP IS THE ONE THE LEAF ASSIGNED TO THIS SLICE. EdgeCountRequest.IDs says there is no size cap on the role, that the bound belongs to the WIRE, and that "the graph-counts wire slice sets it". It is 100 — the same number, from the same `maxDependencyAnchors`, as the stored-edge read on the same collection, because the two bound the same thing and a client holding both should not have to learn two numbers. And it is now PINNED: nothing kept the document's maxItems and the Go constant together on ANY of the operations that read it, so TestSpecDefaultsMatchSharedConstants grew a case covering all THREE — including listBlockingAnnotations, which enforces the same constant and declares the same maxItems and predates this slice entirely. Pinning only the two operations this slice touched would have gated the copies that happened to be in front of me and left the oldest one free to drift. VALIDATION STAYS IN THE ROLE, which is the opposite of `issues:count` and for a reason the checklist spells out. That operation refuses its one enum at the edge because NO role refusal is reachable; here the role has ONE body on all three legs with ValidateEdgeCountRequest inside it, so four refusals are reachable — a missing or unrecognized direction, a status beside direction=out, an empty id, an unusable type. The handler classifies on the sentinel and names the parameter by re-asking the validator's own questions IN THE VALIDATOR'S OWN ORDER, which is part of that function's contract. Two cases carry two offenders each and assert the FIRST is named; swapping the picker's arms reddens them. The role's own semantics — sum-not-distinct, both dependency planes, the status asymmetry, the missing-anchor rule — are CITED, never restated. A second telling is a second thing to keep true. Config gains a required GraphCounter field on the checklist's terms, `bd serve` binds it beside the issue counter, and TWO stub stores gain the accessor — the checklist's "step with no number", found exactly as it predicts: the read role's hook decorator RECURSES, the recursion lands on a stub that embeds a nil DoltStorage, and the panic arrived the moment `bd serve` began binding the role. storage.RoleFiresHooks needs nothing, because that table takes only decorators that WRAP. THE SECOND STUB IS THE ONE WORTH RECORDING, because it says what the checklist's warning actually costs. serve_source_test.go was found by running the tests a role slice thinks to run; serve_store_identity_test.go's serveIdentityStore has the same nil embed and was found only by the FULL-SUITE macOS shard, because no test name in that file matches a pattern anyone would guess (TestServeAnswersFromTheStoreTheRootCommandOpened). #5508's own message recorded this same gap — "I ran the proxied shards by name and never the whole cmd/bd package" — and I repeated it. The rule that actually holds is the checklist's: GREP FOR THE EMBED, NOT FOR THE INTERFACE. Both stubs are now swept, and a tree-wide sweep for a type declaring Counter() without GraphCounter() finds no third. The structural fix is deliberately NOT in this slice: as long as these stubs embed the whole storage.DoltStorage interface, every future read role is a promoted method on a nil interface — a segfault rather than a compile error. Replacing the embed with an explicit role-accessor subset would turn the next one into a build failure, and that is its own change. ONE NICE PROOF OF #5508's WORK: the capability list needed ZERO hand edits in Go. routes.go's diff is a table row and nothing else — `Capabilities()` is untouched — and TestSpecCapabilityVocabularyMatchesTheRouteTable passes, which means the derived list picked `dependencies.count` up on its own. The only hand edit was the document's own prose vocabulary, which is precisely what that gate exists to demand. Evidence. Three mutations, each red in the right place: the picker's arms reordered reddens the two-offender cases and nothing else; `omitempty` on `count`/`missing` reddens the per-anchor case (which decodes through pointers for exactly that reason); the anchor cap as `>=` reddens the at-the-cap assertion that the two refusal cases could not see. A fourth, drifting the spec's maxItems to 50, reddens the new shared-constant pin. Real Dolt through a real `bd serve`, five cases: out and in over an ASYMMETRIC fixture (2 and 3, so neither direction can pass by coincidence) with the outbound count reconciled against the rows `GET /v0/beads/dependencies` returns; the TWO-PLANE SUM, where a durable anchor depended on by one durable issue and one wisp counts 2 across `dependencies` and `wisp_dependencies` — a count reading one table answers 1 and looks fine; a missing anchor reported beside two found ones, both of the other two carrying 0 so only the flag separates them; the status filter narrowing inbound edges by the dependent's stored status, an unrecognized status counting 0 rather than failing, and the 400 naming `status` beside direction=out; and a repeated anchor answered once. Gates: `make api-check`, `go build ./...`, `go vet ./...`, `gofmt -l` clean, `golangci-lint run --new-from-rev=origin/main` on the touched packages (0 issues), `internal/httpapi`, `internal/telemetry`, `internal/storage` and `internal/storage/uow` (the nine-minute parity run the checklist names), and `BEADS_TEST_PROXIED_SERVER=1 CGO_ENABLED=1 go test ./cmd/bd/ -run TestProxiedServerServeEdgeCounts` green against real Dolt. The pre-commit hook's `go run golangci-lint@v2.10.1` fails on this host with a toolchain mismatch unrelated to this change, so the hook's contents were run by hand before committing with --no-verify. Stacked on feat/httpapi-read-revision (#5535); rebase after that merges. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Review Sim <ci@beads.test> Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
233 lines
8.7 KiB
Go
233 lines
8.7 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"slices"
|
|
|
|
"github.com/steveyegge/beads/internal/httpapi/apigen"
|
|
"github.com/steveyegge/beads/internal/types"
|
|
"github.com/steveyegge/beads/issueops"
|
|
)
|
|
|
|
// maxDependencyAnchors bounds how many issues one call may ask about.
|
|
//
|
|
// It is the operation's ONLY size bound and it is deliberately on the QUESTION
|
|
// rather than on the answer. `bd dep list` takes no limit, so a `limit`
|
|
// parameter here would make the two front doors answer differently by default;
|
|
// a cap on the answer with no cursor behind it would truncate with no way to
|
|
// fetch the rest. Bounding the anchors bounds the work without doing either.
|
|
const maxDependencyAnchors = 100
|
|
|
|
// handleListDependencies answers GET /v0/beads/dependencies.
|
|
//
|
|
// Everything below the wire is on issueops.EdgeReader: which plane each anchor
|
|
// is probed on, whether an absent anchor is a miss or an error, the type
|
|
// filter, the edge order, and the de-duplication of a repeated id. What stays
|
|
// here is transport — two repeatable parameters, the size bound, and flattening
|
|
// the role's per-anchor answer onto the flat array `bd dep list --json` emits.
|
|
func (s *Server) handleListDependencies(w http.ResponseWriter, r *http.Request) {
|
|
q := newQuery(r.URL.Query())
|
|
|
|
ids := q.list("issue_id")
|
|
req := issueops.EdgeReadRequest{IDs: ids}
|
|
for _, depType := range q.list("type") {
|
|
req.Types = append(req.Types, types.DependencyType(depType))
|
|
}
|
|
|
|
if !s.acceptQuery(w, r, q) {
|
|
return
|
|
}
|
|
// Both bounds are checked here rather than left to the role: they are this
|
|
// operation's own limits, not statements about what a stored-edge read
|
|
// means, and the role answers an empty request with an empty result.
|
|
if len(ids) == 0 {
|
|
requestInfo(r.Context()).refuse("issue_id")
|
|
s.fail(w, r, InvalidArgument("issue_id", ReasonInvalidValue, "name at least one issue_id"))
|
|
return
|
|
}
|
|
if len(ids) > maxDependencyAnchors {
|
|
requestInfo(r.Context()).refuse("issue_id")
|
|
s.fail(w, r, InvalidArgument("issue_id", ReasonInvalidValue,
|
|
fmt.Sprintf("at most %d issue_id values per request, got %d", maxDependencyAnchors, len(ids))))
|
|
return
|
|
}
|
|
|
|
rd, err := s.edgeReader(r)
|
|
if err != nil {
|
|
s.failErr(w, r, err)
|
|
return
|
|
}
|
|
result, err := rd.ReadEdges(r.Context(), req)
|
|
if err != nil {
|
|
s.failEdgeReadErr(w, r, err, ids)
|
|
return
|
|
}
|
|
writeJSON(w, wireEdges(result))
|
|
}
|
|
|
|
// handleCountDependencyEdges answers GET /v0/beads/dependencies:count.
|
|
//
|
|
// Everything below the wire is on issueops.GraphCounter: which plane each
|
|
// anchor is probed on, that an absent anchor is a per-anchor miss rather than
|
|
// an error, that the count spans both dependency planes as a sum, the type and
|
|
// status filters, and the de-duplication of a repeated id. What stays here is
|
|
// transport — three repeatable-or-single parameters, the anchor bound, and
|
|
// projecting the role's answer onto the wire envelope.
|
|
//
|
|
// The DIRECTION is not validated here, deliberately. It is the role's closed
|
|
// vocabulary, and issueops.GraphCounter has ONE body on all three legs with
|
|
// ValidateEdgeCountRequest inside it, so refusing at the edge would be a second
|
|
// definition of the same rule — the opposite of GET /v0/beads/issues:count,
|
|
// where the handler owns its enum precisely because no role refusal is
|
|
// reachable there. Here four of them are, and failEdgeCountErr names the
|
|
// parameter for each.
|
|
func (s *Server) handleCountDependencyEdges(w http.ResponseWriter, r *http.Request) {
|
|
q := newQuery(r.URL.Query())
|
|
|
|
ids := q.list("issue_id")
|
|
req := issueops.EdgeCountRequest{
|
|
IDs: ids,
|
|
Direction: issueops.EdgeDirection(q.str("direction")),
|
|
Status: q.str("status"),
|
|
}
|
|
for _, depType := range q.list("type") {
|
|
req.Types = append(req.Types, types.DependencyType(depType))
|
|
}
|
|
|
|
if !s.acceptQuery(w, r, q) {
|
|
return
|
|
}
|
|
// The anchor bound is this operation's own limit rather than a statement
|
|
// about what an edge count means, exactly as the stored-edge read's is —
|
|
// and it is the SAME bound, from the same constant, because the two
|
|
// operations bound the same thing on the same collection.
|
|
if len(ids) == 0 {
|
|
requestInfo(r.Context()).refuse("issue_id")
|
|
s.fail(w, r, InvalidArgument("issue_id", ReasonInvalidValue, "name at least one issue_id"))
|
|
return
|
|
}
|
|
if len(ids) > maxDependencyAnchors {
|
|
requestInfo(r.Context()).refuse("issue_id")
|
|
s.fail(w, r, InvalidArgument("issue_id", ReasonInvalidValue,
|
|
fmt.Sprintf("at most %d issue_id values per request, got %d", maxDependencyAnchors, len(ids))))
|
|
return
|
|
}
|
|
|
|
counter, err := s.graphCounter(r)
|
|
if err != nil {
|
|
s.failErr(w, r, err)
|
|
return
|
|
}
|
|
result, err := counter.CountEdges(r.Context(), req)
|
|
if err != nil {
|
|
s.failEdgeCountErr(w, r, err, req)
|
|
return
|
|
}
|
|
writeJSON(w, wireEdgeCounts(result))
|
|
}
|
|
|
|
// failEdgeCountErr answers a failed edge count. The role's four
|
|
// request-validation refusals are the caller's own input and reach the client
|
|
// as the 400 they are; everything else keeps going through the one mapping in
|
|
// problem.go.
|
|
//
|
|
// It classifies on the SENTINEL, as failEdgeReadErr does, and picks the
|
|
// parameter to name by re-asking the request the validator's own questions IN
|
|
// THE VALIDATOR'S OWN ORDER. That order is part of ValidateEdgeCountRequest's
|
|
// contract — the direction is checked FIRST so an empty request is a refusal
|
|
// about the direction rather than an empty answer — and a picker that asked in
|
|
// a different order would name a second offender on a request carrying two,
|
|
// sending the caller to fix a parameter the server had not reached.
|
|
func (s *Server) failEdgeCountErr(w http.ResponseWriter, r *http.Request, err error, req issueops.EdgeCountRequest) {
|
|
if !errors.Is(err, issueops.ErrValidation) {
|
|
s.failErr(w, r, err)
|
|
return
|
|
}
|
|
param := "type"
|
|
switch {
|
|
case req.Direction != issueops.EdgeDirectionOut && req.Direction != issueops.EdgeDirectionIn:
|
|
param = "direction"
|
|
case req.Status != "" && req.Direction != issueops.EdgeDirectionIn:
|
|
param = "status"
|
|
case slices.Contains(req.IDs, ""):
|
|
param = "issue_id"
|
|
}
|
|
requestInfo(r.Context()).refuse(param)
|
|
s.fail(w, r, InvalidArgument(param, ReasonInvalidValue, err.Error()))
|
|
}
|
|
|
|
// wireEdgeCounts projects the role's per-anchor answer onto the wire envelope.
|
|
//
|
|
// It does NOT flatten, which is the difference from wireEdges beside it. An
|
|
// edge row carries its own issue_id, so a flat array of rows loses nothing; a
|
|
// number does not, so the per-anchor shape is the answer and folding it would
|
|
// produce a total nobody asked for.
|
|
//
|
|
// `anchors` is non-nil so the body carries `[]` rather than `null`, and both
|
|
// `count` and `missing` are emitted on every entry — 0 is the common answer
|
|
// and `false` is a fact, so an omitted member on either would be ambiguous.
|
|
func wireEdgeCounts(result issueops.EdgeCountResult) apigen.EdgeCounts {
|
|
body := apigen.EdgeCounts{Anchors: []apigen.AnchorEdgeCount{}}
|
|
for _, anchor := range result.Anchors {
|
|
body.Anchors = append(body.Anchors, apigen.AnchorEdgeCount{
|
|
Id: anchor.ID,
|
|
Count: anchor.Count,
|
|
Missing: anchor.Missing,
|
|
})
|
|
}
|
|
return body
|
|
}
|
|
|
|
// failEdgeReadErr answers a failed stored-edge read. The role's two
|
|
// request-validation refusals — an empty id, an unusable dependency type — are
|
|
// the caller's own input and reach the client as the 400 they are; everything
|
|
// else keeps going through the one mapping in problem.go.
|
|
//
|
|
// It classifies on the SENTINEL rather than on prose, unlike the read handlers'
|
|
// invalidFilterParam: this role publishes issueops.ErrValidation for exactly
|
|
// those two cases. Which parameter is named comes from the request the handler
|
|
// still holds — an empty entry can only have come from `issue_id`.
|
|
func (s *Server) failEdgeReadErr(w http.ResponseWriter, r *http.Request, err error, ids []string) {
|
|
if !errors.Is(err, issueops.ErrValidation) {
|
|
s.failErr(w, r, err)
|
|
return
|
|
}
|
|
param := "type"
|
|
for _, id := range ids {
|
|
if id == "" {
|
|
param = "issue_id"
|
|
break
|
|
}
|
|
}
|
|
requestInfo(r.Context()).refuse(param)
|
|
s.fail(w, r, InvalidArgument(param, ReasonInvalidValue, err.Error()))
|
|
}
|
|
|
|
// wireEdges flattens the role's per-anchor answer onto the wire envelope.
|
|
//
|
|
// The element type is an ALIAS of types.Dependency — the same struct the CLI's
|
|
// --json marshals. There is no second wire struct here and there must never be
|
|
// one.
|
|
//
|
|
// Both members are non-nil so the body carries `[]` rather than `null`.
|
|
func wireEdges(result issueops.EdgeReadResult) apigen.DependencyEdges {
|
|
body := apigen.DependencyEdges{
|
|
Items: []apigen.Dependency{},
|
|
Missing: []string{},
|
|
}
|
|
for _, anchor := range result.Anchors {
|
|
if anchor.Missing {
|
|
body.Missing = append(body.Missing, anchor.ID)
|
|
continue
|
|
}
|
|
for _, edge := range anchor.Edges {
|
|
if edge == nil {
|
|
continue
|
|
}
|
|
body.Items = append(body.Items, *edge)
|
|
}
|
|
}
|
|
return body
|
|
}
|