beads/cmd/bd/list_page_edges.go
Julian Knutsen 169c5fc499
perf(list): read only the listed issues' dependency edges (#7591)
* perf(list): read only the listed issues' dependency edges

`bd list`'s text renderings (the default tree, --deps, --parent, --format
and --watch) loaded every dependency record in the workspace through
GetAllDependencyRecords to draw one page. On a local store that is one
query. Over a connected HTTP workspace it is a walk of the whole workspace:
every issue id at 200 per request, then every id's edges at 100 per
request. `bd list --limit 20` against the ~3.8k-issue maintainer-city
workspace made 62 sequential requests and took ~5.7 s at ~50 ms a round
trip, where `bd list --json` makes 5.

Every rendering reads only edges a listed issue owns. The tree nests a
child under a parent only when both are listed, and looks the edge up
under the child. Sibling ordering, the --deps annotations and the gate
decoration read the listed node's own edges, and --format prints edges
between listed issues. An unlisted issue's edge onto a listed one, or a
parent off the page, is never read. listDependencyRecords reads exactly
those edges through the EdgeReader role, which every backend serves (the
http store refuses the raw GetDependencyRecordsForIssues batch method).
Over http that is one anchored listDependencies request per 100 listed
ids.

Through a logging proxy in front of the production bd serve, list
--limit 20 goes from 62 requests / 5.7 s to 6 / 0.49 s with
byte-identical output; every changed mode (--deps, --deps=all, --format
digraph/dot, --sort, --status, --ready, --limit 200, --limit 0, --parent)
prints the same bytes as before. On a local store a default page is as
fast or faster; a listing that covers most of a large durable store pays
batched reads instead of one table scan (+0.07 s at 1,000 rows, +0.46 s
at 4,000 on embedded Dolt).

Tests:
- backend/http TestE2E_ListTextRenderingsReadOnlyTheListedIssuesEdges:
  over a served workspace, the tree, --deps=all, a --limit cut, the
  --parent tree and --format digraph each make one listDependencies
  request naming exactly the listed ids, and at most one request more
  than the --json page. Failing first: the old read enumerated every id
  (all=true) and anchored the hidden closed issue too.
- cmd/bd: the page read equals GetAllDependencyRecords restricted to the
  page on Dolt server and embedded stores (both planes, every edge type,
  gates, external refs), and every text rendering prints the same bytes
  from either map; a runListCore spy pins one page read and no
  store-wide read; pure tests over an adversarial fixture (edges unlisted
  issues own pointing at listed ones, parents off the page, dotted
  children, a parent-child cycle) guard the renderers.

Refs: ga-nrnrlr.5

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

* test(http): seed the list page-edges e2e with one graph apply

TestE2E_ListTextRenderingsReadOnlyTheListedIssuesEdges ran 26 s on
http_served_test's round-robin shard in CI, which made that shard the
embedded lane's longest test action (172 s). Most of it was subprocesses:
five creates and a close to seed, plus a --json budget run per case.

The fixture is now one `bd create --graph` plus the close (the batch
create cannot carry a closed row's closed_at over the wire), and one
`bd list --json` run is the budget for every page case, since its request
count does not depend on the page size. 16 subprocesses become 9. All
five cases still fail against the old GetAllDependencyRecords call
sites: each edge read anchors the hidden closed issue, and the four page
cases make 7 requests against the --json page's 5.

Refs: ga-nrnrlr.5

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

---------

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-16 16:05:24 +00:00

64 lines
2.6 KiB
Go

package main
import (
"context"
"github.com/steveyegge/beads/internal/types"
"github.com/steveyegge/beads/issueops"
)
// listEdgeSource is the one accessor listDependencyRecords needs. Every
// storage.DoltStorage satisfies it, and the --watch display takes it rather
// than the whole store so a test can hand it a stub.
type listEdgeSource interface {
EdgeReader() (issueops.EdgeReader, error)
}
// listDependencyRecords returns the stored outgoing dependency edges of the
// listed issues, keyed by issue id, in the shape GetAllDependencyRecords
// returns: no key for an issue with no edges, and a non-nil map on success.
//
// These are the only edges any `bd list` text rendering reads. Every reader
// keys the map by a LISTED issue's id: buildIssueTreeWithDepsOrdered nests a
// child under a parent only when both are listed, and it looks the edge up
// under the child; orderSiblingsByDeps and depRender.annotationsFor read the
// edges of the sibling or node they are drawing; gatesByIssueID reads the
// edges of the listed ids it decorates; and outputFormattedList prints the
// edges of listed issues whose target is listed too. An edge that an unlisted
// issue owns is never read, even when it points at a listed one, and a parent
// that is not listed nests nothing. So the listed ids are the whole question,
// and a full-store read would only fetch edges that are then ignored.
//
// It reads through the EdgeReader role rather than GetDependencyRecordsForIssues
// because every backend serves the role. The http store refuses the raw batch
// method (internal/httpclient/unsupported_gen.go) and answers the role with one
// anchored listDependencies request per 100 ids. GetAllDependencyRecords has no
// wire operation of its own, so over http it pages through every issue id in
// the workspace and then reads every id's edges in chunks of 100: about 60
// sequential requests for a 20-row page of a ~3,800-issue workspace.
func listDependencyRecords(ctx context.Context, src listEdgeSource, issues []*types.Issue) (map[string][]*types.Dependency, error) {
deps := make(map[string][]*types.Dependency)
ids := make([]string, 0, len(issues))
for _, issue := range issues {
if issue != nil {
ids = append(ids, issue.ID)
}
}
if len(ids) == 0 {
return deps, nil
}
reader, err := src.EdgeReader()
if err != nil {
return nil, err
}
result, err := reader.ReadEdges(ctx, issueops.EdgeReadRequest{IDs: ids})
if err != nil {
return nil, err
}
for _, anchor := range result.Anchors {
if len(anchor.Edges) > 0 {
deps[anchor.ID] = anchor.Edges
}
}
return deps, nil
}