fix(query): apply bd list's piped-output limit policy to bd query (#6229) (#6744)

* fix(query): apply bd list's piped-output limit policy to bd query (#6229)

Root cause: bd list resolves an unflagged --limit through a policy that
treats piped stdout as unlimited (GH#4094), but bd query read its --limit
flag straight through, so its default of workapi.DefaultQueryLimit (50)
applied to piped output too. `bd query '...' | consumer` silently dropped
every row past 50; the truncation hint did not fire either, because it only
prints when stderr is a terminal.

Fix: factor the unflagged tail of bd list's policy into unflaggedLimit /
resolveUnflaggedLimit in list_input.go and call it from both commands, so
they cannot drift. bd list's switch is unchanged in behavior; bd query's
change is a single localized branch in gatherQueryInput.

Precedence for bd query, mirroring bd list:
  1. explicit --limit (including 0) wins
  2. piped stdout -> 0 (unlimited)
  3. agent mode on a terminal -> 20
  4. otherwise the 50 default
bd list keeps its own --all and list.limit branches ahead of step 2; bd
query has neither (its --all means "include closed", and there is no
query.limit config key), so none was invented.

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

* review: document the query path's absent defensive cap; unstale a reachability comment

Maintainer review fixes for PR #6744 (GH#6229). Both are comment-only; no
behavior changes.

issueops.QueryRequest.Limit now states that nothing backstops it. ListRequest
pairs its Limit with MaxRows, a circuit breaker for an oversized answer, and
neither knob that sets that one (--max-rows, BEADS_MAX_ROWS) is read anywhere
on the query path, so an unlimited query is unbounded in the plain sense. That
was already reachable via `--limit 0`; this PR makes a piped `bd query` reach
it without asking, which is what makes it worth writing down. Recording the
absence is the documented half of the reviewers' choice — threading a MaxRows
twin onto QueryRequest stays available as a follow-up.

unionCountsGoSideSortKeepsTheRightSubset's comment justified its case with
"DefaultQueryLimit means a limit is always set", which this PR falsified for
the CLI half: an unflagged piped `bd query '<expr>' --sort id` now arrives on
the IsGoSideSort path with Limit 0 and no bound. The HTTP surface still brings
a bound, because an absent `limit` parameter resolves to DefaultQueryLimit, so
the clause is requalified rather than dropped. The assertions were never
affected — they pass an explicit Limit: 5.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Co-authored-by: Julian Knutsen <julianknutsen@users.noreply.github.com>
This commit is contained in:
Harry Miller 2026-09-26 02:50:09 -07:00 • committed by GitHub
commit 0e317c4fc0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 153 additions and 9 deletions

View file

@ -206,6 +206,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
"only 'compact --dolt'", which names the root `bd compact` — a different
command, with no `--dolt` flag and its own proxied route. Scripts matching
the old text need updating.
- **`bd query` no longer silently stops at 50 rows when its output is piped**
([#6229](https://github.com/gastownhall/beads/issues/6229)). `bd list` has
treated piped stdout as unlimited since GH#4094, but `bd query` read its
`--limit` default of 50 straight through, so `bd query '...' | consumer`
dropped every row past 50 with no hint. An unflagged `bd query` now resolves
through the same policy as `bd list`: an explicit `--limit` wins, piped
stdout is unlimited, agent mode on a terminal gets 20, and a terminal gets 50.
- **`bd preflight` honors the `json` config default**
([#6293](https://github.com/gastownhall/beads/pull/6293)). Its `--json` flag
is bound to the same global every sibling command binds, so `json: true` in

View file

@ -331,10 +331,8 @@ func gatherListInput(cmd *cobra.Command) (listInput, error) {
in.effectiveLimit = 0
case listLimitConfigured:
in.effectiveLimit = limit
case !ui.IsTerminal():
in.effectiveLimit = 0 // Piped stdout should not truncate (GH#4094)
case ui.IsAgentMode():
in.effectiveLimit = 20
default:
in.effectiveLimit = unflaggedLimit(limit)
}
// The request carries the limit the caller receives. Which row limit that
// implies for the query - a sort SQL cannot express fetches everything and
@ -389,3 +387,28 @@ func parseListTimeFlag(cmd *cobra.Command, name string) (*time.Time, error) {
}
return &t, nil
}
// agentModeLimit is the page size an unflagged listing gets in agent mode on a
// terminal: ultra-compact output for an LLM context window.
const agentModeLimit = 20
// unflaggedLimit is the limit a listing command uses when the caller named
// none: the part of the policy `bd list` and `bd query` share, so the two
// cannot drift (GH#6229). Each command resolves its own earlier branches first
// (an explicit --limit, `bd list`'s --all and list.limit) and hands its own
// default here as fallback.
func unflaggedLimit(fallback int) int {
return resolveUnflaggedLimit(fallback, ui.IsTerminal(), ui.IsAgentMode())
}
// resolveUnflaggedLimit is unflaggedLimit with the environment passed in, so
// the terminal and agent-mode branches are testable from a piped `go test`.
func resolveUnflaggedLimit(fallback int, stdoutIsTerminal, agentMode bool) int {
switch {
case !stdoutIsTerminal:
return 0 // Piped stdout should not truncate (GH#4094)
case agentMode:
return agentModeLimit
}
return fallback
}

View file

@ -151,6 +151,11 @@ func gatherQueryInput(cmd *cobra.Command, args []string) (queryInput, error) {
in := queryInput{expression: strings.Join(args, " ")}
in.limit, _ = cmd.Flags().GetInt("limit")
if !cmd.Flags().Changed("limit") {
// bd list's policy for an unflagged limit: piped stdout is unlimited,
// agent mode is compact, a terminal gets the default (GH#6229).
in.limit = unflaggedLimit(in.limit)
}
in.longFormat, _ = cmd.Flags().GetBool("long")
in.parseOnly, _ = cmd.Flags().GetBool("parse-only")
in.offset, _ = cmd.Flags().GetInt("offset")

View file

@ -0,0 +1,95 @@
package main
import (
"testing"
"github.com/spf13/cobra"
"github.com/steveyegge/beads/internal/ui"
"github.com/steveyegge/beads/internal/workapi"
)
// newQueryLimitCommand registers the flags gatherQueryInput reads, with
// queryCmd's own --limit default.
func newQueryLimitCommand(t *testing.T, args ...string) *cobra.Command {
t.Helper()
cmd := &cobra.Command{Use: "query"}
cmd.Flags().IntP("limit", "n", workapi.DefaultQueryLimit, "")
cmd.Flags().Int("offset", 0, "")
cmd.Flags().BoolP("all", "a", false, "")
cmd.Flags().Bool("long", false, "")
cmd.Flags().String("sort", "", "")
cmd.Flags().BoolP("reverse", "r", false, "")
cmd.Flags().Bool("parse-only", false, "")
if err := cmd.ParseFlags(args); err != nil {
t.Fatalf("parse %v: %v", args, err)
}
return cmd
}
// TestQueryLimitPolicyMatchesList pins GH#6229: `bd query` read --limit
// straight through, so `bd query ... | consumer` silently stopped at
// workapi.DefaultQueryLimit rows where `bd list | consumer` got every row
// (GH#4094). An unflagged query now resolves through the same policy as list.
func TestQueryLimitPolicyMatchesList(t *testing.T) {
if ui.IsTerminal() {
t.Skip("this test asserts the piped-stdout branch; go test's stdout is a pipe, but this run's is not")
}
for _, tc := range []struct {
name string
args []string
want int
}{
{"an explicit limit wins when piped", []string{"--limit", "7"}, 7},
{"an explicit default-sized limit is still honored when piped", []string{"--limit", "50"}, 50},
{"an explicit zero stays unlimited", []string{"--limit", "0"}, 0},
{"piped stdout is unlimited, not the query default", nil, 0},
// query's --all means "include closed", not "no limit"; piping is
// what lifts the cap here.
{"--all does not change the piped policy", []string{"--all"}, 0},
} {
t.Run(tc.name, func(t *testing.T) {
in, err := gatherQueryInput(newQueryLimitCommand(t, tc.args...), []string{"status=open"})
if err != nil {
t.Fatalf("gatherQueryInput(%v): %v", tc.args, err)
}
if in.limit != tc.want {
t.Errorf("limit = %d, want %d", in.limit, tc.want)
}
if in.request.Limit == nil {
t.Fatal("QueryRequest.Limit is nil; `bd query` resolves its own limit before the request exists")
}
if *in.request.Limit != tc.want {
t.Errorf("QueryRequest.Limit = %d, want %d", *in.request.Limit, tc.want)
}
})
}
}
// TestUnflaggedLimitBranches covers the terminal and agent-mode branches the
// piped `go test` process cannot reach through gatherQueryInput or
// gatherListInput, for both commands' defaults.
func TestUnflaggedLimitBranches(t *testing.T) {
for _, fallback := range []int{workapi.DefaultListLimit, workapi.DefaultQueryLimit} {
for _, tc := range []struct {
name string
terminal bool
agent bool
want int
}{
{"terminal gets the command default", true, false, fallback},
{"agent mode on a terminal is compact", true, true, agentModeLimit},
{"piped stdout is unlimited", false, false, 0},
// Piping outranks agent mode, as it always has for bd list.
{"piped stdout is unlimited in agent mode too", false, true, 0},
} {
if got := resolveUnflaggedLimit(fallback, tc.terminal, tc.agent); got != tc.want {
t.Errorf("%s: resolveUnflaggedLimit(%d, terminal=%v, agent=%v) = %d, want %d",
tc.name, fallback, tc.terminal, tc.agent, got, tc.want)
}
}
}
if agentModeLimit != 20 {
t.Errorf("agentModeLimit = %d, want 20 (bd list's historical agent-mode cap)", agentModeLimit)
}
}

View file

@ -68,11 +68,13 @@ func (s *testSuite) unionGoSideSortKeepsTheRightSubset() {
// unionCountsGoSideSortKeepsTheRightSubset is the same case on the counts twin,
// and that is the twin the live callers reach. workapi.BuildQueryPlan pushes
// SortBy and Limit straight onto the filter for any filter-expressible query —
// no SQLLimit guard, and DefaultQueryLimit means a limit is always set — so
// `bd query '<expr>' --sort id` and GET /v0/beads/issues/query?sort=id both
// arrived here with SortBy "id" and a bound. Its own comment says the pushdown
// makes "the page the first N in the requested order on every backend", which
// was true of every key except the one IsGoSideSort names.
// no SQLLimit guard — so `bd query '<expr>' --sort id` and
// GET /v0/beads/issues/query?sort=id both arrive here with SortBy "id". The
// HTTP surface still brings a bound with it, because an absent `limit`
// parameter resolves to DefaultQueryLimit; the CLI need not, since GH#6229
// gave a piped `bd query` the unflagged limit 0. Its own comment says the
// pushdown makes "the page the first N in the requested order on every
// backend", which was true of every key except the one IsGoSideSort names.
func (s *testSuite) unionCountsGoSideSortKeepsTheRightSubset() {
const prefix = "bd-uw-cgos"
ids := s.seedTwoPlanes(prefix, 12)

View file

@ -60,6 +60,17 @@ type QueryRequest struct {
// constant serve both surfaces. A negative Limit is ErrValidation rather
// than a synonym for unlimited: two spellings of "no bound" is one more
// than a caller can check for.
//
// NOTHING BACKSTOPS IT. ListRequest pairs its Limit with MaxRows, a
// circuit breaker that refuses an oversized answer outright; this request
// has no twin to it by design, and neither knob that sets that one
// (`--max-rows`, BEADS_MAX_ROWS) is read anywhere on the query path. So an
// unlimited query is unbounded in the plain sense: it reads every row the
// expression matches, however many that is, and a caller who asked for one
// gets it rather than an ErrTooManyRows. The ceiling here is Limit itself.
// This is reachable without asking: a piped `bd query` resolves its
// unflagged limit to 0 before the request, the same policy `bd list` has
// applied to piped output since GH#4094.
Limit *int
// Offset skips the first N MATCHING rows, on EVERY implementation and for