beads/internal/httpapi/claim_next_test.go
Julian Knutsen 601be8a6bf
feat(list): bd list's remaining filters over http behind issues.list.filters (#7647)
* feat(list): bd list's remaining filters over http behind issues.list.filters

Over http every `bd list` filter beyond the created bounds and the title,
id and no-assignee scope refused (time bounds, priority range, text and external-ref
matches, spec, label pattern/regex, exclude-type, no-parent, no-labels,
empty-description, pinned/no-pinned, deferred, overdue, mol/wisp type).
The library entry already applied all of them: issueops.Reader.List via
workapi.BuildListFilter, shared by the CLI and the handler. This adds
only the wire.

- Server: GET /v0/beads/issues decodes 29 new optional parameters
  (listFilterParams, decode only; countIssues' names where it already
  spells the filter) into the existing ListRequest, behind one new
  behavior token, issues.list.filters. mol_type/wisp_type are closed
  enums refused by name (an empty value too). wire_revision stays 2;
  apigen and the wireshape golden are regenerated (additive only).
- Every RFC 3339 bound the listing and count decode is normalized to UTC,
  so an offset-bearing bound selects what its Z twin does on a zone-less
  timestamp column.
- workapi.BuildListFilter refuses PinnedFlag with NoPinnedFlag
  (ErrValidation, the CLI's own sentence) instead of letting PinnedFlag
  win; over http that is a 400 on no_pinned.
- Client: the encoder sends the members (encode.ListFiltersMembers, one
  list read by the encoder, the pre-dial gate and a spec-parity test).
  Against a server not advertising the token, httpReader.List refuses
  before dialing with ErrUnsupported naming issues.list.filters. The 29
  E-ListRequest ledger refusal rows retire; the ledger doc is updated;
  skew flag mappings added.
- Tests: handler decode/parity/enum/UTC/malformed/pinned-pair tests;
  encode member/gate/spec parity and bound encoding; per-member
  ride-behind-token and pre-dial skew refusal; two new Reader contract
  cases on every leg (dolt, embedded, uow, served): the eight time
  bounds at exact boundaries (strict, whole-second truncation, NULL never
  matches, +02:00 equals UTC) and one in/out row per remaining family;
  three served Reader cases unparked; TestE2E_ListFilterParityOverHTTP
  (served == embedded, masked server refuses naming the token, no dial);
  five corpus shapes marked pending until the reference server profile
  advertises the token.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* feat(list): exclude_status on listIssues, the 30th issues.list.filters member

issueops.ListRequest gains ExcludeStatus: statuses to leave out of a
listing, appended to the default exclusions (never re-admitting what they
hide), comma-separated or repeated, and validated against the workspace's
statuses by the same rule CountRequest.ExcludeStatus uses (an unknown name
is ErrValidation). The rule moves into one shared function,
workapi.ParseExcludeStatus, called by both builders. With AllFlag it reads
the active set without enumerating every status, so an http-backed caller
can list O(active) rows instead of the whole history.

- Server: listIssues decodes a repeatable `exclude_status` under the same
  issues.list.filters token (no new token); an unknown name is a 400 on
  exclude_status. apigen and the wireshape golden are regenerated
  (additive).
- Client: the encoder sends it and it joins encode.ListFiltersMembers, so
  the pre-dial gate asks for the token.
- It is refused together with ReadyFlag, like the other members the ready
  query cannot carry.
- No `bd list --exclude-status` flag yet (it would need the CLI docs
  regenerated); the member is reachable through the library and the wire.

Tests: builder unit test (append, AllFlag, comma split, unknown name);
handler decode and 400; encoder, round-trip and gate; Reader contract
cases on every leg (active set under AllFlag, repeated and comma values,
adds to the defaults, narrows an explicit Status, unknown name refused).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix(list): label_regex compile errors are 400s; review polish for list filters

- An invalid `label_regex` on GET /v0/beads/issues, /v0/beads/ready and
  /v0/beads/ready:count is now `400 invalid_argument` (`invalid_value` on
  `label_regex`) instead of `500 internal`. The database still compiles
  the pattern in its own dialect; a request that sent a regex and earned
  the engine's "regular expression is invalid" refusal is attributed to
  the parameter (failRegexReadErr). The same text on a request with no
  regex stays a server fault.
- mol_type and wisp_type refuse each other's vocabulary (tests pin
  `mol_type=heartbeat` and `wisp_type=swarm`), so the call-site
  allowlist, not just the spec enum, is covered.
