beads/cmd/bd/serve_source_test.go
Julian Knutsen 4450fe99bb
feat(http): bd blocked over bd serve (blocked.list), plus bd blocked --flags [--raw] (#7649)
* feat(http): bd blocked over bd serve (blocked.list) through issueops.BlockedLister

`bd blocked` refused against an HTTP workspace: it called the legacy
GetBlockedIssues, which the HTTP backend stubs as unsupported. It now runs
one role on every route, and bd serve publishes that role.

- Role: issueops.BlockedLister.ListBlocked(BlockedRequest{ParentID, Labels,
  LabelsAny, ExcludeLabels}) -> BlockedResult{Items}. One body,
  internal/storage/issueops.ExecuteListBlockedInTx over the unchanged
  GetBlockedIssuesInTx; labels normalized in the role (BlockedFilter);
  Items never nil and in a total order (priority, created_at desc, id).
- Legs: DoltStore (read tx), EmbeddedDoltStore (read conn), uow (read unit
  of work through the use case's GetBlockedIssues, so the
  external-dependency uow decorator applies). Census: storage.DoltStorage
  gains BlockedLister(); HookFiringStore passes it through (a read),
  telemetry wraps it, uow.notifyingProvider, externaldeps.uowProvider and
  httpapi.timedProvider build it.
- externaldeps.Store.BlockedLister is built over its own GetBlockedIssues,
  so local `bd blocked` output is unchanged. Over a remote store it takes
  the server's listing when the server advertises
  policy.external_dependencies, else decorates the server's listing here
  through EdgeReader and BatchGetter (decorateBlocked, factored out of
  GetBlockedIssues).
- Wire: GET /v0/beads/blocked (listBlockedIssues; parent, label,
  label_any, exclude_label) answering BlockedPage {items, has_more} with
  the new x-go-type-pinned BlockedIssue schema, behind the new
  blocked.list token. apigen and the wire-shape golden regenerated; the
  golden diff is additive only, so wire_revision stays 2.
  Config.BlockedLister is a required role, wired in bd serve.
- Client: httpclient.Store.BlockedLister dials through Store.dispatch, so
  an older server is refused BEFORE the dial with *wire.CapabilityError
  naming blocked.list. The legacy Store.GetBlockedIssues rides the role
  (page and row cap applied to its answer) and leaves the unsupported
  shell. Divergence ledger row F-blocked retired.
- CLI: `bd blocked` takes BlockedLister() from the store on the direct
  routes and from the provider's BlockedListerSource on the proxied route;
  runBlockedProxiedServer is deleted. --json output is the same rows in
  the same order, [] when empty.

Tests: backend/conformance RunBlockedLister (10 cases: direct blocker,
child attributed to its blocked parent, closed blocker, wisp plane, pinned
status, parent filter, label filters with normalization, order, empty
slice, request not mutated) on dolt, embedded, uow and the served HTTP
client; httpapi handler tests (mapping, unknown/repeated params,
projection, capability); httpclient stub tests (pre-dial refusal naming
the token, encoding, projection, legacy door paging); skew and vocabulary
counts; TestE2E_BlockedOverHTTP (served `bd blocked --json` equals the
local command's after mapping ids and timestamps, member order included,
external blocker named, older-server refusal with no dial); corpus
gc-blocked re-marked and gc-blocked-parent added, both pending until the
reference server advertises blocked.list.

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

* fix(blocked): pin server-side external decoration, refuse has_more, serve the legacy door over http

Review fixes for the blocked.list slice.

- Server-side external decoration is now pinned on the compositions bd
  serve answers from. TestWrapUOWProviderBlockedListerNamesExternalBlockers
  lists through the wrapped unit-of-work provider's own BlockedLister
  accessor (the bd serve provider path) and requires an external-only row
  and a mixed row to carry their external: refs; building that lister over
  the inner provider fails it. TestServedBlockedListerCarriesExternalRefs-
  WhenTheServerEnforces serves the store-shaped composition
  (externaldeps.Store.BlockedLister, policy advertised) and requires the
  operation's own answer, through the undecorated client role, to carry the
  refs, and the client's decorated chain to return it unchanged; serving
  the inner store's lister fails it.
- The client refuses an answer reporting has_more with the new typed
  httpclient.TruncatedListingError instead of returning a short listing:
  the operation has no cursor, and a caller reads a missing row as
  unblocked. The legacy door inherits it.
- externaldeps.Store.GetBlockedIssues over an http store reads the extra
  sources through the served roles (EdgeReader, BatchGetter) instead of the
  raw GetIssuesByIDs / GetDependencyRecordsForIssues the http backend
  refuses, and the decoration skips the source read when there is nothing
  to load. The legacy door on the CLI chain (bd mol stale, the wisp
  commands) now answers over http in both policy modes;
  TestServedLegacyGetBlockedIssuesThroughTheDecorator pins it, paging
  included. bd ready --explain still refuses on its other raw reads; the
  CHANGELOG says so.
- Docs: the spec and the retired F-blocked ledger row state that the
  listing is unpaged, bounded by the client's 32 MiB response limit
  (refused, never read in part), and that has_more is refused.
- Contract: new cases for waits-for and conditional-blocks blockers, no
  type exclusions (gate, epic), and a stale flag with no nameable blocker
  (on the legs that can write the column: dolt, embedded; skipped
  elsewhere with the reason). Order seeds explicit created_at values with
  five rows tied on priority and created_at, two of them wisps, so only the
  id tiebreak orders them; dropping the tiebreak now fails every run.

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

* feat(blocked): bd blocked --flags, the blocked flag per id from stdin

`bd blocked --flags [--json]` reads ids from stdin, one per line (blank
lines and repeats ignored), and prints the blocked flag bd holds for each:
with --json, [{"id","is_blocked"}] in the order the ids were first given,
omitting an id that names no row on either plane, [] for no input. It is
the readiness column a consumer otherwise had to read with `bd sql`, so no
such consumer needs a local-only command.

It is the FLAG, not the listing: `bd blocked` narrows the stored flag to
rows it can name a blocker for, and --flags does not, so a row whose flag
is set with no nameable blocker reads true here and is absent from the
listing. Where the external-dependency policy applies, an unsatisfied
external: blocker sets the flag, as it keeps the row out of `bd ready`.

One library entry on every route: issueops.BlockedFlags over the route's
BlockingAnnotator (the store's accessor on dolt, embedded and HTTP, the
provider's on the proxied route). Over HTTP that is listBlockingAnnotations
with is_blocked behind dependencies.blocking.isBlocked; a server without
that token is refused before the dial, naming it. No wire change. The
listing's filter flags are refused with --flags rather than ignored.

Tests: TestE2E_BlockedFlagsOverHTTP (served JSON equals the local command's
after mapping ids to titles, unknown id omitted, repeat collapsed, [] on
empty stdin, filters refused; a stale-flagged row with no blocker reads
true and is absent from `bd blocked --json`, which fails if --flags is
derived from the listing; an older server's handshake refused naming the
token with no dial); TestProxiedServerBlocked/flags_reads_ids_from_stdin on
the proxied route; corpus gc-blocked-flags, pending until the reference
server advertises the token. CHANGELOG entry.

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

* feat(blocked): bd blocked --flags --raw, the stored is_blocked column

`bd blocked --flags --raw [--json]` answers the stored is_blocked column
for each id read from stdin, exactly what reading is_blocked from the issues
and wisps tables returns, without the external-dependency policy: a row
blocked only by an unsatisfied external: ref reads false (the policy-aware
--flags reads true), and a stale flag reads true either way. Same output
shape as --flags. This lets a consumer move off a `bd sql` read of the
column with no change in what it reads. The default --flags is unchanged.

The role is taken from beneath every decorator: storage.UnwrapStore on the
direct routes, uow.UnwrapProvider on the proxied route. Over HTTP the
innermost store is the http client, whose annotation read is the server's
answer: against a server that does not advertise
policy.external_dependencies that answer is the column, and --raw is
served. A server that advertises it decorates the flag it answers, the v0
wire has no member asking it for the column, and the decorated flag cannot
be turned back into the column client-side, so --raw against such a server
is refused before the annotation read, naming the token, rather than
answered with the decorated flag. --raw without --flags is refused.

Tests: TestE2E_BlockedFlagsOverHTTP/raw_is_the_stored_column (served --raw
equals local --raw; the externally-blocked-only row reads true with --flags
and false with --raw on both; locally blocked rows agree; a stale-flagged
row reads true in both modes; --raw without --flags exits 1; a server
advertising the policy refuses --raw with no annotation request);
TestProxiedServerBlocked/flags_raw_is_the_stored_column; corpus
gc-blocked-flags-raw (pending until the reference server advertises
dependencies.blocking.isBlocked). Routing --raw through the decorated
annotator fails the e2e. CHANGELOG entry.

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

* docs(changelog): bd blocked --flags --raw is for the local and proxied routes

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

* test(blocked): give the e2e gate-kid its own priority; document --flags answers closed ids

TestE2E_BlockedOverHTTP seeded gate-parent and gate-kid at one priority, so
when both landed in the same second their order fell to the generated ids,
which differ between the local and the served workspace: the comparison
failed intermittently. gate-kid now has its own priority. Ten runs on the
embedded lane pass.

bd blocked --flags answers a closed id too, with its stored flag; a SQL read
of the column filtered on status <> 'closed' leaves it out. Said in the
command's doc and the CHANGELOG.

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

* fix(blocked): apply label filters to external-only blocked rows

The external-dependency policy appends a row blocked only by an
unsatisfied external: ref after the label query has run, so --label,
--label-any and --exclude-label never judged it: bd blocked and
GET /v0/beads/blocked listed it whatever its labels, on every route.
Both decorators now filter those rows by the request's labels. The uow
decorator reads them through GetMany, chunked at MaxGetManyIDs, so their
labels are hydrated; GetIssuesByIDs left them empty, which also printed
them unlabelled on the proxied route.

Review fixes (adopt-pr apply-fixes, iteration 1):
- major #1: label filters on external-only rows in the store and uow
  decorators, with absence-asserting arms in the unit, served httpclient
  (server- and client-enforced policy), e2e and proxied tiers; the spec
  and the role doc say such rows are narrowed like every other row.
- minor #2: the policy.external_dependencies token prose names the
  blocked listing (spec, routes.go, regenerated types.gen.go).
- minor #6: SERVE_RUNBOOK and the allowUnlimited comment record the
  blocked listing's unbounded whole-set answer and why.
- minor #10: BlockedLister telemetry-outermost and typed-unsupported pins
  beside the ReadyCounter pair, plus the externaldeps wrap pin.
- nit #9: F-blocked names --flags --raw's refusal against a server that
  applies the policy (ledger.go and the regenerated ledger doc).
- nit #11: the remote BlockedLister no longer builds a local lister it
  then discards.

Review fixes (adopt-pr apply-fixes, iteration 2; the review approved at
940 with one minor and two nits, none gating):
- minor (the --flags refusal list was unpinned): the new
  TestBlockedFlagsRefusesEveryListingFilter pins blockedFlagFilterFlags
  to the flags blockedCmd registers, in both directions. Every
  registered flag except --flags and --raw must be refused, and every
  entry must name a registered flag. The test walks Flags() rather than
  LocalFlags(), so it does not merge the root's persistent flags into
  blockedCmd, and it skips inherited root flags and cobra's --help.
  Mutants:
  - four fail it: a new filter flag left off the list, a dropped
    entry, a misspelled entry, and --raw added to the list;
  - a simulated earlier in-process run (merged root flags plus --help)
    stays green, and fails once either skip is removed.
- nit (the remote branch's span): the remote BlockedLister comment now
  says why that lister is not wrapped again. The inner lister already
  carries telemetry's span, so wrapping would nest a second span of the
  same name. The comment also says what runs outside that span.
- nit (duplicated order comparator): left as is. finishBlockedIssues
  predates this PR, so merging the two sorts would refactor existing
  code with no review pass left after this one.

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

---------

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>
2026-10-17 12:29:13 +00:00

742 lines
34 KiB
Go

package main
import (
"context"
"errors"
"os"
"strings"
"testing"
"github.com/steveyegge/beads/internal/configfile"
"github.com/steveyegge/beads/internal/hooks"
"github.com/steveyegge/beads/internal/storage"
"github.com/steveyegge/beads/internal/storage/domain"
"github.com/steveyegge/beads/internal/types"
"github.com/steveyegge/beads/issueops"
"github.com/steveyegge/beads/memoryops"
)
// TestServeDatabaseSourceClassifiesTheWorkspace drives the one decision point
// bd serve has about where it reads and claims from.
//
// The registered arm is the whole point: the store the root command opened for
// a registered backend is opened through the same registry dispatch every
// ordinary bd command uses, so a workspace fully usable from the CLI must be
// servable too. The embedded arm is the permanent refusal, and it is here so
// that widening the classification cannot quietly narrow the refusal.
func TestServeDatabaseSourceClassifiesTheWorkspace(t *testing.T) {
t.Run("a registered backend is served from its store", func(t *testing.T) {
const name = "serve-registry"
registerContractBackend(t, name)
useStorageModeGlobals(t)
beadsDir := writeContractBackendConfig(t, name)
db, err := serveDatabaseSource(beadsDir)
if err != nil {
t.Fatalf("serveDatabaseSource() = %v, want the store source", err)
}
if db.source != serveSourceStore {
t.Errorf("source = %v, want serveSourceStore", db.source)
}
if db.backend != name {
t.Errorf("backend = %q, want %q", db.backend, name)
}
})
// The registry outranks every dolt-mode signal, because that is the order
// the store open already resolves them in: PersistentPreRunE dispatches on
// backends.Lookup before anything looks at shared-server mode, so a
// registered workspace with BEADS_DOLT_SHARED_SERVER=1 exported still opens
// the registered store. Resolving it the other way here would build a Dolt
// provider over a non-Dolt store and serve a different database than the
// CLI in the same workspace reaches.
t.Run("the registry outranks a shared-server environment", func(t *testing.T) {
const name = "serve-registry-shared"
registerContractBackend(t, name)
useStorageModeGlobals(t)
t.Setenv("BEADS_DOLT_SHARED_SERVER", "1")
beadsDir := writeContractBackendConfig(t, name)
db, err := serveDatabaseSource(beadsDir)
if err != nil {
t.Fatalf("serveDatabaseSource() = %v, want the store source", err)
}
if db.source != serveSourceStore {
t.Errorf("source = %v, want serveSourceStore: the store open gives the registry precedence here", db.source)
}
})
t.Run("an unloadable metadata.json is named, not classified", func(t *testing.T) {
useStorageModeGlobals(t)
beadsDir := t.TempDir()
writeBrokenBeadsConfig(t, beadsDir)
db, err := serveDatabaseSource(beadsDir)
if err == nil {
t.Fatalf("serveDatabaseSource() = %v, nil; want the load failure rather than a classification", db)
}
if !strings.Contains(err.Error(), configfile.ConfigPath(beadsDir)) {
t.Errorf("error does not name the file it could not read: %v", err)
}
})
}
// TestServeIssueRolesComeFromBeneathTheHookDecorator.
//
// bd serve documents that hooks do not fire, and a store's accessors hand out
// its decorators by design — `store.IssueClaimer()` on bd's own chain returns a
// claimer that runs the workspace's on_update script for every claim it lands.
// httpapi.Listen refuses exactly that value (storage.RoleFiresHooks), so this is
// the difference between a server that boots and one that does not.
func TestServeIssueRolesComeFromBeneathTheHookDecorator(t *testing.T) {
// Two layers beneath the hooks, because peeling ONE is the requirement and
// peeling all of them (storage.UnwrapStore) is the mistake that looks
// identical on a single-layer chain. The middle store stands in for the
// telemetry layer bd wires there, which is an Unwrapper too.
stubs := func(inner storage.DoltStorage) *serveRolesDoltStore {
return &serveRolesDoltStore{serveRolesStore: &serveRolesStore{
reader: &serveStubReader{},
claimer: &serveStubClaimer{},
batchCloser: &serveStubBatchCloser{},
readyClaimer: &serveStubReadyClaimer{},
releaser: &serveStubReleaser{},
reclaimer: &serveStubLeaseReclaimer{},
heartbeater: &serveStubHeartbeater{},
lifecycle: &serveStubLifecycle{},
dependencies: &serveStubDependencyEditor{},
batchApplier: &serveStubBatchApplier{},
metadataCAS: &serveStubMetadataCAS{},
counter: &serveStubCounter{},
edgeCounter: &serveStubGraphCounter{},
batchGetter: &serveStubBatchGetter{},
relations: &serveStubRelations{},
commenter: &serveStubCommenter{},
batchCreator: &serveStubBatchCreator{},
inner: inner,
}}
}
inner := stubs(nil)
middle := stubs(inner)
policyReadErr := errors.New("external dependency lookup failed")
middle.dependencyReadErr = policyReadErr
chained := wireStorageDecorators(middle, hooks.NewRunner(t.TempDir()), false)
if _, ok := chained.(*storage.HookFiringStore); !ok {
t.Fatalf("wireStorageDecorators produced %T, not a hook-firing store; this test proves nothing", chained)
}
fromTheStore, err := chained.IssueClaimer()
if err != nil {
t.Fatalf("IssueClaimer: %v", err)
}
if !storage.RoleFiresHooks(fromTheStore) {
t.Fatal("the store's own accessor no longer returns a hook-firing claimer; this test proves nothing")
}
// Same precondition for the lifecycle: RoleFiresHooks has to KNOW about the
// decorator's lifecycle wrapper, or the peel assertion below would pass on a
// predicate that answers false for everything.
lifecycleFromTheStore, err := chained.IssueLifecycle()
if err != nil {
t.Fatalf("IssueLifecycle: %v", err)
}
if !storage.RoleFiresHooks(lifecycleFromTheStore) {
t.Fatal("the store's own accessor no longer returns a hook-firing lifecycle; this test proves nothing")
}
// And for the dependency editor, the third role the decorator wraps. Without
// this its case in the RoleFiresHooks switch is held only by a comment, and
// the peel assertion below would pass on a predicate that answers false for
// everything.
editorFromTheStore, err := chained.DependencyEditor()
if err != nil {
t.Fatalf("DependencyEditor: %v", err)
}
if !storage.RoleFiresHooks(editorFromTheStore) {
t.Fatal("the store's own accessor no longer returns a hook-firing dependency editor; this test proves nothing")
}
// And for the batch applier, the FOURTH and widest role the decorator wraps:
// one call fires on_create, on_update and the close hooks, once per landed
// item plus once per distinct edge source. Without this precondition its case
// in the RoleFiresHooks switch is held only by a comment.
applierFromTheStore, err := chained.BatchApplier()
if err != nil {
t.Fatalf("BatchApplier: %v", err)
}
if !storage.RoleFiresHooks(applierFromTheStore) {
t.Fatal("the store's own accessor no longer returns a hook-firing batch applier; this test proves nothing")
}
// And for the compare-and-set, the FIFTH. It arrived with no precondition of
// its own, and its case in the RoleFiresHooks switch spent that time held
// only by a comment — which is how a rebase that left the case with an EMPTY
// BODY went unnoticed by the compiler, by vet, by lint and by CI. A Go type
// switch does not fall through, so `case *hookMetadataCAS:` with nothing
// under it answers FALSE, and the Listen refusal this whole test exists to
// protect was silently disarmed. Both cases are pinned now.
casFromTheStore, err := chained.MetadataCAS()
if err != nil {
t.Fatalf("MetadataCAS: %v", err)
}
if !storage.RoleFiresHooks(casFromTheStore) {
t.Fatal("the store's own accessor no longer returns a hook-firing compare-and-set; this test proves nothing")
}
// And for the releaser, the SIXTH. Its case in the RoleFiresHooks switch
// would otherwise be held only by a comment, which is exactly how the
// compare-and-set's case above spent time silently empty.
releaserFromTheStore, err := chained.Releaser()
if err != nil {
t.Fatalf("Releaser: %v", err)
}
if !storage.RoleFiresHooks(releaserFromTheStore) {
t.Fatal("the store's own accessor no longer returns a hook-firing releaser; this test proves nothing")
}
// And for the lease reclaimer, which fires once per reverted row.
reclaimerFromTheStore, err := chained.LeaseReclaimer()
if err != nil {
t.Fatalf("LeaseReclaimer: %v", err)
}
if !storage.RoleFiresHooks(reclaimerFromTheStore) {
t.Fatal("the store's own accessor no longer returns a hook-firing lease reclaimer; this test proves nothing")
}
// And for the commenter, the SEVENTH — and the one that was OUTSIDE the
// RoleFiresHooks switch entirely until the add-comment operation went on the
// wire. hook_commenter.go has fired the update hook for every comment it
// lands since it was written; nothing took the role off a store, so nothing
// noticed. An unpeeled one runs the workspace's script once per comment.
commenterFromTheStore, err := chained.Commenter()
if err != nil {
t.Fatalf("Commenter: %v", err)
}
if !storage.RoleFiresHooks(commenterFromTheStore) {
t.Fatal("the store's own accessor no longer returns a hook-firing commenter; this test proves nothing")
}
// THE THREE THAT WERE BLIND BESIDE THE COMMENTER. Each is served over a
// published operation and each was outside the RoleFiresHooks switch, so
// httpapi.Listen admitted a hook-firing value for all three; the class-level
// scan that finds the next one is
// TestRoleFiresHooksKnowsEveryHookFiringRole in internal/storage, and these
// are the store-side halves of the same property.
for _, role := range []struct {
name string
from func() (any, error)
}{
{"ready claimer", func() (any, error) { return chained.ReadyClaimer() }},
{"batch closer", func() (any, error) { return chained.BatchCloser() }},
{"batch creator", func() (any, error) { return chained.BatchCreator() }},
} {
fromTheStore, err := role.from()
if err != nil {
t.Fatalf("the store's %s accessor: %v", role.name, err)
}
if !storage.RoleFiresHooks(fromTheStore) {
t.Fatalf("the store's own accessor no longer returns a hook-firing %s; this test proves nothing", role.name)
}
}
roles, err := serveIssueRoles(chained, false)
if err != nil {
t.Fatalf("serveIssueRoles: %v", err)
}
if storage.RoleFiresHooks(roles.commenter) {
t.Error("bd serve would run this workspace's hooks on every HTTP comment")
}
// The one peel retains the external-dependency policy below the hook layer.
// Policy-wrapped roles intentionally have a distinct identity from the raw
// role beneath them; serving them without hooks is the contract here.
if storage.RoleFiresHooks(roles.readyClaimer) {
t.Error("bd serve would run this workspace's hooks on every HTTP ready claim")
}
if storage.RoleFiresHooks(roles.batchCloser) {
t.Error("bd serve would run this workspace's hooks on every HTTP batch close")
}
// A failed external-policy lookup must still refuse the served close.
// Peeling away that policy would reach the raw stub's ErrUnsupported instead.
_, err = roles.batchCloser.CloseBatch(context.Background(), issueops.CloseBatchRequest{
Actor: "serve-test",
Items: []issueops.BatchCloseItem{{IssueID: "bd-blocked"}},
})
if !errors.Is(err, policyReadErr) {
t.Errorf("served batch closer lost external policy: got %v, want %v", err, policyReadErr)
}
if storage.RoleFiresHooks(roles.batchCreator) || roles.batchCreator != issueops.BatchCreator(middle.batchCreator) {
t.Errorf("batch creator came from %p, want the layer directly beneath the hooks (%p)", roles.batchCreator, middle.batchCreator)
}
if storage.RoleFiresHooks(roles.releaser) {
t.Error("bd serve would run this workspace's hooks on every HTTP release")
}
if storage.RoleFiresHooks(roles.leaseReclaimer) || roles.leaseReclaimer != issueops.LeaseReclaimer(middle.reclaimer) {
t.Errorf("lease reclaimer came from %p, want the layer directly beneath the hooks (%p)", roles.leaseReclaimer, middle.reclaimer)
}
if storage.RoleFiresHooks(roles.heartbeater) || roles.heartbeater != issueops.Heartbeater(middle.heartbeater) {
t.Errorf("heartbeater came from %p, want the layer directly beneath the hooks (%p)", roles.heartbeater, middle.heartbeater)
}
reader, claimer := roles.reader, roles.claimer
// The same predicate httpapi.Listen refuses on, so a regression here is a
// server that refuses to boot rather than one that runs hooks silently.
if storage.RoleFiresHooks(claimer) {
t.Error("bd serve would run this workspace's hooks on every HTTP claim")
}
if reader == nil {
t.Error("bd serve lost the reader while peeling hooks")
}
// The lifecycle is the SECOND role the hook decorator wraps, and it wraps
// four verbs rather than one — so an unpeeled lifecycle would run this
// workspace's on_create, on_update and close hooks for every HTTP mutation.
if storage.RoleFiresHooks(roles.lifecycle) {
t.Error("bd serve would run this workspace's hooks on every HTTP lifecycle mutation")
}
// The dependency editor is the THIRD, and it fires the update hook once per
// DISTINCT SOURCE ISSUE — so an unpeeled editor would run this workspace's
// script several times for one HTTP batch.
if storage.RoleFiresHooks(roles.dependencyEditor) {
t.Error("bd serve would run this workspace's hooks on every HTTP dependency edit")
}
if roles.dependencyEditor != issueops.DependencyEditor(middle.dependencies) {
t.Errorf("dependency editor came from %p, want the layer directly beneath the hooks (%p)",
roles.dependencyEditor, middle.dependencies)
}
// The batch applier is the FOURTH, and the one where the peel is worth the
// most: its wrapper fires four hook vocabularies from one call — per landed
// item, plus once per distinct edge source — so an unpeeled applier serving
// one hundred-item plan is up to a hundred of this workspace's own
// subprocesses spawned inside a single HTTP request.
if storage.RoleFiresHooks(roles.batchApplier) {
t.Error("bd serve would run this workspace's hooks once per item of every HTTP plan")
}
if roles.batchApplier != issueops.BatchApplier(middle.batchApplier) {
t.Errorf("batch applier came from %p, want the layer directly beneath the hooks (%p)",
roles.batchApplier, middle.batchApplier)
}
// The compare-and-set is the FIFTH, and a coordination loop is its designed
// caller — so an unpeeled one runs this workspace's on_update script once per
// contended retry, at whatever rate the clients poll.
if storage.RoleFiresHooks(roles.metadataCAS) {
t.Error("bd serve would run this workspace's hooks on every HTTP compare-and-set")
}
if roles.metadataCAS != issueops.MetadataCAS(middle.metadataCAS) {
t.Errorf("compare-and-set came from %p, want the layer directly beneath the hooks (%p)",
roles.metadataCAS, middle.metadataCAS)
}
t.Run("a workspace with hooks disabled has no layer to peel", func(t *testing.T) {
// BD_NO_HOOKS=1 wires no hook decorator at all, so the roles are the
// store's own and the peel must be conditional.
bare := wireStorageDecorators(middle, hooks.NewRunner(t.TempDir()), true)
roles, err := serveIssueRoles(bare, false)
if err != nil {
t.Fatalf("serveIssueRoles: %v", err)
}
if storage.RoleFiresHooks(roles.claimer) || storage.RoleFiresHooks(roles.reader) {
t.Error("serveIssueRoles produced a hook-firing role with hooks disabled")
}
})
t.Run("no open store is an error, not a nil source", func(t *testing.T) {
// httpapi.Listen refuses a partial role set, but a nil store reaching
// it as an all-nil set would report "no database source" — true, and
// useless. Name the real condition here.
if _, err := serveIssueRoles(nil, false); err == nil {
t.Fatal("serveIssueRoles(nil) = nil error; want a refusal naming the missing store")
}
})
// The events journal is the one CONDITIONAL role, and both polarities
// matter. A backend that cannot read the journal is an ordinary backend —
// the journal is off by default, and a registered third-party backend has
// no obligation to implement a Dolt-shaped seam — so requiring it
// unconditionally would refuse to start servers that have no use for it.
// Requiring it for a workspace that DID enable one is the other half: a
// server that bound anyway would answer that route with a nil dereference.
t.Run("the journal role is required only when the workspace has one", func(t *testing.T) {
// serveRolesStore implements no journal seam, which is the case under
// test: assert that rather than assume it.
if _, ok := storage.UnwrapStore(chained).(storage.EventsJournalCursor); ok {
t.Fatal("the fixture store reads the journal; this test proves nothing")
}
roles, err := serveIssueRoles(chained, false)
if err != nil {
t.Fatalf("serveIssueRoles with the journal off: %v", err)
}
if roles.eventsJournal != nil {
t.Error("a store that cannot read the journal produced a reader")
}
if _, err := serveIssueRoles(chained, true); err == nil {
t.Fatal("serveIssueRoles accepted a journal-enabled workspace on a backend that cannot read the journal")
}
})
}
// TestServeResolvedModeNamesTheRegisteredBackend. The startup label is
// cosmetic, but "embedded (external dolt)" for a registered backend is two
// wrong statements about the topology an operator is trying to identify.
func TestServeResolvedModeNamesTheRegisteredBackend(t *testing.T) {
// A registered workspace has no dolt mode, and this is the field that
// carries one — see the ContextInfo gap noted on serveResolvedMode.
info := domain.ContextInfo{DoltMode: configfile.DoltModeEmbedded}
got := serveResolvedMode(info, serveDatabase{source: serveSourceStore, backend: "acme"})
if got != "acme (registered backend)" {
t.Errorf("mode = %q, want %q", got, "acme (registered backend)")
}
if strings.Contains(got, configfile.DoltModeEmbedded) || strings.Contains(got, "dolt") {
t.Errorf("mode reports a Dolt topology for a registered backend: %q", got)
}
}
// writeBrokenBeadsConfig plants a metadata.json that configfile.Load cannot
// parse, which is the one input serveDatabaseSource must refuse rather than
// classify: falling back to a default here would serve the embedded default
// for a workspace whose real backend is unknown.
func writeBrokenBeadsConfig(t *testing.T, beadsDir string) {
t.Helper()
if err := os.WriteFile(configfile.ConfigPath(beadsDir), []byte("{ not json"), 0o600); err != nil {
t.Fatalf("write broken metadata.json: %v", err)
}
}
// serveRolesStore is the smallest source the role extraction can be pointed at:
// it publishes its own roles and unwraps to an inner store with different ones,
// so a peel of the wrong depth lands on identifiably wrong values.
//
// IT EMBEDS NOTHING, and the assertion below is why. A stub that embedded a nil
// storage.DoltStorage would answer every accessor it forgot with a promoted
// method on a nil interface — a segfault inside serveIssueRoles rather than a
// compile error, which is how GraphCounter reached this file. Declaring the
// whole of serveRoleSource means the next role added there stops the build here,
// naming the method.
//
// Only the reader and the claimer carry identifiable values. That is enough to
// pin the peel DEPTH, which is the whole property under test: serveIssueRoles
// peels once and then calls every accessor on that one store value, so no role
// can come from a different layer than these two did. The rest return nil
// because the extraction does not inspect them.
type serveRolesStore struct {
reader *serveStubReader
claimer *serveStubClaimer
batchCloser *serveStubBatchCloser
readyClaimer *serveStubReadyClaimer
releaser *serveStubReleaser
reclaimer *serveStubLeaseReclaimer
heartbeater *serveStubHeartbeater
lifecycle *serveStubLifecycle
dependencies *serveStubDependencyEditor
batchApplier *serveStubBatchApplier
metadataCAS *serveStubMetadataCAS
counter *serveStubCounter
edgeCounter *serveStubGraphCounter
batchGetter *serveStubBatchGetter
relations *serveStubRelations
commenter *serveStubCommenter
batchCreator *serveStubBatchCreator
inner storage.DoltStorage
}
var _ serveRoleSource = (*serveRolesStore)(nil)
// serveRolesDoltStore is serveRolesStore where a WHOLE store is required:
// wireStorageDecorators builds the chain this test peels, and it takes a
// storage.DoltStorage.
//
// The roles above sit one embed shallower than the nil store beneath them, so
// they shadow it: every accessor serveIssueRoles reaches is declared, and only
// the rest of the interface — none of which this test calls — falls through to
// the nil embed. A new role therefore fails the assertion above rather than
// being quietly promoted here.
type serveRolesDoltStore struct {
*serveRolesStore
serveStubRest
dependencyReadErr error
}
func (s *serveRolesDoltStore) GetAllDependencyRecords(context.Context) (map[string][]*types.Dependency, error) {
return nil, s.dependencyReadErr
}
// serveStubRest carries the remainder of storage.DoltStorage for a stub that
// declares its own roles, one embed deeper than those roles. It is the shared
// half of the shadowing described above; serve_store_identity_test.go's stub
// uses it for the same reason.
type serveStubRest struct{ storage.DoltStorage }
func (s *serveRolesStore) IssueReader() (issueops.Reader, error) { return s.reader, nil }
func (s *serveRolesStore) IssueClaimer() (issueops.Claimer, error) { return s.claimer, nil }
func (s *serveRolesStore) Unwrap() storage.DoltStorage { return s.inner }
func (s *serveRolesStore) BatchCloser() (issueops.BatchCloser, error) { return s.batchCloser, nil }
func (s *serveRolesStore) ReadyClaimer() (issueops.ReadyClaimer, error) { return s.readyClaimer, nil }
// Releaser carries an identifiable value for the same reason, and it is the
// SIXTH role the decorator wraps: a peel of the wrong depth would hand bd serve
// a releaser that runs the workspace's hooks once per claim it frees — which a
// reaper draining abandoned work does in a tight loop.
func (s *serveRolesStore) Releaser() (issueops.Releaser, error) { return s.releaser, nil }
// LeaseReclaimer carries an identifiable value for the releaser's reason, and
// for a sharper form of it: its decorator fires on_update once per reverted
// row, so an unpeeled reclaimer runs the workspace's hooks N times per sweep.
func (s *serveRolesStore) LeaseReclaimer() (issueops.LeaseReclaimer, error) {
return s.reclaimer, nil
}
// Heartbeater carries an identifiable value so the test can assert which layer
// bd serve took it from: the layer beneath the hooks, since the hook decorator
// wraps it (a re-arm fires on_update).
func (s *serveRolesStore) Heartbeater() (issueops.Heartbeater, error) {
return s.heartbeater, nil
}
// IssueLifecycle carries an identifiable value for the same reason the reader
// and the claimer do: it is one of the roles the hook decorator wraps, so a peel
// of the wrong depth would hand bd serve a lifecycle that runs the workspace's
// hooks on every close.
func (s *serveRolesStore) IssueLifecycle() (issueops.Lifecycle, error) { return s.lifecycle, nil }
// DependencyEditor carries an identifiable value for the same reason, and it is
// the last of the three the decorator wraps: a peel of the wrong depth would
// hand bd serve an editor that runs the workspace's hooks on every edge it
// writes.
func (s *serveRolesStore) DependencyEditor() (issueops.DependencyEditor, error) {
return s.dependencies, nil
}
// BatchApplier carries an identifiable value for the same reason, and it is the
// FOURTH the decorator wraps: a peel of the wrong depth would hand bd serve an
// applier that runs the workspace's hooks once per item of every plan it
// applies.
func (s *serveRolesStore) BatchApplier() (issueops.BatchApplier, error) {
return s.batchApplier, nil
}
func (*serveRolesStore) WorkspaceConfig() (issueops.WorkspaceConfig, error) { return nil, nil }
func (*serveRolesStore) StatsReporter() (issueops.StatsReporter, error) { return nil, nil }
func (*serveRolesStore) CycleDetector() (issueops.CycleDetector, error) { return nil, nil }
func (*serveRolesStore) EdgeReader() (issueops.EdgeReader, error) { return nil, nil }
func (*serveRolesStore) BlockingAnnotator() (issueops.BlockingAnnotator, error) { return nil, nil }
func (*serveRolesStore) TreeWalker() (issueops.TreeWalker, error) { return nil, nil }
func (*serveRolesStore) ReadyCounter() (issueops.ReadyCounter, error) { return nil, nil }
func (*serveRolesStore) BlockedLister() (issueops.BlockedLister, error) { return nil, nil }
func (*serveRolesStore) Querier() (issueops.Querier, error) { return nil, nil }
func (*serveRolesStore) Sweeper() (issueops.Sweeper, error) { return nil, nil }
func (*serveRolesStore) Deleter() (issueops.Deleter, error) { return nil, nil }
func (*serveRolesStore) Memories() (memoryops.Memories, error) { return nil, nil }
// MetadataCAS carries an identifiable value for the same reason, and it is the
// FIFTH the decorator wraps: a peel of the wrong depth would hand bd serve a
// compare-and-set that runs the workspace's on_update script once per contended
// retry round of every coordination loop pointed at this server.
func (s *serveRolesStore) MetadataCAS() (issueops.MetadataCAS, error) { return s.metadataCAS, nil }
// Counter is declared for a different reason than every accessor above it, and
// the reason is the one engdocs/ADDING_AN_ISSUEOPS_ROLE.md calls "the step with
// no number": a role this stub does NOT declare arrives PROMOTED from the
// embedded storage.DoltStorage, which is nil here, so the first caller to reach
// it is a nil dereference in somebody else's test rather than a compile error.
//
// The count role is not one the hook decorator wraps — it is a READ, so
// hook_counter.go recurses and hands back the inner surface unwrapped — which is
// exactly why it needed this: the recursion lands on this type, and this type
// had no Counter to land on. It went unnoticed until `bd serve` began binding
// the role, and then it surfaced on one CI runner as a panic in a test about
// hook peeling.
func (s *serveRolesStore) Counter() (issueops.Counter, error) { return s.counter, nil }
// GraphCounter is declared for Counter's reason, and it is the SECOND role to
// need this file edited for a promotion rather than for a hook: the edge count
// is a read, so hook_graph_counter.go recurses and the recursion lands here.
// The count role's own comment above says this went unnoticed until `bd serve`
// began binding it; this one was found the same way, by this test panicking the
// moment the binding landed.
func (s *serveRolesStore) GraphCounter() (issueops.GraphCounter, error) { return s.edgeCounter, nil }
// BatchGetter is declared for GraphCounter's reason: the many-ids read is also
// a read, so hook_batch_getter.go recurses and the recursion lands here.
func (s *serveRolesStore) BatchGetter() (issueops.BatchGetter, error) { return s.batchGetter, nil }
// IssueRelations is the FIRST role added to serveIssueRoles since this type
// stopped embedding a nil store, and it is worth recording what that changed —
// because the two comments above it describe the old regime and are now history
// rather than instruction.
//
// Counter and GraphCounter had to be FOUND. Neither is wrapped by the hook
// decorator (both are reads, so their decorators recurse), so neither was
// noticed until `bd serve` began binding it — GraphCounter on a full-package CI
// shard, because no -run pattern anyone reaches for names this test.
//
// This one could not be missed. serveRoleSource names the accessor, the
// assertion above requires it, and omitting it is `*serveRolesStore does not
// implement serveRoleSource (missing method IssueRelations)` at build time, in
// this file, naming the method. Which is the whole return on #5539 landing
// first, measured on the next role rather than asserted about it.
func (s *serveRolesStore) IssueRelations() (issueops.Relations, error) { return s.relations, nil }
// BatchCreator carries an identifiable value rather than nil, and it moved off
// nil for the reason the three preconditions below give: it is one of the roles
// the hook decorator wraps, so a peel of the wrong depth hands bd serve a
// creator that runs the workspace's on_create once per item of every batch.
func (s *serveRolesStore) BatchCreator() (issueops.BatchCreator, error) {
return s.batchCreator, nil
}
// serveStubBatchCreator is the batch-create role's stand-in, ErrUnsupported like
// every stub here.
type serveStubBatchCreator struct{}
func (*serveStubBatchCreator) CreateBatch(context.Context, issueops.CreateBatchRequest) (issueops.CreateBatchResult, error) {
return issueops.CreateBatchResult{}, errors.ErrUnsupported
}
// Commenter is the SEVENTH role the hook decorator wraps and the second added
// under the regime IssueRelations describes: omitting it is a build error in
// this file naming the method, not a nil dereference somewhere else.
//
// It carries an identifiable value rather than nil, unlike the reads above,
// because it IS one of the wrapped roles: hook_commenter.go fires the update
// hook for every comment it lands, so a peel of the wrong depth would hand
// bd serve a commenter that runs the workspace's script once per comment — and
// a comment is exactly the write an agent makes in a loop.
func (s *serveRolesStore) Commenter() (issueops.Commenter, error) { return s.commenter, nil }
// serveStubCommenter is the add-comment role's stand-in, ErrUnsupported like
// every stub here.
type serveStubCommenter struct{}
func (*serveStubCommenter) AddComment(context.Context, issueops.AddCommentRequest) (issueops.AddCommentResult, error) {
return issueops.AddCommentResult{}, errors.ErrUnsupported
}
// serveStubRelations is the neighbor role's stand-in, ErrUnsupported like every
// stub here.
type serveStubRelations struct{}
func (*serveStubRelations) Related(context.Context, issueops.RelatedRequest) ([]*issueops.RelatedIssue, error) {
return nil, errors.ErrUnsupported
}
// serveStubGraphCounter is the edge-count role's stand-in, ErrUnsupported like
// every stub here.
type serveStubGraphCounter struct{}
func (*serveStubGraphCounter) CountEdges(context.Context, issueops.EdgeCountRequest) (issueops.EdgeCountResult, error) {
return issueops.EdgeCountResult{}, errors.ErrUnsupported
}
// serveStubBatchGetter is the many-ids read role's stand-in, ErrUnsupported
// like every stub here.
type serveStubBatchGetter struct{}
func (*serveStubBatchGetter) GetMany(context.Context, issueops.GetManyRequest) (issueops.GetManyResult, error) {
return issueops.GetManyResult{}, errors.ErrUnsupported
}
// serveStubCounter is the count role's stand-in. It answers ErrUnsupported like
// every stub here: this file's subject is which LAYER a role comes from, never
// what the role answers.
type serveStubCounter struct{}
func (*serveStubCounter) Count(context.Context, issueops.CountRequest) (issueops.CountResult, error) {
return issueops.CountResult{}, errors.ErrUnsupported
}
func (*serveStubCounter) CountByGroup(context.Context, issueops.CountByGroupRequest) (issueops.CountByGroupResult, error) {
return issueops.CountByGroupResult{}, errors.ErrUnsupported
}
type serveStubReader struct{}
func (*serveStubReader) Ready(context.Context, issueops.ReadyRequest) (issueops.IssuePage, error) {
return issueops.IssuePage{}, errors.ErrUnsupported
}
func (*serveStubReader) List(context.Context, issueops.ListRequest) (issueops.IssuePage, error) {
return issueops.IssuePage{}, errors.ErrUnsupported
}
func (*serveStubReader) Get(context.Context, issueops.GetRequest) (*issueops.IssueDetails, error) {
return nil, errors.ErrUnsupported
}
type serveStubClaimer struct{}
func (*serveStubClaimer) Claim(context.Context, issueops.ClaimRequest) (issueops.ClaimResult, error) {
return issueops.ClaimResult{}, errors.ErrUnsupported
}
// serveStubReadyClaimer is one of the roles the hook decorator does NOT wrap,
// so it carries no identifiable-value assertion below: this stub exists so the
// role set is complete.
type serveStubBatchCloser struct{}
func (*serveStubBatchCloser) CloseBatch(context.Context, issueops.CloseBatchRequest) (issueops.CloseBatchResult, error) {
return issueops.CloseBatchResult{}, errors.ErrUnsupported
}
type serveStubReadyClaimer struct{}
func (*serveStubReadyClaimer) ClaimNext(context.Context, issueops.ClaimNextRequest) (issueops.ClaimNextResult, error) {
return issueops.ClaimNextResult{}, errors.ErrUnsupported
}
type serveStubReleaser struct{}
func (*serveStubReleaser) Release(context.Context, issueops.ReleaseRequest) (issueops.ReleaseResult, error) {
return issueops.ReleaseResult{}, errors.ErrUnsupported
}
type serveStubHeartbeater struct{}
func (*serveStubHeartbeater) Heartbeat(context.Context, issueops.HeartbeatRequest) (issueops.HeartbeatResult, error) {
return issueops.HeartbeatResult{}, errors.ErrUnsupported
}
type serveStubLeaseReclaimer struct{}
func (*serveStubLeaseReclaimer) Reclaim(context.Context, issueops.ReclaimRequest) (issueops.ReclaimResult, error) {
return issueops.ReclaimResult{}, errors.ErrUnsupported
}
type serveStubLifecycle struct{}
func (*serveStubLifecycle) Create(context.Context, issueops.CreateRequest) (issueops.CreateResult, error) {
return issueops.CreateResult{}, errors.ErrUnsupported
}
func (*serveStubLifecycle) Update(context.Context, issueops.UpdateRequest) (issueops.UpdateResult, error) {
return issueops.UpdateResult{}, errors.ErrUnsupported
}
func (*serveStubLifecycle) Close(context.Context, issueops.CloseRequest) (issueops.CloseResult, error) {
return issueops.CloseResult{}, errors.ErrUnsupported
}
func (*serveStubLifecycle) Reopen(context.Context, issueops.ReopenRequest) (issueops.ReopenResult, error) {
return issueops.ReopenResult{}, errors.ErrUnsupported
}
type serveStubDependencyEditor struct{}
func (*serveStubDependencyEditor) AddDependencies(context.Context, issueops.AddDependenciesRequest) (issueops.AddDependenciesResult, error) {
return issueops.AddDependenciesResult{}, errors.ErrUnsupported
}
func (*serveStubDependencyEditor) RemoveDependency(context.Context, issueops.RemoveDependencyRequest) (issueops.RemoveDependencyResult, error) {
return issueops.RemoveDependencyResult{}, errors.ErrUnsupported
}
type serveStubMetadataCAS struct{}
func (*serveStubMetadataCAS) CompareAndSetKey(context.Context, issueops.CompareAndSetKeyRequest) (issueops.CompareAndSetKeyResult, error) {
return issueops.CompareAndSetKeyResult{}, errors.ErrUnsupported
}
type serveStubBatchApplier struct{}
func (*serveStubBatchApplier) ApplyBatch(context.Context, issueops.ApplyBatchRequest) (issueops.ApplyBatchResult, error) {
return issueops.ApplyBatchResult{}, errors.ErrUnsupported
}