beads/cmd/bd/ready_max_rows_proxied_test.go
Julian Knutsen c507f3b4c9
fix(proxy): unsplit bd ready --gated, and stop policing a flag bd list lacks (#6697)
* fix(proxy): unsplit `bd ready --gated`, and stop policing a flag `bd list` lacks

Post-merge review of #6293 (landed as 6f8d15341) found two majors. Both are
incomplete-fix defects in that PR's own work, not regressions: each is a claim
the PR made that its code does not support.

1. `bd ready --gated` still inherited the max-rows refusal that `bd mol ready
   --gated` was freed from. The two spellings are documented as the same
   gate-resume scan, and on the proxied route they literally are one:
   `runReadyProxiedGated` discards its `readyInput` and calls the same
   `findGateReadyMolecules` as `runMolReadyGatedProxiedServer`. Neither threads
   a row cap. But the new front door keys on `path == "ready"`, which fires for
   every `bd ready` invocation, so with `BEADS_MAX_ROWS` exported only the
   `mol` spelling worked. #6293 moved the refusal off the shared cobra leaf
   name and onto the command; the arm is the level it actually belongs at.

   Both refusal sites are exempted, because exempting one only moves the split:
   `ready.go`'s in-RunE backstop calls `AssertProxyCapability` with an empty
   command, so it always resolves the mode-wide refusal and cannot honor a
   command-specific allow. `readyGatedArm` is the single predicate they share.

   `--claim` stays refused, including `--claim --gated`. That pair is a usage
   error (`gatherReadyInput`), not a gated run, so the mutating arm keeps its
   refusal on every path into it and this exemption cannot reopen it.

2. `list`'s `ProxyCapRepo` row was flipped from `notApplicable()` to
   `repoRefusal`, promising a typed refusal for `bd list --repo` — but `--repo`
   is registered only on `createCmd` (create.go:940), so cobra rejects
   `bd list --repo` as an unknown flag before any of this runs. The row, the
   comment above `repoRefusal`, the CHANGELOG contract and the test that
   "proved" it all described a command line that cannot exist. The policy table
   is the audit source of truth this PR argues for; a row refusing a flag the
   command does not have is worse than the N/A row it replaced.

   Restored to `notApplicable()` and the CHANGELOG retargeted at
   `bd create --repo`, which exists and is gated. `runListProxiedServer`'s
   defensive branch goes back to the mode-wide assert: keyed on the `list` row
   it would now resolve to "allowed" and let a repo override through, which is
   the opposite of what the branch is for.

Tests: both fixes are pinned by tests that fail without them, checked by
reverting each guard in place.

- `bd ready --gated` under `BEADS_MAX_ROWS=5` joins the front-door allow matrix
  against the real command tree, and the e2e allow matrix beside its `mol`
  twin. `--claim` and `--claim --gated` are pinned as still-refused, so
  widening the exemption to a bare `--gated` check does not pass.
- The RunE backstop gets its own unit pin rather than being left to the
  env-gated proxied e2e lane; it asserts two-sidedly, since a guard that
  swallowed the error would pass a "no refusal" check alone.
- `TestProxyCapabilityRowsNameFlagsTheCommandRegisters` is the drift guard the
  review asked for: the existing one pins paths, which is why it could not see
  a row naming a real command and a flag that does not exist. It runs both
  directions — a refused/honored row must name a flag the command registers, an
  N/A row must name one it does not.
- The two `list --repo` tests #6293 added both asserted the false premise, not
  just the one the review named; both are retargeted at the reachable
  `bd create --repo` site and at the corrected row.

Deliberately out of scope: the four non-gating minors and the nit from the same
review are left for separate follow-ups against main.

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

* docs(changelog): record the `bd ready --gated` proxied row-cap fix

The behavior change in the previous commit is user-visible -- a command line
that exited 1 under a cap now runs -- so it gets its own [Unreleased] bullet
rather than riding along under #6293's entry, which describes the refusal as
it shipped.

Notes the ignored-cap consequence explicitly: `--gated` alongside a cap is now
dropped rather than refused, which is what the direct route has always done.

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

---------

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
2026-09-24 00:42:51 -07:00

182 lines
7.3 KiB
Go

package main
import (
"strings"
"testing"
)
// pinProxiedServerMode routes usesProxiedServer() down the proxied branch for
// the duration of one test. resetCommandContext() is what makes the global
// authoritative: usesProxiedServer prefers cmdCtx whenever an earlier test left
// one behind, so setting the global alone is not enough to pin the route.
func pinProxiedServerMode(t *testing.T) {
t.Helper()
restore := proxiedServerMode
proxiedServerMode = true
resetCommandContext()
t.Cleanup(func() {
proxiedServerMode = restore
resetCommandContext()
})
}
// TestReadyRefusesMaxRowsUnderProxiedServerOnClaim pins the --max-rows refusal
// on `bd ready --claim` specifically.
//
// The refusal lives in readyCmd's RunE, above the dispatch into
// runReadyProxiedServer, and --claim is the one ready invocation that takes a
// mutating path on the far side of it. Nothing below that dispatch can enforce
// the cap: runReadyProxiedServer deliberately passes a nil resolver to
// gatherReadyInput, and the proxied claim role never threads MaxRows. So the
// only thing standing between an operator's explicit row cap and a silent
// unbounded claim is this one call, and it is reachable only through RunE --
// the existing gatherReadyInput coverage sits entirely below it and cannot see
// it at all.
//
// Both cap sources are exercised: resolveMaxRows honors --max-rows and
// BEADS_MAX_ROWS, and a guard wired to only the flag would leave every rig that
// sets the env var running uncapped.
func TestReadyRefusesMaxRowsUnderProxiedServerOnClaim(t *testing.T) {
pinProxiedServerMode(t)
pinJSONOutput(t, false)
// The regression safety net. If the refusal ever stops firing, RunE falls
// through to runReadyProxiedServer, which hits an uninitialized provider
// and returns its own error rather than dialing a real server -- so the
// stderr assertion below, not the exit code, is what fails. Both errors
// carry exit code 1.
if uowProvider != nil {
t.Fatal("precondition: uowProvider must be nil so a regression cannot open a real proxied connection")
}
for _, tc := range []struct {
name string
args []string
env string
}{
{name: "flag", args: []string{"--claim", "--max-rows", "5"}},
{name: "env", args: []string{"--claim"}, env: "5"},
} {
t.Run(tc.name, func(t *testing.T) {
t.Setenv(maxRowsEnvVar, tc.env)
// A cloned flag set, not readyCmd's own: RunE reads its flags off
// the command it is handed, and mutating the shared readyCmd would
// leak --claim into every other test in the package.
cmd := newReadyFlagsCommand(t, tc.args...)
var err error
stderr := captureStderr(t, func() { err = readyCmd.RunE(cmd, nil) })
assertExitCode(t, err, 1)
if !strings.Contains(stderr, "--max-rows / BEADS_MAX_ROWS is not supported in proxied-server mode") {
t.Fatalf("stderr = %q, want the proxied --max-rows refusal", stderr)
}
})
}
}
// TestReadyGatedSkipsMaxRowsRefusalUnderProxiedServer pins the third arm.
// `bd ready --gated` and `bd mol ready --gated` are documented as the same
// gate-resume scan, and on the proxied route they literally are one:
// runReadyProxiedGated discards its readyInput and calls the same
// findGateReadyMolecules as runMolReadyGatedProxiedServer. Neither threads a
// row cap, so a cap must not refuse either spelling -- refusing only the
// `bd ready` one split a single documented command line in half for anyone
// with BEADS_MAX_ROWS exported.
//
// This goes through RunE rather than calling the helper, because RunE is where
// the second refusal site lives: the pre-provider front door is pinned
// separately against the real command tree in
// TestProxyCapabilityFrontDoorAllowsSupportedCommands, and exempting one site
// without the other just moves the refusal rather than removing it.
//
// The assertion is two-sided on purpose. A guard that swallowed the error
// entirely would pass a "no refusal" check alone, so the test also requires
// RunE to have reached the proxied dispatch -- uowProvider is nil here, so
// runReadyProxiedServer's own message is the proof it got that far.
func TestReadyGatedSkipsMaxRowsRefusalUnderProxiedServer(t *testing.T) {
pinProxiedServerMode(t)
pinJSONOutput(t, false)
if uowProvider != nil {
t.Fatal("precondition: uowProvider must be nil so the dispatch cannot open a real proxied connection")
}
for _, tc := range []struct {
name string
args []string
env string
}{
{name: "flag", args: []string{"--gated", "--max-rows", "5"}},
{name: "env", args: []string{"--gated"}, env: "5"},
} {
t.Run(tc.name, func(t *testing.T) {
t.Setenv(maxRowsEnvVar, tc.env)
cmd := newReadyFlagsCommand(t, tc.args...)
var err error
stderr := captureStderr(t, func() { err = readyCmd.RunE(cmd, nil) })
if strings.Contains(stderr, "--max-rows / BEADS_MAX_ROWS is not supported in proxied-server mode") {
t.Fatalf("proxied `bd ready %s` was refused for a cap the gated arm never uses: %q",
strings.Join(tc.args, " "), stderr)
}
if !strings.Contains(stderr, "proxied-server UOW provider not initialized") {
t.Fatalf("proxied `bd ready %s` did not reach the proxied dispatch: err=%v stderr=%q",
strings.Join(tc.args, " "), err, stderr)
}
})
}
}
// TestReadyClaimGatedKeepsMaxRowsRefusalUnderProxiedServer is the boundary of
// the exemption above. `--claim --gated` is a usage error rather than a gated
// run, so it must not pick up the gated arm's pass: the claim arm is the one
// invocation that mutates on the far side of this guard, and it keeps the
// refusal on every path into it. Without this, widening the exemption to a bare
// `--gated` check would look correct.
func TestReadyClaimGatedKeepsMaxRowsRefusalUnderProxiedServer(t *testing.T) {
pinProxiedServerMode(t)
pinJSONOutput(t, false)
t.Setenv(maxRowsEnvVar, "5")
err := rejectReadyMaxRowsUnderProxiedServer(newReadyFlagsCommand(t, "--claim", "--gated"))
if err == nil {
t.Fatal("proxied `bd ready --claim --gated` under a live cap was allowed; want the refusal the claim arm is owed")
}
assertExitCode(t, err, 1)
}
// TestReadyClaimWithoutLiveCapIsNotRefusedUnderProxiedServer is the other half:
// the refusal is conditioned on a live cap, not on --claim. Without it, a guard
// that rejected every proxied --claim would pass the test above.
//
// This one calls the helper directly rather than going through RunE. With
// nothing to refuse, RunE proceeds into the proxied query path, and the
// assertion would then be about whatever that path does next instead of about
// the guard.
func TestReadyClaimWithoutLiveCapIsNotRefusedUnderProxiedServer(t *testing.T) {
pinProxiedServerMode(t)
pinJSONOutput(t, false)
t.Setenv(maxRowsEnvVar, "")
// --max-rows 0 is an explicit disable that outranks BEADS_MAX_ROWS, so it
// is not a live cap either even with the env var set.
for _, tc := range []struct {
name string
args []string
env string
}{
{name: "no_cap", args: []string{"--claim"}},
{name: "flag_zero", args: []string{"--claim", "--max-rows", "0"}},
{name: "flag_zero_overrides_env", args: []string{"--claim", "--max-rows", "0"}, env: "5"},
} {
t.Run(tc.name, func(t *testing.T) {
t.Setenv(maxRowsEnvVar, tc.env)
if err := rejectMaxRowsUnderProxiedServer(newReadyFlagsCommand(t, tc.args...)); err != nil {
t.Fatalf("proxied `bd ready %s` was refused with no live cap: %v", strings.Join(tc.args, " "), err)
}
})
}
}