- exclude_status with a workspace custom status: leg tests on the
  embedded store and over the served wire (accepted and applied; a name in
  no vocabulary refused). ReaderFixture cannot configure custom statuses,
  so these sit beside the count's twin rather than in the contract.
- The search e2e gains a positive arm: --desc-contains returns exactly the
  row whose description carries the token.
- Docs: `all` plus `exclude_status` keeps every status not named (pinned,
  hooked and custom done/frozen included unless named), not "the active
  set"; label_regex documents its 400. A corpus source line is made
  neutral; a test message says 30.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix(list): compose issues.list.filters with the --title-contains fold

#7631, now on main, folds `bd list --title-contains` onto listIssues
`title` and refuses the TitleContains left over from a --title and
--title-contains pair that differs, because the listing then had one
title parameter. This branch publishes `title_contains` behind
issues.list.filters, so that leftover now rides it beside `title` and
the server applies both. Neither half is dropped, and against a server
without the token the pre-dial gate refuses, naming issues.list.filters.

- encode.FoldTitleContains still runs first; its doc now says where a
  differing pair goes.
- Tests: the fold table expects `title` plus `title_contains` for a
  differing pair, including the ſ/s pair strings.EqualFold would match;
  the differing-pair refusal names issues.list.filters; the
  list_filters_test sweep pairs its TitleContains probe with a differing
  TitleSearch, since a lone one asks only for issues.list.title; the e2e
  differing-pair refusal becomes a listFilterShapes row that answers
  exactly child-a over http and embedded, and refuses before dialing
  without the token.
- The gc-polecat-closed-after corpus shape's pending reason also names
  issues.list.title. The CHANGELOG --title-contains entry no longer
  lists the differing pair and --closed-after as refused.

Agent-Signature: claude-code-claude-opus-5-5-max on behalf of Julian Knutsen
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* build(bazel): auto-sync BUILD files

Applied from the bazel-sync-patch artifact of https://github.com/gastownhall/beads/actions/runs/39013611268
(`make bazel-sync`: gazelle, tools/bazel/go_srcs.py, bazel mod tidy).
Only BUILD.bazel, MODULE.bazel and MODULE.bazel.lock are ever pushed; see
scripts/bazel-autofix-push.sh.

* fix(list): serve --priority-max in search parity, refuse the parent pair

The proxied tier's search parity table still expected a remote
`bd search --priority-max 1` to refuse, because listIssues could not
carry it. This branch serves priority_max behind issues.list.filters,
so the remote route answered and the dolt-race shard went red in
TestProxiedServerSearchRoutesAgree. The row now expects the answer on
every route. It was the last wireCan=false row; the branch stays, with
a comment, for the next flag listIssues cannot carry.

- ParentID with NoParent: a served listing now carries no_parent beside
  parent, and ANDing the two answered only the edge-less rows under the
  parent's dotted id. BuildListFilter refuses the pair with
  ErrValidation, in the sentence bd list and BuildCountFilter already
  use, so issueops.Reader.List refuses it on every backend and the
  listing answers 400 on no_parent. Pinned by a builder test, a reader
  conformance case (dolt, embedded, uow, served http) and two httpapi
  tests.
- POST /v0/beads/issues:claimNext shares the ready listing's
  label_regex but answered a pattern the database cannot compile with
  500. It now gives the listing's 400 invalid_value on label_regex,
  through the helper the listing's mapping is split into.
- The descendant walk's ledger rows said listIssues publishes no
  parameter for members it now serves behind issues.list.filters. They
  now say the walk does not send them yet, and that --flat or --json
  asks listIssues directly. The ExcludeStatus, Pinned and ExcludeTypes
  rows, the parentwalk.go header, the walk table's doc and two walk test
  comments are corrected the same way, and the ledger doc is
  regenerated. parentWalkRefusals notes that a member moved off the list
  must check issues.list.filters itself, because the walk dials past
  the listing's pre-dial gate.
