diff --git a/.github/scripts/embedded-cmd-test-shards.txt b/.github/scripts/embedded-cmd-test-shards.txt index 57efd5f3d..d756a3a4c 100644 --- a/.github/scripts/embedded-cmd-test-shards.txt +++ b/.github/scripts/embedded-cmd-test-shards.txt @@ -90,6 +90,8 @@ 20 2 TestEmbeddedLintConcurrent 20 3 TestEmbeddedList 20 4 TestEmbeddedListConcurrent +20 13 TestEmbeddedMaxRowsList +20 11 TestEmbeddedMaxRowsNonListPaths 20 5 TestEmbeddedMemory 20 6 TestEmbeddedMemoryConcurrent 20 7 TestEmbeddedMigrate diff --git a/.github/workflows/nightly.yml b/.github/workflows/nightly.yml index 6a64c85e3..a1e1355ad 100644 --- a/.github/workflows/nightly.yml +++ b/.github/workflows/nightly.yml @@ -148,6 +148,27 @@ jobs: BEADS_TEST_SKIP: dolt run: go test -v -race -tags=integration,gms_pure_go -coverprofile=coverage.out -timeout=30m ./... + # The suite above never sets BEADS_TEST_EMBEDDED_DOLT, so every + # skipUnlessEmbeddedDolt-gated test in internal/storage/embeddeddolt + # (including TestBatchApplyContract and the 1000-item case of + # TestLargeBatchApplyWallClock_Embedded) is skipped there, on every + # lane, not just this one. This step is their only non-race coverage of + # a real 1000-item apply through the embedded backend: the race lanes + # (main.yml, pr-risk.yml) shrink that subtest under raceEnabled — see + # internal/storage/embeddeddolt/large_batch_apply_measure_test.go — and + # the PR-conditional server-Dolt full suite (full_embedded in + # .github/scripts/ci-embedded-tier.sh) covers the SAME write body + # (internal/storage/issueops.ApplyBatchInTx) through a different + # backend, not this one. Non-race here because the embedded engine's + # own goroutine/lock machinery runs inside this test binary and the + # race detector dramatically inflates its wall-clock (see + # race_detector_on_test.go), which is exactly what + # TestLargeBatchApplyWallClock_Embedded is measuring. + - name: Embedded Dolt batch-apply suite (non-race) + env: + BEADS_TEST_EMBEDDED_DOLT: "1" + run: go test -tags gms_pure_go -run '^(TestBatchApplyContract|TestLargeBatchApplyWallClock_Embedded)$' ./internal/storage/embeddeddolt + - name: Check coverage threshold run: | COVERAGE=$(go tool cover -func=coverage.out | grep total | awk '{print $3}' | sed 's/%//') diff --git a/backend/conformance/batch_apply_contract.go b/backend/conformance/batch_apply_contract.go index e026872b9..354f0cde3 100644 --- a/backend/conformance/batch_apply_contract.go +++ b/backend/conformance/batch_apply_contract.go @@ -1782,21 +1782,56 @@ func RunBatchApplyLandsAnIdempotencyRecordWithItsWork(t *testing.T, ctx context. // alone is satisfied by a bound one step away. func RunBatchApplyBoundsTheItemCount(t *testing.T, ctx context.Context, fixture BatchApplyFixture) { t.Helper() + RunBatchApplyBoundsTheItemCountAtScale(t, ctx, fixture, publicops.MaxApplyBatchItems) +} + +// RunBatchApplyBoundsTheItemCountAtScale is RunBatchApplyBoundsTheItemCount +// with the "at bound" half's REAL applied count overridable to fewer than +// issueops.MaxApplyBatchItems, for a backend whose real engine makes +// applying a full MaxApplyBatchItems batch prohibitively slow in one CI +// configuration (embedded-dolt under the race detector: this subtest alone +// measured 903-945s there, because the in-process Dolt engine's own +// goroutine/lock machinery runs inside the instrumented binary). +// +// This does NOT weaken the boundary this contract pins. The production +// bound itself — exactly issueops.MaxApplyBatchItems accepted, one more +// refused — is independent of atBoundItems and is NOT re-derived from a +// real apply here: every backend's ApplyBatch calls the SAME +// storage.PlanApplyBatch to decide accept-vs-refuse, before touching +// storage at all (batch_applier.go: "VALIDATION HAPPENS BEFORE THE +// TRANSACTION"), and that shared function's own boundary is independently +// pinned, for real, pure and sub-millisecond, by +// TestPlanApplyBatchAcceptsTheBoundary and its "too many items" sibling in +// internal/storage/batch_apply_test.go — run on every `go test`, every +// backend, every PR, unaffected by atBoundItems. The refusing half below is +// UNCHANGED regardless of atBoundItems: it still sends the true +// MaxApplyBatchItems+1 count, because that half is already O(1) (the same +// pre-transaction length check, no backend ever reaches a write). Only the +// "at bound" half's REAL applied count is adjustable, to prove a backend's +// own write body does not drop, double or truncate a large accepted batch +// without paying for the full MaxApplyBatchItems every time one backend's +// real engine makes that unusually expensive. +func RunBatchApplyBoundsTheItemCountAtScale(t *testing.T, ctx context.Context, fixture BatchApplyFixture, atBoundItems int) { + t.Helper() + if atBoundItems < 1 || atBoundItems > publicops.MaxApplyBatchItems { + t.Fatalf("atBoundItems = %d, want 1..%d (the production bound this contract pins)", atBoundItems, publicops.MaxApplyBatchItems) + } const accepted = "batch apply at the item bound" const refused = "batch apply over the item bound" - atBound := make([]publicops.ApplyItem, publicops.MaxApplyBatchItems) + atBound := make([]publicops.ApplyItem, atBoundItems) for i := range atBound { atBound[i] = batchApplyCreate("", batchApplyMintedIssue(accepted)) } result := batchApplyMust(t, ctx, fixture, publicops.ApplyBatchRequest{Actor: "apply-writer", Items: atBound}) - if len(result.Items) != publicops.MaxApplyBatchItems { - t.Fatalf("a request of exactly MaxApplyBatchItems returned %d item results, want %d", len(result.Items), publicops.MaxApplyBatchItems) + if len(result.Items) != atBoundItems { + t.Fatalf("a request of %d items returned %d item results, want %d", atBoundItems, len(result.Items), atBoundItems) } - if got := batchApplyCount(t, ctx, fixture, "SELECT COUNT(*) FROM issues WHERE title = ?", []any{accepted}); got != publicops.MaxApplyBatchItems { - t.Errorf("%d row(s) landed for a request of exactly MaxApplyBatchItems, want %d", got, publicops.MaxApplyBatchItems) + if got := batchApplyCount(t, ctx, fixture, "SELECT COUNT(*) FROM issues WHERE title = ?", []any{accepted}); got != atBoundItems { + t.Errorf("%d row(s) landed for a request of %d items, want %d", got, atBoundItems, atBoundItems) } + // Unchanged: the true production bound, regardless of atBoundItems. overBound := make([]publicops.ApplyItem, publicops.MaxApplyBatchItems+1) for i := range overBound { overBound[i] = batchApplyCreate("", batchApplyMintedIssue(refused)) diff --git a/cmd/bd/BUILD.bazel b/cmd/bd/BUILD.bazel index 0e22add77..79331ddcb 100644 --- a/cmd/bd/BUILD.bazel +++ b/cmd/bd/BUILD.bazel @@ -510,7 +510,7 @@ go_test( "agent_hook_test.go", "assign_fence_embedded_test.go", "assign_fence_proxied_test.go", - "auto_import_upgrade_test.go", + "auto_import_upgrade_embedded_test.go", "auto_import_upgrade_unit_test.go", "autocommit_embedded_test.go", "backend_registry_contract_test.go", @@ -657,7 +657,7 @@ go_test( "dep_cycles_test.go", "dep_edge_reader_test.go", "dep_embedded_test.go", - "dep_list_cross_db_test.go", + "dep_list_cross_db_embedded_test.go", "dep_proxied_integration_test.go", "dep_relation_test.go", "dep_test.go", @@ -901,6 +901,7 @@ go_test( "main_test.go", "maintenance_commands_proxied_integration_test.go", "markdown_test.go", + "max_rows_embedded_test.go", "max_rows_test.go", "memory_embedded_test.go", "memory_proxied_integration_test.go", @@ -996,6 +997,7 @@ go_test( "prune_proxied_integration_test.go", "purge_embedded_test.go", "purge_test.go", + "purge_wisps_plane_embedded_test.go", "purge_wisps_plane_integration_test.go", "purge_wisps_plane_managed_local_test.go", "query_embedded_test.go", @@ -1125,6 +1127,7 @@ go_test( "storage_class_embedded_test.go", "storage_class_proxied_integration_test.go", "storage_class_pure_test.go", + "store_factory_embedded_test.go", "store_factory_nocgo_test.go", "store_factory_test.go", "store_reopen_embedded_resolution_test.go", @@ -1161,6 +1164,7 @@ go_test( "tracker_frontdoor_proxy_test.go", "tracker_proxy_uow_conformance_integration_test.go", "types_builtin_test.go", + "types_create_agreement_embedded_test.go", "types_create_agreement_test.go", "types_embedded_test.go", "unclaim_conditional_embedded_test.go", @@ -1240,6 +1244,7 @@ go_test( "//cmd/bd/doctor", "//internal/ado", "//internal/beads", + "//internal/ceiling", "//internal/config", "//internal/configfile", "//internal/debug", diff --git a/cmd/bd/auto_import_upgrade_test.go b/cmd/bd/auto_import_upgrade_embedded_test.go similarity index 100% rename from cmd/bd/auto_import_upgrade_test.go rename to cmd/bd/auto_import_upgrade_embedded_test.go diff --git a/cmd/bd/dep_list_cross_db_test.go b/cmd/bd/dep_list_cross_db_embedded_test.go similarity index 100% rename from cmd/bd/dep_list_cross_db_test.go rename to cmd/bd/dep_list_cross_db_embedded_test.go diff --git a/cmd/bd/max_rows_embedded_test.go b/cmd/bd/max_rows_embedded_test.go new file mode 100644 index 000000000..ac2ccf481 --- /dev/null +++ b/cmd/bd/max_rows_embedded_test.go @@ -0,0 +1,689 @@ +//go:build cgo + +// be-u8z9: CLI-layer behavioral tests for BEADS_MAX_ROWS / --max-rows on the +// command paths beyond `bd list`. Each subtest: +// - inits a fresh rig (own DB so the row counts are exact) +// - exercises the command via exec.Command(bd, ...) +// - asserts the process exited with code 2 (cap exceeded) and that stderr +// names the source ("--max-rows=N" or "BEADS_MAX_ROWS=N"). +// +// Gating matches the other embedded-dolt CLI tests: requires +// BEADS_TEST_EMBEDDED_DOLT=1 to opt in. + +package main + +import ( + "fmt" + "os" + "os/exec" + "strings" + "testing" +) + +func bdRunRaw(t *testing.T, bd, dir string, envExtras []string, args ...string) (string, int) { + t.Helper() + cmd := exec.Command(bd, args...) + cmd.Dir = dir + env := bdEnv(dir) + env = append(env, envExtras...) + cmd.Env = env + out, err := cmd.CombinedOutput() + if err == nil { + return string(out), 0 + } + if exitErr, ok := err.(*exec.ExitError); ok { + return string(out), exitErr.ExitCode() + } + t.Fatalf("bd %s unexpected non-exit error: %v\n%s", strings.Join(args, " "), err, out) + return string(out), -1 +} + +// seedReadyIssues creates n top-level (no-dep, no-blocker) issues that all +// appear in `bd ready`. Useful for tests that need exactly n ready rows. +func seedReadyIssues(t *testing.T, bd, dir string, n int) []string { + t.Helper() + ids := make([]string, n) + for i := 0; i < n; i++ { + iss := bdCreate(t, bd, dir, fmt.Sprintf("max-rows seed %d", i), "--type", "task") + ids[i] = iss.ID + } + return ids +} + +// TestEmbeddedMaxRowsNonListPaths covers the non-list CLI paths wired up in +// be-x42v.2: ready, dep tree, find-duplicates, graph --all, plus the env-only +// doctor family (lint, doctor --check=conventions, doctor --check=pollution), +// and config show emission of BEADS_MAX_ROWS. +func TestEmbeddedMaxRowsNonListPaths(t *testing.T) { + if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" { + t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt integration tests") + } + t.Parallel() + + bd := buildEmbeddedBD(t) + + // ----------- bd ready ----------- + + t.Run("ReadyMaxRows_FlagOverCap_Exits2", func(t *testing.T) { + dir, _, _ := bdInit(t, bd, "--prefix", "mrrdf") + seedReadyIssues(t, bd, dir, 6) + + out, code := bdRunRaw(t, bd, dir, nil, "ready", "--max-rows", "3") + if code != 2 { + t.Fatalf("expected exit 2 (cap exceeded), got %d\n%s", code, out) + } + if !strings.Contains(out, "too many rows") { + t.Errorf("stderr missing 'too many rows':\n%s", out) + } + if !strings.Contains(out, "--max-rows=3") { + t.Errorf("stderr missing source --max-rows=3:\n%s", out) + } + }) + + t.Run("ReadyMaxRows_EnvOverCap_Exits2", func(t *testing.T) { + dir, _, _ := bdInit(t, bd, "--prefix", "mrrde") + seedReadyIssues(t, bd, dir, 6) + + out, code := bdRunRaw(t, bd, dir, []string{"BEADS_MAX_ROWS=3"}, "ready") + if code != 2 { + t.Fatalf("expected exit 2 (cap exceeded), got %d\n%s", code, out) + } + if !strings.Contains(out, "BEADS_MAX_ROWS=3") { + t.Errorf("stderr missing source BEADS_MAX_ROWS=3:\n%s", out) + } + }) + + // `bd ready --json` and `bd list --ready --json` route through + // GetReadyWorkWithCountsInTx, a separate query path from the plain + // `bd ready` above (GetReadyWorkInTx). Mirrors + // TestEmbeddedMaxRowsList/Flag_OverCap's --json coverage for `bd list`. + t.Run("ReadyMaxRowsJSON_FlagOverCap_Exits2", func(t *testing.T) { + dir, _, _ := bdInit(t, bd, "--prefix", "mrrjf") + seedReadyIssues(t, bd, dir, 6) + + out, code := bdRunRaw(t, bd, dir, nil, "ready", "--json", "--max-rows", "3") + if code != 2 { + t.Fatalf("expected exit 2 (cap exceeded), got %d\n%s", code, out) + } + if !strings.Contains(out, "too many rows") { + t.Errorf("stderr missing 'too many rows':\n%s", out) + } + if !strings.Contains(out, "--max-rows=3") { + t.Errorf("stderr missing source --max-rows=3:\n%s", out) + } + if idx := strings.Index(out, "Error: too many rows"); idx >= 0 { + if strings.Contains(out[:idx], "[") { + t.Errorf("stdout JSON output leaked before error message:\nfull=%s", out) + } + } + }) + + t.Run("ListReadyMaxRowsJSON_FlagOverCap_Exits2", func(t *testing.T) { + dir, _, _ := bdInit(t, bd, "--prefix", "mrrjl") + seedReadyIssues(t, bd, dir, 6) + + out, code := bdRunRaw(t, bd, dir, nil, "list", "--ready", "--json", "--max-rows", "3") + if code != 2 { + t.Fatalf("expected exit 2 (cap exceeded), got %d\n%s", code, out) + } + if !strings.Contains(out, "too many rows") { + t.Errorf("stderr missing 'too many rows':\n%s", out) + } + if !strings.Contains(out, "--max-rows=3") { + t.Errorf("stderr missing source --max-rows=3:\n%s", out) + } + }) + + // be-x42v.4 follow-up (review SHOULD-FIX 8): confirms the cap still + // fires end-to-end for --include-ephemeral after propagating MaxRows + // into getReadyWispsInTx's wisp query (internal/storage/issueops/ + // ready_work.go) — that change bounds the query itself but must not + // change whether the post-merge cap check in GetReadyWorkInTx trips. + t.Run("ReadyIncludeEphemeralMaxRows_FlagOverCap_Exits2", func(t *testing.T) { + dir, _, _ := bdInit(t, bd, "--prefix", "mrre") + for i := 0; i < 6; i++ { + bdCreate(t, bd, dir, fmt.Sprintf("Ephemeral ready %d", i), "--type", "task", "--ephemeral") + } + + out, code := bdRunRaw(t, bd, dir, nil, "ready", "--include-ephemeral", "--max-rows", "3") + if code != 2 { + t.Fatalf("expected exit 2 (cap exceeded), got %d\n%s", code, out) + } + if !strings.Contains(out, "too many rows") { + t.Errorf("stderr missing 'too many rows':\n%s", out) + } + if !strings.Contains(out, "--max-rows=3") { + t.Errorf("stderr missing source --max-rows=3:\n%s", out) + } + }) + + // be-x42v.4 round-3 follow-up: GetReadyWorkWithCountsInTx (the `--json` + // path) independently bounds each of the issues and wisps queries by + // EffectiveSearchLimit(Limit, MaxRows), so with --include-ephemeral the + // PRE-TRIM merge of two tables can hold up to ~2x that per-table bound + // even though the caller's own --limit already keeps the DELIVERED page + // well within the cap. limit=2, cap=3, 2 ready rows in each table: each + // table's query returns 2 (2<=limit, no overage sniff at the per-table + // level — EffectiveSearchLimit(2,3)=2), the pre-trim merge is 4>cap=3, + // but the page actually handed back to the caller is trimmed to 2<=cap. + // Must exit 0, not trip the cap on the untrimmed merge. + t.Run("ReadyIncludeEphemeralMaxRowsJSON_MergedUnderLimit_NoError", func(t *testing.T) { + dir, _, _ := bdInit(t, bd, "--prefix", "mrrm") + for i := 0; i < 2; i++ { + bdCreate(t, bd, dir, fmt.Sprintf("Plain ready %d", i), "--type", "task") + } + for i := 0; i < 2; i++ { + bdCreate(t, bd, dir, fmt.Sprintf("Ephemeral ready %d", i), "--type", "task", "--ephemeral") + } + + out, code := bdRunRaw(t, bd, dir, nil, "ready", "--json", "--include-ephemeral", + "--limit", "2", "--max-rows", "3") + if code != 0 { + t.Fatalf("expected exit 0 (delivered page under cap despite pre-trim merge overage), got %d\n%s", code, out) + } + if strings.Contains(out, "too many rows") { + t.Errorf("delivered-page-under-cap should not emit cap error:\n%s", out) + } + }) + + // ----------- bd dep tree ----------- + + t.Run("DepTreeMaxRows_TreeNodes_Exits2", func(t *testing.T) { + dir, _, _ := bdInit(t, bd, "--prefix", "mrdt") + // All tasks: default "blocks" dep type rejects epic→task, but task→task + // is fine and produces a tree with the same shape for cap purposes. + root := bdCreate(t, bd, dir, "Tree root", "--type", "task") + // Add 5 children so the dep tree has 6 nodes total (root + 5). + for i := 0; i < 5; i++ { + child := bdCreate(t, bd, dir, fmt.Sprintf("Tree dep %d", i), "--type", "task") + // `bd dep add A B` makes A depend on B (default dep type is "blocks"). + bdDepAdd(t, bd, dir, root.ID, child.ID) + } + + // Sanity: tree of size 6, cap of 2 → must exit 2 with source attribution. + out, code := bdRunRaw(t, bd, dir, nil, "dep", "tree", root.ID, "--max-rows", "2") + if code != 2 { + t.Fatalf("expected exit 2 (tree size > cap), got %d\n%s", code, out) + } + if !strings.Contains(out, "too many rows") { + t.Errorf("stderr missing 'too many rows':\n%s", out) + } + if !strings.Contains(out, "--max-rows=2") { + t.Errorf("stderr missing source --max-rows=2:\n%s", out) + } + }) + + // ----------- bd find-duplicates ----------- + + t.Run("FindDuplicatesMaxRows_Exits2", func(t *testing.T) { + dir, _, _ := bdInit(t, bd, "--prefix", "mrfd") + seedReadyIssues(t, bd, dir, 6) + + out, code := bdRunRaw(t, bd, dir, nil, "find-duplicates", "--max-rows", "3") + if code != 2 { + t.Fatalf("expected exit 2 (cap exceeded), got %d\n%s", code, out) + } + if !strings.Contains(out, "--max-rows=3") { + t.Errorf("stderr missing source --max-rows=3:\n%s", out) + } + }) + + // ----------- bd graph --all ----------- + + t.Run("GraphAllMaxRows_Exits2", func(t *testing.T) { + dir, _, _ := bdInit(t, bd, "--prefix", "mrgr") + seedReadyIssues(t, bd, dir, 6) + + out, code := bdRunRaw(t, bd, dir, nil, "graph", "--all", "--max-rows", "3") + if code != 2 { + t.Fatalf("expected exit 2 (cap exceeded), got %d\n%s", code, out) + } + if !strings.Contains(out, "--max-rows=3") { + t.Errorf("stderr missing source --max-rows=3:\n%s", out) + } + }) + + // be-x42v.4 follow-up (review SHOULD-FIX 7): `bd graph ` without + // --all never resolved the cap at all before the fix — loadGraphSubgraph + // is a BFS over per-ID GetDependents/GetDependencies lookups with no + // IssueFilter to thread MaxRows through, so this checks the final + // connected-component node count post-hoc, mirroring dep tree. + t.Run("GraphSingleIssueMaxRows_NodeCount_Exits2", func(t *testing.T) { + dir, _, _ := bdInit(t, bd, "--prefix", "mrgs") + root := bdCreate(t, bd, dir, "Graph root", "--type", "task") + for i := 0; i < 5; i++ { + child := bdCreate(t, bd, dir, fmt.Sprintf("Graph dep %d", i), "--type", "task") + bdDepAdd(t, bd, dir, root.ID, child.ID) + } + + // Connected component of size 6 (root + 5), cap of 2 → exit 2. + out, code := bdRunRaw(t, bd, dir, nil, "graph", root.ID, "--max-rows", "2") + if code != 2 { + t.Fatalf("expected exit 2 (subgraph size > cap), got %d\n%s", code, out) + } + if !strings.Contains(out, "too many rows") { + t.Errorf("stderr missing 'too many rows':\n%s", out) + } + if !strings.Contains(out, "--max-rows=2") { + t.Errorf("stderr missing source --max-rows=2:\n%s", out) + } + }) + + t.Run("GraphSingleIssueMaxRows_UnderCap_NoError", func(t *testing.T) { + dir, _, _ := bdInit(t, bd, "--prefix", "mrgu") + root := bdCreate(t, bd, dir, "Graph root under cap", "--type", "task") + + out, code := bdRunRaw(t, bd, dir, nil, "graph", root.ID, "--max-rows", "5") + if code != 0 { + t.Fatalf("expected exit 0 (single-node subgraph under cap), got %d\n%s", code, out) + } + if strings.Contains(out, "too many rows") { + t.Errorf("under-cap single-issue graph should not emit cap error:\n%s", out) + } + }) + + // ----------- bd lint (env-only) ----------- + + t.Run("LintMaxRows_EnvOnly_Exits2", func(t *testing.T) { + dir, _, _ := bdInit(t, bd, "--prefix", "mrln") + seedReadyIssues(t, bd, dir, 6) + + out, code := bdRunRaw(t, bd, dir, []string{"BEADS_MAX_ROWS=3"}, "lint") + if code != 2 { + t.Fatalf("expected exit 2 (cap exceeded via env), got %d\n%s", code, out) + } + if !strings.Contains(out, "BEADS_MAX_ROWS=3") { + t.Errorf("stderr missing source BEADS_MAX_ROWS=3:\n%s", out) + } + }) + + t.Run("LintMaxRows_NoFlagAccepted", func(t *testing.T) { + // Designer §4: doctor family is env-opt-in only. The --max-rows + // flag must NOT be registered on `bd lint`; cobra should reject + // it as unknown. + dir, _, _ := bdInit(t, bd, "--prefix", "mrlnf") + + out, code := bdRunRaw(t, bd, dir, nil, "lint", "--max-rows", "1") + if code == 0 { + t.Fatalf("expected non-zero exit (cobra rejects unknown flag), got 0\n%s", out) + } + if !strings.Contains(out, "unknown flag") { + t.Errorf("stderr missing 'unknown flag' rejection:\n%s", out) + } + // Defense against false positives: if `--max-rows` is wired on lint + // by mistake, this assertion would not trip — but the unknown-flag + // check above already covers it. + }) + + // ----------- bd doctor --check=conventions (env-only) ----------- + // + // bd doctor is hard-gated to server mode (doctor.go:188 prints "not yet + // supported in embedded mode" and exits 0 before reaching the check + // dispatch). The embedded-Dolt test rig used here cannot exercise those + // code paths. The cap logic itself runs through the shared SearchIssues + + // EnforceMaxRowsCap path already covered by: + // - TestEnforceMaxRowsCap_* (internal/storage/issueops/search_test.go) + // - TestGetReadyWork_MaxRowsSuite (cmd/bd/ready_max_rows_test.go) + // - be-x42v.3 storage parity tests (bd list/search backend matrix) + // What's _not_ covered without server mode is the env-only resolver + // (resolveMaxRowsEnvOnly) on these specific commands. doctor_conventions.go + // and doctor_pollution.go both call it and pipe through handleMaxRowsError + // identically to lint.go, which IS covered above by LintMaxRows_EnvOnly_Exits2. + // A server-mode follow-up should add behavioral coverage here. + + t.Run("ConventionsMaxRows_EnvOnly_Exits2", func(t *testing.T) { + t.Skip("bd doctor requires server mode (doctor.go:188); server-mode parity covered by separate validator bead") + }) + + t.Run("PollutionMaxRows_EnvOnly_Exits2", func(t *testing.T) { + t.Skip("bd doctor requires server mode (doctor.go:188); server-mode parity covered by separate validator bead") + }) + + // ----------- bd config show ----------- + + t.Run("ConfigShow_ListsBeadsMaxRows", func(t *testing.T) { + dir, _, _ := bdInit(t, bd, "--prefix", "mrcs") + + // When BEADS_MAX_ROWS is set in the environment, `bd config show` + // must surface it in the standalone-env entries (config_show.go + // collectStandaloneEnvEntries). + out, code := bdRunRaw(t, bd, dir, []string{"BEADS_MAX_ROWS=42"}, "config", "show") + if code != 0 { + t.Fatalf("expected exit 0 from `bd config show`, got %d\n%s", code, out) + } + if !strings.Contains(out, "BEADS_MAX_ROWS") { + t.Errorf("expected `bd config show` to list BEADS_MAX_ROWS in output:\n%s", out) + } + if !strings.Contains(out, "42") { + t.Errorf("expected `bd config show` to display the value 42:\n%s", out) + } + + // When the env var is unset, the entry must be absent (designer §4: + // opt-in only; default-disabled). + out2, code2 := bdRunRaw(t, bd, dir, nil, "config", "show") + if code2 != 0 { + t.Fatalf("expected exit 0 from `bd config show` (unset), got %d\n%s", code2, out2) + } + if strings.Contains(out2, "BEADS_MAX_ROWS") { + t.Errorf("expected BEADS_MAX_ROWS to be absent when env is unset:\n%s", out2) + } + }) +} + +// countListIDs returns the number of issue IDs of the form "-" +// observed in the combined `bd list` output. Used to verify that --limit +// actually clipped the result set without depending on JSON parsing of the +// pretty-printed array (which spans many lines). +func countListIDs(out, prefix string) int { + // Each list row prints the issue ID once; counting prefix occurrences in + // raw stdout is a robust enough check for our small fixtures. + return strings.Count(out, prefix+"-") +} + +// TestEmbeddedMaxRowsList covers the 10 designer §6.1 behavioral scenarios +// for `bd list`. Builder bead: be-x42v.2 (CLI wiring). All subtests share a +// single rig of 21 open task issues — the dataset size from the designer's +// fixture. +func TestEmbeddedMaxRowsList(t *testing.T) { + if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" { + t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt integration tests") + } + t.Parallel() + + bd := buildEmbeddedBD(t) + dir, _, _ := bdInit(t, bd, "--prefix", "mrl") + + const totalRows = 21 + for i := 0; i < totalRows; i++ { + bdCreate(t, bd, dir, fmt.Sprintf("List max-rows %d", i), "--type", "task") + } + + t.Run("Disabled_NoEnv", func(t *testing.T) { + // Baseline: with no flag and no env var, `bd list` returns all rows. + out, code := bdRunRaw(t, bd, dir, nil, "list") + if code != 0 { + t.Fatalf("expected exit 0 (no cap), got %d\n%s", code, out) + } + if strings.Contains(out, "too many rows") { + t.Errorf("baseline `bd list` should not emit cap error:\n%s", out) + } + if got := countListIDs(out, "mrl"); got < totalRows { + t.Errorf("expected at least %d rows in output, got %d", totalRows, got) + } + }) + + t.Run("Flag_UnderCap", func(t *testing.T) { + // 21 rows under cap=100: success. + out, code := bdRunRaw(t, bd, dir, nil, "list", "--max-rows", "100") + if code != 0 { + t.Fatalf("expected exit 0 (under cap), got %d\n%s", code, out) + } + if strings.Contains(out, "too many rows") { + t.Errorf("under-cap `bd list` should not emit cap error:\n%s", out) + } + }) + + t.Run("Flag_OverCap", func(t *testing.T) { + // 21 rows over cap=5: exit 2, stderr includes --max-rows=5, stdout + // empty (designer §2.3: stdout is suppressed so `jq` downstream + // doesn't trip on partial JSON). + out, code := bdRunRaw(t, bd, dir, nil, "list", "--json", "--max-rows", "5") + if code != 2 { + t.Fatalf("expected exit 2 (cap exceeded), got %d\n%s", code, out) + } + if !strings.Contains(out, "too many rows") { + t.Errorf("expected 'too many rows' in stderr:\n%s", out) + } + if !strings.Contains(out, "--max-rows=5") { + t.Errorf("expected source `--max-rows=5` in stderr:\n%s", out) + } + // stdout-emptiness check: there should be no JSON `[` array opener + // in the merged output before the error. Cap-exceeded must not emit + // a partial array. + if idx := strings.Index(out, "Error: too many rows"); idx >= 0 { + before := out[:idx] + if strings.Contains(before, "[") { + t.Errorf("stdout JSON output leaked before error message:\nbefore=%q\nfull=%s", before, out) + } + } + }) + + t.Run("Env_OverCap", func(t *testing.T) { + out, code := bdRunRaw(t, bd, dir, []string{"BEADS_MAX_ROWS=5"}, "list") + if code != 2 { + t.Fatalf("expected exit 2 (cap exceeded via env), got %d\n%s", code, out) + } + if !strings.Contains(out, "BEADS_MAX_ROWS=5") { + t.Errorf("expected source `BEADS_MAX_ROWS=5` in stderr:\n%s", out) + } + }) + + t.Run("Flag_OverridesEnv", func(t *testing.T) { + // env says cap=5, flag says cap=100 — flag wins, expect success. + out, code := bdRunRaw(t, bd, dir, []string{"BEADS_MAX_ROWS=5"}, + "list", "--max-rows", "100") + if code != 0 { + t.Fatalf("expected exit 0 (flag overrides env), got %d\n%s", code, out) + } + if strings.Contains(out, "too many rows") { + t.Errorf("flag-override should not emit cap error:\n%s", out) + } + }) + + t.Run("Flag_Zero_OverridesEnv", func(t *testing.T) { + // Explicit `--max-rows 0` disables the cap even when env is set + // (designer §2.1: ops shells with a global env can run a known- + // unbounded query without unsetting). + out, code := bdRunRaw(t, bd, dir, []string{"BEADS_MAX_ROWS=5"}, + "list", "--max-rows", "0") + if code != 0 { + t.Fatalf("expected exit 0 (--max-rows 0 disables), got %d\n%s", code, out) + } + if strings.Contains(out, "too many rows") { + t.Errorf("explicit `--max-rows 0` should disable the cap:\n%s", out) + } + }) + + t.Run("BadEnv_LogsAndIgnores", func(t *testing.T) { + // env=banana → warning to stderr, normal output, exit 0 + // (designer §2.1: fail-open on bad env so a typo in a global shell + // doesn't break automation). + out, code := bdRunRaw(t, bd, dir, []string{"BEADS_MAX_ROWS=banana"}, "list") + if code != 0 { + t.Fatalf("expected exit 0 (bad env ignored), got %d\n%s", code, out) + } + // max_rows.go:79 emits a "Warning: BEADS_MAX_ROWS=\"banana\" is not + // a non-negative integer; ignoring." line. + if !strings.Contains(out, "Warning") || !strings.Contains(out, "BEADS_MAX_ROWS") { + t.Errorf("expected warning about bad env in stderr:\n%s", out) + } + if strings.Contains(out, "too many rows") { + t.Errorf("bad env should not trigger cap error:\n%s", out) + } + }) + + t.Run("Negative_FlagRejected", func(t *testing.T) { + // max_rows.go:55 calls FatalError on negative — exit 1 with message. + out, code := bdRunRaw(t, bd, dir, nil, "list", "--max-rows", "-1") + if code != 1 { + t.Fatalf("expected exit 1 (usage error), got %d\n%s", code, out) + } + if !strings.Contains(out, "must be non-negative") { + t.Errorf("expected 'must be non-negative' in stderr:\n%s", out) + } + }) + + t.Run("LimitSet_CapTighter", func(t *testing.T) { + // limit=100, cap=5: 21 rows are scanned (LIMIT cap+1=6 sniffs + // overage), cap fires, exit 2. The --limit flag does not protect + // callers who set a generous limit but also opt into the cap. + out, code := bdRunRaw(t, bd, dir, nil, "list", + "--limit", "100", "--max-rows", "5") + if code != 2 { + t.Fatalf("expected exit 2 (cap tighter than limit), got %d\n%s", code, out) + } + if !strings.Contains(out, "--max-rows=5") { + t.Errorf("expected source `--max-rows=5` in stderr:\n%s", out) + } + }) + + t.Run("LimitSet_CapLooser", func(t *testing.T) { + // limit=5, cap=100: EffectiveSearchLimit returns 5 (limit wins when + // under cap), no overage detection, 5 rows returned, exit 0. + out, code := bdRunRaw(t, bd, dir, nil, "list", + "--limit", "5", "--max-rows", "100") + if code != 0 { + t.Fatalf("expected exit 0 (limit under cap), got %d\n%s", code, out) + } + if strings.Contains(out, "too many rows") { + t.Errorf("limit-under-cap should not emit cap error:\n%s", out) + } + if got := countListIDs(out, "mrl"); got != 5 { + t.Errorf("expected exactly 5 issue IDs in output, got %d:\n%s", got, out) + } + }) + + // be-x42v.4 follow-up (review MUST-FIX 4, revised round-3): --limit N + // --max-rows N with more than N matches must truncate to N and exit 0 + // (the standard >N-matches truncation semantics), not error. + // + // Round-2 fix (regressed, since replaced): unconditionally skipped the + // workapi.WithFetchOneExtra probe-row bump when Limit>=MaxRows. That avoided + // the false cap error but also stopped fetching the extra row entirely, + // so len(results) could never exceed effectiveLimit and the GH#3212 + // truncation notice silently stopped firing for this case — a partial + // result started presenting as complete. + // + // Round-3 fix: still fetch the probe row (Limit bumps to N+1 as + // normal), but when Limit==MaxRows exactly, bump MaxRows to N+1 too, in + // lockstep, so EnforceMaxRowsCap's comparison excludes the probe row + // from the cap check while the CLI's own len(results)>effectiveLimit + // truncation detection still sees it. See workapi.WithFetchOneExtra's doc + // comment in list.go for why this can't false-negative a real + // violation. Covered directly (bump values, not exit behavior) by + // TestWithFetchOneExtra_LimitEqualsCap_BumpsBothForTruncationProbe + // below, since printTruncationHint's text is gated on + // ui.IsStderrTerminal() and unconditionally suppressed for this + // subprocess harness's piped stderr (see list_embedded_test.go's + // limit_truncation_hint subtest) — these end-to-end subtests can only + // assert the absence of the false cap error and the delivered count. + t.Run("LimitEqualsCap_TruncatesNotErrors", func(t *testing.T) { + out, code := bdRunRaw(t, bd, dir, nil, "list", + "--limit", "5", "--max-rows", "5") + if code != 0 { + t.Fatalf("expected exit 0 (limit==cap truncates, does not error), got %d\n%s", code, out) + } + if strings.Contains(out, "too many rows") { + t.Errorf("limit==cap should not emit cap error:\n%s", out) + } + if got := countListIDs(out, "mrl"); got != 5 { + t.Errorf("expected exactly 5 issue IDs in output, got %d:\n%s", got, out) + } + }) + + t.Run("LimitEqualsCap_JSON_TruncatesNotErrors", func(t *testing.T) { + out, code := bdRunRaw(t, bd, dir, nil, "list", "--json", + "--limit", "5", "--max-rows", "5") + if code != 0 { + t.Fatalf("expected exit 0 (limit==cap truncates under --json), got %d\n%s", code, out) + } + if strings.Contains(out, "too many rows") { + t.Errorf("limit==cap under --json should not emit cap error:\n%s", out) + } + }) + + // be-x42v.4 round-4 follow-up: the round-3 fix to + // finishReadyWorkWithCounts (trim to Limit before EnforceMaxRowsCap) + // wasn't mirrored onto the sibling bd list search paths + // (searchInTx in search.go, finishSearchIssuesWithCounts in + // search_counts.go), which still capped the pre-trim merged + // issues+wisps set. `--include-infra` disables the default SkipWisps + // escape hatch, so the wisps merge actually runs. limit=2, cap=5, 3 + // durable + 3 wisp issues: each leg's query is independently bounded + // by EffectiveSearchLimit, so the merge can produce up to 6 rows + // pre-trim (>cap=5) even though the delivered page (trimmed to 2) is + // well within the cap. Covers both bd list output modes (opus's live + // repro found both non-JSON and --json tripping the false cap error). + t.Run("IncludeInfraMergedUnderLimit_NoError", func(t *testing.T) { + mDir, _, _ := bdInit(t, bd, "--prefix", "mrii") + for i := 0; i < 3; i++ { + bdCreate(t, bd, mDir, fmt.Sprintf("Durable infra %d", i), "--type", "task") + } + for i := 0; i < 3; i++ { + bdCreate(t, bd, mDir, fmt.Sprintf("Wisp infra %d", i), "--type", "task", "--ephemeral") + } + + out, code := bdRunRaw(t, bd, mDir, nil, "list", "--include-infra", + "--limit", "2", "--max-rows", "5") + if code != 0 { + t.Fatalf("expected exit 0 (delivered page under cap despite pre-trim merge overage), got %d\n%s", code, out) + } + if strings.Contains(out, "too many rows") { + t.Errorf("delivered-page-under-cap should not emit cap error:\n%s", out) + } + + outJSON, codeJSON := bdRunRaw(t, bd, mDir, nil, "list", "--include-infra", "--json", + "--limit", "2", "--max-rows", "5") + if codeJSON != 0 { + t.Fatalf("expected exit 0 under --json (delivered page under cap despite pre-trim merge overage), got %d\n%s", codeJSON, outJSON) + } + if strings.Contains(outJSON, "too many rows") { + t.Errorf("delivered-page-under-cap under --json should not emit cap error:\n%s", outJSON) + } + }) + + // be-x42v.4 round-5 follow-up: searchInTx's merge branch concatenates + // two independently ORDER BY'd legs (`append(filtered, wispResults...)`) + // and round-4's trimToSearchLimit then sliced that unsorted + // concatenation — with Limit small enough that durable rows alone fill + // it, a higher-ranked wisp was silently dropped from the page even + // though the CLI's pre-round-4 behavior (return the full merged set, + // let the caller sort-then-trim) surfaced it correctly. Default sort is + // priority ASC: 3 durable issues at P2, one wisp at P0 (highest rank) + // — with --limit 1 the P0 wisp must be the sole delivered row, not one + // of the P2 durable rows the unsorted concatenation happened to put + // first. Must fail against 77227aa67. + t.Run("IncludeInfraMerged_WispOutranksDurable_InDeliveredPage", func(t *testing.T) { + mDir, _, _ := bdInit(t, bd, "--prefix", "mrwo") + for i := 0; i < 3; i++ { + bdCreate(t, bd, mDir, fmt.Sprintf("Durable p2 %d", i), "--type", "task", "--priority", "2") + } + wisp := bdCreate(t, bd, mDir, "Wisp p0 outranks", "--type", "task", "--priority", "0", "--ephemeral") + + out, code := bdRunRaw(t, bd, mDir, nil, "list", "--include-infra", "--limit", "1") + if code != 0 { + t.Fatalf("expected exit 0, got %d\n%s", code, out) + } + if !strings.Contains(out, wisp.ID) { + t.Errorf("P0 wisp %s must be the sole row in a --limit 1 page (default sort is priority ASC), got:\n%s", wisp.ID, out) + } + + outJSON, codeJSON := bdRunRaw(t, bd, mDir, nil, "list", "--include-infra", "--json", "--limit", "1") + if codeJSON != 0 { + t.Fatalf("expected exit 0 under --json, got %d\n%s", codeJSON, outJSON) + } + if !strings.Contains(outJSON, wisp.ID) { + t.Errorf("P0 wisp %s must be the sole row in a --limit 1 --json page, got:\n%s", wisp.ID, outJSON) + } + }) + + // be-x42v.4 follow-up (review MUST-FIX 5): the initial query under + // `bd list --watch` must surface a cap violation with exit 2, same as + // non-watch `bd list`. Before the fix, watchIssues logged the error to + // stderr itself and returned void, so the process always exited 0 — + // the failure never reached RunE/handleMaxRowsError. This subprocess + // exercises only the initial-query failure path (returns before + // entering the polling loop), so it terminates without needing Ctrl+C. + t.Run("Watch_FlagOverCap_Exits2", func(t *testing.T) { + out, code := bdRunRaw(t, bd, dir, nil, "list", "--watch", "--max-rows", "5") + if code != 2 { + t.Fatalf("expected exit 2 (cap exceeded under --watch), got %d\n%s", code, out) + } + if !strings.Contains(out, "too many rows") { + t.Errorf("expected 'too many rows' in stderr:\n%s", out) + } + if !strings.Contains(out, "--max-rows=5") { + t.Errorf("expected source `--max-rows=5` in stderr:\n%s", out) + } + }) +} diff --git a/cmd/bd/max_rows_test.go b/cmd/bd/max_rows_test.go index e8066a3e1..3f788e250 100644 --- a/cmd/bd/max_rows_test.go +++ b/cmd/bd/max_rows_test.go @@ -1,719 +1,18 @@ //go:build cgo -// be-u8z9: CLI-layer behavioral tests for BEADS_MAX_ROWS / --max-rows on the -// command paths beyond `bd list`. Each subtest: -// - inits a fresh rig (own DB so the row counts are exact) -// - exercises the command via exec.Command(bd, ...) -// - asserts the process exited with code 2 (cap exceeded) and that stderr -// names the source ("--max-rows=N" or "BEADS_MAX_ROWS=N"). -// -// The doctor-family commands (lint, doctor-conventions, doctor-pollution) -// are env-only by design (designer §4); they do NOT register --max-rows -// as a flag. A separate subtest asserts cobra rejects the flag. -// -// Gating matches the other embedded-dolt CLI tests: requires -// BEADS_TEST_EMBEDDED_DOLT=1 to opt in. +// be-u8z9: unit tests for workapi.WithFetchOneExtra, the probe-row bump +// helper used by the CLI-layer --max-rows / BEADS_MAX_ROWS enforcement +// exercised end-to-end in max_rows_embedded_test.go. package main import ( - "fmt" - "os" - "os/exec" - "strings" "testing" "github.com/steveyegge/beads/internal/types" "github.com/steveyegge/beads/internal/workapi" ) -// bdRunRaw runs bd with the given args and env extras (appended to bdEnv(dir)). -// Returns combined stdout+stderr and the process exit code. Unlike the other -// helpers in this package it does not call t.Fatal on non-zero exits — that's -// the success case for the max-rows tests. -func bdRunRaw(t *testing.T, bd, dir string, envExtras []string, args ...string) (string, int) { - t.Helper() - cmd := exec.Command(bd, args...) - cmd.Dir = dir - env := bdEnv(dir) - env = append(env, envExtras...) - cmd.Env = env - out, err := cmd.CombinedOutput() - if err == nil { - return string(out), 0 - } - if exitErr, ok := err.(*exec.ExitError); ok { - return string(out), exitErr.ExitCode() - } - t.Fatalf("bd %s unexpected non-exit error: %v\n%s", strings.Join(args, " "), err, out) - return string(out), -1 -} - -// seedReadyIssues creates n top-level (no-dep, no-blocker) issues that all -// appear in `bd ready`. Useful for tests that need exactly n ready rows. -func seedReadyIssues(t *testing.T, bd, dir string, n int) []string { - t.Helper() - ids := make([]string, n) - for i := 0; i < n; i++ { - iss := bdCreate(t, bd, dir, fmt.Sprintf("max-rows seed %d", i), "--type", "task") - ids[i] = iss.ID - } - return ids -} - -// TestEmbeddedMaxRowsNonListPaths covers the non-list CLI paths wired up in -// be-x42v.2: ready, dep tree, find-duplicates, graph --all, plus the env-only -// doctor family (lint, doctor --check=conventions, doctor --check=pollution), -// and config show emission of BEADS_MAX_ROWS. -func TestEmbeddedMaxRowsNonListPaths(t *testing.T) { - if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" { - t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt integration tests") - } - t.Parallel() - - bd := buildEmbeddedBD(t) - - // ----------- bd ready ----------- - - t.Run("ReadyMaxRows_FlagOverCap_Exits2", func(t *testing.T) { - dir, _, _ := bdInit(t, bd, "--prefix", "mrrdf") - seedReadyIssues(t, bd, dir, 6) - - out, code := bdRunRaw(t, bd, dir, nil, "ready", "--max-rows", "3") - if code != 2 { - t.Fatalf("expected exit 2 (cap exceeded), got %d\n%s", code, out) - } - if !strings.Contains(out, "too many rows") { - t.Errorf("stderr missing 'too many rows':\n%s", out) - } - if !strings.Contains(out, "--max-rows=3") { - t.Errorf("stderr missing source --max-rows=3:\n%s", out) - } - }) - - t.Run("ReadyMaxRows_EnvOverCap_Exits2", func(t *testing.T) { - dir, _, _ := bdInit(t, bd, "--prefix", "mrrde") - seedReadyIssues(t, bd, dir, 6) - - out, code := bdRunRaw(t, bd, dir, []string{"BEADS_MAX_ROWS=3"}, "ready") - if code != 2 { - t.Fatalf("expected exit 2 (cap exceeded), got %d\n%s", code, out) - } - if !strings.Contains(out, "BEADS_MAX_ROWS=3") { - t.Errorf("stderr missing source BEADS_MAX_ROWS=3:\n%s", out) - } - }) - - // `bd ready --json` and `bd list --ready --json` route through - // GetReadyWorkWithCountsInTx, a separate query path from the plain - // `bd ready` above (GetReadyWorkInTx). Mirrors - // TestEmbeddedMaxRowsList/Flag_OverCap's --json coverage for `bd list`. - t.Run("ReadyMaxRowsJSON_FlagOverCap_Exits2", func(t *testing.T) { - dir, _, _ := bdInit(t, bd, "--prefix", "mrrjf") - seedReadyIssues(t, bd, dir, 6) - - out, code := bdRunRaw(t, bd, dir, nil, "ready", "--json", "--max-rows", "3") - if code != 2 { - t.Fatalf("expected exit 2 (cap exceeded), got %d\n%s", code, out) - } - if !strings.Contains(out, "too many rows") { - t.Errorf("stderr missing 'too many rows':\n%s", out) - } - if !strings.Contains(out, "--max-rows=3") { - t.Errorf("stderr missing source --max-rows=3:\n%s", out) - } - if idx := strings.Index(out, "Error: too many rows"); idx >= 0 { - if strings.Contains(out[:idx], "[") { - t.Errorf("stdout JSON output leaked before error message:\nfull=%s", out) - } - } - }) - - t.Run("ListReadyMaxRowsJSON_FlagOverCap_Exits2", func(t *testing.T) { - dir, _, _ := bdInit(t, bd, "--prefix", "mrrjl") - seedReadyIssues(t, bd, dir, 6) - - out, code := bdRunRaw(t, bd, dir, nil, "list", "--ready", "--json", "--max-rows", "3") - if code != 2 { - t.Fatalf("expected exit 2 (cap exceeded), got %d\n%s", code, out) - } - if !strings.Contains(out, "too many rows") { - t.Errorf("stderr missing 'too many rows':\n%s", out) - } - if !strings.Contains(out, "--max-rows=3") { - t.Errorf("stderr missing source --max-rows=3:\n%s", out) - } - }) - - // be-x42v.4 follow-up (review SHOULD-FIX 8): confirms the cap still - // fires end-to-end for --include-ephemeral after propagating MaxRows - // into getReadyWispsInTx's wisp query (internal/storage/issueops/ - // ready_work.go) — that change bounds the query itself but must not - // change whether the post-merge cap check in GetReadyWorkInTx trips. - t.Run("ReadyIncludeEphemeralMaxRows_FlagOverCap_Exits2", func(t *testing.T) { - dir, _, _ := bdInit(t, bd, "--prefix", "mrre") - for i := 0; i < 6; i++ { - bdCreate(t, bd, dir, fmt.Sprintf("Ephemeral ready %d", i), "--type", "task", "--ephemeral") - } - - out, code := bdRunRaw(t, bd, dir, nil, "ready", "--include-ephemeral", "--max-rows", "3") - if code != 2 { - t.Fatalf("expected exit 2 (cap exceeded), got %d\n%s", code, out) - } - if !strings.Contains(out, "too many rows") { - t.Errorf("stderr missing 'too many rows':\n%s", out) - } - if !strings.Contains(out, "--max-rows=3") { - t.Errorf("stderr missing source --max-rows=3:\n%s", out) - } - }) - - // be-x42v.4 round-3 follow-up: GetReadyWorkWithCountsInTx (the `--json` - // path) independently bounds each of the issues and wisps queries by - // EffectiveSearchLimit(Limit, MaxRows), so with --include-ephemeral the - // PRE-TRIM merge of two tables can hold up to ~2x that per-table bound - // even though the caller's own --limit already keeps the DELIVERED page - // well within the cap. limit=2, cap=3, 2 ready rows in each table: each - // table's query returns 2 (2<=limit, no overage sniff at the per-table - // level — EffectiveSearchLimit(2,3)=2), the pre-trim merge is 4>cap=3, - // but the page actually handed back to the caller is trimmed to 2<=cap. - // Must exit 0, not trip the cap on the untrimmed merge. - t.Run("ReadyIncludeEphemeralMaxRowsJSON_MergedUnderLimit_NoError", func(t *testing.T) { - dir, _, _ := bdInit(t, bd, "--prefix", "mrrm") - for i := 0; i < 2; i++ { - bdCreate(t, bd, dir, fmt.Sprintf("Plain ready %d", i), "--type", "task") - } - for i := 0; i < 2; i++ { - bdCreate(t, bd, dir, fmt.Sprintf("Ephemeral ready %d", i), "--type", "task", "--ephemeral") - } - - out, code := bdRunRaw(t, bd, dir, nil, "ready", "--json", "--include-ephemeral", - "--limit", "2", "--max-rows", "3") - if code != 0 { - t.Fatalf("expected exit 0 (delivered page under cap despite pre-trim merge overage), got %d\n%s", code, out) - } - if strings.Contains(out, "too many rows") { - t.Errorf("delivered-page-under-cap should not emit cap error:\n%s", out) - } - }) - - // ----------- bd dep tree ----------- - - t.Run("DepTreeMaxRows_TreeNodes_Exits2", func(t *testing.T) { - dir, _, _ := bdInit(t, bd, "--prefix", "mrdt") - // All tasks: default "blocks" dep type rejects epic→task, but task→task - // is fine and produces a tree with the same shape for cap purposes. - root := bdCreate(t, bd, dir, "Tree root", "--type", "task") - // Add 5 children so the dep tree has 6 nodes total (root + 5). - for i := 0; i < 5; i++ { - child := bdCreate(t, bd, dir, fmt.Sprintf("Tree dep %d", i), "--type", "task") - // `bd dep add A B` makes A depend on B (default dep type is "blocks"). - bdDepAdd(t, bd, dir, root.ID, child.ID) - } - - // Sanity: tree of size 6, cap of 2 → must exit 2 with source attribution. - out, code := bdRunRaw(t, bd, dir, nil, "dep", "tree", root.ID, "--max-rows", "2") - if code != 2 { - t.Fatalf("expected exit 2 (tree size > cap), got %d\n%s", code, out) - } - if !strings.Contains(out, "too many rows") { - t.Errorf("stderr missing 'too many rows':\n%s", out) - } - if !strings.Contains(out, "--max-rows=2") { - t.Errorf("stderr missing source --max-rows=2:\n%s", out) - } - }) - - // ----------- bd find-duplicates ----------- - - t.Run("FindDuplicatesMaxRows_Exits2", func(t *testing.T) { - dir, _, _ := bdInit(t, bd, "--prefix", "mrfd") - seedReadyIssues(t, bd, dir, 6) - - out, code := bdRunRaw(t, bd, dir, nil, "find-duplicates", "--max-rows", "3") - if code != 2 { - t.Fatalf("expected exit 2 (cap exceeded), got %d\n%s", code, out) - } - if !strings.Contains(out, "--max-rows=3") { - t.Errorf("stderr missing source --max-rows=3:\n%s", out) - } - }) - - // ----------- bd graph --all ----------- - - t.Run("GraphAllMaxRows_Exits2", func(t *testing.T) { - dir, _, _ := bdInit(t, bd, "--prefix", "mrgr") - seedReadyIssues(t, bd, dir, 6) - - out, code := bdRunRaw(t, bd, dir, nil, "graph", "--all", "--max-rows", "3") - if code != 2 { - t.Fatalf("expected exit 2 (cap exceeded), got %d\n%s", code, out) - } - if !strings.Contains(out, "--max-rows=3") { - t.Errorf("stderr missing source --max-rows=3:\n%s", out) - } - }) - - // be-x42v.4 follow-up (review SHOULD-FIX 7): `bd graph ` without - // --all never resolved the cap at all before the fix — loadGraphSubgraph - // is a BFS over per-ID GetDependents/GetDependencies lookups with no - // IssueFilter to thread MaxRows through, so this checks the final - // connected-component node count post-hoc, mirroring dep tree. - t.Run("GraphSingleIssueMaxRows_NodeCount_Exits2", func(t *testing.T) { - dir, _, _ := bdInit(t, bd, "--prefix", "mrgs") - root := bdCreate(t, bd, dir, "Graph root", "--type", "task") - for i := 0; i < 5; i++ { - child := bdCreate(t, bd, dir, fmt.Sprintf("Graph dep %d", i), "--type", "task") - bdDepAdd(t, bd, dir, root.ID, child.ID) - } - - // Connected component of size 6 (root + 5), cap of 2 → exit 2. - out, code := bdRunRaw(t, bd, dir, nil, "graph", root.ID, "--max-rows", "2") - if code != 2 { - t.Fatalf("expected exit 2 (subgraph size > cap), got %d\n%s", code, out) - } - if !strings.Contains(out, "too many rows") { - t.Errorf("stderr missing 'too many rows':\n%s", out) - } - if !strings.Contains(out, "--max-rows=2") { - t.Errorf("stderr missing source --max-rows=2:\n%s", out) - } - }) - - t.Run("GraphSingleIssueMaxRows_UnderCap_NoError", func(t *testing.T) { - dir, _, _ := bdInit(t, bd, "--prefix", "mrgu") - root := bdCreate(t, bd, dir, "Graph root under cap", "--type", "task") - - out, code := bdRunRaw(t, bd, dir, nil, "graph", root.ID, "--max-rows", "5") - if code != 0 { - t.Fatalf("expected exit 0 (single-node subgraph under cap), got %d\n%s", code, out) - } - if strings.Contains(out, "too many rows") { - t.Errorf("under-cap single-issue graph should not emit cap error:\n%s", out) - } - }) - - // ----------- bd lint (env-only) ----------- - - t.Run("LintMaxRows_EnvOnly_Exits2", func(t *testing.T) { - dir, _, _ := bdInit(t, bd, "--prefix", "mrln") - seedReadyIssues(t, bd, dir, 6) - - out, code := bdRunRaw(t, bd, dir, []string{"BEADS_MAX_ROWS=3"}, "lint") - if code != 2 { - t.Fatalf("expected exit 2 (cap exceeded via env), got %d\n%s", code, out) - } - if !strings.Contains(out, "BEADS_MAX_ROWS=3") { - t.Errorf("stderr missing source BEADS_MAX_ROWS=3:\n%s", out) - } - }) - - t.Run("LintMaxRows_NoFlagAccepted", func(t *testing.T) { - // Designer §4: doctor family is env-opt-in only. The --max-rows - // flag must NOT be registered on `bd lint`; cobra should reject - // it as unknown. - dir, _, _ := bdInit(t, bd, "--prefix", "mrlnf") - - out, code := bdRunRaw(t, bd, dir, nil, "lint", "--max-rows", "1") - if code == 0 { - t.Fatalf("expected non-zero exit (cobra rejects unknown flag), got 0\n%s", out) - } - if !strings.Contains(out, "unknown flag") { - t.Errorf("stderr missing 'unknown flag' rejection:\n%s", out) - } - // Defense against false positives: if `--max-rows` is wired on lint - // by mistake, this assertion would not trip — but the unknown-flag - // check above already covers it. - }) - - // ----------- bd doctor --check=conventions (env-only) ----------- - // - // bd doctor is hard-gated to server mode (doctor.go:188 prints "not yet - // supported in embedded mode" and exits 0 before reaching the check - // dispatch). The embedded-Dolt test rig used here cannot exercise those - // code paths. The cap logic itself runs through the shared SearchIssues + - // EnforceMaxRowsCap path already covered by: - // - TestEnforceMaxRowsCap_* (internal/storage/issueops/search_test.go) - // - TestGetReadyWork_MaxRowsSuite (cmd/bd/ready_max_rows_test.go) - // - be-x42v.3 storage parity tests (bd list/search backend matrix) - // What's _not_ covered without server mode is the env-only resolver - // (resolveMaxRowsEnvOnly) on these specific commands. doctor_conventions.go - // and doctor_pollution.go both call it and pipe through handleMaxRowsError - // identically to lint.go, which IS covered above by LintMaxRows_EnvOnly_Exits2. - // A server-mode follow-up should add behavioral coverage here. - - t.Run("ConventionsMaxRows_EnvOnly_Exits2", func(t *testing.T) { - t.Skip("bd doctor requires server mode (doctor.go:188); server-mode parity covered by separate validator bead") - }) - - t.Run("PollutionMaxRows_EnvOnly_Exits2", func(t *testing.T) { - t.Skip("bd doctor requires server mode (doctor.go:188); server-mode parity covered by separate validator bead") - }) - - // ----------- bd config show ----------- - - t.Run("ConfigShow_ListsBeadsMaxRows", func(t *testing.T) { - dir, _, _ := bdInit(t, bd, "--prefix", "mrcs") - - // When BEADS_MAX_ROWS is set in the environment, `bd config show` - // must surface it in the standalone-env entries (config_show.go - // collectStandaloneEnvEntries). - out, code := bdRunRaw(t, bd, dir, []string{"BEADS_MAX_ROWS=42"}, "config", "show") - if code != 0 { - t.Fatalf("expected exit 0 from `bd config show`, got %d\n%s", code, out) - } - if !strings.Contains(out, "BEADS_MAX_ROWS") { - t.Errorf("expected `bd config show` to list BEADS_MAX_ROWS in output:\n%s", out) - } - if !strings.Contains(out, "42") { - t.Errorf("expected `bd config show` to display the value 42:\n%s", out) - } - - // When the env var is unset, the entry must be absent (designer §4: - // opt-in only; default-disabled). - out2, code2 := bdRunRaw(t, bd, dir, nil, "config", "show") - if code2 != 0 { - t.Fatalf("expected exit 0 from `bd config show` (unset), got %d\n%s", code2, out2) - } - if strings.Contains(out2, "BEADS_MAX_ROWS") { - t.Errorf("expected BEADS_MAX_ROWS to be absent when env is unset:\n%s", out2) - } - }) -} - -// countListIDs returns the number of issue IDs of the form "-" -// observed in the combined `bd list` output. Used to verify that --limit -// actually clipped the result set without depending on JSON parsing of the -// pretty-printed array (which spans many lines). -func countListIDs(out, prefix string) int { - // Each list row prints the issue ID once; counting prefix occurrences in - // raw stdout is a robust enough check for our small fixtures. - return strings.Count(out, prefix+"-") -} - -// TestEmbeddedMaxRowsList covers the 10 designer §6.1 behavioral scenarios -// for `bd list`. Builder bead: be-x42v.2 (CLI wiring). All subtests share a -// single rig of 21 open task issues — the dataset size from the designer's -// fixture. -func TestEmbeddedMaxRowsList(t *testing.T) { - if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" { - t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt integration tests") - } - t.Parallel() - - bd := buildEmbeddedBD(t) - dir, _, _ := bdInit(t, bd, "--prefix", "mrl") - - const totalRows = 21 - for i := 0; i < totalRows; i++ { - bdCreate(t, bd, dir, fmt.Sprintf("List max-rows %d", i), "--type", "task") - } - - t.Run("Disabled_NoEnv", func(t *testing.T) { - // Baseline: with no flag and no env var, `bd list` returns all rows. - out, code := bdRunRaw(t, bd, dir, nil, "list") - if code != 0 { - t.Fatalf("expected exit 0 (no cap), got %d\n%s", code, out) - } - if strings.Contains(out, "too many rows") { - t.Errorf("baseline `bd list` should not emit cap error:\n%s", out) - } - if got := countListIDs(out, "mrl"); got < totalRows { - t.Errorf("expected at least %d rows in output, got %d", totalRows, got) - } - }) - - t.Run("Flag_UnderCap", func(t *testing.T) { - // 21 rows under cap=100: success. - out, code := bdRunRaw(t, bd, dir, nil, "list", "--max-rows", "100") - if code != 0 { - t.Fatalf("expected exit 0 (under cap), got %d\n%s", code, out) - } - if strings.Contains(out, "too many rows") { - t.Errorf("under-cap `bd list` should not emit cap error:\n%s", out) - } - }) - - t.Run("Flag_OverCap", func(t *testing.T) { - // 21 rows over cap=5: exit 2, stderr includes --max-rows=5, stdout - // empty (designer §2.3: stdout is suppressed so `jq` downstream - // doesn't trip on partial JSON). - out, code := bdRunRaw(t, bd, dir, nil, "list", "--json", "--max-rows", "5") - if code != 2 { - t.Fatalf("expected exit 2 (cap exceeded), got %d\n%s", code, out) - } - if !strings.Contains(out, "too many rows") { - t.Errorf("expected 'too many rows' in stderr:\n%s", out) - } - if !strings.Contains(out, "--max-rows=5") { - t.Errorf("expected source `--max-rows=5` in stderr:\n%s", out) - } - // stdout-emptiness check: there should be no JSON `[` array opener - // in the merged output before the error. Cap-exceeded must not emit - // a partial array. - if idx := strings.Index(out, "Error: too many rows"); idx >= 0 { - before := out[:idx] - if strings.Contains(before, "[") { - t.Errorf("stdout JSON output leaked before error message:\nbefore=%q\nfull=%s", before, out) - } - } - }) - - t.Run("Env_OverCap", func(t *testing.T) { - out, code := bdRunRaw(t, bd, dir, []string{"BEADS_MAX_ROWS=5"}, "list") - if code != 2 { - t.Fatalf("expected exit 2 (cap exceeded via env), got %d\n%s", code, out) - } - if !strings.Contains(out, "BEADS_MAX_ROWS=5") { - t.Errorf("expected source `BEADS_MAX_ROWS=5` in stderr:\n%s", out) - } - }) - - t.Run("Flag_OverridesEnv", func(t *testing.T) { - // env says cap=5, flag says cap=100 — flag wins, expect success. - out, code := bdRunRaw(t, bd, dir, []string{"BEADS_MAX_ROWS=5"}, - "list", "--max-rows", "100") - if code != 0 { - t.Fatalf("expected exit 0 (flag overrides env), got %d\n%s", code, out) - } - if strings.Contains(out, "too many rows") { - t.Errorf("flag-override should not emit cap error:\n%s", out) - } - }) - - t.Run("Flag_Zero_OverridesEnv", func(t *testing.T) { - // Explicit `--max-rows 0` disables the cap even when env is set - // (designer §2.1: ops shells with a global env can run a known- - // unbounded query without unsetting). - out, code := bdRunRaw(t, bd, dir, []string{"BEADS_MAX_ROWS=5"}, - "list", "--max-rows", "0") - if code != 0 { - t.Fatalf("expected exit 0 (--max-rows 0 disables), got %d\n%s", code, out) - } - if strings.Contains(out, "too many rows") { - t.Errorf("explicit `--max-rows 0` should disable the cap:\n%s", out) - } - }) - - t.Run("BadEnv_LogsAndIgnores", func(t *testing.T) { - // env=banana → warning to stderr, normal output, exit 0 - // (designer §2.1: fail-open on bad env so a typo in a global shell - // doesn't break automation). - out, code := bdRunRaw(t, bd, dir, []string{"BEADS_MAX_ROWS=banana"}, "list") - if code != 0 { - t.Fatalf("expected exit 0 (bad env ignored), got %d\n%s", code, out) - } - // max_rows.go:79 emits a "Warning: BEADS_MAX_ROWS=\"banana\" is not - // a non-negative integer; ignoring." line. - if !strings.Contains(out, "Warning") || !strings.Contains(out, "BEADS_MAX_ROWS") { - t.Errorf("expected warning about bad env in stderr:\n%s", out) - } - if strings.Contains(out, "too many rows") { - t.Errorf("bad env should not trigger cap error:\n%s", out) - } - }) - - t.Run("Negative_FlagRejected", func(t *testing.T) { - // max_rows.go:55 calls FatalError on negative — exit 1 with message. - out, code := bdRunRaw(t, bd, dir, nil, "list", "--max-rows", "-1") - if code != 1 { - t.Fatalf("expected exit 1 (usage error), got %d\n%s", code, out) - } - if !strings.Contains(out, "must be non-negative") { - t.Errorf("expected 'must be non-negative' in stderr:\n%s", out) - } - }) - - t.Run("LimitSet_CapTighter", func(t *testing.T) { - // limit=100, cap=5: 21 rows are scanned (LIMIT cap+1=6 sniffs - // overage), cap fires, exit 2. The --limit flag does not protect - // callers who set a generous limit but also opt into the cap. - out, code := bdRunRaw(t, bd, dir, nil, "list", - "--limit", "100", "--max-rows", "5") - if code != 2 { - t.Fatalf("expected exit 2 (cap tighter than limit), got %d\n%s", code, out) - } - if !strings.Contains(out, "--max-rows=5") { - t.Errorf("expected source `--max-rows=5` in stderr:\n%s", out) - } - }) - - t.Run("LimitSet_CapLooser", func(t *testing.T) { - // limit=5, cap=100: EffectiveSearchLimit returns 5 (limit wins when - // under cap), no overage detection, 5 rows returned, exit 0. - out, code := bdRunRaw(t, bd, dir, nil, "list", - "--limit", "5", "--max-rows", "100") - if code != 0 { - t.Fatalf("expected exit 0 (limit under cap), got %d\n%s", code, out) - } - if strings.Contains(out, "too many rows") { - t.Errorf("limit-under-cap should not emit cap error:\n%s", out) - } - if got := countListIDs(out, "mrl"); got != 5 { - t.Errorf("expected exactly 5 issue IDs in output, got %d:\n%s", got, out) - } - }) - - // be-x42v.4 follow-up (review MUST-FIX 4, revised round-3): --limit N - // --max-rows N with more than N matches must truncate to N and exit 0 - // (the standard >N-matches truncation semantics), not error. - // - // Round-2 fix (regressed, since replaced): unconditionally skipped the - // workapi.WithFetchOneExtra probe-row bump when Limit>=MaxRows. That avoided - // the false cap error but also stopped fetching the extra row entirely, - // so len(results) could never exceed effectiveLimit and the GH#3212 - // truncation notice silently stopped firing for this case — a partial - // result started presenting as complete. - // - // Round-3 fix: still fetch the probe row (Limit bumps to N+1 as - // normal), but when Limit==MaxRows exactly, bump MaxRows to N+1 too, in - // lockstep, so EnforceMaxRowsCap's comparison excludes the probe row - // from the cap check while the CLI's own len(results)>effectiveLimit - // truncation detection still sees it. See workapi.WithFetchOneExtra's doc - // comment in list.go for why this can't false-negative a real - // violation. Covered directly (bump values, not exit behavior) by - // TestWithFetchOneExtra_LimitEqualsCap_BumpsBothForTruncationProbe - // below, since printTruncationHint's text is gated on - // ui.IsStderrTerminal() and unconditionally suppressed for this - // subprocess harness's piped stderr (see list_embedded_test.go's - // limit_truncation_hint subtest) — these end-to-end subtests can only - // assert the absence of the false cap error and the delivered count. - t.Run("LimitEqualsCap_TruncatesNotErrors", func(t *testing.T) { - out, code := bdRunRaw(t, bd, dir, nil, "list", - "--limit", "5", "--max-rows", "5") - if code != 0 { - t.Fatalf("expected exit 0 (limit==cap truncates, does not error), got %d\n%s", code, out) - } - if strings.Contains(out, "too many rows") { - t.Errorf("limit==cap should not emit cap error:\n%s", out) - } - if got := countListIDs(out, "mrl"); got != 5 { - t.Errorf("expected exactly 5 issue IDs in output, got %d:\n%s", got, out) - } - }) - - t.Run("LimitEqualsCap_JSON_TruncatesNotErrors", func(t *testing.T) { - out, code := bdRunRaw(t, bd, dir, nil, "list", "--json", - "--limit", "5", "--max-rows", "5") - if code != 0 { - t.Fatalf("expected exit 0 (limit==cap truncates under --json), got %d\n%s", code, out) - } - if strings.Contains(out, "too many rows") { - t.Errorf("limit==cap under --json should not emit cap error:\n%s", out) - } - }) - - // be-x42v.4 round-4 follow-up: the round-3 fix to - // finishReadyWorkWithCounts (trim to Limit before EnforceMaxRowsCap) - // wasn't mirrored onto the sibling bd list search paths - // (searchInTx in search.go, finishSearchIssuesWithCounts in - // search_counts.go), which still capped the pre-trim merged - // issues+wisps set. `--include-infra` disables the default SkipWisps - // escape hatch, so the wisps merge actually runs. limit=2, cap=5, 3 - // durable + 3 wisp issues: each leg's query is independently bounded - // by EffectiveSearchLimit, so the merge can produce up to 6 rows - // pre-trim (>cap=5) even though the delivered page (trimmed to 2) is - // well within the cap. Covers both bd list output modes (opus's live - // repro found both non-JSON and --json tripping the false cap error). - t.Run("IncludeInfraMergedUnderLimit_NoError", func(t *testing.T) { - mDir, _, _ := bdInit(t, bd, "--prefix", "mrii") - for i := 0; i < 3; i++ { - bdCreate(t, bd, mDir, fmt.Sprintf("Durable infra %d", i), "--type", "task") - } - for i := 0; i < 3; i++ { - bdCreate(t, bd, mDir, fmt.Sprintf("Wisp infra %d", i), "--type", "task", "--ephemeral") - } - - out, code := bdRunRaw(t, bd, mDir, nil, "list", "--include-infra", - "--limit", "2", "--max-rows", "5") - if code != 0 { - t.Fatalf("expected exit 0 (delivered page under cap despite pre-trim merge overage), got %d\n%s", code, out) - } - if strings.Contains(out, "too many rows") { - t.Errorf("delivered-page-under-cap should not emit cap error:\n%s", out) - } - - outJSON, codeJSON := bdRunRaw(t, bd, mDir, nil, "list", "--include-infra", "--json", - "--limit", "2", "--max-rows", "5") - if codeJSON != 0 { - t.Fatalf("expected exit 0 under --json (delivered page under cap despite pre-trim merge overage), got %d\n%s", codeJSON, outJSON) - } - if strings.Contains(outJSON, "too many rows") { - t.Errorf("delivered-page-under-cap under --json should not emit cap error:\n%s", outJSON) - } - }) - - // be-x42v.4 round-5 follow-up: searchInTx's merge branch concatenates - // two independently ORDER BY'd legs (`append(filtered, wispResults...)`) - // and round-4's trimToSearchLimit then sliced that unsorted - // concatenation — with Limit small enough that durable rows alone fill - // it, a higher-ranked wisp was silently dropped from the page even - // though the CLI's pre-round-4 behavior (return the full merged set, - // let the caller sort-then-trim) surfaced it correctly. Default sort is - // priority ASC: 3 durable issues at P2, one wisp at P0 (highest rank) - // — with --limit 1 the P0 wisp must be the sole delivered row, not one - // of the P2 durable rows the unsorted concatenation happened to put - // first. Must fail against 77227aa67. - t.Run("IncludeInfraMerged_WispOutranksDurable_InDeliveredPage", func(t *testing.T) { - mDir, _, _ := bdInit(t, bd, "--prefix", "mrwo") - for i := 0; i < 3; i++ { - bdCreate(t, bd, mDir, fmt.Sprintf("Durable p2 %d", i), "--type", "task", "--priority", "2") - } - wisp := bdCreate(t, bd, mDir, "Wisp p0 outranks", "--type", "task", "--priority", "0", "--ephemeral") - - out, code := bdRunRaw(t, bd, mDir, nil, "list", "--include-infra", "--limit", "1") - if code != 0 { - t.Fatalf("expected exit 0, got %d\n%s", code, out) - } - if !strings.Contains(out, wisp.ID) { - t.Errorf("P0 wisp %s must be the sole row in a --limit 1 page (default sort is priority ASC), got:\n%s", wisp.ID, out) - } - - outJSON, codeJSON := bdRunRaw(t, bd, mDir, nil, "list", "--include-infra", "--json", "--limit", "1") - if codeJSON != 0 { - t.Fatalf("expected exit 0 under --json, got %d\n%s", codeJSON, outJSON) - } - if !strings.Contains(outJSON, wisp.ID) { - t.Errorf("P0 wisp %s must be the sole row in a --limit 1 --json page, got:\n%s", wisp.ID, outJSON) - } - }) - - // be-x42v.4 follow-up (review MUST-FIX 5): the initial query under - // `bd list --watch` must surface a cap violation with exit 2, same as - // non-watch `bd list`. Before the fix, watchIssues logged the error to - // stderr itself and returned void, so the process always exited 0 — - // the failure never reached RunE/handleMaxRowsError. This subprocess - // exercises only the initial-query failure path (returns before - // entering the polling loop), so it terminates without needing Ctrl+C. - t.Run("Watch_FlagOverCap_Exits2", func(t *testing.T) { - out, code := bdRunRaw(t, bd, dir, nil, "list", "--watch", "--max-rows", "5") - if code != 2 { - t.Fatalf("expected exit 2 (cap exceeded under --watch), got %d\n%s", code, out) - } - if !strings.Contains(out, "too many rows") { - t.Errorf("expected 'too many rows' in stderr:\n%s", out) - } - if !strings.Contains(out, "--max-rows=5") { - t.Errorf("expected source `--max-rows=5` in stderr:\n%s", out) - } - }) -} - -// TestWithFetchOneExtra_LimitEqualsCap_BumpsBothForTruncationProbe verifies -// the exact bump mechanism workapi.WithFetchOneExtra uses to reconcile the -// >N-matches truncation probe (GH#3212) with a MaxRows cap equal to the -// user's --limit (be-x42v.4 round-3 follow-up). -// -// The end-to-end LimitEqualsCap_TruncatesNotErrors subprocess test in -// TestEmbeddedMaxRowsList can only assert the *absence* of the false cap -// error and the trimmed row count — it can't observe the truncation -// *notice* text, which printTruncationHint gates on ui.IsStderrTerminal() -// and is unconditionally suppressed for a subprocess's piped stderr (see -// list_embedded_test.go's limit_truncation_hint subtest, which documents -// the same TTY constraint). This unit test instead asserts the underlying -// signal directly: with Limit==MaxRows, both must bump by one so the query -// still over-fetches by one row (restoring len(results) > effectiveLimit -// detection) while EnforceMaxRowsCap doesn't trip on that extra row. func TestWithFetchOneExtra_LimitEqualsCap_BumpsBothForTruncationProbe(t *testing.T) { got := workapi.WithFetchOneExtra(types.IssueFilter{Limit: 5, MaxRows: 5, MaxRowsSource: "--max-rows"}) if got.Limit != 6 { diff --git a/cmd/bd/prime.go b/cmd/bd/prime.go index e7bc1a4c1..adec80396 100644 --- a/cmd/bd/prime.go +++ b/cmd/bd/prime.go @@ -299,15 +299,14 @@ func primeWorkspaceDir() string { // NOTE: the probes built here are not prime-only — see primeHasGitRemote for // the auto-backup consumer that inherits this directory choice. // -// NOTE: since GH#4927 every return path pairs with a nil error — the -// GetRepoContext() failure falls back to a cwd probe instead of propagating — -// so the err != nil arms at both call sites are unreachable today. The error -// result is retained for future callers that can genuinely fail. -func primeGitCmd(ctx context.Context, args ...string) (*exec.Cmd, error) { +// NOTE: since GH#4927 every return path is infallible — the GetRepoContext() +// failure falls back to a cwd probe instead of propagating — so this no +// longer returns an error. If a future caller needs one, reintroduce it then. +func primeGitCmd(ctx context.Context, args ...string) *exec.Cmd { if ws := primeWorkspaceDir(); ws != "" { cmd := exec.CommandContext(ctx, "git", args...) cmd.Dir = ws - return cmd, nil + return cmd } rc, err := internalbeads.GetRepoContext() if err != nil { @@ -318,9 +317,9 @@ func primeGitCmd(ctx context.Context, args ...string) (*exec.Cmd, error) { // workspace, so probe it directly instead of giving up. The SEC-003 // boundary on BEADS_DIR stays enforced by the callers that consume // BEADS_DIR itself; these probes only ask git about its own workspace. - return exec.CommandContext(ctx, "git", args...), nil + return exec.CommandContext(ctx, "git", args...) } - return rc.GitCmdCWD(ctx, args...), nil + return rc.GitCmdCWD(ctx, args...) } // outputHookJSON wraps content in the SessionStart hook JSON envelope shared @@ -390,10 +389,7 @@ func isMCPActive() bool { var isEphemeralBranch = func() bool { // git rev-parse --abbrev-ref --symbolic-full-name @{u} // Returns error code 128 if no upstream configured - cmd, err := primeGitCmd(context.Background(), "rev-parse", "--abbrev-ref", "--symbolic-full-name", "@{u}") - if err != nil { - return true // Default to ephemeral if we can't determine context - } + cmd := primeGitCmd(context.Background(), "rev-parse", "--abbrev-ref", "--symbolic-full-name", "@{u}") return cmd.Run() != nil } @@ -427,10 +423,7 @@ var primeAgentProfile = func() config.AgentProfile { // which keeps the -C behavior above intact. The SEC-003 boundary on BEADS_DIR // remains enforced elsewhere. var primeHasGitRemote = func() bool { - cmd, err := primeGitCmd(context.Background(), "remote") - if err != nil { - return false - } + cmd := primeGitCmd(context.Background(), "remote") out, err := cmd.Output() if err != nil { return false diff --git a/cmd/bd/purge_wisps_plane_embedded_test.go b/cmd/bd/purge_wisps_plane_embedded_test.go new file mode 100644 index 000000000..7d98671af --- /dev/null +++ b/cmd/bd/purge_wisps_plane_embedded_test.go @@ -0,0 +1,18 @@ +//go:build cgo + +package main + +import ( + "os" + "testing" +) + +func TestEmbeddedPurgeWispsPlaneRetention(t *testing.T) { + if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" { + t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt integration tests") + } + t.Parallel() + bd := buildEmbeddedBD(t) + dir, _, _ := bdInit(t, bd, "--prefix", "pwp") + runPurgeRetentionScenario(t, embeddedPurgeScenarioRunner(bd, dir)) +} diff --git a/cmd/bd/purge_wisps_plane_integration_test.go b/cmd/bd/purge_wisps_plane_integration_test.go index c7147f3ee..dc0845a47 100644 --- a/cmd/bd/purge_wisps_plane_integration_test.go +++ b/cmd/bd/purge_wisps_plane_integration_test.go @@ -4,7 +4,6 @@ package main import ( "encoding/json" - "os" "os/exec" "strings" "testing" @@ -188,16 +187,6 @@ func proxiedPurgeScenarioRunner(bd string, p proxiedProject) purgeScenarioRunner } } -func TestEmbeddedPurgeWispsPlaneRetention(t *testing.T) { - if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" { - t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt integration tests") - } - t.Parallel() - bd := buildEmbeddedBD(t) - dir, _, _ := bdInit(t, bd, "--prefix", "pwp") - runPurgeRetentionScenario(t, embeddedPurgeScenarioRunner(bd, dir)) -} - func TestProxiedServerPurgeWispsPlaneRetention(t *testing.T) { requireSharedProxiedServer(t) t.Parallel() diff --git a/cmd/bd/store_factory_embedded_test.go b/cmd/bd/store_factory_embedded_test.go new file mode 100644 index 000000000..c111543f3 --- /dev/null +++ b/cmd/bd/store_factory_embedded_test.go @@ -0,0 +1,24 @@ +//go:build cgo + +package main + +import ( + "strings" + "testing" + + "github.com/steveyegge/beads/internal/storage/embeddeddolt" +) + +// TestEmbeddedOpen_EmptyDatabaseRejected verifies that embeddeddolt.Open fails +// with a clear error when called with an empty database name, rather than +// deferring to a confusing "no database selected" SQL error. +// Belt-and-suspenders defense for be-sy8 / GH#2988. +func TestEmbeddedOpen_EmptyDatabaseRejected(t *testing.T) { + _, err := embeddeddolt.Open(t.Context(), t.TempDir(), "", "main") + if err == nil { + t.Fatal("expected error for empty database name") + } + if !strings.Contains(err.Error(), "database name must not be empty") { + t.Errorf("unexpected error: %v", err) + } +} diff --git a/cmd/bd/store_factory_test.go b/cmd/bd/store_factory_test.go index 6f76dd92c..aec0dd965 100644 --- a/cmd/bd/store_factory_test.go +++ b/cmd/bd/store_factory_test.go @@ -16,7 +16,6 @@ import ( "github.com/steveyegge/beads/internal/config" "github.com/steveyegge/beads/internal/configfile" "github.com/steveyegge/beads/internal/storage/dolt" - "github.com/steveyegge/beads/internal/storage/embeddeddolt" ) // TestNewDoltStoreFromConfig_NoMetadata verifies that newDoltStoreFromConfig @@ -308,20 +307,6 @@ func TestOpenNonMutatingStoreHonorsSharedServerConfig(t *testing.T) { // a correct implementation. const readOnlySharedServerPort = "19998" -// TestEmbeddedOpen_EmptyDatabaseRejected verifies that embeddeddolt.Open fails -// with a clear error when called with an empty database name, rather than -// deferring to a confusing "no database selected" SQL error. -// Belt-and-suspenders defense for be-sy8 / GH#2988. -func TestEmbeddedOpen_EmptyDatabaseRejected(t *testing.T) { - _, err := embeddeddolt.Open(t.Context(), t.TempDir(), "", "main") - if err == nil { - t.Fatal("expected error for empty database name") - } - if !strings.Contains(err.Error(), "database name must not be empty") { - t.Errorf("unexpected error: %v", err) - } -} - // TestNewDoltStoreFromConfig_HyphenatedDBName verifies that // newDoltStoreFromConfig auto-sanitizes hyphenated database names for embedded // mode and persists the fix to metadata.json. diff --git a/cmd/bd/test_repo_beads_guard_test.go b/cmd/bd/test_repo_beads_guard_test.go index 3857d8fc1..94f4b7838 100644 --- a/cmd/bd/test_repo_beads_guard_test.go +++ b/cmd/bd/test_repo_beads_guard_test.go @@ -8,8 +8,11 @@ import ( "strings" "testing" + "github.com/steveyegge/beads/internal/beads" + "github.com/steveyegge/beads/internal/ceiling" "github.com/steveyegge/beads/internal/config" "github.com/steveyegge/beads/internal/doltserver" + "github.com/steveyegge/beads/internal/git" "github.com/steveyegge/beads/internal/metrics" "github.com/steveyegge/beads/internal/migration" "github.com/steveyegge/beads/internal/testutil" @@ -79,6 +82,58 @@ func TestMain(m *testing.M) { func testMainInner(m *testing.M) int { origWD, _ := os.Getwd() + // Computed once and reused below for the pin, the ceiling-var boundary, + // and the guard's own watch setup, so all three agree on exactly which + // checkout this process started in. + repoRoot := findRepoRootFrom(origWD) + + // Fence the whole test binary's beads/git discovery before any test (or + // package-level init/sync.Once) gets a chance to make the first + // cached-git-context call from this worktree checkout. Without this, that + // first call permanently answers isWorktree=true / a real + // --git-common-dir for the rest of the process — regardless of later + // per-test chdirs — and beads.FindBeadsDir's worktree-fallback discovery + // then treats that as license to read and write the main checkout's + // shared .beads database (e.g. /data/projects/beads/.beads when this + // worktree is a sibling checkout). Pinning "no repository" for repoRoot + // (not just the package directory this process started in) makes that + // ambient state inert by default for any subdirectory of the checkout a + // test might chdir into, and keeps answering that way even after + // git.ResetCaches: see PinNoRepositoryUnderForTesting's doc. Tests that + // need real git behavior chdir OUTSIDE repoRoot into a fixture they + // control (cmd/bd/git_test_helpers.go's runInDir, normally a + // t.TempDir()) and call git.ResetCaches/beads.ResetCaches there, which + // gets real detection scoped to that fixture — the pin only answers for + // repoRoot and its descendants. + // + // Residual scope this pin does NOT cover: -C (main.go's flag handling) + // and doctor.ResolveBeadsDirForRepo's callers resolve a beads directory + // by shelling out to `git -C ...` directly + // (internal/beads/beads.go's FindBeadsDirFrom/ResolveBeadsDirForRepo), + // never reading this package's cached git context at all, so this pin + // cannot fence them. No test in this package currently exercises that + // path against the real checkout, so it is an untested gap rather than + // a guarded one — flagged here rather than silently assumed covered. + if repoRoot != "" { + git.PinNoRepositoryUnderForTesting(repoRoot) + } + beads.ResetCaches() + + // Separately bound any literal ancestor-directory walk that starts + // at (or under) this checkout: the pin above only defeats the + // git-context-cache vector, not a plain upward walk from a worktree + // nested under a directory that itself has a .beads (e.g. a scratch + // checkout under a dir with a live .beads ancestor). Append rather than + // overwrite: Bazel's tools/bazel/test_env.sh already sets a ceiling + // derived from TEST_SRCDIR/TEST_TMPDIR for bazel-run tests, and this + // must only add a boundary, never remove one those runs rely on. + if repoRoot != "" { + ceilingList := repoRoot + if existing := os.Getenv(ceiling.EnvVar); existing != "" { + ceilingList = existing + string(os.PathListSeparator) + repoRoot + } + _ = os.Setenv(ceiling.EnvVar, ceilingList) + } // Isolate config discovery from the repo's tracked `.beads/config.yaml`. // Many tests expect default config values; running from within this repo would @@ -250,17 +305,43 @@ func testMainInner(m *testing.M) int { return runTestsAndSweep(m) } - repoRoot := findRepoRootFrom(origWD) if repoRoot == "" { return runTestsAndSweep(m) } repoBeadsDir := filepath.Join(repoRoot, ".beads") - if _, err := os.Stat(repoBeadsDir); err != nil { + // Backstop: also watch the shared worktree-fallback .beads directory that + // beads.FindBeadsDir's discovery would fall back to for a linked worktree + // (e.g. the main checkout's .beads when this package's repo root is a + // worktree). This is computed directly via git plumbing, independent of + // the process-wide cache PinNoRepositoryUnderForTesting neutralizes + // above, so it still catches pollution if that pin is ever bypassed or + // narrowed. Computed and checked BEFORE deciding whether repoBeadsDir + // itself exists: a linked worktree with no local .beads of its own is + // exactly the layout where the fallback is live, so skipping this whole + // function when repoBeadsDir is absent would guard nothing in precisely + // the case that matters most. + fallbackBeadsDir := worktreeFallbackBeadsDirDirect(repoRoot) + + var guardDirs []string + if _, err := os.Stat(repoBeadsDir); err == nil { + guardDirs = append(guardDirs, repoBeadsDir) + } + if fallbackBeadsDir != "" && fallbackBeadsDir != repoBeadsDir { + if _, err := os.Stat(fallbackBeadsDir); err == nil { + guardDirs = append(guardDirs, fallbackBeadsDir) + } + } + if len(guardDirs) == 0 { return runTestsAndSweep(m) } - watch := []string{ + // Top-level files checked by exact name. interactions.jsonl is excluded: + // legitimately created by init during tests. last-touched is excluded: + // it is bumped by any `bd` invocation anywhere in the checkout, including + // other processes sharing it concurrently, so it is not a reliable signal + // of this suite's own writes. + watchFiles := []string{ "beads.db", "beads.db-wal", "beads.db-shm", @@ -268,17 +349,45 @@ func testMainInner(m *testing.M) int { "issues.jsonl", "beads.jsonl", "metadata.json", - // interactions.jsonl excluded: legitimately created by init during tests + "config.yaml", + "routes.jsonl", "deletions.jsonl", "molecules.jsonl", } - before := snapshotFiles(repoBeadsDir, watch) - code := runTestsAndSweep(m) - after := snapshotFiles(repoBeadsDir, watch) + // dirSnapshot pairs the flat top-level watch with a recursive snapshot of + // dolt/, the Dolt backend's own state directory. A flat name list can't + // see writes inside dolt/ (e.g. a `bd create` landing a real commit in + // the fallback's database), so this is required for the guard to mean + // what its error message claims for a Dolt-backed fallback workspace. + type dirSnapshot struct { + flat, dolt map[string]fileSnap + } + snapshotDir := func(dir string) dirSnapshot { + return dirSnapshot{ + flat: snapshotFiles(dir, watchFiles), + dolt: snapshotDoltTree(filepath.Join(dir, "dolt")), + } + } - if diff := diffSnapshots(before, after); diff != "" { - fmt.Fprintf(os.Stderr, "ERROR: test suite modified repo .beads state:\n%s\n", diff) + before := make(map[string]dirSnapshot, len(guardDirs)) + for _, dir := range guardDirs { + before[dir] = snapshotDir(dir) + } + code := runTestsAndSweep(m) + + var allDiffs string + for _, dir := range guardDirs { + after := snapshotDir(dir) + diff := diffSnapshots(before[dir].flat, after.flat) + diff += diffSnapshots(before[dir].dolt, after.dolt) + if diff != "" { + allDiffs += fmt.Sprintf("in %s:\n%s", dir, diff) + } + } + + if allDiffs != "" { + fmt.Fprintf(os.Stderr, "ERROR: test suite modified repo .beads state:\n%s\n", allDiffs) if code == 0 { code = 1 } @@ -287,6 +396,50 @@ func testMainInner(m *testing.M) int { return code } +// worktreeFallbackBeadsDirDirect resolves the shared .beads directory a +// linked worktree at repoRoot would fall back to, via a direct git shell-out +// independent of internal/git's process-wide cache (which +// PinNoRepositoryUnderForTesting deliberately neutralizes above). Mirrors +// internal/beads's worktreeFallbackBeadsDirForRepo; kept local and minimal +// since this is a test-only detection backstop, not production discovery. +func worktreeFallbackBeadsDirDirect(repoRoot string) string { + if repoRoot == "" { + return "" + } + cmd := exec.Command("git", "-C", repoRoot, "rev-parse", "--git-dir", "--git-common-dir") + out, err := cmd.Output() + if err != nil { + return "" + } + lines := strings.Split(strings.TrimSpace(string(out)), "\n") + if len(lines) < 2 { + return "" + } + gitDir := resolveGitPath(repoRoot, strings.TrimSpace(lines[0])) + commonDir := resolveGitPath(repoRoot, strings.TrimSpace(lines[1])) + if gitDir == "" || commonDir == "" || gitDir == commonDir { + return "" // not a worktree + } + if filepath.Base(commonDir) == ".git" { + return filepath.Join(filepath.Dir(commonDir), ".beads") + } + return filepath.Join(commonDir, ".beads") +} + +func resolveGitPath(repoRoot, p string) string { + if p == "" { + return "" + } + if !filepath.IsAbs(p) { + p = filepath.Join(repoRoot, p) + } + abs, err := filepath.Abs(p) + if err != nil { + return "" + } + return abs +} + type fileSnap struct { exists bool size int64 @@ -307,10 +460,43 @@ func snapshotFiles(dir string, names []string) map[string]fileSnap { return out } +// snapshotDoltTree walks dir recursively and returns a map keyed by path +// relative to dir, for directories whose structure itself needs watching +// (e.g. .beads/dolt, which stores workspace state as an arbitrary tree of +// files rather than a fixed set of top-level names snapshotFiles's explicit +// list can name). A missing or unreadable dir yields an empty map, not an +// error: a .beads without its own dolt/ is normal (a non-Dolt backend, or a +// workspace that was never provisioned), and must not be treated as 212 +// files vanishing on the "after" side. +func snapshotDoltTree(dir string) map[string]fileSnap { + out := make(map[string]fileSnap) + _ = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { + if err != nil || info == nil || info.IsDir() { + return nil //nolint:nilerr // best-effort snapshot; a walk error just narrows what gets watched + } + rel, relErr := filepath.Rel(dir, path) + if relErr != nil { + rel = path + } + out[rel] = fileSnap{exists: true, size: info.Size(), modUnix: info.ModTime().UnixNano()} + return nil + }) + return out +} + +// diffSnapshots reports entries that appeared, disappeared, or changed size +// between before and after. Checks both directions (before→after for +// removals/changes, after→before for additions) so it works for a recursive +// snapshotDoltTree map, where "after" can legitimately contain keys "before" +// never had — unlike snapshotFiles's fixed name list, where the key set is +// identical on both sides and only the addition-check is a no-op. func diffSnapshots(before, after map[string]fileSnap) string { var out string for name, b := range before { - a := after[name] + a, ok := after[name] + if !ok { + a = fileSnap{exists: false} + } if b.exists != a.exists { out += fmt.Sprintf("- %s: exists %v → %v\n", name, b.exists, a.exists) continue @@ -325,6 +511,11 @@ func diffSnapshots(before, after map[string]fileSnap) string { out += fmt.Sprintf("- %s: size %d → %d\n", name, b.size, a.size) } } + for name, a := range after { + if _, ok := before[name]; !ok && a.exists { + out += fmt.Sprintf("- %s: new file (size %d)\n", name, a.size) + } + } return out } diff --git a/cmd/bd/types_create_agreement_embedded_test.go b/cmd/bd/types_create_agreement_embedded_test.go new file mode 100644 index 000000000..60cecaa42 --- /dev/null +++ b/cmd/bd/types_create_agreement_embedded_test.go @@ -0,0 +1,20 @@ +//go:build cgo + +package main + +import ( + "os" + "testing" +) + +func TestEmbeddedTypesAgreeWithCreate(t *testing.T) { + if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" { + t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt integration tests") + } + t.Parallel() + bd := buildEmbeddedBD(t) + dir, beadsDir, _ := bdInit(t, bd, "--prefix", "tac") + assertTypesAgreeWithCreate(t, func(t *testing.T, args ...string) ([]byte, error) { + return bdRunWithFlockRetry(t, bd, dir, args...) + }, beadsDir) +} diff --git a/cmd/bd/types_create_agreement_test.go b/cmd/bd/types_create_agreement_test.go index a371dab96..7ee8476ca 100644 --- a/cmd/bd/types_create_agreement_test.go +++ b/cmd/bd/types_create_agreement_test.go @@ -96,18 +96,6 @@ func TestProxiedServerTypesAgreeWithCreate(t *testing.T) { }, p.beadsDir) } -func TestEmbeddedTypesAgreeWithCreate(t *testing.T) { - if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" { - t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt integration tests") - } - t.Parallel() - bd := buildEmbeddedBD(t) - dir, beadsDir, _ := bdInit(t, bd, "--prefix", "tac") - assertTypesAgreeWithCreate(t, func(t *testing.T, args ...string) ([]byte, error) { - return bdRunWithFlockRetry(t, bd, dir, args...) - }, beadsDir) -} - // Server mode (`bd init --server` against an external dolt sql-server) lists // through DoltStore.GetCustomTypes, the resolver that used to drop // config.yaml types once the database had any. diff --git a/internal/formula/expand.go b/internal/formula/expand.go index b276d2c39..e5c68b928 100644 --- a/internal/formula/expand.go +++ b/internal/formula/expand.go @@ -77,7 +77,7 @@ func ApplyExpansions(steps []*Step, compose *ComposeRules, parser *Parser) ([]*S vars := mergeVars(expFormula, rule.Vars) // Expand the target step (start at depth 0) - expandedSteps, err := expandStep(targetStep, expFormula.Template, 0, vars) + expandedSteps, err := expandStep(targetStep, expFormula.Template, vars) if err != nil { return nil, fmt.Errorf("expand %q: %w", rule.Target, err) } @@ -133,7 +133,7 @@ func ApplyExpansions(steps []*Step, compose *ComposeRules, parser *Parser) ([]*S // Expand each matching step for _, targetStep := range toExpand { - expandedSteps, err := expandStep(targetStep, expFormula.Template, 0, vars) + expandedSteps, err := expandStep(targetStep, expFormula.Template, vars) if err != nil { return nil, fmt.Errorf("map %q -> %q: %w", rule.Select, targetStep.ID, err) } @@ -189,13 +189,12 @@ func countStepIDs(steps []*Step, counts map[string]int) { } } -// expandStep expands a target step using the given template. -// Returns the expanded steps with placeholders substituted. -// The depth parameter tracks recursion depth for children; if it exceeds -// DefaultMaxExpansionDepth, an error is returned. +// expandStep expands a target step using the given template, starting at +// recursion depth 0 (expandStepWith tracks depth for nested children; if it +// exceeds DefaultMaxExpansionDepth, an error is returned). // The vars parameter provides variable values for {varname} substitution. -func expandStep(target *Step, template []*Step, depth int, vars map[string]string) ([]*Step, error) { - return expandStepWith(target, template, depth, func(s string) string { +func expandStep(target *Step, template []*Step, vars map[string]string) ([]*Step, error) { + return expandStepWith(target, template, 0, func(s string) string { return substituteVars(s, vars) }) } @@ -434,7 +433,7 @@ func MaterializeExpansion(f *Formula, targetID string, vars map[string]string) e return nil } - expandedSteps, err := expandStep(materializeTarget(f, targetID), f.Template, 0, vars) + expandedSteps, err := expandStep(materializeTarget(f, targetID), f.Template, vars) if err != nil { return fmt.Errorf("materializing expansion %q: %w", f.Formula, err) } @@ -542,7 +541,7 @@ func applyInlineExpansionsRecursive(steps []*Step, parser *Parser, depth int) ([ vars := mergeVars(expFormula, step.ExpandVars) // Expand the step using the template (reuse existing expandStep) - expandedSteps, err := expandStep(step, expFormula.Template, 0, vars) + expandedSteps, err := expandStep(step, expFormula.Template, vars) if err != nil { return nil, fmt.Errorf("inline expand on step %q: %w", step.ID, err) } diff --git a/internal/formula/expand_test.go b/internal/formula/expand_test.go index 63b2bb968..61f8b698f 100644 --- a/internal/formula/expand_test.go +++ b/internal/formula/expand_test.go @@ -87,7 +87,7 @@ func TestExpandStep(t *testing.T) { }, } - result, err := expandStep(target, template, 0, nil) + result, err := expandStep(target, template, nil) if err != nil { t.Fatalf("expandStep failed: %v", err) } @@ -138,7 +138,7 @@ func TestExpandStepDepthLimit(t *testing.T) { // With depth 0 start, going to level 6 means 7 levels total (0-6) // DefaultMaxExpansionDepth is 5, so this should fail - _, err := expandStep(target, template, 0, nil) + _, err := expandStep(target, template, nil) if err == nil { t.Fatal("expected depth limit error, got nil") } @@ -159,7 +159,7 @@ func TestExpandStepDepthLimit(t *testing.T) { } shallowTemplate := []*Step{shallowChild} - result, err := expandStep(target, shallowTemplate, 0, nil) + result, err := expandStep(target, shallowTemplate, nil) if err != nil { t.Fatalf("expected shallow template to succeed, got: %v", err) } diff --git a/internal/git/gitdir.go b/internal/git/gitdir.go index 1e82e14cf..9977e7735 100644 --- a/internal/git/gitdir.go +++ b/internal/git/gitdir.go @@ -31,8 +31,29 @@ type gitContext struct { var ( gitCtxOnce sync.Once gitCtx gitContext + + // pinnedRootForTesting is the root directory PinNoRepositoryUnderForTesting + // was given, or "" when no pin is active. Deliberately NOT cleared by + // ResetCaches: see that function's comment and PinNoRepositoryUnderForTesting's + // doc for why the pin must outlive a cache reset to do its job. + pinnedRootForTesting string ) +// underPinnedRootForTesting reports whether wd is pinnedRootForTesting itself +// or a descendant of it. Called with the live working directory on every +// getGitContext lookup while a pin is active, so a test that chdirs outside +// the pinned root (e.g. into its own t.TempDir() fixture) still gets real git +// detection scoped to that fixture. +func underPinnedRootForTesting(wd string) bool { + if pinnedRootForTesting == "" || wd == "" { + return false + } + if wd == pinnedRootForTesting { + return true + } + return strings.HasPrefix(wd, pinnedRootForTesting+string(filepath.Separator)) +} + // initGitContext populates the gitContext with a single git call. // This is called once per process via sync.Once. func initGitContext() { @@ -234,6 +255,11 @@ func ResolveWorkTreelessHooksContext(workDir string, env []string) (HooksContext // getGitContext returns the cached git context, initializing it if needed. func getGitContext() (*gitContext, error) { + if pinnedRootForTesting != "" { + if wd, err := os.Getwd(); err == nil && underPinnedRootForTesting(wd) { + return nil, errPinnedNoRepository + } + } gitCtxOnce.Do(initGitContext) if gitCtx.err != nil { return nil, gitCtx.err @@ -470,12 +496,75 @@ func NormalizePath(path string) string { // In production, these caches are safe because the working directory // doesn't change during a single command execution. // +// Deliberately does NOT clear a PinNoRepositoryUnderForTesting pin: that pin +// is directory-scoped (re-evaluated against the live working directory on +// every call, not baked into the one-shot gitCtx this function clears), so a +// test fixture that chdirs out of the pinned root, calls ResetCaches, and +// does real git work there is unaffected — and a test that chdirs back under +// the pinned root and calls ResetCaches on the way out (e.g. cmd/bd's +// runInDir/resetRepoCachesForTest idiom) must keep answering "not a +// repository", or every later test in the binary loses the fence the pin +// exists to provide. See PinNoRepositoryUnderForTesting. +// // WARNING: Not thread-safe. Only call from single-threaded test contexts. func ResetCaches() { gitCtxOnce = sync.Once{} gitCtx = gitContext{} } +// errPinnedNoRepository is returned by getGitContext for any working +// directory PinNoRepositoryUnderForTesting pinned. +var errPinnedNoRepository = errors.New("not a git repository (pinned for testing)") + +// PinNoRepositoryUnderForTesting pins "not a git repository" for root and +// every directory under it, so IsWorktree, GetRepoRoot, and GetMainRepoRoot +// answer as if no repository were present for any process working directory +// at or below root — including across ResetCaches, unlike the one-shot +// sentinel this replaced. +// +// This exists for whole-binary test fencing (GH#7145-style cmd/bd pollution): +// without it, the first cached-git-context call made anywhere in a test +// binary — before any individual test has had a chance to chdir into its own +// fixture — permanently answers for the rest of the process from whatever +// repository the binary happened to start in. For a worktree checkout, that +// answer includes a real --git-common-dir, which beads' worktree-fallback +// discovery (FindBeadsDir) treats as license to read and write the main +// checkout's shared .beads database. root should be the repository root the +// test binary started in (not a narrower directory like the package dir), +// so the fence covers any subdirectory of that checkout a test might chdir +// into without leaving it. +// +// The pin is directory-scoped, not a cache snapshot: getGitContext checks +// the live working directory against root on every call, before touching +// gitCtxOnce/gitCtx at all. A test that chdirs to a fixture OUTSIDE root +// (e.g. its own t.TempDir(), which is not nested under a checkout root) and +// calls ResetCaches gets real git detection scoped to that fixture, exactly +// as before this pin existed (see cmd/bd/git_test_helpers.go's runInDir). +// A test that chdirs back under root — including the package directory +// itself, where most tests run without ever chdir'ing away — keeps +// answering "not a repository" even after ResetCaches, because ResetCaches +// does not clear pinnedRootForTesting. +// +// This lives in production code rather than a _test.go file (like +// ResetCaches, which it pairs with) only so a test binary's TestMain can call +// it: TestMain runs in the package under test, not in a _test.go-only +// helper's package, and Go does not let a non-test file reach a _test.go +// identifier. Treat it exactly like ResetCaches despite that: the only +// intended caller is a TestMain (currently cmd/bd's), called once, before +// m.Run(). Production code must never call this. +// +// WARNING: Not thread-safe, like ResetCaches. Only call before m.Run(), +// before any goroutines that might read the git context are started. +func PinNoRepositoryUnderForTesting(root string) { + abs, err := filepath.Abs(root) + if err == nil { + root = abs + } + pinnedRootForTesting = root + gitCtxOnce = sync.Once{} + gitCtx = gitContext{} +} + // IsJujutsuRepo returns true if the current directory is in a jujutsu (jj) repository. // Jujutsu stores its data in a .jj directory at the repository root. func IsJujutsuRepo() bool { diff --git a/internal/git/gitdir_test.go b/internal/git/gitdir_test.go index 3301366e1..368a9d463 100644 --- a/internal/git/gitdir_test.go +++ b/internal/git/gitdir_test.go @@ -42,6 +42,67 @@ func setupTestRepo(t *testing.T) (repoPath string, cleanup func()) { return repoPath, cleanup } +// TestPinNoRepositoryUnderForTestingSurvivesResetCaches is a probe for the +// bug a branch reviewer found in the original PinNoRepositoryForTesting: it +// seeded a one-shot sentinel into gitCtx, but ResetCaches (called by every +// test fixture that chdirs, e.g. cmd/bd's runInDir/resetRepoCachesForTest) +// unconditionally cleared gitCtx, including that sentinel — so the pin was +// gone the moment any single test in the binary chdir'd and reset caches, +// defeating the whole-binary fence for every test after the first one that +// did. PinNoRepositoryUnderForTesting fixes this by making the pin +// directory-scoped and independent of gitCtx/gitCtxOnce, so ResetCaches +// cannot clear it. This test reproduces the reviewer's repro: pin a real +// repo root, simulate a fixture that chdirs OUT to its own directory and +// resets caches (which must restore real detection there), then chdir BACK +// under the pinned root and reset caches again (which must NOT restore real +// detection, because the process never left the pinned root's jurisdiction). +func TestPinNoRepositoryUnderForTestingSurvivesResetCaches(t *testing.T) { + repoPath, _ := setupTestRepo(t) + + origWD, err := os.Getwd() + if err != nil { + t.Fatalf("Getwd: %v", err) + } + t.Cleanup(func() { + if err := os.Chdir(origWD); err != nil { + t.Fatalf("restore working directory: %v", err) + } + pinnedRootForTesting = "" + ResetCaches() + }) + + if err := os.Chdir(repoPath); err != nil { + t.Fatalf("chdir to repo: %v", err) + } + PinNoRepositoryUnderForTesting(repoPath) + + if got := GetRepoRoot(); got != "" { + t.Fatalf("GetRepoRoot() = %q immediately after pinning, want \"\"", got) + } + + // Simulate a fixture OUTSIDE the pinned root: real detection should work + // there, same as before this pin existed, once caches are reset. + fixtureDir := t.TempDir() + if err := os.Chdir(fixtureDir); err != nil { + t.Fatalf("chdir to fixture: %v", err) + } + ResetCaches() + if got := GetRepoRoot(); got != "" { + t.Fatalf("GetRepoRoot() = %q in an unpinned fixture outside the pinned root, want \"\" (fixtureDir is not a repo, but it must not inherit the pinned repo's root either)", got) + } + + // Return to the pinned root and reset caches again — exactly what + // runInDir's deferred cleanup does on the way out of a subtest. The pin + // must still answer "not a repository" here. + if err := os.Chdir(repoPath); err != nil { + t.Fatalf("chdir back to pinned repo: %v", err) + } + ResetCaches() + if got := GetRepoRoot(); got != "" { + t.Fatalf("GetRepoRoot() = %q after ResetCaches back under the pinned root, want \"\" (the pin must survive ResetCaches)", got) + } +} + func TestGetGitHooksDirTildeExpansion(t *testing.T) { // Use an explicit temporary home so tilde expansion is deterministic // regardless of the environment (CI, containers, overridden homes, etc.). diff --git a/internal/storage/dolt/batch_apply_contract_test.go b/internal/storage/dolt/batch_apply_contract_test.go index d9bd43332..afb3347ea 100644 --- a/internal/storage/dolt/batch_apply_contract_test.go +++ b/internal/storage/dolt/batch_apply_contract_test.go @@ -21,125 +21,218 @@ import ( // and one copy-on-write branch. setupTestStore already marks the PARENT // parallel; no subtest here calls t.Parallel, and the history cases take // before/after deltas that are only meaningful while they run sequentially. +// +// Each subtest below calls testContext(t) for its OWN context rather than +// sharing one built once in newDoltBatchApplyFixture. Only the deadline is +// per-subtest; the store and branch above stay shared, and ordering stays +// sequential. See newDoltBatchApplyFixture's doc comment for why. func TestBatchApplyContract(t *testing.T) { - fixture, ctx, cleanup := newDoltBatchApplyFixture(t, "bapply") + fixture, cleanup := newDoltBatchApplyFixture(t, "bapply") defer cleanup() t.Run("AppliesEveryItemInDeclarationOrder", func(t *testing.T) { + ctx, cancel := testContext(t) + defer cancel() conformance.RunBatchApplyAppliesEveryItemInDeclarationOrder(t, ctx, fixture) }) t.Run("BindsEachNamedKeyToItsMintedID", func(t *testing.T) { + ctx, cancel := testContext(t) + defer cancel() conformance.RunBatchApplyBindsEachNamedKeyToItsMintedID(t, ctx, fixture) }) t.Run("ResolvesABackwardKeyRef", func(t *testing.T) { + ctx, cancel := testContext(t) + defer cancel() conformance.RunBatchApplyResolvesABackwardKeyRef(t, ctx, fixture) }) t.Run("RefusesAKeyDeclaredLater", func(t *testing.T) { + ctx, cancel := testContext(t) + defer cancel() conformance.RunBatchApplyRefusesAKeyDeclaredLater(t, ctx, fixture) }) t.Run("RefusesAKeyNoItemDeclares", func(t *testing.T) { + ctx, cancel := testContext(t) + defer cancel() conformance.RunBatchApplyRefusesAKeyNoItemDeclares(t, ctx, fixture) }) t.Run("RefusesARefNamingNeitherOrBoth", func(t *testing.T) { + ctx, cancel := testContext(t) + defer cancel() conformance.RunBatchApplyRefusesARefNamingNeitherOrBoth(t, ctx, fixture) }) t.Run("RollsBackEverythingWhenTheLastItemRefuses", func(t *testing.T) { + ctx, cancel := testContext(t) + defer cancel() conformance.RunBatchApplyRollsBackEverythingWhenTheLastItemRefuses(t, ctx, fixture) }) t.Run("NeverReordersItsItems", func(t *testing.T) { + ctx, cancel := testContext(t) + defer cancel() conformance.RunBatchApplyNeverReordersItsItems(t, ctx, fixture) }) t.Run("EndGateRefusesAHierarchyTheRequestBuilt", func(t *testing.T) { + ctx, cancel := testContext(t) + defer cancel() conformance.RunBatchApplyEndGateRefusesAHierarchyTheRequestBuilt(t, ctx, fixture) }) t.Run("EndGateCycleSurvivesSkipPerEdgeCycleCheck", func(t *testing.T) { + ctx, cancel := testContext(t) + defer cancel() conformance.RunBatchApplyEndGateCycleSurvivesSkipPerEdgeCycleCheck(t, ctx, fixture) }) t.Run("ExpectedVersionThatMatchesLetsTheItemThrough", func(t *testing.T) { + ctx, cancel := testContext(t) + defer cancel() conformance.RunBatchApplyExpectedVersionThatMatchesLetsTheItemThrough(t, ctx, fixture) }) t.Run("StaleExpectedVersionRefusesTheWholeRequest", func(t *testing.T) { + ctx, cancel := testContext(t) + defer cancel() conformance.RunBatchApplyStaleExpectedVersionRefusesTheWholeRequest(t, ctx, fixture) }) t.Run("RefusesExpectedVersionOnARowAnEarlierItemTouched", func(t *testing.T) { + ctx, cancel := testContext(t) + defer cancel() conformance.RunBatchApplyRefusesExpectedVersionOnARowAnEarlierItemTouched(t, ctx, fixture) }) t.Run("RefusesExpectedVersionOnARowAnEarlierItemCreated", func(t *testing.T) { + ctx, cancel := testContext(t) + defer cancel() conformance.RunBatchApplyRefusesExpectedVersionOnARowAnEarlierItemCreated(t, ctx, fixture) }) t.Run("EvaluatesExpectedStatusAsModified", func(t *testing.T) { + ctx, cancel := testContext(t) + defer cancel() conformance.RunBatchApplyEvaluatesExpectedStatusAsModified(t, ctx, fixture) }) t.Run("EvaluatesExpectedAssigneeAsModified", func(t *testing.T) { + ctx, cancel := testContext(t) + defer cancel() conformance.RunBatchApplyEvaluatesExpectedAssigneeAsModified(t, ctx, fixture) }) t.Run("ClosePolicyEvaluatesAtTheCloseItem", func(t *testing.T) { + ctx, cancel := testContext(t) + defer cancel() conformance.RunBatchApplyClosePolicyEvaluatesAtTheCloseItem(t, ctx, fixture) }) t.Run("AllowsAClosedParentToGainAnOpenChild", func(t *testing.T) { + ctx, cancel := testContext(t) + defer cancel() conformance.RunBatchApplyAllowsAClosedParentToGainAnOpenChild(t, ctx, fixture) }) t.Run("UpdateAfterCloseInOneRequest", func(t *testing.T) { + ctx, cancel := testContext(t) + defer cancel() conformance.RunBatchApplyUpdateAfterCloseInOneRequest(t, ctx, fixture) }) t.Run("ReportsChangedPerItem", func(t *testing.T) { + ctx, cancel := testContext(t) + defer cancel() conformance.RunBatchApplyReportsChangedPerItem(t, ctx, fixture) }) t.Run("ANoOpBatchRecordsNoHistory", func(t *testing.T) { + ctx, cancel := testContext(t) + defer cancel() conformance.RunBatchApplyANoOpBatchRecordsNoHistory(t, ctx, fixture) }) t.Run("RecordsOneEntryForAWriteThatLandedNothing", func(t *testing.T) { + ctx, cancel := testContext(t) + defer cancel() conformance.RunBatchApplyRecordsOneEntryForAWriteThatLandedNothing(t, ctx, fixture) }) t.Run("RecordsExactlyOneHistoryEntry", func(t *testing.T) { + ctx, cancel := testContext(t) + defer cancel() conformance.RunBatchApplyRecordsExactlyOneHistoryEntry(t, ctx, fixture) }) t.Run("HistoryNamesTheActorAndReadsTheProvenance", func(t *testing.T) { + ctx, cancel := testContext(t) + defer cancel() conformance.RunBatchApplyHistoryNamesTheActorAndReadsTheProvenance(t, ctx, fixture) }) t.Run("ARefusedRequestRecordsNoHistory", func(t *testing.T) { + ctx, cancel := testContext(t) + defer cancel() conformance.RunBatchApplyARefusedRequestRecordsNoHistory(t, ctx, fixture) }) t.Run("AnEphemeralBatchKeepsItsWispsAndRecordsNoDurableHistory", func(t *testing.T) { + ctx, cancel := testContext(t) + defer cancel() conformance.RunBatchApplyAnEphemeralBatchKeepsItsWispsAndRecordsNoDurableHistory(t, ctx, fixture) }) t.Run("RefusesACrossPlaneEdgeBetweenRowsItCreated", func(t *testing.T) { + ctx, cancel := testContext(t) + defer cancel() conformance.RunBatchApplyRefusesACrossPlaneEdgeBetweenRowsItCreated(t, ctx, fixture) }) t.Run("AcceptsAnExternalEdgeTarget", func(t *testing.T) { + ctx, cancel := testContext(t) + defer cancel() conformance.RunBatchApplyAcceptsAnExternalEdgeTarget(t, ctx, fixture) }) t.Run("NormalizesTheWaitsForGate", func(t *testing.T) { + ctx, cancel := testContext(t) + defer cancel() conformance.RunBatchApplyNormalizesTheWaitsForGate(t, ctx, fixture) }) t.Run("SplicesAForwardMetadataRef", func(t *testing.T) { + ctx, cancel := testContext(t) + defer cancel() conformance.RunBatchApplySplicesAForwardMetadataRef(t, ctx, fixture) }) t.Run("SplicesASelfMetadataRef", func(t *testing.T) { + ctx, cancel := testContext(t) + defer cancel() conformance.RunBatchApplySplicesASelfMetadataRef(t, ctx, fixture) }) t.Run("RefusesAMetadataRefNoItemDeclares", func(t *testing.T) { + ctx, cancel := testContext(t) + defer cancel() conformance.RunBatchApplyRefusesAMetadataRefNoItemDeclares(t, ctx, fixture) }) t.Run("TheSpliceRecordsAnUpdateEvent", func(t *testing.T) { + ctx, cancel := testContext(t) + defer cancel() conformance.RunBatchApplyTheSpliceRecordsAnUpdateEvent(t, ctx, fixture) }) t.Run("KeepsAStoredNullApartFromAnEmptyString", func(t *testing.T) { + ctx, cancel := testContext(t) + defer cancel() conformance.RunBatchApplyKeepsAStoredNullApartFromAnEmptyString(t, ctx, fixture) }) t.Run("LandsAnIdempotencyRecordWithItsWork", func(t *testing.T) { + ctx, cancel := testContext(t) + defer cancel() conformance.RunBatchApplyLandsAnIdempotencyRecordWithItsWork(t, ctx, fixture) }) t.Run("BoundsTheItemCount", func(t *testing.T) { + // 3x the package's usual testTimeout, not the standard budget: this + // subtest applies MaxApplyBatchItems (1000) real items and then + // MaxApplyBatchItems+1 more, so it does ~1000x the DB work of a + // typical subtest here. Measured at 66s against the standard 90s + // testTimeout on the server tier (25% headroom) - the same class of + // flake risk testTimeout's own doc comment describes for host + // contention. A dedicated 3x budget gives this one subtest realistic + // headroom without raising the shared testTimeout (and therefore + // every `5*testTimeout` cleanup bound) for every other subtest that + // doesn't need it. + ctx, cancel := context.WithTimeout(context.Background(), 3*testTimeout) + defer cancel() conformance.RunBatchApplyBoundsTheItemCount(t, ctx, fixture) }) t.Run("ReplayMintsANewSetOfRows", func(t *testing.T) { + ctx, cancel := testContext(t) + defer cancel() conformance.RunBatchApplyReplayMintsANewSetOfRows(t, ctx, fixture) }) t.Run("DoesNotMutateTheCallerRequest", func(t *testing.T) { + ctx, cancel := testContext(t) + defer cancel() conformance.RunBatchApplyDoesNotMutateTheCallerRequest(t, ctx, fixture) }) t.Run("RefusesAnUnusableRequest", func(t *testing.T) { + ctx, cancel := testContext(t) + defer cancel() conformance.RunBatchApplyRefusesAnUnusableRequest(t, ctx, fixture) }) } @@ -147,17 +240,25 @@ func TestBatchApplyContract(t *testing.T) { // newDoltBatchApplyFixture composes the frozen role kit with this backend's // accessor. Nothing adapts between the two: the kit's hooks are assignable to // the fixture fields of the same name. -func newDoltBatchApplyFixture(t *testing.T, prefix string) (conformance.BatchApplyFixture, context.Context, func()) { +// +// It deliberately does NOT hand back a shared context: every subtest below +// calls testContext(t) for its own freshly-timed context instead of sharing +// one built here. A single context created once, for every subtest in this +// parent, meant one slow subtest (BoundsTheItemCount's real 1000+1001-item +// apply against the server-Dolt tier) could blow the shared deadline and +// cascade "context deadline exceeded" into every subtest that happened to run +// after it — hiding the real failure behind a wall of unrelated red subtests. +// This mirrors openStoreWithOwnBudget's rationale above (be-gvnsq): giving +// each unit of work its own budget removes the coupling. The STORE and its +// copy-on-write branch are still shared across every subtest, and subtests +// still run sequentially (no t.Parallel) — only the context deadline is +// decoupled. +func newDoltBatchApplyFixture(t *testing.T, prefix string) (conformance.BatchApplyFixture, func()) { t.Helper() store, storeCleanup := setupTestStore(t) - ctx, cancel := testContext(t) - stop := func() { - cancel() - storeCleanup() - } applier, err := store.BatchApplier() if err != nil { - stop() + storeCleanup() t.Fatalf("BatchApplier(): %v", err) } kit := newDoltRoleFixtureKit(store, prefix) @@ -177,5 +278,5 @@ func newDoltBatchApplyFixture(t *testing.T, prefix string) (conformance.BatchApp _, err := store.CommitPending(ctx, "batch-apply-contract") return err }, - }, ctx, stop + }, storeCleanup } diff --git a/internal/storage/embeddeddolt/BUILD.bazel b/internal/storage/embeddeddolt/BUILD.bazel index 40ed1ab76..4e3d1b160 100644 --- a/internal/storage/embeddeddolt/BUILD.bazel +++ b/internal/storage/embeddeddolt/BUILD.bazel @@ -189,6 +189,8 @@ go_test( "pull_settle_test.go", "push_commit_pending_test.go", "querier_contract_test.go", + "race_detector_off_test.go", + "race_detector_on_test.go", "reader_contract_test.go", "ready_claimer_contract_test.go", "ready_counter_contract_test.go", diff --git a/internal/storage/embeddeddolt/batch_apply_contract_test.go b/internal/storage/embeddeddolt/batch_apply_contract_test.go index ad707b368..439bc1584 100644 --- a/internal/storage/embeddeddolt/batch_apply_contract_test.go +++ b/internal/storage/embeddeddolt/batch_apply_contract_test.go @@ -132,6 +132,38 @@ func TestBatchApplyContract(t *testing.T) { conformance.RunBatchApplyLandsAnIdempotencyRecordWithItsWork(t, ctx, fixture) }) t.Run("BoundsTheItemCount", func(t *testing.T) { + if raceEnabled { + // Under -race this subtest alone measured 903-945s (the + // in-process Dolt engine's own goroutine/lock machinery runs + // inside this instrumented binary; see race_detector_on_test.go), + // which twice pushed shard 4/20 over its 19-minute budget. The + // true production boundary (MaxApplyBatchItems accepted, one + // more refused) is NOT what this shrinks: see + // RunBatchApplyBoundsTheItemCountAtScale's doc comment — that + // boundary is independently pinned, for real and + // sub-millisecond, by internal/storage/batch_apply_test.go's + // TestPlanApplyBatchAcceptsTheBoundary (the SAME + // storage.PlanApplyBatch every backend calls before touching + // storage at all), and the refusing half below still runs at + // the true MaxApplyBatchItems+1 count because that half never + // reaches a transaction either way. Only the "at bound" half's + // REAL applied count drops here, from 1000 to 150 — still + // large enough to prove this backend's own write body (the + // version-commit-published-after-the-tx wrapper that is + // unique to this backend) does not drop, double or truncate a + // big accepted batch. The SAME inner write body + // (internal/storage/issueops.ApplyBatchInTx) this backend + // shares with internal/storage/dolt still gets a real, full + // 1000-item run there, non-race + // (TestBatchApplyContract/BoundsTheItemCount on the + // server-Dolt tier's dolt_race_off build) — unconditionally on + // merge_group/push, conditionally on PRs (full_embedded gating + // in .github/scripts/ci-embedded-tier.sh), not "every PR". See + // large_batch_apply_measure_test.go's updated note for the + // embedded-specific coverage this leaves. + conformance.RunBatchApplyBoundsTheItemCountAtScale(t, ctx, fixture, 150) + return + } conformance.RunBatchApplyBoundsTheItemCount(t, ctx, fixture) }) t.Run("ReplayMintsANewSetOfRows", func(t *testing.T) { diff --git a/internal/storage/embeddeddolt/large_batch_apply_measure_test.go b/internal/storage/embeddeddolt/large_batch_apply_measure_test.go index 5678b3d3c..889e0cd67 100644 --- a/internal/storage/embeddeddolt/large_batch_apply_measure_test.go +++ b/internal/storage/embeddeddolt/large_batch_apply_measure_test.go @@ -254,12 +254,50 @@ var wallClockShapes = []struct { // (internal/httpapi/server.go, 5 minutes): both measured shapes must // complete in a small fraction of that budget for the ceiling to be // generous headroom rather than a number picked out of thin air. +// +// The 1000-item shape is skipped under -race: that shape alone was observed +// taking 597-651s under the embedded tier's --config=embedded (race-enabled) +// CI lane, a multi-x inflation from race-instrumenting the in-process Dolt +// engine's own internal goroutine/lock machinery on every one of its +// hundreds of internal statements, not from anything this test asserts (it +// has no duration or count assertion to weaken). It twice pushed its shard +// over the job's 19-minute test timeout (see embedded-storage-test-shards.txt +// and this package's TestBatchApplyContract for the sibling case). The +// 356-item shape (the design's primary measured shape) always runs, race or +// not — only the 1000-item shape is conditionally skipped above. +// +// Coverage accounting for the skipped 1000-item shape (no assertion is +// weakened here — this test logs timing only — but the real question is +// what still exercises a true 1000-item apply at all): +// - The shared inner write body (internal/storage/issueops.ApplyBatchInTx), +// used by both this package's BatchApplier and internal/storage/dolt's, +// still gets a real, full 1000-item apply with result assertions, +// non-race, via internal/storage/dolt's own +// TestBatchApplyContract/BoundsTheItemCount. That job runs unconditionally +// on merge_group and push, but is conditional on PRs (gated by +// detect-ci-tier's full_embedded output; see +// .github/scripts/ci-embedded-tier.sh) — so "every PR" overstates it. +// - This package's OWN wrapper around that body (the +// version-commit-published-after-the-tx mechanism unique to the embedded +// backend) does NOT get a full 1000-item apply under -race anymore: this +// package's own TestBatchApplyContract/BoundsTheItemCount applies 150 +// items under -race and the full 1000 only when built without -race (see +// its doc comment and conformance.RunBatchApplyBoundsTheItemCountAtScale). +// So as of that change, the largest real, full-assertion apply embedded's +// own wrapper gets under -race, anywhere in CI, is 150 items; without +// -race it still gets the full 1000 in that same subtest. This is a +// known, deliberate gap for the race-enabled lane specifically, not an +// oversight — closing it needs a dedicated non-race embedded run (see the +// nightly workflow step added alongside this comment). func TestLargeBatchApplyWallClock_Embedded(t *testing.T) { skipUnlessEmbeddedDolt(t) ctx := t.Context() for _, tc := range wallClockShapes { t.Run(tc.name, func(t *testing.T) { + if tc.name == "1000" && raceEnabled { + t.Skip("1000-item shape skipped under -race for wall-clock timing; see doc comment above for what still covers a real 1000-item apply") + } fixture := newPristineEmbeddedDoltFixture(t, tc.db) t.Cleanup(func() { closeEmbeddedDoltStore(t, fixture.store) }) diff --git a/internal/storage/embeddeddolt/race_detector_off_test.go b/internal/storage/embeddeddolt/race_detector_off_test.go new file mode 100644 index 000000000..89a81fc8c --- /dev/null +++ b/internal/storage/embeddeddolt/race_detector_off_test.go @@ -0,0 +1,6 @@ +//go:build cgo && !race + +package embeddeddolt_test + +// raceEnabled is false in non-race builds; see race_detector_on_test.go. +const raceEnabled = false diff --git a/internal/storage/embeddeddolt/race_detector_on_test.go b/internal/storage/embeddeddolt/race_detector_on_test.go new file mode 100644 index 000000000..e3040e9eb --- /dev/null +++ b/internal/storage/embeddeddolt/race_detector_on_test.go @@ -0,0 +1,23 @@ +//go:build cgo && race + +package embeddeddolt_test + +// This pair uses `cgo && race` / `cgo && !race`, unlike the plain +// `race`/`!race` pair in internal/workapi and cmd/bd: every consumer of +// raceEnabled in this package (embeddeddolt_test) is itself gated `//go:build +// cgo`, since the embedded backend only builds under cgo at all. Adding the +// matching cgo tag here keeps this file out of non-cgo builds too, where +// raceEnabled would otherwise compile with no consumer and trip an unused +// lint finding in that lane instead of simply being absent. +// +// raceEnabled reports whether the test binary was built with the race +// detector (-race). Wall-clock numbers are unreliable (and, for the embedded +// backend specifically, dramatically inflated) under race instrumentation: +// the in-process Dolt engine's internal goroutine/lock machinery runs +// entirely inside this test binary, so every synchronization point it hits +// is itself instrumented, unlike the Dolt server (TCP) tier where that +// machinery runs in a separate, non-raced subprocess. Timing-sensitive +// measurements consult this flag before running their most expensive case. +// This is a sibling of the pair in internal/workapi and cmd/bd, which solve +// the same problem for their own packages. +const raceEnabled = true diff --git a/internal/storage/schema/schema_test.go b/internal/storage/schema/schema_test.go index 56bcac182..2b10e591a 100644 --- a/internal/storage/schema/schema_test.go +++ b/internal/storage/schema/schema_test.go @@ -242,13 +242,13 @@ func TestCheckNoDuplicateVersionsPanicsWithBothFilenames(t *testing.T) { checkNoDuplicateVersions(files) } -// TestEmbeddedMigrationSourcesHaveNoDuplicateVersions runs discovery over the +// TestGoEmbedMigrationSourcesHaveNoDuplicateVersions runs discovery over the // real embedded migration tree for both sources. list() panics on duplicate // numeric prefixes — at runtime that panic fires at store open, before any // command's RunE, so a duplicate bricks every bd command. Tests that read a // migration's SQL file directly bypass list() and cannot catch this; this // discovery-level check makes the whole failure class fail in CI instead. -func TestEmbeddedMigrationSourcesHaveNoDuplicateVersions(t *testing.T) { +func TestGoEmbedMigrationSourcesHaveNoDuplicateVersions(t *testing.T) { for _, src := range []migrationSource{mainSource, ignoredSource} { files := src.list() // panics on duplicate versions if len(files) == 0 { diff --git a/internal/templates/agents/agents_test.go b/internal/templates/agents/agents_test.go index 3f394a8ae..0c7e1c6b1 100644 --- a/internal/templates/agents/agents_test.go +++ b/internal/templates/agents/agents_test.go @@ -5,7 +5,7 @@ import ( "testing" ) -func TestEmbeddedDefault(t *testing.T) { +func TestGoEmbedDefault(t *testing.T) { content := EmbeddedDefault() if content == "" { @@ -28,13 +28,13 @@ func TestEmbeddedDefault(t *testing.T) { } } -// TestEmbeddedDefaultArchitectureSummary guards GH#3683: agents reading the +// TestGoEmbedDefaultArchitectureSummary guards GH#3683: agents reading the // generated AGENTS.md need an architecture statement at the top so they don't // build wrong mental models (treating JSONL as source of truth, manually // running bd import, etc.) and have to discover the four deeper architecture // docs the hard way. The summary must appear before the Quick Reference and // link to the canonical sync-concepts entry-point. -func TestEmbeddedDefaultArchitectureSummary(t *testing.T) { +func TestGoEmbedDefaultArchitectureSummary(t *testing.T) { content := EmbeddedDefault() required := []string{ @@ -59,7 +59,7 @@ func TestEmbeddedDefaultArchitectureSummary(t *testing.T) { } } -func TestEmbeddedBeadsSection(t *testing.T) { +func TestGoEmbedBeadsSection(t *testing.T) { section := EmbeddedBeadsSection() if section == "" { @@ -113,7 +113,7 @@ func TestDefaultContainsBothSections(t *testing.T) { } } -func TestEmbeddedDefaultManagedMarkerIsCurrent(t *testing.T) { +func TestGoEmbedDefaultManagedMarkerIsCurrent(t *testing.T) { content := EmbeddedDefault() idx := strings.Index(content, "