* feat(roles): BatchGetter role and issues:batchGet route (S7)
Adds BatchGetter.GetMany(ids) -> {Issues, Missing}, a one-snapshot,
order-preserving, deduped, 1000-id-capped batch read, wired through all
three library legs (dolt, embedded dolt, uow) behind the existing
hook/telemetry decorator chain, plus the POST /v0/beads/issues:batchGet
HTTP route (spec, apigen, wire-shape golden, issues.batchGet capability
token). Conformance contract (RunBatchGetter*, 12 cases) passes on all
three legs against real backends. No HTTP client leg yet (waits on the
generator / PR #7247); left as a documented seam on BatchGetter's doc
comment.
CLI GetIssuesByIDs call sites are left unmigrated this slice and marked
with TODO(batchgetter) comments: every call site reads an unbounded id
list today, and GetMany's 1000-id cap has no chunking/fallback in any
caller yet, so swapping them in now would add a new, unhandled failure
mode to already-shipped CLI paths (export, import, molecule port,
ready-explain, wisp sweep).
Verified via three live-mutate guards against
internal/storage/issueops/batchget.go (disabling the cap check, dropping
the Missing append, and replacing ordered iteration with map iteration):
each turns the embedded-dolt contract red, confirming the cap, Missing
reporting, and order-preservation assertions actually catch a break.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
* fix(roles): close S7 review gaps — revision on wire, BUILD sync, handler tests
Addresses the S7 Opus review's FIX-FIRST items on BatchGetter/issues:batchGet:
HIGH
- batchGetResponse now projects apigen.BatchGetIssue (types.BatchGetIssue +
NewBatchGetIssue), carrying `revision` per issue the way IssueDetails does;
spec, apigen and the wire-shape golden regenerated (additive, no
wire_revision bump).
- make bazel-sync: the 12 files missing from BUILD.bazel srcs are back in
sync across 9 BUILD.bazel files.
- New internal/httpapi/batch_get_test.go: success incl. revision, missing
ids, empty arrays not null, unknown member refused, missing/null ids
refused, query parameter refused, 1001 ids refused with the full request
reaching the role, 1000 ids accepted.
MED
- RunBatchGetterAcceptsExactlyTheCap pins the cap boundary (> vs >=).
- RunBatchGetterHydratesLabels pins Labels survive projection.
- RunBatchGetterReadsOneSnapshot renamed to
RunBatchGetterSharesOneReadStructurally with a doc that no longer
overclaims atomicity; a real interleaving test needs a hook on the frozen
roleFixtureKit, out of scope here.
- Doc comments across ExecuteGetMany, GetManyResult.Issues and the
BatchGetter interface now say hydration is labels only.
LOW
- failBatchGetErr's doc no longer claims blank entries are "already caught".
- PR #7247 references kept.
Re-ran build/vet/gofmt/lint, make api-check, make bazel-sync(-check), the
full internal/httpapi suite, and all three conformance legs (dolt via the
pinned local server, embeddeddolt, uow) plus targeted cmd/bd serve tests —
all green. Live-mutate-guard reverified: cap >=, dropping Missing from the
response, and clearing Labels each now fail the relevant test.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
* fix(roles): sync client vocabulary and repo gates with issues:batchGet
The S7 route landed on the server without the sibling tables that hold
the rest of the tree set-equal with the server's surface, so five
tests and two manifest gates went red on the PR head:
- httpclient/wire: mirror batchGetIssues into the client vocabulary,
CLASSIFIED, not wired, the way the wire wave and #7250's
issues.count.scope landed: OpBatchGetIssues, its issues.batchGet
token on opCapability, its identity row, and the first-slice skew
dimension 35 -> 36 (TestCapabilityTableMatchesTheServerRouteTable).
- httpclient/wire: BatchGetIssue is the first golden.json schema with
a `revision` member that needs no legacy-integer tolerance — the
route postdates #6053 and types.BatchGetIssue declares Revision a
string, and a pre-#6053 server never advertises issues.batchGet, so
Preflight refuses before any body is decoded. Exempt it with that
reason in a new revisionSchemaExempt table rather than adding an
issues[] rewrite no legacy server can reach; the gate refuses an
entry that is both covered and exempt, a stale exemption, and an
empty reason.
- httpclient/encode: give the oracle's role set a BatchGetter stub —
httpapi.Listen requires the whole set, so all three oracle tests
failed to bind.
- storage/uow: exempt IssueUseCase.GetMany from the notifying-parity
partition as a read, the reason hook_batch_getter.go gives.
- shard manifest: place TestBatchGetterContract in the 15-shard
embedded storage block (generator --write, incremental).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* test(roles): pin the BatchGetter pure tier and the spec ids caps
Review fixes, iteration 1. The review approved with no blocker or major
finding and three minors, and this commit takes all three. It changes
tests and one comment only, and every new assertion was mutation-probed
with go test -overlay.
- storage/issueops: add batchget_test.go, the database-free tier that
FinishGetMany's doc already claimed ("pinned without a database") but
that no test supplied. It pins:
- the cap is checked before the blank scan (an over-cap request with
a blank at position 0 answers *TooManyIDsError);
- the bound is inclusive;
- dedupe keeps the first mention, does not trim, and never writes
through the caller's slice;
- Issues and Missing both follow request order;
- both slices are never nil;
- ExecuteGetMany refuses one id repeated past the cap before any
query (a sqlmock with no expectations).
Ten mutants were each killed by exactly their intended test, with the
rest of the package green: last-mention, sort, trim, in-place dedupe,
blank-before-cap, >=, nil Missing, nil Issues, dedupe-before-validate
and validate-after-query.
- httpapi: TestSpecDefaultsMatchSharedConstants now checks ids.maxItems
against the enforced bound on the two request bodies that carry an
`ids` list: batchGetIssues against issueops.MaxGetManyIDs, and
deleteIssues against maxDeleteIDs. Each body is reached through its
operation's own requestBody $ref. Setting either constant to 999 turns
only this test red. The spec's other list caps (batch
create/close/apply items, dependency edge lists) stay ungated, as
before.
- cmd/bd/serve.go: the batchGetter field comment said its accessor
"does NOT recurse through the hook decorator". HookFiringStore.
BatchGetter does recurse to the inner getter; it just doesn't wrap
the result. The comment now says that.
Agent-Signature: claude-code-claude-opus-5-5-max on behalf of Julian Knutsen
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
303 lines
11 KiB
Go
303 lines
11 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/steveyegge/beads/internal/types"
|
|
"github.com/steveyegge/beads/issueops"
|
|
)
|
|
|
|
// These cover the transport half of POST /v0/beads/issues:batchGet — the body
|
|
// shape, this operation's own bounds, and how the role's result is projected
|
|
// onto the wire. Everything below the wire (the cap, deduplication, snapshot
|
|
// sharing, labels-only hydration) is issueops.BatchGetter's, pinned by
|
|
// backend/conformance/batch_getter_contract.go at all three backends.
|
|
|
|
const batchGetPath = "/v0/beads/issues:batchGet"
|
|
|
|
func newBatchGetServer(t *testing.T, getter *roleBatchGetter) *testServer {
|
|
t.Helper()
|
|
return newTestServer(t, rolesConfig(Config{BatchGetter: getter}))
|
|
}
|
|
|
|
func batchGetIssue(id string, rowVersion int64) *types.Issue {
|
|
return &types.Issue{
|
|
ID: id,
|
|
Title: "title for " + id,
|
|
Priority: 1,
|
|
IssueType: types.TypeTask,
|
|
Status: types.StatusOpen,
|
|
Labels: []string{"api"},
|
|
RowVersion: rowVersion,
|
|
CreatedAt: time.Date(2026, 7, 31, 12, 0, 0, 0, time.UTC),
|
|
UpdatedAt: time.Date(2026, 7, 31, 12, 0, 0, 0, time.UTC),
|
|
}
|
|
}
|
|
|
|
// TestBatchGetIssuesAnswersWithRevision pins HIGH item 1: each item in
|
|
// `issues` carries its own `revision`, projected from the issue's RowVersion
|
|
// the same way IssueDetails.Revision is for GET /v0/beads/issues/{id}. The
|
|
// fixture uses guardToken, past 2^53, so a response member that had been
|
|
// decoded (or encoded) as a float64 anywhere on this path would read back a
|
|
// NEARBY number rather than the real one.
|
|
func TestBatchGetIssuesAnswersWithRevision(t *testing.T) {
|
|
issue := batchGetIssue("bd-1", guardToken)
|
|
getter := &roleBatchGetter{result: issueops.GetManyResult{
|
|
Issues: []*types.Issue{issue},
|
|
Missing: []string{},
|
|
}}
|
|
ts := newBatchGetServer(t, getter)
|
|
|
|
resp := ts.claim(t, batchGetPath, `{"ids":["bd-1"]}`)
|
|
if resp.StatusCode != http.StatusOK {
|
|
t.Fatalf("status = %d, want 200: %s", resp.StatusCode, readAll(t, resp))
|
|
}
|
|
raw := readAll(t, resp)
|
|
var body struct {
|
|
Issues []struct {
|
|
ID *string `json:"id"`
|
|
Revision *string `json:"revision"`
|
|
} `json:"issues"`
|
|
Missing []string `json:"missing"`
|
|
}
|
|
if err := json.Unmarshal([]byte(raw), &body); err != nil {
|
|
t.Fatalf("decode %q: %v", raw, err)
|
|
}
|
|
if len(body.Issues) != 1 {
|
|
t.Fatalf("issues = %v, want 1 entry: %s", body.Issues, raw)
|
|
}
|
|
got := body.Issues[0]
|
|
if got.ID == nil || *got.ID != "bd-1" {
|
|
t.Errorf("id = %v, want bd-1", got.ID)
|
|
}
|
|
if got.Revision == nil {
|
|
t.Fatalf("the issue carries no `revision`: %s", raw)
|
|
}
|
|
token, err := types.ParseRevisionToken(*got.Revision)
|
|
if err != nil {
|
|
t.Fatalf("parse revision %q: %v", *got.Revision, err)
|
|
}
|
|
if token != guardToken {
|
|
t.Errorf("revision = %d, want the row's %d", token, guardToken)
|
|
}
|
|
if len(body.Missing) != 0 {
|
|
t.Errorf("missing = %v, want none", body.Missing)
|
|
}
|
|
|
|
reqs := getter.getManyRequests()
|
|
if len(reqs) != 1 || len(reqs[0].IDs) != 1 || reqs[0].IDs[0] != "bd-1" {
|
|
t.Fatalf("role calls = %+v, want one call naming bd-1", reqs)
|
|
}
|
|
}
|
|
|
|
// TestBatchGetIssuesReportsMissingIDs pins that an id naming no stored row
|
|
// comes back in `missing` rather than vanishing or refusing the request, and
|
|
// that a found id and a missing one can ride in the same answer.
|
|
func TestBatchGetIssuesReportsMissingIDs(t *testing.T) {
|
|
issue := batchGetIssue("bd-1", 1)
|
|
getter := &roleBatchGetter{result: issueops.GetManyResult{
|
|
Issues: []*types.Issue{issue},
|
|
Missing: []string{"bd-gone"},
|
|
}}
|
|
ts := newBatchGetServer(t, getter)
|
|
|
|
resp := ts.claim(t, batchGetPath, `{"ids":["bd-1","bd-gone"]}`)
|
|
if resp.StatusCode != http.StatusOK {
|
|
t.Fatalf("status = %d, want 200: %s", resp.StatusCode, readAll(t, resp))
|
|
}
|
|
body := decodeBody(t, resp)
|
|
missing, _ := body["missing"].([]any)
|
|
if len(missing) != 1 || missing[0] != "bd-gone" {
|
|
t.Errorf("missing = %v, want [bd-gone]", body["missing"])
|
|
}
|
|
issues, _ := body["issues"].([]any)
|
|
if len(issues) != 1 {
|
|
t.Errorf("issues = %v, want exactly the one found row", body["issues"])
|
|
}
|
|
}
|
|
|
|
// TestBatchGetIssuesAnswersEmptyArraysNotNull pins the one thing a Go nil
|
|
// slice gets wrong on the way out, for BOTH members: a client is entitled to
|
|
// range over `issues` and `missing` without a nil check even when neither
|
|
// carries anything.
|
|
func TestBatchGetIssuesAnswersEmptyArraysNotNull(t *testing.T) {
|
|
getter := &roleBatchGetter{result: issueops.GetManyResult{
|
|
Issues: []*types.Issue{},
|
|
Missing: []string{"bd-gone"},
|
|
}}
|
|
ts := newBatchGetServer(t, getter)
|
|
|
|
resp := ts.claim(t, batchGetPath, `{"ids":["bd-gone"]}`)
|
|
if resp.StatusCode != http.StatusOK {
|
|
t.Fatalf("status = %d, want 200: %s", resp.StatusCode, readAll(t, resp))
|
|
}
|
|
raw := readAll(t, resp)
|
|
if !strings.Contains(raw, `"issues":[]`) {
|
|
t.Errorf("body = %s, want an empty `issues` array rather than null", raw)
|
|
}
|
|
|
|
allMissing := &roleBatchGetter{result: issueops.GetManyResult{
|
|
Issues: []*types.Issue{},
|
|
Missing: []string{},
|
|
}}
|
|
ts2 := newBatchGetServer(t, allMissing)
|
|
resp2 := ts2.claim(t, batchGetPath, `{"ids":[]}`)
|
|
if resp2.StatusCode != http.StatusOK {
|
|
t.Fatalf("status = %d, want 200: %s", resp2.StatusCode, readAll(t, resp2))
|
|
}
|
|
raw2 := readAll(t, resp2)
|
|
if !strings.Contains(raw2, `"missing":[]`) {
|
|
t.Errorf("body = %s, want an empty `missing` array rather than null", raw2)
|
|
}
|
|
}
|
|
|
|
// TestBatchGetIssuesRefusesAnUnknownMember pins that the body is
|
|
// additionalProperties: false and the refusal names the offending member: `ids`
|
|
// is the whole vocabulary.
|
|
func TestBatchGetIssuesRefusesAnUnknownMember(t *testing.T) {
|
|
getter := &roleBatchGetter{}
|
|
ts := newBatchGetServer(t, getter)
|
|
|
|
resp := ts.claim(t, batchGetPath, `{"ids":["bd-1"],"dry_run":true}`)
|
|
if resp.StatusCode != http.StatusBadRequest {
|
|
t.Fatalf("status = %d, want 400: %s", resp.StatusCode, readAll(t, resp))
|
|
}
|
|
body := decodeBody(t, resp)
|
|
if body["code"] != string(CodeInvalidArgument) {
|
|
t.Errorf("code = %v, want %q", body["code"], CodeInvalidArgument)
|
|
}
|
|
if body["param"] != "dry_run" {
|
|
t.Errorf("param = %v, want dry_run", body["param"])
|
|
}
|
|
if calls := getter.getManyRequests(); len(calls) != 0 {
|
|
t.Errorf("the role was called %d times for a refused request", len(calls))
|
|
}
|
|
}
|
|
|
|
// TestBatchGetIssuesRefusesAMissingOrNullIDs pins that `ids` is required and
|
|
// must actually be an array: absent and explicit null are both refused, before
|
|
// the role is ever reached.
|
|
func TestBatchGetIssuesRefusesAMissingOrNullIDs(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name string
|
|
body string
|
|
}{
|
|
{"no ids member at all", `{}`},
|
|
{"ids is explicitly null", `{"ids":null}`},
|
|
{"ids is a string, not an array", `{"ids":"bd-1"}`},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
getter := &roleBatchGetter{}
|
|
ts := newBatchGetServer(t, getter)
|
|
|
|
resp := ts.claim(t, batchGetPath, tc.body)
|
|
if resp.StatusCode != http.StatusBadRequest {
|
|
t.Fatalf("status = %d, want 400: %s", resp.StatusCode, readAll(t, resp))
|
|
}
|
|
body := decodeBody(t, resp)
|
|
if body["code"] != string(CodeInvalidArgument) {
|
|
t.Errorf("code = %v, want %q", body["code"], CodeInvalidArgument)
|
|
}
|
|
if body["param"] != "ids" {
|
|
t.Errorf("param = %v, want ids", body["param"])
|
|
}
|
|
if calls := getter.getManyRequests(); len(calls) != 0 {
|
|
t.Errorf("the role was called %d times for a refused request", len(calls))
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestBatchGetIssuesRefusesAQueryParameter pins that the document-level
|
|
// unknown-parameter rule reaches this operation too: it declares no
|
|
// parameters, so every query key is refused.
|
|
func TestBatchGetIssuesRefusesAQueryParameter(t *testing.T) {
|
|
getter := &roleBatchGetter{}
|
|
ts := newBatchGetServer(t, getter)
|
|
|
|
resp := ts.claim(t, batchGetPath+"?dry_run=true", `{"ids":["bd-1"]}`)
|
|
if resp.StatusCode != http.StatusBadRequest {
|
|
t.Fatalf("status = %d, want 400: %s", resp.StatusCode, readAll(t, resp))
|
|
}
|
|
body := decodeBody(t, resp)
|
|
if body["param"] != "dry_run" || body["reason"] != string(ReasonUnknownParameter) {
|
|
t.Errorf("param/reason = %v/%v, want dry_run/unknown_parameter", body["param"], body["reason"])
|
|
}
|
|
if calls := getter.getManyRequests(); len(calls) != 0 {
|
|
t.Errorf("the role was called %d times for a refused request", len(calls))
|
|
}
|
|
}
|
|
|
|
// TestBatchGetIssuesRefusesOverTheCap pins the wire's half of MaxGetManyIDs:
|
|
// the fake stands in for the role's own refusal (as
|
|
// TestCountDependencyEdgesNamesTheRefusedParameter does for GraphCounter),
|
|
// and the handler still answers 400 with the error envelope the operation's
|
|
// only member takes, naming the FULL 1001-entry request as sent.
|
|
func TestBatchGetIssuesRefusesOverTheCap(t *testing.T) {
|
|
const over = issueops.MaxGetManyIDs + 1
|
|
getter := &roleBatchGetter{err: &issueops.TooManyIDsError{Requested: over, Cap: issueops.MaxGetManyIDs}}
|
|
ts := newBatchGetServer(t, getter)
|
|
|
|
ids := make([]string, over)
|
|
for i := range ids {
|
|
ids[i] = fmt.Sprintf(`"bd-%d"`, i)
|
|
}
|
|
resp := ts.claim(t, batchGetPath, `{"ids":[`+strings.Join(ids, ",")+`]}`)
|
|
if resp.StatusCode != http.StatusBadRequest {
|
|
t.Fatalf("status = %d, want 400: %s", resp.StatusCode, readAll(t, resp))
|
|
}
|
|
body := decodeBody(t, resp)
|
|
if body["code"] != string(CodeInvalidArgument) {
|
|
t.Errorf("code = %v, want %q", body["code"], CodeInvalidArgument)
|
|
}
|
|
if body["param"] != "ids" {
|
|
t.Errorf("param = %v, want ids", body["param"])
|
|
}
|
|
if body["reason"] != string(ReasonInvalidValue) {
|
|
t.Errorf("reason = %v, want invalid_value", body["reason"])
|
|
}
|
|
|
|
reqs := getter.getManyRequests()
|
|
if len(reqs) != 1 {
|
|
t.Fatalf("role calls = %d, want 1: the role, not the handler, is where this is counted", len(reqs))
|
|
}
|
|
if len(reqs[0].IDs) != over {
|
|
t.Errorf("the role received %d ids, want the full %d sent: the handler must not pre-trim the request", len(reqs[0].IDs), over)
|
|
}
|
|
}
|
|
|
|
// TestBatchGetIssuesAcceptsExactlyTheCap pins the wire's half of the
|
|
// boundary MED item 4 names at the role: a request at exactly MaxGetManyIDs
|
|
// is not refused by anything above the role, so every id reaches it.
|
|
func TestBatchGetIssuesAcceptsExactlyTheCap(t *testing.T) {
|
|
const atCap = issueops.MaxGetManyIDs
|
|
|
|
ids := make([]string, atCap)
|
|
issues := make([]*types.Issue, atCap)
|
|
for i := range ids {
|
|
id := fmt.Sprintf("bd-%d", i)
|
|
ids[i] = `"` + id + `"`
|
|
issues[i] = batchGetIssue(id, 1)
|
|
}
|
|
getter := &roleBatchGetter{result: issueops.GetManyResult{
|
|
Issues: issues,
|
|
Missing: []string{},
|
|
}}
|
|
ts := newBatchGetServer(t, getter)
|
|
|
|
resp := ts.claim(t, batchGetPath, `{"ids":[`+strings.Join(ids, ",")+`]}`)
|
|
if resp.StatusCode != http.StatusOK {
|
|
t.Fatalf("at the cap: status = %d, want 200: %s", resp.StatusCode, readAll(t, resp))
|
|
}
|
|
reqs := getter.getManyRequests()
|
|
if len(reqs) != 1 || len(reqs[0].IDs) != atCap {
|
|
t.Fatalf("the role received %d ids, want the full %d at the cap", len(reqs[0].IDs), atCap)
|
|
}
|
|
}
|
|
|
|
var _ issueops.BatchGetter = (*roleBatchGetter)(nil)
|