- Spec: the listing's no_parent documents the pair's 400, the count's
  exclude_status no longer says it has no listing counterpart, and
  claimNext's label_regex documents its 400. types.gen.go is
  regenerated. Comment-only: countFilters, and readyScopeFields'
  ExcludeStatus entry.
- CHANGELOG and README say the default `bd list --parent` tree view
  does not carry the new filters yet; the label_regex entry names
  claimNext, and a Changed entry records the parent-pair refusal.

Second review pass: docs and comments only, no behavior change.

- The prose copies of the issues.list.filters member set now match
  ListFiltersMembers: CapIssuesListFilters and wire.CapListFilters name
  the status exclusion, and the spec's capabilities text names
  label-regex.
- Every time bound on the listing and the count goes through
  q.timestamp, which normalizes it to UTC, and sqlbuild compares it
  strictly at whole-second precision. listIssues' created_before and
  created_after, and countIssues' created, updated and closed bounds,
  now say so in the words the new listing bounds use. The count's
  updated bounds also say what they filter, and its closed bounds say
  that a NULL closed_at never matches. types.gen.go is regenerated.
- README and CHANGELOG: `--flat` or `--json` applies the filters to the
  parent's direct children, not its whole subtree.
- The 29 listRefusal rows this branch serves stay deleted rather than
  kept as retired rows, as #7550 deleted its TitleSearch, IDFilter and
  NoAssignee rows when it served them. The CHANGELOG now says the rows
  are removed, not retired.

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 Opus 5.5 (1M context) <noreply@anthropic.com>
Co-authored-by: Eddie the Engineer <338826946+gascity-eddie[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
2026-10-17 11:21:18 +00:00

302 lines
13 KiB
Go

package httpapi
import (
"errors"
"fmt"
"net/http"
"net/url"
"strings"
"testing"
"github.com/steveyegge/beads/internal/types"
"github.com/steveyegge/beads/issueops"
)
// These are pure: the claimNext path runs end to end over a real listener
// against a fake ROLE, so the wire edge — the filter decode, the `limit`
// refusal, the body vocabulary, the absent-row answer — is covered on every
// pull request.
//
// What a fake cannot prove is the ATOMICITY that is the operation's entire
// reason for existing, and it is owned where it can be: the role-level contract
// against a real store (backend/conformance/ready_claimer_contract.go, run by
// internal/storage/uow). This slice cites that rather than pretending a fake
// can race itself.
const claimNextPath = "/v0/beads/issues:claimNext"
func (ts *testServer) claimNext(t *testing.T, path, body string) *http.Response {
t.Helper()
return ts.postBody(t, path, "application/json", body)
}
func newClaimNextServer(t *testing.T, claimer *roleReadyClaimer) *testServer {
t.Helper()
return newTestServer(t, rolesConfig(Config{ReadyClaimer: claimer}))
}
func readyRow(id string) *issueops.IssueWithCounts {
return &issueops.IssueWithCounts{Issue: seededIssue(id, "poller", types.StatusInProgress)}
}
// TestClaimNextAnswersWithTheRowItWon is the happy path: the role receives the
// actor, and the response carries the hydrated row under `claimed`.
func TestClaimNextAnswersWithTheRowItWon(t *testing.T) {
claimer := &roleReadyClaimer{result: issueops.ClaimNextResult{Claimed: readyRow("bd-1")}}
ts := newClaimNextServer(t, claimer)
resp := ts.claimNext(t, claimNextPath, `{"actor":"poller"}`)
if resp.StatusCode != http.StatusOK {
t.Fatalf("status = %d, want 200: %s", resp.StatusCode, readAll(t, resp))
}
got := claimer.claimNextRequests()
if len(got) != 1 || got[0].Actor != "poller" {
t.Fatalf("the role received %+v, want exactly one claim by poller", got)
}
body := decodeBody(t, resp)
claimed, ok := body["claimed"].(map[string]any)
if !ok {
t.Fatalf("claimed = %#v, want an object", body["claimed"])
}
if claimed["id"] != "bd-1" {
t.Errorf("claimed.id = %v, want bd-1", claimed["id"])
}
}
// TestClaimNextAnswersAbsenceForADrainedQueue is the operation's other normal
// answer, and the one a polling client is written against.
//
// A drained queue is the STEADY STATE of a work loop, not a failure, so it is a
// 200 with the member absent rather than a 404 or a 409. `claimIssue` 404s an
// id that names nothing because it was asked about a ROW; this one was asked a
// question whose honest answer can be "none".
func TestClaimNextAnswersAbsenceForADrainedQueue(t *testing.T) {
claimer := &roleReadyClaimer{result: issueops.ClaimNextResult{Claimed: nil}}
ts := newClaimNextServer(t, claimer)
resp := ts.claimNext(t, claimNextPath, `{"actor":"poller"}`)
if resp.StatusCode != http.StatusOK {
t.Fatalf("status = %d, want 200 for an empty ready front: %s", resp.StatusCode, readAll(t, resp))
}
body := decodeBody(t, resp)
if _, present := body["claimed"]; present {
t.Errorf("claimed = %#v; absence is the whole signal and a null is a second spelling of it", body["claimed"])
}
// And nothing else is invented beside it. A boolean or a scan count would
// be a second member carrying the same fact, which is a second member that
// can disagree with the first.
if len(body) != 0 {
t.Errorf("the empty answer carries %v; `claimed` is the only member this response has", body)
}
}
// TestClaimNextForwardsTheListingsFilters is the guarantee the shared decoder
// exists for, driven through a real request: every filter the ready listing
// admits reaches the role, spelled the same way.
func TestClaimNextForwardsTheListingsFilters(t *testing.T) {
claimer := &roleReadyClaimer{result: issueops.ClaimNextResult{Claimed: readyRow("bd-1")}}
ts := newClaimNextServer(t, claimer)
query := "?assignee=alice&unassigned=true&type=task&exclude_type=gate" +
"&label=urgent&label_any=a&exclude_label=wip&label_pattern=re*&label_regex=^re" +
"&priority=1&parent=bd-9&metadata_field=k%3Dv&has_metadata_key=owner" +
"&include_ephemeral=true&include_deferred=true&sort=oldest"
resp := ts.claimNext(t, claimNextPath+query, `{"actor":"poller"}`)
if resp.StatusCode != http.StatusOK {
t.Fatalf("status = %d, want 200: %s", resp.StatusCode, readAll(t, resp))
}
got := claimer.claimNextRequests()
if len(got) != 1 {
t.Fatalf("the role received %+v, want exactly one request", got)
}
f := got[0].Filter
switch {
case f.Assignee != "alice", !f.Unassigned, f.IssueType != "task":
t.Errorf("the identity filters did not reach the role: %+v", f)
case len(f.ExcludeTypes) != 1, len(f.Labels) != 1, len(f.LabelsAny) != 1, len(f.ExcludeLabels) != 1:
t.Errorf("the list filters did not reach the role: %+v", f)
case f.LabelPattern != "re*", f.LabelRegex != "^re":
t.Errorf("the label matchers did not reach the role: %+v", f)
case f.Priority == nil || *f.Priority != 1, f.ParentID != "bd-9":
t.Errorf("the graph filters did not reach the role: %+v", f)
case f.MetadataFields["k"] != "v", f.HasMetadataKey != "owner":
t.Errorf("the metadata filters did not reach the role: %+v", f)
case !f.IncludeEphemeral, !f.IncludeDeferred:
t.Errorf("the plane filters did not reach the role: %+v", f)
case f.Sort != "oldest":
t.Errorf("Sort = %q, want oldest", f.Sort)
}
// The role refuses both, and this operation publishes no spelling for
// either, so the request it builds must never carry one.
if f.Limit != nil || f.Offset != 0 {
t.Errorf("the handler sent a page to a role that refuses one: limit=%v offset=%d", f.Limit, f.Offset)
}
}
// TestClaimNextSendsAnExplicitSortPolicy pins the value rather than the
// constant, for the reason handleReady's own test gives: an assertion against
// the value the handler read would hold for whatever value it took.
//
// It matters more here than on the listing. There, the sort decides what order
// rows are PRINTED in; here it decides which row the operation WRITES to.
func TestClaimNextSendsAnExplicitSortPolicy(t *testing.T) {
claimer := &roleReadyClaimer{result: issueops.ClaimNextResult{Claimed: readyRow("bd-1")}}
ts := newClaimNextServer(t, claimer)
if resp := ts.claimNext(t, claimNextPath, `{"actor":"poller"}`); resp.StatusCode != http.StatusOK {
t.Fatalf("status = %d, want 200: %s", resp.StatusCode, readAll(t, resp))
}
got := claimer.claimNextRequests()
if len(got) != 1 || got[0].Filter.Sort != "priority" {
t.Fatalf("Sort = %q, want the document's default sent explicitly; an empty policy adopts storage's hybrid fallback",
got[0].Filter.Sort)
}
}
// TestClaimNextRefusesALimitByValue is the one parameter this operation turns
// down that its sibling accepts, and the refusal's REASON is the assertion.
//
// `unknown_parameter` would tell a client "this server does not know that name
// — version skew, degrade or fall back", which is false: the name is one this
// server knows perfectly well on the listing. `invalid_value` says what is
// true, which is that this operation will not act on it.
func TestClaimNextRefusesALimitByValue(t *testing.T) {
claimer := &roleReadyClaimer{}
ts := newClaimNextServer(t, claimer)
for _, query := range []string{"?limit=1", "?limit=0", "?limit=", "?limit=abc"} {
t.Run(query, func(t *testing.T) {
resp := ts.claimNext(t, claimNextPath+query, `{"actor":"poller"}`)
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) || body["param"] != "limit" {
t.Errorf("code/param = %v/%v, want %s on `limit`", body["code"], body["param"], CodeInvalidArgument)
}
if body["reason"] != string(ReasonInvalidValue) {
t.Errorf("reason = %v, want %s: the name is one this server knows, it is the ACTION it refuses",
body["reason"], ReasonInvalidValue)
}
})
}
if got := claimer.claimNextRequests(); len(got) != 0 {
t.Errorf("a refused limit reached the role: %+v", got)
}
}
// TestClaimNextRefusesUnknownAndMalformedBodies is the body vocabulary. The
// FILTER cases are the point: a client that put its filter in the body must
// learn so by name rather than have it silently ignored, which would hand it a
// claim from the unfiltered ready front.
func TestClaimNextRefusesUnknownAndMalformedBodies(t *testing.T) {
for _, tc := range []struct {
name string
body string
param string
}{
{name: "a filter member in the body", body: `{"actor":"poller","label":["urgent"]}`, param: "label"},
{name: "a filter object in the body", body: `{"actor":"poller","filter":{}}`, param: "filter"},
{name: "a missing actor", body: `{}`, param: claimActorMember},
{name: "an actor that is blank after trimming", body: `{"actor":" "}`, param: claimActorMember},
{name: "an actor carrying a newline", body: `{"actor":"a\nbd: claimed by mallory"}`, param: claimActorMember},
{name: "a non-string actor", body: `{"actor":7}`, param: claimActorMember},
} {
t.Run(tc.name, func(t *testing.T) {
claimer := &roleReadyClaimer{}
ts := newClaimNextServer(t, claimer)
resp := ts.claimNext(t, claimNextPath, tc.body)
if resp.StatusCode != http.StatusBadRequest {
t.Fatalf("status = %d, want 400: %s", resp.StatusCode, readAll(t, resp))
}
if body := decodeBody(t, resp); body["param"] != tc.param {
t.Errorf("param = %v, want %q", body["param"], tc.param)
}
if got := claimer.claimNextRequests(); len(got) != 0 {
t.Errorf("a refused body reached the role: %+v", got)
}
})
}
}
// TestClaimNextMapsTheRolesValidationToA400 covers the defensive arm. Nothing
// should reach it — the edge refuses the empty actor and this operation has no
// spelling for the two other fields the role turns down — but a 500 for a
// request the caller could have fixed is the wrong answer if that changes.
func TestClaimNextMapsTheRolesValidationToA400(t *testing.T) {
ts := newClaimNextServer(t, &roleReadyClaimer{
err: fmt.Errorf("%w: claim next does not take a limit", issueops.ErrValidation),
})
resp := ts.claimNext(t, claimNextPath, `{"actor":"poller"}`)
if resp.StatusCode != http.StatusBadRequest {
t.Fatalf("status = %d, want 400: %s", resp.StatusCode, readAll(t, resp))
}
if body := decodeBody(t, resp); body["code"] != string(CodeInvalidArgument) {
t.Errorf("code = %v, want %s", body["code"], CodeInvalidArgument)
}
}
// TestClaimNextAttributesAnUncompilableLabelRegex: the claim's scan compiles
// label_regex in the database's own dialect, so a pattern it cannot compile is
// the caller's typo — the 400 on label_regex the ready listing gives for the
// same filter — not a 500. The same text on a claim that sent no regex stays a
// server fault.
func TestClaimNextAttributesAnUncompilableLabelRegex(t *testing.T) {
engineErr := errors.New("claim next: Error 1105 (HY000): the given regular expression is invalid")
for _, tc := range []struct {
name, path string
status int
}{
{"with a regex", claimNextPath + "?label_regex=" + url.QueryEscape("tech-("), http.StatusBadRequest},
{"without a regex", claimNextPath, http.StatusInternalServerError},
} {
t.Run(tc.name, func(t *testing.T) {
ts := newClaimNextServer(t, &roleReadyClaimer{err: engineErr})
resp := ts.claimNext(t, tc.path, `{"actor":"poller"}`)
if resp.StatusCode != tc.status {
t.Fatalf("status = %d, want %d: %s", resp.StatusCode, tc.status, readAll(t, resp))
}
if tc.status != http.StatusBadRequest {
return
}
if body := decodeBody(t, resp); body["code"] != string(CodeInvalidArgument) || body["param"] != "label_regex" || body["reason"] != string(ReasonInvalidValue) {
t.Errorf("body = %v, want invalid_argument on label_regex with reason invalid_value", body)
}
})
}
}
// TestClaimNextHasNoConflictAndNoNotFound states the operation's negative space
// as a test. A row a racing agent took is not in the set this claim scanned, so
// there is nothing to 409 about; there is no id in the path, so there is
// nothing to 404 about. Both absences are what a client dispatches on.
func TestClaimNextHasNoConflictAndNoNotFound(t *testing.T) {
for _, code := range operationCodes[OpClaimNextIssue] {
switch code {
case CodeNotFound, CodeAlreadyClaimed, CodeNotClaimable:
t.Errorf("claimNext documents %s; it names no row, so it can refuse for neither reason", code)
}
}
}
// TestClaimNextPathReachesItsHandler drives the documented path, which is the
// one thing route parity cannot check: this literal shares a prefix with the
// claim's wildcard, and ServeMux precedence is what keeps it from being parsed
// as a claim of an issue called ":claimNext".
func TestClaimNextPathReachesItsHandler(t *testing.T) {
claimer := &roleReadyClaimer{result: issueops.ClaimNextResult{Claimed: readyRow("bd-1")}}
ts := newClaimNextServer(t, claimer)
if resp := ts.claimNext(t, claimNextPath, `{"actor":"poller"}`); resp.StatusCode != http.StatusOK {
t.Fatalf("POST the documented claimNext path: status = %d, want 200", resp.StatusCode)
}
line := findLogLine(t, ts.stderr.String(), "path="+claimNextPath)
if !strings.Contains(line, "op="+OpClaimNextIssue) {
t.Errorf("the documented claimNext path is served by another operation:\n%s", line)
}
if got := claimer.claimNextRequests(); len(got) != 1 {
t.Errorf("the documented path did not reach the role: %+v", got)
}
}