test(kv): pin proxied kv list --prefix fast path and sibling-key e2e (bd-s5k6) (#7204)

Follow-up to #6380 (bd kv list --prefix). Both capability-discovery hops
fall back silently, and both modes re-filter in-process with
kvPairsWithPrefix, so a lost SQL-side fast path printed the same rows and
failed no existing test.

- Extract wireProxiedUOWProvider, the unit-of-work twin of
  wireStorageDecorators, so the proxied provider chain that main.go builds
  is unit-testable; main.go now calls it. No behavior change.
- TestKVListProxiedPrefixReadSurvivesProviderChain drives
  runKVListProxiedServer through that chain (hook runner on and off) over a
  prefix-capable stub use case and fails on any GetAllConfig scan;
  TestKVListProxiedPrefixAbsentCapabilityFallsBack keeps it honest.
- TestEmbeddedKV/kv_list_prefix_json and
  TestProxiedServerKV/list_prefix_json run the real command with sibling
  keys (mail.dog.* vs mail.doggerel.*) and an unmatched prefix.
- bdKVListJSON now fails on output with no JSON object instead of returning
  an empty map, so a "no pairs" check cannot pass vacuously.

Agent-Signature: claude-code-claude-opus-5-5-max on behalf of Julian Knutsen

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Julian Knutsen 2026-10-04 14:09:55 -07:00 • committed by GitHub
commit a93498c81f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 219 additions and 15 deletions

View file

@ -7,6 +7,7 @@ import (
"fmt"
"os"
"os/exec"
"reflect"
"strings"
"sync"
"testing"
@ -40,20 +41,24 @@ func bdKVFail(t *testing.T, bd, dir string, args ...string) string {
return string(out)
}
// bdKVListJSON runs "bd kv list --json" and returns parsed map.
func bdKVListJSON(t *testing.T, bd, dir string) map[string]string {
// bdKVListJSON runs "bd kv list --json" plus any extra args (such as
// --prefix) and returns the parsed map. Output with no JSON object fails the
// test rather than reading as an empty listing, which would let a check for
// "no pairs" pass on output that is not the JSON shape at all.
func bdKVListJSON(t *testing.T, bd, dir string, extraArgs ...string) map[string]string {
t.Helper()
cmd := exec.Command(bd, "kv", "list", "--json")
args := append([]string{"kv", "list", "--json"}, extraArgs...)
cmd := exec.Command(bd, args...)
cmd.Dir = dir
cmd.Env = bdEnv(dir)
stdout, stderr, err := runCommandBuffers(t, cmd)
if err != nil {
t.Fatalf("bd kv list --json failed: %v\nstdout:\n%s\nstderr:\n%s", err, stdout.String(), stderr.String())
t.Fatalf("bd %s failed: %v\nstdout:\n%s\nstderr:\n%s", strings.Join(args, " "), err, stdout.String(), stderr.String())
}
s := strings.TrimSpace(stdout.String())
start := strings.Index(s, "{")
if start < 0 {
return map[string]string{}
t.Fatalf("no JSON object in bd %s output: %s", strings.Join(args, " "), s)
}
var raw map[string]interface{}
if err := json.Unmarshal([]byte(s[start:]), &raw); err != nil {
@ -123,6 +128,28 @@ func TestEmbeddedKV(t *testing.T) {
}
})
t.Run("kv_list_prefix_json", func(t *testing.T) {
// mail.doggerel.* shares mail.dog.'s leading characters, so a match
// that dropped the prefix's trailing dot would leak it. The keys set
// by the subtests above must not leak either.
for _, kvp := range [][2]string{
{"mail.dog.m1", "first"},
{"mail.dog.m2", "second"},
{"mail.doggerel.m3", "sibling"},
} {
bdKV(t, bd, dir, "set", kvp[0], kvp[1])
}
m := bdKVListJSON(t, bd, dir, "--prefix", "mail.dog.")
if want := map[string]string{"mail.dog.m1": "first", "mail.dog.m2": "second"}; !reflect.DeepEqual(m, want) {
t.Errorf("kv list --prefix mail.dog. --json = %v; want %v", m, want)
}
if m := bdKVListJSON(t, bd, dir, "--prefix", "mail.owl."); len(m) != 0 {
t.Errorf("kv list --prefix mail.owl. --json = %v; want no pairs", m)
}
})
// ===== Clear =====
t.Run("kv_clear", func(t *testing.T) {

View file

@ -4,6 +4,7 @@ package main
import (
"encoding/json"
"reflect"
"strings"
"testing"
)
@ -30,17 +31,19 @@ func bdProxiedKVFail(t *testing.T, bd, dir string, args ...string) string {
return stdout + stderr
}
// kvListJSONRaw runs `bd kv list --json` and returns the raw JSON object
// text plus the parsed pairs. It pins the wire shape the wyvern wheelhouse
// mail transport parses: a flat JSON object whose only non-string member is
// the outputJSON-injected numeric "schema_version" (present in classic mode
// too — see bdKVListJSON in kv_embedded_test.go); every kv pair is a plain
// string-to-string entry with no envelope.
func kvListJSONRaw(t *testing.T, bd, dir string) (string, map[string]string) {
// kvListJSONRaw runs `bd kv list --json` plus any extra args (such as
// --prefix) and returns the raw JSON object text plus the parsed pairs. It
// pins the wire shape the wyvern wheelhouse mail transport parses: a flat
// JSON object whose only non-string member is the outputJSON-injected numeric
// "schema_version" (present in classic mode too — see bdKVListJSON in
// kv_embedded_test.go); every kv pair is a plain string-to-string entry with
// no envelope.
func kvListJSONRaw(t *testing.T, bd, dir string, extraArgs ...string) (string, map[string]string) {
t.Helper()
stdout, stderr, err := bdProxiedRunBuffers(t, bd, dir, "kv", "list", "--json")
args := append([]string{"kv", "list", "--json"}, extraArgs...)
stdout, stderr, err := bdProxiedRunBuffers(t, bd, dir, args...)
if err != nil {
t.Fatalf("bd kv list --json failed: %v\nstdout:\n%s\nstderr:\n%s", err, stdout, stderr)
t.Fatalf("bd %s failed: %v\nstdout:\n%s\nstderr:\n%s", strings.Join(args, " "), err, stdout, stderr)
}
s := strings.TrimSpace(stdout)
start := strings.Index(s, "{")
@ -146,6 +149,32 @@ func TestProxiedServerKV(t *testing.T) {
}
})
t.Run("list_prefix_json", func(t *testing.T) {
t.Parallel()
p := newSharedProxiedProject(t, bd, "pkvp")
// mail.doggerel.* shares mail.dog.'s leading characters, so a match
// that dropped the prefix's trailing dot would leak it.
for _, kvp := range [][2]string{
{"mail.dog.m1", "first"},
{"mail.dog.m2", "second"},
{"mail.doggerel.m3", "sibling"},
{"other", "unrelated"},
} {
bdProxiedKV(t, bd, p.dir, "set", kvp[0], kvp[1])
}
raw, m := kvListJSONRaw(t, bd, p.dir, "--prefix", "mail.dog.")
if want := map[string]string{"mail.dog.m1": "first", "mail.dog.m2": "second"}; !reflect.DeepEqual(m, want) {
t.Errorf("list --prefix mail.dog. --json = %v; want %v\n%s", m, want, raw)
}
raw, m = kvListJSONRaw(t, bd, p.dir, "--prefix", "mail.owl.")
if len(m) != 0 {
t.Errorf("list --prefix mail.owl. --json = %v; want no pairs\n%s", m, raw)
}
})
t.Run("list_json_shape_matches_classic", func(t *testing.T) {
t.Parallel()
// Same binary, same commands, classic embedded project: the JSON

View file

@ -1941,7 +1941,7 @@ var rootCmd = &cobra.Command{
hookRunner = hooks.NewRunner(filepath.Join(beadsDir, "hooks"))
uowSinks.Hook = hookRunner
}
uowProvider = wireExternalDependencyUOWProvider(uow.NewNotifyingProvider(p, uowSinks))
uowProvider = wireProxiedUOWProvider(p, uowSinks)
// Honor dolt.auto-commit for proxied writes the same way
// issueOpsContext already does for the direct/SQL-server routes

View file

@ -75,6 +75,18 @@ func waitForCommandHooks() {
hookRunner.Wait(hookRunner.Timeout())
}
// wireProxiedUOWProvider composes the proxied-server provider chain:
//
// caller → externaldeps policy → notifying provider → raw provider
//
// It is the unit-of-work twin of wireStorageDecorators, extracted from main.go
// for the same reason: the composition is only unit-testable as a function.
// The notifying layer is present only when sinks carry a hook runner;
// uow.NewNotifyingProvider returns the provider unwrapped otherwise.
func wireProxiedUOWProvider(provider uow.UnitOfWorkProvider, sinks uow.Sinks) uow.UnitOfWorkProvider {
return wireExternalDependencyUOWProvider(uow.NewNotifyingProvider(provider, sinks))
}
func wireExternalDependencyUOWProvider(provider uow.UnitOfWorkProvider) uow.UnitOfWorkProvider {
return externaldeps.WrapUOWProvider(
provider,

View file

@ -3,11 +3,14 @@ package main
import (
"context"
"reflect"
"strings"
"testing"
"github.com/steveyegge/beads/internal/hooks"
"github.com/steveyegge/beads/internal/storage"
"github.com/steveyegge/beads/internal/storage/domain"
"github.com/steveyegge/beads/internal/storage/externaldeps"
"github.com/steveyegge/beads/internal/storage/uow"
"github.com/steveyegge/beads/internal/telemetry"
)
@ -207,6 +210,139 @@ func TestKVPrefixReaderAbsentCapabilityFallsBack(t *testing.T) {
}
}
// kvListStubRows is every config row the proxied stubs below serve: two keys
// under the listed prefix, a sibling key that shares its leading characters, an
// unrelated kv key, and a non-kv config row.
var kvListStubRows = map[string]string{
"kv.mail.dog.m1": "1",
"kv.mail.dog.m2": "2",
"kv.mail.doggerel.m3": "3",
"kv.other": "4",
"issue_prefix": "bd",
}
// wantKVListMailDog is what `bd kv list --prefix mail.dog.` prints over
// kvListStubRows, whichever read served it.
const wantKVListMailDog = "\nKey-Value Store:\n mail.dog.m1 = 1\n mail.dog.m2 = 2\n"
func kvListStubRowsWithPrefix(prefix string) map[string]string {
out := make(map[string]string)
for k, v := range kvListStubRows {
if strings.HasPrefix(k, prefix) {
out[k] = v
}
}
return out
}
// stubFullConfigUC is a config use case WITHOUT the optional prefix-read
// capability: GetAllConfig is its only read, and it counts them.
type stubFullConfigUC struct {
domain.ConfigUseCase // any other method is a nil call: kv list must not reach one
fullReads int
}
func (s *stubFullConfigUC) GetAllConfig(context.Context) (map[string]string, error) {
s.fullReads++
return kvListStubRowsWithPrefix(""), nil
}
// stubPrefixConfigUC is the proxied twin of stubPrefixChainStore: it adds the
// capability and records the prefixes it was asked for. Like the real use case,
// the prefix read returns only the rows under the prefix and the full read
// returns every row, so only the counters can tell the arms apart.
type stubPrefixConfigUC struct {
stubFullConfigUC
prefixReads []string
}
func (s *stubPrefixConfigUC) GetConfigByPrefix(_ context.Context, prefix string) (map[string]string, error) {
s.prefixReads = append(s.prefixReads, prefix)
return kvListStubRowsWithPrefix(prefix), nil
}
// configOnlyUOW serves the config use case and nothing else; kv list reads
// nothing else.
type configOnlyUOW struct {
uow.UnitOfWork
cfg domain.ConfigUseCase
}
func (u configOnlyUOW) Close(context.Context) {}
func (u configOnlyUOW) ConfigUseCase() domain.ConfigUseCase { return u.cfg }
type configOnlyProvider struct{ cfg domain.ConfigUseCase }
func (p configOnlyProvider) NewUOW(context.Context) (uow.UnitOfWork, error) {
return configOnlyUOW{cfg: p.cfg}, nil
}
func (p configOnlyProvider) Close(context.Context) error { return nil }
// runKVListProxiedThrough runs the proxied `bd kv list --prefix` command
// against provider and returns what it printed.
func runKVListProxiedThrough(t *testing.T, provider uow.UnitOfWorkProvider, prefix string) string {
t.Helper()
oldProvider, oldJSON := uowProvider, jsonOutput
uowProvider, jsonOutput = provider, false
t.Cleanup(func() { uowProvider, jsonOutput = oldProvider, oldJSON })
return captureStdout(t, func() error {
return runKVListProxiedServer(t.Context(), prefix)
})
}
// TestKVListProxiedPrefixReadSurvivesProviderChain is the proxied-server twin
// of TestKVPrefixReaderUnwrapsStorageDecorators. runKVListProxiedServer
// discovers the prefix fast path by asserting on the unit of work's config use
// case, and a miss falls back to GetAllConfig plus kvPairsWithPrefix — the same
// rows, so a lost capability fails nothing but a read counter. Every layer
// wireProxiedUOWProvider installs embeds uow.UnitOfWork and passes
// ConfigUseCase through; a layer that wrapped the use case in a type embedding
// domain.ConfigUseCase, which does not declare GetConfigByPrefix, would turn
// every proxied `bd kv list --prefix` into a full scan. Covers both chain
// configurations and drives the command's own read.
func TestKVListProxiedPrefixReadSurvivesProviderChain(t *testing.T) {
for _, tc := range []struct {
name string
sinks uow.Sinks
}{
{name: "HookOn", sinks: uow.Sinks{Hook: hooks.NewRunner("/nonexistent")}},
{name: "HookDisabled"},
} {
t.Run(tc.name, func(t *testing.T) {
cfg := &stubPrefixConfigUC{}
wrapped := wireProxiedUOWProvider(configOnlyProvider{cfg: cfg}, tc.sinks)
got := runKVListProxiedThrough(t, wrapped, "mail.dog.")
if cfg.fullReads != 0 {
t.Fatalf("prefix read fell back to %d full GetAllConfig scan(s) through the wired chain (%T); proxied kv list --prefix would silently full-scan", cfg.fullReads, wrapped)
}
if want := []string{"kv.mail.dog."}; !reflect.DeepEqual(cfg.prefixReads, want) {
t.Errorf("use case prefix reads = %v; want %v — the read never reached the use case's fast path", cfg.prefixReads, want)
}
if got != wantKVListMailDog {
t.Errorf("kv list --prefix mail.dog. printed %q; want %q", got, wantKVListMailDog)
}
})
}
}
// TestKVListProxiedPrefixAbsentCapabilityFallsBack keeps the test above honest:
// a use case without GetConfigByPrefix must be served by one full read, which
// the listing narrows to the same rows the fast path returns.
func TestKVListProxiedPrefixAbsentCapabilityFallsBack(t *testing.T) {
cfg := &stubFullConfigUC{}
wrapped := wireProxiedUOWProvider(configOnlyProvider{cfg: cfg}, uow.Sinks{Hook: hooks.NewRunner("/nonexistent")})
got := runKVListProxiedThrough(t, wrapped, "mail.dog.")
if cfg.fullReads != 1 {
t.Errorf("use case without GetConfigByPrefix served %d full read(s); want exactly 1", cfg.fullReads)
}
if got != wantKVListMailDog {
t.Errorf("kv list --prefix mail.dog. printed %q; want %q", got, wantKVListMailDog)
}
}
func TestWireStorageDecorators_TelemetryOn_HookDisabled(t *testing.T) {
clearTelemetryEnv(t)
t.Setenv("BD_OTEL_STDOUT", "true")