fix(rebase): re-home BEADS_MAX_ROWS enforcement onto post-rebase seams

Fixes gaps exposed by rebasing PR #4160 onto current upstream/main,
found by actually running the PR's own gate tests rather than just
resolving textual conflicts:

- internal/storage/issueops/search.go: the Q2 perf-escape-hatch
  SkipWisps early return (added by main's search perf work, landed
  after this PR's original design) skipped EnforceMaxRowsCap entirely.
  bd list sets SkipWisps=true by default, so `bd list --max-rows N`
  silently ignored the cap. Enforce the cap on that return path too,
  mirroring the existing empty-wisps-table fix alongside it.

- internal/storage/issueops/search_counts.go: SearchIssuesWithCountsInTx
  (the `--json` query path, already present at the PR's merge-base but
  never wired for MaxRows by the original be-x42v.1/.2 commits) neither
  sized its SQL LIMIT off the cap nor enforced it on the merged result.
  Thread filter.MaxRows through EffectiveSearchLimit for the query LIMIT
  and check EnforceMaxRowsCap in the shared finishSearchIssuesWithCounts
  terminal helper (now returns an error), which every exit path already
  routed through. Fixes be-x42v.3's TestEmbeddedMaxRowsList/Flag_OverCap
  (`bd list --json --max-rows 5`), which failed after the rebase.

- cmd/bd/jira_max_rows_gate_test.go,
  cmd/bd/migrate_issues_max_rows_gate_test.go: seed INSERTs predate
  migration 0049 (LONGTEXT NOT NULL on issues.description/design/
  acceptance_criteria/notes, no default) that landed on main after
  this branch's merge-base. Provide empty-string values for the
  now-required columns.

Not fixed (separate, pre-existing gap; no gate test in this PR
exercises it): GetReadyWorkWithCountsInTx (`bd ready --json` /
`bd list --ready --json`) also doesn't thread MaxRows. Left alone to
keep this rebase to conflict resolution rather than expanding scope;
worth a follow-up bead.

Agent-Signature: claude-sonnet-medium on behalf of maphew
This commit is contained in:
matt wilkie 2026-07-23 19:51:47 -07:00
commit 25e35cc3ed
5 changed files with 48 additions and 20 deletions

View file

@ -85,8 +85,8 @@ func TestJiraSync_FilterHasZeroMaxRows(t *testing.T) {
// Seed one row with an external ref so DetectConflicts has
// something to consider (defending against an early bail-out that
// would skip the spy capture).
if _, err := inner.DB().ExecContext(ctx, `INSERT INTO issues (id, title, status, priority, issue_type, external_ref) VALUES (?, ?, ?, ?, ?, ?)`,
"jira-mr-1", "Jira MaxRows gate seed", "open", 1, "task", "https://stubmr.test/EXT-1"); err != nil {
if _, err := inner.DB().ExecContext(ctx, `INSERT INTO issues (id, title, description, design, acceptance_criteria, notes, status, priority, issue_type, external_ref) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
"jira-mr-1", "Jira MaxRows gate seed", "", "", "", "", "open", 1, "task", "https://stubmr.test/EXT-1"); err != nil {
t.Fatalf("seed: %v", err)
}

View file

@ -80,8 +80,8 @@ func TestMigrateIssues_FilterHasZeroMaxRows(t *testing.T) {
// Seed one row in the source repo so findCandidateIssues has
// something to find. The migration uses SourceRepo as the
// filter discriminator.
if _, err := inner.DB().ExecContext(ctx, `INSERT INTO issues (id, title, description, status, priority, issue_type, source_repo) VALUES (?, ?, ?, ?, ?, ?, ?)`,
"mig-mr-1", "Migration MaxRows gate seed", "body", "open", 1, "task", "source-repo-a"); err != nil {
if _, err := inner.DB().ExecContext(ctx, `INSERT INTO issues (id, title, description, design, acceptance_criteria, notes, status, priority, issue_type, source_repo) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
"mig-mr-1", "Migration MaxRows gate seed", "body", "", "", "", "open", 1, "task", "source-repo-a"); err != nil {
t.Fatalf("seed: %v", err)
}

View file

@ -134,7 +134,14 @@ func searchInTx[T any](ctx context.Context, tx DBTX, query string, filter types.
}
// Skip wisps merge entirely when caller opts out (Q2: perf escape hatch).
// This is also a terminal return of the row set actually handed back to
// the caller, so — like the empty-wisps-table early return above — it
// must enforce the cap itself; skipping it here would let SkipWisps
// callers (e.g. bd list's default filter) silently bypass MaxRows.
if filter.SkipWisps {
if err := EnforceMaxRowsCap(len(results), filter.MaxRows, filter.MaxRowsSource); err != nil {
return nil, err
}
return results, nil
}

View file

@ -27,7 +27,7 @@ func SearchIssuesWithCountsInTx(ctx context.Context, tx *sql.Tx, query string, f
return nil, err
}
if len(wisps) > 0 {
return finishSearchIssuesWithCounts(wisps, filter), nil
return finishSearchIssuesWithCounts(wisps, filter)
}
}
// Fall through: the wisps tier is missing/empty or matched no rows.
@ -40,7 +40,7 @@ func SearchIssuesWithCountsInTx(ctx context.Context, tx *sql.Tx, query string, f
if err != nil {
return nil, err
}
return finishSearchIssuesWithCounts(out, filter), nil
return finishSearchIssuesWithCounts(out, filter)
}
out, err := runFilterSearchQueryInTx(ctx, tx, query, filter, IssuesFilterTables, wispDepsExist)
@ -50,7 +50,7 @@ func SearchIssuesWithCountsInTx(ctx context.Context, tx *sql.Tx, query string, f
// Skip wisps merge entirely when caller opts out (Q2: perf escape hatch).
if filter.SkipWisps {
return finishSearchIssuesWithCounts(out, filter), nil
return finishSearchIssuesWithCounts(out, filter)
}
empty, probeErr := wispsTableEmptyOrMissingInTx(ctx, tx)
@ -58,21 +58,21 @@ func SearchIssuesWithCountsInTx(ctx context.Context, tx *sql.Tx, query string, f
return nil, fmt.Errorf("search issues with counts: wisp probe: %w", probeErr)
}
if empty {
return finishSearchIssuesWithCounts(out, filter), nil
return finishSearchIssuesWithCounts(out, filter)
}
if !wispDepsExist {
return finishSearchIssuesWithCounts(out, filter), nil
return finishSearchIssuesWithCounts(out, filter)
}
wisps, err := runFilterSearchQueryInTx(ctx, tx, query, filter, WispsFilterTables, true)
if err != nil {
if isTableNotExistError(err) {
return finishSearchIssuesWithCounts(out, filter), nil
return finishSearchIssuesWithCounts(out, filter)
}
return nil, err
}
if len(wisps) == 0 {
return finishSearchIssuesWithCounts(out, filter), nil
return finishSearchIssuesWithCounts(out, filter)
}
// Prefer the canonical wisp record when an ID exists in both tables (be-iabdi).
@ -93,7 +93,7 @@ func SearchIssuesWithCountsInTx(ctx context.Context, tx *sql.Tx, query string, f
}
}
kept = append(kept, wisps...)
return finishSearchIssuesWithCounts(kept, filter), nil
return finishSearchIssuesWithCounts(kept, filter)
}
func runFilterSearchQueryInTx(ctx context.Context, tx *sql.Tx, query string, filter types.IssueFilter, tables FilterTables, includeWispReverseDeps bool) ([]*types.IssueWithCounts, error) {
@ -106,8 +106,8 @@ func runFilterSearchQueryInTx(ctx context.Context, tx *sql.Tx, query string, fil
whereSQL = "WHERE " + joinAnd(whereClauses)
}
limitSQL := ""
if filter.Limit > 0 {
limitSQL = fmt.Sprintf("LIMIT %d", filter.Limit)
if eff := EffectiveSearchLimit(filter.Limit, filter.MaxRows); eff > 0 {
limitSQL = fmt.Sprintf("LIMIT %d", eff)
}
orderBy := sqlbuild.OrderBy(filter.SortBy, filter.SortDesc, "i")
return runSearchQueryInTx(ctx, tx, tables, whereSQL, orderBy, limitSQL, args, includeWispReverseDeps, filter.SkipLabels)
@ -154,12 +154,24 @@ func scanCountsRowsInTx(ctx context.Context, tx *sql.Tx, mainTable, query string
return out, nil
}
func finishSearchIssuesWithCounts(items []*types.IssueWithCounts, filter types.IssueFilter) []*types.IssueWithCounts {
// finishSearchIssuesWithCounts is the single terminal hook every
// SearchIssuesWithCountsInTx exit path routes through: it sorts the merged
// result, enforces the defensive MaxRows cap (be-x42v) on the row count
// actually assembled — mirroring searchInTx's terminal EnforceMaxRowsCap
// calls — and only then applies the caller-facing Limit trim. Checking the
// cap before the Limit trim matters: runFilterSearchQueryInTx sizes its SQL
// LIMIT via EffectiveSearchLimit (cap+1 when Limit is absent or looser than
// the cap) specifically so overage is still visible here before Limit
// silently truncates it away.
func finishSearchIssuesWithCounts(items []*types.IssueWithCounts, filter types.IssueFilter) ([]*types.IssueWithCounts, error) {
sortSearchIssuesWithCounts(items, filter.SortBy, filter.SortDesc)
if filter.Limit > 0 && len(items) > filter.Limit {
return items[:filter.Limit]
if err := EnforceMaxRowsCap(len(items), filter.MaxRows, filter.MaxRowsSource); err != nil {
return nil, err
}
return items
if filter.Limit > 0 && len(items) > filter.Limit {
return items[:filter.Limit], nil
}
return items, nil
}
// sortSearchIssuesWithCounts must order the merged issues+wisps rows the same

View file

@ -104,7 +104,10 @@ func TestFinishSearchIssuesWithCountsTruncatesByRequestedSort(t *testing.T) {
iwc("bd-newest-p4", 4, at(10)),
}
got := finishSearchIssuesWithCounts(items, types.IssueFilter{SortBy: "created", Limit: 2})
got, err := finishSearchIssuesWithCounts(items, types.IssueFilter{SortBy: "created", Limit: 2})
if err != nil {
t.Fatalf("sort=created limit=2: unexpected error: %v", err)
}
if len(got) != 2 || got[0].Issue.ID != "bd-newest-p4" || got[1].Issue.ID != "bd-new-p3" {
ids := make([]string, len(got))
for i, g := range got {
@ -114,7 +117,10 @@ func TestFinishSearchIssuesWithCountsTruncatesByRequestedSort(t *testing.T) {
}
// SortDesc flips the per-key default direction: created becomes ASC.
got = finishSearchIssuesWithCounts(items, types.IssueFilter{SortBy: "created", SortDesc: true, Limit: 2})
got, err = finishSearchIssuesWithCounts(items, types.IssueFilter{SortBy: "created", SortDesc: true, Limit: 2})
if err != nil {
t.Fatalf("sort=created desc limit=2: unexpected error: %v", err)
}
if len(got) != 2 || got[0].Issue.ID != "bd-old-p0" || got[1].Issue.ID != "bd-mid-p2" {
ids := make([]string, len(got))
for i, g := range got {
@ -124,7 +130,10 @@ func TestFinishSearchIssuesWithCountsTruncatesByRequestedSort(t *testing.T) {
}
// Default (no SortBy) keeps the historical priority/created/id order.
got = finishSearchIssuesWithCounts(items, types.IssueFilter{Limit: 2})
got, err = finishSearchIssuesWithCounts(items, types.IssueFilter{Limit: 2})
if err != nil {
t.Fatalf("default sort limit=2: unexpected error: %v", err)
}
if len(got) != 2 || got[0].Issue.ID != "bd-old-p0" || got[1].Issue.ID != "bd-mid-p2" {
ids := make([]string, len(got))
for i, g := range got {