beads/internal/workapi/storereader/reader.go
CI Bot de9fc833e2 refactor(workapi): move the store reader where a front door cannot reach it
`workapi.NewStoreReader` was exported and lived in internal/workapi, which 22
cmd/bd files already import for the builders. It takes storage.DoltStorage — an
interface a command already holds — so it was a one-line drop-in replacement for
`store.IssueReader()` from any front door, and the wrong one: every storage
decorator adds its layer in its OWN accessor (telemetry's reader-level spans, the
hook store's deliberate pass-through), so a command that constructed a reader
directly would get a bare unspanned one and the code would look entirely
ordinary. The old doc comment said "nothing does; the accessor is the door",
which is a promise, not a guard.

It has real callers, so it cannot simply be unexported: dolt.DoltStore and
embeddeddolt.EmbeddedDoltStore both need one shared body for their accessors,
and they are in different packages. So the body moves down to
internal/workapi/storereader instead, where those two are the only importers,
and the cmd-bd-reader-constructor depguard rule keeps cmd/bd out. Nothing about
internal/httpapi changes: depguard matches pkg by prefix, so
httpapi-transport-boundary already denied the subpackage along with its parent.

The two store packages' own NewIssueReader are unexported at the same time. They
had no caller but their own accessor, they are in internal/ so no out-of-tree
consumer can reach them, and leaving them exported would have left the same door
open one package over.

SEEN TO FAIL, on the golangci-lint version CI installs. `bd show --json` switched
from the accessor to the constructor — the exact bypass — compiles and vets
clean:

  $ golangci-lint run --config=.golangci.yml --build-tags=gms_pure_go ./...
  cmd/bd/show.go:15:2: import
  'github.com/steveyegge/beads/internal/workapi/storereader' is not allowed from
  list 'cmd-bd-reader-constructor': a command reaches the reader role through
  store.IssueReader(), never through the constructor ... (depguard)
  exit 1

ErrUnsupported.Op moves with the function, "NewStoreReader" -> "storereader.New",
so the Op still names the function that refused; its test moves with it.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-07-31 20:04:09 +00:00

128 lines
5.1 KiB
Go

// Package storereader holds the store-backed implementation of
// issueops.Reader: one shared body that every store-shaped backend's
// IssueReader accessor hands back.
//
// It is a package of its own rather than a file in internal/workapi for one
// reason. internal/workapi is the builders' home and 22 cmd/bd files import
// it, so a constructor living there was a one-line drop-in replacement for
// store.IssueReader() from any front door — and one that silently skips the
// telemetry decorator's reader-level spans, because a decorator adds its layer
// in its own accessor. Down here the only importers are the two Dolt store
// packages, and the cmd-bd-reader-constructor depguard rule in .golangci.yml
// makes a front door importing it a lint failure rather than a review comment.
//
// The accessor is the door. This is the thing behind it.
package storereader
import (
"context"
"github.com/steveyegge/beads/internal/storage"
"github.com/steveyegge/beads/internal/types"
"github.com/steveyegge/beads/internal/workapi"
"github.com/steveyegge/beads/issueops"
)
// New returns the issue-query surface backed by a store handle. *DoltStore and
// *EmbeddedDoltStore answer identically because the difference between them is
// below storage.DoltStorage, not above it.
//
// The reader supplies its own ConfigSource from the store it already holds, so
// a caller ON THE ROLE cannot half-perform the "load config, build filter,
// execute" ritual: it has no way to reach the pieces.
//
// THE BOUNDARY, stated once here because this is where the constructor lives:
// today the store-backed role answers `bd show --json`, and the HTTP surface
// reaches the uow-backed one. `bd ready` and `bd list` still call the workapi
// builders directly — they consume the FILTER for the max-rows cap, --claim,
// --watch, the hierarchical --parent tree and the text renderings — so Ready
// and List below are reached only by tests until a front door moves. They are
// covered by reader_test.go for exactly that reason. See issueops.Reader's doc
// comment for why routing only those commands' JSON paths through the role
// would be worse.
func New(store storage.DoltStorage) (issueops.Reader, error) {
if store == nil {
return nil, &storage.ErrUnsupported{Op: "storereader.New", Backend: "nil"}
}
return &storeReader{store: store}, nil
}
type storeReader struct{ store storage.DoltStorage }
var _ issueops.Reader = (*storeReader)(nil)
func (r *storeReader) Ready(ctx context.Context, req issueops.ReadyRequest) (issueops.IssuePage, error) {
filter, err := workapi.BuildReadyFilter(req)
if err != nil {
return issueops.IssuePage{}, err
}
limit := filter.Limit
if limit > 0 {
// The store seam has no HasMore of its own, so ask for one row past
// the page and let its presence be the answer.
filter.Limit = limit + 1
}
items, err := r.store.GetReadyWorkWithCounts(ctx, filter)
if err != nil {
return issueops.IssuePage{}, err
}
return page(items, limit), nil
}
func (r *storeReader) List(ctx context.Context, req issueops.ListRequest) (issueops.IssuePage, error) {
cfg, err := workapi.LoadStoreListConfig(ctx, r.store)
if err != nil {
return issueops.IssuePage{}, err
}
filter, err := workapi.BuildListFilter(req, cfg)
if err != nil {
return issueops.IssuePage{}, err
}
var items []*types.IssueWithCounts
if req.ReadyFlag {
items, err = r.store.GetReadyWorkWithCounts(ctx, workapi.ReadyFilterFromIssueFilter(workapi.WithFetchOneExtra(filter)))
} else {
items, err = r.store.SearchIssuesWithCounts(ctx, "", workapi.WithFetchOneExtra(filter))
}
if err != nil {
return issueops.IssuePage{}, err
}
// The display order is applied here, after the fetch, because it is the
// order the trim below has to cut on: filter.Limit is already 0 for a sort
// the database cannot express (SQLLimit), so those queries return
// everything and this is where the requested order first exists.
workapi.SortIssuesWithCounts(items, req.SortBy, req.Reverse)
return page(items, workapi.PageLimit(req)), nil
}
func (r *storeReader) Get(ctx context.Context, req issueops.GetRequest) (*issueops.IssueDetails, error) {
src := workapi.NewStoreDetailSource(r.store)
issue, isWisp, err := workapi.GetIssueOrWisp(ctx, src, req.ID)
if err != nil {
return nil, err
}
return workapi.BuildIssueDetails(ctx, src, issue, isWisp, workapi.DetailOptions{
IncludeDependents: req.IncludeDependents,
IncludeComments: req.IncludeComments,
})
}
// page trims an over-fetched result back to the requested limit and reports
// whether the trim removed anything. A limit of 0 is unlimited, so nothing is
// trimmed and there is by definition nothing more.
//
// Items is never nil on the way out: an empty page is an empty array on every
// surface that serializes one, and a caller must not have to tell null from
// empty to learn that nothing matched.
func page(items []*types.IssueWithCounts, limit int) issueops.IssuePage {
hasMore := limit > 0 && len(items) > limit
if hasMore {
items = items[:limit]
}
if items == nil {
items = []*types.IssueWithCounts{}
}
return issueops.IssuePage{Items: items, HasMore: hasMore}
}