diff --git a/.bazelignore b/.bazelignore deleted file mode 100644 index 446a903e1..000000000 --- a/.bazelignore +++ /dev/null @@ -1,10 +0,0 @@ -npm-package/node_modules -website -node_modules -.claude/worktrees -.worktrees -.beads -.dolt -dist -.runtime -tmp diff --git a/.bazelrc b/.bazelrc deleted file mode 100644 index 0ac5774bf..000000000 --- a/.bazelrc +++ /dev/null @@ -1,523 +0,0 @@ -# Bazel configuration for beads. Bazel is beads' build and test system: CI -# gates on the `bazel test` lanes in .github/workflows/bazel.yml, and -# `make test`/`make check` run the same commands (engdocs/TESTING.md -# "Building and Testing"). Plain `go build`/`go test` still work as an -# inner-loop convenience but are not what CI enforces. Where actions run is -# per machine, never committed here: contributors read the anonymous, -# read-only cache with --config=fork-cache (defined below); maintainers with -# an rbe-west client certificate execute remotely with --config=remote-exec. -# Make either the default with a `build --config=...` line in the gitignored -# .bazelrc.local. Bzlmod only; Bazel version is pinned in .bazelversion. -common --enable_bzlmod -common --color=no -# C/C++ (and so cgo, -race and the Go stdlib) builds only with MODULE.bazel's -# pinned LLVM toolchain: no host-detected local_config_cc to fall back to, -# which would key actions to the client's gcc and lld and fail remote links -# on workers without lld (scripts/bazel_hermetic_cc_test.go). -common --repo_env=BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1 - -# The pinned remote-worker host in every action key: //platforms:rbe_worker's -# worker-env exec property is the sha256 of tools/rbe/worker-env.txt, and -# rbe-west's schedulers run an action only on a worker that measured that -# host (platforms/BUILD.bazel). Key-affecting, so unconditional: local, -# fork-cache and remote runs hash alike. -build --extra_execution_platforms=//platforms:rbe_worker - -# ICU policy (engdocs/ICU-POLICY.md): build go-mysql-server with its pure-Go -# regex backend so no target links libicu. MODULE.bazel carries the matching -# gazelle_override for github.com/dolthub/go-mysql-server. -build --@rules_go//go/config:tags=gms_pure_go - -# Keep the client PATH/env out of action keys. -build --incompatible_strict_action_env - -test --test_output=errors -# Test environment isolation (the pr-core `go test` environment of -# scripts/ci/lib/test-env.sh): every test process runs under a wrapper that -# gives it a private HOME, XDG_CONFIG_HOME, DOLT_ROOT_PATH, empty global -# gitconfig and a short, per-process TMPDIR (unix-socket sun_path limit). -# Client HOME/TMPDIR are never forwarded, so no host value enters the action key. -test --run_under=//tools/bazel:test_env -test --test_env=BD_DISABLE_METRICS=1 -test --test_env=BD_DISABLE_EVENT_FLUSH=1 - -# PR-core parity: the same test selection as scripts/ci/pr-core.sh (race, -# -short, skips, tag filters); the environment comes from the wrapper above. -test:prcore --@rules_go//go/config:race -test:prcore --test_arg=-test.short -# pr-core.sh's -parallel 4. Without it -test.parallel defaults to GOMAXPROCS, -# the executor's core count (192 on a farm worker), not a runner's 4. -test:prcore --test_arg=-test.parallel=4 -test:prcore --test_arg=-test.skip=^TestEmbedded -test:prcore --test_env=BEADS_TEST_SKIP=dolt -# As in pr.yml's PR Core step: a root or non-POSIX executor fails -# TestAddExcludePatternsRefusesReadErrors instead of skipping it. -test:prcore --test_env=BEADS_TEST_REQUIRE_EXCLUDE_PERMISSION=1 -# Every tag that belongs to another lane (scripts/bazel_policy_test.go -# bazelPRCoreExcludedTags). integration-only targets run zero tests outside -# --config=integration. -test:prcore --test_tag_filters=-dolt-server,-dolt-server-proxied,-dolt-server-integration,-dolt-server-cmd,-embedded,-manual,-integration-only - -test:ci --config=prcore -test:ci --keep_going --test_summary=terse -# tools/bazel/equivalence.py compares the Go tests Bazel ran with the ones -# `go test` would run, from each target's test.xml. rules_go only lists passing -# tests in test.xml under -test.v, and --remote_download_minimal would leave -# test.xml on the remote side, so ask for both. -test:ci --test_env=GO_TEST_WRAP_TESTV=1 -test:ci --remote_download_regex=.*/test\.xml$ -# bazel.yml publishes the non-race bd as pr.yml's ci-build-artifacts. -test:ci --remote_download_regex=.*/bin/cmd/bd/bd_for_tests/bd$ - -# Embedded-Dolt tier: PR Risk's embedded jobs ("Test (Embedded Dolt Cmd -# N/20)", "Test (Embedded Dolt Storage N/5)", "Test (Embedded Dolt -# Conformance - core/audit)"), as the `embedded`-tagged variants of bd_test -# and embeddeddolt_test. Race like the jobs' binaries, no -short, no -# BEADS_TEST_SKIP; each variant sets BEADS_TEST_EMBEDDED_DOLT=1. The shard -# variants run the jobs' shard scripts, so every Bazel shard runs exactly the -# tests of the job with the same number. -# Only the variants and what they need are built (the same race configuration -# as --config=ci, so the two lanes share every compile). -test:embedded --@rules_go//go/config:race -test:embedded --test_tag_filters=embedded -test:embedded --build_tests_only -test:embedded --keep_going -test:embedded --test_summary=terse -# The variants are timeout = "eternal" because the jobs' Go timeouts are past -# "long"; rbe-west rejects any action timeout over 1200s, so eternal means -# 1200s here, and the effective limit of every variant is -test.timeout=19m -# (the storage and cmd shard jobs pass 20m, conformance core/audit 30m), so -# that Go's timeout, which dumps every goroutine, fires before Bazel's 1200s -# kill, which does not (see the BUILD targets). -test:embedded --test_timeout=-1,-1,-1,1200 -# The jobs pass no -parallel: GOMAXPROCS on a 4-vCPU runner. -test:embedded --test_arg=-test.parallel=4 -test:embedded --test_env=GO_TEST_WRAP_TESTV=1 -test:embedded --remote_download_regex=.*/test\.xml$ -# Since D2 step 1 this lane is the tier's only pre-merge run on same-repo PRs -# (pr-risk.yml's legacy jobs stand down). Its results are cached like every -# lane's: an unchanged test reuses its recorded PASS, and nightly re-executes -# everything (--config=fresh below). -# No whole-invocation retry on a remote cache eviction ("Lost inputs"): -# Bazel would re-run every test, and a test that failed in the first -# attempt but passes in the retry would report green, unlike the legacy -# jobs, which never retried. Evicted inputs fail the step instead; a -# visible "Re-run failed jobs" retries it. -test:embedded --experimental_remote_cache_eviction_retries=0 -# The race configuration's nogo validations run once, in bazel.yml's `test` -# lane (`bazel test //... --config=ci`), which is a strict superset of every -# race-group lane's RunNogo/ValidateNogo keys. --norun_validations is a -# build-request option, not a configuration flag: it changes no action key -# (compile, link and nogo actions are shared with `test` through the action -# cache; test results are not, since each lane's test actions carry its own -# test env and args) and discards no analysis; it only skips re-running nogo's -# validation actions that the owner lane already runs on this configuration. -# scripts/nogo_lint_policy_test.go's TestNogoConfigurationsHaveOneValidatingLane -# pins `test` as this configuration's sole validating lane. -test:embedded --norun_validations - -# Dolt-server lane: what pr.yml's retired container-backed jobs ("Test -# (storage domain + uow)" and "Contract corpus") ran, but every dolt -# sql-server is a child of the test process, started from the pinned dolt in -# the test wrapper's runfiles (BEADS_TEST_DOLT_SERVER=local, set by each -# dolt-server target), so the targets run on remote workers and need no -# docker. As those jobs: race (//cmd/bd/protocol's variant opts out, like its -# job), no -short, no BEADS_TEST_SKIP=dolt, -test.parallel=4, and the -# no--short twins of the job's other packages. Results are cacheable: every -# input (test binary, dolt, env) is in the action key, and a server that -# cannot start fails the target (BEADS_TEST_REQUIRE_DOLT_CONTAINER=1) instead -# of skipping. test.xml is downloaded for tools/bazel/equivalence.py --job, -# test.log for bazel.yml's failure artifact. -test:doltserver --@rules_go//go/config:race -test:doltserver --test_tag_filters=dolt-server -test:doltserver --build_tests_only -test:doltserver --keep_going -test:doltserver --test_env=GO_TEST_WRAP_TESTV=1 -test:doltserver --test_arg=-test.parallel=4 -# Locally (rbe=off, no executor): at most 4 test processes, the jobs' 4-vCPU -# runner shape; remote workers are not limited by it. -test:doltserver --local_test_jobs=4 -test:doltserver --remote_download_regex=.*/test\.(log|xml)$ -# Race-group nogo is owned by the `test` lane; see test:embedded's comment -# above. -test:doltserver --norun_validations - -# Proxied-server tier: PR Risk's "Test (Proxied Dolt Cmd N/15)" jobs, as -# //cmd/bd:bd_proxied_test and //cmd/bd:bd_managed_local_test (the -# managed-local lifecycle in a loopback-only network namespace) (the -# dolt-server-proxied tag). Kept out of -# --config=doltserver because bazel.yml runs that lane locally too (forks, -# rbe=off), where 30 concurrent shards, each with a dolt sql-server busy on -# ~4 cores, do not fit regardless of any single shard's wall time; bazel.yml -# runs this one only with remote execution, in a job of its own -# (bazel-proxied). The build is -# --config=doltserver's: race, like PR Risk's /tmp/bd-cmd-test; the -# subprocess bd is the non-race bd_for_tests, like the jobs'. Each shard runs its job's -# shard script, whose -test.timeout=15m is past "long": the target is -# eternal, which rbe-west caps at 1200s. -test:doltserver-proxied --@rules_go//go/config:race -test:doltserver-proxied --test_tag_filters=dolt-server-proxied -test:doltserver-proxied --build_tests_only -test:doltserver-proxied --keep_going -test:doltserver-proxied --test_summary=terse -test:doltserver-proxied --test_timeout=-1,-1,-1,1200 -test:doltserver-proxied --test_env=GO_TEST_WRAP_TESTV=1 -# The jobs pass no -parallel: GOMAXPROCS (the target's 4, a runner's vCPUs). -test:doltserver-proxied --test_arg=-test.parallel=4 -test:doltserver-proxied --local_test_jobs=4 -test:doltserver-proxied --remote_download_regex=.*/test\.(log|xml)$ -# Since D2 step 2 this lane is the tier's only pre-merge run on same-repo -# PRs. Results are cached like every lane's (nightly: --config=fresh). -# No whole-invocation retry on a remote cache eviction ("Lost inputs"): -# Bazel would re-run every test, and a test that failed in the first -# attempt but passes in the retry would report green, unlike the legacy -# jobs, which never retried. Evicted inputs fail the step instead; a -# visible "Re-run failed jobs" retries it. -test:doltserver-proxied --experimental_remote_cache_eviction_retries=0 -# Race-group nogo is owned by the `test` lane; see test:embedded's comment -# above. -test:doltserver-proxied --norun_validations - -# dolt-server + proxied union (item 5): bazel.yml's bazel-dolt-race job runs -# both tiers in one invocation in mode remote, in place of the bazel-doltserver -# and bazel-proxied lanes, which still run --config=doltserver and -# --config=doltserver-proxied in every other mode. Exactly the union of the two -# member configs' lines, with one tag filter naming both tiers -# (scripts/bazel_dolt_race_test.go). Every other flag is the same in both -# members or outside any action key for the other: the race build, -# -test.parallel=4 and GO_TEST_WRAP_TESTV=1 are common; the eternal timeout -# applies to no dolt-server target; the eviction retry count is what -# doltserver gets from --config=sole-run wherever it executes remotely; -# test_summary, local_test_jobs and remote_download_regex are not in any action -# key. So each member target's test action is unchanged (aquery: all 62 -# TestRunner actions byte-identical), and whichever arm the mode picks shares -# cached results with the other. The member configs stay for those modes, make -# and developers. -test:dolt-race --@rules_go//go/config:race -test:dolt-race --test_tag_filters=dolt-server,dolt-server-proxied -test:dolt-race --build_tests_only -test:dolt-race --keep_going -test:dolt-race --test_summary=terse -test:dolt-race --test_timeout=-1,-1,-1,1200 -test:dolt-race --test_env=GO_TEST_WRAP_TESTV=1 -test:dolt-race --test_arg=-test.parallel=4 -test:dolt-race --local_test_jobs=4 -test:dolt-race --remote_download_regex=.*/test\.(log|xml)$ -test:dolt-race --experimental_remote_cache_eviction_retries=0 -# The union carries its members' --norun_validations line -# (TestBazelDoltRaceConfigIsMemberUnion needs no change: both members and the -# union carry the same line). -test:dolt-race --norun_validations - -# Server-Dolt storage tier: PR Risk's "Test (Server Dolt Conformance)" and -# "Test (Server Dolt Full Suite N/16)" jobs, as -# //internal/storage/dolt:dolt_server_{conformance,full}_test -# (dolt-server-integration tag): the integration-tagged internal/storage/dolt -# tests, non-race like the jobs' binary, against a local dolt sql-server. -# The build flags are exactly --config=integration's (the tags, and race for -# everything the variants do not turn off), so the two lanes share every -# compile: the variants' race-off binary lands in the configuration -# --config=integration already builds the non-race //cmd/bd:bd_for_tests in. -# A per-target gotags transition would instead compile the whole graph a -# third time. No BEADS_TEST_SKIP, no -short; the full suite's -# BEADS_TEST_ENV_RUN_DOLT=1 is target env. Remote only in bazel.yml, in a -# job of its own (bazel-server-storage) that shares bazel-integration's -# compiles through the remote cache. -build:doltserver-integration --@rules_go//go/config:tags=gms_pure_go,integration -test:doltserver-integration --@rules_go//go/config:race -test:doltserver-integration --test_tag_filters=dolt-server-integration -test:doltserver-integration --build_tests_only -test:doltserver-integration --keep_going -test:doltserver-integration --test_summary=terse -test:doltserver-integration --test_timeout=-1,-1,-1,1200 -test:doltserver-integration --test_env=GO_TEST_WRAP_TESTV=1 -# The jobs pass no -parallel: GOMAXPROCS (the targets' 4, a runner's vCPUs). -test:doltserver-integration --test_arg=-test.parallel=4 -test:doltserver-integration --local_test_jobs=4 -test:doltserver-integration --remote_download_regex=.*/test\.(log|xml)$ -# F5 S2 Variant A: this configuration (integration, doltserver-integration, -# doltserver-cmd -- the same fingerprint, race=true plus -# tags=gms_pure_go,integration) has no full-lane owner. Its nogo runs once, -# narrowly, in bazel.yml's bazel-integration job: a `bazel build -# --output_groups=nogo_fix` step over //internal/testutil/integration only -# (the configuration's non-test library; the 56 integration-tagged _test.go -# files this loses vet/lint coverage on are listed in -# tools/nogo/unvalidated_integration_tests.txt, f5-spec.md §3/§5 S2). -# --norun_validations is a build-request option, not a configuration flag: -# it changes no action key and discards no analysis; it only skips -# re-running nogo's validation actions, which bazel-integration's nogo-only -# step already runs for this configuration's library files. -# scripts/nogo_lint_policy_test.go's TestNogoConfigurationsHaveOneValidatingLane -# pins the nogo-only step as this configuration's sole validating lane. -test:doltserver-integration --norun_validations -# Since D2 step 2 this lane is the tier's only pre-merge run on same-repo -# PRs. Results are cached like every lane's (nightly: --config=fresh). -# No whole-invocation retry on a remote cache eviction ("Lost inputs"): -# Bazel would re-run every test, and a test that failed in the first -# attempt but passes in the retry would report green, unlike the legacy -# jobs, which never retried. Evicted inputs fail the step instead; a -# visible "Re-run failed jobs" retries it. -test:doltserver-integration --experimental_remote_cache_eviction_retries=0 - -# cmd/bd Dolt-server tier: //cmd/bd:bd_dolt_server_test (dolt-server-cmd -# tag), the whole integration-tagged cmd/bd suite against a hermetic dolt -# sql-server, i.e. the Dolt-gated cmd/bd tests every other lane skips -# (BEADS_TEST_SKIP=dolt in --config=prcore/ci and --config=integration), and //tests/regression:regression_test -# (the differential regression suite, formerly regression.yml, against the -# catalog-pinned baseline in @bd_releases). The build flags are exactly -# --config=integration's, so it compiles nothing bazel-integration does not -# (shared through the remote cache). bazel.yml runs it where it runs the -# integration lane (remote, or with the read-only cache in mode cache), in a -# job of its own (bazel-cmd-dolt), advisory until pr.yml's -# BAZEL_CMD_DOLT_REQUIRED says otherwise. No BEADS_TEST_SKIP, no -short. -# Results are cacheable, as in --config=doltserver: every input is in the -# action key, and a server that cannot start fails the target. -build:doltserver-cmd --@rules_go//go/config:tags=gms_pure_go,integration -test:doltserver-cmd --@rules_go//go/config:race -test:doltserver-cmd --test_tag_filters=dolt-server-cmd -test:doltserver-cmd --build_tests_only -test:doltserver-cmd --keep_going -test:doltserver-cmd --test_summary=terse -test:doltserver-cmd --test_timeout=-1,-1,-1,1200 -# As in --config=integration: Go's own timeout fires first and dumps every -# goroutine to test.log, which Bazel's kill at 1200s does not. Item 5 A2: the -# -test.timeout=19m arg and the BEADS_TEST_REQUIRE_EXCLUDE_PERMISSION env moved -# onto //cmd/bd:bd_dolt_server_test's and //tests/regression:regression_test's -# own args/env (the rules), so --config=dolt-int below does not need them: -# without the move, server-storage's 15m Go timeout would become 19m under -# the union, and the env would leak into server-storage's targets. The final -# argv and environment Bazel assembles (target args, then -test.timeout=19m, -# then this config's own -test.parallel=4 test_arg; same env map either way) -# are unchanged, so the remote action digest -- what RBE's cache keys on -- -# is unaffected and cached results should carry over. This IS a local -# actionKey change for these two targets' 46 actions: --test_env is part of -# the build configuration's own fingerprint, picked up by every target -# analyzed under that configuration, unlike a target's own env attribute, -# which only the target carries. To be confirmed in practice by the first -# merge group's tests_cached == tests_total in ci-analytics, not claimed here -# as already observed. -test:doltserver-cmd --test_arg=-test.parallel=4 -test:doltserver-cmd --test_env=GO_TEST_WRAP_TESTV=1 -test:doltserver-cmd --local_test_jobs=4 -test:doltserver-cmd --remote_download_regex=.*/test\.(log|xml)$ -# F5 S2 Variant A: same configuration as --config=integration / -# doltserver-integration above; see that block's comment for the owner -# (bazel-integration's nogo-only step over //internal/testutil/integration). -test:doltserver-cmd --norun_validations - -# server-storage + cmd-dolt union (item 5 A2): bazel.yml's bazel-dolt-int job -# runs both tiers in one invocation in mode remote, in place of the -# bazel-server-storage and bazel-cmd-dolt jobs, which still run -# --config=doltserver-integration and --config=doltserver-cmd in every other -# mode (fork-ro, fork-rw, cache, local): there one client does both tiers' -# work with no in-flight deduplication, so a union would cost the sum of the -# tiers instead of the max. Exactly the union of the two member configs' -# lines above, with one tag filter naming both members' tags -# (scripts/bazel_dolt_int_test.go). Every other flag is the same in both -# members: the race build, tags=gms_pure_go,integration, keep_going, -# test_summary, the eternal 1200s timeout, GO_TEST_WRAP_TESTV=1, -# -test.parallel=4, local_test_jobs, remote_download_regex and -# norun_validations. So each member target's test action is unchanged in the -# sense that matters for caching (aquery: all 63 TestRunner actions' remote -# action digests are byte-identical, the key RBE's cache lookups use), though -# each action's local actionKey differs since these flags now reach the -# target via its own env/args rather than the member's build configuration. -# Cached results carry over both ways regardless. -# New for cmd-dolt here: --experimental_remote_cache_eviction_retries=0. -# server-storage already has it (above); cmd-dolt did not (it has no -# --config=sole-run of its own, unlike doltserver). Adding it to the union is -# stricter for cmd-dolt (no whole-invocation retry on a remote cache -# eviction) and policy-conformant with sole-run semantics; the eviction -# allowlist in pr_risk_bazel_coverage_test.go records the new line. -build:dolt-int --@rules_go//go/config:tags=gms_pure_go,integration -test:dolt-int --@rules_go//go/config:race -test:dolt-int --test_tag_filters=dolt-server-integration,dolt-server-cmd -test:dolt-int --build_tests_only -test:dolt-int --keep_going -test:dolt-int --test_summary=terse -test:dolt-int --test_timeout=-1,-1,-1,1200 -test:dolt-int --test_env=GO_TEST_WRAP_TESTV=1 -test:dolt-int --test_arg=-test.parallel=4 -test:dolt-int --local_test_jobs=4 -test:dolt-int --remote_download_regex=.*/test\.(log|xml)$ -test:dolt-int --norun_validations -test:dolt-int --experimental_remote_cache_eviction_retries=0 - -# Integration lane: the `go test` main.yml's former "Main Linux integration" -# jobs ran on push (this lane replaced them, and nightly.yml's retired Full -# Test Suite, which ran the same `go test` over ./... in one job): every package -# built with -tags=integration,gms_pure_go, race, no -short, no -skip, -# BEADS_TEST_SKIP=dolt. The tag is set for the whole build, as `go test -tags` -# does, not per target: gazelle generates srcs/deps with integration files -# included (root BUILD.bazel `gazelle:build_tags`), rules_go drops them from -# every other configuration, and //internal/testutil/integration (integration -# files only) compiles to its real contents only here. A per-target `gotags` -# would not save the compile either: it transitions the whole dep graph. -build:integration --@rules_go//go/config:tags=gms_pure_go,integration -test:integration --@rules_go//go/config:race -test:integration --test_env=BEADS_TEST_SKIP=dolt -# The variants the PR-core lane leaves out stay out, except integration-only -# (targets with tests only under the tag): the jobs' BEADS_TEST_SKIP=dolt -# keeps every container- or server-backed test skipped, which is what the -# untagged base targets already do. scripts/bazel_policy_test.go derives the -# required exclusions from the PR-core list, so a new lane tag lands here too. -# pr-core-only targets (the historical-release suites: shell tests against -# the release-shaped bd, which the PR-core lane already runs) stay out too. -test:integration --test_tag_filters=-dolt-server,-dolt-server-proxied,-dolt-server-integration,-dolt-server-cmd,-embedded,-manual,-pr-core-only -test:integration --build_tests_only -test:integration --keep_going -test:integration --test_summary=terse -# The jobs pass no -p/-parallel: GOMAXPROCS on a 4-vCPU ubuntu-latest runner. -test:integration --test_arg=-test.parallel=4 -# The jobs give each package binary -timeout=30m. rbe-west rejects an action -# timeout over 1200s, so every size gets 1200s, and Go's own -test.timeout (19m, -# after the 1200s rules_go derives from TEST_TIMEOUT: the last flag wins) fires -# first and dumps every goroutine to test.log, which Bazel's kill does not. -test:integration --test_timeout=1200 -test:integration --test_arg=-test.timeout=19m -# The runner is not root, so TestAddExcludePatternsRefusesReadErrors runs -# there; make a root executor fail it rather than skip (as test:prcore does). -test:integration --test_env=BEADS_TEST_REQUIRE_EXCLUDE_PERMISSION=1 -# -test.v in test.xml, as in test:ci, for tools/bazel/equivalence.py. -test:integration --test_env=GO_TEST_WRAP_TESTV=1 -# ci and integration build into the same output directory (the tags flag -# does not change it), so they share bazel-testlogs: a lane's test.xml is -# overwritten by the other lane's next run of the same target. Run -# equivalence.py / check_testcases.py right after each lane's own -# `bazel test`, before the other lane runs (or give the lanes separate jobs). -test:integration --remote_download_regex=.*/test\.xml$ -# F5 S2 Variant A: same configuration as --config=doltserver-integration / -# doltserver-cmd above; see that block's comment for the owner -# (bazel-integration's nogo-only step over //internal/testutil/integration). -# The 56 integration-tagged _test.go files this configuration no longer -# validates in any full lane are the accepted loss, listed in -# tools/nogo/unvalidated_integration_tests.txt. -test:integration --norun_validations - -# Pure-Go lane: pr.yml's "Check pure-Go and js/wasm boundaries -# (CGO_ENABLED=0)" job. CGO is off for every Go target (rules_go drops -# cgo-constrained files like go build does; third_party/patches/ -# gozstd_nocgo.patch lets dolt's store/nbs, an unused but declared dep, -# compile; its package init panics, so a pure binary that links it fails when -# run). bazel.yml builds bd and the job's pure test binaries, runs each of -# them once (the contamination check `go build` does at compile time) and runs -# its -short cmd/bd subset. -build:pure --@rules_go//go/config:pure -test:pure --test_arg=-test.short -# The job passes no -parallel: GOMAXPROCS on a 4-vCPU runner. -test:pure --test_arg=-test.parallel=4 -test:pure --test_env=GO_TEST_WRAP_TESTV=1 -test:pure --remote_download_regex=.*/test\.xml$ - -# js/wasm hook boundary (same job): build for GOOS=js GOARCH=wasm. Bazel -# cannot run the wasm test binary itself; bazel.yml runs it with the Go SDK's -# go_js_wasm_exec (node), like `go test -exec`. The test toolchain needs an -# exec platform matching the target platform, hence -# --use_target_platform_for_tests (no test action runs under this config). -build:js-wasm --platforms=@rules_go//go/toolchain:js_wasm -build:js-wasm --use_target_platform_for_tests -build:js-wasm --remote_download_outputs=toplevel - -# nogo (//tools/nogo: go test's vet checks plus the golangci-lint linters -# .golangci.yml enables) runs beside every Go compile, so `bazel build`/ -# `bazel test` fails on a finding -- in bazel.yml's one validating (owner) -# lane per configuration (since F5 S1: a race-group non-owner lane reuses -# its owner's nogo results instead of re-running them; see -# scripts/nogo_lint_policy_test.go's nogoConfigurations table). -# --config=nogo asks for the analysis alone (the nogo_fix output group; -# nothing is linked), in the race configuration bazel.yml's unit lane -# (test:ci) builds, so a local run reuses CI's remote cache. make lint, -# scripts/pr-lint and the pre-commit hook use it. -build:nogo --@rules_go//go/config:race -build:nogo --keep_going -build:nogo --output_groups=nogo_fix -# The same analyzers over the files only other platforms compile -# (//go:build windows, darwin, !linux ...): scripts/pr-lint builds -# //tools/bazel:release_cross, every go_library and go_binary split-transitioned -# to rules_go's non-cgo platforms, for its windows/amd64 and darwin/arm64 -# passes (golangci-lint's former GOOS=windows/darwin legs). bazel.yml's pure -# lane builds the same target for every release platform, which validates -# nogo there too. -build:nogo-cross --keep_going -build:nogo-cross --output_groups=nogo_fix - -# D2 step 3: bazel.yml's bazel-test, bazel-pure and bazel-doltserver lanes -# add this in the remote modes, where (on same-repo PRs and merge groups) -# they are the only pre-merge run of pr.yml's legacy PR Core, -# pure-Go/js-wasm, domain+uow and contract corpus jobs, which never retried. -# Like the retired PR Risk tiers' configs: no whole-invocation retry after -# a remote cache eviction ("Lost inputs"), whose exit code outranks a test -# failure, so a test that failed in the first attempt and passed in the -# retry would report green. -test:sole-run --experimental_remote_cache_eviction_retries=0 - -# Test result caching. Every lane caches its test results (read and write; -# Bazel's default): a test whose action key (test binary, runfiles, test env, -# platform) is unchanged reuses the PASS the farm recorded for it, on a PR -# (any attempt), a merge group or a push to main, so only what a change -# touched runs. Why that is safe: -# - Identical key, identical test: these targets are hermetic (dolt and -# every input are runfiles). -# - A cached failure is never reused: Bazel treats a failed cached action -# as a miss, so whatever failed re-runs. -# - Only rbe-west's workers write the action cache (clients' writes are -# refused), so a cached PASS is a real remote execution, not a client -# upload. -# - What is given up is re-sampling a nondeterministic (-race, timing) -# test on unchanged inputs. nightly.yml passes fresh-test-results: true, -# so bazel.yml appends this config last to every `bazel test` and the -# whole suite re-executes once a night. -# - No retries: --experimental_remote_cache_eviction_retries=0 (the -# retired tiers' configs and sole-run) and no flaky attempts anywhere. -# Policy: scripts/ci_merge_queue_test.go. -test:fresh --nocache_test_results - -# Read-only anonymous cache for fork PRs: rbe-west's rbe-cache endpoint serves -# action-cache and CAS reads only (writes and Execute are refused by the farm) -# and needs no credentials, so it is committed (scripts/bazel_policy_test.go -# allowlists exactly the two endpoint lines). bazel.yml's rbe job selects it -# (mode "cache") for fork pull requests; setup-bazel adds --config=fork-cache. -# Only non-key flags differ from trusted runs, so fork actions hash like the -# trusted ones and hit what trusted CI executed. -build:fork-cache --remote_cache=grpcs://rbe-cache.ops.gascity.com:8443 -build:fork-cache --remote_instance_name=oss -build:fork-cache --remote_accept_cached -build:fork-cache --noremote_upload_local_results -# bazel.yml passes --build_event_json_file and --profile; with a remote cache -# Bazel would upload the files the build events reference (test logs, the -# profile) over ByteStream. Fork runs upload nothing. -build:fork-cache --experimental_build_event_upload_strategy=local -build:fork-cache --remote_download_minimal -# If the farm closes the endpoint (kill switch) GetCapabilities fails, and -# Bazel 9 then fails every action unless both of these are set. -build:fork-cache --remote_local_fallback -build:fork-cache --incompatible_remote_local_fallback_for_remote_cache -# Lookups are network-bound; local CPU work is still bounded by --local_resources. -build:fork-cache --jobs=64 -# A slow (not closed) endpoint: each lookup gives up after 15s per attempt, -# and the failure-rate circuit breaker stops calling the endpoint once too -# many lookups fail (Bazel's default: 10% within 60s); the rest run locally. -build:fork-cache --remote_timeout=15 -build:fork-cache --remote_retries=2 -build:fork-cache --experimental_circuit_breaker_strategy=failure -# The farm admits 16 connections per source IP. -build:fork-cache --remote_max_connections=8 -# No --remote_cache_compression here: setup-bazel's write-bazelrc.sh adds it -# for fork-cache while rbe-cache advertises zstd (cache-zstd-probe.sh), so the -# server decides and rollback needs no change here. - -# Remote execution against any REAPI-compatible service. Only generic tuning -# lives here; the executor endpoint and TLS credentials are never committed. -# Developers put them in a gitignored user.bazelrc or .bazelrc.local; CI writes -# a generated rc outside the workspace and passes it with --bazelrc. -build:remote-exec --jobs=64 -build:remote-exec --remote_download_minimal -build:remote-exec --remote_timeout=600 -build:remote-exec --remote_retries=3 -# Upload only remotely executed results. A locally executed action (a -# no-remote-exec test, whose result depends on the host) must not seed the -# shared cache that CI and every other developer read. -build:remote-exec --noremote_upload_local_results - -try-import %workspace%/.bazelrc.local -try-import %workspace%/user.bazelrc diff --git a/.bazelversion b/.bazelversion deleted file mode 100644 index b13d146a7..000000000 --- a/.bazelversion +++ /dev/null @@ -1 +0,0 @@ -9.3.0 diff --git a/.claude-plugin/marketplace.json b/.claude-plugin/marketplace.json index aad41ecfb..b31296c69 100644 --- a/.claude-plugin/marketplace.json +++ b/.claude-plugin/marketplace.json @@ -9,7 +9,7 @@ "name": "beads", "source": "./plugins/beads", "description": "AI-supervised issue tracker for coding workflows", - "version": "1.3.0" + "version": "1.3.2-rc.2" } ] } diff --git a/.claude/skills/beads-docs/SKILL.md b/.claude/skills/beads-docs/SKILL.md index a5454d2ea..52bab8362 100644 --- a/.claude/skills/beads-docs/SKILL.md +++ b/.claude/skills/beads-docs/SKILL.md @@ -201,8 +201,7 @@ document what the pinned release does, never main-only features. See ## 9. Verify before you call it done Run the gates in [references/verification.md](references/verification.md). -The short list: `bazel test //test/docsync:docsync_test --config=ci` -(nav↔file sync + link conventions), +The short list: `go test ./test/docsync` (nav↔file sync + link conventions), `./scripts/generate-cli-docs.sh --check` (generated docs fresh), `./scripts/check-doc-freshness.sh` (Last reviewed markers), and a live preview with `make docs-dev` (or `./mint.sh dev`) at `localhost:3000`. diff --git a/.claude/skills/beads-docs/references/simplification.md b/.claude/skills/beads-docs/references/simplification.md index 916834ca5..5152fd2b5 100644 --- a/.claude/skills/beads-docs/references/simplification.md +++ b/.claude/skills/beads-docs/references/simplification.md @@ -39,9 +39,8 @@ don't measure that work by the word delta. *not* simplifying — it buries the real change in the diff. 4. **Loss-check** (below). 5. **Fact-check** (below). -6. **Gates:** `bazel test //test/docsync:docsync_test --config=ci`, every - fence parses, no body H1, no HTML comments (MDX), preview with - `make docs-dev`. +6. **Gates:** `go test ./test/docsync`, every fence parses, no body H1, no + HTML comments (MDX), preview with `make docs-dev`. 7. **Preview, then commit on approval** — one page or section per batch, so the reviewer can steer before a cut compounds across the section. diff --git a/.claude/skills/beads-docs/references/terminology.md b/.claude/skills/beads-docs/references/terminology.md index e3f46d5dc..3718007bd 100644 --- a/.claude/skills/beads-docs/references/terminology.md +++ b/.claude/skills/beads-docs/references/terminology.md @@ -8,7 +8,7 @@ the binary still prints, the docs now lie. ## The discipline 1. **Survey first.** Count occurrences in `docs/`, `engdocs/`, `README.md`, - every `AGENTS.md` (root and nested), and `*.go`. Read enough to tell + `AGENTS.md`, `AGENT_INSTRUCTIONS.md`, and `*.go`. Read enough to tell *prose* (the concept) from *literals*. 2. **Rename prose only**, with judgment per occurrence — fix articles and grammar so it reads naturally. diff --git a/.claude/skills/beads-docs/references/verification.md b/.claude/skills/beads-docs/references/verification.md index 44aabca76..762154ce5 100644 --- a/.claude/skills/beads-docs/references/verification.md +++ b/.claude/skills/beads-docs/references/verification.md @@ -8,7 +8,7 @@ Run these before considering docs work complete. Ordered cheapest-first. # 1. Docs sync: docs.json nav <-> file consistency, link conventions # (root-relative extensionless in docs/, exact paths in engdocs/ and # curated root files), orphan detection (only CLI_REFERENCE.md is exempt). -bazel test //test/docsync:docsync_test --config=ci +go test ./test/docsync # 2. Generated CLI docs freshness: regenerates from the live command tree # and diffs docs/CLI_REFERENCE.md, docs/cli-reference/, and the CLI @@ -55,7 +55,7 @@ all of: 1. **Redirect** — add an entry to the `redirects` array in `docs/docs.json` from the old route to the new one. 2. **Rewrite inbound links** — grep the whole repo, not just `docs/`: - README.md, every AGENTS.md (root and nested), engdocs/, examples/, + README.md, AGENTS.md, AGENT_INSTRUCTIONS.md, engdocs/, examples/, npm-package/, plugins/, integrations/, scripts, and Go comments. 3. **Check bd's printed output** — if `bd` prints the old path (grep `cmd/` and `internal/templates/` for it), fix the Go source to print the new diff --git a/.claude/test-strategy.md b/.claude/test-strategy.md new file mode 100644 index 000000000..3b3709ec5 --- /dev/null +++ b/.claude/test-strategy.md @@ -0,0 +1,103 @@ +# Test Running Strategy for Claude Code + +## Critical Rules + +1. **ALWAYS use `./scripts/test.sh` instead of `go test` directly** + - It automatically skips broken tests from `.test-skip` + - Uses appropriate timeouts (3m default) + - Consistent with human developers and CI/CD + +2. **Use `-run` to target specific tests when developing features** + ```bash + # Good: When working on feature X + ./scripts/test.sh -run TestFeatureX ./cmd/bd/... + + # Avoid: Running full suite unnecessarily + ./scripts/test.sh ./... + ``` + +3. **Understand the bottleneck: COMPILATION not EXECUTION** + - 180s compilation time vs 3.8s actual test execution (cmd/bd) + - Running subset of tests doesn't save much time (still recompiles) + - But use `-run` anyway to avoid seeing unrelated failures + +## Common Commands + +```bash +# Full test suite (what 'make test' runs) +./scripts/test.sh + +# Test specific package +./scripts/test.sh ./cmd/bd/... +./scripts/test.sh ./internal/storage/embeddeddolt/... + +# Test specific feature +./scripts/test.sh -run TestCreate ./cmd/bd/... +./scripts/test.sh -run TestImport + +# Verbose output (when debugging) +./scripts/test.sh -v -run TestSpecificTest +``` + +## When Tests Fail + +1. **Check if it's a known broken test:** + ```bash + cat .test-skip + ``` + +2. **If it's new, investigate:** + - Read the test failure message + - Run with `-v` for more detail + - Check if recent code changes broke it + +3. **If unfixable now:** + - File GitHub issue with details + - Add to `.test-skip` with issue reference + - Document in commit message + +## Package Size Context + +The `cmd/bd` package is LARGE: +- 41,696 lines of code +- 205 files (82 test files) +- 313 individual tests +- Compilation takes ~180 seconds + +This is why: +- Compilation is slow +- Test script uses 3-minute timeout +- Targeting specific tests is important + +## Environment Variables + +Use these when needed: + +```bash +# Custom timeout +TEST_TIMEOUT=5m ./scripts/test.sh + +# Verbose by default +TEST_VERBOSE=1 ./scripts/test.sh + +# Run pattern +TEST_RUN=TestSomething ./scripts/test.sh +``` + +## Quick Reference + +| Task | Command | +|------|---------| +| Run all tests | `make test` or `./scripts/test.sh` | +| Test one package | `./scripts/test.sh ./cmd/bd/...` | +| Test one function | `./scripts/test.sh -run TestName` | +| Verbose output | `./scripts/test.sh -v` | +| Custom timeout | `./scripts/test.sh -timeout 10m` | +| Skip additional test | `./scripts/test.sh -skip TestFoo` | + +## Remember + +- The test script is in `.gitignore` path: `scripts/test.sh` +- Skip list is in repo root: `.test-skip` +- Full documentation: `engdocs/TESTING.md` +- Current broken tests: See GH issues #355, #356 diff --git a/.githooks/post-checkout b/.githooks/post-checkout index 8806ec466..496003ad6 100755 --- a/.githooks/post-checkout +++ b/.githooks/post-checkout @@ -1,5 +1,5 @@ #!/usr/bin/env sh -# --- BEGIN BEADS INTEGRATION v1.3.0 --- +# --- BEGIN BEADS INTEGRATION v1.3.2-rc.2 --- # This section is managed by beads. Do not remove these markers. if command -v bd >/dev/null 2>&1; then export BD_GIT_HOOK=1 @@ -19,7 +19,7 @@ if command -v bd >/dev/null 2>&1; then if command -v "$_bd_timeout_candidate" >/dev/null 2>&1; then if _bd_timeout_version="$("$_bd_timeout_candidate" --version 2>/dev/null)"; then case "$_bd_timeout_version" in - "timeout (GNU coreutils) "*|"timeout (uutils coreutils) "*) _bd_timeout_command=$_bd_timeout_candidate; break ;; + "timeout (GNU coreutils) "*) _bd_timeout_command=$_bd_timeout_candidate; break ;; esac fi fi @@ -56,4 +56,4 @@ if command -v bd >/dev/null 2>&1; then fi if [ "$_bd_exit" -ne 0 ]; then exit "$_bd_exit"; fi fi -# --- END BEADS INTEGRATION v1.3.0 --- +# --- END BEADS INTEGRATION v1.3.2-rc.2 --- diff --git a/.githooks/post-merge b/.githooks/post-merge index f46a0b142..e99d18348 100755 --- a/.githooks/post-merge +++ b/.githooks/post-merge @@ -1,5 +1,5 @@ #!/usr/bin/env sh -# --- BEGIN BEADS INTEGRATION v1.3.0 --- +# --- BEGIN BEADS INTEGRATION v1.3.2-rc.2 --- # This section is managed by beads. Do not remove these markers. if command -v bd >/dev/null 2>&1; then export BD_GIT_HOOK=1 @@ -19,7 +19,7 @@ if command -v bd >/dev/null 2>&1; then if command -v "$_bd_timeout_candidate" >/dev/null 2>&1; then if _bd_timeout_version="$("$_bd_timeout_candidate" --version 2>/dev/null)"; then case "$_bd_timeout_version" in - "timeout (GNU coreutils) "*|"timeout (uutils coreutils) "*) _bd_timeout_command=$_bd_timeout_candidate; break ;; + "timeout (GNU coreutils) "*) _bd_timeout_command=$_bd_timeout_candidate; break ;; esac fi fi @@ -56,4 +56,4 @@ if command -v bd >/dev/null 2>&1; then fi if [ "$_bd_exit" -ne 0 ]; then exit "$_bd_exit"; fi fi -# --- END BEADS INTEGRATION v1.3.0 --- +# --- END BEADS INTEGRATION v1.3.2-rc.2 --- diff --git a/.githooks/pre-commit b/.githooks/pre-commit index 8e4a17732..f708ef665 100755 --- a/.githooks/pre-commit +++ b/.githooks/pre-commit @@ -1,5 +1,5 @@ #!/usr/bin/env bash -# Pre-commit hook: auto-format with gofmt, then lint and vet with nogo. +# Pre-commit hook: auto-format with gofmt and lint with golangci-lint. # Installed via: git config core.hooksPath .githooks # See also: make install (sets hooksPath automatically) @@ -9,24 +9,38 @@ set -euo pipefail # the beads section at the bottom must run for non-Go commits too. staged_go_files=$(git diff --cached --name-only --diff-filter=ACM -- '*.go') if [ -n "$staged_go_files" ]; then - # Auto-format and re-stage, through the go.mod-pinned gofmt. A bare PATH - # gofmt belongs to the host toolchain, and on a host newer than the go - # directive it rewrites and re-stages files into a form CI's gofmt rejects - # -- silently, on every Go commit. See scripts/ci/gofmt-bin.sh. - gofmt_bin="$(./scripts/ci/gofmt-bin.sh)" - echo "$staged_go_files" | xargs "$gofmt_bin" -w + # Build golangci-lint from source so it is never a stale global binary, and + # pin the version to the one CI uses (.github/workflows/{pr,main}.yml). + GOLANGCI_LINT="go run github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.10.1" + + # Build it with the repo's own Go, the way CI does. In CI, actions/setup-go + # installs the go.mod version and exports GOTOOLCHAIN=local. Locally + # GOTOOLCHAIN defaults to "auto", and for a pkg@version build that honours + # golangci-lint's own toolchain directive instead: it downgrades to + # go1.25.12, which then refuses this repo's newer go directive at config + # load ("the Go language version (go1.25) used to build golangci-lint is + # lower than the targeted Go version") and fails every Go commit. Pinning to + # the go directive reproduces CI's resolution on any host toolchain. + go_toolchain="go$(awk '$1 == "go" { print $2; exit }' go.mod)" + if [ "$go_toolchain" = "go" ]; then + echo >&2 "pre-commit: no go directive in go.mod; cannot pin the lint toolchain" + exit 1 + fi + + # Auto-format and re-stage + echo "$staged_go_files" | xargs gofmt -w echo "$staged_go_files" | xargs git add - # Lint and vet: nogo (//tools/nogo) over the staged files' Bazel packages, - # the analyzers bazel.yml gates on (go test's vet checks plus the - # golangci-lint linters .golangci.yml enables). Findings fail the commit; - # nogo prints a `patch -p1` command for the ones with suggested fixes. - # Needs bazel (or bazelisk) on PATH; the windows/darwin passes run in CI - # and in `make lint`. - make lint-changed LINT_CHANGED_SCOPE=staged || exit 1 + # Lint changed code with auto-fix; block on remaining issues. + # CGO_ENABLED=0 avoids typecheck failures from CGO-only dependencies + # (e.g., dolthub/go-icu-regex) that require system headers not always present. + CGO_ENABLED=0 GOTOOLCHAIN="$go_toolchain" $GOLANGCI_LINT run --new-from-rev=HEAD --fix || exit 1 + + # Re-stage any auto-fixes from the linter + echo "$staged_go_files" | xargs git add fi -# --- BEGIN BEADS INTEGRATION v1.3.0 --- +# --- BEGIN BEADS INTEGRATION v1.3.2-rc.2 --- # This section is managed by beads. Do not remove these markers. if command -v bd >/dev/null 2>&1; then export BD_GIT_HOOK=1 @@ -46,7 +60,7 @@ if command -v bd >/dev/null 2>&1; then if command -v "$_bd_timeout_candidate" >/dev/null 2>&1; then if _bd_timeout_version="$("$_bd_timeout_candidate" --version 2>/dev/null)"; then case "$_bd_timeout_version" in - "timeout (GNU coreutils) "*|"timeout (uutils coreutils) "*) _bd_timeout_command=$_bd_timeout_candidate; break ;; + "timeout (GNU coreutils) "*) _bd_timeout_command=$_bd_timeout_candidate; break ;; esac fi fi @@ -83,4 +97,4 @@ if command -v bd >/dev/null 2>&1; then fi if [ "$_bd_exit" -ne 0 ]; then exit "$_bd_exit"; fi fi -# --- END BEADS INTEGRATION v1.3.0 --- +# --- END BEADS INTEGRATION v1.3.2-rc.2 --- diff --git a/.githooks/pre-push b/.githooks/pre-push index 4a20fb57d..2307fa2a0 100755 --- a/.githooks/pre-push +++ b/.githooks/pre-push @@ -1,10 +1,8 @@ #!/usr/bin/env bash -# Pre-push hook: run the Bazel test suite for branch pushes that change Go or -# Bazel inputs (scripts/pre-push-suite.sh), and when pushing a release tag -# (refs/tags/v*), refuse the push if version constants disagree. The version -# check stops a release from being tagged/published with drifted versions (the -# failure mode that left main red after the 1.0.5 release) before it ever -# reaches CI. +# Pre-push hook: when pushing a release tag (refs/tags/v*), refuse the push if +# version constants disagree. This stops a release from being tagged/published +# with drifted versions (the failure mode that left main red after the 1.0.5 +# release) before it ever reaches CI. # # Installed via: git config core.hooksPath .githooks # See also: make install (sets hooksPath automatically) @@ -28,71 +26,20 @@ if [ "$pushing_version_tag" -eq 1 ]; then repo_root="$(git rev-parse --show-toplevel)" # Skip on older checkouts without the consistency check; do not block. if [ -x "$repo_root/scripts/check-versions.sh" ]; then - while read -r local_ref _local_sha _remote_ref _remote_sha; do - case "$local_ref" in - refs/tags/v[0-9]*) - tag_version="${local_ref#refs/tags/v}" - echo "Release tag v$tag_version push detected — verifying version consistency..." - checker_status=0 - (cd "$repo_root" && ./scripts/check-versions.sh --expect "$tag_version") || checker_status=$? - if [ "$checker_status" -ne 0 ]; then - echo "" - if [ "$checker_status" -eq 1 ]; then - echo "❌ Refusing to push release tag v$tag_version: versions are inconsistent." >&2 - echo " See the checker output above for what is inconsistent and how to fix it." >&2 - elif [ "$checker_status" -eq 3 ]; then - echo "❌ Refusing to push release tag v$tag_version: the tag does not match this checkout's release version." >&2 - echo " The release surfaces agree with each other, so do NOT re-run update-versions.sh." >&2 - echo " Push only the tag matching this checkout (see CONTRIBUTING.md)." >&2 - else - echo "❌ Refusing to push release tag v$tag_version: version checker unavailable or failed (exit $checker_status)." >&2 - echo " Check the Go toolchain and checker diagnostics above." >&2 - fi - echo " Override with --no-verify only if you know what you are doing." >&2 - exit 1 - fi - ;; - esac - done <<< "$push_refs" + echo "Release tag push detected — verifying version consistency..." + if ! (cd "$repo_root" && ./scripts/check-versions.sh); then + echo "" + echo "❌ Refusing to push release tag: versions are inconsistent." >&2 + echo " Fix with: scripts/update-versions.sh " >&2 + echo " Override with --no-verify only if you know what you are doing." >&2 + exit 1 + fi fi fi -# The push-time test suite (scripts/pre-push-suite.sh): bazel.yml's test lane, -# `bazel test //... --config=ci`, for branch pushes whose new commits change -# Go or Bazel inputs. A tag push, a deletion, or a docs-only push skips it. -# BD_PREPUSH_SUITE=go runs plain go test instead, under a banner. -# -# The decision keys on the remote ref and the shas, never on the local ref's -# shape: for `git push origin HEAD` and `HEAD:refs/heads/x` git passes the -# literal local ref `HEAD`, which a `refs/heads/*` match would skip (#7351). -zero_sha=0000000000000000000000000000000000000000 -suite_needed=0 -while read -r _local_ref local_sha remote_ref remote_sha; do - case "$remote_ref" in - refs/tags/*) continue ;; - esac - if [ "$local_sha" = "$zero_sha" ] || ! git cat-file -e "$local_sha^{commit}" 2>/dev/null; then - continue - fi - base="$remote_sha" - if [ "$base" = "$zero_sha" ] || ! git cat-file -e "$base^{commit}" 2>/dev/null; then - # A new remote branch: compare against main's merge-base. - base="$(git merge-base "$local_sha" origin/main 2>/dev/null || true)" - fi - if [ -z "$base" ] || git diff --name-only "$base" "$local_sha" -- \ - '*.go' 'go.mod' 'go.sum' '*.bazel' '*.bzl' '.bazelrc' '.bazelversion' 'MODULE.bazel.lock' | grep -q .; then - suite_needed=1 - fi -done <<< "$push_refs" - -if [ "$suite_needed" -eq 1 ]; then - repo_root="$(git rev-parse --show-toplevel)" - "$repo_root/scripts/pre-push-suite.sh" /dev/null 2>&1; then export BD_GIT_HOOK=1 @@ -112,7 +59,7 @@ if command -v bd >/dev/null 2>&1; then if command -v "$_bd_timeout_candidate" >/dev/null 2>&1; then if _bd_timeout_version="$("$_bd_timeout_candidate" --version 2>/dev/null)"; then case "$_bd_timeout_version" in - "timeout (GNU coreutils) "*|"timeout (uutils coreutils) "*) _bd_timeout_command=$_bd_timeout_candidate; break ;; + "timeout (GNU coreutils) "*) _bd_timeout_command=$_bd_timeout_candidate; break ;; esac fi fi @@ -149,4 +96,4 @@ if command -v bd >/dev/null 2>&1; then fi if [ "$_bd_exit" -ne 0 ]; then exit "$_bd_exit"; fi fi -# --- END BEADS INTEGRATION v1.3.0 --- +# --- END BEADS INTEGRATION v1.3.2-rc.2 --- diff --git a/.githooks/prepare-commit-msg b/.githooks/prepare-commit-msg index 2afb7c19c..c4ea7884e 100755 --- a/.githooks/prepare-commit-msg +++ b/.githooks/prepare-commit-msg @@ -1,5 +1,5 @@ #!/usr/bin/env sh -# --- BEGIN BEADS INTEGRATION v1.3.0 --- +# --- BEGIN BEADS INTEGRATION v1.3.2-rc.2 --- # This section is managed by beads. Do not remove these markers. if command -v bd >/dev/null 2>&1; then export BD_GIT_HOOK=1 @@ -19,7 +19,7 @@ if command -v bd >/dev/null 2>&1; then if command -v "$_bd_timeout_candidate" >/dev/null 2>&1; then if _bd_timeout_version="$("$_bd_timeout_candidate" --version 2>/dev/null)"; then case "$_bd_timeout_version" in - "timeout (GNU coreutils) "*|"timeout (uutils coreutils) "*) _bd_timeout_command=$_bd_timeout_candidate; break ;; + "timeout (GNU coreutils) "*) _bd_timeout_command=$_bd_timeout_candidate; break ;; esac fi fi @@ -56,4 +56,4 @@ if command -v bd >/dev/null 2>&1; then fi if [ "$_bd_exit" -ne 0 ]; then exit "$_bd_exit"; fi fi -# --- END BEADS INTEGRATION v1.3.0 --- +# --- END BEADS INTEGRATION v1.3.2-rc.2 --- diff --git a/.github/ISSUE_TEMPLATE/bug_report.yml b/.github/ISSUE_TEMPLATE/bug_report.yml deleted file mode 100644 index ab0b6b7f5..000000000 --- a/.github/ISSUE_TEMPLATE/bug_report.yml +++ /dev/null @@ -1,94 +0,0 @@ -name: Bug report -description: Report broken behavior in bd. -title: "bug: " -body: - - type: markdown - attributes: - value: | - Thanks for the report. This issue is the record a fix PR links to, so the more evidence here, the better. You can open a fix PR right away; no approval is needed first. - - **Agents filling in this form:** answer each question only from evidence you have. If you cannot answer a question from the evidence, write exactly `NOT_ENOUGH_INFO` instead of guessing. - - - type: checkboxes - id: checks - attributes: - label: Before you continue - options: - - label: I searched existing issues and did not find a duplicate. - required: true - - label: I reproduced this with stock `bd`, without an orchestrator or agent framework in the loop. - required: false - - - type: textarea - id: summary - attributes: - label: Summary - description: What is broken, in one or two sentences. - validations: - required: true - - - type: textarea - id: repro - attributes: - label: Steps to reproduce - description: Exact commands, starting from a fresh directory where possible. - render: shell - validations: - required: true - - - type: textarea - id: expected - attributes: - label: Expected behavior - validations: - required: true - - - type: textarea - id: actual - attributes: - label: Actual behavior - description: Include the full error output. - validations: - required: true - - - type: input - id: version - attributes: - label: bd version - description: Output of `bd version`. - validations: - required: true - - - type: dropdown - id: mode - attributes: - label: Storage mode - options: - - Embedded (default `bd init`) - - Server mode (`bd init --server`) - - Proxied server - - Not sure - validations: - required: true - - - type: input - id: os - attributes: - label: Operating system - placeholder: "macOS 15.5 (arm64), Ubuntu 24.04, Windows 11, ..." - validations: - required: true - - - type: textarea - id: impact - attributes: - label: Impact - description: Who is affected, how severe it is (data loss, blocked, workaround exists), and how often it happens. - validations: - required: true - - - type: textarea - id: context - attributes: - label: Additional context - description: Logs, `bd doctor` output, root-cause notes, or a proposed fix. diff --git a/.github/ISSUE_TEMPLATE/config.yml b/.github/ISSUE_TEMPLATE/config.yml deleted file mode 100644 index f2904a2bb..000000000 --- a/.github/ISSUE_TEMPLATE/config.yml +++ /dev/null @@ -1,11 +0,0 @@ -blank_issues_enabled: false -contact_links: - - name: Documentation - url: https://beads.gascity.com/ - about: Installation, concepts, CLI reference, and troubleshooting. - - name: Questions and ideas - url: https://github.com/gastownhall/beads/discussions - about: Ask usage questions or float an idea before it is ready to be an issue. - - name: Security vulnerability - url: https://github.com/gastownhall/beads/security/policy - about: Report security issues privately, never in a public issue. diff --git a/.github/ISSUE_TEMPLATE/feature_request.yml b/.github/ISSUE_TEMPLATE/feature_request.yml deleted file mode 100644 index 5ab476fe9..000000000 --- a/.github/ISSUE_TEMPLATE/feature_request.yml +++ /dev/null @@ -1,108 +0,0 @@ -name: Feature request -description: Propose a change to bd, with the information needed to accept it. -title: "feat: " -body: - - type: markdown - attributes: - value: | - This form collects the context reviewers and future readers need for a change worth discussing. You can open your PR right after filing; no approval is needed first. Small, self-explanatory changes can skip the issue and go straight to a PR. Read the [project charter](https://github.com/gastownhall/beads/blob/main/engdocs/PROJECT_CHARTER.md) first — beads owns issue-tracking primitives, not orchestration policy or storage-engine behavior. - - **Agents filling in this form:** answer each question only from evidence you have. If you cannot answer a question from the evidence, write exactly `NOT_ENOUGH_INFO` instead of guessing. - - - type: checkboxes - id: checks - attributes: - label: Before you continue - options: - - label: I searched existing issues and pull requests and did not find a duplicate. - required: true - - - type: textarea - id: motivation - attributes: - label: Motivation - description: The problem, and who has it. Describe the pain, not the implementation you prefer. - validations: - required: true - - - type: textarea - id: proposal - attributes: - label: Proposed change - description: The behavior, command, flag, or API you want. - validations: - required: true - - - type: textarea - id: impact - attributes: - label: Impact - description: Who is affected and how — users, agents, integrations, downstream consumers of the `--json` output. - validations: - required: true - - - type: textarea - id: risk - attributes: - label: Risk and compatibility - description: Schema or migration changes, wire or `--json` output changes, behavior changes for existing workspaces, and how to roll back. - validations: - required: true - - - type: textarea - id: verification - attributes: - label: Verification plan - description: How we will know it works end to end, beyond unit tests. - validations: - required: true - - - type: textarea - id: alternatives - attributes: - label: Alternatives considered - description: Current workarounds, issue metadata, an integration or plugin, or other designs. - - - type: dropdown - id: size - attributes: - label: Estimated size - options: - - XS (under 50 lines) - - S (under 200 lines) - - M (under 500 lines) - - L (under 1,000 lines) - - XL (more than 1,000 lines; likely needs splitting) - - Not sure - validations: - required: true - - - type: dropdown - id: intent - attributes: - label: Intent - options: - - I am opening or will open a PR - - I'm reporting the need; someone else would implement it - validations: - required: true - - - type: dropdown - id: ai - attributes: - label: AI assistance - description: Was an AI agent used to draft this issue? - options: - - "No" - - Yes, partly - - Yes, mostly - validations: - required: true - - - type: checkboxes - id: accountability - attributes: - label: Accountability - options: - - label: A human has read this issue and stands behind it. - required: true diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md index 6a0be086c..540ff9950 100644 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -1,24 +1,16 @@ -Closes # +## What -## What changed + - +## Why -## Evidence it works + - +## Verification -## Checklist - -- [ ] The why is documented: in a linked issue (reproduction or motivation, impact, risk) or in this description. -- [ ] `make test` (the Bazel test lane, nogo lint included) and any other lane `engdocs/TESTING.md` selects pass locally. -- [ ] User-facing behavior changes are reflected in `docs/` or the generated CLI reference. -- [ ] If an invariant or rule changed, the owning `AGENTS.md` is updated. + diff --git a/.github/actions/setup-bazel/action.yml b/.github/actions/setup-bazel/action.yml deleted file mode 100644 index 497ad56ce..000000000 --- a/.github/actions/setup-bazel/action.yml +++ /dev/null @@ -1,158 +0,0 @@ -name: setup-bazel -description: > - Install a sha256-pinned Bazelisk, have it download the sha256-pinned Bazel - release .bazelversion names (never from the runner cache), restore the - runner Bazel cache (--repository_cache only, whose entries Bazel re-hashes - on use), - and write a CI bazelrc outside the workspace. When the caller passes all of - BAZEL_REMOTE_EXECUTOR, RBE_TLS_CERT and RBE_TLS_KEY in the step env, that rc - enables --config=remote-exec for every bazel command; with none of them the - job runs locally; with BAZEL_FORK_CACHE=true (and no secrets) it enables - the read-only fork cache (--config=fork-cache), and with BAZEL_RRC_READ=true - too (the remote repo contents cache readers) Bazel's remote repo contents - cache through rbe-cache while it answers; with BAZEL_FORK_REMOTE=true - (fork and Dependabot PRs, bazel.yml modes fork-ro/fork-rw; needs - RBE_FORK_PR and RBE_FORK_TIER) it gets a short-lived certificate from - rbe-fork-mint (fork-credential.sh, proving the run with a CSR artifact) and - enables remote execution on rbe-fork; a partial set, or two of these - together, fails. `bazel` on PATH is a wrapper that always passes the - generated rc. -outputs: - remote: - description: '"true" when remote execution is configured' - value: ${{ steps.rc.outputs.remote }} - cache: - description: '"true" when the read-only fork cache (rbe-cache) is configured' - value: ${{ steps.rc.outputs.cache }} - cache-restored: - description: '"true" when a runner Bazel cache was restored (any prefix match)' - value: ${{ steps.restore.outputs.cache-matched-key != '' }} - cache-hit: - description: '"true" when the cache for this .bazelversion and MODULE.bazel.lock was restored exactly' - value: ${{ steps.restore.outputs.cache-hit }} -runs: - using: composite - steps: - # Keyed on what determines its content; saved only by a push to main that - # missed the exact key (see the caller's "Save Bazel runner cache" step). - # A lock change restores the previous lock's entry, which still holds most - # of the downloads. It holds only the content-addressable repository cache, - # which Bazel checks against each download's sha256 on every hit; the - # unverified repo contents cache is off (write-bazelrc.sh) and the Bazel - # binary is not cached, so a poisoned entry (a cache written by an - # untrusted run) cannot change what executes. v3: v2 also held Bazelisk's - # download and the contents cache. v4: MODULE.bazel joins the key because - # the lock does not record non-registry repository rules (llvm_dist), so - # v3 kept restoring the 1.94 GB LLVM .tar.xz after the switch to the - # 149 MB slice. - - name: Restore Bazel runner cache - id: restore - uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 - with: - path: ${{ runner.temp }}/bazel-ci-cache - key: bazel-repo-v4-${{ runner.os }}-${{ hashFiles('.bazelversion', 'MODULE.bazel', 'MODULE.bazel.lock') }} - restore-keys: | - bazel-repo-v4-${{ runner.os }}- - - - name: Install Bazelisk - shell: bash - run: | - set -euo pipefail - version=v1.29.0 - case "$(uname -m)" in - x86_64) arch=amd64; sha=5a408715e932c0250d28bd84555f12edbf70117de42f9181691c736eacc4a992 ;; - aarch64 | arm64) arch=arm64; sha=e20e8b0f4f240091b7a55bf17b9398bd4f40ee70ae0208dff95dd4c445fb4010 ;; - *) echo "setup-bazel: unsupported arch $(uname -m)" >&2; exit 1 ;; - esac - curl -fsSL --retry 3 --max-time 120 -o "$RUNNER_TEMP/bazelisk" \ - "https://github.com/bazelbuild/bazelisk/releases/download/${version}/bazelisk-linux-${arch}" - echo "${sha} $RUNNER_TEMP/bazelisk" | sha256sum -c - - sudo install -m 755 "$RUNNER_TEMP/bazelisk" /usr/local/bin/bazelisk - rm -f "$RUNNER_TEMP/bazelisk" - # The Bazel release .bazelversion names, pinned like Bazelisk above - # (the release's bazel--linux-.sha256). Bazelisk verifies - # only fresh downloads, so its home is a fresh directory outside the - # runner cache: every job downloads and verifies Bazel. A .bazelversion - # bump adds its pair here (policy-tested). - bazel_version="$(tr -d '[:space:]' < .bazelversion)" - case "$bazel_version/$arch" in - 9.2.0/amd64) bazel_sha=7668a95db1250f12c40407251e4e203b4ec8bf39bc495d2f485b2d8c99048694 ;; - 9.2.0/arm64) bazel_sha=049dd21f40ad979db11c3ee68c96a42ce75f1185e69ac61ab20de1501427a410 ;; - 9.3.0/amd64) bazel_sha=d302d22ed77ee5658b87857249baa579db84300ecebe0a1e9a357b5b20f80eb8 ;; - 9.3.0/arm64) bazel_sha=615cc2509819047e4930fe6643c5f01ebd705ccadf75e8f0b85f9b83cc6a6ca0 ;; - *) echo "setup-bazel: no pinned sha256 for Bazel '$bazel_version' ($arch); add it from the release's .sha256 file" >&2; exit 1 ;; - esac - { - echo "BAZELISK_HOME=$RUNNER_TEMP/bazelisk-home" - echo "BAZELISK_VERIFY_SHA256=$bazel_sha" - echo "BAZEL_CI_BAZEL_SHA256=$bazel_sha" - } >> "$GITHUB_ENV" - - # Fork and Dependabot runs (no secrets, no OIDC): a key and a CSR, the CSR - # as an artifact of this run (the mint's proof that the request comes - # from a running job of it), then the certificate. The key stays in the - # secret dir; the CSR and the certificate are public. - # fork-credential.sh key does nothing (no outputs) unless the caller set - # BAZEL_FORK_REMOTE=true; the next two steps follow its csr output. - - name: rbe-fork key and CSR - id: fork-key - shell: bash - env: - BAZEL_CI_SECRET_DIR: ${{ runner.temp }}/bazel-ci-secret - run: bash "$GITHUB_ACTION_PATH/fork-credential.sh" key - - - name: rbe-fork CSR artifact (proof of this run) - id: fork-csr - if: steps.fork-key.outputs.csr != '' - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7 - with: - name: ${{ steps.fork-key.outputs.artifact-name }} - path: ${{ steps.fork-key.outputs.csr }} - retention-days: 1 - if-no-files-found: error - compression-level: 0 - - - name: rbe-fork certificate (rbe-fork-mint) - id: fork-cert - if: steps.fork-key.outputs.csr != '' - shell: bash - env: - BAZEL_CI_SECRET_DIR: ${{ runner.temp }}/bazel-ci-secret - ARTIFACT_ID: ${{ steps.fork-csr.outputs.artifact-id }} - run: bash "$GITHUB_ACTION_PATH/fork-credential.sh" cert - - - name: Write CI bazelrc - id: rc - shell: bash - env: - BAZEL_CI_CACHE_DIR: ${{ runner.temp }}/bazel-ci-cache - BAZEL_CI_SECRET_DIR: ${{ runner.temp }}/bazel-ci-secret - RBE_FORK_CERT_FILE: ${{ steps.fork-cert.outputs.cert }} - RBE_FORK_KEY_FILE: ${{ steps.fork-cert.outputs.key }} - RBE_FORK_ENDPOINT: ${{ steps.fork-cert.outputs.endpoint }} - RBE_FORK_INSTANCE: ${{ steps.fork-cert.outputs.instance }} - run: bash "$GITHUB_ACTION_PATH/write-bazelrc.sh" - - - name: Install bazel wrapper - shell: bash - env: - RC: ${{ steps.rc.outputs.rc }} - run: | - set -euo pipefail - printf '%s\n' '#!/usr/bin/env bash' "exec /usr/local/bin/bazelisk --bazelrc=\"$RC\" \"\$@\"" > "$RUNNER_TEMP/bazel-wrapper" - sudo install -m 755 "$RUNNER_TEMP/bazel-wrapper" /usr/local/bin/bazel - rm -f "$RUNNER_TEMP/bazel-wrapper" - # Downloads Bazel (Bazelisk checks BAZELISK_VERIFY_SHA256; the - # wrapper's --bazelrc would make --version an unknown startup - # option), then checks again: every Bazel binary in Bazelisk's home - # is the pinned release. - /usr/local/bin/bazelisk --version - n=0 - while IFS= read -r -d '' bin; do - echo "${BAZEL_CI_BAZEL_SHA256} $bin" | sha256sum -c - >/dev/null - n=$((n + 1)) - done < <(find "$BAZELISK_HOME/downloads" -type f -path '*/bin/bazel' -print0) - if [ "$n" -eq 0 ]; then - echo "setup-bazel: no Bazel binary under $BAZELISK_HOME" >&2 - exit 1 - fi diff --git a/.github/actions/setup-bazel/cache-rrc-probe.sh b/.github/actions/setup-bazel/cache-rrc-probe.sh deleted file mode 100644 index 1dc60a233..000000000 --- a/.github/actions/setup-bazel/cache-rrc-probe.sh +++ /dev/null @@ -1,151 +0,0 @@ -#!/usr/bin/env bash -# May this fork-cache lane read Bazel's remote repo contents cache through -# rbe-west's anonymous read-only cache (rbe-cache :8443, .bazelrc's -# fork-cache) right now? Exits 0 only when the kill switch below is on, -# rbe-cache answers a REAPI GetCapabilities for instance oss with gRPC status -# 0 and a ServerCapabilities that has cache_capabilities, and then answers a -# GetActionResult for a key no action has, and a ByteStream Read for a blob -# the CAS does not hold, each with NOT_FOUND (gRPC 5): the whole read chain -# (Caddy, nativelink-anon, the action cache and the CAS) is up. Anything else -# exits nonzero: switched off, an HTTP or gRPC error, a refused connection, -# a timeout, an answer that does not parse, no curl or python3. The caller -# then writes no repo contents cache lines, and never fails on it. The -# verdict carries the GetCapabilities TCP connect time without the DNS -# lookup (about one round trip to rbe-cache), for the lane's step summary. -# -# Why a probe: with the startup flag set and the cache unreachable (closed -# by rbe-west's kill switch, or down), Bazel retries every repository -# against it before fetching it itself (engdocs/design/ -# bazel-remote-repo-contents-cache.md, R6), which costs far more than the -# cache saves. Fork pull_request runs see no repository variables, so the -# opt-out is the committed line below and the server's own answer. -# -# Three gRPC calls over curl's HTTP/2, the first answer decoded by -# python3's standard library: no grpcurl, no new action. Nothing goes to -# stdout (callers write their rc from it); one verdict line goes to stderr. A copy lives in gascity -# (tools/rbe/) and beads (.github/actions/setup-bazel/); keep in sync, except -# the kill switch, which is each repository's own. -# -# Tests only: RBE_CACHE_PROBE_URL (a stand-in server) and -# RBE_CACHE_PROBE_MAX_TIME (seconds). -set -uo pipefail - -# Kill switch: off stops this repository's fork-cache lanes reading the -# remote repo contents cache (they fetch external repositories themselves), -# with a one-line pull request; on lets rbe-cache's answer decide. To stop -# every reader of rbe-cache, set it off in both copies (gascity and beads). -# It reaches a lane when the lane's checkout carries it. -fork_rrc_read=on - -url=${RBE_CACHE_PROBE_URL:-https://rbe-cache.ops.gascity.com:8443} -max_time=${RBE_CACHE_PROBE_MAX_TIME:-5} - -rtt= -no() { - echo "rbe-cache rrc probe: $*${rtt:+ ($rtt)}; this lane fetches external repositories itself" >&2 - exit 1 -} -[ "$fork_rrc_read" = on ] || no "switched off (fork_rrc_read=$fork_rrc_read in $(basename "$0"))" -command -v curl >/dev/null 2>&1 || no "no curl" -command -v python3 >/dev/null 2>&1 || no "no python3" -tmp=$(mktemp -d) || no "no temp dir" -trap 'rm -rf "$tmp"' EXIT - -# call WHAT METHOD REQ: one gRPC call (METHOD: service/method); the answer's -# messages go to $tmp/body, its grpc-status to $st, and the TCP connect time -# without the DNS lookup to $tc. Fails the probe (prefixed with WHAT) on a -# transport error or a non-200 answer. -call() { - local what=$1 out code dns - out=$(curl -sS --http2 --connect-timeout 3 --max-time "$max_time" \ - -H 'content-type: application/grpc' -H 'te: trailers' \ - --data-binary @"$3" -D "$tmp/head" -o "$tmp/body" -w '%{http_code} %{time_namelookup} %{time_connect}' \ - "$url/$2" 2>"$tmp/err") || - no "$what$(tr '\n' ' ' <"$tmp/err")" - read -r code dns tc <<<"$out" - tc=$(awk -v d="$dns" -v c="$tc" 'BEGIN { if (c > d) printf "%.6f", c - d }' 2>/dev/null) || tc= - [ "$code" = 200 ] || no "${what}HTTP $code" - # grpc-status is a trailer; curl writes trailers after the headers. - st=$(tr -d '\r' <"$tmp/head" | sed -n 's/^grpc-status: *//Ip' | tail -1) -} - -# GetCapabilitiesRequest{instance_name: "oss"} (fork-cache's -# --remote_instance_name) as one uncompressed gRPC message. -printf '\000\000\000\000\005\012\003oss' >"$tmp/req" || no "cannot write the request" -call "" build.bazel.remote.execution.v2.Capabilities/GetCapabilities "$tmp/req" -rtt=$(awk -v s="$tc" 'BEGIN { if (s > 0) printf "tcp connect %.1f ms", s * 1000 }' 2>/dev/null) || rtt= -[ "$st" = 0 ] || no "gRPC status ${st:-missing}" - -why=$(python3 -I - "$tmp/body" <<'PY' -import sys - - -def varint(b, i): - v = s = 0 - while True: - if i >= len(b) or s > 63: - raise ValueError("truncated varint") - c = b[i] - i += 1 - v |= (c & 0x7F) << s - s += 7 - if c < 0x80: - return v, i - - -def fields(b): - i = 0 - while i < len(b): - key, i = varint(b, i) - num, wire = key >> 3, key & 7 - if wire == 0: - v, i = varint(b, i) - elif wire == 1: - v, i = b[i:i + 8], i + 8 - elif wire == 2: - n, i = varint(b, i) - v, i = b[i:i + n], i + n - elif wire == 5: - v, i = b[i:i + 4], i + 4 - else: - raise ValueError("wire type %d" % wire) - if i > len(b): - raise ValueError("truncated field %d" % num) - yield num, wire, v - - -try: - body = open(sys.argv[1], "rb").read() - if len(body) < 5 or body[0] != 0: - raise ValueError("not an uncompressed gRPC message") - n = int.from_bytes(body[1:5], "big") - if len(body) != 5 + n: - raise ValueError("gRPC length %d, %d bytes" % (n, len(body) - 5)) - # ServerCapabilities.cache_capabilities - cache = [v for num, wire, v in fields(body[5:]) if num == 1 and wire == 2] -except (OSError, ValueError) as e: - print("unreadable answer (%s)" % e) - sys.exit(2) -if not cache: - print("no cache_capabilities in the answer") - sys.exit(1) -PY -) || no "${why:-python3 failed}" - -# GetActionResultRequest{instance_name: "oss", action_digest: {hash: -# sha256("cache-rrc-probe: no action has this key"), size_bytes: 1}}: a key -# no action has, so the action cache behind rbe-cache must say NOT_FOUND. -key=ad12d7a2f1dec188fc1f2c4f5535a4a65eb630fa2f309eca423695e6ec7abee7 -printf '\000\000\000\000\113\012\003oss\022\104\012\100%s\020\001' "$key" >"$tmp/req" || - no "cannot write the request" -call "GetActionResult: " build.bazel.remote.execution.v2.ActionCache/GetActionResult "$tmp/req" -[ "$st" = 5 ] || no "GetActionResult: gRPC status ${st:-missing}, want 5 (NOT_FOUND)" - -# ReadRequest{resource_name: "oss/blobs//1"}: a blob the CAS -# does not hold (its content would have to hash to that key), so the CAS -# behind rbe-cache must say NOT_FOUND. -printf '\000\000\000\000\116\012\114oss/blobs/%s/1' "$key" >"$tmp/req" || - no "cannot write the request" -call "ByteStream Read: " google.bytestream.ByteStream/Read "$tmp/req" -[ "$st" = 5 ] || no "ByteStream Read: gRPC status ${st:-missing}, want 5 (NOT_FOUND)" -echo "rbe-cache rrc probe: rbe-cache answers GetCapabilities, GetActionResult and ByteStream Read${rtt:+ ($rtt)}; this lane reads the remote repo contents cache" >&2 diff --git a/.github/actions/setup-bazel/cache-zstd-probe.sh b/.github/actions/setup-bazel/cache-zstd-probe.sh deleted file mode 100644 index 4e74368eb..000000000 --- a/.github/actions/setup-bazel/cache-zstd-probe.sh +++ /dev/null @@ -1,119 +0,0 @@ -#!/usr/bin/env bash -# Does rbe-west's anonymous read-only cache (rbe-cache :8443, .bazelrc's -# fork-cache) advertise zstd right now? Exits 0 only when its REAPI -# GetCapabilities answer lists ZSTD in cache_capabilities.supported_compressors, -# the field Bazel checks before it accepts --remote_cache_compression (it -# refuses a remote that does not list it). Anything else exits nonzero: not -# listed, an HTTP or gRPC error, a refused connection, a timeout, an answer -# that does not parse, no curl or python3. The caller then leaves the flag -# off, and never fails on it. -# -# So the server decides, at run time: when rbe-west turns zstd on, fork-cache -# runs use it; when it turns it off (rollback), they stop. No repository -# variable (fork pull_request runs see none) and no revert. -# -# One unary gRPC call over curl's HTTP/2, decoded by python3's standard -# library: no grpcurl, no new action. Nothing goes to stdout (callers write -# their rc from it); one verdict line goes to stderr. A byte copy lives in -# gascity (tools/rbe/) and beads (.github/actions/setup-bazel/); keep in sync. -# -# Tests only: RBE_CACHE_PROBE_URL (a stand-in server) and -# RBE_CACHE_PROBE_MAX_TIME (seconds). -set -uo pipefail -url=${RBE_CACHE_PROBE_URL:-https://rbe-cache.ops.gascity.com:8443} -max_time=${RBE_CACHE_PROBE_MAX_TIME:-5} - -no() { - echo "rbe-cache zstd probe: $*; the fork cache stays identity" >&2 - exit 1 -} -command -v curl >/dev/null 2>&1 || no "no curl" -command -v python3 >/dev/null 2>&1 || no "no python3" -tmp=$(mktemp -d) || no "no temp dir" -trap 'rm -rf "$tmp"' EXIT - -# GetCapabilitiesRequest{instance_name: "oss"} (fork-cache's -# --remote_instance_name) as one uncompressed gRPC message. -printf '\000\000\000\000\005\012\003oss' >"$tmp/req" || no "cannot write the request" -code=$(curl -sS --http2 --connect-timeout 3 --max-time "$max_time" \ - -H 'content-type: application/grpc' -H 'te: trailers' \ - --data-binary @"$tmp/req" -D "$tmp/head" -o "$tmp/body" -w '%{http_code}' \ - "$url/build.bazel.remote.execution.v2.Capabilities/GetCapabilities" 2>"$tmp/err") || - no "$(tr '\n' ' ' <"$tmp/err")" -[ "$code" = 200 ] || no "HTTP $code" -# grpc-status is a trailer; curl writes trailers after the headers. -st=$(tr -d '\r' <"$tmp/head" | sed -n 's/^grpc-status: *//Ip' | tail -1) -[ "$st" = 0 ] || no "gRPC status ${st:-missing}" - -why=$(python3 -I - "$tmp/body" <<'PY' -import sys - -ZSTD = 1 # build.bazel.remote.execution.v2.Compressor.Value.ZSTD - - -def varint(b, i): - v = s = 0 - while True: - if i >= len(b) or s > 63: - raise ValueError("truncated varint") - c = b[i] - i += 1 - v |= (c & 0x7F) << s - s += 7 - if c < 0x80: - return v, i - - -def fields(b): - i = 0 - while i < len(b): - key, i = varint(b, i) - num, wire = key >> 3, key & 7 - if wire == 0: - v, i = varint(b, i) - elif wire == 1: - v, i = b[i:i + 8], i + 8 - elif wire == 2: - n, i = varint(b, i) - v, i = b[i:i + n], i + n - elif wire == 5: - v, i = b[i:i + 4], i + 4 - else: - raise ValueError("wire type %d" % wire) - if i > len(b): - raise ValueError("truncated field %d" % num) - yield num, wire, v - - -try: - body = open(sys.argv[1], "rb").read() - if len(body) < 5 or body[0] != 0: - raise ValueError("not an uncompressed gRPC message") - n = int.from_bytes(body[1:5], "big") - if len(body) != 5 + n: - raise ValueError("gRPC length %d, %d bytes" % (n, len(body) - 5)) - compressors = [] - for num, wire, v in fields(body[5:]): - if num != 1 or wire != 2: # ServerCapabilities.cache_capabilities - continue - for cnum, cwire, cv in fields(v): - if cnum != 6: # CacheCapabilities.supported_compressors - continue - if cwire == 0: - compressors.append(cv) - elif cwire == 2: # packed - j = 0 - while j < len(cv): - x, j = varint(cv, j) - compressors.append(x) -except (OSError, ValueError) as e: - print("unreadable answer (%s)" % e) - sys.exit(2) -if ZSTD in compressors: - print("advertises zstd") - sys.exit(0) -print("zstd not advertised (supported_compressors %s)" % compressors) -sys.exit(1) -PY -) || no "${why:-python3 failed}" -echo "rbe-cache zstd probe: $why; the fork cache uses zstd" >&2 diff --git a/.github/actions/setup-bazel/fork-credential.sh b/.github/actions/setup-bazel/fork-credential.sh deleted file mode 100644 index 442bd7641..000000000 --- a/.github/actions/setup-bazel/fork-credential.sh +++ /dev/null @@ -1,91 +0,0 @@ -#!/usr/bin/env bash -# setup-bazel: a short-lived rbe-west client certificate for fork and -# Dependabot pull_request runs, which get no secrets and no OIDC token -# (infra nativelink-cas/west README "rbe-fork"). Two steps around the CSR -# artifact upload (action.yml): -# -# fork-credential.sh key an EC P-256 key in BAZEL_CI_SECRET_DIR (outside the -# workspace; PKCS#8: Bazel's Netty TLS refuses a -# SEC1 "EC PRIVATE KEY"), and a CSR for the upload. -# Outputs: csr (path), artifact-name. -# fork-credential.sh cert asks rbe-fork-mint for a certificate naming the -# uploaded artifact (ARTIFACT_ID). Outputs: cert, -# key, endpoint, instance, tier. -# -# The mint signs only for an in-progress pull_request run of this repository -# whose artifact holds this CSR (only code running in the run can upload one), -# at the head of RBE_FORK_PR. It decides the tier from the PR's author and the -# run's triggering actor; RBE_FORK_TIER is what the rbe job's status probe -# said, and a different answer fails the job (re-run all jobs). The key never -# leaves the runner; the certificate is public. Nothing here is secret enough -# to mask, but the key file is 0600 like the CI key. -set -euo pipefail -MINT=${RBE_FORK_MINT:-https://rbe-mint.ops.gascity.com:8444} -ENDPOINT_RE=${RBE_FORK_ENDPOINT_RE:-'^grpcs://rbe-fork\.ops\.gascity\.com:8444$'} -dir=${BAZEL_CI_SECRET_DIR:?BAZEL_CI_SECRET_DIR is required} -out=${GITHUB_OUTPUT:-/dev/stdout} - -case "${1:-}" in -key) - # Only fork and Dependabot runs (bazel.yml modes fork-ro/fork-rw). - [ "${BAZEL_FORK_REMOTE:-}" = true ] || exit 0 - install -d -m 0700 "$dir" - csr_dir="${RUNNER_TEMP:?}/rbe-csr" - rm -rf "$csr_dir" && install -d -m 0700 "$csr_dir" - umask 077 - openssl genpkey -algorithm EC -pkeyopt ec_paramgen_curve:P-256 -out "$dir/fork.key" 2>/dev/null - openssl req -new -key "$dir/fork.key" -subj "/CN=rbe-fork-request" -out "$csr_dir/csr.pem" - job=${GITHUB_JOB:?} - [[ $job =~ ^[a-z0-9][a-z0-9_-]{0,39}$ ]] || { echo "setup-bazel: job id '$job' does not fit the mint's job pattern" >&2; exit 1; } - { - echo "csr=$csr_dir/csr.pem" - echo "artifact-name=rbe-csr-$job-${GITHUB_RUN_ATTEMPT:?}-$(openssl rand -hex 8)" - } >>"$out" - ;; -cert) - : "${ARTIFACT_ID:?}" "${RBE_FORK_PR:?}" "${RBE_FORK_TIER:?}" "${GITHUB_REPOSITORY:?}" "${GITHUB_RUN_ID:?}" - body=$(jq -cn --arg repo "${GITHUB_REPOSITORY#*/}" --argjson run "$GITHUB_RUN_ID" \ - --argjson attempt "${GITHUB_RUN_ATTEMPT:?}" --argjson pr "$RBE_FORK_PR" --arg job "${GITHUB_JOB:?}" \ - --argjson artifact "$ARTIFACT_ID" \ - '{repo: $repo, run_id: $run, run_attempt: $attempt, pr: $pr, job: $job, artifact_id: $artifact}') - reply="$dir/mint.json" - code=000 - for attempt in 1 2 3 4; do - # --connect-timeout: a closed gate drops the SYN; fail fast, not in 60 s. - code=$(curl -sS -o "$reply" -w '%{http_code}' --connect-timeout 5 --max-time 60 \ - -H 'content-type: application/json' --data "$body" "$MINT/v1/cert") || code=000 - # Retry what may pass on its own: a rate or live-certificate limit (429; - # the rates are per minute, the backoff sums to one), the mint could not - # reach GitHub (502), or the network (000). Everything else is an answer. - case "$code" in 429 | 502 | 000) ;; *) break ;; esac - [ "$attempt" -eq 4 ] || sleep $((attempt * 10)) - done - if [ "$code" != 200 ]; then - echo "::error title=rbe-fork mint refused (HTTP $code)::$(jq -r '.error // empty' "$reply" 2>/dev/null || true)" - exit 1 - fi - endpoint=$(jq -r .endpoint "$reply") - instance=$(jq -r .instance "$reply") - tier=$(jq -r .tier "$reply") - [[ $endpoint =~ $ENDPOINT_RE ]] || { echo "::error::rbe-fork mint returned endpoint '$endpoint'" >&2; exit 1; } - case "$tier/$instance" in ro/oss-fork | rw/oss) ;; *) echo "::error::rbe-fork mint returned tier '$tier' instance '$instance'" >&2; exit 1 ;; esac - if [ "$tier" != "$RBE_FORK_TIER" ]; then - echo "::error title=rbe-fork tier changed::the mint says '$tier', this run's mode says '$RBE_FORK_TIER' (allowlist changed?); re-run all jobs" - exit 1 - fi - umask 077 - jq -r .cert_pem "$reply" >"$dir/fork.crt" - openssl x509 -in "$dir/fork.crt" -noout -subject -enddate - { - echo "cert=$dir/fork.crt" - echo "key=$dir/fork.key" - echo "endpoint=$endpoint" - echo "instance=$instance" - echo "tier=$tier" - } >>"$out" - ;; -*) - echo "usage: $0 key|cert" >&2 - exit 2 - ;; -esac diff --git a/.github/actions/setup-bazel/write-bazelrc.sh b/.github/actions/setup-bazel/write-bazelrc.sh deleted file mode 100644 index 15671cd37..000000000 --- a/.github/actions/setup-bazel/write-bazelrc.sh +++ /dev/null @@ -1,288 +0,0 @@ -#!/usr/bin/env bash -# Write the CI bazelrc for .github/actions/setup-bazel. -# -# Always: --repository_cache under BAZEL_CI_CACHE_DIR (the directory the -# runner cache saves and restores; Bazel checks every hit's sha256) and no -# repo contents cache (--repo_contents_cache=: its extracted repos would be -# restored from the runner cache unverified). Never --disk_cache: remote runs have the -# remote cache, and a local run's disk cache (about 7 GB) would not fit next to -# its output base on a hosted runner. -# -# Remote execution only when all three of BAZEL_REMOTE_EXECUTOR, RBE_TLS_CERT -# and RBE_TLS_KEY are set (repo secrets; empty on fork PRs, so those runs stay -# local). Exactly one or two of them is a misconfiguration and fails. Optional: -# RBE_TLS_CA (base64 PEM CA bundle) and RBE_INSTANCE (instance name; a warning -# when empty, because the default instance may share its action cache with -# other repos on the same farm). Cert and key are base64-encoded PEM (raw PEM -# is accepted too). Every line of the decoded PEM is masked in the job log. -# -# CI uploads only remotely executed results (--noremote_upload_local_results): -# a job's locally executed actions (PR code included) never enter the shared -# action cache from here. Developer rcs keep Bazel's default. -# -# The rc and the key material go to BAZEL_CI_SECRET_DIR, which must be outside -# the workspace (tests that walk the checkout must never see an endpoint or a -# cert path) and outside BAZEL_CI_CACHE_DIR (the runner cache must never store -# credentials). The endpoint is masked in the job log. -# -# BAZEL_FORK_CACHE=true (bazel.yml mode "cache": rbe=cache dispatches, and -# fork/Dependabot runs while rbe-fork is closed) appends --config=fork-cache: -# .bazelrc's credential-free, read-only rbe-west cache. While that cache -# advertises zstd (cache-zstd-probe.sh asks its GetCapabilities; any failure -# means no) it also asks for zstd transfers (--remote_cache_compression). A -# probe, not a repository variable: fork pull_request runs see no vars. No -# other mode ever asks, because only the anonymous cache can advertise zstd, -# and Bazel refuses a remote that does not. With BAZEL_RRC_READ=true too -# (bazel.yml's remote repo contents cache readers alone: the jobs whose -# configs rrc-lane-commands.txt seeds), and while rbe-cache answers -# (cache-rrc-probe.sh, which also holds the committed kill switch), it reads -# Bazel's remote repo contents cache, as those jobs' trusted readers do: the -# trees rrc-seed wrote on main pushes, through rbe-cache's anonymous AC and -# CAS reads. The job's step summary says whether it reads, and why. -# -# RBE_FORK_CERT_FILE + RBE_FORK_KEY_FILE + RBE_FORK_ENDPOINT + RBE_FORK_INSTANCE -# (bazel.yml modes fork-ro/fork-rw: fork-credential.sh's outputs, a -# short-lived rbe-fork-mint certificate) enable --config=remote-exec against -# rbe-fork: instance oss-fork (the fork pool, nothing cached) or oss -# (allowlisted authors: the OSS pool, results cached by the workers). The -# three modes are mutually exclusive. -# -# Outputs (to $GITHUB_OUTPUT when set, else stdout): rc=, -# remote=true|false, cache=true|false. -set -euo pipefail - -cache_dir="${BAZEL_CI_CACHE_DIR:?BAZEL_CI_CACHE_DIR is required}" -secret_dir="${BAZEL_CI_SECRET_DIR:?BAZEL_CI_SECRET_DIR is required}" -workspace="${GITHUB_WORKSPACE:-$PWD}" - -abs() { mkdir -p "$1" && (cd "$1" && pwd -P); } -cache_dir="$(abs "$cache_dir")" -secret_dir="$(abs "$secret_dir")" -workspace="$(cd "$workspace" && pwd -P)" -case "$secret_dir/" in -"$workspace"/*) - echo "setup-bazel: BAZEL_CI_SECRET_DIR ($secret_dir) must be outside the workspace" >&2 - exit 1 - ;; -"$cache_dir"/*) - echo "setup-bazel: BAZEL_CI_SECRET_DIR must not be inside the cached BAZEL_CI_CACHE_DIR" >&2 - exit 1 - ;; -esac -chmod 700 "$secret_dir" -mkdir -p "$cache_dir/repo" - -mask() { - local v="${1:-}" - [[ -z "$v" ]] && return 0 - printf '::add-mask::%s\n' "$v" -} - -# Mask the endpoint and its bare host[:port]: Bazel prints bytestream:// URIs -# that carry the host without the scheme. -executor="${BAZEL_REMOTE_EXECUTOR:-}" -if [[ -n "$executor" ]]; then - mask "$executor" - hostport="${executor#*://}" - hostport="${hostport%%/*}" - mask "$hostport" - mask "${hostport%:*}" - # Bazel's connection errors print the resolved address ("host/1.2.3.4:443"). - if command -v getent >/dev/null 2>&1; then - while read -r addr _; do - mask "$addr" - done < <(getent ahosts "${hostport%:*}" 2>/dev/null | sort -u -k1,1) - fi -fi - -cert="${RBE_TLS_CERT:-}" -key="${RBE_TLS_KEY:-}" -ca="${RBE_TLS_CA:-}" -instance="${RBE_INSTANCE:-}" -fork_cache="${BAZEL_FORK_CACHE:-}" -rrc_reader="${BAZEL_RRC_READ:-}" -fork_cert="${RBE_FORK_CERT_FILE:-}" -fork_key="${RBE_FORK_KEY_FILE:-}" -fork_endpoint="${RBE_FORK_ENDPOINT:-}" -fork_instance="${RBE_FORK_INSTANCE:-}" -fork_fields=0 -for v in "$fork_cert" "$fork_key" "$fork_endpoint" "$fork_instance"; do - [[ -n "$v" ]] && fork_fields=$((fork_fields + 1)) -done -case "$fork_fields/$fork_instance" in -0/ | 4/oss | 4/oss-fork) ;; -*) - echo "setup-bazel: RBE_FORK_CERT_FILE, RBE_FORK_KEY_FILE, RBE_FORK_ENDPOINT and RBE_FORK_INSTANCE (oss or oss-fork) go together" >&2 - exit 1 - ;; -esac -case "$fork_cache" in -"" | true) ;; -*) - echo "setup-bazel: BAZEL_FORK_CACHE must be \"true\" or empty, got \"$fork_cache\"" >&2 - exit 1 - ;; -esac -case "$rrc_reader" in -"" | true) ;; -*) - echo "setup-bazel: BAZEL_RRC_READ must be \"true\" or empty, got \"$rrc_reader\"" >&2 - exit 1 - ;; -esac - -rc="$secret_dir/ci.bazelrc" -umask 077 -{ - echo "# Generated by .github/actions/setup-bazel; not a workspace file." - echo "common --repository_cache=$cache_dir/repo" - # The repo contents cache (default: under the repository cache, so in - # the runner cache) holds extracted repos Bazel never re-verifies; off. - echo "common --repo_contents_cache=" - # Every mode fetches external repositories on the runner. gazelle's - # go_repository fetches Go modules with the go command (fetch_repo), not - # Bazel's downloader, and the go command never retries: one TLS - # handshake timeout against proxy.golang.org failed a whole lane. With - # "|" the go command moves to the next entry on any error (a "," - # list only on 404/410): the proxy twice, then the module's origin. - # fetch_repo checks every module against its go.sum hash whatever the - # source. Key neutral: repository fetching only, no action reads it. - echo "common --repo_env=GOPROXY=https://proxy.golang.org|https://proxy.golang.org|direct" - # Bazel's own downloader (the Go SDK, http_archive) already retries; - # give slow TLS handshakes and reads twice the default timeouts. - echo "common --http_timeout_scaling=2.0" - # The client's JVM heap. Bazel's default caps it at 25% of RAM, about - # 2 GB on the 8 GB 2 vCPU runners every remote lane uses; //... analysis - # fits today, with little headroom. A startup option: key neutral, it only - # restarts the server. Each lane reports peak-heap-size to size this. - echo "startup --host_jvm_args=-Xmx4g" -} >"$rc" - -# write_pem DEST VALUE: decode base64 (or accept raw PEM) and check it is PEM. -write_pem() { - local dest="$1" body="$2" - body="$(printf '%s' "$body" | tr -d '\r')" - if [[ "$body" != *"-----BEGIN "* ]]; then - if ! body="$(printf '%s' "$body" | tr -d '\n ' | base64 -d 2>/dev/null)"; then - echo "setup-bazel: $(basename "$dest") secret is neither PEM nor base64 PEM" >&2 - return 1 - fi - fi - if [[ "$body" != *"-----BEGIN "* ]]; then - echo "setup-bazel: $(basename "$dest") secret does not decode to PEM" >&2 - return 1 - fi - # GitHub masks the secret as stored (base64), not what it decodes to. - local line - while IFS= read -r line; do - [[ -z "$line" || "$line" == -----* ]] || mask "$line" - done <<<"$body" - printf '%s\n' "$body" >"$dest" - chmod 600 "$dest" -} - -set_fields=0 -for v in "$executor" "$cert" "$key"; do - [[ -n "$v" ]] && set_fields=$((set_fields + 1)) -done - -remote=false -cache=false -if [[ "$fork_fields" -eq 4 ]]; then - if [[ "$set_fields" -ne 0 || "$fork_cache" == true ]]; then - echo "setup-bazel: the rbe-fork certificate, the remote execution secrets and BAZEL_FORK_CACHE are mutually exclusive" >&2 - exit 1 - fi - { - echo "build:remote-exec --remote_executor=$fork_endpoint" - echo "build:remote-exec --tls_client_certificate=$fork_cert" - echo "build:remote-exec --tls_client_key=$fork_key" - echo "build:remote-exec --remote_instance_name=$fork_instance" - echo "build:remote-exec --noremote_upload_local_results" - # The farm admits 128 connections per source IP (Blacksmith runners - # share addresses). Key-neutral, like everything in this rc. - echo "build:remote-exec --remote_max_connections=8" - echo "build --config=remote-exec" - } >>"$rc" - remote=true - echo "setup-bazel: remote execution on rbe-fork (instance $fork_instance, short-lived certificate)" -elif [[ "$set_fields" -eq 3 ]]; then - if [[ "$fork_cache" == true ]]; then - echo "setup-bazel: BAZEL_FORK_CACHE and the remote execution secrets are mutually exclusive" >&2 - exit 1 - fi - write_pem "$secret_dir/client.crt" "$cert" - write_pem "$secret_dir/client.key" "$key" - # Outside the rc block below: write_pem's mask commands go to the log. - if [[ -n "$ca" ]]; then - write_pem "$secret_dir/ca.crt" "$ca" - fi - { - echo "build:remote-exec --remote_executor=$executor" - echo "build:remote-exec --tls_client_certificate=$secret_dir/client.crt" - echo "build:remote-exec --tls_client_key=$secret_dir/client.key" - if [[ -n "$ca" ]]; then - echo "build:remote-exec --tls_certificate=$secret_dir/ca.crt" - fi - if [[ -n "$instance" ]]; then - echo "build:remote-exec --remote_instance_name=$instance" - fi - echo "build:remote-exec --noremote_upload_local_results" - # Every command in this job (test, run //:gazelle, query) uses the - # executor; the committed .bazelrc holds the generic remote-exec tuning. - echo "build --config=remote-exec" - } >>"$rc" - if [[ -z "$instance" ]]; then - echo "::warning title=RBE_INSTANCE not set::remote execution uses the executor's default instance, whose action cache other repos on the farm may share; set RBE_INSTANCE to a beads-only instance" - fi - remote=true - echo "setup-bazel: remote execution enabled" -elif [[ "$set_fields" -ne 0 ]]; then - echo "setup-bazel: remote execution is partially configured; BAZEL_REMOTE_EXECUTOR, RBE_TLS_CERT and RBE_TLS_KEY must be set together (or none, for local execution)" >&2 - exit 1 -elif [[ "$fork_cache" == true ]]; then - # The remote repo contents cache, read-only, for the jobs that opt in - # (BAZEL_RRC_READ: only their configs are seeded), while rbe-cache - # answers and the probe's kill switch (fork_rrc_read) is on. With the - # startup flag set and the cache unreachable, every repository first - # fails a lookup (gascity engdocs/design/bazel-remote-repo-contents-cache.md, - # R6), so a closed or down rbe-cache gets no lines, and a notice says - # why. That holds at probe time only: after loading, Bazel fetches the - # files lazily, and losing rbe-cache then fails the command (R4). Both - # lines are key neutral, as for the trusted readers; fork-cache uploads - # nothing, and rbe-cache refuses every write. - rrc_read=false - if [[ "$rrc_reader" != true ]]; then - why="not a remote repo contents cache reader (no BAZEL_RRC_READ)" - elif why=$(bash "$(dirname "${BASH_SOURCE[0]}")/cache-rrc-probe.sh" 2>&1); then - rrc_read=true - echo "$why" - else - echo "::notice title=No remote repo contents cache::$why" - fi - echo "Fork cache remote repo contents cache read: **$([[ "$rrc_read" == true ]] && echo on || echo off)** (${why#rbe-cache rrc probe: })" >>"${GITHUB_STEP_SUMMARY:-/dev/null}" - # zstd for the anonymous cache's transfers while it advertises zstd: - # transport only, so action keys are unchanged. The probes never fail - # this script; they only decide the lines. - { - echo "build --config=fork-cache" - if bash "$(dirname "${BASH_SOURCE[0]}")/cache-zstd-probe.sh"; then - echo "build:fork-cache --remote_cache_compression" - fi - if [[ "$rrc_read" == true ]]; then - echo "startup --experimental_remote_repo_contents_cache" - echo "common --loading_phase_threads=64" - fi - } >>"$rc" - cache=true - echo "setup-bazel: read-only remote cache (rbe-cache); executing locally" -else - echo "setup-bazel: no remote execution secrets; running locally" -fi - -out="${GITHUB_OUTPUT:-/dev/stdout}" -{ - echo "rc=$rc" - echo "remote=$remote" - echo "cache=$cache" -} >>"$out" diff --git a/.github/allowed-actions.json b/.github/allowed-actions.json deleted file mode 100644 index 0113bbd35..000000000 --- a/.github/allowed-actions.json +++ /dev/null @@ -1,17 +0,0 @@ -{ - "github_owned_allowed": true, - "verified_allowed": false, - "patterns_allowed": [ - "DeterminateSystems/determinate-nix-action@*", - "DeterminateSystems/nix-installer-action@*", - "anchore/sbom-action@*", - "codecov/codecov-action@*", - "cygwin/cygwin-install-action@*", - "docker/setup-buildx-action@*", - "dorny/paths-filter@*", - "goreleaser/goreleaser-action@*", - "msys2/setup-msys2@*", - "ossf/scorecard-action@*", - "peter-evans/create-pull-request@*" - ] -} diff --git a/.github/bazel-farm-allowlist.txt b/.github/bazel-farm-allowlist.txt deleted file mode 100644 index 0f70a5807..000000000 --- a/.github/bazel-farm-allowlist.txt +++ /dev/null @@ -1,21 +0,0 @@ -# GitHub users whose fork PRs get the Bazel remote-execution farm -# (.github/workflows/bazel-farm.yml). One numeric user id per line, the login -# as a comment; blank lines and # comments are ignored. Ids, not logins: a -# renamed account's old login can be registered by anyone. Look an id up -# with: gh api users/ --jq .id -# -# Read only from the base branch (the pull_request_target run's own commit), -# never from a PR: editing this file in a PR changes nothing until it merges. -# Both the PR author and the user who triggered the run (opener or pusher) -# must be listed, and the PR must come from the author's own fork. A listed -# user gets same-repo-PR-level trust: its PR code runs with the RBE client -# certificate. Same people as gascity's .github/blacksmith-allowlist.txt. -# rbe-west's rbe-fork-mint reads this file too, from the default branch, by -# the same rules: a listed author's own-fork PR, triggered by a listed user, -# gets rbe-fork tier rw (bazel.yml mode fork-rw: the trusted OSS pool) when -# the farm has rw switched on; everyone else gets tier ro (fork pool). A -# line that is not a numeric id makes the mint treat the list as empty. -8082291 # julianknutsen -91582 # quad341 -36544495 # sjarmak -2568253 # csells diff --git a/.github/copilot-instructions.md b/.github/copilot-instructions.md index 7cd6744a5..00a9b016f 100644 --- a/.github/copilot-instructions.md +++ b/.github/copilot-instructions.md @@ -1,15 +1,138 @@ -# GitHub Copilot instructions for beads +# GitHub Copilot Instructions for Beads -The canonical contributor instructions for this repository are in -[AGENTS.md](../AGENTS.md); area-specific rules are in the nested `AGENTS.md` -files listed in its routing table. Read those, not this file, for workflow, -build, test, and scope rules. +## Project Overview -The rules most often missed in review: +**beads** (command: `bd`) is a Dolt-powered issue tracker designed for AI-supervised coding workflows. Git integration is optional. We dogfood our own tool for all task tracking. -- A PR must close a documented GitHub issue (filed before or alongside it). -- Build with `make install`, never `go build -o bd ./cmd/bd` or `go install ./cmd/bd`. -- `make ci-pr-lint` must pass with zero issues; choose tests with - [engdocs/TESTING.md](../engdocs/TESTING.md). -- Never create test issues in a production database; use `t.TempDir()`. -- CLI output uses small Unicode status symbols, never emoji icons. +**Key Features:** +- Dependency-aware issue tracking +- Auto-sync via Dolt-native replication +- AI-optimized CLI with JSON output +- Dolt server mode for background operations +- MCP server integration for Claude and other AI assistants + +## Tech Stack + +- **Language**: Go 1.21+ +- **Storage**: Dolt (version-controlled SQL database) +- **CLI Framework**: Cobra +- **Testing**: Go standard testing + table-driven tests +- **CI/CD**: GitHub Actions +- **MCP Server**: Python (integrations/beads-mcp/) + +## Coding Guidelines + +### Testing +- Always write tests for new features +- Use `t.TempDir() in Go tests` to avoid polluting production database +- Run `go test -short ./...` before committing +- Never create test issues in production DB (use temporary DB) + +### Code Style +- Run `make ci-pr-lint` before committing changes to Go or lint-controlled files +- Follow existing patterns in `cmd/bd/` for new commands +- Add `--json` flag to all commands for programmatic use +- Update docs when changing behavior + +### Git Workflow +- Install git hooks: `bd hooks install` +- Use `bd dolt push` / `bd dolt pull` for remote sync +- Before implementing related work, opening a PR, or merging/closing a PR, run: + `scripts/pr-preflight.sh --search "" --repo gastownhall/beads` or + `scripts/pr-preflight.sh --repo gastownhall/beads` +- External contributor PRs have priority: build on them when possible, preserve + tests and attribution, and never close or replace them silently. + +## Issue Tracking with bd + +**CRITICAL**: This project uses **bd** for ALL task tracking. Do NOT create markdown TODO lists. + +### Essential Commands + +```bash +# Find work +bd ready --json # Unblocked issues +bd stale --days 30 --json # Forgotten issues + +# Create and manage (ALWAYS include --description) +bd create "Title" --description="Detailed context" -t bug|feature|task -p 0-4 --json +bd update --claim --json +bd close --reason "Done" --json + +# Search +bd list --status open --priority 1 --json +bd show --json + +# Sync (if remote configured) +bd dolt push # Push to Dolt remote +bd dolt pull # Pull from Dolt remote +``` + +### Workflow + +1. **Check ready work**: `bd ready --json` +2. **Claim task**: `bd update --claim` +3. **Work on it**: Implement, test, document +4. **Discover new work?** `bd create "Found bug" --description="What was found and why" -p 1 --deps discovered-from: --json` +5. **Complete**: `bd close --reason "Done" --json` +6. **Sync**: `bd dolt push` (push to Dolt remote if configured) + +**IMPORTANT**: Always include `--description` when creating issues. Issues without descriptions lack context for future work. + +### Priorities + +- `0` - Critical (security, data loss, broken builds) +- `1` - High (major features, important bugs) +- `2` - Medium (default, nice-to-have) +- `3` - Low (polish, optimization) +- `4` - Backlog (future ideas) + +## Project Structure + +``` +beads/ +├── cmd/bd/ # CLI commands (add new commands here) +├── internal/ +│ ├── types/ # Core data types +│ └── storage/ # Storage layer +│ └── dolt/ # Dolt implementation +├── integrations/ +│ └── beads-mcp/ # MCP server (Python) +├── examples/ # Integration examples +├── docs/ # Documentation +└── .beads/ + └── dolt/ # Dolt database (source of truth) +``` + +## Available Resources + +### MCP Server (Recommended) +Use the beads MCP server for native function calls instead of shell commands: +- Install: `pip install beads-mcp` +- Functions: `mcp__beads__ready()`, `mcp__beads__create()`, etc. +- See `integrations/beads-mcp/README.md` + +### Scripts +- `./scripts/bump-version.sh --commit` - Update all version files atomically +- `./scripts/release.sh ` - Complete release workflow +- `./scripts/update-homebrew.sh ` - Update Homebrew formula + +### Key Documentation +- **AGENTS.md** - Comprehensive AI agent guide (detailed workflows, advanced features) +- **AGENT_INSTRUCTIONS.md** - Development procedures, testing, releases +- **README.md** - User-facing documentation +- **docs/CLI_REFERENCE.md** - Complete command reference + +## Important Rules + +- ✅ Use bd for ALL task tracking +- ✅ Always use `--json` flag for programmatic use +- ✅ Use `bd dolt push` / `bd dolt pull` for remote sync +- ✅ Test with `t.TempDir() in Go tests` +- ❌ Do NOT create markdown TODO lists +- ❌ Do NOT create test issues in production DB +- ❌ Do NOT manually modify `.beads/dolt/` + +--- + +**For detailed workflows and advanced features, see [AGENTS.md](../AGENTS.md)** diff --git a/.github/scripts/bazel-farm-authorize.sh b/.github/scripts/bazel-farm-authorize.sh deleted file mode 100755 index 2c9ff356a..000000000 --- a/.github/scripts/bazel-farm-authorize.sh +++ /dev/null @@ -1,125 +0,0 @@ -#!/usr/bin/env bash -# bazel-farm.yml's authorize job: may this pull_request_target run hand a -# fork PR's head to bazel.yml with the RBE secrets? Runs from a checkout of -# the base branch (the run's own commit), never the PR; reads event facts -# from the environment only (the workflow never interpolates them into a -# script). Writes allowed=true|false to $GITHUB_OUTPUT and the reason to the -# log and step summary. Exits non-zero only on a broken invocation. -# -# Allowed only when all hold: -# EVENT_NAME is pull_request_target and ACTION is opened or synchronize -# (reopened and ready_for_review would re-test a head no listed user -# necessarily pushed: a fork collaborator's push while the PR was closed -# or a draft); -# BASE_REPO is REPOSITORY and HEAD_REPO is another repository (a fork); -# HEAD_OWNER_ID (the head repository owner's numeric id) is PR_AUTHOR_ID: -# the PR comes from the author's own fork, not from a third party's fork -# whose owner could push between the author's review and "Create"; -# BASE_REF is DEFAULT_BRANCH; -# HEAD_SHA is a full 40-hex commit id (what bazel.yml checks out); -# PR_AUTHOR_ID and SENDER_ID (numeric user ids) are both on ALLOWLIST. -# SENDER is who triggered this event: the pusher on synchronize, so a -# collaborator on a listed author's fork cannot push code that runs with -# secrets. Ids, not logins: a renamed account's old login can be -# registered by anyone. Logins (PR_AUTHOR, SENDER) are for the log only. -# -# ALLOWLIST: one numeric user id per line; # starts a comment (the login, -# for humans). Any other entry is a broken allowlist and fails the job. -# -# Inputs (environment): ALLOWLIST (file path), EVENT_NAME, ACTION, -# REPOSITORY, BASE_REPO, HEAD_REPO, HEAD_OWNER_ID, BASE_REF, DEFAULT_BRANCH, -# HEAD_SHA, PR_AUTHOR, PR_AUTHOR_ID, SENDER, SENDER_ID, GITHUB_OUTPUT; -# GITHUB_STEP_SUMMARY optional. - -set -euo pipefail -# Byte semantics for the [A-Za-z0-9] / [0-9] classes and lower(): in some -# UTF-8 locales they match or fold non-ASCII (e.g. the Kelvin sign to k). -export LC_ALL=C - -# ASCII lowercase. Not ${v,,}: that is bash >= 4, and macOS ships bash 3.2. -# shellcheck disable=SC2018,SC2019 # ASCII only, on purpose (logins, repo names) -lower() { - printf '%s' "${1:-}" | tr 'A-Z' 'a-z' -} - -: "${GITHUB_OUTPUT:?GITHUB_OUTPUT is not set}" -allowlist="${ALLOWLIST:?ALLOWLIST is not set}" -if [[ ! -f "$allowlist" ]]; then - echo "::error::allowlist $allowlist is missing" >&2 - exit 1 -fi - -decide() { - allowed=false - reason="$1" -} - -# A login: GitHub's charset, lowercased. Anything else never matches. -login() { - local v="${1:-}" - if [[ "$v" =~ ^[A-Za-z0-9]([A-Za-z0-9-]{0,38})$ ]]; then - lower "$v" - fi -} - -# A numeric GitHub user id; anything else is not one. -is_id() { - [[ "${1:-}" =~ ^[1-9][0-9]{0,19}$ ]] -} - -ids=() -while IFS= read -r line || [[ -n "$line" ]]; do - line="${line%%#*}" - line="${line//[[:space:]]/}" - [[ -n "$line" ]] || continue - if ! is_id "$line"; then - echo "::error::allowlist $allowlist has an entry that is not a numeric user id" >&2 - exit 1 - fi - ids+=("$line") -done < "$allowlist" - -listed() { - local want="${1:-}" id - is_id "$want" || return 1 - for id in "${ids[@]}"; do - if [[ "$id" == "$want" ]]; then - return 0 - fi - done - return 1 -} - -allowed=true -reason="author and sender are on the allowlist" -author="${PR_AUTHOR:-}" -sender="${SENDER:-}" -if [[ "${EVENT_NAME:-}" != pull_request_target ]]; then - decide "event is not pull_request_target" -elif ! [[ "${ACTION:-}" =~ ^(opened|synchronize)$ ]]; then - decide "action is not opened or synchronize" -elif [[ -z "${REPOSITORY:-}" || "${BASE_REPO:-}" != "$REPOSITORY" ]]; then - decide "PR base repository is not this repository" -elif [[ -z "${HEAD_REPO:-}" || "$(lower "$HEAD_REPO")" == "$(lower "${REPOSITORY:-}")" ]]; then - decide "PR head is not a fork (same-repo PRs use pr.yml's remote run)" -elif ! is_id "${HEAD_OWNER_ID:-}" || [[ "${HEAD_OWNER_ID}" != "${PR_AUTHOR_ID:-}" ]]; then - decide "PR head is not the author's own fork" -elif [[ -z "${DEFAULT_BRANCH:-}" || "${BASE_REF:-}" != "$DEFAULT_BRANCH" ]]; then - decide "PR does not target the default branch" -elif ! [[ "${HEAD_SHA:-}" =~ ^[0-9a-f]{40}$ ]]; then - decide "head SHA is not a full commit id" -elif ! listed "${PR_AUTHOR_ID:-}"; then - decide "PR author is not on the allowlist" -elif ! listed "${SENDER_ID:-}"; then - decide "the user who triggered this run is not on the allowlist" -fi - -# Logins are printed only after the charset check (log-injection safe). -shown_author="$(login "$author")" -shown_sender="$(login "$sender")" -echo "allowed=$allowed" >> "$GITHUB_OUTPUT" -msg="Bazel farm: allowed=$allowed ($reason; author ${shown_author:-}, sender ${shown_sender:-})" -echo "$msg" -if [[ -n "${GITHUB_STEP_SUMMARY:-}" ]]; then - echo "$msg" >> "$GITHUB_STEP_SUMMARY" -fi diff --git a/.github/scripts/bazel-gate.sh b/.github/scripts/bazel-gate.sh deleted file mode 100755 index 0101669b4..000000000 --- a/.github/scripts/bazel-gate.sh +++ /dev/null @@ -1,62 +0,0 @@ -#!/usr/bin/env bash -# pr.yml's ci-gate view of its bazel.yml call (the `bazel` job). bazel.yml's -# rbe job decides the execution mode once and exports it as the call's -# rbe-mode / rbe-enabled outputs; this script reads those outputs and never -# re-derives the decision (repo variable, fork, secrets) itself. -# -# bazel-gate.sh skips the Bazel gate ids whose job is skipped by design -# in this mode, for CI_GATE_SKIPPED_OK: -# skip: every lane and the aggregate (BAZEL) -# local: the remote-only BAZEL_EMBEDDED, -# BAZEL_PROXIED, BAZEL_SERVER_STORAGE, -# BAZEL_INTEGRATION and BAZEL_CMD_DOLT -# cache: BAZEL_EMBEDDED, BAZEL_PROXIED, -# BAZEL_SERVER_STORAGE (integration -# and cmd-dolt run with the read-only -# cache) -# remote, fork-ro, fork-rw: none -# bazel-gate.sh aggregate the value to gate on for BAZEL: the call's -# aggregate result, or, when the mode is missing -# or invalid (the rbe job failed, the call never -# started), an unexpected value that ci-gate.sh -# rejects. -# -# Any other skip, a failure, a cancellation, or a lane that should run but -# reported nothing (pr.yml maps an empty output to skipped) fails the gate. -# -# Inputs (environment): BAZEL_RBE_MODE, BAZEL_RBE_ENABLED (the call's -# rbe-mode / rbe-enabled outputs), BAZEL_CALL (needs.bazel.result). - -set -euo pipefail - -mode="${BAZEL_RBE_MODE:-}" -enabled="${BAZEL_RBE_ENABLED:-}" -valid=false -case "$mode/$enabled" in - remote/true | fork-ro/true | fork-rw/true | local/false | cache/false | skip/false) valid=true ;; -esac - -case "${1:-}" in - skips) - skips=() - if [[ "$valid" == true ]]; then - case "$mode" in - skip) skips+=(BAZEL BAZEL_TEST BAZEL_PURE BAZEL_RELEASE_CROSS BAZEL_EMBEDDED BAZEL_INTEGRATION BAZEL_DOLTSERVER BAZEL_PROXIED BAZEL_SERVER_STORAGE BAZEL_CMD_DOLT) ;; - local) skips+=(BAZEL_EMBEDDED BAZEL_INTEGRATION BAZEL_PROXIED BAZEL_SERVER_STORAGE BAZEL_CMD_DOLT) ;; - cache) skips+=(BAZEL_EMBEDDED BAZEL_PROXIED BAZEL_SERVER_STORAGE) ;; - esac - fi - echo "${skips[*]-}" - ;; - aggregate) - if [[ "$valid" != true ]]; then - echo "invalid-rbe-mode:$(printf '%s/%s' "$mode" "$enabled" | tr -cd 'a-z/')" - else - echo "${BAZEL_CALL:-}" - fi - ;; - *) - echo "usage: $0 skips|aggregate" >&2 - exit 2 - ;; -esac diff --git a/.github/scripts/ci-capability-selector.sh b/.github/scripts/ci-capability-selector.sh new file mode 100755 index 000000000..ca9624153 --- /dev/null +++ b/.github/scripts/ci-capability-selector.sh @@ -0,0 +1,163 @@ +#!/usr/bin/env bash +# Report candidate PR Risk capabilities without changing the current gates. + +set -euo pipefail + +embedded_cli=false +proxied_cli=false +embedded_storage=false +embedded_conformance=false +server_conformance=false +full=false +reason="" +diff_file="" + +cleanup() { + if [[ -n "$diff_file" ]]; then + rm -f "$diff_file" + fi +} +trap cleanup EXIT + +set_full() { + embedded_cli=true + proxied_cli=true + embedded_storage=true + embedded_conformance=true + server_conformance=true + full=true + reason="$1" +} + +emit() { + local lines=( + "embedded_cli=$embedded_cli" + "proxied_cli=$proxied_cli" + "embedded_storage=$embedded_storage" + "embedded_conformance=$embedded_conformance" + "server_conformance=$server_conformance" + "full=$full" + "reason=$reason" + ) + printf '%s\n' "${lines[@]}" + if [[ -n "${GITHUB_OUTPUT:-}" ]]; then + printf '%s\n' "${lines[@]}" >> "$GITHUB_OUTPUT" + fi +} + +event_name="${GITHUB_EVENT_NAME:-}" +base_sha="${PR_BASE_SHA:-}" +head_sha="${PR_HEAD_SHA:-}" +case "$event_name" in + pull_request) ;; + push) set_full "push"; emit; exit 0 ;; + merge_group) set_full "merge_group"; emit; exit 0 ;; + *) set_full "unsupported_event"; emit; exit 0 ;; +esac + +if [[ -z "$base_sha" || -z "$head_sha" ]]; then + set_full "missing_bounds" + emit + exit 0 +fi + +diff_file=$(mktemp) +if ! git diff --no-ext-diff --find-renames --name-status -z "$base_sha" "$head_sha" -- > "$diff_file"; then + set_full "diff_failed" + emit + exit 0 +fi + +saw_path=false +saw_cmd=false +saw_embedded=false +saw_server=false +saw_conformance=false + +while :; do + status="" + if ! IFS= read -r -d '' status <&3; then + if [[ -n "$status" ]]; then + set_full "malformed_record" + fi + break + fi + if [[ "$status" != "A" && "$status" != "M" ]]; then + set_full "unsafe_status" + break + fi + path="" + if ! IFS= read -r -d '' path <&3; then + set_full "malformed_record" + break + fi + saw_path=true + if [[ -z "$path" || "$path" == /* || "/$path/" == *"/../"* || "$path" == . || "$path" == .. || "$path" == ./* ]]; then + set_full "unsafe_path" + break + fi + case "$path" in + .github/workflows/*|.github/scripts/*) + set_full "unsafe_control" + break + ;; + .buildflags|go.mod|go.sum|Makefile|default.nix|flake.nix|flake.lock|overlay.nix|packages.nix) + set_full "build_input" + break + ;; + cmd/bd/*) + embedded_cli=true + proxied_cli=true + saw_cmd=true + ;; + internal/storage/embeddeddolt/*) + embedded_cli=true + embedded_storage=true + embedded_conformance=true + saw_embedded=true + ;; + internal/storage/dolt/*) + proxied_cli=true + server_conformance=true + saw_server=true + ;; + backend/conformance/*|test/conformance/*) + embedded_conformance=true + server_conformance=true + saw_conformance=true + ;; + internal/storage/*|backend/*|schema/*) + set_full "cross_cutting_storage" + break + ;; + docs/*.md|docs/*.mdx|docs/*.png|docs/*.jpg|docs/*.jpeg|docs/*.gif|docs/*.svg|docs/*.webp|docs/*.avif|engdocs/*.md|engdocs/*.mdx|engdocs/*.png|engdocs/*.jpg|engdocs/*.jpeg|engdocs/*.gif|engdocs/*.svg|engdocs/*.webp|engdocs/*.avif|README.md|CHANGELOG.md|ARTICLES.md|BENCHMARKS.md|NEWSLETTER.md) + ;; + *) + set_full "unowned_path" + break + ;; + esac +done 3< "$diff_file" + +if [[ "$full" != true ]]; then + if [[ "$embedded_cli" == true && "$proxied_cli" == true && "$embedded_storage" == true && "$embedded_conformance" == true && "$server_conformance" == true ]]; then + full=true + reason="full" + elif [[ "$saw_path" != true ]]; then + reason="empty_diff" + elif [[ "$saw_cmd" == true && "$saw_embedded" != true && "$saw_server" != true && "$saw_conformance" != true ]]; then + reason="cmd_bd" + elif [[ "$saw_embedded" == true && "$saw_cmd" != true && "$saw_server" != true && "$saw_conformance" != true ]]; then + reason="embedded_storage" + elif [[ "$saw_server" == true && "$saw_cmd" != true && "$saw_embedded" != true && "$saw_conformance" != true ]]; then + reason="server_storage" + elif [[ "$saw_conformance" == true && "$saw_cmd" != true && "$saw_embedded" != true && "$saw_server" != true ]]; then + reason="shared_conformance" + elif [[ "$saw_cmd" == false && "$saw_embedded" == false && "$saw_server" == false && "$saw_conformance" == false ]]; then + reason="docs_only" + else + reason="mixed" + fi +fi + +emit diff --git a/.github/scripts/ci-embedded-tier.sh b/.github/scripts/ci-embedded-tier.sh new file mode 100755 index 000000000..62c943059 --- /dev/null +++ b/.github/scripts/ci-embedded-tier.sh @@ -0,0 +1,103 @@ +#!/usr/bin/env bash +# Decide whether CI should run the full embedded Dolt build/storage/cmd matrix. + +set -euo pipefail + +event_name="${GITHUB_EVENT_NAME:-}" +base_sha="${PR_BASE_SHA:-}" +head_sha="${PR_HEAD_SHA:-}" + +full_embedded=false +reason="" +changed_files="" + +write_outputs() { + if [ -n "${GITHUB_OUTPUT:-}" ]; then + { + echo "full_embedded=$full_embedded" + echo "reason=$reason" + } >> "$GITHUB_OUTPUT" + fi +} + +case "$event_name" in + push) + full_embedded=true + reason="push to main runs full embedded Dolt coverage" + write_outputs + echo "$reason" + exit 0 + ;; + merge_group) + full_embedded=true + reason="merge queue runs full embedded Dolt coverage" + write_outputs + echo "$reason" + exit 0 + ;; +esac + +if [ "$event_name" != "pull_request" ]; then + full_embedded=true + reason="non-PR event runs full embedded Dolt coverage" + write_outputs + echo "$reason" + exit 0 +fi + +if [ -z "$base_sha" ] || [ -z "$head_sha" ]; then + full_embedded=true + reason="PR diff bounds unavailable; defaulting to full embedded Dolt coverage" + write_outputs + echo "$reason" + exit 0 +fi + +if ! changed_files="$(git diff --name-only "$base_sha" "$head_sha")"; then + full_embedded=true + reason="PR diff failed; defaulting to full embedded Dolt coverage" + write_outputs + echo "$reason" + exit 0 +fi + +if [ -z "$changed_files" ]; then + reason="no changed files detected; skipping embedded Dolt matrix" + write_outputs + echo "$reason" + exit 0 +fi + +while IFS= read -r path; do + case "$path" in + cmd/*|internal/*|tests/*|scripts/*|.github/scripts/*|.github/workflows/*) + full_embedded=true + reason="changed $path" + break + ;; + *.go|go.mod|go.sum|Makefile|default.nix|flake.nix|flake.lock|packages.nix) + full_embedded=true + reason="changed $path" + break + ;; + AGENTS.md|AGENT_INSTRUCTIONS.md|CONTRIBUTING.md|RELEASING.md) + full_embedded=true + reason="changed $path" + break + ;; + esac +done <<< "$changed_files" + +if [ "$full_embedded" = true ]; then + reason="risk path detected: $reason; running full embedded Dolt coverage" +else + reason="docs/metadata-only PR; skipping embedded Dolt storage/cmd matrix" +fi + +write_outputs + +echo "$reason" +echo "Changed files:" +while IFS= read -r path; do + printf ' %s\n' "$path" +done <<< "$changed_files" diff --git a/.github/scripts/embedded-cmd-test-shards.txt b/.github/scripts/embedded-cmd-test-shards.txt index 21e6f181d..57efd5f3d 100644 --- a/.github/scripts/embedded-cmd-test-shards.txt +++ b/.github/scripts/embedded-cmd-test-shards.txt @@ -1,545 +1,154 @@ -# Embedded-Dolt cmd/bd test shard manifest. +# Embedded cmd test shard manifest. # # Format: # -# 100-shard split for the Bazel-only embedded-Dolt cmd tier -# (bazel-embedded in .github/workflows/bazel.yml), bin-packed -# longest-first by measured wall-time from -# scripts/ci/embedded_cmd_test_durations.json (see that file for provenance -# and its "relative weight, not absolute SLA" caveat) onto the shard -# whose estimated wall time grows least: serial tests (no top-level -# t.Parallel) add up, parallel ones share -test.parallel=4. -# Regenerate with scripts/ci/gen_embedded_cmd_shard_manifest.py 100 -# --weights=duration --write after adding, splitting or removing -# TestEmbedded* functions in cmd/bd/*_test.go. --write is -# incremental: it keeps every already-assigned test on its current shard -# and only places newly-discovered names, onto whichever shard is -# currently lightest, so two unrelated PRs that each add one test do not -# textually conflict or silently invalidate each other's packing. Pass -# --repack to force a full from-scratch rebalance instead (a deliberate, -# separate change — expect a large diff). --check only verifies every -# discovered name is listed here exactly once (no stale or duplicate -# entries); it does NOT verify the packing is still well-balanced. -# -# This file is generated: notes added here are erased by the next -# regeneration. Add them to the generator or the durations file instead. - -100 1 TestEmbeddedUpdateConcurrent - -100 2 TestEmbeddedShowConcurrent - -100 3 TestEmbeddedCloseConcurrent - -100 4 TestEmbeddedEpicConcurrent - -100 5 TestEmbeddedConfigConcurrent - -100 6 TestEmbeddedListFilteringBasicAndLabels - -100 7 TestEmbeddedDepConcurrent - -100 8 TestEmbeddedMaxRowsListLimitInteraction - -100 9 TestEmbeddedChildrenConcurrent - -100 10 TestEmbeddedCreateScalarAndRelationships - -100 11 TestEmbeddedKVConcurrent - -100 12 TestEmbeddedGatedRendering - -100 13 TestEmbeddedListFilteringStatusAndHierarchy - -100 14 TestEmbeddedReopenConcurrent - -100 15 TestEmbeddedPurgeWispsPlaneRetention - -100 16 TestEmbeddedMemoryConcurrent - -100 17 TestEmbeddedReopen - -100 18 TestEmbeddedLabelPropagateRename -100 18 TestEmbeddedPrune_protectsBeadCitedByCustomStatusBead - -100 19 TestEmbeddedLabelConcurrent -100 19 TestEmbeddedNewDoltStore_StrictReadOnlyRefusesWritesOnFreshDatabase - -100 20 TestEmbeddedInitRoleRoutingRetainedFork -100 20 TestEmbeddedMigrateSchema_IdentityMismatchRefusesBeforeApplying - -100 21 TestEmbeddedMaxRowsReady -100 21 TestEmbeddedMigratePersonal_movesIssues - -100 22 TestEmbeddedCount -100 22 TestEmbeddedIfRevisionCloseReplaysMoleculeAutoClose - -100 23 TestEmbeddedCountConcurrent -100 23 TestEmbeddedExportExcludeOwner_flag - -100 24 TestEmbeddedEventsTailFollowReportsTruncationOnTheStream -100 24 TestEmbeddedUndeferConcurrent -100 24 TestEmbeddedWispGCProtectsActiveWisps - -100 25 TestEmbeddedDoltRemoteAdd_LocalOnly_FiresBeforeGuard -100 25 TestEmbeddedMaxRowsListBasic -100 25 TestEmbeddedRemovedBackendHealRejectionPerBackend - -100 26 TestEmbeddedDoltRemoteAdd_JSON_AllowGitOrigin_WarningOnStderr -100 26 TestEmbeddedExternalMutationPolicy -100 26 TestEmbeddedPersistentPreRunLogsAndSkipsIdentityCheckForFreshEmbeddedWorkspace - -100 27 TestEmbeddedDoltRemoteAdd_GitOriginCollision_RefusalMessageCopy -100 27 TestEmbeddedNewDoltStoreFromConfig_DottedDBName -100 27 TestEmbeddedShipConcurrent - -100 28 TestEmbeddedDoltLocalOnly_RemoteAdd_RefusesExitOne -100 28 TestEmbeddedHistoryConcurrent -100 28 TestEmbeddedImportChunkedRealStoreResumeConverges - -100 29 TestEmbeddedDoltLocalOnly_RemoteAdd_ErrorOnStderr -100 29 TestEmbeddedEpicCloseEligibleReasonFlag -100 29 TestEmbeddedImportChunkedNoReadyWindowWithReadinessEdgeIntoCycle - -100 30 TestEmbeddedCloseGuardsAndEpics -100 30 TestEmbeddedDoltLocalOnly_Push_ExitsZeroWithMessage -100 30 TestEmbeddedImportChunkedCrossBucketBlocksEdgesResumeBackfills - -100 31 TestEmbeddedDoltLocalOnly_ConfigRoundTrip -100 31 TestEmbeddedIfRevisionCloseWarnsOnForcedOpenChildren -100 31 TestEmbeddedInitSelectedExcludeRoutingForkInvalidQuiet - -100 32 TestEmbeddedDoctorUnsupportedJSON -100 32 TestEmbeddedIfRevisionAdvertisedOnFourVerbs -100 32 TestEmbeddedListSearchAndOutputFormats - -100 33 TestEmbeddedBootstrapJSONLAndGitOrigin -100 33 TestEmbeddedDeleteJournalsTheRequestingActor -100 33 TestEmbeddedExportExcludeOwner_verbose - -100 34 TestEmbeddedCopyIssueRelations_preservesCommentWithTimestamp -100 34 TestEmbeddedEventsTailReportsTruncationToTheCLI -100 34 TestEmbeddedUnclaimBatchAndRefusals -100 34 TestEmbeddedWispGCPurgeClosedDoesNotCascadeIntoLiveSteps - -100 35 TestEmbeddedAutoPruneRespectsBdConfigSet -100 35 TestEmbeddedEventsReadNoticesADisabledJournal -100 35 TestEmbeddedGateCreateBeadRename -100 35 TestEmbeddedVersionedHistoryEnvIsProcessWideAndOnlyTurnsRecordingOn - -100 36 TestEmbeddedAutoPruneLeavesAnUnboundedJournalAlone -100 36 TestEmbeddedDoltRemoteResetData_UnknownRemote -100 36 TestEmbeddedGateConcurrent -100 36 TestEmbeddedUnclaimIfAssigneeCLIDifferentSpelling - -100 37 TestEmbeddedAutoPruneBoundsTheJournalAfterAMutatingCommand -100 37 TestEmbeddedDoltRemoteResetData_CloudRemoteRefused -100 37 TestEmbeddedMaxRowsDepTreeAndDuplicates -100 37 TestEmbeddedSpawnMolecule_SubstitutesTemplatedStepFields - -100 38 TestEmbeddedGraphConcurrent -100 38 TestEmbeddedRoutedCreateJournalsIntoTheTargetWorkspace -100 38 TestEmbeddedUnclaimIfAssigneeFlagValidation - -100 39 TestEmbeddedIfRevisionRejectsLabelAndParentEdits -100 39 TestEmbeddedMaxRowsGraph -100 39 TestEmbeddedNewDoltStoreFromConfig_NoMetadata - -100 40 TestEmbeddedIfRevisionOutranksReassignFenceCLI -100 40 TestEmbeddedNewDoltStoreFromConfig_HyphenatedDBName -100 40 TestEmbeddedUpdateLifecycleClaimParentCommit - -100 41 TestEmbeddedDuplicateConcurrent -100 41 TestEmbeddedVersionedHistoryFollowsTheStoreItWritesInto - -100 42 TestEmbeddedInitAForkAutoContributor -100 42 TestEmbeddedUpdateIfGuardsFlagValidation - -100 43 TestEmbeddedIfRevisionMultiIDRejected -100 43 TestEmbeddedInitExplicitBeadsDirIsAuthoritative - -100 44 TestEmbeddedIfRevisionComposesWithIfAssigneeIfStatus -100 44 TestEmbeddedStaleConcurrent - -100 45 TestEmbeddedGraph -100 45 TestEmbeddedUpdateIfGuardsCLI - -100 46 TestEmbeddedCloseAlreadyClosedClaimNextAndMolecule -100 46 TestEmbeddedIfRevisionMatchAndMismatch - -100 47 TestEmbeddedListSearchAndOutputSearchAndSort -100 47 TestEmbeddedRevisionRemintMatrix - -100 48 TestEmbeddedDeferConcurrent -100 48 TestEmbeddedReassignFenceCLI - -100 49 TestEmbeddedBackupRestoreStrictReadonly -100 49 TestEmbeddedInitAPushConsentAndRemoteBootstrap -100 49 TestEmbeddedMigratePersonal_abortOnNoConfirm - -100 50 TestEmbeddedDiffConcurrent -100 50 TestEmbeddedDoltRemoteAdd_AllowGitOrigin_WarningOnStderr -100 50 TestEmbeddedImportChunkedWaitsForChildImportedLaterStaysBlocked -100 50 TestEmbeddedVersionOutputShape - -100 51 TestEmbeddedDoltRemoteAdd_AllowGitOrigin_Exits0 -100 51 TestEmbeddedHistoryAndShowAsOfReadDoltCommits -100 51 TestEmbeddedImportChunkedStaleRejectedRowKeepsDeferredDepsOut -100 51 TestEmbeddedInitGitBootstrapRoutingFreshQuiet - -100 52 TestEmbeddedConfigSetOtherKeysSkipTheVersionedHistoryScan -100 52 TestEmbeddedDoltLocalOnly_Push_LocalStoreCopy -100 52 TestEmbeddedImportChunkedNoReadyWindowAtAnyFreezePoint -100 52 TestEmbeddedSwarmConcurrent - -100 53 TestEmbeddedDoltLocalOnly_Push_JSON_DisabledStatus -100 53 TestEmbeddedIfRevisionOrdinaryCAS -100 53 TestEmbeddedImportChunkedMixedRegularWispCrossBucketEdgesWired -100 53 TestEmbeddedInitSelectedExcludeRoutingStealthDecoyQuiet - -100 54 TestEmbeddedDiscoveryPinsEnvFileReadonly -100 54 TestEmbeddedFormatJSONAliasMatchesJSON -100 54 TestEmbeddedGraphModeDependencyWorkspaceUnaffected -100 54 TestEmbeddedInitAgentArtifactsGolden -100 54 TestEmbeddedSearchConcurrent -100 54 TestEmbeddedShowAsOfFailureExitCodes - -100 55 TestEmbeddedDoltLocalOnly_Pull_JSON_DisabledStatus -100 55 TestEmbeddedImportChunkedConcurrentUpdateKeepsDeferredDeps -100 55 TestEmbeddedUndefer -100 55 TestEmbeddedVersionedHistorySwitchScansEveryRowNotOnePage - -100 56 TestEmbeddedDepNoCycleCheck -100 56 TestEmbeddedDoltLocalOnly_Pull_ExitsZeroWithMessage -100 56 TestEmbeddedImportChunkedConcurrentUpdateKeepsBlockingDeps -100 56 TestEmbeddedVersionedHistorySwitchRoundTrip - -100 57 TestEmbeddedDoltLocalOnly_DoltStatus_ShowsDisabled -100 57 TestEmbeddedIfRevisionDeletePreflightGoneIsPreconditionFailed -100 57 TestEmbeddedQueryConcurrent -100 57 TestEmbeddedVersionedHistorySwitchRefusesUnversionableMetadata - -100 58 TestEmbeddedDepAAddRejectionsAndBulk -100 58 TestEmbeddedDoctorUnsupportedProse -100 58 TestEmbeddedIfRevisionCascadeDelete -100 58 TestEmbeddedVersionedHistorySwitchRefusesAnUnrecordableGateTimeout - -100 59 TestEmbeddedDeferAutoWake -100 59 TestEmbeddedDoctorSupportedChecks -100 59 TestEmbeddedIdentityGateRendersSchemaSkewFromForwardDriftedWorkspace -100 59 TestEmbeddedVersionedHistorySetManyRefusesUnversionableMetadata - -100 60 TestEmbeddedCommentsCLI -100 60 TestEmbeddedCorruptMetadataFailsLoudUntilRestored -100 60 TestEmbeddedExportExcludeOwner_config -100 60 TestEmbeddedRoutedWriteMissDoesNotCreateTargetDatabase -100 60 TestEmbeddedWispGCPurgeClosedStillPurgesFullyClosedMolecules - -100 61 TestEmbeddedBootstrapCloneFailureScrubsCredentials -100 61 TestEmbeddedContributorRoutedWritesCommitInPlanning -100 61 TestEmbeddedEventsPruneHonorsRetainRowsFloorFromConfig -100 61 TestEmbeddedUpdateRoutedStoreCommitsTargetHead -100 61 TestEmbeddedVersionedHistoryConfigSetWritesTheStoreRow - -100 62 TestEmbeddedAutoPruneDoesNotRunForReadOnlyCommands -100 62 TestEmbeddedDoltRemoteResetData_GitBacked -100 62 TestEmbeddedGateCheckSkipsTemplateGates -100 62 TestEmbeddedReadyBlockersAndLabels -100 62 TestEmbeddedUnclaimIfAssigneeCLI - -100 63 TestEmbeddedDepAndLinkBatchAutoCommitDoesNotAdvanceHead -100 63 TestEmbeddedGateCheckLimitCountsOnlyLiveGates -100 63 TestEmbeddedIfRevisionDeletePreflightGoneParentIsPreconditionFailed -100 63 TestEmbeddedListPageEdges - -100 64 TestEmbeddedAutoExportExcludeOwner_config -100 64 TestEmbeddedDoltRemoteAdd_NoGitOrigin_ProceedsNormally -100 64 TestEmbeddedGateWaiterCheckLifecycle -100 64 TestEmbeddedPromoteMovesAWispParent -100 64 TestEmbeddedSpawnMolecule_PreservesStepLabels - -100 65 TestEmbeddedIfRevisionPreflightGoneIsPreconditionFailedOnEveryVerb -100 65 TestEmbeddedRemovedBackendHealRoundTrip -100 65 TestEmbeddedSupersede - -100 66 TestEmbeddedCloseBasicIsolatedStores -100 66 TestEmbeddedShowCommentsTail - -100 67 TestEmbeddedAdminCompactReadOnlyAllowed -100 67 TestEmbeddedAutoImportFallback_ConflictSkip_DoesNotClobber -100 67 TestEmbeddedBootstrapConcurrent -100 67 TestEmbeddedCountIncludeInfra -100 67 TestEmbeddedCreateCommitPending -100 67 TestEmbeddedCreateDryRunRepoDoesNotInitializeTarget -100 67 TestEmbeddedCreateRepoFromNonBeadsCwd -100 67 TestEmbeddedCreateRepoRelativeUninitRefused -100 67 TestEmbeddedHooks -100 67 TestEmbeddedOnboard -100 67 TestEmbeddedPrime -100 67 TestEmbeddedPromote -100 67 TestEmbeddedQuickstart -100 67 TestEmbeddedQuickstartConcurrent -100 67 TestEmbeddedWhere -100 67 TestEmbeddedWhereConcurrent - -100 68 TestEmbeddedAutoImportFallback_ConflictSkip_EmptyDBImportsAll -100 68 TestEmbeddedBlocked -100 68 TestEmbeddedCompactConcurrent -100 68 TestEmbeddedContext -100 68 TestEmbeddedCreateJSONEchoesSubSecondTimestamps -100 68 TestEmbeddedDoltBlockedAndCommit -100 68 TestEmbeddedDoltConcurrent -100 68 TestEmbeddedInfo -100 68 TestEmbeddedInfoSuppressGitHooks -100 68 TestEmbeddedOrphans -100 68 TestEmbeddedRoutedMutatingSiblingDeleteCloseDelete -100 68 TestEmbeddedSetupConcurrent -100 68 TestEmbeddedTodoConcurrent -100 68 TestEmbeddedTypes - -100 69 TestEmbeddedCompact -100 69 TestEmbeddedConfigValidateJSONNoWorkspaceWritesStdout -100 69 TestEmbeddedCreateConcurrent -100 69 TestEmbeddedDepBulkNoCycleCheckKeepsWholeGraphGate -100 69 TestEmbeddedDuplicatesSharedFixture -100 69 TestEmbeddedGateListScoped -100 69 TestEmbeddedGetDependencies -100 69 TestEmbeddedInfoConcurrent -100 69 TestEmbeddedRenamePrefixConcurrent -100 69 TestEmbeddedRoutedSiblingWritesCommitTargetHeadNote -100 69 TestEmbeddedTypesConcurrent -100 69 TestEmbeddedUpdateBatchAutoCommitDoesNotAdvanceHead - -100 70 TestEmbeddedExportConcurrent -100 70 TestEmbeddedFlattenConcurrent -100 70 TestEmbeddedGCDryRunAndSkips -100 70 TestEmbeddedImportConcurrent -100 70 TestEmbeddedImportUpsertAndPrefixSync -100 70 TestEmbeddedLabelAddRemoveMultiAndDuplicate -100 70 TestEmbeddedMemory -100 70 TestEmbeddedMigrateConcurrent -100 70 TestEmbeddedPurgeConcurrent -100 70 TestEmbeddedRoutedMutatingSiblingAssignTagDepTag - -100 71 TestEmbeddedCloseJSONReportsMoleculeAutoClose -100 71 TestEmbeddedInitRoleRoutingExplicitDefault -100 71 TestEmbeddedPrimeChangeDirRedirectNoticeFollowsTarget -100 71 TestEmbeddedRoutedCreateHonorsTheEnvOverrideOverTheTargetsConfig -100 71 TestEmbeddedSearchRoutesAgree - -100 72 TestEmbeddedContributorCreateMultiple -100 72 TestEmbeddedCreateWithGitRemote -100 72 TestEmbeddedFederationConcurrent -100 72 TestEmbeddedGateCreateRepoErrorsList -100 72 TestEmbeddedHumanConcurrent -100 72 TestEmbeddedImportPromotedWispRoundtrip -100 72 TestEmbeddedPrimeChangeDirPrimesTarget -100 72 TestEmbeddedRoutedMutatingSiblingDeleteCloseClose -100 72 TestEmbeddedRoutedSiblingWritesCommitTargetHeadReopen -100 72 TestEmbeddedUpdateFieldsMappingAndRefs - -100 73 TestEmbeddedCompactMisdatedChild -100 73 TestEmbeddedExportFlags -100 73 TestEmbeddedFederationPeers -100 73 TestEmbeddedGateCreateFlags -100 73 TestEmbeddedGateCreateReady -100 73 TestEmbeddedPrimeChangeDirDefaultTemplateMatchesTarget -100 73 TestEmbeddedQuickConcurrent -100 73 TestEmbeddedRenamePrefixBasic -100 73 TestEmbeddedRoutedMutatingSiblingAssignTagDepDepAdd - -100 74 TestEmbeddedBlockedConcurrent -100 74 TestEmbeddedBranch -100 74 TestEmbeddedCountIncludeEphemeral -100 74 TestEmbeddedDepAddWispSource -100 74 TestEmbeddedMigrate -100 74 TestEmbeddedPrimeConcurrent -100 74 TestEmbeddedRestore -100 74 TestEmbeddedVCConcurrent - -100 75 TestEmbeddedCommentsCLIConcurrent -100 75 TestEmbeddedContextConcurrent -100 75 TestEmbeddedFlatten -100 75 TestEmbeddedInitC -100 75 TestEmbeddedListComments -100 75 TestEmbeddedOpen_EmptyDatabaseRejected -100 75 TestEmbeddedReclaimRunsTheLeaseReclaimerRole -100 75 TestEmbeddedRoutedSiblingWritesCommitTargetHeadComment -100 75 TestEmbeddedVCStatusAndCommit - -100 76 TestEmbeddedCloseAlreadyClosedContinue -100 76 TestEmbeddedCloseAlreadyClosedMultipleIDs -100 76 TestEmbeddedExportFormats -100 76 TestEmbeddedGCForceWithDecay -100 76 TestEmbeddedGCFullAndNoForcePrompts -100 76 TestEmbeddedInitAGitOriginRemote -100 76 TestEmbeddedLabelAddRemoveRemove -100 76 TestEmbeddedOnboardConcurrent - -100 77 TestEmbeddedClaimPools -100 77 TestEmbeddedConfigNoWorkspaceStoreFreeCommands -100 77 TestEmbeddedCreateStorageClassConflicts -100 77 TestEmbeddedExternalCapabilityReadyAfterShip -100 77 TestEmbeddedHumanListStatsRespondDismiss -100 77 TestEmbeddedMemoryCorpusBudget -100 77 TestEmbeddedOrphansConcurrent -100 77 TestEmbeddedStatus - -100 78 TestEmbeddedBranchConcurrent -100 78 TestEmbeddedComments -100 78 TestEmbeddedDepBRemove -100 78 TestEmbeddedLabelAddRemoveAdd -100 78 TestEmbeddedShip -100 78 TestEmbeddedUpdateFieldsNotesOverwrite -100 78 TestEmbeddedVCCommitVariantsAndMerge -100 78 TestEmbeddedVersionConcurrent - -100 79 TestEmbeddedCreateCrossRepoUninit -100 79 TestEmbeddedDeleteJSONDependencyErrorContract -100 79 TestEmbeddedEpicCloseEligibleNotEligible -100 79 TestEmbeddedInitABasic -100 79 TestEmbeddedInitBRemoteSchemaAndCloneFailure -100 79 TestEmbeddedRenamePrefixEdgeCases -100 79 TestEmbeddedUpdateFieldsNotesClearAndDefer - -100 80 TestEmbeddedCreateCrossRepoWithParent -100 80 TestEmbeddedCreateFormCommitsLabelOnlyCreate -100 80 TestEmbeddedCreateStorageClassBasic -100 80 TestEmbeddedRoutedMutatingSiblingAssignTagDepAssign -100 80 TestEmbeddedStatusConcurrent -100 80 TestEmbeddedSwarmCreate -100 80 TestEmbeddedSwarmValidate - -100 81 TestEmbeddedAdminCompactTier2Honesty -100 81 TestEmbeddedBackup -100 81 TestEmbeddedDeleteForceAndBatch -100 81 TestEmbeddedGateCreateBeadCheck -100 81 TestEmbeddedGateList -100 81 TestEmbeddedRestoreConcurrent -100 81 TestEmbeddedStaleLabelFilters -100 81 TestEmbeddedStateConcurrent - -100 82 TestEmbeddedCreateCrossRepoDryRunWithParent -100 82 TestEmbeddedDeleteBasic -100 82 TestEmbeddedDepBTreeAndCycles -100 82 TestEmbeddedDirectCommitPathsBatchAutoCommitDoesNotAdvanceHead -100 82 TestEmbeddedHooksConcurrent -100 82 TestEmbeddedImportFromSources -100 82 TestEmbeddedStale - -100 83 TestEmbeddedAdminBlocked -100 83 TestEmbeddedAutoImportJSONLConcurrentWriter -100 83 TestEmbeddedInitBAutoCommitAndJSONLRemote -100 83 TestEmbeddedLabelEditReportsPrefix -100 83 TestEmbeddedMaxRowsDoctorAndConfig -100 83 TestEmbeddedPurgeFiltered -100 83 TestEmbeddedReadyConcurrent - -100 84 TestEmbeddedDoltRemoteAdd_GitOriginCollision_Exits1 -100 84 TestEmbeddedInitGitBootstrapRoutingDecoyInvalid -100 84 TestEmbeddedInitSelectedExcludeRoutingStealthInvalid -100 84 TestEmbeddedMigratePersonal_preservesComments - -100 85 TestEmbeddedBootstrapExistingDryRunInit -100 85 TestEmbeddedChildrenBasicAndEmpty -100 85 TestEmbeddedCreateGraphAndBulk -100 85 TestEmbeddedCreateRepoRelativeExistingWorkspaceUnaffected -100 85 TestEmbeddedIfRevisionReopenGuardDirect -100 85 TestEmbeddedInitArtifactRouting - -100 86 TestEmbeddedDoltRemoteAdd_DifferentURL_ProceedsNormally -100 86 TestEmbeddedExplicitHierarchicalParentEdges -100 86 TestEmbeddedInitHooksRouting -100 86 TestEmbeddedMigratePersonal_noopWhenEmpty - -100 87 TestEmbeddedCreateRepoProxiedTargetError -100 87 TestEmbeddedGateInheritsDownParentChain -100 87 TestEmbeddedReadyClaimConcurrent -100 87 TestEmbeddedState -100 87 TestEmbeddedSupersedeConcurrent -100 87 TestEmbeddedSwarmCreateForceAndErrors - -100 88 TestEmbeddedChangeDirOverridesInheritedBeadsDir -100 88 TestEmbeddedDepAAddAndList -100 88 TestEmbeddedKV -100 88 TestEmbeddedMaxRowsReadyMerge -100 88 TestEmbeddedPromoteCLI -100 88 TestEmbeddedUnclaimSuccess - -100 89 TestEmbeddedBackupConcurrent -100 89 TestEmbeddedCreateDryRunDoesNotMigrate -100 89 TestEmbeddedGateShowResolve -100 89 TestEmbeddedInitBStealthAndForceReinit -100 89 TestEmbeddedLabelEditReportsNoop -100 89 TestEmbeddedShowBasicsAndJSON - -100 90 TestEmbeddedAutoImportJSONLSkipsNonEmpty -100 90 TestEmbeddedConfig -100 90 TestEmbeddedFindDuplicatesConcurrent -100 90 TestEmbeddedLabelEditReportsListAndTemplate -100 90 TestEmbeddedLintConcurrent -100 90 TestEmbeddedUpdateLifecycleDefer -100 90 TestEmbeddedVersion - -100 91 TestEmbeddedDuplicate -100 91 TestEmbeddedPreviewDoesNotConsumeVersionMarker -100 91 TestEmbeddedPruneDryRunAndFilters -100 91 TestEmbeddedReadOnlyCommandsSpawnNoGit -100 91 TestEmbeddedShowUnresolvableDeps -100 91 TestEmbeddedTypesAgreeWithCreate - -100 92 TestEmbeddedAutoImportJSONLNoExplicitCommit -100 92 TestEmbeddedDefer -100 92 TestEmbeddedDepAAddTypesAndFlags -100 92 TestEmbeddedEpicCloseEligibleClosesEpics -100 92 TestEmbeddedSetup -100 92 TestEmbeddedSwarmStatusList - -100 93 TestEmbeddedCreateStorageClassBatchAndMisc -100 93 TestEmbeddedDeletePreviewAndGuards -100 93 TestEmbeddedDepBRemoveOutputAndRelatesTo -100 93 TestEmbeddedListConcurrent -100 93 TestEmbeddedPruneSafetyAndScope -100 93 TestEmbeddedShowDetailAndCurrentFlags - -100 94 TestEmbeddedCreateDryRunCrossRepoDoesNotMigrateTarget -100 94 TestEmbeddedDepListSingleIDWarnsOnDroppedExternalEdge -100 94 TestEmbeddedEpicCloseEligibleReasonOutput -100 94 TestEmbeddedFederationStatus -100 94 TestEmbeddedGCConcurrent -100 94 TestEmbeddedReadyDefaultJSONAndFlags - -100 95 TestEmbeddedChildrenJSONAndAllStatuses -100 95 TestEmbeddedFindDuplicates -100 95 TestEmbeddedImportClosedChildWhoseParentWasDeleted -100 95 TestEmbeddedInitConcurrent -100 95 TestEmbeddedQuick - -100 96 TestEmbeddedDiff -100 96 TestEmbeddedDuplicatesConcurrent -100 96 TestEmbeddedHistory -100 96 TestEmbeddedShowDetailAndCurrentHistoryAndCurrent -100 96 TestEmbeddedSquashWispMoleculeClearsRootEphemeral - -100 97 TestEmbeddedDuplicatesOwnWorkspaces -100 97 TestEmbeddedHumanPositionalFileAndGuards -100 97 TestEmbeddedPurgeBasics -100 97 TestEmbeddedQuietFlagSuppressesSuccessOutput -100 97 TestEmbeddedSearchStatusLimitSortDatesMetadata - -100 98 TestEmbeddedCloseBasicSharedStore -100 98 TestEmbeddedContributorCreateRoutesToPlanning -100 98 TestEmbeddedGCConditionalMatcherDecode -100 98 TestEmbeddedQuery -100 98 TestEmbeddedTodo - -100 99 TestEmbeddedBlockedLabelFilters -100 99 TestEmbeddedDoltRemoteAndPush -100 99 TestEmbeddedEpicStatus -100 99 TestEmbeddedInitSelectedExcludeRoutingForkDecoyAuto -100 99 TestEmbeddedSearchQueryStatusAssigneeLabels - -100 100 TestEmbeddedContributorCreateShowReadsRouted -100 100 TestEmbeddedCreateCrossRepo -100 100 TestEmbeddedInitGitBootstrapRoutingExistingExplicit -100 100 TestEmbeddedLint -100 100 TestEmbeddedPromoteCLIConcurrent +# There is no committed recent duration artifact for these tests. This manifest +# pins a conservative even split for the 20-shard CI matrix while +# embedded-test-shard.sh continues to hash-distribute newly-added tests that are +# not listed here. +20 1 TestEmbeddedBackup +20 2 TestEmbeddedBackupConcurrent +20 3 TestEmbeddedBlocked +20 4 TestEmbeddedBlockedConcurrent +20 5 TestEmbeddedBootstrap +20 6 TestEmbeddedBootstrapConcurrent +20 7 TestEmbeddedBranch +20 8 TestEmbeddedBranchConcurrent +20 9 TestEmbeddedChildren +20 10 TestEmbeddedChildrenConcurrent +20 11 TestEmbeddedClose +20 12 TestEmbeddedCloseConcurrent +20 13 TestEmbeddedComments +20 14 TestEmbeddedCommentsCLI +20 15 TestEmbeddedCommentsCLIConcurrent +20 16 TestEmbeddedCompact +20 17 TestEmbeddedCompactConcurrent +20 18 TestEmbeddedConfig +20 19 TestEmbeddedConfigConcurrent +20 20 TestEmbeddedConfigNoWorkspaceStoreFreeCommands +20 1 TestEmbeddedConfigValidateJSONNoWorkspaceWritesStdout +20 2 TestEmbeddedContext +20 3 TestEmbeddedContextConcurrent +20 4 TestEmbeddedContributorCreate +20 5 TestEmbeddedCount +20 6 TestEmbeddedCountConcurrent +20 7 TestEmbeddedCreate +20 8 TestEmbeddedCreateCommitPending +20 9 TestEmbeddedCreateConcurrent +20 10 TestEmbeddedCreateCrossRepo +20 11 TestEmbeddedCreateCrossRepoUninit +20 12 TestEmbeddedCreateCrossRepoWithParent +20 13 TestEmbeddedCreateWithGitRemote +20 14 TestEmbeddedDefer +20 15 TestEmbeddedDeferConcurrent +20 16 TestEmbeddedDelete +20 17 TestEmbeddedDep +20 18 TestEmbeddedDepConcurrent +20 19 TestEmbeddedDiff +20 20 TestEmbeddedDiffConcurrent +20 1 TestEmbeddedDolt +20 2 TestEmbeddedDoltConcurrent +20 3 TestEmbeddedDuplicate +20 4 TestEmbeddedDuplicateConcurrent +20 5 TestEmbeddedDuplicates +20 6 TestEmbeddedDuplicatesConcurrent +20 7 TestEmbeddedEpic +20 8 TestEmbeddedEpicConcurrent +20 9 TestEmbeddedExport +20 10 TestEmbeddedExportConcurrent +20 11 TestEmbeddedFederation +20 12 TestEmbeddedFederationConcurrent +20 13 TestEmbeddedFindDuplicates +20 14 TestEmbeddedFindDuplicatesConcurrent +20 15 TestEmbeddedFlatten +20 16 TestEmbeddedFlattenConcurrent +20 17 TestEmbeddedGC +20 18 TestEmbeddedGCConcurrent +20 19 TestEmbeddedGate +20 20 TestEmbeddedGateConcurrent +20 1 TestEmbeddedGateCreate +20 2 TestEmbeddedGetDependencies +20 3 TestEmbeddedGraph +20 4 TestEmbeddedGraphConcurrent +20 5 TestEmbeddedHistory +20 6 TestEmbeddedHistoryConcurrent +20 7 TestEmbeddedHooks +20 8 TestEmbeddedHooksConcurrent +20 9 TestEmbeddedHuman +20 10 TestEmbeddedHumanConcurrent +20 11 TestEmbeddedImport +20 12 TestEmbeddedImportConcurrent +20 13 TestEmbeddedInfo +20 14 TestEmbeddedInfoConcurrent +20 15 TestEmbeddedInit +20 16 TestEmbeddedInitConcurrent +20 17 TestEmbeddedKV +20 18 TestEmbeddedKVConcurrent +20 19 TestEmbeddedLabel +20 20 TestEmbeddedLabelConcurrent +20 1 TestEmbeddedLint +20 2 TestEmbeddedLintConcurrent +20 3 TestEmbeddedList +20 4 TestEmbeddedListConcurrent +20 5 TestEmbeddedMemory +20 6 TestEmbeddedMemoryConcurrent +20 7 TestEmbeddedMigrate +20 8 TestEmbeddedMigrateConcurrent +20 9 TestEmbeddedOnboard +20 10 TestEmbeddedOnboardConcurrent +20 11 TestEmbeddedOrphans +20 12 TestEmbeddedOrphansConcurrent +20 13 TestEmbeddedPrime +20 14 TestEmbeddedPrimeConcurrent +20 15 TestEmbeddedPromote +20 16 TestEmbeddedPromoteCLI +20 17 TestEmbeddedPromoteCLIConcurrent +20 18 TestEmbeddedPrune +20 19 TestEmbeddedPurge +20 20 TestEmbeddedPurgeConcurrent +20 1 TestEmbeddedQuery +20 2 TestEmbeddedQueryConcurrent +20 3 TestEmbeddedQuick +20 4 TestEmbeddedQuickConcurrent +20 5 TestEmbeddedQuickstart +20 6 TestEmbeddedQuickstartConcurrent +20 7 TestEmbeddedReady +20 8 TestEmbeddedReadyConcurrent +20 9 TestEmbeddedRenamePrefix +20 10 TestEmbeddedRenamePrefixConcurrent +20 11 TestEmbeddedReopen +20 12 TestEmbeddedReopenConcurrent +20 13 TestEmbeddedRestore +20 14 TestEmbeddedRestoreConcurrent +20 15 TestEmbeddedSearch +20 16 TestEmbeddedSearchConcurrent +20 17 TestEmbeddedSetup +20 18 TestEmbeddedSetupConcurrent +20 19 TestEmbeddedShip +20 20 TestEmbeddedShipConcurrent +20 1 TestEmbeddedShow +20 2 TestEmbeddedShowConcurrent +20 3 TestEmbeddedStale +20 4 TestEmbeddedStaleConcurrent +20 5 TestEmbeddedState +20 6 TestEmbeddedStateConcurrent +20 7 TestEmbeddedStatus +20 8 TestEmbeddedStatusConcurrent +20 9 TestEmbeddedSupersede +20 10 TestEmbeddedSupersedeConcurrent +20 11 TestEmbeddedSwarm +20 12 TestEmbeddedSwarmConcurrent +20 13 TestEmbeddedTodo +20 14 TestEmbeddedTodoConcurrent +20 15 TestEmbeddedTypes +20 16 TestEmbeddedTypesConcurrent +20 17 TestEmbeddedUndefer +20 18 TestEmbeddedUndeferConcurrent +20 19 TestEmbeddedUpdate +20 20 TestEmbeddedUpdateConcurrent +20 1 TestEmbeddedVC +20 2 TestEmbeddedVCConcurrent +20 3 TestEmbeddedVersion +20 4 TestEmbeddedVersionConcurrent +20 5 TestEmbeddedWhere +20 6 TestEmbeddedWhereConcurrent diff --git a/.github/scripts/embedded-storage-test-shard.sh b/.github/scripts/embedded-storage-test-shard.sh index 0d6968e86..2f78a7883 100755 --- a/.github/scripts/embedded-storage-test-shard.sh +++ b/.github/scripts/embedded-storage-test-shard.sh @@ -140,17 +140,13 @@ if [ "${BEADS_TEST_SHARD_LIST_ONLY:-}" = "1" ]; then exit 0 fi -# 20m, as embedded-test-shard.sh: under -race, TestBatchApplyContract alone -# takes ~10m at the 1000-item issueops.MaxApplyBatchItems cap, and the -# heaviest shard projects to ~13m (see embedded-storage-test-shards.txt). -# The jobs' timeout-minutes (25) leave room for setup after this fires. if [ -x "$STORAGE_BINARY" ]; then - exec "$STORAGE_BINARY" -test.v -test.count=1 -test.timeout=20m \ + exec "$STORAGE_BINARY" -test.v -test.count=1 -test.timeout=15m \ -test.run "$RUN_REGEX" \ "$@" else echo "Warning: pre-built test binary not found at $STORAGE_BINARY, falling back to go test" - exec go test -tags=gms_pure_go -v -race -count=1 -timeout 20m \ + exec go test -tags=gms_pure_go -v -race -count=1 -timeout 15m \ -run "$RUN_REGEX" \ "$@" \ ./internal/storage/embeddeddolt/ diff --git a/.github/scripts/embedded-storage-test-shards.txt b/.github/scripts/embedded-storage-test-shards.txt index c3f45aacb..1327de7a3 100644 --- a/.github/scripts/embedded-storage-test-shards.txt +++ b/.github/scripts/embedded-storage-test-shards.txt @@ -15,27 +15,10 @@ # excluded — it runs in the separate test-embedded-conformance job, which # shards by sub-test path (core/audit), not by top-level function name, and # is a different mechanism from this manifest. -# -# Several tests are placed by measured -race duration instead (PR Risk run -# 36807191489, at the 1000-item issueops.MaxApplyBatchItems cap): -# TestBatchApplyContract (604s) on shard 4, which has ~3m of other tests, -# and TestLargeBatchApplyWallClock_Embedded (261s) on shard 5. Hashed, -# TestBatchApplyContract shared shard 2 with ~5m of other tests and ran it -# past the job's old 15m timeout; placed so, the heaviest shard projects to -# ~13m. -# -# TestLargeBatchApplyStatementCounts_Embedded (247s in that same run) was -# later split into TestLargeBatchApplyStatementCounts356_Embedded, -# TestLargeBatchApplyStatementCounts712_Embedded and -# TestLargeBatchApplyStatementCountsClassic40_Embedded (slice F1, CI shard -# balance; see internal/storage/embeddeddolt/large_batch_apply_measure_test.go). -# All 3 are kept on shard 2, where the single test used to live, rather than -# rebalanced across this frozen 5-shard block. 5 1 TestActiveDatabaseSizeFailsForMissingEmbeddedDatabase 5 2 TestActiveDatabaseSizeScopesToActiveEmbeddedDatabase 5 3 TestAddDependency 5 4 TestAddLabel -5 4 TestBatchApplyContract 5 5 TestBuildDSN_NoDatabase 5 1 TestBuildDSN_SpacesInPath 5 2 TestBuildDSN_WithDatabase @@ -90,10 +73,6 @@ 5 1 TestHistory_NullTextColumns 5 2 TestHookFiringStoreCreateIssuesFiresDependencyUpdatesFromEmbeddedStore 5 3 TestIsBlockedBatchEmbedded -5 2 TestLargeBatchApplyStatementCounts356_Embedded -5 2 TestLargeBatchApplyStatementCounts712_Embedded -5 2 TestLargeBatchApplyStatementCountsClassic40_Embedded -5 5 TestLargeBatchApplyWallClock_Embedded 5 4 TestLeaseLifecycleEmbedded 5 5 TestListWisps 5 1 TestOpenAfterCloseCreatesNewStore @@ -109,500 +88,3 @@ 5 1 TestUpdateIssueType 5 2 TestUpdateMergeOps_DoltEngine 5 3 TestUpdateMergeOps_DoltEngine_TwoIssues - -# Embedded-Dolt storage test shard manifest. -# -# Format: -# -# 40-shard split for the Bazel-only embedded-Dolt storage tier -# (bazel-embedded in .github/workflows/bazel.yml), bin-packed -# longest-processing-time-first by measured wall-time from -# scripts/ci/embedded_storage_test_durations.json (see that file for provenance -# and its "relative weight, not absolute SLA" caveat). -# Regenerate with scripts/ci/gen_embedded_storage_shard_manifest.py 40 -# --weights=duration --write after adding or removing Test* functions in -# internal/storage/embeddeddolt/*_test.go. --write is incremental: it -# keeps every already-assigned test on its current shard and only places -# newly-discovered names, onto whichever shard is currently lightest, so -# two unrelated PRs that each add one test do not textually conflict or -# silently invalidate each other's packing. Pass --repack to force a full -# from-scratch rebalance instead (a deliberate, separate change — expect a -# large diff). --check only verifies every discovered name is listed here -# exactly once (no stale or duplicate entries); it does NOT verify the -# packing is still well-balanced. -# -# This file is generated: notes added here are erased by the next -# regeneration. Add them to the generator or the durations file instead. - -40 1 TestLifecycleCloseReopenContract - -40 2 TestBatchApplyContract - -40 3 TestActiveDatabaseSizeFailsForMissingEmbeddedDatabase -40 3 TestActiveDatabaseSizeScopesToActiveEmbeddedDatabase -40 3 TestBuildDSN_NoDatabase -40 3 TestBuildDSN_SpacesInPath -40 3 TestBuildDSN_WithDatabase -40 3 TestCleanGitRemoteCacheGarbage -40 3 TestCleanGitRemoteCacheGarbage_NoCacheDir -40 3 TestCleanGitRemoteCacheGarbage_Throttled -40 3 TestCommitAllOutOfBandChangeAndHonestNoOp -40 3 TestCreateBatchEquivalencePartsCoverEveryScenario -40 3 TestCreateBatchFastPathsMatchPerRowLarge_Embedded -40 3 TestCrossRecordInvariantContract -40 3 TestDepRekeyMarkerStillInIgnoredSeries -40 3 TestDoltCommitResponseLossIsIndeterminate -40 3 TestEmbeddedDependencyEditorRefusesADottedChildGatedOnItsOwnParent -40 3 TestEmbeddedDoltStoreIssueLifecycleBuildsOperations -40 3 TestEmbeddedDoltStoreIssueLifecycleRejectsNilStore -40 3 TestEmbeddedOpenReadOnly_FreshDatabaseNoWrites -40 3 TestEmbeddedSQLCommitResponseLossIsIndeterminate -40 3 TestEpochContract -40 3 TestExpectedRevisionContract -40 3 TestGitRefPinsMatchDolt -40 3 TestHasRepository -40 3 TestHelperProcess -40 3 TestLargeBatchApplyStatementCounts712_Embedded -40 3 TestLargeGraphCreateTiming_Embedded -40 3 TestLenientGateWarningBody_BluntRefusalKeepsSharedGuidance -40 3 TestLenientGateWarningBody_ShapedDecisions -40 3 TestLenientGateWarning_ComposedOutput -40 3 TestMain -40 3 TestPushCommitsPendingChangesFirst -40 3 TestRemoteEntryPointsUseVersionControlOps -40 3 TestRetentionContract -40 3 TestStageAndCommitAfterSQLCommitResponseLossIsIndeterminateAndNotReplayed -40 3 TestTransactionCleanupAfterSQLCommitIsIndeterminate -40 3 TestTransactionCleanupBeforeSQLCommitRemainsDefinite -40 3 TestUnknownProvenanceLeaseStaysReclaimable - -40 4 TestBlockedListerContract -40 4 TestCreateBatchFastPathsMatchPerRowSmallDepAdd_Embedded -40 4 TestEmbeddedIssueOperationsUpdateLabelPatchOrdering -40 4 TestEmbeddedReaderGetMissIsNotFoundAndBackendFailureDoesNotDecay -40 4 TestEmbeddedReaderListFilterFamiliesNarrowToTheirRows -40 4 TestMergeAndSettleWithStrategy_EmptyStrategyMatchesMergeAndSettle - -40 5 TestEmbeddedClaimerRefusesIncompleteRequestsAndAnAbsentIDWithoutTouchingState -40 5 TestEmbeddedIssueOperationsCloseAndReopenAWisp -40 5 TestEmbeddedReaderListKeysetPositionResumesTheCreatedDescIDAscOrder -40 5 TestRecordVersionKeepsSubSecondChangeAt -40 5 TestWithPeerAuth - -40 6 TestCreateBatchFastPathsMatchPerRowApplyWaitsFor_Embedded -40 6 TestEmbeddedMergeAndSettleFKCascadeRepair -40 6 TestEmbeddedMergeAndSettleSkipsNonMemoryConfigConflict -40 6 TestEmbeddedReaderListKeysetPositionNarrowsWithoutReplacingTheOtherPredicates -40 6 TestEmbeddedRelationsAnswersAWispTargetInTheOutDirection -40 6 TestPromoteStillWorksWithCollisionGuard -40 6 TestSearchWithMissingWispPlaneTables -40 6 TestVersionedHistoryKeepsNumbersTheStoreDistinguishesApart - -40 7 TestConcurrencyMultiProcess -40 7 TestEmbeddedBatchCloserRecordsOneHistoryEntryForWhatLanded -40 7 TestEmbeddedDependencyEditorAcceptsAnExternalTarget -40 7 TestEmbeddedDependencyEditorRelatesToAddLeavesItsSourceUnblocked -40 7 TestEmbeddedIssueOperationsUpdateMetadataReplaceClearsAndValidates -40 7 TestEmbeddedLenientOpenWarning_DataBehind -40 7 TestEmbeddedRelationsLeavesTheCallersRequestAlone -40 7 TestLoseVersionLeavesTheEpochTablesCommitted -40 7 TestMergeAndSettleWithStrategy_ResolvesDeclinedConflict - -40 8 TestDeleterContract -40 8 TestEmbeddedDependencyEditorRoutesWispSourcedRemovalToTheWispPlane -40 8 TestEmbeddedMergeAndSettleDirtyWorkingSetSurvivesRefusedMerge -40 8 TestEmbeddedMintLeavesUpdatedAtWhereHistoryOffDoes -40 8 TestEmbeddedReaderGetBriefDepsProjectsTheDependencyRows -40 8 TestEmbeddedRelationsOrdersNeighborsFromBothPlanesTogether -40 8 TestEmbeddedReopenIssueCategorySemantics -40 8 TestGetIssueCommentsPageEmbedded -40 8 TestListWisps - -40 9 TestBumpEpochCarryingNeitherCarriesNorServesARowFromAFutureEpoch -40 9 TestEmbeddedDependencyEditorRefusesCrossPlaneCycle -40 9 TestEmbeddedDependencyEditorSameTypeReAddIsIdempotent -40 9 TestEmbeddedDependencyEditorSnapshotsTheRequest -40 9 TestEmbeddedMigration0058AddsOnlyMissingConstraintInMixedPopulation -40 9 TestEmbeddedReaderReadyPageWiderThanTheHydrationBatchIsStillThatPrefix -40 9 TestEmbeddedReadyClaimerHydratesOnlyItsBlocksEdgesIntoTheCardinalities -40 9 TestGetReadyWorkIncludeEphemeralExcludesNonReadyWisps -40 9 TestPristineEmbeddedDoltFixtureRelocatesPrefixes -40 9 TestUpdateMergeOps_DoltEngine - -40 10 TestEmbeddedClaimerStampsStartedAtOnceAcrossTheTwoWritesItChoosesBetween -40 10 TestEmbeddedDependencyEditorRemoveIsIdempotent -40 10 TestEmbeddedIssueOperationsUpdateClaimCASAndTransferMatrix -40 10 TestEmbeddedReaderGetResolvesTheExactIDAcrossBothPlanes -40 10 TestEmbeddedReaderReadyEphemeralPageKeepsBothPlanesCountsAtItsBoundary -40 10 TestGraphCounterContract -40 10 TestIgnoredCursorHealRespectsOperatorOverride -40 10 TestRunInTransactionPostCommitStageFailureIsIndeterminate -40 10 TestVersionedHistoryOffMatchesPreStackGolden_Embedded -40 10 TestWithPeerAuth_WarnsWhenStoredPeerSuppressesAmbientPasswordAmbient - -40 11 TestABumpUnderOneStoreIDMovesTheEpochSeenThroughEveryStoreID -40 11 TestEmbeddedBatchCreateFirstVersionCarriesCreationTimeEdges -40 11 TestEmbeddedClaimerRefusalMessagesCarryTheirFragments -40 11 TestEmbeddedGuardedUpdateMintsOnceAfterItsPatches -40 11 TestEmbeddedMergeAndSettleRefusesUnknownTable -40 11 TestEmbeddedReaderListMaxRowsBoundaryIsLimitPlusOffset -40 11 TestEmbeddedReadyClaimerRecordsOneHistoryEntryForAWin -40 11 TestEventsJournal_ActorMatrixOnEmbedded -40 11 TestMemoriesContract -40 11 TestWithPeerAuth_WarnsWhenStoredPeerSuppressesAmbientPasswordStored - -40 12 TestCounterExcludeStatusAcceptsAWorkspaceCustomStatusWithoutIncludeInfra -40 12 TestEmbeddedClaimerAcceptsAConfiguredActiveStatusAndRefusesAConfiguredWipOne -40 12 TestEmbeddedClaimerRefusesAWispIDAsNotFound -40 12 TestEmbeddedDependencyEditorVersionsOnlyARealAddOrRemove -40 12 TestEmbeddedIgnoredMigration0023RepairsForkLineageJournalShape -40 12 TestEmbeddedIssueOperationsCreateUnderAParentMintsTheNextChildID -40 12 TestEmbeddedReadyClaimerAnswersTheQuestionReaderReadyLists -40 12 TestHistoryPresenceContract -40 12 TestLifecycleUpdateContract -40 12 TestReaderListExcludeStatusAcceptsAWorkspaceCustomStatus - -40 13 TestEmbeddedDepRekeyConvergesRenameChain -40 13 TestEmbeddedDependencyEditorAppliesParentChildBeforeBlockingEdges -40 13 TestEmbeddedDependencyEditorRefusesAGhostSource -40 13 TestEmbeddedIssueOperationsCreateParentChildRecomputesWaitsForClosure -40 13 TestEmbeddedIssueOperationsUpdateClaimPatchAppliesAfterClaim -40 13 TestEmbeddedIssueOperationsUpdateFoldsMetadataIntoOneEvent -40 13 TestEmbeddedPre0058RepairMarchesDriftedWispDependenciesToFinalShape -40 13 TestEmbeddedRelationsDirectionSelectsTheInverseGraph -40 13 TestGraphWalkPlansHonorJoinHints - -40 14 TestBatchCreatorContract -40 14 TestBlockingAnnotatorContract -40 14 TestCountsIncludeWispDependencies -40 14 TestEmbeddedIssueOperationsPersistenceRollbackWispAndDetachedResults -40 14 TestEmbeddedOpenForRemoteSync_DataBehind -40 14 TestEmbeddedReaderOffsetSkipsTheRowsBeforeThePage -40 14 TestEmbeddedReaderReadyDefaultTypeExclusionsYieldToAnExplicitType -40 14 TestEmbeddedReadyClaimerClaimsTheFrontRowAndReturnsThePostClaimState -40 14 TestEmbeddedReadyClaimerDoesNotMutateTheCallerRequest -40 14 TestHeartbeatIssueCommitsARefusalOnlyWhenItReadTheIssueEmbedded -40 14 TestHistory_NullTextColumns - -40 15 TestCreateIssuesRejectStaleUpserts -40 15 TestDecryptPassword_ShortCiphertextClassifiesAsKeyMismatch -40 15 TestEmbeddedGetMergeBlockersOnAQuietStore -40 15 TestEmbeddedIgnoredMigration0023NoopsWithoutItsTables -40 15 TestEmbeddedIssueOperationsRequestValuesAreNotMutated -40 15 TestEmbeddedReaderReadyBriefDropsTheFreeFormTextAndNothingElse -40 15 TestEmbeddedRelationsFiltersByAnOpenTypeVocabulary -40 15 TestGetIssue -40 15 TestLegacyTrackedIgnoredCursorHealsOnOpen -40 15 TestMoleculeContract -40 15 TestReadyWorkPageTotalFilterMatrix - -40 16 TestDescendantWalkReachesEveryTargetKindAndStopsOnCycles -40 16 TestEmbeddedDependencyEditorAddedEchoesTheRequestOrder -40 16 TestEmbeddedDependencyEditorRemovesOnlyTheNamedEdge -40 16 TestEmbeddedReaderDoesNotMutateTheCallerRequest -40 16 TestEmbeddedReaderListLimitBoundaryUnderASortTheDatabaseCanExpress -40 16 TestEmbeddedRenameKeepsDependencyIDsDeterministic -40 16 TestExternalCapabilityBlocksEmbeddedReadyAndAppearsInTree -40 16 TestGetReadyWorkIncludeEphemeralAppliesWispFilters -40 16 TestMergeWithStrategy_OursKeepsLocal -40 16 TestWithPeerAuth_DivergedRemoteURLFailsClosed - -40 17 TestAddDependency -40 17 TestEmbeddedBatchCloserWispItemClosesAndEarnsTheClaim -40 17 TestEmbeddedDependencyEditorSameTypeReAddWithChangedThreadMintsOneVersion -40 17 TestEmbeddedIssueOperationsUpdateIssuePlaneOnlyRefusesWisps -40 17 TestEmbeddedIssueOperationsUpdateStatusCrossingSettlesAConditionalBlocksDepender -40 17 TestEmbeddedReaderListRejectsATypeOutsideTheWorkspaceVocabulary -40 17 TestEmbeddedReadyClaimerLeasesADurableWinButNotAnEphemeralOne -40 17 TestHeartbeatMatchesActorAcrossSpellingEmbedded -40 17 TestStatsReporterContract -40 17 TestVersionReconcilerContract - -40 18 TestCreateBatchParticipationGeneration -40 18 TestEmbeddedBatchCloserIdempotentRecloseIsAPerItemSuccess -40 18 TestEmbeddedDependencyEditorMixedBatchWritesBothPlanes -40 18 TestEmbeddedIssueOperationsUpdateRawMetadataTakesTheFunnelsValueShapes -40 18 TestEmbeddedMergeAndSettleMemoryConfigConflict -40 18 TestEmbeddedReaderListIncludeEphemeralMergesThePlanesIntoOneOrder -40 18 TestEmbeddedReaderListParentReachesEveryDescendantAndOnlyItsOwn -40 18 TestMetadataCASContract -40 18 TestOpenDifferentDirsReturnsDifferentStores -40 18 TestPromoteClearsNoHistory -40 18 TestRemoteVerbPlainRemoteWithoutAuthPresentsNone - -40 19 TestEmbeddedDependencyEditorRetypeRefusalLeavesTheOriginalEdge -40 19 TestEmbeddedIssueOperationsUpdateInvalidIssueTypePreservesValidationSentinel -40 19 TestEmbeddedMigration0062FlipsEventsToIgnoredPlane -40 19 TestEmbeddedReaderListMaxRowsIsHonored -40 19 TestEmbeddedRelationsAnswersInThePinnedOrder -40 19 TestIgnoredCursorHealConvergesFromEveryCrashState -40 19 TestOpenAfterCloseCreatesNewStore -40 19 TestOpenReturnsCachedStore -40 19 TestParticipationGenerationFixtureKitIsWired -40 19 TestRemoteVerbsPresentStoredPeerCredentials -40 19 TestRenameLabelMintsOneVersionPerTouchedBead - -40 20 TestBumpEpochCarryingCarriesOnlyTheLineageHoldingTheNewestRow -40 20 TestCreateIssue -40 20 TestEmbeddedCheckedWrappersRouteTheirPreconditions -40 20 TestEmbeddedIssueOperationsCreateRoutesInfraTypesToWisps -40 20 TestEmbeddedMigrateStepCommitLeavesUntouchedUserDirt_4566 -40 20 TestEmbeddedOpenReadOnly_SkipsGateAndMigrations -40 20 TestEmbeddedReadyClaimerClaimsAnEphemeralRowTheFilterAdmits -40 20 TestEventsJournalRestartAndRetentionBoundary -40 20 TestForeignSkipDetailIsBounded -40 20 TestHeartbeatRejectsWispEmbedded -40 20 TestIgnoredCursorHealNeverResurrectsRolledBackVersions - -40 21 TestBumpEpochCarryingCarriesTheLiveRowsOfEveryStoreInTheDatabase -40 21 TestCreateRejectsCrossTableIDCollision -40 21 TestEmbeddedBatchCloserItemsAnswerToTheCloseGuards -40 21 TestEmbeddedDependencyEditorAcceptsATypeOutsideTheConstants -40 21 TestEmbeddedIssueOperationsUpdateParentStagesBlockedStateAndRollsBackFailure -40 21 TestEmbeddedMigration0047DelegatesToSplitTargetRepairAndConvergesThroughIgnoredChain -40 21 TestEmbeddedRelationsLeavesAnExternalTargetOutOfTheAnswer -40 21 TestEmbeddedRemoteMigrateGate_BlocksReopen -40 21 TestEmbeddedRoleFixtureKitCountHistoryMovesWithACommit -40 21 TestEmbeddedTransactionStandaloneWispLifecyclePersistsWithoutDoltCommit -40 21 TestLargeBatchApplyWallClock_Embedded - -40 22 TestEdgeReaderContract -40 22 TestEmbeddedDepRekeyAbortLeavesMarkerUnrecorded -40 22 TestEmbeddedDepRekeyRefusesToWriteIntoDirtyDependencies -40 22 TestEmbeddedDependencyEditorRefusesSelfDependencyWithTheProbeSkipped -40 22 TestEmbeddedReaderListStatusAcceptsACommaSeparatedORSet -40 22 TestEmbeddedReaderReadyLimitBoundary -40 22 TestEventsAutoPruneKeepsAnUnboundedJournalUnbounded -40 22 TestEventsJournalPruneNeverSplitsASkewedPair -40 22 TestLargeBatchApplyStatementCounts356_Embedded -40 22 TestLeaseLifecycleEmbedded -40 22 TestPolicyBlockingAnnotatorForeignOpensAreSublinear - -40 23 TestAsOfReadContract -40 23 TestEmbeddedBatchCloserBackendFailureReturnsNoOutcomes -40 23 TestEmbeddedBatchCloserClaimNextHydratesWhenSomethingClosed -40 23 TestEmbeddedDependencyEditorAcceptsBlockingAcrossIssueTypes -40 23 TestEmbeddedDependencyEditorRecordsNoHistoryForAnAllEphemeralRequest -40 23 TestEmbeddedMigrateConvergesUnderConcurrentInterruptedRetries_4566 -40 23 TestEmbeddedReaderListEmptyPageIsWellFormed -40 23 TestEmbeddedReadyClaimerFencesTheClaimByEveryLabelSetAndTheParentItWasGiven -40 23 TestEmbeddedRoleFixtureKitHooksAreUsable -40 23 TestEventsAutoPruneBoundsTheJournalToTheFloors -40 23 TestJournalContract - -40 24 TestBootstrapperContract -40 24 TestEmbeddedBatchCloserOutcomesMirrorItemsIndexForIndex -40 24 TestEmbeddedClaimerReclaimsAcrossSpellingWithoutWriting -40 24 TestEmbeddedConfigCommitStagesConfig -40 24 TestEmbeddedIgnoredSeriesConvergesWithFreshInitShape -40 24 TestEmbeddedIssueOperationsCreateWithDependenciesSettlesInTheCreatingTransaction -40 24 TestEmbeddedMigrateSelfHealsAfterMidPassFault_4566 -40 24 TestEmbeddedReaderListQueryMatchesTheTitleOrTheID -40 24 TestEmbeddedReaderListWispTypeNarrowsTheAdmittedPlaneRatherThanAdmittingIt -40 24 TestIgnoredCursorHealResumeRespectsOperatorOverride -40 24 TestLargeBatchApplyStatementCountsClassic40_Embedded -40 24 TestReopenIssue - -40 25 TestBatchGetterContract -40 25 TestDatabases -40 25 TestDualWriteFixtureKitIsWired -40 25 TestEmbeddedBatchCloserClaimNextIsNilWhenNothingClosed -40 25 TestEmbeddedIssueOperationsUpdateClaimHonorsConfiguredActiveStatuses -40 25 TestEmbeddedReadyClaimerLeavesEphemeralRowsOutOfTheDefaultReadySet -40 25 TestEmbeddedReadyClaimerRejectsLimitOffsetBriefAndEmptyActor -40 25 TestGetDependentRecordsForIssuesEmbedded -40 25 TestHeartbeaterContract -40 25 TestIgnoredCursorHealIsIdempotent -40 25 TestIsBlockedBatchEmbedded -40 25 TestReadyWorkPageTotal -40 25 TestWithPeerAuth_RestoresAbsentEnv - -40 26 TestEmbeddedIssueOperationsCreateClosedDerivesTheClosedStamp -40 26 TestEmbeddedMigration0058CleansOrphanedAndInvalidWispDependenciesRowsBeforeAddingConstraints -40 26 TestEmbeddedOpenForReadOnlyCommand_LenientGate -40 26 TestEmbeddedReaderListReadyFlagCarriesTheAssigneeAndPriorityFilters -40 26 TestEmbeddedReaderReadyParentScopesToItsTransitiveDescendants -40 26 TestEmbeddedReadyClaimerSkipsIneligibleFrontRows -40 26 TestEmbeddedRecomputeAllBlockedDirtyGraphIsTyped -40 26 TestEmbeddedRelationsRefusesTheZeroDirection -40 26 TestHookFiringStoreCreateIssuesFiresDependencyUpdatesFromEmbeddedStore -40 26 TestReclaimAnyReplicaOverride -40 26 TestWithPeerAuth_EnvFallbackDoesNotObservePeerCredentials - -40 27 TestEmbeddedBatchCloserDurableHistoryNeverNamesAWisp -40 27 TestEmbeddedBatchCloserRequestValidationReturnsZeroResultAndChangesNothing -40 27 TestEmbeddedDependencyEditorAcceptsADiamond -40 27 TestEmbeddedDependencyEditorAddMarksItsSourceInTheSameVerb -40 27 TestEmbeddedMigration0058HealsAlreadyAffectedDatabase -40 27 TestEmbeddedReaderGetOptionalRowListsAreOffByDefault -40 27 TestEmbeddedRelationsRefusesATypeFilterOverTheColumnLength -40 27 TestEventsJournalReadRefusesAnInteriorHole -40 27 TestPartitionWispIDs -40 27 TestRemoteGitDataRefOnEngine -40 27 TestSweeperContract - -40 28 TestBumpEpochCarryingDoesNotCarryALostLineage -40 28 TestEmbeddedBatchCloserForceBypassesOnlyClosePolicy -40 28 TestEmbeddedBatchCloserSettlesTheDependersOfWhatItClosed -40 28 TestEmbeddedClaimerClaimsAnUnassignedOpenIssueAndAnswersTheBareRow -40 28 TestEmbeddedClaimerReclaimsItsOwnInProgressIssueWithoutWriting -40 28 TestEmbeddedClaimerTakesAPoolAssignedIssueButRefusesAForeignHolder -40 28 TestEmbeddedIssueOperationsUpdateLabelPatchValueRules -40 28 TestEmbeddedIssueOperationsUpdateRefusesATypeOutsideTheWorkspaceVocabulary -40 28 TestEmbeddedReaderListReadyFlagAnswersTheBlockerAwareSet -40 28 TestRenameLabel -40 28 TestUpdateIssueType -40 28 TestWithPeerAuth_RestoresEnvWhenCallbackFails - -40 29 TestEmbeddedBatchCloserClaimFilterValueFailureIsARequestValidationFailure -40 29 TestEmbeddedBatchCloserClaimNextIsNilWhenTheFrontIsEmpty -40 29 TestEmbeddedDepRekeyMergesFreshDuplicateEdges -40 29 TestEmbeddedDependencyEditorRecordsOneHistoryEntryPerLandedRequest -40 29 TestEmbeddedMergeAndSettleMetadataConflict -40 29 TestEmbeddedReaderListDefaultExclusionsAndTheirOverrides -40 29 TestEmbeddedReaderListTimeBoundsAreStrictAndWholeSecond -40 29 TestEmbeddedReaderReadySetOwnsItsStatusPinnedAndTemplateDecisions -40 29 TestEmbeddedTransactionSameStatusDoesNotPublishUnrelatedDurableChanges -40 29 TestEventsJournalAccessor -40 29 TestLeaseReclaimerContract -40 29 TestLifecycleCreateContract - -40 30 TestCounterContract -40 30 TestCycleDetectorContract -40 30 TestEmbeddedClaimerTakesAnOpenIssueTheActorAlreadyHolds -40 30 TestEmbeddedCommitToleratesNothingToCommit -40 30 TestEmbeddedEveryAcceptedLifecycleMutationMintsOneVersion -40 30 TestEmbeddedIssueOperationsUpdateClaimConflictCarriesTheLosingState -40 30 TestEmbeddedIssueOperationsUpdateStampsStartedAtOnceOnTheFirstInProgress -40 30 TestEmbeddedReaderReadySortPoliciesOrderTheSameRows -40 30 TestEmbeddedTransactionStandaloneWispMutationDoesNotPublishUnrelatedDurableChanges -40 30 TestLegacyTrackedIgnoredCursorUnwedgesPull -40 30 TestUpdateMergeOps_DoltEngine_TwoIssues - -40 31 TestBumpEpochCarryingWritesOneCarryRowPerLiveIDNamingTheLineageRoot -40 31 TestDualWriteContract -40 31 TestEmbeddedDependencyEditorAcceptsAForeignRepoTarget -40 31 TestEmbeddedDependencyEditorMaintainsBlockedStateAcrossPlanes -40 31 TestEmbeddedDependencyEditorWritesTheTargetIntoItsTypedColumn -40 31 TestEmbeddedMigrateConsent_BehindAndDirtyWorkingSetReconcileRecovers -40 31 TestEmbeddedReaderListPriorityKeysetWalkOverAnOversizedEqualKeyRunLosesNothingAndRepeatsNothing -40 31 TestEmbeddedTransactionWispLifecyclePersistsDurableDependent -40 31 TestEventsSinceEmbedded -40 31 TestGetLabels -40 31 TestReadyCounterContract -40 31 TestWithPeerAuth_KeyMismatchFailsClosed - -40 32 TestCurrentAddressForNeverReseedsAnAbsentStoreEpochRow -40 32 TestEmbeddedClaimerGrantsALiveLeaseOnTheRowItWonAndLeavesARefusedOneAlone -40 32 TestEmbeddedDependencyEditorRefusesAMissingLocalTarget -40 32 TestEmbeddedDirtyTablesGate_BlocksReopenThenWorkingSetReconcileRecovers -40 32 TestEmbeddedIgnoredMigration0019CreatesEventsForFreshClones -40 32 TestEmbeddedIssueOperationsCreateReverseNonBlockingStagesConcreteTables -40 32 TestEmbeddedReadyClaimerEmptyFrontIsNormal -40 32 TestEmbeddedRecomputeAllBlockedWiring -40 32 TestEventsJournal_EmbeddedConcurrentGaplessNoDup -40 32 TestGetNextChildID -40 32 TestRemoteVerbPlainRemoteKeepsAmbientEnv -40 32 TestWithPeerAuth_MissingRemoteFailsClosed - -40 33 TestEmbeddedBatchCloserDuplicateItemRecloseAtItsOwnIndex -40 33 TestEmbeddedDependencyEditorClosedChildAddSatisfiesAnAnyChildrenGate -40 33 TestEmbeddedDependencyEditorValidationRefusalsWriteNothing -40 33 TestEmbeddedIgnoredMigration0023NoopsOnHealthyWorkspace -40 33 TestEmbeddedMoveIssuePersistenceEphemeralNoHistorySmoke -40 33 TestEmbeddedRelationsResolvesTheAnchorIDExactly -40 33 TestEmbeddedSyncCommitsConfigBeforeMerge -40 33 TestEventsAutoPruneThrottleSkipsTheSecondPass -40 33 TestFirstMintLeavesTheEpochTablesCommitted -40 33 TestGetDependentRecordsEmbedded -40 33 TestPruneEventsBatchDeletesTheLowestSeqsOnly -40 33 TestTreeWalkerContract - -40 34 TestAddLabel -40 34 TestBumpEpochCarryingLeavesTheEpochTablesCommitted -40 34 TestDecryptPassword_KeyMismatchNamesTheLocalKey -40 34 TestEmbeddedBlockedRecheckRecordsUnblockingWrites -40 34 TestEmbeddedDependencyEditorAttributesItsEventsToTheActor -40 34 TestEmbeddedDependencyEditorSameTypeReAddWithIdenticalMetadataIsANoOp -40 34 TestEmbeddedIssueOperationsCreateAggregatesRelations -40 34 TestEmbeddedReaderReadyDeferredAndEphemeralGates -40 34 TestEmbeddedReaderReadyPageIsThePrefixOfTheUnboundedAnswerCountsIncluded -40 34 TestMergeWithStrategy_TheirsResolvesAndCommits -40 34 TestQuerierContract -40 34 TestReadyWorkStatementBudget - -40 35 TestEmbeddedBatchCloserAllIdempotentBatchLandsNothing -40 35 TestEmbeddedClaimIssueWrapperClaimsTheWispPlane -40 35 TestEmbeddedDependencyEditorGateScopeFollowsTheEdgeType -40 35 TestEmbeddedDependencyEditorRefusesASamePlaneEdgeClosingACrossPlaneCycle -40 35 TestEmbeddedDependencyEditorRepeatsWithinOneRequestCollapse -40 35 TestEmbeddedIssueOperationsUpdateAllScalarAndPointerFieldsReportChanged -40 35 TestEmbeddedIssueOperationsUpdateWritesEveryScalarPatchField -40 35 TestMerge_NoStrategyStillErrorsWithEscapeHatchHint -40 35 TestReadEventsAutoPruneStateResolvesBothHalvesInOneQuery -40 35 TestReadyWorkWithMissingDeferredParentTables -40 35 TestReclaimExpiredLeaseSurvivesRestartEmbedded - -40 36 TestBumpEpochCarryingFailsAndRollsBackWhenARowAlreadyExistsAtTheNewEpoch -40 36 TestCommenterContract -40 36 TestConcurrentOpenReturnsCached -40 36 TestEmbeddedBatchCloserAllRefusedBatchRecordsNoHistory -40 36 TestEmbeddedDependencyEditorRefusalWritesNothing -40 36 TestEmbeddedDependencyEditorSkipPerEdgeCycleCheckDropsOnlyTheProbe -40 36 TestEmbeddedMigration0057ConvertsTextEventsColumnsToLongtext -40 36 TestEmbeddedReaderListReadyFlagRefusesAFilterItCannotCarry -40 36 TestEmbeddedRelationsRefusesAnUnusableTypeFilter -40 36 TestGetReadyWorkMoleculeFilter -40 36 TestReleaserContract -40 36 TestSchemaAfterInit - -40 37 TestEmbeddedBatchCloserClaimNextSeesAnUnblockingFromItsOwnBatch -40 37 TestEmbeddedBatchCloserOutcomeSnapshotIsTheDocumentedShape -40 37 TestEmbeddedDependencyEditorRecordsOneHistoryEntryForAMixedPlaneRequest -40 37 TestEmbeddedDependencyEditorRefusesACycleThroughAParentChildHop -40 37 TestEmbeddedDependencyEditorRemoveUnmarksItsSourceAndDescendants -40 37 TestEmbeddedIssueOperationsCreateEmptyIDPreservesParentAndReverseDependencies -40 37 TestEmbeddedLenientOpenWarning_OtherRefusalsKeepBluntGuidance -40 37 TestEmbeddedMergeAndSettleReportsOperatorConflicts -40 37 TestEmbeddedReaderListBriefDropsTheFreeFormTextAndNothingElse -40 37 TestEmbeddedReaderListNaturalNumericIDSortTrimsAfterTheFetch -40 37 TestEpochWritesReportAnUnpublishedCommitAsIndeterminate -40 37 TestIgnoredCursorHealRespectsOverrideUnderBroaderPattern - -40 38 TestCreateIssues -40 38 TestEmbeddedClaimerRefusesABuiltInIneligibleStatusWithTheStateThatRefusedIt -40 38 TestEmbeddedDependencyEditorRefusesBlockingEdgeAcrossItsOwnHierarchy -40 38 TestEmbeddedIssueOperationsUpdateClosedFieldsMatchClose -40 38 TestEmbeddedIssueOperationsUpdateNotesConflictPreservesValidationSentinel -40 38 TestEmbeddedReaderGetDetailShapeMatchesTheSeededIssue -40 38 TestEmbeddedReaderListCountsAreBlocksOnlyWhereGetCountsEveryEdge -40 38 TestEmbeddedReaderListKeysetWalkOverAnOversizedGroupLosesNothingAndRepeatsNothing -40 38 TestEmbeddedTempRebuildIntermediatesNeverReachHistory -40 38 TestEventsJournalPruneWhenEveryRowIsOlderThanTheCutoff -40 38 TestPolicyBlockingAnnotatorBatchEqualsPerItem -40 38 TestWithPeerAuth_ConcurrentPeersDoNotMixCredentials - -40 39 TestDualWriteContractFlagOff -40 39 TestEmbeddedBatchCloserPerItemRefusalIsAResultAndSurvivorsCommit -40 39 TestEmbeddedBlockedRecheckFailureKeepsTheWrite -40 39 TestEmbeddedDependencyEditorMixedBatchRefusalRollsBackBothPlanes -40 39 TestEmbeddedIssueOperationsClaimLeavesBlockedStateAlone -40 39 TestEmbeddedMigrateRepairedDependenciesIDColumnCommitsAtomicallyWithVersion53_4690 -40 39 TestEmbeddedReaderListSkipCountsDropsTheCardinalitiesAndNothingElse -40 39 TestEmbeddedRelationsResolvesAWispAnchor -40 39 TestParticipationGenerationFence -40 39 TestPreviewStoreRefusesLocalMetadataWrite -40 39 TestReclaimRefusesForeignReplicaLease -40 39 TestWorkspaceConfigContract - -40 40 TestCountsWithMissingWispPlaneTables -40 40 TestEmbeddedBatchCloserDoesNotMutateTheCallerRequest -40 40 TestEmbeddedBlockedRecheckSettlesWhatTheWriteRecorded -40 40 TestEmbeddedDepRekeyRepairsHalfRekeyedDuplicateEdge -40 40 TestEmbeddedDependencyEditorRefusesBlockingEdgeAcrossAWispHierarchy -40 40 TestEmbeddedDependencyEditorRoutesWispSourcedEdgeToTheWispPlane -40 40 TestEmbeddedDependencyEditorSameTypeReAddWithChangedMetadataMintsOneVersion -40 40 TestEmbeddedIssueOperationsUpdateStatusCrossingSettlesDependers -40 40 TestEmbeddedOpenForRemoteSync_OtherRefusalsStayFatal -40 40 TestEmbeddedRelationsSeparatesNoNeighborsFromNoSuchIssue -40 40 TestEmbeddedReopenCustomDoneFallsBackWhenNormalizedTableMissing diff --git a/.github/scripts/embedded-test-shard.sh b/.github/scripts/embedded-test-shard.sh index 3b7bdc7e2..a89757e69 100755 --- a/.github/scripts/embedded-test-shard.sh +++ b/.github/scripts/embedded-test-shard.sh @@ -3,7 +3,7 @@ # # Usage: embedded-test-shard.sh # -# Discovers all TestEmbedded* top-level functions in cmd/bd/*_test.go. +# Discovers all TestEmbedded* top-level functions from cmd/bd/*_embedded_test.go. # Tests listed in embedded-cmd-test-shards.txt for the requested shard count use # that committed assignment; newly-added tests fall back to hash(name) % total. # Runs the matching subset using the pre-built test binary at @@ -33,12 +33,8 @@ fi SHARD_INDEX=$(( SHARD_NUMBER - 1 )) MANIFEST="${BEADS_TEST_SHARD_MANIFEST:-.github/scripts/embedded-cmd-test-shards.txt}" -# Discover all top-level TestEmbedded* functions across every cmd/bd test -# file, by name rather than by file glob (as proxied-test-shard.sh does): a -# test gated on BEADS_TEST_EMBEDDED_DOLT runs in this lane or in none, wherever -# it lives. scripts/embedded_lane_coverage_test.go fails on a gated test this -# selection misses. -ALL_TESTS=$(grep -rh '^func TestEmbedded' cmd/bd/*_test.go \ +# Discover all top-level TestEmbedded* functions. +ALL_TESTS=$(grep -rh '^func TestEmbedded' cmd/bd/*_embedded_test.go \ | sed 's/func \(TestEmbedded[A-Za-z0-9_]*\).*/\1/' \ | sort -u) diff --git a/.github/scripts/proxied-cmd-test-shards.txt b/.github/scripts/proxied-cmd-test-shards.txt index fca167226..1c5e9dbed 100644 --- a/.github/scripts/proxied-cmd-test-shards.txt +++ b/.github/scripts/proxied-cmd-test-shards.txt @@ -2,287 +2,151 @@ # # Format: # -# 34-shard split for the Bazel-only proxied-server tier -# (bazel-proxied in .github/workflows/bazel.yml), bin-packed -# longest-processing-time-first by measured wall-time from -# scripts/ci/proxied_test_durations.json (see that file for -# provenance and its "relative weight, not absolute SLA" caveat). -# Regenerate with scripts/ci/gen_proxied_shard_manifest.py 34 -# --weights=duration --write after adding or removing -# TestProxiedServer*/TestServerMode* functions. --write is incremental: -# it keeps every already-assigned test on its current shard and only -# places newly-discovered names, onto whichever shard is currently -# lightest, so two unrelated PRs that each add one test do not textually -# conflict or silently invalidate each other's packing. Pass --repack to -# force a full from-scratch rebalance instead (a deliberate, separate -# change — expect a large diff). --check only verifies every discovered -# name is listed here exactly once (no stale or duplicate entries); it -# does NOT verify the packing is still well-balanced, so a reweighted -# proxied_test_durations.json does not by itself fail --check — rerun -# with --repack periodically (or whenever shard loads look uneven in CI) -# to rebalance on purpose. Newly-added tests not listed here -# hash-distribute via proxied-test-shard.sh: cksum(name) % total, -# unrelated to proxied_test_durations.json (that file only feeds this -# generator's own cost estimate for a test it has not measured yet). +# 15-shard split, bin-packed longest-processing-time-first by +# estimated cost (bd-init count; per-init migration chains dominate). +# Regenerate with scripts/ci/gen_proxied_shard_manifest.py after adding, +# splitting, or reweighting TestProxiedServer* functions. Newly-added +# tests not listed here hash-distribute via proxied-test-shard.sh. # -# This file is generated: notes added here are erased by the next -# regeneration. Add them to the generator or the durations file -# instead. +# `bd serve`'s two suites are listed at the shard the hash fallback was +# already giving them, so this records the assignment without reshuffling +# the other 100. TestProxiedServerServeReadParity is the CLI/HTTP read parity +# oracle — the behavioural half of the anti-drift claim — so it being IN this +# lane, and this lane being required by PR Risk's CI gate, is the thing that +# makes that claim enforced on PRs rather than asserted in a doc comment. +# +# TestProxiedServerListSortRetiresTheWalk is listed the same way, at the shard +# `cksum` was already giving it. It is the most expensive entry in the file — +# it seeds 1400 rows and walks the listing three ways — and it is here for the +# same reason as the parity oracle: it is the measurement behind the claim that +# `sort=priority` retires the client-side walk, so it has to run on PRs. -34 1 TestProxiedServerDep2 -34 1 TestProxiedServerMolProgress -34 1 TestProxiedServerReadyClaimConcurrent -34 1 TestProxiedServerServeActivatesTheEventsJournal -34 1 TestProxiedServerServeRelease +15 1 TestProxiedServerCount +15 1 TestProxiedServerDelete +15 1 TestProxiedServerDoltRemoteRemove +15 1 TestProxiedServerExport +15 1 TestProxiedServerGateLifecycle +15 1 TestProxiedServerListSortRetiresTheWalk +15 1 TestProxiedServerMolCurrent +15 1 TestProxiedServerPathHelpers +15 1 TestProxiedServerTodo +15 1 TestProxiedServerUpdateConcurrentAppendNotes -34 2 TestProxiedServerDepListSingleIDWarnsOnDroppedExternalEdge -34 2 TestProxiedServerGateList -34 2 TestProxiedServerList -34 2 TestProxiedServerServeRemoveDependency -34 2 TestProxiedServerUpdateC +15 2 TestProxiedServerCountIncludeInfra +15 2 TestProxiedServerMolBurn +15 2 TestProxiedServerPing +15 2 TestProxiedServerUpdate +15 2 TestProxiedServerUpdateConcurrentClaim +15 2 TestProxiedServerUpdateIfGuards +15 2 TestProxiedServerWispGC -34 3 TestProxiedServerAutoPrune -34 3 TestProxiedServerClose2B -34 3 TestProxiedServerEventsJournalOffByDefault -34 3 TestProxiedServerIfRevisionCloseMismatchRefuses -34 3 TestProxiedServerMolLastActivity -34 3 TestProxiedServerPurgeWispsPlaneRetention -34 3 TestProxiedServerServeLifecycle -34 3 TestProxiedServerStatuses +15 3 TestProxiedServerBlocked +15 3 TestProxiedServerChildren +15 3 TestProxiedServerDeferUndefer +15 3 TestProxiedServerMolSquash +15 3 TestProxiedServerPour +15 3 TestProxiedServerPriority +15 3 TestProxiedServerUpdate2 +15 3 TestProxiedServerUpdateConcurrentMetadata -34 4 TestProxiedServerMolStale -34 4 TestProxiedServerReadyConcurrent -34 4 TestProxiedServerServeRelatedIssues -34 4 TestProxiedServerServeSettingsWrites -34 4 TestProxiedServerShow2 -34 4 TestServerModeServeSkipsPostRunMaintenance +15 4 TestProxiedServerCascadeParity +15 4 TestProxiedServerClose2 +15 4 TestProxiedServerDeleteConcurrent +15 4 TestProxiedServerDuplicates +15 4 TestProxiedServerMolBond +15 4 TestProxiedServerQuery +15 4 TestProxiedServerUnclaimIfAssigneeParity +15 4 TestProxiedServerWispCreate -34 5 TestProxiedServerHistoryRemoteRefusalFrontDoorMatrix -34 5 TestProxiedServerInfo -34 5 TestProxiedServerNote -34 5 TestProxiedServerOutageReconnectAcceptanceMatrix -34 5 TestProxiedServerUpdate3 -34 5 TestServerModeDeferAutoWake +15 5 TestProxiedServerCreate2 +15 5 TestProxiedServerDepConcurrent +15 5 TestProxiedServerFindDuplicates +15 5 TestProxiedServerMolReadyGated +15 5 TestProxiedServerReadyClaimConcurrent +15 5 TestProxiedServerWispList -34 6 TestProxiedServerPing -34 6 TestProxiedServerQuick -34 6 TestProxiedServerServeReadsTheJournalItJustWrote -34 6 TestProxiedServerSwarm -34 6 TestProxiedServerUpdate2B -34 6 TestProxiedServerVersionedHistorySwitchRefusesAnUnrecordableGateTimeout -34 6 TestProxiedServerWispList +15 6 TestProxiedServerAssign +15 6 TestProxiedServerCleanDatabases +15 6 TestProxiedServerEdit +15 6 TestProxiedServerLink +15 6 TestProxiedServerPrime +15 6 TestProxiedServerReady2 +15 6 TestProxiedServerReadyConcurrent +15 6 TestProxiedServerState -34 7 TestProxiedServerCloseConcurrent -34 7 TestProxiedServerCreateInfraTypeRoutesToWisps -34 7 TestProxiedServerDeleteIfRevisionSingleWinner -34 7 TestProxiedServerExternalCreate -34 7 TestProxiedServerIfRevisionGuardDelete -34 7 TestProxiedServerIfRevisionTargetGoneIsPreconditionFailed -34 7 TestProxiedServerLabelC -34 7 TestProxiedServerMoleculeLifecycleAgreesOnEveryRoute -34 7 TestProxiedServerTypesAgreeWithCreate +15 7 TestProxiedServerClose +15 7 TestProxiedServerCloseConcurrent +15 7 TestProxiedServerExternalCreate +15 7 TestProxiedServerHeartbeat +15 7 TestProxiedServerMolStale +15 7 TestProxiedServerReadyWisp +15 7 TestProxiedServerReclaim +15 7 TestProxiedServerRelate -34 8 TestProxiedServerClose2 -34 8 TestProxiedServerDeleteWisp -34 8 TestProxiedServerExport -34 8 TestProxiedServerFindDuplicates -34 8 TestProxiedServerGateCheck -34 8 TestProxiedServerServeReadParity +15 8 TestProxiedServerCreate +15 8 TestProxiedServerGC +15 8 TestProxiedServerGateCheck +15 8 TestProxiedServerMolLastActivity +15 8 TestProxiedServerPromote +15 8 TestProxiedServerQuick +15 8 TestProxiedServerReopenConcurrent +15 8 TestProxiedServerServeClaim +15 8 TestProxiedServerServeReadParity -34 9 TestProxiedServerCloseSiblingBlockersFanIn -34 9 TestProxiedServerGateLifecycle -34 9 TestProxiedServerHistoryRemoteSupportedFrontDoorParity -34 9 TestProxiedServerIfRevisionGuardReopen -34 9 TestProxiedServerIfRevisionOutranksReassignFence -34 9 TestProxiedServerServeRefusesTheJournalWhenItIsDisabled -34 9 TestProxiedServerUpdate -34 9 TestServerModeServe +15 9 TestProxiedServerBatchParity +15 9 TestProxiedServerDep2 +15 9 TestProxiedServerGraph +15 9 TestProxiedServerMolProgress +15 9 TestProxiedServerPrune +15 9 TestProxiedServerSQL +15 9 TestProxiedServerStatus -34 10 TestProxiedServerContext -34 10 TestProxiedServerCreate2B -34 10 TestProxiedServerGateCheckB -34 10 TestProxiedServerServeRefusesAStreamOnADisabledJournal -34 10 TestProxiedServerTypes -34 10 TestProxiedServerUpdate2C +15 10 TestProxiedServerCompactDolt +15 10 TestProxiedServerConfigSetMany +15 10 TestProxiedServerHistory +15 10 TestProxiedServerLabel +15 10 TestProxiedServerList +15 10 TestProxiedServerSQLDatabaseFlag +15 10 TestProxiedServerSwarm +15 10 TestProxiedServerUpdateWisp -34 11 TestProxiedServerBatchParity -34 11 TestProxiedServerCompactHistory -34 11 TestProxiedServerPour -34 11 TestProxiedServerQuery -34 11 TestProxiedServerReady -34 11 TestProxiedServerServeAddDependencies +15 11 TestProxiedServerCompactHistory +15 11 TestProxiedServerDeleteWisp +15 11 TestProxiedServerEpic +15 11 TestProxiedServerHuman +15 11 TestProxiedServerInfo +15 11 TestProxiedServerMolDistill +15 11 TestProxiedServerReady +15 11 TestProxiedServerSetState -34 12 TestProxiedServerBlocked -34 12 TestProxiedServerChildren -34 12 TestProxiedServerCleanDatabases -34 12 TestProxiedServerReadyB -34 12 TestProxiedServerReadyWisp -34 12 TestProxiedServerReclaim -34 12 TestProxiedServerShowCommentsTail -34 12 TestProxiedServerUnclaim +15 12 TestProxiedServerDep +15 12 TestProxiedServerLint +15 12 TestProxiedServerServeLifecycle +15 12 TestProxiedServerServeModeGate +15 12 TestProxiedServerShow3 +15 12 TestProxiedServerStale +15 12 TestProxiedServerTag -34 13 TestProxiedServerCompactDolt -34 13 TestProxiedServerCreate2 -34 13 TestProxiedServerGraph -34 13 TestProxiedServerHistoryRemoteSupportedDirectParity -34 13 TestProxiedServerMolSquash -34 13 TestProxiedServerPromote -34 13 TestProxiedServerServeRefusesAStaleCheckpointWithGone +15 13 TestProxiedServerConfig +15 13 TestProxiedServerConfigConcurrent +15 13 TestProxiedServerGateList +15 13 TestProxiedServerMemory +15 13 TestProxiedServerMultiStatementSQL +15 13 TestProxiedServerReopen +15 13 TestProxiedServerStatuses -34 14 TestProxiedServerAssign -34 14 TestProxiedServerCloseB -34 14 TestProxiedServerDuplicates -34 14 TestProxiedServerHeartbeat -34 14 TestProxiedServerIfRevisionCloseReplaysMoleculeAutoClose -34 14 TestProxiedServerInitIfMissing -34 14 TestProxiedServerMolReadyGated +15 14 TestProxiedServerComment +15 14 TestProxiedServerContext +15 14 TestProxiedServerKV +15 14 TestProxiedServerNote +15 14 TestProxiedServerSearch +15 14 TestProxiedServerShow +15 14 TestProxiedServerTypes -34 15 TestProxiedServerDepConcurrent -34 15 TestProxiedServerImport -34 15 TestProxiedServerMoleculeFrontDoorWriteParity -34 15 TestProxiedServerReopenConcurrent -34 15 TestProxiedServerSQL - -34 16 TestProxiedServerBackupRefusedOnExternalTopology -34 16 TestProxiedServerCascadeParity -34 16 TestProxiedServerConfigSetMany -34 16 TestProxiedServerKV -34 16 TestProxiedServerServeCount -34 16 TestProxiedServerServeCreate -34 16 TestProxiedServerUpdateHooks - -34 17 TestProxiedServerDep -34 17 TestProxiedServerMultiStatementSQL -34 17 TestProxiedServerOrphans -34 17 TestProxiedServerPrime -34 17 TestProxiedServerSQLDatabaseFlag -34 17 TestProxiedServerServeModeGate -34 17 TestServerModeServeIgnoresGraphModeDependency - -34 18 TestProxiedServerClose3 -34 18 TestProxiedServerCount -34 18 TestProxiedServerCountIncludeInfra -34 18 TestProxiedServerDoltRemoteRemove -34 18 TestProxiedServerMolShow -34 18 TestProxiedServerWispCreateRejectsEnumViolatingVar - -34 19 TestProxiedServerCreate -34 19 TestProxiedServerMolCurrent -34 19 TestProxiedServerServeBatchClose -34 19 TestProxiedServerShow3 -34 19 TestServerModeTypesAgreeWithCreate - -34 20 TestProxiedServerDelete -34 20 TestProxiedServerLint -34 20 TestProxiedServerPriority -34 20 TestProxiedServerShow2B -34 20 TestProxiedServerWispGC -34 20 TestServerModeSharedServeGlobalReportsTheServedDatabase - -34 21 TestProxiedServerBatchDepAddRefusesADottedChildGatedOnItsParent -34 21 TestProxiedServerConfig -34 21 TestProxiedServerExternalPolicyDirectParity -34 21 TestProxiedServerMoleculeFrontDoorDirectServerProxyParity -34 21 TestProxiedServerPathHelpers -34 21 TestProxiedServerPrune -34 21 TestProxiedServerServeClose -34 21 TestProxiedServerUnclaimIfAssigneeParity - -34 22 TestProxiedServerGateCreateInheritsRepoMetadata -34 22 TestProxiedServerRecomputeBlocked -34 22 TestProxiedServerServeEdgeCounts -34 22 TestProxiedServerTag -34 22 TestProxiedServerUpdateB -34 22 TestProxiedServerUpdateConcurrentMetadata - -34 23 TestProxiedServerDep2B -34 23 TestProxiedServerIfRevisionGuardUpdate -34 23 TestProxiedServerListWatchCountsTheOffsetIntoMaxRows -34 23 TestProxiedServerMolBurn -34 23 TestProxiedServerSearchRoutesAgree -34 23 TestProxiedServerServeUpdate -34 23 TestProxiedServerUpdateConcurrentClaim -34 23 TestProxiedServerUpdateWisp - -34 24 TestProxiedServerDeleteBlockedJSONSingleDoc -34 24 TestProxiedServerDeleteForceOrphans -34 24 TestProxiedServerHuman -34 24 TestProxiedServerIfRevisionDeleteMatchAndMismatch -34 24 TestProxiedServerIfRevisionGuardAssign -34 24 TestProxiedServerListComments -34 24 TestProxiedServerListSortRetiresTheWalk -34 24 TestProxiedServerShowWatch -34 24 TestProxiedServerUpdateIfGuards - -34 25 TestProxiedServerInitExplicitBeadsDirIsAuthoritative -34 25 TestProxiedServerSetState -34 25 TestProxiedServerShow -34 25 TestProxiedServerShowB -34 25 TestProxiedServerStale -34 25 TestProxiedServerWispCreate - -34 26 TestProxiedServerDeferAutoWake -34 26 TestProxiedServerExternalMutationPolicy -34 26 TestProxiedServerReopenB -34 26 TestProxiedServerSearch -34 26 TestProxiedServerServeClaim -34 26 TestProxiedServerShow3B - -34 27 TestProxiedServerDeferUndefer -34 27 TestProxiedServerGatedRendering -34 27 TestProxiedServerHistory -34 27 TestProxiedServerLabel -34 27 TestProxiedServerLabelB -34 27 TestProxiedServerUpdateClosePolicy - -34 28 TestProxiedServerDep2C -34 28 TestProxiedServerDoltLifecycle -34 28 TestProxiedServerIfRevisionCloseWarnsOnForcedOpenChildren -34 28 TestProxiedServerReady2 -34 28 TestProxiedServerReassignFence -34 28 TestProxiedServerSQLStatementRouting -34 28 TestProxiedServerServeClaimNext -34 28 TestProxiedServerUpdateConcurrentAppendNotes - -34 29 TestProxiedServerGateFrontDoorDirectServerProxyParity -34 29 TestProxiedServerMolDistill -34 29 TestProxiedServerReopen -34 29 TestProxiedServerServeDelete -34 29 TestProxiedServerState - -34 30 TestProxiedServerDeleteConcurrent -34 30 TestProxiedServerReadyC -34 30 TestProxiedServerServeAddComment -34 30 TestProxiedServerStatus -34 30 TestProxiedServerTodo -34 30 TestProxiedServerUpdate2 -34 30 TestProxiedServerVersionedHistoryServeActivatesTheProviderFromItsOwnRow - -34 31 TestProxiedServerCloseBeadGate -34 31 TestProxiedServerComment -34 31 TestProxiedServerConfigB -34 31 TestProxiedServerEventsJournal -34 31 TestProxiedServerGateAddWaiterConvoy -34 31 TestProxiedServerIfRevisionGuardClose -34 31 TestProxiedServerMolBond - -34 32 TestProxiedServerCleanDatabasesPurgeDropped -34 32 TestProxiedServerConfigConcurrent -34 32 TestProxiedServerDepB -34 32 TestProxiedServerServeStreamsTheJournalItIsWriting -34 32 TestProxiedServerServeUpdateClaim -34 32 TestProxiedServerStorageClass - -34 33 TestProxiedServerBatchDefersThenDoltCommitAdvancesHeadOnce -34 33 TestProxiedServerClose -34 33 TestProxiedServerGC -34 33 TestProxiedServerMemory -34 33 TestProxiedServerPourRejectsEnumViolatingVar -34 33 TestProxiedServerRelate - -34 34 TestProxiedServerClose4 -34 34 TestProxiedServerCreate2C -34 34 TestProxiedServerEdit -34 34 TestProxiedServerEpic -34 34 TestProxiedServerLink -34 34 TestProxiedServerServeReadRevision +15 15 TestProxiedServerMolShow +15 15 TestProxiedServerOrphans +15 15 TestProxiedServerRecomputeBlocked +15 15 TestProxiedServerShow2 +15 15 TestProxiedServerUnclaim +15 15 TestProxiedServerUpdate3 +15 15 TestProxiedServerUpdateHooks diff --git a/.github/scripts/proxied-test-shard.sh b/.github/scripts/proxied-test-shard.sh index 0c0c3f7c6..1c23774af 100755 --- a/.github/scripts/proxied-test-shard.sh +++ b/.github/scripts/proxied-test-shard.sh @@ -3,24 +3,13 @@ # # Usage: proxied-test-shard.sh # -# Discovers all TestProxiedServer* and TestServerMode* top-level functions -# across cmd/bd/*_test.go (the proxied integration suites plus -# TestProxiedServerExternalCreate and the TestProxiedServerPathHelpers unit -# test, widening the lane's historical `-run '^TestProxiedServer'`). The -# TestServerMode* suites all gate on requireSharedProxiedServer and drive the -# same shared dolt sql-server, and this lane is the only one that sets -# BEADS_TEST_PROXIED_SERVER=1, so it is the only place they run: before the -# widening a top-level TestServerMode* entry point matched no discovery pattern -# here, so it skipped everywhere else and was never executed by CI at all. -# The TestProxiedServer* side is mixed and should not be generalized over: most -# gate on requireSharedProxiedServer, the rest on requireProxiedServerEnv, and -# TestProxiedServerPathHelpers (pure path helpers) has no gate and no build tag, -# so that one also runs in every ordinary `go test ./cmd/bd/` lane. -# -# Tests listed in proxied-cmd-test-shards.txt for the requested shard count use -# that committed assignment; newly-added tests fall back to hash(name) % total. -# Runs the matching subset using the pre-built test binary at -# BEADS_TEST_CMD_BINARY (or /tmp/bd-cmd-test). +# Discovers all TestProxiedServer* top-level functions across cmd/bd/*_test.go +# (the proxied integration suites plus TestProxiedServerExternalCreate and the +# TestProxiedServerPathHelpers unit test, matching the lane's historical +# `-run '^TestProxiedServer'`). Tests listed in proxied-cmd-test-shards.txt for +# the requested shard count use that committed assignment; newly-added tests +# fall back to hash(name) % total. Runs the matching subset using the pre-built +# test binary at BEADS_TEST_CMD_BINARY (or /tmp/bd-cmd-test). # # Environment: # BEADS_TEST_PROXIED_SERVER=1 required (tests skip without it) @@ -47,17 +36,16 @@ fi SHARD_INDEX=$(( SHARD_NUMBER - 1 )) MANIFEST="${BEADS_TEST_SHARD_MANIFEST:-.github/scripts/proxied-cmd-test-shards.txt}" -# Discover all top-level TestProxiedServer*/TestServerMode* functions across -# every cmd/bd test file (integration suites live in -# *_proxied_integration_test.go, but TestProxiedServerExternalCreate, -# TestProxiedServerPathHelpers and the TestServerMode* suites do not, so match -# by name rather than by file glob). -ALL_TESTS=$(grep -rhE '^func Test(ProxiedServer|ServerMode)' cmd/bd/*_test.go \ - | sed -E 's/func (Test(ProxiedServer|ServerMode)[A-Za-z0-9_]*).*/\1/' \ +# Discover all top-level TestProxiedServer* functions across every cmd/bd test +# file (integration suites live in *_proxied_integration_test.go, but +# TestProxiedServerExternalCreate and TestProxiedServerPathHelpers do not, so +# match by name rather than by file glob). +ALL_TESTS=$(grep -rh '^func TestProxiedServer' cmd/bd/*_test.go \ + | sed 's/func \(TestProxiedServer[A-Za-z0-9_]*\).*/\1/' \ | sort -u) if [ -z "$ALL_TESTS" ]; then - echo "No TestProxiedServer*/TestServerMode* functions found" >&2 + echo "No TestProxiedServer* functions found" >&2 exit 1 fi @@ -74,7 +62,7 @@ if [ -f "$MANIFEST" ]; then if [ -z "${manifest_total:-}" ]; then continue fi - if [ -n "${extra:-}" ] || ! [[ "$manifest_total" =~ ^[0-9]+$ ]] || ! [[ "$manifest_shard" =~ ^[0-9]+$ ]] || ! [[ "$test_name" =~ ^Test(ProxiedServer|ServerMode)[A-Za-z0-9_]+$ ]]; then + if [ -n "${extra:-}" ] || ! [[ "$manifest_total" =~ ^[0-9]+$ ]] || ! [[ "$manifest_shard" =~ ^[0-9]+$ ]] || ! [[ "$test_name" =~ ^TestProxiedServer[A-Za-z0-9_]+$ ]]; then echo "Invalid shard manifest line: $line" >&2 exit 1 fi diff --git a/.github/scripts/rrc-lane-commands.txt b/.github/scripts/rrc-lane-commands.txt deleted file mode 100644 index 331e18657..000000000 --- a/.github/scripts/rrc-lane-commands.txt +++ /dev/null @@ -1,17 +0,0 @@ -# Every bazel.yml lane's loading and analysis, for the remote repo contents -# cache: rrc-seed runs each line with --nobuild on a push to main (uploading -# the repositories it fetches) and rrc-verify runs each cold every night. -# One `bazel` command per line (no shell quoting); # starts a comment. -# scripts/bazel_rrc_test.go checks that every --config a reading lane's -# bazel command uses appears here. -test //... --config=ci -test //... --config=embedded -test //... --config=integration -test //... --config=doltserver -test //... --config=doltserver-proxied -test //... --config=dolt-race -test //... --config=doltserver-integration -test //... --config=doltserver-cmd -test //... --config=dolt-int -build --config=pure //cmd/bd:bd //cmd/bd:bd_test //internal/storage/embeddeddolt:embeddeddolt_test //internal/tracker:tracker_test -build --config=js-wasm //internal/hooks:hooks_test diff --git a/.github/scripts/rrc-run-lanes.sh b/.github/scripts/rrc-run-lanes.sh deleted file mode 100755 index d5f329a84..000000000 --- a/.github/scripts/rrc-run-lanes.sh +++ /dev/null @@ -1,51 +0,0 @@ -#!/usr/bin/env bash -# Loading and analysis of every bazel.yml lane, for the remote repo contents -# cache (bazel.yml's rrc-seed and rrc-verify jobs). -# -# Usage: rrc-run-lanes.sh [STARTUP_FLAG...] -- [COMMAND_FLAG...] -# -# Runs `bazel STARTUP_FLAG... --nobuild COMMAND_FLAG...` for each -# command line in .github/scripts/rrc-lane-commands.txt (or RRC_LANE_COMMANDS), -# in order, stopping at the first failure. --nobuild: repository rules, -# loading and analysis only, never a build or test action. -# -# Bazel exits 1 after `test --nobuild` even when loading and analysis succeed -# ("Couldn't start the build. Unable to run tests"). A command passes when it -# exits 0, or exits non-zero having printed "Build completed successfully" -# and no ERROR line but that one. -set -euo pipefail - -startup=() -while [ $# -gt 0 ] && [ "$1" != -- ]; do - startup+=("$1") - shift -done -[ $# -gt 0 ] || { - echo "usage: rrc-run-lanes.sh [STARTUP_FLAG...] -- [COMMAND_FLAG...]" >&2 - exit 2 -} -shift -flags=("$@") - -file=${RRC_LANE_COMMANDS:-$(dirname "${BASH_SOURCE[0]}")/rrc-lane-commands.txt} -mapfile -t cmds < <(sed -e 's/#.*//' -e 's/[[:space:]]*$//' "$file" | grep -v '^[[:space:]]*$' || true) -if [ "${#cmds[@]}" -eq 0 ]; then - echo "::error::no lane commands in $file" - exit 1 -fi - -log=$(mktemp) -trap 'rm -f "$log"' EXIT -summary=${GITHUB_STEP_SUMMARY:-/dev/null} -for cmd in "${cmds[@]}"; do - read -r -a args <<<"$cmd" - start=$(date +%s) - rc=0 - bazel ${startup[@]+"${startup[@]}"} "${args[@]}" --nobuild ${flags[@]+"${flags[@]}"} &1 | tee "$log" || rc=$? - if [ "$rc" -ne 0 ] && grep -q '^INFO: Build completed successfully' "$log" && - ! grep '^ERROR:' "$log" | grep -qv "^ERROR: Couldn't start the build. Unable to run tests$"; then - rc=0 - fi - echo "rrc: \`bazel $cmd\`: exit $rc, $(($(date +%s) - start))s" | tee -a "$summary" - [ "$rc" -eq 0 ] || exit "$rc" -done diff --git a/.github/scripts/rrc-writer-credential.sh b/.github/scripts/rrc-writer-credential.sh deleted file mode 100755 index a650c7ddc..000000000 --- a/.github/scripts/rrc-writer-credential.sh +++ /dev/null @@ -1,113 +0,0 @@ -#!/usr/bin/env bash -# A 30-minute rbe-west client certificate for the remote repo contents cache -# writer (bazel.yml's rrc-seed job; engdocs/design/bazel-remote-repo-contents-cache.md). -# -# The job trades its GitHub Actions OIDC token (audience rbe-rrc-writer) for a -# certificate CN=rbe-rrc-writer from rbe-west's mint. The mint signs only for -# this repository's bazel.yml on a push to main (repository, ref, event_name -# and job_workflow_ref claims); pull_request, fork and Dependabot runs cannot -# get one (no OIDC token, or a refs/pull/N/merge one). On rbe-west that -# certificate reaches only GetCapabilities, the CAS and an action cache front -# (rrc-gate) that admits repo-contents entries and refuses everything else, so -# it can never write an execution result or execute anything. -# -# The key is an EC P-256 key (PKCS#8: Bazel's Netty TLS refuses a SEC1 "EC -# PRIVATE KEY") generated here in BAZEL_CI_SECRET_DIR, outside the workspace; -# it never leaves the runner. The OIDC token is masked. Outputs (to -# $GITHUB_OUTPUT when set, else stdout): cert, key, endpoint, instance; none -# when the mint answers 503 or cannot be reached (seeding switched off on -# rbe-west: infra nativelink-cas/west/README.md "Client contract"), which -# exits 0 with a warning. -# -# Tests only: RBE_RRC_MINT (a stand-in mint) and RBE_RRC_ENDPOINT_RE. -set -euo pipefail -MINT=${RBE_RRC_MINT:-https://rbe-mint.ops.gascity.com:8444} -ENDPOINT_RE=${RBE_RRC_ENDPOINT_RE:-'^grpcs://rbe-west\.ops\.gascity\.com(:443)?$'} -AUDIENCE=rbe-rrc-writer -dir=${BAZEL_CI_SECRET_DIR:?BAZEL_CI_SECRET_DIR is required} -out=${GITHUB_OUTPUT:-/dev/stdout} - -if [ -z "${ACTIONS_ID_TOKEN_REQUEST_URL:-}" ] || [ -z "${ACTIONS_ID_TOKEN_REQUEST_TOKEN:-}" ]; then - echo "::error title=rrc writer::no OIDC token request URL; the job needs permissions: id-token: write" >&2 - exit 1 -fi - -install -d -m 0700 "$dir" -umask 077 - -oidc=$(curl -sS --fail --connect-timeout 5 --max-time 30 --retry 3 --retry-all-errors \ - -H "Authorization: bearer $ACTIONS_ID_TOKEN_REQUEST_TOKEN" \ - "$ACTIONS_ID_TOKEN_REQUEST_URL&audience=$AUDIENCE" | jq -r '.value // empty') -if [ -z "$oidc" ]; then - echo "::error title=rrc writer::GitHub returned no OIDC token" >&2 - exit 1 -fi -printf '::add-mask::%s\n' "$oidc" - -openssl genpkey -algorithm EC -pkeyopt ec_paramgen_curve:P-256 -out "$dir/rrc-writer.key" 2>/dev/null -csr=$(openssl req -new -key "$dir/rrc-writer.key" -subj "/CN=rbe-rrc-writer-request") -body=$(jq -cn --arg csr "$csr" '{csr_pem: $csr}') - -reply="$dir/rrc-mint.json" -code=000 -for attempt in 1 2 3 4; do - # --connect-timeout: a closed gate drops the SYN; fail fast, not in 60 s. - code=$(curl -sS -o "$reply" -w '%{http_code}' --connect-timeout 5 --max-time 60 \ - -H "Authorization: Bearer $oidc" -H 'content-type: application/json' \ - --data "$body" "$MINT/v1/rrc-writer/cert") || code=000 - # Retry what may pass on its own: a rate limit (429), the mint could not - # reach GitHub's JWKS (502), or the network (000). Everything else is an - # answer. - case "$code" in 429 | 502 | 000) ;; *) break ;; esac - [ "$attempt" -eq 4 ] || sleep $((attempt * 10)) -done -case "$code" in -200) ;; -503 | 000) - # The mint's switch is off, its CA is not installed, or rbe-west's fork - # edge is closed: seeding is off, not broken. No outputs, so the seed - # step skips; the push to main never fails on it. - echo "::warning title=rrc writer: seeding off (HTTP $code)::$(jq -r '.error // empty' "$reply" 2>/dev/null || true)" - exit 0 - ;; -*) - echo "::error title=rrc writer mint refused (HTTP $code)::$(jq -r '.error // empty' "$reply" 2>/dev/null || true)" - exit 1 - ;; -esac - -endpoint=$(jq -r '.endpoint // empty' "$reply") -instance=$(jq -r '.instance // empty' "$reply") -[[ $endpoint =~ $ENDPOINT_RE ]] || { - echo "::error::rrc writer mint returned endpoint '$endpoint'" >&2 - exit 1 -} -[ "$instance" = oss ] || { - echo "::error::rrc writer mint returned instance '$instance', want oss" >&2 - exit 1 -} -jq -r '.cert_pem // empty' "$reply" >"$dir/rrc-writer.crt" -subject=$(openssl x509 -in "$dir/rrc-writer.crt" -noout -subject -nameopt RFC2253) || { - echo "::error::rrc writer mint returned no certificate" >&2 - exit 1 -} -rdns=$(tr ',' '\n' <<<"${subject#subject=}") -cn=$(sed -n 's/^CN=//p' <<<"$rdns") -org=$(sed -n 's/^O=//p' <<<"$rdns") -if ! [[ $cn =~ ^rbe-rrc-writer(-[a-z0-9]+)?$ ]] || [ "$org" != gascity ]; then - echo "::error::rrc writer mint returned a certificate for '$subject'" >&2 - exit 1 -fi -# The certificate must belong to the key generated above. -if [ "$(openssl x509 -in "$dir/rrc-writer.crt" -noout -pubkey | openssl sha256)" != "$(openssl pkey -in "$dir/rrc-writer.key" -pubout | openssl sha256)" ]; then - echo "::error::rrc writer mint returned a certificate for another key" >&2 - exit 1 -fi -openssl x509 -in "$dir/rrc-writer.crt" -noout -subject -enddate -rm -f "$reply" -{ - echo "cert=$dir/rrc-writer.crt" - echo "key=$dir/rrc-writer.key" - echo "endpoint=$endpoint" - echo "instance=$instance" -} >>"$out" diff --git a/.github/scripts/server-storage-test-shards.txt b/.github/scripts/server-storage-test-shards.txt index 16355e29e..e053f0d82 100644 --- a/.github/scripts/server-storage-test-shards.txt +++ b/.github/scripts/server-storage-test-shards.txt @@ -2,10 +2,13 @@ # # Format: # -# TestCloudAuthCLIRouting uses one shared outer store (#5841); its -# environment-sensitive subtests run serially. #6396 checks that remote -# writes use the shared fixture without imposing a machine-speed threshold. -# It remains pinned alone on shard 1. The remaining pins below +# Unlike the embedded manifest, per-test timing is known for the tests +# pinned below (see be-extn's investigation of CI job 95616282508): +# TestCloudAuthCLIRouting alone costs ~9.5 minutes -- its 16 subtests each +# call openCloudAuthTestStore, which does CREATE DATABASE + the full +# migration chain + dolt init + dolt remote add + DROP DATABASE, and the +# test uses t.Setenv so it can never run t.Parallel. It is pinned alone on +# shard 1 so no other test shares its runtime. The remaining pins below # (TestCloudAuthCLIRoutingStructural, TestCreateGuard_* (9 tests), # TestFederationPeerCredentialLifecycleLazyKeyInit, # TestConcurrentWorkQueueDrain, TestHighContentionStress, diff --git a/.github/workflows/bazel-autofix.yml b/.github/workflows/bazel-autofix.yml deleted file mode 100644 index acd5b7ff3..000000000 --- a/.github/workflows/bazel-autofix.yml +++ /dev/null @@ -1,160 +0,0 @@ -# Auto-fix stale Bazel BUILD files on PRs. -# -# bazel.yml's "BUILD files in sync" step runs `make bazel-sync` (gazelle, -# tools/bazel/go_srcs.py, bazel mod tidy) and, when that leaves drift, fails -# and uploads the BUILD.bazel / MODULE.bazel / MODULE.bazel.lock part of the -# fix as the bazel-sync-patch artifact (scripts/ci/bazel-sync-patch.sh). This -# workflow closes the loop, like docs-autofix.yml does for generated CLI docs: -# when a PR run fails and left that artifact, it pushes the sync commit to the -# PR branch (same-repo PRs) or leaves an idempotent comment with the apply -# recipe (fork PRs). Drift elsewhere (third_party/patches, go.mod) is never -# patched; the failing run lists it for a human. -# -# Trigger: only "PR" (pr.yml). bazel.yml has no pull_request trigger of its -# own: PRs run it through pr.yml's `bazel` job (workflow_call), so its -# bazel-sync-patch upload belongs to the PR run, and a failing sync step -# fails that run. The "Bazel" workflow's own runs (push to main, dispatch) -# are never pull_request events, so triggering on them would only ever be a -# no-op. A PR run without the artifact (it failed for other reasons) is a -# no-op too. bazel.yml builds the PR MERGE commit, so the patch also -# carries any drift main has; only files in packages the PR itself changed -# are pushed (see the script), so main's drift never lands on an unrelated PR. -# -# CONCURRENCY: job-level (a skipped run - success, or another event - never -# takes part), and shared with docs-autofix.yml per head branch without -# cancel-in-progress, so the two bots queue instead of racing on one branch -# or cancelling a fix that is already running. The second one sees the moved -# head and stops; with the PAT its push retriggers CI, which re-derives the -# other fix. Both bots now fire on the same PR run's completion, so one runs -# while the other waits. GitHub keeps one pending job per group: a later PR -# run finishing inside that short window can displace a waiting job, whose -# fix the next failing run re-derives. -# -# SECURITY MODEL: this runs with write permissions via workflow_run, so it -# never checks out or executes PR code. It checks out the base default branch -# for scripts, and treats the artifact strictly as data: only the two expected -# regular files are extracted, every path in the patch must match an anchored -# Bazel build-file allowlist (no hidden or `..` segments, nothing under -# third_party/), mode/symlink/rename/binary hunks are refused, and -# the patch is applied to an index in a bare clone (the PR tree never touches -# the disk) and re-checked once staged (scripts/bazel-autofix-push.sh), so a -# hostile patch can at most rewrite BUILD.bazel / MODULE.bazel / -# MODULE.bazel.lock on its own PR branch. Pushes are leased to the run's head -# SHA and skipped for heads that are protected or under a ruleset. The App -# private key is a secret of the `autofix` environment, whose deployment- -# branch policy allows only `main`, so only a job running on main can read -# it; the minted token itself is scoped to this one repo with contents:write -# only, so even a hostile patch gains nothing beyond what the job already -# had. -# -# TOKEN NOTE: pushes made with the default GITHUB_TOKEN do not retrigger PR -# checks. On same-repo PRs the push step mints a short-lived installation -# token for the gastownhall-autofix GitHub App (contents:write, scoped to -# this repo only) and uses that instead. Fork PRs always get the apply-recipe -# comment - no token we hold can push to a fork, so the mint step is skipped -# for them. workflow_run always runs this file from the default branch, so -# edits take effect only after they merge. - -name: Bazel Autofix - -on: - workflow_run: - workflows: ["PR"] - types: [completed] - -permissions: - contents: write - pull-requests: write - actions: read - -jobs: - autofix: - name: Apply Bazel BUILD sync to PR - if: > - github.event.workflow_run.event == 'pull_request' && - github.event.workflow_run.conclusion == 'failure' - runs-on: ubuntu-latest - timeout-minutes: 10 - # Grants the job read access to this environment's secrets; its - # deployment-branch policy restricts that to jobs running on main - # (workflow_run always does), not to any PR branch. - environment: autofix - # Shared with docs-autofix.yml; queue, never cancel (see header). - concurrency: - group: autofix-${{ github.event.workflow_run.head_repository.full_name }}-${{ github.event.workflow_run.head_branch }} - cancel-in-progress: false - steps: - # Trusted side only: base repo default branch, never the PR head. The - # workflow token is not left in .git/config. - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 - with: - persist-credentials: false - - - name: Download BUILD sync patch (if any) - id: patch - env: - GH_TOKEN: ${{ github.token }} - RUN_ID: ${{ github.event.workflow_run.id }} - run: | - artifact_ids="$(gh api --paginate "repos/${GITHUB_REPOSITORY}/actions/runs/${RUN_ID}/artifacts?per_page=100" \ - --jq '.artifacts[] | select(.name == "bazel-sync-patch") | .id')" - artifact_id="$(printf '%s\n' "$artifact_ids" | head -1)" - if [ -z "$artifact_id" ]; then - echo "No BUILD sync patch artifact on run ${RUN_ID}; it failed for other reasons." - echo "found=false" >> "$GITHUB_OUTPUT" - exit 0 - fi - dir="${RUNNER_TEMP}/bazel-patch" - mkdir -p "$dir" - gh api "repos/${GITHUB_REPOSITORY}/actions/artifacts/${artifact_id}/zip" > "${RUNNER_TEMP}/bazel-patch.zip" - # Extract only the two expected names, and only as regular files. - unzip -o "${RUNNER_TEMP}/bazel-patch.zip" bazel-sync.patch bazel-sync-meta.txt -d "$dir" || true - ls -la "$dir" - for f in bazel-sync.patch bazel-sync-meta.txt; do - if [ -L "$dir/$f" ] || { [ -e "$dir/$f" ] && [ ! -f "$dir/$f" ]; }; then - echo "::error::artifact entry $f is not a regular file" - exit 1 - fi - done - if [ ! -f "$dir/bazel-sync.patch" ]; then - echo "Artifact has no bazel-sync.patch; nothing to do." - echo "found=false" >> "$GITHUB_OUTPUT" - exit 0 - fi - echo "found=true" >> "$GITHUB_OUTPUT" - - # Same-repo PRs only: no token we hold can push to a fork, and a fork's - # head_repository can even be absent (deleted fork), so this mints - # nothing for it and the push script falls back to the apply-recipe - # comment. app-token outputs are empty (not failed) when the step is - # skipped, which is what the fallback below relies on. - - name: Mint autofix app token - id: app-token - if: github.event.workflow_run.head_repository.full_name == github.repository - uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3 - with: - app-id: ${{ secrets.AUTOFIX_APP_ID }} - private-key: ${{ secrets.AUTOFIX_APP_PRIVATE_KEY }} - owner: ${{ github.repository_owner }} - repositories: ${{ github.event.repository.name }} - permission-contents: write - - - name: Push sync commit or leave apply recipe - if: steps.patch.outputs.found == 'true' - env: - BASE_REPO: ${{ github.repository }} - HEAD_REPO: ${{ github.event.workflow_run.head_repository.full_name }} - HEAD_BRANCH: ${{ github.event.workflow_run.head_branch }} - HEAD_SHA: ${{ github.event.workflow_run.head_sha }} - PATCH_FILE: ${{ runner.temp }}/bazel-patch/bazel-sync.patch - META_FILE: ${{ runner.temp }}/bazel-patch/bazel-sync-meta.txt - RUN_ID: ${{ github.event.workflow_run.id }} - RUN_URL: ${{ github.event.workflow_run.html_url }} - # Split tokens: gh api calls (PR lookup, comments) always use the - # workflow token, which carries this job's pull-requests:write; the - # App token is used ONLY for the git push, so it needs no more - # than contents:write. - GH_TOKEN: ${{ github.token }} - PUSH_TOKEN: ${{ steps.app-token.outputs.token || github.token }} - AUTOFIX_TOKEN_KIND: ${{ steps.app-token.outputs.token && 'app' || 'default' }} - run: ./scripts/bazel-autofix-push.sh diff --git a/.github/workflows/bazel-farm.yml b/.github/workflows/bazel-farm.yml deleted file mode 100644 index caeb0ad2f..000000000 --- a/.github/workflows/bazel-farm.yml +++ /dev/null @@ -1,153 +0,0 @@ -# Bazel lanes on the remote-execution farm for fork PRs by trusted authors. -# -# Fork PRs get no Actions secrets, so pr.yml's Bazel call runs them locally -# with rbe-west's anonymous read-only cache (mode cache: no embedded, -# proxied-server or server-storage tier). This workflow gives the farm to -# fork PRs whose -# author AND triggering user are on .github/bazel-farm-allowlist.txt (by -# numeric user id), by calling bazel.yml from pull_request_target, which has -# the secrets. -# -# ADVISORY. pr.yml is unchanged: every fork PR, listed or not, still runs the -# cache-mode Bazel lanes (local execution, read-only cache) inside the -# enforced `CI Gate / Required`. This run adds a fast remote signal, -# including the remote-only embedded, proxied-server and server-storage -# tiers, that no required check and no privileged workflow -# reads. Making it authoritative needs a trusted reporter; see -# engdocs/CI_REQUIRED_CHECK_TOPOLOGY.md, "Trusted-author fork PRs". -# -# SECURITY MODEL (policy-tested in scripts/ci_workflow_test.go, -# TestBazelFarmWorkflowSecurity; threat model in the engdoc): -# - pull_request_target runs THIS file, and bazel.yml (`uses: ./`, resolved -# from the same commit), from the base branch: a PR cannot edit either -# for its own run. -# - authorize runs no PR code: a sparse checkout of the run's own base -# commit (github.sha) holding only the allowlist and the decision script, -# no token on disk, no secrets, and event facts passed through env, never -# interpolated into a script. It requires the PR author and the sender -# (the pusher on synchronize) to be listed, so a collaborator on a listed -# author's fork, or someone opening a PR from that fork, gets nothing, and -# the head repository's owner to be the author (no PR from a third -# party's fork). -# - farm hands bazel.yml exactly the four RBE secrets and the event's head -# SHA, which every lane checks out pinned (a later push is a new event, -# judged on its own sender), without persisted credentials. The token is -# contents: read everywhere, so PR code holds no write access. -# - Accepted: a listed author's PR code runs with the RBE client -# certificate, as a same-repo PR's does. -# - Caches: this run is in the default branch's cache scope. GitHub's cache -# is read-only for pull_request_target, and nothing here may declare a -# cache-mode other than read or none (policy-tested). Whether Blacksmith's -# colocated cache enforces that is unverified. Either way, trusted runs -# verify what they restore: the Bazel binary is never cached, the -# repository cache is content-addressed, the repo contents cache is off, -# and go mod verify runs after the module cache restore (see the engdoc). -# - No artifact from this run is consumed: bazel-autofix.yml reads only -# "PR" runs of pull_request events, and bazel.yml skips its sync-patch -# upload here. -# -# Re-runs replay the original event: the same pinned head SHA, and the -# allowlist at the original base commit. De-listing a user therefore also -# means cancelling/deleting their old farm runs, or rotating the RBE -# certificate (engdoc: "Re-runs, Adding and Removing a User"). -# -# Workflow edits take effect only once merged to the default branch. -name: Bazel Farm (trusted forks) - -on: - pull_request_target: - # Not reopened or ready_for_review: they re-test a head no listed user - # necessarily pushed (see the script). - types: [ opened, synchronize ] - branches: [ main ] - -# One run per PR; a newer event supersedes (its sender is judged afresh). -# Differs from bazel.yml's group (bazel-pull_request_target-). -concurrency: - group: bazel-farm-${{ github.event.pull_request.number }} - cancel-in-progress: true - -permissions: - contents: read - -jobs: - authorize: - name: authorize - runs-on: ubuntu-latest - timeout-minutes: 5 - # Cheap prefilter; the script re-checks these and decides on the list. - if: >- - github.event.pull_request.base.repo.full_name == github.repository && - github.event.pull_request.head.repo.full_name != github.repository - permissions: - contents: read - outputs: - allowed: ${{ steps.decide.outputs.allowed }} - steps: - # The base commit this run's workflow file came from; never the PR. - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - with: - ref: ${{ github.sha }} - persist-credentials: false - sparse-checkout: | - .github/bazel-farm-allowlist.txt - .github/scripts/bazel-farm-authorize.sh - sparse-checkout-cone-mode: false - - - name: Check author and sender against the allowlist - id: decide - env: - ALLOWLIST: .github/bazel-farm-allowlist.txt - EVENT_NAME: ${{ github.event_name }} - ACTION: ${{ github.event.action }} - REPOSITORY: ${{ github.repository }} - BASE_REPO: ${{ github.event.pull_request.base.repo.full_name }} - HEAD_REPO: ${{ github.event.pull_request.head.repo.full_name }} - HEAD_OWNER_ID: ${{ github.event.pull_request.head.repo.owner.id }} - BASE_REF: ${{ github.event.pull_request.base.ref }} - DEFAULT_BRANCH: ${{ github.event.repository.default_branch }} - HEAD_SHA: ${{ github.event.pull_request.head.sha }} - PR_AUTHOR: ${{ github.event.pull_request.user.login }} - PR_AUTHOR_ID: ${{ github.event.pull_request.user.id }} - SENDER: ${{ github.event.sender.login }} - SENDER_ID: ${{ github.event.sender.id }} - run: bash .github/scripts/bazel-farm-authorize.sh - - # Not "Bazel": pr.yml's call is named that, and this run's lane results are - # PR-controlled, so its check names ("Bazel Farm / ") must never - # collide with ones a ruleset might require. - farm: - name: Bazel Farm - needs: authorize - if: ${{ needs.authorize.outputs.allowed == 'true' }} - # A called workflow's job gets at most this call's token permissions, and - # GitHub checks them when the run starts, even for a job whose if: skips - # it (startup_failure otherwise; gascity #7478): bazel.yml's rrc-seed asks - # id-token: write (push to main only) and rrc-verify issues: write - # (schedule and dispatch only). Neither runs for this event, every other - # bazel.yml job keeps contents: read, and rbe-west's mint refuses any OIDC - # token but a main-push bazel.yml one - # (scripts/bazel_rrc_test.go, TestBazelWorkflowCallersGrantEveryRequestedPermission). - permissions: - contents: read - id-token: write - issues: write - uses: ./.github/workflows/bazel.yml - # Exactly the four RBE secrets: never the rbe-prewarm app private key - # (B1, security review of bdef342d5). This pull_request_target run is - # the one caller whose code under test is an untrusted fork's, so it - # must never carry the credential that mints gastownhall/gascity - # Actions tokens; pre-warming is accepted as a same-repo-only benefit - # (bazel.yml's rbe-prewarm job also gates on mode remote only, which a - # fork-authorized farm run never reaches). See "rbe-west Pre-warm", - # engdocs/CI_REQUIRED_CHECK_TOPOLOGY.md. - secrets: - RBE_WEST_EXECUTOR: ${{ secrets.RBE_WEST_EXECUTOR }} - RBE_TLS_CERT: ${{ secrets.RBE_TLS_CERT }} - RBE_TLS_KEY: ${{ secrets.RBE_TLS_KEY }} - RBE_TLS_CA: ${{ secrets.RBE_TLS_CA }} - with: - checkout-sha: ${{ github.event.pull_request.head.sha }} - fork-farm: authorized - integration: "on" - build-artifact-name: bazel-farm-build-artifacts diff --git a/.github/workflows/bazel.yml b/.github/workflows/bazel.yml deleted file mode 100644 index b319ab300..000000000 --- a/.github/workflows/bazel.yml +++ /dev/null @@ -1,3242 +0,0 @@ -# The Bazel lanes: the only CI run of the Linux Go test tiers. Runs -# `bazel test //... --config=ci`, the PR-core selection (race, -short, dolt -# skip), plus one job per tier (pure-Go and js/wasm, the release -# cross-compile, the dolt-server lane, the embedded-Dolt, proxied-server and -# server-Dolt storage tiers that pr-risk.yml used to run, integration, the -# cmd/bd Dolt-server tier). -# -# PRs and merge groups run it through pr.yml's `bazel` job (workflow_call). -# The legacy go test jobs these lanes mirrored are retired (ga-96smfk.22), -# so pr.yml's ci-gate requires the lanes to have run remotely on rbe-west -# and passed on every pull_request and merge group (BAZEL_REMOTE): modes -# skip, local and cache are red there (scripts/pr_risk_bazel_coverage_test.go). -# Where a lane executes remotely its `bazel test`s add --config=sole-run: it -# is the tier's only run, so a remote cache eviction fails it rather than -# re-running the invocation (whose retry could turn a failed test green). -# Test results are cached (read and written) on every run but nightly's: a -# test whose action key is unchanged reuses the PASS the farm recorded for -# it, on a PR, a merge group (merge queue) or a push to main alike, and only -# what changed runs. nightly.yml passes fresh-test-results: true, so every -# `bazel test` here appends --config=fresh (BAZEL_FRESH below; -# --nocache_test_results) and re-executes every test once a night, which -# keeps sampling flaky and -race failures. A cached failure is never reused. -# Policy: scripts/ci_merge_queue_test.go. -# This file has no pull_request or merge_group trigger of its own, so a PR -# runs the lane once. Every lane reports its job.status as a workflow_call -# output (named after the job) from an always() last step. pr.yml's ci-gate -# requires the call's aggregate result (BAZEL) and bazel-test, bazel-pure, -# bazel-release-cross, bazel-embedded, bazel-integration, bazel-doltserver, -# bazel-proxied and bazel-server-storage (BAZEL_TEST, BAZEL_PURE, -# BAZEL_RELEASE_CROSS, BAZEL_EMBEDDED, BAZEL_INTEGRATION, BAZEL_DOLTSERVER, -# BAZEL_PROXIED, BAZEL_SERVER_STORAGE); -# bazel-cmd-dolt (the cmd/bd Dolt-server tier, BAZEL_CMD_DOLT) is advisory: -# job-level continue-on-error, and pr.yml requires it only while its -# BAZEL_CMD_DOLT_REQUIRED flag is "true" (pr.yml then also passes -# cmd-dolt-required: true, and bazel-integration leaves //cmd/bd:bd_test, -# which bazel-cmd-dolt runs too, to it); -# .github/scripts/bazel-gate.sh turns the rbe job's rbe-mode output into the -# skips it accepts, so a lane skipped by its `if:` below passes and one that -# should have run and failed, was cancelled or reported nothing fails the -# gate. bazel-integration runs in the PR call (pr.yml passes integration: -# "on") although its legacy counterparts, main.yml's integration jobs, run -# only on push to main: remotely it is minutes, and it catches on the PR what -# main.yml would catch after the merge. Fork and Dependabot PRs run it -# remotely in modes fork-ro/fork-rw like every lane, and in mode cache -# (rbe-fork closed) unlike the remote-only lanes: with the read-only cache -# only what the PR changed is built and tested on the runner, and it is -# their only PR-time integration run (no legacy job runs it on PRs). Mode -# local (rbe=off) still skips it. Direct runs (push to main, -# workflow_dispatch, nightly.yml's call) gate nothing. In mode remote the -# dolt-server and proxied-server tiers run as one union job, bazel-dolt-race -# (`bazel test --config=dolt-race`), whose job.status is both the -# bazel-doltserver and the bazel-proxied output; in every other mode -# bazel-doltserver and bazel-proxied run as before and report their own. -# Exactly one of the two arms runs in any mode, so the gate ids -# (BAZEL_DOLTSERVER, BAZEL_PROXIED) and bazel-gate.sh's skips are unchanged -# (engdocs/CI_REQUIRED_CHECK_TOPOLOGY.md, "dolt-race Union Lane"). -# -# Execution mode, decided once by the rbe job (every other job needs it and -# reads its outputs; none re-derives the condition) and passed to -# .github/actions/setup-bazel: -# remote: repo variable RBE_WEST_WORKERS=true, rbe not off, not a fork or -# Dependabot PR (or a fork PR bazel-farm.yml authorized, below), and the -# secret RBE_WEST_EXECUTOR (grpcs:// endpoint) present, plus -# RBE_TLS_CERT, RBE_TLS_KEY (base64; RBE_TLS_CA optional): remote -# execution on rbe-west, instance "oss" (shared with gascity), from the -# Blacksmith runner. -# fork-ro, fork-rw: fork and Dependabot pull_request runs while rbe-west's -# certificate mint (rbe-fork-mint, infra nativelink-cas/west README -# "rbe-fork") says open for this run. Such runs get no secrets, no OIDC -# token and no repository variables, so the farm decides: the rbe job -# asks the mint's /v1/status, and every lane then proves it is a -# running job of this run (a CSR uploaded as an artifact of it) and gets -# its own certificate of about two hours (setup-bazel, -# fork-credential.sh). fork-ro: instance "oss-fork", the fork pool, -# nothing cached for anyone; fork-rw (PRs from an allowlisted author's -# own fork, triggered by an allowlisted user; the mint reads the -# default branch's .github/bazel-farm-allowlist.txt): instance "oss", -# the trusted OSS pool, results cached by its workers. Every lane runs, -# remotely, as in mode remote, from a GitHub-hosted runner (the client -# only). A lane whose certificate says another tier than this job fails -# ("re-run all jobs"). -# cache: rbe=cache dispatches, fork and Dependabot PRs while rbe-fork is -# closed, unreachable or refuses the run (the mint's canary list, its -# switches, the farm's gate), and same-repo runs without the secret: -# like local (ubuntu-latest, bazel-embedded, bazel-proxied and -# bazel-server-storage skipped), plus rbe-west's anonymous read-only -# cache (.bazelrc --config=fork-cache): hits for every action trusted CI -# already executed, no credentials, no uploads; and bazel-integration -# runs too (a warm run hits all but what the PR changed). The farm can -# close the cache at any time; Bazel then runs everything locally. -# local: rbe=off dispatches (fork simulation without the cache): local -# execution on ubuntu-latest, slower but the same results, for every job -# but the remote-only ones (bazel-embedded, bazel-proxied, -# bazel-server-storage) and bazel-integration, which are skipped. -# skip: same-repo runs while RBE_WEST_WORKERS is unset (the farm switch), -# and authorized fork-farm runs without it or without the secret. Fork -# and Dependabot pull_request runs cannot read the variable; rbe-fork's -# own switches decide for them. -# Trusted-author fork PRs: bazel-farm.yml (pull_request_target, advisory) -# calls this workflow for fork PRs whose author and triggering user are on -# the base branch's .github/bazel-farm-allowlist.txt, with fork-farm: -# authorized and checkout-sha: the event's pinned head SHA. Only that -# combination, on a pull_request_target event, lets a fork run remotely; a -# fork's pull_request run (pr.yml, which the fork may edit) stays in mode -# cache (or local, or fork-ro/fork-rw as the mint decides) whatever it -# passes, and has no secrets anyway. See engdocs/CI_REQUIRED_CHECK_TOPOLOGY.md, -# "Trusted-author fork PRs". (rbe-fork's tier rw replaces this workflow -# once allowlisted forks have run it.) -# workflow_dispatch with rbe=cache simulates a fork run; rbe=off also drops -# the cache. The decision is also a workflow_call output (rbe-mode, -# rbe-enabled), so pr.yml's gate can tell an expected skip from a missing -# lane without re-deriving it. Callers pass secrets explicitly, never -# `secrets: inherit`: pr.yml and nightly.yml pass all five declared secrets -# (the four RBE ones plus rbe-prewarm's bazel-allocator app private key); -# bazel-farm.yml passes only the four RBE secrets (its pull_request_target -# run never gets the app credential - see "rbe-west Pre-warm" below and B1 -# in this task's security review). -# -# Accepted risk (same model as gascity's bazel-test.yml): on push, merge_group -# and same-repo pull_request runs, code from the PR head runs with the RBE -# client certificate. setup-bazel is itself PR code, the key file is readable -# by every later step and by locally executed Bazel actions, and a .bazelrc -# edit can point the BEP or the executor anywhere. Anyone who can push a -# branch here can therefore use or copy the certificate and write to the -# farm's cache. Fork PRs never get it on their own runs, except those -# bazel-farm.yml authorizes (allowlisted author and sender, accepted as -# same-repo-level trust; that workflow's header lists what it adds), and -# except once queued: a merge_group run loads every workflow file and -# script from the queue entry's commit, so a fork (or Dependabot) PR that a -# maintainer queues runs its own edits to .github/, .bazelrc, tools/bazel, -# MODULE.bazel, BUILD files and CI scripts with all CI secrets, the -# pre-warm app key included, even if the entry is later ejected. That is -# the trust of merging it (push to main would run the same code), so the -# maintainer rule is: review a fork PR's changes to those paths before -# queueing it, as before merging (engdocs/CI_REQUIRED_CHECK_TOPOLOGY.md, -# "Merge Queue"). Otherwise: pull_request from a -# fork receives no secrets, bazel-farm.yml is the only pull_request_target -# caller (policy-tested), and forks cannot write the runner cache. Nor do -# Dependabot PRs, which run as same-repo but get no Actions secrets. Fork -# and Dependabot runs in modes fork-ro/fork-rw hold a certificate of their -# own instead, worth what their run gets anyway (remote execution under the -# farm's quotas, AC_OSS reads, hash-verified CAS uploads; no action-cache -# writes) for about two hours: anyone who copies it from the run gets no -# more than the PR's author already has. In place: the four RBE secrets are -# referenced by the setup-bazel step alone (plus the rbe job's emptiness test -# of RBE_WEST_EXECUTOR, and rbe-prewarm's own has-app/mint steps for the -# app private key, below), never by a workflow- or job-level env (policy-tested), -# decoded key material is masked in logs, and CI uploads only remotely -# executed results (--noremote_upload_local_results in .bazelrc's -# remote-exec config and in the generated rc), and the certificate -# is issued to beads CI alone, so it can be revoked without affecting other -# clients. Maintainers decided (2026-09-28) that same-repo PRs keep remote -# execution under this model. Still open: a beads-only RBE_INSTANCE. The farm -# refuses client action-cache writes (only its workers write; rbe-west D2). -# -# Accepted risk, rbe-prewarm's credential (see "rbe-west Pre-warm", -# engdocs/CI_REQUIRED_CHECK_TOPOLOGY.md, and B1 in this task's security -# review): the bazel-allocator private key is shared with gascity's own -# rbe-west scaler (a deliberate, user-approved reuse, not a beads-only -# credential), so same-repo pull_request, merge_group, push and nightly.yml -# runs - the only contexts that ever receive RBE_POOL_APP_PRIVATE_KEY - each -# reach that shared key for the duration of the mint step alone (nothing -# executes from the checked-out repository tree while it, or the token it -# produces, is in scope: see the rbe-prewarm job below). That holds only for -# this file as committed: a merge_group run uses the queued commit's copy, -# so a queued fork PR that edits this job (or pr.yml) reaches the key with -# whatever it wrote. Queueing a fork PR is therefore merging-level trust; -# review its .github/ changes first (see the header above). Unlike the RBE -# client certificate above, this key cannot be rotated or revoked for beads -# alone without also breaking gascity's scaler; the kill switch -# (RBE_POOL_APP_PRIVATE_KEY unset, or RBE_PREWARM_WORKERS=0) disables -# pre-warming without touching the key itself, so it is the first response to -# a suspected leak, not a rotation. -# In pr.yml's run the bd artifact is bazel-ci-build-artifacts, so it cannot -# collide with the build-artifacts job's ci-build-artifacts. F3: the package -# gates no longer consume this artifact (they build their own bd); it is -# kept for the F3.5.3 SHA256SUMS comparison and for debugging. -# -# Runner cache: one entry per (.bazelversion, MODULE.bazel, MODULE.bazel.lock) -# triple, about 1 GB (Bazel's content-addressable --repository_cache, re-hashed on every -# hit; never the Bazel binary or the repo contents cache), written -# only by a push to main that missed it. No --disk_cache: remote runs use the -# remote cache, and local runs would need about 7 GB more. -# -# Workflow-file changes suppress pull_request runs under the org approval -# policy (merge_group runs are not suppressed: they run the queued copy); -# validate edits to this file with workflow_dispatch on the branch. -name: Bazel - -on: - push: - branches: [ main ] - workflow_dispatch: - inputs: - rbe: - description: "Remote execution: on (use secrets), cache (fork simulation: anonymous read-only cache) or off (local only)" - required: true - type: choice - default: "on" - options: [ "on", "cache", "off" ] - details: - description: "List every equivalence divergence" - required: false - type: boolean - default: false - fresh-test-results: - description: "Re-execute every test (--config=fresh: no cached test results), as nightly does" - required: false - type: boolean - default: false - workflow_call: - inputs: - rbe: - type: string - required: false - default: "on" - details: - type: boolean - required: false - default: true - # A string, not a boolean: on push and dispatch inputs.integration is - # null, and null == false in GitHub's loose comparison, so a boolean - # test would silently stop the lane on main. - integration: - description: > - "off" skips bazel-integration, bazel-cmd-dolt and - bazel-server-storage; anything else (pr.yml and bazel-farm.yml - pass "on") runs them where remote execution is on. - type: string - required: false - default: "on" - # true (pr.yml only, policy-tested to equal its committed - # BAZEL_CMD_DOLT_REQUIRED flag): pr.yml's ci-gate requires - # bazel-cmd-dolt, which runs the whole integration-tagged cmd/bd suite - # (//cmd/bd:bd_dolt_server_test wraps the integration build's - # bd_test), so bazel-integration leaves //cmd/bd:bd_test to it and - # each of those tests runs once. A boolean: on push and dispatch it is - # null, which is not == true, so bazel-farm.yml, nightly.yml, push and - # dispatch (bazel-cmd-dolt advisory there) keep bd_test in - # bazel-integration. - cmd-dolt-required: - description: > - true (pr.yml only) drops //cmd/bd:bd_test from bazel-integration: - the required bazel-cmd-dolt lane runs those tests. - type: boolean - required: false - default: false - # true (nightly.yml only): every `bazel test` re-executes every test - # (--config=fresh), the run that samples flaky and -race failures. - # Everyone else reuses cached results for unchanged tests. A boolean: - # null on push, so push to main reuses too. - fresh-test-results: - description: > - true (nightly.yml) re-executes every test: no cached test results. - type: boolean - required: false - default: false - build-artifact-name: - description: > - Name of the uploaded bd artifact (ci-build-artifacts layout). pr.yml - sets bazel-ci-build-artifacts: its build-artifacts job uploads - ci-build-artifacts in the same run. - type: string - required: false - default: "ci-build-artifacts" - # Set only by bazel-farm.yml (policy-tested): the fork PR's head SHA - # from its pull_request_target event, pinned, and "authorized" once - # that workflow's authorize job has checked the base branch's - # allowlist. See the header. - checkout-sha: - description: > - Commit every lane checks out (bazel-farm.yml: the trusted fork PR's - head SHA); empty checks out the event's default ref. - type: string - required: false - default: "" - fork-farm: - description: > - "authorized" (bazel-farm.yml only) lets a pull_request_target fork - run execute remotely; anything else keeps forks local. - type: string - required: false - default: "off" - # "on" (pr.yml only) runs package-mcp and package-npm in this call, - # which waits on nothing but the rbe job. bazel-farm.yml, nightly.yml - # and direct runs (push, dispatch) keep the default "off": main.yml - # keeps its own package jobs, and a fork-farm run has no bd artifact - # consumer waiting on these. - package-gates: - description: > - "on" (pr.yml only) runs the MCP and npm package gates in this call. - type: string - required: false - default: "off" - # Only what the rbe job and setup-bazel need; callers pass them - # explicitly. None is required: fork and Dependabot runs have none. - secrets: - RBE_WEST_EXECUTOR: - required: false - RBE_TLS_CERT: - required: false - RBE_TLS_KEY: - required: false - RBE_TLS_CA: - required: false - # rbe-prewarm only (never read by setup-bazel or the rbe job): the - # "bazel-allocator" GitHub App's private key (its public client ID is - # a literal in the mint step, not a secret). The app is installed on - # gastownhall/gascity alone with Actions (read/write) and Metadata - # (read) permissions, nothing else; rbe-prewarm's mint step reads the - # private key directly (it IS a long-lived credential, held in the - # runner's memory for that one step) to produce a short-lived - # installation token, which is all any later step ever sees. Unset - # disables pre-warming. Declared here so fork-rw could in principle - # request it, but no caller ever does: pr.yml and nightly.yml pass it - # straight through (both are same-repo/trusted triggers only); - # bazel-farm.yml's pull_request_target call does not, so no pre-warm - # credential reaches a fork-authored run before a maintainer queues - # it (B1, security review of bdef342d5 - accepted risk is same-repo - # runs reaching the shared gascity scaler key, not forks). Exception: - # a merge_group run executes the queued commit's workflows, so a - # queued fork PR's edits run with this key (merging-level trust; see - # the header). See - # engdocs/CI_REQUIRED_CHECK_TOPOLOGY.md, "rbe-west Pre-warm". - RBE_POOL_APP_PRIVATE_KEY: - required: false - # The rbe job's decision, and each lane's job.status (success, failure or - # cancelled; empty when the lane was skipped or never reported). pr.yml's - # ci-gate reads them through .github/scripts/bazel-gate.sh. - outputs: - rbe-enabled: - description: "'true' when the lanes executed remotely on rbe-west, else 'false'" - value: ${{ jobs.rbe.outputs.enabled }} - rbe-mode: - description: "remote, fork-ro or fork-rw (every lane runs remotely), cache (no remote-only jobs but integration), local (no remote-only jobs), or skip (no jobs): the rbe job's decision" - value: ${{ jobs.rbe.outputs.mode }} - bazel-test: - description: "bazel-test's job.status" - value: ${{ jobs.bazel-test.outputs.result }} - bazel-pure: - description: "bazel-pure's job.status" - value: ${{ jobs.bazel-pure.outputs.result }} - bazel-release-cross: - description: "bazel-release-cross's job.status" - value: ${{ jobs.bazel-release-cross.outputs.result }} - bazel-embedded: - description: "bazel-embedded's job.status" - value: ${{ jobs.bazel-embedded.outputs.result }} - bazel-integration: - description: "bazel-integration's job.status" - value: ${{ jobs.bazel-integration.outputs.result }} - # Mode remote: bazel-dolt-race runs both tiers and the member jobs - # skip; every other mode: the reverse. One side is always empty. - bazel-doltserver: - description: "the dolt-server tier's job.status: bazel-dolt-race's in mode remote, bazel-doltserver's otherwise" - value: ${{ jobs.bazel-dolt-race.outputs.result || jobs.bazel-doltserver.outputs.result }} - bazel-proxied: - description: "the proxied-server tier's job.status: bazel-dolt-race's in mode remote, bazel-proxied's otherwise" - value: ${{ jobs.bazel-dolt-race.outputs.result || jobs.bazel-proxied.outputs.result }} - # Mode remote: bazel-dolt-int runs both tiers and the member jobs - # skip; every other mode: the reverse. One side is always empty - # (item 5 A2). bazel-dolt-int reports its own per-tier classification - # (classify_tier_results.py), not job.status: the job carries no - # continue-on-error, so job.status alone would read failure for both - # tiers on a cmd-dolt-only failure, unlike bazel-cmd-dolt's own - # advisory, continue-on-error nature outside mode remote. - bazel-server-storage: - description: "the server-Dolt storage tier's result: bazel-dolt-int's server_storage_result in mode remote, bazel-server-storage's job.status otherwise" - value: ${{ jobs.bazel-dolt-int.outputs.server_storage_result || jobs.bazel-server-storage.outputs.result }} - bazel-cmd-dolt: - description: "the cmd/bd Dolt-server tier's result (advisory unless pr.yml's BAZEL_CMD_DOLT_REQUIRED is \"true\"): bazel-dolt-int's cmd_dolt_result in mode remote, bazel-cmd-dolt's job.status otherwise" - value: ${{ jobs.bazel-dolt-int.outputs.cmd_dolt_result || jobs.bazel-cmd-dolt.outputs.result }} - package-mcp: - description: "package-mcp's job.status (empty unless package-gates is 'on')" - value: ${{ jobs.package-mcp.outputs.result }} - package-npm: - description: "package-npm's job.status (empty unless package-gates is 'on')" - value: ${{ jobs.package-npm.outputs.result }} - rbe-prewarm: - description: "rbe-prewarm's job.status (advisory: continue-on-error, never gates or blocks a lane)" - value: ${{ jobs.rbe-prewarm.outputs.result }} - -# Main runs always finish (they may save the runner cache); PR runs (called -# from pr.yml or bazel-farm.yml, whose own groups cancel the calling run too) -# supersede. The group differs from each caller's, as a called workflow's -# must. -concurrency: - group: bazel-${{ github.event_name }}-${{ github.event.pull_request.number || github.ref }} - cancel-in-progress: ${{ github.event_name == 'pull_request' || github.event_name == 'pull_request_target' }} - -permissions: - contents: read - -env: - BD_DISABLE_METRICS: "1" - BD_DISABLE_EVENT_FLUSH: "1" - # Appended last to every `bazel test` (after the lane's config and - # sole-run): --config=fresh (.bazelrc, --nocache_test_results) only when - # the caller asks for fresh results (nightly.yml, or a dispatch); empty - # otherwise, so results are cached and reused. See the header. - BAZEL_FRESH: ${{ inputs.fresh-test-results && '--config=fresh' || '' }} - # Phase B report-only budget for T1/T2 (tools/bazel/critpath.py). - BAZEL_BUDGET_SECONDS: "420" - -jobs: - # The one place the execution mode is decided (see the header), so every - # job sees the same answer for the whole run. Its decide step reads one - # secret, and only to test it for emptiness: the step's env holds a - # boolean, never the value, and the step runs no repository code (it comes - # before the job's checkout). Fork and Dependabot pull_request runs get no - # secrets: rbe-fork-mint decides theirs (fork-ro, fork-rw, or cache while - # it is closed). - # - # Then, only when the lanes will execute remotely (enabled: modes remote, - # fork-ro, fork-rw; merge groups are mode remote), the worker-env preflight: - # rbe-west's pool workers run gascity main's tools/rbe/worker-env and - # advertise its pin, and the schedulers match //platforms:rbe_worker's pin - # exactly, so when gascity re-pins and beads' copy (tools/rbe/worker-env.txt - # and the pin in platforms/BUILD.bazel) falls behind, every remote action - # queues until the lanes time out. tools/rbe/worker-env-sync compares the - # copy with gascity main and, out of step, fails this job: every lane needs - # it, so they all skip and pr.yml's gate is red within a minute, with the - # diff and the pin to commit in this job's summary. A fallback to mode - # cache would not keep the PR green (pr.yml's ci-gate requires a remote - # run, BAZEL_REMOTE) and would only make it slower, so fail fast. gascity - # unreachable (exit 2: unknown) warns and proceeds: GitHub's availability - # never blocks a PR. The steps after decide run repository code (the PR's - # script, sparse-checked-out) but hold no secret and set no output: the - # job's outputs are decide's alone. nightly.yml runs the same check every - # night and opens a ci/infra issue. - rbe: - name: execution mode - # Same-repo PRs (and merge_group/push/dispatch/schedule runs, which are - # never forks) get the org's Blacksmith pool; a pull_request_target farm - # run and any fork or Dependabot PR stay on GitHub-hosted runners. This - # job reads no repository code and holds no secret value (see below), so - # Blacksmith sees nothing sensitive either way. - runs-on: ${{ (github.event_name == 'push' || github.event_name == 'workflow_dispatch' || github.event_name == 'schedule' || github.event_name == 'merge_group' || (github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository && github.actor != 'dependabot[bot]')) && 'blacksmith-2vcpu-ubuntu-2404' || 'ubuntu-latest' }} - timeout-minutes: 5 - outputs: - enabled: ${{ steps.decide.outputs.enabled }} - mode: ${{ steps.decide.outputs.mode }} - tier: ${{ steps.decide.outputs.tier }} - rrc: ${{ steps.rrc.outputs.rrc }} - steps: - # The expressions' == is case-insensitive (RBE_WEST_WORKERS=True counts). - # A push, merge_group or dispatch has no pull_request, so FORK is false. - # Fork and Dependabot pull_request runs see no secrets and no vars: for - # them rbe-west decides (rbe-fork-mint's status: open, and the tier from - # the PR's author and this run's triggering actor). Its answer is the - # whole run's mode; every lane then gets its own certificate - # (setup-bazel, BAZEL_FORK_REMOTE) and fails if the tier changed. A - # mint that is closed, refuses or cannot be reached leaves mode cache, - # as before rbe-fork. - - name: Decide remote execution - id: decide - env: - RBE_VAR_ON: ${{ vars.RBE_WEST_WORKERS == 'true' }} - RBE_INPUT_OFF: ${{ inputs.rbe == 'off' }} - RBE_INPUT_CACHE: ${{ inputs.rbe == 'cache' }} - PULL_REQUEST: ${{ github.event_name == 'pull_request' }} - FORK: ${{ github.event.pull_request.head.repo.fork == true }} - DEPENDABOT: ${{ github.actor == 'dependabot[bot]' }} - FORK_FARM: ${{ inputs.fork-farm == 'authorized' && github.event_name == 'pull_request_target' && inputs.checkout-sha != '' }} - HAS_EXECUTOR: ${{ secrets.RBE_WEST_EXECUTOR != '' }} - PR_NUMBER: ${{ github.event.pull_request.number }} - run: | - for v in RBE_VAR_ON RBE_INPUT_OFF RBE_INPUT_CACHE PULL_REQUEST FORK DEPENDABOT FORK_FARM HAS_EXECUTOR; do - case "${!v}" in - true|false) ;; - *) echo "::error::$v is '${!v}', want true or false"; exit 1 ;; - esac - done - # A fork or Dependabot pull_request run executes remotely only with - # rbe-fork's certificate (mint open for this run), else locally with - # the read-only cache. Any other fork run (pull_request_target) - # stays local unless bazel-farm.yml authorized it; an authorized - # fork run is remote or nothing (pr.yml's call already runs it). - tier= - if [ "$RBE_INPUT_OFF" = true ]; then - mode=local - elif [ "$RBE_INPUT_CACHE" = true ]; then - mode=cache - elif [ "$PULL_REQUEST" = true ] && { [ "$FORK" = true ] || [ "$DEPENDABOT" = true ]; }; then - [[ "$PR_NUMBER" =~ ^[1-9][0-9]{0,8}$ ]] || { echo "::error::PR number '$PR_NUMBER'"; exit 1; } - url="https://rbe-mint.ops.gascity.com:8444/v1/status?repo=${GITHUB_REPOSITORY#*/}&run=$GITHUB_RUN_ID&attempt=$GITHUB_RUN_ATTEMPT&pr=$PR_NUMBER" - st=$(curl -sS --connect-timeout 5 --max-time 30 --retry 3 --retry-all-errors "$url" || echo '{}') - tier=$(jq -r 'if .open == true then .tier else "" end' <<<"$st" 2>/dev/null || true) - case "$tier" in - ro|rw) mode="fork-$tier" ;; - *) - tier= - mode=cache - echo "::warning title=rbe-fork closed for this run::$(jq -r '.reason // .error // "rbe-fork-mint unreachable"' <<<"$st" 2>/dev/null || echo unreachable)" - ;; - esac - elif [ "$FORK" = true ] && [ "$FORK_FARM" != true ]; then - mode=cache - elif [ "$RBE_VAR_ON" != true ]; then - mode=skip - elif [ "$HAS_EXECUTOR" = true ]; then - mode=remote - elif [ "$FORK_FARM" = true ]; then - mode=skip - else - mode=cache - fi - enabled=false - case "$mode" in remote|fork-ro|fork-rw) enabled=true ;; esac - { - echo "mode=$mode" - echo "enabled=$enabled" - echo "tier=$tier" - } >> "$GITHUB_OUTPUT" - echo "Bazel execution mode: **$mode** (RBE_WEST_WORKERS on: $RBE_VAR_ON, rbe=off: $RBE_INPUT_OFF, rbe=cache: $RBE_INPUT_CACHE, fork: $FORK, dependabot: $DEPENDABOT, fork farm: $FORK_FARM, executor secret: $HAS_EXECUTOR)" | tee -a "$GITHUB_STEP_SUMMARY" - - # Bazel's remote repo contents cache on rbe-west (ga-vnycm2.29, ported - # from gascity's bazel.yml; gascity - # engdocs/design/bazel-remote-repo-contents-cache.md): extracted - # external repositories (go_deps, the Go SDK, the LLVM slice) come from - # rbe-west's action cache instead of being re-fetched by every cold lane - # client. vars.RBE_REPO_CONTENTS_CACHE rolls it out: - # unset/off nothing (the default); - # seed main pushes seed the cache (rrc-seed); no lane reads it; - # canary seed, and bazel-test reads; - # on seed, and every lane in .github/scripts/rrc-lane-commands.txt - # reads. - # Mode remote only: fork pull_request runs see no vars (mode cache - # reads through setup-bazel's rc instead, in the jobs that pass - # BAZEL_RRC_READ, behind its probe of rbe-cache: write-bazelrc.sh). Any other value is off, with a warning, so a typo - # never fails the required gate. Like decide, this step runs no - # repository code (it comes before the checkout). - - name: Remote repo contents cache - id: rrc - env: - RRC: ${{ vars.RBE_REPO_CONTENTS_CACHE }} - MODE: ${{ steps.decide.outputs.mode }} - run: | - rrc=off - case "$RRC" in - "" | off) ;; - seed | canary | on) - if [ "$MODE" = remote ]; then rrc=$RRC; fi ;; - *) echo "::warning title=RBE_REPO_CONTENTS_CACHE::unknown value '$RRC' (want off, seed, canary or on); treated as off" ;; - esac - echo "rrc=$rrc" >> "$GITHUB_OUTPUT" - echo "Remote repo contents cache: **$rrc**" >> "$GITHUB_STEP_SUMMARY" - - # The worker-env preflight (see the job's comment): the PR's own copy, - # so a PR that syncs it passes. Only the two paths it compares. - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - if: ${{ steps.decide.outputs.enabled == 'true' }} - with: - ref: ${{ inputs.checkout-sha }} - persist-credentials: false - allow-unsafe-pr-checkout: ${{ inputs.fork-farm == 'authorized' && github.event_name == 'pull_request_target' }} - sparse-checkout: | - tools/rbe - platforms - - - name: worker-env in step with gascity main - if: ${{ steps.decide.outputs.enabled == 'true' }} - env: - MODE: ${{ steps.decide.outputs.mode }} - run: | - rc=0 - tools/rbe/worker-env-sync main || rc=$? - case "$rc" in - 0) ;; - 1) - echo "::error title=Bazel lanes stopped before queueing::beads' worker-env pin is out of step with gascity main, so rbe-west has no worker for this run's remote actions (mode $MODE): copy tools/rbe/worker-env.txt and the worker-env pin in platforms/BUILD.bazel from gastownhall/gascity main (this job's summary has the diff and the line to commit; see tools/rbe/worker-env-sync)." - exit 1 - ;; - *) - echo "::warning title=rbe worker-env not checked::could not compare beads' worker-env pin with gascity main (exit $rc); proceeding in mode $MODE. If the lanes sit at 0 running actions, run tools/rbe/worker-env-sync." - ;; - esac - - # Best-effort pre-warm of gastownhall/gascity's rbe-west OSS worker pool - # (one Blacksmith-hosted NativeLink worker, rbe-worker-pool.yml) so it is - # already booting instead of the lanes below waiting ~120s at "N actions, 0 - # running" while the pool scales from zero. A job of its own (not a step in - # `rbe`), so it never delays the mode decision every other job needs, and - # it is never a `needs` of any lane and never in pr.yml's ci-gate required - # list (advisory; .github/scripts/bazel-gate.sh does not read it). See - # engdocs/CI_REQUIRED_CHECK_TOPOLOGY.md, "rbe-west Pre-warm". - # - # Only when the run's lanes will actually execute on instance "oss": mode - # remote (the CI secrets) or fork-rw (rbe-fork-mint's trusted-author tier, - # cached by the same OSS workers per the header above). Mode fork-ro targets - # instance "oss-fork", a separate, uncached pool rbe-worker-pool.yml does - # not serve, so it is excluded (TestBazelCacheModeReachesTheRC's one named - # exception to "every lane runs remotely" in a fork mode). Like every other - # lane, the `if:` reads only needs.rbe.outputs - TestBazelRBEJobDecidesOnce - # forbids re-deriving fork/event facts outside the rbe job itself - and - # runs-on is the same mode-ternary every other lane uses, so a genuine fork - # or Dependabot PR that reaches mode fork-rw still never touches Blacksmith: - # the runner is only a client there, like every other fork-mode lane (see - # bazel-test's comment above). That PR also carries no repository secrets - # regardless of runs-on (GitHub strips them from a fork's pull_request - # event), so the credential-check step below finds nothing to mint even if - # it runs. The app secret (secrets.RBE_POOL_APP_PRIVATE_KEY) is never - # read in this `if:` or anywhere else in this file - only inside this job's own step env/with, same as the - # rbe job's HAS_EXECUTOR pattern - so a missing credential (the app not - # provisioned yet, or the kill switch) fails soft (no mint attempt, then - # the script's own ::warning::, exit 0) rather than changing whether the - # job runs. - rbe-prewarm: - name: pre-warm rbe-west pool - needs: rbe - # Only mode remote (B1, security review of bdef342d5: was remote or - # fork-rw). fork-rw is reached only by a fork or Dependabot - # pull_request run, which GitHub never hands a workflow_call secret - # regardless of this job's `if:`, so that half of the old condition - # always found no-app and no-op'd; bazel-farm.yml (the one caller with a - # distinct pull_request_target trust tier that could otherwise have - # reached fork-rw with secrets attached) no longer forwards the app - # secrets either, below. See "rbe-west Pre-warm", - # engdocs/CI_REQUIRED_CHECK_TOPOLOGY.md. - if: ${{ needs.rbe.outputs.mode == 'remote' }} - runs-on: ${{ needs.rbe.outputs.mode == 'remote' && 'blacksmith-2vcpu-ubuntu-2404' || 'ubuntu-latest' }} - timeout-minutes: 5 - # Job-level, not step-level: the only continue-on-error in this workflow - # (TestBazelWorkflowSecretsAndFailureSurface and - # TestBazelWorkflowJobsAndExecutionMode both carve out this one path by - # name). Every other job here is a gated lane whose real failure must - # reach pr.yml's ci-gate; this one is advisory and must never fail the - # call bazel.yml's own run represents (a job's failure fails the whole - # reusable-workflow run regardless of `needs`, which would otherwise turn - # pr.yml's ci-gate red for a pre-warm hiccup nothing requires). - continue-on-error: true - outputs: - result: ${{ steps.result.outputs.result }} - steps: - # No checkout in this job (B1, security review of bdef342d5; policy- - # tested that it has none): a step that checked out PR-head content - # and then ran a script from that tree while this job's credential was - # live would let that content - or any same-repo branch push, since - # editing a .github/scripts/*.sh file does not trip the org's - # workflow-file approval gate the way editing this YAML does - read - # the shared gascity scaler key. The dispatch logic therefore lives - # entirely in this trusted workflow definition's own `run:` below, - # never a checked-out file. - - # Same env-boolean pattern as the rbe job's HAS_EXECUTOR: the secret is - # tested for emptiness, never referenced in an `if:` directly, and the - # next step's `if:` reads this step's output (a job-level env can't be - # read from another step's `if:`, only its own `env:`). - - name: Check for the pre-warm app credential - id: has-app - env: - HAS_POOL_APP: ${{ secrets.RBE_POOL_APP_PRIVATE_KEY != '' }} - run: echo "has-app=$HAS_POOL_APP" >> "$GITHUB_OUTPUT" - - # Mints a short-lived installation token for the "bazel-allocator" - # GitHub App (installed on gastownhall/gascity only, Actions - # read/write + Metadata read): skipped entirely while the app's - # private key is not provisioned, leaving steps.mint.outputs.token empty, which - # the dispatch step below treats the same as any other missing-token - # case (::warning::, exit 0). permission-actions narrows the minted - # token to Actions even if the app is ever granted more. This is the - # only step that ever reads the long-lived private key directly - # (below); every step after it sees at most the short-lived token this - # one produces. client-id is the app's public Client ID (GET - # /apps/bazel-allocator; not a credential), committed as a literal so - # it is reviewed and policy-tested with the rest of this step rather - # than living in an unversioned repository variable; it replaces the - # action's deprecated app-id input. - - name: Mint gastownhall/gascity installation token - id: mint - if: ${{ steps.has-app.outputs.has-app == 'true' }} - uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3 - with: - client-id: Iv23ligrqVEhlamZnoPU - private-key: ${{ secrets.RBE_POOL_APP_PRIVATE_KEY }} - owner: gastownhall - repositories: gascity - permission-actions: write - - # Inlined rather than checked out and run from a script file (B1 - # above): every byte this step executes comes from this workflow - # definition, which pull_request_target always loads from the base - # branch and which editing suppresses pull_request runs under the - # org's approval policy (see the header). Not on merge_group: a queue - # entry runs its own commit's copy of this file with no approval - # gate, so a queued fork PR's edit here runs with the key (review - # .github/ changes before queueing). steps.mint.outputs.token - # appears nowhere else in this file (policy-tested): this is the only - # step with the credential in its environment, and no step after it - # touches a repository path or runs a local composite action. - - name: Pre-warm rbe-west OSS worker pool (gastownhall/gascity) - env: - GH_TOKEN: ${{ steps.mint.outputs.token }} - RBE_PREWARM_WORKERS: ${{ vars.RBE_PREWARM_WORKERS }} - run: | - set -u - - POOL_REPO="gastownhall/gascity" - POOL_WORKFLOW="rbe-worker-pool.yml" - POOL_REF="main" - - n="${RBE_PREWARM_WORKERS:-1}" - case "$n" in - '') n=1 ;; - *[!0-9]*) n=1 ;; - # Two or more digits is over the cap unless they are all zeros - # (still the kill switch); deciding it here keeps a string too - # long for the shell's integer compare from slipping past the - # -gt below unclamped. - ??*) - case "$n" in - *[!0]*) n=4 ;; - *) n=0 ;; - esac - ;; - esac - if [ "$n" -gt 4 ]; then - n=4 - fi - - if [ "$n" -le 0 ]; then - echo "RBE_PREWARM_WORKERS=0: rbe-west pre-warm disabled (kill switch)" - exit 0 - fi - - if [ -z "${GH_TOKEN:-}" ]; then - echo "::warning title=rbe-west pre-warm::no dispatch token (bazel-allocator app not provisioned, or the mint step failed); skipping" - exit 0 - fi - - active="$(gh run list -R "$POOL_REPO" --workflow "$POOL_WORKFLOW" --limit 50 \ - --json status --jq '[.[] | select(.status != "completed")] | length' 2>/dev/null)" || true - case "$active" in - '' | *[!0-9]*) - echo "::warning title=rbe-west pre-warm::could not read $POOL_REPO/$POOL_WORKFLOW run status; skipping dispatch" - exit 0 - ;; - esac - - missing=$((n - active)) - if [ "$missing" -le 0 ]; then - echo "${active} $POOL_REPO/$POOL_WORKFLOW worker(s) already active (want ${n}); nothing to dispatch" - exit 0 - fi - - dispatched=0 - i=1 - while [ "$i" -le "$missing" ]; do - if gh workflow run "$POOL_WORKFLOW" -R "$POOL_REPO" --ref "$POOL_REF"; then - dispatched=$((dispatched + 1)) - else - echo "::warning title=rbe-west pre-warm::gh workflow run $POOL_WORKFLOW -R $POOL_REPO failed (attempt $i/$missing)" - fi - i=$((i + 1)) - done - - echo "dispatched ${dispatched}/${missing} $POOL_REPO/$POOL_WORKFLOW worker(s) (active before: ${active}, want: ${n})" - exit 0 - - # Every lane reports its job.status from an always() last step - # (TestBazelLaneIsGatedAlongsideLegacy). The dispatch step above always - # exits 0, so this normally reads success regardless of what it found; - # the job-level continue-on-error above is the backstop for a step - # that fails outside it (the mint action). - - name: Record result - id: result - if: ${{ always() }} - env: - JOB_STATUS: ${{ job.status }} - run: echo "result=$JOB_STATUS" >> "$GITHUB_OUTPUT" - - bazel-test: - name: test - # Remote-execution runs use the org's Blacksmith pool (the farm's edge - # admits any runner holding the client certificate; there is no IP - # allowlist) and do not count against GitHub's concurrent-job cap. Local - # and cache runs (forks, rbe=off/cache, no secrets) build everything - # they miss on the runner, so they get the larger GitHub-hosted one. - # Fork modes (fork-ro/fork-rw) execute remotely but stay GitHub-hosted, - # like every fork job: the runner is only a client. ubuntu-24.04, not - # ubuntu-latest, in every lane that reads the remote repo contents cache - # in mode cache: the cached trees are Linux x86_64 ones from a 24.04 host - # (design R2), so the image must not move under them. - # - # 8 vCPU, not the other lanes' 4: with every action a remote cache hit, - # this lane's `bazel test //... --config=ci` spent ~63 of 81s in - # client-side loading and analysis (1798 packages, 66k configured - # targets; critical path 7.65s, merge group 37721729916), which Bazel - # parallelizes across the client's cores. Analysis-only, seeded runner - # cache (ga-vnycm2.8): 38.3s on 2 vCPU, 25.6s on 4, 16.3s on 8. - needs: rbe - runs-on: ${{ needs.rbe.outputs.mode == 'remote' && 'blacksmith-8vcpu-ubuntu-2404' || 'ubuntu-24.04' }} - # Remote execution goes to rbe-west (instance "oss", the Blacksmith-backed - # worker pool shared with gascity). Skipped only in mode skip (same-repo - # runs while RBE_WEST_WORKERS is unset). - if: ${{ needs.rbe.outputs.mode != 'skip' }} - timeout-minutes: 100 - outputs: - result: ${{ steps.result.outputs.result }} - env: - # This lane is the only pre-merge run of the PR-core tests (pr.yml's - # retired PR Core job never retried): where it executes remotely - # (mode remote, and the fork modes) --config=sole-run fails on a - # remote cache eviction instead of re-running the whole invocation - # (whose retry could turn a failed test green). Test results are - # cached and reused like every lane's; only nightly re-executes them - # all (BAZEL_FRESH). - BAZEL_SOLE_RUN: ${{ needs.rbe.outputs.enabled == 'true' && '--config=sole-run' || '' }} - steps: - # inputs.checkout-sha: see the header (empty: the event's default ref). - # checkout v7 refuses a fork head on pull_request_target unless opted - # in; only bazel-farm.yml's authorized call opts in (false elsewhere). - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - with: - ref: ${{ inputs.checkout-sha }} - persist-credentials: false - allow-unsafe-pr-checkout: ${{ inputs.fork-farm == 'authorized' && github.event_name == 'pull_request_target' }} - - # Go is for tools/bazel/equivalence.py's `go list` and for make; Bazel - # uses its own hermetic SDK. - - name: Set up Go - id: setup-go - uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 - with: - go-version-file: 'go.mod' - cache: false - - # Read-only: main.yml's build-artifacts job owns this cache. - - name: Restore Go module cache - uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 - with: - path: ~/go/pkg/mod - key: beads-go-mod-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-${{ hashFiles('go.mod', 'go.sum') }} - restore-keys: beads-go-mod-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}- - - # Go does not re-hash extracted module directories on use; a restored - # cache written by an untrusted run is checked against go.sum first. - - name: Verify restored Go modules - run: go mod verify - - # The remote repo contents cache, read-only (the rbe job's rrc output: - # canary, bazel-test; on, every lane). Each cached repository costs - # several sequential round trips, resolved on Skyframe's loading - # threads (default: the CPU count), so without 64 of them a cached read - # is slower than fetching. Both lines are key neutral (startup and - # loading only). Reading only: setup-bazel's rc keeps - # --noremote_upload_local_results, so nothing here writes; rrc-seed - # writes, on push to main. Before Set up Bazel: no server starts first. - - name: Remote repo contents cache (read) - if: ${{ needs.rbe.outputs.mode == 'remote' && (needs.rbe.outputs.rrc == 'on' || needs.rbe.outputs.rrc == 'canary') }} - run: | - { - echo 'startup --experimental_remote_repo_contents_cache' - echo 'common --loading_phase_threads=64' - } >> .bazelrc.local - - - name: Set up Bazel - id: bazel - uses: ./.github/actions/setup-bazel - env: - BAZEL_REMOTE_EXECUTOR: ${{ needs.rbe.outputs.mode == 'remote' && secrets.RBE_WEST_EXECUTOR || '' }} - RBE_TLS_CERT: ${{ needs.rbe.outputs.mode == 'remote' && secrets.RBE_TLS_CERT || '' }} - RBE_TLS_KEY: ${{ needs.rbe.outputs.mode == 'remote' && secrets.RBE_TLS_KEY || '' }} - RBE_TLS_CA: ${{ needs.rbe.outputs.mode == 'remote' && secrets.RBE_TLS_CA || '' }} - RBE_INSTANCE: ${{ needs.rbe.outputs.mode == 'remote' && 'oss' || '' }} - BAZEL_FORK_CACHE: ${{ needs.rbe.outputs.mode == 'cache' && 'true' || '' }} - BAZEL_RRC_READ: 'true' # mode cache reads the repo contents cache through rbe-cache - BAZEL_FORK_REMOTE: ${{ startsWith(needs.rbe.outputs.mode, 'fork-') && 'true' || '' }} - RBE_FORK_TIER: ${{ needs.rbe.outputs.tier }} - RBE_FORK_PR: ${{ github.event.pull_request.number }} - - # T1: remote + restored repository cache; T2: remote, cold runner; local: - # no remote execution (reported, never budgeted). - - name: Measurement tier - id: tier - env: - REMOTE: ${{ steps.bazel.outputs.remote }} - RESTORED: ${{ steps.bazel.outputs.cache-restored }} - run: | - if [ "$REMOTE" = "true" ] && [ "$RESTORED" = "true" ]; then tier=T1 - elif [ "$REMOTE" = "true" ]; then tier=T2 - else tier=local; fi - echo "tier=$tier" >> "$GITHUB_OUTPUT" - { - echo "### Bazel / test" - echo - echo "Tier **$tier**: remote execution $REMOTE, runner cache restored $RESTORED." - echo - } >> "$GITHUB_STEP_SUMMARY" - - # --profile and --build_event_json_file are command options, so they go - # after `test` (a startup position fails). - - name: bazel test //... --config=ci - id: test - timeout-minutes: 90 - run: | - set -o pipefail - start=$(date +%s) - rc=0 - bazel test //... --config=ci ${BAZEL_SOLE_RUN:+"$BAZEL_SOLE_RUN"} ${BAZEL_FRESH:+"$BAZEL_FRESH"} \ - --profile="$RUNNER_TEMP/bazel-profile.json" \ - --build_event_json_file="$RUNNER_TEMP/bazel-bep.json" \ - --execution_log_compact_file="$RUNNER_TEMP/bazel-exec.log.zst" \ - --experimental_build_event_upload_strategy=local \ - 2>&1 | tee "$RUNNER_TEMP/bazel-test.log" || rc=$? - echo "bazel test: exit $rc, $(( $(date +%s) - start ))s wall" | tee -a "$GITHUB_STEP_SUMMARY" - echo >> "$GITHUB_STEP_SUMMARY" - exit "$rc" - - # pr.yml's retired build-artifacts job, built by Bazel: the same - # artifact name and layout (bd-linux-gms-pure, SHA256SUMS, - # build-manifest.txt). F3: the package gates build - # their own bd and no longer consume this artifact; it is kept for the - # F3.5.3 SHA256SUMS comparison and for debugging. The script documents - # which binary and how it differs from `go build`'s. - - name: Package bd (ci-build-artifacts layout) - id: bd-artifact - if: ${{ always() && steps.test.outcome != 'skipped' }} - run: ./scripts/ci/package-bazel-bd.sh "$RUNNER_TEMP/bd-artifacts" - - - name: Upload build artifacts - if: ${{ always() && steps.bd-artifact.outcome == 'success' }} - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7 - with: - overwrite: true # a job re-run is the same run: replace, not 409 - name: ${{ inputs.build-artifact-name || 'ci-build-artifacts' }} - path: ${{ runner.temp }}/bd-artifacts/ - retention-days: 1 - if-no-files-found: error - - - name: Critical-path report - if: ${{ always() && steps.test.outcome != 'skipped' }} - env: - TIER: ${{ steps.tier.outputs.tier }} - run: | - budget=() - if [ "$TIER" != "local" ]; then budget=(--budget "$BAZEL_BUDGET_SECONDS"); fi - python3 tools/bazel/critpath.py "$RUNNER_TEMP/bazel-profile.json" \ - --tier "$TIER" --log "$RUNNER_TEMP/bazel-test.log" "${budget[@]}" - - # Fails on any Go test that `go test` runs and Bazel did not (tag filter, - # manual, missing target, empty shard) unless it is allowlisted. - # It reads test.xml from bazel-testlogs, which --config=integration - # shares with --config=ci (same output directory): if a job ever runs - # both lanes, run each lane's equivalence right after its own - # `bazel test`, before the other lane overwrites the logs. - - name: Go test equivalence - if: ${{ always() && steps.test.outcome != 'skipped' }} - timeout-minutes: 15 - env: - DETAILS: ${{ inputs.details && '--details' || '' }} - run: | - args=() - if [ -n "$DETAILS" ]; then args+=("$DETAILS"); fi - python3 tools/bazel/equivalence.py --bep "$RUNNER_TEMP/bazel-bep.json" "${args[@]}" - - # Runs last: it rewrites BUILD files in the checkout. main moves BUILD - # inputs (imports, go.mod) without touching Bazel, so drift shows up here - # first. On drift the step still fails, and the BUILD.bazel / - # MODULE.bazel / MODULE.bazel.lock part of the fix is written as a patch - # for the bazel-sync-patch artifact, which bazel-autofix.yml pushes to - # same-repo PR branches (or turns into an apply recipe for forks). - - name: BUILD files in sync (gazelle, go_srcs, MODULE.bazel) - id: sync - if: ${{ always() && steps.bazel.outcome == 'success' }} - timeout-minutes: 15 - env: - PR_NUMBER: ${{ github.event.pull_request.number }} - PR_HEAD_SHA: ${{ github.event.pull_request.head.sha }} - run: | - start=$(date +%s) - # A stale go_srcs block is drift like any other: record it, then let - # bazel-sync fix it so the patch covers it too. - check=0 - make bazel-sync-check || check=$? - make bazel-sync - drift="$(git status --porcelain --untracked-files=all)" - echo "BUILD sync check: $(( $(date +%s) - start ))s" >> "$GITHUB_STEP_SUMMARY" - if [ -n "$drift" ]; then - echo "::error title=Bazel BUILD files out of sync::run 'make bazel-sync' and commit the result" - echo "$drift" - git diff | head -200 - echo "BUILD files **out of sync**; run \`make bazel-sync\` and commit the result." >> "$GITHUB_STEP_SUMMARY" - ./scripts/ci/bazel-sync-patch.sh "$RUNNER_TEMP/bazel-sync-patch" - exit 1 - fi - exit "$check" - - # Data only: bazel-autofix.yml validates every path before applying it. - # Not from bazel-farm.yml's pull_request_target runs: autofix reads only - # "PR" runs, and a trusted-fork run publishes nothing meant for a - # privileged consumer. - - name: Upload BUILD sync patch - if: ${{ always() && steps.sync.outcome == 'failure' && github.event_name != 'pull_request_target' }} - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7 - with: - overwrite: true # a job re-run is the same run: replace, not 409 - name: bazel-sync-patch - path: ${{ runner.temp }}/bazel-sync-patch/ - if-no-files-found: ignore - retention-days: 7 - - - name: Upload test logs - if: ${{ failure() && steps.test.outcome == 'failure' }} - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7 - with: - overwrite: true # a job re-run is the same run: replace, not 409 - name: bazel-testlogs - path: | - bazel-testlogs/**/test.log - bazel-testlogs/**/test.xml - if-no-files-found: ignore - retention-days: 7 - - # Only a push to main that missed the exact key writes the runner cache. - # Its content (--repository_cache only, never the rc or credentials) is - # fixed by .bazelversion, MODULE.bazel and MODULE.bazel.lock, so one - # entry per triple is enough and later pushes do not churn the quota. - - name: Save Bazel runner cache - if: ${{ always() && github.event_name == 'push' && github.ref == 'refs/heads/main' && steps.bazel.outcome == 'success' && steps.bazel.outputs.cache-hit != 'true' }} - uses: actions/cache/save@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 - with: - path: ${{ runner.temp }}/bazel-ci-cache - key: bazel-repo-v4-${{ runner.os }}-${{ hashFiles('.bazelversion', 'MODULE.bazel', 'MODULE.bazel.lock') }} - - # A redacted, schema'd summary of this invocation's BEP, exec log and - # profile: timing, per-mnemonic cache classes, cold actions, test - # results, repo-fetch time. tools/bazel/ci_analytics_extract.py reads - # only a field whitelist and validates every emitted string, so this - # (unlike the raw BEP file) is safe to upload unredacted; see its - # module docstring. Mirrors gascity's ci/analytics-extractor lane step. - - name: CI analytics summary - id: ci-analytics - if: "!cancelled() && steps.test.outcome != 'skipped'" - continue-on-error: true - timeout-minutes: 3 - env: - LANE: test - MODE: ${{ needs.rbe.outputs.mode }} - CHECK_RUN_ID: ${{ job.check_run_id }} - PR_HINT: ${{ github.event.pull_request.number || 0 }} - run: | - [ -f "$RUNNER_TEMP/bazel-bep.json" ] || exit 0 - zstd --version || echo "::warning::zstd missing: no per-mnemonic data in this lane's summary" - python3 tools/bazel/ci_analytics_extract.py \ - --lane "$LANE" --mode "$MODE" --check-run-id "$CHECK_RUN_ID" --pr-hint "$PR_HINT" \ - --invocation test "$RUNNER_TEMP/bazel-bep.json" "$RUNNER_TEMP/bazel-exec.log.zst" "$RUNNER_TEMP/bazel-profile.json" \ - --out "$RUNNER_TEMP/ci-analytics/ci-analytics.json" - - - name: Upload CI analytics summary - if: "!cancelled() && steps.ci-analytics.outcome == 'success'" - continue-on-error: true - timeout-minutes: 2 - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7 - with: - overwrite: true # a job re-run is the same run: replace, not 409 - name: ci-analytics-test-${{ github.run_attempt }} - path: ${{ runner.temp }}/ci-analytics/ci-analytics.json - retention-days: 7 - if-no-files-found: ignore - - # Last step: the job's result as a workflow_call output for pr.yml's - # ci-gate. always() runs it after a failure, a timeout or a - # cancellation too. - - name: Record job result - id: result - if: ${{ always() }} - env: - JOB_STATUS: ${{ job.status }} - run: echo "result=$JOB_STATUS" >> "$GITHUB_OUTPUT" - - # The CGO_ENABLED=0 portability boundaries (pr.yml's retired - # check-cmd-bd-puregeo-tests job) with Bazel-built binaries: --config=pure - # builds bd and three pure test binaries and runs the -short cmd/bd subset - # (PURE_CMD_BD_TESTS, pinned by scripts/ci_workflow_test.go); - # --config=js-wasm builds the hooks test for js/wasm, which the Go SDK's - # go_js_wasm_exec runs under node with an exact pass-count guard. - bazel-pure: - name: pure-Go and js/wasm - # Same runner and skip rule as bazel-test. - needs: rbe - runs-on: ${{ needs.rbe.outputs.mode == 'remote' && 'blacksmith-4vcpu-ubuntu-2404' || 'ubuntu-24.04' }} - if: ${{ needs.rbe.outputs.mode != 'skip' }} - timeout-minutes: 90 - outputs: - result: ${{ steps.result.outputs.result }} - env: - PURE_CMD_BD_TESTS: '^Test(Help|CheckRemoteSafety|FormatDestroyToken|ShouldWireInitRemote|ExtractPrefix|IsNumericID|GetWorktreeGitDir|DriftItemStatuses|RunDriftChecks|IsServerProbablyRunning|CheckHooksDriftNotGitRepo|CheckServerDriftNoBeadsDir|CheckRemoteDriftNoBeadsDir|NocgoFactoriesHonorSharedServer|NocgoNewDoltStoreFromConfig_ErrorSuggestsCorrectFlag|NocgoNewReadOnlyStoreFromConfig_ErrorSuggestsCorrectFlag|NocgoStoreFactoriesRefuseGraphWorkspace)' - # The only pre-merge run of these boundaries: where it executes - # remotely --config=sole-run fails on a remote cache eviction instead - # of re-running the whole invocation (whose retry could turn a failed - # test green). Test results are cached and reused like every lane's; - # only nightly re-executes them all (BAZEL_FRESH). - BAZEL_SOLE_RUN: ${{ needs.rbe.outputs.enabled == 'true' && '--config=sole-run' || '' }} - steps: - # inputs.checkout-sha: see the header (empty: the event's default ref). - # checkout v7 refuses a fork head on pull_request_target unless opted - # in; only bazel-farm.yml's authorized call opts in (false elsewhere). - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - with: - ref: ${{ inputs.checkout-sha }} - persist-credentials: false - allow-unsafe-pr-checkout: ${{ inputs.fork-farm == 'authorized' && github.event_name == 'pull_request_target' }} - - # Only for go_js_wasm_exec and wasm_exec_node.js, which must match the - # Go version Bazel compiles with (go.mod's; policy-tested). - - name: Set up Go - uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 - with: - go-version-file: 'go.mod' - cache: false - - - name: Set up Node.js - uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v6 - with: - node-version: '24' - - # The remote repo contents cache, read-only, as in bazel-test. - - name: Remote repo contents cache (read) - if: ${{ needs.rbe.outputs.mode == 'remote' && needs.rbe.outputs.rrc == 'on' }} - run: | - { - echo 'startup --experimental_remote_repo_contents_cache' - echo 'common --loading_phase_threads=64' - } >> .bazelrc.local - - - name: Set up Bazel - id: bazel - uses: ./.github/actions/setup-bazel - env: - BAZEL_REMOTE_EXECUTOR: ${{ needs.rbe.outputs.mode == 'remote' && secrets.RBE_WEST_EXECUTOR || '' }} - RBE_TLS_CERT: ${{ needs.rbe.outputs.mode == 'remote' && secrets.RBE_TLS_CERT || '' }} - RBE_TLS_KEY: ${{ needs.rbe.outputs.mode == 'remote' && secrets.RBE_TLS_KEY || '' }} - RBE_TLS_CA: ${{ needs.rbe.outputs.mode == 'remote' && secrets.RBE_TLS_CA || '' }} - RBE_INSTANCE: ${{ needs.rbe.outputs.mode == 'remote' && 'oss' || '' }} - BAZEL_FORK_CACHE: ${{ needs.rbe.outputs.mode == 'cache' && 'true' || '' }} - BAZEL_RRC_READ: 'true' # mode cache reads the repo contents cache through rbe-cache - BAZEL_FORK_REMOTE: ${{ startsWith(needs.rbe.outputs.mode, 'fork-') && 'true' || '' }} - RBE_FORK_TIER: ${{ needs.rbe.outputs.tier }} - RBE_FORK_PR: ${{ github.event.pull_request.number }} - - - name: Run js/wasm hook boundary - id: build-wasm - run: | - set -euo pipefail - bazel build --config=js-wasm //internal/hooks:hooks_test \ - --profile="$RUNNER_TEMP/bazel-profile-build-wasm.json" \ - --build_event_json_file="$RUNNER_TEMP/bazel-bep-build-wasm.json" \ - --execution_log_compact_file="$RUNNER_TEMP/bazel-exec-build-wasm.log.zst" \ - --experimental_build_event_upload_strategy=local - bin="$(bazel cquery --config=js-wasm --output=files //internal/hooks:hooks_test)" - cd internal/hooks - - # Same selector and count guard as pr.yml's job. - test_status=0 - # wasm_exec.js caps argv+env size; the Blacksmith runner's environment - # exceeds it, so run with only what the hook test needs. - wasm_exec="$(go env GOROOT)/lib/wasm/go_js_wasm_exec" - test_output="$(env -i PATH="$PATH" HOME="$HOME" TMPDIR="$RUNNER_TEMP" \ - BD_DISABLE_METRICS=1 BD_DISABLE_EVENT_FLUSH=1 \ - "$wasm_exec" "$GITHUB_WORKSPACE/$bin" \ - -test.run '^TestRunHookReportsUnsupportedExecution$' \ - -test.v 2>&1)" || test_status=$? - printf '%s\n' "$test_output" - (( test_status == 0 )) || exit "$test_status" - - run_count=0 - pass_count=0 - nonpass_count=0 - pass_pattern='^--- PASS: TestRunHookReportsUnsupportedExecution \([0-9.]+s\)$' - nonpass_pattern='^[[:space:]]*--- (FAIL|SKIP): ' - while IFS= read -r line; do - if [[ "$line" == "=== RUN TestRunHookReportsUnsupportedExecution" ]]; then - ((run_count += 1)) - fi - if [[ "$line" =~ $pass_pattern ]]; then - ((pass_count += 1)) - fi - if [[ "$line" =~ $nonpass_pattern ]]; then - ((nonpass_count += 1)) - fi - done <<< "$test_output" - if (( run_count != 1 || pass_count != 1 || nonpass_count != 0 )); then - printf 'expected one exact js/wasm hook test run and pass with no failures or skips; got runs=%s passes=%s nonpasses=%s\n' \ - "$run_count" "$pass_count" "$nonpass_count" >&2 - exit 1 - fi - - - name: Build cmd/bd and pure-Go test binaries (--config=pure) - id: build-pure - run: >- - bazel build --config=pure - //cmd/bd:bd - //cmd/bd:bd_test - //internal/storage/embeddeddolt:embeddeddolt_test - //internal/tracker:tracker_test - --profile="$RUNNER_TEMP/bazel-profile-build-pure.json" - --build_event_json_file="$RUNNER_TEMP/bazel-bep-build-pure.json" - --execution_log_compact_file="$RUNNER_TEMP/bazel-exec-build-pure.log.zst" - --experimental_build_event_upload_strategy=local - - # `go build` with CGO_ENABLED=0 rejects a pure binary whose imports reach - # dolt's cgo-only store/nbs (gozstd). Bazel compiles gozstd's pure stubs - # instead, whose init panics (third_party/patches/gozstd_nocgo.patch), so - # the check happens here: every pure artifact is started once. bd_test - # starts in the next step. -test.run=^$ runs no test (TestMain still - # runs; BEADS_TEST_SKIP=dolt keeps tracker's from starting a container). - - name: Start every pure-Go artifact (gozstd contamination check) - id: test-pure - run: | - set -euo pipefail - # Not instrumented: no BEP/exec-log/profile is captured for this - # one-off smoke invocation (design's bazel-pure invocation list - # names build-wasm, build-pure, test-pure and test-bd only). - bazel run --config=pure //cmd/bd:bd -- version - bazel test --config=pure ${BAZEL_SOLE_RUN:+"$BAZEL_SOLE_RUN"} ${BAZEL_FRESH:+"$BAZEL_FRESH"} \ - //internal/storage/embeddeddolt:embeddeddolt_test \ - //internal/tracker:tracker_test \ - --test_sharding_strategy=disabled \ - '--test_arg=-test.run=^$' \ - --test_env=BEADS_TEST_SKIP=dolt \ - --profile="$RUNNER_TEMP/bazel-profile-test-pure.json" \ - --build_event_json_file="$RUNNER_TEMP/bazel-bep-test-pure.json" \ - --execution_log_compact_file="$RUNNER_TEMP/bazel-exec-test-pure.log.zst" \ - --experimental_build_event_upload_strategy=local - - # One unsharded process, like `go test -run`; the test.xml check keeps a - # selector that matches nothing from passing. - - name: Run pure-Go cmd/bd test subset (--config=pure) - id: test-bd - run: | - set -euo pipefail - bazel test --config=pure ${BAZEL_SOLE_RUN:+"$BAZEL_SOLE_RUN"} ${BAZEL_FRESH:+"$BAZEL_FRESH"} //cmd/bd:bd_test \ - --test_sharding_strategy=disabled \ - "--test_arg=-test.run=$PURE_CMD_BD_TESTS" \ - --profile="$RUNNER_TEMP/bazel-profile-test-bd.json" \ - --build_event_json_file="$RUNNER_TEMP/bazel-bep-test-bd.json" \ - --execution_log_compact_file="$RUNNER_TEMP/bazel-exec-test-bd.log.zst" \ - --experimental_build_event_upload_strategy=local - n="$(grep -c ' 0 )) - - # A redacted, schema'd summary of this job's four BEP-tracked - # invocations (build-wasm, build-pure, test-pure, test-bd; `bazel run - # ... -- version` above is deliberately not tracked, see its comment). - # Same contract as bazel-test's step; see its comment. - - name: CI analytics summary - id: ci-analytics - if: "!cancelled()" - continue-on-error: true - timeout-minutes: 3 - env: - LANE: pure - MODE: ${{ needs.rbe.outputs.mode }} - CHECK_RUN_ID: ${{ job.check_run_id }} - PR_HINT: ${{ github.event.pull_request.number || 0 }} - run: | - args=() - for step in build-wasm build-pure test-pure test-bd; do - bep="$RUNNER_TEMP/bazel-bep-$step.json" - [ -f "$bep" ] || continue - args+=(--invocation "$step" "$bep" "$RUNNER_TEMP/bazel-exec-$step.log.zst" "$RUNNER_TEMP/bazel-profile-$step.json") - done - [ "${#args[@]}" -gt 0 ] || exit 0 - zstd --version || echo "::warning::zstd missing: no per-mnemonic data in this lane's summary" - python3 tools/bazel/ci_analytics_extract.py \ - --lane "$LANE" --mode "$MODE" --check-run-id "$CHECK_RUN_ID" --pr-hint "$PR_HINT" \ - "${args[@]}" \ - --out "$RUNNER_TEMP/ci-analytics/ci-analytics.json" - - - name: Upload CI analytics summary - if: "!cancelled() && steps.ci-analytics.outcome == 'success'" - continue-on-error: true - timeout-minutes: 2 - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7 - with: - overwrite: true # a job re-run is the same run: replace, not 409 - name: ci-analytics-pure-${{ github.run_attempt }} - path: ${{ runner.temp }}/ci-analytics/ci-analytics.json - retention-days: 7 - if-no-files-found: ignore - - # Last step: the job's result as a workflow_call output for pr.yml's - # ci-gate. always() runs it after a failure, a timeout or a - # cancellation too. - - name: Record job result - id: result - if: ${{ always() }} - env: - JOB_STATUS: ${{ job.status }} - run: echo "result=$JOB_STATUS" >> "$GITHUB_OUTPUT" - - # Every release target (scripts/ci/release-targets.txt) on every PR, so a - # platform-gating mistake is caught here instead of at tag time (#5662): - # the whole tree, cgo off, one remote `bazel build --platforms=...` per - # target (the script's header has the coverage boundary: the cgo release - # builds are not compiled here). nogo (//tools/nogo) validates every one of - # those compiles, so this is also the lint and vet gate for files only - # other platforms build (//go:build windows, darwin, !linux ...; - # engdocs/LINTING.md). A job of its own rather than a step of bazel-pure: - # release_cross is a split transition that shares no analysis with the pure - # invocations, and as a step it was about a minute on the longest cached - # lane. Same runner and skip rule as bazel-pure (it runs in every mode but - # skip). - bazel-release-cross: - name: release cross-compile - needs: rbe - runs-on: ${{ needs.rbe.outputs.mode == 'remote' && 'blacksmith-4vcpu-ubuntu-2404' || 'ubuntu-latest' }} - if: ${{ needs.rbe.outputs.mode != 'skip' }} - timeout-minutes: 90 - outputs: - result: ${{ steps.result.outputs.result }} - steps: - # inputs.checkout-sha: see the header (empty: the event's default ref). - # checkout v7 refuses a fork head on pull_request_target unless opted - # in; only bazel-farm.yml's authorized call opts in (false elsewhere). - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - with: - ref: ${{ inputs.checkout-sha }} - persist-credentials: false - allow-unsafe-pr-checkout: ${{ inputs.fork-farm == 'authorized' && github.event_name == 'pull_request_target' }} - - - name: Set up Bazel - id: bazel - uses: ./.github/actions/setup-bazel - env: - BAZEL_REMOTE_EXECUTOR: ${{ needs.rbe.outputs.mode == 'remote' && secrets.RBE_WEST_EXECUTOR || '' }} - RBE_TLS_CERT: ${{ needs.rbe.outputs.mode == 'remote' && secrets.RBE_TLS_CERT || '' }} - RBE_TLS_KEY: ${{ needs.rbe.outputs.mode == 'remote' && secrets.RBE_TLS_KEY || '' }} - RBE_TLS_CA: ${{ needs.rbe.outputs.mode == 'remote' && secrets.RBE_TLS_CA || '' }} - RBE_INSTANCE: ${{ needs.rbe.outputs.mode == 'remote' && 'oss' || '' }} - BAZEL_FORK_CACHE: ${{ needs.rbe.outputs.mode == 'cache' && 'true' || '' }} - BAZEL_FORK_REMOTE: ${{ startsWith(needs.rbe.outputs.mode, 'fork-') && 'true' || '' }} - RBE_FORK_TIER: ${{ needs.rbe.outputs.tier }} - RBE_FORK_PR: ${{ github.event.pull_request.number }} - - - name: Cross-compile every release target (--config=release-cross) - run: | - set -euo pipefail - ./scripts/ci/bazel-release-cross-compile.sh - - # Last step: the job's result as a workflow_call output for pr.yml's - # ci-gate. always() runs it after a failure, a timeout or a - # cancellation too. - - name: Record job result - id: result - if: ${{ always() }} - env: - JOB_STATUS: ${{ job.status }} - run: echo "result=$JOB_STATUS" >> "$GITHUB_OUTPUT" - - # pr-risk.yml's retired embedded-Dolt tier ("Test (Embedded Dolt Cmd N/20)", - # "Test (Embedded Dolt Storage N/5)", "Test (Embedded Dolt Conformance - - # core/audit)") as --config=embedded: those jobs, and the frozen 20-shard - # cmd block they read, no longer exist (see engdocs/TESTING.md) — this runs - # the embedded-tagged variants of - # //cmd/bd:bd_test and //internal/storage/embeddeddolt:embeddeddolt_test. - # The shard variants run the jobs' own shard scripts and manifests, so Bazel - # shard k runs exactly the tests of job k. A job of its own rather than a - # second invocation in bazel-test: the tier's long race shards stay off - # bazel-test's critical path and budget, and it compiles nothing bazel-test - # does not (same race configuration, shared through the remote cache). - bazel-embedded: - name: embedded Dolt tier - # Remote execution only: skipped unless the rbe job chose remote, - # fork-ro or fork-rw (enabled; not in modes cache, local or skip). - # Locally the tier is 72 race test - # processes (50 cmd/bd shards + 20 embeddeddolt storage shards + 2 - # conformance partitions; ~24k test-seconds measured locally -- see - # scripts/ci/embedded_cmd_test_durations.json and - # scripts/ci/embedded_storage_test_durations.json -- up from an earlier, - # smaller test surface's ~7-10k estimate) on one runner, several hours - # per run on the pool whose job cap this track exists to relieve, and it - # proved nothing pr-risk.yml's embedded jobs (which ran for forks before - # #7353 retired them) did not. - # - # Shard counts: storage (40): its tests are serial, so a shard's wall - # time is the sum of its tests, and CI shards run ~1.3-1.6x the rbe-west - # measurements; 40 is where the LPT-packed heaviest shard - # (scripts/ci/gen_embedded_storage_shard_manifest.py --weights=duration - # --repack) is its heaviest single top-level test, ~71s - # (TestLifecycleCloseReopenContract). cmd is a 100-shard block split - # over two targets, //cmd/bd:bd_embedded_test (shards 1-50) and - # //cmd/bd:bd_embedded_part2_test (51-100), because Bazel caps a rule at - # 50 shards ("Having more than 50 shards is indicative of poor test - # organization. Please reduce the number of shards." -- a native - # analysis-time error, not a style lint); the step below checks that - # the two ranges tile the block exactly once. After its slowest - # functions were split into smaller top-level tests, a cmd shard's wall - # time is its slowest test: ~45s median, and at most the *Concurrent - # stress tests' 65-145s (their bd subprocesses queue on one workspace's - # writer lock, so they cannot be split without changing what they test). On RBE the tier's wall time ~= that floor - # + fixed cost (checkout, build, shard dispatch), not ~= - # sum(durations)/shard_count the way a CPU-core-bound local sweep is -- - # each shard is its own remote action and runs concurrently with every - # other shard, farm capacity permitting. - # This lane is the tier's only PR run (pr-risk.yml's legacy jobs are - # retired) and pr.yml's gate requires it to have run here and passed. - # The runs-on expression is the shared one: Blacksmith in mode remote, - # GitHub-hosted in the fork modes (a client only). - needs: rbe - runs-on: ${{ needs.rbe.outputs.mode == 'remote' && 'blacksmith-4vcpu-ubuntu-2404' || 'ubuntu-latest' }} - if: ${{ needs.rbe.outputs.enabled == 'true' }} - timeout-minutes: 60 - outputs: - result: ${{ steps.result.outputs.result }} - steps: - # inputs.checkout-sha: see the header (empty: the event's default ref). - # checkout v7 refuses a fork head on pull_request_target unless opted - # in; only bazel-farm.yml's authorized call opts in (false elsewhere). - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - with: - ref: ${{ inputs.checkout-sha }} - persist-credentials: false - allow-unsafe-pr-checkout: ${{ inputs.fork-farm == 'authorized' && github.event_name == 'pull_request_target' }} - - # The remote repo contents cache, read-only, as in bazel-test. - - name: Remote repo contents cache (read) - if: ${{ needs.rbe.outputs.mode == 'remote' && needs.rbe.outputs.rrc == 'on' }} - run: | - { - echo 'startup --experimental_remote_repo_contents_cache' - echo 'common --loading_phase_threads=64' - } >> .bazelrc.local - - - name: Set up Bazel - id: bazel - uses: ./.github/actions/setup-bazel - env: - BAZEL_REMOTE_EXECUTOR: ${{ needs.rbe.outputs.mode == 'remote' && secrets.RBE_WEST_EXECUTOR || '' }} - RBE_TLS_CERT: ${{ needs.rbe.outputs.mode == 'remote' && secrets.RBE_TLS_CERT || '' }} - RBE_TLS_KEY: ${{ needs.rbe.outputs.mode == 'remote' && secrets.RBE_TLS_KEY || '' }} - RBE_TLS_CA: ${{ needs.rbe.outputs.mode == 'remote' && secrets.RBE_TLS_CA || '' }} - RBE_INSTANCE: ${{ needs.rbe.outputs.mode == 'remote' && 'oss' || '' }} - BAZEL_FORK_CACHE: ${{ needs.rbe.outputs.mode == 'cache' && 'true' || '' }} - BAZEL_RRC_READ: 'true' # mode cache reads the repo contents cache through rbe-cache - BAZEL_FORK_REMOTE: ${{ startsWith(needs.rbe.outputs.mode, 'fork-') && 'true' || '' }} - RBE_FORK_TIER: ${{ needs.rbe.outputs.tier }} - RBE_FORK_PR: ${{ github.event.pull_request.number }} - - # Bazel retries a flaky = True test (up to three attempts) and reports a - # later pass as PASSED. The policy test greps BUILD files for it; this - # asks Bazel itself, so a macro or a variable cannot hide one. Cheap: - # loading only, with the repository cache the test step needs anyway. - - name: No test target is marked flaky - run: | - flaky="$(bazel query 'attr(flaky, 1, tests(//...))')" - if [ -n "$flaky" ]; then - echo "::error::flaky = True on ${flaky//$'\n'/ }: the gated lanes run each test once" - exit 1 - fi - - # --build_event_json_file is a command option, so it goes after `test`. - - name: bazel test //... --config=embedded - id: test - timeout-minutes: 50 - run: | - set -o pipefail - start=$(date +%s) - rc=0 - bazel test //... --config=embedded ${BAZEL_FRESH:+"$BAZEL_FRESH"} \ - --build_event_json_file="$RUNNER_TEMP/bazel-bep.json" \ - --execution_log_compact_file="$RUNNER_TEMP/bazel-exec.log.zst" \ - --profile="$RUNNER_TEMP/bazel-profile.json" \ - --experimental_build_event_upload_strategy=local \ - 2>&1 | tee "$RUNNER_TEMP/bazel-test.log" || rc=$? - echo "bazel test --config=embedded: exit $rc, $(( $(date +%s) - start ))s wall" | tee -a "$GITHUB_STEP_SUMMARY" - exit "$rc" - - # A shard the manifest assigns nothing, or a selector that matches - # nothing, exits 0: fail if any target's or shard's test.xml lists no - # tests (or is missing). - - name: Every target and shard ran tests - if: ${{ always() && steps.test.outcome != 'skipped' }} - run: python3 tools/bazel/check_testcases.py --bep "$RUNNER_TEMP/bazel-bep.json" - - # This lane is the tier's only pre-merge run. The shard scripts discover tests from source, but each - # Bazel binary holds only its target's srcs: a listed test the binary - # lacks matches nothing and passes silently. Fail unless every Bazel - # shard ran exactly the tests its script lists (list-only mode, cheap), - # and unless every shard and conformance partition ran at least one - # top-level test that was not skipped (an injected -test.short or - # BEADS_TEST_SKIP turns the tier into t.Skip calls). - - name: Every listed test ran in its shard - if: ${{ always() && steps.test.outcome != 'skipped' }} - run: >- - python3 tools/bazel/check_shard_coverage.py - --bep "$RUNNER_TEMP/bazel-bep.json" - --suite //cmd/bd:bd_embedded_test .github/scripts/embedded-test-shard.sh 50@0/100 - --suite //cmd/bd:bd_embedded_part2_test .github/scripts/embedded-test-shard.sh 50@50/100 - --suite //internal/storage/embeddeddolt:embeddeddolt_embedded_test .github/scripts/embedded-storage-test-shard.sh 40 - --whole //internal/storage/embeddeddolt:embeddeddolt_conformance_core_test - --whole //internal/storage/embeddeddolt:embeddeddolt_conformance_core_slow_test - --whole //internal/storage/embeddeddolt:embeddeddolt_conformance_audit_test - - - name: Upload test logs - if: ${{ failure() && steps.test.outcome != 'skipped' }} - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7 - with: - overwrite: true # a job re-run is the same run: replace, not 409 - name: bazel-embedded-testlogs - path: | - bazel-testlogs/**/test.log - bazel-testlogs/**/test.xml - if-no-files-found: ignore - retention-days: 7 - - # The served targets' per-test durations, on every run: their pinned - # shard manifests (backend/http/served_pinned_shards.txt from - # tools/bazel/pin_shards.py, the corpus parts' split) are sized from a - # sharded run of this lane, and the logs above are kept only on failure. - # test.xml only, a few KB each. Telemetry like the CI analytics upload: - # nothing in CI reads it, so an artifact 5xx or stall must not fail or - # hold the lane (scripts TestBazelEmbeddedServedTimingsUploadIsTelemetry). - - name: Upload served test timings - if: ${{ !cancelled() && steps.test.outcome != 'skipped' }} - continue-on-error: true - timeout-minutes: 2 - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7 - with: - overwrite: true # a job re-run is the same run: replace, not 409 - name: bazel-embedded-served-testxml - path: bazel-testlogs/backend/http/**/test.xml - if-no-files-found: ignore - retention-days: 7 - - # Same redacted summary as bazel-test's; see its comment. - - name: CI analytics summary - id: ci-analytics - if: "!cancelled() && steps.test.outcome != 'skipped'" - continue-on-error: true - timeout-minutes: 3 - env: - LANE: embedded - MODE: ${{ needs.rbe.outputs.mode }} - CHECK_RUN_ID: ${{ job.check_run_id }} - PR_HINT: ${{ github.event.pull_request.number || 0 }} - run: | - [ -f "$RUNNER_TEMP/bazel-bep.json" ] || exit 0 - zstd --version || echo "::warning::zstd missing: no per-mnemonic data in this lane's summary" - python3 tools/bazel/ci_analytics_extract.py \ - --lane "$LANE" --mode "$MODE" --check-run-id "$CHECK_RUN_ID" --pr-hint "$PR_HINT" \ - --invocation test "$RUNNER_TEMP/bazel-bep.json" "$RUNNER_TEMP/bazel-exec.log.zst" "$RUNNER_TEMP/bazel-profile.json" \ - --out "$RUNNER_TEMP/ci-analytics/ci-analytics.json" - - - name: Upload CI analytics summary - if: "!cancelled() && steps.ci-analytics.outcome == 'success'" - continue-on-error: true - timeout-minutes: 2 - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7 - with: - overwrite: true # a job re-run is the same run: replace, not 409 - name: ci-analytics-embedded-${{ github.run_attempt }} - path: ${{ runner.temp }}/ci-analytics/ci-analytics.json - retention-days: 7 - if-no-files-found: ignore - - # Last step: the job's result as a workflow_call output for pr.yml's - # ci-gate. always() runs it after a failure, a timeout or a - # cancellation too. - - name: Record job result - id: result - if: ${{ always() }} - env: - JOB_STATUS: ${{ job.status }} - run: echo "result=$JOB_STATUS" >> "$GITHUB_OUTPUT" - - # main.yml's "Main Linux integration packages (N/6)" and "Main Linux - # integration cmd/bd (N/8)" jobs (and nightly.yml's retired Full Test - # Suite, the same go test in one job) as --config=integration: every package with - # -tags=integration,gms_pure_go, race, no -short, BEADS_TEST_SKIP=dolt (see - # .bazelrc). A job of its own: ci and integration share bazel-testlogs, so - # bazel-test's equivalence step would read this lane's test.xml if both ran - # in one job, and the tagged race build is a second full compile. - # No equivalence step: equivalence.py's default mode is the --config=ci - # selection (go list without the integration tag, -short, ^TestEmbedded - # skipped), and its job mode needs the integration jobs' go test -json, - # which no run here has. check_testcases.py still catches a target or - # shard that ran no tests. pr.yml's gate requires it (BAZEL_INTEGRATION) - # on same-repo PRs; see the header. - # Exactly-once cmd/bd: where the caller says bazel-cmd-dolt is required - # (inputs.cmd-dolt-required true: pr.yml while its - # BAZEL_CMD_DOLT_REQUIRED is "true"), the target set is //... minus - # //cmd/bd:bd_test. bazel-cmd-dolt has this job's `if:`, so it runs in - # every mode this one does (remote, fork-ro, fork-rw, cache; Dependabot - # too), bazel-gate.sh accepts the two lanes' skips in the same modes, and - # its //cmd/bd:bd_dolt_server_test runs that same integration-build - # bd_test without BEADS_TEST_SKIP=dolt (a superset: the skip only skips). - # Rollback: commit BAZEL_CMD_DOLT_REQUIRED "false" and pr.yml's - # cmd-dolt-required: false together (policy-tested equal); bd_test is - # back here on the next run. - bazel-integration: - name: integration - # Remote (modes remote, fork-ro, fork-rw), or local with the read-only - # cache (mode cache: fork and Dependabot PRs while rbe-fork is closed, - # rbe=cache dispatches), which serves every action - # trusted CI already ran, so only what the PR changed builds and tests - # on the GitHub-hosted runner. Never plain local (rbe=off): the tagged - # race build of //... plus 14 jobs' worth of tests on one runner, cold. - # A caller may turn it off with integration: "off"; pr.yml and - # bazel-farm.yml pass "on". - needs: rbe - runs-on: ${{ needs.rbe.outputs.mode == 'remote' && 'blacksmith-4vcpu-ubuntu-2404' || 'ubuntu-24.04' }} - if: ${{ (needs.rbe.outputs.enabled == 'true' || needs.rbe.outputs.mode == 'cache') && inputs.integration != 'off' }} - # F5 S2 Variant A added the "Nogo: integration library files" step after - # `test`; +5 over the previous 85 keeps it headroom without exceeding - # TestBazelIntegrationJob's test-step-timeout+15 ceiling (75+15=90). - timeout-minutes: 90 - outputs: - result: ${{ steps.result.outputs.result }} - steps: - # inputs.checkout-sha: see the header (empty: the event's default ref). - # checkout v7 refuses a fork head on pull_request_target unless opted - # in; only bazel-farm.yml's authorized call opts in (false elsewhere). - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - with: - ref: ${{ inputs.checkout-sha }} - persist-credentials: false - allow-unsafe-pr-checkout: ${{ inputs.fork-farm == 'authorized' && github.event_name == 'pull_request_target' }} - - # The remote repo contents cache, read-only, as in bazel-test. - - name: Remote repo contents cache (read) - if: ${{ needs.rbe.outputs.mode == 'remote' && needs.rbe.outputs.rrc == 'on' }} - run: | - { - echo 'startup --experimental_remote_repo_contents_cache' - echo 'common --loading_phase_threads=64' - } >> .bazelrc.local - - - name: Set up Bazel - id: bazel - uses: ./.github/actions/setup-bazel - env: - BAZEL_REMOTE_EXECUTOR: ${{ needs.rbe.outputs.mode == 'remote' && secrets.RBE_WEST_EXECUTOR || '' }} - RBE_TLS_CERT: ${{ needs.rbe.outputs.mode == 'remote' && secrets.RBE_TLS_CERT || '' }} - RBE_TLS_KEY: ${{ needs.rbe.outputs.mode == 'remote' && secrets.RBE_TLS_KEY || '' }} - RBE_TLS_CA: ${{ needs.rbe.outputs.mode == 'remote' && secrets.RBE_TLS_CA || '' }} - RBE_INSTANCE: ${{ needs.rbe.outputs.mode == 'remote' && 'oss' || '' }} - BAZEL_FORK_CACHE: ${{ needs.rbe.outputs.mode == 'cache' && 'true' || '' }} - BAZEL_RRC_READ: 'true' # mode cache reads the repo contents cache through rbe-cache - BAZEL_FORK_REMOTE: ${{ startsWith(needs.rbe.outputs.mode, 'fork-') && 'true' || '' }} - RBE_FORK_TIER: ${{ needs.rbe.outputs.tier }} - RBE_FORK_PR: ${{ github.event.pull_request.number }} - - # Remote: a cold compile (a PR touching a widely imported package; the - # first cold run took 17 minutes end to end, warm ones ~3.5) plus the - # longest test action (1200s cap, see .bazelrc) if it starts last. - # scripts/ci_workflow_test.go keeps this at or above that sum. Mode - # cache on a 4-CPU runner (simulated, 2026-10-02): ~2.5 minutes warm, - # 3-5 for a cmd/bd change, plus up to ~11 more when slow blob reads - # make Bazel restart the build ("Lost inputs"), and ~30 with the cache - # closed (the farm's kill switch: everything local, cold), which must - # still fit so closing the cache does not turn fork PRs red. - # Where bazel-cmd-dolt is required, //cmd/bd:bd_test is excluded (see - # above): on a PR that changes cmd/bd or what it imports, its race - # link and 16 shards were this step's last ~2-3.5 minutes (runs of - # 2026-10-05: 215-314s wall, 113-208s of it after every other test - # had finished). - - name: bazel test //... --config=integration - id: test - timeout-minutes: 75 - env: - EXCLUDE_TARGETS: ${{ inputs.cmd-dolt-required == true && '-//cmd/bd:bd_test' || '' }} - run: | - set -o pipefail - start=$(date +%s) - rc=0 - if [[ -n "$EXCLUDE_TARGETS" ]]; then - echo "bazel-cmd-dolt is required: excluding $EXCLUDE_TARGETS (it runs there)" | tee -a "$GITHUB_STEP_SUMMARY" - fi - bazel test //... --config=integration ${BAZEL_FRESH:+"$BAZEL_FRESH"} \ - --build_event_json_file="$RUNNER_TEMP/bazel-bep.json" \ - --execution_log_compact_file="$RUNNER_TEMP/bazel-exec.log.zst" \ - --profile="$RUNNER_TEMP/bazel-profile.json" \ - --experimental_build_event_upload_strategy=local \ - ${EXCLUDE_TARGETS:+-- "$EXCLUDE_TARGETS"} \ - 2>&1 | tee "$RUNNER_TEMP/bazel-test.log" || rc=$? - echo "bazel test --config=integration: exit $rc, $(( $(date +%s) - start ))s wall" | tee -a "$GITHUB_STEP_SUMMARY" - exit "$rc" - - # As in bazel-embedded. dolt_version_test and the repository guards in - # //scripts/repochecks are plain sh_tests (no Go testcases in their - # test.xml), which --config=integration's tag filter keeps, unlike the - # embedded one. - - name: Every target and shard ran tests - if: ${{ always() && steps.test.outcome != 'skipped' }} - run: python3 tools/bazel/check_testcases.py --bep "$RUNNER_TEMP/bazel-bep.json" --not-go //tools/bazel:dolt_version_test --not-go //scripts/repochecks:all - - # F5 S2 Variant A: the integration/doltserver-integration/doltserver-cmd - # configuration (--config=integration plus race) has no full-lane nogo - # owner (every lane above passes --norun_validations, .bazelrc). This - # narrow build keeps full nogo (vet + golangci-lint) on the - # configuration's non-test library package, - # //internal/testutil/integration, which every one of those lanes - # compiles; the 56 integration-tagged _test.go files this does not - # cover are the accepted loss (tools/nogo/unvalidated_integration_tests.txt, - # f5-spec.md §3). --keep_going so one finding does not hide another. - # scripts/nogo_lint_policy_test.go's - # TestNogoConfigurationsHaveOneValidatingLane pins this step as the - # configuration's sole validating lane. - - name: "Nogo: integration library files" - id: nogo-integration - if: ${{ always() && steps.test.outcome != 'skipped' }} - timeout-minutes: 10 - run: | - set -o pipefail - start=$(date +%s) - rc=0 - bazel build --config=integration --@rules_go//go/config:race --keep_going \ - --output_groups=nogo_fix \ - -- //internal/testutil/integration:integration \ - 2>&1 | tee "$RUNNER_TEMP/bazel-nogo-integration.log" || rc=$? - procs=$(grep -oE 'INFO: [0-9]+ process' "$RUNNER_TEMP/bazel-nogo-integration.log" | tail -1 || true) - echo "nogo-integration: exit $rc, $(( $(date +%s) - start ))s wall${procs:+, $procs}" | tee -a "$GITHUB_STEP_SUMMARY" - exit "$rc" - - - name: Upload test logs - if: ${{ failure() && steps.test.outcome != 'skipped' }} - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7 - with: - overwrite: true # a job re-run is the same run: replace, not 409 - name: bazel-integration-testlogs - path: | - bazel-testlogs/**/test.log - bazel-testlogs/**/test.xml - if-no-files-found: ignore - retention-days: 7 - - # Same redacted summary as bazel-test's; see its comment. - - name: CI analytics summary - id: ci-analytics - if: "!cancelled() && steps.test.outcome != 'skipped'" - continue-on-error: true - timeout-minutes: 3 - env: - LANE: integration - MODE: ${{ needs.rbe.outputs.mode }} - CHECK_RUN_ID: ${{ job.check_run_id }} - PR_HINT: ${{ github.event.pull_request.number || 0 }} - run: | - [ -f "$RUNNER_TEMP/bazel-bep.json" ] || exit 0 - zstd --version || echo "::warning::zstd missing: no per-mnemonic data in this lane's summary" - python3 tools/bazel/ci_analytics_extract.py \ - --lane "$LANE" --mode "$MODE" --check-run-id "$CHECK_RUN_ID" --pr-hint "$PR_HINT" \ - --invocation test "$RUNNER_TEMP/bazel-bep.json" "$RUNNER_TEMP/bazel-exec.log.zst" "$RUNNER_TEMP/bazel-profile.json" \ - --out "$RUNNER_TEMP/ci-analytics/ci-analytics.json" - - - name: Upload CI analytics summary - if: "!cancelled() && steps.ci-analytics.outcome == 'success'" - continue-on-error: true - timeout-minutes: 2 - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7 - with: - overwrite: true # a job re-run is the same run: replace, not 409 - name: ci-analytics-integration-${{ github.run_attempt }} - path: ${{ runner.temp }}/ci-analytics/ci-analytics.json - retention-days: 7 - if-no-files-found: ignore - - # Last step: the job's result as a workflow_call output for pr.yml's - # ci-gate. always() runs it after a failure, a timeout or a - # cancellation too. - - name: Record job result - id: result - if: ${{ always() }} - env: - JOB_STATUS: ${{ job.status }} - run: echo "result=$JOB_STATUS" >> "$GITHUB_OUTPUT" - - # pr.yml's retired container-backed jobs, "Test (storage domain + uow)" - # and "Contract corpus", as Bazel's dolt-server lane: --config=doltserver - # runs the dolt-server variants (domain/..., uow in 8 shards, tracker/..., - # doctor/fix, protocol in 8 shards, testutil), each of which starts its - # own dolt sql-server from the pinned dolt, so they execute on the remote - # workers like any other test (locally with rbe=off). - # Not in mode remote: there bazel-dolt-race runs this tier together with - # the proxied-server tier and reports for both (item 5). In the fork - # modes, cache and local there is no in-flight deduplication of one - # client's actions and the client does the work, so a union would cost the - # sum of the two tiers instead of the max: this job runs there as before. - bazel-doltserver: - name: dolt-server lane - # Same runner as bazel-test; bazel-test's skip rule, plus mode remote - # (bazel-dolt-race's). - needs: rbe - runs-on: ${{ needs.rbe.outputs.mode == 'remote' && 'blacksmith-4vcpu-ubuntu-2404' || 'ubuntu-24.04' }} - if: ${{ needs.rbe.outputs.mode != 'skip' && needs.rbe.outputs.mode != 'remote' }} - timeout-minutes: 90 - outputs: - result: ${{ steps.result.outputs.result }} - env: - # This lane is the only pre-merge run of these suites (the go test - # jobs it replaced never retried): where it executes remotely - # --config=sole-run fails on a remote cache eviction instead of - # re-running the whole invocation (whose retry could turn a failed - # test green). - # Test results are cached and reused like every lane's; only nightly - # re-executes them all (BAZEL_FRESH). - BAZEL_SOLE_RUN: ${{ needs.rbe.outputs.enabled == 'true' && '--config=sole-run' || '' }} - steps: - # inputs.checkout-sha: see the header (empty: the event's default ref). - # checkout v7 refuses a fork head on pull_request_target unless opted - # in; only bazel-farm.yml's authorized call opts in (false elsewhere). - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - with: - ref: ${{ inputs.checkout-sha }} - persist-credentials: false - allow-unsafe-pr-checkout: ${{ inputs.fork-farm == 'authorized' && github.event_name == 'pull_request_target' }} - - # The remote repo contents cache, read-only, as in bazel-test. - - name: Remote repo contents cache (read) - if: ${{ needs.rbe.outputs.mode == 'remote' && needs.rbe.outputs.rrc == 'on' }} - run: | - { - echo 'startup --experimental_remote_repo_contents_cache' - echo 'common --loading_phase_threads=64' - } >> .bazelrc.local - - - name: Set up Bazel - id: bazel - uses: ./.github/actions/setup-bazel - env: - BAZEL_REMOTE_EXECUTOR: ${{ needs.rbe.outputs.mode == 'remote' && secrets.RBE_WEST_EXECUTOR || '' }} - RBE_TLS_CERT: ${{ needs.rbe.outputs.mode == 'remote' && secrets.RBE_TLS_CERT || '' }} - RBE_TLS_KEY: ${{ needs.rbe.outputs.mode == 'remote' && secrets.RBE_TLS_KEY || '' }} - RBE_TLS_CA: ${{ needs.rbe.outputs.mode == 'remote' && secrets.RBE_TLS_CA || '' }} - RBE_INSTANCE: ${{ needs.rbe.outputs.mode == 'remote' && 'oss' || '' }} - BAZEL_FORK_CACHE: ${{ needs.rbe.outputs.mode == 'cache' && 'true' || '' }} - BAZEL_RRC_READ: 'true' # mode cache reads the repo contents cache through rbe-cache - BAZEL_FORK_REMOTE: ${{ startsWith(needs.rbe.outputs.mode, 'fork-') && 'true' || '' }} - RBE_FORK_TIER: ${{ needs.rbe.outputs.tier }} - RBE_FORK_PR: ${{ github.event.pull_request.number }} - - - name: bazel test //... --config=doltserver - id: test - timeout-minutes: 80 - run: | - set -o pipefail - start=$(date +%s) - rc=0 - bazel test //... --config=doltserver ${BAZEL_SOLE_RUN:+"$BAZEL_SOLE_RUN"} ${BAZEL_FRESH:+"$BAZEL_FRESH"} \ - --build_event_json_file="$RUNNER_TEMP/bazel-bep.json" \ - --execution_log_compact_file="$RUNNER_TEMP/bazel-exec.log.zst" \ - --profile="$RUNNER_TEMP/bazel-profile.json" \ - --experimental_build_event_upload_strategy=local \ - 2>&1 | tee "$RUNNER_TEMP/bazel-test.log" || rc=$? - echo "bazel test --config=doltserver: exit $rc, $(( $(date +%s) - start ))s wall" | tee -a "$GITHUB_STEP_SUMMARY" - exit "$rc" - - - name: Upload test logs - if: ${{ failure() && steps.test.outcome == 'failure' }} - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7 - with: - overwrite: true # a job re-run is the same run: replace, not 409 - name: bazel-doltserver-testlogs - path: | - bazel-testlogs/**/test.log - bazel-testlogs/**/test.xml - if-no-files-found: ignore - retention-days: 7 - - # Same redacted summary as bazel-test's; see its comment. - - name: CI analytics summary - id: ci-analytics - if: "!cancelled() && steps.test.outcome != 'skipped'" - continue-on-error: true - timeout-minutes: 3 - env: - LANE: doltserver - MODE: ${{ needs.rbe.outputs.mode }} - CHECK_RUN_ID: ${{ job.check_run_id }} - PR_HINT: ${{ github.event.pull_request.number || 0 }} - run: | - [ -f "$RUNNER_TEMP/bazel-bep.json" ] || exit 0 - zstd --version || echo "::warning::zstd missing: no per-mnemonic data in this lane's summary" - python3 tools/bazel/ci_analytics_extract.py \ - --lane "$LANE" --mode "$MODE" --check-run-id "$CHECK_RUN_ID" --pr-hint "$PR_HINT" \ - --invocation test "$RUNNER_TEMP/bazel-bep.json" "$RUNNER_TEMP/bazel-exec.log.zst" "$RUNNER_TEMP/bazel-profile.json" \ - --out "$RUNNER_TEMP/ci-analytics/ci-analytics.json" - - - name: Upload CI analytics summary - if: "!cancelled() && steps.ci-analytics.outcome == 'success'" - continue-on-error: true - timeout-minutes: 2 - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7 - with: - overwrite: true # a job re-run is the same run: replace, not 409 - name: ci-analytics-doltserver-${{ github.run_attempt }} - path: ${{ runner.temp }}/ci-analytics/ci-analytics.json - retention-days: 7 - if-no-files-found: ignore - - # Last step: the job's result as a workflow_call output for pr.yml's - # ci-gate. always() runs it after a failure, a timeout or a - # cancellation too. - - name: Record job result - id: result - if: ${{ always() }} - env: - JOB_STATUS: ${{ job.status }} - run: echo "result=$JOB_STATUS" >> "$GITHUB_OUTPUT" - - # Like pr-risk.yml's retired proxied-server tier, "Test (Proxied Dolt Cmd - # N/15)" (that job, and the frozen 15-shard block it read, no longer exist; - # see engdocs/TESTING.md), as --config=doltserver-proxied: - # //cmd/bd:bd_proxied_test runs the shard script, so shard k runs exactly - # that manifest entry's tests. This lane runs 34 shards of its own - # (.github/scripts/proxied-cmd-test-shards.txt's duration-balanced 34-block; - # see scripts/ci/proxied_test_durations.json) — shard k here was never the - # same split as the retired job's shard k. Same build as --config=doltserver - # (race bd_test, non-race - # bd_for_tests), shared with bazel-doltserver through the remote cache. - # The test binary is race, as PR Risk's retired jobs built it. It also runs - # //cmd/bd:bd_managed_local_test, the managed-local proxied lifecycle - # (formerly proxied-local-smoke.yml) in a loopback-only network - # namespace, which needs the workers' unprivileged user namespaces. A job - # of its own, not gated by any integration switch: it runs in pr.yml's - # call and its gate requires it (BAZEL_PROXIED). Fork modes only: in mode - # remote bazel-dolt-race runs this tier (see bazel-doltserver's comment). - bazel-proxied: - name: proxied-server tier - # Remote execution only, like bazel-embedded: 30 concurrent shards, each - # running its own dolt sql-server busy on ~4 cores, do not fit a - # GitHub-hosted runner regardless of any single shard's wall time. - # This lane is the tier's only PR run (pr-risk.yml's legacy jobs are - # retired) and pr.yml's gate requires it to have run here and passed. - needs: rbe - runs-on: ${{ needs.rbe.outputs.mode == 'remote' && 'blacksmith-4vcpu-ubuntu-2404' || 'ubuntu-latest' }} - if: ${{ needs.rbe.outputs.enabled == 'true' && needs.rbe.outputs.mode != 'remote' }} - timeout-minutes: 30 - outputs: - result: ${{ steps.result.outputs.result }} - steps: - # inputs.checkout-sha: see the header (empty: the event's default ref). - # checkout v7 refuses a fork head on pull_request_target unless opted - # in; only bazel-farm.yml's authorized call opts in (false elsewhere). - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - with: - ref: ${{ inputs.checkout-sha }} - persist-credentials: false - allow-unsafe-pr-checkout: ${{ inputs.fork-farm == 'authorized' && github.event_name == 'pull_request_target' }} - - # The remote repo contents cache, read-only, as in bazel-test. - - name: Remote repo contents cache (read) - if: ${{ needs.rbe.outputs.mode == 'remote' && needs.rbe.outputs.rrc == 'on' }} - run: | - { - echo 'startup --experimental_remote_repo_contents_cache' - echo 'common --loading_phase_threads=64' - } >> .bazelrc.local - - - name: Set up Bazel - id: bazel - uses: ./.github/actions/setup-bazel - env: - BAZEL_REMOTE_EXECUTOR: ${{ needs.rbe.outputs.mode == 'remote' && secrets.RBE_WEST_EXECUTOR || '' }} - RBE_TLS_CERT: ${{ needs.rbe.outputs.mode == 'remote' && secrets.RBE_TLS_CERT || '' }} - RBE_TLS_KEY: ${{ needs.rbe.outputs.mode == 'remote' && secrets.RBE_TLS_KEY || '' }} - RBE_TLS_CA: ${{ needs.rbe.outputs.mode == 'remote' && secrets.RBE_TLS_CA || '' }} - RBE_INSTANCE: ${{ needs.rbe.outputs.mode == 'remote' && 'oss' || '' }} - BAZEL_FORK_CACHE: ${{ needs.rbe.outputs.mode == 'cache' && 'true' || '' }} - BAZEL_RRC_READ: 'true' # mode cache reads the repo contents cache through rbe-cache - BAZEL_FORK_REMOTE: ${{ startsWith(needs.rbe.outputs.mode, 'fork-') && 'true' || '' }} - RBE_FORK_TIER: ${{ needs.rbe.outputs.tier }} - RBE_FORK_PR: ${{ github.event.pull_request.number }} - - # Bazel retries a flaky = True test (up to three attempts) and reports a - # later pass as PASSED. The policy test greps BUILD files for it; this - # asks Bazel itself, so a macro or a variable cannot hide one. Cheap: - # loading only, with the repository cache the test step needs anyway. - - name: No test target is marked flaky - run: | - flaky="$(bazel query 'attr(flaky, 1, tests(//...))')" - if [ -n "$flaky" ]; then - echo "::error::flaky = True on ${flaky//$'\n'/ }: the gated lanes run each test once" - exit 1 - fi - - # Remote: each shard's Go timeout is its job's 15m, 1200s Bazel cap, - # plus a compile. The 34-shard block is LPT-packed by - # scripts/ci/proxied_test_durations.json, a rbe-west capture of each - # top-level test's summed leaf-test time (see its header), to ~187 - # cost-seconds per shard; shards are expected around 55-95s (the same - # packed cost has run up to 1.45x the median wall time). Nearly - # all of that is `bd init --proxied-server` against the shard's dolt - # sql-server, ~one per subtest, each as a user scoped to its own - # database (cmd/bd/proxied_shared_harness_test.go); a shard running far - # longer is worth comparing against this job's recent history before - # assuming a hang. - - name: bazel test //... --config=doltserver-proxied - id: test - timeout-minutes: 25 - run: | - set -o pipefail - start=$(date +%s) - rc=0 - bazel test //... --config=doltserver-proxied ${BAZEL_FRESH:+"$BAZEL_FRESH"} \ - --build_event_json_file="$RUNNER_TEMP/bazel-bep.json" \ - --execution_log_compact_file="$RUNNER_TEMP/bazel-exec.log.zst" \ - --profile="$RUNNER_TEMP/bazel-profile.json" \ - --experimental_build_event_upload_strategy=local \ - 2>&1 | tee "$RUNNER_TEMP/bazel-test.log" || rc=$? - echo "bazel test --config=doltserver-proxied: exit $rc, $(( $(date +%s) - start ))s wall" | tee -a "$GITHUB_STEP_SUMMARY" - exit "$rc" - - # As in bazel-embedded. - - name: Every target and shard ran tests - if: ${{ always() && steps.test.outcome != 'skipped' }} - run: python3 tools/bazel/check_testcases.py --bep "$RUNNER_TEMP/bazel-bep.json" - - # This lane is the tier's only pre-merge run: as in bazel-embedded, every Bazel shard must run - # exactly the tests its shard script lists, and not only skips. - - name: Every listed test ran in its shard - if: ${{ always() && steps.test.outcome != 'skipped' }} - run: >- - python3 tools/bazel/check_shard_coverage.py - --bep "$RUNNER_TEMP/bazel-bep.json" - --suite //cmd/bd:bd_proxied_test .github/scripts/proxied-test-shard.sh 34 - - - name: Upload test logs - if: ${{ failure() && steps.test.outcome != 'skipped' }} - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7 - with: - overwrite: true # a job re-run is the same run: replace, not 409 - name: bazel-proxied-testlogs - path: | - bazel-testlogs/**/test.log - bazel-testlogs/**/test.xml - if-no-files-found: ignore - retention-days: 7 - - # Same redacted summary as bazel-test's; see its comment. - - name: CI analytics summary - id: ci-analytics - if: "!cancelled() && steps.test.outcome != 'skipped'" - continue-on-error: true - timeout-minutes: 3 - env: - LANE: proxied-server - MODE: ${{ needs.rbe.outputs.mode }} - CHECK_RUN_ID: ${{ job.check_run_id }} - PR_HINT: ${{ github.event.pull_request.number || 0 }} - run: | - [ -f "$RUNNER_TEMP/bazel-bep.json" ] || exit 0 - zstd --version || echo "::warning::zstd missing: no per-mnemonic data in this lane's summary" - python3 tools/bazel/ci_analytics_extract.py \ - --lane "$LANE" --mode "$MODE" --check-run-id "$CHECK_RUN_ID" --pr-hint "$PR_HINT" \ - --invocation test "$RUNNER_TEMP/bazel-bep.json" "$RUNNER_TEMP/bazel-exec.log.zst" "$RUNNER_TEMP/bazel-profile.json" \ - --out "$RUNNER_TEMP/ci-analytics/ci-analytics.json" - - - name: Upload CI analytics summary - if: "!cancelled() && steps.ci-analytics.outcome == 'success'" - continue-on-error: true - timeout-minutes: 2 - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7 - with: - overwrite: true # a job re-run is the same run: replace, not 409 - name: ci-analytics-proxied-server-${{ github.run_attempt }} - path: ${{ runner.temp }}/ci-analytics/ci-analytics.json - retention-days: 7 - if-no-files-found: ignore - - # Last step: the job's result as a workflow_call output for pr.yml's - # ci-gate. always() runs it after a failure, a timeout or a - # cancellation too. - - name: Record job result - id: result - if: ${{ always() }} - env: - JOB_STATUS: ${{ job.status }} - run: echo "result=$JOB_STATUS" >> "$GITHUB_OUTPUT" - - # Item 5 (scripts/bazel_dolt_race_test.go): the dolt-server and - # proxied-server tiers in one `bazel test --config=dolt-race` (.bazelrc: - # exactly the union of --config=doltserver's and - # --config=doltserver-proxied's lines, one tag filter for both), mode - # remote only, in place of bazel-doltserver and bazel-proxied, which run - # in every other mode. One client waits on rbe-west for both tiers' - # shards at once (27 + 35 test actions, under the remote-exec config's 64 - # jobs), so the job ends with the slower tier and saves the other's - # runner. Each member target's test action key is unchanged (aquery- - # verified), so cached results carry over both ways, and a revert of the - # cutover re-executes nothing. Gated: its job.status is both the - # bazel-doltserver and the bazel-proxied workflow_call output (a failure - # in either tier fails both ids, which are both required). Every step has - # a timeout and the job's exceeds their sum by 5 minutes while staying - # under the merge queue's 60-minute check timeout - # (TestBazelDoltRaceJobTimeouts), so a hang fails a named step instead of - # dropping the merge group. The member jobs' configs and the union's must - # stay key-identical: the mode decides which runs (TestBazelDoltRaceConfigIsMemberUnion). - # Its ci-analytics LANE is dolt-race. - bazel-dolt-race: - name: dolt-race - needs: rbe - runs-on: ${{ needs.rbe.outputs.mode == 'remote' && 'blacksmith-4vcpu-ubuntu-2404' || 'ubuntu-latest' }} - if: ${{ needs.rbe.outputs.mode == 'remote' }} - # At least every step's timeout-minutes plus 5, and below the merge - # queue's 60-minute check timeout so this job alone cannot exhaust it - # (TestBazelDoltRaceJobTimeouts). Not a whole-run bound: the rbe job runs - # first and sibling lanes have longer timeouts. - timeout-minutes: 56 - outputs: - result: ${{ steps.result.outputs.result }} - steps: - # inputs.checkout-sha: see the header (empty: the event's default ref). - # checkout v7 refuses a fork head on pull_request_target unless opted - # in; only bazel-farm.yml's authorized call opts in (false elsewhere). - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - timeout-minutes: 5 - with: - ref: ${{ inputs.checkout-sha }} - persist-credentials: false - allow-unsafe-pr-checkout: ${{ inputs.fork-farm == 'authorized' && github.event_name == 'pull_request_target' }} - - # The remote repo contents cache, read-only, as in bazel-test. - - name: Remote repo contents cache (read) - if: ${{ needs.rbe.outputs.mode == 'remote' && needs.rbe.outputs.rrc == 'on' }} - timeout-minutes: 1 - run: | - { - echo 'startup --experimental_remote_repo_contents_cache' - echo 'common --loading_phase_threads=64' - } >> .bazelrc.local - - - name: Set up Bazel - id: bazel - timeout-minutes: 5 - uses: ./.github/actions/setup-bazel - env: - BAZEL_REMOTE_EXECUTOR: ${{ needs.rbe.outputs.mode == 'remote' && secrets.RBE_WEST_EXECUTOR || '' }} - RBE_TLS_CERT: ${{ needs.rbe.outputs.mode == 'remote' && secrets.RBE_TLS_CERT || '' }} - RBE_TLS_KEY: ${{ needs.rbe.outputs.mode == 'remote' && secrets.RBE_TLS_KEY || '' }} - RBE_TLS_CA: ${{ needs.rbe.outputs.mode == 'remote' && secrets.RBE_TLS_CA || '' }} - RBE_INSTANCE: ${{ needs.rbe.outputs.mode == 'remote' && 'oss' || '' }} - BAZEL_FORK_CACHE: ${{ needs.rbe.outputs.mode == 'cache' && 'true' || '' }} - BAZEL_RRC_READ: 'true' # mode cache reads the repo contents cache through rbe-cache - BAZEL_FORK_REMOTE: ${{ startsWith(needs.rbe.outputs.mode, 'fork-') && 'true' || '' }} - RBE_FORK_TIER: ${{ needs.rbe.outputs.tier }} - RBE_FORK_PR: ${{ github.event.pull_request.number }} - - # As in bazel-proxied. - - name: No test target is marked flaky - timeout-minutes: 3 - run: | - flaky="$(bazel query 'attr(flaky, 1, tests(//...))')" - if [ -n "$flaky" ]; then - echo "::error::flaky = True on ${flaky//$'\n'/ }: the gated lanes run each test once" - exit 1 - fi - - # Both tiers' shards at once: 27 dolt-server + 35 proxied test actions, - # under the remote-exec config's 64 jobs. No --config=sole-run: its one - # line (no retry on a remote cache eviction) is already in the union. - # Shards are expected around 55-95s on rbe-west (see bazel-proxied's - # test step), the proxied tier's 1200s cap included. The step timeout is - # >= proxied test_timeout 1200 s + about 3 min so Bazel writes TIMEOUT - # logs before GitHub kills the step. - - name: bazel test //... --config=dolt-race - id: test - timeout-minutes: 23 - run: | - set -o pipefail - start=$(date +%s) - rc=0 - bazel test //... --config=dolt-race ${BAZEL_FRESH:+"$BAZEL_FRESH"} \ - --build_event_json_file="$RUNNER_TEMP/bazel-bep.json" \ - --execution_log_compact_file="$RUNNER_TEMP/bazel-exec.log.zst" \ - --profile="$RUNNER_TEMP/bazel-profile.json" \ - --experimental_build_event_upload_strategy=local \ - 2>&1 | tee "$RUNNER_TEMP/bazel-test.log" || rc=$? - echo "bazel test --config=dolt-race: exit $rc, $(( $(date +%s) - start ))s wall" | tee -a "$GITHUB_STEP_SUMMARY" - exit "$rc" - - # The member lanes' checks, once over the union's BEP: bazel-proxied's - # (the dolt-server lane has none). check_shard_coverage ignores targets - # it does not list; check_testcases checks every target, so it covers - # the dolt-server tier too, which bazel-doltserver does not check: a - # dolt-server shard that runs no test turns this job red. - - name: Every target and shard ran tests - if: ${{ always() && steps.test.outcome != 'skipped' }} - timeout-minutes: 2 - run: python3 tools/bazel/check_testcases.py --bep "$RUNNER_TEMP/bazel-bep.json" - - - name: Every listed test ran in its shard - if: ${{ always() && steps.test.outcome != 'skipped' }} - timeout-minutes: 2 - run: >- - python3 tools/bazel/check_shard_coverage.py - --bep "$RUNNER_TEMP/bazel-bep.json" - --suite //cmd/bd:bd_proxied_test .github/scripts/proxied-test-shard.sh 34 - - - name: Upload test logs - if: ${{ failure() && steps.test.outcome != 'skipped' }} - timeout-minutes: 3 - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7 - with: - overwrite: true # a job re-run is the same run: replace, not 409 - name: bazel-dolt-race-testlogs - path: | - bazel-testlogs/**/test.log - bazel-testlogs/**/test.xml - if-no-files-found: ignore - retention-days: 7 - - # Same redacted summary as bazel-test's; see its comment. - - name: CI analytics summary - id: ci-analytics - if: "!cancelled() && steps.test.outcome != 'skipped'" - continue-on-error: true - timeout-minutes: 2 - env: - LANE: dolt-race - MODE: ${{ needs.rbe.outputs.mode }} - CHECK_RUN_ID: ${{ job.check_run_id }} - PR_HINT: ${{ github.event.pull_request.number || 0 }} - run: | - [ -f "$RUNNER_TEMP/bazel-bep.json" ] || exit 0 - zstd --version || echo "::warning::zstd missing: no per-mnemonic data in this lane's summary" - python3 tools/bazel/ci_analytics_extract.py \ - --lane "$LANE" --mode "$MODE" --check-run-id "$CHECK_RUN_ID" --pr-hint "$PR_HINT" \ - --invocation test "$RUNNER_TEMP/bazel-bep.json" "$RUNNER_TEMP/bazel-exec.log.zst" "$RUNNER_TEMP/bazel-profile.json" \ - --out "$RUNNER_TEMP/ci-analytics/ci-analytics.json" - - - name: Upload CI analytics summary - if: "!cancelled() && steps.ci-analytics.outcome == 'success'" - continue-on-error: true - timeout-minutes: 2 - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7 - with: - overwrite: true # a job re-run is the same run: replace, not 409 - name: ci-analytics-dolt-race-${{ github.run_attempt }} - path: ${{ runner.temp }}/ci-analytics/ci-analytics.json - retention-days: 7 - if-no-files-found: ignore - - # Last step: the job's result as the bazel-doltserver and - # bazel-proxied workflow_call outputs for pr.yml's ci-gate. always() - # runs it after a failure, a timeout or a cancellation too. - - name: Record job result - id: result - if: ${{ always() }} - timeout-minutes: 1 - env: - JOB_STATUS: ${{ job.status }} - run: echo "result=$JOB_STATUS" >> "$GITHUB_OUTPUT" - - # pr-risk.yml's server-Dolt storage tier, "Test (Server Dolt Conformance)" - # and "Test (Server Dolt Full Suite N/16)", as --config=doltserver-integration: - # the conformance and 16-shard full-suite variants of - # //internal/storage/dolt over a non-race dolt_test build, each shard - # running the job's own shard script. --config=doltserver-integration has - # exactly --config=integration's build flags, so it compiles nothing - # bazel-integration does not (shared through the remote cache). This is a - # PR-time tier, unlike main.yml's integration jobs, and pr.yml's gate - # requires it (BAZEL_SERVER_STORAGE); it also honors the same - # `integration` input as bazel-integration/bazel-cmd-dolt so an explicit - # "off" skips it too, even though no caller passes that today (pr.yml and - # bazel-farm.yml both pass "on") -- see the `integration` input's own - # description. - # Not in mode remote: there bazel-dolt-int runs this tier together with - # the cmd/bd Dolt-server tier and reports for both (item 5 A2). In the - # fork modes and cache there is no in-flight deduplication of one client's - # actions, so a union would cost the sum of the two tiers instead of the - # max: this job runs there as before. - bazel-server-storage: - name: server-Dolt storage tier - # Remote execution only, like bazel-integration: locally the tagged race - # build of //... alone is most of the timeout. Plus mode remote - # (bazel-dolt-int's). - # This lane is the tier's only PR run (pr-risk.yml's legacy jobs are - # retired) and pr.yml's gate requires it to have run here and passed. - needs: rbe - runs-on: ${{ needs.rbe.outputs.mode == 'remote' && 'blacksmith-4vcpu-ubuntu-2404' || 'ubuntu-latest' }} - if: ${{ needs.rbe.outputs.enabled == 'true' && needs.rbe.outputs.mode != 'remote' && inputs.integration != 'off' }} - timeout-minutes: 30 - outputs: - result: ${{ steps.result.outputs.result }} - steps: - # inputs.checkout-sha: see the header (empty: the event's default ref). - # checkout v7 refuses a fork head on pull_request_target unless opted - # in; only bazel-farm.yml's authorized call opts in (false elsewhere). - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - with: - ref: ${{ inputs.checkout-sha }} - persist-credentials: false - allow-unsafe-pr-checkout: ${{ inputs.fork-farm == 'authorized' && github.event_name == 'pull_request_target' }} - - # The remote repo contents cache, read-only, as in bazel-test. - - name: Remote repo contents cache (read) - if: ${{ needs.rbe.outputs.mode == 'remote' && needs.rbe.outputs.rrc == 'on' }} - run: | - { - echo 'startup --experimental_remote_repo_contents_cache' - echo 'common --loading_phase_threads=64' - } >> .bazelrc.local - - - name: Set up Bazel - id: bazel - uses: ./.github/actions/setup-bazel - env: - BAZEL_REMOTE_EXECUTOR: ${{ needs.rbe.outputs.mode == 'remote' && secrets.RBE_WEST_EXECUTOR || '' }} - RBE_TLS_CERT: ${{ needs.rbe.outputs.mode == 'remote' && secrets.RBE_TLS_CERT || '' }} - RBE_TLS_KEY: ${{ needs.rbe.outputs.mode == 'remote' && secrets.RBE_TLS_KEY || '' }} - RBE_TLS_CA: ${{ needs.rbe.outputs.mode == 'remote' && secrets.RBE_TLS_CA || '' }} - RBE_INSTANCE: ${{ needs.rbe.outputs.mode == 'remote' && 'oss' || '' }} - BAZEL_FORK_CACHE: ${{ needs.rbe.outputs.mode == 'cache' && 'true' || '' }} - BAZEL_RRC_READ: 'true' # mode cache reads the repo contents cache through rbe-cache - BAZEL_FORK_REMOTE: ${{ startsWith(needs.rbe.outputs.mode, 'fork-') && 'true' || '' }} - RBE_FORK_TIER: ${{ needs.rbe.outputs.tier }} - RBE_FORK_PR: ${{ github.event.pull_request.number }} - - # Bazel retries a flaky = True test (up to three attempts) and reports a - # later pass as PASSED. The policy test greps BUILD files for it; this - # asks Bazel itself, so a macro or a variable cannot hide one. Cheap: - # loading only, with the repository cache the test step needs anyway. - - name: No test target is marked flaky - run: | - flaky="$(bazel query 'attr(flaky, 1, tests(//...))')" - if [ -n "$flaky" ]; then - echo "::error::flaky = True on ${flaky//$'\n'/ }: the gated lanes run each test once" - exit 1 - fi - - # Remote: the longest shard takes ~2-3 minutes (the storage suite's Go - # timeout is 15m per shard, 1200s Bazel cap), plus a compile. - - name: bazel test //... --config=doltserver-integration - id: test - timeout-minutes: 25 - run: | - set -o pipefail - start=$(date +%s) - rc=0 - bazel test //... --config=doltserver-integration ${BAZEL_FRESH:+"$BAZEL_FRESH"} \ - --build_event_json_file="$RUNNER_TEMP/bazel-bep.json" \ - --execution_log_compact_file="$RUNNER_TEMP/bazel-exec.log.zst" \ - --profile="$RUNNER_TEMP/bazel-profile.json" \ - --experimental_build_event_upload_strategy=local \ - 2>&1 | tee "$RUNNER_TEMP/bazel-test.log" || rc=$? - echo "bazel test --config=doltserver-integration: exit $rc, $(( $(date +%s) - start ))s wall" | tee -a "$GITHUB_STEP_SUMMARY" - exit "$rc" - - # As in bazel-embedded. - - name: Every target and shard ran tests - if: ${{ always() && steps.test.outcome != 'skipped' }} - run: python3 tools/bazel/check_testcases.py --bep "$RUNNER_TEMP/bazel-bep.json" - - # This lane is the tier's only pre-merge run: as in bazel-embedded, every full-suite shard must run - # exactly the tests its shard script lists, and neither it nor the - # conformance target only skips. - - name: Every listed test ran in its shard - if: ${{ always() && steps.test.outcome != 'skipped' }} - run: >- - python3 tools/bazel/check_shard_coverage.py - --bep "$RUNNER_TEMP/bazel-bep.json" - --suite //internal/storage/dolt:dolt_server_full_test .github/scripts/server-storage-test-shard.sh 16 - --whole //internal/storage/dolt:dolt_server_conformance_test - - # rbe-ci-cost-latency-study.md recommendation 4: this tier's last - # shard has trailed the rest by 4.3-4.5 min in 2 of 8 runs. Warns - # (::warning) when the slowest shard this run is far above the - # median; does not fail the job yet (--fail-on-imbalance switches - # that on once the budget has run clean for a while). - - name: Shard balance - if: ${{ always() && steps.test.outcome != 'skipped' }} - run: python3 tools/bazel/shard_budget.py --bep "$RUNNER_TEMP/bazel-bep.json" - - - name: Upload test logs - if: ${{ failure() && steps.test.outcome != 'skipped' }} - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7 - with: - overwrite: true # a job re-run is the same run: replace, not 409 - name: bazel-server-storage-testlogs - path: | - bazel-testlogs/**/test.log - bazel-testlogs/**/test.xml - if-no-files-found: ignore - retention-days: 7 - - # Same redacted summary as bazel-test's; see its comment. - - name: CI analytics summary - id: ci-analytics - if: "!cancelled() && steps.test.outcome != 'skipped'" - continue-on-error: true - timeout-minutes: 3 - env: - LANE: server-storage - MODE: ${{ needs.rbe.outputs.mode }} - CHECK_RUN_ID: ${{ job.check_run_id }} - PR_HINT: ${{ github.event.pull_request.number || 0 }} - run: | - [ -f "$RUNNER_TEMP/bazel-bep.json" ] || exit 0 - zstd --version || echo "::warning::zstd missing: no per-mnemonic data in this lane's summary" - python3 tools/bazel/ci_analytics_extract.py \ - --lane "$LANE" --mode "$MODE" --check-run-id "$CHECK_RUN_ID" --pr-hint "$PR_HINT" \ - --invocation test "$RUNNER_TEMP/bazel-bep.json" "$RUNNER_TEMP/bazel-exec.log.zst" "$RUNNER_TEMP/bazel-profile.json" \ - --out "$RUNNER_TEMP/ci-analytics/ci-analytics.json" - - - name: Upload CI analytics summary - if: "!cancelled() && steps.ci-analytics.outcome == 'success'" - continue-on-error: true - timeout-minutes: 2 - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7 - with: - overwrite: true # a job re-run is the same run: replace, not 409 - name: ci-analytics-server-storage-${{ github.run_attempt }} - path: ${{ runner.temp }}/ci-analytics/ci-analytics.json - retention-days: 7 - if-no-files-found: ignore - - # Last step: the job's result as a workflow_call output for pr.yml's - # ci-gate. always() runs it after a failure, a timeout or a - # cancellation too. - - name: Record job result - id: result - if: ${{ always() }} - env: - JOB_STATUS: ${{ job.status }} - run: echo "result=$JOB_STATUS" >> "$GITHUB_OUTPUT" - - # The cmd/bd Dolt-server tier, as --config=doltserver-cmd: the whole - # integration-tagged cmd/bd suite (//cmd/bd:bd_dolt_server_test, 32 - # shards, the slowest tests pinned to 16 of them) against a hermetic dolt sql-server, i.e. the Dolt-gated cmd/bd - # tests (TestCLI_*, TestInit*, the store-backed suites) that every other - # lane skips with BEADS_TEST_SKIP=dolt or leaves out of its - # manifest; plus the differential regression suite - # (//tests/regression:regression_test, 14 shards: the candidate - # bd_for_tests against the catalog-pinned v0.49.6 release, formerly - # regression.yml). --config=doltserver-cmd has exactly - # --config=integration's build flags, so it shares bazel-integration's - # compile through the remote cache. Runs exactly where bazel-integration runs (the same if): - # remotely (modes remote, fork-ro, fork-rw), or locally with the - # read-only cache (mode cache: fork and Dependabot PRs while rbe-fork is - # closed), never plain local (rbe=off), and not when a caller passes - # integration: "off". - # Advisory: job-level continue-on-error keeps a failure out of this - # call's aggregate result (BAZEL), and pr.yml's ci-gate requires the - # lane's own result (BAZEL_CMD_DOLT) only while pr.yml's committed - # BAZEL_CMD_DOLT_REQUIRED flag is "true" (scripts/ci_workflow_test.go, - # TestBazelCmdDoltJob); pr.yml then passes cmd-dolt-required: true and - # this lane is the integration build's only bd_test run - # (TestBazelIntegrationExcludesBdTestOnlyWhereCmdDoltCovers). - bazel-cmd-dolt: - name: cmd/bd Dolt-server tier - # Not in mode remote: there bazel-dolt-int runs this tier together with - # the server-Dolt storage tier (item 5 A2). See bazel-server-storage's - # comment above. - needs: rbe - runs-on: ${{ needs.rbe.outputs.mode == 'remote' && 'blacksmith-4vcpu-ubuntu-2404' || 'ubuntu-24.04' }} - if: ${{ (needs.rbe.outputs.enabled == 'true' || needs.rbe.outputs.mode == 'cache') && inputs.integration != 'off' && needs.rbe.outputs.mode != 'remote' }} - timeout-minutes: 85 - continue-on-error: true - outputs: - result: ${{ steps.result.outputs.result }} - steps: - # inputs.checkout-sha: see the header (empty: the event's default ref). - # checkout v7 refuses a fork head on pull_request_target unless opted - # in; only bazel-farm.yml's authorized call opts in (false elsewhere). - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - with: - ref: ${{ inputs.checkout-sha }} - persist-credentials: false - allow-unsafe-pr-checkout: ${{ inputs.fork-farm == 'authorized' && github.event_name == 'pull_request_target' }} - - # The remote repo contents cache, read-only, as in bazel-test. - - name: Remote repo contents cache (read) - if: ${{ needs.rbe.outputs.mode == 'remote' && needs.rbe.outputs.rrc == 'on' }} - run: | - { - echo 'startup --experimental_remote_repo_contents_cache' - echo 'common --loading_phase_threads=64' - } >> .bazelrc.local - - - name: Set up Bazel - id: bazel - uses: ./.github/actions/setup-bazel - env: - BAZEL_REMOTE_EXECUTOR: ${{ needs.rbe.outputs.mode == 'remote' && secrets.RBE_WEST_EXECUTOR || '' }} - RBE_TLS_CERT: ${{ needs.rbe.outputs.mode == 'remote' && secrets.RBE_TLS_CERT || '' }} - RBE_TLS_KEY: ${{ needs.rbe.outputs.mode == 'remote' && secrets.RBE_TLS_KEY || '' }} - RBE_TLS_CA: ${{ needs.rbe.outputs.mode == 'remote' && secrets.RBE_TLS_CA || '' }} - RBE_INSTANCE: ${{ needs.rbe.outputs.mode == 'remote' && 'oss' || '' }} - BAZEL_FORK_CACHE: ${{ needs.rbe.outputs.mode == 'cache' && 'true' || '' }} - BAZEL_RRC_READ: 'true' # mode cache reads the repo contents cache through rbe-cache - BAZEL_FORK_REMOTE: ${{ startsWith(needs.rbe.outputs.mode, 'fork-') && 'true' || '' }} - RBE_FORK_TIER: ${{ needs.rbe.outputs.tier }} - RBE_FORK_PR: ${{ github.event.pull_request.number }} - - # Remote: a compile shared with bazel-integration plus the longest - # of 32 shards (~1 minute: tools/bazel/go_test_pinned_shard.sh pins - # the slowest tests to shards of their own; each shard pays a few - # seconds of TestMain and its own dolt sql-server; Go's - # -test.timeout=19m, Bazel's 1200s cap). Mode cache on a 4-CPU runner: the compile hits - # the read-only cache, and a PR that changes cmd/bd or what it imports - # runs every shard locally, four at a time (~2,200 test-seconds - # measured unloaded, slower four-up: ~20-30 minutes); with the cache - # closed, add the cold race build (~30). - - name: bazel test //... --config=doltserver-cmd - id: test - timeout-minutes: 75 - run: | - set -o pipefail - start=$(date +%s) - rc=0 - bazel test //... --config=doltserver-cmd ${BAZEL_FRESH:+"$BAZEL_FRESH"} \ - --build_event_json_file="$RUNNER_TEMP/bazel-bep.json" \ - --execution_log_compact_file="$RUNNER_TEMP/bazel-exec.log.zst" \ - --profile="$RUNNER_TEMP/bazel-profile.json" \ - --experimental_build_event_upload_strategy=local \ - 2>&1 | tee "$RUNNER_TEMP/bazel-test.log" || rc=$? - echo "bazel test --config=doltserver-cmd: exit $rc, $(( $(date +%s) - start ))s wall" | tee -a "$GITHUB_STEP_SUMMARY" - exit "$rc" - - # As in bazel-embedded: every shard of the target ran tests (the Go - # binary shards itself over every top-level test, so together the - # shards run each exactly once). - - name: Every target and shard ran tests - if: ${{ always() && steps.test.outcome != 'skipped' }} - run: python3 tools/bazel/check_testcases.py --bep "$RUNNER_TEMP/bazel-bep.json" - - - name: Upload test logs - if: ${{ failure() && steps.test.outcome != 'skipped' }} - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7 - with: - overwrite: true # a job re-run is the same run: replace, not 409 - name: bazel-cmd-dolt-testlogs - path: | - bazel-testlogs/**/test.log - bazel-testlogs/**/test.xml - if-no-files-found: ignore - retention-days: 7 - - # Same redacted summary as bazel-test's; see its comment. - - name: CI analytics summary - id: ci-analytics - if: "!cancelled() && steps.test.outcome != 'skipped'" - continue-on-error: true - timeout-minutes: 3 - env: - LANE: cmd-dolt - MODE: ${{ needs.rbe.outputs.mode }} - CHECK_RUN_ID: ${{ job.check_run_id }} - PR_HINT: ${{ github.event.pull_request.number || 0 }} - run: | - [ -f "$RUNNER_TEMP/bazel-bep.json" ] || exit 0 - zstd --version || echo "::warning::zstd missing: no per-mnemonic data in this lane's summary" - python3 tools/bazel/ci_analytics_extract.py \ - --lane "$LANE" --mode "$MODE" --check-run-id "$CHECK_RUN_ID" --pr-hint "$PR_HINT" \ - --invocation test "$RUNNER_TEMP/bazel-bep.json" "$RUNNER_TEMP/bazel-exec.log.zst" "$RUNNER_TEMP/bazel-profile.json" \ - --out "$RUNNER_TEMP/ci-analytics/ci-analytics.json" - - - name: Upload CI analytics summary - if: "!cancelled() && steps.ci-analytics.outcome == 'success'" - continue-on-error: true - timeout-minutes: 2 - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7 - with: - overwrite: true # a job re-run is the same run: replace, not 409 - name: ci-analytics-cmd-dolt-${{ github.run_attempt }} - path: ${{ runner.temp }}/ci-analytics/ci-analytics.json - retention-days: 7 - if-no-files-found: ignore - - # Last step: the job's result as a workflow_call output for pr.yml's - # ci-gate. always() runs it after a failure, a timeout or a - # cancellation too; job.status is the steps' status, unaffected by the - # job-level continue-on-error. - - name: Record job result - id: result - if: ${{ always() }} - env: - JOB_STATUS: ${{ job.status }} - run: echo "result=$JOB_STATUS" >> "$GITHUB_OUTPUT" - - # Item 5 A2 (scripts/bazel_dolt_int_test.go): the server-Dolt storage and - # cmd/bd Dolt-server tiers in one `bazel test --config=dolt-int` (.bazelrc: - # exactly the union of --config=doltserver-integration's and - # --config=doltserver-cmd's lines, one tag filter for both), mode remote - # only, in place of bazel-server-storage and bazel-cmd-dolt, which run in - # every other mode (there a single client does both tiers' work without - # in-flight deduplication, so a union would cost the sum of the two tiers - # rather than the max). One client waits on rbe-west for both tiers' shards - # at once (17 + 46 = 63 test actions, under the remote-exec config's 64 - # jobs), so the job ends with the slower tier and the other tier's runner - # is saved. cmd-dolt's Go-level -test.timeout=19m arg and its - # BEADS_TEST_REQUIRE_EXCLUDE_PERMISSION env moved from --config=doltserver-cmd - # onto //cmd/bd:bd_dolt_server_test's and //tests/regression:regression_test's - # own args/env (their comments), so the union's tag filter alone decides - # which targets see them: without the move, server-storage's per-shard 15m - # Go timeout would become 19m under the union. The final argv and - # environment either way are identical (aquery-verified for all 4 affected - # targets, item 5 A2 PR), so remote action digests -- what RBE's cache keys - # on -- are unchanged and cached results should carry over; this is a local - # actionKey change for cmd-dolt's 46 actions, not a remote one (--test_env - # is part of the build configuration's own fingerprint, which every target - # under that configuration picks up, unlike a target's own env attribute), - # confirmed in practice by the first merge group's tests_cached == - # tests_total in ci-analytics, not claimed here as already observed. - # - # Unlike dolt-race, the members have different gating categories: - # bazel-server-storage is plain-gated (BAZEL_SERVER_STORAGE), bazel-cmd-dolt - # is flag-gated and advisory (BAZEL_CMD_DOLT, continue-on-error) until - # pr.yml's BAZEL_CMD_DOLT_REQUIRED is "true". This job therefore carries no - # job-level continue-on-error: on push, nightly and dispatch nothing reads - # BAZEL_SERVER_STORAGE off a workflow_call output, so job-level - # continue-on-error (job.status reads success regardless of the test step's - # own result) would let a server-storage failure end such a run green, - # where it must stay red exactly as it does on main today. Instead only the - # `bazel test` step carries continue-on-error, and an always() step - # (tools/bazel/classify_tier_results.py) reads the BEP by target label, - # computes each tier's own result, and fails the job itself iff - # server-storage's tier failed (a cmd-dolt-only test failure leaves the job - # green, as bazel-cmd-dolt's own continue-on-error already does outside - # mode remote) or bazel's exit code was neither 0 nor 3 (a build or infra - # failure is not attributable to one tier, so both are marked failed and - # the job fails regardless). Both members' workflow_call outputs read this - # job's matching per-tier output first, then their own member's; a job that - # dies before the classify step leaves both outputs empty, which mode - # remote's gate (bazel-gate.sh, ci-gate.sh) already treats as skipped and - # fails, same as an empty bazel-server-storage/bazel-cmd-dolt result would. - # Every step has a timeout and the job's exceeds their sum by 5 minutes - # while staying under the merge queue's 60-minute check timeout - # (TestBazelDoltIntJobTimeouts): 58 leaves only about 2 minutes for runner - # pickup and the rbe job inside that 60-minute budget, so a hang that runs - # to this job's own timeout can drop the merge group; accepted, because - # such a run is red anyway and the 58-minute job timeout bounds only this - # job, not the whole run (as #7514's dolt-race job comment). The member - # jobs' configs and the union's must stay key-identical: the mode decides - # which runs (TestBazelDoltIntConfigIsMemberUnion). Its ci-analytics LANE - # is dolt-int. - bazel-dolt-int: - name: dolt-int - needs: rbe - runs-on: ${{ needs.rbe.outputs.mode == 'remote' && 'blacksmith-4vcpu-ubuntu-2404' || 'ubuntu-latest' }} - if: ${{ needs.rbe.outputs.mode == 'remote' && inputs.integration != 'off' }} - # At least every step's timeout-minutes plus 5, and below the merge - # queue's 60-minute check timeout so this job alone cannot exhaust it - # (TestBazelDoltIntJobTimeouts). 58 leaves only about 2 minutes for - # runner pickup and the rbe job inside the merge queue's 60-minute check; - # a hang that runs to this timeout can therefore drop the group, which is - # accepted because such a run is red anyway and the 58-minute job timeout - # bounds this job, not the whole run (as #7514's dolt-race job comment). - timeout-minutes: 58 - outputs: - server_storage_result: ${{ steps.classify.outputs.server_storage_result }} - cmd_dolt_result: ${{ steps.classify.outputs.cmd_dolt_result }} - steps: - # inputs.checkout-sha: see the header (empty: the event's default ref). - # checkout v7 refuses a fork head on pull_request_target unless opted - # in; only bazel-farm.yml's authorized call opts in (false elsewhere). - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - timeout-minutes: 5 - with: - ref: ${{ inputs.checkout-sha }} - persist-credentials: false - allow-unsafe-pr-checkout: ${{ inputs.fork-farm == 'authorized' && github.event_name == 'pull_request_target' }} - - # The remote repo contents cache, read-only, as in bazel-test. - - name: Remote repo contents cache (read) - if: ${{ needs.rbe.outputs.mode == 'remote' && needs.rbe.outputs.rrc == 'on' }} - timeout-minutes: 1 - run: | - { - echo 'startup --experimental_remote_repo_contents_cache' - echo 'common --loading_phase_threads=64' - } >> .bazelrc.local - - - name: Set up Bazel - id: bazel - timeout-minutes: 5 - uses: ./.github/actions/setup-bazel - env: - BAZEL_REMOTE_EXECUTOR: ${{ needs.rbe.outputs.mode == 'remote' && secrets.RBE_WEST_EXECUTOR || '' }} - RBE_TLS_CERT: ${{ needs.rbe.outputs.mode == 'remote' && secrets.RBE_TLS_CERT || '' }} - RBE_TLS_KEY: ${{ needs.rbe.outputs.mode == 'remote' && secrets.RBE_TLS_KEY || '' }} - RBE_TLS_CA: ${{ needs.rbe.outputs.mode == 'remote' && secrets.RBE_TLS_CA || '' }} - RBE_INSTANCE: ${{ needs.rbe.outputs.mode == 'remote' && 'oss' || '' }} - BAZEL_FORK_CACHE: ${{ needs.rbe.outputs.mode == 'cache' && 'true' || '' }} - BAZEL_RRC_READ: 'true' # mode cache reads the repo contents cache through rbe-cache - BAZEL_FORK_REMOTE: ${{ startsWith(needs.rbe.outputs.mode, 'fork-') && 'true' || '' }} - RBE_FORK_TIER: ${{ needs.rbe.outputs.tier }} - RBE_FORK_PR: ${{ github.event.pull_request.number }} - - # As in bazel-server-storage and bazel-proxied. - - name: No test target is marked flaky - timeout-minutes: 3 - run: | - flaky="$(bazel query 'attr(flaky, 1, tests(//...))')" - if [ -n "$flaky" ]; then - echo "::error::flaky = True on ${flaky//$'\n'/ }: the gated lanes run each test once" - exit 1 - fi - - # Both tiers' shards at once: 17 server-storage + 46 cmd-dolt test - # actions, under the remote-exec config's 64 jobs. No --config=sole-run: - # its one line (no retry on a remote cache eviction) is already in the - # union. The step timeout is >= the union's eternal-size test_timeout - # cap (1200s, shared by bd_dolt_server_test and regression_test) + about - # 3 min so Bazel writes TIMEOUT logs before GitHub kills the step, plus - # the margin server-storage's own step budgets for a cold compile (its - # own step was 25). continue-on-error: a cmd-dolt-only test failure - # must not fail this step (and so the job) outright; the classify step - # below decides that per-tier, from this step's own exit code (written - # to its "rc" output) and the BEP. - - name: bazel test //... --config=dolt-int - id: test - timeout-minutes: 25 - continue-on-error: true - run: | - set -o pipefail - start=$(date +%s) - rc=0 - bazel test //... --config=dolt-int ${BAZEL_FRESH:+"$BAZEL_FRESH"} \ - --build_event_json_file="$RUNNER_TEMP/bazel-bep.json" \ - --execution_log_compact_file="$RUNNER_TEMP/bazel-exec.log.zst" \ - --profile="$RUNNER_TEMP/bazel-profile.json" \ - --experimental_build_event_upload_strategy=local \ - 2>&1 | tee "$RUNNER_TEMP/bazel-test.log" || rc=$? - echo "bazel test --config=dolt-int: exit $rc, $(( $(date +%s) - start ))s wall" | tee -a "$GITHUB_STEP_SUMMARY" - echo "rc=$rc" >> "$GITHUB_OUTPUT" - exit "$rc" - - # check_testcases split per tier (should-fix 2, PR #7535 review): a - # single shared step with no continue-on-error would fail this job - # outright on a cmd-dolt-only zero-testcase shard (e.g. TestMain dying - # on dolt server start under BEADS_TEST_REQUIRE_DOLT_CONTAINER=1), - # where main (bazel-cmd-dolt alone) stays green, and the divergence - # would only show up under the documented rollback to - # BAZEL_CMD_DOLT_REQUIRED=false. Each step is --label-scoped to its own - # tier (so a --label typo or a label the run did not test fails - # closed, not open: check_testcases.py's own --label section) and - # carries continue-on-error so neither can fail the job by itself; the - # classify step below folds each step's own outcome into that tier's - # result with --check, so a cmd-dolt failure here reaches - # cmd_dolt_result only, same as a cmd-dolt BEP test failure does. - - name: Every target and shard ran tests (server-storage) - id: check-ss - if: ${{ always() && steps.test.outcome != 'skipped' }} - continue-on-error: true - timeout-minutes: 1 - run: >- - python3 tools/bazel/check_testcases.py - --bep "$RUNNER_TEMP/bazel-bep.json" - --label //internal/storage/dolt:dolt_server_full_test - --label //internal/storage/dolt:dolt_server_conformance_test - - - name: Every target and shard ran tests (cmd-dolt) - id: check-cd - if: ${{ always() && steps.test.outcome != 'skipped' }} - continue-on-error: true - timeout-minutes: 1 - run: >- - python3 tools/bazel/check_testcases.py - --bep "$RUNNER_TEMP/bazel-bep.json" - --label //cmd/bd:bd_dolt_server_test - --label //tests/regression:regression_test - - # bazel-server-storage's own check (cmd-dolt has none): every - # full-suite shard ran exactly the tests its shard script lists, and - # neither it nor the conformance target only skips. - - name: Every listed test ran in its shard - if: ${{ always() && steps.test.outcome != 'skipped' }} - timeout-minutes: 2 - run: >- - python3 tools/bazel/check_shard_coverage.py - --bep "$RUNNER_TEMP/bazel-bep.json" - --suite //internal/storage/dolt:dolt_server_full_test .github/scripts/server-storage-test-shard.sh 16 - --whole //internal/storage/dolt:dolt_server_conformance_test - - # bazel-server-storage's own check, scoped with --label to the one - # target it has always budgeted: cmd-dolt's bd_dolt_server_test and - # regression_test shards are sharded too but no job budgeted them - # before the union, and this script has never judged them (see its - # docstring). - - name: Shard balance - if: ${{ always() && steps.test.outcome != 'skipped' }} - timeout-minutes: 2 - run: >- - python3 tools/bazel/shard_budget.py - --bep "$RUNNER_TEMP/bazel-bep.json" - --label //internal/storage/dolt:dolt_server_full_test - - # failure() alone misses an ordinary test failure here: the `bazel - # test` step carries its own continue-on-error (must-fix 2), so a - # failing test does not make failure() true until the classify step - # (which runs after this one) exits non-zero. steps.test.outcome == - # 'failure' catches that case directly (nit, PR #7535 review). - - name: Upload test logs - if: ${{ (failure() || steps.test.outcome == 'failure') && steps.test.outcome != 'skipped' }} - timeout-minutes: 3 - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7 - with: - overwrite: true # a job re-run is the same run: replace, not 409 - name: bazel-dolt-int-testlogs - path: | - bazel-testlogs/**/test.log - bazel-testlogs/**/test.xml - if-no-files-found: ignore - retention-days: 7 - - # Same redacted summary as bazel-test's; see its comment. - - name: CI analytics summary - id: ci-analytics - if: "!cancelled() && steps.test.outcome != 'skipped'" - continue-on-error: true - timeout-minutes: 2 - env: - LANE: dolt-int - MODE: ${{ needs.rbe.outputs.mode }} - CHECK_RUN_ID: ${{ job.check_run_id }} - PR_HINT: ${{ github.event.pull_request.number || 0 }} - run: | - [ -f "$RUNNER_TEMP/bazel-bep.json" ] || exit 0 - zstd --version || echo "::warning::zstd missing: no per-mnemonic data in this lane's summary" - python3 tools/bazel/ci_analytics_extract.py \ - --lane "$LANE" --mode "$MODE" --check-run-id "$CHECK_RUN_ID" --pr-hint "$PR_HINT" \ - --invocation test "$RUNNER_TEMP/bazel-bep.json" "$RUNNER_TEMP/bazel-exec.log.zst" "$RUNNER_TEMP/bazel-profile.json" \ - --out "$RUNNER_TEMP/ci-analytics/ci-analytics.json" - - - name: Upload CI analytics summary - if: "!cancelled() && steps.ci-analytics.outcome == 'success'" - continue-on-error: true - timeout-minutes: 2 - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7 - with: - overwrite: true # a job re-run is the same run: replace, not 409 - name: ci-analytics-dolt-int-${{ github.run_attempt }} - path: ${{ runner.temp }}/ci-analytics/ci-analytics.json - retention-days: 7 - if-no-files-found: ignore - - # Last step: each tier's own result (not job.status: this job carries - # no continue-on-error, see the job's own comment) as the - # bazel-server-storage and bazel-cmd-dolt workflow_call outputs for - # pr.yml's ci-gate. always() runs it after a failure, a timeout or a - # cancellation too (steps.test.outcome stays "skipped" then, so the - # script's --bep file does not exist; it is only read when the test - # step actually ran). This step's own exit code decides job.status: it - # fails the job iff server-storage's tier failed, or `bazel test`'s - # exit code was neither 0 nor 3 (a build or infra failure, not - # attributable to one tier, fails both and the job). - - name: Classify tier results - id: classify - if: ${{ always() && steps.test.outcome != 'skipped' }} - timeout-minutes: 1 - env: - TEST_RC: ${{ steps.test.outputs.rc }} - CHECK_SS_OUTCOME: ${{ steps.check-ss.outcome }} - CHECK_CD_OUTCOME: ${{ steps.check-cd.outcome }} - run: >- - python3 tools/bazel/classify_tier_results.py - --bep "$RUNNER_TEMP/bazel-bep.json" - --rc "$TEST_RC" - --tier server-storage=//internal/storage/dolt:dolt_server_full_test,//internal/storage/dolt:dolt_server_conformance_test - --tier cmd-dolt=//cmd/bd:bd_dolt_server_test,//tests/regression:regression_test - --gate-tier server-storage - --check "server-storage=$CHECK_SS_OUTCOME" - --check "cmd-dolt=$CHECK_CD_OUTCOME" - --github-output "$GITHUB_OUTPUT" - - # F3: moved here from pr.yml so the gate depends on nothing but the rbe - # job's decision (not the whole bazel call). Only pr.yml opts in (package-gates: "on"); - # bazel-farm.yml and nightly.yml keep the default "off" and this job does - # not run for them. Same-repo PRs get a Bazel-built bd (no --config=ci: - # only test:ci is defined in .bazelrc, so bazel build //cmd/bd:bd_for_tests - # is run directly); forks, Dependabot (whatever rbe-fork says: these gates - # take no fork certificate) and rbe-off/skip/cache runs fall back to - # `go build`, same as package-mcp.sh's own prepare_bd_binary() does when - # BEADS_TEST_BD_BINARY is unset. Hence mode == 'remote' below, not enabled - # (the fork modes are enabled too). - package-mcp: - name: Package Gate (MCP) - needs: rbe - # 8 vCPU with 16 xdist workers (BEADS_MCP_PYTEST_WORKERS below): pytest - # was 70s of this job at -n 8 on 4 vCPU (merge group 37721729916), - # dominated by per-test `bd init` fixture setup, which is CPU-bound. - # Was 4 vCPU / -n 8 (tools/f3 timing, 2026-10: ~2.7-3x on 4 CPUs); - # revert to that if the A/B does not save at least 15s. - runs-on: ${{ needs.rbe.outputs.mode == 'remote' && 'blacksmith-8vcpu-ubuntu-2404' || 'ubuntu-latest' }} - if: ${{ inputs.package-gates == 'on' }} - timeout-minutes: 30 - outputs: - result: ${{ steps.result.outputs.result }} - steps: - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - with: - ref: ${{ inputs.checkout-sha }} - fetch-depth: 0 - persist-credentials: false - allow-unsafe-pr-checkout: ${{ inputs.fork-farm == 'authorized' && github.event_name == 'pull_request_target' }} - - - name: Decide applicability - id: detect - env: - PR_BASE_SHA: ${{ github.event.pull_request.base.sha || github.event.merge_group.base_sha }} - PR_HEAD_SHA: ${{ github.event.pull_request.head.sha || github.event.merge_group.head_sha }} - PUSH_BEFORE_SHA: ${{ github.event.before }} - PUSH_AFTER_SHA: ${{ github.sha }} - run: ./scripts/ci/detect-package-gates.sh - - - name: Set up Bazel - id: bazel - if: ${{ steps.detect.outputs.mcp_package == 'true' && needs.rbe.outputs.mode == 'remote' }} - uses: ./.github/actions/setup-bazel - env: - BAZEL_REMOTE_EXECUTOR: ${{ needs.rbe.outputs.mode == 'remote' && secrets.RBE_WEST_EXECUTOR || '' }} - RBE_TLS_CERT: ${{ needs.rbe.outputs.mode == 'remote' && secrets.RBE_TLS_CERT || '' }} - RBE_TLS_KEY: ${{ needs.rbe.outputs.mode == 'remote' && secrets.RBE_TLS_KEY || '' }} - RBE_TLS_CA: ${{ needs.rbe.outputs.mode == 'remote' && secrets.RBE_TLS_CA || '' }} - RBE_INSTANCE: ${{ needs.rbe.outputs.mode == 'remote' && 'oss' || '' }} - BAZEL_FORK_CACHE: ${{ needs.rbe.outputs.mode == 'cache' && 'true' || '' }} - - # No --config=ci: only test:ci is defined in .bazelrc, and `bazel - # build --config=foo` fails when only test:foo exists. The explicit - # --@rules_go//go/config:race matches test:ci's top-level build - # setting (test:ci -> test:prcore), so bd_for_tests' race="off" - # transition lands in the same k8-fastbuild-ST-... output directory - # and action-cache key `bazel test --config=ci` already populated, - # instead of plain k8-fastbuild's; race itself still builds a non-race - # bd (verified with cquery: both configs cquery to the identical - # bazel-out/k8-fastbuild-ST-.../bin/cmd/bd/bd_for_tests/bd path). - - name: bazel build //cmd/bd:bd_for_tests - if: ${{ steps.detect.outputs.mcp_package == 'true' && needs.rbe.outputs.mode == 'remote' }} - run: bazel build --@rules_go//go/config:race //cmd/bd:bd_for_tests --remote_download_regex='.*/bin/cmd/bd/bd_for_tests/bd$' --norun_validations - - - name: Package the Bazel-built bd - if: ${{ steps.detect.outputs.mcp_package == 'true' && needs.rbe.outputs.mode == 'remote' }} - run: | - ./scripts/ci/package-bazel-bd.sh "$RUNNER_TEMP/bd-artifacts" - echo "BEADS_TEST_BD_BINARY=$RUNNER_TEMP/bd-artifacts/bd-linux-gms-pure" >> "$GITHUB_ENV" - # Defense in depth: nothing after this step needs the Bazel server - # or its RBE client key (uv sync and pytest run PR-controlled - # code). Shutting down also frees the JVM's memory for the 16 - # xdist workers that follow on this 8 vCPU runner. - bazel shutdown - rm -rf "$RUNNER_TEMP/bazel-ci-secret" - - - name: Set up Go - id: setup-go - if: ${{ steps.detect.outputs.mcp_package == 'true' && needs.rbe.outputs.mode != 'remote' }} - uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 - with: - go-version-file: 'go.mod' - cache: false - - - name: Restore Go module cache - if: ${{ steps.detect.outputs.mcp_package == 'true' && needs.rbe.outputs.mode != 'remote' }} - uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 - with: - path: ~/go/pkg/mod - key: beads-go-mod-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-${{ hashFiles('go.mod', 'go.sum') }} - restore-keys: beads-go-mod-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}- - - - name: Verify Go modules - if: ${{ steps.detect.outputs.mcp_package == 'true' && needs.rbe.outputs.mode != 'remote' }} - run: go mod verify - - - name: Restore non-race Go build cache - if: ${{ steps.detect.outputs.mcp_package == 'true' && needs.rbe.outputs.mode != 'remote' }} - uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 - with: - path: ${{ runner.temp }}/go-cache/non-race - key: beads-go-build-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-base-gms_pure_go-non-race-${{ github.sha }} - restore-keys: beads-go-build-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-base-gms_pure_go-non-race- - - - name: Set up Python - if: ${{ steps.detect.outputs.mcp_package == 'true' }} - uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0 - with: - python-version: '3.14' - - - name: Install uv - if: ${{ steps.detect.outputs.mcp_package == 'true' }} - run: python -m pip install uv==0.11.16 - - - name: Run MCP package gate - if: ${{ steps.detect.outputs.mcp_package == 'true' }} - env: - GOCACHE: ${{ runner.temp }}/go-cache/non-race - # 16 workers on the 8 vCPU Blacksmith runner; package-mcp.sh's - # default 8 on the GitHub-hosted fallback. - BEADS_MCP_PYTEST_WORKERS: ${{ needs.rbe.outputs.mode == 'remote' && '16' || '8' }} - run: make ci-package-mcp - - # Last step: the job's result as a workflow_call output for pr.yml's - # ci-gate. always() runs it after a failure, a timeout or a - # cancellation too; package-gates == 'on' is the only skip condition - # (the job itself), so PACKAGE_MCP is never "skipped" on pr.yml. - - name: Record job result - id: result - if: ${{ always() }} - env: - JOB_STATUS: ${{ job.status }} - run: echo "result=$JOB_STATUS" >> "$GITHUB_OUTPUT" - - # F3: npm's twin of package-mcp above. Structurally identical apart from - # the language setup (Node instead of Python/uv) and the `make` target. - package-npm: - name: Package Gate (npm) - needs: rbe - runs-on: ${{ needs.rbe.outputs.mode == 'remote' && 'blacksmith-4vcpu-ubuntu-2404' || 'ubuntu-latest' }} - if: ${{ inputs.package-gates == 'on' }} - timeout-minutes: 30 - outputs: - result: ${{ steps.result.outputs.result }} - steps: - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - with: - ref: ${{ inputs.checkout-sha }} - fetch-depth: 0 - persist-credentials: false - allow-unsafe-pr-checkout: ${{ inputs.fork-farm == 'authorized' && github.event_name == 'pull_request_target' }} - - - name: Decide applicability - id: detect - env: - PR_BASE_SHA: ${{ github.event.pull_request.base.sha || github.event.merge_group.base_sha }} - PR_HEAD_SHA: ${{ github.event.pull_request.head.sha || github.event.merge_group.head_sha }} - PUSH_BEFORE_SHA: ${{ github.event.before }} - PUSH_AFTER_SHA: ${{ github.sha }} - run: ./scripts/ci/detect-package-gates.sh - - - name: Set up Bazel - id: bazel - if: ${{ steps.detect.outputs.npm_package == 'true' && needs.rbe.outputs.mode == 'remote' }} - uses: ./.github/actions/setup-bazel - env: - BAZEL_REMOTE_EXECUTOR: ${{ needs.rbe.outputs.mode == 'remote' && secrets.RBE_WEST_EXECUTOR || '' }} - RBE_TLS_CERT: ${{ needs.rbe.outputs.mode == 'remote' && secrets.RBE_TLS_CERT || '' }} - RBE_TLS_KEY: ${{ needs.rbe.outputs.mode == 'remote' && secrets.RBE_TLS_KEY || '' }} - RBE_TLS_CA: ${{ needs.rbe.outputs.mode == 'remote' && secrets.RBE_TLS_CA || '' }} - RBE_INSTANCE: ${{ needs.rbe.outputs.mode == 'remote' && 'oss' || '' }} - BAZEL_FORK_CACHE: ${{ needs.rbe.outputs.mode == 'cache' && 'true' || '' }} - - # No --config=ci: only test:ci is defined in .bazelrc, and `bazel - # build --config=foo` fails when only test:foo exists. The explicit - # --@rules_go//go/config:race matches test:ci's top-level build - # setting (test:ci -> test:prcore), so bd_for_tests' race="off" - # transition lands in the same k8-fastbuild-ST-... output directory - # and action-cache key `bazel test --config=ci` already populated, - # instead of plain k8-fastbuild's; race itself still builds a non-race - # bd (verified with cquery: both configs cquery to the identical - # bazel-out/k8-fastbuild-ST-.../bin/cmd/bd/bd_for_tests/bd path). - - name: bazel build //cmd/bd:bd_for_tests - if: ${{ steps.detect.outputs.npm_package == 'true' && needs.rbe.outputs.mode == 'remote' }} - run: bazel build --@rules_go//go/config:race //cmd/bd:bd_for_tests --remote_download_regex='.*/bin/cmd/bd/bd_for_tests/bd$' --norun_validations - - - name: Package the Bazel-built bd - if: ${{ steps.detect.outputs.npm_package == 'true' && needs.rbe.outputs.mode == 'remote' }} - run: | - ./scripts/ci/package-bazel-bd.sh "$RUNNER_TEMP/bd-artifacts" - echo "BEADS_TEST_BD_BINARY=$RUNNER_TEMP/bd-artifacts/bd-linux-gms-pure" >> "$GITHUB_ENV" - # Defense in depth: nothing after this step needs the Bazel server - # or its RBE client key (npm install runs PR-controlled code). - bazel shutdown - rm -rf "$RUNNER_TEMP/bazel-ci-secret" - - - name: Set up Go - id: setup-go - if: ${{ steps.detect.outputs.npm_package == 'true' && needs.rbe.outputs.mode != 'remote' }} - uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 - with: - go-version-file: 'go.mod' - cache: false - - - name: Restore Go module cache - if: ${{ steps.detect.outputs.npm_package == 'true' && needs.rbe.outputs.mode != 'remote' }} - uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 - with: - path: ~/go/pkg/mod - key: beads-go-mod-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-${{ hashFiles('go.mod', 'go.sum') }} - restore-keys: beads-go-mod-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}- - - - name: Verify Go modules - if: ${{ steps.detect.outputs.npm_package == 'true' && needs.rbe.outputs.mode != 'remote' }} - run: go mod verify - - - name: Restore non-race Go build cache - if: ${{ steps.detect.outputs.npm_package == 'true' && needs.rbe.outputs.mode != 'remote' }} - uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 - with: - path: ${{ runner.temp }}/go-cache/non-race - key: beads-go-build-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-base-gms_pure_go-non-race-${{ github.sha }} - restore-keys: beads-go-build-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-base-gms_pure_go-non-race- - - - name: Set up Node.js - if: ${{ steps.detect.outputs.npm_package == 'true' }} - uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v6 - with: - node-version: '24' - - - name: Run npm package gate - if: ${{ steps.detect.outputs.npm_package == 'true' }} - env: - GOCACHE: ${{ runner.temp }}/go-cache/non-race - run: make ci-package-npm - - # Last step: the job's result as a workflow_call output for pr.yml's - # ci-gate. always() runs it after a failure, a timeout or a - # cancellation too; package-gates == 'on' is the only skip condition - # (the job itself), so PACKAGE_NPM is never "skipped" on pr.yml. - - name: Record job result - id: result - if: ${{ always() }} - env: - JOB_STATUS: ${{ job.status }} - run: echo "result=$JOB_STATUS" >> "$GITHUB_OUTPUT" - - # The remote repo contents cache's only writer (ga-vnycm2.29, ported from - # gascity's bazel.yml). On a push to main, with the rbe job's rrc not off, - # run every lane's loading and analysis (.github/scripts/rrc-lane-commands.txt, - # --nobuild): only main's repository rules and Starlark run (gazelle's - # fetch_repo, the Go SDK and LLVM downloads, extraction: what every lane - # already runs on its runner), never a build or test action. Bazel uploads - # each reproducible repository's tree (--remote_upload_local_results) with - # a 30-minute rbe-rrc-writer-beads certificate that rbe-west's mint signs - # only for this workflow on a push to main - # (.github/scripts/rrc-writer-credential.sh); rbe-west's rrc-gate admits - # repo-contents entries from it and refuses every other action cache - # write, and the certificate cannot Execute. Cache only (--remote_cache, - # no executor). Not a lane: no result output, never in pr.yml's gate - # (which never sees it anyway: push only), so a failed seed only leaves the - # lanes fetching. setup-bazel runs in local mode (no secrets): its rc, the - # runner cache and the pinned Bazel, nothing else. The only bazel.yml job - # with id-token: write; every caller must grant it (GitHub checks a called - # workflow's job permissions at startup, even for a job that skips), and - # the mint refuses any token but a main-push bazel.yml one. - rrc-seed: - name: repo contents cache seed - needs: rbe - if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' && needs.rbe.outputs.mode == 'remote' && needs.rbe.outputs.rrc != 'off' }} - runs-on: blacksmith-8vcpu-ubuntu-2404 - timeout-minutes: 30 - permissions: - contents: read - id-token: write # the rbe-rrc-writer-beads certificate (GitHub OIDC) - steps: - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - with: - persist-credentials: false - - name: Set up Bazel - uses: ./.github/actions/setup-bazel - - name: rbe-rrc-writer certificate - id: writer - env: - BAZEL_CI_SECRET_DIR: ${{ runner.temp }}/bazel-ci-secret - run: bash .github/scripts/rrc-writer-credential.sh - - name: Seed the remote repo contents cache - if: ${{ steps.writer.outputs.cert != '' }} - env: - CERT: ${{ steps.writer.outputs.cert }} - KEY: ${{ steps.writer.outputs.key }} - ENDPOINT: ${{ steps.writer.outputs.endpoint }} - INSTANCE: ${{ steps.writer.outputs.instance }} - run: >- - bash .github/scripts/rrc-run-lanes.sh --experimental_remote_repo_contents_cache -- - --loading_phase_threads=64 - "--remote_cache=$ENDPOINT" "--remote_instance_name=$INSTANCE" - "--tls_client_certificate=$CERT" "--tls_client_key=$KEY" - --remote_upload_local_results - - name: Remove the writer key - if: ${{ always() }} - run: rm -f -- "$RUNNER_TEMP/bazel-ci-secret/rrc-writer.key" "$RUNNER_TEMP/bazel-ci-secret/rrc-writer.crt" - - # Nightly check of the remote repo contents cache (gascity design R1/R2). A - # cached repository tree is trusted by every reader, and nothing on the - # reader checks it against what its rule produces. So, from nightly.yml (or - # a dispatch), fetch every lane's repositories cold (rrc-lane-commands.txt - # with --nobuild and no repo contents cache, Bazel set up exactly as a - # lane) and compare each repository's tree and marker with the cached - # entry a lane would read for the same key (tools/bazel/rrc_verify.py, - # reads only, with the lanes' certificate and the endpoint from - # setup-bazel's rc: no secret is referenced outside setup-bazel). A - # mismatch is a poisoned entry or a rule falsely marked reproducible: the - # job fails and opens (or comments on) an rrc-verify issue. Not a lane: - # never gated. The only bazel.yml job with issues: write; every caller - # must grant it, as for rrc-seed's id-token. - rrc-verify: - name: repo contents cache verify - needs: rbe - if: ${{ (github.event_name == 'schedule' || github.event_name == 'workflow_dispatch') && needs.rbe.outputs.mode == 'remote' && needs.rbe.outputs.rrc != 'off' }} - runs-on: blacksmith-8vcpu-ubuntu-2404 - timeout-minutes: 45 - permissions: - contents: read - issues: write # the rrc-verify alert issue - steps: - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - with: - persist-credentials: false - - name: Set up Bazel - uses: ./.github/actions/setup-bazel - env: - BAZEL_REMOTE_EXECUTOR: ${{ needs.rbe.outputs.mode == 'remote' && secrets.RBE_WEST_EXECUTOR || '' }} - RBE_TLS_CERT: ${{ needs.rbe.outputs.mode == 'remote' && secrets.RBE_TLS_CERT || '' }} - RBE_TLS_KEY: ${{ needs.rbe.outputs.mode == 'remote' && secrets.RBE_TLS_KEY || '' }} - RBE_TLS_CA: ${{ needs.rbe.outputs.mode == 'remote' && secrets.RBE_TLS_CA || '' }} - RBE_INSTANCE: ${{ needs.rbe.outputs.mode == 'remote' && 'oss' || '' }} - - name: Cold fetch (every lane, no repo contents cache) - run: bash .github/scripts/rrc-run-lanes.sh -- --loading_phase_threads=64 - - name: Compare with the cache - id: verify - run: | - set -euo pipefail - secret_dir="$RUNNER_TEMP/bazel-ci-secret" - prefix='build:remote-exec --remote_executor=' - endpoint=$(grep -F -m1 "$prefix" "$secret_dir/ci.bazelrc" || true) - endpoint=${endpoint#"$prefix"} - [ -n "$endpoint" ] || { echo "::error::setup-bazel's rc names no executor"; exit 1; } - python3 tools/bazel/rrc_verify.py \ - --output-base "$(bazel info output_base)" \ - --endpoint "$endpoint" --instance oss \ - --cert "$secret_dir/client.crt" --key "$secret_dir/client.key" \ - --bazel-version "$(tr -d '[:space:]' < .bazelversion)" \ - --json "$RUNNER_TEMP/rrc-verify.json" --summary "$GITHUB_STEP_SUMMARY" - - name: Upload the report - if: ${{ always() && hashFiles(format('{0}/rrc-verify.json', runner.temp)) != '' }} - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7 - with: - overwrite: true # a job re-run is the same run: replace, not 409 - name: rrc-verify-${{ github.run_attempt }} - path: ${{ runner.temp }}/rrc-verify.json - retention-days: 30 - - name: Alert (rrc-verify issue) - if: ${{ failure() && steps.verify.outcome == 'failure' }} - env: - GH_TOKEN: ${{ github.token }} - RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} - run: | - set -euo pipefail - title="rrc-verify: remote repo contents cache differs from a cold fetch" - body="The nightly rrc-verify run found cached repository trees that differ from a cold fetch, or could not check them: $RUN_URL (summary and the rrc-verify report artifact). - - A mismatch is a poisoned entry or a repository rule falsely marked reproducible (gascity engdocs/design/bazel-remote-repo-contents-cache.md, R1/R2). Contain the fork-cache readers first: they see no repository variables, so (1) commit fork_rrc_read=off in both probe scripts, beads .github/actions/setup-bazel/cache-rrc-probe.sh and gascity tools/rbe/cache-rrc-probe.sh (beads has no fresh-merge: an open fork PR keeps the old switch until it is pushed again); (2) for immediate server-side containment, an operator runs rbe-cache-gate close on core, which stops all fork-cache, and fails fork-cache lanes already reading; (3) on a mismatch, set RBE_REPO_CONTENTS_CACHE to seed: the trusted readers read the same entries (rbe-cache serves the oss action cache), and seed stops every one of them while rrc-seed and this check keep running; set it to off only to stop seeding too, which also stops rrc-verify; if this run only says it could not check, leave the variable as it is. Then follow gascity/infra nativelink-cas/west/README.md (remote repo contents cache writer, poisoned entries)." - gh label create rrc-verify -R "$GITHUB_REPOSITORY" --color B60205 \ - --description "Remote repo contents cache verification failures" 2>/dev/null || true - n=$(gh issue list -R "$GITHUB_REPOSITORY" --label rrc-verify --state open --json number --jq '.[0].number // empty') - if [ -n "$n" ]; then - gh issue comment "$n" -R "$GITHUB_REPOSITORY" --body "Again: $RUN_URL" - else - gh issue create -R "$GITHUB_REPOSITORY" --label rrc-verify --title "$title" --body "$body" - fi diff --git a/.github/workflows/ci-measurements.yml b/.github/workflows/ci-measurements.yml new file mode 100644 index 000000000..bf01b789f --- /dev/null +++ b/.github/workflows/ci-measurements.yml @@ -0,0 +1,766 @@ +name: CI Measurements + +on: + workflow_call: + inputs: + suite: + description: "Suite to measure" + required: true + type: string + smoke_version: + description: "Optional previous release for cross-version-smoke" + required: false + type: string + default: "" + workflow_dispatch: + inputs: + suite: + description: "Suite to measure" + required: true + type: choice + default: pr-linux + options: + - pr-linux + - macos-short + - macos-candidates + - linux-integration + - linux-integration-sharded + - linux-integration-hybrid-sharded + - linux-integration-hybrid-16-sharded + - linux-integration-hybrid-prebuilt-sharded + - linux-integration-coverage + - cross-version-smoke + - nix + - mcp-package + - npm-package + smoke_version: + description: "Optional previous release for cross-version-smoke" + required: false + default: "" + +permissions: + contents: read + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }}-${{ inputs.suite }} + cancel-in-progress: false + +env: + BD_DISABLE_METRICS: "1" + BD_DISABLE_EVENT_FLUSH: "1" + +jobs: + pr-linux: + name: Measure PR Linux commands + if: ${{ inputs.suite == 'pr-linux' }} + runs-on: ubuntu-latest + timeout-minutes: 30 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + with: + fetch-depth: 0 + + - name: Set up Go + uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 + with: + go-version-file: 'go.mod' + + - name: Install Dolt + run: ./scripts/ci/install-dolt.sh + + - name: Configure Git and Dolt identity + run: | + git config --global user.name "CI Bot" + git config --global user.email "ci@beads.test" + dolt config --global --add user.name "CI Bot" + dolt config --global --add user.email "ci@beads.test" + + - name: Measure commands + run: | + set -euo pipefail + mkdir -p artifacts + source scripts/ci/lib/timing.sh + source ./.buildflags + + ci_time "install gotestsum" -- go install gotest.tools/gotestsum@v1.13.0 + ci_time "install golangci-lint" -- go install github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.10.1 + status=0 + ci_time "pr-policy wrapper" -- make ci-pr-policy || status=$? + # -timeout=30m keeps this in step with the lane it measures + # (scripts/ci/pr-core.sh). Without it ./cmd/bd can die on `go test`'s + # 10m default and the measurement reports a panic instead of a duration. + ci_time "pr-core gotestsum" -- gotestsum --junitfile artifacts/pr-core-junit.xml --format testdox -- -race -short -timeout=30m -skip '^TestEmbedded' ./... || status=$? + ci_time "pr-lint wrapper" -- make ci-pr-lint || status=$? + exit "$status" + + - name: Upload measurement artifacts + if: always() + uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7 + with: + name: ci-measure-pr-linux + path: artifacts/ + retention-days: 7 + if-no-files-found: ignore + + macos-short: + name: Measure macOS short Go tests + if: ${{ inputs.suite == 'macos-short' }} + runs-on: macos-latest + timeout-minutes: 30 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + + - name: Set up Go + uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 + with: + go-version-file: 'go.mod' + + - name: Install Dolt + run: ./scripts/ci/install-dolt.sh + + - name: Configure Git and Dolt identity + run: | + git config --global user.name "CI Bot" + git config --global user.email "ci@beads.test" + dolt config --global --add user.name "CI Bot" + dolt config --global --add user.email "ci@beads.test" + + - name: Measure commands + run: | + set -euo pipefail + source scripts/ci/lib/timing.sh + source ./.buildflags + + # -timeout=30m matches the macOS lanes this measures (pr.yml, main.yml). + # This is the job that produced the ./cmd/bd duration distribution, and + # at 10m it clips its own longest samples into timeout panics. + ci_time "macos short go test" -- go test -v -race -short -timeout=30m -skip '^TestEmbedded' ./... + + macos-candidates: + name: Measure macOS no-short candidates + if: ${{ inputs.suite == 'macos-candidates' }} + runs-on: macos-latest + timeout-minutes: 60 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + + - name: Set up Go + uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 + with: + go-version-file: 'go.mod' + + - name: Install Dolt + run: ./scripts/ci/install-dolt.sh + + - name: Configure Git and Dolt identity + run: | + git config --global user.name "CI Bot" + git config --global user.email "ci@beads.test" + dolt config --global --add user.name "CI Bot" + dolt config --global --add user.email "ci@beads.test" + + - name: Measure commands + run: | + set -euo pipefail + source scripts/ci/lib/timing.sh + + status=0 + ci_time "macos no-short go test" -- go test -tags=gms_pure_go -v -race -timeout=30m -skip '^TestEmbedded' ./... || status=$? + ci_time "macos integration go test" -- go test -tags=integration,gms_pure_go -v -race -timeout=30m -skip '^TestEmbedded' ./... || status=$? + exit "$status" + + linux-integration: + name: Measure Linux integration tests + if: ${{ inputs.suite == 'linux-integration' }} + runs-on: ubuntu-latest + timeout-minutes: 45 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + + - name: Set up Go + uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 + with: + go-version-file: 'go.mod' + + - name: Install Dolt + run: ./scripts/ci/install-dolt.sh + + - name: Configure Git and Dolt identity + run: | + git config --global user.name "CI Bot" + git config --global user.email "ci@beads.test" + dolt config --global --add user.name "CI Bot" + dolt config --global --add user.email "ci@beads.test" + + - name: Measure commands + run: | + set -euo pipefail + mkdir -p artifacts + source scripts/ci/lib/timing.sh + + ci_time "install gotestsum" -- go install gotest.tools/gotestsum@v1.13.0 + ci_time "linux integration go test" -- env BEADS_TEST_SKIP=dolt gotestsum --junitfile artifacts/linux-integration-junit.xml --format testdox -- -v -race -tags=integration,gms_pure_go -timeout=30m ./... + + - name: Upload measurement artifacts + if: always() + uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7 + with: + name: ci-measure-linux-integration + path: artifacts/ + retention-days: 7 + if-no-files-found: ignore + + linux-integration-sharded: + name: Measure Linux integration shard ${{ matrix.shard }}/6 + if: ${{ inputs.suite == 'linux-integration-sharded' }} + runs-on: ubuntu-latest + timeout-minutes: 30 + strategy: + fail-fast: false + matrix: + shard: [1, 2, 3, 4, 5, 6] + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + + - name: Set up Go + uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 + with: + go-version-file: 'go.mod' + + - name: Install Dolt + run: ./scripts/ci/install-dolt.sh + + - name: Configure Git and Dolt identity + run: | + git config --global user.name "CI Bot" + git config --global user.email "ci@beads.test" + dolt config --global --add user.name "CI Bot" + dolt config --global --add user.email "ci@beads.test" + + - name: Measure shard + env: + SHARD: ${{ matrix.shard }} + TOTAL_SHARDS: 6 + run: | + set -euo pipefail + mkdir -p artifacts + source scripts/ci/lib/timing.sh + + ci_time "install gotestsum" -- go install gotest.tools/gotestsum@v1.13.0 + + mapfile -t packages < <( + GO_TEST_SHARD_TAGS=integration,gms_pure_go \ + scripts/ci/go-test-shard-packages.sh "$SHARD" "$TOTAL_SHARDS" + ) + package_manifest="artifacts/linux-integration-packages-${SHARD}-of-${TOTAL_SHARDS}.txt" + printf '%s\n' "${packages[@]}" | tee "$package_manifest" + + if [[ "${#packages[@]}" -eq 0 ]]; then + echo "No packages assigned to shard ${SHARD}/${TOTAL_SHARDS}; skipping go test" + exit 0 + fi + + ci_time "linux integration shard ${SHARD}/${TOTAL_SHARDS} go test" -- \ + env BEADS_TEST_SKIP=dolt gotestsum \ + --junitfile "artifacts/linux-integration-shard-${SHARD}-of-${TOTAL_SHARDS}.xml" \ + --format testdox \ + -- -v -race -tags=integration,gms_pure_go -timeout=30m "${packages[@]}" + + - name: Upload measurement artifacts + if: always() + uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7 + with: + name: ci-measure-linux-integration-shard-${{ matrix.shard }}-of-6 + path: artifacts/ + retention-days: 7 + if-no-files-found: ignore + + linux-integration-hybrid-packages: + name: Measure Linux integration package shard ${{ matrix.shard }}/6 (no cmd/bd) + if: ${{ inputs.suite == 'linux-integration-hybrid-sharded' || inputs.suite == 'linux-integration-hybrid-16-sharded' || inputs.suite == 'linux-integration-hybrid-prebuilt-sharded' }} + runs-on: ubuntu-latest + timeout-minutes: 30 + strategy: + fail-fast: false + matrix: + shard: [1, 2, 3, 4, 5, 6] + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + + - name: Set up Go + uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 + with: + go-version-file: 'go.mod' + + - name: Install Dolt + run: ./scripts/ci/install-dolt.sh + + - name: Configure Git and Dolt identity + run: | + git config --global user.name "CI Bot" + git config --global user.email "ci@beads.test" + dolt config --global --add user.name "CI Bot" + dolt config --global --add user.email "ci@beads.test" + + - name: Measure shard + env: + SHARD: ${{ matrix.shard }} + TOTAL_SHARDS: 6 + run: | + set -euo pipefail + mkdir -p artifacts + source scripts/ci/lib/timing.sh + + ci_time "install gotestsum" -- go install gotest.tools/gotestsum@v1.13.0 + + package_manifest="artifacts/linux-integration-hybrid-packages-${SHARD}-of-${TOTAL_SHARDS}.txt" + write_package_manifest() { + GO_TEST_SHARD_TAGS=integration,gms_pure_go \ + GO_TEST_SHARD_EXCLUDE_REGEX="^github.com/steveyegge/beads/cmd/bd$" \ + scripts/ci/go-test-shard-packages.sh "$SHARD" "$TOTAL_SHARDS" > "$package_manifest" + } + ci_time "list integration package shard ${SHARD}/${TOTAL_SHARDS}" -- write_package_manifest + cat "$package_manifest" + mapfile -t packages < "$package_manifest" + + if [[ "${#packages[@]}" -eq 0 ]]; then + echo "No packages assigned to shard ${SHARD}/${TOTAL_SHARDS}; skipping go test" + exit 0 + fi + + ci_time "linux integration package shard ${SHARD}/${TOTAL_SHARDS} go test" -- \ + env BEADS_TEST_SKIP=dolt gotestsum \ + --junitfile "artifacts/linux-integration-hybrid-packages-${SHARD}-of-${TOTAL_SHARDS}.xml" \ + --format testdox \ + -- -v -race -tags=integration,gms_pure_go -timeout=30m "${packages[@]}" + + - name: Upload measurement artifacts + if: always() + uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7 + with: + name: ci-measure-linux-integration-hybrid-packages-${{ matrix.shard }}-of-6 + path: artifacts/ + retention-days: 7 + if-no-files-found: ignore + + linux-integration-hybrid-cmd-bd: + name: Measure Linux integration cmd/bd shard ${{ matrix.shard }}/8 + if: ${{ inputs.suite == 'linux-integration-hybrid-sharded' }} + runs-on: ubuntu-latest + timeout-minutes: 30 + strategy: + fail-fast: false + matrix: + shard: [1, 2, 3, 4, 5, 6, 7, 8] + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + + - name: Set up Go + uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 + with: + go-version-file: 'go.mod' + + - name: Install Dolt + run: ./scripts/ci/install-dolt.sh + + - name: Configure Git and Dolt identity + run: | + git config --global user.name "CI Bot" + git config --global user.email "ci@beads.test" + dolt config --global --add user.name "CI Bot" + dolt config --global --add user.email "ci@beads.test" + + - name: Measure shard + env: + SHARD: ${{ matrix.shard }} + TOTAL_SHARDS: 8 + run: | + set -euo pipefail + mkdir -p artifacts + source scripts/ci/lib/timing.sh + + ci_time "install gotestsum" -- go install gotest.tools/gotestsum@v1.13.0 + + test_manifest="artifacts/linux-integration-cmd-bd-tests-${SHARD}-of-${TOTAL_SHARDS}.txt" + write_test_manifest() { + GO_TEST_SHARD_TAGS=integration,gms_pure_go \ + scripts/ci/go-test-shard-names.sh "$SHARD" "$TOTAL_SHARDS" ./cmd/bd > "$test_manifest" + } + ci_time "list cmd/bd test shard ${SHARD}/${TOTAL_SHARDS}" -- write_test_manifest + cat "$test_manifest" + mapfile -t tests < "$test_manifest" + + if [[ "${#tests[@]}" -eq 0 ]]; then + echo "No tests assigned to shard ${SHARD}/${TOTAL_SHARDS}; skipping go test" + exit 0 + fi + + run_regex="^($(IFS='|'; echo "${tests[*]}"))$" + ci_time "linux integration cmd/bd shard ${SHARD}/${TOTAL_SHARDS} go test" -- \ + env BEADS_TEST_SKIP=dolt gotestsum \ + --junitfile "artifacts/linux-integration-cmd-bd-${SHARD}-of-${TOTAL_SHARDS}.xml" \ + --format testdox \ + -- -v -race -tags=integration,gms_pure_go -timeout=30m -run "$run_regex" ./cmd/bd + + - name: Upload measurement artifacts + if: always() + uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7 + with: + name: ci-measure-linux-integration-cmd-bd-${{ matrix.shard }}-of-8 + path: artifacts/ + retention-days: 7 + if-no-files-found: ignore + + linux-integration-hybrid-16-cmd-bd: + name: Measure Linux integration cmd/bd shard ${{ matrix.shard }}/16 + if: ${{ inputs.suite == 'linux-integration-hybrid-16-sharded' }} + runs-on: ubuntu-latest + timeout-minutes: 30 + strategy: + fail-fast: false + matrix: + shard: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16] + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + + - name: Set up Go + uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 + with: + go-version-file: 'go.mod' + + - name: Install Dolt + run: ./scripts/ci/install-dolt.sh + + - name: Configure Git and Dolt identity + run: | + git config --global user.name "CI Bot" + git config --global user.email "ci@beads.test" + dolt config --global --add user.name "CI Bot" + dolt config --global --add user.email "ci@beads.test" + + - name: Measure shard + env: + SHARD: ${{ matrix.shard }} + TOTAL_SHARDS: 16 + run: | + set -euo pipefail + mkdir -p artifacts + source scripts/ci/lib/timing.sh + + ci_time "install gotestsum" -- go install gotest.tools/gotestsum@v1.13.0 + + test_manifest="artifacts/linux-integration-cmd-bd-tests-${SHARD}-of-${TOTAL_SHARDS}.txt" + write_test_manifest() { + GO_TEST_SHARD_TAGS=integration,gms_pure_go \ + scripts/ci/go-test-shard-names.sh "$SHARD" "$TOTAL_SHARDS" ./cmd/bd > "$test_manifest" + } + ci_time "list cmd/bd test shard ${SHARD}/${TOTAL_SHARDS}" -- write_test_manifest + cat "$test_manifest" + mapfile -t tests < "$test_manifest" + + if [[ "${#tests[@]}" -eq 0 ]]; then + echo "No tests assigned to shard ${SHARD}/${TOTAL_SHARDS}; skipping go test" + exit 0 + fi + + run_regex="^($(IFS='|'; echo "${tests[*]}"))$" + ci_time "linux integration cmd/bd shard ${SHARD}/${TOTAL_SHARDS} go test" -- \ + env BEADS_TEST_SKIP=dolt gotestsum \ + --junitfile "artifacts/linux-integration-cmd-bd-${SHARD}-of-${TOTAL_SHARDS}.xml" \ + --format testdox \ + -- -v -race -tags=integration,gms_pure_go -timeout=30m -run "$run_regex" ./cmd/bd + + - name: Upload measurement artifacts + if: always() + uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7 + with: + name: ci-measure-linux-integration-cmd-bd-${{ matrix.shard }}-of-16 + path: artifacts/ + retention-days: 7 + if-no-files-found: ignore + + linux-integration-hybrid-prebuilt-bd: + name: Measure Linux integration prebuild cmd/bd binary + if: ${{ inputs.suite == 'linux-integration-hybrid-prebuilt-sharded' }} + runs-on: ubuntu-latest + timeout-minutes: 20 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + + - name: Set up Go + uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 + with: + go-version-file: 'go.mod' + + - name: Measure build + run: | + set -euo pipefail + mkdir -p artifacts + source scripts/ci/lib/timing.sh + source ./.buildflags + + ci_time "build cmd/bd subprocess binary" -- go build -tags "$BEADS_BUILD_TAGS" -o artifacts/bd ./cmd/bd + + - name: Upload prebuilt bd + uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7 + with: + name: ci-measure-linux-integration-prebuilt-bd + path: artifacts/bd + retention-days: 7 + if-no-files-found: error + + linux-integration-hybrid-prebuilt-cmd-bd: + name: Measure Linux integration prebuilt cmd/bd shard ${{ matrix.shard }}/8 + if: ${{ inputs.suite == 'linux-integration-hybrid-prebuilt-sharded' }} + needs: linux-integration-hybrid-prebuilt-bd + runs-on: ubuntu-latest + timeout-minutes: 30 + strategy: + fail-fast: false + matrix: + shard: [1, 2, 3, 4, 5, 6, 7, 8] + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + + - name: Set up Go + uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 + with: + go-version-file: 'go.mod' + + - name: Download prebuilt bd + uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8 + with: + name: ci-measure-linux-integration-prebuilt-bd + path: prebuilt-bd + + - name: Install Dolt + run: ./scripts/ci/install-dolt.sh + + - name: Configure Git and Dolt identity + run: | + git config --global user.name "CI Bot" + git config --global user.email "ci@beads.test" + dolt config --global --add user.name "CI Bot" + dolt config --global --add user.email "ci@beads.test" + + - name: Measure shard + env: + SHARD: ${{ matrix.shard }} + TOTAL_SHARDS: 8 + BEADS_TEST_BD_BINARY: ${{ github.workspace }}/prebuilt-bd/bd + run: | + set -euo pipefail + mkdir -p artifacts + chmod +x "$BEADS_TEST_BD_BINARY" + source scripts/ci/lib/timing.sh + + ci_time "install gotestsum" -- go install gotest.tools/gotestsum@v1.13.0 + + test_manifest="artifacts/linux-integration-cmd-bd-tests-${SHARD}-of-${TOTAL_SHARDS}.txt" + write_test_manifest() { + GO_TEST_SHARD_TAGS=integration,gms_pure_go \ + scripts/ci/go-test-shard-names.sh "$SHARD" "$TOTAL_SHARDS" ./cmd/bd > "$test_manifest" + } + ci_time "list cmd/bd test shard ${SHARD}/${TOTAL_SHARDS}" -- write_test_manifest + cat "$test_manifest" + mapfile -t tests < "$test_manifest" + + if [[ "${#tests[@]}" -eq 0 ]]; then + echo "No tests assigned to shard ${SHARD}/${TOTAL_SHARDS}; skipping go test" + exit 0 + fi + + run_regex="^($(IFS='|'; echo "${tests[*]}"))$" + ci_time "linux integration prebuilt cmd/bd shard ${SHARD}/${TOTAL_SHARDS} go test" -- \ + env BEADS_TEST_SKIP=dolt gotestsum \ + --junitfile "artifacts/linux-integration-prebuilt-cmd-bd-${SHARD}-of-${TOTAL_SHARDS}.xml" \ + --format testdox \ + -- -v -race -tags=integration,gms_pure_go -timeout=30m -run "$run_regex" ./cmd/bd + + - name: Upload measurement artifacts + if: always() + uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7 + with: + name: ci-measure-linux-integration-prebuilt-cmd-bd-${{ matrix.shard }}-of-8 + path: artifacts/ + retention-days: 7 + if-no-files-found: ignore + + linux-integration-coverage: + name: Measure Linux integration coverage + if: ${{ inputs.suite == 'linux-integration-coverage' }} + runs-on: ubuntu-latest + timeout-minutes: 45 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + + - name: Set up Go + uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 + with: + go-version-file: 'go.mod' + + - name: Install Dolt + run: ./scripts/ci/install-dolt.sh + + - name: Configure Git and Dolt identity + run: | + git config --global user.name "CI Bot" + git config --global user.email "ci@beads.test" + dolt config --global --add user.name "CI Bot" + dolt config --global --add user.email "ci@beads.test" + + - name: Measure commands + run: | + set -euo pipefail + mkdir -p artifacts + source scripts/ci/lib/timing.sh + + ci_time "install gotestsum" -- go install gotest.tools/gotestsum@v1.13.0 + status=0 + ci_time "linux integration coverage go test" -- env BEADS_TEST_SKIP=dolt gotestsum --junitfile artifacts/linux-integration-coverage-junit.xml --format testdox -- -v -race -tags=integration,gms_pure_go -coverprofile=artifacts/coverage.out -timeout=30m ./... || status=$? + if [[ -f artifacts/coverage.out ]]; then + ci_time "coverage summary" -- go tool cover -func=artifacts/coverage.out || status=$? + else + echo "coverage.out was not generated; skipping coverage summary" + fi + exit "$status" + + - name: Upload measurement artifacts + if: always() + uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7 + with: + name: ci-measure-linux-integration-coverage + path: artifacts/ + retention-days: 7 + if-no-files-found: ignore + + cross-version-smoke: + name: Measure cross-version smoke + if: ${{ inputs.suite == 'cross-version-smoke' }} + runs-on: ubuntu-latest + timeout-minutes: 20 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + + - name: Set up Go + uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 + with: + go-version-file: 'go.mod' + + - name: Configure Git + run: | + git config --global user.name "CI Bot" + git config --global user.email "ci@beads.test" + + - name: Cache previous release binaries + uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v5 + with: + path: ~/.cache/beads-regression + key: smoke-binaries-measure-${{ runner.os }} + + - name: Measure commands + env: + GH_TOKEN: ${{ github.token }} + SMOKE_VERSION: ${{ inputs.smoke_version }} + run: | + set -euo pipefail + source scripts/ci/lib/timing.sh + source ./.buildflags + + ci_time "build candidate binary" -- go build -o /tmp/bd-candidate ./cmd/bd + export CANDIDATE_BIN=/tmp/bd-candidate + if [[ -z "$SMOKE_VERSION" ]]; then + SMOKE_VERSION="$(gh release list --repo "$GITHUB_REPOSITORY" --limit 1 --json tagName --jq '.[0].tagName')" + fi + if [[ -z "$SMOKE_VERSION" ]]; then + echo "Could not resolve a previous release tag for cross-version smoke" + exit 1 + fi + ci_time "upgrade smoke ${SMOKE_VERSION}" -- ./scripts/upgrade-smoke-test.sh "$SMOKE_VERSION" + + nix: + name: Measure nix build + if: ${{ inputs.suite == 'nix' }} + runs-on: ubuntu-latest + timeout-minutes: 20 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + + - name: Install Nix + uses: DeterminateSystems/determinate-nix-action@61cbfe2efc2d4e7a8a6d56967c3c1058e846c858 # v3 + + - name: Measure commands + run: | + set -euo pipefail + source scripts/ci/lib/timing.sh + + ci_time "nix build .#default" -- nix build .#default --print-build-logs + + mcp-package: + name: Measure MCP package checks + if: ${{ inputs.suite == 'mcp-package' }} + runs-on: ubuntu-latest + timeout-minutes: 20 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + + - name: Set up Go + uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 + with: + go-version-file: 'go.mod' + + - name: Set up Python + uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0 + with: + python-version: '3.14' + + - name: Measure commands + run: | + set -euo pipefail + source scripts/ci/lib/timing.sh + + ci_time "install uv" -- pip install uv==0.11.16 + ci_time "mcp package gate" -- make ci-package-mcp + + - name: Upload measurement artifacts + if: always() + uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7 + with: + name: ci-measure-mcp-package + path: integrations/beads-mcp/dist/ + retention-days: 7 + if-no-files-found: ignore + + npm-package: + name: Measure npm package checks + if: ${{ inputs.suite == 'npm-package' }} + runs-on: ubuntu-latest + timeout-minutes: 20 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + + - name: Set up Go + uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 + with: + go-version-file: 'go.mod' + + - name: Set up Node.js + uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v6 + with: + node-version: '24' + + - name: Measure commands + run: | + set -euo pipefail + source scripts/ci/lib/timing.sh + ci_time "npm package gate" -- make ci-package-npm + + - name: Upload measurement artifacts + if: always() + uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7 + with: + name: ci-measure-npm-package + path: artifacts/ + retention-days: 7 + if-no-files-found: ignore + diff --git a/.github/workflows/close-stale-needs.yml b/.github/workflows/close-stale-needs.yml deleted file mode 100644 index 9e0793973..000000000 --- a/.github/workflows/close-stale-needs.yml +++ /dev/null @@ -1,99 +0,0 @@ -name: Close stale needs-info / needs-repro issues - -on: - schedule: - - cron: '37 9 * * *' - workflow_dispatch: - -permissions: {} - -jobs: - close-needs-info: - runs-on: ubuntu-latest - permissions: - issues: write - steps: - - name: Close stale needs-info issues (14 days) - uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8 - with: - script: | - const labels = ['status/needs-info', 'status/needs-repro']; - const days = 14; - const cutoff = new Date(Date.now() - days * 24 * 60 * 60 * 1000); - - for (const label of labels) { - const issues = await github.paginate(github.rest.issues.listForRepo, { - owner: context.repo.owner, - repo: context.repo.repo, - state: 'open', - labels: label, - per_page: 100, - }); - - for (const issue of issues) { - if (issue.pull_request) continue; - - const events = await github.paginate(github.rest.issues.listEvents, { - owner: context.repo.owner, - repo: context.repo.repo, - issue_number: issue.number, - per_page: 100, - }); - - const labelEvents = events - .filter(e => e.event === 'labeled' && e.label?.name === label); - if (labelEvents.length === 0) continue; - - const labeledAt = new Date(Math.max( - ...labelEvents.map(e => new Date(e.created_at).getTime()) - )); - - const comments = await github.paginate(github.rest.issues.listComments, { - owner: context.repo.owner, - repo: context.repo.repo, - issue_number: issue.number, - since: labeledAt.toISOString(), - per_page: 100, - }); - - const visibleRequest = comments.find(c => { - if (c.user?.login === issue.user?.login) return false; - const lower = (c.body || '').toLowerCase(); - if (lower.includes('closing')) return false; - if (!/14\s*-?\s*days?/.test(lower)) return false; - if (!lower.includes('reply') && !lower.includes('respond')) return false; - if (label === 'status/needs-repro') return lower.includes('repro') || lower.includes('reproduction'); - return lower.includes('info') || lower.includes('information') || lower.includes('details'); - }); - - if (!visibleRequest) { - console.log(`No visible request for #${issue.number} (${label}), skipping.`); - continue; - } - - const requestAt = new Date(visibleRequest.created_at); - if (requestAt > cutoff) continue; - - const authorReplied = comments.some(c => - c.user?.login === issue.user?.login && - new Date(c.created_at) > requestAt - ); - if (authorReplied) continue; - - await github.rest.issues.createComment({ - owner: context.repo.owner, - repo: context.repo.repo, - issue_number: issue.number, - body: `Closing — this was marked \`${label}\` and a response was requested 14 or more days ago with no reply.\n\nIf this is still relevant, please reply with the requested details and we'll reopen.`, - }); - - await github.rest.issues.update({ - owner: context.repo.owner, - repo: context.repo.repo, - issue_number: issue.number, - state: 'closed', - state_reason: 'not_planned', - }); - console.log(`Closed #${issue.number} (${label}, requested ${requestAt.toISOString()})`); - } - } diff --git a/.github/workflows/conformance.yml b/.github/workflows/conformance.yml new file mode 100644 index 000000000..b79c71bbb --- /dev/null +++ b/.github/workflows/conformance.yml @@ -0,0 +1,33 @@ +name: Conformance + +# Runs the same two-tier storage conformance suite developers run locally: +# the in-process store corpus and the real-binary CLI harness. + +on: + pull_request: + branches: [ main, 'release/**', 'hotfix/**' ] + merge_group: + +concurrency: + group: ${{ github.workflow }}-${{ github.event_name }}-${{ github.event.pull_request.number || github.ref }} + cancel-in-progress: true + +env: + BD_DISABLE_METRICS: "1" + BD_DISABLE_EVENT_FLUSH: "1" + +jobs: + conformance: + name: Storage backend conformance (embedded Dolt oracle) + timeout-minutes: 45 + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + + - name: Set up Go + uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 + with: + go-version-file: 'go.mod' + + - name: Run conformance (embedded Dolt oracle) + run: ./scripts/conformance.sh diff --git a/.github/workflows/cross-version-smoke.yml b/.github/workflows/cross-version-smoke.yml new file mode 100644 index 000000000..5f165fca7 --- /dev/null +++ b/.github/workflows/cross-version-smoke.yml @@ -0,0 +1,90 @@ +name: Cross-Version Smoke Tests + +on: + # On tag push (release gate): test the last 30 releases for broad regression coverage + push: + tags: + - 'v*' + + # On PR to main or a release branch: test only the last 5 releases for fast + # feedback. Release-targeted PRs are exactly the ones where cross-version + # upgrade breakage is most expensive to discover at tag time. + pull_request: + branches: [ main, 'release/**', 'hotfix/**' ] + + # Allow manual trigger with custom version count + workflow_dispatch: + inputs: + version_count: + description: 'Number of previous releases to test against' + required: false + default: '5' + +concurrency: + group: ${{ github.workflow }}-${{ github.event_name }}-${{ github.event.pull_request.number || github.ref }} + cancel-in-progress: true + +env: + BD_DISABLE_METRICS: "1" + BD_DISABLE_EVENT_FLUSH: "1" + +jobs: + smoke: + name: Upgrade smoke (${{ matrix.prev_version }} → candidate) + runs-on: ubuntu-latest + timeout-minutes: 15 + + strategy: + fail-fast: false + matrix: + prev_version: ${{ fromJson(needs.versions.outputs.matrix) }} + + needs: versions + + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + + - name: Set up Go + uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 + with: + go-version-file: 'go.mod' + + - name: Configure Git + run: | + git config --global user.name "CI Bot" + git config --global user.email "ci@beads.test" + + - name: Cache previous release binaries + uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v5 + with: + path: ~/.cache/beads-regression + key: smoke-binaries-${{ matrix.prev_version }}-${{ runner.os }} + + - name: Build candidate binary + run: go build -tags gms_pure_go -o /tmp/bd-candidate ./cmd/bd + + - name: Run upgrade smoke tests + env: + CANDIDATE_BIN: /tmp/bd-candidate + run: ./scripts/upgrade-smoke-test.sh ${{ matrix.prev_version }} + + versions: + name: Resolve versions to test + runs-on: ubuntu-latest + outputs: + matrix: ${{ steps.resolve.outputs.matrix }} + steps: + - name: Resolve release versions + id: resolve + env: + GH_TOKEN: ${{ github.token }} + # Tag push and manual dispatch test more versions; PRs test fewer for speed + VERSION_COUNT: ${{ (github.event_name == 'push' && '30') || (github.event_name == 'workflow_dispatch' && inputs.version_count) || '5' }} + run: | + VERSIONS=$(gh release list \ + --repo gastownhall/beads \ + --limit "$VERSION_COUNT" \ + --json tagName \ + --jq '[.[].tagName]') + echo "matrix=$VERSIONS" >> "$GITHUB_OUTPUT" + echo "Testing versions: $VERSIONS" diff --git a/.github/workflows/docs-autofix.yml b/.github/workflows/docs-autofix.yml index f8dcbfd33..c40ec3bb5 100644 --- a/.github/workflows/docs-autofix.yml +++ b/.github/workflows/docs-autofix.yml @@ -11,27 +11,16 @@ # never checks out or executes PR code. It checks out the base default branch # for scripts, and treats the artifact strictly as data: every path in the # patch must match an anchored generated-docs allowlist (single path segment, -# no traversal), mode/symlink/rename/copy/binary hunks are refused, and the -# patch is applied to an index in a bare clone and re-checked once staged -# (scripts/docs-autofix-push.sh), so a hostile patch can at most rewrite -# generated doc files on its own PR branch. Pushes are leased to the run's -# head SHA and skipped for heads that are protected or under a ruleset. The -# App private key is a secret of the `autofix` environment, whose deployment- -# branch policy allows only `main`, so only a job running on main can read -# it; the minted token itself is scoped to this one repo with contents:write -# only, so even a hostile patch gains nothing beyond what the job already -# had. -# -# CONCURRENCY: job-level, shared with bazel-autofix.yml per head branch and -# without cancel-in-progress, so the two bots queue instead of racing on one -# branch (see bazel-autofix.yml). +# no traversal, no symlink modes) and `git apply --index` supplies the +# underlying escape guards (scripts/docs-autofix-push.sh), so a hostile patch +# can at most rewrite generated doc files on its own PR branch. # # TOKEN NOTE: pushes made with the default GITHUB_TOKEN do not retrigger PR -# checks. On same-repo PRs the push step mints a short-lived installation -# token for the gastownhall-autofix GitHub App (contents:write, scoped to -# this repo only) and uses that instead. Fork PRs always get the apply-recipe -# comment - no token we hold can push to a fork, so the mint step is skipped -# for them. +# checks. Configure a DOCS_AUTOFIX_TOKEN repo secret (fine-grained PAT or +# GitHub App token with contents:write on this repo) for fully hands-off +# operation on same-repo PRs; without it the pushed fix still lands but +# checks need a manual re-run. Fork PRs always get the apply-recipe comment - +# no token we hold can push to a fork. name: Docs Autofix @@ -45,6 +34,10 @@ permissions: pull-requests: write actions: read +concurrency: + group: docs-autofix-${{ github.event.workflow_run.head_repository.full_name }}-${{ github.event.workflow_run.head_branch }} + cancel-in-progress: true + jobs: autofix: name: Apply CLI docs regeneration to PR @@ -52,22 +45,9 @@ jobs: github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'failure' runs-on: ubuntu-latest - # Bounded: a hung run would hold the shared group for the default 6h. - timeout-minutes: 10 - # Grants the job read access to this environment's secrets; its - # deployment-branch policy restricts that to jobs running on main - # (workflow_run always does), not to any PR branch. - environment: autofix - # Shared with bazel-autofix.yml; queue, never cancel. - concurrency: - group: autofix-${{ github.event.workflow_run.head_repository.full_name }}-${{ github.event.workflow_run.head_branch }} - cancel-in-progress: false steps: - # Trusted side only: base repo default branch, never the PR head. The - # workflow token is not left in .git/config. + # Trusted side only: base repo default branch, never the PR head. - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 - with: - persist-credentials: false - name: Download docs freshness patch (if any) id: patch @@ -83,40 +63,11 @@ jobs: echo "found=false" >> "$GITHUB_OUTPUT" exit 0 fi - dir="${RUNNER_TEMP}/docs-patch" - mkdir -p "$dir" - gh api "repos/${GITHUB_REPOSITORY}/actions/artifacts/${artifact_id}/zip" > "${RUNNER_TEMP}/docs-patch.zip" - # Extract only the expected name, and only as a regular file. - unzip -o "${RUNNER_TEMP}/docs-patch.zip" cli-docs-freshness.patch -d "$dir" || true - ls -la "$dir" - f="$dir/cli-docs-freshness.patch" - if [ -L "$f" ] || { [ -e "$f" ] && [ ! -f "$f" ]; }; then - echo "::error::artifact entry cli-docs-freshness.patch is not a regular file" - exit 1 - fi - if [ ! -f "$f" ]; then - echo "Artifact has no cli-docs-freshness.patch; nothing to do." - echo "found=false" >> "$GITHUB_OUTPUT" - exit 0 - fi + gh api "repos/${GITHUB_REPOSITORY}/actions/artifacts/${artifact_id}/zip" > patch.zip + unzip -o patch.zip -d "${RUNNER_TEMP}/docs-patch" + ls -la "${RUNNER_TEMP}/docs-patch" echo "found=true" >> "$GITHUB_OUTPUT" - # Same-repo PRs only: no token we hold can push to a fork, and a fork's - # head_repository can even be absent (deleted fork), so this mints - # nothing for it and the push script falls back to the apply-recipe - # comment. app-token outputs are empty (not failed) when the step is - # skipped, which is what the fallback below relies on. - - name: Mint autofix app token - id: app-token - if: github.event.workflow_run.head_repository.full_name == github.repository - uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3 - with: - app-id: ${{ secrets.AUTOFIX_APP_ID }} - private-key: ${{ secrets.AUTOFIX_APP_PRIVATE_KEY }} - owner: ${{ github.repository_owner }} - repositories: ${{ github.event.repository.name }} - permission-contents: write - - name: Push regen commit or leave apply recipe if: steps.patch.outputs.found == 'true' env: @@ -129,9 +80,9 @@ jobs: RUN_URL: ${{ github.event.workflow_run.html_url }} # Split tokens: gh api calls (PR lookup, comments) always use the # workflow token, which carries this job's pull-requests:write; the - # App token is used ONLY for the git push, so it needs no more + # optional PAT is used ONLY for the git push, so it needs no more # than contents:write. GH_TOKEN: ${{ github.token }} - PUSH_TOKEN: ${{ steps.app-token.outputs.token || github.token }} - AUTOFIX_TOKEN_KIND: ${{ steps.app-token.outputs.token && 'app' || 'default' }} + PUSH_TOKEN: ${{ secrets.DOCS_AUTOFIX_TOKEN || github.token }} + AUTOFIX_TOKEN_KIND: ${{ secrets.DOCS_AUTOFIX_TOKEN && 'pat' || 'default' }} run: ./scripts/docs-autofix-push.sh diff --git a/.github/workflows/docs-mintlify.yml b/.github/workflows/docs-mintlify.yml index 171174ee0..57b5d988b 100644 --- a/.github/workflows/docs-mintlify.yml +++ b/.github/workflows/docs-mintlify.yml @@ -3,15 +3,22 @@ name: Docs (Mintlify) checks on: pull_request: types: [opened, synchronize, reopened, ready_for_review] - # The Mintlify site and the check itself. The docsync guard (docs nav, - # links and agent-instruction routing over docs/, engdocs/ and the root - # markdown) is //test/docsync:docsync_test, which bazel.yml's --config=ci - # lane runs on every PR; this workflow keeps only Mintlify's own - # broken-link check, which needs the network (npx mint) and the base - # branch's history. paths: + # The docsync guard also validates engdocs/** and the curated root + # markdown files (rootDocFiles in test/docsync/docsync_test.go) — keep + # this filter in sync with that list, or a docs-breaking PR merges + # green and turns main red at the full test run. - 'docs/**' + - 'engdocs/**' + - 'test/docsync/**' - 'mint.sh' + - 'README.md' + - 'AGENTS.md' + - 'AGENT_INSTRUCTIONS.md' + - 'CONTRIBUTING.md' + - 'CLAUDE.md' + - 'RELEASING.md' + - 'PR_MAINTAINER_GUIDELINES.md' - '.github/scripts/docs-render-check.sh' - '.github/workflows/docs-mintlify.yml' @@ -23,10 +30,24 @@ concurrency: cancel-in-progress: true jobs: + docsync: + name: Docs nav/link sync (docsync guard) + runs-on: ubuntu-latest + if: github.event.pull_request.draft == false + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + + - name: Set up Go + uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 + with: + go-version-file: 'go.mod' + + - name: Run docsync guard + run: go test -tags=gms_pure_go ./test/docsync + broken-links: name: Mintlify broken-links (baseline-aware) - runs-on: ${{ (github.event_name == 'merge_group' || (github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository && github.actor != 'dependabot[bot]')) && 'blacksmith-2vcpu-ubuntu-2404' || 'ubuntu-latest' }} - timeout-minutes: 10 + runs-on: ubuntu-latest if: github.event.pull_request.draft == false steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index c64890e66..97d342eba 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -13,26 +13,367 @@ env: BD_DISABLE_EVENT_FLUSH: "1" jobs: + detect-ci-tier: + name: Detect CI tier + runs-on: ubuntu-latest + outputs: + full_embedded: ${{ steps.tier.outputs.full_embedded }} + reason: ${{ steps.tier.outputs.reason }} + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + with: + fetch-depth: 0 - # macOS full -race -short suite. Linux runs the same suite under Bazel - # (bazel.yml's `test` lane, on every push to main, executed on rbe-west), - # and Windows uses smoke tests below; rbe-west has no macOS workers, so this - # is the one lane that still runs the suite on a runner. Blacksmith macOS, - # the image pr.yml's same-repo macOS legs use; blacksmith-macos-go-build-cache - # seeds its module and non-race caches, and this job saves only its own race - # cache (keyed per go.sum and UTC day, F7b review fix S7). - # - # ./cmd/bd has outgrown `go test`'s 10m per-package default (see - # scripts/test.sh, scripts/ci/pr-core.sh and #6091), and when it trips the - # alarm the panic names whichever test was in flight, so it reads like a - # hang. -timeout=30m sits on the invocation and inside one finite job - # budget: the go deadline fires first and prints goroutine stacks, so a real - # hang is still diagnosable rather than cut off by a bare job kill. - test: - name: Test (macOS) - if: github.event_name == 'push' && github.ref == 'refs/heads/main' && github.repository == 'gastownhall/beads' - runs-on: ${{ 'blacksmith-6vcpu-macos-26' }} - timeout-minutes: 45 + - name: Decide embedded Dolt coverage + id: tier + env: + PR_BASE_SHA: ${{ github.event.pull_request.base.sha }} + PR_HEAD_SHA: ${{ github.event.pull_request.head.sha }} + run: bash .github/scripts/ci-embedded-tier.sh + + # Same guard as pr.yml's check-migration-hygiene: direct pushes to main + # (and PR merges whose base moved) must not bypass the migration lints. + # BASE_SHA is the pre-push tip; on force pushes or history rewrites the + # script warns and skips the frozen-migrations diff but still runs the + # duplicate-version and nondeterministic-SQL checks. + check-migration-hygiene: + name: Check migration hygiene + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + with: + fetch-depth: 0 + + - name: Run migration hygiene checks + env: + BASE_SHA: ${{ github.event.before }} + run: ./scripts/check-migration-hygiene.sh + + build-artifacts: + name: Build Artifacts + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + with: + fetch-depth: 0 + + - name: Set up Go + id: setup-go + uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 + with: + go-version-file: 'go.mod' + cache: false + + - name: Restore Go module cache + id: restore-go-module-cache + uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 + with: + path: ~/go/pkg/mod + key: beads-go-mod-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-${{ hashFiles('go.mod', 'go.sum') }} + restore-keys: beads-go-mod-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}- + + # Per `go help cache`, Go's content-addressed build cache includes + # compiler options, so this base-tag directory can serve builds with + # additional tags. Race builds stay separate; Windows native/cgo + # non-race builds share theirs. + - name: Restore non-race Go build cache + id: restore-non-race-go-build-cache + uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 + with: + path: ${{ runner.temp }}/go-cache/non-race + key: beads-go-build-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-base-gms_pure_go-non-race-${{ github.sha }} + restore-keys: beads-go-build-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-base-gms_pure_go-non-race- + + - name: Build reusable Linux artifacts + env: + GOCACHE: ${{ runner.temp }}/go-cache/non-race + run: | + set -euo pipefail + mkdir -p artifacts + source ./.buildflags + go_version="$(go version)" + commit="$(git rev-parse HEAD)" + go build -tags "$BEADS_BUILD_TAGS" -o artifacts/bd-linux-gms-pure ./cmd/bd + chmod +x artifacts/bd-linux-gms-pure + go test -tags "$BEADS_BUILD_TAGS" -c -o artifacts/bd-cmd-test ./cmd/bd + chmod +x artifacts/bd-cmd-test + ( + cd artifacts + sha256sum bd-linux-gms-pure > SHA256SUMS + ) + { + echo "commit=${commit}" + echo "go_version=${go_version}" + echo "build_tags=${BEADS_BUILD_TAGS}" + echo "artifact=bd-linux-gms-pure" + } > artifacts/build-manifest.txt + + - name: Upload build artifacts + uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7 + with: + name: ci-build-artifacts + path: artifacts/ + retention-days: 1 + if-no-files-found: error + + - name: Save Go module cache + if: steps.restore-go-module-cache.outputs.cache-hit != 'true' + uses: actions/cache/save@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 + with: + path: ~/go/pkg/mod + key: beads-go-mod-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-${{ hashFiles('go.mod', 'go.sum') }} + + - name: Save non-race Go build cache + if: steps.restore-non-race-go-build-cache.outputs.cache-hit != 'true' + uses: actions/cache/save@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 + with: + path: ${{ runner.temp }}/go-cache/non-race + key: beads-go-build-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-base-gms_pure_go-non-race-${{ github.sha }} + + # Fast check: every `go build|test|run|generate|install` invocation in + # tracked scripts/hooks/CI carries -tags=gms_pure_go. Prevents ICU-linkage + # regressions from re-entering the build (see engdocs/ICU-POLICY.md). + check-build-tags: + name: Check build-tag policy + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + - run: ./scripts/check-build-tags.sh + - run: ./scripts/check-go-install-guidance.sh + + # Lock in the cgo-test-contamination fix (mybd-ycx / GH#3683 follow-up). + # cmd/bd carries a mix of cgo-only test helpers (embedded Dolt, sql.DB) and + # pure-Go tests. Untagged test files that reach for cgo-only helpers break + # the entire package's CGO_ENABLED=0 compile, silently neutering pure-Go + # tests (the original symptom in PR #3704). This job compiles the cmd/bd + # test binary with CGO_ENABLED=0 and runs the pure-Go subset, so a + # regression that re-introduces cgo coupling fails fast. + check-cmd-bd-puregeo-tests: + name: Check cmd/bd pure-Go tests compile (CGO_ENABLED=0) + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + + - name: Set up Go + uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 + with: + go-version-file: 'go.mod' + cache: false + + - name: Build cmd/bd (CGO_ENABLED=0, gms_pure_go) + env: + CGO_ENABLED: "0" + run: go build -tags gms_pure_go -o /tmp/bd-puregeo ./cmd/bd + + - name: Compile pure-Go test binaries (CGO_ENABLED=0, gms_pure_go) + env: + CGO_ENABLED: "0" + run: | + go test -tags gms_pure_go -c -o /tmp/bd-cmd-puregeo-test ./cmd/bd + go test -tags gms_pure_go -c -o /tmp/bd-embeddeddolt-puregeo-test ./internal/storage/embeddeddolt + go test -tags gms_pure_go -c -o /tmp/bd-tracker-puregeo-test ./internal/tracker + + - name: Run pure-Go cmd/bd test subset (CGO_ENABLED=0) + env: + CGO_ENABLED: "0" + # Restrict to tests that exercise pure-Go code paths. Tests that + # need a Dolt store skip themselves at runtime when no test server + # is available; the goal here is to catch compile-time contamination, + # not run the full suite. + run: | + go test -tags gms_pure_go -count=1 -short -run '^Test(Help|CheckRemoteSafety|FormatDestroyToken|ShouldWireInitRemote|ExtractPrefix|IsNumericID|GetWorktreeGitDir|DriftItemStatuses|RunDriftChecks|IsServerProbablyRunning|CheckHooksDriftNotGitRepo|CheckServerDriftNoBeadsDir|CheckRemoteDriftNoBeadsDir)' ./cmd/bd + + # Fast check to ensure all version files are in sync + check-version-consistency: + name: Check version consistency + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + + - name: Check all versions match + run: ./scripts/check-versions.sh + + # Catch duplicate migration version numbers before any Go build. + # Two long-lived branches both claiming 'the next number' produce a silent + # under-apply when merged; this job fails at PR time with the conflicting + # filenames so it is caught before any test even compiles. + check-no-duplicate-migrations: + name: Check no duplicate migration versions + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + + - name: Check for duplicate migration version numbers + run: | + dup=$( + find internal/storage/schema/migrations -maxdepth 1 -name '*.up.sql' -printf '%f\n' \ + | sed 's/^\([0-9]*\)_.*/\1/' \ + | sort | uniq -d + ) + if [ -n "$dup" ]; then + echo "Duplicate migration version(s) detected: $dup" + echo "Conflicting files:" + while IFS= read -r v; do + find internal/storage/schema/migrations -name "${v}_*.up.sql" -print + done <<< "$dup" + echo "Renumber one of the colliding files before merging." + exit 1 + fi + + # Check documentation references match actual CLI flags + check-doc-flags: + name: Check doc flags freshness + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + + - name: Set up Go + uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 + with: + go-version-file: 'go.mod' + cache: false + + - name: Build bd + run: CGO_ENABLED=0 go build -tags gms_pure_go -o bd ./cmd/bd/ + + - name: Validate docs against CLI + run: | + ./scripts/check-doc-flags.sh ./bd + ./scripts/check-doc-freshness.sh + + detect-package-gates: + name: Detect package gates + runs-on: ubuntu-latest + outputs: + mcp_package: ${{ steps.detect.outputs.mcp_package }} + npm_package: ${{ steps.detect.outputs.npm_package }} + reason: ${{ steps.detect.outputs.reason }} + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + with: + fetch-depth: 0 + + - name: Decide package gates + id: detect + env: + PR_BASE_SHA: ${{ github.event.pull_request.base.sha }} + PR_HEAD_SHA: ${{ github.event.pull_request.head.sha }} + PUSH_BEFORE_SHA: ${{ github.event.before }} + PUSH_AFTER_SHA: ${{ github.sha }} + run: ./scripts/ci/detect-package-gates.sh + + package-mcp: + name: Package Gate (MCP) + runs-on: ubuntu-latest + needs: [detect-package-gates, build-artifacts] + steps: + - name: Check applicability + id: applicability + run: | + echo "run=${{ needs.detect-package-gates.outputs.mcp_package }}" >> "$GITHUB_OUTPUT" + if [[ "${{ needs.detect-package-gates.outputs.mcp_package }}" != "true" ]]; then + echo "MCP package gate is not applicable: ${{ needs.detect-package-gates.outputs.reason }}" + fi + + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + if: steps.applicability.outputs.run == 'true' + + - name: Set up Python + if: steps.applicability.outputs.run == 'true' + uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0 + with: + python-version: '3.14' + + - name: Install uv + if: steps.applicability.outputs.run == 'true' + run: python -m pip install uv==0.11.16 + + - name: Download build artifacts + if: steps.applicability.outputs.run == 'true' + uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8 + with: + name: ci-build-artifacts + path: ci-build-artifacts + + - name: Verify build artifacts + if: steps.applicability.outputs.run == 'true' + run: | + set -euo pipefail + cd ci-build-artifacts + sha256sum -c SHA256SUMS + chmod +x bd-linux-gms-pure + + - name: Run MCP package gate + if: steps.applicability.outputs.run == 'true' + env: + BEADS_TEST_BD_BINARY: ${{ github.workspace }}/ci-build-artifacts/bd-linux-gms-pure + run: make ci-package-mcp + + package-npm: + name: Package Gate (npm) + runs-on: ubuntu-latest + needs: [detect-package-gates, build-artifacts] + steps: + - name: Check applicability + id: applicability + run: | + echo "run=${{ needs.detect-package-gates.outputs.npm_package }}" >> "$GITHUB_OUTPUT" + if [[ "${{ needs.detect-package-gates.outputs.npm_package }}" != "true" ]]; then + echo "npm package gate is not applicable: ${{ needs.detect-package-gates.outputs.reason }}" + fi + + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + if: steps.applicability.outputs.run == 'true' + + - name: Set up Node.js + if: steps.applicability.outputs.run == 'true' + uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v6 + with: + node-version: '24' + + - name: Download build artifacts + if: steps.applicability.outputs.run == 'true' + uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8 + with: + name: ci-build-artifacts + path: ci-build-artifacts + + - name: Verify build artifacts + if: steps.applicability.outputs.run == 'true' + run: | + set -euo pipefail + cd ci-build-artifacts + sha256sum -c SHA256SUMS + chmod +x bd-linux-gms-pure + + - name: Run npm package gate + if: steps.applicability.outputs.run == 'true' + env: + BEADS_TEST_BD_BINARY: ${{ github.workspace }}/ci-build-artifacts/bd-linux-gms-pure + run: make ci-package-npm + + + pr-policy-wrapper: + name: PR Policy (wrapper timing) + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + with: + fetch-depth: 0 + + - name: Set up Go + uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 + with: + go-version-file: 'go.mod' + cache: false + + - name: Run PR policy wrapper + run: make ci-pr-policy + + pr-core-wrapper: + name: PR Core (wrapper timing) + runs-on: ubuntu-latest + needs: build-artifacts steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 @@ -43,9 +384,100 @@ jobs: go-version-file: 'go.mod' cache: false - - name: Compute cache date - id: cache-date - run: echo "today=$(date -u +%Y%m%d)" >> "$GITHUB_OUTPUT" + - name: Restore Go module cache + id: restore-go-module-cache + uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 + with: + path: ~/go/pkg/mod + key: beads-go-mod-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-${{ hashFiles('go.mod', 'go.sum') }} + restore-keys: beads-go-mod-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}- + + - name: Restore race Go build cache + id: restore-race-go-build-cache + uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 + with: + path: ${{ runner.temp }}/go-cache/race + key: beads-go-build-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-base-gms_pure_go-race-${{ github.sha }} + restore-keys: beads-go-build-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-base-gms_pure_go-race- + + - name: Install Dolt + run: ./scripts/ci/install-dolt.sh + + - name: Configure Git and Dolt identity + run: | + git config --global user.name "CI Bot" + git config --global user.email "ci@beads.test" + dolt config --global --add user.name "CI Bot" + dolt config --global --add user.email "ci@beads.test" + + - name: Download build artifacts + uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8 + with: + name: ci-build-artifacts + path: ci-build-artifacts + + - name: Verify build artifacts + run: | + set -euo pipefail + cd ci-build-artifacts + sha256sum -c SHA256SUMS + chmod +x bd-linux-gms-pure + + - name: Run PR core wrapper + env: + BEADS_TEST_BD_BINARY: ${{ github.workspace }}/ci-build-artifacts/bd-linux-gms-pure + GOCACHE: ${{ runner.temp }}/go-cache/race + run: make ci-pr-core + + pr-lint-wrapper: + name: PR Lint (wrapper timing) + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + + - name: Set up Go + uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 + with: + go-version-file: 'go.mod' + cache: false + + - name: Install golangci-lint + run: go install github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.10.1 + + # No BD_LINT_NEW_FROM_MERGE_BASE here: the push lane sweeps the whole + # tree, including the GOOS=windows cross-lint. pr.yml scopes the same + # wrapper to the PR's own diff. + - name: Run PR lint wrapper + run: make ci-pr-lint + + # Cross-platform test matrix (Linux/macOS only - Windows uses smoke tests) + test: + name: Test (${{ matrix.os }}) + if: github.event_name == 'push' && github.ref == 'refs/heads/main' + runs-on: ${{ matrix.os }} + needs: build-artifacts + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest, macos-latest] + include: + # Linux: full test suite with coverage + - os: ubuntu-latest + coverage: true + test-flags: '-v -race -short -coverprofile=coverage.out' + # macOS: full test suite, no coverage (faster) + - os: macos-latest + coverage: false + test-flags: '-v -race -short' + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + + - name: Set up Go + id: setup-go + uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 + with: + go-version-file: 'go.mod' + cache: false - name: Restore Go module cache id: restore-go-module-cache @@ -57,10 +489,11 @@ jobs: - name: Restore non-race Go build cache id: restore-non-race-go-build-cache + if: matrix.os == 'macos-latest' uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 with: path: ${{ runner.temp }}/go-cache/non-race - key: beads-go-build-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-base-gms_pure_go-non-race-${{ hashFiles('go.sum') }}-${{ steps.cache-date.outputs.today }} + key: beads-go-build-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-base-gms_pure_go-non-race-${{ github.sha }} restore-keys: beads-go-build-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-base-gms_pure_go-non-race- - name: Restore race Go build cache @@ -68,7 +501,7 @@ jobs: uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 with: path: ${{ runner.temp }}/go-cache/race - key: beads-go-build-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-base-gms_pure_go-race-${{ hashFiles('go.sum') }}-${{ steps.cache-date.outputs.today }} + key: beads-go-build-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-base-gms_pure_go-race-${{ github.sha }} restore-keys: beads-go-build-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-base-gms_pure_go-race- - name: Install Dolt (Linux/macOS) @@ -84,48 +517,407 @@ jobs: dolt config --global --add user.name "CI Bot" dolt config --global --add user.email "ci@beads.test" + - name: Install gotestsum + if: matrix.coverage + run: go install gotest.tools/gotestsum@v1.13.0 + + - name: Download build artifacts + if: matrix.os == 'ubuntu-latest' + uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8 + with: + name: ci-build-artifacts + path: ci-build-artifacts + + - name: Verify build artifacts + if: matrix.os == 'ubuntu-latest' + run: | + set -euo pipefail + cd ci-build-artifacts + sha256sum -c SHA256SUMS + chmod +x bd-linux-gms-pure + - name: Build + if: matrix.os != 'ubuntu-latest' env: GOCACHE: ${{ runner.temp }}/go-cache/non-race run: go build -v -tags gms_pure_go ./cmd/bd + # Both lanes below pass -timeout=30m explicitly. ./cmd/bd has outgrown + # `go test`'s 10m per-package default (see scripts/ci/pr-core.sh and + # #6091), and when it trips the alarm the panic names whichever test was + # in flight, so it reads like a hang rather than the package running out + # of clock. The flag lives on the invocation, not in matrix.test-flags, + # so editing the matrix cannot silently drop it. 30m matches every other + # -race lane here for this same code. + - name: Test (with coverage + JUnit XML) + if: matrix.coverage + env: + BEADS_TEST_BD_BINARY: ${{ github.workspace }}/ci-build-artifacts/bd-linux-gms-pure + GOCACHE: ${{ runner.temp }}/go-cache/race + run: | + gotestsum \ + --junitfile junit.xml \ + --format testdox \ + -- -tags gms_pure_go -race -short -timeout=30m -coverprofile=coverage.out -skip '^TestEmbedded' ./... + - name: Test + if: ${{ !matrix.coverage }} env: BEADS_TEST_BD_BINARY: ${{ github.workspace }}/bd GOCACHE: ${{ runner.temp }}/go-cache/race - run: go test -tags gms_pure_go -v -race -short -timeout=30m -skip '^TestEmbedded' ./... + run: go test -tags gms_pure_go ${{ matrix.test-flags }} -timeout=30m -skip '^TestEmbedded' ./... + + - name: Upload coverage to Codecov + if: matrix.coverage && !cancelled() + uses: codecov/codecov-action@fb8b3582c8e4def4969c97caa2f19720cb33a72f # v6 + continue-on-error: true + with: + files: coverage.out + token: ${{ secrets.CODECOV_TOKEN }} + fail_ci_if_error: false + + - name: Upload test results to Codecov + if: matrix.coverage && !cancelled() + uses: codecov/codecov-action@fb8b3582c8e4def4969c97caa2f19720cb33a72f # v6 + continue-on-error: true + with: + files: junit.xml + token: ${{ secrets.CODECOV_TOKEN }} + report_type: test_results + fail_ci_if_error: false + + # Only the macOS leg owns macOS generations. The Linux leg restores the + # Linux writers above and must never become a second cache publisher. + - name: Save Go module cache + if: ${{ !cancelled() && matrix.os == 'macos-latest' && steps.restore-go-module-cache.outputs.cache-hit != 'true' }} + uses: actions/cache/save@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 + with: + path: ~/go/pkg/mod + key: beads-go-mod-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-${{ hashFiles('go.mod', 'go.sum') }} + + - name: Save non-race Go build cache + if: ${{ !cancelled() && matrix.os == 'macos-latest' && steps.restore-non-race-go-build-cache.outputs.cache-hit != 'true' }} + uses: actions/cache/save@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 + with: + path: ${{ runner.temp }}/go-cache/non-race + key: beads-go-build-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-base-gms_pure_go-non-race-${{ github.sha }} - # Restore-only for the module and non-race caches (the macOS saver - # owns them); this job is the race cache's only reader and writer. - name: Save race Go build cache - if: ${{ !cancelled() && steps.restore-race-go-build-cache.outputs.cache-hit != 'true' }} + if: ${{ !cancelled() && matrix.os == 'macos-latest' && steps.restore-race-go-build-cache.outputs.cache-hit != 'true' }} uses: actions/cache/save@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 with: path: ${{ runner.temp }}/go-cache/race - key: beads-go-build-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-base-gms_pure_go-race-${{ hashFiles('go.sum') }}-${{ steps.cache-date.outputs.today }} + key: beads-go-build-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-base-gms_pure_go-race-${{ github.sha }} - # Windows smoke tests only - full test suite times out (see bd-bmev) - # Linux/macOS run comprehensive tests; Windows just verifies binary works + # Focused job for the hexagonal storage layer (internal/storage/domain/* and + # internal/storage/uow). These packages back the proxied-server init path and + # also live behind the existing ./... matrix, but TestDomainDB requires the + # Dolt sql-server image cached locally (testcontainers checks `docker image + # inspect` and skips otherwise). Pulling the image up front guarantees the + # suite actually runs and surfaces failures here with a clear job name. + test-domain-uow: + name: Test (storage domain + uow) + runs-on: ubuntu-latest + needs: build-artifacts + # The race-enabled storage package has outgrown Go's implicit 10-minute + # timeout. Keep both per-package deadlines inside one finite job budget. + timeout-minutes: 30 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + + - name: Set up Go + uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 + with: + go-version-file: 'go.mod' + cache: false + + - name: Install Dolt CLI + run: ./scripts/ci/install-dolt.sh + + - name: Verify dolt on PATH + run: dolt version + + - name: Configure Git and Dolt identity + run: | + git config --global user.name "CI Bot" + git config --global user.email "ci@beads.test" + dolt config --global --add user.name "CI Bot" + dolt config --global --add user.email "ci@beads.test" + + - name: Pull Dolt sql-server image + # Keep tag in sync with internal/testutil/testdoltcommon.go:DoltDockerImage. + # Without this, RequireDoltContainer reports doltNoImage and TestDomainDB skips. + run: ./scripts/ci/pull-dolt-image.sh + + - name: Download build artifacts + uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8 + with: + name: ci-build-artifacts + path: ci-build-artifacts + + - name: Verify build artifacts + run: | + set -euo pipefail + cd ci-build-artifacts + sha256sum -c SHA256SUMS + chmod +x bd-linux-gms-pure + + - name: Test domain + uow + tracker + env: + BEADS_TEST_BD_BINARY: ${{ github.workspace }}/ci-build-artifacts/bd-linux-gms-pure + run: go test -tags gms_pure_go -race -count=1 -timeout 15m -v ./internal/storage/domain/... ./internal/storage/uow/... ./internal/tracker/... + + # The doctor/fix DB-backed suite spins its own Dolt container in + # TestMain. BEADS_FIX_REQUIRE_DOLT=1 turns a missing container into a + # hard failure so the suite can never silently skip in CI again + # (bd-nxt5e: it was dark for months); this job pulls the image above. + - name: Test doctor/fix (Dolt-backed, hard-require container) + env: + BEADS_TEST_BD_BINARY: ${{ github.workspace }}/ci-build-artifacts/bd-linux-gms-pure + BEADS_FIX_REQUIRE_DOLT: "1" + run: go test -tags gms_pure_go -race -count=1 -timeout 10m -v ./cmd/bd/doctor/fix/ + + main-linux-integration-packages: + name: Main Linux integration packages (${{ matrix.shard }}/6) + if: github.event_name == 'push' && github.ref == 'refs/heads/main' + runs-on: ubuntu-latest + needs: build-artifacts + timeout-minutes: 30 + strategy: + fail-fast: false + matrix: + shard: [1, 2, 3, 4, 5, 6] + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + + - name: Set up Go + uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 + with: + go-version-file: 'go.mod' + cache: false + + - name: Download build artifacts + uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8 + with: + name: ci-build-artifacts + path: ci-build-artifacts + + - name: Verify build artifacts + run: | + set -euo pipefail + cd ci-build-artifacts + sha256sum -c SHA256SUMS + chmod +x bd-linux-gms-pure + + - name: Install Dolt + run: ./scripts/ci/install-dolt.sh + + - name: Configure Git and Dolt identity + run: | + git config --global user.name "CI Bot" + git config --global user.email "ci@beads.test" + dolt config --global --add user.name "CI Bot" + dolt config --global --add user.email "ci@beads.test" + + - name: Test package shard + env: + SHARD: ${{ matrix.shard }} + TOTAL_SHARDS: 6 + BEADS_TEST_BD_BINARY: ${{ github.workspace }}/ci-build-artifacts/bd-linux-gms-pure + run: | + set -euo pipefail + mkdir -p artifacts + source scripts/ci/lib/timing.sh + + ci_time "install gotestsum" -- go install gotest.tools/gotestsum@v1.13.0 + + package_manifest="artifacts/main-linux-integration-packages-${SHARD}-of-${TOTAL_SHARDS}.txt" + write_package_manifest() { + GO_TEST_SHARD_TAGS=integration,gms_pure_go \ + GO_TEST_SHARD_EXCLUDE_REGEX="^github.com/steveyegge/beads/cmd/bd$" \ + scripts/ci/go-test-shard-packages.sh "$SHARD" "$TOTAL_SHARDS" > "$package_manifest" + } + ci_time "list integration package shard ${SHARD}/${TOTAL_SHARDS}" -- write_package_manifest + cat "$package_manifest" + mapfile -t packages < "$package_manifest" + + if [[ "${#packages[@]}" -eq 0 ]]; then + echo "No packages assigned to shard ${SHARD}/${TOTAL_SHARDS}; skipping go test" + exit 0 + fi + + ci_time "main linux integration package shard ${SHARD}/${TOTAL_SHARDS} go test" -- \ + env BEADS_TEST_SKIP=dolt gotestsum \ + --junitfile "artifacts/main-linux-integration-packages-${SHARD}-of-${TOTAL_SHARDS}.xml" \ + --format testdox \ + -- -v -race -tags=integration,gms_pure_go -timeout=30m "${packages[@]}" + + - name: Upload integration artifacts + if: always() + uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7 + with: + name: main-linux-integration-packages-${{ matrix.shard }}-of-6 + path: artifacts/ + retention-days: 7 + if-no-files-found: ignore + + main-linux-integration-cmd-bd: + name: Main Linux integration cmd/bd (${{ matrix.shard }}/8) + if: github.event_name == 'push' && github.ref == 'refs/heads/main' + runs-on: ubuntu-latest + needs: build-artifacts + timeout-minutes: 30 + strategy: + fail-fast: false + matrix: + shard: [1, 2, 3, 4, 5, 6, 7, 8] + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + + - name: Set up Go + uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 + with: + go-version-file: 'go.mod' + cache: false + + - name: Download build artifacts + uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8 + with: + name: ci-build-artifacts + path: ci-build-artifacts + + - name: Verify build artifacts + run: | + set -euo pipefail + cd ci-build-artifacts + sha256sum -c SHA256SUMS + chmod +x bd-linux-gms-pure + + - name: Install Dolt + run: ./scripts/ci/install-dolt.sh + + - name: Configure Git and Dolt identity + run: | + git config --global user.name "CI Bot" + git config --global user.email "ci@beads.test" + dolt config --global --add user.name "CI Bot" + dolt config --global --add user.email "ci@beads.test" + + - name: Test cmd/bd shard + env: + SHARD: ${{ matrix.shard }} + TOTAL_SHARDS: 8 + BEADS_TEST_BD_BINARY: ${{ github.workspace }}/ci-build-artifacts/bd-linux-gms-pure + run: | + set -euo pipefail + mkdir -p artifacts + source scripts/ci/lib/timing.sh + + ci_time "install gotestsum" -- go install gotest.tools/gotestsum@v1.13.0 + + test_manifest="artifacts/main-linux-integration-cmd-bd-tests-${SHARD}-of-${TOTAL_SHARDS}.txt" + write_test_manifest() { + GO_TEST_SHARD_TAGS=integration,gms_pure_go \ + scripts/ci/go-test-shard-names.sh "$SHARD" "$TOTAL_SHARDS" ./cmd/bd > "$test_manifest" + } + ci_time "list cmd/bd test shard ${SHARD}/${TOTAL_SHARDS}" -- write_test_manifest + cat "$test_manifest" + mapfile -t tests < "$test_manifest" + + if [[ "${#tests[@]}" -eq 0 ]]; then + echo "No tests assigned to shard ${SHARD}/${TOTAL_SHARDS}; skipping go test" + exit 0 + fi + + run_regex="^($(IFS='|'; echo "${tests[*]}"))$" + ci_time "main linux integration cmd/bd shard ${SHARD}/${TOTAL_SHARDS} go test" -- \ + env BEADS_TEST_SKIP=dolt gotestsum \ + --junitfile "artifacts/main-linux-integration-cmd-bd-${SHARD}-of-${TOTAL_SHARDS}.xml" \ + --format testdox \ + -- -v -race -tags=integration,gms_pure_go -timeout=30m -run "$run_regex" ./cmd/bd + + - name: Upload integration artifacts + if: always() + uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7 + with: + name: main-linux-integration-cmd-bd-${{ matrix.shard }}-of-8 + path: artifacts/ + retention-days: 7 + if-no-files-found: ignore + + # End-to-end coverage for proxied-server CLI subcommands, sharded across the + # CI matrix. Each sub-test drives the bd CLI subprocess against a real dolt + # sql-server started via testcontainers by the shared proxied harness + # (cmd/bd/*_proxied_integration_test.go). # - # Windows uses -tags gms_pure_go to avoid the ICU regex dependency. - # go-icu-regex's regex_windows.go also uses Go stdlib regexp (build tag: windows). - # CGO is still required for gozstd (bundled C source) and the embedded dolt engine. - # pr.yml's Windows jobs (test-windows-liveness, worktree-remove-windows, - # etc.) run on Blacksmith Windows for every PR, forks included - # (ga-96smfk.22), so this job seeds their module and non-race GOCACHEs on - # that one runner selection: Blacksmith runners cannot see caches - # `actions/cache` saved from a GitHub-hosted runner, and no GitHub-hosted - # Windows consumer is left. - test-windows: - name: Test (Windows - smoke) - # F7b review fix (B2): push-to-main, same-repo only, like every saver - # here (the repository check closes the fork gap). Pinned by - # TestBlacksmithSaverJobsGuardedAgainstPullRequest. - if: github.event_name == 'push' && github.ref == 'refs/heads/main' && github.repository == 'gastownhall/beads' - runs-on: ${{ 'blacksmith-4vcpu-windows-2025' }} - # F7b review fix (S8): paid Blacksmith Windows (4x billing). 20 matches - # test-windows-liveness's timeout (S6) for consistency. + # Reuses the cmd/bd test binary compiled once by build-artifacts (bd-cmd-test): + # the proxied tests share the package and are gated at runtime by + # BEADS_TEST_PROXIED_SERVER=1. Shard assignment is committed in + # .github/scripts/proxied-cmd-test-shards.txt; newly-added TestProxiedServer* + # tests hash-distribute until listed there. Adopt the + # `TestProxiedServer` naming convention for new entry points. + test-proxied-cmd: + name: Test (proxied-server cmd ${{ matrix.shard }}/15) + if: github.event_name == 'push' && github.ref == 'refs/heads/main' + runs-on: ubuntu-latest + needs: build-artifacts timeout-minutes: 20 + strategy: + fail-fast: false + matrix: + shard: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + + - name: Download build artifacts + uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8 + with: + name: ci-build-artifacts + path: ci-build-artifacts + + - name: Verify build artifacts + run: | + set -euo pipefail + cd ci-build-artifacts + sha256sum -c SHA256SUMS + chmod +x bd-linux-gms-pure bd-cmd-test + + - name: Install Dolt CLI + # TestProxiedServerExternalCreate needs the dolt binary on PATH; the + # shared harness itself uses the dolt sql-server container image. + run: ./scripts/ci/install-dolt.sh + + - name: Configure Git and Dolt identity + run: | + git config --global user.name "CI Bot" + git config --global user.email "ci@beads.test" + dolt config --global --add user.name "CI Bot" + dolt config --global --add user.email "ci@beads.test" + + - name: Pull Dolt sql-server image + # The shared proxied harness requires the image cached locally + # (isDoltImageCached); without it every proxied test self-skips. Keep the + # tag in sync with internal/testutil/testdoltcommon.go:DoltDockerImage. + run: ./scripts/ci/pull-dolt-image.sh + + - name: Test proxied-server cmd shard + env: + BEADS_TEST_PROXIED_SERVER: "1" + BEADS_TEST_BD_BINARY: ${{ github.workspace }}/ci-build-artifacts/bd-linux-gms-pure + BEADS_TEST_CMD_BINARY: ${{ github.workspace }}/ci-build-artifacts/bd-cmd-test + run: bash .github/scripts/proxied-test-shard.sh ${{ matrix.shard }} 15 + + # Embedded Dolt tests - test the in-process Dolt engine (no server required) + # + # Split into: build → storage tests + parallel cmd test shards. + # cmd/bd embedded tests use a committed shard manifest; newly-added tests + # still auto-distribute via hash fallback until the manifest is updated. + + build-embedded: + name: Build (Embedded Dolt) + needs: detect-ci-tier + if: needs.detect-ci-tier.outputs.full_embedded == 'true' + runs-on: ubuntu-latest steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 @@ -136,19 +928,186 @@ jobs: go-version-file: 'go.mod' cache: false - # F7b review fix (S7/Speed): cache keys are bucketed by go.sum and UTC - # day, not per commit, so ordinary push traffic writes at most one - # entry per day (TestBlacksmithSaverCacheKeysAreNotPerCommit). - - name: Compute cache date - id: cache-date - shell: bash - run: echo "today=$(date -u +%Y%m%d)" >> "$GITHUB_OUTPUT" + - name: Restore Go module cache + id: restore-go-module-cache + uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 + with: + path: ~/go/pkg/mod + key: beads-go-mod-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-${{ hashFiles('go.mod', 'go.sum') }} + restore-keys: beads-go-mod-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}- - # F7b review fix (S9): same Windows toolchain diagnostic as pr.yml's - # Windows jobs. - - name: Check Windows toolchain diagnostics - shell: bash - run: go env CGO_ENABLED CC GOOS GOARCH + - name: Restore race Go build cache + id: restore-race-go-build-cache + uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 + with: + path: ${{ runner.temp }}/go-cache/race + key: beads-go-build-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-base-gms_pure_go-race-${{ github.sha }} + restore-keys: beads-go-build-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-base-gms_pure_go-race- + + - name: Build embedded bd binary + env: + GOCACHE: ${{ runner.temp }}/go-cache/race + run: go build -tags gms_pure_go -race -o /tmp/bd-embedded-test ./cmd/bd/ + + - name: Build embedded storage test binary + env: + GOCACHE: ${{ runner.temp }}/go-cache/race + run: go test -tags gms_pure_go -race -c -o /tmp/embeddeddolt-test ./internal/storage/embeddeddolt/ + + - name: Build embedded cmd test binary + env: + GOCACHE: ${{ runner.temp }}/go-cache/race + run: go test -tags gms_pure_go -race -c -o /tmp/bd-cmd-test ./cmd/bd/ + + - name: Upload binaries + uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7 + with: + name: embedded-test-binaries + path: | + /tmp/bd-embedded-test + /tmp/embeddeddolt-test + /tmp/bd-cmd-test + retention-days: 1 + + - name: Save race Go build cache + if: steps.restore-race-go-build-cache.outputs.cache-hit != 'true' + uses: actions/cache/save@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 + with: + path: ${{ runner.temp }}/go-cache/race + key: beads-go-build-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-base-gms_pure_go-race-${{ github.sha }} + + test-embedded-storage: + name: Test (Embedded Dolt Storage ${{ matrix.shard }}/${{ strategy.job-total }}) + needs: [detect-ci-tier, build-embedded] + if: needs.detect-ci-tier.outputs.full_embedded == 'true' + runs-on: ubuntu-latest + timeout-minutes: 15 + strategy: + fail-fast: false + matrix: + shard: [1, 2, 3, 4, 5] + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + + - name: Download binaries + uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8 + with: + name: embedded-test-binaries + path: /tmp/ + + - name: Fix permissions + run: chmod +x /tmp/embeddeddolt-test + + - name: Configure Git + run: | + git config --global user.name "CI Bot" + git config --global user.email "ci@beads.test" + + - name: Test + env: + BEADS_TEST_EMBEDDED_DOLT: "1" + BEADS_TEST_EMBEDDED_TEST_BINARY: /tmp/embeddeddolt-test + # Manifest-driven shard of the storage suite (mirrors test-embedded-cmd + # / embedded-test-shard.sh). Conformance runs separately in the + # dedicated test-embedded-conformance job below (mirrors pr-risk.yml); + # embedded-storage-test-shard.sh excludes TestConformance from + # discovery, so it never lands in one of these shards. + run: bash .github/scripts/embedded-storage-test-shard.sh ${{ matrix.shard }} 5 + + test-embedded-conformance: + name: Test (Embedded Dolt Conformance - ${{ matrix.partition }}) + needs: [detect-ci-tier, build-embedded] + if: needs.detect-ci-tier.outputs.full_embedded == 'true' + runs-on: ubuntu-latest + timeout-minutes: 35 + strategy: + fail-fast: false + matrix: + partition: [core, audit] + env: + BEADS_TEST_EMBEDDED_DOLT: "1" + BEADS_TEST_EMBEDDED_TEST_BINARY: /tmp/embeddeddolt-test + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + + - name: Download binaries + uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8 + with: + name: embedded-test-binaries + path: /tmp/ + + - name: Fix permissions + run: chmod +x /tmp/embeddeddolt-test + + - name: Configure Git + run: | + git config --global user.name "CI Bot" + git config --global user.email "ci@beads.test" + + - name: Test core conformance + if: matrix.partition == 'core' + # RunAll has a natural Audit seam. Run everything except Audit in the + # core shard and Audit alone in the other shard. The combined suite has + # exceeded 30m under runner variance; each half currently takes ~15m. + # Keep the Go timeout below the job timeout so a real stall still emits + # a diagnostic goroutine dump before Actions stops the job. + run: /tmp/embeddeddolt-test -test.v -test.count=1 -test.timeout=30m -test.run '^TestConformance$' -test.skip '^TestConformance$/^Audit$' + + - name: Test audit conformance + if: matrix.partition == 'audit' + run: /tmp/embeddeddolt-test -test.v -test.count=1 -test.timeout=30m -test.run '^TestConformance$/^Audit$' + + test-embedded-cmd: + name: Test (Embedded Dolt Cmd ${{ matrix.shard }}/${{ strategy.job-total }}) + needs: [detect-ci-tier, build-embedded] + if: needs.detect-ci-tier.outputs.full_embedded == 'true' + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + shard: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + + - name: Download binaries + uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8 + with: + name: embedded-test-binaries + path: /tmp/ + + - name: Fix permissions + run: chmod +x /tmp/bd-embedded-test /tmp/embeddeddolt-test /tmp/bd-cmd-test + + - name: Configure Git + run: | + git config --global user.name "CI Bot" + git config --global user.email "ci@beads.test" + + - name: Test + env: + BEADS_TEST_EMBEDDED_DOLT: "1" + BEADS_TEST_BD_BINARY: /tmp/bd-embedded-test + run: bash .github/scripts/embedded-test-shard.sh ${{ matrix.shard }} 20 + + # Windows smoke tests only - full test suite times out (see bd-bmev) + # Linux/macOS run comprehensive tests; Windows just verifies binary works + # + # Windows uses -tags gms_pure_go to avoid the ICU regex dependency. + # go-icu-regex's regex_windows.go also uses Go stdlib regexp (build tag: windows). + # CGO is still required for gozstd (bundled C source) and the embedded dolt engine. + test-windows: + name: Test (Windows - smoke) + if: github.event_name == 'push' && github.ref == 'refs/heads/main' + runs-on: windows-latest + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + + - name: Set up Go + id: setup-go + uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 + with: + go-version-file: 'go.mod' + cache: false - name: Restore Go module cache id: restore-go-module-cache @@ -163,7 +1122,7 @@ jobs: uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 with: path: ${{ runner.temp }}/go-cache/non-race - key: beads-go-build-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-base-gms_pure_go-non-race-${{ hashFiles('go.sum') }}-${{ steps.cache-date.outputs.today }} + key: beads-go-build-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-base-gms_pure_go-non-race-${{ github.sha }} restore-keys: beads-go-build-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-base-gms_pure_go-non-race- - name: Configure Git @@ -182,19 +1141,6 @@ jobs: - name: Smoke test - help run: ./bd.exe help - # F7b review fix (S2): warms the SAME non-race GOCACHE namespace that - # pr-preflight-platforms'/worktree-remove-windows'/test-windows-liveness' - # Windows legs restore from, on the exact same runner selection - # (blacksmith-4vcpu-windows-2025), so their cold `cmd/bd` test compiles - # (the real long pole; see pr-preflight-platforms' S2 comment) get a - # true cache hit. Confirmed to run under Git Bash on Windows (reviewer - # verification). - - name: Warm non-race GOCACHE (Windows preflight/liveness consumers) - shell: bash - env: - GOCACHE: ${{ runner.temp }}/go-cache/non-race - run: bash scripts/ci/warm-non-race-cache.sh - - name: Save Go module cache if: steps.restore-go-module-cache.outputs.cache-hit != 'true' uses: actions/cache/save@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 @@ -207,84 +1153,66 @@ jobs: uses: actions/cache/save@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 with: path: ${{ runner.temp }}/go-cache/non-race - key: beads-go-build-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-base-gms_pure_go-non-race-${{ hashFiles('go.sum') }}-${{ steps.cache-date.outputs.today }} + key: beads-go-build-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-base-gms_pure_go-non-race-${{ github.sha }} - # macOS saver: pr.yml's pr-preflight-platforms and - # check-doc-freshness-platforms macOS legs run on blacksmith-6vcpu-macos-26 - # for every PR, forks included (ga-96smfk.22). This job is their only - # seeder (and also seeds the `test` job's module and non-race caches), and runs only the shared warm-up those legs compile - # (warm-non-race-cache.sh, the same script the Windows seeder uses) - # plus the non-test ./cmd/bd build -- no tests, so it stays a few minutes of - # macOS time per push. Same push-to-main-only job guard (B2) and go.sum + - # UTC-day cache keys (S7) as test-windows, so a PR can never - # write this namespace and ordinary push traffic writes at most one - # non-race entry per day. Pinned by - # TestBlacksmithSaverJobsGuardedAgainstPullRequest, - # TestBlacksmithSaverCacheKeysAreNotPerCommit and - # TestBlacksmithMacOSSaverMatchesPRLegs. - blacksmith-macos-go-build-cache: - name: Warm macOS Go build cache - runs-on: ${{ 'blacksmith-6vcpu-macos-26' }} - if: github.event_name == 'push' && github.ref == 'refs/heads/main' && github.repository == 'gastownhall/beads' - timeout-minutes: 20 + fmt-check: + name: Check formatting + runs-on: ubuntu-latest steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 - name: Set up Go - id: setup-go uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 with: go-version-file: 'go.mod' cache: false - - name: Compute cache date - id: cache-date - run: echo "today=$(date -u +%Y%m%d)" >> "$GITHUB_OUTPUT" + - name: Check gofmt + run: make fmt-check - - name: Restore Go module cache - id: restore-go-module-cache - uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 + # The whole-tree lint sweep. pr.yml's job of the same name reports only what + # a PR introduces, so this push lane is where a pre-existing or newly landed + # issue anywhere in the tree surfaces. + lint: + name: Lint + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + + - name: Set up Go + uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 with: - path: ~/go/pkg/mod - key: beads-go-mod-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-${{ hashFiles('go.mod', 'go.sum') }} - restore-keys: beads-go-mod-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}- + go-version-file: 'go.mod' + cache: false - - name: Restore non-race Go build cache - id: restore-non-race-go-build-cache - uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 + - name: golangci-lint + uses: golangci/golangci-lint-action@ba0d7d2ec06a0ea1cb5fa41b2e4a3ab91d21278a # v9 + env: + GOWORK: "off" with: - path: ${{ runner.temp }}/go-cache/non-race - key: beads-go-build-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-base-gms_pure_go-non-race-${{ hashFiles('go.sum') }}-${{ steps.cache-date.outputs.today }} - restore-keys: beads-go-build-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-base-gms_pure_go-non-race- + version: v2.10.1 + args: --config=.golangci.yml --modules-download-mode=readonly --timeout=5m --build-tags=gms_pure_go - # Records the toolchain in the log so an image drift is visible next - # to the cache it seeds. - - name: Check macOS toolchain diagnostics + test-nix: + name: Test Nix Flake + runs-on: ubuntu-latest + timeout-minutes: 10 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + - uses: DeterminateSystems/determinate-nix-action@61cbfe2efc2d4e7a8a6d56967c3c1058e846c858 # v3 + - name: Run bd help via Nix + id: nix_help run: | - sw_vers - go env GOOS GOARCH CGO_ENABLED CC - bash --version | head -n 1 - - - name: Build - env: - GOCACHE: ${{ runner.temp }}/go-cache/non-race - run: go build -tags gms_pure_go -o /dev/null ./cmd/bd - - - name: Warm non-race GOCACHE for macOS preflight/doc-freshness - env: - GOCACHE: ${{ runner.temp }}/go-cache/non-race - run: bash scripts/ci/warm-non-race-cache.sh - - - name: Save Go module cache - if: ${{ !cancelled() && steps.restore-go-module-cache.outputs.cache-hit != 'true' }} - uses: actions/cache/save@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 - with: - path: ~/go/pkg/mod - key: beads-go-mod-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-${{ hashFiles('go.mod', 'go.sum') }} - - - name: Save non-race Go build cache - if: ${{ !cancelled() && steps.restore-non-race-go-build-cache.outputs.cache-hit != 'true' }} - uses: actions/cache/save@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 - with: - path: ${{ runner.temp }}/go-cache/non-race - key: beads-go-build-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-base-gms_pure_go-non-race-${{ hashFiles('go.sum') }}-${{ steps.cache-date.outputs.today }} \ No newline at end of file + nix run .#default -- --help > help.txt + - name: Verify help text + if: steps.nix_help.outcome == 'success' + run: | + FIRST_LINE=$(head -n 1 help.txt) + EXPECTED="Issues chained together like beads. A lightweight issue tracker with first-class dependency support." + if [ "$FIRST_LINE" != "$EXPECTED" ]; then + echo "First line of help.txt doesn't match expected output" + echo "Expected: $EXPECTED" + echo "Got: $FIRST_LINE" + exit 1 + fi + echo "Help text first line is correct" diff --git a/.github/workflows/migration-test.yml b/.github/workflows/migration-test.yml new file mode 100644 index 000000000..861960006 --- /dev/null +++ b/.github/workflows/migration-test.yml @@ -0,0 +1,150 @@ +name: Migration Test Harness + +on: + pull_request: + paths-ignore: + - '**/*.md' + - 'docs/**' + - 'website/**' + push: + tags: ['v*'] + workflow_dispatch: + +permissions: + contents: read + +env: + BD_DISABLE_METRICS: "1" + BD_DISABLE_EVENT_FLUSH: "1" + DOLT_TEST_RUNTIME_VERSION: v2.1.8 + DOLT_TEST_RUNTIME_SHA256: f66318f08ed66e409fc39363ae0fff8ce6fbf6dba9f5bac632b91527b9632a74 + +jobs: + historical-upgrades: + name: Historical ${{ matrix.version }} → candidate + # v0.55.4 is dynamically linked against the ICU version shipped here. + # Pin the image so a runner alias update cannot invalidate the fixture. + runs-on: ubuntu-24.04 + timeout-minutes: 15 + strategy: + fail-fast: false + matrix: + include: + - version: v0.9.1 + cache-identity: v0.9.1-source-e3c8554fa2c3e4b9caf7e296e9c8abbe24211a72-go1.25.0-cgo1 + - version: v0.17.0 + cache-identity: v0.17.0-d4d08617a324c85b45c9628bc519d659a9ff9c7c37da67aa48727e0af7f19a75 + - version: v0.49.6 + cache-identity: v0.49.6-8546dc9a47e11dc31ac2bc9a0224a9c690975e91850932cbb62623053fbb7db8 + - version: v0.50.3 + cache-identity: v0.50.3-e94b09e0b6a9324bbc0e81ea36bccaaa42172a926bfedfb389e9a26dedb63184 + - version: v0.55.4 + cache-identity: v0.55.4-e0fa25456dd82890230eef17653448a0bf995104c78864be91c5ed84426a5f49 + - version: v0.56.1 + cache-identity: v0.56.1-4f9f6cc44465a11613ff529009901eaaf841c6b1f91c15e002b0ecda2015a15c + - version: v0.57.0 + cache-identity: v0.57.0-f8629d5627bed7d25f06f92334addc171d679f9aed9d08c5d42a9684205dc04b + - version: v0.62.0 + cache-identity: v0.62.0-4cca7265b22e5c3ca8d62ab0b9752bec31f68b7f5fa636282a4c7e5454c35535 + - version: v0.63.3 + cache-identity: v0.63.3-5f4efd2e010209b3f381dbcd783b2a3a652f50ea72f40ef04c8ba434d408bf9e + - version: v1.0.0 + cache-identity: v1.0.0-7057db1e92428fcf5c08d5dc6b07ead57e588b262cba78b9a26893d55bd29fdb + - version: v1.0.1 + cache-identity: v1.0.1-1d2364d5d7083a4634a9e734ca87822fb79c2b6625988f9f791e3376313b1b77 + - version: v1.1.0 + cache-identity: v1.1.0-b0f3dd607c3fb989ee08d0a6854fba80d0402971eb108f9af6170bc14d491a34 + - version: v1.1.2 + cache-identity: v1.1.2-a72d71ed374955dc9f83a0f90b54bd7b6a0016709dd1676ae2e368651ed401c2 + # The v1.1.2 tree re-tagged by the recovery release, so it adds no new + # storage regime — but it is the tag field upgrades actually start + # from, and it was never in this corpus. + - version: v1.2.2 + cache-identity: v1.2.2-8140098a51d3b81d5548d1c5e6db1a2d9930e5d141efe2a4bff7d079c4d321e8 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + + - name: Set up Go + uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 + with: + go-version-file: 'go.mod' + + - name: Install test dependencies + # Backstop for a hang that escapes the inner `timeout` calls, so it has + # to sit ABOVE what those calls can legitimately consume: 3 update + # attempts x 120s + 2 backoffs x 10s + 180s install = 560s worst case. + # 11m leaves ~100s of headroom and stays under the job's 15m budget, so + # a stuck step fails as a step instead of cancelling the whole job. + timeout-minutes: 11 + run: | + updated="" + for attempt in 1 2 3; do + if sudo timeout 120 apt-get \ + -o Acquire::Retries=3 \ + -o Acquire::http::Timeout=30 \ + -o Acquire::https::Timeout=30 \ + update; then + updated=yes + break + fi + echo "::warning::apt-get update attempt $attempt failed or timed out" + # No backoff after the final attempt - there is nothing left to wait for. + if [ "$attempt" -lt 3 ]; then + sleep 10 + fi + done + # Deliberate best-effort: a stale index still installs these packages + # from the runner image cache far more often than not, and failing the + # job here would turn a transient mirror outage into a red build. The + # warning is what makes the degraded path visible in the job log. + if [ -z "$updated" ]; then + echo "::warning::apt-get update failed after 3 attempts; proceeding with cached index" + fi + sudo timeout 180 apt-get install -y jq libicu74 + + - name: Install Dolt test runtime + # v0.55.4-v0.62.0 host their fixture on this server; v1.0.1-v1.1.2 use + # the same pinned binary as a read-only oracle over the wisp plane, + # which has no `bd dolt sql` equivalent. + if: contains(fromJSON('["v0.55.4","v0.56.1","v0.57.0","v0.62.0","v1.0.1","v1.1.0","v1.1.2","v1.2.2"]'), matrix.version) + run: | + archive="$RUNNER_TEMP/dolt-linux-amd64.tar.gz" + runtime="$RUNNER_TEMP/dolt-runtime" + install_root="$GITHUB_WORKSPACE/.tools/dolt" + curl -fsSL --retry 3 --retry-all-errors --retry-delay 2 \ + "https://github.com/dolthub/dolt/releases/download/${DOLT_TEST_RUNTIME_VERSION}/dolt-linux-amd64.tar.gz" \ + -o "$archive" + echo "${DOLT_TEST_RUNTIME_SHA256} $archive" | sha256sum -c - + mkdir -p "$runtime" "$install_root/bin" + tar -xzf "$archive" -C "$runtime" + install -m 0755 "$runtime/dolt-linux-amd64/bin/dolt" "$install_root/bin/dolt" + "$install_root/bin/dolt" version + + - name: Configure Git + run: | + git config --global user.name "CI Bot" + git config --global user.email "ci@beads.test" + + - name: Cache pinned historical release + uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 + with: + path: ~/.cache/beads-regression + key: historical-dolt-${{ matrix.cache-identity }}-${{ runner.os }}-${{ runner.arch }} + + - name: Build candidate + run: make build + + - name: Set up Go 1.25 for v0.9.1 source build + if: matrix.version == 'v0.9.1' + uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 + with: + go-version: '1.25.0' + + - name: Verify explicit historical upgrade + env: + CANDIDATE_BIN: ./bd + GIT_CONFIG_NOSYSTEM: "1" + DOLT_BIN: ${{ github.workspace }}/.tools/dolt/bin/dolt + HISTORICAL_VERSION: ${{ matrix.version }} + run: | + ./scripts/migration-test/run.sh --version "$HISTORICAL_VERSION" diff --git a/.github/workflows/nightly.yml b/.github/workflows/nightly.yml index 7bd190a2d..86a13223d 100644 --- a/.github/workflows/nightly.yml +++ b/.github/workflows/nightly.yml @@ -1,91 +1,104 @@ -name: Nightly +name: Nightly Full Tests -# Every nightly test runs under Bazel on rbe-west: the `bazel` call below is -# the full suite with no cached test results. Nothing here runs go test on a -# runner (ga-96smfk.22). on: schedule: # Run at 2am UTC daily - cron: '0 2 * * *' workflow_dispatch: - -permissions: - contents: read + inputs: + suite: + description: "Nightly suite or measurement suite" + required: true + type: choice + default: full-test + options: + - full-test + - pr-linux + - macos-short + - macos-candidates + - linux-integration + - linux-integration-sharded + - linux-integration-hybrid-sharded + - linux-integration-hybrid-16-sharded + - linux-integration-hybrid-prebuilt-sharded + - linux-integration-coverage + - cross-version-smoke + - nix + - mcp-package + - npm-package + smoke_version: + description: "Optional previous release for cross-version-smoke" + required: false + default: "" env: BD_DISABLE_METRICS: "1" BD_DISABLE_EVENT_FLUSH: "1" jobs: - # Bazel lane with the full equivalence listing (package and test-name sets; - # PRs gate on it through pr.yml). Remote execution when the repo secrets - # are present; bazel.yml gets the four RBE secrets plus rbe-prewarm's - # bazel-allocator app private key (same-repo/scheduled trigger only, so - # pre-warming is safe here - see bazel.yml's "rbe-west Pre-warm" header and - # engdocs/CI_REQUIRED_CHECK_TOPOLOGY.md). - bazel: - name: Bazel (fresh, full detail) - # A called workflow's job gets at most this call's token permissions, and - # GitHub checks them when the run starts, even for a job whose if: skips - # it (startup_failure otherwise; gascity #7478): bazel.yml's rrc-seed asks - # id-token: write (push to main only) and rrc-verify issues: write - # (schedule and dispatch only). rrc-verify runs here (its alert issue); - # rrc-seed never does, every other bazel.yml job keeps contents: read, - # and rbe-west's mint refuses any OIDC token but a main-push bazel.yml one - # (scripts/bazel_rrc_test.go, TestBazelWorkflowCallersGrantEveryRequestedPermission). - permissions: - contents: read - id-token: write - issues: write - uses: ./.github/workflows/bazel.yml - secrets: - RBE_WEST_EXECUTOR: ${{ secrets.RBE_WEST_EXECUTOR }} - RBE_TLS_CERT: ${{ secrets.RBE_TLS_CERT }} - RBE_TLS_KEY: ${{ secrets.RBE_TLS_KEY }} - RBE_TLS_CA: ${{ secrets.RBE_TLS_CA }} - RBE_POOL_APP_PRIVATE_KEY: ${{ secrets.RBE_POOL_APP_PRIVATE_KEY }} + measure: + name: Measure selected CI suite + if: ${{ github.event_name == 'workflow_dispatch' && inputs.suite != 'full-test' }} + uses: ./.github/workflows/ci-measurements.yml with: - details: true - # The one run that re-executes every test (no cached test results): - # PRs, merge groups and pushes to main reuse unchanged tests' PASSes, - # so this keeps sampling flaky and -race failures every night. - fresh-test-results: true + suite: ${{ inputs.suite }} + smoke_version: ${{ inputs.smoke_version }} - # rbe-west's pool workers are gascity's and advertise the sha256 of - # gascity's worker-env manifest; beads' tools/rbe/worker-env.txt is a - # byte-for-byte copy and platforms/BUILD.bazel carries the same pin. A copy - # that falls behind queues every remote action (bazel.yml's rbe job runs - # the same check before each remote run and fails it fast), so check every - # night and, out of step, open or update a ci/infra issue with the diff and - # the pin to commit (tools/rbe/worker-env-sync-issue). The only job here - # with issues: write, and only its last step uses the token. gascity - # unreachable (exit 2) fails the job without an issue. - rbe-worker-env-sync: - name: rbe worker-env in step with gascity + full-test: + name: Full Test Suite + if: ${{ github.event_name == 'schedule' || inputs.suite == 'full-test' }} runs-on: ubuntu-latest - timeout-minutes: 5 - permissions: - contents: read - issues: write + timeout-minutes: 45 steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + + - name: Set up Go + uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 with: - persist-credentials: false + go-version-file: 'go.mod' - - name: Compare the worker-env manifest and pin with gascity's main - id: sync - env: - WORKER_ENV_SYNC_REPORT: ${{ runner.temp }}/worker-env-sync.md + - name: Install Dolt run: | - rc=0 - tools/rbe/worker-env-sync main || rc=$? - echo "rc=$rc" >> "$GITHUB_OUTPUT" - exit "$rc" + ./scripts/ci/install-dolt.sh - - name: Open or update the out-of-step issue - if: ${{ failure() && steps.sync.outputs.rc == '1' }} + - name: Configure Git and Dolt identity + run: | + git config --global user.name "CI Bot" + git config --global user.email "ci@beads.test" + dolt config --global --add user.name "CI Bot" + dolt config --global --add user.email "ci@beads.test" + + - name: Build + run: go build -v -tags gms_pure_go ./cmd/bd + + - name: Full Test Suite (including integration tests) env: - GH_TOKEN: ${{ github.token }} - REPORT: ${{ runner.temp }}/worker-env-sync.md - RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} - run: tools/rbe/worker-env-sync-issue "$REPORT" "$RUN_URL" + # Skip Docker-based Dolt testcontainer tests — they hang in + # GitHub Actions (known issue, see scripts/repro-dolt-hang/). + # Non-Docker integration tests still run via the installed dolt binary. + BEADS_TEST_SKIP: dolt + run: go test -v -race -tags=integration,gms_pure_go -coverprofile=coverage.out -timeout=30m ./... + + - name: Check coverage threshold + run: | + COVERAGE=$(go tool cover -func=coverage.out | grep total | awk '{print $3}' | sed 's/%//') + echo "Coverage: $COVERAGE%" + # Threshold is lower (30%) because Docker-based Dolt integration + # tests are skipped in CI (BEADS_TEST_SKIP=dolt). With Docker + # tests included, coverage would be ~50%+. + if (( $(echo "$COVERAGE < 30" | bc -l) )); then + echo "❌ Coverage is below 30% threshold" + exit 1 + elif (( $(echo "$COVERAGE < 40" | bc -l) )); then + echo "⚠️ Coverage is below 40% (warning threshold)" + else + echo "✅ Coverage meets threshold" + fi + + - name: Upload coverage + uses: codecov/codecov-action@fb8b3582c8e4def4969c97caa2f19720cb33a72f # v6 + if: success() + continue-on-error: true + with: + file: ./coverage.out + fail_ci_if_error: false diff --git a/.github/workflows/nix-build.yml b/.github/workflows/nix-build.yml index d316a690b..4a181ee3b 100644 --- a/.github/workflows/nix-build.yml +++ b/.github/workflows/nix-build.yml @@ -1,12 +1,15 @@ name: nix build on: - # No pull_request trigger: pr-risk.yml's required `test-nix` job already - # runs `nix run .#default` plus `nix flake check -L` on every PR, a strict - # superset of the `nix build .#default` this workflow exercises here. This - # workflow still runs on push to main (and workflow_dispatch) so the plain - # `nix build` path itself stays covered without duplicating a PR-blocking - # job that never adds coverage pr-risk.yml doesn't already have. + pull_request: + paths: + - 'go.mod' + - 'go.sum' + - 'default.nix' + - 'flake.nix' + - 'flake.lock' + - 'overlay.nix' + - 'packages.nix' push: branches: [main] paths: @@ -32,6 +35,6 @@ jobs: - name: Checkout repository uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 - name: Install Nix - uses: DeterminateSystems/determinate-nix-action@527f17dd63d2d60d3e5552934bc84b9a33a14d11 # v3 + uses: DeterminateSystems/determinate-nix-action@61cbfe2efc2d4e7a8a6d56967c3c1058e846c858 # v3 - name: Build run: nix build .#default --print-build-logs diff --git a/.github/workflows/pr-risk.yml b/.github/workflows/pr-risk.yml index 155b3b944..e667d691e 100644 --- a/.github/workflows/pr-risk.yml +++ b/.github/workflows/pr-risk.yml @@ -3,15 +3,7 @@ name: PR Risk on: pull_request: branches: [ main, 'release/**', 'hotfix/**' ] - # Merge queue (engdocs/CI_REQUIRED_CHECK_TOPOLOGY.md, "Merge Queue"): - # the ruleset's required gate must also report on each queue entry's - # merge-group commit, or a queued PR waits until the queue times it out. - # checks_requested is the queue asking for those checks; pinned - # explicitly (scripts/ci_merge_queue_test.go). The concurrency group - # below keys a merge group on its own gh-readonly-queue/* ref, so queue - # runs never cancel each other or a PR's own runs. merge_group: - types: [ checks_requested ] concurrency: group: ${{ github.workflow }}-${{ github.event_name }}-${{ github.event.pull_request.number || github.ref }} @@ -21,38 +13,423 @@ env: BD_DISABLE_METRICS: "1" BD_DISABLE_EVENT_FLUSH: "1" -# The legacy Go test tiers this workflow used to run (embedded Dolt storage, -# conformance and cmd shards; proxied-server cmd shards; server-Dolt storage -# and full suite) are pr.yml's Bazel lanes on rbe-west now: `embedded Dolt -# tier`, `proxied-server tier` (`dolt-race`, with the dolt-server lane, in -# mode remote) and `server-Dolt storage tier` (bazel.yml), which pr.yml's `CI Gate / Required` requires to have run remotely and -# passed on every pull_request and merge group, fork and Dependabot PRs -# included (ga-96smfk.22). What remains is the Nix packaging check, which no -# Bazel target can stand in for: it builds the package Nix users install. - jobs: + detect-ci-tier: + name: Detect CI tier + runs-on: ubuntu-latest + outputs: + full_embedded: ${{ steps.tier.outputs.full_embedded }} + reason: ${{ steps.tier.outputs.reason }} + shadow_embedded_cli: ${{ steps.shadow-selector.outputs.embedded_cli }} + shadow_proxied_cli: ${{ steps.shadow-selector.outputs.proxied_cli }} + shadow_embedded_storage: ${{ steps.shadow-selector.outputs.embedded_storage }} + shadow_embedded_conformance: ${{ steps.shadow-selector.outputs.embedded_conformance }} + shadow_server_conformance: ${{ steps.shadow-selector.outputs.server_conformance }} + shadow_full: ${{ steps.shadow-selector.outputs.full }} + shadow_reason: ${{ steps.shadow-selector.outputs.reason }} + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + with: + fetch-depth: 0 + + - name: Decide embedded Dolt coverage + id: tier + env: + PR_BASE_SHA: ${{ github.event.pull_request.base.sha }} + PR_HEAD_SHA: ${{ github.event.pull_request.head.sha }} + run: bash .github/scripts/ci-embedded-tier.sh + + - name: Shadow CI capability selector + id: shadow-selector + continue-on-error: true + env: + GITHUB_EVENT_NAME: ${{ github.event_name }} + PR_BASE_SHA: ${{ github.event.pull_request.base.sha }} + PR_HEAD_SHA: ${{ github.event.pull_request.head.sha }} + run: | + set -o pipefail + selection="$(bash .github/scripts/ci-capability-selector.sh)" + printf '%s\n' "$selection" + { + echo '### Shadow CI capability selector' + printf '%s\n' "$selection" | sed 's/^/- `/' | sed 's/$/`/' + } >> "$GITHUB_STEP_SUMMARY" + + # End-to-end coverage for proxied-server CLI subcommands, sharded across the + # CI matrix. Each sub-test drives the bd CLI subprocess against a real dolt + # sql-server started via testcontainers by the shared proxied harness + # (cmd/bd/*_proxied_integration_test.go). + # + # Reuses the cmd/bd test binary compiled once by build-embedded: the proxied + # tests live in the same package and are gated at runtime by + # BEADS_TEST_PROXIED_SERVER=1, so no separate build is needed. Shard + # assignment is committed in .github/scripts/proxied-cmd-test-shards.txt; + # newly-added TestProxiedServer* tests hash-distribute until listed there. + test-proxied-cmd: + name: Test (Proxied Dolt Cmd ${{ matrix.shard }}/${{ strategy.job-total }}) + needs: [detect-ci-tier, build-embedded] + if: needs.detect-ci-tier.outputs.full_embedded == 'true' + runs-on: ubuntu-latest + timeout-minutes: 20 + strategy: + fail-fast: false + matrix: + shard: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + + - name: Download binaries + uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8 + with: + name: embedded-test-binaries + path: /tmp/ + + - name: Fix permissions + run: chmod +x /tmp/bd-proxied /tmp/bd-cmd-test + + - name: Install Dolt CLI + # TestProxiedServerExternalCreate needs the dolt binary on PATH; the + # shared harness itself uses the dolt sql-server container image. + run: ./scripts/ci/install-dolt.sh + + - name: Configure Git and Dolt identity + run: | + git config --global user.name "CI Bot" + git config --global user.email "ci@beads.test" + dolt config --global --add user.name "CI Bot" + dolt config --global --add user.email "ci@beads.test" + + - name: Pull Dolt sql-server image + # The shared proxied harness requires the image cached locally + # (isDoltImageCached); without it every proxied test self-skips. Keep the + # tag in sync with internal/testutil/testdoltcommon.go:DoltDockerImage. + run: ./scripts/ci/pull-dolt-image.sh + + - name: Test proxied-server cmd shard + env: + BEADS_TEST_PROXIED_SERVER: "1" + BEADS_TEST_BD_BINARY: /tmp/bd-proxied + BEADS_TEST_CMD_BINARY: /tmp/bd-cmd-test + run: bash .github/scripts/proxied-test-shard.sh ${{ matrix.shard }} 15 + + # Embedded Dolt tests - test the in-process Dolt engine (no server required) + # + # Split into: build → storage tests + parallel cmd test shards. + # cmd/bd embedded tests use a committed shard manifest; newly-added tests + # still auto-distribute via hash fallback until the manifest is updated. + + build-embedded: + name: Build (Embedded Dolt) + needs: detect-ci-tier + if: needs.detect-ci-tier.outputs.full_embedded == 'true' + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + + - name: Set up Go + id: setup-go + uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 + with: + go-version-file: 'go.mod' + cache: false + + - name: Restore Go module cache + uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 + with: + path: ~/go/pkg/mod + key: beads-go-mod-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-${{ hashFiles('go.mod', 'go.sum') }} + restore-keys: beads-go-mod-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}- + + - name: Restore race Go build cache + uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 + with: + path: ${{ runner.temp }}/go-cache/race + key: beads-go-build-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-base-gms_pure_go-race-${{ github.sha }} + restore-keys: beads-go-build-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-base-gms_pure_go-race- + + - name: Restore non-race Go build cache + uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 + with: + path: ${{ runner.temp }}/go-cache/non-race + key: beads-go-build-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-base-gms_pure_go-non-race-${{ github.sha }} + restore-keys: beads-go-build-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-base-gms_pure_go-non-race- + + - name: Build embedded bd binary + env: + GOCACHE: ${{ runner.temp }}/go-cache/race + run: go build -tags gms_pure_go -race -o /tmp/bd-embedded-test ./cmd/bd/ + + - name: Build embedded storage test binary + env: + GOCACHE: ${{ runner.temp }}/go-cache/race + run: go test -tags gms_pure_go -race -c -o /tmp/embeddeddolt-test ./internal/storage/embeddeddolt/ + + - name: Build embedded cmd test binary + env: + GOCACHE: ${{ runner.temp }}/go-cache/race + run: go test -tags gms_pure_go -race -c -o /tmp/bd-cmd-test ./cmd/bd/ + + # Non-race bd used as the proxied-server test subprocess. The proxied + # tests spawn bd many times per sub-test; a -race subprocess would slow + # every invocation, so build a plain binary for BEADS_TEST_BD_BINARY. + - name: Build proxied bd subprocess binary + env: + GOCACHE: ${{ runner.temp }}/go-cache/non-race + run: go build -tags gms_pure_go -o /tmp/bd-proxied ./cmd/bd/ + + # Server-Dolt conformance binary. No -race: the Dolt server runs + # out-of-process in a container, and omitting it keeps the run inside the + # job's timeout. The dolt package self-skips without the docker image, so + # the binary is harmless if its job is gated off. + - name: Build server Dolt conformance test binary + env: + GOCACHE: ${{ runner.temp }}/go-cache/non-race + run: go test -tags=integration,gms_pure_go -c -o /tmp/dolt-conformance-test ./internal/storage/dolt/ + + - name: Upload binaries + uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7 + with: + name: embedded-test-binaries + path: | + /tmp/bd-embedded-test + /tmp/embeddeddolt-test + /tmp/bd-cmd-test + /tmp/bd-proxied + /tmp/dolt-conformance-test + retention-days: 1 + + test-embedded-storage: + name: Test (Embedded Dolt Storage ${{ matrix.shard }}/${{ strategy.job-total }}) + needs: [detect-ci-tier, build-embedded] + if: needs.detect-ci-tier.outputs.full_embedded == 'true' + runs-on: ubuntu-latest + timeout-minutes: 15 + strategy: + fail-fast: false + matrix: + shard: [1, 2, 3, 4, 5] + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + + - name: Download binaries + uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8 + with: + name: embedded-test-binaries + path: /tmp/ + + - name: Fix permissions + run: chmod +x /tmp/embeddeddolt-test + + - name: Configure Git + run: | + git config --global user.name "CI Bot" + git config --global user.email "ci@beads.test" + + - name: Test + env: + BEADS_TEST_EMBEDDED_DOLT: "1" + BEADS_TEST_EMBEDDED_TEST_BINARY: /tmp/embeddeddolt-test + # Manifest-driven shard of the storage suite (mirrors test-embedded-cmd + # / embedded-test-shard.sh). Conformance runs separately in the + # dedicated test-embedded-conformance job; embedded-storage-test-shard.sh + # excludes TestConformance from discovery, so it never lands in one of + # these shards. + run: bash .github/scripts/embedded-storage-test-shard.sh ${{ matrix.shard }} 5 + + test-embedded-conformance: + name: Test (Embedded Dolt Conformance - ${{ matrix.partition }}) + needs: [detect-ci-tier, build-embedded] + if: needs.detect-ci-tier.outputs.full_embedded == 'true' + runs-on: ubuntu-latest + timeout-minutes: 35 + strategy: + fail-fast: false + matrix: + partition: [core, audit] + env: + BEADS_TEST_EMBEDDED_DOLT: "1" + BEADS_TEST_EMBEDDED_TEST_BINARY: /tmp/embeddeddolt-test + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + + - name: Download binaries + uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8 + with: + name: embedded-test-binaries + path: /tmp/ + + - name: Fix permissions + run: chmod +x /tmp/embeddeddolt-test + + - name: Configure Git + run: | + git config --global user.name "CI Bot" + git config --global user.email "ci@beads.test" + + - name: Test core conformance + if: matrix.partition == 'core' + # RunAll has a natural Audit seam. Run everything except Audit in the + # core shard and Audit alone in the other shard. The combined suite has + # exceeded 30m under runner variance; each half currently takes ~15m. + # Keep the Go timeout below the job timeout so a real stall still emits + # a diagnostic goroutine dump before Actions stops the job. + run: /tmp/embeddeddolt-test -test.v -test.count=1 -test.timeout=30m -test.run '^TestConformance$' -test.skip '^TestConformance$/^Audit$' + + - name: Test audit conformance + if: matrix.partition == 'audit' + run: /tmp/embeddeddolt-test -test.v -test.count=1 -test.timeout=30m -test.run '^TestConformance$/^Audit$' + + test-server-storage: + name: Test (Server Dolt Conformance) + needs: [detect-ci-tier, build-embedded] + if: needs.detect-ci-tier.outputs.full_embedded == 'true' + runs-on: ubuntu-latest + timeout-minutes: 20 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + + - name: Download binaries + uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8 + with: + name: embedded-test-binaries + path: /tmp/ + + - name: Fix permissions + run: chmod +x /tmp/dolt-conformance-test + + - name: Configure Git + run: | + git config --global user.name "CI Bot" + git config --global user.email "ci@beads.test" + + - name: Install Dolt + run: ./scripts/ci/install-dolt.sh + + - name: Configure Dolt + run: | + dolt config --global --add user.name "CI Bot" + dolt config --global --add user.email "ci@beads.test" + dolt version + + - name: Pull Dolt sql-server image + # Presence of this image flips isDoltImageCached() true, so the suite + # exercises server Dolt instead of self-skipping. Keep the tag in sync + # with internal/testutil/testdoltcommon.go:DoltDockerImage. + run: ./scripts/ci/pull-dolt-image.sh + + - name: Test + # Server twin of test-embedded-conformance: same conformance.RunAll, but + # against the container-backed server-mode Dolt backend. No -short / no + # BEADS_TEST_SKIP=dolt, so TestMain starts the shared server and the + # suite runs instead of self-skipping. + run: /tmp/dolt-conformance-test -test.v -test.count=1 -test.timeout=15m -test.run '^TestConformance$' + + test-server-storage-full: + name: Test (Server Dolt Full Suite ${{ matrix.shard }}/${{ strategy.job-total }}) + needs: [detect-ci-tier, build-embedded] + if: needs.detect-ci-tier.outputs.full_embedded == 'true' + runs-on: ubuntu-latest + timeout-minutes: 20 + strategy: + fail-fast: false + matrix: + shard: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16] + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + + - name: Download binaries + uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8 + with: + name: embedded-test-binaries + path: /tmp/ + + - name: Fix permissions + run: chmod +x /tmp/dolt-conformance-test + + - name: Configure Git + run: | + git config --global user.name "CI Bot" + git config --global user.email "ci@beads.test" + + - name: Install Dolt + run: ./scripts/ci/install-dolt.sh + + - name: Configure Dolt + run: | + dolt config --global --add user.name "CI Bot" + dolt config --global --add user.email "ci@beads.test" + dolt version + + - name: Pull Dolt sql-server image + # Presence of this image flips isDoltImageCached() true, so the suite + # exercises server Dolt instead of self-skipping. Keep the tag in sync + # with internal/testutil/testdoltcommon.go:DoltDockerImage. + run: ./scripts/ci/pull-dolt-image.sh + + - name: Test + # build-embedded's dolt-conformance-test binary compiles every + # top-level test in internal/storage/dolt, not just conformance -- + # test-server-storage above only ever runs the ^TestConformance$ + # subset. Everything else in that binary (TestCreateGuard_*, + # federation/create-guard/concurrent suites, and diff-owned tests such + # as TestBenchDBPurgeDoesNotLeak on PR #5792) was reached by no + # PR-triggered lane and silently SKIPped for lack of a live server. + # This sibling job reuses the same artifact and server setup and runs + # everything else instead, so a SKIP there gets a real PASS/FAIL. + # + # Manifest-driven shard of the full suite (mirrors test-embedded-storage + # / server-storage-test-shard.sh): 1126 tests in one 15m-Go-timeout job + # never finished (job 95616282508 died at 256/1126 tests, 15m28s in, + # zero failures -- it just ran out of time). server-storage-test-shard.sh + # excludes TestConformance from discovery, so it never lands in one of + # these shards. + env: + BEADS_TEST_ENV_RUN_DOLT: "1" + run: bash .github/scripts/server-storage-test-shard.sh ${{ matrix.shard }} 16 + + test-embedded-cmd: + name: Test (Embedded Dolt Cmd ${{ matrix.shard }}/${{ strategy.job-total }}) + needs: [detect-ci-tier, build-embedded] + if: needs.detect-ci-tier.outputs.full_embedded == 'true' + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + shard: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + + - name: Download binaries + uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8 + with: + name: embedded-test-binaries + path: /tmp/ + + - name: Fix permissions + run: chmod +x /tmp/bd-embedded-test /tmp/embeddeddolt-test /tmp/bd-cmd-test + + - name: Configure Git + run: | + git config --global user.name "CI Bot" + git config --global user.email "ci@beads.test" + + - name: Test + env: + BEADS_TEST_EMBEDDED_DOLT: "1" + BEADS_TEST_BD_BINARY: /tmp/bd-embedded-test + run: bash .github/scripts/embedded-test-shard.sh ${{ matrix.shard }} 20 + test-nix: name: Test Nix Flake - # F7a: cache-free (no GitHub-saved cache restored/saved here) same-repo - # job moved onto Blacksmith at 4 vCPU; forks, Dependabot and any other - # event stay GitHub-hosted. Must match TestSameRepoBlacksmithRunners. - runs-on: ${{ (github.event_name == 'merge_group' || (github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository && github.actor != 'dependabot[bot]')) && 'blacksmith-4vcpu-ubuntu-2404' || 'ubuntu-latest' }} + runs-on: ubuntu-latest timeout-minutes: 10 steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 - - uses: DeterminateSystems/determinate-nix-action@527f17dd63d2d60d3e5552934bc84b9a33a14d11 # v3 + - uses: DeterminateSystems/determinate-nix-action@61cbfe2efc2d4e7a8a6d56967c3c1058e846c858 # v3 - name: Run bd help via Nix id: nix_help run: | nix run .#default -- --help > help.txt - # A packaging check, not a test: evaluates every flake output (packages, - # apps, devShells, overlay) for every system the flake declares. The - # flake has no `checks`: its one check, hook-timeout-backends, runs - # under Bazel as //tests/hook_timeout_backends:hook_timeout_backends_test - # in bazel.yml's PR-core lane. - - name: Run the flake checks - run: | - nix flake check -L - name: Verify help text if: steps.nix_help.outcome == 'success' run: | @@ -67,18 +444,17 @@ jobs: echo "Help text first line is correct" ci-gate: - name: PR Risk Gate / Required - # F3: same-repo PRs and merge_group get the org's Blacksmith pool; forks, - # Dependabot and any other event stay GitHub-hosted. This job restores no - # GitHub-saved cache. Must match the TestSameRepoBlacksmithRunners pin; - # the gate's NAME must never change (ruleset requires exactly "PR Risk - # Gate / Required"). - runs-on: ${{ (github.event_name == 'merge_group' || (github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository && github.actor != 'dependabot[bot]')) && 'blacksmith-2vcpu-ubuntu-2404' || 'ubuntu-latest' }} - # F3: now on Blacksmith for same-repo PRs; a hang used to burn only the - # default 360 minutes of a free GitHub-hosted runner, now Blacksmith - # minutes too. - timeout-minutes: 10 + name: CI Gate / Required + runs-on: ubuntu-latest needs: + - detect-ci-tier + - build-embedded + - test-embedded-storage + - test-embedded-conformance + - test-server-storage + - test-server-storage-full + - test-embedded-cmd + - test-proxied-cmd - test-nix if: ${{ always() }} steps: @@ -88,6 +464,29 @@ jobs: env: CI_GATE_NAME: PR risk gate CI_GATE_REQUIRED: >- + DETECT_CI_TIER + BUILD_EMBEDDED + TEST_EMBEDDED_STORAGE + TEST_EMBEDDED_CONFORMANCE + TEST_SERVER_STORAGE + TEST_SERVER_STORAGE_FULL + TEST_EMBEDDED_CMD + TEST_PROXIED_CMD TEST_NIX + FULL_EMBEDDED: ${{ needs.detect-ci-tier.outputs.full_embedded }} + DETECT_CI_TIER: ${{ needs.detect-ci-tier.result }} + BUILD_EMBEDDED: ${{ needs.build-embedded.result }} + TEST_EMBEDDED_STORAGE: ${{ needs.test-embedded-storage.result }} + TEST_EMBEDDED_CONFORMANCE: ${{ needs.test-embedded-conformance.result }} + TEST_SERVER_STORAGE: ${{ needs.test-server-storage.result }} + TEST_SERVER_STORAGE_FULL: ${{ needs.test-server-storage-full.result }} + TEST_EMBEDDED_CMD: ${{ needs.test-embedded-cmd.result }} + TEST_PROXIED_CMD: ${{ needs.test-proxied-cmd.result }} TEST_NIX: ${{ needs.test-nix.result }} - run: bash .github/scripts/ci-gate.sh + run: | + skipped_ok="" + if [[ "$FULL_EMBEDDED" != "true" ]]; then + skipped_ok="BUILD_EMBEDDED TEST_EMBEDDED_STORAGE TEST_EMBEDDED_CONFORMANCE TEST_SERVER_STORAGE TEST_SERVER_STORAGE_FULL TEST_EMBEDDED_CMD TEST_PROXIED_CMD" + fi + export CI_GATE_SKIPPED_OK="$skipped_ok" + bash .github/scripts/ci-gate.sh diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml index 22dc8c23e..65cd53f80 100644 --- a/.github/workflows/pr.yml +++ b/.github/workflows/pr.yml @@ -6,15 +6,7 @@ on: # the release branch, not on main. Without it they run almost no CI and the # first full signal is the tag build, where a failure burns the tag. branches: [ main, 'release/**', 'hotfix/**' ] - # Merge queue (engdocs/CI_REQUIRED_CHECK_TOPOLOGY.md, "Merge Queue"): - # the ruleset's required gate must also report on each queue entry's - # merge-group commit, or a queued PR waits until the queue times it out. - # checks_requested is the queue asking for those checks; pinned - # explicitly (scripts/ci_merge_queue_test.go). The concurrency group - # below keys a merge group on its own gh-readonly-queue/* ref, so queue - # runs never cancel each other or a PR's own runs. merge_group: - types: [ checks_requested ] concurrency: group: ${{ github.workflow }}-${{ github.event_name }}-${{ github.event.pull_request.number || github.ref }} @@ -23,127 +15,17 @@ concurrency: env: BD_DISABLE_METRICS: "1" BD_DISABLE_EVENT_FLUSH: "1" - # step's run script. Committed, not a repo variable, so a re-run can never - # change it. - # bazel.yml's bazel-cmd-dolt lane (the cmd/bd Dolt-server tier: the - # Dolt-gated cmd/bd tests no other lane runs) is advisory while this is - # "false": it runs and reports in the bazel call (continue-on-error, so a - # failure never turns the call's aggregate red), and ci-gate ignores its - # BAZEL_CMD_DOLT result. "true" adds BAZEL_CMD_DOLT to CI_GATE_REQUIRED in - # the "Evaluate CI gate" step's run script; bazel-gate.sh still accepts - # its skip in modes skip and local. Committed, not a repo variable, so a - # re-run can never change it. The `bazel` job's cmd-dolt-required - # input must equal it (a `with:` cannot read env; policy-tested): "true" - # also drops //cmd/bd:bd_test from bazel-integration, because the required - # lane runs the same integration-build bd_test without the dolt skip, so - # each cmd/bd test runs once. Rollback: commit "false" here and false in - # that input together; bazel-integration runs bd_test again on the next run. - BAZEL_CMD_DOLT_REQUIRED: "true" jobs: - # The checks that need the PR's base commit, so no hermetic Bazel target - # can hold them: check-migration-hygiene.sh's delta half (C: shipped - # migrations are frozen, D: ignored-plane twins, E: no prepared DML in new - # migrations, all over `git diff `) and the .beads/issues.jsonl - # guard. Everything else this job ran is a Bazel test in bazel.yml's `test` - # lane (ga-96smfk.20): //scripts/repochecks:build_tags_test, - # :go_install_guidance_test, :winget_portable_alias_test, - # :migration_hygiene_test (checks A and B) and :fmt_test, and - # //internal/versioncheck:versioncheck_test for check-versions.sh. No Go: - # both steps are bash and git. - # - # Each step has its own id and `if: ${{ !cancelled() }}`, so one failure - # does not hide the other, and its outcome is a job output that ci-gate - # requires (TestPRCIGateFastChecksTokens). FAST_CHECKS (the job's own - # `result`) is the backstop token for a job lost before any step set its - # output. - fast-checks: - name: Fast checks (frozen migrations, .beads diff) - runs-on: ${{ (github.event_name == 'merge_group' || (github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository && github.actor != 'dependabot[bot]')) && 'blacksmith-2vcpu-ubuntu-2404' || 'ubuntu-latest' }} - timeout-minutes: 10 - outputs: - migration-hygiene: ${{ steps.migration-hygiene.outcome }} - no-beads-changes: ${{ steps.no-beads-changes.outcome }} + build-artifacts: + name: Build Artifacts + runs-on: ubuntu-latest steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 with: - # check-migration-hygiene and check-no-beads-changes diff against - # the base branch; a shallow checkout starves both. fetch-depth: 0 - # Migration hygiene, delta half: edits to migration files that already - # exist on the base branch (shipped migrations are frozen; fix forward - # with a new version), and the twin and prepared-DML rules for new - # migrations. Checks A and B (duplicate versions, nondeterministic SQL) - # read only the tree and run in Bazel. Details: - # scripts/check-migration-hygiene.sh. - - name: Run migration hygiene checks - id: migration-hygiene - if: ${{ !cancelled() }} - env: - BASE_SHA: ${{ github.event.pull_request.base.sha || github.event.merge_group.base_sha }} - MIGRATION_HYGIENE_SCOPE: delta - run: ./scripts/check-migration-hygiene.sh - - # Fast check to catch accidental .beads/issues.jsonl changes from - # contributors. Runs on both PR triggers: a pull_request diffs against - # origin/ (GITHUB_BASE_REF), a merge group (which has no - # base_ref) against the queue's merge_group.base_sha, the commit the - # group's head is built on (fetch-depth 0 above fetches it). - - name: Check for .beads/issues.jsonl changes - id: no-beads-changes - if: ${{ !cancelled() }} - env: - MERGE_GROUP_BASE_SHA: ${{ github.event.merge_group.base_sha }} - run: | - base="${MERGE_GROUP_BASE_SHA:-origin/${GITHUB_BASE_REF:-main}}" - # Fail closed: a pipe into grep -q would pass on a failed diff. - if ! git rev-parse --verify --quiet "$base^{commit}" >/dev/null; then - echo "::error::diff base $base is unavailable" - exit 1 - fi - if git diff --name-only "$base"...HEAD | grep -q "^\.beads/issues\.jsonl$"; then - echo "This PR includes changes to .beads/issues.jsonl" - echo "" - echo "This file is the project's issue database and should not be modified in PRs." - echo "" - echo "To fix, run:" - echo " git checkout origin/main -- .beads/issues.jsonl" - echo " git commit --amend" - echo " git push --force" - echo "" - exit 1 - fi - echo "No .beads/issues.jsonl changes detected" - - # The lingering-handle liveness regression test is //go:build windows: the - # Ubuntu job above selects it by name but compiles it out, and main.yml's - # Windows job runs only on pushes to main (build/smoke, no go test). Run - # the liveness pair and native config fixture on Windows so their platform - # regressions actually execute at PR time. The init credential shell - # fixtures are a separate concern and run natively in - # pr-preflight-platforms, not here. - # Blacksmith Windows (blacksmith-4vcpu-windows-2025: GitHub's windows-2025 - # image minus the full Visual Studio IDE/EdgeDriver/WinAppDriver, VS Build - # Tools 2022 present, no Linux Docker) for every PR and merge group, forks - # and Dependabot included (ga-96smfk.22): Windows and macOS jobs run on - # Blacksmith, everything Linux under Bazel on rbe-west. The first step - # builds ./cmd/bd with CGO_ENABLED=1 (embedded Dolt); the toolchain - # diagnostic step below prints which cgo toolchain backs it. Since - # ga-96smfk.22 this job (not a cross-compiled "-prebuilt" twin) is the - # required run of these tests. - test-windows-liveness: - name: Test (Windows) cmd/bd liveness regression - runs-on: ${{ 'blacksmith-4vcpu-windows-2025' }} - # F7b review fix (S6): raised from 15 to 20, matching open PR #7143's own - # measurement on this exact job (retained runs take about 8m; one run's - # own step timing showed 451s total with a 403s compile step) and - # main.yml's test-windows (S8) for consistency. - timeout-minutes: 20 - steps: - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 - - name: Set up Go id: setup-go uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 @@ -151,190 +33,6 @@ jobs: go-version-file: 'go.mod' cache: false - # F7b review fix (S9): Blacksmith's windows-2025 image docs confirm "VS - # Build Tools 2022" but not which cgo toolchain backs CGO_ENABLED=1 go - # builds there (see the RISK comment above). Print the relevant tool - # versions early and clearly-labelled so a toolchain gap surfaces in - # this step's own log instead of failing deep into the liveness test - # below with an opaque cgo/link error. - - name: Check Windows cgo toolchain diagnostics - shell: bash - run: | - echo "::group::go env (CGO_ENABLED, CC, GOOS, GOARCH)" - go env CGO_ENABLED CC GOOS GOARCH - echo "::endgroup::" - echo "::group::gcc (mingw-w64) presence" - command -v gcc && gcc --version || echo "gcc not found on PATH" - echo "::endgroup::" - echo "::group::choco presence" - command -v choco && choco --version || echo "choco not found on PATH" - echo "::endgroup::" - - - name: Restore Go module cache - uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 - with: - path: ~/go/pkg/mod - key: beads-go-mod-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-${{ hashFiles('go.mod', 'go.sum') }} - restore-keys: beads-go-mod-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}- - - # F7b review fix (S2): restore-only non-race Go build cache seeded by - # main.yml's test-windows Blacksmith leg (warm-non-race-cache.sh) on - # the exact same runner selection, so the cold cmd/bd compile below - # (the real long pole here per the reviewer's step-timing data) gets a - # true cache hit instead of starting cold every PR. - - name: Restore non-race Go build cache - uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 - with: - path: ${{ runner.temp }}/go-cache/non-race - key: beads-go-build-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-base-gms_pure_go-non-race-${{ github.sha }} - restore-keys: beads-go-build-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-base-gms_pure_go-non-race- - - - name: Run native Windows global Prime override - shell: bash - env: - CGO_ENABLED: "1" - GOCACHE: ${{ runner.temp }}/go-cache/non-race - run: | - go_path=$(command -v go) - [[ "$("$go_path" env GOHOSTOS GOOS)" == $'windows\nwindows' ]] - test_status=0 - test_output=$(./scripts/test.sh -v -timeout 5m -run '^TestPrimeBinaryPortfolio$/^TestPrime_HookJSON_GlobalPrimeOverride$' -count=1 ./cmd/bd) || test_status=$? - printf '%s\n' "$test_output" - ((test_status == 0)) - pass_pattern='^[[:space:]]*--- PASS: TestPrimeBinaryPortfolio/TestPrime_HookJSON_GlobalPrimeOverride \(' - nonpass_pattern='^[[:space:]]*--- (FAIL|SKIP): ' - pass_count=0 - while IFS= read -r line; do - if [[ "$line" =~ $pass_pattern ]]; then - ((pass_count += 1)) - fi - if [[ "$line" =~ $nonpass_pattern ]]; then - exit 1 - fi - done <<<"$test_output" - ((pass_count == 1)) - - - name: Run Windows liveness regression test - env: - GOCACHE: ${{ runner.temp }}/go-cache/non-race - run: go test -tags gms_pure_go -count=1 -run '^TestIsServerProbablyRunning' ./cmd/bd - - # internal/doltversion carries Windows-specific branches (PATHEXT - # extension completion in Resolve, ERROR_BAD_EXE_FORMAT detection, the - # executable-bit carve-out) and its behavioral tests run there via .cmd - # stubs — but the Ubuntu test jobs compile those branches without ever - # executing them. Same pattern as the liveness job above: run exactly - # this package on windows-latest so the platform it targets actually - # exercises it at PR time. Small on purpose (~1 min). - # - # internal/storage/dbproxy/server carries the platform-portable path - # handling that #5820 introduced (testPlatformPath, external dolt config - # validation, DSN shaping); run exactly this package on windows-latest - - # the platform those paths target. Tests that need a dolt binary Skipf - # loudly (dolt is not installed on the Windows runners), and the - # unix-socket cases skip on Windows by design. - # - # F7a: fold of two small, independent windows-latest jobs - # (test-windows-doltversion, test-windows-dbproxy-server) into one. Neither - # was in ci-gate's needs/CI_GATE_REQUIRED before the fold (promoting either - # to required-for-merge, once it has run clean for a while, is a separate - # maintainer call), so this fold needs no gate changes. Not one of the - # Windows jobs F4 touches (test-windows-liveness, worktree-remove-windows, - # the preflight/doc-freshness matrices, the twins). - # - # Review SF-1 (2026-10-03): neither step carries continue-on-error, so a - # failing test already fails the job on its own; `if: ${{ !cancelled() }}` - # on the second step only ensures it still runs after the first fails, - # giving both packages' results in one log instead of stopping at the - # first failure. No aggregator step is needed. - # Blacksmith Windows (blacksmith-2vcpu-windows-2025) for every PR; both steps - # build with CGO_ENABLED=0 (or no cgo at all for doltversion), so this job - # carries none of test-windows-liveness's cgo-toolchain risk. - test-windows-small: - name: Test (Windows) small packages (doltversion, dbproxy server) - runs-on: ${{ 'blacksmith-2vcpu-windows-2025' }} - timeout-minutes: 15 - steps: - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 - - - name: Set up Go - id: setup-go - uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 - with: - go-version-file: 'go.mod' - cache: false - - # F7b review fix (S9): see test-windows-liveness's identical step for - # the full rationale. Both packages here build with CGO_ENABLED=0 (or - # no cgo at all), so this is lower-risk than test-windows-liveness, but - # printing the toolchain state costs nothing and keeps the diagnostic - # convention uniform across every moved Windows job. - - name: Check Windows toolchain diagnostics - shell: bash - run: go env CGO_ENABLED CC GOOS GOARCH - - - name: Run doltversion package tests on Windows - id: doltversion - if: ${{ !cancelled() }} - run: go test -tags gms_pure_go -count=1 ./internal/doltversion/ - - - name: Run dbproxy server package tests on Windows - id: dbproxy-server - if: ${{ !cancelled() }} - env: - CGO_ENABLED: "0" - run: go test -tags gms_pure_go -count=1 -v ./internal/storage/dbproxy/server/ - - # Destructive worktree removal depends on native Windows path identity. - # This focused process suite requires NTFS per-directory case sensitivity - # and fails (rather than skips) when the runner cannot provide it. - # Blacksmith Windows (blacksmith-4vcpu-windows-2025) for every PR. - # CGO_ENABLED=0 below, so no cgo-toolchain risk; its non-race GOCACHE - # restore-keys prefix gets a hit from main.yml's test-windows job, which - # saves that key on this exact runner selection (restore-keys falls back - # to the most recent prefix match regardless of the exact sha). Since - # ga-96smfk.22 this job is the required run of these tests. - # - # RISK (F7b review: not flagged in the original implementation -- - # cmd/bd/worktree_remove_git_windows_test.go:233 shells out to - # `fsutil file SetCaseSensitiveInfo`, which requires the "Windows Subsystem - # for Linux" optional feature's per-directory case-sensitivity support to - # be enabled on the image. If that feature is not enabled on Blacksmith's - # windows-2025 image, this FAILS OUTRIGHT rather than skipping (the test - # does not treat an unsupported fsutil call as a skip condition). The - # diagnostic step below surfaces that gap in this job's own log instead of - # a bare test failure deep in the suite; if it reports the feature missing, - # roll back this job's runs-on to the literal `windows-latest` until - # Blacksmith confirms/enables it. - worktree-remove-windows: - name: Worktree remove boundary (Windows) - runs-on: ${{ 'blacksmith-4vcpu-windows-2025' }} - timeout-minutes: 15 - steps: - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 - - - name: Set up Go - id: setup-go - uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 - with: - go-version-file: 'go.mod' - cache: false - - - name: Check Windows case-sensitivity tooling diagnostics - shell: pwsh - run: | - Write-Output "::group::fsutil file SetCaseSensitiveInfo capability" - $tmp = Join-Path $env:RUNNER_TEMP "case-sensitivity-probe" - New-Item -ItemType Directory -Path $tmp -Force | Out-Null - & fsutil file SetCaseSensitiveInfo $tmp enable - if ($LASTEXITCODE -ne 0) { - Write-Output "::warning::fsutil file SetCaseSensitiveInfo failed (exit $LASTEXITCODE) - this job's worktree-remove tests require the Windows Subsystem for Linux optional feature's per-directory case-sensitivity support and will FAIL (not skip) below if it is unavailable on this image" - } else { - Write-Output "fsutil file SetCaseSensitiveInfo: supported" - } - Remove-Item -Recurse -Force $tmp -ErrorAction SilentlyContinue - Write-Output "::endgroup::" - - name: Restore Go module cache uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 with: @@ -349,323 +47,36 @@ jobs: key: beads-go-build-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-base-gms_pure_go-non-race-${{ github.sha }} restore-keys: beads-go-build-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-base-gms_pure_go-non-race- - - name: Run native Windows worktree removal boundary tests + - name: Build reusable Linux artifacts env: - CGO_ENABLED: "0" GOCACHE: ${{ runner.temp }}/go-cache/non-race - run: go test -tags gms_pure_go -count=1 -run '^TestWorktreeRemove' ./cmd/bd + run: | + set -euo pipefail + mkdir -p artifacts + source ./.buildflags + go_version="$(go version)" + commit="$(git rev-parse HEAD)" + go build -tags "$BEADS_BUILD_TAGS" -o artifacts/bd-linux-gms-pure ./cmd/bd + chmod +x artifacts/bd-linux-gms-pure + ( + cd artifacts + sha256sum bd-linux-gms-pure > SHA256SUMS + ) + { + echo "commit=${commit}" + echo "go_version=${go_version}" + echo "build_tags=${BEADS_BUILD_TAGS}" + echo "artifact=bd-linux-gms-pure" + } > artifacts/build-manifest.txt - # Check documentation references match actual CLI flags - # - # Stays a job: the generated CLI docs describe the release pinned in - # docs/cli-docs.pin, and check-doc-flags.sh validates them against bd built - # from that tag (scripts/resolve-docs-bd.sh: git fetch the tag, pure-Go go - # build), attributing drift against the merge-base. Neither input can enter - # a Bazel action key: releases publish no pure-Go linux binary to pin, and - # the attribution needs the PR's base. check-doc-freshness.sh, which this - # job also ran, is //scripts/repochecks:doc_freshness_test. - # - # F7a: moved to Blacksmith for same-repo PRs/merge_group. This job's module - # cache restore step was already absent before the fold (every run cold- - # builds bd), so there is no GitHub-saved cache to lose here. - check-doc-flags: - name: Check doc flags freshness - runs-on: ${{ (github.event_name == 'merge_group' || (github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository && github.actor != 'dependabot[bot]')) && 'blacksmith-4vcpu-ubuntu-2404' || 'ubuntu-latest' }} - timeout-minutes: 15 - steps: - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 - with: - # Full history so check-cli-docs-drift.sh can regenerate docs at the - # merge-base and only fail PRs for drift they actually introduced. - fetch-depth: 0 - - - name: Set up Go - uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 - with: - go-version-file: 'go.mod' - cache: false - - - name: Build bd - run: CGO_ENABLED=0 go build -tags gms_pure_go -o bd ./cmd/bd/ - - - name: Validate docs against CLI - env: - DOC_DRIFT_PATCH_OUT: ${{ runner.temp }}/cli-docs-freshness.patch - # Merge queue: a merge group has no GITHUB_BASE_REF, and without a - # diff base check-cli-docs-drift.sh fails on any drift, including - # drift already on main that the PR run attributed away, which - # would eject every queued PR. Diff against the queue's base - # commit instead (empty on pull_request: origin/$GITHUB_BASE_REF). - BD_DOCS_DIFF_BASE: ${{ github.event.merge_group.base_sha }} - run: ./scripts/check-doc-flags.sh ./bd - - # When the drift check fails, it writes the exact regenerated-docs fix - # (produced with CI's canonical build) so contributors can apply it with - # `git apply` instead of reproducing CI's environment locally. The - # docs-autofix.yml workflow_run job consumes this same artifact to push - # the fix to same-repo PR branches automatically. - - name: Upload docs fix patch - if: failure() + - name: Upload build artifacts uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7 with: - name: cli-docs-freshness-patch - path: ${{ runner.temp }}/cli-docs-freshness.patch - if-no-files-found: ignore + name: ci-build-artifacts + path: artifacts/ + retention-days: 1 + if-no-files-found: error - # The Linux leg is Bazel's (ga-96smfk.20): //scripts:doc_freshness_required_test - # (integration lane: the doc-freshness suite under -required-suite) and - # //scripts/gitattributespolicy:gitattributespolicy_required_host_test - # (-required-host -expected-goos linux), both on rbe-west's Linux workers. - # The macOS and Windows legs stay here, on Blacksmith for every PR and - # merge group, forks and Dependabot included (ga-96smfk.22). Each leg's - # `include` entry names its Blacksmith label in `runner` (`os` keeps the - # check names stable and selects the Windows-only steps). Every leg - # restores a non-race Go build cache seeded by main.yml's push-to-main - # savers on the exact same label (S2 below): test-windows for Windows, - # blacksmith-macos-go-build-cache for macOS. The macOS label is pinned to - # macos-26 (Apple Silicon, the image GitHub's macos-latest resolves to); - # 6 vCPU is enough for a leg dominated by one incremental ./cmd/bd test - # compile. - check-doc-freshness-platforms: - name: Check doc freshness (${{ matrix.os }}) - runs-on: ${{ matrix.runner }} - timeout-minutes: 10 - strategy: - fail-fast: false - matrix: - include: - - os: macos-latest - expected_goos: darwin - runner: blacksmith-6vcpu-macos-26 - - os: windows-latest - expected_goos: windows - runner: blacksmith-4vcpu-windows-2025 - steps: - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 - - - name: Set up Go - id: setup-go - uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 - with: - go-version-file: 'go.mod' - cache: false - - - name: Restore Go module cache - uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 - with: - path: ~/go/pkg/mod - key: beads-go-mod-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-${{ hashFiles('go.mod', 'go.sum') }} - restore-keys: beads-go-mod-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}- - - # F7b review fix (S2): same restore-only non-race Go build cache as - # pr-preflight-platforms above, seeded by the same push-to-main savers - # on the exact same runner selections; this job's two "go test" steps - # below compile ./scripts and ./scripts/gitattributespolicy, so they - # benefit from the same warm cache (main.yml's warm-non-race-cache.sh - # run compiles both exact packages -- F7b spec 4.2). - - name: Restore non-race Go build cache - uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 - with: - path: ${{ runner.temp }}/go-cache/non-race - key: beads-go-build-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-base-gms_pure_go-non-race-${{ github.sha }} - restore-keys: beads-go-build-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-base-gms_pure_go-non-race- - - - name: Exercise native date and Bash process boundary - env: - GOCACHE: ${{ runner.temp }}/go-cache/non-race - run: go test '-tags=integration,gms_pure_go' -count=1 -run '^(TestDocFreshness.*|TestRequiredSuiteContract)$' ./scripts -args -required-suite=doc-freshness - - - name: Exercise repository text EOL policy boundary - env: - GOCACHE: ${{ runner.temp }}/go-cache/non-race - run: go test '-tags=integration,gms_pure_go' -count=1 ./scripts/gitattributespolicy -args -required-host -expected-goos '${{ matrix.expected_goos }}' - - # Blacksmith macOS and Windows legs, labels in the matrix (see - # check-doc-freshness-platforms above). The Linux leg is retired - # (ga-96smfk.22): every Linux step's tests run under Bazel on rbe-west - # (//scripts:scripts_test, //cmd/bd:bd_test, //internal/config:config_test). - # Only the two - # tests of go test's own bd build fallback - # (tools/bazel/equivalence_allowlist.txt) skip under Bazel; they keep - # running here on macOS and Windows. - # - # F7b review (S1): an earlier revision of this job split the Windows leg - # into two `shard` entries on the theory that it would turn one ~9-minute - # leg into two ~4-5 minute parallel legs. Real step-timing data from PR - # runs disproved that: shard 2 (the three Windows-only steps moved below) - # only ever holds 15-25s of work, while shard 1 still takes 450-480s, so - # the split added a second billed Blacksmith Windows runner for no - # measurable critical-path win. The actual long pole on every OS is the - # cold `cmd/bd` test compile in "Exercise generated Git hook timeout - # process boundary" (250-300s); see S2 below (the restore-non-race-cache - # step) for the fix that actually targets it. Reverted to one leg per OS. - # - # Windows leg budget with the caches warm (merge group 37710264475 and PR - # runs, 2026-10-07/08): 45-115s setup (setup-go, then the two cache - # restores, mostly tar extraction), 33-71s for TestPRPreflight (Git Bash - # process creation, now parallel subtests), 27-58s to compile and link - # ./cmd/bd's test binary, and about 25s for the rest. A second Windows leg - # would pay the setup again to save about 20s, so it stays one leg. An - # 8 vCPU Windows leg was tried (#7381): cache restores still hit, but it - # queued 63s for the larger runner and finished at 144s vs 129s here. - pr-preflight-platforms: - name: PR preflight process (${{ matrix.os }}) - runs-on: ${{ matrix.runner }} - timeout-minutes: 20 - strategy: - fail-fast: false - matrix: - include: - - os: macos-latest - expected_goos: darwin - runner: blacksmith-6vcpu-macos-26 - - os: windows-latest - expected_goos: windows - runner: blacksmith-4vcpu-windows-2025 - steps: - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 - - - name: Set up Go - id: setup-go - uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 - with: - go-version-file: 'go.mod' - cache: false - - - name: Restore Go module cache - uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 - with: - path: ~/go/pkg/mod - key: beads-go-mod-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-${{ hashFiles('go.mod', 'go.sum') }} - restore-keys: beads-go-mod-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}- - - # F7b review (S2): restore-only non-race Go build cache, seeded by the - # push-to-main saver on the exact same Blacksmith label (Windows: - # main.yml's test-windows; macOS: main.yml's - # blacksmith-macos-go-build-cache) so the cold cmd/bd compile below - # (the real long pole, 250-300s uncached) gets a true cache hit instead - # of starting cold. GOCACHE must match the seeder's exactly, and - # CGO_ENABLED is left at its platform default (same as the seeder) so - # the key families line up; see mainBlacksmithWindows4vcpuRunsOn in - # ci_workflow_test.go for the seeder side of this contract. - - name: Restore non-race Go build cache - uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 - with: - path: ${{ runner.temp }}/go-cache/non-race - key: beads-go-build-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-base-gms_pure_go-non-race-${{ github.sha }} - restore-keys: beads-go-build-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-base-gms_pure_go-non-race- - - - name: Exercise the real Bash process boundary - shell: bash - env: - GOCACHE: ${{ runner.temp }}/go-cache/non-race - run: go test '-tags=integration,gms_pure_go' -count=1 -run '^TestPRPreflight' ./scripts - - - name: Check native user config diagnostics - shell: bash - env: - CGO_ENABLED: "0" - GOCACHE: ${{ runner.temp }}/go-cache/non-race - run: bash scripts/ci/test-user-config-diagnostic.sh '${{ matrix.expected_goos }}' - - - name: Exercise test.sh prebuilt binary path - shell: bash - env: - GOCACHE: ${{ runner.temp }}/go-cache/non-race - run: go test '-tags=gms_pure_go' -count=1 -run '^TestTestScriptPrebuiltBinaryContract$' ./scripts - - # One go test of ./cmd/bd runs the generated hook timeout boundary and - # plain go test's own bd build fallback (BEADS_TEST_BD_BINARY unset), - # which Bazel never takes, on every OS developers run go test on: the - # tests tools/bazel/equivalence_allowlist.txt lists as skipped under - # Bazel. They were two steps; each go test of ./cmd/bd re-hashes its - # ~1500 packages and relinks the test binary, which cost 5-9s on - # Windows for a fallback run of under one second. - - name: Exercise generated Git hook timeout process boundary and go test's bd build fallback - shell: bash - env: - GOCACHE: ${{ runner.temp }}/go-cache/non-race - run: go test '-tags=gms_pure_go' -count=1 -run '^(TestGeneratedHookTimeoutProcessBoundary|TestGOMODCACHENotUnderTestHome|TestGoBuildBDCommandIsCWDIndependent)$' ./cmd/bd - - # This step recompiles ./cmd/bd's test binary. It must share the restored - # GOCACHE with the hook-timeout/bd-build-fallback step above (same tags, same platform - # default CGO_ENABLED, see the script's header) so it is a relink rather - # than a second cold compile: without GOCACHE here it fell back to the - # runner's empty default cache and cold-compiled all of ./cmd/bd on every - # PR (210-240s on macos-latest, ~120s on Windows; the leg's long pole). - # TestPRPreflightPlatformsRunStepsUseRestoredGoBuildCache pins this. - - name: Exercise native init gateway credential shell - shell: bash - env: - GOCACHE: ${{ runner.temp }}/go-cache/non-race - run: bash scripts/ci/test-init-gateway-credential.sh "$RUNNER_OS" - - - name: Exercise credential command fixtures on Windows - if: matrix.os == 'windows-latest' - shell: pwsh - env: - CGO_ENABLED: "0" - GOCACHE: ${{ runner.temp }}/go-cache/non-race - run: | - $go = (Get-Command go -CommandType Application -ErrorAction Stop | Select-Object -First 1).Source - $hostInfo = @(& $go env GOHOSTOS GOOS) - if ($LASTEXITCODE -ne 0 -or ($hostInfo -join ',') -ne 'windows,windows') { - throw 'Credential fixtures require native Windows Go' - } - $expected = @{ - 'github.com/steveyegge/beads/internal/testutil/credentialcmd' = @('TestProtocol') - 'github.com/steveyegge/beads/internal/creds' = @( - 'TestCommandSourceRealShell', 'TestCredentialCommandFixtureProtocol') - 'github.com/steveyegge/beads/internal/storage/dolt' = @( - 'TestApplyGatewayCredentialCommand', 'TestApplyGatewayCredentialJSONEnvelope', - 'TestApplyGatewayCredentialFailsClosed', 'TestApplyGatewayCredentialPresetWins', - 'TestApplyGatewayCredentialRejectsBadCharToken', 'TestApplyResolvedConfigGatewayCredential') - } - $selector = '^(' + (($expected.Values | ForEach-Object { $_ }) -join '|') + ')$' - $raw = & $go test -json -tags gms_pure_go -count=1 -run $selector @($expected.Keys) - $testStatus = $LASTEXITCODE - $events = @($raw | ForEach-Object { $_ | ConvertFrom-Json -ErrorAction Stop }) - $events | Where-Object Output | ForEach-Object { [Console]::Write($_.Output) } - if ($testStatus -ne 0) { exit $testStatus } - if (@($events | Where-Object { $_.Action -in 'fail', 'skip' }).Count -ne 0) { - throw 'Credential fixtures must pass without skips' - } - foreach ($package in $expected.Keys) { - foreach ($test in $expected[$package]) { - $passes = @($events | Where-Object { - $_.Package -eq $package -and $_.Test -eq $test -and $_.Action -eq 'pass' - }) - if ($passes.Count -ne 1) { throw "Expected one PASS for $package/$test" } - } - } - - - name: Check benchmark environment scrubbing - if: matrix.os == 'windows-latest' - shell: bash - env: - GOCACHE: ${{ runner.temp }}/go-cache/non-race - run: go test -tags gms_pure_go -count=1 -run '^(TestCleanEnvUsesHostKeySemantics|TestBenchmarkCommandBuildersStripDoltEnvOverrides)$' ./scripts/repro-dolt-prod-timeouts - - - name: Check shared environment key semantics - if: matrix.os == 'windows-latest' - shell: bash - env: - GOCACHE: ${{ runner.temp }}/go-cache/non-race - run: bash scripts/ci/test-windows-env-helpers.sh - - # F7a: fold of build-examples + complexity-report, the two pr.yml jobs that - # are advisory-only (not in ci-gate's needs/CI_GATE_REQUIRED) and restore no - # GitHub-saved build cache, onto one Blacksmith runner for same-repo - # PRs/merge_group. Both keep their pre-fold step-level - # continue-on-error/timeout-minutes design: a job-level timeout or failure - # would CANCEL or red the job, and a cancelled/red advisory check is exactly - # the blocking-by-a-different-door outcome both were written to avoid. A - # generous job-level timeout-minutes is still set, because - # TestSameRepoBlacksmithRunners requires one of every job it pins (so a - # stuck Blacksmith runner cannot spend minutes forever) - it sits comfortably - # above the sum of the step timeouts below so it is a backstop, not a - # realistic ceiling. - # # examples/ holds separate Go modules that reach the parent through a # `replace` directive, so their go.mod/go.sum record the parent's whole # dependency graph and go stale whenever the root module's does. Nothing in @@ -673,30 +84,36 @@ jobs: # modules failed a plain `go build` on main with "updates to go.mod needed". # Examples are the first code a new user copies; this keeps them buildable. # - # To promote the examples check to a real gate: drop its step's - # continue-on-error and warning step, then add `advisory-reports` to - # ci-gate's `needs`, `CI_GATE_REQUIRED`, and its env map. + # Advisory on purpose, and NOT in ci-gate's required list. # - # The complexity report is an advisory architecture signal; it deliberately - # does not participate in ci-gate while maintainers evaluate the baseline - # and refactoring payoff. - advisory-reports: - name: Advisory reports (examples build, complexity) - runs-on: ${{ (github.event_name == 'merge_group' || (github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository && github.actor != 'dependabot[bot]')) && 'blacksmith-2vcpu-ubuntu-2404' || 'ubuntu-latest' }} - # Review N-2 (2026-10-03): step timeouts sum to 42 min; 60 (not 45) keeps - # job setup/post overhead from turning a stacked near-timeout stall into a - # job-level cancel, which would red this advisory check through "another - # door" instead of the step that actually stalled. - timeout-minutes: 60 + # Because of the `replace` directive, any root go.mod change that is not + # mirrored into the examples fails this job. Whether that should block a merge + # is a maintainer call about contributor friction, so this PR does not make it + # one. Advisory has to be implemented at the STEP level: a job-level + # continue-on-error job still reports its check run as FAILURE in the PR + # rollup (only the workflow-run conclusion turns success), and pr-preflight + # and the pr-babysit merge patrol gate on per-check-run conclusions — so a + # job-level red here would stall the merge lane repo-wide while claiming to + # be optional. Step-level continue-on-error (the cygwin-leg precedent below) + # keeps the check run green and surfaces failures as warning annotations. + # + # To promote it to a real gate: drop the step's continue-on-error and the + # warning step, then add `build-examples` to ci-gate's `needs`, + # `CI_GATE_REQUIRED`, and its env map. + build-examples: + name: Build example modules + runs-on: ubuntu-latest + # No job-level timeout on purpose: a job-level timeout CANCELS the job, + # and a cancelled advisory job is a red check run — blocking again by a + # different door. Instead every step below carries its own + # timeout-minutes AND continue-on-error, so a hang or failure anywhere + # (setup download stall, cache stall, build hang) degrades to a warning + # annotation while the job itself always concludes green. steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 id: checkout continue-on-error: true timeout-minutes: 5 - with: - # complexity.sh diffs against origin/main; examples/ does not need - # history but shares this checkout. - fetch-depth: 0 - name: Set up Go id: setup-go @@ -732,145 +149,914 @@ jobs: run: | echo "::warning title=Example modules out of date::examples/ failed to type-check against this tree. Run 'go mod tidy' in each module under examples/ (see scripts/build-examples.sh output above). Advisory only; this does not block the merge." - - name: Warn when the examples build could not run at all + - name: Warn when the advisory job could not run at all if: steps.checkout.outcome != 'success' || steps.setup-go.outcome != 'success' run: | echo "::warning title=Example build check skipped::checkout or Go setup failed, so examples/ could not be checked this run (checkout: ${{ steps.checkout.outcome }}, setup-go: ${{ steps.setup-go.outcome }}). Advisory only; this does not block the merge." - - name: Install gocyclo - timeout-minutes: 5 - continue-on-error: true - run: go install github.com/fzipp/gocyclo/cmd/gocyclo@v0.6.0 + # Fast check: every `go build|test|run|generate|install` invocation in + # tracked scripts/hooks/CI carries -tags=gms_pure_go. Prevents ICU-linkage + # regressions from re-entering the build (see engdocs/ICU-POLICY.md). + check-build-tags: + name: Check build-tag policy + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + - run: ./scripts/check-build-tags.sh + - run: ./scripts/check-go-install-guidance.sh - - name: Generate complexity report - id: generate-complexity - if: always() - timeout-minutes: 8 - continue-on-error: true - run: | - set +e - COMPLEXITY_BASE_REF=origin/main ./scripts/ci/complexity.sh diff > complexity-report.txt 2>&1 - status=$? - if [ "$status" -ne 0 ]; then - echo "complexity report unavailable (exit $status); see log above" >> complexity-report.txt - fi - exit 0 + # Lock in two CGO-disabled portability boundaries. cmd/bd carries a mix of + # cgo-only test helpers (embedded Dolt, sql.DB) and pure-Go tests; compiling + # and running its pure-Go subset prevents test contamination (mybd-ycx / + # GH#3683 follow-up). internal/hooks also declares a js/wasm boundary where + # process execution must fail explicitly, so this required leaf job compiles + # the complete package test binary and executes that exact contract under + # Node rather than allowing a missing platform implementation to recur. + check-cmd-bd-puregeo-tests: + name: Check pure-Go and js/wasm boundaries (CGO_ENABLED=0) + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 - - name: Annotate unavailable complexity report - if: always() - timeout-minutes: 1 - continue-on-error: true + - name: Set up Go + uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 + with: + go-version-file: 'go.mod' + cache: false + + - name: Set up Node.js + uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v6 + with: + node-version: '24' + + - name: Run js/wasm hook boundary + env: + CGO_ENABLED: "0" + GOARCH: wasm + GOOS: js run: | - if [ ! -s complexity-report.txt ] || grep -q 'complexity report unavailable' complexity-report.txt; then - echo "::warning title=Complexity report unavailable::Advisory complexity analysis did not complete; inspect the artifact or job log." + set -euo pipefail + + # This required lane owns the exact js/wasm refusal contract. If the + # wasm test surface grows, widen this selector and its count guard + # together rather than assuming compilation executes new tests. + test_status=0 + test_output="$(go test -tags gms_pure_go -count=1 -timeout=2m \ + -exec="$(go env GOROOT)/lib/wasm/go_js_wasm_exec" \ + -run '^TestRunHookReportsUnsupportedExecution$' \ + -v ./internal/hooks 2>&1)" || test_status=$? + printf '%s\n' "$test_output" + (( test_status == 0 )) || exit "$test_status" + + run_count=0 + pass_count=0 + nonpass_count=0 + pass_pattern='^--- PASS: TestRunHookReportsUnsupportedExecution \([0-9.]+s\)$' + nonpass_pattern='^[[:space:]]*--- (FAIL|SKIP): ' + while IFS= read -r line; do + if [[ "$line" == "=== RUN TestRunHookReportsUnsupportedExecution" ]]; then + ((run_count += 1)) + fi + if [[ "$line" =~ $pass_pattern ]]; then + ((pass_count += 1)) + fi + if [[ "$line" =~ $nonpass_pattern ]]; then + ((nonpass_count += 1)) + fi + done <<< "$test_output" + if (( run_count != 1 || pass_count != 1 || nonpass_count != 0 )); then + printf 'expected one exact js/wasm hook test run and pass with no failures or skips; got runs=%s passes=%s nonpasses=%s\n' \ + "$run_count" "$pass_count" "$nonpass_count" >&2 + exit 1 fi - - name: Upload complexity report - if: always() - timeout-minutes: 3 - continue-on-error: true + - name: Build cmd/bd (CGO_ENABLED=0, gms_pure_go) + env: + CGO_ENABLED: "0" + run: go build -tags gms_pure_go -o /tmp/bd-puregeo ./cmd/bd + + - name: Compile pure-Go test binaries (CGO_ENABLED=0, gms_pure_go) + env: + CGO_ENABLED: "0" + run: | + go test -tags gms_pure_go -c -o /tmp/bd-cmd-puregeo-test ./cmd/bd + go test -tags gms_pure_go -c -o /tmp/bd-embeddeddolt-puregeo-test ./internal/storage/embeddeddolt + go test -tags gms_pure_go -c -o /tmp/bd-tracker-puregeo-test ./internal/tracker + + - name: Run pure-Go cmd/bd test subset (CGO_ENABLED=0) + env: + CGO_ENABLED: "0" + # Restrict to tests that exercise pure-Go code paths. Tests that + # need a Dolt store skip themselves at runtime when no test server + # is available; the goal here is to catch compile-time contamination, + # not run the full suite. + run: | + go test -tags gms_pure_go -count=1 -short -run '^Test(Help|CheckRemoteSafety|FormatDestroyToken|ShouldWireInitRemote|ExtractPrefix|IsNumericID|GetWorktreeGitDir|DriftItemStatuses|RunDriftChecks|IsServerProbablyRunning|CheckHooksDriftNotGitRepo|CheckServerDriftNoBeadsDir|CheckRemoteDriftNoBeadsDir)' ./cmd/bd + + # The lingering-handle liveness regression test is //go:build windows: the + # Ubuntu job above selects it by name but compiles it out, and main.yml's + # Windows job runs only on pushes to main (build/smoke, no go test). Run + # exactly the liveness pair on windows-latest so the regression guard + # actually executes at PR time. + test-windows-liveness: + name: Test (Windows) cmd/bd liveness regression + runs-on: windows-latest + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + + - name: Set up Go + id: setup-go + uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 + with: + go-version-file: 'go.mod' + cache: false + + - name: Run Windows liveness regression test + run: go test -tags gms_pure_go -count=1 -run '^TestIsServerProbablyRunning' ./cmd/bd + + # internal/doltversion carries Windows-specific branches (PATHEXT + # extension completion in Resolve, ERROR_BAD_EXE_FORMAT detection, the + # executable-bit carve-out) and its behavioral tests run there via .cmd + # stubs — but the Ubuntu test jobs compile those branches without ever + # executing them. Same pattern as the liveness job above: run exactly + # this package on windows-latest so the platform it targets actually + # exercises it at PR time. Small on purpose (~1 min). + test-windows-doltversion: + name: Test (Windows) doltversion package + runs-on: windows-latest + timeout-minutes: 15 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + + - name: Set up Go + id: setup-go + uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 + with: + go-version-file: 'go.mod' + cache: false + + - name: Run doltversion package tests on Windows + run: go test -tags gms_pure_go -count=1 ./internal/doltversion/ + + # Destructive worktree removal depends on native Windows path identity. + # This focused process suite requires NTFS per-directory case sensitivity + # and fails (rather than skips) when the runner cannot provide it. + worktree-remove-windows: + name: Worktree remove boundary (Windows) + runs-on: windows-latest + timeout-minutes: 15 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + + - name: Set up Go + id: setup-go + uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 + with: + go-version-file: 'go.mod' + cache: false + + - name: Restore Go module cache + uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 + with: + path: ~/go/pkg/mod + key: beads-go-mod-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-${{ hashFiles('go.mod', 'go.sum') }} + restore-keys: beads-go-mod-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}- + + - name: Restore non-race Go build cache + uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 + with: + path: ${{ runner.temp }}/go-cache/non-race + key: beads-go-build-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-base-gms_pure_go-non-race-${{ github.sha }} + restore-keys: beads-go-build-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-base-gms_pure_go-non-race- + + - name: Run native Windows worktree removal boundary tests + env: + CGO_ENABLED: "0" + GOCACHE: ${{ runner.temp }}/go-cache/non-race + run: go test -tags gms_pure_go -count=1 -run '^TestWorktreeRemove' ./cmd/bd + + # internal/storage/dbproxy/server carries the platform-portable path + # handling that #5820 introduced (testPlatformPath, external dolt config + # validation, DSN shaping), and until now no PR-triggered job executed the + # package on Windows - the platform those paths target - so a path-shaped + # regression stayed invisible until someone ran it by hand (the #5820 + # review merged on a contributor's windows/amd64 attestation for exactly + # that reason). Same pattern as the focused jobs above: run exactly this + # package on windows-latest. Tests that need a dolt binary Skipf loudly + # (dolt is not installed on the Windows runners), and the unix-socket + # cases skip on Windows by design - the portability class this job guards + # runs without either. + # + # Deliberately NOT added to ci-gate's `needs`/`CI_GATE_REQUIRED`/env map, + # following the `test-macos` precedent above: promoting a new job to + # required-for-merge is a maintainer call once it has run clean for a while. + # Until then it runs on every PR and surfaces red on the PR page without + # blocking the merge lane. Say the word and I will wire it into the gate in + # the same PR - the three edits are `ci-gate.needs`, `CI_GATE_REQUIRED`, and + # `TEST_WINDOWS_DBPROXY_SERVER: ${{ needs.test-windows-dbproxy-server.result }}`. + test-windows-dbproxy-server: + name: Test (Windows) dbproxy server package + runs-on: windows-latest + timeout-minutes: 15 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + + - name: Set up Go + id: setup-go + uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 + with: + go-version-file: 'go.mod' + cache: false + + - name: Run dbproxy server package tests on Windows + env: + CGO_ENABLED: "0" + run: go test -tags gms_pure_go -count=1 -v ./internal/storage/dbproxy/server/ + + # Fast check to ensure all version files are in sync + check-version-consistency: + name: Check version consistency + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + + - name: Check all versions match + run: ./scripts/check-versions.sh + + # Migration hygiene: duplicate version numbers, nondeterministic SQL + # (UUID()/NOW()/RAND(), the #4259 root-cause class), and edits to migration + # files that already exist on the base branch (shipped migrations are + # frozen; fix forward with a new version). Fails at PR time, before any + # test even compiles. Details: scripts/check-migration-hygiene.sh. + check-migration-hygiene: + name: Check migration hygiene + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + with: + fetch-depth: 0 + + - name: Run migration hygiene checks + env: + BASE_SHA: ${{ github.event.pull_request.base.sha || github.event.merge_group.base_sha }} + run: ./scripts/check-migration-hygiene.sh + + # Check documentation references match actual CLI flags + check-doc-flags: + name: Check doc flags freshness + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + with: + # Full history so check-cli-docs-drift.sh can regenerate docs at the + # merge-base and only fail PRs for drift they actually introduced. + fetch-depth: 0 + + - name: Set up Go + uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 + with: + go-version-file: 'go.mod' + cache: false + + - name: Build bd + run: CGO_ENABLED=0 go build -tags gms_pure_go -o bd ./cmd/bd/ + + - name: Validate docs against CLI + env: + DOC_DRIFT_PATCH_OUT: ${{ runner.temp }}/cli-docs-freshness.patch + run: | + ./scripts/check-doc-flags.sh ./bd + ./scripts/check-doc-freshness.sh + + # When the drift check fails, it writes the exact regenerated-docs fix + # (produced with CI's canonical build) so contributors can apply it with + # `git apply` instead of reproducing CI's environment locally. The + # docs-autofix.yml workflow_run job consumes this same artifact to push + # the fix to same-repo PR branches automatically. + - name: Upload docs fix patch + if: failure() uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7 with: - name: complexity-report - path: complexity-report.txt - retention-days: 7 - if-no-files-found: error + name: cli-docs-freshness-patch + path: ${{ runner.temp }}/cli-docs-freshness.patch + if-no-files-found: ignore - # `test-macos` / "Test (macos-latest)" used to run here and is deliberately - # removed (F4, PR CI -> 5 min). It was a non-required, PR-side mirror of - # main.yml's `test` job's macOS leg (full `go test -race -short ./...`), - # added after the 2026-08-01 incident where three separately-merged PRs each - # introduced a $TMPDIR=/var/folders regression that only reproduces on - # macOS and that main.yml's leg -- `if: github.event_name == 'push' && - # github.ref == 'refs/heads/main'`, i.e. push-to-main only -- could not have - # caught pre-merge. - # - # Removing it is an accepted tradeoff, not a coverage-neutral cleanup: - # required merge-gate coverage is unaffected (this job was never in - # ci-gate's needs/CI_GATE_REQUIRED), but PRs lose the one pre-merge signal - # for macOS-only `go test -race ./...` regressions of this kind; such a - # regression can land on main again and will only be caught by main.yml's - # push-triggered leg, after the fact. The other macOS matrix legs left in - # this file (check-doc-freshness-platforms, pr-preflight-platforms) run on - # every PR but exercise narrow, specific scripts, not the full suite, so - # they are not a substitute. If this tradeoff turns out to be wrong, - # reinstate a PR-side mirror rather than relying on main.yml alone. + check-doc-freshness-platforms: + name: Check doc freshness (${{ matrix.os }}) + runs-on: ${{ matrix.os }} + timeout-minutes: 10 + strategy: + fail-fast: false + matrix: + include: + - os: ubuntu-latest + expected_goos: linux + - os: macos-latest + expected_goos: darwin + - os: windows-latest + expected_goos: windows + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 - # F7a: fold of a 3-host matrix (native, msys2, cygwin) into one sequential - # job on a single windows-latest runner. Review SF-1 (2026-10-03): only the - # Cygwin *setup* step keeps `continue-on-error: true` (a flaky installer - # download is infrastructure noise, not a code failure). The native and - # msys2 install/exercise steps and the Cygwin *exercise* step carry no - # continue-on-error, so a genuine failure in any of them already fails the - # job on its own - no hand-written aggregator step needed. Each of those - # steps keeps `if: ${{ !cancelled() }}` (plus a check that its own leg's - # setup succeeded) so a failure in one leg does not stop the later legs - # from attempting to run - the fail-fast: false behavior the matrix used to - # give for free. - # Blacksmith Windows (blacksmith-4vcpu-windows-2025) for every PR. - # - # RISK (unverified from this sandbox, flagged in the F7b report): the - # native-Make step below assumes Chocolatey is preinstalled (`choco - # install ...`), as it is on GitHub's windows-latest image. Blacksmith's - # windows-2025 image is documented as GitHub's windows-2025 minus the full - # Visual Studio IDE/EdgeDriver/WinAppDriver; Chocolatey is a base-image - # tool unrelated to those, so it is expected to still be present, but this - # is not independently confirmed. The msys2/setup-msys2 and - # cygwin/cygwin-install-action steps download their own tooling and do not - # depend on any preinstalled image contents, so they carry no equivalent - # risk. If only the native-Make step fails here, that is the gap to roll - # back on (revert this runs-on line to the literal `windows-latest`). - windows-make-shell: - name: Windows Make shell (native, msys2, cygwin) - runs-on: ${{ 'blacksmith-4vcpu-windows-2025' }} + - name: Set up Go + id: setup-go + uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 + with: + go-version-file: 'go.mod' + cache: false + + - name: Restore Go module cache + uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 + with: + path: ~/go/pkg/mod + key: beads-go-mod-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-${{ hashFiles('go.mod', 'go.sum') }} + restore-keys: beads-go-mod-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}- + + - name: Exercise native date and Bash process boundary + run: go test '-tags=integration,gms_pure_go' -count=1 -run '^TestDocFreshness' ./scripts + + - name: Exercise repository text EOL policy boundary + run: go test '-tags=integration,gms_pure_go' -count=1 ./scripts/gitattributespolicy -args -required-host -expected-goos '${{ matrix.expected_goos }}' + + pr-preflight-platforms: + name: PR preflight process (${{ matrix.os }}) + runs-on: ${{ matrix.os }} + timeout-minutes: 20 + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest, macos-latest, windows-latest] + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + + - name: Set up Go + id: setup-go + uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 + with: + go-version-file: 'go.mod' + cache: false + + - name: Restore Go module cache + uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 + with: + path: ~/go/pkg/mod + key: beads-go-mod-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-${{ hashFiles('go.mod', 'go.sum') }} + restore-keys: beads-go-mod-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}- + + - name: Exercise the real Bash process boundary + shell: bash + run: go test '-tags=integration,gms_pure_go' -count=1 -run '^TestPRPreflight' ./scripts + + - name: Exercise test.sh prebuilt binary path + shell: bash + run: go test '-tags=gms_pure_go' -count=1 -run '^TestTestScriptPrebuiltBinaryContract$' ./scripts + + - name: Exercise generated Git hook timeout process boundary + shell: bash + run: go test '-tags=gms_pure_go' -count=1 -run '^TestGeneratedHookTimeoutProcessBoundary$' ./cmd/bd + + - name: Check benchmark environment scrubbing + if: matrix.os == 'windows-latest' + shell: bash + run: go test -tags gms_pure_go -count=1 -run '^(TestCleanEnvUsesHostKeySemantics|TestBenchmarkCommandBuildersStripDoltEnvOverrides)$' ./scripts/repro-dolt-prod-timeouts + + # Fast check to catch accidental .beads/issues.jsonl changes from contributors + check-no-beads-changes: + name: Check for .beads changes + runs-on: ubuntu-latest + if: github.event_name == 'pull_request' + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + with: + fetch-depth: 0 + + - name: Check for .beads/issues.jsonl changes + run: | + if git diff --name-only origin/${{ github.base_ref }}...HEAD | grep -q "^\.beads/issues\.jsonl$"; then + echo "This PR includes changes to .beads/issues.jsonl" + echo "" + echo "This file is the project's issue database and should not be modified in PRs." + echo "" + echo "To fix, run:" + echo " git checkout origin/main -- .beads/issues.jsonl" + echo " git commit --amend" + echo " git push --force" + echo "" + exit 1 + fi + echo "No .beads/issues.jsonl changes detected" + + detect-package-gates: + name: Detect package gates + runs-on: ubuntu-latest + outputs: + mcp_package: ${{ steps.detect.outputs.mcp_package }} + npm_package: ${{ steps.detect.outputs.npm_package }} + reason: ${{ steps.detect.outputs.reason }} + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + with: + fetch-depth: 0 + + - name: Decide package gates + id: detect + env: + PR_BASE_SHA: ${{ github.event.pull_request.base.sha }} + PR_HEAD_SHA: ${{ github.event.pull_request.head.sha }} + PUSH_BEFORE_SHA: ${{ github.event.before }} + PUSH_AFTER_SHA: ${{ github.sha }} + run: ./scripts/ci/detect-package-gates.sh + + package-mcp: + name: Package Gate (MCP) + runs-on: ubuntu-latest + needs: [detect-package-gates, build-artifacts] + steps: + - name: Check applicability + id: applicability + run: | + echo "run=${{ needs.detect-package-gates.outputs.mcp_package }}" >> "$GITHUB_OUTPUT" + if [[ "${{ needs.detect-package-gates.outputs.mcp_package }}" != "true" ]]; then + echo "MCP package gate is not applicable: ${{ needs.detect-package-gates.outputs.reason }}" + fi + + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + if: steps.applicability.outputs.run == 'true' + + - name: Set up Python + if: steps.applicability.outputs.run == 'true' + uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0 + with: + python-version: '3.14' + + - name: Install uv + if: steps.applicability.outputs.run == 'true' + run: python -m pip install uv==0.11.16 + + - name: Download build artifacts + if: steps.applicability.outputs.run == 'true' + uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8 + with: + name: ci-build-artifacts + path: ci-build-artifacts + + - name: Verify build artifacts + if: steps.applicability.outputs.run == 'true' + run: | + set -euo pipefail + cd ci-build-artifacts + sha256sum -c SHA256SUMS + chmod +x bd-linux-gms-pure + + - name: Run MCP package gate + if: steps.applicability.outputs.run == 'true' + env: + BEADS_TEST_BD_BINARY: ${{ github.workspace }}/ci-build-artifacts/bd-linux-gms-pure + run: make ci-package-mcp + + package-npm: + name: Package Gate (npm) + runs-on: ubuntu-latest + needs: [detect-package-gates, build-artifacts] + steps: + - name: Check applicability + id: applicability + run: | + echo "run=${{ needs.detect-package-gates.outputs.npm_package }}" >> "$GITHUB_OUTPUT" + if [[ "${{ needs.detect-package-gates.outputs.npm_package }}" != "true" ]]; then + echo "npm package gate is not applicable: ${{ needs.detect-package-gates.outputs.reason }}" + fi + + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + if: steps.applicability.outputs.run == 'true' + + - name: Set up Node.js + if: steps.applicability.outputs.run == 'true' + uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v6 + with: + node-version: '24' + + - name: Download build artifacts + if: steps.applicability.outputs.run == 'true' + uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8 + with: + name: ci-build-artifacts + path: ci-build-artifacts + + - name: Verify build artifacts + if: steps.applicability.outputs.run == 'true' + run: | + set -euo pipefail + cd ci-build-artifacts + sha256sum -c SHA256SUMS + chmod +x bd-linux-gms-pure + + - name: Run npm package gate + if: steps.applicability.outputs.run == 'true' + env: + BEADS_TEST_BD_BINARY: ${{ github.workspace }}/ci-build-artifacts/bd-linux-gms-pure + run: make ci-package-npm + + + pr-policy-wrapper: + name: PR Policy (wrapper timing) + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + with: + fetch-depth: 0 + + - name: Set up Go + uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 + with: + go-version-file: 'go.mod' + cache: false + + - name: Run PR policy wrapper + run: make ci-pr-policy + + pr-core-wrapper: + name: PR Core (wrapper timing) + runs-on: ubuntu-latest + needs: build-artifacts + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + + - name: Set up Go + id: setup-go + uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 + with: + go-version-file: 'go.mod' + cache: false + + - name: Restore Go module cache + uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 + with: + path: ~/go/pkg/mod + key: beads-go-mod-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-${{ hashFiles('go.mod', 'go.sum') }} + restore-keys: beads-go-mod-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}- + + - name: Restore race Go build cache + uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 + with: + path: ${{ runner.temp }}/go-cache/race + key: beads-go-build-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-base-gms_pure_go-race-${{ github.sha }} + restore-keys: beads-go-build-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-base-gms_pure_go-race- + + - name: Install Dolt + run: ./scripts/ci/install-dolt.sh + + - name: Configure Git and Dolt identity + run: | + git config --global user.name "CI Bot" + git config --global user.email "ci@beads.test" + dolt config --global --add user.name "CI Bot" + dolt config --global --add user.email "ci@beads.test" + + - name: Download build artifacts + uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8 + with: + name: ci-build-artifacts + path: ci-build-artifacts + + - name: Verify build artifacts + run: | + set -euo pipefail + cd ci-build-artifacts + sha256sum -c SHA256SUMS + chmod +x bd-linux-gms-pure + + - name: Run PR core wrapper + env: + BEADS_TEST_BD_BINARY: ${{ github.workspace }}/ci-build-artifacts/bd-linux-gms-pure + GOCACHE: ${{ runner.temp }}/go-cache/race + run: make ci-pr-core + + pr-lint-wrapper: + name: PR Lint (wrapper timing) + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + with: + # --new-from-merge-base below walks real history. + fetch-depth: 0 + + - name: Set up Go + uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 + with: + go-version-file: 'go.mod' + cache: false + + - name: Install golangci-lint + run: go install github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.10.1 + + # Scope the wrapper's lint to what this PR introduces, matching the + # `lint` job's only-new-issues below. main.yml runs the same wrapper with + # this unset, so the whole tree is still swept on every push to main. + # Both PR triggers (pull_request, merge_group) target main. + - name: Run PR lint wrapper + env: + BD_LINT_NEW_FROM_MERGE_BASE: origin/main + run: make ci-pr-lint + + # Focused job for the hexagonal storage layer (internal/storage/domain/* and + # internal/storage/uow). These packages back the proxied-server init path and + # also live behind the existing ./... matrix, but TestDomainDB requires the + # Dolt sql-server image cached locally (testcontainers checks `docker image + # inspect` and skips otherwise). Pulling the image up front guarantees the + # suite actually runs and surfaces failures here with a clear job name. + test-domain-uow: + name: Test (storage domain + uow) + runs-on: ubuntu-latest + needs: build-artifacts + # The race-enabled storage package has outgrown Go's implicit 10-minute + # timeout. Keep both per-package deadlines inside one finite job budget. timeout-minutes: 30 steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 - # F7b review fix (S9): print the preinstalled tool versions this job - # depends on before relying on them, so a Blacksmith image gap (see the - # RISK comment above) surfaces here instead of failing deep in a later - # step with an opaque "command not found". - - name: Check Windows tooling diagnostics - shell: pwsh + - name: Set up Go + uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 + with: + go-version-file: 'go.mod' + cache: false + + - name: Install Dolt CLI + run: ./scripts/ci/install-dolt.sh + + - name: Verify dolt on PATH + run: dolt version + + - name: Configure Git and Dolt identity run: | - Write-Output "::group::choco" - choco --version - Write-Output "::endgroup::" - Write-Output "::group::go env (CGO_ENABLED, CC)" - go env CGO_ENABLED CC - Write-Output "::endgroup::" + git config --global user.name "CI Bot" + git config --global user.email "ci@beads.test" + dolt config --global --add user.name "CI Bot" + dolt config --global --add user.email "ci@beads.test" + + - name: Pull Dolt sql-server image + # Keep tag in sync with internal/testutil/testdoltcommon.go:DoltDockerImage. + # Without this, RequireDoltContainer reports doltNoImage and TestDomainDB skips. + run: ./scripts/ci/pull-dolt-image.sh + + - name: Download build artifacts + uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8 + with: + name: ci-build-artifacts + path: ci-build-artifacts + + - name: Verify build artifacts + run: | + set -euo pipefail + cd ci-build-artifacts + sha256sum -c SHA256SUMS + chmod +x bd-linux-gms-pure + + - name: Test domain + uow + tracker + env: + BEADS_TEST_BD_BINARY: ${{ github.workspace }}/ci-build-artifacts/bd-linux-gms-pure + run: go test -tags gms_pure_go -race -count=1 -timeout 15m -v ./internal/storage/domain/... ./internal/storage/uow/... ./internal/tracker/... + + # The doctor/fix DB-backed suite spins its own Dolt container in + # TestMain. BEADS_FIX_REQUIRE_DOLT=1 turns a missing container into a + # hard failure so the suite can never silently skip in CI again + # (bd-nxt5e: it was dark for months); this job pulls the image above. + - name: Test doctor/fix (Dolt-backed, hard-require container) + env: + BEADS_TEST_BD_BINARY: ${{ github.workspace }}/ci-build-artifacts/bd-linux-gms-pure + BEADS_FIX_REQUIRE_DOLT: "1" + run: go test -tags gms_pure_go -race -count=1 -timeout 10m -v ./cmd/bd/doctor/fix/ + + # macOS-only regressions were landing on main undetected: main.yml's Test + # (macos-latest) leg only runs `if: github.event_name == 'push' && + # github.ref == 'refs/heads/main'`, so no PR ever exercises it. On + # 2026-08-01 that leg went red from three separately-merged PRs in a single + # day, all $TMPDIR=/var/folders symlink/path-depth consequences that only + # reproduce on macOS - none of the three could have seen the failure + # pre-merge, and the local bisect lane can't diagnose it either (Linux + # host, macOS-only failure). This job mirrors that leg's steps on every PR + # so the same class of regression fails here instead of on main. It is + # self-contained (no build-artifacts dependency): the macOS leg in main.yml + # never downloads or verifies the Linux build-artifacts bundle, it just + # builds and tests directly. + # + # Deliberately NOT continue-on-error: unlike the advisory legs elsewhere in + # this file that tolerate infrastructure flakiness, a red macOS test here is + # a real regression, and blocking the merge lane on it is the entire point. + # + # Deliberately NOT yet added to ci-gate's `needs`/`CI_GATE_REQUIRED`: + # promoting a new job to required-for-merge, once it has run clean for a + # while, is a separate maintainer call - same pattern as other jobs in this + # file that run on every PR without yet gating the merge. + test-macos: + name: Test (macos-latest) + runs-on: macos-latest + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + + - name: Set up Go + id: setup-go + uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 + with: + go-version-file: 'go.mod' + cache: false + + - name: Restore Go module cache + uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 + with: + path: ~/go/pkg/mod + key: beads-go-mod-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-${{ hashFiles('go.mod', 'go.sum') }} + restore-keys: beads-go-mod-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}- + + - name: Restore non-race Go build cache + uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 + with: + path: ${{ runner.temp }}/go-cache/non-race + key: beads-go-build-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-base-gms_pure_go-non-race-${{ github.sha }} + restore-keys: beads-go-build-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-base-gms_pure_go-non-race- + + - name: Restore race Go build cache + uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 + with: + path: ${{ runner.temp }}/go-cache/race + key: beads-go-build-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-base-gms_pure_go-race-${{ github.sha }} + restore-keys: beads-go-build-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-base-gms_pure_go-race- + + - name: Install Dolt (Linux/macOS) + run: ./scripts/ci/install-dolt.sh + + - name: Verify dolt on PATH + run: dolt version + + - name: Configure Git and Dolt identity + run: | + git config --global user.name "CI Bot" + git config --global user.email "ci@beads.test" + dolt config --global --add user.name "CI Bot" + dolt config --global --add user.email "ci@beads.test" + + - name: Build + env: + GOCACHE: ${{ runner.temp }}/go-cache/non-race + run: go build -v -tags gms_pure_go ./cmd/bd + + - name: Test + env: + BEADS_TEST_BD_BINARY: ${{ github.workspace }}/bd + GOCACHE: ${{ runner.temp }}/go-cache/race + # -timeout is explicit for the same reason it is in scripts/ci/pr-core.sh + # (#6091): ./cmd/bd has outgrown `go test`'s 10m per-package default. On + # this runner it lands between 357s and 600s across recent runs — #6091's + # own macOS lane passed at 596.7s, 3.3s under the wall — so the package + # intermittently dies on the alarm and the panic names whichever test was + # in flight, which reads like a hang instead of the clock running out. 30m + # matches every other -race lane here for this same code. + run: go test -tags gms_pure_go -v -race -short -timeout=30m -skip '^TestEmbedded' ./... + + # Producer-side guard for the Beads<->consumer CLI contract. Runs the whole + # cmd/bd/protocol package: the golden corpus (regenerated from this branch's + # bd and byte-compared to the committed testdata/corpus/, so an unreviewed + # wire change is a hard failure — run `make corpus-regen`), the double-run + # determinism check, and the TestProtocol_/TestJSONContract_ behavioral + # conformance suite that pins the CLI's JSON payloads and semantics. + # + # Run the PACKAGE, never a -run subset: a filter here is what let the + # conformance tests sit red on main for ~3 months while every CI run was + # green (they matched no job's -run, and every broad ./... job skips them + # for want of a Dolt store). Package scope means a newly added protocol test + # is gated the moment it lands. + # + # Needs the Dolt sql-server image (the protocol harness spins a container). + # ~10 min: each test builds an isolated Dolt-backed workspace. If that ever + # becomes unacceptable on PRs, shard this job by an exhaustive `go test + # -list` partition — do not reintroduce a hand-written -run regex. + contract-corpus: + name: Contract corpus (golden + determinism + conformance) + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + + - name: Set up Go + uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 + with: + go-version-file: 'go.mod' + cache: false + + - name: Install Dolt CLI + run: ./scripts/ci/install-dolt.sh + + - name: Configure Git and Dolt identity + run: | + git config --global user.name "CI Bot" + git config --global user.email "ci@beads.test" + dolt config --global --add user.name "CI Bot" + dolt config --global --add user.email "ci@beads.test" + + - name: Pull Dolt sql-server image + # Keep tag in sync with internal/testutil/testdoltcommon.go:DoltDockerImage. + run: ./scripts/ci/pull-dolt-image.sh + + - name: Protocol conformance + corpus golden + determinism + # Require a live Dolt store here: this is the gate that must exercise the + # conformance, golden and double-run checks, so a failed container is a + # hard failure, not a silent skip (see requireDoltStore in the + # cmd/bd/protocol tests). + # + # -timeout is explicit because the package runs well past `go test`'s + # 10m default; without it a slow runner reports a panic, not a verdict. + env: + BEADS_PROTOCOL_REQUIRE_DOLT: "1" + run: go test -tags gms_pure_go -count=1 -timeout 30m ./cmd/bd/protocol + + fmt-check: + name: Check formatting + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + + - name: Set up Go + uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 + with: + go-version-file: 'go.mod' + cache: false + + - name: Check gofmt + run: make fmt-check + + # Lint lanes are split deliberately: the PR lane reports only the issues a PR + # introduces, and main.yml's identical job sweeps the whole tree on every + # push. The tradeoff is that a pre-existing issue in untouched code no longer + # blocks a PR — but a lint word that lands on main now reds main's own run, + # where it belongs, instead of reddening every open PR for a change it did + # not make. + # + # "New" is measured off the diff, so MOVED CODE READS AS NEW: a pre-existing + # violation carried into a PR by a move or a rename is reported against that + # PR and blocks it. Fix it or //nolint it there — that is the accepted cost of + # the trade above. + lint: + name: Lint + runs-on: ubuntu-latest + permissions: + contents: read + # only-new-issues reads the PR patch from the API. + pull-requests: read + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + with: + # On merge_group there is no PR patch, so the action falls back to + # --new-from-rev and needs real history. + fetch-depth: 0 + + - name: Set up Go + uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 + with: + go-version-file: 'go.mod' + cache: false + + # golangci/golangci-lint-action is not on this repository's allowed-actions + # list, so a workflow that names it fails at startup. Install the pinned + # release binary (the same script main uses) and report only issues new + # relative to the PR base, as only-new-issues did. + - name: Install golangci-lint + run: ./scripts/ci/install-golangci-lint.sh + + - name: golangci-lint + env: + GOWORK: "off" + NEW_FROM_REV: ${{ github.event.pull_request.base.sha || github.event.merge_group.base_sha }} + run: >- + golangci-lint run --config=.golangci.yml --modules-download-mode=readonly + --timeout=5m --build-tags=gms_pure_go --new-from-rev="$NEW_FROM_REV" + + windows-make-shell: + name: Windows Make shell (${{ matrix.host }}) + runs-on: windows-latest + strategy: + fail-fast: false + matrix: + host: + - native + - msys2 + - cygwin + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 - name: Install native GNU Make - id: install-native - if: ${{ !cancelled() }} + if: matrix.host == 'native' shell: pwsh run: choco install make --version=4.4.1 --yes --no-progress - # F7b review fix (B1): msys2/setup-msys2 caches its installed packages - # by default (action.yml: cache.default = true), which means this step - # would SAVE an actions/cache entry from a same-repo PR the moment it - # moved onto a Blacksmith runner -- confirmed via a real GitHub cache - # list entry (msys2-pkgs-upd:false-conf:...) with a - # refs/pull/.../merge ref. `runner.environment == 'github-hosted'` - # disables that implicit save on Blacksmith (fail-closed: only the - # literal "github-hosted" value keeps caching on, matching the - # fail-closed convention F7c review fix N1 set for actions/setup-go's - # own `cache` input). Pinned by - # TestBlacksmithReachablePRJobsDisableDefaultCachingActions. - name: Set up MSYS2 GNU Make - id: install-msys2 - if: ${{ !cancelled() }} - timeout-minutes: 10 + if: matrix.host == 'msys2' uses: msys2/setup-msys2@66cd2cce69caa17b53920067426061ca1de3a884 # v2.32.0 with: msystem: MSYS install: git make path-type: minimal - cache: ${{ runner.environment == 'github-hosted' }} - name: Set up Cygwin GNU Make - if: ${{ !cancelled() }} + if: matrix.host == 'cygwin' id: cygwin # cygwin-install-action downloads setup-x86_64.exe from cygwin.com. That # download times out often enough to red the required check for a reason @@ -878,25 +1064,20 @@ jobs: # (non-blocking) warning: the smoke step below is gated on this step # succeeding, so a genuine Cygwin Make regression still fails loudly, # while a flaky installer download only skips the leg with a warning. - # Review SF-2 (2026-10-03): a 10-minute step timeout caps how long a - # hung download can block the native/msys2 legs' exercise steps - # behind it, instead of consuming the whole 30-minute job timeout. continue-on-error: true - timeout-minutes: 10 uses: cygwin/cygwin-install-action@3f0a3f9f988f7e96b8c18098ae05eaec175f5b52 # v6 with: packages: git make add-to-path: 'false' - name: Warn when Cygwin setup was skipped by a download failure - if: ${{ !cancelled() && steps.cygwin.outcome == 'failure' }} + if: matrix.host == 'cygwin' && steps.cygwin.outcome == 'failure' shell: bash run: | echo "::warning title=Cygwin Make-shell leg skipped::cygwin-install-action could not download the Cygwin installer (network flake); skipping the Cygwin-hosted Make smoke test. This is an infrastructure failure, not a code failure." - name: Exercise native Windows Make - id: native - if: ${{ !cancelled() && steps.install-native.outcome == 'success' }} + if: matrix.host == 'native' shell: pwsh run: | $ErrorActionPreference = 'Stop' @@ -967,63 +1148,14 @@ jobs: $proofRoot = Join-Path $env:RUNNER_TEMP ("make install proof " + [guid]::NewGuid().ToString('N')) $fixtureRepo = Join-Path $proofRoot 'fixture repo' $buildDir = Join-Path $fixtureRepo 'build output' - $fakeGoDir = Join-Path $proofRoot 'fake go bin' $installRoot = Join-Path $proofRoot 'install sandbox' $installProfile = Join-Path $installRoot 'profile with spaces' - New-Item -ItemType Directory -Force -Path $buildDir, $fakeGoDir, $installProfile | Out-Null + New-Item -ItemType Directory -Force -Path $buildDir, $installProfile | Out-Null Copy-Item -LiteralPath (Join-Path $env:GITHUB_WORKSPACE 'Makefile') -Destination $fixtureRepo Copy-Item -LiteralPath (Join-Path $env:GITHUB_WORKSPACE 'go.mod') -Destination $fixtureRepo $sentinel = Join-Path $buildDir 'bd.exe' - $fakeGo = Join-Path $fakeGoDir 'go' - $fakeGoScript = @' - #!/usr/bin/env bash - set -euo pipefail - - [[ "${1-}" == build ]] || exit 91 - shift - output= - while (($#)); do - case "$1" in - -o) - [[ $# -ge 2 ]] || exit 92 - output=$2 - shift 2 - ;; - *) shift ;; - esac - done - [[ "$(cygpath -aw "$output")" == "$(cygpath -aw "$BEADS_FAKE_GO_EXPECTED_OUTPUT")" ]] || { - printf 'fake go received output %q, expected %q\n' "$output" "$BEADS_FAKE_GO_EXPECTED_OUTPUT" >&2 - exit 93 - } - printf native-build-sentinel > "$(cygpath -u "$output")" - '@ - if (-not $fakeGoScript.StartsWith('#!', [StringComparison]::Ordinal)) { - throw 'Fake Go shebang must start at byte zero after YAML extraction' - } - [IO.File]::WriteAllText( - $fakeGo, - ($fakeGoScript -replace "`r`n", "`n"), - [Text.UTF8Encoding]::new($false) - ) - $priorPath = $env:PATH - Push-Location $fixtureRepo - try { - $env:BEADS_FAKE_GO_EXPECTED_OUTPUT = $sentinel - $env:PATH = "$fakeGoDir;$priorPath" - & $make --no-print-directory "BUILD_DIR=$buildDir" GIT_BUILD=build-proof CC=proof build - if ($LASTEXITCODE -ne 0) { - throw 'Native Make build failed for a spaced BUILD_DIR' - } - } finally { - $env:PATH = $priorPath - Remove-Item -LiteralPath Env:BEADS_FAKE_GO_EXPECTED_OUTPUT - Pop-Location - } - if (-not (Test-Path -LiteralPath $sentinel -PathType Leaf)) { - throw "Native Make did not build bd.exe at the exact spaced destination '$sentinel'" - } + Set-Content -LiteralPath $sentinel -Encoding ascii -NoNewline -Value 'native-install-sentinel' $sentinelHash = (Get-FileHash -LiteralPath $sentinel -Algorithm SHA256).Hash & $git -C $fixtureRepo init --quiet @@ -1057,179 +1189,24 @@ jobs: throw 'Native Make installed bd.exe with unexpected contents' } - $expectedFirstInstall = @( + $expectedInstall = @( 'D profile with spaces' 'D profile with spaces/.local' 'D profile with spaces/.local/bin' "F profile with spaces/.local/bin/bd.exe $sentinelHash" ) | Sort-Object - $actualFirstInstall = @(Get-TreeManifest -Root $installRoot) - if (Compare-Object -ReferenceObject $expectedFirstInstall -DifferenceObject $actualFirstInstall) { - throw "Native Make changed paths outside the exact first install destination:`n$($actualFirstInstall -join "`n")" + $actualInstall = @(Get-TreeManifest -Root $installRoot) + if (Compare-Object -ReferenceObject $expectedInstall -DifferenceObject $actualInstall) { + throw "Native Make changed paths outside the exact install destination:`n$($actualInstall -join "`n")" } - $fixtureAfterFirstInstall = @(Get-TreeManifest -Root $fixtureRepo) - if (Compare-Object -ReferenceObject $fixtureBefore -DifferenceObject $fixtureAfterFirstInstall) { - throw 'Native Make split the spaced first-install path or otherwise mutated the fixture repository' + $fixtureAfter = @(Get-TreeManifest -Root $fixtureRepo) + if (Compare-Object -ReferenceObject $fixtureBefore -DifferenceObject $fixtureAfter) { + throw 'Native Make split the spaced install path or otherwise mutated the fixture repository' } - $legacyAlias = Join-Path $installProfile '.local\bin\BD' - Set-Content -LiteralPath $legacyAlias -Encoding ascii -NoNewline -Value 'legacy-extensionless-alias' - $installBeforeFailedReplacement = @(Get-TreeManifest -Root $installRoot) - $fixtureBeforeFailedReplacement = @(Get-TreeManifest -Root $fixtureRepo) - $missingBuildDir = Join-Path $fixtureRepo 'missing build output' - if (Test-Path -LiteralPath $missingBuildDir) { - throw "Failure-injection source unexpectedly exists: '$missingBuildDir'" - } - - Push-Location $fixtureRepo - try { - & $make --no-print-directory -o build "BUILD_DIR=$missingBuildDir" GIT_BUILD=install-proof install-force - if ($LASTEXITCODE -eq 0) { - throw 'Native Make replacement unexpectedly succeeded without a source bd.exe' - } - } finally { - Pop-Location - } - - $installAfterFailedReplacement = @(Get-TreeManifest -Root $installRoot) - if (Compare-Object -ReferenceObject $installBeforeFailedReplacement -DifferenceObject $installAfterFailedReplacement) { - throw "Failed native Make replacement changed the live install tree:`n$($installAfterFailedReplacement -join "`n")" - } - $fixtureAfterFailedReplacement = @(Get-TreeManifest -Root $fixtureRepo) - if (Compare-Object -ReferenceObject $fixtureBeforeFailedReplacement -DifferenceObject $fixtureAfterFailedReplacement) { - throw 'Failed native Make replacement mutated the fixture repository' - } - - Set-Content -LiteralPath $sentinel -Encoding ascii -NoNewline -Value 'native-install-sentinel-v2' - $replacementHash = (Get-FileHash -LiteralPath $sentinel -Algorithm SHA256).Hash - $fixtureBeforeReplacement = @(Get-TreeManifest -Root $fixtureRepo) - - Push-Location $fixtureRepo - try { - & $make --no-print-directory -o build "BUILD_DIR=$buildDir" GIT_BUILD=install-proof install-force - if ($LASTEXITCODE -ne 0) { - throw 'Native Make replacement install-force failed with a case-variant legacy alias' - } - } finally { - Pop-Location - } - - if (Test-Path -LiteralPath $legacyAlias) { - throw "Native Make left the case-variant legacy alias '$legacyAlias'" - } - if (-not (Test-Path -LiteralPath $installed -PathType Leaf)) { - throw "Native Make replacement left no bd.exe at '$installed'" - } - if ((Get-FileHash -LiteralPath $installed -Algorithm SHA256).Hash -ne $replacementHash) { - throw 'Native Make replacement installed bd.exe with unexpected contents' - } - - $expectedReplacement = @( - 'D profile with spaces' - 'D profile with spaces/.local' - 'D profile with spaces/.local/bin' - "F profile with spaces/.local/bin/bd.exe $replacementHash" - ) | Sort-Object - $actualReplacement = @(Get-TreeManifest -Root $installRoot) - if (Compare-Object -ReferenceObject $expectedReplacement -DifferenceObject $actualReplacement) { - throw "Native Make replacement changed paths outside the exact install destination:`n$($actualReplacement -join "`n")" - } - - $fixtureAfterReplacement = @(Get-TreeManifest -Root $fixtureRepo) - if (Compare-Object -ReferenceObject $fixtureBeforeReplacement -DifferenceObject $fixtureAfterReplacement) { - throw 'Native Make split the spaced replacement path or otherwise mutated the fixture repository' - } - - $junctionRoot = Join-Path $proofRoot 'junction install sandbox' - $junctionProfile = Join-Path $junctionRoot 'profile with spaces' - $junctionParent = Join-Path $junctionProfile '.local' - $junctionTarget = Join-Path $junctionRoot 'bin target with spaces' - New-Item -ItemType Directory -Force -Path $junctionParent, $junctionTarget | Out-Null - $junctionBin = Join-Path $junctionParent 'bin' - New-Item -ItemType Junction -Path $junctionBin -Target $junctionTarget | Out-Null - $junctionLegacyAlias = Join-Path $junctionTarget 'bd' - Set-Content -LiteralPath $junctionLegacyAlias -Encoding ascii -NoNewline -Value 'junction-legacy-alias' - Set-Content -LiteralPath $sentinel -Encoding ascii -NoNewline -Value 'native-install-sentinel-v3' - $junctionHash = (Get-FileHash -LiteralPath $sentinel -Algorithm SHA256).Hash - $fixtureBeforeJunctionInstall = @(Get-TreeManifest -Root $fixtureRepo) - - $env:USERPROFILE = $junctionProfile - Push-Location $fixtureRepo - try { - & $make --no-print-directory -o build "BUILD_DIR=$buildDir" GIT_BUILD=install-proof install-force - if ($LASTEXITCODE -ne 0) { - throw 'Native Make install-force failed through a junction install directory' - } - } finally { - Pop-Location - } - - if (Test-Path -LiteralPath $junctionLegacyAlias) { - throw "Native Make left the legacy alias behind the install junction '$junctionLegacyAlias'" - } - $junctionInstalled = Join-Path $junctionTarget 'bd.exe' - $junctionVisibleInstalled = Join-Path $junctionBin 'bd.exe' - foreach ($path in @($junctionInstalled, $junctionVisibleInstalled)) { - if (-not (Test-Path -LiteralPath $path -PathType Leaf)) { - throw "Native Make junction install left no bd.exe at '$path'" - } - if ((Get-FileHash -LiteralPath $path -Algorithm SHA256).Hash -ne $junctionHash) { - throw "Native Make junction install wrote unexpected contents at '$path'" - } - } - $expectedJunctionTarget = @("F bd.exe $junctionHash") - $actualJunctionTarget = @(Get-TreeManifest -Root $junctionTarget) - if (Compare-Object -ReferenceObject $expectedJunctionTarget -DifferenceObject $actualJunctionTarget) { - throw "Native Make junction install left unexpected target entries:`n$($actualJunctionTarget -join "`n")" - } - - $fixtureAfterJunctionInstall = @(Get-TreeManifest -Root $fixtureRepo) - if (Compare-Object -ReferenceObject $fixtureBeforeJunctionInstall -DifferenceObject $fixtureAfterJunctionInstall) { - throw 'Native Make junction install mutated the fixture repository' - } - - $directoryAlias = Join-Path $installProfile '.local/bin/BD' - New-Item -ItemType Directory -Path $directoryAlias | Out-Null - $directorySentinel = Join-Path $directoryAlias 'keep.txt' - Set-Content -LiteralPath $directorySentinel -Encoding ascii -NoNewline -Value 'preserve-bd-directory' - $directorySentinelHash = (Get-FileHash -LiteralPath $directorySentinel -Algorithm SHA256).Hash - Set-Content -LiteralPath $sentinel -Encoding ascii -NoNewline -Value 'native-install-sentinel-v4' - $directoryInstallHash = (Get-FileHash -LiteralPath $sentinel -Algorithm SHA256).Hash - $fixtureBeforeDirectoryInstall = @(Get-TreeManifest -Root $fixtureRepo) - - $env:USERPROFILE = $installProfile - Push-Location $fixtureRepo - try { - & $make --no-print-directory -o build "BUILD_DIR=$buildDir" GIT_BUILD=install-proof install-force - if ($LASTEXITCODE -ne 0) { - throw 'Native Make install-force failed with a directory named BD' - } - } finally { - Pop-Location - } - - $expectedDirectoryInstall = @( - 'D profile with spaces' - 'D profile with spaces/.local' - 'D profile with spaces/.local/bin' - 'D profile with spaces/.local/bin/BD' - "F profile with spaces/.local/bin/BD/keep.txt $directorySentinelHash" - "F profile with spaces/.local/bin/bd.exe $directoryInstallHash" - ) | Sort-Object - $actualDirectoryInstall = @(Get-TreeManifest -Root $installRoot) - if (Compare-Object -ReferenceObject $expectedDirectoryInstall -DifferenceObject $actualDirectoryInstall) { - throw "Native Make changed the preserved BD directory or install tree:`n$($actualDirectoryInstall -join "`n")" - } - $fixtureAfterDirectoryInstall = @(Get-TreeManifest -Root $fixtureRepo) - if (Compare-Object -ReferenceObject $fixtureBeforeDirectoryInstall -DifferenceObject $fixtureAfterDirectoryInstall) { - throw 'Native Make install with a BD directory mutated the fixture repository' - } - Write-Output 'Native Make install proof passed: first install, failed copy, case-variant alias, junction, preserved BD directory' - - name: Exercise MSYS2-hosted Make - id: msys2 - if: ${{ !cancelled() && steps.install-msys2.outcome == 'success' }} + if: matrix.host == 'msys2' shell: msys2 {0} run: | set -euo pipefail @@ -1270,12 +1247,11 @@ jobs: make --no-print-directory -f "$profile/smoke.mk" windows-make-shell-smoke - name: Exercise Cygwin-hosted Make - id: cygwin-test # Only run when the Cygwin toolchain actually installed; a skipped install # (download flake) leaves this leg un-exercised rather than failing on a # missing bash. A real install success followed by a Make regression here # still fails the job as before. - if: ${{ !cancelled() && steps.cygwin.outcome == 'success' }} + if: matrix.host == 'cygwin' && steps.cygwin.outcome == 'success' shell: pwsh env: CYGWIN_ROOT: ${{ steps.cygwin.outputs.root }} @@ -1328,75 +1304,32 @@ jobs: throw 'Cygwin Make shell smoke failed' } - # The Bazel lanes (bazel.yml): the only pre-merge run of every Linux Go - # test tier (PR core, pure-Go and js/wasm, the release cross-compile, the - # dolt-server lane, the embedded-Dolt, proxied-server and server-Dolt - # storage tiers that pr-risk.yml used to run, integration and the cmd/bd - # Dolt-server tier). - # ci-gate requires each of them to have run remotely on rbe-west and - # passed, on every pull_request and merge group: same-repo PRs and merge - # groups in mode remote, fork and Dependabot PRs through rbe-west's mint - # (rbe-fork, modes fork-ro/fork-rw). Mode skip (the farm switch off) or - # cache (no secret, or rbe-fork closed or refusing) is red, never a silent - # pass with fewer tests. bazel.yml has no pull_request/merge_group trigger - # of its own: this call is its only PR run. It gets the four RBE secrets - # and the pre-warm app key and nothing else. Test results are cached: a PR - # or merge group re-runs only tests whose inputs changed (nightly re-runs - # everything; .bazelrc test:fresh). Its bd artifact - # (bazel-ci-build-artifacts) is kept for debugging; F3: the package gates - # build their own bd via this call's rbe job. - bazel: - name: Bazel - # A called workflow's job gets at most this call's token permissions, and - # GitHub checks them when the run starts, even for a job whose if: skips - # it (startup_failure otherwise; gascity #7478): bazel.yml's rrc-seed asks - # id-token: write (push to main only) and rrc-verify issues: write - # (schedule and dispatch only). Neither runs for this event, every other - # bazel.yml job keeps contents: read, and rbe-west's mint refuses any OIDC - # token but a main-push bazel.yml one - # (scripts/bazel_rrc_test.go, TestBazelWorkflowCallersGrantEveryRequestedPermission). - permissions: - contents: read - id-token: write - issues: write - uses: ./.github/workflows/bazel.yml - secrets: - RBE_WEST_EXECUTOR: ${{ secrets.RBE_WEST_EXECUTOR }} - RBE_TLS_CERT: ${{ secrets.RBE_TLS_CERT }} - RBE_TLS_KEY: ${{ secrets.RBE_TLS_KEY }} - RBE_TLS_CA: ${{ secrets.RBE_TLS_CA }} - RBE_POOL_APP_PRIVATE_KEY: ${{ secrets.RBE_POOL_APP_PRIVATE_KEY }} - with: - build-artifact-name: bazel-ci-build-artifacts - integration: "on" - # Must equal BAZEL_CMD_DOLT_REQUIRED (top of this file; policy-tested): - # true leaves //cmd/bd:bd_test to the then-required bazel-cmd-dolt. - cmd-dolt-required: true - # F3: only pr.yml opts in; the package-mcp/package-npm jobs run inside - # this call, needing only its rbe job. - package-gates: "on" - ci-gate: name: CI Gate / Required - # F3: same-repo PRs and merge_group get the org's Blacksmith pool; forks, - # Dependabot and any other event stay GitHub-hosted. This job restores no - # GitHub-saved cache. Must match the TestSameRepoBlacksmithRunners pin; - # the gate's NAME must never change (ruleset requires exactly "CI Gate / - # Required"). - runs-on: ${{ (github.event_name == 'merge_group' || (github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository && github.actor != 'dependabot[bot]')) && 'blacksmith-2vcpu-ubuntu-2404' || 'ubuntu-latest' }} - # F3: now on Blacksmith for same-repo PRs; a hang used to burn only the - # default 360 minutes of a free GitHub-hosted runner, now Blacksmith - # minutes too. - timeout-minutes: 10 + runs-on: ubuntu-latest needs: - - fast-checks + - build-artifacts + - check-build-tags + - check-cmd-bd-puregeo-tests - test-windows-liveness - worktree-remove-windows + - check-version-consistency + - check-migration-hygiene - check-doc-flags - check-doc-freshness-platforms - pr-preflight-platforms + - check-no-beads-changes + - detect-package-gates + - package-mcp + - package-npm + - pr-policy-wrapper + - pr-core-wrapper + - pr-lint-wrapper + - test-domain-uow + - contract-corpus + - fmt-check + - lint - windows-make-shell - - bazel if: ${{ always() }} steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 @@ -1405,103 +1338,54 @@ jobs: env: CI_GATE_NAME: PR baseline gate CI_GATE_REQUIRED: >- - FAST_CHECKS + BUILD_ARTIFACTS + CHECK_BUILD_TAGS + CHECK_CMD_BD_PUREGEO_TESTS + TEST_WINDOWS_LIVENESS + WORKTREE_REMOVE_WINDOWS + CHECK_VERSION_CONSISTENCY CHECK_MIGRATION_HYGIENE - CHECK_NO_BEADS_CHANGES CHECK_DOC_FLAGS CHECK_DOC_FRESHNESS_PLATFORMS PR_PREFLIGHT_PLATFORMS + CHECK_NO_BEADS_CHANGES + DETECT_PACKAGE_GATES PACKAGE_MCP PACKAGE_NPM - TEST_WINDOWS_LIVENESS - WORKTREE_REMOVE_WINDOWS + PR_POLICY_WRAPPER + PR_CORE_WRAPPER + PR_LINT_WRAPPER + TEST_DOMAIN_UOW + CONTRACT_CORPUS + FMT_CHECK + LINT WINDOWS_MAKE_SHELL - BAZEL - BAZEL_TEST - BAZEL_PURE - BAZEL_RELEASE_CROSS - BAZEL_EMBEDDED - BAZEL_INTEGRATION - BAZEL_DOLTSERVER - BAZEL_PROXIED - BAZEL_SERVER_STORAGE - BAZEL_REMOTE - # fast-checks' two base-diff steps; each token reads that step's - # outcome (via the job's outputs), with a 'skipped' fallback for a - # lost job (see FAST_CHECKS below). TestPRCIGateFastChecksTokens - # pins this mapping. The repository guards the job used to run - # (build tags, versions, migration checks A and B, gofmt) are Bazel - # tests and gate through BAZEL_TEST. - CHECK_MIGRATION_HYGIENE: ${{ needs.fast-checks.outputs.migration-hygiene || 'skipped' }} - CHECK_NO_BEADS_CHANGES: ${{ needs.fast-checks.outputs.no-beads-changes || 'skipped' }} - # Backstop: if fast-checks itself is lost (e.g. a cancelled runner) - # before any step sets an output, the two tokens above read - # 'skipped' (neither is in CI_GATE_SKIPPED_OK, so the gate still - # reds) - FAST_CHECKS gives ci-gate.sh one clear line naming the - # job. - FAST_CHECKS: ${{ needs.fast-checks.result }} + BUILD_ARTIFACTS: ${{ needs.build-artifacts.result }} + CHECK_BUILD_TAGS: ${{ needs.check-build-tags.result }} + CHECK_CMD_BD_PUREGEO_TESTS: ${{ needs.check-cmd-bd-puregeo-tests.result }} TEST_WINDOWS_LIVENESS: ${{ needs.test-windows-liveness.result }} WORKTREE_REMOVE_WINDOWS: ${{ needs.worktree-remove-windows.result }} + CHECK_VERSION_CONSISTENCY: ${{ needs.check-version-consistency.result }} + CHECK_MIGRATION_HYGIENE: ${{ needs.check-migration-hygiene.result }} CHECK_DOC_FLAGS: ${{ needs.check-doc-flags.result }} CHECK_DOC_FRESHNESS_PLATFORMS: ${{ needs.check-doc-freshness-platforms.result }} PR_PREFLIGHT_PLATFORMS: ${{ needs.pr-preflight-platforms.result }} - # F3: package-mcp/package-npm now run inside the bazel call; their - # job.status surfaces as a call output exactly like the Bazel lanes - # below, never "skipped" on pr.yml (package-gates is always "on" - # here). - PACKAGE_MCP: ${{ needs.bazel.outputs.package-mcp || 'skipped' }} - PACKAGE_NPM: ${{ needs.bazel.outputs.package-npm || 'skipped' }} + CHECK_NO_BEADS_CHANGES: ${{ needs.check-no-beads-changes.result }} + DETECT_PACKAGE_GATES: ${{ needs.detect-package-gates.result }} + PACKAGE_MCP: ${{ needs.package-mcp.result }} + PACKAGE_NPM: ${{ needs.package-npm.result }} + PR_POLICY_WRAPPER: ${{ needs.pr-policy-wrapper.result }} + PR_CORE_WRAPPER: ${{ needs.pr-core-wrapper.result }} + PR_LINT_WRAPPER: ${{ needs.pr-lint-wrapper.result }} + TEST_DOMAIN_UOW: ${{ needs.test-domain-uow.result }} + CONTRACT_CORPUS: ${{ needs.contract-corpus.result }} + FMT_CHECK: ${{ needs.fmt-check.result }} + LINT: ${{ needs.lint.result }} WINDOWS_MAKE_SHELL: ${{ needs.windows-make-shell.result }} - # bazel.yml's lanes report their job.status as outputs; a lane that - # did not report (skipped, or lost before its last step) reads as - # skipped, which passes only in the modes bazel-gate.sh allows. - BAZEL_TEST: ${{ needs.bazel.outputs.bazel-test || 'skipped' }} - BAZEL_PURE: ${{ needs.bazel.outputs.bazel-pure || 'skipped' }} - BAZEL_RELEASE_CROSS: ${{ needs.bazel.outputs.bazel-release-cross || 'skipped' }} - BAZEL_EMBEDDED: ${{ needs.bazel.outputs.bazel-embedded || 'skipped' }} - BAZEL_INTEGRATION: ${{ needs.bazel.outputs.bazel-integration || 'skipped' }} - BAZEL_DOLTSERVER: ${{ needs.bazel.outputs.bazel-doltserver || 'skipped' }} - BAZEL_PROXIED: ${{ needs.bazel.outputs.bazel-proxied || 'skipped' }} - BAZEL_SERVER_STORAGE: ${{ needs.bazel.outputs.bazel-server-storage || 'skipped' }} - # Advisory unless BAZEL_CMD_DOLT_REQUIRED (top of this file) is - # "true": see the run script. - BAZEL_CMD_DOLT: ${{ needs.bazel.outputs.bazel-cmd-dolt || 'skipped' }} - # Inputs of bazel-gate.sh, which sets BAZEL (the call's aggregate, - # red on a missing or inconsistent mode) and the expected skips - # from bazel.yml's own rbe job decision. - BAZEL_CALL: ${{ needs.bazel.result }} - BAZEL_RBE_MODE: ${{ needs.bazel.outputs.rbe-mode }} - BAZEL_RBE_ENABLED: ${{ needs.bazel.outputs.rbe-enabled }} run: | skipped_ok="" - # The cmd/bd Dolt-server tier (bazel.yml's bazel-cmd-dolt) is - # advisory until BAZEL_CMD_DOLT_REQUIRED (top of this file) is - # "true"; it is never in the static list above. - if [[ "$BAZEL_CMD_DOLT_REQUIRED" == "true" ]]; then - CI_GATE_REQUIRED="$CI_GATE_REQUIRED BAZEL_CMD_DOLT" + if [[ "$GITHUB_EVENT_NAME" == "merge_group" ]]; then + skipped_ok="CHECK_NO_BEADS_CHANGES" fi - export CI_GATE_REQUIRED - # Bazel lanes skipped by design in the call's execution mode (all - # of them in mode skip, the remote-only embedded, proxied and - # server-storage tiers in modes local and cache, and integration - # in mode local), and the aggregate unless the mode is missing or - # invalid. - skipped_ok="$skipped_ok $(bash .github/scripts/bazel-gate.sh skips)" - BAZEL="$(bash .github/scripts/bazel-gate.sh aggregate)" - export BAZEL - # The Bazel lanes are the only run of every Linux Go test tier - # (there is no legacy job to fall back to), so on every pull_request - # and merge group they must have run remotely on rbe-west: mode - # remote, or fork-ro/fork-rw through rbe-fork for a PR without the - # CI secrets; mode skip (the farm switch off) or cache (no - # secret, or rbe-fork closed or refusing) is red whatever - # bazel-gate.sh's skip list allows; each lane's own result is - # required above. - BAZEL_REMOTE=success - if [[ ! "$BAZEL_RBE_MODE" =~ ^(remote|fork-ro|fork-rw)$ ]]; then - BAZEL_REMOTE=not-run-remotely - echo "::error::The Bazel lanes did not run remotely on rbe-west (mode '${BAZEL_RBE_MODE:-none}'); they are this PR's only run of the Linux Go test tiers. In mode cache on a PR without the CI secrets, rbe-fork was closed or refused this run: re-run all jobs later." - fi - export BAZEL_REMOTE export CI_GATE_SKIPPED_OK="$skipped_ok" bash .github/scripts/ci-gate.sh diff --git a/.github/workflows/proxied-local-smoke.yml b/.github/workflows/proxied-local-smoke.yml new file mode 100644 index 000000000..047f1aa6e --- /dev/null +++ b/.github/workflows/proxied-local-smoke.yml @@ -0,0 +1,150 @@ +# Managed-local proxied lifecycle lane. +# +# The proxied shards in main.yml exercise command behavior against an +# EXTERNALLY managed Dolt testcontainer. This lane covers the production +# local-first path those shards cannot: bd launches its own loopback proxy, +# the proxy launches a locally installed `dolt sql-server`, and the whole +# lifecycle (create/read, loopback-only listeners, idle shutdown, +# transparent restart with persistence), identity failures, orphan recovery, +# and start/stop convergence run INSIDE a network namespace with only +# loopback up — proving no outbound network is needed once the binaries are +# installed. Linux first; the test lane defines Windows/macOS parity but does +# not implement those OS-specific observation helpers yet. + +name: Proxied Local Lifecycle + +on: + workflow_dispatch: + merge_group: + push: + branches: [main] + paths: + - 'cmd/bd/proxied_local_*' + - 'cmd/bd/proxied_server*.go' + - 'cmd/bd/dolt*.go' + - 'cmd/bd/*_proxied_server.go' + - 'cmd/bd/backup*.go' + - 'cmd/bd/purge*.go' + - 'cmd/bd/db_proxy_child.go' + - 'cmd/bd/init*.go' + - 'cmd/bd/proxied_integration_helpers_test.go' + - 'cmd/bd/store_factory*.go' + - 'cmd/bd/test_dolt_server_cgo_test.go' + - 'cmd/bd/uow_factory.go' + - 'internal/configfile/**' + - 'internal/doltserver/**' + - 'internal/storage/dbproxy/**' + - 'internal/storage/uow/**' + - 'go.mod' + - 'go.sum' + - '.github/workflows/proxied-local-smoke.yml' + pull_request: + paths: + - 'cmd/bd/proxied_local_*' + - 'cmd/bd/proxied_server*.go' + - 'cmd/bd/dolt*.go' + - 'cmd/bd/*_proxied_server.go' + - 'cmd/bd/backup*.go' + - 'cmd/bd/purge*.go' + - 'cmd/bd/db_proxy_child.go' + - 'cmd/bd/init*.go' + - 'cmd/bd/proxied_integration_helpers_test.go' + - 'cmd/bd/store_factory*.go' + - 'cmd/bd/test_dolt_server_cgo_test.go' + - 'cmd/bd/uow_factory.go' + - 'internal/configfile/**' + - 'internal/doltserver/**' + - 'internal/storage/dbproxy/**' + - 'internal/storage/uow/**' + - 'go.mod' + - 'go.sum' + - '.github/workflows/proxied-local-smoke.yml' + +permissions: + contents: read + +jobs: + managed-local-smoke: + name: Managed-local proxied lifecycle (Linux, offline) + runs-on: ubuntu-latest + timeout-minutes: 25 + env: + CGO_ENABLED: "1" + # Pinned so the lane tests a known dolt CLI, not whatever the installer + # resolves on a given day. Bump deliberately, and only in lockstep with + # the measured pin in scripts/ci/install-dolt.sh (see "Which Dolt + # version to install" in docs/architecture/dolt.md). + DOLT_VERSION: 2.2.0 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + + - name: Set up Go + uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 + with: + go-version-file: 'go.mod' + + - name: Install pinned Dolt CLI + run: | + curl -fsSL "https://github.com/dolthub/dolt/releases/download/v${DOLT_VERSION}/dolt-linux-amd64.tar.gz" -o /tmp/dolt.tar.gz + tar -xzf /tmp/dolt.tar.gz -C /tmp + sudo install /tmp/dolt-linux-amd64/bin/dolt /usr/local/bin/dolt + dolt version + + - name: Build bd and compile the test binary (online) + run: | + go build -tags gms_pure_go -o /tmp/bd-under-test ./cmd/bd + go test -tags gms_pure_go -c -o /tmp/proxied-local-smoke.test ./cmd/bd + + - name: Assert the managed-local lifecycle tests are compiled in + run: | + listed="$( + BEADS_TEST_PROXIED_LOCAL=1 \ + BEADS_TEST_SKIP=dolt \ + /tmp/proxied-local-smoke.test \ + -test.list '^TestManagedLocalProxied' + )" + for test_name in \ + TestManagedLocalProxiedLifecycleSmoke \ + TestManagedLocalProxiedForeignListenerNotAdopted \ + TestManagedLocalProxiedReusedPidNeverKilled \ + TestManagedLocalProxiedLegacyRecordFailClosed \ + TestManagedLocalProxiedOrphanBackendReaped \ + TestManagedLocalProxiedNProcessColdStartConvergence \ + TestManagedLocalProxiedStopStartInterleave \ + TestManagedLocalProxiedDoltStatusReportsLiveTopology \ + TestManagedLocalProxiedDoltStartRefusesOverLiveProxy \ + TestManagedLocalProxiedDoltStartRefusesWithProxyDown \ + TestManagedLocalProxiedDoltLifecycleLeavesOtherTopologiesAlone \ + TestManagedLocalProxiedPurgeWispsPlaneRetention \ + TestManagedLocalProxiedCapabilityMatrix \ + TestManagedLocalProxiedBackupRoundTrip \ + TestManagedLocalProxiedAutoBackup \ + TestManagedLocalProxiedBackupRestoreRefusedWhileAttached \ + TestManagedLocalProxiedBackendCleanExitRetiresProxy + do + if ! grep -qx "$test_name" <<<"$listed"; then + echo "managed-local lifecycle test $test_name is missing; got: '$listed'" >&2 + exit 1 + fi + done + + - name: Run managed-local lifecycle lane offline (loopback-only netns) + run: | + sudo env \ + "PATH=$PATH" \ + BEADS_TEST_PROXIED_LOCAL=1 \ + BEADS_TEST_SKIP=dolt \ + BEADS_TEST_BD_BINARY=/tmp/bd-under-test \ + unshare --net -- bash -euxc ' + ip link set lo up + # Prove the namespace really has no outbound network before + # trusting anything the test reports. + if timeout 3 bash -c "exec 3<>/dev/tcp/1.1.1.1/443" 2>/dev/null; then + echo "outbound network unexpectedly available inside the netns" >&2 + exit 1 + fi + cd cmd/bd + /tmp/proxied-local-smoke.test \ + -test.run "^TestManagedLocalProxied" \ + -test.v -test.timeout 15m + ' diff --git a/.github/workflows/regression.yml b/.github/workflows/regression.yml new file mode 100644 index 000000000..c3eec84bb --- /dev/null +++ b/.github/workflows/regression.yml @@ -0,0 +1,121 @@ +name: Regression Tests + +on: + # Run on merges to main + push: + branches: [main] + pull_request: + branches: [main, 'release/**', 'hotfix/**'] + # Allow manual trigger + workflow_dispatch: + +env: + BD_DISABLE_METRICS: "1" + BD_DISABLE_EVENT_FLUSH: "1" + +jobs: + detect-regression-need: + name: Detect regression need + runs-on: ubuntu-latest + outputs: + run_regression: ${{ steps.detect.outputs.run_regression }} + reason: ${{ steps.detect.outputs.reason }} + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + with: + fetch-depth: 0 + + - name: Decide whether to run differential regression tests + id: detect + env: + EVENT_NAME: ${{ github.event_name }} + PR_BASE_SHA: ${{ github.event.pull_request.base.sha }} + PR_HEAD_SHA: ${{ github.event.pull_request.head.sha }} + run: | + set -euo pipefail + + if [[ "$EVENT_NAME" != "pull_request" ]]; then + echo "run_regression=true" >> "$GITHUB_OUTPUT" + echo "reason=$EVENT_NAME event always runs regression tests" >> "$GITHUB_OUTPUT" + exit 0 + fi + + labels="$(jq -r '.pull_request.labels[].name // empty' "$GITHUB_EVENT_PATH")" + if grep -Fxq "run-regression" <<<"$labels"; then + echo "run_regression=true" >> "$GITHUB_OUTPUT" + echo "reason=run-regression label present" >> "$GITHUB_OUTPUT" + exit 0 + fi + if grep -Fxq "skip-regression" <<<"$labels"; then + echo "run_regression=false" >> "$GITHUB_OUTPUT" + echo "reason=skip-regression label present" >> "$GITHUB_OUTPUT" + exit 0 + fi + + changed="$(git diff --name-only "$PR_BASE_SHA" "$PR_HEAD_SHA")" + echo "Changed files:" + printf '%s\n' "$changed" + + regression_candidates="$( + printf '%s\n' "$changed" | + grep -Ev '^(cmd/bd|internal/storage|internal/types)/.*_test\.go$' || + true + )" + + risky_pattern='^(\.github/workflows/regression\.yml|Makefile|go\.(mod|sum)|cmd/bd/|internal/storage/|internal/types/|tests/regression/)' + if grep -Eq "$risky_pattern" <<<"$regression_candidates"; then + echo "run_regression=true" >> "$GITHUB_OUTPUT" + echo "reason=PR changes CLI, storage, types, regression, or build inputs" >> "$GITHUB_OUTPUT" + exit 0 + fi + + echo "run_regression=false" >> "$GITHUB_OUTPUT" + echo "reason=PR does not touch regression-sensitive paths" >> "$GITHUB_OUTPUT" + + regression: + name: Differential Regression (v0.49.6 baseline) + needs: detect-regression-need + if: needs.detect-regression-need.outputs.run_regression == 'true' + runs-on: ubuntu-latest + timeout-minutes: 25 + steps: + - name: Show regression trigger reason + run: echo "${{ needs.detect-regression-need.outputs.reason }}" + + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 + + - name: Set up Go + uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 + with: + go-version-file: 'go.mod' + + - name: Configure Git + run: | + git config --global user.name "CI Bot" + git config --global user.email "ci@beads.test" + + - name: Install Dolt + run: ./scripts/ci/install-dolt.sh + + - name: Configure Dolt + run: | + dolt config --global --add user.name "CI Bot" + dolt config --global --add user.email "ci@beads.test" + dolt version + + - name: Pull Dolt sql-server image + # Without this, isDoltImageCached() returns false in TestMain and the + # regression suite falls back to embedded Dolt with HOME=, which + # has a known cleanup race (see beads issue: TestCreateWithParentFlag + # TempDir RemoveAll "directory not empty" flake). Keep the tag in sync + # with internal/testutil/testdoltcommon.go:DoltDockerImage. + run: ./scripts/ci/pull-dolt-image.sh + + - name: Cache baseline binary + uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v5 + with: + path: ~/.cache/beads-regression + key: regression-baseline-${{ hashFiles('tests/regression/BASELINE_VERSION') }} + + - name: Run regression tests + run: go test -tags=regression,gms_pure_go -timeout=20m -v ./tests/regression/... diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 80685e4d5..157c09478 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -35,14 +35,18 @@ jobs: with: fetch-depth: 0 - - name: Set up Go - uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 - with: - go-version-file: 'go.mod' - cache: false + - name: Verify tag matches version.go + run: | + tag_version="${GITHUB_REF_NAME#v}" + code_version="$(grep 'Version = ' cmd/bd/version.go | sed 's/.*"\(.*\)".*/\1/')" + if [ "$tag_version" != "$code_version" ]; then + echo "ERROR: tag v$tag_version does not match cmd/bd/version.go ($code_version)" >&2 + exit 1 + fi + echo "OK: tag and version.go agree on $code_version" - - name: Verify tag and released versions - run: ./scripts/check-versions.sh --expect "${GITHUB_REF_NAME#v}" + - name: Check version consistency + run: ./scripts/check-versions.sh release-package-mcp: name: Release Package Gate (MCP) @@ -57,10 +61,6 @@ jobs: uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 with: go-version-file: 'go.mod' - # Never restore a cache into release builds: setup-go's key is - # predictable and falls back to main's cache, whose module and build - # caches are not re-verified before binaries are built and signed. - cache: false - name: Set up Python uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0 @@ -94,10 +94,6 @@ jobs: uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 with: go-version-file: 'go.mod' - # Never restore a cache into release builds: setup-go's key is - # predictable and falls back to main's cache, whose module and build - # caches are not re-verified before binaries are built and signed. - cache: false - name: Set up Node.js uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v6 @@ -125,10 +121,6 @@ jobs: uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 with: go-version-file: 'go.mod' - # Never restore a cache into release builds: setup-go's key is - # predictable and falls back to main's cache, whose module and build - # caches are not re-verified before binaries are built and signed. - cache: false - name: Install cross-compilation toolchains and signing tools # Backstop for a hang that escapes the inner `timeout` calls, so it has @@ -237,10 +229,6 @@ jobs: uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 with: go-version-file: 'go.mod' - # Never restore a cache into release builds: setup-go's key is - # predictable and falls back to main's cache, whose module and build - # caches are not re-verified before binaries are built and signed. - cache: false - name: Extract version from tag id: version diff --git a/.github/workflows/remove-needs-info.yml b/.github/workflows/remove-needs-info.yml deleted file mode 100644 index 0cde73337..000000000 --- a/.github/workflows/remove-needs-info.yml +++ /dev/null @@ -1,65 +0,0 @@ -name: Remove needs-info on author response - -on: - issue_comment: - types: [created] - pull_request_target: - types: [synchronize] - -permissions: {} - -jobs: - # pull_request_target is safe here because this job never checks out or runs - # pull request code; it only removes labels from the issue/PR metadata. - remove-label: - runs-on: ubuntu-latest - permissions: - issues: write - pull-requests: write - steps: - - name: Remove needs-info / needs-repro on author response - uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8 - with: - script: | - const labels = ['status/needs-info', 'status/needs-repro']; - - let number, author; - if (context.eventName === 'issue_comment') { - number = context.payload.issue.number; - author = context.payload.issue.user.login; - if (context.payload.comment.user.login !== author) return; - } else { - number = context.payload.pull_request.number; - author = context.payload.pull_request.user.login; - if (context.payload.sender.login !== author) return; - } - - const current = await github.rest.issues.listLabelsOnIssue({ - owner: context.repo.owner, - repo: context.repo.repo, - issue_number: number, - }); - const currentNames = current.data.map(l => l.name); - - for (const label of labels) { - if (!currentNames.includes(label)) continue; - // Idempotent removal: concurrent triggers can race to remove the - // same label, leaving the loser with a 404 "Label does not - // exist". Treat an already-absent label as success rather than - // failing the job. - try { - await github.rest.issues.removeLabel({ - owner: context.repo.owner, - repo: context.repo.repo, - issue_number: number, - name: label, - }); - console.log(`Removed '${label}' from #${number}`); - } catch (err) { - if (err.status === 404) { - console.log(`'${label}' already absent from #${number} (concurrent removal); nothing to do`); - continue; - } - throw err; - } - } diff --git a/.github/workflows/remove-needs-triage.yml b/.github/workflows/remove-needs-triage.yml deleted file mode 100644 index 1a5a7b3ba..000000000 --- a/.github/workflows/remove-needs-triage.yml +++ /dev/null @@ -1,61 +0,0 @@ -name: Remove needs-triage when triaged - -on: - issues: - types: [labeled] - pull_request_target: - types: [labeled] - -permissions: {} - -jobs: - # pull_request_target is safe here because this job never checks out or runs - # pull request code; it only removes labels from the issue/PR metadata. - remove-triage-label: - # Olivia owns label, comment, and inbox removal as one recoverable transaction. - if: github.event.sender.login != 'gascityinc-sarah[bot]' - runs-on: ubuntu-latest - permissions: - issues: write - pull-requests: write - steps: - - name: Remove needs-triage when a non-status label is added - uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8 - with: - script: | - const added = context.payload.label.name; - if (added.startsWith('status/')) return; - - const number = context.issue?.number || context.payload.pull_request?.number; - const target = 'status/needs-triage'; - - const current = await github.rest.issues.listLabelsOnIssue({ - owner: context.repo.owner, - repo: context.repo.repo, - issue_number: number, - }); - - if (!current.data.some(l => l.name === target)) return; - - // The check above narrows a race but cannot close it: adding - // several non-status labels at once fires one `labeled` event - // (and one job) each, and they run concurrently. Every run sees - // the label present here, then all race to remove it. The winner - // gets 204; the losers get 404 "Label does not exist". Treat an - // already-absent label as success so a benign lost race does not - // paint a red X on the PR's checks. - try { - await github.rest.issues.removeLabel({ - owner: context.repo.owner, - repo: context.repo.repo, - issue_number: number, - name: target, - }); - console.log(`Removed '${target}' from #${number} (triaged with '${added}')`); - } catch (err) { - if (err.status === 404) { - console.log(`'${target}' already absent from #${number} (concurrent removal); nothing to do`); - return; - } - throw err; - } diff --git a/.github/workflows/request-needs-info.yml b/.github/workflows/request-needs-info.yml deleted file mode 100644 index 9a561ae04..000000000 --- a/.github/workflows/request-needs-info.yml +++ /dev/null @@ -1,82 +0,0 @@ -name: Request repro or info from reporter - -on: - issues: - types: [labeled] - -permissions: {} - -jobs: - post-request: - runs-on: ubuntu-latest - permissions: - issues: write - steps: - - name: Post visible request comment for needs-repro or needs-info - uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8 - with: - script: | - const needsLabels = ['status/needs-info', 'status/needs-repro']; - const label = context.payload.label?.name; - if (!needsLabels.includes(label)) return; - - const number = context.payload.issue?.number; - if (!number) { - core.setFailed('Unable to determine issue number'); - return; - } - - const owner = context.repo.owner; - const repo = context.repo.repo; - const reporter = context.payload.issue?.user?.login; - - const events = await github.paginate(github.rest.issues.listEvents, { - owner, - repo, - issue_number: number, - per_page: 100, - }); - const labelEvents = events.filter(e => e.event === 'labeled' && e.label?.name === label); - const labeledAt = labelEvents.length - ? new Date(Math.max(...labelEvents.map(e => new Date(e.created_at).getTime()))) - : null; - - const existingComments = await github.paginate(github.rest.issues.listComments, { - owner, - repo, - issue_number: number, - ...(labeledAt ? { since: labeledAt.toISOString() } : {}), - per_page: 100, - }); - - const hasVisibleRequest = existingComments.some(c => { - if (reporter && c.user?.login === reporter) return false; - const lower = (c.body || '').toLowerCase(); - if (lower.includes('closing')) return false; - if (!/14\s*-?\s*days?/.test(lower)) return false; - if (!lower.includes('reply') && !lower.includes('respond')) return false; - if (label === 'status/needs-repro') return lower.includes('repro') || lower.includes('reproduction'); - return lower.includes('info') || lower.includes('information') || lower.includes('details'); - }); - - if (hasVisibleRequest) { - console.log(`Visible request already exists for #${number}, skipping.`); - return; - } - - const requestBodies = { - 'status/needs-repro': - 'Thanks for the report! To help us investigate, please reply with a minimal reproduction within 14 days.\n\n' + - 'If there is no response in that window, this issue may be closed — but you are welcome to respond or reopen it at any time.', - 'status/needs-info': - 'Thanks for the report! To help us investigate, please reply with the requested information or details within 14 days.\n\n' + - 'If there is no response in that window, this issue may be closed — but you are welcome to respond or reopen it at any time.', - }; - - await github.rest.issues.createComment({ - owner, - repo, - issue_number: number, - body: requestBodies[label], - }); - console.log(`Posted needs request for #${number} (${label})`); diff --git a/.github/workflows/triage-label.yml b/.github/workflows/triage-label.yml deleted file mode 100644 index 99c8807ff..000000000 --- a/.github/workflows/triage-label.yml +++ /dev/null @@ -1,41 +0,0 @@ -name: Auto-label new issues and PRs - -on: - issues: - types: [opened, reopened] - pull_request_target: - types: [opened, reopened, ready_for_review] - -permissions: {} - -jobs: - # pull_request_target is safe here because this job never checks out or runs - # pull request code; it only labels the issue/PR from event metadata. - add-triage-label: - runs-on: ubuntu-latest - permissions: - issues: write - pull-requests: write - steps: - - name: Add needs-triage label - uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8 - with: - script: | - const pullRequest = context.payload.pull_request; - if (pullRequest?.draft) { - console.log(`Skipping draft PR #${pullRequest.number}`); - return; - } - - const number = context.issue?.number || pullRequest?.number; - if (!number) { - core.setFailed('Unable to determine issue or PR number'); - return; - } - - await github.rest.issues.addLabels({ - owner: context.repo.owner, - repo: context.repo.repo, - issue_number: number, - labels: ['status/needs-triage'] - }); diff --git a/.github/workflows/update-flake-lock.yml b/.github/workflows/update-flake-lock.yml index 2f6541f5e..8051941e9 100644 --- a/.github/workflows/update-flake-lock.yml +++ b/.github/workflows/update-flake-lock.yml @@ -30,7 +30,7 @@ jobs: token: ${{ steps.flake-token.outputs.token }} - name: Install Nix - uses: DeterminateSystems/determinate-nix-action@527f17dd63d2d60d3e5552934bc84b9a33a14d11 # v3 + uses: DeterminateSystems/determinate-nix-action@61cbfe2efc2d4e7a8a6d56967c3c1058e846c858 # v3 - name: Update flake.lock run: nix flake update diff --git a/.github/workflows/update-vendor-hash.yml b/.github/workflows/update-vendor-hash.yml index 8ad1b449f..f9a35547d 100644 --- a/.github/workflows/update-vendor-hash.yml +++ b/.github/workflows/update-vendor-hash.yml @@ -1,13 +1,10 @@ name: Update vendorHash for dependabot Go bumps # Dependabot bumps to go.mod/go.sum invalidate the proxyVendor FOD hash in -# default.nix, which fails the required PR Risk `test-nix` gate (F7c, -# spec-f7.md §2.4: nix-build.yml itself no longer runs on pull_request, since -# test-nix's `nix run .#default` plus `nix flake check -L`, which evaluates -# every flake output, is a superset of plain `nix build .#default`). This workflow detects the drift, computes the -# new hash via scripts/update-nix-vendorhash.sh, and pushes the update back to -# the dependabot branch so the gate goes green without a maintainer -# round-trip. +# default.nix, which fails the nix-build CI gate. This workflow detects the +# drift, computes the new hash via scripts/update-nix-vendorhash.sh, and +# pushes the update back to the dependabot branch so the gate goes green +# without a maintainer round-trip. # # Tracked under beads-bz5. # @@ -31,9 +28,9 @@ name: Update vendorHash for dependabot Go bumps # # Caveat: # GitHub does not retrigger pull_request workflow runs for commits -# authored by GITHUB_TOKEN. After this workflow pushes, the existing PR Risk -# `test-nix` check on this PR will remain stuck on the pre-fix commit. -# Maintainers either re-run test-nix manually, or this workflow's own +# authored by GITHUB_TOKEN. After this workflow pushes, the existing +# nix-build.yml check on this PR will remain stuck on the pre-fix commit. +# Maintainers either re-run nix-build manually, or this workflow's own # success acts as the green signal that the hash is correct. on: @@ -69,7 +66,7 @@ jobs: token: ${{ secrets.GITHUB_TOKEN }} - name: Install Nix - uses: DeterminateSystems/determinate-nix-action@527f17dd63d2d60d3e5552934bc84b9a33a14d11 # v3 + uses: DeterminateSystems/determinate-nix-action@61cbfe2efc2d4e7a8a6d56967c3c1058e846c858 # v3 - name: Capture pre-update vendorHash id: pre @@ -82,14 +79,6 @@ jobs: - name: Update vendorHash run: ./scripts/update-nix-vendorhash.sh - - name: Verify the build independently of the script's exit status - # update-nix-vendorhash.sh's own verification build (its step 5) is - # internal to the script; this step re-derives the same signal from - # a source the workflow doesn't have to trust: a plain `nix build` - # that this job step fails on if it fails, regardless of what the - # script itself reported. - run: nix build .#default - - name: Detect whether hash changed id: changed run: | diff --git a/.github/workflows/worker-env-autosync.yml b/.github/workflows/worker-env-autosync.yml deleted file mode 100644 index 2978a5617..000000000 --- a/.github/workflows/worker-env-autosync.yml +++ /dev/null @@ -1,196 +0,0 @@ -# Keep beads' rbe worker-env pin in step with gastownhall/gascity main -# without the ~100-minute wait #7419 fixed by hand. -# -# rbe-west's OSS pool is gascity's: its workers advertise the sha256 of the -# toolchain manifest gascity's tools/rbe/worker-env measures, and the -# schedulers match //platforms:rbe_worker's worker-env exec property -# exactly. beads' tools/rbe/worker-env.txt and the platforms/BUILD.bazel pin -# are a byte-for-byte copy of gascity main's; when gascity moves the pin (a -# Go, dolt, or OS/library bump -- #7387 was a Go security release), every -# beads remote-execution run stops in bazel.yml's rbe-job preflight -# (tools/rbe/worker-env-sync) until the copy catches up. nightly.yml's -# rbe-worker-env-sync job already opens a ci/infra issue for a human to fix; -# this instead opens the fix itself, every 15 minutes, so the gap is -# minutes, not until someone reads the issue. -# -# tools/rbe/worker-env-sync only detects the mismatch (exit 1) or that it -# could not compare with gascity (exit 2, e.g. GitHub is briefly -# unreachable; the next run retries). The write half, tools/rbe/worker-env- -# sync-copy, runs only after that exit-1 detection, on a checkout already in -# hand -- so a transient gascity outage can turn "in step" into "unknown", -# never into an unwanted write. -# -# TWO TOKENS, ONE JOB EACH: GitHub never runs checks on a PR a plain -# GITHUB_TOKEN opens (its loop-prevention rule), so this needs a GitHub App -# token to open the PR. The gastownhall-autofix App (contents:write only on -# this repo) mints push-token and does the git push of the fixed branch -# (bot/worker-env-sync); it cannot open, label or merge a PR. The separate -# gascity-autofix App (pull-requests:write only, no contents) mints pr-token -# and does everything PR-shaped: create/update, the status/needs-review-auto -# label, and arming auto-merge. Splitting them means a bug in the PR-shaped -# half can never push a commit, and a bug in the push half can never touch a -# PR; neither token can do the other's job even by accident. Both Apps' -# credentials are secrets of the same `autofix` environment. -# -# peter-evans/create-pull-request takes one token for the whole push+PR -# flow, so it cannot honor this split; this uses plain git for the push and -# gh for the PR instead. Every `${{ }}` is resolved into a step's `env:`, -# never written inside a `run:` script, so a value that happened to look -# like shell (a title or body containing `` ` `` or `$`) is data to the -# shell, not something GitHub's expression syntax or the shell re-parses. -# -# SECURITY: schedule and workflow_dispatch are never fork events (no -# pull_request trigger here at all), so neither App's private key -- secrets -# of the `autofix` environment, whose deployment-branch policy allows only -# main -- is ever readable by a fork. The guard on github.repository is -# defense in depth for a fork of this repo, where the secrets would not -# exist anyway. Both mint steps run only once worker-env-sync has already -# found a real mismatch, so a run that is in step or can't compare asks -# neither App for a token. gh pr merge --auto only arms GitHub's native -# auto-merge (it still waits for every required check and the branch -# protection rule); it does not merge anything itself. -name: rbe worker-env autosync - -on: - schedule: - - cron: "*/15 * * * *" - workflow_dispatch: - -permissions: - contents: read - -jobs: - autosync: - name: Sync worker-env pin with gascity main - if: github.repository == 'gastownhall/beads' - runs-on: ubuntu-latest - timeout-minutes: 10 - # Grants the job read access to this environment's secrets (both Apps' - # id and private key); its deployment-branch policy restricts that to - # jobs running on main, which schedule and workflow_dispatch on this - # repo always are. - environment: autofix - permissions: - contents: read - steps: - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 - with: - persist-credentials: false - - # Never fails the step itself: rc is the signal every later step - # gates on (1, a real mismatch; 2, gascity unreachable -- the next - # scheduled run retries; 0, in step). A run every 15 minutes failing - # red for however long a mismatch takes to review and merge would be - # noise, not signal. - - name: Compare the worker-env manifest and pin with gascity's main - id: sync - run: | - rc=0 - tools/rbe/worker-env-sync main || rc=$? - echo "rc=$rc" >> "$GITHUB_OUTPUT" - - - name: Copy gascity main's worker-env manifest and pin - if: steps.sync.outputs.rc == '1' - run: tools/rbe/worker-env-sync-copy main - - - name: Confirm only the worker-env files moved - if: steps.sync.outputs.rc == '1' - run: | - changed="$(git diff --name-only)" - untracked="$(git ls-files --others --exclude-standard)" - want=$'platforms/BUILD.bazel\ntools/rbe/worker-env.txt' - if [ -n "$untracked" ] || [ "$(printf '%s\n' "$changed" | sort)" != "$(printf '%s\n' "$want" | sort)" ]; then - echo "::error::worker-env-sync-copy touched more than platforms/BUILD.bazel and tools/rbe/worker-env.txt:" - printf '%s\n' "$changed" "$untracked" - exit 1 - fi - - # contents:write only, on this repo alone: all it can do is push - # bot/worker-env-sync. Minted only once a real mismatch needs a push. - - name: Mint push token (gastownhall-autofix) - id: push-token - if: steps.sync.outputs.rc == '1' - uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3 - with: - app-id: ${{ secrets.AUTOFIX_APP_ID }} - private-key: ${{ secrets.AUTOFIX_APP_PRIVATE_KEY }} - owner: gastownhall - repositories: beads - permission-contents: write - - # pull-requests:write only, on this repo alone: all it can do is - # open, label and arm auto-merge on a PR -- never push a commit. - - name: Mint PR token (gascity-autofix) - id: pr-token - if: steps.sync.outputs.rc == '1' - uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3 - with: - app-id: ${{ secrets.GASCITY_AUTOFIX_APP_ID }} - private-key: ${{ secrets.GASCITY_AUTOFIX_APP_PRIVATE_KEY }} - owner: gastownhall - repositories: beads - permission-pull-requests: write - - # push-token alone; it cannot see a PR exists, let alone touch one. - # Leased to the branch: a force-with-lease scoped to refs/heads/ - # bot/worker-env-sync only, so a push can never move any other ref. - - name: Commit and push the sync branch - if: steps.sync.outputs.rc == '1' - env: - PUSH_TOKEN: ${{ steps.push-token.outputs.token }} - REPO: ${{ github.repository }} - BRANCH: bot/worker-env-sync - COMMIT_SUBJECT: "build(rbe): sync worker-env pin with gascity main" - GIT_AUTHOR_NAME: gastownhall-autofix[bot] - GIT_AUTHOR_EMAIL: gastownhall-autofix[bot]@users.noreply.github.com - GIT_COMMITTER_NAME: gastownhall-autofix[bot] - GIT_COMMITTER_EMAIL: gastownhall-autofix[bot]@users.noreply.github.com - run: | - git checkout -B "$BRANCH" - git add platforms/BUILD.bazel tools/rbe/worker-env.txt - git commit -m "$COMMIT_SUBJECT" - git push "https://x-access-token:${PUSH_TOKEN}@github.com/${REPO}.git" \ - "HEAD:refs/heads/$BRANCH" "--force-with-lease=refs/heads/$BRANCH" - - # pr-token alone; it cannot push a commit, only act on the PR the - # step above already pushed. - - name: Open or update the sync PR - id: pr - if: steps.sync.outputs.rc == '1' - env: - GH_TOKEN: ${{ steps.pr-token.outputs.token }} - REPO: ${{ github.repository }} - BRANCH: bot/worker-env-sync - TITLE: "build(rbe): sync worker-env pin with gascity main" - LABEL: status/needs-review-auto - RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} - run: | - body="$(cat <> "$GITHUB_OUTPUT" - - - name: Enable auto-merge - if: steps.sync.outputs.rc == '1' && steps.pr.outputs.number - env: - GH_TOKEN: ${{ steps.pr-token.outputs.token }} - REPO: ${{ github.repository }} - PR_NUMBER: ${{ steps.pr.outputs.number }} - run: gh pr merge --auto --squash --repo "$REPO" "$PR_NUMBER" diff --git a/.gitignore b/.gitignore index a4c9b2468..c5d2e31b2 100644 --- a/.gitignore +++ b/.gitignore @@ -255,9 +255,3 @@ tmp/build/cli-docs/where.md tmp/build/cli-docs/worktree.md tmp/docs/CLI_REFERENCE.md *.gate.lock* - -# Bazel: output symlinks and machine-local rc files (remote executor endpoint, -# TLS credentials). Never commit these. -/bazel-* -/.bazelrc.local -/user.bazelrc diff --git a/.golangci.yml b/.golangci.yml index 1ed2975a4..a3795fafc 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,9 +1,3 @@ -# Lint policy for nogo (//tools/nogo), which runs these linters beside every -# Bazel Go compile: tools/nogo/internal/golangci embeds this file and applies -# it as golangci-lint v2.10.1 would (enabled linters, settings, run.tests, -# generated files, exclusion rules). Decoding is strict, so a key the nogo -# wrappers do not implement fails the build. golangci-lint itself still reads -# it (editors, ad-hoc runs), but CI gates on nogo; see engdocs/LINTING.md. version: "2" run: @@ -307,14 +301,6 @@ linters: - pattern: '^types\.(IssueFilter|WorkFilter)$' pkg: '^github\.com/steveyegge/beads/internal/types$' msg: "do not write a filter here: take one back from a builder in internal/workapi, or pass the whole request to issueops.Reader. internal/httpapi may never name these types; cmd/bd denies them by default and lists the files that legitimately build their own, with the reason for each, in .golangci.yml - see issueops/reader.go" - - pattern: '^fmt\.Print(f|ln)?$' - pkg: '^fmt$' - # Keep the prefix in sync with the list/graph text exclusion below. - msg: "do not write list/graph output through process stdout: route format output through the command writer; mark an intentional non-format path locally" - - pattern: '^os\.Stdout$' - pkg: '^os$' - # Keep the prefix in sync with the list/graph text exclusion below. - msg: "do not write list/graph output through process stdout: route format output through the command writer; mark an intentional non-format path locally" errcheck: exclude-functions: - (*database/sql.DB).Close @@ -341,10 +327,10 @@ linters: exclusions: rules: - # forbidigo holds four rules: filter construction, label-plane access, - # and the two process-stdout forms used by list/graph output. The entries - # from here to the test exemption define their scopes. This first one - # keeps every rule off the rest of the tree; the settings block says why. + # forbidigo holds TWO rules - the filter-construction guard and the + # label-plane guard - and the entries from here to the test exemption are + # their entire scope. This one keeps both off the rest of the tree; the + # settings block above says why. # # The previous version of this comment said there was exactly one rule and # that a second "would need this widened or replaced". That came true, and @@ -357,15 +343,6 @@ linters: - path-except: '^(internal/httpapi/|cmd/bd/)' linters: - forbidigo - # Process stdout is forbidden only across the list/graph file families. - # Existing non-format paths in those families carry local, reviewed - # exceptions; a new file or output site is denied by default. Scoping by - # the diagnostic text keeps the filter and label rules unchanged. - - path-except: '^cmd/bd/(list|graph).*\.go$' - # This prefix must match both fmt.Print and os.Stdout messages above. - text: 'do not write list/graph output through process stdout' - linters: - - forbidigo # # THE LABEL-PLANE RULE IS NOW PACKAGE-WIDE ACROSS cmd/bd (ga-2ltro.12). # It used to be narrowed by a `path-except` to cmd/bd/label.go, the one @@ -467,28 +444,12 @@ linters: # tests: false` means forbidigo never analyzes a test file at all, so that # entry asserts nothing either way and scoping it would be the churn this # comment just refused to write. - # - # `search` LEFT this list when all three of its routes moved onto - # issueops.Reader.List with issueops.SearchListRequest (cmd/bd/search.go); - # it builds no filter any more. - # - # `gate` LEFT it when `bd gate check` joined `bd gate list` on - # issueops.ListGates over the route's Querier: cmd/bd/gate.go and - # cmd/bd/gate_proxied_server.go name no filter type any more. - # `gate_discover` keeps its own hole below, since findPendingGates still - # asks SearchIssues for its gates. - - path: '^cmd/bd/(close|duplicates|find_duplicates|gc|graph|info|label|orphans|purge|todo|wisp|human)(_proxied_server)?\.go$' + - path: '^cmd/bd/(close|duplicates|find_duplicates|gate|gc|graph|info|label|orphans|purge|search|todo|wisp|human)(_proxied_server)?\.go$' text: 'do not write a filter here' linters: - forbidigo # Molecule reporting: each selects the members of one molecule. - # - # `mol current` and `mol progress` LEFT this list when their in_progress - # and hooked listings moved onto issueops.Reader.List (cmd/bd/mol_roles.go) - # so a remote backend answers them. The proxied route's in-transaction - # port still searches through the unit of work, and that filter lives in - # mol_port.go beside the port. - - path: '^cmd/bd/mol_(port|ready_gated|stale)\.go$' + - path: '^cmd/bd/mol_(current|port|progress|ready_gated|stale)\.go$' text: 'do not write a filter here' linters: - forbidigo diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index fa008964c..fc8edcbea 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,15 +1,9 @@ repos: - # Lint and vet: nogo (//tools/nogo) over the changed files' Bazel packages, - # the analyzers CI gates on (go test's vet checks plus the golangci-lint - # linters .golangci.yml enables). Needs bazel or bazelisk on PATH. - - repo: local + - repo: https://github.com/golangci/golangci-lint + rev: v2.10.1 hooks: - - id: nogo - name: nogo (bazel) - entry: make lint-changed LINT_CHANGED_SCOPE=staged - language: system - types: [go] - pass_filenames: false + - id: golangci-lint + args: [--timeout=5m] - repo: https://github.com/pre-commit/pre-commit-hooks rev: v6.0.0 diff --git a/AGENTS.md b/AGENTS.md index e908a5070..ae181ad51 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -1,227 +1,290 @@ -# Beads — Contributor Agent Instructions +# Agent Instructions -This file is for agents and humans **developing beads** (`bd`). If you are -using `bd` to track work in your own project, read the user docs instead: -[docs site](https://beads.gascity.com/) and -[IDE and agent setup](docs/getting-started/ide-setup.md). + - +See [AGENT_INSTRUCTIONS.md](AGENT_INSTRUCTIONS.md) for full instructions. -Nested `AGENTS.md` files hold area-specific rules; the routing table at the -end lists them. `CLAUDE.md` files are symlinks to their sibling `AGENTS.md`. +This file exists for compatibility with tools that look for AGENTS.md. -## How work flows here +The marker above tells `bd doctor` that the intentional divergence between +this file and `CLAUDE.md` (different audiences, different reading orders) is +expected and should not be flagged. -GitHub Issues is the public tracker. Use an issue when it adds context -reviewers need: a user-visible bug, a behavior or design change worth -discussing, or work that spans several PRs. The issue carries the -reproduction, impact, and evidence for a bug, or the motivation, impact, risk, -and verification plan for a change. Small, self-explanatory changes (typos, -flaky tests, refactors, CI or docs tweaks) can go straight to a PR whose body -explains the why. +## Key Sections -1. If the change warrants an issue, find or file one with the bug or feature - form. It does not need maintainer approval first; file it before or - alongside the PR. -2. Work on a branch, open a PR against `main` (its body says - `Closes #` when there is one), and let CI and review run. -3. Maintainers triage issues with `status/needs-triage`, `status/needs-info`, - `status/needs-repro`, `status/needs-design`, and `status/accepted` - (confirmed); see [CONTRIBUTING.md](CONTRIBUTING.md). +- **Issue Tracking** - How to use bd for work management +- **Development Guidelines** - Code standards and testing +- **Project Scope** - Read [engdocs/PROJECT_CHARTER.md](engdocs/PROJECT_CHARTER.md) before adding new feature surface area +- **Visual Design System** - Status icons, colors, and semantic styling for CLI output +- **Contributor Protection** - Read [CONTRIBUTING.md](CONTRIBUTING.md) before handling external PRs +- **Maintainer PR Guidelines** - Read [PR_MAINTAINER_GUIDELINES.md](PR_MAINTAINER_GUIDELINES.md) before triaging, landing, or closing PRs -Maintainers also keep an internal bd ledger. It is optional for contributors -and not a substitute for the GitHub issue. +## Project Scope -## Agent contribution policy +Before adding new feature surface area, read +[engdocs/PROJECT_CHARTER.md](engdocs/PROJECT_CHARTER.md). Beads owns issue tracking +primitives and should not encode orchestration-layer policy, become a storage +engine, or casually expand the database schema when metadata would work. -- File an issue when the change warrants one (see above). When you do, use - the bug or feature form fields, fill each from evidence, and answer - `NOT_ENOUGH_INFO` where the evidence runs out. -- A human reviews and stands behind every issue and PR an agent drafts. - Evidence that the change works end-to-end is required; "unit tests pass" - alone is not evidence. -- Before implementing work, opening a PR, or merging/closing a PR, run the PR - preflight: - ```bash - scripts/pr-preflight.sh --search "" --repo gastownhall/beads - scripts/pr-preflight.sh --repo gastownhall/beads - ``` - The preflight is the agent gate for PR handling; do not rely on - auto-discovery of CONTRIBUTING.md. -- Use `gh` for GitHub issues and PRs, not browser tools. Write PR, issue, - comment, and review bodies to a file, run `scripts/gh-body-lint `, - and pass the file with `gh ... --body-file`. Sign agent-written GitHub - comments and reviews per [engdocs/AGENT_SIGNING.md](engdocs/AGENT_SIGNING.md). +## PR Safety for Agents -## Contributor protection - -External contributor PRs have priority. Read [CONTRIBUTING.md](CONTRIBUTING.md) -— it contains promises we've made to contributors — and, before triaging, -reviewing, landing, closing, or otherwise maintaining PRs, +Before triaging, reviewing, landing, closing, or otherwise maintaining PRs, read [PR_MAINTAINER_GUIDELINES.md](PR_MAINTAINER_GUIDELINES.md). The maintainer policy is to maximize community throughput: find useful contributor value, absorb or transform it locally when practical, preserve attribution, and use request-changes only as a last resort. -Before implementing any feature or fix, check for existing open PRs on the -same topic. If one exists: - -1. **Review it first** — read the diff, understand the approach. -2. **Build on their work, don't rewrite it** — check out their branch, - fix/adapt as needed. -3. **Preserve their tests** — keep them unless they're wrong. -4. **Attribute properly** — `Co-authored-by:` in commits, reference their PR. -5. **Never close or supersede their PR silently.** If a rewrite is - unavoidable, explain why on the original PR and credit their design/tests. - -## Scope: where a change belongs - -Read [engdocs/PROJECT_CHARTER.md](engdocs/PROJECT_CHARTER.md) before adding -feature surface area. Beads owns issue tracking primitives and should not -encode orchestration-layer policy, become a storage engine, or casually expand -the database schema when metadata would work. Prefer, in order: issue -metadata over new fields, a flag on an existing command over a new command, -an integration or plugin over core, and leave orchestration policy to the -orchestration layer. - -**Storage boundary.** Beads talks to storage through a driver interface -(`dolthub/driver` for Dolt). Do not add beads-side flocks, engine -introspection, storage-specific retry or crash-recovery logic, or public SDK -return types that leak driver internals. If the boundary is too narrow, widen -the interface or route the issue to the driver instead of patching around it -in beads. `bd doctor` support for embedded mode is enabled one subcommand at a -time, each human-vetted (GH#3794): do not lift the embedded-mode gate in -`cmd/bd/doctor.go` wholesale, and keep database-layer checks and fixes -server-gated until the driver interface covers them. - -**Layering.** Schema → `internal/storage/issueops` → `cmd/bd`. Add new -primitives at the lowest layer first, one layer per PR, stacked when a fix -spans layers. See -[CONTRIBUTING_PR_GUIDELINES.md](CONTRIBUTING_PR_GUIDELINES.md). - -## Build, test, lint - -Bazel is the build and test system: CI gates on the `bazel test` lanes in -`.github/workflows/bazel.yml`, and nogo (lint + vet), gofmt and the repository -guards exist only as Bazel targets. Install -[Bazelisk](https://github.com/bazelbuild/bazelisk) as `bazel`; the pre-commit -and pre-push hooks need it. - +Before implementing work, opening a PR, or merging/closing a PR, run the PR +preflight: ```bash -make test # bazel test //... --config=ci: the CI test lane (nogo, gofmt, guards, unit tests) -make check # testing.Short policy + make ci-pr-lint + make test -make ci-pr-lint # nogo lint + vet gate, native plus windows/darwin -make check-docs # bazel docsync + doc freshness, then the CLI flag check -make bazel-sync # after adding/removing/renaming Go files or changing imports/go.mod -make install # build and install bd to ~/.local/bin (canonical) +scripts/pr-preflight.sh --search "" --repo gastownhall/beads +scripts/pr-preflight.sh --repo gastownhall/beads ``` -- Each other tier has its own lane (`--config=integration`, `doltserver`, - `embedded`, ...); [engdocs/TESTING.md](engdocs/TESTING.md) lists the exact - command per lane and is the canonical source for test selection, design, - and PR-readiness gates. -- Where actions run: contributors add `--config=fork-cache` (rbe-west's - anonymous read-only cache; nothing is uploaded), maintainers with an rbe-west - certificate `--config=remote-exec`, agent hosts whose `~/.bazelrc` names the - executor neither. Make yours the default with a `build --config=...` line in - the gitignored `.bazelrc.local`, or pass `BAZEL_FLAGS=...` to make. -- Agents run tests only with `bazel test`, also while iterating on one - behavior: +External contributor PRs have priority. Review and build on their branch when +possible, preserve their tests and attribution, and never close or supersede +their PR silently. If a rewrite is unavoidable, explain why on the original PR +and credit their design/tests. - ```bash - bazel test //cmd/bd:bd_test --config=ci --test_filter='^TestName$' --test_output=all - ``` +## Visual Design Anti-Patterns - A target also passes when the test never ran (a filter that matches - nothing, a skip, a file the configuration does not compile), so count a run - only when its log shows `--- PASS: TestName` and no `--- SKIP` for the test - or a `TestName/...` subtest: Go prints `--- PASS` for a test whose every - subtest skipped. "Check That the Test Ran" in - [engdocs/TESTING.md](engdocs/TESTING.md) names the tests `--config=ci` does - not run (Dolt-backed, `-short`-gated, integration-tagged, `TestEmbedded*` - and embedded-Dolt, files `BUILD.bazel` does not list) and the way into - each. An unchanged target returns its cached result, which is the same - evidence. Finish with `make test`. -- Agents do not run `go test` (`./scripts/test.sh`, `make test-go`): CI does - not enforce it, it skips nogo, gofmt and the guards, and agent hosts may - refuse it. It stays a convenience for humans working offline. The exception - is regenerating golden files (`make corpus-regen`, `make githooks-regen`, - `BD_UPDATE_GOLDEN=1`), which Bazel cannot do: see "Regenerating Golden - Files" in [engdocs/TESTING.md](engdocs/TESTING.md). -- **Do NOT** use `go build -o bd ./cmd/bd`, `go install ./cmd/bd`, or raw - `go run ./cmd/bd ...`: they bypass the canonical build path, leave stale - binaries, and raw `go run` misses the `gms_pure_go` tag. Use `make install`, - `./bd`, or `go run -tags gms_pure_go ./cmd/bd ...`. -- All new features need tests. -- **Never pollute a production database with test issues.** Use `t.TempDir()` - in Go tests and a disposable working directory for manual `bd` experiments. -- `make ci-pr-lint` must pass with zero issues; see - [engdocs/LINTING.md](engdocs/LINTING.md). -- If BUILD files are out of sync and you cannot run `make bazel-sync`, CI - syncs them: on same-repo PRs the bazel-autofix workflow pushes the fix to - your branch (pull before pushing again); fork PRs get a comment with an - apply recipe. -- If you changed behavior, update the user docs (`docs/`, via the - `beads-docs` skill) or README in the same PR. +**NEVER use emoji-style icons** (🔴🟠🟡🔵⚪) in CLI output. They cause cognitive overload. -## Commits and PRs +**ALWAYS use small Unicode symbols** with semantic colors (status uses symbols; priority uses labels): +- Status: `○ ◐ ● ✓ ❄` +- Priority: `P0`–`P4` label with color (no status glyph) -- Conventional Commits: `type(scope): summary` (`fix`, `feat`, `docs`, - `test`, `refactor`, `chore`, `ci`, `perf`). -- For agent-prepared commits, include the `Agent-Signature:` trailer described - in [engdocs/AGENT_SIGNING.md](engdocs/AGENT_SIGNING.md). Use - `unknown-model` or `unknown-reasoning` when reliable runtime metadata is - unavailable. -- Maintainers working from the internal ledger may also end the subject with - the bead ID, e.g. `(bd-abc)`, so `bd doctor` can detect orphaned issues. -- Work on a feature branch and open a PR against `main`; direct pushes to - `main` are reserved for releases and narrow operational fixes. -- When the user asks to bump the version, use `./scripts/bump-version.sh`; the - full release process is in [RELEASING.md](RELEASING.md). +See [AGENT_INSTRUCTIONS.md](AGENT_INSTRUCTIONS.md) for full development guidelines. -## Ending a session +## Storage Boundary -Do not push to `main`. Before handing off: +The canonical storage boundary is in +[engdocs/PROJECT_CHARTER.md](engdocs/PROJECT_CHARTER.md#storage-boundary). In short: +Beads talks to storage through a driver interface (`dolthub/driver` for Dolt). +Do not add beads-side flocks, engine introspection, storage-specific retry or +crash-recovery logic, or public SDK return types that leak driver internals. +If the boundary is too narrow, widen the interface or route the issue to the +driver instead of patching around it in beads. -1. Run the quality gates your change needs (`make test`, `make ci-pr-lint`, - and the other Bazel lanes [engdocs/TESTING.md](engdocs/TESTING.md) - selects). If gates are broken on `main`, report it as a P0 issue. -2. Record follow-up work as GitHub issues (or ledger beads, for maintainers). -3. Commit, push, or open a PR only when the person you are working for asked - you to. Report changed files, validation run, and anything left open. +A live application of this rule: `bd doctor` support for embedded mode is +enabled one subcommand at a time, each human-vetted (GH#3794). Do not lift the +embedded-mode gate in `cmd/bd/doctor.go` wholesale, and keep database-layer +checks and fixes server-gated until the driver interface covers them. -## CLI output +## Agent Warning: Interactive Commands -**NEVER use emoji-style icons** (🔴🟠🟡🔵⚪) in CLI output. **ALWAYS use small -Unicode symbols** with semantic colors: status `○ ◐ ● ✓ ❄`; priority as a -`P0`–`P4` label with color (no status glyph). The full visual design system -and CLI design principles are in -[engdocs/UI_PHILOSOPHY.md](engdocs/UI_PHILOSOPHY.md). +**DO NOT use `bd edit`** - it opens an interactive editor ($EDITOR) which AI agents cannot use. -## Non-interactive shell commands +Use `bd update` with flags instead: +```bash +bd update --description "new description" +bd update --title "new title" +bd update --design "design notes" +bd update --notes "additional notes" +bd update --acceptance "acceptance criteria" -Agents cannot answer prompts. Use `cp -f`, `mv -f`, `rm -f`, `rm -rf`, -`cp -rf` (these may be aliased to `-i`); `scp`/`ssh -o BatchMode=yes`; -`apt-get -y`; `HOMEBREW_NO_AUTO_UPDATE=1 brew`. **DO NOT use `bd edit`** — it -opens `$EDITOR`; use `bd update --title/--description/--design/--acceptance/--append-notes` -(pipe text with special characters via `--description=-`). +# Use stdin for descriptions with special characters (backticks, !, nested quotes) +echo 'Description with `backticks` and "quotes"' | bd create "Title" --description=- +echo 'Updated text' | bd update --description=- +``` -## Routing table +## Testing -| When you are touching… | Read first | -|---|---| -| `cmd/bd/` (commands, output, proxied-server routes) | [cmd/bd/AGENTS.md](cmd/bd/AGENTS.md) | -| `engdocs/` (contributor and internals docs) | [engdocs/AGENTS.md](engdocs/AGENTS.md) | -| `docs/` (the published user docs) | `.claude/skills/beads-docs/SKILL.md` | -| Any Go test | [engdocs/TESTING.md](engdocs/TESTING.md) | -| Lint failures or `.golangci.yml` | [engdocs/LINTING.md](engdocs/LINTING.md) | -| A new storage operation or issueops role | [engdocs/ADDING_AN_ISSUEOPS_ROLE.md](engdocs/ADDING_AN_ISSUEOPS_ROLE.md) | -| Schema migrations | [internal/storage/schema/migrations/README.md](internal/storage/schema/migrations/README.md) | -| Dolt concurrency, server vs embedded mode | [engdocs/design/dolt-concurrency.md](engdocs/design/dolt-concurrency.md) | -| Errors returned to users | [engdocs/ERROR_HANDLING.md](engdocs/ERROR_HANDLING.md) | -| The `bd serve` HTTP surface | [engdocs/SERVE_RUNBOOK.md](engdocs/SERVE_RUNBOOK.md) | -| A new example under `examples/` | `examples/README.md` — give the example its own README and link it there | -| Releases and version bumps | [RELEASING.md](RELEASING.md) | -| Scope questions | [engdocs/PROJECT_CHARTER.md](engdocs/PROJECT_CHARTER.md) | -| Planned work and priorities | [ROADMAP.md](ROADMAP.md) | +Use [engdocs/TESTING.md](engdocs/TESTING.md) for the canonical commands, +test-design guidance, and PR-readiness gates. + +## Non-Interactive Shell Commands + +**ALWAYS use non-interactive flags** with file operations to avoid hanging on confirmation prompts. + +Shell commands like `cp`, `mv`, and `rm` may be aliased to include `-i` (interactive) mode on some systems, causing the agent to hang indefinitely waiting for y/n input. + +**Use these forms instead:** +```bash +# Force overwrite without prompting +cp -f source dest # NOT: cp source dest +mv -f source dest # NOT: mv source dest +rm -f file # NOT: rm file + +# For recursive operations +rm -rf directory # NOT: rm -r directory +cp -rf source dest # NOT: cp -r source dest +``` + +**Other commands that may prompt:** +- `scp` - use `-o BatchMode=yes` for non-interactive +- `ssh` - use `-o BatchMode=yes` to fail instead of prompting +- `apt-get` - use `-y` flag +- `brew` - use `HOMEBREW_NO_AUTO_UPDATE=1` env var + +## Landing the Plane (Session Completion) + +**When ending a work session** (or when the user says "let's land the +plane"), you MUST complete ALL steps below. Work is NOT complete until +`git push` succeeds. + +**MANDATORY WORKFLOW:** + +1. **File issues for remaining work** - Create issues for anything that needs follow-up +2. **Run quality gates** (if code changed): + - `make ci-pr-lint` (required zero-finding formatting and lint wrapper) + - `make test` (and `make test-icu-path` only if you intentionally need the ICU regex path) + - File a P0 issue if quality gates are broken +3. **Update issue status** - Close finished work, update in-progress items +4. **PUSH TO REMOTE** - This is MANDATORY: + ```bash + git pull --rebase + git push + git status # MUST show "up to date with origin" + ``` +5. **Clean up**: + ```bash + git stash clear # Remove old stashes + git remote prune origin # Clean up deleted remote branches + ``` +6. **Verify** - All changes committed AND pushed, no untracked files remain +7. **Hand off** - Choose a follow-up issue and give the user a prompt for + the next session, e.g. "Continue work on bd-X: [issue title]. [Brief + context about what's been done and what's next]" + +**CRITICAL RULES:** +- Work is NOT complete until `git push` succeeds +- NEVER stop before pushing - that leaves work stranded locally +- NEVER say "ready to push when you are" - YOU must push +- If push fails, resolve and retry until it succeeds + +Close with a summary for the user: what was completed this session, issues +filed for follow-up, quality-gate status, confirmation everything is pushed, +and the recommended prompt for the next session. + + +## Issue Tracking with bd (beads) + +**IMPORTANT**: This project uses **bd (beads)** for ALL issue tracking. Do NOT use markdown TODOs, task lists, or other tracking methods. + +### Why bd? + +- Dependency-aware: Track blockers and relationships between issues +- Git-friendly: Dolt-powered version control with native sync +- Agent-optimized: JSON output, ready work detection, discovered-from links +- Prevents duplicate tracking systems and confusion + +### Quick Start + +**Check for ready work:** + +```bash +bd ready --json +``` + +**Create new issues:** + +```bash +bd create "Issue title" --description="Detailed context" -t bug|feature|task -p 0-4 --json +bd create "Issue title" --description="What this issue is about" -p 1 --deps discovered-from:bd-123 --json +``` + +**Claim and update:** + +```bash +bd update --claim --json +bd update bd-42 --priority 1 --json +``` + +**Complete work:** + +```bash +bd close bd-42 --reason "Completed" --json +``` + +### Issue Types + +- `bug` - Something broken +- `feature` - New functionality +- `task` - Work item (tests, docs, refactoring) +- `epic` - Large feature with subtasks +- `chore` - Maintenance (dependencies, tooling) + +### Priorities + +- `0` - Critical (security, data loss, broken builds) +- `1` - High (major features, important bugs) +- `2` - Medium (default, nice-to-have) +- `3` - Low (polish, optimization) +- `4` - Backlog (future ideas) + +### Workflow for AI Agents + +1. **Check ready work**: `bd ready` shows unblocked issues +2. **Claim your task atomically**: `bd update --claim` +3. **Work on it**: Implement, test, document +4. **Discover new work?** Create linked issue: + - `bd create "Found bug" --description="Details about what was found" -p 1 --deps discovered-from:` +5. **Complete**: `bd close --reason "Done"` + +### Quality +- Use `--acceptance` and `--design` fields when creating issues +- Use `--validate` to check description completeness + +### Lifecycle +- `bd defer ` / `bd supersede ` for issue management +- `bd stale` / `bd orphans` / `bd lint` for hygiene +- `bd human ` to flag for human decisions +- `bd formula list` / `bd mol pour ` for structured workflows + +### Sync + +bd stores issue history in Dolt: + +- Each write auto-commits to Dolt history +- Use `bd dolt push`/`bd dolt pull` for remote sync +- Do not treat `.beads/issues.jsonl` as the sync protocol + +**Architecture in one line:** issues live in a local Dolt DB; sync uses `refs/dolt/data` on your git remote; `.beads/issues.jsonl` is a passive export. See https://github.com/gastownhall/beads/blob/main/docs/core-concepts/sync-concepts.md for details and anti-patterns. + +### Important Rules + +- ✅ Use bd for ALL task tracking +- ✅ Always use `--json` flag for programmatic use +- ✅ Link discovered work with `discovered-from` dependencies +- ✅ Check `bd ready` before asking "what should I work on?" +- ❌ Do NOT create markdown TODO lists +- ❌ Do NOT use external issue trackers +- ❌ Do NOT duplicate tracking systems + +For more details, see README.md and https://github.com/gastownhall/beads/blob/main/docs/getting-started/quickstart.md. + +## Agent Context Profiles + +The managed Beads block is task-tracking guidance, not permission to override repository, user, or orchestrator instructions. + +- **Conservative (default)**: Use `bd` for task tracking. Do not run git commits, git pushes, or Dolt remote sync unless explicitly asked. At handoff, report changed files, validation, and suggested next commands. +- **Minimal**: Keep tool instruction files as pointers to `bd prime`; use the same conservative git policy unless active instructions say otherwise. +- **Team-maintainer**: Only when the repository explicitly opts in, agents may close beads, run quality gates, commit, and push as part of session close. A current "do not commit" or "do not push" instruction still wins. + +## Session Completion + +This protocol applies when ending a Beads implementation workflow. It is subordinate to explicit user, repository, and orchestrator instructions. + +1. **File issues for remaining work** - Create beads for anything that needs follow-up +2. **Run quality gates** (if code changed) - Tests, linters, builds +3. **Update issue status** - Close finished work, update in-progress items +4. **Handle git/sync by active profile**: + ```bash + # Conservative/minimal/default: report status and proposed commands; wait for approval. + git status + + # Team-maintainer opt-in only, unless current instructions forbid it: + git pull --rebase + bd dolt push + git push + git status + ``` +5. **Hand off** - Summarize changes, validation, issue status, and any blocked sync/commit/push step + +**Critical rules:** +- Explicit user or orchestrator instructions override this Beads block. +- Do not commit or push without clear authority from the active profile or the current user request. +- If a required sync or push is blocked, stop and report the exact command and error. + + diff --git a/AGENT_INSTRUCTIONS.md b/AGENT_INSTRUCTIONS.md index b00961736..bd5b5539c 100644 --- a/AGENT_INSTRUCTIONS.md +++ b/AGENT_INSTRUCTIONS.md @@ -1,9 +1,531 @@ -# Agent Instructions +# Detailed Agent Instructions for Beads Development -Contributor instructions for agents developing beads live in -[AGENTS.md](AGENTS.md). Area-specific rules live in nested `AGENTS.md` files -listed in its routing table. +**For project overview and quick start, see [AGENTS.md](AGENTS.md)** -If you are using `bd` to track work in your own project, see the user docs: -[IDE and agent setup](docs/getting-started/ide-setup.md) and -[https://beads.gascity.com/](https://beads.gascity.com/). +This document contains detailed operational instructions for AI agents working on beads development, testing, and releases. + +## Development Guidelines + +### Code Standards + +- **Go version**: see `go.mod` for the required version (currently 1.26+) +- **Linting**: `make ci-pr-lint` must pass with zero issues; see + [engdocs/LINTING.md](engdocs/LINTING.md) +- **Testing**: All new features need tests; use [engdocs/TESTING.md](engdocs/TESTING.md) for command selection, test design, and PR readiness. +- **Documentation**: Update relevant .md files + +### File Organization + +``` +beads/ +├── cmd/bd/ # CLI commands +├── internal/ +│ ├── types/ # Core data types +│ └── storage/ # Storage layer +│ └── dolt/ # Dolt implementation +├── examples/ # Integration examples +└── *.md # Documentation +``` + +### Testing Workflow + +**IMPORTANT:** Never pollute the production database with test issues! + +For test commands, tier selection, and test-design guidance, use the canonical +[engdocs/TESTING.md](engdocs/TESTING.md). + +**For manual testing**, keep initialization and all experiments in a disposable +working directory. This safety example supplements the canonical testing guide: + +```bash +beads_manual_dir="$(mktemp -d)" +( + set -e + cd "$beads_manual_dir" + bd init --quiet --prefix test --skip-hooks --skip-agents + bd create "Test issue" -p 1 +) +rm -rf -- "$beads_manual_dir" +``` + +`BEADS_DB` alone does not redirect `bd init` workspace setup. Do not run manual +initialization from a production workspace even when selecting another database. + +**For automated tests**, use `t.TempDir()` in Go tests: + +```go +func TestMyFeature(t *testing.T) { + tmpDir := t.TempDir() + testDB := filepath.Join(tmpDir, ".beads", "beads.db") + s := newTestStore(t, testDB) + // ... test code +} +``` + +**Git test isolation:** For tests that create temporary git repos, force repo-local hooks: + +```bash +git config core.hooksPath .git/hooks +``` + +Do not rely on the developer's global git config. Global `core.hooksPath` can leak +into temp repos and produce flaky test behavior. + +**Warning:** bd will warn you when creating issues with a "Test" prefix in the +production database. The disposable working directory is the isolation +boundary for manual initialization and experiments. + +**Tmpfs hosts:** the `cmd/bd` test suite creates an isolated `$HOME` and several +test binaries under `$TMPDIR`. They are normally cleaned by the test process, +but a SIGKILLed or OOMed run can leave orphans behind. On hosts where `/tmp` +is tmpfs (e.g. Fedora Atomic / Bluefin), run `make clean-test-tmp` between +test runs if `du -sh /tmp/beads-* /tmp/bd-*` shows accumulation. See bd-3q2u. + +### Before Committing + +1. **Run tests**: follow [engdocs/TESTING.md](engdocs/TESTING.md). +2. **Run the required lint contract when its code surface changed**: + `make ci-pr-lint` +3. **Update docs**: If you changed behavior, update README.md or other docs +4. **Commit**: With git hooks installed (`bd hooks install`), Dolt changes are auto-committed + +### Commit Message Convention + +When committing work for an issue, include the issue ID in parentheses at the end: + +```bash +git commit -m "Fix auth validation bug (bd-abc)" +git commit -m "Add retry logic for database locks (bd-xyz)" +``` + +This enables `bd doctor` to detect **orphaned issues** - work that was committed but the issue wasn't closed. The doctor check cross-references open issues against git history to find these orphans. + +For agent-prepared commits, also include the +`Agent-Signature:` trailer described in +[engdocs/AGENT_SIGNING.md](engdocs/AGENT_SIGNING.md). Use `unknown-model` or +`unknown-reasoning` when reliable runtime metadata is unavailable. + +### Git Workflow + +bd uses **Dolt** as its primary database. Changes are committed to Dolt history automatically (one Dolt commit per write command). + +**Install git hooks** for commit integration and legacy fallback behavior: +```bash +bd hooks install +``` + +### Git Integration + +**Dolt sync**: Dolt handles sync natively via `bd dolt push` / `bd dolt pull`. No export/import round-trip needed for normal sync. + +**Protected branches**: Dolt stores data under `refs/dolt/data`, separate from standard Git refs. See [docs/reference/protected-branches.md](docs/reference/protected-branches.md). + +**Git worktrees**: Work directly with Dolt — no special flags needed. See [docs/reference/advanced.md](docs/reference/advanced.md). + +**Merge conflicts**: Rare with hash IDs. Dolt uses cell-level 3-way merge for conflict resolution. + +## Git Workflow: PR by Default + +Crew workers use a PR-based workflow. Beads is a dependency of a downstream consumer, so we +defer to the standard PR flow to keep changes reviewable. + +- Work on a feature branch, push the branch, open a PR against `main` +- `gh pr create` is the normal path to land work +- Direct push to main is reserved for releases (tag + release commit) and + narrow operational fixes; prefer a PR when unsure +- When handling external contributor PRs, use fix-merge: checkout the PR + branch locally, fix/rebase onto main, merge via PR, then close the PR + +### Maintainer PR Guidelines + +Before triaging, reviewing, landing, closing, or otherwise maintaining PRs, +read [PR_MAINTAINER_GUIDELINES.md](PR_MAINTAINER_GUIDELINES.md). The +maintainer policy is to maximize community throughput: find useful contributor +value, absorb or transform it locally when practical, preserve attribution, and +use request-changes only as a last resort. + +Sign agent-written GitHub comments and reviews using +[engdocs/AGENT_SIGNING.md](engdocs/AGENT_SIGNING.md). + +### External Contributor PRs: Check Before You Build + +**Read [CONTRIBUTING.md](CONTRIBUTING.md)** — it contains promises we've made to contributors. Violating them damages trust and community. + +Run the read-only preflight before implementing related work, opening a PR, or +merging/closing a PR: + +```bash +scripts/pr-preflight.sh --search "" --repo gastownhall/beads +scripts/pr-preflight.sh --repo gastownhall/beads +``` + +**Before implementing any feature or fix, check for existing open PRs on the same topic:** + +```bash +gh pr list --repo gastownhall/beads --state open --search "" --json number,title,author,headRefName +``` + +**Contributor work gets priority.** If an external PR already exists: +1. **Review it first** — read the diff, understand the approach +2. **Build on their work, don't rewrite it** — checkout their branch, fix/adapt as needed +3. **Preserve their tests** — contributor tests are signal; keep them unless they're wrong +4. **Attribute properly** — use `Co-authored-by:` in commits, reference their PR number +5. **Never auto-close a contributor PR** by merging a rewrite — that discards their contribution silently + +If you must rewrite (e.g., fundamentally different approach needed), explain why on the original PR and credit the contributor's design/tests in your commits. + +Do not rely on auto-discovery of CONTRIBUTING.md; the preflight is the agent +gate for PR handling. + +## Landing the Plane + +See [AGENTS.md](AGENTS.md#landing-the-plane-session-completion) for the +canonical session-completion protocol (quality gates, mandatory `git push`, +cleanup, and next-session hand-off). + +## Agent Session Workflow + +**WARNING: DO NOT use `bd edit`** - it opens an interactive editor ($EDITOR) which AI agents cannot use. Use `bd update` with flags instead: +```bash +bd update --description "new description" +bd update --title "new title" +bd update --design "design notes" +bd update --notes "additional notes" +bd update --acceptance "acceptance criteria" +``` + +**Read execution metadata before prose.** When enacting a bd issue, inspect the +structured metadata before using description or notes to choose execution mode, +delegation, model, reasoning level, or parallel group: + +```bash +bd show --json | jq '.[0] | {id,title,metadata,description,notes}' +``` + +The execution metadata keys are: + +- `execution_agent_type` +- `execution_suggested_model` +- `execution_reasoning_effort` +- `execution_mode` +- `execution_parallel_group` + +When these keys are present, treat them as the authoritative execution hints. +Use `description` for the work scope and `notes` for rationale or fallback +context. Parent/orchestrator agents must read these fields before spawning +subagents because a running subagent cannot change its model or reasoning effort +after launch. + +**Use stdin for descriptions with special characters** (backticks, `!`, nested quotes): +```bash +# Pipe via stdin to avoid shell escaping issues +echo 'Description with `backticks` and "quotes"' | bd create "Title" --stdin +echo 'Updated description with $variables' | bd update --description=- + +# Or use --body-file for longer content +bd create "Title" --body-file=description.md +``` + +**GitHub body hygiene.** For GitHub PR, issue, comment, and review bodies, +write Markdown to a file and pass it with `gh ... --body-file`. Run +`scripts/gh-body-lint ` first to catch literal `\n` sequences and +non-linking `GH#123` references. + +**Example agent session:** + +```bash +# Make changes (each write auto-commits to Dolt) +bd create "Fix bug" -p 1 +bd create "Add tests" -p 1 +bd update bd-42 --claim +bd close bd-40 --reason "Completed" + +# Push Dolt data to remote if configured +bd dolt push + +# Now safe to end session +``` + +This installs: + +- **pre-commit** — Commits pending Dolt changes +- **post-merge** — Runs chained hooks and a legacy JSONL import fallback only when no Dolt remote is configured + +**Note:** Hooks are embedded in the bd binary and work for all bd users (not just source repo users). + +## Common Development Tasks + +### Visual Design System + +When adding CLI output features, follow these design principles for consistent, +cognitively friendly visuals. + +#### No Emoji-Style Icons + +Do not use large colored emoji icons like red/orange/yellow/blue/white circles +for priorities or status. They cause cognitive overload and break visual +consistency. + +Use small Unicode symbols with semantic colors applied via lipgloss: + +- Status: `○ ◐ ● ✓ ❄` +- Priority: `P0`–`P4` label with color (no status glyph) + +#### Status Icons + +Use these symbols consistently across all commands: + +```text +○ open - Available to work (white/default) +◐ in_progress - Currently being worked (yellow) +● blocked - Waiting on dependencies (red) +✓ closed - Completed (muted gray) +❄ deferred - Scheduled for later (blue/muted) +``` + +#### Priority Labels and Colors + +Format priority as the P-label with color only (no status glyph — `●` is blocked status): + +- `P0`: Red + bold (critical) +- `P1`: Orange (high) +- `P2`: Amber (elevated) +- `P3`–`P4`: Default text + +#### Issue Type Colors + +- `bug`: Red (problems need attention) +- `epic`: Purple (larger scope) +- Others: Default text + +#### Design Principles + +1. Small Unicode symbols only; avoid emoji blobs. +2. Semantic colors only for actionable items; do not color everything. +3. Closed items fade using muted gray. +4. Prefer icons over text labels for scanability. +5. Keep icons consistent across list, graph, show, and related commands. +6. Use tree connectors (`├──`, `└──`, `│`) for hierarchies. +7. Reduce cognitive noise; do not show `needs:1` when it is just the parent epic. + +#### Semantic Styles + +Use exported styles from `internal/ui/styles.go`: + +```go +// Status styles +ui.StatusInProgressStyle // Yellow - active work +ui.StatusBlockedStyle // Red - needs attention +ui.StatusClosedStyle // Muted gray - done + +// Priority styles +ui.PriorityP0Style // Red + bold +ui.PriorityP1Style // Orange + +// Type styles +ui.TypeBugStyle // Red +ui.TypeEpicStyle // Purple + +// General styles +ui.PassStyle, ui.WarnStyle, ui.FailStyle +ui.MutedStyle, ui.AccentStyle +ui.RenderMuted(text), ui.RenderAccent(text) +``` + +Example: + +```go +switch issue.Status { +case types.StatusOpen: + icon = "○" +case types.StatusInProgress: + icon = ui.StatusInProgressStyle.Render("◐") +case types.StatusBlocked: + icon = ui.StatusBlockedStyle.Render("●") +case types.StatusClosed: + icon = ui.StatusClosedStyle.Render("✓") +} +``` + +### CLI Design Principles + +**Minimize cognitive overload.** Every new command, flag, or option adds cognitive burden for users. Before adding anything: + +1. **Recovery/fix operations → `bd doctor --fix`**: Don't create separate commands like `bd recover` or `bd repair`. Doctor already detects problems - let `--fix` handle remediation. This keeps all health-related operations in one discoverable place. + For git hook marker migration specifically: use `bd migrate hooks --dry-run` to preview operations, and `bd doctor --fix` for the standard apply path. + +2. **Prefer flags on existing commands**: Before creating a new command, ask: "Can this be a flag on an existing command?" Example: `bd list --stale` instead of `bd stale`. + +3. **Consolidate related operations**: Related operations should live together. Version control uses `bd vc {log,diff,commit}`, not separate top-level commands. + +4. **Count the commands**: Run `bd --help` and count. If we're approaching 30+ commands, we have a discoverability problem. Consider subcommand grouping. + +5. **New commands need strong justification**: A new command should represent a fundamentally different operation, not just a convenience wrapper. + +### Adding a New Command + +1. Create file in `cmd/bd/` +2. Add to root command in `cmd/bd/main.go` +3. Implement with Cobra framework +4. Add `--json` flag for agent use +5. Add tests in `cmd/bd/*_test.go` +6. Document in README.md + +### Adding Storage Features + +1. Add Dolt SQL schema changes in `internal/storage/dolt/` +2. Add migration if needed +3. Update `internal/types/types.go` if new types +4. Implement in `internal/storage/dolt/` (queries, issues, etc.) +5. Add tests +6. Update export/import in `cmd/bd/export.go` and `cmd/bd/import.go` + +### Adding Examples + +1. Create directory in `examples/` +2. Add README.md explaining the example +3. Include working code +4. Link from `examples/README.md` +5. Mention in main README.md + +## Building + +```bash +# Build and install bd to ~/.local/bin (the canonical location) +make install + +# Verify installed binary +bd init --prefix test +bd create "Test issue" -p 1 +bd ready +``` + +For testing commands, test design, and PR-readiness gates, use +[engdocs/TESTING.md](engdocs/TESTING.md). + +> **WARNING**: Do NOT use `go build -o bd ./cmd/bd`, `go install ./cmd/bd`, +> or raw `go run ./cmd/bd ...`. +> These bypass the canonical build path, can create stale binaries in the +> working directory or `~/go/bin/`, and raw `go run` may miss the required +> `gms_pure_go` build tag. Always use `make install`, `./bd`, or +> `go run -tags gms_pure_go ./cmd/bd ...` when you explicitly need `go run`. + +## Version Management + +**IMPORTANT**: When the user asks to "bump the version" or mentions a new version number (e.g., "bump to 0.9.3"), use the version bump script: + +```bash +# Preview changes (shows diff, doesn't commit) +./scripts/bump-version.sh 0.9.3 + +# Auto-commit the version bump +./scripts/bump-version.sh 0.9.3 --commit +git push origin main +``` + +**What it does:** + +- Updates ALL version files (CLI, plugin, MCP server, docs) in one command +- Validates semantic versioning format +- Shows diff preview +- Verifies all versions match after update +- Creates standardized commit message + +**User will typically say:** + +- "Bump to 0.9.3" +- "Update version to 1.0.0" +- "Rev the project to 0.9.4" +- "Increment the version" + +**You should:** + +1. Run `./scripts/bump-version.sh --commit` +2. Push to GitHub +3. Confirm all versions updated correctly + +**Files updated automatically:** + +- `cmd/bd/version.go` - CLI version +- `plugins/beads/.claude-plugin/plugin.json` - Claude plugin version +- `plugins/beads/.codex-plugin/plugin.json` - Codex plugin version +- `.claude-plugin/marketplace.json` - Claude marketplace version +- `integrations/beads-mcp/pyproject.toml` - MCP server version +- `README.md` - Documentation version +- `PLUGIN.md` - Version requirements + +**Why this matters:** We had version mismatches (bd-66) when only `version.go` was updated. This script prevents that by updating all components atomically. + +See `scripts/README.md` for more details. + +## Release Process (Maintainers) + +See [RELEASING.md](RELEASING.md) for the complete release process, including +the automated `./scripts/release.sh ` script and the manual, +step-by-step channel-by-channel instructions. + +## Checking GitHub Issues and PRs + +**IMPORTANT**: When asked to check GitHub issues or PRs, use command-line tools like `gh` instead of browser/playwright tools. + +**Preferred approach:** + +```bash +# List open issues with details +gh issue list --limit 30 + +# List open PRs +gh pr list --limit 30 + +# View specific issue +gh issue view 201 +``` + +**Then provide an in-conversation summary** highlighting: + +- Urgent/critical issues (regressions, bugs, broken builds) +- Common themes or patterns +- Feature requests with high engagement +- Items that need immediate attention + +**Why this matters:** + +- Browser tools consume more tokens and are slower +- CLI summaries are easier to scan and discuss +- Keeps the conversation focused and efficient +- Better for quick triage and prioritization + +**Do NOT use:** `browser_navigate`, `browser_snapshot`, or other playwright tools for GitHub PR/issue reviews unless specifically requested by the user. + +## Telemetry + +`bd` collects anonymous command-usage metrics. Each event is a `cli_command` +record carrying only the command name; each batch also carries the bd version +and OS platform, keyed by a machine-derived, HMAC-protected distinct ID. No +email, repo path, remote URL, issue content, or user-supplied strings are +collected. Events are written under `~/.beads/eventsData` and POSTed to +`https://gastownhall-eventsapi.com/mp/collect`. + +Metrics are enabled by default (opt-out). The friendliest way to see or change +them is `bd metrics` (`bd metrics on` / `bd metrics off` / `bd metrics example`), +which takes effect on the next command with no restart. `BD_DISABLE_METRICS=1` +still works as a one-off, shell-scoped override. The cross-tool +[`DO_NOT_TRACK`](https://donottrack.sh/) standard is honored as a disable-only +opt-out: `DO_NOT_TRACK=1` opts out, while a falsey or empty value +(`DO_NOT_TRACK=0`, `false`, or unset-but-present) falls through to your saved +`bd metrics` preference instead of forcing metrics back on. `BD_DISABLE_METRICS` +is the bidirectional override and takes precedence when both are set. + +## Questions? + +- Check existing issues: `bd list` +- Look at recent commits: `git log --oneline -20` +- Read the docs: README.md, ADVANCED.md, docs/reference/configuration.md +- Create an issue if unsure: `bd create "Question: ..." -t task -p 2` + +## Important Files + +- **README.md** - Main documentation (keep this updated!) +- **ADVANCED.md** - Advanced features (rename, merge, compaction) +- **CONTRIBUTING.md** - Contribution guidelines +- **SECURITY.md** - Security policy diff --git a/BUILD.bazel b/BUILD.bazel deleted file mode 100644 index 0a1d10355..000000000 --- a/BUILD.bazel +++ /dev/null @@ -1,1187 +0,0 @@ -load("@gazelle//:def.bzl", "gazelle") -load("@rules_go//go:def.bzl", "go_library", "go_test") - -# gazelle:prefix github.com/steveyegge/beads -# gazelle drops files whose build constraints name a tag it does not know. -# integration is listed so the `//go:build integration` test files (and -# //internal/testutil/integration) are in srcs and deps; rules_go filters them -# out of every build except --config=integration (.bazelrc), which sets the tag. -# gazelle:build_tags gms_pure_go,integration -# integrations/ holds no Go; it stays visible to Bazel (not in .bazelignore) -# so //:repo_files covers it for //test/docsync's link checks and the -# repository-policy tests in //scripts. -# gazelle:exclude integrations -# The two example Go modules (own go.mod, built against a released beads) are -# not part of this build either, but they are tracked files the repository -# guards (gofmt, build-tag and install-guidance checks in -# //scripts/repochecks) must see, so they are not in .bazelignore. -# gazelle:exclude examples/library-usage -# gazelle:exclude examples/bd-example-extension-go -gazelle(name = "gazelle") - -exports_files( - [".buildflags"], - visibility = [ - "//scripts:__pkg__", - "//scripts/prlintmake:__pkg__", - "//tests/upgrade_smoke:__pkg__", - ], -) - -exports_files([ - "go.mod", - "go.sum", -]) - -# The golangci-lint configuration nogo applies (//tools/nogo), and its test. -# //test/docsync checks the agent instructions' links to it. -exports_files( - [".golangci.yml"], - visibility = [ - "//test/docsync:__pkg__", - "//tools/nogo:__pkg__", - "//tools/nogo/internal/golangci:__pkg__", - ], -) - -# Repository files read by source-scanning tests, declared as their `data` so -# the tests run under `bazel test` and remote execution. -exports_files( - [".gitattributes"], - visibility = ["//scripts/gitattributespolicy:__pkg__"], -) - -# //tests/hook_timeout_backends: the tracked git hooks whose managed section -# it runs against real timeout implementations. //scripts:shell_scripts_test -# runs pre-push's suite dispatch. -exports_files( - [ - ".githooks/post-checkout", - ".githooks/post-merge", - ".githooks/pre-commit", - ".githooks/pre-push", - ".githooks/prepare-commit-msg", - ], - visibility = [ - "//scripts:__pkg__", - "//tests/hook_timeout_backends:__pkg__", - ], -) - -# //scripts:shell_scripts_test: scripts/test.sh's skip list. -exports_files( - [".test-skip"], - visibility = ["//scripts:__pkg__"], -) - -# //scripts:shell_scripts_test: Oracle A's runner, whose EXIT-trap removal of -# its reference worktree oracle_a_script_test.go checks in a fixture repository. -exports_files( - ["tests/oracle-a/run-oracle-a.sh"], - visibility = ["//scripts:__pkg__"], -) - -# The nested Go modules' go.mod files, which mark where the root module ends: -# //scripts:go_test_sources_test's scan of every _test.go file skips their -# trees, as `go test ./...` does. (go.mod is in no Go partition below: it is -# in //:repo_other_files.) -filegroup( - name = "nested_go_modules", - srcs = [ - "examples/bd-example-extension-go/go.mod", - "examples/library-usage/go.mod", - "//tools/nogo:go.mod", - ], - visibility = ["//scripts:__pkg__"], -) - -# //scripts/repochecks:winget_portable_alias_test: the winget manifests. -filegroup( - name = "winget_manifests", - srcs = glob(["winget/**"]), - visibility = ["//scripts/repochecks:__pkg__"], -) - -# //test/docsync: the repository files outside its Markdown and Go-test -# partitions that the docs and agent instructions link to by path. A new link -# to a file not listed here fails docsync_test under Bazel; add the file. -exports_files( - [ - ".github/workflows/bazel.yml", - "beads.go", - "docs/docs.json", - ], - visibility = ["//test/docsync:__pkg__"], -) - -# //cmd/bd:bd_test: tracked git hooks (generator parity) and the formula -# primitive fixtures. -filegroup( - name = "bd_test_files", - srcs = glob([ - ".githooks/*", - "examples/formulas/primitives/**", - ]), - visibility = ["//cmd/bd:__pkg__"], -) - -exports_files( - [".claude-plugin/marketplace.json"], - visibility = ["//cmd/bd/setup:__pkg__"], -) - -# PR Risk's manifest-sharded tiers' shard scripts and their committed -# manifests (embedded-Dolt cmd and storage, proxied-server cmd, server-Dolt -# storage). The variants (//cmd/bd:bd_embedded_test and -# //cmd/bd:bd_embedded_part2_test, //cmd/bd:bd_proxied_test, -# //internal/storage/embeddeddolt:embeddeddolt_embedded_test, -# //internal/storage/dolt:dolt_server_full_test) run them through -# tools/bazel/go_test_manifest_shard.sh, so each Bazel shard runs exactly the -# tests of the CI job with the same shard number. //scripts:go_test_sources_test -# checks the manifests and scripts against the tests they shard. -exports_files( - [ - ".github/scripts/embedded-cmd-test-shards.txt", - ".github/scripts/embedded-storage-test-shard.sh", - ".github/scripts/embedded-storage-test-shards.txt", - ".github/scripts/embedded-test-shard.sh", - ".github/scripts/proxied-cmd-test-shards.txt", - ".github/scripts/proxied-test-shard.sh", - ".github/scripts/server-storage-test-shard.sh", - ".github/scripts/server-storage-test-shards.txt", - ], - visibility = [ - "//cmd/bd:__pkg__", - "//internal/storage/dolt:__pkg__", - "//internal/storage/embeddeddolt:__pkg__", - "//scripts:__pkg__", - ], -) - -# //internal/versioncheck and //scripts/check-versions release-gate tests: the -# metadata surfaces versioncheck.releaseSources gates, plus the canonical -# version it compares them against and the tracked git hooks whose version -# markers it checks. -# -# Hand-listed rather than globbed because releaseSources is itself a closed -# list: a newly gated surface must be added here too, and until it is the test -# fails under Bazel (it passes under `go test`, which sees the whole checkout). -filegroup( - name = "release_metadata_files", - srcs = [ - ".claude-plugin/marketplace.json", - "integrations/beads-mcp/pyproject.toml", - "integrations/beads-mcp/src/beads_mcp/__init__.py", - "integrations/beads-mcp/uv.lock", - "npm-package/package.json", - "//cmd/bd:version.go", - "//cmd/bd:winres/manifest.xml", - "//cmd/bd:winres/winres.json", - "//plugins/beads:.claude-plugin/plugin.json", - "//plugins/beads:.codex-plugin/plugin.json", - "//plugins/beads:.copilot-plugin/plugin.json", - ] + glob([".githooks/*"]), - visibility = [ - "//internal/versioncheck:__pkg__", - "//scripts/check-versions:__pkg__", - ], -) - -go_library( - name = "beads", - srcs = [ - "beads.go", - "beads_cgo.go", - "beads_nocgo.go", - "claim.go", - ], - importpath = "github.com/steveyegge/beads", - visibility = ["//visibility:public"], - deps = [ - "//internal/beads", - "//internal/configfile", - "//internal/doltserver", - "//internal/storage", - "//internal/storage/backends", - "//internal/storage/dolt", - "//internal/storage/domain", - "//internal/storage/embeddeddolt", - "//internal/storage/schema", - "//internal/types", - "//internal/workspacegate", - ], -) - -go_test( - name = "beads_test", - srcs = [ - "beads_gated_test.go", - "beads_test.go", - "claim_roundtrip_test.go", - "claim_test.go", - "errors_test.go", - "export_dependency_add_options_test.go", - "issue_lifecycle_external_test.go", - "issue_roles_external_test.go", - "open_backend_selection_test.go", - "open_nocgo_test.go", - "query_interfaces_cgo_test.go", - "query_interfaces_test.go", - ], - embed = [":beads"], - deps = [ - "//internal/hooks", - "//internal/storage", - "//internal/storage/backends", - "//internal/storage/dolt", - "//internal/storage/domain", - "//internal/storage/embeddeddolt", - "//internal/telemetry", - "//internal/testutil", - "//internal/types", - "//internal/workspacegate", - "//issueops", - ], -) - -# //internal/httpclient/encode:encode_test regenerates and compares the shipped -# divergence ledger doc (TestDivergenceLedgerDocMatchesLedger), so the doc must -# be readable as data under `bazel test`. docsync_files above globs it too, but -# that filegroup's visibility is scoped to //test/docsync alone. -exports_files( - ["engdocs/design/http-divergence-ledger.md"], - visibility = ["//internal/httpclient/encode:__pkg__"], -) - -# --- begin go_srcs (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) --- - -filegroup( - name = "go_srcs", - srcs = glob( - ["**/*.go"], - allow_empty = True, - exclude = ["bazel-*/**"], - ), - visibility = ["//:__subpackages__"], -) - -# --- end go_srcs --- - -# --- begin repo_files (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) --- - -filegroup( - name = "repo_doc_files", - srcs = glob( - [ - "**/*.md", - "**/*.mdx", - ], - allow_empty = True, - exclude = [ - "**/node_modules/**", - ".agents/**", - ".amp/**", - ".augment/**", - ".bazelrc.local", - ".claude/*.lock", - ".claude/*.log", - ".claude/settings.local.json", - ".codex/**", - ".cursor/**", - ".direnv/**", - ".envrc", - ".git/**", - ".idea/**", - ".logs/**", - ".vscode/**", - "bazel-*/**", - "bd", - "bd-fixed", - "bd-original", - "bd-test", - "bd_test", - "beads", - "go.work", - "go.work.sum", - "history/**", - "mcp_agent_mail/**", - "npm-package/bin/*.tar.gz", - "npm-package/bin/*.zip", - "npm-package/bin/CHANGELOG.md", - "npm-package/bin/LICENSE", - "npm-package/bin/README.md", - "npm-package/bin/bd", - "npm-package/package-lock.json", - "output", - "result", - "state.json", - "user.bazelrc", - ], - ) + [ - "//backend:repo_doc_files", - "//backend/conformance:repo_doc_files", - "//backend/http:repo_doc_files", - "//backend/http/corpus:repo_doc_files", - "//beadserrors:repo_doc_files", - "//cmd/bd:repo_doc_files", - "//cmd/bd/doctor:repo_doc_files", - "//cmd/bd/doctor/fix:repo_doc_files", - "//cmd/bd/protocol:repo_doc_files", - "//cmd/bd/setup:repo_doc_files", - "//format:repo_doc_files", - "//internal/ado:repo_doc_files", - "//internal/atomicfile:repo_doc_files", - "//internal/audit:repo_doc_files", - "//internal/beads:repo_doc_files", - "//internal/ceiling:repo_doc_files", - "//internal/compact:repo_doc_files", - "//internal/config:repo_doc_files", - "//internal/configfile:repo_doc_files", - "//internal/creds:repo_doc_files", - "//internal/debug:repo_doc_files", - "//internal/doltremote:repo_doc_files", - "//internal/doltserver:repo_doc_files", - "//internal/doltversion:repo_doc_files", - "//internal/eventsjournal:repo_doc_files", - "//internal/execenv:repo_doc_files", - "//internal/fdhygiene:repo_doc_files", - "//internal/formula:repo_doc_files", - "//internal/formula/cmd/schemagen:repo_doc_files", - "//internal/formula/schemagen:repo_doc_files", - "//internal/git:repo_doc_files", - "//internal/gitenv:repo_doc_files", - "//internal/githooksenv:repo_doc_files", - "//internal/github:repo_doc_files", - "//internal/gitignore:repo_doc_files", - "//internal/gitlab:repo_doc_files", - "//internal/gittraceenv:repo_doc_files", - "//internal/hooks:repo_doc_files", - "//internal/httpapi:repo_doc_files", - "//internal/httpapi/apigen:repo_doc_files", - "//internal/httpapi/spec:repo_doc_files", - "//internal/httpapi/wireshape:repo_doc_files", - "//internal/httpapi/wireshape/cmd/gendigest:repo_doc_files", - "//internal/httpclient:repo_doc_files", - "//internal/httpclient/encode:repo_doc_files", - "//internal/httpclient/wire:repo_doc_files", - "//internal/idgen:repo_doc_files", - "//internal/initprocs:repo_doc_files", - "//internal/jira:repo_doc_files", - "//internal/linear:repo_doc_files", - "//internal/lockfile:repo_doc_files", - "//internal/memoryapi:repo_doc_files", - "//internal/metrics:repo_doc_files", - "//internal/migration:repo_doc_files", - "//internal/migration/legacysqlite:repo_doc_files", - "//internal/molecules:repo_doc_files", - "//internal/notion:repo_doc_files", - "//internal/procid:repo_doc_files", - "//internal/query:repo_doc_files", - "//internal/recipes:repo_doc_files", - "//internal/remotecache:repo_doc_files", - "//internal/routing:repo_doc_files", - "//internal/storage:repo_doc_files", - "//internal/storage/backendnames:repo_doc_files", - "//internal/storage/backends:repo_doc_files", - "//internal/storage/batchbench:repo_doc_files", - "//internal/storage/batchfixtures:repo_doc_files", - "//internal/storage/contextinfo:repo_doc_files", - "//internal/storage/createbatchequiv:repo_doc_files", - "//internal/storage/dberrors:repo_doc_files", - "//internal/storage/dbproxy/identity:repo_doc_files", - "//internal/storage/dbproxy/pidfile:repo_doc_files", - "//internal/storage/dbproxy/proxy:repo_doc_files", - "//internal/storage/dbproxy/server:repo_doc_files", - "//internal/storage/dbproxy/util:repo_doc_files", - "//internal/storage/depid:repo_doc_files", - "//internal/storage/dolt:repo_doc_files", - "//internal/storage/doltutil:repo_doc_files", - "//internal/storage/domain:repo_doc_files", - "//internal/storage/domain/db:repo_doc_files", - "//internal/storage/domain/fs:repo_doc_files", - "//internal/storage/domain/git:repo_doc_files", - "//internal/storage/embeddeddolt:repo_doc_files", - "//internal/storage/embeddeddolt/cmd:repo_doc_files", - "//internal/storage/externaldeps:repo_doc_files", - "//internal/storage/fs:repo_doc_files", - "//internal/storage/git:repo_doc_files", - "//internal/storage/historyoffequiv:repo_doc_files", - "//internal/storage/issueops:repo_doc_files", - "//internal/storage/journalscan:repo_doc_files", - "//internal/storage/kvkeys:repo_doc_files", - "//internal/storage/memoryops:repo_doc_files", - "//internal/storage/rowid:repo_doc_files", - "//internal/storage/schema:repo_doc_files", - "//internal/storage/sqlbuild:repo_doc_files", - "//internal/storage/sqlclass:repo_doc_files", - "//internal/storage/sqlcount:repo_doc_files", - "//internal/storage/storagecontract:repo_doc_files", - "//internal/storage/unsupportedgen:repo_doc_files", - "//internal/storage/uow:repo_doc_files", - "//internal/storage/versioncontrolops:repo_doc_files", - "//internal/telemetry:repo_doc_files", - "//internal/templates:repo_doc_files", - "//internal/templates/agents:repo_doc_files", - "//internal/testutil:repo_doc_files", - "//internal/testutil/bazeltest:repo_doc_files", - "//internal/testutil/credentialcmd:repo_doc_files", - "//internal/testutil/fixtures:repo_doc_files", - "//internal/testutil/integration:repo_doc_files", - "//internal/timeparsing:repo_doc_files", - "//internal/tracker:repo_doc_files", - "//internal/tracker/conformance:repo_doc_files", - "//internal/types:repo_doc_files", - "//internal/ui:repo_doc_files", - "//internal/uimd:repo_doc_files", - "//internal/utils:repo_doc_files", - "//internal/validation:repo_doc_files", - "//internal/versioncheck:repo_doc_files", - "//internal/versionedhistory:repo_doc_files", - "//internal/workapi:repo_doc_files", - "//internal/workapi/storecounter:repo_doc_files", - "//internal/workapi/storequerier:repo_doc_files", - "//internal/workapi/storereader:repo_doc_files", - "//internal/workapi/storereadycounter:repo_doc_files", - "//internal/workapi/storestats:repo_doc_files", - "//internal/workapi/storeversionreconciler:repo_doc_files", - "//internal/workapi/storeworkspaceconfig:repo_doc_files", - "//internal/workspacegate:repo_doc_files", - "//internal/worktreeremove:repo_doc_files", - "//issueops:repo_doc_files", - "//journalops:repo_doc_files", - "//memoryops:repo_doc_files", - "//platforms:repo_doc_files", - "//plugins/beads:repo_doc_files", - "//schema:repo_doc_files", - "//scripts:repo_doc_files", - "//scripts/bench-ready-indexes:repo_doc_files", - "//scripts/check-versions:repo_doc_files", - "//scripts/checkworkflowtags:repo_doc_files", - "//scripts/gitattributespolicy:repo_doc_files", - "//scripts/internal/testbash:repo_doc_files", - "//scripts/migration-test/catalog:repo_doc_files", - "//scripts/pr-lint:repo_doc_files", - "//scripts/prlintmake:repo_doc_files", - "//scripts/repochecks:repo_doc_files", - "//scripts/repro-dolt-hang:repo_doc_files", - "//scripts/repro-dolt-prod-timeouts:repo_doc_files", - "//scripts/workflowtags:repo_doc_files", - "//test/conformance:repo_doc_files", - "//test/docsync:repo_doc_files", - "//test/embedder:repo_doc_files", - "//test/testmainconvention:repo_doc_files", - "//tests/hook_timeout_backends:repo_doc_files", - "//tests/migration:repo_doc_files", - "//tests/regression:repo_doc_files", - "//tests/upgrade_smoke:repo_doc_files", - "//third_party/cc_toolchain:repo_doc_files", - "//third_party/patches:repo_doc_files", - "//tools/bazel:repo_doc_files", - "//tools/docsmint:repo_doc_files", - "//tools/nogo:repo_doc_files", - "//tools/nogo/analyzers/depguard:repo_doc_files", - "//tools/nogo/analyzers/errcheck:repo_doc_files", - "//tools/nogo/analyzers/forbidigo:repo_doc_files", - "//tools/nogo/analyzers/gosec:repo_doc_files", - "//tools/nogo/analyzers/misspell:repo_doc_files", - "//tools/nogo/analyzers/sloglint:repo_doc_files", - "//tools/nogo/analyzers/unconvert:repo_doc_files", - "//tools/nogo/analyzers/unparam:repo_doc_files", - "//tools/nogo/internal/analyzertest:repo_doc_files", - "//tools/nogo/internal/golangci:repo_doc_files", - "//tools/nogo/internal/position:repo_doc_files", - ], - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_files", - srcs = [ - ":repo_doc_files", - ":repo_go_srcs", - ":repo_go_test_srcs", - ":repo_other_files", - ], - visibility = [ - "//scripts:__pkg__", - "//scripts/repochecks:__pkg__", - ], -) - -filegroup( - name = "repo_go_srcs", - srcs = glob( - ["**/*.go"], - allow_empty = True, - exclude = [ - "**/*_test.go", - "**/node_modules/**", - ".agents/**", - ".amp/**", - ".augment/**", - ".bazelrc.local", - ".claude/*.lock", - ".claude/*.log", - ".claude/settings.local.json", - ".codex/**", - ".cursor/**", - ".direnv/**", - ".envrc", - ".git/**", - ".idea/**", - ".logs/**", - ".vscode/**", - "bazel-*/**", - "bd", - "bd-fixed", - "bd-original", - "bd-test", - "bd_test", - "beads", - "go.work", - "go.work.sum", - "history/**", - "mcp_agent_mail/**", - "npm-package/bin/*.tar.gz", - "npm-package/bin/*.zip", - "npm-package/bin/CHANGELOG.md", - "npm-package/bin/LICENSE", - "npm-package/bin/README.md", - "npm-package/bin/bd", - "npm-package/package-lock.json", - "output", - "result", - "state.json", - "user.bazelrc", - ], - ) + [ - "//backend:repo_go_srcs", - "//backend/conformance:repo_go_srcs", - "//backend/http:repo_go_srcs", - "//backend/http/corpus:repo_go_srcs", - "//beadserrors:repo_go_srcs", - "//cmd/bd:repo_go_srcs", - "//cmd/bd/doctor:repo_go_srcs", - "//cmd/bd/doctor/fix:repo_go_srcs", - "//cmd/bd/protocol:repo_go_srcs", - "//cmd/bd/setup:repo_go_srcs", - "//format:repo_go_srcs", - "//internal/ado:repo_go_srcs", - "//internal/atomicfile:repo_go_srcs", - "//internal/audit:repo_go_srcs", - "//internal/beads:repo_go_srcs", - "//internal/ceiling:repo_go_srcs", - "//internal/compact:repo_go_srcs", - "//internal/config:repo_go_srcs", - "//internal/configfile:repo_go_srcs", - "//internal/creds:repo_go_srcs", - "//internal/debug:repo_go_srcs", - "//internal/doltremote:repo_go_srcs", - "//internal/doltserver:repo_go_srcs", - "//internal/doltversion:repo_go_srcs", - "//internal/eventsjournal:repo_go_srcs", - "//internal/execenv:repo_go_srcs", - "//internal/fdhygiene:repo_go_srcs", - "//internal/formula:repo_go_srcs", - "//internal/formula/cmd/schemagen:repo_go_srcs", - "//internal/formula/schemagen:repo_go_srcs", - "//internal/git:repo_go_srcs", - "//internal/gitenv:repo_go_srcs", - "//internal/githooksenv:repo_go_srcs", - "//internal/github:repo_go_srcs", - "//internal/gitignore:repo_go_srcs", - "//internal/gitlab:repo_go_srcs", - "//internal/gittraceenv:repo_go_srcs", - "//internal/hooks:repo_go_srcs", - "//internal/httpapi:repo_go_srcs", - "//internal/httpapi/apigen:repo_go_srcs", - "//internal/httpapi/spec:repo_go_srcs", - "//internal/httpapi/wireshape:repo_go_srcs", - "//internal/httpapi/wireshape/cmd/gendigest:repo_go_srcs", - "//internal/httpclient:repo_go_srcs", - "//internal/httpclient/encode:repo_go_srcs", - "//internal/httpclient/wire:repo_go_srcs", - "//internal/idgen:repo_go_srcs", - "//internal/initprocs:repo_go_srcs", - "//internal/jira:repo_go_srcs", - "//internal/linear:repo_go_srcs", - "//internal/lockfile:repo_go_srcs", - "//internal/memoryapi:repo_go_srcs", - "//internal/metrics:repo_go_srcs", - "//internal/migration:repo_go_srcs", - "//internal/migration/legacysqlite:repo_go_srcs", - "//internal/molecules:repo_go_srcs", - "//internal/notion:repo_go_srcs", - "//internal/procid:repo_go_srcs", - "//internal/query:repo_go_srcs", - "//internal/recipes:repo_go_srcs", - "//internal/remotecache:repo_go_srcs", - "//internal/routing:repo_go_srcs", - "//internal/storage:repo_go_srcs", - "//internal/storage/backendnames:repo_go_srcs", - "//internal/storage/backends:repo_go_srcs", - "//internal/storage/batchbench:repo_go_srcs", - "//internal/storage/batchfixtures:repo_go_srcs", - "//internal/storage/contextinfo:repo_go_srcs", - "//internal/storage/createbatchequiv:repo_go_srcs", - "//internal/storage/dberrors:repo_go_srcs", - "//internal/storage/dbproxy/identity:repo_go_srcs", - "//internal/storage/dbproxy/pidfile:repo_go_srcs", - "//internal/storage/dbproxy/proxy:repo_go_srcs", - "//internal/storage/dbproxy/server:repo_go_srcs", - "//internal/storage/dbproxy/util:repo_go_srcs", - "//internal/storage/depid:repo_go_srcs", - "//internal/storage/dolt:repo_go_srcs", - "//internal/storage/doltutil:repo_go_srcs", - "//internal/storage/domain:repo_go_srcs", - "//internal/storage/domain/db:repo_go_srcs", - "//internal/storage/domain/fs:repo_go_srcs", - "//internal/storage/domain/git:repo_go_srcs", - "//internal/storage/embeddeddolt:repo_go_srcs", - "//internal/storage/embeddeddolt/cmd:repo_go_srcs", - "//internal/storage/externaldeps:repo_go_srcs", - "//internal/storage/fs:repo_go_srcs", - "//internal/storage/git:repo_go_srcs", - "//internal/storage/historyoffequiv:repo_go_srcs", - "//internal/storage/issueops:repo_go_srcs", - "//internal/storage/journalscan:repo_go_srcs", - "//internal/storage/kvkeys:repo_go_srcs", - "//internal/storage/memoryops:repo_go_srcs", - "//internal/storage/rowid:repo_go_srcs", - "//internal/storage/schema:repo_go_srcs", - "//internal/storage/sqlbuild:repo_go_srcs", - "//internal/storage/sqlclass:repo_go_srcs", - "//internal/storage/sqlcount:repo_go_srcs", - "//internal/storage/storagecontract:repo_go_srcs", - "//internal/storage/unsupportedgen:repo_go_srcs", - "//internal/storage/uow:repo_go_srcs", - "//internal/storage/versioncontrolops:repo_go_srcs", - "//internal/telemetry:repo_go_srcs", - "//internal/templates:repo_go_srcs", - "//internal/templates/agents:repo_go_srcs", - "//internal/testutil:repo_go_srcs", - "//internal/testutil/bazeltest:repo_go_srcs", - "//internal/testutil/credentialcmd:repo_go_srcs", - "//internal/testutil/fixtures:repo_go_srcs", - "//internal/testutil/integration:repo_go_srcs", - "//internal/timeparsing:repo_go_srcs", - "//internal/tracker:repo_go_srcs", - "//internal/tracker/conformance:repo_go_srcs", - "//internal/types:repo_go_srcs", - "//internal/ui:repo_go_srcs", - "//internal/uimd:repo_go_srcs", - "//internal/utils:repo_go_srcs", - "//internal/validation:repo_go_srcs", - "//internal/versioncheck:repo_go_srcs", - "//internal/versionedhistory:repo_go_srcs", - "//internal/workapi:repo_go_srcs", - "//internal/workapi/storecounter:repo_go_srcs", - "//internal/workapi/storequerier:repo_go_srcs", - "//internal/workapi/storereader:repo_go_srcs", - "//internal/workapi/storereadycounter:repo_go_srcs", - "//internal/workapi/storestats:repo_go_srcs", - "//internal/workapi/storeversionreconciler:repo_go_srcs", - "//internal/workapi/storeworkspaceconfig:repo_go_srcs", - "//internal/workspacegate:repo_go_srcs", - "//internal/worktreeremove:repo_go_srcs", - "//issueops:repo_go_srcs", - "//journalops:repo_go_srcs", - "//memoryops:repo_go_srcs", - "//platforms:repo_go_srcs", - "//plugins/beads:repo_go_srcs", - "//schema:repo_go_srcs", - "//scripts:repo_go_srcs", - "//scripts/bench-ready-indexes:repo_go_srcs", - "//scripts/check-versions:repo_go_srcs", - "//scripts/checkworkflowtags:repo_go_srcs", - "//scripts/gitattributespolicy:repo_go_srcs", - "//scripts/internal/testbash:repo_go_srcs", - "//scripts/migration-test/catalog:repo_go_srcs", - "//scripts/pr-lint:repo_go_srcs", - "//scripts/prlintmake:repo_go_srcs", - "//scripts/repochecks:repo_go_srcs", - "//scripts/repro-dolt-hang:repo_go_srcs", - "//scripts/repro-dolt-prod-timeouts:repo_go_srcs", - "//scripts/workflowtags:repo_go_srcs", - "//test/conformance:repo_go_srcs", - "//test/docsync:repo_go_srcs", - "//test/embedder:repo_go_srcs", - "//test/testmainconvention:repo_go_srcs", - "//tests/hook_timeout_backends:repo_go_srcs", - "//tests/migration:repo_go_srcs", - "//tests/regression:repo_go_srcs", - "//tests/upgrade_smoke:repo_go_srcs", - "//third_party/cc_toolchain:repo_go_srcs", - "//third_party/patches:repo_go_srcs", - "//tools/bazel:repo_go_srcs", - "//tools/docsmint:repo_go_srcs", - "//tools/nogo:repo_go_srcs", - "//tools/nogo/analyzers/depguard:repo_go_srcs", - "//tools/nogo/analyzers/errcheck:repo_go_srcs", - "//tools/nogo/analyzers/forbidigo:repo_go_srcs", - "//tools/nogo/analyzers/gosec:repo_go_srcs", - "//tools/nogo/analyzers/misspell:repo_go_srcs", - "//tools/nogo/analyzers/sloglint:repo_go_srcs", - "//tools/nogo/analyzers/unconvert:repo_go_srcs", - "//tools/nogo/analyzers/unparam:repo_go_srcs", - "//tools/nogo/internal/analyzertest:repo_go_srcs", - "//tools/nogo/internal/golangci:repo_go_srcs", - "//tools/nogo/internal/position:repo_go_srcs", - ], - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_go_test_srcs", - srcs = glob( - ["**/*_test.go"], - allow_empty = True, - exclude = [ - "**/node_modules/**", - ".agents/**", - ".amp/**", - ".augment/**", - ".bazelrc.local", - ".claude/*.lock", - ".claude/*.log", - ".claude/settings.local.json", - ".codex/**", - ".cursor/**", - ".direnv/**", - ".envrc", - ".git/**", - ".idea/**", - ".logs/**", - ".vscode/**", - "bazel-*/**", - "bd", - "bd-fixed", - "bd-original", - "bd-test", - "bd_test", - "beads", - "go.work", - "go.work.sum", - "history/**", - "mcp_agent_mail/**", - "npm-package/bin/*.tar.gz", - "npm-package/bin/*.zip", - "npm-package/bin/CHANGELOG.md", - "npm-package/bin/LICENSE", - "npm-package/bin/README.md", - "npm-package/bin/bd", - "npm-package/package-lock.json", - "output", - "result", - "state.json", - "user.bazelrc", - ], - ) + [ - "//backend:repo_go_test_srcs", - "//backend/conformance:repo_go_test_srcs", - "//backend/http:repo_go_test_srcs", - "//backend/http/corpus:repo_go_test_srcs", - "//beadserrors:repo_go_test_srcs", - "//cmd/bd:repo_go_test_srcs", - "//cmd/bd/doctor:repo_go_test_srcs", - "//cmd/bd/doctor/fix:repo_go_test_srcs", - "//cmd/bd/protocol:repo_go_test_srcs", - "//cmd/bd/setup:repo_go_test_srcs", - "//format:repo_go_test_srcs", - "//internal/ado:repo_go_test_srcs", - "//internal/atomicfile:repo_go_test_srcs", - "//internal/audit:repo_go_test_srcs", - "//internal/beads:repo_go_test_srcs", - "//internal/ceiling:repo_go_test_srcs", - "//internal/compact:repo_go_test_srcs", - "//internal/config:repo_go_test_srcs", - "//internal/configfile:repo_go_test_srcs", - "//internal/creds:repo_go_test_srcs", - "//internal/debug:repo_go_test_srcs", - "//internal/doltremote:repo_go_test_srcs", - "//internal/doltserver:repo_go_test_srcs", - "//internal/doltversion:repo_go_test_srcs", - "//internal/eventsjournal:repo_go_test_srcs", - "//internal/execenv:repo_go_test_srcs", - "//internal/fdhygiene:repo_go_test_srcs", - "//internal/formula:repo_go_test_srcs", - "//internal/formula/cmd/schemagen:repo_go_test_srcs", - "//internal/formula/schemagen:repo_go_test_srcs", - "//internal/git:repo_go_test_srcs", - "//internal/gitenv:repo_go_test_srcs", - "//internal/githooksenv:repo_go_test_srcs", - "//internal/github:repo_go_test_srcs", - "//internal/gitignore:repo_go_test_srcs", - "//internal/gitlab:repo_go_test_srcs", - "//internal/gittraceenv:repo_go_test_srcs", - "//internal/hooks:repo_go_test_srcs", - "//internal/httpapi:repo_go_test_srcs", - "//internal/httpapi/apigen:repo_go_test_srcs", - "//internal/httpapi/spec:repo_go_test_srcs", - "//internal/httpapi/wireshape:repo_go_test_srcs", - "//internal/httpapi/wireshape/cmd/gendigest:repo_go_test_srcs", - "//internal/httpclient:repo_go_test_srcs", - "//internal/httpclient/encode:repo_go_test_srcs", - "//internal/httpclient/wire:repo_go_test_srcs", - "//internal/idgen:repo_go_test_srcs", - "//internal/initprocs:repo_go_test_srcs", - "//internal/jira:repo_go_test_srcs", - "//internal/linear:repo_go_test_srcs", - "//internal/lockfile:repo_go_test_srcs", - "//internal/memoryapi:repo_go_test_srcs", - "//internal/metrics:repo_go_test_srcs", - "//internal/migration:repo_go_test_srcs", - "//internal/migration/legacysqlite:repo_go_test_srcs", - "//internal/molecules:repo_go_test_srcs", - "//internal/notion:repo_go_test_srcs", - "//internal/procid:repo_go_test_srcs", - "//internal/query:repo_go_test_srcs", - "//internal/recipes:repo_go_test_srcs", - "//internal/remotecache:repo_go_test_srcs", - "//internal/routing:repo_go_test_srcs", - "//internal/storage:repo_go_test_srcs", - "//internal/storage/backendnames:repo_go_test_srcs", - "//internal/storage/backends:repo_go_test_srcs", - "//internal/storage/batchbench:repo_go_test_srcs", - "//internal/storage/batchfixtures:repo_go_test_srcs", - "//internal/storage/contextinfo:repo_go_test_srcs", - "//internal/storage/createbatchequiv:repo_go_test_srcs", - "//internal/storage/dberrors:repo_go_test_srcs", - "//internal/storage/dbproxy/identity:repo_go_test_srcs", - "//internal/storage/dbproxy/pidfile:repo_go_test_srcs", - "//internal/storage/dbproxy/proxy:repo_go_test_srcs", - "//internal/storage/dbproxy/server:repo_go_test_srcs", - "//internal/storage/dbproxy/util:repo_go_test_srcs", - "//internal/storage/depid:repo_go_test_srcs", - "//internal/storage/dolt:repo_go_test_srcs", - "//internal/storage/doltutil:repo_go_test_srcs", - "//internal/storage/domain:repo_go_test_srcs", - "//internal/storage/domain/db:repo_go_test_srcs", - "//internal/storage/domain/fs:repo_go_test_srcs", - "//internal/storage/domain/git:repo_go_test_srcs", - "//internal/storage/embeddeddolt:repo_go_test_srcs", - "//internal/storage/embeddeddolt/cmd:repo_go_test_srcs", - "//internal/storage/externaldeps:repo_go_test_srcs", - "//internal/storage/fs:repo_go_test_srcs", - "//internal/storage/git:repo_go_test_srcs", - "//internal/storage/historyoffequiv:repo_go_test_srcs", - "//internal/storage/issueops:repo_go_test_srcs", - "//internal/storage/journalscan:repo_go_test_srcs", - "//internal/storage/kvkeys:repo_go_test_srcs", - "//internal/storage/memoryops:repo_go_test_srcs", - "//internal/storage/rowid:repo_go_test_srcs", - "//internal/storage/schema:repo_go_test_srcs", - "//internal/storage/sqlbuild:repo_go_test_srcs", - "//internal/storage/sqlclass:repo_go_test_srcs", - "//internal/storage/sqlcount:repo_go_test_srcs", - "//internal/storage/storagecontract:repo_go_test_srcs", - "//internal/storage/unsupportedgen:repo_go_test_srcs", - "//internal/storage/uow:repo_go_test_srcs", - "//internal/storage/versioncontrolops:repo_go_test_srcs", - "//internal/telemetry:repo_go_test_srcs", - "//internal/templates:repo_go_test_srcs", - "//internal/templates/agents:repo_go_test_srcs", - "//internal/testutil:repo_go_test_srcs", - "//internal/testutil/bazeltest:repo_go_test_srcs", - "//internal/testutil/credentialcmd:repo_go_test_srcs", - "//internal/testutil/fixtures:repo_go_test_srcs", - "//internal/testutil/integration:repo_go_test_srcs", - "//internal/timeparsing:repo_go_test_srcs", - "//internal/tracker:repo_go_test_srcs", - "//internal/tracker/conformance:repo_go_test_srcs", - "//internal/types:repo_go_test_srcs", - "//internal/ui:repo_go_test_srcs", - "//internal/uimd:repo_go_test_srcs", - "//internal/utils:repo_go_test_srcs", - "//internal/validation:repo_go_test_srcs", - "//internal/versioncheck:repo_go_test_srcs", - "//internal/versionedhistory:repo_go_test_srcs", - "//internal/workapi:repo_go_test_srcs", - "//internal/workapi/storecounter:repo_go_test_srcs", - "//internal/workapi/storequerier:repo_go_test_srcs", - "//internal/workapi/storereader:repo_go_test_srcs", - "//internal/workapi/storereadycounter:repo_go_test_srcs", - "//internal/workapi/storestats:repo_go_test_srcs", - "//internal/workapi/storeversionreconciler:repo_go_test_srcs", - "//internal/workapi/storeworkspaceconfig:repo_go_test_srcs", - "//internal/workspacegate:repo_go_test_srcs", - "//internal/worktreeremove:repo_go_test_srcs", - "//issueops:repo_go_test_srcs", - "//journalops:repo_go_test_srcs", - "//memoryops:repo_go_test_srcs", - "//platforms:repo_go_test_srcs", - "//plugins/beads:repo_go_test_srcs", - "//schema:repo_go_test_srcs", - "//scripts:repo_go_test_srcs", - "//scripts/bench-ready-indexes:repo_go_test_srcs", - "//scripts/check-versions:repo_go_test_srcs", - "//scripts/checkworkflowtags:repo_go_test_srcs", - "//scripts/gitattributespolicy:repo_go_test_srcs", - "//scripts/internal/testbash:repo_go_test_srcs", - "//scripts/migration-test/catalog:repo_go_test_srcs", - "//scripts/pr-lint:repo_go_test_srcs", - "//scripts/prlintmake:repo_go_test_srcs", - "//scripts/repochecks:repo_go_test_srcs", - "//scripts/repro-dolt-hang:repo_go_test_srcs", - "//scripts/repro-dolt-prod-timeouts:repo_go_test_srcs", - "//scripts/workflowtags:repo_go_test_srcs", - "//test/conformance:repo_go_test_srcs", - "//test/docsync:repo_go_test_srcs", - "//test/embedder:repo_go_test_srcs", - "//test/testmainconvention:repo_go_test_srcs", - "//tests/hook_timeout_backends:repo_go_test_srcs", - "//tests/migration:repo_go_test_srcs", - "//tests/regression:repo_go_test_srcs", - "//tests/upgrade_smoke:repo_go_test_srcs", - "//third_party/cc_toolchain:repo_go_test_srcs", - "//third_party/patches:repo_go_test_srcs", - "//tools/bazel:repo_go_test_srcs", - "//tools/docsmint:repo_go_test_srcs", - "//tools/nogo:repo_go_test_srcs", - "//tools/nogo/analyzers/depguard:repo_go_test_srcs", - "//tools/nogo/analyzers/errcheck:repo_go_test_srcs", - "//tools/nogo/analyzers/forbidigo:repo_go_test_srcs", - "//tools/nogo/analyzers/gosec:repo_go_test_srcs", - "//tools/nogo/analyzers/misspell:repo_go_test_srcs", - "//tools/nogo/analyzers/sloglint:repo_go_test_srcs", - "//tools/nogo/analyzers/unconvert:repo_go_test_srcs", - "//tools/nogo/analyzers/unparam:repo_go_test_srcs", - "//tools/nogo/internal/analyzertest:repo_go_test_srcs", - "//tools/nogo/internal/golangci:repo_go_test_srcs", - "//tools/nogo/internal/position:repo_go_test_srcs", - ], - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_other_files", - srcs = glob( - ["**"], - exclude = [ - "**/*.db", - "**/*.exe", - "**/*.go", - "**/*.md", - "**/*.mdx", - "**/*.out", - "**/*.prof", - "**/*.pyc", - "**/*.test", - "**/__pycache__/**", - "**/node_modules/**", - ".agents/**", - ".amp/**", - ".augment/**", - ".bazelrc.local", - ".claude/*.lock", - ".claude/*.log", - ".claude/settings.local.json", - ".codex/**", - ".cursor/**", - ".direnv/**", - ".envrc", - ".git/**", - ".idea/**", - ".logs/**", - ".vscode/**", - "bazel-*/**", - "bd", - "bd-fixed", - "bd-original", - "bd-test", - "bd_test", - "beads", - "go.work", - "go.work.sum", - "history/**", - "mcp_agent_mail/**", - "npm-package/bin/*.tar.gz", - "npm-package/bin/*.zip", - "npm-package/bin/CHANGELOG.md", - "npm-package/bin/LICENSE", - "npm-package/bin/README.md", - "npm-package/bin/bd", - "npm-package/package-lock.json", - "output", - "result", - "state.json", - "user.bazelrc", - ], - ) + [ - "//backend:repo_other_files", - "//backend/conformance:repo_other_files", - "//backend/http:repo_other_files", - "//backend/http/corpus:repo_other_files", - "//beadserrors:repo_other_files", - "//cmd/bd:repo_other_files", - "//cmd/bd/doctor:repo_other_files", - "//cmd/bd/doctor/fix:repo_other_files", - "//cmd/bd/protocol:repo_other_files", - "//cmd/bd/setup:repo_other_files", - "//format:repo_other_files", - "//internal/ado:repo_other_files", - "//internal/atomicfile:repo_other_files", - "//internal/audit:repo_other_files", - "//internal/beads:repo_other_files", - "//internal/ceiling:repo_other_files", - "//internal/compact:repo_other_files", - "//internal/config:repo_other_files", - "//internal/configfile:repo_other_files", - "//internal/creds:repo_other_files", - "//internal/debug:repo_other_files", - "//internal/doltremote:repo_other_files", - "//internal/doltserver:repo_other_files", - "//internal/doltversion:repo_other_files", - "//internal/eventsjournal:repo_other_files", - "//internal/execenv:repo_other_files", - "//internal/fdhygiene:repo_other_files", - "//internal/formula:repo_other_files", - "//internal/formula/cmd/schemagen:repo_other_files", - "//internal/formula/schemagen:repo_other_files", - "//internal/git:repo_other_files", - "//internal/gitenv:repo_other_files", - "//internal/githooksenv:repo_other_files", - "//internal/github:repo_other_files", - "//internal/gitignore:repo_other_files", - "//internal/gitlab:repo_other_files", - "//internal/gittraceenv:repo_other_files", - "//internal/hooks:repo_other_files", - "//internal/httpapi:repo_other_files", - "//internal/httpapi/apigen:repo_other_files", - "//internal/httpapi/spec:repo_other_files", - "//internal/httpapi/wireshape:repo_other_files", - "//internal/httpapi/wireshape/cmd/gendigest:repo_other_files", - "//internal/httpclient:repo_other_files", - "//internal/httpclient/encode:repo_other_files", - "//internal/httpclient/wire:repo_other_files", - "//internal/idgen:repo_other_files", - "//internal/initprocs:repo_other_files", - "//internal/jira:repo_other_files", - "//internal/linear:repo_other_files", - "//internal/lockfile:repo_other_files", - "//internal/memoryapi:repo_other_files", - "//internal/metrics:repo_other_files", - "//internal/migration:repo_other_files", - "//internal/migration/legacysqlite:repo_other_files", - "//internal/molecules:repo_other_files", - "//internal/notion:repo_other_files", - "//internal/procid:repo_other_files", - "//internal/query:repo_other_files", - "//internal/recipes:repo_other_files", - "//internal/remotecache:repo_other_files", - "//internal/routing:repo_other_files", - "//internal/storage:repo_other_files", - "//internal/storage/backendnames:repo_other_files", - "//internal/storage/backends:repo_other_files", - "//internal/storage/batchbench:repo_other_files", - "//internal/storage/batchfixtures:repo_other_files", - "//internal/storage/contextinfo:repo_other_files", - "//internal/storage/createbatchequiv:repo_other_files", - "//internal/storage/dberrors:repo_other_files", - "//internal/storage/dbproxy/identity:repo_other_files", - "//internal/storage/dbproxy/pidfile:repo_other_files", - "//internal/storage/dbproxy/proxy:repo_other_files", - "//internal/storage/dbproxy/server:repo_other_files", - "//internal/storage/dbproxy/util:repo_other_files", - "//internal/storage/depid:repo_other_files", - "//internal/storage/dolt:repo_other_files", - "//internal/storage/doltutil:repo_other_files", - "//internal/storage/domain:repo_other_files", - "//internal/storage/domain/db:repo_other_files", - "//internal/storage/domain/fs:repo_other_files", - "//internal/storage/domain/git:repo_other_files", - "//internal/storage/embeddeddolt:repo_other_files", - "//internal/storage/embeddeddolt/cmd:repo_other_files", - "//internal/storage/externaldeps:repo_other_files", - "//internal/storage/fs:repo_other_files", - "//internal/storage/git:repo_other_files", - "//internal/storage/historyoffequiv:repo_other_files", - "//internal/storage/issueops:repo_other_files", - "//internal/storage/journalscan:repo_other_files", - "//internal/storage/kvkeys:repo_other_files", - "//internal/storage/memoryops:repo_other_files", - "//internal/storage/rowid:repo_other_files", - "//internal/storage/schema:repo_other_files", - "//internal/storage/sqlbuild:repo_other_files", - "//internal/storage/sqlclass:repo_other_files", - "//internal/storage/sqlcount:repo_other_files", - "//internal/storage/storagecontract:repo_other_files", - "//internal/storage/unsupportedgen:repo_other_files", - "//internal/storage/uow:repo_other_files", - "//internal/storage/versioncontrolops:repo_other_files", - "//internal/telemetry:repo_other_files", - "//internal/templates:repo_other_files", - "//internal/templates/agents:repo_other_files", - "//internal/testutil:repo_other_files", - "//internal/testutil/bazeltest:repo_other_files", - "//internal/testutil/credentialcmd:repo_other_files", - "//internal/testutil/fixtures:repo_other_files", - "//internal/testutil/integration:repo_other_files", - "//internal/timeparsing:repo_other_files", - "//internal/tracker:repo_other_files", - "//internal/tracker/conformance:repo_other_files", - "//internal/types:repo_other_files", - "//internal/ui:repo_other_files", - "//internal/uimd:repo_other_files", - "//internal/utils:repo_other_files", - "//internal/validation:repo_other_files", - "//internal/versioncheck:repo_other_files", - "//internal/versionedhistory:repo_other_files", - "//internal/workapi:repo_other_files", - "//internal/workapi/storecounter:repo_other_files", - "//internal/workapi/storequerier:repo_other_files", - "//internal/workapi/storereader:repo_other_files", - "//internal/workapi/storereadycounter:repo_other_files", - "//internal/workapi/storestats:repo_other_files", - "//internal/workapi/storeversionreconciler:repo_other_files", - "//internal/workapi/storeworkspaceconfig:repo_other_files", - "//internal/workspacegate:repo_other_files", - "//internal/worktreeremove:repo_other_files", - "//issueops:repo_other_files", - "//journalops:repo_other_files", - "//memoryops:repo_other_files", - "//platforms:repo_other_files", - "//plugins/beads:repo_other_files", - "//schema:repo_other_files", - "//scripts:repo_other_files", - "//scripts/bench-ready-indexes:repo_other_files", - "//scripts/check-versions:repo_other_files", - "//scripts/checkworkflowtags:repo_other_files", - "//scripts/gitattributespolicy:repo_other_files", - "//scripts/internal/testbash:repo_other_files", - "//scripts/migration-test/catalog:repo_other_files", - "//scripts/pr-lint:repo_other_files", - "//scripts/prlintmake:repo_other_files", - "//scripts/repochecks:repo_other_files", - "//scripts/repro-dolt-hang:repo_other_files", - "//scripts/repro-dolt-prod-timeouts:repo_other_files", - "//scripts/workflowtags:repo_other_files", - "//test/conformance:repo_other_files", - "//test/docsync:repo_other_files", - "//test/embedder:repo_other_files", - "//test/testmainconvention:repo_other_files", - "//tests/hook_timeout_backends:repo_other_files", - "//tests/migration:repo_other_files", - "//tests/regression:repo_other_files", - "//tests/upgrade_smoke:repo_other_files", - "//third_party/cc_toolchain:repo_other_files", - "//third_party/patches:repo_other_files", - "//tools/bazel:repo_other_files", - "//tools/docsmint:repo_other_files", - "//tools/nogo:repo_other_files", - "//tools/nogo/analyzers/depguard:repo_other_files", - "//tools/nogo/analyzers/errcheck:repo_other_files", - "//tools/nogo/analyzers/forbidigo:repo_other_files", - "//tools/nogo/analyzers/gosec:repo_other_files", - "//tools/nogo/analyzers/misspell:repo_other_files", - "//tools/nogo/analyzers/sloglint:repo_other_files", - "//tools/nogo/analyzers/unconvert:repo_other_files", - "//tools/nogo/analyzers/unparam:repo_other_files", - "//tools/nogo/internal/analyzertest:repo_other_files", - "//tools/nogo/internal/golangci:repo_other_files", - "//tools/nogo/internal/position:repo_other_files", - ], - visibility = ["//:__subpackages__"], -) - -# --- end repo_files --- diff --git a/CHANGELOG.md b/CHANGELOG.md index 884a0d7f0..61c3914f1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,1813 +7,390 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] -### Added +## [1.3.2-rc.2] - 2026-10-10 -- `bd versions ` lists the versions recorded for a bead, and - `versioned-history.enabled` turns recording on. While it is on, every - accepted change to a bead created while it is on, or that enters the issues - plane while it is on (a wisp promoted or made persistent), stores a - snapshot of it as a row of `issue_versions` in the same transaction. It is - off by default and nothing is backfilled: a bead already on the issues - plane when it is turned on is legacy, and editing it records nothing. A - recorded write leaves `updated_at` where an unrecorded one would, so - `bd import`'s stale check sees the same timestamps either way. - `bd versions --json` answers `{"versions": [...], "recording": bool}`; the - command still lists what was recorded after recording is turned off, and - proxied-server and HTTP workspaces refuse it. The setting is a row of each - store's `config` table (`bd config set versioned-history.enabled true`), - and every store reads its own when it is opened: a write routed into - another workspace (a prefix-routed `bd update` or `bd close`, - `bd create --repo`) follows the target store's row, a proxied-server - provider reads it when it is constructed, and a long-lived `bd serve` - keeps the value it read until it restarts. - `BD_VERSIONED_HISTORY_ENABLED=1` (or a `config.yaml` value) turns - recording on for every store the process opens, and never turns it off. - The row replicates on `bd dolt push`/`pull`, and recording is single-writer - only: two clones recording at once can mint the same revision and fail the - pull, and a write from a writer that is not recording is folded into the - next recorded version under that version's actor, so keep recording on for - every writer of a store or off for all of them. Turning it on with - `bd config set` first checks every issue the store would version and - refuses, writing nothing, while any holds a number outside the I-JSON - exact-integer range (magnitude above 2^53-1, however it is spelled) in its - metadata or in a gate's timeout (held as nanoseconds, so about 104.25 - days), and prints the count, the ids, the field and the fix. There is no - override, and the check runs once; after that, a recorded write that would - hold such a number fails atomically. Migration 0071 adds a nullable - `removed_restriction` column to `issue_versions`. `bd version` now refuses - an argument and suggests `bd versions `, instead of printing bd's - build for a mistyped `bd version bd-123`. -- `bd blocked --flags` prints the blocked flag for each id read from stdin - (one per line; blank lines and repeats ignored), with `--json` as - `[{"id","is_blocked"}]` in the order the ids were given, omitting an id that - names no row on either plane. A closed id is answered too, with its stored - flag (a `status <> 'closed'` SQL read of the column leaves it out). It is the flag `bd ready` filters on, not the - listing: a row whose stored flag is set with no nameable blocker reads true - here and is absent from `bd blocked`. It runs `issueops.BlockedFlags` on - every route; over HTTP that is `listBlockingAnnotations` with `is_blocked`, - behind `dependencies.blocking.isBlocked`, refused before the dial against a - server that does not advertise it. The listing's filter flags are refused - with `--flags`. -- `bd blocked --flags --raw` answers the stored `is_blocked` column itself, - without the external-dependency policy: a row blocked only by an - unsatisfied `external:` ref reads `false` (the policy-aware `--flags` - reads `true`); a stale flag reads `true` either way. `--raw` is for the - local and proxied routes, where it reads the column directly and the two - answer the same. Over HTTP it is answered only by a server that does not - advertise `policy.external_dependencies` (whose flag is the column); a - server advertising it answers only the policy-aware flag, so `--raw` - against it is refused before the dial rather than answered with the - decorated flag. `--raw` without `--flags` is refused. -- `bd blocked` works over a connected HTTP workspace. The blocked listing is - a role, `issueops.BlockedLister` (`ListBlocked(BlockedRequest{ParentID, - Labels, LabelsAny, ExcludeLabels}) -> BlockedResult{Items}`), on every - backend leg over the same body `GetBlockedIssues` runs, and `bd serve` - publishes it as `GET /v0/beads/blocked` (`parent`, and the repeatable - `label`, `label_any` and `exclude_label`), answering `{items, has_more}` - with `BlockedIssue` rows (the element `bd blocked --json` prints), behind - the new `blocked.list` capability token (additive; no wire_revision bump). - The listing names unsatisfied `external:` blockers wherever the - external-dependency policy applies: on the server when it advertises - `policy.external_dependencies`, else on the client, as every other policy - read does. The HTTP backend's `BlockedLister()` and legacy - `GetBlockedIssues` dial it; against a server that does not advertise the - token they refuse before dialing with a typed capability error naming - `blocked.list`, and never compose the listing from other reads. The - listing is unpaged: a server answer that reports `has_more` is refused - with the new `httpclient.TruncatedListingError` rather than returned - short, and an answer over the client's 32 MiB response limit is refused - as any oversized response is. The external-dependency decorator's legacy - `GetBlockedIssues` over an HTTP store now reads the extra sources through - the served roles (it failed on a raw multi-id read before), so callers of - that door no longer fail on it; commands that also make other raw reads - the HTTP backend does not serve (`bd ready --explain`) still refuse on - those. `bd - blocked` runs the role on every route (the proxied-server route through - the provider's accessor) with unchanged output: `--json` prints the same - rows in the same order (priority, then newest first, then id, which now - also breaks a tie the order used to leave to chance), and `[]`, never - `null`, when nothing is blocked. -- `bd list`'s remaining filters work over a connected HTTP workspace. - `GET /v0/beads/issues` accepts `updated_after`/`updated_before`, - `closed_after`/`closed_before`, `defer_after`/`defer_before`, - `due_after`/`due_before`, `priority`, `priority_min`, `priority_max`, - `title_contains`, `desc_contains`, `notes_contains`, `external_contains`, - `external_ref`, `spec`, `label_pattern`, `label_regex`, - `empty_description`, `no_labels`, `no_parent`, a repeatable - `exclude_type`, `pinned`, `no_pinned`, `deferred`, `overdue`, `mol_type` - and `wisp_type`, behind one new capability token, `issues.list.filters` - (additive; no wire_revision bump; the names are `issues:count`'s where it - already spells the filter), and applies them through the same Reader role a - local `bd list` reads through. Bounds are strict and compared at - whole-second precision on both routes; relative and date-only values are - resolved on the client's clock and timezone as locally, while `--overdue` - compares against the server's clock. Against a server that does not - advertise the token the client refuses before dialing, naming - `issues.list.filters`, rather than sending a filter an older server rejects - or dropping one and answering a wider listing. The 29 corresponding - divergence-ledger refusal rows are removed. The default `bd list --parent` - tree view does not carry these filters yet: its descendant walk still - refuses one over HTTP rather than drop it, and `--flat` or `--json` skips - the walk and applies them to the parent's direct children (not its whole - subtree). -- `issueops.ListRequest.ExcludeStatus`: statuses to leave out of a listing, - appended to the default exclusions (never re-admitting what they hide), - comma-separated or repeated, each validated against the workspace's - statuses like `CountRequest.ExcludeStatus` (an unknown name is - `ErrValidation`). `GET /v0/beads/issues` accepts it as a repeatable - `exclude_status` under the same `issues.list.filters` token (an unknown - name is a `400` on `exclude_status`), so an HTTP-backed caller can leave - closed history out of an `all` listing (`all` plus - `exclude_status=closed` keeps every status not named, the `pinned` and - `hooked` statuses and custom done/frozen statuses included) instead of - reading the whole history. It is refused together with `ReadyFlag`, like the other - members the ready query cannot carry. There is no `bd list` flag for it - yet. - -- `bd serve` publishes the gate members: `await_type`, - `await_id`, `timeout` (nanoseconds) and `waiters` on `POST - /v0/beads/issues` and on `issues:batchApply` create items, behind the new - `issues.create.gates` capability token, and `await_id` and `waiters` (a - whole-value replacement; `[]` clears) on the `patch` of `PATCH - /v0/beads/issues/{id}` and of `issues:batchApply` update items, behind - `issues.update.gates`. Additive; no wire_revision bump. The bounds are the - library role's, the same ones a local create or update now enforces (see - Changed), answered as `400 invalid_argument` with `param` naming the - member. -- `issueops.IssuePatch.Waiters`: a whole-value replacement of a gate's waiter - list (nil or empty clears it, stored as `[]`), on `Lifecycle.Update` and on - `BatchApplier` update items, on every backend. `issueops.GateFieldError` - (`Field`, `Detail`; wraps `ErrValidation`) names the gate member a create or - update refused, as one of the new `issueops.GateFieldAwaitType`, - `GateFieldAwaitID`, `GateFieldTimeout` and `GateFieldWaiters` constants. -- The HTTP client sends the gate members on create, update and - `issues:batchApply` (create and update items) to a server that advertises - `issues.create.gates` / `issues.update.gates`. Against an older server a - request naming any gate member is refused before the dial with a typed - capability error naming the missing token (never sent without the - member); a batchApply plan is checked whole, so no partial plan is sent. - A request naming no gate member is unaffected by either token. -- `bd heartbeat` works over a connected HTTP workspace. - The lease refresh is a role, `issueops.Heartbeater` - (`Heartbeat(HeartbeatRequest{Actor, IssueID}) -> HeartbeatResult{IssueID, - Holder, HeartbeatAt, LeaseExpiresAt, Rearmed}`), on every backend leg, and - `bd serve` publishes it as `POST /v0/beads/issues/{id}:heartbeat` behind the - new `issues.heartbeat` capability token (additive; no wire_revision bump), - with the new `lease_not_held` (409) problem code (`reason: ephemeral` for a - wisp). A refresh writes ONLY the lease row: the issue's `revision` is not - reminted (a guarded write against the pre-heartbeat token still lands), no - version, event, journal entry or Dolt commit is recorded, and no hook - fires. `Rearmed` is true only when the heartbeat created the caller's lease - row (a hand-doled claim's first heartbeat). A re-arm is recorded as a - mutation; a refresh is not: the re-arm remints `revision`, stamps - `updated_at`, records a `lease_rearmed` event, a journal entry and a - version, makes a Dolt commit and fires `on_update` (see Fixed); the - reported times are whole seconds, exactly what the lease row stores. The - HTTP backend's `Heartbeater()` and legacy `HeartbeatIssue` dial it; - against a server that does not advertise the token they refuse before - dialing with a typed capability error naming `issues.heartbeat`, and never - fall back to a re-claim or a metadata stamp. A - busy (503) answer is replayed, because a repeat only moves the expiry later. - `bd heartbeat` now runs the role on every route (the proxied-server route - through the provider's accessor), with the same - `{"id","status":"heartbeat","owner"}` success output and the same exit - codes. Refusal TEXT on stderr changes per route: the direct routes (Dolt - server, embedded) gain a `lease not held: ` prefix - (`heartbeat X: lease not held: issue not claimable: X status closed`); the - proxied-server route loses its - `Heartbeat: db: IssueSQLRepository.HeartbeatIssue: ` prefix and reads as the - direct routes do; over HTTP the refusal is the problem's detail, which names - the state found (`it is closed`, `an ephemeral wisp`, `open and not - claimed`) or, for a foreign holder, the native `issue already claimed by - `. An id that names no row answers not-found from the role rather - than "not claimable" (the CLI resolves the id first, so its output is - unchanged), and every refusal for a caller that holds no live claim - (closed, released, reclaimed, moved off in_progress, or a wisp) is - `issueops.ErrLeaseNotHeld`, which still matches `ErrNotClaimable`. - -- `GET /v0/beads/dependencies/blocking` accepts `is_blocked`, behind the new - `dependencies.blocking.isBlocked` capability token (additive; no - wire_revision bump), and answers each item's stored, transitive - `is_blocked` flag, the column `bd ready` filters on; an item whose id names - no row carries none. `issueops.BlockingRequest` gains `IncludeIsBlocked` on - every BlockingAnnotator implementation, and `issueops.BlockedFlags` is the - batched read over it. The HTTP backend's `IsBlockedBatch` now serves - through it; against a server that does not advertise the token it still - refuses before dialing, now with a typed capability error naming the - token. An unsatisfied `external:` blocker sets the flag on every route, - just as it keeps the issue out of `bd ready`. -- `bd promote` works over a connected HTTP workspace (S21b). Its one library - entry, `issueops.Promote(ctx, PromoteRoles{Reader, Lifecycle, Commenter}, - PromoteRequest)`, reads the row, refuses one that is not a wisp - (`*issueops.NotAWispError`, the sentence `bd promote` always printed), moves - it with ONE `Lifecycle.Update` whose patch is `Persistence = persistent` - (labels, dependencies, events, comments and the child counter move with it - in the role's transaction), then records the promotion comment. The direct - and proxied-server routes run it over their own roles, so a local promote - now makes the same plane move `bd update --persistent` makes, honoring - `dolt.auto-commit`. That changes five things a script can see on those - routes: the workspace's `on_update` hook fires twice, once for the move and - once for the comment (the direct route never fired it, the proxied-server - route fired it once); the promotion comment is a row `bd comments` lists - (the direct route recorded only a `commented` event); a promoted parent - keeps its child counter, so its next `bd create --parent` mints the number - after its wisp children's instead of failing with - `issue already exists: .1`; a promotion records two Dolt - commits, `bd: promote ` for the move and `bd: comment ` for the - comment (an embedded workspace recorded one, `bd: promote (auto-commit)`, - and the proxied-server route one, `bd: promote `); and a promote that - loses a race to a concurrent one fails with the not-a-wisp sentence instead - of `promoting : wisp not found`. A comment that cannot - be written leaves the promotion standing behind a warning on every route; - the proxied-server route used to roll the promotion back. `bd serve` - publishes the move as `patch.persistence` on `PATCH /v0/beads/issues/{id}` - behind the new `issues.update.persistence` capability (additive; - `wire_revision` is unchanged), which also carries `bd update --persistent`, - `--ephemeral` and `--no-history` over HTTP. Against a server without the - token the client refuses before the dial, naming it; nothing is written. A - move the workspace refuses (an `unversioned`-class row asked for - `--ephemeral` or `--no-history`, or a demotion of a row with retained - snapshots) matches `ErrValidation` on every route: `bd serve` answers it - `400 invalid_argument`, and the local refusal gains a `validation failed: ` - prefix. -- `bd serve` advertises the new `issues.update.claim` behavior token for what - `PATCH /v0/beads/issues/{id}` already served untokened: `claim` (upstream - #6890), resolved on both planes, so a wisp is claimable there (additive; no - wire_revision bump). The HTTP client now picks the claim route from the - handshake before dialing: with the token every claim is one updateIssue call; - without it a claim alone goes to `{id}:claim` (no doomed 400 first; a wisp - still refuses by name, W-ClaimRequest.Wisp, now naming the token), and a - claim beside a patch, a guard, a force override or `allow_template` is - refused before the dial with an unsupported error naming - `issues.update.claim`. Skew note: a server that serves `claim` untokened - (wire_revision 2 builds before this release) now gets those two refusals - from a new client where it used to serve them; upgrade bd serve. A template - claimed alone without the token is still refused, on the client's own - pre-read: `{id}:claim` has no template guard (bd-jkp9v3). -- `issueops.LeaseReclaimer`, the stale-lease sweep behind `bd reclaim`, is a - role on every leg (dolt, embedded dolt, the unit-of-work provider and the - HTTP client), and `bd serve` publishes it as `POST /v0/beads/issues:reclaim` - behind the new `issues.reclaim` capability (additive; `wire_revision` is - unchanged). Every reverted row mints a new revision, reported per entry as - `revision`, so a holder that writes with its pre-reclaim token gets a version - mismatch. An id that is not a stale lease is left out of the answer, not - refused. `bd reclaim` now runs the role on both routes, which changes four - things a script can see: its `--json` entries gain `revision`; the - workspace's `on_update` hook fires once per reverted row (the raw path fired - none); `--id` now accepts at most 1000 ids per run (more is refused, never - truncated); and the role records its own version commit, honoring - `dolt.auto-commit` the way `bd prune` does, as - `bd: reclaim N expired lease(s)` on both routes. Over a connected HTTP - workspace `bd reclaim` now works instead of refusing. -- **`bd show --comments-tail N`** renders only the last N comments in text - output (including under `--watch`) - ([#6618](https://github.com/gastownhall/beads/pull/6618)), preceded by one - elision line naming how many older ones were hidden. A render-only cap for - fat, append-only beads whose full comment history is hundreds of KB — - description and metadata are unchanged, and omitting the flag (or passing - `0`) is byte-identical to today's output. JSON output is untouched; - `--include-comments` still streams every comment there. -- `GET /v0/beads/issues` accepts `no_assignee`, `title` and a repeatable `id` - (one id per value, at most 200), each behind its own new capability token - (`issues.list.noAssignee`, `issues.list.title`, `issues.list.ids`; additive, - no wire_revision bump; `id` and its token are bd-enterprise's contract, - adopted unchanged), and applies them through - the same Reader role a local `bd list` reads through. Over a connected HTTP - workspace `bd list --no-assignee`, `--title` and `--id` now work instead of - refusing. Against a server that does not advertise a flag's token the - client refuses before dialing, and the refusal names the token (every - capability refusal over HTTP now names its token). -- `GET /v0/beads/issues` accepts `q`, `bd search`'s free text, behind the - new `issues.list.search` capability token (additive; no wire_revision - bump). `issueops.ListRequest` gains `Query`, with `SearchIssues`' meaning - (title or id contains the text, case-insensitively), on every Reader - implementation, and `issueops.SearchListRequest` is search's scope as a - request. Every `bd search` route (direct, `--proxied-server` and the HTTP - backend) now asks `Reader.List` with it through one flag mapping; against - a server that does not advertise the token the HTTP client refuses before - dialing with a typed capability error. Two things a script can see follow - from that mapping: `bd search --type` expands aliases (`feat`, `mol`, - `dec`, ...) and validates the type as `bd list` does, so an unknown type - now exits 1 where it used to answer an empty result, and `bd search - --json` rows carry `comments_omitted` (additive) as `bd list --json` rows - do. -- `bd create --graph` now plans its batch through `issueops.BatchApplier` - instead of the old `buildDomainGraphPlan` path, so a graph create gets the - same atomic multi-row semantics as `bd batch apply`. A `waits-for` edge's - `metadata.spawner_id` is stamped only when that edge names a spawner - (never defaulted onto an edge that names none), and `thread_id` now survives - translation into the batch request alongside every other - `GraphApplyEdge`/`GraphApplyNodeDep` field — pinned by a reflection-based - field-coverage test (`TestBuildGraphApplyBatchRequestFieldsSurvive`) that - fails closed if a future field is added to either struct without an - explicit decision about where it goes. The hierarchy/cycle rejection - scenarios this path's local gates cover (blocking through existing or - planned hierarchy, transitive external-parent paths, reverse parent-to-child - edges, cycles hidden in an inline dep, combined scheduling cycles, the end - gate alone with the per-edge cycle probe skipped) are exercised against a - real Dolt store, not just skipped placeholders. A graph plan whose translated - BatchApplier items (nodes, edges and deferred assignments combined) exceed - `issueops.MaxApplyBatchItems` (1000) is now refused outright with - `GraphApplyTooLargeError` rather than silently chunked across several - transactions — `bd create --graph` promises one atomic request, so a plan - over the cap must be split by the caller into multiple `bd create --graph` - calls instead. -- `--if-revision` (gastownhall/beads#4682) is extended to `bd reopen`, the - one lifecycle verb #7203 did not add it to, reusing the same - `issueops.ReopenRequest.ExpectedVersion` field the library already - exposed and the same `parseIfRevisionFlag` / `requireSingleIfRevisionID` - / `reportIfRevisionFailure` helpers #7203 introduced for the other four - verbs — no new CLI surface, no new JSON body shape. A guarded reopen of an - issue that is already open is still judged against the guard on both - routes: a stale revision exits 13, and a matching one is the usual - "already open" no-op. A faithful port of - gc's (gascity) `bdstore_conditional.go` decode logic is now run against a - real built `bd` for all five guarded verbs - (`TestEmbeddedGCConditionalMatcherDecode`), confirming gc's matcher decodes - #7203's numeric-only `expected_revision`/`current_revision` body exactly - — no decimal-string twin fields are needed, since `encoding/json` decodes - a JSON number straight into an `int64` struct field with no lossy - `float64` intermediate. The proxied-server route (`*_proxied_server.go`'s - uow-backed preflight/apply path), previously untested for `--if-revision` - on any verb, now has coverage for all five. -- `backends.Backend` gains an optional `OpenWith(ctx, beadsDir, OpenOptions)` - and a `Remote bool` field for a registered extension backend (for example - an HTTP client registrant). `OpenOptions{Credential, HTTPClient, - UserAgent}` carries per-open injections — the motivating case is a single - embedder process serving many workspaces with distinct credentials against - a backend whose dialer would otherwise be process-global. `Open` keeps - working unmodified: a backend with no `OpenWith` falls back to it and - ignores `HTTPClient`/`UserAgent`, but refuses rather than silently drops a - non-nil `Credential`. The public SDK gains `beads.OpenBestAvailableWith` - (and the `backend` package's matching aliases); `beads.OpenBestAvailable` - is now that function called with a zero `OpenOptions`, with identical - behavior for every existing backend. `doltserver.ResolvePhysicalRoots` now - recognizes a registered remote backend (`backends.IsRemote`) before any - Dolt-mode check, so a remote-backend workspace is never misclassified as - having a local Dolt root to gate. -- `bd serve`'s `GET /v0/beads/context` now reports `wire_revision` (the - non-additive wire-shape counter for this build) and - `min_client_wire_revision` (the oldest revision this build still answers - correctly). A client may declare the revision it was built for on any - request via the optional `Bd-Wire-Revision` header (a plain non-negative - decimal — no leading `+` or leading zero); a declared revision below - `min_client_wire_revision` is refused with `400 invalid_argument`/`reason: - "wire_revision_unsupported"` and `min_wire_revision`, `wire_revision`, and - `bd_version` fields naming this server's floor, current revision, and - release string. The header is absent by default and an absent header is - served exactly as before — this is additive, not a new precondition on - existing clients. A client with no `wire_revision` signal to read yet may - infer one from `bd_version` only as an ADVISORY hint (string-or-number, - never a refusal on an inferred `0`) — see the `wire_revision` property's - inference notes in `internal/httpapi/spec/openapi.v0.yaml` for the cutoff - and known-exception builds. See `internal/httpapi/wire_revision.go` and - that same property for the full revision history. -- `GET /v0/beads/issues`'s `sort` parameter now advertises the - `issues.list.sort` capability token, the same way an operation itself - does, and a CI rule - (`TestNewParameterOnExistingOperationHasABehaviorToken`) requires every - new parameter on an existing operation, or new request body member on an - existing operation, to carry a token that `Capabilities()` actually - advertises (not just backticked `a.b` text shaped like one). Three frozen - baselines (`internal/httpapi/testdata/pretoken_*.json`) grandfather what - predates the rule and are pinned never to grow - (`TestPretokenBaselinesNeverGrow`); a brand-new operation is permanently - exempt — its own review decides its capability story. -- A CI golden digest (`internal/httpapi/wireshape`) pins the JSON name, - type, format, enum, required-ness, and nullability of every EXISTING - response AND request-body member across the whole HTTP spec — including - array item shape and `additionalProperties` value shape — recursing - through `$ref` and through a schema's own `allOf` or `oneOf`, and fails if - any changes without `wire_revision` bumping to match; - `go run ./internal/httpapi/wireshape/cmd/gendigest` refuses to write a - changed or removed entry unless `wire_revision` has moved past what the - existing golden recorded, and refuses any write at a lower - `wire_revision` (purely additive entries otherwise always write). The - same digest also pins every operation PARAMETER (query, path and header), - keyed by operationId + location + name, and records its type, item shape, - enum, required-ness, effective style and explode (OpenAPI's defaults - filled in where unset), and default — a parameter retyped, re-enumerated, - narrowed, re-serialized, switched required, or removed is non-additive - exactly like a response or request-body member. A new member or - parameter always counts as additive, even a REQUIRED one an old client - will not send, and value constraints (`maxLength`, `pattern` and the - like) are outside the digest: such changes need their own review against - `wire_revision`. -- `cmd/bd` registers the http client backend built on top of the v0 `bd - serve` wire (`internal/httpclient`), so a workspace whose - `.beads/metadata.json` selects `"backend": "http"` opens over HTTP through - the ordinary `OpenBestAvailable` path, exactly like a registered extension - backend does. A new `bd connect ` command performs the handshake - (`api_version`, wire revision, and — with `--expect-project-id` — workspace - identity) and writes nothing until it succeeds; it then records the - per-user activation sidecar (`.beads/http_target.json`, never - git-tracked — `cmd/bd/doctor/gitignore.go` now requires it and its local - metadata sidecar `http_local_metadata.json` be ignored) and sets - `metadata.json`'s backend to `"http"`. `bd connect` refuses a plain `http://` - URL to a non-loopback host (a bearer credential would cross the network in - the clear) unless `--allow-plaintext` is given, and refuses to switch a - workspace that already selects a different backend unless `--force`. A - credential is never accepted on the command line or written to disk; it - comes from the same ladder every http request already uses - (`BEADS_HTTP_TOKEN=host[:port]=`, then - `BEADS_HTTP_TOKEN_COMMAND=host[:port]=`, then the credentials - file, then none); both variables take only that host-scoped form, and a - bare value is refused, since it would be sent to whatever server the - workspace's `http_target.json` names. - `bd create` sends no `created_by` on any backend: the - create role defaults it to the request's actor (see *Changed*). The - external-dependency policy decorator (`internal/storage/externaldeps`) - still wraps an http store, and passes every ready, claim, claim-next, - close, blocking and tree request through untouched when the server's - handshake advertises `policy.external_dependencies`, which every `bd - serve` composed through the policy now does (see below). Only against a - server that does not advertise it (an older `bd serve`, or one handed raw - roles) does the client apply the policy itself, in role terms: ready reads - and the ready claim exclude the workspace-wide externally blocked set over - a widened window, and per-issue checks (claim, close, batch close, list - annotations, tree leaves) read only the named issues' edges. - `bd close --claim-next` refuses over http before anything closes, since - the wire's batch close cannot carry the claim; its error says to close - without it and then run `bd ready --claim`. -- `bd serve` advertises the conditional behavior token - `policy.external_dependencies` exactly when the roles or provider it - serves were composed through the external-dependency policy - (`httpapi.Config.ExternalDependencyPolicy`, which `bd serve` derives with - `externaldeps.Applied` / `externaldeps.AppliedToProvider`); a server handed - raw roles withholds it. A client that sees it hands claim-next to the - server as one atomic call and takes the ready count from the server, - instead of judging external dependencies a second time against the CLIENT - machine's project config. -- `bd ping` works on an http workspace (a new `storage.Pinger`, which the - http store answers with its authenticated context read). -- The public `backend/http` package (`bdhttp`) is the out-of-tree door onto - this backend for an embedder that links beads as a library rather than - running `cmd/bd`: `Register(Options)` adds `"http"` to the registry and - installs its transport, `Open`/`Handshake` dial a server directly with no - workspace on disk, and the existing public `beads.OpenBestAvailableWith` - takes a per-call `OpenOptions.Credential` through it — the case a - multi-tenant embedder serving many workspaces needs, where a - process-global credential cannot stand in for one tenant's own. - EXPERIMENTAL, pin an exact beads version. -- Unlimited ready and query reads over the HTTP backend (`bd ready --limit - 0`, `bd query --limit 0`, and every library read asking for no limit) - return every row against any server, bounded on the client by the new - `BEADS_HTTP_UNLIMITED_READ_CAP` (a positive integer up to 1000000; default - 10000). The client never puts `limit=0` on the wire, which a `bd serve - --allow-non-loopback` refuses and which an older server answered with its - default page, silently capping the result: an unlimited ready or query - read is one request bounded at the cap plus one (one consistent snapshot). - Past the cap, or when a server reports more rows below the requested - bound, the read is refused with a typed error - (`httpclient.ErrUnlimitedReadCap`) naming the cap, never truncated. An - unlimited `bd list` is unchanged: it walks its keyset cursor a page at a - time and is bounded only by `--max-rows` / `BEADS_MAX_ROWS`. Unlike a - malformed `BEADS_MAX_ROWS`, which is ignored with a warning, a malformed - cap value fails every unlimited ready or query read rather than falling - back to the default. -- Two molecule capability tokens on `bd serve`. `issues.close.autoCloseMolecule` - announces that `POST /v0/beads/issues/{id}:close` and `POST - /v0/beads/issues:batchClose` accept `auto_close_molecule` and answer - `auto_closed_molecule` and `molecule_auto_close_refusal`; - `issues.advanceMolecule` is the new `POST - /v0/beads/issues/{id}:advanceMolecule`, `bd close --continue`'s advance. - Both run the one library entry (`issueops.CloseRequest.AutoCloseMolecule`, - `issueops.MoleculeStepper`) on the server. The public `issueops` package - gains the molecule rules as one implementation over the BatchGetter, - Relations and EdgeReader roles (`MoleculeRoots`, `LoadMolecule`, - `AnalyzeMolecule`, `ViewMolecule`, `ReadMoleculeProgress`, - `ActiveMoleculeIDs`, `CompletedMolecule`, `MoleculeRootClose`); a - molecule's membership is the parent-child edge. Against a server that - does not advertise a token the HTTP client refuses a library close that - sets `AutoCloseMolecule`, or an advance, before dialing, with a typed - capability error; it never runs a client-side copy of either rule. -- `bd close --json` reports the molecule auto-close. The entry of a close - that completed a molecule carries the root it closed as - `auto_closed_molecule`, or why the root stayed open as - `molecule_auto_close_refusal`, on every route; before, that refusal - reached only stderr. Both members are absent from every other entry, so - each entry still decodes as the closed issue, and the output stays an - array (under `closed` with `--suggest-next`, `--continue` or - `--claim-next`). - -### Changed - -- Over an HTTP workspace, `bd ready --json` without `BD_JSON_ENVELOPE` no - longer sends `GET /v0/beads/ready:count` after a page the limit cut short: - the total only went into the stderr hint. It lists one row past the limit - instead, and the hint says more issues matched without saying how many - (the wording the proxied-server route already uses). A local store, and the - envelope's `pagination.total`, keep the total. -- `bd` initializes its packages with `GOMAXPROCS` capped at 4 and hands it - back before `main` runs anything, so the garbage collections package - initialization triggers do not stop the world across every CPU of a large - host. On a 192-CPU machine this takes about 17 ms off every bd process. An - explicit `GOMAXPROCS` is restored as set. -- Over an HTTP workspace, `bd ready` no longer fetches `GET /v0/beads/context` - before its listing in every process only to learn whether the server - enforces the external-dependency policy. The answer is kept for 5 minutes - per server URL and pinned project under the user cache directory - (`BEADS_HTTP_POLICY_CACHE_TTL`, a duration, a number of seconds, or `off`), - and a handshake the process already holds still wins. Claims and closes - always ask the server. -- `bd serve` normalizes every RFC 3339 time bound on the issue listing and - count (`created_*`, `updated_*`, `closed_*`, and the new `defer_*` and - `due_*`) to UTC before it reaches storage. A bound carrying an offset - (`...+02:00`) now selects exactly what its `Z` twin does; previously a - backend whose timestamp column has no zone could read the offset bound's - wall clock and compare it hours off. `bd`'s own client always sent `Z`, so - its answers are unchanged. -- `bd serve` answers a `label_regex` the database cannot compile with - `400 invalid_argument` (`invalid_value` on `label_regex`) on - `GET /v0/beads/issues`, `GET /v0/beads/ready`, `GET /v0/beads/ready:count` - and `POST /v0/beads/issues:claimNext`, instead of `500 internal`. The pattern is - still compiled by the database in its own dialect; only the attribution - of its refusal changed. -- `issueops.Reader.List` refuses `PinnedFlag` together with `NoPinnedFlag` - with `ErrValidation` (`--pinned and --no-pinned are mutually exclusive`) on - every backend; it used to let `PinnedFlag` win silently. `bd list` already - refused the pair at flag parse, so its behavior is unchanged; over HTTP the - pair is a `400 invalid_argument` on `no_pinned`. -- `issueops.Reader.List` refuses `ParentID` together with `NoParent` with - `ErrValidation` (`--parent and --no-parent are mutually exclusive`) on - every backend, as `CountRequest` already did; it used to answer with only - the edge-less rows under the parent's dotted id, which both clauses match. - `bd list` already refused the pair at flag parse, so its behavior is - unchanged; over HTTP the pair is a `400 invalid_argument` on `no_parent`. - -- **`bd gate discover` skips template gates, as `bd gate check` does.** A - template gh:run gate is a proto that work is poured from; on the direct, - proxied and embedded routes `bd gate discover` used to record a discovered - run ID on it, which every later pour copied. It is now left alone on every - route. `bd gate add-waiter` on a template gate is refused by the update - role's template guard, where the direct, proxied and embedded routes all - used to write the waiter. -- **`bd gate create` refuses a negative `--timeout` on every route** - (`timeout cannot be negative`); the direct, proxied and embedded routes - used to store it. A `--type` longer than 32 characters fails with the same - typed validation error instead of a raw Dolt column error. -- **`bd gate create` and `bd gate add-waiter` follow `dolt.auto-commit` - like every other role write.** Under `dolt.auto-commit=off` `bd gate - create` no longer makes its own Dolt commit on the direct and embedded - routes (the proxied route already did not). The Dolt commit messages are - the role's: `bd: create gate X blocking Y` becomes the create's default - message, and `bd gate add-waiter`'s becomes the update's (`bd: update X` - on the direct and embedded routes, `update issue` on the proxied route). - On the proxied route `bd gate create` no longer journals a - `dependency_added` event on the blocked issue, matching the direct route - and `bd create --deps`. -- **Gate members are bounds-checked in the library, on every local backend.** - `issueops.Lifecycle.Create`, `Lifecycle.Update` and `BatchApplier` (create - and update items) now refuse, with a typed `*issueops.GateFieldError` - wrapping `ErrValidation`: a negative `Timeout` (previously accepted and - stored), an `await_type` over 32 characters, an `await_id` over 255 - characters, and a waiter list whose JSON encoding exceeds its TEXT column - (65535 bytes). Creates check all four; an update can change only - `await_id` and `waiters`, so it checks those two. The over-width values - previously failed with a raw Dolt driver error. There is no `await_type` vocabulary, no gate-type-only rule - and no waiter deduplication. -- **Over an http workspace, a bd command answers its repeated reads of a row - or setting from one response.** `bd show --json` makes one request - instead of three, text `bd show` three instead of six, and `bd update`, - `bd close`, `bd label add`, `bd comments` and `bd dep add` one to seven - fewer (port of bd-enterprise e626072b8). A one-shot command's transport - answers an identical `getIssue`, `getSetting` or `listSettings` GET from the - answer it already received, for at most 5 s from that answer. Any write the - command makes clears it, and only a 2xx that decoded is kept. `bd serve`, - `--watch` and `--follow` never memoize, and neither do library embedders. - Within one command a re-read no longer sees another writer's change made in - the meantime (ledger row `L-read-memo`); `BD_HTTP_READ_MEMO=0` turns the - memo off. The identity-gate preview and the real open now share one - transport, so a write makes one handshake. `bd list` reads its vocabulary - with one `listSettings`, and the hook decorator skips its post-write re-reads - when no hook for the event is installed. -- **`created_by` defaults to the actor in the library, for every create - shape and every backend.** `PreparePublicCreateRequest`, which - `Lifecycle.Create`, `BatchCreator` items and batch-apply create items all - prepare through, now sets an empty `Issue.CreatedBy` to the request's - `Actor`; an explicit value is kept. The CLI (`bd create`, the state event - create) and the three `bd serve` create handlers no longer stamp it - themselves. A library caller that left `CreatedBy` empty used to store - `""` locally and the actor over http; both now store the actor. The raw - `CreateIssue` seam is unchanged: import keeps the creator its rows carry, - and `bd swarm`, `bd todo`, `bd gate` and the create form still stamp - `currentActor()` there. -- **On an http workspace, `bd update -s closed` does not record - `closed_by_session`.** The update wire cannot carry it, so the CLI drops - it rather than refusing the close; the status change, its close-policy - guards and every other field in the same update still apply. A one-line - notice on stderr names the drop, both for an explicit `--session` and for - the ambient `CLAUDE_SESSION_ID` every agent under Claude Code exports, so - it is never silent. `bd close` (and `bd close --session`) still records - the session over http: its wire operation carries it. Local backends are - unchanged. -- **On an http workspace, `bd reopen` sends no Provenance label.** The - CLI's label is the fixed `bd: reopen `, which the wire cannot carry; - the server records its own reopen entry for the same issue and actor, so - the reopen no longer refuses. Local backends still record the label. -- **Routing settings are local-only on a remote backend.** For an http - workspace, `routing.mode`, `routing.contributor`, `routing.default`, - `routing.maintainer`, `contributor.auto_route` and - `contributor.planning_repo` come from local config (`config.yaml` or the - environment) only; the server's shared settings document is not read for - them, because routing targets are local filesystem paths that one shared - document cannot name for every client. `bd create`'s routing decision, `bd - migrate-personal` and the missing-id lookup fallback all read the same - local-only answer, and none pays a settings round trip for it. Local - backends still fall back to the database values. -- **The molecule auto-close runs inside the step's close, and an error in - it fails the close.** `bd close` of a molecule's last step closes the - root in the same transaction (opt-in `AutoCloseMolecule`, so an - orchestrator that runs its own molecule lifecycle is not doubled), and it - replays on an idempotent re-close. The root close is unforced, so a root - a direct close would refuse stays open and is reported while the step's - close stands: one its close policy refuses (such as a live blocker), and - now also one a close guard refuses (a template, a pinned root, a root - another actor holds). Any other failure of the root close (such as a - storage error) now fails the step's close with it, where the CLI used to - close the root afterwards and only warn. - Two closes that finish a molecule's last steps at the same time no - longer leave its root open: one of them retries and closes it. The cost - is that concurrent closes of steps under one open auto-closing root now - serialize, whether or not either finishes the molecule: each conflict is - retried, and a close still conflicting when its retry budget runs out - fails with the serialization error. A batch close reports each root once, - on the first item under it that closed. -- **`bd close --continue` claims the next step for real.** The advance is - the `MoleculeStepper` role on every route: the assignee is set and a step - another agent holds is skipped, never taken (the direct and http routes - used to flip the status only). -- **`bd mol current`, `bd mol progress`, `bd ready --mol` and `bd mol show` - read through the library's molecule entries on every route**, so - membership is the parent-child edge everywhere (`bd mol show` no longer - adds children found only by an id pattern). -- **Against a server without the molecule tokens, `bd close` says what it - did not do.** If the server does not advertise - `issues.close.autoCloseMolecule` (an older `bd serve`, or one whose - orchestrator owns the molecule lifecycle), the close goes out without the - auto-close request. When that leaves a molecule with every step closed and - its root open, a notice on stderr names the root, once per root in a batch - and again on a re-close; a step under an ordinary epic or an unfinished - molecule prints nothing. If it does not advertise - `issues.advanceMolecule`, or the handshake that would say so fails, `bd - close --continue` refuses before closing anything, so the step is never - left closed with the next step unclaimed. -- Every `bd` invocation used to start two git subprocesses before doing any - work: `git rev-parse` to locate the repository and `git config user.name` - to resolve the actor. On Linux the repository is now located in-process - (ordinary repositories, linked worktrees, submodules, no repository), - producing exactly what `git rev-parse` printed and still running git for - anything it does not model (`GIT_DIR` and related overrides, - `core.worktree`/`core.bare`, repository extensions, includes, foreign - ownership subject to `safe.directory`). The `git config user.name` actor - fallback is resolved only when a command actually needs an actor, so - read-only commands such as `bd list` and `bd show` start no git process - for it. The actor's value and priority order are unchanged. In an embedded - workspace with `backup.enabled` unset, the per-command auto-backup check - ("is there a git remote?") is likewise answered from the git config files - when they are plain (no includes, no command-line config), instead of - running `git -C .beads rev-parse --git-common-dir` and `git remote`. - -- `bd create --graph` now stores a plan whose path from a node's parent to - the node runs through a `waits-for` edge; the graph-only preflight that - walked every ready-work edge used to refuse it. The plan now goes through - the same end gate as `bd dep add` and `bd batch apply`, which walks only - the scheduling edges (`blocks`, `conditional-blocks`, `parent-child`) and - stores this shape too. Avoid it in plans: once the `waits-for` spawner has - an open child, every issue in the shape, that child included, stays - blocked and none is ever ready. The same path through scheduling edges - alone is still refused. -- `bd preflight --fix --json` no longer returns a `Version sync` fix result: - version updates must keep all release surfaces aligned via `scripts/update-versions.sh`. -- Release-tag pushes require Go and reject batches containing different release versions. -- On version drift the release-tag pre-push hook points at the checker's - remedies instead of prescribing `scripts/update-versions.sh `, and - `scripts/check-versions.sh` and `bd preflight` list the three that work: with - `cmd/bd/version.go` already at the release version, that re-run rewrites only - the `.githooks` markers and `uv.lock` and leaves any other drifted file as it was. -- **BREAKING (fail-closed): bd no longer migrates an existing database without - explicit consent** (Beads 1.2 release remediation). The 1.2.0/1.2.1 releases - applied migrations 54–65 at store open on ANY command — including pure reads - like `bd list` — with no prompt, and older binaries then refuse the migrated - database (schema-skew guard). A command that would apply pending - main-sequence migrations to an existing database at store open (in embedded - mode that includes reads) now refuses with guidance instead, and the refused - open performs ZERO writes (dolt_ignore seeding and clone-local migrations - included). Server-mode read commands open without running migrations, so - they keep reading at the current schema. `bd dolt commit` and `bd vc commit` - warn and commit at the current schema without migrating, so the dirty-table - recovery (commit, then `bd migrate schema`) still works on a database that - is both behind and dirty. Consent is any of: `bd migrate schema`, the verb - that names the migration (a preview flag withholds it); `--force` on - `bd migrate` or `bd migrate schema`; or `BD_ALLOW_MIGRATE=1` for scripts/CI - (`BD_ALLOW_REMOTE_MIGRATE=1` is honored as an alias). Bare `bd migrate`, - including `--update-repo-id`, `--inspect` and `--dry-run`, does not consent. - Fresh databases (`bd init`) still migrate to latest — creating a database is - consent for its schema, kept across the retry of an interrupted server - bootstrap — and the same creation principle covers a workspace the - invocation just cloned into existence (`bd init --remote`, `bd bootstrap` - sync), so the #4259 behind-remote flows keep their exact behavior. A - database already at this binary's latest version never consults the gate; - one left at v65 by 1.2.x or at v66 by 1.3.x still has migrations pending - under this release, so it asks once. The remote-backed case keeps all of the - #4259 remote-migrate gate's additional protections (smart gate, adopt - fast-forward, fork-skew analysis) unchanged. JSON consumers: the refusal - renders as a structured error whose `error` states the refusal and whose - `hint` is a non-runnable operator directive (neither names a migration - command or a consent variable), plus a `migrate_consent` object - (`current_version`, `required_version`, `pending`, `severity: "blocking"`, - `human_decision_required: true`, and `options`: `migrate` runs - `bd migrate schema`, or `bd migrate schema --global` under `--global`; - `keep` has no command). -- **Shipped migrations are frozen by CI** (same remediation): the content hash - of every released up-migration (main 0001–0066, ignored 0001–0026, through - the 1.3.x releases) is pinned by `TestShippedMigrationsAreFrozen`. Any - amendment to a released migration must land as a new migration (0070+, or - ignored 0028+, as of this change) so installed schemas and freshly-migrated - ones cannot fork. -- **BREAKING: the close guards now hold for every close operation, not only - `bd close`.** The template read-only guard, the pin guard and the assignee - authority fence (be-035) used to be a pre-read in `cmd/bd`, so a close - through `bd serve` (`POST /v0/beads/issues/{id}:close`), `issues:batchClose`, - a `close` item of `issues:batchApply`, or a library caller of - `issueops.Lifecycle.Close`, `BatchCloser` or `BatchApplier` closed a - template, a pinned issue, or a bead another actor holds that `bd close` - refused. They now live in the role, inside the close's own transaction, on - every backend: a template refuses with `*issueops.TemplateReadOnlyError` (no - bypass), and unless `Force` is set a pinned issue refuses with - `*issueops.PinnedError` and another actor's bead with - `*issueops.CloseNotAssigneeError` (matching `ErrNotOwner`). Over HTTP they - are `409` `template_read_only`, `issue_pinned` and `not_assignee` (the - holder in `assignee`, also on a batch-close outcome), and the client - rebuilds the same typed errors. `bd close` prints the same lines and exits - as before, and `bd close --if-revision`, which skipped the guards, now - applies them too. Two paths stay outside the guards. The raw storage - `CloseIssue`, which a `close` line of `bd batch` and the other closes - `cmd/bd` makes outside `bd close` use, runs neither the guards nor close - policy on dolt, embedded or proxied; over HTTP it now sends - `force`, so there it also stops applying close policy, and only a template - refuses. A status update into the done category (`bd update -s closed`, a - `PATCH` whose `status` is done, an `update` item of `issues:batchApply`) - answers to close policy only, as before, so it still closes a pinned issue - or another actor's bead. Migration: HTTP and library callers that close - pinned issues or beads another actor holds must send `force` (`Force` on the - request), or reclaim the bead first; the close operations never close a - template, forced or not (pour it instead). -- **BREAKING (out-of-tree storage backends): a create without a priority now - arrives as priority 0 with `DefaultPriority` set.** `bd create` without - `--priority`, a `bd create --graph` node without `priority` and a - `bd create --file` template without a valid `### Priority` used to reach a - backend's `IssueLifecycle().Create`, `BatchCreator().CreateBatch` and - `BatchApplier().ApplyBatch` as priority 2. They now arrive as priority 0 - with `issueops.CreateRequest.DefaultPriority` (or `BatchCreateItem` / - `CreateItem.DefaultPriority`) set, as does an HTTP create without - `priority` when `bd serve` fronts the backend. The fields are additive, so - such a backend compiles unchanged, but the shared preparation that applies - the default lives under `internal/`, so a backend that ignores the flag now - stores P0 (critical) for those `bd create` paths, and still stores P0 for - that HTTP create. Migration: store `issueops.DefaultCreatePriority` when - `DefaultPriority` is set, and refuse `DefaultPriority` with a non-zero - priority as `ErrValidation`; `conformance.RunRoleContracts` checks both - through `RunLifecycleCreateAppliesTheDefaultPriority`, - `RunBatchCreatorAppliesTheDefaultPriority` and - `RunBatchApplyAppliesTheDefaultPriority`. +Second release candidate for 1.3.2. Like rc.1 it has **no schema migration**: +upgrading from 1.3.1 or 1.3.2-rc.1 is a binary swap. It adds one fix on top of +rc.1, the backport of [#6876](https://github.com/gastownhall/beads/pull/6876), +which keeps the post-commit blocked-state recheck from reverting concurrent +writes. The rc.1 upgrade note (run `bd recompute-blocked` once per workspace +after upgrading from bd ≤1.3.0, [#7037](https://github.com/gastownhall/beads/issues/7037)) +still applies. ### Fixed -- An HTTP workspace whose server cannot be reached now says which step - failed. A connect that is refused names the address and says nothing is - listening; one that times out (a filtered port, a down host) names the - address that never answered and how long it waited; a host name that does - not resolve is named as such. The error was the transport's - `Get "https://…/v0/beads/context": dial tcp …: i/o timeout`, which named the - request rather than the address. -- **`bd link` works over a connected HTTP workspace.** It wrote through the - store's raw `AddDependencyWithOptions`, which the HTTP client refuses, so - every `bd link` failed there with `operation "AddDependencyWithOptions" not - supported by the http backend`, whatever the type. It now writes through - the `DependencyEditor` role, the route `bd dep add` takes, on the direct - and HTTP routes alike (the proxied-server route is unchanged). Over HTTP - that is one `dependencies:add` request, so `bd link` keeps accepting any - non-empty type of at most 32 characters (`owned-by`, `ships-in`, ...). - An empty or over-long type is still refused before anything is written, - and an edge that already exists with another type still reports the typed - conflict. On a Dolt store the history commit is now the role's, like - `bd dep add`'s: `bd: dep add ` in place of - `dependency: add -> `. +- **The post-commit blocked-state recheck no longer reverts concurrent + writes** ([#7146](https://github.com/gastownhall/beads/pull/7146), backport + of [#6876](https://github.com/gastownhall/beads/pull/6876), fixes + [#7030](https://github.com/gastownhall/beads/issues/7030)). The 1.3.1 line + carried the #6716 fan-in fix + ([#6719](https://github.com/gastownhall/beads/pull/6719)/[#6936](https://github.com/gastownhall/beads/pull/6936), + [#6942](https://github.com/gastownhall/beads/pull/6942)/[#6947](https://github.com/gastownhall/beads/pull/6947)) + in the form #6876 later corrected on `main`: on the Dolt server backend the + recheck minted its Dolt commit while its own SQL transaction was still open. + `DOLT_ADD` stages the table from the session's BEGIN-time root, so that + commit could write `issues` rows that other sessions had just committed back + to their BEGIN-time values. The recheck runs only when unblocking writes + (closes, dependency removals, deletes) race on `issues`, which is exactly + when this matters. The recompute now runs alone in its own retried + transaction and the Dolt commit is published after it commits. A failed + trailing commit is counted as `post_tx_commit_dropped` and logged, instead + of being reported as a recheck failure, because the flag repair itself is + already committed. -- **`bd query` reports an uppercase duration unit as an unparseable time, not - a negated internal error.** `created>30M` failed with - `not a compact duration: "-30M"`, naming a value nobody typed; it now fails - with `cannot parse time expression: "30M"`. A signed spelling - (`created>+30M`) stops at the parser, as `created>+30x` already did, so - quote one to match it as a literal (`label="+30M"`). Units remain lowercase - (`30min`, `6h`, `7d`, `2w`, `3m`, `1y`); help and docs already say so. -- **Strict `--readonly` no longer creates workspace gate files.** A - `bd --readonly` command took its shared workspace gates with - create-if-missing, so on a workspace no bd command had gated yet it left - `/.beads.gate.lock` and the physical-root gate file - (`.beads/embeddeddolt.gate.lock` or `.beads/dolt.gate.lock`) behind, although - strict readonly promises not to mutate the workspace. It now opens an - existing gate file read-only and holds it shared as before, so a maintenance - operation still sees it and a running one still makes it wait; when no gate - file exists it holds nothing. Every other command still creates the files. -- **Strict `--readonly` refuses `bd backup init`, `backup sync`, - `backup remove`, `backup restore` and `bd import`.** None of them checked - readonly mode. On a server-mode store they ran to completion under - `--readonly`: `backup init` and `backup remove` rewrote or emptied the - server's backup registry, `backup sync` wrote the backup destination, - `backup restore --force` replaced the store, and `import` wrote issues. On - an embedded store they failed with the storage layer's `store is read-only` - error instead, after `backup sync` had left `.beads/backup.lock` behind and - `backup restore` had left workspace-gate holder files. They now exit 1 with - the standard `Error: operation '' is not allowed in read-only mode`, - and a restore is refused before it takes the exclusive workspace gate. - `bd import --dry-run` is refused too, as `bd create --dry-run` already is - and as a migration freeze already refuses it. `bd backup status` still runs - under `--readonly`. -- **`bd init --server --external` no longer starts a Dolt server of its own.** - Init builds its store config by hand and turned auto-start on for every - server init, so an `--external` init that named no port launched a - repo-local `dolt sql-server`, although `--external` says the server is - managed outside bd. `--external` now turns auto-start off. When the - external server cannot be reached, init gives one message on every route - (host and port, socket, shared server, auto-start pinned off): it names the - endpoint it dialed, says that bd did not start a server, and no longer - advises `bd dolt start`. With no port named at all, it asks for one - (`--server-port` or `BEADS_DOLT_SERVER_PORT`) instead of offering a default. -- **A failed `bd init --server` can be retried.** A first attempt that could - not reach its server left an empty `.beads/dolt` and nothing on disk that - records the server choice, so the legacy-upgrade guard refused every retry - as a "legacy Dolt workspace" and no bd command could repair it. When init - itself selects server mode, the guard now admits exactly that root: empty, - with no `.local_version` witness. A pre-1.0 witness, or a `.beads/dolt` that - holds anything, is still refused, and commands other than `bd init` are - decided as before. -- **A graph-mode workspace is refused instead of opened as an Issue store.** - Graph-preview builds mark their workspaces with `"graph_mode": "link"` and - `graph_*` binding fields in `metadata.json`, beside an ordinary Dolt - backend. bd opened such a workspace as an Issue store and ran Issue schema - initialization against the graph database, and every `metadata.json` rewrite - (including the `config.json` to `metadata.json` migration) dropped the - marker and the binding. Now any `graph_mode` other than `dependency`, - including a value bd does not recognize, makes every command that runs in - the workspace or names it with `--db` or `BEADS_DB` (`bd init`, `bd doctor` - and `bd bootstrap` included), and `bd create --repo` aimed at it, refuse it - before touching anything. The refusal names the format and exits with code - 16, so installed beads git hooks refuse commits and pushes there too. - Commands that reach such a workspace only through a route from another - one, such as a routed `bd show`, fail without touching it, but not with - code 16. Store-less commands such as `bd version` and `bd help` still run. - A `metadata.json` rewrite keeps `graph_mode` and the `graph_*` fields - graph-preview builds write, and like those builds it omits a zero - `graph_schema_version` and a false `graph_ready`. -- **A graph workspace without `graph_mode` in its metadata is refused too.** - Graph-preview builds write a `.beads/graph-preview-format` marker before - `metadata.json`. bd ignored it. When an older bd had rewritten - `metadata.json` without `graph_mode`, bd opened the graph database by its - recorded name and ran Issue schema initialization against it; when - `metadata.json` was never written or was lost, bd fell back to the default - database name and created an Issue database inside the graph workspace, and - `bd init` initialized an Issue workspace there. A `.beads` holding that - marker, whatever it contains and whatever its metadata says (absent, - stripped, unparseable or an explicit `dependency`), is now refused wherever - a `graph_mode` workspace is: commands run in it or naming it with `--db` or - `BEADS_DB`, `bd create --repo` aimed at it, and `bd bootstrap` in a - workspace below it that would take its metadata exit with code 16 and a - hint to restore `metadata.json`, and routed and cross-repository opens fail - without opening its database. -- **The Go library's opens refuse a graph workspace too.** - `beads.OpenBestAvailable`, `OpenBestAvailableWith`, `OpenFromConfig` and - `OpenGated` opened a workspace whose `metadata.json` declares a - `graph_mode` other than `dependency`, or whose `.beads` holds the - `graph-preview-format` marker, as an Issue store and ran Issue schema - migrations into the graph database. They now - return `*beads.UnsupportedWorkspaceFormatError` (match it with `errors.As`) - before opening, creating or changing anything, the same refusal the CLI - exits 16 with. -- **`bd setup claude --remove` no longer leaves a blank-line hole.** Removing - the beads section from `CLAUDE.md` (or, with the other setup recipes, from - `AGENTS.md` or `GEMINI.md`) left the blank lines around it behind, three in - the `CLAUDE.md` `bd init` writes, and setup then re-added the section after - two more, so every remove and re-add grew the file. `--remove` now takes - those blank lines with the section, and setup appends a section after one - blank line, so removing and re-adding it toggles between the same two files. - A re-added section goes at the end of the file. -- **`bd config set` no longer truncates a `config.yaml` that has a line of - 64 KiB or more.** Setting an undotted key (such as `actor` or `no-db`) - rewrites the file line by line, and a line of 65,536 bytes or more ended - that read early without an error, so the command reported success and wrote - back only the lines before the long one, plus the key it set. Dotted keys - and `bd config unset` were not affected. Setting an undotted key in such a - file now fails with `bufio.Scanner: token too long`. `bd config set` leaves - the file as it was. `set-many` stops at that key, but it has already written - any `config.yaml` keys listed before it, such as a dotted `sync.remote`. -- **`bd show --as-of` exits 1 when it reads no issue.** A ref the Dolt AS OF - read does not take (an instant, a versioned-history address), a ref that - does not exist, or an issue that did not exist at the ref was reported on - stderr while the command exited 0 with nothing on stdout, even under - `--json`. It now ends the way plain `bd show` does: exit 1, with the JSON - error envelope on stdout under `--json`. A batch that reads at least one of - its ids still exits 0. Both the direct and the proxied-server routes - changed. -- **`bd list --format dot` prints the same graph the same way every time.** - Nodes are declared in id order and edges in source, target and type order. - They used to follow the listing order, which breaks a priority tie by - creation time to the second, so two equal-priority issues swapped places - depending on whether they were created in the same second, and the - proxied-server route did not pin the order of one issue's edges. `--sort` - and `--limit` still choose which issues are listed. -- **`bd init --quiet` no longer prints "Removed SessionStart hook" or "Removed - PreCompact hook"** when the project's `.claude/settings.json` (or the legacy - `.claude/settings.local.json`) still carries the bare `bd prime` hooks an - older bd registered. The hooks are still migrated to the SessionStart - `bd prime --hook-json` hook; the removal lines now go to the installer's own - output, which `--quiet` discards, instead of straight to stdout. -- **A new `AGENTS.md` from `bd init` lists `bd dolt push` in its Quick - Reference only when the workspace has a Dolt remote.** The managed Beads - section already left out its push lines when no Dolt remote is configured or - the workspace is `no-push`, but the Quick Reference above it still showed - `bd dolt push # Push beads data to remote`. It now follows the same rule. - `bd init` still leaves an existing `AGENTS.md` alone outside the managed - section, and writes an `--agents-template` as given. -- **`bd setup claude` no longer rewrites a CLAUDE.md whose beads section is - already current.** A rerun (and `bd init`, which runs it) wrote the same - bytes back as a new file, resetting its mode to 0644 and its mtime, and - reported "Updated existing beads section"; it now leaves the file alone and - reports the section current (the report `bd init` already gives a current - AGENTS.md). `bd setup factory`, `opencode`, `mux` and `gemini` share the - fix. A section whose END marker is missing or comes before BEGIN, which was - also reported as updated while nothing changed, is now reported as an error: - `bd setup factory`, `opencode` and `mux` exit 1 (`mux` before installing its - `.mux` hooks), while `claude` and `gemini` warn and carry on. When CLAUDE.md - is an `@AGENTS.md` stub and it is AGENTS.md that cannot be updated, `claude` - now names AGENTS.md in the warning and keeps the beads block left in the - stub, which it used to delete although AGENTS.md was never updated. -- **Time-bound filters and the keyset cursor compare the same instant on every - engine, whatever zone they are given in.** The storage filter builder binds - every time bound (`created_*`, `updated_*`, `closed_*`, `started_*`, - `defer_*`, `due_*`) as the UTC spelling of its instant. An engine whose - timestamp column carries no zone drops a literal's offset, so a `+02:00` - bound would have compared as that wall clock; Dolt already honored the - offset and `bd` resolves bounds to UTC, so no answer changes here. The - keyset cursor (`AfterCreatedAt`) is now bound in UTC too: embedded Dolt's - driver sends a `time.Time` as its wall clock with no zone, so a library - caller paging with a cursor in another zone was answered from the wrong - position there (server Dolt was unaffected). -- **`bd setup claude --global` keeps its beads section in `~/.claude/CLAUDE.md`.** - It wrote the section into the current directory's `CLAUDE.md`: run from your - home directory it created `~/CLAUDE.md`, which by default stops Claude Code - reading `AGENTS.md` in the projects below it, and `--remove --global` - stripped the section a project's own install had put in its `CLAUDE.md` - while leaving that project's hooks in place. A global install and removal now - use only the user-level files beside `~/.claude/settings.json`. A new - `~/.claude/CLAUDE.md` holds just the section, rendered the same whichever - project you run from. Claude Code loads that file in every project, so its - section, task-tracking rules included, applies only in repositories that use - beads. With global or plugin hooks `--check` accepts it when the project's - instructions have none, or names `--global` when it is missing and the - project has no `CLAUDE.md` of its own. Run from `~/.claude`, - or from your home directory unless `~/CLAUDE.md` still holds a section an - earlier run left there, `--check` judges the user-level files, and from your - home directory it names `--global` when no hooks are installed. From your - home directory, whose `.claude/settings.json` is the global one, `--remove` - removes the user-level section with the hooks. If you ran `--global` before, - re-run `bd setup claude --global` to add the section to `~/.claude/CLAUDE.md`. - A section an earlier `--global` run left in a directory's `CLAUDE.md` stays - there; delete it by hand if you don't want it. -- **`bd blocked --label`, `--label-any` and `--exclude-label` apply to a row - blocked only by an unsatisfied `external:` dependency.** The - external-dependency policy lists such a row itself, after the label query - has run, and it was listed whatever its labels; it is now narrowed by the - label filters like every other row, on every route. On the proxied-server - route such a row also printed with no labels; it now prints them. -- **`bd gate add-waiter` survives contention the store gave up replaying.** - It retried only a version miss, so a convoy adder failed when its guarded - write ended in a serialization failure or deadlock the backend stopped - replaying (Dolt/MySQL 1213/1205, Postgres 40001/40P01), or, over HTTP, in a - 503 busy. Those are now retried, but at most twice: each arrives only after - the store or `bd serve` has spent a whole replay budget on the write (up to - 15s), so the 20-write bound a version miss gets could hold an adder for - minutes. Each retry waits the jittered backoff plus a busy answer's - Retry-After, as `bd serve` sent it. The retry is safe because every round - re-reads the gate, deduplicates and writes the whole list guarded by the - version it read: a 503 is not proof that nothing was written (a gateway can - answer one after the commit), and a write that did land is found on the - re-read, so the waiter is never added twice; when the last write allowed is - answered busy, one more read checks for it before the command fails. A - server fault (500) and an unreachable database are still returned at once, - and once the retries run out the error says no write was confirmed and - names the write contention, instead of saying the gate kept changing. -- `bd list --title-contains X` works against an HTTP workspace. The client - folds it onto the listing's existing `title` parameter (the store compiles - both flags to the same case-insensitive substring predicate), so it needs - the `issues.list.title` capability, like `--title`. A `--title` and a - `--title-contains` that differ other than in letter case are two - predicates, so they ride `title` and `title_contains` together and also - need `issues.list.filters` (see Added), as does the `--closed-after` in - gascity's polecat pre-flight lookup of recently closed beads. Either flag - with `--parent` in the default tree view still refuses over HTTP. -- **`bd gate create`, `bd gate add-waiter` and `bd gate discover` work over a - connected HTTP workspace.** They wrote through the legacy `CreateIssue`, - `AddDependency` and map-based `UpdateIssue`, which the HTTP store does not - implement, so each failed before any gate member reached the wire. They now - run through the library roles on every route: - - `bd gate create` is ONE `Lifecycle.Create` carrying the gate members and - a reverse `blocks` edge from the blocked issue to the new gate, so the - gate and its edge land atomically (one `createIssue` over HTTP, behind - `issues.create.gates`). The direct route used to make two writes and an - explicit commit; the proxied-server route now uses the same role. - - `bd gate add-waiter` reads the gate through `Reader.Get`, deduplicates, - and writes the whole waiter list through `Lifecycle.Update` guarded by - the row version it read. On a version mismatch it re-reads and retries - after a jittered, doubling backoff (10 ms up to 400 ms per wait, at most - 20 writes), so a convoy of workers registering on one gate at once all - succeed. On the direct route concurrent adders used to lose - each other's waiters while every one exited 0; no waiter is lost now. - Over HTTP it needs `issues.update.gates`. - - `bd gate discover` lists through `issueops.ListGates` (as `bd gate check` - does) and records each discovered run ID through `Lifecycle.Update`, the - same `await_id` write `bd gate check` and `bd close` make. Over HTTP it - needs `issues.update.gates`. Its summary now counts only the gates whose - run ID was recorded, and it exits non-zero when any matched gate's write - failed (it used to report such a gate as updated and exit 0). +## [1.3.2-rc.1] - 2026-10-05 - Against a server that does not advertise the token it needs, `bd gate - create` and `bd gate add-waiter` are refused before any write, naming the - token; `bd gate discover` writes nothing and reports the token as that - gate's error. The served corpus gains `mc-gate-create-timer`, - `mc-gate-check-timer-resolves`, `mc-gate-add-waiter` and - `mc-update-await-id` (pending until the reference server advertises the - gate tokens), pinned passing by `TestE2E_GatesOverHTTP`. -- **`bd import` no longer reports a failed dependency check as a skipped - dependency.** Import skips an edge the dependency checks refuse (a - self-dependency, a cycle, or a blocker that is the issue's own ancestor or - descendant) and reports it on a `Skipped dependency:` line, or in - `skipped_dependencies` with `--json`. It did the same when the check's own - query failed, for example on a timeout, a dropped connection or a statement - the backend could not run: the SQL error became the skip reason, and on - Dolt, where the transaction survives a failed statement, the import - committed without the edge and exited 0. Such an error now fails the - import. Refused edges are still skipped as before. +First release candidate for 1.3.2, a patch on top of 1.3.1 with **no schema +migration**: upgrading from 1.3.1 is a binary swap. It fixes `bd purge` +deleting the closed root of a chain that still has live work under it, and an +auto-backup watermark that could claim a commit the snapshot does not contain. +It also carries an upgrade note for stores migrated by 1.3.0 or 1.3.1 +([#7037](https://github.com/gastownhall/beads/issues/7037)). -- **`bd mol bond ` ignored prefix routing** — operand resolution now uses - the same routing fallback as `bd show`/`bd update`/`bd close`, so a bead that - lives in a routed store (a prefix-routed rig via `routes.jsonl`, or the - contributor planning store) resolves instead of failing with `'' not found - as issue or formula`. Previously `bd mol bond` resolved operands - against the local store only, so `gt sling ` — which cooks the - default formula and bonds it to a rig-routed bead — died at `bd mol bond` even - though `bd show ` resolved the same ID. Resolution is write-intent: the - routed target opens writable and the bond commits where the bead lives. Two - operands that live in the same routed rig share one store handle and bond - normally; only a bond whose operands resolve to genuinely different databases - is rejected rather than written to the wrong one. As part of this change, an - auto-route — contributor, maintainer or default — now opens writable for the - commands that change an existing issue (`bd update`, `bd close`, - `bd comment`, `bd unclaim`, `bd dep` and the other routed mutation commands, - as well as `bd mol bond`), so the write commits where the issue lives: for a - contributor, in the planning repository, as the contributor workflow - documents. Every auto-route used to open read-only, so on an embedded - workspace these writes were refused. `bd unclaim` now opens a routed issue - writable, as `bd update --claim` does, and commits the release there; on an - embedded workspace `bd human respond` and `bd human dismiss` now record a - routed change as a Dolt commit in the routed repository instead of leaving it - uncommitted in that repository's working set. A routed - write, prefix or auto, never creates the target's database: an embedded - repository that has not been bootstrapped yet fails the write instead of - gaining an empty database that `bd bootstrap` would then take for the real - one. An auto-routed store, read or write, now opens the target's own - configured `dolt_database`, as a prefix route already did, even when - `BEADS_DOLT_SERVER_DATABASE` names the current workspace's database. An - auto-routed write refuses a target whose `metadata.json` is unreadable or - names no `dolt_database`, while a target with no `metadata.json` at all (a - planning repository nothing has been created in yet) opens the one embedded - database it holds, or its default database when it holds none or several; a - prefix route still requires the target's `dolt_database`. A - routing failure, such as an unreadable `routes.jsonl` or a - routed store that fails to open, is now reported as itself instead of as - `no issue found matching`. Same routing-parity fix as - [#3608](https://github.com/gastownhall/beads/issues/3608) for `bd close`. - Fixes [#4714](https://github.com/gastownhall/beads/issues/4714). +### Upgrade notes -- **`bd gate check` works over a connected HTTP workspace.** Its open-gate - listing built the same raw `SearchIssues` filter `bd gate list` did before - S20, so the HTTP backend refused the command before reading a gate and gc's - core `gate-sweep` order (`bd gate check --type=timer --escalate`) failed on - every remote scope. It now lists through `issueops.ListGates` over the - route's Querier (direct, proxied and remote alike). Closing a resolved gate - was already served. A bead gate's first sighting of its awaited bead - (`await_seen`, recorded when the check finds that bead, not yet closed, in - the gate's own store) was written through the legacy map-based - `UpdateIssue`, which the HTTP store does not implement, so every open bead - gate with an open target was an error on every sweep; it now goes through - the Lifecycle role's `Update` (a single-key metadata set, `issues.update` on - the wire), as does the gh:run discovery's `await_id` persist, which the - HTTP client sends to a server that advertises `issues.update.gates` (see - Added). Against an older server it refuses that patch before the dial, so a - gh:run gate with a workflow-name hint stays that gate's error, naming the - missing token and the server rather than a Go method. - The sweep also skips the gates of a persisted proto - (`bd cook --persist`): they are templates that every pour copies, so a - write to one was refused on every sweep and, in a local workspace, a - resolved one (an expired timer, say) was closed in the proto itself. The - listing's query leaves them out (`template=false`), so they take no row of - `--limit` and cannot crowd a live gate out of the sweep; `bd gate list` - still lists them. A negative `--limit` is now an error; the direct and - proxied routes used to read it as no limit. The served corpus gains - `mc-gate-check-timer`, `mc-gate-check-gh`, `gate-check-all` and a served - test over await-typed and template gates. +- **After upgrading from bd ≤1.3.0, run `bd recompute-blocked` once per + workspace** ([#7037](https://github.com/gastownhall/beads/issues/7037)). + Migration 0059, first shipped in 1.3.0, recomputes the denormalized + `is_blocked` flag with a recursive query. On Dolt servers older than 2.4.0, + and on the embedded engine, a query-planner bug + ([dolthub/dolt#11886](https://github.com/dolthub/dolt/issues/11886), fixed + in Dolt 2.4.0) drops the query's `parent-child` edge filter. Blockedness then + spreads across every dependency type, including `relates-to`, + `discovered-from` and `tracks`. Beads that are not actually blocked are + marked blocked and silently disappear from `bd ready` and `bd list --ready`. + The flag is stored, so the damage persists until it is recomputed: nothing in + 1.3.2 repairs it automatically. -- **`bd list`'s text renderings read only the listed issues' dependency - edges.** The tree (the default), `--deps`, `--parent`, `--format` and - `--watch` loaded every dependency record in the workspace - (`GetAllDependencyRecords`) to draw one page. Over a connected HTTP - workspace that is a walk of the whole workspace: every issue id, 200 per - request, then every id's edges, 100 per request. `bd list --limit 20` - against a ~3,800-issue workspace made 62 sequential requests (~5.7 s at - ~50 ms a round trip). Every rendering reads only edges a listed issue owns, - so the listing now reads exactly those through the `EdgeReader` role: one - request per 100 listed ids over HTTP. The same listing makes 6 requests - (~0.5 s), one more than `bd list --json`, and prints the same bytes. On a - local store a default page is as fast or faster. A listing that covers most - of a large store now pays batched reads (four queries per 200 listed ids) - instead of one scan of both edge tables, which costs more past roughly - 1,000 listed rows: +0.07 s at 1,000 rows and +0.46 s at 4,000 on embedded - Dolt. A store with many wisps, whose edges the old read also scanned, is - faster at every page size. -- A heartbeat can no longer resurrect a lease after a release or close. A - heartbeat re-arming a hand-doled claim (in_progress with no lease row) could - race a release or close on Dolt and leave an orphaned lease row on an open - or closed issue; every later heartbeat by the former holder then refreshed - it and answered success for a claim it no longer held. This affected every - Dolt route, including releases before this one. The re-arm is now recorded - as a mutation and remints the issue's row_lock, which every claim-ending - path rewrites, so it collides with a racing release or close and one side - is replayed against the other's result (the cost: a hand-doled claim's - first heartbeat changes its `revision` once). The refresh also requires, in - the same transaction, that the issue is still in_progress under the holder, - and a refusal deletes the caller's orphaned lease row, so a lease row - orphaned any other way never answers a heartbeat. A refresh and a refusal - still make no Dolt commit on any route; a re-arm commits only its own - tables (never `DOLT_COMMIT('-Am')`), so another writer's uncommitted - batch-mode edits to other tables stay uncommitted (a re-arm stages its - tables whole, as every other direct-route write does). On embedded Dolt, a heartbeat landing in - the same second as the previous one no longer re-grants the lease - (rewriting its `granted_at` / `granted_node`), and on every route a - heartbeat whose actor is a different spelling of the lease's holder - (`gastown.mayor` for `gastown__mayor`) refreshes the lease in place, - keeping its `granted_at` and granting replica, instead of re-granting it - under this node. For a Postgres port: the - refresh now reads the issue row, so under SERIALIZABLE expect more retried - serialization failures between a holder's own edits and its heartbeat. -- **`bd gate list` works over a connected HTTP workspace** (S20). The - workspace-wide listing built a raw `SearchIssues` filter, which the HTTP - backend refuses, so gc's `renudge-stale-human-gates` order failed on every - remote scope. Every route now calls one library entry, - `issueops.ListGates`, which asks the Querier role (`queryIssues`, served by - every deployed server) for `type=gate` with closed rows hidden unless - `--all`: the same rows the raw filter selected, on both planes. `--limit 0` - is unlimited, as before. The bead-scoped form (`bd gate list `) is - unchanged. -- **`bd export` works over a connected HTTP workspace** (S21). It read - through the raw `SearchIssues` method and the bulk label, dependency and - comment reads, which the HTTP backend refuses, so gc's 15-minute JSONL - export order (`gc bd export --all`) failed on every remote scope. On a remote - backend the read set now comes from one library entry, `issueops.ReadExport`, - composed over role reads every deployed server publishes: an unlimited - `Reader.List` for the rows, their labels and counts, `EdgeReader.ReadEdges` - for the dependency records, and `Reader.Get` with comments for each row - that has any. The JSONL is written by the same code as on every other - route. Memories (`--all`, `--include-memories`) come from the served memory - plane (`listMemories`), not the settings listing, which withholds every - `kv.*` row and so exported none. A failed read fails the export, never - shortens it. A remote export does not yet stamp the `wisp_plane` marker, - because the server publishes no plane membership. So a current `bd import` - of a remote export puts genuine no-history wisps on the durable issues - plane instead of the wisps plane, and gc's JSONL archive keeps them. - Nothing is dropped. -- **A silent HTTP connection no longer costs a read the full 60s.** The HTTP - backend client gives a read's first attempt 15s from when it is sent to get - its response headers (`BEADS_HTTP_RESPONSE_HEADER_TIMEOUT`: a duration, a - number of seconds, or `off`), then retries it once on another connection - with no header timer, on what is left of the same 60s request timeout. A - slow but healthy read still finishes if its retry's headers and body - together fit in that remainder (about 45s); one that needs longer now fails - at 60s. A read whose connection is reset is retried the same way (on Windows - only when the reset surfaces as an early EOF), and each retry is logged to - stderr with its count. The events:watch stream's connect gets neither the - timer nor the retry. A write is never retried; one that was sent and timed - out before its headers fails with a typed `wire.ResponseHeaderTimeoutError` - naming the server's host, because it may or may not have been applied, - while a dial or TLS timeout stays a plain connection error. The batch get, - a read on POST, is now also retried on a 503 the way a GET is. -- **A large batch apply over HTTP no longer fails at 60s while the server - commits it.** `bd serve` runs an `issues:batchApply` of more than 100 items - (a large `bd create --graph` plan, say) under its `--large-apply-ceiling` - (5 minutes by default), on a context detached from the client, but the HTTP - backend client gave every request 60s. A large apply that took longer - failed client-side with a timeout while the server went on to commit it. - Such a request is now dialed with 6 minutes, which covers the server's - default ceiling plus what it may spend before (reading the request, waiting - for the one-wide large-apply slot) and after (writing the answer back). - Every other request keeps 60s. An `*http.Client` passed in through - `backend/http`'s `Options.HTTPClient` is raised the same way for that one - request when its `Timeout` is shorter than 6 minutes; one with no `Timeout` - stays unbounded. A server whose `--large-apply-ceiling` is raised past about - 5m15s can still outlast the client: it does not advertise its ceiling, so - the client cannot learn it. -- **An update of a template is now refused for every caller, not only - `bd update`.** The template guard used to be a pre-read in `cmd/bd`, so an - update through `bd serve` (`PATCH /v0/beads/issues/{id}`), an `update` item - of `issues:batchApply`, or a library caller of `issueops.Lifecycle.Update` or - `BatchApplier` edited a template that `bd update` refused. It now lives in - the role, in the update's own transaction, on every backend: any update - naming a template refuses with `*issueops.TemplateReadOnlyError` (matching - `ErrTemplateReadOnly`), whatever its patch and force flags, after the - compare-and-set guards. Over HTTP it is `409` `template_read_only`, and the - client rebuilds the same typed error. HTTP and library callers that edited - templates are now refused (pour the template instead) unless they set the - new `UpdateRequest.AllowTemplate` (`allow_template` on the PATCH body), which - stands the guard down for that one request. `bd update`, `bd assign` and the - proxied `bd tag` print the same line as before; `bd label` and - `bd set-state` set `AllowTemplate` and keep editing templates as they always - have. A batch create that makes a template and splices its metadata still - lands: the splice finishes the create. Version skew: a new `bd` against an - older `bd serve` (no `issues.update.allowTemplate` token in the handshake) - refuses a single template update itself before dialing, and never sends - `allow_template` to it. A batch `update` item gets no such check (its target - resolves on the server), so through an older `bd serve` it still edits a - template. An older `bd label` or `bd set-state` against a new `bd serve` - cannot send `allow_template`, so it now gets `409` `template_read_only` on a - template; upgrade the client. Not yet moved: `bd comment`, `bd note`, - `bd priority` and `bd tag` still refuse templates with their own pre-read in - `cmd/bd` (their direct-route writes do not go through `Lifecycle.Update`), so - for those four the guard lives only in the CLI. Claim, reopen, `:casMetadata` - and the served `addComment` have no template check at all yet (bd-jkp9v3): - `POST /v0/beads/issues/{id}:claim` (`issueops.Claimer`) still claims a - template that a `PATCH` with `claim: true` refuses. Close's guards move to - the role in their own change (#7425). -- The dotted-id hierarchy refusal (a child such as `bd-abc.1` may not carry an - explicit edge to its own ancestor, other than the parent-child edge to its - immediate parent) moved from `cmd/bd` into the library as - `issueops.CheckDottedChildDependency` / `*issueops.DottedChildDependencyError` - (unwraps to `ErrValidation`). Every `DependencyEditor` (dolt, embedded, - unit of work, HTTP) and every `BatchApplier` dep_add item now enforces it, so - an HTTP `dependencies:add`, a `batch:apply` edge and `bd create --graph`'s own - edges are refused like `bd dep add` (HTTP: 400 `invalid_argument`). `bd dep - add`, `bd dep --blocks`, `bd link` and `bd dep add --file` print the same - messages as before, and a `bd batch` script's `dep add` line, which neither - backend checked, is now refused with that message and rolls the whole batch - back. The refusal is decided from the two ids, ahead of every check that - reads the stored graph, so for a child whose parent-child edge is already - stored (a `bd create --parent` child) an HTTP caller now gets that 400 where - it used to get 409 `dependency_cycle` (a blocking type) or - `dependency_exists` (any other type). -- An HTTP create (`issues:create`, `issues:batchCreate`, a `batch:apply` - create item) that omits `priority` now stores the create default, P2, as the - spec documents, instead of P0 (critical). The default lives in one place: - `issueops.CreateRequest.DefaultPriority` (and `BatchCreateItem` / - `CreateItem.DefaultPriority`) asks the shared create preparation to store - `issueops.DefaultCreatePriority`; the handlers set it for an absent member, - the HTTP client omits `priority` for it only when the server advertises the - new additive `issues.create.defaultPriority` handshake token (an older - `bd serve` reads an absent `priority` as P0, so a new client sends `2` - explicitly to it), and `bd create` (no `--priority`), - `bd create --graph` (a node without `priority`) and `bd create --file` (a - template without `### Priority`) rely on it rather than spelling 2. An - explicit `0` is still P0, and `DefaultPriority` with a non-zero priority is - `ErrValidation`. CLI output is unchanged on the in-tree backends, except - that `bd create --file` now warns on stderr about a `### Priority` it cannot - parse, as it already did for `### Type`, rather than dropping it silently. - The client decides from the handshake its store cached (once per command - in `bd`), so a create that lands on an older build behind the same URL (a - `bd serve` rolled back under a live store, or mixed builds during a - rollout) still stores P0, with no error on either side. - Out-of-tree storage backends must now honor `DefaultPriority`; see the - BREAKING (out-of-tree storage backends) entry under `### Changed`. -- Blocked-state repair no longer lets a stale `is_blocked` bit on a parent - with no reason row of its own hold itself up through a parent that blocks - on its own child, in a malformed parent-child cycle (gastownhall/beads#7386). - The explained-parent walk, on both the full repair and the per-write - recompute, now climbs through such a parent, whose bit is inherited, - instead of treating it as a final unexplained ancestor; `bd doctor`'s - Blocked State check counts the stale bit and `--fix` clears it. + `bd recompute-blocked` rebuilds the flag from the dependency graph. It is + idempotent and reports `rows_corrected`. It applies to every workspace whose + store crossed migration 0059 under bd 1.3.0 or later, which includes a + workspace now on 1.3.1 that was originally created by 1.2.x or earlier. + Running it on a workspace that was not affected changes nothing. -- `bd show --current` over the HTTP backend no longer prints the wrong issue. - Its in-progress/hooked lookup used the raw `SearchIssues` method, which - the HTTP backend refuses; the refusal was swallowed and the command fell - back to the last-touched issue, exit 0. The lookup now reads through the - `issueops.Querier` role, and a failed read is an error on every route - (including `--proxied-server`), never a fallback. A miss (nothing in - progress or hooked, or no actor) is another matter: `--proxied-server` now - falls back to the last-touched issue as the direct route always has, where - it used to fail with `no current issue found`. -- `bd list --type --include-ephemeral` (and `GET - /v0/beads/issues` with the same parameters) now include that type's durable - rows — a promoted wisp, an issue retyped after it was created — beside its - wisps, as `--include-ephemeral` promises; the type used to narrow the read - to its wisps alone. `bd count --include-infra --include-ephemeral` and the - count endpoint count the same rows. An infra `--type` without - `--include-ephemeral` still reads only the wisps, and `bd search --type - `, now served by the same listing, still finds that type's - wisps and durable rows alike. -- `bd recompute-blocked` on a workspace connected to `bd serve` is a no-op - that exits 0 instead of refusing: the server maintains `is_blocked` on - every write and the column lives in its database. `--json` reports - `{"rows_corrected": 0, "maintained_by": "server"}`. -- **The http store's `GetIssue` answers a miss with `storage.ErrNotFound`, - like the local stores.** It used to answer a missing (or empty) id with - `(nil, nil)`, which every caller written against the backend contract - read as a hit: `bd create --graph` refused every plan naming an explicit - id over http. The miss is now `not found: issue `, wrapping - `storage.ErrNotFound` exactly as `issueops.GetIssueInTx` does; the empty - id never dials. -- **`bd list`, `bd children`, `bd show --json` counts, the default `bd dep - tree`, `bd ready --claim` and `bd close` work on an http workspace.** The - external-dependency decorator rebuilt its views from storage seams the - http store only partly serves, so `bd list`/`bd children` refused, `bd - show --json` printed zero dependency and comment counts without an error, - and the default dep tree refused; each view now passes the server's own - role through (see *Added*). A missing id is a plain not-found after one - `getIssue`, instead of a partial-id search refusal, and `bd update - --claim` answers a not-found gc classifies. -- **`bd mol wisp gc --age 0s` can miss a just-created wisp.** Dolt's - `DATETIME(0)` column rounds fractional seconds half-up on write rather - than truncating, so a wisp created in the second half of a wall-clock - second can read back with `updated_at` up to 0.5s in the future. The GC's - age check (`now.Sub(updatedAt) > ageThreshold`) then compares against a - negative duration and fails, even though `--age 0s`'s own contract is - that every wisp qualifies. `ageThreshold <= 0` now bypasses the clock - comparison entirely (#7236). -- **`bd mol wisp gc` no longer deletes a fresh step because a wisp it - depends on is abandoned.** The GC applied `--age` only to the wisps it - selected directly, then deleted their dependents at any age as long as - they were not live work, so a step created seconds earlier went with an - old wisp it depended on, and raising `--age` did not help. A dependent - now has to meet `--age` itself, by the same rule as the wisp it depends - on (including the #7236 rounding fix). One that is too young is spared - and loses its edge to the deleted wisp: an open one is collected once it - ages out, a closed one is left for `--all`, `--closed` or `bd purge`, - since without `--all` the GC reaches a closed wisp only as the dependent - of an abandoned one (#5219). -- `bd create --parent` works on the HTTP backend: the create role mints the - `.` child id (and inherits the parent's labels) inside the - create on every route, instead of the CLI reserving it through a raw - `GetNextChildID` the HTTP backend does not serve. `bd mol current` (with - or without an id) and `bd mol progress` no longer answer empty or refuse - over http, and `bd ready --mol` step rows carry their assignee and - timestamps. -- **Server-mode config writes are committed again.** On the direct SQL-server - route, `bd remember`, `bd forget` and `bd config set|unset|set-many` wrote - the `config` table but never created a Dolt commit: `maybeAutoCommit` - returns early off the embedded route, and plain `Commit()` excludes config - (GH#2455). The rows sat in the working set, where `bd dolt push` does not - carry them, and a dirty internal key such as `status.custom` made the next - `bd dolt pull` refuse until an explicit `bd dolt commit`. With - `dolt.auto-commit` on (the default) these writes now commit right away - through the scoped `CommitConfigOnly`, which stages only `config` and the - lookup tables projected from it, so a concurrent operation's other dirty - tables are never swept; `batch` and `off` defer them to `bd dolt commit` - like every other write. `bd remember` and `bd forget` commit only user - `kv.*` rows. When an internal config key is dirty (such as the - `issue_prefix` that `bd rename-prefix` writes), the memory is still - stored but not committed: the command warns on stderr with the keys named - and exits 0, `--json` adds `committed: false`, `commit_error` and - `dirty_config_keys`, and `bd dolt commit` publishes the memory with those - keys. v1.0.1 ([#3052](https://github.com/gastownhall/beads/pull/3052)), - whose commits this restores, also warned on a failed memory commit - instead of failing the write. `bd config unset` of a yaml-only secret key - also commits its delete of a credential row an older bd stored in the - database, so HEAD, which `bd dolt push` publishes, no longer keeps it - ([#4078](https://github.com/gastownhall/beads/issues/4078)). +### Fixed -- **PRs based on `hotfix/**` branches now run full CI, not just - cross-version historical smokes and triage labeling.** `pr.yml`, - `pr-risk.yml`, `conformance.yml`, `cross-version-smoke.yml` and - `regression.yml` all trigger `pull_request` on `main` and `release/**`, - but not on `hotfix/**` — a backport PR based on a hotfix line (the same - position `release/**` was added for) got no unit shards, no lint, no - risk gate, and no required CI Gate at all: 15 passing cross-version - smokes and triage jobs, nothing else. Added `hotfix/**` alongside - `release/**` in all five (#7148). +- **`bd purge` keeps the whole closed chain above a live bead, not just its + direct parent** ([#7041](https://github.com/gastownhall/beads/pull/7041), + fixes [#7031](https://github.com/gastownhall/beads/issues/7031)). The + live-dependent protection added in 1.3.1 only protected a closed candidate + that a not-done bead pointed at directly. Take a closed root R, a closed child + S1 and an open grandchild S2. S1 was protected because S2 is live, but R was + not, because its only dependent S1 is closed. Purge deleted R, which orphaned + S1 and S2, and `--dry-run` reported the same deletion. Protection now + propagates through closed candidates until nothing changes, on every backend + (dolt server, embedded, proxied/uow). An orchestrator retention sweep + (`bd purge --wisps-plane --older-than ...`) is the common way to hit this. +- **The backup watermark no longer runs ahead of the snapshot** + ([#7044](https://github.com/gastownhall/beads/pull/7044), fixes + [#7032](https://github.com/gastownhall/beads/issues/7032)). Auto-backup read + Dolt HEAD *after* the sync and recorded that as the newest commit backed up. + A commit that landed during the sync was therefore recorded as backed up even + though the snapshot did not contain it, and auto-backup reported "no changes + since last backup" until HEAD moved again. HEAD is now read before the sync, + so such a commit stays pending for the next backup. Only opted-in auto-backup + (`backup.enabled: true`) was affected; `bd backup sync` does not record a + commit watermark. +### CI -- **Ordinary bd commands now wait out a maintenance operation instead of - failing at once.** While `bd init`, `bd backup restore`, `bd migrate` or - `bd bootstrap` holds the workspace gate exclusively, every other bd command - on that workspace — and, in shared-server mode, on every project sharing - the server (another project's ~8s `bd init`) — failed immediately with "a - maintenance operation is running". They now wait up to 30s (one "waiting - for another bd process" notice after 2s; Ctrl-C aborts), then fail with the - same error naming the bound (and, in shared-server mode, saying the holder - may be another project). Override with `BEADS_GATE_WAIT_TIMEOUT` (`1m`, - `90`); scripts that relied on the old fail-fast behavior should set - `BEADS_GATE_WAIT_TIMEOUT=0`. Git hooks stay fail-fast so a commit or - checkout is never stalled. Waiting is writer-fair: once a maintenance - operation is waiting for the gate, newly started commands queue behind it, - so a steady stream of short commands can no longer starve `bd init`. The - queue only ever delays a command, never fails it, and a maintenance - operation that cannot get in (for example behind a `bd list --watch`) - stops queueing others after 10s. Queued maintenance operations run one - after another, and a waiting command waits for all of them. The queue - marker is an OS lock beside the gate (`*.gate.lock.intent`, covered by the - existing `*.gate.lock*` gitignore pattern), so a crashed waiter never - leaves a stale queue behind. `bd init`'s own wait (`BEADS_INIT_GATE_TIMEOUT`) - rises from 30s to 60s, since back-to-back inits under load take 10-15s - each. +- PRs into `hotfix/**` branches now run full CI, including the required + `CI Gate / Required` check + ([#7198](https://github.com/gastownhall/beads/pull/7198), fixes + [#7148](https://github.com/gastownhall/beads/issues/7148); ported to this + branch in [#7217](https://github.com/gastownhall/beads/pull/7217)). -- **Concurrent `bd init --shared-server` runs in different projects no - longer refuse each other.** Every shared-server project gates the one shared - dolt data dir, and `bd init` holds that gate exclusively for its ~8s run but - waited only 5s for it, so a second init (or an init during another - project's long command) failed with "bd init refuses to run over live bd - activity". `bd init` now waits (60s by default, see the gate-wait entry - above), printing one "waiting for another bd process on the shared server" - notice after 2s, then fails with the same refusal naming the bound. - Override it with `BEADS_INIT_GATE_TIMEOUT` (`2m`, `90`). Other exclusive - operations keep their 5s wait. +## [1.3.1] - 2026-09-30 -- **A proxied-server command against an unreachable external Dolt upstream - now fails within about a second with a clear error instead of stalling - ~20-30s.** The local db proxy stayed up, so the client only saw a bare - close, which the bootstrap ping retried as a transient drop for its whole - 30s budget before reporting `invalid connection`. The proxy now answers such - a connection with a MySQL error (2003, prefixed `beads db proxy:`) saying - what happened: the dial was refused / the socket is missing / the host is - unreachable, or the upstream closed the connection before the MySQL greeting - (down, restarting, or at its connection limit). The client retries that - error only for about a second, so an endpoint that is flapping or rebinding - is still ridden out within one command. A backend that drops a connection - after the greeting, a dial timeout, and every refusal from a managed (local - sidecar) backend keep the full transient retry. -- **`bd ready --explain` no longer reports a pinned dependency as a resolved - blocker.** The ready query skips a pinned target exactly as it skips a closed - one, so a `blocks` edge onto a pinned bead never fences its dependent — but - the explanation listed every blocking edge of a ready issue under - `Resolved blockers`, status unread, and a bead wired behind a long-lived - pinned bring-up reported itself satisfied while its precondition was unmet. - Each blocking edge is now sorted by the target's status: closed under - `Resolved blockers` (and `resolved_blockers`), pinned under - `Pinned dependencies (never block)` (`pinned_dependencies`), and any other - status under `Open dependencies (not blocking)` (`open_dependencies`) — the - shape #6066 reports for a foreign-prefix blocker, now visible instead of - passing as resolved. `Reason` keeps its `N blocker(s) resolved` lead and - appends the pinned and open counts as clauses. Both routes fetch the ready - issues' dependency targets alongside the blockers they already fetched. - Pinned-never-blocks itself is unchanged. +First stable release of the 1.3.1 patch line. It ships the same code as +[1.3.1-rc.2]; the only change since that candidate is the version stamp. The +full notes are in the [1.3.1-rc.2] and [1.3.1-rc.1] sections below, which +together cover everything since [1.3.0]. -- **`bd list` no longer silently drops all but the last repeated filter flag.** - `--status`, `--state`, and `--id` were plain string flags, so - `bd list --status open --status closed --status pinned` kept only `pinned` — - a census over 477 issues quietly answered over 3, with nothing in the output - to distinguish the narrowed answer from a correct one. Repeats of those three - flags now accumulate (duplicates collapse) instead of overwriting, so - `--status open --status closed` selects the same set as - `--status open,closed`. The one repeat that does not simply accumulate is one - involving `--status all`: `all` cannot be combined with other statuses, so - `--status all --status open` is now refused rather than narrowed to `open`. - `--type` and `--assignee` are single-valued all the way down — unioning would - fail type validation or exact-match nobody — so a repeat of either now refuses - loudly instead of silently keeping the last value: - `invalid argument "epic" for "-t, --type" flag: --type given more than once - (already "bug"); pass a single value`. - Single-flag and comma-form spellings behave exactly as before. +There is **no schema migration**: upgrading from 1.3.0, 1.3.1-rc.1 or +1.3.1-rc.2 is a binary swap. -- **`bd restore`, `bd admin compact`, `bd repo `, - `bd migrate [sync|hooks|schema]` and `bd preflight` no longer register a - local `--json` that shadows the root persistent flag.** pflag keeps a - command's own flag and drops the inherited one, so - `rootCmd.PersistentFlags().Changed("json")` stays false for that command; - [#6293](https://github.com/gastownhall/beads/pull/6293) compensated for that - per command by making the pre-run consult the subcommand's own `Changed` bit - before the root's. Deleting the eleven local registrations removes the - shadowing itself rather than working around it: `--json` moves to the Global - Flags section of those commands' `--help`, and a guard test now fails the - build if a command shadows the root flag again — closing the class before an - *unbound* local copy can reappear and invert the output mode it was meant to - select. +### Upgrade notes -- **`bd init --force` can no longer silently recreate a missing server-side - database as empty** (be-5up5). `--force` is an alias for `--reinit-local`, - which skips the existing-data guard entirely, and the reinit path's own typed - confirmation keys on the issue count — which is 0 in exactly the case where - the database is missing. So `bd init --force` against a lost database created - a fresh empty one at the same name, with no prompt and no destroy token: the - 2026-08-11 fleet-wide data-loss reflex, reached through the flag an operator - in a panic is most likely to try. The missing-database refusal now applies on - the reinit path too. `--force`/`--reinit-local` authorize destroying a - database that exists; only the explicit, per-invocation `--recreate-missing` - authorizes creating one where the configured database has gone missing. +- Coming from 1.3.0, read the upgrade notes in [1.3.1-rc.2] and the + **Changed** entries in [1.3.1-rc.1]. The rc.1 change most likely to affect + scripts is the new `bd dolt status --json` shape on a proxied workspace + ([#6580](https://github.com/gastownhall/beads/pull/6580)). +- Coming from 1.3.1-rc.2, nothing changes in behaviour. -- **`bd init --reinit-local` and `--force` no longer wave through a workspace - that predates `project_id`.** Their missing-database safety net returned - early whenever `.beads/metadata.json` carried no `project_id`, before it ever - probed for this project's local database directory — so a workspace - initialized before GH#2372 minted that field, whose local Dolt storage had - since been lost, was protected under plain `bd init` but not under the two - flags an operator reaches for in a panic. The probe now runs first and - unconditionally, and an absent `project_id` is allowed through only when - there is no local Dolt data directory either, matching the plain-`bd init` - guard. `--recreate-missing` still opens it. The signal decision behind both - guards is recorded in `engdocs/adr/0004-missing-database-guard-signal.md`. +## [1.3.1-rc.2] - 2026-09-29 -- **`bd init --recreate-missing` now reaches its own opt-in.** The existing-data - check derived "is this project's database here?" from the Dolt data - directory, which exists on every initialized workspace, so the - missing-database branch was unreachable and both `--recreate-missing` opt-ins - behind it were dead code. Four surfaces — the flag help, `bd help - init-safety`, `docs/recovery/init-safety.md`, and the guard's own refusal - text — told the operator to run `bd init --recreate-missing --prefix

`; - that command exited 1 with a false "already initialized" message and pointed - back at `bd init --reinit-local`, which is the destructive path, leaving the - documented recovery circling between two refusals. The check now probes - `//.dolt`, the same probe the reinit-path guard - uses. +Second release candidate for 1.3.1, still a patch line on top of 1.3.0 with +**no schema migration**: upgrading from 1.3.0 or 1.3.1-rc.1 is a binary swap. +It adds the proxied-server `bd backup` family and opt-in auto-backup on +managed-local workspaces, a `bd purge` that can run an orchestrator's wisp +retention sweep, and fixes for the #6716 fan-in stall, partial batch closes, +`bd sql` result sets, `bd show --watch`, `bd types`, `BEADS_DIR` discovery and +proxy liveness, plus eleven fixes from `main` ([#6653](https://github.com/gastownhall/beads/pull/6653)) and the smart-gate +data-behind stop ([#6659](https://github.com/gastownhall/beads/pull/6659), [#6695](https://github.com/gastownhall/beads/pull/6695)). Engineering notes for embedders +reading proxied-server surfaces are in `engdocs/` ([#6652](https://github.com/gastownhall/beads/pull/6652)). -- **The missing-database guard probes this project's own database directory** - (`//.dolt`) rather than the Dolt data directory that - merely contains it. The data directory always exists on an initialized - workspace — shared-server mode creates the machine-global one on resolution, - and per-project mode creates it at init — so probing it made the guard a - no-op in shared-server mode, which is the topology of the 2026-08-11 loss. - User-visible wherever the data directory exists but the database is gone. +### Upgrade notes -- **The missing-database refusal no longer prints a ready-to-paste - `bd init --recreate-missing` command line.** It names the flag and points - to `bd help init-safety`, which keeps the full invocation behind the - diagnosis it requires (ADR 0002 Invariant 4). The refusal also fires when - the server merely could not be reached, and a stopped server, a wrong port - or a wrong data dir all look the same from there; recreating in any of them - strands the real data behind an empty namesake. Run without `--prefix`, the - old line also printed `--prefix` with no value. The `--force` and - `--reinit-local` help, `bd help init-safety` and the recovery playbook now - say that neither flag authorizes recreating a missing server-mode database. +These behaviour changes can affect scripts; each is detailed below. -- **`BEADS_DOLT_POOL_READ_TIMEOUT` / `dolt.pool-read-timeout` (and the write - twins) now apply to every `bd` command in server mode.** The knobs shipped in - #5089, but their env/config ladder ran only for callers of `NewFromConfig*`; - the CLI's own store open hand-builds its config and goes straight to `New`, - so every `bd` command kept the built-in 10 s pool - deadline whatever the knob said — on a large shared server that is what made - `bd close` of a bead with dependents die in its recompute with `i/o timeout` - and no relief valve. The ladder now runs from the constructor every DoltStore - open shares — the CLI's store and library callers of - `New`/`NewFromConfig*`; `bd serve`'s HTTP data path builds its own - DSN without pool deadlines and is unchanged - ([#6144](https://github.com/gastownhall/beads/issues/6144)). Note for - operators of loaded servers: the documented precedence now reaches `bd - import` as well — it used to inherit the 5 m long-read fallback - unconditionally because the CLI's knob value was always 0, so a - `BEADS_DOLT_POOL_READ_TIMEOUT` set below that now bounds import too; size the - knob for your largest import, or leave it unset for the fallback. +- `bd show --watch ` exits 1 when the id cannot be found (it used to exit + 0), on both the direct and proxied routes. +- `bd sql` in direct server mode prints `OK` / `{"status":"ok"}` for a + multi-statement write or a write the parser cannot classify, instead of + `OK, N rows affected` / `{"rows_affected":N}`. `bd sql --readonly` refuses + statements it cannot parse (for example `PRAGMA`) instead of running them + as reads. +- A failure to read the custom issue types now fails `bd types`, + `bd create --graph` and `bd config set storage-class.*` instead of being + treated as "no custom types". +- `BEADS_DIR` must point at the `.beads` directory itself. A `BEADS_DIR` that + names a project root, or a `.beads` that does not exist yet, now fails with + "no beads database found" instead of silently resolving to the nearest + workspace above the current directory. +- `bd purge` always keeps closed beads a live bead depends on + (`live_dependent_skipped`); there is no flag to turn this off. +- `--older-than` on `bd purge` and `bd prune` is exact at hour precision: + `12h` no longer rounds up to a day, so `bd prune --older-than 12h` deletes + rows it used to keep. +- `bd close` with several ids exits non-zero when any of them fails to close. +- Unflagged piped `bd query` is no longer capped at 50 rows. +- On a proxied workspace with `dolt.auto-commit` set to `batch` or `off`, + writes are no longer committed to Dolt history one by one; run + `bd dolt commit` to flush them. +- `bd backup sync` fails after waiting 5 seconds if another backup of the same + workspace is running. +- `bd ready` / `bd list --ready` include issues in custom `active`-category + statuses, and a store open against an unreachable Dolt server on a + proxied-server workspace or under `bd serve` retries for up to ~40s before + failing. Both come from [#6653](https://github.com/gastownhall/beads/pull/6653); see **Changed**. -- **`BEADS_DOLT_MAX_CONNS` / `dolt.max-conns` now size the pool for ordinary - `bd` commands in server mode.** The pool-size knob (GH#3140) had the same - hole as the deadline knobs above: it was read only on the `NewFromConfig*` - path (routed stores, `bd doctor`, ...), so the CLI's own store open always - ran on the built-in 10-connection pool whatever the knob said. It now shares - the deadlines' ladder in the constructor every DoltStore open reaches - ([#7052](https://github.com/gastownhall/beads/pull/7052)). +### Added -- **No-DB commands in a redirected workspace no longer lose the source - repo's `dolt_database`** (be-xil, be-fyt). `bd doctor`, `bootstrap`, - `context`, `dolt`, `init` and `version` skip store init, and the beads dir - they select is resolved through `beads.FindBeadsDir()`, which follows a - `.beads/redirect` internally. The source repo's configured database name was - therefore gone before anything could capture it, so `bd doctor` fell through - to the shared target directory's own default database and reported - `Dolt Schema: wrong database` — offering a `--fix` that would have repointed - the rig at an unrelated rig's store. The redirect source is now captured - before that resolution, matching the store-requiring path. -- **An explicit `--db`, `BEADS_DB` or `BD_DB` target is no longer shadowed by - the ambient repo's redirect-source database** (be-fyt). `beads.GetRedirectInfo()` - deliberately resolves from the CWD repo's local `.beads` regardless of - `--db`/`BEADS_DIR` (bd-wayc3), so when the ambient repo itself had an active - redirect, the preservation above re-opened the same "wrong database" failure - through a narrower door. Preservation is now skipped whenever the caller - named an explicit target, asking the same question `selectedNoDBBeadsDir` - asks so the two cannot drift apart. Note that a `--db` value naming a - *database* rather than a path is not an explicit *directory* target: it is - consumed only on the store-requiring path, the ambient workspace is still - what gets selected, and its source database is still preserved. +- **`bd backup` works on a proxied-server workspace bd runs the Dolt server + for** ([#6584](https://github.com/gastownhall/beads/pull/6584)). `bd backup init`, `sync`, `remove`, `status` and `restore` are routed + over the proxied provider; before this, a proxied workspace — the default + topology since 1.3.0 — had no backup path at all. `bd backup restore` stops + the proxy and its Dolt child before replacing the database and leaves the + workspace quiescent, so the next command relaunches against the restored + data. -- **An ambient `BEADS_DOLT_SERVER_PORT` now marks a workspace externally - managed — suppressing auto-start — and no longer stops bd reaping its own - orphaned server** - ([#5934](https://github.com/gastownhall/beads/pull/5934)). Setting - `BEADS_DOLT_SERVER_PORT` (or the legacy `BEADS_DOLT_PORT`) now makes - `ResolveServerMode` classify a workspace as an externally-managed server, so - bd stops trying to own a lifecycle it does not own. The stale-server cleanup - path is deliberately carved out of that rule: it keeps resolving the mode - without the port var, so bd still reaps a same-repo orphan it started - (GH#2430) instead of declining because the environment named a port. A - `proxied-server` workspace is exempt from the new rule entirely — it reaches - its server through the proxy, so an ambient port does not describe its - lifecycle. + The family stays **refused by design** on a proxied workspace pointed at a + Dolt server bd does not own (an external host, socket, or a beads-team-server + database): `CALL DOLT_BACKUP('add', …)` registers the backup remote on the + *server*, where it is global to every client connected to it, so one + workspace's backup decision would silently become everyone's — the shape + `bd backup`'s own help text already warns about for auto-backup. A `file://` + destination has a second problem on top, since the server resolves that path + on its own filesystem. The refusal keeps the `proxy.backup.unsupported` code + and now carries `"reason": "design"`; a backup story for those deployments + belongs to whoever runs the server. Whether a *remote-scheme* destination + (DoltHub, `aws://`, `gs://`) should eventually be allowed there is recorded as + an open question rather than settled — the server pushes those over the + network from its own environment, which is the same credential question the + next slice has to answer for `dolt push`. -- **`bd` caps how large the auto-backup destination can grow, instead of - growing it forever** ([#6071](https://github.com/gastownhall/beads/pull/6071)). - `CALL DOLT_BACKUP('sync', ...)` only ever adds new chunks to the - destination — it never prunes ones that became unreachable on the source - (history rewrites, superseded data) — and Dolt exposes no supported way to - GC a backup destination in place. Left uncapped, the destination could - only grow until disk filled; this is the root cause of the 2026-06-19 - outage, where a 1.7GB store produced a 43GB backup directory. Auto-backup - now pauses (nothing is deleted) once the destination reaches - `backup.size-cap-mb` (default 2048MB); set it to `0` to disable the cap - entirely. The pause is no longer stderr-only: `bd backup status` and its - `--json` output now report a `size_cap` object (`enabled`, `cap_mb`, - `current_bytes`, `exceeded`), so an agent/CI caller relying on `--json` or - `--quiet` can see that auto-backup has stopped instead of reading a - reassuring "Last backup" line while nothing further syncs. The remediation - advice no longer suggests deleting the backup directory — nothing - guarantees a deleted destination is cleanly recreated by the next sync, and - the server-side backup remote stays registered against that path; it now - points at raising `backup.size-cap-mb` or pointing `backup.git-repo` at a - different git repository instead — not `bd backup init`, which configures - manual `bd backup sync`'s destination and leaves auto-backup paused. The - size-cap check itself runs after both the interval throttle and change - detection, so it costs nothing on any path that is not about to sync — an - idle workspace never reaches it at all, and a paused destination re-arms - the interval throttle on the skip, so it is measured at most once per - `backup.interval` rather than on every command. `backup.size-warn-interval` - (default 24h) controls how often the pause is re-announced. Manual `bd - backup` / `bd backup sync` are not capped. + `bd backup status` and `bd backup` help no longer attribute auto-backup being + OFF on a server topology to a missing git remote, which was never the reason + there ([#6694](https://github.com/gastownhall/beads/pull/6694)). Auto-backup + itself is opt-in on this topology; see the next entry. -- **`bd context` resolves a workspace outside a git repository instead of - hard-failing** ([#4772](https://github.com/gastownhall/beads/issues/4772)). - The command's own help promises it "does not require the database to be open, - making it useful for diagnostics in degraded states", but it exited with - `cannot determine repository root` whenever the working directory was not - inside a git repo — even with a perfectly valid `.beads/` present — so - consumers using `bd context --json` as a health probe got a false - "unreachable" in healthy non-git scopes. +- **Auto-backup runs on a managed-local proxied-server workspace** + ([#6879](https://github.com/gastownhall/beads/pull/6879)). The + proxied arm of the post-command hook now calls auto-backup, so an explicit + `backup.enabled=true` (or `BD_BACKUP_ENABLED=1`) takes the same throttled, + change-detected Dolt-native backup into `.beads/backup` it takes in direct + mode, through the proxied provider's non-transactional seam. Before this the + opt-in was inert on every proxied workspace and `bd backup status` said so. + The default stays OFF, as on every server-mode workspace: many bd clients + share one server. It is honored exactly where `bd backup sync` is — a + proxied server bd started itself — and stays inert on an external or + team-server topology, strict `--readonly`, `bd serve`, a preview + (`--dry-run`/`--inspect`) and a migration freeze. A command that opened no + provider (e.g. `bd dolt stop`) backs nothing up rather than relaunching the + server to do so. `bd backup status` now reports the proxied default as + `auto: off in proxied-server mode; set backup.enabled=true to opt in`. - `GetRepoContext()` finds and boundary-validates `.beads/` *before* it asks - git for a root, so that one failure means only that git is missing. It now - returns a typed `*beads.NoRepoRootError`, and `GetRepoContextAllowingNoGit()` - selects it with `errors.As` and roots the context at the `.beads` parent. - Typed rather than by message text on purpose: the SEC-003 unsafe-location - rejection embeds the offending path verbatim, so a substring test would let a - workspace under a hostile path clear its own security refusal. Every other - failure, that one included, propagates untouched. +- **Auto-backup and `bd backup sync` no longer overlap.** A per-workspace + backup lock (`.beads/backup.lock`) serializes them in every mode: + auto-backup skips when another backup holds it, and `bd backup sync` waits + up to 5 seconds (the bound restore uses for the workspace gate) and then + fails with "another backup is running for this workspace". - Scoped to the callers that only read config: `bd context` on both routes, and - the `domain/fs` context provider — which `bd serve` also resolves through, so - `bd serve` in a non-git directory with a valid `.beads/` now proceeds rather - than failing at context resolution. Anything that runs git commands still - uses `GetRepoContext()`. The synthesized context roots at the repository - containing the `.beads/` (falling back to its parent when git cannot be asked - there either) and decides `is_redirected` positionally — whether discovery - standing in the working directory would have found that `.beads/` — so - `--db`, `BEADS_DB`/`BD_DB` and `-C` are reported the same way `BEADS_DIR` is. - `bd context` also prints `cwd repo: git: unavailable` rather than silently - omitting the one degraded state it exists to report. +- **`bd purge` covers an orchestrator's wisp retention sweep** + ([#6883](https://github.com/gastownhall/beads/pull/6883)). Three changes + that together let `bd purge --wisps-plane --older-than 168h --force` replace + a raw `DELETE FROM wisps` retention step: + - **Live-dependent protection, always on.** `bd purge` no longer deletes a + closed bead that a live bead depends on through a `parent-child`, + `tracks` or `blocks` edge — a closed molecule root whose step is still + open, a closed wisp a live convoy tracks, a closed blocker of live work. + "Live" is any status that is not done, including custom statuses. The + held-back count is reported as `live_dependent_skipped` in `--json` (and + on its own line in text output). The protection is on the role + (`issueops.SweepRequest.ProtectLiveDependents`); `bd prune` does not ask + for it. + - **`--wisps-plane`** selects every closed row stored in the wisps table, + including `--no-history` beads, which the default ephemeral selection + leaves to `bd prune`. It works on embedded, server and proxied + workspaces, and because it reaches durable-tier rows it requires + `--older-than` or `--pattern`, like `bd prune` + (`issueops.SweepWispsPlane`). + - **`--older-than` accepts hour and finer durations** (`36h`, `168h`, + `90m`, down to `1s`) on both `bd purge` and `bd prune`. Day values (`7`, + `7d`, `2w`) are unchanged. An `Nh` value used to be converted to whole + days: floored above a day (`36h` swept rows only 24 hours old) and + rounded UP to one day below it (`12h` kept everything younger than 24 + hours). Both are now taken exactly, so `bd prune --older-than 12h` now + deletes rows closed 12–24 hours ago that it used to keep. A value too + large to represent (e.g. `213504d`) is now refused instead of silently + wrapping to a tiny age. + - **`--limit N`** caps one `bd purge` run at N beads, oldest-closed first + (`issueops.SweepRequest.Limit`), so a large backlog drains in bounded + transactions; `--json` then adds `remaining` and `has_more`. Loop while + `has_more` is true. -- **Server-mode `bd federation sync` and `bd federation status` work with Dolt - remotes that have no stored peer credentials** - ([#5128](https://github.com/gastownhall/beads/pull/5128), fixes - [#4837](https://github.com/gastownhall/beads/issues/4837)). A remote added - by `bd federation add-peer` without `--user` or by `bd dolt remote add` has - no `federation_peers` row, and every server-mode `bd federation` push, pull - and fetch to it failed at the credential lookup with `failed to get peer - credentials: not found: federation peer `. A missing row now means no - stored credentials, as it already did in embedded mode, so the operation - runs with Dolt's own credential discovery: `bd federation sync` without - `--peer` syncs those remotes too, and `status` fetches them. Errors reading - or decrypting a stored row still fail. A mistyped `--peer` now fails with - Dolt's own unknown-remote error instead. +### Changed -- **`bd comment`/`bd comments add` refuse an abbreviated id and a reserved - word instead of silently resolving one to the wrong issue** - ([#5393](https://github.com/gastownhall/beads/pull/5393)). `bd comment list - `, a typo for `bd comments list`, used to parse as id="list" with no - guard on the id slot — and since no issue is ever literally named "list", - the default abbreviation-tolerant resolver would fuzzy-match it against - whatever existing bead or wisp id happened to start with "list" and write - the rest of the command line there as a comment. Confirmed in production: - 15+ automated sessions made this exact typo over two days and all landed on - the same unrelated wisp. `bd comment`'s id positional now (a) refuses the - four words a `bd comments ` typo is likely to produce — `list`, `add`, - `rm`, `delete` — before any id resolution is attempted, on both the - embedded and proxied-server paths, and (b) requires an EXACT id match - (`bd-a1b2c3d4`, not `a1b2`) rather than the abbreviation-tolerant matching - every other command keeps. The refusal message is truthful about which - case applies: a reserved word gets a hint toward `bd comments`; an - abbreviation of a REAL issue gets `id abbreviations are not accepted on - comment writes; use the full id from \`bd show \`` rather than the - generic (and in that case false) "no issue found matching" text a genuine - not-found gets. See [Working with - IDs](docs/core-concepts/hash-ids.md#working-with-ids) for the full - read/write distinction. The proxied-server comment path was investigated - separately and found to already be exact-id-only by construction (its - resolution never performed abbreviation matching in the first place) — a - regression test now locks that in rather than leaving it undocumented. +- **Proxied-server refusals now say *why* they refuse** ([#6582](https://github.com/gastownhall/beads/pull/6582)). The JSON a refused + command prints gains a `reason` field next to the existing `code`, `error` + and `mutates`: `design` for a refusal that is expected to stay (shared + history, multi-repo routing, destructive admin, strict `--readonly`) and + `unimplemented` for a capability gap with a named owner. Every existing code, + message and exit status is unchanged, and `reason` is absent on refusals that + report a runtime state rather than a policy, so existing consumers are + unaffected. The policy behind it moved into one registry + (`cmd/bd/capability_registry.go`) that every command must appear in. +- **A command with no proxied-server route now fails with a typed error** + (`proxy.store.unrouted`) instead of the bare string `proxy server store + should be uow provider`. -- **`bd delete` no longer resolves an id it cannot find to a different issue - whose id begins with it.** - ([#7375](https://github.com/gastownhall/beads/pull/7375)). The direct - (embedded and server) route resolved - each id with the abbreviation-tolerant resolver, so an id that was already - gone (a retried delete, a re-run `--from-file` list, a parent whose child - survives) resolved by leading-prefix match to whichever surviving issue's id - it begins: after `fx-t00003` was deleted, `bd delete fx-t00003` previewed - `fx-t000031` and printed `bd delete fx-t000031 --force` as the next step, - and with `--force` deleted it. `--quiet` silenced the partial-id notice, so - a `--force --quiet` re-run deleted issues it never named without a word. - `bd delete` now requires an exact id on the preview and with `--force`, as - `bd comment` does and as the team-server route already did; full ids, bare - full hashes, child ids and routed ids resolve as before. An abbreviation is - refused with `issue "t000" not found: bd delete needs the full issue id, and - "t000" is only the start of fx-t000031`, naming every id it matches; under - `--if-revision` it is the guard's lost race instead (`precondition_failed`, - exit 13), as for any other gone id. This is a user-visible change: a script - or habit that deletes by abbreviation now gets that error and must pass the - full id. Reads and other writes keep accepting abbreviations; see [Working - with IDs](docs/core-concepts/hash-ids.md#working-with-ids). +- **`bd backup restore --json` prints a result object on every topology** + (`{"restored": true, "source": "

"}`). It previously printed nothing at + all, which left a caller unable to tell a completed restore from a silently + skipped one. This applies to direct/embedded workspaces too, not only proxied + ones. +- **`bd backup init`, `sync`, `remove` and `restore` no longer print a usage + block after a failure.** They now silence cobra's own error and usage + rendering like the rest of the CLI, so a failure is one error line on stderr + instead of the message followed by `Error: exit code 1` and the full usage + text. Exit statuses are unchanged; this applies on every topology. -- **A git hook beads never wrote is no longer reported as installed** - ([#6084](https://github.com/gastownhall/beads/issues/6084)). `getHookVersion` - returns no error for a readable hook file that carries no beads markers, so a - hand-written `pre-commit` — one beads does not own — was printed as - `✓ installed (version )` by `bd hooks list`, counted as installed by - `bd info`, and reported as no drift by `bd config drift`. Those surfaces now - report such a file as not installed. This is a user-visible change to a - scripted contract: in a repository whose hooks beads does not own, - `bd config drift` now exits 1 (drift detected) where it exited 0, so - automation keyed on that exit code should re-check those repositories before - upgrading. A hook that calls `bd hooks run ` still counts as installed - even with no beads marker — that is the integration beads prescribes for - external hook managers such as lefthook and husky (GH#946) — and it is - version-agnostic, so it is never reported outdated and never prompts - `bd hooks install`, which would overwrite the manager's own hook. - `bd hooks list`, `bd info` and `bd config drift` now use the same predicate as - `bd doctor` for deciding whether a hook file is a beads hook; `bd doctor`'s own - installed/missing check still only tests for file existence and is tracked - separately. +- **`bd purge` keeps closed beads a live bead depends on, always** + ([#6883](https://github.com/gastownhall/beads/pull/6883)). A closed bead that a not-done bead depends on through + `parent-child`, `tracks` or `blocks` is no longer purged (reported as + `live_dependent_skipped`), so a purge that used to delete a closed molecule + root under a live step now leaves it. There is no flag to turn this off. + See the `--wisps-plane` entry under Added. -- **`bd close` verifies a bead gate in proxied-server mode.** The close - pre-check built its bead-gate lookup on the local store, which the - proxied-server route never opens, so closing a bead gate there refused with - `no local store available` even after the awaited bead had closed, while - `bd gate check` resolved the same gate. The close pre-check now reads the - awaited bead through the same fresh-read path `bd gate check` uses on that - route; the direct and embedded routes are unchanged - ([#5861](https://github.com/gastownhall/beads/issues/5861) item 1). +- **`--older-than` on `bd purge` and `bd prune` is taken at hour precision** + ([#6883](https://github.com/gastownhall/beads/pull/6883)). An `Nh` value is no longer rounded to whole days: `12h` used to + round UP to one day and now means 12 hours, so `bd prune --older-than 12h` + deletes rows closed 12–24 hours ago that it used to keep; `36h` used to floor + to one day and now means 36 hours. Day values (`7`, `7d`, `2w`) are + unchanged. See the `bd purge` entry under Added. -- **`bd recall`, `bd forget`, `bd remember `, - `GET /v0/beads/memories/{key}` and `DELETE /v0/beads/memories/{key}` now - treat an empty-valued memory as present** - ([#5964](https://github.com/gastownhall/beads/pull/5964), fixes - [#5963](https://github.com/gastownhall/beads/issues/5963)). Presence used to - be derived from the stored value's truthiness, so a memory written out of - band with an empty string was invisible even though its row existed. `bd - recall` printed "No memory with key" and exited 1, `bd forget` refused to - delete it, `bd remember ` refused to recall it, and both HTTP - doors answered 404 (the DELETE leaving the row in place), while - `GET /v0/beads/memories` already enumerated the same row. Presence now comes from row existence on direct Dolt, embedded Dolt and - the proxied backend. An empty-valued memory is found, recalls as an empty - string, and is deleted on the first forget. Only a key with no row is a - miss. +- **`dolt.auto-commit=batch` and `off` now take effect on a proxied-server + workspace, and `bd dolt commit` works there** ([#6499](https://github.com/gastownhall/beads/pull/6499), + [#4995](https://github.com/gastownhall/beads/issues/4995)). Every proxied write used to mint a Dolt history commit + whatever `dolt.auto-commit` said, and `bd dolt commit` was refused with + `proxy.dolt_commit.unsupported`. Under `batch`/`off` a proxied write now + lands in the server's working set without advancing Dolt history, and + `bd dolt commit` is the flush point, as on the direct route. Commands that + are explicit commit points (`bd batch`, `bd mol bond`/`pour`/`squash`, + `bd mol wisp create`, and the wisp half of `bd mol burn`) still commit, and + `bd serve` writes still commit per write. `on`, the default, is unchanged. + **Behaviour change:** a proxied workspace that set `batch` or `off` and was + getting per-write commits anyway now accumulates uncommitted changes until + `bd dolt commit`, and `dolt log`, `dolt push` and backups do not see them + before that. `bd vc commit`'s proxied refusal now points at `bd dolt commit`. -- **`bd doctor` no longer flags a `.local_version` that starts with `v`.** The - canonical spelling of a Go module version — and the string a build stamped - from a Go pseudo-version reports and writes into `.local_version` itself — - was rejected by the Version Tracking check (`Invalid version format`), and - read as major 0 in version comparisons, because the helpers parsed digits - first. They now accept an optional leading `v`; versions without it are - unchanged ([#6152](https://github.com/gastownhall/beads/issues/6152)). +- **`bd ready` and `bd list --ready` now return issues whose custom status is in + the `active` category** ([#5918](https://github.com/gastownhall/beads/pull/5918), + [#5831](https://github.com/gastownhall/beads/issues/5831)). The ready query + treated its `open` pin as an exact match, so a workspace that defined, say, + `triaged:active` never saw those issues in ready work — even though + `bd statuses`, the `ready_issues` view, and claim eligibility all already + counted that category as ready. **The ready set grows on upgrade** for any + workspace using custom active statuses: counts and dashboards will read higher, + and `bd ready` can hand out issues it used to withhold. Custom + `wip`/`done`/`frozen`/unspecified statuses stay out, and `in_progress` stays + out, both unchanged. -- **`bd serve`'s blocked-close refusal names the blockers.** A `409 - not_closable` for a live blocker — on `issues/{id}:close`, `issues:batchClose` - per-item outcomes, `PATCH issues/{id}` into a done status, and - `issues:batchApply` — said only "issue is blocked", so an HTTP client could - not tell the user what held the close, while `bd close` on the direct and - proxied routes prints `cannot close blocked issue: X is blocked by [Y]`. The - refusal now carries a `blockers` extension member (`[{id, kind, type}]`, - `kind` `local` or `external`, `type` the blocking edge type when known) and a - `detail` that opens with the direct route's sentence. The code, the status - and the `open_children` discriminator are unchanged. The list travels typed - end to end: close refusals are now an `issueops.BlockedError` (still matching - `ErrCloseBlocked`, with a byte-identical message), so the server never parses - prose to build the member and a client can rebuild the same typed error. +- **`bd list --wisp-type ` with no plane admitted is now a refusal instead of + an empty listing** ([#6098](https://github.com/gastownhall/beads/pull/6098)). + `--wisp-type` narrows an admitted plane rather than admitting one, so alone it + could not match any row and exited 0 with an empty result for every input. It + now fails with a hint naming `--include-ephemeral`. A script that ran it and + read the empty output as "no such wisps" now sees an error — which is the + point: the old success answered a question it had not asked. The refusal is + CLI-only; the equivalent API request stays lawful and still composes to an + empty page. -- **The #6716 fan-in stall is fixed on the proxied-server route and the - remaining store routes.** Two blockers of one dependent taken away at the - same time (parallel workers closing both, or a close racing a - `bd dep remove` or a delete of the other) could leave the dependent - `is_blocked=1` and missing from `bd ready` until `bd recompute-blocked` - ([#6716](https://github.com/gastownhall/beads/issues/6716)). The post-commit - recheck the Dolt store write transactions gained for that issue now also - runs on every write the proxied-server (uow/domain-db) route serves, which - under `--proxied-server` — the default topology — is `bd close` (single - and batch), `bd update --status`, `bd dep remove`, `bd delete`, `bd batch` - and `bd serve`; on `RunInTransaction` (`bd batch` direct, `bd cook`, - `bd mol squash`/`burn`, and SDK callers that close or update inside a - transaction); and on the wisp close, update and delete writers and the - legacy dependency removal behind `bd duplicates --merge`. Demote-to-wisp - moves a row between planes without taking a blocker away and is unchanged. +- **The `--ready` footer names the status pin that actually applied** + ([#5924](https://github.com/gastownhall/beads/pull/5924)). With no `--status` + (or `--status all`) the line is unchanged: `open only — --ready excludes + in_progress`. With an explicit `--status` it now reads ` only` + instead of reusing the default-open sentence, which is no longer true once the + selector is honored (see **Fixed**). Anything scraping that parenthetical for + a `--status`-plus-`--ready` run sees new text — though that combination also + returned the wrong rows before this release. Note that the default sentence now + understates the set, since custom active statuses join it (above); the footer + and `--ready` help text are corrected upstream on main + ([#6187](https://github.com/gastownhall/beads/issues/6187), + [#6188](https://github.com/gastownhall/beads/issues/6188)). + +- **`bd doctor` reports a new missing gitignore pattern on every workspace + created before this release, until `bd doctor --fix` is run** + ([#5978](https://github.com/gastownhall/beads/pull/5978)). + `.beads/dolt-server-config.yaml` joined the required `.beads/.gitignore` + patterns (see **Fixed**), so a workspace that was clean a moment ago now + warns. `bd doctor --fix` appends the rule and silences it; workspaces + initialised after this release get it from the template and never warn. + +- **Opening a store against an unreachable Dolt server now takes up to ~40s to + fail instead of failing at once — on proxied-server workspaces, and under + `bd serve`** ([#6003](https://github.com/gastownhall/beads/pull/6003)). The + bootstrap ping is retried for up to 30 seconds, each attempt capped at 10s, so + a server still coming up is waited out rather than turned into a hard failure + (see **Fixed**). The cost is that a genuinely dead or misconfigured endpoint no + longer errors immediately: an interactive command against a stopped server, and + any script or CI step that counted on a fast failure, now waits out the retry + budget first. Durable rejections — bad credentials, unknown database, a + hostname that does not resolve — are still reported immediately without + retrying. + + Only the paths that build a unit-of-work provider take that ping, so this is a + minority of topologies: every command in a proxied-server workspace, local or + external, and `bd serve` in a server, external-server or shared-server + workspace, which builds its own provider through the same funnel. **Embedded — + the default — is unaffected entirely**; `bd serve` refuses it outright, and no + embedded command can slow down here. An ordinary CLI command in a server or + shared-server workspace is also unaffected: it opens a Dolt store, which does + not take this ping. Only that mode's `bd serve` startup does. + + The 30s figure is the retry budget, not the wall-clock bound. It stops + *scheduling* attempts at 30s but cannot cancel one already in flight, and each + attempt carries its own 10s cap, so ~40s is the honest worst case for a single + open. A provider open pings twice — once for the schema-init connection, once + for the database pool — each with a fresh budget, so a backend that satisfies + the first ping and the schema init and is then unreachable for the second can + push one command to ~80s. That ordering is narrow, but it is reachable. + +- **A backup sync that would leave the backup's manifest ahead of its chunk + files now fails at that point instead of corrupting the backup quietly** + ([#5931](https://github.com/gastownhall/beads/pull/5931), + [#4070](https://github.com/gastownhall/beads/issues/4070)). The Dolt bump + brings upstream's defense, which rejects a manifest update referencing a + missing table file. Previously that update was accepted and only some later + sync failed, confusingly, with `table file not found` — and stayed broken. A + sync that used to appear to succeed can now report a failure; that failure is + the corruption being caught at the moment it happens. This is a mitigation, + not a root-cause fix: backup file-and-manifest publish is still non-atomic, + and **backups already in the manifest-ahead state are not repaired** by + upgrading — they surface as a failing sync and have to be re-seeded. + +### Fixed - **An explicit `BEADS_DIR` is authoritative during workspace discovery.** When `BEADS_DIR` named a directory without project files yet (missing, @@ -1846,6 +423,48 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 `proxy_pid` still set), including for a stranded proxy started by an older bd. ([#6937](https://github.com/gastownhall/beads/pull/6937)) +- **`bd show --watch` works under `--proxied-server`.** It was refused with + `proxy.watch.unsupported` although the provider can answer the poll the way + `bd list --watch` already does, so every proxied workspace — the default + transport for managed-local setups — had no way to watch a bead. The proxied + route now shares the direct route's loop: render once, re-read every 2s, + redraw only when the issue's status or `updated_at` changes, stop cleanly on + Ctrl+C/SIGTERM, and require exactly one id. Each poll opens its own short + unit of work, so a long watch never pins a transaction, and a poll that + fails (the issue was deleted, the backend blipped) keeps the last render + without printing, as on the direct route. On both routes a watch whose id + cannot be found now exits 1 instead of 0 ([#6933](https://github.com/gastownhall/beads/pull/6933)). + +- **A server-mode workspace with an empty `.beads/dolt` is no longer refused + as legacy** + ([#5682](https://github.com/gastownhall/beads/issues/5682), + [#6935](https://github.com/gastownhall/beads/pull/6935)). bd creates `.beads/dolt` on use even when the data lives on an + external Dolt server, and leaves it empty. When the gitignored + `.local_version` witness was missing — after a fresh checkout, or when a + provisioner created the empty root before `bd init --server --external` — + the legacy-upgrade guard refused every command, `bd init` included, as a + "legacy Dolt server workspace", and no bd command could repair it. An empty + root holds nothing a legacy release could have left, so the guard now admits + it: `bd init` writes the witness, and an existing workspace in that state + re-seeds it on the next command. A pre-1.0 witness, or a `.beads/dolt` that + holds anything, is still refused. + +- **`bd types` now lists exactly the types `bd create --type` accepts** + ([#6934](https://github.com/gastownhall/beads/pull/6934)). The two resolved + the type set through different code and could disagree: a server-mode + workspace's `bd types` dropped `.beads/config.yaml` `types.custom` entries + once the database had any custom type, although create accepts them; in + proxied mode `bd config set storage-class.` checked `config.yaml` alone + and refused custom types registered in the database; and the built-in system + types `message`, `molecule`, `gate` and `event` were never listed. Every + mode now composes custom types by one rule (the `custom_types` table, else + the `types.custom` config row, always unioned with `config.yaml`). `bd types` + gains a "System types" section, and `bd types --json` an additive + `system_types` field; `core_types` and `custom_types` are unchanged. A + failure to read the custom types now fails `bd types`, `bd create --graph` + and `bd config set storage-class.*` instead of being silently treated as "no + custom types". + - **`bd sql` no longer drops the rows of CTE queries or CALL result sets** ([#6932](https://github.com/gastownhall/beads/pull/6932)). In proxied-server mode, `WITH name(cols) AS (...) SELECT ...` and `WITH RECURSIVE ...` queries @@ -1864,166 +483,25 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 statements it cannot parse (for example `PRAGMA`) instead of treating them as reads. -- **`bd types` now lists exactly the types `bd create --type` accepts** - ([#6934](https://github.com/gastownhall/beads/pull/6934)). The two resolved - the type set through different code and could disagree: a server-mode - workspace's `bd types` dropped `.beads/config.yaml` `types.custom` entries - once the database had any custom type, although create accepts them; in - proxied mode `bd config set storage-class.` checked `config.yaml` alone - and refused custom types registered in the database; and the built-in system - types `message`, `molecule`, `gate` and `event` were never listed. Every - mode now composes custom types by one rule (the `custom_types` table, else - the `types.custom` config row, always unioned with `config.yaml`). `bd types` - gains a "System types" section, and `bd types --json` an additive - `system_types` field; `core_types` and `custom_types` are unchanged. A - failure to read the custom types now fails `bd types`, `bd create --graph` - and `bd config set storage-class.*` instead of being silently treated as "no - custom types". - -- **`bd` refuses a bare tooling sentinel (`null`, `undefined`, an empty string) - as an issue ID** ([#6214](https://github.com/gastownhall/beads/issues/6214)). - `jq -r` prints the literal `null` when a selector misses and JS tooling prints - `undefined`, so `bd update "$(bd list --json | jq -r '.[0].id')" --status - closed` arrived at ID resolution as a plain four-character token. Those tokens - are a valid partial-ID shape, so they reached the leading-prefix abbreviation - branch and resolved to whichever issue's hash began with them — closing an - issue the caller never named and exiting 0 with a success line. `bd update - null`, `bd comment null` and every other command that resolves an ID now fail - before any lookup, because the guard sits in the shared resolver. An ID that - merely contains a token (`bd-null`, `null3t0`, `undefined-behavior`) still - resolves, and abbreviation matching is otherwise unchanged. The empty string - already failed; it now says why. - -- **Table-rebuild `__temp__` intermediates can no longer materialize as - tracked tables on `@@dolt_transaction_commit=1` servers.** The - ignored-series rebuilds stage every clone-local table through a - `__temp__` rename, but the intermediate names carried no - `dolt_ignore` pattern: against a transaction-commit server each - `CREATE TABLE __temp__X` auto-committed a real table at HEAD, and the - rename onto the ignored final name left an unstageable `__temp__X -> X` - half in `dolt_status` that wedged every later open behind the dirty-table - guard (observed in the field as a three-city shared hub wedging twice in - one day, each time ending in fleet-wide write refusal and a manual SQL - repair). `__temp__%` now ships in the canonical seeded pattern set — - asserted, like the events-journal patterns, before the series that creates - the tables — so intermediates are born ignored. The scoped seed commit also - gains `--skip-empty`: under `@@dolt_transaction_commit=1` the server has - already committed the seeded rows at their own transaction boundaries, and - the previously unconditional `DOLT_COMMIT` died with "nothing to commit", - killing the pass on exactly the under-seeded stores the heal targets. - Fenced deployments: adding a canonical pattern un-converges `dolt_ignore` for - every existing database, so the next write-mode open issues one - `INSERT IGNORE INTO dolt_ignore`; a hosted or box-fenced wire client that is - denied that INSERT fails its open until one privileged `bd` opens the store - and heals the pattern — self-healing, one privileged open per upgrade - (degrading a denied seed to a skip is tracked separately). - -- **`bd ready --parent` and `bd blocked --parent` no longer re-scan the whole - parent-child edge relation for every descendant they find.** The transitive - descendant walk recursed against a materialized `parent_edges` CTE that Dolt - cannot index through, so its cost was (parent-child rows) × (descendants): - 7.5 s for a 483-descendant parent on a 4.6k-edge database, and past the - shared-pool read deadline on a busy server. The walk now recurses directly - off `dependencies` / `wisp_dependencies` through their typed target indexes - and returns the same rows in a fraction of the time - ([#6128](https://github.com/gastownhall/beads/issues/6128)). - -- **`bd list --parent` on a proxied-server workspace no longer drops a child - whose prefix differs from its parent's** - ([#6130](https://github.com/gastownhall/beads/pull/6130)). The edge to such - a parent is stored in `depends_on_external` — `issueops.IsExternalDepTarget` - routes every cross-prefix target there — but the descendant walk behind the - proxied tree view resolved a parent only through the issue and wisp target - columns, so the child and its subtree were missing from the tree and from - `--watch`, while the direct route listed them. The walk now reads all three - target columns, as the direct route's `--parent` filter does. Nothing listed - before can drop out: the added column is read only when the other two are - both empty. - -- **`notion.token` is kept out of the Dolt database** - ([#6676](https://github.com/gastownhall/beads/issues/6676)). It was missing - from the yaml-only key list that holds the other tracker secrets, so - `bd config set notion.token` — the command `bd notion status` recommends — - wrote it to the database, whose contents `bd dolt push` sends to the remote. - It is now written to `config.yaml` like `github.token`, `gitlab.token` and - the other tracker secrets, and refused on a git-tracked `config.yaml` with a - pointer to `NOTION_TOKEN`. A token an older bd already stored in the database - is still read, after `config.yaml` and before `NOTION_TOKEN`, so existing - setups keep authenticating. `bd config unset notion.token` removes both - copies — the `config.yaml` entry and any row an older bd left in the database - — and reports which of them it actually removed; that cleanup covers every - yaml-only secret key, not just this one. The database half is best effort and - says so: it is skipped only where reaching the row would mean creating a - database (the command never creates one), so it still runs for a server-mode - or proxied workspace whose database is simply not on local disk, and it is - attempted even when the `config.yaml` edit fails, so a workspace whose only - copy is the stored row still has a remover. `bd notion status` now reports the - auth source as `database_legacy` rather than `config_token` while the stored - row is the one authenticating, so an affected workspace can identify itself. - A token that was already pushed should still be rotated: deleting the row - locally does not unpublish it from remotes that already have a copy. - -- **A server-mode workspace with an empty `.beads/dolt` is no longer refused - as legacy** - ([#5682](https://github.com/gastownhall/beads/issues/5682), - [#6935](https://github.com/gastownhall/beads/pull/6935)). bd creates `.beads/dolt` on use even when the data lives on an - external Dolt server, and leaves it empty. When the gitignored - `.local_version` witness was missing — after a fresh checkout, or when a - provisioner created the empty root before `bd init --server --external` — - the legacy-upgrade guard refused every command, `bd init` included, as a - "legacy Dolt server workspace", and no bd command could repair it. An empty - root holds nothing a legacy release could have left, so the guard now admits - it: `bd init` writes the witness, and an existing workspace in that state - re-seeds it on the next command. A pre-1.0 witness, or a `.beads/dolt` that - holds anything, is still refused. - -- **`bd -C dir prime` now describes the target workspace instead of the launch - directory** ([#5509](https://github.com/gastownhall/beads/issues/5509)). `-C` - resolves `BEADS_DIR` but never changes directory, so prime's cwd-relative - surfaces still described wherever the command was launched: the clone-local - `.beads/PRIME.md` tier, the AGENTS.md/CLAUDE.md divergence reminder, the git - upstream/remote probes behind the template's git-authority wording, and the - redirect notice. All four now resolve against the `-C` target, matching - `cd dir && bd prime`; without `-C` the behavior is byte-for-byte unchanged. - - Note for `-C` users: the git-remote probe prime shares with auto-backup - (`isBackupAutoEnabled`) follows the same change, so under - `bd -C dir ` auto-backup enablement and the `bd backup status` - note are now decided by the `-C` target's git remote rather than the launch - directory's. That makes the probe agree with the store actually being backed - up, but it can flip auto-backup on or off for `-C` invocations whose launch - directory and target differ in remote configuration. - -- **`bd show --watch` works under `--proxied-server`.** It was refused with - `proxy.watch.unsupported` although the provider can answer the poll the way - `bd list --watch` already does, so every proxied workspace — the default - transport for managed-local setups — had no way to watch a bead. The proxied - route now shares the direct route's loop: render once, re-read every 2s, - redraw only when the issue's status or `updated_at` changes, stop cleanly on - Ctrl+C/SIGTERM, and require exactly one id. Each poll opens its own short - unit of work, so a long watch never pins a transaction, and a poll that - fails (the issue was deleted, the backend blipped) keeps the last render - without printing, as on the direct route. On both routes a watch whose id - cannot be found now exits non-zero instead of 0. - -- **Generated git hooks accept uutils coreutils `timeout` as a deadline helper** - ([#5541](https://github.com/gastownhall/beads/issues/5541)). The managed hook - section probes `timeout` and `gtimeout` with `--version` and accepted only the - GNU coreutils banner, so a host whose `timeout` is uutils coreutils - (Ubuntu 25.10+, or a distribution configured with `uutils-coreutils` in place - of GNU) failed the probe and fell through. Where Perl was - installed the shim still got a deadline from the Perl `alarm` arm — which Git - for Windows Perl does not guarantee across `exec` — so the missing deadline - bit uutils hosts *without* Perl, which ran `bd hooks run` unbounded (with the - documented warning). The probe now also accepts the - `timeout (uutils coreutils) ` banner; native Windows `timeout.exe` stays - rejected ([#5503](https://github.com/gastownhall/beads/issues/5503)). The - widened probe ships in the generated hook section, which is rewritten only by - `bd hooks install` — hosts that already installed hooks must run it once to - pick up the fix. +- **Unblocking two blockers of one dependent at the same time no longer leaves + the dependent stuck as blocked** ([#6716](https://github.com/gastownhall/beads/issues/6716); [#6719](https://github.com/gastownhall/beads/pull/6719), [#6942](https://github.com/gastownhall/beads/pull/6942)). + Closing both blockers in parallel (formula fan-in with parallel workers), or + a close racing a `bd dep remove` or a delete of the other, could leave the + dependent `is_blocked=1` and missing from `bd ready` until + `bd recompute-blocked`. A write that takes a blocker away (a close, an update + to an inactive status, a dependency removal, a delete) now rechecks the + dependents it recomputed once its transaction has committed. This covers the + Dolt store write transactions, every write the proxied-server (uow/domain-db) + route serves — under `--proxied-server`, the default topology, that is + `bd close` (single and batch), `bd update --status`, `bd dep remove`, + `bd delete`, `bd batch` and `bd serve` — `RunInTransaction` (`bd batch` + direct, `bd cook`, `bd mol squash`/`burn`, and SDK callers that close or + update inside a transaction), the wisp close, update and delete writers, and + the legacy dependency removal behind `bd duplicates --merge`. Demote-to-wisp + moves a row between planes without taking a blocker away and is unchanged. - **`bd close` now exits non-zero when any issue in a batch fails to close** - ([#6648](https://github.com/gastownhall/beads/issues/6648)). A batch with one + ([#6648](https://github.com/gastownhall/beads/issues/6648); [#6740](https://github.com/gastownhall/beads/pull/6740), [#6771](https://github.com/gastownhall/beads/pull/6771)). A batch with one refused id used to exit 0 as long as another id closed, so scripts could not tell that part of the batch was left open. The closable ids still close, the refusals are still printed, and a final `Error: N of M issues failed to close` @@ -2042,62 +520,69 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 transaction and a sibling's refusal does not roll it back — so the summary names the claimed id rather than leaving it silently assigned. -- Unblocking two blockers of one dependent at the same time — closing both, - or a close racing a `bd dep remove` or a delete of the other — no longer - leaves the dependent stuck as blocked and hidden from `bd ready` until - `bd recompute-blocked`: a write that takes a blocker away (a close, an - update to an inactive status, a dependency removal, a delete) and runs - through a Dolt store write transaction now rechecks the dependents it - recomputed once that transaction has committed - ([#6716](https://github.com/gastownhall/beads/issues/6716)). The - proxied-server route and the remaining store routes are covered by the - entry above. +- **`bd query` no longer silently stops at 50 rows when its output is piped** + ([#6229](https://github.com/gastownhall/beads/issues/6229), [#6744](https://github.com/gastownhall/beads/pull/6744)). `bd list` has + treated piped stdout as unlimited since GH#4094, but `bd query` read its + `--limit` default of 50 straight through, so `bd query '...' | consumer` + dropped every row past 50 with no hint. An unflagged `bd query` now resolves + through the same policy as `bd list`: an explicit `--limit` wins, piped + stdout is unlimited, agent mode on a terminal gets 20, and a terminal gets 50. +- **Deleting or purging a wisp no longer leaves orphan rows behind on stores + without the wisp foreign keys** ([#6563](https://github.com/gastownhall/beads/pull/6563), [#6627](https://github.com/gastownhall/beads/pull/6627), [#6487](https://github.com/gastownhall/beads/issues/6487)). + On a store whose `wisp_dependencies` and wisp auxiliary tables lack the + migration-0047 FK constraints, every wisp delete (including `bd purge`, + `bd mol burn` and wisp GC) left its `wisp_dependencies` edges on both sides + and its `wisp_labels`, `wisp_events`, `wisp_comments` and + `wisp_child_counters` rows in place, which showed up as dangling parent + references and store bloat. The delete paths now remove them. This is a + forward fix: orphans already in a store are not cleaned up. -- **`bd list --watch --format` is refused instead of silently dropping the - format** ([#6277](https://github.com/gastownhall/beads/issues/6277)). - `--watch` always re-renders the pretty listing, so on the direct route a - `--format` template was ignored without a word, while `--proxied-server` - already refused the combination. Both routes now fail with the same - `--format cannot be combined with --watch` usage error. - -- Explicit `bd update --claim` and direct `bd close` now enforce unsatisfied - external capability dependencies. Mixed close batches still commit eligible - items together, and `--claim-next` skips externally blocked work even when - `--force` overrides the close guard. +- **A clone missing its local `events`, `bd_events_journal` or `bd_events_seq` + table recreates it on open** ([#6547](https://github.com/gastownhall/beads/pull/6547)). Those tables are dolt-ignored, so + a fresh clone can arrive at the latest schema version without them, and bd + then treated the schema as complete and never created them. Their absence + now makes the migration runner replay the migrations that create them, from + each table's own floor rather than from zero. - **The smart migrate gate no longer auto-migrates a clone whose data is behind - the remote, and `bd dolt pull` now works from that state** - ([#6575](https://github.com/gastownhall/beads/issues/6575)). The gate's - first-mover verdict was computed from schema facts alone, so a clone level - with the remote on schema but behind it in unpulled data commits was - classified a safe first-mover and migrated in place — minting local-only - schema commits on a history missing those commits, which can leave every - later `bd dolt pull` refusing to merge - ([#6368](https://github.com/gastownhall/beads/issues/6368)). The - equal-version path now reads the branch position its callers already supply - and stops with a pull-first remedy instead. + the remote** ([#6575](https://github.com/gastownhall/beads/issues/6575)). The + gate's first-mover verdict was computed from schema facts alone — equal + migration content hashes against the cached remote ref, at the same version, + at or above the convergence floor — and none of those say where the clone's + branch sits relative to that ref. A clone level with the remote on *schema* + but behind it in unpulled *data* commits satisfied every precondition, was + classified a safe first-mover, and migrated in place, minting local-only + schema commits on a HEAD missing those commits. When the pending batch moves + a tracked table onto the `dolt_ignore` plane, every later `bd dolt pull` then + refuses with `local changes would be stomped by merge: events` + ([#6368](https://github.com/gastownhall/beads/issues/6368)) while `dolt + status` reads clean — and the clone can no longer fetch the very commits it + was behind on. Upgrading from a 1.1-era database is the ordinary way in, so + this release's upgrade audience is the exposed one. The gate now reads the + branch position its callers already supply and stops with guidance naming the + remedy: run `bd dolt pull`, then re-run what you were blocked on, and the + migrate proceeds as a true first-mover. The stop applies whenever the clone has commits left to pull, whether or not - it also has local commits of its own. Since bd auto-commits every write, a - clone that is both behind and ahead is the ordinary multi-machine state and - reaches the same wedge; it is told its pull will *merge* rather than - fast-forward, and how to resolve conflicts if it reports them. A clone that - is level, or that only has unpushed local commits with nothing to pull, - still auto-migrates exactly as before. + it also has commits of its own. bd auto-commits every write, so a clone that + is both behind and ahead is the ordinary multi-machine state, and it reaches + the same wedge; it is told its pull will *merge* rather than fast-forward, and + how to resolve conflicts if it reports them. A clone that is level with the + remote, or that only has unpushed local commits with nothing to pull, still + auto-migrates exactly as before. - Crucially, the refusal no longer blocks its own remedy. `bd dolt pull` opens - the store too, so on a refused clone it used to hit the very refusal that - prescribed it — and an embedded workspace has no external `dolt` binary to - fall back to, which left `BD_ALLOW_REMOTE_MIGRATE=1` (performing the - migration the stop exists to prevent) as the only way forward. `bd dolt pull` - now opens leniently for this one refusal, the way `bd dolt commit` does for - the dirty-working-set refusal + **`bd dolt pull` works from the refused state.** It opens the store too, so + on a refused clone it would otherwise hit the very refusal that prescribes + it — and an embedded workspace has no external `dolt` binary to fall back to, + which would leave `BD_ALLOW_REMOTE_MIGRATE=1` (performing the migration the + stop exists to prevent) as the only way forward. `bd dolt pull` now opens + leniently for this one refusal, the way `bd dolt commit` does for the + dirty-working-set refusal ([#4566](https://github.com/gastownhall/beads/issues/4566)); every other gate - refusal still fails that open. This supersedes the 1.3.0 upgrade note's - statement that the pending-migration gate refuses `bd dolt pull`: for the - data-behind stop it does not, by design. Re-run the command you were blocked - on once the pull completes and the migration proceeds. + refusal still fails that open. This supersedes the [1.3.0] upgrade note's + statement that the pending-migration gate refuses `bd dolt pull` as well as + `bd dolt push`: for the data-behind stop it does not, by design. Commands that keep working through the stop say the same thing. `bd list`, `bd ready` and `bd show` succeed against the old schema, and `bd dolt commit` @@ -2114,24 +599,23 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 `--json` callers get the same remedy the terminal does: `observed`, `expected` and `options` describe the pull (a single `pull-first` option) rather than the migrate-or-adopt decision that does not apply here, plus a - `data_behind_shape` of `fast-forward` or `diverged`. For the fast-forward - shape on a non-shared store — one unconditional option, no local commits, - nothing discarded — `human_decision_required` is now `false`, so an agent - can run the pull instead of stalling for approval of a step the same payload - calls riskless. It stays `true` for the diverged shape (the pull merges and - can need conflict resolution) and on a shared store (the follow-up consent - step needs an operator who can confirm every co-resident client is upgraded). - On a shared Dolt sql-server the guidance still carries #5920's consequence — - migrating promotes the schema for every co-resident client — and names the - consent step the retry needs there: `bd migrate schema --force`. It is the - forced form because this stop always has a remote configured (behind-ness is - read from the remote-tracking ref), and the bare `bd migrate schema` consent - is only read for a shared database with *no* remote; the flag consents to - migrating a remote-backed shared store, and by then the pull has landed the - commits the clone was missing. The payload's `docs` link points at the - data-behind section rather than the migrate-or-adopt recipe, and that - section — plus the upgrade and init-safety ordering rules — now records the - `bd dolt pull` exception instead of stating the pull is always refused. + `data_behind_shape` of `fast-forward` or `diverged`. `fallback_reason` stays + `data-behind`. For the fast-forward shape on a non-shared store — one + unconditional option, no local commits, nothing discarded — + `human_decision_required` is now `false`, so an agent can run the pull + instead of stalling for approval of a step the same payload calls riskless. + It stays `true` for the diverged shape (the pull merges and can need conflict + resolution) and on a shared store (the follow-up consent step needs an + operator who can confirm every co-resident client is upgraded). On a shared + Dolt sql-server the guidance carries #5920's consequence — migrating promotes + the schema for every co-resident client, and clients still on an older bd + refuse the database until upgraded — and names the consent step the retry + needs there: `bd migrate schema --force`. It is the forced form because this + stop always has a remote configured (behind-ness is read from the + remote-tracking ref), and the bare `bd migrate schema` consent is only read + for a shared database with *no* remote; the flag consents to migrating a + remote-backed shared store, and by then the pull has landed the commits the + clone was missing. **Proxied-server mode reaches the stop too, and is told where to run the remedy.** The store-open gate on that path supplied no branch-position @@ -2147,281 +631,169 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 option is `pull-first-on-server-host`, and `expected` carries the same qualifier so a single-field reader is not handed a locally-refused command. The shared-store consent step is the other way round: `bd migrate schema - --force` is *not* refused here — the capability registry permits the - two-word `migrate schema` path — so the guidance says it can be run from this + --force` is *not* refused here — the refusal table keys on the command path + and has no `migrate schema` row — so the guidance says it can be run from this workspace but only after the pull lands on the server host, and the warning against forcing past the stop names it alongside `BD_ALLOW_REMOTE_MIGRATE=1` as the two consent surfaces this topology can still reach. Read-only proxied opens print the same pull-first block instead of the shared-consent template. - The *Clone behind the remote* docs section the payload links to, and the - upgrade and init-safety ordering rules that point at it, now give the same - proxied-server answer. - Both existing escape hatches are unchanged: `bd migrate --force` / - `BD_ALLOW_REMOTE_MIGRATE=1` are still consulted before the smart gate, and - `BD_SMART_GATE=0` still yields the blunt gate unconditionally (including for - `bd dolt pull`, which is what opting out means). Pulling before the first - open after upgrading avoids the state entirely. + Both existing workarounds keep working unchanged: `BD_SMART_GATE=0` opts out + of the smart gate entirely (which means the blunt gate applies to `bd dolt + pull` too — that is what opting out is), and running `bd dolt pull` before the + first open after upgrading avoids the state to begin with. `bd migrate + --force` / `BD_ALLOW_REMOTE_MIGRATE=1` are still consulted before the gate and + still override it. -- **A `--format` typed on `bd list` now beats a `json: true` config default** - ([#6278](https://github.com/gastownhall/beads/issues/6278)). With `json: true` - in `config.yaml` (or the environment), `bd list --format digraph`, `dot` or a - template printed the JSON listing instead, because `bd list`'s own `--format` - flag shadows the root one the config-precedence check looks at. A typed - `--format` now wins over the config default; an explicit `--json` still wins - over `--format`. +- **`bd list --status --ready` honors `--status`** + ([#5924](https://github.com/gastownhall/beads/pull/5924), + [#5832](https://github.com/gastownhall/beads/issues/5832)). Combining the two + silently dropped `--status` and returned the unfiltered ready set, because the + ready path pinned `open` in two places regardless of what was asked for, so a + custom status or `in_progress` never reached the ready query. An explicit + `--status` is now the ready-work status selector, matching how the other list + filters already compose with `--ready`. `--ready` with no `--status` still + defaults to open, and `--status all` remains the no-filter spelling. The footer + wording moves with it — see **Changed**. -- **Strict readonly no longer records last-touched state** - ([#5785](https://github.com/gastownhall/beads/issues/5785)). Under strict - readonly (`--readonly`, or `readonly` in config), `bd show` wrote the issue - id to `.beads/last-touched` even though readonly promises not to mutate - workspace-local state — the database store was opened read-only while this - marker file was still written. `SetLastTouchedID` now returns early in that - mode, so a readonly workspace leaves the marker untouched. Ordinary - (non-readonly) `bd show` keeps recording last-touched exactly as before, so - only consumers driving `bd --readonly … --json` see the change. +- **`bd list --wisp-type` can reach rows at all** + ([#6098](https://github.com/gastownhall/beads/pull/6098)). `bd list` now + registers `--include-ephemeral`, the plane knob `--wisp-type` needs, off by + default so the default listing is byte-identical. `bd list + --include-ephemeral --wisp-type heartbeat` is the working spelling. Before + this, the only way to admit the wisp plane from `bd list` was `--type + `, which also lifts the infra-type exclusions and changes template + treatment — three decisions for a caller who wanted one classification. Bare + `--wisp-type` is now refused rather than empty; see **Changed**. -- **Markdown bodies now honor the terminal's actual background instead of - always rendering dark** - ([#5776](https://github.com/gastownhall/beads/pull/5776)). - `glamour.WithEnvironmentConfig()` defaults to `styles.DarkStyle` whenever - `GLAMOUR_STYLE` is unset, with no regard for the terminal's real - background — unlike `internal/ui`'s own adaptive colors, which probe it - via `lipgloss.HasDarkBackground`. That left every markdown body's colors - wrong on a light-background terminal even though the section header - wrapping it adapted correctly. The fix lands at the shared renderer, so it - covers every markdown body `bd` prints — DESCRIPTION, DESIGN, NOTES, - ACCEPTANCE CRITERIA, and comment bodies, direct and proxied alike — not - just `bd show`'s DESCRIPTION field. An explicit `GLAMOUR_STYLE` override - still takes priority over the detected background. +- **`bd doctor` knows about the generated `.beads/dolt-server-config.yaml`** + ([#5978](https://github.com/gastownhall/beads/pull/5978)). + `doltserver.Start()` writes that file when the resolved dolt binary supports + `auto_gc_behavior.archive_level`, but the hyphenated name falls outside the + `dolt-server.` prefix its five sibling server-state entries share, so neither + the gitignore template nor the required-pattern list covered it. It holds an + absolute `cfg_dir` and a per-machine port, so committing it breaks every other + clone — the same reason the template already gives for `redirect`. It shows up + as untracked in every consumer repo whose dolt supports `archive_level`. It is + now in both lists: the template covers new workspaces, and the required + patterns are what `bd doctor --fix` appends to an existing + `.beads/.gitignore` — see **Changed** for the warning that implies. -### Added +- **The missing-prefix error stops recommending a config key that does nothing** + ([#5936](https://github.com/gastownhall/beads/pull/5936), + [#5916](https://github.com/gastownhall/beads/issues/5916)). When the database's + `issue_prefix` config row was absent, the error advised using the + `issue-prefix` key in `config.yaml` — but that gate only reads the database's + config table, so following the advice produced a tracked config field that + changed nothing and misled the next reader. The message now points at + `bd init --prefix` or `bd bootstrap`, which is what writes the row, and says + plainly which store the check reads. Error text only; resolution behaviour is + unchanged. -- **`POST /v0/beads/issues:batchApply` accepts up to 1000 items, up from 100.** - `issueops.MaxApplyBatchItems` moves from 100 to 1000; the request body cap - rises from 4 MiB to 16 MiB (`maxApplyBatchBodyBytes`); and the whole plan - still applies in exactly ONE transaction — the cap raise never chunks a - request. A request over 100 items now runs under a separate, EXTENDED - whole-run budget instead of the ordinary 60s request deadline: a new - `bd serve --large-apply-ceiling` flag sets it (5 minutes by default), applied - flat regardless of item count, never scaled down. A request at or under 100 - items is completely unaffected and keeps exactly the 60s deadline it has - always had. A request over 100 items also serializes against every other - such request through a dedicated one-wide semaphore, so at most one - oversized write transaction is in flight at a time; the wait for that - semaphore is itself bounded, so a burst of oversized requests is shed with - `503` rather than queuing indefinitely and starving ordinary traffic. An - ordinary (<=100-item) request is never made to wait on it. Graceful shutdown - waits out an in-flight large apply up to its own deadline, but refuses any - large apply still queued for the semaphore once shutdown begins, rather than - extending the drain further. `bd serve`'s orchestrator stop grace must be at - least `--large-apply-ceiling` plus 5 seconds, or an external `SIGKILL` can - still cut off an in-flight large apply the drain would otherwise have waited - out; see `engdocs/SERVE_RUNBOOK.md`. A new `issues.batchApplyLarge` - capability token on `GET /v0/beads/context` lets a client learn whether a - given server accepts the raised envelope before sending a plan over the old - 100-item bound. Measured wall-clock for the whole-transaction apply - (embedded Dolt, build+commit only) backs the 5-minute default: a 356-item - plan (the design doc's primary measured shape) commits in ~12.6s (23.8x - headroom under the ceiling), and a 1000-item plan (the new cap) commits in - ~57.5s (5.2x headroom). +- **`bd create --file` says that it creates one issue per `##` heading** + ([#5921](https://github.com/gastownhall/beads/pull/5921)). The usage string was + "Create multiple issues from markdown file", which named neither the per-H2 + semantics nor `--body-file`. Someone wanting "one issue, description loaded + from a file" reached for the shortest flag and got a silent bulk import — a + briefing file with `## Problem` / `## Context` / `## Acceptance Criteria` + became three issues. The usage now names the behaviour, and the existing + rejection of a positional title combined with `--file` carries a `--body-file` + hint. `--file` alone on a section-headed file is unchanged: bd cannot tell that + apart from a genuine batch file by content. -- **A long-running schema migration now says so instead of going quiet** - ([#5997](https://github.com/gastownhall/beads/pull/5997)). Migrations are - allowed to take a long time by design — migration 0047's full-table - `is_blocked` recompute is a real example — but until now a slow one and a - wedged one looked identical from outside: `bd` simply stopped printing. A - watchdog now emits a WARN naming the migration's version, name, and elapsed - time once the migration's own SQL has been running past the interval, and - repeats every interval while it keeps running, so an operator reading logs - can tell "still working" from "stopped emitting anything". Coverage is the - migration body specifically — the per-step Dolt commit that follows it on - the production embedded path is outside the watchdog, so a stall there is - still quiet. Set `BEADS_MIGRATION_WATCHDOG_INTERVAL` to change the - 5-minute default; it accepts durations like `10m` and bare seconds like `90`, - and falls back to the default when unset or unparsable. This is observability - only and never a circuit breaker: the migration receives the caller's exact - context, its error is passed through unchanged, and nothing is aborted or - rolled back. The warning is deliberately not terminal-gated, so it survives - `bd serve`, systemd, CI, and piped invocations. +- **The list truncation hint no longer leaves a padded blank line under the + shell prompt** ([#5927](https://github.com/gastownhall/beads/pull/5927), + [#5685](https://github.com/gastownhall/beads/issues/5685)). The hint passed its + surrounding newlines through the warning renderer, which treats them as a + block, pads the blank lines out to the widest line's width, and drops the + trailing newline — so the next prompt printed on top of a run of spaces. The + newlines are kept outside the renderer now, leaving one coloured line with a + real trailing newline. -- **Auto-backup runs on a managed-local proxied-server workspace.** The - proxied arm of the post-command hook now calls auto-backup, so an explicit - `backup.enabled=true` (or `BD_BACKUP_ENABLED=1`) takes the same throttled, - change-detected Dolt-native backup into `.beads/backup` it takes in direct - mode, through the proxied provider's non-transactional seam. Before this the - opt-in was inert on every proxied workspace and `bd backup status` said so. - The default stays OFF, as on every server-mode workspace: many bd clients - share one server. It is honored exactly where `bd backup sync` is — a - proxied server bd started itself — and stays inert on an external or - team-server topology, strict `--readonly`, `bd serve`, a preview - (`--dry-run`/`--inspect`) and a migration freeze. A command that opened no - provider (e.g. `bd dolt stop`) backs nothing up rather than relaunching the - server to do so. `bd backup status` now reports the proxied default as - `auto: off in proxied-server mode; set backup.enabled=true to opt in`. +- **`bd setup claude` and `bd init` stop rewriting `.claude/settings.json` on + every run** ([#5944](https://github.com/gastownhall/beads/pull/5944), + [#5693](https://github.com/gastownhall/beads/issues/5693)). The settings map + was marshaled without a trailing newline and written unconditionally, so each + run stripped the newline Claude Code's own writes leave behind and rewrote the + file even when no hook had changed. Because `bd init` git-adds that file, it + surfaced as a staged diff with no semantic content. The write now keeps the + trailing newline and is skipped when the bytes match what is already on disk, + at all four call sites — the settings write and the `settings.local.json` + migration on install, and both again on remove. -- **Auto-backup and `bd backup sync` no longer overlap.** A per-workspace - backup lock (`.beads/backup.lock`) serializes them in every mode: - auto-backup skips when another backup holds it, and `bd backup sync` waits - up to 5 seconds (the bound restore uses for the workspace gate) and then - fails with "another backup is running for this workspace". +- **`bd gate check` resolves bead gates whose target lives in a prefix-routed + rig** ([#5859](https://github.com/gastownhall/beads/pull/5859)). After a local + miss, the evaluator follows the target bead ID through `routes.jsonl` and reads + the owning store without writing to it; previously such a gate stayed pending + until someone resolved it manually. This covers explicit gate checks in + embedded, server, and proxied-server command paths. The legacy + `:` await value remains accepted for compatibility, a malformed + cross-rig value is rejected, and when prefix routing is unavailable the original + local not-found error is preserved. -- **`bd purge` covers an orchestrator's wisp retention sweep.** Three changes - that together let `bd purge --wisps-plane --older-than 168h --force` replace - a raw `DELETE FROM wisps` retention step: - - **Live-dependent protection, always on.** `bd purge` no longer deletes a - closed bead that a live bead depends on through a `parent-child`, - `tracks` or `blocks` edge — a closed molecule root whose step is still - open, a closed wisp a live convoy tracks, a closed blocker of live work. - "Live" is any status that is not done, including custom statuses. The - held-back count is reported as `live_dependent_skipped` in `--json` (and - on its own line in text output). The protection is on the role - (`issueops.SweepRequest.ProtectLiveDependents`); `bd prune` does not ask - for it. - - **`--wisps-plane`** selects every closed row stored in the wisps table, - including `--no-history` beads, which the default ephemeral selection - leaves to `bd prune`. It works on embedded, server and proxied - workspaces, and because it reaches durable-tier rows it requires - `--older-than` or `--pattern`, like `bd prune` - (`issueops.SweepWispsPlane`). - - **`--older-than` accepts hour and finer durations** (`36h`, `168h`, - `90m`, down to `1s`) on both `bd purge` and `bd prune`. Day values (`7`, - `7d`, `2w`) are unchanged. An `Nh` value used to be converted to whole - days: floored above a day (`36h` swept rows only 24 hours old) and - rounded UP to one day below it (`12h` kept everything younger than 24 - hours). Both are now taken exactly, so `bd prune --older-than 12h` now - deletes rows closed 12–24 hours ago that it used to keep. A value too - large to represent (e.g. `213504d`) is now refused instead of silently - wrapping to a tiny age. - - **`--limit N`** caps one `bd purge` run at N beads, oldest-closed first - (`issueops.SweepRequest.Limit`), so a large backlog drains in bounded - transactions; `--json` then adds `remaining` and `has_more`. Loop while - `has_more` is true. +- **A transient connection drop while opening a Dolt store is retried instead of + failing the command** + ([#6003](https://github.com/gastownhall/beads/pull/6003)). The bootstrap ping + in the unit-of-work provider was unretried, so a server still finishing startup + — or one that reset the connection mid-handshake — became a hard failure. + Connection-level shapes (`driver.ErrBadConn`, `mysql.ErrInvalidConn`, EOF, any + `*net.OpError`) are now retried for up to 30s, each attempt capped at 10s so a + server that accepts TCP and then stalls cannot block for the caller's whole + context. Durable rejections and an unresolvable hostname still fail without + retrying. See **Changed** for the latency this adds against a dead endpoint. -- **`bd backup` works on a proxied-server workspace bd runs the Dolt server - for.** `bd backup init`, `sync`, `remove`, `status` and `restore` are routed - over the proxied provider; before this, a proxied workspace — the default - topology since 1.3.0 — had no backup path at all. `bd backup restore` stops - the proxy and its Dolt child before replacing the database and leaves the - workspace quiescent, so the next command relaunches against the restored - data. +## [1.3.1-rc.1] - 2026-09-16 - The family stays **refused by design** on a proxied workspace pointed at a - Dolt server bd does not own (an external host, socket, or a beads-team-server - database): `CALL DOLT_BACKUP('add', …)` registers the backup remote on the - *server*, where it is global to every client connected to it, so one - workspace's backup decision would silently become everyone's — the shape - `bd backup`'s own help text already warns about for auto-backup. A `file://` - destination has a second problem on top, since the server resolves that path - on its own filesystem. The refusal keeps the `proxy.backup.unsupported` code - and now carries `"reason": "design"`; a backup story for those deployments - belongs to whoever runs the server. Whether a *remote-scheme* destination - (DoltHub, `aws://`, `gs://`) should eventually be allowed there is recorded as - an open question rather than settled — the server pushes those over the - network from its own environment, which is the same credential question the - next slice has to answer for `dolt push`. +### Changed - Backups on this topology are **explicit only**. Auto-backup has no - post-command hook on the proxied path, so `backup.enabled=true` does nothing - there; `bd backup status` and `bd backup` help now say so instead of - attributing the OFF to a missing git remote, which was never the reason on a - server topology. Wiring an auto-backup hook through the new routes is left to - a later slice. +- **`bd dolt status --json` changes shape on a proxied workspace** + ([#6580](https://github.com/gastownhall/beads/pull/6580)). It now emits + `{"mode": "proxied-server", "root": ..., "running": , "proxy_pid": + ..., "proxy_port": ..., "backend_managed": ..., "backend_running": ..., + "backend_pid": ..., "backend_port": ..., "backend_endpoint": ..., + "idle_timeout": ...}` instead of the always-false `{"running": false, "pid": + 0, "port": 0}` — `pid` and `port` are gone, replaced by the `proxy_*` and + `backend_*` pairs. `running` no longer reports a bd-managed dolt PID file; it + describes the proxy, the endpoint every bd command connects through, so it is + now usually true where it used to be always false. `backend_managed` is false + on external proxied topologies, where the dolt server is not bd's process to + report on and `backend_endpoint` names it instead. Anything parsing the old + payload changes behaviour on upgrade. Direct-server, shared, embedded and + externally-managed workspaces keep the output they had. -- **`bd` says when it resolved a partial ID instead of matching one exactly.** - ([#6302](https://github.com/gastownhall/beads/pull/6302)) ID resolution tries an exact match first and only then falls back to - leading-prefix abbreviation (`a3f8` -> `a3f8e9`). Ambiguity was already a hard - error naming every candidate, but a *sole* abbreviation match was returned - silently, so a caller could act on an issue it had not named without any - signal. bd now writes a one-line note to stderr identifying both the input and - what it resolved to. The case that motivates it: a hierarchical child is a - valid leading-prefix match for its own parent's ID, so after a parent is - renamed or deleted the vacated ID quietly resolves to the child. Note this - covers **every** leading-prefix abbreviation, not only that shape, so the - ordinary documented `a3f8` -> `bd-a3f8e9` path now emits a line too, once per - resolved argument. That breadth is deliberate: a `.`-boundary rule would fix - parent/child but leave `bd-x.1` -> `bd-x.11` just as silent. stdout and - `--json` are unchanged; silence it with `--quiet` or - `BD_NO_PARTIAL_ID_NOTICE=1`. +- **`bd config set ` deletes a flat top-level key of the same + literal name** ([#6578](https://github.com/gastownhall/beads/pull/6578)). + Dotted keys are now written as a nested mapping (see **Fixed** below for why), + and where the file already carried the flat spelling — a top-level key whose + *name* contains the dot, such as `dolt.host: "10.0.0.1"` — that entry is + removed and the value re-emitted under `dolt:`. Only the key being written is + touched; other dotted keys, including ones bd did not write, are left exactly + as they are. -- **`bd count` accepts `--has-metadata-key`** - ([#6160](https://github.com/gastownhall/beads/issues/6160)), closing the last - metadata-filter gap against `bd list`, `bd ready` and `bd search`. A caller - counting rows that merely carry a key no longer has to list them and measure - the page. + `.beads/config.yaml` is not exclusively bd's, so this can be visible outside + bd: an external tool that both writes *and* reads flat dotted keys there will + stop seeing any value bd migrates this way. Any dotted key bd stores in + `config.yaml` rather than the database can collide; in practice that means + `dolt.*`, `export.auto`, and `backup.enabled`. If the other writer then + re-adds its flat key, the file carries both spellings and bd's own readers + disagree — Viper-backed reads (`bd config get`, and the `dolt.auto-start` + lookup on the store-open path) return the **flat** value, while + `GetStringFromDir` returns the **nested** one, so a `bd config set` can be + shadowed by a stale flat entry. Tracked as + [#6594](https://github.com/gastownhall/beads/issues/6594). Until that lands, + in a workspace whose config another tool maintains, let that tool own its + keys, or check `.beads/config.yaml` for a duplicated key after running + `bd config set`. -- **`bd count` supports repeatable `--metadata-field key=value` filters** - ([#6023](https://github.com/gastownhall/beads/issues/6023)), so callers can - count the same metadata-scoped set `bd list` returns without fetching every - row. -- **`bd label rename ` fixes a label everywhere it appears, in one - command instead of a per-issue add-then-remove loop** - ([#6009](https://github.com/gastownhall/beads/pull/6009)). An issue that - already carries the new label doesn't error — the rename drops its stale - old label and counts the issue as a merge instead, reported honestly: - `Renamed label 'Backend' to 'backend': 6 issues (2 already had 'backend')`. - A label nothing carries is a clean no-op (`No issues found with label - 'Backend'`, exit 0). The sweep covers both planes, durable issues and - wisps, and the direct and proxied-server routes end at the same - `issueops.RenameLabelInTx` primitive — the proxied route journals through - `domain.LabelUseCase.RenameLabel` rather than a per-issue add+remove - fan-out, so merge semantics, the zero-carrier no-op, and the single - `label_renamed` event per touched row are identical on both. - - `--dry-run` previews the blast radius (a count and the first 10 issues, - all of them under `--json`) without writing anything, through a read-only - path kept apart from the write path so a preview can never reach a - transaction. Its `merged` count is a plain snapshot intersection of two - independent reads, though, not the write path's measured one: the real - rename derives `merged` from what its `INSERT IGNORE` actually affected, - specifically so a concurrent label add between two reads can't skew the - count, and the preview has no insert to measure against. Treat the - dry-run number as an estimate — the flag's own help text says so now too. - A rename that fails partway through reports an UPPER BOUND - up to how many - issues may have been renamed, and up to how many of those were merges - - rather than only the error. It cannot be a landed count: both routes assign - those numbers inside the transaction, so a rollback or an exhausted retry - returns them still describing an attempt that landed nothing. Only a Dolt - publication failure after the SQL side already committed is a genuine - partial, and the count alone cannot tell the two apart. - - -- **`bd label remove --prefix` removes every label matching a prefix in one - call**, instead of requiring an exact label name per removal. `bd list`/`bd - ready` already supported `--label-pattern`/`--label-regex` for *finding* - issues by label shape; `label remove` had no equivalent for *stripping* a - whole label family (e.g. every `pool:refused:*` reason label) — callers had - to fetch the labels themselves and issue one `label remove` per exact name. - `--prefix` resolves the matching labels per issue and removes that issue's - whole matching set in a single edit; `bd label remove --prefix - pool:refused:` replaces that fetch-then-loop. Routed through the same `issueops.Lifecycle`/ - `issueops.Reader` roles as a plain `label remove`, so it works identically - on the embedded and proxied-server (`bd serve`) storage modes. - -- **`PATCH /v0/beads/issues/{id}` accepts `claim: true`**, the wire spelling of - `bd update --claim`. It passes through to the same lifecycle role the - direct route uses (`issueops.UpdateRequest.Claim`), so the claim and the - patch land in one transaction with the direct route's semantics: a - same-actor re-claim is idempotent (`changed: false`), a foreign holder is - 409 `already_claimed` and an unclaimable status 409 `not_claimable` — both - naming `claim` and carrying `{id}:claim`'s `assignee` / `issue_status` - members — and a refused claim writes none of the patch. With `claim: true` - the `patch` may be empty. `claim` beside `expected_assignee`, - `expected_status` or `force_assignee_transfer` is a 400. A server that - predates the member refuses it as `unknown_parameter` before any database - work, so an HTTP client can forward `bd update --claim` instead of refusing - it, and can never mistake an older server for one that dropped the claim. +- **Two `bd config set` writes that used to exit 0 now exit 1** + ([#6578](https://github.com/gastownhall/beads/pull/6578)), because both wrote + a value no reader could see: setting a dotted key under a parent that already + holds a scalar (`sync: enabled`, then `bd config set sync.remote`), and + setting one in a file whose top level is not a mapping. A script that + tolerated the old exit 0 now has to handle the failure. ### Fixed -- **`bd show` counts a wisp's comments instead of reporting `comment_count: 0`** - ([#5565](https://github.com/gastownhall/beads/issues/5565)). On the - direct/embedded path, `CountIssueComments` always queried the permanent - `comments` table, while `GetIssueComments` routes an active wisp to - `wisp_comments`, so `--include-comments` returned rows the count said did - not exist and `comments_omitted` was never set. The count now routes by wisp - status the same way, matching proxied-server mode. - - **`bd dolt start` no longer puts a second sql-server over a proxied workspace's data directory.** On a proxied-server workspace the default root IS `.beads/dolt`, and `bd dolt start` knew nothing about proxied mode: with @@ -2434,442 +806,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 its backend's lifecycle — it starts on demand and `bd dolt stop` shuts it down. Direct-server and embedded workspaces are unaffected. -- **`bd info` honors `doctor.suppress.git-hooks`** - ([#6027](https://github.com/gastownhall/beads/issues/6027)). `bd info` - printed the "Git hooks not installed" / "outdated" warning unconditionally, - so suppressing the check for `bd doctor` left it nagging on every `bd info`. - It now reads the same `doctor.suppress.*` config through the doctor helper - and skips the warning when `git-hooks` is suppressed. `bd info --json` never - carried the warning and is unchanged. - - **`bd dolt status` tells the truth on a proxied workspace.** It read the classic PID file, which proxied mode never writes, and so reported `Dolt server: not running` while the proxy was serving CRUD — the wrong answer that sent operators to `bd dolt start` in the first place. It now reads the proxy's own records and reports the proxy and its dolt backend separately, - without starting either. **JSON output shape change**: on a proxied - workspace `bd dolt status --json` now emits - `{"mode": "proxied-server", "root": ..., "running": , "proxy_pid": - ..., "proxy_port": ..., "backend_managed": ..., "backend_running": ..., - "backend_pid": ..., "backend_port": ..., "idle_timeout": ...}` instead of - the always-false `{"running": false, "pid": 0, "port": 0}`. `running` - describes the proxy, the endpoint every bd command connects through; - `backend_managed` is false on external proxied topologies, where the dolt - server is not bd's process to report on. - -- **`bd create --metadata` and `bd update --metadata` now require a JSON - object** ([#6035](https://github.com/gastownhall/beads/issues/6035)). Both - checked only that the value was valid JSON, so `bd create --metadata - '"oops"'` stored a bare string (or an array or number) as the issue's - metadata, while `bd update` refused the same value only late, in the - storage merge, with an internal unmarshal error. A shared check now refuses - a string, number, boolean, array or `null` up front on every create and - update path with `invalid --metadata: must be a JSON object`; `{}` is still - accepted. `null` is the one value update handled differently before: the - storage merge accepted it as a no-op and exited 0, so `bd update --metadata - null` now exits 1 where it used to silently change nothing. Values inside - the object stay opaque, so a string value that looks like JSON is kept as - written. - -- **`bd mol ready --gated` no longer refuses under `--proxied-server` when - `BEADS_MAX_ROWS` is set** - ([#6293](https://github.com/gastownhall/beads/pull/6293)). The proxied - capability front door keyed its rules on cobra's leaf name, and `bd ready` - and `bd mol ready --gated` share the leaf "ready", so the row cap refusal - written for the former also refused the latter — a proxy-supported command - with no `--max-rows` flag to refuse. Rules are keyed on the full command - path now. - -- **`bd init` no longer tells you not to run `bd init` when it refuses a - foreign database** ([#5558](https://github.com/gastownhall/beads/issues/5558)). - When `bd init` opens a server database that already exists and belongs to a - different project — for example when `--server-host`/`--server-port`/ - `--database` point it somewhere other than this workspace's own server — it - correctly refuses with `PROJECT IDENTITY MISMATCH`, but the refusal was the - one written for ordinary opens, ending in "Do NOT run 'bd init'". The - init-time refusal now names the database and points at remedies that work - from there: point `bd init` at this project's server - (`--server-host`/`--server-port`) or an unused database name - (`--database`), or run `bd doctor --fix` / `bd bootstrap` if `metadata.json` - is the stale side. Only `bd init`'s own open gets this wording; every other - open, including the library API, `bd doctor --fix` and `bd bootstrap`, keeps - the existing message. - -### Changed - -- **`bd init` refuses to initialize directly in your home directory** - ([#4635](https://github.com/gastownhall/beads/issues/4635)). `bd init` sets - up the directory it runs in as a project: it runs `git init` there unless it - finds a repository, and installs agent files — `CLAUDE.md`, `AGENTS.md`, - `.gitignore`, `.claude/`, `.codex/`, `.cursor/`, `.agents/`. In your home - directory most of those are your agents' user-wide settings, so bd's hooks - ended up running in every project, and init did this silently under - `--non-interactive`, which engages automatically whenever stdin is not a TTY - (so: whenever an agent runs it). A project is virtually never the home - directory itself, so this now refuses with exit code `15` - (`ExitHomeDirRefused`; `13` and `14` already mean a lost conditional-write - race and a migration freeze), documented at - `docs/recovery/init-safety.md#init-home-refused`. - - Deliberately narrow: it fires only when the working directory is *exactly* - your home directory, your home is not itself a git repository (tracked - dotfiles are unaffected, but a home that only sits inside an enclosing - repository, or a `GIT_DIR` inherited from the environment, does not count), - and you did not name a `BEADS_DIR` yourself. A subdirectory of home is an - ordinary project and is never refused. If you do name a `BEADS_DIR` from - an untracked home, init sets up only that workspace: no `git init`, no - agent files or `.gitignore` in your home directory, and nothing in a - repository your home sits inside (no hooks, `beads.role`, Dolt remote taken - from its origin, or bootstrap commit) unless a flag such as `--stealth` or - `--setup-exclude` asks for it. `--force` and - `--reinit-local` do not override the refusal — they bypass the local - data-safety guard, which is a different question from "is this the right - directory". The guard runs first in init's `RunE`, ahead of every path with - an effect, including the proxied dispatch and the store-opening - `--reinit-local` pre-checks. - -- **The managed shared Dolt server now opens its configured remotesapi - listener** ([#6020](https://github.com/gastownhall/beads/pull/6020)). - `dolt.remotesapi-port` (machine-global user config) and - `BEADS_DOLT_REMOTESAPI_PORT` now reach the launched `dolt sql-server` - (as `--remotesapi-port` in flag mode, `remotesapi.port` in generated-YAML - mode), and `doltserver.Restart` applies a listener change under one - lifecycle lock. Zero (the shared-mode default) still means no listener. - - This changes the meaning of a pre-existing knob: `BEADS_DOLT_REMOTESAPI_PORT` - previously only informed federation doctor checks; a managed server launched - with it set now actually opens that listener. A server still running from - before the setting appeared refuses `bd dolt start` with a - `bd dolt stop && bd dolt start` remedy, while auto-start keeps serving SQL - and prints a warning until the server is restarted. - - Security caveat: Dolt binds the remotesapi listener on all interfaces and - serves it unauthenticated, unlike the managed 127.0.0.1 SQL listener. Only - enable it where a firewall, private interface, or tunnel bounds who can - reach the port. - -- **`bd mol pour` and `bd mol wisp` now reject a `--var` name the proto cannot - consume, instead of dropping it silently.** An unusable name was accepted and - ignored, which turned a typo in an optional var into a conditional step that - quietly never appeared, or a defaulted var that quietly kept its default - - with a successful exit and no output to suggest otherwise. A name is accepted - if any proto being poured declares it, references it as a `{{handlebar}}` in - any field the pour substitutes - the prose fields, `assignee`, `labels`, a - gate's `await_id`, and every string value in its metadata - references it - from a step `condition` or from a step that `condition` removed, or uses it - as a `{name}` placeholder that a standalone `type = "expansion"` formula - substitutes as it builds its steps from `[[template]]`. The last - three matter because the condition filter and the `[[template]]` expansion - both run before the cook, and nothing in the cooked subgraph records - the names they consume or drop; collecting them ahead of the filter - also keeps a name's validity a property of the formula rather than of - another var's value. Anything left over cannot affect the pour, so it - is reported: `unknown variables: has_spke (available: has_spike, story)`. A - var belonging to an `--attach` proto still passes, as do a proto's - documentation handlebars. The refusal applies only where the declared-var set - is fully known, which means a proto cooked from a formula: a proto loaded from - the database (`bd cook --persist` output, and every `--attach` proto) no - longer has its `[vars]` declarations, so a declared-but-unreferenced name is - indistinguishable from a typo and nothing is refused for that pour rather than - risk breaking one that works. Out of scope for now: `bd mol bond --var` and - `bd mol seed --var` go through `formula.ValidateProvidedVars`, which only - iterates declared vars, so they still drop an unknown name silently. Also - fixed alongside: the missing-var hint printed `--var =` when there was - no missing var to name. - -- **A formula with a `waits_for` gate and no spawner to wait for is now - rejected, and an invalid formula is no longer reported as not found.** A gate - step infers its spawner from `needs[0]`, so with nothing to infer from, - cooking stamped the `gate:` label and emitted no dependency edge at - all - a gate bead that never blocks and never resolves. `bd create` already - refused `--waits-for-gate` without `--waits-for`; the formula path was the - outlier. `Formula.Validate` now enforces the same rule. Separately, a - validation failure is distinguishable from "this name is not a formula": - callers that fall back to a proto/issue lookup previously swallowed the real - error and reported `not found as issue or formula`, so an invalid formula - looked like a typo. `bd mol bond --dry-run` also resolves through - `parser.Resolve` now, so it resolves and validates the same way the real - bond does rather than previewing a bond that then errors on those checks. - Later cook transforms (inline expansions, expansions, aspects and - condition filtering) are still only exercised by the real bond. - -- **`bd prime` describes the memory split instead of prohibiting `MEMORY.md`** - ([#6111](https://github.com/gastownhall/beads/pull/6111), refs - [#5169](https://github.com/gastownhall/beads/issues/5169)). The guidance - line used to read `Do NOT use MEMORY.md files — they fragment across - accounts`. That warning predates harnesses that ship a first-party, - machine-local memory whose index file is also named `MEMORY.md`, so in a - workspace running both, prime told the agent every session not to use a file - the harness was actively maintaining. The line now says what each store is - for: durable **project** facts go in `bd remember`, because per-tool memory - files fragment across accounts; per-operator preferences stay in the - harness's own memory. Output text only, no behaviour change. The same - sentence is updated in its sibling renderings — the MCP/minimal context, the - generated AGENTS.md sections for the minimal and Codex harness templates, - and the README snippet — so `bd init` and copy-paste do not re-introduce the - prohibition. - -- **BREAKING: unforced `bd update --notes` over existing notes is now refused, - not warned** ([#5946](https://github.com/gastownhall/beads/pull/5946)). - Replacing existing non-empty notes with different non-empty content — the - blind clobber that destroys another agent's handoff notes — previously - printed a warning *after* succeeding; it now writes nothing and exits 1. The - same fence turns `PATCH /v0/beads/issues/{id}` and `batch:apply` from 200 - into 409 `notes_overwrite_refused` when `patch.notes` would do the same, on - both backends and both batch legs. Migration: pass `--force` (CLI) or - `force_notes_overwrite` (API) to overwrite deliberately — the CLI then - prints the old warning as an audit trail — or preserve history with - `--append-notes` / `bd note`. - - Two edges are deliberate. **An explicit clear is never fenced behind - `--force`**: `--notes ""` (or an API patch to the empty string) does not - trip this refusal, because every sibling text field clears the same way, - the pattern the fence exists to stop is an agent writing its own content - over someone else's, and the refusal's advice — force, or append — is - meaningless for a clear. (The CLI gives the clear its own verb, - `--clear-notes` — see the next entry — but never gates it behind - `--force`.) And - **`--force` is no longer mutually exclusive with `--if-assignee`**, since a - guarded notes overwrite has to be able to say both. This widens what - `--force` reaches under the guard: its assignee half stays suppressed (a - transfer under `--if-assignee` authorizes only through the matching CAS, - never the force bypass), but its close-policy half — closing despite open - children or a live blocker — now applies there too, a combination the CLI - previously rejected outright. `bd edit` is unaffected: it pre-fills the - editor with the current notes, a sighted edit rather than a blind clobber. - -- **`bd mol bond --ref` no longer deadlocks the molecule it bonds into, and no - longer accepts `--type conditional`.** A `--ref` arm is nested inside its - target and its hierarchical ID records that, so an ordering edge onto the - target was unsatisfiable in both directions: the arm waited for the molecule - to close, and the molecule could not close while it held an open arm. `bd - ready` went empty with nothing to point at. For a sequential bond (the - default) that edge is now dropped, so the arm carries **no ordering** and is - ready as soon as it is spawned - containment is a nested arm's only - satisfiable relationship to its container. A conditional edge has no such - degradation: it means "run only if the target fails", so dropping it would - run the arm unconditionally, turning a deadlock into a silent false dispatch. - `--ref` with `--type conditional` is therefore refused outright (including - under `--dry-run` and on the proxied route); omit `--ref` to bond a - conditional arm as a sibling, which is satisfiable and keeps its edge. - -- **`bd mol ready --gated` prints a dispatch command that exists.** The hint - named `bd sling`, which has never been a command in this binary, so the one - actionable line in the output could not be run. It now prints `bd assign - `, and says which listed molecule the example belongs - to when more than one is waiting. - -- **`bd purge` keeps closed beads a live bead depends on.** A closed bead - that a not-done bead depends on through `parent-child`, `tracks` or - `blocks` is no longer purged (reported as `live_dependent_skipped`), so a - purge that used to delete a closed molecule root under a live step now - leaves it. See the `--wisps-plane` entry under Added. - -- **`bd status` now discloses the counted rows a default `bd list` will not - show.** `Total Issues` counts the database; `bd list` shows a filtered view of - it, and the two disagreed silently — an operator reconciling a total against - a listing found rows they could not account for. A new line names the - breakdown and the flags that reveal each part: `Not shown by bd list: 2 gates - (--include-gates --all), 1 template (--include-templates --all)`, omitted - entirely when there is nothing to disclose. The counts describe the database, - every status included, which is why the remedy names `--all` beside the type - flag: the default listing hides closed rows independently of what a row is, - so a closed gate is counted here and the type flag alone would not show it. - `gate_issues` and `template_issues` join the `--json` payload and the OpenAPI - `Statistics` schema, additively; they are broken out of `TotalIssues` rather - than removed from it, so the buckets still do not sum to the total in either - direction. `bd status --assigned` gets the same breakdown, which it needs for - the same reason against `bd list --assignee`. Not yet covered: durable rows - carrying a configured infra type (GH#6439), which need the workspace's - `types.infra` set that the portable count has no seam to read. - -- **Proxied-server refusals now say *why* they refuse.** The JSON a refused - command prints gains a `reason` field next to the existing `code`, `error` - and `mutates`: `design` for a refusal that is expected to stay (shared - history, multi-repo routing, destructive admin, strict `--readonly`) and - `unimplemented` for a capability gap with a named owner. Adding `reason` left - every existing code, message and exit status as it was, and `reason` is absent - on refusals that report a runtime state rather than a policy, so existing - consumers are unaffected. The policy behind it moved into one registry - (`cmd/bd/capability_registry.go`) that every command must appear in. -- **A command with no proxied-server route now fails with a typed error** - (`proxy.store.unrouted`) instead of the bare string `proxy server store - should be uow provider`. -- **`bd backup restore --json` prints a result object on every topology** - (`{"restored": true, "source": ""}`). It previously printed nothing at - all, which left a caller unable to tell a completed restore from a silently - skipped one. This applies to direct/embedded workspaces too, not only proxied - ones. -- **`bd backup init`, `sync`, `remove` and `restore` no longer print a usage - block after a failure.** They now silence cobra's own error and usage - rendering like the rest of the CLI, so a failure is one error line on stderr - instead of the message followed by `Error: exit code 1` and the full usage - text. Exit statuses are unchanged; this applies on every topology. - -- **`bd label remove` and `bd label add` no longer claim a no-op edit** - ([#5988](https://github.com/gastownhall/beads/issues/5988)). Removing a - label the issue never had printed `✓ Removed label 'x' from `, and - adding one it already had printed `✓ Added label`, so a real edit could not - be told from a no-op. Such a label is now reported as - `• Label 'x' was not on ` / `• already has label 'x'`, with JSON - status `unchanged` instead of `removed`/`added`; a multi-label edit reports - each label by what actually happened to it. The exit code is still 0, so - idempotent callers are unaffected. - -- **Proxied `--repo` and row-cap refusals are typed again** - ([#6293](https://github.com/gastownhall/beads/pull/6293)). `bd create --repo` - under `--proxied-server` answers `--json` with the stable - `{"code": "proxy.repo.unsupported", …, "mutates": false}` on stdout rather - than prose on stderr, and `bd ready --claim --max-rows N` refuses with the - same `proxy.max_rows.unsupported` shape as `bd ready --max-rows N`: `--claim` - does not exempt a row cap the proxied route cannot enforce either way. - `bd create` is the only command that registers `--repo`, so it is the only - command this refusal applies to. -- **`bd ready --gated` runs under `--proxied-server` with a row cap set** - ([#6697](https://github.com/gastownhall/beads/pull/6697)). It was refused - whenever `--max-rows` or `BEADS_MAX_ROWS` resolved to a positive cap, while - `bd mol ready --gated` — the documented alias for the same gate-resume scan — - was not, so one command line behaved differently under its two spellings. The - gated arm lists molecules whose gate closed rather than ready rows and threads - no cap on either route, so it no longer consults one; a cap passed alongside - `--gated` is ignored, matching what the direct route has always done. Every - other `bd ready` arm still refuses an active cap, `--claim` included. -- **The proxied `bd admin compact` refusal names the command it means** - ([#6293](https://github.com/gastownhall/beads/pull/6293)). The message is now - "only 'bd admin compact --dolt' is supported in proxied-server mode"; it read - "only 'compact --dolt'", which names the root `bd compact` — a different - command, with no `--dolt` flag and its own proxied route. Scripts matching - the old text need updating. -- **`bd query` no longer silently stops at 50 rows when its output is piped** - ([#6229](https://github.com/gastownhall/beads/issues/6229)). `bd list` has - treated piped stdout as unlimited since GH#4094, but `bd query` read its - `--limit` default of 50 straight through, so `bd query '...' | consumer` - dropped every row past 50 with no hint. An unflagged `bd query` now resolves - through the same policy as `bd list`: an explicit `--limit` wins, piped - stdout is unlimited, agent mode on a terminal gets 20, and a terminal gets 50. - -- **`bd preflight` honors the `json` config default** - ([#6293](https://github.com/gastownhall/beads/pull/6293)). `json: true` in the - config file selects JSON output for `bd preflight`, as it already did - elsewhere; previously only the explicit flag was honored. -- **BREAKING: `bd update --notes ""` is now refused; clear with - `--clear-notes`** ([#6021](https://github.com/gastownhall/beads/issues/6021)). - An empty `--notes` previously wiped the whole notes field at exit 0 behind - the same `✓ Updated` receipt as a successful write — and the empty string - is exactly what a dead command substitution (`--notes "$(cat - missing.txt)"`) collapses to, the accident behind every recorded loss in - the report. At the flag layer the accident and the deliberate clear are the - same bytes, so the empty value is refused unconditionally (exit 1, naming - the alternative) and the deliberate clear gets its own verb: `bd update - --clear-notes`, mutually exclusive with `--notes` and - `--append-notes`. A verb rather than an `--allow-empty-notes` opt-in - because what needs authorizing is an intent, not an input path — the - description guard's `--allow-empty-description` unblocks stdin/file - plumbing, which notes does not have — and because an opt-in passed - habitually would silently disarm the guard, while a habitual - `--clear-notes` fails loudly on the next real update. `--force` does not - bypass the refusal (the overwrite fence answers a different question), - `--append-notes ""` remains a safe no-op, and the HTTP API is unchanged: - `patch.notes: ""` still clears — a JSON payload is a deliberate - construction, not a shell substitution. - -- **`--notes` help no longer reads as additive** - ([#6272](https://github.com/gastownhall/beads/issues/6272)). `bd update - --help` described `--notes` as "Additional notes" and the `bd prime` text - said "Add supplementary notes" — both read as appending, so agents - following the injected guidance faithfully clobbered prior context. The - update help now names the operation ("Replace the notes field …") with the - `--append-notes` contrast at the point of use, and the base/create help is - field-named like `--description` and `--design`, implying no merge - behavior. - -- **`bd gate check` resolves bead gates whose target lives in a prefix-routed - rig** ([#5859](https://github.com/gastownhall/beads/pull/5859)). After a local - miss, the evaluator follows the target bead ID through `routes.jsonl` and - reads the owning store without writing to it. This covers explicit gate - checks in embedded, server, and proxied-server command paths; the legacy - `:` await value remains accepted for compatibility. - -### Fixed - -- **`bd gate check` no longer reports an unreadable store as "pending".** The - bead arm of the check dropped the lookup error on the floor, so with dolt - down (or any backend or transport failure on the awaited bead's read) every - bead gate printed as still waiting and the command exited 0 — the same - output as a healthy, genuinely pending gate. A read of the workspace's own - store that fails for any reason other than not-found is now an error row - (`✗ : error checking - ...`), counted in the summary, and `bd close` - on such a gate keeps refusing (`could not check bead gate`, `--force` to - override) rather than letting a dead store read as satisfied. On the - classic and proxied routes alike, `bd gate check` now exits non-zero - whenever any gate it checks (gh, timer, or bead) could not be checked or - closed. A bead in a prefix-routed rig whose store cannot be read stays - pending, with the routing failure in its reason (`cannot confirm the - awaited bead is gone`); for a missing bead, see - [#6395](https://github.com/gastownhall/beads/pull/6395). - -- **`bd gate check` resolves a bead gate whose awaited bead was deleted** - ([#6395](https://github.com/gastownhall/beads/pull/6395)). A bead gate - waited for its target to close, so deleting the target left the gate - pending forever and its step blocked. The first `bd gate check` that finds - the target in this rig's own store now records the sighting in the gate's - metadata (`await_seen`), and a later check that no longer finds it there - resolves the gate (`awaited bead no longer exists`). An await ID that - no check has seen, such as a typo or a bead deleted before the first check, - stays pending with a diagnostic instead of unblocking the step: correct the - await ID or close the gate with `bd gate resolve`. A miss outside this - rig's store (a target whose prefix routes to another rig, or a contributor - auto-routed store) does not prove the bead gone, so the gate stays pending - and the reason names the cause. `bd close` on a bead gate follows the same - rule: it no longer needs `--force` once an earlier check saw the awaited - bead and the bead is gone from the gate's rig, with no route pointing - elsewhere. `bd rename` and `bd rename-prefix` point a bead gate at its - bead's new ID, sighting included, so a renamed bead does not read as - deleted, even to a check or `bd close` that read the gate before the - rename; a rename that fails partway leaves the gate pending without a - sighting, never resolved. A rename by an older bd does not move the - gate: if a check had already seen the bead, a later check or `bd close` - treats that rename as a deletion and closes the gate while the bead is - still open, so upgrade every client that renames beads together with the - ones that check gates. - -- **An epic that blocks on its own children no longer hides them from `bd - ready`** ([#6506](https://github.com/gastownhall/beads/issues/6506)). A - parent-child edge now propagates only a parent's *exogenous* blockedness — - blockedness whose cause lies outside the parent's own subtree — so the - close-gate idiom, a parent carrying `blocks` edges onto its own children (or - grandchildren) so it cannot close before them, stops darkening the very work - it is waiting for. The parent itself stays blocked, and a close gate does not - unlock the work under an epic that is itself blocked from outside: a sub-epic - blocked BOTH by its own children AND by an exogenously blocked ancestor still - darkens its children - ([#6601](https://github.com/gastownhall/beads/issues/6601)). - "Inside my own subtree" is decided to a fixed depth of **four** - parent-child levels — epic → sub-epic → leg → task — so a parent that - blocks on something deeper than that still darkens its whole subtree, exactly - as it did before this fix; that depth is a measured trade (the - bounded walk is a fixed chain of index probes, where a recursive one cost - 7.5x per evaluation) and raising it is a one-constant change. Existing stores - repair on the next write that touches the hierarchy, or with - `bd doctor --fix`. -- `bd dep add` no longer explains its refusal of a blocking edge onto your own - descendant by claiming the block would cascade down and never clear — it - does not, as of the fix above. The refusal stands, and now names the - sanctioned way to say it: a waits-for gate over the children. - -- **`routes.jsonl` prefixes containing a hyphen now route** - ([#5048](https://github.com/gastownhall/beads/issues/5048)). Prefix routing - cut the bead ID at its first `-` and required an exact match, so a route - such as `claude-os-` could never match `claude-os-76l` and the lookup fell - through to "not found". Routing now picks the longest route prefix the ID - starts with. Single-hyphen routes behave as before; where both `hq-` and - `hq-cv-` are configured, `hq-cv-*` IDs now take the `hq-cv-` route instead - of `hq-`. + without starting either. The `--json` payload changes shape to carry that — + see **Changed** above before parsing it. - **A dotted config key now round-trips: what `bd config set` writes, `bd config get` and bd's own readers find** - ([#6574](https://github.com/gastownhall/beads/pull/6574), - [#6594](https://github.com/gastownhall/beads/issues/6594), bd-zj95). Setting a + ([#6578](https://github.com/gastownhall/beads/pull/6578), bd-zj95). Setting a key like `sync.remote` or `dolt.host` into a `config.yaml` with no matching section appended a key whose *name* contained the dot — a top-level `sync.remote: "..."` — instead of nesting it under `sync:`. Viper finds a key @@ -2877,120 +824,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 walks nested mappings, so it does not. The value was set and invisible at the same time depending on which reader asked, and `bd init --remote` in a fresh workspace was the ordinary way in: the remote was recorded and then not found. - New dotted keys are written nested now. An existing flat spelling is updated - in place for compatibility with other writers of the shared config file — - rewriting that one line and nothing else, the comment on it included — and - bd's direct reader recognizes both forms with the same precedence as Viper. - `bd doctor` warns when both forms coexist. `bd config unset` removes the - nested form — it only ever matched the flat spelling, so unsetting + Dotted keys are written nested now, and `bd config unset` removes the nested + form — it only ever matched the flat spelling, so unsetting `sync.remote` silently left the remote live. Keys the write does not own, including dotted ones somebody else put there, are left exactly as they are. - - Four things that used to succeed now report an error instead, because each one - produced a value no reader could see, or claimed to remove a value it had - left live. On `bd config set`: a key under a parent that already holds a value - (`sync: enabled`, then `bd config set sync.remote`), and a key in a file whose - top level is not a mapping. On `bd config unset`: a key inside a flow-style - mapping (`sync: {remote: "..."}`), which bd matches by line and so cannot - edit, and a key whose value is a block scalar (`remote: |`, in either - spelling), where commenting the key out would leave the body behind as a value - of its own. Unsetting a key that is not set remains a successful no-op in - every shape. -- **A self-hosted GitLab no longer re-creates its own issues on every push** - ([#6735](https://github.com/gastownhall/beads/issues/6735)). The tracker - decided whether a stored `external_ref` was one of its own by looking for - the substring "gitlab" in the URL, so an instance whose hostname does not - contain it — `https://nova.teachx.ai`, say — had its own issue and - work-item URLs classified as foreign. `bd gitlab push` then took the - create-not-update path for issues that were already synced, and pull - reconciliation and conflict detection skipped them. Such a URL is now also - recognized when its host, and base path for a GitLab served from a - sub-path, match the configured `gitlab.url`. - - Recognition only widens: every ref the substring check already claimed is - classified exactly as before, so no existing link changes meaning. Two - limits are worth knowing. Matching is host-scoped, not project-scoped — with - GitLab at the host root, a ref pointing at another project on the same host - now counts as this tracker's, as has always been the case for `gitlab.com`. - And the host is compared literally, so `gitlab.url` must name it the way - GitLab spells it in the `web_url`s it returns; writing an explicit default - port (`https://host:443`) matches none of them and leaves the old behavior - in place. - -### Changed - -- **`bd ready --json` answers a capped page and its total in one pass, and - `storage.Storage` gains a required `GetReadyWorkWithCountsAndTotal` method** - ([#6731](https://github.com/gastownhall/beads/pull/6731)). A full capped page - used to trigger a second counting pass in its own transaction, re-running the - expired-defer sweep and every wisp/deferred probe; the total now comes from - `COUNT(*) OVER ()` on the page's own query inside the same read transaction. - Two behaviors improve with it: the `--json` total honors external-dependency - exclusions (the old second pass counted externally blocked issues, so text and - `--json` could report different numbers), and the page and its total now come - from one snapshot instead of two, so "Showing N of M" can no longer be - internally inconsistent under concurrent writes. One contract change: a total - that fails now fails the command, where it previously could not — the total is - taken from the page's own query, so the page would have failed anyway. - - **Out-of-tree storage backends must implement the new method to compile.** - Every in-tree implementer is updated, but `internal/storage/backends` is an - extension seam whose registrants are supplied by a downstream distribution and - return `storage.DoltStorage` directly. A registrant must implement - `GetReadyWorkWithCountsAndTotal(ctx, types.WorkFilter) ([]*types.IssueWithCounts, int, error)`: - the page identical to `GetReadyWorkWithCounts`, and the total identical to the - number `CountReadyWork` answers for the same filter (which ignores `Limit`), - resolved in the page's own read transaction. There is - deliberately no optional-interface fallback — a store that cannot size the - ready set should fail to compile rather than silently fall back to an - unbounded query. - -- **`storage.Storage` gains a required `SearchIssueSummaries` method** - ([#3458](https://github.com/gastownhall/beads/pull/3458)). It is a - narrow-projection variant of `SearchIssues` for list-shaped rendering: it - returns `[]*types.IssueSummary` (exported as `backend.IssueSummary`), which - carries the list columns, labels, and the four wisp-plane markers but none of - the TEXT/JSON columns. Nothing in `bd` calls it yet, so no command's output - changes. Consumers that only *call* the interface are unaffected; any external - type that *implements* it (a custom store, mock, or proxy, or an - `internal/storage/backends` registrant) must add the method to compile: - `SearchIssueSummaries(ctx context.Context, query string, filter types.IssueFilter) ([]*types.IssueSummary, error)`. - It honors `SortBy`/`SortDesc` and `SkipLabels` exactly as `SearchIssues` - does, `IncludeDependencies` is a silent no-op (a summary has nowhere to put - dependency records), and wisps are merged in unless `SkipWisps` is set, with - their markers set so a wisp row stays distinguishable from a durable issue. - -- **Push `--dry-run` now honors `--create-only`** - ([#6337](https://github.com/gastownhall/beads/issues/6337)). The sequential - tracker push previewed "Would update" for every already-linked issue, even - though a real `--create-only` run skips them, so the preview over-reported - updates and under-reported skips. This affected `bd jira sync` and - `bd linear sync` with `--push --create-only --dry-run`. The dry-run now - applies the same `--create-only` gate as the real run. Linear's dry-run also - no longer counts an issue its batch filter skipped (create-only, a `ShouldPush` - hook, parent, type, or conflict) as skipped twice. Notion's batch dry-run was already correct. - -- **`bd config unset` no longer reports success while the key stays set, and - now names where the unset actually landed.** A key present in - `config.yaml` but not claimed by `IsYamlOnlyKey` was deleted from the - database only, so it stayed effective; it is now cleared from both, and the - reported location - `Unset (in database, config.yaml)` - is derived - from what the writes actually changed rather than from a pre-check of viper's - merged value, which counted defaults and environment variables as present in - the file. A yaml-only key that was not in the file prints ` was not set - in config.yaml` instead of claiming a write. A workspace with no project - `config.yaml` at all is an answer rather than a failure, so a database-backed - unset there no longer deletes the row and then exits non-zero. Unsetting a - key whose value is the indented block beneath it (a mapping or a list), which - used to comment out the key line and orphan the block into a `config.yaml` - that no longer parsed, is now refused alongside the flow-style and - block-scalar shapes above. An unset also keeps the file's trailing newline, - and an unset that changes nothing now leaves the file byte-identical - on a - CRLF `config.yaml` it previously reported a write for a key that was never - there and rewrote every line ending to LF. The stderr side-effect hint is - gated on the write too, so a no-op no longer announces a removal it did not - make, and the `--json` payload for a yaml-only key carries a `changed` boolean - so the machine branch can tell an absent key from an unpopulated field. + Two consequences of the nested write — the flat-key migration and two new + failure exits — are under **Changed** above. ## [1.3.0] - 2026-09-15 @@ -3269,22 +1108,6 @@ which dumps the entire release history.) `BD_ALLOW_REMOTE_MIGRATE=1` in scripted use. Reads keep working on the current schema meanwhile. Embedded (single-user) databases still auto-migrate silently, unchanged. (#5920) -- **Commands that cannot work under `--proxied-server` now refuse before any - provider opens** ([#6293](https://github.com/gastownhall/beads/pull/6293)). - `bd doctor`, `bd backup`, `bd restore`, `bd diff`, `bd migrate`, `bd branch`, - `bd conflicts`, `bd vc`, `bd federation`, `bd repo`, `bd flatten`, `bd sync`, - `bd dolt push|pull|commit`, `bd dolt remote add|list|reset-data`, and - `bd admin compact` without `--dolt` return a typed refusal carrying a stable - code (`proxy.doctor.unsupported` and friends) instead of failing partway - through, or silently doing nothing. Two changes worth planning for: - - `bd doctor` under `--proxied-server` used to print a note to stderr and - exit **0**; it now exits **1** with `proxy.doctor.unsupported`. A script - that treated bare `bd doctor` as a no-op in this mode needs to stop calling - it, or tolerate the non-zero status. - - Under `--json`, a refusal is strict JSON on **stdout** - (`{"code": …, "error": …, "mutates": false}`) rather than prose on stderr, - so a wrapper parsing stdout gets a machine-readable answer and - `"mutates": false` states that the refusal touched nothing. - The smart migration gate's "auto-migrate as safe first-mover" and auto-fast-forward arms are now embedded-only. On a shared server the gate diff --git a/CLAUDE.md b/CLAUDE.md deleted file mode 120000 index 47dc3e3d8..000000000 --- a/CLAUDE.md +++ /dev/null @@ -1 +0,0 @@ -AGENTS.md \ No newline at end of file diff --git a/CLAUDE.md b/CLAUDE.md new file mode 100644 index 000000000..9f08e2027 --- /dev/null +++ b/CLAUDE.md @@ -0,0 +1,23 @@ +# Claude Code Entry Point for Beads + +This file is intentionally short. Do not copy workflow, build, storage, or UI +rules here; those details drift quickly when repeated across agent entrypoints. + +## Read First + +- **Workflow and safety**: [AGENTS.md](AGENTS.md) +- **Detailed agent operations**: [AGENT_INSTRUCTIONS.md](AGENT_INSTRUCTIONS.md) +- **Architecture orientation**: [engdocs/CLAUDE.md](engdocs/CLAUDE.md) +- **PR maintenance policy**: [PR_MAINTAINER_GUIDELINES.md](PR_MAINTAINER_GUIDELINES.md) + +## Current Ground Rules + +- Run `bd prime` before doing tracked work. +- Follow `go.mod` and [AGENT_INSTRUCTIONS.md](AGENT_INSTRUCTIONS.md) for build + and test commands; do not hard-code toolchain versions here. +- Beads uses Dolt as the issue database. Use `bd dolt push` / `bd dolt pull` + for issue data sync; do not use export/import as a routine git workflow. +- The CLI Visual Design System lives in + [AGENT_INSTRUCTIONS.md](AGENT_INSTRUCTIONS.md#visual-design-system). +- If this file conflicts with a linked source, trust the linked source and fix + this file by removing the duplicate. diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index daca0d8ab..ba3288a57 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -2,65 +2,14 @@ Thank you for your interest in contributing to bd! This document provides guidelines and instructions for contributing. -## Issues and pull requests - -GitHub Issues is the public tracker. Use an issue when it adds context -reviewers need: a user-visible bug, a behavior or design change worth -discussing first, or work that spans several pull requests. The issue holds -why the change is needed, what it affects, and how we will know it works; file -it with the bug or feature form before or alongside your pull request, and it -does not need maintainer approval first. Small, self-explanatory changes -(typos, flaky tests, refactors, CI or docs tweaks) can go straight to a pull -request whose description explains the why. - -### Triage labels - -| Label | Meaning | Applied by | -|---|---|---| -| `status/needs-triage` | Awaiting initial triage. | Automation, on every new issue | -| `status/needs-info` | Waiting on essential information from the reporter. | Maintainers | -| `status/needs-repro` | Needs a reproducible bug report. | Maintainers | -| `status/needs-design` | The direction needs a design decision before work starts. | Maintainers | -| `status/accepted` | Confirmed and on our radar. | Maintainers | - -Issues left in `status/needs-info` or `status/needs-repro` are closed after 14 -days without a reply to the request; reply with the details and a -maintainer will reopen them. -Priority (`priority/p0`–`priority/p3`) and kind (`kind/bug`, `kind/feature`, -`kind/docs`, `kind/chore`) labels are set during triage. - -### Pull request pipeline labels - -These labels drive the maintainers' automated review and merge workflow. They -are applied by maintainers and automation; contributors don't need to set -them. - -| Label | Meaning | -|---|---| -| `status/needs-review` | Request the PR review workflow. | -| `status/needs-review-auto` | Request the automated PR review workflow. | -| `status/reviewing` | The PR review workflow is running. | -| `status/review-failed` | The PR review workflow failed before merge-ready. | -| `status/merge-ready` | The PR is ready for the merge workflow. | -| `status/merge-queued` | Queued for deterministic PR-review merge. | -| `status/merge-failed` | The merge queue needs operator attention. | -| `status/needs-bugflow` | Request the bugflow investigation workflow. | - -The current priorities are on the [roadmap](ROADMAP.md); work in a priority -area is reviewed first. - ## Development Setup ### Prerequisites -- [Bazelisk](https://github.com/bazelbuild/bazelisk), installed as `bazel` - (it reads `.bazelversion`). **Required:** Bazel is the build and test system - CI gates on, and the pre-commit (nogo lint) and pre-push (test suite) hooks - run it. -- Go (see `go.mod` for the required version; currently 1.26+), for `make - install` and the `go test` inner loop +- Go (see `go.mod` for the required version; currently 1.26+) - Git - A C compiler (CGO is required for the embedded Dolt database) +- golangci-lint v2.10.1 for the required local lint gate - ICU headers are **not required** for building -- see [engdocs/ICU-POLICY.md](engdocs/ICU-POLICY.md) ### Getting Started @@ -70,44 +19,16 @@ area is reviewed first. git clone https://github.com/gastownhall/beads cd beads -# Read rbe-west's anonymous, read-only cache: everything CI already built and -# tested is a cache hit, and nothing you build is uploaded. -echo 'build --config=fork-cache' >> .bazelrc.local +# Build the project (uses gms_pure_go tag via Makefile) +make build -# Run the test suite CI gates on (bazel test //... --config=ci) +# Run tests (uses correct build tags automatically) make test -# Build and install bd to ~/.local/bin (also enables the git hooks) +# Build and install locally to ~/.local/bin make install ``` -### Building and Testing - -Bazel is the gate: `.github/workflows/bazel.yml` runs every pull request's -tests as `bazel test` lanes, and nogo (lint + vet), gofmt and the repository -guards exist only as Bazel targets. The make targets run the same commands: - -| Command | Runs | -|---|---| -| `make test` | `bazel test //... --config=ci`: the test lane (unit tests, nogo, gofmt, repository guards) | -| `make check` | the testing.Short policy, `make ci-pr-lint` and `make test` | -| `make ci-pr-lint` | nogo natively plus the windows/amd64 and darwin/arm64 passes | -| `make check-docs` | the Bazel docsync and doc-freshness tests, then the CLI flag check | -| `bazel test //... --config=integration` | the integration lane; [engdocs/TESTING.md](engdocs/TESTING.md) lists every other lane's command | - -Where actions run is your choice, set in the gitignored `.bazelrc.local` (or -per command with `make test BAZEL_FLAGS=--config=...`): - -- `--config=fork-cache` (contributors): reads the anonymous cache, runs misses - on your machine, uploads nothing. -- `--config=remote-exec` (maintainers with an rbe-west client certificate): - executes remotely; the executor and TLS lines stay in your `user.bazelrc` or - `.bazelrc.local`. - -`go test` (`./scripts/test.sh`, or `make test-go` / `make check-go` / -`make check-docs-go`) still works as an inner-loop convenience, but CI does not -enforce it and it skips nogo, gofmt and the guards. Finish with `make test`. - ## Project Structure ``` @@ -117,7 +38,7 @@ beads/ │ ├── types/ # Core data types (Issue, Dependency, etc.) │ └── storage/ # Storage interface and implementations │ └── dolt/ # Dolt database backend -├── .golangci.yml # Linter configuration (applied by nogo, tools/nogo) +├── .golangci.yml # Linter configuration └── .github/workflows/ # CI/CD pipelines ``` @@ -140,24 +61,23 @@ We follow standard Go conventions: ### Linting -Lint and vet run as nogo under Bazel: go test's vet checks plus the -golangci-lint linters `.golangci.yml` enables, the same analyzers CI gates on. +Use the same pinned golangci-lint version and repository-owned wrapper as CI: ```bash -# Run the required lint and vet gate (native, windows and darwin) -make ci-pr-lint +# Install the version pinned by CI +go install github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.10.1 -# Faster: only the Bazel packages of your changed Go files -make lint-changed +# Run the required formatting and lint contract +make ci-pr-lint ``` -`make ci-pr-lint` must pass with zero issues. It analyzes the repository's -normal `gms_pure_go` build and cross-checks the Windows- and macOS-only -non-cgo code. Accepted intentional patterns are encoded narrowly in -`.golangci.yml`; do not ignore a failing baseline. See -[engdocs/LINTING.md](engdocs/LINTING.md) for the full policy. +`make ci-pr-lint` must pass with zero issues. It checks formatting, lints the +repository's normal `gms_pure_go` build, and cross-lints Windows-only non-CGO +code. Accepted intentional patterns are encoded narrowly in `.golangci.yml`; +do not ignore a failing baseline. See [engdocs/LINTING.md](engdocs/LINTING.md) +for the full policy. -CI runs the same analyzers on all pull requests, in the Bazel test lane. +CI runs the same required wrapper on all pull requests. ## Making Changes @@ -170,23 +90,21 @@ engine, or expand the database schema when issue metadata is sufficient. ### Workflow -1. If the change warrants one, find or file an issue (see [Issues and pull requests](#issues-and-pull-requests)) -2. Fork the repository and create a feature branch (`git checkout -b feature/my-feature`) +1. Fork the repository +2. Create a feature branch (`git checkout -b feature/my-feature`) 3. Make your changes 4. Add tests for new functionality 5. Run tests and linter locally 6. Commit your changes with clear messages 7. Push to your fork -8. Open a pull request that explains the change and says `Closes #` when there is one +8. Open a pull request ### Commit Messages -Use [Conventional Commits](https://www.conventionalcommits.org/): -`type(scope): summary`, where type is one of `fix`, `feat`, `docs`, `test`, -`refactor`, `chore`, `ci`, or `perf`. +Write clear, concise commit messages: ``` -feat(dep): add cycle detection for dependency graphs +Add cycle detection for dependency graphs - Implement recursive CTE-based cycle detection - Add tests for simple and complex cycles @@ -197,10 +115,6 @@ feat(dep): add cycle detection for dependency graphs **One issue per PR, and one PR per issue.** No piggybacking or riders — each PR should address exactly one thing. -Read [CONTRIBUTING_PR_GUIDELINES.md](CONTRIBUTING_PR_GUIDELINES.md) for the -layering rules (schema → storage/issueops → cmd/bd, one layer per PR) and the -repro and benchmark evidence reviewers expect. - - Keep PRs focused on a single feature or fix - Do not include unrelated changes, cleanup, or "while I'm here" improvements - Do not include `.beads/` data (database, JSONL) in your PR @@ -224,8 +138,7 @@ For test commands, test design, and PR-readiness gates, see the canonical - Follow the proportional validation budget in [engdocs/TESTING.md](engdocs/TESTING.md): docs-only changes use docs checks; - Go changes use focused and affected-package tests plus one final `make test` - (the Bazel test lane). + Go changes use focused and affected-package tests plus one final `make test`. - If you hit a test failure unrelated to your change, don't silently skip it -- check `.test-skip` and file an issue if it's not already tracked (see [engdocs/TESTING.md](engdocs/TESTING.md#failures-skips-and-review)). @@ -265,12 +178,22 @@ round-trip paths should pattern-match on those tests. ## Feature Requests and Bug Reports -Use the issue forms: [bug report](https://github.com/gastownhall/beads/issues/new?template=bug_report.yml) -or [feature request](https://github.com/gastownhall/beads/issues/new?template=feature_request.yml). -The feature form asks for the motivation, impact, risk and compatibility, -and verification plan up front, so a maintainer can accept the issue without -a round trip. Questions go to -[Discussions](https://github.com/gastownhall/beads/discussions). +### Reporting Bugs + +Include in your bug report: +- Steps to reproduce +- Expected behavior +- Actual behavior +- Version of bd (`bd version` if implemented) +- Operating system and Go version + +### Feature Requests + +When proposing new features: +- Explain the use case +- Describe the proposed solution +- Consider backwards compatibility +- Discuss alternatives you've considered ## Your PR Will Not Be Overwritten @@ -368,7 +291,7 @@ docker run --rm -v $(pwd):/workspace -w /workspace nixos/nix \ If the build fails with a `vendorHash` mismatch, run `./scripts/update-nix-vendorhash.sh` to recompute and update `default.nix`, or update it manually with the `got:` hash from the error message and rebuild. -On a PR, this is covered by PR Risk's required `test-nix` job (`.github/workflows/pr-risk.yml`), which runs `nix run .#default -- --help` plus `nix flake check -L` (an evaluation of every flake output; the flake has no test checks) on every PR touching `go.mod`, `go.sum`, `default.nix`, `flake.nix`, or `flake.lock` -- a superset of plain `nix build .#default`. `.github/workflows/nix-build.yml` dropped its own `pull_request` trigger as redundant (F7c, spec-f7.md §2.4) and now only runs `nix build .#default` on push to `main` and on `workflow_dispatch`, so dependabot bumps that invalidate `vendorHash` still fail loudly post-merge instead of silently breaking Nix users on main. For dependabot Go-module bumps specifically, `.github/workflows/update-vendor-hash.yml` runs the same `update-nix-vendorhash.sh` script and pushes the hash bump back to the dependabot branch automatically (note: GitHub does not retrigger `pull_request` workflows for `GITHUB_TOKEN`-authored commits, so a maintainer may need to re-run PR Risk's `test-nix` once after the auto-fix push to mark the gate green). +The `nix build` CI job (`.github/workflows/nix-build.yml`) runs on any PR that touches `go.mod`, `go.sum`, `default.nix`, `flake.nix`, or `flake.lock`, so dependabot bumps that invalidate `vendorHash` fail loudly instead of silently breaking Nix users on main. For dependabot Go-module bumps specifically, `.github/workflows/update-vendor-hash.yml` runs the same `update-nix-vendorhash.sh` script and pushes the hash bump back to the dependabot branch automatically (note: GitHub does not retrigger `pull_request` workflows for `GITHUB_TOKEN`-authored commits, so a maintainer may need to re-run `nix build .#default` once after the auto-fix push to mark the gate green). ### Debugging @@ -386,15 +309,11 @@ dlv debug ./cmd/bd -- create "Test issue" (For maintainers) -Follow [RELEASING.md](RELEASING.md); it is the canonical release process. - -The pre-push version gate requires Go and validates each `v*` release tag -against the checkout's canonical version. A batch containing different release -versions is refused; push only the tag matching this checkout. - -`bd preflight` finds the nearest Beads source module by walking ancestor -directories, checking both source markers and the module identity. Its version -check runs directly in Go; `scripts/check-versions.sh` remains a Bash entrypoint. +1. Update version in code +2. Update CHANGELOG.md +3. Tag release: `git tag v0.x.0` +4. Push tag: `git push origin v0.x.0` +5. GitHub Actions will build and publish ## Questions? diff --git a/MODULE.bazel b/MODULE.bazel deleted file mode 100644 index e157d6d59..000000000 --- a/MODULE.bazel +++ /dev/null @@ -1,417 +0,0 @@ -module( - name = "beads", - version = "0.0.0", -) - -bazel_dep(name = "platforms", version = "1.1.0") -bazel_dep(name = "rules_go", version = "0.63.0") - -# nogo honors //nolint directives through ast.CommentMap ranges, which differ -# from golangci-lint's (a comment trailing `func f(...) {` attaches to the -# body's first statement). This tree's directives were written against -# golangci-lint, so the patch adds golangci-lint's ranges as well. -single_version_override( - module_name = "rules_go", - patch_strip = 1, - patches = ["//third_party/patches:rules_go_nogo_golangci_nolint.patch"], - version = "0.63.0", -) - -bazel_dep(name = "gazelle", version = "0.53.0") -bazel_dep(name = "rules_shell", version = "0.6.1") -bazel_dep(name = "toolchains_llvm", version = "1.11.0") - -# Loaded by @llvm_dist_linux_x86_64's BUILD file (toolchains_llvm's template). -bazel_dep(name = "rules_cc", version = "0.2.17") - -# Hermetic C/C++ toolchain, ported from gastownhall/gascity. rules_go builds -# the Go stdlib under -race and every cgo package (mattn/go-sqlite3, gozstd, -# ...) with the registered C toolchain, and every Go compile depends on the -# stdlib, so a host-detected toolchain (local_config_cc: CC=/usr/bin/gcc, -# -fuse-ld=lld only where the CLIENT has lld, gcc-version-specific generated -# inputs) keys those actions to the machine that ran the analysis and fails -# remotely on workers without lld. Instead: LLVM pinned by sha256, plus a -# sysroot of pinned Ubuntu 24.04 packages (the RBE workers' release, so -# binaries load the same glibc and libstdc++ there), byte-identical on every -# Linux x86_64 host. No ICU: beads builds go-mysql-server with its pure-Go -# regex backend (engdocs/ICU-POLICY.md), so nothing may link libicu. .bazelrc -# turns host detection off so nothing falls back to the host compiler; -# scripts/bazel_hermetic_cc_test.go guards both. -deb_sysroot = use_repo_rule("//third_party/cc_toolchain:deb_sysroot.bzl", "deb_sysroot") - -# Where every pinned Ubuntu 24.04 package below comes from, tried in order -# (the sha256 pins, not the source, decide what is accepted): -# - gascity's content-addressed mirror, a release holding each pinned -# .deb as .deb (gascity's tools/cc_toolchain/mirror_debs.sh -# publishes it; gascity pins the same packages), so a cold fetch does not -# depend on Canonical's servers; -# - the snapshot these packages were pinned from (immutable, but a single -# host that returns 503 for minutes at a time); -# - Launchpad's librarian, which keeps every file ever published to the -# primary archive, superseded versions included; -# - archive.ubuntu.com, which drops superseded versions. -NOBLE_DEB_URLS = [ - "https://github.com/gastownhall/gascity/releases/download/toolchain-noble-debs-20261001/{sha256}.deb", - "https://snapshot.ubuntu.com/ubuntu/20261001T000000Z/{path}", - "https://launchpad.net/ubuntu/+archive/primary/+files/{file}", - "https://archive.ubuntu.com/ubuntu/{path}", -] - -deb_sysroot( - name = "cc_sysroot_noble_amd64", - packages = { - "pool/main/g/gcc-13/libgcc-13-dev_13.3.0-6ubuntu2~24.04.1_amd64.deb": "cd689db2691edaa10f37329307292796bb599e722e0505c79e14caaa1fe9a93a", - "pool/main/g/gcc-13/libstdc++-13-dev_13.3.0-6ubuntu2~24.04.1_amd64.deb": "ee5633e863e19c3381ed97842ce35ed32ede96a3d1ae4e94c051d3036fe21347", - "pool/main/g/gcc-14/libgcc-s1_14.2.0-4ubuntu2~24.04.1_amd64.deb": "aa7fadbe33b78bcf99885318040601c550c208929565b179891d9a3cc2aa68cd", - "pool/main/g/gcc-14/libstdc++6_14.2.0-4ubuntu2~24.04.1_amd64.deb": "a51f8de7829211db961a31f02158058ad1a95f92ac6d0a5dff6350e2821c54c0", - "pool/main/g/glibc/libc6-dev_2.39-0ubuntu8.9_amd64.deb": "e13d5fcc1b2a86f75bca8e0026a8e39f24fe97ca86e92be79991f8697eb1306f", - "pool/main/g/glibc/libc6_2.39-0ubuntu8.9_amd64.deb": "ff5557d99b51f761c4b7c92368b9cc45565eda17df9bf9eb4b134d09825008be", - "pool/main/l/linux/linux-libc-dev_6.8.0-146.146_amd64.deb": "1a7bb04132d01cd14bea3d615e5861096ca1f70c4659902eb14b36c21993cadc", - }, - remove = [ - "etc", - "usr/bin", - "usr/sbin", - "usr/share", - "usr/lib/x86_64-linux-gnu/gconv", - "usr/lib/gcc/x86_64-linux-gnu/13/crtoffload*", - "usr/lib/gcc/x86_64-linux-gnu/13/lib*san*", - "usr/lib/gcc/x86_64-linux-gnu/13/libatomic*", - "usr/lib/gcc/x86_64-linux-gnu/13/libbacktrace*", - "usr/lib/gcc/x86_64-linux-gnu/13/libgcov*", - "usr/lib/gcc/x86_64-linux-gnu/13/libgomp*", - "usr/lib/gcc/x86_64-linux-gnu/13/libitm*", - "usr/lib/gcc/x86_64-linux-gnu/13/libquadmath*", - "usr/lib/gcc/x86_64-linux-gnu/13/libstdc++*.a", - ], - urls = NOBLE_DEB_URLS, -) - -LLVM_VERSION = "22.1.8" - -llvm_dist = use_repo_rule("//third_party/cc_toolchain:llvm_dist.bzl", "llvm_dist") - -llvm_dist( - name = "llvm_dist_linux_x86_64", - llvm_version = LLVM_VERSION, - members = [ - "bin/clang", - "bin/clang++", - "bin/clang-22", - "bin/clang-cpp", - "bin/ld.lld", - "bin/ld64.lld", - "bin/lld", - "bin/llvm-ar", - "bin/llvm-as", - "bin/llvm-cov", - "bin/llvm-install-name-tool", - "bin/llvm-nm", - "bin/llvm-objcopy", - "bin/llvm-objdump", - "bin/llvm-profdata", - "bin/llvm-ranlib", - "bin/llvm-readelf", - "bin/llvm-readobj", - "bin/llvm-strip", - "bin/llvm-symbolizer", - "bin/wasm-ld", - "include/c++", - "include/x86_64-unknown-linux-gnu", - "lib/clang/22/include", - "lib/clang/22/lib/x86_64-unknown-linux-gnu/clang_rt.crtbegin.o", - "lib/clang/22/lib/x86_64-unknown-linux-gnu/clang_rt.crtend.o", - "lib/clang/22/lib/x86_64-unknown-linux-gnu/libclang_rt.builtins.a", - # `bazel coverage` links cgo binaries with -fprofile-instr-generate, - # which pulls in compiler-rt's profile runtime (~170KB). - "lib/clang/22/lib/x86_64-unknown-linux-gnu/libclang_rt.profile.a", - ], - # LLVM-22.1.8-Linux-X64.tar.xz (sha256 df0e1ecf...0006384), sliced to the - # members above by tools/cc_toolchain/repack_llvm.sh: 149 MB instead of - # 1.94 GB, and no single-threaded xz pass on every cold runner. The files - # are byte-identical to the release's, so no action key changes. - # tools/cc_toolchain/llvm_members.txt must list the same members. - sha256 = "de6147773999137b70ef3d7bd8ecb1e5b91f2a161d02fd09aec591b84516bec5", - sliced = True, - stub_tools = [ - "clang-format", - "clang-tidy", - "clangd", - "llvm-dwp", - ], - # The sha256 pin, not the host, guarantees the bytes. gascity's release - # asset for now; a beads mirror of the same bytes can be listed first. - urls = [ - "https://github.com/gastownhall/gascity/releases/download/toolchain-llvm-22.1.8-slice-1/LLVM-22.1.8-Linux-X64-slice.tar.zst", - ], -) - -# Every other host (macOS arm64, Linux arm64) runs toolchains_llvm's stock -# LLVM_VERSION release for its platform, pinned by sha256 in toolchains_llvm's -# distribution table and fetched only there. Once any toolchain_root is set, -# toolchains_llvm creates no stock distribution of its own and fails every -# build on a host no root covers. Not hermetic there: no sysroot, so cgo -# uses the host's headers and libraries, as before. -llvm_host_dist = use_repo_rule("@toolchains_llvm//toolchain:rules.bzl", "llvm") - -llvm_host_dist( - name = "llvm_dist_host", - llvm_version = LLVM_VERSION, - # The bare rule reads the per-host dict; llvm_toolchain's macro would - # fill it in from llvm_version. - llvm_versions = {"": LLVM_VERSION}, -) - -llvm = use_extension("@toolchains_llvm//toolchain/extensions:llvm.bzl", "llvm") -llvm.toolchain( - name = "llvm_toolchain", - # Pair Darwin's host SDK with its native linker. LLVM 22.1.8's ld64.lld - # cannot parse the arm64e.x1 TAPI targets in the macOS 27 SDK. Keep the - # pinned Clang compiler; Linux keeps the bundled LLD and pinned sysroot. - linker = { - "darwin-aarch64": "auto", - "darwin-x86_64": "auto", - }, - llvm_version = LLVM_VERSION, - # The sysroot's libstdc++, linked dynamically as host-gcc builds did. - stdlib = {"linux-x86_64": "dynamic-stdc++"}, -) -llvm.toolchain_root( - name = "llvm_toolchain", - label = "@llvm_dist_linux_x86_64//:BUILD.bazel", - targets = ["linux-x86_64"], -) -llvm.toolchain_root( - name = "llvm_toolchain", - label = "@llvm_dist_host//:BUILD.bazel", -) -llvm.sysroot( - name = "llvm_toolchain", - label = "@cc_sysroot_noble_amd64//:sysroot", - targets = ["linux-x86_64"], -) -use_repo(llvm, "llvm_toolchain") - -register_toolchains("@llvm_toolchain//:all") - -go_sdk = use_extension("@rules_go//go:extensions.bzl", "go_sdk") -go_sdk.download( - name = "go_sdk", - version = "1.26.7", -) -use_repo(go_sdk, "go_sdk") - -# nogo: //tools/nogo's analyzers (go test's vet checks plus the golangci-lint -# linters .golangci.yml enables) run beside every first-party Go compile, so -# a finding fails `bazel build`/`bazel test` wherever it executes (RBE -# included). External repos only feed analyzer facts; they are not validated. -go_sdk.nogo( - includes = ["//:__subpackages__"], - nogo = "//tools/nogo", -) - -# gazelle compiles fetch_repo and gazelle, which every go_deps repository -# runs, with `go build` in a repository rule before any go_deps fetch can -# start (18-24 s of every CI lane's loading). This substitute serves those -# reproducible binaries from the repository cache by pinned sha256 and builds -# them from source on a miss (tools/bazel/go_repository_tools.bzl). -gazelle_non_module_deps = use_extension("@gazelle//internal/bzlmod:non_module_deps.bzl", "non_module_deps") - -go_repository_tools = use_repo_rule("//tools/bazel:go_repository_tools.bzl", "go_repository_tools") - -go_repository_tools(name = "gazelle_go_repository_tools") - -override_repo( - gazelle_non_module_deps, - bazel_gazelle_go_repository_tools = "gazelle_go_repository_tools", -) - -# gazelle.go.work joins the beads module and the nogo analyzers' module -# (tools/nogo/go.mod) into one dependency graph for Bazel only; `go build` in -# the repository root never reads it (it is not named go.work). -go_deps = use_extension("@gazelle//:extensions.bzl", "go_deps") -go_deps.from_file(go_work = "//:gazelle.go.work") - -# beads builds go-mysql-server with its stdlib-regex backend (ICU policy): -# gazelle must see gms_pure_go as satisfied so it selects regex_pure.go and -# drops the go-icu-regex (libicu, cgo) edge entirely. -go_deps.gazelle_override( - directives = [ - "gazelle:build_tags gms_pure_go", - "gazelle:exclude internal/regex/regex_cgo.go", - ], - path = "github.com/dolthub/go-mysql-server", -) - -# protoc-gen-validate ships validate/validate.pb.go, so its Go library can -# compile from the checked-in code. Its own validate/BUILD instead makes the -# Go library a go_proto_library, so every lane built protoc from source just -# to regenerate that file: about 4.5k C++ compile, link and archive actions, -# each carrying the LLVM toolchain as inputs. The module reaches the graph -# only through grpc xds and the cloud SDKs. Gazelle regenerates its BUILD -# files with proto rules off; build_file_name makes it write BUILD.bazel (which -# Bazel prefers) rather than merge into the upstream BUILD files. -go_deps.gazelle_override( - build_file_generation = "on", - directives = [ - "gazelle:build_file_name BUILD.bazel", - "gazelle:proto disable", - ], - path = "github.com/envoyproxy/protoc-gen-validate", -) -go_deps.module_override( - patch_strip = 1, - patches = ["//third_party/patches:eventsapi_schema_build.patch"], - path = "github.com/dolthub/eventsapi_schema", -) -go_deps.module_override( - patch_strip = 1, - patches = ["//third_party/patches:flatbuffers_v23_importpath.patch"], - path = "github.com/dolthub/flatbuffers/v23", -) -go_deps.module_override( - patch_strip = 1, - patches = ["//third_party/patches:dolt_go_serial.patch"], - path = "github.com/dolthub/dolt/go", -) - -# Pure-Go stubs so --config=pure can compile dolt's store/nbs (see the patch). -go_deps.module_override( - patch_strip = 1, - patches = ["//third_party/patches:gozstd_nocgo.patch"], - path = "github.com/dolthub/gozstd", -) - -# nogo does not analyze the standard library, so ctrlflow has no noReturn -# facts for t.Fatal, log.Fatal, os.Exit, ...; buildssa (unparam) and the vet -# passes then treat them as returning, unlike golangci-lint and go vet. The -# patch makes ctrlflow know those standard-library functions intrinsically. -go_deps.module_override( - patch_strip = 1, - patches = ["//third_party/patches:x_tools_ctrlflow_stdlib_noreturn.patch"], - path = "golang.org/x/tools", -) -use_repo(go_deps, "cc_mvdan_unparam", "com_github_anthropics_anthropic_sdk_go", "com_github_ashanbrown_forbidigo_v2", "com_github_burntsushi_toml", "com_github_cenkalti_backoff_v4", "com_github_charmbracelet_x_ansi", "com_github_creack_pty", "com_github_data_dog_go_sqlmock", "com_github_dolthub_dolt_go", "com_github_dolthub_driver_v2", "com_github_dolthub_eventkit", "com_github_dolthub_go_mysql_server", "com_github_dolthub_vitess", "com_github_go_sql_driver_mysql", "com_github_golangci_misspell", "com_github_golangci_unconvert", "com_github_google_uuid", "com_github_gowebpki_jcs", "com_github_invopop_jsonschema", "com_github_johanneskaufmann_html_to_markdown_v2", "com_github_kisielk_errcheck", "com_github_mattn_go_sqlite3", "com_github_microcosm_cc_bluemonday", "com_github_oapi_codegen_oapi_codegen_v2", "com_github_olebedev_when", "com_github_openpeedeep_depguard_v2", "com_github_securego_gosec_v2", "com_github_spf13_cobra", "com_github_spf13_pflag", "com_github_spf13_viper", "com_github_stretchr_testify", "com_github_subosito_gotenv", "com_github_testcontainers_testcontainers_go", "com_github_testcontainers_testcontainers_go_modules_dolt", "com_github_yuin_goldmark", "in_gopkg_yaml_v3", "io_opentelemetry_go_otel", "io_opentelemetry_go_otel_exporters_otlp_otlpmetric_otlpmetrichttp", "io_opentelemetry_go_otel_exporters_stdout_stdoutmetric", "io_opentelemetry_go_otel_exporters_stdout_stdouttrace", "io_opentelemetry_go_otel_metric", "io_opentelemetry_go_otel_sdk", "io_opentelemetry_go_otel_sdk_metric", "io_opentelemetry_go_otel_trace", "io_rsc_script", "land_charm_glamour_v2", "land_charm_huh_v2", "land_charm_lipgloss_v2", "org_go_simpler_sloglint", "org_golang_x_mod", "org_golang_x_net", "org_golang_x_sync", "org_golang_x_sys", "org_golang_x_term", "org_golang_x_tools") - -# Hermetic Dolt CLI for tests (tools/bazel/dolt.bzl pins the release to -# scripts/ci/install-dolt.sh). //tools/bazel:dolt selects the repository for -# the execution platform, so only that tarball is downloaded. -dolt_release = use_repo_rule("//tools/bazel:dolt.bzl", "dolt_release") - -dolt_release( - name = "dolt_linux_amd64", - platform = "linux-amd64", -) - -dolt_release( - name = "dolt_linux_arm64", - platform = "linux-arm64", -) - -dolt_release( - name = "dolt_darwin_amd64", - platform = "darwin-amd64", -) - -dolt_release( - name = "dolt_darwin_arm64", - platform = "darwin-arm64", -) - -# The migration corpus's external Dolt runtime (scripts/migration-test/lib/ -# versions.sh DOLT_TEST_RUNTIME_VERSION): an older release than DOLT_VERSION, -# deliberately, as the historical server-Dolt fixtures' host. -dolt_release( - name = "dolt_test_runtime_linux_amd64", - platform = "linux-amd64", - version = "2.1.8", -) - -# The tracked release formula, for //scripts:shell_scripts_test and the -# whole-checkout guard //scripts/repochecks:go_install_guidance_test. .beads/ -# also holds live bd data and is in .bazelignore, so no main-repo target can -# declare the formula; this local repository exposes only .beads/formulas. -new_local_repository = use_repo_rule("@bazel_tools//tools/build_defs/repo:local.bzl", "new_local_repository") - -new_local_repository( - name = "beads_formulas", - build_file_content = """exports_files( - glob(["*.formula.toml"]), - visibility = [ - "@@//scripts:__pkg__", - "@@//scripts/repochecks:__pkg__", - ], -) -""", - path = ".beads/formulas", -) - -# Historical bd releases for the regression, migration and cross-version -# suites (tools/bazel/bd_releases.bzl): one repository per release in the -# release catalog that records a linux_amd64 asset digest, behind the -# @bd_releases hub. Adding a release is a catalog change only. -bd_releases = use_extension("//tools/bazel:bd_releases.bzl", "bd_releases") -bd_releases.catalog(path = "//scripts:migration-test/release-catalog.json") -use_repo(bd_releases, "bd_releases") - -# The source-tag SQLite release (v0.9.1) has no release asset: the migration -# corpus builds it from its module source with the Go release it was -# qualified on, offline (//tests/migration:bd_source_tag). The proxy files -# are pinned in tests/migration/source_tag_modules.json, the toolchain here. -go_module_proxy = use_repo_rule("//tools/bazel:go_module_proxy.bzl", "go_module_proxy") - -go_module_proxy( - name = "bd_source_tag_goproxy", - lock = "//tests/migration:source_tag_modules.json", -) - -http_archive = use_repo_rule("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") - -http_archive( - name = "go_1_25_0_linux_amd64", - build_file_content = """exports_files( - ["go/bin/go"], - visibility = ["//visibility:public"], -) - -filegroup( - name = "sdk", - srcs = glob(["go/**"]), - visibility = ["//visibility:public"], -) -""", - sha256 = "2852af0cb20a13139b3448992e69b868e50ed0f8a1e5940ee1de9e19a123b613", - urls = ["https://go.dev/dl/go1.25.0.linux-amd64.tar.gz"], -) - -# The timeout implementations //tests/hook_timeout_backends runs the tracked -# git hook shims against (formerly checks.nix's hook-timeout-backends, which -# took them from nixpkgs): static x86_64 builds, so the test needs nothing -# from the executor beyond a shell, perl and GNU coreutils. -http_file = use_repo_rule("@bazel_tools//tools/build_defs/repo:http.bzl", "http_file") - -http_file( - name = "busybox_static", - executable = True, - sha256 = "6e123e7f3202a8c1e9b1f94d8941580a25135382b99e8d3e34fb858bba311348", - urls = ["https://busybox.net/downloads/binaries/1.35.0-x86_64-linux-musl/busybox"], -) - -http_file( - name = "toybox_static", - executable = True, - sha256 = "38e03ffd8aead8ca26833e1e5c90a9ea7250f3719c29d779ebff7aa55c9d2dac", - urls = ["https://landley.net/toybox/downloads/binaries/0.8.15/toybox-x86_64"], -) - -http_archive( - name = "uutils_coreutils", - build_file_content = """exports_files( - ["coreutils"], - visibility = ["//visibility:public"], -) -""", - sha256 = "9b555b01457279bcc3e46753f0ef324fd3d02a0535111200a4632d1297263b54", - strip_prefix = "coreutils-0.12.0-x86_64-unknown-linux-musl", - urls = ["https://github.com/uutils/coreutils/releases/download/0.12.0/coreutils-0.12.0-x86_64-unknown-linux-musl.tar.gz"], -) diff --git a/MODULE.bazel.lock b/MODULE.bazel.lock deleted file mode 100644 index 82bd1b0bc..000000000 --- a/MODULE.bazel.lock +++ /dev/null @@ -1,853 +0,0 @@ -{ - "lockFileVersion": 28, - "registryFileHashes": { - "https://bcr.bazel.build/bazel_registry.json": "8a28e4aff06ee60aed2a8c281907fb8bcbf3b753c91fb5a5c57da3215d5b3497", - "https://bcr.bazel.build/modules/abseil-cpp/20210324.2/MODULE.bazel": "7cd0312e064fde87c8d1cd79ba06c876bd23630c83466e9500321be55c96ace2", - "https://bcr.bazel.build/modules/abseil-cpp/20211102.0/MODULE.bazel": "70390338f7a5106231d20620712f7cccb659cd0e9d073d1991c038eb9fc57589", - "https://bcr.bazel.build/modules/abseil-cpp/20230125.1/MODULE.bazel": "89047429cb0207707b2dface14ba7f8df85273d484c2572755be4bab7ce9c3a0", - "https://bcr.bazel.build/modules/abseil-cpp/20230802.0.bcr.1/MODULE.bazel": "1c8cec495288dccd14fdae6e3f95f772c1c91857047a098fad772034264cc8cb", - "https://bcr.bazel.build/modules/abseil-cpp/20230802.0/MODULE.bazel": "d253ae36a8bd9ee3c5955384096ccb6baf16a1b1e93e858370da0a3b94f77c16", - "https://bcr.bazel.build/modules/abseil-cpp/20230802.1/MODULE.bazel": "fa92e2eb41a04df73cdabeec37107316f7e5272650f81d6cc096418fe647b915", - "https://bcr.bazel.build/modules/abseil-cpp/20240116.1/MODULE.bazel": "37bcdb4440fbb61df6a1c296ae01b327f19e9bb521f9b8e26ec854b6f97309ed", - "https://bcr.bazel.build/modules/abseil-cpp/20240116.2/MODULE.bazel": "73939767a4686cd9a520d16af5ab440071ed75cec1a876bf2fcfaf1f71987a16", - "https://bcr.bazel.build/modules/abseil-cpp/20250127.1/MODULE.bazel": "c4a89e7ceb9bf1e25cf84a9f830ff6b817b72874088bf5141b314726e46a57c1", - "https://bcr.bazel.build/modules/abseil-cpp/20250512.1/MODULE.bazel": "d209fdb6f36ffaf61c509fcc81b19e81b411a999a934a032e10cd009a0226215", - "https://bcr.bazel.build/modules/abseil-cpp/20250814.1/MODULE.bazel": "51f2312901470cdab0dbdf3b88c40cd21c62a7ed58a3de45b365ddc5b11bcab2", - "https://bcr.bazel.build/modules/abseil-cpp/20250814.1/source.json": "cea3901d7e299da7320700abbaafe57a65d039f10d0d7ea601c4a66938ea4b0c", - "https://bcr.bazel.build/modules/apple_support/1.11.1/MODULE.bazel": "1843d7cd8a58369a444fc6000e7304425fba600ff641592161d9f15b179fb896", - "https://bcr.bazel.build/modules/apple_support/1.15.1/MODULE.bazel": "a0556fefca0b1bb2de8567b8827518f94db6a6e7e7d632b4c48dc5f865bc7c85", - "https://bcr.bazel.build/modules/apple_support/1.21.0/MODULE.bazel": "ac1824ed5edf17dee2fdd4927ada30c9f8c3b520be1b5fd02a5da15bc10bff3e", - "https://bcr.bazel.build/modules/apple_support/1.21.1/MODULE.bazel": "5809fa3efab15d1f3c3c635af6974044bac8a4919c62238cce06acee8a8c11f1", - "https://bcr.bazel.build/modules/apple_support/2.5.2/MODULE.bazel": "bb1b7081e390a8d3885f642cb0073dfcf8e7a36525e079fa8f5835af7079c0fe", - "https://bcr.bazel.build/modules/apple_support/2.5.2/source.json": "dd51ee925a550e92b55eb41c4c809a967da311a2d2520241252a238a48b39c4c", - "https://bcr.bazel.build/modules/aspect_bazel_lib/2.14.0/MODULE.bazel": "2b31ffcc9bdc8295b2167e07a757dbbc9ac8906e7028e5170a3708cecaac119f", - "https://bcr.bazel.build/modules/aspect_bazel_lib/2.19.3/MODULE.bazel": "253d739ba126f62a5767d832765b12b59e9f8d2bc88cc1572f4a73e46eb298ca", - "https://bcr.bazel.build/modules/aspect_bazel_lib/2.19.3/source.json": "ffab9254c65ba945f8369297ad97ca0dec213d3adc6e07877e23a48624a8b456", - "https://bcr.bazel.build/modules/aspect_bazel_lib/2.8.1/MODULE.bazel": "812d2dd42f65dca362152101fbec418029cc8fd34cbad1a2fde905383d705838", - "https://bcr.bazel.build/modules/bazel_features/1.1.1/MODULE.bazel": "27b8c79ef57efe08efccbd9dd6ef70d61b4798320b8d3c134fd571f78963dbcd", - "https://bcr.bazel.build/modules/bazel_features/1.10.0/MODULE.bazel": "f75e8807570484a99be90abcd52b5e1f390362c258bcb73106f4544957a48101", - "https://bcr.bazel.build/modules/bazel_features/1.11.0/MODULE.bazel": "f9382337dd5a474c3b7d334c2f83e50b6eaedc284253334cf823044a26de03e8", - "https://bcr.bazel.build/modules/bazel_features/1.15.0/MODULE.bazel": "d38ff6e517149dc509406aca0db3ad1efdd890a85e049585b7234d04238e2a4d", - "https://bcr.bazel.build/modules/bazel_features/1.17.0/MODULE.bazel": "039de32d21b816b47bd42c778e0454217e9c9caac4a3cf8e15c7231ee3ddee4d", - "https://bcr.bazel.build/modules/bazel_features/1.18.0/MODULE.bazel": "1be0ae2557ab3a72a57aeb31b29be347bcdc5d2b1eb1e70f39e3851a7e97041a", - "https://bcr.bazel.build/modules/bazel_features/1.19.0/MODULE.bazel": "59adcdf28230d220f0067b1f435b8537dd033bfff8db21335ef9217919c7fb58", - "https://bcr.bazel.build/modules/bazel_features/1.21.0/MODULE.bazel": "675642261665d8eea09989aa3b8afb5c37627f1be178382c320d1b46afba5e3b", - "https://bcr.bazel.build/modules/bazel_features/1.23.0/MODULE.bazel": "fd1ac84bc4e97a5a0816b7fd7d4d4f6d837b0047cf4cbd81652d616af3a6591a", - "https://bcr.bazel.build/modules/bazel_features/1.27.0/MODULE.bazel": "621eeee06c4458a9121d1f104efb80f39d34deff4984e778359c60eaf1a8cb65", - "https://bcr.bazel.build/modules/bazel_features/1.28.0/MODULE.bazel": "4b4200e6cbf8fa335b2c3f43e1d6ef3e240319c33d43d60cc0fbd4b87ece299d", - "https://bcr.bazel.build/modules/bazel_features/1.3.0/MODULE.bazel": "cdcafe83ec318cda34e02948e81d790aab8df7a929cec6f6969f13a489ccecd9", - "https://bcr.bazel.build/modules/bazel_features/1.30.0/MODULE.bazel": "a14b62d05969a293b80257e72e597c2da7f717e1e69fa8b339703ed6731bec87", - "https://bcr.bazel.build/modules/bazel_features/1.33.0/MODULE.bazel": "8b8dc9d2a4c88609409c3191165bccec0e4cb044cd7a72ccbe826583303459f6", - "https://bcr.bazel.build/modules/bazel_features/1.34.0/MODULE.bazel": "e8475ad7c8965542e0c7aac8af68eb48c4af904be3d614b6aa6274c092c2ea1e", - "https://bcr.bazel.build/modules/bazel_features/1.36.0/MODULE.bazel": "596cb62090b039caf1cad1d52a8bc35cf188ca9a4e279a828005e7ee49a1bec3", - "https://bcr.bazel.build/modules/bazel_features/1.38.0/MODULE.bazel": "f9b8a9c890ebd216b4049fd12a31d3c2602e3403c7af636b04fbbd7453edc9c9", - "https://bcr.bazel.build/modules/bazel_features/1.4.1/MODULE.bazel": "e45b6bb2350aff3e442ae1111c555e27eac1d915e77775f6fdc4b351b758b5d7", - "https://bcr.bazel.build/modules/bazel_features/1.42.1/MODULE.bazel": "275a59b5406ff18c01739860aa70ad7ccb3cfb474579411decca11c93b951080", - "https://bcr.bazel.build/modules/bazel_features/1.42.1/source.json": "fcd4396b2df85f64f2b3bb436ad870793ecf39180f1d796f913cc9276d355309", - "https://bcr.bazel.build/modules/bazel_features/1.9.0/MODULE.bazel": "885151d58d90d8d9c811eb75e3288c11f850e1d6b481a8c9f766adee4712358b", - "https://bcr.bazel.build/modules/bazel_features/1.9.1/MODULE.bazel": "8f679097876a9b609ad1f60249c49d68bfab783dd9be012faf9d82547b14815a", - "https://bcr.bazel.build/modules/bazel_skylib/1.0.3/MODULE.bazel": "bcb0fd896384802d1ad283b4e4eb4d718eebd8cb820b0a2c3a347fb971afd9d8", - "https://bcr.bazel.build/modules/bazel_skylib/1.1.1/MODULE.bazel": "1add3e7d93ff2e6998f9e118022c84d163917d912f5afafb3058e3d2f1545b5e", - "https://bcr.bazel.build/modules/bazel_skylib/1.2.0/MODULE.bazel": "44fe84260e454ed94ad326352a698422dbe372b21a1ac9f3eab76eb531223686", - "https://bcr.bazel.build/modules/bazel_skylib/1.2.1/MODULE.bazel": "f35baf9da0efe45fa3da1696ae906eea3d615ad41e2e3def4aeb4e8bc0ef9a7a", - "https://bcr.bazel.build/modules/bazel_skylib/1.3.0/MODULE.bazel": "20228b92868bf5cfc41bda7afc8a8ba2a543201851de39d990ec957b513579c5", - "https://bcr.bazel.build/modules/bazel_skylib/1.4.1/MODULE.bazel": "a0dcb779424be33100dcae821e9e27e4f2901d9dfd5333efe5ac6a8d7ab75e1d", - "https://bcr.bazel.build/modules/bazel_skylib/1.4.2/MODULE.bazel": "3bd40978e7a1fac911d5989e6b09d8f64921865a45822d8b09e815eaa726a651", - "https://bcr.bazel.build/modules/bazel_skylib/1.5.0/MODULE.bazel": "32880f5e2945ce6a03d1fbd588e9198c0a959bb42297b2cfaf1685b7bc32e138", - "https://bcr.bazel.build/modules/bazel_skylib/1.6.1/MODULE.bazel": "8fdee2dbaace6c252131c00e1de4b165dc65af02ea278476187765e1a617b917", - "https://bcr.bazel.build/modules/bazel_skylib/1.7.0/MODULE.bazel": "0db596f4563de7938de764cc8deeabec291f55e8ec15299718b93c4423e9796d", - "https://bcr.bazel.build/modules/bazel_skylib/1.7.1/MODULE.bazel": "3120d80c5861aa616222ec015332e5f8d3171e062e3e804a2a0253e1be26e59b", - "https://bcr.bazel.build/modules/bazel_skylib/1.8.1/MODULE.bazel": "88ade7293becda963e0e3ea33e7d54d3425127e0a326e0d17da085a5f1f03ff6", - "https://bcr.bazel.build/modules/bazel_skylib/1.8.2/MODULE.bazel": "69ad6927098316848b34a9142bcc975e018ba27f08c4ff403f50c1b6e646ca67", - "https://bcr.bazel.build/modules/bazel_skylib/1.8.2/source.json": "34a3c8bcf233b835eb74be9d628899bb32999d3e0eadef1947a0a562a2b16ffb", - "https://bcr.bazel.build/modules/buildozer/8.5.1/MODULE.bazel": "a35d9561b3fc5b18797c330793e99e3b834a473d5fbd3d7d7634aafc9bdb6f8f", - "https://bcr.bazel.build/modules/buildozer/8.5.1/source.json": "e3386e6ff4529f2442800dee47ad28d3e6487f36a1f75ae39ae56c70f0cd2fbd", - "https://bcr.bazel.build/modules/gawk/5.3.2.bcr.1/MODULE.bazel": "cdf8cbe5ee750db04b78878c9633cc76e80dcf4416cbe982ac3a9222f80713c8", - "https://bcr.bazel.build/modules/gawk/5.3.2.bcr.1/source.json": "fa7b512dfcb5eafd90ce3959cf42a2a6fe96144ebbb4b3b3928054895f2afac2", - "https://bcr.bazel.build/modules/gazelle/0.51.3/MODULE.bazel": "618a729142f66de1e2cb776d026413763be5b80d5e3d29ffb9d3d90c5defde90", - "https://bcr.bazel.build/modules/gazelle/0.53.0/MODULE.bazel": "76158efbaf12e35ae17af02f4a715bc6e7b3b410c693d34f422922ff4a56a925", - "https://bcr.bazel.build/modules/gazelle/0.53.0/source.json": "1f5a4dd0399e5a20e0daeaa3ba1f42391a1b909bc3d337f7a62d3216ddbdfad0", - "https://bcr.bazel.build/modules/google_benchmark/1.8.2/MODULE.bazel": "a70cf1bba851000ba93b58ae2f6d76490a9feb74192e57ab8e8ff13c34ec50cb", - "https://bcr.bazel.build/modules/googletest/1.11.0/MODULE.bazel": "3a83f095183f66345ca86aa13c58b59f9f94a2f81999c093d4eeaa2d262d12f4", - "https://bcr.bazel.build/modules/googletest/1.14.0.bcr.1/MODULE.bazel": "22c31a561553727960057361aa33bf20fb2e98584bc4fec007906e27053f80c6", - "https://bcr.bazel.build/modules/googletest/1.14.0/MODULE.bazel": "cfbcbf3e6eac06ef9d85900f64424708cc08687d1b527f0ef65aa7517af8118f", - "https://bcr.bazel.build/modules/googletest/1.15.2/MODULE.bazel": "6de1edc1d26cafb0ea1a6ab3f4d4192d91a312fd2d360b63adaa213cd00b2108", - "https://bcr.bazel.build/modules/googletest/1.17.0/MODULE.bazel": "dbec758171594a705933a29fcf69293d2468c49ec1f2ebca65c36f504d72df46", - "https://bcr.bazel.build/modules/googletest/1.17.0/source.json": "38e4454b25fc30f15439c0378e57909ab1fd0a443158aa35aec685da727cd713", - "https://bcr.bazel.build/modules/jq.bzl/0.1.0/MODULE.bazel": "2ce69b1af49952cd4121a9c3055faa679e748ce774c7f1fda9657f936cae902f", - "https://bcr.bazel.build/modules/jq.bzl/0.1.0/source.json": "746bf13cac0860f091df5e4911d0c593971cd8796b5ad4e809b2f8e133eee3d5", - "https://bcr.bazel.build/modules/jsoncpp/1.9.5/MODULE.bazel": "31271aedc59e815656f5736f282bb7509a97c7ecb43e927ac1a37966e0578075", - "https://bcr.bazel.build/modules/jsoncpp/1.9.6/MODULE.bazel": "2f8d20d3b7d54143213c4dfc3d98225c42de7d666011528dc8fe91591e2e17b0", - "https://bcr.bazel.build/modules/jsoncpp/1.9.6/source.json": "a04756d367a2126c3541682864ecec52f92cdee80a35735a3cb249ce015ca000", - "https://bcr.bazel.build/modules/libpfm/4.11.0/MODULE.bazel": "45061ff025b301940f1e30d2c16bea596c25b176c8b6b3087e92615adbd52902", - "https://bcr.bazel.build/modules/mboworks_bzl/0.5.1/MODULE.bazel": "20c921aded4f4e00a6128dad47a843371c196ba8987474c11c8ba7b9da8c16f1", - "https://bcr.bazel.build/modules/mboworks_bzl/0.5.1/source.json": "3d9086bb203475113a16afb0ca0c9b97fdb37fc6e5a62b604b60ffc2450dc8c5", - "https://bcr.bazel.build/modules/nlohmann_json/3.6.1/MODULE.bazel": "6f7b417dcc794d9add9e556673ad25cb3ba835224290f4f848f8e2db1e1fca74", - "https://bcr.bazel.build/modules/nlohmann_json/3.6.1/source.json": "f448c6e8963fdfa7eb831457df83ad63d3d6355018f6574fb017e8169deb43a9", - "https://bcr.bazel.build/modules/package_metadata/0.0.2/MODULE.bazel": "fb8d25550742674d63d7b250063d4580ca530499f045d70748b1b142081ebb92", - "https://bcr.bazel.build/modules/package_metadata/0.0.3/MODULE.bazel": "77890552ecea9e284b5424c9de827a58099348763a4359e975c359a83d4faa83", - "https://bcr.bazel.build/modules/package_metadata/0.0.5/MODULE.bazel": "ef4f9439e3270fdd6b9fd4dbc3d2f29d13888e44c529a1b243f7a31dfbc2e8e4", - "https://bcr.bazel.build/modules/package_metadata/0.0.5/source.json": "2326db2f6592578177751c3e1f74786b79382cd6008834c9d01ec865b9126a85", - "https://bcr.bazel.build/modules/platforms/0.0.10/MODULE.bazel": "8cb8efaf200bdeb2150d93e162c40f388529a25852b332cec879373771e48ed5", - "https://bcr.bazel.build/modules/platforms/0.0.11/MODULE.bazel": "0daefc49732e227caa8bfa834d65dc52e8cc18a2faf80df25e8caea151a9413f", - "https://bcr.bazel.build/modules/platforms/0.0.4/MODULE.bazel": "9b328e31ee156f53f3c416a64f8491f7eb731742655a47c9eec4703a71644aee", - "https://bcr.bazel.build/modules/platforms/0.0.5/MODULE.bazel": "5733b54ea419d5eaf7997054bb55f6a1d0b5ff8aedf0176fef9eea44f3acda37", - "https://bcr.bazel.build/modules/platforms/0.0.6/MODULE.bazel": "ad6eeef431dc52aefd2d77ed20a4b353f8ebf0f4ecdd26a807d2da5aa8cd0615", - "https://bcr.bazel.build/modules/platforms/0.0.7/MODULE.bazel": "72fd4a0ede9ee5c021f6a8dd92b503e089f46c227ba2813ff183b71616034814", - "https://bcr.bazel.build/modules/platforms/0.0.8/MODULE.bazel": "9f142c03e348f6d263719f5074b21ef3adf0b139ee4c5133e2aa35664da9eb2d", - "https://bcr.bazel.build/modules/platforms/0.0.9/MODULE.bazel": "4a87a60c927b56ddd67db50c89acaa62f4ce2a1d2149ccb63ffd871d5ce29ebc", - "https://bcr.bazel.build/modules/platforms/1.0.0/MODULE.bazel": "f05feb42b48f1b3c225e4ccf351f367be0371411a803198ec34a389fb22aa580", - "https://bcr.bazel.build/modules/platforms/1.1.0/MODULE.bazel": "1c0c09f5bdcf4b3f924720d2478a3711cb39f4977019ca5988685e5b7e18b3d2", - "https://bcr.bazel.build/modules/platforms/1.1.0/source.json": "fcf351c47596c939140ab0d333dfdd08ed1ea6ce33c2fe70c12493a301cf1344", - "https://bcr.bazel.build/modules/protobuf/21.7/MODULE.bazel": "a5a29bb89544f9b97edce05642fac225a808b5b7be74038ea3640fae2f8e66a7", - "https://bcr.bazel.build/modules/protobuf/27.0/MODULE.bazel": "7873b60be88844a0a1d8f80b9d5d20cfbd8495a689b8763e76c6372998d3f64c", - "https://bcr.bazel.build/modules/protobuf/27.1/MODULE.bazel": "703a7b614728bb06647f965264967a8ef1c39e09e8f167b3ca0bb1fd80449c0d", - "https://bcr.bazel.build/modules/protobuf/29.0-rc2/MODULE.bazel": "6241d35983510143049943fc0d57937937122baf1b287862f9dc8590fc4c37df", - "https://bcr.bazel.build/modules/protobuf/29.0-rc3/MODULE.bazel": "33c2dfa286578573afc55a7acaea3cada4122b9631007c594bf0729f41c8de92", - "https://bcr.bazel.build/modules/protobuf/29.0/MODULE.bazel": "319dc8bf4c679ff87e71b1ccfb5a6e90a6dbc4693501d471f48662ac46d04e4e", - "https://bcr.bazel.build/modules/protobuf/29.1/MODULE.bazel": "557c3457560ff49e122ed76c0bc3397a64af9574691cb8201b4e46d4ab2ecb95", - "https://bcr.bazel.build/modules/protobuf/3.19.0/MODULE.bazel": "6b5fbb433f760a99a22b18b6850ed5784ef0e9928a72668b66e4d7ccd47db9b0", - "https://bcr.bazel.build/modules/protobuf/3.19.6/MODULE.bazel": "9233edc5e1f2ee276a60de3eaa47ac4132302ef9643238f23128fea53ea12858", - "https://bcr.bazel.build/modules/protobuf/32.1/MODULE.bazel": "89cd2866a9cb07fee9ff74c41ceace11554f32e0d849de4e23ac55515cfada4d", - "https://bcr.bazel.build/modules/protobuf/33.4/MODULE.bazel": "114775b816b38b6d0ca620450d6b02550c60ceedfdc8d9a229833b34a223dc42", - "https://bcr.bazel.build/modules/protobuf/33.4/source.json": "555f8686b4c7d6b5ba731fbea13bf656b4bfd9a7ff629c1d9d3f6e1d6155de79", - "https://bcr.bazel.build/modules/pybind11_bazel/2.11.1/MODULE.bazel": "88af1c246226d87e65be78ed49ecd1e6f5e98648558c14ce99176da041dc378e", - "https://bcr.bazel.build/modules/pybind11_bazel/2.12.0/MODULE.bazel": "e6f4c20442eaa7c90d7190d8dc539d0ab422f95c65a57cc59562170c58ae3d34", - "https://bcr.bazel.build/modules/pybind11_bazel/2.12.0/source.json": "6900fdc8a9e95866b8c0d4ad4aba4d4236317b5c1cd04c502df3f0d33afed680", - "https://bcr.bazel.build/modules/re2/2023-09-01/MODULE.bazel": "cb3d511531b16cfc78a225a9e2136007a48cf8a677e4264baeab57fe78a80206", - "https://bcr.bazel.build/modules/re2/2024-07-02.bcr.1/MODULE.bazel": "b4963dda9b31080be1905ef085ecd7dd6cd47c05c79b9cdf83ade83ab2ab271a", - "https://bcr.bazel.build/modules/re2/2024-07-02.bcr.1/source.json": "2ff292be6ef3340325ce8a045ecc326e92cbfab47c7cbab4bd85d28971b97ac4", - "https://bcr.bazel.build/modules/re2/2024-07-02/MODULE.bazel": "0eadc4395959969297cbcf31a249ff457f2f1d456228c67719480205aa306daa", - "https://bcr.bazel.build/modules/rules_android/0.1.1/MODULE.bazel": "48809ab0091b07ad0182defb787c4c5328bd3a278938415c00a7b69b50c4d3a8", - "https://bcr.bazel.build/modules/rules_android/0.1.1/source.json": "e6986b41626ee10bdc864937ffb6d6bf275bb5b9c65120e6137d56e6331f089e", - "https://bcr.bazel.build/modules/rules_apple/3.16.0/MODULE.bazel": "0d1caf0b8375942ce98ea944be754a18874041e4e0459401d925577624d3a54a", - "https://bcr.bazel.build/modules/rules_apple/4.1.0/MODULE.bazel": "76e10fd4a48038d3fc7c5dc6e63b7063bbf5304a2e3bd42edda6ec660eebea68", - "https://bcr.bazel.build/modules/rules_apple/4.1.0/source.json": "8ee81e1708756f81b343a5eb2b2f0b953f1d25c4ab3d4a68dc02754872e80715", - "https://bcr.bazel.build/modules/rules_cc/0.0.1/MODULE.bazel": "cb2aa0747f84c6c3a78dad4e2049c154f08ab9d166b1273835a8174940365647", - "https://bcr.bazel.build/modules/rules_cc/0.0.10/MODULE.bazel": "ec1705118f7eaedd6e118508d3d26deba2a4e76476ada7e0e3965211be012002", - "https://bcr.bazel.build/modules/rules_cc/0.0.13/MODULE.bazel": "0e8529ed7b323dad0775ff924d2ae5af7640b23553dfcd4d34344c7e7a867191", - "https://bcr.bazel.build/modules/rules_cc/0.0.15/MODULE.bazel": "6704c35f7b4a72502ee81f61bf88706b54f06b3cbe5558ac17e2e14666cd5dcc", - "https://bcr.bazel.build/modules/rules_cc/0.0.16/MODULE.bazel": "7661303b8fc1b4d7f532e54e9d6565771fea666fbdf839e0a86affcd02defe87", - "https://bcr.bazel.build/modules/rules_cc/0.0.17/MODULE.bazel": "2ae1d8f4238ec67d7185d8861cb0a2cdf4bc608697c331b95bf990e69b62e64a", - "https://bcr.bazel.build/modules/rules_cc/0.0.2/MODULE.bazel": "6915987c90970493ab97393024c156ea8fb9f3bea953b2f3ec05c34f19b5695c", - "https://bcr.bazel.build/modules/rules_cc/0.0.6/MODULE.bazel": "abf360251023dfe3efcef65ab9d56beefa8394d4176dd29529750e1c57eaa33f", - "https://bcr.bazel.build/modules/rules_cc/0.0.8/MODULE.bazel": "964c85c82cfeb6f3855e6a07054fdb159aced38e99a5eecf7bce9d53990afa3e", - "https://bcr.bazel.build/modules/rules_cc/0.0.9/MODULE.bazel": "836e76439f354b89afe6a911a7adf59a6b2518fafb174483ad78a2a2fde7b1c5", - "https://bcr.bazel.build/modules/rules_cc/0.1.1/MODULE.bazel": "2f0222a6f229f0bf44cd711dc13c858dad98c62d52bd51d8fc3a764a83125513", - "https://bcr.bazel.build/modules/rules_cc/0.1.2/MODULE.bazel": "557ddc3a96858ec0d465a87c0a931054d7dcfd6583af2c7ed3baf494407fd8d0", - "https://bcr.bazel.build/modules/rules_cc/0.1.5/MODULE.bazel": "88dfc9361e8b5ae1008ac38f7cdfd45ad738e4fa676a3ad67d19204f045a1fd8", - "https://bcr.bazel.build/modules/rules_cc/0.2.0/MODULE.bazel": "b5c17f90458caae90d2ccd114c81970062946f49f355610ed89bebf954f5783c", - "https://bcr.bazel.build/modules/rules_cc/0.2.13/MODULE.bazel": "eecdd666eda6be16a8d9dc15e44b5c75133405e820f620a234acc4b1fdc5aa37", - "https://bcr.bazel.build/modules/rules_cc/0.2.15/MODULE.bazel": "6a0a4a75a57aa6dc888300d848053a58c6b12a29f89d4304e1c41448514ec6e8", - "https://bcr.bazel.build/modules/rules_cc/0.2.17/MODULE.bazel": "1849602c86cb60da8613d2de887f9566a6d354a6df6d7009f9d04a14402f9a84", - "https://bcr.bazel.build/modules/rules_cc/0.2.17/source.json": "3832f45d145354049137c0090df04629d9c2b5493dc5c2bf46f1834040133a07", - "https://bcr.bazel.build/modules/rules_foreign_cc/0.9.0/MODULE.bazel": "c9e8c682bf75b0e7c704166d79b599f93b72cfca5ad7477df596947891feeef6", - "https://bcr.bazel.build/modules/rules_fuzzing/0.5.2/MODULE.bazel": "40c97d1144356f52905566c55811f13b299453a14ac7769dfba2ac38192337a8", - "https://bcr.bazel.build/modules/rules_go/0.63.0/MODULE.bazel": "a2079e783a5d2715a90e6e2970ec8ffd717a487ff2aecb5f553e3975c5294a21", - "https://bcr.bazel.build/modules/rules_go/0.63.0/source.json": "5bf37b45c440ac03abad0cbfa8b8b8dd89cc4e82bc8305f468b8436fa58bae78", - "https://bcr.bazel.build/modules/rules_java/4.0.0/MODULE.bazel": "5a78a7ae82cd1a33cef56dc578c7d2a46ed0dca12643ee45edbb8417899e6f74", - "https://bcr.bazel.build/modules/rules_java/5.3.5/MODULE.bazel": "a4ec4f2db570171e3e5eb753276ee4b389bae16b96207e9d3230895c99644b86", - "https://bcr.bazel.build/modules/rules_java/6.3.0/MODULE.bazel": "a97c7678c19f236a956ad260d59c86e10a463badb7eb2eda787490f4c969b963", - "https://bcr.bazel.build/modules/rules_java/6.5.2/MODULE.bazel": "1d440d262d0e08453fa0c4d8f699ba81609ed0e9a9a0f02cd10b3e7942e61e31", - "https://bcr.bazel.build/modules/rules_java/7.10.0/MODULE.bazel": "530c3beb3067e870561739f1144329a21c851ff771cd752a49e06e3dc9c2e71a", - "https://bcr.bazel.build/modules/rules_java/7.12.2/MODULE.bazel": "579c505165ee757a4280ef83cda0150eea193eed3bef50b1004ba88b99da6de6", - "https://bcr.bazel.build/modules/rules_java/7.2.0/MODULE.bazel": "06c0334c9be61e6cef2c8c84a7800cef502063269a5af25ceb100b192453d4ab", - "https://bcr.bazel.build/modules/rules_java/7.6.1/MODULE.bazel": "2f14b7e8a1aa2f67ae92bc69d1ec0fa8d9f827c4e17ff5e5f02e91caa3b2d0fe", - "https://bcr.bazel.build/modules/rules_java/8.3.2/MODULE.bazel": "7336d5511ad5af0b8615fdc7477535a2e4e723a357b6713af439fe8cf0195017", - "https://bcr.bazel.build/modules/rules_java/8.5.1/MODULE.bazel": "d8a9e38cc5228881f7055a6079f6f7821a073df3744d441978e7a43e20226939", - "https://bcr.bazel.build/modules/rules_java/8.6.1/MODULE.bazel": "f4808e2ab5b0197f094cabce9f4b006a27766beb6a9975931da07099560ca9c2", - "https://bcr.bazel.build/modules/rules_java/9.1.0/MODULE.bazel": "ee63f27e36a3fada80342869361182f120a9819c74320e8e65b1e04ba0cd7a9d", - "https://bcr.bazel.build/modules/rules_java/9.1.0/source.json": "da589573c1dee2c9ac4a568b301269a2e8191110ff0345c1a959fa7ea6c4dfd6", - "https://bcr.bazel.build/modules/rules_jvm_external/4.4.2/MODULE.bazel": "a56b85e418c83eb1839819f0b515c431010160383306d13ec21959ac412d2fe7", - "https://bcr.bazel.build/modules/rules_jvm_external/5.1/MODULE.bazel": "33f6f999e03183f7d088c9be518a63467dfd0be94a11d0055fe2d210f89aa909", - "https://bcr.bazel.build/modules/rules_jvm_external/5.2/MODULE.bazel": "d9351ba35217ad0de03816ef3ed63f89d411349353077348a45348b096615036", - "https://bcr.bazel.build/modules/rules_jvm_external/6.3/MODULE.bazel": "c998e060b85f71e00de5ec552019347c8bca255062c990ac02d051bb80a38df0", - "https://bcr.bazel.build/modules/rules_jvm_external/6.7/MODULE.bazel": "e717beabc4d091ecb2c803c2d341b88590e9116b8bf7947915eeb33aab4f96dd", - "https://bcr.bazel.build/modules/rules_jvm_external/6.7/source.json": "5426f412d0a7fc6b611643376c7e4a82dec991491b9ce5cb1cfdd25fe2e92be4", - "https://bcr.bazel.build/modules/rules_kotlin/1.9.6/MODULE.bazel": "d269a01a18ee74d0335450b10f62c9ed81f2321d7958a2934e44272fe82dcef3", - "https://bcr.bazel.build/modules/rules_kotlin/1.9.6/source.json": "2faa4794364282db7c06600b7e5e34867a564ae91bda7cae7c29c64e9466b7d5", - "https://bcr.bazel.build/modules/rules_license/0.0.3/MODULE.bazel": "627e9ab0247f7d1e05736b59dbb1b6871373de5ad31c3011880b4133cafd4bd0", - "https://bcr.bazel.build/modules/rules_license/0.0.7/MODULE.bazel": "088fbeb0b6a419005b89cf93fe62d9517c0a2b8bb56af3244af65ecfe37e7d5d", - "https://bcr.bazel.build/modules/rules_license/1.0.0/MODULE.bazel": "a7fda60eefdf3d8c827262ba499957e4df06f659330bbe6cdbdb975b768bb65c", - "https://bcr.bazel.build/modules/rules_license/1.0.0/source.json": "a52c89e54cc311196e478f8382df91c15f7a2bfdf4c6cd0e2675cc2ff0b56efb", - "https://bcr.bazel.build/modules/rules_pkg/0.7.0/MODULE.bazel": "df99f03fc7934a4737122518bb87e667e62d780b610910f0447665a7e2be62dc", - "https://bcr.bazel.build/modules/rules_pkg/1.0.1/MODULE.bazel": "5b1df97dbc29623bccdf2b0dcd0f5cb08e2f2c9050aab1092fd39a41e82686ff", - "https://bcr.bazel.build/modules/rules_pkg/1.0.1/source.json": "bd82e5d7b9ce2d31e380dd9f50c111d678c3bdaca190cb76b0e1c71b05e1ba8a", - "https://bcr.bazel.build/modules/rules_proto/4.0.0/MODULE.bazel": "a7a7b6ce9bee418c1a760b3d84f83a299ad6952f9903c67f19e4edd964894e06", - "https://bcr.bazel.build/modules/rules_proto/5.3.0-21.7/MODULE.bazel": "e8dff86b0971688790ae75528fe1813f71809b5afd57facb44dad9e8eca631b7", - "https://bcr.bazel.build/modules/rules_proto/6.0.0-rc1/MODULE.bazel": "1e5b502e2e1a9e825eef74476a5a1ee524a92297085015a052510b09a1a09483", - "https://bcr.bazel.build/modules/rules_proto/6.0.2/MODULE.bazel": "ce916b775a62b90b61888052a416ccdda405212b6aaeb39522f7dc53431a5e73", - "https://bcr.bazel.build/modules/rules_proto/7.0.2/MODULE.bazel": "bf81793bd6d2ad89a37a40693e56c61b0ee30f7a7fdbaf3eabbf5f39de47dea2", - "https://bcr.bazel.build/modules/rules_proto/7.1.0/MODULE.bazel": "002d62d9108f75bb807cd56245d45648f38275cb3a99dcd45dfb864c5d74cb96", - "https://bcr.bazel.build/modules/rules_proto/7.1.0/source.json": "39f89066c12c24097854e8f57ab8558929f9c8d474d34b2c00ac04630ad8940e", - "https://bcr.bazel.build/modules/rules_python/0.10.2/MODULE.bazel": "cc82bc96f2997baa545ab3ce73f196d040ffb8756fd2d66125a530031cd90e5f", - "https://bcr.bazel.build/modules/rules_python/0.23.1/MODULE.bazel": "49ffccf0511cb8414de28321f5fcf2a31312b47c40cc21577144b7447f2bf300", - "https://bcr.bazel.build/modules/rules_python/0.25.0/MODULE.bazel": "72f1506841c920a1afec76975b35312410eea3aa7b63267436bfb1dd91d2d382", - "https://bcr.bazel.build/modules/rules_python/0.28.0/MODULE.bazel": "cba2573d870babc976664a912539b320cbaa7114cd3e8f053c720171cde331ed", - "https://bcr.bazel.build/modules/rules_python/0.31.0/MODULE.bazel": "93a43dc47ee570e6ec9f5779b2e64c1476a6ce921c48cc9a1678a91dd5f8fd58", - "https://bcr.bazel.build/modules/rules_python/0.33.2/MODULE.bazel": "3e036c4ad8d804a4dad897d333d8dce200d943df4827cb849840055be8d2e937", - "https://bcr.bazel.build/modules/rules_python/0.4.0/MODULE.bazel": "9208ee05fd48bf09ac60ed269791cf17fb343db56c8226a720fbb1cdf467166c", - "https://bcr.bazel.build/modules/rules_python/1.3.0/MODULE.bazel": "8361d57eafb67c09b75bf4bbe6be360e1b8f4f18118ab48037f2bd50aa2ccb13", - "https://bcr.bazel.build/modules/rules_python/1.4.1/MODULE.bazel": "8991ad45bdc25018301d6b7e1d3626afc3c8af8aaf4bc04f23d0b99c938b73a6", - "https://bcr.bazel.build/modules/rules_python/1.6.0/MODULE.bazel": "7e04ad8f8d5bea40451cf80b1bd8262552aa73f841415d20db96b7241bd027d8", - "https://bcr.bazel.build/modules/rules_python/1.7.0/MODULE.bazel": "d01f995ecd137abf30238ad9ce97f8fc3ac57289c8b24bd0bf53324d937a14f8", - "https://bcr.bazel.build/modules/rules_python/1.7.0/source.json": "028a084b65dcf8f4dc4f82f8778dbe65df133f234b316828a82e060d81bdce32", - "https://bcr.bazel.build/modules/rules_shell/0.2.0/MODULE.bazel": "fda8a652ab3c7d8fee214de05e7a9916d8b28082234e8d2c0094505c5268ed3c", - "https://bcr.bazel.build/modules/rules_shell/0.3.0/MODULE.bazel": "de4402cd12f4cc8fda2354fce179fdb068c0b9ca1ec2d2b17b3e21b24c1a937b", - "https://bcr.bazel.build/modules/rules_shell/0.4.1/MODULE.bazel": "00e501db01bbf4e3e1dd1595959092c2fadf2087b2852d3f553b5370f5633592", - "https://bcr.bazel.build/modules/rules_shell/0.6.1/MODULE.bazel": "72e76b0eea4e81611ef5452aa82b3da34caca0c8b7b5c0c9584338aa93bae26b", - "https://bcr.bazel.build/modules/rules_shell/0.6.1/source.json": "20ec05cd5e592055e214b2da8ccb283c7f2a421ea0dc2acbf1aa792e11c03d0c", - "https://bcr.bazel.build/modules/rules_swift/1.16.0/MODULE.bazel": "4a09f199545a60d09895e8281362b1ff3bb08bbde69c6fc87aff5b92fcc916ca", - "https://bcr.bazel.build/modules/rules_swift/2.1.1/MODULE.bazel": "494900a80f944fc7aa61500c2073d9729dff0b764f0e89b824eb746959bc1046", - "https://bcr.bazel.build/modules/rules_swift/2.4.0/MODULE.bazel": "1639617eb1ede28d774d967a738b4a68b0accb40650beadb57c21846beab5efd", - "https://bcr.bazel.build/modules/rules_swift/3.1.2/MODULE.bazel": "72c8f5cf9d26427cee6c76c8e3853eb46ce6b0412a081b2b6db6e8ad56267400", - "https://bcr.bazel.build/modules/rules_swift/3.1.2/source.json": "e85761f3098a6faf40b8187695e3de6d97944e98abd0d8ce579cb2daf6319a66", - "https://bcr.bazel.build/modules/stardoc/0.5.1/MODULE.bazel": "1a05d92974d0c122f5ccf09291442580317cdd859f07a8655f1db9a60374f9f8", - "https://bcr.bazel.build/modules/stardoc/0.5.3/MODULE.bazel": "c7f6948dae6999bf0db32c1858ae345f112cacf98f174c7a8bb707e41b974f1c", - "https://bcr.bazel.build/modules/stardoc/0.6.2/MODULE.bazel": "7060193196395f5dd668eda046ccbeacebfd98efc77fed418dbe2b82ffaa39fd", - "https://bcr.bazel.build/modules/stardoc/0.7.0/MODULE.bazel": "05e3d6d30c099b6770e97da986c53bd31844d7f13d41412480ea265ac9e8079c", - "https://bcr.bazel.build/modules/stardoc/0.7.2/MODULE.bazel": "fc152419aa2ea0f51c29583fab1e8c99ddefd5b3778421845606ee628629e0e5", - "https://bcr.bazel.build/modules/stardoc/0.7.2/source.json": "58b029e5e901d6802967754adf0a9056747e8176f017cfe3607c0851f4d42216", - "https://bcr.bazel.build/modules/swift_argument_parser/1.3.1.1/MODULE.bazel": "5e463fbfba7b1701d957555ed45097d7f984211330106ccd1352c6e0af0dcf91", - "https://bcr.bazel.build/modules/swift_argument_parser/1.3.1.2/MODULE.bazel": "75aab2373a4bbe2a1260b9bf2a1ebbdbf872d3bd36f80bff058dccd82e89422f", - "https://bcr.bazel.build/modules/swift_argument_parser/1.3.1.2/source.json": "5fba48bbe0ba48761f9e9f75f92876cafb5d07c0ce059cc7a8027416de94a05b", - "https://bcr.bazel.build/modules/tar.bzl/0.2.1/MODULE.bazel": "52d1c00a80a8cc67acbd01649e83d8dd6a9dc426a6c0b754a04fe8c219c76468", - "https://bcr.bazel.build/modules/tar.bzl/0.6.0/MODULE.bazel": "a3584b4edcfafcabd9b0ef9819808f05b372957bbdff41601429d5fd0aac2e7c", - "https://bcr.bazel.build/modules/tar.bzl/0.6.0/source.json": "4a620381df075a16cb3a7ed57bd1d05f7480222394c64a20fa51bdb636fda658", - "https://bcr.bazel.build/modules/toolchains_llvm/1.11.0/MODULE.bazel": "c0b9c103deeaf0fc103934ff0a189273b4f9b82cc1622c59b67e1c3a83d6b5e6", - "https://bcr.bazel.build/modules/toolchains_llvm/1.11.0/source.json": "d06e53520101c3890a094e5849b0ff16dde3a6f6531f25c997f340ebff34d569", - "https://bcr.bazel.build/modules/upb/0.0.0-20220923-a547704/MODULE.bazel": "7298990c00040a0e2f121f6c32544bab27d4452f80d9ce51349b1a28f3005c43", - "https://bcr.bazel.build/modules/yq.bzl/0.1.1/MODULE.bazel": "9039681f9bcb8958ee2c87ffc74bdafba9f4369096a2b5634b88abc0eaefa072", - "https://bcr.bazel.build/modules/yq.bzl/0.1.1/source.json": "2d2bad780a9f2b9195a4a370314d2c17ae95eaa745cefc2e12fbc49759b15aa3", - "https://bcr.bazel.build/modules/zlib/1.2.11/MODULE.bazel": "07b389abc85fdbca459b69e2ec656ae5622873af3f845e1c9d80fe179f3effa0", - "https://bcr.bazel.build/modules/zlib/1.2.12/MODULE.bazel": "3b1a8834ada2a883674be8cbd36ede1b6ec481477ada359cd2d3ddc562340b27", - "https://bcr.bazel.build/modules/zlib/1.3.1.bcr.5/MODULE.bazel": "eec517b5bbe5492629466e11dae908d043364302283de25581e3eb944326c4ca", - "https://bcr.bazel.build/modules/zlib/1.3.1.bcr.5/source.json": "22bc55c47af97246cfc093d0acf683a7869377de362b5d1c552c2c2e16b7a806", - "https://bcr.bazel.build/modules/zlib/1.3.1/MODULE.bazel": "751c9940dcfe869f5f7274e1295422a34623555916eb98c174c1e945594bf198" - }, - "selectedYankedVersions": {}, - "moduleExtensions": { - "@@pybind11_bazel+//:internal_configure.bzl%internal_configure_extension": { - "general": { - "bzlTransitiveDigest": "BsgERqYptkJeeC8wFaLj251IoTZTSy/58WA6kVp0q8I=", - "usagesDigest": "D1r3lfzMuUBFxgG8V6o0bQTLMk3GkaGOaPzw53wrwyw=", - "recordedInputs": [ - "REPO_MAPPING:pybind11_bazel+,bazel_tools bazel_tools", - "FILE:@@pybind11_bazel+//MODULE.bazel e6f4c20442eaa7c90d7190d8dc539d0ab422f95c65a57cc59562170c58ae3d34" - ], - "generatedRepoSpecs": { - "pybind11": { - "repoRuleId": "@@bazel_tools//tools/build_defs/repo:http.bzl%http_archive", - "attributes": { - "build_file": "@@pybind11_bazel+//:pybind11-BUILD.bazel", - "strip_prefix": "pybind11-2.12.0", - "urls": [ - "https://github.com/pybind/pybind11/archive/v2.12.0.zip" - ] - } - } - } - } - }, - "@@rules_kotlin+//src/main/starlark/core/repositories:bzlmod_setup.bzl%rules_kotlin_extensions": { - "general": { - "bzlTransitiveDigest": "WDuVCwqV73DvcHB5Of+02lhUZgPxr9MqtpvMRyUi0eQ=", - "usagesDigest": "QI2z8ZUR+mqtbwsf2fLqYdJAkPOHdOV+tF2yVAUgRzw=", - "recordedInputs": [ - "REPO_MAPPING:rules_kotlin+,bazel_tools bazel_tools" - ], - "generatedRepoSpecs": { - "com_github_jetbrains_kotlin_git": { - "repoRuleId": "@@rules_kotlin+//src/main/starlark/core/repositories:compiler.bzl%kotlin_compiler_git_repository", - "attributes": { - "urls": [ - "https://github.com/JetBrains/kotlin/releases/download/v1.9.23/kotlin-compiler-1.9.23.zip" - ], - "sha256": "93137d3aab9afa9b27cb06a824c2324195c6b6f6179d8a8653f440f5bd58be88" - } - }, - "com_github_jetbrains_kotlin": { - "repoRuleId": "@@rules_kotlin+//src/main/starlark/core/repositories:compiler.bzl%kotlin_capabilities_repository", - "attributes": { - "git_repository_name": "com_github_jetbrains_kotlin_git", - "compiler_version": "1.9.23" - } - }, - "com_github_google_ksp": { - "repoRuleId": "@@rules_kotlin+//src/main/starlark/core/repositories:ksp.bzl%ksp_compiler_plugin_repository", - "attributes": { - "urls": [ - "https://github.com/google/ksp/releases/download/1.9.23-1.0.20/artifacts.zip" - ], - "sha256": "ee0618755913ef7fd6511288a232e8fad24838b9af6ea73972a76e81053c8c2d", - "strip_version": "1.9.23-1.0.20" - } - }, - "com_github_pinterest_ktlint": { - "repoRuleId": "@@bazel_tools//tools/build_defs/repo:http.bzl%http_file", - "attributes": { - "sha256": "01b2e0ef893383a50dbeb13970fe7fa3be36ca3e83259e01649945b09d736985", - "urls": [ - "https://github.com/pinterest/ktlint/releases/download/1.3.0/ktlint" - ], - "executable": true - } - }, - "rules_android": { - "repoRuleId": "@@bazel_tools//tools/build_defs/repo:http.bzl%http_archive", - "attributes": { - "sha256": "cd06d15dd8bb59926e4d65f9003bfc20f9da4b2519985c27e190cddc8b7a7806", - "strip_prefix": "rules_android-0.1.1", - "urls": [ - "https://github.com/bazelbuild/rules_android/archive/v0.1.1.zip" - ] - } - } - } - } - }, - "@@rules_python+//python/extensions:config.bzl%config": { - "general": { - "bzlTransitiveDigest": "L+2ZCpb7GIJqvWaXpzkfk/80d916sIFVmSPzrSm6of8=", - "usagesDigest": "ZVSXMAGpD+xzVNPuvF1IoLBkty7TROO0+akMapt1pAg=", - "recordedInputs": [ - "REPO_MAPPING:rules_python+,bazel_tools bazel_tools", - "REPO_MAPPING:rules_python+,pypi__build rules_python++config+pypi__build", - "REPO_MAPPING:rules_python+,pypi__click rules_python++config+pypi__click", - "REPO_MAPPING:rules_python+,pypi__colorama rules_python++config+pypi__colorama", - "REPO_MAPPING:rules_python+,pypi__importlib_metadata rules_python++config+pypi__importlib_metadata", - "REPO_MAPPING:rules_python+,pypi__installer rules_python++config+pypi__installer", - "REPO_MAPPING:rules_python+,pypi__more_itertools rules_python++config+pypi__more_itertools", - "REPO_MAPPING:rules_python+,pypi__packaging rules_python++config+pypi__packaging", - "REPO_MAPPING:rules_python+,pypi__pep517 rules_python++config+pypi__pep517", - "REPO_MAPPING:rules_python+,pypi__pip rules_python++config+pypi__pip", - "REPO_MAPPING:rules_python+,pypi__pip_tools rules_python++config+pypi__pip_tools", - "REPO_MAPPING:rules_python+,pypi__pyproject_hooks rules_python++config+pypi__pyproject_hooks", - "REPO_MAPPING:rules_python+,pypi__setuptools rules_python++config+pypi__setuptools", - "REPO_MAPPING:rules_python+,pypi__tomli rules_python++config+pypi__tomli", - "REPO_MAPPING:rules_python+,pypi__wheel rules_python++config+pypi__wheel", - "REPO_MAPPING:rules_python+,pypi__zipp rules_python++config+pypi__zipp" - ], - "generatedRepoSpecs": { - "rules_python_internal": { - "repoRuleId": "@@rules_python+//python/private:internal_config_repo.bzl%internal_config_repo", - "attributes": { - "transition_setting_generators": {}, - "transition_settings": [] - } - }, - "pypi__build": { - "repoRuleId": "@@bazel_tools//tools/build_defs/repo:http.bzl%http_archive", - "attributes": { - "url": "https://files.pythonhosted.org/packages/e2/03/f3c8ba0a6b6e30d7d18c40faab90807c9bb5e9a1e3b2fe2008af624a9c97/build-1.2.1-py3-none-any.whl", - "sha256": "75e10f767a433d9a86e50d83f418e83efc18ede923ee5ff7df93b6cb0306c5d4", - "type": "zip", - "build_file_content": "package(default_visibility = [\"//visibility:public\"])\n\nload(\"@rules_python//python:py_library.bzl\", \"py_library\")\n\npy_library(\n name = \"lib\",\n srcs = glob([\"**/*.py\"]),\n data = glob([\"**/*\"], exclude=[\n # These entries include those put into user-installed dependencies by\n # data_exclude to avoid non-determinism.\n \"**/*.py\",\n \"**/*.pyc\",\n \"**/*.pyc.*\", # During pyc creation, temp files named *.pyc.NNN are created\n \"**/*.dist-info/RECORD\",\n \"BUILD\",\n \"WORKSPACE\",\n ]),\n # This makes this directory a top-level in the python import\n # search path for anything that depends on this.\n imports = [\".\"],\n)\n" - } - }, - "pypi__click": { - "repoRuleId": "@@bazel_tools//tools/build_defs/repo:http.bzl%http_archive", - "attributes": { - "url": "https://files.pythonhosted.org/packages/00/2e/d53fa4befbf2cfa713304affc7ca780ce4fc1fd8710527771b58311a3229/click-8.1.7-py3-none-any.whl", - "sha256": "ae74fb96c20a0277a1d615f1e4d73c8414f5a98db8b799a7931d1582f3390c28", - "type": "zip", - "build_file_content": "package(default_visibility = [\"//visibility:public\"])\n\nload(\"@rules_python//python:py_library.bzl\", \"py_library\")\n\npy_library(\n name = \"lib\",\n srcs = glob([\"**/*.py\"]),\n data = glob([\"**/*\"], exclude=[\n # These entries include those put into user-installed dependencies by\n # data_exclude to avoid non-determinism.\n \"**/*.py\",\n \"**/*.pyc\",\n \"**/*.pyc.*\", # During pyc creation, temp files named *.pyc.NNN are created\n \"**/*.dist-info/RECORD\",\n \"BUILD\",\n \"WORKSPACE\",\n ]),\n # This makes this directory a top-level in the python import\n # search path for anything that depends on this.\n imports = [\".\"],\n)\n" - } - }, - "pypi__colorama": { - "repoRuleId": "@@bazel_tools//tools/build_defs/repo:http.bzl%http_archive", - "attributes": { - "url": "https://files.pythonhosted.org/packages/d1/d6/3965ed04c63042e047cb6a3e6ed1a63a35087b6a609aa3a15ed8ac56c221/colorama-0.4.6-py2.py3-none-any.whl", - "sha256": "4f1d9991f5acc0ca119f9d443620b77f9d6b33703e51011c16baf57afb285fc6", - "type": "zip", - "build_file_content": "package(default_visibility = [\"//visibility:public\"])\n\nload(\"@rules_python//python:py_library.bzl\", \"py_library\")\n\npy_library(\n name = \"lib\",\n srcs = glob([\"**/*.py\"]),\n data = glob([\"**/*\"], exclude=[\n # These entries include those put into user-installed dependencies by\n # data_exclude to avoid non-determinism.\n \"**/*.py\",\n \"**/*.pyc\",\n \"**/*.pyc.*\", # During pyc creation, temp files named *.pyc.NNN are created\n \"**/*.dist-info/RECORD\",\n \"BUILD\",\n \"WORKSPACE\",\n ]),\n # This makes this directory a top-level in the python import\n # search path for anything that depends on this.\n imports = [\".\"],\n)\n" - } - }, - "pypi__importlib_metadata": { - "repoRuleId": "@@bazel_tools//tools/build_defs/repo:http.bzl%http_archive", - "attributes": { - "url": "https://files.pythonhosted.org/packages/2d/0a/679461c511447ffaf176567d5c496d1de27cbe34a87df6677d7171b2fbd4/importlib_metadata-7.1.0-py3-none-any.whl", - "sha256": "30962b96c0c223483ed6cc7280e7f0199feb01a0e40cfae4d4450fc6fab1f570", - "type": "zip", - "build_file_content": "package(default_visibility = [\"//visibility:public\"])\n\nload(\"@rules_python//python:py_library.bzl\", \"py_library\")\n\npy_library(\n name = \"lib\",\n srcs = glob([\"**/*.py\"]),\n data = glob([\"**/*\"], exclude=[\n # These entries include those put into user-installed dependencies by\n # data_exclude to avoid non-determinism.\n \"**/*.py\",\n \"**/*.pyc\",\n \"**/*.pyc.*\", # During pyc creation, temp files named *.pyc.NNN are created\n \"**/*.dist-info/RECORD\",\n \"BUILD\",\n \"WORKSPACE\",\n ]),\n # This makes this directory a top-level in the python import\n # search path for anything that depends on this.\n imports = [\".\"],\n)\n" - } - }, - "pypi__installer": { - "repoRuleId": "@@bazel_tools//tools/build_defs/repo:http.bzl%http_archive", - "attributes": { - "url": "https://files.pythonhosted.org/packages/e5/ca/1172b6638d52f2d6caa2dd262ec4c811ba59eee96d54a7701930726bce18/installer-0.7.0-py3-none-any.whl", - "sha256": "05d1933f0a5ba7d8d6296bb6d5018e7c94fa473ceb10cf198a92ccea19c27b53", - "type": "zip", - "build_file_content": "package(default_visibility = [\"//visibility:public\"])\n\nload(\"@rules_python//python:py_library.bzl\", \"py_library\")\n\npy_library(\n name = \"lib\",\n srcs = glob([\"**/*.py\"]),\n data = glob([\"**/*\"], exclude=[\n # These entries include those put into user-installed dependencies by\n # data_exclude to avoid non-determinism.\n \"**/*.py\",\n \"**/*.pyc\",\n \"**/*.pyc.*\", # During pyc creation, temp files named *.pyc.NNN are created\n \"**/*.dist-info/RECORD\",\n \"BUILD\",\n \"WORKSPACE\",\n ]),\n # This makes this directory a top-level in the python import\n # search path for anything that depends on this.\n imports = [\".\"],\n)\n" - } - }, - "pypi__more_itertools": { - "repoRuleId": "@@bazel_tools//tools/build_defs/repo:http.bzl%http_archive", - "attributes": { - "url": "https://files.pythonhosted.org/packages/50/e2/8e10e465ee3987bb7c9ab69efb91d867d93959095f4807db102d07995d94/more_itertools-10.2.0-py3-none-any.whl", - "sha256": "686b06abe565edfab151cb8fd385a05651e1fdf8f0a14191e4439283421f8684", - "type": "zip", - "build_file_content": "package(default_visibility = [\"//visibility:public\"])\n\nload(\"@rules_python//python:py_library.bzl\", \"py_library\")\n\npy_library(\n name = \"lib\",\n srcs = glob([\"**/*.py\"]),\n data = glob([\"**/*\"], exclude=[\n # These entries include those put into user-installed dependencies by\n # data_exclude to avoid non-determinism.\n \"**/*.py\",\n \"**/*.pyc\",\n \"**/*.pyc.*\", # During pyc creation, temp files named *.pyc.NNN are created\n \"**/*.dist-info/RECORD\",\n \"BUILD\",\n \"WORKSPACE\",\n ]),\n # This makes this directory a top-level in the python import\n # search path for anything that depends on this.\n imports = [\".\"],\n)\n" - } - }, - "pypi__packaging": { - "repoRuleId": "@@bazel_tools//tools/build_defs/repo:http.bzl%http_archive", - "attributes": { - "url": "https://files.pythonhosted.org/packages/49/df/1fceb2f8900f8639e278b056416d49134fb8d84c5942ffaa01ad34782422/packaging-24.0-py3-none-any.whl", - "sha256": "2ddfb553fdf02fb784c234c7ba6ccc288296ceabec964ad2eae3777778130bc5", - "type": "zip", - "build_file_content": "package(default_visibility = [\"//visibility:public\"])\n\nload(\"@rules_python//python:py_library.bzl\", \"py_library\")\n\npy_library(\n name = \"lib\",\n srcs = glob([\"**/*.py\"]),\n data = glob([\"**/*\"], exclude=[\n # These entries include those put into user-installed dependencies by\n # data_exclude to avoid non-determinism.\n \"**/*.py\",\n \"**/*.pyc\",\n \"**/*.pyc.*\", # During pyc creation, temp files named *.pyc.NNN are created\n \"**/*.dist-info/RECORD\",\n \"BUILD\",\n \"WORKSPACE\",\n ]),\n # This makes this directory a top-level in the python import\n # search path for anything that depends on this.\n imports = [\".\"],\n)\n" - } - }, - "pypi__pep517": { - "repoRuleId": "@@bazel_tools//tools/build_defs/repo:http.bzl%http_archive", - "attributes": { - "url": "https://files.pythonhosted.org/packages/25/6e/ca4a5434eb0e502210f591b97537d322546e4833dcb4d470a48c375c5540/pep517-0.13.1-py3-none-any.whl", - "sha256": "31b206f67165b3536dd577c5c3f1518e8fbaf38cbc57efff8369a392feff1721", - "type": "zip", - "build_file_content": "package(default_visibility = [\"//visibility:public\"])\n\nload(\"@rules_python//python:py_library.bzl\", \"py_library\")\n\npy_library(\n name = \"lib\",\n srcs = glob([\"**/*.py\"]),\n data = glob([\"**/*\"], exclude=[\n # These entries include those put into user-installed dependencies by\n # data_exclude to avoid non-determinism.\n \"**/*.py\",\n \"**/*.pyc\",\n \"**/*.pyc.*\", # During pyc creation, temp files named *.pyc.NNN are created\n \"**/*.dist-info/RECORD\",\n \"BUILD\",\n \"WORKSPACE\",\n ]),\n # This makes this directory a top-level in the python import\n # search path for anything that depends on this.\n imports = [\".\"],\n)\n" - } - }, - "pypi__pip": { - "repoRuleId": "@@bazel_tools//tools/build_defs/repo:http.bzl%http_archive", - "attributes": { - "url": "https://files.pythonhosted.org/packages/8a/6a/19e9fe04fca059ccf770861c7d5721ab4c2aebc539889e97c7977528a53b/pip-24.0-py3-none-any.whl", - "sha256": "ba0d021a166865d2265246961bec0152ff124de910c5cc39f1156ce3fa7c69dc", - "type": "zip", - "build_file_content": "package(default_visibility = [\"//visibility:public\"])\n\nload(\"@rules_python//python:py_library.bzl\", \"py_library\")\n\npy_library(\n name = \"lib\",\n srcs = glob([\"**/*.py\"]),\n data = glob([\"**/*\"], exclude=[\n # These entries include those put into user-installed dependencies by\n # data_exclude to avoid non-determinism.\n \"**/*.py\",\n \"**/*.pyc\",\n \"**/*.pyc.*\", # During pyc creation, temp files named *.pyc.NNN are created\n \"**/*.dist-info/RECORD\",\n \"BUILD\",\n \"WORKSPACE\",\n ]),\n # This makes this directory a top-level in the python import\n # search path for anything that depends on this.\n imports = [\".\"],\n)\n" - } - }, - "pypi__pip_tools": { - "repoRuleId": "@@bazel_tools//tools/build_defs/repo:http.bzl%http_archive", - "attributes": { - "url": "https://files.pythonhosted.org/packages/0d/dc/38f4ce065e92c66f058ea7a368a9c5de4e702272b479c0992059f7693941/pip_tools-7.4.1-py3-none-any.whl", - "sha256": "4c690e5fbae2f21e87843e89c26191f0d9454f362d8acdbd695716493ec8b3a9", - "type": "zip", - "build_file_content": "package(default_visibility = [\"//visibility:public\"])\n\nload(\"@rules_python//python:py_library.bzl\", \"py_library\")\n\npy_library(\n name = \"lib\",\n srcs = glob([\"**/*.py\"]),\n data = glob([\"**/*\"], exclude=[\n # These entries include those put into user-installed dependencies by\n # data_exclude to avoid non-determinism.\n \"**/*.py\",\n \"**/*.pyc\",\n \"**/*.pyc.*\", # During pyc creation, temp files named *.pyc.NNN are created\n \"**/*.dist-info/RECORD\",\n \"BUILD\",\n \"WORKSPACE\",\n ]),\n # This makes this directory a top-level in the python import\n # search path for anything that depends on this.\n imports = [\".\"],\n)\n" - } - }, - "pypi__pyproject_hooks": { - "repoRuleId": "@@bazel_tools//tools/build_defs/repo:http.bzl%http_archive", - "attributes": { - "url": "https://files.pythonhosted.org/packages/ae/f3/431b9d5fe7d14af7a32340792ef43b8a714e7726f1d7b69cc4e8e7a3f1d7/pyproject_hooks-1.1.0-py3-none-any.whl", - "sha256": "7ceeefe9aec63a1064c18d939bdc3adf2d8aa1988a510afec15151578b232aa2", - "type": "zip", - "build_file_content": "package(default_visibility = [\"//visibility:public\"])\n\nload(\"@rules_python//python:py_library.bzl\", \"py_library\")\n\npy_library(\n name = \"lib\",\n srcs = glob([\"**/*.py\"]),\n data = glob([\"**/*\"], exclude=[\n # These entries include those put into user-installed dependencies by\n # data_exclude to avoid non-determinism.\n \"**/*.py\",\n \"**/*.pyc\",\n \"**/*.pyc.*\", # During pyc creation, temp files named *.pyc.NNN are created\n \"**/*.dist-info/RECORD\",\n \"BUILD\",\n \"WORKSPACE\",\n ]),\n # This makes this directory a top-level in the python import\n # search path for anything that depends on this.\n imports = [\".\"],\n)\n" - } - }, - "pypi__setuptools": { - "repoRuleId": "@@bazel_tools//tools/build_defs/repo:http.bzl%http_archive", - "attributes": { - "url": "https://files.pythonhosted.org/packages/90/99/158ad0609729111163fc1f674a5a42f2605371a4cf036d0441070e2f7455/setuptools-78.1.1-py3-none-any.whl", - "sha256": "c3a9c4211ff4c309edb8b8c4f1cbfa7ae324c4ba9f91ff254e3d305b9fd54561", - "type": "zip", - "build_file_content": "package(default_visibility = [\"//visibility:public\"])\n\nload(\"@rules_python//python:py_library.bzl\", \"py_library\")\n\npy_library(\n name = \"lib\",\n srcs = glob([\"**/*.py\"]),\n data = glob([\"**/*\"], exclude=[\n # These entries include those put into user-installed dependencies by\n # data_exclude to avoid non-determinism.\n \"**/*.py\",\n \"**/*.pyc\",\n \"**/*.pyc.*\", # During pyc creation, temp files named *.pyc.NNN are created\n \"**/*.dist-info/RECORD\",\n \"BUILD\",\n \"WORKSPACE\",\n ]),\n # This makes this directory a top-level in the python import\n # search path for anything that depends on this.\n imports = [\".\"],\n)\n" - } - }, - "pypi__tomli": { - "repoRuleId": "@@bazel_tools//tools/build_defs/repo:http.bzl%http_archive", - "attributes": { - "url": "https://files.pythonhosted.org/packages/97/75/10a9ebee3fd790d20926a90a2547f0bf78f371b2f13aa822c759680ca7b9/tomli-2.0.1-py3-none-any.whl", - "sha256": "939de3e7a6161af0c887ef91b7d41a53e7c5a1ca976325f429cb46ea9bc30ecc", - "type": "zip", - "build_file_content": "package(default_visibility = [\"//visibility:public\"])\n\nload(\"@rules_python//python:py_library.bzl\", \"py_library\")\n\npy_library(\n name = \"lib\",\n srcs = glob([\"**/*.py\"]),\n data = glob([\"**/*\"], exclude=[\n # These entries include those put into user-installed dependencies by\n # data_exclude to avoid non-determinism.\n \"**/*.py\",\n \"**/*.pyc\",\n \"**/*.pyc.*\", # During pyc creation, temp files named *.pyc.NNN are created\n \"**/*.dist-info/RECORD\",\n \"BUILD\",\n \"WORKSPACE\",\n ]),\n # This makes this directory a top-level in the python import\n # search path for anything that depends on this.\n imports = [\".\"],\n)\n" - } - }, - "pypi__wheel": { - "repoRuleId": "@@bazel_tools//tools/build_defs/repo:http.bzl%http_archive", - "attributes": { - "url": "https://files.pythonhosted.org/packages/7d/cd/d7460c9a869b16c3dd4e1e403cce337df165368c71d6af229a74699622ce/wheel-0.43.0-py3-none-any.whl", - "sha256": "55c570405f142630c6b9f72fe09d9b67cf1477fcf543ae5b8dcb1f5b7377da81", - "type": "zip", - "build_file_content": "package(default_visibility = [\"//visibility:public\"])\n\nload(\"@rules_python//python:py_library.bzl\", \"py_library\")\n\npy_library(\n name = \"lib\",\n srcs = glob([\"**/*.py\"]),\n data = glob([\"**/*\"], exclude=[\n # These entries include those put into user-installed dependencies by\n # data_exclude to avoid non-determinism.\n \"**/*.py\",\n \"**/*.pyc\",\n \"**/*.pyc.*\", # During pyc creation, temp files named *.pyc.NNN are created\n \"**/*.dist-info/RECORD\",\n \"BUILD\",\n \"WORKSPACE\",\n ]),\n # This makes this directory a top-level in the python import\n # search path for anything that depends on this.\n imports = [\".\"],\n)\n" - } - }, - "pypi__zipp": { - "repoRuleId": "@@bazel_tools//tools/build_defs/repo:http.bzl%http_archive", - "attributes": { - "url": "https://files.pythonhosted.org/packages/da/55/a03fd7240714916507e1fcf7ae355bd9d9ed2e6db492595f1a67f61681be/zipp-3.18.2-py3-none-any.whl", - "sha256": "dce197b859eb796242b0622af1b8beb0a722d52aa2f57133ead08edd5bf5374e", - "type": "zip", - "build_file_content": "package(default_visibility = [\"//visibility:public\"])\n\nload(\"@rules_python//python:py_library.bzl\", \"py_library\")\n\npy_library(\n name = \"lib\",\n srcs = glob([\"**/*.py\"]),\n data = glob([\"**/*\"], exclude=[\n # These entries include those put into user-installed dependencies by\n # data_exclude to avoid non-determinism.\n \"**/*.py\",\n \"**/*.pyc\",\n \"**/*.pyc.*\", # During pyc creation, temp files named *.pyc.NNN are created\n \"**/*.dist-info/RECORD\",\n \"BUILD\",\n \"WORKSPACE\",\n ]),\n # This makes this directory a top-level in the python import\n # search path for anything that depends on this.\n imports = [\".\"],\n)\n" - } - } - } - } - }, - "@@rules_python+//python/uv:uv.bzl%uv": { - "general": { - "bzlTransitiveDigest": "IgkgxNrtlAec7OnaFrWoCC5NQhHcY/aIz4jsqcNMm8Y=", - "usagesDigest": "H8dQoNZcoqP+Mu0tHZTi4KHATzvNkM5ePuEqoQdklIU=", - "recordedInputs": [ - "REPO_MAPPING:rules_python+,bazel_tools bazel_tools", - "REPO_MAPPING:rules_python+,platforms platforms" - ], - "generatedRepoSpecs": { - "uv": { - "repoRuleId": "@@rules_python+//python/uv/private:uv_toolchains_repo.bzl%uv_toolchains_repo", - "attributes": { - "toolchain_type": "'@@rules_python+//python/uv:uv_toolchain_type'", - "toolchain_names": [ - "none" - ], - "toolchain_implementations": { - "none": "'@@rules_python+//python:none'" - }, - "toolchain_compatible_with": { - "none": [ - "@platforms//:incompatible" - ] - }, - "toolchain_target_settings": {} - } - } - } - } - }, - "@@toolchains_llvm+//toolchain/extensions:distributions.bzl%llvm_distributions": { - "general": { - "bzlTransitiveDigest": "RfYsFLwRh0rVY8ReiYgbOEyr84zuBs7mbcHq3dPY3jI=", - "usagesDigest": "vl2VXD2E16aV3RG06i5tx7yyYASHiQFjvsiNZSDzQIg=", - "recordedInputs": [], - "generatedRepoSpecs": { - "llvm_distributions_data": { - "repoRuleId": "@@toolchains_llvm+//toolchain/internal:distributions_repo.bzl%llvm_distributions_repo", - "attributes": { - "srcs": [ - "@@toolchains_llvm+//toolchain/distributions:pre_github.jsonc", - "@@toolchains_llvm+//toolchain/distributions:github_legacy.jsonc", - "@@toolchains_llvm+//toolchain/distributions:github.jsonc", - "@@toolchains_llvm+//toolchain/distributions:extra.jsonc" - ] - } - } - } - } - } - }, - "facts": { - "@@rules_go+//go:extensions.bzl%go_sdk": { - "1.25.0": { - "aix_ppc64": [ - "go1.25.0.aix-ppc64.tar.gz", - "e5234a7dac67bc86c528fe9752fc9d63557918627707a733ab4cac1a6faed2d4" - ], - "darwin_amd64": [ - "go1.25.0.darwin-amd64.tar.gz", - "5bd60e823037062c2307c71e8111809865116714d6f6b410597cf5075dfd80ef" - ], - "darwin_arm64": [ - "go1.25.0.darwin-arm64.tar.gz", - "544932844156d8172f7a28f77f2ac9c15a23046698b6243f633b0a0b00c0749c" - ], - "dragonfly_amd64": [ - "go1.25.0.dragonfly-amd64.tar.gz", - "5ed3cf9a810a1483822538674f1336c06b51aa1b94d6d545a1a0319a48177120" - ], - "freebsd_386": [ - "go1.25.0.freebsd-386.tar.gz", - "abea5d5c6697e6b5c224731f2158fe87c602996a2a233ac0c4730cd57bf8374e" - ], - "freebsd_amd64": [ - "go1.25.0.freebsd-amd64.tar.gz", - "86e6fe0a29698d7601c4442052dac48bd58d532c51cccb8f1917df648138730b" - ], - "freebsd_arm": [ - "go1.25.0.freebsd-arm.tar.gz", - "d90b78e41921f72f30e8bbc81d9dec2cff7ff384a33d8d8debb24053e4336bfe" - ], - "freebsd_arm64": [ - "go1.25.0.freebsd-arm64.tar.gz", - "451d0da1affd886bfb291b7c63a6018527b269505db21ce6e14724f22ab0662e" - ], - "freebsd_riscv64": [ - "go1.25.0.freebsd-riscv64.tar.gz", - "7b565f76bd8bda46549eeaaefe0e53b251e644c230577290c0f66b1ecdb3cdbe" - ], - "illumos_amd64": [ - "go1.25.0.illumos-amd64.tar.gz", - "b1e1fdaab1ad25aa1c08d7a36c97d45d74b98b89c3f78c6d2145f77face54a2c" - ], - "linux_386": [ - "go1.25.0.linux-386.tar.gz", - "8c602dd9d99bc9453b3995d20ce4baf382cc50855900a0ece5de9929df4a993a" - ], - "linux_amd64": [ - "go1.25.0.linux-amd64.tar.gz", - "2852af0cb20a13139b3448992e69b868e50ed0f8a1e5940ee1de9e19a123b613" - ], - "linux_arm64": [ - "go1.25.0.linux-arm64.tar.gz", - "05de75d6994a2783699815ee553bd5a9327d8b79991de36e38b66862782f54ae" - ], - "linux_armv6l": [ - "go1.25.0.linux-armv6l.tar.gz", - "a5a8f8198fcf00e1e485b8ecef9ee020778bf32a408a4e8873371bfce458cd09" - ], - "linux_loong64": [ - "go1.25.0.linux-loong64.tar.gz", - "cab86b1cf761b1cb3bac86a8877cfc92e7b036fc0d3084123d77013d61432afc" - ], - "linux_mips": [ - "go1.25.0.linux-mips.tar.gz", - "d66b6fb74c3d91b9829dc95ec10ca1f047ef5e89332152f92e136cf0e2da5be1" - ], - "linux_mips64": [ - "go1.25.0.linux-mips64.tar.gz", - "4082e4381a8661bc2a839ff94ba3daf4f6cde20f8fb771b5b3d4762dc84198a2" - ], - "linux_mips64le": [ - "go1.25.0.linux-mips64le.tar.gz", - "70002c299ec7f7175ac2ef673b1b347eecfa54ae11f34416a6053c17f855afcc" - ], - "linux_mipsle": [ - "go1.25.0.linux-mipsle.tar.gz", - "b00a3a39eff099f6df9f1c7355bf28e4589d0586f42d7d4a394efb763d145a73" - ], - "linux_ppc64": [ - "go1.25.0.linux-ppc64.tar.gz", - "df166f33bd98160662560a72ff0b4ba731f969a80f088922bddcf566a88c1ec1" - ], - "linux_ppc64le": [ - "go1.25.0.linux-ppc64le.tar.gz", - "0f18a89e7576cf2c5fa0b487a1635d9bcbf843df5f110e9982c64df52a983ad0" - ], - "linux_riscv64": [ - "go1.25.0.linux-riscv64.tar.gz", - "c018ff74a2c48d55c8ca9b07c8e24163558ffec8bea08b326d6336905d956b67" - ], - "linux_s390x": [ - "go1.25.0.linux-s390x.tar.gz", - "34e5a2e19f2292fbaf8783e3a241e6e49689276aef6510a8060ea5ef54eee408" - ], - "netbsd_386": [ - "go1.25.0.netbsd-386.tar.gz", - "f8586cdb7aa855657609a5c5f6dbf523efa00c2bbd7c76d3936bec80aa6c0aba" - ], - "netbsd_amd64": [ - "go1.25.0.netbsd-amd64.tar.gz", - "ae8dc1469385b86a157a423bb56304ba45730de8a897615874f57dd096db2c2a" - ], - "netbsd_arm": [ - "go1.25.0.netbsd-arm.tar.gz", - "1ff7e4cc764425fc9dd6825eaee79d02b3c7cafffbb3691687c8d672ade76cb7" - ], - "netbsd_arm64": [ - "go1.25.0.netbsd-arm64.tar.gz", - "e1b310739f26724216aa6d7d7208c4031f9ff54c9b5b9a796ddc8bebcb4a5f16" - ], - "openbsd_386": [ - "go1.25.0.openbsd-386.tar.gz", - "4802a9b20e533da91adb84aab42e94aa56cfe3e5475d0550bed3385b182e69d8" - ], - "openbsd_amd64": [ - "go1.25.0.openbsd-amd64.tar.gz", - "c016cd984bebe317b19a4f297c4f50def120dc9788490540c89f28e42f1dabe1" - ], - "openbsd_arm": [ - "go1.25.0.openbsd-arm.tar.gz", - "a1e31d0bf22172ddde42edf5ec811ef81be43433df0948ece52fecb247ccfd8d" - ], - "openbsd_arm64": [ - "go1.25.0.openbsd-arm64.tar.gz", - "343ea8edd8c218196e15a859c6072d0dd3246fbbb168481ab665eb4c4140458d" - ], - "openbsd_ppc64": [ - "go1.25.0.openbsd-ppc64.tar.gz", - "694c14da1bcaeb5e3332d49bdc2b6d155067648f8fe1540c5de8f3cf8e157154" - ], - "openbsd_riscv64": [ - "go1.25.0.openbsd-riscv64.tar.gz", - "aa510ad25cf54c06cd9c70b6d80ded69cb20188ac6e1735655eef29ff7e7885f" - ], - "plan9_386": [ - "go1.25.0.plan9-386.tar.gz", - "46f8cef02086cf04bf186c5912776b56535178d4cb319cd19c9fdbdd29231986" - ], - "plan9_amd64": [ - "go1.25.0.plan9-amd64.tar.gz", - "29b34391d84095e44608a228f63f2f88113a37b74a79781353ec043dfbcb427b" - ], - "plan9_arm": [ - "go1.25.0.plan9-arm.tar.gz", - "0a047107d13ebe7943aaa6d54b1d7bbd2e45e68ce449b52915a818da715799c2" - ], - "solaris_amd64": [ - "go1.25.0.solaris-amd64.tar.gz", - "9977f9e4351984364a3b2b78f8b88bfd1d339812356d5237678514594b7d3611" - ], - "windows_386": [ - "go1.25.0.windows-386.zip", - "df9f39db82a803af0db639e3613a36681ab7a42866b1384b3f3a1045663961a7" - ], - "windows_amd64": [ - "go1.25.0.windows-amd64.zip", - "89efb4f9b30812eee083cc1770fdd2913c14d301064f6454851428f9707d190b" - ], - "windows_arm64": [ - "go1.25.0.windows-arm64.zip", - "27bab004c72b3d7bd05a69b6ec0fc54a309b4b78cc569dd963d8b3ec28bfdb8c" - ] - }, - "1.26.7": { - "aix_ppc64": [ - "go1.26.7.aix-ppc64.tar.gz", - "e5aed98bc83f569a374dbfd79d632b058e5130192fbd4c4ee0d4f6e3d9c101bb" - ], - "darwin_amd64": [ - "go1.26.7.darwin-amd64.tar.gz", - "92e8b34bff3c89ab16404c595669ac8cb004cc2f676dcbd1f5b87a6b8def3b47" - ], - "darwin_arm64": [ - "go1.26.7.darwin-arm64.tar.gz", - "020a1e8224811be75163e920bc77e0926a1390a6aeea19bdcf23f74b9d749f6d" - ], - "dragonfly_amd64": [ - "go1.26.7.dragonfly-amd64.tar.gz", - "d980dc9ecc48de09b2fa1765886abb8c7b0ad738bf8cc483b76019facc2df4d1" - ], - "freebsd_386": [ - "go1.26.7.freebsd-386.tar.gz", - "7eecfa4d6c126bcdedb8e16583ec672857e607659c672c60a9d645f8957da117" - ], - "freebsd_amd64": [ - "go1.26.7.freebsd-amd64.tar.gz", - "2ad37146fa8449907716b494dd268aeae6f79592b875915525c28c8c337ad32a" - ], - "freebsd_arm": [ - "go1.26.7.freebsd-arm.tar.gz", - "bc56379ed419cd93cf7002dcb95f554b3e3a2bd58d804182f8873e313a870b70" - ], - "freebsd_arm64": [ - "go1.26.7.freebsd-arm64.tar.gz", - "23d425a3a51876df8bf050f080b1ae50340048def689fbcc44c939fa23485ce0" - ], - "illumos_amd64": [ - "go1.26.7.illumos-amd64.tar.gz", - "be51df2411786e1fa6325461e509224a94c5e8d001da8e4548d9e028a53a3c22" - ], - "linux_386": [ - "go1.26.7.linux-386.tar.gz", - "5b6fad4a7fe8fa230c935625b67450305be95e2f4f976b731623f76b88fc4821" - ], - "linux_amd64": [ - "go1.26.7.linux-amd64.tar.gz", - "ffb5f8de10c62550dfddab66b36b57030721e0a44a3218e9e1181d7b59f121ca" - ], - "linux_arm64": [ - "go1.26.7.linux-arm64.tar.gz", - "5a4ec883379d51ee9ce1040d5e87f8d35e20387574dd8c947feb01eabc3c1b37" - ], - "linux_armv6l": [ - "go1.26.7.linux-armv6l.tar.gz", - "6a41557335d1d9fa9bfc7439cedcfd0e4df2fc83d916175b5f3eae4685f5fe3f" - ], - "linux_loong64": [ - "go1.26.7.linux-loong64.tar.gz", - "89b5e92446e719719e23db447a41860d7d573252bab0704c9acb09ad97e9eca1" - ], - "linux_mips": [ - "go1.26.7.linux-mips.tar.gz", - "fbdbf78b04494843fde47b15150e12adb7e2c0e2289eacbc18e6629a99604620" - ], - "linux_mips64": [ - "go1.26.7.linux-mips64.tar.gz", - "93b1360a29f4a71dbb6995f149b9312f67f5ce52197fea0042e9c9423809a496" - ], - "linux_mips64le": [ - "go1.26.7.linux-mips64le.tar.gz", - "402dd2bd57fb4c982ef0eccbb4f7209ae98a753856e99baf962f1ac54493d65d" - ], - "linux_mipsle": [ - "go1.26.7.linux-mipsle.tar.gz", - "967da58e88c45c401593a8552be9beedaa989a3484a3105850d2beb6ca9ed698" - ], - "linux_ppc64": [ - "go1.26.7.linux-ppc64.tar.gz", - "8feee58b3fa8d270343519d10d66736150e627d39aee937e6e889ba5445c8be9" - ], - "linux_ppc64le": [ - "go1.26.7.linux-ppc64le.tar.gz", - "22d3b362d557175fd16b79651cab0cad64f8aaaedca745f66d16f44d56bc5de1" - ], - "linux_riscv64": [ - "go1.26.7.linux-riscv64.tar.gz", - "21e1dc0e2f7dd9f80e0e94613fc324f1f99403b99997f9d8c5f80f8132fc642b" - ], - "linux_s390x": [ - "go1.26.7.linux-s390x.tar.gz", - "bc3308e05e9397958ad4aac453e3dc4565ba6d548e9ace0a9b4b165feb6382f2" - ], - "netbsd_386": [ - "go1.26.7.netbsd-386.tar.gz", - "c0a28bd30d8c07966cf7bc43fc6936866fb1a0e84ab4e983d4e8ca15edc1a4d1" - ], - "netbsd_amd64": [ - "go1.26.7.netbsd-amd64.tar.gz", - "dff0b16d129f5e70f3bb38ed98d467404b5b130c7c8b3fe6b074601855f2bcd5" - ], - "netbsd_arm": [ - "go1.26.7.netbsd-arm.tar.gz", - "36d15f8c5c5a5a048251b39ce67ed5a7ae2d072f1aa5c301cbde110670e824b2" - ], - "netbsd_arm64": [ - "go1.26.7.netbsd-arm64.tar.gz", - "b75802b366e5b60aa6cfded871f0a72abadf297c5a68c523c5857822a5b92d34" - ], - "openbsd_386": [ - "go1.26.7.openbsd-386.tar.gz", - "c26201688339b2a10bf980be2d64adfd3a383b31a164377b0123ad78bde6979c" - ], - "openbsd_amd64": [ - "go1.26.7.openbsd-amd64.tar.gz", - "562de088b844764029f59ff52e82218711a4b585ab7ae175b88eff53e051a8df" - ], - "openbsd_arm": [ - "go1.26.7.openbsd-arm.tar.gz", - "646e19cd3449aec767bf75622d368b02c73d529a5478a25f2a7e21148b1a4cb6" - ], - "openbsd_arm64": [ - "go1.26.7.openbsd-arm64.tar.gz", - "2045303632e112290e939dfd4678401109f612f7913f459e2d7d079e750723f5" - ], - "openbsd_ppc64": [ - "go1.26.7.openbsd-ppc64.tar.gz", - "23e89de68225dc41dd981601602e67fab0c4ca87af8f70a25131c06a301f9000" - ], - "openbsd_riscv64": [ - "go1.26.7.openbsd-riscv64.tar.gz", - "c0de2c6c353a582ca7682ed5a550056a8b5d59649260da4d1e327cf96684c822" - ], - "plan9_386": [ - "go1.26.7.plan9-386.tar.gz", - "14ae7414a2ea05d26e1fc499d9b372c7f8f17726c49a5222fa9063b0f752a22c" - ], - "plan9_amd64": [ - "go1.26.7.plan9-amd64.tar.gz", - "f9459b8137bdb81621d4e0cdba4c849a617c89c11a3088083ad3a8c97467c6a5" - ], - "plan9_arm": [ - "go1.26.7.plan9-arm.tar.gz", - "4f444bd2e0f8a08ba8799131dd3531bea8723bf96b8ffa0e0948936a3c4aa8f0" - ], - "solaris_amd64": [ - "go1.26.7.solaris-amd64.tar.gz", - "04b5b280e0e0de1b91d7da5e34801dbf75e5621615f80cfd9935091d673f1238" - ], - "windows_386": [ - "go1.26.7.windows-386.zip", - "59cf0b9814c41c960d70df3cd5360fda0793e5d2230737d58e009392181fb636" - ], - "windows_amd64": [ - "go1.26.7.windows-amd64.zip", - "f4f534a486e4bc3387fa18f08208f2f854b7aaea8a08f2a2d829a914a05abb11" - ], - "windows_arm64": [ - "go1.26.7.windows-arm64.zip", - "6f1b08de9e2dd94f69c52e524ab6834737275253291e8fd7f1c12ed4eceeda89" - ] - } - } - }, - "factsVersions": {} -} diff --git a/Makefile b/Makefile index d0bfbbdc6..f8ed0b1a6 100644 --- a/Makefile +++ b/Makefile @@ -43,35 +43,14 @@ export PATH := $(GIT_WINDOWS_ROOT)/usr/bin;$(PATH) endif endif -.PHONY: all build doctor-build test test-icu-path test-full-cgo test-regression test-upgrade test-cross-version test-migration corpus-regen githooks-regen bench bench-quick clean clean-test-tmp install install-force help check-up-to-date fmt fmt-check check-testing-short -.PHONY: lint lint-changed vet -.PHONY: check check-go test-go check-docs-go -.PHONY: ci-pr-core ci-pr-lint ci-complexity ci-complexity-diff ci-complexity-check ci-package-mcp ci-package-npm nogo-ownership +.PHONY: all build doctor-build test test-icu-path test-full-cgo test-regression test-upgrade test-cross-version test-migration corpus-regen bench bench-quick clean clean-test-tmp install install-force help check-up-to-date fmt fmt-check check-testing-short +.PHONY: ci-pr-core ci-pr-policy ci-pr-lint ci-package-mcp ci-package-npm .PHONY: api-gen api-check -.PHONY: bazel-sync bazel-sync-check # Default target all: build -# Build and test engine. Bazel is what CI gates on: .github/workflows/bazel.yml -# runs `bazel test` lanes on rbe-west, and nogo (lint + vet), formatting and -# the repository guards are Bazel targets there. The primary targets (test, -# check, check-docs) run the same `bazel test` commands as those lanes, so a -# local run shares CI's action keys and its remote cache (engdocs/TESTING.md -# "Building and testing"). Each keeps a plain go twin under an explicit -go -# name (test-go, check-go, check-docs-go) for offline work and hosts Bazel does -# not serve; that twin is an inner-loop convenience, not what CI enforces. -# GitHub Actions jobs that still run Go-native suites call the -go names. -BAZEL ?= bazel -# Extra flags for every `bazel test` below: --config=fork-cache (contributors: -# the anonymous read-only cache) or --config=remote-exec (maintainers with an -# rbe-west client certificate). Hosts whose ~/.bazelrc names the executor need -# neither. Better: put `build --config=...` in the gitignored .bazelrc.local. -BAZEL_FLAGS ?= -BAZEL_TEST = $(BAZEL) test $(BAZEL_FLAGS) - BUILD_DIR := . -BD_BUILD_OUTPUT := $(BUILD_DIR)/bd$(if $(filter Windows_NT,$(OS)),.exe) GIT_BUILD := $(shell git rev-parse --short HEAD) ifeq ($(OS),Windows_NT) INSTALL_DIR := $(USERPROFILE)/.local/bin @@ -124,21 +103,21 @@ build: ifeq ($(OS),Windows_NT) @if [ -n "$$CC" ]; then \ echo "Using CC=$$CC"; \ - go build -tags "$(BUILD_TAGS)" -ldflags="-X main.Build=$(GIT_BUILD)" -o "$(BD_BUILD_OUTPUT)" ./cmd/bd; \ + go build -tags "$(BUILD_TAGS)" -ldflags="-X main.Build=$(GIT_BUILD)" -o $(BUILD_DIR)/bd.exe ./cmd/bd; \ elif command -v gcc >/dev/null 2>&1; then \ - CC=gcc go build -tags "$(BUILD_TAGS)" -ldflags="-X main.Build=$(GIT_BUILD)" -o "$(BD_BUILD_OUTPUT)" ./cmd/bd; \ + CC=gcc go build -tags "$(BUILD_TAGS)" -ldflags="-X main.Build=$(GIT_BUILD)" -o $(BUILD_DIR)/bd.exe ./cmd/bd; \ elif command -v clang >/dev/null 2>&1 && clang -dumpmachine 2>/dev/null | grep -qi 'windows.*gnu'; then \ - CC=clang go build -tags "$(BUILD_TAGS)" -ldflags="-X main.Build=$(GIT_BUILD)" -o "$(BD_BUILD_OUTPUT)" ./cmd/bd; \ + CC=clang go build -tags "$(BUILD_TAGS)" -ldflags="-X main.Build=$(GIT_BUILD)" -o $(BUILD_DIR)/bd.exe ./cmd/bd; \ else \ for bin in $(WINDOWS_CGO_BINS); do \ if [ -x "$$bin/gcc.exe" ]; then \ echo "Using Windows CGO gcc from $$bin"; \ - PATH="$$bin:$$PATH" CC=gcc go build -tags "$(BUILD_TAGS)" -ldflags="-X main.Build=$(GIT_BUILD)" -o "$(BD_BUILD_OUTPUT)" ./cmd/bd; \ + PATH="$$bin:$$PATH" CC=gcc go build -tags "$(BUILD_TAGS)" -ldflags="-X main.Build=$(GIT_BUILD)" -o $(BUILD_DIR)/bd.exe ./cmd/bd; \ exit $$?; \ fi; \ if [ -x "$$bin/clang.exe" ] && "$$bin/clang.exe" -dumpmachine 2>/dev/null | grep -qi 'windows.*gnu'; then \ echo "Using Windows CGO clang from $$bin"; \ - PATH="$$bin:$$PATH" CC=clang go build -tags "$(BUILD_TAGS)" -ldflags="-X main.Build=$(GIT_BUILD)" -o "$(BD_BUILD_OUTPUT)" ./cmd/bd; \ + PATH="$$bin:$$PATH" CC=clang go build -tags "$(BUILD_TAGS)" -ldflags="-X main.Build=$(GIT_BUILD)" -o $(BUILD_DIR)/bd.exe ./cmd/bd; \ exit $$?; \ fi; \ done; \ @@ -148,9 +127,9 @@ ifeq ($(OS),Windows_NT) exit 1; \ fi else - go build -tags "$(BUILD_TAGS)" -ldflags="-X main.Build=$(GIT_BUILD)" -o "$(BD_BUILD_OUTPUT)" ./cmd/bd + go build -tags "$(BUILD_TAGS)" -ldflags="-X main.Build=$(GIT_BUILD)" -o $(BUILD_DIR)/bd ./cmd/bd ifeq ($(shell uname),Darwin) - @codesign -s - -f "$(BD_BUILD_OUTPUT)" 2>/dev/null || true + @codesign -s - -f $(BUILD_DIR)/bd 2>/dev/null || true @echo "Signed bd for macOS" endif endif @@ -192,27 +171,11 @@ doctor-build: echo " CGO_ENABLED=1 go build -tags gms_pure_go ./cmd/bd" ;; \ esac -# bazel.yml's test lane (`bazel test //... --config=ci`): every untagged -# go_test with PR Core's selection (race, -short, skips), plus nogo lint/vet, -# formatting and the repository guards. The other lanes' commands are in -# engdocs/TESTING.md "Building and testing". +# Run all tests (skips known broken tests listed in .test-skip) test: - $(BAZEL_TEST) //... --config=ci - -# Plain go test (skips known broken tests listed in .test-skip): an inner-loop -# convenience without Bazel; CI does not run it. -test-go: @echo "Running tests..." @TEST_COVER=1 ./scripts/test.sh -# Fast quality gates: the testing.Short policy, the full nogo lint gate -# (native plus the windows/darwin passes) and `make test`. -check: check-testing-short ci-pr-lint test - -# The same gates without Bazel where they have a Go form: gofmt and go test. -# nogo has no go twin, so this is a convenience CI does not enforce. -check-go: fmt-check check-testing-short test-go - # Run the opt-in ICU regex path test suite (no skip list). # This is a local developer workflow for intentionally exercising the leftover # ICU path; it is not part of normal validation. @@ -229,70 +192,12 @@ test-full-cgo: ci-pr-core: @./scripts/ci/pr-core.sh -# Lint and vet are nogo (//tools/nogo): go test's vet checks plus the -# golangci-lint linters .golangci.yml enables, validated beside every Go -# compile, so any `bazel build`/`bazel test` (local or on rbe-west) fails on a -# finding. These targets ask Bazel for the analysis alone (.bazelrc's -# --config=nogo; nothing is linked). +ci-pr-policy: + @./scripts/ci/pr-policy.sh + ci-pr-lint: @./scripts/ci/pr-lint.sh -# nogo-ownership is a dynamic, advisory cross-check (not part of the required -# //scripts static suite, which cannot shell out to Bazel): it asks real -# `bazel aquery` whether the nogo owner lane for each configuration group -# (today: race, bazel-test/--config=ci) really does validate every file a -# non-owner lane (embedded, doltserver, doltserver-proxied, dolt-race, which -# pass --norun_validations) compiles. See tools/bazel/nogo_ownership.py and -# scripts/nogo_lint_policy_test.go's nogoConfigurations table. -nogo-ownership: - @python3 tools/bazel/nogo_ownership.py - -# The CI lint gate: native, plus //tools/bazel:release_cross for windows/amd64 -# and darwin/arm64 (BD_LINT_TARGETS selects; scripts/pr-lint). -lint: ci-pr-lint - -# go test's vet checks run inside nogo; same as lint. -vet: lint - -# nogo over the Bazel packages of changed Go files, natively (the pre-commit -# hook's lint). LINT_CHANGED_SCOPE: staged, or worktree (changed against -# LINT_CHANGED_REF, staged, and untracked). -LINT_CHANGED_SCOPE ?= worktree -LINT_CHANGED_REF ?= HEAD -lint-changed: - @case "$(LINT_CHANGED_SCOPE)" in \ - staged) files="$$(git diff --cached --name-only --diff-filter=ACMRT -- '*.go')" ;; \ - worktree) files="$$( \ - git diff --name-only --diff-filter=ACMRT "$(LINT_CHANGED_REF)" -- '*.go'; \ - git diff --cached --name-only --diff-filter=ACMRT -- '*.go'; \ - git ls-files --others --exclude-standard -- '*.go')" ;; \ - *) echo "unknown LINT_CHANGED_SCOPE=$(LINT_CHANGED_SCOPE); expected staged or worktree" >&2; exit 2 ;; \ - esac; \ - packages=""; missing=""; \ - for file in $$files; do \ - dir="$$(dirname "$$file")"; \ - case "/$$dir/" in */testdata/*) continue ;; esac; \ - if [ -f "$$dir/BUILD.bazel" ]; then packages="$$packages //$${dir#.}:all"; else missing="$$missing $$dir"; fi; \ - done; \ - if [ -n "$$missing" ]; then \ - echo "lint-changed: no BUILD.bazel in$$missing; run 'make bazel-sync'" >&2; exit 1; \ - fi; \ - packages="$$(printf '%s\n' $$packages | sort -u | sed 's#^///#//#; s#^//\.:#//:#')"; \ - if [ -z "$$packages" ]; then echo "lint-changed: no changed Go packages"; exit 0; fi; \ - echo "lint-changed:" $$packages; \ - $(BAZEL) build --config=nogo -- $$packages - -# Opt-in architecture experiment. Install gocyclo v0.6.0 first; -# report is advisory while check exercises the local baseline guard. -ci-complexity: - @./scripts/ci/complexity.sh report - -ci-complexity-diff: - @./scripts/ci/complexity.sh diff - -ci-complexity-check: - @./scripts/ci/complexity.sh check - # The generated half of the wire contract. The document is hand-written and is # the source of truth; this file is its output. API_GEN_FILE := internal/httpapi/apigen/types.gen.go @@ -302,8 +207,9 @@ api-gen: go generate -tags "$(BUILD_TAGS)" ./internal/httpapi/apigen # Two-part spec drift gate: regenerate and fail if regeneration CHANGED -# anything, then run the spec tests. CI runs the drift half as -# //scripts/repochecks:types_gen_drift_test. +# anything, then run the spec tests. Runs in the PR workflow's policy job +# (scripts/ci/pr-policy.sh), on every pull request, never only on +# push-to-main. # # The drift question is "do the checked-out types already match the checked-out # spec", so the comparison is before-vs-after regeneration rather than @@ -346,7 +252,7 @@ test-regression: # Override version: ./scripts/upgrade-smoke-test.sh v0.62.0 test-upgrade: build @echo "Running upgrade smoke tests..." - @CANDIDATE_BIN="$(BD_BUILD_OUTPUT)" ./scripts/upgrade-smoke-test.sh + @CANDIDATE_BIN=./bd ./scripts/upgrade-smoke-test.sh # Run cross-version smoke tests (last 30 tags → candidate). @@ -355,14 +261,14 @@ test-upgrade: build # All from v0.30.0: ./scripts/cross-version-smoke-test.sh --from v0.30.0 test-cross-version: build @echo "Running cross-version smoke tests..." - @CANDIDATE_BIN="$(BD_BUILD_OUTPUT)" ./scripts/cross-version-smoke-test.sh + @CANDIDATE_BIN=./bd ./scripts/cross-version-smoke-test.sh # Run the authenticated historical upgrade corpus with strict fidelity checks. # All qualified versions: ./scripts/migration-test/run.sh # Single version: ./scripts/migration-test/run.sh --version v0.49.6 test-migration: build @echo "Running migration test harness..." - @CANDIDATE_BIN="$(BD_BUILD_OUTPUT)" ./scripts/migration-test/run.sh + @CANDIDATE_BIN=./bd ./scripts/migration-test/run.sh # Regenerate the golden-JSON contract corpus (cmd/bd/protocol/testdata/corpus/). # Run after any deliberate bd --json wire change; review the diff, then commit. @@ -371,15 +277,6 @@ corpus-regen: @echo "Regenerating contract corpus..." go test -tags "$(BUILD_TAGS)" ./cmd/bd/protocol -run TestCorpusGolden -corpus.update -count=1 -# The tracked .githooks/* carry the managed section cmd/bd/hooks.go generates; -# TestTrackedManagedHookSectionsMatchGenerator holds them byte-equal. Run this -# after changing the generator or after bumping Version in cmd/bd/version.go -# (the section markers carry the version), then commit the regenerated hooks -# alongside it. -githooks-regen: - @echo "Regenerating managed sections in .githooks/*..." - BD_UPDATE_HOOKS_GOLDEN=1 go test -tags "$(BUILD_TAGS)" ./cmd/bd -run TestTrackedManagedHookSectionsMatchGenerator -count=1 - # Run performance benchmarks against Dolt storage backend # Requires CGO and Dolt; generates CPU profile files @@ -425,21 +322,18 @@ endif # The old rm-first shape added an ENOENT window on top. Same treatment for the # beads symlink. # -# On Git for Windows, `rm path/bd` can resolve and delete `path/bd.exe` when no -# literal extensionless entry exists. Replace bd.exe first, then enumerate real -# legacy aliases (including case variants and redirected install directories) -# so cleanup cannot remove the executable or weaken a failed install. Keep -# directories named bd; the Windows PATH above supplies MSYS find, not System32 find. -# find -H follows the install-directory argument; internal aliases are selected by -# ! -type d, without assuming how this find implementation classifies junctions. +# EXCEPTION — native Windows keeps the rm-first + cp shape: under Git for +# Windows' bash the staged tmp+rename leaves no bd.exe at the destination even +# though cp && mv exit 0 (caught by pr.yml's spaced-USERPROFILE install proof; +# root cause untraced). Restore Windows atomicity only with that proof green. install install-force: build @mkdir -p "$(INSTALL_DIR)" ifeq ($(OS),Windows_NT) - @cp "$(BD_BUILD_OUTPUT)" "$(INSTALL_DIR)/.bd.exe.install.tmp.$$$$" && mv -f "$(INSTALL_DIR)/.bd.exe.install.tmp.$$$$" "$(INSTALL_DIR)/bd.exe" - @find -H "$(INSTALL_DIR)" -mindepth 1 -maxdepth 1 -iname bd ! -type d -exec rm -f -- {} + + @rm -f "$(INSTALL_DIR)/bd" "$(INSTALL_DIR)/bd.exe" + @cp "$(BUILD_DIR)/bd.exe" "$(INSTALL_DIR)/bd.exe" @echo "Installed bd.exe to $(INSTALL_DIR)/bd.exe" else - @cp "$(BD_BUILD_OUTPUT)" "$(INSTALL_DIR)/.bd.install.tmp.$$$$" && mv -f "$(INSTALL_DIR)/.bd.install.tmp.$$$$" "$(INSTALL_DIR)/bd" + @cp "$(BUILD_DIR)/bd" "$(INSTALL_DIR)/.bd.install.tmp.$$$$" && mv -f "$(INSTALL_DIR)/.bd.install.tmp.$$$$" "$(INSTALL_DIR)/bd" @echo "Installed bd to $(INSTALL_DIR)/bd" @ln -sfn bd "$(INSTALL_DIR)/.beads.install.tmp.$$$$" && mv -f "$(INSTALL_DIR)/.beads.install.tmp.$$$$" "$(INSTALL_DIR)/beads" @echo "Created 'beads' alias -> bd" @@ -448,31 +342,21 @@ endif install: check-up-to-date -# Format all Go files. -# Through the go.mod-pinned gofmt, never a bare PATH one: a newer local Go -# formats differently from CI, so a bare gofmt -w here rewrites files into a -# form CI's gofmt then rejects. See scripts/ci/gofmt-bin.sh. +# Format all Go files fmt: @echo "Formatting Go files..." - @gofmt_bin="$$(./scripts/ci/gofmt-bin.sh)" && "$$gofmt_bin" -w . + @gofmt -w . @echo "Done" # Check that all Go files are properly formatted (for CI) fmt-check: @./scripts/ci/fmt-check.sh -# Docs checks: docsync and doc freshness as bazel.yml's test lane runs them, -# then the CLI flag check, which validates against bd built from the release in -# docs/cli-docs.pin (so it stays a script, like pr.yml's check-doc-flags job). +# Validate documentation references against actual CLI flags check-docs: - $(BAZEL_TEST) //test/docsync:docsync_test //scripts/repochecks:doc_freshness_test --config=ci - @./scripts/check-doc-flags.sh - -# The same docs checks with plain go test and a go-built bd. -check-docs-go: @echo "Building bd for docs checks..." - @CGO_ENABLED=0 go build -tags "$(BUILD_TAGS)" -ldflags="-X main.Build=$(GIT_BUILD)" -o "$(BUILD_DIR)/bd" ./cmd/bd - @./scripts/check-doc-flags.sh "$(BUILD_DIR)/bd" + @CGO_ENABLED=0 go build -tags "$(BUILD_TAGS)" -ldflags="-X main.Build=$(GIT_BUILD)" -o $(BUILD_DIR)/bd ./cmd/bd + @./scripts/check-doc-flags.sh ./bd @./scripts/check-doc-freshness.sh @go test -tags=gms_pure_go ./test/docsync @@ -504,21 +388,6 @@ diagrams-excalidraw: docs-dev: ./mint.sh dev -# Regenerate BUILD.bazel files with gazelle and refresh the MODULE.bazel -# use_repo list + MODULE.bazel.lock, then refresh the go_srcs filegroups that -# source-scanning tests declare as data (tools/bazel/go_srcs.py). Run after -# changing Go imports, go.mod, or packages. -bazel-sync: - $(BAZEL) run //:gazelle - python3 tools/bazel/go_srcs.py - $(BAZEL) mod tidy - -# Fail (printing a diff) when a go_srcs block that tools/bazel/go_srcs.py -# manages is stale, e.g. after gazelle added a package under a scanned tree. -# Needs no Bazel; scripts/bazel_policy_test.go runs the same check. -bazel-sync-check: - python3 tools/bazel/go_srcs.py --check - # Ensure -short is not used as an implicit CI tier boundary. check-testing-short: @./scripts/check-testing-short.sh @@ -543,19 +412,12 @@ help: @echo "Beads Makefile targets:" @echo " make build - Build the bd binary" @echo " make doctor-build - Diagnose build env (GOFLAGS/CGO/CC) for the ICU build trap" - @echo " make test - bazel test //... --config=ci (bazel.yml's test lane; BAZEL_FLAGS adds flags)" - @echo " make test-go - Plain go test via scripts/test.sh (inner loop; not what CI enforces)" - @echo " make check - testing.Short policy + nogo lint gate + make test" - @echo " make check-go - gofmt + testing.Short policy + make test-go (no Bazel)" + @echo " make test - Run all tests" @echo " make test-icu-path - Run opt-in ICU regex path tests (maintainer-only)" @echo " make test-full-cgo - Deprecated alias for make test-icu-path" @echo " make ci-pr-core - Run required PR core Go test wrapper" - @echo " make ci-pr-lint - Run the required lint gate: nogo (vet + golangci-lint's linters) under Bazel, native + windows/darwin" - @echo " make lint - Same as ci-pr-lint (make vet too)" - @echo " make lint-changed - nogo over the Bazel packages of changed Go files (LINT_CHANGED_SCOPE=staged|worktree)" - @echo " make ci-complexity - Report production cyclomatic complexity (advisory)" - @echo " make ci-complexity-diff - Compare complexity with COMPLEXITY_BASE_REF" - @echo " make ci-complexity-check - Check complexity against the local baseline" + @echo " make ci-pr-policy - Run required PR policy wrapper" + @echo " make ci-pr-lint - Run required PR formatting and lint wrapper" @echo " make ci-package-mcp - Run MCP Python package gate" @echo " make ci-package-npm - Run npm package gate" @echo " make test-regression - Run differential regression tests (baseline vs candidate)" @@ -568,12 +430,9 @@ help: @echo " make install-force - Install bd, skipping the origin/main update check" @echo " make fmt - Format all Go files with gofmt" @echo " make fmt-check - Check Go formatting (for CI)" - @echo " make check-docs - Bazel docsync + doc freshness tests, then the CLI flag check" - @echo " make check-docs-go - The same docs checks with plain go test" + @echo " make check-docs - Validate docs against CLI flags" @echo " make api-gen - Regenerate HTTP API types from the OpenAPI spec" @echo " make api-check - OpenAPI drift gate (regenerate, diff-or-fail, spec tests)" - @echo " make bazel-sync - Regenerate Bazel BUILD files and tidy MODULE.bazel (gazelle + mod tidy)" - @echo " make bazel-sync-check - Fail if generated go_srcs Bazel filegroups are stale" @echo " make clean - Remove build artifacts and profile files" @echo " make clean-test-tmp - Sweep orphaned cmd/bd test temp dirs from \$$TMPDIR" @echo " make help - Show this help message" diff --git a/README.md b/README.md index f7909900f..025cdc3e4 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,7 @@ bd setup factory # Factory.ai Droid - creates/updates AGENTS.md **Note:** Beads is a CLI tool you install once and use everywhere. You don't need to clone this repository into your project. -`bd init` creates or updates `AGENTS.md` by default so agents can discover the beads workflow, and also installs the project Claude Code, Codex, and Cursor integrations unless you pass `--skip-agents` or `--stealth`. Use `bd setup --list` to see supported integrations, including `bd setup codex`, `bd setup factory`, `bd setup claude`, `bd setup mux`, `bd setup cursor`, and more. See [Agent and IDE setup](docs/getting-started/ide-setup.md). +`bd init` creates or updates `AGENTS.md` by default so agents can discover the beads workflow, and also installs project Claude/Codex integrations unless you pass `--skip-agents` or `--stealth`. Use `bd setup --list` to see supported integrations, including `bd setup codex`, `bd setup factory`, `bd setup claude`, `bd setup mux`, `bd setup cursor`, and more. See [Agent and IDE setup](docs/getting-started/ide-setup.md). Manual copy-paste is only for unsupported agents, existing projects where you cannot rerun `bd init`/`bd setup`, or custom instruction files. In those cases, run `bd onboard` and paste the printed snippet into the file your agent reads. @@ -53,7 +53,7 @@ This project uses bd (beads) for issue tracking. - Run `bd prime` for workflow context and command guidance. - Use `bd ready`, `bd show `, `bd update --claim`, and `bd close `. -- Use `bd remember "insight"` for durable project facts; keep per-operator preferences in your harness's own memory. +- Use `bd remember "insight"` for persistent project memory; do not create MEMORY.md files. - Do not use markdown TODO lists for task tracking. ``` @@ -109,8 +109,8 @@ version: sync remote-backed databases with your current `bd`, back up with `bd export --all`, upgrade the binary, then run `bd info --whats-new`, `bd hooks install`, and `bd version`. If the upgrade crosses a schema migration on a remote-backed database, exactly one designated clone runs -`bd migrate schema` and `bd dolt push`; other clones install the new -binary and run `bd bootstrap`. See the full +`bd migrate` and `bd dolt push`; other clones install the new binary +and run `bd bootstrap`. See the full [upgrade guide](https://beads.gascity.com/getting-started/upgrading) or [docs/getting-started/installation.md](docs/getting-started/installation.md#updating-bd). @@ -175,153 +175,6 @@ are unaffected. the guard with a warning on stderr. Use this only if you know the forward migrations are additive and safe for your specific workload. -## 🔌 HTTP Backend (experimental) - -A workspace can also talk to a remote `bd serve` over HTTP instead of opening -a local Dolt database — useful for a thin client against a shared server, or -for embedding beads in another program as a library. - -```bash -# Point this workspace at a server (verifies the server first; writes nothing on failure) -bd connect https://bd.example.com --expect-project-id my-project - -# The everyday commands work the same way: create, show, list, update, close, -# dependencies (including `bd dep tree`), comments, ready/count queries, and -# batch apply all round-trip over the wire with the same semantics as a local -# database. So does `bd ready --claim`, except against an older server or while -# any issue holds an unsatisfied external ref: then it lists and claims in two -# steps rather than one transaction (ledger row L14, linked below). -bd create "Fix auth bug" -p 1 -t bug -bd ready --json -bd close bd-a1b2 "Fixed" - -# Loopback needs no TLS -bd connect http://127.0.0.1:8080 - -# Forget the server and restore this workspace's previous backend selection -bd connect --clear -``` - -HTTP mode is not a byte-for-byte mirror of local mode, though: some -operations are knowingly refused (the wire has no way to express them) or -degrade to a different-but-not-misreadable behavior (for example, a few -unbounded listing shapes lose single-query snapshot isolation and instead -walk the server page by page). Every one of these is a deliberate, tested -row, never a silent gap — see -[`engdocs/design/http-divergence-ledger.md`](engdocs/design/http-divergence-ledger.md) -for the full, generated list of what differs and why before depending on an -HTTP-backed workspace for a workflow you haven't checked against it. One -refusal to know up front: `bd close --claim-next` closes nothing over HTTP, -because the wire's batch close cannot carry the claim. Close without it, then -run `bd ready --claim`. - -[Cross-repo dependencies](docs/core-concepts/dependencies.md#cross-repo-dependencies) -keep their policy over HTTP. The client resolves each `external:` reference -against its own `external_projects` configuration. `bd dep tree` shows the -reference as a leaf, and while it is unsatisfied the issue stays out of -`bd ready` and `bd ready --claim`, and `bd close` refuses it without -`--force`. A server that advertises enforcing that policy itself is trusted -to do so instead. - -Three CLI-level differences are worth knowing up front: - -- IDs resolve exactly: an abbreviated id is not expanded (the wire has no - substring search), and a missing id is reported as not found after one - lookup. -- `bd update -s closed` does not record `closed_by_session` (from `--session` - or `CLAUDE_SESSION_ID`), because the wire's update cannot carry it; the - close itself happens as usual. `bd close` does record the session. `bd - reopen`'s history entry carries the server's own label. -- Contributor auto-routing (`routing.*` settings) is read from local - `config.yaml` / environment only, never from the server's settings: its - targets are local paths. - -`bd connect` writes two things on success: `.beads/metadata.json` gets -`"backend": "http"`, and a per-user, never-git-tracked sidecar -(`.beads/http_target.json`) records the server URL and pinned project id. It -never writes a credential to disk. A bearer credential, if the server -requires one, comes from `BEADS_HTTP_TOKEN=host[:port]=`, then -`BEADS_HTTP_TOKEN_COMMAND=host[:port]=` (a helper that prints a -token), then the credentials file, then no credential at all. Both variables -must name the server they are for (a pattern without a port matches that -host on any port); a bare value is refused, because it would be sent to -whatever server the workspace's `http_target.json` names. Connecting to a -non-loopback server over plain `http://` is refused by default (a bearer -credential would cross the network unencrypted) — pass `--allow-plaintext` to -override, or use `https://`. Switching a workspace that already selects a -different backend needs `--force`. - -Each read (a GET, or the POST batch get) gives its first attempt 15s, from -when the request is sent, to get the server's response headers -(`BEADS_HTTP_RESPONSE_HEADER_TIMEOUT`: a duration such as `30s`, a number of -seconds, or `off`). A read whose headers do not arrive in time, or whose -connection is reset, is retried once on another connection, and each retry -is logged to stderr with its count. The retry has no header timer, but it -runs on what is left of the 60s bound on the whole request, counted from -when the first attempt was sent: a slow but healthy read still finishes if -the retry's headers and body together fit in that remainder (about 45s after -a header timeout), and one that needs longer now fails at 60s. The header -timer never cuts off a body that is still streaming; the 60s bound still -does. The events:watch stream's connect gets neither the header timer nor -the retry. On Windows a reset connection is retried only when it surfaces as -an early EOF, because the check matches the Unix reset error (`ECONNRESET`), -which a Windows reset (`WSAECONNRESET`) is not. A write gets no -header timer (the server answers a write only after it commits, so a large -one can be slow and healthy) and is never retried; if the 60s bound expires -after it was sent but before its headers, it fails with an error naming the -server's host, because it may or may not have been applied. A batch apply of -more than 100 items (a large `bd create --graph` plan, say) gets 6 minutes -instead of 60s, because `bd serve` gives such a request its -`--large-apply-ceiling` (5 minutes by default) and finishes it even after the -client has given up. A server whose ceiling is raised past about 5m15s can -still outlast the client. A dial or TLS timeout stays a plain connection -error, since the server never saw the request, and a connect that fails says -so: it names the address that refused the connection (nothing listening) or -never answered (a filtered port or a down host, after the dialer's 30s), or -the host name that did not resolve. `off` turns off only the -header timer: a reset read is still retried, and a write's timeout is still -reported as above. - -Whether the server applies bd's external-dependency policy itself (the -`policy.external_dependencies` capability) is remembered for 5 minutes per -server URL and pinned project, in `beads/http-policy/` under the user cache -directory, so `bd ready` does not fetch the server's context before every -listing just to learn it. `BEADS_HTTP_POLICY_CACHE_TTL` sets that window (a -duration such as `1m`, a number of seconds, or `off`); a server restarted -with a different composition is seen once it passes. Claims and closes never -use the cached answer: they ask the server, since a stale one could skip the -client-side refusal of externally blocked work. A workspace connected without -a project pin is not cached. - -`bd search`, `bd show --current` and `bd recompute-blocked` work over HTTP -too, each in its own way. `bd search` asks the server's listing with its text (`q`, behind the `issues.list.search` capability) and -refuses before dialing against a server that does not advertise it. `bd list ---no-assignee`, `--title` and `--id` likewise ride the listing's -`no_assignee`, `title` and repeatable `id`, each behind its own capability -(`issues.list.noAssignee`, `issues.list.title`, `issues.list.ids`), and the -rest of `bd list`'s filters (`--updated-*`, `--closed-*`, `--defer-*`, -`--due-*`, `--priority*`, the text and external-ref matches, `--spec`, -`--label-pattern`/`--label-regex`, `--exclude-type`, `--no-parent`, -`--no-labels`, `--empty-description`, `--pinned`/`--no-pinned`, -`--deferred`, `--overdue`, `--mol-type`, `--wisp-type`) ride it behind one -capability, `issues.list.filters`. Time bounds are resolved on the client as -they are locally; `--overdue` compares against the server's clock. The -default `--parent` tree view does not carry those filters yet and refuses one -rather than drop it; `--flat` or `--json` applies them to the parent's direct -children (not its whole subtree). -`bd show --current` finds the actor's in-progress or hooked issue through the -server's boolean query. `bd recompute-blocked` is a no-op that exits 0: the -server maintains `is_blocked` on every write, and repairing a stale flag is -done in the server's own workspace (`--json` reports -`{"rows_corrected": 0, "maintained_by": "server"}`). - -An embedding program that links beads as a library rather than running -`cmd/bd` registers this backend itself through the public -[`backend/http`](backend/http) package (`bdhttp.Register`), and can open a -workspace with a credential scoped to one caller via -`beads.OpenBestAvailableWith` — see that package's doc comment for the -multi-tenant case. This surface is EXPERIMENTAL: pin an exact beads version. - ## 🌐 Community Tools See [docs/community-tools.md](docs/community-tools.md) for a curated list of community-built UIs, extensions, and integrations—including terminal interfaces, web UIs, editor extensions, and native apps. @@ -358,6 +211,5 @@ This is useful for: ## 📝 Documentation -* [Documentation site](https://beads.gascity.com/) | [Installing](docs/getting-started/installation.md) | [Sync Concepts](docs/core-concepts/sync-concepts.md) | [Agent Setup](docs/getting-started/ide-setup.md) | [Copilot CLI Setup](docs/integrations/copilot-cli.md) | [Copilot VS Code MCP](docs/integrations/github-copilot.md) | [Articles](ARTICLES.md) | [Sync Branch Mode](docs/reference/protected-branches.md) | [Troubleshooting](docs/reference/troubleshooting.md) | [FAQ](docs/reference/faq.md) -* Contributing: [CONTRIBUTING.md](CONTRIBUTING.md) (Bazel builds and tests beads: install [Bazelisk](https://github.com/bazelbuild/bazelisk), then `make test`) | [Roadmap](ROADMAP.md) | [Contributor agent instructions](AGENTS.md) +* [Documentation site](https://beads.gascity.com/) | [Installing](docs/getting-started/installation.md) | [Sync Concepts](docs/core-concepts/sync-concepts.md) | [Agent Workflow](AGENT_INSTRUCTIONS.md) | [Copilot CLI Setup](docs/integrations/copilot-cli.md) | [Copilot VS Code MCP](docs/integrations/github-copilot.md) | [Articles](ARTICLES.md) | [Sync Branch Mode](docs/reference/protected-branches.md) | [Troubleshooting](docs/reference/troubleshooting.md) | [FAQ](docs/reference/faq.md) * [![Ask DeepWiki](https://deepwiki.com/badge.svg)](https://deepwiki.com/gastownhall/beads) diff --git a/RELEASING.md b/RELEASING.md index 50acb6497..894abbb20 100644 --- a/RELEASING.md +++ b/RELEASING.md @@ -94,19 +94,15 @@ release-critical gates do not all run on every PR, so "tag the tip of main" means tagging a SHA nobody has fully tested. A burned tag is never reused (the v1.1.1 and v1.2.0 precedents), so that gamble is expensive. -**Cut the branch from a SHA that is green on `Bazel`, `Main` and -`Nightly`.** The `Bazel` workflow (every Linux test lane, on rbe-west) and -the `Main` workflow (the macOS and Windows jobs) run on push to `main`, and -the `Nightly` workflow (the full Bazel suite with no cached test results) -runs on a 2am UTC schedule, so the newest SHA with all three is usually a -few hours behind the tip: +**Cut the branch from a SHA that is green on both `Main` and +`Nightly Full Tests`.** The `Main` workflow runs on push to `main` and the +`Nightly Full Tests` workflow runs on a 2am UTC schedule, so the newest SHA +with both is usually a few hours behind the tip: ```bash -gh run list --workflow Bazel --branch main --event push --status success --limit 5 \ - --json headSha,conclusion,createdAt gh run list --workflow Main --branch main --status success --limit 5 \ --json headSha,conclusion,createdAt -gh run list --workflow Nightly --status success --limit 3 \ +gh run list --workflow "Nightly Full Tests" --status success --limit 3 \ --json headSha,conclusion,createdAt git fetch origin @@ -140,17 +136,16 @@ git log --oneline main..release/1.3.0 Before starting a release: -- [ ] `release/x.y.z` branch cut from a SHA green on `Bazel`, `Main` **and** - `Nightly` (see [Release Branches](#release-branches)) -- [ ] All tests passing (`make test`, the Bazel test lane) +- [ ] `release/x.y.z` branch cut from a SHA green on `Main` **and** + `Nightly Full Tests` (see [Release Branches](#release-branches)) +- [ ] All tests passing (`go test ./...`) - [ ] npm package tests passing (`cd npm-package && npm run test:all`) - [ ] **Upgrade smoke tests pass** (`make test-upgrade`) — see [Release Stability Gate](engdocs/RELEASE-STABILITY-GATE.md) -- [ ] **Regression tests pass** - (`bazel test //tests/regression:regression_test --config=doltserver-cmd`) +- [ ] **Regression tests pass** (`make test-regression`) - [ ] **Every release target cross-compiles** — see - [Cross-compile before tagging](#cross-compile-before-tagging). PR CI - builds them only with cgo off, and a target that fails at tag time - burns the tag. + [Cross-compile before tagging](#cross-compile-before-tagging). PR CI does + not build them ([#5662](https://github.com/gastownhall/beads/issues/5662)), + and a target that fails at tag time burns the tag. - [ ] **CHANGELOG.md updated with release notes** (see format below) - [ ] **CHANGELOG rollup checked** — nothing left under `[Unreleased]` that belongs in this release, and nothing filed under a *previous* release's @@ -168,15 +163,12 @@ Before starting a release: ### Cross-compile before tagging -`.goreleaser.yml` builds darwin, linux, windows **and freebsd**; the v1.2.0 -tag burned on a freebsd compilation failure no pre-tag gate caught -([#5661](https://github.com/gastownhall/beads/pull/5661), -[#5662](https://github.com/gastownhall/beads/issues/5662)). PR CI now builds -the whole tree for every target in `scripts/ci/release-targets.txt` with Bazel -(`scripts/ci/bazel-release-cross-compile.sh`, in bazel.yml's pure-Go lane), -but with cgo off, while linux/amd64, linux/arm64, windows/amd64 and the darwin -pair ship with `CGO_ENABLED=1`. Build the real release configuration by hand -before tagging: +`.goreleaser.yml` builds darwin, linux, windows **and freebsd**, but PR CI +builds none of the cross targets — the v1.2.0 tag burned on a freebsd +compilation failure that no pre-tag gate could have caught +([#5661](https://github.com/gastownhall/beads/pull/5661) fixed the break, +[#5662](https://github.com/gastownhall/beads/issues/5662) tracks the CI gap). +Until that gap is closed, build them by hand before tagging: ```bash goreleaser build --snapshot --clean diff --git a/ROADMAP.md b/ROADMAP.md deleted file mode 100644 index 006b5602e..000000000 --- a/ROADMAP.md +++ /dev/null @@ -1,71 +0,0 @@ -# Beads Roadmap - -Last updated: 2026-10-05 · Owner: [@julianknutsen](https://github.com/julianknutsen) - -This page lists the areas the maintainers are prioritizing. It reflects -current thinking, not commitments or dates: priorities change as we learn, -and the live state of each item is its tracking issue on GitHub. - -## How this roadmap is used - -- **Priority areas get expedited review.** Issues and PRs that advance a - priority area below are triaged and reviewed first. -- **Propose work with an issue when it warrants discussion.** For work - inside or outside a priority area that needs context or a design decision, - open an issue with the feature form, before or alongside your PR; small, - self-explanatory changes can go straight to a PR. See - [CONTRIBUTING.md](CONTRIBUTING.md#issues-and-pull-requests). -- **Scope.** Proposals must fit the product boundary in - [engdocs/PROJECT_CHARTER.md](engdocs/PROJECT_CHARTER.md). -- **Updates.** The owner rewrites this page at each minor release; the date - above says when it was last reviewed. - -## Priority areas for 1.4 - -### Versioned beads and history - -- **Goal:** every change to a bead is recorded as a version. Users can read - a bead's history and past versions, and writers can use `expectedRevision` - compare-and-swap to avoid lost updates. Everything ships behind a flag, off - by default, until the conformance suite and a production-corpus soak say - otherwise. -- **This period:** all six phases, enabled from the CLI by the end of the - period: conformance suite - ([#6133](https://github.com/gastownhall/beads/issues/6133)), additive - schema ([#6134](https://github.com/gastownhall/beads/issues/6134)), - dual-write history behind a flag - ([#6135](https://github.com/gastownhall/beads/issues/6135)), versioned - reads and CAS ([#6136](https://github.com/gastownhall/beads/issues/6136)), - production-corpus validation - ([#6137](https://github.com/gastownhall/beads/issues/6137)), and CLI - surfacing and enablement - ([#6138](https://github.com/gastownhall/beads/issues/6138)). -- **Tracking issue:** [#6132](https://github.com/gastownhall/beads/issues/6132); - design discussion stays on - [#5898](https://github.com/gastownhall/beads/issues/5898). - -### Beads Graph Preview 2 - -- **Goal:** ship Beads Graph Preview 2, the next public checkpoint for the - generic Bead-and-Link graph model: a coherent daily-use CLI journey in - graph mode that behaves like ordinary `bd`, backed by a complete, - reviewable CLI specification. -- **This period:** the reviewed graph CLI specification, a complete - daily-use Issue journey proven through the installed CLI on embedded and - shared-server Dolt, and editing around that Issue (labels, notes, Memory - and Link edits). -- **Tracking issue:** [#7170](https://github.com/gastownhall/beads/issues/7170) - -### Contributor onboarding - -- **Goal:** a new contributor, human or agent, finds the rules for the code - they are changing next to that code, understands its intent and - invariants, and lands a well-explained PR, backed by an issue when the - change warrants one. -- **This period:** contributor-only agent instructions and documented-issue - intake ([#7223](https://github.com/gastownhall/beads/pull/7223)); - per-package `AGENTS.md` files for the storage packages, naming their - guard tests and lint rules; a package-doc linter and package docs for the - packages that have none; and a curated set of `help wanted` starter - issues. -- **Tracking issue:** [#7228](https://github.com/gastownhall/beads/issues/7228) diff --git a/VERSIONED-BEADS-WRITE-PATHS.md b/VERSIONED-BEADS-WRITE-PATHS.md deleted file mode 100644 index bed384ade..000000000 --- a/VERSIONED-BEADS-WRITE-PATHS.md +++ /dev/null @@ -1,156 +0,0 @@ -# Versioned Beads: issue-plane write paths and their Phase 2 disposition - -This document has two parts. **Dispositions at this head** is the current inventory: every row whose disposition changed after the completeness fix a14f48b17 (merged as 5098ec85f), the seam's call sites at this head, and notes that are true now. **As found at 321a79278** is the review artifact this PR started from — the table donnabox's item 5 on #6358 asked for, derived before the completeness fix — kept verbatim under a dated heading because it is the record of what the review found. Wherever the two parts differ, the current part wins; a reader citing a row number should cite it from the current part. - -## Dispositions at this head - -Status after a14f48b17 (merged as 5098ec85f on `deploy/be-764ey-gate`): the table below was derived from the tree at 321a79278, before the completeness fix, and is kept as the record of what the review found. Since that commit the following rows changed disposition: - -- Rows 17-23, 25 and 27 (close, reopen, claim, unclaim/release, lease reclaim, defer wake, label add/remove, promote, persistence move) are now **VERSIONED**: each mints once, after its row write and event, gated on a row actually changing. -- Rows 14-16 (legacy `AddDependency`/`RemoveDependency` verbs, the uow dependency repository, batch-apply structural adds) are now **VERSIONED** through the lowest-level helpers `addDependencyInTx`/`removeDependencyInTx` (mint gated on the row insert/delete); the publicops editor's own mint call was removed so no leg double-mints. -- Ordering: `ExecuteUpdate` mints once, after field, label, parent and persistence patches; batch create mints per issue after its creation-time edges are persisted, so the first version carries the outgoing edge set. -- `version_completeness_test.go` (46 must-mint entry points, exemption table with reasons, never-mints inverse guard) and `embeddeddolt/version_completeness_behaviour_test.go` pin the set at runtime. - -The seam is `issueops.RecordVersionInTx` in internal/storage/issueops/version_history.go (line 87 at 321a79278; cite the function rather than a line at later heads, since comment edits move it), which sits beside the events-journal seam `issueops.RecordEventInTx` (journal.go:309). At 321a79278 it was reached from exactly six call sites: create.go:196, update.go:547, dependency_editor.go:181 and :272, and domain/db/issue.go:87/:102 (`Insert`) and :339 (`Update`); after a14f48b17 it is reached from 27 (18 in issueops, 9 in domain/db), listed in the section "Seam call sites after the completeness fix" below. Dispositions: **VERSIONED** = reaches `RecordVersionInTx` and mints an `issue_versions` row plus the `current_revision` bump; **NO-OP** = discarded before the seam (`DiscardNoopIssueUpdates`, the dependency editor's `eventWritten` gate, create's early returns, or the wisp exclusion); **OUT OF CONTRACT** = mutates an issue-plane row (issues, wisps, dependencies, labels, comments and wisp_* twins) without ever calling the seam. Paths are under `internal/storage/` unless they start with `cmd/`, `internal/compact/` or `backend/`. Checkout: /var/tmp/mayor-beads-phase2 @ deploy/be-764ey-gate; all line numbers are against commit 321a79278, the HEAD when this inventory was read (see the last note on drift). - -### Rows whose disposition changed after a14f48b17 - -Row numbers are the historical table's. Line numbers in this table are against the Go tree at this head (unchanged since 5098ec85f). - -| # | Path | Version seam at this head | Disposition | Mechanism | -|---|---|---|---|---| -| 13 | any of rows 1-8 whose target row is a wisp **or a promoted no-history bead** | no | NO-OP (a flagged issues-table create is stamped, not versioned) | The table that holds the row decides its plane, not its flags. A row in the wisps table mints and stamps nothing, whatever its flags say (FR-8). An update-shaped call on a flagged wisp returns on `IsWisp`, which is `Ephemeral \|\| NoHistory` (helpers.go:36-41). Every other wisps-table row is excluded by the seam's read of `participation_generation` from the issues table, which finds no row and returns: a flagged wisp's create-shaped call, and an unflagged row (bd mol squash's in-place flag clear, import's `wisp_plane` marker), which passes `IsWisp`. An issues-table row carrying `NoHistory` (a promoted no-history bead, bd-r9uce) is never versioned while it says so, because no-history means no history: an update-shaped call returns on `IsWisp`. A create-shaped call on such a row, with the flag on, still stamps `participation_generation` and mints nothing, so the row is not legacy once `bd update --persistent` repairs the flag in place. | -| 14 | dependencies.go:`addDependencyInTx` via `AddDependencyInTx` from the store verbs, `ApplyParentPatch`, `applyBatchRun.applyDepAdd` | yes | VERSIONED | mint moved into `addDependencyInTx` (`mintDependencyVersion`, dependencies.go:393), gated on the row insert rather than on `eventWritten`; the publicops editor's own mint call was removed so no leg double-mints | -| 15 | domain/db/dependency.go:`dependencySQLRepositoryImpl.Insert` (uow `AddDependencies`, create-time edges) | yes | VERSIONED | seam at dependency.go:228 and :240 after the insert | -| 16 | dependencies.go:`removeDependencyInTx` via `RemoveDependencyInTx` and domain/db/dependency.go:`Delete` | yes | VERSIONED | gated on the row delete; dependency.go:409 on the uow leg | -| 17 | close.go:`closeIssueInTx` (all close variants; `ExecuteCloseBatch` loops it; domain/db `Close`/`CloseChecked`) | yes | VERSIONED | close.go:394, after the row write and event, gated on the row changing | -| 18 | reopen.go:`reopenIssueInTx` | yes | VERSIONED | reopen.go:126 | -| 19 | claim.go:`claimIssueInTx` and domain/db/issue.go:`Claim` | yes | VERSIONED | claim.go:239 and issue.go:551; `ExecuteUpdate`'s Claim flag passes `mintVersion=false` and mints once at its own end | -| 20 | unclaim.go:`finishUnclaimInTx` (unclaim, release) | yes | VERSIONED | unclaim.go:137 | -| 21 | lease.go:`ReclaimExpiredLeasesInTx` | yes | VERSIONED | lease.go:786 | -| 22 | wake_defers.go:`wakeExpiredDefersInTable` | yes | VERSIONED | wake_defers.go:143 | -| 23 | labels.go:`addLabelInTx` / `removeLabelInTx` and domain/db/label.go:`Insert` / `Delete` | yes | VERSIONED | labels.go:190 and :239 gated on `RowsAffected() > 0`; label.go:91 and :130; `applyLabelPatch` (aggregate.go:360) mints only when called outside `ExecuteUpdate` | -| 25 | promote.go:`PromoteFromEphemeralInTx` | yes | VERSIONED | promote.go:141, after the label, dependency and comment copies | -| 27 | persistence.go:`moveIssuePersistenceInTx` | yes | VERSIONED | persistence.go:76 and :153; `ExecuteUpdate` passes `mintVersion=false` and mints once at its own end | -| 34 | batch_apply.go:`applyBatchRun` | yes, per item | VERSIONED | its create/update/close items reach rows 1/2, 5/6 and 17; its dep-add items reach `addDependencyInTx`, which now mints (row 14) | - -Rows not listed keep the disposition shown in the historical table: rows 1-8 VERSIONED; rows 9-12 NO-OP gates inside them; rows 24, 26, 28-33 and 35-39 OUT OF CONTRACT; row 40 writes no issue-plane row; row 41 is Phase 3. - -### Counts at this head - -Of the 34 live issue-plane write paths (rows 1-8 and 14-39), **21 are VERSIONED** (rows 1-8, 14-23, 25, 27, 34) and **13 are OUT OF CONTRACT** (rows 24, 26, 28-33, 35-39); rows 9-13 are the no-op gates inside the versioned ones. The out-of-contract set is what the design puts out of contract: comments (not part of the snapshot), is_blocked flips, delete, rename and demote (Phase 3's deleted-Versioned-Bead guarantee), compaction bookkeeping and restore, migrations, merge-settle, doctor fixes and raw SQL. - -### Still deferred - -Still true and deliberately out of contract or deferred: an unchanged re-import of an existing row mints a version (create has no field-level no-op gate — and the journal seam sits in the same position: `RecordEventInTx(EventCreate)` at create.go:207 runs on every accepted upsert, only the events-table write behind the `isNew` gate at create.go:171 is skipped, so a no-op gate belongs in front of both seams, not the version seam alone); the uow leg mints per repository write, so a domain create with N labels and M edges yields 1+N+M versions where the direct legs yield one (each a distinct accepted state; follow-up); demote-to-wisp and delete/rename strand version rows because `issue_versions` has no FK to `issues` (Phase 3's deleted-Versioned-Bead guarantee); compaction bookkeeping, restore, migrations, merge-settle, doctor fixes and `bd sql` remain out of contract by design. The flag-off contract is behaviourally a no-op at the seam (`RecordVersionInTx` returns before any query) but not literally zero added work: labels.go's `RowsAffected` gate and the per-transaction `sync.Map` entry run with the flag off; the differential harness that would prove the stronger claim is not in this PR. The items that become live the first time the flag is on — the uow leg's 1+N+M, the re-import no-op gate on both seams, duplicate-key metadata, the single-writer allocator, and the `attribution_status` vocabulary being enforced only in Go — are tracked together in gastownhall/beads#6379. - -### Seam call sites at this head - -Verified with `grep -rn 'RecordVersionInTx(' internal/ --include='*.go'` over non-test files at d484b809d (the Go tree is unchanged between the merge of a14f48b17 as 5098ec85f and this note): **27 call sites** reach the seam, 18 on the direct legs in `issueops/` and 9 on the uow leg in `domain/db/`. The figure of 29 quoted in the PR thread was an over-count; this table is the correction. Line numbers are against that tree. The 46 "must-mint entry points" pinned by `version_completeness_test.go` are a different count by design: they are the public entry functions whose call graph must reach the seam, not the seam's own call sites. - -Direct legs, `internal/storage/issueops/` (18): - -| File:line | Function | Mutation | -|---|---|---| -| aggregate.go:360 | `applyLabelPatch` | label patch (mints only when used outside `ExecuteUpdate`, which passes `mintVersion=false`) | -| aggregate.go:423 | `applyParentPatch` | parent patch (same rule) | -| claim.go:239 | `claimIssueInTx` | claim | -| close.go:394 | `closeIssueInTx` | close, all variants; `ExecuteCloseBatch` loops it | -| create.go:216 | `CreateIssueInTxWithResult` | singular create and import upsert | -| create.go:384 | `CreateIssuesInTxWithContext` | batch create, per issue, after its creation-time edges (`DeferVersionMint`) | -| dependencies.go:393 | `mintDependencyVersion` | from `addDependencyInTx` / `removeDependencyInTx`: legacy verbs, the publicops editor, batch-apply | -| execution.go:269 | `ExecuteUpdate` | once, after field, label, parent and persistence patches | -| labels.go:190 | `addLabelInTx` | label add | -| labels.go:239 | `removeLabelInTx` | label remove | -| lease.go:786 | `ReclaimExpiredLeasesInTx` | lease reclaim | -| persistence.go:76, :153 | `moveIssuePersistenceInTx` | persistence move (two sites in the one function) | -| promote.go:141 | `PromoteFromEphemeralInTx` | promote from ephemeral | -| reopen.go:126 | `reopenIssueInTx` | reopen | -| unclaim.go:137 | `finishUnclaimInTx` | unclaim / release | -| update.go:555 | `updateIssueInTx` | update | -| wake_defers.go:143 | `wakeExpiredDefersInTable` | defer wake | - -uow leg, `internal/storage/domain/db/` (9): - -| File:line | Method | Mutation | -|---|---|---| -| issue.go:87, :102 | issue repository `Insert` | create (two sites in the one method) | -| issue.go:339 | issue repository `Update` | update | -| issue.go:551 | issue repository `Claim` | claim | -| label.go:91 | label repository `Insert` | label add | -| label.go:130 | label repository `Delete` | label remove | -| dependency.go:228, :240 | dependency repository `Insert` | edge add (two sites in the one method) | -| dependency.go:409 | dependency repository `Delete` | edge remove | - -### Notes at this head - -- **What actually versions:** every accepted issue-plane mutation on every leg — create (singular, batch, import), update (and everything that reaches `updateIssueInTx`: metadata merge/delete/CAS, deleted-reference rewrite, compaction's content rewrite), dependency add and remove (editor, legacy verbs, uow repository, batch-apply), close, reopen, claim, unclaim/release, lease reclaim, defer wake, label add and remove, promote, persistence move. `version_completeness_test.go` pins 46 must-mint entry points with an exemption table (a reason per row) and an inverse never-mints guard; `embeddeddolt/version_completeness_behaviour_test.go` pins the runtime deltas. -- **Cross-leg symmetry on dependencies:** resolved. The dolt and embeddeddolt editors, the legacy store verbs and the uow repository all reach the seam through `addDependencyInTx` / `removeDependencyInTx`, gated on the row insert or delete. -- **Ordering inside a versioned call:** the mint is the last durable-state write. `ExecuteUpdate` mints once after its field, label, parent and persistence patches; batch create mints per issue after `PersistDependenciesWithOptionsResult`, so the first version carries the creation-time edge set. -- **Count vs design:** 21 VERSIONED, 13 OUT OF CONTRACT, 5 no-op gates (see Counts at this head). The design's "eighteen" is still not reconcilable item by item from this checkout. -- **Import upsert:** an unchanged re-import of an existing row still mints, and it also journals — `RecordEventInTx(EventCreate)` at create.go:207 runs on every accepted upsert — so the no-op gate belongs in front of both seams (gastownhall/beads#6379, item 2). -- **No FK from issue_versions to issues** (0067), so delete, rename and demote strand version rows under ids that no longer exist in `issues` — Phase 3 (gastownhall/beads#6379, item 6). -- **Non-canonicalizable snapshots fail the mutation** by policy: duplicate keys in an issue's `metadata` (a `json.RawMessage`) make `canonicalDurableState` return an error and abort the transaction with the flag on (gastownhall/beads#6379, item 3). -- **Single writer means one writer at a time per store,** not merely one clone: `MAX(revision)+1` is not a safe allocator for two concurrent transactions in one store either, and no contract case runs concurrent writers (gastownhall/beads#6379, item 4). -- **Exemption tables that name the seam:** journal_completeness_test.go `beadDMLExemptions` and row_lock_guard_test.go `funcNameExemptions` both list `RecordVersionInTx` as bookkeeping for an already-journaled mutation; unchanged. - -## As found at 321a79278 (historical; superseded above wherever the two parts differ) - -The table and notes below are the inventory as derived from the tree at 321a79278, the HEAD when it was read, and all line numbers in this part are against that commit. It is kept verbatim as the record of what the review found. Rows 13-23, 25, 27 and 34 are superseded by the current table above; the notes below that contradict the notes above are superseded too. - -| # | Path (file:function) | Writes | Events seam | Version seam | Disposition | Why | -|---|---|---|---|---|---|---| -| 1 | issueops/create.go:`CreateIssueInTxWithResult` (`CreateIssueInTx`; execution.go:`ExecuteCreate`; dolt/issues.go:`createIssue`; dolt/transaction.go and embeddeddolt/transaction.go `CreateIssue`) | issues or wisps row via `InsertIssueIfNew` (ON DUPLICATE KEY UPDATE upsert) + labels + comments | yes | yes | VERSIONED | both seams run once after labels/comments persist (create.go:193-197); an existing-row upsert (`isNew=false`) still mints — create has no field-level no-op gate | -| 2 | domain/db/issue.go:`issueSQLRepositoryImpl.Insert` (`InsertBatch` loops it; domain `IssueUseCase.CreateIssue`/`CreateWisp` from uow/issue_operations.go, uow/batch_creator.go, uow/batch_applier.go) | issues or wisps via `issueops.InsertIssueStrictInTx` | yes | yes | VERSIONED | uow leg; `RecordVersionInTx` directly after `RecordEventInTx` in both branches (issue.go:84-87, 99-102) | -| 3 | issueops/create.go:`CreateIssuesInTxWithContext` / `CreateIssuesInTxWithResult` (create_batch.go:`ExecuteCreateBatch`; store `CreateIssuesWithFullOptions` on both direct legs) | row 1 per issue, then `PersistDependenciesWithOptionsResult` | yes (per issue, plus dep events) | yes (per issue) | VERSIONED | per-issue loop (create.go:309); creation-time edges are inserted AFTER each issue's version row, so they are neither in that row's `durable_state` nor versioned themselves | -| 4 | Import: uow/importer.go:`importer.ImportBatch`; embeddeddolt/store.go:`ImportJSONLData`; embeddeddolt/transaction.go:`CreateIssueImport`; cmd/bd/import_shared.go via `store.CreateIssuesWithFullOptions` | as row 3 | yes | yes | VERSIONED | import is not a separate write path — it funnels into rows 1/3; at 321a79278 rows get `attribution_status` supplied/undetermined and `imported` is only reserved (version_history.go:57-61); 56b2199d4, which landed mid-session, collapses the values to claimed/unknown with no `imported` at all | -| 5 | issueops/update.go:`updateIssueInTx` (`UpdateIssueInTx`, `UpdateIssueWithoutEventInTx`; execution.go:`ExecuteUpdate`; metadata.go:`writeMergedMetadataInTx` from `MergeMetadataInTx`/`DeleteMetadataInTx`/metadata_cas.go:`CompareAndSetMetadataKeyInTx`; delete_role.go:`RewriteDeletedReferencesInTx`; compaction's content rewrite via `store.UpdateIssue`) | UPDATE issues or wisps SET fields, row_lock | yes | yes | VERSIONED | seams at update.go:544-547, after `DiscardNoopIssueUpdates` (:391) and NOT inside `if recordEvent` — `UpdateIssueWithoutEventInTx` still versions | -| 6 | domain/db/issue.go:`issueSQLRepositoryImpl.Update` (domain `IssueUseCase.ApplyUpdate` from uow/issue_operations.go, uow/batch_applier.go) | UPDATE issues or wisps | yes | yes | VERSIONED | uow leg; same no-op gate (issue.go:212-219), seams at :336-339 | -| 7 | issueops/dependency_editor.go:`addDependencyEdgeInTx` (`ExecuteAddDependencies` from dolt/dependency_editor.go, embeddeddolt/dependency_editor.go) | INSERT dependencies or wisp_dependencies via `AddDependencyInTx` (+ is_blocked marks) | yes (`RecordDepEventInTx` inside `addDependencyInTx`) | yes, iff `eventWritten` | VERSIONED | versions the referencing (source) issue only when a row was recorded (dependency_editor.go:180-183) | -| 8 | issueops/dependency_editor.go:`ExecuteRemoveDependency` (from dolt/dependency_editor.go, embeddeddolt/dependency_editor.go) | DELETE dependencies or wisp_dependencies via `RemoveDependencyInTx` | yes | yes, iff `eventWritten` | VERSIONED | returns `Removed:false` before the seam when no row matched (:263-274) | -| 9 | update.go:`updateIssueInTx` / domain/db/issue.go:`Update` when `DiscardNoopIssueUpdates` leaves no field | nothing | no | no | NO-OP | returns before the UPDATE (update.go:391-397; issue.go:212-219) — the FR-2 contract case | -| 10 | dependency_editor.go:`addDependencyEdgeInTx` on a same-type re-add | UPDATE dependencies SET metadata (dependencies.go:274) | yes (dependencies.go:281) | no | NO-OP | `addDependencyInTx` returns `eventWritten=false` on the existing-edge branch, so the seam is skipped although metadata is rewritten and journaled | -| 11 | dependency_editor.go:`ExecuteRemoveDependency` on an absent edge | nothing | no | no | NO-OP | `removeDependencyInTx` reports `eventWritten=false`; editor returns at :267 | -| 12 | create.go:`CreateIssueInTxWithResult` on collision-skip, `ConflictSkip` or `RejectStaleUpserts` hit | nothing | no | no | NO-OP | early returns at create.go:126-128, 135-143 precede both seams | -| 13 | any of rows 1-8 whose target row is a wisp | as per row | yes | no | NO-OP | `RecordVersionInTx` returns nil on `IsWisp` (version_history.go:96, FR-8); wisps.current_revision stays at its default | -| 14 | dependencies.go:`addDependencyInTx` via `AddDependencyInTx` from the store verbs — dolt/dependencies.go:`addDependencyWithOptions`, dolt/transaction.go and embeddeddolt/transaction.go `AddDependencyWithOptions`, embeddeddolt/dependencies.go, dolt/wisps.go:`addWispDependency`, batch_apply.go:`applyBatchRun.applyDepAdd`, aggregate.go:`ApplyParentPatch` | INSERT dependencies or wisp_dependencies | yes | no | OUT OF CONTRACT | same DML as row 7 but no caller invokes `RecordVersionInTx`; only the publicops editor role versions | -| 15 | domain/db/dependency.go:`dependencySQLRepositoryImpl.Insert` (domain `DependencyUseCase.AddDependencies` from uow/dependency_editor.go and uow/batch_applier.go; `IssueUseCase` create-time edges, domain/issue.go:980,1041,1053) | INSERT dependencies or wisp_dependencies | yes | no | OUT OF CONTRACT | the uow leg's dependency add never reaches the seam — only `Insert`/`Update` in domain/db/issue.go do | -| 16 | dependencies.go:`removeDependencyInTx` via `RemoveDependencyInTx` from store verbs (dolt/dependencies.go and both transactions `RemoveDependencyWithOptions`, `ApplyParentPatch`) and domain/db/dependency.go:`Delete` (uow `RemoveDependencyBySource`) | DELETE dependencies or wisp_dependencies | yes (when EmitEvent) | no | OUT OF CONTRACT | as row 14 | -| 17 | close.go:`closeIssueInTx` (`CloseIssueInTx`, `CloseIssueWithoutEventInTx`, `CloseIssueCheckedInTx`; execution.go:`ExecuteClose`; close_batch.go:`ExecuteCloseBatch` loops `ExecuteClose`; domain/db `Close`/`CloseChecked`) | UPDATE issues or wisps SET status, closed_at, close_reason, closed_by_session, row_lock (close.go:340) | yes (EventClose, close.go:387, not gated by `recordEvent`) | no | OUT OF CONTRACT | direct UPDATE; never calls `RecordVersionInTx`, so a close is invisible to issue_versions | -| 18 | reopen.go:`reopenIssueInTx` (`ReopenIssueInTx`; execution.go:`ExecuteReopen`; domain/db `Reopen`) | UPDATE status/closed_at (reopen.go:64) | yes | no | OUT OF CONTRACT | as row 17 | -| 19 | claim.go:`ClaimIssueInTx` (public_claim.go:`ExecuteClaim`; `ClaimReadyIssueInTx` from claim_next.go:`ExecuteClaimNext`; `ExecuteUpdate`'s Claim flag) and domain/db/issue.go:`Claim` (uow) | UPDATE assignee/status/started_at (claim.go:115,124; issue.go:478,488) | yes | no | OUT OF CONTRACT | a claim-only request mints nothing; `ExecuteUpdate` versions only if other fields also change (execution.go: `ClaimIssueInTx` then `if len(updates) > 0`) | -| 20 | unclaim.go:`UnclaimIssueInTx` / `UnclaimIssueIfAssigneeInTx` via `finishUnclaimInTx`; release_role.go:`ReleaseIssueInTx` wraps them (domain/db `UnclaimIssue`/`ReleaseIssue`) | UPDATE assignee/status (unclaim.go:80,197) | yes | no | OUT OF CONTRACT | direct UPDATE | -| 21 | lease.go:`ReclaimExpiredLeasesInTx` (dolt/issues.go:473, embeddeddolt/issues.go:134, domain/db `ReclaimExpiredLeases`) | UPDATE issues assignee/status revert (lease.go:758) + DELETE leases | yes | no | OUT OF CONTRACT | `HeartbeatIssueInTx` writes only `leases` and is not an issue-record write | -| 22 | wake_defers.go:`wakeExpiredDefersInTable` (`WakeExpiredDefersInTx`; dolt/queries.go:63, embeddeddolt/queries.go:33, domain/db `WakeExpiredDefers`) | UPDATE status (wake_defers.go:116) | yes (actor `DeferWakeActor`) | no | OUT OF CONTRACT | scheduled status flip outside the seam | -| 23 | labels.go:`AddLabelInTx` / `RemoveLabelInTx` (dolt/labels.go, embeddeddolt/labels.go, both transactions, aggregate.go:`ApplyLabelPatch` from `ExecuteUpdate`) and domain/db/label.go:`Insert` / `Delete` (uow `LabelUseCase`) | INSERT IGNORE / DELETE labels or wisp_labels | yes (EventUpdate) | no | OUT OF CONTRACT | labels ARE in `durable_state` (get_issue.go:59 hydrates them) but a label change mints nothing; in `ExecuteUpdate` the label patch runs after `UpdateIssueInTx`'s mint, so a same-call field+label edit records the old label set | -| 24 | comments.go:`addIssueCommentInTx` (`AddIssueCommentInTx`, `ImportIssueCommentInTx`, commenter.go:`ExecuteAddComment`); dolt/transaction.go:`AddComment` / `ImportIssueComment` (`InsertDerivedEventReturningID` / `InsertDerivedComment`); domain/db/comment.go:`InsertRecord` (uow `CommentUseCase`); create.go:`PersistComments` | INSERT comments or wisp_comments (dolt tx `AddComment` writes an events-table comment row) | yes (`RecordCommentEventInTx`) | no | OUT OF CONTRACT | comments are not in `durable_state` either (`GetIssueInTx` joins labels, not comments) | -| 25 | promote.go:`PromoteFromEphemeralInTx` (dolt/ephemeral_routing.go:205, embeddeddolt/store.go:1003, domain/db `PromoteFromEphemeral`) | INSERT issues (+ label/dep/comment copies), DELETE wisps | yes | no | OUT OF CONTRACT | promoted row lands with current_revision default 1 and no version row | -| 26 | dolt/ephemeral_routing.go:`DoltStore.demoteToWispInTx` | `UpdateIssueWithoutEventInTx` (row 5), then raw aux copies + DELETE FROM issues (:303) | yes | yes (update half only) | OUT OF CONTRACT | mints a version row for an issue the same tx then deletes; issue_versions has no FK to issues (0067), so the row strands | -| 27 | persistence.go:`MoveIssuePersistenceInTx` (`ExecuteUpdate` persistence patch; domain/db `MovePersistence`) | `InsertIssueStrictInTx` into the target plane, copy aux, DELETE source (persistence.go:70,106) | yes (twice) | no | OUT OF CONTRACT | plane move bypasses both the create and update seams | -| 28 | delete.go:`deleteIssueRowInTx` / `DeleteIssuesInTx` / `DeleteResolvedSetInTx`; delete_role.go:`DeleteInTx`; sweep.go:`SweepInTx`; dolt/wisps.go:`deleteWisp` / `deleteWispBatchTx`; domain/db/issue.go:`Delete` / `DeleteByIDs` (+ label/dependency/events `DeleteAllForIDs`, domain/issue_delete.go) | DELETE issues or wisps + aux + leases | yes (`RecordDeleteInTx`) | no | OUT OF CONTRACT | no surviving row; the deleted-Versioned-Bead guarantee is Phase 3 (PR #6358); orphaned issue_versions rows remain. delete_role.go:`RewriteDeletedReferencesInTx` then goes through row 5, so neighbours DO mint versions | -| 29 | bulk_ops.go:`DeleteIssuesBySourceRepoInTx` (dolt/issues.go:946, embeddeddolt/store.go:987) | bulk DELETE issues WHERE source_repo = ? | yes (per id) | no | OUT OF CONTRACT | bulk path | -| 30 | bulk_ops.go:`UpdateIssueIDInTx` via `updateIssueIDInTx` / `updateWispIDInTx` (dolt/rename.go:14, embeddeddolt/store.go:995) | UPDATE issues SET id = ? + dependency rekey | yes (dep-remove, delete old, create new, dep-add via `recordRenameInJournal`) | no | OUT OF CONTRACT | history stays keyed to the old issue_id; the new id carries current_revision with no matching rows | -| 31 | compaction.go:`ApplyCompactionInTx` (dolt/compact.go:52, embeddeddolt/store.go:1179) | UPDATE issues SET compaction_level, compacted_at, compacted_at_commit, original_size, updated_at | no (`beadDMLExemptions`) | no | OUT OF CONTRACT | the content rewrite that precedes it (internal/compact/compactor.go:147, cmd/bd/compact.go:702 via `store.UpdateIssue`) IS row 5, so compaction mints exactly one version for the summary while its own columns land unversioned | -| 32 | compaction.go:`RestoreFromSnapshotInTx` (dolt/compact.go:82, embeddeddolt/store.go:1203) | UPDATE issues content from snapshot (compaction.go:173,181) | no | no | OUT OF CONTRACT | restore's CAS composition is Phase 3 (PR #6358) | -| 33 | blocked_state.go mark/unmark templates via `RecomputeIsBlocked*` / `MarkIsBlockedInTx`; dependencies.go:`markDirectBlockingDependencySourceInTx`; domain/db/dependency.go:`markDirectBlockedSource` | UPDATE issues or wisps SET is_blocked | yes, flips only (journal.go:`recordBlockedJournalChanges`, actorless) | no | OUT OF CONTRACT | derived state; is_blocked is not hydrated into `durable_state` | -| 34 | batch_apply.go:`applyBatchRun` (`ApplyBatch`; uow/batch_applier.go) | per item: create rows 1/2, update rows 5/6, close row 17, dep add rows 14/15 | per item | per item | OUT OF CONTRACT (mixed) | dep-add items call `AddDependencyInTx` / `DependencyUseCase.AddDependencies`, never the versioned editor | -| 35 | schema/cli_migrations.go:`cliMigration0053RepairRigWisps` (INSERT IGNORE INTO issues, UPDATE issues, DELETE FROM wisps); schema/migration_repairs.go and schema/wisp_dep_forward_repair.go (dependencies rewrites) | migration DML | no | no | OUT OF CONTRACT | 0067/0068 themselves are DDL only (ADD COLUMN ... DEFAULT 1; no row backfill) | -| 36 | versioncontrolops/automerge.go:`resolveIssuesFieldMerge` (UPDATE `issues`); versioncontrolops/mergesettle.go:`TryRepairFKCascadeViolations` (`fkCascadeRepairDeletes` on dependencies/labels/comments) | merge-settle rewrites and orphan deletes | no | no | OUT OF CONTRACT | raw connection during merge settlement | -| 37 | dolt/federation.go:`deleteExcludedIssues` (from `stageFilteredBranch`) | DELETE FROM issues WHERE ephemeral = 1 / issue_type = ? on the staging branch | no | no | OUT OF CONTRACT | raw conn on a filtered-push branch | -| 38 | cmd/bd/doctor/fix/validation.go:`OrphanedDependencies` / `ChildParentDependencies` / `CrossTableDuplicates` | `tx.Exec` DELETE FROM dependencies / issues | no | no | OUT OF CONTRACT | raw doctor fixes | -| 39 | cmd/bd/sql.go (`storage.RawDBAccessor.UnderlyingDB` then `db.ExecContext`); cmd/bd/sql_proxied_server.go via uow `RawSQLUseCase().Exec` to domain/db/raw_sql.go | arbitrary SQL | no | no | OUT OF CONTRACT | the proxied variant runs inside a uow tx with the version scope bound (uow/dolt_sql_provider.go:178) but raw SQL never calls the seam | -| 40 | issueops/bootstrap.go:`BootstrapInTx` (uow/bootstrapper.go, hook_bootstrapper.go) | config + metadata tables only (`SetConfigInTx`, `SetMetadataInTx`) | no | no | OUT OF CONTRACT (n/a) | writes no issue-plane row at all | -| 41 | PLANNED 18th: `fk_dep_issue_target` ON DELETE CASCADE (schema/cli_migrations.go:284,311; migrations 0041/0043) | DB-level cascade deleting dependencies rows when their target issues row is deleted; no Go call site | no | no | Phase 3 (planned) | PR #6358 body: "Phase 3 items by design: the `fk_dep_issue_target` cascade (the 18th path)"; today it fires silently underneath rows 28-29 | - -### Notes as found at 321a79278 - -- **Count vs design.** Grouping by leaf mutation function, the code has **34 live issue-plane write paths** (rows 1-8 and 14-39; row 40 excluded because it writes no issue row; rows 9-13 are gates inside rows 1-8) plus the planned FK cascade — not eighteen. Of the 34, **8 are VERSIONED, 26 OUT OF CONTRACT**, and 5 NO-OP gates sit inside the versioned ones. The design's own list (be-hs42e.3) is not resolvable from this checkout or `bd show`; only PR #6358's "the 18th path" line was found, so the eighteen could not be reconciled item by item. -- **What actually versions:** create, update (and their uow twins in domain/db/issue.go) and the publicops dependency editor. Every other journaled mutation — close, reopen, claim, unclaim/release, lease reclaim, defer wake, labels, comments, promote, persistence move, delete, rename — reaches `RecordEventInTx` but never `RecordVersionInTx`, even though version_history.go:20-27 describes the seam as living "inside the same already-short-circuited functions that call RecordEventInTx". journal_completeness_test.go pins 57 `mutationEntryPoints` for the journal; the version seam is wired into 3 of them, and no equivalent completeness test exists for it. -- **Cross-leg asymmetry on dependencies:** the dolt and embeddeddolt DependencyEditor roles version (rows 7-8); the uow DependencyEditor role goes through domain/db dependency `Insert`/`Delete` and does not (rows 15-16). The legacy store verbs `AddDependency`/`RemoveDependency` do not on any leg (rows 14, 16). -- **Versioned only via a helper:** `MergeMetadataInTx`, `DeleteMetadataInTx`, `CompareAndSetMetadataKeyInTx`, `RewriteDeletedReferencesInTx`, compaction's content rewrite and `demoteToWispInTx` version solely because they call `UpdateIssueInTx` / `UpdateIssueWithoutEventInTx`. -- **Per-issue loops:** batch create, import, uow `InsertBatch` (versioned per issue); `ExecuteCloseBatch` loops `ExecuteClose` (row 17, unversioned per issue); `DeleteIssuesInTx` (unversioned). -- **Ordering inside a versioned call:** batch-create edges (row 3) and `ExecuteUpdate`'s label/parent patches (rows 23, 14) land after the mint, so the minted `durable_state` predates them. -- **Import upsert:** an unchanged re-import of an existing row (`isNew=false`, stale check not tripped) mints a version because create has no field-level no-op gate — unlike update's FR-2 behaviour (row 9). -- **No FK from issue_versions to issues** (migration 0067 creates the table without one), so delete, rename and demote (rows 26, 28, 30) strand version rows under ids that no longer exist in issues. -- **Exemption tables that name the seam:** journal_completeness_test.go `beadDMLExemptions` and row_lock_guard_test.go `funcNameExemptions` both list `RecordVersionInTx` as bookkeeping for an already-journaled mutation. -- **Drift during this session (2026-09-07 13:31-13:33):** the branch advanced to 56b2199d4 (`attribution_status` renamed to claimed|unknown, ordinals documented as local) and the working tree then carried further uncommitted edits by another agent — version_history.go (RFC 8785 canonical `durable_state` via `jcs`, with go.mod/go.sum), cli_migrations.go, the 0068 up/down SQL, three schema tests and an untracked version_history_durable_state_test.go. Neither adds or removes a `RecordVersionInTx` call site (the six above were re-verified against 321a79278 and the live tree) and no disposition changes; this inventory is against 321a79278 and does not cover them. diff --git a/backend/BUILD.bazel b/backend/BUILD.bazel deleted file mode 100644 index 912285a3e..000000000 --- a/backend/BUILD.bazel +++ /dev/null @@ -1,106 +0,0 @@ -load("@rules_go//go:def.bzl", "go_library", "go_test") - -go_library( - name = "backend", - srcs = [ - "backend.go", - "errors.go", - "types.go", - ], - importpath = "github.com/steveyegge/beads/backend", - visibility = ["//visibility:public"], - deps = [ - "//internal/storage", - "//internal/storage/backends", - "//internal/types", - ], -) - -go_test( - name = "backend_test", - srcs = [ - "completeness_test.go", - "errors_test.go", - ], - data = [":go_srcs"], - deps = [ - ":backend", - "//backend/conformance", - "//internal/storage", - "//internal/testutil/bazeltest", - ], -) - -# --- begin go_srcs (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) --- - -filegroup( - name = "go_srcs", - srcs = glob( - ["**/*.go"], - allow_empty = True, - ), - visibility = ["//:__subpackages__"], -) - -# --- end go_srcs --- - -# --- begin repo_files (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) --- - -filegroup( - name = "repo_doc_files", - srcs = glob( - [ - "**/*.md", - "**/*.mdx", - ], - allow_empty = True, - exclude = ["**/node_modules/**"], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_go_srcs", - srcs = glob( - ["**/*.go"], - allow_empty = True, - exclude = [ - "**/*_test.go", - "**/node_modules/**", - ], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_go_test_srcs", - srcs = glob( - ["**/*_test.go"], - allow_empty = True, - exclude = ["**/node_modules/**"], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_other_files", - srcs = glob( - ["**"], - exclude = [ - "**/*.db", - "**/*.exe", - "**/*.go", - "**/*.md", - "**/*.mdx", - "**/*.out", - "**/*.prof", - "**/*.pyc", - "**/*.test", - "**/__pycache__/**", - "**/node_modules/**", - ], - ), - visibility = ["//:__subpackages__"], -) - -# --- end repo_files --- diff --git a/backend/backend.go b/backend/backend.go index 2981739aa..de2f184dd 100644 --- a/backend/backend.go +++ b/backend/backend.go @@ -140,37 +140,6 @@ type VCStatus = storage.Status // store. See the field docs on the aliased struct. type Backend = backends.Backend -// OpenOptions carries per-open injections (Credential, HTTPClient, -// UserAgent) that Backend.OpenWith may use. See the field docs on the -// aliased struct. -type OpenOptions = backends.OpenOptions - -// Credential is the opaque per-open credential marker OpenOptions.Credential -// carries. A backend that needs one declares its own narrower interface and -// type-asserts the value its OpenWith receives. See the aliased type's docs. -type Credential = backends.Credential - -// ErrCredentialWithoutOpenWith is returned by Backend.OpenWithOptions when -// OpenOptions.Credential is set but the backend has no OpenWith to honor it. -var ErrCredentialWithoutOpenWith = backends.ErrCredentialWithoutOpenWith - -// ErrHTTPClientWithoutOpenWith is returned by Backend.OpenWithOptions when -// OpenOptions.HTTPClient is set but the backend has no OpenWith to honor it. -// Same fail-closed family as ErrCredentialWithoutOpenWith. -var ErrHTTPClientWithoutOpenWith = backends.ErrHTTPClientWithoutOpenWith - -// ErrUserAgentWithoutOpenWith is returned by Backend.OpenWithOptions when -// OpenOptions.UserAgent is set but the backend has no OpenWith to honor it. -// Same fail-closed family as ErrCredentialWithoutOpenWith. -var ErrUserAgentWithoutOpenWith = backends.ErrUserAgentWithoutOpenWith - -// ErrUnsupportedCredential is the typed refusal an OpenWith implementation -// MUST return (directly or wrapped, so errors.Is still matches) when it -// receives a non-nil Credential that does not type-assert to the narrower -// credential interface that backend's OpenWith expects. See the aliased -// error's docs in internal/storage/backends for the full contract. -var ErrUnsupportedCredential = backends.ErrUnsupportedCredential - // Register adds a backend under name. It is process-start wiring: call it // once during initialization (typically from a registrant package's init that // the embedding binary blank-imports), before any concurrent store access. @@ -202,12 +171,6 @@ func WorkspaceIsBeadsDir(name string) bool { return backends.WorkspaceIsBeadsDir(name) } -// IsRemote reports whether name is a registered backend that is a pure -// network client of a bd serve with no local database (see Backend.Remote). -func IsRemote(name string) bool { - return backends.IsRemote(name) -} - // Iter is the streaming-read cursor returned by the Iter* methods. type Iter[T any] = storage.Iter[T] diff --git a/backend/completeness_test.go b/backend/completeness_test.go index 82ab9b43d..1ce656d59 100644 --- a/backend/completeness_test.go +++ b/backend/completeness_test.go @@ -5,6 +5,7 @@ import ( "go/parser" "go/token" "io/fs" + "path/filepath" "reflect" "runtime" "sort" @@ -13,7 +14,6 @@ import ( "github.com/steveyegge/beads/backend" "github.com/steveyegge/beads/backend/conformance" - "github.com/steveyegge/beads/internal/testutil/bazeltest" ) const internalPrefix = "github.com/steveyegge/beads/internal/" @@ -77,7 +77,7 @@ func publicAliases(t *testing.T) map[string]bool { if !ok { t.Fatal("runtime.Caller failed") } - dir := bazeltest.CallerDir(thisFile, "backend") + dir := filepath.Dir(thisFile) fset := token.NewFileSet() pkgs, err := parser.ParseDir(fset, dir, func(fi fs.FileInfo) bool { return !strings.HasSuffix(fi.Name(), "_test.go") diff --git a/backend/conformance/BUILD.bazel b/backend/conformance/BUILD.bazel deleted file mode 100644 index 2ba90b30b..000000000 --- a/backend/conformance/BUILD.bazel +++ /dev/null @@ -1,184 +0,0 @@ -load("@rules_go//go:def.bzl", "go_library", "go_test") - -go_library( - name = "conformance", - srcs = [ - "audit.go", - "audit_config_metadata_slots_repomtime.go", - "audit_dependencies_readiness.go", - "audit_issue-lifecycle.go", - "audit_labels-comments-events.go", - "audit_molecule_wisp_batch_iter.go", - "audit_search-counts-stats.go", - "batch_apply_contract.go", - "batch_apply_gate_contract.go", - "batch_closer_contract.go", - "batch_creator_contract.go", - "batch_getter_contract.go", - "blocked_lister_contract.go", - "blocked_state.go", - "blocking_annotator_contract.go", - "bootstrapper_contract.go", - "claim.go", - "claimer_contract.go", - "commenter_contract.go", - "conformance.go", - "counter_contract.go", - "cross_record_invariant_contract.go", - "cycle_detector_contract.go", - "deleter_contract.go", - "dependency_editor_contract.go", - "dualwrite_history_contract.go", - "dualwrite_participation_generation_contract.go", - "edge_reader_contract.go", - "expected_revision_contract.go", - "graph_counter_contract.go", - "heartbeater_contract.go", - "history_matching.go", - "importer_contract.go", - "issue_operations_contract.go", - "issue_operations_staging.go", - "journal_contract.go", - "lease_reclaimer_contract.go", - "lifecycle_close_reopen_contract.go", - "lifecycle_create_contract.go", - "lifecycle_create_gate_contract.go", - "lifecycle_update_contract.go", - "lifecycle_update_gate_contract.go", - "memories_contract.go", - "metadata_cas_contract.go", - "molecule_contract.go", - "portable.go", - "querier_contract.go", - "reader_contract.go", - "ready_claimer_contract.go", - "ready_counter_contract.go", - "reclaim_scope.go", - "relations_contract.go", - "releaser_contract.go", - "retention_epoch_contract.go", - "role_bundle.go", - "role_bundle_cases.go", - "search_paging.go", - "stats_reporter_contract.go", - "sweeper_contract.go", - "transaction_parity.go", - "tree_walker_contract.go", - "version_reconciler_contract.go", - "versioned_read_contract.go", - "workspaceconfig_contract.go", - ], - importpath = "github.com/steveyegge/beads/backend/conformance", - visibility = ["//visibility:public"], - deps = [ - "//internal/hooks", - "//internal/storage", - "//internal/storage/issueops", - "//internal/storage/kvkeys", - "//internal/storage/sqlbuild", - "//internal/types", - "//issueops", - "//journalops", - "//memoryops", - ], -) - -go_test( - name = "conformance_test", - srcs = [ - "role_bundle_test.go", - "role_coverage_gate_test.go", - "role_coverage_scan_test.go", - "versioned_history_wiring_test.go", - ], - data = [ - ":go_srcs", - "//beadserrors:go_srcs", - "//internal/types:go_srcs", - "//issueops:go_srcs", - "//journalops:go_srcs", - "//memoryops:go_srcs", - ], - embed = [":conformance"], - deps = [ - "//internal/storage", - "//internal/testutil/bazeltest", - "//issueops", - "//journalops", - "//memoryops", - ], -) - -# --- begin go_srcs (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) --- - -filegroup( - name = "go_srcs", - srcs = glob( - ["**/*.go"], - allow_empty = True, - ), - visibility = ["//:__subpackages__"], -) - -# --- end go_srcs --- - -# --- begin repo_files (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) --- - -filegroup( - name = "repo_doc_files", - srcs = glob( - [ - "**/*.md", - "**/*.mdx", - ], - allow_empty = True, - exclude = ["**/node_modules/**"], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_go_srcs", - srcs = glob( - ["**/*.go"], - allow_empty = True, - exclude = [ - "**/*_test.go", - "**/node_modules/**", - ], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_go_test_srcs", - srcs = glob( - ["**/*_test.go"], - allow_empty = True, - exclude = ["**/node_modules/**"], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_other_files", - srcs = glob( - ["**"], - exclude = [ - "**/*.db", - "**/*.exe", - "**/*.go", - "**/*.md", - "**/*.mdx", - "**/*.out", - "**/*.prof", - "**/*.pyc", - "**/*.test", - "**/__pycache__/**", - "**/node_modules/**", - ], - ), - visibility = ["//:__subpackages__"], -) - -# --- end repo_files --- diff --git a/backend/conformance/audit_labels-comments-events.go b/backend/conformance/audit_labels-comments-events.go index e6eda7b45..7aa69e159 100644 --- a/backend/conformance/audit_labels-comments-events.go +++ b/backend/conformance/audit_labels-comments-events.go @@ -113,12 +113,11 @@ func testAuditLabelEventNonIdempotent(t *testing.T, f Factory) { } } -// testAuditCountCommentsWispParity pins that CountIssueComments wisp-routes the -// same way GetIssueComments does: a wisp's structured comments live in -// `wisp_comments`, and the count must agree with the rows (GH#5565 — the count -// used to query only `comments` and report 0, so `bd show` showed comment_count 0 -// on a wisp that had comments). -func testAuditCountCommentsWispParity(t *testing.T, f Factory) { +// testAuditCountCommentsWispAsymmetry pins the intentional route asymmetry: +// GetIssueComments wisp-routes, but CountIssueComments always queries `comments`. +// A wisp with structured comments returns them from GetIssueComments but reports 0 +// from CountIssueComments. +func testAuditCountCommentsWispAsymmetry(t *testing.T, f Factory) { s := f(t) c := ctx() must(t, s.CreateIssue(c, withDefaults(&types.Issue{ID: "test-w", Title: "wisp", Ephemeral: true}), "a")) @@ -134,8 +133,8 @@ func testAuditCountCommentsWispParity(t *testing.T, f Factory) { } n, err := s.CountIssueComments(c, "test-w") must(t, err) - if n != int64(len(got)) { - t.Errorf("CountIssueComments(wisp) = %d, want %d (must wisp-route like GetIssueComments)", n, len(got)) + if n != 0 { + t.Errorf("CountIssueComments(wisp) = %d, want 0 (queries only `comments`, never wisp-routed)", n) } } @@ -298,7 +297,7 @@ func RunAudit_labels_comments_events(t *testing.T, f Factory) { t.Helper() t.Run("EventValueNullability", func(t *testing.T) { testAuditEventValueNullability(t, f) }) t.Run("LabelEventNonIdempotent", func(t *testing.T) { testAuditLabelEventNonIdempotent(t, f) }) - t.Run("CountCommentsWispParity", func(t *testing.T) { testAuditCountCommentsWispParity(t, f) }) + t.Run("CountCommentsWispAsymmetry", func(t *testing.T) { testAuditCountCommentsWispAsymmetry(t, f) }) t.Run("LabelCollationOrder", func(t *testing.T) { testAuditLabelCollationOrder(t, f) }) t.Run("EventsSinceStrictBoundary", func(t *testing.T) { testAuditEventsSinceStrictBoundary(t, f) }) t.Run("EventsLimitAndAll", func(t *testing.T) { testAuditEventsLimitAndAll(t, f) }) diff --git a/backend/conformance/audit_molecule_wisp_batch_iter.go b/backend/conformance/audit_molecule_wisp_batch_iter.go index 48d5909db..c0ad03286 100644 --- a/backend/conformance/audit_molecule_wisp_batch_iter.go +++ b/backend/conformance/audit_molecule_wisp_batch_iter.go @@ -501,13 +501,9 @@ func testAuditCreateAllWispsInlineDependencies(t *testing.T, f Factory) { } } -// A mixed regular+wisp batch with a cross-bucket edge is rejected before insert -// (the strict plane rule, issueops.ValidateCreateIssuesMixedBucketDependencies); -// with SkipDependencyValidationErrors — the import mode — both issues persist -// AND the edge is wired: every row of both planes is written on the one -// transaction before the dependency pass, which resolves the target on its own -// plane. Until wy-a648lq the tolerant arm dropped the edge instead, and a -// re-import could never backfill it. +// A mixed regular+wisp batch with a cross-bucket edge is rejected before insert; +// with SkipDependencyValidationErrors the edge is dropped and both issues persist +// (create.go:288-361). func testAuditCreateCrossBucketDependency(t *testing.T, f Factory) { s := f(t) c := ctx() @@ -524,7 +520,7 @@ func testAuditCreateCrossBucketDependency(t *testing.T, f Factory) { t.Errorf("(a) test-a persisted despite rejected batch: %v", err) } - // (b) With skip: both persist, and the cross-bucket edge is wired. + // (b) With skip: both persist, edge dropped. must(t, s.CreateIssuesWithFullOptions(c, []*types.Issue{ withDefaults(&types.Issue{ID: "test-c", Title: "C"}), withDefaults(&types.Issue{ID: "test-d", Title: "D", Ephemeral: true, Dependencies: []*types.Dependency{{IssueID: "test-d", DependsOnID: "test-c", Type: types.DepBlocks}}}), @@ -536,8 +532,8 @@ func testAuditCreateCrossBucketDependency(t *testing.T, f Factory) { t.Errorf("(b) test-d missing: %v", err) } recs, _ := s.GetAllDependencyRecords(c) - if got := auditDepTargets(recs["test-d"]); !slices.Equal(got, []string{"test-c"}) { - t.Errorf("(b) test-d deps = %v, want [test-c] (in-batch cross-bucket edge wired, not dropped)", got) + if len(recs["test-d"]) != 0 { + t.Errorf("(b) cross-bucket edge not dropped: %v", auditDepTargets(recs["test-d"])) } } diff --git a/backend/conformance/audit_search-counts-stats.go b/backend/conformance/audit_search-counts-stats.go index 115b1651f..e09bed52f 100644 --- a/backend/conformance/audit_search-counts-stats.go +++ b/backend/conformance/audit_search-counts-stats.go @@ -653,9 +653,7 @@ func testAuditStaleStatusOverride(t *testing.T, f Factory) { } // Epics with ZERO children are silently skipped; epics WITH children are returned -// with Total/Closed counts and EligibleForClose = (Total == completing-closed). -// A child closed for a non-completing reason (duplicate/wontfix/superseded/…) -// counts toward Closed but not toward eligibility (GH#5026). +// with Total/Closed counts and EligibleForClose = (Total>0 && Total==Closed). func testAuditEpicsEligiblePartial(t *testing.T, f Factory) { s := f(t) c := ctx() @@ -665,12 +663,9 @@ func testAuditEpicsEligiblePartial(t *testing.T, f Factory) { must(t, s.CreateIssue(c, withDefaults(&types.Issue{ID: "ee-2", Title: "E2", IssueType: types.TypeEpic, Status: types.StatusOpen}), "a")) must(t, s.CreateIssue(c, withDefaults(&types.Issue{ID: "ee-2a", Title: "c", Status: types.StatusClosed}), "a")) must(t, s.CreateIssue(c, withDefaults(&types.Issue{ID: "ee-3", Title: "E3", IssueType: types.TypeEpic, Status: types.StatusOpen}), "a")) - must(t, s.CreateIssue(c, withDefaults(&types.Issue{ID: "ee-4", Title: "E4", IssueType: types.TypeEpic, Status: types.StatusOpen}), "a")) - must(t, s.CreateIssue(c, withDefaults(&types.Issue{ID: "ee-4a", Title: "c", Status: types.StatusClosed, CloseReason: "duplicate of ee-1"}), "a")) must(t, s.AddDependency(c, &types.Dependency{IssueID: "ee-1a", DependsOnID: "ee-1", Type: types.DepParentChild}, "a")) must(t, s.AddDependency(c, &types.Dependency{IssueID: "ee-1b", DependsOnID: "ee-1", Type: types.DepParentChild}, "a")) must(t, s.AddDependency(c, &types.Dependency{IssueID: "ee-2a", DependsOnID: "ee-2", Type: types.DepParentChild}, "a")) - must(t, s.AddDependency(c, &types.Dependency{IssueID: "ee-4a", DependsOnID: "ee-4", Type: types.DepParentChild}, "a")) epics, err := s.GetEpicsEligibleForClosure(c) must(t, err) @@ -700,17 +695,6 @@ func testAuditEpicsEligiblePartial(t *testing.T, f Factory) { if _, present := byID["ee-3"]; present { t.Error("ee-3 (zero children) must be ABSENT (silent skip)") } - - // dcr-tf2 / GH#5026: a single child closed as a duplicate still counts as - // Closed (raw close count), but must NOT count toward EligibleForClose — - // the epic's real scope is unfinished, not done. - e4 := byID["ee-4"] - if e4 == nil { - t.Fatal("ee-4 missing (has children, must be returned)") - } - if e4.TotalChildren != 1 || e4.ClosedChildren != 1 || e4.EligibleForClose { - t.Errorf("ee-4 = {Total:%d Closed:%d Eligible:%v}, want {1 1 false} (duplicate close is non-completing)", e4.TotalChildren, e4.ClosedChildren, e4.EligibleForClose) - } } // GetIssuesByLabel unions wisp_labels: an ephemeral (wisp) issue sharing the label diff --git a/backend/conformance/batch_apply_contract.go b/backend/conformance/batch_apply_contract.go index 333745ada..e026872b9 100644 --- a/backend/conformance/batch_apply_contract.go +++ b/backend/conformance/batch_apply_contract.go @@ -6,7 +6,6 @@ import ( "errors" "fmt" "reflect" - "strings" "testing" "github.com/steveyegge/beads/internal/types" @@ -171,12 +170,6 @@ func RunBatchApplyAppliesEveryItemInDeclarationOrder(t *testing.T, ctx context.C if got := batchApplyColumn(t, ctx, fixture, "status", blocked); got == string(types.StatusClosed) { t.Errorf("%s was closed by a request that only asked to close %s", blocked, blocker) } - // Neither create item names a creator, so each defaults to the request's actor. - for _, id := range []string{blocked, blocker} { - if got := batchApplyColumn(t, ctx, fixture, "created_by", id); got != "apply-writer" { - t.Errorf("%s created_by = %q, want the request's actor %q", id, got, "apply-writer") - } - } } // RunBatchApplyBindsEachNamedKeyToItsMintedID pins ApplyBatchResult.Keys: it is @@ -888,12 +881,9 @@ func RunBatchApplyEvaluatesExpectedAssigneeAsModified(t *testing.T, ctx context. // close it. The refusal is the assertion; the rollback of the parent it created // is what says the refusal took the request with it. // -// POSITIVE HALF: the identical request with Force lands, and that is the proof -// the refusal was the policy rather than the shape. Force "bypasses blocker and -// open-child close policy, the pin and the assignee fence, and never the -// template guard" (batchapplier.go), and the parent trips no guard — the -// unforced half reached the policy, which the guards run before — so the -// policy is the only refusal Force had to waive. +// POSITIVE HALF: the identical request with Force lands, which is the whole of +// what Force does ("bypasses blocker and open-child close policy, and nothing +// else") and the proof that the refusal was the policy rather than the shape. func RunBatchApplyClosePolicyEvaluatesAtTheCloseItem(t *testing.T, ctx context.Context, fixture BatchApplyFixture) { t.Helper() parent := fixture.IssuePrefix + "-closepolicy-parent" @@ -933,7 +923,7 @@ func RunBatchApplyClosePolicyEvaluatesAtTheCloseItem(t *testing.T, ctx context.C t.Errorf("%s status = %q under Force, want %q", parent, got, types.StatusClosed) } if got := batchApplyColumn(t, ctx, fixture, "status", child); got != string(types.StatusOpen) { - t.Errorf("%s status = %q, want %q: Force waives the parent's close policy, it does not close the child", child, got, types.StatusOpen) + t.Errorf("%s status = %q, want %q: Force bypasses the close policy and nothing else", child, got, types.StatusOpen) } assertBatchApplyTypedEdgeCount(t, ctx, fixture, child, parent, string(publicops.DepParentChild), 1) } @@ -1531,215 +1521,6 @@ func RunBatchApplyNormalizesTheWaitsForGate(t *testing.T, ctx context.Context, f }) } -// RunBatchApplyStampsSpawnerIDOnlyWhenNamed is the S11 review fix-up -// regression for HIGH-2: a waits-for DepAddItem must only acquire -// metadata.spawner_id when the caller explicitly named a spawner -// (HasSpawner=true, which bd create --graph sets when the plan declares -// edges[].spawner_key/spawner_id, as does an embedder lowering such a plan -// onto this role). An edge with no named spawner must keep its gate-only -// metadata untouched — stamping it unconditionally from the resolved target -// caused unnecessary rewrite/version churn when gc re-applies the same edge -// (see CHANGELOG.md). -func RunBatchApplyStampsSpawnerIDOnlyWhenNamed(t *testing.T, ctx context.Context, fixture BatchApplyFixture) { - t.Helper() - spawner := fixture.IssuePrefix + "-spawner-named" - waiter := fixture.IssuePrefix + "-waiter-named" - batchApplySeedIssue(t, ctx, fixture, spawner, types.StatusOpen) - batchApplySeedIssue(t, ctx, fixture, waiter, types.StatusOpen) - - batchApplyMust(t, ctx, fixture, publicops.ApplyBatchRequest{ - Actor: "apply-writer", - Items: []publicops.ApplyItem{ - {Kind: publicops.ItemDepAdd, DepAdd: &publicops.DepAddItem{ - Source: publicops.Ref{ID: waiter}, - Target: publicops.Ref{ID: spawner}, - Type: publicops.DepWaitsFor, - Metadata: `{"gate":"any-children"}`, - HasSpawner: true, - }}, - }, - }) - stored := batchApplyEdgeMetadata(t, ctx, fixture, waiter, spawner) - var meta types.WaitsForMeta - if err := json.Unmarshal([]byte(stored), &meta); err != nil { - t.Fatalf("stored named-spawner metadata %q: %v", stored, err) - } - if meta.SpawnerID != spawner { - t.Errorf("named-spawner edge spawner_id = %q, want %q (metadata %q)", meta.SpawnerID, spawner, stored) - } - - spawner2 := fixture.IssuePrefix + "-spawner-unnamed" - waiter2 := fixture.IssuePrefix + "-waiter-unnamed" - batchApplySeedIssue(t, ctx, fixture, spawner2, types.StatusOpen) - batchApplySeedIssue(t, ctx, fixture, waiter2, types.StatusOpen) - - batchApplyMust(t, ctx, fixture, publicops.ApplyBatchRequest{ - Actor: "apply-writer", - Items: []publicops.ApplyItem{ - batchApplyDepAdd(publicops.Ref{ID: waiter2}, publicops.Ref{ID: spawner2}, publicops.DepWaitsFor, `{"gate":"any-children"}`), - }, - }) - stored2 := batchApplyEdgeMetadata(t, ctx, fixture, waiter2, spawner2) - var meta2 types.WaitsForMeta - if err := json.Unmarshal([]byte(stored2), &meta2); err != nil { - t.Fatalf("stored unnamed-spawner metadata %q: %v", stored2, err) - } - if meta2.SpawnerID != "" { - t.Errorf("unnamed-spawner edge must not stamp spawner_id, got %q (metadata %q)", meta2.SpawnerID, stored2) - } -} - -// RunBatchApplyCarriesThreadIDOntoTheStoredEdge pins DepAddItem.ThreadID -// (the 2026-10 Opus-review HIGH-2 finding): a conversation-threading id named -// on a dep_add item must land on the stored dependency row's own thread_id -// column, and an item that names none must store none. ThreadID is a plain -// column read independently of HasSpawner/Metadata, so this is its own case -// rather than a clause folded onto the spawner one above. -// -// Wired onto the http leg this exercises BOTH halves of the wire at once: the -// client's applyDepAddItemBody encode (batchapplier.go) and the server's -// applyDepAddItemMembers decode (internal/httpapi/batch_apply.go) must each -// carry the field, because either one silently dropping it leaves the stored -// column blank and fails the assertion below. -func RunBatchApplyCarriesThreadIDOntoTheStoredEdge(t *testing.T, ctx context.Context, fixture BatchApplyFixture) { - t.Helper() - source := fixture.IssuePrefix + "-thread-source" - target := fixture.IssuePrefix + "-thread-target" - batchApplySeedIssue(t, ctx, fixture, source, types.StatusOpen) - batchApplySeedIssue(t, ctx, fixture, target, types.StatusOpen) - - const thread = "thread-conv-1" - batchApplyMust(t, ctx, fixture, publicops.ApplyBatchRequest{ - Actor: "apply-writer", - Items: []publicops.ApplyItem{ - {Kind: publicops.ItemDepAdd, DepAdd: &publicops.DepAddItem{ - Source: publicops.Ref{ID: source}, Target: publicops.Ref{ID: target}, - Type: publicops.DepBlocks, ThreadID: thread, - }}, - }, - }) - if got := batchApplyEdgeThreadID(t, ctx, fixture, source, target); got != thread { - t.Errorf("stored edge thread_id = %q, want %q", got, thread) - } - - source2 := fixture.IssuePrefix + "-thread-none-source" - target2 := fixture.IssuePrefix + "-thread-none-target" - batchApplySeedIssue(t, ctx, fixture, source2, types.StatusOpen) - batchApplySeedIssue(t, ctx, fixture, target2, types.StatusOpen) - batchApplyMust(t, ctx, fixture, publicops.ApplyBatchRequest{ - Actor: "apply-writer", - Items: []publicops.ApplyItem{ - batchApplyDepAdd(publicops.Ref{ID: source2}, publicops.Ref{ID: target2}, publicops.DepBlocks, ""), - }, - }) - if got := batchApplyEdgeThreadID(t, ctx, fixture, source2, target2); got != "" { - t.Errorf("an edge naming no thread must not stamp thread_id, got %q", got) - } -} - -// RunBatchApplyCarriesThreadIDOntoAnExistingEdge pins DepAddItem.ThreadID on -// the re-add arm: a dep_add naming an edge that already exists with the same -// type is the idempotent re-add, and a thread it names still has to reach the -// stored row. That arm once compared and rewrote metadata alone, so a -// thread-only re-add was a silent no-op and a re-add moving both landed the -// metadata and dropped the thread — on the very path gc re-applies its plans -// through. -// -// The steps run on ONE edge, each from the state the step before it left: a -// thread-only re-add puts a thread on an edge that had none, a re-add moving -// the gate and the thread lands both, and a re-add naming no thread keeps the -// stored one, because an empty ThreadID names no thread rather than asking for -// none. -func RunBatchApplyCarriesThreadIDOntoAnExistingEdge(t *testing.T, ctx context.Context, fixture BatchApplyFixture) { - t.Helper() - source := fixture.IssuePrefix + "-rethread-source" - spawner := fixture.IssuePrefix + "-rethread-spawner" - batchApplySeedIssue(t, ctx, fixture, source, types.StatusOpen) - batchApplySeedIssue(t, ctx, fixture, spawner, types.StatusOpen) - - add := func(gate, thread string) { - t.Helper() - batchApplyMust(t, ctx, fixture, publicops.ApplyBatchRequest{ - Actor: "apply-writer", - Items: []publicops.ApplyItem{ - {Kind: publicops.ItemDepAdd, DepAdd: &publicops.DepAddItem{ - Source: publicops.Ref{ID: source}, Target: publicops.Ref{ID: spawner}, - Type: publicops.DepWaitsFor, Metadata: `{"gate":"` + gate + `"}`, ThreadID: thread, - }}, - }, - }) - } - assertEdge := func(step, wantGate, wantThread string) { - t.Helper() - assertBatchApplyEdgeCount(t, ctx, fixture, source, spawner, 1) - stored := batchApplyEdgeMetadata(t, ctx, fixture, source, spawner) - var meta types.WaitsForMeta - if err := json.Unmarshal([]byte(stored), &meta); err != nil { - t.Fatalf("%s: stored waits-for metadata %q is not a gate object: %v", step, stored, err) - } - if meta.Gate != wantGate { - t.Errorf("%s: stored gate = %q (metadata %q), want %q", step, meta.Gate, stored, wantGate) - } - if got := batchApplyEdgeThreadID(t, ctx, fixture, source, spawner); got != wantThread { - t.Errorf("%s: stored edge thread_id = %q, want %q", step, got, wantThread) - } - } - - add(types.WaitsForAllChildren, "") - assertEdge("the first add, naming no thread", types.WaitsForAllChildren, "") - - add(types.WaitsForAllChildren, "thread-conv-1") - assertEdge("a re-add moving the thread alone", types.WaitsForAllChildren, "thread-conv-1") - - add(types.WaitsForAnyChildren, "thread-conv-2") - assertEdge("a re-add moving the gate and the thread", types.WaitsForAnyChildren, "thread-conv-2") - - add(types.WaitsForAnyChildren, "") - assertEdge("a re-add naming no thread", types.WaitsForAnyChildren, "thread-conv-2") -} - -// RunBatchApplyRefusesAThreadIDLongerThanItsColumn pins the bound on -// DepAddItem.ThreadID: one longer than the thread_id column holds is -// ErrValidation and writes no edge, on every leg alike. The http leg's decode -// refused it as a typed 400 while the local legs carried it to the write, where -// it failed as whatever storage error the column raised, mid-transaction — two -// error classes for one request. The shared plan bounds it now, before any -// database work. -// -// The positive half is the same edge with a thread exactly at the bound, which -// lands whole; without it, a role refusing every thread id satisfies the -// refusal. -func RunBatchApplyRefusesAThreadIDLongerThanItsColumn(t *testing.T, ctx context.Context, fixture BatchApplyFixture) { - t.Helper() - source := fixture.IssuePrefix + "-longthread-source" - target := fixture.IssuePrefix + "-longthread-target" - batchApplySeedIssue(t, ctx, fixture, source, types.StatusOpen) - batchApplySeedIssue(t, ctx, fixture, target, types.StatusOpen) - - request := func(thread string) publicops.ApplyBatchRequest { - return publicops.ApplyBatchRequest{ - Actor: "apply-writer", - Items: []publicops.ApplyItem{ - {Kind: publicops.ItemDepAdd, DepAdd: &publicops.DepAddItem{ - Source: publicops.Ref{ID: source}, Target: publicops.Ref{ID: target}, - Type: publicops.DepBlocks, ThreadID: thread, - }}, - }, - } - } - tooLong := strings.Repeat("t", types.MaxFieldLen+1) - if _, err := fixture.BatchApplier.ApplyBatch(ctx, request(tooLong)); !errors.Is(err, publicops.ErrValidation) { - t.Fatalf("a %d-character thread_id: error = %v, want ErrValidation", len(tooLong), err) - } - assertBatchApplyEdgeCount(t, ctx, fixture, source, target, 0) - - atBound := strings.Repeat("t", types.MaxFieldLen) - batchApplyMust(t, ctx, fixture, request(atBound)) - if got := batchApplyEdgeThreadID(t, ctx, fixture, source, target); got != atBound { - t.Errorf("a %d-character thread_id stored as %d characters, want it whole", len(atBound), len(got)) - } -} - // RunBatchApplySplicesAForwardMetadataRef pins the one exception to the // backward-only rule (CreateItem.MetadataRefs): "IT IS THE ONE PLACE A KEY MAY // REACH FORWARD … Every id is minted before any splice is applied, so the @@ -2001,56 +1782,21 @@ 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, atBoundItems) + atBound := make([]publicops.ApplyItem, publicops.MaxApplyBatchItems) 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) != atBoundItems { - t.Fatalf("a request of %d items returned %d item results, want %d", atBoundItems, len(result.Items), atBoundItems) + 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 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) + 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) } - // Unchanged: the true production bound, regardless of atBoundItems. overBound := make([]publicops.ApplyItem, publicops.MaxApplyBatchItems+1) for i := range overBound { overBound[i] = batchApplyCreate("", batchApplyMintedIssue(refused)) @@ -2403,20 +2149,6 @@ func batchApplyEdgeMetadata(t *testing.T, ctx context.Context, fixture BatchAppl return blob } -// batchApplyEdgeThreadID reads the stored thread_id column of one edge, which -// is where DepAddItem.ThreadID has to be visible — a plain column, independent -// of the type-specific Metadata blob batchApplyEdgeMetadata reads. -func batchApplyEdgeThreadID(t *testing.T, ctx context.Context, fixture BatchApplyFixture, source, target string) string { - t.Helper() - var value string - const query = "SELECT COALESCE(thread_id, '') FROM dependencies WHERE issue_id = ?" + - " AND COALESCE(depends_on_issue_id, depends_on_wisp_id, depends_on_external) = ?" - if err := fixture.QueryScalar(ctx, query, []any{source, target}, &value); err != nil { - t.Fatalf("reading edge thread_id for %s -> %s: %v", source, target, err) - } - return value -} - // batchApplyMetadataKey reads the WHOLE metadata blob off a row and reports one // key's raw bytes. The whole blob is read rather than a JSON path extract, // because that is what lets a case see an absent key and a null one as the @@ -2501,287 +2233,3 @@ func batchApplyHistoryMatching(t *testing.T, ctx context.Context, fixture BatchA } return count } - -// RunBatchApplyUpdateItemsRefuseATemplate pins that an update item answers to -// Lifecycle.Update's template guard: an item targeting a template refuses with -// an *ItemError carrying *TemplateReadOnlyError, whatever force flags it -// carries, and the request is all or nothing, so the create ahead of it does -// not land and the template is untouched. -func RunBatchApplyUpdateItemsRefuseATemplate(t *testing.T, ctx context.Context, fixture BatchApplyFixture) { - t.Helper() - template := fixture.IssuePrefix + "-tmplupd-template" - bystander := fixture.IssuePrefix + "-tmplupd-bystander" - seeded := batchApplyIssue(template, "a template") - seeded.IsTemplate = true - if err := fixture.CreateIssue(ctx, seeded, "seed"); err != nil { - t.Fatalf("seed %s: %v", template, err) - } - - update := batchApplyUpdate(publicops.Ref{ID: template}, publicops.IssuePatch{ - Title: publicops.Field[string]{Set: true, Value: "edited"}, - Status: publicops.Field[publicops.Status]{Set: true, Value: types.StatusClosed}, - }) - update.Update.ForceClosePolicy = true - _, err := fixture.BatchApplier.ApplyBatch(ctx, publicops.ApplyBatchRequest{ - Actor: "apply-writer", - ForceIDPrefix: true, - Items: []publicops.ApplyItem{ - batchApplyCreate("bystander", batchApplyIssue(bystander, "must not land beside a refusal")), - update, - }, - }) - var itemErr *publicops.ItemError - if !errors.As(err, &itemErr) { - t.Fatalf("an update item on template %s: error = %v, want an *ItemError", template, err) - } - if itemErr.Kind != publicops.ItemUpdate || itemErr.Index != 1 { - t.Errorf("ItemError = %#v, want the update item at index 1", itemErr) - } - var refusal *publicops.TemplateReadOnlyError - if !errors.As(err, &refusal) || refusal.IssueID != template { - t.Errorf("update item on template = %v, want *TemplateReadOnlyError naming %s", err, template) - } - assertBatchApplyRowCount(t, ctx, fixture, "issues", bystander, 0) - if got := batchApplyColumn(t, ctx, fixture, "title", template); got != "a template" { - t.Errorf("%s title after the refusal = %q, want it untouched", template, got) - } - if got := batchApplyColumn(t, ctx, fixture, "status", template); got != string(types.StatusOpen) { - t.Errorf("%s status after the refusal = %q, want it untouched", template, got) - } -} - -// RunBatchApplySplicesTheMetadataOfATemplateItCreates is the template guard's -// boundary: the one write a template legitimately takes inside a batch is a -// CREATE item that makes it and asks for a metadata splice. The splice runs as -// an update of the row the request itself is creating, after every id exists, -// and it must land — the template is being written, not modified — so a guard -// placed where the splice reaches it would refuse a template's own creation. -func RunBatchApplySplicesTheMetadataOfATemplateItCreates(t *testing.T, ctx context.Context, fixture BatchApplyFixture) { - t.Helper() - created := fixture.IssuePrefix + "-tmplsplice-created" - fresh := batchApplyIssue(created, "a template this request creates") - fresh.IsTemplate = true - item := batchApplyCreate("created", fresh) - item.Create.MetadataRefs = map[string]publicops.Ref{"gc.self": {Key: "created"}} - batchApplyMust(t, ctx, fixture, publicops.ApplyBatchRequest{ - Actor: "apply-writer", ForceIDPrefix: true, Items: []publicops.ApplyItem{item}, - }) - if _, ok := batchApplyMetadataKey(t, ctx, fixture, created, "gc.self"); !ok { - t.Errorf("%s carries no gc.self key: the splice that finishes a template's own create must land", created) - } -} - -// RunBatchApplyCloseItemsAnswerToTheCloseGuards pins that a close item answers -// to the close guards Lifecycle.Close states, under CloseRequest's rules: a -// template refuses forced or not, a pin and another actor's bead refuse unless -// the item's Force is set. A refusal is the item's *ItemError carrying the -// guard's typed error, and the request is all or nothing, so the create item -// ahead of it does not land. -// -// The assignee case is AS-MODIFIED: the holder is set by an update item of the -// same request, so a guard that read the row before the request began — or not -// at all — closes it. -func RunBatchApplyCloseItemsAnswerToTheCloseGuards(t *testing.T, ctx context.Context, fixture BatchApplyFixture) { - t.Helper() - template := fixture.IssuePrefix + "-closeguard-template" - pinned := fixture.IssuePrefix + "-closeguard-pinned" - held := fixture.IssuePrefix + "-closeguard-held" - bystander := fixture.IssuePrefix + "-closeguard-bystander" - for _, issue := range []*types.Issue{ - {ID: template, IsTemplate: true}, - {ID: pinned, Pinned: true}, - {ID: held}, - } { - issue.Title, issue.Status, issue.Priority, issue.IssueType = issue.ID, types.StatusOpen, 2, types.TypeTask - if err := fixture.CreateIssue(ctx, issue, "seed"); err != nil { - t.Fatalf("seed %s: %v", issue.ID, err) - } - } - - plan := func(target string, force bool, extra ...publicops.ApplyItem) publicops.ApplyBatchRequest { - closeItem := batchApplyClose(publicops.Ref{ID: target}) - closeItem.Close.Force = force - items := []publicops.ApplyItem{batchApplyCreate("bystander", batchApplyIssue(bystander, "must not land beside a refusal"))} - items = append(items, extra...) - return publicops.ApplyBatchRequest{Actor: "apply-closer", ForceIDPrefix: true, Items: append(items, closeItem)} - } - refused := func(label string, request publicops.ApplyBatchRequest, check func(error)) { - t.Helper() - _, err := fixture.BatchApplier.ApplyBatch(ctx, request) - var itemErr *publicops.ItemError - if !errors.As(err, &itemErr) { - t.Fatalf("%s: error = %v, want an *ItemError", label, err) - } - if itemErr.Kind != publicops.ItemClose || itemErr.Index != len(request.Items)-1 { - t.Errorf("%s: ItemError = %#v, want the close item at index %d", label, itemErr, len(request.Items)-1) - } - check(err) - assertBatchApplyRowCount(t, ctx, fixture, "issues", bystander, 0) - } - - for _, force := range []bool{false, true} { - refused("template close item", plan(template, force), func(err error) { - var refusal *publicops.TemplateReadOnlyError - if !errors.As(err, &refusal) || refusal.IssueID != template { - t.Errorf("template close item (force=%v) = %v, want *TemplateReadOnlyError naming %s", force, err, template) - } - }) - } - refused("pinned close item", plan(pinned, false), func(err error) { - var refusal *publicops.PinnedError - if !errors.As(err, &refusal) || refusal.IssueID != pinned { - t.Errorf("pinned close item = %v, want *PinnedError naming %s", err, pinned) - } - }) - assign := batchApplyUpdate(publicops.Ref{ID: held}, publicops.IssuePatch{Assignee: publicops.Field[string]{Set: true, Value: "holder"}}) - refused("held close item", plan(held, false, assign), func(err error) { - var refusal *publicops.CloseNotAssigneeError - if !errors.As(err, &refusal) { - t.Errorf("close item on a bead an earlier item assigned away = %v, want *CloseNotAssigneeError", err) - return - } - if refusal.IssueID != held || refusal.Assignee != "holder" || refusal.Actor != "apply-closer" { - t.Errorf("assignee refusal = %#v, want IssueID %q, Assignee holder, Actor apply-closer", refusal, held) - } - }) - for _, id := range []string{template, pinned, held} { - if got := batchApplyColumn(t, ctx, fixture, "status", id); got != string(types.StatusOpen) { - t.Errorf("%s status after the refusals = %q, want it still open", id, got) - } - } - - // The item's Force waives the pin and the assignee fence. - forcedPin := plan(pinned, true) - forcedPin.Items = forcedPin.Items[1:] - batchApplyMust(t, ctx, fixture, forcedPin) - forcedHeld := plan(held, true, assign) - forcedHeld.Items = forcedHeld.Items[1:] - batchApplyMust(t, ctx, fixture, forcedHeld) - for _, id := range []string{pinned, held} { - if got := batchApplyColumn(t, ctx, fixture, "status", id); got != string(types.StatusClosed) { - t.Errorf("%s status under the item's Force = %q, want %q", id, got, types.StatusClosed) - } - } -} - -// RunBatchApplyRefusesADottedChildGatedOnItsOwnParent pins the dotted-id -// hierarchy rule (publicops.CheckDottedChildDependency) on a dep_add item: an -// edge from a dotted-id child to its own ancestor is refused with an -// *ItemError naming the edge, and the whole request rolls back. The rule is -// applied to the RESOLVED ids, so a key bound to a dotted child this request -// creates — the `bd create --graph` shape — is refused like a stored id. -// -// POSITIVE HALF: the parent-child edge to the immediate dotted parent lands. -// -// WITH THE HIERARCHY STORED: once that edge exists (the shape `bd create -// --parent` leaves), a blocking edge on the same pair is still this validation -// refusal, not the hierarchy conflict the stored edge would raise (which does -// not match ErrValidation) — the rule reads only the resolved ids, so it -// answers first — and the stored edge survives. -func RunBatchApplyRefusesADottedChildGatedOnItsOwnParent(t *testing.T, ctx context.Context, fixture BatchApplyFixture) { - t.Helper() - parent := fixture.IssuePrefix + "-dotted" - child := parent + ".1" - minted := parent + ".2" - batchApplySeedIssue(t, ctx, fixture, parent, types.StatusOpen) - batchApplySeedIssue(t, ctx, fixture, child, types.StatusOpen) - - for _, refused := range []struct { - name string - items []publicops.ApplyItem - index int - }{ - {"a stored child blocking on its parent", []publicops.ApplyItem{ - batchApplyDepAdd(publicops.Ref{ID: child}, publicops.Ref{ID: parent}, publicops.DepBlocks, ""), - }, 0}, - {"a child this request creates blocking on its parent", []publicops.ApplyItem{ - batchApplyCreate("child", batchApplyIssue(minted, "the new child")), - batchApplyDepAdd(publicops.Ref{Key: "child"}, publicops.Ref{ID: parent}, publicops.DepBlocks, ""), - }, 1}, - } { - _, err := fixture.BatchApplier.ApplyBatch(ctx, publicops.ApplyBatchRequest{ - Actor: "apply-writer", - ForceIDPrefix: true, - Items: refused.items, - }) - if !errors.Is(err, publicops.ErrValidation) { - t.Errorf("%s: error = %v, want ErrValidation", refused.name, err) - continue - } - var itemErr *publicops.ItemError - if !errors.As(err, &itemErr) { - t.Errorf("%s: error = %v, want an *ItemError naming the edge item", refused.name, err) - continue - } - if itemErr.Index != refused.index || itemErr.Kind != publicops.ItemDepAdd { - t.Errorf("%s: ItemError = %#v, want Index %d and Kind %q", refused.name, itemErr, refused.index, publicops.ItemDepAdd) - } - } - assertBatchApplyEdgeCount(t, ctx, fixture, child, parent, 0) - assertBatchApplyRowCount(t, ctx, fixture, "issues", minted, 0) - - batchApplyMust(t, ctx, fixture, publicops.ApplyBatchRequest{ - Actor: "apply-writer", - Items: []publicops.ApplyItem{ - batchApplyDepAdd(publicops.Ref{ID: child}, publicops.Ref{ID: parent}, publicops.DepParentChild, ""), - }, - }) - assertBatchApplyEdgeCount(t, ctx, fixture, child, parent, 1) - - _, err := fixture.BatchApplier.ApplyBatch(ctx, publicops.ApplyBatchRequest{ - Actor: "apply-writer", - Items: []publicops.ApplyItem{ - batchApplyDepAdd(publicops.Ref{ID: child}, publicops.Ref{ID: parent}, publicops.DepBlocks, ""), - }, - }) - var itemErr *publicops.ItemError - if !errors.Is(err, publicops.ErrValidation) || !errors.As(err, &itemErr) { - t.Errorf("blocks on a parent whose parent-child edge is stored: error = %v, want an *ItemError matching ErrValidation", err) - } - assertBatchApplyEdgeCount(t, ctx, fixture, child, parent, 1) -} - -// RunBatchApplyAppliesTheDefaultPriority pins CreateItem.DefaultPriority: a -// create item that asks for the default stores publicops.DefaultCreatePriority, -// and one naming priority 0 stores P0. `bd create --graph` sends a node without -// a priority this way, and so does an HTTP batch apply create item without one. -// A create item asking for the default while naming a priority is -// ErrValidation and writes nothing, as it is for Lifecycle.Create. -func RunBatchApplyAppliesTheDefaultPriority(t *testing.T, ctx context.Context, fixture BatchApplyFixture) { - t.Helper() - unset := batchApplyMintedIssue("defaulted") - unset.Priority = 0 - zero := batchApplyMintedIssue("critical") - zero.Priority = 0 - result := batchApplyMust(t, ctx, fixture, publicops.ApplyBatchRequest{ - Actor: "apply-writer", - Items: []publicops.ApplyItem{ - {Kind: publicops.ItemCreate, Create: &publicops.CreateItem{Key: "d", Issue: unset, DefaultPriority: true}}, - {Kind: publicops.ItemCreate, Create: &publicops.CreateItem{Key: "z", Issue: zero}}, - }, - }) - for key, want := range map[string]int{"d": publicops.DefaultCreatePriority, "z": 0} { - id := result.Keys[key] - if id == "" { - t.Fatalf("key %q bound no id: %v", key, result.Keys) - } - if got := batchApplyCount(t, ctx, fixture, "SELECT priority FROM issues WHERE id = ?", []any{id}); got != want { - t.Errorf("stored priority of %s (%s) = %d, want %d", key, id, got, want) - } - } - - conflict := fixture.IssuePrefix + "-prio-conflict" - clash := batchApplyIssue(conflict, conflict) - clash.Priority = 3 - _, err := fixture.BatchApplier.ApplyBatch(ctx, publicops.ApplyBatchRequest{ - Actor: "apply-writer", - ForceIDPrefix: true, - Items: []publicops.ApplyItem{ - {Kind: publicops.ItemCreate, Create: &publicops.CreateItem{Key: "c", Issue: clash, DefaultPriority: true}}, - }, - }) - if !errors.Is(err, publicops.ErrValidation) { - t.Fatalf("DefaultPriority with priority 3: err = %v, want ErrValidation", err) - } - assertBatchApplyRowCount(t, ctx, fixture, "issues", conflict, 0) -} diff --git a/backend/conformance/batch_apply_gate_contract.go b/backend/conformance/batch_apply_gate_contract.go deleted file mode 100644 index c38689461..000000000 --- a/backend/conformance/batch_apply_gate_contract.go +++ /dev/null @@ -1,145 +0,0 @@ -package conformance - -import ( - "context" - "encoding/json" - "errors" - "slices" - "testing" - "time" - - "github.com/steveyegge/beads/internal/types" - publicops "github.com/steveyegge/beads/issueops" -) - -// The BatchApplier half of the gate-member clauses; see -// lifecycle_create_gate_contract.go. - -// RunBatchApplyCarriesTheGateMembers pins the gate members through -// BatchApplier: a create item stores all four, an update item replaces the -// waiter list of an existing row whole, a clear through an update item stores -// the empty list `[]`, and a create or update item past a bound refuses the -// WHOLE request as ErrValidation naming its member (on its *ItemError for the -// update item), so the earlier item rolls back with it. -func RunBatchApplyCarriesTheGateMembers(t *testing.T, ctx context.Context, fixture BatchApplyFixture) { - t.Helper() - - gate := fixture.IssuePrefix + "-bag-gate" - existing := fixture.IssuePrefix + "-bag-existing" - batchApplySeedIssue(t, ctx, fixture, existing, types.StatusOpen) - - issue := batchApplyIssue(gate, "a gate made by a plan") - issue.IssueType = types.TypeGate - issue.AwaitType = "timer" - issue.AwaitID = "await-plan" - issue.Timeout = 2 * time.Hour - issue.Waiters = []string{"a", "a", "b"} - batchApplyMust(t, ctx, fixture, publicops.ApplyBatchRequest{ - Actor: "apply-writer", - ForceIDPrefix: true, - Items: []publicops.ApplyItem{ - batchApplyCreate("gate", issue), - batchApplyUpdate(publicops.Ref{ID: existing}, publicops.IssuePatch{ - AwaitID: publicops.Field[string]{Set: true, Value: "bead:" + gate}, - Waiters: publicops.Field[[]string]{Set: true, Value: []string{"x", "y"}}, - }), - }, - }) - assertBatchApplyGateColumns(t, ctx, fixture, gate, "timer", "await-plan", 2*time.Hour, []string{"a", "a", "b"}) - assertBatchApplyGateColumns(t, ctx, fixture, existing, "", "bead:"+gate, 0, []string{"x", "y"}) - - // A clear through an update item stores the empty JSON list, not NULL and - // not the JSON null: the column is read raw here because every reader - // above treats the three alike. - batchApplyMust(t, ctx, fixture, publicops.ApplyBatchRequest{ - Actor: "apply-writer", - Items: []publicops.ApplyItem{ - batchApplyUpdate(publicops.Ref{ID: existing}, publicops.IssuePatch{ - AwaitID: publicops.Field[string]{Set: true}, - Waiters: publicops.Field[[]string]{Set: true}, - }), - }, - }) - assertBatchApplyGateColumns(t, ctx, fixture, existing, "", "", 0, nil) - var clearedWaiters string - if err := fixture.QueryScalar(ctx, "SELECT COALESCE(waiters, '') FROM issues WHERE id = ?", []any{existing}, &clearedWaiters); err != nil { - t.Fatalf("read the waiters column of %s: %v", existing, err) - } - if clearedWaiters != "[]" { - t.Errorf("%s waiters column after a clear = %q, want the stored empty list %q", existing, clearedWaiters, "[]") - } - - first := fixture.IssuePrefix + "-bag-rolledback" - refused := batchApplyIssue(fixture.IssuePrefix+"-bag-refused", "past the bound") - refused.AwaitType = gateFieldAtBoundAwaitType + "x" - _, err := fixture.BatchApplier.ApplyBatch(ctx, publicops.ApplyBatchRequest{ - Actor: "apply-writer", - ForceIDPrefix: true, - Items: []publicops.ApplyItem{ - batchApplyCreate("first", batchApplyIssue(first, "rolls back with the refusal")), - batchApplyCreate("refused", refused), - }, - }) - assertGateFieldError(t, err, "a create item with await_type past its bound", publicops.GateFieldAwaitType) - assertBatchApplyRowCount(t, ctx, fixture, "issues", first, 0) - - // An update item past a bound refuses the same way, named on its own - // item: the earlier create rolls back, and the target keeps its columns. - firstOfUpdate := fixture.IssuePrefix + "-bag-rolledback-upd" - _, err = fixture.BatchApplier.ApplyBatch(ctx, publicops.ApplyBatchRequest{ - Actor: "apply-writer", - ForceIDPrefix: true, - Items: []publicops.ApplyItem{ - batchApplyCreate("first", batchApplyIssue(firstOfUpdate, "rolls back with the refusal")), - batchApplyUpdate(publicops.Ref{ID: gate}, publicops.IssuePatch{ - Title: publicops.Field[string]{Set: true, Value: "must not land"}, - AwaitID: publicops.Field[string]{Set: true, Value: gateFieldAtBoundAwaitID + "x"}, - }), - }, - }) - assertGateFieldError(t, err, "an update item with await_id past its bound", publicops.GateFieldAwaitID) - var itemErr *publicops.ItemError - if !errors.As(err, &itemErr) { - t.Fatalf("an update item with await_id past its bound: error = %v, want an *ItemError naming the item", err) - } - if itemErr.Index != 1 || itemErr.Kind != publicops.ItemUpdate { - t.Errorf("ItemError = %#v, want Index 1 and Kind %q", itemErr, publicops.ItemUpdate) - } - assertBatchApplyRowCount(t, ctx, fixture, "issues", firstOfUpdate, 0) - assertBatchApplyGateColumns(t, ctx, fixture, gate, "timer", "await-plan", 2*time.Hour, []string{"a", "a", "b"}) - if got := batchApplyColumn(t, ctx, fixture, "title", gate); got != "a gate made by a plan" { - t.Errorf("%s title = %q, want the created title: a refused item writes nothing", gate, got) - } -} - -// assertBatchApplyGateColumns reads the four gate columns of one durable row -// raw, so neither side of the comparison comes from the role. -func assertBatchApplyGateColumns(t *testing.T, ctx context.Context, fixture BatchApplyFixture, id, awaitType, awaitID string, timeout time.Duration, waiters []string) { - t.Helper() - var gotType, gotID, gotWaiters string - var gotTimeout int64 - if err := fixture.QueryScalar(ctx, - "SELECT COALESCE(await_type, ''), COALESCE(await_id, ''), COALESCE(timeout_ns, 0), COALESCE(waiters, '') FROM issues WHERE id = ?", - []any{id}, &gotType, &gotID, &gotTimeout, &gotWaiters); err != nil { - t.Fatalf("read the gate columns of %s: %v", id, err) - } - if gotType != awaitType || gotID != awaitID || time.Duration(gotTimeout) != timeout { - t.Errorf("%s gate columns = (%q, %q, %s), want (%q, %q, %s)", id, gotType, gotID, time.Duration(gotTimeout), awaitType, awaitID, timeout) - } - if stored := parseGateWaiters(gotWaiters); !slices.Equal(stored, waiters) { - t.Errorf("%s waiters column = %q (%q), want %q", id, gotWaiters, stored, waiters) - } -} - -// parseGateWaiters reads the waiters TEXT column's JSON array, treating the -// empty string, `null` and `[]` as the one empty list they all mean. -func parseGateWaiters(raw string) []string { - if raw == "" || raw == "null" || raw == "[]" { - return nil - } - var out []string - if err := json.Unmarshal([]byte(raw), &out); err != nil { - return []string{""} - } - return out -} diff --git a/backend/conformance/batch_closer_contract.go b/backend/conformance/batch_closer_contract.go index a62b7a667..fde364126 100644 --- a/backend/conformance/batch_closer_contract.go +++ b/backend/conformance/batch_closer_contract.go @@ -39,10 +39,9 @@ import ( // still reports OpenChildren (batchcloser.go:81-86); a duplicated id is one // of those at its own index, and the first occurrence's reason is what the // row keeps (batchcloser.go:28-33). -// - Force is request-wide and bypasses blocker and open-child policy, the -// pin and the assignee fence, never the template guard; the per-item -// precondition its clause also names is a category the request type keeps -// EMPTY (batchcloser.go:43-55). +// - Force is request-wide and bypasses only blocker and open-child policy; +// the per-item precondition its clause also names is a category the request +// type keeps EMPTY (batchcloser.go:43-53). // - ClaimNext runs after the closes, inside the same transaction, and only // when at least one item closed (batchcloser.go:55-70, 100-103, 143-146). // - LANDED means CHANGED: an all-idempotent batch earns no claim and records @@ -1398,96 +1397,3 @@ func requireBatchCloserWisps(t *testing.T, fixture BatchCloserFixture) { t.Skipf("fixture has no CreateWisp: this backend cannot seed the ephemeral plane, so the ephemeral rules are UNPINNED here") } } - -// RunBatchCloserItemsAnswerToTheCloseGuards pins that every item of a batch -// answers to the close guards Lifecycle.Close states — template, pin, assignee -// — as a PER-ITEM refusal carrying the same typed error, while the survivors -// still commit; and that the request-wide Force waives the pin and the -// assignee fence and never the template. -// -// The guarded items sit between two plain ones, so an implementation that -// aborted the batch on a guard, or skipped the guard for items after the -// first, fails here rather than in `bd close a b c`. -func RunBatchCloserItemsAnswerToTheCloseGuards(t *testing.T, ctx context.Context, fixture BatchCloserFixture) { - t.Helper() - first := fixture.IssuePrefix + "-guard-first" - template := fixture.IssuePrefix + "-guard-template" - pinned := fixture.IssuePrefix + "-guard-pinned" - held := fixture.IssuePrefix + "-guard-held" - last := fixture.IssuePrefix + "-guard-last" - seedBatchCloserIssue(t, ctx, fixture, first) - seedBatchCloserIssue(t, ctx, fixture, last) - for _, issue := range []*types.Issue{ - {ID: template, IsTemplate: true}, - {ID: pinned, Status: types.StatusPinned}, - {ID: held, Assignee: "holder"}, - } { - issue.Title, issue.Priority, issue.IssueType = issue.ID, 2, types.TypeTask - if issue.Status == "" { - issue.Status = types.StatusOpen - } - if err := fixture.CreateIssue(ctx, issue, "seed"); err != nil { - t.Fatalf("seed %s: %v", issue.ID, err) - } - } - - result, err := fixture.Closer.CloseBatch(ctx, publicops.CloseBatchRequest{ - Actor: "closer", - Items: []publicops.BatchCloseItem{{IssueID: first}, {IssueID: template}, {IssueID: pinned}, {IssueID: held}, {IssueID: last}}, - }) - if err != nil { - t.Fatalf("CloseBatch over guarded items: %v, want per-item refusals", err) - } - if len(result.Outcomes) != 5 { - t.Fatalf("outcomes = %d, want 5", len(result.Outcomes)) - } - for _, i := range []int{0, 4} { - if result.Outcomes[i].Err != nil || !result.Outcomes[i].Changed { - t.Errorf("outcome %d (%s) = %+v, want the plain item to land", i, result.Outcomes[i].IssueID, result.Outcomes[i]) - } - } - var templateRefusal *publicops.TemplateReadOnlyError - if !errors.As(result.Outcomes[1].Err, &templateRefusal) || templateRefusal.IssueID != template { - t.Errorf("template item = %v, want *TemplateReadOnlyError naming %s", result.Outcomes[1].Err, template) - } - var pinRefusal *publicops.PinnedError - if !errors.As(result.Outcomes[2].Err, &pinRefusal) || pinRefusal.IssueID != pinned { - t.Errorf("pinned item = %v, want *PinnedError naming %s", result.Outcomes[2].Err, pinned) - } - var assigneeRefusal *publicops.CloseNotAssigneeError - if !errors.As(result.Outcomes[3].Err, &assigneeRefusal) { - t.Errorf("held item = %v, want *CloseNotAssigneeError", result.Outcomes[3].Err) - } else if assigneeRefusal.IssueID != held || assigneeRefusal.Assignee != "holder" || assigneeRefusal.Actor != "closer" { - t.Errorf("held item refusal = %#v, want IssueID %q, Assignee holder, Actor closer", assigneeRefusal, held) - } - for _, i := range []int{1, 2, 3} { - if result.Outcomes[i].Issue != nil { - t.Errorf("refused outcome %d carries an Issue snapshot, want nil beside Err", i) - } - } - assertBatchCloserStatus(t, ctx, fixture, first, types.StatusClosed) - assertBatchCloserStatus(t, ctx, fixture, last, types.StatusClosed) - assertBatchCloserStatus(t, ctx, fixture, template, types.StatusOpen) - assertBatchCloserStatus(t, ctx, fixture, pinned, types.StatusPinned) - assertBatchCloserStatus(t, ctx, fixture, held, types.StatusOpen) - - forced, err := fixture.Closer.CloseBatch(ctx, publicops.CloseBatchRequest{ - Actor: "closer", - Items: []publicops.BatchCloseItem{{IssueID: template}, {IssueID: pinned}, {IssueID: held}}, - Force: true, - }) - if err != nil { - t.Fatalf("forced CloseBatch over guarded items: %v", err) - } - if !errors.Is(forced.Outcomes[0].Err, publicops.ErrTemplateReadOnly) { - t.Errorf("forced template item = %v, want ErrTemplateReadOnly: force never waives read-only", forced.Outcomes[0].Err) - } - for _, i := range []int{1, 2} { - if forced.Outcomes[i].Err != nil || !forced.Outcomes[i].Changed { - t.Errorf("forced outcome %d (%s) = %+v, want force to bypass the guard", i, forced.Outcomes[i].IssueID, forced.Outcomes[i]) - } - } - assertBatchCloserStatus(t, ctx, fixture, template, types.StatusOpen) - assertBatchCloserStatus(t, ctx, fixture, pinned, types.StatusClosed) - assertBatchCloserStatus(t, ctx, fixture, held, types.StatusClosed) -} diff --git a/backend/conformance/batch_creator_contract.go b/backend/conformance/batch_creator_contract.go index 1ff4852f4..0b48334f0 100644 --- a/backend/conformance/batch_creator_contract.go +++ b/backend/conformance/batch_creator_contract.go @@ -97,14 +97,6 @@ func RunBatchCreatorCreatesEveryItemAsOneAct(t *testing.T, ctx context.Context, } seen[issue.ID] = true assertBatchCreatorRowCount(t, ctx, fixture, "issues", issue.ID, 1) - // No item names a creator, so each defaults to the batch's actor. - var createdBy string - if err := fixture.QueryScalar(ctx, "SELECT COALESCE(created_by, '') FROM issues WHERE id = ?", []any{issue.ID}, &createdBy); err != nil { - t.Fatalf("read created_by for %s: %v", issue.ID, err) - } - if createdBy != request.Actor || issue.CreatedBy != request.Actor { - t.Errorf("item %d created_by = %q (result %q), want the request's actor %q", i, createdBy, issue.CreatedBy, request.Actor) - } } if labels := result.Issues[0].Labels; len(labels) != 1 || labels[0] != "alpha" { t.Errorf("result issue 0 labels = %v, want [alpha]: the snapshot is promised hydrated with labels", labels) @@ -697,63 +689,3 @@ func batchCreatorHistoryCount(t *testing.T, ctx context.Context, fixture BatchCr } return entries } - -// RunBatchCreatorAppliesTheDefaultPriority pins BatchCreateItem.DefaultPriority: -// an item that asks for the default stores publicops.DefaultCreatePriority, and -// an item naming priority 0 stores P0 in the same batch. The default is -// Lifecycle.Create's, applied in the shared preparation, so an HTTP -// batchCreate without `priority` and a `bd create --file` template without one -// land the same row. An item asking for the default while naming a priority is -// ErrValidation and writes nothing, as it is for Lifecycle.Create. -func RunBatchCreatorAppliesTheDefaultPriority(t *testing.T, ctx context.Context, fixture BatchCreatorFixture) { - t.Helper() - // Minted ids and an unset status, so the case asks nothing of the items - // but their priority — the http wire's batch item has no id or status slot. - unset := &types.Issue{Title: fixture.IssuePrefix + " defaulted", IssueType: types.TypeTask} - zero := &types.Issue{Title: fixture.IssuePrefix + " critical", IssueType: types.TypeTask} - - result, err := fixture.BatchCreator.CreateBatch(ctx, publicops.CreateBatchRequest{ - Actor: "batch-writer", - Items: []publicops.BatchCreateItem{ - {Issue: unset, DefaultPriority: true}, - {Issue: zero}, - }, - }) - if err != nil { - t.Fatalf("CreateBatch: %v", err) - } - if len(result.Issues) != 2 || result.Issues[0] == nil || result.Issues[1] == nil { - t.Fatalf("result = %v, want two created issues", result.Issues) - } - for i, want := range []int{publicops.DefaultCreatePriority, 0} { - created := result.Issues[i] - if created.Priority != want { - t.Errorf("item %d result priority = %d, want %d", i, created.Priority, want) - } - var got int - if err := fixture.QueryScalar(ctx, "SELECT priority FROM issues WHERE id = ?", []any{created.ID}, &got); err != nil { - t.Fatalf("read priority of %s: %v", created.ID, err) - } - if got != want { - t.Errorf("item %d stored priority = %d, want %d", i, got, want) - } - } - - conflict := fixture.IssuePrefix + " conflicting" - _, err = fixture.BatchCreator.CreateBatch(ctx, publicops.CreateBatchRequest{ - Actor: "batch-writer", - Items: []publicops.BatchCreateItem{ - {Issue: &types.Issue{Title: conflict, Priority: 3, IssueType: types.TypeTask}, DefaultPriority: true}, - }, - }) - if !errors.Is(err, storage.ErrValidation) { - t.Fatalf("DefaultPriority with priority 3: err = %v, want ErrValidation", err) - } - var stored int - if err := fixture.QueryScalar(ctx, "SELECT COUNT(*) FROM issues WHERE title = ?", []any{conflict}, &stored); err != nil { - t.Fatalf("count issues titled %q: %v", conflict, err) - } - if stored != 0 { - t.Errorf("issues titled %q after the conflicting priority batch = %d, want 0", conflict, stored) - } -} diff --git a/backend/conformance/batch_getter_contract.go b/backend/conformance/batch_getter_contract.go deleted file mode 100644 index f64dabde6..000000000 --- a/backend/conformance/batch_getter_contract.go +++ /dev/null @@ -1,443 +0,0 @@ -package conformance - -import ( - "context" - "errors" - "fmt" - "testing" - - "github.com/steveyegge/beads/internal/types" - publicops "github.com/steveyegge/beads/issueops" -) - -// This file holds the contract every implementation of publicops.BatchGetter -// must satisfy. Each case asserts what issueops/batchgetter.go PROMISES, cited -// by symbol; a backend that disagrees is parked at its own wiring site with -// skipKnownDivergence so the case still runs on the ones that agree. -// -// THREE LEGS, ONE BODY, GraphCounterFixture's reason: all three reach the same -// storage/issueops.ExecuteGetMany, which itself reaches GetIssuesByIDsInTx for -// the actual read. The two stores wrap it in their own read transaction, and -// the unit-of-work provider reaches it through the domain repository, whose -// runner publishes exactly the DBTX method set that function takes. So a -// three-leg run here is ONE READING plus two engine checks and three wrapper -// checks, and the cases are written for that: they assert the result's shape -// and order rather than message text, so a wrapper that loses the request, -// drops ordering or breaks errors.Is is what a per-leg failure would actually -// be. -// -// EVERY CASE NAMES THE EXACT IDS IT SEEDED, GraphCounterFixture's reason: the -// three fixtures share one database per suite and the two store fixtures share -// it with every other role's cases. -type BatchGetterFixture struct { - // IssuePrefix namespaces the ids each assertion seeds, so several of them - // can share one database. - IssuePrefix string - BatchGetter publicops.BatchGetter - // CreateIssue seeds a durable issue in the issues plane. - CreateIssue func(context.Context, *types.Issue, string) error - // CreateIssues seeds many durable issues in one call. Optional: only - // RunBatchGetterAcceptsExactlyTheCap uses it, to seed its MaxGetManyIDs - // issues in one write instead of MaxGetManyIDs single-row commits (about - // 200s of the embedded suites' race runs); nil falls back to CreateIssue - // per row. The seeded rows are the same either way, and the case asserts - // only on the GetMany read. - CreateIssues func(context.Context, []*types.Issue, string) error - // CreateWisp seeds an ephemeral issue in the wisps plane. It is a separate - // field rather than an Ephemeral flag on CreateIssue because the three - // adapters reach the two planes through different verbs. - CreateWisp func(context.Context, *types.Issue, string) error - // CountHistory reports how many history entries the fixture's branch has. - // A nil hook means "this backend cannot observe history", and the case that - // needs it SKIPS rather than passing quietly. - CountHistory func(context.Context) (int, error) -} - -// RunBatchGetterFindsRequestedIssues pins the found half of GetManyResult: an -// id a GetManyRequest names and the store holds comes back hydrated, with the -// id that resolved it. -func RunBatchGetterFindsRequestedIssues(t *testing.T, ctx context.Context, fixture BatchGetterFixture) { - t.Helper() - first := fixture.IssuePrefix + "-found-a" - second := fixture.IssuePrefix + "-found-b" - seedBatchGetterIssues(t, ctx, fixture, first, second) - - result := getMany(t, ctx, fixture, publicops.GetManyRequest{IDs: []string{first, second}}) - assertBatchGetterIssueIDs(t, result, first, second) - assertBatchGetterMissingIDs(t, result) -} - -// RunBatchGetterReportsMissingIDs pins GetManyResult.Missing: an id naming no -// stored row is reported there, beside whatever else resolved, rather than -// refusing the whole call. The ghost sits BETWEEN two real ids, the same -// placement RunGraphCounterDistinguishesNoEdgesFromNoAnchor uses, so a body -// that short-circuited on the first miss cannot pass by accident. -func RunBatchGetterReportsMissingIDs(t *testing.T, ctx context.Context, fixture BatchGetterFixture) { - t.Helper() - first := fixture.IssuePrefix + "-missing-real1" - ghost := fixture.IssuePrefix + "-missing-ghost" - second := fixture.IssuePrefix + "-missing-real2" - seedBatchGetterIssues(t, ctx, fixture, first, second) - - result := getMany(t, ctx, fixture, publicops.GetManyRequest{IDs: []string{first, ghost, second}}) - assertBatchGetterIssueIDs(t, result, first, second) - assertBatchGetterMissingIDs(t, result, ghost) -} - -// RunBatchGetterCollapsesRepeatedIDs pins GetManyRequest.IDs's repeats clause: -// an id named twice is one entry, at the position of its first mention. The -// repeat is placed AFTER a different id, RunGraphCounterCollapsesRepeatedAnchors' -// reason: a body that de-duplicated by sorting rather than by first mention -// would answer b, a instead of a, b. -func RunBatchGetterCollapsesRepeatedIDs(t *testing.T, ctx context.Context, fixture BatchGetterFixture) { - t.Helper() - first := fixture.IssuePrefix + "-dup-b" - second := fixture.IssuePrefix + "-dup-a" - seedBatchGetterIssues(t, ctx, fixture, first, second) - - result := getMany(t, ctx, fixture, publicops.GetManyRequest{ - IDs: []string{first, second, first, second, first}, - }) - assertBatchGetterIssueIDs(t, result, first, second) -} - -// RunBatchGetterAnswersInRequestOrder pins GetManyResult.Issues' ordering -// clause: the answer comes back in the REQUEST's order, never the storage -// engine's natural order. The ids are seeded in the opposite order from the one -// requested, so a body that forwarded the storage engine's order would fail. -func RunBatchGetterAnswersInRequestOrder(t *testing.T, ctx context.Context, fixture BatchGetterFixture) { - t.Helper() - first := fixture.IssuePrefix + "-order-c" - second := fixture.IssuePrefix + "-order-b" - third := fixture.IssuePrefix + "-order-a" - // Seeded in storage order third, second, first — the reverse of the - // request below — so a body that forwarded insertion order would fail. - seedBatchGetterIssues(t, ctx, fixture, third, second, first) - - result := getMany(t, ctx, fixture, publicops.GetManyRequest{IDs: []string{first, second, third}}) - assertBatchGetterIssueIDs(t, result, first, second, third) -} - -// RunBatchGetterResolvesIDsExactly pins the exactness GetManyRequest.IDs -// documents: a prefix of a real id and a real id padded with whitespace are -// both reported Missing rather than resolved, the same exactness -// RunGraphCounterResolvesIDsExactly pins for EdgeCountRequest.IDs. -func RunBatchGetterResolvesIDsExactly(t *testing.T, ctx context.Context, fixture BatchGetterFixture) { - t.Helper() - real := fixture.IssuePrefix + "-exact-real" - seedBatchGetterIssues(t, ctx, fixture, real) - - prefix := real[:len(real)-2] - spaced := " " + real + " " - result := getMany(t, ctx, fixture, publicops.GetManyRequest{IDs: []string{real, prefix, spaced}}) - assertBatchGetterIssueIDs(t, result, real) - assertBatchGetterMissingIDs(t, result, prefix, spaced) -} - -// RunBatchGetterAnswersAnEmptyRequest pins GetManyRequest.IDs's empty-slice -// clause: no ids is not an error, it is an answer with no issues — and both -// slices are never nil, so a front door that marshals them emits [] rather -// than null. -func RunBatchGetterAnswersAnEmptyRequest(t *testing.T, ctx context.Context, fixture BatchGetterFixture) { - t.Helper() - result, err := fixture.BatchGetter.GetMany(ctx, publicops.GetManyRequest{}) - if err != nil { - t.Fatalf("GetMany with no ids = %v, want an empty answer", err) - } - if len(result.Issues) != 0 { - t.Fatalf("GetMany with no ids returned %d issues, want none", len(result.Issues)) - } - if result.Issues == nil { - t.Error("Issues is nil; the contract promises it is never nil for a successful call") - } - if result.Missing == nil { - t.Error("Missing is nil; the contract promises it is never nil for a successful call") - } -} - -// RunBatchGetterRefusesAnUnusableRequest pins the request vocabulary -// ValidateGetManyRequest applies: an empty-string id is refused with -// ErrValidation. -func RunBatchGetterRefusesAnUnusableRequest(t *testing.T, ctx context.Context, fixture BatchGetterFixture) { - t.Helper() - real := fixture.IssuePrefix + "-refuse-real" - seedBatchGetterIssues(t, ctx, fixture, real) - - _, err := fixture.BatchGetter.GetMany(ctx, publicops.GetManyRequest{IDs: []string{real, ""}}) - if !errors.Is(err, publicops.ErrValidation) { - t.Errorf("GetMany with an empty id beside a real one = %v, want ErrValidation", err) - } -} - -// RunBatchGetterRefusesOverTheCap pins MaxGetManyIDs: a request naming more -// ids than the cap is refused with a *publicops.TooManyIDsError that wraps -// ErrValidation, counted on the request as sent, BEFORE deduplication — the -// request below repeats one id past the cap, so a body that checked the -// deduplicated count would wrongly let it through. -func RunBatchGetterRefusesOverTheCap(t *testing.T, ctx context.Context, fixture BatchGetterFixture) { - t.Helper() - real := fixture.IssuePrefix + "-cap-real" - seedBatchGetterIssues(t, ctx, fixture, real) - - ids := make([]string, publicops.MaxGetManyIDs+1) - for i := range ids { - ids[i] = real - } - _, err := fixture.BatchGetter.GetMany(ctx, publicops.GetManyRequest{IDs: ids}) - var tooMany *publicops.TooManyIDsError - if !errors.As(err, &tooMany) { - t.Fatalf("GetMany over the cap = %v, want *publicops.TooManyIDsError", err) - } - if tooMany.Requested != len(ids) { - t.Errorf("TooManyIDsError.Requested = %d, want %d (the count before deduplication)", tooMany.Requested, len(ids)) - } - if tooMany.Cap != publicops.MaxGetManyIDs { - t.Errorf("TooManyIDsError.Cap = %d, want %d", tooMany.Cap, publicops.MaxGetManyIDs) - } - if !errors.Is(err, publicops.ErrValidation) { - t.Errorf("GetMany over the cap = %v, want it to also match ErrValidation", err) - } -} - -// RunBatchGetterAcceptsExactlyTheCap pins the boundary MaxGetManyIDs draws: -// a request naming EXACTLY the cap's worth of distinct ids is accepted and -// answered in full, not refused. RunBatchGetterRefusesOverTheCap pins the -// cap+1 side of this line; without this case a `>` to `>=` mutation on the -// comparison would still refuse a one-over request (cap+1 is still > cap-1) -// and pass that case while silently refusing every legitimate cap-sized -// request. -func RunBatchGetterAcceptsExactlyTheCap(t *testing.T, ctx context.Context, fixture BatchGetterFixture) { - t.Helper() - ids := make([]string, publicops.MaxGetManyIDs) - for i := range ids { - ids[i] = fmt.Sprintf("%s-atcap-%d", fixture.IssuePrefix, i) - } - seedBatchGetterIssuesInBulk(t, ctx, fixture, ids...) - - result := getMany(t, ctx, fixture, publicops.GetManyRequest{IDs: ids}) - if len(result.Issues) != publicops.MaxGetManyIDs { - t.Fatalf("GetMany with exactly MaxGetManyIDs (%d) ids returned %d issues, want %d", - publicops.MaxGetManyIDs, len(result.Issues), publicops.MaxGetManyIDs) - } - assertBatchGetterMissingIDs(t, result) -} - -// RunBatchGetterHydratesLabels pins that a resolved issue's Labels come back -// populated: hydration for this role is LABELS ONLY (no dependencies, -// dependents or comments — issueops.GetManyResult.Issues documents the -// scope), so Labels is the one piece of relational data a regression here -// could silently drop without any other case noticing. -func RunBatchGetterHydratesLabels(t *testing.T, ctx context.Context, fixture BatchGetterFixture) { - t.Helper() - id := fixture.IssuePrefix + "-labels-a" - seed := batchGetterSeed(id, false) - seed.Labels = []string{"alpha", "beta"} - if err := fixture.CreateIssue(ctx, seed, "batch-getter-seed"); err != nil { - t.Fatalf("seed issue %s: %v", id, err) - } - - result := getMany(t, ctx, fixture, publicops.GetManyRequest{IDs: []string{id}}) - assertBatchGetterIssueIDs(t, result, id) - got := result.Issues[0].Labels - want := []string{"alpha", "beta"} - if len(got) != len(want) { - t.Fatalf("GetMany(%s).Labels = %v, want %v", id, got, want) - } - seen := make(map[string]bool, len(got)) - for _, l := range got { - seen[l] = true - } - for _, l := range want { - if !seen[l] { - t.Errorf("GetMany(%s).Labels = %v, want it to include %q", id, got, l) - } - } -} - -// RunBatchGetterLeavesTheRequestAlone pins the no-mutation clause on -// BatchGetter: IDs is the one member a body could write through to the caller -// — de-duplication is exactly the step that would — and the contract says a -// body de-duplicates into its own copy. -func RunBatchGetterLeavesTheRequestAlone(t *testing.T, ctx context.Context, fixture BatchGetterFixture) { - t.Helper() - first := fixture.IssuePrefix + "-immutable-a" - second := fixture.IssuePrefix + "-immutable-b" - seedBatchGetterIssues(t, ctx, fixture, first, second) - - ids := []string{first, first, second} - getMany(t, ctx, fixture, publicops.GetManyRequest{IDs: ids}) - - if len(ids) != 3 || ids[0] != first || ids[1] != first || ids[2] != second { - t.Errorf("the request's IDs slice is now %v; the contract says a body de-duplicates into its own copy", ids) - } -} - -// RunBatchGetterSharesOneReadStructurally pins the STRUCTURAL half of the -// one-snapshot clause on BatchGetter.GetMany: the existence check and the -// hydration are one call answering one found id and one missing id together -// against a STATIC fixture, which is consistent with — but does not by -// itself PROVE — the two sharing one transaction under a concurrent writer. -// A body that split the probe from the hydration into two round trips could -// still pass this case against a fixture nothing else is mutating, so it -// checks both halves of one GetManyResult instead of asserting on two -// separate calls the way RunBatchGetterFindsRequestedIssues and -// RunBatchGetterReportsMissingIDs do, but it is NOT a concurrency test: no -// writer races the read here. -// -// Renamed from RunBatchGetterReadsOneSnapshot, which overclaimed an -// atomicity guarantee this case alone does not empirically demonstrate. A -// real interleaving test — a concurrent writer racing GetMany's transaction -// so the case can show a found id and a racing create or delete resolve -// consistently with ONE snapshot boundary — needs a synchronization hook -// (e.g. "pause this leg's read transaction after its query, before it -// returns, so a second goroutine can write and commit") that roleFixtureKit -// does not expose today; that kit is FROZEN (see role_fixture_kit_test.go) -// and a role slice does not edit it. Adding the hook is tracked as follow-up -// work rather than done here, so this case stays honestly structural. -func RunBatchGetterSharesOneReadStructurally(t *testing.T, ctx context.Context, fixture BatchGetterFixture) { - t.Helper() - present := fixture.IssuePrefix + "-snapshot-present" - ghost := fixture.IssuePrefix + "-snapshot-ghost" - seedBatchGetterIssues(t, ctx, fixture, present) - - result := getMany(t, ctx, fixture, publicops.GetManyRequest{IDs: []string{present, ghost}}) - if len(result.Issues) != 1 || len(result.Missing) != 1 { - t.Fatalf("GetMany([%s, %s]) = %d issues, %d missing; want exactly one of each from the same read", - present, ghost, len(result.Issues), len(result.Missing)) - } - assertBatchGetterIssueIDs(t, result, present) - assertBatchGetterMissingIDs(t, result, ghost) -} - -// RunBatchGetterCrossesBothPlanes pins that GetMany resolves ids regardless of -// which dependency plane stores them: a durable issue and an ephemeral wisp -// named in the same request both come back hydrated. -func RunBatchGetterCrossesBothPlanes(t *testing.T, ctx context.Context, fixture BatchGetterFixture) { - t.Helper() - issue := fixture.IssuePrefix + "-plane-issue" - wisp := fixture.IssuePrefix + "-plane-wisp" - seedBatchGetterIssues(t, ctx, fixture, issue) - seedBatchGetterWisp(t, ctx, fixture, wisp) - - result := getMany(t, ctx, fixture, publicops.GetManyRequest{IDs: []string{issue, wisp}}) - assertBatchGetterIssueIDs(t, result, issue, wisp) -} - -// RunBatchGetterWritesNothing pins BatchGetter's read clause: a GetMany -// records no history entry. The delta is taken around the calls rather than as -// an absolute count, RunGraphCounterWritesNothing's reason: the seeds above it -// are versioned writes of their own. -func RunBatchGetterWritesNothing(t *testing.T, ctx context.Context, fixture BatchGetterFixture) { - t.Helper() - if fixture.CountHistory == nil { - t.Skip("this backend cannot observe history, so the writes-nothing clause cannot be checked here") - } - real := fixture.IssuePrefix + "-quiet-real" - ghost := fixture.IssuePrefix + "-quiet-ghost" - seedBatchGetterIssues(t, ctx, fixture, real) - - before, err := fixture.CountHistory(ctx) - if err != nil { - t.Fatalf("CountHistory before: %v", err) - } - getMany(t, ctx, fixture, publicops.GetManyRequest{IDs: []string{real, ghost}}) - // A refusal changes nothing either, so the same delta covers both. - _, _ = fixture.BatchGetter.GetMany(ctx, publicops.GetManyRequest{IDs: []string{""}}) - after, err := fixture.CountHistory(ctx) - if err != nil { - t.Fatalf("CountHistory after: %v", err) - } - if after != before { - t.Fatalf("history entries went %d -> %d across two GetMany calls, want no change", before, after) - } -} - -func getMany(t *testing.T, ctx context.Context, fixture BatchGetterFixture, request publicops.GetManyRequest) publicops.GetManyResult { - t.Helper() - result, err := fixture.BatchGetter.GetMany(ctx, request) - if err != nil { - t.Fatalf("GetMany(%v): %v", request.IDs, err) - } - return result -} - -func seedBatchGetterIssues(t *testing.T, ctx context.Context, fixture BatchGetterFixture, ids ...string) { - t.Helper() - for _, id := range ids { - if err := fixture.CreateIssue(ctx, batchGetterSeed(id, false), "batch-getter-seed"); err != nil { - t.Fatalf("seed issue %s: %v", id, err) - } - } -} - -// seedBatchGetterIssuesInBulk seeds the same rows as seedBatchGetterIssues, -// through the fixture's CreateIssues hook when it has one. -func seedBatchGetterIssuesInBulk(t *testing.T, ctx context.Context, fixture BatchGetterFixture, ids ...string) { - t.Helper() - if fixture.CreateIssues == nil { - seedBatchGetterIssues(t, ctx, fixture, ids...) - return - } - issues := make([]*types.Issue, len(ids)) - for i, id := range ids { - issues[i] = batchGetterSeed(id, false) - } - if err := fixture.CreateIssues(ctx, issues, "batch-getter-seed"); err != nil { - t.Fatalf("seed %d issues: %v", len(issues), err) - } -} - -func seedBatchGetterWisp(t *testing.T, ctx context.Context, fixture BatchGetterFixture, id string) { - t.Helper() - if err := fixture.CreateWisp(ctx, batchGetterSeed(id, true), "batch-getter-seed"); err != nil { - t.Fatalf("seed wisp %s: %v", id, err) - } -} - -func batchGetterSeed(id string, ephemeral bool) *types.Issue { - return &types.Issue{ - ID: id, - Title: id, - Status: types.StatusOpen, - Priority: 2, - IssueType: types.TypeTask, - Ephemeral: ephemeral, - } -} - -func batchGetterIssueIDs(result publicops.GetManyResult) []string { - out := make([]string, 0, len(result.Issues)) - for _, issue := range result.Issues { - out = append(out, issue.ID) - } - return out -} - -func assertBatchGetterIssueIDs(t *testing.T, result publicops.GetManyResult, want ...string) { - t.Helper() - got := batchGetterIssueIDs(result) - if len(got) != len(want) { - t.Fatalf("issues = %v, want %v", got, want) - } - for i := range want { - if got[i] != want[i] { - t.Fatalf("issues = %v, want %v", got, want) - } - } -} - -func assertBatchGetterMissingIDs(t *testing.T, result publicops.GetManyResult, want ...string) { - t.Helper() - got := result.Missing - if len(got) != len(want) { - t.Fatalf("missing = %v, want %v", got, want) - } - for i := range want { - if got[i] != want[i] { - t.Fatalf("missing = %v, want %v", got, want) - } - } - if got == nil { - t.Error("Missing is nil; the contract promises it is never nil for a successful call") - } -} diff --git a/backend/conformance/blocked_lister_contract.go b/backend/conformance/blocked_lister_contract.go deleted file mode 100644 index 9313e3e76..000000000 --- a/backend/conformance/blocked_lister_contract.go +++ /dev/null @@ -1,426 +0,0 @@ -package conformance - -import ( - "context" - "fmt" - "reflect" - "slices" - "testing" - "time" - - "github.com/steveyegge/beads/internal/types" - publicops "github.com/steveyegge/beads/issueops" -) - -// This file holds the semantic contract every implementation of -// publicops.BlockedLister must satisfy: what issueops/blockedlister.go -// promises, asserted on every leg (dolt, embedded dolt, unit of work, and the -// served HTTP client) so `bd blocked` answers one listing whichever route it -// takes. -// -// EVERY CASE IS SCOPED BY A LABEL: the blocked set is a property of the whole -// database, not of an id prefix, so a row seeded by one case is inside the -// next one's answer unless each asks through Labels. - -// BlockedListerFixture supplies adapter-specific storage access for the -// blocked-listing assertions. -type BlockedListerFixture struct { - // IssuePrefix namespaces the ids each case seeds, so several cases can - // share one database. - IssuePrefix string - // BlockedLister is the surface under test. - BlockedLister publicops.BlockedLister - // CreateIssue seeds a durable issue in the issues plane. - CreateIssue func(context.Context, *types.Issue, string) error - // CreateWisp seeds an ephemeral issue in the wisps plane. - CreateWisp func(context.Context, *types.Issue, string) error - // AddDependency seeds ONE edge, recomputing the stored blocked flag the - // way every dependency write does. - AddDependency func(context.Context, *types.Dependency, string) error - // MarkBlocked sets the stored is_blocked flag on one durable issue - // directly, without the edge that would justify it: the stale flag `bd - // recompute-blocked` exists to repair. It must fail unless exactly one row - // changed. A nil hook means this leg cannot write the column, and the case - // that needs it skips with that reason. - MarkBlocked func(context.Context, string) error -} - -// RunBlockedLister runs every blocked-listing case as a subtest of t. -func RunBlockedLister(t *testing.T, ctx context.Context, fixture BlockedListerFixture) { - t.Helper() - t.Run("NamesADirectBlocker", func(t *testing.T) { runBlockedNamesADirectBlocker(t, ctx, fixture) }) - t.Run("AttributesAChildToItsBlockedParent", func(t *testing.T) { runBlockedAttributesAChildToItsParent(t, ctx, fixture) }) - t.Run("ClosedBlockerDoesNotBlock", func(t *testing.T) { runBlockedClosedBlockerDoesNotBlock(t, ctx, fixture) }) - t.Run("IncludesTheWispPlane", func(t *testing.T) { runBlockedIncludesTheWispPlane(t, ctx, fixture) }) - t.Run("ExcludesThePinnedStatus", func(t *testing.T) { runBlockedExcludesThePinnedStatus(t, ctx, fixture) }) - t.Run("ParentFilter", func(t *testing.T) { runBlockedParentFilter(t, ctx, fixture) }) - t.Run("LabelFilters", func(t *testing.T) { runBlockedLabelFilters(t, ctx, fixture) }) - t.Run("Order", func(t *testing.T) { runBlockedOrder(t, ctx, fixture) }) - t.Run("NamesWaitsForAndConditionalBlockers", func(t *testing.T) { runBlockedNamesWaitsForAndConditionalBlockers(t, ctx, fixture) }) - t.Run("OmitsAStaleFlagWithNoNameableBlocker", func(t *testing.T) { runBlockedOmitsAStaleFlag(t, ctx, fixture) }) - t.Run("HasNoTypeExclusions", func(t *testing.T) { runBlockedHasNoTypeExclusions(t, ctx, fixture) }) - t.Run("EmptyIsAnEmptySlice", func(t *testing.T) { runBlockedEmptyIsAnEmptySlice(t, ctx, fixture) }) - t.Run("DoesNotMutateTheCallerRequest", func(t *testing.T) { runBlockedDoesNotMutateTheRequest(t, ctx, fixture) }) -} - -// runBlockedNamesADirectBlocker: an issue with an open `blocks` target is -// listed with that target as its one blocker; the target itself is not. -func runBlockedNamesADirectBlocker(t *testing.T, ctx context.Context, f BlockedListerFixture) { - label := f.IssuePrefix + "-bl-direct" - blocked, blocker := blListerID(f, "direct-src"), blListerID(f, "direct-dst") - blListerSeedIssue(t, ctx, f, blListerIssue(blocked, 1, label)) - blListerSeedIssue(t, ctx, f, blListerIssue(blocker, 1, label)) - blListerSeedEdge(t, ctx, f, blocked, blocker, types.DepBlocks) - - items := blListerList(t, ctx, f, publicops.BlockedRequest{Labels: []string{label}}) - blListerAssertRows(t, items, map[string][]string{blocked: {blocker}}) -} - -// runBlockedAttributesAChildToItsParent: a child of a blocked parent, with no -// blocker of its own, is listed with its parent named as the blocker. -func runBlockedAttributesAChildToItsParent(t *testing.T, ctx context.Context, f BlockedListerFixture) { - label := f.IssuePrefix + "-bl-child" - parent, child, blocker := blListerID(f, "child-parent"), blListerID(f, "child-child"), blListerID(f, "child-blocker") - blListerSeedIssue(t, ctx, f, blListerIssue(parent, 1, label)) - blListerSeedIssue(t, ctx, f, blListerIssue(blocker, 1, label+"-other")) - blListerSeedIssue(t, ctx, f, blListerIssue(child, 1, label)) - blListerSeedEdge(t, ctx, f, child, parent, types.DepParentChild) - blListerSeedEdge(t, ctx, f, parent, blocker, types.DepBlocks) - - items := blListerList(t, ctx, f, publicops.BlockedRequest{Labels: []string{label}}) - blListerAssertRows(t, items, map[string][]string{parent: {blocker}, child: {parent}}) -} - -// runBlockedClosedBlockerDoesNotBlock: a `blocks` edge onto a closed target -// names no blocker, so the source is not listed. -func runBlockedClosedBlockerDoesNotBlock(t *testing.T, ctx context.Context, f BlockedListerFixture) { - label := f.IssuePrefix + "-bl-closed" - src, dst := blListerID(f, "closed-src"), blListerID(f, "closed-dst") - blListerSeedIssue(t, ctx, f, blListerIssue(src, 1, label)) - closed := blListerIssue(dst, 1, label) - closed.Status = types.StatusClosed - blListerSeedIssue(t, ctx, f, closed) - blListerSeedEdge(t, ctx, f, src, dst, types.DepBlocks) - - items := blListerList(t, ctx, f, publicops.BlockedRequest{Labels: []string{label}}) - blListerAssertRows(t, items, map[string][]string{}) -} - -// runBlockedIncludesTheWispPlane: a blocked wisp is listed beside the issues. -func runBlockedIncludesTheWispPlane(t *testing.T, ctx context.Context, f BlockedListerFixture) { - label := f.IssuePrefix + "-bl-wisp" - wisp, blocker := blListerID(f, "wisp-src"), blListerID(f, "wisp-dst") - w := blListerIssue(wisp, 1, label) - w.Ephemeral = true - if err := f.CreateWisp(ctx, w, "seed"); err != nil { - t.Fatalf("seed wisp %s: %v", wisp, err) - } - b := blListerIssue(blocker, 1, label) - b.Ephemeral = true - if err := f.CreateWisp(ctx, b, "seed"); err != nil { - t.Fatalf("seed wisp %s: %v", blocker, err) - } - blListerSeedEdge(t, ctx, f, wisp, blocker, types.DepBlocks) - - items := blListerList(t, ctx, f, publicops.BlockedRequest{Labels: []string{label}}) - blListerAssertRows(t, items, map[string][]string{wisp: {blocker}}) -} - -// runBlockedExcludesThePinnedStatus: a row whose status is pinned is never -// listed, whatever blocks it. -func runBlockedExcludesThePinnedStatus(t *testing.T, ctx context.Context, f BlockedListerFixture) { - label := f.IssuePrefix + "-bl-pinned" - pinned, open, blocker := blListerID(f, "pinned-src"), blListerID(f, "pinned-open"), blListerID(f, "pinned-dst") - p := blListerIssue(pinned, 1, label) - p.Status = types.StatusPinned - blListerSeedIssue(t, ctx, f, p) - blListerSeedIssue(t, ctx, f, blListerIssue(open, 1, label)) - blListerSeedIssue(t, ctx, f, blListerIssue(blocker, 1, label+"-other")) - blListerSeedEdge(t, ctx, f, pinned, blocker, types.DepBlocks) - blListerSeedEdge(t, ctx, f, open, blocker, types.DepBlocks) - - items := blListerList(t, ctx, f, publicops.BlockedRequest{Labels: []string{label}}) - blListerAssertRows(t, items, map[string][]string{open: {blocker}}) -} - -// runBlockedParentFilter: ParentID narrows to the parent's direct children. -func runBlockedParentFilter(t *testing.T, ctx context.Context, f BlockedListerFixture) { - label := f.IssuePrefix + "-bl-parent" - parent, kid, stranger, blocker := blListerID(f, "pf-parent"), blListerID(f, "pf-kid"), blListerID(f, "pf-stranger"), blListerID(f, "pf-blocker") - blListerSeedIssue(t, ctx, f, blListerIssue(parent, 1, label+"-other")) - blListerSeedIssue(t, ctx, f, blListerIssue(blocker, 1, label+"-other")) - blListerSeedIssue(t, ctx, f, blListerIssue(kid, 1, label)) - blListerSeedIssue(t, ctx, f, blListerIssue(stranger, 1, label)) - blListerSeedEdge(t, ctx, f, kid, parent, types.DepParentChild) - blListerSeedEdge(t, ctx, f, kid, blocker, types.DepBlocks) - blListerSeedEdge(t, ctx, f, stranger, blocker, types.DepBlocks) - - all := blListerList(t, ctx, f, publicops.BlockedRequest{Labels: []string{label}}) - blListerAssertRows(t, all, map[string][]string{kid: {blocker}, stranger: {blocker}}) - narrowed := blListerList(t, ctx, f, publicops.BlockedRequest{ParentID: parent, Labels: []string{label}}) - blListerAssertRows(t, narrowed, map[string][]string{kid: {blocker}}) -} - -// runBlockedLabelFilters: Labels is AND, LabelsAny is OR, ExcludeLabels -// removes, and every value is trimmed with blanks dropped. -func runBlockedLabelFilters(t *testing.T, ctx context.Context, f BlockedListerFixture) { - base := f.IssuePrefix + "-bl-lab" - red, blue := base+"-red", base+"-blue" - both, onlyRed, onlyBlue, blocker := blListerID(f, "lab-both"), blListerID(f, "lab-red"), blListerID(f, "lab-blue"), blListerID(f, "lab-blocker") - blListerSeedIssue(t, ctx, f, blListerIssue(both, 1, base, red, blue)) - blListerSeedIssue(t, ctx, f, blListerIssue(onlyRed, 1, base, red)) - blListerSeedIssue(t, ctx, f, blListerIssue(onlyBlue, 1, base, blue)) - blListerSeedIssue(t, ctx, f, blListerIssue(blocker, 1, base+"-other")) - for _, id := range []string{both, onlyRed, onlyBlue} { - blListerSeedEdge(t, ctx, f, id, blocker, types.DepBlocks) - } - want := func(ids ...string) map[string][]string { - m := map[string][]string{} - for _, id := range ids { - m[id] = []string{blocker} - } - return m - } - - blListerAssertRows(t, blListerList(t, ctx, f, publicops.BlockedRequest{Labels: []string{" " + red + " ", "", blue}}), want(both)) - blListerAssertRows(t, blListerList(t, ctx, f, publicops.BlockedRequest{Labels: []string{base}, LabelsAny: []string{red, blue}}), want(both, onlyRed, onlyBlue)) - blListerAssertRows(t, blListerList(t, ctx, f, publicops.BlockedRequest{Labels: []string{base}, ExcludeLabels: []string{" " + blue}}), want(onlyRed)) -} - -// runBlockedOrder: priority ascending, then created_at descending, then id. -// -// The created_at values are seeded explicitly, so every key decides at least -// one pair: two rows share a priority and differ in created_at (newest first), -// and five share both priority and created_at (the id breaks the tie). The -// expected order is written out rather than recomputed from the answer. -func runBlockedOrder(t *testing.T, ctx context.Context, f BlockedListerFixture) { - label := f.IssuePrefix + "-bl-order" - blocker := blListerID(f, "order-blocker") - blListerSeedIssue(t, ctx, f, blListerIssue(blocker, 1, label+"-other")) - older := time.Date(2026, 1, 1, 10, 0, 0, 0, time.UTC) - newer := older.Add(time.Hour) - // Two of the five tied rows are wisps. The store body assembles rows - // issues-plane first, each plane in id order, so the ids alone would put - // the tied run in the wrong order (b, d, e, then a, c); only the id - // tiebreak puts it right. - rows := []struct { - name string - priority int - created time.Time - wisp bool - }{ - {"order-p3", 3, newer, false}, - {"order-p0-older", 0, older, false}, - {"order-p0-newer-e", 0, newer, false}, - {"order-p0-newer-c", 0, newer, true}, - {"order-p0-newer-a", 0, newer, true}, - {"order-p0-newer-d", 0, newer, false}, - {"order-p0-newer-b", 0, newer, false}, - {"order-p1", 1, older, false}, - } - for _, row := range rows { - issue := blListerIssue(blListerID(f, row.name), row.priority, label) - issue.CreatedAt = row.created - issue.UpdatedAt = row.created - if row.wisp { - issue.Ephemeral = true - if err := f.CreateWisp(ctx, issue, "seed"); err != nil { - t.Fatalf("seed wisp %s: %v", issue.ID, err) - } - } else { - blListerSeedIssue(t, ctx, f, issue) - } - blListerSeedEdge(t, ctx, f, issue.ID, blocker, types.DepBlocks) - } - want := []string{ - blListerID(f, "order-p0-newer-a"), - blListerID(f, "order-p0-newer-b"), - blListerID(f, "order-p0-newer-c"), - blListerID(f, "order-p0-newer-d"), - blListerID(f, "order-p0-newer-e"), - blListerID(f, "order-p0-older"), - blListerID(f, "order-p1"), - blListerID(f, "order-p3"), - } - got := blListerIDs(blListerList(t, ctx, f, publicops.BlockedRequest{Labels: []string{label}})) - if !slices.Equal(got, want) { - t.Errorf("listing order = %v, want %v (priority ascending, created_at descending, id)", got, want) - } -} - -// runBlockedNamesWaitsForAndConditionalBlockers: a waits-for onto a spawner -// whose child is still open, and a conditional-blocks onto an open target, -// each name their target, as a blocks edge does. -func runBlockedNamesWaitsForAndConditionalBlockers(t *testing.T, ctx context.Context, f BlockedListerFixture) { - label := f.IssuePrefix + "-bl-wf" - spawner, spawned := blListerID(f, "wf-spawner"), blListerID(f, "wf-spawned") - waiter := blListerID(f, "wf-waiter") - target, conditional := blListerID(f, "cb-target"), blListerID(f, "cb-src") - blListerSeedIssue(t, ctx, f, blListerIssue(spawner, 1, label+"-other")) - blListerSeedIssue(t, ctx, f, blListerIssue(spawned, 1, label+"-other")) - blListerSeedIssue(t, ctx, f, blListerIssue(target, 1, label+"-other")) - blListerSeedIssue(t, ctx, f, blListerIssue(waiter, 1, label)) - blListerSeedIssue(t, ctx, f, blListerIssue(conditional, 1, label)) - blListerSeedEdge(t, ctx, f, spawned, spawner, types.DepParentChild) - blListerSeedEdge(t, ctx, f, waiter, spawner, types.DepWaitsFor) - blListerSeedEdge(t, ctx, f, conditional, target, types.DepConditionalBlocks) - - items := blListerList(t, ctx, f, publicops.BlockedRequest{Labels: []string{label}}) - blListerAssertRows(t, items, map[string][]string{waiter: {spawner}, conditional: {target}}) -} - -// runBlockedOmitsAStaleFlag: a row whose stored flag says blocked but which -// has no active blocker and no parent names no blocker, so it is not listed. -// This is the narrowing gc's readiness projection documents relying on. -func runBlockedOmitsAStaleFlag(t *testing.T, ctx context.Context, f BlockedListerFixture) { - if f.MarkBlocked == nil { - t.Skip("this leg cannot write the stored is_blocked flag directly") - } - label := f.IssuePrefix + "-bl-stale" - stale, real, blocker := blListerID(f, "stale-src"), blListerID(f, "stale-real"), blListerID(f, "stale-blocker") - blListerSeedIssue(t, ctx, f, blListerIssue(stale, 1, label)) - blListerSeedIssue(t, ctx, f, blListerIssue(real, 1, label)) - blListerSeedIssue(t, ctx, f, blListerIssue(blocker, 1, label+"-other")) - blListerSeedEdge(t, ctx, f, real, blocker, types.DepBlocks) - if err := f.MarkBlocked(ctx, stale); err != nil { - t.Fatalf("mark %s blocked: %v", stale, err) - } - - items := blListerList(t, ctx, f, publicops.BlockedRequest{Labels: []string{label}}) - blListerAssertRows(t, items, map[string][]string{real: {blocker}}) -} - -// runBlockedHasNoTypeExclusions: a gate is listed like any other row; the -// blocked door excludes no issue type (unlike the ready front). -func runBlockedHasNoTypeExclusions(t *testing.T, ctx context.Context, f BlockedListerFixture) { - label := f.IssuePrefix + "-bl-type" - gate, epic, blocker := blListerID(f, "type-gate"), blListerID(f, "type-epic"), blListerID(f, "type-blocker") - g := blListerIssue(gate, 1, label) - g.IssueType = types.TypeGate - g.AwaitType = "human" - blListerSeedIssue(t, ctx, f, g) - e := blListerIssue(epic, 1, label) - e.IssueType = types.TypeEpic - blListerSeedIssue(t, ctx, f, e) - blListerSeedIssue(t, ctx, f, blListerIssue(blocker, 1, label+"-other")) - blListerSeedEdge(t, ctx, f, gate, blocker, types.DepBlocks) - blListerSeedEdge(t, ctx, f, epic, blocker, types.DepBlocks) - - items := blListerList(t, ctx, f, publicops.BlockedRequest{Labels: []string{label}}) - blListerAssertRows(t, items, map[string][]string{gate: {blocker}, epic: {blocker}}) -} - -// runBlockedEmptyIsAnEmptySlice: nothing blocked is an empty, non-nil slice -// and a nil error. -func runBlockedEmptyIsAnEmptySlice(t *testing.T, ctx context.Context, f BlockedListerFixture) { - result, err := f.BlockedLister.ListBlocked(ctx, publicops.BlockedRequest{Labels: []string{f.IssuePrefix + "-bl-nothing"}}) - if err != nil { - t.Fatalf("ListBlocked: %v", err) - } - if result.Items == nil || len(result.Items) != 0 { - t.Fatalf("Items = %#v, want an empty non-nil slice", result.Items) - } -} - -// runBlockedDoesNotMutateTheRequest: the caller's label slices come back as -// they were sent, normalization notwithstanding. -func runBlockedDoesNotMutateTheRequest(t *testing.T, ctx context.Context, f BlockedListerFixture) { - req := publicops.BlockedRequest{ - ParentID: " ", - Labels: []string{" a ", "", "a"}, - LabelsAny: []string{" b"}, - ExcludeLabels: []string{"c "}, - } - before := publicops.BlockedRequest{ - ParentID: req.ParentID, - Labels: slices.Clone(req.Labels), - LabelsAny: slices.Clone(req.LabelsAny), - ExcludeLabels: slices.Clone(req.ExcludeLabels), - } - if _, err := f.BlockedLister.ListBlocked(ctx, req); err != nil { - t.Fatalf("ListBlocked: %v", err) - } - if !reflect.DeepEqual(req, before) { - t.Errorf("request after = %#v, before = %#v", req, before) - } -} - -func blListerID(f BlockedListerFixture, name string) string { - return fmt.Sprintf("%s-%s", f.IssuePrefix, name) -} - -func blListerIssue(id string, priority int, labels ...string) *types.Issue { - return &types.Issue{ - ID: id, - Title: id, - Status: types.StatusOpen, - Priority: priority, - IssueType: types.TypeTask, - Labels: labels, - } -} - -func blListerSeedIssue(t *testing.T, ctx context.Context, f BlockedListerFixture, issue *types.Issue) { - t.Helper() - if err := f.CreateIssue(ctx, issue, "seed"); err != nil { - t.Fatalf("seed issue %s: %v", issue.ID, err) - } -} - -func blListerSeedEdge(t *testing.T, ctx context.Context, f BlockedListerFixture, from, to string, kind types.DependencyType) { - t.Helper() - if err := f.AddDependency(ctx, &types.Dependency{IssueID: from, DependsOnID: to, Type: kind}, "seed"); err != nil { - t.Fatalf("seed %s edge %s -> %s: %v", kind, from, to, err) - } -} - -func blListerList(t *testing.T, ctx context.Context, f BlockedListerFixture, req publicops.BlockedRequest) []*types.BlockedIssue { - t.Helper() - result, err := f.BlockedLister.ListBlocked(ctx, req) - if err != nil { - t.Fatalf("ListBlocked(%+v): %v", req, err) - } - if result.Items == nil { - t.Fatalf("ListBlocked(%+v) Items is nil; the role promises a non-nil slice", req) - } - return result.Items -} - -func blListerIDs(items []*types.BlockedIssue) []string { - ids := make([]string, 0, len(items)) - for _, item := range items { - ids = append(ids, item.ID) - } - return ids -} - -// assertBlockedRows requires the listing to be exactly want: the same ids, -// each naming exactly the blockers want gives it (in any order), with -// BlockedByCount equal to their number. -func blListerAssertRows(t *testing.T, items []*types.BlockedIssue, want map[string][]string) { - t.Helper() - got := map[string][]string{} - for _, item := range items { - if item == nil { - t.Fatalf("listing carries a nil row") - } - if _, dup := got[item.ID]; dup { - t.Errorf("row %s listed twice", item.ID) - } - blockers := slices.Clone(item.BlockedBy) - slices.Sort(blockers) - got[item.ID] = blockers - if item.BlockedByCount != len(item.BlockedBy) { - t.Errorf("row %s: BlockedByCount = %d, len(BlockedBy) = %d", item.ID, item.BlockedByCount, len(item.BlockedBy)) - } - } - normalized := map[string][]string{} - for id, blockers := range want { - b := slices.Clone(blockers) - slices.Sort(b) - normalized[id] = b - } - if !reflect.DeepEqual(got, normalized) { - t.Errorf("listing = %v, want %v", got, normalized) - } -} diff --git a/backend/conformance/blocking_annotator_contract.go b/backend/conformance/blocking_annotator_contract.go index e17b49fb9..523b05037 100644 --- a/backend/conformance/blocking_annotator_contract.go +++ b/backend/conformance/blocking_annotator_contract.go @@ -546,65 +546,3 @@ func assertParent(t *testing.T, result publicops.BlockingResult, id, want string t.Errorf("entry %s parent = %q, want %q", id, got, want) } } - -// RunBlockingAnnotatorReportsTheTransitiveBlockedFlag pins -// BlockingRequest.IncludeIsBlocked and IssueBlocking.IsBlocked: asked, each id -// with a row carries its STORED TRANSITIVE is_blocked flag — a child of a -// blocked parent is blocked with no blocking edge of its own, which BlockedBy -// alone would read as free — and an id naming no row carries none. Unasked, -// no item carries a flag. This is the read issueops.BlockedFlags (storage's -// IsBlockedBatch on a role-served backend) is built on. -func RunBlockingAnnotatorReportsTheTransitiveBlockedFlag(t *testing.T, ctx context.Context, fixture BlockingAnnotatorFixture) { - t.Helper() - blocker := fixture.IssuePrefix + "-flag-blocker" - blocked := fixture.IssuePrefix + "-flag-blocked" - child := fixture.IssuePrefix + "-flag-child" - free := fixture.IssuePrefix + "-flag-free" - ghost := fixture.IssuePrefix + "-flag-ghost" - seedBlockingIssues(t, ctx, fixture, blocker, blocked, child, free) - seedBlockingEdge(t, ctx, fixture, child, blocked, types.DepParentChild) - seedBlockingEdge(t, ctx, fixture, blocked, blocker, types.DepBlocks) - - ids := []string{blocked, child, free, ghost} - unasked := annotateBlocking(t, ctx, fixture, publicops.BlockingRequest{IDs: ids}) - for _, item := range unasked.Items { - if item.IsBlocked != nil { - t.Errorf("unasked: %s carries IsBlocked=%v, want no flag", item.ID, *item.IsBlocked) - } - } - - asked := annotateBlocking(t, ctx, fixture, publicops.BlockingRequest{IDs: ids, IncludeIsBlocked: true}) - want := map[string]*bool{blocked: new(true), child: new(true), free: new(false), ghost: nil} - if len(asked.Items) != len(ids) { - t.Fatalf("asked: %d items, want %d", len(asked.Items), len(ids)) - } - for _, item := range asked.Items { - w, ok := want[item.ID] - if !ok { - t.Errorf("asked: unexpected item %s", item.ID) - continue - } - switch { - case w == nil && item.IsBlocked != nil: - t.Errorf("asked: %s (no row) IsBlocked=%v, want no flag", item.ID, *item.IsBlocked) - case w != nil && item.IsBlocked == nil: - t.Errorf("asked: %s IsBlocked absent, want %v", item.ID, *w) - case w != nil && *item.IsBlocked != *w: - t.Errorf("asked: %s IsBlocked=%v, want %v", item.ID, *item.IsBlocked, *w) - } - } - - flags, err := publicops.BlockedFlags(ctx, fixture.Annotator, append(ids, blocked, "")) - if err != nil { - t.Fatalf("BlockedFlags: %v", err) - } - wantFlags := map[string]bool{blocked: true, child: true, free: false} - if len(flags) != len(wantFlags) { - t.Errorf("BlockedFlags = %v, want %v (no entry for the ghost or the blank id)", flags, wantFlags) - } - for id, w := range wantFlags { - if got, ok := flags[id]; !ok || got != w { - t.Errorf("BlockedFlags[%s] = %v (present %v), want %v", id, got, ok, w) - } - } -} diff --git a/backend/conformance/counter_contract.go b/backend/conformance/counter_contract.go index b18f5fd3f..e70e2e0f2 100644 --- a/backend/conformance/counter_contract.go +++ b/backend/conformance/counter_contract.go @@ -57,19 +57,6 @@ type CounterFixture struct { // A nil hook means "this backend cannot observe history", and the case // that needs it SKIPS rather than passing quietly. CountHistory func(context.Context) (int, error) - // AddDependency seeds one dependency edge, the same hook - // ReaderFixture/DependencyEditorFixture take it from (role_fixture_kit's - // shared AddDependency). It exists here only for the ParentID/NoParent - // scope cases (S8): NoParent excludes by PARENT-CHILD EDGE alone - // (sqlbuild/filter.go), unlike ParentID's dotted-id-prefix fallback, so - // pinning it needs a real edge and not just a naming convention. - AddDependency func(context.Context, *types.Dependency, string) error - // List runs the List role's query directly (the store's IssueReader), so - // the storage-level parity cases below (S8 review, follow-up #5) can - // compare Count's answer against len(List's rows) for the same scope - // without going through a second builder. A nil hook skips those cases - // rather than failing: not every backend wiring has a Reader to hand. - List func(context.Context, publicops.ListRequest) (publicops.IssuePage, error) } // RunCounterCountsTheDurablePlaneByDefault pins counter.go:123-126 from the @@ -189,323 +176,6 @@ func RunCounterAnUnknownStatusMatchesNothing(t *testing.T, ctx context.Context, assertCounterTotal(t, ctx, fixture, scope, 0) } -// RunCounterParentIDScopesToChildren pins CountRequest.ParentID (counter.go, -// S8): restricted to ONE issue's children, reached two ways at once, exactly -// as ListRequest.ParentID is (sqlbuild/filter.go) — a parent-child dependency -// edge, OR (for a row with no such edge) a dotted-id prefix match. The parent -// itself, an unrelated row and a same-prefix DECOY (sibling id with no -// separator, e.g. "parent2") are all seeded so neither arm can pass by -// over-matching. -func RunCounterParentIDScopesToChildren(t *testing.T, ctx context.Context, fixture CounterFixture) { - t.Helper() - parent := fixture.IssuePrefix + "-parentid-parent" - byEdge := fixture.IssuePrefix + "-parentid-byedge" - byDotted := parent + ".1" - decoy := parent + "x" - unrelated := fixture.IssuePrefix + "-parentid-unrelated" - - for _, id := range []string{parent, byEdge, byDotted, decoy, unrelated} { - seedCounterIssue(t, ctx, fixture, counterSeed(id)) - } - requireCounterAddDependency(t, fixture) - if err := fixture.AddDependency(ctx, &types.Dependency{ - IssueID: byEdge, DependsOnID: parent, Type: types.DepParentChild, - }, "seed"); err != nil { - t.Fatalf("seed edge %s -> %s: %v", byEdge, parent, err) - } - - scope := counterScope(parent, byEdge, byDotted, decoy, unrelated) - scope.ParentID = parent - // Both children count; the parent, the decoy and the unrelated row do not. - assertCounterTotal(t, ctx, fixture, scope, 2) -} - -// RunCounterNoParentExcludesChildren pins CountRequest.NoParent -// (counter.go, S8): restricted to rows with NO parent-child edge. -// -// Unlike ParentID, NoParent's SQL clause (sqlbuild/filter.go) checks ONLY the -// parent-child dependency table, not the dotted-id convention — a row that is -// a dotted-id child with no edge at all still passes NoParent. That asymmetry -// is exactly what the "byDotted" row below pins: it is excluded from -// ParentID's answer by nothing (RunCounterParentIDScopesToChildren already -// admits it), but NoParent admits it too, because it carries no edge. -func RunCounterNoParentExcludesChildren(t *testing.T, ctx context.Context, fixture CounterFixture) { - t.Helper() - parent := fixture.IssuePrefix + "-noparent-parent" - child := fixture.IssuePrefix + "-noparent-child" - byDotted := parent + ".1" - - for _, id := range []string{parent, child, byDotted} { - seedCounterIssue(t, ctx, fixture, counterSeed(id)) - } - requireCounterAddDependency(t, fixture) - if err := fixture.AddDependency(ctx, &types.Dependency{ - IssueID: child, DependsOnID: parent, Type: types.DepParentChild, - }, "seed"); err != nil { - t.Fatalf("seed edge %s -> %s: %v", child, parent, err) - } - - scope := counterScope(parent, child, byDotted) - assertCounterTotal(t, ctx, fixture, scope, 3) - - scope.NoParent = true - // child is excluded (has an edge); parent and byDotted remain (neither - // carries a parent-child edge, and NoParent does not consult the dotted - // naming convention at all). - assertCounterTotal(t, ctx, fixture, scope, 2) -} - -// RunCounterExcludeTypesNarrowsThePredicate pins CountRequest.ExcludeTypes -// (counter.go, S8): named types are excluded from the count, composing with -// (rather than replacing) IncludeInfra's own "gate" exclusion. -func RunCounterExcludeTypesNarrowsThePredicate(t *testing.T, ctx context.Context, fixture CounterFixture) { - t.Helper() - task := fixture.IssuePrefix + "-extype-task" - chore := fixture.IssuePrefix + "-extype-chore" - gate := fixture.IssuePrefix + "-extype-gate" - seedCounterIssue(t, ctx, fixture, counterSeed(task)) - choreSeed := counterSeed(chore) - choreSeed.IssueType = types.TypeChore - seedCounterIssue(t, ctx, fixture, choreSeed) - gateSeed := counterSeed(gate) - gateSeed.IssueType = types.IssueType("gate") - seedCounterIssue(t, ctx, fixture, gateSeed) - - scope := counterScope(task, chore, gate) - assertCounterTotal(t, ctx, fixture, scope, 3) - - scope.ExcludeTypes = []string{"chore"} - assertCounterTotal(t, ctx, fixture, scope, 2) - - // Composes with IncludeInfra's own default gate exclusion: both the - // explicit chore exclusion and the include-infra gate exclusion apply at - // once, leaving only task. - scope.IncludeInfra = true - assertCounterTotal(t, ctx, fixture, scope, 1) -} - -// RunCounterExcludeStatusNarrowsThePredicate pins CountRequest.ExcludeStatus -// (counter.go, S8): a Count-only field with no List counterpart. Named -// statuses are excluded, and — UNLIKE Status — an unrecognized name is -// refused as ErrValidation rather than excluding nothing, because a typo -// would silently overcount (counter.go). -func RunCounterExcludeStatusNarrowsThePredicate(t *testing.T, ctx context.Context, fixture CounterFixture) { - t.Helper() - open := fixture.IssuePrefix + "-exstatus-open" - closed := fixture.IssuePrefix + "-exstatus-closed" - seedCounterIssue(t, ctx, fixture, counterSeed(open)) - closedSeed := counterSeed(closed) - closedSeed.Status = types.StatusClosed - seedCounterIssue(t, ctx, fixture, closedSeed) - - scope := counterScope(open, closed) - assertCounterTotal(t, ctx, fixture, scope, 2) - - scope.ExcludeStatus = []string{"closed"} - assertCounterTotal(t, ctx, fixture, scope, 1) - - // Unlike Status and IssueType, ExcludeStatus IS validated (S8 review, - // follow-up #3): a misspelled entry would silently exclude nothing and - // OVERCOUNT, so an unrecognized name is refused as ErrValidation instead - // of treated as a no-op. - scope.ExcludeStatus = []string{"no-such-status"} - if _, err := fixture.Counter.Count(ctx, scope); !errors.Is(err, publicops.ErrValidation) { - t.Errorf("Count with an unrecognized exclude-status = %v, want ErrValidation", err) - } -} - -// requireCounterAddDependency skips a case that needs a real parent-child -// edge when the fixture offers no way to create one, rather than failing with -// a nil-pointer call — mirroring counterHistoryCount's optional-hook pattern. -func requireCounterAddDependency(t *testing.T, fixture CounterFixture) { - t.Helper() - if fixture.AddDependency == nil { - t.Skip("fixture cannot seed a dependency edge: AddDependency is nil") - } -} - -// requireCounterList skips a case that needs the List role when the fixture -// offers no way to reach it, mirroring requireCounterAddDependency. -func requireCounterList(t *testing.T, fixture CounterFixture) { - t.Helper() - if fixture.List == nil { - t.Skip("fixture cannot run the List role: List is nil") - } -} - -// assertCounterMatchesList compares Count's Total against len(List's rows) -// for the same scope, with List's AllFlag and IncludeAllTypes both set so -// List's own default exclusions (status, pinned, template, gate, infra-plane) -// are lifted exactly as far as Count's own default lifts none of them -// (workapi.BuildCountFilter's doc, count.go) — the one combination that -// makes the two cardinalities comparable at all for an arbitrary scope. -// -// This is a DIFFERENT check than -// TestBuildCountFilterParentFieldsMatchListFilter -// (internal/workapi/count_test.go), which compares the two FILTERS a shared -// request builds without touching a store; this one runs both roles against -// a real backend and compares what they actually return (S8 review, follow-up -// #5). -func assertCounterMatchesList(t *testing.T, ctx context.Context, fixture CounterFixture, idFilter, parentID string, noParent bool, excludeTypes []string) { - t.Helper() - countReq := publicops.CountRequest{ - IDFilter: idFilter, - ParentID: parentID, - NoParent: noParent, - ExcludeTypes: excludeTypes, - } - total := counterTotal(t, ctx, fixture, countReq) - - listReq := publicops.ListRequest{ - IDFilter: idFilter, - ParentID: parentID, - NoParent: noParent, - ExcludeTypes: excludeTypes, - AllFlag: true, - IncludeAllTypes: true, - } - page, err := fixture.List(ctx, listReq) - if err != nil { - t.Fatalf("List(%+v): %v", listReq, err) - } - if got := int64(len(page.Items)); got != total { - t.Errorf("Count(%+v) = %d, len(List(%+v).Items) = %d, want equal", countReq, total, listReq, got) - } -} - -// RunCounterParentIDMatchesListCardinality pins the storage-level half of -// CountRequest.ParentID's promise (S8 review, follow-up #5a): for one scope, -// Count's Total equals len(List's rows) for the equivalent ParentID scope. -func RunCounterParentIDMatchesListCardinality(t *testing.T, ctx context.Context, fixture CounterFixture) { - t.Helper() - requireCounterList(t, fixture) - requireCounterAddDependency(t, fixture) - - parent := fixture.IssuePrefix + "-parity-parentid-parent" - byEdge := fixture.IssuePrefix + "-parity-parentid-byedge" - byDotted := parent + ".1" - unrelated := fixture.IssuePrefix + "-parity-parentid-unrelated" - - for _, id := range []string{parent, byEdge, byDotted, unrelated} { - seedCounterIssue(t, ctx, fixture, counterSeed(id)) - } - if err := fixture.AddDependency(ctx, &types.Dependency{ - IssueID: byEdge, DependsOnID: parent, Type: types.DepParentChild, - }, "seed"); err != nil { - t.Fatalf("seed edge %s -> %s: %v", byEdge, parent, err) - } - - idFilter := counterScope(parent, byEdge, byDotted, unrelated).IDFilter - assertCounterMatchesList(t, ctx, fixture, idFilter, parent, false, nil) -} - -// RunCounterNoParentMatchesListCardinality pins the storage-level half of -// CountRequest.NoParent's promise (S8 review, follow-up #5a). -func RunCounterNoParentMatchesListCardinality(t *testing.T, ctx context.Context, fixture CounterFixture) { - t.Helper() - requireCounterList(t, fixture) - requireCounterAddDependency(t, fixture) - - parent := fixture.IssuePrefix + "-parity-noparent-parent" - child := fixture.IssuePrefix + "-parity-noparent-child" - byDotted := parent + ".1" - - for _, id := range []string{parent, child, byDotted} { - seedCounterIssue(t, ctx, fixture, counterSeed(id)) - } - if err := fixture.AddDependency(ctx, &types.Dependency{ - IssueID: child, DependsOnID: parent, Type: types.DepParentChild, - }, "seed"); err != nil { - t.Fatalf("seed edge %s -> %s: %v", child, parent, err) - } - - idFilter := counterScope(parent, child, byDotted).IDFilter - assertCounterMatchesList(t, ctx, fixture, idFilter, "", true, nil) -} - -// RunCounterExcludeTypesMatchesListCardinality pins the storage-level half of -// CountRequest.ExcludeTypes' promise (S8 review, follow-up #5a). ExcludeStatus -// has no List counterpart (counter.go) so it is not part of this parity -// family; RunCounterExcludeStatusNarrowsThePredicate above is its only -// coverage. -func RunCounterExcludeTypesMatchesListCardinality(t *testing.T, ctx context.Context, fixture CounterFixture) { - t.Helper() - requireCounterList(t, fixture) - - task := fixture.IssuePrefix + "-parity-extype-task" - chore := fixture.IssuePrefix + "-parity-extype-chore" - seedCounterIssue(t, ctx, fixture, counterSeed(task)) - choreSeed := counterSeed(chore) - choreSeed.IssueType = types.TypeChore - seedCounterIssue(t, ctx, fixture, choreSeed) - - idFilter := counterScope(task, chore).IDFilter - assertCounterMatchesList(t, ctx, fixture, idFilter, "", false, []string{"chore"}) -} - -// RunCounterParentIDIncludesAWispChild pins that ParentID's edge match reaches -// into the wisp tier exactly as IncludeInfra's merge does (S8 review, -// follow-up #5b): a child seeded in the ephemeral/wisp tier, linked to its -// parent by the same parent-child edge ParentID matches on, is invisible to -// Count's durable-only default and counted once IncludeInfra merges the wisps -// table in. -func RunCounterParentIDIncludesAWispChild(t *testing.T, ctx context.Context, fixture CounterFixture) { - t.Helper() - requireCounterAddDependency(t, fixture) - - parent := fixture.IssuePrefix + "-wispchild-parent" - wispChild := fixture.IssuePrefix + "-wispchild-child" - seedCounterIssue(t, ctx, fixture, counterSeed(parent)) - seedCounterWisp(t, ctx, fixture, counterSeed(wispChild)) - if err := fixture.AddDependency(ctx, &types.Dependency{ - IssueID: wispChild, DependsOnID: parent, Type: types.DepParentChild, - }, "seed"); err != nil { - t.Fatalf("seed edge %s -> %s: %v", wispChild, parent, err) - } - - scope := counterScope(parent, wispChild) - scope.ParentID = parent - // Durable-only default (counter.go, RunCounterCountsTheDurablePlaneByDefault): - // the wisp child is invisible without IncludeInfra. - assertCounterTotal(t, ctx, fixture, scope, 0) - - scope.IncludeInfra = true - assertCounterTotal(t, ctx, fixture, scope, 1) -} - -// RunCounterParentIDAndExcludeStatusComposeOnAClosedChild pins that ParentID -// and ExcludeStatus apply as a conjunction, not alternatives (S8 review, -// follow-up #5b): a closed child under its parent is counted by ParentID -// alone (Count hides nothing by default), and excluded once ExcludeStatus -// names its status, leaving only the open child. -func RunCounterParentIDAndExcludeStatusComposeOnAClosedChild(t *testing.T, ctx context.Context, fixture CounterFixture) { - t.Helper() - requireCounterAddDependency(t, fixture) - - parent := fixture.IssuePrefix + "-closedchild-parent" - openChild := fixture.IssuePrefix + "-closedchild-open" - closedChild := fixture.IssuePrefix + "-closedchild-closed" - seedCounterIssue(t, ctx, fixture, counterSeed(parent)) - seedCounterIssue(t, ctx, fixture, counterSeed(openChild)) - closedSeed := counterSeed(closedChild) - closedSeed.Status = types.StatusClosed - seedCounterIssue(t, ctx, fixture, closedSeed) - for _, child := range []string{openChild, closedChild} { - if err := fixture.AddDependency(ctx, &types.Dependency{ - IssueID: child, DependsOnID: parent, Type: types.DepParentChild, - }, "seed"); err != nil { - t.Fatalf("seed edge %s -> %s: %v", child, parent, err) - } - } - - scope := counterScope(parent, openChild, closedChild) - scope.ParentID = parent - assertCounterTotal(t, ctx, fixture, scope, 2) - - scope.ExcludeStatus = []string{"closed"} - assertCounterTotal(t, ctx, fixture, scope, 1) -} - // RunCounterGroupsPartitionTheScalarSet pins counter.go:168-169 and :244-245 // for a dimension whose buckets do NOT overlap: the grouped Total is the same // number a scalar Count returns, and the buckets partition it exactly. diff --git a/backend/conformance/cross_record_invariant_contract.go b/backend/conformance/cross_record_invariant_contract.go deleted file mode 100644 index e872ca613..000000000 --- a/backend/conformance/cross_record_invariant_contract.go +++ /dev/null @@ -1,372 +0,0 @@ -package conformance - -import ( - "context" - "testing" - "time" -) - -// This file holds the contract for R16.1 (gastownhall/beads#6133, -// be-hs42e.1 §5-§7, FR-02, FR-03): enforcement that spans more than one -// record, which ExpectedRevisionFixture's per-record CompareAndSetVersion -// structurally cannot see. -// -// GuardedWrite AND ExpectedRevisionFixture.CompareAndSetVersion SHARE THE -// EXACT SAME TYPE, PerRecordCASWrite, declared once in -// expected_revision_contract.go. R16.1-a's boundary claim is literally that -// the second is the first, called twice against records that individually -// pass but jointly violate a store invariant — sharing the type makes that -// claim structural, checked by the compiler, rather than merely asserted in -// a doc comment. -// -// TWO CONCRETE SUBJECTS ARE REQUIRED BY FR-02 RATHER THAN INVENTED HERE: -// Memory key/alias uniqueness (#5877 R2) and graph-edge -// MaximumEndpointMultiplicity. MemoryKeyAliasWrite and GraphEdgeWrite exist -// so the cases named for them construct a REAL joint violation in each -// subject's own vocabulary, rather than a synthetic one this file made up. -// -// EVERY HOOK BELOW IS INDEPENDENTLY NILABLE — see the package-level note in -// expected_revision_contract.go. Every case nil-checks every hook it uses -// and SKIPS BY NAME when one is missing. - -// RecordWrite is one record's half of a multi-record write submitted to -// EnforceCrossRecordInvariant. -type RecordWrite struct { - ID string - Expected *Address - Patch map[string]any -} - -// InvariantResult is the outcome of a call to EnforceCrossRecordInvariant. -type InvariantResult struct { - Accepted bool - InvariantRefusal *InvariantRefusal -} - -// InvariantRefusal names the specific store invariant a multi-record write -// violated (R16.1-c) — never a bare boolean, so a caller (and a test) can -// tell which invariant fired. -type InvariantRefusal struct { - InvariantName string -} - -// LeaseHandle is an advisory lease over subject. It is observed by -// convention only (FR-03): holding one does not, by itself, change what any -// per-record or invariant-enforcing write accepts. -type LeaseHandle struct { - Subject string - ExpiresAt time.Time - // Release ends the lease early. A nil Release means the fixture expects - // the lease to simply expire; cases treat a nil Release as optional - // cleanup, not a required call. - Release func(ctx context.Context) error -} - -// CrossRecordInvariantFixture supplies the capabilities under test for -// R16.1: enforcement that spans more than one record, and the advisory -// lease primitive FR-03 says is insufficient on its own to provide it. -type CrossRecordInvariantFixture struct { - IssuePrefix string - - // GuardedWrite is a per-record CAS write over the SAME type as - // ExpectedRevisionFixture.CompareAndSetVersion (PerRecordCASWrite) — see - // this file's package doc comment for why. A nil hook means this - // backend exposes no per-record guard to construct the boundary case - // against, and cases that need one skip with that reason. - GuardedWrite PerRecordCASWrite - - // EnforceCrossRecordInvariant evaluates writes as one joint operation - // and refuses the whole batch, with a named InvariantRefusal, if - // accepting it would violate a store invariant spanning more than one - // of the given records. A nil hook means this backend has not wired - // store-invariant enforcement yet, and cases that need one skip with - // that reason. - EnforceCrossRecordInvariant func(ctx context.Context, writes []RecordWrite) (InvariantResult, error) - - // AcquireAdvisoryLease acquires a convention-only lease over subject for - // up to ttl. A nil hook means this backend has no lease primitive to - // test as an insufficient guard, and cases that need one skip with that - // reason. - AcquireAdvisoryLease func(ctx context.Context, subject string, ttl time.Duration) (LeaseHandle, error) - - // CountHistoryForSubject reports how many durable history entries exist - // for subject. A nil hook means this backend cannot observe history by - // subject, and cases that need one skip with that reason. - CountHistoryForSubject func(ctx context.Context, subject string) (int, error) - - // MemoryKeyAliasWrite writes memoryID's key/alias pair unconditionally, - // in the shape of a per-record write (FR-02's first named subject, - // #5877 R2). A nil hook means this backend exposes no Memory key/alias - // write to probe, and the case that needs it skips with that reason. - MemoryKeyAliasWrite func(ctx context.Context, memoryID, key, alias string) (ExpectedRevisionResult, error) - - // GraphEdgeWrite writes a graph edge unconditionally, in the shape of a - // per-record write (FR-02's second named subject, - // MaximumEndpointMultiplicity). A nil hook means this backend exposes no - // graph edge write to probe, and the case that needs it skips with that - // reason. - GraphEdgeWrite func(ctx context.Context, fromID, toID, edgeType string) (ExpectedRevisionResult, error) -} - -// RunCrossRecordInvariantSurvivesTwoPassingPerRecordGuards pins R16.1-a's -// boundary claim directly: two writes, each individually valid under -// GuardedWrite (== CompareAndSetVersion), that JOINTLY violate a store -// invariant neither call's own precondition can see. Both must be Accepted -// through GuardedWrite alone — the whole point of R16.1 is that this -// per-record guard cannot catch what only spans records. -func RunCrossRecordInvariantSurvivesTwoPassingPerRecordGuards(t *testing.T, ctx context.Context, fixture CrossRecordInvariantFixture) { - t.Helper() - if fixture.GuardedWrite == nil { - t.Skip("this backend exposes no per-record guard to construct the boundary case against (GuardedWrite is nil)") - } - shared := fixture.IssuePrefix + "-crossrec-shared-alias" - - first, err := fixture.GuardedWrite(ctx, fixture.IssuePrefix+"-crossrec-a", nil, map[string]any{"alias": shared}) - if err != nil { - t.Fatalf("GuardedWrite(a): %v", err) - } - if !first.Accepted { - t.Fatalf("GuardedWrite(a) claiming alias %q was refused (Refusal=%+v), want Accepted: a per-record guard cannot see a joint violation", shared, first.Refusal) - } - - second, err := fixture.GuardedWrite(ctx, fixture.IssuePrefix+"-crossrec-b", nil, map[string]any{"alias": shared}) - if err != nil { - t.Fatalf("GuardedWrite(b): %v", err) - } - if !second.Accepted { - t.Fatalf("GuardedWrite(b) claiming the SAME alias %q was refused (Refusal=%+v), want Accepted (R16.1-a: per-record CAS structurally cannot catch a cross-record conflict)", shared, second.Refusal) - } -} - -// RunStoreInvariantTransactionScopesExactlyTheSpanningRecords pins R16.1-b: -// EnforceCrossRecordInvariant refuses a batch that jointly violates an -// invariant, and — checked against an unrelated, non-conflicting write in -// the same call shape — must not refuse more broadly than the records that -// actually span the violation. -func RunStoreInvariantTransactionScopesExactlyTheSpanningRecords(t *testing.T, ctx context.Context, fixture CrossRecordInvariantFixture) { - t.Helper() - if fixture.EnforceCrossRecordInvariant == nil { - t.Skip("this backend has not wired store-invariant enforcement yet (EnforceCrossRecordInvariant is nil)") - } - shared := fixture.IssuePrefix + "-crossrec-scope-alias" - spanning := []RecordWrite{ - {ID: fixture.IssuePrefix + "-crossrec-scope-a", Patch: map[string]any{"alias": shared}}, - {ID: fixture.IssuePrefix + "-crossrec-scope-b", Patch: map[string]any{"alias": shared}}, - } - - var beforeA, beforeB int - if fixture.CountHistoryForSubject != nil { - var err error - beforeA, err = fixture.CountHistoryForSubject(ctx, spanning[0].ID) - if err != nil { - t.Fatalf("CountHistoryForSubject(%s) before the refused batch: %v", spanning[0].ID, err) - } - beforeB, err = fixture.CountHistoryForSubject(ctx, spanning[1].ID) - if err != nil { - t.Fatalf("CountHistoryForSubject(%s) before the refused batch: %v", spanning[1].ID, err) - } - } else { - t.Logf("this backend cannot observe history by subject (CountHistoryForSubject is nil): the before-batch history counts for %s and %s go uncaptured", spanning[0].ID, spanning[1].ID) - } - - result, err := fixture.EnforceCrossRecordInvariant(ctx, spanning) - if err != nil { - t.Fatalf("EnforceCrossRecordInvariant(spanning): %v", err) - } - if result.Accepted { - t.Fatal("EnforceCrossRecordInvariant accepted two writes that jointly violate the shared-alias invariant") - } - - if fixture.CountHistoryForSubject != nil { - afterA, err := fixture.CountHistoryForSubject(ctx, spanning[0].ID) - if err != nil { - t.Fatalf("CountHistoryForSubject(%s) after the refused batch: %v", spanning[0].ID, err) - } - afterB, err := fixture.CountHistoryForSubject(ctx, spanning[1].ID) - if err != nil { - t.Fatalf("CountHistoryForSubject(%s) after the refused batch: %v", spanning[1].ID, err) - } - if afterA != beforeA || afterB != beforeB { - t.Errorf("a refused batch left a trace: history count for %s went %d->%d, for %s went %d->%d; a refusal must be atomic — the whole batch takes no effect, not just the record that individually triggered it", spanning[0].ID, beforeA, afterA, spanning[1].ID, beforeB, afterB) - } - } else { - t.Logf("this backend cannot observe history by subject (CountHistoryForSubject is nil): the refused batch's atomicity (no partial trace left behind for %s or %s) goes unverified", spanning[0].ID, spanning[1].ID) - } - - unrelated := []RecordWrite{ - {ID: fixture.IssuePrefix + "-crossrec-scope-unrelated", Patch: map[string]any{"alias": fixture.IssuePrefix + "-crossrec-scope-unrelated-alias"}}, - } - independent, err := fixture.EnforceCrossRecordInvariant(ctx, unrelated) - if err != nil { - t.Fatalf("EnforceCrossRecordInvariant(unrelated): %v", err) - } - if !independent.Accepted { - t.Fatalf("EnforceCrossRecordInvariant refused an unrelated, non-conflicting write (InvariantRefusal=%+v); the invariant transaction must scope exactly the spanning records, not more", independent.InvariantRefusal) - } -} - -// RunInvariantRefusalNamesTheViolatedInvariant pins R16.1-c: a refused batch -// carries an InvariantRefusal naming the specific invariant that fired, not -// a bare boolean. -func RunInvariantRefusalNamesTheViolatedInvariant(t *testing.T, ctx context.Context, fixture CrossRecordInvariantFixture) { - t.Helper() - if fixture.EnforceCrossRecordInvariant == nil { - t.Skip("this backend has not wired store-invariant enforcement yet (EnforceCrossRecordInvariant is nil)") - } - shared := fixture.IssuePrefix + "-crossrec-named-alias" - result, err := fixture.EnforceCrossRecordInvariant(ctx, []RecordWrite{ - {ID: fixture.IssuePrefix + "-crossrec-named-a", Patch: map[string]any{"alias": shared}}, - {ID: fixture.IssuePrefix + "-crossrec-named-b", Patch: map[string]any{"alias": shared}}, - }) - if err != nil { - t.Fatalf("EnforceCrossRecordInvariant: %v", err) - } - if result.Accepted { - t.Fatal("EnforceCrossRecordInvariant accepted a joint violation; cannot check the refusal's InvariantName") - } - if result.InvariantRefusal == nil { - t.Fatal("InvariantRefusal = nil on a non-accepted result") - } - if result.InvariantRefusal.InvariantName == "" { - t.Error("InvariantRefusal.InvariantName is empty, want a specific invariant name (R16.1-c)") - } -} - -// RunMemoryKeyAliasUniquenessIsAStoreInvariantNotACAS pins FR-02's first -// required subject: Memory key/alias uniqueness (#5877 R2) is a STORE -// invariant, not row-level CAS, so two memories independently claiming the -// same alias each succeed when checked one at a time through a -// per-record-shaped call. -func RunMemoryKeyAliasUniquenessIsAStoreInvariantNotACAS(t *testing.T, ctx context.Context, fixture CrossRecordInvariantFixture) { - t.Helper() - if fixture.MemoryKeyAliasWrite == nil { - t.Skip("this backend exposes no Memory key/alias write to probe (MemoryKeyAliasWrite is nil)") - } - alias := fixture.IssuePrefix + "-crossrec-memalias" - - first, err := fixture.MemoryKeyAliasWrite(ctx, fixture.IssuePrefix+"-crossrec-mem-a", "key-a", alias) - if err != nil { - t.Fatalf("MemoryKeyAliasWrite(a): %v", err) - } - if !first.Accepted { - t.Fatalf("MemoryKeyAliasWrite(a) was refused (Refusal=%+v), want Accepted", first.Refusal) - } - - second, err := fixture.MemoryKeyAliasWrite(ctx, fixture.IssuePrefix+"-crossrec-mem-b", "key-b", alias) - if err != nil { - t.Fatalf("MemoryKeyAliasWrite(b): %v", err) - } - if !second.Accepted { - t.Fatalf("MemoryKeyAliasWrite(b) claiming the SAME alias %q was refused (Refusal=%+v); alias uniqueness (#5877 R2) is a store invariant, so a per-record write cannot enforce it and must be Accepted here", alias, second.Refusal) - } -} - -// RunMaximumEndpointMultiplicityIsAStoreInvariantNotACAS pins FR-02's second -// required subject: MaximumEndpointMultiplicity is a STORE invariant, not -// row-level CAS, so two edges independently claiming the same endpoint each -// succeed when checked one at a time through a per-record-shaped call. -func RunMaximumEndpointMultiplicityIsAStoreInvariantNotACAS(t *testing.T, ctx context.Context, fixture CrossRecordInvariantFixture) { - t.Helper() - if fixture.GraphEdgeWrite == nil { - t.Skip("this backend exposes no graph edge write to probe (GraphEdgeWrite is nil)") - } - from := fixture.IssuePrefix + "-crossrec-edge-from" - - first, err := fixture.GraphEdgeWrite(ctx, from, fixture.IssuePrefix+"-crossrec-edge-to-a", "depends-on") - if err != nil { - t.Fatalf("GraphEdgeWrite(a): %v", err) - } - if !first.Accepted { - t.Fatalf("GraphEdgeWrite(a) was refused (Refusal=%+v), want Accepted", first.Refusal) - } - - second, err := fixture.GraphEdgeWrite(ctx, from, fixture.IssuePrefix+"-crossrec-edge-to-b", "depends-on") - if err != nil { - t.Fatalf("GraphEdgeWrite(b): %v", err) - } - if !second.Accepted { - t.Fatalf("GraphEdgeWrite(b) from the SAME endpoint %q was refused (Refusal=%+v); MaximumEndpointMultiplicity is a store invariant, so a per-record write cannot enforce it and must be Accepted here", from, second.Refusal) - } -} - -// RunAdvisoryLeaseAloneDoesNotPreventTheInvariantViolation pins FR-03's -// negative case: an advisory lease is observed BY CONVENTION ONLY, so -// holding one over the shared subject does not, by itself, stop the same -// per-record violation RunCrossRecordInvariantSurvivesTwoPassingPerRecordGuards -// constructs. -func RunAdvisoryLeaseAloneDoesNotPreventTheInvariantViolation(t *testing.T, ctx context.Context, fixture CrossRecordInvariantFixture) { - t.Helper() - if fixture.AcquireAdvisoryLease == nil { - t.Skip("this backend has no lease primitive to test as an insufficient guard (AcquireAdvisoryLease is nil)") - } - if fixture.GuardedWrite == nil { - t.Skip("this backend exposes no per-record guard to construct the boundary case against (GuardedWrite is nil)") - } - shared := fixture.IssuePrefix + "-crossrec-leased-alias" - crossRecordInvariantAcquireLease(t, ctx, fixture, shared, time.Minute) - - first, err := fixture.GuardedWrite(ctx, fixture.IssuePrefix+"-crossrec-leased-a", nil, map[string]any{"alias": shared}) - if err != nil { - t.Fatalf("GuardedWrite(a) under a held lease: %v", err) - } - second, err := fixture.GuardedWrite(ctx, fixture.IssuePrefix+"-crossrec-leased-b", nil, map[string]any{"alias": shared}) - if err != nil { - t.Fatalf("GuardedWrite(b) under a held lease: %v", err) - } - if !first.Accepted || !second.Accepted { - t.Fatalf("holding an advisory lease changed a per-record GuardedWrite's own verdict (first.Accepted=%v second.Accepted=%v); the lease is observed by convention only and GuardedWrite must not consult it (FR-03)", first.Accepted, second.Accepted) - } -} - -// RunLeaseAcquisitionRecordsNoHistoryEntry mirrors -// RunMetadataCASAWispSwapRecordsNoDurableHistory's delta-around-the-call -// shape (metadata_cas_contract.go, history_matching.go): acquiring an -// advisory lease is bookkeeping, not a durable state change, so it must -// record no history entry for its subject. -func RunLeaseAcquisitionRecordsNoHistoryEntry(t *testing.T, ctx context.Context, fixture CrossRecordInvariantFixture) { - t.Helper() - if fixture.AcquireAdvisoryLease == nil { - t.Skip("this backend has no lease primitive to test (AcquireAdvisoryLease is nil)") - } - if fixture.CountHistoryForSubject == nil { - t.Skip("this backend cannot observe history by subject (CountHistoryForSubject is nil)") - } - subject := fixture.IssuePrefix + "-crossrec-lease-history" - - before, err := fixture.CountHistoryForSubject(ctx, subject) - if err != nil { - t.Fatalf("CountHistoryForSubject(before): %v", err) - } - - crossRecordInvariantAcquireLease(t, ctx, fixture, subject, time.Minute) - - after, err := fixture.CountHistoryForSubject(ctx, subject) - if err != nil { - t.Fatalf("CountHistoryForSubject(after): %v", err) - } - if got := after - before; got != 0 { - t.Errorf("acquiring an advisory lease recorded %d history entries for %q, want none", got, subject) - } -} - -// --- fixture helpers ------------------------------------------------------- - -// crossRecordInvariantAcquireLease acquires an advisory lease and registers -// its Release (when the fixture provides one) as a test cleanup, so a case -// does not have to thread a defer through its own body. No caller needs the -// handle itself — both cases only need a lease held over the subject — so -// this does not return one. -func crossRecordInvariantAcquireLease(t *testing.T, ctx context.Context, fixture CrossRecordInvariantFixture, subject string, ttl time.Duration) { - t.Helper() - lease, err := fixture.AcquireAdvisoryLease(ctx, subject, ttl) - if err != nil { - t.Fatalf("AcquireAdvisoryLease(%s): %v", subject, err) - } - if lease.Release != nil { - t.Cleanup(func() { - if err := lease.Release(ctx); err != nil { - t.Errorf("releasing the advisory lease on %q: %v", subject, err) - } - }) - } -} diff --git a/backend/conformance/cycle_detector_contract.go b/backend/conformance/cycle_detector_contract.go index 32fe8dcb7..d6c60c7df 100644 --- a/backend/conformance/cycle_detector_contract.go +++ b/backend/conformance/cycle_detector_contract.go @@ -75,11 +75,11 @@ type SQLStatement struct { Args []any } -// RunCycleDetectorReportsNoCycleForAnAcyclicSubgraph pins cycledetector.go:171 +// RunCycleDetectorReportsNoCycleForAnAcyclicSubgraph pins cycledetector.go:135 // from the empty side: an acyclic graph is an empty report and a nil error, // with no ErrNotFound to classify. The nil-slice half IS asserted globally — // that one is a property of the answer, not of the workspace -// (cycledetector.go:108). +// (cycledetector.go:78). func RunCycleDetectorReportsNoCycleForAnAcyclicSubgraph(t *testing.T, ctx context.Context, fixture CycleDetectorFixture) { t.Helper() first := fixture.IssuePrefix + "-acyclic-a" @@ -98,10 +98,10 @@ func RunCycleDetectorReportsNoCycleForAnAcyclicSubgraph(t *testing.T, ctx contex } // RunCycleDetectorFindsADurableCycleRotatedToItsLowestID pins the shape of a -// found cycle: members in EDGE ORDER (cycledetector.go:78-80), rotated so the -// lowest id comes first (cycledetector.go:82-88), the closing edge implied +// found cycle: members in EDGE ORDER (cycledetector.go:48-50), rotated so the +// lowest id comes first (cycledetector.go:52-58), the closing edge implied // rather than repeated, and Partial false when every member was described -// (cycledetector.go:93). +// (cycledetector.go:63). // // The three ids are seeded so that the edge order and the sorted order are NOT // the same sequence, which is what makes the rotation assertion say something. @@ -138,8 +138,8 @@ func RunCycleDetectorFindsADurableCycleRotatedToItsLowestID(t *testing.T, ctx co } } -// RunCycleDetectorReportsTheSameCyclesEveryRun pins cycledetector.go:82-88 and -// :126-127: two calls against an unchanged database must agree, id for id and +// RunCycleDetectorReportsTheSameCyclesEveryRun pins cycledetector.go:52-58 and +// :91-92: two calls against an unchanged database must agree, id for id and // cycle for cycle. // // It compares the WHOLE report rather than the case's own cycle: the walk used @@ -175,7 +175,7 @@ func RunCycleDetectorReportsTheSameCyclesEveryRun(t *testing.T, ctx context.Cont } // RunCycleDetectorMergesTheDurableAndEphemeralPlanes pins -// cycledetector.go:156-158: the two dependency planes are one graph, so a cycle +// cycledetector.go:120-122: the two dependency planes are one graph, so a cycle // that runs issue → wisp → issue is found. This is the clause a single-table // read passes every other case and fails here. func RunCycleDetectorMergesTheDurableAndEphemeralPlanes(t *testing.T, ctx context.Context, fixture CycleDetectorFixture) { @@ -198,12 +198,10 @@ func RunCycleDetectorMergesTheDurableAndEphemeralPlanes(t *testing.T, ctx contex } } -// RunCycleDetectorFollowsOnlyBlockingEdges pins cycledetector.go:145-154: the +// RunCycleDetectorFollowsOnlyBlockingEdges pins cycledetector.go:110-119: the // walk follows `blocks` and `conditional-blocks`, and nothing else — not -// `waits-for`, whose gate semantics make a mutual wait legitimate, not -// `parent-child`, which the ADD-time gate does walk, and not `tracks`, on the -// DEFAULT (zero-value) request. IncludeTracks's own widened walk is pinned -// separately by RunCycleDetectorIncludeTracks*, below. +// `waits-for`, whose gate semantics make a mutual wait legitimate, and not +// `parent-child`, which the ADD-time gate does walk. // // The conditional-blocks half is asserted in the same case as the exclusions on // purpose: a body narrowed to `blocks` alone would pass an exclusion-only case, @@ -219,7 +217,6 @@ func RunCycleDetectorFollowsOnlyBlockingEdges(t *testing.T, ctx context.Context, {types.DepWaitsFor, "wait", false}, {types.DepParentChild, "kid", false}, {types.DepRelated, "rel", false}, - {types.DepTracks, "trk", false}, } { first := fmt.Sprintf("%s-edge-%s-a", fixture.IssuePrefix, test.slug) second := fmt.Sprintf("%s-edge-%s-b", fixture.IssuePrefix, test.slug) @@ -239,87 +236,8 @@ func RunCycleDetectorFollowsOnlyBlockingEdges(t *testing.T, ctx context.Context, } } -// RunCycleDetectorIncludeTracksIgnoresAPureTracksLoop pins -// DetectCyclesRequest.IncludeTracks's own guard: widening the walk to `tracks` -// must not resurrect the PR #4933 false-positive shape, where ordinary -// convoy/tracking topology with no scheduling edge at all got reported as a -// cycle. A loop made entirely of `tracks` edges stays silent even with -// IncludeTracks set. -func RunCycleDetectorIncludeTracksIgnoresAPureTracksLoop(t *testing.T, ctx context.Context, fixture CycleDetectorFixture) { - t.Helper() - first := fixture.IssuePrefix + "-trkloop-a" - second := fixture.IssuePrefix + "-trkloop-b" - seedCycleDetectorIssue(t, ctx, fixture, first) - seedCycleDetectorIssue(t, ctx, fixture, second) - seedCycleDetectorEdges(t, ctx, fixture, false, - cycleDetectorEdge{Source: first, Target: second, Type: types.DepTracks}, - cycleDetectorEdge{Source: second, Target: first, Type: types.DepTracks}) - - report := cycleDetectorReportIncludingTracks(t, ctx, fixture) - if found := cycleDetectorTouching(report, first, second); len(found) > 0 { - t.Errorf("a mutual tracks-only pair produced %v under IncludeTracks, want none: a cycle needs at least one scheduling edge", found) - } -} - -// RunCycleDetectorIncludeTracksFindsTheMoleculeRootShape pins the gc-818bx -// deadlock this option exists to surface: a molecule root that -// blocks-depends on its own entry step, which tracks-depends back to the -// root. Neither edge alone is a cycle; the shape only closes across both edge -// types, which is exactly what the default (blocks-only) walk cannot see. -func RunCycleDetectorIncludeTracksFindsTheMoleculeRootShape(t *testing.T, ctx context.Context, fixture CycleDetectorFixture) { - t.Helper() - root := fixture.IssuePrefix + "-molroot" - step := fixture.IssuePrefix + "-molstep" - seedCycleDetectorIssue(t, ctx, fixture, root) - seedCycleDetectorIssue(t, ctx, fixture, step) - seedCycleDetectorEdges(t, ctx, fixture, false, - cycleDetectorEdge{Source: root, Target: step, Type: types.DepBlocks}, - cycleDetectorEdge{Source: step, Target: root, Type: types.DepTracks}) - - // The default request must still see nothing: the closing edge is a - // tracks edge, outside the base walk. - if found := cycleDetectorTouching(cycleDetectorReport(t, ctx, fixture), root, step); len(found) > 0 { - t.Errorf("the default request reported %v; IncludeTracks defaults to false and must leave the base walk unchanged", found) - } - - cycle := cycleDetectorFind(t, cycleDetectorReportIncludingTracks(t, ctx, fixture), root, step) - assertCycleDetectorPath(t, cycle, root, step) -} - -// RunCycleDetectorIncludeTracksWalksEachEdgeInItsStoredDirection pins the -// direction of the widened graph, which the two-node cases above cannot: a -// two-node cycle reads the same reversed once it is rotated to its lowest id. -// This loop has three nodes in edge order a, c, b. Reversed it is a, b, c, the -// same member set in the other order, so a graph builder that stored -// depends_on_id -> issue_id would still find exactly one cycle here and fail -// only on the path. -// -// The blocks edge sits in the middle of the loop and the closing edges are -// tracks, so the default request must see nothing. -func RunCycleDetectorIncludeTracksWalksEachEdgeInItsStoredDirection(t *testing.T, ctx context.Context, fixture CycleDetectorFixture) { - t.Helper() - a := fixture.IssuePrefix + "-trkdir-a" - b := fixture.IssuePrefix + "-trkdir-b" - c := fixture.IssuePrefix + "-trkdir-c" - for _, id := range []string{a, b, c} { - seedCycleDetectorIssue(t, ctx, fixture, id) - } - // a -> c -> b -> a. - seedCycleDetectorEdges(t, ctx, fixture, false, - cycleDetectorEdge{Source: a, Target: c, Type: types.DepTracks}, - cycleDetectorEdge{Source: c, Target: b, Type: types.DepBlocks}, - cycleDetectorEdge{Source: b, Target: a, Type: types.DepTracks}) - - if found := cycleDetectorTouching(cycleDetectorReport(t, ctx, fixture), a, b, c); len(found) > 0 { - t.Errorf("the default request reported %v; two of the three edges are tracks, outside the base walk", found) - } - - cycle := cycleDetectorFind(t, cycleDetectorReportIncludingTracks(t, ctx, fixture), a, b, c) - assertCycleDetectorPath(t, cycle, a, c, b) -} - -// RunCycleDetectorReportsAnHonestPartial pins cycledetector.go:176-180 and -// :54-63 against real storage: a member the database cannot describe keeps its +// RunCycleDetectorReportsAnHonestPartial pins cycledetector.go:140-144 and +// :24-33 against real storage: a member the database cannot describe keeps its // place on the path, carries no issue, and marks the cycle. The previous body // dropped the member, so this three-node cycle came back as a TWO-node cycle and // looked complete. @@ -354,7 +272,7 @@ func RunCycleDetectorReportsAnHonestPartial(t *testing.T, ctx context.Context, f } } -// RunCycleDetectorCountsAWhollyUndescribableCycle pins cycledetector.go:111-115: +// RunCycleDetectorCountsAWhollyUndescribableCycle pins cycledetector.go:81-85: // a cycle no member of which can be described is still IN the report, so the // count cannot shrink because rows went missing. Under the previous body this // cycle produced nothing at all, so `bd dep cycles` printed a smaller number and @@ -379,7 +297,7 @@ func RunCycleDetectorCountsAWhollyUndescribableCycle(t *testing.T, ctx context.C } } -// RunCycleDetectorWritesNothing pins cycledetector.go:166-167: detecting is a +// RunCycleDetectorWritesNothing pins cycledetector.go:130-131: detecting is a // read. It is asserted on the history log rather than on a row read-back // because every versioned unit of work in this tree ends in a Dolt commit, so a // sweep that took a write transaction would show up here even when it changed @@ -515,17 +433,6 @@ func cycleDetectorReport(t *testing.T, ctx context.Context, fixture CycleDetecto return report } -// cycleDetectorReportIncludingTracks is cycleDetectorReport with -// IncludeTracks set, for the cases pinning that option's own widened walk. -func cycleDetectorReportIncludingTracks(t *testing.T, ctx context.Context, fixture CycleDetectorFixture) publicops.CycleReport { - t.Helper() - report, err := fixture.Detector.DetectCycles(ctx, publicops.DetectCyclesRequest{IncludeTracks: true}) - if err != nil { - t.Fatalf("DetectCycles: %v", err) - } - return report -} - // cycleDetectorPaths renders a report as id paths, which is what the // determinism case compares. func cycleDetectorPaths(report publicops.CycleReport) [][]string { diff --git a/backend/conformance/dependency_editor_contract.go b/backend/conformance/dependency_editor_contract.go index 4f5b33268..cc8270086 100644 --- a/backend/conformance/dependency_editor_contract.go +++ b/backend/conformance/dependency_editor_contract.go @@ -2,12 +2,10 @@ package conformance import ( "context" - "encoding/json" "errors" "fmt" "testing" - storeops "github.com/steveyegge/beads/internal/storage/issueops" "github.com/steveyegge/beads/internal/types" publicops "github.com/steveyegge/beads/issueops" ) @@ -62,33 +60,6 @@ type DependencyEditorFixture struct { // the cases that need it then skip with that reason rather than pass // quietly. It is non-nil on all three backends today. CountHistory func(context.Context) (int, error) - // SetJournalEnabled turns on durable journaling for the workspace under - // test (storage.EventsJournalConfigurer) — the same operator switch - // JournalFixture.SetJournalEnabled wires, and off by default for the same - // reason: a workspace that never opted in records nothing in - // bd_events_journal while every other write still succeeds normally. - // - // The idempotency cases need it on because bd_events_journal has no - // deduplication of its own (issueops.RecordDepEventInTx always inserts): - // a same-type re-add or an absent removal is a no-op only if nothing - // downstream of the role recorded a change, and the journal is the one - // place that is not otherwise observable through this fixture. It is - // non-nil on all three backends. - SetJournalEnabled func(enabled bool) - // SetVersionedHistoryEnabled turns on dual-write issue-version history - // (storage.VersionedHistoryConfigurer) — off by default for the same - // reason SetJournalEnabled is. - // - // It exists for one case: the metadata-changed re-add, which can only be - // driven through AddDependency (the role's own request type carries no - // metadata). That hook writes through the backend's raw store path rather - // than the role's transaction wrapper, and on at least one backend only - // the latter issues the underlying VCS commit CountHistory observes — so - // a case reached through AddDependency cannot tell a mint happened by - // watching CountHistory the way the role-driven history cases do. It has - // to read issue_versions directly instead, which only advances when this - // switch is on. It is non-nil on all three backends. - SetVersionedHistoryEnabled func(enabled bool) } // RunDependencyEditorRoutesWispSourcedEdgeToTheWispPlane is the regression pin @@ -281,18 +252,8 @@ func RunDependencyEditorAddedEchoesTheRequestOrder(t *testing.T, ctx context.Con // promises a dependency_added entry for a GENUINELY NEW edge, so a backend // that re-emitted on the no-op would leave a history of work that did not // happen even where the row count could not tell. -// -// bd_events_journal is the fourth, and the one none of the others catch: -// unlike the events table it has no deduplication of its own -// (issueops.RecordDepEventInTx always inserts), so a same-type re-add that -// gates on TYPE alone — never the stored metadata VALUE — journals a change -// that never happened even while the row count, the events table and the -// history delta all read as a clean no-op. This is #5898's R3 case: a -// change-free write must create no version and must not journal one either. func RunDependencyEditorSameTypeReAddIsIdempotent(t *testing.T, ctx context.Context, fixture DependencyEditorFixture) { t.Helper() - fixture.SetJournalEnabled(true) - t.Cleanup(func() { fixture.SetJournalEnabled(false) }) source := fixture.IssuePrefix + "-idem-source" target := fixture.IssuePrefix + "-idem-target" seedDependencyEditorIssue(t, ctx, fixture, source) @@ -306,7 +267,6 @@ func RunDependencyEditorSameTypeReAddIsIdempotent(t *testing.T, ctx context.Cont t.Fatalf("AddDependencies first: %v", err) } assertDependencyEditorEventCount(t, ctx, fixture, "events", source, types.EventDependencyAdded, 1) - assertDependencyEditorJournalCountIsOne(t, ctx, fixture, source, string(storeops.EventDepAdd)) assertHistoryDelta := dependencyEditorHistoryProbe(t, ctx, fixture) result, err := fixture.Editor.AddDependencies(ctx, request) @@ -319,308 +279,9 @@ func RunDependencyEditorSameTypeReAddIsIdempotent(t *testing.T, ctx context.Cont assertDependencyEditorOutgoingCount(t, ctx, fixture, "dependencies", source, 1) assertDependencyEdgeTypedCount(t, ctx, fixture, "dependencies", source, target, string(publicops.DepBlocks), 1) assertDependencyEditorEventCount(t, ctx, fixture, "events", source, types.EventDependencyAdded, 1) - assertDependencyEditorJournalCountIsOne(t, ctx, fixture, source, string(storeops.EventDepAdd)) assertHistoryDelta(0, "every edge of the request already existed with the requested type, so nothing was written and nothing is versioned") } -// RunDependencyEditorSameTypeReAddWithChangedMetadataMintsOneVersion pins the -// other half of the same-type re-add clause from -// RunDependencyEditorSameTypeReAddIsIdempotent: same TYPE is not the whole -// idempotency test, because the row also carries metadata, and a re-add that -// changes it is a real mutation of the source issue rather than a no-op that -// happens to share a type. The leaf updates the stored metadata and mints -// EXACTLY ONE version carrying the new state — the same terms as a genuinely -// new edge — because from the source issue's point of view a metadata change -// is as real as the edge appearing in the first place (#5898 leg 2). -// -// It drives the re-add through fixture.AddDependency rather than -// fixture.Editor.AddDependencies because publicops.DependencyEdge carries no -// metadata for the role API to change — the same reason -// RunDependencyEditorClosedChildAddSatisfiesAnAnyChildrenGate reaches past -// the role for a waits-for gate. -// -// bd_events_journal is asserted at TWO, not one: unlike the change-free -// re-add, this path still calls issueops.RecordDepEventInTx — dependencies.go -// calls it "an observable graph mutation" and journals the complete -// replacement edge for replay — so the second write adds its own journal -// entry even though it adds no second row to "dependencies" and no second -// dependency_added row to "events", because those two describe the STORED -// ROW and the AUDIT TRAIL, neither of which duplicates on a metadata-only -// change. -func RunDependencyEditorSameTypeReAddWithChangedMetadataMintsOneVersion(t *testing.T, ctx context.Context, fixture DependencyEditorFixture) { - t.Helper() - if fixture.AddDependency == nil { - t.Skip("fixture has no AddDependency hook: metadata is not settable through publicops.DependencyEdge") - } - fixture.SetJournalEnabled(true) - t.Cleanup(func() { fixture.SetJournalEnabled(false) }) - fixture.SetVersionedHistoryEnabled(true) - t.Cleanup(func() { fixture.SetVersionedHistoryEnabled(false) }) - source := fixture.IssuePrefix + "-idemmeta-source" - target := fixture.IssuePrefix + "-idemmeta-target" - seedDependencyEditorIssue(t, ctx, fixture, source) - seedDependencyEditorIssue(t, ctx, fixture, target) - - first := &types.Dependency{IssueID: source, DependsOnID: target, Type: types.DepBlocks, Metadata: `{"note":"v1"}`} - if err := fixture.AddDependency(ctx, first, "writer"); err != nil { - t.Fatalf("AddDependency first: %v", err) - } - assertDependencyEditorOutgoingCount(t, ctx, fixture, "dependencies", source, 1) - assertDependencyEdgeTypedCount(t, ctx, fixture, "dependencies", source, target, string(types.DepBlocks), 1) - assertDependencyEditorEventCount(t, ctx, fixture, "events", source, types.EventDependencyAdded, 1) - assertDependencyEditorJournalCountIsOne(t, ctx, fixture, source, string(storeops.EventDepAdd)) - - versionsBefore := countDependencyEditorIssueVersions(t, ctx, fixture, source) - second := &types.Dependency{IssueID: source, DependsOnID: target, Type: types.DepBlocks, Metadata: `{"note":"v2"}`} - if err := fixture.AddDependency(ctx, second, "writer"); err != nil { - t.Fatalf("re-adding the same edge with changed metadata refused: %v", err) - } - assertDependencyEditorOutgoingCount(t, ctx, fixture, "dependencies", source, 1) - assertDependencyEdgeTypedCount(t, ctx, fixture, "dependencies", source, target, string(types.DepBlocks), 1) - assertDependencyEditorEventCount(t, ctx, fixture, "events", source, types.EventDependencyAdded, 1) - - var journalCount int - if err := fixture.QueryScalar(ctx, - "SELECT COUNT(*) FROM bd_events_journal WHERE issue_id = ? AND op = ?", - []any{source, string(storeops.EventDepAdd)}, &journalCount); err != nil { - t.Fatalf("count bd_events_journal dep_add rows for %s: %v", source, err) - } - if journalCount != 2 { - t.Errorf("bd_events_journal dep_add rows for %s = %d, want 2: the metadata-changed re-add must journal its own replacement-edge entry", source, journalCount) - } - - var storedMetadata string - if err := fixture.QueryScalar(ctx, - "SELECT metadata FROM dependencies WHERE issue_id = ? AND COALESCE(depends_on_issue_id, depends_on_wisp_id, depends_on_external) = ?", - []any{source, target}, &storedMetadata); err != nil { - t.Fatalf("read stored metadata for %s -> %s: %v", source, target, err) - } - // Compared after re-encoding rather than byte-for-byte: a native JSON - // column is free to re-canonicalize what it stores (e.g. inserting the - // space after ':' Dolt's does), and that is not the thing this case is - // about. canonicalizeForContract has no numeric blind spot here because - // this fixture's metadata values never carry a bare JSON number. - storedCanon, err := canonicalizeForContract(json.RawMessage(storedMetadata)) - if err != nil { - t.Fatalf("canonicalize stored metadata for %s -> %s: %v", source, target, err) - } - wantCanon, err := canonicalizeForContract(json.RawMessage(second.Metadata)) - if err != nil { - t.Fatalf("canonicalize want metadata for %s -> %s: %v", source, target, err) - } - if storedCanon != wantCanon { - t.Errorf("stored metadata = %s, want %s: the re-add must persist the NEW metadata, not keep the old", storedCanon, wantCanon) - } - - // Not dependencyEditorHistoryProbe/CountHistory: AddDependency seeds this - // edge out of band of the role, through the backend's raw store path, and - // on at least one backend that path never issues the version-control - // commit CountHistory counts — only the role's own transaction wrapper - // does. issue_versions is the seam both paths mint through, so it is the - // one signal that is meaningful for a write reached this way. - if delta := countDependencyEditorIssueVersions(t, ctx, fixture, source) - versionsBefore; delta != 1 { - t.Errorf("issue_versions rows for %s went %d -> %d (delta %d), want a delta of 1: the metadata genuinely changed, so the re-add is a real mutation of the source issue and must mint exactly one version (#5898 leg 2)", source, versionsBefore, versionsBefore+delta, delta) - } -} - -// countDependencyEditorIssueVersions reports how many issue_versions rows -// exist for the given issue. Shared by every case here that asserts a -// version DELTA rather than an absolute count: the fixture's issues accrue -// versions from more than this one seam, so the relative change across an -// operation is the only thing a case can rely on. -func countDependencyEditorIssueVersions(t *testing.T, ctx context.Context, fixture DependencyEditorFixture, id string) int { - t.Helper() - var count int - if err := fixture.QueryScalar(ctx, "SELECT COUNT(*) FROM issue_versions WHERE issue_id = ?", []any{id}, &count); err != nil { - t.Fatalf("count issue_versions rows for %s: %v", id, err) - } - return count -} - -// RunDependencyEditorSameTypeReAddWithIdenticalMetadataIsANoOp pins the leg of -// the same-type re-add clause that -// RunDependencyEditorSameTypeReAddWithChangedMetadataMintsOneVersion -// deliberately does not cover: metadata that is byte-different but -// SEMANTICALLY IDENTICAL to what is already stored. A native JSON column is -// free to re-canonicalize what it persists — Dolt's inserts a space after -// ':', sorts object keys, and narrows a bare integral float to an int — so a -// comparison written against the raw bytes the caller last sent, rather than -// a canonical form of what is actually stored, reports a change that never -// happened and mints a version for a write that changed nothing (#6650). -// -// Two sub-cases pin the two distinct ways "byte-different" can still mean -// "the same value", using the same real-world shape -// (internal/types.WaitsForMeta, which is what a waits-for edge's metadata -// actually is): -// - CompactSpelling re-adds the exact compact spelling -// json.Marshal(types.WaitsForMeta{Gate: "any-children"}) already -// produced. The caller sends identical bytes both times; only Dolt's own -// storage-side renormalization of the FIRST write can make the stored -// form disagree with them. -// - ReorderedKeys re-adds the same fields with their keys spelled in a -// different order — a caller-side difference no byte comparison -// survives, but the same JSON value all the same. -// -// Both must be a no-op by the same signals -// RunDependencyEditorSameTypeReAddWithChangedMetadataMintsOneVersion checks -// for a real change: the stored row, the events table, the journal, and the -// issue_versions delta all have to read as "nothing happened." -func RunDependencyEditorSameTypeReAddWithIdenticalMetadataIsANoOp(t *testing.T, ctx context.Context, fixture DependencyEditorFixture) { - t.Helper() - if fixture.AddDependency == nil { - t.Skip("fixture has no AddDependency hook: metadata is not settable through publicops.DependencyEdge") - } - - compact, err := json.Marshal(types.WaitsForMeta{Gate: "any-children"}) - if err != nil { - t.Fatalf("marshal compact WaitsForMeta: %v", err) - } - - for _, sub := range []struct { - name string - tag string - first string - readd string - }{ - { - name: "CompactSpelling", - tag: "idemmetasame-compact", - first: string(compact), - readd: string(compact), - }, - { - name: "ReorderedKeys", - tag: "idemmetasame-reorder", - first: `{"gate":"any-children","spawner_id":"src-1"}`, - readd: `{"spawner_id":"src-1","gate":"any-children"}`, - }, - } { - t.Run(sub.name, func(t *testing.T) { - fixture.SetJournalEnabled(true) - t.Cleanup(func() { fixture.SetJournalEnabled(false) }) - fixture.SetVersionedHistoryEnabled(true) - t.Cleanup(func() { fixture.SetVersionedHistoryEnabled(false) }) - source := fixture.IssuePrefix + "-" + sub.tag + "-source" - target := fixture.IssuePrefix + "-" + sub.tag + "-target" - seedDependencyEditorIssue(t, ctx, fixture, source) - seedDependencyEditorIssue(t, ctx, fixture, target) - - first := &types.Dependency{IssueID: source, DependsOnID: target, Type: types.DepBlocks, Metadata: sub.first} - if err := fixture.AddDependency(ctx, first, "writer"); err != nil { - t.Fatalf("AddDependency first: %v", err) - } - assertDependencyEditorOutgoingCount(t, ctx, fixture, "dependencies", source, 1) - assertDependencyEdgeTypedCount(t, ctx, fixture, "dependencies", source, target, string(types.DepBlocks), 1) - assertDependencyEditorEventCount(t, ctx, fixture, "events", source, types.EventDependencyAdded, 1) - assertDependencyEditorJournalCountIsOne(t, ctx, fixture, source, string(storeops.EventDepAdd)) - - versionsBefore := countDependencyEditorIssueVersions(t, ctx, fixture, source) - second := &types.Dependency{IssueID: source, DependsOnID: target, Type: types.DepBlocks, Metadata: sub.readd} - if err := fixture.AddDependency(ctx, second, "writer"); err != nil { - t.Fatalf("re-adding the same edge with semantically identical metadata refused: %v", err) - } - assertDependencyEditorOutgoingCount(t, ctx, fixture, "dependencies", source, 1) - assertDependencyEdgeTypedCount(t, ctx, fixture, "dependencies", source, target, string(types.DepBlocks), 1) - assertDependencyEditorEventCount(t, ctx, fixture, "events", source, types.EventDependencyAdded, 1) - assertDependencyEditorJournalCountIsOne(t, ctx, fixture, source, string(storeops.EventDepAdd)) - - if delta := countDependencyEditorIssueVersions(t, ctx, fixture, source) - versionsBefore; delta != 0 { - t.Errorf("issue_versions rows for %s went %d -> %d (delta %d), want a delta of 0: the re-add's metadata is semantically identical to what is already stored (byte differences aside), so nothing changed and nothing should be versioned (#6650)", source, versionsBefore, versionsBefore+delta, delta) - } - }) - } -} - -// RunDependencyEditorSameTypeReAddWithChangedThreadMintsOneVersion is the -// thread_id twin of -// RunDependencyEditorSameTypeReAddWithChangedMetadataMintsOneVersion. The row -// carries a thread as well as metadata, and the source issue's version snapshot -// carries each edge's thread_id, so a same-type re-add naming a thread the row -// does not carry is a real mutation on the same terms as a metadata change: -// the leaf updates the row in place, journals the replacement edge, and mints -// EXACTLY ONE version (#5898 leg 2). The re-add arm once compared metadata -// alone, which read that re-add as change-free and dropped the thread. -// -// The metadata is identical across every write, so the thread is the only -// thing that moves. A last re-add names no thread, and is the change-free -// no-op: an empty thread names none rather than asking for none, which is -// what keeps the callers that never carry a thread from erasing one. -// -// It drives the writes through fixture.AddDependency for the reason the -// metadata case gives: publicops.DependencyEdge carries no thread either. -func RunDependencyEditorSameTypeReAddWithChangedThreadMintsOneVersion(t *testing.T, ctx context.Context, fixture DependencyEditorFixture) { - t.Helper() - if fixture.AddDependency == nil { - t.Skip("fixture has no AddDependency hook: a thread is not settable through publicops.DependencyEdge") - } - fixture.SetJournalEnabled(true) - t.Cleanup(func() { fixture.SetJournalEnabled(false) }) - fixture.SetVersionedHistoryEnabled(true) - t.Cleanup(func() { fixture.SetVersionedHistoryEnabled(false) }) - source := fixture.IssuePrefix + "-idemthread-source" - target := fixture.IssuePrefix + "-idemthread-target" - seedDependencyEditorIssue(t, ctx, fixture, source) - seedDependencyEditorIssue(t, ctx, fixture, target) - - const metadata = `{"note":"v1"}` - add := func(thread, refusal string) { - t.Helper() - dep := &types.Dependency{IssueID: source, DependsOnID: target, Type: types.DepBlocks, Metadata: metadata, ThreadID: thread} - if err := fixture.AddDependency(ctx, dep, "writer"); err != nil { - t.Fatalf("%s: %v", refusal, err) - } - assertDependencyEdgeTypedCount(t, ctx, fixture, "dependencies", source, target, string(types.DepBlocks), 1) - assertDependencyEditorEventCount(t, ctx, fixture, "events", source, types.EventDependencyAdded, 1) - } - storedThread := func() string { - t.Helper() - var thread string - if err := fixture.QueryScalar(ctx, - "SELECT COALESCE(thread_id, '') FROM dependencies WHERE issue_id = ? AND COALESCE(depends_on_issue_id, depends_on_wisp_id, depends_on_external) = ?", - []any{source, target}, &thread); err != nil { - t.Fatalf("read stored thread_id for %s -> %s: %v", source, target, err) - } - return thread - } - journalCount := func() int { - t.Helper() - var count int - if err := fixture.QueryScalar(ctx, - "SELECT COUNT(*) FROM bd_events_journal WHERE issue_id = ? AND op = ?", - []any{source, string(storeops.EventDepAdd)}, &count); err != nil { - t.Fatalf("count bd_events_journal dep_add rows for %s: %v", source, err) - } - return count - } - - add("", "AddDependency first") - assertDependencyEditorJournalCountIsOne(t, ctx, fixture, source, string(storeops.EventDepAdd)) - - versionsBefore := countDependencyEditorIssueVersions(t, ctx, fixture, source) - add("thread-conv-1", "re-adding the same edge with a new thread refused") - if got := storedThread(); got != "thread-conv-1" { - t.Errorf("stored thread_id = %q, want %q: the re-add must persist the new thread, not keep the old", got, "thread-conv-1") - } - if got := journalCount(); got != 2 { - t.Errorf("bd_events_journal dep_add rows for %s = %d, want 2: the thread-changed re-add must journal its own replacement-edge entry", source, got) - } - if delta := countDependencyEditorIssueVersions(t, ctx, fixture, source) - versionsBefore; delta != 1 { - t.Errorf("issue_versions rows for %s went %d -> %d (delta %d), want a delta of 1: the thread genuinely changed, so the re-add is a real mutation of the source issue and must mint exactly one version (#5898 leg 2)", source, versionsBefore, versionsBefore+delta, delta) - } - - versionsBefore = countDependencyEditorIssueVersions(t, ctx, fixture, source) - add("", "re-adding the same edge naming no thread refused") - if got := storedThread(); got != "thread-conv-1" { - t.Errorf("stored thread_id after a re-add naming no thread = %q, want %q kept", got, "thread-conv-1") - } - if got := journalCount(); got != 2 { - t.Errorf("bd_events_journal dep_add rows for %s = %d after a change-free re-add, want 2 still", source, got) - } - if delta := countDependencyEditorIssueVersions(t, ctx, fixture, source) - versionsBefore; delta != 0 { - t.Errorf("issue_versions rows for %s went %d -> %d (delta %d) on a re-add naming no thread, want a delta of 0: it keeps the stored thread, so nothing changed (#5898 R3)", source, versionsBefore, versionsBefore+delta, delta) - } -} - // RunDependencyEditorRepeatsWithinOneRequestCollapse pins the clause that // answers a request naming ONE PAIR TWICE. The same-type re-add rule is stated // for the pair rather than for the call, so the second occurrence inside a @@ -793,17 +454,8 @@ func RunDependencyEditorRefusalWritesNothing(t *testing.T, ctx context.Context, // missing edge is Removed false with a NIL error, not ErrNotFound, because an // agent replaying its own teardown should not have to classify an error to // discover it already ran. -// bd_events_journal is checked alongside the events table for the same -// reason RunDependencyEditorSameTypeReAddIsIdempotent checks it: the journal -// has no deduplication of its own, so a removal that finds nothing must not -// journal a dep_remove either. This side of #5898's R3 case is expected to -// already hold — RemoveDependencyInTx only records when it actually deleted a -// row — but it is pinned here so a future regression on this leg is caught -// alongside the add-side one rather than only on the leg that is broken today. func RunDependencyEditorRemoveIsIdempotent(t *testing.T, ctx context.Context, fixture DependencyEditorFixture) { t.Helper() - fixture.SetJournalEnabled(true) - t.Cleanup(func() { fixture.SetJournalEnabled(false) }) source := fixture.IssuePrefix + "-rm-source" target := fixture.IssuePrefix + "-rm-target" seedDependencyEditorIssue(t, ctx, fixture, source) @@ -826,7 +478,6 @@ func RunDependencyEditorRemoveIsIdempotent(t *testing.T, ctx context.Context, fi } assertDependencyEditorOutgoingCount(t, ctx, fixture, "dependencies", source, 0) assertDependencyEditorEventCount(t, ctx, fixture, "events", source, types.EventDependencyRemoved, 1) - assertDependencyEditorJournalCountIsOne(t, ctx, fixture, source, string(storeops.EventDepRemove)) removed, err = fixture.Editor.RemoveDependency(ctx, request) if err != nil { @@ -836,7 +487,6 @@ func RunDependencyEditorRemoveIsIdempotent(t *testing.T, ctx context.Context, fi t.Error("Removed = true, want false for an edge that was already gone") } assertDependencyEditorEventCount(t, ctx, fixture, "events", source, types.EventDependencyRemoved, 1) - assertDependencyEditorJournalCountIsOne(t, ctx, fixture, source, string(storeops.EventDepRemove)) } // RunDependencyEditorAppliesParentChildBeforeBlockingEdges pins @@ -2529,26 +2179,6 @@ func assertDependencyEditorEventCount(t *testing.T, ctx context.Context, fixture } } -// assertDependencyEditorJournalCountIsOne asserts exactly one bd_events_journal -// row exists for one issue and op — the same shape as -// assertDependencyEditorEventCount, against the table that has no -// deduplication of its own and so is the one an unconditional journal write on -// a no-op would actually show up in. Every call site pins the count at one -// (the single genuine write); there is no idempotency case that expects zero -// or more than one, so unlike assertDependencyEditorEventCount this does not -// take a want. -func assertDependencyEditorJournalCountIsOne(t *testing.T, ctx context.Context, fixture DependencyEditorFixture, issueID, op string) { - t.Helper() - var got int - query := "SELECT COUNT(*) FROM bd_events_journal WHERE issue_id = ? AND op = ?" - if err := fixture.QueryScalar(ctx, query, []any{issueID, op}, &got); err != nil { - t.Fatalf("count bd_events_journal %s rows for %s: %v", op, issueID, err) - } - if got != 1 { - t.Errorf("bd_events_journal %s rows for %s = %d, want 1", op, issueID, got) - } -} - // assertDependencyEditorEventActor reads the actor off the one event of a // given type for one issue. It reads the DURABLE stream only: the attribution // clause is about who is recorded, and the plane that record lands in is @@ -2631,82 +2261,3 @@ func dependencyEditorHistoryProbe(t *testing.T, ctx context.Context, fixture Dep assertDependencyEditorHistoryDelta(t, ctx, fixture, before, want, why) } } - -// RunDependencyEditorRefusesADottedChildGatedOnItsOwnParent pins -// publicops.CheckDottedChildDependency on the role: an explicit edge from a -// dotted-id child to its own ancestor is refused as a -// *DottedChildDependencyError (ErrValidation) on every leg, and nothing is -// written. (It is decided before any per-edge probe, so the probe-skipped -// spelling, which the wire does not publish, adds nothing here.) It used to be -// enforced only by the CLI ahead of the role, so an HTTP or embedded caller -// could gate bd-abc.1 on bd-abc. -// -// POSITIVE HALF: the hierarchy itself — a parent-child edge to the IMMEDIATE -// dotted parent — is allowed, and so is an edge between siblings. A body that -// refused every edge touching a dotted id would satisfy the refusals alone. -// -// WITH THE HIERARCHY STORED: once that parent-child edge exists (the shape -// `bd create --parent` leaves), a blocking edge on the same pair is still this -// refusal, not the type or hierarchy conflict the stored edge would raise — the -// rule reads nothing, so it answers first — and the stored edge survives. -func RunDependencyEditorRefusesADottedChildGatedOnItsOwnParent(t *testing.T, ctx context.Context, fixture DependencyEditorFixture) { - t.Helper() - parent := fixture.IssuePrefix + "-dotted" - child := parent + ".1" - grandchild := child + ".2" - sibling := parent + ".3" - for _, id := range []string{parent, child, grandchild, sibling} { - seedDependencyEditorIssue(t, ctx, fixture, id) - } - - for _, refused := range []struct { - name string - edge publicops.DependencyEdge - }{ - {"child blocks on its parent", publicops.DependencyEdge{IssueID: child, DependsOnID: parent, Type: publicops.DepBlocks}}, - {"grandchild blocks on its grandparent", publicops.DependencyEdge{IssueID: grandchild, DependsOnID: parent, Type: publicops.DepBlocks}}, - {"grandchild parent-child to its grandparent", publicops.DependencyEdge{IssueID: grandchild, DependsOnID: parent, Type: publicops.DepParentChild}}, - {"child relates to its parent", publicops.DependencyEdge{IssueID: child, DependsOnID: parent, Type: publicops.DepRelated}}, - } { - _, err := fixture.Editor.AddDependencies(ctx, publicops.AddDependenciesRequest{ - Actor: "writer", - Edges: []publicops.DependencyEdge{refused.edge}, - }) - var dotted *publicops.DottedChildDependencyError - if !errors.As(err, &dotted) { - t.Errorf("%s: error = %v, want *DottedChildDependencyError", refused.name, err) - continue - } - if !errors.Is(err, publicops.ErrValidation) { - t.Errorf("%s: error = %v, want it to match ErrValidation", refused.name, err) - } - if dotted.IssueID != refused.edge.IssueID || dotted.DependsOnID != refused.edge.DependsOnID { - t.Errorf("%s: refusal names %s -> %s, want %s -> %s", refused.name, - dotted.IssueID, dotted.DependsOnID, refused.edge.IssueID, refused.edge.DependsOnID) - } - } - assertDependencyEditorNoEdgesFrom(t, ctx, fixture, child, grandchild) - - if _, err := fixture.Editor.AddDependencies(ctx, publicops.AddDependenciesRequest{ - Actor: "writer", - Edges: []publicops.DependencyEdge{ - {IssueID: grandchild, DependsOnID: child, Type: publicops.DepParentChild}, - {IssueID: sibling, DependsOnID: child, Type: publicops.DepBlocks}, - }, - }); err != nil { - t.Fatalf("the immediate parent-child edge and a sibling edge: %v", err) - } - assertDependencyEdgeTypedCount(t, ctx, fixture, "dependencies", grandchild, child, string(publicops.DepParentChild), 1) - assertDependencyEdgeCount(t, ctx, fixture, "dependencies", sibling, child, 1) - - _, err := fixture.Editor.AddDependencies(ctx, publicops.AddDependenciesRequest{ - Actor: "writer", - Edges: []publicops.DependencyEdge{{IssueID: grandchild, DependsOnID: child, Type: publicops.DepBlocks}}, - }) - var dotted *publicops.DottedChildDependencyError - if !errors.As(err, &dotted) || !errors.Is(err, publicops.ErrValidation) { - t.Errorf("blocks on a parent whose parent-child edge is stored: error = %v, want *DottedChildDependencyError matching ErrValidation", err) - } - assertDependencyEdgeTypedCount(t, ctx, fixture, "dependencies", grandchild, child, string(publicops.DepParentChild), 1) - assertDependencyEdgeCount(t, ctx, fixture, "dependencies", grandchild, child, 1) -} diff --git a/backend/conformance/dualwrite_history_contract.go b/backend/conformance/dualwrite_history_contract.go deleted file mode 100644 index e94a15d01..000000000 --- a/backend/conformance/dualwrite_history_contract.go +++ /dev/null @@ -1,346 +0,0 @@ -package conformance - -import ( - "context" - "testing" -) - -// This file holds the contract Phase 2's dual-write history mechanism must -// satisfy: FR-1, FR-2, FR-3, FR-5 and FR-7 of be-hs42e.3 (gastownhall/beads#6135) -// directly, plus FR-6 per leg (see below). FR-4 and FR-8 are this phase's -// requirements too but are not testable through this fixture's five closures -// — see the exclusion list below for why. FR-9 is deliberately NOT this -// file's job: it is the differential test harness task 4 builds separately -// (flag-off-vs-recorded-baseline plus flag-on-perturbs-nothing-but- -// current_revision), not a property a per-mutation fixture case can express. -// This is the phase that starts populating -// issue_versions/current_revision/store_epoch behind -// storage.VersionedHistoryConfigurer while every read path still answers from -// the durable issues/wisps rows exactly as before. Nothing here asserts that a -// dual-written value is ever READ back through a query surface — that CAS -// enforcement and read-path switch is Phase 3/4 (#6136), unbuilt at this -// phase — only that the write side mints what it promises, and only when it -// promises to. -// -// WHY THIS IS A DEDICATED FIXTURE RATHER THAN AN EXTENSION OF AN EXISTING ONE. -// An earlier design iteration proposed folding these cases into the -// pre-existing RetentionFixture/CrossRecordInvariantFixture pair. That -// approach was withdrawn (be-q0w1q) after review surfaced a version-row -// identity gap those fixtures' cases would have exercised before it was -// resolved: (issue_id, revision) was the only key issue_versions had, which -// cannot express a third write racing the same revision the way a -// content-addressed or generated identity can. be-hs42e.3 design §15 -// resolves the identity question with a dedicated version_id (migration -// 0068, landing after this contract exists — see the task-1 half of this -// bead), and this contract is written against §15's resolved shape, not the -// one the withdrawn approach would have extended. -// -// THE FIXTURE'S FIVE CLOSURES ARE THE OBSERVABLE SURFACE, NOT THE SCHEMA. A -// case here never reads issue_versions or store_epoch directly — that would -// make the contract a second copy of one leg's SQL rather than a promise -// every leg keeps in its own idiom (raw SQL against the dolt leg's own -// *sql.DB, an exported accessor on the embedded store, RawSQLUseCase through -// a unit of work). Mutate and MutateAsNoOp drive the same front door every -// other conformance fixture in this package drives — CreateIssue and -// UpdateIssue, or their unit-of-work equivalents — never the version table: -// emission lives at the RecordVersionInTx seam beneath all of them, so a -// closure that inserted a row directly would be asserting that this test can -// write a row, not that the engine does. -// -// WHAT THIS CONTRACT DELIBERATELY DOES NOT PIN, and the reason, so a later -// reader does not mistake the gap for an oversight: -// -// - THE EXACT BYTES OF durable_state. Design §15's R5.1 artifact is a -// verbatim marshal of the mutated issue, and its shape is pinned where it -// is produced, not here: a byte-for-byte assertion in a cross-leg -// contract would break every time a field is added to types.Issue for -// reasons that have nothing to do with dual-write correctness. What IS -// pinned (RunDualWriteAttributionIsRecordedWithTheMutation) is that a row -// exists and its attribution columns are populated the moment the -// mutation that produced it returns — the externally observable proxy -// for "atomic with the mutation" a black-box fixture can actually check. -// - store_epoch AND issue_versions.epoch (FR-6, "stamped, never bumped"). -// None of this fixture's five closures can see either: CurrentRevision -// reads issues.current_revision, and VersionRowCount and -// LatestVersionAttribution read issue_versions but never its epoch -// column. Adding a sixth closure just to reach one column — one two of -// three legs would have to invent white-box access for — would make the -// shared surface bespoke to a single requirement. FR-6 is instead pinned -// per leg, where a leg already has (or can cheaply grow) the access: see -// the dolt leg's TestDualWriteStampsTheCurrentStoreEpochOnEachVersionRow. -// - PARTICIPATION-GENERATION GATING (design §16.2b). Whether an -// update-shaped mutation on a legacy (participation_generation IS NULL) -// row is correctly skipped is Phase 2 behavior this bead also owns, but -// it depends on migration 0070's participation_generation column, which -// does not exist when this contract is first written (0070 lands last in -// this bead's build order, by the mayor's explicit sequencing — steps 4-5 -// of design §16.3 moved to their own migration slot when 0068 shipped as -// attribution_status-only, see that migration's header). It gets its own -// dedicated cases once 0070 lands, beside this file rather than inside -// it. -// - CONCURRENT WRITERS. Every case here is one writer, one issue, one -// mutation at a time — the same restriction journal_contract.go states -// for the same reason: a single-threaded case cannot exercise what only -// concurrency can break, and faking it with sleeps buys a flaky suite -// instead of a guarantee. -// - FR-4 ("attribution is metadata: never compared when deciding whether -// two versions' durable state differs, never included in any future -// digest/equality check"). This is a constraint on code that does not -// exist in Phase 2 — no digest or equality check over durable_state is -// built by this phase — so there is nothing yet for a case to run -// against. FR-4 has no dedicated case here for that reason, not because -// it was missed. -// - FR-8 (`wisps.current_revision`/`wisps.participation_generation` are -// never read or written by this phase). None of this fixture's five -// closures can express it: Mutate's front door is CreateIssue against -// the issues table, so this fixture has no wisp-routed id to check in -// the first place. FR-8 is a routing exclusion, not a property of a -// mutated issue's own version history, and belongs beside the explicit -// test design §9 item 5 already calls for (task 1's responsibility, not -// this contract's). -// -// SEEDING DISCIPLINE: EVERY CASE CREATES ITS OWN ISSUE. Unlike the journal — -// append-only and workspace-global, so every case rebaselines off the live -// head — a version history is PER ISSUE, and Mutate's front door is create, -// which cannot run twice against the same id. So every case mints its own id -// from fixture.IssuePrefix plus a marker naming the case, and no case may -// assume anything about another id's history. -type DualWriteFixture struct { - // IssuePrefix namespaces the id each case mints. Ids are global to a - // workspace, so two cases sharing one would read each other's version - // history. - IssuePrefix string - // Mutate performs one ACCEPTED mutation against a not-yet-existing id: - // the fixture's chosen front door for "a mutation this phase must - // version" is CreateIssue (or its unit-of-work equivalent), because - // create is one of the call sites RecordVersionInTx wires into and the - // one every leg can drive without first needing a row to exist. - Mutate func(ctx context.Context, id string) error - // MutateAsNoOp performs a mutation against an id Mutate already created - // that existing production code (issueops.DiscardNoopIssueUpdates) - // recognizes as a no-op before it ever reaches RecordVersionInTx — the - // fixture's chosen shape is an update that re-sets a field to the value - // Mutate already gave it. It is a DIFFERENT call than Mutate on purpose: - // FR-2 is a claim about the engine's no-op skip, and a fixture that used - // the same closure for both could not tell a real skip from Mutate - // simply never having run. - MutateAsNoOp func(ctx context.Context, id string) error - // CurrentRevision reads issues.current_revision for id. - CurrentRevision func(ctx context.Context, id string) (int64, error) - // VersionRowCount reads how many issue_versions rows exist for id. - VersionRowCount func(ctx context.Context, id string) (int, error) - // LatestVersionAttribution reads change_actor, change_agent and - // change_message off the highest-revision issue_versions row for id. - // Columns the schema allows NULL come back as "": this fixture answers - // "what did the mutation record", and a leg that recorded nothing for a - // column answers that question with an empty string, not a sentinel a - // case would have to special-case around. - LatestVersionAttribution func(ctx context.Context, id string) (actor, agent, message string, err error) -} - -// RunDualWriteMintsOneVersionRowPerAcceptedMutation pins FR-1: an accepted -// mutation mints EXACTLY one issue_versions row, not zero and not more than -// one. -// -// Zero would mean the phase built the flag and never wired the seam behind -// it — every other case in this file would still read a functioning store, -// because nothing about ordinary issue mutation depends on versioning -// existing. More than one is the other silent failure this phase has to -// avoid: a seam invoked twice for one caller-visible mutation (once from the -// front door's own transaction and once from a retry or a decorator wrapping -// it a second time) would double the history of every issue in the store -// without a single ordinary read ever noticing, since nothing reads -// issue_versions yet. -func RunDualWriteMintsOneVersionRowPerAcceptedMutation(t *testing.T, ctx context.Context, fixture DualWriteFixture) { - t.Helper() - id := fixture.IssuePrefix + "-mints-one" - if err := fixture.Mutate(ctx, id); err != nil { - t.Fatalf("mutating %s: %v", id, err) - } - count, err := fixture.VersionRowCount(ctx, id) - if err != nil { - t.Fatalf("counting version rows for %s: %v", id, err) - } - if count != 1 { - t.Errorf("version row count for %s = %d, want exactly 1: one accepted mutation mints one row, "+ - "not zero (the seam never fired) and not more than one (it fired twice for a single "+ - "caller-visible mutation)", id, count) - } -} - -// RunDualWriteNoOpMutationMintsNoRow pins FR-2: a mutation -// issueops.DiscardNoopIssueUpdates already discards before it reaches any -// write mints NO version row. -// -// This is the inheritance the task-1 design calls for explicitly: -// RecordVersionInTx sits inside the same already-short-circuited function -// bodies as RecordEventInTx, so a no-op never reaches it, and this case is -// what would catch a future refactor that moved the call ABOVE that -// short-circuit — versioning an update that changed nothing, which would -// make current_revision (and FR-5's identity between it and the row count) -// diverge from "the number of times this issue's durable state actually -// changed" for every issue anyone re-saves without editing. -func RunDualWriteNoOpMutationMintsNoRow(t *testing.T, ctx context.Context, fixture DualWriteFixture) { - t.Helper() - id := fixture.IssuePrefix + "-noop-mints-none" - if err := fixture.Mutate(ctx, id); err != nil { - t.Fatalf("mutating %s: %v", id, err) - } - before, err := fixture.VersionRowCount(ctx, id) - if err != nil { - t.Fatalf("counting version rows for %s before the no-op: %v", id, err) - } - - if err := fixture.MutateAsNoOp(ctx, id); err != nil { - t.Fatalf("no-op mutating %s: %v", id, err) - } - after, err := fixture.VersionRowCount(ctx, id) - if err != nil { - t.Fatalf("counting version rows for %s after the no-op: %v", id, err) - } - if after != before { - t.Errorf("version row count for %s went from %d to %d across a no-op mutation, want unchanged: "+ - "a mutation the engine already discards as a no-op must never reach RecordVersionInTx", - id, before, after) - } -} - -// RunDualWriteAttributionIsRecordedWithTheMutation pins FR-3 and FR-4: the -// version row minted for an accepted mutation carries that mutation's -// attribution, visible the instant the mutation's own call returns. -// -// "Visible the instant the call returns" is the load-bearing half: this -// contract cannot see two writes land in the same database transaction, but -// it CAN see whether a caller has to do anything extra — flush, commit -// separately, poll — before the attribution it just supplied shows up beside -// the row it mutated. A dual-write seam that recorded the version -// asynchronously, or in a transaction of its own committed after the -// caller's, would still pass every other case in this file and fail this one. -func RunDualWriteAttributionIsRecordedWithTheMutation(t *testing.T, ctx context.Context, fixture DualWriteFixture) { - t.Helper() - id := fixture.IssuePrefix + "-attribution" - if err := fixture.Mutate(ctx, id); err != nil { - t.Fatalf("mutating %s: %v", id, err) - } - actor, _, _, err := fixture.LatestVersionAttribution(ctx, id) - if err != nil { - t.Fatalf("reading attribution for %s immediately after the mutation that must have minted it: %v", id, err) - } - if actor == "" { - t.Errorf("change_actor for %s is empty immediately after an accepted mutation: FR-3/FR-4 require "+ - "the version row to carry the mutation's attribution, and an actor is the one attribution "+ - "input every leg's Mutate closure supplies", id) - } -} - -// RunDualWriteCurrentRevisionMatchesTheNewVersionRow pins FR-5: after any -// accepted mutation, issues.current_revision agrees with how many version -// rows the issue actually has. -// -// This restates the bead's task-1 description — bump current_revision by a -// plain +1 on the row already carrying the mutation, in the same transaction -// as the version row it accompanies — as something a black-box fixture can -// check without seeing either write: current_revision IS a count of the -// version rows RecordVersionInTx has minted for this issue, never ahead of -// it and never behind. Divergence in either direction is a real bug this -// phase must not ship: current_revision racing ahead of the row count would -// tell Phase 3/4's future CAS check to expect a revision that was never -// durably recorded, and falling behind would let a second writer believe it -// observed the latest version when a row past it already exists. -func RunDualWriteCurrentRevisionMatchesTheNewVersionRow(t *testing.T, ctx context.Context, fixture DualWriteFixture) { - t.Helper() - id := fixture.IssuePrefix + "-revision-matches" - if err := fixture.Mutate(ctx, id); err != nil { - t.Fatalf("mutating %s: %v", id, err) - } - revision, err := fixture.CurrentRevision(ctx, id) - if err != nil { - t.Fatalf("reading current_revision for %s: %v", id, err) - } - count, err := fixture.VersionRowCount(ctx, id) - if err != nil { - t.Fatalf("counting version rows for %s: %v", id, err) - } - if revision != int64(count) { - t.Errorf("current_revision for %s = %d but it has %d version row(s): the two must always agree — "+ - "current_revision is nothing but a count of the rows RecordVersionInTx has minted for this "+ - "issue", id, revision, count) - } -} - -// RunDualWriteFlagOffProducesNoVersionRows pins FR-7: with -// storage.VersionedHistoryConfigurer left off, an otherwise-identical -// mutation mints no version row and leaves current_revision at its untouched -// schema default. -// -// This is the phase's core safety property, restated from the write side: -// every store this bead ships to already runs with the flag off, so a -// dual-write engine that fired regardless of the switch would start writing -// issue_versions rows nobody asked for, in every workspace, the moment this -// PR merges. The fixture this case is called against must be constructed -// with the flag OFF from the start — see each leg's flag-off constructor — -// so the case cannot pass by accident against a fixture that happens to -// share state with a flag-on one. -func RunDualWriteFlagOffProducesNoVersionRows(t *testing.T, ctx context.Context, fixture DualWriteFixture) { - t.Helper() - id := fixture.IssuePrefix + "-flag-off" - if err := fixture.Mutate(ctx, id); err != nil { - t.Fatalf("mutating %s with the flag off: %v", id, err) - } - count, err := fixture.VersionRowCount(ctx, id) - if err != nil { - t.Fatalf("counting version rows for %s: %v", id, err) - } - if count != 0 { - t.Errorf("version row count for %s = %d with storage.VersionedHistoryConfigurer OFF, want 0: a "+ - "disabled flag must produce byte-identical writes to the pre-Phase-2 engine, and a row minted "+ - "anyway means every existing workspace starts accumulating history nobody enabled", id, count) - } - revision, err := fixture.CurrentRevision(ctx, id) - if err != nil { - t.Fatalf("reading current_revision for %s: %v", id, err) - } - if revision != 1 { - t.Errorf("current_revision for %s = %d with the flag off, want 1 (the column's own schema "+ - "default, untouched): a flag-off store must never execute the bump half of RecordVersionInTx "+ - "either", id, revision) - } -} - -// RunDualWriteNoOpMutationLeavesThePriorVersionRowUnperturbed strengthens -// FR-2 from the one angle RunDualWriteNoOpMutationMintsNoRow does not reach: -// that case pins that a no-op mints no NEW row; this pins that a no-op does -// not silently rewrite the row already there. -// -// A dual-write seam that mistakenly ran an UPDATE instead of an -// insert-or-skip — reusing the prior row's primary key because the no-op -// path recomputed "the current version" instead of being skipped before it -// got there — would pass RunDualWriteNoOpMutationMintsNoRow outright: the row -// count never changes when an UPDATE overwrites in place. Comparing the -// attribution byte-for-byte across the no-op is what catches that failure -// mode instead. -func RunDualWriteNoOpMutationLeavesThePriorVersionRowUnperturbed(t *testing.T, ctx context.Context, fixture DualWriteFixture) { - t.Helper() - id := fixture.IssuePrefix + "-noop-unperturbed" - if err := fixture.Mutate(ctx, id); err != nil { - t.Fatalf("mutating %s: %v", id, err) - } - beforeActor, beforeAgent, beforeMessage, err := fixture.LatestVersionAttribution(ctx, id) - if err != nil { - t.Fatalf("reading attribution for %s before the no-op: %v", id, err) - } - - if err := fixture.MutateAsNoOp(ctx, id); err != nil { - t.Fatalf("no-op mutating %s: %v", id, err) - } - afterActor, afterAgent, afterMessage, err := fixture.LatestVersionAttribution(ctx, id) - if err != nil { - t.Fatalf("reading attribution for %s after the no-op: %v", id, err) - } - if beforeActor != afterActor || beforeAgent != afterAgent || beforeMessage != afterMessage { - t.Errorf("attribution for %s changed across a no-op mutation, (%q,%q,%q) -> (%q,%q,%q): a mutation "+ - "that mints no new row must also leave the existing one exactly as the accepted mutation "+ - "before it left it, not silently rewritten in place", - id, beforeActor, beforeAgent, beforeMessage, afterActor, afterAgent, afterMessage) - } -} diff --git a/backend/conformance/dualwrite_participation_generation_contract.go b/backend/conformance/dualwrite_participation_generation_contract.go deleted file mode 100644 index 7943e7e4a..000000000 --- a/backend/conformance/dualwrite_participation_generation_contract.go +++ /dev/null @@ -1,644 +0,0 @@ -package conformance - -import ( - "context" - "testing" -) - -// This file holds the write-fence cases dualwrite_history_contract.go's own -// "WHAT THIS CONTRACT DELIBERATELY DOES NOT PIN" section explicitly deferred: -// "PARTICIPATION-GENERATION GATING (design §16.2b)... It gets its own -// dedicated cases once 0070 lands, beside this file rather than inside it." -// Migration 0070 now carries participation_generation on both issues and -// wisps (design §16.3 steps 4-5, be-dt74u amendment, be-h89oq), so this is -// that file. -// -// Design §16.2b, verbatim substance: the flag-on writer does not increment -// current_revision or mint an issue_versions row for an UPDATE-shaped -// mutation against a record whose participation_generation is NULL — a skip, -// not a refusal (FR-2's zero-rows no-op shape). A CREATE-shaped mutation is -// different: a brand-new row has no prior legacy state to preserve, so if the -// flag is already on at creation time, the writer stamps -// participation_generation as part of the same insert that begins its -// history. A create with the flag off leaves the column NULL, indistinguishable -// from a true legacy row (FR-7's byte-identical requirement). -// -// WHY A SEPARATE FIXTURE RATHER THAN THREE MORE DualWriteFixture CLOSURES. -// DualWriteFixture's Mutate/MutateAsNoOp pair is deliberately flag-fixed-at- -// construction (see that file's SEEDING DISCIPLINE note and each leg's -// newXxxDualWriteFixture, which calls SetVersionedHistoryEnabled exactly -// once). The legacy-record case here is inherently "created while the flag -// was OFF, then mutated after the flag turns ON" — a fixture built once with -// one flag state cannot express that sequence, so SetFlag here is a toggle -// callable more than once per fixture, not a one-time constructor argument. -// -// THE FIXTURE'S CLOSURES ARE THE OBSERVABLE SURFACE, NOT THE SCHEMA — same -// discipline as DualWriteFixture: Mutate/MutateExisting drive CreateIssue and -// UpdateIssue (or their unit-of-work equivalents), never issue_versions or -// store_epoch directly. -// -// WHAT THIS CONTRACT DELIBERATELY DOES NOT PIN: -// - THE EXACT STAMPED VALUE. That a create-shaped mutation with the flag on -// stamps SOME non-NULL participation_generation is pinned here; that the -// value equals the current store_epoch.epoch is not — reaching -// store_epoch is the same one-column, two-of-three-legs-need-white-box- -// access problem dualwrite_history_contract.go's FR-6 note describes, and -// the same resolution applies: pinned per leg, dolt-only, alongside -// TestDualWriteStampsTheCurrentStoreEpochOnEachVersionRow. -// - wisps.participation_generation. FR-8 (carried by dualwrite_history_contract.go's -// exclusion list) applies here too: this fixture's Mutate front door is -// CreateIssue against the issues table, the wisp-arrival cases read only -// the issues row a wisp becomes, and no phase reads or writes the wisps -// column regardless. -type ParticipationGenerationFixture struct { - // IssuePrefix namespaces the id each case mints, the same discipline - // DualWriteFixture uses and for the same reason. - IssuePrefix string - // SetFlag toggles versioned history for every subsequent call through - // this fixture, and may be called more than once per case — unlike - // DualWriteFixture, whose flag is fixed at construction. - SetFlag func(ctx context.Context, enabled bool) error - // Mutate creates a new issue with the given id: a CREATE-shaped mutation - // in design §16.2b's sense. - Mutate func(ctx context.Context, id string) error - // MutateExisting applies a real (non-no-op) UPDATE-shaped mutation to an - // already-existing issue — a different value than Mutate gave it, so a - // vacuous no-op skip can never be mistaken for the fence. - MutateExisting func(ctx context.Context, id string) error - // CreateWisp creates a new wisp-plane (ephemeral) issue with the given - // id: the starting point for the two ways a record reaches the issues - // plane without being created there. A wisp is never versioned (FR-8) - // and never carries a participation declaration, in either flag state. - CreateWisp func(ctx context.Context, id string) error - // PromoteWisp promotes the wisp id onto the issues plane - // (PromoteFromEphemeral, or the unit-of-work leg's PromoteWisp). - PromoteWisp func(ctx context.Context, id string) error - // MovePersistent moves the wisp id onto the issues plane through a - // persistence-mode update (bd update --persistent). It is its own - // closure rather than a second spelling of PromoteWisp because it mints at - // a different site on every leg: the guarded update's one end-of-update - // mint on dolt and embeddeddolt, MoveIssuePersistenceInTx's own on uow. - // On an issues-plane row whose flags disagree with it (see - // CreateNoHistoryIssue) the same update repairs the flags in place, a - // same-plane normalize, rather than moving the row. - MovePersistent func(ctx context.Context, id string) error - // ClearWispFlag clears the wisp id's ephemeral flag IN PLACE: the untyped - // UpdateIssue of {"wisp": false} bd mol squash makes on a squashed root - // (UpdateWisp on the unit-of-work leg, the way its molecule writer routes - // it). The row stays in the wisps table, now carrying neither Ephemeral - // nor NoHistory. - ClearWispFlag func(ctx context.Context, id string) error - // SquashWispRoot closes the wisp id and then clears its flag as - // ClearWispFlag does, both in ONE transaction: the shape bd mol squash's - // root auto-close takes, where an error from either write aborts both. - SquashWispRoot func(ctx context.Context, id string) error - // CreateUnflaggedWisp creates id in the wisps table with neither - // Ephemeral nor NoHistory set: import's "wisp_plane" marker, which pins - // the plane whatever the flags say (WispPlaneOverride = &true, through - // the batch create import runs), or on the unit-of-work leg CreateWisp, - // which routes by the call rather than by the flags. - CreateUnflaggedWisp func(ctx context.Context, id string) error - // CreateNoHistoryIssue creates id in the ISSUES table carrying - // NoHistory=true: import's promoted-no-history shape (bd-r9uce), a - // durable row whose stray flag is preserved while WispPlaneOverride = - // &false pins its plane, through the batch create import runs; or on the - // unit-of-work leg CreateIssue, which routes by the call. - CreateNoHistoryIssue func(ctx context.Context, id string) error - // CurrentRevision reads issues.current_revision for id. - CurrentRevision func(ctx context.Context, id string) (int64, error) - // VersionRowCount reads how many issue_versions rows exist for id. - VersionRowCount func(ctx context.Context, id string) (int, error) - // ParticipationGeneration reads issues.participation_generation for id, - // returning nil for SQL NULL. - ParticipationGeneration func(ctx context.Context, id string) (*int64, error) - // InWispsTable reports whether the wisps table holds a row for id: the - // precondition the wisps-table cases check, so they cannot pass on a row - // that a step moved to the issues table. - InWispsTable func(ctx context.Context, id string) (bool, error) -} - -// RunParticipationGenerationCreateWithFlagOnStampsAValue pins design -// §16.2(a)/(b): a create-shaped mutation with the flag already on stamps a -// non-NULL participation_generation on the new row, as part of the same -// insert that begins its history — a brand-new row has no legacy state to -// preserve, so there is nothing to skip. -func RunParticipationGenerationCreateWithFlagOnStampsAValue(t *testing.T, ctx context.Context, fixture ParticipationGenerationFixture) { - t.Helper() - if err := fixture.SetFlag(ctx, true); err != nil { - t.Fatalf("enabling versioned history: %v", err) - } - id := fixture.IssuePrefix + "-create-stamps" - if err := fixture.Mutate(ctx, id); err != nil { - t.Fatalf("mutating %s: %v", id, err) - } - gen, err := fixture.ParticipationGeneration(ctx, id) - if err != nil { - t.Fatalf("reading participation_generation for %s: %v", id, err) - } - if gen == nil { - t.Errorf("participation_generation for %s is NULL immediately after a create-shaped mutation with "+ - "the flag on, want a stamped non-NULL value: design §16.2b requires the writer to declare "+ - "participation at insert time, since a brand-new row has no prior legacy state to preserve", id) - } -} - -// RunParticipationGenerationCreateWithFlagOffLeavesLegacyNull pins the other -// half of design §16.2(b): a create-shaped mutation with the flag off leaves -// participation_generation NULL, indistinguishable from a true legacy row — -// flag-off must stay byte-identical to pre-Phase-2 behavior (FR-7). -func RunParticipationGenerationCreateWithFlagOffLeavesLegacyNull(t *testing.T, ctx context.Context, fixture ParticipationGenerationFixture) { - t.Helper() - if err := fixture.SetFlag(ctx, false); err != nil { - t.Fatalf("disabling versioned history: %v", err) - } - id := fixture.IssuePrefix + "-create-flag-off" - if err := fixture.Mutate(ctx, id); err != nil { - t.Fatalf("mutating %s: %v", id, err) - } - gen, err := fixture.ParticipationGeneration(ctx, id) - if err != nil { - t.Fatalf("reading participation_generation for %s: %v", id, err) - } - if gen != nil { - t.Errorf("participation_generation for %s = %d with the flag off at creation, want NULL: a "+ - "flag-off create must remain indistinguishable from a true legacy row (design §16.2b)", id, *gen) - } -} - -// RunParticipationGenerationSkipsUpdateOnLegacyRecord pins the write fence's -// central claim, design §16.2(b): the flag-on writer does not increment -// current_revision or mint an issue_versions row for an update-shaped -// mutation against a record whose participation_generation is NULL. The -// record must be a REAL legacy record — created while the flag was off, not -// merely one the fixture asserts is NULL — so this also re-confirms that -// precondition before trusting the rest of the case. -func RunParticipationGenerationSkipsUpdateOnLegacyRecord(t *testing.T, ctx context.Context, fixture ParticipationGenerationFixture) { - t.Helper() - if err := fixture.SetFlag(ctx, false); err != nil { - t.Fatalf("disabling versioned history: %v", err) - } - id := fixture.IssuePrefix + "-legacy-skip" - if err := fixture.Mutate(ctx, id); err != nil { - t.Fatalf("mutating %s: %v", id, err) - } - if gen, err := fixture.ParticipationGeneration(ctx, id); err != nil { - t.Fatalf("reading participation_generation for %s: %v", id, err) - } else if gen != nil { - t.Fatalf("participation_generation for %s = %d before the flag was ever turned on, want NULL — "+ - "this case cannot test the legacy-record fence against a record that was never actually legacy", - id, *gen) - } - - if err := fixture.SetFlag(ctx, true); err != nil { - t.Fatalf("enabling versioned history: %v", err) - } - beforeRevision, err := fixture.CurrentRevision(ctx, id) - if err != nil { - t.Fatalf("reading current_revision for %s: %v", id, err) - } - beforeCount, err := fixture.VersionRowCount(ctx, id) - if err != nil { - t.Fatalf("counting version rows for %s: %v", id, err) - } - - if err := fixture.MutateExisting(ctx, id); err != nil { - t.Fatalf("mutating existing %s: %v", id, err) - } - - afterRevision, err := fixture.CurrentRevision(ctx, id) - if err != nil { - t.Fatalf("reading current_revision for %s: %v", id, err) - } - afterCount, err := fixture.VersionRowCount(ctx, id) - if err != nil { - t.Fatalf("counting version rows for %s: %v", id, err) - } - if afterCount != beforeCount { - t.Errorf("version row count for %s went from %d to %d across an update-shaped mutation on a "+ - "legacy (participation_generation IS NULL) record with the flag now on, want unchanged: "+ - "design §16.2b requires the writer to skip minting for a record it has not positively "+ - "promoted", id, beforeCount, afterCount) - } - if afterRevision != beforeRevision { - t.Errorf("current_revision for %s went from %d to %d across the same skipped mutation, want "+ - "unchanged: a skip means neither half of RecordVersionInTx's write runs, not just the "+ - "version-row half", id, beforeRevision, afterRevision) - } -} - -// RunParticipationGenerationProceedsForPromotedRecord is the fence's -// not-vacuous check: an update-shaped mutation against a record whose -// participation_generation is ALREADY stamped (created with the flag on, -// i.e. positively declared, not legacy) must mint normally. Without this -// case, an implementation that skips every update unconditionally would -// still pass RunParticipationGenerationSkipsUpdateOnLegacyRecord. -func RunParticipationGenerationProceedsForPromotedRecord(t *testing.T, ctx context.Context, fixture ParticipationGenerationFixture) { - t.Helper() - if err := fixture.SetFlag(ctx, true); err != nil { - t.Fatalf("enabling versioned history: %v", err) - } - id := fixture.IssuePrefix + "-promoted-proceeds" - if err := fixture.Mutate(ctx, id); err != nil { - t.Fatalf("mutating %s: %v", id, err) - } - if gen, err := fixture.ParticipationGeneration(ctx, id); err != nil { - t.Fatalf("reading participation_generation for %s: %v", id, err) - } else if gen == nil { - t.Fatalf("participation_generation for %s is NULL immediately after a create with the flag on — "+ - "this case cannot test the promoted-record path against a record that was never actually "+ - "promoted", id) - } - - beforeCount, err := fixture.VersionRowCount(ctx, id) - if err != nil { - t.Fatalf("counting version rows for %s: %v", id, err) - } - if err := fixture.MutateExisting(ctx, id); err != nil { - t.Fatalf("mutating existing %s: %v", id, err) - } - afterCount, err := fixture.VersionRowCount(ctx, id) - if err != nil { - t.Fatalf("counting version rows for %s: %v", id, err) - } - if afterCount != beforeCount+1 { - t.Errorf("version row count for %s went from %d to %d across an update-shaped mutation on an "+ - "already-promoted record, want exactly +1: the fence must skip only a legacy (NULL) record, "+ - "not every update (design §16.2b)", id, beforeCount, afterCount) - } -} - -// RunParticipationGenerationFlagOffSkipsUpdateOnParticipatingRecord pins the -// switch's off half for a record the fence would let through: created with -// the flag on (so participation_generation is stamped), then updated after -// the flag is turned back off, it must mint nothing. ProceedsForPromotedRecord -// shows the fence passes such a record, so only the flag stands between this -// update and a mint. No other case can see a writer that ignores the flag on -// updates: every other flag-off record is legacy, and the fence stops those -// first. -func RunParticipationGenerationFlagOffSkipsUpdateOnParticipatingRecord(t *testing.T, ctx context.Context, fixture ParticipationGenerationFixture) { - t.Helper() - if err := fixture.SetFlag(ctx, true); err != nil { - t.Fatalf("enabling versioned history: %v", err) - } - id := fixture.IssuePrefix + "-flag-off-skip" - if err := fixture.Mutate(ctx, id); err != nil { - t.Fatalf("mutating %s: %v", id, err) - } - if gen, err := fixture.ParticipationGeneration(ctx, id); err != nil { - t.Fatalf("reading participation_generation for %s: %v", id, err) - } else if gen == nil { - t.Fatalf("participation_generation for %s is NULL immediately after a create with the flag on — "+ - "this case cannot test the flag against a record the fence would have stopped anyway", id) - } - - if err := fixture.SetFlag(ctx, false); err != nil { - t.Fatalf("disabling versioned history: %v", err) - } - beforeRevision, err := fixture.CurrentRevision(ctx, id) - if err != nil { - t.Fatalf("reading current_revision for %s: %v", id, err) - } - beforeCount, err := fixture.VersionRowCount(ctx, id) - if err != nil { - t.Fatalf("counting version rows for %s: %v", id, err) - } - - if err := fixture.MutateExisting(ctx, id); err != nil { - t.Fatalf("mutating existing %s: %v", id, err) - } - - afterRevision, err := fixture.CurrentRevision(ctx, id) - if err != nil { - t.Fatalf("reading current_revision for %s: %v", id, err) - } - afterCount, err := fixture.VersionRowCount(ctx, id) - if err != nil { - t.Fatalf("counting version rows for %s: %v", id, err) - } - if afterCount != beforeCount { - t.Errorf("version row count for %s went from %d to %d across an update-shaped mutation with the "+ - "flag off, want unchanged: turning versioned history off stops minting for every record, "+ - "including one that already participates", id, beforeCount, afterCount) - } - if afterRevision != beforeRevision { - t.Errorf("current_revision for %s went from %d to %d across the same update with the flag off, "+ - "want unchanged", id, beforeRevision, afterRevision) - } -} - -// RunParticipationGenerationWispPromotionBeginsHistory pins that promoting a -// wisp onto the issues plane is CREATE-shaped in design §16.2b's sense. (This -// is wisp promotion, PromoteFromEphemeral: not R2.3's promotion of a legacy -// record, which is what ProceedsForPromotedRecord's name refers to.) The -// issues row a promotion inserts is brand new on that plane, and the wisp -// behind it was never versioned and never declared participation (FR-8), so -// there is no legacy state to preserve: with the flag on, the promotion stamps -// participation_generation and mints the record's first version, as a create -// there would. Treated as UPDATE-shaped, the fence finds the new row NULL and -// skips, and because nothing stamps the column afterwards every later update -// skips too, so a record created with the flag on is never versioned at all. -func RunParticipationGenerationWispPromotionBeginsHistory(t *testing.T, ctx context.Context, fixture ParticipationGenerationFixture) { - t.Helper() - runWispArrivalBeginsHistory(t, ctx, fixture, "promote", fixture.PromoteWisp) -} - -// RunParticipationGenerationWispPersistenceMoveBeginsHistory is -// RunParticipationGenerationWispPromotionBeginsHistory for the other way a -// wisp reaches the issues plane, a persistence-mode update (bd update -// --persistent). It needs its own case because it reaches a different mint -// site than promotion on every leg (see MovePersistent). -func RunParticipationGenerationWispPersistenceMoveBeginsHistory(t *testing.T, ctx context.Context, fixture ParticipationGenerationFixture) { - t.Helper() - runWispArrivalBeginsHistory(t, ctx, fixture, "move-persistent", fixture.MovePersistent) -} - -// runWispArrivalBeginsHistory is the body the two wisp-arrival cases share: -// with the flag on, a wisp that arrives on the issues plane through arrive -// carries a stamped participation_generation and exactly one version row, -// and the next update-shaped mutation mints normally. -func runWispArrivalBeginsHistory(t *testing.T, ctx context.Context, fixture ParticipationGenerationFixture, how string, arrive func(context.Context, string) error) { - t.Helper() - if err := fixture.SetFlag(ctx, true); err != nil { - t.Fatalf("enabling versioned history: %v", err) - } - id := fixture.IssuePrefix + "-" + how - if err := fixture.CreateWisp(ctx, id); err != nil { - t.Fatalf("creating wisp %s: %v", id, err) - } - if err := arrive(ctx, id); err != nil { - t.Fatalf("moving wisp %s onto the issues plane (%s): %v", id, how, err) - } - - gen, err := fixture.ParticipationGeneration(ctx, id) - if err != nil { - t.Fatalf("reading participation_generation for %s: %v", id, err) - } - if gen == nil { - t.Errorf("participation_generation for %s is NULL after the wisp reached the issues plane (%s) with "+ - "the flag on, want a stamped value: the issues row is new, so the arrival is create-shaped "+ - "(design §16.2b), and a NULL here leaves the record fenced as legacy for good", id, how) - } - arrivedCount, err := fixture.VersionRowCount(ctx, id) - if err != nil { - t.Fatalf("counting version rows for %s: %v", id, err) - } - if arrivedCount != 1 { - t.Errorf("version row count for %s = %d after the wisp reached the issues plane (%s) with the flag "+ - "on, want 1: a wisp is never versioned (FR-8), so the arrival mints the record's first version", - id, arrivedCount, how) - } - - if err := fixture.MutateExisting(ctx, id); err != nil { - t.Fatalf("mutating existing %s: %v", id, err) - } - afterCount, err := fixture.VersionRowCount(ctx, id) - if err != nil { - t.Fatalf("counting version rows for %s: %v", id, err) - } - if afterCount != arrivedCount+1 { - t.Errorf("version row count for %s went from %d to %d across an update-shaped mutation after the "+ - "wisp reached the issues plane (%s), want exactly +1: a record that arrived with the flag on is "+ - "not legacy, so the fence must let its updates mint", id, arrivedCount, afterCount, how) - } -} - -// RunParticipationGenerationWispFlagClearMintsNothing pins that the fence -// decides a row's plane by the TABLE that holds it, not by the row's flags. -// bd mol squash clears a wisp root's ephemeral flag in place, so the row -// stays in the wisps table carrying neither Ephemeral nor NoHistory, and a -// plane test read off those flags takes it for an issues-plane row. It is -// not one: a wisps-table row is never versioned (FR-8) and has no issues row -// whose participation_generation the fence could read. So the update must -// succeed and mint nothing. Read off the flags, the fence either aborts the -// whole update looking for an issues row that is not there, or, without the -// fence, mints a stray version for a wisp. -func RunParticipationGenerationWispFlagClearMintsNothing(t *testing.T, ctx context.Context, fixture ParticipationGenerationFixture) { - t.Helper() - runWispTableRowMintsNothing(t, ctx, fixture, "flag-clear", fixture.CreateWisp, fixture.ClearWispFlag) -} - -// RunParticipationGenerationWispRootSquashMintsNothing is -// RunParticipationGenerationWispFlagClearMintsNothing in the shape bd mol -// squash actually runs it: the root's close and its flag clear in one -// transaction, so a mint that fails on the cleared row takes the close down -// with it and the squash cannot complete. -func RunParticipationGenerationWispRootSquashMintsNothing(t *testing.T, ctx context.Context, fixture ParticipationGenerationFixture) { - t.Helper() - runWispTableRowMintsNothing(t, ctx, fixture, "squash", fixture.CreateWisp, fixture.SquashWispRoot) -} - -// RunParticipationGenerationUnflaggedWispCreateMintsNothing pins the -// create-shaped half of the same rule: a row CREATED in the wisps table -// without either flag (import's "wisp_plane" marker) reaches the -// create-shaped mint, and must come out of it with no error and no version, -// exactly as a flagged wisp does. -func RunParticipationGenerationUnflaggedWispCreateMintsNothing(t *testing.T, ctx context.Context, fixture ParticipationGenerationFixture) { - t.Helper() - runWispTableRowMintsNothing(t, ctx, fixture, "unflagged-create", fixture.CreateUnflaggedWisp) -} - -// runWispTableRowMintsNothing is the body the three wisps-table cases share: -// with the flag on, every step succeeds on id, the wisps table still holds id -// afterwards, and id ends with no issue_versions row. -func runWispTableRowMintsNothing(t *testing.T, ctx context.Context, fixture ParticipationGenerationFixture, how string, steps ...func(context.Context, string) error) { - t.Helper() - if err := fixture.SetFlag(ctx, true); err != nil { - t.Fatalf("enabling versioned history: %v", err) - } - id := fixture.IssuePrefix + "-" + how - for i, step := range steps { - if err := step(ctx, id); err != nil { - t.Fatalf("step %d of %s on wisps-table row %s with the flag on: %v — a row the wisps table "+ - "holds is never versioned (FR-8), so its mint must be a no-op, not an error that aborts "+ - "the mutation", i+1, how, id, err) - } - } - // The precondition, checked after the steps rather than assumed: a step - // that moved the row to the issues table would leave a NULL - // participation_generation there, the update-shaped mint would be fenced - // as legacy, and the zero count below would hold without saying anything - // about a wisps-table row. - if inWisps, err := fixture.InWispsTable(ctx, id); err != nil { - t.Fatalf("checking whether the wisps table holds %s: %v", id, err) - } else if !inWisps { - t.Fatalf("%s is not in the wisps table after %s — this case pins that a wisps-table row mints "+ - "nothing, and cannot say so about a row a step moved out of that table", id, how) - } - count, err := fixture.VersionRowCount(ctx, id) - if err != nil { - t.Fatalf("counting version rows for %s: %v", id, err) - } - if count != 0 { - t.Errorf("version row count for wisps-table row %s = %d after %s with the flag on, want 0: a "+ - "wisp is never versioned (FR-8), whatever its flags say", id, count, how) - } -} - -// RunParticipationGenerationNoHistoryIssueCreateDeclaresParticipation pins -// that a create declares participation for every ISSUES-table row it makes -// with the flag on, a row carrying NoHistory included. Import keeps a -// promoted no-history wisp's durable row on the issues plane with its stray -// no_history=true (bd-r9uce). While the row says no-history it is not -// versioned, so the create mints nothing. But it was created with history -// on, so it is not legacy: once bd update --persistent repairs the flag in -// place (a same-plane normalize, update-shaped by design), the repair and -// every update after it must mint. Left NULL by the create, the column reads -// as legacy to the fence, and the row is never versioned at all. -func RunParticipationGenerationNoHistoryIssueCreateDeclaresParticipation(t *testing.T, ctx context.Context, fixture ParticipationGenerationFixture) { - t.Helper() - if err := fixture.SetFlag(ctx, true); err != nil { - t.Fatalf("enabling versioned history: %v", err) - } - id := fixture.IssuePrefix + "-no-history-create" - if err := fixture.CreateNoHistoryIssue(ctx, id); err != nil { - t.Fatalf("creating no-history issues-plane row %s: %v", id, err) - } - gen, err := fixture.ParticipationGeneration(ctx, id) - if err != nil { - t.Fatalf("reading participation_generation for %s: %v", id, err) - } - if gen == nil { - t.Errorf("participation_generation for %s is NULL after a create with the flag on, want a stamped "+ - "value: the row is new on the issues plane, so the create declares participation (design "+ - "§16.2b) even though its NoHistory flag keeps it unversioned for now", id) - } - if count, err := fixture.VersionRowCount(ctx, id); err != nil { - t.Fatalf("counting version rows for %s: %v", id, err) - } else if count != 0 { - t.Errorf("version row count for %s = %d after a create carrying NoHistory, want 0: a no-history "+ - "row is not versioned while it says so", id, count) - } - - if err := fixture.MovePersistent(ctx, id); err != nil { - t.Fatalf("repairing %s to persistent: %v", id, err) - } - repairedCount, err := fixture.VersionRowCount(ctx, id) - if err != nil { - t.Fatalf("counting version rows for %s: %v", id, err) - } - if repairedCount != 1 { - t.Errorf("version row count for %s = %d after the persistent repair, want 1: the row was created "+ - "with the flag on, so the fence must let the repair's update-shaped mint through", id, repairedCount) - } - - if err := fixture.MutateExisting(ctx, id); err != nil { - t.Fatalf("mutating existing %s: %v", id, err) - } - afterCount, err := fixture.VersionRowCount(ctx, id) - if err != nil { - t.Fatalf("counting version rows for %s: %v", id, err) - } - if afterCount != repairedCount+1 { - t.Errorf("version row count for %s went from %d to %d across an update-shaped mutation after the "+ - "repair, want exactly +1", id, repairedCount, afterCount) - } -} - -// RunParticipationGenerationLegacyNoHistoryIssueStaysLegacy is -// RunParticipationGenerationNoHistoryIssueCreateDeclaresParticipation's -// negative twin, and pins R2.3: an ordinary write never promotes a legacy -// record. The same no-history issues-table row, created while the flag was -// OFF, is legacy. Only a create-shaped call stamps a flagged row, so once the -// flag is on, an update-shaped mutation must leave participation_generation -// NULL and mint nothing. So must the bd update --persistent repair that then -// clears the flag in place: that repair is update-shaped too (a same-plane -// normalize), so the row it leaves is an ordinary legacy row, still fenced. -func RunParticipationGenerationLegacyNoHistoryIssueStaysLegacy(t *testing.T, ctx context.Context, fixture ParticipationGenerationFixture) { - t.Helper() - if err := fixture.SetFlag(ctx, false); err != nil { - t.Fatalf("disabling versioned history: %v", err) - } - id := fixture.IssuePrefix + "-no-history-legacy" - if err := fixture.CreateNoHistoryIssue(ctx, id); err != nil { - t.Fatalf("creating no-history issues-plane row %s with the flag off: %v", id, err) - } - if gen, err := fixture.ParticipationGeneration(ctx, id); err != nil { - t.Fatalf("reading participation_generation for %s: %v", id, err) - } else if gen != nil { - t.Fatalf("participation_generation for %s = %d before the flag was ever turned on, want NULL — "+ - "this case cannot test that a legacy no-history row stays legacy against a row that was never "+ - "legacy", id, *gen) - } - - if err := fixture.SetFlag(ctx, true); err != nil { - t.Fatalf("enabling versioned history: %v", err) - } - beforeRevision, err := fixture.CurrentRevision(ctx, id) - if err != nil { - t.Fatalf("reading current_revision for %s: %v", id, err) - } - - assertStillLegacy := func(after string) { - t.Helper() - if gen, err := fixture.ParticipationGeneration(ctx, id); err != nil { - t.Fatalf("reading participation_generation for %s: %v", id, err) - } else if gen != nil { - t.Errorf("participation_generation for %s = %d after %s with the flag on, want NULL: the row "+ - "was created with the flag off, and an update-shaped write must not promote a legacy "+ - "record (R2.3), whatever its no-history flag says", id, *gen, after) - } - if count, err := fixture.VersionRowCount(ctx, id); err != nil { - t.Fatalf("counting version rows for %s: %v", id, err) - } else if count != 0 { - t.Errorf("version row count for %s = %d after %s with the flag on, want 0: a legacy row's "+ - "update-shaped mint is fenced (design §16.2b)", id, count, after) - } - if revision, err := fixture.CurrentRevision(ctx, id); err != nil { - t.Fatalf("reading current_revision for %s: %v", id, err) - } else if revision != beforeRevision { - t.Errorf("current_revision for %s went from %d to %d after %s, want unchanged: a fenced "+ - "mint runs neither half of the seam's write", id, beforeRevision, revision, after) - } - } - - if err := fixture.MutateExisting(ctx, id); err != nil { - t.Fatalf("mutating existing %s: %v", id, err) - } - assertStillLegacy("an update-shaped mutation") - - if err := fixture.MovePersistent(ctx, id); err != nil { - t.Fatalf("repairing %s to persistent: %v", id, err) - } - assertStillLegacy("the persistent repair") -} - -// RunParticipationGenerationFenceIgnoresSchemaSkewHatch pins design §16.2(c): -// BD_IGNORE_SCHEMA_SKEW=1 downgrades CheckForwardDrift's refusal to a warning -// (internal/storage/schema/schema.go), but it must have no effect on this -// fence — the fence keys on the participation_generation column's real data, -// not on the migration cursor the hatch is about, so a write admitted through -// that hatch is still, correctly, skipped when the record is legacy. -func RunParticipationGenerationFenceIgnoresSchemaSkewHatch(t *testing.T, ctx context.Context, fixture ParticipationGenerationFixture) { - t.Helper() - t.Setenv("BD_IGNORE_SCHEMA_SKEW", "1") - - if err := fixture.SetFlag(ctx, false); err != nil { - t.Fatalf("disabling versioned history: %v", err) - } - id := fixture.IssuePrefix + "-schema-skew-hatch" - if err := fixture.Mutate(ctx, id); err != nil { - t.Fatalf("mutating %s: %v", id, err) - } - if err := fixture.SetFlag(ctx, true); err != nil { - t.Fatalf("enabling versioned history: %v", err) - } - beforeCount, err := fixture.VersionRowCount(ctx, id) - if err != nil { - t.Fatalf("counting version rows for %s: %v", id, err) - } - - if err := fixture.MutateExisting(ctx, id); err != nil { - t.Fatalf("mutating existing %s: %v", id, err) - } - - afterCount, err := fixture.VersionRowCount(ctx, id) - if err != nil { - t.Fatalf("counting version rows for %s: %v", id, err) - } - if afterCount != beforeCount { - t.Errorf("version row count for %s went from %d to %d with BD_IGNORE_SCHEMA_SKEW=1 set, want "+ - "unchanged: the schema-skew hatch must not bypass the participation_generation fence — they "+ - "key on different planes (migration cursor vs. real column data) — so a write admitted "+ - "through the hatch against a legacy record must still be skipped", id, beforeCount, afterCount) - } -} diff --git a/backend/conformance/expected_revision_contract.go b/backend/conformance/expected_revision_contract.go deleted file mode 100644 index 7d71970e7..000000000 --- a/backend/conformance/expected_revision_contract.go +++ /dev/null @@ -1,599 +0,0 @@ -package conformance - -import ( - "context" - "errors" - "fmt" - "testing" - "time" -) - -// This file holds the vocabulary every one of the three new versioned-history -// contract files shares (Address, ChangeAttribution, Restriction, -// RetainedWindow, the PerRecordCASWrite hook shape, and the sentinel errors), -// plus the contract for R16/R17 (gastownhall/beads#6133, be-hs42e.1 §5-§7): -// the whole-of-state expectedRevision CAS write and its typed Refusal. -// -// THE SHARED VOCABULARY LIVES HERE, NOT IN A FOURTH FILE. All three contract -// files declare `package conformance`, so Go same-package visibility already -// gives cross-file access with no import; the architecture doc names exactly -// three contract files and does not say where their shared types live. This -// file's own §4a choice is to keep them beside R16/R17 rather than split out -// a vocabulary-only file with no tests of its own — R16/R17 is the first -// contract that needs them, so a reader asking "what is an Address" starts -// here. -// -// EVERY HOOK ON EVERY FIXTURE IN ALL THREE NEW FILES IS INDEPENDENTLY -// NILABLE — unlike metadata_cas_contract.go, where MetadataCAS itself is -// required and only two hooks are optional. Phase 0 ships zero real -// implementations of any of these capabilities in any backend's fixture kit -// (architecture §12: "every new hook is nil in every backend's fixture -// kit"), so every case below nil-checks every hook it uses and SKIPS BY NAME -// when one is missing — the same idiom history_matching.go uses for -// CountHistoryMatching. -// -// THIS SUITE DEFINES INTERFACES AND TESTS ONLY. Nothing introduced by this -// phase implements real CAS, invariant, retention, or epoch enforcement. The -// positive-path body of every Run function below is a complete, realistic -// assertion of what Phase 3's real hooks must do — because this suite IS the -// spec Phase 3 implements against (NFR-03) — but none of it runs today, -// since versioned_history_wiring_test.go wires every hook nil. -// -// RunExpectedRevisionCoversFieldsOutsideAnyWatchedSubset (R16-c) IS EXPECTED -// TO FAIL the moment a real backend wires CompareAndSetVersion, unless that -// backend has independently closed the row_lock partial-coverage gap the -// architecture doc's own §10 risk table names. That is deliberate — see the -// function's own doc comment — not a bug to relax the assertion around. -// -// DEFERRALS RECORDED HERE, NOT ACTED ON IN PHASE 0 (bee-ghosttrack's PR -// #6147 round-1 review; kept as ONE note so a later phase does not have to -// reconstruct these from PR comment history): -// -// - UNRETAINED AND DISCLOSURE-GATING ARE A FUTURE AUTHORIZATION AXIS, NOT A -// SIXTH Restriction VALUE. R11's bounded-window edge (an Address old -// enough to have fallen outside a retention window, distinct from Gone) -// and disclosure-gating (a store that presents a Gone or Unretained -// Address as Unknown to a caller not authorized to learn it existed at -// all) both layer an AUTHORIZATION dimension over the five states this -// file already defines — WHO is asking, not WHAT the store knows. Adding -// either as a sixth Restriction constant would conflate those two -// dimensions; a later phase should model authorization as an orthogonal -// axis instead. -// - INVARIANT-DECLARATION DISCOVERABILITY: nothing in this suite lets a -// caller enumerate which store invariants EnforceCrossRecordInvariant -// knows about ahead of a refusal — InvariantRefusal.InvariantName is -// only ever observed reactively. Deferred to whatever phase gives -// invariants a first-class registry. -// - LEASE TTL SELF-EXPIRY: LeaseHandle carries ExpiresAt -// (cross_record_invariant_contract.go) but nothing in this suite -// exercises a lease actually expiring on its own — only explicit -// Release. Deferred until a real lease primitive exists to observe -// timing against. -// - R20-j's DURABILITY IS NOT PROVEN ACROSS A RESTART in this suite — -// RunAStoreThatRemovesStateStillAnswersGoneDurably (retention_epoch_ -// contract.go) reads twice in the same process, which cannot -// distinguish real durability from an in-memory cache that merely -// outlives two calls. Deferred to the E2E tier, which can actually -// restart a backend between reads. -// - ERASURE'S GONE-RECORD TOKEN RETENTION (R5.1 machinery) is out of -// scope here: RunErasureMintsACorrectedVersionRatherThanEditingInPlace -// (retention_epoch_contract.go) checks that the original resolves -// GoneErasure and the correction resolves Live, not what token or -// tombstone content backs that GoneErasure answer. Deferred to the -// later phase that specifies R5.1's erasure-record shape. -// -// THE "_attribution" PATCH-KEY CONVENTION (expectedRevisionAttributionPatch, -// this file) IS THE CANONICAL WAY TO THREAD A ChangeAttribution THROUGH A -// PerRecordCASWrite's generic patch parameter. Phase 3's hook documentation -// should point implementers at this convention explicitly, so no backend -// invents a second one. - -// Address opaquely identifies one Version. It names no physical column — per -// be-hs42e.1 §5, this is conceptual vocabulary that any future backend's hook -// translates to and from whatever it actually stores. -type Address string - -// ChangeAttribution is who/what/why/when produced a Version, as observed by -// the transaction that produced it (be-hs42e.1 §5). Actor, Agent, and -// Message are independently nullable. -type ChangeAttribution struct { - Actor *string - Agent *string - Message *string - At time.Time -} - -// Restriction is one of the five states a store may report for an Address -// (be-hs42e.1 §5, FR-05). Gone and Unknown are deliberately distinct values, -// never conflated: Gone means the store once held this Version and no longer -// does; Unknown means the store has no knowledge of the lineage at all -// (FR-06) — e.g. it never synced far enough back to have an opinion. -type Restriction int - -const ( - RestrictionLive Restriction = iota - RestrictionGoneRetention - RestrictionGoneErasure - RestrictionGoneReorganization - RestrictionUnknown -) - -func (r Restriction) String() string { - switch r { - case RestrictionLive: - return "live" - case RestrictionGoneRetention: - return "gone-retention" - case RestrictionGoneErasure: - return "gone-erasure" - case RestrictionGoneReorganization: - return "gone-reorganization" - case RestrictionUnknown: - return "unknown" - default: - return fmt.Sprintf("Restriction(%d)", int(r)) - } -} - -// RetainedWindow is the surviving range a store reports once some Addresses -// within a lineage have gone (be-hs42e.1 §5, R20-c/h). -type RetainedWindow struct { - LowerBound Address - UpperBound Address -} - -// PerRecordCASWrite is the shape every per-record whole-of-state CAS write -// takes. ExpectedRevisionFixture.CompareAndSetVersion and -// CrossRecordInvariantFixture.GuardedWrite share this EXACT type on purpose: -// R16.1-a's boundary claim is literally that the second is the first, called -// twice — sharing the type makes that claim structural, not just asserted. -type PerRecordCASWrite func(ctx context.Context, id string, expected *Address, patch map[string]any) (ExpectedRevisionResult, error) - -// ExpectedRevisionResult is the outcome of a PerRecordCASWrite call. Accepted -// and Refusal are mutually exclusive: read Refusal only when Accepted is -// false, read NewVersion only when it is true. -type ExpectedRevisionResult struct { - Accepted bool - NewVersion Address - Refusal *Refusal -} - -// Refusal is R17's typed, machine-readable outcome for a write that named a -// version no longer current — never a generic error, never indistinguishable -// from an accepted write (R17-c/d1). -// -// THE FIXTURE KIT IS THE TRANSLATION BOUNDARY, NOT A SHAPE MANDATE ON REAL -// BACKENDS: R17 itself permits either of two shipped refusal shapes — a -// (result, nil-error) pair carrying a populated Refusal, or a typed native -// error a backend already returns for this case. ExpectedRevisionFixture's -// CompareAndSetVersion hook normalizes whichever shape a real backend ships -// to this ONE Refusal-populated-result shape for the suite to assert -// against uniformly. Nothing here should be read as banning a native -// typed-error refusal shape at the backend level — only as saying the -// fixture adapter that wires a backend into this suite must translate it -// into this shape before handing it back. -type Refusal struct { - // RefusingVersion is the Address that was actually current when the - // write was evaluated (R17-a). - RefusingVersion Address - // Attribution is RefusingVersion's ChangeAttribution, as observed by the - // refusing transaction (R17-b). - Attribution ChangeAttribution -} - -// ErrRevisionNotFound means the write named an id with no current version at -// all — distinct from a Refusal, which requires a current version to refuse -// against (R17-d2). -var ErrRevisionNotFound = errors.New("expected-revision: id has no current version") - -// ErrRevisionValidation means the write itself was malformed, independent of -// whether its expectation held (R17-d3). -var ErrRevisionValidation = errors.New("expected-revision: request is invalid") - -// ExpectedRevisionFixture supplies the whole-of-state CAS capability under -// test (R16, R17): a write that names the Version it expects to be current, -// accepted only while that expectation holds, refused with a typed Refusal -// otherwise. -type ExpectedRevisionFixture struct { - IssuePrefix string - - // CompareAndSetVersion performs a whole-of-state CAS write: it is - // accepted only while expected (nil meaning "no version named", i.e. an - // unconditional write, R16-b) names the currently-current Address. A nil - // hook means this backend does not yet implement expectedRevision CAS, - // and every case in this file SKIPS with that reason. - CompareAndSetVersion PerRecordCASWrite - - // CurrentVersion reports the Address currently current for id. A nil - // hook means this backend cannot independently confirm the current - // Address, and cases that need one skip with that reason. - CurrentVersion func(ctx context.Context, id string) (Address, error) - - // MutateOutsideExpectedRevision writes to id through a path R16-c - // specifically targets: one NOT gated by the whole-of-state precondition - // (the architecture doc names row_lock's documented partial-coverage gap - // as the concrete probe — see §7 risk, §10 risk table). A nil hook means - // this backend exposes no such out-of-band path, and the one case that - // needs it skips with that reason. - MutateOutsideExpectedRevision func(ctx context.Context, id string, field, value string) error -} - -// RunExpectedRevisionAcceptsAWriteNamingTheCurrentVersion pins R16's -// baseline: a write naming the Address that is actually current must be -// accepted. -func RunExpectedRevisionAcceptsAWriteNamingTheCurrentVersion(t *testing.T, ctx context.Context, fixture ExpectedRevisionFixture) { - t.Helper() - if fixture.CompareAndSetVersion == nil { - t.Skip("this backend does not implement expectedRevision CAS (CompareAndSetVersion is nil)") - } - if fixture.CurrentVersion == nil { - t.Skip("this backend cannot independently confirm the current Address (CurrentVersion is nil)") - } - id, seeded := expectedRevisionSeed(t, ctx, fixture, "current", map[string]any{"phase": "start"}) - - current, err := fixture.CurrentVersion(ctx, id) - if err != nil { - t.Fatalf("CurrentVersion(%s): %v", id, err) - } - if current != seeded.NewVersion { - t.Fatalf("CurrentVersion(%s) = %s, want the seed's NewVersion %s", id, current, seeded.NewVersion) - } - - result, err := fixture.CompareAndSetVersion(ctx, id, ¤t, map[string]any{"phase": "advanced"}) - if err != nil { - t.Fatalf("CompareAndSetVersion(%s, expected=current): %v", id, err) - } - if !result.Accepted { - t.Fatalf("CompareAndSetVersion naming the true current Address was refused (Refusal=%+v), want Accepted (R16: a write naming the current Version must be accepted)", result.Refusal) - } -} - -// RunExpectedRevisionAcceptsAWriteNamingNoVersion pins R16-b's paraphrase: a -// nil expectation means an unconditional write, not "the key must be -// absent" — the unconditional path must still exist over a record that -// already has a current Version. -func RunExpectedRevisionAcceptsAWriteNamingNoVersion(t *testing.T, ctx context.Context, fixture ExpectedRevisionFixture) { - t.Helper() - if fixture.CompareAndSetVersion == nil { - t.Skip("this backend does not implement expectedRevision CAS (CompareAndSetVersion is nil)") - } - id, _ := expectedRevisionSeed(t, ctx, fixture, "unconditional", map[string]any{"phase": "start"}) - - result, err := fixture.CompareAndSetVersion(ctx, id, nil, map[string]any{"phase": "overwritten"}) - if err != nil { - t.Fatalf("CompareAndSetVersion(%s, expected=nil) over an existing record: %v", id, err) - } - if !result.Accepted { - t.Fatal("CompareAndSetVersion(expected=nil) over an existing record was refused, want Accepted (R16-b: nil means an unconditional write, not \"must be absent\")") - } -} - -// RunExpectedRevisionCoversFieldsOutsideAnyWatchedSubset pins R16's -// whole-of-state promise against the concrete gap the architecture doc names -// (§7 risk, §10 risk table): row_lock's documented partial-coverage leaves -// some fields writable without reminting the Version those fields belong to. -// A write through MutateOutsideExpectedRevision must still count as a change -// against the expectation — otherwise a caller's CAS can silently observe a -// stale precondition as current. -// -// THIS CASE IS EXPECTED TO FAIL the moment a real backend wires -// CompareAndSetVersion, UNLESS that backend has independently closed the -// row_lock gap first (architecture §10 risk table: "this suite existing and -// failing loudly against that gap IS THE POINT"). It is not a bug in this -// test if it starts failing against a real hook — it is the suite doing its -// job. Do not weaken this assertion to make a real backend pass; close the -// gap instead. -func RunExpectedRevisionCoversFieldsOutsideAnyWatchedSubset(t *testing.T, ctx context.Context, fixture ExpectedRevisionFixture) { - t.Helper() - if fixture.CompareAndSetVersion == nil { - t.Skip("this backend does not implement expectedRevision CAS (CompareAndSetVersion is nil)") - } - if fixture.MutateOutsideExpectedRevision == nil { - t.Skip("this backend exposes no out-of-band mutation path to probe the whole-of-state boundary with (MutateOutsideExpectedRevision is nil)") - } - id, seeded := expectedRevisionSeed(t, ctx, fixture, "outofband", map[string]any{"phase": "start"}) - staleExpected := seeded.NewVersion - - if err := fixture.MutateOutsideExpectedRevision(ctx, id, "side_channel", "changed"); err != nil { - t.Fatalf("MutateOutsideExpectedRevision(%s): %v", id, err) - } - - result, err := fixture.CompareAndSetVersion(ctx, id, &staleExpected, map[string]any{"phase": "advanced"}) - if err != nil { - t.Fatalf("CompareAndSetVersion(%s, expected=pre-mutation version): %v", id, err) - } - if result.Accepted { - t.Fatal("CompareAndSetVersion naming the pre-mutation Address was accepted after an out-of-band field write; " + - "R16's whole-of-state precondition must cover fields outside any single watched subset") - } -} - -// RunRefusalReportsTheRefusingVersionAddress pins R17-a: a Refusal names the -// Address that was actually current when the write was evaluated, read here -// independently via CurrentVersion rather than trusted from the caller's own -// stale value. -func RunRefusalReportsTheRefusingVersionAddress(t *testing.T, ctx context.Context, fixture ExpectedRevisionFixture) { - t.Helper() - if fixture.CompareAndSetVersion == nil { - t.Skip("this backend does not implement expectedRevision CAS (CompareAndSetVersion is nil)") - } - if fixture.CurrentVersion == nil { - t.Skip("this backend cannot independently confirm the current Address (CurrentVersion is nil)") - } - id, stale := expectedRevisionSeedStale(t, ctx, fixture, "refaddr") - - result, err := fixture.CompareAndSetVersion(ctx, id, &stale, map[string]any{"phase": "clobber"}) - if err != nil { - t.Fatalf("CompareAndSetVersion(%s, stale expected): %v", id, err) - } - if result.Accepted || result.Refusal == nil { - t.Fatalf("CompareAndSetVersion over a stale expectation = %+v, want a Refusal", result) - } - - real, err := fixture.CurrentVersion(ctx, id) - if err != nil { - t.Fatalf("CurrentVersion(%s): %v", id, err) - } - if result.Refusal.RefusingVersion != real { - t.Errorf("Refusal.RefusingVersion = %s, want the real current Address %s (R17-a)", result.Refusal.RefusingVersion, real) - } -} - -// RunRefusalReportsTheRefusingVersionsChangeAttribution pins R17-b: a -// Refusal carries the ChangeAttribution the refusing transaction observed -// for the version that beat the caller. -// -// §4a: PerRecordCASWrite's patch is a generic map[string]any with no -// dedicated attribution field, so this case threads a ChangeAttribution -// through the documented patch["_attribution"] convention -// (expectedRevisionAttributionPatch) — this file's own choice, since the -// architecture doc does not pin how a caller supplies attribution on a -// per-record write, only that the backend must observe and later report it. -func RunRefusalReportsTheRefusingVersionsChangeAttribution(t *testing.T, ctx context.Context, fixture ExpectedRevisionFixture) { - t.Helper() - if fixture.CompareAndSetVersion == nil { - t.Skip("this backend does not implement expectedRevision CAS (CompareAndSetVersion is nil)") - } - actor := "cas-winner" - wantAttribution := ChangeAttribution{Actor: &actor, At: time.Now().UTC()} - - id, seeded := expectedRevisionSeed(t, ctx, fixture, "refattr", map[string]any{"phase": "start"}) - staleAddr := seeded.NewVersion - advanced, err := fixture.CompareAndSetVersion(ctx, id, &staleAddr, - expectedRevisionAttributionPatch(map[string]any{"phase": "moved-on"}, wantAttribution)) - if err != nil { - t.Fatalf("advancing %s with a known attribution: %v", id, err) - } - if !advanced.Accepted { - t.Fatalf("advancing %s with a known attribution was refused", id) - } - - result, err := fixture.CompareAndSetVersion(ctx, id, &staleAddr, map[string]any{"phase": "clobber"}) - if err != nil { - t.Fatalf("CompareAndSetVersion(%s, stale expected): %v", id, err) - } - if result.Accepted || result.Refusal == nil { - t.Fatalf("CompareAndSetVersion over a stale expectation = %+v, want a Refusal", result) - } - if !sameAttribution(result.Refusal.Attribution, wantAttribution) { - t.Errorf("Refusal.Attribution = %+v, want %+v (R17-b: the attribution the refusing transaction observed)", result.Refusal.Attribution, wantAttribution) - } -} - -// RunRefusalIsATypedOutcomeNotAGenericError pins R17-c/d1: a stale -// expectation comes back as a populated Refusal with a nil error, never as a -// generic Go error. -func RunRefusalIsATypedOutcomeNotAGenericError(t *testing.T, ctx context.Context, fixture ExpectedRevisionFixture) { - t.Helper() - if fixture.CompareAndSetVersion == nil { - t.Skip("this backend does not implement expectedRevision CAS (CompareAndSetVersion is nil)") - } - id, stale := expectedRevisionSeedStale(t, ctx, fixture, "typedoutcome") - - result, err := fixture.CompareAndSetVersion(ctx, id, &stale, map[string]any{"phase": "clobber"}) - if err != nil { - t.Fatalf("a stale expectation returned error = %v, want nil: R17 reports a refusal, not an error (R17-c)", err) - } - if result.Accepted { - t.Fatal("a stale expectation was accepted") - } - if result.Refusal == nil { - t.Fatal("Refusal = nil on a non-accepted result, want a populated Refusal (R17-d1)") - } -} - -// RunRefusalIsDistinguishableFromAnAcceptedWrite runs an accepted and a -// refused case side by side and pins that Accepted is the only signal a -// caller needs: Refusal and NewVersion are never both populated, and never -// both empty. -func RunRefusalIsDistinguishableFromAnAcceptedWrite(t *testing.T, ctx context.Context, fixture ExpectedRevisionFixture) { - t.Helper() - if fixture.CompareAndSetVersion == nil { - t.Skip("this backend does not implement expectedRevision CAS (CompareAndSetVersion is nil)") - } - - for _, test := range []struct { - name string - prepare func(t *testing.T) (string, Address) - }{ - {"accepted", func(t *testing.T) (string, Address) { - id, seeded := expectedRevisionSeed(t, ctx, fixture, "distinguish-accept", map[string]any{"phase": "start"}) - return id, seeded.NewVersion - }}, - {"refused", func(t *testing.T) (string, Address) { - return expectedRevisionSeedStale(t, ctx, fixture, "distinguish-refuse") - }}, - } { - t.Run(test.name, func(t *testing.T) { - id, expected := test.prepare(t) - result, err := fixture.CompareAndSetVersion(ctx, id, &expected, map[string]any{"phase": "attempt"}) - if err != nil { - t.Fatalf("CompareAndSetVersion(%s): %v", id, err) - } - if result.Accepted && result.Refusal != nil { - t.Fatalf("result = %+v, want Refusal populated only when Accepted is false", result) - } - if !result.Accepted && result.Refusal == nil { - t.Fatalf("result = %+v, want Refusal populated when Accepted is false", result) - } - if result.Accepted && result.NewVersion == "" { - t.Error("Accepted = true but NewVersion is empty") - } - }) - } -} - -// RunRefusalIsDistinguishableFromNotFound pins R17-d2: an id with no current -// version at all is ErrRevisionNotFound, never a Refusal — a Refusal -// requires a current version to refuse against. -func RunRefusalIsDistinguishableFromNotFound(t *testing.T, ctx context.Context, fixture ExpectedRevisionFixture) { - t.Helper() - if fixture.CompareAndSetVersion == nil { - t.Skip("this backend does not implement expectedRevision CAS (CompareAndSetVersion is nil)") - } - ghost := Address("ghost-version") - id := fixture.IssuePrefix + "-exprev-neverseeded" - - result, err := fixture.CompareAndSetVersion(ctx, id, &ghost, map[string]any{"phase": "attempt"}) - if !errors.Is(err, ErrRevisionNotFound) { - t.Fatalf("CompareAndSetVersion on a never-seeded id error = %v, want ErrRevisionNotFound (R17-d2)", err) - } - if result.Accepted || result.Refusal != nil { - t.Errorf("result = %+v, want neither Accepted nor a Refusal for a not-found id", result) - } -} - -// RunRefusalIsDistinguishableFromValidationFailure pins R17-d3: a malformed -// request is ErrRevisionValidation, independent of whether its expectation -// would have held. -func RunRefusalIsDistinguishableFromValidationFailure(t *testing.T, ctx context.Context, fixture ExpectedRevisionFixture) { - t.Helper() - if fixture.CompareAndSetVersion == nil { - t.Skip("this backend does not implement expectedRevision CAS (CompareAndSetVersion is nil)") - } - result, err := fixture.CompareAndSetVersion(ctx, "", nil, map[string]any{"phase": "attempt"}) - if !errors.Is(err, ErrRevisionValidation) { - t.Fatalf("CompareAndSetVersion with an empty id error = %v, want ErrRevisionValidation (R17-d3)", err) - } - if result.Accepted || result.Refusal != nil { - t.Errorf("result = %+v, want neither Accepted nor a Refusal for a malformed request", result) - } -} - -// RunRefusalNeverSilentlyPicksAWinner pins the race shape underneath R17: -// two writes competing over the same stale expectation must produce exactly -// one Accepted and one real Refusal — never both accepted, never a merged or -// silently overwritten result — and the Address left current afterward must -// be the winner's, read independently. -func RunRefusalNeverSilentlyPicksAWinner(t *testing.T, ctx context.Context, fixture ExpectedRevisionFixture) { - t.Helper() - if fixture.CompareAndSetVersion == nil { - t.Skip("this backend does not implement expectedRevision CAS (CompareAndSetVersion is nil)") - } - if fixture.CurrentVersion == nil { - t.Skip("this backend cannot independently confirm the current Address (CurrentVersion is nil)") - } - id, seeded := expectedRevisionSeed(t, ctx, fixture, "competing", map[string]any{"phase": "start"}) - shared := seeded.NewVersion - - first, err := fixture.CompareAndSetVersion(ctx, id, &shared, map[string]any{"phase": "writer-a"}) - if err != nil { - t.Fatalf("writer A: CompareAndSetVersion(%s): %v", id, err) - } - second, err := fixture.CompareAndSetVersion(ctx, id, &shared, map[string]any{"phase": "writer-b"}) - if err != nil { - t.Fatalf("writer B: CompareAndSetVersion(%s): %v", id, err) - } - - if first.Accepted == second.Accepted { - t.Fatalf("two competing writes against the same expectation both reported Accepted=%v, want exactly one to win", first.Accepted) - } - winner, loser := first, second - if second.Accepted { - winner, loser = second, first - } - if loser.Refusal == nil { - t.Fatal("the losing write carries no Refusal") - } - - real, err := fixture.CurrentVersion(ctx, id) - if err != nil { - t.Fatalf("CurrentVersion(%s): %v", id, err) - } - if real != winner.NewVersion { - t.Errorf("CurrentVersion(%s) = %s after the race, want the winner's NewVersion %s: a third value means the race was decided somewhere other than the two calls made here", id, real, winner.NewVersion) - } -} - -// --- fixture helpers ------------------------------------------------------- - -// expectedRevisionSeed performs an unconditional (expected=nil) write to -// mint id's first Version, and fatals unless the fixture accepts it — every -// case in this file needs a live record to test a whole-of-state CAS -// against, and CompareAndSetVersion(expected=nil) is the only write path -// this fixture exposes to make one. -func expectedRevisionSeed(t *testing.T, ctx context.Context, fixture ExpectedRevisionFixture, tag string, patch map[string]any) (string, ExpectedRevisionResult) { - t.Helper() - id := fixture.IssuePrefix + "-exprev-" + tag - if patch == nil { - patch = map[string]any{} - } - result, err := fixture.CompareAndSetVersion(ctx, id, nil, patch) - if err != nil { - t.Fatalf("seeding %s: CompareAndSetVersion(expected=nil) error = %v", id, err) - } - if !result.Accepted { - t.Fatalf("seeding %s: CompareAndSetVersion(expected=nil) Accepted = false, want true (R16-b: an unconditional write to a fresh id must be accepted)", id) - } - return id, result -} - -// expectedRevisionSeedStale seeds id, advances it once more, and returns the -// Address from BEFORE that second write — a caller naming this Address as -// Expected has a stale expectation, because the second write has since -// moved the current Version on. -func expectedRevisionSeedStale(t *testing.T, ctx context.Context, fixture ExpectedRevisionFixture, tag string) (string, Address) { - t.Helper() - id, seeded := expectedRevisionSeed(t, ctx, fixture, tag, map[string]any{"phase": "start"}) - staleAddr := seeded.NewVersion - advanced, err := fixture.CompareAndSetVersion(ctx, id, &staleAddr, map[string]any{"phase": "moved-on"}) - if err != nil { - t.Fatalf("advancing %s past the address a later case will treat as stale: %v", id, err) - } - if !advanced.Accepted { - t.Fatalf("advancing %s past the address a later case will treat as stale was refused", id) - } - return id, staleAddr -} - -// expectedRevisionAttributionPatch adds attribution to patch under the -// "_attribution" key — the convention this file uses to thread a -// ChangeAttribution through PerRecordCASWrite's generic patch parameter. See -// RunRefusalReportsTheRefusingVersionsChangeAttribution's doc comment for -// why this convention exists. -func expectedRevisionAttributionPatch(patch map[string]any, attribution ChangeAttribution) map[string]any { - if patch == nil { - patch = map[string]any{} - } - patch["_attribution"] = attribution - return patch -} - -// sameAttribution compares two ChangeAttribution values by content. It does -// not use reflect.DeepEqual: comparing time.Time by its internal -// representation is fragile (monotonic readings can differ for an otherwise -// equal instant), so At is compared with time.Time.Equal and the nullable -// string fields are compared by dereferenced value. -func sameAttribution(a, b ChangeAttribution) bool { - return samePtrString(a.Actor, b.Actor) && - samePtrString(a.Agent, b.Agent) && - samePtrString(a.Message, b.Message) && - a.At.Equal(b.At) -} - -func samePtrString(a, b *string) bool { - if a == nil || b == nil { - return a == b - } - return *a == *b -} diff --git a/backend/conformance/heartbeater_contract.go b/backend/conformance/heartbeater_contract.go deleted file mode 100644 index bde6409f0..000000000 --- a/backend/conformance/heartbeater_contract.go +++ /dev/null @@ -1,837 +0,0 @@ -package conformance - -import ( - "context" - "errors" - "strings" - "testing" - "time" - - storeops "github.com/steveyegge/beads/internal/storage/issueops" - "github.com/steveyegge/beads/internal/types" - publicops "github.com/steveyegge/beads/issueops" -) - -// This file holds the contract every implementation of publicops.Heartbeater -// must satisfy. Each case asserts what issueops/heartbeater.go PROMISES, cited -// by symbol. -// -// THREE LEGS, ONE BODY. dolt, embedded dolt and the unit-of-work provider all -// reach internal/storage/issueops.ExecuteHeartbeatInTx, so a per-leg failure -// here is a wrapper's failure: a commit the leg added, a hook it fired, a -// refusal that stopped matching errors.Is. -// -// THE QUIET CASE IS THE POINT. A heartbeat is the hottest write in a fleet and -// it is cheap because it writes the lease row and nothing else. Most of what -// RunHeartbeaterChangesNothingOnTheRow asserts is an ABSENCE — no revision, no -// version, no updated_at, no event, no journal entry, no history entry, no -// hook — and each absence is something a plausible wrapper would add. -// -// THE LIVE-CLAIM FENCE IS AN INVARIANT AND A PREDICATE. The RefusesAfter* -// cases hold the invariant: each ends a claim one way and asserts both that -// the lease row is gone and that the holder's next heartbeat is refused. The -// RefusesAnOrphanedLease* cases hold the predicate: a lease row that survives -// on an issue that is no longer claimed by its holder (what a re-arm racing a -// release or close can leave on Dolt) is refused AND deleted, never refreshed. -type HeartbeaterFixture struct { - // IssuePrefix namespaces the ids each assertion seeds, so several of them - // can share one database. - IssuePrefix string - // Heartbeater is the surface under test. - Heartbeater publicops.Heartbeater - // CreateIssue seeds a durable issue in the issues plane. - CreateIssue func(context.Context, *types.Issue, string) error - // CreateWisp seeds an ephemeral issue in the wisps plane. A nil CreateWisp - // SKIPS the wisp case loudly. - CreateWisp func(context.Context, *types.Issue, string) error - // Claimer takes the claim, and with it the lease row, a heartbeat - // refreshes. - Claimer publicops.Claimer - // Lifecycle ends or moves a claim (close, a status move, a hand-doled - // claim) and performs the guarded write the revision case lands. - Lifecycle publicops.Lifecycle - // Releaser gives a claim up for the release case. Nil SKIPS that case. - Releaser publicops.Releaser - // LeaseReclaimer reverts an aged claim for the reclaim case. Nil, or a nil - // Exec, SKIPS that case. - LeaseReclaimer publicops.LeaseReclaimer - // Exec runs raw statements against the backing database: the cases use it - // to age a lease. - Exec func(ctx context.Context, statements []SQLStatement) error - // ExecUncommitted runs raw statements as ANOTHER writer that leaves its - // change uncommitted in the version-controlled working set (a batch-mode - // edit): SQL-committed, no version-control commit. Nil means Exec already - // behaves so. - ExecUncommitted func(ctx context.Context, statements []SQLStatement) error - // QueryScalar runs a single-row query and scans it, so the post-state is - // read RAW rather than through the role being tested. - QueryScalar func(context.Context, string, []any, ...any) error - // CountHistory reports how many version-control entries the fixture's - // branch has. Nil SKIPS that one assertion, loudly, in the quiet case. - CountHistory func(context.Context) (int, error) - // SetJournalEnabled turns on durable journaling, so the quiet case can - // observe that a heartbeat records nothing in bd_events_journal. Nil SKIPS - // that one assertion, loudly. - SetJournalEnabled func(enabled bool) - // SetVersionedHistoryEnabled turns on dual-write issue-version history, so - // the quiet case can observe that a heartbeat records no issue_versions - // row. Nil SKIPS that one assertion, loudly. - SetVersionedHistoryEnabled func(enabled bool) - // HookedHeartbeater is the same role taken off the leg's HOOK-FIRING chain - // (storage.HookFiringStore on the store legs, uow.NewNotifyingProvider on - // the unit-of-work leg), built over the runner FiredUpdates reads. Nil, - // with FiredUpdates, SKIPS the no-hook assertion loudly. - HookedHeartbeater publicops.Heartbeater - // FiredUpdates reports, in firing order, the issue id of every on_update - // the hooked chain ran so far. - FiredUpdates func(context.Context) ([]string, error) -} - -const ( - heartbeaterHolder = "hb-holder" - heartbeaterIntruder = "hb-intruder" - heartbeaterReaper = "hb-reaper" -) - -// RunHeartbeaterRefreshesTheHoldersLease pins the refresh itself -// (Heartbeater: "pushes the lease the caller holds forward by the store's -// TTL") and every member of the HeartbeatResult, against the row READ RAW. -func RunHeartbeaterRefreshesTheHoldersLease(t *testing.T, ctx context.Context, fixture HeartbeaterFixture) { - t.Helper() - requireHeartbeaterExec(t, fixture) - id := fixture.IssuePrefix + "-refresh" - heartbeaterSeedClaimed(t, ctx, fixture, id) - aged := time.Now().UTC().Add(-time.Minute).Truncate(time.Second) - if err := fixture.Exec(ctx, []SQLStatement{{ - Query: "UPDATE leases SET lease_expires_at = ?, heartbeat_at = ? WHERE issue_id = ?", - Args: []any{aged, aged, id}, - }}); err != nil { - t.Fatalf("age the lease on %s: %v", id, err) - } - - before := time.Now().UTC().Add(-time.Second) - result := heartbeat(t, ctx, fixture.Heartbeater, publicops.HeartbeatRequest{Actor: heartbeaterHolder, IssueID: id}) - after := time.Now().UTC().Add(time.Second) - - if result.IssueID != id || result.Holder != heartbeaterHolder || result.Rearmed { - t.Fatalf("result = %+v, want {IssueID:%s Holder:%s Rearmed:false}", result, id, heartbeaterHolder) - } - if result.HeartbeatAt.Before(before) || result.HeartbeatAt.After(after) { - t.Fatalf("HeartbeatAt = %v, want the server clock at the call (between %v and %v)", result.HeartbeatAt, before, after) - } - if ttl := result.LeaseExpiresAt.Sub(result.HeartbeatAt); heartbeaterAbs(ttl-storeops.DefaultLeaseTTL) > time.Second { - t.Fatalf("LeaseExpiresAt - HeartbeatAt = %v, want the store's lease TTL %v", ttl, storeops.DefaultLeaseTTL) - } - if moved := heartbeaterScalar[int](t, ctx, fixture, - "SELECT COUNT(*) FROM leases WHERE issue_id = ? AND holder = ? AND lease_expires_at > ? AND heartbeat_at > ?", - id, heartbeaterHolder, aged, aged); moved != 1 { - t.Fatal("the lease row's lease_expires_at and heartbeat_at did not move forward") - } - heartbeaterRequireStoredMatches(t, ctx, fixture, id, result, before, after) -} - -// RunHeartbeaterASameSecondHeartbeatIsNotARearm pins HeartbeatResult.Rearmed -// against the refresh that changes no value: a heartbeat landing in the same -// second as the claim or the previous heartbeat writes values identical to the -// row's (the lease columns hold whole seconds), which some engines report as -// zero rows affected. It is still a refresh of the caller's own row: Rearmed -// is false, and the lease's granted_at and granted_node do not move. -func RunHeartbeaterASameSecondHeartbeatIsNotARearm(t *testing.T, ctx context.Context, fixture HeartbeaterFixture) { - t.Helper() - requireHeartbeaterExec(t, fixture) - for i := 0; i < 3; i++ { - id := fixture.IssuePrefix + "-samesec-" + string(rune('a'+i)) - heartbeaterSeedClaimed(t, ctx, fixture, id) - granted := heartbeaterScalar[string](t, ctx, fixture, - "SELECT CONCAT(CAST(granted_at AS CHAR), '|', COALESCE(granted_node, '')) FROM leases WHERE issue_id = ?", id) - for beat := 0; beat < 2; beat++ { - result := heartbeat(t, ctx, fixture.Heartbeater, publicops.HeartbeatRequest{Actor: heartbeaterHolder, IssueID: id}) - if result.Rearmed { - t.Fatalf("heartbeat %d on %s right after the claim = %+v, want Rearmed:false: the holder's lease row existed", beat+1, id, result) - } - } - if got := heartbeaterScalar[string](t, ctx, fixture, - "SELECT CONCAT(CAST(granted_at AS CHAR), '|', COALESCE(granted_node, '')) FROM leases WHERE issue_id = ?", id); got != granted { - t.Fatalf("granted_at|granted_node on %s moved from %s to %s across refreshes; a refresh never re-grants", id, granted, got) - } - } -} - -// RunHeartbeaterChangesNothingOnTheRow pins the role's quiet promise -// (Heartbeater: "A REFRESH WRITES ONLY THE LEASE ROW"): the issue row's revision -// (row_lock), its current_revision, its updated_at, its events, the events -// journal, issue_versions, the version-control history and the hooks are all -// exactly where the claim left them. -func RunHeartbeaterChangesNothingOnTheRow(t *testing.T, ctx context.Context, fixture HeartbeaterFixture) { - t.Helper() - requireHeartbeaterExec(t, fixture) - if fixture.SetJournalEnabled != nil { - fixture.SetJournalEnabled(true) - defer fixture.SetJournalEnabled(false) - } else { - t.Log("HeartbeaterFixture.SetJournalEnabled is nil: the journal assertion is skipped on this backend") - } - if fixture.SetVersionedHistoryEnabled != nil { - fixture.SetVersionedHistoryEnabled(true) - defer fixture.SetVersionedHistoryEnabled(false) - } else { - t.Log("HeartbeaterFixture.SetVersionedHistoryEnabled is nil: the issue_versions assertion is skipped on this backend") - } - id := fixture.IssuePrefix + "-quiet" - heartbeaterSeedClaimed(t, ctx, fixture, id) - // updated_at is stored at second precision on some backends; step past - // the claim's second so a stamp the heartbeat added cannot hide in it. - time.Sleep(1100 * time.Millisecond) - - snapshot := func() heartbeaterRowSnapshot { - return heartbeaterSnapshot(t, ctx, fixture, id) - } - before := snapshot() - fired := heartbeaterFired(t, ctx, fixture, id) - - surface := fixture.Heartbeater - if fixture.HookedHeartbeater != nil && fixture.FiredUpdates != nil { - surface = fixture.HookedHeartbeater - } else { - t.Log("HeartbeaterFixture.HookedHeartbeater/FiredUpdates is nil: the no-hook assertion is skipped on this backend") - } - heartbeat(t, ctx, surface, publicops.HeartbeatRequest{Actor: heartbeaterHolder, IssueID: id}) - - after := snapshot() - if before != after { - t.Fatalf("a heartbeat changed the issue's durable state:\nbefore %+v\nafter %+v", before, after) - } - if got := heartbeaterFired(t, ctx, fixture, id); got != fired { - t.Fatalf("on_update fired %d time(s) for %s across a refresh, want 0: a refresh fires no hook", got-fired, id) - } -} - -// RunHeartbeaterAGuardedWriteWithThePreHeartbeatRevisionStillWins is the -// reason the quiet case matters to a caller: the holder's revision token -// survives its own heartbeats, so a guarded write it composed from the claim -// still lands. -func RunHeartbeaterAGuardedWriteWithThePreHeartbeatRevisionStillWins(t *testing.T, ctx context.Context, fixture HeartbeaterFixture) { - t.Helper() - if fixture.Lifecycle == nil { - t.Skip("HeartbeaterFixture.Lifecycle is nil: this backend cannot land a guarded write") - } - id := fixture.IssuePrefix + "-guarded" - revision := heartbeaterSeedClaimed(t, ctx, fixture, id) - - heartbeat(t, ctx, fixture.Heartbeater, publicops.HeartbeatRequest{Actor: heartbeaterHolder, IssueID: id}) - heartbeat(t, ctx, fixture.Heartbeater, publicops.HeartbeatRequest{Actor: heartbeaterHolder, IssueID: id}) - - if _, err := fixture.Lifecycle.Update(ctx, publicops.UpdateRequest{ - Actor: heartbeaterHolder, IssueID: id, ExpectedVersion: &revision, - Patch: publicops.IssuePatch{Notes: publicops.Field[string]{Set: true, Value: "after two heartbeats"}}, - }); err != nil { - t.Fatalf("guarded write with the claim's revision after heartbeats = %v, want it to land", err) - } -} - -// RunHeartbeaterRefusesAForeignHolder pins the fence (Heartbeater REFUSALS: -// "an issue another actor holds: a *LeaseConflictError naming the Holder, -// wrapping ErrAlreadyClaimed"), and that the refusal writes nothing. -func RunHeartbeaterRefusesAForeignHolder(t *testing.T, ctx context.Context, fixture HeartbeaterFixture) { - t.Helper() - requireHeartbeaterExec(t, fixture) - id := fixture.IssuePrefix + "-foreign" - heartbeaterSeedClaimed(t, ctx, fixture, id) - // Age the holder's lease first, so a heartbeat that ignored the holder - // fence would really CHANGE the row (some engines count only changed rows, - // and the columns hold whole seconds: an unaged row refreshed in the - // claim's second changes nothing and would hide the missing fence). - aged := time.Now().UTC().Add(-time.Minute).Truncate(time.Second) - if err := fixture.Exec(ctx, []SQLStatement{{ - Query: "UPDATE leases SET lease_expires_at = ?, heartbeat_at = ? WHERE issue_id = ?", - Args: []any{aged, aged, id}, - }}); err != nil { - t.Fatalf("age the lease on %s: %v", id, err) - } - lease := heartbeaterScalar[string](t, ctx, fixture, "SELECT CONCAT(CAST(lease_expires_at AS CHAR), '|', CAST(heartbeat_at AS CHAR), '|', holder) FROM leases WHERE issue_id = ?", id) - - _, err := fixture.Heartbeater.Heartbeat(ctx, publicops.HeartbeatRequest{Actor: heartbeaterIntruder, IssueID: id}) - if !errors.Is(err, publicops.ErrAlreadyClaimed) { - t.Fatalf("heartbeat by a non-holder = %v, want ErrAlreadyClaimed", err) - } - if errors.Is(err, publicops.ErrLeaseNotHeld) { - t.Fatalf("heartbeat by a non-holder = %v also matches ErrLeaseNotHeld; the two refusals must stay distinct", err) - } - conflict := heartbeaterConflict(t, err) - if conflict.IssueID != id || conflict.Holder != heartbeaterHolder || conflict.Status != types.StatusInProgress { - t.Fatalf("LeaseConflictError = %+v, want {IssueID:%s Holder:%s Status:%s}", conflict, id, heartbeaterHolder, types.StatusInProgress) - } - if got := heartbeaterScalar[string](t, ctx, fixture, "SELECT CONCAT(CAST(lease_expires_at AS CHAR), '|', CAST(heartbeat_at AS CHAR), '|', holder) FROM leases WHERE issue_id = ?", id); got != lease { - t.Fatalf("a refused heartbeat moved the holder's lease from %s to %s", lease, got) - } -} - -// RunHeartbeaterCommitsOnlyItsOwnTables pins the commit discipline against a -// DIRTY working set (Heartbeater: "A REFRESH WRITES ONLY THE LEASE ROW"; "A -// RE-ARM IS RECORDED AS A MUTATION"): another writer has left an uncommitted -// versioned change (a batch-mode edit, here a config row). A refresh and a -// refusal make no version-control commit and leave that change uncommitted; -// a re-arm makes exactly one commit, of its own tables, and still leaves the -// other writer's change uncommitted rather than sweeping it in under the -// heartbeat's message. -func RunHeartbeaterCommitsOnlyItsOwnTables(t *testing.T, ctx context.Context, fixture HeartbeaterFixture) { - t.Helper() - requireHeartbeaterExec(t, fixture) - if fixture.CountHistory == nil || fixture.Lifecycle == nil { - t.Skip("HeartbeaterFixture.CountHistory/Lifecycle is nil: this backend has no version-control history to observe") - } - refreshed := fixture.IssuePrefix + "-dirty-refresh" - heartbeaterSeedClaimed(t, ctx, fixture, refreshed) - refused := fixture.IssuePrefix + "-dirty-refused" - heartbeaterSeed(t, ctx, fixture, refused) - rearmed := fixture.IssuePrefix + "-dirty-rearm" - heartbeaterSeed(t, ctx, fixture, rearmed) - if _, err := fixture.Lifecycle.Update(ctx, publicops.UpdateRequest{ - Actor: heartbeaterHolder, IssueID: rearmed, - Patch: publicops.IssuePatch{ - Status: publicops.Field[publicops.Status]{Set: true, Value: types.StatusInProgress}, - Assignee: publicops.Field[string]{Set: true, Value: heartbeaterHolder}, - }, - }); err != nil { - t.Fatalf("hand-dole %s: %v", rearmed, err) - } - - probe := "heartbeat.dirty-probe." + fixture.IssuePrefix - dirty := fixture.Exec - if fixture.ExecUncommitted != nil { - dirty = fixture.ExecUncommitted - } - if err := dirty(ctx, []SQLStatement{{ - Query: "REPLACE INTO config (`key`, value) VALUES (?, ?)", Args: []any{probe, "uncommitted"}, - }}); err != nil { - t.Fatalf("dirty the working set: %v", err) - } - defer func() { - _ = dirty(ctx, []SQLStatement{{Query: "DELETE FROM config WHERE `key` = ?", Args: []any{probe}}}) - }() - foreignDirty := func(when string) { - t.Helper() - if n := heartbeaterScalar[int](t, ctx, fixture, - "SELECT COUNT(*) FROM dolt_status WHERE table_name = 'config'"); n == 0 { - t.Fatalf("%s: the other writer's uncommitted config change was committed; a heartbeat commits only its own tables", when) - } - } - history := func() int { - t.Helper() - n, err := fixture.CountHistory(ctx) - if err != nil { - t.Fatalf("CountHistory: %v", err) - } - return n - } - foreignDirty("before any heartbeat") - before := history() - - heartbeat(t, ctx, fixture.Heartbeater, publicops.HeartbeatRequest{Actor: heartbeaterHolder, IssueID: refreshed}) - if got := history(); got != before { - t.Fatalf("a refresh moved the version-control history %d -> %d; a refresh makes no commit", before, got) - } - foreignDirty("after a refresh") - - if _, err := fixture.Heartbeater.Heartbeat(ctx, publicops.HeartbeatRequest{Actor: heartbeaterHolder, IssueID: refused}); err == nil { - t.Fatalf("heartbeat on unclaimed %s succeeded, want a refusal", refused) - } - if got := history(); got != before { - t.Fatalf("a refusal moved the version-control history %d -> %d; a refusal makes no commit", before, got) - } - foreignDirty("after a refusal") - - if result := heartbeat(t, ctx, fixture.Heartbeater, publicops.HeartbeatRequest{Actor: heartbeaterHolder, IssueID: rearmed}); !result.Rearmed { - t.Fatalf("heartbeat on hand-doled %s = %+v, want Rearmed", rearmed, result) - } - if got := history(); got != before+1 { - t.Fatalf("a re-arm moved the version-control history %d -> %d, want exactly one commit", before, got) - } - foreignDirty("after a re-arm") -} - -// RunHeartbeaterRefusesAnOrphanedLeaseOnAReleasedIssue and -// ...OnAClosedIssue and ...OnAnIssueSomeoneElseHolds pin the live-claim -// predicate (Heartbeater: "THE LIVE-CLAIM FENCE IS A PREDICATE"): a lease row -// that names the caller but survives on an issue that is open, closed, or -// in_progress under another assignee is refused, never refreshed, and the -// refusal deletes it (Heartbeater: the one write a refusal makes). The row -// is planted raw: it is the state a re-arm racing a release or close leaves. -func RunHeartbeaterRefusesAnOrphanedLeaseOnAReleasedIssue(t *testing.T, ctx context.Context, fixture HeartbeaterFixture) { - t.Helper() - requireHeartbeaterExec(t, fixture) - id := fixture.IssuePrefix + "-orphan-open" - heartbeaterSeed(t, ctx, fixture, id) - heartbeaterRefusesAnOrphan(t, ctx, fixture, id, types.StatusOpen, "") -} - -func RunHeartbeaterRefusesAnOrphanedLeaseOnAClosedIssue(t *testing.T, ctx context.Context, fixture HeartbeaterFixture) { - t.Helper() - requireHeartbeaterExec(t, fixture) - if fixture.Lifecycle == nil { - t.Skip("HeartbeaterFixture.Lifecycle is nil: this backend cannot close the claim") - } - id := fixture.IssuePrefix + "-orphan-closed" - heartbeaterSeedClaimed(t, ctx, fixture, id) - if _, err := fixture.Lifecycle.Close(ctx, publicops.CloseRequest{Actor: heartbeaterHolder, IssueID: id, Reason: "done"}); err != nil { - t.Fatalf("close %s: %v", id, err) - } - heartbeaterRefusesAnOrphan(t, ctx, fixture, id, types.StatusClosed, "") -} - -func RunHeartbeaterRefusesAnOrphanedLeaseOnAnIssueSomeoneElseHolds(t *testing.T, ctx context.Context, fixture HeartbeaterFixture) { - t.Helper() - requireHeartbeaterExec(t, fixture) - if fixture.Lifecycle == nil { - t.Skip("HeartbeaterFixture.Lifecycle is nil: this backend cannot hand-dole a claim") - } - id := fixture.IssuePrefix + "-orphan-moved" - heartbeaterSeed(t, ctx, fixture, id) - if _, err := fixture.Lifecycle.Update(ctx, publicops.UpdateRequest{ - Actor: heartbeaterIntruder, IssueID: id, - Patch: publicops.IssuePatch{ - Status: publicops.Field[publicops.Status]{Set: true, Value: types.StatusInProgress}, - Assignee: publicops.Field[string]{Set: true, Value: heartbeaterIntruder}, - }, - }); err != nil { - t.Fatalf("hand-dole %s to %s: %v", id, heartbeaterIntruder, err) - } - heartbeaterRefusesAnOrphan(t, ctx, fixture, id, types.StatusInProgress, heartbeaterIntruder) -} - -// heartbeaterRefusesAnOrphan plants a lease row for heartbeaterHolder on id -// (which the holder does not hold), heartbeats as the holder, and asserts the -// refusal and that the row is gone. -func heartbeaterRefusesAnOrphan(t *testing.T, ctx context.Context, fixture HeartbeaterFixture, id string, status types.Status, foreign string) { - t.Helper() - now := time.Now().UTC().Truncate(time.Second) - if err := fixture.Exec(ctx, []SQLStatement{ - {Query: "DELETE FROM leases WHERE issue_id = ?", Args: []any{id}}, - { - Query: "INSERT INTO leases (issue_id, holder, granted_at, lease_expires_at, heartbeat_at, granted_node) VALUES (?, ?, ?, ?, ?, '')", - Args: []any{id, heartbeaterHolder, now.Add(-time.Minute), now.Add(time.Hour), now.Add(-time.Minute)}, - }, - }); err != nil { - t.Fatalf("plant an orphaned lease on %s: %v", id, err) - } - - _, err := fixture.Heartbeater.Heartbeat(ctx, publicops.HeartbeatRequest{Actor: heartbeaterHolder, IssueID: id}) - conflict := heartbeaterConflict(t, err) - if foreign != "" { - if !errors.Is(err, publicops.ErrAlreadyClaimed) || conflict.Holder != foreign { - t.Fatalf("heartbeat over an orphaned lease on an issue %s holds = %v (%+v), want ErrAlreadyClaimed naming %s", foreign, err, conflict, foreign) - } - } else if !errors.Is(err, publicops.ErrLeaseNotHeld) || conflict.Holder != "" { - t.Fatalf("heartbeat over an orphaned lease on a %s issue = %v (%+v), want ErrLeaseNotHeld", status, err, conflict) - } - if conflict.IssueID != id || conflict.Status != status || conflict.Ephemeral { - t.Fatalf("LeaseConflictError = %+v, want {IssueID:%s Status:%s Ephemeral:false}", conflict, id, status) - } - if n := heartbeaterScalar[int](t, ctx, fixture, "SELECT COUNT(*) FROM leases WHERE issue_id = ?", id); n != 0 { - t.Fatalf("lease rows on %s after the refusal = %d, want 0: a refusal deletes the caller's orphaned lease", id, n) - } - // And it stays refused: the next heartbeat finds no row to refresh. - if _, err := fixture.Heartbeater.Heartbeat(ctx, publicops.HeartbeatRequest{Actor: heartbeaterHolder, IssueID: id}); err == nil { - t.Fatalf("a second heartbeat on %s succeeded after the orphan was refused", id) - } -} - -// heartbeaterRequireStoredMatches pins the reported times to the stored row -// (LeaseRenewal: "exactly what the lease row now holds"): lease_expires_at and -// heartbeat_at as stored are the result's, within the columns' one-second -// precision, and the stored expiry is the call's clock plus the store's TTL. -func heartbeaterRequireStoredMatches(t *testing.T, ctx context.Context, fixture HeartbeaterFixture, id string, result publicops.HeartbeatResult, before, after time.Time) { - t.Helper() - window := func(at time.Time) (time.Time, time.Time) { return at.Add(-time.Second), at.Add(time.Second) } - hbLo, hbHi := window(result.HeartbeatAt) - exLo, exHi := window(result.LeaseExpiresAt) - if n := heartbeaterScalar[int](t, ctx, fixture, - "SELECT COUNT(*) FROM leases WHERE issue_id = ? AND holder = ? AND heartbeat_at BETWEEN ? AND ? AND lease_expires_at BETWEEN ? AND ?", - id, result.Holder, hbLo, hbHi, exLo, exHi); n != 1 { - t.Fatalf("the stored lease row on %s does not hold the reported heartbeat_at %v and lease_expires_at %v (within 1s)", - id, result.HeartbeatAt, result.LeaseExpiresAt) - } - ttlLo, ttlHi := before.Add(storeops.DefaultLeaseTTL), after.Add(storeops.DefaultLeaseTTL) - if n := heartbeaterScalar[int](t, ctx, fixture, - "SELECT COUNT(*) FROM leases WHERE issue_id = ? AND lease_expires_at BETWEEN ? AND ?", - id, ttlLo, ttlHi); n != 1 { - t.Fatalf("the stored lease_expires_at on %s is not the call's clock plus the lease TTL %v", id, storeops.DefaultLeaseTTL) - } - if !result.HeartbeatAt.Equal(result.HeartbeatAt.Truncate(time.Second)) || !result.LeaseExpiresAt.Equal(result.LeaseExpiresAt.Truncate(time.Second)) { - t.Fatalf("result times %v / %v carry sub-second digits the lease columns cannot hold", result.HeartbeatAt, result.LeaseExpiresAt) - } -} - -// RunHeartbeaterRefusesAfterClose, ...AfterRelease, ...AfterReclaim and -// ...AfterStatusMovedOffInProgress pin the in_progress fence through the -// lease-row invariant (Heartbeater: "a lease row exists only while its issue -// is in_progress with that holder as assignee"): each ends the claim one way, -// asserts the lease row went with it, and that the former holder's heartbeat -// is refused with ErrLeaseNotHeld, which also matches ErrNotClaimable. -func RunHeartbeaterRefusesAfterClose(t *testing.T, ctx context.Context, fixture HeartbeaterFixture) { - t.Helper() - if fixture.Lifecycle == nil { - t.Skip("HeartbeaterFixture.Lifecycle is nil: this backend cannot close the claim") - } - heartbeaterRefusesAfter(t, ctx, fixture, "closed", types.StatusClosed, func(id string) error { - _, err := fixture.Lifecycle.Close(ctx, publicops.CloseRequest{Actor: heartbeaterHolder, IssueID: id, Reason: "done"}) - return err - }) -} - -func RunHeartbeaterRefusesAfterRelease(t *testing.T, ctx context.Context, fixture HeartbeaterFixture) { - t.Helper() - if fixture.Releaser == nil { - t.Skip("HeartbeaterFixture.Releaser is nil: this backend cannot release the claim") - } - heartbeaterRefusesAfter(t, ctx, fixture, "released", types.StatusOpen, func(id string) error { - _, err := fixture.Releaser.Release(ctx, publicops.ReleaseRequest{Actor: heartbeaterHolder, IssueID: id}) - return err - }) -} - -func RunHeartbeaterRefusesAfterReclaim(t *testing.T, ctx context.Context, fixture HeartbeaterFixture) { - t.Helper() - if fixture.LeaseReclaimer == nil || fixture.Exec == nil { - t.Skip("HeartbeaterFixture.LeaseReclaimer/Exec is nil: this backend cannot reclaim the claim") - } - heartbeaterRefusesAfter(t, ctx, fixture, "reclaimed", types.StatusOpen, func(id string) error { - if err := fixture.Exec(ctx, []SQLStatement{{ - Query: "UPDATE leases SET lease_expires_at = ? WHERE issue_id = ?", - Args: []any{time.Now().UTC().Add(-time.Hour), id}, - }}); err != nil { - return err - } - result, err := fixture.LeaseReclaimer.Reclaim(ctx, publicops.ReclaimRequest{ - Actor: heartbeaterReaper, Filter: publicops.ReclaimFilter{IDs: []string{id}}, - }) - if err == nil && len(result.Reclaimed) != 1 { - t.Fatalf("Reclaimed = %+v, want exactly %s", result.Reclaimed, id) - } - return err - }) -} - -func RunHeartbeaterRefusesAfterStatusMovedOffInProgress(t *testing.T, ctx context.Context, fixture HeartbeaterFixture) { - t.Helper() - if fixture.Lifecycle == nil { - t.Skip("HeartbeaterFixture.Lifecycle is nil: this backend cannot move the status") - } - heartbeaterRefusesAfter(t, ctx, fixture, "blocked", types.StatusBlocked, func(id string) error { - _, err := fixture.Lifecycle.Update(ctx, publicops.UpdateRequest{ - Actor: heartbeaterHolder, IssueID: id, - Patch: publicops.IssuePatch{Status: publicops.Field[publicops.Status]{Set: true, Value: types.StatusBlocked}}, - }) - return err - }) -} - -// RunHeartbeaterRefusesAWisp pins the plane rule (Heartbeater REFUSALS: "a -// wisp ... ErrLeaseNotHeld"): wisps are never leased, so even a wisp the -// caller holds in_progress is refused, and no lease row appears for it. -func RunHeartbeaterRefusesAWisp(t *testing.T, ctx context.Context, fixture HeartbeaterFixture) { - t.Helper() - if fixture.CreateWisp == nil || fixture.QueryScalar == nil { - t.Skip("HeartbeaterFixture.CreateWisp/QueryScalar is nil: this backend cannot seed a wisp") - } - id := fixture.IssuePrefix + "-wisp" - if err := fixture.CreateWisp(ctx, &types.Issue{ - ID: id, Title: id, Status: types.StatusInProgress, Assignee: heartbeaterHolder, - Priority: 2, IssueType: types.TypeTask, Ephemeral: true, - }, "seed"); err != nil { - t.Fatalf("seed wisp %s: %v", id, err) - } - - _, err := fixture.Heartbeater.Heartbeat(ctx, publicops.HeartbeatRequest{Actor: heartbeaterHolder, IssueID: id}) - if !errors.Is(err, publicops.ErrLeaseNotHeld) || !errors.Is(err, publicops.ErrNotClaimable) { - t.Fatalf("heartbeat on a wisp = %v, want ErrLeaseNotHeld matching ErrNotClaimable", err) - } - if !strings.Contains(err.Error(), "ephemeral") { - t.Fatalf("heartbeat on a wisp = %q, want the refusal to say the issue is ephemeral", err) - } - if conflict := heartbeaterConflict(t, err); conflict.IssueID != id || conflict.Holder != "" { - t.Fatalf("LeaseConflictError = %+v, want {IssueID:%s Holder:\"\"}", conflict, id) - } - if n := heartbeaterScalar[int](t, ctx, fixture, "SELECT COUNT(*) FROM leases WHERE issue_id = ?", id); n != 0 { - t.Fatalf("lease rows for wisp %s = %d, want 0: wisps are never leased", id, n) - } -} - -// RunHeartbeaterRearmsAHandDoledClaim pins the self-heal (HeartbeatResult. -// Rearmed): an issue put in_progress with the caller as assignee by a generic -// update holds no lease row, and the holder's heartbeat grants one rather than -// refusing. The re-arm is recorded as a mutation (Heartbeater: "A RE-ARM IS -// RECORDED AS A MUTATION"): it remints the revision, which is what makes it -// collide with a racing release or close, and records a lease_rearmed event; -// the refresh after it does neither. -func RunHeartbeaterRearmsAHandDoledClaim(t *testing.T, ctx context.Context, fixture HeartbeaterFixture) { - t.Helper() - if fixture.Lifecycle == nil || fixture.QueryScalar == nil { - t.Skip("HeartbeaterFixture.Lifecycle/QueryScalar is nil: this backend cannot hand-dole a claim") - } - id := fixture.IssuePrefix + "-doled" - heartbeaterSeed(t, ctx, fixture, id) - if _, err := fixture.Lifecycle.Update(ctx, publicops.UpdateRequest{ - Actor: heartbeaterHolder, IssueID: id, - Patch: publicops.IssuePatch{ - Status: publicops.Field[publicops.Status]{Set: true, Value: types.StatusInProgress}, - Assignee: publicops.Field[string]{Set: true, Value: heartbeaterHolder}, - }, - }); err != nil { - t.Fatalf("hand-dole %s: %v", id, err) - } - if n := heartbeaterScalar[int](t, ctx, fixture, "SELECT COUNT(*) FROM leases WHERE issue_id = ?", id); n != 0 { - t.Fatalf("lease rows after a generic update = %d, want 0: the case needs a claim with no lease", n) - } - revision := heartbeaterScalar[int64](t, ctx, fixture, "SELECT row_lock FROM issues WHERE id = ?", id) - - before := time.Now().UTC().Add(-time.Second) - result := heartbeat(t, ctx, fixture.Heartbeater, publicops.HeartbeatRequest{Actor: heartbeaterHolder, IssueID: id}) - after := time.Now().UTC().Add(time.Second) - if !result.Rearmed || result.Holder != heartbeaterHolder { - t.Fatalf("result = %+v, want Rearmed:true Holder:%s", result, heartbeaterHolder) - } - heartbeaterRequireStoredMatches(t, ctx, fixture, id, result, before, after) - if n := heartbeaterScalar[int](t, ctx, fixture, - "SELECT COUNT(*) FROM leases WHERE issue_id = ? AND holder = ? AND lease_expires_at > ?", - id, heartbeaterHolder, time.Now().UTC()); n != 1 { - t.Fatal("the re-armed heartbeat left no live lease row for the holder") - } - rearmed := heartbeaterScalar[int64](t, ctx, fixture, "SELECT row_lock FROM issues WHERE id = ?", id) - if rearmed == revision { - t.Fatalf("row_lock stayed %d across a re-arming heartbeat; a re-arm remints it so a racing release or close collides with it", revision) - } - rearmEvents := func() int { - return heartbeaterScalar[int](t, ctx, fixture, - "SELECT COUNT(*) FROM events WHERE issue_id = ? AND event_type = ?", id, string(types.EventLeaseRearmed)) - } - if n := rearmEvents(); n != 1 { - t.Fatalf("lease_rearmed events on %s after a re-arm = %d, want 1: a re-arm is recorded as a mutation", id, n) - } - // The next heartbeat refreshes the lease it just granted: no re-arm, and - // no revision change. - if again := heartbeat(t, ctx, fixture.Heartbeater, publicops.HeartbeatRequest{Actor: heartbeaterHolder, IssueID: id}); again.Rearmed { - t.Fatalf("second heartbeat after the re-arm = %+v, want Rearmed:false", again) - } - if got := heartbeaterScalar[int64](t, ctx, fixture, "SELECT row_lock FROM issues WHERE id = ?", id); got != rearmed { - t.Fatalf("row_lock moved from %d to %d across a refresh after the re-arm", rearmed, got) - } - if n := rearmEvents(); n != 1 { - t.Fatalf("lease_rearmed events on %s after a refresh = %d, want still 1: a refresh records nothing", id, n) - } -} - -// RunHeartbeaterAnswersNotFoundForAnAbsentID pins Heartbeater REFUSALS: "an -// IssueID that names no row on either plane: ErrNotFound". -func RunHeartbeaterAnswersNotFoundForAnAbsentID(t *testing.T, ctx context.Context, fixture HeartbeaterFixture) { - t.Helper() - _, err := fixture.Heartbeater.Heartbeat(ctx, publicops.HeartbeatRequest{ - Actor: heartbeaterHolder, IssueID: fixture.IssuePrefix + "-absent", - }) - if !errors.Is(err, publicops.ErrNotFound) { - t.Fatalf("heartbeat on an absent id = %v, want ErrNotFound", err) - } -} - -// RunHeartbeaterRefusesABlankOrOverLongActor pins the request rules -// (HeartbeatRequest.Actor, Heartbeater REFUSALS): each is a -// *HeartbeatFieldError naming its field, matching ErrValidation. -func RunHeartbeaterRefusesABlankOrOverLongActor(t *testing.T, ctx context.Context, fixture HeartbeaterFixture) { - t.Helper() - id := fixture.IssuePrefix + "-refusals" - for _, test := range []struct { - name string - request publicops.HeartbeatRequest - field string - }{ - {"an empty actor", publicops.HeartbeatRequest{IssueID: id}, publicops.HeartbeatFieldActor}, - {"a blank actor", publicops.HeartbeatRequest{Actor: " \t", IssueID: id}, publicops.HeartbeatFieldActor}, - {"an over-long actor", publicops.HeartbeatRequest{Actor: strings.Repeat("a", types.MaxFieldLen+1), IssueID: id}, publicops.HeartbeatFieldActor}, - {"an empty id", publicops.HeartbeatRequest{Actor: heartbeaterHolder}, publicops.HeartbeatFieldIssueID}, - } { - t.Run(test.name, func(t *testing.T) { - _, err := fixture.Heartbeater.Heartbeat(ctx, test.request) - if !errors.Is(err, publicops.ErrValidation) { - t.Fatalf("Heartbeat(%s) = %v, want ErrValidation", test.name, err) - } - var field *publicops.HeartbeatFieldError - if !errors.As(err, &field) || field.Field != test.field { - t.Fatalf("Heartbeat(%s) = %v, want a *HeartbeatFieldError naming %q", test.name, err, test.field) - } - }) - } -} - -// RunHeartbeaterDoesNotMutateTheRequest pins the role's "never mutate -// caller-owned request values". -func RunHeartbeaterDoesNotMutateTheRequest(t *testing.T, ctx context.Context, fixture HeartbeaterFixture) { - t.Helper() - id := fixture.IssuePrefix + "-nomutate" - heartbeaterSeedClaimed(t, ctx, fixture, id) - request := publicops.HeartbeatRequest{Actor: heartbeaterHolder, IssueID: id} - want := request - heartbeat(t, ctx, fixture.Heartbeater, request) - if request != want { - t.Fatalf("request after Heartbeat = %+v, want %+v", request, want) - } -} - -// heartbeaterRefusesAfter seeds and claims one issue, ends the claim with end, -// and asserts the invariant and the refusal. -func heartbeaterRefusesAfter(t *testing.T, ctx context.Context, fixture HeartbeaterFixture, suffix string, status types.Status, end func(id string) error) { - t.Helper() - if fixture.QueryScalar == nil { - t.Skip("HeartbeaterFixture.QueryScalar is nil: this backend cannot read the lease row") - } - id := fixture.IssuePrefix + "-after-" + suffix - heartbeaterSeedClaimed(t, ctx, fixture, id) - if n := heartbeaterScalar[int](t, ctx, fixture, "SELECT COUNT(*) FROM leases WHERE issue_id = ?", id); n != 1 { - t.Fatalf("lease rows for claimed %s = %d, want 1", id, n) - } - if err := end(id); err != nil { - t.Fatalf("end the claim on %s (%s): %v", id, suffix, err) - } - if n := heartbeaterScalar[int](t, ctx, fixture, "SELECT COUNT(*) FROM leases WHERE issue_id = ?", id); n != 0 { - t.Fatalf("lease rows for %s %s = %d, want 0: the path that ended the claim left its lease row", suffix, id, n) - } - - _, err := fixture.Heartbeater.Heartbeat(ctx, publicops.HeartbeatRequest{Actor: heartbeaterHolder, IssueID: id}) - if !errors.Is(err, publicops.ErrLeaseNotHeld) || !errors.Is(err, publicops.ErrNotClaimable) { - t.Fatalf("heartbeat after the claim was %s = %v, want ErrLeaseNotHeld matching ErrNotClaimable", suffix, err) - } - if errors.Is(err, publicops.ErrAlreadyClaimed) { - t.Fatalf("heartbeat after the claim was %s = %v also matches ErrAlreadyClaimed", suffix, err) - } - conflict := heartbeaterConflict(t, err) - if conflict.IssueID != id || conflict.Holder != "" || conflict.Status != status { - t.Fatalf("LeaseConflictError = %+v, want {IssueID:%s Holder:\"\" Status:%s}", conflict, id, status) - } - if n := heartbeaterScalar[int](t, ctx, fixture, "SELECT COUNT(*) FROM leases WHERE issue_id = ?", id); n != 0 { - t.Fatalf("a refused heartbeat left %d lease row(s) for %s", n, id) - } -} - -// heartbeaterRowSnapshot is every durable trace a heartbeat must not leave. -// A field the backend cannot observe holds -1. -type heartbeaterRowSnapshot struct { - RowLock int64 - CurrentRevision int64 - UpdatedAt string - Events int - JournalMaxSeq int64 - IssueVersions int - History int -} - -func heartbeaterSnapshot(t *testing.T, ctx context.Context, fixture HeartbeaterFixture, id string) heartbeaterRowSnapshot { - t.Helper() - snap := heartbeaterRowSnapshot{ - RowLock: heartbeaterScalar[int64](t, ctx, fixture, "SELECT row_lock FROM issues WHERE id = ?", id), - CurrentRevision: heartbeaterScalar[int64](t, ctx, fixture, "SELECT current_revision FROM issues WHERE id = ?", id), - UpdatedAt: heartbeaterScalar[string](t, ctx, fixture, "SELECT CAST(updated_at AS CHAR) FROM issues WHERE id = ?", id), - Events: heartbeaterScalar[int](t, ctx, fixture, "SELECT COUNT(*) FROM events WHERE issue_id = ?", id), - JournalMaxSeq: -1, - IssueVersions: -1, - History: -1, - } - if fixture.SetJournalEnabled != nil { - snap.JournalMaxSeq = heartbeaterScalar[int64](t, ctx, fixture, "SELECT COALESCE(MAX(seq), 0) FROM bd_events_journal") - } - if fixture.SetVersionedHistoryEnabled != nil { - snap.IssueVersions = heartbeaterScalar[int](t, ctx, fixture, "SELECT COUNT(*) FROM issue_versions WHERE issue_id = ?", id) - } - if fixture.CountHistory != nil { - n, err := fixture.CountHistory(ctx) - if err != nil { - t.Fatalf("CountHistory: %v", err) - } - snap.History = n - } - return snap -} - -func heartbeaterFired(t *testing.T, ctx context.Context, fixture HeartbeaterFixture, id string) int { - t.Helper() - if fixture.HookedHeartbeater == nil || fixture.FiredUpdates == nil { - return 0 - } - ids, err := fixture.FiredUpdates(ctx) - if err != nil { - t.Fatalf("FiredUpdates: %v", err) - } - n := 0 - for _, fired := range ids { - if fired == id { - n++ - } - } - return n -} - -func requireHeartbeaterExec(t *testing.T, fixture HeartbeaterFixture) { - t.Helper() - if fixture.Exec == nil || fixture.QueryScalar == nil { - t.Skip("HeartbeaterFixture.Exec/QueryScalar is nil: this backend cannot read or age a lease row") - } -} - -func heartbeaterSeed(t *testing.T, ctx context.Context, fixture HeartbeaterFixture, id string) { - t.Helper() - if err := fixture.CreateIssue(ctx, &types.Issue{ - ID: id, Title: id, Status: types.StatusOpen, Priority: 2, IssueType: types.TypeTask, - }, "seed"); err != nil { - t.Fatalf("seed %s: %v", id, err) - } -} - -// heartbeaterSeedClaimed creates id and claims it as heartbeaterHolder through -// the leg's Claimer, answering the claim's row version. -func heartbeaterSeedClaimed(t *testing.T, ctx context.Context, fixture HeartbeaterFixture, id string) int64 { - t.Helper() - if fixture.Claimer == nil { - t.Skip("HeartbeaterFixture.Claimer is nil: this backend cannot take the claim a heartbeat refreshes") - } - heartbeaterSeed(t, ctx, fixture, id) - claimed, err := fixture.Claimer.Claim(ctx, publicops.ClaimRequest{Actor: heartbeaterHolder, IssueID: id}) - if err != nil { - t.Fatalf("claim %s as %s: %v", id, heartbeaterHolder, err) - } - if claimed.Issue == nil { - t.Fatalf("claim %s answered no issue", id) - } - return claimed.Issue.RowVersion -} - -func heartbeat(t *testing.T, ctx context.Context, surface publicops.Heartbeater, request publicops.HeartbeatRequest) publicops.HeartbeatResult { - t.Helper() - result, err := surface.Heartbeat(ctx, request) - if err != nil { - t.Fatalf("Heartbeat(%+v): %v", request, err) - } - return result -} - -func heartbeaterConflict(t *testing.T, err error) *publicops.LeaseConflictError { - t.Helper() - var conflict *publicops.LeaseConflictError - if !errors.As(err, &conflict) { - t.Fatalf("refusal %v (%T) is not a *LeaseConflictError", err, err) - } - return conflict -} - -func heartbeaterScalar[T any](t *testing.T, ctx context.Context, fixture HeartbeaterFixture, query string, args ...any) T { - t.Helper() - var value T - if err := fixture.QueryScalar(ctx, query, args, &value); err != nil { - t.Fatalf("%s %v: %v", query, args, err) - } - return value -} - -func heartbeaterAbs(d time.Duration) time.Duration { - if d < 0 { - return -d - } - return d -} diff --git a/backend/conformance/importer_contract.go b/backend/conformance/importer_contract.go index 533336ea6..9308055ce 100644 --- a/backend/conformance/importer_contract.go +++ b/backend/conformance/importer_contract.go @@ -235,63 +235,45 @@ func RunImporterReportsTheAbsentTargetItDroppedOnce(t *testing.T, ctx context.Co assertImporterSkipped(t, result, []publicops.SkippedDependency{{IssueID: source, DependsOnID: absent}}) } -// RunImporterWiresTheCrossPlaneEdgeBetweenItsRows pins the plane rule's OTHER -// branch. The request is the one RunBatchCreatorRefusesACrossPlaneInBatchEdge +// RunImporterReportsTheCrossPlaneEdgeItDropped pins the same obligation for the +// plane rule. The request is the one RunBatchCreatorRefusesACrossPlaneInBatchEdge // sends and refuses whole — a durable item and an ephemeral item created -// together with an edge between them. BatchCreator refuses because its -// unit-of-work body creates item by item and cannot promise the edge; the -// importer writes every row of BOTH planes in one transaction before it -// persists any edge, so the edge is writable. Until wy-a648lq the engine's -// per-batch plane filter dropped it anyway — skip-reported, but a re-import -// upserts both rows unchanged and never backfills it, so an exported snapshot -// lost every regular<->wisp edge for good (wy-4276q8). A report justifies a -// drop only when the drop is forced; this one was not. +// together with an edge between them — where the import keeps BOTH ROWS and +// drops only the edge. // -// BOTH DIRECTIONS ARE ASSERTED, because they land in different places: a wisp -// depending on a durable row writes wisp_dependencies.depends_on_issue_id, a -// durable row depending on a wisp writes dependencies.depends_on_wisp_id, and -// an implementation could wire one and drop the other. Every row is asserted -// on its own table, and the skip report must be EMPTY — an entry naming an -// edge the batch actually wrote is the report lying in the other direction. -func RunImporterWiresTheCrossPlaneEdgeBetweenItsRows(t *testing.T, ctx context.Context, fixture ImporterFixture) { +// BOTH ROWS ARE ASSERTED, on their own tables. The dropped edge is the visible +// part, and an implementation that dropped the ephemeral item along with it +// would satisfy every statement about the edge: there is no edge because there +// is no wisp. The audit twin's arm (b) reads both rows back through GetIssue, +// which resolves across planes and so cannot say WHICH table either landed in. +// +// The report is again an exact list. Arm (b) of the audit case asserts no +// report at all — it passes a nil skip callback — so a silent cross-plane drop +// is invisible to it, which is precisely the drop BatchCreator calls data +// loss. +func RunImporterReportsTheCrossPlaneEdgeItDropped(t *testing.T, ctx context.Context, fixture ImporterFixture) { t.Helper() durable := fixture.IssuePrefix + "-impplane-durable" wisp := fixture.IssuePrefix + "-impplane-wisp" - depender := fixture.IssuePrefix + "-impplane-depender" ephemeral := importerIssue(wisp, "the ephemeral end") ephemeral.Ephemeral = true ephemeral.Dependencies = []*types.Dependency{ {IssueID: wisp, DependsOnID: durable, Type: types.DepBlocks}, } - durableDepender := importerIssue(depender, "the durable row blocked by the wisp") - durableDepender.Dependencies = []*types.Dependency{ - {IssueID: depender, DependsOnID: wisp, Type: types.DepBlocks}, - } result := runImporterBatch(t, ctx, fixture, "cross-plane", - importerIssue(durable, "the durable end"), ephemeral, durableDepender) + importerIssue(durable, "the durable end"), ephemeral) assertImporterRowCount(t, ctx, fixture, "issues", durable, 1) assertImporterRowCount(t, ctx, fixture, "wisps", wisp, 1) - assertImporterRowCount(t, ctx, fixture, "issues", depender, 1) - if result.Created != 3 { - t.Errorf("Created = %d, want 3", result.Created) + if result.Created != 2 { + t.Errorf("Created = %d, want 2: dropping an edge costs the batch neither row", result.Created) } - // BOTH BELTS PER DIRECTION: the pinned cell fails a misrouted edge (the - // expected cell reads 0), and the summed total fails a DUPLICATE landing in - // any of the six table x column cells, which no per-cell count can see. The - // two together also make "the other plane holds none" exact: total 1 with - // the expected cell at 1 leaves every remaining cell empty. - assertImporterPlaneEdgeCount(t, ctx, fixture, "wisp_dependencies", "depends_on_issue_id", wisp, durable, 1) - assertImporterPlaneEdgeCount(t, ctx, fixture, "dependencies", "depends_on_issue_id", wisp, durable, 0) - assertImporterEdgeCount(t, ctx, fixture, wisp, durable, 1) - assertImporterPlaneEdgeCount(t, ctx, fixture, "dependencies", "depends_on_wisp_id", depender, wisp, 1) - assertImporterPlaneEdgeCount(t, ctx, fixture, "wisp_dependencies", "depends_on_wisp_id", depender, wisp, 0) - assertImporterEdgeCount(t, ctx, fixture, depender, wisp, 1) - assertImporterSkipped(t, result, nil) + assertImporterEdgeCount(t, ctx, fixture, wisp, durable, 0) + assertImporterSkipped(t, result, []publicops.SkippedDependency{{IssueID: wisp, DependsOnID: durable}}) } -// RunImporterReportsTheCycleEdgeItDropped pins the last of the drops: an +// RunImporterReportsTheCycleEdgeItDropped pins the last of the three drops: an // in-batch dependency cycle. Both rows land, the graph stays acyclic, and the // ONE edge given up to keep it acyclic is named. // @@ -424,9 +406,7 @@ func assertImporterRowCount(t *testing.T, ctx context.Context, fixture ImporterF // importerEdgeCount counts the stored edges from source to target across BOTH // dependency tables and all three target columns. A dropped edge is dropped on // every plane, so the cases that expect zero want zero everywhere, and a -// per-table count could report one while the row sat in the other. WHICH of the -// six cells a row landed in is a placement detail the cases that care about it -// assert through assertImporterPlaneEdgeCount instead. +// per-table count could report one while the row sat in the other. func importerEdgeCount(t *testing.T, ctx context.Context, fixture ImporterFixture, source, target string) int { t.Helper() var got int @@ -449,29 +429,6 @@ func assertImporterEdgeCount(t *testing.T, ctx context.Context, fixture Importer } } -// assertImporterPlaneEdgeCount pins WHERE an edge landed: one table and one -// target column, the way RunImporterWiresTheCrossPlaneEdgeBetweenItsRows -// documents its claim. importerEdgeCount deliberately sums both planes, which -// is right for "dropped everywhere" and wrong for "wired in the right place": -// an edge written to the other plane's table, or under the wrong target -// column, counts as one there too. ONE CALL PINS ONE CELL, so the cross-plane -// case asserts the expected cell holds exactly one row and the mirror cell of -// the other plane holds none, and keeps importerEdgeCount's summed total -// alongside them — a duplicate under one of the four cells neither call names -// is invisible to this helper and is exactly what the sum still catches. -func assertImporterPlaneEdgeCount(t *testing.T, ctx context.Context, fixture ImporterFixture, table, column, source, target string, want int) { - t.Helper() - //nolint:gosec // G201: table and column are hardcoded names from this contract's call sites. - query := "SELECT COUNT(*) FROM " + table + " WHERE issue_id = ? AND " + column + " = ?" - var got int - if err := fixture.QueryScalar(ctx, query, []any{source, target}, &got); err != nil { - t.Fatalf("count %s.%s edges %s -> %s: %v", table, column, source, target, err) - } - if got != want { - t.Errorf("%s.%s rows %s -> %s = %d, want %d", table, column, source, target, got, want) - } -} - // assertImporterScalar asserts one stored column of one durable row. func assertImporterScalar(t *testing.T, ctx context.Context, fixture ImporterFixture, what, id, want string) { t.Helper() diff --git a/backend/conformance/issue_operations_contract.go b/backend/conformance/issue_operations_contract.go index 7f6372471..8ccb84c3a 100644 --- a/backend/conformance/issue_operations_contract.go +++ b/backend/conformance/issue_operations_contract.go @@ -1479,7 +1479,7 @@ func RunIssueOperationsUpdateWritesEveryScalarPatchField(t *testing.T, ctx conte {"defer_until", patchedDefer.Format(issueOperationsStoredTimeLayout)}, } - written, err := fixture.Operations.Update(ctx, publicops.UpdateRequest{Actor: "writer", IssueID: id, Patch: patch, ForceNotesOverwrite: true}) + written, err := fixture.Operations.Update(ctx, publicops.UpdateRequest{Actor: "writer", IssueID: id, Patch: patch}) if err != nil { t.Fatalf("full scalar patch on %s: %v", id, err) } diff --git a/backend/conformance/lease_reclaimer_contract.go b/backend/conformance/lease_reclaimer_contract.go deleted file mode 100644 index eadc6c176..000000000 --- a/backend/conformance/lease_reclaimer_contract.go +++ /dev/null @@ -1,625 +0,0 @@ -package conformance - -import ( - "context" - "errors" - "fmt" - "os" - "path/filepath" - "runtime" - "slices" - "sort" - "strings" - "testing" - "time" - - "github.com/steveyegge/beads/internal/hooks" - "github.com/steveyegge/beads/internal/types" - publicops "github.com/steveyegge/beads/issueops" -) - -// This file holds the contract every implementation of publicops.LeaseReclaimer -// must satisfy. Each case asserts what issueops/leasereclaimer.go PROMISES, -// cited by symbol. -// -// FOUR LEGS, ONE BODY. dolt, embedded dolt and the unit-of-work provider all -// reach internal/storage/issueops.ExecuteReclaimInTx, and the http leg reaches -// the same body on the far side of POST /v0/beads/issues:reclaim. So a per-leg -// failure here is a wrapper's failure — a lost scope, a revision that never -// reached the caller, a refusal that stopped matching errors.Is — and the -// cases are written to catch exactly those. -// -// EVERY SWEEP HERE IS SCOPED. The legs share one database per suite with every -// other role's cases, and an UNSCOPED reclaim would revert whatever stale lease -// another case left behind. Every call below names its ids, or an assignee or -// label this case alone uses, so a case can never reach outside what it seeded. -// The workspace-wide form is the same SQL with the scope clause absent, which -// sqlbuild.ReclaimScopeSQL's own tests pin. -// -// STALENESS IS MANUFACTURED, NOT WAITED FOR. The role refuses a negative -// OlderThan (the raw method's -time.Hour trick is exactly what it rules out), -// so a case claims through the leg's real Claimer — which writes the lease row -// every reclaim reads — and then moves that row's lease_expires_at into the -// past through Exec. -type LeaseReclaimerFixture struct { - // IssuePrefix namespaces the ids each assertion seeds, so several of them - // can share one database. - IssuePrefix string - // LeaseReclaimer is the surface under test. - LeaseReclaimer publicops.LeaseReclaimer - // CreateIssue seeds a durable issue in the issues plane. - CreateIssue func(context.Context, *types.Issue, string) error - // Claimer takes the claim, and with it the lease row, a reclaim reverts. It - // is the leg's own role rather than a raw insert so the lease is the shape - // a real claim leaves, granted_node included. - Claimer publicops.Claimer - // Lifecycle performs the guarded write the revision case races against the - // reclaim. A nil Lifecycle SKIPS that case loudly. - Lifecycle publicops.Lifecycle - // Exec runs raw statements against the backing database: the cases use it - // to age a lease and to attach labels. A nil Exec SKIPS every case that - // needs a stale lease, which is nearly all of them, so every leg supplies - // one. - Exec func(ctx context.Context, statements []SQLStatement) error - // QueryScalar runs a single-row query and scans it, so the post-state is - // read RAW rather than through the role being tested. - QueryScalar func(context.Context, string, []any, ...any) error - // HookedLeaseReclaimer is the same role taken off the leg's HOOK-FIRING - // chain (storage.HookFiringStore on the store legs and the http leg, - // uow.NewNotifyingProvider on the unit-of-work leg), built over the runner - // FiredUpdates reads. Nil, with FiredUpdates, SKIPS the hook case loudly. - HookedLeaseReclaimer publicops.LeaseReclaimer - // FiredUpdates reports, in firing order, the issue id of every on_update - // the hooked chain ran so far. It must not return before the hooks it is - // asked about have finished. - FiredUpdates func(context.Context) ([]string, error) -} - -const ( - leaseReclaimerReaper = "reaper" - leaseReclaimerHolder = "lr-holder" -) - -// RunLeaseReclaimerRevertsAStaleLease pins the post-state the role names -// (issueops/leasereclaimer.go, LeaseReclaimer: "each reclaimed row moves -// in_progress -> open and the assignee is cleared") and the three members of -// the ReclaimedLease it reports, each READ RAW. -func RunLeaseReclaimerRevertsAStaleLease(t *testing.T, ctx context.Context, fixture LeaseReclaimerFixture) { - t.Helper() - requireLeaseReclaimerExec(t, fixture) - id := fixture.IssuePrefix + "-revert" - leaseReclaimerSeedStale(t, ctx, fixture, leaseReclaimerHolder, id) - - result := leaseReclaim(t, ctx, fixture, publicops.ReclaimRequest{ - Actor: leaseReclaimerReaper, - Filter: publicops.ReclaimFilter{IDs: []string{id}}, - }) - if len(result.Reclaimed) != 1 { - t.Fatalf("Reclaimed = %+v, want exactly %s", result.Reclaimed, id) - } - got := result.Reclaimed[0] - if got.ID != id || got.PreviousOwner != leaseReclaimerHolder { - t.Fatalf("Reclaimed[0] = %+v, want {ID:%s PreviousOwner:%s}", got, id, leaseReclaimerHolder) - } - - if status := leaseReclaimerScalar[string](t, ctx, fixture, "SELECT status FROM issues WHERE id = ?", id); status != string(types.StatusOpen) { - t.Fatalf("status after reclaim = %q, want %q", status, types.StatusOpen) - } - if held := leaseReclaimerScalar[int](t, ctx, fixture, - "SELECT COUNT(*) FROM issues WHERE id = ? AND (COALESCE(assignee, '') <> '' OR started_at IS NOT NULL)", id); held != 0 { - t.Fatal("reclaim left the assignee or started_at set; the row is still held by a dead worker") - } - if leases := leaseReclaimerScalar[int](t, ctx, fixture, "SELECT COUNT(*) FROM leases WHERE issue_id = ?", id); leases != 0 { - t.Fatalf("lease rows after reclaim = %d, want 0", leases) - } - stored := leaseReclaimerScalar[int64](t, ctx, fixture, "SELECT row_lock FROM issues WHERE id = ?", id) - if got.Revision != types.RevisionToken(stored) { - t.Fatalf("Reclaimed[0].Revision = %q, want the row's own token %q", got.Revision, types.RevisionToken(stored)) - } -} - -// RunLeaseReclaimerMintsARevisionThatFencesTheFormerHolder is the reason the -// revision is minted at all (LeaseReclaimer: "a racing holder's guarded write -// fails with ErrVersionMismatch"). A worker that lost its lease and wakes up -// holding the token its claim handed it must NOT be able to land a guarded -// write over the issue another worker may already have taken; the token the -// reclaim reported, and only it, lands. -func RunLeaseReclaimerMintsARevisionThatFencesTheFormerHolder(t *testing.T, ctx context.Context, fixture LeaseReclaimerFixture) { - t.Helper() - requireLeaseReclaimerExec(t, fixture) - if fixture.Lifecycle == nil { - t.Skip("LeaseReclaimerFixture.Lifecycle is nil: this backend cannot race a guarded write against the reclaim") - } - id := fixture.IssuePrefix + "-fence" - claimed := leaseReclaimerSeedStale(t, ctx, fixture, leaseReclaimerHolder, id)[id] - - result := leaseReclaim(t, ctx, fixture, publicops.ReclaimRequest{ - Actor: leaseReclaimerReaper, - Filter: publicops.ReclaimFilter{IDs: []string{id}}, - }) - if len(result.Reclaimed) != 1 { - t.Fatalf("Reclaimed = %+v, want exactly %s", result.Reclaimed, id) - } - minted, err := types.ParseRevisionToken(result.Reclaimed[0].Revision) - if err != nil { - t.Fatalf("Reclaimed[0].Revision %q is not a revision token: %v", result.Reclaimed[0].Revision, err) - } - if minted == claimed { - t.Fatalf("reclaim reported revision %d, the claim's own token: nothing was minted", minted) - } - - stale := claimed - _, err = fixture.Lifecycle.Update(ctx, publicops.UpdateRequest{ - Actor: leaseReclaimerHolder, IssueID: id, ExpectedVersion: &stale, - Patch: publicops.IssuePatch{Notes: publicops.Field[string]{Set: true, Value: "late write"}}, - }) - if !errors.Is(err, publicops.ErrVersionMismatch) { - t.Fatalf("guarded write with the pre-reclaim token = %v, want ErrVersionMismatch", err) - } - - fresh := minted - if _, err := fixture.Lifecycle.Update(ctx, publicops.UpdateRequest{ - Actor: leaseReclaimerReaper, IssueID: id, ExpectedVersion: &fresh, - Patch: publicops.IssuePatch{Notes: publicops.Field[string]{Set: true, Value: "after reclaim"}}, - }); err != nil { - t.Fatalf("guarded write with the token the reclaim reported = %v, want it to land", err) - } -} - -// RunLeaseReclaimerLeavesALiveLeaseAlone pins staleness as the gate: a lease -// that has not expired is not eligible, even when its id is named, and naming -// it is not an error (ReclaimRequest.Filter: "FILTER.IDS IS NOT AN ERROR WHEN -// IT NAMES A NON-STALE OR UNKNOWN ISSUE"). -func RunLeaseReclaimerLeavesALiveLeaseAlone(t *testing.T, ctx context.Context, fixture LeaseReclaimerFixture) { - t.Helper() - requireLeaseReclaimerExec(t, fixture) - id := fixture.IssuePrefix + "-live" - leaseReclaimerSeedClaimed(t, ctx, fixture, leaseReclaimerHolder, id) - - result := leaseReclaim(t, ctx, fixture, publicops.ReclaimRequest{ - Actor: leaseReclaimerReaper, - Filter: publicops.ReclaimFilter{IDs: []string{id}}, - }) - if len(result.Reclaimed) != 0 { - t.Fatalf("Reclaimed = %+v, want nothing: the lease has not expired", result.Reclaimed) - } - leaseReclaimerRequireHeld(t, ctx, fixture, id) -} - -// RunLeaseReclaimerHonorsTheGraceWindow pins ReclaimRequest.OlderThan: a lease -// expired less than OlderThan ago is left alone, and the same lease is -// reclaimed once the window is narrower than its age. -func RunLeaseReclaimerHonorsTheGraceWindow(t *testing.T, ctx context.Context, fixture LeaseReclaimerFixture) { - t.Helper() - requireLeaseReclaimerExec(t, fixture) - id := fixture.IssuePrefix + "-grace" - leaseReclaimerSeedClaimed(t, ctx, fixture, leaseReclaimerHolder, id) - leaseReclaimerAge(t, ctx, fixture, 10*time.Minute, id) - - request := publicops.ReclaimRequest{ - Actor: leaseReclaimerReaper, - OlderThan: time.Hour, - Filter: publicops.ReclaimFilter{IDs: []string{id}}, - } - if result := leaseReclaim(t, ctx, fixture, request); len(result.Reclaimed) != 0 { - t.Fatalf("OlderThan 1h over a lease 10m stale: Reclaimed = %+v, want nothing", result.Reclaimed) - } - leaseReclaimerRequireHeld(t, ctx, fixture, id) - - request.OlderThan = time.Minute - if got := leaseReclaimerIDs(leaseReclaim(t, ctx, fixture, request)); !slices.Equal(got, []string{id}) { - t.Fatalf("OlderThan 1m over a lease 10m stale: Reclaimed = %v, want [%s]", got, id) - } -} - -// RunLeaseReclaimerScopesToTheNamedIDs pins the IDs scope from both sides: an -// id that is not named is never reverted though it is stale, and an id that is -// named but absent or live is simply missing from Reclaimed — the call does -// not fail and does not report it. -func RunLeaseReclaimerScopesToTheNamedIDs(t *testing.T, ctx context.Context, fixture LeaseReclaimerFixture) { - t.Helper() - requireLeaseReclaimerExec(t, fixture) - named := fixture.IssuePrefix + "-scope-named" - unnamed := fixture.IssuePrefix + "-scope-unnamed" - live := fixture.IssuePrefix + "-scope-live" - ghost := fixture.IssuePrefix + "-scope-ghost" - leaseReclaimerSeedStale(t, ctx, fixture, leaseReclaimerHolder, named, unnamed) - leaseReclaimerSeedClaimed(t, ctx, fixture, leaseReclaimerHolder, live) - - result := leaseReclaim(t, ctx, fixture, publicops.ReclaimRequest{ - Actor: leaseReclaimerReaper, - Filter: publicops.ReclaimFilter{IDs: []string{ghost, live, named}}, - }) - if got := leaseReclaimerIDs(result); !slices.Equal(got, []string{named}) { - t.Fatalf("Reclaimed = %v, want [%s]: only the named stale lease", got, named) - } - leaseReclaimerRequireHeld(t, ctx, fixture, unnamed) - leaseReclaimerRequireHeld(t, ctx, fixture, live) -} - -// RunLeaseReclaimerAnswersAnAbsentIDWithAnEmptyResult pins the empty answer: -// a sweep scoped only to ids that name no row is not a refusal, and its -// Reclaimed is empty and NON-NIL (ReclaimResult.Reclaimed: "Never nil for a -// successful call"). -func RunLeaseReclaimerAnswersAnAbsentIDWithAnEmptyResult(t *testing.T, ctx context.Context, fixture LeaseReclaimerFixture) { - t.Helper() - result, err := fixture.LeaseReclaimer.Reclaim(ctx, publicops.ReclaimRequest{ - Actor: leaseReclaimerReaper, - Filter: publicops.ReclaimFilter{IDs: []string{fixture.IssuePrefix + "-absent-a", fixture.IssuePrefix + "-absent-b"}}, - }) - if err != nil { - t.Fatalf("Reclaim scoped to absent ids = %v, want an empty answer", err) - } - if result.Reclaimed == nil { - t.Fatal("Reclaimed is nil; the role promises an empty, non-nil slice") - } - if len(result.Reclaimed) != 0 { - t.Fatalf("Reclaimed = %+v, want nothing", result.Reclaimed) - } -} - -// RunLeaseReclaimerScopesToAssigneesAndLabels re-homes the raw method's scope -// cases (testReclaimScoped) onto the role: assignee, label (AND), label-any -// (OR) and exclude-label each narrow the sweep and never widen it. -func RunLeaseReclaimerScopesToAssigneesAndLabels(t *testing.T, ctx context.Context, fixture LeaseReclaimerFixture) { - t.Helper() - requireLeaseReclaimerExec(t, fixture) - p := fixture.IssuePrefix - a1, a2, b1, c1 := p+"-lbl-a1", p+"-lbl-a2", p+"-lbl-b1", p+"-lbl-c1" - laneA, laneB, opus := p+"-lane-a", p+"-lane-b", p+"-tier-opus" - workerA, workerB, workerC := p+"-wa", p+"-wb", p+"-wc" - leaseReclaimerSeedStale(t, ctx, fixture, workerA, a1, a2) - leaseReclaimerSeedStale(t, ctx, fixture, workerB, b1) - leaseReclaimerSeedStale(t, ctx, fixture, workerC, c1) - leaseReclaimerLabel(t, ctx, fixture, a1, laneA) - leaseReclaimerLabel(t, ctx, fixture, a2, laneA) - leaseReclaimerLabel(t, ctx, fixture, a2, opus) - leaseReclaimerLabel(t, ctx, fixture, b1, laneB) - - reclaim := func(filter publicops.ReclaimFilter) []string { - t.Helper() - return leaseReclaimerIDs(leaseReclaim(t, ctx, fixture, publicops.ReclaimRequest{Actor: leaseReclaimerReaper, Filter: filter})) - } - - if got := reclaim(publicops.ReclaimFilter{Labels: []string{laneB}}); !slices.Equal(got, []string{b1}) { - t.Fatalf("reclaim(labels %s) = %v, want [%s]", laneB, got, b1) - } - if got := reclaim(publicops.ReclaimFilter{Assignees: []string{workerC}}); !slices.Equal(got, []string{c1}) { - t.Fatalf("reclaim(assignees %s) = %v, want [%s]", workerC, got, c1) - } - if got := reclaim(publicops.ReclaimFilter{Labels: []string{laneA}, ExcludeLabels: []string{opus}}); !slices.Equal(got, []string{a1}) { - t.Fatalf("reclaim(labels %s, exclude %s) = %v, want [%s]", laneA, opus, got, a1) - } - leaseReclaimerRequireHeld(t, ctx, fixture, a2) - if got := reclaim(publicops.ReclaimFilter{LabelsAny: []string{p + "-lane-z", opus}}); !slices.Equal(got, []string{a2}) { - t.Fatalf("reclaim(labels-any lane-z,%s) = %v, want [%s]", opus, got, a2) - } -} - -// RunLeaseReclaimerAttributesTheRecoveryEvent pins ReclaimRequest.Actor: the -// recovery event each reverted row records names the actor who ran the sweep, -// with the lease's former holder beside it. -func RunLeaseReclaimerAttributesTheRecoveryEvent(t *testing.T, ctx context.Context, fixture LeaseReclaimerFixture) { - t.Helper() - requireLeaseReclaimerExec(t, fixture) - id := fixture.IssuePrefix + "-event" - leaseReclaimerSeedStale(t, ctx, fixture, leaseReclaimerHolder, id) - leaseReclaim(t, ctx, fixture, publicops.ReclaimRequest{ - Actor: leaseReclaimerReaper, - Filter: publicops.ReclaimFilter{IDs: []string{id}}, - }) - if n := leaseReclaimerScalar[int](t, ctx, fixture, - "SELECT COUNT(*) FROM events WHERE issue_id = ? AND event_type = ? AND actor = ? AND old_value = ?", - id, string(types.EventLeaseReclaimed), leaseReclaimerReaper, leaseReclaimerHolder); n != 1 { - t.Fatalf("lease_reclaimed events by %s naming %s = %d, want 1", leaseReclaimerReaper, leaseReclaimerHolder, n) - } -} - -// RunLeaseReclaimerFiresOnUpdateOncePerReclaimedRow pins the hook the role -// names (LeaseReclaimer: "ITS HOOK IS on_update, once per reverted row"): each -// reverted row fires on_update exactly once, and a row the sweep did not -// revert — named but live — fires nothing. -func RunLeaseReclaimerFiresOnUpdateOncePerReclaimedRow(t *testing.T, ctx context.Context, fixture LeaseReclaimerFixture) { - t.Helper() - requireLeaseReclaimerExec(t, fixture) - if fixture.HookedLeaseReclaimer == nil || fixture.FiredUpdates == nil { - t.Skip("LeaseReclaimerFixture.HookedLeaseReclaimer/FiredUpdates is nil: this backend cannot observe its hooks") - } - first := fixture.IssuePrefix + "-hook-a" - second := fixture.IssuePrefix + "-hook-b" - live := fixture.IssuePrefix + "-hook-live" - leaseReclaimerSeedStale(t, ctx, fixture, leaseReclaimerHolder, first, second) - leaseReclaimerSeedClaimed(t, ctx, fixture, leaseReclaimerHolder, live) - before, err := fixture.FiredUpdates(ctx) - if err != nil { - t.Fatalf("FiredUpdates before: %v", err) - } - - result, err := fixture.HookedLeaseReclaimer.Reclaim(ctx, publicops.ReclaimRequest{ - Actor: leaseReclaimerReaper, - Filter: publicops.ReclaimFilter{IDs: []string{first, second, live}}, - }) - if err != nil { - t.Fatalf("hooked Reclaim: %v", err) - } - if got := leaseReclaimerIDs(result); !slices.Equal(got, []string{first, second}) { - t.Fatalf("Reclaimed = %v, want [%s %s]", got, first, second) - } - - after, err := fixture.FiredUpdates(ctx) - if err != nil { - t.Fatalf("FiredUpdates after: %v", err) - } - fired := map[string]int{} - for _, id := range after[len(before):] { - fired[id]++ - } - for _, id := range []string{first, second} { - if fired[id] != 1 { - t.Fatalf("on_update fired %d time(s) for %s, want exactly 1 (fired: %v)", fired[id], id, after[len(before):]) - } - } - if fired[live] != 0 { - t.Fatalf("on_update fired for %s, which the sweep did not revert", live) - } -} - -// RunLeaseReclaimerRefusesAMalformedRequest pins every refusal the role lists, -// each as ErrValidation (the cap as *TooManyReclaimIDsError), and that a -// refusal changes nothing: the stale lease every request names survives all of -// them. -func RunLeaseReclaimerRefusesAMalformedRequest(t *testing.T, ctx context.Context, fixture LeaseReclaimerFixture) { - t.Helper() - requireLeaseReclaimerExec(t, fixture) - id := fixture.IssuePrefix + "-malformed" - leaseReclaimerSeedStale(t, ctx, fixture, leaseReclaimerHolder, id) - - tooMany := make([]string, publicops.MaxReclaimIDs+1) - for i := range tooMany { - tooMany[i] = id - } - for _, test := range []struct { - name string - request publicops.ReclaimRequest - tooMany bool - }{ - {"an empty actor", publicops.ReclaimRequest{Filter: publicops.ReclaimFilter{IDs: []string{id}}}, false}, - // One past the column every reverted row's recovery event records it - // in: a typed refusal before the sweep, never the backend's own error - // from the first row it would have reverted. - {"an over-long actor", publicops.ReclaimRequest{Actor: strings.Repeat("r", types.MaxFieldLen+1), - Filter: publicops.ReclaimFilter{IDs: []string{id}}}, false}, - {"a negative grace window", publicops.ReclaimRequest{Actor: leaseReclaimerReaper, OlderThan: -time.Second, - Filter: publicops.ReclaimFilter{IDs: []string{id}}}, false}, - {"a blank scoped id", publicops.ReclaimRequest{Actor: leaseReclaimerReaper, - Filter: publicops.ReclaimFilter{IDs: []string{id, ""}}}, false}, - {"more ids than the cap", publicops.ReclaimRequest{Actor: leaseReclaimerReaper, - Filter: publicops.ReclaimFilter{IDs: tooMany}}, true}, - } { - _, err := fixture.LeaseReclaimer.Reclaim(ctx, test.request) - if !errors.Is(err, publicops.ErrValidation) { - t.Fatalf("%s: Reclaim error = %v, want ErrValidation", test.name, err) - } - if test.tooMany { - var capErr *publicops.TooManyReclaimIDsError - if !errors.As(err, &capErr) || capErr.Requested != len(tooMany) || capErr.Cap != publicops.MaxReclaimIDs { - t.Fatalf("%s: Reclaim error = %v, want *TooManyReclaimIDsError{Requested:%d Cap:%d}", - test.name, err, len(tooMany), publicops.MaxReclaimIDs) - } - } - leaseReclaimerRequireHeld(t, ctx, fixture, id) - } -} - -// RunLeaseReclaimerRefusesABlankScopeEntry pins the blank-entry refusal on -// the scopes that are not ids: an empty or all-blank assignee or label is -// ErrValidation as a *ReclaimFieldError naming its field, and changes -// nothing — the stale lease every request names survives. -func RunLeaseReclaimerRefusesABlankScopeEntry(t *testing.T, ctx context.Context, fixture LeaseReclaimerFixture) { - t.Helper() - requireLeaseReclaimerExec(t, fixture) - id := fixture.IssuePrefix + "-blankscope" - leaseReclaimerSeedStale(t, ctx, fixture, leaseReclaimerHolder, id) - - for _, test := range []struct { - field string - filter publicops.ReclaimFilter - }{ - {publicops.ReclaimFieldAssignees, publicops.ReclaimFilter{IDs: []string{id}, Assignees: []string{leaseReclaimerHolder, " "}}}, - {publicops.ReclaimFieldLabels, publicops.ReclaimFilter{IDs: []string{id}, Labels: []string{""}}}, - {publicops.ReclaimFieldLabelsAny, publicops.ReclaimFilter{IDs: []string{id}, LabelsAny: []string{"\t"}}}, - {publicops.ReclaimFieldExcludeLabels, publicops.ReclaimFilter{IDs: []string{id}, ExcludeLabels: []string{"x", " "}}}, - } { - _, err := fixture.LeaseReclaimer.Reclaim(ctx, publicops.ReclaimRequest{Actor: leaseReclaimerReaper, Filter: test.filter}) - var fieldErr *publicops.ReclaimFieldError - if !errors.Is(err, publicops.ErrValidation) || !errors.As(err, &fieldErr) || fieldErr.Field != test.field { - t.Fatalf("blank %s entry: Reclaim error = %v, want a *ReclaimFieldError naming %s", test.field, err, test.field) - } - leaseReclaimerRequireHeld(t, ctx, fixture, id) - } -} - -// RunLeaseReclaimerAcceptsExactlyTheCap pins the cap's boundary: MaxReclaimIDs -// entries is a request, one more is a refusal (the case above). -func RunLeaseReclaimerAcceptsExactlyTheCap(t *testing.T, ctx context.Context, fixture LeaseReclaimerFixture) { - t.Helper() - ids := make([]string, publicops.MaxReclaimIDs) - for i := range ids { - ids[i] = fmt.Sprintf("%s-cap-%04d", fixture.IssuePrefix, i) - } - result, err := fixture.LeaseReclaimer.Reclaim(ctx, publicops.ReclaimRequest{ - Actor: leaseReclaimerReaper, Filter: publicops.ReclaimFilter{IDs: ids}, - }) - if err != nil { - t.Fatalf("Reclaim with exactly MaxReclaimIDs ids = %v, want an answer", err) - } - if len(result.Reclaimed) != 0 { - t.Fatalf("Reclaimed = %+v, want nothing: none of the ids exist", result.Reclaimed) - } -} - -// RunLeaseReclaimerDoesNotMutateTheCallerRequest pins the role's "never mutate -// caller-owned request values": the scope slices come back as they were sent. -func RunLeaseReclaimerDoesNotMutateTheCallerRequest(t *testing.T, ctx context.Context, fixture LeaseReclaimerFixture) { - t.Helper() - requireLeaseReclaimerExec(t, fixture) - id := fixture.IssuePrefix + "-nomut" - leaseReclaimerSeedStale(t, ctx, fixture, leaseReclaimerHolder, id) - ids := []string{fixture.IssuePrefix + "-nomut-ghost", id} - assignees := []string{leaseReclaimerHolder} - request := publicops.ReclaimRequest{ - Actor: leaseReclaimerReaper, - Filter: publicops.ReclaimFilter{IDs: ids, Assignees: assignees}, - } - leaseReclaim(t, ctx, fixture, request) - if !slices.Equal(ids, []string{fixture.IssuePrefix + "-nomut-ghost", id}) || !slices.Equal(assignees, []string{leaseReclaimerHolder}) { - t.Fatalf("Reclaim rewrote the caller's scope: ids=%v assignees=%v", ids, assignees) - } -} - -// NewUpdateHookLog plants an on_update script that appends the id of every -// issue it fires for, and returns the runner a hook-firing chain is built over -// plus a reader for LeaseReclaimerFixture.FiredUpdates. The reader waits for -// in-flight hooks before reading, so it answers for every hook already fired. -// -// It answers (nil, nil) where a shell hook cannot run, so a fixture built from -// it leaves the hook case to skip rather than fail. -func NewUpdateHookLog(t *testing.T) (*hooks.Runner, func(context.Context) ([]string, error)) { - t.Helper() - if runtime.GOOS == "windows" { - return nil, nil - } - dir := t.TempDir() - log := filepath.Join(t.TempDir(), "on_update.log") - script := "#!/bin/sh\necho \"$1\" >> " + log + "\n" - if err := os.WriteFile(filepath.Join(dir, "on_update"), []byte(script), 0o755); err != nil { //nolint:gosec // G306: a hook must be executable to run at all - t.Fatalf("plant the on_update hook: %v", err) - } - runner := hooks.NewRunner(dir) - return runner, func(context.Context) ([]string, error) { - if !runner.Wait(30 * time.Second) { - return nil, errors.New("on_update hooks still running after 30s") - } - data, err := os.ReadFile(log) // #nosec G304 -- this helper's own temp file - if errors.Is(err, os.ErrNotExist) { - return []string{}, nil - } - if err != nil { - return nil, err - } - var ids []string - for _, line := range strings.Split(strings.TrimSpace(string(data)), "\n") { - if line = strings.TrimSpace(line); line != "" { - ids = append(ids, line) - } - } - return ids, nil - } -} - -func requireLeaseReclaimerExec(t *testing.T, fixture LeaseReclaimerFixture) { - t.Helper() - if fixture.Exec == nil || fixture.Claimer == nil || fixture.QueryScalar == nil { - t.Skip("LeaseReclaimerFixture.Exec/Claimer/QueryScalar is nil: this backend cannot manufacture a stale lease") - } -} - -// leaseReclaimerSeedClaimed creates each id and claims it as holder through the -// leg's Claimer, answering each claim's row version. -func leaseReclaimerSeedClaimed(t *testing.T, ctx context.Context, fixture LeaseReclaimerFixture, holder string, ids ...string) map[string]int64 { - t.Helper() - versions := map[string]int64{} - for _, id := range ids { - if err := fixture.CreateIssue(ctx, &types.Issue{ - ID: id, Title: id, Status: types.StatusOpen, Priority: 2, IssueType: types.TypeTask, - }, "seed"); err != nil { - t.Fatalf("seed %s: %v", id, err) - } - claimed, err := fixture.Claimer.Claim(ctx, publicops.ClaimRequest{Actor: holder, IssueID: id}) - if err != nil { - t.Fatalf("claim %s as %s: %v", id, holder, err) - } - if claimed.Issue == nil { - t.Fatalf("claim %s answered no issue", id) - } - versions[id] = claimed.Issue.RowVersion - } - return versions -} - -// leaseReclaimerSeedStale claims each id and ages its lease an hour past -// expiry, far beyond any grace window a case below uses by default. -func leaseReclaimerSeedStale(t *testing.T, ctx context.Context, fixture LeaseReclaimerFixture, holder string, ids ...string) map[string]int64 { - t.Helper() - versions := leaseReclaimerSeedClaimed(t, ctx, fixture, holder, ids...) - leaseReclaimerAge(t, ctx, fixture, time.Hour, ids...) - return versions -} - -// leaseReclaimerAge moves each id's lease expiry to age in the past. -func leaseReclaimerAge(t *testing.T, ctx context.Context, fixture LeaseReclaimerFixture, age time.Duration, ids ...string) { - t.Helper() - expired := time.Now().UTC().Add(-age) - statements := make([]SQLStatement, 0, len(ids)) - for _, id := range ids { - statements = append(statements, SQLStatement{ - Query: "UPDATE leases SET lease_expires_at = ? WHERE issue_id = ?", - Args: []any{expired, id}, - }) - } - if err := fixture.Exec(ctx, statements); err != nil { - t.Fatalf("age leases %v: %v", ids, err) - } - for _, id := range ids { - if n := leaseReclaimerScalar[int](t, ctx, fixture, "SELECT COUNT(*) FROM leases WHERE issue_id = ?", id); n != 1 { - t.Fatalf("lease rows for %s = %d, want 1: the claim wrote no lease to reclaim", id, n) - } - } -} - -func leaseReclaimerLabel(t *testing.T, ctx context.Context, fixture LeaseReclaimerFixture, id, label string) { - t.Helper() - if err := fixture.Exec(ctx, []SQLStatement{{ - Query: "INSERT INTO labels (issue_id, label) VALUES (?, ?)", - Args: []any{id, label}, - }}); err != nil { - t.Fatalf("label %s %s: %v", id, label, err) - } -} - -func leaseReclaimerRequireHeld(t *testing.T, ctx context.Context, fixture LeaseReclaimerFixture, id string) { - t.Helper() - if status := leaseReclaimerScalar[string](t, ctx, fixture, "SELECT status FROM issues WHERE id = ?", id); status != string(types.StatusInProgress) { - t.Fatalf("%s status = %q, want it still %q", id, status, types.StatusInProgress) - } -} - -func leaseReclaim(t *testing.T, ctx context.Context, fixture LeaseReclaimerFixture, request publicops.ReclaimRequest) publicops.ReclaimResult { - t.Helper() - result, err := fixture.LeaseReclaimer.Reclaim(ctx, request) - if err != nil { - t.Fatalf("Reclaim(%+v): %v", request.Filter, err) - } - if result.Reclaimed == nil { - t.Fatal("Reclaimed is nil; the role promises a non-nil slice for a successful call") - } - return result -} - -func leaseReclaimerIDs(result publicops.ReclaimResult) []string { - ids := make([]string, 0, len(result.Reclaimed)) - for _, r := range result.Reclaimed { - ids = append(ids, r.ID) - } - sort.Strings(ids) - return ids -} - -func leaseReclaimerScalar[T any](t *testing.T, ctx context.Context, fixture LeaseReclaimerFixture, query string, args ...any) T { - t.Helper() - var value T - if err := fixture.QueryScalar(ctx, query, args, &value); err != nil { - t.Fatalf("%s %v: %v", query, args, err) - } - return value -} diff --git a/backend/conformance/lifecycle_close_reopen_contract.go b/backend/conformance/lifecycle_close_reopen_contract.go index c11556181..ca40fe53a 100644 --- a/backend/conformance/lifecycle_close_reopen_contract.go +++ b/backend/conformance/lifecycle_close_reopen_contract.go @@ -846,12 +846,11 @@ func RunLifecycleCloseAndReopenSpanTheConfiguredDoneCategory(t *testing.T, ctx c // RunLifecycleExpectedVersionIsCheckedBeforeTheNoOps pins the ORDERING clause // both requests spell out. CloseRequest.ExpectedVersion "requires the current -// row version to match, and is checked BEFORE the idempotent close" -// (issueops/issueops.go:362-373); ReopenRequest.ExpectedVersion is "checked -// before a non-done no-op" (:390-391); and Close's own doc adds "ExpectedVersion +// row version to match and is checked before an idempotent close" +// (issueops/issueops.go:295-300); ReopenRequest.ExpectedVersion is "checked +// before a non-done no-op" (:272-275); and Close's own doc adds "ExpectedVersion // is checked first, including for an idempotent close", with Force bypassing -// "the pin, the assignee fence, and blocker and open-child policy" and nothing -// else (:485-498). +// "blocker and open-child policy" and nothing else (:364-365). // // The dangerous shape is not a stale version on a live mutation — it is a stale // version on the request that would have done nothing anyway. An implementation @@ -1093,87 +1092,6 @@ func RunLifecycleResultsAreHydratedPostStateSnapshots(t *testing.T, ctx context. lifecycleCloseReopenAssertSnapshot(t, "reopen result", reopened.Issue, peer) } -// RunLifecycleResultsCarryThePostWriteRowVersion pins that every write this -// role owns — Update, Close and Reopen — hands back a result whose -// Issue.RowVersion is the row's REAL post-write optimistic-concurrency token, -// not a zero value. types.Issue.RowVersion is `json:"-"`: an implementation -// that forgot to populate it on the way back from its own storage would still -// compile and would still pass every content assertion elsewhere in this -// file, because none of them read RowVersion. -// -// The assertion is a CHAIN rather than three independent zero-checks, because -// a zero-check alone cannot tell a dropped token from a WRONG one (an -// implementation could answer a nonzero constant, or the PRE-write version, -// and still clear it). Each step feeds the token the previous write answered -// with straight into the next write's ExpectedVersion, with no intervening -// read: the claim's token guards the Notes update, Update's token guards -// Close, and Close's token guards Reopen. A guard only passes if the token it -// was given equals the row's actual current version at the instant the next -// write runs, so a wrong or stale token fails closed — the next write -// refuses with ErrVersionMismatch — rather than silently succeeding. Any one -// dropped member breaks the chain at that link. -// -// The chain's first link is a CLAIM-ONLY request (Claim: true, no Patch), -// deliberately unguarded (UpdateRequest.Claim forbids combining Claim with -// ExpectedVersion — see issueops.UpdateRequest's doc): a backend that answers -// Lifecycle.Update's generic patch wire but special-cases a bare claim onto a -// different internal path (the http leg once dialed the Claimer role directly -// for one; it now folds the claim into updateIssue, keeping claimIssue only as -// the fallback for a server that predates updateIssue's `claim` member) could -// easily populate RowVersion on one path and not the other; this exercises -// the claim-only shape specifically, not just the Patch shape the rest of the -// chain already covered. -func RunLifecycleResultsCarryThePostWriteRowVersion(t *testing.T, ctx context.Context, fixture LifecycleCloseReopenFixture) { - t.Helper() - - id := fixture.IssuePrefix + "-lcr-rowversion" - lifecycleCloseReopenSeedIssue(t, ctx, fixture, id, types.StatusOpen, nil) - - claimed, err := fixture.Lifecycle.Update(ctx, publicops.UpdateRequest{ - Actor: "writer", IssueID: id, Claim: true, - }) - if err != nil { - t.Fatalf("claim %s: %v", id, err) - } - if claimed.Issue == nil || claimed.Issue.RowVersion == 0 { - t.Fatalf("claim result RowVersion = %+v, want a nonzero post-write token", claimed.Issue) - } - claimVersion := claimed.Issue.RowVersion - - updated, err := fixture.Lifecycle.Update(ctx, publicops.UpdateRequest{ - Actor: "writer", IssueID: id, ExpectedVersion: &claimVersion, - Patch: publicops.IssuePatch{Notes: publicops.Field[string]{Set: true, Value: "rowversion chain"}}, - }) - if err != nil { - t.Fatalf("update %s guarded by the claim's own token: %v — the claim result's RowVersion was not the row's real current version", id, err) - } - if updated.Issue == nil || updated.Issue.RowVersion == 0 { - t.Fatalf("update result RowVersion = %+v, want a nonzero post-write token", updated.Issue) - } - updateVersion := updated.Issue.RowVersion - - closed, err := fixture.Lifecycle.Close(ctx, publicops.CloseRequest{ - Actor: "writer", IssueID: id, ExpectedVersion: &updateVersion, - }) - if err != nil { - t.Fatalf("close %s guarded by the update's own token: %v — the update result's RowVersion was not the row's real current version", id, err) - } - if closed.Issue == nil || closed.Issue.RowVersion == 0 { - t.Fatalf("close result RowVersion = %+v, want a nonzero post-write token", closed.Issue) - } - closeVersion := closed.Issue.RowVersion - - reopened, err := fixture.Lifecycle.Reopen(ctx, publicops.ReopenRequest{ - Actor: "writer", IssueID: id, ExpectedVersion: &closeVersion, - }) - if err != nil { - t.Fatalf("reopen %s guarded by the close's own token: %v — the close result's RowVersion was not the row's real current version", id, err) - } - if reopened.Issue == nil || reopened.Issue.RowVersion == 0 { - t.Fatalf("reopen result RowVersion = %+v, want a nonzero post-write token", reopened.Issue) - } -} - // RunLifecycleCloseAndReopenRequireActorAndIssueID pins the deterministic // validation floor both verbs sit on. The Lifecycle doc states "Deterministic // request validation failures match ErrValidation" and "Refusals and @@ -1384,14 +1302,13 @@ func RunLifecycleCloseSettlesItsTransitiveAndCrossPlaneDependers(t *testing.T, c // RunLifecycleCloseAdmitsATransitivelyBlockedTarget uses clears the first and // not the second, and closing the child to clear the second would take the // second subject with it, since a closed row cannot be blocked and so has -// nothing left to flip. Force waives close policy, the pin and the assignee -// fence, and nothing else — CloseRequest.Force "bypasses blocker and -// open-child close policy and the two close guards a force has always waived -// in bd" and "never bypasses the template guard, validation, ExpectedVersion, -// or lifecycle rules" (issueops/issueops.go:357-360) — so it is the shape that -// keeps both subjects observable, and it is not exotic: it is the same forced -// close of a blocked issue that -// RunLifecycleCloseIsIdempotentOnAClosedRowThatStillLooksBlocked is built on. +// nothing left to flip. Force waives close policy and nothing else — +// CloseRequest.Force "bypasses only blocker and open-child close policy" and +// "never bypasses validation, ExpectedVersion, or lifecycle rules" +// (issueops/issueops.go:310-311) — so it is the shape that keeps both subjects +// observable, and it is not exotic: it is the same forced close of a +// blocked issue that RunLifecycleCloseIsIdempotentOnAClosedRowThatStillLooksBlocked +// is built on. // // All three legs reach ONE body here (internal/storage/issueops.closeIssueInTx), // so this is a wrapper and engine check rather than a third vote, exactly as @@ -1824,175 +1741,3 @@ func (c *lifecycleCloseReopenEventCounter) assertNoneAdded(t *testing.T, label s } c.total = total } - -// RunLifecycleCloseEnforcesTheCloseGuards pins the three close guards -// issueops.Lifecycle.Close states before close policy — template read-only, -// the pin, and the assignee authority fence (be-035) — on the role itself, so -// every caller of it is held to them and not only `bd close`, which used to -// apply them as a CLI pre-read the served and library routes skipped. -// -// Each refusal is asserted at the level a caller acts on: the typed error with -// its fields, its sentinel, and the sentence bd prints — read off the typed -// error, because a served refusal wraps it in the problem envelope — plus the -// row and the event stream left untouched. Force is asserted as the bypass for -// exactly the two guards it has always waived, and NOT for the template. -// -// The controls are load-bearing too: an unassigned issue and one held by the -// closing actor under a different spelling both close unforced, so a guard -// that refused every assigned row, or compared verbatim, fails here. A wisp -// case proves the guard reads the plane the close writes. A pinned issue with -// an open child reads the PIN's refusal, pinning the order (guards before -// policy). And a forced close of a boolean-pinned issue leaves the pin set, so -// the plain retry must stay the idempotent no-op (ga-ktn9pe.4.8) rather than -// refuse on the residue. -func RunLifecycleCloseEnforcesTheCloseGuards(t *testing.T, ctx context.Context, fixture LifecycleCloseReopenFixture) { - t.Helper() - - template := fixture.IssuePrefix + "-lcg-template" - statusPinned := fixture.IssuePrefix + "-lcg-status-pinned" - flagPinned := fixture.IssuePrefix + "-lcg-flag-pinned" - held := fixture.IssuePrefix + "-lcg-held" - respelled := fixture.IssuePrefix + "-lcg-respelled" - unassigned := fixture.IssuePrefix + "-lcg-unassigned" - pinnedParent := fixture.IssuePrefix + "-lcg-pinned-parent" - pinnedChild := fixture.IssuePrefix + "-lcg-pinned-child" - wisp := fixture.IssuePrefix + "-lcg-wisp-held" - - seed := func(issue *types.Issue) { - t.Helper() - issue.Title, issue.Priority, issue.IssueType = issue.ID, 2, types.TypeTask - if issue.Status == "" { - issue.Status = types.StatusOpen - } - if err := fixture.CreateIssue(ctx, issue, "seed"); err != nil { - t.Fatalf("seed %s: %v", issue.ID, err) - } - } - seed(&types.Issue{ID: template, IsTemplate: true}) - seed(&types.Issue{ID: statusPinned, Status: types.StatusPinned}) - seed(&types.Issue{ID: flagPinned, Pinned: true}) - seed(&types.Issue{ID: held, Assignee: "holder"}) - seed(&types.Issue{ID: respelled, Assignee: "gastown.closer"}) - seed(&types.Issue{ID: unassigned}) - seed(&types.Issue{ID: pinnedParent, Pinned: true}) - seed(&types.Issue{ID: pinnedChild}) - lifecycleCloseReopenSeedEdge(t, ctx, fixture, pinnedChild, pinnedParent, types.DepParentChild) - - refuses := func(id string, force bool, label string, check func(error)) { - t.Helper() - before := lifecycleCloseReopenReadRow(t, ctx, fixture, id) - events := newLifecycleCloseReopenEventCounter(t, ctx, fixture, id) - _, err := fixture.Lifecycle.Close(ctx, publicops.CloseRequest{Actor: "closer", IssueID: id, Force: force}) - check(err) - lifecycleCloseReopenAssertRow(t, ctx, fixture, id, "after the "+label+" refusal", before) - events.assertNoneAdded(t, label+" refusal") - } - - // The template refuses, forced or not: no flag waives read-only. - for _, force := range []bool{false, true} { - refuses(template, force, "template", func(err error) { - var refusal *publicops.TemplateReadOnlyError - if !errors.As(err, &refusal) { - t.Fatalf("close of template %s (force=%v): err = %v, want *TemplateReadOnlyError", template, force, err) - } - if refusal.IssueID != template || !errors.Is(err, publicops.ErrTemplateReadOnly) { - t.Errorf("template refusal = %#v (%v), want IssueID %q matching ErrTemplateReadOnly", refusal, err, template) - } - want := "cannot modify template " + template + ": templates are read-only; use 'bd mol pour' to create a work item" - if refusal.Error() != want { - t.Errorf("template refusal reads %q, want %q", refusal.Error(), want) - } - }) - } - - // Both pin spellings refuse unforced (ga-z3vht). - for _, id := range []string{statusPinned, flagPinned} { - refuses(id, false, "pin", func(err error) { - var refusal *publicops.PinnedError - if !errors.As(err, &refusal) { - t.Fatalf("unforced close of pinned %s: err = %v, want *PinnedError", id, err) - } - if refusal.IssueID != id || !errors.Is(err, publicops.ErrPinned) { - t.Errorf("pin refusal = %#v (%v), want IssueID %q matching ErrPinned", refusal, err, id) - } - if want := "cannot modify pinned issue " + id + " (use --force to override)"; refusal.Error() != want { - t.Errorf("pin refusal reads %q, want %q", refusal.Error(), want) - } - }) - } - - // Another actor's bead refuses unforced, naming holder and actor. - refuses(held, false, "assignee", func(err error) { - var refusal *publicops.CloseNotAssigneeError - if !errors.As(err, &refusal) { - t.Fatalf("unforced close of %s held by another actor: err = %v, want *CloseNotAssigneeError", held, err) - } - if refusal.IssueID != held || refusal.Assignee != "holder" || refusal.Actor != "closer" { - t.Errorf("assignee refusal = %#v, want IssueID %q, Assignee holder, Actor closer", refusal, held) - } - if !errors.Is(err, publicops.ErrNotOwner) { - t.Errorf("assignee refusal %v does not match ErrNotOwner", err) - } - if want := `cannot close ` + held + `: assignee is "holder", actor is "closer"; reclaim or use --force to override`; refusal.Error() != want { - t.Errorf("assignee refusal reads %q, want %q", refusal.Error(), want) - } - }) - - // The guard runs before close policy: a pinned parent with an open child - // reads the pin, not the open children. - refuses(pinnedParent, false, "pin-before-policy", func(err error) { - if !errors.Is(err, publicops.ErrPinned) { - t.Fatalf("unforced close of pinned parent %s with an open child: err = %v, want ErrPinned first", pinnedParent, err) - } - }) - - // The controls: an unassigned bead and the actor's own bead under another - // spelling of its identity close unforced. - for _, tc := range []struct{ id, actor string }{{unassigned, "closer"}, {respelled, "gastown_closer"}} { - result, err := fixture.Lifecycle.Close(ctx, publicops.CloseRequest{Actor: tc.actor, IssueID: tc.id}) - if err != nil { - t.Fatalf("unforced close of %s by %s: %v, want it to land", tc.id, tc.actor, err) - } - if !result.Changed || result.Issue.Status != types.StatusClosed { - t.Errorf("unforced close of %s = %#v, want a committed close", tc.id, result) - } - } - - // Force bypasses the pin and the assignee fence. - for _, id := range []string{statusPinned, flagPinned, held} { - result, err := fixture.Lifecycle.Close(ctx, publicops.CloseRequest{Actor: "closer", IssueID: id, Force: true}) - if err != nil { - t.Fatalf("forced close of %s: %v, want force to bypass the guard", id, err) - } - if !result.Changed || result.Issue.Status != types.StatusClosed { - t.Errorf("forced close of %s = %#v, want a committed close", id, result) - } - } - - // The forced close left pinned=true on the flag-pinned row; the plain retry - // is the idempotent re-close, not a pin refusal. - retry, err := fixture.Lifecycle.Close(ctx, publicops.CloseRequest{Actor: "closer", IssueID: flagPinned}) - if err != nil { - t.Fatalf("plain re-close of the already-closed, still-pinned %s: %v, want the idempotent no-op", flagPinned, err) - } - if retry.Changed { - t.Errorf("plain re-close of %s reported Changed = true, want the no-op", flagPinned) - } - - // The guard reads the plane the close writes. - if err := fixture.CreateWisp(ctx, &types.Issue{ - ID: wisp, Title: wisp, Status: types.StatusOpen, Priority: 2, IssueType: types.TypeTask, Assignee: "holder", Ephemeral: true, - }, "seed"); err != nil { - t.Fatalf("seed wisp %s: %v", wisp, err) - } - if _, err := fixture.Lifecycle.Close(ctx, publicops.CloseRequest{Actor: "closer", IssueID: wisp}); !errors.As(err, new(*publicops.CloseNotAssigneeError)) { - t.Fatalf("unforced close of wisp %s held by another actor: err = %v, want *CloseNotAssigneeError", wisp, err) - } - var status string - if err := fixture.QueryScalar(ctx, "SELECT status FROM wisps WHERE id = ?", []any{wisp}, &status); err != nil { - t.Fatalf("read wisp %s status: %v", wisp, err) - } - if types.Status(status) != types.StatusOpen { - t.Errorf("wisp %s status after the refusal = %q, want it still open", wisp, status) - } -} diff --git a/backend/conformance/lifecycle_create_contract.go b/backend/conformance/lifecycle_create_contract.go index 5d728a6c8..ca906424a 100644 --- a/backend/conformance/lifecycle_create_contract.go +++ b/backend/conformance/lifecycle_create_contract.go @@ -490,12 +490,6 @@ func RunLifecycleCreateWritesEveryScalarField(t *testing.T, ctx context.Context, t.Errorf("%s created_at = %v after a create that named none, want a stamp between %v and %v — a bare non-empty check would accept the zero time", stamped, autoStamp, lower, upper) } - // And no created_by: the request's actor is the creator (CreateRequest.Issue), - // stamped by the role itself, so a library caller and every front door store - // the same value for the same request. - if got := lifecycleCreateRow(t, ctx, fixture, stamped).CreatedBy; got != "writer" { - t.Errorf("%s created_by = %q after a create that named none, want the request's actor %q", stamped, got, "writer") - } want := []lifecycleCreateMember{ {"title", "created title"}, @@ -810,54 +804,3 @@ func lifecycleCreatePlaneName(ephemeral bool) string { } return "durable" } - -// RunLifecycleCreateAppliesTheDefaultPriority pins -// CreateRequest.DefaultPriority: a create that asks for the default stores -// publicops.DefaultCreatePriority, and one that names priority 0 stores P0 — -// the zero value is a real priority, never "absent". Asking for the default -// while naming a priority is ErrValidation and writes nothing. -// -// It is the one place the default lives: `bd create` without --priority and an -// HTTP create without `priority` both reach it through this flag, so a backend -// that dropped it would store P0 (critical) for every such create. -func RunLifecycleCreateAppliesTheDefaultPriority(t *testing.T, ctx context.Context, fixture LifecycleCreateFixture) { - t.Helper() - defaulted := fixture.IssuePrefix + "-prio-default" - critical := fixture.IssuePrefix + "-prio-zero" - conflict := fixture.IssuePrefix + "-prio-conflict" - - for _, tc := range []struct { - id string - priority int - useDefault bool - want int - }{ - {id: defaulted, useDefault: true, want: publicops.DefaultCreatePriority}, - {id: critical, priority: 0, want: 0}, - } { - created, err := fixture.Lifecycle.Create(ctx, publicops.CreateRequest{ - Actor: "writer", - DefaultPriority: tc.useDefault, - Issue: &types.Issue{ID: tc.id, Title: tc.id, Priority: tc.priority, IssueType: types.TypeTask}, - }) - if err != nil { - t.Fatalf("create %s: %v", tc.id, err) - } - if created.Issue.Priority != tc.want { - t.Errorf("create %s result priority = %d, want %d", tc.id, created.Issue.Priority, tc.want) - } - if got := lifecycleCreateRow(t, ctx, fixture, tc.id).Priority; got != tc.want { - t.Errorf("stored %s priority = %d, want %d", tc.id, got, tc.want) - } - } - - _, err := fixture.Lifecycle.Create(ctx, publicops.CreateRequest{ - Actor: "writer", - DefaultPriority: true, - Issue: &types.Issue{ID: conflict, Title: conflict, Priority: 3, IssueType: types.TypeTask}, - }) - if !errors.Is(err, storage.ErrValidation) { - t.Fatalf("DefaultPriority with priority 3: err = %v, want ErrValidation", err) - } - assertLifecycleCreateAbsent(t, ctx, fixture, conflict, "after the conflicting priority create") -} diff --git a/backend/conformance/lifecycle_create_gate_contract.go b/backend/conformance/lifecycle_create_gate_contract.go deleted file mode 100644 index 4f5c22d30..000000000 --- a/backend/conformance/lifecycle_create_gate_contract.go +++ /dev/null @@ -1,170 +0,0 @@ -package conformance - -import ( - "context" - "errors" - "fmt" - "strings" - "testing" - "time" - - "github.com/steveyegge/beads/internal/types" - publicops "github.com/steveyegge/beads/issueops" -) - -// This file and its two siblings (lifecycle_update_gate_contract.go, -// batch_apply_gate_contract.go) hold the gate-member clauses of Lifecycle.Create, -// Lifecycle.Update and BatchApplier: await_type, await_id, timeout and waiters -// on a create, await_id and waiters on a patch, and the one set of bounds every -// route refuses with a *publicops.GateFieldError. -// -// THE BOUNDS ARE THE COLUMNS', and the boundary is asserted from both sides: a -// value AT the bound lands whole, a value one past it is refused naming its -// member and writes nothing. await_type is VARCHAR(32) and await_id -// VARCHAR(255), both counted in characters, so each at-bound value is -// multi-byte — a byte count would refuse it. waiters is a TEXT column holding -// a JSON array, bounded in BYTES. A negative timeout is the one refusal that is -// not a width. -// -// NOTHING ELSE IS A RULE, and two cases say so: a gate member lands on an -// issue of any type (local `bd update --await-id` has always worked on any -// row), and waiters are stored as given, duplicates included (deduplication is -// the CLI's). A backend that grew a stricter rule fails here. - -// gateFieldAtBoundAwaitType is a 32-character, 64-byte await_type. -var gateFieldAtBoundAwaitType = strings.Repeat("é", 32) - -// gateFieldAtBoundAwaitID is a 255-character, 510-byte await_id. -var gateFieldAtBoundAwaitID = strings.Repeat("é", types.MaxFieldLen) - -// gateFieldAtBoundWaiters is one waiter whose JSON array encoding is exactly -// types.MaxTextBytes bytes: the entry plus `["` and `"]`. -func gateFieldAtBoundWaiters() []string { - return []string{strings.Repeat("w", types.MaxTextBytes-4)} -} - -// gateFieldOverBoundWaiters encodes to one byte past the TEXT column. -func gateFieldOverBoundWaiters() []string { - return []string{strings.Repeat("w", types.MaxTextBytes-3)} -} - -// assertGateFieldError checks err is ErrValidation carried by a -// *publicops.GateFieldError naming field. -func assertGateFieldError(t *testing.T, err error, label, field string) { - t.Helper() - if !errors.Is(err, publicops.ErrValidation) { - t.Fatalf("%s: error = %v, want ErrValidation", label, err) - } - var fieldErr *publicops.GateFieldError - if !errors.As(err, &fieldErr) { - t.Fatalf("%s: error = %v, want a *GateFieldError naming %s", label, err, field) - } - if fieldErr.Field != field { - t.Errorf("%s: GateFieldError.Field = %q, want %q", label, fieldErr.Field, field) - } -} - -// gateMembers renders the four gate members off an issue for comparison. -func gateMembers(issue *types.Issue) string { - waiters := issue.Waiters - if len(waiters) == 0 { - waiters = nil - } - return fmt.Sprintf("await_type=%q await_id=%q timeout=%s waiters=%q", issue.AwaitType, issue.AwaitID, issue.Timeout, waiters) -} - -func assertGateMembers(t *testing.T, id, label string, issue *types.Issue, awaitType, awaitID string, timeout time.Duration, waiters []string) { - t.Helper() - if issue == nil { - t.Fatalf("%s %s: no issue to read the gate members off", id, label) - } - if len(waiters) == 0 { - waiters = nil - } - want := fmt.Sprintf("await_type=%q await_id=%q timeout=%s waiters=%q", awaitType, awaitID, timeout, waiters) - if got := gateMembers(issue); got != want { - // The at-bound values are long; report sizes rather than the values. - if len(got)+len(want) > 2000 { - t.Errorf("%s %s gate members differ (got %d bytes, want %d bytes; await_type %d/%d runes, await_id %d/%d runes, waiters %d/%d entries)", - id, label, len(got), len(want), - len([]rune(issue.AwaitType)), len([]rune(awaitType)), len([]rune(issue.AwaitID)), len([]rune(awaitID)), - len(issue.Waiters), len(waiters)) - return - } - t.Errorf("%s %s gate members = %s, want %s", id, label, got, want) - } -} - -// RunLifecycleCreateWritesTheGateMembers pins that Lifecycle.Create stores all -// four gate members (CreateRequest.Issue's "gate" fields) on the row and -// answers them on the result, with the waiter list stored as given, duplicate -// included, and on an issue that is not of the gate type. -func RunLifecycleCreateWritesTheGateMembers(t *testing.T, ctx context.Context, fixture LifecycleCreateFixture) { - t.Helper() - - gate := fixture.IssuePrefix + "-lcg-gate" - issue := lifecycleCreateIssue(gate) - issue.IssueType = types.TypeGate - issue.AwaitType = "gh:run" - issue.AwaitID = "12345" - issue.Timeout = 90 * time.Minute - issue.Waiters = []string{"rig/workers/a", "rig/workers/b", "rig/workers/a"} - created, err := fixture.Lifecycle.Create(ctx, publicops.CreateRequest{Actor: "writer", ForceIDPrefix: true, Issue: issue}) - if err != nil { - t.Fatalf("create gate %s: %v", gate, err) - } - want := []string{"rig/workers/a", "rig/workers/b", "rig/workers/a"} - assertGateMembers(t, gate, "in the create result", created.Issue, "gh:run", "12345", 90*time.Minute, want) - assertGateMembers(t, gate, "in the stored row", lifecycleCreateRow(t, ctx, fixture, gate), "gh:run", "12345", 90*time.Minute, want) - - // Any type carries them: no gate-type-only rule. - task := fixture.IssuePrefix + "-lcg-task" - carrier := lifecycleCreateIssue(task) - carrier.AwaitType = "formula-defined" - carrier.AwaitID = "bead:" + gate - carrier.Waiters = []string{"mail:someone"} - if _, err := fixture.Lifecycle.Create(ctx, publicops.CreateRequest{Actor: "writer", ForceIDPrefix: true, Issue: carrier}); err != nil { - t.Fatalf("create task %s carrying gate members: %v", task, err) - } - assertGateMembers(t, task, "in the stored row", lifecycleCreateRow(t, ctx, fixture, task), "formula-defined", "bead:"+gate, 0, []string{"mail:someone"}) -} - -// RunLifecycleCreateBoundsTheGateMembers pins the four GateFieldError bounds -// on create from both sides: each at-bound value lands whole, each value one -// past it is refused as ErrValidation naming its member, and the refused -// create leaves no row. -func RunLifecycleCreateBoundsTheGateMembers(t *testing.T, ctx context.Context, fixture LifecycleCreateFixture) { - t.Helper() - - atBound := fixture.IssuePrefix + "-lcg-atbound" - issue := lifecycleCreateIssue(atBound) - issue.IssueType = types.TypeGate - issue.AwaitType = gateFieldAtBoundAwaitType - issue.AwaitID = gateFieldAtBoundAwaitID - issue.Timeout = 0 - issue.Waiters = gateFieldAtBoundWaiters() - if _, err := fixture.Lifecycle.Create(ctx, publicops.CreateRequest{Actor: "writer", ForceIDPrefix: true, Issue: issue}); err != nil { - t.Fatalf("create %s with every gate member at its bound: %v", atBound, err) - } - assertGateMembers(t, atBound, "in the stored row", lifecycleCreateRow(t, ctx, fixture, atBound), - gateFieldAtBoundAwaitType, gateFieldAtBoundAwaitID, 0, gateFieldAtBoundWaiters()) - - for _, test := range []struct { - name string - field string - mutate func(*types.Issue) - }{ - {"await_type", publicops.GateFieldAwaitType, func(i *types.Issue) { i.AwaitType = gateFieldAtBoundAwaitType + "x" }}, - {"await_id", publicops.GateFieldAwaitID, func(i *types.Issue) { i.AwaitID = gateFieldAtBoundAwaitID + "x" }}, - {"timeout", publicops.GateFieldTimeout, func(i *types.Issue) { i.Timeout = -time.Nanosecond }}, - {"waiters", publicops.GateFieldWaiters, func(i *types.Issue) { i.Waiters = gateFieldOverBoundWaiters() }}, - } { - id := fixture.IssuePrefix + "-lcg-over-" + strings.ReplaceAll(test.name, "_", "") - refused := lifecycleCreateIssue(id) - refused.IssueType = types.TypeGate - test.mutate(refused) - _, err := fixture.Lifecycle.Create(ctx, publicops.CreateRequest{Actor: "writer", ForceIDPrefix: true, Issue: refused}) - assertGateFieldError(t, err, "create with "+test.name+" past its bound", test.field) - assertLifecycleCreateAbsent(t, ctx, fixture, id, "after the refused "+test.name) - } -} diff --git a/backend/conformance/lifecycle_update_contract.go b/backend/conformance/lifecycle_update_contract.go index ae5e0a420..4448f7ff1 100644 --- a/backend/conformance/lifecycle_update_contract.go +++ b/backend/conformance/lifecycle_update_contract.go @@ -4,11 +4,8 @@ import ( "context" "encoding/json" "errors" - "fmt" "reflect" - "slices" "sort" - "sync" "testing" "time" @@ -440,7 +437,7 @@ func RunLifecycleUpdateAppendsNotesWithoutReplacingThem(t *testing.T, ctx contex replaced, err := fixture.Lifecycle.Update(ctx, publicops.UpdateRequest{Actor: "writer", IssueID: id, Patch: publicops.IssuePatch{ Notes: publicops.Field[string]{Set: true, Value: "replaced"}, - }, ForceNotesOverwrite: true}) + }}) if err != nil { t.Fatalf("replace notes on %s: %v", id, err) } @@ -908,385 +905,6 @@ func RunLifecycleUpdateConditionalGuardsGateOrdinaryEdits(t *testing.T, ctx cont maskedEvents.assert(t, "guard masked by the one before it", 0) } -// RunLifecycleUpdateExpectedVersionSingleWinnerUnderConcurrency pins A8's -// (gastownhall/beads#4682) headline claim about Lifecycle.Update's -// ExpectedVersion guard, for `bd update/close/assign/delete --if-revision`: -// the compare and the write are ONE atomic operation, not a read-then-write a -// concurrent writer can slip between (T4.4, BEADS-JOURNAL-PLAN.md Item 4). N -// goroutines race the SAME token — the row's version as of before any of them -// run — against the same row; exactly one may apply it, every other goroutine -// must see ErrVersionMismatch (never a second silent success), and the row -// that is left behind must be exactly the single winner's content, never a -// torn write belonging to neither the seed nor any one racer. -// -// THIS IS A GENUINE CONCURRENCY TEST, not N sequential attempts dressed up as -// parallel ones. cmd/bd's embedded-mode backend admits only one `bd` process -// at a time (an exclusive flock — see acquireEmbeddedLock in -// cmd/bd/store_factory.go), which would make "exactly one winner" trivially -// true even of a buggy read-then-write implementation with no atomicity at -// all, because no second process could ever observe the first one's -// in-flight state. That is why the CLI-level --if-revision tests -// (cmd/bd/if_revision_embedded_test.go) assert the match/mismatch CONTRACT -// sequentially and leave the race to this fixture: goroutines sharing one -// already-open Lifecycle are the one shape in this suite that can actually -// interleave at the transaction level, independent of whatever process-level -// serialization a given CLI backend layers on top. -func RunLifecycleUpdateExpectedVersionSingleWinnerUnderConcurrency(t *testing.T, ctx context.Context, fixture LifecycleUpdateFixture) { - t.Helper() - - const racers = 16 - - id := fixture.IssuePrefix + "-lup-singlewinner" - seedLifecycleUpdateIssue(t, ctx, fixture, lifecycleUpdateIssue(id)) - - seed := lifecycleUpdateRow(t, ctx, fixture, id) - racedToken := seed.RowVersion - - // Priority is a small enum (0-4, see validation.ValidatePriority); each - // racer's own index would overflow it past four racers, so the index rides - // in Notes instead (free-text, nothing else writes it in this test) and - // priority merely alternates, giving the final-state check two independent - // columns that must agree on which one racer actually landed. - var wg sync.WaitGroup - won := make([]bool, racers) - errs := make([]error, racers) - wg.Add(racers) - for i := 0; i < racers; i++ { - i := i - go func() { - defer wg.Done() - version := racedToken - result, err := fixture.Lifecycle.Update(ctx, publicops.UpdateRequest{ - Actor: "racer", - IssueID: id, - Patch: publicops.IssuePatch{ - Priority: publicops.Field[int]{Set: true, Value: i % 5}, - Notes: publicops.Field[string]{Set: true, Value: fmt.Sprintf("racer-%d", i)}, - }, - ExpectedVersion: &version, - }) - errs[i] = err - won[i] = err == nil && result.Changed - }() - } - wg.Wait() - - winners := 0 - winner := -1 - for i, win := range won { - if win { - winners++ - winner = i - continue - } - if !errors.Is(errs[i], storage.ErrVersionMismatch) { - t.Errorf("racer %d: err = %v, want nil (won) or ErrVersionMismatch (lost the race)", i, errs[i]) - } - } - if winners != 1 { - t.Fatalf("%d racers shared one --if-revision token %d; got %d winners, want exactly 1 (errs=%v)", - racers, racedToken, winners, errs) - } - - final := lifecycleUpdateRow(t, ctx, fixture, id) - wantNotes := fmt.Sprintf("racer-%d", winner) - if final.Notes != wantNotes { - t.Errorf("final notes = %q, want the single winner's %q — a torn or lost write", final.Notes, wantNotes) - } - if final.Priority != winner%5 { - t.Errorf("final priority = %d, want the single winner's %d — a torn or lost write", final.Priority, winner%5) - } - if final.RowVersion == racedToken { - t.Errorf("final row version = %d, want it to have advanced past the raced token %d", final.RowVersion, racedToken) - } -} - -// RunLifecycleUpdateExpectedVersionSingleWinnerWithDisjointColumnsUnderConcurrency -// is RunLifecycleUpdateExpectedVersionSingleWinnerUnderConcurrency's sibling, -// and the reason that one is not enough on its own (mc-zndi7.76, gap 1). -// -// Every racer there writes the SAME two cells (Priority and Notes), so a -// backend whose storage layer auto-merges disjoint-cell changes from a common -// base — Dolt's native behavior — could pass that test on native cell-level -// conflict detection ALONE, with the ExpectedVersion/row_lock compare-and-swap -// doing nothing at all: sixteen racers colliding on one cell look identical to -// sixteen racers colliding on one ROW VERSION. A hand-applied mutant that -// reminted row_lock to an unconditional fresh random value on every write -// (rather than deriving it from, and checking it against, ExpectedVersion) -// still passed the shared-cell test on this suite's in-tree backends 3 of 3 -// runs each, and only showed its true colors — 3 to 7 winners per round — on a -// real Dolt server CLI race with disjoint-column patches, which is the shape -// this case restores. -// -// Each racer here writes a column NO OTHER RACER TOUCHES, so Dolt's per-cell -// auto-merge cannot provide false mutual exclusion for any pair of them: -// whatever excludes all but one racer here has to be the row_lock/ -// ExpectedVersion compare-and-swap itself, not an accident of which cells -// collide. -func RunLifecycleUpdateExpectedVersionSingleWinnerWithDisjointColumnsUnderConcurrency(t *testing.T, ctx context.Context, fixture LifecycleUpdateFixture) { - t.Helper() - - type disjointField struct { - name string - apply func(*publicops.IssuePatch, string) - read func(*types.Issue) string - } - fields := []disjointField{ - {"Description", func(p *publicops.IssuePatch, v string) { p.Description = publicops.Field[string]{Set: true, Value: v} }, func(i *types.Issue) string { return i.Description }}, - {"Design", func(p *publicops.IssuePatch, v string) { p.Design = publicops.Field[string]{Set: true, Value: v} }, func(i *types.Issue) string { return i.Design }}, - {"AcceptanceCriteria", func(p *publicops.IssuePatch, v string) { - p.AcceptanceCriteria = publicops.Field[string]{Set: true, Value: v} - }, func(i *types.Issue) string { return i.AcceptanceCriteria }}, - {"Notes", func(p *publicops.IssuePatch, v string) { p.Notes = publicops.Field[string]{Set: true, Value: v} }, func(i *types.Issue) string { return i.Notes }}, - {"SpecID", func(p *publicops.IssuePatch, v string) { p.SpecID = publicops.Field[string]{Set: true, Value: v} }, func(i *types.Issue) string { return i.SpecID }}, - {"AwaitID", func(p *publicops.IssuePatch, v string) { p.AwaitID = publicops.Field[string]{Set: true, Value: v} }, func(i *types.Issue) string { return i.AwaitID }}, - {"Owner", func(p *publicops.IssuePatch, v string) { p.Owner = publicops.Field[string]{Set: true, Value: v} }, func(i *types.Issue) string { return i.Owner }}, - // Assignee rides the same Update/ExpectedVersion machinery `bd assign` - // dispatches onto (cmd/bd/mutate_proxied_server.go's proxiedAssign, and - // its direct-route twin), so a disjoint racer on this column is this - // suite's single-winner coverage for assign (mc-zndi7.76, gap 3): there - // is no separate Assign method on Lifecycle to race independently. - {"Assignee", func(p *publicops.IssuePatch, v string) { p.Assignee = publicops.Field[string]{Set: true, Value: v} }, func(i *types.Issue) string { return i.Assignee }}, - } - racers := len(fields) - - id := fixture.IssuePrefix + "-lup-singlewinner-disjoint" - seedLifecycleUpdateIssue(t, ctx, fixture, lifecycleUpdateIssue(id)) - - seed := lifecycleUpdateRow(t, ctx, fixture, id) - racedToken := seed.RowVersion - - var wg sync.WaitGroup - won := make([]bool, racers) - errs := make([]error, racers) - wg.Add(racers) - for i := 0; i < racers; i++ { - i := i - go func() { - defer wg.Done() - version := racedToken - var patch publicops.IssuePatch - fields[i].apply(&patch, fmt.Sprintf("racer-%d-%s", i, fields[i].name)) - result, err := fixture.Lifecycle.Update(ctx, publicops.UpdateRequest{ - Actor: "racer", - IssueID: id, - Patch: patch, - ExpectedVersion: &version, - }) - errs[i] = err - won[i] = err == nil && result.Changed - }() - } - wg.Wait() - - winners := 0 - winner := -1 - for i, win := range won { - if win { - winners++ - winner = i - continue - } - if !errors.Is(errs[i], storage.ErrVersionMismatch) { - t.Errorf("racer %d (%s): err = %v, want nil (won) or ErrVersionMismatch (lost the race)", i, fields[i].name, errs[i]) - } - } - if winners != 1 { - t.Fatalf("%d racers on disjoint columns shared one --if-revision token %d; got %d winners, want exactly 1 (errs=%v)", - racers, racedToken, winners, errs) - } - - final := lifecycleUpdateRow(t, ctx, fixture, id) - wantValue := fmt.Sprintf("racer-%d-%s", winner, fields[winner].name) - for i, f := range fields { - got := f.read(final) - if i == winner { - if got != wantValue { - t.Errorf("final %s = %q, want the single winner's %q — a torn or lost write", f.name, got, wantValue) - } - continue - } - if got != "" { - t.Errorf("final %s = %q, want empty — a disjoint loser's write was not supposed to apply (false mutual exclusion from native cell-merge, not the version guard)", f.name, got) - } - } - if final.RowVersion == racedToken { - t.Errorf("final row version = %d, want it to have advanced past the raced token %d", final.RowVersion, racedToken) - } -} - -// RunLifecycleCloseExpectedVersionSingleWinnerUnderConcurrency is -// RunLifecycleUpdateExpectedVersionSingleWinnerUnderConcurrency's Close twin -// (mc-zndi7.76, gap 3): CloseRequest.ExpectedVersion rides the same row_lock -// compare-and-swap Update's does (CloseRequest.ExpectedVersion's doc), and -// until now nothing proved that under real concurrency for Close specifically. -// N racers close the SAME open issue racing the SAME pre-close token; exactly -// one may apply its close, every other racer must see ErrVersionMismatch (not -// the idempotent-reclose no-op — the token is stale by the time it would -// observe that), and the row left behind must be exactly the single winner's. -func RunLifecycleCloseExpectedVersionSingleWinnerUnderConcurrency(t *testing.T, ctx context.Context, fixture LifecycleUpdateFixture) { - t.Helper() - - const racers = 16 - - id := fixture.IssuePrefix + "-lup-close-singlewinner" - seedLifecycleUpdateIssue(t, ctx, fixture, lifecycleUpdateIssue(id)) - - seed := lifecycleUpdateRow(t, ctx, fixture, id) - racedToken := seed.RowVersion - - var wg sync.WaitGroup - won := make([]bool, racers) - errs := make([]error, racers) - wg.Add(racers) - for i := 0; i < racers; i++ { - i := i - go func() { - defer wg.Done() - version := racedToken - result, err := fixture.Lifecycle.Close(ctx, publicops.CloseRequest{ - Actor: "racer", - IssueID: id, - Reason: fmt.Sprintf("racer-%d", i), - ExpectedVersion: &version, - }) - errs[i] = err - won[i] = err == nil && result.Changed - }() - } - wg.Wait() - - winners := 0 - winner := -1 - for i, win := range won { - if win { - winners++ - winner = i - continue - } - if !errors.Is(errs[i], storage.ErrVersionMismatch) { - t.Errorf("racer %d: err = %v, want nil (won) or ErrVersionMismatch (lost the race)", i, errs[i]) - } - } - if winners != 1 { - t.Fatalf("%d racers shared one close --if-revision token %d; got %d winners, want exactly 1 (errs=%v)", - racers, racedToken, winners, errs) - } - - final := lifecycleUpdateRow(t, ctx, fixture, id) - if final.Status != types.StatusClosed { - t.Errorf("final status = %q, want closed", final.Status) - } - wantReason := fmt.Sprintf("racer-%d", winner) - if final.CloseReason != wantReason { - t.Errorf("final close reason = %q, want the single winner's %q — a torn or lost write", final.CloseReason, wantReason) - } - if final.RowVersion == racedToken { - t.Errorf("final row version = %d, want it to have advanced past the raced token %d", final.RowVersion, racedToken) - } -} - -// RunLifecycleUpdateExpectedVersionSingleWinnerAcrossUpdateAndCloseUnderConcurrency -// is this suite's mixed-verb single-winner case (mc-zndi7.76, gap 3): Update -// and Close share one row_lock compare-and-swap, so a token raced across BOTH -// verbs at once must still admit exactly one winner, whichever verb it turns -// out to be — an exclusion mechanism keyed to the row rather than to a given -// verb's own code path. Delete is deliberately NOT a racer here: delete's -// production code does not remint row_lock on a deletion (nothing is left to -// remint), so it does not share this exclusion at all today. That is a real, -// separately tracked bug (the sibling delete bead, mc-zndi7.73) and not a gap -// in this test — folding Delete in here would either mask the bug behind -// Update/Close's real exclusion winning the race, or make this case flake -// red on current main, neither of which belongs in a test pinning what DOES -// work. -func RunLifecycleUpdateExpectedVersionSingleWinnerAcrossUpdateAndCloseUnderConcurrency(t *testing.T, ctx context.Context, fixture LifecycleUpdateFixture) { - t.Helper() - - const racers = 8 // four Update racers, four Close racers, interleaved. - - id := fixture.IssuePrefix + "-lup-mixed-singlewinner" - seedLifecycleUpdateIssue(t, ctx, fixture, lifecycleUpdateIssue(id)) - - seed := lifecycleUpdateRow(t, ctx, fixture, id) - racedToken := seed.RowVersion - - type outcome struct { - kind string - changed bool - err error - } - outcomes := make([]outcome, racers) - var wg sync.WaitGroup - wg.Add(racers) - for i := 0; i < racers; i++ { - i := i - go func() { - defer wg.Done() - version := racedToken - if i%2 == 0 { - result, err := fixture.Lifecycle.Update(ctx, publicops.UpdateRequest{ - Actor: "racer", - IssueID: id, - Patch: publicops.IssuePatch{ - Owner: publicops.Field[string]{Set: true, Value: fmt.Sprintf("update-racer-%d", i)}, - }, - ExpectedVersion: &version, - }) - outcomes[i] = outcome{kind: "update", changed: err == nil && result.Changed, err: err} - return - } - result, err := fixture.Lifecycle.Close(ctx, publicops.CloseRequest{ - Actor: "racer", - IssueID: id, - Reason: fmt.Sprintf("close-racer-%d", i), - ExpectedVersion: &version, - }) - outcomes[i] = outcome{kind: "close", changed: err == nil && result.Changed, err: err} - }() - } - wg.Wait() - - winners := 0 - winner := -1 - for i, o := range outcomes { - if o.changed { - winners++ - winner = i - continue - } - if !errors.Is(o.err, storage.ErrVersionMismatch) { - t.Errorf("racer %d (%s): err = %v, want nil (won) or ErrVersionMismatch (lost the race)", i, o.kind, o.err) - } - } - if winners != 1 { - t.Fatalf("%d update/close racers shared one --if-revision token %d; got %d winners, want exactly 1 (outcomes=%+v)", - racers, racedToken, winners, outcomes) - } - - final := lifecycleUpdateRow(t, ctx, fixture, id) - switch outcomes[winner].kind { - case "update": - wantOwner := fmt.Sprintf("update-racer-%d", winner) - if final.Owner != wantOwner { - t.Errorf("final owner = %q, want the single winner's %q — a torn or lost write", final.Owner, wantOwner) - } - if final.Status == types.StatusClosed { - t.Errorf("the winning racer was an update, not a close; final status must not be closed") - } - case "close": - if final.Status != types.StatusClosed { - t.Errorf("final status = %q, want closed — the winning racer was a close", final.Status) - } - wantReason := fmt.Sprintf("close-racer-%d", winner) - if final.CloseReason != wantReason { - t.Errorf("final close reason = %q, want the single winner's %q — a torn or lost write", final.CloseReason, wantReason) - } - } - if final.RowVersion == racedToken { - t.Errorf("final row version = %d, want it to have advanced past the raced token %d", final.RowVersion, racedToken) - } -} - // RunLifecycleUpdateConditionalGuardAcceptsRespelledAssignee pins the // ga-5ksp5 fix beside RunLifecycleUpdateConditionalGuardsGateOrdinaryEdits // above rather than inside it: that test's later "order-dependent composition" @@ -1920,10 +1538,10 @@ func RunLifecycleUpdateParentIDReplacesEveryParent(t *testing.T, ctx context.Con // RunLifecycleUpdatePersistentPreservesUnversionedClass pins the half of the // Persistence clause nobody else asserts: "Persistent preserves an existing -// durable unversioned class" (issueops/issueops.go:149). The refusal beside -// it — an unversioned row cannot be demoted to a wisp mode — is -// RunLifecycleUpdateWispModesRefuseAnUnversionedRow; this clause says the legal -// direction is a no-op that does NOT normalize the row into versioned storage. +// durable unversioned class" (issueops/issueops.go:135-136). The refusal beside +// it — an unversioned row cannot be demoted to a wisp mode — is pinned +// elsewhere; this clause says the legal direction is a no-op that does NOT +// normalize the row into versioned storage. // // WHAT A PERSISTENCE RESTATEMENT MUST NOT DO is where the two observations come // in. A persistence MOVE deletes the row from one plane and re-inserts it into @@ -1964,90 +1582,6 @@ func RunLifecycleUpdatePersistentPreservesUnversionedClass(t *testing.T, ctx con assertLifecycleUpdateWispAbsent(t, ctx, fixture, id, "after the persistent restatement") } -// RunLifecycleUpdateWispModesRefuseAnUnversionedRow pins the refusal beside -// that clause: Ephemeral and NoHistory "select wisp retention modes, never -// durable unversioned storage" (issueops/issueops.go:150-151), so an -// unversioned row asked for either is refused. The refusal is the request's, -// a deterministic validation failure, so it matches ErrValidation on every -// leg — over http that is the 400 naming the patch rather than a 500 that -// drops the reason and alerts the operator — and it leaves the row where it -// was: durable, still unversioned, and absent from the wisp plane. -func RunLifecycleUpdateWispModesRefuseAnUnversionedRow(t *testing.T, ctx context.Context, fixture LifecycleUpdateFixture) { - t.Helper() - - id := fixture.IssuePrefix + "-lup-unversioned-wisp" - seeded := lifecycleUpdateIssue(id) - seeded.StorageClass = types.StorageClassUnversioned - seedLifecycleUpdateIssue(t, ctx, fixture, seeded) - - for _, mode := range []publicops.PersistenceMode{publicops.PersistenceModeEphemeral, publicops.PersistenceModeNoHistory} { - label := "after the refused " + string(mode) + " move" - before := lifecycleUpdateRow(t, ctx, fixture, id) - if _, err := fixture.Lifecycle.Update(ctx, publicops.UpdateRequest{Actor: "writer", IssueID: id, Patch: publicops.IssuePatch{ - Persistence: publicops.Field[publicops.PersistenceMode]{Set: true, Value: mode}, - }}); !errors.Is(err, storage.ErrValidation) { - t.Fatalf("move unversioned %s to %s: error = %v, want ErrValidation", id, mode, err) - } - assertLifecycleUpdateRowUnchanged(t, ctx, fixture, id, label, before) - after := lifecycleUpdateRow(t, ctx, fixture, id) - if after.Ephemeral || after.NoHistory || after.StorageClass != types.StorageClassUnversioned { - t.Errorf("%s %s = {ephemeral %v, no-history %v, class %q}, want the durable unversioned row it was", - id, label, after.Ephemeral, after.NoHistory, after.StorageClass) - } - assertLifecycleUpdateWispAbsent(t, ctx, fixture, id, label) - } -} - -// RunLifecycleUpdatePersistentPromotesAWisp pins the move `bd promote` makes -// (issueops.Promote): Persistence = persistent on an ephemeral wisp is a -// MUTATION that leaves the row on the durable plane under the same id, with -// its labels carried across and nothing left behind on the wisp plane. A -// second identical request is the no-op the "same current mode" clause -// promises, which is how Promote tells a lost race from a promotion. -func RunLifecycleUpdatePersistentPromotesAWisp(t *testing.T, ctx context.Context, fixture LifecycleUpdateFixture) { - t.Helper() - if fixture.CreateWisp == nil { - t.Skip("fixture has no CreateWisp: this backend has no wisp plane to promote from") - } - - id := fixture.IssuePrefix + "-lup-promote" - seedLifecycleUpdateWisp(t, ctx, fixture, lifecycleUpdateIssue(id, "keep-me")) - if got := lifecycleUpdateRow(t, ctx, fixture, id); !got.Ephemeral { - t.Fatalf("seeded %s is not ephemeral; the case has nothing to promote", id) - } - - promote := publicops.UpdateRequest{Actor: "writer", IssueID: id, Patch: publicops.IssuePatch{ - Persistence: publicops.Field[publicops.PersistenceMode]{Set: true, Value: publicops.PersistenceModePersistent}, - }} - promoted, err := fixture.Lifecycle.Update(ctx, promote) - if err != nil { - t.Fatalf("promote %s: %v", id, err) - } - if !promoted.Changed { - t.Errorf("promoting wisp %s reported Changed = false, want a mutation", id) - } - if promoted.Issue == nil || promoted.Issue.ID != id || promoted.Issue.Ephemeral { - t.Errorf("promote result issue = %+v, want %s on the durable plane", promoted.Issue, id) - } - - after := lifecycleUpdateRow(t, ctx, fixture, id) - if after.Ephemeral || after.NoHistory { - t.Errorf("%s came back {ephemeral %v, no-history %v} after the promotion, want a durable-plane row", id, after.Ephemeral, after.NoHistory) - } - if !slices.Contains(after.Labels, "keep-me") { - t.Errorf("%s labels after the promotion = %v, want the wisp's label carried across", id, after.Labels) - } - assertLifecycleUpdateWispAbsent(t, ctx, fixture, id, "after the promotion") - - again, err := fixture.Lifecycle.Update(ctx, promote) - if err != nil { - t.Fatalf("re-promote %s: %v", id, err) - } - if again.Changed { - t.Errorf("re-promoting durable %s reported Changed = true, want the same-mode no-op", id) - } -} - // RunLifecycleUpdateProvenanceLabelsHistory pins UpdateRequest.Provenance // against the history the backend actually writes // (issueops/issueops.go:261-270): the entry reads as the caller's own string, @@ -2604,134 +2138,3 @@ func lifecycleUpdateHasEdge(dependencies []*types.Dependency, wantTarget string) } return false } - -// RunLifecycleUpdateRefusesATemplate pins Lifecycle.Update's template guard: -// templates are read-only, so EVERY update naming one refuses with -// *TemplateReadOnlyError — a field edit, a label edit, a claim, a status -// crossing — whatever force flags ride along, and nothing is written. It is the -// guard `bd update` used to apply as a CLI pre-read, which left bd serve and -// library callers editing templates bd refused. -// -// The refusal is asserted at the level a caller acts on — the typed error, its -// sentinel and the sentence bd prints, read off the typed error because a -// served refusal wraps it — and the row is read back to prove nothing moved. -// Two controls: the same patches land on an ordinary sibling, so the guard is -// not refusing the patch shapes; and a stale ExpectedVersion on the template -// still reports the mismatch, so the guard runs after the compare-and-set -// preconditions like every other update fence. -func RunLifecycleUpdateRefusesATemplate(t *testing.T, ctx context.Context, fixture LifecycleUpdateFixture) { - t.Helper() - template := fixture.IssuePrefix + "-lut-template" - sibling := fixture.IssuePrefix + "-lut-sibling" - for _, issue := range []*types.Issue{ - {ID: template, IsTemplate: true}, - {ID: sibling}, - } { - issue.Title, issue.Status, issue.Priority, issue.IssueType = issue.ID, types.StatusOpen, 2, types.TypeTask - if err := fixture.CreateIssue(ctx, issue, "seed"); err != nil { - t.Fatalf("seed %s: %v", issue.ID, err) - } - } - before, err := fixture.GetIssue(ctx, template) - if err != nil { - t.Fatalf("read seeded template %s: %v", template, err) - } - - requests := []struct { - name string - request publicops.UpdateRequest - }{ - {name: "field edit", request: publicops.UpdateRequest{ - Patch: publicops.IssuePatch{Title: publicops.Field[string]{Set: true, Value: "edited"}}, - }}, - {name: "label edit", request: publicops.UpdateRequest{ - Patch: publicops.IssuePatch{Labels: publicops.LabelPatch{Add: []string{"lut-tag"}}}, - }}, - {name: "claim", request: publicops.UpdateRequest{Claim: true}}, - {name: "forced status crossing", request: publicops.UpdateRequest{ - Patch: publicops.IssuePatch{Status: publicops.Field[publicops.Status]{Set: true, Value: types.StatusClosed}}, - ForceClosePolicy: true, - }}, - {name: "forced assignee transfer", request: publicops.UpdateRequest{ - Patch: publicops.IssuePatch{Assignee: publicops.Field[string]{Set: true, Value: "someone"}}, - ForceAssigneeTransfer: true, - }}, - } - for _, tc := range requests { - request := tc.request - request.Actor, request.IssueID = "writer", template - _, err := fixture.Lifecycle.Update(ctx, request) - var refusal *publicops.TemplateReadOnlyError - if !errors.As(err, &refusal) { - t.Fatalf("%s on template %s: err = %v, want *TemplateReadOnlyError", tc.name, template, err) - } - if refusal.IssueID != template || !errors.Is(err, publicops.ErrTemplateReadOnly) { - t.Errorf("%s refusal = %#v (%v), want IssueID %q matching ErrTemplateReadOnly", tc.name, refusal, err, template) - } - if want := "cannot modify template " + template + ": templates are read-only; use 'bd mol pour' to create a work item"; refusal.Error() != want { - t.Errorf("%s refusal reads %q, want %q", tc.name, refusal.Error(), want) - } - after, err := fixture.GetIssue(ctx, template) - if err != nil { - t.Fatalf("read template %s after the %s refusal: %v", template, tc.name, err) - } - if after.RowVersion != before.RowVersion || after.Title != before.Title || after.Status != before.Status || - after.Assignee != before.Assignee || len(after.Labels) != len(before.Labels) { - t.Errorf("template %s after the %s refusal = %+v, want it unchanged from %+v", template, tc.name, after, before) - } - - control := tc.request - control.Actor, control.IssueID = "writer", sibling - if _, err := fixture.Lifecycle.Update(ctx, control); err != nil { - t.Errorf("%s on the ordinary sibling %s: %v, want it to land", tc.name, sibling, err) - } - } - - stale := before.RowVersion + 1 - _, err = fixture.Lifecycle.Update(ctx, publicops.UpdateRequest{ - Actor: "writer", IssueID: template, ExpectedVersion: &stale, - Patch: publicops.IssuePatch{Title: publicops.Field[string]{Set: true, Value: "edited"}}, - }) - if !errors.Is(err, publicops.ErrVersionMismatch) { - t.Errorf("stale-guarded update of template %s: err = %v, want ErrVersionMismatch ahead of the template guard", template, err) - } -} - -// RunLifecycleUpdateAllowTemplateEditsATemplate pins the template guard's one -// stand-down: an update with AllowTemplate — what bd label and bd set-state -// send, since they have always edited templates — lands on a template, and -// only that request is waived (the same patch without it still refuses). -func RunLifecycleUpdateAllowTemplateEditsATemplate(t *testing.T, ctx context.Context, fixture LifecycleUpdateFixture) { - t.Helper() - template := fixture.IssuePrefix + "-lua-template" - issue := &types.Issue{ID: template, Title: template, Status: types.StatusOpen, Priority: 2, IssueType: types.TypeTask, IsTemplate: true} - if err := fixture.CreateIssue(ctx, issue, "seed"); err != nil { - t.Fatalf("seed %s: %v", template, err) - } - - result, err := fixture.Lifecycle.Update(ctx, publicops.UpdateRequest{ - Actor: "writer", IssueID: template, AllowTemplate: true, - Patch: publicops.IssuePatch{Labels: publicops.LabelPatch{Add: []string{"lua-tag"}}}, - }) - if err != nil { - t.Fatalf("AllowTemplate label edit on template %s: %v, want it to land", template, err) - } - if !result.Changed || result.Issue == nil || !lifecycleUpdateHasLabel(result.Issue.Labels, "lua-tag") { - t.Errorf("AllowTemplate label edit on template %s = %+v, want a change carrying lua-tag", template, result) - } - after, err := fixture.GetIssue(ctx, template) - if err != nil { - t.Fatalf("read template %s: %v", template, err) - } - if !after.IsTemplate { - t.Errorf("template %s is no longer a template after an AllowTemplate edit", template) - } - - _, err = fixture.Lifecycle.Update(ctx, publicops.UpdateRequest{ - Actor: "writer", IssueID: template, - Patch: publicops.IssuePatch{Labels: publicops.LabelPatch{Add: []string{"lua-other"}}}, - }) - if !errors.Is(err, publicops.ErrTemplateReadOnly) { - t.Errorf("the same edit without AllowTemplate: err = %v, want ErrTemplateReadOnly", err) - } -} diff --git a/backend/conformance/lifecycle_update_gate_contract.go b/backend/conformance/lifecycle_update_gate_contract.go deleted file mode 100644 index 06c730625..000000000 --- a/backend/conformance/lifecycle_update_gate_contract.go +++ /dev/null @@ -1,133 +0,0 @@ -package conformance - -import ( - "context" - "testing" - "time" - - publicops "github.com/steveyegge/beads/issueops" -) - -// The patch half of the gate-member clauses; see lifecycle_create_gate_contract.go. - -// RunLifecycleUpdatePatchesTheGateMembers pins IssuePatch.AwaitID and -// IssuePatch.Waiters: a set value lands and is answered on the result, waiters -// is a WHOLE-VALUE replacement stored as given (duplicates kept), a same-value -// patch is a no-op, a clear (empty await_id, nil waiters) empties both, and a -// second clear is a no-op. await_type and timeout are not patch members, so -// they ride through every edit untouched. The row is a task: no gate-type-only -// rule. -func RunLifecycleUpdatePatchesTheGateMembers(t *testing.T, ctx context.Context, fixture LifecycleUpdateFixture) { - t.Helper() - - id := fixture.IssuePrefix + "-lug-gate" - seeded := lifecycleUpdateIssue(id) - seeded.AwaitType = "timer" - seeded.AwaitID = "seeded-await" - seeded.Timeout = time.Minute - seeded.Waiters = []string{"seeded"} - seedLifecycleUpdateIssue(t, ctx, fixture, seeded) - - patch := publicops.IssuePatch{ - AwaitID: publicops.Field[string]{Set: true, Value: "run-42"}, - Waiters: publicops.Field[[]string]{Set: true, Value: []string{"w1", "w2", "w1"}}, - } - wantWaiters := []string{"w1", "w2", "w1"} - updated, err := fixture.Lifecycle.Update(ctx, publicops.UpdateRequest{Actor: "writer", IssueID: id, Patch: patch}) - if err != nil { - t.Fatalf("patch the gate members of %s: %v", id, err) - } - if !updated.Changed { - t.Errorf("patching the gate members of %s reported Changed = false, want a committed edit", id) - } - assertGateMembers(t, id, "in the update result", updated.Issue, "timer", "run-42", time.Minute, wantWaiters) - assertGateMembers(t, id, "in the stored row", lifecycleUpdateRow(t, ctx, fixture, id), "timer", "run-42", time.Minute, wantWaiters) - - after := lifecycleUpdateRow(t, ctx, fixture, id) - again, err := fixture.Lifecycle.Update(ctx, publicops.UpdateRequest{Actor: "writer", IssueID: id, Patch: patch}) - if err != nil { - t.Fatalf("re-apply the same gate patch to %s: %v", id, err) - } - if again.Changed { - t.Errorf("re-applying the same gate patch to %s reported Changed = true, want false", id) - } - assertLifecycleUpdateRowUnchanged(t, ctx, fixture, id, "after the same-value gate patch", after) - - clear := publicops.UpdateRequest{Actor: "writer", IssueID: id, Patch: publicops.IssuePatch{ - AwaitID: publicops.Field[string]{Set: true}, - Waiters: publicops.Field[[]string]{Set: true}, - }} - cleared, err := fixture.Lifecycle.Update(ctx, clear) - if err != nil { - t.Fatalf("clear the gate members of %s: %v", id, err) - } - if !cleared.Changed { - t.Errorf("clearing the gate members of %s reported Changed = false, want a committed edit", id) - } - assertGateMembers(t, id, "in the clear's result", cleared.Issue, "timer", "", time.Minute, nil) - assertGateMembers(t, id, "in the cleared row", lifecycleUpdateRow(t, ctx, fixture, id), "timer", "", time.Minute, nil) - - afterClear := lifecycleUpdateRow(t, ctx, fixture, id) - reclear, err := fixture.Lifecycle.Update(ctx, clear) - if err != nil { - t.Fatalf("re-clear the gate members of %s: %v", id, err) - } - if reclear.Changed { - t.Errorf("re-clearing the already-empty gate members of %s reported Changed = true, want false", id) - } - assertLifecycleUpdateRowUnchanged(t, ctx, fixture, id, "after the re-clear", afterClear) - - // An explicitly empty list clears exactly as a nil one does. - if _, err := fixture.Lifecycle.Update(ctx, publicops.UpdateRequest{Actor: "writer", IssueID: id, Patch: publicops.IssuePatch{ - Waiters: publicops.Field[[]string]{Set: true, Value: []string{"back"}}, - }}); err != nil { - t.Fatalf("re-set the waiters of %s: %v", id, err) - } - if _, err := fixture.Lifecycle.Update(ctx, publicops.UpdateRequest{Actor: "writer", IssueID: id, Patch: publicops.IssuePatch{ - Waiters: publicops.Field[[]string]{Set: true, Value: []string{}}, - }}); err != nil { - t.Fatalf("clear the waiters of %s with an empty list: %v", id, err) - } - assertGateMembers(t, id, "after the empty-list clear", lifecycleUpdateRow(t, ctx, fixture, id), "timer", "", time.Minute, nil) -} - -// RunLifecycleUpdateBoundsTheGateMembers pins the patch half of the -// GateFieldError bounds from both sides: at-bound await_id and waiters land -// whole, one past either is refused as ErrValidation naming its member, and -// the refusal writes nothing — not the gate member, and not an ordinary member -// riding in the same patch. -func RunLifecycleUpdateBoundsTheGateMembers(t *testing.T, ctx context.Context, fixture LifecycleUpdateFixture) { - t.Helper() - - id := fixture.IssuePrefix + "-lug-bounds" - seedLifecycleUpdateIssue(t, ctx, fixture, lifecycleUpdateIssue(id)) - - if _, err := fixture.Lifecycle.Update(ctx, publicops.UpdateRequest{Actor: "writer", IssueID: id, Patch: publicops.IssuePatch{ - AwaitID: publicops.Field[string]{Set: true, Value: gateFieldAtBoundAwaitID}, - Waiters: publicops.Field[[]string]{Set: true, Value: gateFieldAtBoundWaiters()}, - }}); err != nil { - t.Fatalf("patch %s with await_id and waiters at their bounds: %v", id, err) - } - assertGateMembers(t, id, "in the stored row", lifecycleUpdateRow(t, ctx, fixture, id), "", gateFieldAtBoundAwaitID, 0, gateFieldAtBoundWaiters()) - - before := lifecycleUpdateRow(t, ctx, fixture, id) - for _, test := range []struct { - name string - field string - patch publicops.IssuePatch - }{ - {"await_id", publicops.GateFieldAwaitID, publicops.IssuePatch{ - Title: publicops.Field[string]{Set: true, Value: "must not land"}, - AwaitID: publicops.Field[string]{Set: true, Value: gateFieldAtBoundAwaitID + "x"}, - }}, - {"waiters", publicops.GateFieldWaiters, publicops.IssuePatch{ - Title: publicops.Field[string]{Set: true, Value: "must not land"}, - Waiters: publicops.Field[[]string]{Set: true, Value: gateFieldOverBoundWaiters()}, - }}, - } { - _, err := fixture.Lifecycle.Update(ctx, publicops.UpdateRequest{Actor: "writer", IssueID: id, Patch: test.patch}) - assertGateFieldError(t, err, "patch with "+test.name+" past its bound", test.field) - assertLifecycleUpdateRowUnchanged(t, ctx, fixture, id, "after the refused "+test.name, before) - assertGateMembers(t, id, "after the refused "+test.name, lifecycleUpdateRow(t, ctx, fixture, id), "", gateFieldAtBoundAwaitID, 0, gateFieldAtBoundWaiters()) - } -} diff --git a/backend/conformance/memories_contract.go b/backend/conformance/memories_contract.go index dc45191f4..c7b33221c 100644 --- a/backend/conformance/memories_contract.go +++ b/backend/conformance/memories_contract.go @@ -233,11 +233,14 @@ func RunMemoriesRememberWithExplicitKeyStoresVerbatim(t *testing.T, ctx context. } // RunMemoriesRememberReplacesAndReportsIt pins -// memoryops.RememberResult.Replaced against row presence. +// memoryops.RememberResult.Replaced, including the leg that makes it different +// from RecallResult.Found. // // Three legs: a first write reports false, a second reports true and REPLACES -// rather than appends, and a write over an out-of-band EMPTY row reports true. -// The third is the one a body computing `previous != ""` gets wrong. +// rather than appends, and a write over an out-of-band EMPTY row reports true +// even though a Recall of that key would have reported Found false. The third +// is the divergence the leaf doc states — Replaced is about the ROW — and it is +// the one a body computing `previous != ""` gets wrong. func RunMemoriesRememberReplacesAndReportsIt(t *testing.T, ctx context.Context, fixture MemoriesFixture) { t.Helper() neighbors := seedMemoriesAllFourClasses(t, ctx, fixture) @@ -259,7 +262,7 @@ func RunMemoriesRememberReplacesAndReportsIt(t *testing.T, ctx context.Context, if err := fixture.SetConfig(ctx, "kv.memory."+emptied, ""); err != nil { t.Fatalf("seed an empty memory row out of band: %v", err) } - assertMemoriesRecall(t, ctx, fixture, emptied, "", true) + assertMemoriesRecall(t, ctx, fixture, emptied, "", false) overEmpty := rememberMemory(t, ctx, fixture, memoryops.RememberRequest{Key: emptied, Content: "now it has content"}) if !overEmpty.Replaced { t.Fatalf("Remember over a row stored EMPTY reported Replaced false: Replaced is about the row, " + @@ -371,8 +374,8 @@ func RunMemoriesRecallAnswersTheStoredValue(t *testing.T, ctx context.Context, f } // RunMemoriesRecallReportsAMissAsNotFoundNotAnError pins the decision recorded -// in memoryops/errors.go: absence is a result callers inspect, not an -// operational error they must classify. +// in memoryops/errors.go: there is no ErrNotFound on this role, because the +// seam beneath it cannot see the difference the error would claim. func RunMemoriesRecallReportsAMissAsNotFoundNotAnError(t *testing.T, ctx context.Context, fixture MemoriesFixture) { t.Helper() seedMemoriesAllFourClasses(t, ctx, fixture) @@ -398,70 +401,33 @@ func RunMemoriesRecallReportsAMissAsNotFoundNotAnError(t *testing.T, ctx context } } -// RunMemoriesEmptyRowIsPresentUntilForgotten pins presence to the ROW rather -// than to whether its value is non-empty. No front door creates an empty -// memory, but out-of-band config writes and older workspaces can contain one, -// and it must remain addressable through Recall and Forget. +// RunMemoriesRecallConflatesStoredEmptyWithAbsent pins +// memoryops.RecallResult.Found's central claim, and the one asymmetry that lets +// a caller see past it: List enumerates the key, because the ROW exists. // -// The second Forget is the retry leg: after the first call deletes the row, it -// reports a genuine miss and records no further history. -func RunMemoriesEmptyRowIsPresentUntilForgotten(t *testing.T, ctx context.Context, fixture MemoriesFixture) { +// That is the same same-answer-different-row shape the settings contract pins +// for `bd config get`, and stating it is what keeps a backend from "fixing" it +// on one leg and making the answer depend on the route. +func RunMemoriesRecallConflatesStoredEmptyWithAbsent(t *testing.T, ctx context.Context, fixture MemoriesFixture) { t.Helper() - neighbors := seedMemoriesAllFourClasses(t, ctx, fixture) + seedMemoriesAllFourClasses(t, ctx, fixture) emptied := fixture.IssuePrefix + "-stored-empty" never := fixture.IssuePrefix + "-never-stored" if err := fixture.SetConfig(ctx, "kv.memory."+emptied, ""); err != nil { t.Fatalf("seed an empty memory row out of band: %v", err) } - assertMemoriesRecall(t, ctx, fixture, emptied, "", true) + assertMemoriesRecall(t, ctx, fixture, emptied, "", false) assertMemoriesRecall(t, ctx, fixture, never, "", false) plane := listMemories(t, ctx, fixture, "") if value, ok := plane[emptied]; !ok || value != "" { - t.Fatalf("List[%q] = %q (present=%v), want an enumerated empty value", emptied, value, ok) + t.Fatalf("List[%q] = %q (present=%v), want an empty value that IS enumerated: "+ + "the row exists even though Recall denies it", emptied, value, ok) } if _, ok := plane[never]; ok { t.Fatalf("List carries %q, which nothing stored", never) } - - first := forgetMemory(t, ctx, fixture, emptied) - if !first.Found || first.Value != "" { - t.Fatalf("first Forget(%q) = %+v, want Found true with the empty stored value", emptied, first) - } - assertMemoriesRawAbsent(t, ctx, fixture, "kv.memory."+emptied) - assertMemoriesNeighborsSurvived(t, ctx, fixture, neighbors) - - var afterFirst int - if fixture.CountHistory != nil { - var err error - afterFirst, err = fixture.CountHistory(ctx) - if err != nil { - t.Fatalf("CountHistory after first Forget: %v", err) - } - } - - second := forgetMemory(t, ctx, fixture, emptied) - if second.Found || second.Value != "" { - t.Fatalf("second Forget(%q) = %+v, want Found false with no value", emptied, second) - } - assertMemoriesRawAbsent(t, ctx, fixture, "kv.memory."+emptied) - assertMemoriesNeighborsSurvived(t, ctx, fixture, neighbors) - - // The no-write half of the retry is the only leg that needs history, which - // is why it comes last: a backend that cannot observe history has run every - // other leg by here, and SKIPS this one with that reason, as the - // MemoriesFixture.CountHistory doc promises, rather than passing quietly. - if fixture.CountHistory == nil { - t.Skip("this backend cannot observe history, so the no-write half of the retry is unobservable here") - } - afterSecond, err := fixture.CountHistory(ctx) - if err != nil { - t.Fatalf("CountHistory after second Forget: %v", err) - } - if afterSecond != afterFirst { - t.Fatalf("history entries went %d -> %d across the second Forget, want no write", afterFirst, afterSecond) - } } // RunMemoriesForgetRemovesExactlyTheNamedRow IS THE QUADRANT CASE, and the @@ -562,7 +528,13 @@ func RunMemoriesForgetReportsTheForgottenValue(t *testing.T, ctx context.Context // RunMemoriesForgetOfAnAbsentKeyIsNotFoundAndDeletesNothing pins that a miss is // a RESULT and not an error, that it changes no row anywhere in the table, that // the second call — the one a retrying caller actually makes — answers the same -// way. +// way, and that the SECOND kind of miss behaves like the first: a memory stored +// as the empty string is Found false, and its row is left standing. +// +// That last leg is memoryops.ForgetResult.Found's parenthesis, and it keeps +// Forget's answer and Recall's answer the same statement. A body that deleted +// on "the row is there" rather than on "Recall would find it" would report +// nothing removed while removing something. // // The whole-table row count is the assertion that matters: "the key is still // absent" would pass on a delete that swept a neighboring plane. @@ -570,6 +542,10 @@ func RunMemoriesForgetOfAnAbsentKeyIsNotFoundAndDeletesNothing(t *testing.T, ctx t.Helper() neighbors := seedMemoriesAllFourClasses(t, ctx, fixture) key := fixture.IssuePrefix + "-never-stored-forget" + emptied := fixture.IssuePrefix + "-empty-forget" + if err := fixture.SetConfig(ctx, "kv.memory."+emptied, ""); err != nil { + t.Fatalf("seed an empty memory row out of band: %v", err) + } var before int if err := fixture.QueryScalar(ctx, "SELECT COUNT(*) FROM config", nil, &before); err != nil { @@ -577,12 +553,14 @@ func RunMemoriesForgetOfAnAbsentKeyIsNotFoundAndDeletesNothing(t *testing.T, ctx } for attempt := 1; attempt <= 2; attempt++ { - result, err := fixture.Memories.Forget(ctx, memoryops.ForgetRequest{Key: key}) - if err != nil { - t.Fatalf("Forget(%q) attempt %d = %v, want success with Found false", key, attempt, err) - } - if result.Found || result.Value != "" { - t.Fatalf("Forget(%q) attempt %d = %+v, want Found false and no value", key, attempt, result) + for _, miss := range []string{key, emptied} { + result, err := fixture.Memories.Forget(ctx, memoryops.ForgetRequest{Key: miss}) + if err != nil { + t.Fatalf("Forget(%q) attempt %d = %v, want success with Found false", miss, attempt, err) + } + if result.Found || result.Value != "" { + t.Fatalf("Forget(%q) attempt %d = %+v, want Found false and no value", miss, attempt, result) + } } } @@ -591,8 +569,11 @@ func RunMemoriesForgetOfAnAbsentKeyIsNotFoundAndDeletesNothing(t *testing.T, ctx t.Fatalf("count config rows after: %v", err) } if after != before { - t.Fatalf("config rows went %d -> %d across two forgets that found nothing, want no change", before, after) + t.Fatalf("config rows went %d -> %d across four forgets that found nothing, want no change", before, after) } + // The stored-empty row is specifically still there: Found false said nothing + // was removed, and that has to be true of the row as well as of the answer. + assertMemoriesRawValue(t, ctx, fixture, "kv.memory."+emptied, "") assertMemoriesNeighborsSurvived(t, ctx, fixture, neighbors) // The empty key is a refusal, not a miss. diff --git a/backend/conformance/molecule_contract.go b/backend/conformance/molecule_contract.go deleted file mode 100644 index 36021d53a..000000000 --- a/backend/conformance/molecule_contract.go +++ /dev/null @@ -1,480 +0,0 @@ -package conformance - -import ( - "context" - "errors" - "testing" - - "github.com/steveyegge/beads/internal/types" - publicops "github.com/steveyegge/beads/issueops" -) - -// This file holds the contract for the molecule operations every backend -// serves through one library entry each: -// -// - CloseRequest.AutoCloseMolecule / CloseBatchRequest.AutoCloseMolecule — -// the close closes the molecule root it completed IN ITS OWN TRANSACTION -// (internal/storage/issueops.CloseCompletedMoleculeInTx on the store -// backends, uow.CloseCompletedMoleculeInUOW on the unit of work, the -// server's own on http). Opt-in; unforced; guarded on the root revision. -// - issueops.MoleculeStepper — the advance behind `bd close --continue`, -// whose claim is a real claim (assignee = Actor) that never takes a step -// another actor holds. -// -// THE LEAF DOC COMMENTS ARE THE SPEC (issueops/issueops.go CloseRequest, -// issueops/batchcloser.go, issueops/moleculestepper.go, issueops/molecule.go). - -// MoleculeFixture supplies adapter-specific storage access for the molecule -// assertions. -type MoleculeFixture struct { - // IssuePrefix namespaces the ids each assertion seeds. - IssuePrefix string - Lifecycle publicops.Lifecycle - BatchCloser publicops.BatchCloser - Stepper publicops.MoleculeStepper - // CreateIssue and CreateWisp seed one row in the issues and wisps plane. - CreateIssue func(context.Context, *types.Issue, string) error - CreateWisp func(context.Context, *types.Issue, string) error - // AddDependency seeds one edge. - AddDependency func(context.Context, *types.Dependency, string) error - // QueryScalar runs a single-row query and scans it. - QueryScalar func(context.Context, string, []any, ...any) error -} - -const moleculeActor = "molecule-contract" - -type moleculeSeed struct { - t *testing.T - ctx context.Context - fixture MoleculeFixture -} - -func (s moleculeSeed) issue(id string, typ types.IssueType, status types.Status, ephemeral bool, labels ...string) { - s.t.Helper() - issue := &types.Issue{ID: id, Title: "molecule " + id, IssueType: typ, Status: status, Priority: 2, Labels: labels, Ephemeral: ephemeral} - if status == types.StatusClosed { - issue.CloseReason = "seeded closed" - } - create := s.fixture.CreateIssue - if ephemeral { - create = s.fixture.CreateWisp - } - if err := create(s.ctx, issue, moleculeActor); err != nil { - s.t.Fatalf("seed %s: %v", id, err) - } -} - -func (s moleculeSeed) edge(from, to string, typ types.DependencyType) { - s.t.Helper() - if err := s.fixture.AddDependency(s.ctx, &types.Dependency{IssueID: from, DependsOnID: to, Type: typ}, moleculeActor); err != nil { - s.t.Fatalf("seed edge %s -> %s: %v", from, to, err) - } -} - -// row reads id's status and assignee from whichever plane holds it. -func (s moleculeSeed) row(id string) (status, assignee string) { - s.t.Helper() - err := s.fixture.QueryScalar(s.ctx, - `SELECT status, COALESCE(assignee, '') FROM issues WHERE id = ? - UNION ALL SELECT status, COALESCE(assignee, '') FROM wisps WHERE id = ?`, - []any{id, id}, &status, &assignee) - if err != nil { - s.t.Fatalf("read %s: %v", id, err) - } - return status, assignee -} - -// molecule seeds root <- step1 (closed), step2 (open) and answers the ids. -func (s moleculeSeed) molecule(name string, rootType types.IssueType, ephemeral bool, labels ...string) (root, done, last string) { - s.t.Helper() - root = s.fixture.IssuePrefix + "-" + name - done, last = root+".1", root+".2" - s.issue(root, rootType, types.StatusOpen, ephemeral, labels...) - s.issue(done, types.TypeTask, types.StatusClosed, ephemeral) - s.issue(last, types.TypeTask, types.StatusOpen, ephemeral) - s.edge(done, root, types.DepParentChild) - s.edge(last, root, types.DepParentChild) - return root, done, last -} - -func (s moleculeSeed) closeStep(id string, autoClose bool) publicops.CloseResult { - s.t.Helper() - res, err := s.fixture.Lifecycle.Close(s.ctx, publicops.CloseRequest{ - Actor: moleculeActor, IssueID: id, Reason: "step done", Session: "molecule-session", AutoCloseMolecule: autoClose, - }) - if err != nil { - s.t.Fatalf("close %s: %v", id, err) - } - return res -} - -// RunMoleculeCloseAutoClosesTheCompletedRoot pins CloseRequest.AutoCloseMolecule: -// closing the last open step closes an auto-closing root in the same call, -// reports it, and records the auto-close reason. -func RunMoleculeCloseAutoClosesTheCompletedRoot(t *testing.T, ctx context.Context, fixture MoleculeFixture) { - t.Helper() - s := moleculeSeed{t, ctx, fixture} - for _, shape := range []struct { - name string - rootType types.IssueType - labels []string - }{ - {"poured", types.TypeMolecule, nil}, - {"template", types.TypeEpic, []string{publicops.MoleculeTemplateLabel}}, - } { - root, _, last := s.molecule("auto-"+shape.name, shape.rootType, false, shape.labels...) - res := s.closeStep(last, true) - if !res.Changed || res.Issue == nil || res.Issue.Status != types.StatusClosed { - t.Fatalf("%s: close %s = %+v, want the step closed", shape.name, last, res) - } - if res.AutoClosedMolecule == nil || res.AutoClosedMolecule.ID != root { - t.Fatalf("%s: AutoClosedMolecule = %+v, want %s", shape.name, res.AutoClosedMolecule, root) - } - if res.MoleculeAutoCloseRefusal != "" { - t.Errorf("%s: MoleculeAutoCloseRefusal = %q, want none", shape.name, res.MoleculeAutoCloseRefusal) - } - if status, _ := s.row(root); status != string(types.StatusClosed) { - t.Errorf("%s: root %s status = %s, want closed", shape.name, root, status) - } - var reason string - if err := fixture.QueryScalar(ctx, `SELECT close_reason FROM issues WHERE id = ?`, []any{root}, &reason); err != nil { - t.Fatalf("read %s close_reason: %v", root, err) - } - if reason != publicops.MoleculeAutoCloseReason { - t.Errorf("%s: root close_reason = %q, want %q", shape.name, reason, publicops.MoleculeAutoCloseReason) - } - } -} - -// RunMoleculeAutoCloseIsOptInAndRuleBound pins the opt-in default and the -// three roots the auto-close must leave open: one a request did not ask for, -// an incomplete one, and an ordinary epic (explicit close-eligible work). -func RunMoleculeAutoCloseIsOptInAndRuleBound(t *testing.T, ctx context.Context, fixture MoleculeFixture) { - t.Helper() - s := moleculeSeed{t, ctx, fixture} - - root, _, last := s.molecule("optin", types.TypeMolecule, false) - if res := s.closeStep(last, false); res.AutoClosedMolecule != nil { - t.Errorf("close without AutoCloseMolecule reported %s auto-closed", res.AutoClosedMolecule.ID) - } - if status, _ := s.row(root); status != string(types.StatusOpen) { - t.Errorf("root %s status after an un-flagged close = %s, want open", root, status) - } - - root, _, last = s.molecule("plain", types.TypeEpic, false) - if res := s.closeStep(last, true); res.AutoClosedMolecule != nil { - t.Errorf("an ordinary epic auto-closed: %s", res.AutoClosedMolecule.ID) - } - if status, _ := s.row(root); status != string(types.StatusOpen) { - t.Errorf("ordinary epic %s status = %s, want open", root, status) - } - - root, done, last := s.molecule("incomplete", types.TypeMolecule, false) - if res := s.closeStep(done, true); res.AutoClosedMolecule != nil { - t.Errorf("an incomplete molecule auto-closed: %s", res.AutoClosedMolecule.ID) - } - if status, _ := s.row(root); status != string(types.StatusOpen) { - t.Errorf("incomplete molecule %s status = %s, want open (%s still open)", root, status, last) - } -} - -// RunMoleculeRecloseHealsAStrandedRoot pins the replay clause: an idempotent -// re-close of the last step still closes a root left open. -func RunMoleculeRecloseHealsAStrandedRoot(t *testing.T, ctx context.Context, fixture MoleculeFixture) { - t.Helper() - s := moleculeSeed{t, ctx, fixture} - root := fixture.IssuePrefix + "-stranded" - step := root + ".1" - s.issue(root, types.TypeMolecule, types.StatusOpen, false) - s.issue(step, types.TypeTask, types.StatusClosed, false) - s.edge(step, root, types.DepParentChild) - - res := s.closeStep(step, true) - if res.Changed { - t.Errorf("re-close of %s reported Changed", step) - } - if res.AutoClosedMolecule == nil || res.AutoClosedMolecule.ID != root { - t.Fatalf("re-close AutoClosedMolecule = %+v, want %s healed", res.AutoClosedMolecule, root) - } - if status, _ := s.row(root); status != string(types.StatusClosed) { - t.Errorf("stranded root %s status = %s, want closed", root, status) - } -} - -// RunMoleculeBatchCloseAutoClosesTheRootOnce pins the batch clause: a batch -// that closes the remaining steps closes the root once, on one outcome. -func RunMoleculeBatchCloseAutoClosesTheRootOnce(t *testing.T, ctx context.Context, fixture MoleculeFixture) { - t.Helper() - s := moleculeSeed{t, ctx, fixture} - root, _, last := s.molecule("batch", types.TypeMolecule, false) - other := root + ".3" - s.issue(other, types.TypeTask, types.StatusOpen, false) - s.edge(other, root, types.DepParentChild) - - res, err := fixture.BatchCloser.CloseBatch(ctx, publicops.CloseBatchRequest{ - Actor: moleculeActor, - Items: []publicops.BatchCloseItem{{IssueID: last, Reason: "done"}, {IssueID: other, Reason: "done"}}, - AutoCloseMolecule: true, - }) - if err != nil { - t.Fatalf("CloseBatch: %v", err) - } - reported := 0 - for _, outcome := range res.Outcomes { - if outcome.Err != nil { - t.Fatalf("outcome %s: %v", outcome.IssueID, outcome.Err) - } - if outcome.AutoClosedMolecule != nil { - reported++ - if outcome.AutoClosedMolecule.ID != root { - t.Errorf("outcome %s auto-closed %s, want %s", outcome.IssueID, outcome.AutoClosedMolecule.ID, root) - } - } - } - if reported != 1 { - t.Errorf("the root was reported on %d outcomes, want exactly one", reported) - } - if status, _ := s.row(root); status != string(types.StatusClosed) { - t.Errorf("root %s status = %s, want closed", root, status) - } -} - -// RunMoleculeRefusedRootKeepsTheStepClose pins the refusal clause: a root its -// own close policy refuses (a live blocker) stays open and is reported in the -// policy's own sentence, and the step close it followed stands. -func RunMoleculeRefusedRootKeepsTheStepClose(t *testing.T, ctx context.Context, fixture MoleculeFixture) { - t.Helper() - s := moleculeSeed{t, ctx, fixture} - root, _, last := s.molecule("blocked", types.TypeMolecule, false) - blocker := root + "-blocker" - s.issue(blocker, types.TypeTask, types.StatusOpen, false) - s.edge(root, blocker, types.DepBlocks) - - res := s.closeStep(last, true) - if res.AutoClosedMolecule != nil { - t.Errorf("a blocked root auto-closed: %s", res.AutoClosedMolecule.ID) - } - if want := publicops.NewCloseBlockedError(root, []string{blocker}).Error(); res.MoleculeAutoCloseRefusal != want { - t.Errorf("MoleculeAutoCloseRefusal = %q, want the policy's own sentence %q", res.MoleculeAutoCloseRefusal, want) - } - if status, _ := s.row(last); status != string(types.StatusClosed) { - t.Errorf("step %s status = %s, want closed (the refusal must not undo it)", last, status) - } - if status, _ := s.row(root); status != string(types.StatusOpen) { - t.Errorf("blocked root %s status = %s, want open", root, status) - } -} - -// RunMoleculeGuardedRootKeepsTheStepClose pins the guard clause: the root close -// is unforced, so a root a direct close would refuse — assigned to someone -// other than Actor, pinned, or a template — stays open and is reported in the -// guard's own sentence, on a single close and on a batch, and the step's close -// (and a batch's other items) stand. -func RunMoleculeGuardedRootKeepsTheStepClose(t *testing.T, ctx context.Context, fixture MoleculeFixture) { - t.Helper() - s := moleculeSeed{t, ctx, fixture} - for _, guard := range []struct { - name string - apply func(*types.Issue) - // refusal is the guard's own error for root, whose sentence - // MoleculeAutoCloseRefusal reports. - refusal func(root string) error - }{ - {"assigned", func(root *types.Issue) { root.Assignee = "someone-else" }, func(root string) error { - return &publicops.CloseNotAssigneeError{IssueID: root, Assignee: "someone-else", Actor: moleculeActor} - }}, - {"pinned", func(root *types.Issue) { root.Pinned = true }, func(root string) error { - return &publicops.PinnedError{IssueID: root} - }}, - {"template", func(root *types.Issue) { root.IsTemplate = true }, func(root string) error { - return &publicops.TemplateReadOnlyError{IssueID: root} - }}, - } { - for _, batch := range []bool{false, true} { - name := guard.name - if batch { - name += "-batch" - } - root := fixture.IssuePrefix + "-guarded-" + name - last := root + ".2" - rootIssue := &types.Issue{ID: root, Title: "molecule " + root, IssueType: types.TypeMolecule, Status: types.StatusOpen, Priority: 2} - guard.apply(rootIssue) - if err := fixture.CreateIssue(ctx, rootIssue, moleculeActor); err != nil { - t.Fatalf("%s: seed %s: %v", name, root, err) - } - s.issue(root+".1", types.TypeTask, types.StatusClosed, false) - s.issue(last, types.TypeTask, types.StatusOpen, false) - s.edge(root+".1", root, types.DepParentChild) - s.edge(last, root, types.DepParentChild) - - var ( - autoClosed *publicops.Issue - refusal string - closed = []string{last} - ) - if batch { - other := root + "-other" - s.issue(other, types.TypeTask, types.StatusOpen, false) - closed = append(closed, other) - res, err := fixture.BatchCloser.CloseBatch(ctx, publicops.CloseBatchRequest{ - Actor: moleculeActor, - Items: []publicops.BatchCloseItem{{IssueID: last, Reason: "done"}, {IssueID: other, Reason: "done"}}, - AutoCloseMolecule: true, - }) - if err != nil { - t.Fatalf("%s: CloseBatch: %v", name, err) - } - for _, outcome := range res.Outcomes { - if outcome.Err != nil { - t.Fatalf("%s: outcome %s: %v", name, outcome.IssueID, outcome.Err) - } - if outcome.IssueID == last { - autoClosed, refusal = outcome.AutoClosedMolecule, outcome.MoleculeAutoCloseRefusal - } - } - } else { - res := s.closeStep(last, true) - autoClosed, refusal = res.AutoClosedMolecule, res.MoleculeAutoCloseRefusal - } - - if autoClosed != nil { - t.Errorf("%s: a guarded root auto-closed: %s", name, autoClosed.ID) - } - if want := guard.refusal(root).Error(); refusal != want { - t.Errorf("%s: MoleculeAutoCloseRefusal = %q, want the guard's own sentence %q", name, refusal, want) - } - for _, id := range closed { - if status, _ := s.row(id); status != string(types.StatusClosed) { - t.Errorf("%s: %s status = %s, want closed (the refusal must not undo it)", name, id, status) - } - } - if status, _ := s.row(root); status != string(types.StatusOpen) { - t.Errorf("%s: guarded root %s status = %s, want open", name, root, status) - } - } - } -} - -// RunMoleculeStepperClaimsTheNextReadyStepForTheActor pins the advance and its -// claim: the next ready step becomes in progress AND assigned to Actor; without -// AutoClaim nothing is written. -func RunMoleculeStepperClaimsTheNextReadyStepForTheActor(t *testing.T, ctx context.Context, fixture MoleculeFixture) { - t.Helper() - s := moleculeSeed{t, ctx, fixture} - root := fixture.IssuePrefix + "-advance" - a, b, c := root+".1", root+".2", root+".3" - s.issue(root, types.TypeMolecule, types.StatusOpen, false) - s.issue(a, types.TypeTask, types.StatusClosed, false) - s.issue(b, types.TypeTask, types.StatusOpen, false) - s.issue(c, types.TypeTask, types.StatusOpen, false) - for _, id := range []string{a, b, c} { - s.edge(id, root, types.DepParentChild) - } - s.edge(c, b, types.DepBlocks) - - res, err := fixture.Stepper.Advance(ctx, publicops.AdvanceRequest{Actor: moleculeActor, ClosedStepID: a}) - if err != nil { - t.Fatalf("Advance: %v", err) - } - if res.MoleculeID != root || res.Claimed || res.NextStep == nil || res.NextStep.ID != b { - t.Fatalf("Advance without a claim = %+v, want next %s in %s, unclaimed", res, b, root) - } - if status, assignee := s.row(b); status != string(types.StatusOpen) || assignee != "" { - t.Errorf("%s after an unclaimed advance = %s/%q, want open and unassigned", b, status, assignee) - } - - res, err = fixture.Stepper.Advance(ctx, publicops.AdvanceRequest{Actor: moleculeActor, ClosedStepID: a, AutoClaim: true}) - if err != nil { - t.Fatalf("Advance with a claim: %v", err) - } - if !res.Claimed || res.NextStep == nil || res.NextStep.ID != b || res.NextStep.Assignee != moleculeActor { - t.Fatalf("Advance with a claim = %+v, want %s claimed for %s", res, b, moleculeActor) - } - if status, assignee := s.row(b); status != string(types.StatusInProgress) || assignee != moleculeActor { - t.Errorf("%s after the claim = %s/%q, want in_progress assigned to %s", b, status, assignee, moleculeActor) - } - if status, _ := s.row(c); status != string(types.StatusOpen) { - t.Errorf("%s (blocked) status = %s, want open", c, status) - } -} - -// RunMoleculeStepperNeverTakesAStepAnotherActorHolds pins the refusal the claim -// carries: a ready step assigned to someone else is skipped for the next one, -// and left exactly as it was. -func RunMoleculeStepperNeverTakesAStepAnotherActorHolds(t *testing.T, ctx context.Context, fixture MoleculeFixture) { - t.Helper() - s := moleculeSeed{t, ctx, fixture} - root := fixture.IssuePrefix + "-held" - a, held, free := root+".1", root+".2", root+".3" - s.issue(root, types.TypeMolecule, types.StatusOpen, false) - s.issue(a, types.TypeTask, types.StatusClosed, false) - heldIssue := &types.Issue{ID: held, Title: "held", IssueType: types.TypeTask, Status: types.StatusOpen, Priority: 2, Assignee: "someone-else"} - if err := fixture.CreateIssue(ctx, heldIssue, moleculeActor); err != nil { - t.Fatalf("seed %s: %v", held, err) - } - s.issue(free, types.TypeTask, types.StatusOpen, false) - for _, id := range []string{a, held, free} { - s.edge(id, root, types.DepParentChild) - } - - res, err := fixture.Stepper.Advance(ctx, publicops.AdvanceRequest{Actor: moleculeActor, ClosedStepID: a, AutoClaim: true}) - if err != nil { - t.Fatalf("Advance: %v", err) - } - if !res.Claimed || res.NextStep == nil || res.NextStep.ID != free { - t.Fatalf("Advance = %+v, want %s skipped (held) and %s claimed", res, held, free) - } - if status, assignee := s.row(held); status != string(types.StatusOpen) || assignee != "someone-else" { - t.Errorf("held step %s = %s/%q, want untouched (open, someone-else)", held, status, assignee) - } -} - -// RunMoleculeStepperAnswersCompleteAndOutsideAMolecule pins the two answers -// that claim nothing. -func RunMoleculeStepperAnswersCompleteAndOutsideAMolecule(t *testing.T, ctx context.Context, fixture MoleculeFixture) { - t.Helper() - s := moleculeSeed{t, ctx, fixture} - orphan := fixture.IssuePrefix + "-orphan" - s.issue(orphan, types.TypeTask, types.StatusClosed, false) - res, err := fixture.Stepper.Advance(ctx, publicops.AdvanceRequest{Actor: moleculeActor, ClosedStepID: orphan, AutoClaim: true}) - if err != nil { - t.Fatalf("Advance(orphan): %v", err) - } - if res.MoleculeID != "" || res.NextStep != nil || res.Claimed || res.ClosedStep == nil || res.ClosedStep.ID != orphan { - t.Errorf("Advance(orphan) = %+v, want the step and no molecule", res) - } - - root := fixture.IssuePrefix + "-complete" - s.issue(root, types.TypeEpic, types.StatusOpen, false) - s.issue(root+".1", types.TypeTask, types.StatusClosed, false) - s.edge(root+".1", root, types.DepParentChild) - res, err = fixture.Stepper.Advance(ctx, publicops.AdvanceRequest{Actor: moleculeActor, ClosedStepID: root + ".1", AutoClaim: true}) - if err != nil { - t.Fatalf("Advance(complete): %v", err) - } - if res.MoleculeID != root || !res.Complete || res.NextStep != nil || res.Claimed { - t.Errorf("Advance(complete) = %+v, want %s complete", res, root) - } - - if _, err := fixture.Stepper.Advance(ctx, publicops.AdvanceRequest{Actor: moleculeActor, ClosedStepID: fixture.IssuePrefix + "-absent"}); !errors.Is(err, publicops.ErrNotFound) { - t.Errorf("Advance(absent) = %v, want ErrNotFound", err) - } -} - -// RunMoleculeStepperClaimsAWispStep pins the wisp plane: a wisp molecule's next -// step is claimed for the actor too. -func RunMoleculeStepperClaimsAWispStep(t *testing.T, ctx context.Context, fixture MoleculeFixture) { - t.Helper() - s := moleculeSeed{t, ctx, fixture} - root, done, last := s.molecule("wisp", types.TypeMolecule, true) - res, err := fixture.Stepper.Advance(ctx, publicops.AdvanceRequest{Actor: moleculeActor, ClosedStepID: done, AutoClaim: true}) - if err != nil { - t.Fatalf("Advance: %v", err) - } - if res.MoleculeID != root || !res.Claimed || res.NextStep == nil || res.NextStep.ID != last { - t.Fatalf("Advance over a wisp molecule = %+v, want %s claimed", res, last) - } - if status, assignee := s.row(last); status != string(types.StatusInProgress) || assignee != moleculeActor { - t.Errorf("wisp step %s = %s/%q, want in_progress assigned to %s", last, status, assignee, moleculeActor) - } -} diff --git a/backend/conformance/portable.go b/backend/conformance/portable.go index 488c02593..050b7f008 100644 --- a/backend/conformance/portable.go +++ b/backend/conformance/portable.go @@ -42,7 +42,6 @@ func RunPortableMethods(t *testing.T, factory Factory) { t.Run("ImportIssueComment", func(t *testing.T) { testImportIssueComment(t, factory) }) t.Run("AddIssueCommentBurstOrder", func(t *testing.T) { testAddIssueCommentBurstOrder(t, factory) }) t.Run("PromoteFromEphemeral", func(t *testing.T) { testPromoteFromEphemeral(t, factory) }) - t.Run("PromoteClosedWispPreservesSession", func(t *testing.T) { testPromoteClosedWispPreservesSession(t, factory) }) t.Run("UpdateIssueID", func(t *testing.T) { testUpdateIssueID(t, factory) }) t.Run("DeleteIssuesBySourceRepo", func(t *testing.T) { testDeleteIssuesBySourceRepo(t, factory) }) t.Run("CreateIssuesWithFullOptions", func(t *testing.T) { testCreateIssuesWithFullOptions(t, factory) }) @@ -578,36 +577,6 @@ func testPromoteFromEphemeral(t *testing.T, f Factory) { } } -// testPromoteClosedWispPreservesSession verifies that promoting a *closed* -// ephemeral wisp preserves its closed_by_session provenance. Before the fix -// (GH#4662), closed_by_session was absent from the shared issue select/scan -// and insert column set, so promotion (read wisp -> insert issue) hydrated a -// zero value and dropped the original closing session. Promotion re-inserts -// through that shared (classic) writer, so this case guards GH#4662's path -// only; the domain writer's copy of the column (db.insertIssueRow) is pinned -// by the domain/db Insert tests. -func testPromoteClosedWispPreservesSession(t *testing.T, f Factory) { - s := f(t) - c := ctx() - - must(t, s.CreateIssue(c, withDefaults(&types.Issue{ID: "test-cw", Title: "Closed wisp", Ephemeral: true}), "a")) - must(t, s.CloseIssue(c, "test-cw", "done", "a", "sess-xyz")) - - must(t, s.PromoteFromEphemeral(c, "test-cw", "a")) - - got, err := s.GetIssue(c, "test-cw") - must(t, err) - if got.Ephemeral { - t.Error("promoted issue still Ephemeral") - } - if got.Status != types.StatusClosed { - t.Errorf("promoted issue status = %q, want %q", got.Status, types.StatusClosed) - } - if got.ClosedBySession != "sess-xyz" { - t.Errorf("closed_by_session after promote = %q, want %q (GH#4662)", got.ClosedBySession, "sess-xyz") - } -} - // --- Id rekey / source-repo purge / batch create --- func testUpdateIssueID(t *testing.T, f Factory) { diff --git a/backend/conformance/reader_contract.go b/backend/conformance/reader_contract.go index 245a9efaa..6910a5bbb 100644 --- a/backend/conformance/reader_contract.go +++ b/backend/conformance/reader_contract.go @@ -113,10 +113,6 @@ type ReaderFixture struct { AddDependency func(context.Context, *types.Dependency, string) error // AddComment puts one comment on a durable issue. AddComment func(ctx context.Context, issueID, author, text string) error - // PromoteWisp moves a seeded wisp into the durable plane. It is the only - // way to seed a DURABLE row of an infra type: creating one routes it to the - // wisps plane on every backend. - PromoteWisp func(ctx context.Context, id, actor string) error } // RunReaderReadyDefaultTypeExclusionsYieldToAnExplicitType pins @@ -790,112 +786,6 @@ func RunReaderListReadyFlagRefusesAFilterItCannotCarry(t *testing.T, ctx context assertReaderPageIDs(t, "List --id without --ready", page, []string{one}) } -// RunReaderListQueryMatchesTheTitleOrTheID pins ListRequest.Query: `bd -// search`'s text, which matches a row whose title OR id contains it, case -// insensitively, and which narrows without lifting a default exclusion — and -// issueops.SearchListRequest, the request every `bd search` route sends, which -// does lift them. -// -// EVERY KIND OF ROW the search scope promises is seeded with the text, because -// a scope that silently loses one kind answers every other row correctly: a -// template, a gate, a wisp of an infra type, and a DURABLE row of that type — -// a promoted wisp, which a type filter that narrows an infra type to its -// ephemeral rows drops with no error. Each must come back under the bare scope -// and under its own type. -// -// It scopes itself with a label rather than an id set, so the served leg runs -// it as written: listIssues publishes no id parameter. -func RunReaderListQueryMatchesTheTitleOrTheID(t *testing.T, ctx context.Context, fixture ReaderFixture) { - t.Helper() - if fixture.PromoteWisp == nil { - t.Fatal("fixture has no PromoteWisp: a durable row of an infra type cannot be seeded, and it is the row a narrowed type filter drops") - } - scope := readerLabel(fixture, "lsquery") - needle := strings.ToLower(fixture.IssuePrefix) + "qneedle" - titleHit := readerID(fixture, "lsquery", "title") - idHit := readerID(fixture, "lsquery", "idhit") - closedHit := readerID(fixture, "lsquery", "closed") - miss := readerID(fixture, "lsquery", "miss") - template := readerID(fixture, "lsquery", "template") - gate := readerID(fixture, "lsquery", "gate") - wispMessage := readerID(fixture, "lsquery", "wispmsg") - promotedMessage := readerID(fixture, "lsquery", "promotedmsg") - - // The title carries the needle in UPPER case and the request asks in lower, - // so a case-sensitive match finds nothing. - titleIssue := readerIssue(titleHit, types.TypeTask, scope) - titleIssue.Title = "fix the " + strings.ToUpper(needle) + " path" - // The id-hit's title says nothing about it: only its id can match. - idIssue := readerIssue(idHit, types.TypeTask, scope) - idIssue.Title = "unrelated words" - closedIssue := readerIssue(closedHit, types.TypeTask, scope) - closedIssue.Title = "old " + needle - closedIssue.Status = types.StatusClosed - missIssue := readerIssue(miss, types.TypeTask, scope) - missIssue.Title = "nothing to see" - templateIssue := readerIssue(template, types.TypeTask, scope) - templateIssue.Title = needle + " template" - templateIssue.IsTemplate = true - gateIssue := readerIssue(gate, types.TypeGate, scope) - gateIssue.Title = needle + " gate" - for _, issue := range []*types.Issue{titleIssue, idIssue, closedIssue, missIssue, templateIssue, gateIssue} { - seedReaderIssue(t, ctx, fixture, issue) - } - // Two rows of the same infra type, one per plane. `message` is built in, - // so the type is valid on every backend without configuration. - for _, id := range []string{wispMessage, promotedMessage} { - message := readerIssue(id, types.TypeMessage, scope) - message.Title = needle + " message" - message.Ephemeral = true - seedReaderWisp(t, ctx, fixture, message) - } - if err := fixture.PromoteWisp(ctx, promotedMessage, "seed"); err != nil { - t.Fatalf("promote wisp %s: %v", promotedMessage, err) - } - - searchType := func(issueType types.IssueType) publicops.ListRequest { - req := searchScoped(needle, scope) - req.IssueType = string(issueType) - return req - } - - for _, tc := range []struct { - name string - req publicops.ListRequest - want []string - }{ - {"a title match, case-insensitively, under the default exclusions", publicops.ListRequest{Labels: []string{scope}, Query: needle}, []string{titleHit}}, - {"AllFlag takes the closed match back", publicops.ListRequest{Labels: []string{scope}, Query: needle, AllFlag: true}, []string{titleHit, closedHit}}, - {"an id match whose title does not contain the text", publicops.ListRequest{Labels: []string{scope}, Query: idHit, AllFlag: true}, []string{idHit}}, - {"text nothing contains", publicops.ListRequest{Labels: []string{scope}, Query: needle + "-absent", AllFlag: true}, nil}, - {"no text narrows nothing", publicops.ListRequest{Labels: []string{scope}}, []string{titleHit, idHit, miss}}, - {"SearchListRequest, `bd search`'s scope, takes every status and every kind of row back", searchScoped(needle, scope), []string{titleHit, closedHit, template, gate, wispMessage, promotedMessage}}, - {"SearchListRequest under an infra type keeps both planes' rows of it", searchType(types.TypeMessage), []string{wispMessage, promotedMessage}}, - {"SearchListRequest under the gate type", searchType(types.TypeGate), []string{gate}}, - {"SearchListRequest under a plain type keeps its templates", searchType(types.TypeTask), []string{titleHit, closedHit, template}}, - // Outside the search scope an infra type alone reads only its - // ephemeral rows, which is where creating one puts it; IncludeEphemeral - // is what lifts that narrowing to the durable rows as well. - {"an infra type alone reads only its ephemeral rows", publicops.ListRequest{Labels: []string{scope}, Query: needle, IssueType: string(types.TypeMessage)}, []string{wispMessage}}, - {"IncludeEphemeral lifts that narrowing", publicops.ListRequest{Labels: []string{scope}, Query: needle, IssueType: string(types.TypeMessage), IncludeEphemeral: true}, []string{wispMessage, promotedMessage}}, - } { - page, err := fixture.Reader.List(ctx, tc.req) - if err != nil { - t.Errorf("List %s: %v", tc.name, err) - continue - } - assertReaderPageIDSet(t, "List "+tc.name, page, tc.want) - } -} - -// searchScoped is the library's search request narrowed to one case's label, -// the request every `bd search` route sends. -func searchScoped(query, label string) publicops.ListRequest { - req := publicops.SearchListRequest(query) - req.Labels = []string{label} - return req -} - // RunReaderListEmptyPageIsWellFormed pins the page shape when nothing matches // (reader.go:376-381). Items is never nil for a successful call, so no caller // has to tell null from empty to learn that nothing matched, and an empty page @@ -3175,335 +3065,6 @@ func assertReaderBriefRow(t *testing.T, what string, brief, full *types.Issue) { } } -// RunReaderListTimeBoundsAreStrictAndWholeSecond pins the eight time bounds -// ListRequest carries beside the created pair — UpdatedAfter/Before, -// ClosedAfter/Before, DeferAfter/Before and DueAfter/Before — at their exact -// boundaries (reader.go: each is a strict bound on its column). -// -// THE BOUNDARY IS THE ASSERTION. Three rows sit one second apart around an -// instant T, so a body that compared inclusively, swapped the two ends, or -// bound the wrong column fails on a row rather than on nothing: -// -// - after=T selects exactly the T+1s row and before=T exactly the T-1s row, -// so the row AT T is excluded by both; -// - a bound carrying a sub-second remainder (T+900ms) behaves as T on both -// ends, because the bound is compared at whole-second precision (the -// storage builder formats it as RFC 3339 without a fraction); -// - a row whose column is NULL — an open row's closed_at, an unset -// defer_until or due_at — never matches either bound; -// - a bound spelled in a non-UTC zone selects exactly what the same instant -// spelled in UTC does. An offset is part of the instant, not decoration, -// and a body that dropped it would move the bound by the offset. -// -// Every family scopes itself by id and seeds no labels, so no write after the -// insert can move updated_at off the instant it was seeded at; AllFlag keeps -// the closed rows in view. -func RunReaderListTimeBoundsAreStrictAndWholeSecond(t *testing.T, ctx context.Context, fixture ReaderFixture) { - t.Helper() - at := time.Now().UTC().Truncate(time.Second).Add(-3 * time.Hour) - created := at.Add(-time.Hour) - instants := []struct { - tag string - at time.Time - }{ - {"early", at.Add(-time.Second)}, - {"exact", at}, - {"late", at.Add(time.Second)}, - } - - for _, family := range []struct { - name string - // set stamps the family's column onto a seed. - set func(issue *types.Issue, v time.Time) - // nullable seeds a fourth row with the column NULL. - nullable bool - after func(*publicops.ListRequest, *time.Time) - before func(*publicops.ListRequest, *time.Time) - }{ - { - name: "updated", - set: func(issue *types.Issue, v time.Time) { issue.UpdatedAt = v }, - after: func(r *publicops.ListRequest, v *time.Time) { r.UpdatedAfter = v }, - before: func(r *publicops.ListRequest, v *time.Time) { r.UpdatedBefore = v }, - }, - { - name: "closed", - set: func(issue *types.Issue, v time.Time) { - issue.Status = types.StatusClosed - issue.UpdatedAt = v - issue.ClosedAt = &v - }, - nullable: true, - after: func(r *publicops.ListRequest, v *time.Time) { r.ClosedAfter = v }, - before: func(r *publicops.ListRequest, v *time.Time) { r.ClosedBefore = v }, - }, - { - name: "defer", - set: func(issue *types.Issue, v time.Time) { issue.DeferUntil = &v }, - nullable: true, - after: func(r *publicops.ListRequest, v *time.Time) { r.DeferAfter = v }, - before: func(r *publicops.ListRequest, v *time.Time) { r.DeferBefore = v }, - }, - { - name: "due", - set: func(issue *types.Issue, v time.Time) { issue.DueAt = &v }, - nullable: true, - after: func(r *publicops.ListRequest, v *time.Time) { r.DueAfter = v }, - before: func(r *publicops.ListRequest, v *time.Time) { r.DueBefore = v }, - }, - } { - caseTag := "ltb" + family.name - ids := map[string]string{} - var scope []string - for _, inst := range instants { - id := readerID(fixture, caseTag, inst.tag) - ids[inst.tag] = id - scope = append(scope, id) - issue := readerIssue(id, types.TypeTask, "") - issue.CreatedAt, issue.UpdatedAt = created, created - family.set(issue, inst.at) - seedReaderIssue(t, ctx, fixture, issue) - } - if family.nullable { - id := readerID(fixture, caseTag, "null") - scope = append(scope, id) - issue := readerIssue(id, types.TypeTask, "") - issue.CreatedAt, issue.UpdatedAt = created, created - seedReaderIssue(t, ctx, fixture, issue) - } - idScope := readerIDFilter(scope...) - - list := func(what string, apply func(*publicops.ListRequest, *time.Time), bound time.Time) []string { - t.Helper() - req := publicops.ListRequest{IDFilter: idScope, AllFlag: true} - apply(&req, &bound) - page, err := fixture.Reader.List(ctx, req) - if err != nil { - t.Fatalf("List (%s %s): %v", family.name, what, err) - } - assertReaderPageNotNil(t, "List ("+family.name+" "+what+")", page) - got := readerPageIDs(page) - slices.Sort(got) - return got - } - expect := func(what string, apply func(*publicops.ListRequest, *time.Time), bound time.Time, want string) { - t.Helper() - if got := list(what, apply, bound); !slices.Equal(got, []string{want}) { - t.Errorf("List (%s %s %s) = %v, want exactly [%s]", family.name, what, bound.Format(time.RFC3339Nano), got, want) - } - } - - plus2 := time.FixedZone("UTC+2", 2*60*60) - for _, bound := range []struct { - what string - v time.Time - }{ - {"at T", at}, - {"at T with a sub-second remainder", at.Add(900 * time.Millisecond)}, - {"at T spelled in a +02:00 zone", at.In(plus2)}, - } { - expect("after "+bound.what, family.after, bound.v, ids["late"]) - expect("before "+bound.what, family.before, bound.v, ids["early"]) - } - - // The NULL row is outside even the widest bounds on both ends. - if family.nullable { - wide := at.Add(-24 * time.Hour) - if got, want := list("after a day earlier", family.after, wide), sortedCopy(ids["early"], ids["exact"], ids["late"]); !slices.Equal(got, want) { - t.Errorf("List (%s after a day earlier) = %v, want %v — a NULL column never matches", family.name, got, want) - } - if got, want := list("before a day later", family.before, at.Add(24*time.Hour)), sortedCopy(ids["early"], ids["exact"], ids["late"]); !slices.Equal(got, want) { - t.Errorf("List (%s before a day later) = %v, want %v — a NULL column never matches", family.name, got, want) - } - } - } -} - -// RunReaderListFilterFamiliesNarrowToTheirRows pins one included and one -// excluded row for each remaining ListRequest filter family that `bd list` -// publishes beyond the scope filters: the priority trio, the three text -// substrings, the empty-description and no-labels predicates, the top-level -// predicate, the type exclusion, the spec prefix, the label pattern and -// regex, the external-ref pair, the pinned-flag pair, the deferred and -// overdue predicates, the status exclusion and the molecule type. -// -// Each row is scoped by id and every arm asks AllFlag (the pinned arms -// excepted, which are about that interplay), so an arm whose filter the body -// dropped answers its whole scope and fails, and one whose filter the body -// inverted answers the other row. -// -// PinnedFlag with NoPinnedFlag is a contradiction, and the role refuses it as -// a validation error rather than letting one of the two silently win. -// ParentID with NoParent is refused the same way, as CountRequest refuses it. -func RunReaderListFilterFamiliesNarrowToTheirRows(t *testing.T, ctx context.Context, fixture ReaderFixture) { - t.Helper() - id := func(name string) string { return readerID(fixture, "lff", name) } - seed := func(name string, mutate func(*types.Issue)) string { - t.Helper() - issue := readerIssue(id(name), types.TypeTask, "") - if mutate != nil { - mutate(issue) - } - seedReaderIssue(t, ctx, fixture, issue) - return issue.ID - } - intp := func(v int) *int { return &v } - past := time.Now().UTC().Truncate(time.Second).Add(-48 * time.Hour) - future := time.Now().UTC().Truncate(time.Second).Add(48 * time.Hour) - label := readerLabel(fixture, "lffpat") - external := fixture.IssuePrefix + "-LFF-ext-17" - spec := "docs/" + fixture.IssuePrefix + "-lff/" - - p0 := seed("p0", func(i *types.Issue) { i.Priority = 0 }) - p2 := seed("p2", func(i *types.Issue) { i.Priority = 2 }) - p4 := seed("p4", func(i *types.Issue) { i.Priority = 4 }) - - textT := seed("text-t", func(i *types.Issue) { i.Title = "a title carrying ttok-lff" }) - textD := seed("text-d", func(i *types.Issue) { i.Description = "a description carrying dtok-lff" }) - textN := seed("text-n", func(i *types.Issue) { i.Notes = "notes carrying ntok-lff" }) - - labeled := seed("labeled", func(i *types.Issue) { i.Labels = []string{label} }) - unlabeled := seed("unlabeled", nil) - - parent := seed("parent", func(i *types.Issue) { i.IssueType = types.TypeEpic }) - child := seed("child", nil) - if err := fixture.AddDependency(ctx, &types.Dependency{IssueID: child, DependsOnID: parent, Type: types.DepParentChild}, "seed"); err != nil { - t.Fatalf("seed edge %s -> %s: %v", child, parent, err) - } - loose := seed("loose", nil) - - typeTask := seed("type-task", nil) - typeBug := seed("type-bug", func(i *types.Issue) { i.IssueType = types.TypeBug }) - typeChore := seed("type-chore", func(i *types.Issue) { i.IssueType = types.TypeChore }) - - specced := seed("specced", func(i *types.Issue) { i.SpecID = spec + "design.md" }) - unspecced := seed("unspecced", nil) - - extRow := seed("ext", func(i *types.Issue) { i.ExternalRef = &external }) - extOther := seed("ext-other", func(i *types.Issue) { - other := fixture.IssuePrefix + "-other-99" - i.ExternalRef = &other - }) - - pinned := seed("pinned", func(i *types.Issue) { i.Pinned = true }) - unpinned := seed("unpinned", nil) - - deferred := seed("deferred", func(i *types.Issue) { i.DeferUntil = &future }) - undeferred := seed("undeferred", nil) - - overdue := seed("overdue", func(i *types.Issue) { i.DueAt = &past }) - dueLater := seed("due-later", func(i *types.Issue) { i.DueAt = &future }) - closedOverdue := seed("closed-overdue", func(i *types.Issue) { - i.Status = types.StatusClosed - closedAt := time.Now().UTC().Truncate(time.Second).Add(-time.Hour) - i.ClosedAt = &closedAt - i.DueAt = &past - }) - noDue := seed("no-due", nil) - - swarm := seed("mol-swarm", func(i *types.Issue) { i.MolType = types.MolTypeSwarm }) - patrol := seed("mol-patrol", func(i *types.Issue) { i.MolType = types.MolTypePatrol }) - - stOpen := seed("st-open", nil) - stProgress := seed("st-progress", func(i *types.Issue) { i.Status = types.StatusInProgress }) - stBlocked := seed("st-blocked", func(i *types.Issue) { i.Status = types.StatusBlocked }) - stClosed := seed("st-closed", func(i *types.Issue) { - i.Status = types.StatusClosed - closedAt := time.Now().UTC().Truncate(time.Second).Add(-time.Hour) - i.ClosedAt = &closedAt - }) - statusScope := readerIDFilter(stOpen, stProgress, stBlocked, stClosed) - - molType := func(m types.MolType) *publicops.MolType { return &m } - scoped := func(ids ...string) publicops.ListRequest { - return publicops.ListRequest{IDFilter: readerIDFilter(ids...), AllFlag: true} - } - with := func(req publicops.ListRequest, mutate func(*publicops.ListRequest)) publicops.ListRequest { - mutate(&req) - return req - } - - for _, test := range []struct { - name string - req publicops.ListRequest - want []string - }{ - {"Priority is exact", with(scoped(p0, p2, p4), func(r *publicops.ListRequest) { r.Priority = intp(2) }), []string{p2}}, - {"Priority 0 is a value, not unset", with(scoped(p0, p2, p4), func(r *publicops.ListRequest) { r.Priority = intp(0) }), []string{p0}}, - {"PriorityMin is inclusive", with(scoped(p0, p2, p4), func(r *publicops.ListRequest) { r.PriorityMin = intp(2) }), []string{p2, p4}}, - {"PriorityMax is inclusive", with(scoped(p0, p2, p4), func(r *publicops.ListRequest) { r.PriorityMax = intp(2) }), []string{p0, p2}}, - {"PriorityMin and PriorityMax compose", with(scoped(p0, p2, p4), func(r *publicops.ListRequest) { r.PriorityMin, r.PriorityMax = intp(1), intp(3) }), []string{p2}}, - - {"TitleContains is a case-insensitive substring", with(scoped(textT, textD, textN), func(r *publicops.ListRequest) { r.TitleContains = "TTOK-LFF" }), []string{textT}}, - {"DescContains reads the description only", with(scoped(textT, textD, textN), func(r *publicops.ListRequest) { r.DescContains = "dtok-lff" }), []string{textD}}, - {"NotesContains reads the notes only", with(scoped(textT, textD, textN), func(r *publicops.ListRequest) { r.NotesContains = "ntok-lff" }), []string{textN}}, - {"EmptyDesc keeps the rows with no description", with(scoped(textT, textD, textN), func(r *publicops.ListRequest) { r.EmptyDesc = true }), []string{textT, textN}}, - - {"NoLabels keeps the unlabeled row", with(scoped(labeled, unlabeled), func(r *publicops.ListRequest) { r.NoLabels = true }), []string{unlabeled}}, - {"LabelPattern is a glob", with(scoped(labeled, unlabeled), func(r *publicops.ListRequest) { r.LabelPattern = fixture.IssuePrefix + "-scope-lff*" }), []string{labeled}}, - {"LabelRegex is a regular expression", with(scoped(labeled, unlabeled), func(r *publicops.ListRequest) { r.LabelRegex = "-scope-lffp.t$" }), []string{labeled}}, - - {"NoParent keeps the top-level rows", with(scoped(parent, child, loose), func(r *publicops.ListRequest) { r.NoParent = true }), []string{parent, loose}}, - - {"ExcludeTypes drops the named type", with(scoped(typeTask, typeBug, typeChore), func(r *publicops.ListRequest) { r.ExcludeTypes = []string{"bug"} }), []string{typeTask, typeChore}}, - {"ExcludeTypes takes a comma-separated value", with(scoped(typeTask, typeBug, typeChore), func(r *publicops.ListRequest) { r.ExcludeTypes = []string{"bug,chore"} }), []string{typeTask}}, - - {"SpecPrefix is a prefix", with(scoped(specced, unspecced), func(r *publicops.ListRequest) { r.SpecPrefix = spec }), []string{specced}}, - - {"ExternalContains is a case-insensitive substring", with(scoped(extRow, extOther), func(r *publicops.ListRequest) { r.ExternalContains = "lff-EXT" }), []string{extRow}}, - {"ExternalRef is exact", with(scoped(extRow, extOther), func(r *publicops.ListRequest) { r.ExternalRef = external }), []string{extRow}}, - - {"AllFlag alone admits both pinned-flag states", scoped(pinned, unpinned), []string{pinned, unpinned}}, - {"PinnedFlag selects the flagged row", publicops.ListRequest{IDFilter: readerIDFilter(pinned, unpinned), PinnedFlag: true}, []string{pinned}}, - {"NoPinnedFlag holds the unflagged predicate under AllFlag", with(scoped(pinned, unpinned), func(r *publicops.ListRequest) { r.NoPinnedFlag = true }), []string{unpinned}}, - - {"DeferredFlag keeps the deferred row", with(scoped(deferred, undeferred), func(r *publicops.ListRequest) { r.DeferredFlag = true }), []string{deferred}}, - - {"OverdueFlag keeps the open row past its due date", with(scoped(overdue, dueLater, closedOverdue, noDue), func(r *publicops.ListRequest) { r.OverdueFlag = true }), []string{overdue}}, - - {"ExcludeStatus under AllFlag keeps every status not named", with(scoped(stOpen, stProgress, stBlocked, stClosed), func(r *publicops.ListRequest) { r.ExcludeStatus = []string{"closed"} }), []string{stOpen, stProgress, stBlocked}}, - {"ExcludeStatus takes repeated and comma-separated values", with(scoped(stOpen, stProgress, stBlocked, stClosed), func(r *publicops.ListRequest) { r.ExcludeStatus = []string{"blocked,in_progress", " closed "} }), []string{stOpen}}, - {"ExcludeStatus adds to the default exclusions rather than replacing them", publicops.ListRequest{IDFilter: statusScope, ExcludeStatus: []string{"blocked"}}, []string{stOpen, stProgress}}, - {"ExcludeStatus narrows an explicit Status", publicops.ListRequest{IDFilter: statusScope, Status: "open,blocked,closed", ExcludeStatus: []string{"blocked"}}, []string{stOpen, stClosed}}, - - {"MolType is exact", with(scoped(swarm, patrol), func(r *publicops.ListRequest) { r.MolType = molType(types.MolTypeSwarm) }), []string{swarm}}, - } { - page, err := fixture.Reader.List(ctx, test.req) - if err != nil { - t.Errorf("List (%s): %v", test.name, err) - continue - } - assertReaderPageIDSet(t, "List ("+test.name+")", page, test.want) - } - - // An exclusion naming no status in the workspace vocabulary is refused, - // never an exclusion that silently excludes nothing. - if _, err := fixture.Reader.List(ctx, publicops.ListRequest{IDFilter: statusScope, ExcludeStatus: []string{"clsoed"}}); !errors.Is(err, publicops.ErrValidation) { - t.Errorf("List with ExcludeStatus naming an unknown status = %v, want an error wrapping ErrValidation", err) - } - - _, err := fixture.Reader.List(ctx, publicops.ListRequest{ - IDFilter: readerIDFilter(pinned, unpinned), PinnedFlag: true, NoPinnedFlag: true, - }) - if !errors.Is(err, publicops.ErrValidation) { - t.Errorf("List with PinnedFlag and NoPinnedFlag = %v, want an error wrapping ErrValidation", err) - } - - _, err = fixture.Reader.List(ctx, publicops.ListRequest{ - IDFilter: readerIDFilter(parent, child, loose), ParentID: parent, NoParent: true, - }) - if !errors.Is(err, publicops.ErrValidation) { - t.Errorf("List with ParentID and NoParent = %v, want an error wrapping ErrValidation", err) - } -} - -// sortedCopy returns its arguments as a sorted slice. -func sortedCopy(ids ...string) []string { - out := slices.Clone(ids) - slices.Sort(out) - return out -} - // readerIssue builds the seed every case starts from: an open, unassigned, // unblocked task that qualifies for ready work. A case that needs it to fail one // of those tests changes the field it means to test and nothing else. diff --git a/backend/conformance/retention_epoch_contract.go b/backend/conformance/retention_epoch_contract.go deleted file mode 100644 index b2c0d100e..000000000 --- a/backend/conformance/retention_epoch_contract.go +++ /dev/null @@ -1,1295 +0,0 @@ -package conformance - -import ( - "context" - "errors" - "fmt" - "testing" - "time" -) - -// This file holds the contract for R20 (gastownhall/beads#6133, be-hs42e.1 -// §5-§9): what a store reports about an Address once retention, erasure, or -// reorganization has stopped it resolving to live state (R20-a..R20-l), and -// the store-epoch generation counter that selectively voids addresses minted -// under an earlier one (R20-m/n). -// -// RESTRICTION AND RETAINEDWINDOW ARE DECLARED IN expected_revision_contract.go -// — see that file's package-level note on where this suite's shared -// vocabulary lives and why. -// -// GONE AND UNKNOWN ARE NEVER CONFLATED (FR-05, FR-06): Gone means the store -// once held this Version and no longer does; Unknown means the store has no -// lineage knowledge of the Address at all. Every RetentionAnswer names its -// PRODUCING STORE (R20-i) so a case can catch a backend that silently -// answers for the wrong store — this matters because R20 is explicitly a -// per-store promise, not a global one: a Reorganization can leave one store -// still serving an Address that another has moved past. -// -// THIS FILE ORIGINALLY MADE THREE §4a CHOICES THE ARCHITECTURE DOC DOES NOT -// PIN. Two have since DIED — retired by fixes made within this same PR, -// before merge, in response to review. Kept here, marked DIED, so a reader -// tracing why RetentionAnswer/RetentionFixture look the way they do does not -// have to reconstruct the history from git log or PR comments: -// -// 1. Remove/Commit's refusal under an active Hold is a typed error, -// ErrRetentionHeld, rather than a non-accepted-shaped RetentionAnswer — -// the doc names the promise ("must refuse ... unless forced", R20-h) but -// not its shape. An error was chosen because Commit's success path -// already returns a RetentionAnswer describing a Version that is now -// GONE; reusing that same shape for a refusal would make "the hold -// worked" and "the removal happened" look like the same kind of value. -// (R17's Refusal reasons about the same tradeoff the other way, because -// R17 already had a typed non-error outcome shape to reuse — Remove has -// no such sibling shape to reuse here.) STILL STANDS. -// 2. DIED. ForceRemove's attribution used to be asserted only for shape, -// not content, because RetentionAnswer carried no attribution field. -// RetentionAnswer.Attribution now carries it, and -// RunForcingAHeldRemovalRecordsWhoWhenWhy asserts real content — see -// that function's own doc comment. -// 3. DIED. An address that no operation had touched used to resolve -// RestrictionLive BY DEFAULT, purely by naming convention — a fabricated -// Address, from the retired retentionAddress helper, that no store had -// ever actually minted. Every case that needs a real, live Address now -// mints one for real via RetentionFixture.Mint (see retentionMint). -// RestrictionUnknown for a foreign store (FR-06) is UNCHANGED by this: -// RunAStoreWithNoLineageKnowledgeAnswersUnknownNotGone and -// RunGoneIsDistinguishableFromUnknown still test the Unknown case via a -// second, foreign storeID that genuinely never saw the Address — not via -// an untouched-but-fabricated address on the primary one, which is the -// part that died. retentionAddress itself survives as the one helper -// RunAStoreWithNoLineageKnowledgeAnswersUnknownNotGone still uses, -// precisely because Unknown requires an Address no store ever minted. -// -// EVERY HOOK ON BOTH FIXTURES BELOW IS INDEPENDENTLY NILABLE — see the -// package-level note in expected_revision_contract.go. Every case -// nil-checks every hook it uses and SKIPS BY NAME when one is missing. - -// RetentionAnswer is what a store reports when asked about an Address. -type RetentionAnswer struct { - Restriction Restriction - // RetainedWindow is populated once some Addresses within the lineage - // have gone, naming the surviving range (R20-c) — AND, independently, - // whenever a Hold is protecting an Address from removal, naming that - // Address within the range it protects (R20-h). Nil means neither - // applies (e.g. Restriction is Live with no Hold in effect, or - // Unknown). - RetainedWindow *RetainedWindow - // ProducingStore names the store that produced this answer (R20-i). - ProducingStore string - // Attribution records who, when, and why produced this answer, for the - // operations that force their way past an ordinary refusal — currently - // just ForceRemove (R20-h2). Nil means this answer did not come from an - // attributed forcing operation (e.g. an ordinary Remove/Commit, or a - // Live/Unknown answer never touched by one). - Attribution *ChangeAttribution - // Epoch names the store-epoch generation this answer was evaluated - // under, so a caller can tell WHICH epoch a GoneReorganization answer - // belongs to, not just that reorganization is why the Address is gone - // (R20-n). Nil means this answer did not involve epoch reasoning (e.g. - // an ordinary Remove/Commit under R20-a..R20-l, evaluated by a store - // with no epoch concept at all). - Epoch *int -} - -// RemovalReason is why an Address was removed: R20 requires the three -// reasons stay pairwise distinguishable in the Restriction they produce. -type RemovalReason int - -const ( - RemovalReasonRetention RemovalReason = iota - RemovalReasonErasure - RemovalReasonReorganization -) - -func (r RemovalReason) String() string { - switch r { - case RemovalReasonRetention: - return "retention" - case RemovalReasonErasure: - return "erasure" - case RemovalReasonReorganization: - return "reorganization" - default: - return fmt.Sprintf("RemovalReason(%d)", int(r)) - } -} - -// RemovalPreview is Remove's enumerate-before-you-change half (R20-g): the -// Addresses about to be affected are reported before Commit makes the -// change real. -type RemovalPreview struct { - AffectedAddresses []Address - Commit func(ctx context.Context) (RetentionAnswer, error) -} - -// ErrRetentionHeld means a RemovalPreview's Commit was refused because an -// active Hold covers the Address (R20-h). See this file's package doc -// comment, §4a choice 1, for why this is a typed error rather than a -// non-accepted-shaped RetentionAnswer. -var ErrRetentionHeld = errors.New("retention: address is held") - -// RetentionResolve answers what a store currently reports for address. It is -// shared verbatim between RetentionFixture and EpochFixture: an epoch bump's -// effect on an Address (R20-n) is observed through the exact same read path -// as an ordinary removal's effect (R20-a..R20-l). -type RetentionResolve func(ctx context.Context, storeID string, address Address) (RetentionAnswer, error) - -// RetentionFixture supplies the capabilities under test for R20's removal, -// hold, forced-removal, and erasure semantics. -type RetentionFixture struct { - IssuePrefix string - - // Resolve reports storeID's current answer for address. A nil hook - // means this backend does not yet report retention state, and cases - // that need one skip with that reason. - Resolve RetentionResolve - - // Remove reports the Addresses a removal is about to affect - // (RemovalPreview.AffectedAddresses) before Commit makes the change - // real (R20-g). A nil hook means this backend has no removal primitive, - // and cases that need one skip with that reason. - Remove func(ctx context.Context, storeID string, address Address, reason RemovalReason) (RemovalPreview, error) - - // Hold prevents Remove/Commit from taking effect against address until - // released (R20-h). A nil hook means this backend has no hold - // primitive, and cases that need one skip with that reason. - Hold func(ctx context.Context, storeID string, address Address) error - - // ForceRemove removes address despite an active Hold, recording who, - // when, and why in the resulting Gone record (R20-h2). A nil hook means - // this backend has no forced-removal primitive, and the cases that need - // one skip with that reason. - ForceRemove func(ctx context.Context, storeID string, address Address, attribution ChangeAttribution, reason string) (RetentionAnswer, error) - - // Erase mints a corrected replacement Version for address rather than - // editing it in place (R20-l), returning the corrected Address. A nil - // hook means this backend has no erasure primitive, and the case that - // needs it skips with that reason. - Erase func(ctx context.Context, storeID string, address Address, correctedState string, attribution ChangeAttribution) (Address, error) - - // Mint mints a fresh Version for id under storeID, returning its - // Address — analogous to EpochFixture.MintUnderEpoch, without an epoch - // to peg it to. A nil hook means this backend has no way to mint a - // real, resolvable Address to test retention against, and cases that - // need one skip with that reason. Replaces the retired convention that - // a fabricated, never-minted Address defaults to RestrictionLive (see - // this file's package doc comment, formerly §4a choice 3). - Mint func(ctx context.Context, storeID, id string) (Address, error) -} - -// RunRemovalLeavesTheAddressAbleToAnswer pins R20-a/b: after a committed -// removal, the Address can still be resolved — it must answer, just not -// live. -func RunRemovalLeavesTheAddressAbleToAnswer(t *testing.T, ctx context.Context, fixture RetentionFixture) { - t.Helper() - if fixture.Remove == nil { - t.Skip("this backend has no removal primitive (Remove is nil)") - } - if fixture.Resolve == nil { - t.Skip("this backend does not yet report retention state (Resolve is nil)") - } - if fixture.Mint == nil { - t.Skip("this backend has no way to mint a real Address to test retention against (Mint is nil)") - } - store := retentionStore(fixture, "answerable") - address := retentionMint(t, ctx, fixture, store, "answerable") - - preview, err := fixture.Remove(ctx, store, address, RemovalReasonRetention) - if err != nil { - t.Fatalf("Remove(%s, %s): %v", store, address, err) - } - if _, err := preview.Commit(ctx); err != nil { - t.Fatalf("Commit removing %s: %v", address, err) - } - - answer, err := fixture.Resolve(ctx, store, address) - if err != nil { - t.Fatalf("Resolve(%s, %s) after removal: %v", store, address, err) - } - if answer.Restriction == RestrictionLive { - t.Fatalf("Resolve(%s) after Remove+Commit reports RestrictionLive, want a non-live Restriction: removal must leave the address able to answer, just not live", address) - } -} - -// RunRemovalReasonIsRetentionErasureOrReorganizationDistinctly pins R20-d: -// the three RemovalReasons produce pairwise-distinct Restrictions, so a -// caller can tell which kind of removal it is looking at from the answer -// alone. -func RunRemovalReasonIsRetentionErasureOrReorganizationDistinctly(t *testing.T, ctx context.Context, fixture RetentionFixture) { - t.Helper() - if fixture.Remove == nil { - t.Skip("this backend has no removal primitive (Remove is nil)") - } - if fixture.Resolve == nil { - t.Skip("this backend does not yet report retention state (Resolve is nil)") - } - if fixture.Mint == nil { - t.Skip("this backend has no way to mint a real Address to test retention against (Mint is nil)") - } - store := retentionStore(fixture, "reasons") - cases := []struct { - reason RemovalReason - want Restriction - }{ - {RemovalReasonRetention, RestrictionGoneRetention}, - {RemovalReasonErasure, RestrictionGoneErasure}, - {RemovalReasonReorganization, RestrictionGoneReorganization}, - } - seen := map[Restriction]bool{} - for _, c := range cases { - address := retentionMint(t, ctx, fixture, store, "reason-"+c.reason.String()) - preview, err := fixture.Remove(ctx, store, address, c.reason) - if err != nil { - t.Fatalf("Remove(%s, reason=%s): %v", address, c.reason, err) - } - if _, err := preview.Commit(ctx); err != nil { - t.Fatalf("Commit(%s, reason=%s): %v", address, c.reason, err) - } - answer, err := fixture.Resolve(ctx, store, address) - if err != nil { - t.Fatalf("Resolve(%s) after Remove(reason=%s): %v", address, c.reason, err) - } - if answer.Restriction != c.want { - t.Errorf("Remove(reason=%s) then Resolve = %s, want %s", c.reason, answer.Restriction, c.want) - } - if seen[answer.Restriction] { - t.Errorf("Restriction %s was already reported for a different RemovalReason; the three reasons must map to pairwise-distinct Restrictions", answer.Restriction) - } - seen[answer.Restriction] = true - } -} - -// RunRemovalReportsTheSurvivingRetainedWindow pins R20-c: a removal's answer -// carries a populated RetainedWindow naming the surviving range. -func RunRemovalReportsTheSurvivingRetainedWindow(t *testing.T, ctx context.Context, fixture RetentionFixture) { - t.Helper() - if fixture.Remove == nil { - t.Skip("this backend has no removal primitive (Remove is nil)") - } - if fixture.Mint == nil { - t.Skip("this backend has no way to mint a real Address to test retention against (Mint is nil)") - } - store := retentionStore(fixture, "window") - address := retentionMint(t, ctx, fixture, store, "window") - - preview, err := fixture.Remove(ctx, store, address, RemovalReasonRetention) - if err != nil { - t.Fatalf("Remove(%s): %v", address, err) - } - answer, err := preview.Commit(ctx) - if err != nil { - t.Fatalf("Commit(%s): %v", address, err) - } - if answer.RetainedWindow == nil { - t.Fatal("RetainedWindow = nil after a removal, want a populated window (R20-c)") - } - if answer.RetainedWindow.LowerBound == "" || answer.RetainedWindow.UpperBound == "" { - t.Errorf("RetainedWindow = %+v, want both bounds non-empty", answer.RetainedWindow) - } -} - -// RunRemovalNeverReassignsASurvivingAddress pins R20-f: removing one Address -// must never alter or reassign a different, surviving Address's own answer. -func RunRemovalNeverReassignsASurvivingAddress(t *testing.T, ctx context.Context, fixture RetentionFixture) { - t.Helper() - if fixture.Remove == nil { - t.Skip("this backend has no removal primitive (Remove is nil)") - } - if fixture.Resolve == nil { - t.Skip("this backend does not yet report retention state (Resolve is nil)") - } - if fixture.Mint == nil { - t.Skip("this backend has no way to mint a real Address to test retention against (Mint is nil)") - } - store := retentionStore(fixture, "survivor") - removed := retentionMint(t, ctx, fixture, store, "survivor-removed") - survivor := retentionMint(t, ctx, fixture, store, "survivor-kept") - - before, err := fixture.Resolve(ctx, store, survivor) - if err != nil { - t.Fatalf("Resolve(survivor) before removing its neighbor: %v", err) - } - - preview, err := fixture.Remove(ctx, store, removed, RemovalReasonRetention) - if err != nil { - t.Fatalf("Remove(%s): %v", removed, err) - } - if _, err := preview.Commit(ctx); err != nil { - t.Fatalf("Commit(%s): %v", removed, err) - } - - after, err := fixture.Resolve(ctx, store, survivor) - if err != nil { - t.Fatalf("Resolve(survivor) after removing its neighbor: %v", err) - } - if !sameRetentionAnswer(before, after) { - t.Errorf("removing a neighbor changed the survivor's own answer from %+v to %+v; removing one Address must never reassign or alter another", before, after) - } -} - -// RunGoneIsDistinguishableFromUnknown pins FR-05's core claim: the SAME -// Address, asked of the store that removed it versus a store that never -// held it, must not resolve identically — Gone (once held, now removed) and -// Unknown (no lineage knowledge at all) are never conflated. -func RunGoneIsDistinguishableFromUnknown(t *testing.T, ctx context.Context, fixture RetentionFixture) { - t.Helper() - if fixture.Remove == nil { - t.Skip("this backend has no removal primitive (Remove is nil)") - } - if fixture.Resolve == nil { - t.Skip("this backend does not yet report retention state (Resolve is nil)") - } - if fixture.Mint == nil { - t.Skip("this backend has no way to mint a real Address to test retention against (Mint is nil)") - } - store := retentionStore(fixture, "gonevunknown") - strangerStore := retentionStore(fixture, "gonevunknown-stranger") - gone := retentionMint(t, ctx, fixture, store, "gonevunknown-gone") - - preview, err := fixture.Remove(ctx, store, gone, RemovalReasonRetention) - if err != nil { - t.Fatalf("Remove(%s): %v", gone, err) - } - if _, err := preview.Commit(ctx); err != nil { - t.Fatalf("Commit(%s): %v", gone, err) - } - - goneAnswer, err := fixture.Resolve(ctx, store, gone) - if err != nil { - t.Fatalf("Resolve(gone, its own store): %v", err) - } - unknownAnswer, err := fixture.Resolve(ctx, strangerStore, gone) - if err != nil { - t.Fatalf("Resolve(gone's address, a store that never held it): %v", err) - } - if goneAnswer.Restriction == RestrictionLive || unknownAnswer.Restriction == RestrictionLive { - t.Fatalf("Live leaked into a Gone/Unknown comparison: gone=%s unknown=%s", goneAnswer.Restriction, unknownAnswer.Restriction) - } - if goneAnswer.Restriction == unknownAnswer.Restriction { - t.Errorf("the SAME Address resolved identically (%s) whether asked of the store that removed it or a store that never held it; FR-05 requires Gone and Unknown stay distinct", goneAnswer.Restriction) - } -} - -// RunAnAddressNeverResolvesToADifferentState guards against a mixed-up -// lookup key: two distinct Addresses on the same store, deliberately left -// in different Restrictions, must never have their answers cross- -// contaminate. -func RunAnAddressNeverResolvesToADifferentState(t *testing.T, ctx context.Context, fixture RetentionFixture) { - t.Helper() - if fixture.Remove == nil { - t.Skip("this backend has no removal primitive (Remove is nil)") - } - if fixture.Resolve == nil { - t.Skip("this backend does not yet report retention state (Resolve is nil)") - } - if fixture.Mint == nil { - t.Skip("this backend has no way to mint a real Address to test retention against (Mint is nil)") - } - store := retentionStore(fixture, "nomixup") - removedAddr := retentionMint(t, ctx, fixture, store, "nomixup-removed") - liveAddr := retentionMint(t, ctx, fixture, store, "nomixup-live") - - preview, err := fixture.Remove(ctx, store, removedAddr, RemovalReasonRetention) - if err != nil { - t.Fatalf("Remove(%s): %v", removedAddr, err) - } - if _, err := preview.Commit(ctx); err != nil { - t.Fatalf("Commit(%s): %v", removedAddr, err) - } - - removedAnswer, err := fixture.Resolve(ctx, store, removedAddr) - if err != nil { - t.Fatalf("Resolve(removed): %v", err) - } - liveAnswer, err := fixture.Resolve(ctx, store, liveAddr) - if err != nil { - t.Fatalf("Resolve(live): %v", err) - } - - if liveAnswer.Restriction != RestrictionLive { - t.Fatalf("Resolve(an address nothing ever removed) = %s, want RestrictionLive", liveAnswer.Restriction) - } - if removedAnswer.Restriction == RestrictionLive { - t.Fatal("Resolve(a removed address) = RestrictionLive, want a Gone restriction") - } - if sameRetentionAnswer(removedAnswer, liveAnswer) { - t.Errorf("Resolve(%s) and Resolve(%s) returned identical answers %+v; a mixed-up lookup key would look exactly like this", removedAddr, liveAddr, removedAnswer) - } -} - -// RunADestructiveOperationEnumeratesAffectedAddressesFirst pins R20-g: -// Remove reports the Addresses it is about to affect BEFORE anything -// changes. Checked by confirming that obtaining the preview alone — without -// calling Commit — leaves the address's own answer untouched, regardless of -// what that answer is. -func RunADestructiveOperationEnumeratesAffectedAddressesFirst(t *testing.T, ctx context.Context, fixture RetentionFixture) { - t.Helper() - if fixture.Remove == nil { - t.Skip("this backend has no removal primitive (Remove is nil)") - } - if fixture.Resolve == nil { - t.Skip("this backend does not yet report retention state (Resolve is nil)") - } - if fixture.Mint == nil { - t.Skip("this backend has no way to mint a real Address to test retention against (Mint is nil)") - } - store := retentionStore(fixture, "enumeratefirst") - address := retentionMint(t, ctx, fixture, store, "enumeratefirst") - - before, err := fixture.Resolve(ctx, store, address) - if err != nil { - t.Fatalf("Resolve before Remove: %v", err) - } - - preview, err := fixture.Remove(ctx, store, address, RemovalReasonRetention) - if err != nil { - t.Fatalf("Remove(%s): %v", address, err) - } - if len(preview.AffectedAddresses) == 0 { - t.Fatal("RemovalPreview.AffectedAddresses is empty, want the addresses this removal is about to affect (R20-g)") - } - - // Commit is deliberately NOT called yet. - after, err := fixture.Resolve(ctx, store, address) - if err != nil { - t.Fatalf("Resolve after Remove but before Commit: %v", err) - } - if !sameRetentionAnswer(before, after) { - t.Errorf("Resolve changed from %+v to %+v after Remove but before Commit; enumeration must precede the change, not cause it", before, after) - } -} - -// RunAHoldPreventsRemovalAndReportsInRetainedBounds pins R20-h: an active -// Hold makes Remove/Commit refuse, AND the held Address must show up -// positively in the reported RetainedWindow bounds — not merely be inferred -// from the absence of a Gone restriction. -// -// §4a choice 1 (see this file's package doc comment): Commit's refusal is -// the typed ErrRetentionHeld rather than a non-accepted-shaped -// RetentionAnswer. -func RunAHoldPreventsRemovalAndReportsInRetainedBounds(t *testing.T, ctx context.Context, fixture RetentionFixture) { - t.Helper() - if fixture.Hold == nil { - t.Skip("this backend has no hold primitive (Hold is nil)") - } - if fixture.Remove == nil { - t.Skip("this backend has no removal primitive (Remove is nil)") - } - if fixture.Resolve == nil { - t.Skip("this backend does not yet report retention state (Resolve is nil)") - } - if fixture.Mint == nil { - t.Skip("this backend has no way to mint a real Address to test retention against (Mint is nil)") - } - store := retentionStore(fixture, "held") - address := retentionMint(t, ctx, fixture, store, "held") - - if err := fixture.Hold(ctx, store, address); err != nil { - t.Fatalf("Hold(%s): %v", address, err) - } - - preview, err := fixture.Remove(ctx, store, address, RemovalReasonRetention) - if err != nil { - t.Fatalf("Remove(%s) under a hold: %v", address, err) - } - if _, err := preview.Commit(ctx); !errors.Is(err, ErrRetentionHeld) { - t.Fatalf("Commit(%s) under a hold error = %v, want ErrRetentionHeld", address, err) - } - - answer, err := fixture.Resolve(ctx, store, address) - if err != nil { - t.Fatalf("Resolve(%s) after a refused, held removal: %v", address, err) - } - if answer.Restriction == RestrictionGoneRetention || answer.Restriction == RestrictionGoneErasure || answer.Restriction == RestrictionGoneReorganization { - t.Errorf("Resolve(%s) reports %s after Commit was refused for being held, want it to remain out of any Gone restriction", address, answer.Restriction) - } - if answer.RetainedWindow == nil { - t.Fatalf("Resolve(%s) after a refused, held removal reports RetainedWindow = nil, want a populated window: R20-h's promise is that a Hold shows up IN the retained bounds, not merely as an absence of a Gone restriction", address) - } - if answer.RetainedWindow.LowerBound != address && answer.RetainedWindow.UpperBound != address { - t.Errorf("Resolve(%s).RetainedWindow = %+v, want at least one bound naming the held Address itself: R20-h's promise is that a Hold shows up IN the retained bounds, but this fixture's store name is not guaranteed fresh across runs on a persistent backend, so a prior run's Addresses may widen the window and only one edge is guaranteed to be exactly this Address", address, answer.RetainedWindow) - } -} - -// RunForcingAHeldRemovalRecordsWhoWhenWhy pins R20-h2: ForceRemove crosses an -// active Hold and must record who, when, and why — read back via -// RetentionAnswer.Attribution, not just accepted as parameters (formerly -// §4a choice 2, now DIED; see this file's package doc comment). -func RunForcingAHeldRemovalRecordsWhoWhenWhy(t *testing.T, ctx context.Context, fixture RetentionFixture) { - t.Helper() - if fixture.Hold == nil { - t.Skip("this backend has no hold primitive (Hold is nil)") - } - if fixture.ForceRemove == nil { - t.Skip("this backend has no forced-removal primitive (ForceRemove is nil)") - } - if fixture.Mint == nil { - t.Skip("this backend has no way to mint a real Address to test retention against (Mint is nil)") - } - store := retentionStore(fixture, "forced") - address := retentionMint(t, ctx, fixture, store, "forced") - - if err := fixture.Hold(ctx, store, address); err != nil { - t.Fatalf("Hold(%s): %v", address, err) - } - - actor := "retention-operator" - message := "legal hold expired" - attribution := ChangeAttribution{Actor: &actor, Message: &message, At: time.Now().UTC()} - - answer, err := fixture.ForceRemove(ctx, store, address, attribution, "hold-expired") - if err != nil { - t.Fatalf("ForceRemove(%s) despite an active hold: %v", address, err) - } - if answer.Restriction == RestrictionLive { - t.Errorf("ForceRemove(%s) reports RestrictionLive, want a Gone restriction", address) - } - if answer.ProducingStore != store { - t.Errorf("ForceRemove(%s).ProducingStore = %q, want %q", address, answer.ProducingStore, store) - } - if answer.Attribution == nil { - t.Fatalf("ForceRemove(%s).Attribution = nil, want the who/when/why recorded for this forced removal (R20-h2)", address) - } - if !sameAttribution(*answer.Attribution, attribution) { - t.Errorf("ForceRemove(%s).Attribution = %+v, want %+v (R20-h2: who/when/why must be recorded, not just accepted as parameters)", address, *answer.Attribution, attribution) - } -} - -// RunEveryRetentionAnswerNamesItsProducingStore pins R20-i for both a live -// and a non-live answer: ProducingStore always names the store that -// actually produced the answer. -func RunEveryRetentionAnswerNamesItsProducingStore(t *testing.T, ctx context.Context, fixture RetentionFixture) { - t.Helper() - if fixture.Resolve == nil { - t.Skip("this backend does not yet report retention state (Resolve is nil)") - } - if fixture.Remove == nil { - t.Skip("this backend has no removal primitive (Remove is nil)") - } - if fixture.Mint == nil { - t.Skip("this backend has no way to mint a real Address to test retention against (Mint is nil)") - } - storeA := retentionStore(fixture, "namesA") - storeB := retentionStore(fixture, "namesB") - liveAddr := retentionMint(t, ctx, fixture, storeA, "names-live") - goneAddr := retentionMint(t, ctx, fixture, storeB, "names-gone") - - preview, err := fixture.Remove(ctx, storeB, goneAddr, RemovalReasonRetention) - if err != nil { - t.Fatalf("Remove(%s): %v", goneAddr, err) - } - if _, err := preview.Commit(ctx); err != nil { - t.Fatalf("Commit(%s): %v", goneAddr, err) - } - - liveAnswer, err := fixture.Resolve(ctx, storeA, liveAddr) - if err != nil { - t.Fatalf("Resolve(%s, %s): %v", storeA, liveAddr, err) - } - if liveAnswer.ProducingStore != storeA { - t.Errorf("Resolve(%s, ...).ProducingStore = %q, want %q (R20-i: live answer)", storeA, liveAnswer.ProducingStore, storeA) - } - - goneAnswer, err := fixture.Resolve(ctx, storeB, goneAddr) - if err != nil { - t.Fatalf("Resolve(%s, %s): %v", storeB, goneAddr, err) - } - if goneAnswer.ProducingStore != storeB { - t.Errorf("Resolve(%s, ...).ProducingStore = %q, want %q (R20-i: non-live answer)", storeB, goneAnswer.ProducingStore, storeB) - } -} - -// RunAStoreWithNoLineageKnowledgeAnswersUnknownNotGone pins FR-06: a store -// asked about a store/Address pair it has no lineage relationship with at -// all — never received it, never removed it — must answer Unknown, not one -// of the Gone restrictions. -func RunAStoreWithNoLineageKnowledgeAnswersUnknownNotGone(t *testing.T, ctx context.Context, fixture RetentionFixture) { - t.Helper() - if fixture.Resolve == nil { - t.Skip("this backend does not yet report retention state (Resolve is nil)") - } - store := retentionStore(fixture, "nolineage") - address := retentionAddress(fixture, "nolineage-nevertracked") - - answer, err := fixture.Resolve(ctx, store, address) - if err != nil { - t.Fatalf("Resolve(%s, %s): %v", store, address, err) - } - if answer.Restriction != RestrictionUnknown { - t.Errorf("Resolve on a store/address pair with no lineage relationship = %s, want RestrictionUnknown (FR-06)", answer.Restriction) - } -} - -// RunAStoreThatRemovesStateStillAnswersGoneDurably pins R20-j: a removed -// Address's Gone answer is durable — two successive reads must agree, not -// merely happen to agree once. -func RunAStoreThatRemovesStateStillAnswersGoneDurably(t *testing.T, ctx context.Context, fixture RetentionFixture) { - t.Helper() - if fixture.Remove == nil { - t.Skip("this backend has no removal primitive (Remove is nil)") - } - if fixture.Resolve == nil { - t.Skip("this backend does not yet report retention state (Resolve is nil)") - } - if fixture.Mint == nil { - t.Skip("this backend has no way to mint a real Address to test retention against (Mint is nil)") - } - store := retentionStore(fixture, "durable") - address := retentionMint(t, ctx, fixture, store, "durable") - - preview, err := fixture.Remove(ctx, store, address, RemovalReasonErasure) - if err != nil { - t.Fatalf("Remove(%s): %v", address, err) - } - if _, err := preview.Commit(ctx); err != nil { - t.Fatalf("Commit(%s): %v", address, err) - } - - first, err := fixture.Resolve(ctx, store, address) - if err != nil { - t.Fatalf("Resolve(%s), first read: %v", address, err) - } - second, err := fixture.Resolve(ctx, store, address) - if err != nil { - t.Fatalf("Resolve(%s), second read: %v", address, err) - } - if first.Restriction == RestrictionLive { - t.Fatalf("Resolve(%s) after removal = RestrictionLive, want a Gone restriction", address) - } - if !sameRetentionAnswer(first, second) { - t.Errorf("two successive Resolve(%s) calls disagreed: %+v vs %+v; a removal must answer Gone DURABLY, not from a cache that can revert", address, first, second) - } -} - -// RunErasureMintsACorrectedVersionRatherThanEditingInPlace pins R20-l: Erase -// returns a NEW Address for the corrected state rather than editing the -// original in place — the original resolves GoneErasure, the corrected -// replacement resolves Live. -func RunErasureMintsACorrectedVersionRatherThanEditingInPlace(t *testing.T, ctx context.Context, fixture RetentionFixture) { - t.Helper() - if fixture.Erase == nil { - t.Skip("this backend has no erasure primitive (Erase is nil)") - } - if fixture.Resolve == nil { - t.Skip("this backend does not yet report retention state (Resolve is nil)") - } - if fixture.Mint == nil { - t.Skip("this backend has no way to mint a real Address to test retention against (Mint is nil)") - } - store := retentionStore(fixture, "erasure") - original := retentionMint(t, ctx, fixture, store, "erasure-original") - actor := "retention-operator" - attribution := ChangeAttribution{Actor: &actor, At: time.Now().UTC()} - - corrected, err := fixture.Erase(ctx, store, original, `{"corrected":true}`, attribution) - if err != nil { - t.Fatalf("Erase(%s): %v", original, err) - } - if corrected == original { - t.Fatalf("Erase(%s) returned the SAME Address, want a NEW one (R20-l: erasure mints a corrected version rather than editing in place)", original) - } - - originalAnswer, err := fixture.Resolve(ctx, store, original) - if err != nil { - t.Fatalf("Resolve(original): %v", err) - } - if originalAnswer.Restriction != RestrictionGoneErasure { - t.Errorf("Resolve(the erased original) = %s, want RestrictionGoneErasure", originalAnswer.Restriction) - } - - correctedAnswer, err := fixture.Resolve(ctx, store, corrected) - if err != nil { - t.Fatalf("Resolve(corrected): %v", err) - } - if correctedAnswer.Restriction != RestrictionLive { - t.Errorf("Resolve(the corrected replacement) = %s, want RestrictionLive", correctedAnswer.Restriction) - } -} - -// EpochBumpTrigger enumerates the only three events R20-m allows to advance -// a store's epoch. -type EpochBumpTrigger int - -const ( - EpochBumpTriggerRestore EpochBumpTrigger = iota - EpochBumpTriggerDestructiveReinit - EpochBumpTriggerTokenSchemeChange -) - -func (tr EpochBumpTrigger) String() string { - switch tr { - case EpochBumpTriggerRestore: - return "restore" - case EpochBumpTriggerDestructiveReinit: - return "destructive-reinit" - case EpochBumpTriggerTokenSchemeChange: - return "token-scheme-change" - default: - return fmt.Sprintf("EpochBumpTrigger(%d)", int(tr)) - } -} - -// EpochFixture supplies the capabilities under test for R20-m/n: the -// store-epoch generation counter and its selective effect on addresses -// minted under an earlier epoch. -type EpochFixture struct { - IssuePrefix string - - // CurrentEpoch reports storeID's current epoch generation. A nil hook - // means this backend has no epoch concept yet, and cases that need one - // skip with that reason. - CurrentEpoch func(ctx context.Context, storeID string) (int, error) - - // BumpEpoch advances storeID's epoch for the given trigger, returning - // the new epoch. A nil hook means this backend has no epoch concept - // yet, and cases that need one skip with that reason. - BumpEpoch func(ctx context.Context, storeID string, trigger EpochBumpTrigger) (int, error) - - // MintUnderEpoch mints a fresh Version for id under storeID's current - // epoch, returning its Address. A nil hook means this backend has no - // way to mint a Version under a known epoch, and cases that need one - // skip with that reason. - MintUnderEpoch func(ctx context.Context, storeID, id string) (Address, error) - - // StillServes reports whether storeID still serves the Version named by - // a prior-epoch address, despite the epoch having moved on. A nil hook - // means this backend cannot report that, and the case that needs it - // skips with that reason. - StillServes func(ctx context.Context, storeID string, address Address) (bool, error) - - // Resolve is the SAME RetentionResolve type RetentionFixture uses — an - // epoch bump's effect on an Address is observed through the identical - // read path as an ordinary removal. A nil hook means this backend does - // not yet report retention state, and cases that need one skip with - // that reason. - Resolve RetentionResolve - - // CurrentAddressFor reports the Address a still-served, prior-epoch - // Version now resolves to under the new epoch. A nil hook means this - // backend cannot report that, and the case that needs it skips with - // that reason. - // - // It only reads: it mints nothing and changes no state. It returns the - // address it was given when nothing carried the Version to a new one, - // and an error for an address the store does not serve. - CurrentAddressFor func(ctx context.Context, storeID string, oldAddress Address) (Address, error) - - // LoseVersion records that storeID no longer serves the Version named by - // address. That address, and every address the same Version was carried - // to or from by a token-scheme change, resolves GoneReorganization from - // then on, in every later epoch: an epoch voids what the store lost, - // never what it still serves (R20-n). It is idempotent, never revives - // anything, and returns an error for an address storeID never minted. A - // nil LoseVersion means this backend cannot record a loss, and the cases - // that need one skip with that reason. - LoseVersion func(ctx context.Context, storeID string, address Address) error -} - -// RunAnEpochBumpIsTriggeredOnlyByRestoreReinitOrSchemeChange pins R20-m: a -// store's epoch advances for exactly the three named triggers. -// -// THIS CASE CANNOT PROVE A FOURTH, UNINVITED TRIGGER NEVER BUMPS THE EPOCH -// in some real backend — enumerating "never" over an open set of possible -// call sites is not something an interface-level test can do. That is -// Phase 3's implementation discipline to hold, not something this suite can -// enforce (NFR-03's blind spot, mirroring canonicalizeForContract's stated -// blind spot in metadata_cas_contract.go). -func RunAnEpochBumpIsTriggeredOnlyByRestoreReinitOrSchemeChange(t *testing.T, ctx context.Context, fixture EpochFixture) { - t.Helper() - if fixture.BumpEpoch == nil { - t.Skip("this backend has no epoch concept yet (BumpEpoch is nil)") - } - if fixture.CurrentEpoch == nil { - t.Skip("this backend cannot report its current epoch (CurrentEpoch is nil)") - } - store := epochStore(fixture, "triggers") - - for _, trigger := range []EpochBumpTrigger{ - EpochBumpTriggerRestore, - EpochBumpTriggerDestructiveReinit, - EpochBumpTriggerTokenSchemeChange, - } { - before, err := fixture.CurrentEpoch(ctx, store) - if err != nil { - t.Fatalf("CurrentEpoch before %s: %v", trigger, err) - } - after, err := fixture.BumpEpoch(ctx, store, trigger) - if err != nil { - t.Fatalf("BumpEpoch(%s): %v", trigger, err) - } - if after <= before { - t.Errorf("BumpEpoch(%s) = %d, want an epoch greater than the pre-bump value %d", trigger, after, before) - } - observed, err := fixture.CurrentEpoch(ctx, store) - if err != nil { - t.Fatalf("CurrentEpoch after %s: %v", trigger, err) - } - if observed != after { - t.Errorf("CurrentEpoch after BumpEpoch(%s) = %d, want the bumped value %d", trigger, observed, after) - } - } -} - -// RunEpochBumpVoidsOnlyAddressesOfVersionsNoLongerServed pins R20-n — "Rev7 -// tightening... the clause most likely to be gotten wrong" in the -// architecture doc's own words: an epoch voids what the store lost, never -// what it still serves. -// -// Survival is RECORDED, never inferred (gastownhall/beads#6664, bee-ghosttrack -// review 5360880888, Major 1). An earlier form of this case made a Version -// survive by minting it again in every later epoch, and treated a Version -// nobody minted again as lost. That reads loss off the absence of a mint, so -// it could not express a Version the store lost after minting it, and it -// voided every survivor that nobody happened to mint again. Here a restore or -// a destructive reinit leaves every address row alone: an address the store -// still serves stays Live at its own address with no upkeep, and the only way -// an address goes Gone is LoseVersion saying so. -// -// Each phase is a named subtest, so a backend that cannot record a loss skips -// only the phases that need LoseVersion, and a failure names its phase. -func RunEpochBumpVoidsOnlyAddressesOfVersionsNoLongerServed(t *testing.T, ctx context.Context, fixture EpochFixture) { - t.Helper() - epochSkipUnlessReady(t, fixture, false) - - // survivor-and-lost: of two Versions minted before a transition, the one - // the store loses goes Gone and the other stays Live at the address it - // already had, with no re-mint, for both triggers that leave addresses - // alone. - t.Run("survivor-and-lost", func(t *testing.T) { - if fixture.LoseVersion == nil { - t.Skip(epochLoseVersionSkip) - } - for _, trigger := range []EpochBumpTrigger{EpochBumpTriggerRestore, EpochBumpTriggerDestructiveReinit} { - t.Run(trigger.String(), func(t *testing.T) { - store := epochStore(fixture, "survivor-"+trigger.String()) - addrA := epochMintAddress(t, ctx, fixture, store, "record-a") - addrB := epochMintAddress(t, ctx, fixture, store, "record-b") - - newEpoch := epochBumpTo(t, ctx, fixture, store, trigger) - epochLoseAddress(t, ctx, fixture, store, addrB) - - epochRequireLive(t, ctx, fixture, store, addrA, newEpoch, - "record-a was never lost, so the transition leaves it served at its own address with no re-mint (R20-n's exception)") - epochRequireGone(t, ctx, fixture, store, addrB, newEpoch, - "record-b was lost, so the transition voids it (R20-n)") - - current := epochCurrentAddress(t, ctx, fixture, store, addrA) - epochRequireLive(t, ctx, fixture, store, current, newEpoch, - "the address CurrentAddressFor reports for a survivor must be served") - epochRequireLive(t, ctx, fixture, store, addrA, newEpoch, - "asking CurrentAddressFor must not retire or move the survivor's address") - - if lostCurrent, err := fixture.CurrentAddressFor(ctx, store, addrB); err == nil { - t.Errorf("CurrentAddressFor(a lost address) = %s with no error, want an error: a lost Version has no current address", lostCurrent) - } - epochRequireGone(t, ctx, fixture, store, addrB, newEpoch, - "CurrentAddressFor on a lost address must not bring it back") - }) - } - }) - - // gone-is-terminal: once an address is lost it stays lost. A later mint of - // the same id starts a new lineage under a new address, and neither that - // mint nor CurrentAddressFor revives the lost one (bee-ghosttrack's - // assertion for Major 1: a read-only lookup must not mint a replacement - // that makes a lost address look served). - t.Run("gone-is-terminal", func(t *testing.T) { - if fixture.LoseVersion == nil { - t.Skip(epochLoseVersionSkip) - } - store := epochStore(fixture, "terminal") - lost := epochMintAddress(t, ctx, fixture, store, "record-g") - epochBumpTo(t, ctx, fixture, store, EpochBumpTriggerRestore) - epochLoseAddress(t, ctx, fixture, store, lost) - laterEpoch := epochBumpTo(t, ctx, fixture, store, EpochBumpTriggerRestore) - - fresh := epochMintAddress(t, ctx, fixture, store, "record-g") - if fresh == lost { - t.Fatalf("MintUnderEpoch(record-g) returned %s, the address already lost: a lost address is never handed out again", lost) - } - epochRequireLive(t, ctx, fixture, store, fresh, laterEpoch, - "a fresh mint of the id is a new lineage and is served") - epochRequireGone(t, ctx, fixture, store, lost, laterEpoch, - "a later mint of the same id must not revive a lost address") - - if replacement, err := fixture.CurrentAddressFor(ctx, store, lost); err == nil { - t.Errorf("CurrentAddressFor(a lost address) = %s with no error, want an error: asking where a lost address resolves must not mint a replacement", replacement) - } - epochRequireGone(t, ctx, fixture, store, lost, laterEpoch, - "CurrentAddressFor on a lost address must not revive it") - - // Losing it a second time is a no-op that leaves the fresh lineage - // alone. - epochLoseAddress(t, ctx, fixture, store, lost) - epochRequireLive(t, ctx, fixture, store, fresh, laterEpoch, - "losing the old address again must leave the fresh lineage served") - }) - - // survival-needs-no-upkeep: with nothing lost, an address stays Live across - // any number of counter-only transitions without being minted again, and a - // later mint of the same id neither retires it nor revives anything. This - // is the phase the earlier, mint-driven form failed: it answered Gone for - // every survivor nobody minted again. - t.Run("survival-needs-no-upkeep", func(t *testing.T) { - store := epochStore(fixture, "upkeep") - kept := epochMintAddress(t, ctx, fixture, store, "record-c") - epochBumpTo(t, ctx, fixture, store, EpochBumpTriggerRestore) - epochBumpTo(t, ctx, fixture, store, EpochBumpTriggerDestructiveReinit) - finalEpoch := epochBumpTo(t, ctx, fixture, store, EpochBumpTriggerRestore) - - epochRequireLive(t, ctx, fixture, store, kept, finalEpoch, - "nothing was lost, so three counter-only transitions leave the address served without a re-mint") - - fresh := epochMintAddress(t, ctx, fixture, store, "record-c") - if fresh == kept { - t.Fatalf("MintUnderEpoch(record-c) returned %s again after three bumps, want a new address for the new epoch", kept) - } - epochRequireLive(t, ctx, fixture, store, kept, finalEpoch, - "a later mint of the same id must not retire a served address") - epochRequireLive(t, ctx, fixture, store, fresh, finalEpoch, - "the fresh mint is served") - - current := epochCurrentAddress(t, ctx, fixture, store, kept) - epochRequireLive(t, ctx, fixture, store, current, finalEpoch, - "the current address of a survivor must be served") - epochRequireLive(t, ctx, fixture, store, kept, finalEpoch, - "asking for the current address must not change the survivor") - }) -} - -// RunTokenSchemeChangeCarriesLiveAddressesAndOnlyThose pins R20-n's retained -// mapping for the one trigger that changes how addresses are encoded. A -// token-scheme-change bump gives every Version the store still serves a new -// address under the new epoch and keeps the old address resolving, so a holder -// of the old address is neither stranded nor pointed at something else. A -// Version the store already lost gets no new address and stays Gone. -func RunTokenSchemeChangeCarriesLiveAddressesAndOnlyThose(t *testing.T, ctx context.Context, fixture EpochFixture) { - t.Helper() - epochSkipUnlessReady(t, fixture, true) - store := epochStore(fixture, "scheme-change") - - addrA := epochMintAddress(t, ctx, fixture, store, "record-a") - addrB := epochMintAddress(t, ctx, fixture, store, "record-b") - // record-b is lost in the epoch it was minted in, which a store may do. - epochLoseAddress(t, ctx, fixture, store, addrB) - - newEpoch := epochBumpTo(t, ctx, fixture, store, EpochBumpTriggerTokenSchemeChange) - - epochRequireLive(t, ctx, fixture, store, addrA, newEpoch, - "record-a is still served after the scheme change, through the retained mapping") - epochRequireGone(t, ctx, fixture, store, addrB, newEpoch, - "record-b was lost before the scheme change, which must not carry it") - - carried := epochCurrentAddress(t, ctx, fixture, store, addrA) - if carried == addrA { - t.Errorf("CurrentAddressFor(%s) = the same address after a token-scheme change, want a new one: a scheme change re-encodes every served address, which is what tells a carry from a no-op", addrA) - } - epochRequireLive(t, ctx, fixture, store, carried, newEpoch, - "the carried address must be served under the new epoch") - epochRequireLive(t, ctx, fixture, store, addrA, newEpoch, - "the old address must keep resolving after its carried address is looked up") - - if again := epochCurrentAddress(t, ctx, fixture, store, carried); again != carried { - t.Errorf("CurrentAddressFor(%s) = %s, want the carried address itself: from the newest address the mapping is already current", carried, again) - } - if lostCurrent, err := fixture.CurrentAddressFor(ctx, store, addrB); err == nil { - t.Errorf("CurrentAddressFor(a lost address) = %s with no error, want an error: a lost Version is not carried", lostCurrent) - } -} - -// RunACarriedAddressKeepsResolvingAcrossLaterBumps pins that a carried mapping -// is recorded once and then simply persists: later counter-only transitions do -// not change it, and a second token-scheme change extends it to the newest -// address without breaking any older one. Every address of the Version points -// directly at the newest address (one hop from any member), never along a -// chain. -func RunACarriedAddressKeepsResolvingAcrossLaterBumps(t *testing.T, ctx context.Context, fixture EpochFixture) { - t.Helper() - epochSkipUnlessReady(t, fixture, false) - store := epochStore(fixture, "carried-later") - - addrA := epochMintAddress(t, ctx, fixture, store, "record-a") - epochBumpTo(t, ctx, fixture, store, EpochBumpTriggerRestore) - epochBumpTo(t, ctx, fixture, store, EpochBumpTriggerTokenSchemeChange) - addrA1 := epochCurrentAddress(t, ctx, fixture, store, addrA) - if addrA1 == addrA { - t.Fatalf("CurrentAddressFor(%s) = the same address after a token-scheme change, want a new one", addrA) - } - - // Two counter-only transitions, with nothing minted in between. - epochBumpTo(t, ctx, fixture, store, EpochBumpTriggerRestore) - counterEpoch := epochBumpTo(t, ctx, fixture, store, EpochBumpTriggerDestructiveReinit) - epochRequireLive(t, ctx, fixture, store, addrA, counterEpoch, - "the original address must still resolve after counter-only transitions") - epochRequireLive(t, ctx, fixture, store, addrA1, counterEpoch, - "the carried address must still resolve after counter-only transitions") - if got := epochCurrentAddress(t, ctx, fixture, store, addrA); got != addrA1 { - t.Errorf("CurrentAddressFor(%s) = %s, want %s: a counter-only transition does not change the mapping", addrA, got, addrA1) - } - - // A second scheme change carries the Version again. - secondEpoch := epochBumpTo(t, ctx, fixture, store, EpochBumpTriggerTokenSchemeChange) - addrA2 := epochCurrentAddress(t, ctx, fixture, store, addrA) - if addrA2 == addrA || addrA2 == addrA1 { - t.Fatalf("CurrentAddressFor(%s) = %s after a second scheme change, want an address different from %s and %s", addrA, addrA2, addrA, addrA1) - } - for _, member := range []Address{addrA, addrA1, addrA2} { - epochRequireLive(t, ctx, fixture, store, member, secondEpoch, - "every address of a carried Version must resolve after a second scheme change") - } - if got := epochCurrentAddress(t, ctx, fixture, store, addrA1); got != addrA2 { - t.Errorf("CurrentAddressFor(%s) = %s, want %s: every address of the Version points straight at the newest one", addrA1, got, addrA2) - } - - finalEpoch := epochBumpTo(t, ctx, fixture, store, EpochBumpTriggerRestore) - epochRequireLive(t, ctx, fixture, store, addrA2, finalEpoch, - "the newest carried address must still resolve after a later restore") -} - -// RunALostAddressStaysGoneAcrossASchemeChange pins that loss is terminal even -// across a token-scheme change. Losing a Version loses every address of its -// lineage at once, and a later scheme change must not carry a lost lineage -// back into service. -func RunALostAddressStaysGoneAcrossASchemeChange(t *testing.T, ctx context.Context, fixture EpochFixture) { - t.Helper() - epochSkipUnlessReady(t, fixture, true) - store := epochStore(fixture, "lost-scheme-change") - - addrA := epochMintAddress(t, ctx, fixture, store, "record-a") - epochBumpTo(t, ctx, fixture, store, EpochBumpTriggerTokenSchemeChange) - addrA1 := epochCurrentAddress(t, ctx, fixture, store, addrA) - lostEpoch := epochBumpTo(t, ctx, fixture, store, EpochBumpTriggerRestore) - - // One lineage, two addresses: losing either loses both. - epochLoseAddress(t, ctx, fixture, store, addrA) - epochRequireGone(t, ctx, fixture, store, addrA, lostEpoch, - "the original address of a lost Version must be Gone") - epochRequireGone(t, ctx, fixture, store, addrA1, lostEpoch, - "the carried address of a lost Version must be Gone too") - - schemeEpoch := epochBumpTo(t, ctx, fixture, store, EpochBumpTriggerTokenSchemeChange) - epochRequireGone(t, ctx, fixture, store, addrA, schemeEpoch, - "a later scheme change must not carry a lost lineage back into service") - epochRequireGone(t, ctx, fixture, store, addrA1, schemeEpoch, - "a later scheme change must not carry a lost lineage back into service") - for _, lostAddress := range []Address{addrA, addrA1} { - if current, err := fixture.CurrentAddressFor(ctx, store, lostAddress); err == nil { - t.Errorf("CurrentAddressFor(%s) = %s with no error, want an error: a lost lineage has no current address", lostAddress, current) - } - } - - fresh := epochMintAddress(t, ctx, fixture, store, "record-a") - if fresh == addrA || fresh == addrA1 { - t.Fatalf("MintUnderEpoch(record-a) returned %s, an address of the lost lineage: a lost address is never handed out again", fresh) - } - epochRequireLive(t, ctx, fixture, store, fresh, schemeEpoch, - "a fresh mint of the id after the loss is a new lineage and is served") -} - -// --- fixture helpers ------------------------------------------------------- - -// retentionStore names a storeID namespaced by the fixture's IssuePrefix and -// tag, so cases that need more than one store (e.g. to probe FR-06's -// no-lineage-knowledge case) get independent ones. -func retentionStore(fixture RetentionFixture, tag string) string { - return fixture.IssuePrefix + "-retention-store-" + tag -} - -// retentionAddress builds a fresh, syntactically valid Address namespaced by -// the fixture's IssuePrefix and tag, WITHOUT ever giving it to any store. -// RunAStoreWithNoLineageKnowledgeAnswersUnknownNotGone is the only remaining -// caller: Unknown specifically means a store with no lineage knowledge of -// the Address at all (FR-06), so fabricating one that no store has ever -// seen is the correct construction here, not a shortcut — unlike every -// other case in this file, which now mints a real Address via -// fixture.Mint/retentionMint instead of relying on the retired -// Live-baseline convention (see this file's package doc comment, formerly -// §4a choice 3). -func retentionAddress(fixture RetentionFixture, tag string) Address { - return Address(fixture.IssuePrefix + "-retention-address-" + tag) -} - -// retentionMint mints a fresh Address for tag on store via fixture.Mint, -// fataling the case if minting fails — analogous to how -// EpochFixture.MintUnderEpoch is called directly in this file's Epoch -// cases, without its own wrapper. Cases use this in place of the retired -// retentionAddress convention (formerly §4a choice 3, now DIED — see this -// file's package doc comment) for any Address that must resolve as a real, -// store-established Version. -func retentionMint(t *testing.T, ctx context.Context, fixture RetentionFixture, store, tag string) Address { - t.Helper() - address, err := fixture.Mint(ctx, store, tag) - if err != nil { - t.Fatalf("Mint(%s, %s): %v", store, tag, err) - } - return address -} - -// epochStore names a storeID namespaced by the fixture's IssuePrefix and tag. -func epochStore(fixture EpochFixture, tag string) string { - return fixture.IssuePrefix + "-epoch-store-" + tag -} - -// epochLoseVersionSkip is the skip reason of every phase that needs -// EpochFixture.LoseVersion. -const epochLoseVersionSkip = "this backend cannot record that the store lost a Version (LoseVersion is nil)" - -// epochSkipUnlessReady skips the case when the fixture lacks a capability the -// epoch cases need. The cases that record a loss also need LoseVersion. The -// all-nil wiring test runs every case against an empty fixture, so each case -// must call this before it touches any of them. -func epochSkipUnlessReady(t *testing.T, fixture EpochFixture, needsLoseVersion bool) { - t.Helper() - if fixture.MintUnderEpoch == nil { - t.Skip("this backend has no way to mint a Version under a known epoch (MintUnderEpoch is nil)") - } - if fixture.BumpEpoch == nil { - t.Skip("this backend has no epoch concept yet (BumpEpoch is nil)") - } - if fixture.StillServes == nil { - t.Skip("this backend cannot report whether it still serves a prior-epoch Version (StillServes is nil)") - } - if fixture.Resolve == nil { - t.Skip("this backend does not yet report retention state (Resolve is nil)") - } - if fixture.CurrentAddressFor == nil { - t.Skip("this backend cannot report a still-served Version's new Address (CurrentAddressFor is nil)") - } - if needsLoseVersion && fixture.LoseVersion == nil { - t.Skip(epochLoseVersionSkip) - } -} - -// epochMintAddress mints id on store, failing the case if minting fails. -func epochMintAddress(t *testing.T, ctx context.Context, fixture EpochFixture, store, id string) Address { - t.Helper() - address, err := fixture.MintUnderEpoch(ctx, store, id) - if err != nil { - t.Fatalf("MintUnderEpoch(%s, %s): %v", store, id, err) - } - return address -} - -// epochBumpTo bumps store's epoch for trigger and returns the epoch it -// reports. Epochs are captured from the return value, never assumed: the -// counter is one per database, shared by every store the suite uses. -func epochBumpTo(t *testing.T, ctx context.Context, fixture EpochFixture, store string, trigger EpochBumpTrigger) int { - t.Helper() - epoch, err := fixture.BumpEpoch(ctx, store, trigger) - if err != nil { - t.Fatalf("BumpEpoch(%s, %s): %v", store, trigger, err) - } - return epoch -} - -// epochLoseAddress records that store lost the Version behind address. -func epochLoseAddress(t *testing.T, ctx context.Context, fixture EpochFixture, store string, address Address) { - t.Helper() - if err := fixture.LoseVersion(ctx, store, address); err != nil { - t.Fatalf("LoseVersion(%s, %s): %v", store, address, err) - } -} - -// epochCurrentAddress asks where address now resolves, failing the case on an -// error or an empty answer. -func epochCurrentAddress(t *testing.T, ctx context.Context, fixture EpochFixture, store string, address Address) Address { - t.Helper() - current, err := fixture.CurrentAddressFor(ctx, store, address) - if err != nil { - t.Fatalf("CurrentAddressFor(%s, %s): %v", store, address, err) - } - if current == "" { - t.Fatalf("CurrentAddressFor(%s, %s) returned an empty Address, want a real one", store, address) - } - return current -} - -// epochRequireLive asserts that address is served at epoch: StillServes is true -// and Resolve answers RestrictionLive naming that epoch. why says what the -// assertion is checking, for the failure message. -func epochRequireLive(t *testing.T, ctx context.Context, fixture EpochFixture, store string, address Address, epoch int, why string) { - t.Helper() - serves, err := fixture.StillServes(ctx, store, address) - if err != nil { - t.Fatalf("StillServes(%s): %v", address, err) - } - if !serves { - t.Errorf("StillServes(%s) = false, want true: %s", address, why) - } - answer, err := fixture.Resolve(ctx, store, address) - if err != nil { - t.Fatalf("Resolve(%s): %v", address, err) - } - if answer.Restriction != RestrictionLive { - t.Errorf("Resolve(%s) = %s, want RestrictionLive: %s", address, answer.Restriction, why) - } - epochRequireAnswerEpoch(t, answer, address, epoch, why) -} - -// epochRequireGone asserts that address is no longer served at epoch: -// StillServes is false and Resolve answers RestrictionGoneReorganization -// naming that epoch. -func epochRequireGone(t *testing.T, ctx context.Context, fixture EpochFixture, store string, address Address, epoch int, why string) { - t.Helper() - serves, err := fixture.StillServes(ctx, store, address) - if err != nil { - t.Fatalf("StillServes(%s): %v", address, err) - } - if serves { - t.Errorf("StillServes(%s) = true, want false: %s", address, why) - } - answer, err := fixture.Resolve(ctx, store, address) - if err != nil { - t.Fatalf("Resolve(%s): %v", address, err) - } - if answer.Restriction != RestrictionGoneReorganization { - t.Errorf("Resolve(%s) = %s, want RestrictionGoneReorganization: %s", address, answer.Restriction, why) - } - epochRequireAnswerEpoch(t, answer, address, epoch, why) -} - -// epochRequireAnswerEpoch asserts that a Live or Gone answer names the current -// epoch (R20-n: the answer carries the epoch, not just the Restriction). -func epochRequireAnswerEpoch(t *testing.T, answer RetentionAnswer, address Address, epoch int, why string) { - t.Helper() - if answer.Epoch == nil { - t.Errorf("Resolve(%s).Epoch = nil, want the current epoch %d: %s", address, epoch, why) - return - } - if *answer.Epoch != epoch { - t.Errorf("Resolve(%s).Epoch = %d, want the current epoch %d: %s", address, *answer.Epoch, epoch, why) - } -} - -// sameRetentionAnswer compares two RetentionAnswer values by content rather -// than identity, so a case can assert "this did not change" without -// depending on the concrete Restriction the backend happened to pick. -func sameRetentionAnswer(a, b RetentionAnswer) bool { - return a.Restriction == b.Restriction && - a.ProducingStore == b.ProducingStore && - sameRetainedWindow(a.RetainedWindow, b.RetainedWindow) && - sameAttributionPtr(a.Attribution, b.Attribution) && - sameIntPtr(a.Epoch, b.Epoch) -} - -func sameAttributionPtr(a, b *ChangeAttribution) bool { - if a == nil || b == nil { - return a == b - } - return sameAttribution(*a, *b) -} - -func sameIntPtr(a, b *int) bool { - if a == nil || b == nil { - return a == b - } - return *a == *b -} - -func sameRetainedWindow(a, b *RetainedWindow) bool { - if a == nil || b == nil { - return a == b - } - return *a == *b -} diff --git a/backend/conformance/role_bundle.go b/backend/conformance/role_bundle.go index 3b2831ba1..eed3edf33 100644 --- a/backend/conformance/role_bundle.go +++ b/backend/conformance/role_bundle.go @@ -89,7 +89,6 @@ type RoleContractBundle struct { CycleDetector func(t *testing.T) *CycleDetectorFixture Deleter func(t *testing.T) *DeleterFixture DependencyEditor func(t *testing.T) *DependencyEditorFixture - DualWrite func(t *testing.T) *DualWriteFixture EdgeReader func(t *testing.T) *EdgeReaderFixture GraphCounter func(t *testing.T) *GraphCounterFixture Importer func(t *testing.T) *ImporterFixture diff --git a/backend/conformance/role_bundle_cases.go b/backend/conformance/role_bundle_cases.go index 1c5e2cf00..769eb1c14 100644 --- a/backend/conformance/role_bundle_cases.go +++ b/backend/conformance/role_bundle_cases.go @@ -44,10 +44,6 @@ var roleContractCases = []roleContract{ RunBatchApplyRefusesACrossPlaneEdgeBetweenRowsItCreated, RunBatchApplyAcceptsAnExternalEdgeTarget, RunBatchApplyNormalizesTheWaitsForGate, - RunBatchApplyStampsSpawnerIDOnlyWhenNamed, - RunBatchApplyCarriesThreadIDOntoTheStoredEdge, - RunBatchApplyCarriesThreadIDOntoAnExistingEdge, - RunBatchApplyRefusesAThreadIDLongerThanItsColumn, RunBatchApplySplicesAForwardMetadataRef, RunBatchApplySplicesASelfMetadataRef, RunBatchApplyRefusesAMetadataRefNoItemDeclares, @@ -58,12 +54,6 @@ var roleContractCases = []roleContract{ RunBatchApplyReplayMintsANewSetOfRows, RunBatchApplyDoesNotMutateTheCallerRequest, RunBatchApplyRefusesAnUnusableRequest, - RunBatchApplyUpdateItemsRefuseATemplate, - RunBatchApplySplicesTheMetadataOfATemplateItCreates, - RunBatchApplyCloseItemsAnswerToTheCloseGuards, - RunBatchApplyRefusesADottedChildGatedOnItsOwnParent, - RunBatchApplyAppliesTheDefaultPriority, - RunBatchApplyCarriesTheGateMembers, ), roleCases("BatchCloser", "BatchCloser()", oncePerRole, @@ -88,7 +78,6 @@ var roleContractCases = []roleContract{ RunBatchCloserAllRefusedBatchRecordsNoHistory, RunBatchCloserDoesNotMutateTheCallerRequest, RunBatchCloserSettlesTheDependersOfWhatItClosed, - RunBatchCloserItemsAnswerToTheCloseGuards, ), roleCases("BatchCreator", "BatchCreator()", oncePerRole, @@ -106,7 +95,6 @@ var roleContractCases = []roleContract{ RunBatchCreatorRecordsNoHistoryForAnEphemeralBatch, RunBatchCreatorDoesNotMutateTheCallerRequest, RunBatchCreatorEchoesSubSecondTimestamps, - RunBatchCreatorAppliesTheDefaultPriority, ), roleCases("BlockingAnnotator", "BlockingAnnotator()", oncePerRole, @@ -128,7 +116,6 @@ var roleContractCases = []roleContract{ RunBlockingAnnotatorRefusesAnEmptyID, RunBlockingAnnotatorLeavesTheRequestAlone, RunBlockingAnnotatorWritesNothing, - RunBlockingAnnotatorReportsTheTransitiveBlockedFlag, ), // One contract file covers both halves of workspace identity: the eight @@ -199,15 +186,6 @@ var roleContractCases = []roleContract{ RunCounterIncludeInfraExcludesGates, RunCounterCountsClosedRows, RunCounterAnUnknownStatusMatchesNothing, - RunCounterParentIDScopesToChildren, - RunCounterNoParentExcludesChildren, - RunCounterExcludeTypesNarrowsThePredicate, - RunCounterExcludeStatusNarrowsThePredicate, - RunCounterParentIDMatchesListCardinality, - RunCounterNoParentMatchesListCardinality, - RunCounterExcludeTypesMatchesListCardinality, - RunCounterParentIDIncludesAWispChild, - RunCounterParentIDAndExcludeStatusComposeOnAClosedChild, RunCounterGroupsPartitionTheScalarSet, RunCounterLabelBucketsOverlapSoTotalIsNotTheirSum, RunCounterNamesTheEmptyBuckets, @@ -227,9 +205,6 @@ var roleContractCases = []roleContract{ RunCycleDetectorReportsTheSameCyclesEveryRun, RunCycleDetectorMergesTheDurableAndEphemeralPlanes, RunCycleDetectorFollowsOnlyBlockingEdges, - RunCycleDetectorIncludeTracksIgnoresAPureTracksLoop, - RunCycleDetectorIncludeTracksFindsTheMoleculeRootShape, - RunCycleDetectorIncludeTracksWalksEachEdgeInItsStoredDirection, RunCycleDetectorReportsAnHonestPartial, RunCycleDetectorCountsAWhollyUndescribableCycle, RunCycleDetectorWritesNothing, @@ -270,9 +245,6 @@ var roleContractCases = []roleContract{ RunDependencyEditorRefusesCrossPlaneCycle, RunDependencyEditorAddedEchoesTheRequestOrder, RunDependencyEditorSameTypeReAddIsIdempotent, - RunDependencyEditorSameTypeReAddWithChangedMetadataMintsOneVersion, - RunDependencyEditorSameTypeReAddWithIdenticalMetadataIsANoOp, - RunDependencyEditorSameTypeReAddWithChangedThreadMintsOneVersion, RunDependencyEditorRepeatsWithinOneRequestCollapse, RunDependencyEditorAttributesItsEventsToTheActor, RunDependencyEditorRetypeRefusalLeavesTheOriginalEdge, @@ -306,25 +278,6 @@ var roleContractCases = []roleContract{ RunDependencyEditorAcceptsADiamond, RunDependencyEditorGateScopeFollowsTheEdgeType, RunDependencyEditorAcceptsBlockingAcrossIssueTypes, - RunDependencyEditorRefusesADottedChildGatedOnItsOwnParent, - ), - - // The accessor named here is not an accessor at all, alone among these - // rows except Journal: storage.VersionedHistoryConfigurer is reached by - // TYPE ASSERTION, either on a store (the dolt and embedded-dolt legs) or - // on a unit-of-work provider (the uow leg), because enabling dual-write - // history is engine configuration rather than an issue-shaped operation - // storage.Storage publishes. A backend that does not implement Phase 2's - // dual-write mechanism leaves this field nil; see - // dualwrite_history_contract.go's header. - roleCases("DualWrite", "the storage.VersionedHistoryConfigurer type assertion", oncePerRole, - func(b RoleContractBundle) func(t *testing.T) *DualWriteFixture { return b.DualWrite }, - RunDualWriteMintsOneVersionRowPerAcceptedMutation, - RunDualWriteNoOpMutationMintsNoRow, - RunDualWriteAttributionIsRecordedWithTheMutation, - RunDualWriteCurrentRevisionMatchesTheNewVersionRow, - RunDualWriteFlagOffProducesNoVersionRows, - RunDualWriteNoOpMutationLeavesThePriorVersionRowUnperturbed, ), roleCases("EdgeReader", "EdgeReader()", oncePerRole, @@ -371,7 +324,7 @@ var roleContractCases = []roleContract{ func(b RoleContractBundle) func(t *testing.T) *ImporterFixture { return b.Importer }, RunImporterRejectsAStaleRowAndNamesIt, RunImporterReportsTheAbsentTargetItDroppedOnce, - RunImporterWiresTheCrossPlaneEdgeBetweenItsRows, + RunImporterReportsTheCrossPlaneEdgeItDropped, RunImporterReportsTheCycleEdgeItDropped, ), @@ -443,14 +396,12 @@ var roleContractCases = []roleContract{ RunLifecycleExpectedVersionIsCheckedBeforeTheNoOps, RunLifecycleReopenRecordsItsReason, RunLifecycleResultsAreHydratedPostStateSnapshots, - RunLifecycleResultsCarryThePostWriteRowVersion, RunLifecycleCloseAndReopenRequireActorAndIssueID, RunLifecycleReopenProvenanceLabelsHistory, RunLifecycleCloseSettlesItsTransitiveAndCrossPlaneDependers, RunLifecycleCloseSettlesTheClosedRowItselfAndItsChild, RunLifecycleCloseOnASpawnersLastChildSatisfiesAWaitsForGate, RunLifecycleReopenReblocksItsDependers, - RunLifecycleCloseEnforcesTheCloseGuards, ), // The accessor-reachable half of Lifecycle.Create, moved out of the staging @@ -463,9 +414,6 @@ var roleContractCases = []roleContract{ RunLifecycleCreateInheritsParentLabels, RunLifecycleCreateWritesEveryScalarField, RunLifecycleCreateEchoesSubSecondTimestamps, - RunLifecycleCreateAppliesTheDefaultPriority, - RunLifecycleCreateWritesTheGateMembers, - RunLifecycleCreateBoundsTheGateMembers, ), roleCases("LifecycleUpdate", "IssueLifecycle()", oncePerRole, @@ -480,10 +428,6 @@ var roleContractCases = []roleContract{ RunLifecycleUpdateRefusesUnknownIDsAndActorlessRequests, RunLifecycleUpdateRefusalWritesNoMemberOfThePatch, RunLifecycleUpdateConditionalGuardsGateOrdinaryEdits, - RunLifecycleUpdateExpectedVersionSingleWinnerUnderConcurrency, - RunLifecycleUpdateExpectedVersionSingleWinnerWithDisjointColumnsUnderConcurrency, - RunLifecycleCloseExpectedVersionSingleWinnerUnderConcurrency, - RunLifecycleUpdateExpectedVersionSingleWinnerAcrossUpdateAndCloseUnderConcurrency, RunLifecycleUpdateConditionalGuardAcceptsRespelledAssignee, RunLifecycleUpdateMetadataPatchOrdersMergeSetUnset, RunLifecycleUpdateClosePolicy, @@ -492,13 +436,7 @@ var roleContractCases = []roleContract{ RunLifecycleUpdateParentIDReplacesTheParentEdge, RunLifecycleUpdateParentIDReplacesEveryParent, RunLifecycleUpdatePersistentPreservesUnversionedClass, - RunLifecycleUpdateWispModesRefuseAnUnversionedRow, - RunLifecycleUpdatePersistentPromotesAWisp, RunLifecycleUpdateProvenanceLabelsHistory, - RunLifecycleUpdateRefusesATemplate, - RunLifecycleUpdateAllowTemplateEditsATemplate, - RunLifecycleUpdatePatchesTheGateMembers, - RunLifecycleUpdateBoundsTheGateMembers, ), roleCases("Memories", "Memories()", oncePerRole, @@ -513,7 +451,7 @@ var roleContractCases = []roleContract{ RunMemoriesRememberRefusesAnUnderivableKey, RunMemoriesRecallAnswersTheStoredValue, RunMemoriesRecallReportsAMissAsNotFoundNotAnError, - RunMemoriesEmptyRowIsPresentUntilForgotten, + RunMemoriesRecallConflatesStoredEmptyWithAbsent, RunMemoriesForgetRemovesExactlyTheNamedRow, RunMemoriesForgetNeverTouchesTheSettingsPlane, RunMemoriesForgetReportsTheForgottenValue, @@ -576,7 +514,6 @@ var roleContractCases = []roleContract{ RunReaderListKeysetPositionResumesTheCreatedDescIDAscOrder, RunReaderListReadyFlagAnswersTheBlockerAwareSet, RunReaderListReadyFlagRefusesAFilterItCannotCarry, - RunReaderListQueryMatchesTheTitleOrTheID, RunReaderListEmptyPageIsWellFormed, RunReaderListMaxRowsIsHonored, RunReaderListMaxRowsBoundaryIsLimitPlusOffset, @@ -604,8 +541,6 @@ var roleContractCases = []roleContract{ RunReaderListWispTypeNarrowsTheAdmittedPlaneRatherThanAdmittingIt, RunReaderListBriefDropsTheFreeFormTextAndNothingElse, RunReaderReadyBriefDropsTheFreeFormTextAndNothingElse, - RunReaderListTimeBoundsAreStrictAndWholeSecond, - RunReaderListFilterFamiliesNarrowToTheirRows, ), roleCases("ReadyClaimer", "ReadyClaimer() and IssueReader()", oncePerRole, @@ -672,8 +607,6 @@ var roleContractCases = []roleContract{ func(b RoleContractBundle) func(t *testing.T) *StatsReporterFixture { return b.StatsReporter }, RunStatsReporterCountsEveryDurableRowByStatus, RunStatsReporterExcludesTheWispTier, - RunStatsReporterBreaksOutTheRowsTheDefaultListingSuppresses, - RunStatsReporterBreaksOutAGateThatIsAlsoATemplate, RunStatsReporterAStatusOutsideTheTalliesIsCountedOnlyInTotal, RunStatsReporterBlockedCountsTheGraphNotTheStatus, RunStatsReporterBlockedExcludesByStatusNotByThePinnedFlag, @@ -685,7 +618,6 @@ var roleContractCases = []roleContract{ RunStatsReporterAssigneeStatsScopesToOneActor, RunStatsReporterAssigneeBlockedCountsTheStatusNotTheGraph, RunStatsReporterAssigneeStatsMergesTheWispTier, - RunStatsReporterAssigneeStatsBreaksOutTheSuppressedRows, RunStatsReporterAssigneeStatsPopulatesBothPointers, RunStatsReporterAssigneeStatsRefusesAnEmptyAssignee, ), diff --git a/backend/conformance/role_bundle_test.go b/backend/conformance/role_bundle_test.go index ce692298b..477f333c7 100644 --- a/backend/conformance/role_bundle_test.go +++ b/backend/conformance/role_bundle_test.go @@ -9,13 +9,12 @@ import ( "io/fs" "os" "os/exec" + "path/filepath" "reflect" "runtime" "sort" "strings" "testing" - - "github.com/steveyegge/beads/internal/testutil/bazeltest" ) // TestRoleContractCasesCoverEveryContractCase is the drift guard on the role @@ -28,16 +27,9 @@ import ( // which is the same defect class as a test with correct assertions on a fixture // that cannot fail, one level up. So the expected set is DERIVED from the // package source rather than written down: every top-level func named Run* that -// takes (*testing.T, context.Context, Fixture) where Fixture is one -// of RoleContractBundle's own field types is a role-tier case. That bundle-field -// check is what keeps this scoped to the role tier specifically: the package -// also holds other conformance tiers (e.g. the history-semantics contracts that -// versioned_history_wiring_test.go wires directly, with no RoleContractBundle -// field of their own — see that file's header) whose Run*Fixture-shaped -// functions must NOT be swept in here, since nothing in RoleContractBundle ever -// calls them. The entry points that take a Factory instead — RunAll, RunAudit -// and friends, RunPortableMethods, RunSearchPaging, RunDeferredReads — are not -// role-tier cases either. +// takes (*testing.T, context.Context, Fixture) is a role-tier case, and +// the entry points that take a Factory instead — RunAll, RunAudit and friends, +// RunPortableMethods, RunSearchPaging, RunDeferredReads — are not. // // The table's own half of the comparison is derived too: roleCases resolves // each function VALUE back to its declared name (runFuncName), so a row cannot @@ -315,26 +307,22 @@ func TestRoleCasesFailsWhenAFactoryReturnsNil(t *testing.T) { func runProbeChild(t *testing.T, mode string) (string, error) { t.Helper() cmd := exec.Command(os.Args[0], "-test.run=^"+t.Name()+"$", "-test.v") - cmd.Env = append(bazeltest.ShardFreeEnv(os.Environ()), roleBundleProbeEnv+"="+mode) + cmd.Env = append(os.Environ(), roleBundleProbeEnv+"="+mode) out, err := cmd.CombinedOutput() return string(out), err } // parseRoleContractCases returns every role-tier case declared in this // package's non-test sources, in filename then declaration order — the order -// roleContractCases is written in. A Run*Fixture-shaped function whose Fixture -// type is not one of RoleContractBundle's own field types belongs to a -// different conformance tier and is not a role-tier case; see -// roleTierFixtureNames. +// roleContractCases is written in. func parseRoleContractCases(t *testing.T) []string { t.Helper() - roleFixtures := roleTierFixtureNames() _, thisFile, _, ok := runtime.Caller(0) if !ok { t.Fatal("runtime.Caller failed") } fset := token.NewFileSet() - pkgs, err := parser.ParseDir(fset, bazeltest.CallerDir(thisFile, "backend/conformance"), func(fi fs.FileInfo) bool { + pkgs, err := parser.ParseDir(fset, filepath.Dir(thisFile), func(fi fs.FileInfo) bool { return !strings.HasSuffix(fi.Name(), "_test.go") }, 0) if err != nil { @@ -353,7 +341,7 @@ func parseRoleContractCases(t *testing.T) []string { if !ok || fn.Recv != nil || !strings.HasPrefix(fn.Name.Name, "Run") { continue } - if fixtureName, ok := takesFixture(fn); ok && roleFixtures[fixtureName] { + if takesFixture(fn) { cases = append(cases, fn.Name.Name) } } @@ -362,54 +350,22 @@ func parseRoleContractCases(t *testing.T) []string { return cases } -// roleTierFixtureNames returns the pointee type name of every *XxxFixture a -// RoleContractBundle field produces. This is the role tier's actual membership -// test: a Run*Fixture-shaped function is a role-tier case only if its Fixture -// type appears here, because that is the only way RunRoleContracts could ever -// reach it. Other conformance tiers in this package (e.g. Phase 0's -// history-semantics contracts, wired directly by -// versioned_history_wiring_test.go rather than through this bundle) mint their -// own *Fixture types that intentionally never appear here. -func roleTierFixtureNames() map[string]bool { - names := map[string]bool{} - bundleType := reflect.TypeOf(RoleContractBundle{}) - for i := range bundleType.NumField() { - field := bundleType.Field(i).Type - if field.Kind() != reflect.Func || field.NumOut() != 1 { - continue - } - out := field.Out(0) - if out.Kind() != reflect.Ptr { - continue - } - names[out.Elem().Name()] = true - } - return names -} - // takesFixture reports whether fn has the role-tier case signature: -// (t *testing.T, ctx context.Context, fixture Fixture), returning the -// Fixture type's name so the caller can check it against the role tier's -// actual membership (roleTierFixtureNames) rather than the signature shape -// alone — the shape is necessary but not sufficient, since other conformance -// tiers use it too. -func takesFixture(fn *ast.FuncDecl) (fixtureName string, ok bool) { +// (t *testing.T, ctx context.Context, fixture Fixture). +func takesFixture(fn *ast.FuncDecl) bool { if fn.Type.Params == nil || len(fn.Type.Params.List) != 3 { - return "", false + return false } params := fn.Type.Params.List - star, starOk := params[0].Type.(*ast.StarExpr) - if !starOk || !isQualified(star.X, "testing", "T") { - return "", false + star, ok := params[0].Type.(*ast.StarExpr) + if !ok || !isQualified(star.X, "testing", "T") { + return false } if !isQualified(params[1].Type, "context", "Context") { - return "", false + return false } - fixture, identOk := params[2].Type.(*ast.Ident) - if !identOk || !strings.HasSuffix(fixture.Name, "Fixture") { - return "", false - } - return fixture.Name, true + fixture, ok := params[2].Type.(*ast.Ident) + return ok && strings.HasSuffix(fixture.Name, "Fixture") } func isQualified(expr ast.Expr, pkg, name string) bool { diff --git a/backend/conformance/role_coverage_scan_test.go b/backend/conformance/role_coverage_scan_test.go index cf361082b..341075f79 100644 --- a/backend/conformance/role_coverage_scan_test.go +++ b/backend/conformance/role_coverage_scan_test.go @@ -16,7 +16,6 @@ import ( "testing" "github.com/steveyegge/beads/internal/storage" - "github.com/steveyegge/beads/internal/testutil/bazeltest" "github.com/steveyegge/beads/issueops" "github.com/steveyegge/beads/journalops" "github.com/steveyegge/beads/memoryops" @@ -60,9 +59,6 @@ var errorType = reflect.TypeOf((*error)(nil)).Elem() // repoRoot locates the module root from this file's own path, so the gate // finds the facade packages without depending on the working directory. func repoRoot() (string, error) { - if root := bazeltest.OverrideRoot(); root != "" { - return root, nil - } _, thisFile, _, ok := runtime.Caller(0) if !ok { return "", errors.New("runtime.Caller failed") diff --git a/backend/conformance/stats_reporter_contract.go b/backend/conformance/stats_reporter_contract.go index 071bd1008..34a7b3940 100644 --- a/backend/conformance/stats_reporter_contract.go +++ b/backend/conformance/stats_reporter_contract.go @@ -103,88 +103,6 @@ func RunStatsReporterExcludesTheWispTier(t *testing.T, ctx context.Context, fixt assertStatsReporterDelta(t, before, after, statsReporterCounts{total: 1, open: 1}) } -// RunStatsReporterBreaksOutTheRowsTheDefaultListingSuppresses pins the -// GateIssues and TemplateIssues tally: the durable rows a default `bd list` -// will not show, broken out of Total rather than removed from it, so `bd -// status` and `bd list` can be reconciled instead of silently disagreeing. -// -// The two predicates have to be the exact complements of the listing's: -// `issue_type = 'gate'` against list.go's `ExcludeTypes = append(..., "gate")`, -// and `is_template = 1` against sqlbuild's default -// `(is_template = 0 OR is_template IS NULL)`. A backend that spelled either -// one loosely - `is_template <> 0`, or a LIKE on the type - moves the same -// numbers on these seeds, so the case also seeds the NEGATIVE rows: a plain -// task, and a gate-typed row that is not a template, each of which must move -// exactly one of the two counts and not the other. -// -// Both counts overlap the status buckets rather than forming their own, in the -// same way PinnedIssues does; the deltas below assert that by counting the -// gate and proto rows in Total and Open as well. -// -// THE POPULATION IS EVERY STATUS, which the closed pair below is here to -// decide. Open seeds alone cannot: a backend counting these over all statuses -// and one counting them only over the rows a default listing shows report -// identical numbers on every open row, so the contract would leave the -// question to each implementation. A closed gate and a closed proto separate -// the two readings, and this file is where that belongs - it is the axis -// `bd status`'s printed remedy turns on, and all three backends answer it here -// at once. -func RunStatsReporterBreaksOutTheRowsTheDefaultListingSuppresses(t *testing.T, ctx context.Context, fixture StatsReporterFixture) { - t.Helper() - before := statsReporterSummary(t, ctx, fixture, publicops.StatsRequest{}) - - // Neither suppression: the control. A backend that counted every row - // would move both counts by three rather than by one. - seedStatsReporterIssue(t, ctx, fixture, statsReporterSeed(fixture, "suppressed-plain", types.StatusOpen)) - - gate := statsReporterSeed(fixture, "suppressed-gate", types.StatusOpen) - gate.IssueType = types.TypeGate - seedStatsReporterIssue(t, ctx, fixture, gate) - - proto := statsReporterSeed(fixture, "suppressed-proto", types.StatusOpen) - proto.IsTemplate = true - seedStatsReporterIssue(t, ctx, fixture, proto) - - // The status axis. These two are suppressed by the default listing TWICE - // over - once for what they are, once for being closed - and they are - // counted here all the same, because the two counts describe the database - // the way TotalIssues does and not the rows a listing shows. A backend - // that scoped either count to the listing's default population passes - // every seed above and fails on exactly these. - closedGate := statsReporterSeed(fixture, "suppressed-closed-gate", types.StatusClosed) - closedGate.IssueType = types.TypeGate - seedStatsReporterIssue(t, ctx, fixture, closedGate) - - closedProto := statsReporterSeed(fixture, "suppressed-closed-proto", types.StatusClosed) - closedProto.IsTemplate = true - seedStatsReporterIssue(t, ctx, fixture, closedProto) - - after := statsReporterSummary(t, ctx, fixture, publicops.StatsRequest{}) - assertStatsReporterDelta(t, before, after, statsReporterCounts{ - total: 5, open: 3, closed: 2, gates: 2, templates: 2, - }) -} - -// RunStatsReporterBreaksOutAGateThatIsAlsoATemplate pins that the two -// suppressions are independent: a proto's gate step is both a gate and a -// template, and `bd list` hides it for either reason on its own, so it belongs -// in both counts. A backend that treated the two as exclusive arms of one CASE -// reports one of them as zero here while passing the case above. -func RunStatsReporterBreaksOutAGateThatIsAlsoATemplate(t *testing.T, ctx context.Context, fixture StatsReporterFixture) { - t.Helper() - before := statsReporterSummary(t, ctx, fixture, publicops.StatsRequest{}) - - both := statsReporterSeed(fixture, "suppressed-gateproto", types.StatusOpen) - both.IssueType = types.TypeGate - both.IsTemplate = true - seedStatsReporterIssue(t, ctx, fixture, both) - - after := statsReporterSummary(t, ctx, fixture, publicops.StatsRequest{}) - assertStatsReporterDelta(t, before, after, statsReporterCounts{ - total: 1, open: 1, gates: 1, templates: 1, - }) -} - // RunStatsReporterAStatusOutsideTheTalliesIsCountedOnlyInTotal pins the second // half of statsreporter.go:92-98 — the tallies are exact equality against the // stored status, so a row whose status is none of the four lands in Total and @@ -534,43 +452,6 @@ func RunStatsReporterAssigneeStatsMergesTheWispTier(t *testing.T, ctx context.Co } } -// RunStatsReporterAssigneeStatsBreaksOutTheSuppressedRows is the scoped half of -// the pair. `bd status --assigned` reaches a different implementation - the -// fold over one actor's rows in internal/workapi/stats.go, not -// ScanIssueCountsInTx - and its filter sets only Assignee, so the actor's gates -// and protos ARE in these rows while `bd list --assignee` suppresses both. The -// two counts must therefore be populated here too; a zero would be the same -// silent disagreement, one scope down. -func RunStatsReporterAssigneeStatsBreaksOutTheSuppressedRows(t *testing.T, ctx context.Context, fixture StatsReporterFixture) { - t.Helper() - assignee := statsReporterAssignee(fixture, "asuppressed") - - plain := statsReporterSeed(fixture, "asuppressed-plain", types.StatusOpen) - plain.Assignee = assignee - seedStatsReporterIssue(t, ctx, fixture, plain) - - gate := statsReporterSeed(fixture, "asuppressed-gate", types.StatusOpen) - gate.Assignee = assignee - gate.IssueType = types.TypeGate - seedStatsReporterIssue(t, ctx, fixture, gate) - - proto := statsReporterSeed(fixture, "asuppressed-proto", types.StatusOpen) - proto.Assignee = assignee - proto.IsTemplate = true - seedStatsReporterIssue(t, ctx, fixture, proto) - - summary := statsReporterAssigneeSummary(t, ctx, fixture, assignee) - if summary.TotalIssues != 3 { - t.Fatalf("TotalIssues = %d, want 3 (the fixture namespaces this actor, so this is an absolute)", summary.TotalIssues) - } - if summary.GateIssues != 1 { - t.Errorf("GateIssues = %d, want 1 — the actor's gate is in this answer but not in `bd list --assignee`", summary.GateIssues) - } - if summary.TemplateIssues != 1 { - t.Errorf("TemplateIssues = %d, want 1 — the actor's proto is in this answer but not in `bd list --assignee`", summary.TemplateIssues) - } -} - // RunStatsReporterAssigneeStatsPopulatesBothPointers pins the clause // FoldStatsAssigneeSummary exists to guarantee: on this path BlockedIssues and // ReadyIssues are ALWAYS non-nil, including for an actor with no rows at all. @@ -618,8 +499,6 @@ type statsReporterCounts struct { deferred int pinned int blocked int - gates int - templates int } // assertStatsReporterDelta compares two summaries field by field against the @@ -640,8 +519,6 @@ func assertStatsReporterDelta(t *testing.T, before, after types.Statistics, want {"ClosedIssues", before.ClosedIssues, after.ClosedIssues, want.closed}, {"DeferredIssues", before.DeferredIssues, after.DeferredIssues, want.deferred}, {"PinnedIssues", before.PinnedIssues, after.PinnedIssues, want.pinned}, - {"GateIssues", before.GateIssues, after.GateIssues, want.gates}, - {"TemplateIssues", before.TemplateIssues, after.TemplateIssues, want.templates}, } { if got := field.after - field.before; got != field.want { t.Errorf("%s moved by %d (%d -> %d), want %d", field.name, got, field.before, field.after, field.want) diff --git a/backend/conformance/versioned_history_wiring_test.go b/backend/conformance/versioned_history_wiring_test.go deleted file mode 100644 index b022b716e..000000000 --- a/backend/conformance/versioned_history_wiring_test.go +++ /dev/null @@ -1,142 +0,0 @@ -package conformance - -import ( - "context" - "testing" -) - -// TestVersionedHistoryPhase0RunsInFullSkipMode is Phase 0's own wiring: it -// constructs each of the three new Fixtures with every hook nil — exactly -// the state every backend's real fixture kit is in as of this phase's merge -// (architecture §12: "every new hook is nil in every backend's fixture kit") -// — and runs every R16/R16.1/R17/R20 case once each, proving the whole suite -// compiles and skips cleanly under today's CI (NFR-01). -// -// Each leg under internal/storage wires these same entrypoints, with the same -// all-nil fixtures, so TestEveryLegWiresEveryRoleContract counts them; this -// local copy is not a substitute for those, it just proves the all-skip -// behavior once without needing to build all three legs to see it. -func TestVersionedHistoryPhase0RunsInFullSkipMode(t *testing.T) { - ctx := context.Background() - - t.Run("ExpectedRevision", func(t *testing.T) { - fixture := ExpectedRevisionFixture{IssuePrefix: "vh0"} - t.Run("AcceptsAWriteNamingTheCurrentVersion", func(t *testing.T) { - RunExpectedRevisionAcceptsAWriteNamingTheCurrentVersion(t, ctx, fixture) - }) - t.Run("AcceptsAWriteNamingNoVersion", func(t *testing.T) { - RunExpectedRevisionAcceptsAWriteNamingNoVersion(t, ctx, fixture) - }) - t.Run("CoversFieldsOutsideAnyWatchedSubset", func(t *testing.T) { - RunExpectedRevisionCoversFieldsOutsideAnyWatchedSubset(t, ctx, fixture) - }) - t.Run("RefusalReportsTheRefusingVersionAddress", func(t *testing.T) { - RunRefusalReportsTheRefusingVersionAddress(t, ctx, fixture) - }) - t.Run("RefusalReportsTheRefusingVersionsChangeAttribution", func(t *testing.T) { - RunRefusalReportsTheRefusingVersionsChangeAttribution(t, ctx, fixture) - }) - t.Run("RefusalIsATypedOutcomeNotAGenericError", func(t *testing.T) { - RunRefusalIsATypedOutcomeNotAGenericError(t, ctx, fixture) - }) - t.Run("RefusalIsDistinguishableFromAnAcceptedWrite", func(t *testing.T) { - RunRefusalIsDistinguishableFromAnAcceptedWrite(t, ctx, fixture) - }) - t.Run("RefusalIsDistinguishableFromNotFound", func(t *testing.T) { - RunRefusalIsDistinguishableFromNotFound(t, ctx, fixture) - }) - t.Run("RefusalIsDistinguishableFromValidationFailure", func(t *testing.T) { - RunRefusalIsDistinguishableFromValidationFailure(t, ctx, fixture) - }) - t.Run("RefusalNeverSilentlyPicksAWinner", func(t *testing.T) { - RunRefusalNeverSilentlyPicksAWinner(t, ctx, fixture) - }) - }) - - t.Run("CrossRecordInvariant", func(t *testing.T) { - fixture := CrossRecordInvariantFixture{IssuePrefix: "vh0"} - t.Run("SurvivesTwoPassingPerRecordGuards", func(t *testing.T) { - RunCrossRecordInvariantSurvivesTwoPassingPerRecordGuards(t, ctx, fixture) - }) - t.Run("StoreInvariantTransactionScopesExactlyTheSpanningRecords", func(t *testing.T) { - RunStoreInvariantTransactionScopesExactlyTheSpanningRecords(t, ctx, fixture) - }) - t.Run("InvariantRefusalNamesTheViolatedInvariant", func(t *testing.T) { - RunInvariantRefusalNamesTheViolatedInvariant(t, ctx, fixture) - }) - t.Run("MemoryKeyAliasUniquenessIsAStoreInvariantNotACAS", func(t *testing.T) { - RunMemoryKeyAliasUniquenessIsAStoreInvariantNotACAS(t, ctx, fixture) - }) - t.Run("MaximumEndpointMultiplicityIsAStoreInvariantNotACAS", func(t *testing.T) { - RunMaximumEndpointMultiplicityIsAStoreInvariantNotACAS(t, ctx, fixture) - }) - t.Run("AdvisoryLeaseAloneDoesNotPreventTheInvariantViolation", func(t *testing.T) { - RunAdvisoryLeaseAloneDoesNotPreventTheInvariantViolation(t, ctx, fixture) - }) - t.Run("LeaseAcquisitionRecordsNoHistoryEntry", func(t *testing.T) { - RunLeaseAcquisitionRecordsNoHistoryEntry(t, ctx, fixture) - }) - }) - - t.Run("Retention", func(t *testing.T) { - fixture := RetentionFixture{IssuePrefix: "vh0"} - t.Run("RemovalLeavesTheAddressAbleToAnswer", func(t *testing.T) { - RunRemovalLeavesTheAddressAbleToAnswer(t, ctx, fixture) - }) - t.Run("RemovalReasonIsRetentionErasureOrReorganizationDistinctly", func(t *testing.T) { - RunRemovalReasonIsRetentionErasureOrReorganizationDistinctly(t, ctx, fixture) - }) - t.Run("RemovalReportsTheSurvivingRetainedWindow", func(t *testing.T) { - RunRemovalReportsTheSurvivingRetainedWindow(t, ctx, fixture) - }) - t.Run("RemovalNeverReassignsASurvivingAddress", func(t *testing.T) { - RunRemovalNeverReassignsASurvivingAddress(t, ctx, fixture) - }) - t.Run("GoneIsDistinguishableFromUnknown", func(t *testing.T) { - RunGoneIsDistinguishableFromUnknown(t, ctx, fixture) - }) - t.Run("AnAddressNeverResolvesToADifferentState", func(t *testing.T) { - RunAnAddressNeverResolvesToADifferentState(t, ctx, fixture) - }) - t.Run("ADestructiveOperationEnumeratesAffectedAddressesFirst", func(t *testing.T) { - RunADestructiveOperationEnumeratesAffectedAddressesFirst(t, ctx, fixture) - }) - t.Run("AHoldPreventsRemovalAndReportsInRetainedBounds", func(t *testing.T) { - RunAHoldPreventsRemovalAndReportsInRetainedBounds(t, ctx, fixture) - }) - t.Run("ForcingAHeldRemovalRecordsWhoWhenWhy", func(t *testing.T) { - RunForcingAHeldRemovalRecordsWhoWhenWhy(t, ctx, fixture) - }) - t.Run("EveryRetentionAnswerNamesItsProducingStore", func(t *testing.T) { - RunEveryRetentionAnswerNamesItsProducingStore(t, ctx, fixture) - }) - t.Run("AStoreWithNoLineageKnowledgeAnswersUnknownNotGone", func(t *testing.T) { - RunAStoreWithNoLineageKnowledgeAnswersUnknownNotGone(t, ctx, fixture) - }) - t.Run("AStoreThatRemovesStateStillAnswersGoneDurably", func(t *testing.T) { - RunAStoreThatRemovesStateStillAnswersGoneDurably(t, ctx, fixture) - }) - t.Run("ErasureMintsACorrectedVersionRatherThanEditingInPlace", func(t *testing.T) { - RunErasureMintsACorrectedVersionRatherThanEditingInPlace(t, ctx, fixture) - }) - }) - - t.Run("Epoch", func(t *testing.T) { - fixture := EpochFixture{IssuePrefix: "vh0"} - t.Run("AnEpochBumpIsTriggeredOnlyByRestoreReinitOrSchemeChange", func(t *testing.T) { - RunAnEpochBumpIsTriggeredOnlyByRestoreReinitOrSchemeChange(t, ctx, fixture) - }) - t.Run("EpochBumpVoidsOnlyAddressesOfVersionsNoLongerServed", func(t *testing.T) { - RunEpochBumpVoidsOnlyAddressesOfVersionsNoLongerServed(t, ctx, fixture) - }) - t.Run("TokenSchemeChangeCarriesLiveAddressesAndOnlyThose", func(t *testing.T) { - RunTokenSchemeChangeCarriesLiveAddressesAndOnlyThose(t, ctx, fixture) - }) - t.Run("ACarriedAddressKeepsResolvingAcrossLaterBumps", func(t *testing.T) { - RunACarriedAddressKeepsResolvingAcrossLaterBumps(t, ctx, fixture) - }) - t.Run("ALostAddressStaysGoneAcrossASchemeChange", func(t *testing.T) { - RunALostAddressStaysGoneAcrossASchemeChange(t, ctx, fixture) - }) - }) -} diff --git a/backend/conformance/versioned_read_contract.go b/backend/conformance/versioned_read_contract.go deleted file mode 100644 index 0e187117c..000000000 --- a/backend/conformance/versioned_read_contract.go +++ /dev/null @@ -1,356 +0,0 @@ -package conformance - -import ( - "context" - "testing" - "time" -) - -// This file holds the contract for R7.1 (gastownhall/beads#6136, design doc -// gastownhall/beads#5898 revision 9): the as-of read — the "versioned -// reads" half of Phase 3's title, authored here from scratch, since no -// fixture for it exists anywhere in this package to un-skip. -// -// R7.1, VERBATIM (design doc #5898 rev 9): "An as-of read is a named read -// whose selector is an instant. Given an instant T or a version address, a -// store returns one Bead's durable state as of that point — the latest -// version it holds that was accepted at or before T, with everything later -// excluded — under R7's record shape, marking the address it served, or -// refuses as gone or unknown with the reason (R8, R20). Like every history -// answer it is complete for the answering store's holdings and may change -// after a sync (TC3)." -// -// ADDRESS, RESTRICTION, AND CHANGEATTRIBUTION ARE DECLARED IN -// expected_revision_contract.go — see that file's package-level note on -// where this suite's shared vocabulary lives and why. This file reuses -// Restriction's five values as-is for the gone-or-unknown refusal outcome -// (R8): an as-of read never invents a sixth state, and never distinguishes -// "gone" from "unknown" any differently than R20 already does. -// -// EVERY HOOK ON THE FIXTURE BELOW IS INDEPENDENTLY NILABLE — see the -// package-level note in expected_revision_contract.go. Every case -// nil-checks every hook it uses and SKIPS BY NAME when one is missing. No -// backend implements as-of reads yet — the implementation is -// the child this suite is written ahead of. -// -// EXPLICIT NON-GOAL: MEMORY-PLANE CONSUMER POLICY IS NOT PART OF THIS -// PRIMITIVE. R7.1's own text is explicit that a closed-before-T boundary, -// sibling exclusion, and supersession exclusion "are Memory-plane policies -// a consumer layers on this primitive (#5877), not part of it." This suite -// does not implement or assert any of the three — a future reader who finds -// them absent should read that as this file honoring R7.1's own scope, not -// as an oversight. This is the read-side analog of the R16/R16.1 boundary -// risk already recorded in expected_revision_contract.go: do not conflate -// the primitive with policy layered on top of it. -// -// THIS SUITE ASSERTS LOCAL COMPLETENESS ONLY (TC3). Nothing below requires, -// or even checks, that two stores asked about the same lineage agree — R7.1 -// itself says an as-of answer "may change after a sync," so two stores at -// different sync points are expected to answer differently, and neither is -// wrong. RunAsOfReadIsCompleteOnlyForTheAnsweringStoresHoldings is the one -// case that touches more than one store, and what it asserts is exactly -// this: each store answers strictly from its own holdings, never a -// sibling's — not that the two agree. - -// AsOfSelector names the point R7.1 resolves against: either a wall-clock -// instant, or a specific version Address treated as the selector itself — -// "the read that names its own answer" (R7.1: "an instant T or a version -// address"). Exactly one of At/Version is populated in every case this file -// exercises; R7.1 does not speak to the shape where both or neither are -// set, so this suite does not either. -type AsOfSelector struct { - At *time.Time - Version *Address -} - -// AsOfReadRefusal is R8's typed outcome for an as-of read that cannot be -// answered. Restriction is always one of the four Gone*/Unknown values, -// never RestrictionLive — a live answer is a served AsOfReadResult, not a -// refusal. Reason explains why (R8). -type AsOfReadRefusal struct { - Restriction Restriction - Reason string -} - -// AsOfReadResult is the outcome of an as-of read (R7.1). Address/State and -// Refusal are mutually exclusive: read Address/State only when Refusal is -// nil. -type AsOfReadResult struct { - // Address is the version this read served, marking "the address it - // served" (R7.1). Populated only when Refusal is nil. - Address Address - // State is the served version's durable state under R7's record shape. - // Opaque to this suite: the fixture adapter translates a backend's real - // record shape into this map, and no case here asserts its field-level - // content — only that a served result carries an Address. - State map[string]any - // Refusal is populated instead of Address/State when the read cannot be - // answered (R8). Nil means the read was served. - Refusal *AsOfReadRefusal -} - -// AsOfRead resolves storeID's answer to an as-of read against id's lineage, -// at selector (R7.1). -type AsOfRead func(ctx context.Context, storeID, id string, selector AsOfSelector) (AsOfReadResult, error) - -// AsOfReadFixture supplies the capability under test for R7.1: a read whose -// selector is an instant or a version address, resolving to the latest -// version accepted at or before that point, or a typed refusal. -type AsOfReadFixture struct { - IssuePrefix string - - // Resolve performs the as-of read under test (R7.1). A nil hook means - // this backend does not implement as-of reads yet, and every case in - // this file skips with that reason. - Resolve AsOfRead - - // MintAt mints a fresh version for id under storeID, recorded as - // accepted at the given instant, and returns its Address. Cases use - // this to build a lineage with known accept-times to resolve an as-of - // read against. A nil hook means this backend has no way to mint a - // version at a controlled accept-time, and cases that need one skip - // with that reason. - MintAt func(ctx context.Context, storeID, id string, acceptedAt time.Time) (Address, error) - - // MakeUnanswerable arranges for id to be unanswerable at storeID for - // the given Restriction (one of RestrictionGoneRetention/GoneErasure/ - // GoneReorganization/Unknown) — independently of - // RetentionFixture/EpochFixture's own removal machinery, since R7.1's - // refusal is a sibling contract to R17/R20, not a dependent of them. A - // nil hook means this backend has no way to arrange this, and the case - // that needs it skips with that reason. - MakeUnanswerable func(ctx context.Context, storeID, id string, restriction Restriction) error -} - -// RunAsOfReadSelectorAcceptsEitherAnInstantOrAVersionAddress pins R7.1's -// selector shape: an instant T and the version address that instant -// resolves to are two names for the same answer, and a store must accept -// either. -func RunAsOfReadSelectorAcceptsEitherAnInstantOrAVersionAddress(t *testing.T, ctx context.Context, fixture AsOfReadFixture) { - t.Helper() - if fixture.Resolve == nil { - t.Skip("this backend does not implement as-of reads (Resolve is nil)") - } - if fixture.MintAt == nil { - t.Skip("this backend has no way to mint a version at a controlled accept-time (MintAt is nil)") - } - store := asOfReadStore(fixture, "selector-kinds") - id := fixture.IssuePrefix + "-asof-selector" - t0 := time.Now().UTC().Add(-time.Hour) - asOfReadMint(t, ctx, fixture, store, id, t0) - t1 := t0.Add(time.Minute) - second := asOfReadMint(t, ctx, fixture, store, id, t1) - - byInstant, err := fixture.Resolve(ctx, store, id, AsOfSelector{At: &t1}) - if err != nil { - t.Fatalf("Resolve(selector=instant): %v", err) - } - if byInstant.Refusal != nil { - t.Fatalf("Resolve(selector=instant) = %+v, want a served result", byInstant) - } - - byVersion, err := fixture.Resolve(ctx, store, id, AsOfSelector{Version: &second}) - if err != nil { - t.Fatalf("Resolve(selector=version address): %v", err) - } - if byVersion.Refusal != nil { - t.Fatalf("Resolve(selector=version address) = %+v, want a served result", byVersion) - } - - if byInstant.Address != second { - t.Errorf("Resolve(selector=instant matching a version's own accept-time).Address = %s, want %s", byInstant.Address, second) - } - if byVersion.Address != second { - t.Errorf("Resolve(selector=version address).Address = %s, want the same address named %s", byVersion.Address, second) - } - if byInstant.Address != byVersion.Address { - t.Errorf("an instant selector (served %s) and a version-address selector naming the same version (served %s) resolved to different addresses; R7.1 accepts either as a name for the same answer", byInstant.Address, byVersion.Address) - } -} - -// RunAsOfReadReturnsTheLatestVersionAtOrBeforeTStrictlyExcludingLater pins -// R7.1's ordering promise: resolving at T returns the latest version -// accepted at or before T, and a version accepted after T is excluded even -// when its own instant is close by. -func RunAsOfReadReturnsTheLatestVersionAtOrBeforeTStrictlyExcludingLater(t *testing.T, ctx context.Context, fixture AsOfReadFixture) { - t.Helper() - if fixture.Resolve == nil { - t.Skip("this backend does not implement as-of reads (Resolve is nil)") - } - if fixture.MintAt == nil { - t.Skip("this backend has no way to mint a version at a controlled accept-time (MintAt is nil)") - } - store := asOfReadStore(fixture, "ordering") - id := fixture.IssuePrefix + "-asof-ordering" - t0 := time.Now().UTC().Add(-time.Hour) - asOfReadMint(t, ctx, fixture, store, id, t0) - t1 := t0.Add(time.Minute) - middle := asOfReadMint(t, ctx, fixture, store, id, t1) - t2 := t0.Add(2 * time.Minute) - asOfReadMint(t, ctx, fixture, store, id, t2) - - atExactly, err := fixture.Resolve(ctx, store, id, AsOfSelector{At: &t1}) - if err != nil { - t.Fatalf("Resolve(T=middle's own instant): %v", err) - } - if atExactly.Refusal != nil || atExactly.Address != middle { - t.Errorf("Resolve(T=middle's own instant) = %+v, want the version accepted exactly at T (%s), not a later one", atExactly, middle) - } - - between := t1.Add(30 * time.Second) - betweenResult, err := fixture.Resolve(ctx, store, id, AsOfSelector{At: &between}) - if err != nil { - t.Fatalf("Resolve(T strictly between two versions): %v", err) - } - if betweenResult.Refusal != nil || betweenResult.Address != middle { - t.Errorf("Resolve(T strictly between two versions) = %+v, want the latest version at-or-before T (%s); the version accepted after T must be strictly excluded", betweenResult, middle) - } -} - -// RunAsOfReadReportsTheAddressItServed pins R7.1's "marking the address it -// served": a served result names the exact Address of the version it -// returned, not merely a truthy success. -func RunAsOfReadReportsTheAddressItServed(t *testing.T, ctx context.Context, fixture AsOfReadFixture) { - t.Helper() - if fixture.Resolve == nil { - t.Skip("this backend does not implement as-of reads (Resolve is nil)") - } - if fixture.MintAt == nil { - t.Skip("this backend has no way to mint a version at a controlled accept-time (MintAt is nil)") - } - store := asOfReadStore(fixture, "reports-address") - id := fixture.IssuePrefix + "-asof-reports-address" - at := time.Now().UTC() - address := asOfReadMint(t, ctx, fixture, store, id, at) - - result, err := fixture.Resolve(ctx, store, id, AsOfSelector{At: &at}) - if err != nil { - t.Fatalf("Resolve: %v", err) - } - if result.Refusal != nil { - t.Fatalf("Resolve = %+v, want a served result", result) - } - if result.Address == "" { - t.Fatal("Resolve returned an empty Address on a served result, want the address of the version it served (R7.1)") - } - if result.Address != address { - t.Errorf("Resolve(...).Address = %s, want the served version's own address %s", result.Address, address) - } -} - -// RunAsOfReadRefusesAsGoneOrUnknownNeverSubstitutingAnotherVersion pins R8: -// a read that cannot be answered refuses as a typed Gone or Unknown outcome -// with a reason, and never silently returns a neighboring version, the -// nearest surviving version, or current state as a substitute — R8's own -// text names all three as non-answers. -func RunAsOfReadRefusesAsGoneOrUnknownNeverSubstitutingAnotherVersion(t *testing.T, ctx context.Context, fixture AsOfReadFixture) { - t.Helper() - if fixture.Resolve == nil { - t.Skip("this backend does not implement as-of reads (Resolve is nil)") - } - if fixture.MintAt == nil { - t.Skip("this backend has no way to mint a version at a controlled accept-time (MintAt is nil)") - } - if fixture.MakeUnanswerable == nil { - t.Skip("this backend has no way to make a lineage unanswerable (MakeUnanswerable is nil)") - } - - for _, restriction := range []Restriction{ - RestrictionGoneRetention, - RestrictionGoneErasure, - RestrictionGoneReorganization, - RestrictionUnknown, - } { - t.Run(restriction.String(), func(t *testing.T) { - store := asOfReadStore(fixture, "unanswerable-"+restriction.String()) - id := fixture.IssuePrefix + "-asof-unanswerable-" + restriction.String() - at := time.Now().UTC() - address := asOfReadMint(t, ctx, fixture, store, id, at) - - if err := fixture.MakeUnanswerable(ctx, store, id, restriction); err != nil { - t.Fatalf("MakeUnanswerable(%s, %s): %v", id, restriction, err) - } - - result, err := fixture.Resolve(ctx, store, id, AsOfSelector{At: &at}) - if err != nil { - t.Fatalf("Resolve after MakeUnanswerable(%s): %v", restriction, err) - } - if result.Refusal == nil { - t.Fatalf("Resolve after MakeUnanswerable(%s) = %+v, want a Refusal, not a served result (R8)", restriction, result) - } - if result.Refusal.Restriction != restriction { - t.Errorf("Refusal.Restriction = %s, want %s", result.Refusal.Restriction, restriction) - } - if result.Refusal.Reason == "" { - t.Error("Refusal.Reason is empty, want a reason (R8)") - } - if result.Address != "" { - t.Errorf("Resolve on a refusal returned Address = %s, want empty: R8 forbids silently substituting a neighboring version, the nearest surviving version, or current state (the pre-refusal address was %s)", result.Address, address) - } - }) - } -} - -// RunAsOfReadIsCompleteOnlyForTheAnsweringStoresHoldings pins R7.1's closing -// clause and TC3: an as-of answer "is complete for the answering store's -// holdings and may change after a sync." This case asserts NOTHING about -// agreement between two stores asked about the same lineage — proving that -// absence is the point, not a gap. It mints a lineage into exactly one of -// two stores and confirms each store answers strictly from its own -// holdings: the store that holds the lineage serves it, and the store that -// never received it answers Unknown (FR-06, reused from R20) rather than -// reaching across to a sibling store's knowledge, erroring, or answering -// Gone — which would claim knowledge of a lineage it never held. -func RunAsOfReadIsCompleteOnlyForTheAnsweringStoresHoldings(t *testing.T, ctx context.Context, fixture AsOfReadFixture) { - t.Helper() - if fixture.Resolve == nil { - t.Skip("this backend does not implement as-of reads (Resolve is nil)") - } - if fixture.MintAt == nil { - t.Skip("this backend has no way to mint a version at a controlled accept-time (MintAt is nil)") - } - holds := asOfReadStore(fixture, "tc3-holds-the-lineage") - neverSynced := asOfReadStore(fixture, "tc3-never-synced") - id := fixture.IssuePrefix + "-asof-tc3" - at := time.Now().UTC() - address := asOfReadMint(t, ctx, fixture, holds, id, at) - - local, err := fixture.Resolve(ctx, holds, id, AsOfSelector{At: &at}) - if err != nil { - t.Fatalf("Resolve on the store that holds the lineage: %v", err) - } - if local.Refusal != nil || local.Address != address { - t.Fatalf("Resolve on the store that holds the lineage = %+v, want it served from that store's own holdings (%s)", local, address) - } - - remote, err := fixture.Resolve(ctx, neverSynced, id, AsOfSelector{At: &at}) - if err != nil { - t.Fatalf("Resolve on a store that never received this lineage: %v", err) - } - if remote.Refusal == nil { - t.Fatalf("Resolve on a store that never received this lineage = %+v, want a Refusal: an as-of answer is complete for the answering store's own holdings, not a sibling's (TC3)", remote) - } - if remote.Refusal.Restriction != RestrictionUnknown { - t.Errorf("Refusal.Restriction on the never-synced store = %s, want RestrictionUnknown: no lineage knowledge is Unknown, not Gone (FR-06)", remote.Refusal.Restriction) - } -} - -// --- fixture helpers ------------------------------------------------------- - -// asOfReadStore names a storeID namespaced by the fixture's IssuePrefix and -// tag, so cases that need more than one store (e.g. the TC3 local- -// completeness case) get independent ones. -func asOfReadStore(fixture AsOfReadFixture, tag string) string { - return fixture.IssuePrefix + "-asof-store-" + tag -} - -// asOfReadMint mints a version for id on store at the given instant via -// fixture.MintAt, fataling the case if minting fails. -func asOfReadMint(t *testing.T, ctx context.Context, fixture AsOfReadFixture, store, id string, at time.Time) Address { - t.Helper() - address, err := fixture.MintAt(ctx, store, id, at) - if err != nil { - t.Fatalf("MintAt(%s, %s, %s): %v", store, id, at, err) - } - return address -} diff --git a/backend/http/BUILD.bazel b/backend/http/BUILD.bazel deleted file mode 100644 index f6359f820..000000000 --- a/backend/http/BUILD.bazel +++ /dev/null @@ -1,192 +0,0 @@ -load("@rules_go//go:def.bzl", "go_library", "go_test") -load("@rules_shell//shell:sh_test.bzl", "sh_test") - -go_library( - name = "http", - srcs = [ - "attach.go", - "credential.go", - "errors.go", - "http.go", - "target.go", - ], - importpath = "github.com/steveyegge/beads/backend/http", - visibility = ["//visibility:public"], - deps = [ - "//backend", - "//internal/configfile", - "//internal/gitignore", - "//internal/httpapi/apigen", - "//internal/httpclient", - "//internal/httpclient/wire", - "//internal/storage/backends", - ], -) - -go_test( - name = "http_test", - # The served e2e cases run ~4-5 min on the farm under - # BEADS_TEST_EMBEDDED_DOLT=1, past the medium 300s action limit. - size = "large", - timeout = "long", - srcs = [ - "attach_test.go", - "e2e_blocked_flags_test.go", - "e2e_blocked_test.go", - "e2e_cli_test.go", - "e2e_export_test.go", - "e2e_gates_test.go", - "e2e_heartbeat_test.go", - "e2e_link_test.go", - "e2e_list_filters_test.go", - "e2e_list_page_edges_test.go", - "e2e_mol_test.go", - "e2e_promote_test.go", - "e2e_read_your_own_write_test.go", - "e2e_recompute_blocked_test.go", - "e2e_remote_roles_test.go", - "e2e_request_budget_test.go", - "e2e_search_test.go", - "e2e_show_current_test.go", - "e2e_show_single_fetch_test.go", - "open_test.go", - "per_open_credential_test.go", - "register_test.go", - ], - # The e2e files exec the real bd (bazeltest.PrebuiltBD); they run under - # BEADS_TEST_EMBEDDED_DOLT=1 and skip otherwise. - data = ["//cmd/bd:bd_for_tests"], - env = {"BEADS_TEST_BD_BINARY": "$(rlocationpath //cmd/bd:bd_for_tests)"}, - deps = [ - ":http", - "//backend", - "//internal/configfile", - "//internal/httpapi", - "//internal/httpclient", - "//internal/storage", - "//internal/storage/backends", - "//internal/storage/domain", - "//internal/storage/embeddeddolt", - "//internal/storage/issueops", - "//internal/testutil/bazeltest", - ], -) - -# http_test again, with the e2e tier on: BEADS_TEST_EMBEDDED_DOLT=1 starts the -# in-process server over embedded Dolt and BEADS_HTTP_TEST_REQUIRED=1 turns a -# skip of that tier into a failure. http_test alone skips the TestE2E_* cases -# in every lane, because no lane sets those variables for it. It repeats -# http_test's `env` (go_test_pinned_shard.sh, like go_test_variant.sh, does -# not inherit it). -# -# Shard count: the package's tests are serial, and as one shard the target -# ran 235-394s on the lane's executors (2026-10-10..13), the embedded lane's -# longest action in most runs where it executed; the lane's other shards end -# by ~90-125s. The 19 TestE2E_* cases that take >= 3s are pinned -# (tools/bazel/go_test_pinned_shard.sh, served_pinned_shards.txt from -# tools/bazel/pin_shards.py --shards 6 --min-seconds 3) onto 6 shards, and -# the binary's round-robin runs the other ~28 (well under a second -# together) in the 7th. The durations are the maximum over two sharded runs -# of the lane (bazel.yml's bazel-embedded-served-testxml artifact): 11-90s -# each, ~770s together. The same test's time varied 2-5x between those runs -# with the executors' load, and sharded runs took up to 2x what the -# single-shard runs did, so size the pins from sharded runs of the lane, -# never from a single-shard or local one. -# Regenerate the manifest from PR runs' bazel-embedded-served-testxml -# artifacts (each http_served_test directory holds shard_*_of_7/test.xml; -# pass several runs, pin_shards.py takes each test's maximum): -# python3 tools/bazel/pin_shards.py --shards 6 --min-seconds 3 \ -# --write backend/http/served_pinned_shards.txt \ -# /http_served_test /http_served_test -# -# Tags: -# embedded: the embedded-Dolt tier; excluded from --config=prcore/ci, -# selected by --config=embedded. -sh_test( - name = "http_served_test", - size = "large", - # -test.timeout=19m fires (with a goroutine dump) before Bazel's 1200s - # action limit (eternal under --config=embedded). - timeout = "eternal", - srcs = ["//tools/bazel:go_test_pinned_shard.sh"], - args = [ - "$(rootpath :served_pinned_shards.txt)", - "$(rootpath :http_test)", - "-test.v", - "-test.count=1", - "-test.timeout=19m", - ], - data = [ - "served_pinned_shards.txt", - ":http_test", - "//cmd/bd:bd_for_tests", - ], - env = { - "BEADS_HTTP_TEST_REQUIRED": "1", - "BEADS_TEST_BD_BINARY": "$(rlocationpath //cmd/bd:bd_for_tests)", - "BEADS_TEST_EMBEDDED_DOLT": "1", - }, - shard_count = 7, - tags = ["embedded"], -) - -# --- begin repo_files (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) --- - -filegroup( - name = "repo_doc_files", - srcs = glob( - [ - "**/*.md", - "**/*.mdx", - ], - allow_empty = True, - exclude = ["**/node_modules/**"], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_go_srcs", - srcs = glob( - ["**/*.go"], - allow_empty = True, - exclude = [ - "**/*_test.go", - "**/node_modules/**", - ], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_go_test_srcs", - srcs = glob( - ["**/*_test.go"], - allow_empty = True, - exclude = ["**/node_modules/**"], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_other_files", - srcs = glob( - ["**"], - exclude = [ - "**/*.db", - "**/*.exe", - "**/*.go", - "**/*.md", - "**/*.mdx", - "**/*.out", - "**/*.prof", - "**/*.pyc", - "**/*.test", - "**/__pycache__/**", - "**/node_modules/**", - ], - ), - visibility = ["//:__subpackages__"], -) - -# --- end repo_files --- diff --git a/backend/http/attach.go b/backend/http/attach.go deleted file mode 100644 index f2175ff5f..000000000 --- a/backend/http/attach.go +++ /dev/null @@ -1,133 +0,0 @@ -// Written fresh for OSS beads S6 (no bd-enterprise source copied): closes the -// S6 review's MED-8 finding, which asked for one public helper an embedder -// (or `bd connect`) can call to activate a workspace for this backend, -// instead of each caller hand-assembling the two-file write itself. -package bdhttp - -import ( - "context" - "fmt" - - "github.com/steveyegge/beads/internal/configfile" - "github.com/steveyegge/beads/internal/gitignore" - "github.com/steveyegge/beads/internal/httpclient" -) - -// Attach activates the http backend for the workspace at beadsDir: it writes -// the per-user activation sidecar (SaveTarget) pinning target's server and -// project, then sets the workspace's git-tracked backend selection in -// metadata.json to this backend, preserving every other configured field -// (database, project id, ...) and creating a fresh default metadata.json only -// when none exists yet. Both files are written atomically (temp file plus -// rename, 0600): see SaveTarget and configfile.Config.Save. -// -// Unless the caller already set target.PreviousBackend, Attach fills it in -// from the workspace's PRIOR backend selection, so `bd connect --clear` can -// restore it later; see that field's doc. -// -// Attach does NOT verify target itself — it has no network access and takes -// target on faith. A caller MUST call Handshake (or httpclient.Handshake) -// against target first and only call Attach once that succeeds; attaching an -// unverified target lets a workspace record a server that is wrong, hostile, -// or simply never answers. Connect, below, does both in the right order and -// is the easier door for a caller that does not need them split. -// -// Attach does not create beadsDir itself — MkdirAll is the caller's job, same -// as SaveTarget's own precondition. It DOES ensure both of this backend's -// per-user filenames are covered by beadsDir's .gitignore -// (gitignore.EnsurePatternIgnored): the sidecar, and -// httpclient.LocalMetadataFileName, which the store writes on first use. That -// is the same guarantee `bd connect` makes via doctor.EnsureGitignoreForBeadsDir -// — but through a small, independent implementation here rather than -// importing cmd/bd/doctor, whose package as a whole pulls in dolt and -// git-process dependencies that would break backend/http's promise of a -// minimal dependency footprint for an embedder. The two are complementary and -// idempotent together. -// -// It is the one place that writes BOTH of the files that activate beads/http -// (the sidecar and metadata.json's backend selection; the local metadata file -// is the store's own), so a caller that used to hand-assemble the pair -// (`bd connect`'s own connectCmd.RunE, test/embedder's linkage proof) can call -// this instead and stay correct across a future change to either file's shape. -func Attach(beadsDir string, target Target) error { - cfg, err := configfile.Load(beadsDir) - if err != nil { - return fmt.Errorf("reading %s: %w", configfile.ConfigFileName, err) - } - if cfg == nil { - cfg = configfile.DefaultConfig() - } - // target.PreviousBackend (bee-ghosttrack CHANGES_REQUESTED on #7288, - // should-fix 1) is what `bd connect --clear` restores metadata.json's - // backend selection to. A caller rarely sets it itself (connect.go - // doesn't), so fill it in here unless the workspace is ALREADY on this - // backend — a re-connect (a new URL, or re-pinning the same one) must - // not overwrite an earlier recorded previous backend with "http" itself, - // or a chain of reconnects would forget what to restore and --clear - // would land back on http with no sidecar. Carry the ALREADY-recorded - // value forward instead; a failed or missing read just leaves it "", - // the same as a workspace with nothing to restore. The check reads the - // raw field: in a process that never ran Register, GetBackend() reports an - // http workspace as the dolt default, and the reconnect would record that. - if target.PreviousBackend == "" { - if cfg.Backend == httpclient.Backend { - if prior, err := httpclient.LoadTarget(beadsDir); err == nil { - target.PreviousBackend = prior.PreviousBackend - } - } else { - target.PreviousBackend = cfg.GetBackend() - } - } - if err := SaveTarget(beadsDir, target); err != nil { - return fmt.Errorf("writing %s: %w", httpclient.TargetFileName, err) - } - // Both of this backend's per-user files: the sidecar just written, and - // the local metadata file the store itself writes on first use, which - // never passes through here and so must be covered in advance. - for _, name := range []string{httpclient.TargetFileName, httpclient.LocalMetadataFileName} { - if err := gitignore.EnsurePatternIgnored(beadsDir, name); err != nil { - return fmt.Errorf("ensuring %s is gitignored: %w", name, err) - } - } - // Only the backend SELECTION changes here. cfg.Database/cfg.ProjectID (if - // an existing metadata.json carried them for a different, prior backend) - // are left exactly as loaded — see connect.go's own comment on this same - // hazard: this backend's identity lives entirely in the sidecar just - // written above, and clobbering those fields would corrupt a future - // `bd connect --clear` back to a different backend. - cfg.Backend = httpclient.Backend - if err := cfg.Save(beadsDir); err != nil { - return fmt.Errorf("writing %s: %w", configfile.ConfigFileName, err) - } - return nil -} - -// Connect is Handshake followed by Attach: it verifies target — the same -// server-identity check `bd connect` performs before it ever writes -// anything — and only then activates the workspace. It is the convenience -// door for a caller that wants Attach's ordering guarantee (never attach a -// workspace to a server this process has not handshaken) without composing -// Handshake and Attach itself. -// -// Mirroring `bd connect`'s own project-id pinning: if target.ExpectProjectID -// is empty, Connect pins it to whatever project the server answered with -// before calling Attach, so the workspace is tied to that project from this -// call on. If it is already set, Handshake itself refuses a mismatch and -// Connect returns that error without calling Attach at all. Connect does not -// replicate `bd connect`'s other CLI-level policy — --force re-pinning an -// already-attached workspace, or refusing to switch a workspace from a -// different backend — those stay the command's own job; Connect's contract -// is exactly "handshake, then attach". -func Connect(ctx context.Context, beadsDir string, target Target, opts Options) (*ServerSnapshot, error) { - snapshot, err := Handshake(ctx, target, opts) - if err != nil { - return nil, err - } - if target.ExpectProjectID == "" { - target.ExpectProjectID = snapshot.ProjectID - } - if err := Attach(beadsDir, target); err != nil { - return nil, err - } - return snapshot, nil -} diff --git a/backend/http/attach_test.go b/backend/http/attach_test.go deleted file mode 100644 index 7594b3240..000000000 --- a/backend/http/attach_test.go +++ /dev/null @@ -1,180 +0,0 @@ -package bdhttp_test - -// Written fresh for OSS beads S6: pins the bee-ghosttrack CHANGES_REQUESTED -// finding on #7288, should-fix 1 — `bd connect --clear` restoring a -// workspace's previous backend selection needs Attach to have recorded what -// that previous backend WAS, onto the sidecar, at connect time. - -import ( - "net/url" - "os" - "path/filepath" - "strings" - "testing" - - bdhttp "github.com/steveyegge/beads/backend/http" - "github.com/steveyegge/beads/internal/configfile" - "github.com/steveyegge/beads/internal/httpclient" -) - -func mustAttachTestURL(t *testing.T, raw string) *url.URL { - t.Helper() - u, err := url.Parse(raw) - if err != nil { - t.Fatalf("url.Parse(%q): %v", raw, err) - } - return u -} - -// TestAttachRecordsThePriorBackendAsPreviousBackend is the first-connect -// case: a workspace that selected "dolt" before ever running `bd connect` -// must have Attach record "dolt" as PreviousBackend, so --clear can restore -// it later. -func TestAttachRecordsThePriorBackendAsPreviousBackend(t *testing.T) { - beadsDir := t.TempDir() - cfg := &configfile.Config{Backend: "dolt", Database: "beads.db"} - if err := cfg.Save(beadsDir); err != nil { - t.Fatalf("seed metadata.json: %v", err) - } - - target := bdhttp.Target{BaseURL: mustAttachTestURL(t, "http://127.0.0.1:8080")} - if err := bdhttp.Attach(beadsDir, target); err != nil { - t.Fatalf("Attach: %v", err) - } - - got, err := bdhttp.LoadTarget(beadsDir) - if err != nil { - t.Fatalf("LoadTarget: %v", err) - } - if got.PreviousBackend != "dolt" { - t.Errorf("PreviousBackend = %q, want %q", got.PreviousBackend, "dolt") - } - - after, err := configfile.Load(beadsDir) - if err != nil { - t.Fatalf("reload metadata.json: %v", err) - } - // Asserts on the raw stored field, not GetBackend(): GetBackend() falls - // back to BackendDolt for any name backendnames.Has doesn't recognize - // (TestGetBackendAllowlist in internal/configfile), and "http" is only - // ever added there by bdhttp.Register/httpclient.Register — a - // once-per-process, panics-on-duplicate production wiring call this test - // package must not make. The raw field is exactly what Attach controls. - if after.Backend != httpclient.Backend { - t.Errorf("metadata.json backend = %q after Attach, want %q", after.Backend, httpclient.Backend) - } -} - -// TestAttachOnAFreshWorkspaceRecordsTheImplicitDoltDefault covers a -// workspace with no metadata.json at all: GetBackend()'s own default -// ("dolt") is what a future --clear restores to, matching what every other -// backend-selection read in this codebase already treats as this -// workspace's backend. -func TestAttachOnAFreshWorkspaceRecordsTheImplicitDoltDefault(t *testing.T) { - beadsDir := t.TempDir() - target := bdhttp.Target{BaseURL: mustAttachTestURL(t, "http://127.0.0.1:8080")} - if err := bdhttp.Attach(beadsDir, target); err != nil { - t.Fatalf("Attach: %v", err) - } - got, err := bdhttp.LoadTarget(beadsDir) - if err != nil { - t.Fatalf("LoadTarget: %v", err) - } - if got.PreviousBackend != "dolt" { - t.Errorf("PreviousBackend = %q, want %q (configfile.BackendDolt)", got.PreviousBackend, "dolt") - } -} - -// TestAttachOnAReconnectKeepsTheEarlierPreviousBackend covers a workspace -// ALREADY on http (a bare re-connect, or --force re-pinning the same -// backend): Attach must not overwrite an already-recorded PreviousBackend -// with "http" itself, or a chain of reconnects would forget what --clear is -// supposed to restore. -func TestAttachOnAReconnectKeepsTheEarlierPreviousBackend(t *testing.T) { - beadsDir := t.TempDir() - // Not dolt: an unregistered "http" reads back through GetBackend() as the - // dolt default, so a dolt prior could not tell carrying it forward from - // recording that default. - cfg := &configfile.Config{Backend: "postgres", Database: "beads.db"} - if err := cfg.Save(beadsDir); err != nil { - t.Fatalf("seed metadata.json: %v", err) - } - - first := bdhttp.Target{BaseURL: mustAttachTestURL(t, "http://127.0.0.1:8080")} - if err := bdhttp.Attach(beadsDir, first); err != nil { - t.Fatalf("first Attach: %v", err) - } - - // A second connect to a different url, same workspace, same backend - // ("http", already selected) — the shape `bd connect --force` to a new - // server produces for an already-http workspace. - second := bdhttp.Target{BaseURL: mustAttachTestURL(t, "http://127.0.0.1:9090")} - if err := bdhttp.Attach(beadsDir, second); err != nil { - t.Fatalf("second Attach: %v", err) - } - - got, err := bdhttp.LoadTarget(beadsDir) - if err != nil { - t.Fatalf("LoadTarget: %v", err) - } - if got.PreviousBackend != "postgres" { - t.Errorf("PreviousBackend after a reconnect = %q, want the original %q carried forward, not the just-left \"http\"", got.PreviousBackend, "postgres") - } - if got.BaseURL.String() != "http://127.0.0.1:9090" { - t.Errorf("BaseURL = %q, want the second connect's url", got.BaseURL.String()) - } -} - -// TestAttachHonorsACallerSuppliedPreviousBackend covers Connect's doc -// contract: a caller that already set target.PreviousBackend itself (none -// of this package's own callers do today) must have that value win over -// Attach's own inference. -func TestAttachHonorsACallerSuppliedPreviousBackend(t *testing.T) { - beadsDir := t.TempDir() - cfg := &configfile.Config{Backend: "dolt", Database: "beads.db"} - if err := cfg.Save(beadsDir); err != nil { - t.Fatalf("seed metadata.json: %v", err) - } - - target := bdhttp.Target{BaseURL: mustAttachTestURL(t, "http://127.0.0.1:8080"), PreviousBackend: "mysql"} - if err := bdhttp.Attach(beadsDir, target); err != nil { - t.Fatalf("Attach: %v", err) - } - got, err := bdhttp.LoadTarget(beadsDir) - if err != nil { - t.Fatalf("LoadTarget: %v", err) - } - if got.PreviousBackend != "mysql" { - t.Errorf("PreviousBackend = %q, want the caller-supplied %q to win over the inferred %q", got.PreviousBackend, "mysql", "dolt") - } -} - -// TestAttachGitignoresBothPerUserFiles pins Attach's .gitignore guarantee for -// both files this backend keeps per user in beadsDir: the sidecar Attach -// writes, and the local metadata file the store writes later, on first use. -// `bd connect` also covers the second through doctor's required patterns, -// but an embedder activating through Attach alone has only this. A reconnect -// must not duplicate either line. -func TestAttachGitignoresBothPerUserFiles(t *testing.T) { - beadsDir := t.TempDir() - for _, raw := range []string{"http://127.0.0.1:8080", "http://127.0.0.1:9090"} { - if err := bdhttp.Attach(beadsDir, bdhttp.Target{BaseURL: mustAttachTestURL(t, raw)}); err != nil { - t.Fatalf("Attach(%s): %v", raw, err) - } - } - content, err := os.ReadFile(filepath.Join(beadsDir, ".gitignore")) - if err != nil { - t.Fatalf("read .gitignore after Attach: %v", err) - } - for _, name := range []string{httpclient.TargetFileName, httpclient.LocalMetadataFileName} { - count := 0 - for _, line := range strings.Split(string(content), "\n") { - if strings.TrimSpace(line) == name { - count++ - } - } - if count != 1 { - t.Errorf(".gitignore names %s %d time(s) after two Attaches, want exactly 1:\n%s", name, count, content) - } - } -} diff --git a/backend/http/corpus/BUILD.bazel b/backend/http/corpus/BUILD.bazel deleted file mode 100644 index ee8631077..000000000 --- a/backend/http/corpus/BUILD.bazel +++ /dev/null @@ -1,146 +0,0 @@ -load("@rules_go//go:def.bzl", "go_test") -load("@rules_shell//shell:sh_test.bzl", "sh_test") - -go_test( - name = "corpus_test", - # The corpus runs every shape through the real bd (bazeltest.PrebuiltBD), - # served and embedded, under BEADS_TEST_EMBEDDED_DOLT=1 (corpus_served_test - # below); it skips otherwise. - size = "large", - timeout = "long", - srcs = [ - "corpus_parts_test.go", - "corpus_test.go", - "gate_check_served_test.go", - "harness_test.go", - "reference_proxy_test.go", - "required_corpus_tier_test.go", - ], - data = glob(["testdata/**"]) + ["//cmd/bd:bd_for_tests"], - env = {"BEADS_TEST_BD_BINARY": "$(rlocationpath //cmd/bd:bd_for_tests)"}, - deps = [ - "//internal/httpapi", - "//internal/httpapi/apigen", - "//internal/storage", - "//internal/storage/domain", - "//internal/storage/embeddeddolt", - "//internal/testutil/bazeltest", - "//internal/types", - "@in_gopkg_yaml_v3//:yaml_v3", - ], -) - -# corpus_test again, with the corpus tier on: BEADS_TEST_EMBEDDED_DOLT=1 runs -# TestServedCorpusPart1..N and BEADS_HTTP_TEST_REQUIRED=1 turns a skip of -# them into a failure. corpus_test alone skips them in every lane, because no -# lane sets those variables for it. It repeats corpus_test's `env` -# (go_test_pinned_shard.sh, like go_test_variant.sh, does not inherit it). -# -# Shard count: as one shard (one TestServedCorpus over every shape) the -# target ran 292-597s on the lane's executors (2026-10-11..13), the embedded -# lane's longest action in most runs where it executed. About 100s of that -# is the per-process server and seed, the rest the ~130 shapes (~1.9s each, -# serial, ~242s together). The shapes are now dealt round robin across -# corpusParts = 4 parts (corpus_test.go), each a top-level test with its own -# server and seed, and served_pinned_shards.txt pins part k to shard k; the -# 5th shard runs the package's other tests through the binary's -# round-robin (the engine-free ones, plus -# TestServedGateCheckOverAwaitTypedGates, which seeds its own small served -# store). Each part pays the setup again, though it seeds the served -# and embedded sides concurrently (~62-76s on the lane's executors), so a -# part's shard is that plus ~60s of shapes: 114-146s measured. Three parts -# ran 170-180s each; they save one setup (~75 test-seconds a run) but -# lengthen the lane's served long pole, which is on the gate's critical -# path, by ~34s. -# -# Tags: -# embedded: the embedded-Dolt tier; excluded from --config=prcore/ci, -# selected by --config=embedded. -sh_test( - name = "corpus_served_test", - size = "large", - # -test.timeout=19m fires (with a goroutine dump) before Bazel's 1200s - # action limit (eternal under --config=embedded). - timeout = "eternal", - srcs = ["//tools/bazel:go_test_pinned_shard.sh"], - args = [ - "$(rootpath :served_pinned_shards.txt)", - "$(rootpath :corpus_test)", - "-test.v", - "-test.count=1", - "-test.timeout=19m", - ], - data = [ - "served_pinned_shards.txt", - ":corpus_test", - "//cmd/bd:bd_for_tests", - ], - env = { - "BEADS_HTTP_TEST_REQUIRED": "1", - "BEADS_TEST_BD_BINARY": "$(rlocationpath //cmd/bd:bd_for_tests)", - "BEADS_TEST_EMBEDDED_DOLT": "1", - }, - shard_count = 5, - tags = ["embedded"], -) - -# --- begin repo_files (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) --- - -filegroup( - name = "repo_doc_files", - srcs = glob( - [ - "**/*.md", - "**/*.mdx", - ], - allow_empty = True, - exclude = ["**/node_modules/**"], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_go_srcs", - srcs = glob( - ["**/*.go"], - allow_empty = True, - exclude = [ - "**/*_test.go", - "**/node_modules/**", - ], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_go_test_srcs", - srcs = glob( - ["**/*_test.go"], - allow_empty = True, - exclude = ["**/node_modules/**"], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_other_files", - srcs = glob( - ["**"], - exclude = [ - "**/*.db", - "**/*.exe", - "**/*.go", - "**/*.md", - "**/*.mdx", - "**/*.out", - "**/*.prof", - "**/*.pyc", - "**/*.test", - "**/__pycache__/**", - "**/node_modules/**", - ], - ), - visibility = ["//:__subpackages__"], -) - -# --- end repo_files --- diff --git a/backend/http/corpus/README.md b/backend/http/corpus/README.md deleted file mode 100644 index 28451a0cf..000000000 --- a/backend/http/corpus/README.md +++ /dev/null @@ -1,154 +0,0 @@ -# Served command corpus - -This suite is the working definition of "bd fully supports a remote HTTP -backend". It runs every distinct `bd` command shape that an orchestrator (gc) -and its packs and formulas issue against a work store. Each shape runs -against a served workspace and is compared with the same command on an -embedded workspace. - -## What runs - -The shapes are dealt round robin, in corpus order, across `corpusParts` -parts (`corpus_test.go`). Each part is a top-level test, -`TestServedCorpusPart1` .. `TestServedCorpusPartN`, and each does the -following for its share of the shapes: - -1. Builds `bd` from this tree. `BEADS_TEST_BD_BINARY=` reuses a prebuilt - binary instead. -2. Starts an in-process `bd serve` (`httpapi.Listen` over embedded Dolt) the - way a deployed server is bound: `AllowNonLoopback` plus a bearer token - file. The socket is still `127.0.0.1`, but the bind mode decides every - behaviour the server keys on, including its refusal of unlimited reads. -3. Puts a reference proxy in front of the server. The proxy makes it answer - like the deployed reference server described in - `testdata/reference_server.json`: - - the context handshake publishes exactly that server's capability set and - its `ContextResponse` members (so no `wire_revision`); - - an operation the reference server does not publish gets the server's own - 404; - - a query parameter or top-level request member it does not declare gets - the server's own 400 `unknown_parameter`. -4. Runs `bd connect` on one workspace and `bd init` on another. Both get the - `seed` from `testdata/corpus.yaml`: one graph of named rows, plus bulk - graphs that put more than one server page of ready rows and wisps in the - store. Both sides use `BEADS_ACTOR=worker` and set `CLAUDE_SESSION_ID`. - The embedded side is initialized and seeded on its own goroutine while - steps 2 and 3 run and the served side is seeded (`prepareSide`): the two - sides share no directory, env or process, and each runs the seed in - order, so the seeded data does not depend on the overlap. -5. Runs each of its shapes as a subtest, on both sides. The embedded run must - exit with the shape's `expect_exit` and match its `expect_stderr`. If it does - not, the shape is wrong or bd changed, and the subtest fails as a corpus - bug that no pending mark or allowlist entry hides. The served run must: - - exit like the embedded run; - - carry no refusal (`ErrUnsupported`, a v0-wire refusal, the - loopback-only 400, or a reference-profile 404/400); - - match the embedded output under the shape's `compare` mode, after - timestamps and revision tokens are dropped (but not `defer_until` or - `due_at`: a shape sets those itself, as absolute dates, and they must - match exactly); - - for writes, give the same JSON from every `verify` read on both sides - afterwards. - -A part takes its own server, workspaces and seed, so no shape can see -another part's writes. That is safe because no shape depends on another -shape's effects: -- every check compares the served run with the embedded run after the same - commands, so it holds whatever ran before it on both sides; -- each part keeps corpus order, so its reads still run on the bare seed, - before any of its writes; -- each write owns its target ids (`e2e-t-*`, or ids it creates). - -`TestCorpusPartsDealEveryShapeOnce` checks that every shape is in exactly one -part, in corpus order. A new shape must keep that independence: it may rely on -the seed and its own `setup`, never on another shape having run. - -Every part seeds again, so running all the parts in one process takes -longer than one run over every shape did; each part logs its setup time -(`part N setup ...`). Run one part with -`-run 'TestServedCorpusPart2$'`. - -There are four parts. In the embedded lane each part's setup (server start -plus both seeds, run concurrently) takes ~62-76s and the shapes ~242s in -total. Four parts ran 114-146s each and three ran 170-180s. Three would save -one setup (~75 test-seconds a run), but the longest part is the lane's -served long pole and sits on the CI gate's critical path, so four parts win. -To change the count, update `corpusParts`, `servedCorpusParts`, -`served_pinned_shards.txt` and the target's `shard_count` together. - -``` -CGO_ENABLED=1 BEADS_TEST_EMBEDDED_DOLT=1 BEADS_HTTP_TEST_REQUIRED=1 \ - go test -tags gms_pure_go -run 'TestServedCorpus|TestCorpus' ./backend/http/corpus/ -``` - -In CI, `//backend/http/corpus:corpus_served_test` runs the suite with both -variables set, in the Embedded Dolt lane (`--config=embedded`; see -[engdocs/TESTING.md](../../../engdocs/TESTING.md)). Its -`served_pinned_shards.txt` pins part k to Bazel shard k, so the parts run -concurrently. `corpus_test` on its own sets neither variable, so it skips -the parts. - -Two environment variables change the run: -- `BEADS_CORPUS_REPORT=` writes a JSON summary with per-shape status - and request counts. -- `BEADS_CORPUS_SKIP_PENDING=1` skips pending shapes without running them. - -## The data - -| File | Holds | -|---|---| -| `testdata/corpus.yaml` | assets, the seed, and the shapes | -| `testdata/allowlist.yaml` | permanent refusals, each with its reason and the refusal text the served run must match | -| `testdata/reference_server.json` | the reference server's wire surface | - -Each shape in `corpus.yaml` has these fields: - -| Field | Meaning | -|---|---| -| `id` | `gc-*` for orchestrator subprocess shapes, `mc-*` for pack/formula/prompt shapes | -| `source` | where the shape comes from | -| `args` | the argv; `{file:NAME}` expands to an asset path and `{rev:ID}` to that side's current revision of `ID` | -| `stdin` | text piped to `bd` on both sides | -| `setup` | commands run on both sides before the shape; one that fails fails the shape | -| `compare` | `json` (default), `ids`, `count`, `text` or `exit` | -| `ordered` | under `json` and `ids`, array order is significant; by default arrays are sorted before comparing | -| `prefix_of` | for a limited `ids` listing: its unlimited form. Each side's limited rows must be a prefix, in order, of that side's own unlimited listing, both sides return as many rows, and the two unlimited listings hold the same ids. A limit that cuts through a tie set (same priority, `created_at` in the same second) lands on different rows on two workspaces seeded seconds apart, so the limited rows are not compared across sides directly | -| `verify` | reads run after the shape | -| `ignore_fields` | JSON keys dropped, at any depth, before a `json` comparison and before `verify` output is compared; for example comment ids, which each workspace mints itself | -| `expect_exit`, `expect_stderr` | expected outcome on both sides (exit 0 by default). An allowlisted shape must state `expect_exit`; only its embedded run is held to these | -| `pending` | a known failure | -| `requests` | exactly how many requests the served run of `args` makes through the reference proxy, not counting `setup`, `verify` or `{rev:ID}` reads. Each request is a sequential round trip, so the count is the shape's latency floor over a gateway. A change that makes the shape cheaper updates the pin | - -`pending: {slice, reason}` marks a known failure and names the slice or -slices that fix it, for example `S6c+S15`. A pending shape **still runs**: -- while it fails, the subtest is skipped and the skip records what was - observed; -- once it passes, the subtest fails with "now passes; remove the pending - mark". - -So removing a mark is how a slice proves itself, and any other failure is -unexpected and fails the suite. - -## Updating the reference profile - -`reference_server.json` was derived from two things: -- the deployed server's live `GET /v0/beads/context` capabilities; -- its published OpenAPI document (bd 1.3.0, no `wire_revision`). For each - operation the profile keeps the method, the path template, the query - parameter names, and the top-level request members. - -When the deployed server is upgraded (SRV-1), regenerate the file the same -way. Then remove the `SRV-1` pending marks that now pass. - -## Known fidelity limits - -- Responses pass through unfiltered. A response member the reference server - would not send still reaches the client. -- Only top-level request members are checked; nested members (for example - `batchApply` items) are not. -- The served backend underneath is embedded Dolt, not the reference - deployment's storage backend. -- Out of scope, because they are local by design or never touch a remote - store: `init`, `bootstrap`, `migrate`, `doctor`, `flatten`, `gc`, and the - `dolt` subcommands. `context`, `version`, `backup status` and `prime` - are in the corpus, but only checked for exit codes. diff --git a/backend/http/corpus/corpus_parts_test.go b/backend/http/corpus/corpus_parts_test.go deleted file mode 100644 index e8c3ec84f..000000000 --- a/backend/http/corpus/corpus_parts_test.go +++ /dev/null @@ -1,211 +0,0 @@ -//go:build cgo - -package corpus_test - -import ( - "context" - "errors" - "fmt" - "os" - "path/filepath" - "reflect" - "runtime" - "strings" - "sync" - "testing" -) - -// TestCorpusPartsDealEveryShapeOnce runs without the embedded engine. It -// holds the deal TestServedCorpusPart1..N run to what makes splitting the -// corpus safe: every shape lands in exactly one part, each part keeps corpus -// order (so a part's reads still run before its writes, on its own fresh -// seed), no part is empty, and there is one TestServedCorpusPartN per part. -func TestCorpusPartsDealEveryShapeOnce(t *testing.T) { - if len(servedCorpusParts) != corpusParts { - t.Fatalf("servedCorpusParts has %d tests, want corpusParts = %d", len(servedCorpusParts), corpusParts) - } - c, _ := loadCorpus(t) - index := map[string]int{} - for i, s := range c.Shapes { - index[s.ID] = i - } - seen := map[string]int{} - for part := 1; part <= corpusParts; part++ { - shapes := corpusPart(c.Shapes, part) - if len(shapes) == 0 { - t.Errorf("part %d runs no shapes", part) - } - last := -1 - for _, s := range shapes { - if prev, dup := seen[s.ID]; dup { - t.Errorf("shape %s is in part %d and part %d", s.ID, prev, part) - } - seen[s.ID] = part - i, ok := index[s.ID] - if !ok { - t.Errorf("part %d runs %s, which is not a corpus shape", part, s.ID) - continue - } - if i <= last { - t.Errorf("part %d runs %s out of corpus order", part, s.ID) - } - last = i - } - } - for _, s := range c.Shapes { - if _, ok := seen[s.ID]; !ok { - t.Errorf("shape %s is in no part", s.ID) - } - } -} - -// The deal itself, on a corpus of 10 against 4 parts: round robin in corpus -// order. -func TestCorpusPartIsRoundRobinInOrder(t *testing.T) { - var shapes []shape - for i := 0; i < 10; i++ { - shapes = append(shapes, shape{ID: fmt.Sprint(i)}) - } - want := map[int]string{1: "[0 4 8]", 2: "[1 5 9]", 3: "[2 6]", 4: "[3 7]"} - for part, w := range want { - var ids []string - for _, s := range corpusPartOf(shapes, part, 4) { - ids = append(ids, s.ID) - } - if got := fmt.Sprint(ids); got != w { - t.Errorf("part %d of 4 = %s, want %s", part, got, w) - } - } -} - -// fakeBD writes a stand-in for bd that appends its arguments, one call per -// line, to calls.log in its working directory, and exits 3 when an argument -// is "boom". -func fakeBD(t *testing.T) string { - t.Helper() - bin := filepath.Join(t.TempDir(), "bd") - script := "#!/bin/sh\necho \"$*\" >> calls.log\nfor a in \"$@\"; do [ \"$a\" = boom ] && { echo boom >&2; exit 3; }; done\nexit 0\n" - if err := os.WriteFile(bin, []byte(script), 0o700); err != nil { - t.Fatal(err) - } - return bin -} - -func calls(t *testing.T, sd *side) []string { - t.Helper() - raw, err := os.ReadFile(filepath.Join(sd.dir, "calls.log")) - if err != nil { - t.Fatal(err) - } - return strings.Split(strings.TrimSpace(string(raw)), "\n") -} - -// prepareSide is what lets testServedCorpus seed the served and embedded -// sides at once: each side attaches, writes its assets and runs the seed in -// its own directories, in seed order, and a failure comes back as an error -// rather than a t.Fatal from a goroutine that is not the test's. -func TestPrepareSideSeedsBothSidesConcurrently(t *testing.T) { - bin := fakeBD(t) - c := &corpusFile{ - Files: []corpusAsset{{Name: "plan.json", Content: "{}"}}, - Seed: [][]string{{"create", "--graph", "{file:plan.json}"}, {"close", "e2e-x"}}, - } - a := &side{name: "served", dir: t.TempDir(), files: t.TempDir()} - b := &side{name: "embedded", dir: t.TempDir(), files: t.TempDir()} - var wg sync.WaitGroup - errs := make([]error, 2) - for i, sd := range []*side{a, b} { - wg.Add(1) - go func() { - defer wg.Done() - errs[i] = prepareSide(context.Background(), t, bin, c, sd, []string{"init", sd.name}) - }() - } - wg.Wait() - for i, sd := range []*side{a, b} { - if errs[i] != nil { - t.Fatalf("%s: %v", sd.name, errs[i]) - } - want := []string{"init " + sd.name, "create --graph " + filepath.Join(sd.files, "plan.json"), "close e2e-x"} - if got := calls(t, sd); !reflect.DeepEqual(got, want) { - t.Errorf("%s ran %q, want %q", sd.name, got, want) - } - if _, err := os.Stat(filepath.Join(sd.files, "plan.json")); err != nil { - t.Errorf("%s: asset not written: %v", sd.name, err) - } - } -} - -func TestPrepareSideReportsAFailedStepAsAnError(t *testing.T) { - bin := fakeBD(t) - c := &corpusFile{Seed: [][]string{{"create", "boom"}, {"close", "e2e-x"}}} - sd := &side{name: "embedded", dir: t.TempDir(), files: t.TempDir()} - err := prepareSide(context.Background(), t, bin, c, sd, []string{"init"}) - if err == nil || !strings.Contains(err.Error(), "seed [create boom] on embedded failed") || !strings.Contains(err.Error(), "exit=3") { - t.Fatalf("err = %v, want the failed seed step with its exit", err) - } - if got := calls(t, sd); !reflect.DeepEqual(got, []string{"init", "create boom"}) { - t.Errorf("ran %q; the seed must stop at the failed step", got) - } - - sd = &side{name: "served", dir: t.TempDir(), files: t.TempDir()} - if err := prepareSide(context.Background(), t, bin, c, sd, []string{"connect", "boom"}); err == nil || !strings.Contains(err.Error(), "bd connect boom on served") { - t.Fatalf("err = %v, want the failed attach", err) - } -} - -// Once the other side has failed, the test cancels: a side stops before its -// next step instead of seeding on for nothing. -func TestPrepareSideStopsWhenCancelled(t *testing.T) { - bin := fakeBD(t) - c := &corpusFile{Seed: [][]string{{"create", "e2e-x"}}} - sd := &side{name: "embedded", dir: t.TempDir(), files: t.TempDir()} - ctx, cancel := context.WithCancel(context.Background()) - cancel() - if err := prepareSide(ctx, t, bin, c, sd, []string{"init"}); !errors.Is(err, context.Canceled) { - t.Fatalf("err = %v, want context.Canceled", err) - } - if _, err := os.Stat(filepath.Join(sd.dir, "calls.log")); !os.IsNotExist(err) { - t.Errorf("bd ran after the cancel (calls.log: %v)", err) - } -} - -// A part test wired to another part's number would run that part's shapes -// twice and drop its own without a word; testServedCorpus refuses to run -// unless the test's name is its part's (corpusPartNameErr). -func TestCorpusPartNameMustMatchItsPart(t *testing.T) { - if err := corpusPartNameErr("TestServedCorpusPart3", 3); err != nil { - t.Errorf("matching name: %v", err) - } - for _, c := range []struct { - name string - part int - }{ - {"TestServedCorpusPart3", 2}, - {"TestServedCorpusPart1", 10}, - {"TestServedCorpusPart10", 1}, - {"TestServedCorpusPart3/sub", 3}, - {"TestServedCorpus", 1}, - // A stray part past corpusParts would run no shapes at all. - {fmt.Sprintf("TestServedCorpusPart%d", corpusParts+1), corpusParts + 1}, - {"TestServedCorpusPart0", 0}, - } { - if err := corpusPartNameErr(c.name, c.part); err == nil { - t.Errorf("corpusPartNameErr(%q, %d) = nil, want a mismatch", c.name, c.part) - } - } - // Every wired part test passes the guard: run each with the engine - // off, so the guard (which comes first) is all that can fail it. - for k, fn := range servedCorpusParts { - want := fmt.Sprintf("TestServedCorpusPart%d", k+1) - if got := testFuncName(fn); got != want { - t.Errorf("servedCorpusParts[%d] is %s, want %s", k, got, want) - } - } -} - -// testFuncName is fn's unqualified function name. -func testFuncName(fn func(*testing.T)) string { - full := runtime.FuncForPC(reflect.ValueOf(fn).Pointer()).Name() - return full[strings.LastIndex(full, ".")+1:] -} diff --git a/backend/http/corpus/corpus_test.go b/backend/http/corpus/corpus_test.go deleted file mode 100644 index ee4c30331..000000000 --- a/backend/http/corpus/corpus_test.go +++ /dev/null @@ -1,968 +0,0 @@ -//go:build cgo - -// Package corpus_test is the served command-corpus suite: every distinct bd -// command shape an orchestrator (gc) and its packs/formulas issue, run by the -// real bd binary against an in-process bd serve masked to a reference -// deployment's wire surface, and compared with the same command on an embedded -// workspace. See README.md. -package corpus_test - -import ( - "context" - "encoding/json" - "errors" - "fmt" - "os" - "path/filepath" - "reflect" - "regexp" - "sort" - "strconv" - "strings" - "sync" - "testing" - "time" - - "gopkg.in/yaml.v3" -) - -// corpusFile is testdata/corpus.yaml. -type corpusFile struct { - Files []corpusAsset `yaml:"files"` - Seed [][]string `yaml:"seed"` - Shapes []shape `yaml:"shapes"` -} - -// corpusAsset is a file the shapes reference as {file:NAME}. Exactly one of -// Content and Bulk is set. -type corpusAsset struct { - Name string `yaml:"name"` - Content string `yaml:"content"` - Bulk *bulkGraph `yaml:"bulk"` -} - -// bulkGraph generates a `bd create --graph` plan of Count independent tasks -// with ids IDPrefix-001.. so the suite can hold more rows than one server page -// without a 100-line literal in the corpus. -type bulkGraph struct { - IDPrefix string `yaml:"id_prefix"` - Count int `yaml:"count"` - Priority int `yaml:"priority"` - Ephemeral bool `yaml:"ephemeral"` - Metadata map[string]string `yaml:"metadata"` -} - -type shape struct { - ID string `yaml:"id"` - Source string `yaml:"source"` - // Args is the bd argv. Tokens may carry {file:NAME} (the side's copy of a - // corpus asset) and {rev:ID} (that side's current revision of ID). - Args []string `yaml:"args"` - Stdin string `yaml:"stdin"` - Setup [][]string `yaml:"setup"` - // Compare is how the served stdout is held to the embedded stdout: - // json (default), ids, count, text, or exit (exit code only). - Compare string `yaml:"compare"` - // Ordered keeps array order significant under json/ids. - Ordered bool `yaml:"ordered"` - // PrefixOf is the unlimited form of a limited listing (compare: ids). A - // limit cut through a tie set (same priority, created_at within one - // second) lands at a different row on two workspaces seeded seconds - // apart, so the limited rows are not compared across sides. Instead each - // side's limited rows must be, in order, a prefix of that SAME side's - // PrefixOf listing, both sides must return as many rows, and the two - // PrefixOf listings must hold the same ids. - PrefixOf []string `yaml:"prefix_of"` - // IgnoreFields are extra JSON keys dropped before comparing. - IgnoreFields []string `yaml:"ignore_fields"` - // ExpectExit is the exit code both sides must produce (default 0). An - // allowlisted shape must state it: the embedded run is held to it even - // though the served run is only held to the allowlisted refusal. - ExpectExit *int `yaml:"expect_exit"` - // ExpectStderr, when set, must match both sides' stderr (only the - // embedded side's on an allowlisted shape). - ExpectStderr string `yaml:"expect_stderr"` - // Verify are reads run on both sides after the shape and compared as json: - // how a write is checked for having done the same thing on both backends. - Verify [][]string `yaml:"verify"` - // Requests, when set, is exactly how many requests the served run of Args - // makes through the reference proxy (not its setup or verify reads). Each - // is a sequential round trip, so the count is the shape's latency floor - // over a gateway: a change that adds one fails here, and one that saves - // one updates the pin. - Requests *int `yaml:"requests"` - // Pending marks a known failure and names the slice that fixes it. - Pending *pending `yaml:"pending"` -} - -// wantExit is the exit code the embedded run must produce. -func (s shape) wantExit() int { - if s.ExpectExit == nil { - return 0 - } - return *s.ExpectExit -} - -type pending struct { - Slice string `yaml:"slice"` - Reason string `yaml:"reason"` -} - -// allowEntry is testdata/allowlist.yaml: a shape that is EXPECTED to be -// refused over http, permanently, for a stated reason. The served run must -// fail and match Refusal; if it ever succeeds the entry is stale and fails. -// The embedded run is still held to the shape's expect_exit and -// expect_stderr, and nothing else of the shape is checked. -type allowEntry struct { - ID string `yaml:"id"` - Reason string `yaml:"reason"` - Refusal string `yaml:"refusal"` -} - -// refusalMarkers are the texts a refusal of an operation over http carries: -// issueops.ErrUnsupported, encode.RefusedError, the client's capability -// preflight (wire.CapabilityError, which never reaches the proxy), the -// server's loopback-only unlimited-read 400, an unrouted 404, and a strict -// decoder's unknown member. -var refusalMarkers = regexp.MustCompile(`(?i)not supported by the|not expressible on the v0 wire|loopback-only|capability "[^"]+" not advertised|does not advertise capability|no such route on this server|unknown_parameter|unknown query parameter|json: unknown field`) - -// volatileKey names JSON members that legitimately differ between two -// workspaces fed the same commands: timestamps and revision tokens. -var volatileKey = regexp.MustCompile(`(^|_)at$|^revision$|^row_version$|^expected_revision$|^current_revision$`) - -// callerTimeKey names the timestamps a shape's own arguments set (bd create -// --defer, --due). They are compared as given, neither dropped as volatile -// nor scrubbed to , so a client that sends the wrong one fails; a shape -// must give them as absolute dates, since a relative one resolves against -// each side's own clock. -var callerTimeKey = map[string]bool{"defer_until": true, "due_at": true} - -var timestampText = regexp.MustCompile(`\d{4}-\d{2}-\d{2}[T ]\d{2}:\d{2}(:\d{2}(\.\d+)?)?(Z|[+-]\d{2}:?\d{2})?`) - -func loadCorpus(t *testing.T) (*corpusFile, map[string]allowEntry) { - t.Helper() - raw, err := os.ReadFile("testdata/corpus.yaml") - if err != nil { - t.Fatalf("read corpus: %v", err) - } - var c corpusFile - dec := yaml.NewDecoder(strings.NewReader(string(raw))) - dec.KnownFields(true) - if err := dec.Decode(&c); err != nil { - t.Fatalf("parse testdata/corpus.yaml: %v", err) - } - rawAllow, err := os.ReadFile("testdata/allowlist.yaml") - if err != nil { - t.Fatalf("read allowlist: %v", err) - } - var entries []allowEntry - dec = yaml.NewDecoder(strings.NewReader(string(rawAllow))) - dec.KnownFields(true) - if err := dec.Decode(&entries); err != nil { - t.Fatalf("parse testdata/allowlist.yaml: %v", err) - } - allow := map[string]allowEntry{} - for _, e := range entries { - if e.ID == "" || strings.TrimSpace(e.Reason) == "" || e.Refusal == "" { - t.Fatalf("allowlist entry %+v needs id, reason and refusal", e) - } - allow[e.ID] = e - } - return &c, allow -} - -// TestCorpusFileIsWellFormed runs without the embedded engine: it keeps the -// data honest (unique ids, a source and a slice for every pending mark, no -// allowlist entry for a shape that does not exist, no shape both pending and -// allowlisted, an allowlisted shape states its embedded exit code and -// carries no check that the allowlist would skip, every expect_stderr and -// refusal compiles, and the reference profile loads). -func TestCorpusFileIsWellFormed(t *testing.T) { - c, allow := loadCorpus(t) - seen := map[string]bool{} - for _, s := range c.Shapes { - if s.ID == "" || len(s.Args) == 0 { - t.Errorf("shape %+v needs an id and args", s) - continue - } - if seen[s.ID] { - t.Errorf("duplicate shape id %q", s.ID) - } - seen[s.ID] = true - if strings.TrimSpace(s.Source) == "" { - t.Errorf("shape %s names no source", s.ID) - } - if s.Pending != nil && (s.Pending.Slice == "" || strings.TrimSpace(s.Pending.Reason) == "") { - t.Errorf("shape %s is pending without a slice and a reason", s.ID) - } - if _, ok := allow[s.ID]; ok { - if s.Pending != nil { - t.Errorf("shape %s is both pending and allowlisted; pick one", s.ID) - } - if s.ExpectExit == nil { - t.Errorf("shape %s is allowlisted without expect_exit; state the exit code the embedded run must produce", s.ID) - } - if modeOf(s) != "exit" || len(s.Verify) > 0 { - t.Errorf("shape %s is allowlisted, so only its exit code and stderr are checked; give it compare: exit and no verify", s.ID) - } - if s.Requests != nil { - t.Errorf("shape %s is allowlisted, so its request count is not checked; drop requests", s.ID) - } - } - switch s.Compare { - case "", "json", "ids", "count", "text", "exit": - default: - t.Errorf("shape %s: unknown compare mode %q", s.ID, s.Compare) - } - if len(s.PrefixOf) > 0 && s.Compare != "ids" { - t.Errorf("shape %s: prefix_of needs compare: ids", s.ID) - } - if _, err := regexp.Compile(s.ExpectStderr); err != nil { - t.Errorf("shape %s: expect_stderr: %v", s.ID, err) - } - if s.Requests != nil && *s.Requests < 0 { - t.Errorf("shape %s: requests %d is negative", s.ID, *s.Requests) - } - } - for id, entry := range allow { - if !seen[id] { - t.Errorf("allowlist names %q, which is not a corpus shape", id) - } - if _, err := regexp.Compile(entry.Refusal); err != nil { - t.Errorf("allowlist entry %s: refusal: %v", id, err) - } - } - for _, a := range c.Files { - if (a.Content == "") == (a.Bulk == nil) { - t.Errorf("asset %s needs exactly one of content and bulk", a.Name) - } - } - loadReferenceProfile(t) -} - -// side is one workspace the corpus runs in. -type side struct { - name string - dir string - files string - env []string -} - -type outcome struct { - id, status, detail string - requests int64 -} - -// corpusParts is how many parts the shapes are dealt across. Each part is a -// top-level test of its own (TestServedCorpusPart1..N, servedCorpusParts) -// that starts its own server and seeds its own pair of workspaces, so the -// embedded lane runs each part in a Bazel shard of its own -// (served_pinned_shards.txt pins part k to shard k). The parts can be split -// because no shape depends on another's effects: every comparison is the -// served side against the embedded side after the same commands, and each -// part keeps corpus order, so its reads still run on the bare seed before -// any of its writes (see testdata/corpus.yaml's header). -// -// Four parts, from the embedded lane's measured part times (PR #7538): each -// part's server start and concurrent seed take ~62-76s and the 127 shapes -// ~242s together, so four parts ran 114-146s each and three ran 170-180s. -// Three would save one setup (~75 test-seconds a run), but the longest part -// is the embedded lane's served long pole, on the gate's critical path -// (forks' above all), so the ~34s it saves is worth more. -const corpusParts = 4 - -// servedCorpusParts are the parts' tests, one per part, in part order. -var servedCorpusParts = []func(*testing.T){ - TestServedCorpusPart1, TestServedCorpusPart2, TestServedCorpusPart3, TestServedCorpusPart4, -} - -func TestServedCorpusPart1(t *testing.T) { testServedCorpus(t, 1) } -func TestServedCorpusPart2(t *testing.T) { testServedCorpus(t, 2) } -func TestServedCorpusPart3(t *testing.T) { testServedCorpus(t, 3) } -func TestServedCorpusPart4(t *testing.T) { testServedCorpus(t, 4) } - -// corpusPartNameErr fails a part test whose name is not its part's: a -// TestServedCorpusPart3 that ran part 2 would run part 2's shapes twice and -// silently drop its own. A part outside 1..corpusParts (a stray -// TestServedCorpusPart5 left after the parts shrink) would run no shapes. -func corpusPartNameErr(name string, part int) error { - if part < 1 || part > corpusParts { - return fmt.Errorf("%s runs corpus part %d; there are parts 1..%d (corpusParts)", name, part, corpusParts) - } - if want := fmt.Sprintf("TestServedCorpusPart%d", part); name != want { - return fmt.Errorf("%s runs corpus part %d; only %s may (fix its testServedCorpus call)", name, part, want) - } - return nil -} - -// corpusPart returns the shapes part (1..corpusParts) runs. -func corpusPart(shapes []shape, part int) []shape { - return corpusPartOf(shapes, part, corpusParts) -} - -// corpusPartOf deals shapes round robin, in corpus order, across parts parts -// and returns part's (1-based) share: shapes part-1, part-1+parts, and so on. -// Round robin keeps each part a mix of the corpus's reads and writes in -// their original order. -func corpusPartOf(shapes []shape, part, parts int) []shape { - var out []shape - for i := part - 1; i < len(shapes); i += parts { - out = append(out, shapes[i]) - } - return out -} - -// testServedCorpus is the suite, for one part of the corpus. Each shape is a -// subtest; a pending shape is still RUN and then skipped with what it did, so -// a slice that fixes it sees the subtest fail with "now passes; remove the -// pending mark". BEADS_CORPUS_SKIP_PENDING=1 skips pending shapes without -// running them. BEADS_CORPUS_REPORT= writes a JSON summary of every -// part this process has run. -func testServedCorpus(t *testing.T, part int) { - // First, before the engine gate: a part test wired to the wrong part - // fails in every lane, not only where the corpus runs. - if err := corpusPartNameErr(t.Name(), part); err != nil { - t.Fatal(err) - } - skipUnlessEmbeddedDolt(t) - c, allow := loadCorpus(t) - shapes := corpusPart(c.Shapes, part) - profile := loadReferenceProfile(t) - bin := buildBD(t) - - common := []string{ - "BEADS_ACTOR=worker", - "CLAUDE_SESSION_ID=corpus-session-s23", - "BD_BACKUP_ENABLED=false", - "BD_EXPORT_AUTO=false", - } - - // The embedded side needs no server: init and seed it on a goroutine - // while this one starts the server and seeds the served side. Each side - // has its own directories, env and bd processes (prepareSide), and each - // still runs the seed in order, so the seeded data is what it was when - // the two sides were seeded command by command in turn. The deferred - // cancel and Wait run on every exit, a t.Fatal included, so the goroutine - // never outlives the test or its TempDirs. - setupStart := time.Now() - ctx, cancel := context.WithCancel(context.Background()) - var seeding sync.WaitGroup - defer seeding.Wait() - defer cancel() - local := &side{name: "embedded", dir: t.TempDir(), files: t.TempDir(), env: common} - var localErr error - seeding.Add(1) - go func() { - defer seeding.Done() - localErr = prepareSide(ctx, t, bin, c, local, - []string{"init", "-p", corpusPrefix, "--non-interactive", "--skip-hooks", "--skip-agents", "-q"}) - if localErr != nil { - cancel() - } - }() - - serverAddr := startCorpusServer(t) - proxy := newReferenceProxy(t, profile, serverAddr) - proxyAddr := startProxy(t, proxy) - served := &side{name: "served", dir: t.TempDir(), files: t.TempDir(), - env: append([]string{"BEADS_HTTP_TOKEN=" + proxyAddr + "=" + corpusToken}, common...)} - servedErr := prepareSide(ctx, t, bin, c, served, - []string{"connect", "http://" + proxyAddr, "--expect-project-id", corpusProjectID, "--json"}) - if servedErr != nil { - cancel() - } - seeding.Wait() - // The side that failed first is the one to report; the other may only - // have seen the cancel. - for _, err := range []error{servedErr, localErr} { - if err != nil && !errors.Is(err, context.Canceled) { - t.Fatal(err) - } - } - t.Logf("part %d setup (server start and both sides seeded concurrently): %s", part, time.Since(setupStart).Round(100*time.Millisecond)) - proxy.takeRefusals() - - skipPending := os.Getenv("BEADS_CORPUS_SKIP_PENDING") == "1" - var outcomes []outcome - for _, s := range shapes { - s := s - t.Run(s.ID, func(t *testing.T) { - o := outcome{id: s.ID} - defer func() { outcomes = append(outcomes, o) }() - if s.Pending != nil && skipPending { - o.status = "pending:" + s.Pending.Slice - t.Skipf("pending %s: %s", s.Pending.Slice, s.Pending.Reason) - } - before := proxy.requests.Load() - failures := runShape(t, bin, s, served, local, allow, proxy) - o.requests = proxy.requests.Load() - before - entry, allowed := allow[s.ID] - switch { - case hasCorpusBug(failures): - // The embedded side itself disagrees with the corpus: no pending - // mark or allowlist entry may hide that, or a pending shape that - // can never pass would sit in the corpus forever. - o.status = "FAIL(corpus)" - o.detail = failures[0] - for _, f := range failures { - t.Error(f) - } - case s.Pending != nil && len(failures) == 0: - o.status = "FAIL(pending-now-passes)" - t.Errorf("shape is marked pending %s but now passes on the reference profile; remove the pending mark from testdata/corpus.yaml", s.Pending.Slice) - case s.Pending != nil: - o.status = "pending:" + s.Pending.Slice - o.detail = failures[0] - t.Skipf("pending %s: %s\nobserved:\n%s", s.Pending.Slice, s.Pending.Reason, strings.Join(failures, "\n")) - case len(failures) > 0: - o.status = "FAIL" - o.detail = failures[0] - for _, f := range failures { - t.Error(f) - } - case allowed: - o.status = "allowed" - o.detail = entry.Reason - default: - o.status = "pass" - } - }) - } - - summarize(t, part, len(c.Shapes), outcomes) -} - -// runShape runs one shape on both sides and returns every way the served run -// fell short. It never calls t.Error itself, so the caller can turn a pending -// shape's failures into a skip. -func runShape(t *testing.T, bin string, s shape, served, local *side, allow map[string]allowEntry, proxy *referenceProxy) []string { - t.Helper() - var fails []string - failf := func(format string, a ...any) { fails = append(fails, fmt.Sprintf(format, a...)) } - wantStderr, err := regexp.Compile(s.ExpectStderr) - if err != nil { - failf("CORPUS BUG: expect_stderr %q: %v", s.ExpectStderr, err) - return fails - } - - for _, args := range s.Setup { - for _, sd := range []*side{served, local} { - if r := runBD(t, bin, sd.dir, sd.env, "", expand(context.Background(), t, bin, sd, args)...); r.code != 0 { - failf("setup %v failed on %s: %s", args, sd.name, r) - return fails - } - } - } - proxy.takeRefusals() - args := expand(context.Background(), t, bin, served, s.Args) - before := proxy.requests.Load() - sr := runBD(t, bin, served.dir, served.env, s.Stdin, args...) - servedRequests := proxy.requests.Load() - before - wireRefusals := proxy.takeRefusals() - lr := runBD(t, bin, local.dir, local.env, s.Stdin, expand(context.Background(), t, bin, local, s.Args)...) - t.Logf("bd %s\nserved: %s\nembedded: exit=%d", strings.Join(s.Args, " "), sr, lr.code) - if len(wireRefusals) > 0 { - t.Logf("reference-profile refusals on the wire: %v", wireRefusals) - } - - // The embedded run is the oracle for every shape, an allowlisted one - // included: when it no longer does what the shape says, the shape is - // wrong or bd changed, whatever the served run did. - if lr.code != s.wantExit() { - failf("CORPUS BUG: the embedded run exited %d, want %d; the shape itself is wrong or bd changed: %s", lr.code, s.wantExit(), lr) - return fails - } - if s.ExpectStderr != "" && !wantStderr.MatchString(lr.stderr) { - failf("CORPUS BUG: embedded stderr does not match %q: %s", s.ExpectStderr, lr.stderr) - return fails - } - - if entry, ok := allow[s.ID]; ok { - re, err := regexp.Compile(entry.Refusal) - if err != nil { - failf("allowlist refusal regexp for %s: %v", s.ID, err) - return fails - } - if sr.code == 0 { - failf("allowlisted (%s) but the served run SUCCEEDED; drop it from testdata/allowlist.yaml and give it a comparison", entry.Reason) - } else if !re.MatchString(sr.stderr + sr.stdout) { - failf("allowlisted refusal did not match %q (a different failure than the one allowed): %s", entry.Refusal, sr) - } - return fails - } - - if m := refusalMarkers.FindString(sr.stderr + sr.stdout); m != "" { - failf("served run carries a refusal (%q): %s", m, sr) - } else if len(wireRefusals) > 0 && sr.code != 0 { - failf("served run failed on a reference-profile refusal %v: %s", wireRefusals, sr) - } - if sr.code != lr.code { - failf("exit code: served %d, embedded %d: %s", sr.code, lr.code, sr) - } - if s.ExpectStderr != "" && !wantStderr.MatchString(sr.stderr) { - failf("served stderr does not match %q (embedded does): %s", s.ExpectStderr, sr) - } - if s.Requests != nil && servedRequests != int64(*s.Requests) { - failf("the served run made %d requests, want exactly %d (requests); a change that saves one updates the pin", servedRequests, *s.Requests) - } - if len(fails) > 0 { - return fails - } - if len(s.PrefixOf) > 0 { - vs := runBD(t, bin, served.dir, served.env, "", expand(context.Background(), t, bin, served, s.PrefixOf)...) - vl := runBD(t, bin, local.dir, local.env, "", expand(context.Background(), t, bin, local, s.PrefixOf)...) - if vs.code != 0 || vl.code != 0 { - failf("prefix_of %v: exit served %d, embedded %d: served %s", s.PrefixOf, vs.code, vl.code, vs) - } else if d := comparePrefix(sr.stdout, lr.stdout, vs.stdout, vl.stdout); d != "" { - failf("output parity (ids, prefix_of): %s", d) - } - } else if d := compareOutputs(s, sr.stdout, lr.stdout); d != "" { - failf("output parity (%s): %s", modeOf(s), d) - } - for _, args := range s.Verify { - vs := runBD(t, bin, served.dir, served.env, "", expand(context.Background(), t, bin, served, args)...) - vl := runBD(t, bin, local.dir, local.env, "", expand(context.Background(), t, bin, local, args)...) - if vs.code != vl.code { - failf("verify %v: exit served %d, embedded %d: served %s", args, vs.code, vl.code, vs) - continue - } - if vl.code != 0 { - continue // both refused the read (e.g. a deleted id): equal is the check - } - if d := compareJSON(vs.stdout, vl.stdout, s.IgnoreFields, false); d != "" { - failf("verify %v diverged after the write: %s", args, d) - } - } - return fails -} - -func hasCorpusBug(failures []string) bool { - for _, f := range failures { - if strings.HasPrefix(f, "CORPUS BUG") { - return true - } - } - return false -} - -func modeOf(s shape) string { - if s.Compare == "" { - return "json" - } - return s.Compare -} - -func compareOutputs(s shape, served, local string) string { - switch modeOf(s) { - case "exit": - return "" - case "text": - a, b := scrubText(served), scrubText(local) - if a != b { - return fmt.Sprintf("text differs\nserved:\n%s\nembedded:\n%s", clip(a, 3000), clip(b, 3000)) - } - return "" - case "count": - a, errA := topLevelLen(served) - b, errB := topLevelLen(local) - if errA != nil || errB != nil { - return fmt.Sprintf("count: served err=%v embedded err=%v\nserved:\n%s", errA, errB, clip(served, 2000)) - } - if a != b { - return fmt.Sprintf("row count: served %d, embedded %d", a, b) - } - return "" - case "ids": - a, errA := idList(served, s.Ordered) - b, errB := idList(local, s.Ordered) - if errA != nil || errB != nil { - return fmt.Sprintf("ids: served err=%v embedded err=%v\nserved:\n%s", errA, errB, clip(served, 2000)) - } - if !reflect.DeepEqual(a, b) { - return fmt.Sprintf("ids differ\nserved (%d): %v\nembedded (%d): %v", len(a), a, len(b), b) - } - return "" - default: - return compareJSON(served, local, s.IgnoreFields, s.Ordered) - } -} - -// comparePrefix holds a limited listing to its unlimited form on each side: -// see shape.PrefixOf. -func comparePrefix(served, local, servedAll, localAll string) string { - lists := make([][]string, 4) - for i, out := range []string{served, local, servedAll, localAll} { - ids, err := idList(out, true) - if err != nil { - return fmt.Sprintf("listing %d: %v\n%s", i, err, clip(out, 2000)) - } - lists[i] = ids - } - s, l, sAll, lAll := lists[0], lists[1], lists[2], lists[3] - if len(s) != len(l) { - return fmt.Sprintf("row count: served %d, embedded %d", len(s), len(l)) - } - for _, c := range []struct { - name string - rows, all []string - }{{"served", s, sAll}, {"embedded", l, lAll}} { - if len(c.rows) > len(c.all) || !reflect.DeepEqual(c.rows, c.all[:len(c.rows)]) { - return fmt.Sprintf("%s limited rows are not a prefix of its own unlimited listing\nlimited: %v\nunlimited: %v", c.name, c.rows, c.all) - } - } - sSet := append([]string(nil), sAll...) - lSet := append([]string(nil), lAll...) - sort.Strings(sSet) - sort.Strings(lSet) - if !reflect.DeepEqual(sSet, lSet) { - return fmt.Sprintf("unlimited ids differ\nserved (%d): %v\nembedded (%d): %v", len(sSet), sSet, len(lSet), lSet) - } - return "" -} - -func scrubText(s string) string { - return strings.TrimSpace(timestampText.ReplaceAllString(s, "")) -} - -func decode(s string) (any, error) { - var v any - if err := json.Unmarshal([]byte(strings.TrimSpace(s)), &v); err != nil { - return nil, fmt.Errorf("not JSON: %w", err) - } - return v, nil -} - -// rows finds the row array in a bd --json payload: the payload itself, or the -// first array member of an envelope object. -func rows(v any) ([]any, bool) { - switch x := v.(type) { - case []any: - return x, true - case map[string]any: - for _, k := range []string{"issues", "data", "items", "results"} { - if arr, ok := x[k].([]any); ok { - return arr, true - } - } - } - return nil, false -} - -func topLevelLen(s string) (int, error) { - v, err := decode(s) - if err != nil { - return 0, err - } - arr, ok := rows(v) - if !ok { - return 0, fmt.Errorf("no row array in output") - } - return len(arr), nil -} - -func idList(s string, ordered bool) ([]string, error) { - v, err := decode(s) - if err != nil { - return nil, err - } - arr, ok := rows(v) - if !ok { - return nil, fmt.Errorf("no row array in output") - } - out := []string{} - for _, r := range arr { - if m, ok := r.(map[string]any); ok { - if id, ok := m["id"].(string); ok { - out = append(out, id) - } - } - } - if !ordered { - sort.Strings(out) - } - return out, nil -} - -func compareJSON(served, local string, ignore []string, ordered bool) string { - a, errA := decode(served) - b, errB := decode(local) - if errA != nil || errB != nil { - return fmt.Sprintf("served err=%v embedded err=%v\nserved:\n%s\nembedded:\n%s", errA, errB, clip(served, 2000), clip(local, 2000)) - } - drop := map[string]bool{} - for _, k := range ignore { - drop[k] = true - } - a, b = normalize(a, drop, ordered), normalize(b, drop, ordered) - if path, x, y, same := firstDiff("$", a, b); !same { - return fmt.Sprintf("first difference at %s: served=%s embedded=%s", path, brief(x), brief(y)) - } - return "" -} - -func normalize(v any, drop map[string]bool, ordered bool) any { - switch x := v.(type) { - case map[string]any: - out := make(map[string]any, len(x)) - for k, val := range x { - if drop[k] { - continue - } - if callerTimeKey[k] { - out[k] = val - continue - } - if volatileKey.MatchString(k) { - continue - } - out[k] = normalize(val, drop, ordered) - } - return out - case []any: - out := make([]any, len(x)) - for i, e := range x { - out[i] = normalize(e, drop, ordered) - } - if !ordered { - sort.SliceStable(out, func(i, j int) bool { return sortKey(out[i]) < sortKey(out[j]) }) - } - return out - case string: - return timestampText.ReplaceAllString(x, "") - default: - return v - } -} - -func sortKey(v any) string { - if m, ok := v.(map[string]any); ok { - for _, k := range []string{"id", "issue_id", "key"} { - if s, ok := m[k].(string); ok { - return k + "=" + s - } - } - if inner, ok := m["issue"].(map[string]any); ok { - if s, ok := inner["id"].(string); ok { - return "issue.id=" + s - } - } - } - raw, _ := json.Marshal(v) - return string(raw) -} - -func firstDiff(path string, a, b any) (string, any, any, bool) { - switch x := a.(type) { - case map[string]any: - y, ok := b.(map[string]any) - if !ok { - return path, a, b, false - } - keys := map[string]bool{} - for k := range x { - keys[k] = true - } - for k := range y { - keys[k] = true - } - sorted := make([]string, 0, len(keys)) - for k := range keys { - sorted = append(sorted, k) - } - sort.Strings(sorted) - for _, k := range sorted { - xv, xok := x[k] - yv, yok := y[k] - if !xok || !yok { - return path + "." + k, xv, yv, false - } - if p, l, r, same := firstDiff(path+"."+k, xv, yv); !same { - return p, l, r, false - } - } - return "", nil, nil, true - case []any: - y, ok := b.([]any) - if !ok { - return path, a, b, false - } - if len(x) != len(y) { - return path + ".length", len(x), len(y), false - } - for i := range x { - if p, l, r, same := firstDiff(path+"["+strconv.Itoa(i)+"]", x[i], y[i]); !same { - return p, l, r, false - } - } - return "", nil, nil, true - default: - if reflect.DeepEqual(a, b) { - return "", nil, nil, true - } - return path, a, b, false - } -} - -func brief(v any) string { - if v == nil { - return "" - } - raw, _ := json.Marshal(v) - return clip(string(raw), 400) -} - -var placeholder = regexp.MustCompile(`\{(file|rev):([^}]+)\}`) - -// expand substitutes {file:NAME} and {rev:ID} in args for side sd. It never -// fails t (a {rev:} it cannot read becomes "0"), so prepareSide may call it -// off the test's goroutine; its {rev:} lookup runs under ctx, so a cancelled -// prepareSide starts no further bd there either. -func expand(ctx context.Context, t *testing.T, bin string, sd *side, args []string) []string { - t.Helper() - out := make([]string, len(args)) - for i, a := range args { - out[i] = placeholder.ReplaceAllStringFunc(a, func(m string) string { - parts := placeholder.FindStringSubmatch(m) - switch parts[1] { - case "file": - return filepath.Join(sd.files, parts[2]) - default: - r, err := execBD(ctx, bin, sd.dir, sd.env, "", "show", parts[2], "--json") - if err != nil { - t.Logf("{rev:%s} on %s: %v", parts[2], sd.name, err) - return "0" - } - if r.code != 0 { - t.Logf("{rev:%s} on %s: show failed: %s", parts[2], sd.name, r) - return "0" - } - v, err := decode(r.stdout) - if err != nil { - return "0" - } - if arr, ok := rows(v); ok && len(arr) > 0 { - v = arr[0] - } - if m, ok := v.(map[string]any); ok { - if rev, ok := m["revision"].(string); ok { - return rev - } - } - return "0" - } - }) - } - return out -} - -// prepareSide attaches sd to its store with bd attach (connect or init), -// writes the corpus's asset files into sd.files and runs the seed on sd, in -// order. It touches only sd's own directories and bd processes and returns a -// failure as an error instead of failing t, so testServedCorpus runs it for -// the two sides at once; it stops before its next bd once ctx is done. -func prepareSide(ctx context.Context, t *testing.T, bin string, c *corpusFile, sd *side, attach []string) error { - t.Helper() - r, err := execBD(ctx, bin, sd.dir, sd.env, "", attach...) - if err != nil { - return err - } - if r.code != 0 { - return fmt.Errorf("bd %s on %s: %s", strings.Join(attach, " "), sd.name, r) - } - if err := writeAssets(c.Files, sd.files); err != nil { - return fmt.Errorf("%s: %w", sd.name, err) - } - for _, args := range c.Seed { - if err := ctx.Err(); err != nil { - return err - } - r, err := execBD(ctx, bin, sd.dir, sd.env, "", expand(ctx, t, bin, sd, args)...) - if err != nil { - return err - } - if r.code != 0 { - return fmt.Errorf("seed %v on %s failed; the seed must use shapes that work on the reference profile: %s", args, sd.name, r) - } - } - return nil -} - -func writeAssets(assets []corpusAsset, dir string) error { - for _, a := range assets { - content := a.Content - if a.Bulk != nil { - content = bulkPlan(*a.Bulk) - } - if err := os.WriteFile(filepath.Join(dir, a.Name), []byte(content), 0o600); err != nil { - return fmt.Errorf("write asset %s: %w", a.Name, err) - } - } - return nil -} - -func bulkPlan(b bulkGraph) string { - type node struct { - Key string `json:"key"` - ID string `json:"id"` - Title string `json:"title"` - Type string `json:"type"` - Priority int `json:"priority"` - Ephemeral bool `json:"ephemeral,omitempty"` - Metadata map[string]string `json:"metadata,omitempty"` - } - plan := struct { - CommitMessage string `json:"commit_message"` - Nodes []node `json:"nodes"` - }{CommitMessage: "corpus bulk " + b.IDPrefix} - for i := 1; i <= b.Count; i++ { - id := fmt.Sprintf("%s-%03d", b.IDPrefix, i) - plan.Nodes = append(plan.Nodes, node{Key: id, ID: id, Title: "bulk " + id, Type: "task", - Priority: b.Priority, Ephemeral: b.Ephemeral, Metadata: b.Metadata}) - } - raw, _ := json.MarshalIndent(plan, "", " ") - return string(raw) -} - -// reported is every outcome of every part this process has run, in the order -// the parts ran, for BEADS_CORPUS_REPORT. -var reported []outcome - -func summarize(t *testing.T, part, total int, outcomes []outcome) { - t.Helper() - counts := map[string]int{} - var lines []string - for _, o := range outcomes { - key := o.status - if strings.HasPrefix(key, "pending:") { - key = "pending" - } - counts[key]++ - lines = append(lines, fmt.Sprintf("%-26s %-48s req=%-3d %s", o.status, o.id, o.requests, firstLine(o.detail))) - } - t.Logf("corpus summary, part %d of %d (%d of %d shapes): %v\n%s", part, corpusParts, len(outcomes), total, counts, strings.Join(lines, "\n")) - reported = append(reported, outcomes...) - if path := os.Getenv("BEADS_CORPUS_REPORT"); path != "" { - type row struct { - ID string `json:"id"` - Status string `json:"status"` - Requests int64 `json:"requests"` - Detail string `json:"detail,omitempty"` - } - var out []row - for _, o := range reported { - out = append(out, row{o.id, o.status, o.requests, o.detail}) - } - raw, _ := json.MarshalIndent(out, "", " ") - if err := os.WriteFile(path, raw, 0o600); err != nil { - t.Errorf("write BEADS_CORPUS_REPORT: %v", err) - } - } -} - -func firstLine(s string) string { - s = strings.TrimSpace(s) - if i := strings.IndexByte(s, '\n'); i >= 0 { - s = s[:i] - } - return clip(s, 160) -} diff --git a/backend/http/corpus/gate_check_served_test.go b/backend/http/corpus/gate_check_served_test.go deleted file mode 100644 index 60d9f82c0..000000000 --- a/backend/http/corpus/gate_check_served_test.go +++ /dev/null @@ -1,355 +0,0 @@ -//go:build cgo - -package corpus_test - -import ( - "context" - "encoding/json" - "net/http" - "os" - "path/filepath" - "slices" - "strings" - "sync" - "testing" - "time" - - "github.com/steveyegge/beads/internal/httpapi" - "github.com/steveyegge/beads/internal/storage/embeddeddolt" - "github.com/steveyegge/beads/internal/types" -) - -// gateCheckRequestLog records every request the reference proxy forwards. -type gateCheckRequestLog struct { - mu sync.Mutex - seen []string - next http.Handler -} - -func (l *gateCheckRequestLog) ServeHTTP(w http.ResponseWriter, r *http.Request) { - l.mu.Lock() - l.seen = append(l.seen, r.Method+" "+r.URL.Path) - l.mu.Unlock() - l.next.ServeHTTP(w, r) -} - -func (l *gateCheckRequestLog) take() []string { - l.mu.Lock() - defer l.mu.Unlock() - out := l.seen - l.seen = nil - return out -} - -func gateCheckWrites(reqs []string) []string { - var writes []string - for _, r := range reqs { - if !strings.HasPrefix(r, http.MethodGet+" ") { - writes = append(writes, r) - } - } - return writes -} - -// TestServedGateCheckOverAwaitTypedGates runs `bd gate check` over HTTP, through -// the reference proxy, against REAL await-typed gates, on a server that -// advertises neither gate token (issues.create.gates, issues.update.gates; the -// profile is masked here, so this holds whatever reference_server.json -// advertises). No create through it carries await_*: the gates are seeded -// straight into the served store before it listens, and every outcome is read -// back from that store: -// -// - --dry-run writes nothing; -// - an expired timer closes, a pending one stays open; -// - a bead gate whose target is open records its sighting (issues.update), -// and one whose target is closed closes; -// - a merged gh:pr gate closes, a closed one escalates and stays open; -// - a gh:run gate with a workflow-name hint discovers its run but cannot -// persist await_id through a server without issues.update.gates: the -// client refuses the patch before the dial, that gate's error names the -// missing token and the server, not a Go method, and await_id is -// untouched; -// - a persisted proto's gates are templates: listed, but no sweep evaluates -// or writes them, and they take no row of --limit (they sort first here). -// -// The corpus.yaml shapes prove the listing only (their gates have no -// await_type); this is the test that proves what the sweep does to the gates. -func TestServedGateCheckOverAwaitTypedGates(t *testing.T) { - skipUnlessEmbeddedDolt(t) - bin := buildBD(t) - ctx := context.Background() - - store, err := embeddeddolt.Open(ctx, t.TempDir(), corpusDatabase, "main") - if err != nil { - t.Fatalf("open the served store: %v", err) - } - t.Cleanup(func() { _ = store.Close() }) - if err := store.SetConfig(ctx, "issue_prefix", corpusPrefix); err != nil { - t.Fatalf("set the issue prefix: %v", err) - } - // The templates keep P0 and every other row gets P2, so the store's default - // order puts the template gates ahead of every live one. - seed := func(i *types.Issue) { - t.Helper() - i.Status = types.StatusOpen - i.Title = i.ID - if !i.IsTemplate { - i.Priority = 2 - } - if err := store.CreateIssue(ctx, i, "seed"); err != nil { - t.Fatalf("seed %s: %v", i.ID, err) - } - } - seed(&types.Issue{ID: "e2e-tgt-open", IssueType: "task"}) - seed(&types.Issue{ID: "e2e-tgt-closed", IssueType: "task"}) - if err := store.CloseIssue(ctx, "e2e-tgt-closed", "done", "seed", ""); err != nil { - t.Fatalf("close e2e-tgt-closed: %v", err) - } - seed(&types.Issue{ID: "e2e-g-timer", IssueType: "gate", AwaitType: "timer", Timeout: time.Millisecond}) - seed(&types.Issue{ID: "e2e-g-timer-pending", IssueType: "gate", AwaitType: "timer", Timeout: 100 * time.Hour}) - seed(&types.Issue{ID: "e2e-g-bead-open", IssueType: "gate", AwaitType: "bead", AwaitID: "e2e-tgt-open"}) - seed(&types.Issue{ID: "e2e-g-bead-closed", IssueType: "gate", AwaitType: "bead", AwaitID: "e2e-tgt-closed"}) - seed(&types.Issue{ID: "e2e-g-pr-merged", IssueType: "gate", AwaitType: "gh:pr", AwaitID: "11"}) - seed(&types.Issue{ID: "e2e-g-pr-closed", IssueType: "gate", AwaitType: "gh:pr", AwaitID: "12"}) - seed(&types.Issue{ID: "e2e-g-run-hint", IssueType: "gate", AwaitType: "gh:run", AwaitID: "ci-wf"}) - seed(&types.Issue{ID: "e2e-tpl-timer", IssueType: "gate", AwaitType: "timer", Timeout: time.Millisecond, IsTemplate: true}) - seed(&types.Issue{ID: "e2e-tpl-bead", IssueType: "gate", AwaitType: "bead", AwaitID: "e2e-tgt-open", IsTemplate: true}) - seed(&types.Issue{ID: "e2e-tpl-run", IssueType: "gate", AwaitType: "gh:run", AwaitID: "ci-wf", IsTemplate: true}) - - tokenFile := filepath.Join(t.TempDir(), "tokens") - if err := os.WriteFile(tokenFile, []byte(corpusToken+"\n"), 0o600); err != nil { - t.Fatalf("write the token file: %v", err) - } - auth, err := httpapi.NewTokenFileAuth(tokenFile) - if err != nil { - t.Fatalf("load the token file: %v", err) - } - cfg := corpusServeConfig(t, store) - cfg.AllowNonLoopback = true - cfg.Auth = auth - srv, err := httpapi.Listen(cfg) - if err != nil { - t.Fatalf("bind the in-process server: %v", err) - } - serveCtx, cancel := context.WithCancel(context.Background()) - done := make(chan struct{}) - go func() { defer close(done); _ = srv.Serve(serveCtx) }() - t.Cleanup(func() { - cancel() - select { - case <-done: - case <-time.After(30 * time.Second): - t.Error("the in-process server did not shut down") - } - }) - - // The gh:run case pins a server that cannot take an await_id patch, so the - // profile is held without issues.update.gates whatever - // reference_server.json advertises: upgrading the reference profile must - // not turn this case into a different test. - profile := loadReferenceProfile(t) - profile.Capabilities = slices.DeleteFunc(profile.Capabilities, func(c string) bool { return c == "issues.update.gates" }) - proxy := newReferenceProxy(t, profile, srv.Addr()) - reqs := &gateCheckRequestLog{next: proxy} - proxyAddr := startProxy(t, reqs) - - // gh and gt are fakes on PATH: gh answers the run discovery and the two PR - // states, gt records each escalation. - binDir := t.TempDir() - gh := `#!/bin/sh -case "$1 $2" in -"run list") echo '[{"databaseId":4242,"name":"ci-wf","status":"completed","conclusion":"success","createdAt":"2026-10-15T00:00:00Z","workflowName":"ci-wf"}]' ;; -"run view") echo '{"status":"completed","conclusion":"success","name":"ci-wf"}' ;; -"pr view") if [ "$3" = "11" ]; then echo '{"state":"MERGED","title":"m"}'; else echo '{"state":"CLOSED","title":"c"}'; fi ;; -*) echo "unexpected gh $*" >&2; exit 3 ;; -esac -` - if err := os.WriteFile(filepath.Join(binDir, "gh"), []byte(gh), 0o755); err != nil { - t.Fatalf("write fake gh: %v", err) - } - if err := os.WriteFile(filepath.Join(binDir, "gt"), []byte("#!/bin/sh\nexit 0\n"), 0o755); err != nil { - t.Fatalf("write fake gt: %v", err) - } - - dir := t.TempDir() - env := []string{ - "BEADS_HTTP_TOKEN=" + proxyAddr + "=" + corpusToken, - "BEADS_ACTOR=worker", - "BD_BACKUP_ENABLED=false", - "BD_EXPORT_AUTO=false", - "PATH=" + binDir + string(os.PathListSeparator) + os.Getenv("PATH"), - } - if r := runBD(t, bin, dir, env, "", "connect", "http://"+proxyAddr, "--expect-project-id", corpusProjectID, "--json"); r.code != 0 { - t.Fatalf("bd connect: %s", r) - } - reqs.take() - time.Sleep(50 * time.Millisecond) // the 1ms timer is expired by now - - get := func(id string) *types.Issue { - t.Helper() - issue, err := store.GetIssue(ctx, id) - if err != nil { - t.Fatalf("read %s back: %v", id, err) - } - return issue - } - wantStatus := func(id string, want types.Status) { - t.Helper() - if got := get(id).Status; got != want { - t.Errorf("%s status = %s, want %s", id, got, want) - } - } - seenOf := func(id string) string { - t.Helper() - var meta map[string]json.RawMessage - raw := get(id).Metadata - if len(raw) == 0 { - return "" - } - if err := json.Unmarshal(raw, &meta); err != nil { - t.Fatalf("%s metadata %s: %v", id, raw, err) - } - var seen string - _ = json.Unmarshal(meta["await_seen"], &seen) - return seen - } - allGates := []string{"e2e-g-timer", "e2e-g-timer-pending", "e2e-g-bead-open", "e2e-g-bead-closed", "e2e-g-pr-merged", "e2e-g-pr-closed", "e2e-g-run-hint"} - - t.Run("dry-run writes nothing", func(t *testing.T) { - r := runBD(t, bin, dir, env, "", "gate", "check", "--dry-run") - if r.code != 0 { - t.Fatalf("bd gate check --dry-run: %s", r) - } - if w := gateCheckWrites(reqs.take()); len(w) != 0 { - t.Fatalf("--dry-run sent writes %v\n%s", w, r) - } - for _, id := range allGates { - wantStatus(id, types.StatusOpen) - } - if seen := seenOf("e2e-g-bead-open"); seen != "" { - t.Errorf("--dry-run recorded a sighting %q", seen) - } - if got := get("e2e-g-run-hint").AwaitID; got != "ci-wf" { - t.Errorf("--dry-run persisted await_id %q", got) - } - }) - - t.Run("limit counts only live gates", func(t *testing.T) { - // bd gate list still lists templates, so its first three rows show the - // order the sweep's own listing would be cut from. - r := runBD(t, bin, dir, env, "", "gate", "list", "--limit", "3", "--json") - var listed []types.Issue - if start := strings.Index(r.stdout, "["); r.code != 0 || start < 0 || json.Unmarshal([]byte(r.stdout[start:]), &listed) != nil { - t.Fatalf("bd gate list --limit 3 --json: %s", r) - } - var ids []string - for _, g := range listed { - if strings.HasPrefix(g.ID, "e2e-tpl-") { - ids = append(ids, g.ID) - } - } - if len(listed) != 3 || len(ids) != 3 { - t.Fatalf("bd gate list --limit 3 listed %d gates, %d of them templates (%v), want the three template gates first", len(listed), len(ids), ids) - } - reqs.take() - - r = runBD(t, bin, dir, env, "", "gate", "check", "--dry-run", "--limit", "3") - if r.code != 0 { - t.Fatalf("bd gate check --dry-run --limit 3: %s", r) - } - if w := gateCheckWrites(reqs.take()); len(w) != 0 { - t.Fatalf("--dry-run sent writes %v\n%s", w, r) - } - if !strings.Contains(r.stdout, "Checked 3 gates") { - t.Errorf("--limit 3 behind three template gates must still check three live gates:\n%s", r) - } - if strings.Contains(r.stdout+r.stderr, "e2e-tpl-") { - t.Errorf("a template gate was checked:\n%s", r) - } - }) - - t.Run("timer sweep closes the expired timer", func(t *testing.T) { - r := runBD(t, bin, dir, env, "", "gate", "check", "--type=timer", "--escalate") - if r.code != 0 { - t.Fatalf("bd gate check --type=timer --escalate: %s", r) - } - reqs.take() - wantStatus("e2e-g-timer", types.StatusClosed) - wantStatus("e2e-g-timer-pending", types.StatusOpen) - wantStatus("e2e-tpl-timer", types.StatusOpen) - }) - - t.Run("bead sweep records the sighting and closes the done gate", func(t *testing.T) { - r := runBD(t, bin, dir, env, "", "gate", "check", "--type=bead") - if r.code != 0 { - t.Fatalf("bd gate check --type=bead: %s", r) - } - writes := gateCheckWrites(reqs.take()) - sighted := false - for _, w := range writes { - if strings.Contains(w, "e2e-g-bead-open") { - sighted = true - } - if strings.Contains(w, "e2e-tpl-bead") { - t.Errorf("the sweep wrote to the template gate: %s", w) - } - } - if !sighted { - t.Errorf("no write to e2e-g-bead-open reached the server; writes: %v\n%s", writes, r) - } - if seen := seenOf("e2e-g-bead-open"); seen != "e2e-tgt-open" { - t.Errorf("e2e-g-bead-open await_seen = %q, want e2e-tgt-open\n%s", seen, r) - } - if seen := seenOf("e2e-tpl-bead"); seen != "" { - t.Errorf("e2e-tpl-bead recorded a sighting %q; a template gate is never checked", seen) - } - wantStatus("e2e-g-bead-open", types.StatusOpen) - wantStatus("e2e-g-bead-closed", types.StatusClosed) - }) - - t.Run("gh:pr sweep closes the merged PR gate", func(t *testing.T) { - r := runBD(t, bin, dir, env, "", "gate", "check", "--type=gh:pr", "--escalate") - if r.code != 0 { - t.Fatalf("bd gate check --type=gh:pr --escalate: %s", r) - } - reqs.take() - wantStatus("e2e-g-pr-merged", types.StatusClosed) - wantStatus("e2e-g-pr-closed", types.StatusOpen) - if !strings.Contains(r.stdout, "e2e-g-pr-closed: ESCALATE") { - t.Errorf("closed PR gate was not escalated:\n%s", r) - } - }) - - t.Run("gh:run await_id persist refuses by the missing token", func(t *testing.T) { - r := runBD(t, bin, dir, env, "", "gate", "check", "--type=gh:run") - if r.code == 0 { - t.Fatalf("bd gate check --type=gh:run succeeded; the reference server does not advertise issues.update.gates: %s", r) - } - if w := gateCheckWrites(reqs.take()); len(w) != 0 { - t.Errorf("the refused await_id persist sent writes %v", w) - } - if !strings.Contains(r.stderr, "e2e-g-run-hint") || !strings.Contains(r.stderr, `does not advertise capability "issues.update.gates"`) { - t.Errorf("gh:run error does not name the gate and the missing issues.update.gates token:\n%s", r) - } - if !strings.Contains(r.stderr, "bd serve at http://"+proxyAddr) { - t.Errorf("gh:run error does not name the server it reached:\n%s", r) - } - if strings.Contains(r.stderr, "e2e-tpl-run") { - t.Errorf("the template gh:run gate was checked:\n%s", r) - } - // The capability refusal names the wire operation (updateIssue) that - // needs the token; the role and store methods stay out of it. - for _, leak := range []string{"UpdateIssue", "Lifecycle.Update"} { - if strings.Contains(r.stderr, leak) { - t.Errorf("gh:run error leaks %q:\n%s", leak, r) - } - } - if got := get("e2e-g-run-hint").AwaitID; got != "ci-wf" { - t.Errorf("e2e-g-run-hint await_id = %q, want ci-wf untouched", got) - } - wantStatus("e2e-g-run-hint", types.StatusOpen) - if got := get("e2e-tpl-run").AwaitID; got != "ci-wf" { - t.Errorf("e2e-tpl-run await_id = %q, want ci-wf untouched", got) - } - }) -} diff --git a/backend/http/corpus/harness_test.go b/backend/http/corpus/harness_test.go deleted file mode 100644 index d2a2c4d02..000000000 --- a/backend/http/corpus/harness_test.go +++ /dev/null @@ -1,333 +0,0 @@ -//go:build cgo - -package corpus_test - -import ( - "context" - "errors" - "fmt" - "io" - "net" - "net/http" - "os" - "os/exec" - "path/filepath" - "runtime" - "strings" - "sync" - "testing" - "time" - - "github.com/steveyegge/beads/internal/httpapi" - "github.com/steveyegge/beads/internal/storage" - "github.com/steveyegge/beads/internal/storage/domain" - "github.com/steveyegge/beads/internal/storage/embeddeddolt" - "github.com/steveyegge/beads/internal/testutil/bazeltest" -) - -const ( - corpusProjectID = "proj-corpus-s23" - corpusDatabase = "corpus" - corpusPrefix = "e2e" - // corpusToken is the bearer the in-process server accepts. The reference - // deployment authenticates every request, so the suite does too. - // #nosec G101 -- a throwaway test credential for an httptest-scoped server. - corpusToken = "corpus-s23-test-token" - // bdTimeout bounds one bd subprocess so a hung shape fails rather than - // stalling the whole suite. - bdTimeout = 90 * time.Second -) - -// startCorpusServer binds the in-process server the way the reference -// deployment is bound: AllowNonLoopback with a bearer token file. The socket is -// still 127.0.0.1 (this suite never leaves the host), but every behavior the -// server keys off its bind mode — the loopback-only refusal of an unlimited -// read above all — is the non-loopback one, because that is decided by -// Config.AllowNonLoopback and not by the address. -func startCorpusServer(t *testing.T) string { - t.Helper() - beadsDir := t.TempDir() - ctx := context.Background() - store, err := embeddeddolt.Open(ctx, beadsDir, corpusDatabase, "main") - if err != nil { - t.Fatalf("open the served store: %v", err) - } - t.Cleanup(func() { _ = store.Close() }) - if err := store.SetConfig(ctx, "issue_prefix", corpusPrefix); err != nil { - t.Fatalf("set the issue prefix: %v", err) - } - - tokenFile := filepath.Join(t.TempDir(), "tokens") - if err := os.WriteFile(tokenFile, []byte(corpusToken+"\n"), 0o600); err != nil { - t.Fatalf("write the token file: %v", err) - } - auth, err := httpapi.NewTokenFileAuth(tokenFile) - if err != nil { - t.Fatalf("load the token file: %v", err) - } - - cfg := corpusServeConfig(t, store) - cfg.AllowNonLoopback = true - cfg.Auth = auth - srv, err := httpapi.Listen(cfg) - if err != nil { - t.Fatalf("bind the in-process server: %v", err) - } - serveCtx, cancel := context.WithCancel(context.Background()) - done := make(chan struct{}) - go func() { defer close(done); _ = srv.Serve(serveCtx) }() - t.Cleanup(func() { - cancel() - select { - case <-done: - case <-time.After(30 * time.Second): - t.Error("the in-process server did not shut down") - } - }) - return srv.Addr() -} - -// startProxy serves h on an ephemeral loopback port. -func startProxy(t *testing.T, h http.Handler) string { - t.Helper() - srv := &http.Server{Handler: h, ReadHeaderTimeout: 10 * time.Second} - ln, err := net.Listen("tcp", "127.0.0.1:0") - if err != nil { - t.Fatalf("listen for the reference proxy: %v", err) - } - go func() { _ = srv.Serve(ln) }() - t.Cleanup(func() { _ = srv.Close() }) - return ln.Addr().String() -} - -// corpusServeConfig takes every role off the store, as bd serve does. It -// mirrors backend/http's e2eServeConfig (a sibling test package, so it cannot -// be imported). -func corpusServeConfig(t *testing.T, s storage.DoltStorage) httpapi.Config { - t.Helper() - cfg := httpapi.Config{ - Addr: "127.0.0.1:0", - Stdout: io.Discard, - Stderr: io.Discard, - Workspace: domain.ContextInfo{ProjectID: corpusProjectID, Database: corpusDatabase}, - } - var err error - must := func(name string) { - t.Helper() - if err != nil { - t.Fatalf("%s(): %v", name, err) - } - } - cfg.Reader, err = s.IssueReader() - must("IssueReader") - cfg.Claimer, err = s.IssueClaimer() - must("IssueClaimer") - cfg.ReadyClaimer, err = s.ReadyClaimer() - must("ReadyClaimer") - cfg.Releaser, err = s.Releaser() - must("Releaser") - cfg.Lifecycle, err = s.IssueLifecycle() - must("IssueLifecycle") - cfg.Settings, err = s.WorkspaceConfig() - must("WorkspaceConfig") - cfg.Stats, err = s.StatsReporter() - must("StatsReporter") - cfg.CycleDetector, err = s.CycleDetector() - must("CycleDetector") - cfg.EdgeReader, err = s.EdgeReader() - must("EdgeReader") - cfg.GraphCounter, err = s.GraphCounter() - must("GraphCounter") - cfg.BatchGetter, err = s.BatchGetter() - must("BatchGetter") - cfg.LeaseReclaimer, err = s.LeaseReclaimer() - must("LeaseReclaimer") - cfg.Heartbeater, err = s.Heartbeater() - must("Heartbeater") - cfg.BlockedLister, err = s.BlockedLister() - must("BlockedLister") - cfg.Relations, err = s.IssueRelations() - must("IssueRelations") - cfg.Commenter, err = s.Commenter() - must("Commenter") - cfg.BlockingAnnotator, err = s.BlockingAnnotator() - must("BlockingAnnotator") - cfg.TreeWalker, err = s.TreeWalker() - must("TreeWalker") - cfg.ReadyCounter, err = s.ReadyCounter() - must("ReadyCounter") - cfg.Counter, err = s.Counter() - must("Counter") - cfg.Querier, err = s.Querier() - must("Querier") - cfg.Sweeper, err = s.Sweeper() - must("Sweeper") - cfg.Deleter, err = s.Deleter() - must("Deleter") - cfg.BatchCreator, err = s.BatchCreator() - must("BatchCreator") - cfg.BatchCloser, err = s.BatchCloser() - must("BatchCloser") - cfg.DependencyEditor, err = s.DependencyEditor() - must("DependencyEditor") - cfg.MetadataCAS, err = s.MetadataCAS() - must("MetadataCAS") - cfg.BatchApplier, err = s.BatchApplier() - must("BatchApplier") - cfg.Memories, err = s.Memories() - must("Memories") - return cfg -} - -// This file is the corpus tier's cgo half; see TestCorpusTierIsLinkedWhenRequired. -func init() { corpusTierLinked = true } - -// skipUnlessEmbeddedDolt is the served tier's gate, as in backend/http. -func skipUnlessEmbeddedDolt(t *testing.T) { - t.Helper() - if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") == "1" { - return - } - if os.Getenv("BEADS_HTTP_TEST_REQUIRED") == "1" { - t.Fatal("BEADS_HTTP_TEST_REQUIRED=1 but BEADS_TEST_EMBEDDED_DOLT is not 1; the http corpus tier is not enforced") - } - t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run the http command-corpus tier") -} - -type bdResult struct { - stdout, stderr string - code int -} - -func (r bdResult) String() string { - return fmt.Sprintf("exit=%d\nstdout:\n%s\nstderr:\n%s", r.code, clip(r.stdout, 4000), clip(r.stderr, 4000)) -} - -func clip(s string, n int) string { - if len(s) <= n { - return s - } - return s[:n] + fmt.Sprintf("\n... (%d more bytes)", len(s)-n) -} - -// runBD runs the built bd binary in dir with a hermetic environment. -func runBD(t *testing.T, bin, dir string, env []string, stdin string, args ...string) bdResult { - t.Helper() - r, err := execBD(context.Background(), bin, dir, env, stdin, args...) - if err != nil { - t.Fatal(err) - } - return r -} - -// execBD is runBD without the testing.T: it returns an error, rather than -// failing a test, when bd could not be run at all or ctx was cancelled, so it -// is safe on a goroutine that is not the test's. A bd that runs and fails is -// a bdResult with its exit code, as in runBD. -func execBD(ctx context.Context, bin, dir string, env []string, stdin string, args ...string) (bdResult, error) { - if err := ctx.Err(); err != nil { - return bdResult{}, err - } - ctx, cancel := context.WithTimeout(ctx, bdTimeout) - defer cancel() - cmd := exec.CommandContext(ctx, bin, args...) - cmd.Dir = dir - cmd.Env = minimalBDEnv(dir, env...) - if stdin != "" { - cmd.Stdin = strings.NewReader(stdin) - } - var o, e strings.Builder - cmd.Stdout, cmd.Stderr = &o, &e - err := cmd.Run() - code := 0 - if err != nil { - var ee *exec.ExitError - switch { - case errors.Is(ctx.Err(), context.DeadlineExceeded): - code = -1 - e.WriteString(fmt.Sprintf("\n[corpus harness] bd timed out after %s", bdTimeout)) - case ctx.Err() != nil: - return bdResult{}, fmt.Errorf("bd %s: %w", strings.Join(args, " "), ctx.Err()) - case errors.As(err, &ee): - code = ee.ExitCode() - default: - return bdResult{}, fmt.Errorf("running bd %s: %v", strings.Join(args, " "), err) - } - } - return bdResult{stdout: o.String(), stderr: e.String(), code: code}, nil -} - -// minimalBDEnv is backend/http's hermetic bd environment: PATH and the few -// variables a Go binary needs, HOME repointed at dir, nothing BEADS_* carried -// over from the invoking shell. extra is applied last. -func minimalBDEnv(dir string, extra ...string) []string { - env := []string{ - "HOME=" + dir, - "BD_DISABLE_METRICS=1", - "BD_DISABLE_EVENT_FLUSH=1", - } - for _, key := range []string{"PATH", "TMPDIR", "SYSTEMROOT", "WINDIR"} { - if v, ok := os.LookupEnv(key); ok { - env = append(env, key+"="+v) - } - } - return append(env, extra...) -} - -var ( - buildBDOnce sync.Once - bdBinPath string - bdBuildErr error - bdBinOwnBuild bool -) - -// buildBD builds bd from this tree once per test process (or takes Bazel's -// prebuilt binary). TestMain deletes the build. -func buildBD(t *testing.T) string { - t.Helper() - buildBDOnce.Do(func() { - if bazeltest.IsBazel() { - bdBinPath, bdBuildErr = bazeltest.PrebuiltBD() - return - } - if prebuilt, err := bazeltest.PrebuiltBD(); err != nil || prebuilt != "" { - bdBinPath, bdBuildErr = prebuilt, err - return - } - dir, err := os.MkdirTemp(os.Getenv("TMPDIR"), "bd-corpus") - if err != nil { - bdBuildErr = fmt.Errorf("mkdir temp for bd binary: %w", err) - return - } - bin := filepath.Join(dir, "bd-corpus") - cmd := exec.Command("go", "build", "-tags", "gms_pure_go", "-o", bin, "./cmd/bd") - cmd.Dir = repoRoot() - if out, err := cmd.CombinedOutput(); err != nil { - bdBuildErr = fmt.Errorf("build bd: %w\n%s", err, out) - _ = os.RemoveAll(dir) - return - } - bdBinPath = bin - bdBinOwnBuild = true - }) - if bdBuildErr != nil { - t.Fatal(bdBuildErr) - } - if bdBinPath == "" { - t.Fatal("no bd binary: Bazel did not provide one") - } - return bdBinPath -} - -func TestMain(m *testing.M) { - code := m.Run() - if bdBinOwnBuild && bdBinPath != "" { - _ = os.RemoveAll(filepath.Dir(bdBinPath)) - } - os.Exit(code) -} - -func repoRoot() string { - _, file, _, _ := runtime.Caller(0) // /backend/http/corpus/harness_test.go - return filepath.Dir(filepath.Dir(filepath.Dir(filepath.Dir(file)))) -} diff --git a/backend/http/corpus/reference_proxy_test.go b/backend/http/corpus/reference_proxy_test.go deleted file mode 100644 index 0142b1ad4..000000000 --- a/backend/http/corpus/reference_proxy_test.go +++ /dev/null @@ -1,279 +0,0 @@ -//go:build cgo - -package corpus_test - -import ( - "bytes" - "encoding/json" - "fmt" - "io" - "maps" - "net/http" - "net/http/httputil" - "net/url" - "os" - "regexp" - "slices" - "sort" - "strings" - "sync" - "sync/atomic" - "testing" - - "github.com/steveyegge/beads/internal/httpapi" - "github.com/steveyegge/beads/internal/httpapi/apigen" -) - -// referenceProfile is testdata/reference_server.json: the wire surface of the -// deployed server the corpus must work against. See README.md. -type referenceProfile struct { - Capabilities []string `json:"capabilities"` - ContextMembers []string `json:"context_members"` - Operations []referenceOperation `json:"operations"` -} - -type referenceOperation struct { - Method string `json:"method"` - Path string `json:"path"` - OperationID string `json:"operation_id"` - Query []string `json:"query"` - Body []string `json:"body"` - - pattern *regexp.Regexp - query map[string]bool - body map[string]bool -} - -func loadReferenceProfile(t *testing.T) *referenceProfile { - t.Helper() - raw, err := os.ReadFile("testdata/reference_server.json") - if err != nil { - t.Fatalf("read the reference server profile: %v", err) - } - var p referenceProfile - if err := json.Unmarshal(raw, &p); err != nil { - t.Fatalf("parse testdata/reference_server.json: %v", err) - } - if len(p.Capabilities) == 0 || len(p.Operations) == 0 || len(p.ContextMembers) == 0 { - t.Fatal("testdata/reference_server.json is missing capabilities, operations or context_members") - } - for i := range p.Operations { - op := &p.Operations[i] - pattern, err := templatePattern(op.Path) - if err != nil { - t.Fatalf("testdata/reference_server.json: operation %s path %q: %v", op.OperationID, op.Path, err) - } - op.pattern = pattern - op.query = setOf(op.Query) - op.body = setOf(op.Body) - } - return &p -} - -// templatePattern turns an OpenAPI path template into an anchored regexp. A -// path parameter never spans a "/" or a ":" (AIP-136 custom methods hang off -// the last segment as {id}:verb, and no issue id, config key or memory key -// carries a raw colon), so /issues/{id} does not swallow /issues/{id}:claim. -// A "{" with no "}" is an error: the profile is edited by hand, and without the -// check the loop would never advance past it. -func templatePattern(tmpl string) (*regexp.Regexp, error) { - var b strings.Builder - b.WriteString("^") - for { - open := strings.IndexByte(tmpl, '{') - if open < 0 { - b.WriteString(regexp.QuoteMeta(tmpl)) - break - } - closeAt := strings.IndexByte(tmpl[open:], '}') - if closeAt < 0 { - return nil, fmt.Errorf("unclosed { in %q", tmpl[open:]) - } - b.WriteString(regexp.QuoteMeta(tmpl[:open])) - b.WriteString(`[^/:]+`) - tmpl = tmpl[open+closeAt+1:] - } - b.WriteString("$") - return regexp.Compile(b.String()) -} - -func setOf(xs []string) map[string]bool { - m := make(map[string]bool, len(xs)) - for _, x := range xs { - m[x] = true - } - return m -} - -// referenceProxy fronts the in-process server and makes it answer the way the -// reference server answers, at the wire level, for everything this suite can -// see from a client: -// -// - GET /v0/beads/context publishes only the reference ContextResponse -// members (so no wire_revision: the reference server predates it) and -// exactly the reference capability set, whatever this build advertises; -// - a request for an operation the reference document does not publish gets -// the server's own unrouted 404 problem; -// - a query parameter or top-level JSON request member the reference -// operation does not declare gets the server's own 400 unknown_parameter -// problem naming it — which is how the real reference server answers, -// since its decoder is strict. -// -// Responses are otherwise passed through untouched. That is deliberately -// generous in one direction (a response member the reference server would not -// send still reaches the client) and is the residual fidelity gap README.md -// records. -type referenceProxy struct { - profile *referenceProfile - upstream *url.URL - rp *httputil.ReverseProxy - - requests atomic.Int64 - - mu sync.Mutex - refused []string // "METHOD path: why", for the per-shape log -} - -func newReferenceProxy(t *testing.T, profile *referenceProfile, upstreamAddr string) *referenceProxy { - t.Helper() - u, err := url.Parse("http://" + upstreamAddr) - if err != nil { - t.Fatalf("parse upstream address: %v", err) - } - p := &referenceProxy{profile: profile, upstream: u} - p.rp = &httputil.ReverseProxy{ - Rewrite: func(r *httputil.ProxyRequest) { - r.SetURL(u) // also rewrites Host, so the server's Host gate sees its own bind - }, - FlushInterval: -1, // events:watch streams - } - return p -} - -func (p *referenceProxy) takeRefusals() []string { - p.mu.Lock() - defer p.mu.Unlock() - out := p.refused - p.refused = nil - return out -} - -func (p *referenceProxy) noteRefusal(r *http.Request, why string) { - p.mu.Lock() - defer p.mu.Unlock() - p.refused = append(p.refused, fmt.Sprintf("%s %s: %s", r.Method, r.URL.Path, why)) -} - -func (p *referenceProxy) match(r *http.Request) *referenceOperation { - path := r.URL.EscapedPath() - var best *referenceOperation - for i := range p.profile.Operations { - op := &p.profile.Operations[i] - if op.Method != r.Method || !op.pattern.MatchString(path) { - continue - } - // Prefer the most literal template when two match (none do today). - if best == nil || len(op.Path) > len(best.Path) { - best = op - } - } - return best -} - -func (p *referenceProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) { - p.requests.Add(1) - if r.Method == http.MethodGet && r.URL.Path == "/v0/beads/context" { - p.serveContext(w, r) - return - } - op := p.match(r) - if op == nil { - p.noteRefusal(r, "operation not published by the reference server") - detail := "no such route on this server" - httpapi.Write(w, httpapi.Result{Problem: apigen.Problem{ - Status: http.StatusNotFound, - Title: http.StatusText(http.StatusNotFound), - Code: string(httpapi.CodeNotFound), - Detail: &detail, - }}) - return - } - if offender := firstUnknown(slices.Collect(maps.Keys(r.URL.Query())), op.query); offender != "" { - p.noteRefusal(r, "query parameter "+offender+" unknown to "+op.OperationID) - httpapi.Write(w, httpapi.InvalidArgument(offender, httpapi.ReasonUnknownParameter, - "unknown query parameter "+offender)) - return - } - if r.Body != nil && r.Body != http.NoBody { - raw, err := io.ReadAll(r.Body) - _ = r.Body.Close() - if err != nil { - http.Error(w, "reference proxy: read body: "+err.Error(), http.StatusBadGateway) - return - } - r.Body = io.NopCloser(bytes.NewReader(raw)) - r.ContentLength = int64(len(raw)) - var members map[string]json.RawMessage - if len(bytes.TrimSpace(raw)) > 0 && json.Unmarshal(raw, &members) == nil { - if offender := firstUnknown(slices.Collect(maps.Keys(members)), op.body); offender != "" { - p.noteRefusal(r, "request member "+offender+" unknown to "+op.OperationID) - httpapi.Write(w, httpapi.InvalidArgument(offender, httpapi.ReasonUnknownParameter, - "json: unknown field \""+offender+"\"")) - return - } - } - } - p.rp.ServeHTTP(w, r) -} - -// firstUnknown names the smallest key not in known, the same deterministic -// choice the server makes, or "" when every key is known. -func firstUnknown(keys []string, known map[string]bool) string { - sort.Strings(keys) - for _, k := range keys { - if !known[k] { - return k - } - } - return "" -} - -func (p *referenceProxy) serveContext(w http.ResponseWriter, r *http.Request) { - out := r.Clone(r.Context()) - out.URL.Scheme, out.URL.Host, out.Host = p.upstream.Scheme, p.upstream.Host, p.upstream.Host - out.RequestURI = "" - resp, err := http.DefaultTransport.RoundTrip(out) - if err != nil { - http.Error(w, "reference proxy: "+err.Error(), http.StatusBadGateway) - return - } - defer resp.Body.Close() - raw, err := io.ReadAll(resp.Body) - if err != nil { - http.Error(w, "reference proxy: "+err.Error(), http.StatusBadGateway) - return - } - if resp.StatusCode != http.StatusOK { - maps.Copy(w.Header(), resp.Header) - w.WriteHeader(resp.StatusCode) - _, _ = w.Write(raw) - return - } - var body map[string]json.RawMessage - if err := json.Unmarshal(raw, &body); err != nil { - http.Error(w, "reference proxy: decode context: "+err.Error(), http.StatusBadGateway) - return - } - keep := setOf(p.profile.ContextMembers) - for k := range body { - if !keep[k] { - delete(body, k) - } - } - caps, _ := json.Marshal(p.profile.Capabilities) - body["capabilities"] = caps - masked, _ := json.Marshal(body) - w.Header().Set("Content-Type", "application/json") - w.WriteHeader(http.StatusOK) - _, _ = w.Write(masked) -} diff --git a/backend/http/corpus/required_corpus_tier_test.go b/backend/http/corpus/required_corpus_tier_test.go deleted file mode 100644 index 055afeb79..000000000 --- a/backend/http/corpus/required_corpus_tier_test.go +++ /dev/null @@ -1,23 +0,0 @@ -package corpus_test - -import ( - "os" - "testing" -) - -// corpusTierLinked reports whether this test binary links the corpus tier. -// harness_test.go, which builds only under cgo like every other file in this -// package, sets it from its init. -var corpusTierLinked bool - -// TestCorpusTierIsLinkedWhenRequired is the one corpus check that builds -// without cgo. skipUnlessEmbeddedDolt turns a missing BEADS_TEST_EMBEDDED_DOLT -// into a failure under BEADS_HTTP_TEST_REQUIRED=1, but it lives in a cgo file: -// with cgo off the go tool drops every other file in this package, so no -// corpus test exists to fail, and a required run would pass having run nothing. -func TestCorpusTierIsLinkedWhenRequired(t *testing.T) { - if os.Getenv("BEADS_HTTP_TEST_REQUIRED") == "1" && !corpusTierLinked { - t.Fatal("BEADS_HTTP_TEST_REQUIRED=1 but this test binary was built without cgo, which drops " + - "the http command corpus (every other file in this package); build it with CGO_ENABLED=1") - } -} diff --git a/backend/http/corpus/served_pinned_shards.txt b/backend/http/corpus/served_pinned_shards.txt deleted file mode 100644 index 6fc0f44d9..000000000 --- a/backend/http/corpus/served_pinned_shards.txt +++ /dev/null @@ -1,12 +0,0 @@ -# Pinned shards for tools/bazel/go_test_pinned_shard.sh. Not pin_shards.py's: -# each corpus part (corpus_test.go's corpusParts; TestServedCorpusPart1..N) -# runs alone in the shard of its number, on its own server and seed, and the -# binary's round-robin runs the package's other (engine-free) tests in the -# one shard left over. Change corpusParts, this file and the target's -# shard_count together (scripts TestCorpusPartsArePinnedOnePerShard). -# -# -1 TestServedCorpusPart1 -2 TestServedCorpusPart2 -3 TestServedCorpusPart3 -4 TestServedCorpusPart4 diff --git a/backend/http/corpus/testdata/allowlist.yaml b/backend/http/corpus/testdata/allowlist.yaml deleted file mode 100644 index 090577cc9..000000000 --- a/backend/http/corpus/testdata/allowlist.yaml +++ /dev/null @@ -1,29 +0,0 @@ -# Shapes that are EXPECTED to be refused on a served workspace, permanently. -# The served run must fail and its output must match `refusal`; if it ever -# succeeds, the entry is stale and the suite fails until it is removed. The -# embedded run is still held to the shape's expect_exit, which an allowlisted -# shape must state, and to its expect_stderr: a shape that stops doing what it -# says on the embedded backend fails as a corpus bug, not as allowed. -# A gap some slice will close is NOT listed here: it is a `pending` mark on the -# shape in corpus.yaml instead. - -- id: gc-sql-ready-projection - reason: >- - bd sql needs a Dolt sql-server workspace: the embedded backend refuses it - too (the shape's expect_exit and expect_stderr pin that), and an http - workspace refuses it with the same "embedded mode" text. gc must not issue - it against a remote workspace (DESIGN B3 #10 and B6: gc reads the ready - projection through roles there). - refusal: "'bd sql' is not yet supported" -- id: gc-sql-release-fallback - reason: >- - bd sql needs a Dolt sql-server workspace and the embedded backend refuses - it too; gc's ReleaseIfCurrent fallback must take the conditional update - path on a remote workspace (DESIGN B3 #10). - refusal: "'bd sql' is not yet supported" -- id: mc-meta-set - reason: >- - `bd meta set` is not an OSS bd command (pack scripts written against an - older pinned CLI); it fails on the embedded backend too. The packs should - use `bd update --set-metadata`, which the corpus covers. - refusal: 'unknown command "meta"' diff --git a/backend/http/corpus/testdata/corpus.yaml b/backend/http/corpus/testdata/corpus.yaml deleted file mode 100644 index 925d1db64..000000000 --- a/backend/http/corpus/testdata/corpus.yaml +++ /dev/null @@ -1,882 +0,0 @@ -# Served command corpus (slice S23): every distinct bd command shape the -# orchestrator (gc) and its packs/formulas issue against a work store. -# -# Sources: -# gc: gascity origin/main internal/beads/bdstore*.go, internal/config/workquery.go, -# cmd/gc (BdStore subprocess shapes, work_query/scale_check/on_boot/on_death -# shell, hook claim, agent-script actions, doctor/preflight probes). -# mc: pack, formula, prompt and script invocations counted across the -# maintainer packs (flag sets, not values; values here are representative). -# -# Each shape runs on a served workspace (bd connect to an in-process bd serve, -# non-loopback bind mode, bearer auth, masked to testdata/reference_server.json) -# and on an embedded workspace, with BEADS_ACTOR=worker and CLAUDE_SESSION_ID set -# on both. See README.md for the fields. Reads come first; writes after them -# each own their target ids (e2e-t-*), so write order never changes a read. -# The shapes are dealt round robin, in this order, across the suite's parts, -# and each part seeds its own workspaces: a shape may rely on the seed and its -# own setup, never on another shape having run. -# -# pending: {slice, reason} marks a known failure. The shape still runs; when it -# passes the subtest FAILS until the mark is removed. That is how a slice proves -# itself. Permanent refusals live in allowlist.yaml instead. - -files: - - name: seed-core.json - content: | - {"commit_message": "corpus seed", "nodes": [ - {"key": "r1", "id": "e2e-r1", "title": "Routed open task", "type": "task", "priority": 1, "labels": ["lane:a"], "metadata": {"gc.routed_to": "pool-a"}}, - {"key": "r2", "id": "e2e-r2", "title": "Held routed task", "type": "task", "labels": ["hold:mayor"], "metadata": {"gc.routed_to": "pool-a"}}, - {"key": "r3", "id": "e2e-r3", "title": "Routed epic", "type": "epic", "metadata": {"gc.routed_to": "pool-a"}}, - {"key": "wf1", "id": "e2e-wf1", "title": "Workflow root", "type": "task", "metadata": {"gc.run_target": "pool-a", "gc.kind": "workflow"}}, - {"key": "a1", "id": "e2e-a1", "title": "Alice bug", "type": "bug", "assignee": "alice"}, - {"key": "ip1", "id": "e2e-ip1", "title": "Worker in progress", "type": "task", "priority": 0, "assignee": "worker", "metadata": {"gc.routed_to": "pool-a"}}, - {"key": "wr", "id": "e2e-wr", "title": "Worker ready task", "type": "task", "assignee": "worker"}, - {"key": "ipna", "id": "e2e-ipna", "title": "Orphaned in progress", "type": "task", "metadata": {"gc.routed_to": "pool-a"}}, - {"key": "ipnb", "id": "e2e-ipnb", "title": "Orphaned workflow", "type": "task", "metadata": {"gc.run_target": "pool-a", "gc.kind": "workflow"}}, - {"key": "blk", "id": "e2e-blk", "title": "Blocked by r1", "type": "task", "deps": [{"target": "r1"}]}, - {"key": "done", "id": "e2e-done", "title": "Closed task", "type": "task"}, - {"key": "ext", "id": "e2e-ext", "title": "GitHub source", "type": "task", "external_ref": "gh-123", "metadata": {"gc.kind": "github_source", "gc.github.kind": "issue", "gc.github.repo": "o/r"}}, - {"key": "ep", "id": "e2e-ep", "title": "Epic", "type": "epic"}, - {"key": "ep.1", "id": "e2e-ep.1", "title": "Epic child one", "type": "task", "parent_key": "ep"}, - {"key": "ep.2", "id": "e2e-ep.2", "title": "Epic child two", "type": "task", "parent_key": "ep"}, - {"key": "mol", "id": "e2e-mol", "title": "Molecule root", "type": "epic"}, - {"key": "mol.1", "id": "e2e-mol.1", "title": "step one", "type": "task", "assignee": "worker", "parent_key": "mol"}, - {"key": "mol.2", "id": "e2e-mol.2", "title": "step two", "type": "task", "parent_key": "mol", "deps": [{"target": "mol.1"}], "metadata": {"gc.root_bead_id": "e2e-mol", "gc.routed_to": "pool-a"}}, - {"key": "mol.3", "id": "e2e-mol.3", "title": "step three", "type": "task", "parent_key": "mol"}, - {"key": "mac", "id": "e2e-mac", "title": "Auto-close molecule", "type": "epic"}, - {"key": "mac.1", "id": "e2e-mac.1", "title": "done step", "type": "task", "parent_key": "mac"}, - {"key": "mac.2", "id": "e2e-mac.2", "title": "last step", "type": "task", "assignee": "worker", "parent_key": "mac"}, - {"key": "wisp-1", "id": "e2e-wisp-1", "title": "Wisp wisp-1", "type": "task", "ephemeral": true, "metadata": {"gc.routed_to": "pool-a"}, "labels": ["lane:w"]}, - {"key": "wisp-2", "id": "e2e-wisp-2", "title": "Wisp wisp-2", "type": "task", "ephemeral": true, "assignee": "worker", "labels": ["lane:w"]}, - {"key": "wisp-wr", "id": "e2e-wisp-wr", "title": "Wisp worker ready", "type": "task", "ephemeral": true, "priority": 1, "assignee": "worker"}, - {"key": "wisp-c", "id": "e2e-wisp-c", "title": "Wisp wisp-c", "type": "task", "ephemeral": true}, - {"key": "wisp-p", "id": "e2e-wisp-p", "title": "Wisp wisp-p", "type": "task", "ephemeral": true}, - {"key": "wisp-m", "id": "e2e-wisp-m", "title": "Wisp molecule", "type": "epic", "ephemeral": true}, - {"key": "wisp-m.1", "id": "e2e-wisp-m.1", "title": "wisp step", "type": "task", "ephemeral": true, "parent_key": "wisp-m"}, - {"key": "gate-o", "id": "e2e-gate-o", "title": "Open gate", "type": "gate"}, - {"key": "gate-c", "id": "e2e-gate-c", "title": "Closed gate", "type": "gate"}, - {"key": "wisp-gate", "id": "e2e-wisp-gate", "title": "Wisp gate", "type": "gate", "ephemeral": true}, - {"key": "t-upd", "id": "e2e-t-upd", "title": "target upd", "type": "task", "labels": ["lane:z"]}, - {"key": "t-upd2", "id": "e2e-t-upd2", "title": "target upd2", "type": "task"}, - {"key": "t-m1", "id": "e2e-t-m1", "title": "target m1", "type": "task"}, - {"key": "t-m2", "id": "e2e-t-m2", "title": "target m2", "type": "task"}, - {"key": "t-claim", "id": "e2e-t-claim", "title": "target claim", "type": "task"}, - {"key": "t-claim2", "id": "e2e-t-claim2", "title": "target claim2", "type": "task"}, - {"key": "t-rev", "id": "e2e-t-rev", "title": "target rev", "type": "task"}, - {"key": "t-rev2", "id": "e2e-t-rev2", "title": "target rev2", "type": "task"}, - {"key": "t-sling", "id": "e2e-t-sling", "title": "target sling", "type": "task"}, - {"key": "t-as", "id": "e2e-t-as", "title": "target as", "type": "task"}, - {"key": "t-ms", "id": "e2e-t-ms", "title": "target ms", "type": "task"}, - {"key": "t-msc", "id": "e2e-t-msc", "title": "target msc", "type": "task"}, - {"key": "t-um", "id": "e2e-t-um", "title": "target um", "type": "task", "metadata": {"k": "v"}}, - {"key": "t-notes", "id": "e2e-t-notes", "title": "target notes", "type": "task"}, - {"key": "t-er", "id": "e2e-t-er", "title": "target er", "type": "task"}, - {"key": "t-md", "id": "e2e-t-md", "title": "target md", "type": "task"}, - {"key": "t-asg", "id": "e2e-t-asg", "title": "target asg", "type": "task"}, - {"key": "t-dac", "id": "e2e-t-dac", "title": "target dac", "type": "task"}, - {"key": "t-c1", "id": "e2e-t-c1", "title": "target c1", "type": "task"}, - {"key": "t-c2", "id": "e2e-t-c2", "title": "target c2", "type": "task"}, - {"key": "t-c3", "id": "e2e-t-c3", "title": "target c3", "type": "task"}, - {"key": "t-c4", "id": "e2e-t-c4", "title": "target c4", "type": "task"}, - {"key": "t-c5", "id": "e2e-t-c5", "title": "target c5", "type": "task"}, - {"key": "t-d1", "id": "e2e-t-d1", "title": "target d1", "type": "task"}, - {"key": "t-d2", "id": "e2e-t-d2", "title": "target d2", "type": "task"}, - {"key": "t-d3", "id": "e2e-t-d3", "title": "target d3", "type": "task"}, - {"key": "t-d4", "id": "e2e-t-d4", "title": "target d4", "type": "task"}, - {"key": "t-df1", "id": "e2e-t-df1", "title": "target df1", "type": "task"}, - {"key": "t-df2", "id": "e2e-t-df2", "title": "target df2", "type": "task"}, - {"key": "t-da", "id": "e2e-t-da", "title": "target da", "type": "task"}, - {"key": "t-db", "id": "e2e-t-db", "title": "target db", "type": "task"}, - {"key": "t-l", "id": "e2e-t-l", "title": "target l", "type": "task"}, - {"key": "t-as2", "id": "e2e-t-as2", "title": "target as2", "type": "task"}, - {"key": "t-cm", "id": "e2e-t-cm", "title": "target cm", "type": "task"}, - {"key": "t-rel", "id": "e2e-t-rel", "title": "target rel", "type": "task", "assignee": "worker"}, - {"key": "t-xfer", "id": "e2e-t-xfer", "title": "target xfer", "type": "task", "assignee": "worker"}, - {"key": "t-ifa", "id": "e2e-t-ifa", "title": "target ifa", "type": "task", "assignee": "worker", "metadata": {"seeded": "keep"}}, - {"key": "t-death", "id": "e2e-t-death", "title": "target death", "type": "task", "assignee": "worker"}, - {"key": "t-rc", "id": "e2e-t-rc", "title": "target rc", "type": "task", "assignee": "worker"}, - {"key": "t-hb", "id": "e2e-t-hb", "title": "target hb", "type": "task"}, - {"key": "t-hb2", "id": "e2e-t-hb2", "title": "target hb2", "type": "task"}, - {"key": "t-hb3", "id": "e2e-t-hb3", "title": "target hb3", "type": "task"}, - {"key": "t-boot", "id": "e2e-t-boot", "title": "target boot", "type": "task"}, - {"key": "t-ro", "id": "e2e-t-ro", "title": "target reopen", "type": "task"}, - {"key": "t-dr", "id": "e2e-t-dr", "title": "target dep remove", "type": "task", "deps": [{"target": "t-db"}]}, - {"key": "t-dc", "id": "e2e-t-dc", "title": "cascade root", "type": "epic"}, - {"key": "t-dc.1", "id": "e2e-t-dc.1", "title": "cascade child", "type": "task", "parent_key": "t-dc"}, - {"key": "t-gcr", "id": "e2e-t-gcr", "title": "target gate create", "type": "task"}, - {"key": "t-gct", "id": "e2e-t-gct", "title": "target gate check", "type": "task"}, - {"key": "t-gw", "id": "e2e-t-gw", "title": "target gate waiter", "type": "gate"}, - {"key": "t-gaw", "id": "e2e-t-gaw", "title": "target gate await id", "type": "gate"} - ]} - - name: bulk-ready-a.json - bulk: {id_prefix: e2e-bra, count: 60, priority: 3, metadata: {gc.routed_to: pool-a}} - - name: bulk-ready-b.json - bulk: {id_prefix: e2e-brb, count: 60, priority: 3, metadata: {gc.routed_to: pool-a}} - - name: bulk-wisp-a.json - bulk: {id_prefix: e2e-wisp-bwa, count: 55, priority: 3, ephemeral: true, metadata: {gc.routed_to: pool-a}} - - name: bulk-wisp-b.json - bulk: {id_prefix: e2e-wisp-bwb, count: 55, priority: 3, ephemeral: true, metadata: {gc.routed_to: pool-a}} - - name: graph-small.json - content: | - {"commit_message": "gc graph apply", "nodes": [ - {"key": "root", "id": "e2e-g-root", "title": "graph root", "type": "epic", "metadata": {"gc.kind": "workflow"}}, - {"key": "s1", "id": "e2e-g-s1", "title": "graph step 1", "type": "task", "parent_key": "root", "assignee": "worker", "labels": ["lane:g"], "metadata": {"gc.routed_to": "pool-a"}}, - {"key": "s2", "id": "e2e-g-s2", "title": "graph step 2", "type": "task", "parent_key": "root"} - ], "edges": [{"from_key": "s2", "to_key": "s1", "type": "blocks"}]} - - name: graph-eph.json - content: | - {"nodes": [ - {"key": "root", "id": "e2e-wisp-g-root", "title": "wisp graph root", "type": "epic"}, - {"key": "s1", "id": "e2e-wisp-g-s1", "title": "wisp graph step", "type": "task", "parent_key": "root"} - ]} - - name: delete-list.txt - content: | - e2e-t-df1 - e2e-t-df2 - -seed: - - [create, --graph, "{file:seed-core.json}", --json] - - [create, --graph, "{file:bulk-ready-a.json}", --json] - - [create, --graph, "{file:bulk-ready-b.json}", --json] - - [create, --graph, "{file:bulk-wisp-a.json}", --json] - - [create, --graph, "{file:bulk-wisp-b.json}", --json] - # Graph plans carry no status: a node created in_progress or closed stamps - # started_at/closed_at, which batchApply's create vocabulary cannot carry - # (gc's own plans never set a status). Lifecycle goes through update/close. - - [update, e2e-ip1, e2e-ipna, e2e-ipnb, e2e-mol.1, e2e-mac.2, e2e-t-rel, e2e-t-xfer, e2e-t-ifa, e2e-t-death, e2e-t-rc, e2e-t-boot, --status, in_progress] - - [update, e2e-wisp-2, --status, in_progress] - - [close, --force, e2e-done, e2e-ep.2, e2e-mol.3, e2e-mac.1, e2e-t-ro, e2e-gate-c] - - [comments, add, e2e-r1, "seed comment"] - -shapes: - # ---------------------------------------------------------------- show - # requests pins each show's round trips (mc-5dkzp): resolving the id, - # fetching the row and reading it for output are one request per id, which - # the per-command read memo answers once. - - id: gc-show-json - source: gc bdstore.go BdStore.Get (show --json ); counts must not read 0 - args: [show, --json, e2e-blk] - requests: 1 - - id: gc-show-json-batch - source: gc bdstore_exact_batch.go GetExactBatch - args: [show, --json, e2e-r1, e2e-a1, e2e-blk, e2e-mol] - requests: 4 - - id: gc-show-json-wisp - source: gc workquery.go blocked_by enrichment; wisps live in the ready tier - args: [show, e2e-wisp-1, --json] - requests: 1 - - id: gc-show-id-flag - source: gc cmd_bd_show_watch.go bdWatchRunner (show --json --id=) - args: [show, --json, --id=e2e-r1] - requests: 1 - # The worker holds eight in-progress issues; e2e-ip1 is the only P0 among - # them, so "first in the store's default order" (priority first) names it on - # both sides. Among equal priorities the order rests on created_at, and a - # seed that straddles a second boundary on one side only picked another issue. - - id: gc-show-current - source: gc cmd_bd_show_watch.go bdWatchRunner (show --current --json) - args: [show, --current, --json] - requests: 3 - - id: gc-show-missing - source: gc BdStore.Get not-found classification (routing fallbacks) - args: [show, --json, e2e-nonexistent] - expect_exit: 1 - expect_stderr: "(?i)not found" - compare: exit - requests: 1 - # Text show reads the row, then its dependents and its comments: three - # different URLs the memo cannot collapse. - - id: mc-show-text - source: mc prompts (bd show , ~80) - args: [show, e2e-r1] - compare: text - requests: 3 - - # ---------------------------------------------------------------- ready - # e2e-wr and e2e-wisp-wr are the only open rows assigned to the worker when - # these shapes run, so the assignee shapes compare real rows: a client that - # sends the wrong assignee or drops --include-ephemeral returns a different - # list. The wisp is P1 and the task P2, so --limit=1 picks the wisp on both - # sides by priority alone. - - id: gc-ready-unlimited - source: gc bdstore.go bdReadyArgs (BdStore.Ready, ready --json --limit 0) - args: [ready, --json, --limit, "0"] - compare: ids - - id: gc-ready-ephemeral-assignee-unlimited - source: gc bdstore.go bdReadyArgs with --include-ephemeral --assignee - args: [ready, --json, --include-ephemeral, --assignee, worker, --limit, "0"] - compare: ids - - id: gc-ready-pool-demand - source: gc workquery.go bdReadyPoolDemandShell (scale_check, --limit=20) - args: [ready, --metadata-field, gc.routed_to=pool-a, --unassigned, --exclude-type=epic, --exclude-label, "hold:mayor", --exclude-label, "hold:external", --json, --limit=20] - compare: ids - - id: gc-ready-pool-demand-unlimited - source: gc workquery.go bdReadyPoolDemandShell (work_query, --include-ephemeral --limit 0) - args: [ready, --include-ephemeral, --metadata-field, gc.routed_to=pool-a, --unassigned, --exclude-type=epic, --exclude-label, "hold:mayor", --exclude-label, "hold:external", --json, --limit, "0"] - compare: ids - - id: gc-ready-workflow-migration - source: gc workquery.go bdReadyPoolDemandMigrationShell (--sort oldest --limit 0) - args: [ready, --include-ephemeral, --metadata-field, gc.run_target=pool-a, --metadata-field, gc.kind=workflow, --unassigned, --exclude-type=epic, --exclude-label, "hold:mayor", --exclude-label, "hold:external", --json, --sort, oldest, --limit, "0"] - compare: ids - - id: gc-ready-graph-anchor - source: gc workquery.go assignedGraphWorkflowAnchorReadyFunctionScript - args: [ready, --include-ephemeral, --metadata-field, gc.root_bead_id=e2e-mol, --metadata-field, gc.routed_to=pool-a, --exclude-type=epic, --exclude-label, "hold:mayor", --exclude-label, "hold:external", --json, --sort, oldest, --limit=20] - compare: ids - - id: gc-ready-assigned-tier - source: gc workquery.go assignedReadyTierCommand (--assignee= --limit=1) - args: [ready, --include-ephemeral, --assignee=worker, --json, --limit=1] - compare: ids - - id: gc-ready-probe - source: gc examples/bd gc-beads-bd.sh (bare bd ready probe) - args: [ready] - compare: exit - - id: mc-ready-json - source: mc scripts/prompts (bd ready --json, ~36) - args: [ready, --json] - compare: ids - - id: mc-ready-mol - source: mc skills/pr-review (bd ready --mol ) - args: [ready, --mol, e2e-mol, --json] - - id: mc-ready-parent - source: mc skills/pr-review (bd ready --parent ) - args: [ready, --parent, e2e-ep, --json] - compare: ids - - # ---------------------------------------------------------------- list - - id: gc-list-full-filters - source: gc bdstore.go listViaBDList (List/ListByLabel/ListByMetadata) - args: [list, --json, --label=lane:a, --status=open, --type=task, --include-infra, --include-gates, --limit, "0", --metadata-field, gc.routed_to=pool-a] - - id: gc-list-ping - source: gc bdstore.go BdStore.Ping (list --json --limit 0) - args: [list, --json, --limit, "0"] - compare: ids - - id: gc-list-limit1 - source: gc doctor_gate_sandbox.go / doctor_store_preflight.go - args: [list, --json, --limit, "1"] - compare: count - - id: gc-list-readonly - source: gc doctor_v2_session_migration.go readOnlyBdRunner - args: [--readonly, list, --json, --limit, "1"] - compare: count - - id: gc-list-assignee-status - source: gc bdstore.go listViaBDList (ListByAssignee) - args: [list, --json, --assignee=worker, --status=in_progress, --include-infra, --include-gates, --limit, "0"] - - id: gc-list-all-created-before - source: gc bdstore.go listViaBDList (--all --created-before --include-templates --skip-labels) - args: [list, --json, --all, --created-before, "2100-01-01T00:00:00Z", --include-infra, --include-gates, --include-templates, --limit, "0", --skip-labels] - compare: ids - - id: gc-list-children - source: gc bdstore.go listViaBDList (Children, --parent) - args: [list, --json, --include-infra, --include-gates, --limit, "0", --parent, e2e-ep, --all] - - id: gc-list-in-progress-tier - source: gc workquery.go assignedInProgressTierCommandWithLimit - args: [list, --status, in_progress, --assignee=worker, --json, --limit=20] - - id: gc-list-on-death - source: gc workquery.go EffectiveOnDeath - args: [list, --assignee=worker, --status=in_progress, --json] - - id: gc-list-on-boot - source: gc workquery.go on_boot recovery (--no-assignee) - args: [list, --metadata-field, gc.routed_to=pool-a, --status=in_progress, --no-assignee, --json] - pending: {slice: "SRV-1", reason: "S15b sends --no-assignee as listIssues no_assignee behind issues.list.noAssignee, which the reference server does not advertise, so the client refuses before dialing (pinned passing against the current server by backend/http TestE2E_ListScopeFiltersOverHTTP); remove the mark when reference_server.json is upgraded"} - - id: gc-list-on-boot-workflow - source: gc workquery.go on_boot recovery, workflow lane - args: [list, --metadata-field, gc.run_target=pool-a, --metadata-field, gc.kind=workflow, --status=in_progress, --no-assignee, --json] - pending: {slice: "SRV-1", reason: "S15b sends --no-assignee as listIssues no_assignee behind issues.list.noAssignee, which the reference server does not advertise, so the client refuses before dialing (pinned passing against the current server by backend/http TestE2E_ListScopeFiltersOverHTTP); remove the mark when reference_server.json is upgraded"} - - id: mc-list-meta-status - source: mc assets snapshot.md (list --json --limit --metadata-field --status) - args: [list, --json, --limit, "50", --metadata-field, gc.routed_to=pool-a, --status, open] - compare: ids - # More than 50 rows match, most at one priority: --limit 50 cuts a tie set - # whose created_at order differs between the two seeds, so each side's 50 - # rows are held to its own unlimited listing instead. - prefix_of: [list, --json, --limit, "0", --metadata-field, gc.routed_to=pool-a, --status, open] - - id: mc-list-github-source - source: mc github-intake (list --all --json --limit --metadata-field x3) - args: [list, --metadata-field, gc.kind=github_source, --metadata-field, gc.github.kind=issue, --metadata-field, gc.github.repo=o/r, --all, --json, --limit, "0"] - - id: mc-list-all-type - source: mc normalized_candidate_graph.py (list --all --type) - args: [list, --all, --type, epic, --json, --limit, "50"] - - id: mc-list-include-gates-infra-label - source: mc mol-normalized-issue-pr-v1 (list --all --include-gates --include-infra --json --label --limit) - args: [list, --all, --include-gates, --include-infra, --json, --label, "lane:a", --limit, "0"] - - id: mc-list-title - source: mc mol-normalized-issue-pr-v1 (list --all --include-infra --json --limit --title) - args: [list, --all, --include-infra, --json, --limit, "10", --title, "Routed open task"] - pending: {slice: "SRV-1", reason: "S15b sends --title as listIssues title behind issues.list.title, which the reference server does not advertise, so the client refuses before dialing (pinned passing against the current server by backend/http TestE2E_ListScopeFiltersOverHTTP); remove the mark when reference_server.json is upgraded"} - - id: mc-list-id - source: mc skills/pr-review (list --all --id --json --limit) - args: [list, --all, --id, "e2e-r1,e2e-a1", --json, --limit, "0"] - pending: {slice: "SRV-1", reason: "S15b sends --id as listIssues id behind issues.list.ids, which the reference server does not advertise, so the client refuses before dialing (pinned passing against the current server by backend/http TestE2E_ListScopeFiltersOverHTTP); remove the mark when reference_server.json is upgraded"} - - id: gc-polecat-title-contains - source: gascity core formulas/mol-polecat-base.toml (list --title-contains --status a,b,c --json) - args: [list, --title-contains, "Routed", --status, "open,in_progress,blocked", --json] - pending: {slice: "SRV-1", reason: "--title-contains folds onto listIssues title behind issues.list.title, which the reference server does not advertise, so the client refuses before dialing (pinned passing against the current server by backend/http TestE2E_ListScopeFiltersOverHTTP); remove the mark when reference_server.json is upgraded"} - - id: gc-polecat-title-contains-closed-after - source: gascity core formulas/mol-polecat-base.toml (pre-flight 3d, list --title-contains --status closed --closed-after DATE --json) - args: [list, --title-contains, "closed task", --status, closed, --closed-after, "2000-01-01", --json] - pending: {slice: "SRV-1", reason: "needs issues.list.title (--title-contains folds onto listIssues title) and issues.list.filters (closed_after), which the reference server does not advertise, so the client refuses before dialing; remove the mark when reference_server.json is upgraded"} - - id: gc-polecat-closed-after - source: gascity core formulas/mol-polecat-base.toml:267 (list --title-contains --status closed --closed-after --json) - args: [list, --title-contains, "task", --status, closed, --closed-after, "2000-01-01", --json] - compare: ids - pending: {slice: "SRV-1", reason: "needs issues.list.title (--title-contains folds onto listIssues title) and issues.list.filters (closed_after), which the reference server does not advertise, so the client refuses before dialing (pinned passing against the current server by backend/http TestE2E_ListFilterParityOverHTTP); remove the mark when reference_server.json is upgraded"} - - id: wf-session-cleanup - source: an order cleanup script's closed-session sweep (list --all --include-infra --include-gates --status closed --label --closed-before --updated-before) - args: [list, --json, --all, --include-infra, --include-gates, --status, closed, --label, "lane:a", --closed-before, "2100-01-01T00:00:00Z", --updated-before, "2100-01-01T00:00:00Z", --limit, "0"] - compare: ids - pending: {slice: "SRV-1", reason: "needs issues.list.filters, which the reference server does not advertise, so the client refuses before dialing (pinned passing against the current server by backend/http TestE2E_ListFilterParityOverHTTP); remove the mark when reference_server.json is upgraded"} - - id: list-filter-priority-range - source: bd list --priority-min/--priority-max (listing filter parity) - args: [list, --json, --priority-min, "1", --priority-max, "3", --limit, "0"] - compare: ids - pending: {slice: "SRV-1", reason: "needs issues.list.filters, which the reference server does not advertise, so the client refuses before dialing (pinned passing against the current server by backend/http TestE2E_ListFilterParityOverHTTP); remove the mark when reference_server.json is upgraded"} - - id: list-filter-exclude-type-no-parent - source: bd list --exclude-type --no-parent (listing filter parity) - args: [list, --json, --exclude-type, epic, --no-parent, --limit, "0"] - compare: ids - pending: {slice: "SRV-1", reason: "needs issues.list.filters, which the reference server does not advertise, so the client refuses before dialing (pinned passing against the current server by backend/http TestE2E_ListFilterParityOverHTTP); remove the mark when reference_server.json is upgraded"} - - id: list-filter-updated-after - source: bd list --updated-after (listing filter parity) - args: [list, --json, --updated-after, "2000-01-01", --limit, "0"] - compare: ids - pending: {slice: "SRV-1", reason: "needs issues.list.filters, which the reference server does not advertise, so the client refuses before dialing (pinned passing against the current server by backend/http TestE2E_ListFilterParityOverHTTP); remove the mark when reference_server.json is upgraded"} - - id: mc-list-assignee-status - source: mc review summaries (list --assignee --json --limit --status) - args: [list, --assignee, alice, --json, --limit, "10", --status, open] - - id: mc-list-label-text - source: mc normalized_child_launch.py (list --label) - args: [list, --label, "lane:a"] - compare: text - - id: mc-list-plain - source: mc scripts (bare bd list) - args: [list] - compare: exit - - id: mc-children - source: mc prompts (bd children ) - args: [children, e2e-ep, --json] - - # ---------------------------------------------------------------- query / count / search - - id: gc-query-wisps - source: gc bdstore.go listEphemeral (query --json "ephemeral=true AND ..." --limit N) - args: [query, --json, "ephemeral=true AND label=lane:w", --limit, "50"] - - id: gc-query-wisps-all - source: gc bdstore.go listEphemeral with IncludeClosed (--all) - args: [query, --json, "ephemeral=true AND status=in_progress", --all, --limit, "50"] - - id: gc-query-wisp-by-id - source: gc bdstore.go getEphemeralByID - args: [query, --json, "ephemeral=true AND id=e2e-wisp-1", --all, --limit, "1"] - - id: gc-query-wisps-unlimited - source: gc workquery.go bdQueryEphemeralStatusShell (--limit=0, wisp tier) - args: [query, --json, "ephemeral=true AND status=open", --limit=0] - compare: ids - - id: mc-query - source: mc pr_merge.py (bd query) - args: [query, --json, "status=open AND priority<=1", --limit, "50"] - - id: gc-count - source: gc native CountIssues fallback (count without scope) - args: [count, --json] - - id: gc-count-status - source: gc count with a status filter - args: [count, --json, --status, open] - - id: mc-search - source: mc mol-bug-report-flow-v2 (bd search) - args: [search, "Routed", --json] - compare: ids - pending: {slice: "S6c+SRV-1", reason: "S26 sends bd search as listIssues q behind issues.list.search, which the reference server does not advertise, so the client refuses before dialing; remove the mark when reference_server.json is upgraded"} - - id: mc-status - source: mc normalized_child_launch.py (bd status) - args: [status, --json] - - # ---------------------------------------------------------------- deps / graph reads - - id: gc-dep-list - source: gc bdstore.go DepList - args: [dep, list, e2e-blk, --json] - - id: gc-dep-list-up - source: gc bdstore.go DepList (--direction=up) - args: [dep, list, e2e-r1, --json, --direction=up] - - id: gc-dep-list-batch - source: gc bdstore.go DepListBatch - args: [dep, list, e2e-blk, e2e-mol.2, --json] - - id: mc-dep-tree - source: mc pr_review.py city_dep_subtree (dep tree --json) - args: [dep, tree, e2e-mol, --json] - - id: gc-blocked - source: gc bdstore_ready_projection.go fetchReadyProjectionViaBlocked - args: [blocked, --json] - pending: {slice: "SRV-1", reason: "bd blocked dials listBlockedIssues behind blocked.list, which the reference server does not advertise, so the client refuses at its capability preflight (pinned passing against the current server by backend/http TestE2E_BlockedOverHTTP); remove the mark when reference_server.json is upgraded"} - - id: gc-blocked-flags - source: gc bdstore_ready_projection.go readiness door (bd blocked --flags --json, ids on stdin) - args: [blocked, --flags, --json] - stdin: "e2e-mol\ne2e-mol.1\ne2e-mol.2\ne2e-no-such-row\n" - compare: json - pending: {slice: "SRV-1", reason: "bd blocked --flags reads listBlockingAnnotations is_blocked behind dependencies.blocking.isBlocked, which the reference server does not advertise, so the client refuses at its capability preflight (pinned passing against the current server by backend/http TestE2E_BlockedFlagsOverHTTP); remove the mark when reference_server.json is upgraded"} - - id: gc-blocked-flags-raw - source: gc bdstore_ready_projection.go readiness door (the stored is_blocked column, bd blocked --flags --raw --json, ids on stdin) - args: [blocked, --flags, --raw, --json] - stdin: "e2e-mol\ne2e-mol.1\ne2e-mol.2\ne2e-no-such-row\n" - compare: json - pending: {slice: "SRV-1", reason: "bd blocked --flags reads listBlockingAnnotations is_blocked behind dependencies.blocking.isBlocked, which the reference server does not advertise, so the client refuses at its capability preflight (pinned passing against the current server by backend/http TestE2E_BlockedFlagsOverHTTP); remove the mark when reference_server.json is upgraded"} - - id: gc-blocked-parent - source: bd blocked --parent (the molecule-scoped blocked listing) - args: [blocked, --json, --parent, e2e-mol] - pending: {slice: "SRV-1", reason: "bd blocked dials listBlockedIssues behind blocked.list, which the reference server does not advertise, so the client refuses at its capability preflight (pinned passing against the current server by backend/http TestE2E_BlockedOverHTTP); remove the mark when reference_server.json is upgraded"} - - # ---------------------------------------------------------------- molecules - - id: mc-mol-current - source: mc worker prompts (bd mol current, ~38; inferred from in_progress work) - args: [mol, current, --json] - - id: mc-mol-current-text - source: mc gc-role-worker template (bd mol current, text) - args: [mol, current] - compare: text - - id: mc-mol-current-id - source: mc prompts (bd mol current ) - args: [mol, current, e2e-mol, --json] - - id: mc-mol-progress - source: mc prompts (bd mol progress ) - args: [mol, progress, e2e-mol, --json] - - # ---------------------------------------------------------------- config / probes / local - - id: gc-config-get-types - source: gc beads_provider_lifecycle.go / checks_custom_types.go (config get --json types.custom) - args: [config, get, --json, types.custom] - - id: gc-config-get-prefix - source: gc gc-beads-bd.sh (config get issue_prefix) - args: [config, get, issue_prefix] - compare: text - - id: gc-types - source: gc checks_custom_types.go (types --json) - args: [types, --json] - - id: gc-context - source: gc beads_preflight_checker.go (context --json); never touches the store - args: [context, --json] - compare: exit - - id: gc-version - source: gc bdstore_blocked_repair.go BDVersion - args: [version] - compare: exit - - id: gc-ping - source: gc cmd_beads_city_migrate_proxied.go / dashboard (ping --json) - args: [ping, --json] - compare: exit - - id: mc-prime - source: mc formulas (bd prime) - args: [prime] - compare: exit - - id: gc-sql-ready-projection - source: gc bdstore_ready_projection.go fetchReadyProjection - args: [sql, "select id,is_blocked from issues where status <> 'closed' union all select id,is_blocked from wisps where status <> 'closed'", --json] - expect_exit: 1 - expect_stderr: "'bd sql' is not yet supported" - compare: exit - - id: gc-sql-release-fallback - source: gc bdstore.go ReleaseIfCurrent fallback (sql UPDATE) - args: [sql, --json, "UPDATE issues SET status='open', assignee='' WHERE id='e2e-t-rel' AND status='in_progress' AND assignee='worker'"] - expect_exit: 1 - expect_stderr: "'bd sql' is not yet supported" - compare: exit - - id: gc-purge-dry-run - source: gc bdstore.go Purge (purge --json --dry-run) - args: [purge, --json, --dry-run] - compare: exit - pending: {slice: "SRV-1", reason: "purge needs the sweep tokens the reference server does not advertise (B3 #13); remove the mark when reference_server.json is upgraded"} - - id: gc-recompute-blocked - source: gc bdstore_blocked_repair.go RecomputeBlocked - args: [recompute-blocked, --json] - compare: exit - - id: gc-backup-status - source: gc checks_proxied_backup_coverage.go (backup status --json) - args: [backup, status, --json] - compare: exit - # The seed holds an open gate, a closed gate and an ephemeral gate, so the - # open listing and the --all listing answer different, non-empty sets. - - id: mc-gate-list - source: gc core pack order renudge-stale-human-gates.sh (gc bd gate list --limit 0 --json) - args: [gate, list, --limit, "0", --json] - - id: mc-gate-list-all - source: core pack orders (gate list --all) - args: [gate, list, --all, --json] - # gate check lists the open gates through the same ListGates entry. No - # seeded gate carries an await type (the seed graph carries no gate members; - # mc-gate-check-timer-resolves below creates a real timer gate), so the - # typed sweeps find nothing of their type and the untyped one lists the open - # gates and evaluates none: the listing is what these shapes prove served, - # and nothing resolves, escalates (no gt exec) or writes. - - id: mc-gate-check-timer - source: gc core pack gate-sweep.sh (bd gate check --type=timer --escalate) - args: [gate, check, --type=timer, --escalate] - compare: text - - id: mc-gate-check-gh - source: gc core pack gate-sweep.sh (bd gate check --type=gh --escalate) - args: [gate, check, --type=gh, --escalate] - compare: text - - id: gate-check-all - source: bd gate check (every open gate) - args: [gate, check] - compare: text - - - id: gc-export-all - source: gc core pack reaper (gc bd export --all -o F) - args: [export, --all, -o, "{file:export.jsonl}"] - compare: exit - - # ================================================================ writes - # ---------------------------------------------------------------- create - - id: gc-create-full - source: gc bdstore.go CreateWithStorage (every optional flag but --parent) - args: [create, --json, "gc created", -t, task, --id, e2e-n1, --priority, "1", --description, "made by gc", --assignee, worker, --deps, e2e-r1, --labels, "lane:n,x", --metadata, '{"gc.routed_to":"pool-a"}'] - compare: exit - verify: [[show, --json, e2e-n1]] - - id: gc-create-parent - source: gc bdstore.go CreateWithStorage (--parent, server-minted child id) - args: [create, --json, "gc child", -t, task, --parent, e2e-ep] - compare: exit - verify: [[show, --json, e2e-ep.3]] - - id: gc-create-ephemeral - source: gc bdstore.go CreateWithStorage (--ephemeral) - args: [create, --json, "gc wisp", -t, task, --id, e2e-wisp-n2, --ephemeral, --metadata, '{"gc.routed_to":"pool-a"}'] - compare: exit - verify: [[show, --json, e2e-wisp-n2]] - - id: gc-create-no-history - source: gc bdstore.go CreateWithStorage (--no-history, agent beads) - args: [create, --json, "gc agent bead", -t, task, --id, e2e-n3, --no-history] - compare: exit - verify: [[show, --json, e2e-n3]] - - id: gc-create-defer - source: gc bdstore.go CreateWithStorage (--defer RFC3339) - args: [create, --json, "gc deferred", -t, task, --id, e2e-n4, --defer, "2100-01-01T00:00:00Z"] - compare: exit - verify: [[show, --json, e2e-n4]] - - id: mc-create-external-ref - source: mc snapshot.md (create --external-ref --labels --metadata --type) - args: [create, "triaged issue", --external-ref, gh-999, --labels, "triage", --metadata, '{"gc.kind":"github_source"}', --type, task, --id, e2e-n5, --json] - compare: exit - verify: [[show, --json, e2e-n5]] - - id: mc-create-text - source: mc prompts (bd create, text output) - args: [create, "plain create", --type, task, --id, e2e-n6] - compare: exit - verify: [[show, --json, e2e-n6]] - - id: gc-create-graph - source: gc bdstore_graph_apply.go ApplyGraphPlanWithStorage - args: [create, --graph, "{file:graph-small.json}", --json] - verify: [[show, --json, e2e-g-root, e2e-g-s1, e2e-g-s2]] - - id: gc-create-graph-ephemeral - source: gc bdstore_graph_apply.go (--ephemeral by storage class) - args: [create, --graph, "{file:graph-eph.json}", --json, --ephemeral] - verify: [[show, --json, e2e-wisp-g-root, e2e-wisp-g-s1]] - - # ---------------------------------------------------------------- update - - id: gc-update-fields - source: gc bdstore.go bdUpdateArgs (Update) - args: [update, --json, e2e-t-upd, --title, "updated", --status, in_progress, --type, bug, --priority, "1", --description, "new", --assignee, bob, --set-metadata, a=1, --set-metadata, b=2, --add-label, x, --remove-label, "lane:z"] - compare: exit - verify: [[show, --json, e2e-t-upd]] - - id: gc-update-parent - source: gc bdstore.go bdUpdateArgs (--parent) - args: [update, --json, e2e-t-upd2, --parent, e2e-ep] - compare: exit - verify: [[show, --json, e2e-t-upd2]] - - id: gc-update-many - source: gc bdstore.go UpdateAll / setMetadataBatchAll - args: [update, --json, e2e-t-m1, e2e-t-m2, --set-metadata, gc.batch=1, --priority, "3"] - compare: exit - verify: [[show, --json, e2e-t-m1, e2e-t-m2]] - - id: gc-update-claim - source: gc bdstore.go BdStore.Claim and gc hook --claim (update --claim --json) - args: [update, e2e-t-claim, --claim, --json] - compare: exit - verify: [[show, --json, e2e-t-claim]] - - id: gc-update-claim-actor - source: gc cmd_agent_script.go bd_claim (update --claim --actor X) - args: [update, e2e-t-claim2, --claim, --actor, alice] - compare: exit - verify: [[show, --json, e2e-t-claim2]] - - id: gc-update-claim-wisp - source: gc ready queues include ephemeral work; claiming a wisp - args: [update, e2e-wisp-c, --claim, --json] - compare: exit - verify: [[show, --json, e2e-wisp-c]] - pending: {slice: "SRV-1", reason: "the reference server lacks UpdateIssueRequest.claim, and the claimIssue fallback excludes wisps (B3 #6); remove the mark when reference_server.json is upgraded"} - - id: gc-update-if-revision - source: gc bdstore_conditional.go UpdateIfMatch / CompareAndSetMetadataKey - args: [update, e2e-t-rev, --set-metadata, k=v, --if-revision, "{rev:e2e-t-rev}"] - compare: exit - verify: [[show, --json, e2e-t-rev]] - - id: gc-update-if-revision-stale - source: gc bdstore_conditional.go UpdateIfMatch mismatch (exit 13) - args: [update, e2e-t-rev2, --set-metadata, k=v, --if-revision, "1"] - expect_exit: 13 - compare: exit - verify: [[show, --json, e2e-t-rev2]] - - id: gc-update-release-if-current - source: gc bdstore_conditional_release.go releaseIfCurrentViaBdVerb - args: [update, e2e-t-rel, --if-assignee, worker, --if-status, in_progress, --status, open, --assignee, ""] - compare: exit - verify: [[show, --json, e2e-t-rel]] - - id: gc-update-transfer-if-current - source: gc bdstore_conditional_release.go TransferIfCurrent - args: [update, e2e-t-xfer, --if-assignee, worker, --if-status, in_progress, --assignee, alice] - compare: exit - verify: [[show, --json, e2e-t-xfer]] - - id: gc-update-if-assignment - source: gc bdstore_conditional.go UpdateIfAssignment - args: [update, --json, e2e-t-ifa, --set-metadata, k=v, --if-status, in_progress, --if-assignee, worker] - compare: exit - verify: [[show, --json, e2e-t-ifa]] - # e2e-t-ifa is seeded with metadata seeded=keep, so each refused (exit 13) - # update's verify still holds that the refusal left existing metadata - # alone, in whichever corpus part runs it (the parts seed separately, so - # the k=v written by gc-update-if-assignment may not be there). - - id: gc-update-if-assignment-assignee-stale - source: gc bdstore_conditional.go UpdateIfAssignment mismatch (exit 13) - args: [update, --json, e2e-t-ifa, --set-metadata, probe=a, --if-status, in_progress, --if-assignee, nobody] - expect_exit: 13 - compare: exit - verify: [[show, --json, e2e-t-ifa]] - - id: gc-update-if-assignment-status-stale - source: gc bdstore_conditional.go UpdateIfAssignment mismatch (exit 13) - args: [update, --json, e2e-t-ifa, --set-metadata, probe=s, --if-status, open, --if-assignee, worker] - expect_exit: 13 - compare: exit - verify: [[show, --json, e2e-t-ifa]] - - id: gc-update-on-death - source: gc workquery.go on_death (update --assignee "" --status open --set-metadata) - args: [update, e2e-t-death, --assignee, "", --status, open, --set-metadata, gc.run_target=pool-a] - compare: exit - verify: [[show, --json, e2e-t-death]] - - id: gc-update-on-boot - source: gc workquery.go on_boot (update --status open) - args: [update, e2e-t-boot, --status, open] - compare: exit - verify: [[show, --json, e2e-t-boot]] - - id: gc-update-sling - source: gc workquery.go DefaultSlingQuery (update --set-metadata gc.routed_to=) - args: [update, e2e-t-sling, --set-metadata, gc.routed_to=pool-b] - compare: exit - verify: [[show, --json, e2e-t-sling]] - - id: gc-update-agent-script - source: gc cmd_agent_script.go bd_update (--assignee --status --append-notes --set-metadata) - args: [update, e2e-t-as, --assignee, worker, --status, in_progress, --append-notes, "agent note", --set-metadata, k=v] - compare: exit - verify: [[show, --json, e2e-t-as]] - - id: gc-update-dolt-auto-commit-off - source: gc bdstore.go bdTransientWriteArgs (--dolt-auto-commit off prefix) - args: [--dolt-auto-commit, "off", update, --json, e2e-t-dac, --set-metadata, k=v] - compare: exit - verify: [[show, --json, e2e-t-dac]] - - id: mc-update-meta-status - source: mc prompts (update --set-metadata --status, ~151) - args: [update, e2e-t-ms, --set-metadata, review.verdict=iterate, --status, in_progress] - compare: exit - verify: [[show, --json, e2e-t-ms]] - - id: mc-update-meta-status-closed - source: mc prompts (update --set-metadata --status closed, under Claude Code) - args: [update, e2e-t-msc, --set-metadata, review.verdict=done, --status, closed] - compare: exit - verify: [[show, --json, e2e-t-msc]] - pending: {slice: "S6d", reason: "a silent data difference, not a refusal: the update succeeds on both sides under CLAUDE_SESSION_ID, but the served update records no closed_by_session (an http update patch cannot carry one, so the CLI drops it without a word; bd close does carry it) where embedded records the session, so the verify show differs (B3 #7)"} - - id: mc-update-unset-metadata - source: mc adopt-pr-operator (update --unset-metadata) - args: [update, e2e-t-um, --unset-metadata, k] - compare: exit - verify: [[show, --json, e2e-t-um]] - - id: mc-update-notes - source: mc github fix-issue formula (update --notes) - args: [update, e2e-t-notes, --notes, "notes text"] - compare: exit - verify: [[show, --json, e2e-t-notes]] - - id: mc-update-external-ref-metadata - source: mc snapshot.md (update --external-ref --metadata) - args: [update, e2e-t-er, --external-ref, gh-1, --metadata, '{"gc.kind":"github_source"}'] - compare: exit - verify: [[show, --json, e2e-t-er]] - - id: mc-update-metadata - source: mc build-base prepare.md (update --metadata) - args: [update, e2e-t-md, --metadata, '{"x":"1"}'] - compare: exit - verify: [[show, --json, e2e-t-md]] - - id: mc-update-assignee - source: mc review reports (update --assignee) - args: [update, e2e-t-asg, --assignee, bob] - compare: exit - verify: [[show, --json, e2e-t-asg]] - - # ---------------------------------------------------------------- close / reopen - - id: gc-close-force-reason-many - source: gc bdstore.go bdCloseArgs (CloseAll/CloseAllWithReason) - args: [close, --force, --json, --reason, done, e2e-t-c1, e2e-t-c2] - compare: exit - verify: [[show, --json, e2e-t-c1, e2e-t-c2]] - - id: gc-close-if-revision - source: gc bdstore_conditional.go CloseIfMatch - args: [close, --force, --json, e2e-t-c3, --if-revision, "{rev:e2e-t-c3}"] - compare: exit - verify: [[show, --json, e2e-t-c3]] - - id: mc-close-text - source: mc formulas (bd close ) - args: [close, e2e-t-c4] - compare: exit - verify: [[show, --json, e2e-t-c4]] - - id: mc-close-reason - source: mc build-basic-review (bd close --reason) - args: [close, e2e-t-c5, --reason, "review applied"] - compare: exit - verify: [[show, --json, e2e-t-c5]] - - id: gc-close-last-step-autocloses-molecule - source: gc/mc molecule lifecycle (closing the last open step closes the root) - args: [close, --force, --json, e2e-mac.2] - compare: exit - verify: [[show, --json, e2e-mac]] - - id: gc-reopen - source: gc bdstore.go Reopen - args: [reopen, --json, e2e-t-ro] - compare: exit - verify: [[show, --json, e2e-t-ro]] - - # ---------------------------------------------------------------- delete / reclaim / promote / burn - - id: gc-delete - source: gc bdstore.go Delete - args: [delete, --force, --json, e2e-t-d1] - compare: exit - verify: [[show, --json, e2e-t-d1]] - - id: gc-delete-if-revision - source: gc bdstore_conditional.go DeleteIfMatch - args: [delete, --force, --json, e2e-t-d2, --if-revision, "{rev:e2e-t-d2}"] - compare: exit - verify: [[show, --json, e2e-t-d2]] - - id: gc-delete-batch - source: gc bdstore.go DeleteBatch (no --json) - args: [delete, e2e-t-d3, e2e-t-d4, --force] - compare: exit - verify: [[show, --json, e2e-t-d3]] - - id: gc-delete-cascade - source: gc cmd_convoy_dispatch.go deleteWorkflowMatches - args: [delete, e2e-t-dc, --cascade, --force] - compare: exit - verify: [[show, --json, e2e-t-dc.1]] - - id: mc-delete-from-file - source: mc session_cleanup.py / core reaper (delete --from-file) - args: [delete, --from-file, "{file:delete-list.txt}", --force] - compare: exit - verify: [[show, --json, e2e-t-df1]] - - id: gc-reclaim - source: gc bdstore.go ReclaimStale (reclaim --id --json) - args: [reclaim, --id, e2e-t-rc, --json] - compare: exit - verify: [[show, --json, e2e-t-rc]] - pending: {slice: "SRV-1", reason: "the reference server does not advertise issues.reclaim (B3 #12), so the client refuses at its capability preflight"} - - id: gc-heartbeat - source: gc formulas/mol-do-work.toml:47 (gc bd heartbeat -> bd heartbeat) - setup: [[update, e2e-t-hb, --claim]] - args: [heartbeat, e2e-t-hb, --json] - verify: [[show, --json, e2e-t-hb]] - pending: {slice: "reference-server-heartbeat", reason: "the reference server does not advertise issues.heartbeat, so the client refuses at its capability preflight; pinned passing by backend/http TestE2E_HeartbeatOverHTTP; remove the mark when reference_server.json is upgraded"} - - id: gc-heartbeat-non-holder - source: gc formulas/mol-do-work.toml:47 (a worker heartbeating a bead another actor holds) - setup: [[update, e2e-t-hb2, --claim, --actor, alice]] - args: [heartbeat, e2e-t-hb2] - expect_exit: 1 - expect_stderr: "(?i)claimed by|alice" - compare: exit - pending: {slice: "reference-server-heartbeat", reason: "the reference server does not advertise issues.heartbeat, so the client refuses at its capability preflight; pinned passing by backend/http TestE2E_HeartbeatOverHTTP; remove the mark when reference_server.json is upgraded"} - - id: gc-heartbeat-closed - source: gc formulas/mol-do-work.toml:47 (a worker heartbeating a bead that was closed under it) - setup: [[update, e2e-t-hb3, --claim], [close, e2e-t-hb3]] - args: [heartbeat, e2e-t-hb3] - expect_exit: 1 - expect_stderr: "(?i)status closed|is closed" - compare: exit - pending: {slice: "reference-server-heartbeat", reason: "the reference server does not advertise issues.heartbeat, so the client refuses at its capability preflight; pinned passing by backend/http TestE2E_HeartbeatOverHTTP; remove the mark when reference_server.json is upgraded"} - - id: gc-heartbeat-wisp - source: gc formulas/mol-do-work.toml:47 (a heartbeat on a wisp, which is never leased) - args: [heartbeat, e2e-wisp-2] - expect_exit: 1 - expect_stderr: "(?i)ephemeral" - compare: exit - pending: {slice: "reference-server-heartbeat", reason: "the reference server does not advertise issues.heartbeat, so the client refuses at its capability preflight; pinned passing by backend/http TestE2E_HeartbeatOverHTTP; remove the mark when reference_server.json is upgraded"} - - id: mc-promote - source: mc mol-normalized-issue-pr-v1 (promote --reason) - args: [promote, e2e-wisp-p, --reason, "keep it"] - compare: exit - pending: {slice: "SRV-1", reason: "the reference server does not advertise issues.update.persistence, so the client refuses at its capability preflight"} - - id: mc-mol-burn - source: mc review fixtures (mol burn --force) - args: [mol, burn, e2e-wisp-m, --force] - compare: exit - verify: [[show, --json, e2e-wisp-m.1]] - pending: {slice: "S17", reason: "mol burn needs RunInTransaction (B3 #14)"} - - # ---------------------------------------------------------------- deps / comments / labels / config writes - - id: gc-dep-add - source: gc bdstore.go DepAdd - args: [dep, add, e2e-t-da, e2e-t-db, --type, blocks] - compare: exit - verify: [[dep, list, e2e-t-da, --json]] - - id: gc-dep-remove - source: gc bdstore.go DepRemove - args: [dep, remove, e2e-t-dr, e2e-t-db] - compare: exit - verify: [[dep, list, e2e-t-dr, --json]] - - id: mc-comment - source: mc bug_hunt.py (bd comment ) - args: [comment, e2e-t-cm, "a comment"] - compare: exit - verify: [[comments, e2e-t-cm, --json]] - ignore_fields: [id] # comment ids are minted per workspace - - id: mc-comments-list - source: mc prompts (bd comments ) - args: [comments, e2e-r1, --json] - ignore_fields: [id] # comment ids are minted per workspace - - id: mc-label-add - source: mc prompts (bd label add) - args: [label, add, e2e-t-l, "lane:q"] - compare: exit - verify: [[show, --json, e2e-t-l]] - - id: mc-assign - source: mc prompts (bd assign) - args: [assign, e2e-t-as2, alice] - compare: exit - verify: [[show, --json, e2e-t-as2]] - - id: gc-config-set-types - source: gc beads_provider_lifecycle.go (config set types.custom a,b) - args: [config, set, types.custom, "corpus-a,corpus-b"] - compare: exit - verify: [[config, get, --json, types.custom]] - - id: mc-meta-set - source: mc adopt-pr-review checks (bd meta set k=v) - args: [meta, set, e2e-r1, review.verdict=done] - expect_exit: 1 - expect_stderr: 'unknown command "meta"' - compare: exit - # ---------------------------------------------------------------- gates - # The gate writes: create (one createIssue carrying the gate members and the - # blocks edge), a real timer gate resolving under gate check, add-waiter (a - # guarded waiters patch) and update --await-id. A --timeout=1ns timer is - # expired by the time gate check runs, so the resolution is deterministic. - - id: mc-gate-create-timer - source: core formulas / gate-sweep (bd gate create --type=timer --timeout --blocks) - args: [gate, create, --type=timer, --timeout=1ns, --blocks, e2e-t-gcr, --json] - ignore_fields: [id] # each side mints its own gate id - pending: {slice: "SRV-1", reason: "bd gate create sends the gate members on createIssue behind issues.create.gates, which the reference server does not advertise, so the client refuses before dialing (pinned passing against the current server by backend/http TestE2E_GatesOverHTTP); remove the mark when reference_server.json is upgraded"} - - id: mc-gate-check-timer-resolves - source: core gate-sweep.sh (gate check --type=timer --escalate) over a real timer gate - setup: [[gate, create, --type=timer, --timeout=1ns, --blocks, e2e-t-gct]] - args: [gate, check, --type=timer, --escalate] - compare: exit - verify: [[ready, --json, --limit, "0"]] # e2e-t-gct is ready on both sides - ignore_fields: [depends_on_id] # its (closed) gate's id is minted per workspace - pending: {slice: "SRV-1", reason: "the setup bd gate create sends the gate members on createIssue behind issues.create.gates, which the reference server does not advertise, so the client refuses before dialing (pinned passing against the current server by backend/http TestE2E_GatesOverHTTP); remove the mark when reference_server.json is upgraded"} - - id: mc-gate-add-waiter - source: bd done --phase-complete (gate add-waiter) - args: [gate, add-waiter, e2e-t-gw, "rig/workers/a"] - compare: text - verify: [[show, --json, e2e-t-gw]] - pending: {slice: "SRV-1", reason: "bd gate add-waiter sends waiters on updateIssue behind issues.update.gates, which the reference server does not advertise, so the client refuses before dialing (pinned passing against the current server by backend/http TestE2E_GatesOverHTTP); remove the mark when reference_server.json is upgraded"} - - id: mc-update-await-id - source: gate discover / gate check gh:run persist (update --await-id) - args: [update, e2e-t-gaw, --await-id, "12345", --json] - compare: exit - verify: [[show, --json, e2e-t-gaw]] - pending: {slice: "SRV-1", reason: "bd update --await-id sends await_id on updateIssue behind issues.update.gates, which the reference server does not advertise, so the client refuses before dialing (pinned passing against the current server by backend/http TestE2E_GatesOverHTTP); remove the mark when reference_server.json is upgraded"} diff --git a/backend/http/corpus/testdata/reference_server.json b/backend/http/corpus/testdata/reference_server.json deleted file mode 100644 index 7bdf6d0d0..000000000 --- a/backend/http/corpus/testdata/reference_server.json +++ /dev/null @@ -1,595 +0,0 @@ -{ - "comment": [ - "The reference server profile the corpus suite masks the in-process server to.", - "It is the wire surface of a deployed bd 1.3.0 `bd serve` that predates wire_revision:", - "capabilities is that server's advertised ContextResponse.capabilities (read off a live GET /v0/beads/context),", - "context_members is its ContextResponse property set, and operations are the method, path template,", - "query parameter names and top-level JSON request members of its published OpenAPI document (MIT).", - "Regenerate by hand when the reference deployment is upgraded; see README.md." - ], - "capabilities": [ - "config.get", - "config.list", - "config.set", - "config.unset", - "dependencies.add", - "dependencies.blocking", - "dependencies.count", - "dependencies.cycles", - "dependencies.list", - "dependencies.remove", - "dependencies.tree", - "events.list", - "events.watch", - "issues.addComment", - "issues.batchApply", - "issues.batchClose", - "issues.batchCreate", - "issues.casMetadata", - "issues.claim", - "issues.claimNext", - "issues.close", - "issues.count", - "issues.create", - "issues.delete", - "issues.get", - "issues.list", - "issues.list.sort", - "issues.query", - "issues.related", - "issues.release", - "issues.reopen", - "issues.sweep", - "issues.update", - "memories.forget", - "memories.get", - "memories.list", - "memories.remember", - "policy.external_dependencies", - "project.enforce", - "ready.count", - "ready.list", - "stats.get" - ], - "context_members": [ - "api_version", - "backend", - "bd_version", - "beads_dir", - "capabilities", - "database", - "dolt_mode", - "project_id", - "repo_root", - "schema_version" - ], - "operations": [ - { - "method": "GET", - "path": "/healthz", - "operation_id": "health", - "query": [], - "body": [] - }, - { - "method": "GET", - "path": "/v0/beads/config", - "operation_id": "listSettings", - "query": [], - "body": [] - }, - { - "method": "DELETE", - "path": "/v0/beads/config/{key}", - "operation_id": "unsetSetting", - "query": [], - "body": [] - }, - { - "method": "GET", - "path": "/v0/beads/config/{key}", - "operation_id": "getSetting", - "query": [], - "body": [] - }, - { - "method": "PUT", - "path": "/v0/beads/config/{key}", - "operation_id": "setSetting", - "query": [], - "body": [ - "value" - ] - }, - { - "method": "GET", - "path": "/v0/beads/context", - "operation_id": "getContext", - "query": [], - "body": [] - }, - { - "method": "GET", - "path": "/v0/beads/dependencies", - "operation_id": "listDependencies", - "query": [ - "issue_id", - "type" - ], - "body": [] - }, - { - "method": "GET", - "path": "/v0/beads/dependencies/blocking", - "operation_id": "listBlockingAnnotations", - "query": [ - "issue_id" - ], - "body": [] - }, - { - "method": "GET", - "path": "/v0/beads/dependencies/cycles", - "operation_id": "listDependencyCycles", - "query": [], - "body": [] - }, - { - "method": "GET", - "path": "/v0/beads/dependencies/tree", - "operation_id": "getDependencyTree", - "query": [ - "direction", - "max_depth", - "root_id", - "status" - ], - "body": [] - }, - { - "method": "POST", - "path": "/v0/beads/dependencies:add", - "operation_id": "addDependencies", - "query": [], - "body": [ - "actor", - "edges" - ] - }, - { - "method": "GET", - "path": "/v0/beads/dependencies:count", - "operation_id": "countDependencyEdges", - "query": [ - "direction", - "issue_id", - "status", - "type" - ], - "body": [] - }, - { - "method": "POST", - "path": "/v0/beads/dependencies:remove", - "operation_id": "removeDependency", - "query": [], - "body": [ - "actor", - "depends_on_id", - "issue_id" - ] - }, - { - "method": "GET", - "path": "/v0/beads/events", - "operation_id": "listEvents", - "query": [ - "limit", - "since" - ], - "body": [] - }, - { - "method": "GET", - "path": "/v0/beads/events:watch", - "operation_id": "watchEvents", - "query": [ - "since" - ], - "body": [] - }, - { - "method": "GET", - "path": "/v0/beads/issues", - "operation_id": "listIssues", - "query": [ - "all", - "assignee", - "brief", - "created_after", - "created_before", - "cursor", - "exclude_label", - "has_metadata_key", - "include_ephemeral", - "include_gates", - "include_infra", - "include_templates", - "label", - "label_any", - "limit", - "metadata_field", - "parent", - "reverse", - "sort", - "status", - "type" - ], - "body": [] - }, - { - "method": "POST", - "path": "/v0/beads/issues", - "operation_id": "createIssue", - "query": [], - "body": [ - "acceptance_criteria", - "actor", - "assignee", - "defer_until", - "dependencies", - "description", - "design", - "due_at", - "ephemeral", - "estimated_minutes", - "external_ref", - "force_id_prefix", - "id", - "inherit_labels_from_parent", - "issue_type", - "labels", - "metadata", - "no_history", - "notes", - "owner", - "parent_id", - "priority", - "sender", - "status", - "title", - "waits_for" - ] - }, - { - "method": "GET", - "path": "/v0/beads/issues/{id}", - "operation_id": "getIssue", - "query": [ - "brief_deps", - "include_comments", - "include_dependents" - ], - "body": [] - }, - { - "method": "PATCH", - "path": "/v0/beads/issues/{id}", - "operation_id": "updateIssue", - "query": [], - "body": [ - "actor", - "expected_assignee", - "expected_status", - "expected_version", - "force_assignee_transfer", - "force_close_policy", - "patch" - ] - }, - { - "method": "POST", - "path": "/v0/beads/issues/{id}/comments", - "operation_id": "addComment", - "query": [], - "body": [ - "author", - "text" - ] - }, - { - "method": "GET", - "path": "/v0/beads/issues/{id}/related", - "operation_id": "listRelatedIssues", - "query": [ - "direction", - "type" - ], - "body": [] - }, - { - "method": "POST", - "path": "/v0/beads/issues/{id}:casMetadata", - "operation_id": "compareAndSetMetadata", - "query": [], - "body": [ - "actor", - "expected", - "key", - "value" - ] - }, - { - "method": "POST", - "path": "/v0/beads/issues/{id}:claim", - "operation_id": "claimIssue", - "query": [], - "body": [ - "actor" - ] - }, - { - "method": "POST", - "path": "/v0/beads/issues/{id}:close", - "operation_id": "closeIssue", - "query": [], - "body": [ - "actor", - "expected_version", - "force", - "reason", - "session" - ] - }, - { - "method": "POST", - "path": "/v0/beads/issues/{id}:release", - "operation_id": "releaseIssue", - "query": [], - "body": [ - "actor", - "expected_assignee", - "force" - ] - }, - { - "method": "POST", - "path": "/v0/beads/issues/{id}:reopen", - "operation_id": "reopenIssue", - "query": [], - "body": [ - "actor", - "expected_version", - "reason" - ] - }, - { - "method": "POST", - "path": "/v0/beads/issues:batchApply", - "operation_id": "applyBatch", - "query": [], - "body": [ - "actor", - "force_id_prefix", - "items", - "provenance", - "skip_per_edge_cycle_check" - ] - }, - { - "method": "POST", - "path": "/v0/beads/issues:batchClose", - "operation_id": "batchCloseIssues", - "query": [], - "body": [ - "actor", - "claim_next", - "force", - "items", - "session" - ] - }, - { - "method": "POST", - "path": "/v0/beads/issues:batchCreate", - "operation_id": "batchCreateIssues", - "query": [], - "body": [ - "actor", - "items" - ] - }, - { - "method": "POST", - "path": "/v0/beads/issues:claimNext", - "operation_id": "claimNextIssue", - "query": [ - "assignee", - "exclude_label", - "exclude_type", - "has_metadata_key", - "include_deferred", - "include_ephemeral", - "label", - "label_any", - "label_pattern", - "label_regex", - "metadata_field", - "parent", - "priority", - "sort", - "type", - "unassigned" - ], - "body": [ - "actor" - ] - }, - { - "method": "GET", - "path": "/v0/beads/issues:count", - "operation_id": "countIssues", - "query": [ - "assignee", - "closed_after", - "closed_before", - "created_after", - "created_before", - "desc_contains", - "empty_description", - "group_by", - "id", - "include_infra", - "label", - "label_any", - "metadata_field", - "no_assignee", - "no_labels", - "notes_contains", - "priority", - "priority_max", - "priority_min", - "status", - "title", - "title_contains", - "type", - "updated_after", - "updated_before" - ], - "body": [] - }, - { - "method": "POST", - "path": "/v0/beads/issues:delete", - "operation_id": "deleteIssues", - "query": [], - "body": [ - "actor", - "cascade", - "dry_run", - "expected_version", - "force", - "ids" - ] - }, - { - "method": "GET", - "path": "/v0/beads/issues:query", - "operation_id": "queryIssues", - "query": [ - "all", - "limit", - "q", - "reverse", - "sort" - ], - "body": [] - }, - { - "method": "POST", - "path": "/v0/beads/issues:sweep", - "operation_id": "sweepIssues", - "query": [], - "body": [ - "actor", - "closed_before", - "dry_run", - "pattern", - "protect_referenced", - "tier" - ] - }, - { - "method": "GET", - "path": "/v0/beads/memories", - "operation_id": "listMemories", - "query": [ - "search" - ], - "body": [] - }, - { - "method": "POST", - "path": "/v0/beads/memories", - "operation_id": "rememberMemory", - "query": [], - "body": [ - "content", - "key" - ] - }, - { - "method": "DELETE", - "path": "/v0/beads/memories/{key}", - "operation_id": "forgetMemory", - "query": [], - "body": [] - }, - { - "method": "GET", - "path": "/v0/beads/memories/{key}", - "operation_id": "getMemory", - "query": [], - "body": [] - }, - { - "method": "GET", - "path": "/v0/beads/ready", - "operation_id": "listReadyWork", - "query": [ - "assignee", - "brief", - "exclude_label", - "exclude_type", - "has_metadata_key", - "include_deferred", - "include_ephemeral", - "label", - "label_any", - "label_pattern", - "label_regex", - "limit", - "metadata_field", - "parent", - "priority", - "sort", - "type", - "unassigned" - ], - "body": [] - }, - { - "method": "GET", - "path": "/v0/beads/ready:count", - "operation_id": "countReadyWork", - "query": [ - "assignee", - "exclude_label", - "exclude_type", - "has_metadata_key", - "include_deferred", - "include_ephemeral", - "label", - "label_any", - "label_pattern", - "label_regex", - "metadata_field", - "parent", - "priority", - "type", - "unassigned" - ], - "body": [] - }, - { - "method": "GET", - "path": "/v0/beads/stats", - "operation_id": "getStats", - "query": [ - "assignee", - "skip_blocked" - ], - "body": [] - } - ] -} \ No newline at end of file diff --git a/backend/http/credential.go b/backend/http/credential.go deleted file mode 100644 index 3290fd3b7..000000000 --- a/backend/http/credential.go +++ /dev/null @@ -1,66 +0,0 @@ -package bdhttp - -import ( - "net/url" - - "github.com/steveyegge/beads/internal/httpclient" -) - -// CredentialProvider authorizes outbound requests to a bd serve. -// -// An implementation must be safe for concurrent use, must never log or -// persist a secret, and must FAIL CLOSED: a configured source that errors -// aborts the request rather than silently downgrading to an unauthenticated -// one. -// -// Authorize is called once per request, not once per attempt: a read whose -// first attempt stalls or has its connection reset is resent once with the -// same headers, up to the client's overall timeout later. A provider whose -// credentials are single-use or short-lived should expect that resend to draw -// a 401, which reaches Refresh like any other. -type CredentialProvider = httpclient.CredentialProvider - -const ( - // TokenEnv carries the bearer token itself, the highest rung of the - // built-in ladder. - // #nosec G101 -- the NAME of an environment variable, not a credential. - TokenEnv = httpclient.TokenEnv - // TokenCommandEnv names a helper that prints a token, either bare or in - // the kubectl ExecCredential envelope {"token","expirationTimestamp"}. - // #nosec G101 -- the NAME of an environment variable, not a credential. - TokenCommandEnv = httpclient.TokenCommandEnv -) - -// BearerProvider is the built-in credential ladder: TokenEnv, then -// TokenCommandEnv, then the credentials file's [host:port] section, then no -// credential at all (the tip OSS server's loopback-trust posture, a -// legitimate answer and not a failure). -type BearerProvider = httpclient.BearerProvider - -// NewBearerProvider builds the built-in ladder for a server. Open and -// Handshake already dial with it; it is exported so an embedder that wants to -// WRAP the default (decorate it for one target, delegate to it for the rest) -// does not have to reimplement it. -func NewBearerProvider(base *url.URL) *BearerProvider { - return httpclient.NewBearerProvider(base) -} - -// ProvidedCredential adapts a CredentialProvider into the opaque -// backends.Credential marker interface, so it can be carried through -// beads.OpenOptions / beads.OpenBestAvailableWith (and, directly, through -// backends.OpenOptions) to the registered "http" backend's OpenWith hook -// Register installs. -// -// A multi-tenant embedder (for example gc, Gas City: one process serving many -// workspaces, called "cities") that wants a distinct credential per workspace -// wraps that workspace's own provider in a ProvidedCredential when it opens -// through the registry (the direct door takes the provider itself, as -// Options.Credential): the registered dialer Register installs is -// process-wide and cannot hold one credential per tenant, but the per-open -// OpenWith seam can, and this is the opaque wrapper type that seam -// recognizes. Either way the supplied provider is the ONLY credential the -// open uses, and no ambient credential, CA or plaintext grant is read; see the -// package doc's "Credentials" section. See httpclient.ResolveCredential and -// backends.OpenOptions.Credential's own doc comment for the single-tenant -// ambient-ladder default this exists to opt out of. -type ProvidedCredential = httpclient.ProvidedCredential diff --git a/backend/http/e2e_blocked_flags_test.go b/backend/http/e2e_blocked_flags_test.go deleted file mode 100644 index 1943bd676..000000000 --- a/backend/http/e2e_blocked_flags_test.go +++ /dev/null @@ -1,287 +0,0 @@ -//go:build cgo - -package bdhttp_test - -import ( - "context" - "encoding/json" - "errors" - "os/exec" - "reflect" - "sort" - "strings" - "testing" - - "github.com/steveyegge/beads/internal/storage/issueops" -) - -// runBDStdin is runBD with stdin. -func runBDStdin(t *testing.T, f *remoteRolesFixture, stdin string, args ...string) bdResult { - t.Helper() - cmd := exec.Command(f.bin, args...) - cmd.Dir = f.dir - cmd.Env = minimalBDEnv(f.dir) - cmd.Stdin = strings.NewReader(stdin) - var o, e strings.Builder - cmd.Stdout, cmd.Stderr = &o, &e - err := cmd.Run() - code := 0 - var exitErr *exec.ExitError - if errors.As(err, &exitErr) { - code = exitErr.ExitCode() - } else if err != nil { - t.Fatalf("run bd %v: %v", args, err) - } - return bdResult{stdout: o.String(), stderr: e.String(), code: code} -} - -type blockedFlagRow struct { - ID string `json:"id"` - IsBlocked bool `json:"is_blocked"` -} - -func parseBlockedFlags(t *testing.T, out string) []blockedFlagRow { - t.Helper() - var rows []blockedFlagRow - if err := json.Unmarshal([]byte(strings.TrimSpace(out)), &rows); err != nil { - t.Fatalf("parse bd blocked --flags --json: %v\n%s", err, out) - } - if rows == nil { - t.Fatalf("bd blocked --flags --json printed null, want an array: %s", out) - } - return rows -} - -// flagStdin is every id the fixture created, in title order, plus an id that -// names no row, a blank line and a repeat: the omitted, ignored and collapsed -// cases. -func flagStdin(f *remoteRolesFixture) string { - titles := make([]string, 0, len(f.ids)) - for title := range f.ids { - titles = append(titles, title) - } - sort.Strings(titles) - var b strings.Builder - for _, title := range titles { - b.WriteString(f.ids[title] + "\n") - } - b.WriteString("\n " + f.ids[titles[0]] + " \ne2e-no-such-row\n") - return b.String() -} - -// TestE2E_BlockedFlagsOverHTTP runs `bd blocked --flags --json` (the blocked -// flag per id read from stdin, the readiness column gc projects) through the -// real `bd` binary against an in-process `bd serve` and against a local -// embedded workspace holding the same graph, and requires the same JSON once -// each generated id is mapped to its title. Over HTTP the read is -// listBlockingAnnotations with is_blocked; an older server's handshake is -// refused before the dial, naming dependencies.blocking.isBlocked. -// -// It also pins that this is the FLAG and not the listing: a row whose stored -// flag is set with no nameable blocker reads true here and is absent from -// `bd blocked --json`. -func TestE2E_BlockedFlagsOverHTTP(t *testing.T) { - skipUnlessEmbeddedDolt(t) - bin := buildBD(t) - - seed := func(f *remoteRolesFixture) { - f.seed() - blocker := f.create("blocker", "-t", "task") - gp := f.create("gate-parent", "-t", "epic") - kid := f.create("gate-kid", "-t", "task") - f.mustRun(nil, "dep", "add", kid, gp, "--type", "parent-child") - f.mustRun(nil, "dep", "add", gp, blocker) - } - - t.Run("served equals local", func(t *testing.T) { - addr := startE2EServer(t) - local := newEmbeddedFixture(t, bin) - seed(local) - served := newHTTPFixture(t, bin, addr) - seed(served) - - lr := runBDStdin(t, local, flagStdin(local), "blocked", "--flags", "--json") - sr := runBDStdin(t, served, flagStdin(served), "blocked", "--flags", "--json") - if lr.code != 0 || sr.code != 0 { - t.Fatalf("exit local=%d served=%d\nlocal: %s %s\nserved: %s %s", lr.code, sr.code, lr.stdout, lr.stderr, sr.stdout, sr.stderr) - } - normalize := func(f *remoteRolesFixture, rows []blockedFlagRow) []blockedFlagRow { - titleOf := map[string]string{} - for title, id := range f.ids { - titleOf[id] = title - } - out := make([]blockedFlagRow, 0, len(rows)) - for _, row := range rows { - if title, ok := titleOf[row.ID]; ok { - row.ID = title - } - out = append(out, row) - } - return out - } - want := normalize(local, parseBlockedFlags(t, lr.stdout)) - got := normalize(served, parseBlockedFlags(t, sr.stdout)) - if !reflect.DeepEqual(want, got) { - t.Errorf("served bd blocked --flags --json differs from the local command:\nlocal: %v\nserved: %v", want, got) - } - if len(want) != len(local.ids) { - t.Errorf("local answered %d rows for %d ids (the unknown id omitted, the repeat collapsed): %v", len(want), len(local.ids), want) - } - blocked := map[string]bool{} - for _, row := range got { - blocked[row.ID] = row.IsBlocked - } - for title, wantBlocked := range map[string]bool{ - "loner": true, "child-x": true, "gate-parent": true, "gate-kid": true, - "blocker": false, "child-b": false, "parent": false, - } { - if blocked[title] != wantBlocked { - t.Errorf("%s is_blocked = %v, want %v (rows %v)", title, blocked[title], wantBlocked, got) - } - } - // Byte-for-byte the same members in the same order. - if strings.Count(lr.stdout, "\n") != strings.Count(sr.stdout, "\n") { - t.Errorf("line counts differ:\nlocal:\n%s\nserved:\n%s", lr.stdout, sr.stdout) - } - // Nothing piped is an empty array. - if r := runBDStdin(t, served, "", "blocked", "--flags", "--json"); r.code != 0 || strings.TrimSpace(r.stdout) != "[]" { - t.Errorf("empty stdin: exit %d stdout %q, want [] and exit 0", r.code, r.stdout) - } - // The listing's filters are refused, not silently ignored. - if r := runBDStdin(t, served, "x\n", "blocked", "--flags", "--json", "--label", "a"); r.code != 1 { - t.Errorf("--flags --label exited %d, want 1: %s", r.code, r.stdout) - } - }) - - t.Run("the flag, not the listing", func(t *testing.T) { - addr, reference := startE2EServerWithStore(t, e2eProjectID, e2eDatabase) - served := newHTTPFixture(t, bin, addr) - stale := served.create("stale") - if err := reference.RunEventsMaintenanceTx(context.Background(), func(ctx context.Context, tx issueops.DBTX) error { - _, err := tx.ExecContext(ctx, "UPDATE issues SET is_blocked = 1 WHERE id = ?", stale) - return err - }); err != nil { - t.Fatalf("set the stale flag: %v", err) - } - rows := parseBlockedFlags(t, runBDStdin(t, served, stale+"\n", "blocked", "--flags", "--json").stdout) - if len(rows) != 1 || rows[0].ID != stale || !rows[0].IsBlocked { - t.Errorf("--flags for a stale-flagged row = %v, want [{%s true}]", rows, stale) - } - listing := served.mustRun(nil, "blocked", "--json").stdout - if strings.Contains(listing, stale) { - t.Errorf("bd blocked --json lists %s, which has no nameable blocker: %s", stale, listing) - } - }) - - t.Run("raw is the stored column", func(t *testing.T) { - addr, reference := startE2EServerWithStore(t, e2eProjectID, e2eDatabase) - local := newEmbeddedFixture(t, bin) - seed(local) - served := newHTTPFixture(t, bin, addr) - seed(served) - - flagsOf := func(f *remoteRolesFixture, stdin string, args ...string) map[string]bool { - t.Helper() - r := runBDStdin(t, f, stdin, append([]string{"blocked", "--flags", "--json"}, args...)...) - if r.code != 0 { - t.Fatalf("bd blocked --flags --json %v exited %d: %s %s", args, r.code, r.stdout, r.stderr) - } - titleOf := map[string]string{} - for title, id := range f.ids { - titleOf[id] = title - } - out := map[string]bool{} - for _, row := range parseBlockedFlags(t, r.stdout) { - key := row.ID - if title, ok := titleOf[row.ID]; ok { - key = title - } - out[key] = row.IsBlocked - } - return out - } - - localRaw := flagsOf(local, flagStdin(local), "--raw") - servedRaw := flagsOf(served, flagStdin(served), "--raw") - if !reflect.DeepEqual(localRaw, servedRaw) { - t.Errorf("served --raw differs from local --raw:\nlocal: %v\nserved: %v", localRaw, servedRaw) - } - lr := runBDStdin(t, local, flagStdin(local), "blocked", "--flags", "--json", "--raw") - sr := runBDStdin(t, served, flagStdin(served), "blocked", "--flags", "--json", "--raw") - if strings.Count(lr.stdout, "\n") != strings.Count(sr.stdout, "\n") { - t.Errorf("--raw line counts differ:\nlocal:\n%s\nserved:\n%s", lr.stdout, sr.stdout) - } - - // child-x is blocked ONLY by an unsatisfied external ref: the policy - // flag says blocked, the stored column does not. - decorated := flagsOf(served, flagStdin(served)) - if !decorated["child-x"] || servedRaw["child-x"] || localRaw["child-x"] { - t.Errorf("child-x (externally blocked only): --flags %v, --raw served %v local %v; want true, false, false", - decorated["child-x"], servedRaw["child-x"], localRaw["child-x"]) - } - // Locally blocked rows read the same either way. - for _, title := range []string{"loner", "gate-parent", "gate-kid", "blocker"} { - if decorated[title] != servedRaw[title] { - t.Errorf("%s: --flags %v, --raw %v; a local blocker is the column", title, decorated[title], servedRaw[title]) - } - } - - // A stale flag (set, no blocker) is the column, so both read true. - stale := served.create("stale") - if err := reference.RunEventsMaintenanceTx(context.Background(), func(ctx context.Context, tx issueops.DBTX) error { - _, err := tx.ExecContext(ctx, "UPDATE issues SET is_blocked = 1 WHERE id = ?", stale) - return err - }); err != nil { - t.Fatalf("set the stale flag: %v", err) - } - if got := flagsOf(served, stale+"\n", "--raw"); !got["stale"] { - t.Errorf("--raw for the stale-flagged row = %v, want true", got) - } - if got := flagsOf(served, stale+"\n"); !got["stale"] { - t.Errorf("--flags for the stale-flagged row = %v, want true", got) - } - - // --raw qualifies --flags only. - if r := runBDStdin(t, served, "", "blocked", "--json", "--raw"); r.code != 1 || !strings.Contains(r.stdout+r.stderr, "--raw") { - t.Errorf("bd blocked --raw without --flags: exit %d %s %s, want exit 1 naming --raw", r.code, r.stdout, r.stderr) - } - - // A server that applies the policy answers the decorated flag and the - // wire cannot ask it for the column: --raw is refused there, before - // the annotation read, rather than answered with the policy flag. - proxyAddr, counter := startMaskingProxy(t, addr, func(body map[string]any) { - caps, _ := body["capabilities"].([]any) - body["capabilities"] = append(caps, "policy.external_dependencies") - }) - enforced := newHTTPFixture(t, bin, proxyAddr) - counter.reset() - r := runBDStdin(t, enforced, served.ids["child-x"]+"\n", "blocked", "--flags", "--json", "--raw") - if r.code != 1 || !strings.Contains(r.stdout+r.stderr, "policy.external_dependencies") { - t.Errorf("--raw against a policy server: exit %d %s %s, want exit 1 naming the policy token", r.code, r.stdout, r.stderr) - } - for _, p := range counter.snapshot() { - if strings.Contains(p, "/v0/beads/dependencies/blocking") { - t.Errorf("a refused --raw dialed %s", p) - } - } - }) - - t.Run("older server mask", func(t *testing.T) { - addr := startE2EServer(t) - proxyAddr, counter := startMaskingProxy(t, addr, mcContextMask) - f := newHTTPFixture(t, bin, proxyAddr) - counter.reset() - r := runBDStdin(t, f, "e2e-anything\n", "blocked", "--flags", "--json") - if r.code != 1 { - t.Fatalf("bd blocked --flags against a server without the token exited %d, want 1: %s", r.code, r.stdout) - } - if !strings.Contains(r.stdout+r.stderr, "dependencies.blocking.isBlocked") { - t.Errorf("refusal does not name the missing capability: stdout=%s stderr=%s", r.stdout, r.stderr) - } - for _, p := range counter.snapshot() { - if strings.Contains(p, "/v0/beads/dependencies/blocking") { - t.Errorf("a refused bd blocked --flags dialed %s", p) - } - } - }) -} diff --git a/backend/http/e2e_blocked_test.go b/backend/http/e2e_blocked_test.go deleted file mode 100644 index 786379fba..000000000 --- a/backend/http/e2e_blocked_test.go +++ /dev/null @@ -1,229 +0,0 @@ -//go:build cgo - -package bdhttp_test - -import ( - "encoding/json" - "reflect" - "slices" - "strings" - "testing" -) - -// TestE2E_BlockedOverHTTP runs `bd blocked --json` (the blocked door gc's -// readiness projection reads) through the real `bd` binary against an -// in-process `bd serve`, and against a local embedded workspace holding the -// same graph. -// -// Over HTTP the CLI takes the store's BlockedLister, which dials -// GET /v0/beads/blocked behind blocked.list. The two answers must be the same -// JSON once the values that cannot agree between two workspaces (each row's -// generated id, and its timestamps) are mapped out: the same rows, the same -// members, the same blockers (local, parent-attributed and external), in the -// same order. Against a server that does not advertise blocked.list (an older -// server's handshake) the client refuses before the dial, naming the token. -func TestE2E_BlockedOverHTTP(t *testing.T) { - skipUnlessEmbeddedDolt(t) - bin := buildBD(t) - addr := startE2EServer(t) - - seedBlocked := func(f *remoteRolesFixture) { - f.seed() - // A plain blocks edge, a blocked parent's child attributed to the - // parent, and a labelled blocked row for the filter arms. - blocker := f.create("blocker", "-t", "task", "-p", "1") - gp := f.create("gate-parent", "-t", "epic", "-p", "2") - // Its own priority: rows tied on priority and created_at fall to the - // id order, and ids are generated per workspace, so a tie would order - // the two workspaces differently. - kid := f.create("gate-kid", "-t", "task", "-p", "3") - tagged := f.create("tagged", "-t", "task", "-p", "1", "-l", "lane:a") - // Blocked by nothing but an unsatisfied external ref, as child-x is, - // and labelled: the decorator lists such rows itself, after the label - // query ran, so the filter arms must judge them too. - extTagged := f.create("ext-tagged", "-t", "task", "-p", "4", "-l", "lane:a") - f.mustRun(nil, "dep", "add", kid, gp, "--type", "parent-child") - f.mustRun(nil, "dep", "add", gp, blocker) - f.mustRun(nil, "dep", "add", tagged, blocker) - f.mustRun(nil, "dep", "add", extTagged, "external:other:cap") - } - - local := newEmbeddedFixture(t, bin) - seedBlocked(local) - served := newHTTPFixture(t, bin, addr) - seedBlocked(served) - - for _, args := range [][]string{ - {"blocked", "--json"}, - {"blocked", "--json", "--label", "lane:a"}, - {"blocked", "--json", "--label-any", "lane:a"}, - {"blocked", "--json", "--exclude-label", "lane:a"}, - {"blocked", "--json", "--parent", "{gate-parent}"}, - {"blocked", "--json", "--label", "no-such-label"}, - } { - t.Run(strings.Join(args[1:], " "), func(t *testing.T) { - want := local.mustRun(nil, local.expand(args)...).stdout - got := served.mustRun(nil, served.expand(args)...).stdout - if strings.TrimSpace(want) == "null" || strings.TrimSpace(got) == "null" { - t.Fatalf("bd blocked --json printed null: local=%s served=%s", want, got) - } - if w, g := normalizeBlocked(t, local, want), normalizeBlocked(t, served, got); !reflect.DeepEqual(w, g) { - t.Errorf("served bd blocked --json differs from the local command:\nlocal: %v\nserved: %v", w, g) - } - if w, g := blockedKeyOrder(t, want), blockedKeyOrder(t, got); !reflect.DeepEqual(w, g) { - t.Errorf("served member order %v differs from the local command's %v", g, w) - } - }) - } - - t.Run("names the external blocker", func(t *testing.T) { - got := normalizeBlocked(t, served, served.mustRun(nil, "blocked", "--json").stdout) - found := false - for _, row := range got { - if row["id"] == "child-x" { - found = reflect.DeepEqual(row["blocked_by"], []any{"external:other:cap"}) - } - } - if !found { - t.Errorf("served bd blocked --json does not list child-x blocked by its external ref: %v", got) - } - }) - - // The arms above compare the two answers, which agree just as well when - // both list a row the filter rules out. These pin which rows each filter - // leaves, the external-only ones (child-x unlabelled, ext-tagged - // labelled) among them. - for _, tc := range []struct { - args []string - want []string // the whole listing, when set - listed, ruled []string - }{ - {args: []string{"--label", "lane:a"}, want: []string{"ext-tagged", "tagged"}}, - {args: []string{"--label-any", "lane:a"}, want: []string{"ext-tagged", "tagged"}}, - {args: []string{"--label", "no-such-label"}, want: []string{}}, - {args: []string{"--exclude-label", "lane:a"}, listed: []string{"child-x"}, ruled: []string{"ext-tagged", "tagged"}}, - } { - t.Run("filters external-only rows "+strings.Join(tc.args, " "), func(t *testing.T) { - for _, f := range []struct { - name string - f *remoteRolesFixture - }{{"local", local}, {"served", served}} { - got := f.f.titlesOf(f.f.mustRun(nil, append([]string{"blocked", "--json"}, tc.args...)...).stdout) - if tc.want != nil && !slices.Equal(got, tc.want) { - t.Errorf("%s bd blocked %s listed %v, want %v", f.name, strings.Join(tc.args, " "), got, tc.want) - } - for _, title := range tc.listed { - if !slices.Contains(got, title) { - t.Errorf("%s bd blocked %s does not list %s: %v", f.name, strings.Join(tc.args, " "), title, got) - } - } - for _, title := range tc.ruled { - if slices.Contains(got, title) { - t.Errorf("%s bd blocked %s lists %s, which the filter rules out: %v", f.name, strings.Join(tc.args, " "), title, got) - } - } - } - }) - } - - t.Run("older server mask", func(t *testing.T) { - proxyAddr, counter := startMaskingProxy(t, addr, mcContextMask) - f := newHTTPFixture(t, bin, proxyAddr) - counter.reset() - r := f.run(nil, "blocked", "--json") - if r.code != 1 { - t.Fatalf("bd blocked against a server without blocked.list exited %d, want 1: %s", r.code, r.stdout) - } - if !strings.Contains(r.stdout+r.stderr, "blocked.list") { - t.Errorf("refusal does not name the missing capability: stdout=%s stderr=%s", r.stdout, r.stderr) - } - for _, p := range counter.snapshot() { - if strings.Contains(p, "/v0/beads/blocked") { - t.Errorf("a refused bd blocked dialed %s; the refusal must come before the dial", p) - } - } - }) -} - -// normalizeBlocked parses `bd blocked --json` and maps out what two -// workspaces cannot share: each id (and each blocker id) becomes the title the -// fixture created it under, and every timestamp member is blanked. -func normalizeBlocked(t *testing.T, f *remoteRolesFixture, out string) []map[string]any { - t.Helper() - var rows []map[string]any - if err := json.Unmarshal([]byte(strings.TrimSpace(out)), &rows); err != nil { - t.Fatalf("parse bd blocked --json: %v\n%s", err, out) - } - titleOf := map[string]string{} - for title, id := range f.ids { - titleOf[id] = title - } - var walk func(key string, v any) any - walk = func(key string, v any) any { - switch v := v.(type) { - case map[string]any: - for k, inner := range v { - v[k] = walk(k, inner) - } - return v - case []any: - for i := range v { - v[i] = walk("", v[i]) - } - return v - case string: - if strings.HasSuffix(key, "_at") { - return "
intermediates (ignored/0001, ignored/0002). The -// final names are dolt_ignored clone-local tables, but the intermediates -// carried no ignore pattern, so against a @@dolt_transaction_commit=1 server -// every CREATE __temp__X auto-committed a REAL table at HEAD; the later -// RENAME to an ignored name then left rename halves in dolt_status that no -// working-set shuffle could stage (content-inferred rename classification), -// wedging every older bd behind the dirty-table guard until a manual repair. -// -// The store under test is shaped like the field case: a clone whose -// dolt_ignore was seeded by binaries that predate the __temp__% pattern. The -// fresh-box MigrateUp must assert the pattern itself, in the same pass, -// before the ignored series runs — so no __temp__ intermediate ever lands in -// committed history and the pass ends with a clean working set. -func TestEmbeddedTempRebuildIntermediatesNeverReachHistory(t *testing.T) { - requireEmbedded(t) - ctx := t.Context() - - dir := seedMainSchemaAt(t, ctx, schema.LatestVersion()) - conn, closeConn := openPinnedConn(t, ctx, dir) - defer closeConn() - if _, err := schema.MigrateUp(ctx, conn); err != nil { - t.Fatalf("seed MigrateUp: %v", err) - } - - // Shape the store like a real clone (the Door B dance from - // TestEmbeddedIgnoredSeriesConvergesWithFreshInitShape): committed history - // carries the main tables and the at-latest main cursor; the clone-local - // tables and the ignored cursor are working-set state a clone never - // receives. - for _, table := range []string{ - "wisp_dependencies", "wisp_events", "wisp_comments", "wisp_labels", - "wisp_child_counters", "wisps", "events", "leases", "repo_mtimes", - "local_metadata", "bd_events_journal", "bd_events_seq", - "ignored_schema_migrations", - } { - execFrozenGuard(t, ctx, conn, "DROP TABLE IF EXISTS "+table) - } - execFrozenGuard(t, ctx, conn, - "DELETE FROM dolt_ignore WHERE pattern IN ('leases', 'bd_events_journal', 'bd_events_seq')") - // The field stores were seeded by binaries that predate the __temp__% - // pattern; strip it from the baseline so the pass under test has to - // assert it itself, before the ignored series runs. - execFrozenGuard(t, ctx, conn, "DELETE FROM dolt_ignore WHERE pattern = '__temp__%'") - mustDrain(t, ctx, conn, "CALL DOLT_ADD('-A')") - mustDrain(t, ctx, conn, "CALL DOLT_COMMIT('-m', 'test: clone-shaped baseline', '--skip-empty')") - - // The hub that hit this twice runs @@dolt_transaction_commit=1: every - // transaction commit becomes a dolt commit of every non-ignored delta, - // which is the mechanism that materialized the intermediates. - if _, err := conn.ExecContext(ctx, "SET @@dolt_transaction_commit = 1"); err != nil { - t.Fatalf("enable dolt_transaction_commit: %v", err) - } - - // The fresh-box open. - if _, err := schema.MigrateUp(ctx, conn); err != nil { - t.Fatalf("fresh-box MigrateUp: %v", err) - } - - // No rebuild intermediate may exist in ANY commit. - rows, err := conn.QueryContext(ctx, "SELECT DISTINCT table_name FROM dolt_diff") - if err != nil { - t.Fatalf("read dolt_diff table names: %v", err) - } - defer rows.Close() - var leaked []string - for rows.Next() { - var name string - if err := rows.Scan(&name); err != nil { - t.Fatalf("scan dolt_diff table name: %v", err) - } - if strings.HasPrefix(name, "__temp__") { - leaked = append(leaked, name) - } - } - if err := rows.Err(); err != nil { - t.Fatalf("iterate dolt_diff table names: %v", err) - } - if len(leaked) > 0 { - t.Errorf("rebuild intermediates reached committed history: %v", leaked) - } - - // And the pass must end clean — the incident's other half was the - // permanently-dirty status that wedged older binaries' opens. - dirty, err := statusTables(ctx, conn) - if err != nil { - t.Fatalf("read dolt_status: %v", err) - } - if len(dirty) > 0 { - t.Errorf("working set dirty after the fresh-box pass: %v", dirty) - } -} diff --git a/internal/storage/embeddeddolt/molecule_contract_test.go b/internal/storage/embeddeddolt/molecule_contract_test.go deleted file mode 100644 index a268b890b..000000000 --- a/internal/storage/embeddeddolt/molecule_contract_test.go +++ /dev/null @@ -1,63 +0,0 @@ -//go:build cgo - -package embeddeddolt_test - -import ( - "context" - "testing" - - "github.com/steveyegge/beads/backend/conformance" -) - -// TestMoleculeContract runs the molecule contract against the embedded store. -func TestMoleculeContract(t *testing.T) { - skipUnlessEmbeddedDolt(t) - te := newTestEnv(t, "mol") - ctx := t.Context() - fixture := newEmbeddedMoleculeFixture(t, te, "mol") - - for _, test := range []struct { - name string - run func(*testing.T, context.Context, conformance.MoleculeFixture) - }{ - {"CloseAutoClosesTheCompletedRoot", conformance.RunMoleculeCloseAutoClosesTheCompletedRoot}, - {"AutoCloseIsOptInAndRuleBound", conformance.RunMoleculeAutoCloseIsOptInAndRuleBound}, - {"RecloseHealsAStrandedRoot", conformance.RunMoleculeRecloseHealsAStrandedRoot}, - {"BatchCloseAutoClosesTheRootOnce", conformance.RunMoleculeBatchCloseAutoClosesTheRootOnce}, - {"RefusedRootKeepsTheStepClose", conformance.RunMoleculeRefusedRootKeepsTheStepClose}, - {"GuardedRootKeepsTheStepClose", conformance.RunMoleculeGuardedRootKeepsTheStepClose}, - {"StepperClaimsTheNextReadyStepForTheActor", conformance.RunMoleculeStepperClaimsTheNextReadyStepForTheActor}, - {"StepperNeverTakesAStepAnotherActorHolds", conformance.RunMoleculeStepperNeverTakesAStepAnotherActorHolds}, - {"StepperAnswersCompleteAndOutsideAMolecule", conformance.RunMoleculeStepperAnswersCompleteAndOutsideAMolecule}, - {"StepperClaimsAWispStep", conformance.RunMoleculeStepperClaimsAWispStep}, - } { - t.Run(test.name, func(t *testing.T) { test.run(t, ctx, fixture) }) - } -} - -func newEmbeddedMoleculeFixture(t *testing.T, te *testEnv, prefix string) conformance.MoleculeFixture { - t.Helper() - lifecycle, err := te.store.IssueLifecycle() - if err != nil { - t.Fatalf("IssueLifecycle(): %v", err) - } - closer, err := te.store.BatchCloser() - if err != nil { - t.Fatalf("BatchCloser(): %v", err) - } - stepper, err := te.store.MoleculeStepper() - if err != nil { - t.Fatalf("MoleculeStepper(): %v", err) - } - kit := newEmbeddedRoleFixtureKit(te, prefix) - return conformance.MoleculeFixture{ - IssuePrefix: kit.IssuePrefix, - Lifecycle: lifecycle, - BatchCloser: closer, - Stepper: stepper, - CreateIssue: kit.CreateIssue, - CreateWisp: kit.CreateWisp, - AddDependency: kit.AddDependency, - QueryScalar: kit.QueryScalar, - } -} diff --git a/internal/storage/embeddeddolt/molecule_stepper.go b/internal/storage/embeddeddolt/molecule_stepper.go deleted file mode 100644 index ba81d7f5e..000000000 --- a/internal/storage/embeddeddolt/molecule_stepper.go +++ /dev/null @@ -1,19 +0,0 @@ -//go:build cgo - -package embeddeddolt - -import ( - "github.com/steveyegge/beads/issueops" -) - -// MoleculeStepper returns the advance-a-molecule surface for this store: -// issueops.NewMoleculeStepper composed over this store's own BatchGetter, -// Relations, EdgeReader, Claimer and Lifecycle, so the advance reads and -// claims exactly as those roles do and no second copy of the rule exists. -func (s *EmbeddedDoltStore) MoleculeStepper() (issueops.MoleculeStepper, error) { - roles, err := issueops.MoleculeStepperRolesOf(s) - if err != nil { - return nil, err - } - return issueops.NewMoleculeStepper(roles), nil -} diff --git a/internal/storage/embeddeddolt/push_commit_pending_test.go b/internal/storage/embeddeddolt/push_commit_pending_test.go deleted file mode 100644 index 56364d40f..000000000 --- a/internal/storage/embeddeddolt/push_commit_pending_test.go +++ /dev/null @@ -1,131 +0,0 @@ -//go:build cgo - -package embeddeddolt - -import ( - "errors" - "strings" - "testing" - - "github.com/steveyegge/beads/internal/types" -) - -func wantStopRemote(t *testing.T, err error) { - t.Helper() - if !errors.Is(err, errStopRemote) { - t.Fatalf("error = %v, want errStopRemote (stopped before the remote entry point)", err) - } -} - -func wantRemoteNotFound(t *testing.T, err error) { - t.Helper() - if err == nil || !strings.Contains(err.Error(), "remote") { - t.Fatalf("error = %v, want a remote-not-found failure (no real remote is configured in this test store)", err) - } -} - -// TestPushCommitsPendingChangesFirst is a regression test for #5433: every -// Pull variant auto-commits pending changes before pulling (GH#2474), but no -// Push variant did, so a write that landed in the working set but was never -// explicitly committed (auto-commit off, or a session that ended right after -// a write with no intervening commit) silently pushed nothing new while -// still reporting success. Push/ForcePush/PushRemote/PushTo now call -// CommitPending first, matching Pull's own pattern. -// -// Uses captureRemoteEntryPoints (remote_peer_auth_test.go) to stop each verb -// at its remote entry point with errStopRemote, keeping the test off the -// network while still exercising the real CommitPending call that runs -// before it. -func TestPushCommitsPendingChangesFirst(t *testing.T) { - ctx := t.Context() - - cases := []struct { - name string - call func(*EmbeddedDoltStore) error - // wantErr checks the error from call. Push/ForcePush/PushRemote route - // through the swappable vcPush/vcForcePush vars that - // captureRemoteEntryPoints intercepts with errStopRemote before any - // network IO. PushTo calls versioncontrolops.Push directly (no - // swappable seam for it), so it actually attempts the push and fails - // on "remote not found" -- still proves CommitPending ran first, - // since that failure only happens after this fix's guard clause. - wantErr func(t *testing.T, err error) - // clean runs the verb with nothing pending instead of creating an issue - // first. The guard must tolerate an empty working set -- CommitPending - // -> commitAll(tolerateEmpty=true) maps Dolt's "nothing to commit" to - // (false, nil) -- and still reach the remote entry point with HEAD - // unmoved. That is the common case for a push against an - // already-committed store, and nothing else in this package covers it. - clean bool - }{ - {name: "Push", call: func(s *EmbeddedDoltStore) error { return s.Push(ctx) }, wantErr: wantStopRemote}, - {name: "ForcePush", call: func(s *EmbeddedDoltStore) error { return s.ForcePush(ctx) }, wantErr: wantStopRemote}, - {name: "PushRemote", call: func(s *EmbeddedDoltStore) error { return s.PushRemote(ctx, defaultRemote, false) }, wantErr: wantStopRemote}, - {name: "PushTo", call: func(s *EmbeddedDoltStore) error { return s.PushTo(ctx, defaultRemote) }, wantErr: wantRemoteNotFound}, - {name: "PushCleanStore", call: func(s *EmbeddedDoltStore) error { return s.Push(ctx) }, wantErr: wantStopRemote, clean: true}, - } - - for _, tc := range cases { - t.Run(tc.name, func(t *testing.T) { - store := newPeerAuthTestStore(t) - - // newPeerAuthTestStore opens a bare store with no issue_prefix - // configured yet; give it one and commit so the fixture itself - // isn't the pending change this test is trying to isolate. - if err := store.SetConfig(ctx, "issue_prefix", "fedauth"); err != nil { - t.Fatalf("SetConfig(issue_prefix): %v", err) - } - if err := store.Commit(ctx, "bd init"); err != nil { - t.Fatalf("Commit(bd init): %v", err) - } - - var got presentedAuth - captureRemoteEntryPoints(t, &got) - - beforeCreate, err := store.GetCurrentCommit(ctx) - if err != nil { - t.Fatalf("GetCurrentCommit (before create): %v", err) - } - - beforePush := beforeCreate - - if !tc.clean { - issue := &types.Issue{ - ID: "fedauth-" + tc.name, - Title: "pending change", - Status: types.StatusOpen, - Priority: 2, - IssueType: types.TypeTask, - } - if err := store.CreateIssue(ctx, issue, "tester"); err != nil { - t.Fatalf("CreateIssue: %v", err) - } - - afterCreate, err := store.GetCurrentCommit(ctx) - if err != nil { - t.Fatalf("GetCurrentCommit (after create): %v", err) - } - if afterCreate != beforeCreate { - t.Fatalf("test invariant broken: CreateIssue committed on its own (HEAD moved %s -> %s) -- this test needs a genuinely pending change to be meaningful", beforeCreate, afterCreate) - } - beforePush = afterCreate - } - - tc.wantErr(t, tc.call(store)) - - afterPush, err := store.GetCurrentCommit(ctx) - if err != nil { - t.Fatalf("GetCurrentCommit (after %s): %v", tc.name, err) - } - if tc.clean { - if afterPush != beforePush { - t.Fatalf("%s on a clean store must not create a commit: HEAD moved %s -> %s (the guard's tolerateEmpty tolerance is broken)", tc.name, beforePush, afterPush) - } - return - } - if afterPush == beforePush { - t.Fatalf("%s must commit pending changes before pushing (#5433): HEAD did not move (%s), the created issue is still uncommitted", tc.name, afterPush) - } - }) - } -} diff --git a/internal/storage/embeddeddolt/queries.go b/internal/storage/embeddeddolt/queries.go index a0282d2a3..16271fce2 100644 --- a/internal/storage/embeddeddolt/queries.go +++ b/internal/storage/embeddeddolt/queries.go @@ -9,18 +9,10 @@ import ( "fmt" "os" - "github.com/steveyegge/beads/internal/storage" "github.com/steveyegge/beads/internal/storage/issueops" "github.com/steveyegge/beads/internal/types" ) -// WakeExpiredDefersAdvisory preserves the backend wake contract for decorators. -func (s *EmbeddedDoltStore) WakeExpiredDefersAdvisory(ctx context.Context) { - s.wakeExpiredDefers(ctx) -} - -var _ storage.ExpiredDeferWaker = (*EmbeddedDoltStore)(nil) - // wakeExpiredDefers runs the lazy defer-wake sweep (issueops.WakeExpiredDefersInTx) // in its own write transaction before a ready-work read. Advisory by contract: // a ready listing must never fail because the sweep could not run — strict @@ -70,21 +62,6 @@ func (s *EmbeddedDoltStore) GetReadyWorkWithCounts(ctx context.Context, filter t return result, err } -// GetReadyWorkWithCountsAndTotal returns the ready page and the size of the -// whole ready set in one read transaction (and one defer-wake sweep), so a -// capped `bd ready` needs no separate count pass. -func (s *EmbeddedDoltStore) GetReadyWorkWithCountsAndTotal(ctx context.Context, filter types.WorkFilter) ([]*types.IssueWithCounts, int, error) { - s.wakeExpiredDefers(ctx) - var result []*types.IssueWithCounts - var total int - err := s.withConn(ctx, false, func(tx *sql.Tx) error { - var err error - result, total, err = issueops.GetReadyWorkWithCountsAndTotalInTx(ctx, tx, filter) - return err - }) - return result, total, err -} - // CountReadyWork returns the total ready-work count for filter. It is identical // to len(GetReadyWorkWithCounts(filter with Limit=0)) but sizes the total with // cheap indexed COUNT(*)s instead of re-running the counts mega-query. Backs the diff --git a/internal/storage/embeddeddolt/race_detector_off_test.go b/internal/storage/embeddeddolt/race_detector_off_test.go deleted file mode 100644 index 89a81fc8c..000000000 --- a/internal/storage/embeddeddolt/race_detector_off_test.go +++ /dev/null @@ -1,6 +0,0 @@ -//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 deleted file mode 100644 index e3040e9eb..000000000 --- a/internal/storage/embeddeddolt/race_detector_on_test.go +++ /dev/null @@ -1,23 +0,0 @@ -//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/embeddeddolt/reader_contract_test.go b/internal/storage/embeddeddolt/reader_contract_test.go index 3e9286096..7d792ad2c 100644 --- a/internal/storage/embeddeddolt/reader_contract_test.go +++ b/internal/storage/embeddeddolt/reader_contract_test.go @@ -133,12 +133,6 @@ func TestEmbeddedReaderListEmptyPageIsWellFormed(t *testing.T) { conformance.RunReaderListEmptyPageIsWellFormed(t, ctx, newEmbeddedReaderFixture(t, "rdr")) } -func TestEmbeddedReaderListQueryMatchesTheTitleOrTheID(t *testing.T) { - skipUnlessEmbeddedDolt(t) - ctx := t.Context() - conformance.RunReaderListQueryMatchesTheTitleOrTheID(t, ctx, newEmbeddedReaderFixture(t, "rdr")) -} - func TestEmbeddedReaderGetResolvesTheExactIDAcrossBothPlanes(t *testing.T) { skipUnlessEmbeddedDolt(t) ctx := t.Context() @@ -247,18 +241,6 @@ func TestEmbeddedReaderListWispTypeNarrowsTheAdmittedPlaneRatherThanAdmittingIt( conformance.RunReaderListWispTypeNarrowsTheAdmittedPlaneRatherThanAdmittingIt(t, ctx, newEmbeddedReaderFixture(t, "rdr")) } -func TestEmbeddedReaderListTimeBoundsAreStrictAndWholeSecond(t *testing.T) { - skipUnlessEmbeddedDolt(t) - ctx := t.Context() - conformance.RunReaderListTimeBoundsAreStrictAndWholeSecond(t, ctx, newEmbeddedReaderFixture(t, "rdr")) -} - -func TestEmbeddedReaderListFilterFamiliesNarrowToTheirRows(t *testing.T) { - skipUnlessEmbeddedDolt(t) - ctx := t.Context() - conformance.RunReaderListFilterFamiliesNarrowToTheirRows(t, ctx, newEmbeddedReaderFixture(t, "rdr")) -} - func TestEmbeddedReaderListBriefDropsTheFreeFormTextAndNothingElse(t *testing.T) { skipUnlessEmbeddedDolt(t) ctx := t.Context() @@ -294,6 +276,5 @@ func newEmbeddedReaderFixture(t *testing.T, prefix string) conformance.ReaderFix _, err := te.store.AddIssueComment(ctx, issueID, author, text) return err }, - PromoteWisp: te.store.PromoteFromEphemeral, } } diff --git a/internal/storage/embeddeddolt/ready_total_test.go b/internal/storage/embeddeddolt/ready_total_test.go deleted file mode 100644 index 5f00f8c29..000000000 --- a/internal/storage/embeddeddolt/ready_total_test.go +++ /dev/null @@ -1,396 +0,0 @@ -//go:build cgo - -package embeddeddolt_test - -import ( - "context" - "database/sql" - "fmt" - "slices" - "strings" - "testing" - "time" - - "github.com/steveyegge/beads/internal/storage/embeddeddolt" - "github.com/steveyegge/beads/internal/storage/issueops" - "github.com/steveyegge/beads/internal/types" -) - -// countingTx counts the statements a read issues. Against a remote SQL server -// every statement is one sequential round trip, so this number — not rows -// scanned — is what `bd ready` latency is made of. -type countingTx struct { - tx *sql.Tx - stmts []string -} - -func (c *countingTx) record(q string) { - c.stmts = append(c.stmts, strings.Join(strings.Fields(q), " ")) -} - -func (c *countingTx) ExecContext(ctx context.Context, q string, args ...any) (sql.Result, error) { - c.record(q) - return c.tx.ExecContext(ctx, q, args...) -} - -func (c *countingTx) QueryContext(ctx context.Context, q string, args ...any) (*sql.Rows, error) { - c.record(q) - return c.tx.QueryContext(ctx, q, args...) -} - -func (c *countingTx) QueryRowContext(ctx context.Context, q string, args ...any) *sql.Row { - c.record(q) - return c.tx.QueryRowContext(ctx, q, args...) -} - -// withCountingReadTx runs fn inside one read transaction on a raw connection -// to te's database and returns the statements fn issued. -func withCountingReadTx(t *testing.T, te *testEnv, fn func(tx issueops.DBTX) error) []string { - t.Helper() - ctx := t.Context() - db, cleanup, err := embeddeddolt.OpenSQL(ctx, te.dataDir, te.database, "main") - if err != nil { - t.Fatalf("OpenSQL: %v", err) - } - defer func() { _ = cleanup() }() - tx, err := db.BeginTx(ctx, nil) - if err != nil { - t.Fatalf("begin: %v", err) - } - defer func() { _ = tx.Rollback() }() - c := &countingTx{tx: tx} - if err := fn(c); err != nil { - t.Fatalf("read: %v", err) - } - return c.stmts -} - -func readyIDs(items []*types.IssueWithCounts) []string { - ids := make([]string, 0, len(items)) - for _, it := range items { - ids = append(ids, it.Issue.ID) - } - return ids -} - -type readyTotalWorld struct { - wisps bool - deferredParent bool - collision bool -} - -// seedReadyTotalWorld builds a ready front with every shape the total has to -// get right: plain ready issues, a blocked issue (its blocker stays ready), a -// future-deferred parent whose children are hidden, ready wisps, and an ID -// present in both tables (the overlap the merge dedupes wisp-wins). -func seedReadyTotalWorld(t *testing.T, te *testEnv, prefix string, w readyTotalWorld) { - t.Helper() - ctx := t.Context() - create := func(iss *types.Issue) { - t.Helper() - if iss.Status == "" { - iss.Status = types.StatusOpen - } - if iss.IssueType == "" { - iss.IssueType = types.TypeTask - } - if err := te.store.CreateIssue(ctx, iss, "tester"); err != nil { - t.Fatalf("CreateIssue %s: %v", iss.ID, err) - } - } - dep := func(from, to string, typ types.DependencyType) { - t.Helper() - if err := te.store.AddDependency(ctx, &types.Dependency{IssueID: from, DependsOnID: to, Type: typ}, "tester"); err != nil { - t.Fatalf("AddDependency %s -> %s: %v", from, to, err) - } - } - for i := 0; i < 5; i++ { - create(&types.Issue{ID: fmt.Sprintf("%s-r%d", prefix, i), Title: fmt.Sprintf("ready %d", i), Priority: i % 3}) - } - create(&types.Issue{ID: prefix + "-blocker", Title: "blocker", Priority: 1}) - create(&types.Issue{ID: prefix + "-blocked", Title: "blocked", Priority: 1}) - dep(prefix+"-blocked", prefix+"-blocker", types.DepBlocks) - - if w.deferredParent { - future := time.Now().UTC().Add(72 * time.Hour) - create(&types.Issue{ID: prefix + "-dparent", Title: "deferred parent", IssueType: types.TypeEpic, DeferUntil: &future}) - create(&types.Issue{ID: prefix + "-dchild", Title: "child of deferred", Priority: 0}) - dep(prefix+"-dchild", prefix+"-dparent", types.DepParentChild) - } - if w.wisps { - for i := 0; i < 3; i++ { - create(&types.Issue{ID: fmt.Sprintf("%s-w%d", prefix, i), Title: fmt.Sprintf("wisp %d", i), Priority: 1, Ephemeral: true}) - } - if w.deferredParent { - create(&types.Issue{ID: prefix + "-wdchild", Title: "wisp child of deferred", Priority: 0, Ephemeral: true}) - dep(prefix+"-wdchild", prefix+"-dparent", types.DepParentChild) - } - } - if w.collision { - // GH#4455 made creates refuse this, so it is written the way a store - // corrupted before that fix holds it: the same row in both tables. - te.exec(t, ctx, "INSERT INTO wisps (id, title, status, priority, issue_type, ephemeral, created_at, updated_at) "+ - "SELECT id, title, status, priority, issue_type, 1, created_at, updated_at FROM issues WHERE id = ?", prefix+"-r0") - } -} - -// TestReadyWorkPageTotal pins the in-band total GetReadyWorkWithCountsAndTotal -// hands `bd ready`: for every page size it must equal both the ReadyCounter -// identity (CountReadyWorkInTx) and the length of the unbounded page, and the -// page itself must be the unbounded page's prefix — the counting pass it -// replaced proved nothing more. -func TestReadyWorkPageTotal(t *testing.T) { - skipUnlessEmbeddedDolt(t) - - // The expected ready sets are written out by hand rather than derived - // from another reader in this package: GetReadyWorkWithCountsInTx, - // CountReadyWorkInTx and the method under test all share - // probeReadyWorkInTx, so agreement among them alone could not catch a - // probe that hid (or leaked) a row in all three at once. Keyed by - // IncludeEphemeral; suffixes are appended to the world's prefix. - plain := []string{"blocker", "r0", "r1", "r2", "r3", "r4"} - withWisps := []string{"blocker", "r0", "r1", "r2", "r3", "r4", "w0", "w1", "w2"} - worlds := []struct { - name string - world readyTotalWorld - want map[bool][]string - }{ - {"issues_only", readyTotalWorld{}, map[bool][]string{false: plain, true: plain}}, - // dparent is future-deferred, so it and its child are hidden. - {"issues_deferred_parent", readyTotalWorld{deferredParent: true}, map[bool][]string{false: plain, true: plain}}, - {"wisps", readyTotalWorld{wisps: true}, map[bool][]string{false: plain, true: withWisps}}, - // The wisp child of the deferred parent is hidden too. - {"wisps_deferred_parent", readyTotalWorld{wisps: true, deferredParent: true}, map[bool][]string{false: plain, true: withWisps}}, - // r0 sits in both tables; it is listed (and counted) exactly once. - {"wisps_collision", readyTotalWorld{wisps: true, collision: true}, map[bool][]string{false: plain, true: withWisps}}, - } - for i, wc := range worlds { - t.Run(wc.name, func(t *testing.T) { - prefix := fmt.Sprintf("rt%d", i) - te := newTestEnv(t, prefix) - seedReadyTotalWorld(t, te, prefix, wc.world) - - for _, includeEphemeral := range []bool{false, true} { - base := types.WorkFilter{IncludeEphemeral: includeEphemeral} - var unbounded []*types.IssueWithCounts - var counted int - withCountingReadTx(t, te, func(tx issueops.DBTX) error { - var err error - if unbounded, err = issueops.GetReadyWorkWithCountsInTx(t.Context(), tx, base); err != nil { - return err - } - counted, err = issueops.CountReadyWorkInTx(t.Context(), tx, base) - return err - }) - if counted != len(unbounded) { - t.Fatalf("ephemeral=%v: CountReadyWorkInTx = %d, unbounded page has %d rows %v", - includeEphemeral, counted, len(unbounded), readyIDs(unbounded)) - } - all := readyIDs(unbounded) - if got, want := sortedIDs(all), prefixed(prefix, wc.want[includeEphemeral]); strings.Join(got, ",") != strings.Join(want, ",") { - t.Fatalf("ephemeral=%v: ready set = %v, want %v", includeEphemeral, got, want) - } - if len(all) < 3 { - t.Fatalf("ephemeral=%v: seeded world too small to page: %v", includeEphemeral, all) - } - sawWisp := false - for _, id := range all { - if strings.Contains(id, "-w") { - sawWisp = true - } - if strings.HasSuffix(id, "-blocked") || strings.Contains(id, "dchild") { - t.Fatalf("ephemeral=%v: %s is not ready but was listed: %v", includeEphemeral, id, all) - } - } - - // Identity alone cannot see a wisp family that was never read: - // the page and the count would agree on leaving it out. - if wantWisp := wc.world.wisps && includeEphemeral; sawWisp != wantWisp { - t.Fatalf("ephemeral=%v: wisps listed = %v, want %v: %v", includeEphemeral, sawWisp, wantWisp, all) - } - - for _, limit := range []int{0, 1, 2, len(all) - 1, len(all), len(all) + 3, len(all) + 5} { - filter := base - filter.Limit = limit - var page []*types.IssueWithCounts - var total int - withCountingReadTx(t, te, func(tx issueops.DBTX) error { - var err error - page, total, err = issueops.GetReadyWorkWithCountsAndTotalInTx(t.Context(), tx, filter) - return err - }) - if total != len(all) { - t.Errorf("ephemeral=%v limit=%d: total = %d, want %d", includeEphemeral, limit, total, len(all)) - } - want := all - if limit > 0 && limit < len(all) { - want = all[:limit] - } - if got := readyIDs(page); strings.Join(got, ",") != strings.Join(want, ",") { - t.Errorf("ephemeral=%v limit=%d: page = %v, want %v", includeEphemeral, limit, got, want) - } - } - } - }) - } -} - -// TestReadyWorkStatementBudget is the round-trip regression guard. `bd ready -// --json --limit 1` against a remote store with ~55 ms per round trip took -// 5.4 s — slower than --limit 0 — because a capped page re-ran every probe per -// table family and then sized the set in a second pass. These budgets are the -// statements ONE read transaction issues for the page AND its total; the -// separate count transaction is gone entirely. -func TestReadyWorkStatementBudget(t *testing.T) { - skipUnlessEmbeddedDolt(t) - - cases := []struct { - name string - world readyTotalWorld - limit int - want int - }{ - // probe + mega-query - {"issues_only/limit0", readyTotalWorld{}, 0, 2}, - // probe + (ID page with window total + counts hydration) - {"issues_only/limit1", readyTotalWorld{}, 1, 3}, - // probe + mega-query per family - {"wisps/limit0", readyTotalWorld{wisps: true}, 0, 3}, - // probe + (ID page + hydration) per family - {"wisps/limit1", readyTotalWorld{wisps: true}, 1, 5}, - // + one statement for every deferred-parent child leg - {"wisps_deferred_parent/limit0", readyTotalWorld{wisps: true, deferredParent: true}, 0, 4}, - {"wisps_deferred_parent/limit1", readyTotalWorld{wisps: true, deferredParent: true}, 1, 6}, - // + the overlap count, only because an ID sits in both tables - {"wisps_collision/limit1", readyTotalWorld{wisps: true, collision: true}, 1, 6}, - } - for i, tc := range cases { - t.Run(tc.name, func(t *testing.T) { - prefix := fmt.Sprintf("rb%d", i) - te := newTestEnv(t, prefix) - seedReadyTotalWorld(t, te, prefix, tc.world) - - filter := types.WorkFilter{IncludeEphemeral: true, Limit: tc.limit} - var total int - stmts := withCountingReadTx(t, te, func(tx issueops.DBTX) error { - var err error - _, total, err = issueops.GetReadyWorkWithCountsAndTotalInTx(t.Context(), tx, filter) - return err - }) - if total == 0 { - t.Fatalf("total = 0; the seeded world has ready work") - } - // A ceiling, not an exact pin: a change that saves a statement - // should not have to edit this table, but one that adds a round - // trip fails. The budgets are today's counts, so there is no slack. - if len(stmts) > tc.want { - t.Errorf("page+total issued %d statements, budget %d (+%d):\n %s", - len(stmts), tc.want, len(stmts)-tc.want, strings.Join(stmts, "\n ")) - } else if len(stmts) < tc.want { - t.Logf("page+total issued %d statements, under budget %d by %d; consider tightening:\n %s", - len(stmts), tc.want, tc.want-len(stmts), strings.Join(stmts, "\n ")) - } - }) - } -} - -func sortedIDs(ids []string) []string { - out := slices.Clone(ids) - slices.Sort(out) - return out -} - -func prefixed(prefix string, suffixes []string) []string { - out := make([]string, 0, len(suffixes)) - for _, s := range suffixes { - out = append(out, prefix+"-"+s) - } - slices.Sort(out) - return out -} - -// TestReadyWorkPageTotalFilterMatrix runs the in-band total across the -// WorkFilter fields `bd ready` forwards. Each filter's ready set is written -// out by hand (the oracle); the total must equal the unbounded listing's -// length, and every capped page must be that listing's prefix, so a filter -// the window total ignored — or applied to one family only — fails here. -func TestReadyWorkPageTotalFilterMatrix(t *testing.T) { - skipUnlessEmbeddedDolt(t) - - const prefix = "rm" - te := newTestEnv(t, prefix) - seedReadyTotalWorld(t, te, prefix, readyTotalWorld{wisps: true, deferredParent: true}) - ctx := t.Context() - id := func(s string) string { return prefix + "-" + s } - for issue, labels := range map[string][]string{"r0": {"a", "b"}, "r1": {"a"}, "blocker": {"a"}, "w0": {"b"}} { - for _, l := range labels { - if err := te.store.AddLabel(ctx, id(issue), l, "tester"); err != nil { - t.Fatalf("AddLabel %s %s: %v", issue, l, err) - } - } - } - for _, issue := range []string{"r2", "w1"} { - if err := te.store.UpdateIssue(ctx, id(issue), map[string]interface{}{"assignee": "alice"}, "tester"); err != nil { - t.Fatalf("assign %s: %v", issue, err) - } - } - - all := []string{"blocker", "r0", "r1", "r2", "r3", "r4", "w0", "w1", "w2"} - alice, zero := "alice", 0 - parent := id("dparent") - cases := []struct { - name string - filter types.WorkFilter - want []string - }{ - {"hybrid", types.WorkFilter{}, all}, - {"oldest", types.WorkFilter{SortPolicy: types.SortPolicyOldest}, all}, - {"priority", types.WorkFilter{SortPolicy: types.SortPolicyPriority}, all}, - {"labels_all", types.WorkFilter{Labels: []string{"a", "b"}}, []string{"r0"}}, - {"labels_any", types.WorkFilter{LabelsAny: []string{"a", "b"}}, []string{"blocker", "r0", "r1", "w0"}}, - {"assignee", types.WorkFilter{Assignee: &alice}, []string{"r2", "w1"}}, - {"unassigned", types.WorkFilter{Unassigned: true}, []string{"blocker", "r0", "r1", "r3", "r4", "w0", "w2"}}, - {"priority_0", types.WorkFilter{Priority: &zero}, []string{"r0", "r3"}}, - {"exclude_ids", types.WorkFilter{ExcludeIDs: []string{id("r1"), id("w2")}}, []string{"blocker", "r0", "r2", "r3", "r4", "w0", "w1"}}, - {"include_deferred", types.WorkFilter{IncludeDeferred: true}, append(slices.Clone(all), "dchild", "dparent", "wdchild")}, - {"parent_deferred", types.WorkFilter{ParentID: &parent, IncludeDeferred: true}, []string{"dchild", "wdchild"}}, - {"max_rows", types.WorkFilter{MaxRows: 50}, all}, - {"type_epic", types.WorkFilter{Type: string(types.TypeEpic), IncludeDeferred: true}, []string{"dparent"}}, - {"type_task_oldest", types.WorkFilter{Type: string(types.TypeTask), SortPolicy: types.SortPolicyOldest}, all}, - } - for _, tc := range cases { - t.Run(tc.name, func(t *testing.T) { - base := tc.filter - base.IncludeEphemeral = true - var unbounded []*types.IssueWithCounts - withCountingReadTx(t, te, func(tx issueops.DBTX) error { - var err error - unbounded, err = issueops.GetReadyWorkWithCountsInTx(t.Context(), tx, base) - return err - }) - listed := readyIDs(unbounded) - if got, want := sortedIDs(listed), prefixed(prefix, tc.want); strings.Join(got, ",") != strings.Join(want, ",") { - t.Fatalf("ready set = %v, want %v", got, want) - } - n := len(listed) - for _, limit := range []int{1, 2, n, n + 3} { - filter := base - filter.Limit = limit - var page []*types.IssueWithCounts - var total int - withCountingReadTx(t, te, func(tx issueops.DBTX) error { - var err error - page, total, err = issueops.GetReadyWorkWithCountsAndTotalInTx(t.Context(), tx, filter) - return err - }) - if total != n { - t.Errorf("limit=%d: total = %d, want %d", limit, total, n) - } - want := listed[:min(limit, n)] - if got := readyIDs(page); strings.Join(got, ",") != strings.Join(want, ",") { - t.Errorf("limit=%d: page = %v, want prefix %v", limit, got, want) - } - } - }) - } -} diff --git a/internal/storage/embeddeddolt/remote_git_ref_test.go b/internal/storage/embeddeddolt/remote_git_ref_test.go deleted file mode 100644 index 828c9c181..000000000 --- a/internal/storage/embeddeddolt/remote_git_ref_test.go +++ /dev/null @@ -1,185 +0,0 @@ -//go:build cgo - -package embeddeddolt_test - -import ( - "context" - "os" - "os/exec" - "path/filepath" - "strings" - "testing" - - "github.com/steveyegge/beads/internal/storage" - "github.com/steveyegge/beads/internal/storage/doltutil" - "github.com/steveyegge/beads/internal/storage/embeddeddolt" - "github.com/steveyegge/beads/internal/storage/versioncontrolops" -) - -// runGit runs git in dir and fails the test on error. -func runGit(t *testing.T, dir string, args ...string) string { - t.Helper() - cmd := exec.Command("git", args...) - cmd.Dir = dir - // Repository-local hooks path, so a developer's global core.hooksPath - // cannot run inside the fixture repositories (engdocs/TESTING.md). - cmd.Env = append(os.Environ(), - "GIT_TERMINAL_PROMPT=0", - "GIT_AUTHOR_NAME=bd-test", "GIT_AUTHOR_EMAIL=bd-test@example.com", - "GIT_COMMITTER_NAME=bd-test", "GIT_COMMITTER_EMAIL=bd-test@example.com", - "GIT_CONFIG_COUNT=1", "GIT_CONFIG_KEY_0=core.hooksPath", "GIT_CONFIG_VALUE_0="+filepath.Join(dir, ".git", "hooks"), - ) - out, err := cmd.CombinedOutput() - if err != nil { - t.Fatalf("git %s: %v\n%s", strings.Join(args, " "), err, out) - } - return string(out) -} - -// newBareGitRemote creates a bare repository with one commit on main, the -// shape a git host presents before any Dolt data has been pushed to it. -func newBareGitRemote(t *testing.T, name string) string { - t.Helper() - base := t.TempDir() - bare := filepath.Join(base, name) - runGit(t, base, "init", "--bare", "-b", "main", bare) - seed := filepath.Join(base, "seed") - if err := os.MkdirAll(seed, 0o755); err != nil { - t.Fatal(err) - } - runGit(t, seed, "init", "-b", "main") - runGit(t, seed, "commit", "--allow-empty", "-m", "init") - runGit(t, seed, "push", bare, "main") - return bare -} - -// lsRemoteRef returns the object a ref points at on the bare repository, or -// "" when the ref does not exist there. -func lsRemoteRef(t *testing.T, bare, ref string) string { - t.Helper() - return strings.TrimSpace(runGit(t, bare, "ls-remote", bare, ref)) -} - -// The engine, not a mock, is the authority on the SQL shapes above: a remote -// added with a ref lists that ref back from dolt_remotes and from -// repo_state.json, for a branch and for a ref outside refs/heads/; a remote -// added without one lists the default; Dolt refuses a ref on a non-git URL -// and the refusal reaches the caller; a push lands on the custom ref and -// never on refs/dolt/data; a clone with the ref records it on origin. -func TestRemoteGitDataRefOnEngine(t *testing.T) { - if _, err := exec.LookPath("git"); err != nil { - t.Skip("git not available") - } - ctx := context.Background() - te := newTestEnv(t, "gref") - - shapes := map[string]string{ - "branch": "refs/heads/issue-data", - "unit": "refs/dolt/units/team-12542", - } - bares := map[string]string{} - for name, ref := range shapes { - bares[name] = newBareGitRemote(t, name) - if err := te.store.AddRemoteWithRef(ctx, name, "git+file://"+bares[name], ref); err != nil { - t.Fatalf("AddRemoteWithRef(%s, %s): %v", name, ref, err) - } - } - plainDir := t.TempDir() - if err := te.store.AddRemote(ctx, "plain", "file://"+plainDir); err != nil { - t.Fatalf("AddRemote(plain): %v", err) - } - - t.Run("listed from SQL and from disk", func(t *testing.T) { - remotes, err := te.store.ListRemotes(ctx) - if err != nil { - t.Fatalf("ListRemotes: %v", err) - } - assertRefs(t, "ListRemotes", remotes, shapes) - - persisted, err := doltutil.PersistedRemotes(filepath.Join(te.dataDir, te.database)) - if err != nil { - t.Fatalf("PersistedRemotes: %v", err) - } - assertRefs(t, "PersistedRemotes", persisted, shapes) - }) - - t.Run("ref on a non-git remote is Dolt's error, not a silent default", func(t *testing.T) { - err := te.store.AddRemoteWithRef(ctx, "bad", "file://"+t.TempDir(), "refs/heads/issue-data") - if err == nil { - t.Fatal("AddRemoteWithRef on file:// with a ref = nil, want error") - } - if !strings.Contains(err.Error(), "git remotes") { - t.Errorf("error should be Dolt's --ref refusal, got: %v", err) - } - if ok, _ := te.store.HasRemote(ctx, "bad"); ok { - t.Error("refused remote must not exist") - } - }) - - for name, ref := range shapes { - t.Run("push and clone on "+ref, func(t *testing.T) { - bare := bares[name] - if err := te.store.PushRemote(ctx, name, false); err != nil { - t.Fatalf("PushRemote(%s): %v", name, err) - } - if got := lsRemoteRef(t, bare, ref); got == "" { - t.Fatalf("push did not create %s on the git remote", ref) - } - if got := lsRemoteRef(t, bare, storage.DefaultGitDataRef); got != "" { - t.Errorf("push created %s (%s) although the remote was added with --ref %s", storage.DefaultGitDataRef, got, ref) - } - if got := lsRemoteRef(t, bare, "refs/heads/main"); got == "" { - t.Error("refs/heads/main disappeared from the git remote") - } - - cloneDir := t.TempDir() - boot, bootCleanup, err := embeddeddolt.OpenSQL(ctx, cloneDir, "", "") - if err != nil { - t.Fatalf("OpenSQL for clone: %v", err) - } - cloneErr := versioncontrolops.DoltCloneWithRef(ctx, boot, "git+file://"+bare, "cloned", "", ref) - _ = bootCleanup() - if cloneErr != nil { - t.Fatalf("DoltCloneWithRef: %v", cloneErr) - } - persisted, err := doltutil.PersistedRemotes(filepath.Join(cloneDir, "cloned")) - if err != nil { - t.Fatalf("PersistedRemotes on clone: %v", err) - } - assertRefs(t, "clone repo_state.json", persisted, map[string]string{"origin": ref}) - - db, cleanup, err := embeddeddolt.OpenSQL(ctx, cloneDir, "cloned", "main") - if err != nil { - t.Fatalf("OpenSQL on clone: %v", err) - } - defer func() { _ = cleanup() }() - listed, err := versioncontrolops.ListRemotes(ctx, db) - if err != nil { - t.Fatalf("ListRemotes on clone: %v", err) - } - assertRefs(t, "clone dolt_remotes", listed, map[string]string{"origin": ref}) - }) - } -} - -// assertRefs checks that every named remote in want lists exactly that ref -// and that the remote named plain, when present, lists the default. -func assertRefs(t *testing.T, where string, remotes []storage.RemoteInfo, want map[string]string) { - t.Helper() - got := map[string]string{} - for _, r := range remotes { - got[r.Name] = r.Ref - } - for name, ref := range want { - if _, ok := got[name]; !ok { - t.Errorf("%s: remote %s missing from %+v", where, name, remotes) - continue - } - if got[name] != ref { - t.Errorf("%s: remote %s Ref = %q, want %q", where, name, got[name], ref) - } - } - if ref, ok := got["plain"]; ok && ref != "" { - t.Errorf("%s: remote plain Ref = %q, want empty", where, ref) - } -} diff --git a/internal/storage/embeddeddolt/retention_epoch_contract_test.go b/internal/storage/embeddeddolt/retention_epoch_contract_test.go deleted file mode 100644 index 6c9a7b52e..000000000 --- a/internal/storage/embeddeddolt/retention_epoch_contract_test.go +++ /dev/null @@ -1,191 +0,0 @@ -//go:build cgo - -package embeddeddolt_test - -import ( - "context" - - "testing" - - "github.com/steveyegge/beads/backend/conformance" - "github.com/steveyegge/beads/internal/storage/embeddeddolt" - storeops "github.com/steveyegge/beads/internal/storage/issueops" -) - -// TestRetentionContract wires this leg into the R20 retention contract. -// Phase 0 leaves every hook nil in every backend's fixture kit (architecture -// §12), so each case below skips by name; this file exists so -// TestEveryLegWiresEveryRoleContract counts this leg, and so the cases start -// running for real the moment this leg's fixture kit grows a non-nil hook. -func TestRetentionContract(t *testing.T) { - ctx := context.Background() - fixture := conformance.RetentionFixture{IssuePrefix: "rete"} - - t.Run("RemovalLeavesTheAddressAbleToAnswer", func(t *testing.T) { - conformance.RunRemovalLeavesTheAddressAbleToAnswer(t, ctx, fixture) - }) - t.Run("RemovalReasonIsRetentionErasureOrReorganizationDistinctly", func(t *testing.T) { - conformance.RunRemovalReasonIsRetentionErasureOrReorganizationDistinctly(t, ctx, fixture) - }) - t.Run("RemovalReportsTheSurvivingRetainedWindow", func(t *testing.T) { - conformance.RunRemovalReportsTheSurvivingRetainedWindow(t, ctx, fixture) - }) - t.Run("RemovalNeverReassignsASurvivingAddress", func(t *testing.T) { - conformance.RunRemovalNeverReassignsASurvivingAddress(t, ctx, fixture) - }) - t.Run("GoneIsDistinguishableFromUnknown", func(t *testing.T) { - conformance.RunGoneIsDistinguishableFromUnknown(t, ctx, fixture) - }) - t.Run("AnAddressNeverResolvesToADifferentState", func(t *testing.T) { - conformance.RunAnAddressNeverResolvesToADifferentState(t, ctx, fixture) - }) - t.Run("ADestructiveOperationEnumeratesAffectedAddressesFirst", func(t *testing.T) { - conformance.RunADestructiveOperationEnumeratesAffectedAddressesFirst(t, ctx, fixture) - }) - t.Run("AHoldPreventsRemovalAndReportsInRetainedBounds", func(t *testing.T) { - conformance.RunAHoldPreventsRemovalAndReportsInRetainedBounds(t, ctx, fixture) - }) - t.Run("ForcingAHeldRemovalRecordsWhoWhenWhy", func(t *testing.T) { - conformance.RunForcingAHeldRemovalRecordsWhoWhenWhy(t, ctx, fixture) - }) - t.Run("EveryRetentionAnswerNamesItsProducingStore", func(t *testing.T) { - conformance.RunEveryRetentionAnswerNamesItsProducingStore(t, ctx, fixture) - }) - t.Run("AStoreWithNoLineageKnowledgeAnswersUnknownNotGone", func(t *testing.T) { - conformance.RunAStoreWithNoLineageKnowledgeAnswersUnknownNotGone(t, ctx, fixture) - }) - t.Run("AStoreThatRemovesStateStillAnswersGoneDurably", func(t *testing.T) { - conformance.RunAStoreThatRemovesStateStillAnswersGoneDurably(t, ctx, fixture) - }) - t.Run("ErasureMintsACorrectedVersionRatherThanEditingInPlace", func(t *testing.T) { - conformance.RunErasureMintsACorrectedVersionRatherThanEditingInPlace(t, ctx, fixture) - }) -} - -// TestEpochContract runs the R20 epoch contract (gastownhall/beads#5898 -// revision 9, #6136) against the embedded-Dolt- -// backed store, which reaches internal/storage/issueops's epoch Tx -// functions through this leg's own issue-operation transaction -// (EmbeddedDoltStore.BumpEpoch/BumpEpochCarrying/MintUnderEpoch/LoseVersion) -// or a read-only connection (EmbeddedDoltStore.CurrentEpoch/StillServes/ -// Resolve/CurrentAddressFor) — see internal/storage/embeddeddolt/epoch_cas.go. -// -// All three legs run that one shared body, so this is not an independent -// vote on the design — it is the check on THIS leg's wrapper and this file's -// own conformance-vocabulary translation. -func TestEpochContract(t *testing.T) { - skipUnlessEmbeddedDolt(t) - te := newTestEnv(t, "epch") - ctx := t.Context() - fixture := conformance.EpochFixture{ - IssuePrefix: "epch", - CurrentEpoch: epochEmbeddedCurrentEpoch(te.store), - BumpEpoch: epochEmbeddedBumpEpoch(te.store), - MintUnderEpoch: epochEmbeddedMintUnderEpoch(te.store), - StillServes: epochEmbeddedStillServes(te.store), - Resolve: epochEmbeddedResolve(te.store), - CurrentAddressFor: epochEmbeddedCurrentAddressFor(te.store), - LoseVersion: epochEmbeddedLoseVersion(te.store), - } - - t.Run("AnEpochBumpIsTriggeredOnlyByRestoreReinitOrSchemeChange", func(t *testing.T) { - conformance.RunAnEpochBumpIsTriggeredOnlyByRestoreReinitOrSchemeChange(t, ctx, fixture) - }) - t.Run("EpochBumpVoidsOnlyAddressesOfVersionsNoLongerServed", func(t *testing.T) { - conformance.RunEpochBumpVoidsOnlyAddressesOfVersionsNoLongerServed(t, ctx, fixture) - }) - t.Run("TokenSchemeChangeCarriesLiveAddressesAndOnlyThose", func(t *testing.T) { - conformance.RunTokenSchemeChangeCarriesLiveAddressesAndOnlyThose(t, ctx, fixture) - }) - t.Run("ACarriedAddressKeepsResolvingAcrossLaterBumps", func(t *testing.T) { - conformance.RunACarriedAddressKeepsResolvingAcrossLaterBumps(t, ctx, fixture) - }) - t.Run("ALostAddressStaysGoneAcrossASchemeChange", func(t *testing.T) { - conformance.RunALostAddressStaysGoneAcrossASchemeChange(t, ctx, fixture) - }) -} - -func epochEmbeddedCurrentEpoch(store *embeddeddolt.EmbeddedDoltStore) func(ctx context.Context, storeID string) (int, error) { - return func(ctx context.Context, storeID string) (int, error) { - return store.CurrentEpoch(ctx, storeID) - } -} - -// epochEmbeddedBumpEpoch chooses the bump by the typed trigger: only a -// token-scheme-change bump carries live addresses to the new epoch, and the -// other two triggers move the counter alone. The choice lives here, in the -// contract-test translation layer, because the shared body has no switch on -// the reason string. -func epochEmbeddedBumpEpoch(store *embeddeddolt.EmbeddedDoltStore) func(ctx context.Context, storeID string, trigger conformance.EpochBumpTrigger) (int, error) { - return func(ctx context.Context, storeID string, trigger conformance.EpochBumpTrigger) (int, error) { - if trigger == conformance.EpochBumpTriggerTokenSchemeChange { - return store.BumpEpochCarrying(ctx, storeID, trigger.String()) - } - return store.BumpEpoch(ctx, storeID, trigger.String()) - } -} - -func epochEmbeddedMintUnderEpoch(store *embeddeddolt.EmbeddedDoltStore) func(ctx context.Context, storeID, id string) (conformance.Address, error) { - return func(ctx context.Context, storeID, id string) (conformance.Address, error) { - address, err := store.MintUnderEpoch(ctx, storeID, id) - if err != nil { - return "", err - } - return conformance.Address(address), nil - } -} - -func epochEmbeddedStillServes(store *embeddeddolt.EmbeddedDoltStore) func(ctx context.Context, storeID string, address conformance.Address) (bool, error) { - return func(ctx context.Context, storeID string, address conformance.Address) (bool, error) { - return store.StillServes(ctx, storeID, string(address)) - } -} - -func epochEmbeddedResolve(store *embeddeddolt.EmbeddedDoltStore) func(ctx context.Context, storeID string, address conformance.Address) (conformance.RetentionAnswer, error) { - return func(ctx context.Context, storeID string, address conformance.Address) (conformance.RetentionAnswer, error) { - result, err := store.Resolve(ctx, storeID, string(address)) - if err != nil { - return conformance.RetentionAnswer{}, err - } - return epochResolveResultToConformance(result), nil - } -} - -func epochEmbeddedCurrentAddressFor(store *embeddeddolt.EmbeddedDoltStore) func(ctx context.Context, storeID string, oldAddress conformance.Address) (conformance.Address, error) { - return func(ctx context.Context, storeID string, oldAddress conformance.Address) (conformance.Address, error) { - address, err := store.CurrentAddressFor(ctx, storeID, string(oldAddress)) - if err != nil { - return "", err - } - return conformance.Address(address), nil - } -} - -func epochEmbeddedLoseVersion(store *embeddeddolt.EmbeddedDoltStore) func(ctx context.Context, storeID string, address conformance.Address) error { - return func(ctx context.Context, storeID string, address conformance.Address) error { - return store.LoseVersion(ctx, storeID, string(address)) - } -} - -// epochRestrictionToConformance translates this leg's local -// storeops.EpochRestriction into backend/conformance's own Restriction -// vocabulary — the contract-test file is the translation boundary, per -// epoch_cas.go's package doc. -func epochRestrictionToConformance(r storeops.EpochRestriction) conformance.Restriction { - switch r { - case storeops.EpochRestrictionLive: - return conformance.RestrictionLive - case storeops.EpochRestrictionGoneReorganization: - return conformance.RestrictionGoneReorganization - default: - return conformance.RestrictionUnknown - } -} - -func epochResolveResultToConformance(r storeops.EpochResolveResult) conformance.RetentionAnswer { - return conformance.RetentionAnswer{ - Restriction: epochRestrictionToConformance(r.Restriction), - ProducingStore: r.ProducingStore, - Epoch: r.Epoch, - } -} diff --git a/internal/storage/embeddeddolt/stats_reporter_contract_test.go b/internal/storage/embeddeddolt/stats_reporter_contract_test.go index cb201ef28..97615535e 100644 --- a/internal/storage/embeddeddolt/stats_reporter_contract_test.go +++ b/internal/storage/embeddeddolt/stats_reporter_contract_test.go @@ -32,12 +32,6 @@ func TestStatsReporterContract(t *testing.T) { t.Run("ExcludesTheWispTier", func(t *testing.T) { conformance.RunStatsReporterExcludesTheWispTier(t, ctx, fixture) }) - t.Run("BreaksOutTheRowsTheDefaultListingSuppresses", func(t *testing.T) { - conformance.RunStatsReporterBreaksOutTheRowsTheDefaultListingSuppresses(t, ctx, fixture) - }) - t.Run("BreaksOutAGateThatIsAlsoATemplate", func(t *testing.T) { - conformance.RunStatsReporterBreaksOutAGateThatIsAlsoATemplate(t, ctx, fixture) - }) t.Run("AStatusOutsideTheTalliesIsCountedOnlyInTotal", func(t *testing.T) { conformance.RunStatsReporterAStatusOutsideTheTalliesIsCountedOnlyInTotal(t, ctx, fixture) }) @@ -71,9 +65,6 @@ func TestStatsReporterContract(t *testing.T) { t.Run("AssigneeStatsMergesTheWispTier", func(t *testing.T) { conformance.RunStatsReporterAssigneeStatsMergesTheWispTier(t, ctx, fixture) }) - t.Run("AssigneeStatsBreaksOutTheSuppressedRows", func(t *testing.T) { - conformance.RunStatsReporterAssigneeStatsBreaksOutTheSuppressedRows(t, ctx, fixture) - }) t.Run("AssigneeStatsPopulatesBothPointers", func(t *testing.T) { conformance.RunStatsReporterAssigneeStatsPopulatesBothPointers(t, ctx, fixture) }) diff --git a/internal/storage/embeddeddolt/store.go b/internal/storage/embeddeddolt/store.go index 7fa8f3ade..986ad4af7 100644 --- a/internal/storage/embeddeddolt/store.go +++ b/internal/storage/embeddeddolt/store.go @@ -32,7 +32,6 @@ var _ storage.Flattener = (*EmbeddedDoltStore)(nil) var _ storage.Compactor = (*EmbeddedDoltStore)(nil) var _ storage.SchemaMigrator = (*EmbeddedDoltStore)(nil) var _ storage.EventsJournalConfigurer = (*EmbeddedDoltStore)(nil) -var _ storage.VersionedHistoryConfigurer = (*EmbeddedDoltStore)(nil) var _ storage.ExternalRefHistoryQuerier = (*EmbeddedDoltStore)(nil) // EmbeddedDoltStore implements storage.DoltStorage backed by the embedded Dolt engine. @@ -53,10 +52,6 @@ type EmbeddedDoltStore struct { // eventsJournalEnabled activates the durable events journal for THIS store // instance only (storage.EventsJournalConfigurer); never process-global. eventsJournalEnabled atomic.Bool - // versionedHistoryEnabled activates dual-write issue-version history for - // THIS store instance only (storage.VersionedHistoryConfigurer); never - // process-global. - versionedHistoryEnabled atomic.Bool // readOnly marks a store opened via OpenReadOnly or // OpenForPreviewCommand: open-time mutations (CREATE DATABASE, schema // migrations) were skipped and write transactions are refused @@ -70,37 +65,12 @@ type EmbeddedDoltStore struct { intent openIntent } -// lenientSharedGateGuidance is the blunt migrate-or-adopt block shared by the -// lenient-open warnings. Before #6660 it was appended to the mode line and its -// first fragment continued that sentence on the same line; it now always leads -// the warning and so starts its own line, which is why the opening sentence is -// wrapped as a full line rather than as a continuation. -const lenientSharedGateGuidance = " This is a coordination decision, not an auto-fix - do NOT run a\n" + - " migration unless you are the single designated migrator (only ONE\n" + - " clone may migrate a shared remote, else the schema forks; #4259):\n" + - " • designated migrator (only ONE machine): bd migrate --force && bd dolt push\n" + - " • every other clone (another already migrated): bd bootstrap\n" + - " • several machines: only ONE migrates; sync each other clone and run\n" + - " bd dolt pull after the migrator pushes, before upgrading it\n" - -// lenientGateWarningBody preserves the generic migrate-or-adopt guidance for -// blunt gate refusals while allowing smart-gate decisions to explain their -// narrower recovery. A non-empty Decision means UserMessage has a shaped body; -// data-behind is the one shaped refusal deliberately represented as a fallback. -func lenientGateWarningBody(gateErr *schema.RemoteMigrateGateError) string { - if gateErr.Decision != "" || gateErr.IsDataBehind() { - return "Warning: " + gateErr.UserMessage() - } - return "Warning: " + gateErr.Error() + "\n" + lenientSharedGateGuidance -} - // openIntent classifies why a store is being opened. openStrict fails the // open on any pending-migration refusal; openReadOnlyCommand and // openWorkingSetReconcile relax both the #4259 remote-migrate gate refusal and // the #4566 dirty-table refusal, each with its own warning text (see -// initSchema), and openWorkingSetReconcile alone also relaxes the -// migration-consent refusal (toleratesConsentRefusal); openRemoteSync relaxes -// exactly one gate refusal and nothing else. +// initSchema); openRemoteSync relaxes exactly one gate refusal and nothing +// else. type openIntent int const ( @@ -116,9 +86,7 @@ const ( // commands (bd dolt commit, bd vc commit): their entire purpose is to // clear the dirty working set that a migration would otherwise refuse to // touch, so failing the open here would deadlock the documented recovery - // (#4566). For the same reason it relaxes the migration-consent refusal, - // which fires before the dirty-table guard. Used by - // OpenForWorkingSetReconcile. + // (#4566). Used by OpenForWorkingSetReconcile. openWorkingSetReconcile // openRemoteSync is the same shape of deadlock break as // openWorkingSetReconcile, for the #6575 data-behind gate refusal: that @@ -134,83 +102,6 @@ const ( openRemoteSync ) -// lenientGateWarning renders the complete stderr warning for an open that is -// continuing past a remote-migrate gate refusal, so the composed text is a -// pure function of (intent, gateErr) and can be asserted directly in tests. -// Every arm terminates with a newline: the warning is the last thing written -// before the command's own output, and an unterminated arm glues the two -// together (#6660 regressed exactly that when the shared guidance, which used -// to be the terminator of the two non-data-behind arms, moved to the front). -// -// #6575/#6660: lenientSharedGateGuidance is the blunt migrate-or-adopt block. -// On data-behind its bullets are measurably wrong — `bd migrate --force` -// applies the migration this stop exists to prevent, the `bd dolt push` after -// it is rejected non-fast-forward while the clone is still behind, and -// `bd bootstrap` no-ops against an existing workspace. On the smart decisions -// they are instead redundant with the shaped body — and, for the -// `bd migrate --force` bullet, contradicted by it (fork-skew's body says -// migrating cannot un-fork the schema). Either way gateErr's own UserMessage -// is where the right body lives, already branched per decision and, for -// data-behind, per fast-forward vs diverged pull. Printing %v (Error(), the -// one-line summary) and appending the bullets dropped it: a 1.1-era upgrader's -// first `bd list` and their `bd dolt commit` both land in the two arms below, -// so the first thing bd said to a data-behind clone was the wedge. Server -// mode's warnLenientOpenRefusal (dolt/store.go) has rendered UserMessage all -// along; these arms are the ones that lagged. -// -// The intent framing stays: the command in hand is still SUCCEEDING against -// the old schema, which UserMessage — written for the fatal refusal — does not -// say. So the mode line is appended to the shaped body rather than replacing -// it. -func lenientGateWarning(intent openIntent, gateErr *schema.RemoteMigrateGateError) string { - body := lenientGateWarningBody(gateErr) - switch intent { - case openRemoteSync: - // This is the refusal's own prescribed remedy running. It gets - // the short confirmation rather than the full data-behind body: - // toleratesGateRefusal admits only the data-behind stop here, so - // the operator has necessarily just been handed that body by the - // command this pull is unblocking, and re-printing "pull first" - // at the moment they are finally doing it is noise. What it must - // never do is name `bd migrate --force`. - return fmt.Sprintf( - "Warning: %[1]v\n"+ - " Remote-sync command: continuing on schema v%[2]d without migrating, so\n"+ - " this pull can bring in the commits this clone is behind on. Re-run the\n"+ - " command you were blocked on once it completes.\n", - gateErr, gateErr.CurrentVersion) - case openWorkingSetReconcile: - if gateErr.IsDataBehind() { - return fmt.Sprintf( - "%[1]s"+ - " Working-set reconcile command: continuing on schema v%[2]d without\n"+ - " migrating; the commit applies to the working set at the current\n"+ - " schema. It does not resolve the schema — the pull above is what does.\n", - body, gateErr.CurrentVersion) - } - return fmt.Sprintf( - "%[1]s"+ - " Working-set reconcile command: continuing on schema v%[2]d without\n"+ - " migrating; the commit applies to the working set at the current\n"+ - " schema.\n", - body, gateErr.CurrentVersion) - default: // openReadOnlyCommand - if gateErr.IsDataBehind() { - return fmt.Sprintf( - "%[1]s"+ - " Read-only command: continuing on schema v%[2]d without migrating, so\n"+ - " this read succeeds against the old schema. Writes stay blocked until\n"+ - " this clone has pulled.\n", - body, gateErr.CurrentVersion) - } - return fmt.Sprintf( - "%[1]s"+ - " Read-only command: continuing on schema v%[2]d without migrating.\n"+ - " Writes are blocked until the schema is reconciled.\n", - body, gateErr.CurrentVersion) - } -} - // toleratesGateRefusal reports whether this open's intent may warn and // continue past a remote-migrate gate refusal (#4259/#5920/#6575) instead of // failing the open. @@ -243,27 +134,6 @@ func (s *EmbeddedDoltStore) toleratesMigrationRefusal() bool { return s.intent == openReadOnlyCommand || s.intent == openWorkingSetReconcile } -// toleratesConsentRefusal reports whether this open's intent may warn and -// continue on the current schema past the migration-consent refusal -// (schema.MigrateConsentError) instead of failing the open. -// -// Only openWorkingSetReconcile may. MigrateUp checks consent before the #4566 -// dirty-table guard, so on a database that is both behind and dirty a strict -// commit open rebuilds the #4566 deadlock one check earlier: `bd migrate -// schema` is refused as dirty and names `bd dolt commit`, which is refused -// for consent. The commit needs no migration, and MigrateUp refuses before -// any write, so it runs on the current schema. -// -// openReadOnlyCommand stays strict on purpose. The remote-migrate gate lets -// reads ride through because its migrate-or-adopt decision is cross-clone -// coordination that takes a while to settle; this decision is one local -// command either way (`bd migrate schema`, or keep the matching release), and -// the refusal is where the operator learns it is needed. openRemoteSync stays -// strict for the reason toleratesMigrationRefusal gives. -func (s *EmbeddedDoltStore) toleratesConsentRefusal() bool { - return s.intent == openWorkingSetReconcile -} - // errClosed is returned when a method is called after Close. var errClosed = errors.New("embeddeddolt: store is closed") @@ -483,8 +353,6 @@ func (s *EmbeddedDoltStore) commitConn(ctx context.Context, commit bool, fn func } clearJournalScope := issueops.ScopeEventsJournalTransaction(tx, s.eventsJournalEnabled.Load()) defer clearJournalScope() - clearVersionScope := issueops.ScopeVersionedHistoryTransaction(tx, s.versionedHistoryEnabled.Load()) - defer clearVersionScope() clearRecheckScope := issueops.ScopeBlockedRecheckTransaction(tx) defer clearRecheckScope() @@ -512,12 +380,6 @@ func (s *EmbeddedDoltStore) SetEventsJournalEnabled(enabled bool) { s.eventsJournalEnabled.Store(enabled) } -// SetVersionedHistoryEnabled activates dual-write issue-version history for -// this store instance only. -func (s *EmbeddedDoltStore) SetVersionedHistoryEnabled(enabled bool) { - s.versionedHistoryEnabled.Store(enabled) -} - // commitEmbeddedTx classifies an unconfirmed SQL commit response as // indeterminate: the engine may have applied it before the connection failed. func commitEmbeddedTx(tx *sql.Tx) error { @@ -650,9 +512,85 @@ func (s *EmbeddedDoltStore) initSchema(ctx context.Context) error { // working-set commit. Warn and continue on the current schema; // a plain (openStrict) open still fails with the full // migrate-or-adopt guidance. - // lenientGateWarning owns the rendering (and the rationale for - // preferring gateErr's shaped body over the blunt bullets). - fmt.Fprint(os.Stderr, lenientGateWarning(s.intent, gateErr)) + const sharedGuidance = " This is a\n" + + " coordination decision, not an auto-fix - do NOT run a migration unless\n" + + " you are the single designated migrator (only ONE clone may migrate a\n" + + " shared remote, else the schema forks; #4259):\n" + + " • designated migrator (only ONE machine): bd migrate --force && bd dolt push\n" + + " • every other clone (another already migrated): bd bootstrap\n" + + " • several machines: only ONE migrates; sync each other clone and run\n" + + " bd dolt pull after the migrator pushes, before upgrading it\n" + // #6575: sharedGuidance is the blunt migrate-or-adopt block, and + // on the data-behind stop every bullet in it is measurably wrong + // — `bd migrate --force` applies the migration this stop exists + // to prevent, the `bd dolt push` after it is rejected + // non-fast-forward while the clone is still behind, and + // `bd bootstrap` no-ops against an existing workspace. The one + // command that moves the clone forward is `bd dolt pull`, and + // gateErr's own UserMessage is where that body lives, already + // branched for the fast-forward and diverged pulls. Printing + // %v (Error(), the one-line summary) and appending the bullets + // dropped it: a 1.1-era upgrader's first `bd list` and their + // `bd dolt commit` both land in the two arms below, so the first + // thing bd said to a data-behind clone was the wedge. Server + // mode's warnLenientOpenRefusal (dolt/store.go) has rendered + // UserMessage all along; these arms are the ones that lagged. + // + // The intent framing stays: the command in hand is still + // SUCCEEDING against the old schema, which UserMessage — written + // for the fatal refusal — does not say. So the mode line is + // appended to the data-behind body rather than replacing it. + body := "Warning: " + gateErr.Error() + "\n" + if gateErr.IsDataBehind() { + body = "Warning: " + gateErr.UserMessage() + } + switch s.intent { + case openRemoteSync: + // This is the refusal's own prescribed remedy running. It gets + // the short confirmation rather than the full data-behind body: + // toleratesGateRefusal admits only the data-behind stop here, so + // the operator has necessarily just been handed that body by the + // command this pull is unblocking, and re-printing "pull first" + // at the moment they are finally doing it is noise. What it must + // never do is name `bd migrate --force`. + fmt.Fprintf(os.Stderr, + "Warning: %[1]v\n"+ + " Remote-sync command: continuing on schema v%[2]d without migrating, so\n"+ + " this pull can bring in the commits this clone is behind on. Re-run the\n"+ + " command you were blocked on once it completes.\n", + gateErr, gateErr.CurrentVersion) + case openWorkingSetReconcile: + if gateErr.IsDataBehind() { + fmt.Fprintf(os.Stderr, + "%[1]s"+ + " Working-set reconcile command: continuing on schema v%[2]d without\n"+ + " migrating; the commit applies to the working set at the current\n"+ + " schema. It does not resolve the schema — the pull above is what does.\n", + body, gateErr.CurrentVersion) + break + } + fmt.Fprintf(os.Stderr, + "%[1]s"+ + " Working-set reconcile command: continuing on schema v%[2]d without\n"+ + " migrating; the commit applies to the working set at the current\n"+ + " schema."+sharedGuidance, + body, gateErr.CurrentVersion) + default: // openReadOnlyCommand + if gateErr.IsDataBehind() { + fmt.Fprintf(os.Stderr, + "%[1]s"+ + " Read-only command: continuing on schema v%[2]d without migrating, so\n"+ + " this read succeeds against the old schema. Writes stay blocked until\n"+ + " this clone has pulled.\n", + body, gateErr.CurrentVersion) + break + } + fmt.Fprintf(os.Stderr, + "%[1]s"+ + " Read-only command: continuing on schema v%[2]d without migrating.\n"+ + " Writes are blocked until the schema is reconciled."+sharedGuidance, + body, gateErr.CurrentVersion) + } return nil } return err @@ -661,11 +599,6 @@ func (s *EmbeddedDoltStore) initSchema(ctx context.Context) error { // Embedded mode relies on the dolthub/driver/v2's local file/concurrency // controls; schema.MigrateUpWithLock requires a sql-server session lock. if _, err := schema.MigrateUp(ctx, conn); err != nil { - var consentErr *schema.MigrateConsentError - if s.toleratesConsentRefusal() && errors.As(err, &consentErr) { - fmt.Fprint(os.Stderr, consentErr.WorkingSetWarning()) - return nil - } var dirtyErr *schema.DirtyTablesError if s.toleratesMigrationRefusal() && errors.As(err, &dirtyErr) { // The guard exists to keep dirty user data from being entangled @@ -680,13 +613,13 @@ func (s *EmbeddedDoltStore) initSchema(ctx context.Context) error { fmt.Fprintf(os.Stderr, "Warning: %v\n"+ " Committing the working set at the current schema; when it completes,\n"+ - " re-run 'bd migrate schema'.\n", + " re-run 'bd migrate'.\n", dirtyErr) default: // openReadOnlyCommand fmt.Fprintf(os.Stderr, "Warning: %v\n"+ " Continuing without migrating. Run 'bd dolt commit' to commit the\n"+ - " working set at the current schema, then re-run 'bd migrate schema'.\n", + " working set at the current schema, then re-run 'bd migrate'.\n", dirtyErr) } return nil @@ -1165,17 +1098,6 @@ func (s *EmbeddedDoltStore) History(ctx context.Context, issueID string) ([]*sto return result, err } -// ListVersions implements storage.VersionLister over issue_versions. -func (s *EmbeddedDoltStore) ListVersions(ctx context.Context, issueID string) ([]storage.IssueVersion, error) { - var result []storage.IssueVersion - err := s.withConn(ctx, false, func(tx *sql.Tx) error { - var err error - result, err = issueops.ListVersionsInTx(ctx, tx, issueID) - return err - }) - return result, err -} - func (s *EmbeddedDoltStore) AsOf(ctx context.Context, issueID string, ref string) (*types.Issue, error) { var result *types.Issue err := s.withConn(ctx, false, func(tx *sql.Tx) error { @@ -1540,8 +1462,3 @@ func (s *EmbeddedDoltStore) GetStaleIssues(ctx context.Context, filter types.Sta }) return result, err } - -// Compile-time proof that this leg serves versions -- see the matching -// assertion in internal/storage/dolt/versioned.go for why it lives in the -// leg's own package. -var _ storage.VersionLister = (*EmbeddedDoltStore)(nil) diff --git a/internal/storage/embeddeddolt/test_fixture_test.go b/internal/storage/embeddeddolt/test_fixture_test.go index 8f6820546..78b71e1a5 100644 --- a/internal/storage/embeddeddolt/test_fixture_test.go +++ b/internal/storage/embeddeddolt/test_fixture_test.go @@ -16,7 +16,6 @@ import ( "github.com/steveyegge/beads/internal/storage" "github.com/steveyegge/beads/internal/storage/embeddeddolt" - "github.com/steveyegge/beads/internal/storage/schema" "github.com/steveyegge/beads/internal/types" ) @@ -50,12 +49,6 @@ func TestMain(m *testing.M) { } embeddedDoltFixtureRoot = root - // Package-wide migration consent: these tests exercise the machinery that - // runs BELOW the consent gate (migrate_consent.go), exactly as production - // does once an operator has consented. Gate-behavior tests manage the - // consent state themselves. - schema.SetLocalMigrateConsent(true) - code := m.Run() if err := os.RemoveAll(root); err != nil && code == 0 { fmt.Fprintln(os.Stderr, "remove embedded Dolt test fixture root:", err) diff --git a/internal/storage/embeddeddolt/transaction.go b/internal/storage/embeddeddolt/transaction.go index a9eb77f3a..64efdded2 100644 --- a/internal/storage/embeddeddolt/transaction.go +++ b/internal/storage/embeddeddolt/transaction.go @@ -50,12 +50,6 @@ func (s *EmbeddedDoltStore) runTransactionWithMessage(ctx context.Context, fn fu }); err != nil { return err } - afterSQLCommit() - - // Anything the body minted ran with versioned history scoped on this - // store's transaction (withConn), so its version rows must be staged with - // the mutation they describe rather than left dirty in the working set. - tracker.MarkVersionedHistoryDirty(s.versionedHistoryEnabled.Load()) // Create a Dolt version commit from the working set changes. if commitMsg != "" && len(tracker.DirtyTables()) > 0 { @@ -68,11 +62,6 @@ func (s *EmbeddedDoltStore) runTransactionWithMessage(ctx context.Context, fn fu return nil } -// afterSQLCommit runs between runTransactionWithMessage's SQL commit and the -// Dolt commit that publishes it. It is a no-op outside tests, which use it to -// fail the publication of a write whose rows have already landed. -var afterSQLCommit = func() {} - func wrapCommitIndeterminate(op string, err error) error { return fmt.Errorf("%s: %w: %w", op, err, storage.ErrCommitIndeterminate) } diff --git a/internal/storage/embeddeddolt/version_completeness_behaviour_test.go b/internal/storage/embeddeddolt/version_completeness_behaviour_test.go deleted file mode 100644 index d79c1afd0..000000000 --- a/internal/storage/embeddeddolt/version_completeness_behaviour_test.go +++ /dev/null @@ -1,424 +0,0 @@ -//go:build cgo - -package embeddeddolt_test - -import ( - "encoding/json" - "testing" - "time" - - "github.com/steveyegge/beads/internal/storage" - "github.com/steveyegge/beads/internal/types" - "github.com/steveyegge/beads/issueops" -) - -// These tests are the RUNTIME twin of issueops' version_completeness_test.go, -// which pins by static analysis that every issue-plane mutation reaches -// RecordVersionInTx. That guard cannot see whether the mint actually lands — -// that INSERT IGNORE reports zero affected rows for a duplicate label on this -// engine, that a closed row can still be snapshotted, that a batch create's -// deferred mint runs after its edges — so the law is exercised here once, -// flag on, on the in-process leg: every accepted mutation mints exactly one -// version row, a no-op mints none, and current_revision always equals the -// row count (FR-1, FR-2, FR-5 of #6135). It is an ENGINE CHECK on the shared -// issueops bodies, like TestEmbeddedDependencyEditorVersionsOnlyARealAddOrRemove -// beside it: the dolt and uow legs run the same functions. -// -// Every assertion is a DELTA against the issue's own version-row count, so -// the cases stay correct regardless of how many rows their seeds minted. - -type versionProbe struct { - t *testing.T - kit roleFixtureKit -} - -func newVersionProbe(t *testing.T, te *testEnv, prefix string) versionProbe { - t.Helper() - configurer, ok := any(te.store).(storage.VersionedHistoryConfigurer) - if !ok { - t.Fatalf("%T does not implement storage.VersionedHistoryConfigurer", te.store) - } - configurer.SetVersionedHistoryEnabled(true) - return versionProbe{t: t, kit: newEmbeddedRoleFixtureKit(te, prefix)} -} - -func (p versionProbe) rows(id string) int { - p.t.Helper() - var count int - if err := p.kit.QueryScalar(p.t.Context(), "SELECT COUNT(*) FROM issue_versions WHERE issue_id = ?", []any{id}, &count); err != nil { - p.t.Fatalf("count issue_versions for %s: %v", id, err) - } - return count -} - -func (p versionProbe) currentRevision(id string) int { - p.t.Helper() - var revision int - if err := p.kit.QueryScalar(p.t.Context(), "SELECT current_revision FROM issues WHERE id = ?", []any{id}, &revision); err != nil { - p.t.Fatalf("read current_revision for %s: %v", id, err) - } - return revision -} - -// latestState decodes the highest-revision durable_state for id. -func (p versionProbe) latestState(id string) map[string]any { - p.t.Helper() - var raw string - if err := p.kit.QueryScalar(p.t.Context(), - "SELECT durable_state FROM issue_versions WHERE issue_id = ? ORDER BY revision DESC LIMIT 1", - []any{id}, &raw); err != nil { - p.t.Fatalf("read latest durable_state for %s: %v", id, err) - } - var state map[string]any - if err := json.Unmarshal([]byte(raw), &state); err != nil { - p.t.Fatalf("decode durable_state for %s: %v\n%s", id, err, raw) - } - return state -} - -// expect asserts the version-row count of id moved from before by delta and -// returns the new count, so the steps of a scenario chain. -func (p versionProbe) expect(step, id string, before, delta int) int { - p.t.Helper() - got := p.rows(id) - if got != before+delta { - p.t.Fatalf("%s: version rows for %s = %d, want %d (before %d, delta %+d)", step, id, got, before+delta, before, delta) - } - return got -} - -// revisionMatchesRows pins FR-5 for id: current_revision is nothing but the -// count of rows RecordVersionInTx has minted. -func (p versionProbe) revisionMatchesRows(id string) { - p.t.Helper() - if rev, n := p.currentRevision(id), p.rows(id); rev != n { - p.t.Fatalf("current_revision for %s = %d but it has %d version row(s)", id, rev, n) - } -} - -func dependencyTargets(t *testing.T, state map[string]any) []string { - t.Helper() - raw, _ := state["dependencies"].([]any) - targets := make([]string, 0, len(raw)) - for _, dep := range raw { - m, _ := dep.(map[string]any) - target, _ := m["depends_on_id"].(string) - targets = append(targets, target) - } - return targets -} - -func labelsOf(state map[string]any) []string { - raw, _ := state["labels"].([]any) - labels := make([]string, 0, len(raw)) - for _, l := range raw { - s, _ := l.(string) - labels = append(labels, s) - } - return labels -} - -// TestEmbeddedEveryAcceptedLifecycleMutationMintsOneVersion walks the -// lifecycle verbs the write-path inventory found unversioned — claim, release, -// label add/remove, the legacy dependency verbs, close and reopen — and pins -// one row per accepted change and none for each verb's no-op shape. -func TestEmbeddedEveryAcceptedLifecycleMutationMintsOneVersion(t *testing.T) { - skipUnlessEmbeddedDolt(t) - te := newTestEnv(t, "vermint") - ctx := t.Context() - p := newVersionProbe(t, te, "vermint") - store := te.store - - a, b := p.kit.IssuePrefix+"-a", p.kit.IssuePrefix+"-b" - for _, id := range []string{a, b} { - if err := p.kit.CreateIssue(ctx, &types.Issue{ - ID: id, Title: id, Status: types.StatusOpen, Priority: 2, IssueType: types.TypeTask, - }, "seed"); err != nil { - t.Fatalf("CreateIssue(%s): %v", id, err) - } - } - na := p.expect("seed", a, 0, 1) - nb := p.expect("seed", b, 0, 1) - - // claim: one row for the CAS that wrote assignee/status/started_at; the - // idempotent re-claim by the same actor returns without writing. - if err := store.ClaimIssue(ctx, a, "worker"); err != nil { - t.Fatalf("ClaimIssue: %v", err) - } - na = p.expect("claim", a, na, 1) - if err := store.ClaimIssue(ctx, a, "worker"); err != nil { - t.Fatalf("ClaimIssue (idempotent re-claim): %v", err) - } - na = p.expect("idempotent re-claim", a, na, 0) - - // release: assignee and status revert. - if err := store.UnclaimIssue(ctx, a, "worker", false); err != nil { - t.Fatalf("UnclaimIssue: %v", err) - } - na = p.expect("unclaim", a, na, 1) - - // labels: INSERT IGNORE on an existing label and DELETE of an absent one - // are the no-op shapes. - if err := store.AddLabel(ctx, a, "l1", "actor"); err != nil { - t.Fatalf("AddLabel: %v", err) - } - na = p.expect("add label", a, na, 1) - if labels := labelsOf(p.latestState(a)); len(labels) != 1 || labels[0] != "l1" { - t.Fatalf("add label: latest durable_state labels = %v, want [l1]", labels) - } - if err := store.AddLabel(ctx, a, "l1", "actor"); err != nil { - t.Fatalf("AddLabel (duplicate): %v", err) - } - na = p.expect("duplicate add label", a, na, 0) - if err := store.RemoveLabel(ctx, a, "l1", "actor"); err != nil { - t.Fatalf("RemoveLabel: %v", err) - } - na = p.expect("remove label", a, na, 1) - if err := store.RemoveLabel(ctx, a, "l1", "actor"); err != nil { - t.Fatalf("RemoveLabel (absent): %v", err) - } - na = p.expect("absent remove label", a, na, 0) - - // the legacy store dependency verbs (no DependencyEditor role): the - // referencing issue versions, the target does not. - if err := store.AddDependency(ctx, &types.Dependency{IssueID: a, DependsOnID: b, Type: types.DepBlocks}, "actor"); err != nil { - t.Fatalf("AddDependency: %v", err) - } - na = p.expect("legacy dep add", a, na, 1) - nb = p.expect("legacy dep add (target)", b, nb, 0) - if targets := dependencyTargets(t, p.latestState(a)); len(targets) != 1 || targets[0] != b { - t.Fatalf("legacy dep add: latest durable_state dependencies = %v, want [%s]", targets, b) - } - if err := store.RemoveDependency(ctx, a, b, "actor"); err != nil { - t.Fatalf("RemoveDependency: %v", err) - } - na = p.expect("legacy dep remove", a, na, 1) - if targets := dependencyTargets(t, p.latestState(a)); len(targets) != 0 { - t.Fatalf("legacy dep remove: latest durable_state dependencies = %v, want none", targets) - } - - // close, including the WithoutEvent-style idempotent already-closed - // return, then reopen. - if err := store.CloseIssue(ctx, a, "done", "actor", ""); err != nil { - t.Fatalf("CloseIssue: %v", err) - } - na = p.expect("close", a, na, 1) - if status, _ := p.latestState(a)["status"].(string); status != string(types.StatusClosed) { - t.Fatalf("close: latest durable_state status = %q, want closed", status) - } - if err := store.CloseIssue(ctx, a, "done", "actor", ""); err != nil { - t.Fatalf("CloseIssue (already closed): %v", err) - } - na = p.expect("already-closed close", a, na, 0) - if err := store.ReopenIssue(ctx, a, "", "actor"); err != nil { - t.Fatalf("ReopenIssue: %v", err) - } - p.expect("reopen", a, na, 1) - - p.revisionMatchesRows(a) - p.revisionMatchesRows(b) -} - -// TestEmbeddedMintLeavesUpdatedAtWhereHistoryOffDoes pins that minting a -// version is not itself an edit. issues.updated_at carries ON UPDATE -// CURRENT_TIMESTAMP, so a mint whose own UPDATE left it unassigned would move -// it to the clock on every versioned write, including the label and -// dependency writes that never touch it with history off. bd import's -// stale-upsert check reads that column, so a moved updated_at would also turn -// the source's next real edit away as stale. Each scenario runs with history -// off first, as the control the history-on run must match. -func TestEmbeddedMintLeavesUpdatedAtWhereHistoryOffDoes(t *testing.T) { - skipUnlessEmbeddedDolt(t) - // Fixed instants well before any clock this runs under, so a mint that - // wrote the clock cannot pass for any of them. - aged := time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC) - sourceEdit := time.Date(2025, 2, 1, 0, 0, 0, 0, time.UTC) - laterSourceEdit := time.Date(2025, 3, 1, 0, 0, 0, 0, time.UTC) - - for _, history := range []struct { - name string - on bool - }{{"HistoryOff", false}, {"HistoryOn", true}} { - t.Run(history.name, func(t *testing.T) { - te := newTestEnv(t, "verts") - ctx := t.Context() - te.store.SetVersionedHistoryEnabled(history.on) - // With history on every step below must mint exactly one version, - // so a mint that never ran cannot pass for one that kept updated_at. - mints := 0 - if history.on { - mints = 1 - } - - a, b := "verts-a", "verts-b" - for _, id := range []string{a, b} { - if err := te.store.CreateIssue(ctx, &types.Issue{ - ID: id, Title: id, Status: types.StatusOpen, Priority: 2, IssueType: types.TypeTask, - }, "seed"); err != nil { - t.Fatalf("CreateIssue(%s): %v", id, err) - } - } - age := func() { te.exec(t, ctx, "UPDATE issues SET updated_at = ? WHERE id = ?", aged, a) } - versions := func() int { - t.Helper() - var n int - te.queryScalar(t, ctx, "SELECT COUNT(*) FROM issue_versions WHERE issue_id = ?", []any{a}, &n) - return n - } - // check asserts a's updated_at and that the step minted as the - // history setting says, and returns the new version count. - check := func(step string, before int, want time.Time) int { - t.Helper() - var got time.Time - te.queryScalar(t, ctx, "SELECT updated_at FROM issues WHERE id = ?", []any{a}, &got) - if !got.UTC().Equal(want) { - t.Errorf("%s: updated_at for %s is %v, want %v", step, a, got.UTC(), want) - } - n := versions() - if n != before+mints { - t.Fatalf("%s: version rows for %s = %d, want %d", step, a, n, before+mints) - } - return n - } - - // Label and dependency writes never assign updated_at themselves. - age() - n := versions() - if err := te.store.AddLabel(ctx, a, "l1", "actor"); err != nil { - t.Fatalf("AddLabel: %v", err) - } - n = check("add label", n, aged) - if err := te.store.RemoveLabel(ctx, a, "l1", "actor"); err != nil { - t.Fatalf("RemoveLabel: %v", err) - } - n = check("remove label", n, aged) - if err := te.store.AddDependency(ctx, &types.Dependency{IssueID: a, DependsOnID: b, Type: types.DepBlocks}, "actor"); err != nil { - t.Fatalf("AddDependency: %v", err) - } - n = check("add dependency", n, aged) - if err := te.store.RemoveDependency(ctx, a, b, "actor"); err != nil { - t.Fatalf("RemoveDependency: %v", err) - } - n = check("remove dependency", n, aged) - - // bd import's options. The upsert stores the source's own - // updated_at, and the source's next edit is compared against it. - // Aged again so this half does not depend on the half above. - age() - var rejected []string - opts := storage.BatchCreateOptions{ - RejectStaleUpserts: true, - SkipDependencyValidationErrors: true, - OnStaleRejected: func(id string) { rejected = append(rejected, id) }, - } - importEdit := func(title string, at time.Time) { - t.Helper() - if err := te.store.CreateIssuesWithFullOptions(ctx, []*types.Issue{{ - ID: a, Title: title, Status: types.StatusOpen, Priority: 2, IssueType: types.TypeTask, - CreatedAt: aged, UpdatedAt: at, - }}, "importer", opts); err != nil { - t.Fatalf("import %q: %v", title, err) - } - if len(rejected) != 0 { - t.Fatalf("import %q with source updated_at %v was rejected as stale (%v)", title, at, rejected) - } - } - importEdit("source edit", sourceEdit) - n = check("import", n, sourceEdit) - importEdit("later source edit", laterSourceEdit) - check("later import", n, laterSourceEdit) - }) - } -} - -// TestEmbeddedBatchCreateFirstVersionCarriesCreationTimeEdges pins the -// batch-create ordering fix: the one version minted at creation is minted -// AFTER PersistDependenciesWithOptionsResult, so it carries the outgoing edge -// set, and the dependency pass mints nothing of its own. -func TestEmbeddedBatchCreateFirstVersionCarriesCreationTimeEdges(t *testing.T) { - skipUnlessEmbeddedDolt(t) - te := newTestEnv(t, "vercreate") - ctx := t.Context() - p := newVersionProbe(t, te, "vercreate") - - src, tgt := p.kit.IssuePrefix+"-src", p.kit.IssuePrefix+"-tgt" - issues := []*types.Issue{ - {ID: src, Title: src, Status: types.StatusOpen, Priority: 2, IssueType: types.TypeTask, - Dependencies: []*types.Dependency{{IssueID: src, DependsOnID: tgt, Type: types.DepBlocks}}}, - {ID: tgt, Title: tgt, Status: types.StatusOpen, Priority: 2, IssueType: types.TypeTask}, - } - if err := te.store.CreateIssues(ctx, issues, "actor"); err != nil { - t.Fatalf("CreateIssues: %v", err) - } - p.expect("batch create", src, 0, 1) - p.expect("batch create", tgt, 0, 1) - if targets := dependencyTargets(t, p.latestState(src)); len(targets) != 1 || targets[0] != tgt { - t.Fatalf("batch create: the first version's durable_state dependencies = %v, want [%s]: "+ - "the mint must run after the creation-time edges are persisted", targets, tgt) - } - p.revisionMatchesRows(src) - p.revisionMatchesRows(tgt) -} - -// TestEmbeddedGuardedUpdateMintsOnceAfterItsPatches pins the ExecuteUpdate -// ordering fix: a guarded update carrying a field edit, a label patch and a -// parent patch mints ONE version, whose durable_state carries all three, and -// a guarded update that changes nothing mints none. -func TestEmbeddedGuardedUpdateMintsOnceAfterItsPatches(t *testing.T) { - skipUnlessEmbeddedDolt(t) - te := newTestEnv(t, "verupd") - ctx := t.Context() - p := newVersionProbe(t, te, "verupd") - - child, parent := p.kit.IssuePrefix+"-child", p.kit.IssuePrefix+"-parent" - for _, id := range []string{child, parent} { - if err := p.kit.CreateIssue(ctx, &types.Issue{ - ID: id, Title: id, Status: types.StatusOpen, Priority: 2, IssueType: types.TypeTask, - }, "seed"); err != nil { - t.Fatalf("CreateIssue(%s): %v", id, err) - } - } - lifecycle, err := te.store.IssueLifecycle() - if err != nil { - t.Fatalf("IssueLifecycle(): %v", err) - } - - n := p.rows(child) - if _, err := lifecycle.Update(ctx, issueops.UpdateRequest{ - Actor: "actor", IssueID: child, - Patch: issueops.IssuePatch{ - Title: issueops.Field[string]{Set: true, Value: "renamed"}, - Labels: issueops.LabelPatch{Add: []string{"patched"}}, - ParentID: issueops.Field[string]{Set: true, Value: parent}, - }, - }); err != nil { - t.Fatalf("Lifecycle.Update: %v", err) - } - n = p.expect("guarded update with field + label + parent patches", child, n, 1) - state := p.latestState(child) - if title, _ := state["title"].(string); title != "renamed" { - t.Fatalf("guarded update: durable_state title = %q, want renamed", title) - } - if labels := labelsOf(state); len(labels) != 1 || labels[0] != "patched" { - t.Fatalf("guarded update: durable_state labels = %v, want [patched]: the label patch must land before the mint", labels) - } - if targets := dependencyTargets(t, state); len(targets) != 1 || targets[0] != parent { - t.Fatalf("guarded update: durable_state dependencies = %v, want [%s]: the parent patch must land before the mint", targets, parent) - } - - // The same request again changes nothing: the field is a no-op, the label - // set already matches and the parent is already set. - if _, err := lifecycle.Update(ctx, issueops.UpdateRequest{ - Actor: "actor", IssueID: child, - Patch: issueops.IssuePatch{ - Title: issueops.Field[string]{Set: true, Value: "renamed"}, - Labels: issueops.LabelPatch{Add: []string{"patched"}}, - ParentID: issueops.Field[string]{Set: true, Value: parent}, - }, - }); err != nil { - t.Fatalf("Lifecycle.Update (no-op): %v", err) - } - p.expect("no-op guarded update", child, n, 0) - p.revisionMatchesRows(child) - p.revisionMatchesRows(parent) -} diff --git a/internal/storage/embeddeddolt/version_control.go b/internal/storage/embeddeddolt/version_control.go index 124ad798c..130cf8894 100644 --- a/internal/storage/embeddeddolt/version_control.go +++ b/internal/storage/embeddeddolt/version_control.go @@ -197,30 +197,10 @@ func (s *EmbeddedDoltStore) CommitMergeResolution(ctx context.Context, message s return err } -// CommitConfigOnly commits the config table (GH#4078). Embedded mode is -// single-process, so the '-Am' Commit cannot sweep a concurrent operation's -// working set — the scoped-staging concern that exists in server mode -// (GH#2455) does not apply here, and this aliases Commit for the same -// reason CommitWithConfig does. -func (s *EmbeddedDoltStore) CommitConfigOnly(ctx context.Context, message string) error { - return s.Commit(ctx, message) -} - -// CommitConfigUserKVOnly aliases Commit for the reason CommitConfigOnly does: -// with no concurrent writer, there is no other writer's config row to screen out. -func (s *EmbeddedDoltStore) CommitConfigUserKVOnly(ctx context.Context, message string) error { - return s.Commit(ctx, message) -} - func (s *EmbeddedDoltStore) AddRemote(ctx context.Context, name, url string) error { - return s.AddRemoteWithRef(ctx, name, url, "") -} - -// AddRemoteWithRef adds a remote whose Dolt data lives on the git ref ref; -// see storage.RemoteStore. -func (s *EmbeddedDoltStore) AddRemoteWithRef(ctx context.Context, name, url, ref string) error { return s.withMutatingDBConn(ctx, func(db versioncontrolops.DBConn) error { - return versioncontrolops.AddRemote(ctx, db, name, url, ref) + _, err := db.ExecContext(ctx, "CALL DOLT_REMOTE('add', ?, ?)", name, url) + return err }) } @@ -553,14 +533,6 @@ func (s *EmbeddedDoltStore) ListRemotes(ctx context.Context) ([]storage.RemoteIn // where before it read it holding no lock at all. func (s *EmbeddedDoltStore) Push(ctx context.Context) error { - // GH#5433: every Pull variant below auto-commits pending changes first; - // push never did, so a working set with pending-but-uncommitted changes - // (auto-commit off, or a session that ended right after a write with no - // intervening commit) silently pushed nothing new and still reported - // success. - if _, err := s.CommitPending(ctx, "beads"); err != nil { - return fmt.Errorf("commit pending before push: %w", err) - } return s.withPeerAuth(ctx, defaultRemote, func(user string) error { return s.withMutatingDBConn(ctx, func(db versioncontrolops.DBConn) error { return vcPush(ctx, db, defaultRemote, s.branch, user) @@ -626,10 +598,6 @@ func (s *EmbeddedDoltStore) PullRemoteWithStrategy(ctx context.Context, remote, } func (s *EmbeddedDoltStore) ForcePush(ctx context.Context) error { - // GH#5433: see Push. - if _, err := s.CommitPending(ctx, "beads"); err != nil { - return fmt.Errorf("commit pending before push: %w", err) - } return s.withPeerAuth(ctx, defaultRemote, func(user string) error { return s.withMutatingDBConn(ctx, func(db versioncontrolops.DBConn) error { return vcForcePush(ctx, db, defaultRemote, s.branch, user) @@ -638,10 +606,6 @@ func (s *EmbeddedDoltStore) ForcePush(ctx context.Context) error { } func (s *EmbeddedDoltStore) PushRemote(ctx context.Context, remote string, force bool) error { - // GH#5433: see Push. - if _, err := s.CommitPending(ctx, "beads"); err != nil { - return fmt.Errorf("commit pending before push: %w", err) - } return s.withPeerAuth(ctx, remote, func(user string) error { return s.withMutatingDBConn(ctx, func(db versioncontrolops.DBConn) error { if force { @@ -678,10 +642,6 @@ func (s *EmbeddedDoltStore) Fetch(ctx context.Context, peer string) error { } func (s *EmbeddedDoltStore) PushTo(ctx context.Context, peer string) error { - // GH#5433: see Push. - if _, err := s.CommitPending(ctx, "beads"); err != nil { - return fmt.Errorf("commit pending before push: %w", err) - } return s.withPeerAuth(ctx, peer, func(user string) error { return s.withMutatingDBConn(ctx, func(db versioncontrolops.DBConn) error { return versioncontrolops.Push(ctx, db, peer, s.branch, user) diff --git a/internal/storage/embeddeddolt/version_subsecond_test.go b/internal/storage/embeddeddolt/version_subsecond_test.go deleted file mode 100644 index 9b672caaf..000000000 --- a/internal/storage/embeddeddolt/version_subsecond_test.go +++ /dev/null @@ -1,99 +0,0 @@ -//go:build cgo - -package embeddeddolt - -import ( - "context" - "database/sql" - "testing" - "time" -) - -// TestRecordVersionKeepsSubSecondChangeAt pins the WRITER half of migration -// 0069's contract: a sub-second change_at must survive through -// RecordVersionAtInTx into the row and back. -// -// 0069 widened issue_versions.change_at to DATETIME(6) precisely so that two -// versions minted inside the same second stay distinguishable, because -// --at is the only PORTABLE selector the read surface offers while -// revision remains a per-store ordinal and no durable version address has -// shipped. The column's own half is pinned by -// TestMigration0069ChangeAtSurvivesSubSecondPrecisionThroughDoltCLI in -// internal/storage/schema. The writer's half was NOT pinned, and that is -// exactly how it got defeated: recordVersionAtInTx carried an -// `at.Truncate(time.Second)` — correct while the column was precision 0, -// where Dolt rounds half-up — which kept flooring every value after the -// widening landed. A store with both applied stored four versions minted in -// one second as the identical 15:40:38.000000. -// -// So this test asserts the two things a caller actually depends on: the -// microseconds are there, and same-second versions differ. -func TestRecordVersionKeepsSubSecondChangeAt(t *testing.T) { - skipUnlessEmbeddedDolt(t) - - ctx := context.Background() - store, cleanup, err := buildAsOfReadStore(ctx) - if err != nil { - t.Fatalf("build store: %v", err) - } - t.Cleanup(cleanup) - - h := newAsOfReadHarness(t) - h.stores["subsecond"] = store - - const id = "vt-subsec" - // Two instants inside the SAME second, a quarter of a second apart. Under - // the old floor both landed on :07.000000; under a precision-0 column the - // second would additionally have rounded up to :08. - first := time.Date(2026, 9, 27, 11, 4, 7, 250000000, time.UTC) - second := time.Date(2026, 9, 27, 11, 4, 7, 750000000, time.UTC) - - if _, err := h.mintAt(ctx, "subsecond", id, first); err != nil { - t.Fatalf("mint at %s: %v", first, err) - } - if _, err := h.mintAt(ctx, "subsecond", id, second); err != nil { - t.Fatalf("mint at %s: %v", second, err) - } - - var stamps []time.Time - if err := store.withConn(ctx, false, func(tx *sql.Tx) error { - rows, err := tx.QueryContext(ctx, - `SELECT change_at FROM issue_versions WHERE issue_id = ? ORDER BY revision`, id) - if err != nil { - return err - } - defer func() { _ = rows.Close() }() - for rows.Next() { - var at time.Time - if err := rows.Scan(&at); err != nil { - return err - } - stamps = append(stamps, at) - } - return rows.Err() - }); err != nil { - t.Fatalf("read change_at back: %v", err) - } - - if len(stamps) < 2 { - t.Fatalf("got %d version rows for %s, want at least 2", len(stamps), id) - } - // The harness mints a creation version too, so take the last two. - got := stamps[len(stamps)-2:] - - for i, at := range got { - if at.Nanosecond() == 0 { - t.Errorf("change_at[%d] = %s has a zero fractional part; the sub-second component was dropped between the caller and the row. "+ - "If recordVersionAtInTx floors `at` again, or the column reverts to DATETIME(0), --at can no longer separate versions minted in the same second.", - i, at.Format("15:04:05.000000")) - } - } - if got[0].Equal(got[1]) { - t.Errorf("two versions minted 500ms apart both stored %s — same-second versions must stay distinguishable by change_at, "+ - "which is what migration 0069 widened the column for", got[0].Format("15:04:05.000000")) - } - if !got[1].After(got[0]) { - t.Errorf("change_at went backwards or sideways: %s then %s", - got[0].Format("15:04:05.000000"), got[1].Format("15:04:05.000000")) - } -} diff --git a/internal/storage/embeddeddolt/versioned_read_contract_test.go b/internal/storage/embeddeddolt/versioned_read_contract_test.go deleted file mode 100644 index 709e02e3c..000000000 --- a/internal/storage/embeddeddolt/versioned_read_contract_test.go +++ /dev/null @@ -1,309 +0,0 @@ -//go:build cgo - -package embeddeddolt - -import ( - "context" - "database/sql" - "fmt" - "os" - "path/filepath" - "testing" - "time" - - "github.com/steveyegge/beads/backend/conformance" - "github.com/steveyegge/beads/internal/storage/issueops" - "github.com/steveyegge/beads/internal/types" -) - -// asOfReadHarness is this leg's Design D dispatch table (gastownhall/beads#5898 -// revision 9, gastownhall/beads#6136): the conformance suite -// invents storeID values to select among N genuinely isolated stores, and -// isolating them is entirely this harness's job -- AsOfReadInTx itself is -// storeID-oblivious (see the package doc on -// internal/storage/issueops/asof_read.go). Each storeID gets its own fresh -// *EmbeddedDoltStore, built lazily on first use (buildAsOfReadStore) and torn -// down via t.Cleanup on the TestAsOfReadContract-level *testing.T. created -// tracks which (storeID, id) pairs already have a base issues row, since -// mintAt's second and later calls for the same pair must only add a -// historical issue_versions row, never a second base create. -// -// store() cannot use a *testing.T-tied fixture helper (e.g. -// newPristineEmbeddedDoltFixture): AsOfReadFixture's hook types -// (Resolve/MintAt/MakeUnanswerable) carry no *testing.T of their own, so a -// store built lazily inside a hook -- itself invoked from inside a t.Run -// subtest closure, sometimes nested two deep (the restriction-loop case -// below) -- has access only to whichever *testing.T this harness was -// constructed with: necessarily the OUTER TestAsOfReadContract's t, captured -// before any t.Run. Calling FailNow/SkipNow (t.Fatalf/t.Skip) on a *testing.T -// from a goroutine other than the one running that exact test -- which a -// t.Run subtest's closure always is, relative to the outer test -- panics -// with "subtest may have called FailNow on a parent test" (go's testing -// package requires FailNow/SkipNow to run only on the goroutine executing -// that specific test). buildAsOfReadStore below is a Fatalf/Skip-free -// construction path for exactly this reason: its failures come back as a -// plain error, which flows through the calling hook's own (..., error) -// return to conformance.RunAsOfReadXXX, which correctly Fatalfs using the -// SUBTEST's own *testing.T on the SUBTEST's own goroutine. Embedded-Dolt -// availability skipping itself is handled once, up front in -// TestAsOfReadContract's own body via skipUnlessEmbeddedDolt(t) -- safe there -// because that runs before any t.Run, in the one goroutine that actually owns -// t. See internal/storage/dolt/versioned_read_contract_test.go for this -// leg's sibling, which this file mirrors as closely as the two backends' -// store-construction APIs allow. -type asOfReadHarness struct { - t *testing.T - stores map[string]*EmbeddedDoltStore - created map[[2]string]bool -} - -func newAsOfReadHarness(t *testing.T) *asOfReadHarness { - return &asOfReadHarness{ - t: t, - stores: map[string]*EmbeddedDoltStore{}, - created: map[[2]string]bool{}, - } -} - -func (h *asOfReadHarness) store(ctx context.Context, storeID string) (*EmbeddedDoltStore, error) { - if s, ok := h.stores[storeID]; ok { - return s, nil - } - store, cleanup, err := buildAsOfReadStore(ctx) - if err != nil { - return nil, err - } - // t.Cleanup only appends to a mutex-guarded slice -- unlike Fatalf/Skip it - // never calls runtime.Goexit, so registering it on the outer h.t from - // inside a subtest's goroutine is safe. h.t's own cleanups run only after - // ALL of TestAsOfReadContract's t.Run calls have returned (none of them, - // nor their own nested t.Run calls, ever invoke t.Parallel), so every - // store this harness builds stays open for the whole test, matching - // created's own whole-test lifetime. - h.t.Cleanup(cleanup) - h.stores[storeID] = store - return store, nil -} - -// buildAsOfReadStore constructs one fresh, isolated *EmbeddedDoltStore: its -// own temp directory (so its own on-disk database, with nothing shared -// between stores -- unlike the dolt leg's single testcontainers server, an -// embedded store's isolation is just its own directory, so a fixed database -// name is safe to reuse across every call), issue_prefix seeded. Minus every -// t.Fatalf/t.Skip call; see the asOfReadHarness doc comment for why no call -// in this file may make one outside TestAsOfReadContract's own body. -func buildAsOfReadStore(ctx context.Context) (*EmbeddedDoltStore, func(), error) { - tmpDir, err := os.MkdirTemp("", "embeddeddolt-asof-test-*") - if err != nil { - return nil, nil, fmt.Errorf("as-of read fixture: create temp dir: %w", err) - } - - beadsDir := filepath.Join(tmpDir, ".beads") - store, err := Open(ctx, beadsDir, "asof", "main") - if err != nil { - os.RemoveAll(tmpDir) - return nil, nil, fmt.Errorf("as-of read fixture: open embedded Dolt store: %w", err) - } - - if err := store.SetConfig(ctx, "issue_prefix", "asof"); err != nil { - store.Close() - os.RemoveAll(tmpDir) - return nil, nil, fmt.Errorf("as-of read fixture: set issue_prefix: %w", err) - } - - cleanup := func() { - store.Close() - os.RemoveAll(tmpDir) - } - return store, cleanup, nil -} - -// ensureCreated bare-creates id in storeID's store the first time it is -// asked for, giving RecordVersionAtInTx (called from mintAt) a base issues -// row to snapshot -- recordVersionAtInTx starts with GetIssueInTx, so it has -// nothing to read until this has run once. -func (h *asOfReadHarness) ensureCreated(ctx context.Context, storeID, id string) error { - key := [2]string{storeID, id} - if h.created[key] { - return nil - } - store, err := h.store(ctx, storeID) - if err != nil { - return err - } - issue := &types.Issue{ - ID: id, - Title: "as-of read fixture issue", - Status: types.StatusOpen, - Priority: 3, - IssueType: types.TypeTask, - } - if err := store.CreateIssue(ctx, issue, "asof-read-fixture"); err != nil { - return fmt.Errorf("as-of read fixture: bare-create %s: %w", id, err) - } - h.created[key] = true - return nil -} - -func (h *asOfReadHarness) resolve(ctx context.Context, storeID, id string, selector conformance.AsOfSelector) (conformance.AsOfReadResult, error) { - store, err := h.store(ctx, storeID) - if err != nil { - return conformance.AsOfReadResult{}, err - } - var result issueops.AsOfReadResult - err = store.withConn(ctx, false, func(tx *sql.Tx) error { - var err error - result, err = issueops.AsOfReadInTx(ctx, tx, storeID, id, toIssueopsSelector(selector)) - return err - }) - if err != nil { - return conformance.AsOfReadResult{}, err - } - return toConformanceResult(result), nil -} - -// mintAt is the two-phase implementation MintAt's contract needs: phase one -// (ensureCreated) runs only once per (storeID, id); phase two -// (RecordVersionAtInTx) runs every call and always supplies the row mintAt -// reports the address of. RecordVersionAtInTx deliberately does not gate on -// versionedHistoryEnabled (this harness never turns that on for its stores), -// so it always mints, and reading MAX(revision) back inside the same -// transaction (read-your-writes) is how we learn which revision it picked -- -// RecordVersionAtInTx itself reports no revision number. -func (h *asOfReadHarness) mintAt(ctx context.Context, storeID, id string, acceptedAt time.Time) (conformance.Address, error) { - if err := h.ensureCreated(ctx, storeID, id); err != nil { - return "", err - } - store, err := h.store(ctx, storeID) - if err != nil { - return "", err - } - var revision int64 - err = store.withConn(ctx, true, func(tx *sql.Tx) error { - if err := issueops.RecordVersionAtInTx(ctx, tx, id, "asof-read-fixture", acceptedAt); err != nil { - return err - } - return tx.QueryRowContext(ctx, - `SELECT MAX(revision) FROM issue_versions WHERE issue_id = ?`, id, - ).Scan(&revision) - }) - if err != nil { - return "", err - } - return conformance.Address(issueops.AsOfVersionAddress(id, revision)), nil -} - -func (h *asOfReadHarness) makeUnanswerable(ctx context.Context, storeID, id string, restriction conformance.Restriction) error { - asOfRestriction, ok := conformanceToAsOfRestriction[restriction] - if !ok { - return fmt.Errorf("as-of read fixture: unmapped restriction %v", restriction) - } - store, err := h.store(ctx, storeID) - if err != nil { - return err - } - reason := fmt.Sprintf("as-of read fixture: made unanswerable as %s", restriction) - return store.withConn(ctx, true, func(tx *sql.Tx) error { - return issueops.MarkLatestVersionRemovedInTx(ctx, tx, id, asOfRestriction, reason) - }) -} - -// conformanceToAsOfRestriction and its inverse are this leg's translation -// boundary between backend/conformance.Restriction (hyphenated String(), -// int-backed) and issueops.AsOfRestriction (underscored, string-backed -- -// its values ARE the removed_restriction column's contents). issueops must -// stay conformance-oblivious and conformance must stay backend-oblivious -// (internal/storage/issueops/asof_read.go's package doc), so this mapping is -// deliberately duplicated per leg rather than shared centrally. -var conformanceToAsOfRestriction = map[conformance.Restriction]issueops.AsOfRestriction{ - conformance.RestrictionLive: issueops.AsOfRestrictionLive, - conformance.RestrictionGoneRetention: issueops.AsOfRestrictionGoneRetention, - conformance.RestrictionGoneErasure: issueops.AsOfRestrictionGoneErasure, - conformance.RestrictionGoneReorganization: issueops.AsOfRestrictionGoneReorganization, - conformance.RestrictionUnknown: issueops.AsOfRestrictionUnknown, -} - -var asOfRestrictionToConformance = map[issueops.AsOfRestriction]conformance.Restriction{ - issueops.AsOfRestrictionLive: conformance.RestrictionLive, - issueops.AsOfRestrictionGoneRetention: conformance.RestrictionGoneRetention, - issueops.AsOfRestrictionGoneErasure: conformance.RestrictionGoneErasure, - issueops.AsOfRestrictionGoneReorganization: conformance.RestrictionGoneReorganization, - issueops.AsOfRestrictionUnknown: conformance.RestrictionUnknown, -} - -func toIssueopsSelector(selector conformance.AsOfSelector) issueops.AsOfSelector { - out := issueops.AsOfSelector{At: selector.At} - if selector.Version != nil { - out.Address = string(*selector.Version) - } - return out -} - -func toConformanceResult(result issueops.AsOfReadResult) conformance.AsOfReadResult { - if result.Refused { - return conformance.AsOfReadResult{ - Refusal: &conformance.AsOfReadRefusal{ - Restriction: asOfRestrictionToConformance[result.Restriction], - Reason: result.Reason, - }, - } - } - return conformance.AsOfReadResult{ - Address: conformance.Address(result.Address), - State: result.State, - } -} - -// skipUnlessEmbeddedDolt is this file's own copy of the embeddeddolt_test -// package's helper of the same name (create_issue_test.go): that one is -// unexported and lives in the external test package, unreachable from here. -// This harness must live in-package (see the asOfReadHarness doc comment for -// why: raw DBTX access requires the unexported withConn), so it carries its -// own copy rather than importing across the package/package_test boundary. -// Reads the same BEADS_TEST_EMBEDDED_DOLT env var so `go test -// ./internal/storage/embeddeddolt/...` gates uniformly regardless of which -// half of the package a given test file lives in. -func skipUnlessEmbeddedDolt(t *testing.T) { - t.Helper() - if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" { - t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt tests") - } -} - -// TestAsOfReadContract wires this leg into the R7.1 as-of-read contract -// (gastownhall/beads#5898 revision 9, gastownhall/beads#6136) with a real -// Resolve/MintAt/MakeUnanswerable backed by one isolated *EmbeddedDoltStore -// per storeID (asOfReadHarness). -func TestAsOfReadContract(t *testing.T) { - // Must run before any t.Run: skipUnlessEmbeddedDolt calls t.Skip - // internally, and this is the one place in this file where that is safe - // -- the outer test's own goroutine, before any subtest closure exists. - // See the asOfReadHarness doc comment for why no other call in this file - // may do the same. - skipUnlessEmbeddedDolt(t) - - ctx := context.Background() - h := newAsOfReadHarness(t) - fixture := conformance.AsOfReadFixture{ - IssuePrefix: "asof", - Resolve: h.resolve, - MintAt: h.mintAt, - MakeUnanswerable: h.makeUnanswerable, - } - - t.Run("SelectorAcceptsEitherAnInstantOrAVersionAddress", func(t *testing.T) { - conformance.RunAsOfReadSelectorAcceptsEitherAnInstantOrAVersionAddress(t, ctx, fixture) - }) - t.Run("ReturnsTheLatestVersionAtOrBeforeTStrictlyExcludingLater", func(t *testing.T) { - conformance.RunAsOfReadReturnsTheLatestVersionAtOrBeforeTStrictlyExcludingLater(t, ctx, fixture) - }) - t.Run("ReportsTheAddressItServed", func(t *testing.T) { - conformance.RunAsOfReadReportsTheAddressItServed(t, ctx, fixture) - }) - t.Run("RefusesAsGoneOrUnknownNeverSubstitutingAnotherVersion", func(t *testing.T) { - conformance.RunAsOfReadRefusesAsGoneOrUnknownNeverSubstitutingAnotherVersion(t, ctx, fixture) - }) - t.Run("IsCompleteOnlyForTheAnsweringStoresHoldings", func(t *testing.T) { - conformance.RunAsOfReadIsCompleteOnlyForTheAnsweringStoresHoldings(t, ctx, fixture) - }) -} diff --git a/internal/storage/externaldeps/BUILD.bazel b/internal/storage/externaldeps/BUILD.bazel deleted file mode 100644 index 1075b6435..000000000 --- a/internal/storage/externaldeps/BUILD.bazel +++ /dev/null @@ -1,127 +0,0 @@ -load("@rules_go//go:def.bzl", "go_library", "go_test") - -go_library( - name = "externaldeps", - srcs = [ - "batch_closer.go", - "blocked_lister.go", - "decorator.go", - "remote_roles.go", - "resolver.go", - "role_accessors.go", - "uow_decorator.go", - ], - importpath = "github.com/steveyegge/beads/internal/storage/externaldeps", - visibility = ["//:__subpackages__"], - deps = [ - "//internal/storage", - "//internal/storage/domain", - "//internal/storage/issueops", - "//internal/storage/uow", - "//internal/types", - "//internal/workapi", - "//internal/workapi/storereader", - "//internal/workapi/storereadycounter", - "//issueops", - "//memoryops", - ], -) - -go_test( - name = "externaldeps_test", - srcs = [ - "advertise_test.go", - "blocked_lister_test.go", - "blocking_annotator_batch_test.go", - "decorator_test.go", - "live_probe_test.go", - "ready_wake_test.go", - "remote_roles_composition_test.go", - "remote_roles_test.go", - "uow_decorator_test.go", - ], - embed = [":externaldeps"], - deps = [ - "//internal/httpapi", - "//internal/storage", - "//internal/storage/domain", - "//internal/storage/issueops", - "//internal/storage/uow", - "//internal/types", - "//issueops", - ], -) - -# --- begin go_srcs (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) --- - -filegroup( - name = "go_srcs", - srcs = glob( - ["**/*.go"], - allow_empty = True, - ), - visibility = ["//:__subpackages__"], -) - -# --- end go_srcs --- - -# --- begin repo_files (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) --- - -filegroup( - name = "repo_doc_files", - srcs = glob( - [ - "**/*.md", - "**/*.mdx", - ], - allow_empty = True, - exclude = ["**/node_modules/**"], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_go_srcs", - srcs = glob( - ["**/*.go"], - allow_empty = True, - exclude = [ - "**/*_test.go", - "**/node_modules/**", - ], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_go_test_srcs", - srcs = glob( - ["**/*_test.go"], - allow_empty = True, - exclude = ["**/node_modules/**"], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_other_files", - srcs = glob( - ["**"], - exclude = [ - "**/*.db", - "**/*.exe", - "**/*.go", - "**/*.md", - "**/*.mdx", - "**/*.out", - "**/*.prof", - "**/*.pyc", - "**/*.test", - "**/__pycache__/**", - "**/node_modules/**", - ], - ), - visibility = ["//:__subpackages__"], -) - -# --- end repo_files --- diff --git a/internal/storage/externaldeps/advertise_test.go b/internal/storage/externaldeps/advertise_test.go deleted file mode 100644 index 83d8c0276..000000000 --- a/internal/storage/externaldeps/advertise_test.go +++ /dev/null @@ -1,82 +0,0 @@ -package externaldeps - -import ( - "bytes" - "context" - "encoding/json" - "net/http" - "slices" - "testing" - "time" - - "github.com/steveyegge/beads/internal/httpapi" - "github.com/steveyegge/beads/internal/storage/uow" -) - -// TestPolicyLayerIsAdvertisedOnTheHandshake closes the loop between this -// package and the server's handshake, the composition `bd serve` performs -// (cmd/bd/serve.go): the policy layer found by AppliedToProvider becomes -// httpapi.Config.ExternalDependencyPolicy, and the handshake must then publish -// httpapi.CapExternalDependencies. A client reads that token to stand its own -// policy down (remote_roles.go), so a server composed through the policy that -// never says so makes every client apply the policy a second time, with the -// CLIENT machine's project config; one that says so without the layer makes -// every client skip it. -func TestPolicyLayerIsAdvertisedOnTheHandshake(t *testing.T) { - plain := &fakeUOWProvider{} - for _, tc := range []struct { - name string - provider uow.UnitOfWorkProvider - advertise bool - }{ - {"composed through the policy", WrapUOWProvider(plain, nil, nil), true}, - {"raw provider", plain, false}, - } { - t.Run(tc.name, func(t *testing.T) { - caps := handshakeCapabilities(t, httpapi.Config{ - Provider: tc.provider, - ExternalDependencyPolicy: AppliedToProvider(tc.provider), - }) - if got := slices.Contains(caps, httpapi.CapExternalDependencies); got != tc.advertise { - t.Fatalf("handshake advertises %s = %v, want %v: %v", httpapi.CapExternalDependencies, got, tc.advertise, caps) - } - }) - } -} - -func handshakeCapabilities(t *testing.T, cfg httpapi.Config) []string { - t.Helper() - cfg.Addr = "127.0.0.1:0" - cfg.Stdout, cfg.Stderr = &bytes.Buffer{}, &bytes.Buffer{} - srv, err := httpapi.Listen(cfg) - if err != nil { - t.Fatalf("Listen: %v", err) - } - ctx, cancel := context.WithCancel(context.Background()) - done := make(chan error, 1) - go func() { done <- srv.Serve(ctx) }() - t.Cleanup(func() { - cancel() - select { - case <-done: - case <-time.After(30 * time.Second): - t.Error("Serve did not return after shutdown") - } - }) - client := &http.Client{Timeout: 20 * time.Second} - resp, err := client.Get("http://" + srv.Addr() + "/v0/beads/context") - if err != nil { - t.Fatalf("GET /v0/beads/context: %v", err) - } - defer func() { _ = resp.Body.Close() }() - if resp.StatusCode != http.StatusOK { - t.Fatalf("GET /v0/beads/context: status %d", resp.StatusCode) - } - var body struct { - Capabilities []string `json:"capabilities"` - } - if err := json.NewDecoder(resp.Body).Decode(&body); err != nil { - t.Fatalf("decode the handshake: %v", err) - } - return body.Capabilities -} diff --git a/internal/storage/externaldeps/batch_closer.go b/internal/storage/externaldeps/batch_closer.go deleted file mode 100644 index c910653be..000000000 --- a/internal/storage/externaldeps/batch_closer.go +++ /dev/null @@ -1,74 +0,0 @@ -package externaldeps - -import ( - "context" - - "github.com/steveyegge/beads/internal/storage" - storageissueops "github.com/steveyegge/beads/internal/storage/issueops" - "github.com/steveyegge/beads/internal/workapi" - "github.com/steveyegge/beads/issueops" -) - -// BatchCloser preserves external policy on bd close, including its next claim. -// Over a remote store, remoteBatchCloser refuses the next claim unless the -// server applies the policy itself. -func (s *Store) BatchCloser() (issueops.BatchCloser, error) { - if s.rolesAreRemote() { - inner, err := s.inner.BatchCloser() - if err != nil { - return nil, err - } - return &remoteBatchCloser{inner: inner, policy: s}, nil - } - return &batchCloser{policy: s}, nil -} - -type batchCloser struct{ policy *Store } - -func (c *batchCloser) CloseBatch(ctx context.Context, request issueops.CloseBatchRequest) (issueops.CloseBatchResult, error) { - ctx = storage.WithLivePolicyProbe(ctx) - if err := storageissueops.ValidateCloseBatchRequest(request); err != nil { - return issueops.CloseBatchResult{}, err - } - if request.ClaimNext != nil { - if _, err := workapi.BuildReadyFilter(*request.ClaimNext); err != nil { - return issueops.CloseBatchResult{}, err - } - } - var policy storage.BatchClosePolicy - switch { - case request.ClaimNext != nil: - // The claim-after-close picks among every ready issue, so it needs the - // workspace-wide blocker set. - state, err := c.policy.loadBlockingState(ctx) - if err != nil { - return issueops.CloseBatchResult{}, err - } - policy = storage.NewBatchClosePolicy(state.refsByIssue) - case !request.Force: - // Only a claim reads blockers beyond the batch. Scoping the rest - // keeps an unrelated blocked issue from demanding policy support - // of a backend that lacks it — and on a remote store reads only the - // batch's own edges rather than every edge in the workspace. - ids := make([]string, 0, len(request.Items)) - for _, item := range request.Items { - ids = append(ids, item.IssueID) - } - refs, err := c.policy.externalBlockersFor(ctx, ids) - if err != nil { - return issueops.CloseBatchResult{}, err - } - blockers := make(map[string][]string, len(request.Items)) - for _, item := range request.Items { - blockers[item.IssueID] = refs[item.IssueID] - } - policy = storage.NewBatchClosePolicy(blockers) - } - inner, err := storage.BatchCloserWithPolicy(c.policy.inner, policy) - if err != nil { - return issueops.CloseBatchResult{}, err - } - return inner.CloseBatch(ctx, request) -} - -var _ issueops.BatchCloser = (*batchCloser)(nil) diff --git a/internal/storage/externaldeps/blocked_lister.go b/internal/storage/externaldeps/blocked_lister.go deleted file mode 100644 index 740c2f7ee..000000000 --- a/internal/storage/externaldeps/blocked_lister.go +++ /dev/null @@ -1,114 +0,0 @@ -package externaldeps - -import ( - "context" - - storageissueops "github.com/steveyegge/beads/internal/storage/issueops" - "github.com/steveyegge/beads/internal/types" - "github.com/steveyegge/beads/issueops" -) - -// BlockedLister lists the blocked issues through GetBlockedIssues above, so -// the role names unsatisfied external blockers exactly as the legacy read -// does, and `bd blocked` answers the same on either. It must be overridden -// here: the embedded passthrough would hand back the inner store's lister, -// which knows nothing of external refs. -// -// The inner layers get their turn back the way ReadyCounter gives it: the -// finished lister is wrapped by a decorator beneath that offers -// WrapBlockedLister (telemetry), asserted rather than imported. -// -// A remote store answers with its own lister (the server's blocked -// operation) whenever its server applies the policy itself, since that -// server's own decorator stack has already named the external blockers; -// against any other server the server's listing is decorated here, through -// reads a served store answers (EdgeReader, BatchGetter). remoteBlockedLister -// decides per call, since the handshake is read lazily, and a failed probe -// fails the call rather than skipping the policy. Unlike ReadyCounter's, the -// remote lister keeps no local leg to fall back on, so no local lister is -// built for it: the inner store's lister already carries the inner layers. -// Nor is remoteBlockedLister wrapped again: with telemetry on, the inner -// lister already carries its span around the server's listing, and wrapping -// would nest a second span of the same name. The policy probe, and the -// decoration against a server that leaves the policy to its clients, run -// outside that span. -func (s *Store) BlockedLister() (issueops.BlockedLister, error) { - if s.rolesAreRemote() { - inner, err := s.inner.BlockedLister() - if err != nil { - return nil, err - } - return &remoteBlockedLister{inner: inner, policy: s}, nil - } - lister, err := storageissueops.NewBlockedListerOver(s) - if err != nil { - return nil, err - } - if wrapper, ok := s.inner.(blockedListerWrapper); ok { - lister = wrapper.WrapBlockedLister(lister) - } - return lister, nil -} - -// blockedListerWrapper is how a decorator beneath this one adds its layer to -// a blocked lister it did not construct. -type blockedListerWrapper interface { - WrapBlockedLister(issueops.BlockedLister) issueops.BlockedLister -} - -type remoteBlockedLister struct { - inner issueops.BlockedLister - policy *Store -} - -// ListBlocked is the server's listing when the server applies the policy. -// Against any other server it is that listing with the external blockers -// added here, the decoration GetBlockedIssues makes over a local store. -func (l *remoteBlockedLister) ListBlocked(ctx context.Context, req issueops.BlockedRequest) (issueops.BlockedResult, error) { - enforced, err := l.policy.serverEnforcesPolicy(ctx) - if err != nil { - return issueops.BlockedResult{}, err - } - result, err := l.inner.ListBlocked(ctx, req) - if err != nil || enforced { - return result, err - } - items, err := l.policy.decorateBlocked(ctx, storageissueops.BlockedFilter(req), result.Items, - l.policy.remoteEdges, l.policy.remoteIssuesByIDs) - if err != nil { - return issueops.BlockedResult{}, err - } - return storageissueops.BlockedResultOf(items), nil -} - -// remoteIssuesByIDs reads rows by id through the inner BatchGetter, which a -// served store answers. -func (s *Store) remoteIssuesByIDs(ctx context.Context, ids []string) ([]*types.Issue, error) { - if len(ids) == 0 { - return nil, nil - } - getter, err := s.inner.BatchGetter() - if err != nil { - return nil, err - } - return getManyChunked(ctx, getter, ids) -} - -// getManyChunked reads rows by id through getter, in requests of at most -// issueops.MaxGetManyIDs ids, both planes, labels hydrated (GetMany's one -// hydration, the label filter's input). A missing id is simply absent, as -// GetIssuesByIDs leaves it. -func getManyChunked(ctx context.Context, getter issueops.BatchGetter, ids []string) ([]*types.Issue, error) { - var out []*types.Issue - for start := 0; start < len(ids); start += issueops.MaxGetManyIDs { - end := min(start+issueops.MaxGetManyIDs, len(ids)) - res, err := getter.GetMany(ctx, issueops.GetManyRequest{IDs: ids[start:end]}) - if err != nil { - return nil, err - } - out = append(out, res.Issues...) - } - return out, nil -} - -var _ issueops.BlockedLister = (*remoteBlockedLister)(nil) diff --git a/internal/storage/externaldeps/blocked_lister_test.go b/internal/storage/externaldeps/blocked_lister_test.go deleted file mode 100644 index 883898f36..000000000 --- a/internal/storage/externaldeps/blocked_lister_test.go +++ /dev/null @@ -1,176 +0,0 @@ -package externaldeps - -import ( - "context" - "reflect" - "slices" - "testing" - - storageissueops "github.com/steveyegge/beads/internal/storage/issueops" - "github.com/steveyegge/beads/internal/storage/uow" - "github.com/steveyegge/beads/internal/types" - publicops "github.com/steveyegge/beads/issueops" -) - -// TestWrapUOWProviderBlockedListerNamesExternalBlockers pins the blocked -// listing on the composition every `bd serve` answers from: the unit-of-work -// provider wrapped by this package (cmd/bd's wireExternalDependencyUOWProvider) -// handing out BlockedLister through its own accessor. The server advertises -// policy.external_dependencies for this composition, so its listing is the one -// a client stands down to, and it must already carry the external refs: -// -// - a row blocked ONLY by an unsatisfied external ref is listed, with the -// ref as its blocker (the undecorated listing never names it, because the -// stored blocked flag knows nothing of other projects); -// - a row with a local blocker AND an external ref names both. -// -// A lister built over the inner provider rather than this one would lose -// both, and gc reads the missing row as unblocked. -func TestWrapUOWProviderBlockedListerNamesExternalBlockers(t *testing.T) { - local, mixed, extOnly := issue("be-local"), issue("be-mixed"), issue("be-ext") - mixed.Priority, extOnly.Priority = 1, 2 - inner := &fakeUOW{ - issues: &fakeIssueUseCase{ - ready: []*types.Issue{local, mixed, extOnly}, - blocked: []*types.BlockedIssue{ - {Issue: *mixed, BlockedByCount: 1, BlockedBy: []string{local.ID}}, - }, - }, - deps: &fakeDependencyUseCase{ - external: map[string][]*types.Dependency{ - mixed.ID: {externalDep(mixed.ID, "external:remote:billing", types.DepBlocks)}, - extOnly.ID: {externalDep(extOnly.ID, "external:remote:payments", types.DepBlocks)}, - }, - }, - } - provider := WrapUOWProvider(&fakeUOWProvider{uw: inner}, func(ProjectName) (string, bool) { - return "", false - }, nil) - source, ok := provider.(uow.BlockedListerSource) - if !ok { - t.Fatalf("wrapped provider %T does not offer BlockedLister", provider) - } - lister, err := source.BlockedLister() - if err != nil { - t.Fatalf("BlockedLister: %v", err) - } - result, err := lister.ListBlocked(t.Context(), publicops.BlockedRequest{}) - if err != nil { - t.Fatalf("ListBlocked: %v", err) - } - got := map[string][]string{} - var order []string - for _, item := range result.Items { - blockers := slices.Clone(item.BlockedBy) - slices.Sort(blockers) - got[item.ID] = blockers - order = append(order, item.ID) - if item.BlockedByCount != len(item.BlockedBy) { - t.Errorf("%s: BlockedByCount = %d, len(BlockedBy) = %d", item.ID, item.BlockedByCount, len(item.BlockedBy)) - } - } - want := map[string][]string{ - mixed.ID: {local.ID, "external:remote:billing"}, - extOnly.ID: {"external:remote:payments"}, - } - slices.Sort(want[mixed.ID]) - if !reflect.DeepEqual(got, want) { - t.Fatalf("listing = %v, want %v", got, want) - } - if !slices.Equal(order, []string{mixed.ID, extOnly.ID}) { - t.Errorf("order = %v, want priority order [%s %s]", order, mixed.ID, extOnly.ID) - } -} - -// externalOnlyLabelCases are label filters over two rows blocked ONLY by an -// unsatisfied external ref (externalOnlyLabelRows): be-tagged carries lane:a -// and team:x, be-bare carries no label. The undecorated listing, whose SQL -// applies the label clauses, never sees either row; the decoration adds them -// after that query ran, so each want is what those clauses answer for a row -// the query lists itself. -var externalOnlyLabelCases = []struct { - name string - req publicops.BlockedRequest - want []string -}{ - {"no filter", publicops.BlockedRequest{}, []string{"be-tagged", "be-bare"}}, - {"label", publicops.BlockedRequest{Labels: []string{"lane:a"}}, []string{"be-tagged"}}, - {"labels are all required", publicops.BlockedRequest{Labels: []string{"lane:a", "team:y"}}, nil}, - {"label matches exactly", publicops.BlockedRequest{Labels: []string{"Lane:A"}}, nil}, - {"label_any", publicops.BlockedRequest{LabelsAny: []string{"lane:b", "team:x"}}, []string{"be-tagged"}}, - {"label_any matching none", publicops.BlockedRequest{LabelsAny: []string{"lane:b"}}, nil}, - {"exclude_label", publicops.BlockedRequest{ExcludeLabels: []string{"lane:a"}}, []string{"be-bare"}}, - {"exclude_label matching none", publicops.BlockedRequest{ExcludeLabels: []string{"lane:b"}}, []string{"be-tagged", "be-bare"}}, - {"label and exclude_label", publicops.BlockedRequest{Labels: []string{"lane:a"}, ExcludeLabels: []string{"team:x"}}, nil}, -} - -func externalOnlyLabelRows() ([]*types.Issue, map[string][]*types.Dependency) { - tagged, bare := issue("be-tagged"), issue("be-bare") - tagged.Priority, bare.Priority = 1, 2 - tagged.Labels = []string{"lane:a", "team:x"} - return []*types.Issue{tagged, bare}, map[string][]*types.Dependency{ - tagged.ID: {externalDep(tagged.ID, "external:remote:payments", types.DepBlocks)}, - bare.ID: {externalDep(bare.ID, "external:remote:payments", types.DepBlocks)}, - } -} - -// TestBlockedListingFiltersExternalOnlyRowsByLabel pins the label filters on -// the rows the decoration adds, on both of this package's compositions, each -// read through the role and through the legacy GetBlockedIssues door: the -// store decorator (the direct and embedded routes, a store-shaped bd serve, -// and the http client's own decoration, which shares decorateBlocked) and the -// unit-of-work decorator (the proxied route and a provider-shaped bd serve, -// whose plain by-id reads hydrate no labels at all). -func TestBlockedListingFiltersExternalOnlyRowsByLabel(t *testing.T) { - t.Run("store", func(t *testing.T) { - rows, deps := externalOnlyLabelRows() - store := testStore(&fakeStore{ready: rows, deps: deps}, &fakeStore{}, false) - lister, err := store.BlockedLister() - if err != nil { - t.Fatalf("BlockedLister: %v", err) - } - runExternalOnlyLabelCases(t, lister, store.GetBlockedIssues) - }) - t.Run("unit of work", func(t *testing.T) { - rows, deps := externalOnlyLabelRows() - provider := WrapUOWProvider(&fakeUOWProvider{uw: &fakeUOW{ - issues: &fakeIssueUseCase{ready: rows}, - deps: &fakeDependencyUseCase{external: deps}, - }}, func(ProjectName) (string, bool) { return "", false }, nil) - source, ok := provider.(uow.BlockedListerSource) - if !ok { - t.Fatalf("wrapped provider %T does not offer BlockedLister", provider) - } - lister, err := source.BlockedLister() - if err != nil { - t.Fatalf("BlockedLister: %v", err) - } - uw, err := provider.NewUOW(t.Context()) - if err != nil { - t.Fatalf("NewUOW: %v", err) - } - runExternalOnlyLabelCases(t, lister, uw.IssueUseCase().GetBlockedIssues) - }) -} - -func runExternalOnlyLabelCases(t *testing.T, lister publicops.BlockedLister, legacy func(context.Context, types.WorkFilter) ([]*types.BlockedIssue, error)) { - t.Helper() - for _, tc := range externalOnlyLabelCases { - t.Run(tc.name, func(t *testing.T) { - result, err := lister.ListBlocked(t.Context(), tc.req) - if err != nil { - t.Fatalf("ListBlocked: %v", err) - } - if ids := blockedIssueIDs(result.Items); !slices.Equal(ids, tc.want) { - t.Errorf("ListBlocked = %v, want %v", ids, tc.want) - } - items, err := legacy(t.Context(), storageissueops.BlockedFilter(tc.req)) - if err != nil { - t.Fatalf("GetBlockedIssues: %v", err) - } - if ids := blockedIssueIDs(items); !slices.Equal(ids, tc.want) { - t.Errorf("GetBlockedIssues = %v, want %v", ids, tc.want) - } - }) - } -} diff --git a/internal/storage/externaldeps/blocking_annotator_batch_test.go b/internal/storage/externaldeps/blocking_annotator_batch_test.go deleted file mode 100644 index 47879f041..000000000 --- a/internal/storage/externaldeps/blocking_annotator_batch_test.go +++ /dev/null @@ -1,192 +0,0 @@ -package externaldeps - -import ( - "context" - "fmt" - "maps" - "slices" - "testing" - - "github.com/steveyegge/beads/internal/storage" - "github.com/steveyegge/beads/internal/types" - publicops "github.com/steveyegge/beads/issueops" -) - -// pageStore is a fakeStore whose local blocking state is local (an id present -// is blocked, by those blockers) and which counts the reads a page makes. -type pageStore struct { - *fakeStore - local map[string][]string - ownReads int - perIDCalls int -} - -func (p *pageStore) GetDependencyRecordsForIssues(ctx context.Context, ids []string) (map[string][]*types.Dependency, error) { - p.ownReads++ - return p.fakeStore.GetDependencyRecordsForIssues(ctx, ids) -} - -func (p *pageStore) IsBlocked(_ context.Context, id string) (bool, []string, error) { - p.perIDCalls++ - blockers, ok := p.local[id] - return ok, slices.Clone(blockers), nil -} - -func (p *pageStore) BlockingAnnotator() (publicops.BlockingAnnotator, error) { - return pageAnnotator{}, nil -} - -// pageAnnotator is the backend's own answer: no local blockers. -type pageAnnotator struct{} - -func (pageAnnotator) AnnotateBlocking(_ context.Context, req publicops.BlockingRequest) (publicops.BlockingResult, error) { - items := make([]publicops.IssueBlocking, 0, len(req.IDs)) - for _, id := range req.IDs { - items = append(items, publicops.IssueBlocking{ID: id, BlockedBy: []string{}}) - } - return publicops.BlockingResult{Items: items}, nil -} - -// batchPageStore also answers a page's local blockers in one call -// (storage.BlockersBatchStore). -type batchPageStore struct { - *pageStore - batchCalls int -} - -func (b *batchPageStore) IsBlockedWithBlockersBatch(_ context.Context, ids []string) (map[string][]string, error) { - b.batchCalls++ - out := make(map[string][]string) - for _, id := range ids { - if blockers, ok := b.local[id]; ok { - out[id] = slices.Clone(blockers) - } - } - return out, nil -} - -var _ storage.BlockersBatchStore = (*batchPageStore)(nil) - -func countingPolicy(raw storage.DoltStorage, foreign *fakeStore, opens *int) *Store { - return New(raw, - func(project ProjectName) (string, bool) { return "/projects/remote", project == "remote" }, - func(context.Context, string) (storage.DoltStorage, error) { - *opens++ - return foreign, nil - }, - ) -} - -// TestBlockingAnnotatorReadsAPageOnce: annotating N ids reads their own edges -// once, opens the one foreign project they all name once, and asks the local -// blockers in one batch, rather than IsBlocked's reads and foreign open per -// id (9-20 ms each over a remote serve, so a 1,400-id refresh took 12 s). The -// answer is still IsBlocked's: local blockers first, then the unsatisfied -// refs; a satisfied ref is not listed. -func TestBlockingAnnotatorReadsAPageOnce(t *testing.T) { - const n = 40 - ids := make([]string, 0, n) - deps := make(map[string][]*types.Dependency, n) - local := make(map[string][]string) - for i := range n { - id := fmt.Sprintf("be-%02d", i) - ids = append(ids, id) - switch i % 4 { - case 0: // unsatisfied external only - deps[id] = []*types.Dependency{externalDep(id, "external:remote:pending", types.DepBlocks)} - case 1: // satisfied external only - deps[id] = []*types.Dependency{externalDep(id, "external:remote:shipped", types.DepBlocks)} - case 2: // local blockers plus both refs - local[id] = []string{"be-x", "be-w (waits-for)"} - deps[id] = []*types.Dependency{ - externalDep(id, "external:remote:pending", types.DepBlocks), - externalDep(id, "external:remote:shipped", types.DepBlocks), - } - case 3: // held by its parent chain only: blocked, no direct blockers - local[id] = nil - } - } - shipped := issue("remote-shipped") - shipped.Status = types.StatusClosed - foreign := &fakeStore{labels: map[string][]*types.Issue{ - "provides:shipped": {shipped}, - "provides:pending": {issue("remote-pending")}, - }} - raw := &batchPageStore{pageStore: &pageStore{fakeStore: &fakeStore{deps: deps}, local: local}} - opens := 0 - store := countingPolicy(raw, foreign, &opens) - annotator, err := store.BlockingAnnotator() - if err != nil { - t.Fatalf("BlockingAnnotator: %v", err) - } - - result, err := annotator.AnnotateBlocking(t.Context(), publicops.BlockingRequest{IDs: ids}) - if err != nil { - t.Fatalf("AnnotateBlocking: %v", err) - } - if raw.ownReads != 1 || opens != 1 || raw.batchCalls != 1 || raw.perIDCalls != 0 { - t.Fatalf("one page of %d ids made %d own-edge reads, %d foreign opens, %d batch and %d per-id local reads; want 1, 1, 1, 0", - n, raw.ownReads, opens, raw.batchCalls, raw.perIDCalls) - } - - got := make(map[string]string, n) - for _, item := range result.Items { - got[item.ID] = fmt.Sprint(item.BlockedBy) - } - want := make(map[string]string, n) - for i, id := range ids { - switch i % 4 { - case 0: - want[id] = "[external:remote:pending]" - case 1, 3: - want[id] = "[]" - case 2: - want[id] = "[be-x be-w (waits-for) external:remote:pending]" - } - } - if !maps.Equal(got, want) { - t.Fatalf("AnnotateBlocking = %v\nwant %v", got, want) - } - - // The same answer per item, through IsBlocked, which the page used to make. - for _, item := range result.Items { - blocked, blockers, err := store.IsBlocked(t.Context(), item.ID) - if err != nil { - t.Fatalf("IsBlocked(%s): %v", item.ID, err) - } - if blocked && !slices.Equal(blockers, item.BlockedBy) { - t.Errorf("%s: page says %v, IsBlocked says %v", item.ID, item.BlockedBy, blockers) - } - } -} - -// TestBlockingAnnotatorFallsBackPerIDWithoutTheBatchRead: a store without -// storage.BlockersBatchStore still gets IsBlocked's answer, one local read per -// id, while the external half stays one read and one foreign open. -func TestBlockingAnnotatorFallsBackPerIDWithoutTheBatchRead(t *testing.T) { - ids := []string{"be-a", "be-b", "be-c"} - deps := map[string][]*types.Dependency{} - local := map[string][]string{} - for _, id := range ids { - deps[id] = []*types.Dependency{externalDep(id, "external:remote:pending", types.DepBlocks)} - local[id] = []string{"be-x"} - } - raw := &pageStore{fakeStore: &fakeStore{deps: deps}, local: local} - opens := 0 - annotator, err := countingPolicy(raw, &fakeStore{}, &opens).BlockingAnnotator() - if err != nil { - t.Fatalf("BlockingAnnotator: %v", err) - } - result, err := annotator.AnnotateBlocking(t.Context(), publicops.BlockingRequest{IDs: ids}) - if err != nil { - t.Fatalf("AnnotateBlocking: %v", err) - } - if raw.ownReads != 1 || opens != 1 || raw.perIDCalls != len(ids) { - t.Fatalf("own-edge reads = %d, foreign opens = %d, per-id local reads = %d; want 1, 1, %d", raw.ownReads, opens, raw.perIDCalls, len(ids)) - } - for _, item := range result.Items { - if want := []string{"be-x", "external:remote:pending"}; !slices.Equal(item.BlockedBy, want) { - t.Errorf("%s BlockedBy = %v, want %v", item.ID, item.BlockedBy, want) - } - } -} diff --git a/internal/storage/externaldeps/decorator.go b/internal/storage/externaldeps/decorator.go deleted file mode 100644 index b8aacb8a1..000000000 --- a/internal/storage/externaldeps/decorator.go +++ /dev/null @@ -1,1042 +0,0 @@ -package externaldeps - -import ( - "context" - "errors" - "fmt" - "slices" - "sort" - "strings" - "sync" - - "github.com/steveyegge/beads/internal/storage" - "github.com/steveyegge/beads/internal/storage/issueops" - "github.com/steveyegge/beads/internal/types" - "github.com/steveyegge/beads/internal/workapi/storereadycounter" - publicops "github.com/steveyegge/beads/issueops" -) - -// Store decorates a local store with query-time external capability handling. -type Store struct { - storage.DoltStorage - inner storage.DoltStorage - locateProject ProjectLocator - openProject StoreOpener - warnProject func(ProjectName) - warnMu sync.Mutex - warned map[ProjectName]struct{} -} - -// New constructs an external-capability-aware storage decorator. -func New(inner storage.DoltStorage, locateProject ProjectLocator, openProject StoreOpener) *Store { - return &Store{ - DoltStorage: inner, - inner: inner, - locateProject: locateProject, - openProject: openProject, - warnProject: defaultProjectWarning, - warned: make(map[ProjectName]struct{}), - } -} - -// Unwrap exposes the decorated store to storage.UnwrapStore. -func (s *Store) Unwrap() storage.DoltStorage { return s.inner } - -// Applied reports whether store's decorator chain includes this policy layer, -// so the roles it hands out apply the external-dependency policy. bd serve -// reads it to decide whether its handshake may advertise -// policy.external_dependencies (httpapi.Config.ExternalDependencyPolicy). -func Applied(store storage.DoltStorage) bool { - for store != nil { - if _, ok := store.(*Store); ok { - return true - } - u, ok := store.(storage.Unwrapper) - if !ok { - return false - } - store = u.Unwrap() - } - return false -} - -// IssueLifecycle preserves the external close policy for public lifecycle -// operations. Returning the inner lifecycle directly would promote around the -// decorator when bd close or bd update uses the lifecycle seam. -func (s *Store) IssueLifecycle() (publicops.Lifecycle, error) { - inner, err := s.inner.IssueLifecycle() - if err != nil { - return nil, err - } - return &lifecycle{inner: inner, policy: s}, nil -} - -type lifecycle struct { - inner publicops.Lifecycle - policy *Store -} - -var _ publicops.Lifecycle = (*lifecycle)(nil) - -func (l *lifecycle) Create(ctx context.Context, request publicops.CreateRequest) (publicops.CreateResult, error) { - return l.inner.Create(ctx, request) -} - -func (l *lifecycle) Update(ctx context.Context, request publicops.UpdateRequest) (publicops.UpdateResult, error) { - ctx = storage.WithLivePolicyProbe(ctx) - if request.Claim { - if err := l.policy.guardExternalClose(ctx, request.IssueID, false); err != nil { - return publicops.UpdateResult{}, err - } - } - if request.Patch.Status.Set && string(request.Patch.Status.Value) == string(types.StatusClosed) { - if err := l.policy.guardExternalClose(ctx, request.IssueID, request.ForceClosePolicy); err != nil { - return publicops.UpdateResult{}, err - } - } - return l.inner.Update(ctx, request) -} - -func (l *lifecycle) Close(ctx context.Context, request publicops.CloseRequest) (publicops.CloseResult, error) { - ctx = storage.WithLivePolicyProbe(ctx) - if err := l.policy.guardExternalClose(ctx, request.IssueID, request.Force); err != nil { - return publicops.CloseResult{}, err - } - return l.inner.Close(ctx, request) -} - -func (l *lifecycle) Reopen(ctx context.Context, request publicops.ReopenRequest) (publicops.ReopenResult, error) { - return l.inner.Reopen(ctx, request) -} - -func (s *Store) guardExternalClose(ctx context.Context, id string, force bool) error { - if force { - return nil - } - // externalBlockersFor reads only this issue's edges on a remote store - // rather than every edge in the workspace. - refs, err := s.externalBlockersFor(ctx, []string{id}) - if err != nil { - return err - } - if blockers := refs[id]; len(blockers) > 0 { - return publicops.NewCloseBlockedError(id, blockers) - } - return nil -} - -func (s *Store) warnUnresolvedProject(project ProjectName) { - s.warnMu.Lock() - defer s.warnMu.Unlock() - if _, warned := s.warned[project]; warned { - return - } - s.warned[project] = struct{}{} - if s.warnProject != nil { - s.warnProject(project) - } -} - -type blockingState struct { - refsByIssue map[string][]string -} - -func (s *Store) loadBlockingState(ctx context.Context) (blockingState, error) { - // Design 3.6: a remote server that already enforces this policy itself - // (advertised as the handshake's wire.CapExternalDependencies capability, - // probed here through storage.ExternalDependencyPolicyProber) has already - // applied it before answering, so this decorator's own pass would be - // redundant — skip to an empty blocking state. A store that does not - // implement the prober, or that implements it and reports false (a remote - // server silent on the capability included), falls through to the - // ordinary client-side enforcement below: the policy is never silently - // skipped merely because the inner store is remote. - if enforced, err := s.serverEnforcesPolicy(ctx); err != nil || enforced { - return blockingState{}, err - } - - queryStore, ok := storage.UnwrapStore(s.inner).(storage.ExternalDependencyQueryStore) - var allDeps map[string][]*types.Dependency - var err error - if ok { - allDeps, err = queryStore.GetExternalBlockingDependencyRecords(ctx) - } else { - // Compatibility fallback for third-party stores that predate the narrow - // optional capability. First-party stores implement the indexed query. - allDeps, err = s.inner.GetAllDependencyRecords(ctx) - } - if err != nil { - return blockingState{}, fmt.Errorf("external dependencies: list blocking records: %w", err) - } - - return s.blockingStateFromRecords(ctx, allDeps) -} - -// serverEnforcesPolicy asks the inner store's ExternalDependencyPolicyProber, -// if it has one, whether its server already enforced this policy. -func (s *Store) serverEnforcesPolicy(ctx context.Context) (bool, error) { - prober, ok := storage.UnwrapStore(s.inner).(storage.ExternalDependencyPolicyProber) - if !ok { - return false, nil - } - enforced, err := prober.ServerEnforcesExternalDependencyPolicy(ctx) - if err != nil { - return false, fmt.Errorf("external dependencies: probe server policy: %w", err) - } - return enforced, nil -} - -func (s *Store) blockingStateFromRecords(ctx context.Context, allDeps map[string][]*types.Dependency) (blockingState, error) { - refs := make([]reference, 0) - refsByIssue := make(map[string][]string) - for issueID, deps := range allDeps { - for _, dep := range deps { - if dep == nil || !dep.Type.IsBlockingEdge() || !isExternalReference(dep.DependsOnID) { - continue - } - refs = append(refs, parseReference(dep.DependsOnID)) - refsByIssue[issueID] = appendUnique(refsByIssue[issueID], dep.DependsOnID) - } - } - - satisfied, err := s.resolveReferences(ctx, refs) - if err != nil { - return blockingState{}, fmt.Errorf("external dependencies: resolve blockers: %w", err) - } - for issueID, issueRefs := range refsByIssue { - unsatisfied := issueRefs[:0] - for _, ref := range issueRefs { - if !satisfied[ref] { - unsatisfied = append(unsatisfied, ref) - } - } - if len(unsatisfied) == 0 { - delete(refsByIssue, issueID) - continue - } - refsByIssue[issueID] = unsatisfied - } - - return blockingState{refsByIssue: refsByIssue}, nil -} - -// GetReadyWork excludes sources with unsatisfied external blocking edges. -func (s *Store) GetReadyWork(ctx context.Context, filter types.WorkFilter) ([]*types.Issue, error) { - state, err := s.loadBlockingState(ctx) - if err != nil { - return nil, err - } - queryFilter, blockedIDs := s.readyExclusion(filter, state.refsByIssue) - issues, err := s.inner.GetReadyWork(ctx, queryFilter) - if err != nil { - return nil, innerCapError(err, filter, blockedIDs) - } - kept := dropBlockedIssues(issues, blockedIDs, filter.Limit) - if err := capKeptRows(len(kept), filter, blockedIDs); err != nil { - return nil, err - } - return kept, nil -} - -// GetReadyWorkWithCounts is the counts-bearing equivalent of GetReadyWork. -func (s *Store) GetReadyWorkWithCounts(ctx context.Context, filter types.WorkFilter) ([]*types.IssueWithCounts, error) { - state, err := s.loadBlockingState(ctx) - if err != nil { - return nil, err - } - return s.readyWorkWithCounts(ctx, filter, state) -} - -// readyWorkWithCounts is GetReadyWorkWithCounts' body against an -// already-loaded blockingState, which remoteReadyClaimer reuses across its -// passes. -func (s *Store) readyWorkWithCounts(ctx context.Context, filter types.WorkFilter, state blockingState) ([]*types.IssueWithCounts, error) { - queryFilter, blockedIDs := s.readyExclusion(filter, state.refsByIssue) - rows, err := s.inner.GetReadyWorkWithCounts(ctx, queryFilter) - if err != nil { - return nil, innerCapError(err, filter, blockedIDs) - } - kept, _ := dropBlockedIssuesWithCounts(rows, blockedIDs, filter.Limit) - if err := capKeptRows(len(kept), filter, blockedIDs); err != nil { - return nil, err - } - return kept, nil -} - -// GetReadyWorkWithCountsAndTotal applies the same external exclusions as -// GetReadyWorkWithCounts, so the page and its total describe one ready set. -// It must be overridden here: the embedded passthrough would reach the inner -// store without the exclusions. -func (s *Store) GetReadyWorkWithCountsAndTotal(ctx context.Context, filter types.WorkFilter) ([]*types.IssueWithCounts, int, error) { - state, err := s.loadBlockingState(ctx) - if err != nil { - return nil, 0, err - } - return s.readyWorkWithCountsAndTotal(ctx, filter, state) -} - -// readyExclusion resolves how this decorator applies its OWN additional -// exclusions (issues with an unsatisfied external blocker) against s.inner. -// -// A store that can express types.WorkFilter.ExcludeIDs over its own -// transport (every first-party SQL-backed store) gets them folded straight -// into the filter, exactly as before: the WHERE clause does the exclusion. -// -// A store that cannot (storage.ExcludeIDsUnsupportedStore — today only -// httpclient.Store: design 3.6 / L12 leaves listReadyWork with no -// id-exclusion parameter on the v0 wire at all) instead gets a bumped Limit -// and the exclusions applied client-side by the caller. Requesting -// Limit+len(refsByIssue) rows from the UNFILTERED ready set is provably -// enough headroom to still surface Limit genuinely-unblocked rows whenever -// that many exist: refsByIssue cannot hold more entries than the number of -// issues anywhere with an unsatisfied external blocker, so at most -// len(refsByIssue) of the bumped window's extra rows can be ones this -// decorator has to drop. Whichever path is taken, the policy itself is never -// skipped — only where the exclusion happens differs. -// -// The client-side path also takes the MaxRows cap off a limited inner query. -// That store caps the rows it fetched, which the bump lets outnumber Limit, so -// `--limit N --max-rows N` failed with exit 2 as soon as more than N issues -// were ready, refusing a page that could never exceed N. The caller enforces -// the cap on the rows it keeps instead (capKeptRows), after the Limit trim, -// which is where issueops enforces it on the rows a SQL store delivers -// (finishReadyWorkWithCounts). -// -// An unlimited inner query keeps its cap, bumped the same way to MaxRows plus -// len(blockedIDs). Past that many rows, more than MaxRows of them are kept -// whichever rows the drop takes, so the bump refuses nothing the caller's cap -// would admit, and a store that sizes an unlimited request by MaxRows (the -// http bridge, under its client read cap) asks for that bound and one row -// more rather than the whole ready set. Its refusal reaches the caller as the -// caller's own (innerCapError). -func (s *Store) readyExclusion(filter types.WorkFilter, refsByIssue map[string][]string) (queryFilter types.WorkFilter, blockedIDs map[string]bool) { - if len(refsByIssue) == 0 { - return filter, nil - } - if unsupported, ok := storage.UnwrapStore(s.inner).(storage.ExcludeIDsUnsupportedStore); !ok || !unsupported.ExcludeIDsUnsupported() { - return withExternalExclusions(filter, refsByIssue), nil - } - blockedIDs = make(map[string]bool, len(refsByIssue)) - for issueID := range refsByIssue { - blockedIDs[issueID] = true - } - queryFilter = filter - switch { - case filter.Limit > 0: - queryFilter.Limit += len(blockedIDs) - queryFilter.MaxRows = 0 - queryFilter.MaxRowsSource = "" - case filter.MaxRows > 0: - queryFilter.MaxRows += len(blockedIDs) - } - return queryFilter, blockedIDs -} - -// capKeptRows enforces the caller's MaxRows cap on the kept rows it is -// handed: readyExclusion took that cap off the inner query, or bumped it by -// the rows the caller drops. With nothing to drop client-side the inner query -// kept the cap and has already enforced it. -func capKeptRows(kept int, filter types.WorkFilter, blockedIDs map[string]bool) error { - if len(blockedIDs) == 0 { - return nil - } - return issueops.EnforceMaxRowsCap(kept, filter.MaxRows, filter.MaxRowsSource) -} - -// innerCapError hands the caller its own cap's refusal when the inner query -// refused the MaxRows readyExclusion bumped: Found rows, more than MaxRows -// plus len(blockedIDs), of which at least Found-len(blockedIDs), more than -// MaxRows, would have been kept. Any other error is returned as it is. -func innerCapError(err error, filter types.WorkFilter, blockedIDs map[string]bool) error { - var tooMany *issueops.ErrTooManyRows - if len(blockedIDs) == 0 || filter.Limit > 0 || filter.MaxRows <= 0 || !errors.As(err, &tooMany) { - return err - } - return &issueops.ErrTooManyRows{ - Found: tooMany.Found - len(blockedIDs), - Cap: filter.MaxRows, - Source: filter.MaxRowsSource, - } -} - -// dropBlockedIssues is readyExclusion's client-side half: it drops the rows -// blockedIDs names and trims what remains to limit. It copies into a fresh -// slice rather than compacting issues in place, so the slice s.inner returned -// still holds every row it fetched. -func dropBlockedIssues(issues []*types.Issue, blockedIDs map[string]bool, limit int) []*types.Issue { - if len(blockedIDs) == 0 { - return issues - } - kept := make([]*types.Issue, 0, len(issues)) - for _, issue := range issues { - if issue != nil && blockedIDs[issue.ID] { - continue - } - kept = append(kept, issue) - } - if limit > 0 && len(kept) > limit { - kept = kept[:limit] - } - return kept -} - -// dropBlockedIssuesWithCounts is dropBlockedIssues for counts-bearing rows. It -// also reports how many rows it dropped from the whole fetched window (before -// the limit trim), which is the figure readyWorkWithCountsAndTotal subtracts -// from the inner store's total. -func dropBlockedIssuesWithCounts(rows []*types.IssueWithCounts, blockedIDs map[string]bool, limit int) (kept []*types.IssueWithCounts, dropped int) { - if len(blockedIDs) == 0 { - return rows, 0 - } - kept = make([]*types.IssueWithCounts, 0, len(rows)) - for _, row := range rows { - if row != nil && row.Issue != nil && blockedIDs[row.ID] { - dropped++ - continue - } - kept = append(kept, row) - } - if limit > 0 && len(kept) > limit { - kept = kept[:limit] - } - return kept, dropped -} - -func withExternalExclusions(filter types.WorkFilter, refsByIssue map[string][]string) types.WorkFilter { - filter.ExcludeIDs = slices.Clone(filter.ExcludeIDs) - newIDs := make([]string, 0, len(refsByIssue)) - for issueID := range refsByIssue { - if !slices.Contains(filter.ExcludeIDs, issueID) { - newIDs = append(newIDs, issueID) - } - } - sort.Strings(newIDs) - filter.ExcludeIDs = append(filter.ExcludeIDs, newIDs...) - return filter -} - -// readyWorkWithCountsAndTotal is GetReadyWorkWithCountsAndTotal's body, split -// out so CountReadyWork's fallback (below) can reuse it against an -// already-loaded blockingState instead of loading it a second time. -func (s *Store) readyWorkWithCountsAndTotal(ctx context.Context, filter types.WorkFilter, state blockingState) ([]*types.IssueWithCounts, int, error) { - queryFilter, blockedIDs := s.readyExclusion(filter, state.refsByIssue) - rows, total, err := s.inner.GetReadyWorkWithCountsAndTotal(ctx, queryFilter) - if err != nil { - return nil, 0, innerCapError(err, filter, blockedIDs) - } - kept, blockedSeen := dropBlockedIssuesWithCounts(rows, blockedIDs, filter.Limit) - if len(blockedIDs) == 0 { - return kept, total, nil - } - if err := capKeptRows(len(kept), filter, blockedIDs); err != nil { - return nil, 0, err - } - - var adjustedTotal int - if total <= len(rows) { - // The fetch's own reported total says the window already held the - // entire ready set, so every blocked row the window contains is every - // blocked row the ready set contains: the subtraction is exact. This - // is judged from the rows that came back, not from the Limit asked - // for, so it holds however a store reads a zero Limit: every row to a - // SQL store, and over http one request bounded at the client cap plus - // one, refused rather than truncated past it (an older http client - // sent no limit at all and got the server's default page back). - adjustedTotal = total - blockedSeen - } else { - // The ready set is bigger than the window that came back (the bumped - // window: the caller's page, or CountReadyWork's one-row page), so - // the window cannot say how many of its unseen rows are blocked. Nor - // can refsByIssue: it names every workspace issue with an unsatisfied - // external blocker, closed ones and ones the filter excludes included, - // and only those in the filtered ready set were ever in total. Size - // that set instead. bd ready's own page (above) is exact either way. - adjustedTotal, err = s.refetchedReadyTotal(ctx, filter, total, blockedIDs) - if err != nil { - return nil, 0, err - } - } - if adjustedTotal < len(kept) { - adjustedTotal = len(kept) - } - if adjustedTotal < 0 { - adjustedTotal = 0 - } - return kept, adjustedTotal, nil -} - -// refetchedReadyTotal sizes the externally filtered ready set when the first -// window held only part of it: it refetches the whole filtered set, ids only, -// and subtracts the blocked rows actually in it. A refetch that comes back -// truncated too (the set grew in between, or the server bounded the page) -// falls back to subtracting every blocked id, the bound least likely to -// overstate how much ready work remains. -func (s *Store) refetchedReadyTotal(ctx context.Context, filter types.WorkFilter, total int, blockedIDs map[string]bool) (int, error) { - full := filter - full.Offset = 0 - full.Limit = total + len(blockedIDs) - full.MaxRows = 0 - full.MaxRowsSource = "" - full.Lite = true - rows, fullTotal, err := s.inner.GetReadyWorkWithCountsAndTotal(ctx, full) - if err != nil { - return 0, err - } - if fullTotal > len(rows) { - return total - len(blockedIDs), nil - } - _, blocked := dropBlockedIssuesWithCounts(rows, blockedIDs, 0) - return fullTotal - blocked, nil -} - -// CountReadyWork reports the externally filtered ready count. -func (s *Store) CountReadyWork(ctx context.Context, filter types.WorkFilter) (int, error) { - filter.Limit = 0 - filter.Offset = 0 - state, err := s.loadBlockingState(ctx) - if err != nil { - return 0, err - } - if counter, ok := storage.UnwrapStore(s.inner).(storage.ReadyWorkCounter); ok { - return counter.CountReadyWork(ctx, withExternalExclusions(filter, state.refsByIssue)) - } - // s.inner has no indexed counter (httpclient.Store among others): reuse - // the counts-and-total path above, which already copes with a store whose - // transport cannot express ExcludeIDs at all. It asks for a ONE-ROW Lite - // page, never every row: the total is the inner store's own count of the - // whole ready set (over http, the server's countReadyWork beside that - // page), and refetchedReadyTotal sizes the externally blocked case. A zero - // Limit would download the whole ready set to report one number — over - // http, up to BEADS_HTTP_UNLIMITED_READ_CAP full rows, refused past it. - page := filter - page.Limit = 1 - page.Lite = true - _, total, err := s.readyWorkWithCountsAndTotal(ctx, page, state) - return total, err -} - -// ReadyCounter sizes the ready set through CountReadyWork above, so the total -// text-mode `bd ready` prints honors the same external exclusions as the page -// and as the in-band total `bd ready --json` takes from -// GetReadyWorkWithCountsAndTotal. It must be overridden here: the embedded -// passthrough would hand back the inner store's counter, which counts -// externally blocked issues as ready. -// -// Overriding costs the layers beneath their turn, though, and this accessor -// cannot simply recurse the way IssueLifecycle above does: the exclusions live -// on THIS store, so the counter has to be built over it. The inner store gets -// its layer back by wrapping the finished counter — which is what -// telemetry.InstrumentedStorage.WrapReadyCounter exists for, and why the -// documented storage.ReadyCounter.CountReady span still appears for text-mode -// `bd ready` in the cmd/bd chain (hooks -> externaldeps -> telemetry -> store). -// -// A remote store answers with its own counter (the server's count operation) -// whenever its server applies the policy itself; remoteReadyCounter decides -// per call, since the handshake is read lazily. -func (s *Store) ReadyCounter() (publicops.ReadyCounter, error) { - counter, err := storereadycounter.New(s) - if err != nil { - return nil, err - } - if wrapper, ok := s.inner.(readyCounterWrapper); ok { - counter = wrapper.WrapReadyCounter(counter) - } - if s.rolesAreRemote() { - inner, err := s.inner.ReadyCounter() - if err != nil { - return nil, err - } - return &remoteReadyCounter{inner: inner, fallback: counter, policy: s}, nil - } - return counter, nil -} - -// readyCounterWrapper is how a decorator beneath this one adds its layer to a -// ready counter it did not construct. The shape is asserted rather than -// imported so this package stays independent of which layers are wired below -// it — telemetry's wrapper is absent entirely when telemetry is disabled. -type readyCounterWrapper interface { - WrapReadyCounter(publicops.ReadyCounter) publicops.ReadyCounter -} - -// ClaimReadyIssue resolves external blockers before using the existing local -// compare-and-swap claim operation. Cross-project state cannot be atomic with -// the local claim, but local claim ownership remains race-safe. -func (s *Store) ClaimReadyIssue(ctx context.Context, filter types.WorkFilter, actor string) (*types.Issue, error) { - ctx = storage.WithLivePolicyProbe(ctx) - state, err := s.loadBlockingState(ctx) - if err != nil { - return nil, err - } - filter = withExternalExclusions(filter, state.refsByIssue) - return s.inner.ClaimReadyIssue(ctx, filter, actor) -} - -// GetBlockedIssues adds unsatisfied external refs to local blocker details and -// includes sources whose only blockers are external. -func (s *Store) GetBlockedIssues(ctx context.Context, filter types.WorkFilter) ([]*types.BlockedIssue, error) { - base, err := s.inner.GetBlockedIssues(ctx, unpagedBlockedFilter(filter)) - if err != nil { - return nil, err - } - // A served store refuses the two raw reads the decoration makes for the - // extra sources; over one they go through the roles it serves, as the - // remote blocked lister's do (blocked_lister.go). Against a server that - // applies the policy itself the decoration adds nothing (loadBlockingState - // stands down) and neither read is made. - dependencyRecords, issuesByIDs := s.inner.GetDependencyRecordsForIssues, s.inner.GetIssuesByIDs - if s.rolesAreRemote() { - dependencyRecords, issuesByIDs = s.remoteEdges, s.remoteIssuesByIDs - } - return s.decorateBlocked(ctx, filter, base, dependencyRecords, issuesByIDs) -} - -// decorateBlocked is GetBlockedIssues' decoration of an undecorated blocked -// listing (base): unsatisfied external refs are added to each row's blockers, -// and the sources blocked only by such refs are listed too, narrowed by the -// same parent and label filters the base listing was. The two reads it makes -// for those extra sources (their parent edges, when the filter names a -// parent, and the rows themselves, labels hydrated as GetIssuesByIDs and -// GetMany both hydrate them) are passed in, so the remote blocked lister can -// make them through roles a served store answers. -func (s *Store) decorateBlocked( - ctx context.Context, - filter types.WorkFilter, - base []*types.BlockedIssue, - dependencyRecords func(context.Context, []string) (map[string][]*types.Dependency, error), - issuesByIDs func(context.Context, []string) ([]*types.Issue, error), -) ([]*types.BlockedIssue, error) { - state, err := s.loadBlockingState(ctx) - if err != nil { - return nil, err - } - - result := make([]*types.BlockedIssue, 0, len(base)+len(state.refsByIssue)) - byID := make(map[string]*types.BlockedIssue, len(base)+len(state.refsByIssue)) - for _, item := range base { - if item == nil { - continue - } - clone := *item - clone.BlockedBy = slices.Clone(item.BlockedBy) - for _, ref := range state.refsByIssue[item.ID] { - clone.BlockedBy = appendUnique(clone.BlockedBy, ref) - } - clone.BlockedByCount = len(clone.BlockedBy) - result = append(result, &clone) - byID[item.ID] = &clone - } - - missingIDs := make([]string, 0, len(state.refsByIssue)) - for issueID := range state.refsByIssue { - if byID[issueID] == nil { - missingIDs = append(missingIDs, issueID) - } - } - parentDeps := make(map[string][]*types.Dependency) - if filter.ParentID != nil && len(missingIDs) > 0 { - parentDeps, err = dependencyRecords(ctx, missingIDs) - if err != nil { - return nil, fmt.Errorf("external dependencies: load blocked parent edges: %w", err) - } - } - filteredMissingIDs := missingIDs[:0] - for _, issueID := range missingIDs { - if matchesParentFilter(issueID, filter.ParentID, parentDeps) { - filteredMissingIDs = append(filteredMissingIDs, issueID) - } - } - var issues []*types.Issue - if len(filteredMissingIDs) > 0 { - issues, err = issuesByIDs(ctx, filteredMissingIDs) - if err != nil { - return nil, fmt.Errorf("external dependencies: load blocked sources: %w", err) - } - } - for _, issue := range issues { - if issue == nil || issue.Status == types.StatusClosed || issue.Status == types.StatusPinned { - continue - } - if !matchesLabelFilter(issue.Labels, filter) { - continue - } - refs := slices.Clone(state.refsByIssue[issue.ID]) - blocked := &types.BlockedIssue{ - Issue: *issue, - BlockedByCount: len(refs), - BlockedBy: refs, - } - result = append(result, blocked) - } - - return finishBlockedIssues(result, filter) -} - -// unpagedBlockedFilter lets the external policy combine local and external -// blockers before applying the caller's page and row cap. -func unpagedBlockedFilter(filter types.WorkFilter) types.WorkFilter { - filter.Offset = 0 - filter.Limit = 0 - filter.MaxRows = 0 - filter.MaxRowsSource = "" - return filter -} - -func finishBlockedIssues(items []*types.BlockedIssue, filter types.WorkFilter) ([]*types.BlockedIssue, error) { - sort.Slice(items, func(i, j int) bool { - if items[i].Priority != items[j].Priority { - return items[i].Priority < items[j].Priority - } - if !items[i].CreatedAt.Equal(items[j].CreatedAt) { - return items[i].CreatedAt.After(items[j].CreatedAt) - } - return items[i].ID < items[j].ID - }) - if filter.Offset > 0 { - if filter.Offset >= len(items) { - items = nil - } else { - items = items[filter.Offset:] - } - } - if filter.Limit > 0 && len(items) > filter.Limit { - items = items[:filter.Limit] - } - if err := issueops.EnforceMaxRowsCap(len(items), filter.MaxRows, filter.MaxRowsSource); err != nil { - return nil, err - } - return items, nil -} - -func matchesParentFilter(issueID string, parentID *string, allDeps map[string][]*types.Dependency) bool { - if parentID == nil { - return true - } - if strings.HasPrefix(issueID, *parentID+".") { - return true - } - for _, dep := range allDeps[issueID] { - if dep != nil && dep.Type == types.DepParentChild && dep.DependsOnID == *parentID { - return true - } - } - return false -} - -// matchesLabelFilter is the label half of the blocked body's filter -// (sqlbuild.LabelSetClauses in issueops.GetBlockedIssuesInTx), for a row the -// decoration adds after that query ran: every Labels entry present, at least -// one LabelsAny entry present when any is named, and no ExcludeLabels entry -// present. It compares with Go string equality, which is the SQL comparison -// under the collation every beads table is created with (utf8mb4_0900_bin; -// see issueops' createBatchCache). -func matchesLabelFilter(labels []string, filter types.WorkFilter) bool { - for _, label := range filter.Labels { - if !slices.Contains(labels, label) { - return false - } - } - if len(filter.LabelsAny) > 0 && !slices.ContainsFunc(filter.LabelsAny, func(label string) bool { - return slices.Contains(labels, label) - }) { - return false - } - for _, label := range filter.ExcludeLabels { - if slices.Contains(labels, label) { - return false - } - } - return true -} - -// IsBlocked includes explicit unsatisfied external blockers in the close guard. -// A store that serves its roles refuses the reads this makes, so over one the -// per-issue roles in remote_roles.go answer instead. -func (s *Store) IsBlocked(ctx context.Context, issueID string) (bool, []string, error) { - blocked, blockers, err := s.inner.IsBlocked(ctx, issueID) - if err != nil { - return false, nil, err - } - deps, err := s.inner.GetDependencyRecordsForIssues(ctx, []string{issueID}) - if err != nil { - return false, nil, err - } - refs := make([]reference, 0) - for _, dep := range deps[issueID] { - if dep != nil && dep.Type.IsBlockingEdge() && isExternalReference(dep.DependsOnID) { - refs = append(refs, parseReference(dep.DependsOnID)) - } - } - satisfied, err := s.resolveReferences(ctx, refs) - if err != nil { - return false, nil, err - } - for _, ref := range refs { - if !satisfied[ref.raw] { - blockers = appendUnique(blockers, ref.raw) - } - } - return blocked || len(blockers) > 0, blockers, nil -} - -// blockedState is one id's IsBlocked answer. -type blockedState struct { - blocked bool - blockers []string -} - -// blockedStates answers IsBlocked for every one of ids with the same per-id -// result, in a bounded number of reads: the local half through the inner -// store's storage.BlockersBatchStore (one IsBlocked per id on a store without -// it), and the external half from ONE read of the ids' own edges, whose refs -// are resolved together, so each foreign project is opened at most once -// however many ids name it. -func (s *Store) blockedStates(ctx context.Context, ids []string) (map[string]blockedState, error) { - distinct := make([]string, 0, len(ids)) - seen := make(map[string]struct{}, len(ids)) - for _, id := range ids { - if _, dup := seen[id]; dup { - continue - } - seen[id] = struct{}{} - distinct = append(distinct, id) - } - states, err := s.localBlockedStates(ctx, distinct) - if err != nil { - return nil, err - } - deps, err := s.inner.GetDependencyRecordsForIssues(ctx, distinct) - if err != nil { - return nil, err - } - refs := make([]reference, 0) - for _, id := range distinct { - for _, dep := range deps[id] { - if dep != nil && dep.Type.IsBlockingEdge() && isExternalReference(dep.DependsOnID) { - refs = append(refs, parseReference(dep.DependsOnID)) - } - } - } - satisfied, err := s.resolveReferences(ctx, refs) - if err != nil { - return nil, err - } - for _, id := range distinct { - state := states[id] - for _, dep := range deps[id] { - if dep != nil && dep.Type.IsBlockingEdge() && isExternalReference(dep.DependsOnID) && !satisfied[dep.DependsOnID] { - state.blockers = appendUnique(state.blockers, dep.DependsOnID) - } - } - state.blocked = state.blocked || len(state.blockers) > 0 - states[id] = state - } - return states, nil -} - -// localBlockedStates is the inner store's IsBlocked for every one of ids. -func (s *Store) localBlockedStates(ctx context.Context, ids []string) (map[string]blockedState, error) { - states := make(map[string]blockedState, len(ids)) - if batch, ok := storage.UnwrapStore(s.inner).(storage.BlockersBatchStore); ok { - blocked, err := batch.IsBlockedWithBlockersBatch(ctx, ids) - if err != nil { - return nil, err - } - for id, blockers := range blocked { - states[id] = blockedState{blocked: true, blockers: blockers} - } - return states, nil - } - for _, id := range ids { - blocked, blockers, err := s.inner.IsBlocked(ctx, id) - if err != nil { - return nil, err - } - states[id] = blockedState{blocked: blocked, blockers: blockers} - } - return states, nil -} - -// IsBlockedBatch preserves the external blocker invariant for batch callers. -// The embedded Dolt implementation promotes this method from the wrapped -// store, so it must be declared explicitly here rather than relying on -// IsBlocked alone. Over a store that serves its roles the flag comes from the -// server's annotator (dependencies.blocking.isBlocked), and -// remoteExternalBlocked adds the invariant unless the server already applies -// the policy; only IsBlocked still refuses there. -func (s *Store) IsBlockedBatch(ctx context.Context, issueIDs []string) (map[string]bool, error) { - blocked, err := s.inner.IsBlockedBatch(ctx, issueIDs) - if err != nil { - return nil, err - } - if s.rolesAreRemote() { - // Over a store that serves its roles the raw edge read below is - // refused; the flag came from the server's annotator, and the - // external invariant is added from the edges of these rows only — - // unless the server already applies the policy itself. - return s.remoteExternalBlocked(ctx, issueIDs, blocked) - } - deps, err := s.inner.GetDependencyRecordsForIssues(ctx, issueIDs) - if err != nil { - return nil, err - } - refs := make([]reference, 0) - for _, issueDeps := range deps { - for _, dep := range issueDeps { - if dep != nil && dep.Type.IsBlockingEdge() && isExternalReference(dep.DependsOnID) { - refs = append(refs, parseReference(dep.DependsOnID)) - } - } - } - satisfied, err := s.resolveReferences(ctx, refs) - if err != nil { - return nil, err - } - for issueID, issueDeps := range deps { - for _, dep := range issueDeps { - if dep != nil && dep.Type.IsBlockingEdge() && isExternalReference(dep.DependsOnID) && !satisfied[dep.DependsOnID] { - blocked[issueID] = true - } - } - } - return blocked, nil -} - -// CloseIssueChecked applies the external guard before the atomic local close. -// The local store cannot see foreign capability state, so promoting its method -// would allow an externally blocked issue to close without --force. A store -// that serves its roles refuses this method; bd close goes through -// remoteBatchCloser (remote_roles.go) there. -func (s *Store) CloseIssueChecked(ctx context.Context, issueID, actor string, opts storage.CloseIssueOptions) (storage.CloseIssueResult, error) { - ctx = storage.WithLivePolicyProbe(ctx) - if !opts.Force { - issue, err := s.inner.GetIssue(ctx, issueID) - if err != nil { - return storage.CloseIssueResult{}, err - } - if issue != nil && issue.Status != types.StatusClosed { - blocked, blockers, err := s.IsBlocked(ctx, issueID) - if err != nil { - return storage.CloseIssueResult{}, err - } - if blocked && len(blockers) > 0 { - return storage.CloseIssueResult{}, publicops.NewCloseBlockedError(issueID, blockers) - } - } - } - return s.inner.CloseIssueChecked(ctx, issueID, actor, opts) -} - -// GetDependencyTree appends external refs as synthetic leaf nodes because no -// local issue row exists for the normal graph hydrator to return. A store that -// serves its roles refuses the reads this makes; bd dep tree goes through -// remoteTreeWalker (remote_roles.go) there. -func (s *Store) GetDependencyTree(ctx context.Context, issueID string, maxDepth int, showAllPaths bool, reverse bool) ([]*types.TreeNode, error) { - tree, err := s.inner.GetDependencyTree(ctx, issueID, maxDepth, showAllPaths, reverse) - if err != nil || reverse || len(tree) == 0 { - return tree, err - } - - issueIDs := make([]string, 0, len(tree)) - for _, node := range tree { - if node != nil && !isExternalReference(node.ID) { - issueIDs = append(issueIDs, node.ID) - } - } - deps, err := s.inner.GetDependencyRecordsForIssues(ctx, issueIDs) - if err != nil { - return nil, fmt.Errorf("external dependencies: load tree edges: %w", err) - } - return s.appendTreeExternalReferences(ctx, tree, deps, maxDepth, showAllPaths) -} - -func (s *Store) appendTreeExternalReferences(ctx context.Context, tree []*types.TreeNode, deps map[string][]*types.Dependency, maxDepth int, showAllPaths bool) ([]*types.TreeNode, error) { - refs := make([]reference, 0) - for _, issueDeps := range deps { - for _, dep := range issueDeps { - if dep != nil && isExternalReference(dep.DependsOnID) { - refs = append(refs, parseReference(dep.DependsOnID)) - } - } - } - satisfied, err := s.resolveReferences(ctx, refs) - if err != nil { - return nil, err - } - - effectiveMaxDepth := maxDepth - if effectiveMaxDepth <= 0 { - effectiveMaxDepth = 50 - } - seen := make(map[string]bool, len(tree)+len(refs)) - for _, node := range tree { - if node != nil { - seen[node.ID] = true - } - } - for _, parent := range tree { - if parent == nil || parent.Depth >= effectiveMaxDepth { - continue - } - for _, dep := range deps[parent.ID] { - if dep == nil || !isExternalReference(dep.DependsOnID) { - continue - } - if !showAllPaths && seen[dep.DependsOnID] { - continue - } - ref := parseReference(dep.DependsOnID) - status := types.StatusOpen - title := "○ " + externalTitle(ref) - if satisfied[ref.raw] { - status = types.StatusClosed - title = "✓ " + externalTitle(ref) - } - tree = append(tree, &types.TreeNode{ - Issue: types.Issue{ - ID: dep.DependsOnID, - Title: title, - Status: status, - IssueType: types.TypeTask, - }, - Depth: parent.Depth + 1, - ParentID: parent.ID, - EdgeFromParent: dep.Type, - }) - seen[dep.DependsOnID] = true - } - } - return tree, nil -} - -func externalTitle(ref reference) string { - if ref.valid { - return string(ref.capability) - } - return ref.raw -} - -// IterReadyWork preserves the decorator semantics for iterator callers. -func (s *Store) IterReadyWork(ctx context.Context, filter types.WorkFilter) (storage.Iter[types.Issue], error) { - issues, err := s.GetReadyWork(ctx, filter) - if err != nil { - return nil, err - } - return storage.NewSliceIter(issues), nil -} - -// IterBlockedIssues preserves the decorator semantics for iterator callers. -func (s *Store) IterBlockedIssues(ctx context.Context, filter types.WorkFilter) (storage.Iter[types.BlockedIssue], error) { - issues, err := s.GetBlockedIssues(ctx, filter) - if err != nil { - return nil, err - } - return storage.NewSliceIter(issues), nil -} - -func appendUnique(values []string, value string) []string { - if slices.Contains(values, value) { - return values - } - return append(values, value) -} diff --git a/internal/storage/externaldeps/decorator_test.go b/internal/storage/externaldeps/decorator_test.go deleted file mode 100644 index ac15047aa..000000000 --- a/internal/storage/externaldeps/decorator_test.go +++ /dev/null @@ -1,1236 +0,0 @@ -package externaldeps - -import ( - "context" - "errors" - "fmt" - "maps" - "slices" - "testing" - "time" - - "github.com/steveyegge/beads/internal/storage" - "github.com/steveyegge/beads/internal/storage/issueops" - "github.com/steveyegge/beads/internal/types" - publicops "github.com/steveyegge/beads/issueops" -) - -type fakeStore struct { - storage.DoltStorage - ready []*types.Issue - blocked []*types.BlockedIssue - tree []*types.TreeNode - deps map[string][]*types.Dependency - labels map[string][]*types.Issue - labelErr error - claimed []string - isBlocked bool - blockerIDs []string - closed []string - lifecycle publicops.Lifecycle - batch *fakeBatchCloser - enforced bool - enforcedErr error - // liveProbes records storage.LivePolicyProbe for every policy probe, in - // order. - liveProbes []bool - - excludeIDsUnsupported bool - // defaultPage, when positive, is how many rows a zero-Limit ready listing - // returns, while GetReadyWorkWithCountsAndTotal's total still counts the - // whole set: the shape httpclient.Store had while its wire omitted a zero - // limit and the server answered with its default page. That store now - // bounds an unlimited read itself, but the decorator still judges a window - // by the rows that came back, which this keeps pinned. Zero keeps the SQL - // stores' meaning, every row. - defaultPage int - // readyFilters records every GetReadyWorkWithCountsAndTotal filter, in - // order. - readyFilters []types.WorkFilter -} - -func (f *fakeStore) IssueLifecycle() (publicops.Lifecycle, error) { return f.lifecycle, nil } - -// ServerEnforcesExternalDependencyPolicy makes every fakeStore satisfy -// storage.ExternalDependencyPolicyProber. Defaulting to (false, nil) keeps -// every existing test's raw store on today's client-side-enforcement path -// unchanged; enforced:true is set explicitly only by the test below that -// pins loadBlockingState's skip branch. -func (f *fakeStore) ServerEnforcesExternalDependencyPolicy(ctx context.Context) (bool, error) { - f.liveProbes = append(f.liveProbes, storage.LivePolicyProbe(ctx)) - return f.enforced, f.enforcedErr -} - -// ExcludeIDsUnsupported likewise makes every fakeStore satisfy -// storage.ExcludeIDsUnsupportedStore. Defaulting to false keeps every existing -// test's exclusions folded into WorkFilter.ExcludeIDs; excludeIDsUnsupported: -// true models httpclient.Store, whose ready listings refuse ExcludeIDs (see -// GetReadyWork below), so the decorator must drop blocked rows client-side. -func (f *fakeStore) ExcludeIDsUnsupported() bool { return f.excludeIDsUnsupported } - -// BatchCloser without BatchCloserWithPolicy models a backend that predates -// storage.PolicyBatchCloserSource. -func (f *fakeStore) BatchCloser() (publicops.BatchCloser, error) { return f.batch, nil } - -type fakeBatchCloser struct{ closed int } - -func (f *fakeBatchCloser) CloseBatch(_ context.Context, request publicops.CloseBatchRequest) (publicops.CloseBatchResult, error) { - f.closed++ - return publicops.CloseBatchResult{Outcomes: make([]publicops.CloseOutcome, len(request.Items))}, nil -} - -type fakeLifecycle struct { - publicops.Lifecycle - closed int - updated int -} - -func (f *fakeLifecycle) Close(context.Context, publicops.CloseRequest) (publicops.CloseResult, error) { - f.closed++ - return publicops.CloseResult{}, nil -} - -func (f *fakeLifecycle) Update(context.Context, publicops.UpdateRequest) (publicops.UpdateResult, error) { - f.updated++ - return publicops.UpdateResult{}, nil -} - -func (f *fakeStore) GetReadyWork(_ context.Context, filter types.WorkFilter) ([]*types.Issue, error) { - candidates, err := f.readyCandidates(filter) - if err != nil { - return nil, err - } - limit := filter.Limit - if limit <= 0 { - limit = f.defaultPage - } - return page(candidates, filter.Offset, limit), nil -} - -// readyCandidates is the whole ready set filter admits, before paging. -func (f *fakeStore) readyCandidates(filter types.WorkFilter) ([]*types.Issue, error) { - if f.excludeIDsUnsupported && len(filter.ExcludeIDs) > 0 { - return nil, errors.New("fakeStore: ExcludeIDs cannot be expressed") - } - candidates := make([]*types.Issue, 0, len(f.ready)) - for _, issue := range f.ready { - if !slices.Contains(filter.ExcludeIDs, issue.ID) { - candidates = append(candidates, issue) - } - } - return candidates, nil -} - -func (f *fakeStore) GetReadyWorkWithCounts(ctx context.Context, filter types.WorkFilter) ([]*types.IssueWithCounts, error) { - issues, err := f.GetReadyWork(ctx, filter) - if err != nil { - return nil, err - } - result := make([]*types.IssueWithCounts, 0, len(issues)) - for _, issue := range issues { - result = append(result, &types.IssueWithCounts{Issue: issue}) - } - return result, nil -} - -func (f *fakeStore) GetReadyWorkWithCountsAndTotal(ctx context.Context, filter types.WorkFilter) ([]*types.IssueWithCounts, int, error) { - f.readyFilters = append(f.readyFilters, filter) - items, err := f.GetReadyWorkWithCounts(ctx, filter) - if err != nil { - return nil, 0, err - } - all, err := f.readyCandidates(filter) - if err != nil { - return nil, 0, err - } - return items, len(all), nil -} - -func (f *fakeStore) GetBlockedIssues(_ context.Context, _ types.WorkFilter) ([]*types.BlockedIssue, error) { - return slices.Clone(f.blocked), nil -} - -func (f *fakeStore) IsBlocked(_ context.Context, _ string) (bool, []string, error) { - return f.isBlocked, slices.Clone(f.blockerIDs), nil -} - -func (f *fakeStore) IsBlockedBatch(_ context.Context, ids []string) (map[string]bool, error) { - result := make(map[string]bool, len(ids)) - for _, id := range ids { - result[id] = f.isBlocked - } - return result, nil -} - -// BlockingAnnotator serves the decoration a local backend derives for these -// rows: no local blockers, and when asked, isBlocked as each row's stored -// flag. -func (f *fakeStore) BlockingAnnotator() (publicops.BlockingAnnotator, error) { - return localAnnotator{f}, nil -} - -type localAnnotator struct{ f *fakeStore } - -func (a localAnnotator) AnnotateBlocking(ctx context.Context, req publicops.BlockingRequest) (publicops.BlockingResult, error) { - items := make([]publicops.IssueBlocking, 0, len(req.IDs)) - for _, id := range req.IDs { - item := publicops.IssueBlocking{ID: id} - if issue, _ := a.f.GetIssue(ctx, id); issue != nil && req.IncludeIsBlocked { - item.IsBlocked = boolPtr(a.f.isBlocked) - } - items = append(items, item) - } - return publicops.BlockingResult{Items: items}, nil -} - -func (f *fakeStore) CloseIssueChecked(ctx context.Context, id, _ string, _ storage.CloseIssueOptions) (storage.CloseIssueResult, error) { - issue, err := f.GetIssue(ctx, id) - if err != nil { - return storage.CloseIssueResult{}, err - } - if issue == nil { - return storage.CloseIssueResult{}, storage.ErrNotFound - } - if issue.Status == types.StatusClosed { - return storage.CloseIssueResult{Unchanged: true}, nil - } - issue.Status = types.StatusClosed - f.closed = append(f.closed, id) - return storage.CloseIssueResult{}, nil -} - -func (f *fakeStore) GetAllDependencyRecords(_ context.Context) (map[string][]*types.Dependency, error) { - return f.deps, nil -} - -func (f *fakeStore) GetExternalBlockingDependencyRecords(_ context.Context) (map[string][]*types.Dependency, error) { - return f.deps, nil -} - -func (f *fakeStore) GetDependencyRecordsForIssues(_ context.Context, issueIDs []string) (map[string][]*types.Dependency, error) { - result := make(map[string][]*types.Dependency) - for _, id := range issueIDs { - result[id] = f.deps[id] - } - return result, nil -} - -func (f *fakeStore) GetIssuesByLabel(_ context.Context, label string) ([]*types.Issue, error) { - if f.labelErr != nil { - return nil, f.labelErr - } - return f.labels[label], nil -} - -func (f *fakeStore) GetDependencyTree(_ context.Context, _ string, _ int, _ bool, _ bool) ([]*types.TreeNode, error) { - return slices.Clone(f.tree), nil -} - -func (f *fakeStore) ClaimIssue(_ context.Context, id, actor string) error { - for _, issue := range f.ready { - if issue.ID == id { - if issue.Assignee != "" { - return storage.ErrAlreadyClaimed - } - issue.Assignee = actor - issue.Status = types.StatusInProgress - f.claimed = append(f.claimed, id) - return nil - } - } - return storage.ErrNotFound -} - -func (f *fakeStore) ClaimReadyIssue(ctx context.Context, filter types.WorkFilter, actor string) (*types.Issue, error) { - filter.Limit = 1 - issues, err := f.GetReadyWork(ctx, filter) - if err != nil || len(issues) == 0 { - return nil, err - } - if err := f.ClaimIssue(ctx, issues[0].ID, actor); err != nil { - return nil, err - } - return f.GetIssue(ctx, issues[0].ID) -} - -func (f *fakeStore) GetIssue(_ context.Context, id string) (*types.Issue, error) { - for _, issue := range f.ready { - if issue.ID == id { - return issue, nil - } - } - for _, blocked := range f.blocked { - if blocked.ID == id { - return &blocked.Issue, nil - } - } - return nil, nil -} - -func (f *fakeStore) GetIssuesByIDs(ctx context.Context, ids []string) ([]*types.Issue, error) { - result := make([]*types.Issue, 0, len(ids)) - for _, id := range ids { - issue, err := f.GetIssue(ctx, id) - if err != nil { - return nil, err - } - if issue != nil { - result = append(result, issue) - } - } - return result, nil -} - -func (f *fakeStore) Close() error { return nil } - -func page[T any](items []*T, offset, limit int) []*T { - if offset >= len(items) { - return nil - } - if offset < 0 { - offset = 0 - } - end := len(items) - if limit > 0 && offset+limit < end { - end = offset + limit - } - return slices.Clone(items[offset:end]) -} - -func issue(id string) *types.Issue { - return &types.Issue{ID: id, Title: id, Status: types.StatusOpen, IssueType: types.TypeTask} -} - -func blockedIssueIDs(items []*types.BlockedIssue) []string { - ids := make([]string, 0, len(items)) - for _, item := range items { - if item != nil { - ids = append(ids, item.ID) - } - } - return ids -} - -func externalDep(source, ref string, depType types.DependencyType) *types.Dependency { - return &types.Dependency{IssueID: source, DependsOnID: ref, Type: depType} -} - -func testStore(raw storage.DoltStorage, foreign *fakeStore, configured bool) *Store { - return New( - raw, - func(project ProjectName) (string, bool) { - if !configured || project != "remote" { - return "", false - } - return "/projects/remote", true - }, - func(_ context.Context, path string) (storage.DoltStorage, error) { - if path != "/projects/remote" { - return nil, errors.New("unexpected path") - } - return foreign, nil - }, - ) -} - -func TestGetReadyWorkFiltersExternalBlockerAndFillsPage(t *testing.T) { - a, b, c := issue("be-a"), issue("be-b"), issue("be-c") - raw := &fakeStore{ - ready: []*types.Issue{a, b, c}, - deps: map[string][]*types.Dependency{ - a.ID: {externalDep(a.ID, "external:remote:payments", types.DepBlocks)}, - }, - } - store := testStore(raw, &fakeStore{}, true) - - got, err := store.GetReadyWork(t.Context(), types.WorkFilter{Limit: 2}) - if err != nil { - t.Fatalf("GetReadyWork: %v", err) - } - if ids := issueIDs(got); !slices.Equal(ids, []string{"be-b", "be-c"}) { - t.Fatalf("ready IDs = %v, want [be-b be-c]", ids) - } -} - -func TestGetReadyWorkHonorsShippedAndNonBlockingExternalRefs(t *testing.T) { - a, b := issue("be-a"), issue("be-b") - raw := &fakeStore{ - ready: []*types.Issue{a, b}, - deps: map[string][]*types.Dependency{ - a.ID: {externalDep(a.ID, "external:remote:payments", types.DepBlocks)}, - b.ID: {externalDep(b.ID, "external:remote:future", types.DepTracks)}, - }, - } - foreign := &fakeStore{labels: map[string][]*types.Issue{ - "provides:payments": {{ID: "remote-done", Status: types.StatusClosed}}, - }} - store := testStore(raw, foreign, true) - - got, err := store.GetReadyWork(t.Context(), types.WorkFilter{}) - if err != nil { - t.Fatalf("GetReadyWork: %v", err) - } - if ids := issueIDs(got); !slices.Equal(ids, []string{"be-a", "be-b"}) { - t.Fatalf("ready IDs = %v, want [be-a be-b]", ids) - } -} - -func TestGetReadyWorkFailsClosedForUnconfiguredProject(t *testing.T) { - a := issue("be-a") - raw := &fakeStore{ - ready: []*types.Issue{a}, - deps: map[string][]*types.Dependency{ - a.ID: {externalDep(a.ID, "external:remote:payments", types.DepBlocks)}, - }, - } - store := testStore(raw, &fakeStore{}, false) - - got, err := store.GetReadyWork(t.Context(), types.WorkFilter{}) - if err != nil { - t.Fatalf("GetReadyWork: %v", err) - } - if len(got) != 0 { - t.Fatalf("ready = %v, want no issues", issueIDs(got)) - } -} - -// TestLoadBlockingStateSkipsClientSideEnforcementWhenServerReportsItEnforced -// pins design 3.6's skip branch in loadBlockingState (internal/storage/ -// externaldeps/decorator.go) on its CURRENT shape: a remote store -// implementing storage.ExternalDependencyPolicyProber that reports -// enforced=true must stop this decorator from applying its own client-side -// exclusion at all. The S6 review's M5 mutation targeted an earlier, cruder -// "skip whenever the backend is remote" implementation that the HIGH-1 fix -// replaced with this probe-based design, so M5's exact mutation target no -// longer exists; this test gives the probe's own skip branch equivalent -// mutation coverage. Nothing else in the suite exercises enforced==true: see -// internal/httpclient/served_external_dependency_policy_test.go, which -// documents exercising only enforced==false against a real served server -// (its harness serves raw roles, so it withholds the capability). -// -// configured=false on the foreign-project locator reproduces the fail-closed -// path from TestGetReadyWorkFailsClosedForUnconfiguredProject: if the -// `if enforced { return blockingState{}, nil }` skip were removed or -// inverted, this would fall through to ordinary client-side enforcement, -// which cannot resolve "remote" and would exclude be-a. Seeing be-a survive -// is therefore proof the skip branch fired. -func TestLoadBlockingStateSkipsClientSideEnforcementWhenServerReportsItEnforced(t *testing.T) { - a := issue("be-a") - raw := &fakeStore{ - ready: []*types.Issue{a}, - deps: map[string][]*types.Dependency{ - a.ID: {externalDep(a.ID, "external:remote:payments", types.DepBlocks)}, - }, - enforced: true, - } - store := testStore(raw, &fakeStore{}, false) - - got, err := store.GetReadyWork(t.Context(), types.WorkFilter{}) - if err != nil { - t.Fatalf("GetReadyWork: %v", err) - } - if ids := issueIDs(got); !slices.Equal(ids, []string{a.ID}) { - t.Fatalf("ready IDs = %v, want [%s] (a server-enforced probe should skip this decorator's own client-side exclusion)", ids, a.ID) - } -} - -func TestGetReadyWorkFailsClosedWhenForeignReadFails(t *testing.T) { - a := issue("be-a") - raw := &fakeStore{ready: []*types.Issue{a}, deps: map[string][]*types.Dependency{ - a.ID: {externalDep(a.ID, "external:remote:payments", types.DepBlocks)}, - }} - store := testStore(raw, &fakeStore{labelErr: errors.New("foreign database unavailable")}, true) - - got, err := store.GetReadyWork(t.Context(), types.WorkFilter{}) - if err != nil { - t.Fatalf("GetReadyWork: %v", err) - } - if len(got) != 0 { - t.Fatalf("ready = %v, want no issues after foreign read failure", issueIDs(got)) - } -} - -func TestGetReadyWorkOpensEachForeignProjectOnce(t *testing.T) { - a, b := issue("be-a"), issue("be-b") - raw := &fakeStore{ - ready: []*types.Issue{a, b}, - deps: map[string][]*types.Dependency{ - a.ID: {externalDep(a.ID, "external:remote:payments", types.DepBlocks)}, - b.ID: {externalDep(b.ID, "external:remote:identity", types.DepBlocks)}, - }, - } - foreign := &fakeStore{} - openCount := 0 - store := New( - raw, - func(ProjectName) (string, bool) { return "/projects/remote", true }, - func(context.Context, string) (storage.DoltStorage, error) { - openCount++ - return foreign, nil - }, - ) - - if _, err := store.GetReadyWork(t.Context(), types.WorkFilter{}); err != nil { - t.Fatalf("GetReadyWork: %v", err) - } - if openCount != 1 { - t.Fatalf("foreign project opens = %d, want 1", openCount) - } -} - -func TestClaimReadyIssueSkipsExternalBlocker(t *testing.T) { - a, b := issue("be-a"), issue("be-b") - raw := &fakeStore{ - ready: []*types.Issue{a, b}, - deps: map[string][]*types.Dependency{ - a.ID: {externalDep(a.ID, "external:remote:payments", types.DepBlocks)}, - }, - } - store := testStore(raw, &fakeStore{}, true) - - got, err := store.ClaimReadyIssue(t.Context(), types.WorkFilter{}, "worker") - if err != nil { - t.Fatalf("ClaimReadyIssue: %v", err) - } - if got == nil || got.ID != b.ID { - t.Fatalf("claimed = %+v, want %s", got, b.ID) - } - if !slices.Equal(raw.claimed, []string{b.ID}) { - t.Fatalf("claim attempts = %v, want [%s]", raw.claimed, b.ID) - } -} - -func TestCountReadyWorkUsesExternalPolicy(t *testing.T) { - a, b := issue("be-a"), issue("be-b") - raw := &fakeStore{ - ready: []*types.Issue{a, b}, - deps: map[string][]*types.Dependency{ - a.ID: {externalDep(a.ID, "external:remote:payments", types.DepBlocks)}, - }, - } - store := testStore(raw, &fakeStore{}, true) - - got, err := store.CountReadyWork(t.Context(), types.WorkFilter{}) - if err != nil { - t.Fatalf("CountReadyWork: %v", err) - } - if got != 1 { - t.Fatalf("ready count = %d, want 1", got) - } -} - -// TestReadyPageTotalUsesExternalPolicy: the page `bd ready --json` lists and -// the total it prints beside it must both exclude externally blocked work. The -// decorator embeds the inner store, so without its own override this method -// would pass straight through and count the excluded rows. -func TestReadyPageTotalUsesExternalPolicy(t *testing.T) { - a, b, c := issue("be-a"), issue("be-b"), issue("be-c") - raw := &fakeStore{ - ready: []*types.Issue{a, b, c}, - deps: map[string][]*types.Dependency{ - a.ID: {externalDep(a.ID, "external:remote:payments", types.DepBlocks)}, - }, - } - store := testStore(raw, &fakeStore{}, true) - - items, total, err := store.GetReadyWorkWithCountsAndTotal(t.Context(), types.WorkFilter{Limit: 1}) - if err != nil { - t.Fatalf("GetReadyWorkWithCountsAndTotal: %v", err) - } - if len(items) != 1 || items[0].ID != b.ID { - t.Fatalf("page = %v, want [%s]", items, b.ID) - } - if total != 2 { - t.Fatalf("total = %d, want 2 (be-a is externally blocked)", total) - } -} - -// TestReadyTotalsAgreeAcrossOutputModes: text `bd ready` sizes a full page -// through the ReadyCounter role, `bd ready --json` through the in-band total. -// With an externally blocked issue present both must exclude it; before the -// decorator overrode ReadyCounter, the text total passed through to the inner -// store and counted it. -func TestReadyTotalsAgreeAcrossOutputModes(t *testing.T) { - a, b, c := issue("be-a"), issue("be-b"), issue("be-c") - raw := &fakeStore{ - ready: []*types.Issue{a, b, c}, - deps: map[string][]*types.Dependency{ - a.ID: {externalDep(a.ID, "external:remote:payments", types.DepBlocks)}, - }, - } - store := testStore(raw, &fakeStore{}, true) - - counter, err := store.ReadyCounter() - if err != nil { - t.Fatalf("ReadyCounter: %v", err) - } - text, err := counter.CountReady(t.Context(), publicops.ReadyRequest{}) - if err != nil { - t.Fatalf("CountReady: %v", err) - } - _, jsonTotal, err := store.GetReadyWorkWithCountsAndTotal(t.Context(), types.WorkFilter{Limit: 1}) - if err != nil { - t.Fatalf("GetReadyWorkWithCountsAndTotal: %v", err) - } - if text.Total != 2 || int(text.Total) != jsonTotal { - t.Fatalf("text total = %d, json total = %d; want both 2 (be-a is externally blocked)", text.Total, jsonTotal) - } -} - -// TestReadyTotalsWhenStoreCannotExpressExcludeIDs pins the path httpclient.Store -// takes: the decorator bumps Limit and drops externally blocked rows -// client-side, and the page, its total, and CountReadyWork must all still -// exclude them. be-a comes back first, ahead of the rows kept — the order in -// which compacting the fetched rows in place once overwrote be-a before the -// total's blocked-row count reached it, so the total came out 3, not 2. -func TestReadyTotalsWhenStoreCannotExpressExcludeIDs(t *testing.T) { - a, b, c := issue("be-a"), issue("be-b"), issue("be-c") - raw := &fakeStore{ - ready: []*types.Issue{a, b, c}, - deps: map[string][]*types.Dependency{ - a.ID: {externalDep(a.ID, "external:remote:payments", types.DepBlocks)}, - }, - excludeIDsUnsupported: true, - } - store := testStore(raw, &fakeStore{}, true) - - for _, tc := range []struct { - name string - limit int - wantIDs []string - }{ - {name: "unlimited", limit: 0, wantIDs: []string{b.ID, c.ID}}, - // The bumped window (2+1) holds the whole ready set: the total is exact. - {name: "window holds the set", limit: 2, wantIDs: []string{b.ID, c.ID}}, - // The ready set outgrows the bumped window (1+1): the total is the - // inner total less every externally blocked issue. - {name: "set outgrows the window", limit: 1, wantIDs: []string{b.ID}}, - } { - t.Run(tc.name, func(t *testing.T) { - filter := types.WorkFilter{Limit: tc.limit} - issues, err := store.GetReadyWork(t.Context(), filter) - if err != nil { - t.Fatalf("GetReadyWork: %v", err) - } - if ids := issueIDs(issues); !slices.Equal(ids, tc.wantIDs) { - t.Fatalf("GetReadyWork IDs = %v, want %v", ids, tc.wantIDs) - } - rows, total, err := store.GetReadyWorkWithCountsAndTotal(t.Context(), filter) - if err != nil { - t.Fatalf("GetReadyWorkWithCountsAndTotal: %v", err) - } - ids := make([]string, 0, len(rows)) - for _, row := range rows { - ids = append(ids, row.ID) - } - if !slices.Equal(ids, tc.wantIDs) || total != 2 { - t.Fatalf("page = %v, total = %d; want %v, total 2 (be-a is externally blocked)", ids, total, tc.wantIDs) - } - }) - } - - got, err := store.CountReadyWork(t.Context(), types.WorkFilter{}) - if err != nil { - t.Fatalf("CountReadyWork: %v", err) - } - if got != 2 { - t.Fatalf("ready count = %d, want 2 (be-a is externally blocked)", got) - } -} - -// TestCountReadyWorkFallbackFetchesABoundedPage pins what CountReadyWork asks -// of a store with no ReadyWorkCounter: a one-row Lite page whose total is the -// store's own count, never a zero Limit. Over http a zero Limit is an -// unlimited read, which would download every ready row to report one number -// and be refused past BEADS_HTTP_UNLIMITED_READ_CAP. -func TestCountReadyWorkFallbackFetchesABoundedPage(t *testing.T) { - a, b, c := issue("be-a"), issue("be-b"), issue("be-c") - for _, tc := range []struct { - name string - deps map[string][]*types.Dependency - want int - // wantFirst is the first fetch's Limit: the one-row page, bumped by - // every externally blocked id, since this store drops them itself. - wantFirst int - }{ - {name: "nothing externally blocked", want: 3, wantFirst: 1}, - { - name: "one issue externally blocked", - deps: map[string][]*types.Dependency{ - a.ID: {externalDep(a.ID, "external:remote:payments", types.DepBlocks)}, - }, - want: 2, - wantFirst: 2, - }, - } { - t.Run(tc.name, func(t *testing.T) { - raw := &fakeStore{ready: []*types.Issue{a, b, c}, deps: tc.deps, excludeIDsUnsupported: true} - got, err := testStore(raw, &fakeStore{}, true).CountReadyWork(t.Context(), types.WorkFilter{}) - if err != nil { - t.Fatalf("CountReadyWork: %v", err) - } - if got != tc.want { - t.Fatalf("ready count = %d, want %d", got, tc.want) - } - if len(raw.readyFilters) == 0 { - t.Fatal("CountReadyWork fetched nothing; the fallback is not on the path") - } - if first := raw.readyFilters[0]; first.Limit != tc.wantFirst || !first.Lite { - t.Errorf("first fetch: Limit %d, Lite %v; want Limit %d, Lite true", first.Limit, first.Lite, tc.wantFirst) - } - for i, f := range raw.readyFilters { - if f.Limit <= 0 { - t.Errorf("fetch %d asked for every row (Limit %d)", i, f.Limit) - } - } - }) - } -} - -// TestReadyTotalsWhenAnUnlimitedFetchComesBackAsOnePage pins the shape -// httpclient.Store once had and an every-row Limit==0 fake hid: the wire -// omitted a zero Limit, the server answered with its default page, and the -// total still counted the whole ready set. CountReadyWork, text-mode bd -// ready's total, fetched with Limit 0 then, so be-z (ready, externally -// blocked, and past that page) stayed in its total because the decorator took -// a zero Limit for a window that held every row. The count now asks for a -// one-row page and the JSON total below still asks with Limit 0: both must -// leave be-z out. -func TestReadyTotalsWhenAnUnlimitedFetchComesBackAsOnePage(t *testing.T) { - const defaultPage = 100 // workapi.DefaultReadyLimit, the server's page for an omitted limit - ready := make([]*types.Issue, 0, defaultPage+2) - for i := range defaultPage + 1 { - ready = append(ready, issue(fmt.Sprintf("be-%03d", i))) - } - z := issue("be-z") - raw := &fakeStore{ - ready: append(ready, z), - deps: map[string][]*types.Dependency{ - z.ID: {externalDep(z.ID, "external:remote:payments", types.DepBlocks)}, - }, - excludeIDsUnsupported: true, - defaultPage: defaultPage, - } - store := testStore(raw, &fakeStore{}, true) - const want = defaultPage + 1 // every ready issue but be-z - - counter, err := store.ReadyCounter() - if err != nil { - t.Fatalf("ReadyCounter: %v", err) - } - text, err := counter.CountReady(t.Context(), publicops.ReadyRequest{}) - if err != nil { - t.Fatalf("CountReady: %v", err) - } - if text.Total != want { - t.Fatalf("text total = %d, want %d (be-z is externally blocked)", text.Total, want) - } - // The JSON total must agree, whether the page is unlimited (the server's - // default page comes back) or the CLI's default limit (bumped by one). - for _, limit := range []int{0, defaultPage} { - rows, total, err := store.GetReadyWorkWithCountsAndTotal(t.Context(), types.WorkFilter{Limit: limit}) - if err != nil { - t.Fatalf("GetReadyWorkWithCountsAndTotal(Limit %d): %v", limit, err) - } - if len(rows) != defaultPage || total != want { - t.Fatalf("Limit %d: %d rows, total %d; want %d rows, total %d (be-z is externally blocked)", limit, len(rows), total, defaultPage, want) - } - } -} - -// TestReadyTotalsIgnoreExternallyBlockedIssuesOutsideTheReadySet pins the -// total of a ready set bigger than the window that came back. refsByIssue -// names every workspace issue with an unsatisfied external blocker, closed -// ones and ones the filter excludes included, but only those in the filtered -// ready set were ever in the inner total, so subtracting all of them once -// reported 100 of 101 ready issues as soon as an issue outside the ready set -// held an external blocker. -func TestReadyTotalsIgnoreExternallyBlockedIssuesOutsideTheReadySet(t *testing.T) { - const defaultPage = 100 // workapi.DefaultReadyLimit, the server's page for an omitted limit - for _, tc := range []struct { - name string - // readyBlocked adds be-z: ready, externally blocked, and past the page. - readyBlocked bool - // outside is how many externally blocked issues are not ready. - outside int - }{ - {name: "one blocked issue outside the ready set", outside: 1}, - {name: "blocked issues inside and outside the ready set", readyBlocked: true, outside: 20}, - } { - t.Run(tc.name, func(t *testing.T) { - ready := make([]*types.Issue, 0, defaultPage+2) - for i := range defaultPage + 1 { - ready = append(ready, issue(fmt.Sprintf("be-%03d", i))) - } - deps := make(map[string][]*types.Dependency) - if tc.readyBlocked { - z := issue("be-z") - ready = append(ready, z) - deps[z.ID] = []*types.Dependency{externalDep(z.ID, "external:remote:payments", types.DepBlocks)} - } - for i := range tc.outside { - id := fmt.Sprintf("be-done-%02d", i) - deps[id] = []*types.Dependency{externalDep(id, "external:remote:payments", types.DepBlocks)} - } - raw := &fakeStore{ - ready: ready, - deps: deps, - excludeIDsUnsupported: true, - defaultPage: defaultPage, - } - store := testStore(raw, &fakeStore{}, true) - const want = defaultPage + 1 // every ready issue but be-z - - counter, err := store.ReadyCounter() - if err != nil { - t.Fatalf("ReadyCounter: %v", err) - } - text, err := counter.CountReady(t.Context(), publicops.ReadyRequest{}) - if err != nil { - t.Fatalf("CountReady: %v", err) - } - if text.Total != want { - t.Fatalf("text total = %d, want %d", text.Total, want) - } - for _, limit := range []int{0, defaultPage} { - rows, total, err := store.GetReadyWorkWithCountsAndTotal(t.Context(), types.WorkFilter{Limit: limit}) - if err != nil { - t.Fatalf("GetReadyWorkWithCountsAndTotal(Limit %d): %v", limit, err) - } - if len(rows) != defaultPage || total != want { - t.Fatalf("Limit %d: %d rows, total %d; want %d rows, total %d", limit, len(rows), total, defaultPage, want) - } - } - }) - } -} - -func TestGetBlockedIssuesAddsUnsatisfiedExternalRefs(t *testing.T) { - a, c := issue("be-a"), issue("be-c") - raw := &fakeStore{ - ready: []*types.Issue{a}, - blocked: []*types.BlockedIssue{{ - Issue: *c, - BlockedByCount: 1, - BlockedBy: []string{"be-local"}, - }}, - deps: map[string][]*types.Dependency{ - a.ID: {externalDep(a.ID, "external:remote:payments", types.DepBlocks)}, - c.ID: {externalDep(c.ID, "external:remote:identity", types.DepBlocks)}, - }, - } - store := testStore(raw, &fakeStore{}, true) - - got, err := store.GetBlockedIssues(t.Context(), types.WorkFilter{}) - if err != nil { - t.Fatalf("GetBlockedIssues: %v", err) - } - byID := make(map[string]*types.BlockedIssue, len(got)) - for _, blocked := range got { - byID[blocked.ID] = blocked - } - if !slices.Equal(byID[a.ID].BlockedBy, []string{"external:remote:payments"}) { - t.Fatalf("%s blockers = %v", a.ID, byID[a.ID].BlockedBy) - } - if !slices.Equal(byID[c.ID].BlockedBy, []string{"be-local", "external:remote:identity"}) { - t.Fatalf("%s blockers = %v", c.ID, byID[c.ID].BlockedBy) - } -} - -func TestIssueLifecycleRefusesExternalCloseAndDoneUpdate(t *testing.T) { - issue := issue("be-external") - lifecycle := &fakeLifecycle{} - raw := &fakeStore{ - ready: []*types.Issue{issue}, - lifecycle: lifecycle, - deps: map[string][]*types.Dependency{ - issue.ID: {externalDep(issue.ID, "external:remote:payments", types.DepBlocks)}, - }, - } - store := testStore(raw, &fakeStore{}, false) - ops, err := store.IssueLifecycle() - if err != nil { - t.Fatalf("IssueLifecycle: %v", err) - } - _, err = ops.Close(t.Context(), publicops.CloseRequest{IssueID: issue.ID}) - if !errors.Is(err, storage.ErrCloseBlocked) { - t.Fatalf("Close error = %v, want ErrCloseBlocked", err) - } - assertExternalCloseBlockers(t, err, issue.ID, "external:remote:payments") - if _, err := ops.Update(t.Context(), publicops.UpdateRequest{IssueID: issue.ID, Patch: publicops.IssuePatch{ - Status: publicops.Field[types.Status]{Set: true, Value: types.StatusClosed}, - }}); !errors.Is(err, storage.ErrCloseBlocked) { - t.Fatalf("Update error = %v, want ErrCloseBlocked", err) - } - if lifecycle.closed != 0 || lifecycle.updated != 0 { - t.Fatalf("inner lifecycle calls = close:%d update:%d, want zero", lifecycle.closed, lifecycle.updated) - } -} - -func TestIssueLifecycleClaimExternalPolicy(t *testing.T) { - for _, tc := range []struct { - name string - depType types.DependencyType - providerStatus types.Status - labelErr error - wantBlocked bool - }{ - {"unshipped", types.DepBlocks, types.StatusOpen, nil, true}, - {"shipped", types.DepBlocks, types.StatusClosed, nil, false}, - {"unreadable", types.DepBlocks, types.StatusClosed, errors.New("foreign read failed"), true}, - {"nonblocking", types.DepRelated, types.StatusOpen, nil, false}, - } { - t.Run(tc.name, func(t *testing.T) { - lifecycle := &fakeLifecycle{} - raw := &fakeStore{lifecycle: lifecycle, deps: map[string][]*types.Dependency{ - "be-consumer": {externalDep("be-consumer", "external:remote:payments", tc.depType)}, - }} - foreign := &fakeStore{labelErr: tc.labelErr, labels: map[string][]*types.Issue{ - "provides:payments": {{ID: "remote-provider", Status: tc.providerStatus}}, - }} - ops, err := testStore(raw, foreign, true).IssueLifecycle() - if err != nil { - t.Fatal(err) - } - _, err = ops.Update(t.Context(), publicops.UpdateRequest{IssueID: "be-consumer", Actor: "worker", Claim: true}) - if errors.Is(err, storage.ErrCloseBlocked) != tc.wantBlocked { - t.Fatalf("claim error = %v, want blocked=%v", err, tc.wantBlocked) - } - if !tc.wantBlocked && err != nil { - t.Fatal(err) - } - wantCalls := 1 - if tc.wantBlocked { - wantCalls = 0 - } - if lifecycle.updated != wantCalls { - t.Fatalf("inner updates = %d, want %d", lifecycle.updated, wantCalls) - } - }) - } -} - -// A backend without policy support still closes a batch that no external -// blocker touches. A blocked item or a next claim, which must exclude every -// blocked candidate, still refuses rather than bypassing the policy. -func TestBatchCloserScopesPolicyWithoutClaim(t *testing.T) { - for _, tc := range []struct { - name string - items []string - claimNext bool - wantUnsupported bool - }{ - {"unrelated_blocker", []string{"be-free"}, false, false}, - {"blocked_item", []string{"be-free", "be-consumer"}, false, true}, - {"claim_next", []string{"be-free"}, true, true}, - } { - t.Run(tc.name, func(t *testing.T) { - batch := &fakeBatchCloser{} - raw := &fakeStore{batch: batch, deps: map[string][]*types.Dependency{ - "be-consumer": {externalDep("be-consumer", "external:remote:payments", types.DepBlocks)}, - }} - foreign := &fakeStore{labels: map[string][]*types.Issue{ - "provides:payments": {{ID: "remote-provider", Status: types.StatusOpen}}, - }} - closer, err := testStore(raw, foreign, true).BatchCloser() - if err != nil { - t.Fatal(err) - } - request := publicops.CloseBatchRequest{Actor: "worker"} - for _, id := range tc.items { - request.Items = append(request.Items, publicops.BatchCloseItem{IssueID: id}) - } - if tc.claimNext { - request.ClaimNext = &publicops.ReadyRequest{} - } - _, err = closer.CloseBatch(t.Context(), request) - var unsupported *storage.ErrUnsupported - if errors.As(err, &unsupported) != tc.wantUnsupported { - t.Fatalf("close batch error = %v, want unsupported=%v", err, tc.wantUnsupported) - } - if !tc.wantUnsupported && err != nil { - t.Fatal(err) - } - wantCalls := 1 - if tc.wantUnsupported { - wantCalls = 0 - } - if batch.closed != wantCalls { - t.Fatalf("inner batches = %d, want %d", batch.closed, wantCalls) - } - }) - } -} - -func TestGetBlockedIssuesPaginatesCombinedExternalResults(t *testing.T) { - local, external := issue("be-local"), issue("be-external") - createdAt := time.Date(2026, time.August, 2, 0, 0, 0, 0, time.UTC) - local.Priority, external.Priority = 0, 0 - local.CreatedAt, external.CreatedAt = createdAt, createdAt - raw := &fakeStore{ - ready: []*types.Issue{external}, - blocked: []*types.BlockedIssue{{Issue: *local, BlockedBy: []string{"be-local-blocker"}, BlockedByCount: 1}}, - deps: map[string][]*types.Dependency{ - external.ID: {externalDep(external.ID, "external:remote:payments", types.DepBlocks)}, - }, - } - store := testStore(raw, &fakeStore{}, false) - - first, err := store.GetBlockedIssues(t.Context(), types.WorkFilter{Limit: 1}) - if err != nil { - t.Fatalf("GetBlockedIssues first page: %v", err) - } - if ids := blockedIssueIDs(first); !slices.Equal(ids, []string{external.ID}) { - t.Fatalf("first page IDs = %v, want [%s]", ids, external.ID) - } - second, err := store.GetBlockedIssues(t.Context(), types.WorkFilter{Offset: 1, Limit: 1}) - if err != nil { - t.Fatalf("GetBlockedIssues second page: %v", err) - } - if ids := blockedIssueIDs(second); !slices.Equal(ids, []string{local.ID}) { - t.Fatalf("second page IDs = %v, want [%s]", ids, local.ID) - } - _, err = store.GetBlockedIssues(t.Context(), types.WorkFilter{MaxRows: 1, MaxRowsSource: "--max-rows"}) - var capErr *issueops.ErrTooManyRows - if !errors.As(err, &capErr) { - t.Fatalf("GetBlockedIssues MaxRows error = %v, want ErrTooManyRows", err) - } -} - -func TestIsBlockedIncludesUnsatisfiedExternalRef(t *testing.T) { - a := issue("be-a") - ref := "external:remote:payments" - raw := &fakeStore{ - ready: []*types.Issue{a}, - deps: map[string][]*types.Dependency{ - a.ID: {externalDep(a.ID, ref, types.DepBlocks)}, - }, - } - store := testStore(raw, &fakeStore{}, true) - - blocked, blockers, err := store.IsBlocked(t.Context(), a.ID) - if err != nil { - t.Fatalf("IsBlocked: %v", err) - } - if !blocked || !slices.Equal(blockers, []string{ref}) { - t.Fatalf("IsBlocked = %v, %v; want true, [%s]", blocked, blockers, ref) - } -} - -func TestIsBlockedBatchIncludesUnsatisfiedExternalRef(t *testing.T) { - a, b := issue("be-a"), issue("be-b") - ref := "external:remote:payments" - raw := &fakeStore{ready: []*types.Issue{a, b}, deps: map[string][]*types.Dependency{ - a.ID: {externalDep(a.ID, ref, types.DepBlocks)}, - }} - store := testStore(raw, &fakeStore{}, true) - - got, err := store.IsBlockedBatch(t.Context(), []string{a.ID, b.ID}) - if err != nil { - t.Fatalf("IsBlockedBatch: %v", err) - } - if !got[a.ID] || got[b.ID] { - t.Fatalf("IsBlockedBatch = %v, want only %s blocked", got, a.ID) - } -} - -// TestBlockingAnnotatorFlagsUnsatisfiedExternalRef: the annotation's -// is_blocked flag keeps IsBlockedBatch's invariant, so a row the stored column -// calls unblocked reads blocked while an external ref still blocks it. -func TestBlockingAnnotatorFlagsUnsatisfiedExternalRef(t *testing.T) { - a, b := issue("be-a"), issue("be-b") - ref := "external:remote:payments" - raw := &fakeStore{ready: []*types.Issue{a, b}, deps: map[string][]*types.Dependency{ - a.ID: {externalDep(a.ID, ref, types.DepBlocks)}, - }} - annotator, err := testStore(raw, &fakeStore{}, true).BlockingAnnotator() - if err != nil { - t.Fatalf("BlockingAnnotator: %v", err) - } - - result, err := annotator.AnnotateBlocking(t.Context(), publicops.BlockingRequest{IDs: []string{a.ID, b.ID}, IncludeIsBlocked: true}) - if err != nil { - t.Fatalf("AnnotateBlocking: %v", err) - } - if got, want := blockedFlags(result), map[string]string{a.ID: "true", b.ID: "false"}; !maps.Equal(got, want) { - t.Fatalf("AnnotateBlocking is_blocked = %v, want %v", got, want) - } -} - -func TestCloseIssueCheckedRefusesUnsatisfiedExternalRef(t *testing.T) { - a := issue("be-a") - ref := "external:remote:payments" - raw := &fakeStore{ready: []*types.Issue{a}, deps: map[string][]*types.Dependency{ - a.ID: {externalDep(a.ID, ref, types.DepBlocks)}, - }} - store := testStore(raw, &fakeStore{}, true) - - _, err := store.CloseIssueChecked(t.Context(), a.ID, "tester", storage.CloseIssueOptions{}) - if !errors.Is(err, storage.ErrCloseBlocked) { - t.Fatalf("CloseIssueChecked error = %v, want ErrCloseBlocked", err) - } - assertExternalCloseBlockers(t, err, a.ID, ref) - if len(raw.closed) != 0 { - t.Fatalf("raw close calls = %v, want none", raw.closed) - } - if _, err := store.CloseIssueChecked(t.Context(), a.ID, "tester", storage.CloseIssueOptions{Force: true}); err != nil { - t.Fatalf("forced CloseIssueChecked: %v", err) - } -} - -func TestGetDependencyTreeAddsResolvedExternalLeaf(t *testing.T) { - root := issue("be-root") - ref := "external:remote:payments" - raw := &fakeStore{ - tree: []*types.TreeNode{{Issue: *root}}, - deps: map[string][]*types.Dependency{ - root.ID: {externalDep(root.ID, ref, types.DepBlocks)}, - }, - } - foreign := &fakeStore{labels: make(map[string][]*types.Issue)} - store := testStore(raw, foreign, true) - - tree, err := store.GetDependencyTree(t.Context(), root.ID, 10, false, false) - if err != nil { - t.Fatalf("GetDependencyTree: %v", err) - } - if len(tree) != 2 { - t.Fatalf("tree length = %d, want 2", len(tree)) - } - leaf := tree[1] - if leaf.ID != ref || leaf.Status != types.StatusOpen || leaf.ParentID != root.ID || leaf.EdgeFromParent != types.DepBlocks { - t.Fatalf("external leaf = %+v", leaf) - } - - foreign.labels["provides:payments"] = []*types.Issue{{ID: "remote-done", Status: types.StatusClosed}} - tree, err = store.GetDependencyTree(t.Context(), root.ID, 10, false, false) - if err != nil { - t.Fatalf("GetDependencyTree after ship: %v", err) - } - if tree[1].Status != types.StatusClosed { - t.Fatalf("shipped external status = %s, want closed", tree[1].Status) - } -} - -func TestUnavailableProjectWarnsOnce(t *testing.T) { - a := issue("be-a") - ref := "external:remote:payments" - raw := &fakeStore{ - ready: []*types.Issue{a}, - deps: map[string][]*types.Dependency{ - a.ID: {externalDep(a.ID, ref, types.DepBlocks)}, - }, - } - store := testStore(raw, &fakeStore{}, false) - var warnings []ProjectName - store.warnProject = func(project ProjectName) { warnings = append(warnings, project) } - - for range 2 { - if _, err := store.GetReadyWork(t.Context(), types.WorkFilter{}); err != nil { - t.Fatalf("GetReadyWork: %v", err) - } - } - if !slices.Equal(warnings, []ProjectName{"remote"}) { - t.Fatalf("warnings = %v, want [remote]", warnings) - } -} - -func TestUnwrapReturnsInnerStore(t *testing.T) { - raw := &fakeStore{} - store := testStore(raw, &fakeStore{}, true) - if got := store.Unwrap(); got != raw { - t.Fatalf("Unwrap() = %T %p, want raw %p", got, got, raw) - } -} - -func issueIDs(issues []*types.Issue) []string { - ids := make([]string, 0, len(issues)) - for _, issue := range issues { - ids = append(ids, issue.ID) - } - return ids -} - -// assertExternalCloseBlockers checks that an external close refusal carries the -// typed blocker list — the unsatisfied reference, as an external blocker with no -// reported edge type — and still spells the historical sentence. -func assertExternalCloseBlockers(t *testing.T, err error, issueID, ref string) { - t.Helper() - var blocked *publicops.BlockedError - if !errors.As(err, &blocked) { - t.Fatalf("close refusal %v carries no *BlockedError", err) - } - want := []publicops.Blocker{{ID: ref}} - if blocked.IssueID != issueID || !slices.Equal(blocked.Blockers, want) || !blocked.Blockers[0].External() { - t.Errorf("BlockedError = %+v, want issue %s blocked by %+v (external)", blocked, issueID, want) - } - if wantText := "cannot close blocked issue: " + issueID + " is blocked by [" + ref + "]"; err.Error() != wantText { - t.Errorf("refusal = %q, want %q", err.Error(), wantText) - } -} - -// TestIsBlockedBatchHonorsShippedExternalRef (bd-yewju3 store half): as -// GetReadyWork does, the flag leaves a row unblocked when its external ref's -// project has shipped it. -func TestIsBlockedBatchHonorsShippedExternalRef(t *testing.T) { - a := issue("be-a") - raw := &fakeStore{ready: []*types.Issue{a}, deps: map[string][]*types.Dependency{ - a.ID: {externalDep(a.ID, "external:remote:payments", types.DepBlocks)}, - }} - foreign := &fakeStore{labels: map[string][]*types.Issue{ - "provides:payments": {{ID: "remote-done", Status: types.StatusClosed}}, - }} - - got, err := testStore(raw, foreign, true).IsBlockedBatch(t.Context(), []string{a.ID}) - if err != nil { - t.Fatalf("IsBlockedBatch: %v", err) - } - if want := map[string]bool{a.ID: false}; !maps.Equal(got, want) { - t.Fatalf("IsBlockedBatch = %v, want %v", got, want) - } -} - -// TestBlockingAnnotatorLeavesShippedExternalRefUnblocked: the store arm's -// served flag reads IsBlocked, so a shipped ref leaves the row unblocked. -func TestBlockingAnnotatorLeavesShippedExternalRefUnblocked(t *testing.T) { - a := issue("be-a") - raw := &fakeStore{ready: []*types.Issue{a}, deps: map[string][]*types.Dependency{ - a.ID: {externalDep(a.ID, "external:remote:payments", types.DepBlocks)}, - }} - foreign := &fakeStore{labels: map[string][]*types.Issue{ - "provides:payments": {{ID: "remote-done", Status: types.StatusClosed}}, - }} - annotator, err := testStore(raw, foreign, true).BlockingAnnotator() - if err != nil { - t.Fatalf("BlockingAnnotator: %v", err) - } - - result, err := annotator.AnnotateBlocking(t.Context(), publicops.BlockingRequest{IDs: []string{a.ID}, IncludeIsBlocked: true}) - if err != nil { - t.Fatalf("AnnotateBlocking: %v", err) - } - if got, want := blockedFlags(result), map[string]string{a.ID: "false"}; !maps.Equal(got, want) { - t.Fatalf("AnnotateBlocking is_blocked = %v, want %v", got, want) - } -} - -// TestBlockingAnnotatorAddsNoFlagUnasked: a request that did not ask for -// is_blocked answers exactly as before, even for an externally blocked row. -func TestBlockingAnnotatorAddsNoFlagUnasked(t *testing.T) { - a := issue("be-a") - raw := &fakeStore{ready: []*types.Issue{a}, deps: map[string][]*types.Dependency{ - a.ID: {externalDep(a.ID, "external:remote:payments", types.DepBlocks)}, - }} - annotator, err := testStore(raw, &fakeStore{}, true).BlockingAnnotator() - if err != nil { - t.Fatalf("BlockingAnnotator: %v", err) - } - - result, err := annotator.AnnotateBlocking(t.Context(), publicops.BlockingRequest{IDs: []string{a.ID}}) - if err != nil { - t.Fatalf("AnnotateBlocking: %v", err) - } - if got, want := blockedFlags(result), map[string]string{a.ID: "absent"}; !maps.Equal(got, want) { - t.Fatalf("AnnotateBlocking is_blocked = %v, want %v", got, want) - } -} diff --git a/internal/storage/externaldeps/live_probe_test.go b/internal/storage/externaldeps/live_probe_test.go deleted file mode 100644 index d8cca4370..000000000 --- a/internal/storage/externaldeps/live_probe_test.go +++ /dev/null @@ -1,142 +0,0 @@ -package externaldeps - -import ( - "context" - "testing" - - "github.com/steveyegge/beads/internal/storage" - "github.com/steveyegge/beads/internal/types" - publicops "github.com/steveyegge/beads/issueops" -) - -// TestClaimsAndClosesAskForALivePolicyProbe pins which entry points mark -// their policy probe live (storage.WithLivePolicyProbe): every claim and close -// guard, so a cached "the server enforces it" can never stand down their -// client-side refusal, and no read, so a listing may answer from the cache. -func TestClaimsAndClosesAskForALivePolicyProbe(t *testing.T) { - ctx := context.Background() - for _, tc := range []struct { - name string - live bool - run func(t *testing.T, s *Store) - }{ - {"ready listing", false, func(t *testing.T, s *Store) { - if _, err := s.GetReadyWork(ctx, types.WorkFilter{}); err != nil { - t.Fatal(err) - } - }}, - {"lifecycle close", true, func(t *testing.T, s *Store) { - lc, err := s.IssueLifecycle() - if err != nil { - t.Fatal(err) - } - if _, err := lc.Close(ctx, publicops.CloseRequest{IssueID: "a"}); err != nil { - t.Fatal(err) - } - }}, - {"lifecycle claim", true, func(t *testing.T, s *Store) { - lc, err := s.IssueLifecycle() - if err != nil { - t.Fatal(err) - } - if _, err := lc.Update(ctx, publicops.UpdateRequest{IssueID: "a", Claim: true}); err != nil { - t.Fatal(err) - } - }}, - {"claim ready", true, func(t *testing.T, s *Store) { - if _, err := s.ClaimReadyIssue(ctx, types.WorkFilter{}, "me"); err != nil { - t.Fatal(err) - } - }}, - {"batch close", true, func(t *testing.T, s *Store) { - closer, err := s.BatchCloser() - if err != nil { - t.Fatal(err) - } - if _, err := closer.CloseBatch(ctx, publicops.CloseBatchRequest{Actor: "me", Items: []publicops.BatchCloseItem{{IssueID: "a"}}}); err != nil { - t.Fatal(err) - } - }}, - } { - t.Run(tc.name, func(t *testing.T) { - raw := &fakeStore{lifecycle: &fakeLifecycle{}, batch: &fakeBatchCloser{}} - tc.run(t, testStore(raw, &fakeStore{}, false)) - if len(raw.liveProbes) == 0 { - t.Fatal("the policy was never probed") - } - for i, live := range raw.liveProbes { - if live != tc.live { - t.Errorf("probe %d: live = %v, want %v", i, live, tc.live) - } - } - }) - } - if storage.LivePolicyProbe(ctx) { - t.Error("a plain context reads as live") - } -} - -// TestRemoteClaimsAndClosesAskForALivePolicyProbe is the same pin over a -// remote store's role views (remote_roles.go): direct claim, claim-next and -// batch close probe live; the reader's ready listing does not. -func TestRemoteClaimsAndClosesAskForALivePolicyProbe(t *testing.T) { - for _, tc := range []struct { - name string - live bool - run func(t *testing.T, s *Store) - }{ - {"ready", false, func(t *testing.T, s *Store) { - r, err := s.IssueReader() - if err != nil { - t.Fatal(err) - } - if _, err := r.Ready(t.Context(), publicops.ReadyRequest{}); err != nil { - t.Fatal(err) - } - }}, - {"claim", true, func(t *testing.T, s *Store) { - c, err := s.IssueClaimer() - if err != nil { - t.Fatal(err) - } - if _, err := c.Claim(t.Context(), publicops.ClaimRequest{Actor: "a", IssueID: "be-a"}); err != nil { - t.Fatal(err) - } - }}, - {"claim next", true, func(t *testing.T, s *Store) { - c, err := s.ReadyClaimer() - if err != nil { - t.Fatal(err) - } - if _, err := c.ClaimNext(t.Context(), publicops.ClaimNextRequest{Actor: "a"}); err != nil { - t.Fatal(err) - } - }}, - {"batch close", true, func(t *testing.T, s *Store) { - c, err := s.BatchCloser() - if err != nil { - t.Fatal(err) - } - if _, err := c.CloseBatch(t.Context(), publicops.CloseBatchRequest{Actor: "a", Items: []publicops.BatchCloseItem{{IssueID: "be-a"}}}); err != nil { - t.Fatal(err) - } - }}, - } { - t.Run(tc.name, func(t *testing.T) { - base, _ := newRemotePolicyStore(true) - raw := &passthroughFake{remoteFake: base.inner.(*remoteFake), claimer: &recordingClaimer{}} - store := New(raw, func(ProjectName) (string, bool) { return "", false }, nil) - store.warnProject = nil - tc.run(t, store) - probes := raw.liveProbes - if len(probes) == 0 { - t.Fatal("the policy was never probed") - } - for i, live := range probes { - if live != tc.live { - t.Errorf("probe %d: live = %v, want %v", i, live, tc.live) - } - } - }) - } -} diff --git a/internal/storage/externaldeps/ready_wake_test.go b/internal/storage/externaldeps/ready_wake_test.go deleted file mode 100644 index fc280f47b..000000000 --- a/internal/storage/externaldeps/ready_wake_test.go +++ /dev/null @@ -1,67 +0,0 @@ -package externaldeps - -import ( - "context" - "testing" - - "github.com/steveyegge/beads/internal/storage" - "github.com/steveyegge/beads/internal/types" - publicops "github.com/steveyegge/beads/issueops" -) - -type wakeStore struct { - *fakeStore - wake func() -} - -func (s *wakeStore) WakeExpiredDefersAdvisory(context.Context) { s.wake() } - -func (s *wakeStore) SearchIssuesWithCounts(ctx context.Context, _ string, filter types.IssueFilter) ([]*types.IssueWithCounts, error) { - rows := make([]*types.IssueWithCounts, 0, len(filter.IDs)) - for _, id := range filter.IDs { - row, err := s.GetIssue(ctx, id) - if err != nil { - return nil, err - } - rows = append(rows, &types.IssueWithCounts{Issue: row}) - } - return rows, nil -} - -func TestReadyClaimerWakesBeforeExternalFilteredSelection(t *testing.T) { - for _, sweepSucceeds := range []bool{true, false} { - name := "wake succeeds" - if !sweepSucceeds { - name = "failed advisory wake still allows claim" - } - t.Run(name, func(t *testing.T) { - blocked, eligible := issue("be-blocked"), issue("be-eligible") - raw := &fakeStore{deps: map[string][]*types.Dependency{ - blocked.ID: {externalDep(blocked.ID, "external:remote:payments", types.DepBlocks)}, - }} - if !sweepSucceeds { - raw.ready = []*types.Issue{blocked, eligible} - } - wakes := 0 - backend := &wakeStore{fakeStore: raw, wake: func() { - wakes++ - if sweepSucceeds { - raw.ready = []*types.Issue{blocked, eligible} - } - }} - // An intervening decorator must not hide the wake capability. - inner := storage.NewHookFiringStore(backend, nil) - claimer, err := New(inner, nil, nil).ReadyClaimer() - if err != nil { - t.Fatal(err) - } - got, err := claimer.ClaimNext(t.Context(), publicops.ClaimNextRequest{Actor: "worker"}) - if err != nil { - t.Fatal(err) - } - if wakes != 1 || got.Claimed == nil || got.Claimed.ID != eligible.ID { - t.Fatalf("wakes=%d, claimed=%v; want one wake and %s", wakes, got.Claimed, eligible.ID) - } - }) - } -} diff --git a/internal/storage/externaldeps/remote_roles.go b/internal/storage/externaldeps/remote_roles.go deleted file mode 100644 index d2c049b76..000000000 --- a/internal/storage/externaldeps/remote_roles.go +++ /dev/null @@ -1,636 +0,0 @@ -package externaldeps - -import ( - "context" - "errors" - "fmt" - - "github.com/steveyegge/beads/internal/storage" - storageissueops "github.com/steveyegge/beads/internal/storage/issueops" - "github.com/steveyegge/beads/internal/types" - "github.com/steveyegge/beads/internal/workapi" - "github.com/steveyegge/beads/internal/workapi/storereader" - "github.com/steveyegge/beads/issueops" -) - -// THE REMOTE COMPOSITION. -// -// A remote backend (storage.RemoteBackendStore: today the HTTP client of a -// `bd serve`) serves its ROLES natively and only a slice of the legacy -// storage.DoltStorage method seam. The local role views in role_accessors.go -// are built from that seam — storereader over this decorator, IsBlocked, -// GetDependencyTree, ClaimReadyIssue — so over a remote store they reached the -// generated ErrUnsupported stubs: `bd list` and `bd children` refused -// (SearchIssuesWithCounts), `bd show --json` hydrated its counts from stubbed -// reads and printed zeros, and the default `bd dep tree` refused -// (GetDependencyTree). -// -// So for a remote store each role view is the INNER store's own role, passed -// through, with this decorator's external-dependency policy laid over it in -// role terms. The policy itself is unchanged and is never skipped merely -// because the store is remote (design 3.6): -// -// - a server that advertises wire.CapExternalDependencies (every bd serve -// that composes the policy, which is every bd serve) has applied it -// already, so every view below first asks serverEnforcesPolicy and then -// hands the request to the inner role UNTOUCHED: no client validation, no -// edge reads, and claim-next stays the server's atomic operation; -// - only against a server that does not advertise it (an older bd serve, or -// another implementation) does the policy run here, client-side. Ready reads and the -// ready claim exclude externally blocked sources, which needs the -// workspace-wide blocker set (loadBlockingState). Every per-issue question -// — may this issue be claimed or closed, what blocks these listed rows, -// which external leaves hang off this tree — reads only the edges of the -// issues it names, through the inner EdgeReader, instead of walking every -// edge in the workspace. - -// maxRemoteEdgeAnchors is how many anchors one EdgeReader read names. It is -// the server's own per-request anchor bound (internal/httpapi's -// maxDependencyAnchors); the http client refuses past it rather than chunking. -const maxRemoteEdgeAnchors = 100 - -// rolesAreRemote reports whether the inner store is a remote backend whose -// roles, not its legacy method seam, are the surface to compose over. -func (s *Store) rolesAreRemote() bool { - remote, ok := storage.UnwrapStore(s.inner).(storage.RemoteBackendStore) - return ok && remote.IsRemoteBackendStore() -} - -// externalBlockersFor answers, for exactly the named issues, which unsatisfied -// external references block each of them. An issue with none is absent from -// the map. -// -// A remote store reads only those issues' edges (one EdgeReader request per -// maxRemoteEdgeAnchors ids); a local store keeps the indexed workspace-wide -// read loadBlockingState already makes, which is cheaper there than a -// per-issue one. -func (s *Store) externalBlockersFor(ctx context.Context, ids []string) (map[string][]string, error) { - if len(ids) == 0 { - return nil, nil - } - if !s.rolesAreRemote() { - state, err := s.loadBlockingState(ctx) - if err != nil { - return nil, err - } - out := make(map[string][]string, len(ids)) - for _, id := range ids { - if refs := state.refsByIssue[id]; len(refs) > 0 { - out[id] = refs - } - } - return out, nil - } - enforced, err := s.serverEnforcesPolicy(ctx) - if err != nil || enforced { - return nil, err - } - deps, err := s.remoteEdges(ctx, ids) - if err != nil { - return nil, fmt.Errorf("external dependencies: read blocking edges: %w", err) - } - state, err := s.blockingStateFromRecords(ctx, deps) - if err != nil { - return nil, err - } - return state.refsByIssue, nil -} - -// remoteEdges reads the stored outgoing edges of ids through the inner -// EdgeReader, keyed by source id. Each id is named once, and an empty one not -// at all: the http EdgeReader refuses an empty anchor, which would answer a -// batch close's own validation question with an edge-read error. -func (s *Store) remoteEdges(ctx context.Context, ids []string) (map[string][]*types.Dependency, error) { - anchors := make([]string, 0, len(ids)) - named := make(map[string]bool, len(ids)) - for _, id := range ids { - if id != "" && !named[id] { - named[id] = true - anchors = append(anchors, id) - } - } - deps := make(map[string][]*types.Dependency, len(anchors)) - if len(anchors) == 0 { - return deps, nil - } - reader, err := s.inner.EdgeReader() - if err != nil { - return nil, err - } - for start := 0; start < len(anchors); start += maxRemoteEdgeAnchors { - end := min(start+maxRemoteEdgeAnchors, len(anchors)) - res, err := reader.ReadEdges(ctx, issueops.EdgeReadRequest{IDs: anchors[start:end]}) - if err != nil { - return nil, err - } - for _, anchor := range res.Anchors { - if len(anchor.Edges) > 0 { - deps[anchor.ID] = append(deps[anchor.ID], anchor.Edges...) - } - } - } - return deps, nil -} - -// externallyBlockedIDs is the workspace-wide set of issues an unsatisfied -// external reference blocks — what a ready read has to exclude. Empty when the -// server already enforces the policy (loadBlockingState's own probe). -func (s *Store) externallyBlockedIDs(ctx context.Context) (map[string]bool, error) { - state, err := s.loadBlockingState(ctx) - if err != nil { - return nil, err - } - if len(state.refsByIssue) == 0 { - return nil, nil - } - blocked := make(map[string]bool, len(state.refsByIssue)) - for id := range state.refsByIssue { - blocked[id] = true - } - return blocked, nil -} - -// ── Reader ────────────────────────────────────────────────────────── - -type remoteReader struct { - inner issueops.Reader - policy *Store -} - -// Get is the inner detail read: the policy decides no detail field, and the -// server's view carries the dependency and comment counts the legacy seam -// could not hydrate. -func (r *remoteReader) Get(ctx context.Context, req issueops.GetRequest) (*issueops.IssueDetails, error) { - return r.inner.Get(ctx, req) -} - -// List passes an ordinary listing straight through. A ready listing -// (ReadyFlag, `bd list --ready`) is the ready question, which a remote -// listing cannot carry (httpclient ledger row E-ListRequest.ReadyFlag: over -// the wire it is listReadyWork, a different operation). The policy store's -// own reader answers it through the ready reads, which the remote store -// serves and the policy filters, so it is the same answer the CLI gets -// locally; against a server that applies the policy those reads stand down. -func (r *remoteReader) List(ctx context.Context, req issueops.ListRequest) (issueops.IssuePage, error) { - if !req.ReadyFlag { - return r.inner.List(ctx, req) - } - ready, err := storereader.New(r.policy) - if err != nil { - return issueops.IssuePage{}, err - } - return ready.List(ctx, req) -} - -// Ready excludes externally blocked sources. -// -// No remote ready read can carry an id exclusion (L12), so the exclusion is -// applied to a widened window instead: from offset 0, Offset+Limit plus one -// row per blocked id. At most that many rows of the window can be dropped, -// so the caller's page is exact whenever enough unblocked rows exist. -func (r *remoteReader) Ready(ctx context.Context, req issueops.ReadyRequest) (issueops.IssuePage, error) { - blocked, err := r.policy.externallyBlockedIDs(ctx) - if err != nil { - return issueops.IssuePage{}, err - } - if len(blocked) == 0 { - return r.inner.Ready(ctx, req) - } - limit := workapi.LimitOr(req.Limit, workapi.DefaultReadyLimit) - widened := req - widened.Offset = 0 - widened.Limit = widenedLimit(req.Offset, limit, len(blocked)) - page, err := r.inner.Ready(ctx, widened) - if err != nil { - return issueops.IssuePage{}, err - } - return excludeAndPage(page, blocked, req.Offset, limit), nil -} - -// widenedLimit is the window a client-side exclusion reads: 0 (unlimited) -// stays unlimited. -func widenedLimit(offset, limit, blocked int) *int { - n := 0 - if limit > 0 { - n = offset + limit + blocked - } - return &n -} - -// excludeAndPage drops the blocked rows from a widened window, then cuts the -// caller's page out of what remains. HasMore is true when unblocked rows are -// left past the page, or when the window itself was cut short by the server — -// beyond it the rows are unread, so "no more" cannot be claimed. -func excludeAndPage(page issueops.IssuePage, blocked map[string]bool, offset, limit int) issueops.IssuePage { - kept := make([]*issueops.IssueWithCounts, 0, len(page.Items)) - for _, row := range page.Items { - if row != nil && row.Issue != nil && blocked[row.ID] { - continue - } - kept = append(kept, row) - } - if offset >= len(kept) { - return issueops.IssuePage{Items: []*issueops.IssueWithCounts{}, HasMore: page.HasMore} - } - kept = kept[offset:] - hasMore := page.HasMore - if limit > 0 && len(kept) > limit { - kept = kept[:limit] - hasMore = true - } - return issueops.IssuePage{Items: kept, HasMore: hasMore} -} - -// ── Claimer ───────────────────────────────────────────────────────── - -type remoteClaimer struct { - inner issueops.Claimer - policy *Store -} - -// Claim refuses a direct claim of externally blocked work before the server's -// atomic claim. Local blocking edges are the server's claim to judge. Against -// a server that applies the policy, externalBlockersFor reads nothing and the -// claim goes through untouched. -func (c *remoteClaimer) Claim(ctx context.Context, req issueops.ClaimRequest) (issueops.ClaimResult, error) { - ctx = storage.WithLivePolicyProbe(ctx) - blockers, err := c.policy.externalBlockersFor(ctx, []string{req.IssueID}) - if err != nil { - return issueops.ClaimResult{}, err - } - if refs := blockers[req.IssueID]; len(refs) > 0 { - return issueops.ClaimResult{}, fmt.Errorf("%w: %s is blocked by %v", storage.ErrCloseBlocked, req.IssueID, refs) - } - return c.inner.Claim(ctx, req) -} - -// ── ReadyClaimer ──────────────────────────────────────────────────── - -// remoteClaimPage is how many externally unblocked candidates one pass of -// remoteReadyClaimer reads, matching httpclient's composed ready claim. -const remoteClaimPage = 25 - -type remoteReadyClaimer struct { - inner issueops.ReadyClaimer - policy *Store -} - -// ClaimNext is the server's atomic claim-next, untouched, against a server -// that applies the policy itself, and against any server while nothing is -// externally blocked. Only an older server with externally blocked work -// cannot be told what to skip, so there the choice is made here, among the -// candidates a local claim-next selects (ClaimReadyIssueInTx): read the open, -// unassigned ready rows in the requested order, skip the blocked ones, and -// claim each remaining candidate by id until one is won. A candidate another -// actor took in between is a lost race and the next one is tried. A full -// window lost end to end is read once more: claimed rows leave the unassigned -// set, so the second pass reads the candidates behind them. Only a claim that -// tried candidates and won none is an error. -func (c *remoteReadyClaimer) ClaimNext(ctx context.Context, req issueops.ClaimNextRequest) (issueops.ClaimNextResult, error) { - ctx = storage.WithLivePolicyProbe(ctx) - // externallyBlockedIDs is empty against a server that applies the policy - // (loadBlockingState's probe), so that server's atomic claim answers. - blocked, err := c.policy.externallyBlockedIDs(ctx) - if err != nil { - return issueops.ClaimNextResult{}, err - } - if len(blocked) == 0 { - return c.inner.ClaimNext(ctx, req) - } - if err := storageissueops.ValidateClaimNextRequest(req); err != nil { - return issueops.ClaimNextResult{}, err - } - reader, err := c.policy.inner.IssueReader() - if err != nil { - return issueops.ClaimNextResult{}, err - } - claimer, err := c.policy.inner.IssueClaimer() - if err != nil { - return issueops.ClaimNextResult{}, err - } - // Unassigned whatever assignee the request names, as locally: a row - // another actor holds is never a candidate. Each window holds - // remoteClaimPage candidates past the blocked rows it may also hold. - filter := req.Filter - filter.Unassigned = true - filter.Assignee = "" - filter.Offset = 0 - window := remoteClaimPage + len(blocked) - filter.Limit = &window - lost := 0 - for range 2 { - page, err := reader.Ready(ctx, filter) - if err != nil { - return issueops.ClaimNextResult{}, err - } - claimed, tried, err := claimFirstCandidate(ctx, claimer, req.Actor, page.Items, blocked) - if err != nil { - return issueops.ClaimNextResult{}, err - } - if claimed != nil { - return issueops.ClaimNextResult{Claimed: claimed}, nil - } - if tried == 0 { - // Nothing unblocked is left to take, whatever an earlier pass lost. - return issueops.ClaimNextResult{}, nil - } - lost += tried - if len(page.Items) < window { - // A short window is the whole front: reading it again would only - // re-read the rows that just refused. - break - } - } - // There was work, and every candidate went to another actor first: an - // empty answer would say there is no work. - return issueops.ClaimNextResult{}, fmt.Errorf("claim ready: lost %d races for externally unblocked ready work; re-run to take the next one", lost) -} - -// claimFirstCandidate claims, by id and in order, the first row that is not -// externally blocked and that no other actor takes first. tried counts the -// candidates it attempted, so with nothing claimed it is the races lost. -func claimFirstCandidate(ctx context.Context, claimer issueops.Claimer, actor string, rows []*issueops.IssueWithCounts, blocked map[string]bool) (*issueops.IssueWithCounts, int, error) { - tried := 0 - for _, row := range rows { - if row == nil || row.Issue == nil || blocked[row.ID] { - continue - } - tried++ - res, err := claimer.Claim(ctx, issueops.ClaimRequest{Actor: actor, IssueID: row.ID}) - if isLostClaimRace(err) { - continue - } - if err != nil { - return nil, tried, err - } - claimed := *row - if res.Issue != nil { - claimed.Issue = res.Issue - } - return &claimed, tried, nil - } - return nil, tried, nil -} - -// isLostClaimRace classifies the refusals that mean someone else took a -// candidate first. ErrNotFound joins them, as in httpclient's own composed -// claim: a ready row deleted before it was claimed is the same situation for -// a caller that wants the next piece of work. -func isLostClaimRace(err error) bool { - return errors.Is(err, issueops.ErrAlreadyClaimed) || - errors.Is(err, issueops.ErrNotClaimable) || - errors.Is(err, issueops.ErrNotFound) -} - -// ── ReadyCounter ──────────────────────────────────────────────────── - -type remoteReadyCounter struct { - inner issueops.ReadyCounter - fallback issueops.ReadyCounter - policy *Store -} - -// CountReady is the server's count when the server applies the policy. Against -// any other server it is the decorator's externally filtered count. -func (c *remoteReadyCounter) CountReady(ctx context.Context, req issueops.ReadyRequest) (issueops.ReadyCountResult, error) { - enforced, err := c.policy.serverEnforcesPolicy(ctx) - if err != nil { - return issueops.ReadyCountResult{}, err - } - if enforced { - return c.inner.CountReady(ctx, req) - } - return c.fallback.CountReady(ctx, req) -} - -// ── BlockingAnnotator ─────────────────────────────────────────────── - -type remoteBlockingAnnotator struct { - inner issueops.BlockingAnnotator - policy *Store -} - -// AnnotateBlocking adds each listed row's unsatisfied external blockers to the -// server's derived answer, reading the edges of those rows only. A server that -// applies the policy has already added them, and its answer is returned as is. -func (a *remoteBlockingAnnotator) AnnotateBlocking(ctx context.Context, req issueops.BlockingRequest) (issueops.BlockingResult, error) { - result, err := a.inner.AnnotateBlocking(ctx, req) - if err != nil { - return issueops.BlockingResult{}, err - } - if enforced, err := a.policy.serverEnforcesPolicy(ctx); err != nil || enforced { - return result, err - } - ids := make([]string, 0, len(result.Items)) - for _, item := range result.Items { - if item.ID != "" { - ids = append(ids, item.ID) - } - } - blockers, err := a.policy.externalBlockersFor(ctx, ids) - if err != nil { - return issueops.BlockingResult{}, err - } - for i := range result.Items { - for _, ref := range blockers[result.Items[i].ID] { - result.Items[i].BlockedBy = appendUnique(result.Items[i].BlockedBy, ref) - } - if req.IncludeIsBlocked && len(blockers[result.Items[i].ID]) > 0 { - result.Items[i].IsBlocked = boolPtr(true) - } - } - return result, nil -} - -// remoteExternalBlocked adds the external-dependency invariant to a remote -// store's IsBlockedBatch answer: an id with an unsatisfied external blocker -// is blocked. A server that enforces the policy already folded it in. -func (s *Store) remoteExternalBlocked(ctx context.Context, ids []string, blocked map[string]bool) (map[string]bool, error) { - if enforced, err := s.serverEnforcesPolicy(ctx); err != nil || enforced { - if err != nil { - return nil, err - } - return blocked, nil - } - external, err := s.externalBlockersFor(ctx, ids) - if err != nil { - return nil, err - } - for id, refs := range external { - if len(refs) > 0 { - blocked[id] = true - } - } - return blocked, nil -} - -// ── TreeWalker ────────────────────────────────────────────────────── - -type remoteTreeWalker struct { - inner issueops.TreeWalker - policy *Store -} - -// WalkTree is the server's walk. For the plain down-tree request it also hangs -// the synthetic external leaves the local walker shows — unless the server -// enforces the external-dependency policy, whose own walker already does. -// -// The probe only spares the edge reads: a leaf is display, not enforcement, -// and appendTreeExternalReferences skips an id the tree already holds. So a -// probe that fails renders the leaves here rather than failing a walk the -// server has already answered. -func (t *remoteTreeWalker) WalkTree(ctx context.Context, req issueops.WalkTreeRequest) (issueops.TreeResult, error) { - result, err := t.inner.WalkTree(ctx, req) - if err != nil || len(result.Nodes) == 0 { - return result, err - } - if (req.Direction != "" && req.Direction != issueops.TreeDown) || req.Status != "" || req.MaxRows != 0 { - return result, nil - } - if enforced, err := t.policy.serverEnforcesPolicy(ctx); err == nil && enforced { - return result, nil - } - ids := make([]string, 0, len(result.Nodes)) - for _, node := range result.Nodes { - if node != nil && !isExternalReference(node.ID) { - ids = append(ids, node.ID) - } - } - deps, err := t.policy.remoteEdges(ctx, ids) - if err != nil { - return issueops.TreeResult{}, fmt.Errorf("external dependencies: load tree edges: %w", err) - } - nodes, err := t.policy.appendTreeExternalReferences(ctx, result.Nodes, deps, req.MaxDepth, false) - if err != nil { - return issueops.TreeResult{}, err - } - return issueops.TreeResult{Nodes: nodes}, nil -} - -// ── BatchCloser ───────────────────────────────────────────────────── - -type remoteBatchCloser struct { - inner issueops.BatchCloser - policy *Store -} - -// CloseBatch applies the close policy around the server's batch close. A -// remote store has no policy-snapshot batch closer -// (storage.PolicyBatchCloserSource), so an unforced batch that names an -// externally blocked live issue is split: that item gets the policy refusal as -// its outcome — exactly the outcome the local batch body records — and the -// rest go to the server in one request. A blocked item that is already closed -// or missing still travels, so its idempotent re-close and the server's -// not-found precedence are the server's answer, as they are locally; one whose -// state cannot be read gets that read's error (liveRefusal). -// -// A forced batch has nothing to judge. A ClaimNext batch goes to -// closeAndClaimNext, Force or not. Against a server that applies the policy, -// externalBlockersFor reads nothing and the whole request goes through -// untouched. -func (c *remoteBatchCloser) CloseBatch(ctx context.Context, request issueops.CloseBatchRequest) (issueops.CloseBatchResult, error) { - ctx = storage.WithLivePolicyProbe(ctx) - if request.ClaimNext != nil { - return c.closeAndClaimNext(ctx, request) - } - if request.Force { - return c.inner.CloseBatch(ctx, request) - } - ids := make([]string, 0, len(request.Items)) - for _, item := range request.Items { - ids = append(ids, item.IssueID) - } - blockers, err := c.policy.externalBlockersFor(ctx, ids) - if err != nil { - return issueops.CloseBatchResult{}, err - } - if len(blockers) == 0 { - return c.inner.CloseBatch(ctx, request) - } - if err := storageissueops.ValidateCloseBatchRequest(request); err != nil { - return issueops.CloseBatchResult{}, err - } - - outcomes := make([]issueops.CloseOutcome, len(request.Items)) - forward := request - forward.Items = nil - forwarded := make([]int, 0, len(request.Items)) - policy := storage.NewBatchClosePolicy(blockers) - for i, item := range request.Items { - if refusal := policy.CheckClose(item.IssueID, false); refusal != nil { - if err := c.liveRefusal(ctx, item.IssueID, refusal); err != nil { - outcomes[i] = issueops.CloseOutcome{IssueID: item.IssueID, Err: err} - continue - } - } - forward.Items = append(forward.Items, item) - forwarded = append(forwarded, i) - } - if len(forward.Items) > 0 { - res, err := c.inner.CloseBatch(ctx, forward) - if err != nil { - return issueops.CloseBatchResult{}, err - } - if len(res.Outcomes) != len(forwarded) { - return issueops.CloseBatchResult{}, fmt.Errorf("close batch: server answered %d outcomes for %d items", len(res.Outcomes), len(forwarded)) - } - for j, i := range forwarded { - outcomes[i] = res.Outcomes[j] - } - } - return issueops.CloseBatchResult{Outcomes: outcomes}, nil -} - -// liveRefusal is the outcome of an externally blocked item, or nil when the -// item travels to the server. As in the local close only a live issue is -// refused, so an already closed one keeps its idempotent re-close and a -// missing one the server's not-found. Any other failed read is the item's -// outcome: it is neither refused nor closed on a guess. -func (c *remoteBatchCloser) liveRefusal(ctx context.Context, issueID string, refusal error) error { - issue, err := c.policy.inner.GetIssue(ctx, issueID) - switch { - case errors.Is(err, storage.ErrNotFound): - return nil - case err != nil: - return err - case issue != nil && issue.Status != types.StatusClosed: - return refusal - } - return nil -} - -var ( - _ issueops.BatchCloser = (*remoteBatchCloser)(nil) - _ issueops.Reader = (*remoteReader)(nil) - _ issueops.Claimer = (*remoteClaimer)(nil) - _ issueops.ReadyClaimer = (*remoteReadyClaimer)(nil) - _ issueops.BlockingAnnotator = (*remoteBlockingAnnotator)(nil) - _ issueops.TreeWalker = (*remoteTreeWalker)(nil) - _ issueops.ReadyCounter = (*remoteReadyCounter)(nil) -) - -// closeAndClaimNext answers a batch with a next claim. A request that is -// invalid on every backend is still ErrValidation. Against a server that does -// not apply the policy the batch is refused whole, before anything is read or -// closed: a served close cannot keep externally blocked work out of its claim, -// and the v0 wire's batch close cannot carry a next claim at all (ledger row -// W-CloseBatchRequest.ClaimNext). A server that applies the policy gets the -// request as it came; over the v0 wire its closer refuses the claim too. -func (c *remoteBatchCloser) closeAndClaimNext(ctx context.Context, request issueops.CloseBatchRequest) (issueops.CloseBatchResult, error) { - if err := storageissueops.ValidateCloseBatchRequest(request); err != nil { - return issueops.CloseBatchResult{}, err - } - if _, err := workapi.BuildReadyFilter(*request.ClaimNext); err != nil { - return issueops.CloseBatchResult{}, err - } - enforced, err := c.policy.serverEnforcesPolicy(ctx) - if err != nil { - return issueops.CloseBatchResult{}, err - } - if !enforced { - return issueops.CloseBatchResult{}, fmt.Errorf( - "close with a next claim: the served batch close cannot carry a next claim, so nothing was closed; close without --claim-next, then run `bd ready --claim` (%w)", - &storage.ErrUnsupported{Op: "CloseBatchRequest.ClaimNext", Backend: fmt.Sprintf("%T", storage.UnwrapStore(c.policy.inner))}) - } - return c.inner.CloseBatch(ctx, request) -} diff --git a/internal/storage/externaldeps/remote_roles_composition_test.go b/internal/storage/externaldeps/remote_roles_composition_test.go deleted file mode 100644 index 308124e9b..000000000 --- a/internal/storage/externaldeps/remote_roles_composition_test.go +++ /dev/null @@ -1,908 +0,0 @@ -package externaldeps - -import ( - "context" - "errors" - "fmt" - "slices" - "strings" - "testing" - - "github.com/steveyegge/beads/internal/storage" - "github.com/steveyegge/beads/internal/types" - publicops "github.com/steveyegge/beads/issueops" -) - -// remoteStore models httpclient.Store for remote_roles.go. Each role it hands -// out is served over the embedded fakeStore's rows, the way a server that -// knows nothing of the policy answers it, and each legacy method a base -// accessor builds on is refused, so a composition that reaches one fails the -// test instead of passing on a fake that answers it. Like httpclient.Store it -// cannot express ExcludeIDs and has no BatchCloserWithPolicy. -type remoteStore struct { - *fakeStore - // served logs each served role call, in order. - served []string - // closes logs the item ids of each served batch close. - closes [][]string - // edgeReads logs how many anchors each served edge read named. - edgeReads []int - // scans counts the whole-workspace dependency scans read through it. - scans int - // localBlockers is the served annotation's own BlockedBy, per id. - localBlockers map[string][]string - // claimErr, when set, answers every served Claim. - claimErr error - // takenFirst names the rows a rival actor claims just before a served - // Claim of them lands: that claim is a lost race, and the row leaves the - // unassigned set. - takenFirst map[string]bool - // getErrs, per id, fails the GetIssue read of that id. - getErrs map[string]error -} - -var errLegacyRefused = errors.New("remoteStore: legacy method refused") - -// wireEdgeAnchorCap is the most anchors the wire's listDependencies accepts. -const wireEdgeAnchorCap = 100 - -func newRemoteStore(ready ...*types.Issue) *remoteStore { - return &remoteStore{fakeStore: &fakeStore{ - ready: ready, - deps: make(map[string][]*types.Dependency), - excludeIDsUnsupported: true, - }} -} - -func (r *remoteStore) IsRemoteBackendStore() bool { return true } - -func (r *remoteStore) GetIssue(ctx context.Context, id string) (*types.Issue, error) { - if err := r.getErrs[id]; err != nil { - return nil, err - } - return r.fakeStore.GetIssue(ctx, id) -} - -// httpclient.Store serves the vocabulary a listing loads before its search. -func (r *remoteStore) GetCustomStatusesDetailed(context.Context) ([]types.CustomStatus, error) { - return nil, nil -} -func (r *remoteStore) GetCustomTypes(context.Context) ([]string, error) { return nil, nil } -func (r *remoteStore) GetInfraTypes(context.Context) map[string]bool { return nil } - -func (r *remoteStore) SearchIssuesWithCounts(context.Context, string, types.IssueFilter) ([]*types.IssueWithCounts, error) { - return nil, errLegacyRefused -} - -func (r *remoteStore) IsBlocked(context.Context, string) (bool, []string, error) { - return false, nil, errLegacyRefused -} - -func (r *remoteStore) GetDependencyRecordsForIssues(context.Context, []string) (map[string][]*types.Dependency, error) { - return nil, errLegacyRefused -} - -func (r *remoteStore) GetDependencyTree(context.Context, string, int, bool, bool) ([]*types.TreeNode, error) { - return nil, errLegacyRefused -} - -func (r *remoteStore) ClaimReadyIssue(context.Context, types.WorkFilter, string) (*types.Issue, error) { - return nil, errLegacyRefused -} - -func (r *remoteStore) GetAllDependencyRecords(ctx context.Context) (map[string][]*types.Dependency, error) { - r.scans++ - return r.fakeStore.GetAllDependencyRecords(ctx) -} - -func (r *remoteStore) GetExternalBlockingDependencyRecords(ctx context.Context) (map[string][]*types.Dependency, error) { - r.scans++ - return r.fakeStore.GetExternalBlockingDependencyRecords(ctx) -} - -func (r *remoteStore) IssueReader() (publicops.Reader, error) { return servedReader{r}, nil } -func (r *remoteStore) IssueClaimer() (publicops.Claimer, error) { return servedClaimer{r}, nil } -func (r *remoteStore) ReadyClaimer() (publicops.ReadyClaimer, error) { - return servedReadyClaimer{r}, nil -} -func (r *remoteStore) TreeWalker() (publicops.TreeWalker, error) { return servedWalker{r}, nil } -func (r *remoteStore) EdgeReader() (publicops.EdgeReader, error) { return servedEdgeReader{r}, nil } -func (r *remoteStore) BatchCloser() (publicops.BatchCloser, error) { return servedCloser{r}, nil } - -func (r *remoteStore) BlockingAnnotator() (publicops.BlockingAnnotator, error) { - return servedAnnotator{r}, nil -} - -type servedReader struct{ s *remoteStore } - -// Ready serves the ready rows the way a server that knows nothing of the -// policy does: in order, narrowed by the request's assignee rule (Unassigned -// wins over Assignee, as in workapi.BuildReadyFilter), up to its limit, with -// has_more past it. -func (r servedReader) Ready(_ context.Context, req publicops.ReadyRequest) (publicops.IssuePage, error) { - r.s.served = append(r.s.served, "Ready") - items := make([]*types.IssueWithCounts, 0, len(r.s.ready)) - for _, issue := range r.s.ready { - if req.Unassigned { - if issue.Assignee != "" { - continue - } - } else if req.Assignee != "" && issue.Assignee != req.Assignee { - continue - } - items = append(items, &types.IssueWithCounts{Issue: issue}) - } - if req.Limit != nil && *req.Limit >= 0 && *req.Limit < len(items) { - return publicops.IssuePage{Items: items[:*req.Limit], HasMore: true}, nil - } - return publicops.IssuePage{Items: items}, nil -} - -func (r servedReader) List(_ context.Context, req publicops.ListRequest) (publicops.IssuePage, error) { - if req.ReadyFlag { - // Like httpclient: listIssues cannot carry ReadyFlag (ledger row - // E-ListRequest.ReadyFlag); a ready listing is listReadyWork's question. - return publicops.IssuePage{}, &storage.ErrUnsupported{Op: "ListRequest.ReadyFlag", Backend: "http"} - } - r.s.served = append(r.s.served, "List") - items := make([]*types.IssueWithCounts, 0, len(r.s.ready)) - for _, issue := range r.s.ready { - items = append(items, &types.IssueWithCounts{Issue: issue}) - } - return publicops.IssuePage{Items: items}, nil -} - -func (r servedReader) Get(ctx context.Context, req publicops.GetRequest) (*publicops.IssueDetails, error) { - r.s.served = append(r.s.served, "Get "+req.ID) - issue, err := r.s.GetIssue(ctx, req.ID) - if err != nil { - return nil, err - } - if issue == nil { - return nil, publicops.ErrNotFound - } - return &publicops.IssueDetails{Issue: *issue}, nil -} - -type servedClaimer struct{ s *remoteStore } - -func (c servedClaimer) Claim(ctx context.Context, req publicops.ClaimRequest) (publicops.ClaimResult, error) { - c.s.served = append(c.s.served, "Claim "+req.IssueID) - if c.s.claimErr != nil { - return publicops.ClaimResult{}, c.s.claimErr - } - if c.s.takenFirst[req.IssueID] { - if err := c.s.ClaimIssue(ctx, req.IssueID, "rival"); err != nil { - return publicops.ClaimResult{}, err - } - } - if err := c.s.ClaimIssue(ctx, req.IssueID, req.Actor); err != nil { - return publicops.ClaimResult{}, err - } - issue, err := c.s.GetIssue(ctx, req.IssueID) - return publicops.ClaimResult{Issue: issue, Changed: true}, err -} - -// servedReadyClaimer claims the first unassigned ready row, blind to the -// policy as a claim-next with no exclusion parameter is. -type servedReadyClaimer struct{ s *remoteStore } - -func (c servedReadyClaimer) ClaimNext(ctx context.Context, req publicops.ClaimNextRequest) (publicops.ClaimNextResult, error) { - c.s.served = append(c.s.served, "ClaimNext") - for _, issue := range c.s.ready { - if issue.Assignee != "" { - continue - } - if err := c.s.ClaimIssue(ctx, issue.ID, req.Actor); err != nil { - return publicops.ClaimNextResult{}, err - } - return publicops.ClaimNextResult{Claimed: &types.IssueWithCounts{Issue: issue}}, nil - } - return publicops.ClaimNextResult{}, nil -} - -type servedAnnotator struct{ s *remoteStore } - -func (a servedAnnotator) AnnotateBlocking(_ context.Context, req publicops.BlockingRequest) (publicops.BlockingResult, error) { - a.s.served = append(a.s.served, "AnnotateBlocking") - items := make([]publicops.IssueBlocking, 0, len(req.IDs)) - for _, id := range req.IDs { - items = append(items, publicops.IssueBlocking{ID: id, BlockedBy: slices.Clone(a.s.localBlockers[id])}) - } - return publicops.BlockingResult{Items: items}, nil -} - -type servedWalker struct{ s *remoteStore } - -func (w servedWalker) WalkTree(context.Context, publicops.WalkTreeRequest) (publicops.TreeResult, error) { - w.s.served = append(w.s.served, "WalkTree") - return publicops.TreeResult{Nodes: slices.Clone(w.s.tree)}, nil -} - -type servedEdgeReader struct{ s *remoteStore } - -func (e servedEdgeReader) ReadEdges(_ context.Context, req publicops.EdgeReadRequest) (publicops.EdgeReadResult, error) { - if len(req.IDs) > wireEdgeAnchorCap { - return publicops.EdgeReadResult{}, fmt.Errorf("served ReadEdges: %d anchors, past the wire's %d", len(req.IDs), wireEdgeAnchorCap) - } - if slices.Contains(req.IDs, "") { - // As httpclient's validateAnchors refuses one. - return publicops.EdgeReadResult{}, fmt.Errorf("%w: an anchor id must not be empty", publicops.ErrValidation) - } - e.s.edgeReads = append(e.s.edgeReads, len(req.IDs)) - anchors := make([]publicops.AnchorEdges, 0, len(req.IDs)) - for _, id := range req.IDs { - anchors = append(anchors, publicops.AnchorEdges{ID: id, Edges: e.s.deps[id]}) - } - return publicops.EdgeReadResult{Anchors: anchors}, nil -} - -type servedCloser struct{ s *remoteStore } - -// errServedClaimNextRefused is the served closer's answer to a next claim. -// httpclient refuses one on every server: the v0 wire's batch close has no -// member to carry it (ledger row W-CloseBatchRequest.ClaimNext). -var errServedClaimNextRefused = &storage.ErrUnsupported{Op: "BatchCloser.CloseBatch", Backend: "http"} - -func (c servedCloser) CloseBatch(ctx context.Context, req publicops.CloseBatchRequest) (publicops.CloseBatchResult, error) { - if req.ClaimNext != nil { - c.s.served = append(c.s.served, "CloseBatch ClaimNext") - return publicops.CloseBatchResult{}, errServedClaimNextRefused - } - ids := make([]string, 0, len(req.Items)) - outcomes := make([]publicops.CloseOutcome, 0, len(req.Items)) - for _, item := range req.Items { - ids = append(ids, item.IssueID) - result, err := c.s.CloseIssueChecked(ctx, item.IssueID, req.Actor, storage.CloseIssueOptions{}) - outcomes = append(outcomes, publicops.CloseOutcome{IssueID: item.IssueID, Changed: err == nil && !result.Unchanged, Err: err}) - } - c.s.closes = append(c.s.closes, ids) - return publicops.CloseBatchResult{Outcomes: outcomes}, nil -} - -const paymentsRef = "external:remote:payments" - -func blockOnPayments(raw *remoteStore, ids ...string) { - for _, id := range ids { - raw.deps[id] = []*types.Dependency{externalDep(id, paymentsRef, types.DepBlocks)} - } -} - -func closeItems(ids ...string) []publicops.BatchCloseItem { - items := make([]publicops.BatchCloseItem, 0, len(ids)) - for _, id := range ids { - items = append(items, publicops.BatchCloseItem{IssueID: id}) - } - return items -} - -// TestRemoteRolesAnswerWithoutTheLegacyMethods pins the blocker the base -// accessors had over httpclient.Store: built over legacy methods a remote -// store refuses, they failed bd list, bd show, bd dep tree, bd ready --claim -// and bd close in a workspace where nothing had an external dependency. -func TestRemoteRolesAnswerWithoutTheLegacyMethods(t *testing.T) { - a, b := issue("be-a"), issue("be-b") - raw := newRemoteStore(a, b) - raw.tree = []*types.TreeNode{{Issue: *a}} - store := testStore(raw, &fakeStore{}, true) - ctx := t.Context() - - reader, err := store.IssueReader() - if err != nil { - t.Fatalf("IssueReader: %v", err) - } - if page, err := reader.List(ctx, publicops.ListRequest{}); err != nil || len(page.Items) != 2 { - t.Fatalf("List = %d items, %v; want 2", len(page.Items), err) - } - if details, err := reader.Get(ctx, publicops.GetRequest{ID: a.ID}); err != nil || details.ID != a.ID { - t.Fatalf("Get = %v, %v; want %s", details, err, a.ID) - } - if page, err := reader.Ready(ctx, publicops.ReadyRequest{}); err != nil || len(page.Items) != 2 { - t.Fatalf("Ready = %d items, %v; want 2", len(page.Items), err) - } - - claimer, err := store.IssueClaimer() - if err != nil { - t.Fatalf("IssueClaimer: %v", err) - } - if _, err := claimer.Claim(ctx, publicops.ClaimRequest{Actor: "worker", IssueID: a.ID}); err != nil { - t.Fatalf("Claim: %v", err) - } - readyClaimer, err := store.ReadyClaimer() - if err != nil { - t.Fatalf("ReadyClaimer: %v", err) - } - if next, err := readyClaimer.ClaimNext(ctx, publicops.ClaimNextRequest{Actor: "worker"}); err != nil || next.Claimed == nil || next.Claimed.ID != b.ID { - t.Fatalf("ClaimNext = %+v, %v; want %s", next.Claimed, err, b.ID) - } - - annotator, err := store.BlockingAnnotator() - if err != nil { - t.Fatalf("BlockingAnnotator: %v", err) - } - if result, err := annotator.AnnotateBlocking(ctx, publicops.BlockingRequest{IDs: []string{a.ID, b.ID}}); err != nil || len(result.Items) != 2 { - t.Fatalf("AnnotateBlocking = %+v, %v; want 2 items", result.Items, err) - } - walker, err := store.TreeWalker() - if err != nil { - t.Fatalf("TreeWalker: %v", err) - } - if tree, err := walker.WalkTree(ctx, publicops.WalkTreeRequest{RootID: a.ID, MaxDepth: 50}); err != nil || len(tree.Nodes) != 1 { - t.Fatalf("WalkTree = %d nodes, %v; want the served root alone", len(tree.Nodes), err) - } - - closer, err := store.BatchCloser() - if err != nil { - t.Fatalf("BatchCloser: %v", err) - } - result, err := closer.CloseBatch(ctx, publicops.CloseBatchRequest{Actor: "worker", Items: closeItems(a.ID, b.ID)}) - if err != nil || len(result.Outcomes) != 2 { - t.Fatalf("CloseBatch = %+v, %v; want 2 outcomes", result.Outcomes, err) - } - - if want := []string{"List", "Get be-a", "Ready", "Claim be-a", "ClaimNext", "AnnotateBlocking", "WalkTree"}; !slices.Equal(raw.served, want) { - t.Fatalf("served calls = %v, want %v", raw.served, want) - } - if want := [][]string{{a.ID, b.ID}}; !slices.EqualFunc(raw.closes, want, slices.Equal[[]string]) { - t.Fatalf("served closes = %v, want %v", raw.closes, want) - } -} - -// TestRemoteReaderPolicesReadyWorkOnly: an externally blocked issue leaves the -// ready listing and stays in a plain one, which claims nothing about -// readiness. -func TestRemoteReaderPolicesReadyWorkOnly(t *testing.T) { - a, b := issue("be-a"), issue("be-b") - raw := newRemoteStore(a, b) - blockOnPayments(raw, a.ID) - reader, err := testStore(raw, &fakeStore{}, true).IssueReader() - if err != nil { - t.Fatalf("IssueReader: %v", err) - } - - ready, err := reader.Ready(t.Context(), publicops.ReadyRequest{}) - if err != nil { - t.Fatalf("Ready: %v", err) - } - if len(ready.Items) != 1 || ready.Items[0].ID != b.ID { - t.Fatalf("Ready = %d items, want %s alone (%s is externally blocked)", len(ready.Items), b.ID, a.ID) - } - listed, err := reader.List(t.Context(), publicops.ListRequest{}) - if err != nil { - t.Fatalf("List: %v", err) - } - if len(listed.Items) != 2 { - t.Fatalf("List = %d items, want both", len(listed.Items)) - } -} - -// TestRemoteReaderAnswersAReadyListingWithoutTheWireFlag: `bd list --ready` -// is a List with ReadyFlag, which the served listing refuses. Over a remote -// store it is the ready question, answered by the policy store's own reader -// with externally blocked work left out, and never sent to the served List. -func TestRemoteReaderAnswersAReadyListingWithoutTheWireFlag(t *testing.T) { - a, b := issue("be-a"), issue("be-b") - for _, tc := range []struct { - name string - blocked bool - want []string - }{ - {name: "no external refs", want: []string{a.ID, b.ID}}, - {name: "an externally blocked issue", blocked: true, want: []string{b.ID}}, - } { - t.Run(tc.name, func(t *testing.T) { - raw := newRemoteStore(a, b) - if tc.blocked { - blockOnPayments(raw, a.ID) - } - reader, err := testStore(raw, &fakeStore{}, true).IssueReader() - if err != nil { - t.Fatalf("IssueReader: %v", err) - } - listed, err := reader.List(t.Context(), publicops.ListRequest{ReadyFlag: true}) - if err != nil { - t.Fatalf("List(ReadyFlag): %v", err) - } - got := make([]string, 0, len(listed.Items)) - for _, row := range listed.Items { - got = append(got, row.ID) - } - if !slices.Equal(got, tc.want) { - t.Fatalf("List(ReadyFlag) = %v, want %v", got, tc.want) - } - if slices.Contains(raw.served, "List") { - t.Fatalf("served calls = %v: a ready listing reached the served List", raw.served) - } - }) - } -} - -func TestRemoteClaimerRefusesExternallyBlockedWork(t *testing.T) { - for _, enforced := range []bool{false, true} { - t.Run(fmt.Sprintf("server enforces %v", enforced), func(t *testing.T) { - a := issue("be-a") - raw := newRemoteStore(a) - raw.enforced = enforced - blockOnPayments(raw, a.ID) - claimer, err := testStore(raw, &fakeStore{}, true).IssueClaimer() - if err != nil { - t.Fatalf("IssueClaimer: %v", err) - } - - _, err = claimer.Claim(t.Context(), publicops.ClaimRequest{Actor: "worker", IssueID: a.ID}) - if enforced { - // The server already applied the policy, so the claim is its - // to answer. - if err != nil || !slices.Equal(raw.served, []string{"Claim be-a"}) { - t.Fatalf("Claim = %v, served %v; want the served claim", err, raw.served) - } - return - } - if !errors.Is(err, storage.ErrCloseBlocked) || !strings.Contains(err.Error(), paymentsRef) { - t.Fatalf("Claim = %v, want ErrCloseBlocked naming %s", err, paymentsRef) - } - if len(raw.served) != 0 { - t.Fatalf("served calls = %v, want none for a refused claim", raw.served) - } - }) - } -} - -// TestRemoteReadyClaimerSkipsExternallyBlockedWork: the served claim-next -// cannot be told what to skip, so with anything externally blocked the claim -// walks the policy's ready page through the served Claimer instead. be-a sorts -// first, where the served claim-next would take it. -func TestRemoteReadyClaimerSkipsExternallyBlockedWork(t *testing.T) { - a, b := issue("be-a"), issue("be-b") - raw := newRemoteStore(a, b) - blockOnPayments(raw, a.ID) - claimer, err := testStore(raw, &fakeStore{}, true).ReadyClaimer() - if err != nil { - t.Fatalf("ReadyClaimer: %v", err) - } - - next, err := claimer.ClaimNext(t.Context(), publicops.ClaimNextRequest{Actor: "worker"}) - if err != nil { - t.Fatalf("ClaimNext: %v", err) - } - if next.Claimed == nil || next.Claimed.ID != b.ID || b.Assignee != "worker" { - t.Fatalf("ClaimNext claimed %+v, want %s for worker", next.Claimed, b.ID) - } - if want := []string{"Ready", "Claim be-b"}; !slices.Equal(raw.served, want) { - t.Fatalf("served calls = %v, want %v", raw.served, want) - } -} - -// TestRemoteReadyClaimerReportsLostRaces: a front that had work but lost every -// candidate is an error, not the empty answer that means there is no work. -func TestRemoteReadyClaimerReportsLostRaces(t *testing.T) { - a, b := issue("be-a"), issue("be-b") - raw := newRemoteStore(a, b) - blockOnPayments(raw, a.ID) - raw.claimErr = storage.ErrAlreadyClaimed - claimer, err := testStore(raw, &fakeStore{}, true).ReadyClaimer() - if err != nil { - t.Fatalf("ReadyClaimer: %v", err) - } - - next, err := claimer.ClaimNext(t.Context(), publicops.ClaimNextRequest{Actor: "worker"}) - if err == nil || !strings.Contains(err.Error(), "lost 1 races") || next.Claimed != nil { - t.Fatalf("ClaimNext = %+v, %v; want a lost-races error", next.Claimed, err) - } -} - -// TestRemoteReadyClaimerTakesOnlyUnassignedWork: the composed claim selects -// what a local claim-next selects (ClaimReadyIssueInTx), open and unassigned -// whatever assignee the request names. A row another actor holds, sorted -// first, is not a race to lose, and a request naming the caller does not try -// to re-take the caller's own row. -func TestRemoteReadyClaimerTakesOnlyUnassignedWork(t *testing.T) { - for _, tc := range []struct { - name string - filter publicops.ReadyRequest - }{ - {name: "any assignee"}, - {name: "the caller as assignee", filter: publicops.ReadyRequest{Assignee: "worker"}}, - } { - t.Run(tc.name, func(t *testing.T) { - held, blocked, mine, free := issue("be-held"), issue("be-blocked"), issue("be-mine"), issue("be-free") - held.Assignee, mine.Assignee = "alice", "worker" - raw := newRemoteStore(held, blocked, mine, free) - blockOnPayments(raw, blocked.ID) - claimer, err := testStore(raw, &fakeStore{}, true).ReadyClaimer() - if err != nil { - t.Fatalf("ReadyClaimer: %v", err) - } - - next, err := claimer.ClaimNext(t.Context(), publicops.ClaimNextRequest{Actor: "worker", Filter: tc.filter}) - if err != nil { - t.Fatalf("ClaimNext: %v", err) - } - if next.Claimed == nil || next.Claimed.ID != free.ID || free.Assignee != "worker" { - t.Fatalf("ClaimNext claimed %+v, want %s for worker", next.Claimed, free.ID) - } - if want := []string{"Ready", "Claim be-free"}; !slices.Equal(raw.served, want) { - t.Fatalf("served calls = %v, want %v", raw.served, want) - } - if held.Assignee != "alice" { - t.Fatalf("be-held assignee = %q, want alice's claim untouched", held.Assignee) - } - }) - } -} - -// TestRemoteReadyClaimerWalksAContendedFront: candidates a rival takes between -// the read and the claim are passed over, however many lead the front. A full -// window lost end to end is read once more, and the rows the rivals took have -// left it; only when that pass is lost too is it an error, and a second pass -// with nothing left to take is no work. -func TestRemoteReadyClaimerWalksAContendedFront(t *testing.T) { - for _, tc := range []struct { - name string - rivals int // leading candidates a rival takes first - last bool // be-last follows them - lastTaken bool // and a rival takes it first too - wantLast bool - wantErr string - wantReads int - }{ - {name: "three lost ahead", rivals: 3, last: true, wantLast: true, wantReads: 1}, - {name: "a full window lost", rivals: remoteClaimPage, last: true, wantLast: true, wantReads: 2}, - {name: "both passes lost", rivals: remoteClaimPage, last: true, lastTaken: true, wantErr: "lost 26 races", wantReads: 2}, - {name: "nothing behind a lost window", rivals: remoteClaimPage, wantReads: 2}, - } { - t.Run(tc.name, func(t *testing.T) { - blocked, last := issue("be-blocked"), issue("be-last") - raw := newRemoteStore(blocked) - blockOnPayments(raw, blocked.ID) - raw.takenFirst = make(map[string]bool) - for i := range tc.rivals { - taken := issue(fmt.Sprintf("be-taken-%02d", i)) - raw.ready = append(raw.ready, taken) - raw.takenFirst[taken.ID] = true - } - if tc.last { - raw.ready = append(raw.ready, last) - raw.takenFirst[last.ID] = tc.lastTaken - } - claimer, err := testStore(raw, &fakeStore{}, true).ReadyClaimer() - if err != nil { - t.Fatalf("ReadyClaimer: %v", err) - } - - next, err := claimer.ClaimNext(t.Context(), publicops.ClaimNextRequest{Actor: "worker"}) - switch { - case tc.wantErr != "": - if err == nil || !strings.Contains(err.Error(), tc.wantErr) || next.Claimed != nil { - t.Fatalf("ClaimNext = %+v, %v; want a %q error", next.Claimed, err, tc.wantErr) - } - case err != nil: - t.Fatalf("ClaimNext: %v", err) - case tc.wantLast: - if next.Claimed == nil || next.Claimed.ID != last.ID || last.Assignee != "worker" { - t.Fatalf("ClaimNext claimed %+v, want %s for worker", next.Claimed, last.ID) - } - case next.Claimed != nil: - t.Fatalf("ClaimNext claimed %+v, want no work: rivals took every candidate", next.Claimed) - } - reads := 0 - for _, call := range raw.served { - if call == "Ready" { - reads++ - } - } - if reads != tc.wantReads { - t.Fatalf("served ready reads = %d, want %d", reads, tc.wantReads) - } - }) - } -} - -// TestRemoteBlockingAnnotatorMergesExternalBlockers: the served annotation's -// own blockers stay, the external ones join them without an is_blocked flag -// the request did not ask for, and the edges behind them are read in chunks -// the wire accepts. -func TestRemoteBlockingAnnotatorMergesExternalBlockers(t *testing.T) { - issues := make([]*types.Issue, 0, 150) - ids := make([]string, 0, 150) - for i := range 150 { - issues = append(issues, issue(fmt.Sprintf("be-%03d", i))) - ids = append(ids, issues[i].ID) - } - raw := newRemoteStore(issues...) - blockOnPayments(raw, "be-000", "be-149") - raw.localBlockers = map[string][]string{"be-000": {"be-local"}} - annotator, err := testStore(raw, &fakeStore{}, true).BlockingAnnotator() - if err != nil { - t.Fatalf("BlockingAnnotator: %v", err) - } - - result, err := annotator.AnnotateBlocking(t.Context(), publicops.BlockingRequest{IDs: ids}) - if err != nil { - t.Fatalf("AnnotateBlocking: %v", err) - } - blockedBy := make(map[string][]string, len(result.Items)) - for _, item := range result.Items { - blockedBy[item.ID] = item.BlockedBy - if item.IsBlocked != nil { - t.Errorf("%s is_blocked = %v unasked, want no flag", item.ID, *item.IsBlocked) - } - } - if want := []string{"be-local", paymentsRef}; !slices.Equal(blockedBy["be-000"], want) { - t.Errorf("be-000 blocked by %v, want %v", blockedBy["be-000"], want) - } - if want := []string{paymentsRef}; !slices.Equal(blockedBy["be-149"], want) { - t.Errorf("be-149 blocked by %v, want %v", blockedBy["be-149"], want) - } - if len(blockedBy["be-001"]) != 0 { - t.Errorf("be-001 blocked by %v, want nothing", blockedBy["be-001"]) - } - if want := []int{100, 50}; !slices.Equal(raw.edgeReads, want) { - t.Errorf("served edge reads named %v anchors, want %v", raw.edgeReads, want) - } -} - -// TestRemoteEdgesNamesEachAnchorOnce: the edge read behind every per-issue -// policy question names each id once and never an empty one, which the http -// EdgeReader refuses; nothing to name reads nothing. -func TestRemoteEdgesNamesEachAnchorOnce(t *testing.T) { - raw := newRemoteStore(issue("be-a")) - blockOnPayments(raw, "be-a") - store := testStore(raw, &fakeStore{}, true) - - deps, err := store.remoteEdges(t.Context(), []string{"be-a", "", "be-a"}) - if err != nil { - t.Fatalf("remoteEdges: %v", err) - } - if len(deps["be-a"]) != 1 { - t.Fatalf("be-a edges = %v, want its one edge", deps["be-a"]) - } - if _, err := store.remoteEdges(t.Context(), []string{""}); err != nil { - t.Fatalf("remoteEdges of an empty id: %v", err) - } - if want := []int{1}; !slices.Equal(raw.edgeReads, want) { - t.Fatalf("served edge reads named %v anchors, want %v", raw.edgeReads, want) - } -} - -// TestRemoteTreeWalkerAddsExternalLeaves: the served down-tree gains the leaf -// its stored edges name, a leaf the server rendered is not repeated, and any -// other walk is the served answer as it came. A policy probe that fails does -// not fail the walk the server answered: its leaves are rendered here. -func TestRemoteTreeWalkerAddsExternalLeaves(t *testing.T) { - root := &types.TreeNode{Issue: *issue("be-a")} - served := &types.TreeNode{Issue: types.Issue{ID: paymentsRef, Title: "served leaf"}, Depth: 1, ParentID: "be-a"} - for _, tc := range []struct { - name string - req publicops.WalkTreeRequest - tree []*types.TreeNode - probeErr error - wantIDs []string - }{ - {name: "down walk", req: publicops.WalkTreeRequest{RootID: "be-a", MaxDepth: 50}, tree: []*types.TreeNode{root}, wantIDs: []string{"be-a", paymentsRef}}, - {name: "server rendered the leaf", req: publicops.WalkTreeRequest{RootID: "be-a", MaxDepth: 50}, tree: []*types.TreeNode{root, served}, wantIDs: []string{"be-a", paymentsRef}}, - {name: "up walk", req: publicops.WalkTreeRequest{RootID: "be-a", MaxDepth: 50, Direction: publicops.TreeUp}, tree: []*types.TreeNode{root}, wantIDs: []string{"be-a"}}, - {name: "probe fails", req: publicops.WalkTreeRequest{RootID: "be-a", MaxDepth: 50}, tree: []*types.TreeNode{root}, probeErr: errors.New("probe failed"), wantIDs: []string{"be-a", paymentsRef}}, - {name: "probe fails, server rendered the leaf", req: publicops.WalkTreeRequest{RootID: "be-a", MaxDepth: 50}, tree: []*types.TreeNode{root, served}, probeErr: errors.New("probe failed"), wantIDs: []string{"be-a", paymentsRef}}, - } { - t.Run(tc.name, func(t *testing.T) { - raw := newRemoteStore() - raw.tree = tc.tree - raw.enforcedErr = tc.probeErr - blockOnPayments(raw, "be-a") - walker, err := testStore(raw, &fakeStore{}, true).TreeWalker() - if err != nil { - t.Fatalf("TreeWalker: %v", err) - } - - result, err := walker.WalkTree(t.Context(), tc.req) - if err != nil { - t.Fatalf("WalkTree: %v", err) - } - ids := make([]string, 0, len(result.Nodes)) - for _, node := range result.Nodes { - ids = append(ids, node.ID) - } - if !slices.Equal(ids, tc.wantIDs) { - t.Fatalf("tree = %v, want %v", ids, tc.wantIDs) - } - if leaf := result.Nodes[len(result.Nodes)-1]; tc.name == "down walk" || tc.name == "probe fails" { - if leaf.Status != types.StatusOpen || leaf.ParentID != "be-a" || leaf.Depth != 1 || leaf.Title != "○ payments" { - t.Fatalf("leaf = %+v, want the open payments leaf under be-a", leaf) - } - } - }) - } -} - -// TestRemoteBatchCloserRefusesExternallyBlockedItems: a still-open externally -// blocked item is refused the way the local policy close refuses it and the -// rest close in one served request. An item already closed keeps the served -// answer, Force waives the policy, and a server that enforces the policy gets -// the whole batch. -func TestRemoteBatchCloserRefusesExternallyBlockedItems(t *testing.T) { - for _, tc := range []struct { - name string - force bool - enforced bool - wantRefused bool - }{ - {name: "policy", wantRefused: true}, - {name: "force", force: true}, - {name: "server enforces", enforced: true}, - } { - t.Run(tc.name, func(t *testing.T) { - blocked, free, done := issue("be-blocked"), issue("be-free"), issue("be-done") - done.Status = types.StatusClosed - raw := newRemoteStore(blocked, free, done) - raw.enforced = tc.enforced - blockOnPayments(raw, blocked.ID, done.ID) - closer, err := testStore(raw, &fakeStore{}, true).BatchCloser() - if err != nil { - t.Fatalf("BatchCloser: %v", err) - } - - request := publicops.CloseBatchRequest{Actor: "worker", Force: tc.force, Items: closeItems(blocked.ID, free.ID, done.ID)} - result, err := closer.CloseBatch(t.Context(), request) - if err != nil { - t.Fatalf("CloseBatch: %v", err) - } - if len(result.Outcomes) != len(request.Items) { - t.Fatalf("CloseBatch answered %d outcomes for %d items", len(result.Outcomes), len(request.Items)) - } - for i, outcome := range result.Outcomes { - if outcome.IssueID != request.Items[i].IssueID { - t.Fatalf("outcome %d is %s's, want %s's", i, outcome.IssueID, request.Items[i].IssueID) - } - } - wantServed := []string{blocked.ID, free.ID, done.ID} - if tc.wantRefused { - refusal := result.Outcomes[0].Err - want := "cannot close blocked issue: be-blocked is blocked by [" + paymentsRef + "]" - if !errors.Is(refusal, storage.ErrCloseBlocked) || refusal.Error() != want { - t.Fatalf("be-blocked outcome = %v, want %q", refusal, want) - } - wantServed = []string{free.ID, done.ID} - } else if result.Outcomes[0].Err != nil { - t.Fatalf("be-blocked outcome = %v, want it closed", result.Outcomes[0].Err) - } - if result.Outcomes[1].Err != nil || result.Outcomes[2].Err != nil { - t.Fatalf("be-free, be-done outcomes = %v, %v; want the served answers", result.Outcomes[1].Err, result.Outcomes[2].Err) - } - if want := [][]string{wantServed}; !slices.EqualFunc(raw.closes, want, slices.Equal[[]string]) { - t.Fatalf("served closes = %v, want %v", raw.closes, want) - } - }) - } -} - -// TestRemoteBatchCloserAnswersAFailedReadWithIt: whether an externally blocked -// item is live is a read, and a read that fails is that item's outcome. Passing -// the item to the server's close instead would close blocked work on a guess. -// A missing item still travels, for the server's own not-found. -func TestRemoteBatchCloserAnswersAFailedReadWithIt(t *testing.T) { - errRead := errors.New("served read failed") - for _, tc := range []struct { - name string - present bool - readErr error - wantErr error - wantServed []string - }{ - {name: "failed read", present: true, readErr: errRead, wantErr: errRead, wantServed: []string{"be-free"}}, - {name: "missing", readErr: fmt.Errorf("%w: issue be-blocked", storage.ErrNotFound), wantErr: storage.ErrNotFound, wantServed: []string{"be-blocked", "be-free"}}, - } { - t.Run(tc.name, func(t *testing.T) { - blocked, free := issue("be-blocked"), issue("be-free") - raw := newRemoteStore(free) - if tc.present { - raw.ready = append(raw.ready, blocked) - } - blockOnPayments(raw, blocked.ID) - raw.getErrs = map[string]error{blocked.ID: tc.readErr} - closer, err := testStore(raw, &fakeStore{}, true).BatchCloser() - if err != nil { - t.Fatalf("BatchCloser: %v", err) - } - - result, err := closer.CloseBatch(t.Context(), publicops.CloseBatchRequest{Actor: "worker", Items: closeItems(blocked.ID, free.ID)}) - if err != nil { - t.Fatalf("CloseBatch: %v", err) - } - if len(result.Outcomes) != 2 || result.Outcomes[0].IssueID != blocked.ID || !errors.Is(result.Outcomes[0].Err, tc.wantErr) || result.Outcomes[1].Err != nil { - t.Fatalf("outcomes = %+v; want be-blocked answered %v and be-free closed", result.Outcomes, tc.wantErr) - } - if blocked.Status != types.StatusOpen { - t.Fatalf("be-blocked is %s, want it still open", blocked.Status) - } - if want := [][]string{tc.wantServed}; !slices.EqualFunc(raw.closes, want, slices.Equal[[]string]) { - t.Fatalf("served closes = %v, want %v", raw.closes, want) - } - }) - } -} - -// TestRemoteBatchCloserRefusesANextClaim: the served batch close cannot carry -// a next claim, so a batch with one is refused whole, the same way whether the -// workspace holds an open holder of an external ref, only a closed one, or -// none, and with Force too. The refusal leads with that wire fact, reads -// nothing, closes nothing and names the commands that do the same work. A -// server that enforces the policy itself gets the request as it came, and its -// closer answers. -func TestRemoteBatchCloserRefusesANextClaim(t *testing.T) { - for _, tc := range []struct { - name string - holder types.Status - force bool - enforced bool - wantServed []string - }{ - {name: "open holder", holder: types.StatusOpen}, - {name: "closed holder", holder: types.StatusClosed}, - {name: "no holder"}, - {name: "force", holder: types.StatusOpen, force: true}, - {name: "server enforces", holder: types.StatusOpen, enforced: true, wantServed: []string{"CloseBatch ClaimNext"}}, - } { - t.Run(tc.name, func(t *testing.T) { - free, holder := issue("be-free"), issue("be-holder") - raw := newRemoteStore(free, holder) - raw.enforced = tc.enforced - if tc.holder != "" { - holder.Status = tc.holder - blockOnPayments(raw, holder.ID) - } - closer, err := testStore(raw, &fakeStore{}, true).BatchCloser() - if err != nil { - t.Fatalf("BatchCloser: %v", err) - } - - _, err = closer.CloseBatch(t.Context(), publicops.CloseBatchRequest{ - Actor: "worker", Force: tc.force, Items: closeItems(free.ID), ClaimNext: &publicops.ReadyRequest{}, - }) - if tc.enforced { - if !errors.Is(err, errServedClaimNextRefused) { - t.Fatalf("CloseBatch with ClaimNext = %v, want the served closer's answer", err) - } - } else { - var unsupported *storage.ErrUnsupported - if !errors.As(err, &unsupported) || unsupported.Op != "CloseBatchRequest.ClaimNext" { - t.Fatalf("CloseBatch with ClaimNext = %v, want the CloseBatchRequest.ClaimNext refusal", err) - } - if msg := err.Error(); !strings.Contains(msg, "cannot carry a next claim") || !strings.Contains(msg, "nothing was closed") || !strings.Contains(msg, "then run `bd ready --claim`") { - t.Fatalf("refusal %q does not say why, that nothing closed, and what to run instead", msg) - } - } - if !slices.Equal(raw.served, tc.wantServed) { - t.Fatalf("served calls = %v, want %v", raw.served, tc.wantServed) - } - if len(raw.closes) != 0 || len(raw.edgeReads) != 0 || raw.scans != 0 { - t.Fatalf("closes %v, edge reads %v, scans %d; want none", raw.closes, raw.edgeReads, raw.scans) - } - }) - } -} - -// TestRemoteBatchCloserValidatesANextClaimFirst: a next claim that is invalid -// on every backend is ErrValidation, not the refusal. -func TestRemoteBatchCloserValidatesANextClaimFirst(t *testing.T) { - for name, claim := range map[string]*publicops.ReadyRequest{ - "offset": {Offset: 1}, - "bad sort": {Sort: "bogus"}, - } { - t.Run(name, func(t *testing.T) { - raw := newRemoteStore(issue("be-free")) - closer, err := testStore(raw, &fakeStore{}, true).BatchCloser() - if err != nil { - t.Fatalf("BatchCloser: %v", err) - } - - _, err = closer.CloseBatch(t.Context(), publicops.CloseBatchRequest{ - Actor: "worker", Items: closeItems("be-free"), ClaimNext: claim, - }) - var unsupported *storage.ErrUnsupported - if !errors.Is(err, storage.ErrValidation) || errors.As(err, &unsupported) { - t.Fatalf("CloseBatch with ClaimNext %+v = %v, want ErrValidation", claim, err) - } - }) - } -} diff --git a/internal/storage/externaldeps/remote_roles_test.go b/internal/storage/externaldeps/remote_roles_test.go deleted file mode 100644 index ebfa86531..000000000 --- a/internal/storage/externaldeps/remote_roles_test.go +++ /dev/null @@ -1,359 +0,0 @@ -package externaldeps - -import ( - "context" - "errors" - "strings" - "testing" - - "github.com/steveyegge/beads/internal/storage" - "github.com/steveyegge/beads/internal/storage/uow" - "github.com/steveyegge/beads/internal/types" - "github.com/steveyegge/beads/issueops" -) - -func pageOf(hasMore bool, ids ...string) issueops.IssuePage { - items := make([]*issueops.IssueWithCounts, 0, len(ids)) - for _, id := range ids { - items = append(items, &issueops.IssueWithCounts{Issue: &types.Issue{ID: id}}) - } - return issueops.IssuePage{Items: items, HasMore: hasMore} -} - -func idsOf(page issueops.IssuePage) string { - ids := make([]string, 0, len(page.Items)) - for _, row := range page.Items { - ids = append(ids, row.ID) - } - return strings.Join(ids, ",") -} - -// TestExcludeAndPage pins the client-side half of the remote ready -// exclusion: drop the blocked rows from the widened window, then cut the -// caller's own page, with an honest HasMore. -func TestExcludeAndPage(t *testing.T) { - blocked := map[string]bool{"b1": true, "b2": true} - for _, tc := range []struct { - name string - window issueops.IssuePage - offset int - limit int - wantIDs string - wantHasMore bool - }{ - {"blocked rows leave the page full", pageOf(false, "b1", "a", "b2", "c", "d"), 0, 2, "a,c", true}, - {"offset counts unblocked rows only", pageOf(false, "a", "b1", "c", "d"), 1, 2, "c,d", false}, - {"unlimited keeps every unblocked row", pageOf(false, "a", "b1", "c"), 0, 0, "a,c", false}, - {"a server-truncated window keeps HasMore", pageOf(true, "a", "b1"), 0, 5, "a", true}, - {"an offset past the end is an empty page", pageOf(false, "a"), 3, 2, "", false}, - } { - t.Run(tc.name, func(t *testing.T) { - got := excludeAndPage(tc.window, blocked, tc.offset, tc.limit) - if idsOf(got) != tc.wantIDs || got.HasMore != tc.wantHasMore { - t.Errorf("got %q hasMore=%v, want %q hasMore=%v", idsOf(got), got.HasMore, tc.wantIDs, tc.wantHasMore) - } - if got.Items == nil { - t.Error("an empty page must be an empty slice, not nil") - } - }) - } -} - -func TestWidenedLimit(t *testing.T) { - if got := *widenedLimit(3, 10, 2); got != 15 { - t.Errorf("widenedLimit(3,10,2) = %d, want 15", got) - } - if got := *widenedLimit(3, 0, 2); got != 0 { - t.Errorf("an unlimited read stays unlimited, got %d", got) - } -} - -// remoteFake is a remote backend (storage.RemoteBackendStore) whose legacy -// method seam is the embedded fakeStore's and whose reader role is its own: -// remoteReader must be reached through the decorator's IssueReader, and the -// fake's legacy ready reads are never consulted by it. -type remoteFake struct { - *fakeStore - reader *recordingReader -} - -func (r *remoteFake) IsRemoteBackendStore() bool { return true } - -func (r *remoteFake) IssueReader() (issueops.Reader, error) { return r.reader, nil } - -// recordingReader serves a fixed ready set in order and records the limits it -// was asked for, so the widened window is observable. -type recordingReader struct { - ready []string - limits []int - lists int -} - -func (r *recordingReader) Ready(_ context.Context, req issueops.ReadyRequest) (issueops.IssuePage, error) { - limit := 0 - if req.Limit != nil { - limit = *req.Limit - } - r.limits = append(r.limits, limit) - ids := r.ready[req.Offset:] - hasMore := false - if limit > 0 && len(ids) > limit { - ids, hasMore = ids[:limit], true - } - return pageOf(hasMore, ids...), nil -} - -func (r *recordingReader) List(context.Context, issueops.ListRequest) (issueops.IssuePage, error) { - r.lists++ - return pageOf(false, r.ready...), nil -} - -func (r *recordingReader) Get(context.Context, issueops.GetRequest) (*issueops.IssueDetails, error) { - return nil, issueops.ErrNotFound -} - -func newRemotePolicyStore(enforced bool) (*Store, *recordingReader) { - reader := &recordingReader{ready: []string{"be-x", "be-a", "be-b"}} - raw := &remoteFake{ - fakeStore: &fakeStore{ - enforced: enforced, - deps: map[string][]*types.Dependency{ - "be-x": {externalDep("be-x", "external:remote:payments", types.DepBlocks)}, - }, - }, - reader: reader, - } - store := New(raw, func(ProjectName) (string, bool) { return "", false }, nil) - store.warnProject = nil - return store, reader -} - -// TestRemoteReaderReadyExcludesExternallyBlocked is the role-level half of -// S6c's ready exclusion (bd ready itself reads through the legacy seam): the -// reader the decorator hands out for a remote store drops the externally -// blocked row from a widened window and still fills the page. -func TestRemoteReaderReadyExcludesExternallyBlocked(t *testing.T) { - store, reader := newRemotePolicyStore(false) - rd, err := store.IssueReader() - if err != nil { - t.Fatal(err) - } - if _, ok := rd.(*remoteReader); !ok { - t.Fatalf("IssueReader over a remote store = %T, want the inner role passed through", rd) - } - one := 1 - page, err := rd.Ready(t.Context(), issueops.ReadyRequest{Limit: &one}) - if err != nil { - t.Fatal(err) - } - if idsOf(page) != "be-a" || !page.HasMore { - t.Errorf("Ready(limit 1) = %q hasMore=%v, want \"be-a\" with more", idsOf(page), page.HasMore) - } - if len(reader.limits) != 1 || reader.limits[0] != 2 { - t.Errorf("inner ready limits = %v, want one read widened to 2", reader.limits) - } - if _, err := rd.List(t.Context(), issueops.ListRequest{}); err != nil || reader.lists != 1 { - t.Errorf("an ordinary List did not pass through: lists=%d err=%v", reader.lists, err) - } -} - -// TestRemoteReaderReadyPassesThroughWhenServerEnforces: a server that -// advertises the policy has applied it; the client neither filters nor widens. -func TestRemoteReaderReadyPassesThroughWhenServerEnforces(t *testing.T) { - store, reader := newRemotePolicyStore(true) - rd, err := store.IssueReader() - if err != nil { - t.Fatal(err) - } - two := 2 - page, err := rd.Ready(t.Context(), issueops.ReadyRequest{Limit: &two}) - if err != nil { - t.Fatal(err) - } - if idsOf(page) != "be-x,be-a" || reader.limits[0] != 2 { - t.Errorf("Ready = %q (limits %v), want the server's page untouched", idsOf(page), reader.limits) - } -} - -type recordingClaimer struct{ claimed []string } - -func (c *recordingClaimer) Claim(_ context.Context, req issueops.ClaimRequest) (issueops.ClaimResult, error) { - c.claimed = append(c.claimed, req.IssueID) - return issueops.ClaimResult{Issue: &types.Issue{ID: req.IssueID}, Changed: true}, nil -} - -// remoteClaimFake adds the claim and edge roles a remote claim check uses. -type remoteClaimFake struct { - *remoteFake - claimer *recordingClaimer -} - -func (r *remoteClaimFake) IssueClaimer() (issueops.Claimer, error) { return r.claimer, nil } - -func (r *remoteClaimFake) EdgeReader() (issueops.EdgeReader, error) { return fakeEdges{r.deps}, nil } - -type fakeEdges struct { - deps map[string][]*types.Dependency -} - -func (f fakeEdges) ReadEdges(_ context.Context, req issueops.EdgeReadRequest) (issueops.EdgeReadResult, error) { - out := issueops.EdgeReadResult{} - for _, id := range req.IDs { - out.Anchors = append(out.Anchors, issueops.AnchorEdges{ID: id, Edges: f.deps[id]}) - } - return out, nil -} - -// TestRemoteClaimerRefusesExternallyBlocked pins the direct-claim guard of -// the remote composition (the CLI's own `update --claim` goes through the -// lifecycle guard instead; this role is what bd serve and library callers -// reach), reading only the claimed issue's edges. -func TestRemoteClaimerRefusesExternallyBlocked(t *testing.T) { - store, _ := newRemotePolicyStore(false) - raw := &remoteClaimFake{remoteFake: store.inner.(*remoteFake), claimer: &recordingClaimer{}} - store = New(raw, func(ProjectName) (string, bool) { return "", false }, nil) - store.warnProject = nil - claimer, err := store.IssueClaimer() - if err != nil { - t.Fatal(err) - } - if _, err := claimer.Claim(t.Context(), issueops.ClaimRequest{Actor: "a", IssueID: "be-x"}); !errors.Is(err, storage.ErrCloseBlocked) { - t.Errorf("claim of the externally blocked be-x: err = %v, want ErrCloseBlocked", err) - } - if _, err := claimer.Claim(t.Context(), issueops.ClaimRequest{Actor: "a", IssueID: "be-a"}); err != nil { - t.Errorf("claim of the unblocked be-a: %v", err) - } - if strings.Join(raw.claimer.claimed, ",") != "be-a" { - t.Errorf("server claims = %v, want only be-a", raw.claimer.claimed) - } -} - -// passthroughFake is a remote store whose policy-bearing roles all record -// their calls, and whose edge reader fails the test if read: against a server -// that applies the policy, the client must neither read edges nor run its own -// read-then-claim loop. -type passthroughFake struct { - *remoteFake - claimer *recordingClaimer - claimNexts int - counts int - closes int - edgeReads int -} - -func (p *passthroughFake) IssueClaimer() (issueops.Claimer, error) { return p.claimer, nil } - -func (p *passthroughFake) EdgeReader() (issueops.EdgeReader, error) { return countingEdges{p}, nil } - -func (p *passthroughFake) ReadyClaimer() (issueops.ReadyClaimer, error) { return p, nil } - -func (p *passthroughFake) ReadyCounter() (issueops.ReadyCounter, error) { return p, nil } - -func (p *passthroughFake) BatchCloser() (issueops.BatchCloser, error) { return p, nil } - -func (p *passthroughFake) ClaimNext(context.Context, issueops.ClaimNextRequest) (issueops.ClaimNextResult, error) { - p.claimNexts++ - return issueops.ClaimNextResult{Claimed: &issueops.IssueWithCounts{Issue: &types.Issue{ID: "be-x"}}}, nil -} - -func (p *passthroughFake) CountReady(context.Context, issueops.ReadyRequest) (issueops.ReadyCountResult, error) { - p.counts++ - return issueops.ReadyCountResult{Total: 3}, nil -} - -func (p *passthroughFake) CloseBatch(_ context.Context, req issueops.CloseBatchRequest) (issueops.CloseBatchResult, error) { - p.closes++ - return issueops.CloseBatchResult{Outcomes: make([]issueops.CloseOutcome, len(req.Items))}, nil -} - -type countingEdges struct{ p *passthroughFake } - -func (c countingEdges) ReadEdges(context.Context, issueops.EdgeReadRequest) (issueops.EdgeReadResult, error) { - c.p.edgeReads++ - return issueops.EdgeReadResult{}, nil -} - -// TestRemoteRolesPassThroughWhenServerEnforces pins the advertised half of -// design 3.6 at the role level: every policy-bearing role over a server that -// advertises policy.external_dependencies is the server's own operation, -// untouched. In particular claim-next is ONE call to the server's atomic -// ClaimNext, even though the client's own view would call be-x blocked. -func TestRemoteRolesPassThroughWhenServerEnforces(t *testing.T) { - base, reader := newRemotePolicyStore(true) - raw := &passthroughFake{remoteFake: base.inner.(*remoteFake), claimer: &recordingClaimer{}} - store := New(raw, func(ProjectName) (string, bool) { return "", false }, nil) - store.warnProject = nil - ctx := t.Context() - - rc, err := store.ReadyClaimer() - if err != nil { - t.Fatal(err) - } - res, err := rc.ClaimNext(ctx, issueops.ClaimNextRequest{Actor: "a"}) - if err != nil || res.Claimed == nil || res.Claimed.ID != "be-x" { - t.Errorf("ClaimNext = %+v, %v; want the server's own answer", res, err) - } - if raw.claimNexts != 1 || len(reader.limits) != 0 || len(raw.claimer.claimed) != 0 { - t.Errorf("claim-next: server ClaimNext calls=%d, ready reads=%v, by-id claims=%v; want one atomic call and nothing else", - raw.claimNexts, reader.limits, raw.claimer.claimed) - } - - claimer, err := store.IssueClaimer() - if err != nil { - t.Fatal(err) - } - if _, err := claimer.Claim(ctx, issueops.ClaimRequest{Actor: "a", IssueID: "be-x"}); err != nil { - t.Errorf("claim of be-x against an enforcing server: %v; want the server to judge it", err) - } - - closer, err := store.BatchCloser() - if err != nil { - t.Fatal(err) - } - if _, err := closer.CloseBatch(ctx, issueops.CloseBatchRequest{Actor: "a", Items: []issueops.BatchCloseItem{{IssueID: "be-x"}}}); err != nil || raw.closes != 1 { - t.Errorf("close of be-x: closes=%d err=%v; want one server batch close", raw.closes, err) - } - - counter, err := store.ReadyCounter() - if err != nil { - t.Fatal(err) - } - if got, err := counter.CountReady(ctx, issueops.ReadyRequest{}); err != nil || got.Total != 3 || raw.counts != 1 { - t.Errorf("CountReady = %+v, %v (server counts %d); want the server's count", got, err, raw.counts) - } - - if raw.edgeReads != 0 { - t.Errorf("the client read edges %d times against a server that applies the policy", raw.edgeReads) - } -} - -// TestAppliedFindsThePolicyLayer pins what bd serve reads to decide whether to -// advertise policy.external_dependencies: the decorator anywhere in the chain -// counts, its absence does not. -func TestAppliedFindsThePolicyLayer(t *testing.T) { - raw := &fakeStore{} - if Applied(raw) || Applied(nil) { - t.Error("a chain without the policy layer reports it applied") - } - if !Applied(New(raw, nil, nil)) { - t.Error("the policy decorator itself is not reported applied") - } - if !Applied(storage.NewHookFiringStore(New(raw, nil, nil), nil)) { - t.Error("the policy decorator beneath the hook layer is not found") - } - plain := &fakeUOWProvider{} - if AppliedToProvider(nil) || AppliedToProvider(plain) { - t.Error("a provider chain without the policy layer reports it applied") - } - wrapped := WrapUOWProvider(plain, nil, nil) - if !AppliedToProvider(wrapped) { - t.Error("WrapUOWProvider's layer is not reported applied") - } - if !AppliedToProvider(uow.NewNotifyingProvider(wrapped, uow.Sinks{Hook: noHooks{}})) { - t.Error("the policy layer beneath a notifying provider is not found") - } -} - -type noHooks struct{} - -func (noHooks) Run(string, *types.Issue) {} diff --git a/internal/storage/externaldeps/resolver.go b/internal/storage/externaldeps/resolver.go deleted file mode 100644 index 2caeceb95..000000000 --- a/internal/storage/externaldeps/resolver.go +++ /dev/null @@ -1,124 +0,0 @@ -// Package externaldeps resolves explicit cross-project capability dependencies. -package externaldeps - -import ( - "context" - "fmt" - "os" - "strings" - - "github.com/steveyegge/beads/internal/storage" - "github.com/steveyegge/beads/internal/types" -) - -const externalPrefix = "external:" - -func defaultProjectWarning(project ProjectName) { - fmt.Fprintf(os.Stderr, "Warning: external project %q is unavailable; its capability dependencies remain blocking\n", project) -} - -// ProjectName is the configured name in external_projects. -type ProjectName string - -// CapabilityName is the suffix matched by a provides: label. -type CapabilityName string - -// ProjectLocator resolves a configured project name to its workspace root. -// The bool is false when the project is not configured or unavailable. -type ProjectLocator func(ProjectName) (string, bool) - -// StoreOpener opens a project workspace as a read-only store. -type StoreOpener func(context.Context, string) (storage.DoltStorage, error) - -type reference struct { - raw string - project ProjectName - capability CapabilityName - valid bool -} - -func parseReference(raw string) reference { - ref := reference{raw: raw} - if !strings.HasPrefix(raw, externalPrefix) { - return ref - } - parts := strings.SplitN(raw, ":", 3) - if len(parts) != 3 || parts[1] == "" || parts[2] == "" { - return ref - } - ref.project = ProjectName(parts[1]) - ref.capability = CapabilityName(parts[2]) - ref.valid = true - return ref -} - -func isExternalReference(raw string) bool { - return strings.HasPrefix(raw, externalPrefix) -} - -// resolveReferences returns ref -> satisfied. Every ref starts unsatisfied; -// malformed refs and foreign-store failures therefore fail closed. -func (s *Store) resolveReferences(ctx context.Context, refs []reference) (map[string]bool, error) { - result := make(map[string]bool, len(refs)) - byProject := make(map[ProjectName]map[CapabilityName][]string) - for _, ref := range refs { - result[ref.raw] = false - if !ref.valid { - continue - } - if byProject[ref.project] == nil { - byProject[ref.project] = make(map[CapabilityName][]string) - } - byProject[ref.project][ref.capability] = append(byProject[ref.project][ref.capability], ref.raw) - } - - if s.locateProject == nil || s.openProject == nil { - return result, nil - } - - for project, capabilities := range byProject { - if err := ctx.Err(); err != nil { - return nil, err - } - path, ok := s.locateProject(project) - if !ok { - s.warnUnresolvedProject(project) - continue - } - foreign, err := s.openProject(ctx, path) - if err != nil || foreign == nil { - if ctxErr := ctx.Err(); ctxErr != nil { - return nil, ctxErr - } - s.warnUnresolvedProject(project) - continue - } - - for capability, rawRefs := range capabilities { - issues, queryErr := foreign.GetIssuesByLabel(ctx, "provides:"+string(capability)) - if queryErr != nil { - if ctxErr := ctx.Err(); ctxErr != nil { - _ = foreign.Close() - return nil, ctxErr - } - s.warnUnresolvedProject(project) - continue - } - satisfied := false - for _, issue := range issues { - if issue != nil && issue.Status == types.StatusClosed { - satisfied = true - break - } - } - if satisfied { - for _, raw := range rawRefs { - result[raw] = true - } - } - } - _ = foreign.Close() - } - - return result, nil -} diff --git a/internal/storage/externaldeps/role_accessors.go b/internal/storage/externaldeps/role_accessors.go deleted file mode 100644 index 8cfe321a7..000000000 --- a/internal/storage/externaldeps/role_accessors.go +++ /dev/null @@ -1,199 +0,0 @@ -package externaldeps - -import ( - "context" - "fmt" - - "github.com/steveyegge/beads/internal/storage" - storageissueops "github.com/steveyegge/beads/internal/storage/issueops" - "github.com/steveyegge/beads/internal/types" - "github.com/steveyegge/beads/internal/workapi" - "github.com/steveyegge/beads/internal/workapi/storereader" - "github.com/steveyegge/beads/issueops" -) - -// IssueReader builds the read role on the policy store. In particular, Ready -// and List(ReadyFlag) must call this store's filtered ready methods rather than -// promoted methods on the undecorated store. -// -// A remote inner store answers with its own reader instead, with the ready -// exclusion laid over it (remote_roles.go): storereader is built from the -// legacy method seam a remote store only partly serves. -func (s *Store) IssueReader() (issueops.Reader, error) { - if s.rolesAreRemote() { - inner, err := s.inner.IssueReader() - if err != nil { - return nil, err - } - return &remoteReader{inner: inner, policy: s}, nil - } - return storereader.New(s) -} - -// IssueClaimer rejects a direct claim of externally blocked work before the -// backend's atomic claim operation. ReadyClaimer below handles selection among -// candidates; this method covers callers that already name an issue. -func (s *Store) IssueClaimer() (issueops.Claimer, error) { - inner, err := s.inner.IssueClaimer() - if err != nil { - return nil, err - } - if s.rolesAreRemote() { - return &remoteClaimer{inner: inner, policy: s}, nil - } - return &issueClaimer{inner: inner, policy: s}, nil -} - -type issueClaimer struct { - inner issueops.Claimer - policy *Store -} - -func (c *issueClaimer) Claim(ctx context.Context, req issueops.ClaimRequest) (issueops.ClaimResult, error) { - ctx = storage.WithLivePolicyProbe(ctx) - blocked, blockers, err := c.policy.IsBlocked(ctx, req.IssueID) - if err != nil { - return issueops.ClaimResult{}, err - } - if blocked { - return issueops.ClaimResult{}, fmt.Errorf("%w: %s is blocked by %v", storage.ErrCloseBlocked, req.IssueID, blockers) - } - return c.inner.Claim(ctx, req) -} - -// ReadyClaimer keeps external blockers out of the ready-claim selection used -// by HTTP serving. The local compare-and-swap remains inside ClaimReadyIssue. -func (s *Store) ReadyClaimer() (issueops.ReadyClaimer, error) { - if s.rolesAreRemote() { - inner, err := s.inner.ReadyClaimer() - if err != nil { - return nil, err - } - return &remoteReadyClaimer{inner: inner, policy: s}, nil - } - return &readyClaimer{policy: s}, nil -} - -type readyClaimer struct{ policy *Store } - -func (c *readyClaimer) ClaimNext(ctx context.Context, req issueops.ClaimNextRequest) (issueops.ClaimNextResult, error) { - ctx = storage.WithLivePolicyProbe(ctx) - if err := storageissueops.ValidateClaimNextRequest(req); err != nil { - return issueops.ClaimNextResult{}, err - } - filter, err := workapi.BuildReadyFilter(req.Filter) - if err != nil { - return issueops.ClaimNextResult{}, err - } - // ClaimReadyIssue does not perform the lazy wake owned by backend ready - // roles. Preserve it before selection, including through telemetry/hooks. - if waker, ok := storage.UnwrapStore(c.policy.inner).(storage.ExpiredDeferWaker); ok { - waker.WakeExpiredDefersAdvisory(ctx) - } - claimed, err := c.policy.ClaimReadyIssue(ctx, filter, req.Actor) - if err != nil || claimed == nil { - return issueops.ClaimNextResult{}, err - } - rows, err := c.policy.SearchIssuesWithCounts(ctx, "", types.IssueFilter{IDs: []string{claimed.ID}}) - if err != nil { - return issueops.ClaimNextResult{}, err - } - if len(rows) != 1 { - return issueops.ClaimNextResult{}, fmt.Errorf("claim ready: hydrate %s: expected one row, got %d", claimed.ID, len(rows)) - } - return issueops.ClaimNextResult{Claimed: rows[0]}, nil -} - -// BlockingAnnotator augments the backend's derived local answer with the -// external blockers that the policy resolves for the same ids. -func (s *Store) BlockingAnnotator() (issueops.BlockingAnnotator, error) { - inner, err := s.inner.BlockingAnnotator() - if err != nil { - return nil, err - } - if s.rolesAreRemote() { - return &remoteBlockingAnnotator{inner: inner, policy: s}, nil - } - return &blockingAnnotator{inner: inner, policy: s}, nil -} - -type blockingAnnotator struct { - inner issueops.BlockingAnnotator - policy *Store -} - -// AnnotateBlocking overlays, on the backend's own answer, what IsBlocked -// reports for each item: a blocked item's BlockedBy is IsBlocked's blocker -// list (local live blockers plus unsatisfied external refs). It reads that -// for the whole page at -// once (blockedStates), so a page costs a bounded number of reads and opens -// each foreign project at most once, instead of IsBlocked's reads and foreign -// opens per item. -func (a *blockingAnnotator) AnnotateBlocking(ctx context.Context, req issueops.BlockingRequest) (issueops.BlockingResult, error) { - result, err := a.inner.AnnotateBlocking(ctx, req) - if err != nil { - return issueops.BlockingResult{}, err - } - if len(result.Items) == 0 { - return result, nil - } - ids := make([]string, 0, len(result.Items)) - for _, item := range result.Items { - ids = append(ids, item.ID) - } - states, err := a.policy.blockedStates(ctx, ids) - if err != nil { - return issueops.BlockingResult{}, err - } - for i := range result.Items { - state := states[result.Items[i].ID] - if state.blocked { - result.Items[i].BlockedBy = state.blockers - // The flag follows IsBlockedBatch's external invariant: an - // unsatisfied external blocker blocks, whatever the column says. - if req.IncludeIsBlocked { - result.Items[i].IsBlocked = boolPtr(true) - } - } - } - return result, nil -} - -func boolPtr(v bool) *bool { return &v } - -// TreeWalker preserves synthetic external leaves for the normal down-tree -// request. Reverse walks do not follow a source's dependencies, and the -// combined/up variants retain the backend's existing traversal semantics. -func (s *Store) TreeWalker() (issueops.TreeWalker, error) { - inner, err := s.inner.TreeWalker() - if err != nil { - return nil, err - } - if s.rolesAreRemote() { - return &remoteTreeWalker{inner: inner, policy: s}, nil - } - return &treeWalker{inner: inner, policy: s}, nil -} - -type treeWalker struct { - inner issueops.TreeWalker - policy *Store -} - -func (t *treeWalker) WalkTree(ctx context.Context, req issueops.WalkTreeRequest) (issueops.TreeResult, error) { - if (req.Direction != "" && req.Direction != issueops.TreeDown) || req.Status != "" || req.MaxRows != 0 { - return t.inner.WalkTree(ctx, req) - } - nodes, err := t.policy.GetDependencyTree(ctx, req.RootID, req.MaxDepth, false, false) - if err != nil { - return issueops.TreeResult{}, err - } - return issueops.TreeResult{Nodes: nodes}, nil -} - -var ( - _ issueops.Claimer = (*issueClaimer)(nil) - _ issueops.ReadyClaimer = (*readyClaimer)(nil) - _ issueops.BlockingAnnotator = (*blockingAnnotator)(nil) - _ issueops.TreeWalker = (*treeWalker)(nil) -) diff --git a/internal/storage/externaldeps/uow_decorator.go b/internal/storage/externaldeps/uow_decorator.go deleted file mode 100644 index ec0d0d8a0..000000000 --- a/internal/storage/externaldeps/uow_decorator.go +++ /dev/null @@ -1,516 +0,0 @@ -package externaldeps - -import ( - "context" - "database/sql" - "errors" - "fmt" - "slices" - - "github.com/steveyegge/beads/internal/storage" - "github.com/steveyegge/beads/internal/storage/domain" - "github.com/steveyegge/beads/internal/storage/issueops" - "github.com/steveyegge/beads/internal/storage/uow" - "github.com/steveyegge/beads/internal/types" - publicops "github.com/steveyegge/beads/issueops" - "github.com/steveyegge/beads/memoryops" -) - -// WrapUOWProvider installs the external capability policy on the server UOW -// path. Proxied commands otherwise bypass storage decorators entirely. -func WrapUOWProvider(inner uow.UnitOfWorkProvider, locate ProjectLocator, open StoreOpener) uow.UnitOfWorkProvider { - if inner == nil { - return nil - } - return &uowProvider{UnitOfWorkProvider: inner, policy: New(nil, locate, open)} -} - -type uowProvider struct { - uow.UnitOfWorkProvider - policy *Store -} - -var _ uow.UnitOfWorkProvider = (*uowProvider)(nil) -var _ uow.MaintenanceProvider = (*uowProvider)(nil) -var _ uow.ProviderUnwrapper = (*uowProvider)(nil) - -// Unwrap lets callers deliberately peel policy decorators. In particular, -// bd serve must get beneath the notifying provider before handing a provider -// to HTTP handlers, which must never run workspace hooks. -func (p *uowProvider) Unwrap() uow.UnitOfWorkProvider { return p.UnitOfWorkProvider } - -// AppliedToProvider is Applied for the unit-of-work chain: it reports whether -// provider's decorator chain includes WrapUOWProvider's policy layer. -func AppliedToProvider(provider uow.UnitOfWorkProvider) bool { - for provider != nil { - if _, ok := provider.(*uowProvider); ok { - return true - } - u, ok := provider.(uow.ProviderUnwrapper) - if !ok { - return false - } - provider = u.Unwrap() - } - return false -} - -// RunNonTx preserves the optional maintenance capability exposed by the -// proxied provider. Wrapping the provider must not make unrelated commands -// such as compact lose access to their pinned connection. -func (p *uowProvider) RunNonTx(ctx context.Context, fn func(context.Context, *sql.Conn) error) error { - provider, ok := p.UnitOfWorkProvider.(uow.MaintenanceProvider) - if !ok { - return fmt.Errorf("external dependency UOW wrapper: maintenance operations unsupported") - } - return provider.RunNonTx(ctx, fn) -} - -func (p *uowProvider) NewUOW(ctx context.Context) (uow.UnitOfWork, error) { - inner, err := p.UnitOfWorkProvider.NewUOW(ctx) - if err != nil { - return nil, err - } - return &unitOfWork{UnitOfWork: inner, policy: p.policy}, nil -} - -// Provider capability accessors build roles on this wrapper. Delegating them -// to the inner provider would silently discard external-dependency policy for -// every command that reaches the proxied seam through an optional source. -func (p *uowProvider) IssueLifecycle() (publicops.Lifecycle, error) { return uow.NewIssueOperations(p) } -func (p *uowProvider) IssueReader() (publicops.Reader, error) { return uow.NewIssueReader(p) } -func (p *uowProvider) IssueClaimer() (publicops.Claimer, error) { return uow.NewIssueClaimer(p) } -func (p *uowProvider) IssueRelations() (publicops.Relations, error) { return uow.NewIssueRelations(p) } -func (p *uowProvider) EdgeReader() (publicops.EdgeReader, error) { return uow.NewEdgeReader(p) } -func (p *uowProvider) BlockingAnnotator() (publicops.BlockingAnnotator, error) { - return uow.NewBlockingAnnotator(p) -} -func (p *uowProvider) TreeWalker() (publicops.TreeWalker, error) { return uow.NewTreeWalker(p) } -func (p *uowProvider) BatchGetter() (publicops.BatchGetter, error) { return uow.NewBatchGetter(p) } - -// MoleculeStepper composes over this decorator's own roles, so the advance's -// claim answers to the external-dependency policy the claimer applies. -func (p *uowProvider) MoleculeStepper() (publicops.MoleculeStepper, error) { - return uow.NewMoleculeStepper(p) -} - -func (p *uowProvider) GraphCounter() (publicops.GraphCounter, error) { - return uow.NewGraphCounter(p) -} -func (p *uowProvider) Counter() (publicops.Counter, error) { return uow.NewCounter(p) } -func (p *uowProvider) ReadyCounter() (publicops.ReadyCounter, error) { - return uow.NewReadyCounter(p) -} -func (p *uowProvider) ReadyClaimer() (publicops.ReadyClaimer, error) { - return uow.NewReadyClaimer(p) -} -func (p *uowProvider) Querier() (publicops.Querier, error) { return uow.NewQuerier(p) } -func (p *uowProvider) StatsReporter() (publicops.StatsReporter, error) { - return uow.NewStatsReporter(p) -} -func (p *uowProvider) CycleDetector() (publicops.CycleDetector, error) { - return uow.NewCycleDetector(p) -} -func (p *uowProvider) Commenter() (publicops.Commenter, error) { return uow.NewCommenter(p) } -func (p *uowProvider) BatchCloser() (publicops.BatchCloser, error) { - return uow.NewBatchCloser(p) -} -func (p *uowProvider) BatchCreator() (publicops.BatchCreator, error) { - return uow.NewBatchCreator(p) -} -func (p *uowProvider) DependencyEditor() (publicops.DependencyEditor, error) { - return uow.NewDependencyEditor(p) -} -func (p *uowProvider) BatchApplier() (publicops.BatchApplier, error) { - return uow.NewBatchApplier(p) -} -func (p *uowProvider) Deleter() (publicops.Deleter, error) { return uow.NewDeleter(p) } -func (p *uowProvider) Sweeper() (publicops.Sweeper, error) { return uow.NewSweeper(p) } -func (p *uowProvider) Importer() (publicops.Importer, error) { return uow.NewImporter(p) } -func (p *uowProvider) Bootstrapper() (publicops.Bootstrapper, error) { - return uow.NewBootstrapper(p) -} -func (p *uowProvider) InitVerifier() (publicops.InitVerifier, error) { - return uow.NewInitVerifier(p) -} -func (p *uowProvider) WorkspaceConfig() (publicops.WorkspaceConfig, error) { - return uow.NewWorkspaceConfig(p) -} -func (p *uowProvider) VersionReconciler() (publicops.VersionReconciler, error) { - return uow.NewVersionReconciler(p) -} -func (p *uowProvider) MetadataCAS() (publicops.MetadataCAS, error) { return uow.NewMetadataCAS(p) } -func (p *uowProvider) Releaser() (publicops.Releaser, error) { return uow.NewReleaser(p) } -func (p *uowProvider) LeaseReclaimer() (publicops.LeaseReclaimer, error) { - return uow.NewLeaseReclaimer(p) -} - -// Heartbeater passes through: the external-dependency policy governs what a -// write may point at, and a heartbeat on an existing claim points at nothing. -func (p *uowProvider) Heartbeater() (publicops.Heartbeater, error) { return uow.NewHeartbeater(p) } - -// BlockedLister is built over THIS provider, so its unit of work's -// GetBlockedIssues is the decorated one above: unsatisfied external blockers -// are named, exactly as the store decorator names them. -func (p *uowProvider) BlockedLister() (publicops.BlockedLister, error) { - return uow.NewBlockedLister(p) -} -func (p *uowProvider) Memories() (memoryops.Memories, error) { return uow.NewMemories(p) } -func (p *uowProvider) EventsJournalCursor() (storage.EventsJournalCursor, error) { - return uow.NewEventsJournalCursor(p) -} - -func (p *uowProvider) SetPoolLimits(limits uow.PoolLimits) { - if tuner, ok := p.UnitOfWorkProvider.(uow.PoolTuner); ok { - tuner.SetPoolLimits(limits) - } -} - -func (p *uowProvider) SetEventsJournalEnabled(enabled bool) { - if configurer, ok := p.UnitOfWorkProvider.(storage.EventsJournalConfigurer); ok { - configurer.SetEventsJournalEnabled(enabled) - } -} - -// SetVersionedHistoryEnabled forwards dual-write issue-version activation -// inward, for the same reason SetEventsJournalEnabled does. -// -// This wrapper is the OUTERMOST provider on both real chains — cmd/bd/main.go -// and cmd/bd/serve.go each build wireExternalDependencyUOWProvider(...) around -// everything else — and activation in this family works by type-asserting the -// outermost provider value for the configurer interface. Without this method -// the assertion fails on exactly the chains that matter, so activation would -// silently enable nothing on uow-backed processes while the direct-store plane -// turned on: split-brain, with no error and no log. The inner provider's own -// forwarder cannot be reached from those call sites. -func (p *uowProvider) SetVersionedHistoryEnabled(enabled bool) { - if configurer, ok := p.UnitOfWorkProvider.(storage.VersionedHistoryConfigurer); ok { - configurer.SetVersionedHistoryEnabled(enabled) - } -} - -func (p *uowProvider) RunEventsMaintenanceTx(ctx context.Context, fn func(context.Context, issueops.DBTX) error) error { - runner, ok := p.UnitOfWorkProvider.(issueops.EventsMaintenanceRunner) - if !ok { - return fmt.Errorf("external dependency UOW wrapper: events-journal maintenance unsupported") - } - return runner.RunEventsMaintenanceTx(ctx, fn) -} - -var ( - _ uow.PoolTuner = (*uowProvider)(nil) - _ storage.EventsJournalConfigurer = (*uowProvider)(nil) - _ storage.VersionedHistoryConfigurer = (*uowProvider)(nil) - _ issueops.EventsMaintenanceRunner = (*uowProvider)(nil) - _ uow.IssueLifecycleSource = (*uowProvider)(nil) - _ uow.IssueReaderSource = (*uowProvider)(nil) - _ uow.IssueClaimerSource = (*uowProvider)(nil) - _ uow.MoleculeStepperSource = (*uowProvider)(nil) - _ uow.RelationsSource = (*uowProvider)(nil) - _ uow.EdgeReaderSource = (*uowProvider)(nil) - _ uow.BlockingAnnotatorSource = (*uowProvider)(nil) - _ uow.TreeWalkerSource = (*uowProvider)(nil) - _ uow.GraphCounterSource = (*uowProvider)(nil) - _ uow.BatchGetterSource = (*uowProvider)(nil) - _ uow.CounterSource = (*uowProvider)(nil) - _ uow.ReadyCounterSource = (*uowProvider)(nil) - _ uow.ReadyClaimerSource = (*uowProvider)(nil) - _ uow.QuerierSource = (*uowProvider)(nil) - _ uow.StatsReporterSource = (*uowProvider)(nil) - _ uow.CycleDetectorSource = (*uowProvider)(nil) - _ uow.CommenterSource = (*uowProvider)(nil) - _ uow.BatchCloserSource = (*uowProvider)(nil) - _ uow.BatchCreatorSource = (*uowProvider)(nil) - _ uow.DependencyEditorSource = (*uowProvider)(nil) - _ uow.BatchApplierSource = (*uowProvider)(nil) - _ uow.DeleterSource = (*uowProvider)(nil) - _ uow.SweeperSource = (*uowProvider)(nil) - _ uow.ImporterSource = (*uowProvider)(nil) - _ uow.BootstrapperSource = (*uowProvider)(nil) - _ uow.InitVerifierSource = (*uowProvider)(nil) - _ uow.WorkspaceConfigSource = (*uowProvider)(nil) - _ uow.VersionReconcilerSource = (*uowProvider)(nil) - _ uow.MetadataCASSource = (*uowProvider)(nil) - _ uow.ReleaserSource = (*uowProvider)(nil) - _ uow.LeaseReclaimerSource = (*uowProvider)(nil) - _ uow.HeartbeaterSource = (*uowProvider)(nil) - _ uow.BlockedListerSource = (*uowProvider)(nil) - _ uow.MemoriesSource = (*uowProvider)(nil) - _ uow.EventsJournalCursorSource = (*uowProvider)(nil) -) - -type unitOfWork struct { - uow.UnitOfWork - policy *Store - issue domain.IssueUseCase - deps domain.DependencyUseCase -} - -var _ uow.UnitOfWork = (*unitOfWork)(nil) - -// Unwrap keeps the transaction runner reachable to infrastructure roles such -// as import. The policy only decorates use-case methods, so peeling it does -// not bypass a mutation guard for callers that use the domain surface. -func (u *unitOfWork) Unwrap() uow.UnitOfWork { return u.UnitOfWork } - -func (u *unitOfWork) IssueUseCase() domain.IssueUseCase { - if u.issue == nil { - u.issue = &issueUseCase{ - IssueUseCase: u.UnitOfWork.IssueUseCase(), - deps: u.DependencyUseCase(), - policy: u.policy, - } - } - return u.issue -} - -func (u *unitOfWork) DependencyUseCase() domain.DependencyUseCase { - if u.deps == nil { - u.deps = &dependencyUseCase{ - DependencyUseCase: u.UnitOfWork.DependencyUseCase(), - policy: u.policy, - } - } - return u.deps -} - -type issueUseCase struct { - domain.IssueUseCase - deps domain.DependencyUseCase - policy *Store -} - -func (u *issueUseCase) blockingState(ctx context.Context) (blockingState, error) { - deps, err := u.deps.GetExternalBlockingDependencyRecords(ctx) - if err != nil { - return blockingState{}, err - } - return u.policy.blockingStateFromRecords(ctx, deps) -} - -func (u *issueUseCase) GetReadyWork(ctx context.Context, filter types.WorkFilter) (domain.SearchPage, error) { - state, err := u.blockingState(ctx) - if err != nil { - return domain.SearchPage{}, fmt.Errorf("external dependencies: %w", err) - } - return u.IssueUseCase.GetReadyWork(ctx, withExternalExclusions(filter, state.refsByIssue)) -} - -func (u *issueUseCase) GetReadyWorkWithCounts(ctx context.Context, filter types.WorkFilter) (domain.SearchCountsPage, error) { - state, err := u.blockingState(ctx) - if err != nil { - return domain.SearchCountsPage{}, fmt.Errorf("external dependencies: %w", err) - } - return u.IssueUseCase.GetReadyWorkWithCounts(ctx, withExternalExclusions(filter, state.refsByIssue)) -} - -func (u *issueUseCase) ClaimReadyIssue(ctx context.Context, filter types.WorkFilter, actor string) (domain.ClaimReadyResult, error) { - state, err := u.blockingState(ctx) - if err != nil { - return domain.ClaimReadyResult{}, fmt.Errorf("external dependencies: %w", err) - } - return u.IssueUseCase.ClaimReadyIssue(ctx, withExternalExclusions(filter, state.refsByIssue), actor) -} - -func (u *issueUseCase) GetBlockedIssues(ctx context.Context, filter types.WorkFilter) ([]*types.BlockedIssue, error) { - base, err := u.IssueUseCase.GetBlockedIssues(ctx, unpagedBlockedFilter(filter)) - if err != nil { - return nil, err - } - state, err := u.blockingState(ctx) - if err != nil { - return nil, fmt.Errorf("external dependencies: %w", err) - } - - result := make([]*types.BlockedIssue, 0, len(base)+len(state.refsByIssue)) - byID := make(map[string]bool, len(base)) - for _, item := range base { - if item == nil { - continue - } - clone := *item - clone.BlockedBy = slices.Clone(item.BlockedBy) - for _, ref := range state.refsByIssue[item.ID] { - clone.BlockedBy = appendUnique(clone.BlockedBy, ref) - } - clone.BlockedByCount = len(clone.BlockedBy) - result = append(result, &clone) - byID[item.ID] = true - } - - missing := make([]string, 0, len(state.refsByIssue)) - for id := range state.refsByIssue { - if !byID[id] { - missing = append(missing, id) - } - } - parentDeps := make(map[string][]*types.Dependency) - if filter.ParentID != nil && len(missing) > 0 { - parentDeps, err = u.deps.GetIssueDependencyRecords(ctx, missing) - if err != nil { - return nil, fmt.Errorf("external dependencies: load blocked parent edges: %w", err) - } - wispParentDeps, err := u.deps.GetWispDependencyRecords(ctx, missing) - if err != nil { - return nil, fmt.Errorf("external dependencies: load blocked wisp parent edges: %w", err) - } - for id, deps := range wispParentDeps { - parentDeps[id] = append(parentDeps[id], deps...) - } - } - // GetMany reads both planes with labels hydrated, as the store decorator's - // reads do; GetIssuesByIDs here would leave Labels empty, so the label - // filter below could not judge these rows and they would print unlabelled. - issues, err := getManyChunked(ctx, u.IssueUseCase, missing) - if err != nil { - return nil, fmt.Errorf("external dependencies: load blocked sources: %w", err) - } - for _, issue := range issues { - if issue == nil || issue.Status == types.StatusClosed || issue.Status == types.StatusPinned { - continue - } - if !matchesParentFilter(issue.ID, filter.ParentID, parentDeps) || !matchesLabelFilter(issue.Labels, filter) { - continue - } - refs := slices.Clone(state.refsByIssue[issue.ID]) - result = append(result, &types.BlockedIssue{Issue: *issue, BlockedByCount: len(refs), BlockedBy: refs}) - } - return finishBlockedIssues(result, filter) -} - -func (u *issueUseCase) CloseIssueChecked(ctx context.Context, id string, params domain.CloseIssueParams, actor string, force bool) (domain.CloseIssueResult, error) { - if err := u.guardExternalCheckedClose(ctx, id, force, u.IssueUseCase.GetIssue); err != nil { - return domain.CloseIssueResult{}, err - } - return u.IssueUseCase.CloseIssueChecked(ctx, id, params, actor, force) -} - -func (u *issueUseCase) CloseWispChecked(ctx context.Context, id string, params domain.CloseIssueParams, actor string, force bool) (domain.CloseIssueResult, error) { - if err := u.guardExternalCheckedClose(ctx, id, force, u.IssueUseCase.GetWisp); err != nil { - return domain.CloseIssueResult{}, err - } - return u.IssueUseCase.CloseWispChecked(ctx, id, params, actor, force) -} - -func (u *issueUseCase) guardExternalCheckedClose(ctx context.Context, id string, force bool, get func(context.Context, string) (*types.Issue, error)) error { - err := u.guardExternalClose(ctx, id, force) - if !errors.Is(err, storage.ErrCloseBlocked) { - return err - } - current, readErr := get(ctx, id) - if readErr != nil { - return readErr - } - // The backend owns not-found and idempotent re-close behavior. The - // snapshot must not turn an already-settled close into a refusal. - if current == nil || current.Status == types.StatusClosed { - return nil - } - return err -} - -func (u *issueUseCase) ApplyUpdate(ctx context.Context, id string, spec domain.UpdateSpec, actor string) (*types.Issue, error) { - if spec.Claim || isClosedUpdate(spec.Fields) { - if err := u.guardExternalClose(ctx, id, false); err != nil { - return nil, err - } - } - return u.IssueUseCase.ApplyUpdate(ctx, id, spec, actor) -} - -func (u *issueUseCase) ClaimIssue(ctx context.Context, id, actor string) (domain.ClaimResult, error) { - if err := u.guardExternalClose(ctx, id, false); err != nil { - return domain.ClaimResult{}, err - } - return u.IssueUseCase.ClaimIssue(ctx, id, actor) -} - -func (u *issueUseCase) ClaimWisp(ctx context.Context, id, actor string) (domain.ClaimResult, error) { - if err := u.guardExternalClose(ctx, id, false); err != nil { - return domain.ClaimResult{}, err - } - return u.IssueUseCase.ClaimWisp(ctx, id, actor) -} - -func isClosedUpdate(fields map[string]any) bool { - switch status := fields["status"].(type) { - case string: - return status == string(types.StatusClosed) - case types.Status: - return status == types.StatusClosed - default: - return false - } -} - -func (u *issueUseCase) guardExternalClose(ctx context.Context, id string, force bool) error { - if force { - return nil - } - state, err := u.blockingState(ctx) - if err != nil { - return fmt.Errorf("external dependencies: %w", err) - } - if blockers := state.refsByIssue[id]; len(blockers) > 0 { - return publicops.NewCloseBlockedError(id, blockers) - } - return nil -} - -type dependencyUseCase struct { - domain.DependencyUseCase - policy *Store -} - -// IsBlockedBatch keeps the external blocker invariant on the stored column, -// as the store decorator's IsBlockedBatch does: an id with an unsatisfied -// external blocker is blocked whatever the column says. The served is_blocked -// flag is this read on bd serve's unit-of-work arm, so it decides by the -// edges and resolver GetReadyWork excludes by, read through the same unit of -// work. Unlike Ready it resolves only the refs of the ids the column -// answered: a client asks in chunks, and no chunk may reopen every foreign -// project the workspace names. An id naming no row stays absent. -func (u *dependencyUseCase) IsBlockedBatch(ctx context.Context, ids []string) (map[string]bool, error) { - blocked, err := u.DependencyUseCase.IsBlockedBatch(ctx, ids) - if err != nil || len(blocked) == 0 { - return blocked, err - } - records, err := u.GetExternalBlockingDependencyRecords(ctx) - if err != nil { - return nil, fmt.Errorf("external dependencies: %w", err) - } - answered := make(map[string][]*types.Dependency, len(blocked)) - for id := range blocked { - if deps, ok := records[id]; ok { - answered[id] = deps - } - } - state, err := u.policy.blockingStateFromRecords(ctx, answered) - if err != nil { - return nil, fmt.Errorf("external dependencies: %w", err) - } - for id := range blocked { - if len(state.refsByIssue[id]) > 0 { - blocked[id] = true - } - } - return blocked, nil -} - -func (u *dependencyUseCase) GetDependencyTree(ctx context.Context, rootID string, opts domain.DepTreeOpts) ([]*types.TreeNode, error) { - tree, err := u.DependencyUseCase.GetDependencyTree(ctx, rootID, opts) - if err != nil || opts.Direction == domain.DepDirectionIn || len(tree) == 0 { - return tree, err - } - ids := make([]string, 0, len(tree)) - for _, node := range tree { - if node != nil && !isExternalReference(node.ID) { - ids = append(ids, node.ID) - } - } - deps, err := u.GetIssueDependencyRecords(ctx, ids) - if err != nil { - return nil, fmt.Errorf("external dependencies: load tree edges: %w", err) - } - return u.policy.appendTreeExternalReferences(ctx, tree, deps, opts.MaxDepth, opts.ShowAllPaths) -} diff --git a/internal/storage/externaldeps/uow_decorator_test.go b/internal/storage/externaldeps/uow_decorator_test.go deleted file mode 100644 index 9849e5e18..000000000 --- a/internal/storage/externaldeps/uow_decorator_test.go +++ /dev/null @@ -1,599 +0,0 @@ -package externaldeps - -import ( - "context" - "errors" - "maps" - "slices" - "strconv" - "testing" - "time" - - "github.com/steveyegge/beads/internal/storage" - "github.com/steveyegge/beads/internal/storage/domain" - "github.com/steveyegge/beads/internal/storage/issueops" - "github.com/steveyegge/beads/internal/storage/uow" - "github.com/steveyegge/beads/internal/types" - publicops "github.com/steveyegge/beads/issueops" -) - -type fakeUOWProvider struct { - uow.UnitOfWorkProvider - uw uow.UnitOfWork -} - -func (p *fakeUOWProvider) NewUOW(context.Context) (uow.UnitOfWork, error) { return p.uw, nil } - -type fakeUOW struct { - uow.UnitOfWork - issues domain.IssueUseCase - deps domain.DependencyUseCase -} - -func (u *fakeUOW) IssueUseCase() domain.IssueUseCase { return u.issues } -func (u *fakeUOW) DependencyUseCase() domain.DependencyUseCase { return u.deps } - -// Close lets a role run over the fake: RunTxRead closes each unit of work it -// opens. -func (u *fakeUOW) Close(context.Context) {} - -type fakeIssueUseCase struct { - domain.IssueUseCase - ready []*types.Issue - wisps []*types.Issue - blocked []*types.BlockedIssue - closed []string -} - -func (u *fakeIssueUseCase) GetIssue(_ context.Context, id string) (*types.Issue, error) { - for _, issue := range u.ready { - if issue.ID == id { - return issue, nil - } - } - return nil, nil -} - -func (u *fakeIssueUseCase) GetWisp(_ context.Context, id string) (*types.Issue, error) { - for _, issue := range u.wisps { - if issue.ID == id { - return issue, nil - } - } - return nil, nil -} - -// GetMany answers both planes, as ExecuteGetMany does, each row with the -// labels it was seeded with (GetMany's one hydration). -func (u *fakeIssueUseCase) GetMany(ctx context.Context, request publicops.GetManyRequest) (publicops.GetManyResult, error) { - result := publicops.GetManyResult{Issues: []*publicops.Issue{}, Missing: []string{}} - for _, id := range request.IDs { - issue, err := u.GetIssue(ctx, id) - if err == nil && issue == nil { - issue, err = u.GetWisp(ctx, id) - } - if err != nil { - return publicops.GetManyResult{}, err - } - if issue == nil { - result.Missing = append(result.Missing, id) - continue - } - result.Issues = append(result.Issues, issue) - } - return result, nil -} - -func (u *fakeIssueUseCase) CloseIssueChecked(_ context.Context, id string, _ domain.CloseIssueParams, _ string, _ bool) (domain.CloseIssueResult, error) { - u.closed = append(u.closed, id) - return domain.CloseIssueResult{}, nil -} - -func (u *fakeIssueUseCase) CloseWispChecked(_ context.Context, id string, _ domain.CloseIssueParams, _ string, _ bool) (domain.CloseIssueResult, error) { - u.closed = append(u.closed, id) - return domain.CloseIssueResult{}, nil -} - -func (u *fakeIssueUseCase) ApplyUpdate(_ context.Context, id string, spec domain.UpdateSpec, _ string) (*types.Issue, error) { - u.closed = append(u.closed, id) - status, _ := spec.Fields["status"].(string) - return &types.Issue{ID: id, Status: types.Status(status)}, nil -} - -func (u *fakeIssueUseCase) GetReadyWork(_ context.Context, filter types.WorkFilter) (domain.SearchPage, error) { - items := make([]*types.Issue, 0, len(u.ready)) - for _, issue := range u.ready { - if !slices.Contains(filter.ExcludeIDs, issue.ID) { - items = append(items, issue) - } - } - return domain.SearchPage{Items: items}, nil -} - -func (u *fakeIssueUseCase) GetBlockedIssues(_ context.Context, _ types.WorkFilter) ([]*types.BlockedIssue, error) { - return slices.Clone(u.blocked), nil -} - -type fakeDependencyUseCase struct { - domain.DependencyUseCase - external map[string][]*types.Dependency - records map[string][]*types.Dependency - wispDeps map[string][]*types.Dependency - // isBlocked is the stored is_blocked column, per row; an id it does not - // name has no row. - isBlocked map[string]bool -} - -func (u *fakeDependencyUseCase) GetBlockingInfo(context.Context, []string) (domain.BlockingInfo, error) { - return domain.BlockingInfo{}, nil -} - -func (u *fakeDependencyUseCase) IsBlockedBatch(_ context.Context, ids []string) (map[string]bool, error) { - result := make(map[string]bool, len(ids)) - for _, id := range ids { - if blocked, ok := u.isBlocked[id]; ok { - result[id] = blocked - } - } - return result, nil -} - -func (u *fakeDependencyUseCase) GetExternalBlockingDependencyRecords(context.Context) (map[string][]*types.Dependency, error) { - return u.external, nil -} - -func (u *fakeDependencyUseCase) GetIssueDependencyRecords(_ context.Context, ids []string) (map[string][]*types.Dependency, error) { - result := make(map[string][]*types.Dependency, len(ids)) - for _, id := range ids { - result[id] = u.records[id] - } - return result, nil -} - -func (u *fakeDependencyUseCase) GetWispDependencyRecords(_ context.Context, ids []string) (map[string][]*types.Dependency, error) { - result := make(map[string][]*types.Dependency, len(ids)) - for _, id := range ids { - result[id] = u.wispDeps[id] - } - return result, nil -} - -func TestWrapUOWProviderFiltersProxiedReadyWork(t *testing.T) { - blocked, ready := issue("be-blocked"), issue("be-ready") - inner := &fakeUOW{ - issues: &fakeIssueUseCase{ready: []*types.Issue{blocked, ready}}, - deps: &fakeDependencyUseCase{external: map[string][]*types.Dependency{ - blocked.ID: {externalDep(blocked.ID, "external:remote:payments", types.DepBlocks)}, - }}, - } - provider := WrapUOWProvider(&fakeUOWProvider{uw: inner}, func(ProjectName) (string, bool) { - return "", false - }, nil) - uw, err := provider.NewUOW(t.Context()) - if err != nil { - t.Fatalf("NewUOW: %v", err) - } - got, err := uw.IssueUseCase().GetReadyWork(t.Context(), types.WorkFilter{}) - if err != nil { - t.Fatalf("GetReadyWork: %v", err) - } - if ids := issueIDs(got.Items); !slices.Equal(ids, []string{ready.ID}) { - t.Fatalf("proxied ready IDs = %v, want [%s]", ids, ready.ID) - } -} - -// TestWrapUOWProviderBlockedFlagAgreesWithReadyWork: the stored is_blocked -// column has no external term, so a row whose only blocker is an unsatisfied -// external ref reads false there. bd serve answers the served flag on this -// arm, so the flag must call that row blocked, as GetReadyWork does, without -// naming a row the column did not answer: neither an orphan edge's source nor -// an unasked row. -func TestWrapUOWProviderBlockedFlagAgreesWithReadyWork(t *testing.T) { - blocked, free, elsewhere := issue("be-blocked"), issue("be-free"), issue("be-elsewhere") - const ghost = "be-ghost" - inner := &fakeUOW{ - issues: &fakeIssueUseCase{ready: []*types.Issue{blocked, free, elsewhere}}, - deps: &fakeDependencyUseCase{ - external: map[string][]*types.Dependency{ - blocked.ID: {externalDep(blocked.ID, "external:remote:payments", types.DepBlocks)}, - elsewhere.ID: {externalDep(elsewhere.ID, "external:remote:payments", types.DepBlocks)}, - ghost: {externalDep(ghost, "external:remote:payments", types.DepBlocks)}, - }, - isBlocked: map[string]bool{blocked.ID: false, free.ID: false, elsewhere.ID: false}, - }, - } - provider := WrapUOWProvider(&fakeUOWProvider{uw: inner}, func(ProjectName) (string, bool) { - return "", false - }, nil) - uw, err := provider.NewUOW(t.Context()) - if err != nil { - t.Fatalf("NewUOW: %v", err) - } - ready, err := uw.IssueUseCase().GetReadyWork(t.Context(), types.WorkFilter{}) - if err != nil { - t.Fatalf("GetReadyWork: %v", err) - } - if ids := issueIDs(ready.Items); !slices.Equal(ids, []string{free.ID}) { - t.Fatalf("proxied ready IDs = %v, want [%s]", ids, free.ID) - } - - asked := []string{blocked.ID, free.ID, ghost} - flags, err := uw.DependencyUseCase().IsBlockedBatch(t.Context(), asked) - if err != nil { - t.Fatalf("IsBlockedBatch: %v", err) - } - if want := map[string]bool{blocked.ID: true, free.ID: false}; !maps.Equal(flags, want) { - t.Errorf("IsBlockedBatch = %v, want %v", flags, want) - } - - annotator, err := provider.(uow.BlockingAnnotatorSource).BlockingAnnotator() - if err != nil { - t.Fatalf("BlockingAnnotator: %v", err) - } - result, err := annotator.AnnotateBlocking(t.Context(), publicops.BlockingRequest{IDs: asked, IncludeIsBlocked: true}) - if err != nil { - t.Fatalf("AnnotateBlocking: %v", err) - } - if got, want := blockedFlags(result), map[string]string{blocked.ID: "true", free.ID: "false", ghost: "absent"}; !maps.Equal(got, want) { - t.Errorf("AnnotateBlocking is_blocked = %v, want %v", got, want) - } -} - -// TestWrapUOWProviderBlockedFlagResolvesOnlyTheAskedRefs: a client asks for -// the served flag in chunks, so unlike Ready, which must resolve every -// external ref in the workspace, IsBlockedBatch opens only the foreign -// projects its asked rows name, neither an unasked row's nor an orphan -// edge's. -func TestWrapUOWProviderBlockedFlagResolvesOnlyTheAskedRefs(t *testing.T) { - blocked, free, elsewhere := issue("be-blocked"), issue("be-free"), issue("be-elsewhere") - // shipped's external blocker is SATISFIED: the foreign project's labelled - // index carries a closed provider of the capability. Its record is answered - // and resolved, and it must still read unblocked — the half of the - // predicate a "raise every answered external record" body would break. - shipped := issue("be-shipped") - const ghost = "be-ghost" - inner := &fakeUOW{deps: &fakeDependencyUseCase{ - external: map[string][]*types.Dependency{ - blocked.ID: {externalDep(blocked.ID, "external:remote:invoices", types.DepBlocks)}, - shipped.ID: {externalDep(shipped.ID, "external:remote:payments", types.DepBlocks)}, - elsewhere.ID: {externalDep(elsewhere.ID, "external:other:billing", types.DepBlocks)}, - ghost: {externalDep(ghost, "external:orphan:audit", types.DepBlocks)}, - }, - isBlocked: map[string]bool{blocked.ID: false, free.ID: false, elsewhere.ID: false, shipped.ID: false}, - }} - opens := make(map[string]int) - provider := WrapUOWProvider(&fakeUOWProvider{uw: inner}, - func(project ProjectName) (string, bool) { return "/projects/" + string(project), true }, - func(_ context.Context, path string) (storage.DoltStorage, error) { - opens[path]++ - return &fakeStore{labels: map[string][]*types.Issue{ - "provides:payments": {{ID: "remote-done", Status: types.StatusClosed}}, - }}, nil - }, - ) - uw, err := provider.NewUOW(t.Context()) - if err != nil { - t.Fatalf("NewUOW: %v", err) - } - - for _, tc := range []struct { - asked []string - want map[string]bool - wantOpens map[string]int - }{ - {asked: []string{free.ID, ghost}, want: map[string]bool{free.ID: false}, wantOpens: map[string]int{}}, - { - asked: []string{blocked.ID, free.ID}, - want: map[string]bool{blocked.ID: true, free.ID: false}, - wantOpens: map[string]int{"/projects/remote": 1}, - }, - { - asked: []string{shipped.ID, free.ID}, - want: map[string]bool{shipped.ID: false, free.ID: false}, - wantOpens: map[string]int{"/projects/remote": 1}, - }, - { - asked: []string{blocked.ID, shipped.ID}, - want: map[string]bool{blocked.ID: true, shipped.ID: false}, - wantOpens: map[string]int{"/projects/remote": 1}, - }, - } { - clear(opens) - flags, err := uw.DependencyUseCase().IsBlockedBatch(t.Context(), tc.asked) - if err != nil { - t.Fatalf("IsBlockedBatch(%v): %v", tc.asked, err) - } - if !maps.Equal(flags, tc.want) { - t.Errorf("IsBlockedBatch(%v) = %v, want %v", tc.asked, flags, tc.want) - } - if !maps.Equal(opens, tc.wantOpens) { - t.Errorf("IsBlockedBatch(%v) opened %v, want %v", tc.asked, opens, tc.wantOpens) - } - } -} - -// blockedFlags renders each annotated row's is_blocked flag, "absent" where -// the row carries none. -func blockedFlags(result publicops.BlockingResult) map[string]string { - flags := make(map[string]string, len(result.Items)) - for _, item := range result.Items { - flags[item.ID] = "absent" - if item.IsBlocked != nil { - flags[item.ID] = strconv.FormatBool(*item.IsBlocked) - } - } - return flags -} - -func TestWrapUOWProviderKeepsUnitOfWorkUnwrappable(t *testing.T) { - inner := &fakeUOW{} - provider := WrapUOWProvider(&fakeUOWProvider{uw: inner}, nil, nil) - wrapped, err := provider.NewUOW(t.Context()) - if err != nil { - t.Fatalf("NewUOW: %v", err) - } - - unwrapper, ok := wrapped.(interface{ Unwrap() uow.UnitOfWork }) - if !ok { - t.Fatalf("wrapped UOW %T does not expose Unwrap", wrapped) - } - if got := unwrapper.Unwrap(); got != inner { - t.Fatalf("Unwrap() = %T, want the wrapped UOW", got) - } -} - -func TestWrapUOWProviderPreservesIssueReader(t *testing.T) { - inner := &fakeUOWProvider{uw: &fakeUOW{}} - provider := WrapUOWProvider(inner, nil, nil) - source, ok := provider.(uow.IssueReaderSource) - if !ok { - t.Fatalf("wrapped provider %T does not preserve IssueReaderSource", provider) - } - if reader, err := source.IssueReader(); err != nil || reader == nil { - t.Fatalf("IssueReader() = %T, %v", reader, err) - } - if got := uow.UnwrapProvider(provider); got != inner { - t.Fatalf("UnwrapProvider() = %T, want the wrapped provider", got) - } -} - -func TestWrapUOWProviderRefusesProxiedCheckedClose(t *testing.T) { - blocked := issue("be-blocked") - issues := &fakeIssueUseCase{ready: []*types.Issue{blocked}} - inner := &fakeUOW{ - issues: issues, - deps: &fakeDependencyUseCase{external: map[string][]*types.Dependency{ - blocked.ID: {externalDep(blocked.ID, "external:remote:payments", types.DepBlocks)}, - }}, - } - provider := WrapUOWProvider(&fakeUOWProvider{uw: inner}, func(ProjectName) (string, bool) { - return "", false - }, nil) - uw, err := provider.NewUOW(t.Context()) - if err != nil { - t.Fatalf("NewUOW: %v", err) - } - _, err = uw.IssueUseCase().CloseIssueChecked(t.Context(), blocked.ID, domain.CloseIssueParams{}, "tester", false) - if !errors.Is(err, storage.ErrCloseBlocked) { - t.Fatalf("CloseIssueChecked error = %v, want ErrCloseBlocked", err) - } - assertExternalCloseBlockers(t, err, blocked.ID, "external:remote:payments") - if len(issues.closed) != 0 { - t.Fatalf("inner close calls = %v, want none", issues.closed) - } -} - -func TestWrapUOWProviderFiltersExternalBlockedWorkByParent(t *testing.T) { - parent, child := issue("be-parent"), issue("be-child") - inner := &fakeUOW{ - issues: &fakeIssueUseCase{ready: []*types.Issue{child}}, - deps: &fakeDependencyUseCase{ - external: map[string][]*types.Dependency{ - child.ID: {externalDep(child.ID, "external:remote:payments", types.DepBlocks)}, - }, - records: map[string][]*types.Dependency{ - child.ID: {{IssueID: child.ID, DependsOnID: parent.ID, Type: types.DepParentChild}}, - }, - }, - } - provider := WrapUOWProvider(&fakeUOWProvider{uw: inner}, func(ProjectName) (string, bool) { - return "", false - }, nil) - uw, err := provider.NewUOW(t.Context()) - if err != nil { - t.Fatalf("NewUOW: %v", err) - } - got, err := uw.IssueUseCase().GetBlockedIssues(t.Context(), types.WorkFilter{ParentID: &parent.ID}) - if err != nil { - t.Fatalf("GetBlockedIssues: %v", err) - } - if len(got) != 1 || got[0].ID != child.ID { - t.Fatalf("blocked issues = %v, want [%s]", got, child.ID) - } -} - -func TestWrapUOWProviderGuardsDoneUpdatesAndWispCloses(t *testing.T) { - durable, wisp := issue("be-durable"), issue("be-wisp") - issues := &fakeIssueUseCase{ready: []*types.Issue{durable}, wisps: []*types.Issue{wisp}} - inner := &fakeUOW{ - issues: issues, - deps: &fakeDependencyUseCase{external: map[string][]*types.Dependency{ - durable.ID: {externalDep(durable.ID, "external:remote:payments", types.DepBlocks)}, - wisp.ID: {externalDep(wisp.ID, "external:remote:payments", types.DepBlocks)}, - }}, - } - provider := WrapUOWProvider(&fakeUOWProvider{uw: inner}, func(ProjectName) (string, bool) { - return "", false - }, nil) - uw, err := provider.NewUOW(t.Context()) - if err != nil { - t.Fatalf("NewUOW: %v", err) - } - issueUC := uw.IssueUseCase() - for _, id := range []string{durable.ID, wisp.ID} { - if _, err := issueUC.ApplyUpdate(t.Context(), id, domain.UpdateSpec{Claim: true, Fields: map[string]any{"notes": "must not be written"}}, "tester"); !errors.Is(err, storage.ErrCloseBlocked) { - t.Fatalf("ApplyUpdate claim %s: %v, want ErrCloseBlocked", id, err) - } - } - if _, err := issueUC.ClaimIssue(t.Context(), durable.ID, "tester"); !errors.Is(err, storage.ErrCloseBlocked) { - t.Fatalf("ClaimIssue error = %v, want ErrCloseBlocked", err) - } - if _, err := issueUC.ClaimWisp(t.Context(), wisp.ID, "tester"); !errors.Is(err, storage.ErrCloseBlocked) { - t.Fatalf("ClaimWisp error = %v, want ErrCloseBlocked", err) - } - if _, err := issueUC.ApplyUpdate(t.Context(), durable.ID, domain.UpdateSpec{Fields: map[string]any{"status": string(types.StatusClosed)}}, "tester"); !errors.Is(err, storage.ErrCloseBlocked) { - t.Fatalf("ApplyUpdate error = %v, want ErrCloseBlocked", err) - } - if _, err := issueUC.CloseWispChecked(t.Context(), wisp.ID, domain.CloseIssueParams{}, "tester", false); !errors.Is(err, storage.ErrCloseBlocked) { - t.Fatalf("CloseWispChecked error = %v, want ErrCloseBlocked", err) - } - if len(issues.closed) != 0 { - t.Fatalf("inner mutation calls = %v, want none", issues.closed) - } - blocked, err := issueUC.GetBlockedIssues(t.Context(), types.WorkFilter{}) - if err != nil { - t.Fatalf("GetBlockedIssues: %v", err) - } - if ids := blockedIssueIDs(blocked); !slices.Equal(ids, []string{durable.ID, wisp.ID}) { - t.Fatalf("blocked IDs = %v, want [%s %s]", ids, durable.ID, wisp.ID) - } - durable.Status, wisp.Status = types.StatusClosed, types.StatusClosed - if _, err := issueUC.CloseIssueChecked(t.Context(), durable.ID, domain.CloseIssueParams{}, "tester", false); err != nil { - t.Fatalf("durable re-close: %v", err) - } - if _, err := issueUC.CloseWispChecked(t.Context(), wisp.ID, domain.CloseIssueParams{}, "tester", false); err != nil { - t.Fatalf("wisp re-close: %v", err) - } - if !slices.Equal(issues.closed, []string{durable.ID, wisp.ID}) { - t.Fatalf("re-closes did not reach the backend: %v", issues.closed) - } -} - -func TestWrapUOWProviderPaginatesCombinedExternalBlockedWork(t *testing.T) { - local, external := issue("be-local"), issue("be-external") - createdAt := time.Date(2026, time.August, 2, 0, 0, 0, 0, time.UTC) - local.Priority, external.Priority = 0, 0 - local.CreatedAt, external.CreatedAt = createdAt, createdAt - inner := &fakeUOW{ - issues: &fakeIssueUseCase{ - ready: []*types.Issue{external}, - blocked: []*types.BlockedIssue{{Issue: *local, BlockedBy: []string{"be-local-blocker"}, BlockedByCount: 1}}, - }, - deps: &fakeDependencyUseCase{external: map[string][]*types.Dependency{ - external.ID: {externalDep(external.ID, "external:remote:payments", types.DepBlocks)}, - }}, - } - provider := WrapUOWProvider(&fakeUOWProvider{uw: inner}, func(ProjectName) (string, bool) { - return "", false - }, nil) - uw, err := provider.NewUOW(t.Context()) - if err != nil { - t.Fatalf("NewUOW: %v", err) - } - first, err := uw.IssueUseCase().GetBlockedIssues(t.Context(), types.WorkFilter{Limit: 1}) - if err != nil { - t.Fatalf("GetBlockedIssues first page: %v", err) - } - if ids := blockedIssueIDs(first); !slices.Equal(ids, []string{external.ID}) { - t.Fatalf("first page IDs = %v, want [%s]", ids, external.ID) - } - second, err := uw.IssueUseCase().GetBlockedIssues(t.Context(), types.WorkFilter{Offset: 1, Limit: 1}) - if err != nil { - t.Fatalf("GetBlockedIssues second page: %v", err) - } - if ids := blockedIssueIDs(second); !slices.Equal(ids, []string{local.ID}) { - t.Fatalf("second page IDs = %v, want [%s]", ids, local.ID) - } - _, err = uw.IssueUseCase().GetBlockedIssues(t.Context(), types.WorkFilter{MaxRows: 1, MaxRowsSource: "--max-rows"}) - var capErr *issueops.ErrTooManyRows - if !errors.As(err, &capErr) { - t.Fatalf("GetBlockedIssues MaxRows error = %v, want ErrTooManyRows", err) - } -} - -// fakeConfigurableProvider is a provider that implements BOTH optional -// activation configurers and records what reached it. -type fakeConfigurableProvider struct { - uow.UnitOfWorkProvider - journal bool - version bool -} - -func (p *fakeConfigurableProvider) SetEventsJournalEnabled(enabled bool) { p.journal = enabled } - -func (p *fakeConfigurableProvider) SetVersionedHistoryEnabled(enabled bool) { p.version = enabled } - -// TestWrapUOWProviderForwardsActivationConfigurers pins the whole optional -// activation family through this wrapper, not one member of it. -// -// uowProvider is the OUTERMOST provider on both real chains (cmd/bd/main.go -// and cmd/bd/serve.go each wrap everything else in -// wireExternalDependencyUOWProvider), and activation discovers its target by -// type-asserting that outermost value. A configurer the wrapper does not -// implement therefore fails the assertion on exactly the chains that matter, -// and any forwarder further in is unreachable — with no error and no log, -// because a missing optional-interface method is invisible to the compiler. -// The table is the point: the next configurer added to this family gets one -// row here and is covered, rather than shipping the same silent gap. -func TestWrapUOWProviderForwardsActivationConfigurers(t *testing.T) { - cases := []struct { - name string - enable func(uow.UnitOfWorkProvider) bool - got func(*fakeConfigurableProvider) bool - }{ - { - name: "EventsJournalConfigurer", - enable: func(p uow.UnitOfWorkProvider) bool { - configurer, ok := p.(storage.EventsJournalConfigurer) - if ok { - configurer.SetEventsJournalEnabled(true) - } - return ok - }, - got: func(p *fakeConfigurableProvider) bool { return p.journal }, - }, - { - name: "VersionedHistoryConfigurer", - enable: func(p uow.UnitOfWorkProvider) bool { - configurer, ok := p.(storage.VersionedHistoryConfigurer) - if ok { - configurer.SetVersionedHistoryEnabled(true) - } - return ok - }, - got: func(p *fakeConfigurableProvider) bool { return p.version }, - }, - } - for _, tc := range cases { - t.Run(tc.name, func(t *testing.T) { - inner := &fakeConfigurableProvider{} - provider := WrapUOWProvider(inner, nil, nil) - if !tc.enable(provider) { - t.Fatalf("wrapped provider %T does not implement %s — activation type-asserts the OUTERMOST provider, so it would silently enable nothing", provider, tc.name) - } - if !tc.got(inner) { - t.Fatalf("%s did not reach the inner provider through the wrapper", tc.name) - } - }) - } -} - -// TestWrapUOWProviderToleratesInnerWithoutConfigurers pins the other half of -// the forwarding contract: an inner provider that implements neither -// configurer must be a silent no-op, not a panic. The wrapper still satisfies -// both interfaces (that is what makes activation reach it at all), so the -// inner type assertion is the only thing standing between a non-configurable -// store and a nil-method call. -func TestWrapUOWProviderToleratesInnerWithoutConfigurers(t *testing.T) { - provider := WrapUOWProvider(&fakeUOWProvider{uw: &fakeUOW{}}, nil, nil) - journal, ok := provider.(storage.EventsJournalConfigurer) - if !ok { - t.Fatalf("wrapped provider %T does not implement storage.EventsJournalConfigurer", provider) - } - version, ok := provider.(storage.VersionedHistoryConfigurer) - if !ok { - t.Fatalf("wrapped provider %T does not implement storage.VersionedHistoryConfigurer", provider) - } - journal.SetEventsJournalEnabled(true) - version.SetVersionedHistoryEnabled(true) -} diff --git a/internal/storage/fs/BUILD.bazel b/internal/storage/fs/BUILD.bazel deleted file mode 100644 index 48994986c..000000000 --- a/internal/storage/fs/BUILD.bazel +++ /dev/null @@ -1,86 +0,0 @@ -load("@rules_go//go:def.bzl", "go_library") - -go_library( - name = "fs", - srcs = ["provider.go"], - importpath = "github.com/steveyegge/beads/internal/storage/fs", - visibility = ["//:__subpackages__"], - deps = [ - "//internal/storage/domain", - "//internal/storage/domain/fs", - ], -) - -# --- begin go_srcs (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) --- - -filegroup( - name = "go_srcs", - srcs = glob( - ["**/*.go"], - allow_empty = True, - ), - visibility = ["//:__subpackages__"], -) - -# --- end go_srcs --- - -# --- begin repo_files (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) --- - -filegroup( - name = "repo_doc_files", - srcs = glob( - [ - "**/*.md", - "**/*.mdx", - ], - allow_empty = True, - exclude = ["**/node_modules/**"], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_go_srcs", - srcs = glob( - ["**/*.go"], - allow_empty = True, - exclude = [ - "**/*_test.go", - "**/node_modules/**", - ], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_go_test_srcs", - srcs = glob( - ["**/*_test.go"], - allow_empty = True, - exclude = ["**/node_modules/**"], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_other_files", - srcs = glob( - ["**"], - exclude = [ - "**/*.db", - "**/*.exe", - "**/*.go", - "**/*.md", - "**/*.mdx", - "**/*.out", - "**/*.prof", - "**/*.pyc", - "**/*.test", - "**/__pycache__/**", - "**/node_modules/**", - ], - ), - visibility = ["//:__subpackages__"], -) - -# --- end repo_files --- diff --git a/internal/storage/git/BUILD.bazel b/internal/storage/git/BUILD.bazel deleted file mode 100644 index 5ede35634..000000000 --- a/internal/storage/git/BUILD.bazel +++ /dev/null @@ -1,86 +0,0 @@ -load("@rules_go//go:def.bzl", "go_library") - -go_library( - name = "git", - srcs = ["provider.go"], - importpath = "github.com/steveyegge/beads/internal/storage/git", - visibility = ["//:__subpackages__"], - deps = [ - "//internal/storage/domain", - "//internal/storage/domain/git", - ], -) - -# --- begin go_srcs (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) --- - -filegroup( - name = "go_srcs", - srcs = glob( - ["**/*.go"], - allow_empty = True, - ), - visibility = ["//:__subpackages__"], -) - -# --- end go_srcs --- - -# --- begin repo_files (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) --- - -filegroup( - name = "repo_doc_files", - srcs = glob( - [ - "**/*.md", - "**/*.mdx", - ], - allow_empty = True, - exclude = ["**/node_modules/**"], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_go_srcs", - srcs = glob( - ["**/*.go"], - allow_empty = True, - exclude = [ - "**/*_test.go", - "**/node_modules/**", - ], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_go_test_srcs", - srcs = glob( - ["**/*_test.go"], - allow_empty = True, - exclude = ["**/node_modules/**"], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_other_files", - srcs = glob( - ["**"], - exclude = [ - "**/*.db", - "**/*.exe", - "**/*.go", - "**/*.md", - "**/*.mdx", - "**/*.out", - "**/*.prof", - "**/*.pyc", - "**/*.test", - "**/__pycache__/**", - "**/node_modules/**", - ], - ), - visibility = ["//:__subpackages__"], -) - -# --- end repo_files --- diff --git a/internal/storage/historyoffequiv/BUILD.bazel b/internal/storage/historyoffequiv/BUILD.bazel deleted file mode 100644 index c9e4b9a8a..000000000 --- a/internal/storage/historyoffequiv/BUILD.bazel +++ /dev/null @@ -1,96 +0,0 @@ -load("@rules_go//go:def.bzl", "go_library") - -go_library( - name = "historyoffequiv", - srcs = [ - "dump.go", - "historyoffequiv.go", - "scenario.go", - ], - embedsrcs = [ - "golden/embedded.json", - "golden/server.json", - "golden/uow.json", - ], - importpath = "github.com/steveyegge/beads/internal/storage/historyoffequiv", - visibility = ["//:__subpackages__"], - deps = [ - "//internal/storage", - "//internal/types", - "//issueops", - ], -) - -# --- begin go_srcs (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) --- - -filegroup( - name = "go_srcs", - srcs = glob( - ["**/*.go"], - allow_empty = True, - ), - visibility = ["//:__subpackages__"], -) - -# --- end go_srcs --- - -# --- begin repo_files (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) --- - -filegroup( - name = "repo_doc_files", - srcs = glob( - [ - "**/*.md", - "**/*.mdx", - ], - allow_empty = True, - exclude = ["**/node_modules/**"], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_go_srcs", - srcs = glob( - ["**/*.go"], - allow_empty = True, - exclude = [ - "**/*_test.go", - "**/node_modules/**", - ], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_go_test_srcs", - srcs = glob( - ["**/*_test.go"], - allow_empty = True, - exclude = ["**/node_modules/**"], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_other_files", - srcs = glob( - ["**"], - exclude = [ - "**/*.db", - "**/*.exe", - "**/*.go", - "**/*.md", - "**/*.mdx", - "**/*.out", - "**/*.prof", - "**/*.pyc", - "**/*.test", - "**/__pycache__/**", - "**/node_modules/**", - ], - ), - visibility = ["//:__subpackages__"], -) - -# --- end repo_files --- diff --git a/internal/storage/historyoffequiv/dump.go b/internal/storage/historyoffequiv/dump.go deleted file mode 100644 index b5dbb36e9..000000000 --- a/internal/storage/historyoffequiv/dump.go +++ /dev/null @@ -1,254 +0,0 @@ -package historyoffequiv - -import ( - "context" - "encoding/json" - "fmt" - "regexp" - "strconv" - "strings" - "testing" - "time" -) - -// Every column of every base table is dumped: there is no list of tables to -// keep up to date, so a table a later change starts writing is compared the -// moment it exists. Each column is cast to text in SQL by its declared type -// (datetimes to a fixed format with microseconds, JSON to its text, binary to -// hex), and the text is then normalized only where a value is not a function -// of the scenario: -// -// - A datetime inside the run's window (the instants the scenario's writes -// stamp from the wall clock) reads "", and so does an RFC 3339 -// instant inside the window embedded in text (an event's snapshot of the -// row it changed). NULL stays NULL, and an instant the scenario supplied -// (fixedAt, decades outside the window) stays verbatim, so a write that -// starts or stops stamping a column, or that overwrites a supplied -// instant, still changes the digest. -// - A JSON value is re-serialized with sorted keys, so two engines' (or two -// versions') spacing and key order do not read as a difference. -// - The columns in volatileColumns hold values minted from randomness or -// derived from a wall-clock instant. Each reads "" under its -// rule, which keeps NULL, the empty string and the "none" value apart -// from a minted one. -// -// Rows are compared as a sorted multiset per table. - -type volatility int - -const ( - // volatileAlways masks every non-empty value. - volatileAlways volatility = iota - // volatileNonZero masks every value but "0", the column's "none". - volatileNonZero - // volatileUUID masks a UUID-shaped value and keeps any other verbatim. - volatileUUID -) - -// volatileColumns are the columns whose values differ between two runs of the -// same scenario on the same code, each with why. They were found by running -// the scenario twice on each leg and diffing the dumps. -var volatileColumns = map[string]map[string]volatility{ - // An event's id is a digest over its content and its wall-clock - // created_at (issueops.InsertDerivedEvent). - "events": {"id": volatileAlways}, - "wisp_events": {"id": volatileAlways}, - // A live comment's id is the same kind of digest; an imported comment - // keeps the id it arrived with, which stays verbatim. - "comments": {"id": volatileUUID}, - "wisp_comments": {"id": volatileUUID}, - // row_lock is a fresh random CAS token on every write; 0 means none. - "issues": {"row_lock": volatileNonZero}, - "wisps": {"row_lock": volatileNonZero}, - // Per-session coordination tokens (dependency-coordination/v1/..., - // issue-create/v1/...) are random; their keys are digests of the ids - // involved and stay verbatim. - "local_metadata": {"value": volatileAlways}, -} - -var ( - uuidRe = regexp.MustCompile(`^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$`) - rfc3339Re = regexp.MustCompile(`\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-]\d{2}:\d{2})`) -) - -func maskVolatile(table, column string, v string) (string, bool) { - rule, ok := volatileColumns[table][column] - if !ok || v == "" { - return v, false - } - switch rule { - case volatileNonZero: - if v == "0" { - return v, false - } - case volatileUUID: - if !uuidRe.MatchString(v) { - return v, false - } - } - return "", true -} - -// Two pseudo-tables carry the Dolt commit graph, compared like any table: the -// messages of the commits the scenario made, in order (a change to what a -// write commits, or whether it commits at all, shows up there), and the -// working set it left behind (a change to what a write stages shows up -// there). -const ( - doltLogTable = "@dolt_log" - doltStatusTable = "@dolt_status" -) - -func doltLogCount(t *testing.T, ctx context.Context, q Query) int { - t.Helper() - rows, err := q(ctx, "SELECT CAST(COUNT(*) AS CHAR) FROM dolt_log") - if err != nil { - t.Fatalf("count dolt_log: %v", err) - } - n, err := strconv.Atoi(*rows[0][0]) - if err != nil { - t.Fatalf("count dolt_log: %v", err) - } - return n -} - -func dumpAll(t *testing.T, ctx context.Context, q Query, windowStart, windowEnd time.Time, logBefore int) dump { - t.Helper() - tableRows, err := q(ctx, `SELECT table_name FROM information_schema.tables - WHERE table_schema = DATABASE() AND table_type = 'BASE TABLE' ORDER BY table_name`) - if err != nil { - t.Fatalf("list tables: %v", err) - } - colRows, err := q(ctx, `SELECT table_name, column_name, data_type FROM information_schema.columns - WHERE table_schema = DATABASE() ORDER BY table_name, ordinal_position`) - if err != nil { - t.Fatalf("list columns: %v", err) - } - type column struct{ name, dataType string } - columns := map[string][]column{} - for _, r := range colRows { - columns[*r[0]] = append(columns[*r[0]], column{name: *r[1], dataType: strings.ToLower(*r[2])}) - } - out := dump{Tables: map[string]tableDump{}} - for _, r := range tableRows { - table := *r[0] - if strings.HasPrefix(table, "dolt_") { - continue // Dolt's own system tables; the commit graph is read below. - } - cols := columns[table] - if len(cols) == 0 { - t.Fatalf("table %s has no columns in information_schema.columns", table) - } - exprs := make([]string, len(cols)) - names := make([]string, len(cols)) - for i, c := range cols { - names[i] = c.name - exprs[i] = textExpr(c.name, c.dataType) - } - rows, err := q(ctx, fmt.Sprintf("SELECT %s FROM `%s`", strings.Join(exprs, ", "), table)) - if err != nil { - t.Fatalf("dump %s: %v", table, err) - } - for _, row := range rows { - for i, c := range cols { - row[i] = normalize(t, table, c.name, c.dataType, row[i], windowStart, windowEnd) - } - } - sortRows(rows) - out.Tables[table] = tableDump{Columns: names, Rows: rows} - } - - // The scenario's commits, newest first as dolt_log lists them, numbered so - // the sorted comparison keeps their order. The committer identity comes - // from the environment (CI sets its own), so only the message is kept. - logRows, err := q(ctx, "SELECT message FROM dolt_log") - if err != nil { - t.Fatalf("read dolt_log: %v", err) - } - made := len(logRows) - logBefore - if made < 0 { - t.Fatalf("dolt_log shrank from %d to %d commits during the scenario", logBefore, len(logRows)) - } - var log [][]*string - for i, r := range logRows[:made] { - seq := fmt.Sprintf("%04d", made-i) - log = append(log, []*string{&seq, r[0]}) - } - sortRows(log) - out.Tables[doltLogTable] = tableDump{Columns: []string{"seq", "message"}, Rows: log} - - status, err := q(ctx, "SELECT table_name, CAST(staged AS CHAR), status FROM dolt_status") - if err != nil { - t.Fatalf("read dolt_status: %v", err) - } - sortRows(status) - out.Tables[doltStatusTable] = tableDump{Columns: []string{"table_name", "staged", "status"}, Rows: status} - return out -} - -func textExpr(name, dataType string) string { - c := "`" + name + "`" - switch dataType { - case "datetime", "timestamp": - return fmt.Sprintf("DATE_FORMAT(%s, '%%Y-%%m-%%d %%H:%%i:%%s.%%f')", c) - case "date": - return fmt.Sprintf("DATE_FORMAT(%s, '%%Y-%%m-%%d')", c) - case "blob", "tinyblob", "mediumblob", "longblob", "binary", "varbinary": - return fmt.Sprintf("HEX(%s)", c) - default: - return fmt.Sprintf("CAST(%s AS CHAR)", c) - } -} - -const datetimeLayout = "2006-01-02 15:04:05.000000" - -func normalize(t *testing.T, table, column, dataType string, v *string, windowStart, windowEnd time.Time) *string { - t.Helper() - if v == nil { - return nil - } - if masked, ok := maskVolatile(table, column, *v); ok { - return &masked - } - switch dataType { - case "datetime", "timestamp": - at, err := time.Parse(datetimeLayout, *v) - if err != nil { - t.Fatalf("%s.%s: datetime %q: %v", table, column, *v, err) - } - if !at.Before(windowStart) && !at.After(windowEnd) { - s := "" - return &s - } - case "json": - var parsed any - dec := json.NewDecoder(strings.NewReader(*v)) - dec.UseNumber() // keep integers past 2^53 exact - if err := dec.Decode(&parsed); err != nil { - return v - } - b, err := json.Marshal(parsed) - if err != nil { - t.Fatalf("%s.%s: re-marshal JSON: %v", table, column, err) - } - s := string(b) - return maskInstants(&s, windowStart, windowEnd) - } - return maskInstants(v, windowStart, windowEnd) -} - -// maskInstants replaces each RFC 3339 instant inside the run's window that v -// embeds with "". -func maskInstants(v *string, windowStart, windowEnd time.Time) *string { - if !rfc3339Re.MatchString(*v) { - return v - } - s := rfc3339Re.ReplaceAllStringFunc(*v, func(m string) string { - at, err := time.Parse(time.RFC3339Nano, m) - if err != nil || at.Before(windowStart) || at.After(windowEnd) { - return m - } - return "" - }) - return &s -} diff --git a/internal/storage/historyoffequiv/golden/embedded.json b/internal/storage/historyoffequiv/golden/embedded.json deleted file mode 100644 index 765849833..000000000 --- a/internal/storage/historyoffequiv/golden/embedded.json +++ /dev/null @@ -1,539 +0,0 @@ -{ - "recorded_from": "4a67d0d028", - "steps": [ - "create a", - "create b blocked by a", - "create epic", - "create epic.1 under epic", - "create wisp w1", - "create no-history nh", - "create c", - "create d", - "create e", - "update a fields labels metadata", - "update a no-op", - "update w1 to persistent", - "update c to ephemeral", - "update d parent epic", - "add dependencies", - "remove dependency b-\u003ea", - "comment on a and nh", - "claim heartbeat release b", - "claim e and reclaim no lease", - "metadata compare-and-set on a", - "close reopen close epic.1", - "close batch b and refused epic", - "force close blocked no-history nh", - "create batch", - "apply batch", - "defer f1 into the past and wake it", - "delete f2", - "create wisps x y z", - "update wisp x fields labels metadata", - "update wisp x no-op", - "claim and heartbeat refuse wisps", - "reclaim sweep scoped to in-progress wisp x", - "release wisp x", - "add and remove wisp dependencies", - "comment on wisp x", - "metadata compare-and-set on wisp x", - "close reopen wisp x", - "apply batch on the wisps plane", - "close batch wisps x and nh", - "defer wisp z into the past and wake it", - "delete wisp y", - "store create s1", - "store create issues s2 s3", - "store import upsert s1 and new s4", - "store update s2", - "store update s2 no-op", - "store labels", - "store dependencies", - "store comment", - "store claim s3", - "store close and reopen s2", - "store promote wisp sw", - "store transaction", - "store delete s3" - ], - "tables": { - "@dolt_log": { - "columns": [ - "seq", - "message" - ], - "rows": 28, - "sha256": "df2b8cf059f646557d21be3a0eca54f8561525f3d7e7ee714cfb65962d31012f" - }, - "@dolt_status": { - "columns": [ - "table_name", - "staged", - "status" - ], - "rows": 3, - "sha256": "ee3e710041f54216c2ae7e45bc7774cfcaa42aff3f4d96f5996fc4a7cc419f31" - }, - "bd_events_journal": { - "columns": [ - "seq", - "ts", - "op", - "issue_id", - "issue_json", - "dep_json", - "comment_json", - "actor" - ], - "rows": 0, - "sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - }, - "bd_events_seq": { - "columns": [ - "id", - "next_seq" - ], - "rows": 1, - "sha256": "13751b39fe07c3673f53b4f2c5ef7f2a28b26c0daed72eb7332c93c1fe520618" - }, - "child_counters": { - "columns": [ - "parent_id", - "last_child" - ], - "rows": 1, - "sha256": "753b8feea7bdc0fc74468faa597a32984edc501e9873e9f4ee1df7b59d56d0a6" - }, - "comments": { - "columns": [ - "id", - "issue_id", - "author", - "text", - "created_at" - ], - "rows": 2, - "sha256": "d5870471fe72b8e50fe898978f24a80a23e07750ab538d47353acfabfb0e9aed" - }, - "compaction_snapshots": { - "columns": [ - "id", - "issue_id", - "compaction_level", - "snapshot_json", - "created_at" - ], - "rows": 0, - "sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - }, - "config": { - "columns": [ - "key", - "value" - ], - "rows": 10, - "sha256": "671b33231eb47e4ad897051f4e8d86d3cca88685eeaa9ced537fe24c927fdea9" - }, - "custom_statuses": { - "columns": [ - "name", - "category" - ], - "rows": 0, - "sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - }, - "custom_types": { - "columns": [ - "name" - ], - "rows": 0, - "sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - }, - "dependencies": { - "columns": [ - "id", - "issue_id", - "type", - "created_at", - "created_by", - "metadata", - "thread_id", - "depends_on_issue_id", - "depends_on_wisp_id", - "depends_on_external" - ], - "rows": 6, - "sha256": "90f7dcd9895000df82d3b062d71a4c8ca81e7d5c9912f60d76515cb3eeee40d6" - }, - "events": { - "columns": [ - "issue_id", - "event_type", - "actor", - "old_value", - "new_value", - "comment", - "created_at", - "id" - ], - "rows": 51, - "sha256": "898f31694ce12dcd693f374634aea358b68239f6954fdcaae3b6342c1242f2bd" - }, - "federation_peers": { - "columns": [ - "name", - "remote_url", - "username", - "password_encrypted", - "sovereignty", - "last_sync", - "created_at", - "updated_at" - ], - "rows": 0, - "sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - }, - "ignored_schema_migrations": { - "columns": [ - "version", - "applied_at", - "content_hash" - ], - "rows": 27, - "sha256": "7164db2c129cbaafbd9c426d27e809d043cafc77e8aef1f68d1760905a685acb" - }, - "interactions": { - "columns": [ - "id", - "kind", - "created_at", - "actor", - "issue_id", - "model", - "prompt", - "response", - "error", - "tool_name", - "exit_code", - "parent_id", - "label", - "reason", - "extra" - ], - "rows": 0, - "sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - }, - "issue_counter": { - "columns": [ - "prefix", - "last_id" - ], - "rows": 0, - "sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - }, - "issue_snapshots": { - "columns": [ - "id", - "issue_id", - "snapshot_time", - "compaction_level", - "original_size", - "compressed_size", - "original_content", - "archived_events" - ], - "rows": 0, - "sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - }, - "issue_versions": { - "columns": [ - "issue_id", - "revision", - "epoch", - "durable_state", - "change_actor", - "change_agent", - "change_message", - "change_at", - "removed_at", - "removed_reason", - "attribution_status" - ], - "rows": 0, - "sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - }, - "issues": { - "columns": [ - "id", - "content_hash", - "title", - "description", - "design", - "acceptance_criteria", - "notes", - "status", - "priority", - "issue_type", - "assignee", - "estimated_minutes", - "created_at", - "created_by", - "owner", - "updated_at", - "closed_at", - "closed_by_session", - "external_ref", - "spec_id", - "compaction_level", - "compacted_at", - "compacted_at_commit", - "original_size", - "sender", - "ephemeral", - "wisp_type", - "pinned", - "is_template", - "mol_type", - "work_type", - "source_system", - "metadata", - "source_repo", - "close_reason", - "event_kind", - "actor", - "target", - "payload", - "await_type", - "await_id", - "timeout_ns", - "waiters", - "hook_bead", - "role_bead", - "agent_state", - "last_activity", - "role_type", - "rig", - "due_at", - "defer_until", - "no_history", - "started_at", - "is_blocked", - "row_lock", - "storage_class", - "current_revision" - ], - "rows": 15, - "sha256": "1f81df40f4c4d458f20cfb6535ca13468fa03fbd10276c44752977813a4b7e20" - }, - "labels": { - "columns": [ - "issue_id", - "label" - ], - "rows": 8, - "sha256": "0102293e23eec630b67542f93c5d8da6eef5ca627cff8662970e7cb211b013ef" - }, - "leases": { - "columns": [ - "issue_id", - "holder", - "granted_at", - "lease_expires_at", - "heartbeat_at", - "granted_node" - ], - "rows": 1, - "sha256": "1eca894450e8e801850ed2cbf1c746775ad50868acaaf17e033a5da1e4be10aa" - }, - "local_metadata": { - "columns": [ - "key", - "value" - ], - "rows": 31, - "sha256": "c1d1cbc41ca9968c5c7dd380609c28126c4024868dbade26575ce105b8931fb3" - }, - "metadata": { - "columns": [ - "key", - "value" - ], - "rows": 0, - "sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - }, - "provenance_events": { - "columns": [ - "id", - "issue_id", - "kind", - "actor", - "ref", - "ref_kind", - "payload", - "source", - "occurred_at", - "created_at" - ], - "rows": 0, - "sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - }, - "repo_mtimes": { - "columns": [ - "repo_path", - "jsonl_path", - "mtime_ns", - "last_checked" - ], - "rows": 0, - "sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - }, - "routes": { - "columns": [ - "prefix", - "path", - "created_at", - "updated_at" - ], - "rows": 0, - "sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - }, - "schema_migrations": { - "columns": [ - "version", - "content_hash" - ], - "rows": 69, - "sha256": "1d7a6ae809f330f8b4c3516af50ce7604c1db3cd4c1177af3e5c2f1991d74e14" - }, - "store_epoch": { - "columns": [ - "id", - "epoch", - "bumped_at", - "bumped_reason" - ], - "rows": 0, - "sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - }, - "wisp_child_counters": { - "columns": [ - "parent_id", - "last_child" - ], - "rows": 0, - "sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - }, - "wisp_comments": { - "columns": [ - "id", - "issue_id", - "author", - "text", - "created_at" - ], - "rows": 2, - "sha256": "3c5f1e3eac9ff32fb1158c4421909ddd49c9131edff756c4a0e184114da93350" - }, - "wisp_dependencies": { - "columns": [ - "id", - "issue_id", - "depends_on_issue_id", - "depends_on_wisp_id", - "depends_on_external", - "type", - "created_at", - "created_by", - "metadata", - "thread_id" - ], - "rows": 2, - "sha256": "80f14f79545a236513ea88f0e873f3dd477f7763aaa8cbfaa8f659a517c2a909" - }, - "wisp_events": { - "columns": [ - "id", - "issue_id", - "event_type", - "actor", - "old_value", - "new_value", - "comment", - "created_at" - ], - "rows": 25, - "sha256": "1ee25446153c51b39c4de60d7f03bb56172a84e72d28df2dd44ff79aa5fc3b23" - }, - "wisp_labels": { - "columns": [ - "issue_id", - "label" - ], - "rows": 4, - "sha256": "f9c2698a90ceb3bf0ee8c4034af2845b54d65cc7bdd7638965a4682ecd104c37" - }, - "wisps": { - "columns": [ - "id", - "content_hash", - "title", - "description", - "design", - "acceptance_criteria", - "notes", - "status", - "priority", - "issue_type", - "assignee", - "estimated_minutes", - "created_at", - "created_by", - "owner", - "updated_at", - "closed_at", - "closed_by_session", - "external_ref", - "spec_id", - "compaction_level", - "compacted_at", - "compacted_at_commit", - "original_size", - "sender", - "ephemeral", - "wisp_type", - "pinned", - "is_template", - "mol_type", - "work_type", - "source_system", - "metadata", - "source_repo", - "close_reason", - "event_kind", - "actor", - "target", - "payload", - "await_type", - "await_id", - "timeout_ns", - "waiters", - "hook_bead", - "role_bead", - "agent_state", - "last_activity", - "role_type", - "rig", - "due_at", - "defer_until", - "no_history", - "started_at", - "row_lock", - "storage_class", - "current_revision", - "is_blocked" - ], - "rows": 6, - "sha256": "f28b157579dedd4373778dc6050b2d1995e1fe30e545e5c70c6b6ccf08ed75bf" - } - } -} diff --git a/internal/storage/historyoffequiv/golden/server.json b/internal/storage/historyoffequiv/golden/server.json deleted file mode 100644 index 73c11eed2..000000000 --- a/internal/storage/historyoffequiv/golden/server.json +++ /dev/null @@ -1,539 +0,0 @@ -{ - "recorded_from": "4a67d0d028", - "steps": [ - "create a", - "create b blocked by a", - "create epic", - "create epic.1 under epic", - "create wisp w1", - "create no-history nh", - "create c", - "create d", - "create e", - "update a fields labels metadata", - "update a no-op", - "update w1 to persistent", - "update c to ephemeral", - "update d parent epic", - "add dependencies", - "remove dependency b-\u003ea", - "comment on a and nh", - "claim heartbeat release b", - "claim e and reclaim no lease", - "metadata compare-and-set on a", - "close reopen close epic.1", - "close batch b and refused epic", - "force close blocked no-history nh", - "create batch", - "apply batch", - "defer f1 into the past and wake it", - "delete f2", - "create wisps x y z", - "update wisp x fields labels metadata", - "update wisp x no-op", - "claim and heartbeat refuse wisps", - "reclaim sweep scoped to in-progress wisp x", - "release wisp x", - "add and remove wisp dependencies", - "comment on wisp x", - "metadata compare-and-set on wisp x", - "close reopen wisp x", - "apply batch on the wisps plane", - "close batch wisps x and nh", - "defer wisp z into the past and wake it", - "delete wisp y", - "store create s1", - "store create issues s2 s3", - "store import upsert s1 and new s4", - "store update s2", - "store update s2 no-op", - "store labels", - "store dependencies", - "store comment", - "store claim s3", - "store close and reopen s2", - "store promote wisp sw", - "store transaction", - "store delete s3" - ], - "tables": { - "@dolt_log": { - "columns": [ - "seq", - "message" - ], - "rows": 42, - "sha256": "c43f56fef8a71aa0775d5c79d962b3bc1565305b2c18c3a670941891e376811f" - }, - "@dolt_status": { - "columns": [ - "table_name", - "staged", - "status" - ], - "rows": 1, - "sha256": "8658f682f824193bcb00dd2c9744be1e86b90a249374d368903303dfb62ac195" - }, - "bd_events_journal": { - "columns": [ - "seq", - "ts", - "op", - "issue_id", - "issue_json", - "dep_json", - "comment_json", - "actor" - ], - "rows": 0, - "sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - }, - "bd_events_seq": { - "columns": [ - "id", - "next_seq" - ], - "rows": 1, - "sha256": "13751b39fe07c3673f53b4f2c5ef7f2a28b26c0daed72eb7332c93c1fe520618" - }, - "child_counters": { - "columns": [ - "parent_id", - "last_child" - ], - "rows": 1, - "sha256": "753b8feea7bdc0fc74468faa597a32984edc501e9873e9f4ee1df7b59d56d0a6" - }, - "comments": { - "columns": [ - "id", - "issue_id", - "author", - "text", - "created_at" - ], - "rows": 2, - "sha256": "d5870471fe72b8e50fe898978f24a80a23e07750ab538d47353acfabfb0e9aed" - }, - "compaction_snapshots": { - "columns": [ - "id", - "issue_id", - "compaction_level", - "snapshot_json", - "created_at" - ], - "rows": 0, - "sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - }, - "config": { - "columns": [ - "key", - "value" - ], - "rows": 10, - "sha256": "671b33231eb47e4ad897051f4e8d86d3cca88685eeaa9ced537fe24c927fdea9" - }, - "custom_statuses": { - "columns": [ - "name", - "category" - ], - "rows": 0, - "sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - }, - "custom_types": { - "columns": [ - "name" - ], - "rows": 0, - "sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - }, - "dependencies": { - "columns": [ - "id", - "issue_id", - "type", - "created_at", - "created_by", - "metadata", - "thread_id", - "depends_on_issue_id", - "depends_on_wisp_id", - "depends_on_external" - ], - "rows": 6, - "sha256": "90f7dcd9895000df82d3b062d71a4c8ca81e7d5c9912f60d76515cb3eeee40d6" - }, - "events": { - "columns": [ - "issue_id", - "event_type", - "actor", - "old_value", - "new_value", - "comment", - "created_at", - "id" - ], - "rows": 51, - "sha256": "898f31694ce12dcd693f374634aea358b68239f6954fdcaae3b6342c1242f2bd" - }, - "federation_peers": { - "columns": [ - "name", - "remote_url", - "username", - "password_encrypted", - "sovereignty", - "last_sync", - "created_at", - "updated_at" - ], - "rows": 0, - "sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - }, - "ignored_schema_migrations": { - "columns": [ - "version", - "applied_at", - "content_hash" - ], - "rows": 27, - "sha256": "7164db2c129cbaafbd9c426d27e809d043cafc77e8aef1f68d1760905a685acb" - }, - "interactions": { - "columns": [ - "id", - "kind", - "created_at", - "actor", - "issue_id", - "model", - "prompt", - "response", - "error", - "tool_name", - "exit_code", - "parent_id", - "label", - "reason", - "extra" - ], - "rows": 0, - "sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - }, - "issue_counter": { - "columns": [ - "prefix", - "last_id" - ], - "rows": 0, - "sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - }, - "issue_snapshots": { - "columns": [ - "id", - "issue_id", - "snapshot_time", - "compaction_level", - "original_size", - "compressed_size", - "original_content", - "archived_events" - ], - "rows": 0, - "sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - }, - "issue_versions": { - "columns": [ - "issue_id", - "revision", - "epoch", - "durable_state", - "change_actor", - "change_agent", - "change_message", - "change_at", - "removed_at", - "removed_reason", - "attribution_status" - ], - "rows": 0, - "sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - }, - "issues": { - "columns": [ - "id", - "content_hash", - "title", - "description", - "design", - "acceptance_criteria", - "notes", - "status", - "priority", - "issue_type", - "assignee", - "estimated_minutes", - "created_at", - "created_by", - "owner", - "updated_at", - "closed_at", - "closed_by_session", - "external_ref", - "spec_id", - "compaction_level", - "compacted_at", - "compacted_at_commit", - "original_size", - "sender", - "ephemeral", - "wisp_type", - "pinned", - "is_template", - "mol_type", - "work_type", - "source_system", - "metadata", - "source_repo", - "close_reason", - "event_kind", - "actor", - "target", - "payload", - "await_type", - "await_id", - "timeout_ns", - "waiters", - "hook_bead", - "role_bead", - "agent_state", - "last_activity", - "role_type", - "rig", - "due_at", - "defer_until", - "no_history", - "started_at", - "is_blocked", - "row_lock", - "storage_class", - "current_revision" - ], - "rows": 15, - "sha256": "1f81df40f4c4d458f20cfb6535ca13468fa03fbd10276c44752977813a4b7e20" - }, - "labels": { - "columns": [ - "issue_id", - "label" - ], - "rows": 8, - "sha256": "0102293e23eec630b67542f93c5d8da6eef5ca627cff8662970e7cb211b013ef" - }, - "leases": { - "columns": [ - "issue_id", - "holder", - "granted_at", - "lease_expires_at", - "heartbeat_at", - "granted_node" - ], - "rows": 1, - "sha256": "1eca894450e8e801850ed2cbf1c746775ad50868acaaf17e033a5da1e4be10aa" - }, - "local_metadata": { - "columns": [ - "key", - "value" - ], - "rows": 31, - "sha256": "c1d1cbc41ca9968c5c7dd380609c28126c4024868dbade26575ce105b8931fb3" - }, - "metadata": { - "columns": [ - "key", - "value" - ], - "rows": 0, - "sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - }, - "provenance_events": { - "columns": [ - "id", - "issue_id", - "kind", - "actor", - "ref", - "ref_kind", - "payload", - "source", - "occurred_at", - "created_at" - ], - "rows": 0, - "sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - }, - "repo_mtimes": { - "columns": [ - "repo_path", - "jsonl_path", - "mtime_ns", - "last_checked" - ], - "rows": 0, - "sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - }, - "routes": { - "columns": [ - "prefix", - "path", - "created_at", - "updated_at" - ], - "rows": 0, - "sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - }, - "schema_migrations": { - "columns": [ - "version", - "content_hash" - ], - "rows": 69, - "sha256": "1d7a6ae809f330f8b4c3516af50ce7604c1db3cd4c1177af3e5c2f1991d74e14" - }, - "store_epoch": { - "columns": [ - "id", - "epoch", - "bumped_at", - "bumped_reason" - ], - "rows": 0, - "sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - }, - "wisp_child_counters": { - "columns": [ - "parent_id", - "last_child" - ], - "rows": 0, - "sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - }, - "wisp_comments": { - "columns": [ - "id", - "issue_id", - "author", - "text", - "created_at" - ], - "rows": 2, - "sha256": "3c5f1e3eac9ff32fb1158c4421909ddd49c9131edff756c4a0e184114da93350" - }, - "wisp_dependencies": { - "columns": [ - "id", - "issue_id", - "depends_on_issue_id", - "depends_on_wisp_id", - "depends_on_external", - "type", - "created_at", - "created_by", - "metadata", - "thread_id" - ], - "rows": 2, - "sha256": "80f14f79545a236513ea88f0e873f3dd477f7763aaa8cbfaa8f659a517c2a909" - }, - "wisp_events": { - "columns": [ - "id", - "issue_id", - "event_type", - "actor", - "old_value", - "new_value", - "comment", - "created_at" - ], - "rows": 25, - "sha256": "1ee25446153c51b39c4de60d7f03bb56172a84e72d28df2dd44ff79aa5fc3b23" - }, - "wisp_labels": { - "columns": [ - "issue_id", - "label" - ], - "rows": 4, - "sha256": "f9c2698a90ceb3bf0ee8c4034af2845b54d65cc7bdd7638965a4682ecd104c37" - }, - "wisps": { - "columns": [ - "id", - "content_hash", - "title", - "description", - "design", - "acceptance_criteria", - "notes", - "status", - "priority", - "issue_type", - "assignee", - "estimated_minutes", - "created_at", - "created_by", - "owner", - "updated_at", - "closed_at", - "closed_by_session", - "external_ref", - "spec_id", - "compaction_level", - "compacted_at", - "compacted_at_commit", - "original_size", - "sender", - "ephemeral", - "wisp_type", - "pinned", - "is_template", - "mol_type", - "work_type", - "source_system", - "metadata", - "source_repo", - "close_reason", - "event_kind", - "actor", - "target", - "payload", - "await_type", - "await_id", - "timeout_ns", - "waiters", - "hook_bead", - "role_bead", - "agent_state", - "last_activity", - "role_type", - "rig", - "due_at", - "defer_until", - "no_history", - "started_at", - "row_lock", - "storage_class", - "current_revision", - "is_blocked" - ], - "rows": 6, - "sha256": "f28b157579dedd4373778dc6050b2d1995e1fe30e545e5c70c6b6ccf08ed75bf" - } - } -} diff --git a/internal/storage/historyoffequiv/golden/uow.json b/internal/storage/historyoffequiv/golden/uow.json deleted file mode 100644 index 91b2d1f84..000000000 --- a/internal/storage/historyoffequiv/golden/uow.json +++ /dev/null @@ -1,531 +0,0 @@ -{ - "recorded_from": "4a67d0d028", - "steps": [ - "create a", - "create b blocked by a", - "create epic", - "create epic.1 under epic", - "create wisp w1", - "create no-history nh", - "create c", - "create d", - "create e", - "update a fields labels metadata", - "update a no-op", - "update w1 to persistent", - "update c to ephemeral", - "update d parent epic", - "add dependencies", - "remove dependency b-\u003ea", - "comment on a and nh", - "claim heartbeat release b", - "claim e and reclaim no lease", - "metadata compare-and-set on a", - "close reopen close epic.1", - "close batch b and refused epic", - "force close blocked no-history nh", - "create batch", - "apply batch", - "defer f1 into the past and wake it", - "delete f2", - "create wisps x y z", - "update wisp x fields labels metadata", - "update wisp x no-op", - "claim and heartbeat refuse wisps", - "reclaim sweep scoped to in-progress wisp x", - "release wisp x", - "add and remove wisp dependencies", - "comment on wisp x", - "metadata compare-and-set on wisp x", - "close reopen wisp x", - "apply batch on the wisps plane", - "close batch wisps x and nh", - "defer wisp z into the past and wake it", - "delete wisp y", - "domain upsert new u1", - "domain upsert existing u1", - "domain upsert new wisp uw", - "domain upsert existing wisp uw", - "import upsert u1 and new u2" - ], - "tables": { - "@dolt_log": { - "columns": [ - "seq", - "message" - ], - "rows": 30, - "sha256": "5f4e90621c1c115e95223419c9c8c8533a342469e359beeac2ec5a4b4ff7ab1b" - }, - "@dolt_status": { - "columns": [ - "table_name", - "staged", - "status" - ], - "rows": 0, - "sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - }, - "bd_events_journal": { - "columns": [ - "seq", - "ts", - "op", - "issue_id", - "issue_json", - "dep_json", - "comment_json", - "actor" - ], - "rows": 0, - "sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - }, - "bd_events_seq": { - "columns": [ - "id", - "next_seq" - ], - "rows": 1, - "sha256": "13751b39fe07c3673f53b4f2c5ef7f2a28b26c0daed72eb7332c93c1fe520618" - }, - "child_counters": { - "columns": [ - "parent_id", - "last_child" - ], - "rows": 0, - "sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - }, - "comments": { - "columns": [ - "id", - "issue_id", - "author", - "text", - "created_at" - ], - "rows": 2, - "sha256": "5b88b60c5a975432baff9ecfc6d0230e3bdc12275471a2db90c6f36b80ab4561" - }, - "compaction_snapshots": { - "columns": [ - "id", - "issue_id", - "compaction_level", - "snapshot_json", - "created_at" - ], - "rows": 0, - "sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - }, - "config": { - "columns": [ - "key", - "value" - ], - "rows": 10, - "sha256": "671b33231eb47e4ad897051f4e8d86d3cca88685eeaa9ced537fe24c927fdea9" - }, - "custom_statuses": { - "columns": [ - "name", - "category" - ], - "rows": 0, - "sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - }, - "custom_types": { - "columns": [ - "name" - ], - "rows": 0, - "sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - }, - "dependencies": { - "columns": [ - "id", - "issue_id", - "type", - "created_at", - "created_by", - "metadata", - "thread_id", - "depends_on_issue_id", - "depends_on_wisp_id", - "depends_on_external" - ], - "rows": 6, - "sha256": "10891c2f5540db5ffec57100e87d971863a67023d963cda658c43c56ce368a69" - }, - "events": { - "columns": [ - "issue_id", - "event_type", - "actor", - "old_value", - "new_value", - "comment", - "created_at", - "id" - ], - "rows": 39, - "sha256": "372c6143415ba862a0d951695e2c942670fc0a24a3b828d6755df97dbb34ade1" - }, - "federation_peers": { - "columns": [ - "name", - "remote_url", - "username", - "password_encrypted", - "sovereignty", - "last_sync", - "created_at", - "updated_at" - ], - "rows": 0, - "sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - }, - "ignored_schema_migrations": { - "columns": [ - "version", - "applied_at", - "content_hash" - ], - "rows": 27, - "sha256": "7164db2c129cbaafbd9c426d27e809d043cafc77e8aef1f68d1760905a685acb" - }, - "interactions": { - "columns": [ - "id", - "kind", - "created_at", - "actor", - "issue_id", - "model", - "prompt", - "response", - "error", - "tool_name", - "exit_code", - "parent_id", - "label", - "reason", - "extra" - ], - "rows": 0, - "sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - }, - "issue_counter": { - "columns": [ - "prefix", - "last_id" - ], - "rows": 0, - "sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - }, - "issue_snapshots": { - "columns": [ - "id", - "issue_id", - "snapshot_time", - "compaction_level", - "original_size", - "compressed_size", - "original_content", - "archived_events" - ], - "rows": 0, - "sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - }, - "issue_versions": { - "columns": [ - "issue_id", - "revision", - "epoch", - "durable_state", - "change_actor", - "change_agent", - "change_message", - "change_at", - "removed_at", - "removed_reason", - "attribution_status" - ], - "rows": 0, - "sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - }, - "issues": { - "columns": [ - "id", - "content_hash", - "title", - "description", - "design", - "acceptance_criteria", - "notes", - "status", - "priority", - "issue_type", - "assignee", - "estimated_minutes", - "created_at", - "created_by", - "owner", - "updated_at", - "closed_at", - "closed_by_session", - "external_ref", - "spec_id", - "compaction_level", - "compacted_at", - "compacted_at_commit", - "original_size", - "sender", - "ephemeral", - "wisp_type", - "pinned", - "is_template", - "mol_type", - "work_type", - "source_system", - "metadata", - "source_repo", - "close_reason", - "event_kind", - "actor", - "target", - "payload", - "await_type", - "await_id", - "timeout_ns", - "waiters", - "hook_bead", - "role_bead", - "agent_state", - "last_activity", - "role_type", - "rig", - "due_at", - "defer_until", - "no_history", - "started_at", - "is_blocked", - "row_lock", - "storage_class", - "current_revision" - ], - "rows": 12, - "sha256": "92e9eb8264175b0a6723941b3e946cd796afe4539cf9927798ce62800cab67ef" - }, - "labels": { - "columns": [ - "issue_id", - "label" - ], - "rows": 6, - "sha256": "d8cb82847c5ed046c6f264c12a51b1cc128f314fdc501aad3be0bd38cd3ec341" - }, - "leases": { - "columns": [ - "issue_id", - "holder", - "granted_at", - "lease_expires_at", - "heartbeat_at", - "granted_node" - ], - "rows": 1, - "sha256": "1eca894450e8e801850ed2cbf1c746775ad50868acaaf17e033a5da1e4be10aa" - }, - "local_metadata": { - "columns": [ - "key", - "value" - ], - "rows": 31, - "sha256": "c1d1cbc41ca9968c5c7dd380609c28126c4024868dbade26575ce105b8931fb3" - }, - "metadata": { - "columns": [ - "key", - "value" - ], - "rows": 0, - "sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - }, - "provenance_events": { - "columns": [ - "id", - "issue_id", - "kind", - "actor", - "ref", - "ref_kind", - "payload", - "source", - "occurred_at", - "created_at" - ], - "rows": 0, - "sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - }, - "repo_mtimes": { - "columns": [ - "repo_path", - "jsonl_path", - "mtime_ns", - "last_checked" - ], - "rows": 0, - "sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - }, - "routes": { - "columns": [ - "prefix", - "path", - "created_at", - "updated_at" - ], - "rows": 0, - "sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - }, - "schema_migrations": { - "columns": [ - "version", - "content_hash" - ], - "rows": 69, - "sha256": "1d7a6ae809f330f8b4c3516af50ce7604c1db3cd4c1177af3e5c2f1991d74e14" - }, - "store_epoch": { - "columns": [ - "id", - "epoch", - "bumped_at", - "bumped_reason" - ], - "rows": 0, - "sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - }, - "wisp_child_counters": { - "columns": [ - "parent_id", - "last_child" - ], - "rows": 0, - "sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" - }, - "wisp_comments": { - "columns": [ - "id", - "issue_id", - "author", - "text", - "created_at" - ], - "rows": 2, - "sha256": "3c5f1e3eac9ff32fb1158c4421909ddd49c9131edff756c4a0e184114da93350" - }, - "wisp_dependencies": { - "columns": [ - "id", - "issue_id", - "depends_on_issue_id", - "depends_on_wisp_id", - "depends_on_external", - "type", - "created_at", - "created_by", - "metadata", - "thread_id" - ], - "rows": 2, - "sha256": "80f14f79545a236513ea88f0e873f3dd477f7763aaa8cbfaa8f659a517c2a909" - }, - "wisp_events": { - "columns": [ - "id", - "issue_id", - "event_type", - "actor", - "old_value", - "new_value", - "comment", - "created_at" - ], - "rows": 27, - "sha256": "34d262640e93ffdc948862667d4bc5f01a9f655bc22d8e11abdfc777731a5771" - }, - "wisp_labels": { - "columns": [ - "issue_id", - "label" - ], - "rows": 4, - "sha256": "f9c2698a90ceb3bf0ee8c4034af2845b54d65cc7bdd7638965a4682ecd104c37" - }, - "wisps": { - "columns": [ - "id", - "content_hash", - "title", - "description", - "design", - "acceptance_criteria", - "notes", - "status", - "priority", - "issue_type", - "assignee", - "estimated_minutes", - "created_at", - "created_by", - "owner", - "updated_at", - "closed_at", - "closed_by_session", - "external_ref", - "spec_id", - "compaction_level", - "compacted_at", - "compacted_at_commit", - "original_size", - "sender", - "ephemeral", - "wisp_type", - "pinned", - "is_template", - "mol_type", - "work_type", - "source_system", - "metadata", - "source_repo", - "close_reason", - "event_kind", - "actor", - "target", - "payload", - "await_type", - "await_id", - "timeout_ns", - "waiters", - "hook_bead", - "role_bead", - "agent_state", - "last_activity", - "role_type", - "rig", - "due_at", - "defer_until", - "no_history", - "started_at", - "row_lock", - "storage_class", - "current_revision", - "is_blocked" - ], - "rows": 7, - "sha256": "f6885bac56aee7d91b48fdf56c657128ff0b530fa186fabfb52bbf271a3bc09b" - } - } -} diff --git a/internal/storage/historyoffequiv/historyoffequiv.go b/internal/storage/historyoffequiv/historyoffequiv.go deleted file mode 100644 index afcdfc7f2..000000000 --- a/internal/storage/historyoffequiv/historyoffequiv.go +++ /dev/null @@ -1,528 +0,0 @@ -// Package historyoffequiv is the versioned-history flag-off differential -// (gastownhall/beads#6132, #6135: "flag-off must remain byte-identical to -// today, proven by differential test"). -// -// Each storage leg (embedded Dolt, Dolt server, unit of work) runs one -// scripted mutation set through its own front doors on a store whose -// versioned history was never turned on, the default. Run then dumps every -// column of every table, digests each table, and compares the digests with -// the leg's golden. The goldens were RECORDED FROM THE CODE BEFORE THE -// VERSIONED-HISTORY STACK (origin/main 4a67d0d028, the parent of #6943). The -// schema added since, listed in schemaAdditions under the rule spelled out -// there, is the only difference the comparison tolerates; any other -// difference in any table is a flag-off behavior change and fails the test. -// -// # Maintaining the goldens -// -// Run records instead of checking when GoldenDirEnv is set. Recording and -// checking prepare a run the same way: each schemaAdditions entry the -// database has is checked under the rule and set aside before any table is -// digested. A golden recorded from any tree whose flag-off storage behavior -// matches the committed goldens is therefore byte-identical to them apart -// from recorded_from, and recording from the current tree is also how a -// failure is read: diff its full dump against one recorded from the base. -// -// - A migration whose schema flag-off leaves untouched (nullable columns -// that stay NULL, tables that stay empty) appends an entry to -// schemaAdditions in its own PR: those columns and tables, and its -// bookkeeping rows. The goldens do not change. -// - A deliberate change to what flag-off storage writes, including a -// migration that writes rows or values while history is off, -// re-records the affected legs from its PR's tree. The PR shows the -// golden diff, and that the full dumps differ from a recording of its -// base only where the change means them to. -// - A change to the scenario re-records every leg. Record it from -// 4a67d0d028 while the harness still builds there (copy this package -// and the three legs' test files into a worktree of that commit), so -// the new steps are anchored to the pre-stack code too; once it no -// longer builds there, from the PR's base. -// - A Dolt upgrade that changes only how the engine renders a stored -// value (a JSON or datetime cast) re-records the legs that run the new -// engine from the PR's tree, and the PR shows that the full dumps -// differ from a recording of its base, made with the old engine, only -// in that rendering. The embedded leg runs the engine go.mod pins; the -// server and unit-of-work legs run the dolt binary the test finds (CI: -// DOLT_VERSION in tools/bazel/dolt.bzl). -// -// recorded_from names the tree a golden was recorded from: a commit, or a -// commit and the PR on top of it. -package historyoffequiv - -import ( - "context" - "crypto/sha256" - "database/sql" - "embed" - "encoding/hex" - "encoding/json" - "fmt" - "os" - "path/filepath" - "slices" - "sort" - "strconv" - "strings" - "testing" - "time" -) - -// Prefix is the issue prefix every scenario id carries. A leg configures its -// store with it before Run. -const Prefix = "ho" - -// Query runs one read-only statement against the leg's database (the one its -// store writes) and returns every row, each value as text and nil for SQL -// NULL. Run casts every column to text in the statement itself, so a leg's -// driver never decides how a value is formatted. -type Query func(ctx context.Context, query string) ([][]*string, error) - -// Step is one mutation of the scenario, named for failure messages and for -// the golden's step list. -type Step struct { - Name string - Run func(ctx context.Context) error -} - -// Leg is what a backend hands Run. Its store is constructed the way the leg's -// tests construct one and is never handed SetVersionedHistoryEnabled, so -// "history off" is the default a store starts with rather than a switch -// turned off (FR-7). -type Leg struct { - // Name selects the golden: golden/.json. - Name string - // Roles is the leg's role-accessor surface. Every leg drives the same - // SharedSteps through it. - Roles Roles - // Wake runs the leg's lazy defer-wake sweep, the one write no role - // exposes. - Wake func(ctx context.Context) error - // Extra are the leg's own steps, run after the shared ones: the store - // legs' storage.DoltStorage methods (DoltStorageSteps), the unit-of-work - // leg's domain upsert. - Extra []Step - // Query reads the leg's database. - Query Query -} - -// GoldenDirEnv switches Run from checking to recording: set to a directory, -// Run writes the leg's golden there instead of comparing, with a full dump -// of the rows it digested beside it (.full.json.txt) for diffing two -// recordings by hand. RecordedFromEnv names the tree being recorded from; -// Run copies it into the golden. When to re-record is under "Maintaining the -// goldens" in the package doc. -const ( - GoldenDirEnv = "HISTORYOFFEQUIV_GOLDEN_DIR" - RecordedFromEnv = "HISTORYOFFEQUIV_RECORDED_FROM" -) - -// schemaAddition is schema added since the pre-stack code the goldens are -// anchored to that flag-off storage leaves untouched, grouped by the change -// that added it. -type schemaAddition struct { - // By names the change, for failure messages. - By string - // Columns are its nullable columns, by table. - Columns map[string][]string - // Tables are its tables. - Tables []string - // Migrations are the migration runner's bookkeeping rows for its own - // migrations, by bookkeeping table (each keyed by its version column). - Migrations map[string][]string -} - -// schemaAdditions is THE ONLY DIFFERENCE from the goldens this test -// tolerates. The rule, applied to every run before any table is digested, -// whether the run records a golden or checks one: -// -// 1. A listed column, where the database has it, must be NULL in every -// row; it is then left out of its table's dump, so the rest of every row -// still has to match the golden byte for byte. A listed column holding -// any value fails: with versioned history off nothing may write it. -// 2. A listed table, where the database has it, must be empty; it is then -// left out. -// 3. A listed bookkeeping row, where the database has it, must be there -// once; it is then left out. Every other row of a bookkeeping table is -// compared like any table's. -// -// After that, checking demands the golden's tables and columns exactly, and -// every table's digest over the golden's columns must equal the golden's. -// Nothing else is excused: not a value, not a row, not a row count. A later -// migration whose schema flag-off leaves untouched appends its own entry -// here (see "Maintaining the goldens" in the package doc). -var schemaAdditions = []schemaAddition{{ - By: "the versioned-history stack (#6943, #6661, #6664)", - Columns: map[string][]string{ - // Migration 0070: the participation_generation write fence (#6943). - // A create with history off leaves it NULL, indistinguishable from - // a legacy row (design §16.2b, FR-7). - "issues": {"participation_generation"}, - // Migration 0070 mirrors it, inert, on the wisps table (FR-8). - "wisps": {"participation_generation"}, - // Migration 0071 (#6661). - "issue_versions": {"removed_restriction"}, - }, - // Migration 0072 (#6664). - Tables: []string{"epoch_minted_addresses"}, - // 0070-0072 on the main series, and ignored/0028 (the wisps half of - // 0070) on the dolt_ignored series. - Migrations: map[string][]string{ - "schema_migrations": {"70", "71", "72"}, - "ignored_schema_migrations": {"28"}, - }, -}} - -// runWindow widens the span of the run on each side to bound the wall-clock -// instants the scenario's writes stamp. It is generous so that no time zone a -// DATETIME may be read back in, and no rounding of a sub-second instant, can -// put such an instant outside it. The scenario's own timestamps are decades -// away (fixedAt), so a column whose value moved from a scenario-supplied -// instant to "now" (or back) still shows up in the digest. -const runWindow = 36 * time.Hour - -// Run applies the shared scenario and the leg's extra steps, then checks -// every table against the leg's golden (or records it). -func Run(t *testing.T, leg Leg) { - t.Helper() - if leg.Name == "" || leg.Roles == nil || leg.Wake == nil || leg.Query == nil { - t.Fatalf("historyoffequiv.Run: leg %q needs a Name, Roles, Wake and Query", leg.Name) - } - ctx := context.Background() - before := doltLogCount(t, ctx, leg.Query) - start := time.Now().UTC() - steps := append(SharedSteps(leg.Roles, leg.Wake), leg.Extra...) - var names []string - for _, step := range steps { - if err := step.Run(ctx); err != nil { - t.Fatalf("step %q: %v", step.Name, err) - } - names = append(names, step.Name) - } - end := time.Now().UTC() - got := setAsideAdditions(t, dumpAll(t, ctx, leg.Query, start.Add(-runWindow), end.Add(runWindow), before)) - got.Steps = names - - if dir := os.Getenv(GoldenDirEnv); dir != "" { - if t.Failed() { - t.Fatalf("not recording golden/%s.json: the run breaks the schemaAdditions rule (above)", leg.Name) - } - writeGolden(t, dir, leg.Name, got) - return - } - checkGolden(t, leg.Name, got) -} - -// dump is every table's normalized rows. -type dump struct { - Steps []string - Tables map[string]tableDump -} - -type tableDump struct { - Columns []string - Rows [][]*string -} - -type golden struct { - RecordedFrom string `json:"recorded_from"` - Steps []string `json:"steps"` - Tables map[string]goldenTable `json:"tables"` -} - -type goldenTable struct { - Columns []string `json:"columns"` - Rows int `json:"rows"` - SHA256 string `json:"sha256"` -} - -//go:embed golden/*.json -var goldenFS embed.FS - -// setAsideAdditions applies the schemaAdditions rule to a run's dump: it -// checks every listed column, table and bookkeeping row the database has, -// reporting each breach, and returns the dump without them. Recording and -// checking both digest only what it returns. -func setAsideAdditions(t *testing.T, d dump) dump { - t.Helper() - columns := map[string]map[string]string{} // table -> column -> By - tables := map[string]string{} - migrations := map[string]map[string]string{} // table -> version -> By - for _, a := range schemaAdditions { - for table, cols := range a.Columns { - for _, c := range cols { - if columns[table] == nil { - columns[table] = map[string]string{} - } - columns[table][c] = a.By - } - } - for _, table := range a.Tables { - tables[table] = a.By - } - for table, versions := range a.Migrations { - for _, v := range versions { - if migrations[table] == nil { - migrations[table] = map[string]string{} - } - migrations[table][v] = a.By - } - } - } - - out := dump{Steps: d.Steps, Tables: map[string]tableDump{}} - for _, table := range sortedKeys(d.Tables) { - td := d.Tables[table] - if by, ok := tables[table]; ok { - if len(td.Rows) != 0 { - t.Errorf("table %s, added by %s, has %d rows with versioned history off, want none:\n%s", - table, by, len(td.Rows), formatRows(td.Columns, td.Rows)) - } - continue - } - var keep []int - for i, c := range td.Columns { - by, listed := columns[table][c] - if !listed { - keep = append(keep, i) - continue - } - for _, row := range td.Rows { - if row[i] != nil { - t.Errorf("%s.%s, added by %s, holds %q with versioned history off, want NULL in every row; row: %s", - table, c, by, *row[i], formatRow(row)) - } - } - } - kept := tableDump{Columns: make([]string, len(keep))} - for j, i := range keep { - kept.Columns[j] = td.Columns[i] - } - version := slices.Index(kept.Columns, "version") - if migrations[table] != nil && version < 0 { - t.Errorf("bookkeeping table %s has no version column: %v", table, td.Columns) - } - seen := map[string]int{} - for _, row := range td.Rows { - projected := make([]*string, len(keep)) - for j, i := range keep { - projected[j] = row[i] - } - if version >= 0 && projected[version] != nil { - if _, listed := migrations[table][*projected[version]]; listed { - seen[*projected[version]]++ - continue - } - } - kept.Rows = append(kept.Rows, projected) - } - for _, v := range sortedKeys(seen) { - if seen[v] != 1 { - t.Errorf("%s has %d rows for migration %s, added by %s, want at most 1", table, seen[v], v, migrations[table][v]) - } - } - sortRows(kept.Rows) - out.Tables[table] = kept - } - return out -} - -// checkGolden compares a set-aside dump with golden/.json. -func checkGolden(t *testing.T, leg string, got dump) { - t.Helper() - b, err := goldenFS.ReadFile("golden/" + leg + ".json") - if err != nil { - t.Fatalf("no golden for leg %q: record one with %s (see the package doc): %v", leg, GoldenDirEnv, err) - } - var want golden - if err := json.Unmarshal(b, &want); err != nil { - t.Fatalf("golden/%s.json: %v", leg, err) - } - from := fmt.Sprintf("golden/%s.json (recorded from %s)", leg, want.RecordedFrom) - if !slices.Equal(got.Steps, want.Steps) { - t.Fatalf("the scenario's steps differ from those %s ran, so its digests no longer describe this scenario; "+ - "re-record it (\"Maintaining the goldens\" in the package doc):\ngot: %q\nwant: %q", - from, got.Steps, want.Steps) - } - unexplained := "; a migration whose schema flag-off leaves untouched lists it in schemaAdditions, " + - "and a change to what flag-off writes re-records the goldens (\"Maintaining the goldens\" in the package doc)" - for _, table := range sortedKeys(got.Tables) { - if _, ok := want.Tables[table]; !ok { - t.Errorf("table %s is not in %s and not in schemaAdditions%s:\n%s", - table, from, unexplained, formatRows(got.Tables[table].Columns, got.Tables[table].Rows)) - } - } - for _, table := range sortedKeys(want.Tables) { - wt := want.Tables[table] - gt, ok := got.Tables[table] - if !ok { - t.Errorf("table %s is in %s but not in this database", table, from) - continue - } - index := map[string]int{} - for i, c := range gt.Columns { - index[c] = i - } - ok = true - for _, c := range wt.Columns { - if _, present := index[c]; !present { - t.Errorf("%s.%s is in %s but not in this database", table, c, from) - ok = false - } - } - for _, c := range gt.Columns { - if !slices.Contains(wt.Columns, c) { - t.Errorf("%s.%s is not in %s and not in schemaAdditions%s", table, c, from, unexplained) - ok = false - } - } - if !ok { - continue - } - projected := make([][]*string, 0, len(gt.Rows)) - for _, row := range gt.Rows { - p := make([]*string, len(wt.Columns)) - for j, c := range wt.Columns { - p[j] = row[index[c]] - } - projected = append(projected, p) - } - sortRows(projected) - if d := digest(projected); d != wt.SHA256 || len(projected) != wt.Rows { - t.Errorf("%s differs from %s: got %d rows %s, want %d rows %s; rows now (columns %v):\n%s", - table, from, len(projected), d, wt.Rows, wt.SHA256, wt.Columns, formatRows(wt.Columns, projected)) - } - } -} - -// writeGolden records a set-aside dump as golden/.json in dir. -func writeGolden(t *testing.T, dir, leg string, d dump) { - t.Helper() - g := golden{RecordedFrom: os.Getenv(RecordedFromEnv), Steps: d.Steps, Tables: map[string]goldenTable{}} - if g.RecordedFrom == "" { - t.Fatalf("%s is set but %s is not: name the tree this golden is recorded from", GoldenDirEnv, RecordedFromEnv) - } - full := map[string][]string{} - for table, td := range d.Tables { - g.Tables[table] = goldenTable{Columns: td.Columns, Rows: len(td.Rows), SHA256: digest(td.Rows)} - full[table] = append(full[table], strings.Join(td.Columns, " | ")) - for _, row := range td.Rows { - full[table] = append(full[table], formatRow(row)) - } - } - b, err := json.MarshalIndent(g, "", " ") - if err != nil { - t.Fatal(err) - } - if err := os.WriteFile(filepath.Join(dir, leg+".json"), append(b, '\n'), 0o600); err != nil { - t.Fatal(err) - } - fb, _ := json.MarshalIndent(full, "", " ") - _ = os.WriteFile(filepath.Join(dir, leg+".full.json.txt"), fb, 0o600) -} - -func digest(rows [][]*string) string { - h := sha256.New() - for _, row := range rows { - h.Write([]byte(formatRow(row))) - h.Write([]byte{'\n'}) - } - return hex.EncodeToString(h.Sum(nil)) -} - -func formatRow(row []*string) string { - parts := make([]string, len(row)) - for i, v := range row { - if v == nil { - parts[i] = "NULL" - } else { - parts[i] = strconv.Quote(*v) - } - } - return strings.Join(parts, " ") -} - -func formatRows(columns []string, rows [][]*string) string { - lines := []string{" " + strings.Join(columns, " | ")} - for _, row := range rows { - lines = append(lines, " "+formatRow(row)) - } - return strings.Join(lines, "\n") -} - -func sortRows(rows [][]*string) { - sort.SliceStable(rows, func(i, j int) bool { return formatRow(rows[i]) < formatRow(rows[j]) }) -} - -func sortedKeys[V any](m map[string]V) []string { - keys := make([]string, 0, len(m)) - for k := range m { - keys = append(keys, k) - } - sort.Strings(keys) - return keys -} - -// SQLQuery adapts a leg that reads through database/sql. open returns a -// handle on the leg's database and a function that releases it. -func SQLQuery(open func(ctx context.Context) (*sql.DB, func(), error)) Query { - return func(ctx context.Context, query string) ([][]*string, error) { - db, release, err := open(ctx) - if err != nil { - return nil, err - } - defer release() - rows, err := db.QueryContext(ctx, query) - if err != nil { - return nil, err - } - defer rows.Close() - cols, err := rows.Columns() - if err != nil { - return nil, err - } - var out [][]*string - for rows.Next() { - raw := make([]sql.NullString, len(cols)) - dest := make([]any, len(cols)) - for i := range raw { - dest[i] = &raw[i] - } - if err := rows.Scan(dest...); err != nil { - return nil, err - } - row := make([]*string, len(cols)) - for i, v := range raw { - if v.Valid { - s := v.String - row[i] = &s - } - } - out = append(out, row) - } - return out, rows.Err() - } -} - -// AnyRows converts rows of driver values (a raw-SQL use case's result) to -// Query's shape. -func AnyRows(rows [][]any) [][]*string { - out := make([][]*string, 0, len(rows)) - for _, r := range rows { - row := make([]*string, len(r)) - for i, v := range r { - switch v := v.(type) { - case nil: - case []byte: - s := string(v) - row[i] = &s - case string: - s := v - row[i] = &s - default: - s := fmt.Sprint(v) - row[i] = &s - } - } - out = append(out, row) - } - return out -} diff --git a/internal/storage/historyoffequiv/scenario.go b/internal/storage/historyoffequiv/scenario.go deleted file mode 100644 index 90dadd04b..000000000 --- a/internal/storage/historyoffequiv/scenario.go +++ /dev/null @@ -1,539 +0,0 @@ -package historyoffequiv - -import ( - "context" - "encoding/json" - "errors" - "fmt" - "time" - - "github.com/steveyegge/beads/internal/storage" - "github.com/steveyegge/beads/internal/types" - publicops "github.com/steveyegge/beads/issueops" -) - -// Roles is the role-accessor surface all three legs offer. The shared -// scenario reaches storage only through it (and Leg.Wake), so each leg runs -// the scenario through its own implementations: the embedded store's and the -// server store's issueops-backed roles, and the unit-of-work provider's -// domain-backed ones. -type Roles interface { - IssueLifecycle() (publicops.Lifecycle, error) - BatchCreator() (publicops.BatchCreator, error) - BatchApplier() (publicops.BatchApplier, error) - BatchCloser() (publicops.BatchCloser, error) - DependencyEditor() (publicops.DependencyEditor, error) - Commenter() (publicops.Commenter, error) - IssueClaimer() (publicops.Claimer, error) - Heartbeater() (publicops.Heartbeater, error) - Releaser() (publicops.Releaser, error) - LeaseReclaimer() (publicops.LeaseReclaimer, error) - MetadataCAS() (publicops.MetadataCAS, error) - Deleter() (publicops.Deleter, error) -} - -// fixedAt is every timestamp the scenario supplies. It is decades outside -// any run's window, so a column that keeps it reads it verbatim in the dump -// and a column that loses it to the wall clock reads "". -var fixedAt = time.Date(2001, 2, 3, 4, 5, 6, 0, time.UTC) - -func id(suffix string) string { return Prefix + "-" + suffix } - -func newIssue(suffix, title string, issueType types.IssueType) *types.Issue { - return &types.Issue{ - ID: id(suffix), Title: title, Description: "about " + suffix, Status: types.StatusOpen, - Priority: 2, IssueType: issueType, CreatedAt: fixedAt, UpdatedAt: fixedAt, - } -} - -func set[T any](v T) publicops.Field[T] { return publicops.Field[T]{Set: true, Value: v} } - -func raw(s string) *json.RawMessage { - m := json.RawMessage(s) - return &m -} - -// SharedSteps is the scenario every leg runs: each write role on the issues -// plane, then each again on the wisps plane (an ephemeral wisp, and the -// no-history wisp nh along the way), including the paths the -// versioned-history stack rewired (single and batch creates, the persistence -// moves between planes, the defer wake), refusals that must write nothing (a -// batch close item close policy refuses), and no-op updates that must leave -// every row alone. On the wisps plane the lease roles refuse (Claim, -// Heartbeat) or find nothing (Reclaim), as their contracts say, and those -// steps check the refusal. Neither reclaim sweep finds an expired lease: a -// role cannot age one, so they cover the sweep's reads and commit, not a -// revert. -func SharedSteps(r Roles, wake func(context.Context) error) []Step { - create := func(req publicops.CreateRequest) func(context.Context) error { - req.Actor = "alice" - return func(ctx context.Context) error { - lifecycle, err := r.IssueLifecycle() - if err != nil { - return err - } - _, err = lifecycle.Create(ctx, req) - return err - } - } - update := func(issueID string, patch publicops.IssuePatch) func(context.Context) error { - return func(ctx context.Context) error { - lifecycle, err := r.IssueLifecycle() - if err != nil { - return err - } - _, err = lifecycle.Update(ctx, publicops.UpdateRequest{Actor: "bob", IssueID: issueID, Patch: patch}) - return err - } - } - - a := newIssue("a", "alpha", types.TypeTask) - a.Labels = []string{"one", "two"} - a.Metadata = json.RawMessage(`{"k":1,"s":"x"}`) - a.Notes = "first note" - b := newIssue("b", "bravo", types.TypeBug) - b.Priority = 1 - epic := newIssue("epic", "the epic", types.TypeEpic) - child := newIssue("epic.1", "epic child", types.TypeTask) - w1 := newIssue("w1", "a wisp", types.TypeTask) - w1.Ephemeral = true - w1.Labels = []string{"wl"} - nh := newIssue("nh", "no history", types.TypeTask) - nh.NoHistory = true - c := newIssue("c", "charlie", types.TypeFeature) - c.Labels = []string{"moving"} - d := newIssue("d", "delta", types.TypeTask) - e := newIssue("e", "echo", types.TypeTask) - // The wisps-plane block's wisps: x carries the field, lease, comment, - // metadata and close traffic, y the refused claim and the dependency, - // apply and delete traffic, z the defer wake. - wisp := func(suffix, title string) *types.Issue { - w := newIssue(suffix, title, types.TypeTask) - w.Ephemeral = true - return w - } - x := wisp("x", "x-ray") - x.Labels = []string{"xl", "xm"} - x.Metadata = json.RawMessage(`{"k":1}`) - y, z := wisp("y", "yankee"), wisp("z", "zulu") - - return []Step{ - {"create a", create(publicops.CreateRequest{Issue: a})}, - {"create b blocked by a", create(publicops.CreateRequest{Issue: b, - Dependencies: []publicops.CreateDependency{{TargetID: id("a"), Type: types.DepBlocks}}})}, - {"create epic", create(publicops.CreateRequest{Issue: epic})}, - {"create epic.1 under epic", create(publicops.CreateRequest{Issue: child, ParentID: id("epic")})}, - {"create wisp w1", create(publicops.CreateRequest{Issue: w1})}, - {"create no-history nh", create(publicops.CreateRequest{Issue: nh})}, - {"create c", create(publicops.CreateRequest{Issue: c})}, - {"create d", create(publicops.CreateRequest{Issue: d})}, - {"create e", create(publicops.CreateRequest{Issue: e})}, - - {"update a fields labels metadata", update(id("a"), publicops.IssuePatch{ - Title: set("alpha two"), - Priority: set(3), - Assignee: set("bob"), - Status: set(types.StatusInProgress), - AppendNotes: set("second note"), - Labels: publicops.LabelPatch{Add: []string{"three"}, Remove: []string{"two"}}, - Metadata: publicops.MetadataPatch{Set: map[string]json.RawMessage{"k": json.RawMessage(`2`)}}, - })}, - {"update a no-op", update(id("a"), publicops.IssuePatch{Title: set("alpha two")})}, - {"update w1 to persistent", update(id("w1"), publicops.IssuePatch{Persistence: set(types.PersistenceModePersistent)})}, - {"update c to ephemeral", update(id("c"), publicops.IssuePatch{Persistence: set(types.PersistenceModeEphemeral)})}, - {"update d parent epic", update(id("d"), publicops.IssuePatch{ParentID: set(id("epic"))})}, - - {"add dependencies", func(ctx context.Context) error { - editor, err := r.DependencyEditor() - if err != nil { - return err - } - _, err = editor.AddDependencies(ctx, publicops.AddDependenciesRequest{Actor: "carol", Edges: []publicops.DependencyEdge{ - {IssueID: id("d"), DependsOnID: id("b"), Type: types.DepBlocks}, - {IssueID: id("e"), DependsOnID: id("a"), Type: types.DepRelated}, - {IssueID: id("nh"), DependsOnID: id("d"), Type: types.DepBlocks}, - }}) - return err - }}, - {"remove dependency b->a", func(ctx context.Context) error { - editor, err := r.DependencyEditor() - if err != nil { - return err - } - _, err = editor.RemoveDependency(ctx, publicops.RemoveDependencyRequest{Actor: "carol", IssueID: id("b"), DependsOnID: id("a")}) - return err - }}, - {"comment on a and nh", func(ctx context.Context) error { - commenter, err := r.Commenter() - if err != nil { - return err - } - if _, err := commenter.AddComment(ctx, publicops.AddCommentRequest{Author: "carol", IssueID: id("a"), Text: "hello a"}); err != nil { - return err - } - _, err = commenter.AddComment(ctx, publicops.AddCommentRequest{Author: "carol", IssueID: id("nh"), Text: "hello nh"}) - return err - }}, - {"claim heartbeat release b", func(ctx context.Context) error { - claimer, err := r.IssueClaimer() - if err != nil { - return err - } - if _, err := claimer.Claim(ctx, publicops.ClaimRequest{Actor: "dave", IssueID: id("b")}); err != nil { - return fmt.Errorf("claim: %w", err) - } - heartbeater, err := r.Heartbeater() - if err != nil { - return err - } - if _, err := heartbeater.Heartbeat(ctx, publicops.HeartbeatRequest{Actor: "dave", IssueID: id("b")}); err != nil { - return fmt.Errorf("heartbeat: %w", err) - } - releaser, err := r.Releaser() - if err != nil { - return err - } - _, err = releaser.Release(ctx, publicops.ReleaseRequest{Actor: "dave", IssueID: id("b")}) - return err - }}, - {"claim e and reclaim no lease", func(ctx context.Context) error { - claimer, err := r.IssueClaimer() - if err != nil { - return err - } - if _, err := claimer.Claim(ctx, publicops.ClaimRequest{Actor: "erin", IssueID: id("e")}); err != nil { - return fmt.Errorf("claim: %w", err) - } - reclaimer, err := r.LeaseReclaimer() - if err != nil { - return err - } - // e's lease has not expired, so the sweep reverts nothing. - _, err = reclaimer.Reclaim(ctx, publicops.ReclaimRequest{Actor: "reaper", - Filter: publicops.ReclaimFilter{IDs: []string{id("e")}}}) - return err - }}, - {"metadata compare-and-set on a", func(ctx context.Context) error { - cas, err := r.MetadataCAS() - if err != nil { - return err - } - _, err = cas.CompareAndSetKey(ctx, publicops.CompareAndSetKeyRequest{Actor: "frank", IssueID: id("a"), Key: "cas", Value: raw(`"v1"`)}) - return err - }}, - {"close reopen close epic.1", func(ctx context.Context) error { - lifecycle, err := r.IssueLifecycle() - if err != nil { - return err - } - if _, err := lifecycle.Close(ctx, publicops.CloseRequest{Actor: "bob", IssueID: id("epic.1"), Reason: "done"}); err != nil { - return fmt.Errorf("close: %w", err) - } - if _, err := lifecycle.Reopen(ctx, publicops.ReopenRequest{Actor: "bob", IssueID: id("epic.1"), Reason: "not yet"}); err != nil { - return fmt.Errorf("reopen: %w", err) - } - _, err = lifecycle.Close(ctx, publicops.CloseRequest{Actor: "bob", IssueID: id("epic.1"), Reason: "done again"}) - return err - }}, - {"close batch b and refused epic", func(ctx context.Context) error { - closer, err := r.BatchCloser() - if err != nil { - return err - } - // b closes; epic keeps its open child d, so close policy refuses - // that item and the batch keeps its survivor. - _, err = closer.CloseBatch(ctx, publicops.CloseBatchRequest{Actor: "bob", Items: []publicops.BatchCloseItem{ - {IssueID: id("b"), Reason: "b done"}, {IssueID: id("epic"), Reason: "epic done"}, - }}) - return err - }}, - {"force close blocked no-history nh", func(ctx context.Context) error { - lifecycle, err := r.IssueLifecycle() - if err != nil { - return err - } - _, err = lifecycle.Close(ctx, publicops.CloseRequest{Actor: "bob", IssueID: id("nh"), Reason: "nh done", Force: true}) - return err - }}, - {"create batch", func(ctx context.Context) error { - creator, err := r.BatchCreator() - if err != nil { - return err - } - f1 := newIssue("f1", "foxtrot one", types.TypeTask) - f1.Labels = []string{"batch"} - f2 := newIssue("f2", "foxtrot two", types.TypeTask) - fw := newIssue("fw", "foxtrot wisp", types.TypeTask) - fw.Ephemeral = true - _, err = creator.CreateBatch(ctx, publicops.CreateBatchRequest{Actor: "gina", Items: []publicops.BatchCreateItem{ - {Issue: f1}, - {Issue: f2, Dependencies: []publicops.CreateDependency{{TargetID: id("f1"), Type: types.DepBlocks}}}, - {Issue: fw}, - }}) - return err - }}, - {"apply batch", func(ctx context.Context) error { - applier, err := r.BatchApplier() - if err != nil { - return err - } - g1 := newIssue("g1", "golf one", types.TypeTask) - g2 := newIssue("g2", "golf two", types.TypeBug) - g2.Labels = []string{"applied"} - _, err = applier.ApplyBatch(ctx, publicops.ApplyBatchRequest{Actor: "hank", Items: []publicops.ApplyItem{ - {Kind: publicops.ItemCreate, Create: &publicops.CreateItem{Key: "g1", Issue: g1}}, - {Kind: publicops.ItemCreate, Create: &publicops.CreateItem{Key: "g2", Issue: g2}}, - {Kind: publicops.ItemDepAdd, DepAdd: &publicops.DepAddItem{Source: publicops.Ref{Key: "g2"}, Target: publicops.Ref{Key: "g1"}, Type: types.DepBlocks}}, - {Kind: publicops.ItemUpdate, Update: &publicops.UpdateItem{Target: publicops.Ref{ID: id("d")}, Patch: publicops.IssuePatch{Assignee: set("hank")}}}, - {Kind: publicops.ItemClose, Close: &publicops.CloseItem{Target: publicops.Ref{Key: "g1"}, Reason: "applied"}}, - }}) - return err - }}, - {"defer f1 into the past and wake it", func(ctx context.Context) error { - past := fixedAt - if err := update(id("f1"), publicops.IssuePatch{Status: set(types.StatusDeferred), DeferUntil: set(&past)})(ctx); err != nil { - return fmt.Errorf("defer: %w", err) - } - return wake(ctx) - }}, - {"delete f2", func(ctx context.Context) error { - deleter, err := r.Deleter() - if err != nil { - return err - } - _, err = deleter.Delete(ctx, publicops.DeleteRequest{Actor: "ivan", IDs: []string{id("f2")}, Force: true}) - return err - }}, - - // The wisps plane, role by role. - {"create wisps x y z", func(ctx context.Context) error { - for _, w := range []*types.Issue{x, y, z} { - if err := create(publicops.CreateRequest{Issue: w})(ctx); err != nil { - return fmt.Errorf("create %s: %w", w.ID, err) - } - } - return nil - }}, - {"update wisp x fields labels metadata", update(id("x"), publicops.IssuePatch{ - Title: set("x-ray two"), - Priority: set(1), - Assignee: set("bob"), - Status: set(types.StatusInProgress), - AppendNotes: set("wisp note"), - Labels: publicops.LabelPatch{Add: []string{"xn"}, Remove: []string{"xm"}}, - Metadata: publicops.MetadataPatch{Set: map[string]json.RawMessage{"k": json.RawMessage(`2`)}}, - })}, - {"update wisp x no-op", update(id("x"), publicops.IssuePatch{Title: set("x-ray two")})}, - {"claim and heartbeat refuse wisps", func(ctx context.Context) error { - // The lease roles do not lease a wisp: Claim answers ErrNotFound, - // Heartbeat ErrNotClaimable, and neither may write. - claimer, err := r.IssueClaimer() - if err != nil { - return err - } - if _, err := claimer.Claim(ctx, publicops.ClaimRequest{Actor: "dave", IssueID: id("y")}); !errors.Is(err, publicops.ErrNotFound) { - return fmt.Errorf("claim wisp y = %v, want ErrNotFound", err) - } - heartbeater, err := r.Heartbeater() - if err != nil { - return err - } - if _, err := heartbeater.Heartbeat(ctx, publicops.HeartbeatRequest{Actor: "bob", IssueID: id("x")}); !errors.Is(err, publicops.ErrNotClaimable) { - return fmt.Errorf("heartbeat wisp x = %v, want ErrNotClaimable", err) - } - return nil - }}, - {"reclaim sweep scoped to in-progress wisp x", func(ctx context.Context) error { - reclaimer, err := r.LeaseReclaimer() - if err != nil { - return err - } - // The sweep only reads the issues table, so it reverts nothing. - _, err = reclaimer.Reclaim(ctx, publicops.ReclaimRequest{Actor: "reaper", - Filter: publicops.ReclaimFilter{IDs: []string{id("x")}}}) - return err - }}, - {"release wisp x", func(ctx context.Context) error { - releaser, err := r.Releaser() - if err != nil { - return err - } - _, err = releaser.Release(ctx, publicops.ReleaseRequest{Actor: "bob", IssueID: id("x")}) - return err - }}, - {"add and remove wisp dependencies", func(ctx context.Context) error { - editor, err := r.DependencyEditor() - if err != nil { - return err - } - if _, err := editor.AddDependencies(ctx, publicops.AddDependenciesRequest{Actor: "carol", Edges: []publicops.DependencyEdge{ - {IssueID: id("y"), DependsOnID: id("x"), Type: types.DepBlocks}, - {IssueID: id("x"), DependsOnID: id("a"), Type: types.DepRelated}, - }}); err != nil { - return fmt.Errorf("add: %w", err) - } - _, err = editor.RemoveDependency(ctx, publicops.RemoveDependencyRequest{Actor: "carol", IssueID: id("y"), DependsOnID: id("x")}) - return err - }}, - {"comment on wisp x", func(ctx context.Context) error { - commenter, err := r.Commenter() - if err != nil { - return err - } - _, err = commenter.AddComment(ctx, publicops.AddCommentRequest{Author: "carol", IssueID: id("x"), Text: "hello x"}) - return err - }}, - {"metadata compare-and-set on wisp x", func(ctx context.Context) error { - cas, err := r.MetadataCAS() - if err != nil { - return err - } - _, err = cas.CompareAndSetKey(ctx, publicops.CompareAndSetKeyRequest{Actor: "frank", IssueID: id("x"), Key: "cas", Value: raw(`"w1"`)}) - return err - }}, - {"close reopen wisp x", func(ctx context.Context) error { - lifecycle, err := r.IssueLifecycle() - if err != nil { - return err - } - if _, err := lifecycle.Close(ctx, publicops.CloseRequest{Actor: "bob", IssueID: id("x"), Reason: "x done"}); err != nil { - return fmt.Errorf("close: %w", err) - } - _, err = lifecycle.Reopen(ctx, publicops.ReopenRequest{Actor: "bob", IssueID: id("x"), Reason: "x not yet"}) - return err - }}, - {"apply batch on the wisps plane", func(ctx context.Context) error { - applier, err := r.BatchApplier() - if err != nil { - return err - } - h1 := wisp("h1", "hotel wisp") - h1.Labels = []string{"applied"} - _, err = applier.ApplyBatch(ctx, publicops.ApplyBatchRequest{Actor: "hank", Items: []publicops.ApplyItem{ - {Kind: publicops.ItemCreate, Create: &publicops.CreateItem{Key: "h1", Issue: h1}}, - {Kind: publicops.ItemDepAdd, DepAdd: &publicops.DepAddItem{Source: publicops.Ref{ID: id("y")}, Target: publicops.Ref{Key: "h1"}, Type: types.DepBlocks}}, - {Kind: publicops.ItemUpdate, Update: &publicops.UpdateItem{Target: publicops.Ref{ID: id("y")}, Patch: publicops.IssuePatch{Assignee: set("hank")}}}, - {Kind: publicops.ItemClose, Close: &publicops.CloseItem{Target: publicops.Ref{Key: "h1"}, Reason: "applied"}}, - }}) - return err - }}, - {"close batch wisps x and nh", func(ctx context.Context) error { - closer, err := r.BatchCloser() - if err != nil { - return err - } - // x closes; nh is already closed, so the batch closes x alone. - _, err = closer.CloseBatch(ctx, publicops.CloseBatchRequest{Actor: "bob", Items: []publicops.BatchCloseItem{ - {IssueID: id("x"), Reason: "x done again"}, {IssueID: id("nh"), Reason: "nh again"}, - }}) - return err - }}, - {"defer wisp z into the past and wake it", func(ctx context.Context) error { - past := fixedAt - if err := update(id("z"), publicops.IssuePatch{Status: set(types.StatusDeferred), DeferUntil: set(&past)})(ctx); err != nil { - return fmt.Errorf("defer: %w", err) - } - return wake(ctx) - }}, - {"delete wisp y", func(ctx context.Context) error { - deleter, err := r.Deleter() - if err != nil { - return err - } - _, err = deleter.Delete(ctx, publicops.DeleteRequest{Actor: "ivan", IDs: []string{id("y")}, Force: true}) - return err - }}, - } -} - -// DoltStorageSteps are the store legs' extra steps: the storage.DoltStorage -// methods most of bd's commands still write through, including the import -// upsert (CreateIssuesWithFullOptions) whose create-versus-update choice the -// stack rewired, promotion out of the wisps table, and RunInTransaction. -func DoltStorageSteps(s storage.DoltStorage) []Step { - return []Step{ - {"store create s1", func(ctx context.Context) error { - s1 := newIssue("s1", "sierra one", types.TypeTask) - s1.Labels = []string{"store"} - return s.CreateIssue(ctx, s1, "sam") - }}, - {"store create issues s2 s3", func(ctx context.Context) error { - return s.CreateIssues(ctx, []*types.Issue{newIssue("s2", "sierra two", types.TypeTask), newIssue("s3", "sierra three", types.TypeBug)}, "sam") - }}, - {"store import upsert s1 and new s4", func(ctx context.Context) error { - s1 := newIssue("s1", "sierra one imported", types.TypeTask) - s1.Labels = []string{"store", "imported"} - s1.UpdatedAt = fixedAt.Add(time.Hour) - s4 := newIssue("s4", "sierra four", types.TypeTask) - s4.Dependencies = []*types.Dependency{{IssueID: id("s4"), DependsOnID: id("s1"), Type: types.DepBlocks, CreatedAt: fixedAt}} - s4.Comments = []*types.Comment{{ID: "ho-imported-comment", IssueID: id("s4"), Author: "importer", Text: "imported", CreatedAt: fixedAt}} - sw := newIssue("sw", "sierra wisp", types.TypeTask) - sw.Ephemeral = true - return s.CreateIssuesWithFullOptions(ctx, []*types.Issue{s1, s4, sw}, "importer", - storage.BatchCreateOptions{SkipPrefixValidation: true}) - }}, - {"store update s2", func(ctx context.Context) error { - return s.UpdateIssue(ctx, id("s2"), map[string]interface{}{"title": "sierra two updated", "priority": 1, "assignee": "sam"}, "sam") - }}, - {"store update s2 no-op", func(ctx context.Context) error { - return s.UpdateIssue(ctx, id("s2"), map[string]interface{}{"title": "sierra two updated"}, "sam") - }}, - {"store labels", func(ctx context.Context) error { - if err := s.AddLabel(ctx, id("s2"), "added", "sam"); err != nil { - return err - } - return s.RemoveLabel(ctx, id("s1"), "store", "sam") - }}, - {"store dependencies", func(ctx context.Context) error { - if err := s.AddDependency(ctx, &types.Dependency{IssueID: id("s3"), DependsOnID: id("s2"), Type: types.DepBlocks}, "sam"); err != nil { - return err - } - return s.RemoveDependency(ctx, id("s4"), id("s1"), "sam") - }}, - {"store comment", func(ctx context.Context) error { - _, err := s.AddIssueComment(ctx, id("s3"), "sam", "store comment") - return err - }}, - {"store claim s3", func(ctx context.Context) error { - bulk, ok := s.(storage.BulkIssueStore) - if !ok { - return fmt.Errorf("%T is not a storage.BulkIssueStore", s) - } - return bulk.ClaimIssue(ctx, id("s3"), "tess") - }}, - {"store close and reopen s2", func(ctx context.Context) error { - if err := s.CloseIssue(ctx, id("s2"), "closed by store", "sam", "session-1"); err != nil { - return err - } - return s.ReopenIssue(ctx, id("s2"), "reopened by store", "sam") - }}, - {"store promote wisp sw", func(ctx context.Context) error { - bulk, ok := s.(storage.BulkIssueStore) - if !ok { - return fmt.Errorf("%T is not a storage.BulkIssueStore", s) - } - return bulk.PromoteFromEphemeral(ctx, id("sw"), "sam") - }}, - {"store transaction", func(ctx context.Context) error { - return s.RunInTransaction(ctx, "bd: historyoffequiv transaction", func(tx storage.Transaction) error { - t1 := newIssue("t1", "tango one", types.TypeTask) - if err := tx.CreateIssue(ctx, t1, "tx"); err != nil { - return err - } - if err := tx.UpdateIssue(ctx, id("t1"), map[string]interface{}{"description": "changed in tx"}, "tx"); err != nil { - return err - } - if err := tx.AddLabel(ctx, id("t1"), "tx", "tx"); err != nil { - return err - } - if err := tx.AddDependency(ctx, &types.Dependency{IssueID: id("t1"), DependsOnID: id("s1"), Type: types.DepRelated}, "tx"); err != nil { - return err - } - return tx.CloseIssue(ctx, id("s4"), "closed in tx", "tx", "") - }) - }}, - {"store delete s3", func(ctx context.Context) error { - return s.DeleteIssue(ctx, id("s3")) - }}, - } -} diff --git a/internal/storage/hook_batch_applier_test.go b/internal/storage/hook_batch_applier_test.go index 84bbbff72..92844911c 100644 --- a/internal/storage/hook_batch_applier_test.go +++ b/internal/storage/hook_batch_applier_test.go @@ -39,12 +39,6 @@ func (r *recordingBatchApplyHooks) CompleteIssueOperationMetadata(_ context.Cont func (r *recordingBatchApplyHooks) CompleteIssueOperationRelease(_ context.Context, issueID string) { r.completions = append(r.completions, "release:"+issueID) } -func (r *recordingBatchApplyHooks) CompleteIssueOperationReclaim(_ context.Context, issueID string) { - r.completions = append(r.completions, "reclaim:"+issueID) -} -func (r *recordingBatchApplyHooks) CompleteIssueOperationUpdateByID(_ context.Context, issueID string) { - r.completions = append(r.completions, "update-by-id:"+issueID) -} func issueIDOrNil(issue *types.Issue) string { if issue == nil { @@ -236,5 +230,3 @@ func (r *recordingBatchApplyIssues) CompleteIssueOperationDependency(context.Con func (r *recordingBatchApplyIssues) CompleteIssueOperationComment(context.Context, string) {} func (r *recordingBatchApplyIssues) CompleteIssueOperationMetadata(context.Context, string) {} func (r *recordingBatchApplyIssues) CompleteIssueOperationRelease(context.Context, string) {} -func (r *recordingBatchApplyIssues) CompleteIssueOperationReclaim(context.Context, string) {} -func (r *recordingBatchApplyIssues) CompleteIssueOperationUpdateByID(context.Context, string) {} diff --git a/internal/storage/hook_batch_closer.go b/internal/storage/hook_batch_closer.go index a669f0e68..c5328f91e 100644 --- a/internal/storage/hook_batch_closer.go +++ b/internal/storage/hook_batch_closer.go @@ -25,14 +25,6 @@ type hookBatchCloser struct { hooks issueOperationHooks } -func (h *HookFiringStore) BatchCloserWithPolicy(policy BatchClosePolicy) (issueops.BatchCloser, error) { - inner, err := BatchCloserWithPolicy(h.inner, policy) - if err != nil { - return nil, err - } - return &hookBatchCloser{inner: inner, hooks: h}, nil -} - // CloseBatch fires the close hook once PER LANDED ITEM, in request order, and // fires nothing for an item that refused. Per item rather than once for the // batch because a hook script is written against one issue: collapsing N @@ -67,9 +59,6 @@ func (o *hookBatchCloser) CloseBatch(ctx context.Context, request issueops.Close if outcome.Changed { o.hooks.CompleteIssueOperationClose(outcome.Issue) } - if outcome.AutoClosedMolecule != nil { - o.hooks.CompleteIssueOperationClose(outcome.AutoClosedMolecule) - } } if result.ClaimedNext != nil { o.hooks.CompleteIssueOperationUpdate(result.ClaimedNext.Issue) diff --git a/internal/storage/hook_batch_closer_test.go b/internal/storage/hook_batch_closer_test.go index 0b6419bb0..5037cee5d 100644 --- a/internal/storage/hook_batch_closer_test.go +++ b/internal/storage/hook_batch_closer_test.go @@ -30,33 +30,6 @@ type batchCloserStore struct { func (s batchCloserStore) BatchCloser() (issueops.BatchCloser, error) { return s.closer, s.err } -type policyBatchCloserStore struct { - batchCloserStore - policy BatchClosePolicy -} - -func (s *policyBatchCloserStore) BatchCloserWithPolicy(policy BatchClosePolicy) (issueops.BatchCloser, error) { - s.policy = policy - return s.BatchCloser() -} - -func TestHookFiringStoreForwardsBatchClosePolicy(t *testing.T) { - inner := &fakeBatchCloser{} - raw := &policyBatchCloserStore{batchCloserStore: batchCloserStore{closer: inner}} - store := &HookFiringStore{inner: raw} - closer, err := BatchCloserWithPolicy(store, NewBatchClosePolicy(map[string][]string{"blocked": {"external:p:c"}})) - if err != nil { - t.Fatal(err) - } - if !errors.Is(raw.policy.CheckClose("blocked", false), ErrCloseBlocked) { - t.Fatal("hook wrapper dropped external policy") - } - hooked, ok := closer.(*hookBatchCloser) - if !ok || hooked.inner != inner || hooked.hooks != store { - t.Fatalf("policy accessor lost hook layer: %T", closer) - } -} - // TestHookFiringStoreBatchCloserLayersHooksOverInner pins the recursion, for // the reason its Lifecycle sibling pins it: delegating would compile, satisfy // Storage, and silently stop running every on_close script. diff --git a/internal/storage/hook_batch_getter.go b/internal/storage/hook_batch_getter.go deleted file mode 100644 index e88239f1c..000000000 --- a/internal/storage/hook_batch_getter.go +++ /dev/null @@ -1,17 +0,0 @@ -package storage - -import ( - "github.com/steveyegge/beads/issueops" -) - -// BatchGetter returns the inner store's batch-read surface. -// -// It recurses, like every other accessor on this decorator, rather than being -// absent: the accessor set is uniform across the chain, so a caller never has -// to know which decorators a store is wearing. What it does NOT do is wrap the -// result — reading issues by id fires no completion hooks, because nothing -// completed. That is the same statement hook_graph_counter.go makes, and it is -// why this file is three lines. -func (h *HookFiringStore) BatchGetter() (issueops.BatchGetter, error) { - return h.inner.BatchGetter() -} diff --git a/internal/storage/hook_blocked_lister.go b/internal/storage/hook_blocked_lister.go deleted file mode 100644 index e4c58fa02..000000000 --- a/internal/storage/hook_blocked_lister.go +++ /dev/null @@ -1,14 +0,0 @@ -package storage - -import ( - "github.com/steveyegge/beads/issueops" -) - -// BlockedLister returns the inner store's blocked-listing surface. -// -// It recurses, like every other accessor on this decorator, and does not wrap -// the result: listing the blocked issues fires no completion hook, because -// nothing completed (hook_ready_counter.go makes the same statement). -func (h *HookFiringStore) BlockedLister() (issueops.BlockedLister, error) { - return h.inner.BlockedLister() -} diff --git a/internal/storage/hook_decorator.go b/internal/storage/hook_decorator.go index 4a352736a..3062f20a9 100644 --- a/internal/storage/hook_decorator.go +++ b/internal/storage/hook_decorator.go @@ -153,18 +153,6 @@ func RoleFiresHooks(role any) bool { // hazard, on the operation that reaches it with nothing else in the way. case *hookBatchCreator: return true - // The lease reclaimer fires the update hook once per row it reverts - // (hook_lease_reclaimer.go), so a scheduled reaper sweep is N - // subprocesses per run — the same value Releaser and ReadyClaimer are - // refused for, and the one whose caller is a clock rather than a human - // or a request. - case *hookLeaseReclaimer: - return true - // The heartbeater fires the update hook for a RE-ARM only - // (hook_heartbeater.go): rare, but a write the workspace's script runs - // for, so a server handed an unpeeled one is refused like the others. - case *hookHeartbeater: - return true } return false } @@ -208,7 +196,7 @@ func (h *HookFiringStore) CreateIssues(ctx context.Context, issues []*types.Issu h.fireHook(p.event, p.issue) } } - if h.hookConfigured(hooks.EventUpdate) { + if h.runner != nil { for _, p := range dependencyHookEvents(ctx, issues, h.inner.GetIssue, h.inner.GetDependencyRecords) { h.fireHook(p.event, p.issue) } @@ -339,19 +327,6 @@ func (h *HookFiringStore) RemoveLabel(ctx context.Context, issueID, label, actor return nil } -// RenameLabel renames a label across every issue and wisp that carries it, -// firing on_update for each touched id. -func (h *HookFiringStore) RenameLabel(ctx context.Context, oldLabel, newLabel, actor string) (renamed, merged int, ids []string, err error) { - renamed, merged, ids, err = h.inner.RenameLabel(ctx, oldLabel, newLabel, actor) - if err != nil { - return renamed, merged, ids, err - } - for _, id := range ids { - h.fireHookByID(ctx, hooks.EventUpdate, id) - } - return renamed, merged, ids, nil -} - // ── Comment mutations ─────────────────────────────────────────────── // AddIssueComment adds a comment and fires on_update. @@ -422,7 +397,7 @@ func (h *HookFiringStore) CompleteIssueOperationCreate(ctx context.Context, issu for _, pending := range createHookEvents(issue) { h.fireHook(pending.event, pending.issue) } - if !h.hookConfigured(hooks.EventUpdate) || len(dependencies) == 0 { + if h.runner == nil || len(dependencies) == 0 { return } request := &types.Issue{Dependencies: cloneDependenciesForHook(dependencies)} @@ -478,24 +453,8 @@ func (h *HookFiringStore) CompleteIssueOperationRelease(ctx context.Context, iss h.fireHookByID(ctx, hooks.EventUpdate, issueID) } -// CompleteIssueOperationReclaim fires the update hook for one row a lease -// sweep reverted, which is the event the generic update path fires for the -// same write — assignee and status are exactly the fields a reclaim moves, -// the same pair a release moves. It re-reads the row rather than taking the -// caller's, so a script sees the unheld issue the sweep produced. -func (h *HookFiringStore) CompleteIssueOperationReclaim(ctx context.Context, issueID string) { - h.fireHookByID(ctx, hooks.EventUpdate, issueID) -} - -// CompleteIssueOperationUpdateByID fires on_update for a write that names -// its row by id (a heartbeat's re-arm), reading the row back to hand it to -// the script. -func (h *HookFiringStore) CompleteIssueOperationUpdateByID(ctx context.Context, issueID string) { - h.fireHookByID(ctx, hooks.EventUpdate, issueID) -} - func (h *HookFiringStore) fireHookByID(ctx context.Context, event, id string) { - if !h.hookConfigured(event) { + if h.runner == nil { return } issue, err := h.inner.GetIssue(ctx, id) @@ -506,7 +465,7 @@ func (h *HookFiringStore) fireHookByID(ctx context.Context, event, id string) { } func (h *HookFiringStore) fireDependencyHookByID(ctx context.Context, id string) { - if !h.hookConfigured(hooks.EventUpdate) { + if h.runner == nil { return } issue, err := dependencySnapshot(ctx, id, h.inner.GetIssue, h.inner.GetDependencyRecords) @@ -516,22 +475,6 @@ func (h *HookFiringStore) fireDependencyHookByID(ctx context.Context, id string) h.runner.Run(hooks.EventUpdate, issue) } -// hookConfigured reports whether firing event could run anything. The by-id -// paths re-read the issue (and its edges) to build the hook's payload, and over -// a remote backend each of those reads is a gateway round trip a command pays -// after its write (bts-xpw.3.14.6) — for nothing, when no executable hook for -// the event exists and Run would skip it anyway. A runner that cannot say -// (a test double with Run alone) is assumed to have one. -func (h *HookFiringStore) hookConfigured(event string) bool { - if h.runner == nil { - return false - } - if probe, ok := h.runner.(interface{ HookExists(string) bool }); ok { - return probe.HookExists(event) - } - return true -} - // ── Hook tracking transaction ─────────────────────────────────────── // pendingHook records a hook to fire after transaction commit. diff --git a/internal/storage/hook_decorator_internal_test.go b/internal/storage/hook_decorator_internal_test.go index 1a8fc2fc0..8f63aae02 100644 --- a/internal/storage/hook_decorator_internal_test.go +++ b/internal/storage/hook_decorator_internal_test.go @@ -586,87 +586,3 @@ func TestHookFiringStoreIssueLifecycleTransactionReopenForwardsCommitOnly(t *tes } }) } - -// probingHookRunner is a recordingHookRunner that also answers the probe -// hookConfigured asks a real hooks.Runner, and records each event it was asked -// about. -type probingHookRunner struct { - recordingHookRunner - exists map[string]bool - probed []string -} - -func (r *probingHookRunner) HookExists(event string) bool { - r.probed = append(r.probed, event) - return r.exists[event] -} - -// countingHookStore counts the re-reads the decorator makes to build a payload. -type countingHookStore struct { - fakeHookStore - gets *int -} - -func (s countingHookStore) GetIssue(ctx context.Context, id string) (*types.Issue, error) { - *s.gets++ - return s.fakeHookStore.GetIssue(ctx, id) -} - -// The real runner answers the probe hookConfigured asks. A runner that cannot -// is assumed to hold a hook for every event, and the decorator would re-read -// after every write. -var _ interface{ HookExists(string) bool } = (*hooks.Runner)(nil) - -// TestHookFiringStoreReReadsOnlyForAnEventWithAHook (mc-5dkzp): the by-id -// paths re-read the issue to build a hook's payload, which over an http -// workspace is a round trip the command pays after its write. The decorator -// asks the runner about the event it is about to fire — that event, not -// another — and re-reads and fires only when a hook for it exists. -func TestHookFiringStoreReReadsOnlyForAnEventWithAHook(t *testing.T) { - for _, tc := range []struct { - name string - exists map[string]bool - fires bool - }{ - {"on_update exists", map[string]bool{hooks.EventUpdate: true}, true}, - {"every hook but on_update", map[string]bool{hooks.EventCreate: true, hooks.EventClose: true}, false}, - {"no hooks", nil, false}, - } { - t.Run(tc.name, func(t *testing.T) { - runner := &probingHookRunner{exists: tc.exists} - gets := 0 - inner := countingHookStore{ - fakeHookStore: fakeHookStore{issues: map[string]*types.Issue{"bd-1": {ID: "bd-1", Title: "hooked"}}}, - gets: &gets, - } - store := &HookFiringStore{DoltStorage: inner, inner: inner, runner: runner} - - store.CompleteIssueOperationComment(context.Background(), "bd-1") // re-reads the row - store.CompleteIssueOperationDependency(context.Background(), "bd-1") // re-reads the row and its edges - - if want := []string{hooks.EventUpdate, hooks.EventUpdate}; !reflect.DeepEqual(runner.probed, want) { - t.Fatalf("probed %v, want %v: the decorator must ask about the event it fires", runner.probed, want) - } - wantGets, wantEvents := 0, []string(nil) - if tc.fires { - wantGets, wantEvents = 2, []string{hooks.EventUpdate, hooks.EventUpdate} - } - if gets != wantGets || !reflect.DeepEqual(runner.events, wantEvents) { - t.Fatalf("re-read %d times and fired %v; want %d re-reads and %v", gets, runner.events, wantGets, wantEvents) - } - }) - } - - // The same through a real runner over a hooks directory with nothing in it. - gets := 0 - inner := countingHookStore{ - fakeHookStore: fakeHookStore{issues: map[string]*types.Issue{"bd-1": {ID: "bd-1"}}}, - gets: &gets, - } - store := NewHookFiringStore(inner, hooks.NewRunner(t.TempDir())) - store.CompleteIssueOperationComment(context.Background(), "bd-1") - store.CompleteIssueOperationDependency(context.Background(), "bd-1") - if gets != 0 { - t.Fatalf("with no hook installed the decorator re-read the row %d times; want none", gets) - } -} diff --git a/internal/storage/hook_heartbeater.go b/internal/storage/hook_heartbeater.go deleted file mode 100644 index 84edb18aa..000000000 --- a/internal/storage/hook_heartbeater.go +++ /dev/null @@ -1,47 +0,0 @@ -package storage - -import ( - "context" - - "github.com/steveyegge/beads/issueops" -) - -// Heartbeater returns the inner store's lease-refresh surface with this -// decorator's completion hook layered over it. -// -// A REFRESH FIRES NOTHING. It writes only the ephemeral lease row: it changes -// no field a hook script could read back, it is the hottest write in a fleet, -// and the raw HeartbeatIssue has never fired a hook. Firing on_update for it -// would run a user's subprocess every ninety seconds per worker for a row -// that did not change. -// -// A RE-ARM FIRES on_update, once. It is recorded as a mutation (row_lock, -// updated_at, an event, a journal entry, a version), the row it names is -// still there to hand a script, and the result's Rearmed is the fact that the -// row was written — the Releaser's Changed rule. -// -// It recurses instead of delegating, so a blind delegation would hand back -// the inner store's surface and silently stop firing it. -func (h *HookFiringStore) Heartbeater() (issueops.Heartbeater, error) { - inner, err := h.inner.Heartbeater() - if err != nil { - return nil, err - } - return &hookHeartbeater{inner: inner, hooks: h}, nil -} - -type hookHeartbeater struct { - inner issueops.Heartbeater - hooks issueOperationHooks -} - -// Heartbeat fires the update hook for a re-arm and nothing for a refresh or -// a refusal. -func (b *hookHeartbeater) Heartbeat(ctx context.Context, request issueops.HeartbeatRequest) (issueops.HeartbeatResult, error) { - result, err := b.inner.Heartbeat(ctx, request) - if err != nil || !result.Rearmed { - return result, err - } - b.hooks.CompleteIssueOperationUpdateByID(ctx, result.IssueID) - return result, nil -} diff --git a/internal/storage/hook_heartbeater_test.go b/internal/storage/hook_heartbeater_test.go deleted file mode 100644 index bed58c5f1..000000000 --- a/internal/storage/hook_heartbeater_test.go +++ /dev/null @@ -1,42 +0,0 @@ -package storage - -import ( - "context" - "reflect" - "testing" - - "github.com/steveyegge/beads/issueops" -) - -type stubHeartbeater struct{ result issueops.HeartbeatResult } - -func (s stubHeartbeater) Heartbeat(context.Context, issueops.HeartbeatRequest) (issueops.HeartbeatResult, error) { - return s.result, nil -} - -// TestHookHeartbeaterFiresOnlyForARearm pins hook_heartbeater.go: a refresh -// fires nothing, a re-arm (a mutation) fires on_update once. -func TestHookHeartbeaterFiresOnlyForARearm(t *testing.T) { - for _, test := range []struct { - name string - rearmed bool - want []string - }{ - {"a refresh fires nothing", false, nil}, - {"a re-arm fires on_update", true, []string{"update-by-id:bd-1"}}, - } { - t.Run(test.name, func(t *testing.T) { - recorder := &recordingIssueOperationHooks{} - hooked := &hookHeartbeater{ - inner: stubHeartbeater{result: issueops.HeartbeatResult{IssueID: "bd-1", Rearmed: test.rearmed}}, - hooks: recorder, - } - if _, err := hooked.Heartbeat(t.Context(), issueops.HeartbeatRequest{Actor: "w", IssueID: "bd-1"}); err != nil { - t.Fatal(err) - } - if !reflect.DeepEqual(recorder.completions, test.want) { - t.Fatalf("completions = %v, want %v", recorder.completions, test.want) - } - }) - } -} diff --git a/internal/storage/hook_issue_operations.go b/internal/storage/hook_issue_operations.go index 9dbcf7278..ba41e5d81 100644 --- a/internal/storage/hook_issue_operations.go +++ b/internal/storage/hook_issue_operations.go @@ -35,8 +35,6 @@ type issueOperationHooks interface { CompleteIssueOperationComment(ctx context.Context, issueID string) CompleteIssueOperationMetadata(ctx context.Context, issueID string) CompleteIssueOperationRelease(ctx context.Context, issueID string) - CompleteIssueOperationReclaim(ctx context.Context, issueID string) - CompleteIssueOperationUpdateByID(ctx context.Context, issueID string) } var _ issueOperationHooks = (*HookFiringStore)(nil) @@ -71,11 +69,6 @@ func (o *hookIssueOperations) Close(ctx context.Context, request issueops.CloseR result, err := o.inner.Close(ctx, request) if err == nil { o.hooks.CompleteIssueOperationClose(result.Issue) - // A molecule root the close also closed in its transaction is a - // close of its own, and announces as one. - if result.AutoClosedMolecule != nil { - o.hooks.CompleteIssueOperationClose(result.AutoClosedMolecule) - } } return result, err } diff --git a/internal/storage/hook_issue_operations_test.go b/internal/storage/hook_issue_operations_test.go index 77eb0b359..0c86b95ea 100644 --- a/internal/storage/hook_issue_operations_test.go +++ b/internal/storage/hook_issue_operations_test.go @@ -63,12 +63,6 @@ func (r *recordingIssueOperationHooks) CompleteIssueOperationMetadata(_ context. func (r *recordingIssueOperationHooks) CompleteIssueOperationRelease(_ context.Context, issueID string) { r.completions = append(r.completions, "release:"+issueID) } -func (r *recordingIssueOperationHooks) CompleteIssueOperationReclaim(_ context.Context, issueID string) { - r.completions = append(r.completions, "reclaim:"+issueID) -} -func (r *recordingIssueOperationHooks) CompleteIssueOperationUpdateByID(_ context.Context, issueID string) { - r.completions = append(r.completions, "update-by-id:"+issueID) -} // lifecycleStore is a DoltStorage whose only real method is IssueLifecycle. type lifecycleStore struct { diff --git a/internal/storage/hook_lease_reclaimer.go b/internal/storage/hook_lease_reclaimer.go deleted file mode 100644 index e94877f93..000000000 --- a/internal/storage/hook_lease_reclaimer.go +++ /dev/null @@ -1,42 +0,0 @@ -package storage - -import ( - "context" - - "github.com/steveyegge/beads/issueops" -) - -// LeaseReclaimer returns the inner store's lease-sweep surface with this -// decorator's completion hooks layered over it. -// -// It WRAPS rather than recursing unwrapped, for the reason Releaser does: a -// reclaim changes an issue's assignee and status per reverted row, which is -// an on_update in the vocabulary internal/hooks publishes, and the row it -// names is still there to hand a script. -// -// It recurses instead of delegating, so a blind delegation would hand back -// the inner store's surface and silently stop firing it. -func (h *HookFiringStore) LeaseReclaimer() (issueops.LeaseReclaimer, error) { - inner, err := h.inner.LeaseReclaimer() - if err != nil { - return nil, err - } - return &hookLeaseReclaimer{inner: inner, hooks: h}, nil -} - -type hookLeaseReclaimer struct { - inner issueops.LeaseReclaimer - hooks issueOperationHooks -} - -// Reclaim fires the update hook once for every row the sweep reverted. -func (r *hookLeaseReclaimer) Reclaim(ctx context.Context, request issueops.ReclaimRequest) (issueops.ReclaimResult, error) { - result, err := r.inner.Reclaim(ctx, request) - if err != nil { - return result, err - } - for _, reclaimed := range result.Reclaimed { - r.hooks.CompleteIssueOperationReclaim(ctx, reclaimed.ID) - } - return result, nil -} diff --git a/internal/storage/hook_lease_reclaimer_test.go b/internal/storage/hook_lease_reclaimer_test.go deleted file mode 100644 index a3d8506df..000000000 --- a/internal/storage/hook_lease_reclaimer_test.go +++ /dev/null @@ -1,83 +0,0 @@ -package storage - -import ( - "context" - "errors" - "reflect" - "testing" - - "github.com/steveyegge/beads/issueops" -) - -type fakeLeaseReclaimer struct { - result issueops.ReclaimResult - err error -} - -func (f *fakeLeaseReclaimer) Reclaim(context.Context, issueops.ReclaimRequest) (issueops.ReclaimResult, error) { - return f.result, f.err -} - -// TestHookLeaseReclaimerFiresOncePerReclaimedRow is the decorator's whole -// subject: one on_update completion for every row the sweep reverted, in the -// sweep's order, and none at all for a refusal — even a refusal that somehow -// carries rows, because the role leaves its result unspecified on error. -func TestHookLeaseReclaimerFiresOncePerReclaimedRow(t *testing.T) { - for _, test := range []struct { - name string - inner *fakeLeaseReclaimer - wantFire []string - }{ - { - name: "every reverted row fires once", - inner: &fakeLeaseReclaimer{result: issueops.ReclaimResult{Reclaimed: []issueops.ReclaimedLease{ - {ID: "bd-1"}, {ID: "bd-2"}, - }}}, - wantFire: []string{"reclaim:bd-1", "reclaim:bd-2"}, - }, - { - name: "a sweep that reverted nothing fires nothing", - inner: &fakeLeaseReclaimer{result: issueops.ReclaimResult{Reclaimed: []issueops.ReclaimedLease{}}}, - }, - { - name: "a refusal fires nothing", - inner: &fakeLeaseReclaimer{err: errors.New("boom")}, - }, - { - name: "a refusal that carries rows still fires nothing", - inner: &fakeLeaseReclaimer{ - result: issueops.ReclaimResult{Reclaimed: []issueops.ReclaimedLease{{ID: "bd-1"}}}, - err: errors.New("boom"), - }, - }, - } { - t.Run(test.name, func(t *testing.T) { - recorder := &recordingIssueOperationHooks{} - reclaimer := &hookLeaseReclaimer{inner: test.inner, hooks: recorder} - - _, err := reclaimer.Reclaim(context.Background(), issueops.ReclaimRequest{Actor: "reaper"}) - if (err != nil) != (test.inner.err != nil) { - t.Fatalf("Reclaim error = %v, want %v", err, test.inner.err) - } - if !reflect.DeepEqual(recorder.completions, test.wantFire) { - t.Fatalf("hooks fired = %v, want %v", recorder.completions, test.wantFire) - } - }) - } -} - -// TestHookLeaseReclaimerPassesTheResultThrough pins that the decorator reports -// exactly what the role reported, revisions included: a hook layer that -// rewrote the result would be deciding which token a caller feeds forward. -func TestHookLeaseReclaimerPassesTheResultThrough(t *testing.T) { - want := issueops.ReclaimResult{Reclaimed: []issueops.ReclaimedLease{{ID: "bd-1", PreviousOwner: "w", Revision: "42"}}} - reclaimer := &hookLeaseReclaimer{inner: &fakeLeaseReclaimer{result: want}, hooks: &recordingIssueOperationHooks{}} - - got, err := reclaimer.Reclaim(context.Background(), issueops.ReclaimRequest{Actor: "reaper"}) - if err != nil { - t.Fatalf("Reclaim error = %v", err) - } - if !reflect.DeepEqual(got, want) { - t.Fatalf("Reclaim result = %+v, want %+v", got, want) - } -} diff --git a/internal/storage/hook_molecule_stepper.go b/internal/storage/hook_molecule_stepper.go deleted file mode 100644 index 4c85daedb..000000000 --- a/internal/storage/hook_molecule_stepper.go +++ /dev/null @@ -1,31 +0,0 @@ -package storage - -import ( - "context" - - "github.com/steveyegge/beads/issueops" -) - -// MoleculeStepper returns the inner store's advance surface, wrapped so a -// claimed step fires on_update — the event every claim fires. An advance that -// claimed nothing wrote nothing and fires nothing. -func (h *HookFiringStore) MoleculeStepper() (issueops.MoleculeStepper, error) { - inner, err := h.inner.MoleculeStepper() - if err != nil { - return nil, err - } - return &hookMoleculeStepper{inner: inner, hooks: h}, nil -} - -type hookMoleculeStepper struct { - inner issueops.MoleculeStepper - hooks *HookFiringStore -} - -func (s *hookMoleculeStepper) Advance(ctx context.Context, req issueops.AdvanceRequest) (issueops.AdvanceResult, error) { - result, err := s.inner.Advance(ctx, req) - if err == nil && result.Claimed && result.NextStep != nil { - s.hooks.CompleteIssueOperationUpdate(result.NextStep) - } - return result, err -} diff --git a/internal/storage/issueops/BUILD.bazel b/internal/storage/issueops/BUILD.bazel deleted file mode 100644 index 094dd2847..000000000 --- a/internal/storage/issueops/BUILD.bazel +++ /dev/null @@ -1,314 +0,0 @@ -load("@rules_go//go:def.bzl", "go_library", "go_test") - -go_library( - name = "issueops", - srcs = [ - "aggregate.go", - "as_of.go", - "asof_read.go", - "batch_apply.go", - "batchget.go", - "batching.go", - "blocked.go", - "blocked_consistency.go", - "blocked_merge.go", - "blocked_recheck.go", - "blocked_recheck_report.go", - "blocked_role.go", - "blocked_state.go", - "blocking_annotation.go", - "bootstrap.go", - "bulk_ops.go", - "child_id.go", - "claim.go", - "claim_next.go", - "close.go", - "close_batch.go", - "close_guard.go", - "commenter.go", - "comments.go", - "commit_pending.go", - "compaction.go", - "config_helpers.go", - "config_metadata.go", - "count.go", - "create.go", - "create_batch.go", - "create_batch_cache.go", - "create_dep_batch.go", - "create_fastpath_testhook.go", - "create_only_guard.go", - "cycle_report.go", - "cycles.go", - "delete.go", - "delete_role.go", - "dep_counts.go", - "dependencies.go", - "dependency_coordination.go", - "dependency_editor.go", - "dependency_queries.go", - "dependency_tree.go", - "derivedid.go", - "diff.go", - "edge_counts.go", - "edges.go", - "epic_closure.go", - "epoch_cas.go", - "errors.go", - "events.go", - "execution.go", - "federation.go", - "filters.go", - "gate_fields.go", - "get_issue.go", - "heartbeat_role.go", - "helpers.go", - "history.go", - "history_presence.go", - "identity.go", - "journal.go", - "journal_autoprune.go", - "journal_dependencies.go", - "journal_prune.go", - "labels.go", - "lease.go", - "lease_reclaimer.go", - "metadata.go", - "metadata_cas.go", - "molecule.go", - "persistence.go", - "promote.go", - "provenance.go", - "public_claim.go", - "public_create.go", - "public_hydration.go", - "public_snapshot.go", - "ready_work.go", - "ready_work_counts.go", - "ready_work_probe.go", - "relations.go", - "release_role.go", - "reopen.go", - "scan.go", - "search.go", - "search_counts.go", - "stale.go", - "statistics.go", - "sweep.go", - "tree_walk.go", - "unclaim.go", - "update.go", - "update_cas.go", - "version.go", - "version_commit.go", - "version_history.go", - "version_history_scan.go", - "version_list.go", - "wake_defers.go", - "wisp_filter_convert.go", - "wisp_routing.go", - ], - importpath = "github.com/steveyegge/beads/internal/storage/issueops", - visibility = ["//:__subpackages__"], - deps = [ - "//internal/config", - "//internal/debug", - "//internal/idgen", - "//internal/storage", - "//internal/storage/dberrors", - "//internal/storage/depid", - "//internal/storage/domain", - "//internal/storage/rowid", - "//internal/storage/sqlbuild", - "//internal/types", - "//internal/utils", - "//internal/workapi", - "//issueops", - "@com_github_dolthub_go_mysql_server//sql", - "@com_github_go_sql_driver_mysql//:mysql", - "@com_github_gowebpki_jcs//:jcs", - "@io_opentelemetry_go_otel//:otel", - "@io_opentelemetry_go_otel_metric//:metric", - ], -) - -go_test( - name = "issueops_test", - srcs = [ - "aggregate_authorize_notes_test.go", - "aggregate_authorize_transfer_test.go", - "as_of_test.go", - "asof_read_test.go", - "batch_apply_test.go", - "batchget_test.go", - "batching_test.go", - "blocked_consistency_test.go", - "blocked_recheck_test.go", - "blocked_state_result_test.go", - "blocked_state_template_test.go", - "close_guard_test.go", - "close_test.go", - "config_helpers_test.go", - "config_prefix_test.go", - "counts_missing_table_test.go", - "create_batch_cache_test.go", - "create_only_errors_test.go", - "create_only_guard_test.go", - "create_test.go", - "custom_types_parity_test.go", - "cycle_report_test.go", - "cycles_test.go", - "delete_cascade_tables_test.go", - "dependencies_test.go", - "dependency_coordination_test.go", - "dependency_queries_test.go", - "dependency_tree_test.go", - "edge_counts_test.go", - "edge_role_requests_test.go", - "epoch_cas_test.go", - "execution_test.go", - "filters_test.go", - "gate_fields_test.go", - "get_issue_test.go", - "helpers_test.go", - "identity_parity_test.go", - "journal_autoprune_test.go", - "journal_completeness_test.go", - "journal_prune_test.go", - "journal_truncation_test.go", - "journal_vocabulary_test.go", - "label_rename_merged_count_test.go", - "lease_reclaimer_test.go", - "lease_replica_test.go", - "metadata_test.go", - "prepare_timestamps_utc_test.go", - "public_claim_test.go", - "public_create_test.go", - "public_hydration_test.go", - "public_snapshot_test.go", - "ready_work_missing_table_test.go", - "ready_work_test.go", - "reopen_test.go", - "role_requests_test.go", - "row_lock_guard_test.go", - "scan_test.go", - "search_counts_test.go", - "search_goside_sort_test.go", - "search_lite_merge_test.go", - "search_missing_table_test.go", - "search_summary_merge_test.go", - "search_test.go", - "tree_walk_test.go", - "update_closed_fields_test.go", - "update_force_close_policy_test.go", - "update_mergeops_test.go", - "update_test.go", - "update_validation_test.go", - "version_completeness_test.go", - "version_history_durable_state_test.go", - "version_history_integer_admission_test.go", - "version_history_number_admission_test.go", - "version_history_preenable_scan_test.go", - "version_history_test.go", - "wake_defers_test.go", - "wisp_routing_test.go", - ], - data = glob(["testdata/**"]) + [ - ":go_srcs", - "//internal/storage/dolt:go_srcs", - "//internal/storage/domain/db:go_srcs", - "//internal/storage/schema:migrations", - "//internal/storage/versioncontrolops:go_srcs", - ], - embed = [":issueops"], - deps = [ - "//internal/config", - "//internal/storage", - "//internal/storage/depid", - "//internal/storage/domain", - "//internal/storage/journalscan", - "//internal/storage/rowid", - "//internal/storage/sqlbuild", - "//internal/storage/sqlcount", - "//internal/types", - "//internal/validation", - "//issueops", - "@com_github_data_dog_go_sqlmock//:go-sqlmock", - "@com_github_dolthub_go_mysql_server//sql", - "@com_github_go_sql_driver_mysql//:mysql", - ], -) - -# --- begin go_srcs (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) --- - -filegroup( - name = "go_srcs", - srcs = glob( - ["**/*.go"], - allow_empty = True, - ), - visibility = ["//:__subpackages__"], -) - -# --- end go_srcs --- - -# --- begin repo_files (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) --- - -filegroup( - name = "repo_doc_files", - srcs = glob( - [ - "**/*.md", - "**/*.mdx", - ], - allow_empty = True, - exclude = ["**/node_modules/**"], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_go_srcs", - srcs = glob( - ["**/*.go"], - allow_empty = True, - exclude = [ - "**/*_test.go", - "**/node_modules/**", - ], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_go_test_srcs", - srcs = glob( - ["**/*_test.go"], - allow_empty = True, - exclude = ["**/node_modules/**"], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_other_files", - srcs = glob( - ["**"], - exclude = [ - "**/*.db", - "**/*.exe", - "**/*.go", - "**/*.md", - "**/*.mdx", - "**/*.out", - "**/*.prof", - "**/*.pyc", - "**/*.test", - "**/__pycache__/**", - "**/node_modules/**", - ], - ), - visibility = ["//:__subpackages__"], -) - -# --- end repo_files --- diff --git a/internal/storage/issueops/aggregate.go b/internal/storage/issueops/aggregate.go index 047c4e763..3478e19b9 100644 --- a/internal/storage/issueops/aggregate.go +++ b/internal/storage/issueops/aggregate.go @@ -25,19 +25,6 @@ func (tables ChangedTables) Add(names ...string) { } } -// Sorted returns the recorded tables in name order (nil when empty). -func (tables ChangedTables) Sorted() []string { - if len(tables) == 0 { - return nil - } - out := make([]string, 0, len(tables)) - for table := range tables { - out = append(out, table) - } - sort.Strings(out) - return out -} - // Merge records every table in other. func (tables ChangedTables) Merge(other map[string]bool) { for table := range other { @@ -58,10 +45,9 @@ func UpdateFields(patch publicops.IssuePatch) map[string]interface{} { {patch.Design.Set, "design", patch.Design.Value}, {patch.AcceptanceCriteria.Set, "acceptance_criteria", patch.AcceptanceCriteria.Value}, {patch.Notes.Set, "notes", patch.Notes.Value}, - {patch.AppendNotes.Set, OpAppendNotes, patch.AppendNotes.Value}, + {patch.AppendNotes.Set, "append_notes", patch.AppendNotes.Value}, {patch.SpecID.Set, "spec_id", patch.SpecID.Value}, {patch.AwaitID.Set, "await_id", patch.AwaitID.Value}, - {patch.Waiters.Set, "waiters", PatchWaiters(patch.Waiters)}, {patch.Status.Set, "status", patch.Status.Value}, {patch.Priority.Set, "priority", patch.Priority.Value}, {patch.IssueType.Set, "issue_type", patch.IssueType.Value}, @@ -80,17 +66,6 @@ func UpdateFields(patch publicops.IssuePatch) map[string]interface{} { return updates } -// PatchWaiters is the storage value a set IssuePatch.Waiters writes: a copy of -// the list, with a nil list spelled empty so a clear stores the empty JSON -// array rather than JSON null. Both the issueops and the unit-of-work update -// bodies write through it. -func PatchWaiters(field publicops.Field[[]string]) []string { - if len(field.Value) == 0 { - return []string{} - } - return append([]string(nil), field.Value...) -} - // ValidateUpdateRequest checks mutually exclusive guarded-update options and // the canonical field values every backend must reject identically. Backends // call it before touching the row so an invalid patch cannot half-apply. @@ -101,18 +76,6 @@ func ValidateUpdateRequest(request publicops.UpdateRequest) error { if request.ForceAssigneeTransfer && (request.Claim || !request.Patch.Assignee.Set || request.ExpectedAssignee != nil) { return fmt.Errorf("%w: invalid forced assignee transfer", storage.ErrValidation) } - if request.ForceNotesOverwrite && !request.Patch.Notes.Set { - return fmt.Errorf("%w: invalid forced notes overwrite", storage.ErrValidation) - } - // Checked HERE, ahead of the notes-overwrite fence in ExecuteUpdate: a - // patch setting both Notes and AppendNotes is an invalid REQUEST, not a - // refused overwrite, and must report ErrValidation even when the notes - // fence would independently refuse the same Notes value. The uow backend - // reaches this same check through validateUpdateRequest, which both of - // its update paths run before their own fence calls, for the same reason. - if request.Patch.Notes.Set && request.Patch.AppendNotes.Set { - return fmt.Errorf("%w: cannot combine a notes replacement with %s", storage.ErrValidation, OpAppendNotes) - } patch := request.Patch if patch.Title.Set { if err := types.ValidateIssueTitle(patch.Title.Value); err != nil { @@ -129,9 +92,6 @@ func ValidateUpdateRequest(request publicops.UpdateRequest) error { return fmt.Errorf("%w: update estimated_minutes: %w", storage.ErrValidation, err) } } - if err := ValidateGatePatch(patch); err != nil { - return err - } if patch.Persistence.Set && !patch.Persistence.Value.IsValid() { return fmt.Errorf("%w: invalid persistence mode %q", storage.ErrValidation, patch.Persistence.Value) } @@ -225,63 +185,6 @@ func AuthorizeAssigneeTransfer(ctx context.Context, tx DBTX, before *types.Issue return AuthorizeAssigneeTransferWithPools(before, request, pools) } -// AuthorizeNotesOverwrite protects existing non-empty notes from an unforced -// replacement. It refuses when the patch sets Notes to a -// publicops.NotesReplacement of before.Notes (which exempts an explicit -// clear) and request.ForceNotesOverwrite is false. -// -// It applies REGARDLESS of request.ExpectedAssignee, deliberately: unlike the -// assignee fence, there is no compare-and-set that authorizes a notes -// overwrite the way a matching ExpectedAssignee authorizes an assignee -// transfer, so pairing a notes edit with an assignee guard does not exempt it. -// A STALE guard still outranks it, though — both backends check the -// compare-and-set preconditions before calling here, so a mismatch reports as -// the mismatch, never as this refusal. -// -// Enforcement boundary: this fences the issueops-contract surfaces -// (ExecuteUpdate, the uow update and batch legs). The map-based -// UpdateIssueInTx funnel does not consult it, and it has several -// notes-replacing callers, not one. They fall into classes that are all -// sighted or derived rather than blind: editor pre-fill that starts from the -// current notes (`bd edit`), read-combine-write that appends to them -// (`bd note`, `bd defer --reason`), mechanical ID rewrites of existing text -// (`bd rename`, delete-tombstoning), and explicit clears (`bd compact`). Each -// derives its new value from the current notes, so none is the blind clobber -// this fence exists to stop. `bd import` is exempt BY DESIGN (GH#6190), along with every -// other live-state fence: its contract is row replacement, guarded by its own -// staleness check (only strictly-newer rows rewrite local state; deliberate -// overwrites of newer state require --allow-stale) and reported field-by-field -// in updated_issues — the bulk-path analog of this fence's per-field --force. -func AuthorizeNotesOverwrite(before *types.Issue, request publicops.UpdateRequest) error { - if !request.Patch.Notes.Set || !publicops.NotesReplacement(before.Notes, request.Patch.Notes.Value) || request.ForceNotesOverwrite { - return nil - } - return fmt.Errorf("%w: issue %s", storage.ErrNotesOverwrite, before.ID) -} - -// AuthorizeTemplateUpdate refuses an update of a template with -// *publicops.TemplateReadOnlyError. Templates are read-only — work comes out of -// one by pouring it — so every update naming one refuses, whatever its patch -// and whatever force flags it carries: no force flag waives it (only the -// caller's explicit AllowTemplate does). -// -// It is the guard `bd update` (and assign, and the proxied mutations) used to -// apply as a pre-read in cmd/bd, which left bd serve, BatchApplier update items -// and library callers writing to templates bd refused. It sits beside the -// other update fences and is called from the same three places — ExecuteUpdate, -// and the unit-of-work update and batch legs — over the same-transaction -// pre-image, after the compare-and-set preconditions, so a stale guard still -// reports as the mismatch. -// -// request.AllowTemplate is the caller saying it edits a template deliberately -// (bd label, bd set-state); the guard stands down for that request only. -func AuthorizeTemplateUpdate(before *types.Issue, request publicops.UpdateRequest) error { - if before != nil && before.IsTemplate && !request.AllowTemplate { - return &publicops.TemplateReadOnlyError{IssueID: before.ID} - } - return nil -} - // ApplyMetadataPatch returns the canonical metadata value and whether it changes. func ApplyMetadataPatch(current json.RawMessage, patch publicops.MetadataPatch) (json.RawMessage, bool, error) { if !patch.Replace.Set && !patch.Merge.Set && len(patch.Set) == 0 && len(patch.Unset) == 0 { @@ -383,17 +286,7 @@ func metadataChanged(current, next json.RawMessage) (bool, error) { } // ApplyLabelPatch applies ordered label edits and reports whether rows changed. -// A patch that changed the label set mints ONE version row for the issue, -// after every label row it wrote; a patch that leaves the set as it was mints -// nothing. func ApplyLabelPatch(ctx context.Context, tx DBTX, current *types.Issue, patch publicops.LabelPatch, actor string) (bool, error) { - return applyLabelPatch(ctx, tx, current, patch, actor, true) -} - -// applyLabelPatch is the body of ApplyLabelPatch. mintVersion controls whether -// the patch mints its own version row; ExecuteUpdate passes false and mints -// once for the whole guarded update after its last patch. -func applyLabelPatch(ctx context.Context, tx DBTX, current *types.Issue, patch publicops.LabelPatch, actor string, mintVersion bool) (bool, error) { if !patch.Replace.Set && len(patch.Add) == 0 && len(patch.Remove) == 0 { return false, nil } @@ -440,21 +333,13 @@ func applyLabelPatch(ctx context.Context, tx DBTX, current *types.Issue, patch p if !ok { return false, fmt.Errorf("apply labels: transaction must be *sql.Tx") } - // The per-label helpers run without minting: the patch is one mutation - // and is versioned once below, after the last row, so the version carries - // the complete post-patch set rather than one row per label. for _, label := range stringSetDifference(existing, target) { - if err := removeLabelInTx(ctx, sqlTx, "", "", current.ID, label, actor, false); err != nil { + if err := RemoveLabelInTx(ctx, sqlTx, "", "", current.ID, label, actor); err != nil { return false, err } } for _, label := range stringSetDifference(target, existing) { - if err := addLabelInTx(ctx, sqlTx, "", "", current.ID, label, actor, false); err != nil { - return false, err - } - } - if mintVersion { - if err := RecordVersionInTx(ctx, tx, current.ID, actor); err != nil { + if err := AddLabelInTx(ctx, sqlTx, "", "", current.ID, label, actor); err != nil { return false, err } } @@ -468,17 +353,8 @@ type ParentPatchResult struct { WispRowsChanged bool } -// ApplyParentPatch replaces the parent-child targets and reports concrete -// changes. A patch that rewired the parent edges mints ONE version row for the -// child (the referencing issue), after the last edge write; an unchanged -// parent set mints nothing. +// ApplyParentPatch replaces the parent-child targets and reports concrete changes. func ApplyParentPatch(ctx context.Context, tx DBTX, current *types.Issue, parent publicops.Field[string], actor string) (ParentPatchResult, error) { - return applyParentPatch(ctx, tx, current, parent, actor, true) -} - -// applyParentPatch is the body of ApplyParentPatch; mintVersion is -// applyLabelPatch's, for the same reason. -func applyParentPatch(ctx context.Context, tx DBTX, current *types.Issue, parent publicops.Field[string], actor string, mintVersion bool) (ParentPatchResult, error) { if !parent.Set { return ParentPatchResult{}, nil } @@ -503,21 +379,14 @@ func applyParentPatch(ctx context.Context, tx DBTX, current *types.Issue, parent if !ok { return ParentPatchResult{}, fmt.Errorf("apply parent: transaction must be *sql.Tx") } - // The edge helpers run without minting: the patch is one mutation of the - // child's edge set and is versioned once below, after the last edge. var recomputed RecomputeIsBlockedResult for _, parentID := range stringSetDifference(existing, target) { - if _, err := removeDependencyInTx(ctx, sqlTx, current.ID, parentID, actor, false, &recomputed, false); err != nil { + if _, err := removeDependencyInTx(ctx, sqlTx, current.ID, parentID, actor, false, &recomputed); err != nil { return ParentPatchResult{}, err } } for _, parentID := range stringSetDifference(target, existing) { - if _, err := addDependencyInTx(ctx, sqlTx, &types.Dependency{IssueID: current.ID, DependsOnID: parentID, Type: types.DepParentChild}, actor, AddDependencyOpts{}, &recomputed, false); err != nil { - return ParentPatchResult{}, err - } - } - if mintVersion { - if err := RecordVersionInTx(ctx, tx, current.ID, actor); err != nil { + if _, err := addDependencyInTx(ctx, sqlTx, &types.Dependency{IssueID: current.ID, DependsOnID: parentID, Type: types.DepParentChild}, actor, AddDependencyOpts{}, &recomputed); err != nil { return ParentPatchResult{}, err } } diff --git a/internal/storage/issueops/aggregate_authorize_notes_test.go b/internal/storage/issueops/aggregate_authorize_notes_test.go deleted file mode 100644 index 5efabce59..000000000 --- a/internal/storage/issueops/aggregate_authorize_notes_test.go +++ /dev/null @@ -1,99 +0,0 @@ -package issueops - -import ( - "errors" - "strings" - "testing" - - "github.com/steveyegge/beads/internal/storage" - "github.com/steveyegge/beads/internal/types" - publicops "github.com/steveyegge/beads/issueops" -) - -// TestAuthorizeNotesOverwrite pins every branch of the notes-overwrite fence. -func TestAuthorizeNotesOverwrite(t *testing.T) { - withNotes := func(notes string) *types.Issue { - return &types.Issue{ID: "bd-1", Notes: notes} - } - notesPatch := func(mutate func(*publicops.UpdateRequest)) publicops.UpdateRequest { - request := publicops.UpdateRequest{ - Actor: "actor", - IssueID: "bd-1", - Patch: publicops.IssuePatch{Notes: publicops.Field[string]{Set: true, Value: "replacement"}}, - } - if mutate != nil { - mutate(&request) - } - return request - } - holder := "someone" - - cases := []struct { - name string - before *types.Issue - request publicops.UpdateRequest - refuse bool - }{ - { - name: "unset notes patch", - before: withNotes("original"), - request: notesPatch(func(r *publicops.UpdateRequest) { r.Patch.Notes = publicops.Field[string]{} }), - }, - { - name: "existing notes empty", - before: withNotes(""), - request: notesPatch(nil), - }, - { - name: "new value matches existing", - before: withNotes("original"), - request: notesPatch(func(r *publicops.UpdateRequest) { r.Patch.Notes.Value = "original" }), - }, - { - // An explicit clear passes unfenced, like every sibling text - // field's clear; the fence targets non-empty replacement content. - name: "explicit clear", - before: withNotes("original"), - request: notesPatch(func(r *publicops.UpdateRequest) { r.Patch.Notes.Value = "" }), - }, - { - name: "forced overwrite", - before: withNotes("original"), - request: notesPatch(func(r *publicops.UpdateRequest) { r.ForceNotesOverwrite = true }), - }, - { - // Deliberately NOT exempted by ExpectedAssignee, unlike the - // assignee fence: there is no compare-and-set that authorizes a - // notes overwrite, so pairing the two guards does not stand this - // one down. - name: "expected assignee set does not exempt it", - before: withNotes("original"), - request: notesPatch(func(r *publicops.UpdateRequest) { r.ExpectedAssignee = &holder }), - refuse: true, - }, - { - name: "unforced overwrite refuses", - before: withNotes("original"), - request: notesPatch(nil), - refuse: true, - }, - } - - for _, tc := range cases { - t.Run(tc.name, func(t *testing.T) { - err := AuthorizeNotesOverwrite(tc.before, tc.request) - if !tc.refuse { - if err != nil { - t.Fatalf("AuthorizeNotesOverwrite = %v, want nil", err) - } - return - } - if !errors.Is(err, storage.ErrNotesOverwrite) { - t.Fatalf("AuthorizeNotesOverwrite = %v, want ErrNotesOverwrite", err) - } - if !strings.Contains(err.Error(), tc.before.ID) { - t.Errorf("refusal %q omits issue %s", err, tc.before.ID) - } - }) - } -} diff --git a/internal/storage/issueops/as_of_test.go b/internal/storage/issueops/as_of_test.go deleted file mode 100644 index 30b2ba994..000000000 --- a/internal/storage/issueops/as_of_test.go +++ /dev/null @@ -1,40 +0,0 @@ -package issueops - -import "testing" - -// TestValidateRefAcceptsWellFormedRefs pins ValidateRef to its documented -// contract: it checks syntax only. It is the shared ref guard for AS OF, -// dolt_diff, the fast-forward path, CommitExists' hash-prefix lookup and -// federation remote refs, so it must never reject a well-formed ref by -// guessing what the ref means. Refs spelled entirely within Dolt's [0-9a-v] -// hash alphabet are the case that matters: no shape check can tell such a -// branch name from a hash prefix, and CommitExists is documented to accept -// prefixes. The rejection cases are covered by TestValidateRef in -// internal/storage/dolt. -func TestValidateRefAcceptsWellFormedRefs(t *testing.T) { - valid := []string{ - "main", - "release/v2.0", - "feature/auth.flow", - "wip/my-feature", - // 16-character branch names spelled entirely within Dolt's [0-9a-v] - // hash alphabet, so nothing about their shape separates them from a - // hash prefix. - "productionmirror", - "releasecandidate", - "integrationtests", - // Hash prefixes, which CommitExists resolves with a LIKE match. - "0123456789abcdef", - "0123456789abcdefghijklmnopqrstu", - // A full 32-character Dolt commit hash. - "0123456789abcdefghijklmnopqrstuv", - } - - for _, ref := range valid { - t.Run(ref, func(t *testing.T) { - if err := ValidateRef(ref); err != nil { - t.Errorf("ValidateRef(%q) = %v, want nil", ref, err) - } - }) - } -} diff --git a/internal/storage/issueops/asof_read.go b/internal/storage/issueops/asof_read.go deleted file mode 100644 index b71f1f724..000000000 --- a/internal/storage/issueops/asof_read.go +++ /dev/null @@ -1,286 +0,0 @@ -package issueops - -import ( - "context" - "database/sql" - "encoding/json" - "errors" - "fmt" - "strconv" - "strings" - "time" -) - -// This file implements R7.1 as-of read (gastownhall/beads#5898 revision 9, -// gastownhall/beads#6136; conformance suite: backend/conformance/ -// versioned_read_contract.go): given an instant T or a version address, it -// returns one issue's durable state as of that point -- the latest version -// accepted at or before T, with everything later excluded -- marking the -// address it served, or refuses as gone or unknown with the reason (R8, -// R20). It reads issue_versions rows minted by RecordVersionInTx / -// RecordVersionAtInTx (version_history.go); it adds no writer of its own for -// the mutation path. -// -// NOT part of this primitive (R7.1's own text): memory-plane consumer -// policy -- closed-before-T boundary, sibling exclusion, supersession -// exclusion (gastownhall/beads#5877). Those are layered on top by a -// consumer, not implemented here. -// -// ALL THREE LEGS SHARE THIS BODY, the same way CompareAndSetMetadataKeyInTx, -// R16's CompareAndSetVersionInTx, and R20's epoch_cas.go do: the two -// Dolt-backed stores wrap it in their own transaction, and the -// unit-of-work provider reaches it through its own leg-specific adapter. -// Each leg's own contract-test file is the translation boundary to and from -// backend/conformance's Address/Restriction vocabulary -- this file knows -// nothing about the conformance package (same package-boundary precedent as -// epoch_cas.go's EpochRestriction). -// -// removed_at / removed_reason (migration 0067, virgin and unused before this -// slice) plus removed_restriction (migration 0071, added by this slice) are -// the durable "this version row was removed" marker AsOfReadInTx reads. -// R7.1 only READS them here; a future R17 (not yet implemented on this -// branch) will be the production WRITER, via real hold/remove/erase -// primitives -- no collision, since this file never writes them outside the -// test-support seam below. -// -// STOREID IS ACCEPTED BUT NOT A FILTER, the same way CurrentEpochInTx -// accepts one: issue_versions has no store_id column, and a store's rows are -// already exactly the rows in that store's own database (one logical store -// per database, matching production reality). The parameter exists only so -// this function's signature matches AsOfReadFixture.Resolve's hook shape, -// which the conformance suite's own ad-hoc dispatch string requires (Design -// D: the suite invents storeID values to select among N genuinely isolated -// per-leg test stores; the leg fixture, not this function, does that -// isolation). - -// AsOfRestriction is this file's own local answer vocabulary for R7.1's -// refusal path -- deliberately not backend/conformance.Restriction (see the -// package-boundary note above). Unlike R20's EpochRestriction (int-backed: -// R20 never persists a restriction, it recomputes GoneReorganization at read -// time from an epoch comparison), this type is STRING-backed: it is written -// directly into issue_versions.removed_restriction and read back with a -// plain cast, no separate parse/format step needed. AsOfRestrictionLive -// exists only for API symmetry with EpochRestriction's shape -- it is never -// persisted (Live is the absence of a value, removed_at IS NULL, not a -// stored one) and AsOfReadInTx never returns it. -type AsOfRestriction string - -const ( - AsOfRestrictionLive AsOfRestriction = "live" - AsOfRestrictionGoneRetention AsOfRestriction = "gone_retention" - AsOfRestrictionGoneErasure AsOfRestriction = "gone_erasure" - AsOfRestrictionGoneReorganization AsOfRestriction = "gone_reorganization" - AsOfRestrictionUnknown AsOfRestriction = "unknown" -) - -// NormalizeRemovedRestriction is the one place a stored removed_restriction -// becomes an answer. The column is a free VARCHAR written by whatever produced -// the row, so only the values this file's vocabulary names are handed back. -// NULL, "", "live" (never persisted: Live is the absence of removed_at) and -// any other string say nothing about why the row was removed, and are reported -// as unknown rather than passed on as if they were an answer. -// -// Everything that says why a removed version is gone goes through it -- the -// as-of read, the version listing and the label bd versions prints -- so they -// cannot answer differently for the same row. It does not decide whether the -// row was removed; that is removed_at, which a caller reads first. -func NormalizeRemovedRestriction(stored string) AsOfRestriction { - restriction := AsOfRestriction(stored) - switch restriction { - case AsOfRestrictionGoneRetention, AsOfRestrictionGoneErasure, - AsOfRestrictionGoneReorganization, AsOfRestrictionUnknown: - return restriction - default: - return AsOfRestrictionUnknown - } -} - -// AsOfSelector selects a point in an issue's version history to read: either -// a literal instant (At) or a previously-served version address (Address) -- -// exactly one populated, mirroring the duality -// backend/conformance.AsOfSelector exposes to its own callers. This file -// owns its own copy rather than importing conformance's type (see the -// package-boundary note above). -type AsOfSelector struct { - At *time.Time - Address string -} - -// AsOfReadResult is AsOfReadInTx's answer. State and Address are populated -// only on a served (non-refused) result; Restriction and Reason only on a -// refused one. -type AsOfReadResult struct { - Address string - State map[string]any - Refused bool - Restriction AsOfRestriction - Reason string -} - -// AsOfVersionAddress is the address AsOfReadInTx serves for a resolved -// revision: a deterministic token over (issueID, revision). storeID is not -// part of the token -- a version address is meaningful only against its own -// issueID's rows, and Resolve is always called with issueID already known -// as its own separate parameter (matching the conformance fixture's own hook -// shape) -- but issueID is embedded anyway for readability/debuggability, -// per the design's stated rationale. -// -// Exported so each leg's AsOfReadFixture.MintAt can build the exact same -// token its own RecordVersionAtInTx write will resolve against, without -// duplicating the "asof:%s:%d" format in three separate packages. -func AsOfVersionAddress(issueID string, revision int64) string { - return fmt.Sprintf("asof:%s:%d", issueID, revision) -} - -// parseAsOfVersionAddress reverses AsOfVersionAddress. ok is false for any -// address not shaped like one this file mints, INCLUDING one whose embedded -// issueID does not match the issueID the caller separately supplied -- a -// version address for a different issue is never valid in that caller's -// context. -func parseAsOfVersionAddress(address, issueID string) (revision int64, ok bool) { - parts := strings.SplitN(address, ":", 3) - if len(parts) != 3 || parts[0] != "asof" || parts[1] != issueID { - return 0, false - } - rev, err := strconv.ParseInt(parts[2], 10, 64) - if err != nil { - return 0, false - } - return rev, true -} - -// resolveAsOfRevisionInTx resolves selector to the one issue_versions -// revision it names, for issueID: the version-address branch parses the -// revision straight out of the token, the instant branch is -// -// SELECT revision FROM issue_versions -// WHERE issue_id = ? AND change_at <= ? -// ORDER BY change_at DESC, revision DESC LIMIT 1 -// -// -- the latest version accepted at or before T, with everything later -// strictly excluded (R7.1's own wording); revision DESC is a deterministic -// tie-break for two versions minted in the same instant. found is false -// when no row resolves either way -- the address named nothing this store -// holds, or no version of issueID existed yet at T -- which the caller -// reports as Unknown, never substituting a neighboring version (R8). -func resolveAsOfRevisionInTx(ctx context.Context, tx DBTX, issueID string, selector AsOfSelector) (revision int64, found bool, err error) { - if selector.Address != "" { - rev, ok := parseAsOfVersionAddress(selector.Address, issueID) - if !ok { - return 0, false, nil - } - var exists int - err := tx.QueryRowContext(ctx, - `SELECT 1 FROM issue_versions WHERE issue_id = ? AND revision = ?`, issueID, rev, - ).Scan(&exists) - if errors.Is(err, sql.ErrNoRows) { - return 0, false, nil - } - if err != nil { - return 0, false, fmt.Errorf("as-of read: resolve address for %s: %w", issueID, err) - } - return rev, true, nil - } - - if selector.At == nil { - return 0, false, fmt.Errorf("as-of read: selector for %s has neither an instant nor a version address", issueID) - } - var rev int64 - err = tx.QueryRowContext(ctx, - `SELECT revision FROM issue_versions WHERE issue_id = ? AND change_at <= ? ORDER BY change_at DESC, revision DESC LIMIT 1`, - issueID, *selector.At, - ).Scan(&rev) - if errors.Is(err, sql.ErrNoRows) { - return 0, false, nil - } - if err != nil { - return 0, false, fmt.Errorf("as-of read: resolve instant for %s: %w", issueID, err) - } - return rev, true, nil -} - -// AsOfReadInTx answers R7.1's as-of read for issueID under selector: it -// resolves selector to one issue_versions row (resolveAsOfRevisionInTx), -// then reports that SAME row's own removed_at/removed_reason/ -// removed_restriction. Asking as-of an earlier T than when a later removal -// happened is therefore not a refusal -- the earlier row itself was never -// marked removed, only whichever row a later MarkLatestVersionRemovedInTx -// call touched was. -// -// storeID is accepted only for signature parity (see the package doc above); -// it plays no role in resolution or scoping. -func AsOfReadInTx(ctx context.Context, tx DBTX, storeID, issueID string, selector AsOfSelector) (AsOfReadResult, error) { - _ = storeID - - revision, found, err := resolveAsOfRevisionInTx(ctx, tx, issueID, selector) - if err != nil { - return AsOfReadResult{}, err - } - if !found { - return AsOfReadResult{ - Refused: true, - Restriction: AsOfRestrictionUnknown, - Reason: fmt.Sprintf("no version of %s found at or before the requested point", issueID), - }, nil - } - - var durableState []byte - var removedAt sql.NullTime - var removedReason sql.NullString - var removedRestriction sql.NullString - err = tx.QueryRowContext(ctx, - `SELECT durable_state, removed_at, removed_reason, removed_restriction - FROM issue_versions WHERE issue_id = ? AND revision = ?`, - issueID, revision, - ).Scan(&durableState, &removedAt, &removedReason, &removedRestriction) - if errors.Is(err, sql.ErrNoRows) { - return AsOfReadResult{ - Refused: true, - Restriction: AsOfRestrictionUnknown, - Reason: fmt.Sprintf("no version of %s found at or before the requested point", issueID), - }, nil - } - if err != nil { - return AsOfReadResult{}, fmt.Errorf("as-of read: load revision %d for %s: %w", revision, issueID, err) - } - - if removedAt.Valid { - return AsOfReadResult{ - Refused: true, - Restriction: NormalizeRemovedRestriction(removedRestriction.String), - Reason: removedReason.String, - }, nil - } - - state := map[string]any{} - if len(durableState) > 0 { - if err := json.Unmarshal(durableState, &state); err != nil { - return AsOfReadResult{}, fmt.Errorf("as-of read: unmarshal durable state for %s revision %d: %w", issueID, revision, err) - } - } - return AsOfReadResult{Address: AsOfVersionAddress(issueID, revision), State: state}, nil -} - -// MarkLatestVersionRemovedInTx is a test-support seam for R7.1's -// MakeUnanswerable fixture hook: it marks issueID's LATEST version row (by -// revision, not by time) as removed under restriction, independent of -// R17/R20's real (not yet implemented) hold/remove/erase primitives. -// "Latest" is correct for every AsOfReadContract case: each mints exactly -// one version before calling this, so the latest row and the row under test -// always coincide. Production code never calls this; only per-leg -// AsOfReadFixture.MakeUnanswerable implementations do. -func MarkLatestVersionRemovedInTx(ctx context.Context, tx DBTX, issueID string, restriction AsOfRestriction, reason string) error { - var revision int64 - if err := tx.QueryRowContext(ctx, - `SELECT revision FROM issue_versions WHERE issue_id = ? ORDER BY revision DESC LIMIT 1`, issueID, - ).Scan(&revision); err != nil { - return fmt.Errorf("as-of read: find latest version for %s: %w", issueID, err) - } - if _, err := tx.ExecContext(ctx, - `UPDATE issue_versions SET removed_at = ?, removed_reason = ?, removed_restriction = ? WHERE issue_id = ? AND revision = ?`, - time.Now().UTC(), reason, string(restriction), issueID, revision, - ); err != nil { - return fmt.Errorf("as-of read: mark version removed for %s: %w", issueID, err) - } - return nil -} diff --git a/internal/storage/issueops/asof_read_test.go b/internal/storage/issueops/asof_read_test.go deleted file mode 100644 index eb7f1128a..000000000 --- a/internal/storage/issueops/asof_read_test.go +++ /dev/null @@ -1,173 +0,0 @@ -package issueops - -import ( - "context" - "fmt" - "regexp" - "testing" - "time" - - "github.com/DATA-DOG/go-sqlmock" -) - -// TestAsOfReadMapsAnyStoredRestrictionOutsideTheVocabularyToUnknown is finding -// 5 of the #6661 review. removed_restriction is a free VARCHAR(30) written by -// whatever produced the row, and AsOfReadInTx handed it back as an -// AsOfRestriction by a plain cast. The type's doc says the answer is one of four -// values and that AsOfRestrictionLive is never returned (Live is the absence of -// removed_at, not a stored value), yet "live" and any other string passed -// straight through, so a caller switching on the result saw a value the -// vocabulary does not contain and could not tell it from an answer. -// -// A stored value outside the four says nothing about why the row was removed, -// so it is reported as unknown: this store cannot say. The hyphenated spelling -// is in the table because it is what the migration header used to tell a writer -// to store. -func TestAsOfReadMapsAnyStoredRestrictionOutsideTheVocabularyToUnknown(t *testing.T) { - const ( - issueID = "bd-1" - revision = int64(3) - reason = "retention window elapsed" - ) - removedAt := time.Date(2026, 9, 2, 0, 0, 0, 0, time.UTC) - - for _, tt := range []struct { - name string - stored any // the removed_restriction column; nil is SQL NULL - want AsOfRestriction - }{ - {"retention", "gone_retention", AsOfRestrictionGoneRetention}, - {"erasure", "gone_erasure", AsOfRestrictionGoneErasure}, - {"reorganization", "gone_reorganization", AsOfRestrictionGoneReorganization}, - {"unknown", "unknown", AsOfRestrictionUnknown}, - {"empty string", "", AsOfRestrictionUnknown}, - {"NULL", nil, AsOfRestrictionUnknown}, - {"live is never a stored answer", "live", AsOfRestrictionUnknown}, - {"hyphenated spelling of a real value", "gone-retention", AsOfRestrictionUnknown}, - {"arbitrary text", "quarantined", AsOfRestrictionUnknown}, - } { - t.Run(tt.name, func(t *testing.T) { - _, mock, tx := beginMockTx(t) - mock.ExpectQuery(regexp.QuoteMeta(`SELECT 1 FROM issue_versions WHERE issue_id = ? AND revision = ?`)). - WithArgs(issueID, revision). - WillReturnRows(sqlmock.NewRows([]string{"1"}).AddRow(1)) - mock.ExpectQuery(`SELECT durable_state, removed_at, removed_reason, removed_restriction\s+FROM issue_versions`). - WithArgs(issueID, revision). - WillReturnRows(sqlmock.NewRows([]string{"durable_state", "removed_at", "removed_reason", "removed_restriction"}). - AddRow([]byte("{}"), removedAt, reason, tt.stored)) - - got, err := AsOfReadInTx(context.Background(), tx, "store", issueID, - AsOfSelector{Address: AsOfVersionAddress(issueID, revision)}) - if err != nil { - t.Fatalf("AsOfReadInTx: %v", err) - } - if !got.Refused { - t.Fatalf("a version row with removed_at set must be refused, got %+v", got) - } - if got.Restriction != tt.want { - t.Errorf("stored removed_restriction %#v: Restriction = %q, want %q", tt.stored, got.Restriction, tt.want) - } - if got.Reason != reason { - t.Errorf("Reason = %q, want %q: the stored reason must survive the restriction being normalised", got.Reason, reason) - } - if err := mock.ExpectationsWereMet(); err != nil { - t.Fatalf("unmet SQL expectations: %v", err) - } - }) - } -} - -// TestListVersionsAgreesWithTheAsOfReadOnWhyAVersionWasRemoved is finding 3 of the -// #6661 review. AsOfReadInTx puts a stored removed_restriction through the four-value -// vocabulary: anything outside it, a NULL or empty column included, is unknown. The -// listing returned the column as stored, for every row, so bd versions and its JSON -// could name a restriction the as-of read never gives ("live", "quarantined", a -// hyphenated spelling), say nothing for a removed version the as-of read calls -// unknown, and attach a restriction to a version that was never removed. Both reads -// must answer the same question the same way: a removed version's restriction is one -// of the vocabulary's values, and a version that is not removed carries none. -// -// The as-of read is the oracle, run on the same stored value, so the two cannot drift -// apart again whichever one is changed. -func TestListVersionsAgreesWithTheAsOfReadOnWhyAVersionWasRemoved(t *testing.T) { - const ( - issueID = "bd-1" - revision = int64(3) - reason = "retention window elapsed" - ) - changeAt := time.Date(2026, 9, 1, 12, 0, 0, 0, time.UTC) - removedAt := time.Date(2026, 9, 2, 0, 0, 0, 0, time.UTC) - - for _, tt := range []struct { - name string - stored any // the removed_restriction column; nil is SQL NULL - }{ - {"retention", "gone_retention"}, - {"erasure", "gone_erasure"}, - {"reorganization", "gone_reorganization"}, - {"unknown", "unknown"}, - {"empty string", ""}, - {"NULL", nil}, - {"live is never a stored answer", "live"}, - {"hyphenated spelling of a real value", "gone-retention"}, - {"arbitrary text", "quarantined"}, - } { - for _, removed := range []bool{true, false} { - t.Run(fmt.Sprintf("%s/removed=%t", tt.name, removed), func(t *testing.T) { - // The listing selects COALESCE(removed_restriction, ''), so NULL reaches it as "". - listed, _ := tt.stored.(string) - var removedAtValue any // nil is SQL NULL: a version that was never removed - var storedReason string - if removed { - removedAtValue = removedAt - storedReason = reason - } - - _, mock, tx := beginMockTx(t) - mock.ExpectQuery(`FROM issue_versions\s+WHERE issue_id = \?\s+ORDER BY revision DESC`). - WithArgs(issueID). - WillReturnRows(sqlmock.NewRows([]string{ - "issue_id", "revision", "epoch", "change_actor", "change_agent", "change_message", - "change_at", "attribution_status", "removed_at", "removed_reason", "removed_restriction", "state_bytes", - }).AddRow(issueID, revision, int64(1), "", "", "", changeAt, "", removedAtValue, storedReason, listed, int64(0))) - mock.ExpectQuery(regexp.QuoteMeta(`SELECT 1 FROM issue_versions WHERE issue_id = ? AND revision = ?`)). - WithArgs(issueID, revision). - WillReturnRows(sqlmock.NewRows([]string{"1"}).AddRow(1)) - mock.ExpectQuery(`SELECT durable_state, removed_at, removed_reason, removed_restriction\s+FROM issue_versions`). - WithArgs(issueID, revision). - WillReturnRows(sqlmock.NewRows([]string{"durable_state", "removed_at", "removed_reason", "removed_restriction"}). - AddRow([]byte("{}"), removedAtValue, storedReason, tt.stored)) - - versions, err := ListVersionsInTx(context.Background(), tx, issueID) - if err != nil { - t.Fatalf("ListVersionsInTx: %v", err) - } - asOf, err := AsOfReadInTx(context.Background(), tx, "store", issueID, - AsOfSelector{Address: AsOfVersionAddress(issueID, revision)}) - if err != nil { - t.Fatalf("AsOfReadInTx: %v", err) - } - if len(versions) != 1 { - t.Fatalf("ListVersionsInTx returned %d versions, want 1", len(versions)) - } - got := versions[0].RemovedRestriction - - if removed { - if !asOf.Refused { - t.Fatalf("the as-of read of a removed version must be refused, got %+v", asOf) - } - if got != string(asOf.Restriction) { - t.Errorf("stored removed_restriction %#v: the listing says %q, the as-of read says %q; they must agree", tt.stored, got, asOf.Restriction) - } - return - } - if asOf.Refused { - t.Fatalf("the as-of read of a version that was not removed must be served, got %+v", asOf) - } - if got != "" { - t.Errorf("stored removed_restriction %#v on a version that was not removed: the listing says %q, want no restriction", tt.stored, got) - } - }) - } - } -} diff --git a/internal/storage/issueops/batch_apply.go b/internal/storage/issueops/batch_apply.go index 0a069f57b..394c207f6 100644 --- a/internal/storage/issueops/batch_apply.go +++ b/internal/storage/issueops/batch_apply.go @@ -238,10 +238,9 @@ func (r *applyBatchRun) applyItem(ctx context.Context, tx *sql.Tx, index int) er // cap. Revisit it with a measurement, not with a copy of ExecuteCreateBatch. func (r *applyBatchRun) applyCreate(ctx context.Context, tx *sql.Tx, index int, item *publicops.CreateItem) error { created, tables, err := ExecuteCreate(ctx, tx, publicops.CreateRequest{ - Actor: r.plan.Actor, - Issue: item.Issue, - ForceIDPrefix: r.plan.ForceIDPrefix, - DefaultPriority: item.DefaultPriority, + Actor: r.plan.Actor, + Issue: item.Issue, + ForceIDPrefix: r.plan.ForceIDPrefix, }) if err != nil { return &publicops.ItemError{Index: index, Kind: publicops.ItemCreate, Key: item.Key, Err: err} @@ -280,7 +279,6 @@ func (r *applyBatchRun) applyUpdate(ctx context.Context, tx *sql.Tx, index int, ExpectedAssignee: item.ExpectedAssignee, ForceClosePolicy: item.ForceClosePolicy, ForceAssigneeTransfer: item.ForceAssigneeTransfer, - ForceNotesOverwrite: item.ForceNotesOverwrite, }) if err != nil { return &publicops.ItemError{Index: index, Kind: publicops.ItemUpdate, Key: item.Target.Key, IssueID: id, Err: err} @@ -353,12 +351,6 @@ func (r *applyBatchRun) applyDepAdd(ctx context.Context, tx *sql.Tx, index int, // bound to. The planner catches only the syntactically identical pair. return itemErr(fmt.Errorf("%w: %s", publicops.ErrSelfDependency, source)) } - // The dotted-id hierarchy rule DependencyEditor applies, on the RESOLVED - // ids: a key can name a row whose minted id is a dotted child, so the planner - // cannot decide this before the creates ran. - if err := publicops.CheckDottedChildDependency(source, target, item.Type); err != nil { - return itemErr(err) - } // THE CROSS-PLANE REFUSAL IS ABOUT ROWS THIS REQUEST CREATED, and only // those. The two planes hold their edges in different tables, so an edge // between a durable row and a wisp the same batch minted is one the batch @@ -377,20 +369,11 @@ func (r *applyBatchRun) applyDepAdd(ctx context.Context, tx *sql.Tx, index int, } sourceTable, _, eventTable, depTable := WispTableRouting(sourceWisp) - metadata := item.Metadata - if item.HasSpawner { - metadata, err = StampWaitsForSpawnerID(item.Type, item.Metadata, target) - if err != nil { - return itemErr(fmt.Errorf("%w: %v", publicops.ErrValidation, err)) - } - } - dep := &types.Dependency{ IssueID: source, DependsOnID: target, Type: item.Type, - Metadata: metadata, - ThreadID: item.ThreadID, + Metadata: item.Metadata, } eventWritten, err := AddDependencyInTx(ctx, tx, dep, r.plan.Actor, AddDependencyOpts{ SourceTable: sourceTable, @@ -427,43 +410,6 @@ func (r *applyBatchRun) applyDepAdd(ctx context.Context, tx *sql.Tx, index int, return nil } -// StampWaitsForSpawnerID fills a waits-for edge's metadata with the spawner id -// this item resolved, AFTER every id this request mints exists. -// -// A GRAPH PLAN'S SPAWNER IS ALWAYS THE EDGE'S OWN TARGET — the caller cannot -// spell a waits-for item any other way, since the planner above this role -// forces spawner_key/spawner_id to match to_key/to_id before the request ever -// reaches a transaction. So the resolved target this item just computed IS the -// spawner id, known or not at the time the caller built the request: a -// plan-local Ref.Key names a row this very request is minting, and its id does -// not exist until the create item ahead of this one in Items runs. Stamping it -// here, after resolve, is what lets a waits-for edge name a same-batch spawner -// by key at all — item.Metadata alone has no way to carry an id nothing had -// minted yet. -// -// metadata.spawner_id is otherwise redundant with dependencies.depends_on_id -// (NewGraphEdgeDependency's doc states gate evaluation reads the column, never -// the blob) and this keeps it from drifting from that column regardless of -// what a caller's Metadata happened to say. -func StampWaitsForSpawnerID(depType types.DependencyType, metadata, target string) (string, error) { - if depType != types.DepWaitsFor { - return metadata, nil - } - meta := types.WaitsForMeta{} - trimmed := strings.TrimSpace(metadata) - if trimmed != "" { - if err := json.Unmarshal([]byte(trimmed), &meta); err != nil { - return "", fmt.Errorf("waits-for metadata is not a well-formed gate object: %w", err) - } - } - meta.SpawnerID = target - raw, err := json.Marshal(meta) - if err != nil { - return "", fmt.Errorf("serializing waits-for metadata: %w", err) - } - return string(raw), nil -} - // spliceMetadataRefs writes the resolved ids into the metadata of the create // items that asked for them, AFTER every id in the request exists. // @@ -488,13 +434,11 @@ func (r *applyBatchRun) spliceMetadataRefs(ctx context.Context, tx *sql.Tx) erro return err } id := r.result.Items[index].IssueID - // Unguarded for templates: the splice finishes this request's own - // create, so a template it creates is not being modified. - updated, tables, err := executeUpdate(ctx, tx, publicops.UpdateRequest{ + updated, tables, err := ExecuteUpdate(ctx, tx, publicops.UpdateRequest{ Actor: r.plan.Actor, IssueID: id, Patch: publicops.IssuePatch{Metadata: publicops.MetadataPatch{Set: set}}, - }, false) + }) if err != nil { return &publicops.ItemError{ Index: index, Kind: publicops.ItemCreate, Key: item.Create.Key, IssueID: id, Err: err, diff --git a/internal/storage/issueops/batchget.go b/internal/storage/issueops/batchget.go deleted file mode 100644 index f3686dd35..000000000 --- a/internal/storage/issueops/batchget.go +++ /dev/null @@ -1,107 +0,0 @@ -package issueops - -import ( - "context" - "fmt" - - "github.com/steveyegge/beads/internal/storage" - publicops "github.com/steveyegge/beads/issueops" -) - -// ValidateGetManyRequest applies the request rules every BatchGetter -// implementation shares. -// -// THE CAP IS CHECKED FIRST, against the request AS SENT, before -// deduplication: the obligation it bounds is reading the request apart (and, -// below the role, holding one snapshot open long enough to resolve it), not -// reading the rows the ids resolve to, so a caller cannot dodge it by -// repeating one id 1,500 times. The per-entry blank check that follows tells a -// caller's mistake from a legitimately empty request, exactly as -// ValidateEdgeCountRequest's does for EdgeCountRequest.IDs. -func ValidateGetManyRequest(request publicops.GetManyRequest) error { - if len(request.IDs) > publicops.MaxGetManyIDs { - return &publicops.TooManyIDsError{Requested: len(request.IDs), Cap: publicops.MaxGetManyIDs} - } - for i, id := range request.IDs { - if id == "" { - return fmt.Errorf("%w: get many id at position %d is empty", storage.ErrValidation, i) - } - } - return nil -} - -// DedupeGetManyIDs collapses duplicates, keeping the caller's FIRST mention of -// each id. Unlike NormalizeDeleteIDs's sibling, it does NOT trim whitespace: -// GetManyRequest.IDs are exact ids, and a caller that padded one with -// whitespace gets that id back in Missing rather than a silently corrected -// lookup, the same exactness RunGraphCounterResolvesIDsExactly pins for -// EdgeCountRequest.IDs. -func DedupeGetManyIDs(ids []string) []string { - seen := make(map[string]struct{}, len(ids)) - out := make([]string, 0, len(ids)) - for _, id := range ids { - if _, ok := seen[id]; ok { - continue - } - seen[id] = struct{}{} - out = append(out, id) - } - return out -} - -// FinishGetMany assembles the ordered answer from the ids a caller asked for -// (already deduplicated) and the issues a read actually found, keyed by id. -// -// It is a pure function beside the body for ExecuteEdgeCount's reason: the -// part that decides what the answer MEANS — an id not in found is Missing, and -// Issues/Missing both preserve the caller's order — is pinned without a -// database, and the conformance contract is left to assert what only a real -// backend can show. -func FinishGetMany(ids []string, found map[string]*publicops.Issue) publicops.GetManyResult { - result := publicops.GetManyResult{ - Issues: make([]*publicops.Issue, 0, len(ids)), - Missing: make([]string, 0), - } - for _, id := range ids { - if issue, ok := found[id]; ok { - result.Issues = append(result.Issues, issue) - continue - } - result.Missing = append(result.Missing, id) - } - return result -} - -// ExecuteGetMany returns the issues named by request.IDs from tx, in ONE -// snapshot. It is the body behind the BatchGetter accessor on ALL THREE legs: -// the two stores wrap it in their own read transaction, and the unit-of-work -// provider reaches it through the domain repository, whose runner publishes -// exactly the DBTX method set GetIssuesByIDsInTx takes. -// -// VALIDATION HAPPENS HERE, for ExecuteEdgeCount's reason: this body is the -// only body, so a leg that forgot to validate would be answering a different -// contract, and there is no second implementation for the check to belong to. -// It runs before the transaction's one query, so a request over MaxGetManyIDs -// never reaches storage. -// -// HYDRATION IS LABELS ONLY, because that is all GetIssuesByIDsInTx fills: no -// dependencies, dependents or comments. A caller that needs those reads -// Reader.Get instead, one id at a time. -func ExecuteGetMany(ctx context.Context, tx DBTX, request publicops.GetManyRequest) (publicops.GetManyResult, error) { - if err := ValidateGetManyRequest(request); err != nil { - return publicops.GetManyResult{}, err - } - ids := DedupeGetManyIDs(request.IDs) - if len(ids) == 0 { - return publicops.GetManyResult{Issues: []*publicops.Issue{}, Missing: []string{}}, nil - } - issues, err := GetIssuesByIDsInTx(ctx, tx, ids, nil) - if err != nil { - return publicops.GetManyResult{}, fmt.Errorf("get many: %w", err) - } - found := make(map[string]*publicops.Issue, len(issues)) - for _, issue := range issues { - found[issue.ID] = issue - } - return FinishGetMany(ids, found), nil -} diff --git a/internal/storage/issueops/batchget_test.go b/internal/storage/issueops/batchget_test.go deleted file mode 100644 index 70ceb16cf..000000000 --- a/internal/storage/issueops/batchget_test.go +++ /dev/null @@ -1,136 +0,0 @@ -package issueops - -import ( - "context" - "errors" - "slices" - "testing" - - "github.com/DATA-DOG/go-sqlmock" - - "github.com/steveyegge/beads/internal/storage" - publicops "github.com/steveyegge/beads/issueops" -) - -// These are the parts of issueops.BatchGetter's answer that are decided -// without a database — the request rules, the first-mention collapse and the -// ordered split into Issues and Missing — so they are pinned here in -// milliseconds and the conformance contract is left to assert what only a real -// backend can show. - -func TestValidateGetManyRequestChecksTheCapBeforeTheBlankScan(t *testing.T) { - // A request over the cap that ALSO carries a blank id has to report the - // cap: a caller told only about the blank would fix it and get a second, - // different refusal back for the same request. The blank sits at position - // 0, so a scan that ran first could not miss it. - ids := slices.Repeat([]string{"bd-1"}, publicops.MaxGetManyIDs+1) - ids[0] = "" - - err := ValidateGetManyRequest(publicops.GetManyRequest{IDs: ids}) - var tooMany *publicops.TooManyIDsError - if !errors.As(err, &tooMany) { - t.Fatalf("an over-cap request with a blank id = %v, want *TooManyIDsError", err) - } - if tooMany.Requested != len(ids) || tooMany.Cap != publicops.MaxGetManyIDs { - t.Errorf("refusal = %+v, want Requested %d and Cap %d", *tooMany, len(ids), publicops.MaxGetManyIDs) - } - if !errors.Is(err, storage.ErrValidation) { - t.Errorf("the cap refusal %v does not match ErrValidation", err) - } -} - -func TestValidateGetManyRequestRefusals(t *testing.T) { - for _, test := range []struct { - name string - ids []string - want bool - }{ - {"no ids at all", nil, false}, - {"exactly the cap's worth of mentions", slices.Repeat([]string{"bd-1"}, publicops.MaxGetManyIDs), false}, - {"one mention past the cap", slices.Repeat([]string{"bd-1"}, publicops.MaxGetManyIDs+1), true}, - {"an empty id beside a real one", []string{"bd-1", ""}, true}, - } { - err := ValidateGetManyRequest(publicops.GetManyRequest{IDs: test.ids}) - if got := errors.Is(err, storage.ErrValidation); got != test.want { - t.Errorf("%s: ErrValidation = %t (err %v), want %t", test.name, got, err, test.want) - } - } -} - -func TestDedupeGetManyIDsKeepsTheFirstMentionAndLeavesTheRequestAlone(t *testing.T) { - // The repeat of bd-b follows a different id, so a collapse that sorted, or - // that kept each id's LAST mention, would answer bd-a before bd-b. The - // padded id is a different exact id, not a duplicate to trim away. - request := []string{"bd-b", "bd-a", "bd-b", " bd-a "} - sent := slices.Clone(request) - - got := DedupeGetManyIDs(request) - if want := []string{"bd-b", "bd-a", " bd-a "}; !slices.Equal(got, want) { - t.Errorf("DedupeGetManyIDs(%q) = %q, want %q", sent, got, want) - } - // GetManyRequest promises IDs is never written through, and collapsing in - // place is the idiomatic way to break that promise. - if !slices.Equal(request, sent) { - t.Errorf("DedupeGetManyIDs rewrote the caller's slice to %q, want %q", request, sent) - } -} - -func TestFinishGetManySplitsTheRequestInItsOwnOrder(t *testing.T) { - // The id list is the order, not the found map — whose iteration order Go - // randomizes on purpose — and neither half is sorted, so a sorting - // assembly would answer differently on both. - result := FinishGetMany( - []string{"c", "ghost-2", "a", "ghost-1"}, - map[string]*publicops.Issue{"a": {ID: "a"}, "c": {ID: "c"}}, - ) - - var issueIDs []string - for _, issue := range result.Issues { - issueIDs = append(issueIDs, issue.ID) - } - if want := []string{"c", "a"}; !slices.Equal(issueIDs, want) { - t.Errorf("Issues = %q, want %q (the request's order)", issueIDs, want) - } - if want := []string{"ghost-2", "ghost-1"}; !slices.Equal(result.Missing, want) { - t.Errorf("Missing = %q, want %q (the request's order)", result.Missing, want) - } -} - -func TestFinishGetManyAnswersNeverNil(t *testing.T) { - // An all-found request answers with an empty, non-nil Missing, and an empty - // request with two: a front door that marshals them emits [] rather than - // null. - allFound := FinishGetMany([]string{"a"}, map[string]*publicops.Issue{"a": {ID: "a"}}) - if allFound.Missing == nil || len(allFound.Missing) != 0 { - t.Errorf("Missing = %#v for an all-found request, want an empty, non-nil slice", allFound.Missing) - } - - empty := FinishGetMany(nil, nil) - if empty.Issues == nil || len(empty.Issues) != 0 { - t.Errorf("Issues = %#v for an empty request, want an empty, non-nil slice", empty.Issues) - } - if empty.Missing == nil || len(empty.Missing) != 0 { - t.Errorf("Missing = %#v for an empty request, want an empty, non-nil slice", empty.Missing) - } -} - -func TestExecuteGetManyRefusesOverTheCapBeforeTheQuery(t *testing.T) { - // A mock with no expectations fails any statement it is handed, so the - // refusal is proved to come from the body's own check rather than from - // storage. The request repeats ONE id past the cap, so a body that - // collapsed repeats before validating would have let it through to the - // query instead. - db, _, err := sqlmock.New() - if err != nil { - t.Fatalf("sqlmock.New: %v", err) - } - defer db.Close() - - _, err = ExecuteGetMany(context.Background(), db, publicops.GetManyRequest{ - IDs: slices.Repeat([]string{"bd-1"}, publicops.MaxGetManyIDs+1), - }) - var tooMany *publicops.TooManyIDsError - if !errors.As(err, &tooMany) { - t.Fatalf("one id repeated past the cap = %v, want *TooManyIDsError before any query", err) - } -} diff --git a/internal/storage/issueops/blocked.go b/internal/storage/issueops/blocked.go index f25beed55..50dd87067 100644 --- a/internal/storage/issueops/blocked.go +++ b/internal/storage/issueops/blocked.go @@ -166,9 +166,37 @@ func GetDescendantIDsInTx(ctx context.Context, tx DBTX, rootID string, maxDepth } queryDescendants := func(includeWisps bool) ([]string, bool, error) { - rows, err := tx.QueryContext(ctx, - sqlbuild.DescendantWalkQuery(includeWisps), - sqlbuild.DescendantWalkArgs(rootID, maxDepth, includeWisps)...) + edgeQuery := fmt.Sprintf(` + SELECT issue_id, %s FROM dependencies WHERE type = 'parent-child' + `, DepTargetExpr) + if includeWisps { + edgeQuery += fmt.Sprintf(` + UNION ALL + SELECT issue_id, %s FROM wisp_dependencies WHERE type = 'parent-child' + `, DepTargetExpr) + } + + //nolint:gosec // G201: edgeQuery is built from hardcoded SQL plus DepTargetExpr (no user input) + query := fmt.Sprintf(` + WITH RECURSIVE + parent_edges(issue_id, depends_on_id) AS ( + %s + ), + descendants(id, depth, path) AS ( + SELECT issue_id, 1, CONCAT(',', ?, ',', issue_id, ',') + FROM parent_edges + WHERE depends_on_id = ? + UNION ALL + SELECT e.issue_id, d.depth + 1, CONCAT(d.path, e.issue_id, ',') + FROM parent_edges e + JOIN descendants d ON e.depends_on_id = d.id + WHERE (? <= 0 OR d.depth < ?) + AND LOCATE(CONCAT(',', e.issue_id, ','), d.path) = 0 + ) + SELECT id, depth FROM descendants WHERE id <> ? + `, edgeQuery) + + rows, err := tx.QueryContext(ctx, query, rootID, rootID, maxDepth, maxDepth, rootID) if err != nil { return nil, false, err } diff --git a/internal/storage/issueops/blocked_consistency.go b/internal/storage/issueops/blocked_consistency.go index 4f4b3cd03..015464647 100644 --- a/internal/storage/issueops/blocked_consistency.go +++ b/internal/storage/issueops/blocked_consistency.go @@ -265,14 +265,12 @@ func RecomputeAllIsBlockedInTx(ctx context.Context, tx DBTX) (int64, error) { // // Unlike the scoped passes, the full repair runs UNBATCHED semi-join statements // (markAllBlockedSQL/unmarkAllBlockedSQL): the id lists here cover every row by -// construction, so an IN-batch adds nothing but statement count. This was the -// first home of the decorrelated union — the batched templates' original -// OR-of-correlated-EXISTS shape ran per outer row and made the full repair -// cost ~80+ statement-seconds on fast hardware and >600s on small cloud CPUs -// (bd-t9ypt); the batched templates now use the same union scoped to their -// batch (gastownhall/beads#6288). The id lists are still captured for the -// journal snapshot, and len(wispIDs) doubles as the "wisps table exists and -// has rows" signal. +// construction, so an IN-batch adds nothing but statement count, and the +// batched templates' OR-of-correlated-EXISTS shape cannot be decorrelated by +// the engine — per-outer-row subquery execution made the full repair cost +// ~80+ statement-seconds on fast hardware and >600s on small cloud CPUs +// (bd-t9ypt). The id lists are still captured for the journal snapshot, and +// len(wispIDs) doubles as the "wisps table exists and has rows" signal. func recomputeIsBlockedCounting(ctx context.Context, tx DBTX, issueIDs, wispIDs []string) (int64, error) { if len(issueIDs) == 0 && len(wispIDs) == 0 { return 0, nil @@ -284,21 +282,15 @@ func recomputeIsBlockedCounting(ctx context.Context, tx DBTX, issueIDs, wispIDs var total int64 for { var changed int64 - // One exogeneity read per PASS, shared by every statement in it. It is - // a property of the PARENT, so the same answer serves both tables. - explained, err := subtreeExplainedParentsInTx(ctx, tx) - if err != nil { - return total, err - } if len(issueIDs) > 0 { - n, err := recomputeAllIsBlockedPassInTx(ctx, tx, "issues", "i", "dependencies", explained) + n, err := recomputeAllIsBlockedPassInTx(ctx, tx, "issues", "i", "dependencies") if err != nil { return total, err } changed += n } if len(wispIDs) > 0 { - n, err := recomputeAllIsBlockedPassInTx(ctx, tx, "wisps", "w", "wisp_dependencies", explained) + n, err := recomputeAllIsBlockedPassInTx(ctx, tx, "wisps", "w", "wisp_dependencies") if err != nil { return total, err } @@ -318,12 +310,9 @@ func recomputeIsBlockedCounting(ctx context.Context, tx DBTX, issueIDs, wispIDs // table using the unbatched semi-join statements, returning rows corrected. // Error wrapping mirrors runMarkUnmarkBatchedInTx verbatim — downstream // consumers (wh-bridge-sync among them) match on these strings. -func recomputeAllIsBlockedPassInTx( - ctx context.Context, tx DBTX, table, alias, depTable string, explained subtreeExplainedParents, -) (int64, error) { +func recomputeAllIsBlockedPassInTx(ctx context.Context, tx DBTX, table, alias, depTable string) (int64, error) { var changed int64 - res, err := tx.ExecContext(ctx, - markAllBlockedSQL(table, alias, depTable, explained), explained.args()...) + res, err := tx.ExecContext(ctx, markAllBlockedSQL(table, alias, depTable)) if err != nil { return changed, fmt.Errorf("recompute is_blocked (mark): %w", err) } @@ -333,8 +322,7 @@ func recomputeAllIsBlockedPassInTx( } changed += n - res, err = tx.ExecContext(ctx, - unmarkAllBlockedSQL(table, alias, depTable, explained), explained.args()...) + res, err = tx.ExecContext(ctx, unmarkAllBlockedSQL(table, alias, depTable)) if err != nil { return changed, fmt.Errorf("recompute is_blocked (unmark): %w", err) } @@ -350,965 +338,74 @@ func recomputeAllIsBlockedPassInTx( // same predicate, restructured so the engine can execute it as a semi-join. // //nolint:gosec // G201: table, alias, and depTable are constants from the only two callers. -func markAllBlockedSQL(table, alias, depTable string, explained subtreeExplainedParents) string { +func markAllBlockedSQL(table, alias, depTable string) string { return fmt.Sprintf(` UPDATE %[1]s %[2]s SET %[2]s.is_blocked = 1, %[2]s.updated_at = %[2]s.updated_at WHERE %[2]s.is_blocked = 0 AND %[2]s.status <> 'closed' AND %[2]s.status <> 'pinned' AND %[2]s.id IN (%[3]s) - `, table, alias, shouldBeBlockedIDsUnionPrecomputedSQL(depTable, explained)) + `, table, alias, shouldBeBlockedIDsUnionSQL(depTable)) } // unmarkAllBlockedSQL is the full-table analog of the batched unmark // template. NOT IN is null-hostile — one NULL in the subquery result makes the // predicate UNKNOWN for every row and the unmark silently stops firing — which -// is why every leg of the union carries an explicit +// is why every leg of shouldBeBlockedIDsUnionSQL carries an explicit // d.issue_id IS NOT NULL guard. // //nolint:gosec // G201: table, alias, and depTable are constants from the only two callers. -func unmarkAllBlockedSQL(table, alias, depTable string, explained subtreeExplainedParents) string { +func unmarkAllBlockedSQL(table, alias, depTable string) string { return fmt.Sprintf(` UPDATE %[1]s %[2]s SET %[2]s.is_blocked = 0, %[2]s.updated_at = %[2]s.updated_at WHERE %[2]s.is_blocked = 1 AND ( %[2]s.status = 'closed' OR %[2]s.status = 'pinned' OR %[2]s.id NOT IN (%[3]s) ) - `, table, alias, shouldBeBlockedIDsUnionPrecomputedSQL(depTable, explained)) + `, table, alias, shouldBeBlockedIDsUnionSQL(depTable)) } -// subtreeExplainedParents is one read of parentsExplainedBySubtreeSQL, pruned -// by pruneExplainedByAncestryInTx: the ids of blocked parents whose -// blockedness is solely explained by their own subtree, with nothing blocked -// above them that is not, and which therefore may not darken their children -// (gastownhall/beads#6506). The ids are split by the parent's kind. +// shouldBeBlockedIDsUnionSQL selects, UNCORRELATED with any outer row, every +// depTable issue_id that currently has a reason to be blocked: one UNION leg +// per branch of the correlated disjunction inside the batched mark/unmark +// templates in blocked_state.go, in the same order. Semantically, +// `.id IN (this select)` ≡ that disjunction: every branch +// of the disjunction correlates with the outer row ONLY through +// d.issue_id = .id, so membership of the outer id in the union of each +// branch's issue_ids is the same predicate — but the engine evaluates this +// form once as hash lookups instead of per outer row (bd-t9ypt). The lockstep +// test pins the two forms to each other. // -// WHY THE IDS AND NOT THE SQL. A spliced `p.id NOT IN (deriving query)` is -// evaluated once per leg per statement, and a full repair pass is four -// statements over two parent kinds. Measured at 1000 issues / 2870 deps: -// 46 ms on origin/main, 420 ms with the set spliced into all four, 80 ms read -// once per pass and bound as ids. The read itself is the same query either -// way; what changes is how many times a pass pays for it. The BATCHED path -// makes the same trade against its own scope rather than the whole store -// (scopedExplainedParentsInTx) — spliced there, it measured 6.9x. -// -// The set is small by construction — blocked parents that carry a blocking -// reason of their own, all of whose reasons point inside their own subtree — -// so binding it as an IN-list is bounded by the blocked-parent count, not by -// the store. -type subtreeExplainedParents struct { - issueParents []string - wispParents []string -} - -// args returns the bound ids in the order the union's legs consume them: the -// issue-parent leg (3) before the wisp-parent leg (4). A caller that splices -// the union more than once (countStaleIsBlockedSQL) repeats this list once per -// splice, in the same order. -func (e subtreeExplainedParents) args() []any { - out := make([]any, 0, len(e.issueParents)+len(e.wispParents)) - for _, id := range e.issueParents { - out = append(out, id) - } - for _, id := range e.wispParents { - out = append(out, id) - } - return out -} - -// notInPlaceholders renders the leg's exclusion for n bound ids — and nothing -// at all for none, because `NOT IN ()` is a syntax error and "exclude nothing" -// is the right reading of an empty set. -func notInPlaceholders(n int) string { - if n == 0 { - return "" - } - return "AND p.id NOT IN (" + strings.TrimSuffix(strings.Repeat("?,", n), ",") + ")" -} - -// subtreeExplainedParentsInTx reads both kinds' sets, once, for every -// unbatched statement that needs them. -// -// It is ONE read per kind and not one per dependency table because exogeneity -// is a property of the PARENT — of its own blocking reasons and its own -// subtree — and says nothing about which table the children asking about it -// live in. The candidate restriction inside the query therefore spans both -// tables (unbatchedCandSources): a parent that is a parent anywhere is -// computed once and excluded everywhere. Halving the reads is the difference -// between the doctor count landing at 1.7x origin/main and at 1.4x. -func subtreeExplainedParentsInTx(ctx context.Context, tx DBTX) (subtreeExplainedParents, error) { - var read subtreeExplainedParents - var err error - - read.issueParents, err = queryIDs(ctx, tx, - parentsExplainedBySubtreeSQL("issues", "dependencies", "depends_on_issue_id", unbatchedCandSources())) - if err != nil { - return read, fmt.Errorf("read issue parents explained by their own subtree: %w", err) - } - read.wispParents, err = queryIDs(ctx, tx, - parentsExplainedBySubtreeSQL("wisps", "wisp_dependencies", "depends_on_wisp_id", unbatchedCandSources())) - if err != nil && !isTableNotExistError(err) { - return read, fmt.Errorf("read wisp parents explained by their own subtree: %w", err) - } - // Every parent in the store was a candidate, so a blocked parent this read - // did not return is one its own reasons do not explain. - return pruneExplainedByAncestryInTx(ctx, tx, read, true) -} - -// scopedExplainedParentsInTx is subtreeExplainedParentsInTx confined to ONE -// BATCH: the same two reads, but the candidate parents are read only from the -// batch's own dependency rows, so the set is derived for the handful of -// parents above the batch instead of for every parent in the store. -// -// WHY A READ AND NOT A SPLICE. The batched mark/unmark templates used to -// splice the deriving query into both of their parent-child legs, -// batch-scoped, which reads as the cheap option: the scope confines cand to -// the batch, so the subquery is small. It is not cheap. A spliced -// `p.id NOT IN (subquery)` is paid once per leg per STATEMENT, and a batched -// pass is a mark and an unmark, so one 200-id recompute derived the set four -// times. Measured at 1000 issues / 2870 deps on embedded dolt, one pass over -// 200 ids: 53 ms on origin/main, 422 ms with the query spliced into both legs -// of both statements (6.9x, against a 1.5x bar), 61 ms read once per batch and -// bound as ids. It is the same trade the unbatched statements make one level -// up (see subtreeExplainedParents): the read is the same query either way, -// and what changes is how many times a pass pays for it. -// -// scope is the batch predicate already rendered to placeholders -// ("AND d.issue_id IN (?,?,…)") and args its ids. Both parent kinds are read -// with the same scope, because exogeneity is a property of the PARENT and the -// batch is only what bounds which parents can be asked about. -func scopedExplainedParentsInTx( - ctx context.Context, tx DBTX, depTable, scope string, args []any, -) (subtreeExplainedParents, error) { - var read subtreeExplainedParents - hasIssueParent, hasWispParent, err := batchParentKindsInTx(ctx, tx, depTable, scope, args) - if err != nil { - if isTableNotExistError(err) { - return read, nil - } - return read, err - } - cands := []candSource{{depTable: depTable, scope: scope}} - - if hasIssueParent { - read.issueParents, err = queryIDs(ctx, tx, - parentsExplainedBySubtreeSQL("issues", "dependencies", "depends_on_issue_id", cands), args...) - if err != nil { - return read, fmt.Errorf("read batch issue parents explained by their own subtree: %w", err) - } - } - if hasWispParent { - read.wispParents, err = queryIDs(ctx, tx, - parentsExplainedBySubtreeSQL("wisps", "wisp_dependencies", "depends_on_wisp_id", cands), args...) - if err != nil && !isTableNotExistError(err) { - return read, fmt.Errorf("read batch wisp parents explained by their own subtree: %w", err) - } - } - // Only the batch's own parents were candidates, so the parents above them - // are read by id as the walk climbs. - return pruneExplainedByAncestryInTx(ctx, tx, read, false) -} - -// parentRef is one parent in pruneExplainedByAncestryInTx's walk: its id and -// its kind, which picks the tables its rows live in. The two id spaces are kept -// apart rather than trusted not to collide. -type parentRef struct { - id string - wisp bool -} - -// parentEdge is one parent-child edge from child up to a parent that is -// blocked. -type parentEdge struct { - child, parent parentRef -} - -// pruneExplainedByAncestryInTx drops from a read of parentsExplainedBySubtreeSQL -// every parent that is blocked FROM ABOVE: one with a blocked parent that is -// not itself explained, at any depth. It is the contract's recursive clause -// ("…or P's own parent-child parent is exogenously blocked") for parents that -// carry reasons of their own. -// -// WHY THE FIXPOINT DOES NOT CARRY IT. The read decides from a parent's OWN -// reason rows. The legs' p.is_blocked = 1 conjunct carries inheritance only -// for a parent with no reason row: such a parent is dark exactly while -// something above it cascades into it. A parent with a subtree reason is dark -// either way, so its bit says nothing about what is above it. Q blocks on -// outside X, pc P -> Q, P blocks on its own child C: the read explains P, so C -// comes bright while the epic above it is still gated by X. A one-level check -// ("no blocked parent outside the set") is not enough either. Put a second -// close gate between them (R blocks on X, pc Q -> R, Q blocks on P, pc P -> Q, -// P blocks on C): P's blocked parent Q is in the set, so P stays explained and -// R's gate never reaches C. Whether a parent is explained depends on the whole -// chain above it, and no is_blocked bit records that chain. -// -// So this resolves it here, over the few parents the read returned: -// -// - the edges: each explained parent's blocked parents -// (blockedParentEdgesInTx); -// - membership of every blocked parent the walk meets. A complete read -// already decides it for every parent with a reason row of its own: every -// such parent in the store was a candidate, so one it did not return is -// not explained. A batch-scoped read decided only the batch's parents, so -// the walk reads the rest by id (explainedAmongInTx). A blocked parent -// with no reason row of its own is decided by neither read — its bit is -// inherited — so the walk climbs through it (withoutReasonAmongInTx) and -// lets the set below decide it; -// - the set itself (dropBlockedFromAbove): the GREATEST set of the parents -// the walk entered as explained (the read's parents, those a scoped walk -// found explained, and the no-reason parents it climbed through) all of -// whose blocked parents are in it. Start from everything entered, and -// drop any parent with a blocked parent outside the set, until nothing -// drops. Greatest, because "exogenously blocked" needs a finite chain up -// to a real outside reason, so a malformed hierarchy cycle of close gates -// with nothing exogenous above it stays explained. The greatest set is -// unique, so the order of the drops cannot change it. Only the read's -// parents are returned: the others only decide what drops. -// -// COST. Nothing when the read is empty, which covers every store with no close -// gate and every batch with none at or above it. Otherwise it is one indexed -// edge read per level the walk climbs, plus, at a level that meets blocked -// parents it has not met before, an explained read of them by id on the -// scoped path (explainedAmongInTx) and, on both paths, a reason-row read by id -// of those still undecided (withoutReasonAmongInTx), over parents that are few -// by construction. -func pruneExplainedByAncestryInTx( - ctx context.Context, tx DBTX, read subtreeExplainedParents, complete bool, -) (subtreeExplainedParents, error) { - if len(read.issueParents) == 0 && len(read.wispParents) == 0 { - return read, nil - } - refs := read.refs() - // explained holds every blocked parent the walk has met: true while it may - // still be explained (explained by its own subtree, or a no-reason parent - // the walk climbs through), false once it is known not to be. - explained := make(map[parentRef]bool, len(refs)) - for _, p := range refs { - explained[p] = true - } - above, err := climbBlockedParentsInTx(ctx, tx, refs, explained, complete) - if err != nil { - return read, err - } - dropBlockedFromAbove(explained, above) - - var out subtreeExplainedParents - for _, p := range refs { - switch { - case !explained[p]: - case p.wisp: - out.wispParents = append(out.wispParents, p.id) - default: - out.issueParents = append(out.issueParents, p.id) - } - } - return out, nil -} - -// refs lists the read's parents with their kinds, issue parents first. -func (e subtreeExplainedParents) refs() []parentRef { - out := make([]parentRef, 0, len(e.issueParents)+len(e.wispParents)) - for _, id := range e.issueParents { - out = append(out, parentRef{id: id}) - } - for _, id := range e.wispParents { - out = append(out, parentRef{id: id, wisp: true}) - } - return out -} - -// climbBlockedParentsInTx is pruneExplainedByAncestryInTx's walk: up from the -// read's parents through their blocked parents, returning the edges (child to -// its blocked parents) and entering every blocked parent it meets in -// explained. -// -// A blocked parent the walk meets is one of three things. Explained by its -// own subtree: a complete read already returned it (so it is met), a scoped -// read decides it by id (explainedAmongInTx), and the walk climbs on from it. -// Carrying a reason row of its own and not explained: final, it is -// exogenously blocked, and nothing above it can change that. Carrying NO -// reason row of its own: its bit is inherited, so it says nothing by itself -// (gastownhall/beads#7386). Such a parent is transparent — blocked from above -// exactly when one of its own blocked parents is — so the walk enters it as -// explained and climbs through it, and dropBlockedFromAbove decides it with -// the rest. Treating it as final would let a stale bit on it hold itself up: -// in a malformed hierarchy cycle it darkens the explained parent below it, -// which darkens it back, and full repair converges on the stale state. -func climbBlockedParentsInTx( - ctx context.Context, tx DBTX, frontier []parentRef, explained map[parentRef]bool, complete bool, -) (map[parentRef][]parentRef, error) { - above := make(map[parentRef][]parentRef) - for len(frontier) > 0 { - edges, err := blockedParentEdgesInTx(ctx, tx, frontier) - if err != nil { - return nil, err - } - var unknown []parentRef - for _, e := range edges { - above[e.child] = append(above[e.child], e.parent) - if _, met := explained[e.parent]; !met { - explained[e.parent] = false - unknown = append(unknown, e.parent) - } - } - if len(unknown) == 0 { - break - } - // A fresh slice: the first frontier is the caller's refs, read again - // after the walk to build the result. - var next []parentRef - if !complete { - found, err := explainedAmongInTx(ctx, tx, unknown) - if err != nil { - return nil, err - } - for _, p := range found { - explained[p] = true - next = append(next, p) - } - } - var undecided []parentRef - for _, p := range unknown { - if !explained[p] { - undecided = append(undecided, p) - } - } - transparent, err := withoutReasonAmongInTx(ctx, tx, undecided) - if err != nil { - return nil, err - } - for _, p := range transparent { - explained[p] = true - next = append(next, p) - } - frontier = next - } - return above, nil -} - -// dropBlockedFromAbove shrinks explained to the greatest set the contract -// allows: it drops any parent with a blocked parent outside the set, until -// nothing drops. -func dropBlockedFromAbove(explained map[parentRef]bool, above map[parentRef][]parentRef) { - for dropped := true; dropped; { - dropped = false - for p, ok := range explained { - if ok && !allExplained(above[p], explained) { - explained[p] = false - dropped = true - } - } - } -} - -func allExplained(parents []parentRef, explained map[parentRef]bool) bool { - for _, q := range parents { - if !explained[q] { - return false - } - } - return true -} - -// blockedParentEdgesInTx reads the parent-child edges from children up to a -// parent that is blocked. An edge lives in the child's dependency table and -// names the parent in the column for the parent's kind, so this is one lookup -// per child kind and parent kind, each keyed on the child's issue_id. The hints -// pin that drive, as on the batched legs. -// -//nolint:gosec // G201: table and column names are constants; the ids are ? placeholders. -func blockedParentEdgesInTx(ctx context.Context, tx DBTX, children []parentRef) ([]parentEdge, error) { - var issueIDs, wispIDs []string - for _, c := range children { - if c.wisp { - wispIDs = append(wispIDs, c.id) - } else { - issueIDs = append(issueIDs, c.id) - } - } - var out []parentEdge - for _, side := range []struct { - depTable string - ids []string - wisp bool - }{ - {"dependencies", issueIDs, false}, - {"wisp_dependencies", wispIDs, true}, - } { - for start := 0; start < len(side.ids); start += queryBatchSize { - end := min(start+queryBatchSize, len(side.ids)) - placeholders, args := buildSQLInClause(side.ids[start:end]) - for _, parent := range []struct { - table, col string - wisp bool - }{ - {"issues", "depends_on_issue_id", false}, - {"wisps", "depends_on_wisp_id", true}, - } { - edges, err := queryParentEdges(ctx, tx, fmt.Sprintf(` - SELECT /*+ JOIN_ORDER(d, p) LOOKUP_JOIN(d, p) */ d.issue_id, p.id - FROM %[1]s d - JOIN %[2]s p ON p.id = d.%[3]s - WHERE d.issue_id IN (%[4]s) - AND d.type = 'parent-child' - AND p.is_blocked = 1 - `, side.depTable, parent.table, parent.col, placeholders), args, side.wisp, parent.wisp) - if err != nil { - return nil, fmt.Errorf("read blocked parents of explained parents: %w", err) - } - out = append(out, edges...) - } - } - } - return out, nil -} - -// queryParentEdges runs one of blockedParentEdgesInTx's lookups and tags each -// (child, parent) row with the kinds the lookup was for. A table that does not -// exist has no edges. -func queryParentEdges( - ctx context.Context, tx DBTX, query string, args []any, childWisp, parentWisp bool, -) ([]parentEdge, error) { - rows, err := tx.QueryContext(ctx, query, args...) - if err != nil { - if isTableNotExistError(err) { - return nil, nil - } - return nil, err - } - defer func() { _ = rows.Close() }() - var out []parentEdge - for rows.Next() { - var childID, parentID string - if err := rows.Scan(&childID, &parentID); err != nil { - return nil, fmt.Errorf("scan: %w", err) - } - out = append(out, parentEdge{ - child: parentRef{id: childID, wisp: childWisp}, - parent: parentRef{id: parentID, wisp: parentWisp}, - }) - } - return out, rows.Err() -} - -// explainedAmongInTx is parentsExplainedBySubtreeSQL for the named parents, by -// id. pruneExplainedByAncestryInTx uses it for the blocked parents above a -// batch that the batch-scoped read was not asked about. -// -// Unlike the wisp reads above, it does not tolerate a missing table, because it -// cannot meet one: the prune runs only on a non-empty read, and that read came -// from the same core query, which reads both item tables and both dependency -// tables whichever kind it asks about. -func explainedAmongInTx(ctx context.Context, tx DBTX, parents []parentRef) ([]parentRef, error) { - var out []parentRef - for _, kind := range []struct { - table, depTable string - wisp bool - }{ - {"issues", "dependencies", false}, - {"wisps", "wisp_dependencies", true}, - } { - var ids []string - for _, p := range parents { - if p.wisp == kind.wisp { - ids = append(ids, p.id) - } - } - for start := 0; start < len(ids); start += queryBatchSize { - end := min(start+queryBatchSize, len(ids)) - placeholders, args := buildSQLInClause(ids[start:end]) - found, err := queryIDs(ctx, tx, - parentsExplainedBySubtreeCoreSQL(kind.table, kind.depTable, candByIDSQL(kind.table, placeholders)), - args...) - if err != nil { - return nil, fmt.Errorf("read explained parents above the batch: %w", err) - } - for _, id := range found { - out = append(out, parentRef{id: id, wisp: kind.wisp}) - } - } - } - return out, nil -} - -// withoutReasonAmongInTx reports which of parents carry NO blocking reason row -// of their own: no open blocks/conditional-blocks target and no blocking -// waits-for gate. Such a parent's is_blocked bit is inherited from above, so -// neither explained-parent read can decide it (it has no row to aggregate), -// and climbBlockedParentsInTx climbs through it instead. The reason predicate -// is the one the explained-parent reads aggregate, blockingReasonSQL, so the -// two readings of "reason row" cannot drift. -// -//nolint:gosec // G201: depTable is a constant; placeholders carries only ? placeholders. -func withoutReasonAmongInTx(ctx context.Context, tx DBTX, parents []parentRef) ([]parentRef, error) { - if len(parents) == 0 { - return nil, nil - } - carrying := make(map[parentRef]bool, len(parents)) - for _, kind := range []struct { - depTable string - wisp bool - }{ - {"dependencies", false}, - {"wisp_dependencies", true}, - } { - var ids []string - for _, p := range parents { - if p.wisp == kind.wisp { - ids = append(ids, p.id) - } - } - for start := 0; start < len(ids); start += queryBatchSize { - end := min(start+queryBatchSize, len(ids)) - placeholders, args := buildSQLInClause(ids[start:end]) - found, err := queryIDs(ctx, tx, fmt.Sprintf(` - SELECT DISTINCT pr.issue_id FROM %s pr - WHERE pr.issue_id IN (%s) - AND (%s)`, kind.depTable, placeholders, blockingReasonSQL("pr")), args...) - if err != nil { - return nil, fmt.Errorf("read blocked parents carrying a reason of their own: %w", err) - } - for _, id := range found { - carrying[parentRef{id: id, wisp: kind.wisp}] = true - } - } - } - var out []parentRef - for _, p := range parents { - if !carrying[p] { - out = append(out, p) - } - } - return out, nil -} - -// batchParentKindsInTx reports which parent KINDS the batch can possibly ask -// about: whether any parent-child row in the batch names an issue parent, and -// whether any names a wisp parent. -// -// It is the cheap guard in front of the two reads above, and it is what keeps -// the SMALL-batch case honest. A kind with no parent-child row in the batch -// has an empty candidate set, hence an empty exogeneity set, so its read is -// pure overhead — and that overhead is not small: the read is a ten-join walk -// under two CTEs, which dolt spends ~8 ms planning whether it returns rows or -// not. Most batches name no wisp parent at all, and a batch of roots (or of -// rows with no hierarchy) names neither. Measured at 1000 issues / 2870 deps, -// per pass: a 2-id batch of unparented rows 37 ms with both reads run blind -// against 20 ms with this guard, which is origin/main's own number. -// -// One indexed aggregate, both columns at once, because COUNT ignores NULLs and -// a parent-child row carries exactly one of the two. -// -//nolint:gosec // G201: depTable is a constant from the caller; scope carries only ? placeholders. -func batchParentKindsInTx( - ctx context.Context, tx DBTX, depTable, scope string, args []any, -) (bool, bool, error) { - query := fmt.Sprintf(` - SELECT COUNT(d.depends_on_issue_id), COUNT(d.depends_on_wisp_id) - FROM %[1]s d - WHERE d.issue_id IS NOT NULL %[2]s - AND d.type = 'parent-child' - `, depTable, scope) - rows, err := tx.QueryContext(ctx, query, args...) - if err != nil { - return false, false, err - } - defer func() { _ = rows.Close() }() - var issueParents, wispParents int64 - if rows.Next() { - if err := rows.Scan(&issueParents, &wispParents); err != nil { - return false, false, fmt.Errorf("scan batch parent kinds: %w", err) - } - } - if err := rows.Err(); err != nil { - return false, false, err - } - return issueParents > 0, wispParents > 0, nil -} - -// candSource names one dependency table the candidate-parent set is read from, -// with the batch scope that confines it there (empty for the unbatched reads). -type candSource struct { - depTable string - scope string -} - -// unbatchedCandSources is every table a parent-child edge can live in. The -// batched templates pass their own single scoped source instead. -func unbatchedCandSources() []candSource { - return []candSource{{depTable: "dependencies"}, {depTable: "wisp_dependencies"}} -} - -func queryIDs(ctx context.Context, tx DBTX, query string, args ...any) ([]string, error) { - rows, err := tx.QueryContext(ctx, query, args...) - if err != nil { - return nil, err - } - defer func() { _ = rows.Close() }() - var out []string - for rows.Next() { - var id string - if err := rows.Scan(&id); err != nil { - return nil, err - } - out = append(out, id) - } - return out, rows.Err() -} - -// shouldBeBlockedIDsUnionPrecomputedSQL is the union for the UNBATCHED -// statements: no batch scope, and the two parent-child legs subtract an -// already-read id list instead of re-deriving it (see -// subtreeExplainedParents). The caller binds explained.args() once per splice. -// -//nolint:gosec // G201: depTable is constant; the exclusions are ? placeholders. -func shouldBeBlockedIDsUnionPrecomputedSQL(depTable string, explained subtreeExplainedParents) string { - return shouldBeBlockedIDsUnionCoreSQL(depTable, "", - notInPlaceholders(len(explained.issueParents)), - notInPlaceholders(len(explained.wispParents))) -} - -// shouldBeBlockedIDsUnionScopedPrecomputedSQL selects, UNCORRELATED with any -// outer row, every depTable issue_id that currently has a reason to be -// blocked: one UNION leg per reason — an open blocks/conditional-blocks target -// (issue or wisp), an EXOGENOUSLY blocked parent-child parent (issue or wisp; -// see parentsExplainedBySubtreeSQL), a blocking waits-for gate. -// Semantically, `.id IN (this select)` ≡ the disjunction of those -// reasons correlated on d.issue_id = .id, which is what the batched -// mark/unmark templates in blocked_state.go once spelled out as five -// correlated EXISTS — but the engine evaluates this form once as hash -// lookups instead of per outer row (bd-t9ypt). Full repair, doctor count and -// the batched templates share the one core below. -// -// It carries an -// extra predicate on the dependency row appended to every leg's WHERE — "" for -// the whole table (full repair, doctor count), batchScopeSQL for the batched -// mark/unmark templates in blocked_state.go, which confine each leg to -// d.issue_id IN (batch) so the union stays index-sized per statement. scope is -// spliced verbatim: a %s inside it survives this Sprintf for the batch runner. -// -// Like the unbatched form above, and unlike the shape this round replaced, the -// two parent-child legs SUBTRACT AN ALREADY-READ id list rather than the query -// that derives it: the batch runner reads the set once per batch -// (scopedExplainedParentsInTx) and binds it into both statements. The caller -// binds explained.args() once per splice, interleaved with the batch ids in -// text order — expandBatchTemplate does that bookkeeping. -// -// The waits-for leg wraps its dependency rows in a DISTINCT derived table -// BEFORE the gate predicate (waitsForGateBlockedSQL, six correlated EXISTS -// over the parent-child children and the spawner) is applied, so the engine -// narrows to the leg's few waits-for rows first and evaluates the gate only -// per surviving row. Written flat, dolt 2.1.8's planner pushed the gate's -// correlated subqueries ahead of the type filter and re-ran them against the -// whole overlay for every candidate row — over a 19k-row uncommitted working -// set the flat leg took 436 s while returning zero rows; behind the derived -// table, 0.01 s (gastownhall/beads#6288, wy-pyrus4). The DISTINCT is -// semantically free (the leg feeds a UNION, and the gate depends only on the -// projected columns) and is what makes the barrier structural: a plain -// single-table derived table is the mergeable shape MySQL-dialect planners -// fold back into the outer query; a DISTINCT one is materialized. -// -// A scoped union pins each joined leg to drive from the batch's own -// dependency rows and look the target or parent up by primary key -// (JOIN_ORDER/LOOKUP_JOIN hints; on MySQL, JOIN_ORDER is honored — the order -// it forces is the one wanted — and the unknown LOOKUP_JOIN hint is ignored -// with a warning). Left to its cost model, the Dolt sql-server planner -// intermittently — depending on when its background statistics refresh -// lands — turned the issues legs around: scanning every open issue through -// the (is_blocked, status) index and probing dependencies per row. Over an -// import's ~2000-5000 uncommitted issues that is ~3 s per leg and 6-10 s per -// mark/unmark statement, where the lookup plan takes ~0.1 s; one plan flip -// early in an import of 5000 cost most of its 28 minutes and brushed the -// 10 s read timeout. The unscoped form (full repair, doctor count) walks the -// whole table and keeps the planner's choice. TestGraphWalkPlansHonorJoinHints -// (embedded) pins the hinted plans. -// -//nolint:gosec // G201: depTable and scope are constants; waitsForGateBlockedSQL is a constant template. -func shouldBeBlockedIDsUnionScopedPrecomputedSQL( - depTable, scope string, explained subtreeExplainedParents, -) string { - return shouldBeBlockedIDsUnionCoreSQL(depTable, scope, - notInPlaceholders(len(explained.issueParents)), - notInPlaceholders(len(explained.wispParents))) -} - -// shouldBeBlockedIDsUnionCoreSQL is the five legs themselves. The two -// parent-child legs take their exogeneity condition as text: both callers -// pass an already-read IN-list rendered to placeholders (notInPlaceholders), -// saying "unless this parent is explained by its own subtree", and the empty -// string says "no parent is", which is what an empty set means. -// -//nolint:gosec // G201: depTable and scope are constants; the conditions come from the two callers above. -func shouldBeBlockedIDsUnionCoreSQL(depTable, scope, issueParentCond, wispParentCond string) string { - targetHint, parentHint := "", "" - if scope != "" { - targetHint = "/*+ JOIN_ORDER(d, t) LOOKUP_JOIN(d, t) */ " - parentHint = "/*+ JOIN_ORDER(d, p) LOOKUP_JOIN(d, p) */ " - } +//nolint:gosec // G201: depTable is constant; waitsForGateBlockedSQL is a constant template. +func shouldBeBlockedIDsUnionSQL(depTable string) string { return fmt.Sprintf(` - SELECT %[6]sd.issue_id FROM %[1]s d + SELECT d.issue_id FROM %[1]s d JOIN issues t ON t.id = d.depends_on_issue_id - WHERE d.issue_id IS NOT NULL %[3]s + WHERE d.issue_id IS NOT NULL AND (d.type = 'blocks' OR d.type = 'conditional-blocks') AND t.status <> 'closed' AND t.status <> 'pinned' UNION - SELECT %[6]sd.issue_id FROM %[1]s d + SELECT d.issue_id FROM %[1]s d JOIN wisps t ON t.id = d.depends_on_wisp_id - WHERE d.issue_id IS NOT NULL %[3]s + WHERE d.issue_id IS NOT NULL AND (d.type = 'blocks' OR d.type = 'conditional-blocks') AND t.status <> 'closed' AND t.status <> 'pinned' UNION - SELECT %[7]sd.issue_id FROM %[1]s d + SELECT d.issue_id FROM %[1]s d JOIN issues p ON p.id = d.depends_on_issue_id - WHERE d.issue_id IS NOT NULL %[3]s + WHERE d.issue_id IS NOT NULL AND d.type = 'parent-child' AND p.is_blocked = 1 - %[4]s UNION - SELECT %[7]sd.issue_id FROM %[1]s d + SELECT d.issue_id FROM %[1]s d JOIN wisps p ON p.id = d.depends_on_wisp_id - WHERE d.issue_id IS NOT NULL %[3]s + WHERE d.issue_id IS NOT NULL AND d.type = 'parent-child' AND p.is_blocked = 1 - %[5]s UNION - SELECT d.issue_id FROM ( - SELECT DISTINCT d.issue_id, d.depends_on_issue_id, d.depends_on_wisp_id, d.metadata - FROM %[1]s d - WHERE d.issue_id IS NOT NULL %[3]s - AND d.type = 'waits-for' - ) d - WHERE (%[2]s) - `, depTable, waitsForGateBlockedSQL, scope, issueParentCond, wispParentCond, targetHint, parentHint) -} - -// subtreeWalkDepth is how far parentsExplainedBySubtreeSQL walks up from a -// blocking reason's target looking for the parent that holds the reason — the -// depth to which "inside my own subtree" is decided. -// -// The walk is spelled as this many pairs of LEFT JOINs rather than as WITH -// RECURSIVE, and that is a measured choice, not a portability one (dolt runs -// recursive CTEs elsewhere in this package). At 1000 issues / 2870 deps the -// seeded recursive walk cost 45 ms per evaluation against 6 ms for four levels -// of joins: a recursive CTE pays a whole query's overhead per ITERATION, so -// its cost tracks hierarchy depth even when it returns a hundred rows. A fixed -// join chain pays once, and each level is an index probe. -// -// The price is a horizon. A reason target more than this many parent-child -// edges below its blocker reads as exogenous, which is the pre-#6506 behavior -// for that one pair: it propagates MORE, never less, so nothing is hidden that -// the old code showed, and the permanent lock this fix removes needs the -// blocker and the target in the SAME subtree within the horizon. Four covers -// epic -> sub-epic -> leg -> task, which is deeper than any dotted-id -// hierarchy this store has carried, and it is the knob to turn first if a -// real store is ever found below it. -// -// BOTH SIDES OF THE HORIZON ARE PINNED, by a pair of dolt tests that differ by -// one edge: TestParentChildCascade_ReasonAtTheWalkHorizon (a reason exactly -// this many levels down; the subtree comes bright) and -// _ReasonPastTheWalkHorizon (one level further; the subtree stays dark, which -// is origin/main's own answer, so that test passes unchanged on origin/main). -// Raising this constant makes the second one fail and name the rows that -// changed. Measured cost of raising it, at 1000 issues / 2870 deps: see the -// commit that introduced the pin. -const subtreeWalkDepth = 4 - -// parentsExplainedBySubtreeSQL is the parent-child legs' exogeneity test -// (gastownhall/beads#6506): the set of blocked parents whose blockedness is -// SOLELY explained by their own subtree, which the legs subtract with -// `p.id NOT IN (...)`. It decides once per statement, uncorrelated with any -// outer row, which blocked parents may not darken their children. -// -// The bug it closes: the legs used to cascade on p.is_blocked = 1 alone, -// WHATEVER set that bit. A parent that is blocked only by its own children — -// the "close gate on the epic" idiom, where P carries blocks edges onto C1 and -// C2 so P cannot close before them — therefore darkened C1 and C2, the very -// children it was waiting for. Neither child could ever appear in bd ready, so -// neither could be worked, so neither could close: a permanent lock. P's OWN -// is_blocked stays 1 (it really does depend on open children); only the -// propagation to its children changes. -// -// The contract: a parent-child edge propagates only EXOGENOUS blockedness. P -// counts as blocked FOR CHILD C iff P has a blocking reason — an open -// blocks/conditional-blocks target, or a blocking waits-for gate — whose -// target is OUTSIDE P'S OWN SUBTREE, or P's own parent-child parent is -// (recursively) exogenously blocked. -// -// SUBTREE, not direct children. A depth-1 reading of "own child" leaves the -// permanent lock alive one level down: P blocks on grandchild G, pc C -> P, -// pc G -> C. G is not P's direct child, so P reads exogenous, so P darkens C, -// so C darkens G — and G is the only row whose close can free P. The walk up -// the ancestor chain (subtreeWalkDepth) is what makes this a descendant test. -// -// SHAPE. The caller's condition is a subtraction: -// -// AND p.id NOT IN (this select, pruned by pruneExplainedByAncestryInTx) -// -// A parent cascades unless it appears here. A parent with NO blocking reason -// row of its own is absent from this set, because the set is built from reason -// rows, so it cascades. For such a parent that is the contract's recursive -// clause, carried by the EXISTING fixpoint and not by a second pass of SQL -// recursion: it is dark because ITS parent is, so its children inherit. -// Because the legs keep the p.is_blocked = 1 conjunct, a chain converges the -// way it always did, one level per fixpoint pass, and the single-pass doctor -// COUNT stays the documented lower bound. A parent that DOES carry a reason is -// dark whatever is above it, so the fixpoint cannot carry the clause for it: -// pruneExplainedByAncestryInTx drops it from the set when a blocked parent -// above it is not itself explained. -// -// Every reason row is either inside the parent's subtree or outside it, so -// "no reason row is exogenous" (MAX(...) = 0 below) is the whole membership -// test; the two bits the shape started with collapse to one. -// -// COST (gastownhall/beads#6288, wy-pyrus4, and B2 of the #6506 review). Three -// earlier cuts were built and measured at 1000 issues / 2870 deps, and all -// three are in the rejected pile: -// -// - a per-row correlated EXISTS whose body held two more correlated EXISTS, -// the exact shape #6288 removed from the waits-for leg: 3.3-3.7x on the -// full repair, 5.2x on the doctor count (the review's own numbers); -// - the set as a derived table LEFT JOINed on p.id: ~30x. A join operand is -// a join operand — dolt planned it as the inner side of a nested loop and -// rebuilt it per outer row, the correlated shape again in a derived -// table's clothes; -// - the same set built with WITH RECURSIVE: it blew the driver read timeout -// on the full repair (see subtreeWalkDepth). -// -// What is left is uncorrelated by construction. Nothing below references the -// outer row — that is what lets the engine build the set once per statement -// and probe it, the form this file already relies on for -// `.id IN (the should-be-blocked union)` (bd-t9ypt) — and it is the -// property to preserve in any future edit here. Measured against a leg with -// no exogeneity test at all: 3 ms -> 18 ms per evaluation, flat in the number -// of parent-child rows the leg returns. -// -// The set is also confined to the statement's own work: -// -// - cand is the set of parents the caller can possibly ask about. A batched -// mark/unmark passes ONE source — its own dependency table, carrying its -// own batch scope (see scopedExplainedParentsInTx) — so it computes the -// handful of parents above the batch. The unbatched read passes both -// tables unscoped, which is every row that is a parent at all and already -// prunes most of the table. The alias must be d: batchScopeSQL is written -// against d.issue_id and is spliced verbatim. It binds to this subquery's -// own FROM, not an enclosing leg's d — the set is not lateral and -// correlates with nothing outside it. -// - rsn is those parents' blocking reason rows, and is where the reason -// predicate is paid: once per candidate parent row, not once per -// parent-child edge in the leg. -// -// blockingReasonSQL mirrors union legs 1, 2 and 5 row by row; keep the three -// in step or the "solely explained by its own subtree" reading silently -// drifts. -// -// parentTable/parentDepTable/parentCol describe the PARENT's kind (issues + -// dependencies, or wisps + wisp_dependencies); cands name the tables the -// parent-child edges are read from, which are the CHILDREN's. A parent-child -// edge lives in the child's table and names the parent in parentCol. -// -//nolint:gosec // G201: every argument is a constant from the two call sites; scope is batchScopeSQL. -func parentsExplainedBySubtreeSQL(parentTable, parentDepTable, parentCol string, cands []candSource) string { - var cand strings.Builder - for i, c := range cands { - if i > 0 { - cand.WriteString("\n\t\t UNION") - } - fmt.Fprintf(&cand, ` - SELECT DISTINCT d.%[1]s FROM %[2]s d - WHERE d.issue_id IS NOT NULL %[3]s - AND d.type = 'parent-child' - AND d.%[1]s IS NOT NULL`, parentCol, c.depTable, c.scope) - } - return parentsExplainedBySubtreeCoreSQL(parentTable, parentDepTable, cand.String()) -} - -// candByIDSQL is a cand body naming parents by id: placeholders is a rendered -// IN-list, for explainedAmongInTx. -// -//nolint:gosec // G201: parentTable is a constant; placeholders carries only ? placeholders. -func candByIDSQL(parentTable, placeholders string) string { - return fmt.Sprintf(` - SELECT c.id FROM %s c WHERE c.id IN (%s)`, parentTable, placeholders) -} - -// parentsExplainedBySubtreeCoreSQL is parentsExplainedBySubtreeSQL over a cand -// body the caller has already built: the parent-child rows of a batch or of the -// whole store (parentsExplainedBySubtreeSQL), or parents named by id -// (candByIDSQL). -// -//nolint:gosec // G201: parentTable and parentDepTable are constants; cand is built from constants and ? placeholders. -func parentsExplainedBySubtreeCoreSQL(parentTable, parentDepTable, cand string) string { - // The ancestor walk: one pair of indexed LEFT JOINs per level, on the - // dependency tables directly. Materializing the parent-child edges into a - // CTE first was measured at 6.6x on the batched write path — the whole - // edge set of the store, built per statement, to answer a question about a - // handful of rows. Joined straight onto idx_dependencies_issue it is a - // probe per level per reason row instead. - var joins, match strings.Builder - child := "r.tgt" - for i := 1; i <= subtreeWalkDepth; i++ { - ia, wa := fmt.Sprintf("i%d", i), fmt.Sprintf("w%d", i) - fmt.Fprintf(&joins, ` - LEFT JOIN dependencies %[1]s ON %[1]s.issue_id = %[3]s AND %[1]s.type = 'parent-child' - LEFT JOIN wisp_dependencies %[2]s ON %[2]s.issue_id = %[3]s AND %[2]s.type = 'parent-child'`, - ia, wa, child) - // The level's parent, whichever table and column carries it. A child - // has one parent-child edge, so at most one of these four is set. - parent := fmt.Sprintf( - "COALESCE(%[1]s.depends_on_issue_id, %[1]s.depends_on_wisp_id, %[2]s.depends_on_issue_id, %[2]s.depends_on_wisp_id)", - ia, wa) - if i > 1 { - match.WriteString(" OR ") - } - fmt.Fprintf(&match, "%s = r.pid", parent) - child = parent - } - - return fmt.Sprintf(` - WITH - cand(pid) AS (%[3]s - ), - rsn(pid, tgt) AS ( - SELECT pr.issue_id, COALESCE(pr.depends_on_issue_id, pr.depends_on_wisp_id) - FROM %[2]s pr - JOIN %[1]s pp ON pp.id = pr.issue_id - JOIN cand ON cand.pid = pr.issue_id - WHERE pp.is_blocked = 1 - AND pr.issue_id IS NOT NULL - AND (%[4]s) - ), - bits(pid, exogenous) AS ( - SELECT r.pid, MAX(CASE WHEN %[6]s THEN 0 ELSE 1 END) - FROM rsn r%[5]s - GROUP BY r.pid - ) - SELECT b.pid FROM bits b WHERE b.exogenous = 0 - `, parentTable, parentDepTable, cand, - blockingReasonSQL("pr"), joins.String(), match.String()) -} - -// blockingReasonSQL is the row-level spelling of "this dependency row is, -// right now, a reason for its own issue to be blocked" — union legs 1 and 2 -// (an open blocks/conditional-blocks target, issue or wisp) and leg 5 (a -// blocking waits-for gate), for a dependency row aliased rowAlias. -// -// It is a per-ROW predicate, but both callers evaluate it in a set, over a -// bounded list of parents' rows: parentsExplainedBySubtreeSQL's aggregate, -// over the reason rows of the parents that statement can be asked about, once, -// and withoutReasonAmongInTx, over the rows of the parents the walk could not -// otherwise decide, by id. It is never applied per row of an enclosing leg — -// that is the correlated shape #6288 removed and B2 measured. -// -//nolint:gosec // G201: rowAlias is a constant; waitsForGateBlockedSQLFor rewrites a constant template. -func blockingReasonSQL(rowAlias string) string { - return fmt.Sprintf(` - ( ( (%[1]s.type = 'blocks' OR %[1]s.type = 'conditional-blocks') - AND ( EXISTS (SELECT 1 FROM issues bt - WHERE bt.id = %[1]s.depends_on_issue_id - AND bt.status <> 'closed' AND bt.status <> 'pinned') - OR EXISTS (SELECT 1 FROM wisps bt - WHERE bt.id = %[1]s.depends_on_wisp_id - AND bt.status <> 'closed' AND bt.status <> 'pinned') ) ) - OR ( %[1]s.type = 'waits-for' AND (%[2]s) ) ) - `, rowAlias, waitsForGateBlockedSQLFor(rowAlias)) + SELECT d.issue_id FROM %[1]s d + WHERE d.issue_id IS NOT NULL + AND d.type = 'waits-for' + AND (%[2]s) + `, depTable, waitsForGateBlockedSQL) } // CountIsBlockedInconsistenciesInTx reports how many issue and wisp rows carry a @@ -1316,8 +413,7 @@ func blockingReasonSQL(rowAlias string) string { // detection behind the bd doctor "Blocked State" check (bd-6dnrw.37); the // repair is RecomputeAllIsBlockedInTx. // -// Both derive their membership test from the one union -// builder above, and they are pinned together by the blocked-consistency +// The two share no SQL but are pinned together by the blocked-consistency // lockstep test: a converged database counts 0, and any row this counts is one // a recompute pass changes. The count is a single-pass lower bound — a // corrupted parent's children are only counted on the pass after the parent is @@ -1326,20 +422,13 @@ func blockingReasonSQL(rowAlias string) string { func CountIsBlockedInconsistenciesInTx(ctx context.Context, tx DBTX) (int64, error) { var total int64 - // One exogeneity read for both statements; each splices the union twice, - // so the ids are bound twice per statement. - explained, err := subtreeExplainedParentsInTx(ctx, tx) - if err != nil { - return 0, fmt.Errorf("count stale is_blocked issues: %w", err) - } - args := append(explained.args(), explained.args()...) - n, err := countRows(ctx, tx, countStaleIsBlockedSQL("issues", "i", "dependencies", explained), args...) + n, err := countRows(ctx, tx, countStaleIsBlockedSQL("issues", "i", "dependencies")) if err != nil { return 0, fmt.Errorf("count stale is_blocked issues: %w", err) } total += n - n, err = countRows(ctx, tx, countStaleIsBlockedSQL("wisps", "w", "wisp_dependencies", explained), args...) + n, err = countRows(ctx, tx, countStaleIsBlockedSQL("wisps", "w", "wisp_dependencies")) if err != nil { if isTableNotExistError(err) { return total, nil @@ -1361,14 +450,14 @@ func CountIsBlockedInconsistenciesInTx(ctx context.Context, tx DBTX) (int64, err // set-membership form of De Morgan over the shouldBeBlocked disjunction). // // is_blocked is 0 or 1, so the branches are mutually exclusive and the count is -// their sum. Built on the uncorrelated union for the same +// their sum. Built on the uncorrelated shouldBeBlockedIDsUnionSQL for the same // reason as the full repair (bd-t9ypt): the correlated disjunction form ran per // outer row and cost whole-table scans per branch. table/alias/depTable are // hardcoded constants supplied by the only two callers. // //nolint:gosec // G201: table, alias, and depTable are constant; only the constant gate SQL is interpolated. -func countStaleIsBlockedSQL(table, alias, depTable string, explained subtreeExplainedParents) string { - union := shouldBeBlockedIDsUnionPrecomputedSQL(depTable, explained) +func countStaleIsBlockedSQL(table, alias, depTable string) string { + union := shouldBeBlockedIDsUnionSQL(depTable) return fmt.Sprintf(` SELECT COUNT(*) FROM %[1]s %[2]s WHERE @@ -1383,8 +472,8 @@ func countStaleIsBlockedSQL(table, alias, depTable string, explained subtreeExpl } // countRows runs a single COUNT(*) query and returns the scalar. -func countRows(ctx context.Context, tx DBTX, query string, args ...any) (int64, error) { - rows, err := tx.QueryContext(ctx, query, args...) +func countRows(ctx context.Context, tx DBTX, query string) (int64, error) { + rows, err := tx.QueryContext(ctx, query) if err != nil { return 0, err } diff --git a/internal/storage/issueops/blocked_role.go b/internal/storage/issueops/blocked_role.go deleted file mode 100644 index f57dadfc2..000000000 --- a/internal/storage/issueops/blocked_role.go +++ /dev/null @@ -1,114 +0,0 @@ -package issueops - -import ( - "context" - "fmt" - "slices" - - "github.com/steveyegge/beads/internal/types" - "github.com/steveyegge/beads/internal/utils" - publicops "github.com/steveyegge/beads/issueops" -) - -// BlockedFilter is the WorkFilter a BlockedRequest compiles to: the filter -// `bd blocked` has always handed GetBlockedIssues, built from the same four -// flags. Labels are normalized as every other label filter normalizes them -// (trimmed, blanks and repeats dropped): these clauses match a label EXACTLY, -// so an untrimmed `--label 'a, b'` value would otherwise match nothing. -// -// It never writes through the request's slices (utils.NormalizeLabels -// returns a new one), and it sets no page: the listing has none. -func BlockedFilter(req publicops.BlockedRequest) types.WorkFilter { - var filter types.WorkFilter - if req.ParentID != "" { - parentID := req.ParentID - filter.ParentID = &parentID - } - filter.Labels = utils.NormalizeLabels(req.Labels) - filter.LabelsAny = utils.NormalizeLabels(req.LabelsAny) - filter.ExcludeLabels = utils.NormalizeLabels(req.ExcludeLabels) - return filter -} - -// BlockedResultOf finishes a blocked listing for the role: never nil, and in -// the role's total order (priority ascending, created_at descending, id). A -// row's blocker slice is never nil either, so it encodes as [] like the -// count beside it says. -// -// The order is restated rather than trusted because the store body breaks no -// tie between two rows of equal priority created in the same instant (it -// sorts rows assembled from a map); the id makes the listing one answer on -// every leg. The external-dependency decorator already finishes with the -// same three keys, so this is a no-op over it. -func BlockedResultOf(items []*types.BlockedIssue) publicops.BlockedResult { - out := make([]*types.BlockedIssue, 0, len(items)) - for _, item := range items { - if item == nil { - continue - } - if item.BlockedBy == nil { - clone := *item - clone.BlockedBy = []string{} - item = &clone - } - out = append(out, item) - } - slices.SortStableFunc(out, func(a, b *types.BlockedIssue) int { - if a.Priority != b.Priority { - return a.Priority - b.Priority - } - if !a.CreatedAt.Equal(b.CreatedAt) { - if a.CreatedAt.After(b.CreatedAt) { - return -1 - } - return 1 - } - switch { - case a.ID < b.ID: - return -1 - case a.ID > b.ID: - return 1 - } - return 0 - }) - return publicops.BlockedResult{Items: out} -} - -// ExecuteListBlockedInTx is the store-backed body behind -// issueops.BlockedLister: GetBlockedIssuesInTx over the request's filter, -// finished by BlockedResultOf, inside the caller's read transaction. -func ExecuteListBlockedInTx(ctx context.Context, tx DBTX, req publicops.BlockedRequest) (publicops.BlockedResult, error) { - items, err := GetBlockedIssuesInTx(ctx, tx, BlockedFilter(req)) - if err != nil { - return publicops.BlockedResult{}, err - } - return BlockedResultOf(items), nil -} - -// BlockedIssuesReader is the legacy blocked read a BlockedLister can be built -// over: storage.DoltStorage's GetBlockedIssues, and the external-dependency -// decorator's override of it. -type BlockedIssuesReader interface { - GetBlockedIssues(ctx context.Context, filter types.WorkFilter) ([]*types.BlockedIssue, error) -} - -// NewBlockedListerOver returns the BlockedLister that answers through -// reader's GetBlockedIssues. It is how a decorator that overrides that read -// (externaldeps.Store) hands out the role with its own decoration, so the -// role and the legacy read cannot answer differently. -func NewBlockedListerOver(reader BlockedIssuesReader) (publicops.BlockedLister, error) { - if reader == nil { - return nil, fmt.Errorf("new blocked lister: reader must not be nil") - } - return blockedListerOver{reader: reader}, nil -} - -type blockedListerOver struct{ reader BlockedIssuesReader } - -func (l blockedListerOver) ListBlocked(ctx context.Context, req publicops.BlockedRequest) (publicops.BlockedResult, error) { - items, err := l.reader.GetBlockedIssues(ctx, BlockedFilter(req)) - if err != nil { - return publicops.BlockedResult{}, err - } - return BlockedResultOf(items), nil -} diff --git a/internal/storage/issueops/blocked_state.go b/internal/storage/issueops/blocked_state.go index 66997bf2e..fb62b1e16 100644 --- a/internal/storage/issueops/blocked_state.go +++ b/internal/storage/issueops/blocked_state.go @@ -4,7 +4,6 @@ import ( "context" "database/sql" "fmt" - "regexp" "github.com/steveyegge/beads/internal/types" ) @@ -20,46 +19,21 @@ type DBTX interface { QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row } -// waitsForGateBlockedSQL decides whether a waits-for row d (spawner = -// d.depends_on_issue_id or d.depends_on_wisp_id) still gates its waiter. -// -// Each "the spawner has an open / a closed parent-child child" test is split -// per dependency table AND per spawner column, and pins the child lookup by -// primary key (JOIN_ORDER/LOOKUP_JOIN; on MySQL, JOIN_ORDER is honored and -// the unknown LOOKUP_JOIN hint is ignored with a warning). The earlier form -// matched both spawner columns with one OR inside each EXISTS, which no -// single index serves, so the engine scanned the whole edge table per -// correlated evaluation: closing one child of a 1000-child spawner with 20 -// waiters on a 50k-issue / 100k-edge database spent ~39 s in the recompute. -// Split, each EXISTS is an index lookup on its target column (the -// idx_*_issue_target / idx_*_wisp_target indexes). EXISTS(a OR b) is -// EXISTS(a) OR EXISTS(b), so the result is unchanged; the IS NOT NULL guards -// stay, as before (cd.col = NULL never matches anyway). const waitsForGateBlockedSQL = ` ( ( EXISTS ( - SELECT /*+ JOIN_ORDER(cd, child) LOOKUP_JOIN(cd, child) */ 1 FROM dependencies cd JOIN issues child ON child.id = cd.issue_id + SELECT 1 FROM dependencies cd JOIN issues child ON child.id = cd.issue_id WHERE cd.type = 'parent-child' - AND d.depends_on_issue_id IS NOT NULL AND cd.depends_on_issue_id = d.depends_on_issue_id + AND ((d.depends_on_issue_id IS NOT NULL AND cd.depends_on_issue_id = d.depends_on_issue_id) + OR (d.depends_on_wisp_id IS NOT NULL AND cd.depends_on_wisp_id = d.depends_on_wisp_id)) AND child.status <> 'closed' AND child.status <> 'pinned' ) OR EXISTS ( - SELECT /*+ JOIN_ORDER(cd, child) LOOKUP_JOIN(cd, child) */ 1 FROM dependencies cd JOIN issues child ON child.id = cd.issue_id + SELECT 1 FROM wisp_dependencies cd JOIN wisps child ON child.id = cd.issue_id WHERE cd.type = 'parent-child' - AND d.depends_on_wisp_id IS NOT NULL AND cd.depends_on_wisp_id = d.depends_on_wisp_id - AND child.status <> 'closed' AND child.status <> 'pinned' - ) - OR EXISTS ( - SELECT /*+ JOIN_ORDER(cd, child) LOOKUP_JOIN(cd, child) */ 1 FROM wisp_dependencies cd JOIN wisps child ON child.id = cd.issue_id - WHERE cd.type = 'parent-child' - AND d.depends_on_issue_id IS NOT NULL AND cd.depends_on_issue_id = d.depends_on_issue_id - AND child.status <> 'closed' AND child.status <> 'pinned' - ) - OR EXISTS ( - SELECT /*+ JOIN_ORDER(cd, child) LOOKUP_JOIN(cd, child) */ 1 FROM wisp_dependencies cd JOIN wisps child ON child.id = cd.issue_id - WHERE cd.type = 'parent-child' - AND d.depends_on_wisp_id IS NOT NULL AND cd.depends_on_wisp_id = d.depends_on_wisp_id + AND ((d.depends_on_issue_id IS NOT NULL AND cd.depends_on_issue_id = d.depends_on_issue_id) + OR (d.depends_on_wisp_id IS NOT NULL AND cd.depends_on_wisp_id = d.depends_on_wisp_id)) AND child.status <> 'closed' AND child.status <> 'pinned' ) ) @@ -70,27 +44,17 @@ const waitsForGateBlockedSQL = ` COALESCE(JSON_UNQUOTE(JSON_EXTRACT(d.metadata, '$.gate')), 'all-children') = 'any-children' AND ( EXISTS ( - SELECT /*+ JOIN_ORDER(cd, child) LOOKUP_JOIN(cd, child) */ 1 FROM dependencies cd JOIN issues child ON child.id = cd.issue_id + SELECT 1 FROM dependencies cd JOIN issues child ON child.id = cd.issue_id WHERE cd.type = 'parent-child' - AND d.depends_on_issue_id IS NOT NULL AND cd.depends_on_issue_id = d.depends_on_issue_id + AND ((d.depends_on_issue_id IS NOT NULL AND cd.depends_on_issue_id = d.depends_on_issue_id) + OR (d.depends_on_wisp_id IS NOT NULL AND cd.depends_on_wisp_id = d.depends_on_wisp_id)) AND child.status = 'closed' ) OR EXISTS ( - SELECT /*+ JOIN_ORDER(cd, child) LOOKUP_JOIN(cd, child) */ 1 FROM dependencies cd JOIN issues child ON child.id = cd.issue_id + SELECT 1 FROM wisp_dependencies cd JOIN wisps child ON child.id = cd.issue_id WHERE cd.type = 'parent-child' - AND d.depends_on_wisp_id IS NOT NULL AND cd.depends_on_wisp_id = d.depends_on_wisp_id - AND child.status = 'closed' - ) - OR EXISTS ( - SELECT /*+ JOIN_ORDER(cd, child) LOOKUP_JOIN(cd, child) */ 1 FROM wisp_dependencies cd JOIN wisps child ON child.id = cd.issue_id - WHERE cd.type = 'parent-child' - AND d.depends_on_issue_id IS NOT NULL AND cd.depends_on_issue_id = d.depends_on_issue_id - AND child.status = 'closed' - ) - OR EXISTS ( - SELECT /*+ JOIN_ORDER(cd, child) LOOKUP_JOIN(cd, child) */ 1 FROM wisp_dependencies cd JOIN wisps child ON child.id = cd.issue_id - WHERE cd.type = 'parent-child' - AND d.depends_on_wisp_id IS NOT NULL AND cd.depends_on_wisp_id = d.depends_on_wisp_id + AND ((d.depends_on_issue_id IS NOT NULL AND cd.depends_on_issue_id = d.depends_on_issue_id) + OR (d.depends_on_wisp_id IS NOT NULL AND cd.depends_on_wisp_id = d.depends_on_wisp_id)) AND child.status = 'closed' ) ) @@ -129,25 +93,6 @@ const waitsForGateBlockedSQL = ` ) ` -// waitsForGateRowAliasRE matches the dependency-row alias references in -// waitsForGateBlockedSQL — `d.` at a word boundary, which never matches the -// gate's own inner `cd.` rows (no boundary between c and d). -var waitsForGateRowAliasRE = regexp.MustCompile(`\bd\.`) - -// waitsForGateBlockedSQLFor returns waitsForGateBlockedSQL with its -// dependency-row alias rewritten from d to alias, for callers that evaluate -// the gate somewhere the row cannot be called d — blockingReasonSQL applies it -// to a parent's own dependency rows inside a union leg whose outer row is -// already d (gastownhall/beads#6506). Rewriting beats shadowing: an inner d -// would resolve correctly by scope rules but reads as a bug at every later -// glance. -func waitsForGateBlockedSQLFor(alias string) string { - if alias == "d" { - return waitsForGateBlockedSQL - } - return waitsForGateRowAliasRE.ReplaceAllString(waitsForGateBlockedSQL, alias+".") -} - // RecomputeIsBlockedResult reports which issue tables had rows changed while // the blocked-state fixpoint converged. type RecomputeIsBlockedResult struct { @@ -166,16 +111,6 @@ func RecomputeIsBlockedInTx(ctx context.Context, tx DBTX, issueIDs, wispIDs []st // reports whether an UPDATE changed rows in each issue table. func RecomputeIsBlockedInTxWithResult( ctx context.Context, tx DBTX, issueIDs, wispIDs []string, -) (RecomputeIsBlockedResult, error) { - return recomputeIsBlockedInTxWithResult(ctx, tx, issueIDs, wispIDs, false) -} - -// recomputeIsBlockedInTxWithResult is RecomputeIsBlockedInTxWithResult with -// the caller's say on the no-edge shortcut (planRecomputeInTx): a create -// passes splitEdgeless, because its ids are mostly fresh rows without edges; -// every other caller leaves it to the chunk size. -func recomputeIsBlockedInTxWithResult( - ctx context.Context, tx DBTX, issueIDs, wispIDs []string, splitEdgeless bool, ) (RecomputeIsBlockedResult, error) { var result RecomputeIsBlockedResult if len(issueIDs) == 0 && len(wispIDs) == 0 { @@ -185,25 +120,17 @@ func recomputeIsBlockedInTxWithResult( if err != nil { return result, err } - issuePlan, err := planRecomputeInTx(ctx, tx, issuesBlockedSpec, issueIDs, splitEdgeless) - if err != nil { - return result, err - } - wispPlan, err := planRecomputeInTx(ctx, tx, wispsBlockedSpec, wispIDs, splitEdgeless) - if err != nil { - return result, err - } - for pass := 0; ; pass++ { + for { var changed int64 - n, err := runRecomputePassInTx(ctx, tx, issuePlan, pass == 0) + n, err := recomputeIsBlockedPassForIssuesInTx(ctx, tx, issueIDs) if err != nil { return result, err } changed += n result.IssueRowsChanged = result.IssueRowsChanged || n > 0 - n, err = runRecomputePassInTx(ctx, tx, wispPlan, pass == 0) + n, err = recomputeIsBlockedPassForWispsInTx(ctx, tx, wispIDs) if err != nil { return result, err } @@ -253,11 +180,20 @@ func RecomputeIsBlockedForWispIDsInTx(ctx context.Context, tx DBTX, ids []string return RecomputeIsBlockedInTx(ctx, tx, nil, ids) } +//nolint:gosec // G201: SQL templates are constant; only IN-clause placeholders are formatted in. +func recomputeIsBlockedPassForIssuesInTx(ctx context.Context, tx DBTX, ids []string) (int64, error) { + if len(ids) == 0 { + return 0, nil + } + + return runMarkUnmarkBatchedInTx(ctx, tx, markBlockedTemplateForIssues(), unmarkBlockedTemplateForIssues(), ids) +} + func markIsBlockedPassForIssuesInTx(ctx context.Context, tx DBTX, ids []string) (int64, error) { if len(ids) == 0 { return 0, nil } - return runMarkBatchedInTx(ctx, tx, issuesBlockedSpec, ids) + return runMarkBatchedInTx(ctx, tx, markBlockedTemplateForIssues(), ids) } // The mark/unmark templates explicitly assign updated_at to itself: @@ -267,168 +203,204 @@ func markIsBlockedPassForIssuesInTx(ctx context.Context, tx DBTX, ids []string) // clones that recomputed the same flip at different times, bd-578h9.19) and // makes stale-guard/conflict-guard consumers treat the row as user-edited. // An explicit assignment suppresses the ON UPDATE clause. -// -// Both templates decide membership through -// shouldBeBlockedIDsUnionScopedPrecomputedSQL, the same uncorrelated union the -// full repair and the doctor count use (blocked_consistency.go), scoped to the -// batch: one derived blocked set per batch, computed once and probed by hash. -// The previous shape — five correlated EXISTS per outer row — was re-executed -// per row by the engine: ~4 s per 200-id batch on committed, indexed data, and -// unbounded (>69 min observed on dolt 2.1.8) over a large uncommitted working -// set, where every probe re-read the uncommitted overlay -// (gastownhall/beads#6288). -// -// The parent-child legs' exogeneity set (gastownhall/beads#6506) is READ once -// per batch and BOUND into both statements as ids, never spliced as the query -// that derives it: spliced, a 200-id recompute paid for the derivation four -// times and measured 6.9x origin/main (scopedExplainedParentsInTx has the -// numbers). Which is why the templates are built per batch rather than once: -// the number of bound ids is a property of the batch. -// -// The batch IN-list therefore appears more than once per statement — the -// outer row filter and one per union leg (six occurrences today) — and the -// exogeneity ids appear as already-rendered ? placeholders inside two of those -// legs. expandBatchTemplate binds both groups in TEXT order. - -// batchScopeSQL is the per-leg predicate that confines the should-be-blocked -// union to the batch (see shouldBeBlockedIDsUnionScopedPrecomputedSQL); its %s -// is filled with the batch placeholders by expandBatchTemplate, and by fmt -// here ONLY for the batch-scoped exogeneity read, which binds its own ids. -const batchScopeSQL = "AND d.issue_id IN (%s)" - -// blockedTableSpec names the one table a batched mark/unmark pass runs over. -// The batched statements are now built per batch (the exogeneity ids are), so -// the runner takes the table rather than a finished template. -type blockedTableSpec struct { - table, alias, depTable string -} - -var ( - issuesBlockedSpec = blockedTableSpec{table: "issues", alias: "i", depTable: "dependencies"} - wispsBlockedSpec = blockedTableSpec{table: "wisps", alias: "w", depTable: "wisp_dependencies"} -) - -func markBlockedTemplateForIssues(explained subtreeExplainedParents) string { - return markBlockedTemplate(issuesBlockedSpec, explained) -} - -func unmarkBlockedTemplateForIssues(explained subtreeExplainedParents) string { - return unmarkBlockedTemplate(issuesBlockedSpec, explained) -} - -func markBlockedTemplateForWisps(explained subtreeExplainedParents) string { - return markBlockedTemplate(wispsBlockedSpec, explained) -} - -func unmarkBlockedTemplateForWisps(explained subtreeExplainedParents) string { - return unmarkBlockedTemplate(wispsBlockedSpec, explained) -} - -// markBlockedTemplate is the batched mark statement for one table: the -// batch-scoped analog of markAllBlockedSQL. The union is confined to the -// batch's own dependency rows, so `.id IN (union)` is exactly the -// old correlated disjunction for every id in the batch. -// -//nolint:gosec // G201: the spec's fields are constants from the four callers above. -func markBlockedTemplate(spec blockedTableSpec, explained subtreeExplainedParents) string { +func markBlockedTemplateForIssues() string { return fmt.Sprintf(` - UPDATE %[1]s %[2]s SET %[2]s.is_blocked = 1, %[2]s.updated_at = %[2]s.updated_at - WHERE %[2]s.id IN (%%s) - AND %[2]s.is_blocked = 0 - AND %[2]s.status <> 'closed' AND %[2]s.status <> 'pinned' - AND %[2]s.id IN (%[3]s) - `, spec.table, spec.alias, - shouldBeBlockedIDsUnionScopedPrecomputedSQL(spec.depTable, batchScopeSQL, explained)) + UPDATE issues i SET i.is_blocked = 1, i.updated_at = i.updated_at + WHERE i.id IN (%%s) + AND i.is_blocked = 0 + AND i.status <> 'closed' AND i.status <> 'pinned' + AND ( + EXISTS ( + SELECT 1 FROM dependencies d + JOIN issues t ON t.id = d.depends_on_issue_id + WHERE d.issue_id = i.id + AND (d.type = 'blocks' OR d.type = 'conditional-blocks') + AND t.status <> 'closed' AND t.status <> 'pinned' + ) + OR EXISTS ( + SELECT 1 FROM dependencies d + JOIN wisps t ON t.id = d.depends_on_wisp_id + WHERE d.issue_id = i.id + AND (d.type = 'blocks' OR d.type = 'conditional-blocks') + AND t.status <> 'closed' AND t.status <> 'pinned' + ) + OR EXISTS ( + SELECT 1 FROM dependencies d + JOIN issues p ON p.id = d.depends_on_issue_id + WHERE d.issue_id = i.id + AND d.type = 'parent-child' + AND p.is_blocked = 1 + ) + OR EXISTS ( + SELECT 1 FROM dependencies d + JOIN wisps p ON p.id = d.depends_on_wisp_id + WHERE d.issue_id = i.id + AND d.type = 'parent-child' + AND p.is_blocked = 1 + ) + OR EXISTS ( + SELECT 1 FROM dependencies d + WHERE d.issue_id = i.id AND d.type = 'waits-for' + AND (%s) + ) + ) + `, waitsForGateBlockedSQL) } -// unmarkBlockedTemplate is the batched unmark statement for one table: the -// batch-scoped analog of unmarkAllBlockedSQL. NOT IN is null-hostile; the -// union's d.issue_id IS NOT NULL guards keep it total. -// -//nolint:gosec // G201: the spec's fields are constants from the four callers above. -func unmarkBlockedTemplate(spec blockedTableSpec, explained subtreeExplainedParents) string { +func unmarkBlockedTemplateForIssues() string { return fmt.Sprintf(` - UPDATE %[1]s %[2]s SET %[2]s.is_blocked = 0, %[2]s.updated_at = %[2]s.updated_at - WHERE %[2]s.id IN (%%s) - AND %[2]s.is_blocked = 1 - AND ( %[2]s.status = 'closed' OR %[2]s.status = 'pinned' - OR %[2]s.id NOT IN (%[3]s) ) - `, spec.table, spec.alias, - shouldBeBlockedIDsUnionScopedPrecomputedSQL(spec.depTable, batchScopeSQL, explained)) + UPDATE issues i SET i.is_blocked = 0, i.updated_at = i.updated_at + WHERE i.id IN (%%s) + AND i.is_blocked = 1 + AND ( + i.status = 'closed' OR i.status = 'pinned' + OR ( + NOT EXISTS ( + SELECT 1 FROM dependencies d + JOIN issues t ON t.id = d.depends_on_issue_id + WHERE d.issue_id = i.id + AND (d.type = 'blocks' OR d.type = 'conditional-blocks') + AND t.status <> 'closed' AND t.status <> 'pinned' + ) + AND NOT EXISTS ( + SELECT 1 FROM dependencies d + JOIN wisps t ON t.id = d.depends_on_wisp_id + WHERE d.issue_id = i.id + AND (d.type = 'blocks' OR d.type = 'conditional-blocks') + AND t.status <> 'closed' AND t.status <> 'pinned' + ) + AND NOT EXISTS ( + SELECT 1 FROM dependencies d + JOIN issues p ON p.id = d.depends_on_issue_id + WHERE d.issue_id = i.id + AND d.type = 'parent-child' + AND p.is_blocked = 1 + ) + AND NOT EXISTS ( + SELECT 1 FROM dependencies d + JOIN wisps p ON p.id = d.depends_on_wisp_id + WHERE d.issue_id = i.id + AND d.type = 'parent-child' + AND p.is_blocked = 1 + ) + AND NOT EXISTS ( + SELECT 1 FROM dependencies d + WHERE d.issue_id = i.id AND d.type = 'waits-for' + AND (%s) + ) + ) + ) + `, waitsForGateBlockedSQL) } -// expandBatchTemplate finishes a batched template: every %s takes the batch's -// IN-list placeholders, and the args come out in the order the finished -// statement reads them — the batch ids once per %s, and the already-rendered -// ? placeholders the template carries (the two parent-child legs' exogeneity -// ids, see notInPlaceholders) interleaved where they actually appear. -// -// TEXT ORDER is the whole contract, and it is why this walks the template -// instead of counting: the batch scope and the exogeneity ids alternate inside -// the union (leg 3's scope, leg 3's NOT IN, leg 4's scope, leg 4's NOT IN), so -// no amount of counting says where a bound group goes. A template with one %s -// and no ? degrades to the plain Sprintf it always was. -// -// The count is textual, so a template must contain no percent sign that is not -// one of the %s verbs (no LIKE 'x%' pattern, no %%) and no ? of its own beyond -// the bound group — both pinned by blocked_state_template_test.go. A mismatch -// between the ? count and the bound ids is a programmer error and is returned -// as one rather than sent to the engine, where it would surface as an opaque -// argument-count error. -// -//nolint:gosec // G201: tmpl is a constant template; only IN-clause placeholders are formatted in. -func expandBatchTemplate( - tmpl, placeholders string, args, bound []interface{}, -) (string, []interface{}, error) { - var fills []interface{} - out := make([]interface{}, 0, len(args)+len(bound)) - next := 0 - for i := 0; i < len(tmpl); i++ { - switch { - case tmpl[i] == '%' && i+1 < len(tmpl) && tmpl[i+1] == 's': - fills = append(fills, placeholders) - out = append(out, args...) - i++ - case tmpl[i] == '?': - if next < len(bound) { - out = append(out, bound[next]) - } - next++ - } +//nolint:gosec // G201: SQL templates are constant; only IN-clause placeholders are formatted in. +func recomputeIsBlockedPassForWispsInTx(ctx context.Context, tx DBTX, ids []string) (int64, error) { + if len(ids) == 0 { + return 0, nil } - if next != len(bound) { - return "", nil, fmt.Errorf( - "batched template carries %d ? placeholders, want %d bound ids", next, len(bound)) - } - return fmt.Sprintf(tmpl, fills...), out, nil + + return runMarkUnmarkBatchedInTx(ctx, tx, markBlockedTemplateForWisps(), unmarkBlockedTemplateForWisps(), ids) } func markIsBlockedPassForWispsInTx(ctx context.Context, tx DBTX, ids []string) (int64, error) { if len(ids) == 0 { return 0, nil } - return runMarkBatchedInTx(ctx, tx, wispsBlockedSpec, ids) + return runMarkBatchedInTx(ctx, tx, markBlockedTemplateForWisps(), ids) } -// batchExogeneity reads the batch-scoped exogeneity set for one chunk of ids: -// ONE read for the chunk, shared by the chunk's mark and its unmark. -// -// It is read before the mark, so the unmark sees the set as it was at the top -// of the chunk. That is the same staleness the unbatched full repair accepts -// (one read per PASS, see recomputeIsBlockedCounting), and it is safe for the -// same reason: both callers are fixpoint loops that stop only on a pass that -// changes NO rows, and on that pass the set was read against the state the -// pass ends in. So the state the loop converges to satisfies both predicates -// under the set derived from that very state; a stale read can only cost an -// extra pass, never a wrong fixpoint. -func batchExogeneity( - ctx context.Context, tx DBTX, spec blockedTableSpec, placeholders string, args []interface{}, -) (subtreeExplainedParents, error) { - return scopedExplainedParentsInTx(ctx, tx, spec.depTable, - fmt.Sprintf(batchScopeSQL, placeholders), args) +func markBlockedTemplateForWisps() string { + return fmt.Sprintf(` + UPDATE wisps w SET w.is_blocked = 1, w.updated_at = w.updated_at + WHERE w.id IN (%%s) + AND w.is_blocked = 0 + AND w.status <> 'closed' AND w.status <> 'pinned' + AND ( + EXISTS ( + SELECT 1 FROM wisp_dependencies d + JOIN issues t ON t.id = d.depends_on_issue_id + WHERE d.issue_id = w.id + AND (d.type = 'blocks' OR d.type = 'conditional-blocks') + AND t.status <> 'closed' AND t.status <> 'pinned' + ) + OR EXISTS ( + SELECT 1 FROM wisp_dependencies d + JOIN wisps t ON t.id = d.depends_on_wisp_id + WHERE d.issue_id = w.id + AND (d.type = 'blocks' OR d.type = 'conditional-blocks') + AND t.status <> 'closed' AND t.status <> 'pinned' + ) + OR EXISTS ( + SELECT 1 FROM wisp_dependencies d + JOIN issues p ON p.id = d.depends_on_issue_id + WHERE d.issue_id = w.id + AND d.type = 'parent-child' + AND p.is_blocked = 1 + ) + OR EXISTS ( + SELECT 1 FROM wisp_dependencies d + JOIN wisps p ON p.id = d.depends_on_wisp_id + WHERE d.issue_id = w.id + AND d.type = 'parent-child' + AND p.is_blocked = 1 + ) + OR EXISTS ( + SELECT 1 FROM wisp_dependencies d + WHERE d.issue_id = w.id AND d.type = 'waits-for' + AND (%s) + ) + ) + `, waitsForGateBlockedSQL) } -func runMarkUnmarkBatchedInTx(ctx context.Context, tx DBTX, spec blockedTableSpec, ids []string) (int64, error) { +func unmarkBlockedTemplateForWisps() string { + return fmt.Sprintf(` + UPDATE wisps w SET w.is_blocked = 0, w.updated_at = w.updated_at + WHERE w.id IN (%%s) + AND w.is_blocked = 1 + AND ( + w.status = 'closed' OR w.status = 'pinned' + OR ( + NOT EXISTS ( + SELECT 1 FROM wisp_dependencies d + JOIN issues t ON t.id = d.depends_on_issue_id + WHERE d.issue_id = w.id + AND (d.type = 'blocks' OR d.type = 'conditional-blocks') + AND t.status <> 'closed' AND t.status <> 'pinned' + ) + AND NOT EXISTS ( + SELECT 1 FROM wisp_dependencies d + JOIN wisps t ON t.id = d.depends_on_wisp_id + WHERE d.issue_id = w.id + AND (d.type = 'blocks' OR d.type = 'conditional-blocks') + AND t.status <> 'closed' AND t.status <> 'pinned' + ) + AND NOT EXISTS ( + SELECT 1 FROM wisp_dependencies d + JOIN issues p ON p.id = d.depends_on_issue_id + WHERE d.issue_id = w.id + AND d.type = 'parent-child' + AND p.is_blocked = 1 + ) + AND NOT EXISTS ( + SELECT 1 FROM wisp_dependencies d + JOIN wisps p ON p.id = d.depends_on_wisp_id + WHERE d.issue_id = w.id + AND d.type = 'parent-child' + AND p.is_blocked = 1 + ) + AND NOT EXISTS ( + SELECT 1 FROM wisp_dependencies d + WHERE d.issue_id = w.id AND d.type = 'waits-for' + AND (%s) + ) + ) + ) + `, waitsForGateBlockedSQL) +} + +//nolint:gosec // G201: callers pass constant templates; only IN-clause placeholders are formatted in. +func runMarkUnmarkBatchedInTx(ctx context.Context, tx DBTX, markTmpl, unmarkTmpl string, ids []string) (int64, error) { var changed int64 for start := 0; start < len(ids); start += queryBatchSize { end := start + queryBatchSize @@ -436,17 +408,8 @@ func runMarkUnmarkBatchedInTx(ctx context.Context, tx DBTX, spec blockedTableSpe end = len(ids) } placeholders, args := buildSQLInClause(ids[start:end]) - explained, err := batchExogeneity(ctx, tx, spec, placeholders, args) - if err != nil { - return changed, fmt.Errorf("recompute is_blocked (exogeneity): %w", err) - } - stmt, stmtArgs, err := expandBatchTemplate( - markBlockedTemplate(spec, explained), placeholders, args, explained.args()) - if err != nil { - return changed, fmt.Errorf("recompute is_blocked (mark): %w", err) - } - res, err := tx.ExecContext(ctx, stmt, stmtArgs...) + res, err := tx.ExecContext(ctx, fmt.Sprintf(markTmpl, placeholders), args...) if err != nil { return changed, fmt.Errorf("recompute is_blocked (mark): %w", err) } @@ -456,12 +419,7 @@ func runMarkUnmarkBatchedInTx(ctx context.Context, tx DBTX, spec blockedTableSpe } changed += n - stmt, stmtArgs, err = expandBatchTemplate( - unmarkBlockedTemplate(spec, explained), placeholders, args, explained.args()) - if err != nil { - return changed, fmt.Errorf("recompute is_blocked (unmark): %w", err) - } - res, err = tx.ExecContext(ctx, stmt, stmtArgs...) + res, err = tx.ExecContext(ctx, fmt.Sprintf(unmarkTmpl, placeholders), args...) if err != nil { return changed, fmt.Errorf("recompute is_blocked (unmark): %w", err) } @@ -474,140 +432,8 @@ func runMarkUnmarkBatchedInTx(ctx context.Context, tx DBTX, spec blockedTableSpe return changed, nil } -// recomputeSplitMinChunk is the chunk size from which planRecomputeInTx -// probes for edgeless ids without being asked to. -const recomputeSplitMinChunk = 32 - -// recomputeChunk is one queryBatchSize chunk of a recompute's ids, split by -// whether each id has a dependency row of its own (see planRecomputeInTx). -type recomputeChunk struct { - withEdges, edgeless []string -} - -// recomputePlan is a recompute's chunks for one table, split once per -// RecomputeIsBlockedInTxWithResult call: the fixpoint loop writes only -// is_blocked, never dependency rows, so the split holds for every pass. -type recomputePlan struct { - spec blockedTableSpec - chunks []recomputeChunk -} - -// planRecomputeInTx chunks ids exactly as runMarkUnmarkBatchedInTx would and -// splits each chunk by whether spec.depTable holds a row with that id as its -// issue_id. -// -// Every leg of the should-be-blocked union the mark/unmark templates probe is -// scoped to `d.issue_id IN (batch)` on spec.depTable -// (shouldBeBlockedIDsUnionScopedPrecomputedSQL), so an id with no row of its -// own there is never in that union: mark cannot select it, and unmark reduces -// to "is_blocked = 1 -> 0". Those ids — every freshly created issue without -// edges, which is most of what a large create or apply-batch recomputes — -// need not ride the two union statements, whose cost grows with the table. -// -// The split costs one probe per chunk, which only pays when the chunk holds -// edgeless ids worth skipping. So it runs when the caller asks (splitEdgeless: -// the create paths) or when the chunk is large; the single-issue recomputes of -// close, reopen, update and dependency edits keep exactly the statements they -// ran before the shortcut existed. -func planRecomputeInTx(ctx context.Context, tx DBTX, spec blockedTableSpec, ids []string, splitEdgeless bool) (recomputePlan, error) { - plan := recomputePlan{spec: spec} - for start := 0; start < len(ids); start += queryBatchSize { - end := min(start+queryBatchSize, len(ids)) - chunk := recomputeChunk{withEdges: ids[start:end]} - if !createFastPathsDisabled.Load() && (splitEdgeless || end-start >= recomputeSplitMinChunk) { - var err error - chunk.withEdges, chunk.edgeless, err = splitByOwnDependencyRowsInTx(ctx, tx, spec.depTable, ids[start:end]) - if err != nil { - return plan, err - } - } - plan.chunks = append(plan.chunks, chunk) - } - return plan, nil -} - -// runRecomputePassInTx is one fixpoint pass over the plan, -// statement-for-statement equivalent to runMarkUnmarkBatchedInTx over the -// plan's full ids. Per chunk: the mark and unmark templates run over the ids -// with edges — the union, scoped to them, holds exactly what it held scoped to -// the whole chunk, and so does the batch's exogeneity read (an edgeless id -// names no parent) — and then, on the first pass only, a plain unmark clears -// the edgeless ids. It runs after both templates so their unions see the -// edgeless rows' pre-pass is_blocked, as they did when those rows were cleared -// by the chunk's own unmark statement; on later passes it would match no row -// (nothing marks an edgeless id), so it is skipped. -// -// It is a function rather than a method on recomputePlan because the -// structural write guards follow bare-identifier calls only: as a method, the -// mark/unmark DML it reaches would be invisible from the exported recompute -// entry points, and their versionExemptions entries would read as stale -// (TestEveryBeadMutatorMintsOrIsExempt). -func runRecomputePassInTx(ctx context.Context, tx DBTX, p recomputePlan, firstPass bool) (int64, error) { - var changed int64 - for _, chunk := range p.chunks { - if len(chunk.withEdges) > 0 { - n, err := runMarkUnmarkBatchedInTx(ctx, tx, p.spec, chunk.withEdges) - changed += n - if err != nil { - return changed, err - } - } - if !firstPass || len(chunk.edgeless) == 0 { - continue - } - placeholders, args := buildSQLInClause(chunk.edgeless) - //nolint:gosec // G201: table is one of two constants; only placeholders are formatted in. - res, err := tx.ExecContext(ctx, fmt.Sprintf(` - UPDATE %[1]s t SET t.is_blocked = 0, t.updated_at = t.updated_at - WHERE t.id IN (%[2]s) AND t.is_blocked = 1 - `, p.spec.table, placeholders), args...) - if err != nil { - return changed, fmt.Errorf("recompute is_blocked (unmark): %w", err) - } - n, err := res.RowsAffected() - if err != nil { - return changed, fmt.Errorf("recompute is_blocked (unmark rows affected): %w", err) - } - changed += n - } - return changed, nil -} - -// splitByOwnDependencyRowsInTx partitions ids by whether depTable holds at -// least one row with that id as its issue_id, preserving input order. -// -//nolint:gosec // G201: depTable is one of two constants; only placeholders are formatted in. -func splitByOwnDependencyRowsInTx(ctx context.Context, tx DBTX, depTable string, ids []string) (withEdges, withoutEdges []string, err error) { - placeholders, args := buildSQLInClause(ids) - rows, err := tx.QueryContext(ctx, fmt.Sprintf( - "SELECT DISTINCT issue_id FROM %s WHERE issue_id IN (%s)", depTable, placeholders), args...) - if err != nil { - return nil, nil, fmt.Errorf("recompute is_blocked: probe %s: %w", depTable, err) - } - has := make(map[string]bool, len(ids)) - for rows.Next() { - var id string - if err := rows.Scan(&id); err != nil { - _ = rows.Close() - return nil, nil, fmt.Errorf("recompute is_blocked: probe %s: %w", depTable, err) - } - has[id] = true - } - _ = rows.Close() - if err := rows.Err(); err != nil { - return nil, nil, fmt.Errorf("recompute is_blocked: probe %s: %w", depTable, err) - } - for _, id := range ids { - if has[id] { - withEdges = append(withEdges, id) - } else { - withoutEdges = append(withoutEdges, id) - } - } - return withEdges, withoutEdges, nil -} - -func runMarkBatchedInTx(ctx context.Context, tx DBTX, spec blockedTableSpec, ids []string) (int64, error) { +//nolint:gosec // G201: callers pass constant templates; only IN-clause placeholders are formatted in. +func runMarkBatchedInTx(ctx context.Context, tx DBTX, markTmpl string, ids []string) (int64, error) { var changed int64 for start := 0; start < len(ids); start += queryBatchSize { end := start + queryBatchSize @@ -615,17 +441,8 @@ func runMarkBatchedInTx(ctx context.Context, tx DBTX, spec blockedTableSpec, ids end = len(ids) } placeholders, args := buildSQLInClause(ids[start:end]) - explained, err := batchExogeneity(ctx, tx, spec, placeholders, args) - if err != nil { - return changed, fmt.Errorf("mark is_blocked (exogeneity): %w", err) - } - stmt, stmtArgs, err := expandBatchTemplate( - markBlockedTemplate(spec, explained), placeholders, args, explained.args()) - if err != nil { - return changed, fmt.Errorf("mark is_blocked: %w", err) - } - res, err := tx.ExecContext(ctx, stmt, stmtArgs...) + res, err := tx.ExecContext(ctx, fmt.Sprintf(markTmpl, placeholders), args...) if err != nil { return changed, fmt.Errorf("mark is_blocked: %w", err) } @@ -690,9 +507,6 @@ func AffectedByDepChangeInTx(ctx context.Context, tx DBTX, source, target string if err := loadWaitersOnSpawnerIDsInTx(ctx, tx, []string{target}, &issueSeed, issueSeen, &wispSeed, wispSeen); err != nil { return nil, nil, err } - if err := appendSiblingsUnderAncestorsInTx(ctx, tx, target, &issueSeed, issueSeen, &wispSeed, wispSeen); err != nil { - return nil, nil, err - } } return expandByParentChildDescendantsInTx(ctx, tx, issueSeed, wispSeed, issueSeen, wispSeen) default: @@ -711,9 +525,6 @@ func AffectedByDepChangeForWispInTx(ctx context.Context, tx DBTX, source, target if err := loadWaitersOnSpawnerIDsInTx(ctx, tx, []string{target}, &issueSeed, issueSeen, &wispSeed, wispSeen); err != nil { return nil, nil, err } - if err := appendSiblingsUnderAncestorsInTx(ctx, tx, target, &issueSeed, issueSeen, &wispSeed, wispSeen); err != nil { - return nil, nil, err - } } return expandByParentChildDescendantsInTx(ctx, tx, issueSeed, wispSeed, issueSeen, wispSeen) default: @@ -721,120 +532,6 @@ func AffectedByDepChangeForWispInTx(ctx context.Context, tx DBTX, source, target } } -// appendSiblingsUnderAncestorsInTx seeds every existing parent-child child of -// parent AND of each of parent's own ancestors (of either kind) for recompute. -// -// A parent-child edge onto a parent that BLOCKS on the new child reclassifies -// that blocking reason from exogenous to subtree-derived, and the cascade legs -// propagate only exogenous blockedness (gastownhall/beads#6506, -// parentsExplainedBySubtreeSQL). So writing or removing one edge can flip the -// answer for the parent's OTHER children, which no other seed in this walk -// reaches: the close-gate epic is built one child at a time, and the second -// edge is what un-darkens the first child. -// -// The walk goes all the way UP because the exogeneity test is over the whole -// SUBTREE, not over direct children. Hanging G under C changes nothing about C -// but everything about any ANCESTOR of C that blocks on G: that reason just -// moved inside its subtree, so the ancestor stops darkening its own children. -// The reviewer's depth-2 lock is exactly this shape (P blocks G; pc C -> P, -// C2 -> P, G -> C), and only P's sibling set — not C's — carries the flip. -// Seeding each ancestor's whole sibling set, which -// expandByParentChildDescendantsInTx then expands to their subtrees, is what -// keeps the incremental write path agreeing with the full repair. -func appendSiblingsUnderAncestorsInTx( - ctx context.Context, tx DBTX, - parent string, - issueSeed *[]string, issueSeen map[string]bool, - wispSeed *[]string, wispSeen map[string]bool, -) error { - parents, err := ancestorChainInTx(ctx, tx, parent) - if err != nil { - return err - } - if len(parents) == 0 { - return nil - } - // The parents' own kinds are not known here, so probe both target columns; - // the one that does not match a parent simply returns no rows. - for _, w := range []struct { - depTable, parentCol string - seed *[]string - seen map[string]bool - }{ - {"dependencies", "depends_on_issue_id", issueSeed, issueSeen}, - {"wisp_dependencies", "depends_on_issue_id", wispSeed, wispSeen}, - {"dependencies", "depends_on_wisp_id", issueSeed, issueSeen}, - {"wisp_dependencies", "depends_on_wisp_id", wispSeed, wispSeen}, - } { - if err := appendChildrenInTx(ctx, tx, w.depTable, w.parentCol, parents, w.seen, w.seed); err != nil { - return err - } - } - return nil -} - -// ancestorChainInTx returns id, plus every parent-child ancestor of id THAT -// CARRIES A BLOCKING REASON OF ITS OWN — a dependency row that is not -// parent-child. Order is not meaningful. -// -// The filter is what keeps the walk from being quadratic. Only an ancestor -// that holds a blocking reason can have that reason reclassified by an edge -// below it, so only such an ancestor's children can flip; an ancestor with -// nothing but hierarchy edges contributes seeds that can never change. Without -// the filter a 70-deep chain reseeds every ancestor's subtree on every edge -// written into it, which is O(depth x size) per write and times out. -// -// It walks with isAncestorInTx's own recursive members (reachabilityMembers in -// dependencies.go): one per dependency table, each an index probe on issue_id -// pinned by join hints, merged by UNION distinct, so a malformed diamond or -// cycle in the hierarchy terminates by unique reachable node rather than -// running away. The members project NULL for a row that is not parent-child, -// which the outer filter drops. Joining the frontier to a derived UNION of the -// two tables instead, this walk's first shape, is the scan #7224 removed from -// isAncestorInTx (minutes per walk at 100k edges), and it runs here on every -// parent-child write and merge replay. TestGraphWalkPlansHonorJoinHints -// (embedded) pins the lookup plan. -// -//nolint:gosec // G201: the members are built from constant table names and type filters. -func ancestorChainInTx(ctx context.Context, tx DBTX, id string) ([]string, error) { - if id == "" { - return nil, nil - } - query := fmt.Sprintf(` - WITH RECURSIVE ancestors(node) AS ( - SELECT ? - UNION - %s - ) - SELECT node FROM ancestors - WHERE node IS NOT NULL - AND ( node = ? - OR EXISTS (SELECT 1 FROM dependencies x - WHERE x.issue_id = ancestors.node AND x.type <> 'parent-child') - OR EXISTS (SELECT 1 FROM wisp_dependencies x - WHERE x.issue_id = ancestors.node AND x.type <> 'parent-child') ) - `, reachabilityMembers("ancestors", cycleDetectionTables(), "d.type = 'parent-child'")) - - rows, err := tx.QueryContext(ctx, query, id, id) - if err != nil { - return nil, fmt.Errorf("load ancestor chain of %s: %w", id, err) - } - defer func() { _ = rows.Close() }() - - var out []string - for rows.Next() { - var node string - if err := rows.Scan(&node); err != nil { - return nil, fmt.Errorf("scan ancestor chain of %s: %w", id, err) - } - out = append(out, node) - } - if err := rows.Err(); err != nil { - return nil, fmt.Errorf("ancestor chain rows for %s: %w", id, err) - } - return out, nil -} - func loadBlockingDependersInTx( ctx context.Context, tx DBTX, targetCol, id string, @@ -1009,19 +706,13 @@ func appendChildrenInTx( if len(parentIDs) == 0 { return nil } - // One IN-list read per chunk of parents rather than one read per parent. - // The set of children found does not depend on how the parents are - // grouped; only the order they join the queue can, and every caller - // treats the queue as a set to recompute. - for start := 0; start < len(parentIDs); start += queryBatchSize { - end := min(start+queryBatchSize, len(parentIDs)) - placeholders, args := buildSQLInClause(parentIDs[start:end]) - query := fmt.Sprintf(` - SELECT issue_id FROM %s - WHERE type = 'parent-child' - AND %s IN (%s) - `, depTable, parentCol, placeholders) - rows, err := tx.QueryContext(ctx, query, args...) + query := fmt.Sprintf(` + SELECT issue_id FROM %s + WHERE type = 'parent-child' + AND %s = ? + `, depTable, parentCol) + for _, parentID := range parentIDs { + rows, err := tx.QueryContext(ctx, query, parentID) if err != nil { return fmt.Errorf("expand children from %s on %s: %w", depTable, parentCol, err) } diff --git a/internal/storage/issueops/blocked_state_result_test.go b/internal/storage/issueops/blocked_state_result_test.go index 50d243b7d..e84ef356c 100644 --- a/internal/storage/issueops/blocked_state_result_test.go +++ b/internal/storage/issueops/blocked_state_result_test.go @@ -25,45 +25,13 @@ func newBlockedStateResultMock(t *testing.T) (sqlmock.Sqlmock, DBTX) { return mock, db } -// expectBatchExogeneityRead is the batch-scoped exogeneity read every batched -// pass now runs before its statements: first the parent-kind guard, and then -// one read per kind the batch actually names. Answering the guard with no -// parents of either kind is also the pin on the guard itself — a batch with no -// parent-child row must not run either read. -// -// Ordered before the execs because sqlmock is ordered and the runner reads -// once per batch, then binds the ids into both statements. -func expectBatchExogeneityRead(mock sqlmock.Sqlmock, depTable string) { - mock.ExpectQuery(regexp.QuoteMeta("SELECT COUNT(d.depends_on_issue_id), COUNT(d.depends_on_wisp_id)")). - WillReturnRows(sqlmock.NewRows([]string{"issue_parents", "wisp_parents"}).AddRow(0, 0)) -} - -// expectOwnEdgesProbe expects planRecomputeInTx's probe of depTable and -// answers that every id in withEdges has a dependency row of its own. -func expectOwnEdgesProbe(mock sqlmock.Sqlmock, depTable string, withEdges ...string) { - rows := sqlmock.NewRows([]string{"issue_id"}) - for _, id := range withEdges { - rows.AddRow(id) - } - mock.ExpectQuery(regexp.QuoteMeta("SELECT DISTINCT issue_id FROM " + depTable + " WHERE issue_id IN")). - WillReturnRows(rows) -} - func expectBlockedStatePass(mock sqlmock.Sqlmock, table, alias string, mark, unmark driver.Result) { - expectBatchExogeneityRead(mock, blockedSpecFor(table).depTable) mock.ExpectExec(regexp.QuoteMeta("UPDATE " + table + " " + alias + " SET " + alias + ".is_blocked = 1")). WillReturnResult(mark) mock.ExpectExec(regexp.QuoteMeta("UPDATE " + table + " " + alias + " SET " + alias + ".is_blocked = 0")). WillReturnResult(unmark) } -func blockedSpecFor(table string) blockedTableSpec { - if table == "wisps" { - return wispsBlockedSpec - } - return issuesBlockedSpec -} - func TestRecomputeIsBlockedInTxWithResult(t *testing.T) { t.Run("issue rows changed", func(t *testing.T) { mock, db := newBlockedStateResultMock(t) @@ -115,7 +83,6 @@ func TestRunMarkUnmarkBatchedInTxPropagatesRowsAffectedErrors(t *testing.T) { for _, phase := range []string{"mark", "unmark"} { t.Run(phase, func(t *testing.T) { mock, db := newBlockedStateResultMock(t) - expectBatchExogeneityRead(mock, "dependencies") if phase == "mark" { mock.ExpectExec(regexp.QuoteMeta("UPDATE issues i SET i.is_blocked = 1")). WillReturnResult(sqlmock.NewErrorResult(sentinel)) @@ -127,7 +94,7 @@ func TestRunMarkUnmarkBatchedInTxPropagatesRowsAffectedErrors(t *testing.T) { } _, err := runMarkUnmarkBatchedInTx( - context.Background(), db, issuesBlockedSpec, []string{"issue-1"}, + context.Background(), db, markBlockedTemplateForIssues(), unmarkBlockedTemplateForIssues(), []string{"issue-1"}, ) if !errors.Is(err, sentinel) { t.Fatalf("err = %v, want rows-affected error", err) @@ -135,39 +102,3 @@ func TestRunMarkUnmarkBatchedInTxPropagatesRowsAffectedErrors(t *testing.T) { }) } } - -// TestRecomputeIsBlockedSkipsUnionForIDsWithoutEdges pins the no-edge -// shortcut a create asks for (the single-id cases above pin that nobody else -// pays its probe): an id with no dependency row of its own cannot be in the scoped -// should-be-blocked union, so it never reaches the union statements. It is -// cleared by one plain unmark on the first pass, after the chunk's union -// statements (which must see its pre-pass is_blocked, as the chunk's own -// unmark once did), and not revisited on later passes. -func TestRecomputeIsBlockedSkipsUnionForIDsWithoutEdges(t *testing.T) { - mock, db := newBlockedStateResultMock(t) - expectOwnEdgesProbe(mock, "dependencies", "issue-2") - // Pass 1: the union statements over the id with edges, then the plain - // unmark over the edgeless ids. - expectBatchExogeneityRead(mock, "dependencies") - mock.ExpectExec(regexp.QuoteMeta("UPDATE issues i SET i.is_blocked = 1")). - WillReturnResult(sqlmock.NewResult(0, 1)) - mock.ExpectExec(regexp.QuoteMeta("UPDATE issues i SET i.is_blocked = 0")). - WillReturnResult(sqlmock.NewResult(0, 0)) - mock.ExpectExec(regexp.QuoteMeta("UPDATE issues t SET t.is_blocked = 0, t.updated_at = t.updated_at")). - WithArgs("issue-1", "issue-3"). - WillReturnResult(sqlmock.NewResult(0, 1)) - // Pass 2: only the id with edges. - expectBatchExogeneityRead(mock, "dependencies") - mock.ExpectExec(regexp.QuoteMeta("UPDATE issues i SET i.is_blocked = 1")). - WillReturnResult(sqlmock.NewResult(0, 0)) - mock.ExpectExec(regexp.QuoteMeta("UPDATE issues i SET i.is_blocked = 0")). - WillReturnResult(sqlmock.NewResult(0, 0)) - - result, err := recomputeIsBlockedInTxWithResult(context.Background(), db, []string{"issue-1", "issue-2", "issue-3"}, nil, true) - if err != nil { - t.Fatalf("RecomputeIsBlockedInTxWithResult: %v", err) - } - if !result.IssueRowsChanged || result.WispRowsChanged { - t.Fatalf("result = %+v, want issue change only", result) - } -} diff --git a/internal/storage/issueops/blocked_state_template_test.go b/internal/storage/issueops/blocked_state_template_test.go deleted file mode 100644 index e59c7e251..000000000 --- a/internal/storage/issueops/blocked_state_template_test.go +++ /dev/null @@ -1,213 +0,0 @@ -package issueops - -import ( - "strings" - "testing" -) - -// The batched mark/unmark templates reach the engine through -// expandBatchTemplate, which fills the batch IN-lists by walking the template -// for "%s" and interleaves the exogeneity ids the template already carries as -// "?" placeholders. Three things are therefore load-bearing and invisible to -// the sqlmock suite next door — its expectations match an unanchored statement -// prefix and never assert bound args, so it stays green against a miscounted -// template: the %s occurrence count, the absence of any other percent sign in -// the finished template (a future LIKE 'x%' or %d in a union leg would shift -// the count or corrupt the statement), and the absence of any "?" the caller -// did not bind. Today a break surfaces only in the container-backed dolt -// suite; these tests pin it at the fast tier (gastownhall/beads#6291). - -// batchTemplateOccurrences is the number of batch IN-lists a batched template -// carries: the outer row filter, and one per leg of the scoped -// should-be-blocked union. -// -// It was briefly 8: the parent-child legs' exogeneity set was spliced as the -// query that derives it, and that query carried the batch scope twice more. -// The set is now read once per batch and bound as ids -// (scopedExplainedParentsInTx), which is both 6.9x faster on a 200-id batch -// and why the ? group below exists. -const batchTemplateOccurrences = 6 - -// explainedFixture is a non-empty exogeneity set of each kind, so the bound-? -// plumbing is exercised rather than degenerating to the empty case. -func explainedFixture() subtreeExplainedParents { - return subtreeExplainedParents{ - issueParents: []string{"parent-1", "parent-2"}, - wispParents: []string{"wisp-parent-1"}, - } -} - -func batchedTemplates(explained subtreeExplainedParents) map[string]string { - return map[string]string{ - "markBlockedTemplateForIssues": markBlockedTemplateForIssues(explained), - "unmarkBlockedTemplateForIssues": unmarkBlockedTemplateForIssues(explained), - "markBlockedTemplateForWisps": markBlockedTemplateForWisps(explained), - "unmarkBlockedTemplateForWisps": unmarkBlockedTemplateForWisps(explained), - } -} - -func TestBatchedTemplatePercentBudget(t *testing.T) { - for name, tmpl := range batchedTemplates(explainedFixture()) { - t.Run(name, func(t *testing.T) { - if got := strings.Count(tmpl, "%s"); got != batchTemplateOccurrences { - t.Errorf("%%s count = %d, want %d — expandBatchTemplate binds one batch id group per occurrence, so a changed count must be a deliberate edit here", got, batchTemplateOccurrences) - } - // The stray-percent guard: every percent sign in the finished - // template must belong to one of the %s verbs counted above. - if got := strings.Count(tmpl, "%"); got != batchTemplateOccurrences { - t.Errorf("total %% count = %d, want %d — a percent sign outside a %%s (LIKE 'x%%', %%d, %%%%) miscounts the template or corrupts the statement", got, batchTemplateOccurrences) - } - }) - } -} - -// TestBatchedTemplateQuestionMarksAreTheExogeneitySet pins the other half of -// expandBatchTemplate's contract: the ONLY ? a template carries are the two -// parent-child legs' bound exogeneity ids, so an empty set leaves none. -func TestBatchedTemplateQuestionMarksAreTheExogeneitySet(t *testing.T) { - explained := explainedFixture() - want := len(explained.issueParents) + len(explained.wispParents) - for name, tmpl := range batchedTemplates(explained) { - t.Run(name, func(t *testing.T) { - if got := strings.Count(tmpl, "?"); got != want { - t.Errorf("? count = %d, want %d (the bound exogeneity ids)", got, want) - } - }) - } - for name, tmpl := range batchedTemplates(subtreeExplainedParents{}) { - t.Run(name+"/empty", func(t *testing.T) { - if got := strings.Count(tmpl, "?"); got != 0 { - t.Errorf("? count = %d over an empty exogeneity set, want 0", got) - } - }) - } -} - -func TestWaitsForGateBlockedSQLCarriesNoPercentOrPlaceholder(t *testing.T) { - // The gate is spliced into every template as a resolved constant, so any - // percent sign or placeholder it grew would land in the counted text above. - if got := strings.Count(waitsForGateBlockedSQL, "%"); got != 0 { - t.Errorf("waitsForGateBlockedSQL %% count = %d, want 0", got) - } - if got := strings.Count(waitsForGateBlockedSQL, "?"); got != 0 { - t.Errorf("waitsForGateBlockedSQL ? count = %d, want 0", got) - } -} - -func TestExpandBatchTemplateFillsEveryOccurrence(t *testing.T) { - placeholders, args := buildSQLInClause([]string{"issue-1", "issue-2"}) - explained := explainedFixture() - bound := explained.args() - wantArgs := batchTemplateOccurrences*len(args) + len(bound) - - for name, tmpl := range batchedTemplates(explained) { - t.Run(name, func(t *testing.T) { - stmt, stmtArgs, err := expandBatchTemplate(tmpl, placeholders, args, bound) - if err != nil { - t.Fatalf("expandBatchTemplate: %v", err) - } - - if strings.ContainsAny(stmt, "%") { - t.Errorf("expanded statement still contains %%: %s", stmt) - } - if got := strings.Count(stmt, "?"); got != wantArgs { - t.Errorf("placeholder count = %d, want %d", got, wantArgs) - } - if got := len(stmtArgs); got != wantArgs { - t.Fatalf("arg count = %d, want %d — placeholders and args must stay in lockstep", got, wantArgs) - } - // TEXT ORDER: the batch group for the outer filter and legs 1-2, - // then leg 3's batch group followed by the issue parents, then leg - // 4's batch group followed by the wisp parents, then leg 5's. - var want []interface{} - for k := 0; k < batchTemplateOccurrences; k++ { - want = append(want, args...) - if k == 3 { - want = append(want, "parent-1", "parent-2") - } - if k == 4 { - want = append(want, "wisp-parent-1") - } - } - for i, arg := range stmtArgs { - if arg != want[i] { - t.Errorf("arg %d = %v, want %v", i, arg, want[i]) - } - } - }) - } -} - -// TestExpandBatchTemplateRefusesABoundMismatch is the guard that keeps a -// miscounted bound group from reaching the engine as an opaque argument-count -// error: the template's ? placeholders and the bound ids must agree exactly. -func TestExpandBatchTemplateRefusesABoundMismatch(t *testing.T) { - placeholders, args := buildSQLInClause([]string{"issue-1"}) - tmpl := markBlockedTemplateForIssues(subtreeExplainedParents{issueParents: []string{"parent-1"}}) - - if _, _, err := expandBatchTemplate(tmpl, placeholders, args, nil); err == nil { - t.Error("want an error when the template carries a ? the caller did not bind") - } - if _, _, err := expandBatchTemplate( - tmpl, placeholders, args, []interface{}{"parent-1", "parent-2"}); err == nil { - t.Error("want an error when the caller binds more ids than the template carries") - } -} - -func TestExpandBatchTemplateSingleOccurrenceDegrades(t *testing.T) { - // A template with one %s and no bound ids is the plain Sprintf it always - // was: the args pass through untouched rather than being repeated. - placeholders, args := buildSQLInClause([]string{"issue-1", "issue-2"}) - stmt, stmtArgs, err := expandBatchTemplate( - "SELECT id FROM issues WHERE id IN (%s)", placeholders, args, nil) - if err != nil { - t.Fatalf("expandBatchTemplate: %v", err) - } - - if want := "SELECT id FROM issues WHERE id IN (?,?)"; stmt != want { - t.Errorf("stmt = %q, want %q", stmt, want) - } - if len(stmtArgs) != len(args) { - t.Errorf("arg count = %d, want %d", len(stmtArgs), len(args)) - } -} - -// TestBatchedTemplatesPinLookupJoins pins the join hints on the scoped -// should-be-blocked union's four joined legs: without them the Dolt -// sql-server planner intermittently drives the issues legs from an index -// scan of every open issue, 6-10 s per statement over an import's -// uncommitted working set (see shouldBeBlockedIDsUnionScopedPrecomputedSQL). -// The unscoped union (full repair, doctor count) keeps the planner's choice. -func TestBatchedTemplatesPinLookupJoins(t *testing.T) { - for name, tmpl := range batchedTemplates(explainedFixture()) { - for _, hint := range []string{"JOIN_ORDER(d, t) LOOKUP_JOIN(d, t)", "JOIN_ORDER(d, p) LOOKUP_JOIN(d, p)"} { - if got := strings.Count(tmpl, hint); got != 2 { - t.Errorf("%s carries %q %d times, want 2 (one per joined issues/wisps leg)", name, hint, got) - } - } - } - for _, depTable := range []string{"dependencies", "wisp_dependencies"} { - union := shouldBeBlockedIDsUnionPrecomputedSQL(depTable, explainedFixture()) - if strings.Contains(union, "LOOKUP_JOIN(d, t)") || strings.Contains(union, "LOOKUP_JOIN(d, p)") { - t.Errorf("unscoped union over %s should not pin its legs' joins", depTable) - } - } -} - -// TestWaitsForGateSplitsSpawnerColumns pins the waits-for gate's per-column -// EXISTS split: one OR across both spawner columns inside an EXISTS has no -// index to use and scans the edge table per evaluation (see -// waitsForGateBlockedSQL). Two tables x two columns x {open, closed} children. -func TestWaitsForGateSplitsSpawnerColumns(t *testing.T) { - if got := strings.Count(waitsForGateBlockedSQL, "JOIN_ORDER(cd, child) LOOKUP_JOIN(cd, child)"); got != 8 { - t.Errorf("gate pins %d child lookups, want 8", got) - } - if strings.Contains(waitsForGateBlockedSQL, "OR (d.depends_on_wisp_id IS NOT NULL") { - t.Errorf("gate still ORs the spawner columns inside one EXISTS") - } - for _, col := range []string{"depends_on_issue_id", "depends_on_wisp_id"} { - if got := strings.Count(waitsForGateBlockedSQL, "cd."+col+" = d."+col); got != 4 { - t.Errorf("gate matches cd.%s in %d EXISTS, want 4", col, got) - } - } -} diff --git a/internal/storage/issueops/blocking_annotation.go b/internal/storage/issueops/blocking_annotation.go index bec8b48c8..49ef3a99c 100644 --- a/internal/storage/issueops/blocking_annotation.go +++ b/internal/storage/issueops/blocking_annotation.go @@ -85,28 +85,5 @@ func ExecuteBlockingAnnotation(ctx context.Context, tx DBTX, request publicops.B if err != nil { return publicops.BlockingResult{}, err } - result := FinishBlockingAnnotation(anchors, blockedBy, blocks, parent) - if request.IncludeIsBlocked { - flags, err := IsBlockedBatchInTx(ctx, tx, anchors) - if err != nil { - return publicops.BlockingResult{}, err - } - ApplyBlockedFlags(&result, flags) - } - return result, nil -} - -// ApplyBlockedFlags stamps BlockingRequest.IncludeIsBlocked's answer onto a -// finished annotation: each item whose id has a row in flags gets that row's -// stored transitive is_blocked flag, and an item with none keeps a nil flag. -// It is shared by every body that answers the role, so "absent from flags" -// means "no row" on all of them. -func ApplyBlockedFlags(result *publicops.BlockingResult, flags map[string]bool) { - for i := range result.Items { - blocked, ok := flags[result.Items[i].ID] - if !ok { - continue - } - result.Items[i].IsBlocked = &blocked - } + return FinishBlockingAnnotation(anchors, blockedBy, blocks, parent), nil } diff --git a/internal/storage/issueops/claim.go b/internal/storage/issueops/claim.go index 8d5733bfb..a55e2481a 100644 --- a/internal/storage/issueops/claim.go +++ b/internal/storage/issueops/claim.go @@ -31,21 +31,9 @@ type ClaimResult struct { // success (supports agent retry workflows). // Routes to the correct table (issues/wisps) automatically. // The caller is responsible for Dolt versioning (DOLT_ADD/COMMIT) if needed. -// A claim that wrote the row mints one version row (assignee, status and -// started_at are durable state); the idempotent re-claim mints nothing. -func ClaimIssueInTx(ctx context.Context, tx DBTX, id string, actor string) (*ClaimResult, error) { - return claimIssueInTx(ctx, tx, id, actor, true) -} - -// claimIssueInTx is the body of ClaimIssueInTx. mintVersion controls whether a -// successful claim mints its own version row: the exported entry point always -// does, while ExecuteUpdate — which may apply field, label, parent and -// persistence patches after the claim in the same call — passes false and -// mints exactly once after the last of them, so the version carries the final -// state and one guarded update never records two. // //nolint:gosec // G201: table names come from WispTableRouting (hardcoded constants) -func claimIssueInTx(ctx context.Context, tx DBTX, id string, actor string, mintVersion bool) (*ClaimResult, error) { +func ClaimIssueInTx(ctx context.Context, tx DBTX, id string, actor string) (*ClaimResult, error) { // The CAS below writes assignee = actor. actor is user-settable (--actor / // BEADS_ACTOR), so bound it against the VARCHAR(255) assignee column up front // and return a typed ErrFieldTooLong rather than a raw backend error. @@ -230,16 +218,10 @@ func claimIssueInTx(ctx context.Context, tx DBTX, id string, actor string, mintV // A claim changes assignee and status, so it journals as an update. The // idempotent re-claim path returns above without writing and journals - // nothing — and mints nothing: the version row below is for a claim that - // actually rewrote the row. + // nothing. if err := RecordEventInTx(ctx, tx, EventUpdate, id, actor); err != nil { return nil, err } - if mintVersion { - if err := RecordVersionInTx(ctx, tx, id, actor); err != nil { - return nil, err - } - } return &ClaimResult{OldIssue: oldIssue, IsWisp: isWisp}, nil } diff --git a/internal/storage/issueops/close.go b/internal/storage/issueops/close.go index 03f9fbb94..170a0218b 100644 --- a/internal/storage/issueops/close.go +++ b/internal/storage/issueops/close.go @@ -11,7 +11,6 @@ import ( "github.com/steveyegge/beads/internal/storage" "github.com/steveyegge/beads/internal/types" - publicops "github.com/steveyegge/beads/issueops" ) const closeCheckedSavepointPrefix = "issueops_close_checked_" @@ -54,12 +53,6 @@ func CloseIssueWithoutEventInTx(ctx context.Context, tx DBTX, id string, reason, // is_blocked column whose direct blockers have since closed. Reading the live // blocker list self-heals against a stale column instead of acting on it. // -// Before close policy it applies the close guards (enforceCloseGuardsInTx) to a -// target that is not already closed: a template refuses with -// *TemplateReadOnlyError whatever force says, and unless force is set a pinned -// issue refuses with *PinnedError and one assigned to someone other than actor -// with *CloseNotAssigneeError. -// // When expectedVersion is non-nil it adds an ORTHOGONAL optimistic-concurrency // precondition: the row's current RowVersion (row_lock) must still equal // *expectedVersion or the close refuses with storage.ErrVersionMismatch. This @@ -86,14 +79,6 @@ func CloseIssueCheckedInTx(ctx context.Context, tx DBTX, id, reason, actor, sess if !found { return nil, fmt.Errorf("%w: issue %s", storage.ErrNotFound, id) } - // The close guards run before close policy, so a target that trips both - // reads the guard's refusal, as `bd close` always printed it. They only - // read, so they need no savepoint. - if !closed { - if err := enforceCloseGuardsInTx(ctx, tx, id, targetColumn, actor, force); err != nil { - return nil, err - } - } // domain/db also supplies a *sql.DB Runner, whose independently pooled // statements cannot retain a savepoint. The shared UOW uses a pinned // *sql.Conn after START TRANSACTION, while embedded callers use *sql.Tx. @@ -166,12 +151,12 @@ func enforceClosePolicyForTargetInTx(ctx context.Context, tx DBTX, id, targetCol return 0, &storage.CloseOpenChildrenError{IssueID: id, OpenChildren: openChildren} } if !force && !closed { - blocked, blockers, err := isBlockedByInTx(ctx, tx, id) + blocked, blockers, err := IsBlockedInTx(ctx, tx, id) if err != nil { return 0, err } if blocked && len(blockers) > 0 { - return 0, &publicops.BlockedError{IssueID: id, Blockers: blockers, Err: storage.ErrCloseBlocked} + return 0, fmt.Errorf("%w: %s is blocked by %v", storage.ErrCloseBlocked, id, blockers) } } return openChildren, nil @@ -399,17 +384,10 @@ func closeIssueInTx(ctx context.Context, tx DBTX, id string, reason, actor, sess NoteStatusChangeBlockedRecheck(tx, id, string(types.StatusClosed), affectedIssues, affectedWisps) // Snapshot only after all derived blocked-state maintenance has completed. - // recordEvent gates the human audit event, never the journal — nor the - // version row: a close changes status, closed_at, close_reason and - // closed_by_session, all durable state, so it mints whether or not the - // audit event was suppressed (as update.go already does). The rows == 0 - // return above (already closed) wrote nothing and mints nothing. + // recordEvent gates the human audit event, never the journal. if err := RecordEventInTx(ctx, tx, EventClose, id, actor); err != nil { return nil, err } - if err := RecordVersionInTx(ctx, tx, id, actor); err != nil { - return nil, err - } return &CloseResult{IsWisp: isWisp, IssueRowsChanged: !isWisp || recompute.IssueRowsChanged}, nil } diff --git a/internal/storage/issueops/close_batch.go b/internal/storage/issueops/close_batch.go index 4d468323e..5fce1ff91 100644 --- a/internal/storage/issueops/close_batch.go +++ b/internal/storage/issueops/close_batch.go @@ -59,19 +59,8 @@ func ValidateCloseBatchRequest(request publicops.CloseBatchRequest) error { func CloseBatchCommitMessage(result publicops.CloseBatchResult) string { ids := make([]string, 0, len(result.Outcomes)) ephemeral := 0 - var autoClosed []string for _, outcome := range result.Outcomes { - if outcome.Err != nil { - continue - } - if root := outcome.AutoClosedMolecule; root != nil { - if IsWisp(root) { - ephemeral++ - } else { - autoClosed = append(autoClosed, root.ID) - } - } - if !outcome.Changed { + if outcome.Err != nil || !outcome.Changed { continue } if closeOutcomeIsEphemeral(outcome) { @@ -90,9 +79,6 @@ func CloseBatchCommitMessage(result publicops.CloseBatchResult) string { case ephemeral > 1: parts = append(parts, fmt.Sprintf("close %d ephemeral items", ephemeral)) } - if len(autoClosed) > 0 { - parts = append(parts, "auto-close "+strings.Join(autoClosed, ", ")) - } if result.ClaimedNext != nil { parts = append(parts, "claim "+result.ClaimedNext.ID) } @@ -129,30 +115,11 @@ func closeOutcomeIsEphemeral(outcome publicops.CloseOutcome) bool { // claim, and so does a batch in which nothing landed — landed meaning an item // that CHANGED, so a batch whose items were all already closed earns no claim. func ExecuteCloseBatch(ctx context.Context, tx *sql.Tx, request publicops.CloseBatchRequest, claimFilter *types.WorkFilter) (publicops.CloseBatchResult, ChangedTables, error) { - return ExecuteCloseBatchWithPolicy(ctx, tx, request, claimFilter, storage.BatchClosePolicy{}) -} - -// ExecuteCloseBatchWithPolicy applies an externally resolved snapshot without -// splitting the closes and next claim into separate transactions. -func ExecuteCloseBatchWithPolicy(ctx context.Context, tx *sql.Tx, request publicops.CloseBatchRequest, claimFilter *types.WorkFilter, policy storage.BatchClosePolicy) (publicops.CloseBatchResult, ChangedTables, error) { result := publicops.CloseBatchResult{Outcomes: make([]publicops.CloseOutcome, len(request.Items))} tables := ChangedTables{} landed := 0 for i, item := range request.Items { - if err := policy.CheckClose(item.IssueID, request.Force); err != nil { - // Preserve idempotent re-closes and not-found precedence. Only - // a live target can be refused by the external close policy. - closed, _, found, readErr := isClosedInTx(ctx, tx, item.IssueID) - if readErr != nil { - result.Outcomes[i] = publicops.CloseOutcome{IssueID: item.IssueID, Err: readErr} - continue - } - if found && !closed { - result.Outcomes[i] = publicops.CloseOutcome{IssueID: item.IssueID, Err: err} - continue - } - } closed, changedTables, err := ExecuteClose(ctx, tx, publicops.CloseRequest{ Actor: request.Actor, IssueID: item.IssueID, @@ -176,27 +143,8 @@ func ExecuteCloseBatchWithPolicy(ctx context.Context, tx *sql.Tx, request public } } - if request.AutoCloseMolecule { - closedAt := make([]int, 0, len(result.Outcomes)) - stepIDs := make([]string, 0, len(result.Outcomes)) - for i, outcome := range result.Outcomes { - if outcome.Err == nil { - closedAt = append(closedAt, i) - stepIDs = append(stepIDs, outcome.IssueID) - } - } - autos, autoTables, err := CloseCompletedMoleculesInTx(ctx, tx, stepIDs, request.Actor, request.Session) - if err != nil { - return publicops.CloseBatchResult{}, nil, err - } - tables.Merge(autoTables) - for j, i := range closedAt { - result.Outcomes[i].AutoClosedMolecule, result.Outcomes[i].MoleculeAutoCloseRefusal = autos[j].Root, autos[j].Refusal - } - } - if claimFilter != nil && landed > 0 { - claim, claimTables, err := ExecuteClaimNext(ctx, tx, request.Actor, policy.FilterClaim(*claimFilter)) + claim, claimTables, err := ExecuteClaimNext(ctx, tx, request.Actor, *claimFilter) if err != nil { return publicops.CloseBatchResult{}, nil, err } diff --git a/internal/storage/issueops/close_guard.go b/internal/storage/issueops/close_guard.go deleted file mode 100644 index 9c715d89f..000000000 --- a/internal/storage/issueops/close_guard.go +++ /dev/null @@ -1,112 +0,0 @@ -package issueops - -import ( - "context" - "database/sql" - "fmt" - - "github.com/steveyegge/beads/internal/types" - publicops "github.com/steveyegge/beads/issueops" -) - -// closeGuardRow is the slice of the close target the close guards read. -type closeGuardRow struct { - isTemplate bool - pinned bool - status types.Status - assignee string -} - -// enforceCloseGuardsInTx applies the close guards — template read-only, the -// pin, and the assignee authority fence (be-035) — to an open target whose -// dependency routing the caller already read from this transaction. -// -// They are the guards `bd close` used to run as a pre-read in cmd/bd, which -// left every other caller of the role (bd serve, BatchCloser, BatchApplier -// close items, a library embedder) closing what bd refused. Living here, they -// read the row the close is about to write, inside the same transaction, so -// there is no read-then-write window either. -// -// Order is the historical one: template first (no bypass), then the pin, then -// the assignee — so a request that trips more than one reads the refusal bd -// always printed for it. force bypasses the pin and the assignee and nothing -// else. -// -// The caller runs this only for a target that is not already literally closed: -// a re-close has no state change to guard, and it stays the idempotent no-op it -// has always been (ga-ktn9pe.4.8) — a forced close of a pinned bead leaves the -// pin set, and the plain retry must not then refuse. -func enforceCloseGuardsInTx(ctx context.Context, tx DBTX, id, targetColumn, actor string, force bool) error { - row, err := readCloseGuardRowInTx(ctx, tx, id, targetColumn) - if err != nil { - return err - } - return CheckClosable(id, &types.Issue{ - IsTemplate: row.isTemplate, - Pinned: row.pinned, - Status: row.status, - Assignee: row.assignee, - }, actor, force) -} - -// CheckClosable is the close guards' one decision over an issue's pre-image: -// template read-only (*TemplateReadOnlyError, no bypass), then — unless force — -// the pin (*PinnedError, either spelling) and the assignee authority fence -// (*CloseNotAssigneeError; an unassigned issue is closable by anyone). A nil -// issue passes. It is pure: enforceCloseGuardsInTx applies it to the row the -// close is about to write, inside the close transaction, which is the -// authority; `bd close` also applies it to its resolved pre-image ahead of its -// own gate-satisfaction check, so a refusal that trips both reads the guard's -// sentence, as it always has. -// -// The caller decides whether to guard at all: an already-closed target is an -// idempotent re-close with no state change to guard (ga-ktn9pe.4.8). -func CheckClosable(id string, issue *types.Issue, actor string, force bool) error { - if issue == nil { - return nil - } - if issue.IsTemplate { - return &publicops.TemplateReadOnlyError{IssueID: id} - } - if force { - return nil - } - // Both pin spellings are load-bearing (ga-z3vht): Gas Town pins by - // status, Gas City by the column. See validation.NotPinned. - if issue.Pinned || issue.Status == types.StatusPinned { - return &publicops.PinnedError{IssueID: id} - } - if issue.Assignee != "" && !actorMatches(issue.Assignee, actor) { - return &publicops.CloseNotAssigneeError{IssueID: id, Assignee: issue.Assignee, Actor: actor} - } - return nil -} - -//nolint:gosec // G201: table is one of two hardcoded identifiers chosen from targetColumn. -func readCloseGuardRowInTx(ctx context.Context, tx DBTX, id, targetColumn string) (closeGuardRow, error) { - table := "issues" - switch targetColumn { - case "depends_on_issue_id": - case "depends_on_wisp_id": - table = "wisps" - default: - return closeGuardRow{}, fmt.Errorf("close guard: unsupported target column %q", targetColumn) - } - var ( - row closeGuardRow - template sql.NullBool - pinned sql.NullBool - status string - assignee sql.NullString - ) - if err := tx.QueryRowContext(ctx, - "SELECT is_template, pinned, status, assignee FROM "+table+" WHERE id = ?", id, - ).Scan(&template, &pinned, &status, &assignee); err != nil { - return closeGuardRow{}, fmt.Errorf("read close guard state for %s from %s: %w", id, table, err) - } - row.isTemplate = template.Valid && template.Bool - row.pinned = pinned.Valid && pinned.Bool - row.status = types.Status(status) - row.assignee = assignee.String - return row, nil -} diff --git a/internal/storage/issueops/close_guard_test.go b/internal/storage/issueops/close_guard_test.go deleted file mode 100644 index 83da8f86f..000000000 --- a/internal/storage/issueops/close_guard_test.go +++ /dev/null @@ -1,77 +0,0 @@ -package issueops - -import ( - "errors" - "fmt" - "testing" - - "github.com/steveyegge/beads/internal/types" - publicops "github.com/steveyegge/beads/issueops" -) - -// TestCheckClosable pins the close guards' one decision: template first (no -// bypass), then — unless force — the pin (both spellings) and the assignee -// fence (separator-insensitive; unassigned is anyone's). -func TestCheckClosable(t *testing.T) { - cases := []struct { - name string - issue *types.Issue - actor string - force bool - want error - }{ - {"nil", nil, "alice", false, nil}, - {"plain", &types.Issue{Status: types.StatusOpen}, "alice", false, nil}, - {"template", &types.Issue{IsTemplate: true}, "alice", false, publicops.ErrTemplateReadOnly}, - {"forced template", &types.Issue{IsTemplate: true}, "alice", true, publicops.ErrTemplateReadOnly}, - {"template outranks pin", &types.Issue{IsTemplate: true, Pinned: true}, "alice", false, publicops.ErrTemplateReadOnly}, - {"pinned column", &types.Issue{Pinned: true}, "alice", false, publicops.ErrPinned}, - {"pinned status", &types.Issue{Status: types.StatusPinned}, "alice", false, publicops.ErrPinned}, - {"forced pin", &types.Issue{Pinned: true}, "alice", true, nil}, - {"pin outranks holder", &types.Issue{Pinned: true, Assignee: "bob"}, "alice", false, publicops.ErrPinned}, - {"held", &types.Issue{Assignee: "bob"}, "alice", false, publicops.ErrNotOwner}, - {"forced held", &types.Issue{Assignee: "bob"}, "alice", true, nil}, - {"own", &types.Issue{Assignee: "alice"}, "alice", false, nil}, - {"unassigned", &types.Issue{}, "alice", false, nil}, - } - for _, tc := range cases { - t.Run(tc.name, func(t *testing.T) { - err := CheckClosable("bd-1", tc.issue, tc.actor, tc.force) - if tc.want == nil { - if err != nil { - t.Fatalf("CheckClosable = %v, want nil", err) - } - return - } - if !errors.Is(err, tc.want) { - t.Fatalf("CheckClosable = %v, want %v", err, tc.want) - } - }) - } - var held *publicops.CloseNotAssigneeError - if err := CheckClosable("bd-1", &types.Issue{Assignee: "bob"}, "alice", false); !errors.As(err, &held) || - held.IssueID != "bd-1" || held.Assignee != "bob" || held.Actor != "alice" { - t.Fatalf("held refusal = %#v", err) - } -} - -// TestRefusedMoleculeAutoCloseReportsTheRefusalsOwnSentence pins -// RefusedMoleculeAutoClose: whatever the layers between a root's refused close -// and the auto-close wrapped around it (the unit of work's use case and -// repository), Refusal is the sentence the refusing guard or policy wrote. -func TestRefusedMoleculeAutoCloseReportsTheRefusalsOwnSentence(t *testing.T) { - for _, refusal := range []error{ - publicops.NewCloseBlockedError("bd-1", []string{"bd-2"}), - &publicops.CloseOpenChildrenError{IssueID: "bd-1", OpenChildren: 2}, - &publicops.TemplateReadOnlyError{IssueID: "bd-1"}, - &publicops.PinnedError{IssueID: "bd-1"}, - &publicops.CloseNotAssigneeError{IssueID: "bd-1", Assignee: "bob", Actor: "alice"}, - } { - wrapped := fmt.Errorf("close bd-1: %w", fmt.Errorf("db: IssueSQLRepository.CloseChecked bd-1: %w", refusal)) - for _, err := range []error{refusal, wrapped} { - if got := RefusedMoleculeAutoClose(err).Refusal; got != refusal.Error() { - t.Errorf("RefusedMoleculeAutoClose(%q).Refusal = %q, want %q", err, got, refusal.Error()) - } - } - } -} diff --git a/internal/storage/issueops/close_test.go b/internal/storage/issueops/close_test.go index 3b57d0995..538d07be8 100644 --- a/internal/storage/issueops/close_test.go +++ b/internal/storage/issueops/close_test.go @@ -2,12 +2,9 @@ package issueops import ( "context" - "errors" "testing" "github.com/DATA-DOG/go-sqlmock" - "github.com/steveyegge/beads/internal/storage" - publicops "github.com/steveyegge/beads/issueops" ) // TestCountOpenChildrenInTxUsesTargetedOpenEdges keeps the close guard from @@ -46,65 +43,3 @@ func TestCountOpenChildrenInTxUsesTargetedOpenEdges(t *testing.T) { t.Fatalf("unmet targeted-count SQL expectations: %v", err) } } - -// TestExecuteCloseBatchWithPolicyDefersToTheTargetRead pins the external -// policy's fallback read. A snapshot names only ids, so it refuses a live -// target alone: a missing target keeps the backend's not-found, and a status -// read that fails reports that failure instead of a policy refusal. -func TestExecuteCloseBatchWithPolicyDefersToTheTargetRead(t *testing.T) { - t.Parallel() - - const id = "close-policy-target" - readErr := errors.New("status read failed") - statusQuery := func(table string) string { return `SELECT status FROM ` + table + ` WHERE id = \?` } - for _, tc := range []struct { - name string - expect func(sqlmock.Sqlmock) - want error - }{ - { - // Neither table has the row, for the policy's read or for - // ExecuteClose's own, so the backend's not-found stands. - name: "missing", - expect: func(mock sqlmock.Sqlmock) { - for range 2 { - for _, table := range []string{"issues", "wisps"} { - mock.ExpectQuery(statusQuery(table)). - WithArgs(id). - WillReturnRows(sqlmock.NewRows([]string{"status"})) - } - } - }, - want: storage.ErrNotFound, - }, - { - name: "unreadable", - expect: func(mock sqlmock.Sqlmock) { - mock.ExpectQuery(statusQuery("issues")). - WithArgs(id). - WillReturnError(readErr) - }, - want: readErr, - }, - } { - t.Run(tc.name, func(t *testing.T) { - t.Parallel() - - _, mock, tx := beginMockTx(t) - tc.expect(mock) - result, _, err := ExecuteCloseBatchWithPolicy(context.Background(), tx, publicops.CloseBatchRequest{ - Actor: "tester", - Items: []publicops.BatchCloseItem{{IssueID: id}}, - }, nil, storage.NewBatchClosePolicy(map[string][]string{id: {"external:p:c"}})) - if err != nil { - t.Fatalf("ExecuteCloseBatchWithPolicy: %v", err) - } - if got := result.Outcomes[0].Err; !errors.Is(got, tc.want) || errors.Is(got, storage.ErrCloseBlocked) { - t.Fatalf("outcome error = %v, want %v rather than a policy refusal", got, tc.want) - } - if err := mock.ExpectationsWereMet(); err != nil { - t.Fatalf("unmet target-read SQL expectations: %v", err) - } - }) - } -} diff --git a/internal/storage/issueops/config_helpers.go b/internal/storage/issueops/config_helpers.go index bcef3bc4a..f17d858d4 100644 --- a/internal/storage/issueops/config_helpers.go +++ b/internal/storage/issueops/config_helpers.go @@ -459,7 +459,6 @@ func ParseTypesConfigValue(value string) []string { // path must keep the projection in step with the string value or stale // table rows win forever. Returns the name of the synced table ("" when // the key has no projection) so transactional callers can mark it dirty. -// ConfigProjectionTables must list every table it can return. func SyncConfigTables(ctx context.Context, tx DBTX, key, value string) (string, error) { switch key { case "status.custom": @@ -476,14 +475,6 @@ func SyncConfigTables(ctx context.Context, tx DBTX, key, value string) (string, return "", nil } -// ConfigProjectionTables lists every table SyncConfigTables can write, for a -// caller that commits config writes without knowing which keys they touched: -// a config row committed without its projection is the split the projection -// exists to prevent. -func ConfigProjectionTables() []string { - return []string{"custom_statuses", "custom_types"} -} - // ResolveInfraTypesInTx reads infrastructure types from the database, // falling back to config.yaml then to hardcoded defaults. // Returns a map[string]bool for O(1) lookups. diff --git a/internal/storage/issueops/config_metadata.go b/internal/storage/issueops/config_metadata.go index 66fc5c569..0942ed9ee 100644 --- a/internal/storage/issueops/config_metadata.go +++ b/internal/storage/issueops/config_metadata.go @@ -53,43 +53,6 @@ func GetAllConfigInTx(ctx context.Context, tx DBTX) (map[string]string, error) { return result, rows.Err() } -// ConfigPrefixLikeQuery is the one SELECT for prefix-scoped config reads. -// Its explicit ESCAPE character pairs with ConfigPrefixPattern's escaping -// ('!' rather than backslash: the engine does not treat backslash as the -// default LIKE escape, and '!' avoids literal-vs-placeholder ambiguity); -// keeping query and pattern builder side by side is what stops them drifting. -const ConfigPrefixLikeQuery = "SELECT `key`, value FROM config WHERE `key` LIKE ? ESCAPE '!'" - -// ConfigPrefixPattern builds the LIKE pattern that matches config keys -// starting with the given literal prefix, escaping LIKE metacharacters -// (%, _) and the escape character itself so the prefix always matches -// literally. Use only with ConfigPrefixLikeQuery. -func ConfigPrefixPattern(prefix string) string { - return strings.NewReplacer(`!`, `!!`, `%`, `!%`, `_`, `!_`).Replace(prefix) + "%" -} - -// GetConfigByPrefixInTx retrieves the configuration pairs whose key starts -// with prefix, within an existing transaction. The filter runs in SQL so a -// scoped read of a large config/kv store (e.g. one mailbox out of thousands -// of kv mail envelopes) does not serialize the whole table. -func GetConfigByPrefixInTx(ctx context.Context, tx DBTX, prefix string) (map[string]string, error) { - rows, err := tx.QueryContext(ctx, ConfigPrefixLikeQuery, ConfigPrefixPattern(prefix)) - if err != nil { - return nil, fmt.Errorf("get config by prefix %s: %w", prefix, err) - } - defer rows.Close() - - result := make(map[string]string) - for rows.Next() { - var k, v string - if err := rows.Scan(&k, &v); err != nil { - return nil, fmt.Errorf("get config by prefix %s: scan: %w", prefix, err) - } - result[k] = v - } - return result, rows.Err() -} - // SetMetadataInTx sets a metadata value within an existing transaction. func SetMetadataInTx(ctx context.Context, tx DBTX, key, value string) error { _, err := tx.ExecContext(ctx, "REPLACE INTO metadata (`key`, value) VALUES (?, ?)", key, value) diff --git a/internal/storage/issueops/config_prefix_test.go b/internal/storage/issueops/config_prefix_test.go deleted file mode 100644 index ea0bb6d69..000000000 --- a/internal/storage/issueops/config_prefix_test.go +++ /dev/null @@ -1,20 +0,0 @@ -package issueops - -import "testing" - -// ConfigPrefixPattern must escape every LIKE metacharacter so a prefix always -// matches literally: an unescaped `_` in "kv.a_." would also match "kv.axb". -func TestConfigPrefixPattern(t *testing.T) { - cases := []struct{ in, want string }{ - {"kv.mail.dog.", "kv.mail.dog.%"}, - {"kv.a_.", "kv.a!_.%"}, - {"kv.a%.", "kv.a!%.%"}, - {"kv.a!.", "kv.a!!.%"}, - {"", "%"}, - } - for _, c := range cases { - if got := ConfigPrefixPattern(c.in); got != c.want { - t.Errorf("ConfigPrefixPattern(%q) = %q, want %q", c.in, got, c.want) - } - } -} diff --git a/internal/storage/issueops/counts_missing_table_test.go b/internal/storage/issueops/counts_missing_table_test.go index fdec3c821..8841e486d 100644 --- a/internal/storage/issueops/counts_missing_table_test.go +++ b/internal/storage/issueops/counts_missing_table_test.go @@ -3,7 +3,6 @@ package issueops import ( "context" "database/sql" - "database/sql/driver" "errors" "fmt" "testing" @@ -41,21 +40,6 @@ type countsEntryPoint struct { wantTolerated string } -// readyProbeSQL matches the one-statement ready probe (probeReadyWorkInTx). -const readyProbeSQL = `^SELECT EXISTS \(SELECT 1 FROM wisps\), EXISTS \(SELECT 1 FROM wisp_dependencies\)` - -// readyProbeRows answers the ready probe: wisps present, wisp_dependencies -// present, then any further flags the caller's probe asks for. -func readyProbeRows(flags ...bool) *sqlmock.Rows { - cols := make([]string, len(flags)) - vals := make([]driver.Value, len(flags)) - for i, f := range flags { - cols[i] = fmt.Sprintf("f%d", i) - vals[i] = f - } - return sqlmock.NewRows(cols).AddRow(vals...) -} - func scalarCountRows(n int) *sqlmock.Rows { return sqlmock.NewRows([]string{"count"}).AddRow(n) } @@ -176,8 +160,11 @@ var countsEntryPoints = []countsEntryPoint{ name: "GetReadyWorkWithCountsInTx", missing: countsMegaQueryTables, prime: func(mock sqlmock.Sqlmock, wispErr error) { - mock.ExpectQuery(readyProbeSQL).WillReturnRows(readyProbeRows(true, true)) + mock.ExpectQuery(`SELECT 1 FROM wisp_dependencies LIMIT 1`). + WillReturnRows(sqlmock.NewRows([]string{"1"}).AddRow(1)) mock.ExpectQuery(`(?s)FROM issues i`).WillReturnRows(emptyCountsRows()) + mock.ExpectQuery(`SELECT 1 FROM wisps LIMIT 1`). + WillReturnRows(sqlmock.NewRows([]string{"1"}).AddRow(1)) mock.ExpectQuery(`(?s)FROM wisps i`).WillReturnError(wispErr) }, run: func(ctx context.Context, tx *sql.Tx) (string, error) { @@ -190,12 +177,12 @@ var countsEntryPoints = []countsEntryPoint{ name: "CountReadyWorkInTx", missing: []string{"wisp_labels"}, prime: func(mock sqlmock.Sqlmock, wispErr error) { - // Both families are counted in one statement; when its wisp half - // names a table the database may lack, the issues count is taken - // on its own. - mock.ExpectQuery(readyProbeSQL).WillReturnRows(readyProbeRows(true, true, false)) - mock.ExpectQuery(`SELECT \(SELECT COUNT\(\*\) FROM issues .*\), \(SELECT COUNT\(\*\) FROM wisps`).WillReturnError(wispErr) + mock.ExpectQuery(`SELECT 1 FROM wisp_dependencies LIMIT 1`). + WillReturnRows(sqlmock.NewRows([]string{"1"}).AddRow(1)) mock.ExpectQuery(`SELECT COUNT\(\*\) FROM issues`).WillReturnRows(scalarCountRows(1)) + mock.ExpectQuery(`SELECT 1 FROM wisps LIMIT 1`). + WillReturnRows(sqlmock.NewRows([]string{"1"}).AddRow(1)) + mock.ExpectQuery(`SELECT COUNT\(\*\) FROM wisps`).WillReturnError(wispErr) }, run: func(ctx context.Context, tx *sql.Tx) (string, error) { n, err := CountReadyWorkInTx(ctx, tx, readyFilter()) @@ -336,63 +323,3 @@ func TestCountsHealthyPlaneStillMerges(t *testing.T) { t.Fatalf("unmet sql expectations: %v", err) } } - -// TestReadyProbeFallsBackOnPreWispDatabase pins the one-statement ready probe's -// escape hatch. A pre-migration database has no wisps table, so the combined -// probe (which names it) fails as a whole; the ready read must then fall back -// to the per-table probes — whose missing-table tolerance is the documented -// contract — and answer issues-only, for the page, its in-band total and the -// separate count alike. -func TestReadyProbeFallsBackOnPreWispDatabase(t *testing.T) { - primeFallback := func(mock sqlmock.Sqlmock) { - mock.ExpectQuery(readyProbeSQL).WillReturnError(tableNotFound("wisps")) - mock.ExpectQuery(`SELECT 1 FROM wisp_dependencies LIMIT 1`).WillReturnError(tableNotFound("wisp_dependencies")) - mock.ExpectQuery(`SELECT 1 FROM wisps LIMIT 1`).WillReturnError(tableNotFound("wisps")) - } - - t.Run("page_and_total", func(t *testing.T) { - _, mock, tx := beginMockTx(t) - primeFallback(mock) - mock.ExpectQuery(`^SELECT id, COUNT\(\*\) OVER \(\) FROM issues`). - WillReturnRows(sqlmock.NewRows([]string{"id", "total"})) - filter := readyFilter() - filter.Limit = 1 - out, total, err := GetReadyWorkWithCountsAndTotalInTx(context.Background(), tx, filter) - if err != nil { - t.Fatalf("GetReadyWorkWithCountsAndTotalInTx on a pre-wisp database: %v", err) - } - if len(out) != 0 || total != 0 { - t.Fatalf("got %d rows, total %d; want an empty issues-only answer", len(out), total) - } - if err := mock.ExpectationsWereMet(); err != nil { - t.Fatalf("unmet sql expectations: %v", err) - } - }) - - t.Run("count", func(t *testing.T) { - _, mock, tx := beginMockTx(t) - primeFallback(mock) - mock.ExpectQuery(`^SELECT COUNT\(\*\) FROM issues WHERE`).WillReturnRows(scalarCountRows(4)) - n, err := CountReadyWorkInTx(context.Background(), tx, readyFilter()) - if err != nil { - t.Fatalf("CountReadyWorkInTx on a pre-wisp database: %v", err) - } - if n != 4 { - t.Fatalf("CountReadyWorkInTx = %d, want the issues-only 4", n) - } - if err := mock.ExpectationsWereMet(); err != nil { - t.Fatalf("unmet sql expectations: %v", err) - } - }) - - // The fallback is for a MISSING table only; any other probe failure is - // the caller's error, never a quietly issues-only answer. - t.Run("unrelated_error_propagates", func(t *testing.T) { - _, mock, tx := beginMockTx(t) - boom := errors.New("connection refused") - mock.ExpectQuery(readyProbeSQL).WillReturnError(boom) - if _, _, err := GetReadyWorkWithCountsAndTotalInTx(context.Background(), tx, readyFilter()); !errors.Is(err, boom) { - t.Fatalf("probe failure not propagated: %v", err) - } - }) -} diff --git a/internal/storage/issueops/create.go b/internal/storage/issueops/create.go index 97104ad32..b55d04596 100644 --- a/internal/storage/issueops/create.go +++ b/internal/storage/issueops/create.go @@ -12,7 +12,6 @@ import ( "github.com/go-sql-driver/mysql" "github.com/steveyegge/beads/internal/storage" "github.com/steveyegge/beads/internal/storage/depid" - "github.com/steveyegge/beads/internal/storage/domain" "github.com/steveyegge/beads/internal/types" ) @@ -32,21 +31,6 @@ type BatchContext struct { // issue for a result the caller discards. Singular creates leave this // false so they keep reconciling immediately, per-issue. SkipChildCounterReconcile bool - // DeferVersionMint tells CreateIssueInTxWithResult NOT to mint the - // issue's version row itself but to report the deferral on its result. - // CreateIssuesInTxWithContext sets this because it persists the batch's - // creation-time dependency edges AFTER the per-issue loop, and the first - // version must carry that outgoing edge set: it mints once per accepted - // issue after the edges (and the blocked-state recompute) have landed. - // Singular creates leave this false and mint in place — they never run - // the dependency pass. - DeferVersionMint bool - // cache, when set, answers the per-issue presence/label lookups from one - // up-front batch read and buffers the batch's audit events for one - // bulk write (see createBatchCache). CreateIssuesInTxWithContext sets it - // on its private copy for multi-issue batches; it is never set on a - // caller's context. - cache *createBatchCache } // NewBatchContext reads config from the database and returns a BatchContext. @@ -92,22 +76,6 @@ type CreateIssueResult struct { // up to the entry point so their journal rows land AFTER the create's. A // consumer must never see a comment for a bead it has not been told about. persistedComments []EventComment - // versionDeferred reports that this create reached the version seam but - // left the mint to the batch entry point (BatchContext.DeferVersionMint). - // Set only on the path that would otherwise have minted, so the batch - // mints for exactly the issues a singular create would have. - versionDeferred bool - // isNew is whether this create actually inserted a new row, as opposed - // to resolving to an already-existing one. On the per-row path - // (CreateIssueInTxWithResult) it is InsertIssueIfNew's result; a row - // written by deferredCreates' multi-row INSERT reaches - // finishCreateIssueInTx directly and is always new, since only an id - // absent from both planes is deferrable. The participation_generation - // write-fence (design §16.2b) needs this to choose - // RecordVersionForCreateInTx over RecordVersionInTx; a batch that defers - // its mint (BatchContext.DeferVersionMint) carries it here since the - // per-issue isNew local is out of scope by the time the batch mints. - isNew bool } type persistedDependency struct { @@ -139,12 +107,28 @@ func mergeChangedTables(dst map[string]bool, src map[string]bool) map[string]boo func CreateIssueInTxWithResult(ctx context.Context, tx DBTX, bc *BatchContext, issue *types.Issue, actor string) (CreateIssueResult, error) { var result CreateIssueResult - issueTable, eventTable, skip, err := prepareCreateIssueInTx(ctx, tx, bc, issue, actor) - if err != nil || skip { + if err := PrepareIssueForInsert(issue, bc.CustomStatuses, bc.CustomTypes); err != nil { return result, err } - isNew, staleRejected, err := insertIssueIfNewCached(ctx, tx, issueTable, issue, bc.Opts, bc.cache) + issueTable, eventTable := TableRouting(issue) + + if err := assignCreateIssueIDInTx(ctx, tx, bc, issue, actor); err != nil { + return result, err + } + if bc.Opts.CreateOnly { + if err := EnsureIssueIDAvailableInTx(ctx, tx, issue.ID); err != nil { + return result, err + } + } + + if skip, err := checkCrossTableIDCollision(ctx, tx, issue.ID, issueTable, bc.Opts); err != nil { + return result, err + } else if skip { + return result, nil + } + + isNew, staleRejected, err := InsertIssueIfNew(ctx, tx, issueTable, issue, bc.Opts) if err != nil { return result, err } @@ -158,43 +142,7 @@ func CreateIssueInTxWithResult(ctx context.Context, tx DBTX, bc *BatchContext, i } return result, nil } - return finishCreateIssueInTx(ctx, tx, bc, issue, actor, issueTable, eventTable, isNew) -} - -// prepareCreateIssueInTx is the part of a create before its row write: -// normalization and validation, table routing, id assignment, the create-only -// guard and the cross-plane collision check. skip reports a collision the -// options tolerate (ConflictSkip): nothing is written for the issue. -func prepareCreateIssueInTx(ctx context.Context, tx DBTX, bc *BatchContext, issue *types.Issue, actor string) (issueTable, eventTable string, skip bool, err error) { - if err := PrepareIssueForInsert(issue, bc.CustomStatuses, bc.CustomTypes); err != nil { - return "", "", false, err - } - - issueTable, eventTable = TableRouting(issue) - - if err := assignCreateIssueIDInTx(ctx, tx, bc, issue, actor); err != nil { - return "", "", false, err - } - if bc.Opts.CreateOnly { - if err := EnsureIssueIDAvailableInTx(ctx, tx, issue.ID); err != nil { - return "", "", false, err - } - } - - skip, err = checkCrossTableIDCollisionCached(ctx, tx, issue.ID, issueTable, bc.Opts, bc.cache) - if err != nil { - return "", "", false, err - } - return issueTable, eventTable, skip, nil -} - -// finishCreateIssueInTx is the part of a create after its row is written: -// lease reconciliation, the created event, labels, comments, the child -// counter, the journal entry and the version seam. -func finishCreateIssueInTx(ctx context.Context, tx DBTX, bc *BatchContext, issue *types.Issue, actor, issueTable, eventTable string, isNew bool) (CreateIssueResult, error) { - var result CreateIssueResult result.markChanged(issueTable) - result.isNew = isNew // Reconcile the ephemeral lease row with the accepted issue state // (restore an imported lease / drop an orphaned one — see @@ -207,15 +155,13 @@ func finishCreateIssueInTx(ctx context.Context, tx DBTX, bc *BatchContext, issue } if isNew { - if bc.cache != nil { - bc.cache.bufferEvent(eventTable, createdAuxEvent(issue.ID, actor)) - } else if err := RecordEventInTable(ctx, tx, eventTable, issue.ID, types.EventCreated, actor, ""); err != nil { + if err := RecordEventInTable(ctx, tx, eventTable, issue.ID, types.EventCreated, actor, ""); err != nil { return result, fmt.Errorf("failed to record event for %s: %w", issue.ID, err) } result.markChanged(eventTable) } - labelResult, err := persistLabelsCached(ctx, tx, issue, actor, eventTable, bc.cache) + labelResult, err := PersistLabels(ctx, tx, issue, actor, eventTable) if err != nil { return result, err } @@ -247,19 +193,6 @@ func finishCreateIssueInTx(ctx context.Context, tx DBTX, bc *BatchContext, issue if err := RecordEventInTx(ctx, tx, EventCreate, issue.ID, actor); err != nil { return result, err } - // The version row is the create's LAST durable-state write. A batch - // create persists this issue's outgoing edges after this function returns, - // so it defers the mint to its own end (DeferVersionMint); a singular - // create has nothing after this point and mints here. - if bc.DeferVersionMint { - result.versionDeferred = true - } else if isNew { - if err := RecordVersionForCreateInTx(ctx, tx, issue.ID, actor); err != nil { - return result, err - } - } else if err := RecordVersionInTx(ctx, tx, issue.ID, actor); err != nil { - return result, err - } // Creation-time comments (import/interchange carries them inline) are // replayable content the create snapshot does NOT contain — issue hydration // joins labels but not comments — so each inserted comment gets its own op, @@ -336,17 +269,6 @@ func CreateIssuesInTxWithResult(ctx context.Context, tx DBTX, issues []*types.Is return CreateIssuesInTxWithContext(ctx, tx, bc, issues, actor) } -// deferredVersion carries a create's issue id and whether it was a genuinely -// new row (CreateIssueResult.isNew) from the per-issue loop in -// CreateIssuesInTxWithContext to that function's own final mint pass. The -// loop fills it from both of its write paths: a row CreateIssueInTxWithResult -// wrote on its own, and a row deferredCreates wrote in a multi-row INSERT and -// finished through finishCreateIssueInTx, which is always new. -type deferredVersion struct { - id string - isNew bool -} - // CreateIssuesInTxWithContext is CreateIssuesInTxWithResult with a // caller-supplied BatchContext. Callers that split config reads from row // writes across SQL sessions (doltTransaction's wisp tier) build the context @@ -354,22 +276,11 @@ type deferredVersion struct { // The caller's bc is not modified, so one context can serve several calls. func CreateIssuesInTxWithContext(ctx context.Context, tx DBTX, bc *BatchContext, issues []*types.Issue, actor string) (CreateIssuesResult, error) { opts := bc.Opts - // The plane rule is a REFUSAL, never a filter. A strict batch (BatchCreator's - // contract) is refused whole before any row is written. A tolerant batch - // (SkipDependencyValidationErrors — the import mode) keeps every in-batch - // regular<->wisp edge: every row of BOTH planes is written on this one tx - // below before PersistDependenciesWithOptionsResult runs, and that pass - // resolves each target on its own plane, so the edge is writable — the - // old per-batch filter skip-reported it, and since a re-import upserts the - // rows unchanged the edge was never backfilled (wy-4276q8, wy-a648lq). A - // caller that writes the two planes on different SQL sessions - // (doltTransaction.CreateIssues) validates up front and passes one plane - // per call, so a mixed batch reaching here always shares one tx. - if !opts.SkipDependencyValidationErrors { - if err := validateCreateIssuesMixedBucketDependencies(issues); err != nil { - return CreateIssuesResult{}, err - } + filteredIssues, err := filterCreateIssuesMixedBucketDependencies(issues, opts) + if err != nil { + return CreateIssuesResult{}, err } + issues = filteredIssues // This function already runs a slice-wide ReconcileChildCounters below, // covering every accepted issue; skip the redundant per-issue reconcile. @@ -377,82 +288,21 @@ func CreateIssuesInTxWithContext(ctx context.Context, tx DBTX, bc *BatchContext, // reconcile behavior. batch := *bc batch.SkipChildCounterReconcile = true - // The per-issue create leaves the version mint to this function: the - // creation-time edges below land after the per-issue loop, and the first - // version of each issue must carry them (one version per issue at - // creation, minted last). - batch.DeferVersionMint = true - if len(issues) >= createBatchCacheMinIssues && !createFastPathsDisabled.Load() { - cache, err := newCreateBatchCache(ctx, tx, issues) - if err != nil { - return CreateIssuesResult{}, err - } - batch.cache = cache - } result := CreateIssuesResult{} accepted := issues[:0:0] - var toVersion []deferredVersion - record := func(issue *types.Issue, issueResult CreateIssueResult) { - result.merge(issueResult.ChangedTables) - if issueResult.versionDeferred { - toVersion = append(toVersion, deferredVersion{id: issue.ID, isNew: issueResult.isNew}) - } - if issueResult.StaleRejected { - return // stale snapshot: keep its deps out of the batch too - } - accepted = append(accepted, issue) - } - // Brand-new rows are written in multi-row INSERTs (see deferredCreates): - // a run of them is prepared in order, written together, then finished in - // order, before the next issue that is not one of them is touched. - var deferred deferredCreates - flush := func() error { - results, err := deferred.flush(ctx, tx, &batch, actor) - if err != nil { - return err - } - for i, issueResult := range results { - record(deferred.created[i].issue, issueResult) - } - deferred.created = deferred.created[:0] - return nil - } for _, issue := range issues { - if batch.cache.deferrable(issue, opts) { - // deferrable requires the id absent from both planes, so the - // cross-plane collision check cannot ask for a skip here. - issueTable, eventTable, _, err := prepareCreateIssueInTx(ctx, tx, &batch, issue, actor) - if err != nil { - // The issues before this one are written (and can fail) first, - // as they would have been one at a time. - if flushErr := flush(); flushErr != nil { - return CreateIssuesResult{}, flushErr - } - return CreateIssuesResult{}, err - } - batch.cache.markInserted(issueTable, issue.ID) - deferred.created = append(deferred.created, deferredCreate{issue: issue, issueTable: issueTable, eventTable: eventTable}) - continue - } - if err := flush(); err != nil { - return CreateIssuesResult{}, err - } issueResult, err := CreateIssueInTxWithResult(ctx, tx, &batch, issue, actor) if err != nil { return CreateIssuesResult{}, err } - record(issue, issueResult) - } - if err := flush(); err != nil { - return CreateIssuesResult{}, err + result.merge(issueResult.ChangedTables) + if issueResult.StaleRejected { + continue // stale snapshot: keep its deps out of the batch too + } + accepted = append(accepted, issue) } issues = accepted - // The buffered created/label_added events land before anything else in - // the batch could read the events tables (nothing in it does). - if err := batch.cache.flushEvents(ctx, tx); err != nil { - return CreateIssuesResult{}, err - } depResult, err := PersistDependenciesWithOptionsResult(ctx, tx, issues, actor, opts) if err != nil { @@ -472,9 +322,7 @@ func CreateIssuesInTxWithContext(ctx context.Context, tx DBTX, bc *BatchContext, if err != nil { return CreateIssuesResult{}, err } - // The ids are this batch's rows, most of them fresh and edgeless: let the - // recompute skip the union statements for those (planRecomputeInTx). - recomputed, err := recomputeIsBlockedInTxWithResult(ctx, tx, issueIDs, wispIDs, true) + recomputed, err := RecomputeIsBlockedInTxWithResult(ctx, tx, issueIDs, wispIDs) if err != nil { return CreateIssuesResult{}, err } @@ -484,22 +332,6 @@ func CreateIssuesInTxWithContext(ctx context.Context, tx DBTX, bc *BatchContext, if recomputed.WispRowsChanged { result.markChanged("wisps") } - // Mint each created issue's first version LAST — after its creation-time - // edges and the blocked-state recompute — so durable_state carries the - // outgoing edge set. PersistDependenciesWithOptionsResult writes the edge - // rows directly and mints nothing itself, so this is the one version per - // issue at creation. Wisps are excluded by the seam. - for _, dv := range toVersion { - if dv.isNew { - if err := RecordVersionForCreateInTx(ctx, tx, dv.id, actor); err != nil { - return CreateIssuesResult{}, err - } - continue - } - if err := RecordVersionInTx(ctx, tx, dv.id, actor); err != nil { - return CreateIssuesResult{}, err - } - } return result, nil } @@ -545,20 +377,27 @@ func stageableChangedTables(changed map[string]bool) map[string]bool { return dirty } -// ValidateCreateIssuesMixedBucketDependencies refuses same-batch dependency -// edges between regular issues and wisps. It is the plane rule of the STRICT -// batch (BatchCreator's contract, CrossPlaneBatchEdgeError): a caller whose -// regular and wisp rows are written on different SQL sessions -// (doltTransaction.CreateIssues) cannot create both ends of such an edge -// atomically, so the batch is refused whole before any row is written. It is -// deliberately NOT applied to a tolerant batch (SkipDependencyValidationErrors): -// on one tx the dependency pass runs after every row of both planes exists and -// writes the edge (see CreateIssuesInTxWithContext). +// ValidateCreateIssuesMixedBucketDependencies rejects same-batch dependency +// edges between regular issues and wisps. Dependencies are stored in separate +// backing tables per bucket, so a batch cannot create both ends atomically when +// the edge crosses buckets. func ValidateCreateIssuesMixedBucketDependencies(issues []*types.Issue) error { - return validateCreateIssuesMixedBucketDependencies(issues) + _, err := filterCreateIssuesMixedBucketDependencies(issues, storage.BatchCreateOptions{}) + return err } -func validateCreateIssuesMixedBucketDependencies(issues []*types.Issue) error { +// FilterCreateIssuesMixedBucketDependencies applies the same cross-bucket +// dependency policy as CreateIssuesInTx, but over the full issue set. Callers +// that split one logical batch into bounded sub-batches (chunked import) must +// run this once up front: the per-batch filter inside the engine only sees one +// sub-batch, so it could no longer detect an edge whose endpoints land in +// different chunks. Filtered edges are reported via opts.OnSkippedDependency; +// issues whose dependency list changes are copied, never mutated. +func FilterCreateIssuesMixedBucketDependencies(issues []*types.Issue, opts storage.BatchCreateOptions) ([]*types.Issue, error) { + return filterCreateIssuesMixedBucketDependencies(issues, opts) +} + +func filterCreateIssuesMixedBucketDependencies(issues []*types.Issue, opts storage.BatchCreateOptions) ([]*types.Issue, error) { batchWispByID := make(map[string]bool, len(issues)) hasRegular := false hasWisp := false @@ -577,15 +416,21 @@ func validateCreateIssuesMixedBucketDependencies(issues []*types.Issue) error { } } if !hasRegular || !hasWisp { - return nil + return issues, nil } - for _, issue := range issues { + var filteredIssues []*types.Issue + for issueIndex, issue := range issues { if issue == nil { continue } - for _, dep := range issue.Dependencies { + var keptDeps []*types.Dependency + filteredDeps := false + for depIndex, dep := range issue.Dependencies { if dep == nil { + if filteredDeps { + keptDeps = append(keptDeps, dep) + } continue } sourceID := issue.ID @@ -598,15 +443,37 @@ func validateCreateIssuesMixedBucketDependencies(issues []*types.Issue) error { } targetIsWisp, targetInBatch := batchWispByID[dep.DependsOnID] if targetInBatch && sourceIsWisp != targetIsWisp { - // Through the shared constructor, so the two bodies raise - // one message AND one sentinel: the role promises this - // refusal is the caller's fault, and an untyped error left - // callers classifying it by prose. - return CrossPlaneBatchEdgeError(sourceID, dep.DependsOnID) + if !opts.SkipDependencyValidationErrors { + // Through the shared constructor, so the two bodies raise + // one message AND one sentinel: the role promises this + // refusal is the caller's fault, and an untyped error left + // callers classifying it by prose. + return nil, CrossPlaneBatchEdgeError(sourceID, dep.DependsOnID) + } + if !filteredDeps { + keptDeps = append([]*types.Dependency(nil), issue.Dependencies[:depIndex]...) + filteredDeps = true + } + recordSkippedDependencyEdge(opts, sourceID, dep.DependsOnID, "cross-bucket dependency between regular issue and wisp in the same batch") + continue + } + if filteredDeps { + keptDeps = append(keptDeps, dep) } } + if filteredDeps { + if filteredIssues == nil { + filteredIssues = append([]*types.Issue(nil), issues...) + } + issueCopy := *issue + issueCopy.Dependencies = keptDeps + filteredIssues[issueIndex] = &issueCopy + } } - return nil + if filteredIssues != nil { + return filteredIssues, nil + } + return issues, nil } func createBlockedRecomputeIDs(ctx context.Context, tx DBTX, issues []*types.Issue, dependencies []persistedDependency) ([]string, []string, error) { @@ -637,42 +504,16 @@ func createBlockedRecomputeIDs(ctx context.Context, tx DBTX, issues []*types.Iss isWisp := IsWisp(issue) add(issue.ID, isWisp) } - // The rows a created edge affects are AffectedByDepChange(ForWisp)InTx's: - // its source, plus the waiters on a parent-child edge's target, closed - // over parent-child descendants. That closure distributes over union, so - // the batch seeds every edge at once and expands once, rather than - // re-walking the descendants of each edge's source separately. - var depIssueSeed, depWispSeed, spawnerIDs []string - depIssueSeen, depWispSeen, spawnerSeen := map[string]bool{}, map[string]bool{}, map[string]bool{} for _, dependency := range dependencies { - switch dependency.depType { - case types.DepBlocks, types.DepConditionalBlocks, types.DepWaitsFor, types.DepParentChild: - default: - continue - } + var affectedIssues, affectedWisps []string + var err error if dependency.sourceWisp { - if !depWispSeen[dependency.source] { - depWispSeen[dependency.source] = true - depWispSeed = append(depWispSeed, dependency.source) - } - } else if !depIssueSeen[dependency.source] { - depIssueSeen[dependency.source] = true - depIssueSeed = append(depIssueSeed, dependency.source) + affectedIssues, affectedWisps, err = AffectedByDepChangeForWispInTx(ctx, tx, dependency.source, dependency.target, dependency.depType) + } else { + affectedIssues, affectedWisps, err = AffectedByDepChangeInTx(ctx, tx, dependency.source, dependency.target, dependency.depType) } - if dependency.depType == types.DepParentChild && dependency.target != "" && !spawnerSeen[dependency.target] { - spawnerSeen[dependency.target] = true - spawnerIDs = append(spawnerIDs, dependency.target) - } - } - if len(depIssueSeed) > 0 || len(depWispSeed) > 0 { - if len(spawnerIDs) > 0 { - if err := loadWaitersOnSpawnerIDsInTx(ctx, tx, spawnerIDs, &depIssueSeed, depIssueSeen, &depWispSeed, depWispSeen); err != nil { - return nil, nil, fmt.Errorf("affected by created dependencies: %w", err) - } - } - affectedIssues, affectedWisps, err := expandByParentChildDescendantsInTx(ctx, tx, depIssueSeed, depWispSeed, depIssueSeen, depWispSeen) if err != nil { - return nil, nil, fmt.Errorf("affected by created dependencies: %w", err) + return nil, nil, fmt.Errorf("affected by created dependency %s -> %s: %w", dependency.source, dependency.target, err) } for _, id := range affectedIssues { add(id, false) @@ -702,10 +543,6 @@ func PrepareIssueForInsert(issue *types.Issue, customStatuses, customTypes []str } else { issue.UpdatedAt = issue.UpdatedAt.UTC() } - // Optional timestamps (closed_at, started_at, due_at, …) may arrive from a - // JSONL import carrying a non-UTC offset; normalize them to UTC so the stored - // instant matches created_at/updated_at instead of keeping local wall-clock. - issue.NormalizeOptionalTimestampsToUTC() // Ensure closed issues have a closed_at timestamp. if issue.Status == types.StatusClosed && issue.ClosedAt == nil { @@ -788,12 +625,6 @@ func AllWisps(issues []*types.Issue) bool { // //nolint:gosec // G201: siblingTable is one of two hardcoded constants func checkCrossTableIDCollision(ctx context.Context, tx DBTX, id, issueTable string, opts storage.BatchCreateOptions) (skip bool, err error) { - return checkCrossTableIDCollisionCached(ctx, tx, id, issueTable, opts, nil) -} - -// checkCrossTableIDCollisionCached is checkCrossTableIDCollision reading the -// sibling plane's presence from cache when it covers id. -func checkCrossTableIDCollisionCached(ctx context.Context, tx DBTX, id, issueTable string, opts storage.BatchCreateOptions, cache *createBatchCache) (skip bool, err error) { if id == "" { return false, nil } @@ -801,8 +632,8 @@ func checkCrossTableIDCollisionCached(ctx context.Context, tx DBTX, id, issueTab if issueTable == "wisps" { siblingTable = "issues" } - siblingCount, err := cache.rowCount(ctx, tx, siblingTable, id) - if err != nil { + var siblingCount int + if err := tx.QueryRowContext(ctx, fmt.Sprintf(`SELECT COUNT(*) FROM %s WHERE id = ?`, siblingTable), id).Scan(&siblingCount); err != nil { return false, fmt.Errorf("failed to check cross-table ID collision for %s: %w", id, err) } if siblingCount == 0 { @@ -838,16 +669,9 @@ func checkCrossTableIDCollisionCached(ctx context.Context, tx DBTX, id, issueTab // //nolint:gosec // G201: table is a hardcoded constant func InsertIssueIfNew(ctx context.Context, tx DBTX, issueTable string, issue *types.Issue, opts storage.BatchCreateOptions) (isNew bool, staleRejected bool, err error) { - return insertIssueIfNewCached(ctx, tx, issueTable, issue, opts, nil) -} - -// insertIssueIfNewCached is InsertIssueIfNew reading the row's presence from -// cache when it covers the id, and recording the write in cache. -func insertIssueIfNewCached(ctx context.Context, tx DBTX, issueTable string, issue *types.Issue, opts storage.BatchCreateOptions, cache *createBatchCache) (isNew bool, staleRejected bool, err error) { var existingCount int if issue.ID != "" { - existingCount, err = cache.rowCount(ctx, tx, issueTable, issue.ID) - if err != nil { + if err := tx.QueryRowContext(ctx, fmt.Sprintf(`SELECT COUNT(*) FROM %s WHERE id = ?`, issueTable), issue.ID).Scan(&existingCount); err != nil { return false, false, fmt.Errorf("failed to check issue existence for %s: %w", issue.ID, err) } } @@ -861,7 +685,6 @@ func insertIssueIfNewCached(ctx context.Context, tx DBTX, issueTable string, iss } return false, false, err } - cache.markInserted(issueTable, issue.ID) return true, false, nil } if opts.RejectStaleUpserts && existingCount > 0 { @@ -878,7 +701,6 @@ func insertIssueIfNewCached(ctx context.Context, tx DBTX, issueTable string, iss if err := insertIssueIntoTable(ctx, tx, issueTable, issue, opts.RejectStaleUpserts); err != nil { return false, false, fmt.Errorf("failed to insert issue %s: %w", issue.ID, err) } - cache.markInserted(issueTable, issue.ID) return existingCount == 0, false, nil } @@ -904,96 +726,6 @@ func InsertIssueStrictInTx(ctx context.Context, tx DBTX, table string, issue *ty } func PersistLabels(ctx context.Context, tx DBTX, issue *types.Issue, actor, eventTable string) (CreateIssueResult, error) { - return persistLabelsCached(ctx, tx, issue, actor, eventTable, nil) -} - -// createdAuxEvent is the created event RecordEventInTable mints for a new -// issue, as an AuxEvent a batch can buffer. -func createdAuxEvent(issueID, actor string) AuxEvent { - return AuxEvent{ - IssueID: issueID, - EventType: types.EventCreated, - Actor: actor, - OldValue: str(""), - NewValue: str(""), - } -} - -// labelAddedAuxEvent is the label_added event PersistLabels records for a -// label its insert actually added. -func labelAddedAuxEvent(issueID, actor, label string) AuxEvent { - return AuxEvent{ - IssueID: issueID, - EventType: types.EventLabelAdded, - Actor: actor, - Comment: str("Added label: " + label), - } -} - -// persistLabelsCached is PersistLabels for a batch: with a cache it knows the -// issue's stored labels before inserting, so the labels that are new land in -// one multi-row INSERT and their label_added events are buffered, instead of -// one INSERT IGNORE (plus a RowsAffected probe) and one event write per label. -// The labels added, their order, and the events recorded are the same. -func persistLabelsCached(ctx context.Context, tx DBTX, issue *types.Issue, actor, eventTable string, cache *createBatchCache) (CreateIssueResult, error) { - if cache == nil { - return persistLabelsPerRow(ctx, tx, issue, actor, eventTable) - } - var result CreateIssueResult - if len(issue.Labels) == 0 { - return result, nil - } - labelTable := "labels" - if IsWisp(issue) { - labelTable = "wisp_labels" - } - stored, err := cache.storedLabels(ctx, tx, labelTable, issue.ID) - if err != nil { - return result, err - } - seen := make(map[string]struct{}, len(issue.Labels)) - var added []string - for _, label := range issue.Labels { - if _, ok := seen[label]; ok { - continue - } - seen[label] = struct{}{} - // Same over-length refusal, in the same label order, as the per-row - // path; the enclosing transaction rolls everything back. - if err := types.CheckFieldLen("label", label); err != nil { - return result, err - } - if stored[label] { - continue - } - added = append(added, label) - } - if len(added) == 0 { - return result, nil - } - values := make([]string, len(added)) - args := make([]any, 0, 2*len(added)) - for i, label := range added { - values[i] = "(?, ?)" - args = append(args, issue.ID, label) - } - //nolint:gosec // G201: table is determined by ephemeral flag; only placeholders are formatted in. - if _, err := tx.ExecContext(ctx, fmt.Sprintf(` - INSERT IGNORE INTO %s (issue_id, label) - VALUES %s - `, labelTable, strings.Join(values, ", ")), args...); err != nil { - return result, fmt.Errorf("failed to insert labels for %s: %w", issue.ID, err) - } - result.markChanged(labelTable) - result.markChanged(eventTable) - for _, label := range added { - cache.addLabel(labelTable, issue.ID, label) - cache.bufferEvent(eventTable, labelAddedAuxEvent(issue.ID, actor, label)) - } - return result, nil -} - -func persistLabelsPerRow(ctx context.Context, tx DBTX, issue *types.Issue, actor, eventTable string) (CreateIssueResult, error) { var result CreateIssueResult if len(issue.Labels) == 0 { return result, nil @@ -1032,7 +764,13 @@ func persistLabelsPerRow(ctx context.Context, tx DBTX, issue *types.Issue, actor continue } result.markChanged(labelTable) - if err := InsertDerivedEvent(ctx, tx, eventTable, labelAddedAuxEvent(issue.ID, actor, label)); err != nil { + comment := "Added label: " + label + if err := InsertDerivedEvent(ctx, tx, eventTable, AuxEvent{ + IssueID: issue.ID, + EventType: types.EventLabelAdded, + Actor: actor, + Comment: str(comment), + }); err != nil { return result, fmt.Errorf("failed to record label event %q for %s: %w", label, issue.ID, err) } result.markChanged(eventTable) @@ -1125,33 +863,23 @@ func PersistDependenciesWithOptionsResult(ctx context.Context, tx DBTX, issues [ depTable string } var pending []pendingDependency - var deps []*types.Dependency for _, issue := range issues { + if len(issue.Dependencies) == 0 { + continue + } for _, dep := range issue.Dependencies { // Default IssueID to the owning issue when not pre-set (e.g., // markdown bulk create where the ID is auto-generated). if dep.IssueID == "" { dep.IssueID = issue.ID } - deps = append(deps, dep) + depTable := "dependencies" + if IsActiveWispInTx(ctx, tx, dep.IssueID) { + depTable = "wisp_dependencies" + } + pending = append(pending, pendingDependency{dep: dep, depTable: depTable}) } } - // A multi-edge batch answers its per-edge routing, presence and graph - // reads from batch reads (depBatchLookups); nil keeps the per-edge reads. - var lookups *depBatchLookups - if len(deps) >= depBatchLookupsMinDeps && !createFastPathsDisabled.Load() { - var err error - if lookups, err = newDepBatchLookups(ctx, tx, deps); err != nil { - return result, err - } - } - for _, dep := range deps { - depTable := "dependencies" - if lookups.isWisp(ctx, tx, dep.IssueID) { - depTable = "wisp_dependencies" - } - pending = append(pending, pendingDependency{dep: dep, depTable: depTable}) - } // Persist hierarchy first so blocking edges in the same import see the full // planned ancestry. The enclosing create transaction rolls this phase back @@ -1164,22 +892,29 @@ func PersistDependenciesWithOptionsResult(ctx context.Context, tx DBTX, issues [ continue } isCrossPrefix := types.ExtractPrefix(dep.IssueID) != types.ExtractPrefix(dep.DependsOnID) - kind := lookups.classify(ctx, tx, dep, isCrossPrefix) + kind := ClassifyDepTarget(ctx, tx, dep, isCrossPrefix) if kind != DepTargetExternal { - exists, err := lookups.targetExists(ctx, tx, kind, dep.DependsOnID) - if err != nil { - return result, fmt.Errorf("failed to check dependency target %s for %s: %w", dep.DependsOnID, dep.IssueID, err) + lookupTable := "issues" + if kind == DepTargetWisp { + lookupTable = "wisps" } - if !exists { - recordSkippedDependency(opts, dep, "target not found") - continue + var exists int + //nolint:gosec // G201: lookupTable is one of two hardcoded constants + if err := tx.QueryRowContext(ctx, + fmt.Sprintf("SELECT 1 FROM %s WHERE id = ?", lookupTable), + dep.DependsOnID).Scan(&exists); err != nil { + if err == sql.ErrNoRows { + recordSkippedDependency(opts, dep, "target not found") + continue + } + return result, fmt.Errorf("failed to check dependency target %s for %s: %w", dep.DependsOnID, dep.IssueID, err) } } if kind != DepTargetExternal && types.ExtractPrefix(dep.IssueID) == types.ExtractPrefix(dep.DependsOnID) { - if err := lookups.checkHierarchy(ctx, tx, dep); err != nil { - if opts.SkipDependencyValidationErrors && isDependencyValidationRefusal(err) { + if err := CheckBlockingHierarchyInTx(ctx, tx, dep, nil); err != nil { + if opts.SkipDependencyValidationErrors { recordSkippedDependency(opts, dep, err.Error()) continue } @@ -1187,8 +922,8 @@ func PersistDependenciesWithOptionsResult(ctx context.Context, tx DBTX, issues [ } } - if err := lookups.checkCycle(ctx, tx, dep); err != nil { - if opts.SkipDependencyValidationErrors && isDependencyValidationRefusal(err) { + if err := CheckDependencyCycleInTx(ctx, tx, dep, nil); err != nil { + if opts.SkipDependencyValidationErrors { recordSkippedDependency(opts, dep, err.Error()) continue } @@ -1220,9 +955,6 @@ func PersistDependenciesWithOptionsResult(ctx context.Context, tx DBTX, issues [ if err != nil { return result, fmt.Errorf("failed to check dependency insert result for %s -> %s: %w", dep.IssueID, dep.DependsOnID, err) } - if err := lookups.recordInsert(ctx, tx, item.depTable, dep, rowsAffected); err != nil { - return result, err - } if rowsAffected > 0 { result.markChanged(item.depTable) result.persistedDependencies = append(result.persistedDependencies, persistedDependency{ @@ -1257,24 +989,6 @@ func dependencyCreatedBy(dep *types.Dependency, actor string) string { return actor } -// isDependencyValidationRefusal reports whether err, from checkHierarchy or -// checkCycle, is a typed refusal of the edge itself (a self-dependency, a -// cycle, or a blocker that is the issue's own ancestor or descendant) — the -// only failures SkipDependencyValidationErrors tolerates. Every other error is -// a failed walk query (an untranslated statement, a timeout, a dropped -// connection): recording it as a skipped edge would commit the import without -// the edge and report the SQL error only as a skip reason. On a backend whose -// transaction aborts on a failed statement, the import would instead fail -// later at an unrelated statement, with the real cause hidden in the skip -// reason. -func isDependencyValidationRefusal(err error) bool { - if errors.Is(err, domain.ErrSelfDependency) || errors.Is(err, domain.ErrDependencyCycle) { - return true - } - var hierarchy *domain.DependencyHierarchyConflictError - return errors.As(err, &hierarchy) -} - func recordSkippedDependency(opts storage.BatchCreateOptions, dep *types.Dependency, reason string) { if dep == nil { return diff --git a/internal/storage/issueops/create_batch.go b/internal/storage/issueops/create_batch.go index 29c660ccc..26c720e4a 100644 --- a/internal/storage/issueops/create_batch.go +++ b/internal/storage/issueops/create_batch.go @@ -17,11 +17,10 @@ func CloneCreateBatchRequest(request publicops.CreateBatchRequest) publicops.Cre clone := request clone.Items = make([]publicops.BatchCreateItem, len(request.Items)) for i, item := range request.Items { - // Start from the item itself so every scalar member (DefaultPriority - // today) survives the clone; only the reference members are deepened. - clone.Items[i] = item - clone.Items[i].Issue = clonePublicIssue(item.Issue) - clone.Items[i].Dependencies = append([]publicops.CreateDependency(nil), item.Dependencies...) + clone.Items[i] = publicops.BatchCreateItem{ + Issue: clonePublicIssue(item.Issue), + Dependencies: append([]publicops.CreateDependency(nil), item.Dependencies...), + } } return clone } @@ -54,11 +53,10 @@ func ValidateCreateBatchRequest(request publicops.CreateBatchRequest) error { // an item through it, so no item restates CreateRequest's field rules. func CreateBatchItemRequest(request publicops.CreateBatchRequest, item publicops.BatchCreateItem) publicops.CreateRequest { return publicops.CreateRequest{ - Actor: request.Actor, - Issue: item.Issue, - Dependencies: item.Dependencies, - ForceIDPrefix: request.ForceIDPrefix, - DefaultPriority: item.DefaultPriority, + Actor: request.Actor, + Issue: item.Issue, + Dependencies: item.Dependencies, + ForceIDPrefix: request.ForceIDPrefix, } } @@ -190,7 +188,7 @@ func ExecuteCreateBatch(ctx context.Context, tx *sql.Tx, request publicops.Creat } // CrossPlaneBatchEdgeError is the refusal the store-backed body raises from -// validateCreateIssuesMixedBucketDependencies, spelled here so the unit-of-work +// filterCreateIssuesMixedBucketDependencies, spelled here so the unit-of-work // body can raise the identical one before it starts writing. func CrossPlaneBatchEdgeError(sourceID, targetID string) error { return fmt.Errorf("mixed regular/wisp CreateIssues batch cannot include cross-bucket dependency %s -> %s; create the issues first, then add the in-batch dependency after both issues exist%.0w", diff --git a/internal/storage/issueops/create_batch_cache.go b/internal/storage/issueops/create_batch_cache.go deleted file mode 100644 index 468b409d5..000000000 --- a/internal/storage/issueops/create_batch_cache.go +++ /dev/null @@ -1,408 +0,0 @@ -package issueops - -import ( - "context" - "database/sql" - "fmt" - "strings" - - "github.com/steveyegge/beads/internal/storage" - "github.com/steveyegge/beads/internal/types" -) - -// createBatchCache amortizes the per-issue lookups a multi-issue create -// (import, bulk create) would otherwise pay one round trip each for. It is -// built once per CreateIssuesInTxWithContext call and changes HOW the batch -// learns facts, never WHICH facts it acts on: -// -// - Row presence. The cross-plane collision probe and the new-vs-upsert -// probe each read one id from one table. The cache reads every explicit -// id of the batch from both planes up front and then tracks the rows the -// batch itself inserts, so each answer equals what the per-row read would -// have returned at that point of the batch. Ids it did not read up front -// (generated ids) fall through to the per-row read. -// - Label presence. PersistLabels inserts one label per statement and -// learns from RowsAffected whether the label was new. The cache reads the -// batch's stored labels up front, so each issue's new labels are known -// before its insert, which then lands as one multi-row statement. -// - Issue rows. A run of issues the presence read shows to be brand new is -// written with multi-row INSERTs and then finished one by one, in order -// (deferredCreates); the per-row write would have been a plain insert for -// each of them. -// - Audit events. created/label_added rows in the events tables are -// content-addressed (InsertDerivedEvent) and nothing in the batch reads -// them back, so the cache buffers them and writes them in one pass at the -// end of the per-issue loop, deriving each id exactly as the per-row -// insert would have (same digest, same lowest free ordinal, counting -// same-content rows already stored and those minted earlier in the batch). -// -// A singular create never builds one: its single lookup of each kind costs -// what the cache's batch read would. -// -// COLLATION ASSUMPTION. The cache (and the dependency pass's in-memory graph, -// create_dep_batch.go) compares ids and labels with Go string equality where -// the per-row SQL compared them under the column collation. Those agree under -// the collation every beads table is created with — Dolt's default -// utf8mb4_0900_bin, which is case-sensitive and NO PAD (the migrations name -// no other). On a case-insensitive or PAD SPACE collation they would not: a -// "Bug"/"bug" label pair would land as one row but buffer two label_added -// events. A schema change to such a collation must revisit these paths. -type createBatchCache struct { - // probed holds the ids whose presence in both planes was read up front. - probed map[string]bool - // present is table -> id -> row present, as of the current point of the - // batch (the up-front read plus every row the batch has inserted since). - present map[string]map[string]bool - // labels is label table -> issue id -> stored label set, for the issues - // whose labels were read up front (see labelsProbed). - labels map[string]map[string]map[string]bool - labelsProbed map[string]map[string]bool - // events are the buffered events-plane rows, in mint order. - events []bufferedAuxEvent -} - -type bufferedAuxEvent struct { - table string - event AuxEvent -} - -// createBatchCacheMinIssues is the batch size at which the up-front reads pay -// for themselves. A one-issue create reads exactly what it would read per row. -const createBatchCacheMinIssues = 2 - -// newCreateBatchCache reads the presence of every explicit id in issues in -// both planes, and the stored labels of every explicit id that carries -// labels, in a handful of IN-list queries. -func newCreateBatchCache(ctx context.Context, tx DBTX, issues []*types.Issue) (*createBatchCache, error) { - c := &createBatchCache{ - probed: map[string]bool{}, - present: map[string]map[string]bool{"issues": {}, "wisps": {}}, - labels: map[string]map[string]map[string]bool{"labels": {}, "wisp_labels": {}}, - labelsProbed: map[string]map[string]bool{"labels": {}, "wisp_labels": {}}, - } - var ids []string - labelIDs := map[string][]string{} - for _, issue := range issues { - if issue == nil || issue.ID == "" || c.probed[issue.ID] { - continue - } - c.probed[issue.ID] = true - ids = append(ids, issue.ID) - } - for _, table := range []string{"issues", "wisps"} { - found, err := idSetInTx(ctx, tx, table, ids) - if err != nil { - return nil, err - } - for id := range found { - c.present[table][id] = true - } - } - seenLabelID := map[string]map[string]bool{"labels": {}, "wisp_labels": {}} - for _, issue := range issues { - if issue == nil || issue.ID == "" || len(issue.Labels) == 0 { - continue - } - table := "labels" - if IsWisp(issue) { - table = "wisp_labels" - } - if !seenLabelID[table][issue.ID] { - seenLabelID[table][issue.ID] = true - labelIDs[table] = append(labelIDs[table], issue.ID) - } - } - for _, table := range []string{"labels", "wisp_labels"} { - if len(labelIDs[table]) == 0 { - continue - } - if err := c.loadLabels(ctx, tx, table, labelIDs[table]); err != nil { - return nil, err - } - } - return c, nil -} - -// idSetInTx returns the subset of ids present in table (issues or wisps). -// -//nolint:gosec // G201: table is one of two hardcoded constants; only placeholders are formatted in. -func idSetInTx(ctx context.Context, tx DBTX, table string, ids []string) (map[string]bool, error) { - found := map[string]bool{} - for start := 0; start < len(ids); start += queryBatchSize { - end := min(start+queryBatchSize, len(ids)) - placeholders, args := buildSQLInClause(ids[start:end]) - rows, err := tx.QueryContext(ctx, fmt.Sprintf("SELECT id FROM %s WHERE id IN (%s)", table, placeholders), args...) - if err != nil { - return nil, fmt.Errorf("read %s presence for batch: %w", table, err) - } - for rows.Next() { - var id string - if err := rows.Scan(&id); err != nil { - _ = rows.Close() - return nil, fmt.Errorf("read %s presence for batch: %w", table, err) - } - found[id] = true - } - _ = rows.Close() - if err := rows.Err(); err != nil { - return nil, fmt.Errorf("read %s presence for batch: %w", table, err) - } - } - return found, nil -} - -//nolint:gosec // G201: table is one of two hardcoded constants; only placeholders are formatted in. -func (c *createBatchCache) loadLabels(ctx context.Context, tx DBTX, table string, ids []string) error { - for _, id := range ids { - c.labelsProbed[table][id] = true - } - for start := 0; start < len(ids); start += queryBatchSize { - end := min(start+queryBatchSize, len(ids)) - placeholders, args := buildSQLInClause(ids[start:end]) - rows, err := tx.QueryContext(ctx, fmt.Sprintf("SELECT issue_id, label FROM %s WHERE issue_id IN (%s)", table, placeholders), args...) - if err != nil { - return fmt.Errorf("read stored labels for batch: %w", err) - } - for rows.Next() { - var issueID, label string - if err := rows.Scan(&issueID, &label); err != nil { - _ = rows.Close() - return fmt.Errorf("read stored labels for batch: %w", err) - } - c.addLabel(table, issueID, label) - } - _ = rows.Close() - if err := rows.Err(); err != nil { - return fmt.Errorf("read stored labels for batch: %w", err) - } - } - return nil -} - -func (c *createBatchCache) addLabel(table, issueID, label string) { - set := c.labels[table][issueID] - if set == nil { - set = map[string]bool{} - c.labels[table][issueID] = set - } - set[label] = true -} - -// rowPresent answers "is there a row with id in table right now" when the -// cache can; ok is false for an id it did not read up front. -func (c *createBatchCache) rowPresent(table, id string) (present, ok bool) { - if c == nil || !c.probed[id] { - return false, false - } - return c.present[table][id], true -} - -// rowCount adapts rowPresent to the COUNT(*) shape the per-row probes scan. -func (c *createBatchCache) rowCount(ctx context.Context, tx DBTX, table, id string) (int, error) { - if present, ok := c.rowPresent(table, id); ok { - if present { - return 1, nil - } - return 0, nil - } - var count int - //nolint:gosec // G201: table is one of two hardcoded constants. - err := tx.QueryRowContext(ctx, fmt.Sprintf(`SELECT COUNT(*) FROM %s WHERE id = ?`, table), id).Scan(&count) - return count, err -} - -// markInserted records that the batch wrote (inserted or upserted) a row with -// id into table. -func (c *createBatchCache) markInserted(table, id string) { - if c == nil { - return - } - c.present[table][id] = true -} - -// storedLabels returns the labels stored for issueID in table, reading them -// when the up-front read did not cover the issue (a generated id). -// -//nolint:gosec // G201: table is one of two hardcoded constants. -func (c *createBatchCache) storedLabels(ctx context.Context, tx DBTX, table, issueID string) (map[string]bool, error) { - if !c.labelsProbed[table][issueID] { - if err := c.loadLabels(ctx, tx, table, []string{issueID}); err != nil { - return nil, err - } - } - return c.labels[table][issueID], nil -} - -// bufferEvent queues one events-plane row for flushEvents. -func (c *createBatchCache) bufferEvent(table string, e AuxEvent) { - c.events = append(c.events, bufferedAuxEvent{table: table, event: normalizeAuxEvent(table, e)}) -} - -// auxEventKey is the same-content key InsertDerivedEventReturningID matches on. -type auxEventKey struct { - issueID, eventType, actor string - oldValue, newValue, comment sql.NullString - createdAt string -} - -func keyOfAuxEvent(e AuxEvent) auxEventKey { - return auxEventKey{ - issueID: e.IssueID, eventType: string(e.EventType), actor: e.Actor, - oldValue: e.OldValue, newValue: e.NewValue, comment: e.Comment, - createdAt: e.CreatedAt, - } -} - -// flushEvents writes the buffered events, per table, under the ids the -// per-row InsertDerivedEvent would have minted: it reads the stored rows that -// could share content with a buffered one (same issue, same second), then -// assigns each buffered row the lowest free ordinal of its digest, counting -// those stored rows and the rows minted before it in this flush. -func (c *createBatchCache) flushEvents(ctx context.Context, tx DBTX) error { - if c == nil || len(c.events) == 0 { - return nil - } - byTable := map[string][]AuxEvent{} - var tables []string - for _, b := range c.events { - if _, ok := byTable[b.table]; !ok { - tables = append(tables, b.table) - } - byTable[b.table] = append(byTable[b.table], b.event) - } - c.events = nil - for _, table := range tables { - if err := flushAuxEvents(ctx, tx, table, byTable[table]); err != nil { - return err - } - } - return nil -} - -//nolint:gosec // G201: table is a hardcoded routing constant; only placeholders are formatted in. -func flushAuxEvents(ctx context.Context, tx DBTX, table string, events []AuxEvent) error { - taken := map[auxEventKey]map[string]bool{} - var issueIDs, createdAts []string - seenIssue, seenAt := map[string]bool{}, map[string]bool{} - for _, e := range events { - if !seenIssue[e.IssueID] { - seenIssue[e.IssueID] = true - issueIDs = append(issueIDs, e.IssueID) - } - if !seenAt[e.CreatedAt] { - seenAt[e.CreatedAt] = true - createdAts = append(createdAts, e.CreatedAt) - } - } - atPlaceholders, atArgs := buildSQLInClause(createdAts) - for start := 0; start < len(issueIDs); start += queryBatchSize { - end := min(start+queryBatchSize, len(issueIDs)) - idPlaceholders, idArgs := buildSQLInClause(issueIDs[start:end]) - args := append(append([]any{}, idArgs...), atArgs...) - rows, err := tx.QueryContext(ctx, fmt.Sprintf(` - SELECT id, issue_id, event_type, actor, old_value, new_value, comment, - DATE_FORMAT(created_at, '%%Y-%%m-%%d %%H:%%i:%%s') - FROM %s - WHERE issue_id IN (%s) AND created_at IN (%s)`, table, idPlaceholders, atPlaceholders), args...) - if err != nil { - return fmt.Errorf("scan same-content events in %s: %w", table, err) - } - for rows.Next() { - var id string - var k auxEventKey - if err := rows.Scan(&id, &k.issueID, &k.eventType, &k.actor, &k.oldValue, &k.newValue, &k.comment, &k.createdAt); err != nil { - _ = rows.Close() - return fmt.Errorf("scan same-content events in %s: %w", table, err) - } - if taken[k] == nil { - taken[k] = map[string]bool{} - } - taken[k][id] = true - } - _ = rows.Close() - if err := rows.Err(); err != nil { - return fmt.Errorf("scan same-content events in %s: %w", table, err) - } - } - - const cols = 8 - const rowsPerInsert = queryBatchSize - for start := 0; start < len(events); start += rowsPerInsert { - end := min(start+rowsPerInsert, len(events)) - values := make([]string, 0, end-start) - args := make([]any, 0, (end-start)*cols) - for _, e := range events[start:end] { - k := keyOfAuxEvent(e) - if taken[k] == nil { - taken[k] = map[string]bool{} - } - id := firstFreeDerivedID(table, auxEventDigest(e), taken[k]) - taken[k][id] = true - values = append(values, "(?, ?, ?, ?, ?, ?, ?, ?)") - args = append(args, id, e.IssueID, string(e.EventType), e.Actor, e.OldValue, e.NewValue, e.Comment, e.CreatedAt) - } - if _, err := tx.ExecContext(ctx, fmt.Sprintf(` - INSERT INTO %s (id, issue_id, event_type, actor, old_value, new_value, comment, created_at) - VALUES %s`, table, strings.Join(values, ", ")), args...); err != nil { - return fmt.Errorf("record events in %s: %w", table, err) - } - } - return nil -} - -// deferrable reports whether issue's row can join a multi-row INSERT: the -// batch knows, from its up-front read and its own writes, that no row with -// this id exists in either plane, so the per-row write would be a plain -// insert (InsertIssueIfNew's existence probe answers 0; ConflictSkip and -// RejectStaleUpserts only act on an existing row). CreateOnly batches keep the -// per-row path for its shard coordination write. -func (c *createBatchCache) deferrable(issue *types.Issue, opts storage.BatchCreateOptions) bool { - if c == nil || issue == nil || issue.ID == "" || opts.CreateOnly || !c.probed[issue.ID] { - return false - } - return !c.present["issues"][issue.ID] && !c.present["wisps"][issue.ID] -} - -// deferredCreate is one prepared issue whose row write is pending. -type deferredCreate struct { - issue *types.Issue - issueTable, eventTable string -} - -// deferredCreates is a run of prepared, brand-new issues. Writing their rows -// together and then finishing each in order is invisible to the batch: every -// finishing step (lease, events, labels, comments, journal, version seam) -// reads only its own issue's rows, which are all written by then. -type deferredCreates struct { - created []deferredCreate -} - -// flush writes the pending rows per table in multi-row INSERTs, then -// finishes each issue in order, returning one result per pending issue. -func (d *deferredCreates) flush(ctx context.Context, tx DBTX, bc *BatchContext, actor string) ([]CreateIssueResult, error) { - if len(d.created) == 0 { - return nil, nil - } - byTable := map[string][]*types.Issue{} - var tables []string - for _, c := range d.created { - if _, ok := byTable[c.issueTable]; !ok { - tables = append(tables, c.issueTable) - } - byTable[c.issueTable] = append(byTable[c.issueTable], c.issue) - } - for _, table := range tables { - if err := insertIssueRowsIntoTable(ctx, tx, table, byTable[table], bc.Opts.RejectStaleUpserts); err != nil { - return nil, err - } - } - results := make([]CreateIssueResult, len(d.created)) - for i, c := range d.created { - result, err := finishCreateIssueInTx(ctx, tx, bc, c.issue, actor, c.issueTable, c.eventTable, true) - if err != nil { - return nil, err - } - results[i] = result - } - return results, nil -} diff --git a/internal/storage/issueops/create_batch_cache_test.go b/internal/storage/issueops/create_batch_cache_test.go deleted file mode 100644 index db2719a8c..000000000 --- a/internal/storage/issueops/create_batch_cache_test.go +++ /dev/null @@ -1,284 +0,0 @@ -package issueops - -import ( - "context" - "database/sql" - "database/sql/driver" - "errors" - "fmt" - "regexp" - "strings" - "testing" - - "github.com/DATA-DOG/go-sqlmock" - - "github.com/steveyegge/beads/internal/storage/rowid" - "github.com/steveyegge/beads/internal/types" -) - -// TestFlushAuxEventsMintsThePerRowIDs pins that the buffered events flush -// assigns each row the id InsertDerivedEvent would have: the lowest ordinal of -// its digest not already held by a same-content row — counting rows already -// stored and rows minted earlier in the same flush — while a different-content -// row with the same issue and second is not counted. -func TestFlushAuxEventsMintsThePerRowIDs(t *testing.T) { - ctx := context.Background() - db, mock, tx := beginMockTx(t) - defer db.Close() - - const at = "2026-01-02 03:04:05" - created := normalizeAuxEvent("events", AuxEvent{ - IssueID: "bd-1", EventType: types.EventCreated, Actor: "importer", - OldValue: str(""), NewValue: str(""), CreatedAt: at, - }) - label := normalizeAuxEvent("events", AuxEvent{ - IssueID: "bd-1", EventType: types.EventLabelAdded, Actor: "importer", - Comment: str("Added label: x"), CreatedAt: at, - }) - createdDigest, labelDigest := auxEventDigest(created), auxEventDigest(label) - storedCreated := rowid.New("events", 0, createdDigest) - - mock.ExpectQuery(regexp.QuoteMeta("FROM events")). - WithArgs("bd-1", at). - WillReturnRows(sqlmock.NewRows([]string{"id", "issue_id", "event_type", "actor", "old_value", "new_value", "comment", "created_at"}). - // A stored same-content created row holds ordinal 0. - AddRow(storedCreated, "bd-1", string(types.EventCreated), "importer", "", "", nil, at). - // Same issue and second, different content: not counted. - AddRow("legacy-random-id", "bd-1", string(types.EventLabelAdded), "importer", nil, nil, "Added label: y", at)) - mock.ExpectExec(regexp.QuoteMeta("INSERT INTO events")). - WithArgs( - rowid.New("events", 1, createdDigest), "bd-1", string(types.EventCreated), "importer", str(""), str(""), sql.NullString{}, at, - rowid.New("events", 2, createdDigest), "bd-1", string(types.EventCreated), "importer", str(""), str(""), sql.NullString{}, at, - rowid.New("events", 0, labelDigest), "bd-1", string(types.EventLabelAdded), "importer", sql.NullString{}, sql.NullString{}, str("Added label: x"), at, - ). - WillReturnResult(sqlmock.NewResult(0, 3)) - - cache := &createBatchCache{} - cache.bufferEvent("events", created) - cache.bufferEvent("events", created) - cache.bufferEvent("events", label) - if err := cache.flushEvents(ctx, tx); err != nil { - t.Fatalf("flushEvents: %v", err) - } - if err := mock.ExpectationsWereMet(); err != nil { - t.Fatalf("unmet expectations: %v", err) - } -} - -// TestDepBatchGraphTracksTheStoredRows pins the graph's bookkeeping across the -// dependency pass's inserts: an unseen pair is added with the inserted type -// without a read; a pair it already holds is re-read, because a matched -// duplicate (which keeps the stored type) and a real insert both report -// RowsAffected 1 on a clientFoundRows connection. -func TestDepBatchGraphTracksTheStoredRows(t *testing.T) { - ctx := context.Background() - db, mock, tx := beginMockTx(t) - defer db.Close() - - l := &depBatchLookups{graph: newDepGraph()} - for _, id := range []string{"bd-a", "bd-b", "bd-c"} { - l.graph.loadedOut[id] = true - l.graph.loadedIn[id] = true - } - l.graph.add(depEdgeKey{table: "dependencies", source: "bd-b", target: "bd-a"}, types.DepRelated) - reaches := func(from, to string, parentsOnly bool) bool { - t.Helper() - ok, err := l.graph.reaches(ctx, tx, from, to, parentsOnly) - if err != nil { - t.Fatalf("reaches: %v", err) - } - return ok - } - - // New pair: no read, and the scheduling walk sees it at once. - if err := l.recordInsert(ctx, tx, "dependencies", &types.Dependency{IssueID: "bd-c", DependsOnID: "bd-b", Type: types.DepBlocks}, 1); err != nil { - t.Fatalf("recordInsert(new): %v", err) - } - if !reaches("bd-c", "bd-b", false) { - t.Fatal("new blocks edge not walkable") - } - // Known pair reported as written: re-read; the stored row kept "related". - mock.ExpectQuery(regexp.QuoteMeta("SELECT type FROM dependencies WHERE issue_id = ? AND "+DepTargetExpr+" = ?")). - WithArgs("bd-b", "bd-a"). - WillReturnRows(sqlmock.NewRows([]string{"type"}).AddRow(string(types.DepRelated))) - if err := l.recordInsert(ctx, tx, "dependencies", &types.Dependency{IssueID: "bd-b", DependsOnID: "bd-a", Type: types.DepParentChild}, 1); err != nil { - t.Fatalf("recordInsert(known): %v", err) - } - if reaches("bd-b", "bd-a", true) || reaches("bd-c", "bd-a", false) { - t.Fatal("a matched duplicate must not add the incoming type's edge") - } - // Nothing written: nothing changes, nothing is read. - if err := l.recordInsert(ctx, tx, "dependencies", &types.Dependency{IssueID: "bd-a", DependsOnID: "bd-c", Type: types.DepBlocks}, 0); err != nil { - t.Fatalf("recordInsert(noop): %v", err) - } - if reaches("bd-a", "bd-c", false) { - t.Fatal("an unwritten edge must not be walkable") - } - if err := mock.ExpectationsWereMet(); err != nil { - t.Fatalf("unmet expectations: %v", err) - } -} - -// TestIssueInsertChunksHonorsRowsAndBytes pins the multi-row INSERT budget: -// at most issueInsertRowsPerStatement rows, a new statement before the -// estimated bytes would pass issueInsertBytesPerStatement, and an oversized -// row still travels (alone) rather than being dropped. -func TestIssueInsertChunksHonorsRowsAndBytes(t *testing.T) { - small := func(n int) []*types.Issue { - out := make([]*types.Issue, n) - for i := range out { - out[i] = &types.Issue{ID: fmt.Sprintf("bd-%d", i), Title: "t"} - } - return out - } - sizes := func(chunks [][]*types.Issue) []int { - var out []int - for _, c := range chunks { - out = append(out, len(c)) - } - return out - } - if got := sizes(issueInsertChunks(small(250))); fmt.Sprint(got) != "[100 100 50]" { - t.Fatalf("250 small rows chunked as %v, want [100 100 50]", got) - } - big := strings.Repeat("x", 10<<20) - huge := strings.Repeat("y", 20<<20) - issues := []*types.Issue{ - {ID: "bd-a", Description: big}, - {ID: "bd-b", Notes: big}, - {ID: "bd-c", Title: "small"}, - {ID: "bd-d", Description: huge}, - {ID: "bd-e", Title: "small"}, - } - if got := sizes(issueInsertChunks(issues)); fmt.Sprint(got) != "[1 2 1 1]" { - t.Fatalf("long-text rows chunked as %v, want [1 2 1 1]", got) - } -} - -// TestInsertIssueRowsReplaysAFailedStatement pins the multi-row INSERT's -// failure handling: the rows of a refused statement are written one at a -// time, the batch continues when every row lands, and the first row that -// cannot land fails it with the per-row path's error. -func TestInsertIssueRowsReplaysAFailedStatement(t *testing.T) { - issues := []*types.Issue{{ID: "bd-1", Title: "one"}, {ID: "bd-2", Title: "two"}} - multi := regexp.QuoteMeta("INSERT INTO issues") - t.Run("replay succeeds", func(t *testing.T) { - db, mock, tx := beginMockTx(t) - defer db.Close() - mock.ExpectExec(multi).WillReturnError(errors.New("packet too large")) - mock.ExpectExec(multi).WithArgs(issueRowArgMatchers("bd-1")...).WillReturnResult(sqlmock.NewResult(0, 1)) - mock.ExpectExec(multi).WithArgs(issueRowArgMatchers("bd-2")...).WillReturnResult(sqlmock.NewResult(0, 1)) - if err := insertIssueRowsIntoTable(context.Background(), tx, "issues", issues, false); err != nil { - t.Fatalf("insertIssueRowsIntoTable = %v, want nil after a clean replay", err) - } - if err := mock.ExpectationsWereMet(); err != nil { - t.Fatal(err) - } - }) - t.Run("replay fails", func(t *testing.T) { - db, mock, tx := beginMockTx(t) - defer db.Close() - mock.ExpectExec(multi).WillReturnError(errors.New("packet too large")) - mock.ExpectExec(multi).WithArgs(issueRowArgMatchers("bd-1")...).WillReturnResult(sqlmock.NewResult(0, 1)) - mock.ExpectExec(multi).WithArgs(issueRowArgMatchers("bd-2")...).WillReturnError(errors.New("data too long")) - err := insertIssueRowsIntoTable(context.Background(), tx, "issues", issues, false) - if err == nil || !strings.Contains(err.Error(), "failed to insert issue bd-2") || !strings.Contains(err.Error(), "data too long") { - t.Fatalf("insertIssueRowsIntoTable = %v, want the per-row error for bd-2", err) - } - }) -} - -// issueRowArgMatchers matches one single-row issue INSERT whose id is id. -func issueRowArgMatchers(id string) []driver.Value { - args := make([]driver.Value, len(issueInsertArgs(&types.Issue{}))) - for i := range args { - args[i] = sqlmock.AnyArg() - } - args[0] = id - return args -} - -// TestDepGraphSearchesFromBothEnds pins the lazy, bidirectional search: each -// step expands the smaller frontier, loading only that level's unloaded nodes -// in that direction (outgoing through issue_id, incoming through the three -// target columns), never the whole edge tables and never a node twice; an -// incoming read ignores a row matched through a column that is not its -// target; and a write whose pair the graph cannot vouch for is re-read -// rather than guessed. -func TestDepGraphSearchesFromBothEnds(t *testing.T) { - ctx := context.Background() - db, mock, tx := beginMockTx(t) - defer db.Close() - type row = [3]string - expect := func(incoming bool, ids []string, rows map[string][]row) { - args := make([]driver.Value, len(ids)) - for i, id := range ids { - args[i] = id - } - for _, table := range []string{"dependencies", "wisp_dependencies"} { - cols := []string{"issue_id"} - if incoming { - cols = []string{"depends_on_issue_id", "depends_on_wisp_id", "depends_on_external"} - } - for i, col := range cols { - r := sqlmock.NewRows([]string{"issue_id", "target", "type"}) - if i == 0 { - for _, x := range rows[table] { - r.AddRow(x[0], x[1], x[2]) - } - } - q := "SELECT issue_id, " + DepTargetExpr + ", type FROM " + table + " WHERE " + col + " IN (" - mock.ExpectQuery(regexp.QuoteMeta(q)).WithArgs(args...).WillReturnRows(r) - } - } - } - g := newDepGraph() - // Stored: a -> b (blocks), b -> c (parent-child), w -> a (wisp table, - // blocks). Is a reachable from c? (No.) Is c reachable from w? (Yes.) - // Step 1, frontiers {c} and {a} tie: forward from c, which has no edges. - expect(false, []string{"c"}, nil) - if ok, err := g.reaches(ctx, tx, "c", "a", false); err != nil || ok { - t.Fatalf("reaches(c, a) = %v, %v; want false, nil", ok, err) - } - // Forward from w: w -> a; then backward from c (smaller? tie: forward - // again from {a}): a -> b; then {b} vs {c}: forward b -> c meets c. - expect(false, []string{"w"}, map[string][]row{"wisp_dependencies": {{"w", "a", "blocks"}}}) - expect(false, []string{"a"}, map[string][]row{"dependencies": {{"a", "b", "blocks"}}}) - expect(false, []string{"b"}, map[string][]row{"dependencies": {{"b", "c", "parent-child"}}}) - if ok, err := g.reaches(ctx, tx, "w", "c", false); err != nil || !ok { - t.Fatalf("reaches(w, c) = %v, %v; want true, nil", ok, err) - } - // Backward: once the forward frontier {x, y} outgrows the backward one, - // the search keeps reading incoming rows — c, b, a, w — and never loads - // x or y. A row matched through a non-target column is ignored, and rows - // already known (b -> c, a -> b, w -> a) are not counted again. - g.loadedOut["s"] = true - g.add(depEdgeKey{table: "dependencies", source: "s", target: "x"}, types.DepBlocks) - g.add(depEdgeKey{table: "dependencies", source: "s", target: "y"}, types.DepBlocks) - expect(true, []string{"c"}, map[string][]row{"dependencies": {{"b", "c", "parent-child"}, {"q", "elsewhere", "blocks"}}}) - expect(true, []string{"b"}, map[string][]row{"dependencies": {{"a", "b", "blocks"}}}) - expect(true, []string{"a"}, map[string][]row{"wisp_dependencies": {{"w", "a", "blocks"}}}) - expect(true, []string{"w"}, nil) - if ok, err := g.reaches(ctx, tx, "s", "c", false); err != nil || ok { - t.Fatalf("reaches(s, c) = %v, %v; want false, nil", ok, err) - } - if _, known := g.rows[depEdgeKey{table: "dependencies", source: "q", target: "elsewhere"}]; known { - t.Fatal("a row matched through a non-target column was taken as an incoming edge") - } - if n := g.out.parents["b"]["c"]; n != 1 { - t.Fatalf("edge b -> c counted %d times after being read from both ends, want 1", n) - } - // A written pair neither end vouches for is re-read, not assumed new. - l := &depBatchLookups{graph: g} - mock.ExpectQuery(regexp.QuoteMeta("SELECT type FROM dependencies WHERE issue_id = ? AND "+DepTargetExpr+" = ?")). - WithArgs("m", "n").WillReturnRows(sqlmock.NewRows([]string{"type"}).AddRow("related")) - if err := l.recordInsert(ctx, tx, "dependencies", &types.Dependency{IssueID: "m", DependsOnID: "n", Type: types.DepBlocks}, 1); err != nil { - t.Fatalf("recordInsert: %v", err) - } - if g.out.sched["m"]["n"] != 0 { - t.Fatal("a matched duplicate's incoming type was taken as the stored one") - } - if err := mock.ExpectationsWereMet(); err != nil { - t.Fatal(err) - } -} diff --git a/internal/storage/issueops/create_dep_batch.go b/internal/storage/issueops/create_dep_batch.go deleted file mode 100644 index ae6c69abb..000000000 --- a/internal/storage/issueops/create_dep_batch.go +++ /dev/null @@ -1,467 +0,0 @@ -package issueops - -import ( - "context" - "database/sql" - "fmt" - - "github.com/steveyegge/beads/internal/storage/domain" - "github.com/steveyegge/beads/internal/types" -) - -// depBatchLookups answers the per-edge reads PersistDependenciesWithOptionsResult -// makes — plane routing, target presence, and the cycle and hierarchy walks — -// from a few batch reads instead of one or more statements per edge. -// -// Routing and presence: the dependency pass runs after every row of the batch -// is written and writes no issue or wisp rows itself, so one read of the wisps -// set and of the target ids present in issues answers every per-edge -// IsActiveWispInTx / SELECT 1 the pass would make, identically. -// -// The walks: per edge, CheckBlockingHierarchyInTx and CheckDependencyCycleInTx -// each run a recursive CTE over the union of both dependency tables. Dolt -// cannot index into that union, so every recursion step rescans the edge -// tables, and a batch whose edges form a chain pays that per edge — a 400-issue -// import with one blocks edge each spent ~73s of ~82s there. depGraph answers -// the same reachability questions in memory with a bidirectional search: it -// walks forward from the start along outgoing edges and backward from the goal -// along incoming ones, expanding the smaller frontier, and loads a node's -// edges in a direction (through the issue_id index, or the target-column -// indexes) the first time the search needs them. The batch's endpoints are -// loaded up front. A batch's goal is nearly always a row the batch itself just -// created, whose only incoming edges are the batch's own, so the backward -// side usually settles a check without a read. Memory and reads scale with -// the subgraph the searches touch, not with the edge tables, and every -// written edge is applied, so each check sees exactly the graph the CTE would -// have seen at that point. -type depBatchLookups struct { - wisps map[string]struct{} - issuesExists map[string]bool - graph *depGraph -} - -// depBatchGraphMinEdges is the number of scheduling edges in a batch at which -// the in-memory walk replaces the per-edge CTEs. Each per-edge CTE scans the -// edge tables at least once, while the walk reads only the edges it reaches, -// so the threshold is small. -const depBatchGraphMinEdges = 2 - -// depBatchLookupsMinDeps is the number of edges at which the batch routing -// read replaces the per-edge reads. -const depBatchLookupsMinDeps = 2 - -func newDepBatchLookups(ctx context.Context, tx DBTX, deps []*types.Dependency) (*depBatchLookups, error) { - l := &depBatchLookups{} - var ids []string - seen := map[string]bool{} - add := func(id string) { - if id != "" && !seen[id] { - seen[id] = true - ids = append(ids, id) - } - } - scheduling := 0 - for _, dep := range deps { - add(dep.IssueID) - add(dep.DependsOnID) - if types.IsSchedulingEdge(dep.Type) && dep.IssueID != dep.DependsOnID { - scheduling++ - } - } - wisps, err := WispIDSetInTx(ctx, tx, ids) - if err != nil { - return nil, fmt.Errorf("route batch dependencies: %w", err) - } - l.wisps = wisps - var issueTargets []string - seenTarget := map[string]bool{} - for _, dep := range deps { - if _, isWisp := wisps[dep.DependsOnID]; isWisp || seenTarget[dep.DependsOnID] { - continue - } - seenTarget[dep.DependsOnID] = true - issueTargets = append(issueTargets, dep.DependsOnID) - } - if l.issuesExists, err = idSetInTx(ctx, tx, "issues", issueTargets); err != nil { - return nil, err - } - if scheduling >= depBatchGraphMinEdges { - l.graph = newDepGraph() - // Every edge the pass writes has its source here, and every search - // starts (forward) and ends (backward) at one of these. - if err = l.graph.load(ctx, tx, ids, false); err != nil { - return nil, err - } - if err = l.graph.load(ctx, tx, ids, true); err != nil { - return nil, err - } - } - return l, nil -} - -// isWisp is IsActiveWispInTx for an id the batch read covered. -func (l *depBatchLookups) isWisp(ctx context.Context, tx DBTX, id string) bool { - if l == nil { - return IsActiveWispInTx(ctx, tx, id) - } - _, ok := l.wisps[id] - return ok -} - -// classify is ClassifyDepTarget using the batch routing read. -func (l *depBatchLookups) classify(ctx context.Context, tx DBTX, dep *types.Dependency, isCrossPrefix bool) DepTargetKind { - if l == nil { - return ClassifyDepTarget(ctx, tx, dep, isCrossPrefix) - } - if isCrossPrefix || IsExternalDepTarget(dep.IssueID, dep.DependsOnID) { - return DepTargetExternal - } - if l.isWisp(ctx, tx, dep.DependsOnID) { - return DepTargetWisp - } - return DepTargetIssue -} - -// targetExists is the per-edge `SELECT 1 FROM WHERE id = ?`. -// -//nolint:gosec // G201: lookupTable is one of two hardcoded constants. -func (l *depBatchLookups) targetExists(ctx context.Context, tx DBTX, kind DepTargetKind, id string) (bool, error) { - if l != nil { - if kind == DepTargetWisp { - // The wisps set IS the presence proof: classify returned - // DepTargetWisp only because the id is in it, and the pass writes - // no wisp rows, so the per-edge `SELECT 1 FROM wisps` would find it. - return true, nil - } - return l.issuesExists[id], nil - } - lookupTable := "issues" - if kind == DepTargetWisp { - lookupTable = "wisps" - } - var exists int - err := tx.QueryRowContext(ctx, fmt.Sprintf("SELECT 1 FROM %s WHERE id = ?", lookupTable), id).Scan(&exists) - if err == sql.ErrNoRows { - return false, nil - } - if err != nil { - return false, err - } - return true, nil -} - -// checkHierarchy is CheckBlockingHierarchyInTx(ctx, tx, dep, nil). -func (l *depBatchLookups) checkHierarchy(ctx context.Context, tx DBTX, dep *types.Dependency) error { - if l == nil || l.graph == nil { - return CheckBlockingHierarchyInTx(ctx, tx, dep, nil) - } - if dep.Type != types.DepBlocks && dep.Type != types.DepConditionalBlocks { - return nil - } - if dep.IssueID == dep.DependsOnID { - return nil - } - ancestor, err := l.graph.reaches(ctx, tx, dep.IssueID, dep.DependsOnID, true) - if err != nil { - return fmt.Errorf("failed to check blocker ancestry: %w", err) - } - if ancestor { - return &domain.DependencyHierarchyConflictError{ - IssueID: dep.IssueID, BlockerID: dep.DependsOnID, BlockerIsAncestor: true, - } - } - descendant, err := l.graph.reaches(ctx, tx, dep.DependsOnID, dep.IssueID, true) - if err != nil { - return fmt.Errorf("failed to check blocker ancestry: %w", err) - } - if descendant { - return &domain.DependencyHierarchyConflictError{ - IssueID: dep.IssueID, BlockerID: dep.DependsOnID, - } - } - return nil -} - -// checkCycle is CheckDependencyCycleInTx(ctx, tx, dep, nil). -func (l *depBatchLookups) checkCycle(ctx context.Context, tx DBTX, dep *types.Dependency) error { - if l == nil || l.graph == nil { - return CheckDependencyCycleInTx(ctx, tx, dep, nil) - } - if dep.IssueID == dep.DependsOnID { - return fmt.Errorf("%w: %s cannot depend on itself", domain.ErrSelfDependency, dep.IssueID) - } - if !types.IsSchedulingEdge(dep.Type) { - return nil - } - cycle, err := l.graph.reaches(ctx, tx, dep.DependsOnID, dep.IssueID, false) - if err != nil { - return fmt.Errorf("failed to check for dependency cycle: %w", err) - } - if cycle { - return domain.ErrDependencyCycle - } - return nil -} - -// recordInsert keeps the in-memory graph equal to the edge tables after the -// pass's INSERT ... ON DUPLICATE KEY UPDATE type = type for dep. rowsAffected -// alone cannot say whether that statement inserted (a connection with -// clientFoundRows reports a matched duplicate as 1), so: a pair the graph has -// never seen, while it holds every outgoing row of the source (or every -// incoming row of the target), was certainly inserted with dep.Type; any other -// pair is re-read from the table. -func (l *depBatchLookups) recordInsert(ctx context.Context, tx DBTX, depTable string, dep *types.Dependency, rowsAffected int64) error { - if l == nil || l.graph == nil || rowsAffected == 0 { - return nil - } - key := depEdgeKey{table: depTable, source: dep.IssueID, target: dep.DependsOnID} - _, known := l.graph.rows[key] - if !known && (l.graph.loadedOut[dep.IssueID] || l.graph.loadedIn[dep.DependsOnID]) { - l.graph.add(key, dep.Type) - return nil - } - return l.graph.reload(ctx, tx, key) -} - -type depEdgeKey struct { - table, source, target string -} - -// depGraph mirrors the dependency rows it has read, from both tables, as -// (table, source, target) -> types — a key holds at most one row (the primary -// key is derived from the pair) — with adjacency counts for both search -// directions and both edge sets: scheduling (blocks, conditional-blocks, -// parent-child — cycleReachabilityQuery's) and parent-child alone -// (isAncestorInTx's). loadedOut[n] means every outgoing row of n is known, -// loadedIn[n] every incoming one; each is read at most once, and add/reload -// keep the known rows equal to the stored ones as the pass writes. -type depGraph struct { - loadedOut, loadedIn map[string]bool - rows map[depEdgeKey][]types.DependencyType - out, in depAdjacency -} - -type depAdjacency struct { - sched, parents map[string]map[string]int -} - -func newDepAdjacency() depAdjacency { - return depAdjacency{sched: map[string]map[string]int{}, parents: map[string]map[string]int{}} -} - -func (a depAdjacency) edges(parentsOnly bool) map[string]map[string]int { - if parentsOnly { - return a.parents - } - return a.sched -} - -func newDepGraph() *depGraph { - return &depGraph{ - loadedOut: map[string]bool{}, - loadedIn: map[string]bool{}, - rows: map[depEdgeKey][]types.DependencyType{}, - out: newDepAdjacency(), - in: newDepAdjacency(), - } -} - -// load reads, for every id in ids not yet loaded in that direction, its -// outgoing rows (incoming false: the issue_id index) or its incoming rows -// (incoming true: one read per target-column index), per table per -// queryBatchSize chunk. Rows already known are skipped, so a row reached from -// both ends is counted once. -// -//nolint:gosec // G201: table names are the fixed cycleDetectionTables; only placeholders are formatted in. -func (g *depGraph) load(ctx context.Context, tx DBTX, ids []string, incoming bool) error { - loaded := g.loadedOut - if incoming { - loaded = g.loadedIn - } - var todo []string - for _, id := range ids { - if id != "" && !loaded[id] { - loaded[id] = true - todo = append(todo, id) - } - } - for start := 0; start < len(todo); start += queryBatchSize { - chunk := todo[start:min(start+queryBatchSize, len(todo))] - placeholders, args := buildSQLInClause(chunk) - want := make(map[string]bool, len(chunk)) - for _, id := range chunk { - want[id] = true - } - for _, table := range cycleDetectionTables() { - // Outgoing rows come through the issue_id index. Incoming rows - // come through each target column's own index, one read per - // column: Dolt plans a UNION ALL of the three as a scan (~0.7 s - // on a 100k-edge table) where each leg alone is an index lookup. - queries := []string{fmt.Sprintf("SELECT issue_id, %s, type FROM %s WHERE issue_id IN (%s)", DepTargetExpr, table, placeholders)} - if incoming { - queries = queries[:0] - for _, col := range []string{"depends_on_issue_id", "depends_on_wisp_id", "depends_on_external"} { - queries = append(queries, fmt.Sprintf("SELECT issue_id, %s, type FROM %s WHERE %s IN (%s)", DepTargetExpr, table, col, placeholders)) - } - } - read := map[depEdgeKey][]types.DependencyType{} - for _, query := range queries { - if err := g.readRows(ctx, tx, table, query, args, incoming, want, read); err != nil { - return err - } - } - for key, depTypes := range read { - if incoming && len(depTypes) > 1 { - // The same row matched through two target columns. - depTypes = depTypes[:1] - } - for _, depType := range depTypes { - g.add(key, depType) - } - } - } - } - return nil -} - -// readRows collects the rows query returns into read, skipping rows the graph -// already knows and, for an incoming read, rows whose target (the first set -// target column, as in the CTEs) is not one of the ids asked for. A row with -// no target set (target "") joins to a NULL node in the CTE and is tracked -// for duplicate detection only. -func (g *depGraph) readRows(ctx context.Context, tx DBTX, table, query string, args []any, incoming bool, want map[string]bool, read map[depEdgeKey][]types.DependencyType) error { - rows, err := tx.QueryContext(ctx, query, args...) - if err != nil { - return fmt.Errorf("load dependency edges from %s: %w", table, err) - } - defer func() { _ = rows.Close() }() - for rows.Next() { - var source string - var target sql.NullString - var depType string - if err := rows.Scan(&source, &target, &depType); err != nil { - return fmt.Errorf("load dependency edges from %s: %w", table, err) - } - if incoming && !want[target.String] { - continue - } - key := depEdgeKey{table: table, source: source, target: target.String} - if _, known := g.rows[key]; known { - continue - } - read[key] = append(read[key], types.DependencyType(depType)) - } - if err := rows.Err(); err != nil { - return fmt.Errorf("load dependency edges from %s: %w", table, err) - } - return nil -} - -func bump(m map[string]map[string]int, from, to string, delta int) { - inner := m[from] - if inner == nil { - if delta <= 0 { - return - } - inner = map[string]int{} - m[from] = inner - } - inner[to] += delta - if inner[to] <= 0 { - delete(inner, to) - if len(inner) == 0 { - delete(m, from) - } - } -} - -func (g *depGraph) add(key depEdgeKey, depType types.DependencyType) { - g.rows[key] = append(g.rows[key], depType) - g.adjust(key, depType, 1) -} - -func (g *depGraph) adjust(key depEdgeKey, depType types.DependencyType, delta int) { - if key.target == "" { - return - } - if types.IsSchedulingEdge(depType) { - bump(g.out.sched, key.source, key.target, delta) - bump(g.in.sched, key.target, key.source, delta) - } - if depType == types.DepParentChild { - bump(g.out.parents, key.source, key.target, delta) - bump(g.in.parents, key.target, key.source, delta) - } -} - -// reload replaces the graph's view of one (table, source, target) pair with -// the rows the table holds now. -// -//nolint:gosec // G201: key.table is one of the fixed dependency tables. -func (g *depGraph) reload(ctx context.Context, tx DBTX, key depEdgeKey) error { - for _, t := range g.rows[key] { - g.adjust(key, t, -1) - } - delete(g.rows, key) - rows, err := tx.QueryContext(ctx, fmt.Sprintf( - "SELECT type FROM %s WHERE issue_id = ? AND %s = ?", key.table, DepTargetExpr), key.source, key.target) - if err != nil { - return fmt.Errorf("reload dependency %s -> %s: %w", key.source, key.target, err) - } - defer func() { _ = rows.Close() }() - for rows.Next() { - var depType string - if err := rows.Scan(&depType); err != nil { - return fmt.Errorf("reload dependency %s -> %s: %w", key.source, key.target, err) - } - g.add(key, types.DependencyType(depType)) - } - return rows.Err() -} - -// reaches reports whether goal is reachable from start (start itself -// included, as in the recursive CTEs' anchor row), walking scheduling edges, -// or parent-child edges only when parentsOnly is set. -// -// It searches from both ends — forward from start along outgoing edges, -// backward from goal along incoming ones — always expanding the smaller -// frontier one level, after loading that level's unloaded nodes in that -// direction in one read; the two meet exactly when a path exists. Either -// frontier running dry proves there is none. -func (g *depGraph) reaches(ctx context.Context, tx DBTX, start, goal string, parentsOnly bool) (bool, error) { - if start == goal { - return true, nil - } - seenFwd := map[string]bool{start: true} - seenBwd := map[string]bool{goal: true} - fwd, bwd := []string{start}, []string{goal} - for len(fwd) > 0 && len(bwd) > 0 { - backward := len(bwd) < len(fwd) - frontier, seen, other, adj := fwd, seenFwd, seenBwd, g.out - if backward { - frontier, seen, other, adj = bwd, seenBwd, seenFwd, g.in - } - if err := g.load(ctx, tx, frontier, backward); err != nil { - return false, err - } - edges := adj.edges(parentsOnly) - var next []string - for _, node := range frontier { - for to := range edges[node] { - if other[to] { - return true, nil - } - if !seen[to] { - seen[to] = true - next = append(next, to) - } - } - } - if backward { - bwd = next - } else { - fwd = next - } - } - return false, nil -} diff --git a/internal/storage/issueops/create_fastpath_testhook.go b/internal/storage/issueops/create_fastpath_testhook.go deleted file mode 100644 index 06fe8e424..000000000 --- a/internal/storage/issueops/create_fastpath_testhook.go +++ /dev/null @@ -1,25 +0,0 @@ -package issueops - -import "sync/atomic" - -// createFastPathsDisabled turns off the batch-create fast paths — the -// createBatchCache, the dependency pass's batch lookups and in-memory graph, -// and the blocked-state recompute's no-edge shortcut — so a test can run the -// same batch through both the fast and the per-row bodies on a real engine -// and compare the stored outcome. Production never sets it. -var createFastPathsDisabled atomic.Bool - -// DisableCreateFastPathsForTest switches the batch-create fast paths off for -// the whole process until the returned restore runs. It exists only for the -// fast-vs-per-row equivalence tests (internal/storage/createbatchequiv). -// -// It is process-global, and it also turns off the blocked-state no-edge -// shortcut for every recompute in the process. A test that calls it must not -// use t.Parallel (a sequential top-level test never overlaps parallel ones), -// and two callers must not overlap: a second call while one is active panics. -func DisableCreateFastPathsForTest() (restore func()) { - if !createFastPathsDisabled.CompareAndSwap(false, true) { - panic("issueops.DisableCreateFastPathsForTest: already disabled by an overlapping caller") - } - return func() { createFastPathsDisabled.Store(false) } -} diff --git a/internal/storage/issueops/create_test.go b/internal/storage/issueops/create_test.go index 5cd2f8b89..997781a5f 100644 --- a/internal/storage/issueops/create_test.go +++ b/internal/storage/issueops/create_test.go @@ -3,17 +3,13 @@ package issueops import ( "context" "database/sql" - "database/sql/driver" "errors" - "fmt" - "regexp" "strings" "testing" "github.com/DATA-DOG/go-sqlmock" "github.com/steveyegge/beads/internal/storage" "github.com/steveyegge/beads/internal/storage/depid" - "github.com/steveyegge/beads/internal/storage/domain" "github.com/steveyegge/beads/internal/types" ) @@ -115,6 +111,46 @@ func TestValidateCreateIssuesMixedBucketDependenciesRejectsCrossBucketEdges(t *t } } +func TestFilterCreateIssuesMixedBucketDependenciesSkipsWhenConfigured(t *testing.T) { + regular := &types.Issue{ + ID: "test-regular-source", + IssueType: types.TypeTask, + Dependencies: []*types.Dependency{{ + DependsOnID: "test-wisp-target", + Type: types.DepBlocks, + }}, + } + wisp := &types.Issue{ + ID: "test-wisp-target", + IssueType: types.TypeTask, + Ephemeral: true, + } + var skipped []string + + filtered, err := filterCreateIssuesMixedBucketDependencies([]*types.Issue{regular, wisp}, storage.BatchCreateOptions{ + SkipDependencyValidationErrors: true, + OnSkippedDependency: func(issueID, dependsOnID, reason string) { + skipped = append(skipped, issueID+" -> "+dependsOnID+": "+reason) + }, + }) + if err != nil { + t.Fatalf("filterCreateIssuesMixedBucketDependencies error = %v, want nil", err) + } + if len(filtered) != 2 { + t.Fatalf("len(filtered) = %d, want 2", len(filtered)) + } + if len(filtered[0].Dependencies) != 0 { + t.Fatalf("filtered[0].Dependencies = %#v, want none", filtered[0].Dependencies) + } + if len(regular.Dependencies) != 1 { + t.Fatalf("regular.Dependencies was mutated to %#v, want original dependency preserved", regular.Dependencies) + } + if len(skipped) != 1 || !strings.Contains(skipped[0], "test-regular-source -> test-wisp-target") || + !strings.Contains(skipped[0], "cross-bucket dependency") { + t.Fatalf("skipped = %#v, want cross-bucket dependency detail", skipped) + } +} + func TestPersistDependenciesHonorsImportedCreatedBy(t *testing.T) { ctx := context.Background() db, mock, tx := beginMockTx(t) @@ -362,9 +398,6 @@ func TestPersistDependenciesRejectsHierarchyBlocking(t *testing.T) { } func TestPersistDependenciesValidatesPlannedHierarchyBeforeBlocking(t *testing.T) { - // This case pins the per-edge read sequence; its batch-lookup twin is - // TestPersistDependenciesBatchValidatesPlannedHierarchyBeforeBlocking. - t.Cleanup(DisableCreateFastPathsForTest()) ctx := context.Background() db, mock, tx := beginMockTx(t) defer db.Close() @@ -558,399 +591,3 @@ func TestReconcileChildCountersReturnsWispLookupError(t *testing.T) { t.Fatalf("unmet expectations: %v", err) } } - -// TestPersistDependenciesBatchValidatesPlannedHierarchyBeforeBlocking is the -// batch-lookup twin of TestPersistDependenciesValidatesPlannedHierarchyBeforeBlocking: -// routing and target presence come from one batch read each, edges are read -// only for the batch's own endpoints, and the planned ancestry the -// parent-child phase wrote is what rejects the blocks edge — with no per-edge -// recursive query. -func TestPersistDependenciesBatchValidatesPlannedHierarchyBeforeBlocking(t *testing.T) { - ctx := context.Background() - db, mock, tx := beginMockTx(t) - defer db.Close() - child := &types.Issue{ - ID: "bd-child", - IssueType: types.TypeTask, - Dependencies: []*types.Dependency{ - {DependsOnID: "bd-grand", Type: types.DepBlocks}, // Deliberately first. - {DependsOnID: "bd-parent", Type: types.DepParentChild}, - }, - } - parent := &types.Issue{ - ID: "bd-parent", - IssueType: types.TypeTask, - Dependencies: []*types.Dependency{{ - DependsOnID: "bd-grand", - Type: types.DepParentChild, - }}, - } - - mock.ExpectQuery(regexp.QuoteMeta("SELECT 1 FROM wisps LIMIT 1")).WillReturnError(sql.ErrNoRows) - mock.ExpectQuery(regexp.QuoteMeta("SELECT id FROM issues WHERE id IN (?,?)")). - WithArgs("bd-grand", "bd-parent"). - WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow("bd-grand").AddRow("bd-parent")) - ids := []driver.Value{"bd-child", "bd-grand", "bd-parent"} - for _, table := range []string{"dependencies", "wisp_dependencies"} { - mock.ExpectQuery(regexp.QuoteMeta("SELECT issue_id, " + DepTargetExpr + ", type FROM " + table + " WHERE issue_id IN (?,?,?)")). - WithArgs(ids...). - WillReturnRows(sqlmock.NewRows([]string{"issue_id", "target", "type"})) - } - for _, table := range []string{"dependencies", "wisp_dependencies"} { - for _, col := range []string{"depends_on_issue_id", "depends_on_wisp_id", "depends_on_external"} { - mock.ExpectQuery(regexp.QuoteMeta("SELECT issue_id, " + DepTargetExpr + ", type FROM " + table + " WHERE " + col + " IN (?,?,?)")). - WithArgs(ids...). - WillReturnRows(sqlmock.NewRows([]string{"issue_id", "target", "type"})) - } - } - for _, pair := range [][2]string{{"bd-child", "bd-parent"}, {"bd-parent", "bd-grand"}} { - mock.ExpectExec("INSERT INTO dependencies"). - WithArgs(depid.New(pair[0], pair[1]), pair[0], pair[1], types.DepParentChild, "tester", sqlmock.AnyArg(), "{}", ""). - WillReturnResult(sqlmock.NewResult(0, 1)) - mock.ExpectExec("REPLACE INTO local_metadata"). - WithArgs(dependencyCoordinationKey(pair[1], dependencyCoordinationDurableTier), sqlmock.AnyArg()). - WillReturnResult(sqlmock.NewResult(1, 1)) - } - - _, err := PersistDependenciesWithOptionsResult(ctx, tx, []*types.Issue{child, parent}, "tester", storage.BatchCreateOptions{}) - if err == nil || !strings.Contains(err.Error(), "cannot be blocked by its ancestor") { - t.Fatalf("error = %v, want planned-ancestor rejection", err) - } - - mock.ExpectRollback() - if err := tx.Rollback(); err != nil { - t.Fatalf("rollback: %v", err) - } - if err := mock.ExpectationsWereMet(); err != nil { - t.Fatalf("unmet expectations: %v", err) - } -} - -// A failed walk query is not a validation refusal: under the import mode -// (SkipDependencyValidationErrors) the error must abort the pass rather than be -// recorded as a skipped edge. This covers both checks on the per-edge -// statements (a one-edge batch); the batch graph's twin is -// TestPersistDependenciesBatchTolerantReturnsValidationQueryErrors. -func TestPersistDependenciesTolerantReturnsValidationQueryErrors(t *testing.T) { - walkErr := errors.New("pq: aborted walk (simulated)") - cases := []struct { - name string - depType types.DependencyType - walk string - args []driver.Value - wantMsg string - }{ - {"hierarchy", types.DepBlocks, "WITH RECURSIVE ancestors", []driver.Value{"bd-child", "bd-parent"}, "failed to check blocker ancestry"}, - // parent-child skips the hierarchy check, so its first walk is the cycle check. - {"cycle", types.DepParentChild, "WITH RECURSIVE reachable", []driver.Value{"bd-parent", "bd-child"}, "failed to check for dependency cycle"}, - } - for _, tc := range cases { - t.Run(tc.name, func(t *testing.T) { - ctx := context.Background() - db, mock, tx := beginMockTx(t) - defer db.Close() - issue := &types.Issue{ - ID: "bd-child", - IssueType: types.TypeTask, - Dependencies: []*types.Dependency{{ - DependsOnID: "bd-parent", - Type: tc.depType, - }}, - } - mock.ExpectQuery("SELECT 1 FROM wisps WHERE id = \\? LIMIT 1"). - WithArgs("bd-parent"). - WillReturnError(sql.ErrNoRows) - mock.ExpectQuery("SELECT 1 FROM issues WHERE id = \\?"). - WithArgs("bd-parent"). - WillReturnRows(sqlmock.NewRows([]string{"1"}).AddRow(1)) - mock.ExpectQuery(tc.walk).WithArgs(tc.args...).WillReturnError(walkErr) - - var skipped []string - _, err := PersistDependenciesWithOptionsResult(ctx, tx, []*types.Issue{issue}, "tester", storage.BatchCreateOptions{ - SkipDependencyValidationErrors: true, - OnSkippedDependency: func(issueID, dependsOnID, reason string) { - skipped = append(skipped, issueID+" -> "+dependsOnID+": "+reason) - }, - }) - if !errors.Is(err, walkErr) || !strings.Contains(err.Error(), tc.wantMsg) { - t.Fatalf("error = %v, want the walk error (%q)", err, tc.wantMsg) - } - if len(skipped) != 0 { - t.Fatalf("skipped = %#v, want none: a query error is not a validation refusal", skipped) - } - - mock.ExpectRollback() - if err := tx.Rollback(); err != nil { - t.Fatalf("rollback: %v", err) - } - if err := mock.ExpectationsWereMet(); err != nil { - t.Fatalf("unmet expectations: %v", err) - } - }) - } -} - -func TestIsDependencyValidationRefusal(t *testing.T) { - cases := []struct { - name string - err error - want bool - }{ - {"cycle", domain.ErrDependencyCycle, true}, - {"wrapped self", fmt.Errorf("%w: x cannot depend on itself", domain.ErrSelfDependency), true}, - {"hierarchy", &domain.DependencyHierarchyConflictError{IssueID: "a", BlockerID: "b"}, true}, - {"wrapped hierarchy", fmt.Errorf("edge: %w", &domain.DependencyHierarchyConflictError{IssueID: "a", BlockerID: "b", BlockerIsAncestor: true}), true}, - {"ancestry query", fmt.Errorf("failed to check blocker ancestry: %w", errors.New("42P19")), false}, - {"cycle query", fmt.Errorf("failed to check for dependency cycle: %w", sql.ErrConnDone), false}, - {"graph read", fmt.Errorf("failed to check for dependency cycle: load dependency edges from dependencies: %w", context.DeadlineExceeded), false}, - } - for _, tc := range cases { - if got := isDependencyValidationRefusal(tc.err); got != tc.want { - t.Errorf("%s: isDependencyValidationRefusal(%v) = %v, want %v", tc.name, tc.err, got, tc.want) - } - } -} - -// TestPersistDependenciesBatchTolerantReturnsValidationQueryErrors is the -// batch-lookup twin of TestPersistDependenciesTolerantReturnsValidationQueryErrors. -// With two scheduling edges the walks run on depGraph, which reads a node's -// edges the first time a search reaches it. A stored edge leads each walk off -// the batch's endpoints, and that read fails. The failure must abort the pass: -// load has already marked the node as read, so a pass that carried on would -// answer every later walk through that node from an empty edge set. -func TestPersistDependenciesBatchTolerantReturnsValidationQueryErrors(t *testing.T) { - walkErr := errors.New("pq: aborted walk (simulated)") - cases := []struct { - name string - stored []driver.Value // issue_id, target, type: the edge the walk follows off the endpoints - wantMsg string - }{ - // bd-a's stored parent: the ancestry walk from bd-a reads bd-p's edges. - {"hierarchy", []driver.Value{"bd-a", "bd-p", string(types.DepParentChild)}, "failed to check blocker ancestry"}, - // bd-b's stored blocker: the cycle walk from bd-b reads bd-x's edges. - {"cycle", []driver.Value{"bd-b", "bd-x", string(types.DepBlocks)}, "failed to check for dependency cycle"}, - } - for _, tc := range cases { - t.Run(tc.name, func(t *testing.T) { - ctx := context.Background() - db, mock, tx := beginMockTx(t) - defer db.Close() - issue := &types.Issue{ - ID: "bd-a", - IssueType: types.TypeTask, - Dependencies: []*types.Dependency{ - {DependsOnID: "bd-b", Type: types.DepBlocks}, - {DependsOnID: "bd-c", Type: types.DepBlocks}, - }, - } - expectDepBatchReads(mock, []driver.Value{"bd-b", "bd-c"}, []driver.Value{"bd-a", "bd-b", "bd-c"}, tc.stored) - mock.ExpectQuery(regexp.QuoteMeta("SELECT issue_id, " + DepTargetExpr + ", type FROM dependencies WHERE issue_id IN (?)")). - WithArgs(tc.stored[1]). - WillReturnError(walkErr) - - var skipped []string - _, err := PersistDependenciesWithOptionsResult(ctx, tx, []*types.Issue{issue}, "tester", storage.BatchCreateOptions{ - SkipDependencyValidationErrors: true, - OnSkippedDependency: func(issueID, dependsOnID, reason string) { - skipped = append(skipped, issueID+" -> "+dependsOnID+": "+reason) - }, - }) - if !errors.Is(err, walkErr) || !strings.Contains(err.Error(), tc.wantMsg) { - t.Fatalf("error = %v, want the walk error (%q)", err, tc.wantMsg) - } - if len(skipped) != 0 { - t.Fatalf("skipped = %#v, want none: a query error is not a validation refusal", skipped) - } - - mock.ExpectRollback() - if err := tx.Rollback(); err != nil { - t.Fatalf("rollback: %v", err) - } - if err := mock.ExpectationsWereMet(); err != nil { - t.Fatalf("unmet expectations: %v", err) - } - }) - } -} - -// The import mode still tolerates every typed refusal the walks raise, on both -// lookup shapes: the edge is reported as skipped and the pass goes on. This -// pins the other side of isDependencyValidationRefusal. The self-dependency -// case is TestPersistDependenciesSkipsValidationErrorsWhenConfigured. -func TestPersistDependenciesTolerantSkipsTypedRefusals(t *testing.T) { - tolerant := func(skipped *[]string) storage.BatchCreateOptions { - return storage.BatchCreateOptions{ - SkipDependencyValidationErrors: true, - OnSkippedDependency: func(issueID, dependsOnID, reason string) { - *skipped = append(*skipped, issueID+" -> "+dependsOnID+": "+reason) - }, - } - } - finish := func(t *testing.T, mock sqlmock.Sqlmock, tx *sql.Tx) { - t.Helper() - mock.ExpectRollback() - if err := tx.Rollback(); err != nil { - t.Fatalf("rollback: %v", err) - } - if err := mock.ExpectationsWereMet(); err != nil { - t.Fatalf("unmet expectations: %v", err) - } - } - - type walk struct { - query string - args []driver.Value - count int - } - ancestors := func(node, candidate string, count int) walk { - return walk{"WITH RECURSIVE ancestors", []driver.Value{node, candidate}, count} - } - // The per-edge statements: one blocks edge bd-a -> bd-b. - for _, tc := range []struct { - name string - walks []walk - want string - }{ - {"ancestor", []walk{ancestors("bd-a", "bd-b", 1)}, "bd-a cannot be blocked by its ancestor bd-b"}, - {"descendant", []walk{ancestors("bd-a", "bd-b", 0), ancestors("bd-b", "bd-a", 1)}, "bd-a cannot be blocked by its descendant bd-b"}, - {"cycle", []walk{ - ancestors("bd-a", "bd-b", 0), ancestors("bd-b", "bd-a", 0), - {"WITH RECURSIVE reachable", []driver.Value{"bd-b", "bd-a"}, 1}, - }, domain.ErrDependencyCycle.Error()}, - } { - t.Run(tc.name, func(t *testing.T) { - ctx := context.Background() - db, mock, tx := beginMockTx(t) - defer db.Close() - issue := &types.Issue{ - ID: "bd-a", - IssueType: types.TypeTask, - Dependencies: []*types.Dependency{{DependsOnID: "bd-b", Type: types.DepBlocks}}, - } - mock.ExpectQuery("SELECT 1 FROM wisps WHERE id = \\? LIMIT 1"). - WithArgs("bd-b"). - WillReturnError(sql.ErrNoRows) - mock.ExpectQuery("SELECT 1 FROM issues WHERE id = \\?"). - WithArgs("bd-b"). - WillReturnRows(sqlmock.NewRows([]string{"1"}).AddRow(1)) - for _, w := range tc.walks { - mock.ExpectQuery(w.query).WithArgs(w.args...). - WillReturnRows(sqlmock.NewRows([]string{"count"}).AddRow(w.count)) - } - - var skipped []string - if _, err := PersistDependenciesWithOptionsResult(ctx, tx, []*types.Issue{issue}, "tester", tolerant(&skipped)); err != nil { - t.Fatalf("error = %v, want the refusal skipped", err) - } - if len(skipped) != 1 || !strings.HasPrefix(skipped[0], "bd-a -> bd-b: "+tc.want) { - t.Fatalf("skipped = %#v, want bd-a -> bd-b: %s", skipped, tc.want) - } - finish(t, mock, tx) - }) - } - - // The batch graph: the planned ancestry of - // TestPersistDependenciesBatchValidatesPlannedHierarchyBeforeBlocking, whose - // blocks edge is skipped here instead of failing the batch. - t.Run("batch ancestor", func(t *testing.T) { - ctx := context.Background() - db, mock, tx := beginMockTx(t) - defer db.Close() - child := &types.Issue{ - ID: "bd-child", - IssueType: types.TypeTask, - Dependencies: []*types.Dependency{ - {DependsOnID: "bd-grand", Type: types.DepBlocks}, - {DependsOnID: "bd-parent", Type: types.DepParentChild}, - }, - } - parent := &types.Issue{ - ID: "bd-parent", - IssueType: types.TypeTask, - Dependencies: []*types.Dependency{{DependsOnID: "bd-grand", Type: types.DepParentChild}}, - } - expectDepBatchReads(mock, []driver.Value{"bd-grand", "bd-parent"}, []driver.Value{"bd-child", "bd-grand", "bd-parent"}) - for _, pair := range [][2]string{{"bd-child", "bd-parent"}, {"bd-parent", "bd-grand"}} { - mock.ExpectExec("INSERT INTO dependencies"). - WithArgs(depid.New(pair[0], pair[1]), pair[0], pair[1], types.DepParentChild, "tester", sqlmock.AnyArg(), "{}", ""). - WillReturnResult(sqlmock.NewResult(0, 1)) - mock.ExpectExec("REPLACE INTO local_metadata"). - WithArgs(dependencyCoordinationKey(pair[1], dependencyCoordinationDurableTier), sqlmock.AnyArg()). - WillReturnResult(sqlmock.NewResult(1, 1)) - } - - var skipped []string - if _, err := PersistDependenciesWithOptionsResult(ctx, tx, []*types.Issue{child, parent}, "tester", tolerant(&skipped)); err != nil { - t.Fatalf("error = %v, want the refusal skipped", err) - } - if len(skipped) != 1 || !strings.HasPrefix(skipped[0], "bd-child -> bd-grand: bd-child cannot be blocked by its ancestor bd-grand") { - t.Fatalf("skipped = %#v, want the planned-ancestor refusal of bd-child -> bd-grand", skipped) - } - finish(t, mock, tx) - }) - - // The batch graph: an in-batch cycle. The first edge lands, and the graph - // that now holds it refuses the second. - t.Run("batch cycle", func(t *testing.T) { - ctx := context.Background() - db, mock, tx := beginMockTx(t) - defer db.Close() - a := &types.Issue{ - ID: "bd-a", - IssueType: types.TypeTask, - Dependencies: []*types.Dependency{{DependsOnID: "bd-b", Type: types.DepBlocks}}, - } - b := &types.Issue{ - ID: "bd-b", - IssueType: types.TypeTask, - Dependencies: []*types.Dependency{{DependsOnID: "bd-a", Type: types.DepBlocks}}, - } - expectDepBatchReads(mock, []driver.Value{"bd-b", "bd-a"}, []driver.Value{"bd-a", "bd-b"}) - mock.ExpectExec("INSERT INTO dependencies"). - WithArgs(depid.New("bd-a", "bd-b"), "bd-a", "bd-b", types.DepBlocks, "tester", sqlmock.AnyArg(), "{}", ""). - WillReturnResult(sqlmock.NewResult(0, 1)) - - var skipped []string - if _, err := PersistDependenciesWithOptionsResult(ctx, tx, []*types.Issue{a, b}, "tester", tolerant(&skipped)); err != nil { - t.Fatalf("error = %v, want the refusal skipped", err) - } - if want := "bd-b -> bd-a: " + domain.ErrDependencyCycle.Error(); len(skipped) != 1 || skipped[0] != want { - t.Fatalf("skipped = %#v, want [%q]", skipped, want) - } - finish(t, mock, tx) - }) -} - -// expectDepBatchReads expects newDepBatchLookups' reads for a batch with no -// wisps: the wisps probe, the presence of issueTargets in issues, and the edges -// of ids out of (issue_id) and into (each target column) both edge tables. -// The outgoing read of dependencies returns the stored rows. -func expectDepBatchReads(mock sqlmock.Sqlmock, issueTargets, ids []driver.Value, stored ...[]driver.Value) { - in := func(n int) string { return strings.TrimSuffix(strings.Repeat("?,", n), ",") } - mock.ExpectQuery(regexp.QuoteMeta("SELECT 1 FROM wisps LIMIT 1")).WillReturnError(sql.ErrNoRows) - present := sqlmock.NewRows([]string{"id"}) - for _, id := range issueTargets { - present.AddRow(id) - } - mock.ExpectQuery(regexp.QuoteMeta("SELECT id FROM issues WHERE id IN (" + in(len(issueTargets)) + ")")). - WithArgs(issueTargets...). - WillReturnRows(present) - for _, table := range []string{"dependencies", "wisp_dependencies"} { - rows := sqlmock.NewRows([]string{"issue_id", "target", "type"}) - if table == "dependencies" { - for _, row := range stored { - rows.AddRow(row...) - } - } - mock.ExpectQuery(regexp.QuoteMeta("SELECT issue_id, " + DepTargetExpr + ", type FROM " + table + " WHERE issue_id IN (" + in(len(ids)) + ")")). - WithArgs(ids...). - WillReturnRows(rows) - } - for _, table := range []string{"dependencies", "wisp_dependencies"} { - for _, col := range []string{"depends_on_issue_id", "depends_on_wisp_id", "depends_on_external"} { - mock.ExpectQuery(regexp.QuoteMeta("SELECT issue_id, " + DepTargetExpr + ", type FROM " + table + " WHERE " + col + " IN (" + in(len(ids)) + ")")). - WithArgs(ids...). - WillReturnRows(sqlmock.NewRows([]string{"issue_id", "target", "type"})) - } - } -} diff --git a/internal/storage/issueops/cycle_report.go b/internal/storage/issueops/cycle_report.go index 27b78faf8..cc772173c 100644 --- a/internal/storage/issueops/cycle_report.go +++ b/internal/storage/issueops/cycle_report.go @@ -2,7 +2,6 @@ package issueops import ( "context" - "fmt" "slices" "github.com/steveyegge/beads/internal/types" @@ -12,9 +11,8 @@ import ( // The cycle REPORT: the shared body behind issueops.CycleDetector on all three // backends, split into a pure half and a transactional half so the part that // decides what the answer MEANS is testable without a database. -// CanonicalCyclePaths holds the determinism ruling, CanonicalMixedCyclePaths -// the IncludeTracks widening, and BuildCycles the honest-partial one; all -// three are pinned in cycle_report_test.go. +// CanonicalCyclePaths holds the determinism ruling and BuildCycles the +// honest-partial one; both are pinned in cycle_report_test.go. // CanonicalCyclePaths returns the cycles of a blocking graph as id paths, in a // canonical form: each path rotated so its lowest id comes first, and the paths @@ -82,248 +80,6 @@ func CanonicalCyclePaths(graph map[string][]string) [][]string { return cycles } -// CanonicalMixedCyclePaths reports the qualifying cycles of a graph that also -// carries `tracks` edges (MixedCycleEdge.Scheduling false). A cycle qualifies -// only when at least one of its edges is a scheduling edge. -// -// THE REPORT IS THE UNION, deduplicated by canonical rotation, of two sets: -// -// 1. CanonicalCyclePaths over the scheduling edges alone, which is exactly -// the report the default request produces on the same data. -// 2. For every scheduling edge from -> to whose endpoints share a strongly -// connected component of the mixed graph, the cycle that edge closes with -// the shortest return path from `to` back to `from` over edges of either -// kind (closeMixedCycle). -// -// Set 1 makes the widened report a SUPERSET of the default report. Set 2 alone -// would not: the default walk records the depth-first tree path behind each -// back edge, which need not be the shortest return path of any edge on it. And -// sampling one cycle per component instead would drop real deadlocks, because -// tracks edges can fuse separate blocks-only deadlocks into one component. -// -// Set 2 finds the cycles that need a tracks edge, the shape this exists for: a -// molecule root that blocks-depends (transitively) on its own entry step, -// which tracks-depends back to the root. Every internal edge of a strongly -// connected component lies on a cycle, so each such scheduling edge closes one, -// and a scheduling edge outside every component lies on no cycle at all. -// -// BOUND: at most one cycle per scheduling edge inside a component plus one per -// default-walk cycle, so never more than twice the distinct scheduling edges. -// That is the whole of the volume guarantee, and it counts EDGES, not -// deadlocks. A cycle made only of tracks edges is never reported, which rules -// out the pure-tracks shape AppendMixedCycleGraphInTx documents and nothing -// beyond it: one tracks-only loop fuses its whole neighborhood into a single -// component, after which every scheduling edge inside is on a cycle and is -// reported, however acyclic the scheduling edges are on their own. So the -// exclusion does not keep the report small on a tracks-dense graph — the edge -// count does, which is why this walk is opt-in and why `bd dep cycles` -// truncates its rendering. -// -// COST: one breadth-first search per distinct scheduling-edge target inside a -// component, confined to that component, and shared by every edge into that -// target. On a component of V nodes and E edges that is O(V(V+E)). A search per -// edge was O(E(V+E)), and on a dense tangle E grows with the square of V. -// -// Nodes, adjacency lists, components, and return paths are all ordered, so the -// answer depends only on the graph, not on Go map or SQL row order. The error -// is a broken internal invariant (see closeMixedCycle), never a property of -// the data. -func CanonicalMixedCyclePaths(graph map[string][]MixedCycleEdge) ([][]string, error) { - adjacency := make(map[string][]MixedCycleEdge, len(graph)) - nodeSet := make(map[string]struct{}, len(graph)) - for node, edges := range graph { - nodeSet[node] = struct{}{} - scheduling := make(map[string]bool, len(edges)) - for _, edge := range edges { - nodeSet[edge.To] = struct{}{} - scheduling[edge.To] = scheduling[edge.To] || edge.Scheduling - } - targets := make([]string, 0, len(scheduling)) - for to := range scheduling { - targets = append(targets, to) - } - slices.Sort(targets) - deduped := make([]MixedCycleEdge, 0, len(targets)) - for _, to := range targets { - deduped = append(deduped, MixedCycleEdge{To: to, Scheduling: scheduling[to]}) - } - adjacency[node] = deduped - } - nodes := make([]string, 0, len(nodeSet)) - plain := make(map[string][]string, len(nodeSet)) - schedulingOnly := make(map[string][]string, len(nodeSet)) - for node := range nodeSet { - nodes = append(nodes, node) - for _, edge := range adjacency[node] { - plain[node] = append(plain[node], edge.To) - if edge.Scheduling { - schedulingOnly[node] = append(schedulingOnly[node], edge.To) - } - } - } - slices.Sort(nodes) - - cycles := CanonicalCyclePaths(schedulingOnly) - for _, component := range mixedStronglyConnectedComponents(adjacency, nodes) { - closed, err := closeComponentSchedulingEdges(adjacency, plain, component) - if err != nil { - return nil, err - } - cycles = append(cycles, closed...) - } - slices.SortFunc(cycles, slices.Compare) - return slices.CompactFunc(cycles, slices.Equal), nil -} - -// closeComponentSchedulingEdges closes every scheduling edge inside one strongly -// connected component, which is set 2 of CanonicalMixedCyclePaths. -// -// The edges are grouped by target, so one shortestPathTree rooted at a target -// closes every edge into it. Only one tree is held at a time. -func closeComponentSchedulingEdges(adjacency map[string][]MixedCycleEdge, plain map[string][]string, component []string) ([][]string, error) { - members := make(map[string]bool, len(component)) - for _, node := range component { - members[node] = true - } - sources := make(map[string][]string, len(component)) - for _, from := range component { - for _, edge := range adjacency[from] { - if edge.Scheduling && members[edge.To] { - sources[edge.To] = append(sources[edge.To], from) - } - } - } - - var cycles [][]string - for _, to := range component { - if len(sources[to]) == 0 { - continue - } - tree := shortestPathTree(plain, members, to) - for _, from := range sources[to] { - cycle, err := closeMixedCycle(tree, from, to) - if err != nil { - return nil, err - } - cycles = append(cycles, cycle) - } - } - return cycles, nil -} - -// shortestPathTree is the breadth-first tree over graph from start, confined to -// members: tree[n] is the node before n on a shortest path from start, and -// tree[start] is start. Neighbors are taken in their stored order, so the path -// to any node is the one reachPath returns for the same start and goal. The -// search runs past the first goal so that every goal can share it. -// -// Confining the search to a strongly connected component loses no path between -// two of its members. A node that start reaches, and that reaches back into the -// component, is itself in the component, so a search that leaves the component -// never comes back to it. -func shortestPathTree(graph map[string][]string, members map[string]bool, start string) map[string]string { - tree := map[string]string{start: start} - queue := []string{start} - for len(queue) > 0 { - node := queue[0] - queue = queue[1:] - for _, next := range graph[node] { - if _, seen := tree[next]; seen || !members[next] { - continue - } - tree[next] = node - queue = append(queue, next) - } - } - return tree -} - -// closeMixedCycle returns the canonical cycle the edge from -> to closes: from, -// followed by the shortest path from `to` back to `from`, read out of tree, the -// shortestPathTree rooted at `to`. -// -// The caller passes only edges inside one strongly connected component, with -// the tree rooted at the edge's target, so the tree always reaches `from`. When -// it does not, or the climb ends at a different root, the invariant is broken, -// and that is returned as an error rather than a panic: the caller is a read -// that `bd dep cycles --include-tracks` runs, and a crash there would hide every -// other cycle behind a stack trace. -func closeMixedCycle(tree map[string]string, from, to string) ([]string, error) { - // Climbing from `from` to the root gives the return path backwards. - backwards := []string{from} - for at := from; at != to; { - parent, reached := tree[at] - if !reached || parent == at { - return nil, fmt.Errorf("mixed cycle graph: edge %s -> %s lies in a strongly connected component but has no return path", from, to) - } - at = parent - backwards = append(backwards, at) - } - cycle := make([]string, 0, len(backwards)) - cycle = append(cycle, from) - for i := len(backwards) - 1; i > 0; i-- { - cycle = append(cycle, backwards[i]) - } - return rotateToLowest(cycle), nil -} - -// mixedStronglyConnectedComponents returns Tarjan components with both their -// members and the component list sorted. Targets without outgoing edges are in -// nodes too, so singleton sinks remain part of the traversal even though they -// can never qualify on their own. -func mixedStronglyConnectedComponents(adjacency map[string][]MixedCycleEdge, nodes []string) [][]string { - nextIndex := 0 - index := make(map[string]int, len(nodes)) - lowlink := make(map[string]int, len(nodes)) - onStack := make(map[string]bool, len(nodes)) - stack := make([]string, 0, len(nodes)) - components := make([][]string, 0) - - var visit func(string) - visit = func(node string) { - nextIndex++ - index[node] = nextIndex - lowlink[node] = nextIndex - stack = append(stack, node) - onStack[node] = true - - for _, edge := range adjacency[node] { - neighbor := edge.To - if index[neighbor] == 0 { - visit(neighbor) - lowlink[node] = min(lowlink[node], lowlink[neighbor]) - } else if onStack[neighbor] { - lowlink[node] = min(lowlink[node], index[neighbor]) - } - } - - if lowlink[node] != index[node] { - return - } - component := make([]string, 0) - for { - last := len(stack) - 1 - member := stack[last] - stack = stack[:last] - onStack[member] = false - component = append(component, member) - if member == node { - break - } - } - slices.Sort(component) - components = append(components, component) - } - - for _, node := range nodes { - if index[node] == 0 { - visit(node) - } - } - slices.SortFunc(components, slices.Compare) - return components -} - // rotateToLowest returns a copy of a cycle path rotated so its lowest id comes // first, preserving edge order. The members of a cycle are distinct — the path // it is taken from is a simple path — so the lowest id is unique and names @@ -377,33 +133,19 @@ func BuildCycles(paths [][]string, hydrate func(id string) *types.Issue) []publi // DetectCycleReportInTx is the whole read: build the blocking graph across both // planes, canonicalize it, and hydrate what it can. It reads the same two tables -// and, by default, the same two edge types DetectCyclesInTx reads, because it -// is the same question. req.IncludeTracks answers a wider question instead — -// see DetectCyclesRequest and CanonicalMixedCyclePaths. -func DetectCycleReportInTx(ctx context.Context, tx DBTX, req publicops.DetectCyclesRequest) (publicops.CycleReport, error) { +// and the same two edge types DetectCyclesInTx reads, because it is the same +// question. +func DetectCycleReportInTx(ctx context.Context, tx DBTX) (publicops.CycleReport, error) { + graph := make(map[string][]string) + if err := AppendBlockingGraphInTx(ctx, tx, cycleDetectionTables(), graph); err != nil { + return publicops.CycleReport{}, err + } hydrate := func(id string) *types.Issue { // The error is deliberately not distinguished from a miss: both mean the // same thing to the answer, that this database did not describe the node. issue, _ := GetIssueInTx(ctx, tx, id) return issue } - - if req.IncludeTracks { - graph := make(map[string][]MixedCycleEdge) - if err := AppendMixedCycleGraphInTx(ctx, tx, cycleDetectionTables(), graph); err != nil { - return publicops.CycleReport{}, err - } - paths, err := CanonicalMixedCyclePaths(graph) - if err != nil { - return publicops.CycleReport{}, err - } - return publicops.CycleReport{Cycles: BuildCycles(paths, hydrate)}, nil - } - - graph := make(map[string][]string) - if err := AppendBlockingGraphInTx(ctx, tx, cycleDetectionTables(), graph); err != nil { - return publicops.CycleReport{}, err - } return publicops.CycleReport{ Cycles: BuildCycles(CanonicalCyclePaths(graph), hydrate), }, nil diff --git a/internal/storage/issueops/cycle_report_test.go b/internal/storage/issueops/cycle_report_test.go index a393d10a0..985418558 100644 --- a/internal/storage/issueops/cycle_report_test.go +++ b/internal/storage/issueops/cycle_report_test.go @@ -1,7 +1,6 @@ package issueops import ( - "fmt" "maps" "math/rand" "reflect" @@ -196,479 +195,3 @@ func TestBuildCyclesLeavesACompleteCycleUnmarked(t *testing.T) { t.Error("Partial = true for a fully described cycle") } } - -// The IncludeTracks widening: CanonicalMixedCyclePaths adds `tracks` edges to -// the walk but must report a cycle only when a scheduling (blocks/ -// conditional-blocks) edge is also on it. This is the gc-818bx guard: the -// shape it exists to surface is a molecule root that blocks-depends -// (transitively) on its own entry step, which tracks-depends back to the -// root, without reporting the pure-tracks loops that a -// tracks-in-the-plain-walk change reported when it caused the "thousands of -// cycles" regression and had to revert (ordinary convoy topology loops -// constantly through tracks alone). That requirement is NECESSARY AND NOT -// SUFFICIENT: it excludes the pure-tracks loop only, and -// TestCanonicalMixedCyclePathsBoundsAFusedMoleculeByItsSchedulingEdges pins -// what it leaves standing. - -// TestCanonicalMixedCyclePathsIgnoresAPureTracksLoop pins the regression -// guard directly: a loop made entirely of tracks edges — the shape a convoy -// and its tracked issues form routinely — must not be reported. -func TestCanonicalMixedCyclePathsIgnoresAPureTracksLoop(t *testing.T) { - graph := map[string][]MixedCycleEdge{ - "a": {{To: "b"}}, - "b": {{To: "c"}}, - "c": {{To: "a"}}, - } - if got := mustMixedCyclePaths(t, graph); len(got) != 0 { - t.Errorf("pure-tracks loop reported %v, want no cycles: tracks-only convoy topology is not a deadlock", got) - } -} - -// TestCanonicalMixedCyclePathsBoundsAFusedMoleculeByItsSchedulingEdges pins -// what the "at least one scheduling edge" requirement does NOT buy, the -// direction TestCanonicalMixedCyclePathsKeepsDeadlocksATracksEdgeFuses leaves -// open. That test fuses two REAL deadlocks and checks neither is lost; this one -// fuses none at all. The single tracks edge from an entry step back to its root -// pulls the whole molecule into one strongly connected component, and every -// scheduling edge inside a component lies on a cycle — so a strictly acyclic -// blocks chain, which the default walk reports nothing for, still yields one -// cycle per blocks edge. The requirement excludes a PURE tracks loop and -// nothing else, which is why the guarantee on this walk is the BOUND (here 9, -// inside twice the 9 distinct scheduling edges) and never a count of real -// deadlocks. -func TestCanonicalMixedCyclePathsBoundsAFusedMoleculeByItsSchedulingEdges(t *testing.T) { - const steps = 10 - graph := map[string][]MixedCycleEdge{} - schedulingOnly := map[string][]string{} - for i := range steps { - step := fmt.Sprintf("step-%03d", i) - // root -tracks-> step: the stored direction of a convoy's edge to an - // issue it tracks. - graph["root"] = append(graph["root"], MixedCycleEdge{To: step}) - if i == 0 { - continue - } - // A strictly ACYCLIC blocks chain down the steps. Nothing here closes. - previous := fmt.Sprintf("step-%03d", i-1) - graph[step] = append(graph[step], MixedCycleEdge{To: previous, Scheduling: true}) - schedulingOnly[step] = append(schedulingOnly[step], previous) - } - // The entry step tracks back to the root: one edge, and the only cycle in - // the graph runs through it. - graph["step-000"] = append(graph["step-000"], MixedCycleEdge{To: "root"}) - - if plain := CanonicalCyclePaths(schedulingOnly); len(plain) != 0 { - t.Fatalf("the blocks chain alone reported %v, want none: this fixture only measures fusion while its scheduling edges are acyclic", plain) - } - - schedulingEdges := steps - 1 - if got := mustMixedCyclePaths(t, graph); len(got) != schedulingEdges { - t.Errorf("a tracks-fused molecule over an ACYCLIC blocks chain reported %d cycles, want %d — one per scheduling edge inside the fused component: %v", - len(got), schedulingEdges, got) - } -} - -// TestCanonicalMixedCyclePathsFindsTheMoleculeRootShape pins the case gc-818bx -// exists for: a root that blocks-depends (transitively) on its own entry -// step, closed by a tracks edge back to the root. Neither -// CanonicalCyclePaths (no tracks edge at all) nor a plain-tracks walk (no -// scheduling requirement) would report this correctly — the former misses it -// entirely, the latter would drown it in every other tracks loop. -func TestCanonicalMixedCyclePathsFindsTheMoleculeRootShape(t *testing.T) { - graph := map[string][]MixedCycleEdge{ - "root": {{To: "step", Scheduling: true}}, - "step": {{To: "root"}}, // tracks edge closing the loop - } - got := mustMixedCyclePaths(t, graph) - want := [][]string{{"root", "step"}} - if !reflect.DeepEqual(got, want) { - t.Fatalf("cycles = %v, want %v: a blocks edge plus a closing tracks edge is exactly the deadlock this option surfaces", got, want) - } -} - -// TestCanonicalMixedCyclePathsFindsASchedulingCycleAfterATracksOnlyBackEdge -// pins the edge-labelled DFS trap from the exact-head review of gc-818bx. A -// global visited set can first close and ignore the tracks-only a-b loop, then -// miss the qualifying a-c-b-a loop because b is visited but no longer on the -// active path when c reaches it. -func TestCanonicalMixedCyclePathsFindsASchedulingCycleAfterATracksOnlyBackEdge(t *testing.T) { - graph := map[string][]MixedCycleEdge{ - "a": { - {To: "b"}, - {To: "c", Scheduling: true}, - }, - "b": {{To: "a"}}, - "c": {{To: "b"}}, - } - - got := mustMixedCyclePaths(t, graph) - want := [][]string{{"a", "c", "b"}} - if !reflect.DeepEqual(got, want) { - t.Fatalf("cycles = %v, want %v: a tracks-only back edge must not hide a different cycle containing a scheduling edge", got, want) - } -} - -// TestCanonicalMixedCyclePathsStillFindsAPureSchedulingCycle keeps parity with -// CanonicalCyclePaths for the case that needs no tracks edge at all. -func TestCanonicalMixedCyclePathsStillFindsAPureSchedulingCycle(t *testing.T) { - graph := map[string][]MixedCycleEdge{ - "a": {{To: "b", Scheduling: true}}, - "b": {{To: "a", Scheduling: true}}, - } - got := mustMixedCyclePaths(t, graph) - want := [][]string{{"a", "b"}} - if !reflect.DeepEqual(got, want) { - t.Fatalf("cycles = %v, want %v", got, want) - } -} - -// TestCanonicalMixedCyclePathsAcceptsASchedulingEdgeAnywhereOnTheLoop checks -// that the requirement is "at least one scheduling edge on the cycle", not -// specifically on the closing edge — a longer chain where the blocks edge -// sits in the middle must still be reported. -func TestCanonicalMixedCyclePathsAcceptsASchedulingEdgeAnywhereOnTheLoop(t *testing.T) { - graph := map[string][]MixedCycleEdge{ - "a": {{To: "b"}}, // tracks - "b": {{To: "c", Scheduling: true}}, // blocks, in the middle of the loop - "c": {{To: "a"}}, // tracks, closes the loop - } - got := mustMixedCyclePaths(t, graph) - want := [][]string{{"a", "b", "c"}} - if !reflect.DeepEqual(got, want) { - t.Fatalf("cycles = %v, want %v: a scheduling edge anywhere on the loop must qualify it, not only the closing edge", got, want) - } -} - -// TestCanonicalMixedCyclePathsSelfLoop mirrors -// TestCanonicalCyclePathsFindsSelfLoopsAndReportsNoneForAnAcyclicGraph for the -// mixed graph: a self-loop qualifies exactly when its own edge is scheduling. -func TestCanonicalMixedCyclePathsSelfLoop(t *testing.T) { - if got := mustMixedCyclePaths(t, map[string][]MixedCycleEdge{"a": {{To: "a", Scheduling: true}}}); !reflect.DeepEqual(got, [][]string{{"a"}}) { - t.Errorf("blocking self-loop = %v, want [[a]]", got) - } - if got := mustMixedCyclePaths(t, map[string][]MixedCycleEdge{"a": {{To: "a"}}}); len(got) != 0 { - t.Errorf("tracks-only self-loop = %v, want no cycles", got) - } - if got := mustMixedCyclePaths(t, nil); len(got) != 0 { - t.Errorf("empty graph produced %v, want no cycles", got) - } -} - -// TestCanonicalMixedCyclePathsCollapsesParallelEdgesByOringScheduling mirrors -// TestCanonicalCyclePathsCollapsesParallelEdges: two rows for the same pair of -// nodes — one a tracks edge, one blocking — must collapse to one edge that is -// scheduling, not fall out of the requirement because the dedup happened to -// keep the tracks-flavored copy. -func TestCanonicalMixedCyclePathsCollapsesParallelEdgesByOringScheduling(t *testing.T) { - graph := map[string][]MixedCycleEdge{ - "a": {{To: "b"}, {To: "b", Scheduling: true}}, - "b": {{To: "a"}}, - } - got := mustMixedCyclePaths(t, graph) - want := [][]string{{"a", "b"}} - if !reflect.DeepEqual(got, want) { - t.Fatalf("cycles = %v, want %v: a duplicated edge where either copy is scheduling must count as scheduling", got, want) - } -} - -// mustMixedCyclePaths runs CanonicalMixedCyclePaths and fails the test on its -// error, which only a broken internal invariant can produce. -func mustMixedCyclePaths(t *testing.T, graph map[string][]MixedCycleEdge) [][]string { - t.Helper() - paths, err := CanonicalMixedCyclePaths(graph) - if err != nil { - t.Fatalf("CanonicalMixedCyclePaths: %v", err) - } - return paths -} - -// TestCloseMixedCycleReportsAMissingReturnPathAsAnError pins the invariant -// breach as an error rather than a panic. CanonicalMixedCyclePaths cannot -// reach it (it only closes edges inside a strongly connected component, from a -// tree rooted at the edge's target), so the helper is exercised directly: once -// with an edge that has no way back, and once with a tree rooted elsewhere, -// where the climb must stop instead of looping at the wrong root. -func TestCloseMixedCycleReportsAMissingReturnPathAsAnError(t *testing.T) { - everyone := map[string]bool{"a": true, "b": true, "c": true} - oneWay := map[string][]string{"a": {"b"}} - if cycle, err := closeMixedCycle(shortestPathTree(oneWay, everyone, "b"), "a", "b"); err == nil || cycle != nil { - t.Fatalf("closeMixedCycle = %v, %v; want nil and an error: b cannot reach a", cycle, err) - } - if cycle, err := closeMixedCycle(shortestPathTree(oneWay, everyone, "a"), "a", "b"); err == nil || cycle != nil { - t.Fatalf("closeMixedCycle from a tree rooted at a = %v, %v; want nil and an error", cycle, err) - } - - triangle := map[string][]string{"a": {"c"}, "c": {"b"}, "b": {"a"}} - cycle, err := closeMixedCycle(shortestPathTree(triangle, everyone, "b"), "c", "b") - if err != nil { - t.Fatalf("closeMixedCycle on a real cycle: %v", err) - } - if want := []string{"a", "c", "b"}; !slices.Equal(cycle, want) { - t.Errorf("closeMixedCycle = %v, want %v: the edge, then its return path, rotated to the lowest id", cycle, want) - } -} - -// TestCanonicalMixedCyclePathsKeepsDeadlocksATracksEdgeFuses is the #6148 -// review reproduction. a<->b and c<->d are two separate blocks deadlocks; the -// tracks edges b->c and d->a fuse them into ONE strongly connected component. -// A widening option must not shrink the answer: both deadlocks the default -// walk reports are still reported. -func TestCanonicalMixedCyclePathsKeepsDeadlocksATracksEdgeFuses(t *testing.T) { - plain := map[string][]string{"a": {"b"}, "b": {"a"}, "c": {"d"}, "d": {"c"}} - mixed := map[string][]MixedCycleEdge{ - "a": {{To: "b", Scheduling: true}}, - "b": {{To: "a", Scheduling: true}, {To: "c"}}, - "c": {{To: "d", Scheduling: true}}, - "d": {{To: "c", Scheduling: true}, {To: "a"}}, - } - want := CanonicalCyclePaths(plain) - if !reflect.DeepEqual(want, [][]string{{"a", "b"}, {"c", "d"}}) { - t.Fatalf("default walk = %v, want [[a b] [c d]]: the fixture no longer says what this test claims", want) - } - got := mustMixedCyclePaths(t, mixed) - if !reflect.DeepEqual(got, want) { - t.Fatalf("widened = %v, want %v: tracks edges fusing two deadlocks into one component must not drop either", got, want) - } -} - -// TestCanonicalMixedCyclePathsReportsTheChordTheDefaultWalkMisses pins the -// example CycleReport.Cycles documents. On a -> b -> c -> a plus the chord -// a -> c, the default walk crosses a -> c after c is finished and reports only -// a-b-c. The widened report keeps a-b-c and adds a-c, which the chord closes by -// its shortest return path, even with no tracks edge in the graph. -func TestCanonicalMixedCyclePathsReportsTheChordTheDefaultWalkMisses(t *testing.T) { - if got, want := CanonicalCyclePaths(map[string][]string{"a": {"b", "c"}, "b": {"c"}, "c": {"a"}}), [][]string{{"a", "b", "c"}}; !reflect.DeepEqual(got, want) { - t.Fatalf("default walk = %v, want %v: the documented example no longer holds", got, want) - } - got := mustMixedCyclePaths(t, map[string][]MixedCycleEdge{ - "a": {{To: "b", Scheduling: true}, {To: "c", Scheduling: true}}, - "b": {{To: "c", Scheduling: true}}, - "c": {{To: "a", Scheduling: true}}, - }) - if want := [][]string{{"a", "b", "c"}, {"a", "c"}}; !reflect.DeepEqual(got, want) { - t.Fatalf("widened = %v, want %v", got, want) - } -} - -// TestCanonicalMixedCyclePathsIsASupersetOfTheDefaultReport checks the -// widening promise over generated graphs rather than one fixture: on the same -// data, every cycle CanonicalCyclePaths reports on the scheduling edges alone -// is also in the widened report. Each widened cycle must also be a real cycle -// carrying a scheduling edge, stay within the documented size bound, and not -// move when the edge rows arrive in a different order. -func TestCanonicalMixedCyclePathsIsASupersetOfTheDefaultReport(t *testing.T) { - type rawEdge struct { - from, to string - scheduling bool - } - ids := []string{"a", "b", "c", "d", "e", "f"} - rng := rand.New(rand.NewSource(6148)) - widenedBeyondDefault := 0 - for trial := range 600 { - n := 2 + rng.Intn(len(ids)-1) - edges := make([]rawEdge, 0, 3*n) - for range rng.Intn(3 * n) { - edges = append(edges, rawEdge{ids[rng.Intn(n)], ids[rng.Intn(n)], rng.Intn(2) == 0}) - } - - build := func(order []rawEdge) (map[string][]string, map[string][]MixedCycleEdge) { - plain := map[string][]string{} - mixed := map[string][]MixedCycleEdge{} - for _, e := range order { - mixed[e.from] = append(mixed[e.from], MixedCycleEdge{To: e.to, Scheduling: e.scheduling}) - if e.scheduling { - plain[e.from] = append(plain[e.from], e.to) - } - } - return plain, mixed - } - plain, mixed := build(edges) - want := CanonicalCyclePaths(plain) - got := mustMixedCyclePaths(t, mixed) - - for _, cycle := range want { - if !slices.ContainsFunc(got, func(c []string) bool { return slices.Equal(c, cycle) }) { - t.Fatalf("trial %d, edges %v: default cycle %v is missing from the widened report %v (default report %v)", - trial, edges, cycle, got, want) - } - } - - scheduling := map[[2]string]bool{} - for _, e := range edges { - key := [2]string{e.from, e.to} - scheduling[key] = scheduling[key] || e.scheduling - } - schedulingEdges := 0 - for _, isScheduling := range scheduling { - if isScheduling { - schedulingEdges++ - } - } - if bound := schedulingEdges + len(want); len(got) > bound { - t.Fatalf("trial %d, edges %v: widened report has %d cycles, above the bound %d (scheduling edges + default cycles)", - trial, edges, len(got), bound) - } - if len(got) < len(want) { - t.Fatalf("trial %d: widened report %v is smaller than the default %v", trial, got, want) - } - if len(got) > len(want) && len(want) > 0 { - widenedBeyondDefault++ - } - for _, cycle := range got { - if len(slices.Compact(slices.Sorted(slices.Values(cycle)))) != len(cycle) { - t.Fatalf("trial %d, edges %v: widened cycle %v repeats a member", trial, edges, cycle) - } - hasScheduling := false - for i, from := range cycle { - isScheduling, ok := scheduling[[2]string{from, cycle[(i+1)%len(cycle)]}] - if !ok { - t.Fatalf("trial %d, edges %v: widened cycle %v uses %s -> %s, which is not an edge", - trial, edges, cycle, from, cycle[(i+1)%len(cycle)]) - } - hasScheduling = hasScheduling || isScheduling - } - if !hasScheduling { - t.Fatalf("trial %d, edges %v: widened cycle %v is made only of tracks edges", trial, edges, cycle) - } - } - - shuffled := slices.Clone(edges) - rng.Shuffle(len(shuffled), func(i, j int) { shuffled[i], shuffled[j] = shuffled[j], shuffled[i] }) - _, reordered := build(shuffled) - if again := mustMixedCyclePaths(t, reordered); !reflect.DeepEqual(again, got) { - t.Fatalf("trial %d: the same edges in another order produced %v, want %v", trial, again, got) - } - } - if widenedBeyondDefault == 0 { - t.Fatal("no generated graph had a widened report larger than a non-empty default one; the generator is not exercising mixed cycles") - } -} - -// TestCanonicalMixedCyclePathsIsIndependentOfMapOrder mirrors -// TestCanonicalCyclePathsIsIndependentOfMapOrder against the mixed walk: it is -// a separate implementation and must earn the same determinism guarantee -// independently. -func TestCanonicalMixedCyclePathsIsIndependentOfMapOrder(t *testing.T) { - type rawEdge struct { - from, to string - scheduling bool - } - edges := []rawEdge{ - {"a", "b", true}, {"b", "c", false}, {"c", "a", false}, - {"c", "d", true}, {"d", "b", false}, - {"e", "f", false}, {"f", "e", false}, - {"a", "e", true}, {"g", "a", false}, {"g", "f", false}, - } - - rng := rand.New(rand.NewSource(1)) - var first [][]string - for attempt := range 40 { - shuffled := slices.Clone(edges) - rng.Shuffle(len(shuffled), func(i, j int) { shuffled[i], shuffled[j] = shuffled[j], shuffled[i] }) - - graph := map[string][]MixedCycleEdge{} - for _, edge := range shuffled { - graph[edge.from] = append(graph[edge.from], MixedCycleEdge{To: edge.to, Scheduling: edge.scheduling}) - } - - got := mustMixedCyclePaths(t, graph) - if attempt == 0 { - first = got - continue - } - if !reflect.DeepEqual(got, first) { - t.Fatalf("run %d produced %v, run 0 produced %v: the same graph must produce the same report", attempt, got, first) - } - } - if len(first) == 0 { - t.Fatal("the fixture graph has qualifying cycles; the detector found none") - } -} - -// TestShortestPathTreeClosesEachEdgeAsAPerEdgeSearchDid pins the shared search -// to the answer it replaced. Over generated graphs, every edge inside a strongly -// connected component is closed from one tree per target, confined to the -// component, and compared with reachPath run from scratch for that one edge over -// the whole graph. Ties between equally short return paths must break the same -// way too, or the report would change. -func TestShortestPathTreeClosesEachEdgeAsAPerEdgeSearchDid(t *testing.T) { - ids := []string{"a", "b", "c", "d", "e", "f", "g"} - rng := rand.New(rand.NewSource(61482)) - closedEdges := 0 - for trial := range 400 { - n := 2 + rng.Intn(len(ids)-1) - // The normalization CanonicalMixedCyclePaths applies: sorted, - // deduplicated targets, and every target is a node. - plain := map[string][]string{} - nodeSet := map[string]bool{} - for range rng.Intn(3 * n) { - from, to := ids[rng.Intn(n)], ids[rng.Intn(n)] - plain[from] = append(plain[from], to) - nodeSet[from], nodeSet[to] = true, true - } - adjacency := map[string][]MixedCycleEdge{} - for from, targets := range plain { - slices.Sort(targets) - plain[from] = slices.Compact(targets) - for _, to := range plain[from] { - adjacency[from] = append(adjacency[from], MixedCycleEdge{To: to, Scheduling: true}) - } - } - - for _, component := range mixedStronglyConnectedComponents(adjacency, slices.Sorted(maps.Keys(nodeSet))) { - members := map[string]bool{} - for _, node := range component { - members[node] = true - } - for _, to := range component { - tree := shortestPathTree(plain, members, to) - for _, from := range component { - if !slices.Contains(plain[from], to) { - continue - } - got, err := closeMixedCycle(tree, from, to) - if err != nil { - t.Fatalf("trial %d, graph %v: edge %s -> %s: %v", trial, plain, from, to, err) - } - back := reachPath(plain, to, from) - want := rotateToLowest(append([]string{from}, back[:len(back)-1]...)) - if !slices.Equal(got, want) { - t.Fatalf("trial %d, graph %v: edge %s -> %s closes as %v, a per-edge search gave %v", - trial, plain, from, to, got, want) - } - closedEdges++ - } - } - } - } - if closedEdges == 0 { - t.Fatal("no generated graph had an edge inside a component; the generator is not exercising the search") - } -} - -// BenchmarkCanonicalMixedCyclePathsDenseTangle is the worst case a review of -// #6148 named: a blocks edge i -> j for every pair of n issues with i < j, and -// one tracks edge from the last issue back to the first, which makes all n a -// single strongly connected component. It holds n(n-1)/2 scheduling edges but -// only n-1 distinct edge targets. -func BenchmarkCanonicalMixedCyclePathsDenseTangle(b *testing.B) { - const n = 80 - ids := make([]string, n) - for i := range ids { - ids[i] = fmt.Sprintf("n%03d", i) - } - graph := make(map[string][]MixedCycleEdge, n) - for i := range n { - for j := i + 1; j < n; j++ { - graph[ids[i]] = append(graph[ids[i]], MixedCycleEdge{To: ids[j], Scheduling: true}) - } - } - graph[ids[n-1]] = append(graph[ids[n-1]], MixedCycleEdge{To: ids[0]}) - - b.ResetTimer() - for range b.N { - if _, err := CanonicalMixedCyclePaths(graph); err != nil { - b.Fatal(err) - } - } -} diff --git a/internal/storage/issueops/cycles.go b/internal/storage/issueops/cycles.go index 8a345597b..8869c1136 100644 --- a/internal/storage/issueops/cycles.go +++ b/internal/storage/issueops/cycles.go @@ -120,69 +120,6 @@ func appendDependencyGraphInTx(ctx context.Context, tx DBTX, depTables []string, return nil } -// MixedCycleEdge is one edge in the widened graph AppendMixedCycleGraphInTx -// builds: a neighbor plus whether the edge that reaches it is a blocking -// (scheduling) edge, as opposed to a `tracks` edge kept only to complete a -// path. CanonicalMixedCyclePaths uses the flag to require a blocking edge on -// every cycle it reports. -type MixedCycleEdge struct { - To string - Scheduling bool -} - -// AppendMixedCycleGraphInTx adds blocks, conditional-blocks, and tracks -// dependency edges from the given tables on tx into graph, tagging each edge -// scheduling or not. It is the edge set behind -// issueops.DetectCyclesRequest.IncludeTracks: wide enough to see a molecule -// root's `tracks` edge back to its own entry step. Every edge keeps its stored -// direction, issue_id -> depends_on_id, exactly as in the blocks-only graph. -// -// CanonicalMixedCyclePaths turns this graph into a report that holds every -// cycle the default blocks-only walk finds on the same rows, plus cycles that -// need a tracks edge to close. A cycle made ENTIRELY of tracks edges is never -// reported: ordinary convoy topology loops constantly through tracks alone, -// and reporting every one of those loops is the "thousands of cycles" -// regression a previous change to the plain (blocks-only) walk caused and had -// to revert. That exclusion covers the pure-tracks shape and nothing more: a -// tracks loop still fuses a component, and every scheduling edge inside a -// fused component is reported, so this walk is bounded by the scheduling-edge -// count rather than by the number of real deadlocks. See -// CanonicalMixedCyclePaths's BOUND. -// -//nolint:gosec // G201: depTable is hardcoded to "dependencies" or "wisp_dependencies" -func AppendMixedCycleGraphInTx(ctx context.Context, tx DBTX, depTables []string, graph map[string][]MixedCycleEdge) error { - for _, depTable := range depTables { - rows, err := tx.QueryContext(ctx, fmt.Sprintf(` - SELECT issue_id, %s AS depends_on_id, type - FROM %s - `, DepTargetExpr, depTable)) - if err != nil { - return fmt.Errorf("mixed cycle graph: query %s: %w", depTable, err) - } - for rows.Next() { - var issueID, dependsOnID, depType string - if err := rows.Scan(&issueID, &dependsOnID, &depType); err != nil { - _ = rows.Close() - return fmt.Errorf("mixed cycle graph: scan %s: %w", depTable, err) - } - // The backend conformance cases are this switch's pin: the - // package tests build their graphs by hand, so only - // RunCycleDetectorIncludeTracks* reads a row through it. - switch types.DependencyType(depType) { - case types.DepBlocks, types.DepConditionalBlocks: - graph[issueID] = append(graph[issueID], MixedCycleEdge{To: dependsOnID, Scheduling: true}) - case types.DepTracks: - graph[issueID] = append(graph[issueID], MixedCycleEdge{To: dependsOnID}) - } - } - _ = rows.Close() - if err := rows.Err(); err != nil { - return fmt.Errorf("mixed cycle graph: rows %s: %w", depTable, err) - } - } - return nil -} - // CycleThroughEdgesInGraph reports a rendered cycle that traverses // one of the new edges (issueID -> dependsOnID pairs), or "" when no new edge // lies on a cycle. An edge u -> v is on a cycle exactly when u is reachable diff --git a/internal/storage/issueops/dependencies.go b/internal/storage/issueops/dependencies.go index 17b113438..01efd856c 100644 --- a/internal/storage/issueops/dependencies.go +++ b/internal/storage/issueops/dependencies.go @@ -1,50 +1,18 @@ package issueops import ( - "bytes" "context" "database/sql" "errors" "fmt" "strings" - "github.com/gowebpki/jcs" "github.com/steveyegge/beads/internal/storage/depid" "github.com/steveyegge/beads/internal/storage/domain" "github.com/steveyegge/beads/internal/storage/sqlbuild" "github.com/steveyegge/beads/internal/types" ) -// DependencyMetadataEqual reports whether two dependency metadata JSON -// strings are semantically equal per RFC 8785 (JCS), not merely byte-equal. -// Dolt's native JSON column re-canonicalizes stored text on write (spacing, -// key order, number form), so a byte compare between newly-supplied metadata -// and metadata just read back from the column falsely reports a change on -// every non-empty value (#6650). Both dependency re-add idempotency checks -// share this so they can't drift. Falls back to a byte compare if either -// side fails to parse as JSON. -func DependencyMetadataEqual(a, b string) bool { - aCanon, aErr := jcs.Transform([]byte(a)) - bCanon, bErr := jcs.Transform([]byte(b)) - if aErr != nil || bErr != nil { - return a == b - } - return bytes.Equal(aCanon, bCanon) -} - -// ReAddedDependencyThread returns the thread_id a same-type re-add leaves on an -// existing edge and whether that differs from the stored one. A named thread -// the row does not already carry replaces it; an empty one names no thread -// rather than asking for none, so the stored value — NULL included — is kept. -// Both dependency re-add idempotency checks share this, alongside -// DependencyMetadataEqual, so they can't drift. -func ReAddedDependencyThread(stored sql.NullString, requested string) (sql.NullString, bool) { - if requested == "" || requested == stored.String { - return stored, false - } - return sql.NullString{String: requested, Valid: true}, true -} - type DepTargetKind int const ( @@ -191,9 +159,7 @@ type DepTargetPrecheck struct { // - Source/target existence validation // - Hierarchy deadlock validation for blocking deps (GH#1495, bd-wg7ve) // - Cycle detection via recursive CTE across both dependency tables -// - Idempotent same-type re-adds: a metadata or thread change updates it -// (an empty ThreadID keeps the stored thread), a change-free re-add -// writes and journals nothing (#5898 R3) +// - Idempotent same-type updates (metadata only) // - Type conflict detection // // The caller is responsible for transaction lifecycle, dolt commits, and @@ -204,27 +170,11 @@ type DepTargetPrecheck struct { // same-type re-add or a silent structural add. Callers that stage tables for a // Dolt commit stage the events table only when an event row exists, so a // no-event add cannot sweep unrelated pending rows into the commit (GH#2455). -// -// A genuinely new edge is a change to the referencing (source) issue's durable -// state, so it mints one version row for the source — on EVERY leg that reaches -// this helper (the dependency editor, the legacy store verbs, batch apply), -// whether or not an audit event was requested. A same-type re-add that changes -// metadata or thread is the same kind of durable-state change and mints on the -// same terms; only a genuinely change-free re-add (identical type and metadata, -// no new thread) mints nothing. func AddDependencyInTx(ctx context.Context, tx *sql.Tx, dep *types.Dependency, actor string, opts AddDependencyOpts) (bool, error) { - return addDependencyInTx(ctx, tx, dep, actor, opts, nil, true) + return addDependencyInTx(ctx, tx, dep, actor, opts, nil) } -// addDependencyInTx is the body of AddDependencyInTx. mintVersion controls -// whether a new edge mints the source's version row here: the exported entry -// point always does, while a parent patch (applyParentPatch) rewires several -// edges for one caller-visible mutation and mints once after the last of them. -func addDependencyInTx(ctx context.Context, tx *sql.Tx, dep *types.Dependency, actor string, opts AddDependencyOpts, recomputeResult *RecomputeIsBlockedResult, mintVersion bool) (bool, error) { - if strings.HasPrefix(dep.DependsOnID, "external:") && dep.Type == types.DepParentChild { - return false, fmt.Errorf("external capability dependencies cannot use parent-child edges") - } - +func addDependencyInTx(ctx context.Context, tx *sql.Tx, dep *types.Dependency, actor string, opts AddDependencyOpts, recomputeResult *RecomputeIsBlockedResult) (bool, error) { // Auto-detect source routing if not provided. sourceTable := opts.SourceTable writeTable := opts.WriteTable @@ -309,49 +259,22 @@ func addDependencyInTx(ctx context.Context, tx *sql.Tx, dep *types.Dependency, a // target expression defensively so stale/reclassified rows in another typed // target column cannot bypass the idempotency/conflict check. var existingType string - var existingMetadataNS, existingThreadNS sql.NullString //nolint:gosec // G201: writeTable from WispTableRouting; depTargetEquals has no user input. - err := tx.QueryRowContext(ctx, fmt.Sprintf(`SELECT type, metadata, thread_id FROM %s WHERE issue_id = ? AND %s`, writeTable, depTargetEquals("")), - dep.IssueID, dep.DependsOnID).Scan(&existingType, &existingMetadataNS, &existingThreadNS) + err := tx.QueryRowContext(ctx, fmt.Sprintf(`SELECT type FROM %s WHERE issue_id = ? AND %s`, writeTable, depTargetEquals("")), + dep.IssueID, dep.DependsOnID).Scan(&existingType) if err == nil { - existingMetadata := existingMetadataNS.String - if !existingMetadataNS.Valid { - existingMetadata = "{}" - } if existingType == string(dep.Type) { - // A re-add naming a thread the row does not carry moves it. One - // naming none keeps the stored thread rather than clearing it, so - // the callers that never carry a thread cannot erase one. - thread, threadChanged := ReAddedDependencyThread(existingThreadNS, dep.ThreadID) - if !threadChanged && DependencyMetadataEqual(existingMetadata, metadata) { - // Same type, same metadata, same thread: a change-free write. - // Nothing is written and nothing is journaled (#5898 R3). - return false, nil - } - // Same type, different metadata or thread — idempotent; update the - // row in place. No event is written, so the caller must not stage - // the events table for this re-add. + // Same type — idempotent; update metadata. No event is written, so the + // caller must not stage the events table for this re-add. //nolint:gosec // G201: writeTable from WispTableRouting; depTargetEquals has no user input. - if _, err := tx.ExecContext(ctx, fmt.Sprintf(`UPDATE %s SET metadata = ?, thread_id = ? WHERE issue_id = ? AND %s`, writeTable, depTargetEquals("")), - metadata, thread, dep.IssueID, dep.DependsOnID); err != nil { + if _, err := tx.ExecContext(ctx, fmt.Sprintf(`UPDATE %s SET metadata = ? WHERE issue_id = ? AND %s`, writeTable, depTargetEquals("")), + metadata, dep.IssueID, dep.DependsOnID); err != nil { return false, fmt.Errorf("failed to update dependency metadata: %w", err) } - // A same-type add refreshes the edge's metadata or thread. It is an - // observable graph mutation, so journal the replacement edge for - // replay even though no audit event is written. EventDep has no - // thread member, so a thread-only change journals the same payload - // as the original add; the source's version snapshot is what - // records the new thread. - if err := RecordDepEventInTx(ctx, tx, EventDepAdd, dep.IssueID, string(dep.Type), dep.DependsOnID, metadata, actor); err != nil { - return false, err - } - // The metadata or thread genuinely changed, so — unlike the - // change-free branch above — this re-add is a real durable-state - // mutation of the source issue (its version snapshot carries each - // edge's thread_id) and mints on the same terms as a new edge - // (#5898 leg 2: "a same-type re-add whose metadata actually - // changed mints EXACTLY ONE version carrying the new state"). - return false, mintDependencyVersion(ctx, tx, dep.IssueID, actor, mintVersion) + // A same-type add refreshes edge metadata. It is an observable graph + // mutation, so emit the complete replacement edge for replay even + // though no audit event is written. + return false, RecordDepEventInTx(ctx, tx, EventDepAdd, dep.IssueID, string(dep.Type), dep.DependsOnID, metadata, actor) } return false, &domain.DependencyTypeConflictError{ IssueID: dep.IssueID, @@ -424,36 +347,15 @@ func addDependencyInTx(ctx context.Context, tx *sql.Tx, dep *types.Dependency, a } mergeRecomputeIsBlockedResult(recomputeResult, recomputed) // Snapshot only after all derived blocked-state maintenance has completed. - if err := RecordDepEventInTx(ctx, tx, EventDepAdd, dep.IssueID, string(dep.Type), dep.DependsOnID, metadata, actor); err != nil { - return eventWritten, err - } - return eventWritten, mintDependencyVersion(ctx, tx, dep.IssueID, actor, mintVersion) + return eventWritten, RecordDepEventInTx(ctx, tx, EventDepAdd, dep.IssueID, string(dep.Type), dep.DependsOnID, metadata, actor) } if err := MarkIsBlockedInTx(ctx, tx, affectedIssues, affectedWisps); err != nil { return false, fmt.Errorf("mark is_blocked after add dependency %s -> %s: %w", dep.IssueID, dep.DependsOnID, err) } // Snapshot only after all derived blocked-state maintenance has completed. // The journal is never gated on opts.EmitEvent: a structurally-wired edge is - // as real to a replaying consumer as one added by an explicit dep verb — - // and neither is the version row minted beside it. - if err := RecordDepEventInTx(ctx, tx, EventDepAdd, dep.IssueID, string(dep.Type), dep.DependsOnID, metadata, actor); err != nil { - return eventWritten, err - } - return eventWritten, mintDependencyVersion(ctx, tx, dep.IssueID, actor, mintVersion) -} - -// mintDependencyVersion versions the referencing (source) issue of an edge that -// was actually inserted, deleted, or had its metadata or thread refreshed by a -// same-type re-add: the version-history seam for every dependency write path, -// reached from addDependencyInTx and removeDependencyInTx only past a real row -// write — never from a genuinely change-free re-add or the absent-edge return, -// which mint nothing. mint false defers to a caller that mints once for a -// multi-edge mutation. A wisp source is excluded by the seam itself. -func mintDependencyVersion(ctx context.Context, tx DBTX, issueID, actor string, mint bool) error { - if !mint { - return nil - } - return RecordVersionInTx(ctx, tx, issueID, actor) + // as real to a replaying consumer as one added by an explicit dep verb. + return eventWritten, RecordDepEventInTx(ctx, tx, EventDepAdd, dep.IssueID, string(dep.Type), dep.DependsOnID, metadata, actor) } // RemoveSourceFromAffected drops the dep source from the affected-ID sets @@ -578,66 +480,34 @@ func WouldCreateSchedulingCycleInTx(ctx context.Context, tx DBTX, issueID, depen // ready-work computation, so a chain mixing blocks and parent-child edges // can form a logical livelock that prevents anything from being ready. func cycleReachabilityQuery(depTables []string) string { - return reachabilityQuery("reachable", depTables, "d.type IN ('blocks', 'conditional-blocks', 'parent-child')") -} + if len(depTables) == 1 { + return fmt.Sprintf(` + WITH RECURSIVE reachable(node) AS ( + SELECT ? + UNION + SELECT %s + FROM reachable r + JOIN %s d ON d.issue_id = r.node AND d.type IN ('blocks', 'conditional-blocks', 'parent-child') + ) + SELECT COUNT(*) FROM reachable WHERE node = ? + `, DepTargetExpr, depTables[0]) + } -// reachabilityQuery builds a recursive walk (CTE named cte) from the first -// placeholder along outgoing dependency rows matching typeFilter, counting -// whether the second placeholder is reached (the anchor included). Callers -// pass constants for cte, depTables and typeFilter, which are spliced in -// verbatim. -// -// Each dependency table gets its own recursive member joined directly on -// d.issue_id, so every recursion step is an index lookup on issue_id. Joining -// a UNION of the tables instead (the earlier shape) leaves Dolt nothing to -// index: it rescans every matching edge per step, which on a 100k-edge graph -// is minutes for one check. -// -// The join hints and the filter placement are both load-bearing, and -// TestGraphWalkPlansHonorJoinHints (embedded) pins the resulting plans: -// -// - JOIN_ORDER(r, d) LOOKUP_JOIN(r, d) pins the frontier as the outer side -// with an index probe into the edge table (as sqlbuild.DescendantWalkQuery -// does). Without them the Dolt sql-server's cost-based planner picks a -// type-index scan join when table statistics are not yet available (e.g. -// right after a commit), and the embedded engine does so for a member -// whose table has no statistics, with the same minutes-long result. On -// MySQL, JOIN_ORDER is honored (the order it forces is the one wanted) and -// the unknown LOOKUP_JOIN hint is ignored with a warning. -// - typeFilter is applied in the projection, not the join condition: a row -// of another type yields a NULL node, which joins no row and never equals -// the goal, so the reachable set is unchanged. Left in the ON clause, a -// single-type filter ("d.type = 'parent-child'") is an equality the -// planner can satisfy through a type-leading index instead, keyed by the -// constant — a lookup that returns every parent-child row of the table -// for each frontier node. -// -// The reachable set is the same as the single-member walk over the UNION: -// UNION distinct merges what every member produces per step. -func reachabilityQuery(cte string, depTables []string, typeFilter string) string { + var unions []string + for _, t := range depTables { + unions = append(unions, fmt.Sprintf("SELECT issue_id, %s AS depends_on_id FROM %s WHERE type IN ('blocks', 'conditional-blocks', 'parent-child')", DepTargetExpr, t)) + } + unionQuery := strings.Join(unions, " UNION ") return fmt.Sprintf(` - WITH RECURSIVE %s(node) AS ( + WITH RECURSIVE reachable(node) AS ( SELECT ? UNION - %s + SELECT d.depends_on_id + FROM reachable r + JOIN (%s) d ON d.issue_id = r.node ) - SELECT COUNT(*) FROM %s WHERE node = ? - `, cte, reachabilityMembers(cte, depTables, typeFilter), cte) -} - -// reachabilityMembers is reachabilityQuery's recursive members, one per -// dependency table and joined by UNION, for any walk over the same edges -// (ancestorChainInTx in blocked_state.go shares them). A NULL node, which is -// what a row of another type projects, is the caller's to filter out of a -// walk's result. -func reachabilityMembers(cte string, depTables []string, typeFilter string) string { - members := make([]string, 0, len(depTables)) - for _, t := range depTables { - members = append(members, fmt.Sprintf( - "SELECT /*+ JOIN_ORDER(r, d) LOOKUP_JOIN(r, d) */ CASE WHEN %s THEN %s END FROM %s r JOIN %s d ON d.issue_id = r.node", - typeFilter, depTargetExpr("d"), cte, t)) - } - return strings.Join(members, "\n\t\t\tUNION\n\t\t\t") + SELECT COUNT(*) FROM reachable WHERE node = ? + `, unionQuery) } func cycleDetectionTables() []string { @@ -683,7 +553,21 @@ func CheckBlockingHierarchyInTx(ctx context.Context, tx DBTX, dep *types.Depende // and cousins in the same hierarchy do not match). Uses UNION distinct // recursion so diamond/cyclic parentage terminates by unique reachable node. func isAncestorInTx(ctx context.Context, tx DBTX, node, candidate string, depTables []string) (bool, error) { - query := reachabilityQuery("ancestors", depTables, "d.type = 'parent-child'") + var unions []string + for _, t := range depTables { + unions = append(unions, fmt.Sprintf("SELECT issue_id, %s AS parent_id FROM %s WHERE type = 'parent-child'", DepTargetExpr, t)) + } + //nolint:gosec // G201: depTables are fixed dependency table names from cycleDetectionTables/opts. + query := fmt.Sprintf(` + WITH RECURSIVE ancestors(node) AS ( + SELECT ? + UNION + SELECT d.parent_id + FROM ancestors a + JOIN (%s) d ON d.issue_id = a.node + ) + SELECT COUNT(*) FROM ancestors WHERE node = ? + `, strings.Join(unions, " UNION ")) var n int if err := tx.QueryRowContext(ctx, query, node, candidate).Scan(&n); err != nil { return false, err @@ -1123,15 +1007,10 @@ func checkRenameTargetCollision(ctx context.Context, tx DBTX, table, typedCol, n // //nolint:gosec // G201: depTable from WispTableRouting (hardcoded constants) func RemoveDependencyInTx(ctx context.Context, tx *sql.Tx, issueID, dependsOnID, actor string, emitEvent bool) (bool, error) { - return removeDependencyInTx(ctx, tx, issueID, dependsOnID, actor, emitEvent, nil, true) + return removeDependencyInTx(ctx, tx, issueID, dependsOnID, actor, emitEvent, nil) } -// removeDependencyInTx is the body of RemoveDependencyInTx. A deleted edge is a -// change to the source issue's durable state and mints one version row for it, -// whether or not an audit event was requested; an absent edge returns before -// any write and mints nothing. mintVersion is addDependencyInTx's, for the -// same reason. -func removeDependencyInTx(ctx context.Context, tx *sql.Tx, issueID, dependsOnID, actor string, emitEvent bool, recomputeResult *RecomputeIsBlockedResult, mintVersion bool) (bool, error) { +func removeDependencyInTx(ctx context.Context, tx *sql.Tx, issueID, dependsOnID, actor string, emitEvent bool, recomputeResult *RecomputeIsBlockedResult) (bool, error) { isWisp := IsActiveWispInTx(ctx, tx, issueID) _, _, eventTable, depTable := WispTableRouting(isWisp) @@ -1186,12 +1065,8 @@ func removeDependencyInTx(ctx context.Context, tx *sql.Tx, issueID, dependsOnID, NoteDependencyRemovalBlockedRecheck(tx, issueID, dependsOnID, affectedIssues, affectedWisps) // Snapshot only after all derived blocked-state maintenance has completed. // Never gated on emitEvent — a structural removal is as real to a replaying - // consumer as one from an explicit dep verb. The same holds for the - // version row minted beside it. - if err := RecordDepEventInTx(ctx, tx, EventDepRemove, issueID, depType, dependsOnID, depMetadata, actor); err != nil { - return eventWritten, err - } - return eventWritten, mintDependencyVersion(ctx, tx, issueID, actor, mintVersion) + // consumer as one from an explicit dep verb. + return eventWritten, RecordDepEventInTx(ctx, tx, EventDepRemove, issueID, depType, dependsOnID, depMetadata, actor) } func mergeRecomputeIsBlockedResult(target *RecomputeIsBlockedResult, source RecomputeIsBlockedResult) { diff --git a/internal/storage/issueops/dependencies_test.go b/internal/storage/issueops/dependencies_test.go index af2cfb5cd..88880eb33 100644 --- a/internal/storage/issueops/dependencies_test.go +++ b/internal/storage/issueops/dependencies_test.go @@ -2,7 +2,6 @@ package issueops import ( "context" - "database/sql" "errors" "reflect" "regexp" @@ -178,7 +177,7 @@ func TestCycleReachabilityQuerySingleTableJoinsDirectly(t *testing.T) { t.Fatalf("single-table cycle query should not materialize a derived dependency table:\n%s", query) } if !strings.Contains(query, "d.type IN ('blocks', 'conditional-blocks', 'parent-child')") { - t.Fatalf("query does not filter scheduling-relevant dependency types:\n%s", query) + t.Fatalf("query does not filter scheduling-relevant dependency types at the direct join:\n%s", query) } if strings.Contains(query, "UNION ALL") || strings.Contains(query, "depth") { t.Fatalf("cycle query should traverse unique nodes, not enumerate paths:\n%s", query) @@ -190,39 +189,14 @@ func TestCycleReachabilityQueryMultipleTablesTraversesUniqueNodes(t *testing.T) if strings.Contains(query, "UNION ALL") || strings.Contains(query, "depth") { t.Fatalf("multi-table cycle query should traverse unique nodes, not enumerate paths:\n%s", query) } - // One recursive member per table, each joined directly on issue_id: a - // join against a UNION of the tables cannot use an index, and Dolt then - // rescans every edge per recursion step. - for _, table := range []string{"dependencies", "wisp_dependencies"} { - if !strings.Contains(query, "JOIN "+table+" d ON d.issue_id = r.node") { - t.Fatalf("query does not join %s directly on issue_id:\n%s", table, query) - } + if !strings.Contains(query, "FROM dependencies") { + t.Fatalf("query does not include dependencies table:\n%s", query) } - if strings.Contains(query, "JOIN (SELECT") { - t.Fatalf("multi-table cycle query should not join a derived union of the dependency tables:\n%s", query) + if !strings.Contains(query, "FROM wisp_dependencies") { + t.Fatalf("query does not include wisp_dependencies table:\n%s", query) } - if strings.Count(query, "JOIN_ORDER(r, d) LOOKUP_JOIN(r, d)") != 2 { - t.Fatalf("every recursive member should pin the lookup join:\n%s", query) - } - if !strings.Contains(query, depTargetExpr("d")) { - t.Fatalf("query does not resolve depends_on_id via the typed target columns:\n%s", query) - } -} - -func TestAncestorQueryJoinsEachTableOnIssueID(t *testing.T) { - query := reachabilityQuery("ancestors", []string{"dependencies", "wisp_dependencies"}, "d.type = 'parent-child'") - for _, table := range []string{"dependencies", "wisp_dependencies"} { - // issue_id is the join's only predicate: a type equality there could - // be planned as a lookup into a type-leading index instead. - if !strings.Contains(query, "JOIN "+table+" d ON d.issue_id = r.node\n") { - t.Fatalf("ancestor query does not join %s on issue_id alone:\n%s", table, query) - } - } - if strings.Count(query, "CASE WHEN d.type = 'parent-child' THEN") != 2 { - t.Fatalf("ancestor query does not filter parent-child rows in each member's projection:\n%s", query) - } - if strings.Count(query, "JOIN_ORDER(r, d) LOOKUP_JOIN(r, d)") != 2 { - t.Fatalf("every recursive member should pin the lookup join:\n%s", query) + if !strings.Contains(query, DepTargetExpr) { + t.Fatalf("query does not resolve depends_on_id via DepTargetExpr:\n%s", query) } } @@ -285,138 +259,3 @@ func TestReplaceDependencyTargetRekeysCascadedRows(t *testing.T) { t.Fatalf("unmet sql expectations: %v", err) } } - -// TestDependencyMetadataEqualComparesValuesNotBytes pins the change-free -// re-add gate's comparison rule. The gate decides between "nothing happened" -// and "a real mutation of the source issue" by comparing metadata the CALLER -// supplied against metadata read back out of a native JSON column, and a -// native JSON column is free to re-canonicalize what it persists — Dolt's -// inserts a space after ':', sorts object keys, and narrows a bare integral -// float to an int. A byte compare therefore reports a change on values that -// never changed. Each same-value row here is a spelling difference a byte -// compare fails and a JCS (RFC 8785) compare survives (#6650). -func TestDependencyMetadataEqualComparesValuesNotBytes(t *testing.T) { - t.Parallel() - - for _, tc := range []struct { - name string - a string - b string - want bool - }{ - {name: "IdenticalBytes", a: `{"gate":"any-children"}`, b: `{"gate":"any-children"}`, want: true}, - {name: "EmptyObjects", a: `{}`, b: `{}`, want: true}, - {name: "StorageInsertedSpacing", a: `{"gate":"any-children"}`, b: `{"gate": "any-children"}`, want: true}, - {name: "StorageSortedKeys", a: `{"spawner_id":"src-1","gate":"any-children"}`, b: `{"gate":"any-children","spawner_id":"src-1"}`, want: true}, - {name: "StorageNarrowedIntegralFloat", a: `{"weight":1.0}`, b: `{"weight":1}`, want: true}, - {name: "DifferentValue", a: `{"note":"v1"}`, b: `{"note":"v2"}`, want: false}, - {name: "DifferentKey", a: `{"gate":"any-children"}`, b: `{"mode":"any-children"}`, want: false}, - {name: "EmptyVersusPopulated", a: `{}`, b: `{"gate":"any-children"}`, want: false}, - // Neither side parses, so the rule falls back to the byte compare it - // replaced rather than calling two unparseable strings equal. - {name: "UnparseableFallsBackToBytesEqual", a: `not json`, b: `not json`, want: true}, - {name: "UnparseableFallsBackToBytesUnequal", a: `not json`, b: `also not json`, want: false}, - {name: "OneSideUnparseableFallsBackToBytes", a: `{"gate":"any-children"}`, b: `{"gate":`, want: false}, - } { - t.Run(tc.name, func(t *testing.T) { - t.Parallel() - if got := DependencyMetadataEqual(tc.a, tc.b); got != tc.want { - t.Errorf("DependencyMetadataEqual(%q, %q) = %v, want %v", tc.a, tc.b, got, tc.want) - } - // The rule is symmetric: which side came from the column and - // which from the caller must not change the answer. - if got := DependencyMetadataEqual(tc.b, tc.a); got != tc.want { - t.Errorf("DependencyMetadataEqual(%q, %q) = %v, want %v (asymmetric)", tc.b, tc.a, got, tc.want) - } - }) - } -} - -// TestReAddedDependencyThreadKeepsTheStoredThreadUnlessANewOneIsNamed pins the -// thread half of the same-type re-add gate. A re-add naming a thread the row -// does not carry is a change that must reach the row; a re-add naming none -// says nothing about threads, so it must leave the stored one — NULL, empty -// or named — exactly as it is, or every plain `bd dep add` of a threaded edge -// would erase its thread. -func TestReAddedDependencyThreadKeepsTheStoredThreadUnlessANewOneIsNamed(t *testing.T) { - t.Parallel() - - null := sql.NullString{} - empty := sql.NullString{Valid: true} - named := sql.NullString{String: "thread-1", Valid: true} - for _, tc := range []struct { - name string - stored sql.NullString - requested string - want sql.NullString - wantChanged bool - }{ - {name: "NoneNamedKeepsNull", stored: null, requested: "", want: null}, - {name: "NoneNamedKeepsEmpty", stored: empty, requested: "", want: empty}, - {name: "NoneNamedKeepsTheStoredThread", stored: named, requested: "", want: named}, - {name: "SameThreadIsNoChange", stored: named, requested: "thread-1", want: named}, - {name: "NewThreadOverNull", stored: null, requested: "thread-1", want: named, wantChanged: true}, - {name: "NewThreadOverEmpty", stored: empty, requested: "thread-1", want: named, wantChanged: true}, - {name: "DifferentThreadReplacesIt", stored: named, requested: "thread-2", - want: sql.NullString{String: "thread-2", Valid: true}, wantChanged: true}, - } { - t.Run(tc.name, func(t *testing.T) { - t.Parallel() - got, changed := ReAddedDependencyThread(tc.stored, tc.requested) - if got != tc.want || changed != tc.wantChanged { - t.Errorf("ReAddedDependencyThread(%+v, %q) = (%+v, %v), want (%+v, %v)", - tc.stored, tc.requested, got, changed, tc.want, tc.wantChanged) - } - }) - } -} - -// TestAddDependencyInTxReadsNullStoredMetadataAsEmptyObject pins the widened -// SELECT's handling of a SQL-NULL metadata column. dependencies.metadata is -// nullable (`JSON DEFAULT (JSON_OBJECT())`, no NOT NULL), so a row written out -// of band — bd sql, an external tool, hand SQL — can hold NULL. Before the -// re-add gate read metadata at all this path scanned only `type` and was -// idempotently happy; scanning NULL into a plain string would hard-fail it -// with "converting NULL to string is unsupported", turning every subsequent -// re-add of that edge into an error. NULL is the absent-metadata state, so it -// reads as `{}` and the change-free re-add of a metadata-free edge stays the -// no-op it was. -// -// thread_id is nullable the same way (a VARCHAR with no NOT NULL), and the gate -// reads it too, so the row here holds NULL in both columns: a re-add naming no -// thread keeps whatever thread the row stores, NULL included, so this re-add -// is still change-free. -func TestAddDependencyInTxReadsNullStoredMetadataAsEmptyObject(t *testing.T) { - t.Parallel() - - _, mock, tx := beginMockTx(t) - - mock.ExpectQuery(regexp.QuoteMeta("SELECT issue_type FROM issues WHERE id = ?")). - WithArgs("dep-a"). - WillReturnRows(sqlmock.NewRows([]string{"issue_type"}).AddRow("task")) - mock.ExpectQuery(regexp.QuoteMeta("SELECT type, metadata, thread_id FROM dependencies")). - WithArgs("dep-a", "dep-b"). - WillReturnRows(sqlmock.NewRows([]string{"type", "metadata", "thread_id"}).AddRow(string(types.DepRelated), nil, nil)) - - kind := DepTargetIssue - dep := &types.Dependency{IssueID: "dep-a", DependsOnID: "dep-b", Type: types.DepRelated} - created, err := AddDependencyInTx(context.Background(), tx, dep, "writer", AddDependencyOpts{ - SourceTable: "issues", - TargetTable: "issues", - WriteTable: "dependencies", - SkipCycleCheck: true, - TargetKind: &kind, - PrecheckedTarget: &DepTargetPrecheck{IssueType: "task"}, - }) - if err != nil { - t.Fatalf("AddDependencyInTx(existing edge with NULL metadata) = %v, want nil: a change-free re-add is a no-op, not an error", err) - } - if created { - t.Error("AddDependencyInTx reported the edge as created; the edge already existed with the requested type") - } - // No UPDATE and no journal write are expected: scripting only the two - // reads means any write this path attempted would fail the run. - if err := mock.ExpectationsWereMet(); err != nil { - t.Fatalf("unmet sql expectations: %v", err) - } -} diff --git a/internal/storage/issueops/dependency_editor.go b/internal/storage/issueops/dependency_editor.go index d1c7fac46..b13b90a66 100644 --- a/internal/storage/issueops/dependency_editor.go +++ b/internal/storage/issueops/dependency_editor.go @@ -21,12 +21,6 @@ import ( // self-edge otherwise trips the cycle check and reports the wrong refusal, and // SkipPerEdgeCycleCheck would skip it entirely. // -// The dotted-id hierarchy rule (publicops.CheckDottedChildDependency) is here -// for the same reason: it is decided from the request alone, and putting it in -// the shared validator is what makes every leg — dolt, embedded, the unit of -// work and the server behind httpapi — refuse a child gated on its own dotted -// parent, not only the CLI that used to check it before calling the role. -// // The type check is that there IS a type — non-empty, within the column's // length. It is deliberately not a membership test: the vocabulary is an open, // workspace-configurable set (see the Dep* constants), so refusing an unlisted @@ -49,9 +43,6 @@ func ValidateAddDependenciesRequest(request publicops.AddDependenciesRequest) er if edge.IssueID == edge.DependsOnID { return fmt.Errorf("%w: %s cannot depend on itself", domain.ErrSelfDependency, edge.IssueID) } - if err := publicops.CheckDottedChildDependency(edge.IssueID, edge.DependsOnID, edge.Type); err != nil { - return err - } } return nil } @@ -182,11 +173,6 @@ func addDependencyEdgeInTx(ctx context.Context, tx *sql.Tx, request publicops.Ad if err != nil { return nil, err } - // The edge's referencing (source) issue is versioned by AddDependencyInTx - // itself, at the row write — once, and only for a genuinely inserted row - // (the idempotent re-add mints nothing, the same exemption eventWritten - // draws for the events table below) — so every leg that reaches that - // helper versions identically and this role adds no second mint. // Stage the source's dependency table always and its events table only // when a row was recorded — the same selective staging the stores' own // AddDependencyWithOptions does, so an idempotent re-add cannot sweep @@ -272,9 +258,6 @@ func ExecuteRemoveDependency(ctx context.Context, tx *sql.Tx, request publicops. if !eventWritten { return publicops.RemoveDependencyResult{Removed: false}, nil, nil } - // A removal that actually happened versioned the referencing issue inside - // RemoveDependencyInTx, at the row delete; an absent edge returned above - // without ever reaching that seam. tables := ChangedTables{} tables.Add(depTable, eventTable) return publicops.RemoveDependencyResult{Removed: true}, tables, nil diff --git a/internal/storage/issueops/dependency_queries.go b/internal/storage/issueops/dependency_queries.go index 329765783..39c3b1dfb 100644 --- a/internal/storage/issueops/dependency_queries.go +++ b/internal/storage/issueops/dependency_queries.go @@ -9,7 +9,6 @@ import ( "strings" "github.com/steveyegge/beads/internal/types" - publicops "github.com/steveyegge/beads/issueops" ) // GetAllDependencyRecordsInTx returns all dependency records from permanent and @@ -27,49 +26,6 @@ func GetAllDependencyRecordsInTx(ctx context.Context, tx DBTX) (map[string][]*ty return result, nil } -// GetExternalBlockingDependencyRecordsInTx returns explicit external: edges -// whose dependency type can block ready work. Cross-prefix issue IDs may share -// depends_on_external storage, so the external: prefix check is intentional. -func GetExternalBlockingDependencyRecordsInTx(ctx context.Context, tx DBTX) (map[string][]*types.Dependency, error) { - result := make(map[string][]*types.Dependency) - for _, depTable := range []string{"dependencies", "wisp_dependencies"} { - if err := getExternalBlockingDependencyRecordsIntoFromTable(ctx, tx, depTable, result); err != nil { - if optionalBlockedTable(depTable) && isTableNotExistError(err) { - continue - } - return nil, err - } - } - return result, nil -} - -//nolint:gosec // G201: depTable is "dependencies" or "wisp_dependencies" (hardcoded by caller). -func getExternalBlockingDependencyRecordsIntoFromTable(ctx context.Context, tx DBTX, depTable string, result map[string][]*types.Dependency) error { - rows, err := tx.QueryContext(ctx, fmt.Sprintf(` - SELECT issue_id, %s AS depends_on_id, type, created_at, created_by, metadata, thread_id - FROM %s - WHERE depends_on_external LIKE 'external:%%' - AND type IN ('blocks', 'conditional-blocks', 'waits-for') - ORDER BY issue_id - `, DepTargetExpr, depTable)) - if err != nil { - return fmt.Errorf("get external blocking dependency records from %s: %w", depTable, err) - } - defer rows.Close() - - for rows.Next() { - dep, scanErr := scanDependencyRow(rows) - if scanErr != nil { - return fmt.Errorf("get external blocking dependency records from %s: %w", depTable, scanErr) - } - result[dep.IssueID] = append(result[dep.IssueID], dep) - } - if err := rows.Err(); err != nil { - return fmt.Errorf("get external blocking dependency records from %s: %w", depTable, err) - } - return nil -} - //nolint:gosec // G201: depTable is "dependencies" or "wisp_dependencies" (hardcoded by caller). func getAllDependencyRecordsIntoFromTable(ctx context.Context, tx DBTX, depTable string, result map[string][]*types.Dependency) error { // Total order: issue_id alone is only a grouping key; without a tiebreaker the @@ -944,25 +900,10 @@ func GetNewlyUnblockedByCloseInTx(ctx context.Context, tx DBTX, closedIssueID st // IsBlockedInTx checks if an issue is blocked by active dependencies within // an existing transaction. Returns whether the issue is blocked and, if so, -// a list of blocker descriptions for display, each spelled by -// publicops.Blocker.String. -func IsBlockedInTx(ctx context.Context, tx DBTX, issueID string) (bool, []string, error) { - blocked, blockers, err := isBlockedByInTx(ctx, tx, issueID) - if err != nil || len(blockers) == 0 { - return blocked, nil, err - } - names := make([]string, 0, len(blockers)) - for _, blocker := range blockers { - names = append(names, blocker.String()) - } - return blocked, names, nil -} - -// isBlockedByInTx is IsBlockedInTx with the live direct blockers kept typed, so -// a refusal can carry them without re-reading its own display strings. +// a list of blocker descriptions for display. // //nolint:gosec // G201: table names are hardcoded constants. -func isBlockedByInTx(ctx context.Context, tx DBTX, issueID string) (bool, []publicops.Blocker, error) { +func IsBlockedInTx(ctx context.Context, tx DBTX, issueID string) (bool, []string, error) { var blocked bool found := false for _, table := range []string{"issues", "wisps"} { @@ -985,12 +926,14 @@ func isBlockedByInTx(ctx context.Context, tx DBTX, issueID string) (bool, []publ return false, nil, nil } - var edges []blockerEdge + type depEdge struct { + dependsOnID, depType string + } + var edges []depEdge for _, depTable := range []string{"dependencies", "wisp_dependencies"} { rows, err := tx.QueryContext(ctx, fmt.Sprintf(` SELECT %s AS depends_on_id, type FROM %s WHERE issue_id = ? AND type IN ('blocks', 'waits-for', 'conditional-blocks') - ORDER BY depends_on_id, type `, DepTargetExpr, depTable), issueID) if err != nil { if optionalBlockedTable(depTable) && isTableNotExistError(err) { @@ -999,7 +942,7 @@ func isBlockedByInTx(ctx context.Context, tx DBTX, issueID string) (bool, []publ return false, nil, fmt.Errorf("check blockers from %s: %w", depTable, err) } for rows.Next() { - var e blockerEdge + var e depEdge if err := rows.Scan(&e.dependsOnID, &e.depType); err != nil { _ = rows.Close() return false, nil, fmt.Errorf("scan blocker edge: %w", err) @@ -1024,21 +967,7 @@ func isBlockedByInTx(ctx context.Context, tx DBTX, issueID string) (bool, []publ if err != nil { return false, nil, fmt.Errorf("check blocker status: %w", err) } - return true, liveBlockers(edges, statusByID), nil -} - -// blockerEdge is one blocking-type outbound edge of a stored-blocked issue, as -// IsBlockedInTx and IsBlockedWithBlockersBatchInTx read it. -type blockerEdge struct { - dependsOnID, depType string -} - -// liveBlockers keeps the edges whose target is a row that is neither closed nor -// pinned, in edge order. An edge naming no row (an `external:` reference, a -// dangling id) is dropped: the external-dependency policy reports those itself. -// It is the one filter both the single and the batch read apply. -func liveBlockers(edges []blockerEdge, statusByID map[string]types.Status) []publicops.Blocker { - var blockers []publicops.Blocker + var blockers []string for _, e := range edges { status, ok := statusByID[e.dependsOnID] if !ok { @@ -1047,113 +976,14 @@ func liveBlockers(edges []blockerEdge, statusByID map[string]types.Status) []pub if status == types.StatusClosed || status == types.StatusPinned { continue } - blockers = append(blockers, publicops.Blocker{ID: e.dependsOnID, Type: types.DependencyType(e.depType)}) - } - return blockers -} - -// IsBlockedWithBlockersBatchInTx is IsBlockedInTx for many ids in a bounded -// number of reads rather than several per id: the stored is_blocked flags -// (IsBlockedBatchInTx), the blocking-type outbound edges of the ids that flag -// marks blocked from both dependency tables, and one status read of every -// target those edges name. The answer holds exactly the ids IsBlockedInTx -// reports blocked, each mapped to the blocker list IsBlockedInTx returns for -// it (nil when it has none, as for an issue held only by its parent chain); -// an id it reports unblocked, or that names no row, is absent. -// -// The per-id answers are the single read's: the same issues-first flag, the -// same tables in the same order, the same edge order (depends_on_id, type) and -// the same live-blocker filter. Every status is read once for the whole set, -// which is the same row a per-id read would see in the same transaction. -func IsBlockedWithBlockersBatchInTx(ctx context.Context, tx DBTX, ids []string) (map[string][]string, error) { - flags, err := IsBlockedBatchInTx(ctx, tx, ids) - if err != nil { - return nil, err - } - out := make(map[string][]string) - blockedIDs := make([]string, 0, len(flags)) - for _, id := range ids { - if !flags[id] { - continue + if e.depType != "blocks" { + blockers = append(blockers, e.dependsOnID+" ("+e.depType+")") + } else { + blockers = append(blockers, e.dependsOnID) } - if _, dup := out[id]; dup { - continue - } - out[id] = nil - blockedIDs = append(blockedIDs, id) - } - if len(blockedIDs) == 0 { - return out, nil } - edges := make(map[string][]blockerEdge, len(blockedIDs)) - var targets []string - for _, depTable := range []string{"dependencies", "wisp_dependencies"} { - read, err := readBlockerEdgesFromTable(ctx, tx, depTable, blockedIDs, edges) - if err != nil { - if optionalBlockedTable(depTable) && isTableNotExistError(err) { - continue - } - return nil, err - } - targets = append(targets, read...) - } - if len(targets) == 0 { - return out, nil - } - statusByID, err := loadStatusByIDInTx(ctx, tx, targets) - if err != nil { - return nil, fmt.Errorf("check blocker status: %w", err) - } - for _, id := range blockedIDs { - blockers := liveBlockers(edges[id], statusByID) - if len(blockers) == 0 { - continue - } - names := make([]string, 0, len(blockers)) - for _, blocker := range blockers { - names = append(names, blocker.String()) - } - out[id] = names - } - return out, nil -} - -// readBlockerEdgesFromTable appends depTable's blocking-type outbound edges of -// ids to edges, per source in (depends_on_id, type) order, and returns every -// target it read. It batches at queryBatchSize. A missing table's error is -// returned unwrapped enough for the caller's isTableNotExistError. -// -//nolint:gosec // G201: depTable is a hardcoded "dependencies" or "wisp_dependencies". -func readBlockerEdgesFromTable(ctx context.Context, tx DBTX, depTable string, ids []string, edges map[string][]blockerEdge) ([]string, error) { - var targets []string - for start := 0; start < len(ids); start += queryBatchSize { - end := min(start+queryBatchSize, len(ids)) - placeholders, args := buildSQLInClause(ids[start:end]) - rows, err := tx.QueryContext(ctx, fmt.Sprintf(` - SELECT issue_id, %s AS depends_on_id, type FROM %s - WHERE issue_id IN (%s) AND type IN ('blocks', 'waits-for', 'conditional-blocks') - ORDER BY issue_id, depends_on_id, type - `, DepTargetExpr, depTable, placeholders), args...) - if err != nil { - return nil, fmt.Errorf("check blockers from %s: %w", depTable, err) - } - for rows.Next() { - var source string - var e blockerEdge - if err := rows.Scan(&source, &e.dependsOnID, &e.depType); err != nil { - _ = rows.Close() - return nil, fmt.Errorf("scan blocker edge: %w", err) - } - edges[source] = append(edges[source], e) - targets = append(targets, e.dependsOnID) - } - _ = rows.Close() - if err := rows.Err(); err != nil { - return nil, fmt.Errorf("blocker edge rows from %s: %w", depTable, err) - } - } - return targets, nil + return true, blockers, nil } // IsBlockedBatchInTx returns the denormalized, TRANSITIVE is_blocked flag for diff --git a/internal/storage/issueops/dependency_queries_test.go b/internal/storage/issueops/dependency_queries_test.go index 5d7a68888..1e4884881 100644 --- a/internal/storage/issueops/dependency_queries_test.go +++ b/internal/storage/issueops/dependency_queries_test.go @@ -24,11 +24,6 @@ func dependencyRecordsForIssuesQueryRegex(table string) string { regexp.QuoteMeta(table) + ` WHERE issue_id IN \(\?\) ORDER BY issue_id, depends_on_id, type, id` } -func externalBlockingDependencyRecordsQueryRegex(table string) string { - return `(?s)SELECT issue_id, COALESCE\(depends_on_issue_id, depends_on_wisp_id, depends_on_external\) AS depends_on_id, type, created_at, created_by, metadata, thread_id\s+FROM ` + - regexp.QuoteMeta(table) + `\s+WHERE depends_on_external LIKE 'external:%'\s+AND type IN \('blocks', 'conditional-blocks', 'waits-for'\)\s+ORDER BY issue_id` -} - func dependencyRows() *sqlmock.Rows { return sqlmock.NewRows([]string{ "issue_id", @@ -96,35 +91,6 @@ func TestGetAllDependencyRecordsInTxToleratesMissingWispDependencyTable(t *testi } } -func TestGetExternalBlockingDependencyRecordsInTxReadsPermanentAndWispEdges(t *testing.T) { - t.Parallel() - - _, mock, tx := beginMockTx(t) - now := time.Now() - mock.ExpectQuery(externalBlockingDependencyRecordsQueryRegex("dependencies")). - WillReturnRows(dependencyRows().AddRow( - "perm-source", "external:remote:payments", types.DepBlocks, now, "tester", "{}", "", - )) - mock.ExpectQuery(externalBlockingDependencyRecordsQueryRegex("wisp_dependencies")). - WillReturnRows(dependencyRows().AddRow( - "wisp-source", "external:remote:identity", types.DepWaitsFor, now, "tester", "{}", "", - )) - - got, err := GetExternalBlockingDependencyRecordsInTx(context.Background(), tx) - if err != nil { - t.Fatalf("GetExternalBlockingDependencyRecordsInTx: %v", err) - } - if dep := onlyDependency(t, got, "perm-source"); dep.DependsOnID != "external:remote:payments" { - t.Fatalf("permanent external target = %q", dep.DependsOnID) - } - if dep := onlyDependency(t, got, "wisp-source"); dep.DependsOnID != "external:remote:identity" { - t.Fatalf("wisp external target = %q", dep.DependsOnID) - } - if err := mock.ExpectationsWereMet(); err != nil { - t.Fatalf("unmet SQL expectations: %v", err) - } -} - func onlyDependency(t *testing.T, deps map[string][]*types.Dependency, issueID string) *types.Dependency { t.Helper() diff --git a/internal/storage/issueops/derivedid.go b/internal/storage/issueops/derivedid.go index 0abe880d3..a88a2e16f 100644 --- a/internal/storage/issueops/derivedid.go +++ b/internal/storage/issueops/derivedid.go @@ -27,17 +27,8 @@ import ( // what is stored is byte-for-byte what was digested. // AuxTimeLayout renders a UTC timestamp exactly as Dolt renders -// DATE_FORMAT(created_at, '%Y-%m-%d %H:%i:%s') for a DATETIME(0) column — the -// rendering the digest column lists pin explicitly server-side (see -// auxRekeyTables in internal/storage/schema), asserted byte-for-byte against -// this layout by TestAuxTimeDateFormatMatchesGoRendering in -// internal/storage/dolt. It is part of the id derivation and frozen with the -// column lists. -// -// Historically the SQL side read these columns back as CAST(created_at AS CHAR), -// which renders identically for a DATETIME(0) column — but that equality was an -// engine default rather than a pinned contract, which is why the derivation no -// longer names CAST. +// CAST(created_at AS CHAR) for a DATETIME(0) column. It is part of the id +// derivation and frozen with the column lists. const AuxTimeLayout = "2006-01-02 15:04:05" // NowAuxTime returns the current UTC time in AuxTimeLayout. @@ -88,36 +79,6 @@ func firstFreeDerivedID(table, digest string, taken map[string]bool) string { } } -// normalizeAuxEvent applies the insert-time defaults every events-plane mint -// site shares: a missing created_at is stamped now, and on wisp_events unset -// value columns are stored as "" rather than NULL. -func normalizeAuxEvent(table string, e AuxEvent) AuxEvent { - if e.CreatedAt == "" { - e.CreatedAt = NowAuxTime() - } - if table == "wisp_events" { - // wisp_events value columns DEFAULT '' (0021) where events defaults - // them NULL; the pre-derivation mint sites omitted unset columns and - // stored the default. Keep storing "" so the row — and its digest — - // matches legacy wisp rows and the rekey pass's re-derivation. - for _, p := range []*sql.NullString{&e.OldValue, &e.NewValue, &e.Comment} { - if !p.Valid { - *p = str("") - } - } - } - return e -} - -// auxEventDigest is the content digest of a normalized events-plane row, in -// the frozen digest column order. -func auxEventDigest(e AuxEvent) string { - return rowid.Digest([]sql.NullString{ - str(e.IssueID), str(string(e.EventType)), str(e.Actor), - e.OldValue, e.NewValue, e.Comment, str(e.CreatedAt), - }) -} - // InsertDerivedEvent inserts one events-plane row under its content-derived // id. Exact-duplicate rows already in table (same digest) push the new row to // the next free ordinal, preserving local multiplicity; across replicas an @@ -147,8 +108,24 @@ func InsertDerivedEvent(ctx context.Context, tx DBTX, table string, e AuxEvent) // //nolint:gosec // G201: table is a hardcoded routing constant at every call site. func InsertDerivedEventReturningID(ctx context.Context, tx DBTX, table string, e AuxEvent) (string, error) { - e = normalizeAuxEvent(table, e) - digest := auxEventDigest(e) + if e.CreatedAt == "" { + e.CreatedAt = NowAuxTime() + } + if table == "wisp_events" { + // wisp_events value columns DEFAULT '' (0021) where events defaults + // them NULL; the pre-derivation mint sites omitted unset columns and + // stored the default. Keep storing "" so the row — and its digest — + // matches legacy wisp rows and the rekey pass's re-derivation. + for _, p := range []*sql.NullString{&e.OldValue, &e.NewValue, &e.Comment} { + if !p.Valid { + *p = str("") + } + } + } + digest := rowid.Digest([]sql.NullString{ + str(e.IssueID), str(string(e.EventType)), str(e.Actor), + e.OldValue, e.NewValue, e.Comment, str(e.CreatedAt), + }) taken := make(map[string]bool) rows, err := tx.QueryContext(ctx, fmt.Sprintf(` SELECT id FROM %s diff --git a/internal/storage/issueops/epic_closure.go b/internal/storage/issueops/epic_closure.go index e38afa9a0..ede8f3b50 100644 --- a/internal/storage/issueops/epic_closure.go +++ b/internal/storage/issueops/epic_closure.go @@ -7,8 +7,7 @@ import ( "github.com/steveyegge/beads/internal/types" ) -// GetEpicsEligibleForClosureInTx returns open epics whose children are all closed -// with completing close reasons (not duplicate/wontfix/superseded). +// GetEpicsEligibleForClosureInTx returns epics whose children are all closed. // nolint:gosec // G201: table names are hardcoded, placeholders contain only ? markers func GetEpicsEligibleForClosureInTx(ctx context.Context, tx DBTX) ([]*types.EpicStatus, error) { // Step 1: Get open epic IDs (single-table scan) @@ -67,19 +66,14 @@ func GetEpicsEligibleForClosureInTx(ctx context.Context, tx DBTX) ([]*types.Epic depRows.Close() } - // Step 3: Batch-fetch statuses + close_reason for all child issues (bd-w2w). - // close_reason is required so duplicate/wontfix/superseded closes do not - // count as "complete" for EligibleForClose (GH#5026). + // Step 3: Batch-fetch statuses for all child issues across all epics allChildIDs := make([]string, 0) for _, children := range epicChildMap { allChildIDs = append(allChildIDs, children...) } - type childCloseInfo struct { - status string - closeReason string - } - childInfoMap := make(map[string]childCloseInfo) + childStatusMap := make(map[string]string) if len(allChildIDs) > 0 { + // Check both issues and wisps tables for child statuses (bd-w2w) for _, table := range []string{"issues", "wisps"} { for start := 0; start < len(allChildIDs); start += queryBatchSize { end := start + queryBatchSize @@ -89,10 +83,7 @@ func GetEpicsEligibleForClosureInTx(ctx context.Context, tx DBTX) ([]*types.Epic batch := allChildIDs[start:end] placeholders, args := buildSQLInClause(batch) - statusQuery := fmt.Sprintf( - "SELECT id, status, COALESCE(close_reason, '') FROM %s WHERE id IN (%s)", - table, placeholders, - ) + statusQuery := fmt.Sprintf("SELECT id, status FROM %s WHERE id IN (%s)", table, placeholders) statusRows, err := tx.QueryContext(ctx, statusQuery, args...) if err != nil { if isTableNotExistError(err) { @@ -101,12 +92,12 @@ func GetEpicsEligibleForClosureInTx(ctx context.Context, tx DBTX) ([]*types.Epic return nil, fmt.Errorf("failed to batch-fetch child statuses from %s: %w", table, err) } for statusRows.Next() { - var id, status, closeReason string - if err := statusRows.Scan(&id, &status, &closeReason); err != nil { + var id, status string + if err := statusRows.Scan(&id, &status); err != nil { statusRows.Close() return nil, fmt.Errorf("scan child status: %w", err) } - childInfoMap[id] = childCloseInfo{status: status, closeReason: closeReason} + childStatusMap[id] = status } statusRows.Close() } @@ -144,26 +135,17 @@ func GetEpicsEligibleForClosureInTx(ctx context.Context, tx DBTX) ([]*types.Epic totalChildren := len(children) closedChildren := 0 - completingClosed := 0 for _, childID := range children { - info, ok := childInfoMap[childID] - if !ok || types.Status(info.status) != types.StatusClosed { - continue - } - closedChildren++ - if !types.IsNonCompletingClose(info.closeReason) { - completingClosed++ + if status, ok := childStatusMap[childID]; ok && types.Status(status) == types.StatusClosed { + closedChildren++ } } - // Eligible only when every child is closed AND every close is a completing - // one (finished work). Duplicate/wontfix/superseded closes leave the - // epic's real scope unfinished (GH#5026). results = append(results, &types.EpicStatus{ Epic: issue, TotalChildren: totalChildren, ClosedChildren: closedChildren, - EligibleForClose: totalChildren > 0 && totalChildren == completingClosed, + EligibleForClose: totalChildren > 0 && totalChildren == closedChildren, }) } diff --git a/internal/storage/issueops/epoch_cas.go b/internal/storage/issueops/epoch_cas.go deleted file mode 100644 index 994334ac3..000000000 --- a/internal/storage/issueops/epoch_cas.go +++ /dev/null @@ -1,680 +0,0 @@ -package issueops - -import ( - "context" - "database/sql" - "errors" - "fmt" - "math" - "strings" - "time" - - "github.com/steveyegge/beads/internal/storage/dberrors" -) - -// This file implements R20 epoch-transition enforcement (gastownhall/beads#5898 -// revision 9, #6136): a store-wide epoch generation -// counter (store_epoch, migration 0067) plus a durable record of the -// addresses minted under each generation (epoch_minted_addresses, migration -// 0072), used to answer whether a previously-minted address is still served -// by the store's current epoch. It adds no RetentionFixture/R17 resolve, -// remove, hold, force-remove, erase, or mint logic — R20 epoch reasoning is -// evaluated entirely on its own (out of scope for this slice). -// -// ALL THREE LEGS SHARE THIS BODY, the same way they share -// CompareAndSetMetadataKeyInTx: the two Dolt-backed stores wrap it in their -// own transaction, and the unit-of-work provider reaches it through its own -// leg-specific adapter. Each leg's own contract-test file is the -// translation boundary to and from backend/conformance's -// Address/EpochBumpTrigger/RetentionAnswer vocabulary — this file knows -// nothing about the conformance package. -// -// ADDRESSES ARE NEVER RECOMPUTED FROM store_epoch. Each is a deterministic -// token over (storeID, id, the epoch current at mint time) — see -// epochAddress — persisted once at mint: a later mint of the same id under a -// bumped epoch produces a DIFFERENT address and a new row, and every -// address's row survives to answer StillServes/Resolve after the epoch moves -// on. -// -// SURVIVAL IS RECORDED ON THE ROW, NEVER INFERRED (gastownhall/beads#6664, -// bee-ghosttrack review 5360880888, Major 1). An address is served unless its -// own row says gone_at_epoch, and a row minted in an epoch that has not -// happened yet is not served: the answer is a function of the address's row -// and the current epoch, and nothing reads another row to reach it. Gone is -// terminal. Two writers set gone_at_epoch, both guarded by IS NULL so that -// nothing already lost is touched and nothing is ever cleared: -// LoseVersionInTx, for a Version the store lost, and BumpEpochCarryingInTx, -// for the losing lineage when two live lineages of one id meet at a -// token-scheme change. -// -// A restore or a destructive reinit moves the counter and writes no address -// row (BumpEpochInTx), so every address that was served stays Live at its own -// address with no re-mint. A token-scheme change is the one bump that writes -// address rows (BumpEpochCarryingInTx): it carries every live lineage to the -// new epoch with one carry row whose carried_from names the lineage's ROOT -// address, never the previous carry, so lineages stay flat and any member of -// one reaches its newest address in a single indexed lookup. -// -// THE EPOCH COUNTER IS PER DATABASE, NOT PER STORE (gastownhall/beads#6664, -// bee-ghosttrack review 5360880888, Minor 3). store_epoch has no store_id -// column, and storeID appears only in error text: one bump advances the epoch -// seen through every storeID that shares the database. There is deliberately -// no storeID guard in this body, because the conformance suite uses several -// synthetic storeIDs in one database. -// -// epochAddress'S ENCODING IS LENGTH-PREFIXED, NOT BARE-COLON-JOINED -// (gastownhall/beads#6664, bee-ghosttrack review 5268699223, item B2): a -// colon inside storeID or id can no longer shift the (storeID, id, epoch) -// field boundary onto a different triple the way the old "epch:%s:%s:%d" -// form allowed. MintUnderEpochInTx additionally refuses a storeID or id -// containing ":" outright (validateEpochAddressInputs) so an address stays -// readable by eye without needing to count length prefixes — belt and -// braces, not a correctness dependency of epochAddress itself. Given that, -// upsertEpochMintedAddressInTx's exists branch is a pure no-op: an -// address's row existing now provably means its stored triple already -// matches the incoming one, so there is nothing left to write. - -// EpochRestriction is this file's own local answer vocabulary for R20 — -// deliberately not backend/conformance.Restriction (see package doc above). -// It omits GoneRetention/GoneErasure: those are RetentionFixture/R17 -// outcomes this slice does not produce. -type EpochRestriction int - -const ( - EpochRestrictionLive EpochRestriction = iota - EpochRestrictionGoneReorganization - EpochRestrictionUnknown -) - -// EpochResolveResult is ResolveEpochInTx's answer. Epoch is non-nil exactly -// when the answer required comparing an epoch (Live and GoneReorganization); -// it is nil for Unknown, which never looks store_epoch up at all. -type EpochResolveResult struct { - Restriction EpochRestriction - ProducingStore string - Epoch *int -} - -// ErrEpochAddressNotFound means the address named was never minted by -// storeID — distinct from an ordinary "gone" answer, which requires having -// minted the address in the first place. -var ErrEpochAddressNotFound = errors.New("epoch CAS: address not minted by this store") - -// ErrEpochAddressNotServed means the address exists in this store but the -// store does not serve it: its lineage was lost, or it was minted in an epoch -// that has not happened yet. It is distinct from ErrEpochAddressNotFound so a -// caller can tell an address the store never minted from one it minted and no -// longer serves. -var ErrEpochAddressNotServed = errors.New("epoch CAS: address exists in this store but is not served") - -// ErrEpochAddressSeparator means a storeID or id passed to -// MintUnderEpochInTx contains ":", the character epochAddress's encoding -// uses as a field delimiter (gastownhall/beads#6664, bee-ghosttrack review -// 5268699223, item B2b). epochAddress's length-prefixed encoding does not -// actually depend on this for correctness (see epochAddress's own doc -// comment), but refusing it outright keeps a minted address readable by eye. -var ErrEpochAddressSeparator = errors.New("epoch CAS: storeID or id contains \":\", epochAddress's field separator") - -// ErrEpochAddressTooLong means a storeID and id compose an address that -// epoch_minted_addresses.address, a VARCHAR(255) PRIMARY KEY (migration 0072), -// may not hold. MintUnderEpochInTx refuses such a pair before it reads or -// writes anything, so the caller gets this error instead of a raw driver error -// from the INSERT. The bound is charged at the widest epoch the INT counter can -// reach, not the current one: a token-scheme change re-encodes every live pair -// at the new epoch (BumpEpochCarryingInTx), so a pair that fit at epoch 9 must -// still fit at epoch 10, or the carry, and with it every later scheme change, -// would fail database-wide. -var ErrEpochAddressTooLong = errors.New("epoch CAS: storeID and id compose an address longer than epoch_minted_addresses.address holds") - -// maxEpochAddressLen is the width of epoch_minted_addresses.address, -// VARCHAR(255). The check against it counts bytes, which are never fewer than -// the characters the column counts, so it refuses no later than the column -// would. -const maxEpochAddressLen = 255 - -// epochAddress is a minted address: a deterministic token over storeID, id, -// and the epoch current at mint time, so re-minting the same id under the -// same epoch always reproduces the same address. storeID and id are -// length-prefixed, not just colon-joined (gastownhall/beads#6664, -// bee-ghosttrack review 5268699223, item B2a): reading exactly len(storeID) -// bytes for the first field and len(id) bytes for the second makes the -// (storeID, id) boundary unambiguous no matter what characters either one -// contains, so two different triples can never collide on the same address. -// MintUnderEpochInTx also refuses a storeID or id containing ":" outright -// (validateEpochAddressInputs, item B2b) so an address stays readable by -// eye without relying on that. -func epochAddress(storeID, id string, epoch int) string { - return fmt.Sprintf("epch:%d:%s:%d:%s:%d", len(storeID), storeID, len(id), id, epoch) -} - -// validateEpochAddressInputs rejects a storeID or id containing ":" before -// MintUnderEpochInTx mints an address from them (item B2b above), and a pair -// whose address would not fit the address column at the widest epoch -// (ErrEpochAddressTooLong). -func validateEpochAddressInputs(storeID, id string) error { - if strings.Contains(storeID, ":") { - return fmt.Errorf("%w: storeID %q", ErrEpochAddressSeparator, storeID) - } - if strings.Contains(id, ":") { - return fmt.Errorf("%w: id %q", ErrEpochAddressSeparator, id) - } - if n := len(epochAddress(storeID, id, math.MaxInt32)); n > maxEpochAddressLen { - return fmt.Errorf("%w: %d bytes at the widest epoch, limit %d", ErrEpochAddressTooLong, n, maxEpochAddressLen) - } - return nil -} - -// ensureStoreEpochRow lazily initializes store_epoch's singleton row -// (migration 0067; the table starts empty) to epoch 1 on first use, and -// reports the current epoch either way. Only BumpEpochInTx and -// MintUnderEpochInTx call this: both are about to write regardless, so -// creating the row here is not a surprising extra side effect. Every -// read-only path calls readStoreEpochWithoutSeedingInTx instead -// (gastownhall/beads#6664, bee-ghosttrack review 5268699223, item B4): a write -// from what callers reasonably expect to be a read is surprising at best, and -// a hard failure against a genuinely read-only connection at worst. -// -// The seed follows version_history.go's seed of the same row (gastownhall/ -// beads#6664, bee-ghosttrack review 5360880888): read first, so the INSERT -// happens once per store rather than once per mint; INSERT IGNORE, so a row -// that turns up between the read and the write is not an error; and read the -// row back, so the epoch reported is what the row holds even when the seed was -// ignored, not the 1 it would have written. -func ensureStoreEpochRow(ctx context.Context, tx DBTX) (int, error) { - var epoch int - err := tx.QueryRowContext(ctx, `SELECT epoch FROM store_epoch WHERE id = 1`).Scan(&epoch) - if errors.Is(err, sql.ErrNoRows) { - if _, err := tx.ExecContext(ctx, `INSERT IGNORE INTO store_epoch (id, epoch) VALUES (1, 1)`); err != nil { - return 0, fmt.Errorf("initialize store_epoch: %w", err) - } - err = tx.QueryRowContext(ctx, `SELECT epoch FROM store_epoch WHERE id = 1`).Scan(&epoch) - } - if err != nil { - return 0, fmt.Errorf("read store_epoch: %w", err) - } - return epoch, nil -} - -// EpochMintDirtyTables names the tables a mint (MintUnderEpochInTx) can leave -// modified, for the leg that publishes them to Dolt history. A token-scheme -// bump (BumpEpochCarryingInTx) leaves the same two tables modified. store_epoch is in the set although a mint -// never updates it: ensureStoreEpochRow INSERTs the singleton row on first use, -// so a mint that published only epoch_minted_addresses would leave that row -// modified and uncommitted, and the Dolt commit would hold minted rows without -// the epoch row they depend on (gastownhall/beads#6664 Major 2, bee-ghosttrack -// review 5360880888). Staging a table that turns out to be clean is free. -func EpochMintDirtyTables() ChangedTables { - return ChangedTables{"epoch_minted_addresses": true, "store_epoch": true} -} - -// readStoreEpochWithoutSeedingInTx reports the current epoch without writing: -// store_epoch's singleton row starting absent (migration 0067) and its -// epoch being 1 are the same state, so a read-only caller can answer from -// that default instead of initializing the row the way ensureStoreEpochRow -// does (item B4 above). It is deliberately not version_history.go's -// readStoreEpochInTx, which seeds the row when it is absent. CurrentEpochInTx, -// StillServesInTx, ResolveEpochInTx, CurrentAddressForInTx and -// LoseVersionInTx all read this way; only BumpEpochInTx and -// MintUnderEpochInTx, which are about to write regardless, use -// ensureStoreEpochRow. -func readStoreEpochWithoutSeedingInTx(ctx context.Context, tx DBTX) (int, error) { - var epoch int - err := tx.QueryRowContext(ctx, `SELECT epoch FROM store_epoch WHERE id = 1`).Scan(&epoch) - if errors.Is(err, sql.ErrNoRows) { - return 1, nil - } - if err != nil { - return 0, fmt.Errorf("read store_epoch: %w", err) - } - return epoch, nil -} - -// epochMintedAddress is one row as read from epoch_minted_addresses. -// carriedFrom is NULL for a root row (a fresh mint) and otherwise the address of -// the lineage's root; goneAtEpoch is NULL while the store serves the row and -// otherwise the epoch it was lost in. -type epochMintedAddress struct { - storeID string - mintedID string - mintedEpoch int - carriedFrom sql.NullString - goneAtEpoch sql.NullInt64 -} - -// served reports whether the store serves the row at epoch. A row is served -// unless it was lost, and a row minted in an epoch that has not happened yet -// fails closed rather than being trusted. -func (r epochMintedAddress) served(epoch int) bool { - return !r.goneAtEpoch.Valid && r.mintedEpoch <= epoch -} - -// lineageRoot is the address every member of the row's lineage names: the -// row's carried_from when it is a carry row, and its own address when it is a -// root. -func (r epochMintedAddress) lineageRoot(address string) string { - if r.carriedFrom.Valid { - return r.carriedFrom.String - } - return address -} - -// readEpochMintedAddressInTx reads address's row, if any. found is false -// (with a zero-value row and nil error) when address was never minted — -// distinguishing "not found" from an actual query error is the caller's -// job, the same way readExpectedRevisionRowInTx's callers do it. -func readEpochMintedAddressInTx(ctx context.Context, tx DBTX, address string) (epochMintedAddress, bool, error) { - var row epochMintedAddress - err := tx.QueryRowContext(ctx, - `SELECT store_id, minted_id, minted_epoch, carried_from, gone_at_epoch FROM epoch_minted_addresses WHERE address = ?`, address, - ).Scan(&row.storeID, &row.mintedID, &row.mintedEpoch, &row.carriedFrom, &row.goneAtEpoch) - if errors.Is(err, sql.ErrNoRows) { - return epochMintedAddress{}, false, nil - } - if err != nil { - return epochMintedAddress{}, false, fmt.Errorf("epoch CAS: read minted address %s: %w", address, err) - } - return row, true, nil -} - -// upsertEpochMintedAddressInTx writes address's row. exists (from the -// caller's own prior read) says whether one already does: if so, this is a -// pure no-op (gastownhall/beads#6664, bee-ghosttrack review 5268699223, -// item B2c) — epochAddress is now injective (item B2a), so an existing row -// at this exact address provably already carries this exact (storeID, -// mintedID, mintedEpoch) triple, and the old exists-branch UPDATE (which -// had no store_id/minted_id guard) could only ever have mattered by -// rewriting a DIFFERENT triple's row out from under it on an address -// collision the new encoding no longer allows. -// -// A duplicate-key error from the INSERT is NOT success. It means a row the -// caller's read did not find: another transaction inserted it, and that -// transaction or a later one may already have lost it, so taking the error as -// the benign race of two minters inserting the same row (item B3) could hand -// back an address that reads Gone. The row is read again and trusted only as -// far as this transaction can see it: served at mintedEpoch, the mint stands, -// because the row is the one it would have inserted; present but not served, -// the mint fails with ErrEpochAddressNotServed, as it does when the caller's -// own read finds such a row; not visible at all, the mint fails with the -// duplicate-key error, and a new transaction, which reads the committed row, -// answers it. -// -// On Dolt, the only supported backend, the INSERT does not hit that error for -// a row committed after the transaction's snapshot: it checks the snapshot, -// and the two inserts of one address conflict at COMMIT instead (Error 1213), -// which withRetryTx answers by running the mint again against the committed -// row (TestAMintRacingTheMintAndLossOfItsAddressConflictsAtCommit in the dolt -// package). FOR UPDATE is a parse-only no-op there, so no locking read could -// see further than the re-read does. -func upsertEpochMintedAddressInTx(ctx context.Context, tx DBTX, address, storeID, mintedID string, mintedEpoch int, exists bool) error { - if exists { - return nil - } - _, err := tx.ExecContext(ctx, - `INSERT INTO epoch_minted_addresses (address, store_id, minted_id, minted_epoch, minted_at) VALUES (?, ?, ?, ?, ?)`, - address, storeID, mintedID, mintedEpoch, time.Now().UTC(), - ) - if err == nil { - return nil - } - if !dberrors.IsDuplicateKey(err) { - return fmt.Errorf("epoch CAS: insert minted address %s: %w", address, err) - } - row, found, readErr := readEpochMintedAddressInTx(ctx, tx, address) - switch { - case readErr != nil: - return errors.Join(fmt.Errorf("epoch CAS: insert minted address %s: %w", address, err), readErr) - case !found: - return fmt.Errorf("epoch CAS: insert minted address %s: another transaction holds a row at it that this one cannot read, so whether the store serves it is unknown: %w", address, err) - case !row.served(mintedEpoch): - return fmt.Errorf("epoch CAS: mint %s under epoch for %s: %w: %s", mintedID, storeID, ErrEpochAddressNotServed, address) - } - return nil -} - -// CurrentEpochInTx reports storeID's current epoch generation. store_epoch -// has no store_id column: migration 0067 established it as one physical -// counter per database, matching production reality (one logical store per -// database already) — storeID is accepted here only to satisfy -// EpochFixture's hook signature. -// -// The epoch counter is per database, not per store. storeID appears only in -// error text; one bump advances the epoch seen through every storeID that -// shares this database. -func CurrentEpochInTx(ctx context.Context, tx DBTX, storeID string) (int, error) { - epoch, err := readStoreEpochWithoutSeedingInTx(ctx, tx) - if err != nil { - return 0, fmt.Errorf("epoch CAS: current epoch for %s: %w", storeID, err) - } - return epoch, nil -} - -// BumpEpochInTx advances storeID's epoch generation by one and records why -// (R20-a: a bump is triggered only by restore, destructive-reinit, or -// token-scheme-change — the leg adapter converts the fixture's -// conformance.EpochBumpTrigger to this plain string via trigger.String(), -// keeping that vocabulary out of this file per the package doc above). -// -// It writes store_epoch and nothing else: a restore or a destructive reinit -// leaves every address row alone, so every address that was served stays Live -// at its own address. A token-scheme change, which does write address rows, -// calls BumpEpochCarryingInTx instead. -// -// The epoch counter is per database, not per store. storeID appears only in -// error text; one bump advances the epoch seen through every storeID that -// shares this database. -func BumpEpochInTx(ctx context.Context, tx DBTX, storeID, reason string) (int, error) { - if _, err := ensureStoreEpochRow(ctx, tx); err != nil { - return 0, fmt.Errorf("epoch CAS: bump epoch for %s: %w", storeID, err) - } - if _, err := tx.ExecContext(ctx, - `UPDATE store_epoch SET epoch = epoch + 1, bumped_at = ?, bumped_reason = ? WHERE id = 1`, - time.Now().UTC(), reason, - ); err != nil { - return 0, fmt.Errorf("epoch CAS: bump epoch for %s: %w", storeID, err) - } - var newEpoch int - if err := tx.QueryRowContext(ctx, `SELECT epoch FROM store_epoch WHERE id = 1`).Scan(&newEpoch); err != nil { - return 0, fmt.Errorf("epoch CAS: read epoch after bump for %s: %w", storeID, err) - } - return newEpoch, nil -} - -// BumpEpochCarryingInTx is BumpEpochInTx for a token-scheme change, the one -// trigger that changes how addresses are encoded. It advances the epoch exactly -// as BumpEpochInTx does and also records the carry: every lineage the store -// still serves gets one new row at the new epoch, whose address is the -// deterministic token for (store, id, new epoch) and whose carried_from names -// the lineage's ROOT address. The old addresses keep resolving, and -// CurrentAddressForInTx reports the new one from any member of the lineage. -// -// The carry is DATABASE-WIDE on purpose. The counter is per database, so this -// bump moves the epoch every storeID sees, and carrying only storeID's rows -// would strand every other store's addresses at the scheme boundary. storeID is -// used for error text only, exactly as in BumpEpochInTx. It costs one read of -// every live row and one insert per live id, in one transaction: acceptable -// because a scheme change is a once-per-change migration, not a runtime path. -// -// Two live lineages of one id cannot both receive an address, because an -// address is a function of (store, id, epoch) and so is one per id per epoch. -// The lineage holding the newest row is carried, and every other lineage of -// that id is marked gone at the new epoch (the safe direction: over-void, never -// revive). Lineages already lost are not read, so they are never carried, and a -// row minted in an epoch that has not happened yet is not read either: it stays -// not served. A row already sitting at a carry's address means the counter -// rewound, so that is a failure of the bump: no re-read can settle it the way -// a mint settles a duplicate key (upsertEpochMintedAddressInTx). -// -// CALLERS MUST HOLD THE WORKSPACE GATE (internal/workspacegate) EXCLUSIVELY FOR -// THE BUMP, as a restore does. This is the one epoch operation that needs -// mints excluded by contract: the store cannot make a mint conflict with a bump -// (a mint has no cell of its own to change), so a mint that commits after this -// bump read the live rows would leave a live row that was never carried. -func BumpEpochCarryingInTx(ctx context.Context, tx DBTX, storeID, reason string) (int, error) { - newEpoch, err := BumpEpochInTx(ctx, tx, storeID, reason) - if err != nil { - return 0, err - } - candidates, err := readEpochCarryCandidatesInTx(ctx, tx, newEpoch) - if err != nil { - return 0, fmt.Errorf("epoch CAS: carry addresses to epoch %d for %s: %w", newEpoch, storeID, err) - } - - // The candidates are ordered newest first within each (store, id), so the - // first row of a group holds the lineage that wins it. - type lineageKey struct{ storeID, mintedID string } - var groups []lineageKey - winningRoot := map[lineageKey]string{} - type losingLineage struct{ storeID, root string } - var losers []losingLineage - seenLoser := map[losingLineage]bool{} - for _, c := range candidates { - key := lineageKey{c.storeID, c.mintedID} - winner, seen := winningRoot[key] - if !seen { - winningRoot[key] = c.root() - groups = append(groups, key) - continue - } - if loser := (losingLineage{c.storeID, c.root()}); c.root() != winner && !seenLoser[loser] { - seenLoser[loser] = true - losers = append(losers, loser) - } - } - - for _, loser := range losers { - if err := loseLineageInTx(ctx, tx, loser.storeID, loser.root, newEpoch); err != nil { - return 0, fmt.Errorf("epoch CAS: carry addresses to epoch %d for %s: %w", newEpoch, storeID, err) - } - } - for _, key := range groups { - address := epochAddress(key.storeID, key.mintedID, newEpoch) - if _, err := tx.ExecContext(ctx, - `INSERT INTO epoch_minted_addresses (address, store_id, minted_id, minted_epoch, minted_at, carried_from) VALUES (?, ?, ?, ?, ?, ?)`, - address, key.storeID, key.mintedID, newEpoch, time.Now().UTC(), winningRoot[key], - ); err != nil { - return 0, fmt.Errorf("epoch CAS: carry %s to epoch %d for %s: %w", key.mintedID, newEpoch, storeID, err) - } - } - return newEpoch, nil -} - -// epochCarryCandidate is one live row below the new epoch, as -// BumpEpochCarryingInTx reads it. -type epochCarryCandidate struct { - address string - storeID string - mintedID string - carriedFrom sql.NullString -} - -// root is the address of the candidate's lineage root. -func (c epochCarryCandidate) root() string { - if c.carriedFrom.Valid { - return c.carriedFrom.String - } - return c.address -} - -// readEpochCarryCandidatesInTx reads every row the store still serves below -// newEpoch, newest first within each (store, id). Rows already lost are not -// read, and neither are rows minted at or above newEpoch: those are anomalies -// and stay not served. The result is read to the end before any other statement -// runs on the transaction. -func readEpochCarryCandidatesInTx(ctx context.Context, tx DBTX, newEpoch int) ([]epochCarryCandidate, error) { - rows, err := tx.QueryContext(ctx, - `SELECT address, store_id, minted_id, carried_from FROM epoch_minted_addresses WHERE gone_at_epoch IS NULL AND minted_epoch < ? ORDER BY store_id, minted_id, minted_epoch DESC`, - newEpoch, - ) - if err != nil { - return nil, fmt.Errorf("read live addresses: %w", err) - } - defer func() { _ = rows.Close() }() - var candidates []epochCarryCandidate - for rows.Next() { - var c epochCarryCandidate - if err := rows.Scan(&c.address, &c.storeID, &c.mintedID, &c.carriedFrom); err != nil { - return nil, fmt.Errorf("scan live address: %w", err) - } - candidates = append(candidates, c) - } - if err := rows.Err(); err != nil { - return nil, fmt.Errorf("read live addresses: %w", err) - } - return candidates, nil -} - -// loseLineageInTx marks the lineage rooted at root gone at epoch: the root's own -// row, then every carry row that names it. Each statement is guarded by -// gone_at_epoch IS NULL, so a row already lost is never touched and nothing is -// ever cleared. It is the only place gone_at_epoch is written, shared by -// LoseVersionInTx and BumpEpochCarryingInTx. -func loseLineageInTx(ctx context.Context, tx DBTX, storeID, root string, epoch int) error { - if _, err := tx.ExecContext(ctx, - `UPDATE epoch_minted_addresses SET gone_at_epoch = ? WHERE address = ? AND store_id = ? AND gone_at_epoch IS NULL`, - epoch, root, storeID, - ); err != nil { - return fmt.Errorf("mark %s lost at epoch %d: %w", root, epoch, err) - } - if _, err := tx.ExecContext(ctx, - `UPDATE epoch_minted_addresses SET gone_at_epoch = ? WHERE carried_from = ? AND store_id = ? AND gone_at_epoch IS NULL`, - epoch, root, storeID, - ); err != nil { - return fmt.Errorf("mark the carries of %s lost at epoch %d: %w", root, epoch, err) - } - return nil -} - -// MintUnderEpochInTx mints id's address under storeID's CURRENT epoch, -// deterministically (epochAddress): minting the same id again under the -// same epoch reproduces the same address and is an idempotent no-op upsert -// of the same row. -// -// A mint only ever INSERTS its own root row. It never updates another row, so -// no mint can retire or revive an address, and it never hands back an address -// the store does not serve: a row already sitting at the computed address is -// only possible when the lineage was minted in this epoch and lost in this same -// epoch, and the mint then returns ErrEpochAddressNotServed instead of an -// address that reads Gone. That includes a row another transaction inserts -// after this one read: on Dolt the two inserts conflict at COMMIT and the mint -// runs again against the committed row, and an INSERT that reports the row as -// a duplicate key reads it again first (upsertEpochMintedAddressInTx). -// -// A storeID or id containing ":" is refused (ErrEpochAddressSeparator), and so -// is a pair whose address could outgrow the address column at some epoch -// (ErrEpochAddressTooLong), before anything is read or written. -func MintUnderEpochInTx(ctx context.Context, tx DBTX, storeID, id string) (string, error) { - if err := validateEpochAddressInputs(storeID, id); err != nil { - return "", fmt.Errorf("epoch CAS: mint %s under epoch for %s: %w", id, storeID, err) - } - epoch, err := ensureStoreEpochRow(ctx, tx) - if err != nil { - return "", fmt.Errorf("epoch CAS: mint %s under epoch for %s: %w", id, storeID, err) - } - address := epochAddress(storeID, id, epoch) - row, found, err := readEpochMintedAddressInTx(ctx, tx, address) - if err != nil { - return "", err - } - if found && !row.served(epoch) { - return "", fmt.Errorf("epoch CAS: mint %s under epoch for %s: %w: %s", id, storeID, ErrEpochAddressNotServed, address) - } - if err := upsertEpochMintedAddressInTx(ctx, tx, address, storeID, id, epoch, found); err != nil { - return "", err - } - return address, nil -} - -// StillServesInTx reports whether address is still served under storeID's -// CURRENT epoch. The answer is read from the address's own row and the epoch and -// from nothing else: the row's gone_at_epoch says whether the store lost it, and -// a row minted in an epoch that has not happened yet is not served. An address -// that survived a restore or a destructive reinit therefore stays served with no -// upkeep, and an address from a different store, or one never minted, is not -// served either. -func StillServesInTx(ctx context.Context, tx DBTX, storeID, address string) (bool, error) { - row, found, err := readEpochMintedAddressInTx(ctx, tx, address) - if err != nil { - return false, err - } - if !found || row.storeID != storeID { - return false, nil - } - epoch, err := readStoreEpochWithoutSeedingInTx(ctx, tx) - if err != nil { - return false, fmt.Errorf("epoch CAS: still serves %s for %s: %w", address, storeID, err) - } - return row.served(epoch), nil -} - -// ResolveEpochInTx answers R20's epoch-only restriction for address: Live while -// the store serves it, GoneReorganization once the store lost it (a -// reorganization, not a retention or erasure outcome; RetentionFixture/R17 -// states are out of scope here), and Unknown for an address this store never -// minted. Like StillServesInTx it reads only the address's own row and the -// epoch. ProducingStore is always storeID: this file has no lineage/replica -// model to attribute a foreign store to (unlike RetentionFixture's cross-store -// answers). -func ResolveEpochInTx(ctx context.Context, tx DBTX, storeID, address string) (EpochResolveResult, error) { - row, found, err := readEpochMintedAddressInTx(ctx, tx, address) - if err != nil { - return EpochResolveResult{}, err - } - if !found || row.storeID != storeID { - return EpochResolveResult{Restriction: EpochRestrictionUnknown, ProducingStore: storeID}, nil - } - epoch, err := readStoreEpochWithoutSeedingInTx(ctx, tx) - if err != nil { - return EpochResolveResult{}, fmt.Errorf("epoch CAS: resolve %s for %s: %w", address, storeID, err) - } - if row.served(epoch) { - return EpochResolveResult{Restriction: EpochRestrictionLive, ProducingStore: storeID, Epoch: &epoch}, nil - } - return EpochResolveResult{Restriction: EpochRestrictionGoneReorganization, ProducingStore: storeID, Epoch: &epoch}, nil -} - -// CurrentAddressForInTx reports the address oldAddress now resolves to: the -// newest address of its lineage, or oldAddress itself when nothing carried the -// Version to a new address. It only reads. It executes no INSERT, UPDATE or -// DELETE, because a lookup that minted an address could make a lost address -// look served again (gastownhall/beads#6664, bee-ghosttrack review 5360880888, -// Major 1), and both Dolt legs run it on a read-write transaction that is -// always rolled back, so a stray write would vanish silently instead of -// failing. oldAddress must be one storeID minted (ErrEpochAddressNotFound -// otherwise) and still serves (ErrEpochAddressNotServed otherwise). -func CurrentAddressForInTx(ctx context.Context, tx DBTX, storeID, oldAddress string) (string, error) { - row, found, err := readEpochMintedAddressInTx(ctx, tx, oldAddress) - if err != nil { - return "", err - } - if !found || row.storeID != storeID { - return "", fmt.Errorf("epoch CAS: current address for %s: %w: %s", storeID, ErrEpochAddressNotFound, oldAddress) - } - epoch, err := readStoreEpochWithoutSeedingInTx(ctx, tx) - if err != nil { - return "", fmt.Errorf("epoch CAS: current address for %s: %w", storeID, err) - } - if !row.served(epoch) { - return "", fmt.Errorf("epoch CAS: current address for %s: %w: %s", storeID, ErrEpochAddressNotServed, oldAddress) - } - var newest string - err = tx.QueryRowContext(ctx, - `SELECT address FROM epoch_minted_addresses WHERE carried_from = ? AND store_id = ? AND gone_at_epoch IS NULL AND minted_epoch <= ? ORDER BY minted_epoch DESC LIMIT 1`, - row.lineageRoot(oldAddress), storeID, epoch, - ).Scan(&newest) - if errors.Is(err, sql.ErrNoRows) { - return oldAddress, nil - } - if err != nil { - return "", fmt.Errorf("epoch CAS: current address for %s: read the newest carry of %s: %w", storeID, oldAddress, err) - } - return newest, nil -} - -// LoseVersionInTx records that storeID no longer serves the Version named by -// address: it marks the lineage ROOT's own row and every carry row of that -// root gone at the current epoch, so every address of the Version resolves -// GoneReorganization from then on. Each statement is guarded by -// gone_at_epoch IS NULL, so nothing already lost is touched and nothing is ever -// cleared (Gone is terminal). Losing a lineage that is already lost matches no -// rows and returns nil, so a retried transaction may replay it. address must be -// one storeID minted, or ErrEpochAddressNotFound is returned and nothing is -// written. A lineage member minted in an epoch that has not happened yet -// violates the table's gone-not-before-mint check, and the resulting error is -// returned rather than papered over. -func LoseVersionInTx(ctx context.Context, tx DBTX, storeID, address string) error { - row, found, err := readEpochMintedAddressInTx(ctx, tx, address) - if err != nil { - return err - } - if !found || row.storeID != storeID { - return fmt.Errorf("epoch CAS: lose version for %s: %w: %s", storeID, ErrEpochAddressNotFound, address) - } - epoch, err := readStoreEpochWithoutSeedingInTx(ctx, tx) - if err != nil { - return fmt.Errorf("epoch CAS: lose version %s for %s: %w", address, storeID, err) - } - if err := loseLineageInTx(ctx, tx, storeID, row.lineageRoot(address), epoch); err != nil { - return fmt.Errorf("epoch CAS: lose version %s for %s: %w", address, storeID, err) - } - return nil -} diff --git a/internal/storage/issueops/epoch_cas_test.go b/internal/storage/issueops/epoch_cas_test.go deleted file mode 100644 index 223a40581..000000000 --- a/internal/storage/issueops/epoch_cas_test.go +++ /dev/null @@ -1,1141 +0,0 @@ -package issueops - -import ( - "context" - "database/sql/driver" - "errors" - "go/ast" - "go/parser" - "go/token" - "math" - "path/filepath" - "strconv" - "strings" - "testing" - - "github.com/DATA-DOG/go-sqlmock" -) - -// These are sqlmock tests because each one pins a SHAPE: which statements the -// shared epoch body runs, in what order, and above all which statements it does -// not run. sqlmock rejects any statement without a registered expectation, so a -// test that registers no ExpectExec is a test that forbids every write. What a -// statement does to real rows (set semantics, carry, collision) is checked -// against a real store by the embedded leg's tests. -// -// The survival model these tests pin (gastownhall/beads#6664, bee-ghosttrack -// review 5360880888, Major 1): an address's status is a function of its OWN -// row and the current epoch. It is Live unless gone_at_epoch is set, and a row -// minted in an epoch that has not happened yet is not served. Nothing infers -// status from other rows. -const ( - epochSQLRow = `SELECT store_id, minted_id, minted_epoch, carried_from, gone_at_epoch FROM epoch_minted_addresses WHERE address = \?` - epochSQLReadEpoch = `SELECT epoch FROM store_epoch WHERE id = 1` - epochSQLNewest = `SELECT address FROM epoch_minted_addresses WHERE carried_from = \? AND store_id = \? AND gone_at_epoch IS NULL AND minted_epoch <= \? ORDER BY minted_epoch DESC LIMIT 1` - epochSQLLoseRoot = `UPDATE epoch_minted_addresses SET gone_at_epoch = \? WHERE address = \? AND store_id = \? AND gone_at_epoch IS NULL` - epochSQLLoseCarry = `UPDATE epoch_minted_addresses SET gone_at_epoch = \? WHERE carried_from = \? AND store_id = \? AND gone_at_epoch IS NULL` - epochSQLInsertRoot = `INSERT INTO epoch_minted_addresses \(address, store_id, minted_id, minted_epoch, minted_at\) VALUES \(\?, \?, \?, \?, \?\)` -) - -var epochSQLRowColumns = []string{"store_id", "minted_id", "minted_epoch", "carried_from", "gone_at_epoch"} - -// epochTestRow is one epoch_minted_addresses row as the shared body reads it. -type epochTestRow struct { - storeID string - mintedID string - mintedEpoch int - // carriedFrom is nil for a root row, else the lineage root's address. - carriedFrom any - // goneAtEpoch is nil while the lineage is served, else the epoch it was - // lost in. - goneAtEpoch any -} - -// expectEpochRow registers the read of address's row. A nil row is an address -// the store never minted. -func expectEpochRow(mock sqlmock.Sqlmock, address string, row *epochTestRow) { - rows := sqlmock.NewRows(epochSQLRowColumns) - if row != nil { - rows.AddRow(row.storeID, row.mintedID, row.mintedEpoch, row.carriedFrom, row.goneAtEpoch) - } - mock.ExpectQuery(epochSQLRow).WithArgs(address).WillReturnRows(rows) -} - -// expectEpochRead registers the read of store_epoch's singleton. It takes no -// arguments: the counter is per database, so no storeID ever reaches this SQL. -func expectEpochRead(mock sqlmock.Sqlmock, epoch int) { - mock.ExpectQuery(epochSQLReadEpoch). - WithArgs([]driver.Value{}...). - WillReturnRows(sqlmock.NewRows([]string{"epoch"}).AddRow(epoch)) -} - -// expectEpochAbsent registers the read of store_epoch's singleton finding no -// row, the state the table starts in (migration 0067). The other cases register -// a present row, where a reader that seeds the row and one that does not run the -// same SQL; only an absent row tells them apart. -func expectEpochAbsent(mock sqlmock.Sqlmock) { - mock.ExpectQuery(epochSQLReadEpoch). - WithArgs([]driver.Value{}...). - WillReturnRows(sqlmock.NewRows([]string{"epoch"})) -} - -func intPtr(v int) *int { return &v } - -// TestEpochReadPathsNeverSeedAnAbsentStoreEpochRow pins item B4 -// (gastownhall/beads#6664, bee-ghosttrack review 5268699223): every path that -// only reads answers an absent store_epoch row from the epoch-1 default and -// never inserts it. LoseVersionInTx is here too, because the epoch it reads is -// not one it writes. Each case registers no INSERT, so a read path switched to -// the seeding reader (ensureStoreEpochRow) runs an unexpected statement and -// fails; the Dolt legs cannot catch that switch, because their read -// transactions roll back. -func TestEpochReadPathsNeverSeedAnAbsentStoreEpochRow(t *testing.T) { - t.Parallel() - - const storeID = "absent-epoch-store" - root := epochAddress(storeID, "record-a", 1) - row := &epochTestRow{storeID: storeID, mintedID: "record-a", mintedEpoch: 1} - ctx := context.Background() - - t.Run("CurrentEpochInTx", func(t *testing.T) { - t.Parallel() - _, mock, tx := beginMockTx(t) - expectEpochAbsent(mock) - got, err := CurrentEpochInTx(ctx, tx, storeID) - if err != nil || got != 1 { - t.Fatalf("CurrentEpochInTx with no store_epoch row = (%d, %v), want (1, nil)", got, err) - } - if err := mock.ExpectationsWereMet(); err != nil { - t.Fatalf("CurrentEpochInTx must only read: %v", err) - } - }) - t.Run("StillServesInTx", func(t *testing.T) { - t.Parallel() - _, mock, tx := beginMockTx(t) - expectEpochRow(mock, root, row) - expectEpochAbsent(mock) - got, err := StillServesInTx(ctx, tx, storeID, root) - if err != nil || !got { - t.Fatalf("StillServesInTx with no store_epoch row = (%v, %v), want (true, nil): an address minted at epoch 1 is served at the default epoch", got, err) - } - if err := mock.ExpectationsWereMet(); err != nil { - t.Fatalf("StillServesInTx must only read: %v", err) - } - }) - t.Run("ResolveEpochInTx", func(t *testing.T) { - t.Parallel() - _, mock, tx := beginMockTx(t) - expectEpochRow(mock, root, row) - expectEpochAbsent(mock) - got, err := ResolveEpochInTx(ctx, tx, storeID, root) - if err != nil || got.Restriction != EpochRestrictionLive || got.Epoch == nil || *got.Epoch != 1 { - t.Fatalf("ResolveEpochInTx with no store_epoch row = (%+v, %v), want Live at epoch 1", got, err) - } - if err := mock.ExpectationsWereMet(); err != nil { - t.Fatalf("ResolveEpochInTx must only read: %v", err) - } - }) - t.Run("CurrentAddressForInTx", func(t *testing.T) { - t.Parallel() - _, mock, tx := beginMockTx(t) - expectEpochRow(mock, root, row) - expectEpochAbsent(mock) - mock.ExpectQuery(epochSQLNewest).WithArgs(root, storeID, int64(1)). - WillReturnRows(sqlmock.NewRows([]string{"address"})) - got, err := CurrentAddressForInTx(ctx, tx, storeID, root) - if err != nil || got != root { - t.Fatalf("CurrentAddressForInTx with no store_epoch row = (%q, %v), want (%q, nil)", got, err, root) - } - if err := mock.ExpectationsWereMet(); err != nil { - t.Fatalf("CurrentAddressForInTx must only read: %v", err) - } - }) - t.Run("LoseVersionInTx", func(t *testing.T) { - t.Parallel() - _, mock, tx := beginMockTx(t) - expectEpochRow(mock, root, row) - expectEpochAbsent(mock) - mock.ExpectExec(epochSQLLoseRoot).WithArgs(int64(1), root, storeID).WillReturnResult(sqlmock.NewResult(0, 1)) - mock.ExpectExec(epochSQLLoseCarry).WithArgs(int64(1), root, storeID).WillReturnResult(sqlmock.NewResult(0, 0)) - if err := LoseVersionInTx(ctx, tx, storeID, root); err != nil { - t.Fatalf("LoseVersionInTx with no store_epoch row: %v", err) - } - if err := mock.ExpectationsWereMet(); err != nil { - t.Fatalf("LoseVersionInTx must write only the loss, at the default epoch 1: %v", err) - } - }) -} - -// TestStillServesAndResolveAnswerFromTheAddressRowAndTheEpochAlone pins the -// whole resolver. Each case registers exactly the statements the answer may -// use, the address's own row and then the epoch, so a third statement (for -// instance a look at the id's other rows, which is what the earlier chain -// check did) is an unexpected call the mock rejects. An address nobody minted, -// or another store's, is answered from the row alone: the epoch is not read. -func TestStillServesAndResolveAnswerFromTheAddressRowAndTheEpochAlone(t *testing.T) { - t.Parallel() - - const storeID = "status-store" - const currentEpoch = 5 - root := epochAddress(storeID, "record-a", 1) - - cases := []struct { - name string - row *epochTestRow - wantServes bool - wantRestriction EpochRestriction - readsEpoch bool - }{ - { - name: "a row minted in an earlier epoch and never lost is served", - row: &epochTestRow{storeID: storeID, mintedID: "record-a", mintedEpoch: 1}, - wantServes: true, - wantRestriction: EpochRestrictionLive, - readsEpoch: true, - }, - { - name: "a row minted in the current epoch is served", - row: &epochTestRow{storeID: storeID, mintedID: "record-a", mintedEpoch: currentEpoch}, - wantServes: true, - wantRestriction: EpochRestrictionLive, - readsEpoch: true, - }, - { - name: "a carry row is served on its own row, with no look at the root", - row: &epochTestRow{storeID: storeID, mintedID: "record-a", mintedEpoch: 3, carriedFrom: root}, - wantServes: true, - wantRestriction: EpochRestrictionLive, - readsEpoch: true, - }, - { - name: "a lost row is not served", - row: &epochTestRow{storeID: storeID, mintedID: "record-a", mintedEpoch: 1, goneAtEpoch: 3}, - wantServes: false, - wantRestriction: EpochRestrictionGoneReorganization, - readsEpoch: true, - }, - { - name: "a row lost in the epoch it was minted in is not served", - row: &epochTestRow{storeID: storeID, mintedID: "record-a", mintedEpoch: currentEpoch, goneAtEpoch: currentEpoch}, - wantServes: false, - wantRestriction: EpochRestrictionGoneReorganization, - readsEpoch: true, - }, - { - name: "a row minted in an epoch that has not happened yet fails closed", - row: &epochTestRow{storeID: storeID, mintedID: "record-a", mintedEpoch: currentEpoch + 2}, - wantServes: false, - wantRestriction: EpochRestrictionGoneReorganization, - readsEpoch: true, - }, - { - name: "an address the store never minted is unknown", - row: nil, - wantServes: false, - wantRestriction: EpochRestrictionUnknown, - }, - { - name: "an address another store minted is unknown", - row: &epochTestRow{storeID: "other-store", mintedID: "record-a", mintedEpoch: 1}, - wantServes: false, - wantRestriction: EpochRestrictionUnknown, - }, - } - - for _, tc := range cases { - t.Run(tc.name+" (StillServes)", func(t *testing.T) { - t.Parallel() - _, mock, tx := beginMockTx(t) - expectEpochRow(mock, root, tc.row) - if tc.readsEpoch { - expectEpochRead(mock, currentEpoch) - } - - got, err := StillServesInTx(context.Background(), tx, storeID, root) - if err != nil { - t.Fatalf("StillServesInTx: %v", err) - } - if got != tc.wantServes { - t.Errorf("StillServesInTx = %v, want %v", got, tc.wantServes) - } - if err := mock.ExpectationsWereMet(); err != nil { - t.Fatalf("StillServesInTx ran a different set of statements than the row read and the epoch read: %v", err) - } - }) - t.Run(tc.name+" (Resolve)", func(t *testing.T) { - t.Parallel() - _, mock, tx := beginMockTx(t) - expectEpochRow(mock, root, tc.row) - if tc.readsEpoch { - expectEpochRead(mock, currentEpoch) - } - - got, err := ResolveEpochInTx(context.Background(), tx, storeID, root) - if err != nil { - t.Fatalf("ResolveEpochInTx: %v", err) - } - if got.Restriction != tc.wantRestriction { - t.Errorf("ResolveEpochInTx.Restriction = %v, want %v", got.Restriction, tc.wantRestriction) - } - if got.ProducingStore != storeID { - t.Errorf("ResolveEpochInTx.ProducingStore = %q, want %q", got.ProducingStore, storeID) - } - var wantEpoch *int - if tc.readsEpoch { - wantEpoch = intPtr(currentEpoch) - } - switch { - case wantEpoch == nil && got.Epoch != nil: - t.Errorf("ResolveEpochInTx.Epoch = %d, want nil: an unknown address never looks the epoch up", *got.Epoch) - case wantEpoch != nil && got.Epoch == nil: - t.Errorf("ResolveEpochInTx.Epoch = nil, want the current epoch %d", *wantEpoch) - case wantEpoch != nil && *got.Epoch != *wantEpoch: - t.Errorf("ResolveEpochInTx.Epoch = %d, want the current epoch %d", *got.Epoch, *wantEpoch) - } - if err := mock.ExpectationsWereMet(); err != nil { - t.Fatalf("ResolveEpochInTx ran a different set of statements than the row read and the epoch read: %v", err) - } - }) - } -} - -// TestCurrentAddressForInTxReadsAndNeverWrites pins that CurrentAddressFor is -// a pure read (bee-ghosttrack, review 5360880888, Major 1). The earlier form -// minted a fresh address under every read, which made a lost address look -// served again. Both Dolt legs open a read-write transaction for their read -// path and always roll it back, so a stray write would vanish with no error: -// the transaction type cannot enforce this. These cases register no ExpectExec, -// so any INSERT, UPDATE or DELETE is an unexpected call. -func TestCurrentAddressForInTxReadsAndNeverWrites(t *testing.T) { - t.Parallel() - - const storeID = "current-address-store" - const currentEpoch = 5 - root := epochAddress(storeID, "record-a", 1) - firstCarry := epochAddress(storeID, "record-a", 3) - newestCarry := epochAddress(storeID, "record-a", 5) - - cases := []struct { - name string - // asked is the address the caller holds; row is its row. - asked string - row *epochTestRow - // newest is the lineage's newest carry row, "" when it has none. - newest string - want string - }{ - { - name: "a survivor nothing carried returns its own address", - asked: root, - row: &epochTestRow{storeID: storeID, mintedID: "record-a", mintedEpoch: 1}, - want: root, - }, - { - name: "the root of a carried lineage returns the newest carry", - asked: root, - row: &epochTestRow{storeID: storeID, mintedID: "record-a", mintedEpoch: 1}, - newest: newestCarry, - want: newestCarry, - }, - { - name: "an older carry resolves through the lineage root, one lookup from any member", - asked: firstCarry, - row: &epochTestRow{storeID: storeID, mintedID: "record-a", mintedEpoch: 3, carriedFrom: root}, - newest: newestCarry, - want: newestCarry, - }, - { - name: "the newest carry returns itself", - asked: newestCarry, - row: &epochTestRow{storeID: storeID, mintedID: "record-a", mintedEpoch: 5, carriedFrom: root}, - newest: newestCarry, - want: newestCarry, - }, - } - - for _, tc := range cases { - t.Run(tc.name, func(t *testing.T) { - t.Parallel() - _, mock, tx := beginMockTx(t) - expectEpochRow(mock, tc.asked, tc.row) - expectEpochRead(mock, currentEpoch) - lookup := mock.ExpectQuery(epochSQLNewest).WithArgs(root, storeID, int64(currentEpoch)) - if tc.newest == "" { - lookup.WillReturnRows(sqlmock.NewRows([]string{"address"})) - } else { - lookup.WillReturnRows(sqlmock.NewRows([]string{"address"}).AddRow(tc.newest)) - } - - got, err := CurrentAddressForInTx(context.Background(), tx, storeID, tc.asked) - if err != nil { - t.Fatalf("CurrentAddressForInTx: %v", err) - } - if got != tc.want { - t.Errorf("CurrentAddressForInTx = %q, want %q", got, tc.want) - } - if err := mock.ExpectationsWereMet(); err != nil { - t.Fatalf("CurrentAddressForInTx ran a different set of statements than reads: %v", err) - } - }) - } -} - -// TestCurrentAddressForInTxReturnsTypedErrorsForAddressesItCannotAnswer pins the -// two errors a caller can tell apart: an address the store never minted, and -// one it minted but no longer serves (lost, or from an epoch that has not -// happened). Neither runs the lineage lookup, and neither writes. -func TestCurrentAddressForInTxReturnsTypedErrorsForAddressesItCannotAnswer(t *testing.T) { - t.Parallel() - - const storeID = "typed-error-store" - const currentEpoch = 5 - address := epochAddress(storeID, "record-a", 1) - - cases := []struct { - name string - row *epochTestRow - readsEpoch bool - want error - notWant error - }{ - { - name: "a lost address is not served", - row: &epochTestRow{storeID: storeID, mintedID: "record-a", mintedEpoch: 1, goneAtEpoch: 3}, - readsEpoch: true, - want: ErrEpochAddressNotServed, - notWant: ErrEpochAddressNotFound, - }, - { - name: "an address from an epoch that has not happened is not served", - row: &epochTestRow{storeID: storeID, mintedID: "record-a", mintedEpoch: currentEpoch + 1}, - readsEpoch: true, - want: ErrEpochAddressNotServed, - notWant: ErrEpochAddressNotFound, - }, - { - name: "an address the store never minted is not found", - row: nil, - want: ErrEpochAddressNotFound, - notWant: ErrEpochAddressNotServed, - }, - { - name: "an address another store minted is not found", - row: &epochTestRow{storeID: "other-store", mintedID: "record-a", mintedEpoch: 1}, - want: ErrEpochAddressNotFound, - notWant: ErrEpochAddressNotServed, - }, - } - - for _, tc := range cases { - t.Run(tc.name, func(t *testing.T) { - t.Parallel() - _, mock, tx := beginMockTx(t) - expectEpochRow(mock, address, tc.row) - if tc.readsEpoch { - expectEpochRead(mock, currentEpoch) - } - - got, err := CurrentAddressForInTx(context.Background(), tx, storeID, address) - if err == nil { - t.Fatalf("CurrentAddressForInTx = %q with no error, want %v", got, tc.want) - } - if !errors.Is(err, tc.want) { - t.Errorf("CurrentAddressForInTx error = %v, want it to wrap %v", err, tc.want) - } - if errors.Is(err, tc.notWant) { - t.Errorf("CurrentAddressForInTx error = %v, must not wrap %v: callers tell the two apart", err, tc.notWant) - } - if got != "" { - t.Errorf("CurrentAddressForInTx returned %q alongside an error, want an empty address", got) - } - if err := mock.ExpectationsWereMet(); err != nil { - t.Fatalf("unmet expectations: %v", err) - } - }) - } -} - -// TestLoseVersionInTxMarksTheWholeLineageOnceAndOnlyWhereStillLive pins loss: -// it reads the address's row and the epoch (never creating the singleton), then -// marks the lineage ROOT's own row and every carry row of that root, each -// statement guarded by gone_at_epoch IS NULL so that nothing already lost is -// touched and nothing is ever cleared. Losing a lineage that is already lost -// matches no rows and is not an error, so a retried transaction may replay it. -func TestLoseVersionInTxMarksTheWholeLineageOnceAndOnlyWhereStillLive(t *testing.T) { - t.Parallel() - - const storeID = "lose-store" - const currentEpoch = 4 - root := epochAddress(storeID, "record-a", 1) - carry := epochAddress(storeID, "record-a", 3) - - cases := []struct { - name string - asked string - row *epochTestRow - rootAffected int64 - carryRows int64 - }{ - { - name: "losing the root marks the root row and its carry rows", - asked: root, - row: &epochTestRow{storeID: storeID, mintedID: "record-a", mintedEpoch: 1}, - rootAffected: 1, - carryRows: 1, - }, - { - name: "losing a carry address marks the same lineage through its root", - asked: carry, - row: &epochTestRow{storeID: storeID, mintedID: "record-a", mintedEpoch: 3, carriedFrom: root}, - rootAffected: 1, - carryRows: 1, - }, - { - name: "a lineage that was never carried still runs both guarded statements", - asked: root, - row: &epochTestRow{storeID: storeID, mintedID: "record-a", mintedEpoch: 1}, - rootAffected: 1, - carryRows: 0, - }, - { - name: "a lineage that is already lost matches no rows and is not an error", - asked: root, - row: &epochTestRow{storeID: storeID, mintedID: "record-a", mintedEpoch: 1, goneAtEpoch: 3}, - rootAffected: 0, - carryRows: 0, - }, - } - - for _, tc := range cases { - t.Run(tc.name, func(t *testing.T) { - t.Parallel() - _, mock, tx := beginMockTx(t) - expectEpochRow(mock, tc.asked, tc.row) - expectEpochRead(mock, currentEpoch) - mock.ExpectExec(epochSQLLoseRoot). - WithArgs(int64(currentEpoch), root, storeID). - WillReturnResult(sqlmock.NewResult(0, tc.rootAffected)) - mock.ExpectExec(epochSQLLoseCarry). - WithArgs(int64(currentEpoch), root, storeID). - WillReturnResult(sqlmock.NewResult(0, tc.carryRows)) - - if err := LoseVersionInTx(context.Background(), tx, storeID, tc.asked); err != nil { - t.Fatalf("LoseVersionInTx: %v", err) - } - if err := mock.ExpectationsWereMet(); err != nil { - t.Fatalf("LoseVersionInTx did not run the root update and then the carry update, each guarded: %v", err) - } - }) - } -} - -// TestLoseVersionInTxReportsAnAddressTheStoreNeverMinted pins that a loss -// naming an address this store never minted is an error and writes nothing. -func TestLoseVersionInTxReportsAnAddressTheStoreNeverMinted(t *testing.T) { - t.Parallel() - - const storeID = "lose-absent-store" - address := epochAddress(storeID, "record-a", 1) - - cases := []struct { - name string - row *epochTestRow - }{ - {name: "an address nobody minted", row: nil}, - {name: "an address another store minted", row: &epochTestRow{storeID: "other-store", mintedID: "record-a", mintedEpoch: 1}}, - } - for _, tc := range cases { - t.Run(tc.name, func(t *testing.T) { - t.Parallel() - _, mock, tx := beginMockTx(t) - expectEpochRow(mock, address, tc.row) - - err := LoseVersionInTx(context.Background(), tx, storeID, address) - if !errors.Is(err, ErrEpochAddressNotFound) { - t.Fatalf("LoseVersionInTx = %v, want it to wrap ErrEpochAddressNotFound", err) - } - if err := mock.ExpectationsWereMet(); err != nil { - t.Fatalf("a loss that cannot be recorded must run the row read and nothing else: %v", err) - } - }) - } -} - -// TestLoseVersionInTxSurfacesAStatementFailure pins that a failed update is -// returned, not swallowed. A lineage member minted in an epoch that has not -// happened yet violates the table's gone-not-before-mint check, and the loss -// must report that anomaly rather than paper over it. -func TestLoseVersionInTxSurfacesAStatementFailure(t *testing.T) { - t.Parallel() - - const storeID = "lose-failure-store" - address := epochAddress(storeID, "record-a", 1) - checkViolation := errors.New("check constraint violated") - - _, mock, tx := beginMockTx(t) - expectEpochRow(mock, address, &epochTestRow{storeID: storeID, mintedID: "record-a", mintedEpoch: 1}) - expectEpochRead(mock, 2) - mock.ExpectExec(epochSQLLoseRoot). - WithArgs(int64(2), address, storeID). - WillReturnResult(sqlmock.NewResult(0, 1)) - mock.ExpectExec(epochSQLLoseCarry). - WithArgs(int64(2), address, storeID). - WillReturnError(checkViolation) - - err := LoseVersionInTx(context.Background(), tx, storeID, address) - if !errors.Is(err, checkViolation) { - t.Fatalf("LoseVersionInTx = %v, want it to wrap the failed statement's error", err) - } -} - -// TestBumpEpochInTxWritesOnlyStoreEpoch pins that a plain bump (restore and -// destructive reinit) touches the counter and nothing else. Restore replaces -// the database and reinit keeps what survives, so neither may write an address -// row: an address row a bump wrote would be an address that was not minted. -// Only BumpEpochCarryingInTx, for a token-scheme change, writes address rows. -func TestBumpEpochInTxWritesOnlyStoreEpoch(t *testing.T) { - t.Parallel() - - _, mock, tx := beginMockTx(t) - expectEpochRead(mock, 3) - mock.ExpectExec(`UPDATE store_epoch SET epoch = epoch \+ 1, bumped_at = \?, bumped_reason = \? WHERE id = 1`). - WithArgs(sqlmock.AnyArg(), "restore"). - WillReturnResult(sqlmock.NewResult(0, 1)) - expectEpochRead(mock, 4) - - got, err := BumpEpochInTx(context.Background(), tx, "bump-store", "restore") - if err != nil { - t.Fatalf("BumpEpochInTx: %v", err) - } - if got != 4 { - t.Errorf("BumpEpochInTx = %d, want 4", got) - } - if err := mock.ExpectationsWereMet(); err != nil { - t.Fatalf("BumpEpochInTx ran statements beyond the store_epoch read, update and read-back: %v", err) - } -} - -// TestABumpUnderOneStoreIDMovesTheEpochSeenUnderEveryOther pins the sentence -// the epoch functions' docs now carry (bee-ghosttrack, review 5360880888, -// Minor 3): the counter is per database, not per store, and storeID appears -// only in error text. No SQL here takes the storeID as an argument, so a bump -// made under one storeID is seen under every storeID that shares the database. -// There is deliberately no storeID guard: the conformance suite uses several -// synthetic storeIDs in one database. -func TestABumpUnderOneStoreIDMovesTheEpochSeenUnderEveryOther(t *testing.T) { - t.Parallel() - - _, mock, tx := beginMockTx(t) - expectEpochRead(mock, 2) - mock.ExpectExec(`UPDATE store_epoch SET epoch = epoch \+ 1, bumped_at = \?, bumped_reason = \? WHERE id = 1`). - WithArgs(sqlmock.AnyArg(), "restore"). - WillReturnResult(sqlmock.NewResult(0, 1)) - expectEpochRead(mock, 3) - expectEpochRead(mock, 3) - - bumped, err := BumpEpochInTx(context.Background(), tx, "store-one", "restore") - if err != nil { - t.Fatalf("BumpEpochInTx under store-one: %v", err) - } - seen, err := CurrentEpochInTx(context.Background(), tx, "store-two") - if err != nil { - t.Fatalf("CurrentEpochInTx under store-two: %v", err) - } - if seen != bumped { - t.Errorf("CurrentEpochInTx(store-two) = %d after a bump under store-one reported %d, want the same epoch: the counter is per database", seen, bumped) - } - if err := mock.ExpectationsWereMet(); err != nil { - t.Fatalf("unmet expectations: %v", err) - } -} - -// TestMintUnderEpochInTxNeverHandsBackAnAddressTheStoreDoesNotServe pins the -// mint's refusal. A mint is deterministic in (storeID, id, epoch), so it can -// find a row already sitting at the address it computes; that row is only -// possible when the lineage was minted in this epoch and lost in this same -// epoch. Handing that address back would give the caller an address that -// reads Gone, so the mint returns ErrEpochAddressNotServed instead, and it -// does not touch the row. -func TestMintUnderEpochInTxNeverHandsBackAnAddressTheStoreDoesNotServe(t *testing.T) { - t.Parallel() - - const storeID = "mint-store" - const currentEpoch = 5 - address := epochAddress(storeID, "record-a", currentEpoch) - - _, mock, tx := beginMockTx(t) - expectEpochRead(mock, currentEpoch) - expectEpochRow(mock, address, &epochTestRow{storeID: storeID, mintedID: "record-a", mintedEpoch: currentEpoch, goneAtEpoch: currentEpoch}) - - got, err := MintUnderEpochInTx(context.Background(), tx, storeID, "record-a") - if !errors.Is(err, ErrEpochAddressNotServed) { - t.Fatalf("MintUnderEpochInTx = (%q, %v), want it to wrap ErrEpochAddressNotServed", got, err) - } - if got != "" { - t.Errorf("MintUnderEpochInTx returned %q alongside the refusal, want an empty address", got) - } - if err := mock.ExpectationsWereMet(); err != nil { - t.Fatalf("a refused mint must read and write nothing else: %v", err) - } -} - -// TestMintUnderEpochInTxInsertsOnlyAFreshRootRow pins that the only write a -// mint ever makes is the INSERT of its own root row. A mint that finds its -// address already served returns it with no write at all, so it can never -// modify, retire or revive another row. -func TestMintUnderEpochInTxInsertsOnlyAFreshRootRow(t *testing.T) { - t.Parallel() - - const storeID = "mint-store" - const currentEpoch = 5 - address := epochAddress(storeID, "record-a", currentEpoch) - - t.Run("an absent row is inserted as a root row", func(t *testing.T) { - t.Parallel() - _, mock, tx := beginMockTx(t) - expectEpochRead(mock, currentEpoch) - expectEpochRow(mock, address, nil) - mock.ExpectExec(epochSQLInsertRoot). - WithArgs(address, storeID, "record-a", int64(currentEpoch), sqlmock.AnyArg()). - WillReturnResult(sqlmock.NewResult(0, 1)) - - got, err := MintUnderEpochInTx(context.Background(), tx, storeID, "record-a") - if err != nil { - t.Fatalf("MintUnderEpochInTx: %v", err) - } - if got != address { - t.Errorf("MintUnderEpochInTx = %q, want %q", got, address) - } - if err := mock.ExpectationsWereMet(); err != nil { - t.Fatalf("the mint's only write must be the root INSERT: %v", err) - } - }) - - t.Run("a served row at the address is returned with no write", func(t *testing.T) { - t.Parallel() - _, mock, tx := beginMockTx(t) - expectEpochRead(mock, currentEpoch) - expectEpochRow(mock, address, &epochTestRow{storeID: storeID, mintedID: "record-a", mintedEpoch: currentEpoch}) - - got, err := MintUnderEpochInTx(context.Background(), tx, storeID, "record-a") - if err != nil { - t.Fatalf("MintUnderEpochInTx: %v", err) - } - if got != address { - t.Errorf("MintUnderEpochInTx = %q, want %q", got, address) - } - if err := mock.ExpectationsWereMet(); err != nil { - t.Fatalf("minting an address that is already served must write nothing: %v", err) - } - }) -} - -// TestMintUnderEpochInTxTrustsADuplicateKeyOnlyAsFarAsItCanReadTheRow pins -// what a mint does when its INSERT reports a duplicate key for an address its -// own read found absent: another transaction inserted the row, and may have -// lost it since. The mint reads the row again and returns the address only -// when that row is served; it refuses a row that is not served, and a row it -// cannot read at all is a failure, never the success item B3 once assumed. -func TestMintUnderEpochInTxTrustsADuplicateKeyOnlyAsFarAsItCanReadTheRow(t *testing.T) { - t.Parallel() - - const storeID = "mint-race-store" - const currentEpoch = 5 - address := epochAddress(storeID, "record-a", currentEpoch) - duplicate := errors.New("Error 1062: Duplicate entry for key 'PRIMARY'") - - // expectLostInsert registers a mint whose read finds no row and whose - // INSERT then hits the row another transaction wrote. - expectLostInsert := func(mock sqlmock.Sqlmock) { - expectEpochRead(mock, currentEpoch) - expectEpochRow(mock, address, nil) - mock.ExpectExec(epochSQLInsertRoot). - WithArgs(address, storeID, "record-a", int64(currentEpoch), sqlmock.AnyArg()). - WillReturnError(duplicate) - } - - t.Run("a served row is the one the mint would have inserted", func(t *testing.T) { - t.Parallel() - _, mock, tx := beginMockTx(t) - expectLostInsert(mock) - expectEpochRow(mock, address, &epochTestRow{storeID: storeID, mintedID: "record-a", mintedEpoch: currentEpoch}) - - got, err := MintUnderEpochInTx(context.Background(), tx, storeID, "record-a") - if err != nil { - t.Fatalf("MintUnderEpochInTx after losing the insert to a row it then reads served: %v", err) - } - if got != address { - t.Errorf("MintUnderEpochInTx = %q, want %q", got, address) - } - if err := mock.ExpectationsWereMet(); err != nil { - t.Fatalf("unmet expectations: %v", err) - } - }) - - t.Run("a row lost since it was inserted is refused", func(t *testing.T) { - t.Parallel() - _, mock, tx := beginMockTx(t) - expectLostInsert(mock) - expectEpochRow(mock, address, &epochTestRow{storeID: storeID, mintedID: "record-a", mintedEpoch: currentEpoch, goneAtEpoch: currentEpoch}) - - got, err := MintUnderEpochInTx(context.Background(), tx, storeID, "record-a") - if !errors.Is(err, ErrEpochAddressNotServed) { - t.Fatalf("MintUnderEpochInTx = (%q, %v), want it to wrap ErrEpochAddressNotServed: the row at the address reads Gone", got, err) - } - if got != "" { - t.Errorf("MintUnderEpochInTx returned %q alongside the refusal, want an empty address", got) - } - if err := mock.ExpectationsWereMet(); err != nil { - t.Fatalf("unmet expectations: %v", err) - } - }) - - t.Run("a row the transaction cannot read is not success", func(t *testing.T) { - t.Parallel() - _, mock, tx := beginMockTx(t) - expectLostInsert(mock) - expectEpochRow(mock, address, nil) - - got, err := MintUnderEpochInTx(context.Background(), tx, storeID, "record-a") - if err == nil { - t.Fatalf("MintUnderEpochInTx = %q, nil, want an error: nothing this transaction can read says the store serves the row another transaction inserted", got) - } - if !errors.Is(err, duplicate) { - t.Errorf("MintUnderEpochInTx error = %v, want it to wrap the duplicate-key error, so a caller can tell a race from a refusal", err) - } - if errors.Is(err, ErrEpochAddressNotServed) { - t.Errorf("MintUnderEpochInTx error = %v wraps ErrEpochAddressNotServed, want not: the row's status is unknown, not Gone", err) - } - if got != "" { - t.Errorf("MintUnderEpochInTx returned %q alongside the error, want an empty address", got) - } - if err := mock.ExpectationsWereMet(); err != nil { - t.Fatalf("unmet expectations: %v", err) - } - }) - - t.Run("a failed re-read reports both errors", func(t *testing.T) { - t.Parallel() - _, mock, tx := beginMockTx(t) - expectLostInsert(mock) - readFailure := errors.New("read failed") - mock.ExpectQuery(epochSQLRow).WithArgs(address).WillReturnError(readFailure) - - got, err := MintUnderEpochInTx(context.Background(), tx, storeID, "record-a") - if !errors.Is(err, duplicate) || !errors.Is(err, readFailure) { - t.Fatalf("MintUnderEpochInTx = (%q, %v), want an error wrapping both the duplicate key and the failed re-read", got, err) - } - if got != "" { - t.Errorf("MintUnderEpochInTx returned %q alongside the error, want an empty address", got) - } - if err := mock.ExpectationsWereMet(); err != nil { - t.Fatalf("unmet expectations: %v", err) - } - }) -} - -// TestMintUnderEpochInTxRefusesAPairWhoseAddressCouldOutgrowItsColumn pins the -// bound on epoch_minted_addresses.address, a VARCHAR(255) PRIMARY KEY: a pair -// one byte too long is refused with ErrEpochAddressTooLong before any -// statement runs, and the widest pair that fits is minted. The bound is the -// address at the widest epoch the INT counter holds, so a pair that mints -// today still fits after any number of bumps. -func TestMintUnderEpochInTxRefusesAPairWhoseAddressCouldOutgrowItsColumn(t *testing.T) { - t.Parallel() - - storeID := strings.Repeat("s", 115) - widest := strings.Repeat("i", 115) - if n := len(epochAddress(storeID, widest, math.MaxInt32)); n != maxEpochAddressLen { - t.Fatalf("the widest pair composes to %d bytes at the widest epoch, want exactly %d: the boundary below is not at the limit", n, maxEpochAddressLen) - } - - t.Run("one byte over is refused before any statement", func(t *testing.T) { - t.Parallel() - _, mock, tx := beginMockTx(t) - - got, err := MintUnderEpochInTx(context.Background(), tx, storeID, widest+"i") - if !errors.Is(err, ErrEpochAddressTooLong) { - t.Fatalf("MintUnderEpochInTx(one byte over) = (%q, %v), want it to wrap ErrEpochAddressTooLong", got, err) - } - if got != "" { - t.Errorf("MintUnderEpochInTx returned %q alongside the refusal, want an empty address", got) - } - if err := mock.ExpectationsWereMet(); err != nil { - t.Fatalf("a refused pair must run no statement: %v", err) - } - }) - - t.Run("the widest pair that fits is minted", func(t *testing.T) { - t.Parallel() - // Epoch 1 is the epoch whose address is SHORTEST, so this pair passes - // only because the bound is charged at the widest epoch and met there. - address := epochAddress(storeID, widest, 1) - _, mock, tx := beginMockTx(t) - expectEpochRead(mock, 1) - expectEpochRow(mock, address, nil) - mock.ExpectExec(epochSQLInsertRoot). - WithArgs(address, storeID, widest, int64(1), sqlmock.AnyArg()). - WillReturnResult(sqlmock.NewResult(0, 1)) - - got, err := MintUnderEpochInTx(context.Background(), tx, storeID, widest) - if err != nil { - t.Fatalf("MintUnderEpochInTx(the widest pair): %v", err) - } - if got != address { - t.Errorf("MintUnderEpochInTx = %q, want %q", got, address) - } - if err := mock.ExpectationsWereMet(); err != nil { - t.Fatalf("unmet expectations: %v", err) - } - }) -} - -// TestBumpEpochCarryingInTxCarriesTheWidestMintablePairToTheWidestEpoch is the -// carry half of the address bound. A token-scheme change re-encodes every live -// pair at the new epoch, so the widest pair a mint accepts, carried to the -// widest epoch the counter holds, must produce an address the column holds; -// the mint's bound is what guarantees it. -func TestBumpEpochCarryingInTxCarriesTheWidestMintablePairToTheWidestEpoch(t *testing.T) { - t.Parallel() - - storeID := strings.Repeat("s", 115) - widest := strings.Repeat("i", 115) - if err := validateEpochAddressInputs(storeID, widest); err != nil { - t.Fatalf("validateEpochAddressInputs(the widest pair) = %v, want nil: a mint accepts it", err) - } - root := epochAddress(storeID, widest, 1) - carried := epochAddress(storeID, widest, math.MaxInt32) - if len(carried) > maxEpochAddressLen { - t.Fatalf("the widest mintable pair carried to epoch %d is %d bytes, over the %d the column holds", math.MaxInt32, len(carried), maxEpochAddressLen) - } - - _, mock, tx := beginMockTx(t) - expectEpochRead(mock, math.MaxInt32-1) - mock.ExpectExec(`UPDATE store_epoch SET epoch = epoch \+ 1, bumped_at = \?, bumped_reason = \? WHERE id = 1`). - WithArgs(sqlmock.AnyArg(), "token-scheme-change"). - WillReturnResult(sqlmock.NewResult(0, 1)) - expectEpochRead(mock, math.MaxInt32) - mock.ExpectQuery(`SELECT address, store_id, minted_id, carried_from FROM epoch_minted_addresses WHERE gone_at_epoch IS NULL AND minted_epoch < \? ORDER BY store_id, minted_id, minted_epoch DESC`). - WithArgs(int64(math.MaxInt32)). - WillReturnRows(sqlmock.NewRows([]string{"address", "store_id", "minted_id", "carried_from"}).AddRow(root, storeID, widest, nil)) - mock.ExpectExec(`INSERT INTO epoch_minted_addresses \(address, store_id, minted_id, minted_epoch, minted_at, carried_from\) VALUES \(\?, \?, \?, \?, \?, \?\)`). - WithArgs(carried, storeID, widest, int64(math.MaxInt32), sqlmock.AnyArg(), root). - WillReturnResult(sqlmock.NewResult(0, 1)) - - got, err := BumpEpochCarryingInTx(context.Background(), tx, storeID, "token-scheme-change") - if err != nil { - t.Fatalf("BumpEpochCarryingInTx to the widest epoch: %v", err) - } - if got != math.MaxInt32 { - t.Errorf("BumpEpochCarryingInTx = %d, want %d", got, math.MaxInt32) - } - if err := mock.ExpectationsWereMet(); err != nil { - t.Fatalf("the carry must insert exactly the %d-byte address of the widest pair at the widest epoch: %v", len(carried), err) - } -} - -// TestEpochAddressNeverCollidesAcrossDifferentStoreIDBoundaries is the -// regression test for gastownhall/beads#6664 (bee-ghosttrack, maintainer -// review 5268699223, item B2, first part): epochAddress's ":"-joined -// encoding is ambiguous — a colon inside storeID or id lets two -// semantically different (storeID, id, epoch) triples collide on the -// identical address string. This directly contradicts the package doc's -// "persisted once at mint and never rewritten in place" promise: a -// colliding second mint would silently steal the first triple's row -// (upsertEpochMintedAddressInTx's exists-branch UPDATE has no store_id/ -// minted_id guard). -func TestEpochAddressNeverCollidesAcrossDifferentStoreIDBoundaries(t *testing.T) { - t.Parallel() - - // Under the old "epch:%s:%s:%d" encoding, these two distinct triples - // both produce "epch:store-a:b:c:1": the colon inside the first - // triple's id ("b:c") lands exactly on the second triple's - // storeID/id boundary ("store-a:b" / "c"). - const storeA, idA = "store-a", "b:c" - const storeB, idB = "store-a:b", "c" - a := epochAddress(storeA, idA, 1) - b := epochAddress(storeB, idB, 1) - if a == b { - t.Fatalf("epochAddress(%q, %q, 1) and epochAddress(%q, %q, 1) both produced %q; a colon inside storeID or id must not let two different triples collide on the same address (B2, gastownhall/beads#6664 review 5268699223)", storeA, idA, storeB, idB, a) - } -} - -// TestUpsertEpochMintedAddressInTxIsANoOpWhenTheAddressAlreadyExists is the -// regression test for gastownhall/beads#6664 (bee-ghosttrack, maintainer -// review 5268699223, item B2, third part): once epochAddress is injective -// (TestEpochAddressNeverCollidesAcrossDifferentStoreIDBoundaries above), a -// row's address already existing provably means its stored (store_id, -// minted_id, minted_epoch) triple already matches the incoming one exactly -// — so the exists branch must perform no write at all, rather than the -// UPDATE it runs today, which (before epochAddress is injective) can steal -// another store's row through a colliding address. -func TestUpsertEpochMintedAddressInTxIsANoOpWhenTheAddressAlreadyExists(t *testing.T) { - t.Parallel() - - _, mock, tx := beginMockTx(t) - address := epochAddress("no-op-store", "record-a", 1) - - // Deliberately no ExpectExec/ExpectQuery at all: any write this branch - // attempts is an unexpected call sqlmock rejects. - err := upsertEpochMintedAddressInTx(context.Background(), tx, address, "no-op-store", "record-a", 1, true) - if err != nil { - t.Fatalf("upsertEpochMintedAddressInTx(exists=true) = %v, want nil: an already-existing address must be a no-op, not attempt a write this test set up no expectation for (B2, gastownhall/beads#6664 review 5268699223)", err) - } - if err := mock.ExpectationsWereMet(); err != nil { - t.Fatalf("unmet no-op-on-exists SQL expectations: %v", err) - } -} - -// TestEnsureStoreEpochRowSeedsTheSingletonWithInsertIgnore is the regression -// test for the "Unverified" note of gastownhall/beads#6664 (bee-ghosttrack, -// review 5360880888): ensureStoreEpochRow seeded -// store_epoch's singleton with a plain INSERT where version_history.go seeds -// the very same row with INSERT IGNORE. It follows that seed's shape: read -// first, seed only when the row is absent (so the seed happens once per store, -// not once per mint), then read the row back so the epoch it reports is what -// the row actually holds. -func TestEnsureStoreEpochRowSeedsTheSingletonWithInsertIgnore(t *testing.T) { - t.Parallel() - - _, mock, tx := beginMockTx(t) - - // store_epoch starts empty (migration 0067), so the first read finds no row. - mock.ExpectQuery(`SELECT epoch FROM store_epoch WHERE id = 1`). - WillReturnRows(sqlmock.NewRows([]string{"epoch"})) - mock.ExpectExec(`INSERT IGNORE INTO store_epoch \(id, epoch\) VALUES \(1, 1\)`). - WillReturnResult(sqlmock.NewResult(0, 1)) - mock.ExpectQuery(`SELECT epoch FROM store_epoch WHERE id = 1`). - WillReturnRows(sqlmock.NewRows([]string{"epoch"}).AddRow(1)) - - got, err := ensureStoreEpochRow(context.Background(), tx) - if err != nil { - t.Fatalf("ensureStoreEpochRow on an empty store_epoch: %v", err) - } - if got != 1 { - t.Fatalf("ensureStoreEpochRow on an empty store_epoch = epoch %d, want 1 (a freshly seeded singleton starts at epoch 1)", got) - } - if err := mock.ExpectationsWereMet(); err != nil { - t.Fatalf("unmet store_epoch seed SQL expectations: %v", err) - } -} - -// TestEnsureStoreEpochRowReportsTheEpochOfARowItsSeedDidNotWrite pins why the -// seed reads back instead of returning the literal 1 it would have written: -// INSERT IGNORE writes nothing when the singleton already exists, and the row -// it leaves behind may hold a later epoch than the one this call tried to seed. -func TestEnsureStoreEpochRowReportsTheEpochOfARowItsSeedDidNotWrite(t *testing.T) { - t.Parallel() - - _, mock, tx := beginMockTx(t) - - mock.ExpectQuery(`SELECT epoch FROM store_epoch WHERE id = 1`). - WillReturnRows(sqlmock.NewRows([]string{"epoch"})) - // The seed is ignored: another writer's row is already there. - mock.ExpectExec(`INSERT IGNORE INTO store_epoch \(id, epoch\) VALUES \(1, 1\)`). - WillReturnResult(sqlmock.NewResult(0, 0)) - mock.ExpectQuery(`SELECT epoch FROM store_epoch WHERE id = 1`). - WillReturnRows(sqlmock.NewRows([]string{"epoch"}).AddRow(3)) - - got, err := ensureStoreEpochRow(context.Background(), tx) - if err != nil { - t.Fatalf("ensureStoreEpochRow when the seed is ignored: %v", err) - } - if got != 3 { - t.Fatalf("ensureStoreEpochRow when the seed is ignored = epoch %d, want 3 (the epoch the surviving row holds, not the 1 the ignored seed would have written)", got) - } - if err := mock.ExpectationsWereMet(); err != nil { - t.Fatalf("unmet ignored-seed SQL expectations: %v", err) - } -} - -// handWrittenEpochAddresses returns the position of every string literal in src -// that writes an address by hand, meaning one that starts with the address -// encoding's prefix. It reads string literals only, so a comment that merely -// describes an address, or a call to epochAddress, is never reported. -func handWrittenEpochAddresses(filename string, src any) ([]string, error) { - fset := token.NewFileSet() - parsed, err := parser.ParseFile(fset, filename, src, 0) - if err != nil { - return nil, err - } - // The prefix is assembled here so this guard's own source holds no - // literal that starts with it. - addressPrefix := "epch" + ":" - var hits []string - ast.Inspect(parsed, func(n ast.Node) bool { - lit, ok := n.(*ast.BasicLit) - if !ok || lit.Kind != token.STRING { - return true - } - value, err := strconv.Unquote(lit.Value) - if err != nil { - return true - } - if strings.HasPrefix(value, addressPrefix) { - hits = append(hits, fset.Position(lit.Pos()).String()+": "+lit.Value) - } - return true - }) - return hits, nil -} - -// TestEpochCASTestsBuildEveryAddressWithEpochAddress pins the rule that this -// file never writes an address by hand (bee-ghosttrack, review 5360880888, -// Minor 4). The review found colon-form literals of the encoding that -// epochAddress retired, and one length-prefixed literal whose lengths were -// wrong. A hand-written address goes stale the moment the encoding moves, -// while a test that builds it with epochAddress cannot disagree with the code -// under test. -// -// The server leg's publication tests are held to the same rule (#6664 review): -// their fake serves a row at any address, so a stale one would still pass -// there. They cannot call epochAddress from that package, so they mint the -// address instead. -func TestEpochCASTestsBuildEveryAddressWithEpochAddress(t *testing.T) { - t.Parallel() - - for _, filename := range []string{ - "epoch_cas_test.go", - filepath.Join("..", "dolt", "epoch_cas_publish_test.go"), - } { - hits, err := handWrittenEpochAddresses(filename, nil) - if err != nil { - t.Fatalf("parse %s: %v", filename, err) - } - for _, hit := range hits { - t.Errorf("%s writes an address by hand; build it with epochAddress(storeID, id, epoch), or mint it where epochAddress is out of reach", hit) - } - } -} - -// TestHandWrittenAddressGuardHasTeeth proves the guard above can fail: it must -// report a colon-form literal and a length-prefixed literal, and must stay -// quiet for a call to epochAddress and for a comment describing an address. -func TestHandWrittenAddressGuardHasTeeth(t *testing.T) { - t.Parallel() - - prefix := "epch" + ":" - cases := []struct { - name string - src string - want int - }{ - {name: "a colon-form literal", src: "package p\nvar a = \"" + prefix + "store:id:1\"\n", want: 1}, - {name: "a length-prefixed literal", src: "package p\nvar a = \"" + prefix + "5:store:2:id:1\"\n", want: 1}, - {name: "two literals", src: "package p\nvar a, b = \"" + prefix + "x\", \"" + prefix + "y\"\n", want: 2}, - {name: "a call to epochAddress", src: "package p\nvar a = epochAddress(\"store\", \"id\", 1)\n", want: 0}, - {name: "a comment describing an address", src: "package p\n// the old form was " + prefix + "store:id:1\nvar a = 1\n", want: 0}, - } - for _, tc := range cases { - t.Run(tc.name, func(t *testing.T) { - t.Parallel() - hits, err := handWrittenEpochAddresses("sample.go", tc.src) - if err != nil { - t.Fatalf("parse sample: %v", err) - } - if len(hits) != tc.want { - t.Errorf("the guard reported %d hand-written addresses (%v), want %d", len(hits), hits, tc.want) - } - }) - } -} diff --git a/internal/storage/issueops/execution.go b/internal/storage/issueops/execution.go index 3c00bcde0..c0c7a621b 100644 --- a/internal/storage/issueops/execution.go +++ b/internal/storage/issueops/execution.go @@ -87,17 +87,12 @@ func ExecuteCreate(ctx context.Context, tx *sql.Tx, request publicops.CreateRequ } issue.Dependencies = storage.CreatePublicCreateDependencies(issue.ID, attempt) var skipped []skippedDependency - // Reuse the context read above: same transaction, same options, and - // nothing since has written config, so re-reading it (as - // CreateIssuesInTxWithResult would) only repeats the same reads. - createContext := *batch - createContext.Opts = storage.BatchCreateOptions{ + created, err := CreateIssuesInTxWithResult(ctx, tx, []*types.Issue{issue}, attempt.Actor, storage.BatchCreateOptions{ CreateOnly: true, SkipPrefixValidation: attempt.ForceIDPrefix, OnSkippedDependency: func(issueID, dependsOnID, reason string) { skipped = append(skipped, skippedDependency{issueID: issueID, dependsOnID: dependsOnID, reason: reason}) }, - } - created, err := CreateIssuesInTxWithContext(ctx, tx, &createContext, []*types.Issue{issue}, attempt.Actor) + }) if err != nil { return publicops.CreateResult{}, nil, ClassifyPublicCreateError(err) } @@ -136,15 +131,6 @@ func skippedDependencyError(skipped []skippedDependency) error { // ExecuteUpdate applies a guarded update in tx and reports durable tables changed. func ExecuteUpdate(ctx context.Context, tx *sql.Tx, request publicops.UpdateRequest) (publicops.UpdateResult, ChangedTables, error) { - return executeUpdate(ctx, tx, request, true) -} - -// executeUpdate is ExecuteUpdate with the template guard made optional, for the -// one caller that is not an update of an existing row: the batch apply's -// metadata splice, which finishes a CREATE item of the same request — a -// template the request itself is creating is still being written, not -// modified. -func executeUpdate(ctx context.Context, tx *sql.Tx, request publicops.UpdateRequest, guardTemplate bool) (publicops.UpdateResult, ChangedTables, error) { attempt := CloneUpdateRequest(request) if attempt.Actor == "" || attempt.IssueID == "" { return publicops.UpdateResult{}, nil, fmt.Errorf("%w: update requires actor and issue ID", storage.ErrValidation) @@ -180,25 +166,12 @@ func executeUpdate(ctx context.Context, tx *sql.Tx, request publicops.UpdateRequ } else if err := CheckExpectedFieldsInTx(ctx, tx, attempt.IssueID, attempt.ExpectedAssignee, nil); err != nil { return publicops.UpdateResult{}, nil, err } - if guardTemplate { - if err := AuthorizeTemplateUpdate(before, attempt); err != nil { - return publicops.UpdateResult{}, nil, err - } - } if err := AuthorizeAssigneeTransfer(ctx, tx, before, attempt); err != nil { return publicops.UpdateResult{}, nil, err } - if err := AuthorizeNotesOverwrite(before, attempt); err != nil { - return publicops.UpdateResult{}, nil, err - } - // Every constituent below runs its no-mint variant: one guarded update is - // one caller-visible mutation, and its version is minted exactly once at - // the end, after the last patch, so durable_state carries the final row, - // label set and edge set rather than whichever write happened to run - // first. changedAny := false if attempt.Claim { - claimed, err := claimIssueInTx(ctx, tx, attempt.IssueID, attempt.Actor, false) + claimed, err := ClaimIssueInTx(ctx, tx, attempt.IssueID, attempt.Actor) if err != nil { return publicops.UpdateResult{}, nil, err } @@ -236,7 +209,7 @@ func executeUpdate(ctx context.Context, tx *sql.Tx, request publicops.UpdateRequ updates[OpForceClosePolicy] = true } if len(updates) > 0 { - updated, err := updateIssueInTx(ctx, tx, attempt.IssueID, updates, attempt.Actor, true, false) + updated, err := UpdateIssueInTx(ctx, tx, attempt.IssueID, updates, attempt.Actor) if err != nil { return publicops.UpdateResult{}, nil, err } @@ -249,7 +222,7 @@ func executeUpdate(ctx context.Context, tx *sql.Tx, request publicops.UpdateRequ } } } - labelsChanged, err := applyLabelPatch(ctx, tx, current, attempt.Patch.Labels, attempt.Actor, false) + labelsChanged, err := ApplyLabelPatch(ctx, tx, current, attempt.Patch.Labels, attempt.Actor) if err != nil { return publicops.UpdateResult{}, nil, err } @@ -258,7 +231,7 @@ func executeUpdate(ctx context.Context, tx *sql.Tx, request publicops.UpdateRequ _, labelTable, eventTable, _ := WispTableRouting(IsActiveWispInTx(ctx, tx, attempt.IssueID)) tables.Add(labelTable, eventTable) } - parentResult, err := applyParentPatch(ctx, tx, current, attempt.Patch.ParentID, attempt.Actor, false) + parentResult, err := ApplyParentPatch(ctx, tx, current, attempt.Patch.ParentID, attempt.Actor) if err != nil { return publicops.UpdateResult{}, nil, err } @@ -270,13 +243,12 @@ func executeUpdate(ctx context.Context, tx *sql.Tx, request publicops.UpdateRequ tables.Add("issues") } } - enteredIssuesPlane := false if attempt.Patch.Persistence.Set { current, err := GetIssueInTx(ctx, tx, attempt.IssueID) if err != nil { return publicops.UpdateResult{}, nil, err } - moved, err := moveIssuePersistenceInTx(ctx, tx, current, attempt.Patch.Persistence.Value, attempt.Actor, false) + moved, err := MoveIssuePersistenceInTx(ctx, tx, current, attempt.Patch.Persistence.Value, attempt.Actor) if err != nil { return publicops.UpdateResult{}, nil, err } @@ -284,23 +256,6 @@ func executeUpdate(ctx context.Context, tx *sql.Tx, request publicops.UpdateRequ changedAny = true tables.Merge(moved.ChangedTables) } - enteredIssuesPlane = moved.EnteredIssuesPlane - } - // The one mint for this guarded update, LAST: after the claim, the row - // write, the label and parent patches and the persistence move. Nothing - // changed means nothing to version (the FR-2 no-op case), and a row that - // moved to the wisp plane is excluded by the seam itself. A persistence - // move that brought a wisp onto the issues plane makes this the record's - // first version, so it mints create-shaped (design §16.2b), the same as - // MoveIssuePersistenceInTx's own mint for that move. - if changedAny { - if enteredIssuesPlane { - if err := RecordVersionForCreateInTx(ctx, tx, attempt.IssueID, attempt.Actor); err != nil { - return publicops.UpdateResult{}, nil, err - } - } else if err := RecordVersionInTx(ctx, tx, attempt.IssueID, attempt.Actor); err != nil { - return publicops.UpdateResult{}, nil, err - } } hydrated, err := HydrateIssueOperationResult(ctx, tx, attempt.IssueID, false) if err != nil { @@ -345,16 +300,7 @@ func ExecuteClose(ctx context.Context, tx *sql.Tx, request publicops.CloseReques if err != nil { return publicops.CloseResult{}, nil, err } - result := publicops.CloseResult{Issue: hydrated, Changed: changed, OpenChildren: closed.OpenChildren} - if attempt.AutoCloseMolecule { - auto, autoTables, err := CloseCompletedMoleculeInTx(ctx, tx, attempt.IssueID, attempt.Actor, attempt.Session) - if err != nil { - return publicops.CloseResult{}, nil, err - } - tables.Merge(autoTables) - result.AutoClosedMolecule, result.MoleculeAutoCloseRefusal = auto.Root, auto.Refusal - } - return result, tables, nil + return publicops.CloseResult{Issue: hydrated, Changed: changed, OpenChildren: closed.OpenChildren}, tables, nil } // ExecuteReopen applies a guarded reopen in tx and reports durable tables changed. diff --git a/internal/storage/issueops/gate_fields.go b/internal/storage/issueops/gate_fields.go deleted file mode 100644 index 203e37457..000000000 --- a/internal/storage/issueops/gate_fields.go +++ /dev/null @@ -1,107 +0,0 @@ -package issueops - -import ( - "encoding/json" - "fmt" - "time" - "unicode/utf8" - - "github.com/steveyegge/beads/internal/types" - publicops "github.com/steveyegge/beads/issueops" -) - -// MaxAwaitTypeLen is the await_type column's width in characters -// (VARCHAR(32), migration 0001). -const MaxAwaitTypeLen = 32 - -// ValidateGateFields checks an issue's gate members against the stored -// columns, for every create shape of the role: PreparePublicCreateRequest -// runs it, so Lifecycle.Create, BatchCreator items and BatchApplier create -// items share it on every backend. -// -// The widths are the columns' and no more (see publicops.GateFieldError): -// a Dolt column already refused an oversized value with a raw driver error, -// and a Postgres text column accepted it, so this converges the two on one -// typed refusal. The one rule that is not a width, a non-negative timeout, is -// no column's (timeout_ns is a BIGINT); the wire publishes it as minimum 0. -// The raw store creates (CreateIssue, CreateIssues) and import do not run -// this check, so bd gate create --timeout=-5m, a formula step's gate.timeout -// and a template clone still store a negative timeout until they move onto -// the role. -func ValidateGateFields(issue *types.Issue) error { - if issue == nil { - return nil - } - if err := validateAwaitType(issue.AwaitType); err != nil { - return err - } - if err := validateAwaitID(issue.AwaitID); err != nil { - return err - } - if err := validateGateTimeout(issue.Timeout); err != nil { - return err - } - return validateWaiters(issue.Waiters) -} - -// ValidateGatePatch checks the gate members an IssuePatch can set — await_id -// and waiters — with ValidateGateFields' rules. await_type and timeout are not -// patch members: no update route has ever changed them. -func ValidateGatePatch(patch publicops.IssuePatch) error { - if patch.AwaitID.Set { - if err := validateAwaitID(patch.AwaitID.Value); err != nil { - return err - } - } - if patch.Waiters.Set { - if err := validateWaiters(patch.Waiters.Value); err != nil { - return err - } - } - return nil -} - -func gateFieldError(field, format string, args ...any) error { - return &publicops.GateFieldError{ - Field: field, - Detail: fmt.Sprintf("%s: %s", publicops.ErrValidation.Error(), fmt.Sprintf(format, args...)), - } -} - -func validateAwaitType(value string) error { - if n := utf8.RuneCountInString(value); n > MaxAwaitTypeLen { - return gateFieldError(publicops.GateFieldAwaitType, "await_type is %d characters (max %d)", n, MaxAwaitTypeLen) - } - return nil -} - -func validateAwaitID(value string) error { - if n := utf8.RuneCountInString(value); n > types.MaxFieldLen { - return gateFieldError(publicops.GateFieldAwaitID, "await_id is %d characters (max %d)", n, types.MaxFieldLen) - } - return nil -} - -func validateGateTimeout(value time.Duration) error { - if value < 0 { - return gateFieldError(publicops.GateFieldTimeout, "timeout cannot be negative (got %s)", value) - } - return nil -} - -// validateWaiters bounds the list by the TEXT column it is stored in, as the -// JSON array every backend writes. Entries are otherwise opaque: blank and -// repeated entries are stored as given, as local updates always stored them. -func validateWaiters(waiters []string) error { - if len(waiters) == 0 { - return nil - } - encoded, err := json.Marshal(waiters) - if err != nil { - return gateFieldError(publicops.GateFieldWaiters, "waiters cannot be encoded: %v", err) - } - if n := len(encoded); n > types.MaxTextBytes { - return gateFieldError(publicops.GateFieldWaiters, "waiters encode to %d bytes (max %d)", n, types.MaxTextBytes) - } - return nil -} diff --git a/internal/storage/issueops/gate_fields_test.go b/internal/storage/issueops/gate_fields_test.go deleted file mode 100644 index 9dcd73867..000000000 --- a/internal/storage/issueops/gate_fields_test.go +++ /dev/null @@ -1,97 +0,0 @@ -package issueops - -import ( - "errors" - "strings" - "testing" - "time" - - "github.com/steveyegge/beads/internal/types" - publicops "github.com/steveyegge/beads/issueops" -) - -func TestValidateGateFieldsBoundsEachMemberAtItsColumn(t *testing.T) { - atBound := &types.Issue{ - AwaitType: strings.Repeat("é", MaxAwaitTypeLen), - AwaitID: strings.Repeat("é", types.MaxFieldLen), - Timeout: 0, - Waiters: []string{strings.Repeat("w", types.MaxTextBytes-4)}, - } - if err := ValidateGateFields(atBound); err != nil { - t.Fatalf("every member at its bound: %v, want nil", err) - } - // Free-form type, blank and duplicate waiters, any issue type: no rule. - if err := ValidateGateFields(&types.Issue{IssueType: types.TypeTask, AwaitType: "mail", Waiters: []string{"", "a", "a"}}); err != nil { - t.Fatalf("free-form gate members: %v, want nil", err) - } - if err := ValidateGateFields(nil); err != nil { - t.Fatalf("nil issue: %v, want nil", err) - } - - for _, test := range []struct { - name string - field string - issue types.Issue - }{ - {"await_type", publicops.GateFieldAwaitType, types.Issue{AwaitType: strings.Repeat("é", MaxAwaitTypeLen+1)}}, - {"await_id", publicops.GateFieldAwaitID, types.Issue{AwaitID: strings.Repeat("é", types.MaxFieldLen+1)}}, - {"timeout", publicops.GateFieldTimeout, types.Issue{Timeout: -time.Nanosecond}}, - {"waiters", publicops.GateFieldWaiters, types.Issue{Waiters: []string{strings.Repeat("w", types.MaxTextBytes-3)}}}, - } { - t.Run(test.name, func(t *testing.T) { - issue := test.issue - assertGateFieldErr(t, ValidateGateFields(&issue), test.field) - }) - } -} - -func TestValidateGatePatchChecksOnlySetMembers(t *testing.T) { - if err := ValidateGatePatch(publicops.IssuePatch{}); err != nil { - t.Fatalf("empty patch: %v", err) - } - if err := ValidateGatePatch(publicops.IssuePatch{AwaitID: publicops.Field[string]{Set: true}, Waiters: publicops.Field[[]string]{Set: true}}); err != nil { - t.Fatalf("clearing patch: %v", err) - } - assertGateFieldErr(t, ValidateGatePatch(publicops.IssuePatch{ - AwaitID: publicops.Field[string]{Set: true, Value: strings.Repeat("a", types.MaxFieldLen+1)}, - }), publicops.GateFieldAwaitID) - assertGateFieldErr(t, ValidateUpdateRequest(publicops.UpdateRequest{Patch: publicops.IssuePatch{ - Waiters: publicops.Field[[]string]{Set: true, Value: []string{strings.Repeat("w", types.MaxTextBytes)}}, - }}), publicops.GateFieldWaiters) -} - -func TestValidatePublicCreateRequestRefusesAGateMemberByName(t *testing.T) { - err := ValidatePublicCreateRequest(publicops.CreateRequest{Actor: "a", Issue: &types.Issue{Title: "t", Timeout: -time.Second}}) - assertGateFieldErr(t, err, publicops.GateFieldTimeout) -} - -func TestUpdateFieldsMapsWaitersAsAWholeValue(t *testing.T) { - source := []string{"a", "a"} - updates := UpdateFields(publicops.IssuePatch{Waiters: publicops.Field[[]string]{Set: true, Value: source}}) - got, ok := updates["waiters"].([]string) - if !ok || len(got) != 2 || got[0] != "a" || got[1] != "a" { - t.Fatalf("waiters update = %#v, want the list as given", updates["waiters"]) - } - source[0] = "mutated" - if got[0] != "a" { - t.Fatal("the waiters update aliases the caller's slice") - } - cleared := UpdateFields(publicops.IssuePatch{Waiters: publicops.Field[[]string]{Set: true}}) - if got, ok := cleared["waiters"].([]string); !ok || got == nil || len(got) != 0 { - t.Fatalf("cleared waiters update = %#v, want a non-nil empty list (stored as [] rather than null)", cleared["waiters"]) - } - if _, present := UpdateFields(publicops.IssuePatch{})["waiters"]; present { - t.Fatal("an unset Waiters wrote the waiters key") - } -} - -func assertGateFieldErr(t *testing.T, err error, field string) { - t.Helper() - if !errors.Is(err, publicops.ErrValidation) { - t.Fatalf("error = %v, want ErrValidation", err) - } - var fieldErr *publicops.GateFieldError - if !errors.As(err, &fieldErr) || fieldErr.Field != field { - t.Fatalf("error = %#v, want a *GateFieldError naming %s", err, field) - } -} diff --git a/internal/storage/issueops/heartbeat_role.go b/internal/storage/issueops/heartbeat_role.go deleted file mode 100644 index 646d652ff..000000000 --- a/internal/storage/issueops/heartbeat_role.go +++ /dev/null @@ -1,135 +0,0 @@ -package issueops - -import ( - "context" - "database/sql" - "errors" - "fmt" - "strings" - - "github.com/steveyegge/beads/internal/storage" - "github.com/steveyegge/beads/internal/types" - publicops "github.com/steveyegge/beads/issueops" -) - -// ValidateHeartbeatRequest applies the request rules every Heartbeater -// implementation shares. It is the ONE definition of a valid heartbeat: the -// library legs run it before they open a transaction, and the shared body runs -// it again, so a leg that skipped it still answers the same contract. -// -// THE ACTOR IS NOT TRIMMED, Releaser.Actor's rule: it is the fence's subject -// and is compared with the lease row's holder as sent. Only a blank one is -// refused, and one longer than the lease row's holder column can hold. -func ValidateHeartbeatRequest(request publicops.HeartbeatRequest) error { - if strings.TrimSpace(request.Actor) == "" { - return heartbeatFieldError(publicops.HeartbeatFieldActor, "heartbeat actor is required") - } - if err := types.CheckFieldLen("actor", request.Actor); err != nil { - return heartbeatFieldError(publicops.HeartbeatFieldActor, err.Error()) - } - if request.IssueID == "" { - return heartbeatFieldError(publicops.HeartbeatFieldIssueID, "heartbeat issue id is required") - } - return nil -} - -func heartbeatFieldError(field, detail string) error { - return &publicops.HeartbeatFieldError{Field: field, Detail: storage.ErrValidation.Error() + ": " + detail} -} - -// ExecuteHeartbeatInTx refreshes the lease request.Actor holds on -// request.IssueID, in tx. It is the body behind the Heartbeater accessor on -// ALL THREE library legs: dolt, embedded dolt and the unit-of-work provider. -// -// It is RenewLeaseInTx — the native heartbeat, SQL text unchanged — with the -// role's request rules in front and its typed refusals behind: -// -// - a wisp is refused before anything is written, as every native leg -// refuses it ("is ephemeral"): wisps are never leased; -// - an id that names no row on either plane is storage.ErrNotFound, where -// the native slow path answered ErrNotClaimable; -// - a foreign holder and a missing live claim are *LeaseConflictError, -// classified from the issue row the slow path already read. -// -// A REFUSAL MAY WRITE ONE THING: it deletes the caller's own lease row when -// that row survives on an issue the caller no longer holds a live claim on (an -// orphan; see RenewLeaseInTx). The legs therefore COMMIT the transaction of a -// refusal HeartbeatRefusalCommits recognizes and report the refusal after the -// commit, so the cleanup is not rolled back with it. -// -// WHAT A REFRESH MUST NEVER DO is everything RenewLeaseInTx's own doc rules -// out, and the conformance contract asserts each one: remint row_lock, record -// a version (current_revision, issue_versions), stamp updated_at, write an -// event or a journal entry. The legs add the other two: no version-control -// commit and no hook. A RE-ARM (HeartbeatResult.Rearmed) does every one of -// them, because it is recorded as a mutation: the legs commit it once, as -// LeaseRearmVersionCommit names, and the hook decorator fires on_update. -func ExecuteHeartbeatInTx(ctx context.Context, tx DBTX, request publicops.HeartbeatRequest) (publicops.HeartbeatResult, error) { - if err := ValidateHeartbeatRequest(request); err != nil { - return publicops.HeartbeatResult{}, err - } - id := request.IssueID - - var wispStatus string - if err := tx.QueryRowContext(ctx, "SELECT status FROM wisps WHERE id = ? LIMIT 1", id).Scan(&wispStatus); err == nil { - return publicops.HeartbeatResult{}, &publicops.LeaseConflictError{ - IssueID: id, - Status: types.Status(wispStatus), - Ephemeral: true, - Err: fmt.Errorf("%w: %s is ephemeral", publicops.ErrLeaseNotHeld, id), - } - } - - renewal, err := RenewLeaseInTx(ctx, tx, id, request.Actor) - if err != nil { - var refusal *LeaseRenewalRefusal - if !errors.As(err, &refusal) { - return publicops.HeartbeatResult{}, err - } - return publicops.HeartbeatResult{}, classifyLeaseRenewalRefusal(refusal, request.Actor) - } - return publicops.HeartbeatResult{ - IssueID: id, - Holder: request.Actor, - HeartbeatAt: renewal.HeartbeatAt, - LeaseExpiresAt: renewal.ExpiresAt, - Rearmed: renewal.Rearmed, - }, nil -} - -// classifyLeaseRenewalRefusal types a native heartbeat refusal for the role. -func classifyLeaseRenewalRefusal(refusal *LeaseRenewalRefusal, actor string) error { - id := refusal.IssueID - if !refusal.Found { - if errors.Is(refusal.ReadErr, sql.ErrNoRows) { - return fmt.Errorf("%w: issue %s", storage.ErrNotFound, id) - } - return fmt.Errorf("heartbeat %s: read issue: %w", id, refusal.ReadErr) - } - status := types.Status(refusal.Status) - if refusal.Wisp { - return &publicops.LeaseConflictError{ - IssueID: id, Status: status, Ephemeral: true, - Err: fmt.Errorf("%w: %s is ephemeral", publicops.ErrLeaseNotHeld, id), - } - } - if refusal.Assignee != "" && !actorMatches(refusal.Assignee, actor) { - return &publicops.LeaseConflictError{ - IssueID: id, Holder: refusal.Assignee, Status: status, Err: refusal.Err, - } - } - return &publicops.LeaseConflictError{ - IssueID: id, Status: status, - Err: fmt.Errorf("%w: %s status %s", publicops.ErrLeaseNotHeld, id, refusal.Status), - } -} - -// HeartbeatRefusalCommits reports whether err is a heartbeat refusal whose -// transaction a leg must COMMIT rather than roll back: a typed -// *LeaseConflictError, the one refusal that can carry a write (the orphaned -// lease row ExecuteHeartbeatInTx deletes). Committing a refusal that wrote -// nothing is a no-op. Every other error rolls back as usual. -func HeartbeatRefusalCommits(err error) bool { - var conflict *publicops.LeaseConflictError - return errors.As(err, &conflict) -} diff --git a/internal/storage/issueops/helpers.go b/internal/storage/issueops/helpers.go index e4c4514ac..f305a3658 100644 --- a/internal/storage/issueops/helpers.go +++ b/internal/storage/issueops/helpers.go @@ -114,17 +114,9 @@ func insertIssueCreateOnly(ctx context.Context, tx DBTX, table string, issue *ty return executeIssueInsert(ctx, tx, table, issue, "") } -// issueInsertSQL renders the issue-row INSERT for rows VALUES tuples followed -// by suffix (an ON DUPLICATE KEY UPDATE clause, or ""). Single-row and -// multi-row writes share it, so their column list cannot drift. -// //nolint:gosec // G201: table is a hardcoded constant ("issues" or "wisps") -func issueInsertSQL(table string, rows int, suffix string) string { - tuples := make([]string, rows) - for i := range tuples { - tuples[i] = issueInsertRow - } - return fmt.Sprintf(` +func executeIssueInsert(ctx context.Context, tx DBTX, table string, issue *types.Issue, suffix string) error { + _, err := tx.ExecContext(ctx, fmt.Sprintf(` INSERT INTO %s ( id, content_hash, title, description, design, acceptance_criteria, notes, status, priority, issue_type, assignee, estimated_minutes, @@ -136,13 +128,7 @@ func issueInsertSQL(table string, rows int, suffix string) string { await_type, await_id, timeout_ns, waiters, due_at, defer_until, metadata, row_lock, storage_class - ) VALUES %s - %s - `, table, strings.Join(tuples, ", "), suffix) -} - -// issueInsertRow is one VALUES tuple for issueInsertSQL's column list. -const issueInsertRow = `( + ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, @@ -153,12 +139,9 @@ const issueInsertRow = `( ?, ?, ?, ?, ?, ?, ?, ?, ? - )` - -// issueInsertArgs binds issue to issueInsertRow. row_lock is minted fresh -// per row (freshRowLock), as every issues/wisps content write must. -func issueInsertArgs(issue *types.Issue) []any { - return []any{ + ) + %s + `, table, suffix), issue.ID, issue.ContentHash, issue.Title, issue.Description, issue.Design, issue.AcceptanceCriteria, issue.Notes, issue.Status, issue.Priority, issue.IssueType, NullString(issue.Assignee), NullInt(issue.EstimatedMinutes), issue.CreatedAt, issue.CreatedBy, issue.Owner, issue.UpdatedAt, issue.StartedAt, issue.ClosedAt, NullStringPtr(issue.ExternalRef), issue.SpecID, @@ -169,92 +152,13 @@ func issueInsertArgs(issue *types.Issue) []any { issue.AwaitType, issue.AwaitID, issue.Timeout.Nanoseconds(), FormatJSONStringArray(issue.Waiters), issue.DueAt, issue.DeferUntil, JSONMetadata(issue.Metadata), freshRowLock(), NullString(string(issue.StorageClass.Normalize())), - } -} - -//nolint:gosec // G201: table is a hardcoded constant ("issues" or "wisps") -func executeIssueInsert(ctx context.Context, tx DBTX, table string, issue *types.Issue, suffix string) error { - _, err := tx.ExecContext(ctx, issueInsertSQL(table, 1, suffix), issueInsertArgs(issue)...) + ) if err != nil { return fmt.Errorf("insert issue into %s: %w", table, err) } return nil } -// issueInsertRowsPerStatement and issueInsertBytesPerStatement bound a -// multi-row issue INSERT. The row cap keeps a statement far below the -// 65535-placeholder protocol limit (49 bound columns per row); the byte budget -// keeps rows with long text (description and notes are LONGTEXT) from -// building a statement that a lowered max_allowed_packet or a server read -// timeout would refuse where the single-row inserts would not. -const ( - issueInsertRowsPerStatement = 100 - issueInsertBytesPerStatement = 16 << 20 -) - -// issueInsertRowBytes estimates the bytes issue binds into an INSERT: its -// free-text and JSON columns plus a fixed allowance for the rest. -func issueInsertRowBytes(issue *types.Issue) int { - n := 1024 + len(issue.Title) + len(issue.Description) + len(issue.Design) + - len(issue.AcceptanceCriteria) + len(issue.Notes) + len(issue.Metadata) + - len(issue.Payload) + len(issue.CloseReason) + len(issue.Target) - for _, w := range issue.Waiters { - n += len(w) + 3 - } - return n -} - -// issueInsertChunks splits issues into consecutive statement-sized runs: at -// most issueInsertRowsPerStatement rows, and no more than -// issueInsertBytesPerStatement estimated bytes unless a single row alone -// exceeds it. -func issueInsertChunks(issues []*types.Issue) [][]*types.Issue { - var chunks [][]*types.Issue - start, bytes := 0, 0 - for i, issue := range issues { - rowBytes := issueInsertRowBytes(issue) - if i > start && (i-start >= issueInsertRowsPerStatement || bytes+rowBytes > issueInsertBytesPerStatement) { - chunks = append(chunks, issues[start:i]) - start, bytes = i, 0 - } - bytes += rowBytes - } - if start < len(issues) { - chunks = append(chunks, issues[start:]) - } - return chunks -} - -// insertIssueRowsIntoTable writes issues into table with multi-row INSERTs -// carrying the same ON DUPLICATE KEY UPDATE clause insertIssueIntoTable uses, -// so each row lands exactly as its single-row insert would have. -// -// A multi-row statement that fails writes none of its rows (statements are -// atomic), so its rows are replayed one at a time: the first row that cannot -// be written fails the batch with the per-row path's own error, and when every -// row can be written the batch simply continues, as the per-row path would -// have. -// -//nolint:gosec // G201: table is a hardcoded constant ("issues" or "wisps") -func insertIssueRowsIntoTable(ctx context.Context, tx DBTX, table string, issues []*types.Issue, rejectStaleUpdate bool) error { - suffix := "ON DUPLICATE KEY UPDATE\n\t\t\t" + issueUpsertAssignments(table, rejectStaleUpdate) - for _, chunk := range issueInsertChunks(issues) { - args := make([]any, 0, len(chunk)*49) - for _, issue := range chunk { - args = append(args, issueInsertArgs(issue)...) - } - if _, err := tx.ExecContext(ctx, issueInsertSQL(table, len(chunk), suffix), args...); err == nil { - continue - } - for _, issue := range chunk { - if err := insertIssueIntoTable(ctx, tx, table, issue, rejectStaleUpdate); err != nil { - return fmt.Errorf("failed to insert issue %s: %w", issue.ID, err) - } - } - } - return nil -} - // RecordEventInTable records an event in the specified events table. func RecordEventInTable(ctx context.Context, tx DBTX, table, issueID string, eventType types.EventType, actor, newValue string) error { return InsertDerivedEvent(ctx, tx, table, AuxEvent{ diff --git a/internal/storage/issueops/journal_completeness_test.go b/internal/storage/issueops/journal_completeness_test.go index 2147d46e2..d14458524 100644 --- a/internal/storage/issueops/journal_completeness_test.go +++ b/internal/storage/issueops/journal_completeness_test.go @@ -30,7 +30,6 @@ var derivedReadinessEmitters = map[string]bool{ // chain stays connected AND so a mutator cannot inherit emission by calling // one of them either. "recomputeIsBlockedCounting": true, - "recomputeIsBlockedInTxWithResult": true, "recomputeIsBlockedAfterMergeScoped": true, "recomputeIsBlockedForAll": true, "MarkIsBlockedInTx": true, @@ -98,16 +97,7 @@ var mutationEntryPoints = []string{ "ClaimReadyIssueInTx", "UnclaimIssueInTx", "UnclaimIssueIfAssigneeInTx", - // The heartbeat family journals on its RE-ARM path only (a live claim - // with no lease row the caller holds: row_lock reminted, a lease granted, - // recorded as a mutation). Its REFRESH path writes only the clone-local - // leases table and still records nothing; the fast-path return in - // RenewLeaseInTx says why. The ruling that exempted the whole family - // changed when the re-arm became a mutation (it must collide with a racing - // release or close on row_lock), so the family is listed here. - "RenewLeaseInTx", - "HeartbeatIssueInTx", - "ExecuteHeartbeatInTx", + // HeartbeatIssueInTx is deliberately absent — see journalExemptMutations. "ReclaimExpiredLeasesInTx", // plane moves, graph edges, labels, comments, renames "PromoteFromEphemeralInTx", @@ -115,7 +105,6 @@ var mutationEntryPoints = []string{ "RemoveDependencyInTx", "AddLabelInTx", "RemoveLabelInTx", - "RenameLabelInTx", "UpdateIssueIDInTx", "AddIssueCommentInTx", "ImportIssueCommentInTx", @@ -147,15 +136,13 @@ var mutationEntryPoints = []string{ // beadDMLExemptions are exported functions the DML detector flags as writing a // work-bead table but which legitimately do NOT journal, each with a reason. -// They fall into five buckets: (1) derived child-counter maintenance; (2) +// They fall into four buckets: (1) derived child-counter maintenance; (2) // aux-table writers the templated-%s heuristic can't distinguish from a bead // table (events, child counters); (3) constituent sub-helpers of a // create/rename/promote/delete whose top-level entry point journals the whole // mutation once; (4) compaction maintenance outside the -// create/update/close/delete/dep/label op vocabulary; (5) dual-write -// version-history bookkeeping that runs after the mutation is already -// journaled. The staleness check fails if any stops being flagged, so an -// exemption cannot rot. +// create/update/close/delete/dep/label op vocabulary. The staleness check fails +// if any stops being flagged, so an exemption cannot rot. var beadDMLExemptions = map[string]string{ // (1) Child counters are derived CLI acceleration state. In contrast, // is_blocked is part of the exported bead snapshot and its recompute helpers @@ -195,40 +182,6 @@ var beadDMLExemptions = map[string]string{ // omission is invisible to consumers. "ApplyCompactionInTx": "compaction content rewrite outside the op vocabulary; consumer mirrors of this bead go stale until its next journaled mutation (known contract gap, carried from the reference lineage)", "RestoreFromSnapshotInTx": "restores a compacted issue outside the op vocabulary; consumer mirrors of this bead go stale until its next journaled mutation (known contract gap, carried from the reference lineage)", - - // (5) dual-write version-history bookkeeping. RecordVersionInTx is called - // from inside the same mutationEntryPoints functions (the create, update, - // close, reopen, claim, release, lease-reclaim, defer-wake, label, - // dependency, promote and persistence-move bodies, and their domain/db - // equivalents used by the uow leg — version_completeness_test.go pins the - // set) immediately after those callers already invoke RecordEventInTx, so the - // mutation itself is already journaled by the time RecordVersionInTx runs. - // Its own UPDATE issues SET current_revision = ?, updated_at = updated_at - // only advances a denormalized pointer to match the snapshot row it just - // inserted into issue_versions (not a bead table, so it can't anchor the - // exemption on its own), leaving updated_at where the mutation put it — - // bookkeeping for an already-journaled mutation, not a second mutation - // that needs its own emit. - "RecordVersionInTx": "advances the denormalized current_revision pointer to match a snapshot just inserted into issue_versions (not a bead table); called from the same entry points that already journal the mutation via RecordEventInTx, so this is bookkeeping for an already-journaled mutation, not a second one", - // RecordVersionForCreateInTx is RecordVersionInTx's create-shaped sibling - // (design §16.2b write-fence, be-h89oq): same entry points, same - // already-journaled-by-the-caller bookkeeping, plus stamping - // participation_generation on the row it just inserted into issue_versions. - "RecordVersionForCreateInTx": "advances the denormalized current_revision pointer and stamps participation_generation to match a snapshot just inserted into issue_versions (not a bead table); called from the same create entry points that already journal the mutation via RecordEventInTx, so this is bookkeeping for an already-journaled mutation, not a second one", - - // RecordVersionAtInTx (R7.1 as-of read, gastownhall/beads#5898 rev 9, - // gastownhall/beads#6136) is RecordVersionInTx's test-support - // twin: it shares RecordVersionInTx's body (recordVersionAtInTx) but skips - // the versionedHistoryEnabled gate and takes an explicit historical - // timestamp, so an as-of-read conformance fixture can mint a version row - // dated in the past. It has no production caller and is not part of any - // mutationEntryPoints flow — only per-leg AsOfReadFixture.MintAt - // implementations call it, always directly, never from inside a journaled - // mutation. There is no separate bead mutation here for it to double-journal: - // the same "UPDATE issues SET current_revision only advances a pointer to - // a issue_versions snapshot, not a bead table" reasoning as RecordVersionInTx - // applies verbatim. - "RecordVersionAtInTx": "test-support twin of RecordVersionInTx (R7.1 as-of read); mints a historical-timestamped snapshot directly for conformance fixtures, with no production caller and no mutation of its own to journal — its UPDATE issues SET current_revision only advances a pointer to the issue_versions row it just inserted (not a bead table)", } // journalExemptMutations are mutation paths that deliberately do NOT journal @@ -238,7 +191,13 @@ var beadDMLExemptions = map[string]string{ // never are. TestExemptMutationsDoNotJournal pins the decision from the other // side — each must exist and must NOT emit — so re-adding an emit fails loudly // instead of silently reversing the ruling. -var journalExemptMutations = map[string]string{} +var journalExemptMutations = map[string]string{ + "HeartbeatIssueInTx": "high-frequency lease keepalive: writes only the clone-local `leases` table " + + "(lease-liveness state), never a durable bead field. Journaling it would put a full-snapshot " + + "write plus the shared seq-counter serialization on the hottest write in the fleet, and a replay " + + "consumer gains nothing — lease state is working-set-plane and expires on its own. Lease RECLAIM " + + "is the opposite case and does journal: it clears assignee and reverts status, which is durable.", +} // TestEveryMutationFunctionJournals parses this package's source, builds the // intra-package call graph, and asserts every mutation entry point either diff --git a/internal/storage/issueops/label_rename_merged_count_test.go b/internal/storage/issueops/label_rename_merged_count_test.go deleted file mode 100644 index b2062ccb4..000000000 --- a/internal/storage/issueops/label_rename_merged_count_test.go +++ /dev/null @@ -1,130 +0,0 @@ -package issueops - -import ( - "context" - "testing" - - "github.com/DATA-DOG/go-sqlmock" -) - -// These tests pin the bda-lbjv contract: the merge count a rename reports is -// MEASURED from what the INSERT IGNORE actually affected, never predicted -// from a snapshot read taken earlier in the transaction. -// -// The race they encode: an issue carries oldLabel; a concurrent -// AddLabel(issue, newLabel) COMMITS after this transaction's snapshot is -// established but before its INSERT IGNORE executes. Under REPEATABLE READ -// the insert sees the committed row and no-ops (correct - that IS the -// merge), but a merge count computed from the earlier snapshot read reports -// 0. The data was always fine; the count was schedule-dependent. sqlmock -// scripts exactly that observable: the SELECT-visible state says "no -// carrier of newLabel", the insert's rows-affected says one row already -// existed. A count derived from rows-affected reads 1; the old -// check-then-insert shape read 0 against this same script. - -// expectRenameEventScript scripts the per-issue event mint that follows the -// label sweep (InsertDerivedEvent: same-content SELECT + INSERT). The -// journal half (RecordEventInTx) no-ops because neither the context key nor -// the per-tx registration enables it here. -func expectRenameEventScript(mock sqlmock.Sqlmock, times int) { - for i := 0; i < times; i++ { - mock.ExpectQuery(`SELECT id FROM events`). - WillReturnRows(sqlmock.NewRows([]string{"id"})) - mock.ExpectExec(`INSERT INTO events`). - WillReturnResult(sqlmock.NewResult(0, 1)) - } -} - -func TestRenameLabelInPlane_ConcurrentAddLabelStillCountsMerge(t *testing.T) { - db, mock, err := sqlmock.New() - if err != nil { - t.Fatalf("sqlmock.New: %v", err) - } - defer db.Close() - - mock.ExpectBegin() - // Snapshot read: i1 carries oldLabel. (No read of newLabel's carriers - // exists any more - the fix removed it; nothing here scripts one, so a - // regression reintroducing the check-then-insert SELECT fails loudly on - // an unexpected query.) - mock.ExpectQuery(`SELECT issue_id FROM labels WHERE label = \?`). - WithArgs("old"). - WillReturnRows(sqlmock.NewRows([]string{"issue_id"}).AddRow("i1")) - // The concurrently-committed (i1, new) row makes the INSERT IGNORE a - // no-op: zero rows affected. - mock.ExpectExec(`INSERT IGNORE INTO labels`). - WithArgs("i1", "new"). - WillReturnResult(sqlmock.NewResult(0, 0)) - // The delete is ID-SCOPED (label AND issue_id IN (...)): an unrestricted - // `WHERE label = ?` would let a current-read engine remove a row a - // concurrent AddLabel(oldLabel) committed to an issue OUTSIDE the - // snapshot - deleting a label the sweep never renamed, counted or - // journaled. sqlmock's WithArgs is the pin: a regression back to the - // unrestricted form fails on the argument mismatch. - mock.ExpectExec(`DELETE FROM labels WHERE label = \? AND issue_id IN`). - WithArgs("old", "i1"). - WillReturnResult(sqlmock.NewResult(0, 1)) - expectRenameEventScript(mock, 1) - - ctx := context.Background() - tx, err := db.BeginTx(ctx, nil) - if err != nil { - t.Fatalf("BeginTx: %v", err) - } - renamed, merged, ids, err := renameLabelInPlane(ctx, tx, "labels", "events", "old", "new", "tester") - if err != nil { - t.Fatalf("renameLabelInPlane: %v", err) - } - if renamed != 1 { - t.Errorf("renamed = %d, want 1", renamed) - } - if merged != 1 { - t.Errorf("merged = %d, want 1 (the concurrently-committed row IS a merge; a snapshot-read count reports 0 here)", merged) - } - if len(ids) != 1 || ids[0] != "i1" { - t.Errorf("ids = %v, want [i1]", ids) - } - if err := mock.ExpectationsWereMet(); err != nil { - t.Errorf("unmet expectations: %v", err) - } -} - -func TestRenameLabelInPlane_CleanRenameCountsZeroMerges(t *testing.T) { - db, mock, err := sqlmock.New() - if err != nil { - t.Fatalf("sqlmock.New: %v", err) - } - defer db.Close() - - mock.ExpectBegin() - mock.ExpectQuery(`SELECT issue_id FROM labels WHERE label = \?`). - WithArgs("old"). - WillReturnRows(sqlmock.NewRows([]string{"issue_id"}).AddRow("i1").AddRow("i2")) - // Both rows genuinely inserted: nothing carried newLabel. - mock.ExpectExec(`INSERT IGNORE INTO labels`). - WithArgs("i1", "new", "i2", "new"). - WillReturnResult(sqlmock.NewResult(0, 2)) - mock.ExpectExec(`DELETE FROM labels WHERE label = \? AND issue_id IN`). - WithArgs("old", "i1", "i2"). - WillReturnResult(sqlmock.NewResult(0, 2)) - expectRenameEventScript(mock, 2) - - ctx := context.Background() - tx, err := db.BeginTx(ctx, nil) - if err != nil { - t.Fatalf("BeginTx: %v", err) - } - renamed, merged, _, err := renameLabelInPlane(ctx, tx, "labels", "events", "old", "new", "tester") - if err != nil { - t.Fatalf("renameLabelInPlane: %v", err) - } - if renamed != 2 { - t.Errorf("renamed = %d, want 2", renamed) - } - if merged != 0 { - t.Errorf("merged = %d, want 0", merged) - } - if err := mock.ExpectationsWereMet(); err != nil { - t.Errorf("unmet expectations: %v", err) - } -} diff --git a/internal/storage/issueops/labels.go b/internal/storage/issueops/labels.go index d69b2cc61..89ef888ea 100644 --- a/internal/storage/issueops/labels.go +++ b/internal/storage/issueops/labels.go @@ -2,7 +2,6 @@ package issueops import ( "context" - "errors" "fmt" "strings" @@ -131,18 +130,8 @@ func getLabelsIntoFromTable(ctx context.Context, tx DBTX, labelTable string, ids // AddLabelInTx adds a label to an issue and records an event within an existing // transaction. Automatically routes to wisp tables if the ID is an active wisp. -// Uses INSERT IGNORE for idempotency. A label is part of the issue's durable -// state, so a label that was actually inserted mints a version row; an -// idempotent re-add (INSERT IGNORE affecting no row) mints nothing. +// Uses INSERT IGNORE for idempotency. func AddLabelInTx(ctx context.Context, tx DBTX, labelTable, eventTable, issueID, label, actor string) error { - return addLabelInTx(ctx, tx, labelTable, eventTable, issueID, label, actor, true) -} - -// addLabelInTx is the body of AddLabelInTx. mintVersion controls whether an -// inserted label mints its own version row: the exported entry point always -// does, while a label patch (applyLabelPatch) writes several rows for one -// caller-visible mutation and mints once after the last of them. -func addLabelInTx(ctx context.Context, tx DBTX, labelTable, eventTable, issueID, label, actor string, mintVersion bool) error { // Reject an over-length label up front. The INSERT IGNORE below would // otherwise silently truncate it to the VARCHAR(255) column, storing a label // the caller never sent; a typed ErrFieldTooLong is the clean rejection. @@ -160,14 +149,9 @@ func addLabelInTx(ctx context.Context, tx DBTX, labelTable, eventTable, issueID, } } //nolint:gosec // G201: labelTable is from WispTableRouting ("labels" or "wisp_labels") - res, err := tx.ExecContext(ctx, fmt.Sprintf(`INSERT IGNORE INTO %s (issue_id, label) VALUES (?, ?)`, labelTable), issueID, label) - if err != nil { + if _, err := tx.ExecContext(ctx, fmt.Sprintf(`INSERT IGNORE INTO %s (issue_id, label) VALUES (?, ?)`, labelTable), issueID, label); err != nil { return fmt.Errorf("add label: %w", err) } - inserted, err := res.RowsAffected() - if err != nil { - return fmt.Errorf("add label: rows affected: %w", err) - } comment := "Added label: " + label if err := InsertDerivedEvent(ctx, tx, eventTable, AuxEvent{ IssueID: issueID, @@ -179,31 +163,15 @@ func addLabelInTx(ctx context.Context, tx DBTX, labelTable, eventTable, issueID, } // A label is part of the bead snapshot, so a label write journals as an // update carrying the complete post-mutation set. - if err := RecordEventInTx(ctx, tx, EventUpdate, issueID, actor); err != nil { - return err - } - // Version only when a row landed: INSERT IGNORE on an existing label is - // the no-op case, and a no-op mints nothing (the same gate the dependency - // helpers draw on their inserted/deleted row). - if !mintVersion || inserted == 0 { - return nil - } - return RecordVersionInTx(ctx, tx, issueID, actor) + return RecordEventInTx(ctx, tx, EventUpdate, issueID, actor) } // RemoveLabelInTx removes a label from an issue and records an event within // an existing transaction. Automatically routes to wisp tables if the ID is -// an active wisp. A deleted label row mints a version row; a DELETE that -// matched no row is a no-op and mints nothing. -func RemoveLabelInTx(ctx context.Context, tx DBTX, labelTable, eventTable, issueID, label, actor string) error { - return removeLabelInTx(ctx, tx, labelTable, eventTable, issueID, label, actor, true) -} - -// removeLabelInTx is the body of RemoveLabelInTx; mintVersion is -// addLabelInTx's, for the same reason. +// an active wisp. // //nolint:gosec // G201: table names come from WispTableRouting (hardcoded constants) -func removeLabelInTx(ctx context.Context, tx DBTX, labelTable, eventTable, issueID, label, actor string, mintVersion bool) error { +func RemoveLabelInTx(ctx context.Context, tx DBTX, labelTable, eventTable, issueID, label, actor string) error { if labelTable == "" || eventTable == "" { isWisp := IsActiveWispInTx(ctx, tx, issueID) _, lt, et, _ := WispTableRouting(isWisp) @@ -214,14 +182,9 @@ func removeLabelInTx(ctx context.Context, tx DBTX, labelTable, eventTable, issue eventTable = et } } - res, err := tx.ExecContext(ctx, fmt.Sprintf(`DELETE FROM %s WHERE issue_id = ? AND label = ?`, labelTable), issueID, label) - if err != nil { + if _, err := tx.ExecContext(ctx, fmt.Sprintf(`DELETE FROM %s WHERE issue_id = ? AND label = ?`, labelTable), issueID, label); err != nil { return fmt.Errorf("remove label: %w", err) } - deleted, err := res.RowsAffected() - if err != nil { - return fmt.Errorf("remove label: rows affected: %w", err) - } comment := "Removed label: " + label if err := InsertDerivedEvent(ctx, tx, eventTable, AuxEvent{ IssueID: issueID, @@ -231,211 +194,5 @@ func removeLabelInTx(ctx context.Context, tx DBTX, labelTable, eventTable, issue }); err != nil { return fmt.Errorf("remove label: record event: %w", err) } - if err := RecordEventInTx(ctx, tx, EventUpdate, issueID, actor); err != nil { - return err - } - if !mintVersion || deleted == 0 { - return nil - } - return RecordVersionInTx(ctx, tx, issueID, actor) -} - -// renameLabelPlanes pairs each label table with the event table journaling -// its issues, so RenameLabelInTx sweeps both without hardcoding the pairing -// twice. -var renameLabelPlanes = [2]struct { - labelTable string - eventTable string -}{ - {labelTable: "labels", eventTable: "events"}, - {labelTable: "wisp_labels", eventTable: "wisp_events"}, -} - -// ErrRenameLabelSameName is returned when RenameLabelInTx is asked to rename -// a label to itself. Refused before any write: the merge branch below treats -// every carrier of oldLabel as already carrying newLabel (they are the same -// string), so "insert the merge row, then drop the stale oldLabel row" -// degenerates into "insert a row that already exists via INSERT IGNORE, then -// delete every row carrying this label" -- the label is wiped, not renamed. -// Compared after trimming so incidental leading/trailing whitespace on one -// side does not mask the same no-op; the comparison itself stays -// case-sensitive, matching label identity everywhere else in this store. -var ErrRenameLabelSameName = errors.New("rename label: old and new label are the same") - -// RenameLabelInTx renames a label across every issue and wisp that carries -// it. An issue that already carries newLabel is a merge: the stale oldLabel -// row is dropped rather than raising a duplicate-key error, and merged -// counts the subset of renamed where that happened. oldLabel carried by -// nothing is an honest no-op (renamed, merged both 0, err nil). ids lists -// every touched issue and wisp id, both planes concatenated. Each touched -// durable issue mints one version row (its label set changed); a no-op -// rename mints nothing. oldLabel and -// newLabel equal after trimming is refused with ErrRenameLabelSameName -// rather than treated as a no-op -- see that error's doc for why silently -// proceeding would wipe the label instead of leaving it alone. -func RenameLabelInTx(ctx context.Context, tx DBTX, oldLabel, newLabel, actor string) (renamed, merged int, ids []string, err error) { - if strings.TrimSpace(oldLabel) == strings.TrimSpace(newLabel) { - return 0, 0, nil, ErrRenameLabelSameName - } - if err := types.CheckFieldLen("label", newLabel); err != nil { - return 0, 0, nil, err - } - for _, plane := range renameLabelPlanes { - r, m, planeIDs, err := renameLabelInPlane(ctx, tx, plane.labelTable, plane.eventTable, oldLabel, newLabel, actor) - if err != nil { - return 0, 0, nil, fmt.Errorf("rename label in %s: %w", plane.labelTable, err) - } - renamed += r - merged += m - ids = append(ids, planeIDs...) - } - return renamed, merged, ids, nil -} - -// renameLabelInPlane sweeps one label table (and its matching event table) -// for oldLabel and returns the touched-id and already-had-newLabel counts. -// -//nolint:gosec // G201: labelTable/eventTable are hardcoded routing constants from renameLabelPlanes ("labels"/"events" or "wisp_labels"/"wisp_events"). -func renameLabelInPlane(ctx context.Context, tx DBTX, labelTable, eventTable, oldLabel, newLabel, actor string) (renamed, merged int, ids []string, err error) { - oldIDs, err := issueIDsWithLabelInTx(ctx, tx, labelTable, oldLabel) - if err != nil { - return 0, 0, nil, fmt.Errorf("query issues carrying %q: %w", oldLabel, err) - } - if len(oldIDs) == 0 { - return 0, 0, nil, nil - } - // INSERT IGNORE so an issue that already carries newLabel silently no-ops - // here instead of raising a duplicate-key error - this IS the merge. - // - // merged is MEASURED from what the insert actually affected, never - // predicted from a prior SELECT: INSERT IGNORE reports one affected row - // per id it really inserted, so an id whose (issue_id, newLabel) row - // already exists - including one committed by a concurrent AddLabel - // after this transaction's snapshot was taken - contributes zero, and - // len(oldIDs) - inserted is exactly the merge count. The previous - // check-then-insert shape computed merged from a snapshot read of - // newLabel's carriers taken before the insert, so a concurrent - // AddLabel(newLabel) made the reported count schedule-dependent while - // the stored data stayed correct. - inserted := 0 - for start := 0; start < len(oldIDs); start += queryBatchSize { - end := start + queryBatchSize - if end > len(oldIDs) { - end = len(oldIDs) - } - batch := oldIDs[start:end] - placeholders := make([]string, len(batch)) - args := make([]any, 0, len(batch)*2) - for i, id := range batch { - placeholders[i] = "(?, ?)" - args = append(args, id, newLabel) - } - res, err := tx.ExecContext(ctx, fmt.Sprintf( - `INSERT IGNORE INTO %s (issue_id, label) VALUES %s`, - labelTable, strings.Join(placeholders, ",")), args...) - if err != nil { - return 0, 0, nil, fmt.Errorf("insert renamed label: %w", err) - } - n, err := res.RowsAffected() - if err != nil { - return 0, 0, nil, fmt.Errorf("count inserted renamed-label rows: %w", err) - } - inserted += int(n) - } - merged = len(oldIDs) - inserted - // The delete is RESTRICTED to the ids this transaction actually renamed, - // never `WHERE label = ?` alone: an unrestricted delete's blast radius - // depends on the engine's DELETE visibility (a current-read engine could - // remove a row a concurrent AddLabel committed after our snapshot - - // destroying a label this loop never renamed, counted or journaled), - // while the id-scoped form touches exactly the rows the insert above - // accounted for on every engine. A concurrent add of oldLabel to an - // issue outside oldIDs therefore SURVIVES the rename, which is the same - // observable state as that add having happened after this transaction - // committed - the only order-independent reading of the race. - for start := 0; start < len(oldIDs); start += queryBatchSize { - end := start + queryBatchSize - if end > len(oldIDs) { - end = len(oldIDs) - } - batch := oldIDs[start:end] - placeholders := make([]string, len(batch)) - args := make([]any, 0, len(batch)+1) - args = append(args, oldLabel) - for i, id := range batch { - placeholders[i] = "?" - args = append(args, id) - } - if _, err := tx.ExecContext(ctx, fmt.Sprintf( - `DELETE FROM %s WHERE label = ? AND issue_id IN (%s)`, - labelTable, strings.Join(placeholders, ",")), args...); err != nil { - return 0, 0, nil, fmt.Errorf("delete old label rows: %w", err) - } - } - comment := fmt.Sprintf("Renamed label: '%s' to '%s'", oldLabel, newLabel) - // KNOWN COST: this loop is O(len(oldIDs)) SQL round trips, not one batch - - // InsertDerivedEvent does its own dedup SELECT (a real cross-replica - // convergence check, Protocol v0.1 C2.3, not a redundant one this - // transaction could skip: two independently-created rows with identical - // content must land on the same content-derived id) plus an INSERT, and - // RecordEventInTx does its own snapshot SELECT plus an INSERT, and - // RecordVersionInTx re-reads the issue and its dependencies to mint the - // version row - several round trips per touched issue, inside one long - // transaction. On a - // huge label population that lengthens the transaction, which raises the - // odds an optimistic-concurrency retry has to redo the whole rename from - // scratch rather than just its own small write. - // - // Not batched: no batch primitive exists for this shape anywhere in this - // package (issueops), and building one here would be new shared - // infrastructure grown for a single caller rather than a mitigation - // scoped to this fix - AddLabelInTx/RemoveLabelInTx pay the identical - // per-call cost for their one issue today, and this loop is exactly that - // cost repeated once per touched issue. Left as a disclosed limitation. - for _, id := range oldIDs { - if err := InsertDerivedEvent(ctx, tx, eventTable, AuxEvent{ - IssueID: id, - EventType: types.EventLabelRenamed, - Actor: actor, - OldValue: str(oldLabel), - NewValue: str(newLabel), - Comment: str(comment), - }); err != nil { - return 0, 0, nil, fmt.Errorf("rename label: record event for %s: %w", id, err) - } - if err := RecordEventInTx(ctx, tx, EventUpdate, id, actor); err != nil { - return 0, 0, nil, fmt.Errorf("rename label: journal %s: %w", id, err) - } - // Every id here lost its oldLabel row (and gained newLabel unless it - // already carried it), so its label set - part of the durable - // snapshot - changed: mint one version per touched bead, the same - // gate AddLabelInTx/RemoveLabelInTx draw on a row that landed. - // RecordVersionInTx is itself a no-op for wisps and when versioned - // history is disabled, so the wisp plane needs no special case. - if err := RecordVersionInTx(ctx, tx, id, actor); err != nil { - return 0, 0, nil, fmt.Errorf("rename label: version %s: %w", id, err) - } - } - return len(oldIDs), merged, oldIDs, nil -} - -// issueIDsWithLabelInTx returns every issue/wisp id carrying label in the -// given label table (unordered). -// -//nolint:gosec // G201: labelTable is a hardcoded routing constant ("labels" or "wisp_labels"). -func issueIDsWithLabelInTx(ctx context.Context, tx DBTX, labelTable, label string) ([]string, error) { - rows, err := tx.QueryContext(ctx, fmt.Sprintf(`SELECT issue_id FROM %s WHERE label = ?`, labelTable), label) - if err != nil { - return nil, err - } - defer rows.Close() - var ids []string - for rows.Next() { - var id string - if err := rows.Scan(&id); err != nil { - return nil, fmt.Errorf("scan issue id: %w", err) - } - ids = append(ids, id) - } - return ids, rows.Err() + return RecordEventInTx(ctx, tx, EventUpdate, issueID, actor) } diff --git a/internal/storage/issueops/lease.go b/internal/storage/issueops/lease.go index d45582219..1ba79f473 100644 --- a/internal/storage/issueops/lease.go +++ b/internal/storage/issueops/lease.go @@ -3,9 +3,7 @@ package issueops import ( "context" "crypto/rand" - "database/sql" "encoding/binary" - "errors" "fmt" "os" "strings" @@ -64,14 +62,10 @@ func leaseTTL(ctx context.Context) time.Duration { // races care about (claim, close, updateIssueInTx, reclaim, unclaim all do). // Paths that touch only orthogonal cells (is_blocked, compaction_level, // dependency metadata, rename, or reopen — which acts on closed rows) are safe -// to merge with a reclaim and intentionally do NOT rewrite it. A heartbeat -// REFRESH does not touch the issues row (bd-lrgn1): the lease lives in the +// to merge with a reclaim and intentionally do NOT rewrite it. Heartbeats no +// longer touch the issues row at all (bd-lrgn1): the lease lives in the // ephemeral leases table, where a racing heartbeat and reclaim contend on the -// SAME lease row and conflict without any help. A heartbeat RE-ARM (a lease -// granted to a live claim that has none) is the one deliberate remint that -// changes no lifecycle field, and it is load-bearing: its lease INSERT shares -// no row with a concurrent release or close, so the remint is what makes the -// two collide (see RenewLeaseInTx). Adding a new path that sets +// SAME lease row and conflict without any help. Adding a new path that sets // status/assignee outside updateIssueInTx without rewriting row_lock would // silently reintroduce the zombie-merge bug. // @@ -84,11 +78,10 @@ func leaseTTL(ctx context.Context) time.Duration { // that already goes out of its way to preserve updated_at // (blocked_state.go), and bumping the token there would clobber a concurrent // whole-row CAS over a cell derived from OTHER rows. (The PR's second -// exemption of this kind, the lease heartbeat, no longer arises here: since -// bd-lrgn1 a refresh never touches the issues row, and a re-arm remints on -// purpose, accepting that a guarded write composed before it fails once and -// re-reads.) So the invariant cuts both ways: status/ownership writes MUST -// remint, aux-marker writes MUST NOT. The remint direction is enforced at build time by +// exemption of this kind, the lease heartbeat, no longer arises here — since +// bd-lrgn1 heartbeats never touch the issues row at all.) So the invariant +// cuts both ways: status/ownership writes MUST remint, aux-marker writes MUST +// NOT. The remint direction is enforced at build time by // TestAllIssueRowWritesStampRowLock (row_lock_guard_test.go), whose exemption // markers are the machine-readable form of this list — widen the stamping // policy there first, not by ad-hoc stamps. @@ -300,115 +293,14 @@ func RestoreLeaseOnImportInTx(ctx context.Context, tx DBTX, issue *types.Issue, // storage.ErrNotClaimable / ErrAlreadyClaimed so the caller learns its lease // is gone. // -// A refresh touches ONLY the leases table (ephemeral, dolt_ignored): a +// The write touches ONLY the leases table (ephemeral, dolt_ignored): a // heartbeat mints no Dolt commit and no history, and deliberately does NOT // stamp issues.updated_at — updated_at keeps its merge/LWW meaning and bd // stale consults leases.heartbeat_at for in_progress rows instead (bd-lrgn1). // -// A RE-ARM (a live claim with no lease row the caller holds, which the -// heartbeat grants) is the exception: it is recorded as a mutation (row_lock, -// updated_at, event, journal, version); see RenewLeaseInTx. A caller that runs -// this in a transaction must make the version-control commit -// LeaseRearmVersionCommit names when it re-arms. -// -// It is RenewLeaseInTx with the renewal's report dropped: the legacy -// HeartbeatIssue legs answer only whether the lease was refreshed. -func HeartbeatIssueInTx(ctx context.Context, tx DBTX, id, actor string) error { - _, err := RenewLeaseInTx(ctx, tx, id, actor) - return err -} - -// LeaseRenewal reports one lease RenewLeaseInTx refreshed or re-armed. Both -// times are computed in Go from the same clock reading and TTL the statement -// binds, rounded to whole seconds as the lease columns round them on store, -// so they are what the lease row now holds. -type LeaseRenewal struct { - HeartbeatAt time.Time - ExpiresAt time.Time - // Rearmed is true only when the slow path CREATED the caller's lease row: - // the caller held a live claim with no lease row for it (a hand-doled - // claim), or the issue's lease row named a holder that is not the caller. - // A refresh of the caller's own row is never a re-arm, even when it - // changed no value (two heartbeats in one second). - Rearmed bool -} - -// LeaseRearmVersionCommit is the version-control commit a leg makes after a -// heartbeat: none for a refresh or a refusal (the leases table is -// dolt_ignored), and the release's staged set, issues and events, for a -// re-arm, which is recorded as a mutation. One helper for every leg so the -// message cannot drift. -func LeaseRearmVersionCommit(id string, rearmed bool) (ChangedTables, string) { - if !rearmed { - return nil, "" - } - tables := ChangedTables{} - tables.Add("issues", "events") - return tables, fmt.Sprintf("bd: re-arm lease %s", id) -} - -// LeaseRenewalRefusal is the refusal RenewLeaseInTx answers when no lease row -// matched the caller and the slow path could not re-arm one. It carries the -// issue row the slow path read so a typed front door (ExecuteHeartbeatInTx) -// can classify the refusal without a second read, and it reads and unwraps as -// Err — the exact error HeartbeatIssueInTx has always returned — so the -// legacy legs' text and errors.Is behavior are unchanged. -type LeaseRenewalRefusal struct { - IssueID string - // Found is false when the issue row could not be read. - Found bool - // ReadErr is the error the issue-row read failed with when Found is false. - ReadErr error - Wisp bool - Assignee string - Status string - // Err is the wrapped refusal: storage.ErrAlreadyClaimed or - // storage.ErrNotClaimable. - Err error - // OrphanDeleted is true when the refusal deleted a lease row the caller - // held on an issue it no longer holds a live claim on (see - // RenewLeaseInTx). The deletion is part of the refusing transaction: a - // caller that wants it kept must COMMIT that transaction and report the - // refusal afterwards (HeartbeatRefusalCommits). - OrphanDeleted bool -} - -func (e *LeaseRenewalRefusal) Error() string { return e.Err.Error() } - -// Unwrap makes LeaseRenewalRefusal match the refusal it carries. -func (e *LeaseRenewalRefusal) Unwrap() error { return e.Err } - -// LeaseRenewalRefusalCommits reports whether err is a RenewLeaseInTx refusal -// whose transaction a legacy HeartbeatIssue leg must COMMIT rather than roll -// back: one that read the issue row (Found), the only refusal that can carry a -// write, the orphaned lease row it deletes. A refusal whose issue read failed -// deletes nothing, and may follow a fast-path UPDATE that already extended an -// orphan row's expiry, so its transaction rolls back — the rule the role's -// HeartbeatRefusalCommits applies to the same refusal. -func LeaseRenewalRefusalCommits(err error) bool { - var refused *LeaseRenewalRefusal - return errors.As(err, &refused) && refused.Found -} - -// RenewLeaseInTx is the body of HeartbeatIssueInTx, reporting the renewal: -// see that function for what it writes and, above all, what it must never -// write. Its SQL text is the heartbeat's own and must stay so: the Postgres -// dialect rewrites the one IF() in it keyed on its `update leases set` -// prefix. -// //nolint:gosec // G201: table names come from WispTableRouting (hardcoded constants) -func RenewLeaseInTx(ctx context.Context, tx DBTX, id, actor string) (LeaseRenewal, error) { - // THE BOUND CLOCK IS UNROUNDED; THE REPORTED ONE IS ROUNDED. The lease - // columns are DATETIME (whole seconds), which round a sub-second value on - // store, so the result reports now rounded to the second: exactly what - // the row now holds. The binds stay unrounded on purpose: Dolt counts a - // value-identical UPDATE as 0 rows affected even under CLIENT_FOUND_ROWS, - // and binding the rounded value would make every same-second refresh look - // like a miss and take the slow path (several extra reads on the hottest - // write in the fleet). +func HeartbeatIssueInTx(ctx context.Context, tx DBTX, id, actor string) error { now := time.Now().UTC() - reported := now.Round(time.Second) - ttl := leaseTTL(ctx) // granted_node is backfilled, never overwritten: a lease row whose // provenance is unknown (granted before ignored migration 0016, or before // this replica was named) is being kept alive through THIS node's store, @@ -448,69 +340,28 @@ func RenewLeaseInTx(ctx context.Context, tx DBTX, id, actor string) (LeaseRenewa // `bd unclaim --force ` for a single stranded unit once that replica is // gone for good. // - // SCOPE, precisely: the no-re-home invariant holds on every REFRESH — this - // fast path and the slow path's in-place refresh of a row the caller - // already holds (same second, or the same identity spelled differently, - // ga-v2k49/ga-wzl83), which keeps granted_at and granted_node. Only a - // RE-ARM re-homes: when the caller holds a live claim and no lease row the - // caller holds exists, UpsertLeaseInTx mints one and stamps the LOCAL node. - // RestoreLeaseOnImportInTx can relabel in the other direction (it takes - // granted_node from the snapshot when the local row is expired); that is a - // pre-existing path with its own rationale. - // - // THE LIVE-CLAIM PREDICATE. The fast path refreshes the caller's row only - // while the issue is still in_progress under the caller (verbatim, the - // holder predicate's own rule). "A lease row exists only while its claim - // is live" is an invariant every claim-ending path maintains by deleting - // the row, and the slow path's re-arm keeps it against a concurrent ender - // by reminting row_lock (see below). The predicate is the second line: a - // lease row orphaned any other way (a binary without that remint, a - // hand-edited table) fails the fast path, and the slow path refuses AND - // deletes it, instead of every later heartbeat refreshing it and answering - // success for a claim that no longer exists. The `update leases set` - // prefix and the one IF() the Postgres dialect rewrites are unchanged. - // - // THE COMPARISON IS VERBATIM (assignee = the holder as sent), deliberately, - // as the holder predicate is: a holder whose lease row and assignee are - // spelled differently (ga-v2k49's separator case) misses here on every - // beat and takes the slow path, which judges under actorMatches and - // refreshes the row in place. Correct, a few reads slower, and only for - // that spelling mismatch; the common case pays one indexed count read. - // - // FOR A POSTGRES PORT: the refresh now READS the issues row. Under - // SERIALIZABLE that is a read-write dependency on every concurrent write - // to the same issue (the holder's own edits racing its heartbeat), so - // expect more serialization failures on this statement, and keep it inside - // the retrying write transaction. + // SCOPE, precisely: the no-re-home invariant holds on THIS fast path only. + // The disambiguation fallback below re-homes, by design and unavoidably — + // when `holder = ?` misses because the lease row spells the same identity + // differently (ga-v2k49/ga-wzl83), it re-arms through UpsertLeaseInTx, whose + // ON DUPLICATE KEY UPDATE stamps the LOCAL node over whatever was there. + // So "a heartbeat never re-homes a foreign lease" is false for a + // spelling-mismatched holder, and RestoreLeaseOnImportInTx can relabel in + // the other direction (it takes granted_node from the snapshot when the + // local row is expired). Neither is changed here: both are pre-existing + // paths with their own rationale, and the point of this note is that the + // invariant is a property of the common path, not of the lease row. result, err := tx.ExecContext(ctx, ` UPDATE leases SET lease_expires_at = ?, heartbeat_at = ?, granted_node = IF(COALESCE(granted_node, '') = '', ?, granted_node) WHERE issue_id = ? AND holder = ? - `, now.Add(ttl), now, NodeID(ctx), id, actor) + `, now.Add(leaseTTL(ctx)), now, NodeID(ctx), id, actor) if err != nil { - return LeaseRenewal{}, fmt.Errorf("failed to heartbeat issue: %w", err) + return fmt.Errorf("failed to heartbeat issue: %w", err) } rows, err := result.RowsAffected() if err != nil { - return LeaseRenewal{}, fmt.Errorf("failed to get rows affected: %w", err) - } - if rows > 0 { - // The live-claim check, in the same transaction as the UPDATE (so on - // the same snapshot, and atomically with it: a refusal below commits - // nothing the caller keeps but the orphan delete). It is a separate - // read rather than an EXISTS in the UPDATE because Dolt reports a - // value-identical UPDATE that carries a subquery as 0 rows affected - // even under CLIENT_FOUND_ROWS, which would send every same-second - // refresh down the slow path. - var live int - if err := tx.QueryRowContext(ctx, - `SELECT COUNT(*) FROM issues WHERE id = ? AND status = 'in_progress' AND assignee = ?`, id, actor, - ).Scan(&live); err != nil { - return LeaseRenewal{}, fmt.Errorf("failed to heartbeat issue: %w", err) - } - if live == 0 { - rows = 0 // an orphan or a spelling mismatch: the slow path decides - } + return fmt.Errorf("failed to get rows affected: %w", err) } if rows == 0 { // No lease row. Disambiguate from the issue row: gone @@ -523,47 +374,7 @@ func RenewLeaseInTx(ctx context.Context, tx DBTX, id, actor string) (LeaseRenewa fmt.Sprintf("SELECT COALESCE(assignee, ''), status FROM %s WHERE id = ?", issueTable), id, ).Scan(&assignee, &status) if qerr != nil { - return LeaseRenewal{}, &LeaseRenewalRefusal{ - IssueID: id, ReadErr: qerr, Wisp: isWisp, - Err: fmt.Errorf("%w: %s", storage.ErrNotClaimable, id), - } - } - // A refusal deletes the caller's own lease row, if one survives on an - // issue the caller no longer holds a live claim on: that row is an - // orphan (the re-arm race above), and leaving it would keep a stale - // lease on an issue that is open, closed or someone else's. The holder - // fence keeps it to the caller's own row; a live holder's lease is - // never touched. The fence is judged under actorMatches, as the - // in-place refresh below judges it: a row held under another spelling - // of the caller (ga-v2k49) is the caller's orphan too, so its holder - // is read and bound back, the refresh's own lookup, on this cold path - // only. The delete is judged in this transaction's snapshot, which is - // the snapshot that just found no live claim for the caller. - refused := func(err error) error { - refusal := &LeaseRenewalRefusal{ - IssueID: id, Found: true, Wisp: isWisp, Assignee: assignee, Status: status, Err: err, - } - if isWisp { - return refusal - } - var leaseHolder string - lerr := tx.QueryRowContext(ctx, `SELECT holder FROM leases WHERE issue_id = ?`, id).Scan(&leaseHolder) - switch { - case errors.Is(lerr, sql.ErrNoRows): - return refusal - case lerr != nil: - return fmt.Errorf("read lease for %s: %w", id, lerr) - case !actorMatches(leaseHolder, actor): - return refusal - } - res, derr := tx.ExecContext(ctx, `DELETE FROM leases WHERE issue_id = ? AND holder = ?`, id, leaseHolder) - if derr != nil { - return fmt.Errorf("delete orphaned lease for %s: %w", id, derr) - } - if n, nerr := res.RowsAffected(); nerr == nil && n > 0 { - refusal.OrphanDeleted = true - } - return refusal + return fmt.Errorf("%w: %s", storage.ErrNotClaimable, id) } // Judged under actorMatches, not verbatim (ga-v2k49): the primary // UPDATE's own `holder = ?` predicate above stays a verbatim SQL @@ -572,11 +383,11 @@ func RenewLeaseInTx(ctx context.Context, tx DBTX, id, actor string) (LeaseRenewa // above) just to bind back its own holder value. A spelling // difference across layers (ga-wzl83) already makes that predicate // affect 0 rows and fall here; fixing the disambiguation instead - // means the caller still self-heals correctly (its row is refreshed in - // place below, under its current spelling) at the cost of the slow - // path on a spelling mismatch, never on the byte-identical common case. + // means the caller still self-heals correctly (re-arms below, under + // its current spelling) at the cost of the slow path on a spelling + // mismatch, never on the byte-identical common case. if assignee != "" && !actorMatches(assignee, actor) { - return LeaseRenewal{}, refused(fmt.Errorf("%w by %s", storage.ErrAlreadyClaimed, assignee)) + return fmt.Errorf("%w by %s", storage.ErrAlreadyClaimed, assignee) } if !isWisp && actorMatches(assignee, actor) && status == string(types.StatusInProgress) { // The caller genuinely holds the claim but has no lease row — e.g. @@ -585,80 +396,13 @@ func RenewLeaseInTx(ctx context.Context, tx DBTX, id, actor string) (LeaseRenewa // semantics, or (ga-v2k49) the existing lease row's holder is a // different spelling of the same identity (ga-wzl83) and the // primary UPDATE's verbatim predicate above missed it. A real - // worker's heartbeat recovers under actor's current spelling: a - // lease row the caller already holds is refreshed in place (its - // holder rewritten to that spelling, which the next heartbeat's - // UPDATE matches), and a missing one is re-armed. An ASSIGNEE - // spelled differently from actor still fails the verbatim - // live-claim check, so that holder takes this path on every beat. - // - // FIRST, is there a lease row for this issue the caller already - // holds? Then this is a refresh the fast path missed without - // anything being wrong: on embedded Dolt an UPDATE that changes no - // value (a second heartbeat in the same second) reports 0 rows, and - // a holder spelled differently from the assignee misses the - // verbatim live-claim predicate. Refresh that row in place, keeping - // its granted_at and granted_node (the fast path's no-re-home rule, - // with the same backfill), and report no re-arm. - var leaseHolder string - lerr := tx.QueryRowContext(ctx, `SELECT holder FROM leases WHERE issue_id = ?`, id).Scan(&leaseHolder) - switch { - case lerr == nil && actorMatches(leaseHolder, actor): - if _, err := tx.ExecContext(ctx, ` - UPDATE leases SET lease_expires_at = ?, heartbeat_at = ?, holder = ?, - granted_node = IF(COALESCE(granted_node, '') = '', ?, granted_node) - WHERE issue_id = ? AND holder = ? - `, now.Add(ttl), now, actor, NodeID(ctx), id, leaseHolder); err != nil { - return LeaseRenewal{}, fmt.Errorf("failed to heartbeat issue: %w", err) - } - return LeaseRenewal{HeartbeatAt: reported, ExpiresAt: reported.Add(ttl)}, nil - case lerr != nil && !errors.Is(lerr, sql.ErrNoRows): - return LeaseRenewal{}, fmt.Errorf("read lease for %s: %w", id, lerr) - } - // No lease row the caller holds: grant one. THIS IS A MUTATION, - // recorded as every bead mutator records one: row_lock reminted, - // updated_at stamped, a lease_rearmed event, a journal entry and a - // version row (the legs then make the version-control commit and - // fire the update hook, keyed on LeaseRenewal.Rearmed). Why the - // remint is load-bearing: The live claim this branch acts on was read in - // THIS transaction's snapshot, but a snapshot cannot see a release - // or close committing concurrently, and a lease INSERT shares no - // row with such an ender (its DELETE FROM leases matched nothing in - // its own snapshot): Dolt would commit both and leave an orphaned - // lease on an open or closed issue. Every claim-ending path - // rewrites row_lock (the freshRowLock invariant), so reminting it - // here makes the grant COLLIDE with any concurrent ender, and the - // loser is replayed against the winner's state: either the ender - // sees and deletes this lease, or this heartbeat sees the ended - // claim and is refused. Concurrent re-arms collide with each other - // the same way, so exactly one reports Rearmed. - // - // THE COST, accepted: a re-arm (a hand-doled claim's first - // heartbeat, once) remints the holder's revision token, so a - // guarded write composed before it fails once and re-reads. A - // REFRESH never touches the issue row and records nothing. - if _, err := tx.ExecContext(ctx, `UPDATE issues SET row_lock = ?, updated_at = ? WHERE id = ?`, - freshRowLock(), now, id); err != nil { - return LeaseRenewal{}, fmt.Errorf("remint row_lock for %s: %w", id, err) - } - if err := UpsertLeaseInTx(ctx, tx, id, actor, now, ttl); err != nil { - return LeaseRenewal{}, err - } - if err := RecordFullEventInTable(ctx, tx, "events", id, types.EventLeaseRearmed, actor, "", actor); err != nil { - return LeaseRenewal{}, fmt.Errorf("record lease re-arm event for %s: %w", id, err) - } - if err := RecordEventInTx(ctx, tx, EventUpdate, id, actor); err != nil { - return LeaseRenewal{}, err - } - if err := RecordVersionInTx(ctx, tx, id, actor); err != nil { - return LeaseRenewal{}, err - } - return LeaseRenewal{HeartbeatAt: reported, ExpiresAt: reported.Add(ttl), Rearmed: true}, nil + // worker's heartbeat re-arms recovery — under actor's current + // spelling, which is what the next heartbeat's fast path will see. + return UpsertLeaseInTx(ctx, tx, id, actor, now, leaseTTL(ctx)) } - return LeaseRenewal{}, refused(fmt.Errorf("%w: %s status %s", storage.ErrNotClaimable, id, status)) + return fmt.Errorf("%w: %s status %s", storage.ErrNotClaimable, id, status) } - // A REFRESH IS DELIBERATELY NOT JOURNALED — do not add an emit here (the - // re-arm above is the one heartbeat path recorded as a mutation). A heartbeat is a + // DELIBERATELY NOT JOURNALED — do not add an emit here. A heartbeat is a // high-frequency lease keepalive: it writes only the clone-local `leases` // table (lease-liveness state), never a durable bead field. Journaling it // would put a full-snapshot write plus the shared seq-counter serialization @@ -666,8 +410,8 @@ func RenewLeaseInTx(ctx context.Context, tx DBTX, id, actor string) (LeaseRenewa // lease state lives on the working-set plane and expires on its own. Lease // RECLAIM is the opposite case and does journal: it clears assignee and // reverts status, which is durable bead state. The decision is pinned by - // mutationEntryPoints' heartbeat note in journal_completeness_test.go. - return LeaseRenewal{HeartbeatAt: reported, ExpiresAt: reported.Add(ttl)}, nil + // journalExemptMutations in journal_completeness_test.go. + return nil } // warnReplica writes a replica-guard audit line to STDERR (never stdout — @@ -923,11 +667,6 @@ func reclaimReplicaSQL(filter types.ReclaimFilter, localNode string) (string, [] // the lease from) so the caller can log/emit recovery events. The caller owns // Dolt versioning. // -// This is the one place a reclaimed row's revision token is minted: the row's -// freshRowLock() value is captured off the revert UPDATE and published on -// each types.ReclaimedLease as types.RevisionToken(newLock), the same -// encoding IssueDetails.Revision uses. -// // filter narrows which stale leases are eligible (see types.ReclaimFilter); the // zero filter keeps the historical global behavior. Scoping is applied to the // snapshot SELECT only — the per-row DELETE/UPDATE re-checks staleness by id, @@ -1015,13 +754,12 @@ func ReclaimExpiredLeasesInTx(ctx context.Context, tx DBTX, cutoff time.Time, fi // being in_progress under us (closed) is left alone; row_lock makes a // concurrent close/update conflict at commit time rather than // cell-merge with this write. - newLock := freshRowLock() res, err = tx.ExecContext(ctx, ` UPDATE issues SET status = 'open', assignee = NULL, started_at = NULL, updated_at = ?, row_lock = ? WHERE id = ? AND status = 'in_progress' - `, time.Now().UTC(), newLock, r.ID) + `, time.Now().UTC(), freshRowLock(), r.ID) if err != nil { return nil, fmt.Errorf("reclaim %s: %w", r.ID, err) } @@ -1032,7 +770,6 @@ func ReclaimExpiredLeasesInTx(ctx context.Context, tx DBTX, cutoff time.Time, fi if n == 0 { continue // no longer in_progress — its lease row was stale anyway } - r.Revision = types.RevisionToken(newLock) if err := RecordFullEventInTable(ctx, tx, "events", r.ID, types.EventLeaseReclaimed, actor, r.PreviousOwner, ""); err != nil { return nil, fmt.Errorf("record reclaim event for %s: %w", r.ID, err) @@ -1043,12 +780,6 @@ func ReclaimExpiredLeasesInTx(ctx context.Context, tx DBTX, cutoff time.Time, fi if err := RecordEventInTx(ctx, tx, EventUpdate, r.ID, actor); err != nil { return nil, err } - // The revert rewrote assignee, status and started_at — durable state — - // so the reclaimed issue gets a version row beside its journal entry, - // past the same two re-checks. - if err := RecordVersionInTx(ctx, tx, r.ID, actor); err != nil { - return nil, err - } // Logged HERE, past both re-checks, so the audit trail records reverts // that actually happened: a heartbeat landing after the snapshot makes // the DELETE match nothing, and a "reverting X" line printed up there diff --git a/internal/storage/issueops/lease_reclaimer.go b/internal/storage/issueops/lease_reclaimer.go deleted file mode 100644 index ba0e8b4cb..000000000 --- a/internal/storage/issueops/lease_reclaimer.go +++ /dev/null @@ -1,123 +0,0 @@ -package issueops - -import ( - "context" - "fmt" - "strings" - "time" - - "github.com/steveyegge/beads/internal/storage" - "github.com/steveyegge/beads/internal/types" - publicops "github.com/steveyegge/beads/issueops" -) - -// ValidateReclaimRequest applies the request rules every LeaseReclaimer -// implementation shares. -// -// THE CAP IS CHECKED AGAINST Filter.IDs AS SENT, before deduplication, for -// ValidateGetManyRequest's reason: the obligation it bounds is reading the -// request apart, not reading the rows it resolves to. -// -// THE ACTOR IS BOUNDED HERE, trimmed as the sweep records it, for -// claimIssueInTx's reason: every reverted row's recovery event lands in a -// VARCHAR(255) column, so an over-long actor is a typed refusal on every leg -// rather than a raw backend error on the first sweep that reverts something. -// -// It is the ONE definition of the role's rules: the library legs run it, and -// the HTTP client restates it (pinned by -// TestReclaimValidationMatchesTheSharedValidator). Every refusal but the cap -// is a *publicops.ReclaimFieldError naming its field. bd serve's handler leaves -// the id cap and the blank entries to it, and adds three rules of the wire's -// own that a library caller is not held to: the claim's name rules on the -// actor (a 256-byte bound, no control characters), an `older_than_seconds` a -// time.Duration can hold, and no scope member present but empty. -func ValidateReclaimRequest(request publicops.ReclaimRequest) error { - actor := strings.TrimSpace(request.Actor) - if actor == "" { - return reclaimFieldError(publicops.ReclaimFieldActor, "reclaim actor is required") - } - if err := types.CheckFieldLen("actor", actor); err != nil { - return reclaimFieldError(publicops.ReclaimFieldActor, err.Error()) - } - if request.OlderThan < 0 { - return reclaimFieldError(publicops.ReclaimFieldOlderThan, "reclaim older_than must not be negative") - } - if len(request.Filter.IDs) > publicops.MaxReclaimIDs { - return &publicops.TooManyReclaimIDsError{Requested: len(request.Filter.IDs), Cap: publicops.MaxReclaimIDs} - } - for i, id := range request.Filter.IDs { - if id == "" { - return reclaimFieldError(publicops.ReclaimFieldIDs, fmt.Sprintf("reclaim scope id at position %d is empty", i)) - } - } - for _, scope := range []struct { - field string - values []string - }{ - {publicops.ReclaimFieldAssignees, request.Filter.Assignees}, - {publicops.ReclaimFieldLabels, request.Filter.Labels}, - {publicops.ReclaimFieldLabelsAny, request.Filter.LabelsAny}, - {publicops.ReclaimFieldExcludeLabels, request.Filter.ExcludeLabels}, - } { - for i, v := range scope.values { - if strings.TrimSpace(v) == "" { - return reclaimFieldError(scope.field, fmt.Sprintf("reclaim %s entry at position %d is blank", scope.field, i)) - } - } - } - return nil -} - -func reclaimFieldError(field, detail string) error { - return &publicops.ReclaimFieldError{Field: field, Detail: storage.ErrValidation.Error() + ": " + detail} -} - -// ExecuteReclaimInTx sweeps stale leases from tx, in ONE transaction. It is -// the body behind the LeaseReclaimer accessor on ALL THREE legs: the two -// stores wrap it in their own retrying write transaction, and the -// unit-of-work provider reaches it through the domain repository, whose -// runner publishes exactly the DBTX method set ReclaimExpiredLeasesInTx -// takes. -// -// VALIDATION HAPPENS HERE, for ExecuteGetMany's reason: this body is the only -// body, so a leg that forgot to validate would be answering a different -// contract. It runs before the cutoff is computed and before the sweep's -// query, so a request that fails validation never reaches storage. -// -// THE CUTOFF IS COMPUTED HERE, once, from time.Now().UTC().Add(-OlderThan), so -// every leg sweeps against the same clock read rather than each leg taking its -// own — the dolt and embedded-dolt wrappers and the unit-of-work repository -// all call this function with the request, never with a precomputed cutoff. -func ExecuteReclaimInTx(ctx context.Context, tx DBTX, request publicops.ReclaimRequest) (publicops.ReclaimResult, error) { - if err := ValidateReclaimRequest(request); err != nil { - return publicops.ReclaimResult{}, err - } - cutoff := time.Now().UTC().Add(-request.OlderThan) - reclaimed, err := ReclaimExpiredLeasesInTx(ctx, tx, cutoff, request.Filter, strings.TrimSpace(request.Actor)) - if err != nil { - return publicops.ReclaimResult{}, fmt.Errorf("reclaim: %w", err) - } - if reclaimed == nil { - reclaimed = []publicops.ReclaimedLease{} - } - return publicops.ReclaimResult{Reclaimed: reclaimed}, nil -} - -// ReclaimVersionCommit composes the version-control entry for a finished sweep: -// the durable tables it changed and the message to record them under. A sweep -// that reverted nothing composes neither, which every leg's commit funnel reads -// as "write nothing to history", so a no-op reaper tick leaves no entry -// describing a no-op. -// -// It is ONE helper the three legs share so the message cannot drift between -// them: the same "bd: reclaim N expired lease(s)" the raw ReclaimExpiredLeases -// has always recorded. The staged set is the one the release records, issues -// and events, because a reclaim is the same revert applied by a clock. -func ReclaimVersionCommit(result publicops.ReclaimResult) (ChangedTables, string) { - if len(result.Reclaimed) == 0 { - return nil, "" - } - tables := ChangedTables{} - tables.Add("issues", "events") - return tables, fmt.Sprintf("bd: reclaim %d expired lease(s)", len(result.Reclaimed)) -} diff --git a/internal/storage/issueops/lease_reclaimer_test.go b/internal/storage/issueops/lease_reclaimer_test.go deleted file mode 100644 index 5b97b50af..000000000 --- a/internal/storage/issueops/lease_reclaimer_test.go +++ /dev/null @@ -1,107 +0,0 @@ -package issueops - -import ( - "context" - "errors" - "strings" - "testing" - "time" - - "github.com/DATA-DOG/go-sqlmock" - - "github.com/steveyegge/beads/internal/types" - publicops "github.com/steveyegge/beads/issueops" -) - -// TestValidateReclaimRequestRefusesInTheDocumentedOrder pins the database-free -// tier of the role's refusals: each rule on both sides, and the cap checked -// before the blank scan, against the request as sent. -func TestValidateReclaimRequestRefusesInTheDocumentedOrder(t *testing.T) { - overCap := make([]string, publicops.MaxReclaimIDs+1) - atCap := make([]string, publicops.MaxReclaimIDs) - for i := range atCap { - atCap[i] = "bd-x" - } - for _, test := range []struct { - name string - request publicops.ReclaimRequest - wantErr bool - wantCap bool - }{ - {"a bare valid request", publicops.ReclaimRequest{Actor: "reaper"}, false, false}, - {"a zero grace window", publicops.ReclaimRequest{Actor: "reaper", OlderThan: 0}, false, false}, - {"exactly the cap", publicops.ReclaimRequest{Actor: "reaper", Filter: publicops.ReclaimFilter{IDs: atCap}}, false, false}, - {"no actor", publicops.ReclaimRequest{}, true, false}, - {"a blank actor", publicops.ReclaimRequest{Actor: " "}, true, false}, - // The actor is bounded as the sweep records it: trimmed, then held to - // its column, so padding around a value at the bound is not over it. - {"an actor at the bound", publicops.ReclaimRequest{Actor: strings.Repeat("r", types.MaxFieldLen)}, false, false}, - {"a padded actor at the bound", publicops.ReclaimRequest{Actor: " " + strings.Repeat("r", types.MaxFieldLen) + " "}, false, false}, - {"an over-long actor", publicops.ReclaimRequest{Actor: strings.Repeat("r", types.MaxFieldLen+1)}, true, false}, - {"a negative grace window", publicops.ReclaimRequest{Actor: "reaper", OlderThan: -time.Nanosecond}, true, false}, - {"a blank id", publicops.ReclaimRequest{Actor: "reaper", Filter: publicops.ReclaimFilter{IDs: []string{"bd-1", ""}}}, true, false}, - {"a blank assignee", publicops.ReclaimRequest{Actor: "reaper", Filter: publicops.ReclaimFilter{Assignees: []string{"w", " "}}}, true, false}, - {"a blank label", publicops.ReclaimRequest{Actor: "reaper", Filter: publicops.ReclaimFilter{Labels: []string{""}}}, true, false}, - {"a blank label-any", publicops.ReclaimRequest{Actor: "reaper", Filter: publicops.ReclaimFilter{LabelsAny: []string{"\t"}}}, true, false}, - {"a blank exclude", publicops.ReclaimRequest{Actor: "reaper", Filter: publicops.ReclaimFilter{ExcludeLabels: []string{"x", ""}}}, true, false}, - // Every entry blank AND one past the cap: the cap answers, so a caller - // that sent 1001 ids learns about the cap rather than about an entry. - {"over the cap of blanks", publicops.ReclaimRequest{Actor: "reaper", Filter: publicops.ReclaimFilter{IDs: overCap}}, true, true}, - } { - t.Run(test.name, func(t *testing.T) { - err := ValidateReclaimRequest(test.request) - if (err != nil) != test.wantErr { - t.Fatalf("ValidateReclaimRequest = %v, want error %v", err, test.wantErr) - } - if err == nil { - return - } - if !errors.Is(err, publicops.ErrValidation) { - t.Fatalf("ValidateReclaimRequest = %v, want ErrValidation", err) - } - var capErr *publicops.TooManyReclaimIDsError - if got := errors.As(err, &capErr); got != test.wantCap { - t.Fatalf("errors.As(*TooManyReclaimIDsError) = %v, want %v (err %v)", got, test.wantCap, err) - } - var fieldErr *publicops.ReclaimFieldError - if !test.wantCap && (!errors.As(err, &fieldErr) || fieldErr.Field == "") { - t.Fatalf("refusal %v names no field; every refusal but the cap is a *ReclaimFieldError", err) - } - }) - } -} - -// TestExecuteReclaimInTxRefusesBeforeAnyQuery pins "a request that fails -// validation never reaches storage": the mock expects nothing, so any query -// would fail the test. -func TestExecuteReclaimInTxRefusesBeforeAnyQuery(t *testing.T) { - db, mock, err := sqlmock.New() - if err != nil { - t.Fatalf("sqlmock.New: %v", err) - } - defer db.Close() - - _, err = ExecuteReclaimInTx(context.Background(), db, publicops.ReclaimRequest{}) - if !errors.Is(err, publicops.ErrValidation) { - t.Fatalf("ExecuteReclaimInTx with no actor = %v, want ErrValidation", err) - } - if err := mock.ExpectationsWereMet(); err != nil { - t.Fatal(err) - } -} - -// TestReclaimVersionCommitComposesNothingForANoOp pins the shared commit -// composition: no entry for a sweep that reverted nothing, and one entry -// naming the count, staging issues and events, for one that did. -func TestReclaimVersionCommitComposesNothingForANoOp(t *testing.T) { - if tables, msg := ReclaimVersionCommit(publicops.ReclaimResult{Reclaimed: []publicops.ReclaimedLease{}}); len(tables) != 0 || msg != "" { - t.Fatalf("a no-op sweep composed tables=%v msg=%q, want neither", tables, msg) - } - tables, msg := ReclaimVersionCommit(publicops.ReclaimResult{Reclaimed: []publicops.ReclaimedLease{{ID: "a"}, {ID: "b"}}}) - if !tables["issues"] || !tables["events"] || len(tables) != 2 { - t.Fatalf("tables = %v, want issues and events", tables) - } - if !strings.Contains(msg, "reclaim 2 expired lease(s)") { - t.Fatalf("message = %q, want it to name the two reverted leases", msg) - } -} diff --git a/internal/storage/issueops/molecule.go b/internal/storage/issueops/molecule.go index ac42c4de8..be765c7a7 100644 --- a/internal/storage/issueops/molecule.go +++ b/internal/storage/issueops/molecule.go @@ -3,13 +3,10 @@ package issueops import ( "context" "database/sql" - "errors" "fmt" - "strconv" "strings" "github.com/steveyegge/beads/internal/types" - publicops "github.com/steveyegge/beads/issueops" ) // GetMoleculeProgressInTx returns progress stats for a molecule within an @@ -114,191 +111,3 @@ func GetMoleculeProgressInTx(ctx context.Context, tx *sql.Tx, moleculeID string) return stats, nil } - -// TxMoleculeReader binds the molecule rules (issueops.MoleculeReader) to a -// caller's transaction: it runs the SAME BatchGetter, Relations and EdgeReader -// bodies the store-backed roles run, against tx, so a close that decides -// whether it completed a molecule reads the state its own writes produced. -type TxMoleculeReader struct{ Tx DBTX } - -var _ = publicops.MoleculeReaderOver(TxMoleculeReader{}) - -// GetMany is the BatchGetter body (ExecuteGetMany) in the bound transaction. -func (r TxMoleculeReader) GetMany(ctx context.Context, request publicops.GetManyRequest) (publicops.GetManyResult, error) { - return ExecuteGetMany(ctx, r.Tx, request) -} - -// Related is the Relations body (ExecuteRelated) in the bound transaction. -func (r TxMoleculeReader) Related(ctx context.Context, request publicops.RelatedRequest) ([]*publicops.RelatedIssue, error) { - if err := ValidateRelatedRequest(request); err != nil { - return nil, err - } - return ExecuteRelated(ctx, r.Tx, request) -} - -// ReadEdges is the EdgeReader body (ExecuteEdgeRead) in the bound transaction. -func (r TxMoleculeReader) ReadEdges(ctx context.Context, request publicops.EdgeReadRequest) (publicops.EdgeReadResult, error) { - if err := ValidateEdgeReadRequest(request); err != nil { - return publicops.EdgeReadResult{}, err - } - return ExecuteEdgeRead(ctx, r.Tx, request) -} - -// MoleculeAutoClose is what one molecule auto-close did. -type MoleculeAutoClose struct { - // Root is the post-close snapshot of the root it closed, or nil. - Root *types.Issue - // Refusal is why a completed root stayed open (its close policy or a - // close guard refused it), or "": the refusal's own sentence - // (RefusedMoleculeAutoClose). - Refusal string -} - -// RefusedMoleculeAutoClose is the MoleculeAutoClose for a root whose close -// failed with err, a refusal (issueops.IsMoleculeAutoCloseRefusal). Its -// Refusal is the sentence the refusing guard or close policy wrote, the same -// on every backend. ExecuteClose returns a refusal as it was minted, while the -// unit of work's use case and repository wrap it on the way out ("close : -// db: IssueSQLRepository.CloseChecked : …"); the sentence already names -// the root, so the wrap would only leak a repository type into an -// operator-facing field (bd close --json's molecule_auto_close_refusal). -func RefusedMoleculeAutoClose(err error) MoleculeAutoClose { - var ( - blocked *publicops.BlockedError - openChildren *publicops.CloseOpenChildrenError - template *publicops.TemplateReadOnlyError - pinned *publicops.PinnedError - notAssignee *publicops.CloseNotAssigneeError - ) - switch { - case errors.As(err, &blocked): - err = blocked - case errors.As(err, &openChildren): - err = openChildren - case errors.As(err, &template): - err = template - case errors.As(err, &pinned): - err = pinned - case errors.As(err, ¬Assignee): - err = notAssignee - } - return MoleculeAutoClose{Refusal: err.Error()} -} - -// moleculeCoordinationKeyPrefix names the local_metadata cells an auto-close -// rewrites (TouchMoleculeCoordinationInTx). -const moleculeCoordinationKeyPrefix = "molecule-coordination/v1/" - -// MoleculeCoordinationKey is the local_metadata cell a molecule auto-close -// rewrites before it decides rootID. It shares the dependency coordination -// cells' sharding (4096 rows): the same root always resolves to the same -// cell, and a hash collision between two roots only adds a safe -// serialization conflict. -func MoleculeCoordinationKey(rootID string) string { - return fmt.Sprintf("%s%03x", moleculeCoordinationKeyPrefix, dependencyCoordinationShard(rootID)) -} - -// TouchMoleculeCoordinationInTx rewrites rootID's molecule coordination cell -// with a fresh value, the dependency coordination idiom -// (TouchDependencyCoordinationTableInTx) applied to a molecule's completion. -// -// Two closes that finish a molecule's last two steps in concurrent -// transactions each read the other's step as still open, so neither closes the -// root, and their writes touch disjoint rows: both commit and the complete -// molecule's root is stranded open. Every auto-close that decides a root -// rewrites this one cell first, so two transactions deciding the same root -// conflict at commit instead; the loser's retry reads the winner's close and -// closes the root. -// -// The cost is that sibling closes serialize. The cell is rewritten before the -// steps are read, by every close under the open root and not only the one that -// completes it, so any two concurrent closes of steps under the same root -// conflict and one of them retries. Under heavy fan-out a close can spend its -// whole retry budget conflicting and then fails with the serialization error. -func TouchMoleculeCoordinationInTx(ctx context.Context, tx DBTX, rootID string) error { - if rootID == "" { - return fmt.Errorf("touch molecule coordination: root ID must not be empty") - } - if _, err := tx.ExecContext(ctx, - "REPLACE INTO local_metadata (`key`, value) VALUES (?, ?)", MoleculeCoordinationKey(rootID), strconv.FormatInt(FreshRowLock(), 10)); err != nil { - return fmt.Errorf("touch molecule coordination for %s: %w", rootID, err) - } - return nil -} - -// CloseCompletedMoleculeInTx is CloseRequest.AutoCloseMolecule's body for the -// store-backed backends: CloseCompletedMoleculesInTx for one step. -func CloseCompletedMoleculeInTx(ctx context.Context, tx *sql.Tx, stepID, actor, session string) (MoleculeAutoClose, ChangedTables, error) { - autos, tables, err := CloseCompletedMoleculesInTx(ctx, tx, []string{stepID}, actor, session) - if err != nil { - return MoleculeAutoClose{}, nil, err - } - return autos[0], tables, nil -} - -// CloseCompletedMoleculesInTx is CloseBatchRequest.AutoCloseMolecule's body -// for the store-backed backends, and through CloseCompletedMoleculeInTx -// CloseRequest.AutoCloseMolecule's. For every auto-closing molecule root a -// close of stepIDs could have completed (issueops.AutoClosingMoleculeRoots, -// read in tx), it rewrites the root's coordination cell -// (TouchMoleculeCoordinationInTx), and when every step under the root is now -// closed (issueops.MoleculeStepsClosed) it closes the root in tx through -// ExecuteClose — issueops.MoleculeRootClose: unforced, guarded on the root -// revision read in the same transaction, recording -// issueops.MoleculeAutoCloseReason and session. -// -// It answers one MoleculeAutoClose per step id, in order. Each root is decided -// once, and what happened to it is reported on the first step under it; every -// other step under that root answers the zero value. -// -// A refusal of the root (issueops.IsMoleculeAutoCloseRefusal) is answered as -// MoleculeAutoClose.Refusal and writes nothing; the caller's own closes stand. -// Any other failure is returned and fails the caller's transaction. -func CloseCompletedMoleculesInTx(ctx context.Context, tx *sql.Tx, stepIDs []string, actor, session string) ([]MoleculeAutoClose, ChangedTables, error) { - reader := publicops.MoleculeReaderOver(TxMoleculeReader{Tx: tx}) - roots, err := publicops.AutoClosingMoleculeRoots(ctx, reader, stepIDs) - if err != nil { - return nil, nil, err - } - autos := make([]MoleculeAutoClose, len(stepIDs)) - tables := ChangedTables{} - decided := make(map[string]bool, len(roots)) - for i, stepID := range stepIDs { - root := roots[stepID] - if root == nil || decided[root.ID] { - continue - } - decided[root.ID] = true - auto, rootTables, err := closeCompletedMoleculeRootInTx(ctx, tx, reader, root, actor, session) - if err != nil { - return nil, nil, err - } - tables.Merge(rootTables) - autos[i] = auto - } - return autos, tables, nil -} - -func closeCompletedMoleculeRootInTx(ctx context.Context, tx *sql.Tx, reader publicops.MoleculeReader, root *types.Issue, actor, session string) (MoleculeAutoClose, ChangedTables, error) { - if err := TouchMoleculeCoordinationInTx(ctx, tx, root.ID); err != nil { - return MoleculeAutoClose{}, nil, err - } - complete, err := publicops.MoleculeStepsClosed(ctx, reader, root.ID) - if err != nil || !complete { - return MoleculeAutoClose{}, nil, err - } - closed, tables, err := ExecuteClose(ctx, tx, publicops.MoleculeRootClose(root, actor, session)) - if publicops.IsMoleculeAutoCloseRefusal(err) { - return RefusedMoleculeAutoClose(err), nil, nil - } - if err != nil { - // Not a refusal, such as a storage error. Returned, it fails the - // caller's transaction, so the step's own close rolls back with it - // (CloseRequest.AutoCloseMolecule). - return MoleculeAutoClose{}, nil, fmt.Errorf("auto-closing molecule %s: %w", root.ID, err) - } - if !closed.Changed { - return MoleculeAutoClose{}, tables, nil - } - return MoleculeAutoClose{Root: closed.Issue}, tables, nil -} diff --git a/internal/storage/issueops/persistence.go b/internal/storage/issueops/persistence.go index 18c7084f4..36c83a367 100644 --- a/internal/storage/issueops/persistence.go +++ b/internal/storage/issueops/persistence.go @@ -2,10 +2,8 @@ package issueops import ( "context" - "errors" "fmt" - "github.com/steveyegge/beads/internal/storage" "github.com/steveyegge/beads/internal/types" ) @@ -13,42 +11,15 @@ import ( type PersistenceMoveResult struct { Changed bool ChangedTables map[string]bool - // EnteredIssuesPlane reports that the move took the row out of the wisps - // table into issues. The issues row is new and a wisp never declares - // participation, so the record's next version is its first and must be - // minted create-shaped (design §16.2b). A same-plane repair does not set - // it: that row keeps its own participation. A caller that mints for the - // move itself, as ExecuteUpdate does, reads this to pick the entry point. - EnteredIssuesPlane bool } -// MoveIssuePersistenceInTx moves a complete issue aggregate to the requested -// persistence mode. A move that lands (or normalizes) the row on the issues -// plane mints a version row once the target row and its auxiliary copies -// exist; a move whose target is the wisp plane mints nothing (wisps are never -// versioned). +// MoveIssuePersistenceInTx moves a complete issue aggregate to the requested persistence mode. func MoveIssuePersistenceInTx(ctx context.Context, tx DBTX, current *types.Issue, mode types.PersistenceMode, actor string) (PersistenceMoveResult, error) { - return moveIssuePersistenceInTx(ctx, tx, current, mode, actor, true) -} - -// moveIssuePersistenceInTx is the body of MoveIssuePersistenceInTx. -// mintVersion controls whether the move mints its own version row: the -// exported entry point always does, while ExecuteUpdate applies the -// persistence patch last and mints once for the whole guarded update. -func moveIssuePersistenceInTx(ctx context.Context, tx DBTX, current *types.Issue, mode types.PersistenceMode, actor string, mintVersion bool) (PersistenceMoveResult, error) { if tx == nil || current == nil || current.ID == "" { return PersistenceMoveResult{}, fmt.Errorf("move issue persistence: issue and transaction are required") } ephemeral, noHistory, storageClass, err := types.NormalizePersistenceMode(*current, mode) if err != nil { - // The unversioned refusal is the request's: the row is sound and the - // move it asks for is one its class never makes. The other errors - // describe a row that breaks its own persistence invariants, so they - // stay unclassified; an invalid mode is refused before any update - // path gets here (ValidateUpdateRequest). - if errors.Is(err, types.ErrUnversionedWispMove) { - return PersistenceMoveResult{}, fmt.Errorf("%w: %w", storage.ErrValidation, err) - } return PersistenceMoveResult{}, err } sourceWisp, err := isActiveWispForPersistenceMove(ctx, tx, current.ID) @@ -87,17 +58,6 @@ func moveIssuePersistenceInTx(ctx context.Context, tx DBTX, current *types.Issue if err := RecordEventInTx(ctx, tx, EventUpdate, current.ID, actor); err != nil { return PersistenceMoveResult{}, err } - // Versioned only on the issues plane; a wisp-plane normalize keeps - // the wisp exclusion. Unlike a move, this mint stays update-shaped - // (design §16.2b): the row already lives on the issues plane and - // keeps its own participation_generation. A row created with the flag - // on proceeds; a legacy row stays legacy, since a repair is an - // ordinary write and must not promote it (R2.3). - if mintVersion && !targetWisp { - if err := RecordVersionInTx(ctx, tx, current.ID, actor); err != nil { - return PersistenceMoveResult{}, err - } - } return result, nil } if !sourceWisp && targetWisp { @@ -164,21 +124,10 @@ func moveIssuePersistenceInTx(ctx context.Context, tx DBTX, current *types.Issue result.ChangedTables[persistenceIssueTable(targetWisp)] = true // The bead keeps its ID across a plane move; only where it is stored // changes. Journal one update carrying the moved snapshot, after derived - // blocked-state maintenance has settled. A move INTO the issues plane - // then mints the row's first version there, after its auxiliary copies - // and retargeted edges exist; a move into the wisp plane mints nothing. - // That first mint is create-shaped (design §16.2b): the issues row was - // inserted above without participation_generation, which a wisp never - // declares, so the update-shaped entry would fence it as legacy. + // blocked-state maintenance has settled. if err := RecordEventInTx(ctx, tx, EventUpdate, current.ID, actor); err != nil { return PersistenceMoveResult{}, err } - result.EnteredIssuesPlane = !targetWisp - if mintVersion && !targetWisp { - if err := RecordVersionForCreateInTx(ctx, tx, current.ID, actor); err != nil { - return PersistenceMoveResult{}, err - } - } return result, nil } @@ -189,9 +138,6 @@ func persistenceIssueTable(wisp bool) string { return "issues" } -// rejectPersistenceDemotion refuses a durable-to-wisp move of a row that has -// retained snapshots. The refusal is the request's (ErrValidation); a failed -// count is not. func rejectPersistenceDemotion(ctx context.Context, tx DBTX, id string) error { for _, table := range []string{"issue_snapshots", "compaction_snapshots"} { var count int @@ -199,7 +145,7 @@ func rejectPersistenceDemotion(ctx context.Context, tx DBTX, id string) error { return fmt.Errorf("check retained snapshots in %s: %w", table, err) } if count > 0 { - return fmt.Errorf("%w: cannot demote issue %s with retained snapshots", storage.ErrValidation, id) + return fmt.Errorf("cannot demote issue %s with retained snapshots", id) } } return nil diff --git a/internal/storage/issueops/prepare_timestamps_utc_test.go b/internal/storage/issueops/prepare_timestamps_utc_test.go deleted file mode 100644 index 74efedb4b..000000000 --- a/internal/storage/issueops/prepare_timestamps_utc_test.go +++ /dev/null @@ -1,72 +0,0 @@ -package issueops - -import ( - "testing" - "time" - - "github.com/steveyegge/beads/internal/types" -) - -// TestPrepareIssueForInsertNormalizesOptionalTimestampsToUTC is a born-failing -// reproduction for #5765: on JSONL import an optional timestamp carrying a -// non-UTC offset (e.g. closed_at with -04:00 from an older SQLite-era export) -// kept its local wall-clock digits, so the stored instant shifted by the offset -// while created_at/updated_at were converted correctly. PrepareIssueForInsert -// must normalize every set optional timestamp to true UTC, exactly as it already -// does for CreatedAt/UpdatedAt. -func TestPrepareIssueForInsertNormalizesOptionalTimestampsToUTC(t *testing.T) { - // America/New_York style EDT offset, without depending on tz database load. - edt := time.FixedZone("EDT", -4*60*60) - - // 2026-08-13T10:20:22-04:00 == 2026-08-13T14:20:22Z (the reporter's imp-100). - closed := time.Date(2026, 8, 13, 10, 20, 22, 0, edt) - started := time.Date(2026, 8, 13, 10, 17, 47, 0, edt) - due := time.Date(2026, 8, 20, 9, 0, 0, 0, edt) - deferUntil := time.Date(2026, 8, 15, 8, 30, 0, 0, edt) - - issue := &types.Issue{ - ID: "imp-100", - Title: "EDT offset", - IssueType: types.TypeTask, - Status: types.StatusClosed, - Priority: 2, - CreatedAt: started, - UpdatedAt: closed, - ClosedAt: &closed, - StartedAt: &started, - DueAt: &due, - DeferUntil: &deferUntil, - } - - if err := PrepareIssueForInsert(issue, nil, nil); err != nil { - t.Fatalf("PrepareIssueForInsert: %v", err) - } - - checks := []struct { - name string - got *time.Time - want time.Time - }{ - {"closed_at", issue.ClosedAt, closed}, - {"started_at", issue.StartedAt, started}, - {"due_at", issue.DueAt, due}, - {"defer_until", issue.DeferUntil, deferUntil}, - } - for _, c := range checks { - if c.got == nil { - t.Fatalf("%s was cleared to nil", c.name) - } - if c.got.Location() != time.UTC { - t.Errorf("%s location = %v, want UTC (offset digits kept instead of converting)", c.name, c.got.Location()) - } - if !c.got.Equal(c.want) { - t.Errorf("%s instant = %s, want %s", c.name, c.got.Format(time.RFC3339), c.want.UTC().Format(time.RFC3339)) - } - } - - // The reported field, checked on its exact wall-clock digits: -04:00 dropped - // means 10:20:22Z (wrong); converted means 14:20:22Z (correct). - if wall := issue.ClosedAt.Format("2006-01-02T15:04:05Z07:00"); wall != "2026-08-13T14:20:22Z" { - t.Errorf("closed_at = %s, want 2026-08-13T14:20:22Z", wall) - } -} diff --git a/internal/storage/issueops/promote.go b/internal/storage/issueops/promote.go index 1d3a3d449..3456651ed 100644 --- a/internal/storage/issueops/promote.go +++ b/internal/storage/issueops/promote.go @@ -53,8 +53,7 @@ func PromoteFromEphemeralInTx(ctx context.Context, tx DBTX, id string, actor str if err := PrepareIssueForInsert(issue, types.CustomStatusNames(customStatuses), customTypes); err != nil { return fmt.Errorf("promote wisp to issues: %w", err) } - isNew, _, err := InsertIssueIfNew(ctx, tx, "issues", issue, storage.BatchCreateOptions{}) - if err != nil { + if _, _, err := InsertIssueIfNew(ctx, tx, "issues", issue, storage.BatchCreateOptions{}); err != nil { return fmt.Errorf("promote wisp to issues: %w", err) } @@ -132,24 +131,9 @@ func PromoteFromEphemeralInTx(ctx context.Context, tx DBTX, id string, actor str // The bead keeps its ID across promotion; only its plane changes. Journal // one update carrying the now-durable snapshot, after derived blocked-state - // maintenance has settled — and mint its first version: the promoted row - // is new durable state on the issues plane (a wisp is never versioned, so - // nothing precedes it), minted once after its labels, dependencies and - // comments have been copied across. That makes the mint create-shaped - // (design §16.2b): the issues row inserted above has no - // participation_generation, since a wisp never declares one, and the - // update-shaped entry would read that NULL as legacy and skip this mint - // and every later one. The exception is an id the issues plane already - // held (GH#4455 records such dual presence): GetIssueInTx read that row, - // the upsert resolved to it, and it keeps whatever participation it had, - // so the mint is update-shaped, as finishCreateIssueInTx's is for a create - // that resolved to an existing row. Stamping it would promote a legacy - // record (R2.3). + // maintenance has settled. if err := RecordEventInTx(ctx, tx, EventUpdate, id, actor); err != nil { return err } - if isNew { - return RecordVersionForCreateInTx(ctx, tx, id, actor) - } - return RecordVersionInTx(ctx, tx, id, actor) + return nil } diff --git a/internal/storage/issueops/public_create.go b/internal/storage/issueops/public_create.go index a119aaf8a..693edac9e 100644 --- a/internal/storage/issueops/public_create.go +++ b/internal/storage/issueops/public_create.go @@ -47,30 +47,9 @@ func ValidatePublicCreateRequest(request publicops.CreateRequest) error { if err := types.CheckFieldLen("parent ID", request.ParentID); err != nil { return publicCreateValidationError(fmt.Errorf("create: %w", err)) } - // The gate members, as a *publicops.GateFieldError naming the member so a - // front door can point at it. Here rather than in PreparePublicCreateRequest - // alone so the unit-of-work backend's early check refuses it too. - if err := ValidateGateFields(request.Issue); err != nil { - return err - } - if err := ValidateCreatePriority(request.Issue.Priority, request.DefaultPriority); err != nil { - return err - } return validatePublicCreateDependencies(request) } -// ValidateCreatePriority refuses a create that asks for the default priority -// (CreateRequest.DefaultPriority, and the batch items' copies of it) while its -// issue also names one: the two would disagree about what was asked for. It is -// exported so a wire client refuses the same request before the dial instead -// of dropping the member it cannot send both of. -func ValidateCreatePriority(issuePriority int, useDefault bool) error { - if useDefault && issuePriority != 0 { - return publicCreateValidationError(fmt.Errorf("create: DefaultPriority is set but the issue names priority %d", issuePriority)) - } - return nil -} - // PreparePublicCreateRequest snapshots, normalizes, and validates a public // create request using the supplied configuration. func PreparePublicCreateRequest(request publicops.CreateRequest, context PublicCreateContext) (publicops.CreateRequest, error) { @@ -82,21 +61,6 @@ func PreparePublicCreateRequest(request publicops.CreateRequest, context PublicC if issue.Status == "" { issue.Status = types.StatusOpen } - // THE ONE PLACE the create priority default is applied: every - // Lifecycle.Create, BatchCreator item and BatchApplier create item on every - // backend is prepared here, so the CLI and the HTTP handlers only say - // "absent" and never spell the number. - if request.DefaultPriority { - issue.Priority = publicops.DefaultCreatePriority - } - // created_by defaults to the request's actor, here and nowhere else: every - // create shape (Lifecycle.Create, BatchCreator items, batch-apply create - // items) prepares through this function, so the CLI, the HTTP handlers and - // a library caller all store the same creator for the same request. An - // explicit CreatedBy is kept. - if issue.CreatedBy == "" { - issue.CreatedBy = request.Actor - } if issue.ID != "" && !request.ForceIDPrefix { // The caller's prefix wins when it supplied one; see // CreateRequest.IDPrefix for why a front door may know better than the @@ -114,9 +78,6 @@ func PreparePublicCreateRequest(request publicops.CreateRequest, context PublicC } prepared := request prepared.Issue = issue - // The default is spent: the prepared issue now carries the priority, and - // the flag would contradict it on the re-validation below. - prepared.DefaultPriority = false if prepared.WaitsFor != nil && prepared.WaitsFor.Gate == "" { prepared.WaitsFor.Gate = string(types.WaitsForAllChildren) } diff --git a/internal/storage/issueops/public_create_test.go b/internal/storage/issueops/public_create_test.go index 146802498..fdb6d6f2e 100644 --- a/internal/storage/issueops/public_create_test.go +++ b/internal/storage/issueops/public_create_test.go @@ -46,34 +46,6 @@ func TestPreparePublicCreateRequestCarriesSourceRepo(t *testing.T) { } } -// TestPreparePublicCreateRequestDefaultsCreatedByToTheActor pins the library -// default every create shape shares (Lifecycle.Create, BatchCreator items, -// batch-apply create items): an empty CreatedBy becomes the request's actor, -// and an explicit CreatedBy is kept. Neither the CLI nor an HTTP handler -// stamps it any more, so without this rule a library or wire create stores "". -func TestPreparePublicCreateRequestDefaultsCreatedByToTheActor(t *testing.T) { - for _, tc := range []struct{ name, createdBy, want string }{ - {"empty defaults to the actor", "", "actor"}, - {"explicit is kept", "someone-else", "someone-else"}, - } { - t.Run(tc.name, func(t *testing.T) { - request := publicops.CreateRequest{Actor: "actor", Issue: &publicops.Issue{ - ID: "bd-created-by", Title: "title", IssueType: types.TypeTask, Priority: 2, CreatedBy: tc.createdBy, - }} - prepared, err := PreparePublicCreateRequest(request, PublicCreateContext{IssuePrefix: "bd"}) - if err != nil { - t.Fatalf("PreparePublicCreateRequest() error = %v", err) - } - if prepared.Issue.CreatedBy != tc.want { - t.Fatalf("prepared CreatedBy = %q, want %q", prepared.Issue.CreatedBy, tc.want) - } - if request.Issue.CreatedBy != tc.createdBy { - t.Fatalf("the caller's request was mutated: CreatedBy = %q, want %q", request.Issue.CreatedBy, tc.createdBy) - } - }) - } -} - func TestPublicCreateIssueFieldClassificationIsComplete(t *testing.T) { accepted := map[string]bool{ "ID": true, "Title": true, "Description": true, "Design": true, "AcceptanceCriteria": true, "Notes": true, "SpecID": true, diff --git a/internal/storage/issueops/public_snapshot.go b/internal/storage/issueops/public_snapshot.go index 05bbdd518..7cf5191f5 100644 --- a/internal/storage/issueops/public_snapshot.go +++ b/internal/storage/issueops/public_snapshot.go @@ -24,7 +24,6 @@ func CloneCreateRequest(request publicops.CreateRequest) publicops.CreateRequest func CloneUpdateRequest(request publicops.UpdateRequest) publicops.UpdateRequest { clone := request clone.Patch.Labels.Add = append([]string(nil), request.Patch.Labels.Add...) - clone.Patch.Waiters.Value = append([]string(nil), request.Patch.Waiters.Value...) clone.Patch.Labels.Remove = append([]string(nil), request.Patch.Labels.Remove...) clone.Patch.Labels.Replace.Value = append([]string(nil), request.Patch.Labels.Replace.Value...) clone.Patch.Metadata.Replace.Value = cloneRawMessage(request.Patch.Metadata.Replace.Value) diff --git a/internal/storage/issueops/public_snapshot_test.go b/internal/storage/issueops/public_snapshot_test.go index f6a9c2fc6..92ce0a24c 100644 --- a/internal/storage/issueops/public_snapshot_test.go +++ b/internal/storage/issueops/public_snapshot_test.go @@ -45,12 +45,7 @@ func TestCloneIssueOperationRequestsDeepCopyMutableFields(t *testing.T) { update.Patch.ExternalRef = publicops.Field[*string]{Set: true, Value: &ref} update.Patch.DueAt = publicops.Field[*time.Time]{Set: true, Value: &now} update.Patch.DeferUntil = publicops.Field[*time.Time]{Set: true, Value: &now} - update.Patch.Waiters = publicops.Field[[]string]{Set: true, Value: []string{"waiter"}} updateClone := CloneUpdateRequest(update) - updateClone.Patch.Waiters.Value[0] = "changed" - if update.Patch.Waiters.Value[0] != "waiter" { - t.Fatal("CloneUpdateRequest() shares the caller's Waiters list") - } updateClone.Patch.Metadata.Set["key"][0] = '{' updateClone.Patch.Metadata.Unset[0] = "changed" updateClone.Patch.Metadata.Replace.Value[0] = '{' @@ -99,7 +94,6 @@ func TestIssueOperationCloneFunctionsKeepFrozenRequestFields(t *testing.T) { assertFieldValueMutable(t, reflect.TypeOf(publicops.Field[*string]{}), "IssuePatch.ExternalRef") assertFieldValueMutable(t, reflect.TypeOf(publicops.Field[*time.Time]{}), "IssuePatch.DueAt") assertFieldValueMutable(t, reflect.TypeOf(publicops.Field[*time.Time]{}), "IssuePatch.DeferUntil") - assertFieldValueMutable(t, reflect.TypeOf(publicops.Field[[]string]{}), "IssuePatch.Waiters") } func isMutableKind(kind reflect.Kind) bool { diff --git a/internal/storage/issueops/ready_work.go b/internal/storage/issueops/ready_work.go index a1863a605..206375202 100644 --- a/internal/storage/issueops/ready_work.go +++ b/internal/storage/issueops/ready_work.go @@ -64,15 +64,7 @@ func buildReadyWorkPredicates(ctx context.Context, tx DBTX, filter types.WorkFil } inputs.ParentDescendantIDs = descendantIDs } - return buildReadyWorkPredicatesFrom(filter, tables, inputs) -} -// buildReadyWorkPredicatesFrom renders the ready-work predicates for one table -// family from ID sets the caller already resolved. It issues no SQL, which is -// what lets a caller that renders BOTH families (and the total) resolve the -// deferred-parent children and parent descendants once per transaction -// instead of once per family. -func buildReadyWorkPredicatesFrom(filter types.WorkFilter, tables FilterTables, inputs sqlbuild.ReadyWorkWhereInputs) (*readyWorkPredicates, error) { whereSQL, whereArgs, err := sqlbuild.BuildReadyWorkWhere(filter, tables, inputs) if err != nil { return nil, err @@ -514,60 +506,6 @@ func queryReadyIssueIDPage(ctx context.Context, tx DBTX, query string, args []in return issueIDs, nil } -var ( - deferredChildDepTables = []string{"dependencies", "wisp_dependencies"} - deferredChildIssueTables = []string{"issues", "wisps"} -) - -// deferredChildrenQuery selects the children, recorded in depTable, of parents -// in issueTable whose defer_until is still in the future. -func deferredChildrenQuery(depTable, issueTable string) string { - targetCol := "depends_on_issue_id" - if issueTable == "wisps" { - targetCol = "depends_on_wisp_id" - } - return fmt.Sprintf(` - SELECT dep.issue_id - FROM %s dep - JOIN %s parent ON parent.id = dep.%s - WHERE dep.type = 'parent-child' - AND parent.defer_until IS NOT NULL - AND parent.defer_until > UTC_TIMESTAMP() - `, depTable, issueTable, targetCol) -} - -// getDeferredChildrenAllTablesInTx is getChildrenOfDeferredParentsInTx's -// child scan for a database known to have the whole wisp plane: the four -// dependency-table x parent-table legs go out as ONE statement (UNION ALL) -// instead of four. Callers must already know every table exists — the -// per-leg missing-table tolerance of the loop form cannot apply to a single -// statement. -func getDeferredChildrenAllTablesInTx(ctx context.Context, tx DBTX) ([]string, error) { - legs := make([]string, 0, len(deferredChildDepTables)*len(deferredChildIssueTables)) - for _, depTable := range deferredChildDepTables { - for _, issueTable := range deferredChildIssueTables { - legs = append(legs, deferredChildrenQuery(depTable, issueTable)) - } - } - rows, err := tx.QueryContext(ctx, strings.Join(legs, " UNION ALL ")) - if err != nil { - return nil, fmt.Errorf("deferred parents: get deferred children: %w", err) - } - defer func() { _ = rows.Close() }() - var childIDs []string - for rows.Next() { - var id string - if err := rows.Scan(&id); err != nil { - return nil, fmt.Errorf("deferred parents: scan deferred child: %w", err) - } - childIDs = append(childIDs, id) - } - if err := rows.Err(); err != nil { - return nil, fmt.Errorf("deferred parents: child rows: %w", err) - } - return childIDs, nil -} - // getChildrenOfDeferredParentsInTx returns IDs of issues whose parent has a // future defer_until. Works within an existing transaction. // @@ -600,9 +538,20 @@ func getChildrenOfDeferredParentsInTx(ctx context.Context, tx DBTX) ([]string, e } var childIDs []string - for _, depTable := range deferredChildDepTables { - for _, issueTable := range deferredChildIssueTables { - rows, err := tx.QueryContext(ctx, deferredChildrenQuery(depTable, issueTable)) + for _, depTable := range []string{"dependencies", "wisp_dependencies"} { + for _, issueTable := range []string{"issues", "wisps"} { + targetCol := "depends_on_issue_id" + if issueTable == "wisps" { + targetCol = "depends_on_wisp_id" + } + rows, err := tx.QueryContext(ctx, fmt.Sprintf(` + SELECT dep.issue_id + FROM %s dep + JOIN %s parent ON parent.id = dep.%s + WHERE dep.type = 'parent-child' + AND parent.defer_until IS NOT NULL + AND parent.defer_until > UTC_TIMESTAMP() + `, depTable, issueTable, targetCol)) if err != nil { // This FROM names two tables at once, so the gate has to key on // which one the driver reports missing. The wisp plane is the diff --git a/internal/storage/issueops/ready_work_counts.go b/internal/storage/issueops/ready_work_counts.go index a54aab812..37715469e 100644 --- a/internal/storage/issueops/ready_work_counts.go +++ b/internal/storage/issueops/ready_work_counts.go @@ -27,94 +27,45 @@ func readyHydrationFor(filter types.WorkFilter) sqlbuild.CountsHydration { return sqlbuild.CountsHydration{Lite: filter.Lite} } -// GetReadyWorkWithCountsInTx returns the ready-work page for filter, hydrated -// with counts. It is GetReadyWorkWithCountsAndTotalInTx without the total. -func GetReadyWorkWithCountsInTx(ctx context.Context, tx DBTX, filter types.WorkFilter) ([]*types.IssueWithCounts, error) { - items, _, err := getReadyWorkWithCountsInTx(ctx, tx, filter, false) - return items, err -} - -// GetReadyWorkWithCountsAndTotalInTx returns the ready-work page for filter -// together with the size of the whole ready set the page was cut from — -// identical to CountReadyWorkInTx(filter), and so to -// len(GetReadyWorkWithCountsInTx(filter with Limit=0)) — resolved inside the -// SAME statements that select the page. -// -// It exists because `bd ready --limit N` used to answer its "Showing N of M" -// with a second, separate pass (CountReadyWorkInTx in a new transaction, -// behind a second defer-wake sweep) that re-ran every probe and predicate the -// page had just run. Against a remote SQL server each statement is a -// sequential round trip, so asking for ONE row took longer than asking for -// all of them. Here the total rides the page's ID query as a window count -// (COUNT(*) OVER ()), evaluated over the full predicate before LIMIT, so a -// capped page costs no extra statement for its total. For an unbounded page -// (Limit <= 0) the total is simply the page's length. -// -// The two families are summed and their overlap (an ID ready as both an issue -// and a wisp, which the merge dedupes wisp-wins) subtracted, exactly as -// CountReadyWorkInTx does; the overlap statement runs only when the probe saw -// an ID present in both tables. -func GetReadyWorkWithCountsAndTotalInTx(ctx context.Context, tx DBTX, filter types.WorkFilter) ([]*types.IssueWithCounts, int, error) { - return getReadyWorkWithCountsInTx(ctx, tx, filter, true) -} - -func getReadyWorkWithCountsInTx(ctx context.Context, tx DBTX, filter types.WorkFilter, wantTotal bool) ([]*types.IssueWithCounts, int, error) { - // A capped page needs the collision fact to size the merged set; an - // unbounded one is its own total. - sized := wantTotal && filter.Limit > 0 - probe, err := probeReadyWorkInTx(ctx, tx, filter, sized) +func GetReadyWorkWithCountsInTx(ctx context.Context, tx *sql.Tx, filter types.WorkFilter) ([]*types.IssueWithCounts, error) { + wispDepsExist, err := optionalTableExistsInTx(ctx, tx, "wisp_dependencies") if err != nil { - return nil, 0, fmt.Errorf("get ready work with counts: %w", err) + return nil, fmt.Errorf("get ready work with counts: wisp dependency probe: %w", err) } - issuePreds, err := buildReadyWorkPredicatesFrom(filter, IssuesFilterTables, probe.inputs) + issuePreds, err := buildReadyWorkPredicates(ctx, tx, filter, IssuesFilterTables) if err != nil { - return nil, 0, err + return nil, err } - out, issueTotal, err := runReadyCountsInTx(ctx, tx, IssuesFilterTables, filter.Limit, issuePreds, probe.wispDepsExist, readyHydrationFor(filter), sized) + out, err := runReadyCountsInTx(ctx, tx, IssuesFilterTables, filter.Limit, issuePreds, wispDepsExist, readyHydrationFor(filter)) if err != nil { - return nil, 0, err + return nil, err } - finish := func(items []*types.IssueWithCounts, total int) ([]*types.IssueWithCounts, int, error) { - if !sized { - // Unbounded: every ready row is on the page (the MaxRows cap - // refuses rather than truncates), so the page IS the set. - total = len(items) - } - items, err := finishReadyWorkWithCounts(items, filter) - if err != nil { - return nil, 0, err - } - return items, total, nil + empty, probeErr := wispsTableEmptyOrMissingInTx(ctx, tx) + if probeErr != nil { + return nil, fmt.Errorf("get ready work with counts: wisp probe: %w", probeErr) + } + if empty { + return finishReadyWorkWithCounts(out, filter) + } + if !wispDepsExist { + return finishReadyWorkWithCounts(out, filter) } - if !probe.readsWisps() { - return finish(out, issueTotal) - } - - wispPreds, err := buildReadyWorkPredicatesFrom(filter, WispsFilterTables, probe.inputs) + wispPreds, err := buildReadyWorkPredicates(ctx, tx, filter, WispsFilterTables) if err != nil { - return nil, 0, err + return nil, err } - wisps, wispTotal, err := runReadyCountsInTx(ctx, tx, WispsFilterTables, filter.Limit, wispPreds, true, readyHydrationFor(filter), sized) + wisps, err := runReadyCountsInTx(ctx, tx, WispsFilterTables, filter.Limit, wispPreds, true, readyHydrationFor(filter)) if err != nil { if missingOptionalWispTable(err) { - return finish(out, issueTotal) + return finishReadyWorkWithCounts(out, filter) } - return nil, 0, err + return nil, err } if len(wisps) == 0 { - return finish(out, issueTotal) - } - - total := issueTotal + wispTotal - if sized && probe.idCollision && issueTotal > 0 { - overlap, err := countReadyOverlapInTx(ctx, tx, issuePreds, wispPreds) - if err != nil { - return nil, 0, fmt.Errorf("get ready work with counts: overlap: %w", err) - } - total -= overlap + return finishReadyWorkWithCounts(out, filter) } // Prefer the canonical wisp record when an ID exists in both tables (be-iabdi). @@ -136,7 +87,7 @@ func getReadyWorkWithCountsInTx(ctx context.Context, tx DBTX, filter types.WorkF } kept = append(kept, wisps...) sortIssuesWithCountsByPolicy(kept, filter.SortPolicy) - return finish(kept, total) + return finishReadyWorkWithCounts(kept, filter) } // finishReadyWorkWithCounts is the terminal hook every @@ -193,14 +144,6 @@ func finishReadyWorkWithCounts(items []*types.IssueWithCounts, filter types.Work // For limit <= 0 (unbounded) there is no page to push down, so it runs the // predicate-form mega-query unchanged. // -// withTotal (bounded pages only) also returns the number of rows the family's -// ready predicate admits, as a window count over the same ID query: -// COUNT(*) OVER () is evaluated over every row the WHERE admits, before ORDER -// BY … LIMIT cut the page, so it is exactly `SELECT COUNT(*) … WHERE` — the -// count CountReadyWorkInTx takes — at no extra statement. The ID query has no -// DISTINCT, so the window counts rows and IDs alike. Otherwise the returned -// total is len(result). -// // Both callers pass readyHydrationFor(filter), which carries Lite and nothing // else: ready work always hydrates labels and cardinalities, because // types.WorkFilter carries neither opt-out — the projection that builds it @@ -208,28 +151,18 @@ func finishReadyWorkWithCounts(items []*types.IssueWithCounts, filter types.Work // SkipLabels and SkipCounts are not carried onto the ReadyFlag arm. // //nolint:gosec // G201: whereSQL/orderBySQL/limitSQL are hardcoded fragments; user input rides ? placeholders. -func runReadyCountsInTx(ctx context.Context, tx DBTX, tables FilterTables, limit int, preds *readyWorkPredicates, includeWispReverseDeps bool, hyd sqlbuild.CountsHydration, withTotal bool) ([]*types.IssueWithCounts, int, error) { +func runReadyCountsInTx(ctx context.Context, tx *sql.Tx, tables FilterTables, limit int, preds *readyWorkPredicates, includeWispReverseDeps bool, hyd sqlbuild.CountsHydration) ([]*types.IssueWithCounts, error) { if limit <= 0 { - out, err := runSearchQueryInTx(ctx, tx, tables, preds.whereSQL, preds.orderBySQL, preds.limitSQL, preds.args, includeWispReverseDeps, hyd) - return out, len(out), err + return runSearchQueryInTx(ctx, tx, tables, preds.whereSQL, preds.orderBySQL, preds.limitSQL, preds.args, includeWispReverseDeps, hyd) } - var pageIDs []string - var total int - var err error - if withTotal { - idQuery := fmt.Sprintf("SELECT id, COUNT(*) OVER () FROM %s %s %s %s", tables.Main, preds.whereSQL, preds.orderBySQL, preds.limitSQL) - pageIDs, total, err = queryReadyIDPageWithTotal(ctx, tx, idQuery, preds.args) - } else { - idQuery := fmt.Sprintf("SELECT id FROM %s %s %s %s", tables.Main, preds.whereSQL, preds.orderBySQL, preds.limitSQL) - pageIDs, err = queryReadyIssueIDPage(ctx, tx, idQuery, preds.args) - total = len(pageIDs) - } + idQuery := fmt.Sprintf("SELECT id FROM %s %s %s %s", tables.Main, preds.whereSQL, preds.orderBySQL, preds.limitSQL) + pageIDs, err := queryReadyIssueIDPage(ctx, tx, idQuery, preds.args) if err != nil { - return nil, 0, err + return nil, err } if len(pageIDs) == 0 { - return nil, 0, nil + return nil, nil } // Hydrate the counts for the resolved page, chunking the IN-list. The page @@ -243,7 +176,7 @@ func runReadyCountsInTx(ctx context.Context, tx DBTX, tables FilterTables, limit countsSQL, idArgs := sqlbuild.SearchCountsSQL(tables, pageIDs[start:end], "", "", "", includeWispReverseDeps, hyd) rows, scanErr := scanCountsRowsInTx(ctx, tx, tables.Main, countsSQL, idArgs, hyd) if scanErr != nil { - return nil, 0, scanErr + return nil, scanErr } for _, r := range rows { if r != nil && r.Issue != nil { @@ -260,94 +193,59 @@ func runReadyCountsInTx(ctx context.Context, tx DBTX, tables FilterTables, limit ordered = append(ordered, r) } } - return ordered, total, nil -} - -// queryReadyIDPageWithTotal runs a `SELECT id, COUNT(*) OVER () …` page query -// and returns the page IDs with the window total. An empty page carries no -// row to read the total from, and means the predicate admitted nothing. -func queryReadyIDPageWithTotal(ctx context.Context, tx DBTX, query string, args []interface{}) ([]string, int, error) { - rows, err := tx.QueryContext(ctx, query, args...) - if err != nil { - return nil, 0, fmt.Errorf("failed to get ready work: %w", err) - } - defer func() { _ = rows.Close() }() - - var ids []string - var total int64 - for rows.Next() { - var id string - if err := rows.Scan(&id, &total); err != nil { - return nil, 0, fmt.Errorf("get ready work: scan id: %w", err) - } - ids = append(ids, id) - } - if err := rows.Err(); err != nil { - return nil, 0, fmt.Errorf("get ready work: rows: %w", err) - } - return ids, int(total), nil + return ordered, nil } // CountReadyWorkInTx returns the number of ready-work items — identical to // len(GetReadyWorkWithCountsInTx(filter with Limit=0)) — without materializing // the counts mega-query. The ready set is a union of the issues and wisps that -// match the ready predicate, so it sizes each family with an indexed COUNT(*) -// over that predicate and subtracts the overlap (IDs present in both ready -// sets, which GetReadyWorkWithCountsInTx dedupes wisp-wins). It never re-runs -// the mega-query, so a single wisp no longer disables the fast path. -// -// Statement budget: the shared probe (one statement; plus one for the -// deferred-parent children when a future-deferred parent exists), then both -// family counts in ONE statement of scalar subqueries, then the overlap only -// when the probe saw an ID present in both tables. `bd ready` itself sizes a -// capped --json page in-band (GetReadyWorkWithCountsAndTotalInTx); this backs -// the ReadyCounter role and the human-readable "Showing X of N". -func CountReadyWorkInTx(ctx context.Context, tx DBTX, filter types.WorkFilter) (int, error) { +// match the ready predicate, so it sizes each family with a single indexed +// COUNT(*) over that predicate and subtracts the overlap (IDs present in both +// ready sets, which GetReadyWorkWithCountsInTx dedupes wisp-wins). It never +// re-runs the mega-query, so a single wisp no longer disables the fast path. +// This backs the "Showing X of N" total `bd ready` prints when the page is +// capped. +func CountReadyWorkInTx(ctx context.Context, tx *sql.Tx, filter types.WorkFilter) (int, error) { countFilter := filter countFilter.Limit = 0 - probe, err := probeReadyWorkInTx(ctx, tx, countFilter, true) + wispDepsExist, err := optionalTableExistsInTx(ctx, tx, "wisp_dependencies") if err != nil { - return 0, fmt.Errorf("count ready work: %w", err) + return 0, fmt.Errorf("count ready work: wisp dependency probe: %w", err) } - issuePreds, err := buildReadyWorkPredicatesFrom(countFilter, IssuesFilterTables, probe.inputs) + issuePreds, err := buildReadyWorkPredicates(ctx, tx, countFilter, IssuesFilterTables) if err != nil { return 0, err } + issueCount, err := countReadyPredicateInTx(ctx, tx, "issues", issuePreds.whereSQL, issuePreds.whereArgs) + if err != nil { + return 0, fmt.Errorf("count ready work: issues: %w", err) + } // Mirror GetReadyWorkWithCountsInTx's wisp gating: an empty/missing wisps // table or absent wisp_dependencies means the ready set is issues-only. - if !probe.readsWisps() { - issueCount, err := countReadyPredicateInTx(ctx, tx, "issues", issuePreds.whereSQL, issuePreds.whereArgs) - if err != nil { - return 0, fmt.Errorf("count ready work: issues: %w", err) - } + empty, err := wispsTableEmptyOrMissingInTx(ctx, tx) + if err != nil { + return 0, fmt.Errorf("count ready work: wisp probe: %w", err) + } + if empty || !wispDepsExist { return issueCount, nil } - wispPreds, err := buildReadyWorkPredicatesFrom(countFilter, WispsFilterTables, probe.inputs) + wispPreds, err := buildReadyWorkPredicates(ctx, tx, countFilter, WispsFilterTables) if err != nil { return 0, err } - issueCount, wispCount, err := countReadyFamiliesInTx(ctx, tx, issuePreds, wispPreds) + wispCount, err := countReadyPredicateInTx(ctx, tx, "wisps", wispPreds.whereSQL, wispPreds.whereArgs) if err != nil { - if !missingOptionalWispTable(err) { - // Both families ride one statement now, so name it: the - // issues-only retry below reports `issues:`, and a reader - // otherwise cannot tell the combined statement from it. - return 0, fmt.Errorf("count ready work: issues+wisps: %w", err) + if missingOptionalWispTable(err) { + return issueCount, nil } - // A wisp plane the database may legitimately lack: issues-only, and - // the issues count has to be taken on its own. - issueCount, err = countReadyPredicateInTx(ctx, tx, "issues", issuePreds.whereSQL, issuePreds.whereArgs) - if err != nil { - return 0, fmt.Errorf("count ready work: issues: %w", err) - } - return issueCount, nil + return 0, fmt.Errorf("count ready work: wisps: %w", err) } - if wispCount == 0 || issueCount == 0 || !probe.idCollision { - return issueCount + wispCount, nil + if wispCount == 0 { + return issueCount, nil } overlap, err := countReadyOverlapInTx(ctx, tx, issuePreds, wispPreds) @@ -357,29 +255,12 @@ func CountReadyWorkInTx(ctx context.Context, tx DBTX, filter types.WorkFilter) ( return issueCount + wispCount - overlap, nil } -// countReadyFamiliesInTx counts both families' ready rows in one statement of -// two scalar subqueries (portable: no FROM-less dialect extension beyond -// SELECT of subqueries, which every supported backend accepts). -// -//nolint:gosec // G201: whereSQL fragments are hardcoded; user input rides ? placeholders. -func countReadyFamiliesInTx(ctx context.Context, tx DBTX, issuePreds, wispPreds *readyWorkPredicates) (int, int, error) { - q := fmt.Sprintf("SELECT (SELECT COUNT(*) FROM issues %s), (SELECT COUNT(*) FROM wisps %s)", issuePreds.whereSQL, wispPreds.whereSQL) - args := make([]interface{}, 0, len(issuePreds.whereArgs)+len(wispPreds.whereArgs)) - args = append(args, issuePreds.whereArgs...) - args = append(args, wispPreds.whereArgs...) - var issues, wisps int - if err := tx.QueryRowContext(ctx, q, args...).Scan(&issues, &wisps); err != nil { - return 0, 0, err - } - return issues, wisps, nil -} - // countReadyPredicateInTx counts the rows in one table family that match the // ready predicate. whereSQL already begins with "WHERE " and whereArgs binds // only its placeholders (no ORDER BY params). // //nolint:gosec // G201: whereSQL is hardcoded fragments; user input rides ? placeholders. -func countReadyPredicateInTx(ctx context.Context, tx DBTX, table, whereSQL string, whereArgs []interface{}) (int, error) { +func countReadyPredicateInTx(ctx context.Context, tx *sql.Tx, table, whereSQL string, whereArgs []interface{}) (int, error) { var n int if err := tx.QueryRowContext(ctx, fmt.Sprintf("SELECT COUNT(*) FROM %s %s", table, whereSQL), whereArgs...).Scan(&n); err != nil { return 0, err @@ -392,7 +273,7 @@ func countReadyPredicateInTx(ctx context.Context, tx DBTX, table, whereSQL strin // the issue row for such an ID, so |ready| = issueCount + wispCount - overlap. // //nolint:gosec // G201: whereSQL fragments are hardcoded; user input rides ? placeholders. -func countReadyOverlapInTx(ctx context.Context, tx DBTX, issuePreds, wispPreds *readyWorkPredicates) (int, error) { +func countReadyOverlapInTx(ctx context.Context, tx *sql.Tx, issuePreds, wispPreds *readyWorkPredicates) (int, error) { q := fmt.Sprintf("SELECT COUNT(*) FROM issues %s AND id IN (SELECT id FROM wisps %s)", issuePreds.whereSQL, wispPreds.whereSQL) args := make([]interface{}, 0, len(issuePreds.whereArgs)+len(wispPreds.whereArgs)) args = append(args, issuePreds.whereArgs...) diff --git a/internal/storage/issueops/ready_work_probe.go b/internal/storage/issueops/ready_work_probe.go deleted file mode 100644 index cd8d48988..000000000 --- a/internal/storage/issueops/ready_work_probe.go +++ /dev/null @@ -1,168 +0,0 @@ -package issueops - -import ( - "context" - "fmt" - "strings" - - "github.com/steveyegge/beads/internal/storage/sqlbuild" - "github.com/steveyegge/beads/internal/types" -) - -// readyWorkProbe holds the per-transaction facts every leg of a ready read -// consults: which optional wisp-plane tables exist and hold rows, and the ID -// sets the ready predicate is rendered from. A ready read renders the -// predicate for two table families (issues, wisps) and — for a capped page — -// sizes the whole set as well; each of those used to re-derive these facts -// with its own statements. Against a remote SQL server every statement is a -// sequential round trip, so the facts are resolved ONCE per transaction here -// and shared by every leg. -type readyWorkProbe struct { - // wispsPresent: the wisps table exists and holds at least one row. False - // means the ready set is issues-only (wispsTableEmptyOrMissingInTx). - wispsPresent bool - // wispDepsExist: the wisp_dependencies table exists - // (optionalTableExistsInTx). The counts mega-query joins it, so the wisp - // family is only read when it does. - wispDepsExist bool - // idCollision: some ID exists in both issues and wisps. Only then can the - // two ready sets overlap, so only then does sizing the merged set need the - // overlap count. Probed only when the caller sizes the set; conservatively - // true whenever it was not probed exactly. - idCollision bool - // inputs are the ID sets the ready WHERE clause is rendered from - // (children of future-deferred parents, descendants of filter.ParentID), - // shared by both families. - inputs sqlbuild.ReadyWorkWhereInputs -} - -const futureDeferredPredicate = "defer_until IS NOT NULL AND defer_until > UTC_TIMESTAMP()" - -// probeReadyWorkInTx resolves a readyWorkProbe for filter. -// -// The existence facts are answered by ONE statement of EXISTS subqueries, -// which is portable across every SQL dialect the shared issueops code runs on -// (no window functions, no FROM-less dialect quirks beyond a SELECT of -// scalar subqueries). EXISTS rather than COUNT so each leg stops at its first -// row. -// -// A pre-migration database may legitimately lack the wisp plane, and a single -// statement cannot tolerate one missing table while reading the rest, so a -// missing-table failure falls back to the per-table probes — the exact -// statements, tolerances and error classification these facts were always -// resolved with. -// -// wantCollision asks for the issues/wisps ID-collision fact, which only a -// caller sizing the merged ready set needs. -func probeReadyWorkInTx(ctx context.Context, tx DBTX, filter types.WorkFilter, wantCollision bool) (*readyWorkProbe, error) { - p := &readyWorkProbe{} - excludeDeferred := !filter.IncludeDeferred - - // Which columns this statement carries depends on the filter, so each fact - // records the index it landed on as it is appended rather than the reader - // below counting appends back. Two of these columns are conditional, so a - // hardcoded index is a fact read from the wrong column the first time a - // new one is inserted above it — and the fact being misread would be the - // ready predicate. - cols := make([]string, 0, 5) // the two unconditional facts plus the three conditional ones - appendFact := func(sql string) int { - cols = append(cols, sql) - return len(cols) - 1 - } - - iWispsPresent := appendFact("EXISTS (SELECT 1 FROM wisps)") - // Only the table REFERENCE matters here: the statement fails with a - // missing-table error when wisp_dependencies is absent, and an empty - // table still exists (optionalTableExistsInTx's contract). The value - // is not read, so this fact keeps no index. - appendFact("EXISTS (SELECT 1 FROM wisp_dependencies)") - - iIssuesDeferred, iWispsDeferred := -1, -1 - if excludeDeferred { - iIssuesDeferred = appendFact("EXISTS (SELECT 1 FROM issues WHERE " + futureDeferredPredicate + ")") - iWispsDeferred = appendFact("EXISTS (SELECT 1 FROM wisps WHERE " + futureDeferredPredicate + ")") - } - iCollision := -1 - if wantCollision { - iCollision = appendFact("EXISTS (SELECT 1 FROM issues JOIN wisps ON wisps.id = issues.id)") - } - flags := make([]bool, len(cols)) - dest := make([]any, len(cols)) - for i := range flags { - dest[i] = &flags[i] - } - - hasDeferredParent := false - err := tx.QueryRowContext(ctx, "SELECT "+strings.Join(cols, ", ")).Scan(dest...) - switch { - case err == nil: - p.wispsPresent = flags[iWispsPresent] - // The statement ran, so every table it names exists. - p.wispDepsExist = true - if excludeDeferred { - hasDeferredParent = flags[iIssuesDeferred] || flags[iWispsDeferred] - } - if iCollision >= 0 { - // Without a wisp row there is nothing to collide with. - p.idCollision = p.wispsPresent && flags[iCollision] - } - if hasDeferredParent { - // Every table the child scan reads is known to exist, so its - // four legs can go out as one statement. - children, dcErr := getDeferredChildrenAllTablesInTx(ctx, tx) - if dcErr != nil { - return nil, fmt.Errorf("compute deferred parent children: %w", dcErr) - } - p.inputs.DeferredChildIDs = children - } - case isTableNotExistError(err): - if err := p.probeTableByTableInTx(ctx, tx, excludeDeferred); err != nil { - return nil, err - } - // Not probed exactly; assume the worst so a total is never - // under-corrected. - p.idCollision = wantCollision && p.wispsPresent - default: - return nil, fmt.Errorf("ready probe: %w", err) - } - - // Parent filtering: all transitive descendants of parentID (GH#3396). - if filter.ParentID != nil { - descendantIDs, descErr := GetDescendantIDsInTx(ctx, tx, *filter.ParentID, 0) - if descErr != nil { - return nil, fmt.Errorf("get parent descendants: %w", descErr) - } - p.inputs.ParentDescendantIDs = descendantIDs - } - return p, nil -} - -// probeTableByTableInTx is the per-table fallback for a database missing part -// of the wisp plane. -func (p *readyWorkProbe) probeTableByTableInTx(ctx context.Context, tx DBTX, excludeDeferred bool) error { - wispDepsExist, err := optionalTableExistsInTx(ctx, tx, "wisp_dependencies") - if err != nil { - return fmt.Errorf("wisp dependency probe: %w", err) - } - p.wispDepsExist = wispDepsExist - empty, err := wispsTableEmptyOrMissingInTx(ctx, tx) - if err != nil { - return fmt.Errorf("wisp probe: %w", err) - } - p.wispsPresent = !empty - if excludeDeferred { - children, err := getChildrenOfDeferredParentsInTx(ctx, tx) - if err != nil { - return fmt.Errorf("compute deferred parent children: %w", err) - } - p.inputs.DeferredChildIDs = children - } - return nil -} - -// readsWisps reports whether the wisp family takes part in the ready set: -// GetReadyWorkWithCountsInTx has always read it only when the wisps table has -// rows and wisp_dependencies exists. -func (p *readyWorkProbe) readsWisps() bool { - return p.wispsPresent && p.wispDepsExist -} diff --git a/internal/storage/issueops/reopen.go b/internal/storage/issueops/reopen.go index a527188a8..03490b789 100644 --- a/internal/storage/issueops/reopen.go +++ b/internal/storage/issueops/reopen.go @@ -117,15 +117,10 @@ func reopenIssueInTx(ctx context.Context, tx DBTX, id, reason, actor string, ret } // Snapshot only after all derived blocked-state maintenance has completed. - // A reopen is a status change, so it journals as an update and mints a - // version; the already-open early returns above write nothing and mint - // nothing. + // A reopen is a status change, so it journals as an update. if err := RecordEventInTx(ctx, tx, EventUpdate, id, actor); err != nil { return nil, err } - if err := RecordVersionInTx(ctx, tx, id, actor); err != nil { - return nil, err - } return &ReopenResult{ IsWisp: isWisp, diff --git a/internal/storage/issueops/reopen_test.go b/internal/storage/issueops/reopen_test.go index 4996f1871..47cd8610e 100644 --- a/internal/storage/issueops/reopen_test.go +++ b/internal/storage/issueops/reopen_test.go @@ -57,10 +57,6 @@ func TestReopenIssueInTxRetriesConditionalUpdateWhenLatestStatusIsCustomDone(t * mock.ExpectQuery(`(?s)SELECT id FROM events`). WillReturnRows(sqlmock.NewRows([]string{"id"})) mock.ExpectExec(`(?s)INSERT INTO events`).WillReturnResult(sqlmock.NewResult(0, 1)) - // The batched recompute's parent-kind guard: no parent-child row in the - // batch, so neither exogeneity read runs before the mark and unmark. - mock.ExpectQuery(`(?s)SELECT COUNT\(d\.depends_on_issue_id\)`). - WillReturnRows(sqlmock.NewRows([]string{"i", "w"}).AddRow(0, 0)) mock.ExpectExec(`(?s)UPDATE issues i SET`).WillReturnResult(sqlmock.NewResult(0, 0)) mock.ExpectExec(`(?s)UPDATE issues i SET`).WillReturnResult(sqlmock.NewResult(0, 0)) mock.ExpectRollback() diff --git a/internal/storage/issueops/row_lock_guard_test.go b/internal/storage/issueops/row_lock_guard_test.go index 9f1083a63..ed329b58f 100644 --- a/internal/storage/issueops/row_lock_guard_test.go +++ b/internal/storage/issueops/row_lock_guard_test.go @@ -68,9 +68,8 @@ var sqlWriteKeywordRe = regexp.MustCompile(`(?i)\b(?:SET|VALUES|SELECT)\b`) // the token would clobber a concurrent whole-row CAS (ExpectedVersion) for // no content change. Analysis absorbed from gastownhall/beads#4682. // (The PR's other exemption, the lease heartbeat, is moot on main: since -// bd-lrgn1 a heartbeat refresh lives entirely in the ephemeral leases -// table and never touches the issues row, and a re-arm remints row_lock on -// purpose; see the freshRowLock invariant.) +// bd-lrgn1 heartbeats live entirely in the ephemeral leases table and never +// touch the issues row.) // - "compaction_level = ": compaction apply/restore rewrites bookkeeping and // compacted body text only — cells the reclaim/close races don't care // about, exempted by name in the freshRowLock invariant. The assignment @@ -140,33 +139,6 @@ var funcNameExemptions = map[string]string{ // Reminting here would be pointless content-wise and would let a stale // ExpectedVersion CAS reject a row it should still recognize. "resolveOneConflictRow": "whole-row `theirs` adoption: the adopted row_lock already vouches for the (identical) adopted content", - - // recordVersionAtInTx (version_history.go) is the shared body behind all - // three public entry points: RecordVersionInTx (update-shaped), - // RecordVersionForCreateInTx (create-shaped, design §16.2b's write fence, - // be-h89oq) and the R7.1 test-support twin RecordVersionAtInTx - // (gastownhall/beads#5898 rev 9, gastownhall/beads#6136). - // The three UPDATE issues statements this guard would otherwise flag live - // here, not in any public wrapper, so the exemption is keyed to this name. - // The two production entry points' callers run it immediately after the - // mutation it is snapshotting — create.go/update.go's own writes, or - // domain/db's Insert/Update — and those callers have already minted a - // fresh row_lock in that same INSERT/UPDATE, in the same transaction. - // RecordVersionAtInTx's fixture-only callers likewise run it only after a - // bare-create has already minted the row's row_lock, never as a row's - // first write. Every UPDATE issues statement this function issues writes - // only bookkeeping columns: the update-shaped and create-shaped UPDATE - // issues SET current_revision = ... statements advance the denormalized - // pointer to the issue_versions row just inserted (the create-shaped one - // also stamps participation_generation in the same statement), and the - // no-history stamp sets participation_generation alone on a create-shaped - // row it does not version. None touches row_lock at all (not even to carry - // the existing value forward) because reminting here would bump the token - // a second time within one transaction, invalidating the RowVersion/CAS - // value the primary mutation's own caller already captured and could - // cause a legitimate subsequent CAS check to fail as a spurious lost - // update. - "recordVersionAtInTx": "runs after the primary mutation (or, for RecordVersionAtInTx's fixture-only callers, a bare-create) already minted a fresh row_lock in the same transaction; reminting here would double-bump the token and invalidate the RowVersion/CAS value already returned to that mutation's caller — covers all three of its UPDATE issues statements (the update-shaped and create-shaped current_revision advances and the no-history participation_generation stamp), since all three public entry points delegate to this one shared body", } // TestAllIssueRowWritesStampRowLock is the load-bearing completeness guard for diff --git a/internal/storage/issueops/scan.go b/internal/storage/issueops/scan.go index c7f81f8cc..fb52fa9da 100644 --- a/internal/storage/issueops/scan.go +++ b/internal/storage/issueops/scan.go @@ -57,28 +57,6 @@ var HeavyDropList = []string{ "payload", } -// IssueSummaryColumns is the column list for types.IssueSummary hydration — -// the narrowest projection, used by list-shaped rendering paths that never -// dereference TEXT/JSON columns. Labels are hydrated separately (they live -// in a join table, not a column); see hydrateSummaryLabels in search.go. -// ScanIssueSummaryFrom below scans it positionally and must stay in agreement; -// TestIssueSummaryColumnsMatchScanner pins that agreement by count. -// -// The trailing four are the wisp-plane markers. searchInTx merges the wisps -// table into every non-SkipWisps result, so they are load-bearing rather than -// optional: without them a wisp row and a durable one are indistinguishable in -// `bd list --json`. All four are narrow scalars (TINYINT(1)/short VARCHAR) -// present on both the issues and wisps tables, so they cost none of the -// TEXT/JSON hydration this projection exists to skip. -// -// Unlike IssueSelectColumnsLite, this list is deliberately NOT a subsequence -// of IssueSelectColumns in order (pinned sits beside the other scalars here, -// not between wisp_type and is_template); the two are scanned by different -// functions, so only each list's own agreement with its scanner matters. -const IssueSummaryColumns = `id, title, status, priority, issue_type, assignee, - pinned, created_at, updated_at, closed_at, - ephemeral, no_history, wisp_type, storage_class` - // IssueScanner is the common interface between *sql.Row and *sql.Rows, // allowing a single scan function to work with both single-row and // multi-row query results. @@ -437,63 +415,6 @@ func ScanIssueLiteFrom(s IssueScanner, extra ...any) (*types.Issue, error) { return &issue, nil } -// ScanIssueSummaryFrom scans a narrow issue summary from any source -// implementing IssueScanner. The caller must ensure the query selected -// exactly IssueSummaryColumns in order. Labels are not populated here — the -// caller must hydrate them separately (see hydrateSummaryLabels in search.go). -func ScanIssueSummaryFrom(s IssueScanner) (*types.IssueSummary, error) { - var summary types.IssueSummary - var assignee sql.NullString - var pinned sql.NullInt64 - var createdAtStr, updatedAtStr sql.NullString - var closedAt sql.NullTime - // Wisp-plane markers. Mapped exactly as ScanIssueFrom maps the same four - // columns, so a row read through either scanner carries identical values: - // the TINYINT(1) pair is truthy-on-nonzero, wisp_type takes any non-NULL - // string, and a NULL storage_class stays empty for EffectiveStorageClass to - // resolve per plane rather than being pinned to a literal here. - var ephemeral, noHistory sql.NullInt64 - var wispType, storageClass sql.NullString - - if err := s.Scan( - &summary.ID, &summary.Title, &summary.Status, &summary.Priority, &summary.IssueType, - &assignee, &pinned, &createdAtStr, &updatedAtStr, &closedAt, - &ephemeral, &noHistory, &wispType, &storageClass, - ); err != nil { - return nil, err - } - - if assignee.Valid { - summary.Assignee = assignee.String - } - if pinned.Valid && pinned.Int64 != 0 { - summary.Pinned = true - } - if createdAtStr.Valid { - summary.CreatedAt = ParseTimeString(createdAtStr.String) - } - if updatedAtStr.Valid { - summary.UpdatedAt = ParseTimeString(updatedAtStr.String) - } - if closedAt.Valid { - summary.ClosedAt = &closedAt.Time - } - if ephemeral.Valid && ephemeral.Int64 != 0 { - summary.Ephemeral = true - } - if noHistory.Valid && noHistory.Int64 != 0 { - summary.NoHistory = true - } - if wispType.Valid { - summary.WispType = types.WispType(wispType.String) - } - if storageClass.Valid { - summary.StorageClass = types.StorageClass(storageClass.String) - } - - return &summary, nil -} - // ParseTimeString parses a time string from database TEXT columns (non-nullable). // Supports RFC3339Nano, RFC3339, and MySQL DATETIME format. func ParseTimeString(s string) time.Time { diff --git a/internal/storage/issueops/scan_test.go b/internal/storage/issueops/scan_test.go index 3f2b179ca..11081087a 100644 --- a/internal/storage/issueops/scan_test.go +++ b/internal/storage/issueops/scan_test.go @@ -230,65 +230,3 @@ func TestScanIssueFrom_PopulatesHeavyFields(t *testing.T) { t.Errorf("ScanIssueFrom: Waiters = %v, want [\"a\"]", issue.Waiters) } } - -// countingScanner records how many destination pointers a scan function asks -// for, without needing a database. Scan itself is a no-op, so the caller's -// post-scan mapping runs against zero-valued dests — harmless, and irrelevant -// to the count this fixture exists to observe. -type countingScanner struct{ dests int } - -func (c *countingScanner) Scan(dest ...any) error { - c.dests = len(dest) - return nil -} - -// TestIssueSummaryColumnsMatchScanner ties IssueSummaryColumns to -// ScanIssueSummaryFrom's dest list by count. The two are hand-synced and read -// positionally, so a column added to one and not the other does not fail -// loudly: the SELECT and the Scan disagree on width, and database/sql reports -// it as a generic scan error from whichever query happens to run first, far -// from the edit that caused it. -// -// This is the summary-projection counterpart of -// TestIssueSelectColumns_LitePlusHeavyEqualsFull, which does the same job for -// the lite projection. -func TestIssueSummaryColumnsMatchScanner(t *testing.T) { - t.Parallel() - - cols := parseSelectColumns(IssueSummaryColumns) - - var scanner countingScanner - if _, err := ScanIssueSummaryFrom(&scanner); err != nil { - t.Fatalf("ScanIssueSummaryFrom with counting scanner: %v", err) - } - - if scanner.dests != len(cols) { - t.Errorf("IssueSummaryColumns has %d columns but ScanIssueSummaryFrom scans %d dests.\n"+ - " columns: %v\n"+ - "Action: add the column to both, in the same position — they are read positionally.", - len(cols), scanner.dests, cols) - } -} - -// TestIssueSummaryColumnsAreRealIssueColumns pins every column in -// IssueSummaryColumns to one that actually exists on the issues and wisps -// tables, by requiring it to appear in IssueSelectColumns — the canonical full -// list both tables are already queried with. -// -// Count parity alone cannot catch a typo: misspelling a column while adding -// its dest keeps the widths equal and fails only at query time, against a real -// database, as an opaque "unknown column" from whichever caller runs first. -// Note this is a membership check, not an order check — unlike -// IssueSelectColumnsLite, IssueSummaryColumns is deliberately not a -// subsequence of the full list (see the const's doc comment). -func TestIssueSummaryColumnsAreRealIssueColumns(t *testing.T) { - t.Parallel() - - full := columnSet(parseSelectColumns(IssueSelectColumns)) - for _, col := range parseSelectColumns(IssueSummaryColumns) { - if _, ok := full[col]; !ok { - t.Errorf("IssueSummaryColumns contains %q, which is not in IssueSelectColumns; "+ - "either it is misspelled or it is not a column on the issues/wisps tables", col) - } - } -} diff --git a/internal/storage/issueops/search.go b/internal/storage/issueops/search.go index 4a0b10438..df0ff5b95 100644 --- a/internal/storage/issueops/search.go +++ b/internal/storage/issueops/search.go @@ -32,15 +32,6 @@ func SearchIssueIDsInTx(ctx context.Context, tx DBTX, query string, filter types return searchInTx(ctx, tx, query, filter, idProjection) } -// SearchIssueSummariesInTx is the narrow-projection variant of -// SearchIssuesInTx for list-shaped rendering paths: applies the same WHERE -// clauses, wisp-merge semantics, and sort order as SearchIssuesInTx, but -// projects only the columns in IssueSummaryColumns (plus a separate label -// hydration pass) and returns []*types.IssueSummary instead of full issues. -func SearchIssueSummariesInTx(ctx context.Context, tx DBTX, query string, filter types.IssueFilter) ([]*types.IssueSummary, error) { - return searchInTx(ctx, tx, query, filter, summaryProjection) -} - // SearchWispsPlaneInTx searches the wisps plane ALONE: every row stored in // the wisps table, whatever its ephemeral, no_history or wisp_type values, and // never the issues table. @@ -158,21 +149,6 @@ var idProjection = searchProjection[string]{ }, } -// summaryProjection is the narrow list-rendering variant: reads -// IssueSummaryColumns instead of the wide or lite column sets, scans with -// ScanIssueSummaryFrom, and hydrates only labels (hydrateSummaryLabels) since -// types.IssueSummary carries no Dependencies field to hydrate. idShrink stays -// false (the zero value) — IssueSummaryColumns is already narrower than -// IssueSelectColumnsLite, so Pattern B's extra id-scan round-trip isn't a -// clear win here the way it is for the wide issueProjection. -var summaryProjection = searchProjection[*types.IssueSummary]{ - columns: func(_ FilterTables) string { return IssueSummaryColumns }, - scan: func(rows *sql.Rows) (*types.IssueSummary, error) { return ScanIssueSummaryFrom(rows) }, - id: func(s *types.IssueSummary) string { return s.ID }, - hydrate: hydrateSummaryLabels, - less: sqlbuild.LessSummary, -} - // hydrateIssueLabelsAndDeps bulk-loads labels (and optionally dependencies) // for the given issues. searchTableInTxT runs against exactly one of the // issues/wisps tables, so every ID here belongs to tables.Labels — we use @@ -219,29 +195,6 @@ func missingOptionalWispTable(err error) bool { return ok && sqlbuild.OptionalWispTable(name) } -// hydrateSummaryLabels bulk-loads labels for the given summaries. Mirrors the -// label half of hydrateIssueLabelsAndDeps; types.IssueSummary has no -// Dependencies field, so there is no dependency-hydration half to mirror. -func hydrateSummaryLabels(ctx context.Context, tx DBTX, tables FilterTables, summaries []*types.IssueSummary, filter types.IssueFilter) error { - if filter.SkipLabels { - return nil - } - ids := make([]string, len(summaries)) - for i, s := range summaries { - ids[i] = s.ID - } - labelMap, err := GetLabelsForIssuesFromTableInTx(ctx, tx, tables.Labels, ids) - if err != nil { - return fmt.Errorf("hydrate summary labels: %w", err) - } - for _, s := range summaries { - if labels, ok := labelMap[s.ID]; ok { - s.Labels = labels - } - } - return nil -} - // searchInTx is the shared wisp-merge wrapper. Ephemeral routing, the // empty-wisps probe, the issues+wisps queries, and overlap detection live // here once. Both SearchIssuesInTx and SearchIssueIDsInTx use this body — diff --git a/internal/storage/issueops/search_counts.go b/internal/storage/issueops/search_counts.go index 0cd72d4fe..45d51a7f0 100644 --- a/internal/storage/issueops/search_counts.go +++ b/internal/storage/issueops/search_counts.go @@ -142,7 +142,7 @@ func runFilterSearchQueryInTx(ctx context.Context, tx *sql.Tx, query string, fil } //nolint:gosec // G201: SQL fragments are caller-built from hardcoded shapes -func runSearchQueryInTx(ctx context.Context, tx DBTX, tables FilterTables, whereSQL, orderBySQL, limitSQL string, args []interface{}, includeWispReverseDeps bool, hyd sqlbuild.CountsHydration) ([]*types.IssueWithCounts, error) { +func runSearchQueryInTx(ctx context.Context, tx *sql.Tx, tables FilterTables, whereSQL, orderBySQL, limitSQL string, args []interface{}, includeWispReverseDeps bool, hyd sqlbuild.CountsHydration) ([]*types.IssueWithCounts, error) { searchSQL, _ := sqlbuild.SearchCountsSQL(tables, nil, whereSQL, orderBySQL, limitSQL, includeWispReverseDeps, hyd) return scanCountsRowsInTx(ctx, tx, tables.Main, searchSQL, args, hyd) } @@ -153,7 +153,7 @@ func runSearchQueryInTx(ctx context.Context, tx DBTX, tables FilterTables, where // ready-counts path. // //nolint:gosec // G201: query is builder-produced; user input rides ? placeholders. -func scanCountsRowsInTx(ctx context.Context, tx DBTX, mainTable, query string, args []interface{}, hyd sqlbuild.CountsHydration) ([]*types.IssueWithCounts, error) { +func scanCountsRowsInTx(ctx context.Context, tx *sql.Tx, mainTable, query string, args []interface{}, hyd sqlbuild.CountsHydration) ([]*types.IssueWithCounts, error) { rows, err := tx.QueryContext(ctx, query, args...) if err != nil { return nil, fmt.Errorf("search count %s: %w", mainTable, err) diff --git a/internal/storage/issueops/search_summary_merge_test.go b/internal/storage/issueops/search_summary_merge_test.go deleted file mode 100644 index 5f23c686c..000000000 --- a/internal/storage/issueops/search_summary_merge_test.go +++ /dev/null @@ -1,111 +0,0 @@ -package issueops - -import ( - "context" - "database/sql/driver" - "testing" - "time" - - "github.com/DATA-DOG/go-sqlmock" - "github.com/steveyegge/beads/internal/types" -) - -// summaryColumnNames is derived from IssueSummaryColumns rather than -// hand-listed beside it. The hand-copied version was one of three lists that -// had to be edited in lockstep (the const, ScanIssueSummaryFrom's dests, and -// this fixture); deriving it deletes one of the three outright, and -// TestIssueSummaryColumnsMatchScanner in scan_test.go pins the remaining pair. -func summaryColumnNames() []string { - return parseSelectColumns(IssueSummaryColumns) -} - -// summaryMergeRow builds one full-width IssueSummaryColumns row with only -// identity/status/priority/created_at populated; every other column, including -// the wisp-plane markers, is NULL — this fixture exercises merge ordering, not -// hydration. -// -// Values are placed by column NAME against summaryColumnNames rather than by -// literal position, so adding a column to IssueSummaryColumns widens this row -// automatically instead of silently shifting every value one slot left. -func summaryMergeRow(id string, createdAt time.Time) []driver.Value { - byName := map[string]driver.Value{ - "id": id, - "title": id, - "status": "open", - "priority": 2, - "issue_type": "task", - "created_at": createdAt.Format(time.RFC3339), - } - cols := summaryColumnNames() - row := make([]driver.Value, len(cols)) - for i, col := range cols { - row[i] = byName[col] // absent from the map => nil => SQL NULL - } - return row -} - -// TestSearchIssueSummariesInTx_MergesAcrossIssuesAndWisps is -// TestSearchIssuesInTx_Lite_MergesAcrossIssuesAndWisps's sibling for -// summaryProjection (SearchIssueSummariesInTx): coverage for be-be3pj -// acceptance criterion 3 — wisp-merge behavior under a limit (top-N -// truncation after merge) was previously exercised only for issueProjection -// and issueLiteProjection, never for summaryProjection. summaryProjection -// already carries less: sqlbuild.LessSummary (unlike issueLiteProjection -// before be-yrtwi's fix), so this test is prophylactic: it locks in the -// currently-correct merge+trim behavior rather than reproducing a live bug. -// -// Fixture: the issues table's 2 rows are both older than either wisps-table -// row. A Limit=2 search over IssueFilter{SortBy: "created"} must return the -// true top-2 by created_at DESC — both wisp rows — not the issues-leg rows -// that would survive a naive concatenate-then-trim (always the issues leg, -// since results := append(filtered, wispResults...)). SkipLabels avoids -// mocking the per-leg label hydration queries. -func TestSearchIssueSummariesInTx_MergesAcrossIssuesAndWisps(t *testing.T) { - t.Parallel() - - day := func(d int) time.Time { return time.Date(2026, 6, d, 0, 0, 0, 0, time.UTC) } - - _, mock, tx := beginMockTx(t) - - // Issues leg: SQL already orders these DESC by created_at (day5, day1) — - // both older than anything in the wisps leg below. - mock.ExpectQuery(`(?s)FROM issues.*LIMIT 2`). - WillReturnRows(sqlmock.NewRows(summaryColumnNames()). - AddRow(summaryMergeRow("bd-issue-mid", day(5))...). - AddRow(summaryMergeRow("bd-issue-old", day(1))...)) - - mock.ExpectQuery(`SELECT 1 FROM wisps LIMIT 1`). - WillReturnRows(sqlmock.NewRows([]string{"1"}).AddRow(1)) - - // Wisps leg: SQL already orders these DESC by created_at (day12, day10) — - // both newer than either issues-leg row. True global top-2 by created - // DESC is entirely this leg. - mock.ExpectQuery(`(?s)FROM wisps.*LIMIT 2`). - WillReturnRows(sqlmock.NewRows(summaryColumnNames()). - AddRow(summaryMergeRow("bd-wisp-newest", day(12))...). - AddRow(summaryMergeRow("bd-wisp-new", day(10))...)) - - filter := types.IssueFilter{ - SortBy: "created", - Limit: 2, - SkipLabels: true, - } - - got, err := SearchIssueSummariesInTx(context.Background(), tx, "", filter) - if err != nil { - t.Fatalf("SearchIssueSummariesInTx: %v", err) - } - - if len(got) != 2 || got[0].ID != "bd-wisp-newest" || got[1].ID != "bd-wisp-new" { - ids := make([]string, len(got)) - for i, g := range got { - ids[i] = g.ID - } - t.Fatalf("SearchIssueSummariesInTx merge: got %v, want [bd-wisp-newest bd-wisp-new] "+ - "(true created-DESC top-2 across issues+wisps, not issues-leg concatenation order)", ids) - } - - if err := mock.ExpectationsWereMet(); err != nil { - t.Fatalf("unmet SQL expectations: %v", err) - } -} diff --git a/internal/storage/issueops/statistics.go b/internal/storage/issueops/statistics.go index 907fb6edb..280851d9b 100644 --- a/internal/storage/issueops/statistics.go +++ b/internal/storage/issueops/statistics.go @@ -8,10 +8,9 @@ import ( ) // ScanIssueCountsInTx populates the count fields (TotalIssues, OpenIssues, -// InProgressIssues, ClosedIssues, DeferredIssues, PinnedIssues, GateIssues, -// TemplateIssues) of stats from the issues table. It does NOT compute -// BlockedIssues or ReadyIssues — callers fill those in using their own -// blocked-ID computation strategy. +// InProgressIssues, ClosedIssues, DeferredIssues, PinnedIssues) of stats from +// the issues table. It does NOT compute BlockedIssues or ReadyIssues — callers +// fill those in using their own blocked-ID computation strategy. func ScanIssueCountsInTx(ctx context.Context, tx DBTX, stats *types.Statistics) error { if err := tx.QueryRowContext(ctx, ` SELECT @@ -20,9 +19,7 @@ func ScanIssueCountsInTx(ctx context.Context, tx DBTX, stats *types.Statistics) COALESCE(SUM(CASE WHEN status = 'in_progress' THEN 1 ELSE 0 END), 0), COALESCE(SUM(CASE WHEN status = 'closed' THEN 1 ELSE 0 END), 0), COALESCE(SUM(CASE WHEN status = 'deferred' THEN 1 ELSE 0 END), 0), - COALESCE(SUM(CASE WHEN pinned = 1 THEN 1 ELSE 0 END), 0), - COALESCE(SUM(CASE WHEN issue_type = 'gate' THEN 1 ELSE 0 END), 0), - COALESCE(SUM(CASE WHEN is_template = 1 THEN 1 ELSE 0 END), 0) + COALESCE(SUM(CASE WHEN pinned = 1 THEN 1 ELSE 0 END), 0) FROM issues `).Scan( &stats.TotalIssues, @@ -31,8 +28,6 @@ func ScanIssueCountsInTx(ctx context.Context, tx DBTX, stats *types.Statistics) &stats.ClosedIssues, &stats.DeferredIssues, &stats.PinnedIssues, - &stats.GateIssues, - &stats.TemplateIssues, ); err != nil { return fmt.Errorf("scan issue counts: %w", err) } diff --git a/internal/storage/issueops/testdata/jcs/README.md b/internal/storage/issueops/testdata/jcs/README.md deleted file mode 100644 index 42b580f1f..000000000 --- a/internal/storage/issueops/testdata/jcs/README.md +++ /dev/null @@ -1,18 +0,0 @@ -# RFC 8785 (JCS) test vectors - -The `input/` and `expected/` pairs are copied verbatim from -[github.com/gowebpki/jcs](https://github.com/gowebpki/jcs) v1.0.1 -(`testdata/input` and `testdata/output` upstream — the latter is named -`expected/` here because this repository's `.gitignore` drops any `output` -tree; Apache License 2.0, Copyright 2021 -Bret Jordan & Benedikt Thoma, Copyright 2006-2019 WebPKI.org), which carries -the RFC 8785 JSON Canonicalization Scheme test suite from -[cyberphone/json-canonicalization](https://github.com/cyberphone/json-canonicalization). -Each `input/.json` must canonicalize to exactly the bytes of -`expected/.json` (no trailing newline). - -`TestCanonicalDurableStateIsJCS` drives `canonicalDurableState` — the helper -`RecordVersionInTx` stores `issue_versions.durable_state` through — over every -pair, so the stored bytes are pinned to the published vectors rather than to -whatever the dependency happens to emit. Do not edit the pairs; local cases -belong in the test's own table. diff --git a/internal/storage/issueops/testdata/jcs/expected/arrays.json b/internal/storage/issueops/testdata/jcs/expected/arrays.json deleted file mode 100644 index 5efb93db7..000000000 --- a/internal/storage/issueops/testdata/jcs/expected/arrays.json +++ /dev/null @@ -1 +0,0 @@ -[56,{"1":[],"10":null,"d":true}] \ No newline at end of file diff --git a/internal/storage/issueops/testdata/jcs/expected/false.json b/internal/storage/issueops/testdata/jcs/expected/false.json deleted file mode 100644 index 02e4a84d6..000000000 --- a/internal/storage/issueops/testdata/jcs/expected/false.json +++ /dev/null @@ -1 +0,0 @@ -false \ No newline at end of file diff --git a/internal/storage/issueops/testdata/jcs/expected/french.json b/internal/storage/issueops/testdata/jcs/expected/french.json deleted file mode 100644 index 2e15cd1b2..000000000 --- a/internal/storage/issueops/testdata/jcs/expected/french.json +++ /dev/null @@ -1 +0,0 @@ -{"peach":"This sorting order","péché":"is wrong according to French","pêche":"but canonicalization MUST","sin":"ignore locale"} \ No newline at end of file diff --git a/internal/storage/issueops/testdata/jcs/expected/null.json b/internal/storage/issueops/testdata/jcs/expected/null.json deleted file mode 100644 index ec747fa47..000000000 --- a/internal/storage/issueops/testdata/jcs/expected/null.json +++ /dev/null @@ -1 +0,0 @@ -null \ No newline at end of file diff --git a/internal/storage/issueops/testdata/jcs/expected/simpleString.json b/internal/storage/issueops/testdata/jcs/expected/simpleString.json deleted file mode 100644 index 2261bc20e..000000000 --- a/internal/storage/issueops/testdata/jcs/expected/simpleString.json +++ /dev/null @@ -1 +0,0 @@ -"this is a test string 1235" \ No newline at end of file diff --git a/internal/storage/issueops/testdata/jcs/expected/structures.json b/internal/storage/issueops/testdata/jcs/expected/structures.json deleted file mode 100644 index dc21e2424..000000000 --- a/internal/storage/issueops/testdata/jcs/expected/structures.json +++ /dev/null @@ -1 +0,0 @@ -{"":"empty","1":{"\n":56,"f":{"F":5,"f":"hi"}},"10":{},"111":[{"E":"no","e":"yes"}],"A":{},"a":{}} \ No newline at end of file diff --git a/internal/storage/issueops/testdata/jcs/expected/true.json b/internal/storage/issueops/testdata/jcs/expected/true.json deleted file mode 100644 index f32a5804e..000000000 --- a/internal/storage/issueops/testdata/jcs/expected/true.json +++ /dev/null @@ -1 +0,0 @@ -true \ No newline at end of file diff --git a/internal/storage/issueops/testdata/jcs/expected/unicode.json b/internal/storage/issueops/testdata/jcs/expected/unicode.json deleted file mode 100644 index ee60fd121..000000000 --- a/internal/storage/issueops/testdata/jcs/expected/unicode.json +++ /dev/null @@ -1 +0,0 @@ -{"Unnormalized Unicode":"Å"} \ No newline at end of file diff --git a/internal/storage/issueops/testdata/jcs/expected/values.json b/internal/storage/issueops/testdata/jcs/expected/values.json deleted file mode 100644 index 29b720b6e..000000000 --- a/internal/storage/issueops/testdata/jcs/expected/values.json +++ /dev/null @@ -1 +0,0 @@ -{"literals":[null,true,false],"numbers":[333333333.3333333,1e+30,4.5,0.002,1e-27],"string":"€$\u000f\nA'B\"\\\\\"/"} \ No newline at end of file diff --git a/internal/storage/issueops/testdata/jcs/expected/weird.json b/internal/storage/issueops/testdata/jcs/expected/weird.json deleted file mode 100644 index 62c83a333..000000000 --- a/internal/storage/issueops/testdata/jcs/expected/weird.json +++ /dev/null @@ -1 +0,0 @@ -{"\n":"Newline","\r":"Carriage Return","1":"One","":"Browser Challenge","€":"Control","ö":"Latin Small Letter O With Diaeresis","€":"Euro Sign","😂":"Smiley","דּ":"Hebrew Letter Dalet With Dagesh"} \ No newline at end of file diff --git a/internal/storage/issueops/testdata/jcs/input/arrays.json b/internal/storage/issueops/testdata/jcs/input/arrays.json deleted file mode 100644 index 20e62263c..000000000 --- a/internal/storage/issueops/testdata/jcs/input/arrays.json +++ /dev/null @@ -1,8 +0,0 @@ -[ - 56, - { - "d": true, - "10": null, - "1": [ ] - } -] diff --git a/internal/storage/issueops/testdata/jcs/input/false.json b/internal/storage/issueops/testdata/jcs/input/false.json deleted file mode 100644 index 02e4a84d6..000000000 --- a/internal/storage/issueops/testdata/jcs/input/false.json +++ /dev/null @@ -1 +0,0 @@ -false \ No newline at end of file diff --git a/internal/storage/issueops/testdata/jcs/input/french.json b/internal/storage/issueops/testdata/jcs/input/french.json deleted file mode 100644 index 4ff6d3de2..000000000 --- a/internal/storage/issueops/testdata/jcs/input/french.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "peach": "This sorting order", - "péché": "is wrong according to French", - "pêche": "but canonicalization MUST", - "sin": "ignore locale" -} diff --git a/internal/storage/issueops/testdata/jcs/input/null.json b/internal/storage/issueops/testdata/jcs/input/null.json deleted file mode 100644 index ec747fa47..000000000 --- a/internal/storage/issueops/testdata/jcs/input/null.json +++ /dev/null @@ -1 +0,0 @@ -null \ No newline at end of file diff --git a/internal/storage/issueops/testdata/jcs/input/simpleString.json b/internal/storage/issueops/testdata/jcs/input/simpleString.json deleted file mode 100644 index e6463d629..000000000 --- a/internal/storage/issueops/testdata/jcs/input/simpleString.json +++ /dev/null @@ -1 +0,0 @@ - "this is a test string 1235" \ No newline at end of file diff --git a/internal/storage/issueops/testdata/jcs/input/structures.json b/internal/storage/issueops/testdata/jcs/input/structures.json deleted file mode 100644 index eb71efb84..000000000 --- a/internal/storage/issueops/testdata/jcs/input/structures.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "1": {"f": {"f": "hi","F": 5} ,"\n": 56.0}, - "10": { }, - "": "empty", - "a": { }, - "111": [ {"e": "yes","E": "no" } ], - "A": { } -} \ No newline at end of file diff --git a/internal/storage/issueops/testdata/jcs/input/true.json b/internal/storage/issueops/testdata/jcs/input/true.json deleted file mode 100644 index f32a5804e..000000000 --- a/internal/storage/issueops/testdata/jcs/input/true.json +++ /dev/null @@ -1 +0,0 @@ -true \ No newline at end of file diff --git a/internal/storage/issueops/testdata/jcs/input/unicode.json b/internal/storage/issueops/testdata/jcs/input/unicode.json deleted file mode 100644 index 4b5bc7699..000000000 --- a/internal/storage/issueops/testdata/jcs/input/unicode.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "Unnormalized Unicode":"A\u030a" -} diff --git a/internal/storage/issueops/testdata/jcs/input/values.json b/internal/storage/issueops/testdata/jcs/input/values.json deleted file mode 100644 index f7712c2fb..000000000 --- a/internal/storage/issueops/testdata/jcs/input/values.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "numbers": [333333333.33333329, 1E30, 4.50, 2e-3, 0.000000000000000000000000001], - "string": "\u20ac$\u000F\u000aA'\u0042\u0022\u005c\\\"\/", - "literals": [null, true, false] -} \ No newline at end of file diff --git a/internal/storage/issueops/testdata/jcs/input/weird.json b/internal/storage/issueops/testdata/jcs/input/weird.json deleted file mode 100644 index 53fabe67b..000000000 --- a/internal/storage/issueops/testdata/jcs/input/weird.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "\u20ac": "Euro Sign", - "\r": "Carriage Return", - "\u000a": "Newline", - "1": "One", - "\u0080": "Control\u007f", - "\ud83d\ude02": "Smiley", - "\u00f6": "Latin Small Letter O With Diaeresis", - "\ufb33": "Hebrew Letter Dalet With Dagesh", - "": "Browser Challenge" -} diff --git a/internal/storage/issueops/unclaim.go b/internal/storage/issueops/unclaim.go index 97b126af7..a25bbbf55 100644 --- a/internal/storage/issueops/unclaim.go +++ b/internal/storage/issueops/unclaim.go @@ -129,12 +129,8 @@ func finishUnclaimInTx(ctx context.Context, tx DBTX, eventTable string, id strin } // A release changes assignee and status, so it journals as an update. Both // unclaim entry points funnel through here after their CAS succeeded, so - // this covers the conditional release too — and the version row minted - // beside it, for the same durable-state change. - if err := RecordEventInTx(ctx, tx, EventUpdate, id, actor); err != nil { - return err - } - return RecordVersionInTx(ctx, tx, id, actor) + // this covers the conditional release too. + return RecordEventInTx(ctx, tx, EventUpdate, id, actor) } // UnclaimIssueIfAssigneeInTx atomically releases a claim only while the issue is diff --git a/internal/storage/issueops/update.go b/internal/storage/issueops/update.go index ee77833ba..c0958ed6a 100644 --- a/internal/storage/issueops/update.go +++ b/internal/storage/issueops/update.go @@ -335,24 +335,17 @@ type UpdateResult struct { // //nolint:gosec // G201: table names come from WispTableRouting (hardcoded constants) func UpdateIssueInTx(ctx context.Context, tx DBTX, id string, updates map[string]interface{}, actor string) (*UpdateResult, error) { - return updateIssueInTx(ctx, tx, id, updates, actor, true, true) + return updateIssueInTx(ctx, tx, id, updates, actor, true) } // UpdateIssueWithoutEventInTx applies normal update semantics without recording // an intermediate event. Demotion uses this to preserve the historical event // stream: create/update history is copied, then a single demotion event is added. func UpdateIssueWithoutEventInTx(ctx context.Context, tx DBTX, id string, updates map[string]interface{}, actor string) (*UpdateResult, error) { - return updateIssueInTx(ctx, tx, id, updates, actor, false, true) + return updateIssueInTx(ctx, tx, id, updates, actor, false) } -// updateIssueInTx is the body behind both exported update entry points. -// recordEvent gates the human-facing audit event only. mintVersion controls -// whether an accepted update mints its own version row: both exported entry -// points always do, while ExecuteUpdate — which applies label, parent and -// persistence patches AFTER the row write in the same call — passes false and -// mints once after the last patch, so the version's durable_state carries the -// patched labels and edges rather than the pre-patch row. -func updateIssueInTx(ctx context.Context, tx DBTX, id string, updates map[string]interface{}, actor string, recordEvent bool, mintVersion bool) (*UpdateResult, error) { +func updateIssueInTx(ctx context.Context, tx DBTX, id string, updates map[string]interface{}, actor string, recordEvent bool) (*UpdateResult, error) { updates = cloneUpdateFields(updates) // Pop the override before anything reads the map as a set of columns. It // has to come out ahead of the no-op filter too: the filter keeps every key @@ -406,17 +399,10 @@ func updateIssueInTx(ctx context.Context, tx DBTX, id string, updates map[string // A status update that crosses into the done category is a close by another // name, so it answers to close policy. Running after the no-op filter keeps // a done-to-done restatement policy-free, and running after the callers' - // version and assignee preconditions keeps force away from those: here it - // bypasses the two close-policy refusals and nothing else. A refusal - // returns here, before any write, and aborts the caller's transaction. - // - // Close policy is ALL it answers to. The pin guard and the assignee - // authority fence, which CloseIssueCheckedInTx applies to the close verbs, - // are deliberately not run here: a crossing still closes a pinned issue or - // a bead another actor holds on every update leg (`bd update -s closed`, - // PATCH, batchApply), as it always has. Refusing those would change what - // the update legs admit, which is its own decision and not part of putting - // the close verbs' guards in the role. + // version and assignee preconditions keeps force away from those: it + // bypasses the two close refusals and nothing else, exactly as it does for + // `bd close`. A refusal returns here, before any write, and aborts the + // caller's transaction. crossing, err := CrossesIntoDoneCategoryInTx(ctx, tx, oldIssue.Status, updates) if err != nil { return nil, err @@ -561,11 +547,6 @@ func updateIssueInTx(ctx context.Context, tx DBTX, id string, updates map[string if err := RecordEventInTx(ctx, tx, EventUpdate, id, actor); err != nil { return nil, err } - if mintVersion { - if err := RecordVersionInTx(ctx, tx, id, actor); err != nil { - return nil, err - } - } return updateResult, nil } diff --git a/internal/storage/issueops/update_validation_test.go b/internal/storage/issueops/update_validation_test.go index 6d62ba038..d1834583c 100644 --- a/internal/storage/issueops/update_validation_test.go +++ b/internal/storage/issueops/update_validation_test.go @@ -31,45 +31,6 @@ func TestValidateUpdateRequestRejectsInvalidCanonicalFields(t *testing.T) { } } -// TestValidateUpdateRequestRejectsInvalidNotesRequestShapes covers the two -// notes rules ValidateUpdateRequest owns that are properties of the REQUEST -// rather than of the patch alone, so they cannot be expressed in the -// patch-only table above. Both are contract-level: the batch apply leg reaches -// them here and nowhere else, and the single-issue HTTP handler's own -// pre-check returns before storage is reached, so without these cases the -// branches backing the published ApplyUpdateItem promise are unexercised. -func TestValidateUpdateRequestRejectsInvalidNotesRequestShapes(t *testing.T) { - for _, tc := range []struct { - name string - request publicops.UpdateRequest - }{ - { - name: "forced notes overwrite without a notes value", - request: publicops.UpdateRequest{ - Actor: "actor", IssueID: "bd-validate", - ForceNotesOverwrite: true, - }, - }, - { - name: "notes replacement combined with append", - request: publicops.UpdateRequest{ - Actor: "actor", IssueID: "bd-validate", - Patch: publicops.IssuePatch{ - Notes: publicops.Field[string]{Set: true, Value: "replacement"}, - AppendNotes: publicops.Field[string]{Set: true, Value: "addition"}, - }, - }, - }, - } { - t.Run(tc.name, func(t *testing.T) { - err := ValidateUpdateRequest(tc.request) - if !errors.Is(err, storage.ErrValidation) { - t.Fatalf("ValidateUpdateRequest() error = %v, want ErrValidation", err) - } - }) - } -} - func TestApplyMetadataPatchRejectsUnsafeKeysAndNullMerge(t *testing.T) { for _, tc := range []struct { name string diff --git a/internal/storage/issueops/version.go b/internal/storage/issueops/version.go index df95548a5..35ea83e84 100644 --- a/internal/storage/issueops/version.go +++ b/internal/storage/issueops/version.go @@ -42,7 +42,7 @@ func CheckVersionInTx(ctx context.Context, tx DBTX, id string, expected int64) e return fmt.Errorf("failed to read row version for %s: %w", id, err) } if current.Int64 != expected { - return &storage.VersionMismatchError{Expected: expected, Current: current.Int64} + return fmt.Errorf("%w: expected %d, got %d", storage.ErrVersionMismatch, expected, current.Int64) } return nil } diff --git a/internal/storage/issueops/version_completeness_test.go b/internal/storage/issueops/version_completeness_test.go deleted file mode 100644 index e9b484eb4..000000000 --- a/internal/storage/issueops/version_completeness_test.go +++ /dev/null @@ -1,871 +0,0 @@ -package issueops - -import ( - "fmt" - "go/ast" - "go/parser" - "go/token" - "os" - "path/filepath" - "reflect" - "sort" - "testing" - - "github.com/steveyegge/beads/internal/storage/journalscan" -) - -// This file is the versioned-history twin of journal_completeness_test.go. The -// law it pins (gastownhall/beads#6358, Phase 2 dual-write): every ACCEPTED -// mutation of an issue's durable state — the issues/wisps columns, its labels -// and its outgoing dependencies, i.e. what GetIssueInTx hydrates plus what -// RecordVersionInTx loads — mints exactly one issue_versions row, in the same -// transaction, as the mutation's LAST durable-state write; a no-op mints none; -// a wisp is never versioned (the seam excludes it itself). Comments are not in -// durable_state, is_blocked is derived, and a deleted row has nothing to -// version, so those paths deliberately never reach the seam. -// -// Like the journal guard it detects mutators STRUCTURALLY, by the DML a -// function executes, so a new write path cannot ship without either reaching -// RecordVersionInTx or being justified in the exemption table below. - -// versionMintHelpers are the helpers whose call mints a version row. There is -// exactly one seam; a function mints if it calls it directly or through a -// named helper (mintDependencyVersion, addLabelInTx, updateIssueInTx, ...) -// whose mint it did not switch off — see versionMintGate. -var versionMintHelpers = map[string]bool{ - "RecordVersionInTx": true, - "RecordVersionForCreateInTx": true, -} - -// versionMintGate names the boolean parameter the constituent helpers -// (claimIssueInTx, updateIssueInTx, applyLabelPatch, applyParentPatch, -// moveIssuePersistenceInTx, addLabelInTx, removeLabelInTx, addDependencyInTx, -// removeDependencyInTx) mint under. mintDependencyVersion spells its own -// "mint", but it is only ever handed a caller's mintVersion, never a literal, -// so the one name covers every switch-off in the package. -const versionMintGate = "mintVersion" - -// versionMintEdges follows the same call-graph discipline as journalEmitEdges -// — nothing in the derived-readiness family mints, and a mutator must not be -// able to inherit a mint through a recompute — with one refinement the journal -// guard has no need for: a helper that takes a mintVersion bool mints only -// when asked to, so a call passing the literal false for it is NOT a minting -// edge. That is how a composite mutation runs its constituents without minting -// and mints once itself at the end (ExecuteUpdate's claim, row write, label -// and parent patches and persistence move). Without the refinement the -// fixpoint credited the composite with the mint it told its constituents to -// skip, and deleting its own final RecordVersionInTx stayed green (reviewer -// Probe 2 on gastownhall/beads#6358). A call passing true, a variable, or the -// caller's own mintVersion parameter is still a minting edge, so the leaf -// wrappers (ClaimIssueInTx, ApplyLabelPatch, ...) and the forwarding helpers -// keep theirs. -func versionMintEdges(fns map[string]*journalscan.FuncInfo) func(*journalscan.FuncInfo) []string { - return func(f *journalscan.FuncInfo) []string { - var live []string - for _, callee := range journalEmitEdges(f) { - if journalscan.CallsOnlyWithLiteralFalse(fns, f, callee, versionMintGate) { - continue - } - live = append(live, callee) - } - return live - } -} - -// versionCompositeMutations are the functions that run constituents with -// versionMintGate switched off and mint once themselves, so each must carry -// its own RecordVersionInTx: nothing it calls with the gate off can carry it -// on its behalf. TestCompositeMutationsCarryTheirOwnMint is what turns -// reviewer Probe 2 (delete ExecuteUpdate's final mint block) into a failure. -// ExecuteUpdate's body is executeUpdate (ExecuteUpdate is it with the template -// guard on), so the composite named here is the body that carries the mint. -var versionCompositeMutations = []string{ - "executeUpdate", - "applyLabelPatch", - "applyParentPatch", -} - -// versionedEntryPoints are the issueops functions that mutate an issue's -// durable state and must therefore mint — directly or through a helper that -// does. Every write plumbing bottoms out in one of these; the structural -// cross-check (TestEveryBeadMutatorMintsOrIsExempt) keeps the list complete. -var versionedEntryPoints = []string{ - // the heartbeat family mints on its RE-ARM path only (recorded as a - // mutation); a refresh writes only the clone-local leases table and mints - // nothing. - "RenewLeaseInTx", - "HeartbeatIssueInTx", - "ExecuteHeartbeatInTx", - // create — the singular path mints in place; the batch path defers the - // mint past PersistDependenciesWithOptionsResult so the first version - // carries the creation-time edge set. - "CreateIssueInTx", - "CreateIssueInTxWithResult", - "CreateIssuesInTx", - "CreateIssuesInTxWithResult", - "CreateIssuesInTxWithContext", - // update, including the metadata verbs that write through it, the label - // and parent patches, and the persistence move - "UpdateIssueInTx", - "UpdateIssueWithoutEventInTx", - "MergeMetadataInTx", - "DeleteMetadataInTx", - "CompareAndSetMetadataKeyInTx", - "ApplyLabelPatch", - "ApplyParentPatch", - "MoveIssuePersistenceInTx", - // close / reopen, including the guarded CAS + savepoint path - "CloseIssueInTx", - "CloseIssueWithoutEventInTx", - "CloseIssueCheckedInTx", - "ReopenIssueInTx", - // the delete role's neighbour rewrite versions the surviving neighbours - // (the deleted rows themselves are the delete family, exempt below) - "RewriteDeletedReferencesInTx", - // claim / release / lease recovery - "ClaimIssueInTx", - "ClaimReadyIssueInTx", - "UnclaimIssueInTx", - "UnclaimIssueIfAssigneeInTx", - "ReleaseIssueInTx", - "ReclaimExpiredLeasesInTx", - // plane moves, graph edges, labels, scheduled status flips - "PromoteFromEphemeralInTx", - "AddDependencyInTx", - "RemoveDependencyInTx", - "AddLabelInTx", - "RemoveLabelInTx", - "WakeExpiredDefersInTx", - // the public lifecycle surface (roles/facade wave). These delegate to the - // leaves above; listing them pins the delegation so a role that grows its - // own DML cannot quietly stop versioning. - "ExecuteCreate", - "ExecuteCreateBatch", - "ExecuteUpdate", - "ExecuteClose", - "ExecuteCloseBatch", - "ExecuteReopen", - "ExecuteClaim", - "ExecuteClaimNext", - "ExecuteAddDependencies", - "ExecuteRemoveDependency", - "ApplyBatchInTx", -} - -// versionCreateShapedMintHelper is the create-shaped entry to the seam -// (design §16.2b). It stamps participation_generation instead of checking it, -// so it is right only for a row that is brand new on the issues plane, and -// wrong anywhere else: an update-shaped mutation that reached it would stamp -// a legacy row and so promote it, which R2.3 forbids an ordinary write. -var versionCreateShapedMintHelper = map[string]bool{"RecordVersionForCreateInTx": true} - -// versionUpdateShapedMintHelper is the update-shaped entry: it reads -// participation_generation and mints nothing for a legacy (NULL) row. -var versionUpdateShapedMintHelper = map[string]bool{"RecordVersionInTx": true} - -// versionCreateShapedMintSites are the PURE create-shaped mint sites: every -// row each one mints for is new on the issues plane, so each must call -// RecordVersionForCreateInTx itself and must never call RecordVersionInTx. -// None is left today. PromoteFromEphemeralInTx was one until it learned that -// the issues plane can already hold the id it promotes; it is listed in -// versionDualShapeMintSites now. -var versionCreateShapedMintSites = []string{} - -// versionDualShapeMintSites choose the entry per row at run time: create-shaped -// on the branch where the row is new on the issues plane, update-shaped on the -// branch where it already lived there. Each branch is a common path, so each -// site must call BOTH entries itself. Reaching either one through a delegate -// is not enough: executeUpdate runs its persistence move with -// mintVersion=false, and a batch create switches finishCreateIssueInTx's own -// mint off at run time (DeferVersionMint), so the call graph would credit each -// with mints its constituents never perform. -// -// A site that calls only one entry has had a branch switched to the other. If -// the update-shaped branch now calls the create-shaped entry, an ordinary -// write stamps a legacy row and promotes it (R2.3). If the create-shaped -// branch now calls the update-shaped entry, the fence reads the new row's NULL -// as legacy, and the record is never versioned. -// -// One production dual-shape site lives outside this package, where this -// guard's parse (journalscan.ParsePackage(".")) cannot list it: domain/db's -// issueSQLRepositoryImpl.Insert. Its CreateOnly branch is create-shaped. Its -// upsert, the create that is not CreateOnly, is create-shaped only when it -// inserted a new issues row; it is update-shaped when the issues table -// already held the id, and for every wisps-table insert. The uow leg's -// TestParticipationGenerationUpsertCreate holds those upsert branches at run -// time instead. -var versionDualShapeMintSites = map[string]string{ - "finishCreateIssueInTx": "a singular create mints create-shaped when it inserted the row and update-shaped when it resolved to an existing one", - "CreateIssuesInTxWithContext": "a batch create's deferred mint pass is create-shaped for a row the batch inserted and update-shaped for an existing row it resolved to", - "moveIssuePersistenceInTx": "a move out of the wisps table is create-shaped; the same-plane normalize (bd update --persistent repairing an issues-plane row's flags in place) is update-shaped", - "executeUpdate": "the guarded update's EnteredIssuesPlane branch is create-shaped; every other change it mints for is update-shaped", - "PromoteFromEphemeralInTx": "a wisp promoted onto the issues plane mints create-shaped; when the issues plane already held the id (dual presence, GH#4455) the upsert resolved to that row and the mint is update-shaped", -} - -// versionCreateShapedEntryPoints are the entry points whose every mint is -// create-shaped, so each must reach RecordVersionForCreateInTx. -// deferredCreates.flush (#7205's multi-row INSERT path) is one: it hands -// finishCreateIssueInTx isNew=true for every row. The call graph cannot read -// that literal, so flush still reaches the update-shaped entry through -// finishCreateIssueInTx, and it is held only to the create half. -var versionCreateShapedEntryPoints = []string{ - "deferredCreates.flush", -} - -// versionDualShapeEntryPoints reach both entries by design. Each delegates to -// a dual-shape mint site, or composes create- and update-shaped mutations in -// one call, so it must reach both and is held to neither half of the split on -// its own. The dual-shape mint sites above pin which branch calls which entry. -var versionDualShapeEntryPoints = map[string]string{ - "CreateIssueInTx": "delegates to finishCreateIssueInTx", - "CreateIssueInTxWithResult": "delegates to finishCreateIssueInTx", - "CreateIssuesInTx": "delegates to CreateIssuesInTxWithContext", - "CreateIssuesInTxWithResult": "delegates to CreateIssuesInTxWithContext", - "CreateIssuesInTxWithContext": "a dual-shape mint site itself", - "MoveIssuePersistenceInTx": "delegates to moveIssuePersistenceInTx with its mint on", - "PromoteFromEphemeralInTx": "a dual-shape mint site itself", - "ExecuteCreate": "delegates to the create path, which ends in finishCreateIssueInTx", - "ExecuteCreateBatch": "delegates to CreateIssuesInTxWithContext", - "ExecuteUpdate": "delegates to executeUpdate", - "ApplyBatchInTx": "batch apply runs create items and update items in one transaction", -} - -func versionShapedMints(t *testing.T) (fns map[string]*journalscan.FuncInfo, createShaped, updateShaped map[string]bool) { - t.Helper() - fns, err := journalscan.ParsePackage(".") - if err != nil { - t.Fatalf("parse package: %v", err) - } - edges := versionMintEdges(fns) - createShaped = journalscan.Fixpoint(fns, - func(f *journalscan.FuncInfo) bool { return f.CallsAnyOf(versionCreateShapedMintHelper) }, edges) - updateShaped = journalscan.Fixpoint(fns, - func(f *journalscan.FuncInfo) bool { return f.CallsAnyOf(versionUpdateShapedMintHelper) }, edges) - return fns, createShaped, updateShaped -} - -// TestCreateShapedEntryPointsMintCreateShaped pins the create half of the -// shape split: each pure create-shaped mint site calls -// RecordVersionForCreateInTx itself and never RecordVersionInTx, and every -// pure create-shaped entry point reaches it. If one of them falls back to -// RecordVersionInTx, the fence finds the new row's participation_generation -// NULL, skips the mint, and the record is never versioned. -// TestEveryVersionedEntryPointMints cannot see that, because -// RecordVersionInTx satisfies it. -func TestCreateShapedEntryPointsMintCreateShaped(t *testing.T) { - fns, createShaped, _ := versionShapedMints(t) - for _, site := range versionCreateShapedMintSites { - f, defined := fns[site] - if !defined { - t.Errorf("create-shaped mint site %q not found in issueops — was it renamed? update versionCreateShapedMintSites", site) - continue - } - if !f.CallsAnyOf(versionCreateShapedMintHelper) { - t.Errorf("create-shaped mint site %q does not call RecordVersionForCreateInTx itself; the row it mints for is new on the issues plane, so an update-shaped mint there is fenced as legacy", site) - } - if f.CallsAnyOf(versionUpdateShapedMintHelper) { - t.Errorf("create-shaped mint site %q calls RecordVersionInTx; every row it mints for is new on the issues plane, so that mint is fenced as legacy — or, if it now also mints for an existing row, list it in versionDualShapeMintSites", site) - } - } - for _, entry := range versionCreateShapedEntryPoints { - if _, defined := fns[entry]; !defined { - t.Errorf("create-shaped entry point %q not found in issueops — was it renamed? update versionCreateShapedEntryPoints", entry) - continue - } - if !createShaped[entry] { - t.Errorf("create-shaped entry point %q does not reach RecordVersionForCreateInTx, directly or transitively", entry) - } - } -} - -// TestDualShapeMintSitesCallBothEntries pins the dual-shape sites, the ones -// the update half below has to skip. Each calls RecordVersionInTx and -// RecordVersionForCreateInTx itself, so switching either branch to the -// other entry fails here. Each dual-shape entry point reaches both, so a -// delegate that stopped minting one shape is not silently skipped by the -// update half. -func TestDualShapeMintSitesCallBothEntries(t *testing.T) { - fns, createShaped, updateShaped := versionShapedMints(t) - for _, site := range sortedShapeKeys(versionDualShapeMintSites) { - if versionDualShapeMintSites[site] == "" { - t.Errorf("dual-shape mint site %s has an empty reason", site) - } - f, defined := fns[site] - if !defined { - t.Errorf("dual-shape mint site %q not found in issueops — was it renamed? update versionDualShapeMintSites", site) - continue - } - if !f.CallsAnyOf(versionCreateShapedMintHelper) { - t.Errorf("dual-shape mint site %q no longer calls RecordVersionForCreateInTx itself; its branch for a row new on the issues plane (%s) now fences that row as legacy, and the record is never versioned", site, versionDualShapeMintSites[site]) - } - if !f.CallsAnyOf(versionUpdateShapedMintHelper) { - t.Errorf("dual-shape mint site %q no longer calls RecordVersionInTx itself; its branch for a row already on the issues plane (%s) now stamps participation_generation and promotes a legacy record (R2.3)", site, versionDualShapeMintSites[site]) - } - } - for _, entry := range sortedShapeKeys(versionDualShapeEntryPoints) { - if versionDualShapeEntryPoints[entry] == "" { - t.Errorf("dual-shape entry point %s has an empty reason", entry) - } - if _, defined := fns[entry]; !defined { - t.Errorf("dual-shape entry point %q not found in issueops — was it renamed? update versionDualShapeEntryPoints", entry) - continue - } - if !createShaped[entry] { - t.Errorf("dual-shape entry point %q no longer reaches RecordVersionForCreateInTx — list it as update-shaped instead (remove it from versionDualShapeEntryPoints)", entry) - } - if !updateShaped[entry] { - t.Errorf("dual-shape entry point %q no longer reaches RecordVersionInTx — list it in versionCreateShapedEntryPoints instead", entry) - } - } -} - -// TestUpdateShapedEntryPointsNeverMintCreateShaped pins the update half: -// every versioned entry point that is neither create-shaped nor dual-shape -// must not reach RecordVersionForCreateInTx, so no ordinary write of an -// existing row can stamp it out of legacy. The update-shaped branch inside a -// dual-shape site is TestDualShapeMintSitesCallBothEntries's to pin. -func TestUpdateShapedEntryPointsNeverMintCreateShaped(t *testing.T) { - _, createShaped, _ := versionShapedMints(t) - create := map[string]bool{} - for _, entry := range versionCreateShapedEntryPoints { - create[entry] = true - } - var checked int - for _, entry := range versionedEntryPoints { - if create[entry] { - continue - } - if _, dual := versionDualShapeEntryPoints[entry]; dual { - continue - } - checked++ - if createShaped[entry] { - t.Errorf("update-shaped entry point %q reaches RecordVersionForCreateInTx, so it can stamp participation_generation on an existing row and promote a legacy record (R2.3) — mint it through RecordVersionInTx, or list it in versionCreateShapedEntryPoints or versionDualShapeEntryPoints if a row it mints for is new on the issues plane", entry) - } - } - if checked == 0 { - t.Fatal("no update-shaped entry point was checked — versionedEntryPoints or the shape lists changed; the guard is not actually running") - } -} - -// sortedShapeKeys returns m's keys in order, so the shape guards report in a -// stable order. -func sortedShapeKeys(m map[string]string) []string { - keys := make([]string, 0, len(m)) - for k := range m { - keys = append(keys, k) - } - sort.Strings(keys) - return keys -} - -// versionTestSupportMints sit outside the shape split above, explicitly: they -// are neither create-shaped, dual-shape nor update-shaped. They are not -// mutation entry points (versionedEntryPoints) but the seam's test-support -// twin, exempt in versionExemptions, and they reach the shared body directly -// rather than through either entry. Each must reach neither -// RecordVersionForCreateInTx nor RecordVersionInTx: the unfenced mint -// (mintUnfenced) never stamps participation_generation, so a conformance -// fixture's controlled-timestamp mint cannot promote a legacy row, and it is -// not gated or fenced, so routing it through the update-shaped entry would -// silently stop the as-of fixtures from minting history-off rows. -var versionTestSupportMints = map[string]string{ - "RecordVersionAtInTx": "R7.1 as-of read fixtures' controlled-timestamp mint (mintUnfenced): no gate, no fence, no stamp", -} - -// TestTestSupportMintsStayOutsideTheShapeSplit pins versionTestSupportMints. -func TestTestSupportMintsStayOutsideTheShapeSplit(t *testing.T) { - fns, createShaped, updateShaped := versionShapedMints(t) - shapeListed := map[string]string{} - for _, entry := range versionedEntryPoints { - shapeListed[entry] = "versionedEntryPoints" - } - for _, entry := range versionCreateShapedEntryPoints { - shapeListed[entry] = "versionCreateShapedEntryPoints" - } - for _, site := range versionCreateShapedMintSites { - shapeListed[site] = "versionCreateShapedMintSites" - } - for entry := range versionDualShapeEntryPoints { - shapeListed[entry] = "versionDualShapeEntryPoints" - } - for site := range versionDualShapeMintSites { - shapeListed[site] = "versionDualShapeMintSites" - } - for _, name := range sortedShapeKeys(versionTestSupportMints) { - if versionTestSupportMints[name] == "" { - t.Errorf("%s has an empty test-support reason", name) - } - if _, defined := fns[name]; !defined { - t.Errorf("test-support mint %q not found in issueops — was it renamed? update versionTestSupportMints", name) - continue - } - if list, listed := shapeListed[name]; listed { - t.Errorf("test-support mint %q is listed in %s; it is the seam's twin, not a mutation entry point or mint site, and stays outside the shape split", name, list) - } - if createShaped[name] { - t.Errorf("test-support mint %q reaches RecordVersionForCreateInTx, so a fixture's mint can stamp participation_generation and promote a legacy record (R2.3)", name) - } - if updateShaped[name] { - t.Errorf("test-support mint %q reaches RecordVersionInTx, whose gate and fence would stop an as-of fixture from minting a history-off row", name) - } - if _, exempt := versionExemptions[name]; !exempt { - t.Errorf("test-support mint %q is not in versionExemptions; it is the seam's twin, not a mutation entry point", name) - } - } -} - -// versionExemptions are exported functions the DML detector flags as writing a -// work-bead table but which legitimately do NOT mint a version for the row -// they write, each with a reason. The staleness check fails if any stops being -// flagged, so an exemption cannot rot. Note that some of these DO reach the -// seam transitively for a NEIGHBOUR (DeleteInTx rewrites references on the -// surviving issues through UpdateIssueInTx); the exemption is about the row -// the function itself writes, and versionNeverMints below pins the ones that -// must not reach the seam at all. -var versionExemptions = map[string]string{ - // comments — a separate table, not part of durable_state (GetIssueInTx - // hydrates labels, not comments), so a comment write versions nothing. - "AddIssueCommentInTx": "comments are not in durable_state", - "ImportIssueCommentInTx": "comments are not in durable_state", - "ExecuteAddComment": "comments are not in durable_state", - "AddCommentEventInTx": "comments are not in durable_state", - "PersistComments": "constituent comment write of a create; comments are not in durable_state", - "InsertDerivedComment": "raw comment insert; comments are not in durable_state", - - // is_blocked — derived readiness state, recomputed from the graph, never - // a mutation of the bead in its own right. - "RecomputeIsBlockedInTx": "is_blocked is derived state", - "RecomputeIsBlockedInTxWithResult": "is_blocked is derived state", - "RecomputeIsBlockedForIDsInTx": "is_blocked is derived state", - "RecomputeIsBlockedForWispIDsInTx": "is_blocked is derived state", - "RecomputeIsBlockedAfterMergeInTx": "is_blocked is derived state", - "RecomputeAllIsBlockedInTx": "is_blocked is derived state", - "MarkIsBlockedInTx": "is_blocked is derived state", - - // the delete family — no surviving row to version; the deleted-Versioned- - // Bead guarantee is Phase 3 (#6358). DeleteInTx versions its NEIGHBOURS - // through RewriteDeletedReferencesInTx, never the deleted rows. - "DeleteIssueInTx": "delete family: no surviving row (Phase 3)", - "DeleteIssuesInTx": "delete family: no surviving row (Phase 3)", - "DeleteResolvedSetInTx": "delete family: no surviving row (Phase 3)", - "DeleteInTx": "delete family: no surviving row (Phase 3); neighbours version via RewriteDeletedReferencesInTx", - "SweepInTx": "delete family: no surviving row (Phase 3)", - "DeleteIssuesBySourceRepoInTx": "bulk delete: no surviving row (Phase 3)", - "DeleteWispFromDependenciesInTx": "delete-family cleanup of edges whose target is gone", - "DeleteWispsFromDependenciesInTx": "delete-family cleanup of edges whose target is gone", - "DeleteWispAuxRowsInTx": "delete-family cleanup of aux rows whose owning wisp is gone", - - // rename — history stays keyed to the old id; out of contract by design. - "UpdateIssueIDInTx": "rename: out of contract by design (history stays keyed to the old id)", - "UpdateWispIDInDependenciesInTx": "rename: dep-row rekey, out of contract by design", - "UpdateIssueIDInDependenciesInTx": "rename: dep-row rekey, out of contract by design", - - // compaction bookkeeping and restore — outside the op vocabulary; the - // content rewrite that precedes compaction versions through UpdateIssue. - "ApplyCompactionInTx": "compaction bookkeeping columns, out of contract by design", - "RestoreFromSnapshotInTx": "restore: CAS composition is Phase 3 (#6358)", - - // constituent sub-helpers whose entry point mints the whole mutation once - "InsertIssueIntoTable": "raw issue insert; the calling create entry point mints", - "InsertIssueIfNew": "raw issue insert; the calling create entry point mints", - "InsertIssueStrictInTx": "raw issue insert; the calling create/persistence-move entry point mints", - "PersistLabels": "constituent label write of a create; the create entry point mints", - "PersistDependencies": "creation-time edges; CreateIssuesInTxWithContext mints once per issue after they land", - "PersistDependenciesWithResult": "creation-time edges; CreateIssuesInTxWithContext mints once per issue after they land", - "PersistDependenciesWithOptionsResult": "creation-time edges; CreateIssuesInTxWithContext mints once per issue after they land", - "RetargetInboundDependenciesToWispInTx": "rewrites INBOUND edges during a plane move; the moved issue's own entry point mints", - "RetargetInboundDependenciesToIssueInTx": "rewrites INBOUND edges during a plane move; the moved issue's own entry point mints", - - // aux tables matched via templated %s, not work-bead state - "ReconcileChildCounters": "child_counters are derived CLI acceleration state", - "GetNextChildIDTx": "child_counters allocation, not work-bead state", - "RecordEventInTable": "writes the events audit table (templated %s), not work-bead state", - "RecordFullEventInTable": "writes the events audit table (templated %s), not work-bead state", - "InsertDerivedEvent": "writes the events audit table (templated %s), not work-bead state", - "InsertDerivedEventReturningID": "writes the events audit table (templated %s), not work-bead state", - - // the seam itself: its UPDATE issues SET current_revision is the - // bookkeeping half of the mint, not a mutation that needs its own. - "RecordVersionInTx": "the seam itself; advances current_revision to match the row it just inserted", - "RecordVersionForCreateInTx": "the seam's create-shaped sibling; stamps participation_generation and advances current_revision to match the row it just inserted", - - // RecordVersionAtInTx (R7.1 as-of read, gastownhall/beads#5898 rev 9, - // gastownhall/beads#6136) shares RecordVersionInTx's body - // (recordVersionAtInTx) but deliberately does not call RecordVersionInTx - // itself — it exists precisely to bypass RecordVersionInTx's - // versionedHistoryEnabled gate, so an as-of-read conformance fixture can - // mint a version dated in the past. It is the seam too, by the same - // reasoning as RecordVersionInTx's own entry above. - "RecordVersionAtInTx": "test-support twin of RecordVersionInTx, sharing its body; the seam itself, not a caller of it — advances current_revision to match the row it just inserted", -} - -// versionNeverMints pins the deliberate NOT-versioned rulings from the other -// side: each must exist and must not reach RecordVersionInTx, directly or -// transitively, so a well-meaning edit cannot silently start versioning -// comments, derived state, deletes, renames, compaction bookkeeping, lease -// keepalives or bootstrap. (Migrations live in the schema package and are out -// of this scan's reach; they are out of contract by design.) -var versionNeverMints = map[string]string{ - "AddIssueCommentInTx": "comments are not in durable_state", - "ImportIssueCommentInTx": "comments are not in durable_state", - "ExecuteAddComment": "comments are not in durable_state", - "RecomputeIsBlockedInTx": "is_blocked is derived state", - "RecomputeIsBlockedInTxWithResult": "is_blocked is derived state", - "RecomputeIsBlockedForIDsInTx": "is_blocked is derived state", - "RecomputeIsBlockedForWispIDsInTx": "is_blocked is derived state", - "RecomputeIsBlockedAfterMergeInTx": "is_blocked is derived state", - "RecomputeAllIsBlockedInTx": "is_blocked is derived state", - "MarkIsBlockedInTx": "is_blocked is derived state", - "DeleteIssueInTx": "delete family (Phase 3)", - "DeleteIssuesInTx": "delete family (Phase 3)", - "DeleteResolvedSetInTx": "delete family (Phase 3)", - "DeleteIssuesBySourceRepoInTx": "bulk delete (Phase 3)", - "UpdateIssueIDInTx": "rename: out of contract by design", - "ApplyCompactionInTx": "compaction bookkeeping: out of contract by design", - "RestoreFromSnapshotInTx": "restore: Phase 3", - "BootstrapInTx": "bootstrap: config and metadata tables only, no issue-plane row", -} - -func versionMints(t *testing.T) (map[string]*journalscan.FuncInfo, map[string]bool) { - t.Helper() - return versionMintsIn(t, ".") -} - -// versionMintsIn is versionMints over the package at dir, so the guard's own -// machinery can be proven against a synthetic package. -func versionMintsIn(t *testing.T, dir string) (map[string]*journalscan.FuncInfo, map[string]bool) { - t.Helper() - fns, err := journalscan.ParsePackage(dir) - if err != nil { - t.Fatalf("parse package %s: %v", dir, err) - } - mints := journalscan.Fixpoint(fns, - func(f *journalscan.FuncInfo) bool { return f.CallsAnyOf(versionMintHelpers) }, - versionMintEdges(fns)) - return fns, mints -} - -// compositeMintLeaks returns, for a composite mutation f, every function key -// that one of its live edges — the calls left after the literal-false -// constituents are dropped — resolves to and that reaches the seam: each is a -// mint f would perform besides its own. The called bare name is resolved to -// every key it can denote, the free function and every method of that name, -// exactly as the fixpoint resolves it. mints is keyed "Recv.Name" for a -// method, so a bare-name lookup would quietly stop seeing a constituent the -// moment it became one (TestCompositeCheckSeesMethodConstituents). -func compositeMintLeaks(fns map[string]*journalscan.FuncInfo, mints map[string]bool, f *journalscan.FuncInfo) []string { - seen := map[string]bool{} - var leaks []string - for _, callee := range versionMintEdges(fns)(f) { - for _, key := range journalscan.Resolve(fns, callee) { - if mints[key] && !seen[key] { - seen[key] = true - leaks = append(leaks, key) - } - } - } - sort.Strings(leaks) - return leaks -} - -// TestCompositeMutationsCarryTheirOwnMint pins that each composite mutation -// calls RecordVersionInTx itself AND that none of the edges it leaves live -// (after the literal-false constituents are dropped) reaches the seam, each -// called name resolved to every function it can denote (compositeMintLeaks) -// — so the composite's own call is load-bearing, and deleting it fails -// TestEveryVersionedEntryPointMints rather than being papered over by a -// constituent it explicitly told not to mint. -func TestCompositeMutationsCarryTheirOwnMint(t *testing.T) { - fns, mints := versionMints(t) - for _, name := range versionCompositeMutations { - f, defined := fns[name] - if !defined { - t.Errorf("composite mutation %q not found in issueops — was it renamed? update versionCompositeMutations", name) - continue - } - if !f.CallsAnyOf(versionMintHelpers) { - t.Errorf("composite mutation %q does not call RecordVersionInTx itself; it runs its constituents with %s=false, so nothing else mints for it", name, versionMintGate) - } - for _, key := range compositeMintLeaks(fns, mints, f) { - t.Errorf("composite mutation %q would also mint through %q, so its own RecordVersionInTx is no longer the one mint the write-path doc promises — either that constituent is called with %s left on, or it mints unconditionally", name, key, versionMintGate) - } - } -} - -// TestCompositeCheckSeesMethodConstituents proves the composite check against -// a synthetic package whose constituent is a METHOD: the mints fixpoint keys -// it "svc.constituent", the composite calls it by the bare name, and the -// check must still see the mint a call with the gate left on lets through. -// This is what keeps TestCompositeMutationsCarryTheirOwnMint from quietly -// ceasing to bite should a constituent in issueops become a method. -func TestCompositeCheckSeesMethodConstituents(t *testing.T) { - dir := t.TempDir() - src := `package probe - -func RecordVersionInTx() {} - -type svc struct{} - -func (s *svc) constituent(mintVersion bool) { - if mintVersion { - RecordVersionInTx() - } -} - -// Composite runs its constituent with the gate off and mints once itself. -func Composite(s *svc) { s.constituent(false); RecordVersionInTx() } - -// Leaky leaves the gate on, so its constituent mints as well as it does. -func Leaky(s *svc) { s.constituent(true); RecordVersionInTx() } -` - if err := os.WriteFile(filepath.Join(dir, "probe.go"), []byte(src), 0o600); err != nil { - t.Fatal(err) - } - fns, mints := versionMintsIn(t, dir) - if !mints["svc.constituent"] { - t.Fatal("svc.constituent does not mint; the synthetic package no longer exercises a method constituent") - } - if mints["constituent"] { - t.Fatal("mints is keyed by the bare name of a method; the check must resolve to the receiver-qualified key, and this probe no longer shows that it has to") - } - if got := compositeMintLeaks(fns, mints, fns["Composite"]); len(got) != 0 { - t.Errorf("Composite leaks through %q, want nothing: its constituent is switched off", got) - } - if got := compositeMintLeaks(fns, mints, fns["Leaky"]); !reflect.DeepEqual(got, []string{"svc.constituent"}) { - t.Errorf("Leaky leaks through %q, want [svc.constituent]: a method constituent left with the gate on must be seen through its receiver-qualified key", got) - } -} - -// TestFalseIsNotShadowedInIssueops pins the reading CallsOnlyWithLiteralFalse -// rests on as EXACT for this package rather than merely conservative. The -// scanner counts a false argument as the predeclared identifier only when -// nothing it can see declares that name; this walk proves nothing in the -// package does, in any declaration position — so every mintVersion=false in -// issueops is the switch-off the guard takes it for, and no true is a false -// in disguise. Test files are walked too: a package-level shadow in one would -// change what every false in the package means under test. -func TestFalseIsNotShadowedInIssueops(t *testing.T) { - fset := token.NewFileSet() - pkgs, err := parser.ParseDir(fset, ".", nil, parser.SkipObjectResolution) - if err != nil { - t.Fatalf("parse issueops package: %v", err) - } - var positions int - var shadows []string - declared := func(what string, ident *ast.Ident) { - if ident == nil { - return - } - positions++ - if ident.Name == "false" || ident.Name == "true" { - shadows = append(shadows, fmt.Sprintf("%s: %s named %s", fset.Position(ident.Pos()), what, ident.Name)) - } - } - declaredFields := func(what string, list *ast.FieldList) { - if list == nil { - return - } - for _, field := range list.List { - for _, name := range field.Names { - declared(what, name) - } - } - } - for _, pkg := range pkgs { - for _, file := range pkg.Files { - ast.Inspect(file, func(n ast.Node) bool { - switch node := n.(type) { - case *ast.ImportSpec: - declared("import name", node.Name) - case *ast.ValueSpec: - for _, name := range node.Names { - declared("var/const", name) - } - case *ast.TypeSpec: - declared("type", node.Name) - case *ast.FuncDecl: - declared("function", node.Name) - declaredFields("receiver", node.Recv) - case *ast.FuncType: // the signature of a FuncDecl or a FuncLit - declaredFields("parameter", node.Params) - declaredFields("result", node.Results) - case *ast.AssignStmt: - if node.Tok == token.DEFINE { - for _, lhs := range node.Lhs { - if ident, ok := lhs.(*ast.Ident); ok { - declared("short variable declaration", ident) - } - } - } - case *ast.RangeStmt: - if node.Tok == token.DEFINE { - for _, expr := range []ast.Expr{node.Key, node.Value} { - if ident, ok := expr.(*ast.Ident); ok { - declared("range variable", ident) - } - } - } - case *ast.LabeledStmt: - declared("label", node.Label) - } - return true - }) - } - } - if positions == 0 { - t.Fatal("the walk visited no declaration position in issueops — parsing changed; the guard is not actually running") - } - sort.Strings(shadows) - for _, shadow := range shadows { - t.Errorf("%s shadows a predeclared identifier, so a %s=false in this package may not be the switch-off the guard reads it as — rename it", shadow, versionMintGate) - } -} - -// TestLiteralFalseCalleesResolveUnambiguously pins the other half of the -// switch-off's exactness. Resolution is name-based: a called bare name -// denotes the free function of that name AND every method of that name, and -// when a caller switches the name off with mintVersion=false the fixpoint -// drops its edge to all of them — including a same-named function that -// declares no mintVersion and mints unconditionally, whose mint the switch-off -// cannot have switched off. So every callee reached by a literal-false -// mintVersion call must resolve to exactly one function, or every function it -// resolves to must declare the gate; otherwise the name is ambiguous and one -// of them must be renamed. (Resolve's semantics are deliberately left alone: -// name-based resolution is what keeps the guard free of type information.) -func TestLiteralFalseCalleesResolveUnambiguously(t *testing.T) { - fns, _ := versionMints(t) - callers := make([]string, 0, len(fns)) - for key := range fns { - callers = append(callers, key) - } - sort.Strings(callers) - var switchedOff int - checked := map[string]bool{} - for _, caller := range callers { - f := fns[caller] - for _, callee := range f.AllCallNames() { - if !journalscan.CallsOnlyWithLiteralFalse(fns, f, callee, versionMintGate) { - continue - } - switchedOff++ - if checked[callee] { - continue - } - checked[callee] = true - keys := journalscan.Resolve(fns, callee) - if len(keys) == 1 { - continue - } - var ungated []string - for _, key := range keys { - if fns[key].ParamIndex(versionMintGate) < 0 { - ungated = append(ungated, key) - } - } - if len(ungated) > 0 { - t.Errorf("%q is called with %s=false (by %s) and resolves to %q, of which %q declare no %s: the switch-off would drop the fixpoint's edge to a function it cannot have switched off — rename one of them, or give it a %s parameter", callee, versionMintGate, caller, keys, ungated, versionMintGate, versionMintGate) - } - } - } - if switchedOff == 0 { - t.Fatalf("no %s=false call found in issueops — the composite mutations no longer switch their constituents off, or the predicate stopped seeing the literal; the guard is not exercising the switch-off at all", versionMintGate) - } -} - -// TestEveryVersionedEntryPointMints parses this package's source, builds the -// intra-package call graph, and asserts every versioned entry point reaches -// RecordVersionInTx directly or through a function that transitively does. -// If a listed mutation stops minting — directly or through its delegates — -// this test fails. -func TestEveryVersionedEntryPointMints(t *testing.T) { - fns, mints := versionMints(t) - for _, entry := range versionedEntryPoints { - if _, defined := fns[entry]; !defined { - t.Errorf("versioned entry point %q not found in issueops — was it renamed? update versionedEntryPoints", entry) - continue - } - if !mints[entry] { - t.Errorf("versioned entry point %q does not mint a version: it neither calls RecordVersionInTx nor a function that transitively does", entry) - } - } -} - -// TestEveryBeadMutatorMintsOrIsExempt is the STRUCTURAL completeness -// cross-check. It detects, by DML rather than by name, every EXPORTED function -// that writes a work-bead table (INSERT / UPDATE / DELETE against issues, -// wisps, dependencies, labels, comments, and their wisp variants — literal or -// templated table name), and asserts each one mints (reaches RecordVersionInTx -// directly or transitively) OR is explicitly exempted with a reason. A new -// exported mutator that writes a bead table therefore cannot ship without -// either versioning or being justified in versionExemptions. -func TestEveryBeadMutatorMintsOrIsExempt(t *testing.T) { - fns, mints := versionMints(t) - - beadDML := journalscan.Fixpoint(fns, - func(f *journalscan.FuncInfo) bool { return f.OwnBeadDML }, - func(f *journalscan.FuncInfo) []string { return f.IdentCalls }) - - seenExempt := map[string]bool{} - var checked int - for key, f := range fns { - if f.Recv != "" || !f.Exported || !beadDML[key] { - continue - } - if reason, ok := versionExemptions[f.Name]; ok { - if reason == "" { - t.Errorf("%s has an empty exemption reason", f.Name) - } - seenExempt[f.Name] = true - continue - } - checked++ - if !mints[key] { - t.Errorf("exported function %q writes a work-bead table but mints no version (no RecordVersionInTx directly or transitively) and is not exempted — make it mint, or add it to versionExemptions with a reason", f.Name) - } - } - - if checked == 0 { - t.Fatal("cross-check found no exported bead mutators — DML detection or parsing changed; the guard is not actually running") - } - for m := range versionExemptions { - if !seenExempt[m] { - t.Errorf("exemption %q no longer matches an exported bead-writing function — remove it", m) - } - } -} - -// TestVersionNeverMintsPathsStaySilent is the inverse guard. A deliberate -// decision NOT to version is as much a contract as a decision to version, and -// the structural check above cannot defend it. Each entry must still exist (a -// rename invalidates the ruling) and must still be silent (an added mint -// reverses it). -func TestVersionNeverMintsPathsStaySilent(t *testing.T) { - fns, mints := versionMints(t) - for name, reason := range versionNeverMints { - if reason == "" { - t.Errorf("%s has an empty exemption reason", name) - } - if _, defined := fns[name]; !defined { - t.Errorf("never-mints path %q not found in issueops — was it renamed? update versionNeverMints", name) - continue - } - if mints[name] { - t.Errorf("never-mints path %q now reaches RecordVersionInTx, reversing a deliberate ruling: %s\n"+ - "If the ruling changed, move it to versionedEntryPoints; otherwise remove the mint.", name, reason) - } - } -} - -// TestVersionedEntryPointsAreNotExempt keeps the two tables disjoint: a -// function cannot both be required to mint and be excused from it. -func TestVersionedEntryPointsAreNotExempt(t *testing.T) { - for _, entry := range versionedEntryPoints { - if _, ok := versionExemptions[entry]; ok { - t.Errorf("%q is both a versioned entry point and exempt — pick one", entry) - } - if _, ok := versionNeverMints[entry]; ok { - t.Errorf("%q is both a versioned entry point and a never-mints path — pick one", entry) - } - } -} diff --git a/internal/storage/issueops/version_history.go b/internal/storage/issueops/version_history.go deleted file mode 100644 index 202220502..000000000 --- a/internal/storage/issueops/version_history.go +++ /dev/null @@ -1,804 +0,0 @@ -package issueops - -import ( - "bytes" - "context" - "database/sql" - "encoding/json" - "errors" - "fmt" - "io" - "math" - "strconv" - "sync" - "time" - - "github.com/gowebpki/jcs" - - "github.com/steveyegge/beads/internal/types" -) - -// Dual-write issue-version history records every accepted issue mutation as a -// row in issue_versions, written in the SAME transaction as the mutation -// itself (see internal/storage/schema/migrations/0067_add_versioned_beads_schema.up.sql). -// Activation mirrors the durable events journal (journal.go): a per-instance -// flag (storage.VersionedHistoryConfigurer) is bound to a concrete -// transaction via ScopeVersionedHistoryTransaction immediately after -// BeginTx, so enabling history on one store instance cannot turn it on for -// any other sharing the process. -// -// RecordVersionInTx (and its create-shaped sibling, RecordVersionForCreateInTx -// — design §16.2b's write fence, see that function's own doc) is the single -// seam both direct-SQL legs (dolt, embeddeddolt) and the domain/db package -// (used by uow) call through, from inside the same already-short-circuited -// functions that call RecordEventInTx — every accepted mutation of an -// issue's durable state (create, update, close, reopen, claim, release, -// lease reclaim, defer wake, label add/remove, dependency add/remove, -// promote, persistence move) mints exactly one row, as its LAST -// durable-state write; a no-op mints none. A -// mutation DiscardNoopIssueUpdates has already discarded never reaches either -// seam; the label and dependency helpers, which that filter does not cover, -// gate on their own inserted/deleted row instead (an idempotent re-add or an -// absent remove never reaches this seam either). Composite mutations — -// ExecuteUpdate's claim + row write + label/parent/persistence patches, and a -// batch create's rows + creation-time edges — run their constituents without -// minting and mint once at the end, so the version carries the final state. -// version_completeness_test.go pins the set of issueops functions that must -// reach this seam and the ones deliberately exempt from it. -// -// Ordinals are LOCAL, not wire addresses. issues.current_revision and -// issue_versions.revision are per-store ordinals, minted as MAX(revision)+1 -// inside the writing transaction: two disconnected clones can both hold -// revision 8 for the same issue, each describing a different state. The -// only durable address of a version is version_id (migration 0068 steps -// 1-3, not yet landed). The HTTP API's RowVersion/Revision is a -// compare-and-set token, never an address; Phase 3's read surface returns -// version_id, never the ordinal. -// -// SINGLE WRITER ONLY until the version_id primary-key swap lands. With -// PRIMARY KEY (issue_id, revision), two disconnected writers that each mint -// the same ordinal for the same issue collide on merge, and -// TryAutoResolveMergeConflicts (internal/storage/versioncontrolops/mergesettle.go) fails the -// pull for a table it does not know. Enabling versioned history is therefore -// safe only with a SINGLE writer per store until migration 0068 steps 1-3 -// (UUID version_id primary key, ordinal demoted to an index) land; those -// steps follow as their own PR. -// -// "Single writer" means one writer AT A TIME per store, not merely one -// clone. SELECT COALESCE(MAX(revision), 0) + 1 against PRIMARY KEY -// (issue_id, revision) is not a safe allocator for two concurrent -// transactions in one store either, and the label and dependency paths are -// not serialized on the issue row the way the claim/close CAS is; the -// RunDualWrite* contract cases exclude concurrent writers, so no test -// speaks to it. Tracked as gastownhall/beads#6379 (item 4). -// -// MINT COST, deferred deliberately: each mint adds ~6-7 round-trips inside -// the write transaction (the GetIssueInTx snapshot and its label hydration, -// the participation_generation fence read, the dependency load, the -// store_epoch read, the MAX(revision) scan, the version INSERT, the -// current_revision UPDATE), and the MAX(revision) scan grows with an issue's -// history depth -- on paths that previously ran one or two statements, so it -// lengthens write-lock hold time. Harmless while the flag is off, which is -// every build so far. Before any phase enables it, seed the next ordinal from -// the row's current_revision and retire the aggregate scan -- a natural -// pairing with the version_id UUID swap, since that swap is what makes the -// ordinal stop being the key. The snapshot does not read current_revision; -// the fence read can select it beside participation_generation, so the seed -// costs no extra round-trip. Tracked as gastownhall/beads#6379 alongside -// item 4. - -// THE MINT'S REFUSAL IS AN INTEGRITY BACKSTOP, NOT AN ADMISSION POLICY, AND IT -// BINDS THE ROWS THAT PARTICIPATE IN HISTORY. -// canonicalDurableState refuses what it cannot record faithfully -- a number -// outside the I-JSON exact-integer range, wherever in the issue it sits (in -// metadata, or in a gate's timeout, which is written as nanoseconds), and -// duplicate keys -- and it does so inside the mutating transaction, so with -// history on a write that introduces such a value to a row that participates -// fails atomically and commits nothing: refusing at the mint IS refusing the -// write, for every store-mediated write to such a row. That is what keeps a -// version's content token meaning what it says. It is not a rule about what an -// issue may hold, which stays open while history is off, and that is the case to -// plan for: a row that participates in history (participation_generation is set), -// written while history was off or by a path that does not mint (such as bd sql, -// an older bd or a pull from a clone that had history off), that already holds -// such a value would fail every later write to it once history is on. `bd config -// set versioned-history.enabled true` therefore checks the store first, with the -// check the mint runs (CheckIssueVersionable, over every issue the mint would -// version), and refuses, writing nothing, while any of them holds a value it would -// refuse. -// -// A row that does not participate is outside all of this. Design §16.2b makes an -// update-shaped write to a legacy record (participation_generation NULL) a skip, -// not a refusal: the fence returns before the snapshot is canonicalized, so no -// version is minted and the gate never runs. Such a write succeeds whatever the -// row's metadata holds, exactly as it would with history off, because no version -// row, and so no content token, comes of it. A create-shaped mint never meets a -// legacy row (its call site has just inserted the row), so it runs the gate -// whenever it mints; a new row flagged no-history is stamped but not minted, so -// nothing is canonicalized for it. The switch's check cannot see participation, -// so it checks legacy rows too: an over-approximation that can only refuse more. -// -// That check ran once, at that moment. Nothing keeps a writer that does not mint -// from adding such a row afterwards, even right after the switch is on; if the -// row participates, the first write to it then fails with the same refusal, and -// the same fix applies. Making the check and the write of the setting one -// transaction would narrow that window, not close it. Turning history on through -// the environment or config.yaml does not pass through that command at all; for -// those planes this refusal is the control on the rows that participate, and -// finding the rows ahead of time is a bd doctor check (gastownhall/beads#6379 -// item 3). -// -// Duplicate keys are the exception to "the case to plan for". A Dolt JSON column -// collapses duplicate keys when it stores a document, so on a Dolt store neither -// the mint nor the scan can ever see one, and the refusal is a defense for a -// backend registered through backends.Lookup that keeps them. It is worth -// checking again whenever Dolt is upgraded. - -var versionedHistoryTransactions sync.Map // map[DBTX]bool; entries live for one transaction - -// ScopeVersionedHistoryTransaction associates versioned-history activation -// with one concrete transaction and returns a cleanup function. Store -// implementations call it immediately after BeginTx, alongside (not instead -// of) ScopeEventsJournalTransaction. This is instance/project scoped even -// when many stores share a process; there is no process-wide activation -// switch. -func ScopeVersionedHistoryTransaction(tx DBTX, enabled bool) func() { - if tx == nil { - return func() {} - } - versionedHistoryTransactions.Store(tx, enabled) - return func() { versionedHistoryTransactions.Delete(tx) } -} - -func versionedHistoryEnabled(tx DBTX) bool { - enabled, _ := versionedHistoryTransactions.Load(tx) - on, _ := enabled.(bool) - return on -} - -// VersionedHistoryEnabledFor reports whether versioned history is active on -// tx, the transaction ScopeVersionedHistoryTransaction bound. A commit path -// that stages a NAMED set of tables uses it to add -// VersionedHistoryStagedTables only when the transaction could have minted. -func VersionedHistoryEnabledFor(tx DBTX) bool { - return versionedHistoryEnabled(tx) -} - -// VersionedHistoryStagedTables names every table RecordVersionInTx writes, so -// a staging path can add them to whatever it was already going to stage. -// -// THE PLANE DECISION, stated once here because the reviewer of #6675 rightly -// refused to infer it: issue_versions and store_epoch REPLICATE. They are -// ordinary synced Dolt tables, deliberately NOT dolt_ignore'd the way the -// events journal's tables are (migration 0064), because a bead's version -// history is part of the bead and has to travel with it — and because the -// single-writer constraint this package documents at length is a statement -// about what happens when two clones MERGE these tables, which is only -// meaningful for a table that replicates at all. -// -// Replicating means every operation that mints has to STAGE what it minted, -// or the rows sit dirty in the working set: unreplicated, absent from the -// DOLT_COMMIT of the very mutation they describe, swept into whichever later -// unrelated commit stages broadly, and able to trip DirtyTablesError on the -// next migration. That is the same-transaction durability promise this seam -// exists to keep, so staging is not an optimization here. -// -// `issues` is in the list because the mint's LAST write is -// `UPDATE issues SET current_revision`. On issue-plane mutations that table is -// already staged; on the dependency paths it is not, which is the case this -// list exists for. Note that staging `issues` there is gated on versioned -// history being active (see the store-side callers), so it does not widen what -// an ordinary dep-add commit contains — the pre-existing unstaged-`issues` -// gap on those paths is bd-2y9ke, and deliberately still theirs to fix. -func VersionedHistoryStagedTables() []string { - return []string{"issue_versions", "store_epoch", "issues"} -} - -// issue_versions.attribution_status is a NOT NULL column (migration 0068 -// step 6) whose vocabulary aligns to BDP's carried-attribution status -// (gastownhall/bdp#18, merged 2026-09-07): status ∈ {claimed, unknown}. A -// status is an assertion about the actor the mutation arrived with — -// "claimed" when one was supplied, "unknown" when the mutation path had none -// — and is derived from the same actor string every RecordVersionInTx call -// site already passes (no call site carries any other attribution signal). -// -// "imported" is deliberately NOT a status: it is provenance (where a row -// came from), not an assertion about who performed the mutation. It returns -// as a separate provenance marker in the phase that first imports history; -// no writer for it exists yet — Phase 2 is this table's sole writer, and -// every row it mints originated as an accepted mutation. -const ( - attributionStatusClaimed = "claimed" - attributionStatusUnknown = "unknown" -) - -// attributionStatusForActor derives issue_versions.attribution_status from -// the same actor string every RecordVersionInTx call site already passes: a -// non-empty actor is "claimed", an empty one is "unknown". There is no -// third value — an empty actor means the path had no identity to assert, -// which is exactly what "unknown" says; any stronger reading (a deliberate, -// confirmed absence of attribution) is a claim no call site makes. -func attributionStatusForActor(actor string) string { - if actor == "" { - return attributionStatusUnknown - } - return attributionStatusClaimed -} - -// canonicalDurableState renders issue as the bytes RecordVersionInTx stores -// in issue_versions.durable_state: encoding/json's marshal of the issue, -// canonicalized per RFC 8785 (JCS). The canonical form is a function of the -// issue's content alone -- keys sorted, numbers in their one ES6 form -// (1.0 is 1, 1e300 is 1e+300), only the escapes RFC 8785 requires -- so the -// same issue state always yields the same bytes, whatever encoding/json's -// formatting happens to be. Numbers are canonicalized as IEEE-754 doubles -// (RFC 8785 section 3.2.2.3), so canonicalization alone would round a number -// past 2^53 here, once, by the writer, before anything hashes it; the admission -// gate (refuseUnrepresentableIntegers) refuses such a number instead, so the -// stored bytes and the hashed bytes are the same bytes and nothing is rounded -// silently. Two fields of types.Issue can reach that magnitude: Metadata, and -// Timeout, a time.Duration that encoding/json writes as a count of nanoseconds, -// so a gate timeout above 2^53-1 ns (about 104.25 days) is refused. Its other -// integers are priorities, ordinals and counts held in 32-bit columns. -// -// A refusal names the top-level field it was found in (nameRefusedField). The -// success path reads the document once; the field is located only after a -// refusal. -// -// Two properties worth stating because a hand-rolled canonicalizer would get -// them wrong. First, jcs.Transform normalizes away encoding/json's HTML -// escaping of <, > and &, so the token is a function of content, not of -// Go's escaping policy. Second, a snapshot that cannot be canonicalized -// deliberately FAILS the mutation: types.Issue.Metadata is a json.RawMessage -// passed through verbatim, RFC 8785 rejects duplicate keys, and this seam -// returns the error to its caller, which aborts the transaction. A backend that -// keeps duplicate keys in a metadata document would therefore let an issue -// mutate with the flag off and make it unmutatable with it on, if it participates -// in history; a legacy row stays writable, because the fence skips it before this -// function runs. Dolt does not keep them (see the note above), so on a Dolt -// store this refusal is never reached. Fail-closed is the policy for a backend -// that does -- bytes that could canonicalize two ways would not be a content -// token -- and whether to normalize such metadata first, or to find such rows with -// bd doctor, is gastownhall/beads#6379 (item 3). -func canonicalDurableState(issue any) ([]byte, error) { - marshaled, err := json.Marshal(issue) - if err != nil { - return nil, err - } - if err := refuseUnrepresentableIntegers(marshaled); err != nil { - return nil, nameRefusedField(marshaled, err) - } - canonical, err := jcsCanonicalize(marshaled) - if err != nil { - return nil, nameRefusedField(marshaled, err) - } - return canonical, nil -} - -// refusedFieldError is a refusal from canonicalDurableState that has been traced -// to the top-level field of the issue it was found in. It unwraps to the refusal -// itself, so errors.Is(err, ErrIntegerNotRepresentable) still answers. -type refusedFieldError struct { - field string - err error -} - -func (e *refusedFieldError) Error() string { return fmt.Sprintf("%v (field %q)", e.err, e.field) } - -func (e *refusedFieldError) Unwrap() error { return e.err } - -// nameRefusedField finds the top-level field of marshaled, one issue as a JSON -// object, that the two checks canonicalDurableState runs refuse on their own, and -// returns err naming it. It runs only after a refusal, so what a successful -// canonicalization costs does not change. A document it cannot attribute, one -// that is not an object or in which no single field is refused alone, comes back -// unchanged. -func nameRefusedField(marshaled []byte, err error) error { - dec := json.NewDecoder(bytes.NewReader(marshaled)) - if open, derr := dec.Token(); derr != nil || open != json.Delim('{') { - return err - } - for dec.More() { - keyToken, derr := dec.Token() - key, isString := keyToken.(string) - if derr != nil || !isString { - return err - } - var value json.RawMessage - if derr := dec.Decode(&value); derr != nil { - return err - } - if refuseUnrepresentableIntegers(value) != nil { - return &refusedFieldError{field: key, err: err} - } - if _, jerr := jcsCanonicalize(value); jerr != nil { - return &refusedFieldError{field: key, err: err} - } - } - return err -} - -// jcsCanonicalize is canonicalDurableState's RFC 8785 (JCS) step alone, below -// the admission gate rather than behind it. -// -// IT MUST EXIST SEPARATELY because refuseUnrepresentableIntegers and RFC 8785 -// disagree on purpose, not by oversight: RFC 8785 section 3.2.2.3 canonicalizes -// ANY number as a double, whatever its magnitude, while the admission gate -// refuses any number whose nearest double is past 2^53-1 outright (see -// refuseUnrepresentableIntegers's doc comment on why that rule has no -// notation carve-out). One of the RFC 8785 vectors this store pins conformance -// against, testdata/jcs/input/values.json, both ships from the spec's own -// reference suite AND contains 1E30 -- a value the gate refuses and RFC 8785 -// requires canonicalizing to 1e+30. Routing that vector through -// canonicalDurableState would force a choice between weakening spec-conformance -// coverage and putting a magnitude exception in the gate, and neither is -// right: the gate's refusal is a policy decision about what this store admits, -// not a claim about what JCS can canonicalize. jcsCanonicalize lets a caller -// ask the second question without the first, so TestCanonicalDurableStateIsJCS -// can hold the RFC 8785 vectors to the spec's own bytes -- including -// values.json -- while refuseUnrepresentableIntegers's refusal of the same -// magnitude is pinned separately, in the admission tests -// (version_history_integer_admission_test.go), where it belongs. -func jcsCanonicalize(marshaled []byte) ([]byte, error) { - canonical, err := jcs.Transform(marshaled) - if err != nil { - return nil, fmt.Errorf("canonicalize (RFC 8785): %w", err) - } - return canonical, nil -} - -// ErrIntegerNotRepresentable is the refusal for a JSON number outside the I-JSON -// exact-integer range: one whose nearest binary64 has magnitude above 2^53-1, or -// that overflows binary64. -// -// The name is historical. It began as the refusal for an integer literal binary64 -// cannot hold exactly, and it now also covers a literal spelled as a fraction whose -// nearest double lies at or beyond 2^53, which RFC 8785 would round to an integer -// just the same. It is kept rather than renamed so that nothing matching it with -// errors.Is has to change. -var ErrIntegerNotRepresentable = errors.New("number is outside the I-JSON exact-integer range (magnitude above 2^53-1)") - -// maxExactJSONInteger is 2^53-1, the largest magnitude in the I-JSON -// interoperable integer range (RFC 7493 section 2.2), and BDP's admission law -// (gastownhall/bdp#21) makes a literal past it invalid rather than merely lossy. -// binary64 does represent 2^53 exactly, but 2^53 and 2^53+1 round to the same -// double, so from 2^53 up a literal can no longer be told from its neighbor. -const maxExactJSONInteger = 1<<53 - 1 - -// refuseUnrepresentableIntegers rejects marshaled if any JSON number literal in -// it is outside the I-JSON exact-integer range: the binary64 nearest to the -// literal has magnitude above 2^53-1, or the literal overflows binary64. Which of -// JSON's number forms spells the value -- bare integer, decimal or exponential -- -// does not matter. -// -// WHY REFUSE RATHER THAN ROUND. jcs.Transform canonicalizes numbers as -// IEEE-754 doubles (RFC 8785 section 3.2.2.3), so 9007199254740993 becomes -// 9007199254740992 on the way in. That is not a formatting difference, it is -// a DIFFERENT VALUE, and it collapses two distinct issue states onto one -// durable_state and therefore one content token -- the token can no longer -// tell them apart, and a reader comparing tokens concludes the second write -// was a no-op. Rounding is silent; refusing is not. -// -// This seam is the admission gate, one layer above RecordVersionInTx, which -// is where BDP puts it: an authority adapting an existing store "MUST map or -// refuse values outside this contract before it serves them as BDP -// Resources" (gastownhall/bdp#20 section 5.1). -// -// THE NEAREST DOUBLE, NOT THE LITERAL'S SPELLING. Each literal is classified -// with strconv.ParseFloat, which returns the correctly rounded binary64 in time -// linear in the length of the literal, and that is exactly the value RFC 8785 -// canonicalizes the literal to. So "the nearest double is past the range" and -// "canonicalization would move this literal past the range" are one question, -// and the gate is closed under its own canonicalization: whatever it admits -// canonicalizes to a form it also admits. A rule keyed to integer VALUES was -// not: 9007199254740991.5 is not an integer, so it was admitted, yet it rounds -// to 2^53 and its canonical bytes were then refused. Here 9007199254740993, -// 9007199254740993.0 and 9.007199254740993e15 are refused alike, and so is -// 9007199254740993.5, whose nearest double is 2^53+2. An overflow -// (strconv.ErrRange) is a refusal, not a syntax error; a magnitude too small for -// binary64 rounds to zero and is admitted. -// -// NOTHING IS BUILT PER EXPONENT. ParseFloat never constructs the number, so a -// literal such as 1e1000000 is an overflow refused in the time it takes to read -// it, not a million-digit integer built first and then compared. The gate runs -// inside the writer's transaction, so a classifier whose cost grew with an -// exponent would be a way to hold that transaction open with a few -// exponent-heavy metadata values. -// -// SCOPE. Ordinary fractions are admitted: 0.1 is canonicalized to its nearest -// double, which is how the store holds it. Whether two literals that share a -// nearest double can be told apart inside the store is a property of the store, -// not of this gate, and it is the one thing a Dolt upgrade could change: the -// store-level tests on each leg assert that no two literals the store reads back -// as different numbers are both admitted with one canonical form, and log what -// each Dolt does to a number, rather than assuming it. An integer past 2^53 is -// refused whatever the store does to it: a store may keep it exactly, where RFC -// 8785 would round it, and a store that already rounds it hands the gate an -// integer-valued double, which the rule refuses just the same. A JSON null is a -// token, not a number: only json.Number tokens are classified. Of types.Issue's -// own fields only Metadata (a json.RawMessage passed through verbatim) and -// Timeout (a time.Duration, written as nanoseconds) can reach this bound; its -// other integers are priorities, ordinals and counts held in 32-bit columns. -func refuseUnrepresentableIntegers(marshaled []byte) error { - dec := json.NewDecoder(bytes.NewReader(marshaled)) - dec.UseNumber() - for { - tok, err := dec.Token() - if err == io.EOF { - return nil - } - if err != nil { - return fmt.Errorf("scan for unrepresentable integers: %w", err) - } - num, ok := tok.(json.Number) - if !ok { - continue - } - lit := num.String() - f, err := strconv.ParseFloat(lit, 64) - if err != nil && !errors.Is(err, strconv.ErrRange) { - return fmt.Errorf("scan for unrepresentable integers: invalid JSON number literal %q: %w", lit, err) - } - if math.IsInf(f, 0) || math.Abs(f) > maxExactJSONInteger { - return fmt.Errorf("%w: %s", ErrIntegerNotRepresentable, lit) - } - } -} - -// CheckIssueVersionable reports whether issue could be minted into a version's -// durable_state. It is canonicalDurableState's own refusal -- the admission gate, -// then RFC 8785 canonicalization, over the whole marshaled issue -- so it refuses -// a number outside the I-JSON exact-integer range (ErrIntegerNotRepresentable) -// wherever in the issue it sits, in metadata or in a gate's timeout, and it also -// refuses duplicate keys, which RFC 8785 cannot canonicalize. The refusal names -// the top-level field it found the value in. -// -// It exists so that the check made when versioned history is switched on and -// the check the mint makes cannot disagree: FindUnversionable runs this over -// every issue the mint would version, and a store that passes it cannot then -// refuse the first write to a row it cleared. It is the one such check. A check -// over only part of an issue, its metadata say, can pass what the mint refuses (a -// gate's timeout can be past the range while its metadata is fine), so none may -// be exported beside it. -// -// The issue it is handed need not be the one the mint sees. The scan reads a -// lite projection, with no heavy text columns, no labels and no dependencies, -// and that is sound only because none of what it leaves out can hold a number. -// The tests in version_history_preenable_scan_test.go pin that. -func CheckIssueVersionable(issue *types.Issue) error { - _, err := canonicalDurableState(issue) - return err -} - -// RecordVersionInTx mints one issue_versions row for issueID and advances -// issues.current_revision to match, as of tx (read-your-writes within the -// same transaction). A no-op when versioned history is disabled for tx, or -// when issueID resolves to a wisp -- a row the wisps table holds, whatever -// its flags say: wisps carry current_revision for shape parity only and are -// never versioned this phase (design FR-8). IsWisp is -// Ephemeral || NoHistory, so a promoted no-history bead -- a durable -// issues-plane row with NoHistory=true -- is also never versioned; that is -// what no-history means, not an FR-8 wisp rule, and the write-path doc's -// row 13 says so. -// -// The revision it mints is a local ordinal, not an address — see the -// package comment above on ordinals versus version_id, and on the -// single-writer constraint that holds until version_id lands. -// -// actor is the acting identity that performed the mutation, recorded as the -// version row's attribution — "" when the mutation path genuinely has none, -// matching RecordEventInTx's own convention. It also drives -// attribution_status via attributionStatusForActor. -// -// This is the UPDATE-shaped entry point: design §16.2b's write fence. A -// record whose participation_generation is NULL is legacy — never -// positively promoted — and an update-shaped mutation against it mints -// nothing, neither half of this seam's write (no issue_versions row, no -// current_revision bump). A record that already carries a non-NULL -// participation_generation proceeds normally. Use RecordVersionForCreateInTx -// for a create-shaped mutation instead: a brand-new row has no legacy state -// to preserve, so that entry point stamps the column rather than checking -// it. -// -// The skip is whole: nothing about a legacy record is read, canonicalized or -// admitted, so such a write is neither versioned nor refused (design §16.2b: a -// skip, not a refusal). -func RecordVersionInTx(ctx context.Context, tx DBTX, issueID, actor string) error { - if !versionedHistoryEnabled(tx) { - return nil - } - return recordVersionAtInTx(ctx, tx, issueID, actor, time.Now().UTC(), mintUpdate) -} - -// RecordVersionForCreateInTx is RecordVersionInTx's create-shaped sibling -// (design §16.2b). The write fence does not apply to a brand-new row, so -// instead of checking participation_generation, this stamps it — sourced -// from store_epoch.epoch, the same source the fence reads — as part of the -// row's first mint, whenever versioned history is enabled. A create with the -// flag off never reaches the stamp (this whole seam no-ops while the flag is -// off, same as RecordVersionInTx), leaving the column NULL — indistinguishable -// from a true legacy row (FR-7). A wisp arriving on the issues plane, by -// promotion or a persistence move out of the wisps table, mints through here -// too: its issues row is just as new, and a wisp never declares -// participation (FR-8). A new issues-table row that carries NoHistory (or a -// stray Ephemeral) is stamped but not versioned: no-history keeps it out of -// history for now, but it was created with the flag on, so it is not -// legacy once that flag is repaired. A row the wisps table holds mints and -// stamps nothing, whatever its flags say: it has no issues row to stamp, and -// a wisp is never versioned (FR-8). That covers an ordinary wisp create and -// an unflagged wisps-table row (import's "wisp_plane" marker) alike. -func RecordVersionForCreateInTx(ctx context.Context, tx DBTX, issueID, actor string) error { - if !versionedHistoryEnabled(tx) { - return nil - } - return recordVersionAtInTx(ctx, tx, issueID, actor, time.Now().UTC(), mintCreate) -} - -// RecordVersionAtInTx is RecordVersionInTx's test-support twin: it mints a -// version row at a caller-chosen change_at instead of time.Now(), and -- -// deliberately -- does not gate on versionedHistoryEnabled at all, since its -// whole purpose is controlled-timestamp minting for conformance fixtures -// (R7.1's AsOfReadFixture.MintAt, backend/conformance/versioned_read_contract.go) -// regardless of a store's activation state. Production code never calls -// this; only per-leg as-of-read fixtures do, so their bare issue-create step -// can stay history-off (no unwanted real-time row) while still minting the -// exact versions a test needs at the instants it needs them. -// -// It bypasses design §16.2b's write fence exactly as it bypasses the gate: -// it ignores participation_generation and never stamps it, so it mints for -// a legacy row and a participating row alike and leaves the column as it -// found it. The fence and the stamp belong to the production entry points -// above, never to a fixture's controlled-timestamp mint. The plane rule is -// not the fence, and it does apply: a row the wisps table holds, or one -// flagged Ephemeral or NoHistory, mints nothing here either. -func RecordVersionAtInTx(ctx context.Context, tx DBTX, issueID, actor string, at time.Time) error { - return recordVersionAtInTx(ctx, tx, issueID, actor, at, mintUnfenced) -} - -// versionMintKind says how recordVersionAtInTx treats design §16.2b's write -// fence, the one thing RecordVersionInTx, RecordVersionForCreateInTx and -// RecordVersionAtInTx do not share. -type versionMintKind int - -const ( - // mintUpdate is an update-shaped mutation (RecordVersionInTx): the fence - // applies, and a record whose participation_generation is NULL mints nothing. - mintUpdate versionMintKind = iota - // mintCreate is a create-shaped mutation (RecordVersionForCreateInTx): a - // brand-new row has no legacy state to preserve, so the fence does not - // apply and the mint stamps participation_generation instead. - mintCreate - // mintUnfenced is RecordVersionAtInTx's test-support mint: no fence and no - // stamp, exactly as it behaved before the fence existed. Like the other - // two, it mints nothing for a row that is not on the issues plane. - mintUnfenced -) - -// recordVersionAtInTx is RecordVersionInTx, RecordVersionForCreateInTx and -// RecordVersionAtInTx's shared body, taking at where the callers differ: -// time.Now().UTC() for the production gate-checked paths, a caller-chosen -// instant for test minting; and kind, which says whether design §16.2b's -// write fence applies (mintUpdate), the mint stamps participation_generation -// (mintCreate) or neither (mintUnfenced). The no-op rules documented on -// RecordVersionInTx above (wisps) apply here too, since this is that -// function's entire mechanism minus the gate. -func recordVersionAtInTx(ctx context.Context, tx DBTX, issueID, actor string, at time.Time, kind versionMintKind) error { - // change_at is DATETIME(6) as of migration 0069, and this function - // deliberately does NOT floor `at` to the second. - // - // It used to. While 0067's plain DATETIME (precision 0) was in force that - // floor was load-bearing: Dolt's datetime(0) does not truncate sub-second - // input, it ROUNDS half-up, so a change_at of 13:32:04.77 stored as - // 13:32:05 -- strictly LATER than the instant it recorded. That breaks - // AsOfReadInTx's "latest version accepted at or before T" - // (resolveAsOfRevisionInTx, asof_read.go) for any T between the true - // instant and the rounded-up one. Flooring first left nothing to round. - // - // 0069 widened change_at and removed_at to DATETIME(6). That moves the - // rounding below the microsecond rather than removing it: GMS still rounds - // DATETIME(6) input to the microsecond (.1234567 is stored as .123457). - // The remainder is harmless to that lookup (resolveAsOfRevisionInTx), - // because the instant it compares against is rounded the same way, so a - // stored value still compares equal to the instant it was minted at. What the widening does - // make is the floor actively harmful: it threw away the microseconds the - // widening exists to keep. Two versions minted in the same second - // collapsed onto one stored value and became indistinguishable - // by change_at -- which defeats --at, the only PORTABLE selector the read - // surface offers while revision stays a local ordinal and no durable - // version address has shipped. Measured before this change on a store - // carrying both: four versions minted in one second all read back - // 15:40:38.000000. - // - // So the column's precision is now relied on rather than worked around. - // Pinned by TestRecordVersionKeepsSubSecondChangeAt below; the column's - // own half of the contract is pinned by - // TestMigration0069ChangeAtSurvivesSubSecondPrecisionThroughDoltCLI in - // internal/storage/schema. MarkLatestVersionRemovedInTx (asof_read.go) - // needs no matching change -- it already passes time.Now().UTC() straight - // through, so removed_at was never floored. - - issue, err := GetIssueInTx(ctx, tx, issueID) - if err != nil { - return fmt.Errorf("versioned history: snapshot %s: %w", issueID, err) - } - // A flagged row (Ephemeral || NoHistory) is never versioned, so an - // update-shaped call, or the test-support mint, has nothing to do. A - // create-shaped one may still have to declare participation; see the - // IsWisp branch below. - if IsWisp(issue) && kind != mintCreate { - return nil - } - - // THE TABLE THAT HOLDS THE ROW decides its plane, not its flags. IsWisp - // reads Ephemeral || NoHistory, and a wisps-table row can carry neither: - // bd mol squash clears a wisp root's flag in place, and import's - // "wisp_plane" marker routes an unflagged row to wisps. Such a row passes - // IsWisp, but there is no issues row for it, so this read finds nothing. - // No issues row means nothing to version (FR-8), for an update- or a - // create-shaped call alike, and for the test-support mint too, since this - // is the plane rule and not the fence: mint nothing and let the mutation - // proceed, rather than abort it on sql.ErrNoRows. GetIssueInTx resolved - // the snapshot above, so the row exists; ErrNoRows here can only mean it - // is in the wisps table. - // - // The same read is design §16.2b's write fence. participation_generation - // is read straight off the row rather than through types.Issue: it is - // dual-write bookkeeping, not part of the issue's own content model, so it - // stays out of the durable_state snapshot canonicalDurableState marshals - // below. Only an update-shaped call (mintUpdate) acts on its value: a - // brand-new row (mintCreate) has no legacy state to preserve, and this - // same function stamps the column itself further down; the test-support - // mint (mintUnfenced) ignores it. An update-shaped call against a NULL - // (legacy) value returns here, before either half of this seam's write - // runs. The fence runs before the admission gate (canonicalDurableState) - // on purpose, so a legacy record is skipped whole: nothing about it is - // canonicalized, admitted or refused. - // BD_IGNORE_SCHEMA_SKEW (internal/storage/schema/schema.go) has no - // bearing on this check: it downgrades CheckForwardDrift's - // migration-cursor refusal, a different plane than this column's real - // per-row data. - var participationGeneration sql.NullInt64 - if err := tx.QueryRowContext(ctx, - "SELECT participation_generation FROM issues WHERE id = ?", issueID, - ).Scan(&participationGeneration); err != nil { - if errors.Is(err, sql.ErrNoRows) { - return nil - } - return fmt.Errorf("versioned history: read participation_generation for %s: %w", issueID, err) - } - if IsWisp(issue) { - // Only a create-shaped call gets here: an issues-table row that - // carries NoHistory (or a stray Ephemeral). It is not versioned while - // its flags say so, so this mints nothing. But it is brand new on the - // issues plane with the flag on, so it is not legacy, and the create - // still declares its participation. Import keeps a promoted - // no-history row on the issues plane exactly this way (bd-r9uce). - // When bd update --persistent later repairs the flag in place, that - // repair mints update-shaped: through the guarded update's one - // end-of-update mint (executeUpdate) on dolt and embeddeddolt, or - // MoveIssuePersistenceInTx's own on the unit-of-work path. Left NULL - // here, the fence would read the row as legacy for good. Same source - // and same updated_at rule as the stamp at the end of this function. - epoch, err := readStoreEpochInTx(ctx, tx) - if err != nil { - return err - } - if _, err := tx.ExecContext(ctx, - "UPDATE issues SET participation_generation = ?, updated_at = updated_at WHERE id = ?", - epoch, issueID, - ); err != nil { - return fmt.Errorf("versioned history: stamp participation_generation for %s: %w", issueID, err) - } - return nil - } - if kind == mintUpdate && !participationGeneration.Valid { - return nil - } - - // durable_state is the RFC 8785 (JCS) canonical form of the marshaled - // issue, stored as bytes -- issue_versions.durable_state is a LONGBLOB - // (migration 0068 step 7), not a JSON column. The invariant that buys, - // the one donnabox asked for on #6358 item 4: sha256(stored bytes) is a - // stable content token, because the bytes read back are exactly the - // bytes the writer produced. A Dolt JSON column could not provide that. - // It parses and renormalizes what it stores (measured on dolt 2.2.3: - // 1.0 reads back as 1, 9007199254740993 as 9007199254740992, 1e300 as - // 1e+300, while a LONGBLOB returns the same bytes) -- so the design's - // verbatim-bytes promise (R5.1) and any content-derived token (#5898's - // sha256-jcs) broke between the writer and the disk. JCS pins the bytes - // on the way in (canonicalDurableState); LONGBLOB keeps them as written. - // - // The snapshot itself is the mutated issue (design §15.3, corrected by - // §17.1): the write-path loader GetIssueInTx never hydrates Dependencies - // (types.Issue.Dependencies is omitempty and unrelated to this snapshot's - // own read), so it is populated here, once, for every caller of this - // seam — in GetDependencyRecordsForIssuesInTx's own ordering (issue_id, - // depends_on_id, type, id). - deps, err := GetDependencyRecordsForIssuesInTx(ctx, tx, []string{issueID}) - if err != nil { - return fmt.Errorf("versioned history: load dependencies for %s: %w", issueID, err) - } - issue.Dependencies = deps[issueID] - - epoch, err := readStoreEpochInTx(ctx, tx) - if err != nil { - return err - } - - var newRevision int64 - if err := tx.QueryRowContext(ctx, - "SELECT COALESCE(MAX(revision), 0) + 1 FROM issue_versions WHERE issue_id = ?", issueID, - ).Scan(&newRevision); err != nil { - return fmt.Errorf("versioned history: compute next revision for %s: %w", issueID, err) - } - - durableState, err := canonicalDurableState(issue) - if err != nil { - return fmt.Errorf("versioned history: marshal durable state for %s: %w", issueID, err) - } - - // durableState is bound as []byte, a LONGBLOB parameter -- never - // string(durableState), which would ask the driver to treat it as text. - if _, err := tx.ExecContext(ctx, - `INSERT INTO issue_versions - (issue_id, revision, epoch, durable_state, change_actor, change_agent, change_message, change_at, attribution_status) - VALUES (?, ?, ?, ?, ?, NULL, NULL, ?, ?)`, - issueID, newRevision, epoch, durableState, actor, at, attributionStatusForActor(actor), - ); err != nil { - return fmt.Errorf("versioned history: insert version row for %s: %w", issueID, err) - } - - if kind == mintCreate { - // Stamps participation_generation in the same statement that advances - // current_revision — design §16.2b's "positive declaration sourced - // from store_epoch.epoch," minted here from the epoch already read - // above for this same transaction's version row. Setting updated_at to - // itself keeps the column's ON UPDATE CURRENT_TIMESTAMP clause from - // replacing the timestamp the row was created with, which an import - // supplies. - if _, err := tx.ExecContext(ctx, - "UPDATE issues SET current_revision = ?, participation_generation = ?, updated_at = updated_at WHERE id = ?", - newRevision, epoch, issueID, - ); err != nil { - return fmt.Errorf("versioned history: advance current_revision for %s: %w", issueID, err) - } - return nil - } - // The same updated_at rule for every update-shaped mint. Advancing - // current_revision is bookkeeping, not an edit, but on its own it fires - // the ON UPDATE CURRENT_TIMESTAMP clause, so a label or dependency write - // that never touches updated_at would move it to the wall clock only - // while history is on, and an import's stale-upsert check would then - // reject the source's next edit. History on must leave updated_at - // exactly where history off does (the same reason #4298 and - // blocked_state.go's mark/unmark templates assign it to itself). - if _, err := tx.ExecContext(ctx, - "UPDATE issues SET current_revision = ?, updated_at = updated_at WHERE id = ?", newRevision, issueID, - ); err != nil { - return fmt.Errorf("versioned history: advance current_revision for %s: %w", issueID, err) - } - return nil -} - -// readStoreEpochInTx reads store_epoch.epoch as of tx, the source of both a -// version row's epoch and a participation_generation stamp. -// -// store_epoch is one shared row (id = 1) that every minting transaction -// reads. Read first and seed only when the row is absent, so the seed -// INSERT happens once per store rather than once per mint: an INSERT IGNORE -// on every mint put that shared row into every write transaction's -// footprint for nothing. -func readStoreEpochInTx(ctx context.Context, tx DBTX) (int, error) { - var epoch int - err := tx.QueryRowContext(ctx, "SELECT epoch FROM store_epoch WHERE id = 1").Scan(&epoch) - if errors.Is(err, sql.ErrNoRows) { - if _, seedErr := tx.ExecContext(ctx, "INSERT IGNORE INTO store_epoch (id, epoch) VALUES (1, 1)"); seedErr != nil { - return 0, fmt.Errorf("versioned history: seed store epoch: %w", seedErr) - } - err = tx.QueryRowContext(ctx, "SELECT epoch FROM store_epoch WHERE id = 1").Scan(&epoch) - } - if err != nil { - return 0, fmt.Errorf("versioned history: read store epoch: %w", err) - } - return epoch, nil -} diff --git a/internal/storage/issueops/version_history_durable_state_test.go b/internal/storage/issueops/version_history_durable_state_test.go deleted file mode 100644 index c800ede2c..000000000 --- a/internal/storage/issueops/version_history_durable_state_test.go +++ /dev/null @@ -1,284 +0,0 @@ -package issueops - -import ( - "bytes" - "crypto/sha256" - "encoding/json" - "os" - "path/filepath" - "strings" - "testing" -) - -// TestCanonicalDurableStateIsJCS pins that canonicalDurableState -- the one -// helper RecordVersionInTx stores through -- emits the RFC 8785 (JCS) form, -// not encoding/json's, and that the difference is real: the same value -// marshaled without the JCS step yields different bytes. -// -// Why this matters (donnabox on gastownhall/beads#6358 item 4): the token -// the design derives from durable_state (#5898, sha256-jcs) is only stable if -// the bytes hashed are the bytes stored. Migration 0068 step 7 makes the -// column a LONGBLOB so storage keeps bytes verbatim; this test covers the -// other half, that the writer's bytes are the canonical ones in the first -// place, so the token is a function of content and not of whatever -// encoding/json or a caller-supplied json.Number happened to emit. -// -// The number inputs use json.Number and json.RawMessage deliberately: they -// are the only way to get encoding/json to emit a non-canonical number form -// such as 1.0 or 1E300 verbatim (a float64 1.0 already marshals as 1), which -// is exactly the input a JSON column would have renormalized on its own. -// -// Numbers are one axis. The rest of the pin is table-driven: the RFC 8785 -// test vectors that ship with gowebpki/jcs v1.0.1 (testdata/jcs, see its -// README.md), a key-ordering case the vectors leave implicit (RFC 8785 -// sorts keys by UTF-16 code units, so an astral-plane key sorts AFTER "é" -// even though its first byte is smaller), and the one place encoding/json -// and RFC 8785 disagree for ordinary issue text: encoding/json escapes <, > -// and & as \u003c-style sequences and JCS does not, so the stored bytes must -// be the JCS form or the token would depend on which marshaler ran first. -// Every case is also checked to be a fixed point of canonicalization, and a -// duplicate-key input is refused rather than silently resolved. -// -// THE OFFICIAL VECTORS RUN BELOW THE ADMISSION GATE, through jcsCanonicalize -// rather than canonicalDurableState, and this is deliberate, not an -// inconsistency: testdata/jcs/input/values.json -- from the spec's own -// reference suite, not a case authored for this repo -- contains 1E30, an -// integer-valued literal past 2^53-1 that refuseUnrepresentableIntegers now -// refuses outright (see version_history.go's refuseUnrepresentableIntegers -// and jcsCanonicalize doc comments). RFC 8785 conformance is a claim about -// the canonicalizer, not about this store's admission policy, so weakening -// or dropping that vector to fit the gate would be the wrong trade; the gate -// refusing the same magnitude is pinned on its own terms in -// version_history_integer_admission_test.go. The two local cases below carry -// no such conflict and keep exercising the full gated seam, which is the -// more faithful check for them since production traffic never bypasses the -// gate. -func TestCanonicalDurableStateIsJCS(t *testing.T) { - t.Parallel() - - for _, tc := range jcsTestVectors(t) { - t.Run(tc.name, func(t *testing.T) { - t.Parallel() - marshaled, err := json.Marshal(tc.state) - if err != nil { - t.Fatalf("json.Marshal: %v", err) - } - got, err := jcsCanonicalize(marshaled) - if err != nil { - t.Fatalf("jcsCanonicalize: %v", err) - } - if !bytes.Equal(got, tc.want) { - t.Fatalf("jcsCanonicalize =\n %s\nwant\n %s", got, tc.want) - } - // canonical(canonical(x)) == canonical(x): the stored bytes are - // a fixed point, so re-canonicalizing what was read back can - // never change the token. - again, err := jcsCanonicalize(got) - if err != nil { - t.Fatalf("jcsCanonicalize over its own output: %v", err) - } - if !bytes.Equal(again, got) { - t.Fatalf("canonical form is not a fixed point:\n %s\n %s", got, again) - } - }) - } - - for _, tc := range []jcsCase{ - { - name: "unicode and astral keys sort by UTF-16 code units", - state: json.RawMessage(`{"b":1,"a":2,"é":3,"A":4,"😀":5}`), - want: []byte(`{"A":4,"a":2,"b":1,"é":3,"😀":5}`), - }, - { - name: "html escapes are normalized away", - state: map[string]any{"s": `&"'`}, - want: []byte(`{"s":"&\"'"}`), - }, - } { - t.Run(tc.name, func(t *testing.T) { - t.Parallel() - got, err := canonicalDurableState(tc.state) - if err != nil { - t.Fatalf("canonicalDurableState: %v", err) - } - if !bytes.Equal(got, tc.want) { - t.Fatalf("canonicalDurableState =\n %s\nwant\n %s", got, tc.want) - } - // canonical(canonical(x)) == canonical(x): the stored bytes are - // a fixed point, so re-canonicalizing what was read back can - // never change the token. - again, err := canonicalDurableState(json.RawMessage(got)) - if err != nil { - t.Fatalf("canonicalDurableState over its own output: %v", err) - } - if !bytes.Equal(again, got) { - t.Fatalf("canonical form is not a fixed point:\n %s\n %s", got, again) - } - }) - } - - t.Run("plain marshal html-escapes and the canonical form does not", func(t *testing.T) { - t.Parallel() - // The normalization above is doing work: the plain marshal of the - // same value carries encoding/json's HTML-safe escapes, which RFC - // 8785 does not permit (only the escapes it requires survive). - plain, err := json.Marshal(map[string]any{"s": `&"'`}) - if err != nil { - t.Fatalf("json.Marshal: %v", err) - } - if !bytes.Contains(plain, []byte(`\u003c`)) || !bytes.Contains(plain, []byte(`\u0026`)) { - t.Fatalf("plain json.Marshal no longer HTML-escapes (%s); the fixture no longer exercises the normalization", plain) - } - got, err := canonicalDurableState(json.RawMessage(plain)) - if err != nil { - t.Fatalf("canonicalDurableState: %v", err) - } - if bytes.Contains(got, []byte(`\u00`)) { - t.Fatalf("canonical form still carries a \\u00xx escape: %s", got) - } - }) - - t.Run("duplicate keys are rejected", func(t *testing.T) { - t.Parallel() - // RFC 8785 has no answer for a duplicate key (section 3.1 forbids - // them), and encoding/json's compaction lets the input through, so - // the JCS step is the only place a snapshot with two "k"s can be - // refused instead of stored with whichever value happened to win. - _, err := canonicalDurableState(json.RawMessage(`{"k":1,"k":2}`)) - if err == nil { - t.Fatal("canonicalDurableState(duplicate keys) = nil error, want a refusal") - } - if !strings.Contains(err.Error(), "Duplicate key") { - t.Fatalf("canonicalDurableState(duplicate keys) error = %q, want it to name the duplicate key", err) - } - }) - - // Issue-shaped: string id, nested object, list, null, and the number - // forms that Dolt's JSON type is known to renormalize. - state := map[string]any{ - "id": "bd-1", - "priority": json.Number("1.0"), - "metadata": map[string]any{ - "weight": json.Number("1.50"), - // 1E-300, not 1E300: this test is about number FORM - // canonicalization (ES6 exponential notation), not the admission - // gate. 1E300 denotes an INTEGER value (10^300) past what - // binary64 holds exactly, so refuseUnrepresentableIntegers now - // refuses it -- see version_history_integer_admission_test.go. - // 1E-300 exercises the same exponential FORM without being an - // integer at all, so it stays in scope for this test. - "huge": json.Number("1E-300"), - // 2^53-1, the largest integer binary64 holds exactly. This was - // 2^53+1 until the admission gate began refusing literals that - // binary64 cannot hold: JCS would round such a literal to a - // DIFFERENT VALUE, collapsing two distinct states onto one - // durable_state. That case now lives in - // version_history_integer_admission_test.go, which asserts the - // refusal; this test stays about number FORM canonicalization. - "ordinal": json.Number("9007199254740991"), - "tags": []any{json.Number("2.0"), "b", nil}, - }, - "raw": json.RawMessage(`{ "z" : 10.0e0 , "a" : true }`), - } - - got, err := canonicalDurableState(state) - if err != nil { - t.Fatalf("canonicalDurableState: %v", err) - } - - // RFC 8785: keys sorted, no whitespace, numbers in ES6 Number::toString - // form (1.0 -> 1, 1.50 -> 1.5, 1E-300 -> 1e-300, 10.0e0 -> 10). An - // integer-valued literal past 2^53-1, any spelling, never reaches JCS now - // -- the admission gate refuses it first. - want := []byte(`{"id":"bd-1","metadata":{"huge":1e-300,"ordinal":9007199254740991,"tags":[2,"b",null],"weight":1.5},"priority":1,"raw":{"a":true,"z":10}}`) - if !bytes.Equal(got, want) { - t.Fatalf("canonicalDurableState =\n %s\nwant\n %s", got, want) - } - - // The JCS step is doing work: a plain marshal of the same value keeps - // the caller's number forms and spacing, so its bytes differ. - plain, err := json.Marshal(state) - if err != nil { - t.Fatalf("json.Marshal: %v", err) - } - if bytes.Equal(plain, got) { - t.Fatalf("plain json.Marshal already equals the canonical form (%s); this test no longer proves canonicalization matters", plain) - } - for _, nonCanonical := range []string{"1.0", "1.50", "1E-300", "10.0e0"} { - if !bytes.Contains(plain, []byte(nonCanonical)) { - t.Errorf("plain marshal lost the non-canonical form %q, so the fixture no longer exercises it: %s", nonCanonical, plain) - } - if bytes.Contains(got, []byte(nonCanonical)) { - t.Errorf("canonical form still carries the non-canonical form %q: %s", nonCanonical, got) - } - } - - // Determinism: the token is a function of the content. A second pass - // over the same value, and over the already-canonical bytes, hashes - // identically. - again, err := canonicalDurableState(state) - if err != nil { - t.Fatalf("canonicalDurableState (second pass): %v", err) - } - if sha256.Sum256(again) != sha256.Sum256(got) { - t.Fatalf("canonicalDurableState is not deterministic:\n %s\n %s", got, again) - } - idempotent, err := canonicalDurableState(json.RawMessage(got)) - if err != nil { - t.Fatalf("canonicalDurableState over its own output: %v", err) - } - if !bytes.Equal(idempotent, got) { - t.Fatalf("canonical form is not a fixed point:\n %s\n %s", got, idempotent) - } -} - -// jcsCase is one canonicalization pin: what RecordVersionInTx would hand to -// canonicalDurableState, and the exact bytes RFC 8785 says come out. -type jcsCase struct { - name string - state any - want []byte -} - -// jcsTestVectors loads the RFC 8785 input/expected pairs under testdata/jcs -// (provenance in its README.md; the upstream "output" directory is named -// "expected" here because the repository's .gitignore drops any output/ -// tree). The count is pinned so an emptied or half-copied directory cannot -// pass as a vacuous table. -func jcsTestVectors(t *testing.T) []jcsCase { - t.Helper() - const shipped = 10 // the vectors in gowebpki/jcs v1.0.1's testdata - dir := filepath.Join("testdata", "jcs") - entries, err := os.ReadDir(filepath.Join(dir, "input")) - if err != nil { - t.Fatalf("list RFC 8785 vectors: %v", err) - } - var cases []jcsCase - for _, entry := range entries { - input, err := os.ReadFile(filepath.Join(dir, "input", entry.Name())) - if err != nil { - t.Fatalf("read RFC 8785 vector input: %v", err) - } - want, err := os.ReadFile(filepath.Join(dir, "expected", entry.Name())) - if err != nil { - t.Fatalf("read RFC 8785 vector output: %v", err) - } - cases = append(cases, jcsCase{name: "rfc8785 vector " + entry.Name(), state: json.RawMessage(input), want: want}) - } - if len(cases) != shipped { - t.Fatalf("found %d RFC 8785 vectors under %s, want the %d that ship with gowebpki/jcs v1.0.1", len(cases), dir, shipped) - } - return cases -} - -// TestCanonicalDurableStateRejectsUnmarshalableState pins that a value -// encoding/json cannot marshal surfaces as an error rather than as an empty -// or partial snapshot, so RecordVersionInTx fails the transaction instead of -// minting a version row whose durable_state says nothing. -func TestCanonicalDurableStateRejectsUnmarshalableState(t *testing.T) { - t.Parallel() - - if _, err := canonicalDurableState(map[string]any{"ch": make(chan int)}); err == nil { - t.Fatal("canonicalDurableState(unmarshalable) = nil error, want an error") - } -} diff --git a/internal/storage/issueops/version_history_integer_admission_test.go b/internal/storage/issueops/version_history_integer_admission_test.go deleted file mode 100644 index 9966afa88..000000000 --- a/internal/storage/issueops/version_history_integer_admission_test.go +++ /dev/null @@ -1,186 +0,0 @@ -package issueops - -import ( - "encoding/json" - "errors" - "testing" - - "github.com/steveyegge/beads/internal/types" -) - -func issueWithMetadata(meta string) types.Issue { - return types.Issue{ID: "be-1", Title: "t", Metadata: json.RawMessage(meta)} -} - -// A metadata integer past 2^53 used to be ROUNDED by jcs.Transform, which -// collapsed two distinct issue states onto one durable_state and therefore -// one content token. It must be refused instead, so the loss is loud. -func TestCanonicalDurableStateRefusesIntegerPastBinary64(t *testing.T) { - _, err := canonicalDurableState(issueWithMetadata(`{"n":9007199254740993}`)) - if err == nil { - t.Fatal("canonicalDurableState(metadata 9007199254740993) = nil error, want a refusal") - } - if !errors.Is(err, ErrIntegerNotRepresentable) { - t.Fatalf("error = %v, want ErrIntegerNotRepresentable", err) - } -} - -// The defect this admission gate exists to prevent, stated directly: two -// DISTINCT states must never render to the same bytes. -func TestCanonicalDurableStateNeverCollapsesTwoDistinctStates(t *testing.T) { - // Before the admission gate, BOTH of these canonicalized, and both to - // the same bytes: 2^53+1 rounded down onto 2^53. One content token for - // two live states. Now the unrepresentable side is refused, so the pair - // can never be confused for one another. - a, errA := canonicalDurableState(issueWithMetadata(`{"n":9007199254740993}`)) - b, errB := canonicalDurableState(issueWithMetadata(`{"n":9007199254740991}`)) - if errA == nil && errB == nil && string(a) == string(b) { - t.Fatalf("two distinct states produced identical durable_state %s", a) - } - if errA == nil { - t.Fatalf("the unrepresentable state canonicalized instead of being refused: %s", a) - } - if errB != nil { - t.Fatalf("the representable state was refused: %v", errB) - } -} - -// The bound itself, both sides of it, bare-integer spelling. -func TestCanonicalDurableStateIntegerBoundary(t *testing.T) { - for _, tc := range []struct { - name string - meta string - refused bool - }{ - {"2^53-1 accepted", `{"n":9007199254740991}`, false}, - {"2^53 refused", `{"n":9007199254740992}`, true}, - {"2^53+1 refused", `{"n":9007199254740993}`, true}, - {"negative past bound refused", `{"n":-9007199254740993}`, true}, - {"beyond int64 refused", `{"n":18446744073709551616}`, true}, - } { - t.Run(tc.name, func(t *testing.T) { - _, err := canonicalDurableState(issueWithMetadata(tc.meta)) - if tc.refused && err == nil { - t.Fatalf("%s: nil error, want refusal", tc.meta) - } - if !tc.refused && err != nil { - t.Fatalf("%s: %v, want acceptance", tc.meta, err) - } - }) - } -} - -// Ordinary fractions must keep working: they are not exactly representable -// either, but ES6 shortest-round-trip returns them to the same binary64 -// value, so they neither round to a different value nor collide. -// -// 1e300 is deliberately NOT in this list: it is a mathematical integer -// (10^300) whose magnitude vastly exceeds 2^53-1, so it is refused like any -// other unrepresentable integer, regardless of spelling. See -// TestCanonicalDurableStateRefusesHugeExponentInteger below. -func TestCanonicalDurableStateStillAcceptsOrdinaryDecimals(t *testing.T) { - for _, meta := range []string{`{"n":0.1}`, `{"n":1.0}`, `{"n":-2.5}`, `{"n":0}`} { - if _, err := canonicalDurableState(issueWithMetadata(meta)); err != nil { - t.Fatalf("canonicalDurableState(%s) = %v, want acceptance", meta, err) - } - } -} - -// 1e300 denotes the integer 10^300 at a magnitude vastly past 2^53-1, and -// is subject to the same collision defect this gate -// exists to close -- binary64 spacing near 10^300 is roughly 2^248, so a wide -// range of distinct large integers near 10^300 collapse onto that one -// double, same failure mode as the 2^53 boundary, larger window. The -// spec for this predicate is "value not form, any spelling, no exceptions" -// (see version_history.go's refuseUnrepresentableIntegers doc comment), -// applied literally that refuses 1e300 -- but two pre-gate tests assumed it -// stayed accepted, one of which lived right here until this admission gate -// (see version_history_durable_state_test.go's "huge" fixture, moved off -// 1E300 for the same reason). -// -// RESOLVED: REFUSE 1e300. The rule applies -// literally, with no notation carve-out -- admitting a spelling this large -// while refusing 9007199254740993 would be indefensible, since the collision -// window near 10^300 (binary64 spacing ~2^248) is vastly larger than the one -// at the 2^53 boundary this gate was built to close. This is final, not -// provisional. -func TestCanonicalDurableStateRefusesHugeExponentInteger(t *testing.T) { - _, err := canonicalDurableState(issueWithMetadata(`{"n":1e300}`)) - if err == nil { - t.Fatal("canonicalDurableState(metadata 1e300) = nil error, want a refusal") - } - if !errors.Is(err, ErrIntegerNotRepresentable) { - t.Fatalf("error = %v, want ErrIntegerNotRepresentable", err) - } -} - -// THE GAP QA FOUND IN THE FIRST CUT OF THIS GATE. The first patch's -// refuseUnrepresentableIntegers skipped any literal containing '.', 'e', or -// 'E' outright, reasoning that non-integer literals round-trip losslessly -// through ES6 shortest-round-trip. That reasoning covers genuinely -// fractional literals (0.1, 1.5) but not integer-VALUED literals written in -// decimal or exponential FORM: 9007199254740993.0, 9.007199254740993e15, and -// 90071992547409930e-1 all denote the same integer VALUE as the bare literal -// 9007199254740993, and all three walked straight past the first gate and -// canonicalized to the identical colliding target 9007199254740992 -- the -// exact defect class this gate exists to close, just reachable through -// spelling instead of magnitude. -// -// The predicate under test must inspect the VALUE (the binary64 nearest -// to the literal) rather than the literal's spelling, so it must -// refuse and accept the same values regardless of which of the three JSON -// number forms (integer, decimal, exponent) carries them. This is pinned -// here for the bound itself; ordinary small decimals are covered separately -// by TestCanonicalDurableStateStillAcceptsOrdinaryDecimals. -func TestCanonicalDurableStateIntegerBoundaryAllSpellings(t *testing.T) { - for _, tc := range []struct { - name string - lit string - refused bool - }{ - // at the bound (2^53-1 = 9007199254740991): every spelling accepted. - {"at bound, bare", `9007199254740991`, false}, - {"at bound, decimal", `9007199254740991.0`, false}, - {"at bound, exponent", `9.007199254740991e15`, false}, - - // one past the bound (2^53+1 = 9007199254740993): every spelling - // refused. The decimal and exponent rows are the QA-found gap. - {"past bound, bare", `9007199254740993`, true}, - {"past bound, decimal (THE GAP)", `9007199254740993.0`, true}, - {"past bound, exponent (THE GAP)", `9.007199254740993e15`, true}, - {"past bound, exponent negative-exp form (THE GAP)", `90071992547409930e-1`, true}, - } { - t.Run(tc.name, func(t *testing.T) { - meta := `{"n":` + tc.lit + `}` - _, err := canonicalDurableState(issueWithMetadata(meta)) - if tc.refused && err == nil { - t.Fatalf("metadata %s: nil error, want refusal", meta) - } - if !tc.refused && err != nil { - t.Fatalf("metadata %s: %v, want acceptance", meta, err) - } - if tc.refused && !errors.Is(err, ErrIntegerNotRepresentable) { - t.Fatalf("metadata %s: error = %v, want ErrIntegerNotRepresentable", meta, err) - } - }) - } -} - -// Same property as TestCanonicalDurableStateNeverCollapsesTwoDistinctStates, -// spelled in decimal form -- the exact QA-found gap stated as a distinctness -// property rather than a table row: two states differing only in a -// decimal-spelled metadata integer past 2^53 must never render to the same -// bytes. -func TestCanonicalDurableStateNeverCollapsesDecimalSpellingOfDistinctStates(t *testing.T) { - a, errA := canonicalDurableState(issueWithMetadata(`{"n":9007199254740993.0}`)) - b, errB := canonicalDurableState(issueWithMetadata(`{"n":9007199254740991.0}`)) - if errA == nil && errB == nil && string(a) == string(b) { - t.Fatalf("two distinct states produced identical durable_state %s", a) - } - if errA == nil { - t.Fatalf("the unrepresentable state (9007199254740993.0) canonicalized instead of being refused: %s", a) - } - if errB != nil { - t.Fatalf("the representable state (9007199254740991.0) was refused: %v", errB) - } -} diff --git a/internal/storage/issueops/version_history_number_admission_test.go b/internal/storage/issueops/version_history_number_admission_test.go deleted file mode 100644 index f68193aa9..000000000 --- a/internal/storage/issueops/version_history_number_admission_test.go +++ /dev/null @@ -1,298 +0,0 @@ -package issueops - -import ( - "errors" - "math" - "math/rand" - "runtime" - "strconv" - "strings" - "testing" -) - -// admitNumber runs the admission gate over one JSON number literal in the -// position the mint meets it: an object member of an issue's metadata. -func admitNumber(lit string) error { - return refuseUnrepresentableIntegers([]byte(`{"n":` + lit + `}`)) -} - -// The admission rule, stated once for the tests below: a JSON number literal -// is admitted into a durable_state if and only if the binary64 nearest to it -// has magnitude at most 2^53-1, whatever the literal's spelling (integer, -// decimal or exponent), and a literal that overflows binary64 is refused. -// Ordinary fractions such as 0.1 stay admitted, and so do magnitudes too small -// for binary64, which round to zero. - -// A fraction whose nearest double lies at or beyond 2^53 used to be admitted: -// the old rule looked only at literals that denote an integer VALUE, and -// 9007199254740993.5 is not one. jcs.Transform then rounds it to the integer -// 9007199254740994, so two distinct issue states shared one durable_state, -// which is exactly what the gate exists to prevent. Refusing must depend on -// where the nearest double falls, not on whether the literal looks integral. -func TestNumberAdmissionRefusesFractionsWhoseNearestDoubleIsPastTheBound(t *testing.T) { - for _, tc := range []struct { - lit string - refused bool - why string - }{ - {"9007199254740993.5", true, "nearest double is 2^53+2"}, - {"9007199254740994.5", true, "nearest double is 2^53+2"}, - {"9007199254740991.5", true, "a tie between 2^53-1 and 2^53 that rounds to the even 2^53"}, - {"9007199254740991.75", true, "nearest double is 2^53"}, - {"-9007199254740993.5", true, "magnitude decides, not sign"}, - {"9007199254740990.5", false, "a tie that rounds to the even 2^53-2, inside the range"}, - {"9007199254740991.25", false, "nearest double is 2^53-1, inside the range"}, - {"9007199254740991.0", false, "exactly 2^53-1"}, - } { - t.Run(tc.lit, func(t *testing.T) { - err := admitNumber(tc.lit) - if tc.refused { - if !errors.Is(err, ErrIntegerNotRepresentable) { - t.Fatalf("admitNumber(%s) = %v, want ErrIntegerNotRepresentable (%s)", tc.lit, err, tc.why) - } - if _, mintErr := canonicalDurableState(issueWithMetadata(`{"n":` + tc.lit + `}`)); !errors.Is(mintErr, ErrIntegerNotRepresentable) { - t.Fatalf("canonicalDurableState(metadata %s) = %v, want ErrIntegerNotRepresentable", tc.lit, mintErr) - } - return - } - if err != nil { - t.Fatalf("admitNumber(%s) = %v, want acceptance (%s)", tc.lit, err, tc.why) - } - }) - } -} - -// numberAdmissionCorpus is every literal the closure property is checked -// over: the boundary cases the rule was written around, every spelling of the -// values on either side of 2^53, and pseudo-random doubles in three renderings. -// The seed is fixed so a failure names a literal that reproduces. -func numberAdmissionCorpus() []string { - corpus := []string{ - "0", "-0", "0.0", "-0.0", "1", "1.0", "-2.5", "0.1", "3.14159265358979323846", "0.30000000000000004", - "5e-324", "4.9e-324", "2.2250738585072014e-308", "1e-7", "1e-400", "1e-100000000", - "9007199254740991", "9007199254740992", "9007199254740993", "9.007199254740991e15", "90071992547409930e-1", - "9007199254740993.5", "9007199254740994.5", "9007199254740991.5", "9007199254740990.5", - "1727000000000000000", "1727000000000000123", "1e21", "1e300", "1.7976931348623157e308", - "123456789012345678901234567890", "18446744073709551616", - } - // Every spelling of the integers and near-integers either side of the bound. - for delta := int64(-6); delta <= 6; delta++ { - base := strconv.FormatInt(int64(1)<<53+delta, 10) - for _, frac := range []string{"", ".0", ".25", ".5", ".75", ".999999"} { - corpus = append(corpus, base+frac, "-"+base+frac) - } - } - rng := rand.New(rand.NewSource(20260930)) - for _, scale := range []float64{1, 1e3, 1e9, 1e15, 1e-9} { - for range 60 { - f := (rng.Float64()*2 - 1) * scale - corpus = append(corpus, - strconv.FormatFloat(f, 'g', -1, 64), - strconv.FormatFloat(f, 'e', 17, 64), - strconv.FormatFloat(f, 'f', -1, 64)) - } - } - for range 200 { - f := math.Float64frombits(rng.Uint64()) - if math.IsNaN(f) || math.IsInf(f, 0) { - continue - } - corpus = append(corpus, strconv.FormatFloat(f, 'g', -1, 64), strconv.FormatFloat(f, 'e', -1, 64)) - } - return corpus -} - -// The gate must be closed under its own canonicalization: whatever it admits -// canonicalizes to a form it also admits. Otherwise a value can pass at the -// door and become unmintable one step later, which is what 9007199254740991.5 -// did (it rounds to 2^53, and the canonical bytes are then refused). Checked as -// a property over a corpus, not by example. -func TestNumberAdmissionIsClosedUnderCanonicalization(t *testing.T) { - admitted := 0 - for _, lit := range numberAdmissionCorpus() { - doc := []byte(`{"n":` + lit + `}`) - if err := refuseUnrepresentableIntegers(doc); err != nil { - continue // refused literals are outside the property - } - admitted++ - canonical, err := jcsCanonicalize(doc) - if err != nil { - t.Errorf("literal %s: jcsCanonicalize: %v", lit, err) - continue - } - if err := refuseUnrepresentableIntegers(canonical); err != nil { - t.Errorf("literal %s is admitted but its canonical form %s is refused: %v", lit, canonical, err) - } - } - if admitted < 100 { - t.Fatalf("only %d corpus literals were admitted; the property is close to vacuous", admitted) - } -} - -// The classification must not depend on how large an exponent the literal -// spells. The old rule parsed every literal as an exact rational, so a huge -// exponent either cost a million-digit integer or fell outside what the -// parser accepts and came back as an invalid literal, a different class of -// error from the refusal it is. Overflow is a refusal; a magnitude too small -// for binary64 is admitted. -func TestNumberAdmissionClassifiesByMagnitudeWhateverTheExponent(t *testing.T) { - for _, tc := range []struct { - lit string - refused bool - }{ - {"1e100000000", true}, - {"1e1000000", true}, - {"1e309", true}, - {"-1e400", true}, - {"1.7976931348623159e308", true}, - {"1e-100000000", false}, - {"1e-400", false}, - {"-1e-400", false}, - {"5e-324", false}, - } { - t.Run(tc.lit, func(t *testing.T) { - err := admitNumber(tc.lit) - switch { - case tc.refused && !errors.Is(err, ErrIntegerNotRepresentable): - t.Fatalf("admitNumber(%s) = %v, want ErrIntegerNotRepresentable", tc.lit, err) - case !tc.refused && err != nil: - t.Fatalf("admitNumber(%s) = %v, want acceptance", tc.lit, err) - } - }) - } -} - -// The gate allocates nothing proportional to an exponent. Measured as an -// allocation count and never as wall time, so a loaded machine cannot flake it, -// and it is what distinguishes a classifier that builds a million-digit integer -// from one that does not. -// -// The count is exact only without the race detector. Under it sync.Pool drops -// one Put in four at random (sync/pool.go), and the refusal builds its error with -// fmt.Errorf, whose printer comes from a pool, so a call now and then allocates a -// printer the pool would have supplied: a literal that costs 22 allocations reads -// 23, or 24, on a run where a printer was dropped, and comparing two -// independently averaged samples with a strict > is a coin flip. That noise only -// ever ADDS allocations, so the fewest allocations over many single-call samples -// is the count without it, and it is the same on every run. The comparison stays -// strict, with no margin to tune, and it loses no sensitivity: a classifier that -// builds per-exponent state does so on every call, so it raises the fewest too. -func TestNumberAdmissionAllocationsDoNotGrowWithTheExponent(t *testing.T) { - // The chance that every one of these samples is noisy is under one in 10^19. - const samples = 32 - fewest := func(lit string) float64 { - doc := []byte(`{"n":` + lit + `}`) - least := math.Inf(1) - for range samples { - least = math.Min(least, testing.AllocsPerRun(1, func() { _ = refuseUnrepresentableIntegers(doc) })) - } - return least - } - baseline := fewest("1e400") - for _, lit := range []string{"1e1000000", "1e100000000", "-1e1000000"} { - if got := fewest(lit); got > baseline { - t.Errorf("%s allocates %v times per call against %v for 1e400: allocation grows with the exponent", lit, got, baseline) - } - } -} - -// TestNumberAdmissionBytesDoNotGrowWithTheExponent is the count bound's twin, and finding 6 -// of the #6661 review. The count bound passes a classifier that builds the number it is -// asked about: one big-integer allocation is one allocation whether it holds 400 bits or a -// hundred million. What grows with the exponent is then the size of the allocation, so this -// bounds bytes: the fewest bytes one call allocates for a literal with a huge exponent is -// no more than for a small one, plus a margin and a few bytes for each byte of literal (a -// refusal names the literal in its message). -// -// The fewest over many single-call samples, as in the count bound and for the same reason: -// whatever else allocates while a sample runs only ever ADDS bytes, so the minimum is the -// cost of the call itself. TotalAlloc is read before and after each call, with GOMAXPROCS -// pinned to one for the measurement as testing.AllocsPerRun does, so a background -// goroutine does not add to a sample. -func TestNumberAdmissionBytesDoNotGrowWithTheExponent(t *testing.T) { - const ( - samples = 32 - smallLiteral = "1e400" - // What the bound allows beyond the small literal's bytes: a margin, and a few - // bytes for each byte a longer literal adds to the refusal's message. - slackBytes = 1024 - perLitByte = 16 - ) - defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(1)) - fewest := func(lit string) uint64 { - doc := []byte(`{"n":` + lit + `}`) - least := uint64(math.MaxUint64) - var before, after runtime.MemStats - for range samples { - runtime.ReadMemStats(&before) - _ = refuseUnrepresentableIntegers(doc) - runtime.ReadMemStats(&after) - least = min(least, after.TotalAlloc-before.TotalAlloc) - } - return least - } - baseline := fewest(smallLiteral) - for _, lit := range []string{"1e1000000", "1e100000000", "-1e1000000"} { - limit := baseline + slackBytes + perLitByte*uint64(max(len(lit)-len(smallLiteral), 0)) - if got := fewest(lit); got > limit { - t.Errorf("%s allocates %d bytes per call against %d for %s (limit %d): bytes grow with the exponent", lit, got, baseline, smallLiteral, limit) - } - } -} - -// The sentinel names the range it enforces. Its old wording said a literal -// was not exactly representable, which no longer describes what is refused: -// 9007199254740993.5 is a fraction, and the rule is about magnitude. The -// identifier keeps its historical name so nothing that matches it with -// errors.Is has to change; the text is what a person reads. -func TestIntegerNotRepresentableTextNamesTheIJSONRange(t *testing.T) { - const want = "number is outside the I-JSON exact-integer range (magnitude above 2^53-1)" - got := ErrIntegerNotRepresentable.Error() - if got != want { - t.Fatalf("ErrIntegerNotRepresentable.Error() = %q, want %q", got, want) - } - if !strings.Contains(got, "I-JSON") { - t.Errorf("sentinel text %q does not name I-JSON", got) - } - if strings.Contains(got, "cannot represent") { - t.Errorf("sentinel text %q still uses the old wording", got) - } - err := admitNumber("9007199254740993") - if err == nil || err.Error() != want+": 9007199254740993" { - t.Fatalf("refusal text = %v, want the sentinel followed by the offending literal", err) - } -} - -// Nothing the store already accepted becomes admitted or refused differently. -// Dolt does not hand a literal back the way it was written: an integer up to -// int64 is stored exactly and every other number is stored as a double, so what -// the mint sees is the right-hand column. Each row pins the verdict on that -// column, which is what production meets. The verdicts are the ones the rule -// had before it changed, so this stays green through the change and fails only -// if the new rule moves one. -func TestNumberAdmissionKeepsTheVerdictOnEveryLiteralAsTheStoreReturnsIt(t *testing.T) { - for _, tc := range []struct { - written string - returned string // what a Dolt JSON column hands back for it - refused bool - }{ - {"9007199254740993", "9007199254740993", true}, - {"1727000000000000123", "1727000000000000123", true}, - {"9007199254740993.5", "9007199254740994", true}, - {"1.0", "1", false}, - {"0.1", "0.1", false}, - {"3.14159265358979323846", "3.141592653589793", false}, - {"1e300", "1" + strings.Repeat("0", 300), true}, - {"123456789012345678901234567890", "123456789012345680000000000000", true}, - } { - t.Run(tc.written, func(t *testing.T) { - err := admitNumber(tc.returned) - if tc.refused && !errors.Is(err, ErrIntegerNotRepresentable) { - t.Fatalf("admitNumber(%s), the store's rendering of %s = %v, want a refusal", tc.returned, tc.written, err) - } - if !tc.refused && err != nil { - t.Fatalf("admitNumber(%s), the store's rendering of %s = %v, want acceptance", tc.returned, tc.written, err) - } - }) - } -} diff --git a/internal/storage/issueops/version_history_preenable_scan_test.go b/internal/storage/issueops/version_history_preenable_scan_test.go deleted file mode 100644 index 74f57b903..000000000 --- a/internal/storage/issueops/version_history_preenable_scan_test.go +++ /dev/null @@ -1,680 +0,0 @@ -package issueops - -import ( - "context" - "database/sql/driver" - "encoding/json" - "errors" - "fmt" - "go/ast" - "go/parser" - "go/token" - "io/fs" - "reflect" - "regexp" - "sort" - "strings" - "testing" - "time" - - "github.com/DATA-DOG/go-sqlmock" - - "github.com/steveyegge/beads/internal/storage/sqlbuild" - "github.com/steveyegge/beads/internal/types" -) - -// With versioned history on, the mint runs in the same transaction as the -// mutation that reaches it, so refusing at the mint IS refusing the write: a -// number outside the I-JSON range aborts the writer's own transaction and -// nothing it did is committed. The mint reads the whole issue, so the number -// can be in any field that holds one: in metadata, or in a gate's timeout, -// which encoding/json writes as a count of nanoseconds. The mint's reads run -// first and its two writes, the version row and the current_revision advance, -// come last, so a refusal must arrive after the reads and before either write. -// -// The mock scripts exactly the reads. It expects no INSERT into issue_versions -// and no UPDATE of issues.current_revision, so a mint that wrote before it -// refused would make the mock fail the call with an unexpected-statement error -// instead of the refusal asserted here. The caller's own rollback closes the -// transaction, as every store does when the mint returns an error. -// -// The refusal names the top-level field the number was found in, as well as the -// literal, so a person who reads it knows which part of the issue to fix. -func TestRecordVersionInTxRefusesANumberOutsideTheRangeAndWritesNothing(t *testing.T) { - for _, tc := range []struct { - name string - column string - value driver.Value - literal string - field string - }{ - {"a number in metadata", "metadata", `{"ts":1727000000000000000}`, "1727000000000000000", "metadata"}, - {"a gate timeout past 2^53-1 nanoseconds", "timeout_ns", int64(2600 * time.Hour), "9360000000000000", "timeout"}, - } { - t.Run(tc.name, func(t *testing.T) { - const id = "bd-9" - _, mock, tx := beginMockTx(t) - defer ScopeVersionedHistoryTransaction(tx, true)() - - values := issueRowValues(id, "carries an out-of-range number") - for i, col := range issueColumns() { - if col == tc.column { - values[i] = tc.value - } - } - mock.ExpectQuery(regexp.QuoteMeta("SELECT " + IssueSelectColumns + " FROM issues " + sqlbuild.LeaseJoin("issues") + " WHERE id = ?")). - WithArgs(id). - WillReturnRows(issueRows().AddRow(values...)) - mock.ExpectQuery(regexp.QuoteMeta("SELECT label FROM labels WHERE issue_id = ? ORDER BY label")). - WithArgs(id). - WillReturnRows(sqlmock.NewRows([]string{"label"})) - // design §16.2b's write fence reads participation_generation right after the - // snapshot. A non-NULL value is a participating row, so the mint goes on to the - // refusal this test pins; a NULL one would be a legacy row and mint nothing. - mock.ExpectQuery(regexp.QuoteMeta("SELECT participation_generation FROM issues WHERE id = ?")). - WithArgs(id). - WillReturnRows(sqlmock.NewRows([]string{"participation_generation"}).AddRow(1)) - mock.ExpectQuery(regexp.QuoteMeta("SELECT 1 FROM wisps LIMIT 1")). - WillReturnRows(sqlmock.NewRows([]string{"1"})) - mock.ExpectQuery(`FROM dependencies WHERE issue_id IN`). - WithArgs(id). - WillReturnRows(sqlmock.NewRows([]string{"issue_id", "depends_on_id", "type", "created_at", "created_by", "metadata", "thread_id"})) - mock.ExpectQuery(regexp.QuoteMeta("SELECT epoch FROM store_epoch WHERE id = 1")). - WillReturnRows(sqlmock.NewRows([]string{"epoch"}).AddRow(1)) - mock.ExpectQuery(regexp.QuoteMeta("SELECT COALESCE(MAX(revision), 0) + 1 FROM issue_versions WHERE issue_id = ?")). - WithArgs(id). - WillReturnRows(sqlmock.NewRows([]string{"next"}).AddRow(1)) - mock.ExpectRollback() - - err := RecordVersionInTx(context.Background(), tx, id, "actor") - if !errors.Is(err, ErrIntegerNotRepresentable) { - t.Fatalf("RecordVersionInTx = %v, want ErrIntegerNotRepresentable", err) - } - if !strings.Contains(err.Error(), id) { - t.Errorf("refusal %q does not name the issue %s", err, id) - } - if !strings.Contains(err.Error(), tc.literal) { - t.Errorf("refusal %q does not name the offending literal %s", err, tc.literal) - } - if !strings.Contains(err.Error(), fmt.Sprintf("%q", tc.field)) { - t.Errorf("refusal %q does not name the field %q the number was found in", err, tc.field) - } - if rbErr := tx.Rollback(); rbErr != nil { - t.Fatalf("rolling back the writer's transaction: %v", rbErr) - } - if err := mock.ExpectationsWereMet(); err != nil { - t.Fatalf("the mint read or wrote something other than the scripted reads: %v", err) - } - }) - } -} - -// A legacy row (participation_generation NULL) holding the same out-of-range number is outside -// the mint's refusal: design §16.2b's write fence returns right after the snapshot, so the -// admission gate never runs and RecordVersionInTx returns nil. -// -// The mock scripts the snapshot, the labels and the fence's read returning NULL, and nothing after -// it. A mint that read the dependencies, the epoch or a revision, or wrote, before it decided to -// skip would fail the call with an unexpected-statement error instead of returning nil, so this is -// the cheapest pin of the fence's order against the gate. -func TestRecordVersionInTxSkipsALegacyRowBeforeTheAdmissionGate(t *testing.T) { - const id = "bd-9" - _, mock, tx := beginMockTx(t) - defer ScopeVersionedHistoryTransaction(tx, true)() - - values := issueRowValues(id, "a legacy row that carries an out-of-range number") - for i, col := range issueColumns() { - if col == "metadata" { - values[i] = `{"ts":1727000000000000000}` - } - } - mock.ExpectQuery(regexp.QuoteMeta("SELECT " + IssueSelectColumns + " FROM issues " + sqlbuild.LeaseJoin("issues") + " WHERE id = ?")). - WithArgs(id). - WillReturnRows(issueRows().AddRow(values...)) - mock.ExpectQuery(regexp.QuoteMeta("SELECT label FROM labels WHERE issue_id = ? ORDER BY label")). - WithArgs(id). - WillReturnRows(sqlmock.NewRows([]string{"label"})) - mock.ExpectQuery(regexp.QuoteMeta("SELECT participation_generation FROM issues WHERE id = ?")). - WithArgs(id). - WillReturnRows(sqlmock.NewRows([]string{"participation_generation"}).AddRow(nil)) - mock.ExpectRollback() - - if err := RecordVersionInTx(context.Background(), tx, id, "actor"); err != nil { - t.Fatalf("RecordVersionInTx on a legacy row = %v, want nil: the fence skips it before the admission gate", err) - } - if rbErr := tx.Rollback(); rbErr != nil { - t.Fatalf("rolling back the writer's transaction: %v", rbErr) - } - if err := mock.ExpectationsWereMet(); err != nil { - t.Fatalf("the mint read or wrote something after the fence skipped the row: %v", err) - } -} - -// TestRecordVersionAtInTxKeepsThePlaneRuleButNotTheFence pins what the -// test-support mint (RecordVersionAtInTx, mintUnfenced) does on the paths #6943 -// gave the shared body. -// -// - A row the wisps table holds, flags clear: the plane rule is not the fence, -// so the unfenced mint honours it like the other two kinds. The issues-table -// read finds nothing and the mint returns nil having written nothing, where -// before it would have inserted a version row no issues row points at. -// - An issues-table row flagged NoHistory: never versioned, and the unfenced -// mint never declares participation, so it returns right after the snapshot, -// without reading or stamping participation_generation. -// - A legacy row (participation_generation NULL), flags clear: the fence does -// not apply, so it mints, and the closing UPDATE advances current_revision -// alone, with no stamp. -// -// The mock scripts each sequence exactly, so an extra read or any write fails -// the call with an unexpected-statement error. -func TestRecordVersionAtInTxKeepsThePlaneRuleButNotTheFence(t *testing.T) { - const id = "bd-7" - at := time.Date(2026, 9, 1, 12, 0, 0, 0, time.UTC) - issuesSnapshot := regexp.QuoteMeta("SELECT " + IssueSelectColumns + " FROM issues " + sqlbuild.LeaseJoin("issues") + " WHERE id = ?") - participation := regexp.QuoteMeta("SELECT participation_generation FROM issues WHERE id = ?") - - t.Run("a row the wisps table holds mints nothing", func(t *testing.T) { - _, mock, tx := beginMockTx(t) - mock.ExpectQuery(issuesSnapshot).WithArgs(id).WillReturnRows(issueRows()) - mock.ExpectQuery(regexp.QuoteMeta("SELECT " + IssueSelectColumns + " FROM wisps " + sqlbuild.LeaseJoin("wisps") + " WHERE id = ?")). - WithArgs(id). - WillReturnRows(issueRows().AddRow(issueRowValues(id, "a squashed wisp root, flags clear")...)) - mock.ExpectQuery(regexp.QuoteMeta("SELECT label FROM wisp_labels WHERE issue_id = ? ORDER BY label")). - WithArgs(id). - WillReturnRows(sqlmock.NewRows([]string{"label"})) - mock.ExpectQuery(participation).WithArgs(id).WillReturnRows(sqlmock.NewRows([]string{"participation_generation"})) - mock.ExpectRollback() - - if err := RecordVersionAtInTx(context.Background(), tx, id, "actor", at); err != nil { - t.Fatalf("RecordVersionAtInTx on a wisps-table row = %v, want nil", err) - } - if err := tx.Rollback(); err != nil { - t.Fatalf("rollback: %v", err) - } - if err := mock.ExpectationsWereMet(); err != nil { - t.Fatalf("the unfenced mint did something after finding no issues row: %v", err) - } - }) - - t.Run("an issues row flagged no-history is neither minted nor stamped", func(t *testing.T) { - _, mock, tx := beginMockTx(t) - values := issueRowValues(id, "no-history, on the issues plane") - for i, col := range issueColumns() { - if col == "no_history" { - values[i] = int64(1) - } - } - mock.ExpectQuery(issuesSnapshot).WithArgs(id).WillReturnRows(issueRows().AddRow(values...)) - mock.ExpectQuery(regexp.QuoteMeta("SELECT label FROM labels WHERE issue_id = ? ORDER BY label")). - WithArgs(id). - WillReturnRows(sqlmock.NewRows([]string{"label"})) - mock.ExpectRollback() - - if err := RecordVersionAtInTx(context.Background(), tx, id, "actor", at); err != nil { - t.Fatalf("RecordVersionAtInTx on a no-history row = %v, want nil", err) - } - if err := tx.Rollback(); err != nil { - t.Fatalf("rollback: %v", err) - } - if err := mock.ExpectationsWereMet(); err != nil { - t.Fatalf("the unfenced mint read or wrote something for a no-history row: %v", err) - } - }) - - t.Run("a legacy row is minted and left unstamped", func(t *testing.T) { - _, mock, tx := beginMockTx(t) - mock.ExpectQuery(issuesSnapshot).WithArgs(id). - WillReturnRows(issueRows().AddRow(issueRowValues(id, "legacy, history was off when it was made")...)) - mock.ExpectQuery(regexp.QuoteMeta("SELECT label FROM labels WHERE issue_id = ? ORDER BY label")). - WithArgs(id). - WillReturnRows(sqlmock.NewRows([]string{"label"})) - mock.ExpectQuery(participation).WithArgs(id). - WillReturnRows(sqlmock.NewRows([]string{"participation_generation"}).AddRow(nil)) - mock.ExpectQuery(regexp.QuoteMeta("SELECT 1 FROM wisps LIMIT 1")). - WillReturnRows(sqlmock.NewRows([]string{"1"})) - mock.ExpectQuery(`FROM dependencies WHERE issue_id IN`). - WithArgs(id). - WillReturnRows(sqlmock.NewRows([]string{"issue_id", "depends_on_id", "type", "created_at", "created_by", "metadata", "thread_id"})) - mock.ExpectQuery(regexp.QuoteMeta("SELECT epoch FROM store_epoch WHERE id = 1")). - WillReturnRows(sqlmock.NewRows([]string{"epoch"}).AddRow(1)) - mock.ExpectQuery(regexp.QuoteMeta("SELECT COALESCE(MAX(revision), 0) + 1 FROM issue_versions WHERE issue_id = ?")). - WithArgs(id). - WillReturnRows(sqlmock.NewRows([]string{"next"}).AddRow(1)) - mock.ExpectExec(`INSERT INTO issue_versions`). - WithArgs(id, int64(1), 1, sqlmock.AnyArg(), "actor", at, sqlmock.AnyArg()). - WillReturnResult(sqlmock.NewResult(0, 1)) - mock.ExpectExec(regexp.QuoteMeta("UPDATE issues SET current_revision = ?, updated_at = updated_at WHERE id = ?")). - WithArgs(int64(1), id). - WillReturnResult(sqlmock.NewResult(0, 1)) - mock.ExpectRollback() - - if err := RecordVersionAtInTx(context.Background(), tx, id, "actor", at); err != nil { - t.Fatalf("RecordVersionAtInTx on a legacy row = %v, want nil", err) - } - if err := tx.Rollback(); err != nil { - t.Fatalf("rollback: %v", err) - } - if err := mock.ExpectationsWereMet(); err != nil { - t.Fatalf("the unfenced mint did not mint a legacy row exactly once, unstamped: %v", err) - } - }) -} - -// refusalClass names what an admission verdict is, so two functions can be -// compared for agreement without depending on their exact wording. -func refusalClass(err error) string { - switch { - case err == nil: - return "admitted" - case errors.Is(err, ErrIntegerNotRepresentable): - return "number outside the I-JSON range" - case strings.Contains(err.Error(), "Duplicate key"): - return "duplicate key" - default: - return "other refusal" - } -} - -func issueWithTimeout(timeout time.Duration) types.Issue { - return types.Issue{ID: "be-1", Title: "t", IssueType: types.TypeGate, Timeout: timeout} -} - -// The check the pre-enable scan runs and the check the mint runs must never -// disagree, or a store could pass the scan, be switched on, and then refuse the -// first write to the row the scan cleared. So the check is the mint's own two -// steps behind one exported name, and this pins that they agree on every shape -// of issue. -// -// The domain is the WHOLE issue, because that is what the mint canonicalizes and -// what the scan hands the check. Metadata covers a number past the range, a -// fraction rounding past it, duplicate keys at the top level and nested, -// ordinary values, null, empty and an empty object, and text that is not JSON at -// all. The other field that can reach the range is a gate's timeout, a -// time.Duration that encoding/json writes as nanoseconds: 2^53-1 ns is about -// 104.25 days, so 2500h is admitted and 2600h is not. -func TestCheckIssueVersionableAgreesWithTheMint(t *testing.T) { - const maxExact = 1<<53 - 1 // 9007199254740991 ns, the last whole nanosecond count in range - for _, tc := range []struct { - name string - issue types.Issue - want string - }{ - {"number past the range", issueWithMetadata(`{"ts":1727000000000000000}`), "number outside the I-JSON range"}, - {"fraction rounding past the range", issueWithMetadata(`{"n":9007199254740993.5}`), "number outside the I-JSON range"}, - {"overflowing exponent", issueWithMetadata(`{"n":1e400}`), "number outside the I-JSON range"}, - {"number nested in an array", issueWithMetadata(`{"a":[1,{"b":-9007199254740993}]}`), "number outside the I-JSON range"}, - {"duplicate key", issueWithMetadata(`{"k":1,"k":2}`), "duplicate key"}, - {"nested duplicate key", issueWithMetadata(`{"a":{"k":1,"k":2}}`), "duplicate key"}, - {"ordinary values", issueWithMetadata(`{"a":1,"b":[0.1,2,"x"],"c":{"d":null}}`), "admitted"}, - {"number at the bound", issueWithMetadata(`{"n":9007199254740991}`), "admitted"}, - {"tiny magnitude", issueWithMetadata(`{"n":1e-400}`), "admitted"}, - {"null", issueWithMetadata(`null`), "admitted"}, - {"empty object", issueWithMetadata(`{}`), "admitted"}, - {"empty", issueWithMetadata(``), "admitted"}, - {"text that is not JSON", issueWithMetadata(`{"a":`), "other refusal"}, - - {"no timeout", issueWithTimeout(0), "admitted"}, - {"timeout 2500h", issueWithTimeout(2500 * time.Hour), "admitted"}, - {"timeout 2600h", issueWithTimeout(2600 * time.Hour), "number outside the I-JSON range"}, - {"timeout 8760h", issueWithTimeout(8760 * time.Hour), "number outside the I-JSON range"}, - {"negative timeout -2600h", issueWithTimeout(-2600 * time.Hour), "number outside the I-JSON range"}, - {"timeout of exactly 2^53-1 ns", issueWithTimeout(time.Duration(maxExact)), "admitted"}, - {"timeout of exactly 2^53 ns", issueWithTimeout(time.Duration(maxExact + 1)), "number outside the I-JSON range"}, - {"timeout in range beside ordinary metadata", func() types.Issue { - issue := issueWithTimeout(time.Hour) - issue.Metadata = json.RawMessage(`{"ok":1}`) - return issue - }(), "admitted"}, - } { - t.Run(tc.name, func(t *testing.T) { - check := CheckIssueVersionable(&tc.issue) - _, mint := canonicalDurableState(tc.issue) - if got := refusalClass(check); got != tc.want { - t.Errorf("CheckIssueVersionable = %v (%s), want %s", check, got, tc.want) - } - if got := refusalClass(mint); got != tc.want { - t.Errorf("canonicalDurableState = %v (%s), want %s", mint, got, tc.want) - } - if refusalClass(check) != refusalClass(mint) { - t.Errorf("the pre-enable check and the mint disagree: check %v, mint %v", check, mint) - } - }) - } -} - -// The scan runs the check over every issue the mint would version and skips -// the ones it would not, by the mint's own rule (IsWisp): ephemeral rows and -// no-history rows are never versioned, so a poisoned value in one can never -// abort a write and is not an obstacle to switching history on. The scan -// reports what it found in the order it was given, names each offender with -// the refusal the mint would return and the field it was found in, and finds -// nothing in a clean store. -// -// A gate whose timeout is over 105 days is the case that motivated checking the -// whole issue: its timeout is past 2^53-1 ns, so, if it participates in history, -// the mint refuses every write to it, and a scan that looked only at metadata -// cleared the switch over it. -func TestFindUnversionableFindsWhatTheMintWouldRefuse(t *testing.T) { - oversize := json.RawMessage(`{"ts":1727000000000000000}`) - overlong := 105 * 24 * time.Hour - issues := []*types.Issue{ - {ID: "bd-1", Metadata: oversize}, - {ID: "bd-2", Metadata: json.RawMessage(`{"k":1,"k":2}`)}, - {ID: "bd-3", Metadata: json.RawMessage(`{"ok":1}`)}, - {ID: "bd-4", Ephemeral: true, Metadata: oversize}, - {ID: "bd-5", NoHistory: true, Metadata: oversize}, - {ID: "bd-6"}, - {ID: "bd-7", Metadata: json.RawMessage(`{"n":9007199254740993.5}`)}, - {ID: "bd-8", Ephemeral: true, WispPlaneOverride: boolPtr(false), Metadata: oversize}, - {ID: "bd-9", WispPlaneOverride: boolPtr(true), Metadata: oversize}, - {ID: "bd-10", IssueType: types.TypeGate, Timeout: overlong}, - {ID: "bd-11", IssueType: types.TypeGate, Timeout: 2500 * time.Hour}, - {ID: "bd-12", IssueType: types.TypeGate, Ephemeral: true, Timeout: overlong}, - {ID: "bd-13", IssueType: types.TypeGate, Timeout: -overlong}, - } - - got := FindUnversionable(issues) - - want := []struct { - id string - class string - field string - }{ - {"bd-1", "number outside the I-JSON range", "metadata"}, - {"bd-2", "duplicate key", "metadata"}, - {"bd-7", "number outside the I-JSON range", "metadata"}, - // The override decides which plane the mint routes an issue to, and the - // scan follows it: bd-8 is versioned despite Ephemeral, bd-9 is not. - {"bd-8", "number outside the I-JSON range", "metadata"}, - {"bd-10", "number outside the I-JSON range", "timeout"}, - {"bd-13", "number outside the I-JSON range", "timeout"}, - } - if len(got) != len(want) { - t.Fatalf("FindUnversionable found %d issues %v, want %d", len(got), unversionableIDs(got), len(want)) - } - for i, w := range want { - if got[i].ID != w.id { - t.Fatalf("offender %d is %s, want %s (order of the input); got %v", i, got[i].ID, w.id, unversionableIDs(got)) - } - if class := refusalClass(got[i].Err); class != w.class { - t.Errorf("%s: refusal class %q (%v), want %q", w.id, class, got[i].Err, w.class) - } - if got[i].Field != w.field { - t.Errorf("%s: field %q, want %q", w.id, got[i].Field, w.field) - } - if !strings.Contains(got[i].Err.Error(), fmt.Sprintf("%q", w.field)) { - t.Errorf("%s: refusal %q does not name the field %q", w.id, got[i].Err, w.field) - } - } - - clean := []*types.Issue{ - {ID: "bd-1", Metadata: json.RawMessage(`{"ok":1,"f":0.1}`)}, - {ID: "bd-2"}, - {ID: "bd-3", Metadata: json.RawMessage(`null`)}, - {ID: "bd-4", Ephemeral: true, Metadata: oversize}, - {ID: "bd-5", IssueType: types.TypeGate, Timeout: 2500 * time.Hour}, - {ID: "bd-6", IssueType: types.TypeGate, Ephemeral: true, Timeout: overlong}, - } - if found := FindUnversionable(clean); len(found) != 0 { - t.Fatalf("a store with nothing the mint would refuse reported %v", unversionableIDs(found)) - } - if found := FindUnversionable(nil); len(found) != 0 { - t.Fatalf("no issues reported %v", unversionableIDs(found)) - } -} - -func unversionableIDs(found []UnversionableIssue) []string { - ids := make([]string, 0, len(found)) - for _, f := range found { - ids = append(ids, f.ID) - } - return ids -} - -// The scan does not hand the check the issue the mint sees. It reads a lite -// projection (the heavy text columns are left out, labels are not loaded) and -// never loads dependencies, which the mint adds before it canonicalizes. That is -// sound only while everything the scan leaves out is free of numbers: a number in -// a field the scan never loads is one it can never refuse and the mint will. The -// next three tests are how that stays true. They fail, with the way out, the day -// a field is added that breaks it. - -// fieldByJSONName finds the exported field of the struct type typ that -// encoding/json writes under name. -func fieldByJSONName(typ reflect.Type, name string) (reflect.StructField, bool) { - for i := 0; i < typ.NumField(); i++ { - f := typ.Field(i) - if !f.IsExported() { - continue - } - tag := f.Tag.Get("json") - if tag == "-" { - continue - } - jsonName := strings.Split(tag, ",")[0] - if jsonName == "" { - jsonName = f.Name - } - if jsonName == name { - return f, true - } - } - return reflect.StructField{}, false -} - -// heavyColumnProblems maps each column to its field of the issue type and reports -// every one that is missing or that can hold a number. A string or a slice of -// strings cannot: encoding/json writes both without a JSON number in them. -func heavyColumnProblems(columns []string, issue reflect.Type) []string { - var problems []string - for _, col := range columns { - f, ok := fieldByJSONName(issue, col) - if !ok { - problems = append(problems, fmt.Sprintf("column %q has no %s field written under that name: map it, then say whether it can hold a number", col, issue)) - continue - } - numberFree := f.Type.Kind() == reflect.String || - (f.Type.Kind() == reflect.Slice && f.Type.Elem().Kind() == reflect.String) - if !numberFree { - problems = append(problems, fmt.Sprintf("column %q is %s.%s (%s), which can hold a number the scan never reads but the mint refuses: load it in the scan or classify it lite", col, issue, f.Name, f.Type)) - } - } - return problems -} - -// TestTheScanProjectionOmitsOnlyNumberFreeFields pins that every column the -// scan's lite projection leaves out (HeavyDropList) is a string or a list of -// strings on the issue. -func TestTheScanProjectionOmitsOnlyNumberFreeFields(t *testing.T) { - if problems := heavyColumnProblems(HeavyDropList, reflect.TypeOf(types.Issue{})); len(problems) > 0 { - t.Fatalf("the scan's projection leaves out a column that can hold a number:\n %s", strings.Join(problems, "\n ")) - } -} - -// The guard above would pass vacuously if it never found anything to refuse, so -// it is run once against columns that must be refused: numbers, metadata (a -// verbatim JSON document), a column that maps to nothing, and the columns that -// really are number-free. -func TestTheHeavyColumnGuardRefusesWhatCanHoldANumber(t *testing.T) { - issue := reflect.TypeOf(types.Issue{}) - for _, col := range []string{"priority", "estimated_minutes", "timeout", "metadata", "created_at_unix"} { - if problems := heavyColumnProblems([]string{col}, issue); len(problems) != 1 { - t.Errorf("column %q: got %d problems %v, want exactly one", col, len(problems), problems) - } - } - for _, col := range []string{"description", "design", "acceptance_criteria", "notes", "waiters", "payload"} { - if problems := heavyColumnProblems([]string{col}, issue); len(problems) != 0 { - t.Errorf("column %q is number-free but was refused: %v", col, problems) - } - } - problems := heavyColumnProblems([]string{"priority"}, issue) - if len(problems) == 1 && !strings.Contains(problems[0], "load it in the scan or classify it lite") { - t.Errorf("the refusal %q does not say how to resolve it", problems[0]) - } -} - -var ( - timeType = reflect.TypeOf(time.Time{}) - rawMessageType = reflect.TypeOf(json.RawMessage(nil)) -) - -// numberBearingPaths walks typ the way encoding/json does and returns the path of -// everything that can write a JSON number or a value this walk cannot see into: -// an integer, unsigned integer or float (a time.Duration is an int64), a -// json.RawMessage, an interface, a map. It recurses through slices, arrays, -// pointers and structs, skips what json:"-" leaves out and the unexported fields, -// and treats a time.Time as the string it is written as. -func numberBearingPaths(typ reflect.Type, path string, onPath map[reflect.Type]bool) []string { - switch typ { - case timeType: - return nil - case rawMessageType: - return []string{path + " is a json.RawMessage, passed through verbatim"} - } - switch typ.Kind() { - case reflect.Bool, reflect.String: - return nil - case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, - reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr, - reflect.Float32, reflect.Float64: - return []string{fmt.Sprintf("%s is a %s", path, typ)} - case reflect.Interface, reflect.Map: - return []string{fmt.Sprintf("%s is a %s", path, typ.Kind())} - case reflect.Pointer, reflect.Slice, reflect.Array: - return numberBearingPaths(typ.Elem(), path, onPath) - case reflect.Struct: - if onPath[typ] { - return nil - } - onPath[typ] = true - defer delete(onPath, typ) - var found []string - for i := 0; i < typ.NumField(); i++ { - f := typ.Field(i) - if !f.IsExported() || f.Tag.Get("json") == "-" { - continue - } - found = append(found, numberBearingPaths(f.Type, path+"."+f.Name, onPath)...) - } - return found - default: - return []string{fmt.Sprintf("%s is a %s, which this walk cannot classify", path, typ.Kind())} - } -} - -// TestWhatTheMintAddsAndTheScanDoesNotLoadCarriesNoNumber covers the other half -// of what the scan leaves out. The mint adds the issue's dependencies before it -// canonicalizes, and the scan reads without labels, so a number anywhere in a -// Dependency, a Comment, a BondRef or a label would be refused by the mint and -// invisible to the scan. None carries one today; a field that could (a Weight -// int on Dependency) fails here, and the person who adds it decides whether the -// scan has to load it. -func TestWhatTheMintAddsAndTheScanDoesNotLoadCarriesNoNumber(t *testing.T) { - for _, tc := range []struct { - name string - typ reflect.Type - }{ - {"types.Dependency", reflect.TypeOf(types.Dependency{})}, - {"types.Comment", reflect.TypeOf(types.Comment{})}, - {"types.BondRef", reflect.TypeOf(types.BondRef{})}, - {"a label", reflect.TypeOf("")}, - {"the labels of an issue", reflect.TypeOf([]string(nil))}, - } { - t.Run(tc.name, func(t *testing.T) { - if found := numberBearingPaths(tc.typ, tc.name, map[reflect.Type]bool{}); len(found) > 0 { - t.Fatalf("a field the mint writes and the scan does not load can hold a number:\n %s", strings.Join(found, "\n ")) - } - }) - } -} - -// The walk is run against shapes it must flag and shapes it must not, for the -// same reason as the guard above. -func TestTheNumberFreeWalkFlagsEveryNumberBearingShape(t *testing.T) { - type weighted struct { - Weight int `json:"weight"` - } - type unsigned struct{ N uint32 } - type scored struct{ Score float64 } - type waiting struct{ Wait time.Duration } - type verbatim struct{ Blob json.RawMessage } - type anything struct{ Value any } - type keyed struct{ Extra map[string]string } - type nested struct{ Deps []*weighted } - type tagged struct { - Name string `json:"name"` - Tags []string `json:"tags"` - At time.Time `json:"at"` - Skipped int `json:"-"` - _ int - } - type recursive struct{ Next *recursive } - - for name, typ := range map[string]reflect.Type{ - "an int": reflect.TypeOf(weighted{}), - "an unsigned integer": reflect.TypeOf(unsigned{}), - "a float": reflect.TypeOf(scored{}), - "a time.Duration": reflect.TypeOf(waiting{}), - "a json.RawMessage": reflect.TypeOf(verbatim{}), - "an interface": reflect.TypeOf(anything{}), - "a map": reflect.TypeOf(keyed{}), - "an int behind slice+pointer": reflect.TypeOf(nested{}), - } { - if found := numberBearingPaths(typ, "x", map[reflect.Type]bool{}); len(found) == 0 { - t.Errorf("%s was not flagged", name) - } - } - for name, typ := range map[string]reflect.Type{ - "strings, a time, a skipped int and an unexported int": reflect.TypeOf(tagged{}), - "a type that contains itself": reflect.TypeOf(recursive{}), - } { - if found := numberBearingPaths(typ, "x", map[reflect.Type]bool{}); len(found) != 0 { - t.Errorf("%s was flagged: %v", name, found) - } - } -} - -// There is ONE check, CheckIssueVersionable, and the scan and the mint both go -// through it. A check over a part of the issue, the metadata alone as there once -// was, can pass what the mint refuses, and an exported one invites reuse by the -// next caller (a doctor check, say) that then inherits the gap. So no exported -// function in the package may be named for versionability beside the check and -// the scan that calls it. -func TestNoExportedCheckOverASubPartOfTheIssueExistsBesideCheckIssueVersionable(t *testing.T) { - fset := token.NewFileSet() - pkgs, err := parser.ParseDir(fset, ".", func(fi fs.FileInfo) bool { - return !strings.HasSuffix(fi.Name(), "_test.go") - }, parser.SkipObjectResolution) - if err != nil { - t.Fatalf("parse issueops package: %v", err) - } - allowed := map[string]bool{"CheckIssueVersionable": true, "FindUnversionable": true} - seen := map[string]bool{} - var stray []string - for _, pkg := range pkgs { - for _, file := range pkg.Files { - for _, decl := range file.Decls { - fn, ok := decl.(*ast.FuncDecl) - if !ok || fn.Recv != nil || !fn.Name.IsExported() { - continue - } - if !strings.Contains(strings.ToLower(fn.Name.Name), "versionable") { - continue - } - seen[fn.Name.Name] = true - if !allowed[fn.Name.Name] { - stray = append(stray, fmt.Sprintf("%s (%s)", fn.Name.Name, fset.Position(fn.Pos()))) - } - } - } - } - sort.Strings(stray) - if len(stray) > 0 { - t.Errorf("an exported check over part of the issue exists beside CheckIssueVersionable: %s", strings.Join(stray, ", ")) - } - for name := range allowed { - if !seen[name] { - t.Errorf("%s was not found: this guard no longer sees the check it protects", name) - } - } -} diff --git a/internal/storage/issueops/version_history_scan.go b/internal/storage/issueops/version_history_scan.go deleted file mode 100644 index 855869325..000000000 --- a/internal/storage/issueops/version_history_scan.go +++ /dev/null @@ -1,53 +0,0 @@ -package issueops - -import ( - "errors" - - "github.com/steveyegge/beads/internal/types" -) - -// UnversionableIssue names one issue that RecordVersionInTx would refuse to -// version, and the refusal it would return. Field is the top-level field of the -// issue the refusal was found in ("metadata", "timeout"), empty when the refusal -// does not name one. -type UnversionableIssue struct { - ID string - Field string - Err error -} - -// FindUnversionable runs CheckIssueVersionable over every issue in issues that -// the mint would version and returns the ones it refuses, in the order they were -// given. It is the core of the check made when versioned history is switched on: -// with history on, a write that introduces a value the mint refuses aborts in its -// own transaction, but a row that already holds one -- written while history was -// off, or by a path that does not mint -- would, if it participates in history, -// fail every later write to it. The check is over the whole issue, as the mint's -// is, and not over its metadata alone: a gate's timeout past 2^53-1 nanoseconds is -// refused too. -// -// It skips the rows the mint never versions by IsWisp: ephemeral and no-history -// rows are never versioned, so a value they hold can never abort a write. It -// cannot skip legacy rows, because participation is not on types.Issue, so on a -// store with the write fence (design §16.2b) it is a deliberate -// over-approximation: the mint skips a legacy row whatever it holds, and this -// still refuses over it, which is the safe direction because it can only refuse -// more. It does no I/O; the caller loads the rows, which is what lets one -// function serve the direct store and the proxied route. -func FindUnversionable(issues []*types.Issue) []UnversionableIssue { - var found []UnversionableIssue - for _, issue := range issues { - if issue == nil || IsWisp(issue) { - continue - } - if err := CheckIssueVersionable(issue); err != nil { - var named *refusedFieldError - field := "" - if errors.As(err, &named) { - field = named.field - } - found = append(found, UnversionableIssue{ID: issue.ID, Field: field, Err: err}) - } - } - return found -} diff --git a/internal/storage/issueops/version_history_test.go b/internal/storage/issueops/version_history_test.go deleted file mode 100644 index 3c8818403..000000000 --- a/internal/storage/issueops/version_history_test.go +++ /dev/null @@ -1,61 +0,0 @@ -package issueops - -import "testing" - -// These tests pin the derivation rule for issue_versions.attribution_status, -// the NOT NULL column migration 0068 step 6 adds. Its vocabulary is BDP's -// carried-attribution status (gastownhall/bdp#18, merged 2026-09-07): exactly -// two values, "claimed" and "unknown". "imported" is provenance, not an -// assertion, and is not a status — it returns as a separate provenance marker -// in the phase that first imports history, for which no writer exists yet. -// -// RecordVersionInTx receives only a plain actor string from every call site -// (none passes any additional attribution context), so the only signal -// available to derive attribution_status from is whether actor is empty: a -// non-empty actor is "claimed", an empty one is "unknown". -func TestAttributionStatusForActor(t *testing.T) { - t.Parallel() - - tests := []struct { - name string - actor string - want string - }{ - {name: "non-empty actor is claimed", actor: "alice", want: attributionStatusClaimed}, - {name: "empty actor is unknown", actor: "", want: attributionStatusUnknown}, - } - - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { - t.Parallel() - if got := attributionStatusForActor(tc.actor); got != tc.want { - t.Errorf("attributionStatusForActor(%q) = %q, want %q", tc.actor, got, tc.want) - } - }) - } -} - -// TestAttributionStatusValuesMatchBDP pins the two legal values verbatim -// against bdp#18's vocabulary, so a future edit cannot silently rename one, -// or reintroduce a value BDP does not carry, without failing here — and -// checks that the derivation never yields anything outside that set. -func TestAttributionStatusValuesMatchBDP(t *testing.T) { - t.Parallel() - - values := map[string]string{ - "claimed": attributionStatusClaimed, - "unknown": attributionStatusUnknown, - } - for want, got := range values { - if got != want { - t.Errorf("attribution status constant = %q, want %q", got, want) - } - } - - legal := map[string]bool{"claimed": true, "unknown": true} - for _, actor := range []string{"", "alice", "agent:builder"} { - if got := attributionStatusForActor(actor); !legal[got] { - t.Errorf("attributionStatusForActor(%q) = %q, want one of claimed|unknown", actor, got) - } - } -} diff --git a/internal/storage/issueops/version_list.go b/internal/storage/issueops/version_list.go deleted file mode 100644 index fdfc9f2f9..000000000 --- a/internal/storage/issueops/version_list.go +++ /dev/null @@ -1,77 +0,0 @@ -package issueops - -import ( - "context" - "database/sql" - "fmt" - - "github.com/steveyegge/beads/internal/storage" -) - -// ListVersionsInTx returns every retained version of issueID, newest first. -// -// No subquery wrapper here, deliberately: HistoryInTx wraps its select -// because dolt_history_issues is a Dolt SYSTEM table and the planner's -// max1Row optimization mis-assumes WHERE id=? yields one row. issue_versions -// is an ordinary user table with a composite (issue_id, revision) primary -// key, so that optimization does not apply and the wrapper would only cost a -// materialization. -// -// An issue with no versions returns an empty slice and no error -- that is a -// truthful "nothing retained here", and it is the CALLER's job to distinguish -// it from "the feature is off" and from "no such issue", which it cannot do -// from this result alone. -// -// A removed version's RemovedRestriction is one of the as-of read's four values: -// whatever else the column holds, a NULL or empty one included, is reported as -// unknown, exactly as AsOfReadInTx reports it. A version that was not removed -// carries none. -func ListVersionsInTx(ctx context.Context, tx DBTX, issueID string) ([]storage.IssueVersion, error) { - rows, err := tx.QueryContext(ctx, ` - SELECT - issue_id, revision, epoch, - COALESCE(change_actor, '') AS change_actor, - COALESCE(change_agent, '') AS change_agent, - COALESCE(change_message, '') AS change_message, - change_at, - COALESCE(attribution_status, '') AS attribution_status, - removed_at, - COALESCE(removed_reason, '') AS removed_reason, - COALESCE(removed_restriction, '') AS removed_restriction, - COALESCE(LENGTH(durable_state), 0) AS state_bytes - FROM issue_versions - WHERE issue_id = ? - ORDER BY revision DESC - `, issueID) - if err != nil { - return nil, fmt.Errorf("failed to list issue versions: %w", err) - } - defer func() { _ = rows.Close() }() - - var out []storage.IssueVersion - for rows.Next() { - var v storage.IssueVersion - var removedAt sql.NullTime - if err := rows.Scan( - &v.IssueID, &v.Revision, &v.Epoch, - &v.ChangeActor, &v.ChangeAgent, &v.ChangeMessage, &v.ChangeAt, - &v.AttributionStatus, - &removedAt, &v.RemovedReason, &v.RemovedRestriction, - &v.StateBytes, - ); err != nil { - return nil, fmt.Errorf("failed to scan issue version: %w", err) - } - if removedAt.Valid { - t := removedAt.Time - v.RemovedAt = &t - v.RemovedRestriction = string(NormalizeRemovedRestriction(v.RemovedRestriction)) - } else { - v.RemovedRestriction = "" - } - out = append(out, v) - } - if err := rows.Err(); err != nil { - return nil, fmt.Errorf("failed to read issue versions: %w", err) - } - return out, nil -} diff --git a/internal/storage/issueops/wake_defers.go b/internal/storage/issueops/wake_defers.go index 2ea34a391..bc6bde7dc 100644 --- a/internal/storage/issueops/wake_defers.go +++ b/internal/storage/issueops/wake_defers.go @@ -138,11 +138,6 @@ func wakeExpiredDefersInTable(ctx context.Context, tx DBTX, table, eventsTable s if err := RecordEventInTx(ctx, tx, EventUpdate, id, DeferWakeActor); err != nil { return woken, err } - // The status flip is durable state, so the woken bead is versioned - // under the same system actor the event carries. - if err := RecordVersionInTx(ctx, tx, id, DeferWakeActor); err != nil { - return woken, err - } woken = append(woken, id) } return woken, nil diff --git a/internal/storage/issueops/wake_defers_test.go b/internal/storage/issueops/wake_defers_test.go deleted file mode 100644 index fae1b596e..000000000 --- a/internal/storage/issueops/wake_defers_test.go +++ /dev/null @@ -1,127 +0,0 @@ -package issueops - -import ( - "context" - "database/sql" - "database/sql/driver" - "fmt" - "slices" - "sync/atomic" - "testing" - - "github.com/DATA-DOG/go-sqlmock" - - "github.com/steveyegge/beads/internal/storage/sqlcount" -) - -// With versioned history off the mint is a no-op, so the sweep has no reason -// to look at an expired row before it wakes it, nor after. With history on it -// reads the woken row to mint its version; a later change that also checks -// each row against the mint before waking it (so that one unversionable row -// cannot abort the whole sweep) must keep that check free when history is -// off. -// -// The mock scripts exactly the statements a one-row sweep issues: the -// expired-row SELECT, the UPDATE, the event's same-content SELECT and its -// INSERT, and the empty wisps pass. Nothing else is expected, and the mock is -// reached through sqlcount, which counts every statement the sweep sends. So -// a sweep that loaded the row (or read its participation) before or after -// waking it fails the pin even if it swallowed the error sqlmock answers that -// statement with, which sqlmock's own checks would not see. The history-on -// run of the same script is the control: the mint's read must make the pin -// refuse it. -func TestWakeExpiredDefersWithHistoryOffLoadsNothingBeforeWaking(t *testing.T) { - if err := wakeAgainstScript(t, false); err != nil { - t.Fatalf("history off: %v", err) - } - t.Run("history on is refused by the same script", func(t *testing.T) { - err := wakeAgainstScript(t, true) - if err == nil { - t.Fatal("the sweep with history on passed the history-off script; " + - "the script no longer sees the reads history adds, so the history-off run above proves nothing") - } - t.Logf("history on, as expected: %v", err) - }) -} - -// wakeStatements is how many statements the script below holds, and so how -// many the history-off sweep may send. -const wakeStatements = 5 - -// wakeAgainstScript runs a one-row sweep on a mocked transaction scoped with -// versioned history as asked, against the script of the statements the -// history-off sweep issues. It returns why the sweep broke the script, or nil -// if it sent exactly the scripted statements and woke the scripted row. -func wakeAgainstScript(t *testing.T, history bool) error { - t.Helper() - mock, tx, counts := countedMockTx(t) - if history { - t.Cleanup(ScopeVersionedHistoryTransaction(tx, true)) - } - // Otherwise tx is not scoped for versioned history, which is how every - // transaction reads while it is off. - mock.ExpectQuery(`SELECT id FROM issues WHERE status = 'deferred'`). - WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow("bd-1")) - mock.ExpectExec(`UPDATE issues SET status = 'open', defer_until = NULL`). - WithArgs(sqlmock.AnyArg(), sqlmock.AnyArg(), "bd-1"). - WillReturnResult(sqlmock.NewResult(0, 1)) - mock.ExpectQuery(`SELECT id FROM events WHERE issue_id`). - WillReturnRows(sqlmock.NewRows([]string{"id"})) - mock.ExpectExec(`INSERT INTO events`). - WillReturnResult(sqlmock.NewResult(0, 1)) - mock.ExpectQuery(`SELECT id FROM wisps WHERE status = 'deferred'`). - WillReturnRows(sqlmock.NewRows([]string{"id"})) - - got, err := WakeExpiredDefersInTx(t.Context(), tx) - if err != nil { - return fmt.Errorf("the sweep failed against the script: %w", err) - } - if n := counts.Total(); n != wakeStatements { - return fmt.Errorf("the sweep sent %d statements, want exactly the %d scripted; "+ - "sqlmock refused each extra one with an error the sweep dropped", n, wakeStatements) - } - if err := mock.ExpectationsWereMet(); err != nil { - return fmt.Errorf("the sweep did not send the scripted statements: %w", err) - } - if !slices.Equal(got.Issues, []string{"bd-1"}) || len(got.Wisps) != 0 { - return fmt.Errorf("woken = issues %v, wisps %v, want [bd-1] and none", got.Issues, got.Wisps) - } - return nil -} - -var sqlmockSeq atomic.Int64 - -// countedMockTx begins a transaction on a fresh sqlmock connection that is -// reached through sqlcount, so a pin can count every statement the code -// under test sends, including one whose error it swallowed. -func countedMockTx(t *testing.T) (sqlmock.Sqlmock, *sql.Tx, *sqlcount.Counts) { - t.Helper() - dsn := fmt.Sprintf("issueops_counted_%d", sqlmockSeq.Add(1)) - mockDB, mock, err := sqlmock.NewWithDSN(dsn) - if err != nil { - t.Fatalf("sqlmock.NewWithDSN: %v", err) - } - counts := &sqlcount.Counts{} - db := sql.OpenDB(sqlcount.WrapConnector(sqlmockConnector{dsn: dsn, driver: mockDB.Driver()}, counts)) - t.Cleanup(func() { - _ = db.Close() - _ = mockDB.Close() - }) - mock.ExpectBegin() - tx, err := db.BeginTx(t.Context(), nil) - if err != nil { - t.Fatalf("BeginTx: %v", err) - } - t.Cleanup(func() { _ = tx.Rollback() }) - return mock, tx, counts -} - -// sqlmockConnector opens the sqlmock connection registered under dsn, for -// sql.OpenDB through a wrapping connector. -type sqlmockConnector struct { - dsn string - driver driver.Driver -} - -func (c sqlmockConnector) Connect(context.Context) (driver.Conn, error) { return c.driver.Open(c.dsn) } -func (c sqlmockConnector) Driver() driver.Driver { return c.driver } diff --git a/internal/storage/issueops/wisp_routing.go b/internal/storage/issueops/wisp_routing.go index 69583305d..729a5ffd9 100644 --- a/internal/storage/issueops/wisp_routing.go +++ b/internal/storage/issueops/wisp_routing.go @@ -38,7 +38,7 @@ func wispsTableEmptyOrMissingInTx(ctx context.Context, tx DBTX) (bool, error) { } //nolint:gosec // table is selected by callers from fixed optional wisp tables. -func optionalTableExistsInTx(ctx context.Context, tx DBTX, table string) (bool, error) { +func optionalTableExistsInTx(ctx context.Context, tx *sql.Tx, table string) (bool, error) { var probe int err := tx.QueryRowContext(ctx, fmt.Sprintf("SELECT 1 FROM %s LIMIT 1", table)).Scan(&probe) switch { diff --git a/internal/storage/journalscan/BUILD.bazel b/internal/storage/journalscan/BUILD.bazel deleted file mode 100644 index 0b829365e..000000000 --- a/internal/storage/journalscan/BUILD.bazel +++ /dev/null @@ -1,88 +0,0 @@ -load("@rules_go//go:def.bzl", "go_library", "go_test") - -go_library( - name = "journalscan", - srcs = ["journalscan.go"], - importpath = "github.com/steveyegge/beads/internal/storage/journalscan", - visibility = ["//:__subpackages__"], -) - -go_test( - name = "journalscan_test", - srcs = ["journalscan_test.go"], - embed = [":journalscan"], -) - -# --- begin go_srcs (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) --- - -filegroup( - name = "go_srcs", - srcs = glob( - ["**/*.go"], - allow_empty = True, - ), - visibility = ["//:__subpackages__"], -) - -# --- end go_srcs --- - -# --- begin repo_files (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) --- - -filegroup( - name = "repo_doc_files", - srcs = glob( - [ - "**/*.md", - "**/*.mdx", - ], - allow_empty = True, - exclude = ["**/node_modules/**"], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_go_srcs", - srcs = glob( - ["**/*.go"], - allow_empty = True, - exclude = [ - "**/*_test.go", - "**/node_modules/**", - ], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_go_test_srcs", - srcs = glob( - ["**/*_test.go"], - allow_empty = True, - exclude = ["**/node_modules/**"], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_other_files", - srcs = glob( - ["**"], - exclude = [ - "**/*.db", - "**/*.exe", - "**/*.go", - "**/*.md", - "**/*.mdx", - "**/*.out", - "**/*.prof", - "**/*.pyc", - "**/*.test", - "**/__pycache__/**", - "**/node_modules/**", - ], - ), - visibility = ["//:__subpackages__"], -) - -# --- end repo_files --- diff --git a/internal/storage/journalscan/journalscan.go b/internal/storage/journalscan/journalscan.go index c274dd6b8..d04fc24b1 100644 --- a/internal/storage/journalscan/journalscan.go +++ b/internal/storage/journalscan/journalscan.go @@ -13,7 +13,6 @@ import ( "go/token" "io/fs" "regexp" - "sort" "strings" ) @@ -23,28 +22,11 @@ type FuncInfo struct { Recv string // receiver type name ("" for free functions) Name string // bare method/function name Exported bool // the bare name is exported - Params []string // declared parameter names, in order ("" for an unnamed one) IdentCalls []string // intra-package bare-identifier calls (free functions) SelCalls []string // selector calls, by selector name (x.Foo -> "Foo") - Calls []Call // every call, bare or selector, with its arguments OwnBeadDML bool // body issues INSERT/UPDATE/DELETE against a bead table } -// Call is one call expression in a function body: the called name (a bare -// identifier, or the selector name of x.Foo) and its arguments, each reduced -// to the bare identifier or literal it passes — "" for any other expression. -// The predeclared false and true read as "false" and "true"; a false or true -// that names a declaration of the package's own instead — a shadow, whether -// the parser resolved it within its file or some file declares the name at -// package level — is "" like any other expression the scanner cannot vouch -// for, so a shadow never reads as the literal. A caller forwarding its own -// parameter reads as that parameter's name, which is what lets a guard tell -// "switched off here" from "left to the caller". -type Call struct { - Name string - Args []string -} - // AllCallNames returns every called name, both bare-identifier and selector. func (f *FuncInfo) AllCallNames() []string { return append(append([]string{}, f.IdentCalls...), f.SelCalls...) @@ -60,17 +42,6 @@ func (f *FuncInfo) CallsAnyOf(set map[string]bool) bool { return false } -// ParamIndex returns the position of f's parameter named name, or -1 when f -// declares none by that name. -func (f *FuncInfo) ParamIndex(name string) int { - for i, p := range f.Params { - if p == name { - return i - } - } - return -1 -} - // ReceiverTypeName returns the bare type name of a method receiver // (e.g. *fooImpl -> fooImpl). func ReceiverTypeName(expr ast.Expr) string { @@ -87,31 +58,12 @@ func ReceiverTypeName(expr ast.Expr) string { // top-level function/method, keyed by "Recv.Name" (or "Name" for free funcs). func ParsePackage(dir string) (map[string]*FuncInfo, error) { fset := token.NewFileSet() - // Mode 0 keeps the parser's per-file identifier resolution on. It is what - // lets argNames tell the predeclared false and true from a shadow of the - // same name; under SkipObjectResolution every shadow would read as the - // literal. TestCallsOnlyWithLiteralFalse pins this. pkgs, err := parser.ParseDir(fset, dir, func(fi fs.FileInfo) bool { return !strings.HasSuffix(fi.Name(), "_test.go") }, 0) if err != nil { return nil, err } - // That resolution stops at the file: a use in one file of a false another - // file declares at package level stays unresolved, exactly like the - // predeclared one, though to the compiler it denotes the declaration. - // Fold every file's package-level declarations in so argNames refuses - // those too (TestPackageLevelShadowCrossesFiles). - shadowed := map[string]bool{} - for _, pkg := range pkgs { - for _, file := range pkg.Files { - for name := range predeclaredBools { - if file.Scope != nil && file.Scope.Lookup(name) != nil { - shadowed[name] = true - } - } - } - } out := map[string]*FuncInfo{} for _, pkg := range pkgs { for _, file := range pkg.Files { @@ -120,7 +72,7 @@ func ParsePackage(dir string) (map[string]*FuncInfo, error) { if !ok { continue } - f := &FuncInfo{Name: fn.Name.Name, Exported: fn.Name.IsExported(), Params: paramNames(fn.Type)} + f := &FuncInfo{Name: fn.Name.Name, Exported: fn.Name.IsExported()} if fn.Recv != nil && len(fn.Recv.List) > 0 { f.Recv = ReceiverTypeName(fn.Recv.List[0].Type) } @@ -130,10 +82,8 @@ func ParsePackage(dir string) (map[string]*FuncInfo, error) { switch fun := node.Fun.(type) { case *ast.Ident: f.IdentCalls = append(f.IdentCalls, fun.Name) - f.Calls = append(f.Calls, Call{Name: fun.Name, Args: argNames(node.Args, shadowed)}) case *ast.SelectorExpr: f.SelCalls = append(f.SelCalls, fun.Sel.Name) - f.Calls = append(f.Calls, Call{Name: fun.Sel.Name, Args: argNames(node.Args, shadowed)}) } case *ast.BasicLit: if node.Kind == token.STRING && SQLWritesBeadTable(node.Value) { @@ -153,135 +103,23 @@ func ParsePackage(dir string) (map[string]*FuncInfo, error) { return out, nil } -// paramNames flattens a signature's parameter list into one entry per -// parameter, so a position in it lines up with a position in a call's -// argument list: "a, b int" is two entries, and an unnamed parameter is "". -func paramNames(sig *ast.FuncType) []string { - if sig == nil || sig.Params == nil { - return nil - } - var names []string - for _, field := range sig.Params.List { - if len(field.Names) == 0 { - names = append(names, "") - continue - } - for _, name := range field.Names { - names = append(names, name.Name) - } - } - return names -} - -// predeclaredBools are the names argNames reads as the boolean literals when -// nothing in the package declares them. -var predeclaredBools = map[string]bool{"false": true, "true": true} - -// argNames reduces call arguments to the bare identifier or literal each one -// passes; anything else (a call, a selector, a composite literal) is "". So -// is a false or true that does not denote the predeclared identifier: one the -// parser resolved to a declaration in its own file (a.Obj != nil — a local, a -// parameter, a package-level declaration in the same file) or whose name some -// file declares at package level (shadowed, from ParsePackage). -func argNames(args []ast.Expr, shadowed map[string]bool) []string { - names := make([]string, len(args)) - for i, arg := range args { - switch a := arg.(type) { - case *ast.Ident: - if predeclaredBools[a.Name] && (a.Obj != nil || shadowed[a.Name]) { - continue - } - names[i] = a.Name - case *ast.BasicLit: - names[i] = a.Value - } - } - return names -} - -// Resolve maps a called bare name to the function keys it can denote: the -// free function of that name, if any, then every method of that name in key -// order (name-based resolution, sufficient for a guard). It is the one -// resolution Fixpoint and CallsOnlyWithLiteralFalse use, exported so a guard -// reasoning about a called name's targets — whether a switch-off is -// unambiguous, whether a live edge reaches the seam through a method — sees -// exactly the keys the fixpoint follows. -func Resolve(fns map[string]*FuncInfo, name string) []string { - var keys []string - if _, ok := fns[name]; ok { - keys = append(keys, name) - } - var methods []string - for key, f := range fns { - if f.Recv != "" && f.Name == name { - methods = append(methods, key) - } - } - sort.Strings(methods) - return append(keys, methods...) -} - -// CallsOnlyWithLiteralFalse reports whether f calls callee, and every one of -// those calls passes the predeclared identifier false for callee's boolean -// parameter named gate — the argument callee's own body reads as "skip the -// gated write". A guard that follows call edges to find who reaches a seam -// uses it to drop the edges a caller has explicitly switched off, so a -// composite mutation that runs its constituents with the gate off and -// performs the gated write once itself is not credited with the one it told -// them to skip. -// -// What is checked is the reduced argument (see Call): the bare name false, -// counted as the predeclared identifier only when it resolves to no -// declaration of the package's own. Go lets a local, a parameter or a -// package-level declaration shadow false, and any such shadow reads as an -// unknown expression, never as a switch-off. A consumer can pin that its -// package declares no such shadow at all, so that the name-based reading is -// exact there rather than merely conservative; issueops does, in -// TestFalseIsNotShadowedInIssueops. -// -// The edge stays live (false is returned) whenever the switch-off is not -// certain: callee is unknown or declares no parameter named gate, f never -// calls it, at least one of f's calls passes anything but the literal (true, -// a variable, f's own parameter of that name, a shadowed false, an -// expression), or a call is too short to line up with the signature. callee -// resolves the way Fixpoint resolves edges — Resolve: the free function of -// that name and every method of that name — and every resolution that -// declares the gate must see false. A resolution that declares no gate is -// not consulted, so when a gated function shares its name with an ungated -// one a true result drops the fixpoint's edge to the ungated one too; a -// consumer relying on the switch-off should pin that no literal-false -// callee's name is shared that way (issueops does, in -// TestLiteralFalseCalleesResolveUnambiguously). -func CallsOnlyWithLiteralFalse(fns map[string]*FuncInfo, f *FuncInfo, callee, gate string) bool { - var gateAt []int - for _, key := range Resolve(fns, callee) { - if i := fns[key].ParamIndex(gate); i >= 0 { - gateAt = append(gateAt, i) - } - } - if len(gateAt) == 0 { - return false - } - called := false - for _, call := range f.Calls { - if call.Name != callee { - continue - } - called = true - for _, i := range gateAt { - if i >= len(call.Args) || call.Args[i] != "false" { - return false - } - } - } - return called -} - // Fixpoint returns the set of function keys for which seed is true or which // (transitively) call a name for which it becomes true, following edges. A // called bare name resolves to a free function of that name and to any method -// with that name (Resolve: name-based resolution, sufficient for a guard). +// with that name (name-based resolution, sufficient for a guard). func Fixpoint(fns map[string]*FuncInfo, seed func(*FuncInfo) bool, edges func(*FuncInfo) []string) map[string]bool { + resolve := func(name string) []string { + var keys []string + if _, ok := fns[name]; ok { + keys = append(keys, name) + } + for key, f := range fns { + if f.Recv != "" && f.Name == name { + keys = append(keys, key) + } + } + return keys + } got := map[string]bool{} for key, f := range fns { if seed(f) { @@ -295,7 +133,7 @@ func Fixpoint(fns map[string]*FuncInfo, seed func(*FuncInfo) bool, edges func(*F continue } for _, callee := range edges(f) { - for _, ck := range Resolve(fns, callee) { + for _, ck := range resolve(callee) { if got[ck] { got[key] = true changed = true diff --git a/internal/storage/journalscan/journalscan_test.go b/internal/storage/journalscan/journalscan_test.go index 9137c8ad8..653c86a1d 100644 --- a/internal/storage/journalscan/journalscan_test.go +++ b/internal/storage/journalscan/journalscan_test.go @@ -1,11 +1,6 @@ package journalscan -import ( - "os" - "path/filepath" - "reflect" - "testing" -) +import "testing" // TestSQLWritesBeadTable pins the DML detector the completeness guards rest on. // A false negative here silently disarms every guard, so the templated forms — @@ -48,199 +43,3 @@ func TestSQLWritesBeadTable(t *testing.T) { } } } - -// TestCallsOnlyWithLiteralFalse pins the gate predicate the versioned-history -// guard rests on: a call that passes the predeclared false for a helper's -// mintVersion-style parameter is switched off, and nothing else is — not a -// declaration of the package's own that borrows the name false, which the -// parser's per-file resolution (ParsePackage's mode 0) tells apart. A false -// positive here would drop a real minting edge (a spurious guard failure); a -// false negative would let a composite mutation keep inheriting the mint it -// told its constituents to skip (gastownhall/beads#6358 reviewer Probe 2), -// which is the hole the predicate exists to close. -func TestCallsOnlyWithLiteralFalse(t *testing.T) { - dir := t.TempDir() - src := `package probe - -func seam() {} - -func helper(mintVersion bool) { - if mintVersion { - seam() - } -} - -// grouped declares the gate inside a grouped parameter list, so its position -// must be counted per name, not per group. -func grouped(a, b int, recordEvent, mintVersion bool) { - if mintVersion { - seam() - } -} - -func SwitchedOff() { helper(false) } -func SwitchedOffGrouped() { grouped(1, 2, true, false) } -func SwitchedOn() { helper(true) } -func Variable() { off := false; helper(off) } -func Forwards(mintVersion bool) { helper(mintVersion) } -func Mixed() { helper(false); helper(true) } -func MintsItself() { helper(false); seam() } -func TooShort() { helper() } -func NoGate() { ungated(false) } -func ungated(other bool) { seam() } -func NeverCalls() {} - -// The shadows: Go lets a declaration of the package's own take the name -// false, and none of them may read as the switch-off. -func ShadowedLocal() { false := true; helper(false) } -func ShadowedParam(false bool) { helper(false) } -func ShadowedInClosure() { func() { false := true; helper(false) }() } -` - if err := os.WriteFile(filepath.Join(dir, "probe.go"), []byte(src), 0o600); err != nil { - t.Fatal(err) - } - fns, err := ParsePackage(dir) - if err != nil { - t.Fatalf("ParsePackage: %v", err) - } - - // The two facts the predicate is built from: declared parameter positions - // and the reduced argument list of every call. - if got := fns["grouped"].Params; !reflect.DeepEqual(got, []string{"a", "b", "recordEvent", "mintVersion"}) { - t.Errorf("grouped.Params = %q, want each name of a grouped parameter list counted separately", got) - } - if got := fns["grouped"].ParamIndex("mintVersion"); got != 3 { - t.Errorf("grouped.ParamIndex(mintVersion) = %d, want 3", got) - } - if got := fns["ungated"].ParamIndex("mintVersion"); got != -1 { - t.Errorf("ungated.ParamIndex(mintVersion) = %d, want -1", got) - } - if got := fns["Mixed"].Calls; !reflect.DeepEqual(got, []Call{{Name: "helper", Args: []string{"false"}}, {Name: "helper", Args: []string{"true"}}}) { - t.Errorf("Mixed.Calls = %+v, want both helper calls with their literal argument", got) - } - for _, name := range []string{"ShadowedLocal", "ShadowedParam", "ShadowedInClosure"} { - if got := fns[name].Calls; !reflect.DeepEqual(got, []Call{{Name: "helper", Args: []string{""}}}) { - t.Errorf("%s.Calls = %+v, want the shadowed false reduced to \"\" rather than read as the literal", name, got) - } - } - - for _, tc := range []struct { - caller, callee string - want bool - }{ - {"SwitchedOff", "helper", true}, - {"SwitchedOffGrouped", "grouped", true}, - {"MintsItself", "helper", true}, // the helper edge is off; what MintsItself does on its own is the fixpoint's business - {"SwitchedOn", "helper", false}, - {"Variable", "helper", false}, - {"Forwards", "helper", false}, - {"Mixed", "helper", false}, - {"TooShort", "helper", false}, - {"NoGate", "ungated", false}, - {"NeverCalls", "helper", false}, - {"SwitchedOff", "missing", false}, - {"ShadowedLocal", "helper", false}, - {"ShadowedParam", "helper", false}, - {"ShadowedInClosure", "helper", false}, - } { - if got := CallsOnlyWithLiteralFalse(fns, fns[tc.caller], tc.callee, "mintVersion"); got != tc.want { - t.Errorf("CallsOnlyWithLiteralFalse(%s -> %s) = %v, want %v", tc.caller, tc.callee, got, tc.want) - } - } - - // End to end: following only the edges the predicate leaves live credits - // exactly the callers that can reach the seam, and the predicate is what - // makes the difference — the plain edge set the journal guard follows - // still credits the switched-off callers. - seed := func(f *FuncInfo) bool { return f.CallsAnyOf(map[string]bool{"seam": true}) } - plain := Fixpoint(fns, seed, func(f *FuncInfo) []string { return f.AllCallNames() }) - gated := Fixpoint(fns, seed, func(f *FuncInfo) []string { - var live []string - for _, callee := range f.AllCallNames() { - if !CallsOnlyWithLiteralFalse(fns, f, callee, "mintVersion") { - live = append(live, callee) - } - } - return live - }) - for name, want := range map[string]bool{ - "SwitchedOff": false, "SwitchedOffGrouped": false, "NeverCalls": false, "seam": false, - "SwitchedOn": true, "Variable": true, "Forwards": true, "Mixed": true, "MintsItself": true, - "TooShort": true, "NoGate": true, "helper": true, "grouped": true, "ungated": true, - "ShadowedLocal": true, "ShadowedParam": true, "ShadowedInClosure": true, - } { - if gated[name] != want { - t.Errorf("gated fixpoint reaches seam via %s = %v, want %v", name, gated[name], want) - } - } - for _, name := range []string{"SwitchedOff", "SwitchedOffGrouped"} { - if !plain[name] { - t.Errorf("plain fixpoint does not reach seam via %s; the probe no longer shows the gate doing any work", name) - } - } -} - -// TestPackageLevelShadowCrossesFiles pins the half of shadow detection the -// parser cannot do alone. It resolves identifiers per file, so a use in one -// file of a false that another file declares at package level is unresolved, -// exactly like the predeclared one; ParsePackage folds every file's -// package-level declarations in, so such a use is no switch-off either — and, -// the shadow being package-wide, neither is any other false in the package. -func TestPackageLevelShadowCrossesFiles(t *testing.T) { - dir := t.TempDir() - files := map[string]string{ - "a.go": `package probe - -func seam() {} - -func helper(mintVersion bool) { - if mintVersion { - seam() - } -} - -func LooksSwitchedOff() { helper(false) } -`, - "b.go": `package probe - -var false = true -`, - } - for name, src := range files { - if err := os.WriteFile(filepath.Join(dir, name), []byte(src), 0o600); err != nil { - t.Fatal(err) - } - } - fns, err := ParsePackage(dir) - if err != nil { - t.Fatalf("ParsePackage: %v", err) - } - if got := fns["LooksSwitchedOff"].Calls; !reflect.DeepEqual(got, []Call{{Name: "helper", Args: []string{""}}}) { - t.Errorf("LooksSwitchedOff.Calls = %+v, want the false shadowed by another file reduced to \"\"", got) - } - if CallsOnlyWithLiteralFalse(fns, fns["LooksSwitchedOff"], "helper", "mintVersion") { - t.Error("CallsOnlyWithLiteralFalse(LooksSwitchedOff -> helper) = true; a false shadowed at package level in another file read as the switch-off") - } -} - -// TestResolve pins the name-based resolution every edge and every switch-off -// goes through: a bare name denotes the free function of that name and every -// method of that name — free function first, then methods in key order, so a -// guard's report of an ambiguity is stable. -func TestResolve(t *testing.T) { - fns := map[string]*FuncInfo{ - "helper": {Name: "helper"}, - "svc.helper": {Recv: "svc", Name: "helper"}, - "db.helper": {Recv: "db", Name: "helper"}, - "svc.other": {Recv: "svc", Name: "other"}, - } - for name, want := range map[string][]string{ - "helper": {"helper", "db.helper", "svc.helper"}, - "other": {"svc.other"}, - "missing": nil, - } { - if got := Resolve(fns, name); !reflect.DeepEqual(got, want) { - t.Errorf("Resolve(%q) = %q, want %q", name, got, want) - } - } -} diff --git a/internal/storage/kvkeys/BUILD.bazel b/internal/storage/kvkeys/BUILD.bazel deleted file mode 100644 index 0c82c0ad2..000000000 --- a/internal/storage/kvkeys/BUILD.bazel +++ /dev/null @@ -1,88 +0,0 @@ -load("@rules_go//go:def.bzl", "go_library", "go_test") - -go_library( - name = "kvkeys", - srcs = ["kvkeys.go"], - importpath = "github.com/steveyegge/beads/internal/storage/kvkeys", - visibility = ["//:__subpackages__"], -) - -go_test( - name = "kvkeys_test", - srcs = ["kvkeys_test.go"], - embed = [":kvkeys"], -) - -# --- begin go_srcs (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) --- - -filegroup( - name = "go_srcs", - srcs = glob( - ["**/*.go"], - allow_empty = True, - ), - visibility = ["//:__subpackages__"], -) - -# --- end go_srcs --- - -# --- begin repo_files (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) --- - -filegroup( - name = "repo_doc_files", - srcs = glob( - [ - "**/*.md", - "**/*.mdx", - ], - allow_empty = True, - exclude = ["**/node_modules/**"], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_go_srcs", - srcs = glob( - ["**/*.go"], - allow_empty = True, - exclude = [ - "**/*_test.go", - "**/node_modules/**", - ], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_go_test_srcs", - srcs = glob( - ["**/*_test.go"], - allow_empty = True, - exclude = ["**/node_modules/**"], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_other_files", - srcs = glob( - ["**"], - exclude = [ - "**/*.db", - "**/*.exe", - "**/*.go", - "**/*.md", - "**/*.mdx", - "**/*.out", - "**/*.prof", - "**/*.pyc", - "**/*.test", - "**/__pycache__/**", - "**/node_modules/**", - ], - ), - visibility = ["//:__subpackages__"], -) - -# --- end repo_files --- diff --git a/internal/storage/leg_contract_wiring_test.go b/internal/storage/leg_contract_wiring_test.go index 960efc16b..b48240142 100644 --- a/internal/storage/leg_contract_wiring_test.go +++ b/internal/storage/leg_contract_wiring_test.go @@ -12,8 +12,6 @@ import ( "sort" "strings" "testing" - - "github.com/steveyegge/beads/internal/testutil/bazeltest" ) // conformancePackage is the import path whose Run entrypoints each leg wires. @@ -33,217 +31,31 @@ var neverSatisfiedTags = map[string]bool{"ignore": true, "never": true} const importerOneAccessorWaiverReason = "Importer has one accessor (uow.ImporterSource); " + "the store legs run bd import through the raw seam and implement no Importer role" -// asOfReadNoHTTPWireWaiverReason covers the five R7.1 as-of read contracts -// (backend/conformance/versioned_read_contract.go). Unlike the -// participation-generation cases, which run on this leg because their subject -// is a create or an update the client can send -// (internal/httpclient/served_participation_generation_test.go), the subject -// here is the read itself, AsOfReadFixture.Resolve, and it has no form on the -// wire: -// - R7.1's read is issueops.AsOfReadInTx. No publicops role reaches it and no -// wire operation serves it, so there is nothing for the client to call. -// - The wire carries neither selector (an instant, or a previously served -// version address) nor the gone/unknown refusal the read answers with. -// - The client's only point-in-time read, storage.DoltStorage.AsOf, is a -// Dolt commit-ref read, not R7.1, and it is on the client's unsupported -// allowlist (D8: v0 publishes no history surface). -// -// Binding Resolve to the reference store behind the served env would run the -// cases green, but as a property of that store, never of the client; the store -// legs already pin it. The waiver stands until the wire publishes an as-of -// read. -const asOfReadNoHTTPWireWaiverReason = "the read under test has no form on the http wire: R7.1's as-of read " + - "(issueops.AsOfReadInTx) is reached by no publicops role and no wire operation, the wire carries neither an " + - "instant/version-address selector nor a gone/unknown refusal, and the client's only point-in-time read, " + - "DoltStorage.AsOf, is a Dolt commit-ref read on its unsupported allowlist (D8)" - // The waivers of the three legs registered here. Each is registered beside its // leg rather than written into one literal, so a leg registered elsewhere // brings its waivers the same way; see unwiredContractEntrypoints. func init() { registerContractLegWaivers("dolt", map[string]string{ - "RunImporterRejectsAStaleRowAndNamesIt": importerOneAccessorWaiverReason, - "RunImporterReportsTheAbsentTargetItDroppedOnce": importerOneAccessorWaiverReason, - "RunImporterWiresTheCrossPlaneEdgeBetweenItsRows": importerOneAccessorWaiverReason, - "RunImporterReportsTheCycleEdgeItDropped": importerOneAccessorWaiverReason, - "RunBootstrapperRecordsExactlyOneHistoryEntry": bootstrapSplitWaiverReason, + "RunImporterRejectsAStaleRowAndNamesIt": importerOneAccessorWaiverReason, + "RunImporterReportsTheAbsentTargetItDroppedOnce": importerOneAccessorWaiverReason, + "RunImporterReportsTheCrossPlaneEdgeItDropped": importerOneAccessorWaiverReason, + "RunImporterReportsTheCycleEdgeItDropped": importerOneAccessorWaiverReason, + "RunBootstrapperRecordsExactlyOneHistoryEntry": bootstrapSplitWaiverReason, }) registerContractLegWaivers("embeddeddolt", map[string]string{ - "RunImporterRejectsAStaleRowAndNamesIt": importerOneAccessorWaiverReason, - "RunImporterReportsTheAbsentTargetItDroppedOnce": importerOneAccessorWaiverReason, - "RunImporterWiresTheCrossPlaneEdgeBetweenItsRows": importerOneAccessorWaiverReason, - "RunImporterReportsTheCycleEdgeItDropped": importerOneAccessorWaiverReason, - "RunBootstrapperRecordsExactlyOneHistoryEntry": bootstrapSplitWaiverReason, + "RunImporterRejectsAStaleRowAndNamesIt": importerOneAccessorWaiverReason, + "RunImporterReportsTheAbsentTargetItDroppedOnce": importerOneAccessorWaiverReason, + "RunImporterReportsTheCrossPlaneEdgeItDropped": importerOneAccessorWaiverReason, + "RunImporterReportsTheCycleEdgeItDropped": importerOneAccessorWaiverReason, + "RunBootstrapperRecordsExactlyOneHistoryEntry": bootstrapSplitWaiverReason, }) registerContractLegWaivers("uow", map[string]string{ "RunBootstrapperRecordsNoHistoryEntryOfItsOwn": bootstrapSplitWaiverReason, "RunIssueOperationsCreateReverseNonBlockingStagesConcreteTables": stagingWaiverReason, "RunIssueOperationsCreateParentChildRecomputesWaitsForClosure": stagingWaiverReason, }) - registerContractLegWaivers("http", map[string]string{ - "RunAsOfReadIsCompleteOnlyForTheAnsweringStoresHoldings": asOfReadNoHTTPWireWaiverReason, - "RunAsOfReadRefusesAsGoneOrUnknownNeverSubstitutingAnotherVersion": asOfReadNoHTTPWireWaiverReason, - "RunAsOfReadReportsTheAddressItServed": asOfReadNoHTTPWireWaiverReason, - "RunAsOfReadReturnsTheLatestVersionAtOrBeforeTStrictlyExcludingLater": asOfReadNoHTTPWireWaiverReason, - "RunAsOfReadSelectorAcceptsEitherAnInstantOrAVersionAddress": asOfReadNoHTTPWireWaiverReason, - - "RunCycleDetectorIncludeTracksFindsTheMoleculeRootShape": cycleTracksWireGapWaiverReason, - "RunCycleDetectorIncludeTracksIgnoresAPureTracksLoop": cycleTracksWireGapWaiverReason, - "RunCycleDetectorIncludeTracksWalksEachEdgeInItsStoredDirection": cycleTracksWireGapWaiverReason, - - "RunBootstrapperIdentifiesAFreshSubstrate": bootstrapperPermanentlyUnservableWaiverReason, - "RunBootstrapperLeavesTheSubstrateUntouchedWhenItCannotComplete": bootstrapperPermanentlyUnservableWaiverReason, - "RunBootstrapperRecordsExactlyOneHistoryEntry": bootstrapperPermanentlyUnservableWaiverReason, - "RunBootstrapperRecordsNoHistoryEntryOfItsOwn": bootstrapperPermanentlyUnservableWaiverReason, - "RunBootstrapperRefusesASubstrateCarryingOnlyAPrefix": bootstrapperPermanentlyUnservableWaiverReason, - "RunBootstrapperRefusesASubstrateCarryingOnlyAProjectID": bootstrapperPermanentlyUnservableWaiverReason, - "RunBootstrapperRefusesAnIdentifiedSubstrate": bootstrapperPermanentlyUnservableWaiverReason, - "RunBootstrapperRefusesAnInvalidRequestWithoutWriting": bootstrapperPermanentlyUnservableWaiverReason, - "RunBootstrapperStoresThePrefixWithoutItsTrailingHyphen": bootstrapperPermanentlyUnservableWaiverReason, - - "RunImporterRejectsAStaleRowAndNamesIt": importerOneAccessorWaiverReason, - "RunImporterReportsTheAbsentTargetItDroppedOnce": importerOneAccessorWaiverReason, - "RunImporterWiresTheCrossPlaneEdgeBetweenItsRows": importerOneAccessorWaiverReason, - "RunImporterReportsTheCycleEdgeItDropped": importerOneAccessorWaiverReason, - - "RunACarriedAddressKeepsResolvingAcrossLaterBumps": retentionEpochNoRoleWaiverReason, - "RunADestructiveOperationEnumeratesAffectedAddressesFirst": retentionEpochNoRoleWaiverReason, - "RunAHoldPreventsRemovalAndReportsInRetainedBounds": retentionEpochNoRoleWaiverReason, - "RunALostAddressStaysGoneAcrossASchemeChange": retentionEpochNoRoleWaiverReason, - "RunAStoreThatRemovesStateStillAnswersGoneDurably": retentionEpochNoRoleWaiverReason, - "RunAStoreWithNoLineageKnowledgeAnswersUnknownNotGone": retentionEpochNoRoleWaiverReason, - "RunAnAddressNeverResolvesToADifferentState": retentionEpochNoRoleWaiverReason, - "RunAnEpochBumpIsTriggeredOnlyByRestoreReinitOrSchemeChange": retentionEpochNoRoleWaiverReason, - "RunEpochBumpVoidsOnlyAddressesOfVersionsNoLongerServed": retentionEpochNoRoleWaiverReason, - "RunErasureMintsACorrectedVersionRatherThanEditingInPlace": retentionEpochNoRoleWaiverReason, - "RunEveryRetentionAnswerNamesItsProducingStore": retentionEpochNoRoleWaiverReason, - "RunForcingAHeldRemovalRecordsWhoWhenWhy": retentionEpochNoRoleWaiverReason, - "RunGoneIsDistinguishableFromUnknown": retentionEpochNoRoleWaiverReason, - "RunRemovalLeavesTheAddressAbleToAnswer": retentionEpochNoRoleWaiverReason, - "RunRemovalNeverReassignsASurvivingAddress": retentionEpochNoRoleWaiverReason, - "RunRemovalReasonIsRetentionErasureOrReorganizationDistinctly": retentionEpochNoRoleWaiverReason, - "RunRemovalReportsTheSurvivingRetainedWindow": retentionEpochNoRoleWaiverReason, - "RunTokenSchemeChangeCarriesLiveAddressesAndOnlyThose": retentionEpochNoRoleWaiverReason, - - "RunAdvisoryLeaseAloneDoesNotPreventTheInvariantViolation": crossRecordInvariantNoRoleWaiverReason, - "RunCrossRecordInvariantSurvivesTwoPassingPerRecordGuards": crossRecordInvariantNoRoleWaiverReason, - "RunInvariantRefusalNamesTheViolatedInvariant": crossRecordInvariantNoRoleWaiverReason, - "RunLeaseAcquisitionRecordsNoHistoryEntry": crossRecordInvariantNoRoleWaiverReason, - "RunMaximumEndpointMultiplicityIsAStoreInvariantNotACAS": crossRecordInvariantNoRoleWaiverReason, - "RunMemoryKeyAliasUniquenessIsAStoreInvariantNotACAS": crossRecordInvariantNoRoleWaiverReason, - "RunStoreInvariantTransactionScopesExactlyTheSpanningRecords": crossRecordInvariantNoRoleWaiverReason, - - "RunVersionReconcilerAdvancesBothMarkersOnAnUpgrade": versionReconcilerInitVerifierPermanentlyUnservableWaiverReason, - "RunVersionReconcilerCatchesUpToTheHighWaterMark": versionReconcilerInitVerifierPermanentlyUnservableWaiverReason, - "RunVersionReconcilerLeavesTheMarkersStandingWhenItCannotComplete": versionReconcilerInitVerifierPermanentlyUnservableWaiverReason, - "RunVersionReconcilerRecordsAWorkspaceWithNoMarkers": versionReconcilerInitVerifierPermanentlyUnservableWaiverReason, - "RunVersionReconcilerRecordsNoHistory": versionReconcilerInitVerifierPermanentlyUnservableWaiverReason, - "RunVersionReconcilerRefusesADowngradeWithoutAnError": versionReconcilerInitVerifierPermanentlyUnservableWaiverReason, - "RunVersionReconcilerRefusesAVersionBelowTheHighWaterMark": versionReconcilerInitVerifierPermanentlyUnservableWaiverReason, - "RunVersionReconcilerRefusesAnEmptyVersion": versionReconcilerInitVerifierPermanentlyUnservableWaiverReason, - "RunVersionReconcilerTreatsTheSameVersionAsANoOp": versionReconcilerInitVerifierPermanentlyUnservableWaiverReason, - "RunInitVerifierAnswersEmptyForAnUnidentifiedSubstrate": versionReconcilerInitVerifierPermanentlyUnservableWaiverReason, - "RunInitVerifierReportsAFailedReadAsAnError": versionReconcilerInitVerifierPermanentlyUnservableWaiverReason, - "RunInitVerifierReportsAPartialIdentityAsItStands": versionReconcilerInitVerifierPermanentlyUnservableWaiverReason, - "RunInitVerifierWritesNothing": versionReconcilerInitVerifierPermanentlyUnservableWaiverReason, - - "RunIssueOperationsUpdateClosedFieldsMatchClose": issueOperationsUpdateRawSeamWaiverReason, - "RunIssueOperationsUpdateRawMetadataTakesTheFunnelsValueShapes": issueOperationsUpdateRawSeamWaiverReason, - "RunIssueOperationsUpdateStampsStartedAtOnceOnTheFirstInProgress": issueOperationsUpdateRawSeamWaiverReason, - - "RunLifecycleUpdateExpectedVersionSingleWinnerWithDisjointColumnsUnderConcurrency": issuePatchFieldsNotOnUpdateWireWaiverReason, - "RunLifecycleUpdateExpectedVersionSingleWinnerAcrossUpdateAndCloseUnderConcurrency": issuePatchFieldsNotOnUpdateWireWaiverReason, - - "RunParticipationGenerationNoHistoryIssueCreateDeclaresParticipation": participationGenerationWireGapWaiverReason, - "RunParticipationGenerationUnflaggedWispCreateMintsNothing": participationGenerationWireGapWaiverReason, - "RunParticipationGenerationWispFlagClearMintsNothing": participationGenerationWireGapWaiverReason, - "RunParticipationGenerationWispRootSquashMintsNothing": participationGenerationWireGapWaiverReason, - }) } -// cycleTracksWireGapWaiverReason covers the one CycleDetector widening option -// the HTTP wire does not carry at all. -// -// issueops.DetectCyclesRequest.IncludeTracks is a WIDENING field consumed -// today only by `bd dep cycles` in-process. internal/httpapi/cycles.go calls -// DetectCycles with a bare issueops.DetectCyclesRequest{} — it decodes no -// query parameter for it — and internal/httpclient's httpCycleDetector reads -// the field and, finding no wire parameter to carry it, REFUSES the request -// with a typed, ledger-cited error (encode.RefusedError via refuse(), citing -// "L-cycles-tracks") rather than silently answering the narrower default -// walk. Neither side of the wire has any plumbing for this field, so the -// three IncludeTracks cases cannot be satisfied over HTTP — the client fails -// loudly instead of wiring a narrower answer, which is why these three remain -// unwired rather than run-and-skipped. -// -// No shipped slice adds a query parameter, spec capability token, or -// handshake entry for this option — see "L-cycles-tracks" in the in-repo -// divergence ledger, engdocs/design/http-divergence-ledger.md, which records -// it as a refuse row rather than a wired one. Closing this gap is a new -// slice — a query parameter plus spec/capability plumbing on both ends — not -// a missing test line, so the waiver stays until that slice exists. -const cycleTracksWireGapWaiverReason = "issueops.DetectCyclesRequest.IncludeTracks has no wire plumbing on " + - "either side of internal/httpapi or internal/httpclient today (cycles.go always calls DetectCycles with a " + - "zero-value request; the client's DetectCycles refuses the request via a typed, ledger-cited error — see " + - "\"L-cycles-tracks\" — rather than silently answering the narrower walk); no slice through S13 adds a wire " + - "parameter for it, so closing this is a new, not-yet-numbered slice rather than a missing test line" - -// retentionEpochNoRoleWaiverReason covers the eighteen RetentionFixture and -// EpochFixture cases (backend/conformance/retention_epoch_contract.go). -// All but four of the participation-generation cases run on this leg because -// their subject is a create or an update the client can send -// (internal/httpclient/served_participation_generation_test.go; those four -// stay waived under participationGenerationWireGapWaiverReason). These -// cases are different: every hook of these two fixtures IS the operation -// under test, and none has a form on the wire: -// - EpochFixture's hooks (CurrentEpoch, BumpEpoch, MintUnderEpoch, -// StillServes, Resolve, CurrentAddressFor, LoseVersion) are the R20 epoch -// operations themselves: methods on the concrete *DoltStore and -// *EmbeddedDoltStore over issueops' *InTx bodies, on no storage -// interface, no publicops role and no wire operation. Every call path -// into them starts in a test: the methods are called only from each -// store leg's own _test.go files (its EpochFixture, which the -// backend/conformance contracts drive, and its epoch tests), and the -// *InTx bodies only from those methods, from each other and from tests -// (the uow leg's fixture, through a test-only type assertion, and -// issueops' and the dolt leg's own tests). No command, role or server -// handler reaches them, so no client mutation touches epoch state either. -// - RetentionFixture's hooks (Resolve, Remove, Hold, ForceRemove, Erase, -// Mint) are retention operations no backend implements yet: every -// store leg wires them nil and its cases skip, and the wire has no -// removal, hold, erasure or gone/unknown resolve to bind them to. -// -// Binding the hooks to the reference store behind the served env would run -// the epoch cases green, but as a property of that store, never of the -// client; the store legs already pin it. The waiver stands until retention or -// epoch state gets a publicops role and a wire operation of its own, which -// would be a new role, not a missing test line. -const retentionEpochNoRoleWaiverReason = "every RetentionFixture/EpochFixture hook is the operation under test and " + - "none has a form on the http wire: the R20 epoch operations (CurrentEpoch, BumpEpoch, MintUnderEpoch, " + - "StillServes, Resolve, CurrentAddressFor, LoseVersion) are concrete-store methods on no storage interface, " + - "publicops role or wire operation, reached only from tests (each store leg's fixture and epoch tests call the " + - "methods, the uow fixture calls the issueops bodies through a test-only type assertion), and the retention " + - "hooks (Resolve, Remove, Hold, ForceRemove, Erase, Mint) are implemented by no backend and have no wire " + - "removal, hold, erasure or resolve; binding them to the served env's reference store would test that store, " + - "not the client" - -// crossRecordInvariantNoRoleWaiverReason covers the seven CrossRecordInvariantFixture -// cases, for the same shape of reason as retentionEpochNoRoleWaiverReason. -// -// CrossRecordInvariantFixture (backend/conformance/cross_record_invariant_contract.go) -// is likewise raw hooks with no publicops field — GuardedWrite, -// EnforceCrossRecordInvariant, AcquireAdvisoryLease, -// CountHistoryForSubject, MemoryKeyAliasWrite, GraphEdgeWrite — that assert a -// store's OWN invariant enforcement spanning more than one record at once, -// the kind of guarantee a storage engine's own transaction boundary gives, -// not a wire operation it exposes. dolt, embeddeddolt and uow wire it as the -// engines that hold that boundary; the http leg has none of its own to test. -const crossRecordInvariantNoRoleWaiverReason = "CrossRecordInvariantFixture carries raw per-record/cross-record " + - "storage hooks (GuardedWrite, EnforceCrossRecordInvariant, AcquireAdvisoryLease, CountHistoryForSubject, " + - "MemoryKeyAliasWrite, GraphEdgeWrite) with no publicops role behind them, asserting a storage engine's own " + - "transaction boundary rather than something exposed over a wire; the http leg has no transaction boundary " + - "of its own for this to test" - // bootstrapSplitWaiverReason covers the one pair of entrypoints that is a // RATIFIED PER-LEG SPLIT rather than a gap. // @@ -264,24 +76,6 @@ const bootstrapSplitWaiverReason = "the bootstrap history contracts are a ratifi "legs pin zero because `bd init` commits the identity itself, the unit-of-work leg pins one because the " + "proxied route has no other commit point; each leg wires its own half and the other half is not its promise" -// bootstrapperPermanentlyUnservableWaiverReason covers the nine Bootstrapper -// contracts: PERMANENTLY unservable over http, not merely unwired yet. -// -// internal/httpclient/accessors.go names Bootstrapper (alongside VersionReconciler -// and InitVerifier) as one of the THREE accessors still on the generated -// refusing shell for one reason: "PERMANENTLY UNSERVABLE — no wire operation -// and none coming." `bd init` runs its substrate-identification walk entirely -// client-side before any server exists to dial, so there is no v0 operation for -// an http accessor to bind to and none is coming — closing this gap is not a -// missing line, it is a wire operation nobody has designed. This waiver covers -// Bootstrapper only; VersionReconciler and InitVerifier share the same fate in -// the same comment but are out of scope for the change that added this waiver -// and remain counted against the ceiling until their own reviewed change -// names them too. -const bootstrapperPermanentlyUnservableWaiverReason = "internal/httpclient/accessors.go names Bootstrapper " + - "PERMANENTLY UNSERVABLE over http — no wire operation and none coming — because `bd init` resolves the " + - "substrate identity entirely client-side, before any server exists to dial" - // stagingWaiverReason is why the two staging contracts stop at the two // store-backed legs. // @@ -303,123 +97,6 @@ const stagingWaiverReason = "the unit-of-work provider commits every unit of wor "caller-held working set to stage into and no Commit hook to close one; these two cases assert what a " + "create sweeps into a commit the caller opened" -// versionReconcilerInitVerifierPermanentlyUnservableWaiverReason covers the -// thirteen VersionReconciler and InitVerifier contracts: PERMANENTLY -// unservable over http, not merely unwired yet. -// -// internal/httpclient/accessors.go names VersionReconciler and InitVerifier, -// alongside Bootstrapper, as the three accessors still on the generated -// refusing shell for one reason: "PERMANENTLY UNSERVABLE — no wire operation -// and none coming." Both share Bootstrapper's own fate and (InitVerifier -// shares BootstrapperFixture outright) the same cause: `bd init`'s substrate -// identification and the schema-version reconciliation it drives both run -// entirely client-side, against the on-disk substrate a server-mediated -// caller never touches directly, before or instead of any server existing to -// dial. There is no v0 operation for an http accessor to bind to and none is -// coming. This waiver was left out of the change that added -// bootstrapperPermanentlyUnservableWaiverReason (that change's own comment -// says so) and is added by this one. -const versionReconcilerInitVerifierPermanentlyUnservableWaiverReason = "internal/httpclient/accessors.go names " + - "VersionReconciler and InitVerifier PERMANENTLY UNSERVABLE over http, the same fate as Bootstrapper — no " + - "wire operation and none coming — because both run entirely client-side against the on-disk substrate " + - "(schema-version reconciliation and `bd init`'s own identity read) before or instead of any server existing " + - "to dial" - -// issueOperationsUpdateRawSeamWaiverReason covers the three -// IssueOperationsStagingFixture cases whose SUBJECT is the untyped UpdateRaw -// funnel itself, not merely a fixture that seeds through it. -// -// storage.UpdateIssue is on this client's unsupported allowlist by design -// (D8: re-routing a raw front door would put a second spelling of the edit -// beside the role's), so served_issue_operations_test.go binds UpdateRaw only -// to the REFERENCE store's own untyped funnel (env.reference.UpdateIssue), -// never to the client — see that file's own doc comment. That binding is -// sound for every case actually wired there because none of them takes the -// funnel AS ITS SUBJECT; these three do (RunIssueOperationsUpdateClosedFieldsMatchClose -// drives a reopen/reclose through it, RunIssueOperationsUpdateStampsStartedAtOnceOnTheFirstInProgress -// and RunIssueOperationsUpdateRawMetadataTakesTheFunnelsValueShapes assert -// what it writes). Wiring them against the reference store's funnel would -// assert a property of the store the server happens to share a process with, -// never of the client under test — the same kind of empty assertion binding -// UpdateRaw to the client would be in the other direction. There is no -// client-side UpdateRaw to bind instead: that is D8's allowlist decision, not -// a gap. -const issueOperationsUpdateRawSeamWaiverReason = "these three cases take the untyped UpdateRaw funnel itself as " + - "their subject; storage.UpdateIssue is on this client's unsupported allowlist by design (D8), so there is no " + - "client-side UpdateRaw to bind, and binding the reference store's own funnel instead (as this leg's other " + - "IssueOperationsStagingFixture cases do, to SEED preconditions) would assert a property of the store the " + - "server happens to share a process with, never of the client under test" - -// issuePatchFieldsNotOnUpdateWireWaiverReason covers the two -// LifecycleUpdateFixture concurrency cases whose racers patch -// IssuePatch.SpecID, .AwaitID, and/or .Owner against updateIssue. -// -// THIS IS PENDING, NOT PERMANENT: it is unbuilt work waiting on a wire -// change, not a boundary this leg can never cross. S4 wired the other two -// ExpectedVersion concurrency races (the plain Update race over -// Priority/Notes, and the Close race) against the real served HTTP harness, -// and both pass: precondition_failed decodes to storage.ErrVersionMismatch -// exactly as the local legs report it. These two cases cannot follow -// because SOME of their racers patch fields updateIssue refuses outright -// rather than ever sending — not a race outcome, a refusal before any -// request leaves the client. internal/httpclient/encode/ledger.go already -// names this gap per field (W-IssuePatch.SpecID, W-IssuePatch.Owner, D8 -// refuse-not-drop): neither IssuePatchBody nor -// ApplyPatchBody publishes spec_id at all, and Owner is excluded -// from IssuePatchBody specifically (ApplyPatchBody — issues:batchApply's body -// — does carry it, which is a different operation from updateIssue). Closing -// this gap means publishing those fields on IssuePatchBody, a wire change to -// the write role itself, not a client-side wiring exercise — tracked as a -// follow-up slice, S4b: publish owner/spec_id/await_id on the wire. await_id -// is published on both patch bodies behind issues.update.gates and sent by -// this client (W-IssuePatch.AwaitID retired), so only -// spec_id and owner remain S4b's. Until they land, these two contracts stay -// off this leg rather than asserting a race outcome against a request some of -// its racers never send. -const issuePatchFieldsNotOnUpdateWireWaiverReason = "PENDING, not permanent (tracked as S4b: publish " + - "owner/spec_id/await_id on the wire): two of the four ExpectedVersion concurrency contracts race " + - "IssuePatch.SpecID/.AwaitID/.Owner edits against updateIssue, and SpecID and Owner are refused outright by " + - "this client (W-IssuePatch.SpecID, W-IssuePatch.Owner in " + - "internal/httpclient/encode/ledger.go, D8 refuse-not-drop) rather than ever reaching the server, so the race " + - "never happens over this wire; await_id is sent behind issues.update.gates, and " + - "closing the rest of the gap means publishing spec_id and owner on IssuePatchBody, which is a wire change " + - "to the write role (S4b), not something this leg's test wiring can bind around today" - -// participationGenerationWireGapWaiverReason covers the four -// ParticipationGenerationFixture cases whose mutation has no form on this -// wire. The other cases run on the http leg -// (internal/httpclient/served_participation_generation_test.go): the client -// creates and updates, and the fixture flips the reference store's flag and -// reads its raw columns, as served_dualwrite_history_test.go does. -// -// Each waived case needs a mutation the client cannot send: -// - WispFlagClearMintsNothing clears a wisp's flag IN PLACE with the untyped -// UpdateIssue map {"wisp": false}. storage.UpdateIssue is on this client's -// unsupported allowlist (D8), and IssuePatch has no in-place flag member: -// its Persistence member moves the row between tables, which the -// wisp-arrival cases already run here. -// - WispRootSquashMintsNothing runs the close and that flag clear in ONE -// transaction. RunInTransaction is on the unsupported allowlist too, and -// the wire publishes no squash operation. -// - UnflaggedWispCreateMintsNothing creates an unflagged row in the wisps -// table through import's WispPlaneOverride = &true. The wire drops that -// member (roleIgnoredCreateIssueMembers in batchcreator.go), so an -// unflagged create lands in the issues table. -// - NoHistoryIssueCreateDeclaresParticipation has a no-history ISSUES-table -// create as its subject (WispPlaneOverride = &false). With that member -// dropped, a client create carrying no_history routes to the wisps table -// (TableRouting). Seeding the row through the reference store instead -// would make the create under test the store's own, a property of the -// store the server happens to share a process with, never of the client. -const participationGenerationWireGapWaiverReason = "PENDING, not permanent (no slice tracks it yet; each case runs " + - "on this leg once the wire publishes its mutation: an in-place wisp flag clear, a squash operation, or a create " + - "that names its table plane the way import's WispPlaneOverride does): " + - "the mutation this case needs has no form on the http wire: " + - "an in-place wisp flag clear is an untyped UpdateIssue map and the squash a RunInTransaction, both on this " + - "client's unsupported allowlist (D8), and IssuePatch.Persistence moves the row rather than clearing the flag in " + - "place; an unflagged wisps-table create and a no-history issues-table create both need WispPlaneOverride, " + - "which the wire drops (roleIgnoredCreateIssueMembers), so the first lands in issues and the second in wisps" - // TestEveryLegWiresEveryRoleContract fails when a backend leg skips a role // contract the conformance package exports. // @@ -587,9 +264,6 @@ func inspectLegWiring(t *testing.T, dir string, entrypoints []string, waived map // repositoryRoot locates the module root from this file's own path. func repositoryRoot(t *testing.T) string { t.Helper() - if root := bazeltest.OverrideRoot(); root != "" { - return root - } _, thisFile, _, ok := runtime.Caller(0) if !ok { t.Fatal("runtime.Caller failed") @@ -688,27 +362,6 @@ func finalParamTypeName(fn *ast.FuncDecl) string { // file naming every entrypoint would otherwise satisfy this lock with source no // build compiles. Ordinary constraints — cgo, integration — are counted, // because a contract wired behind one is still wired. -// -// A REFERENCE IS NOT A PASS, and that gap is deliberate rather than a hole in -// this check. Several contracts (RetentionFixture, EpochFixture, BootstrapperFixture, -// VersionReconcilerFixture, ...) are written so a nil hook makes the case -// t.Skip loudly, naming the hook, rather than asserting nothing silently — -// see each fixture's own doc comment. That lets a leg wire the ENTRYPOINT -// (call conformance.RunX, satisfying this static check) while leaving some -// or all of its hooks nil, which runs for real as a skip, not a pass. This -// check cannot see past the call to know which hooks a wiring supplies, so -// it counts the reference as wired either way — a leg that did this would -// read as adopted here while its own `go test -v` output says SKIP. That -// divergence is this package's to prevent by review, the same way -// divergence_citation_gate_test.go (internal/httpclient) already holds the -// served tier's own skipKnownDivergence call sites to a named, ledger-cited -// reason instead of a bare t.Skip: a reviewer reading a wiring diff that adds -// a conformance.RunX reference should expect it to pass for real, or to carry -// a waiver in unwiredContractEntrypoints instead — not to compile a skip and -// call it adopted. As of this lock's current counts, no leg does this: every -// entrypoint a leg's sources name either has every hook that entrypoint's own -// cases need, or the gap is a named waiver here rather than a nil hook left -// standing behind a call this check would count as wired. func conformanceEntrypointsWiredBy(t *testing.T, dir string) (map[string]bool, []string) { t.Helper() files, err := filepath.Glob(filepath.Join(dir, "*_test.go")) diff --git a/internal/storage/memoryops/BUILD.bazel b/internal/storage/memoryops/BUILD.bazel deleted file mode 100644 index 19b7f8a47..000000000 --- a/internal/storage/memoryops/BUILD.bazel +++ /dev/null @@ -1,92 +0,0 @@ -load("@rules_go//go:def.bzl", "go_library", "go_test") - -go_library( - name = "memoryops", - srcs = ["memories.go"], - importpath = "github.com/steveyegge/beads/internal/storage/memoryops", - visibility = ["//:__subpackages__"], - deps = [ - "//internal/storage/issueops", - "//internal/storage/kvkeys", - ], -) - -go_test( - name = "memoryops_test", - srcs = ["memories_test.go"], - embed = [":memoryops"], -) - -# --- begin go_srcs (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) --- - -filegroup( - name = "go_srcs", - srcs = glob( - ["**/*.go"], - allow_empty = True, - ), - visibility = ["//:__subpackages__"], -) - -# --- end go_srcs --- - -# --- begin repo_files (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) --- - -filegroup( - name = "repo_doc_files", - srcs = glob( - [ - "**/*.md", - "**/*.mdx", - ], - allow_empty = True, - exclude = ["**/node_modules/**"], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_go_srcs", - srcs = glob( - ["**/*.go"], - allow_empty = True, - exclude = [ - "**/*_test.go", - "**/node_modules/**", - ], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_go_test_srcs", - srcs = glob( - ["**/*_test.go"], - allow_empty = True, - exclude = ["**/node_modules/**"], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_other_files", - srcs = glob( - ["**"], - exclude = [ - "**/*.db", - "**/*.exe", - "**/*.go", - "**/*.md", - "**/*.mdx", - "**/*.out", - "**/*.prof", - "**/*.pyc", - "**/*.test", - "**/__pycache__/**", - "**/node_modules/**", - ], - ), - visibility = ["//:__subpackages__"], -) - -# --- end repo_files --- diff --git a/internal/storage/memoryops/memories.go b/internal/storage/memoryops/memories.go index 31dce53e5..a9c58e1ff 100644 --- a/internal/storage/memoryops/memories.go +++ b/internal/storage/memoryops/memories.go @@ -84,7 +84,9 @@ func MemoriesFromConfig(all map[string]string) map[string]string { // destroyed. // // replaced is about the ROW, not the value: a memory stored as the empty string -// reports true, just as Recall reports it found. +// reports true even though a Recall of it would report Found false. See +// memoryops.RememberResult.Replaced, which states that divergence rather than +// smoothing it over. func RememberInTx(ctx context.Context, tx *sql.Tx, key, content string) (replaced bool, err error) { storageKey := StorageKey(key) replaced, err = configRowExistsInTx(ctx, tx, storageKey) @@ -97,33 +99,31 @@ func RememberInTx(ctx context.Context, tx *sql.Tx, key, content string) (replace return replaced, nil } -// RecallInTx reads one memory in the caller's transaction and reports whether -// its row exists. A stored empty value is therefore distinct from no row. -func RecallInTx(ctx context.Context, tx *sql.Tx, key string) (value string, found bool, err error) { - storageKey := StorageKey(key) - err = tx.QueryRowContext(ctx, "SELECT value FROM config WHERE `key` = ?", storageKey).Scan(&value) - if errors.Is(err, sql.ErrNoRows) { - return "", false, nil - } - if err != nil { - return "", false, fmt.Errorf("recall memory %s: %w", storageKey, err) - } - return value, true, nil +// RecallInTx reads one memory in the caller's transaction. A key nothing stored +// answers "" with a nil error: the seam cannot tell that from a row stored +// empty, and the role's Found field is where that conflation is stated. +func RecallInTx(ctx context.Context, tx *sql.Tx, key string) (string, error) { + return issueops.GetConfigInTx(ctx, tx, StorageKey(key)) } // ForgetInTx removes one memory and returns what it held, both in the caller's // transaction — which is what makes the returned value the one that was // actually deleted rather than the one an earlier read happened to see. // +// found is Recall's found, not the row's: a memory stored as the empty string +// reports found false and IS NOT DELETED. That keeps Forget's answer and +// Recall's answer the same statement, which is what a front door printing +// "No memory with key ..." after a successful Recall would otherwise contradict. +// // The delete names the one encoded key. There is no LIKE and no prefix sweep in // it, so forgetting "a" cannot take "a-b" with it. func ForgetInTx(ctx context.Context, tx *sql.Tx, key string) (previous string, found bool, err error) { storageKey := StorageKey(key) - previous, found, err = RecallInTx(ctx, tx, key) + previous, err = issueops.GetConfigInTx(ctx, tx, storageKey) if err != nil { return "", false, err } - if !found { + if previous == "" { return "", false, nil } if err := issueops.DeleteConfigInTx(ctx, tx, storageKey); err != nil { diff --git a/internal/storage/remote.go b/internal/storage/remote.go index c92e0f28b..c2db8386c 100644 --- a/internal/storage/remote.go +++ b/internal/storage/remote.go @@ -5,10 +5,6 @@ import "context" // RemoteStore provides remote management and push/pull/fetch operations. type RemoteStore interface { AddRemote(ctx context.Context, name, url string) error - // AddRemoteWithRef is AddRemote for a git-backed remote whose Dolt data - // lives on the git ref ref instead of DefaultGitDataRef. An empty ref is - // AddRemote. - AddRemoteWithRef(ctx context.Context, name, url, ref string) error RemoveRemote(ctx context.Context, name string) error HasRemote(ctx context.Context, name string) (bool, error) ListRemotes(ctx context.Context) ([]RemoteInfo, error) diff --git a/internal/storage/remote_ref.go b/internal/storage/remote_ref.go deleted file mode 100644 index 30d10365c..000000000 --- a/internal/storage/remote_ref.go +++ /dev/null @@ -1,69 +0,0 @@ -package storage - -import ( - "encoding/json" - "fmt" - "strings" -) - -// A git-backed Dolt remote keeps its data on one git ref of the remote -// repository. Dolt records the ref as the remote parameter GitRefParam and -// falls back to DefaultGitDataRef when the parameter is absent. -const ( - GitRefParam = "git_ref" - DefaultGitDataRef = "refs/dolt/data" -) - -// EffectiveGitDataRef returns ref, or DefaultGitDataRef when ref is empty. -func EffectiveGitDataRef(ref string) string { - if ref = strings.TrimSpace(ref); ref != "" { - return ref - } - return DefaultGitDataRef -} - -// RemoteRefsMatch reports whether two git data refs name the same ref. An -// empty ref stands for DefaultGitDataRef, so an unset ref and an explicit -// default compare equal. -func RemoteRefsMatch(a, b string) bool { - return EffectiveGitDataRef(a) == EffectiveGitDataRef(b) -} - -// GitRefFromParamsJSON extracts GitRefParam from a dolt_remotes params value. -// "", "null", an empty object, and an object without the key mean the -// default ref; anything that is not a JSON object is an error rather than a -// silent default. -func GitRefFromParamsJSON(raw string) (string, error) { - raw = strings.TrimSpace(raw) - if raw == "" || raw == "null" { - return "", nil - } - var params map[string]any - if err := json.Unmarshal([]byte(raw), ¶ms); err != nil { - return "", fmt.Errorf("parse dolt_remotes params %q: %w", raw, err) - } - return GitRefFromParams(params) -} - -// GitRefFromParams extracts GitRefParam from an already-decoded dolt_remotes -// params object. It is the one reader of that key: GitRefFromParamsJSON uses -// it for the SQL params column and doltutil.PersistedRemotes for the same -// object in repo_state.json, so the two planes cannot drift. -// -// An absent key and a JSON null mean the default ref. A key that is present -// but is not a string is an error, for the same reason a params value that is -// not a JSON object is: a recorded ref bd cannot read could be a custom ref -// the mirror is pinned to, and reading it as the default would push the -// remote's data onto DefaultGitDataRef. Dolt only ever writes a string here, -// so this is the fail-closed edge of that invariant, not a live shape. -func GitRefFromParams(params map[string]any) (string, error) { - value, ok := params[GitRefParam] - if !ok || value == nil { - return "", nil - } - ref, ok := value.(string) - if !ok { - return "", fmt.Errorf("remote parameter %s is %T, not a string", GitRefParam, value) - } - return strings.TrimSpace(ref), nil -} diff --git a/internal/storage/remote_ref_test.go b/internal/storage/remote_ref_test.go deleted file mode 100644 index bcd60b719..000000000 --- a/internal/storage/remote_ref_test.go +++ /dev/null @@ -1,114 +0,0 @@ -package storage - -import ( - "strings" - "testing" -) - -// An unset ref and Dolt's default name the same ref; any other value is -// compared verbatim, so a branch and a ref outside refs/heads/ are distinct -// from each other and from the default. -func TestRemoteRefsMatch(t *testing.T) { - tests := []struct { - a, b string - want bool - }{ - {"", "", true}, - {"", DefaultGitDataRef, true}, - {DefaultGitDataRef, "", true}, - {" refs/dolt/data ", "", true}, - {"refs/heads/issue-data", "refs/heads/issue-data", true}, - {"refs/dolt/units/team-12542", "refs/dolt/units/team-12542", true}, - {"refs/heads/issue-data", "", false}, - {"refs/dolt/units/team-12542", "", false}, - {"refs/heads/issue-data", "refs/dolt/units/team-12542", false}, - {"refs/heads/issue-data", "refs/heads/issue-data-2", false}, - } - for _, tt := range tests { - if got := RemoteRefsMatch(tt.a, tt.b); got != tt.want { - t.Errorf("RemoteRefsMatch(%q, %q) = %v, want %v", tt.a, tt.b, got, tt.want) - } - } -} - -// A params object carries the ref as a string or not at all. A present value -// of any other JSON type is an error rather than the default ref: reading it as -// the default is how a remote pinned to a custom ref gets pushed onto -// refs/dolt/data, which is exactly the failure this parameter exists to -// prevent. An absent key and an explicit null are the genuine "no ref" shapes. -func TestGitRefFromParams(t *testing.T) { - for _, tt := range []struct { - name string - params map[string]any - want string - }{ - {"absent", map[string]any{"aws-region": "us-east-1"}, ""}, - {"empty object", map[string]any{}, ""}, - {"nil map", nil, ""}, - {"null", map[string]any{GitRefParam: nil}, ""}, - {"empty string", map[string]any{GitRefParam: ""}, ""}, - {"trimmed", map[string]any{GitRefParam: " refs/dolt/units/k "}, "refs/dolt/units/k"}, - {"branch", map[string]any{GitRefParam: "refs/heads/issue-data"}, "refs/heads/issue-data"}, - } { - got, err := GitRefFromParams(tt.params) - if err != nil || got != tt.want { - t.Errorf("GitRefFromParams(%s) = %q, %v; want %q, nil", tt.name, got, err, tt.want) - } - } - - for _, tt := range []struct { - name string - value any - }{ - {"array", []any{"refs/dolt/units/x"}}, - {"number", float64(7)}, - {"bool", true}, - {"object", map[string]any{"ref": "refs/dolt/units/x"}}, - } { - got, err := GitRefFromParams(map[string]any{GitRefParam: tt.value}) - if err == nil { - t.Errorf("GitRefFromParams(%s) = %q, nil; want an error, not the default ref", tt.name, got) - continue - } - if !strings.Contains(err.Error(), GitRefParam) { - t.Errorf("GitRefFromParams(%s) error %q does not name %s", tt.name, err, GitRefParam) - } - } -} - -// GitRefFromParamsJSON reads the same key out of the SQL params column, so it -// must refuse the same shapes: the two readers are one implementation. -func TestGitRefFromParamsJSONRefusesNonStringRef(t *testing.T) { - for _, raw := range []string{ - `{"git_ref":["refs/dolt/units/x"]}`, - `{"git_ref":7}`, - `{"git_ref":true}`, - `{"git_ref":{"ref":"refs/dolt/units/x"}}`, - } { - if got, err := GitRefFromParamsJSON(raw); err == nil { - t.Errorf("GitRefFromParamsJSON(%s) = %q, nil; want an error", raw, got) - } - } - for _, tt := range []struct{ raw, want string }{ - {"", ""}, - {"null", ""}, - {`{}`, ""}, - {`{"git_ref":null}`, ""}, - {`{"aws-region":"us-east-1"}`, ""}, - {`{"git_ref":"refs/heads/issue-data"}`, "refs/heads/issue-data"}, - } { - got, err := GitRefFromParamsJSON(tt.raw) - if err != nil || got != tt.want { - t.Errorf("GitRefFromParamsJSON(%s) = %q, %v; want %q, nil", tt.raw, got, err, tt.want) - } - } -} - -func TestEffectiveGitDataRef(t *testing.T) { - if got := EffectiveGitDataRef(""); got != DefaultGitDataRef { - t.Errorf("EffectiveGitDataRef(\"\") = %q, want %q", got, DefaultGitDataRef) - } - if got := EffectiveGitDataRef(" refs/dolt/units/k "); got != "refs/dolt/units/k" { - t.Errorf("EffectiveGitDataRef trims but does not rewrite: got %q", got) - } -} diff --git a/internal/storage/role_accessor_decorator_test.go b/internal/storage/role_accessor_decorator_test.go index 248f7d890..58027bb5b 100644 --- a/internal/storage/role_accessor_decorator_test.go +++ b/internal/storage/role_accessor_decorator_test.go @@ -68,11 +68,11 @@ func roleAccessorNamesOf(surface reflect.Type) (names, unclassified []string) { } // TestEveryStoreRoleAccessorIsClassified fails when DoltStorage hands out an -// interface the census cannot place. Every one of the thirty-one today is a +// interface the census cannot place. Every one of the twenty-eight today is a // facade role, so this costs nothing and closes the path where a role surface // grows a package and the census quietly stops covering it. (roleAccessorNames // is derived, so the count is prose and only this sentence goes stale — but it -// does go stale, and the last four sweeps found it behind.) +// does go stale, and the last three sweeps found it saying twenty-seven.) func TestEveryStoreRoleAccessorIsClassified(t *testing.T) { _, unclassified := roleAccessorNamesOf(reflect.TypeOf((*DoltStorage)(nil)).Elem()) for _, accessor := range unclassified { @@ -151,77 +151,69 @@ func assertRoleAccessorsAreDeclared(t *testing.T, decorator reflect.Type) { } // roleAccessorStore is a DoltStorage whose only real methods are the -// thirty-one role accessors, each answering with a distinguishable sentinel +// twenty-eight role accessors, each answering with a distinguishable sentinel // so a test can tell a decorated surface from a passed-through one. type roleAccessorStore struct { DoltStorage - lifecycle issueops.Lifecycle - reader issueops.Reader - relations issueops.Relations - edges issueops.EdgeReader - blocking issueops.BlockingAnnotator - tree issueops.TreeWalker - graphCounter issueops.GraphCounter - batchGetter issueops.BatchGetter - counter issueops.Counter - settings issueops.WorkspaceConfig - memories memoryops.Memories - versions issueops.VersionReconciler - stats issueops.StatsReporter - cycles issueops.CycleDetector - commenter issueops.Commenter - claimer issueops.ReadyClaimer - closer issueops.BatchCloser - creator issueops.BatchCreator - editor issueops.DependencyEditor - applier issueops.BatchApplier - readyCounter issueops.ReadyCounter - querier issueops.Querier - sweeper issueops.Sweeper - deleter issueops.Deleter - bootstrapper issueops.Bootstrapper - verifier issueops.InitVerifier - metadataCAS issueops.MetadataCAS - releaser issueops.Releaser - leaseReclaimer issueops.LeaseReclaimer - heartbeater issueops.Heartbeater - blockedLister issueops.BlockedLister - err error + lifecycle issueops.Lifecycle + reader issueops.Reader + relations issueops.Relations + edges issueops.EdgeReader + blocking issueops.BlockingAnnotator + tree issueops.TreeWalker + graphCounter issueops.GraphCounter + counter issueops.Counter + settings issueops.WorkspaceConfig + memories memoryops.Memories + versions issueops.VersionReconciler + stats issueops.StatsReporter + cycles issueops.CycleDetector + commenter issueops.Commenter + claimer issueops.ReadyClaimer + closer issueops.BatchCloser + creator issueops.BatchCreator + editor issueops.DependencyEditor + applier issueops.BatchApplier + readyCounter issueops.ReadyCounter + querier issueops.Querier + sweeper issueops.Sweeper + deleter issueops.Deleter + bootstrapper issueops.Bootstrapper + verifier issueops.InitVerifier + metadataCAS issueops.MetadataCAS + releaser issueops.Releaser + err error } func newRoleAccessorStore() *roleAccessorStore { sentinel := &roleAccessorSentinel{} return &roleAccessorStore{ - memories: &memoryRoleSentinel{}, - lifecycle: sentinel, - reader: sentinel, - relations: sentinel, - edges: sentinel, - blocking: sentinel, - graphCounter: sentinel, - batchGetter: sentinel, - counter: sentinel, - settings: sentinel, - versions: sentinel, - stats: sentinel, - cycles: sentinel, - commenter: sentinel, - claimer: sentinel, - closer: sentinel, - creator: sentinel, - editor: sentinel, - applier: sentinel, - readyCounter: sentinel, - querier: sentinel, - sweeper: sentinel, - deleter: sentinel, - bootstrapper: sentinel, - verifier: sentinel, - metadataCAS: sentinel, - releaser: sentinel, - leaseReclaimer: sentinel, - heartbeater: sentinel, - blockedLister: sentinel, + memories: &memoryRoleSentinel{}, + lifecycle: sentinel, + reader: sentinel, + relations: sentinel, + edges: sentinel, + blocking: sentinel, + graphCounter: sentinel, + counter: sentinel, + settings: sentinel, + versions: sentinel, + stats: sentinel, + cycles: sentinel, + commenter: sentinel, + claimer: sentinel, + closer: sentinel, + creator: sentinel, + editor: sentinel, + applier: sentinel, + readyCounter: sentinel, + querier: sentinel, + sweeper: sentinel, + deleter: sentinel, + bootstrapper: sentinel, + verifier: sentinel, + metadataCAS: sentinel, + releaser: sentinel, } } @@ -280,9 +272,6 @@ func (s *roleAccessorStore) DependencyEditor() (issueops.DependencyEditor, error func (s *roleAccessorStore) GraphCounter() (issueops.GraphCounter, error) { return s.graphCounter, s.err } -func (s *roleAccessorStore) BatchGetter() (issueops.BatchGetter, error) { - return s.batchGetter, s.err -} func (s *roleAccessorStore) MetadataCAS() (issueops.MetadataCAS, error) { return s.metadataCAS, s.err } @@ -292,17 +281,8 @@ func (s *roleAccessorStore) BatchApplier() (issueops.BatchApplier, error) { func (s *roleAccessorStore) Releaser() (issueops.Releaser, error) { return s.releaser, s.err } -func (s *roleAccessorStore) LeaseReclaimer() (issueops.LeaseReclaimer, error) { - return s.leaseReclaimer, s.err -} -func (s *roleAccessorStore) Heartbeater() (issueops.Heartbeater, error) { - return s.heartbeater, s.err -} -func (s *roleAccessorStore) BlockedLister() (issueops.BlockedLister, error) { - return s.blockedLister, s.err -} -// roleAccessorSentinel implements thirty of the thirty-one roles at +// roleAccessorSentinel implements twenty-seven of the twenty-eight roles at // once — every one but memoryops.Memories, whose List collides with // issueops.Reader.List and needs the second sentinel below. // Nothing calls its methods; identity is the whole point. @@ -347,9 +327,6 @@ func (*roleAccessorSentinel) CountByGroup(context.Context, issueops.CountByGroup func (*roleAccessorSentinel) CountEdges(context.Context, issueops.EdgeCountRequest) (issueops.EdgeCountResult, error) { return issueops.EdgeCountResult{}, nil } -func (*roleAccessorSentinel) GetMany(context.Context, issueops.GetManyRequest) (issueops.GetManyResult, error) { - return issueops.GetManyResult{}, nil -} func (*roleAccessorSentinel) GetSetting(context.Context, issueops.GetSettingRequest) (issueops.SettingResult, error) { return issueops.SettingResult{}, nil } @@ -427,15 +404,6 @@ func (*roleAccessorSentinel) CompareAndSetKey(context.Context, issueops.CompareA func (*roleAccessorSentinel) Release(context.Context, issueops.ReleaseRequest) (issueops.ReleaseResult, error) { return issueops.ReleaseResult{}, nil } -func (*roleAccessorSentinel) Reclaim(context.Context, issueops.ReclaimRequest) (issueops.ReclaimResult, error) { - return issueops.ReclaimResult{}, nil -} -func (*roleAccessorSentinel) Heartbeat(context.Context, issueops.HeartbeatRequest) (issueops.HeartbeatResult, error) { - return issueops.HeartbeatResult{}, nil -} -func (*roleAccessorSentinel) ListBlocked(context.Context, issueops.BlockedRequest) (issueops.BlockedResult, error) { - return issueops.BlockedResult{}, nil -} // memoryRoleSentinel is the memory role's sentinel, and the one role // memoryRoleSentinel is the remaining role's sentinel, and the one role @@ -468,10 +436,10 @@ func (*memoryRoleSentinel) List(context.Context, memoryops.ListRequest) (memoryo // paragraph rather than four near-identical ones (bd-8ri3m). Reads fire no // completion hooks, so IssueReader, IssueRelations, Counter, StatsReporter, // CycleDetector, EdgeReader, BlockingAnnotator, TreeWalker, GraphCounter, -// ReadyCounter, BlockedLister, Querier and InitVerifier deliberately return the inner surface +// ReadyCounter, Querier and InitVerifier deliberately return the inner surface // unwrapped, // each in its own hook_*.go. The ones in that column that are NOT reads are -// WorkspaceConfig, Memories, VersionReconciler, Bootstrapper, Sweeper, +// WorkspaceConfig, Memories, VersionReconciler, Bootstrapper, Sweeper and // Deleter: a settings write changes the workspace rather than a bead, a // remembered insight names no bead and there is no on_remember to fire, a // version marker names no bead at all, a bootstrap lands on a workspace whose @@ -498,14 +466,12 @@ func TestHookFiringStoreWrapsTheWriteRolesAndPassesTheReadsThrough(t *testing.T) {"MetadataCAS", func() (any, error) { return store.MetadataCAS() }, inner.metadataCAS, true}, {"BatchApplier", func() (any, error) { return store.BatchApplier() }, inner.applier, true}, {"Releaser", func() (any, error) { return store.Releaser() }, inner.releaser, true}, - {"LeaseReclaimer", func() (any, error) { return store.LeaseReclaimer() }, inner.leaseReclaimer, true}, {"IssueReader", func() (any, error) { return store.IssueReader() }, inner.reader, false}, {"IssueRelations", func() (any, error) { return store.IssueRelations() }, inner.relations, false}, {"EdgeReader", func() (any, error) { return store.EdgeReader() }, inner.edges, false}, {"BlockingAnnotator", func() (any, error) { return store.BlockingAnnotator() }, inner.blocking, false}, {"TreeWalker", func() (any, error) { return store.TreeWalker() }, inner.tree, false}, {"GraphCounter", func() (any, error) { return store.GraphCounter() }, inner.graphCounter, false}, - {"BatchGetter", func() (any, error) { return store.BatchGetter() }, inner.batchGetter, false}, {"Counter", func() (any, error) { return store.Counter() }, inner.counter, false}, {"WorkspaceConfig", func() (any, error) { return store.WorkspaceConfig() }, inner.settings, false}, {"Memories", func() (any, error) { return store.Memories() }, inner.memories, false}, @@ -518,8 +484,6 @@ func TestHookFiringStoreWrapsTheWriteRolesAndPassesTheReadsThrough(t *testing.T) {"Deleter", func() (any, error) { return store.Deleter() }, inner.deleter, false}, {"Bootstrapper", func() (any, error) { return store.Bootstrapper() }, inner.bootstrapper, false}, {"InitVerifier", func() (any, error) { return store.InitVerifier() }, inner.verifier, false}, - {"Heartbeater", func() (any, error) { return store.Heartbeater() }, inner.heartbeater, true}, - {"BlockedLister", func() (any, error) { return store.BlockedLister() }, inner.blockedLister, false}, } { t.Run(test.name, func(t *testing.T) { surface, err := test.got() @@ -559,14 +523,12 @@ func TestHookFiringStoreRoleAccessorsPropagateInnerErrors(t *testing.T) { {"MetadataCAS", func() (any, error) { return store.MetadataCAS() }}, {"BatchApplier", func() (any, error) { return store.BatchApplier() }}, {"Releaser", func() (any, error) { return store.Releaser() }}, - {"LeaseReclaimer", func() (any, error) { return store.LeaseReclaimer() }}, {"IssueReader", func() (any, error) { return store.IssueReader() }}, {"IssueRelations", func() (any, error) { return store.IssueRelations() }}, {"EdgeReader", func() (any, error) { return store.EdgeReader() }}, {"BlockingAnnotator", func() (any, error) { return store.BlockingAnnotator() }}, {"TreeWalker", func() (any, error) { return store.TreeWalker() }}, {"GraphCounter", func() (any, error) { return store.GraphCounter() }}, - {"BatchGetter", func() (any, error) { return store.BatchGetter() }}, {"Counter", func() (any, error) { return store.Counter() }}, {"WorkspaceConfig", func() (any, error) { return store.WorkspaceConfig() }}, {"Memories", func() (any, error) { return store.Memories() }}, @@ -579,8 +541,6 @@ func TestHookFiringStoreRoleAccessorsPropagateInnerErrors(t *testing.T) { {"Deleter", func() (any, error) { return store.Deleter() }}, {"Bootstrapper", func() (any, error) { return store.Bootstrapper() }}, {"InitVerifier", func() (any, error) { return store.InitVerifier() }}, - {"Heartbeater", func() (any, error) { return store.Heartbeater() }}, - {"BlockedLister", func() (any, error) { return store.BlockedLister() }}, } { t.Run(test.name, func(t *testing.T) { if _, err := test.got(); !errors.Is(err, want) { diff --git a/internal/storage/rowid/BUILD.bazel b/internal/storage/rowid/BUILD.bazel deleted file mode 100644 index b0f65270f..000000000 --- a/internal/storage/rowid/BUILD.bazel +++ /dev/null @@ -1,89 +0,0 @@ -load("@rules_go//go:def.bzl", "go_library", "go_test") - -go_library( - name = "rowid", - srcs = ["rowid.go"], - importpath = "github.com/steveyegge/beads/internal/storage/rowid", - visibility = ["//:__subpackages__"], - deps = ["@com_github_google_uuid//:uuid"], -) - -go_test( - name = "rowid_test", - srcs = ["rowid_test.go"], - embed = [":rowid"], -) - -# --- begin go_srcs (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) --- - -filegroup( - name = "go_srcs", - srcs = glob( - ["**/*.go"], - allow_empty = True, - ), - visibility = ["//:__subpackages__"], -) - -# --- end go_srcs --- - -# --- begin repo_files (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) --- - -filegroup( - name = "repo_doc_files", - srcs = glob( - [ - "**/*.md", - "**/*.mdx", - ], - allow_empty = True, - exclude = ["**/node_modules/**"], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_go_srcs", - srcs = glob( - ["**/*.go"], - allow_empty = True, - exclude = [ - "**/*_test.go", - "**/node_modules/**", - ], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_go_test_srcs", - srcs = glob( - ["**/*_test.go"], - allow_empty = True, - exclude = ["**/node_modules/**"], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_other_files", - srcs = glob( - ["**"], - exclude = [ - "**/*.db", - "**/*.exe", - "**/*.go", - "**/*.md", - "**/*.mdx", - "**/*.out", - "**/*.prof", - "**/*.pyc", - "**/*.test", - "**/__pycache__/**", - "**/node_modules/**", - ], - ), - visibility = ["//:__subpackages__"], -) - -# --- end repo_files --- diff --git a/internal/storage/schema/BUILD.bazel b/internal/storage/schema/BUILD.bazel deleted file mode 100644 index 68e8b9b75..000000000 --- a/internal/storage/schema/BUILD.bazel +++ /dev/null @@ -1,265 +0,0 @@ -load("@rules_go//go:def.bzl", "go_library", "go_test") - -go_library( - name = "schema", - srcs = [ - "aux_row_id_backfill.go", - "backfill.go", - "cli_migrations.go", - "cli_prepared_ddl.go", - "content_skew.go", - "converged.go", - "dep_id_backfill.go", - "dirty_tables_error.go", - "ignored_cursor_untrack.go", - "lock.go", - "migrate_consent.go", - "migration_content_hashes.go", - "migration_repairs.go", - "remote_migrate_gate.go", - "schema.go", - "smart_remote_migrate_gate.go", - "wisp_dep_forward_repair.go", - ], - embedsrcs = [ - "migrations/0001_create_issues.up.sql", - "migrations/0002_create_dependencies.up.sql", - "migrations/0003_create_labels.up.sql", - "migrations/0004_create_comments.up.sql", - "migrations/0005_create_events.up.sql", - "migrations/0006_create_config.up.sql", - "migrations/0007_create_metadata.up.sql", - "migrations/0008_create_child_counters.up.sql", - "migrations/0009_create_issue_snapshots.up.sql", - "migrations/0010_create_compaction_snapshots.up.sql", - "migrations/0011_create_repo_mtimes.up.sql", - "migrations/0012_create_routes.up.sql", - "migrations/0013_create_issue_counter.up.sql", - "migrations/0014_create_interactions.up.sql", - "migrations/0015_create_federation_peers.up.sql", - "migrations/0016_default_config.up.sql", - "migrations/0017_create_ready_issues_view.up.sql", - "migrations/0018_create_blocked_issues_view.up.sql", - "migrations/0019_wisps_dolt_ignore.up.sql", - "migrations/0020_create_wisps.up.sql", - "migrations/0021_create_wisp_auxiliary.up.sql", - "migrations/0022_wisp_dep_type_index.up.sql", - "migrations/0023_add_no_history_column.up.sql", - "migrations/0024_create_custom_status_type_tables.up.sql", - "migrations/0025_update_ready_issues_view.up.sql", - "migrations/0026_update_blocked_issues_view.up.sql", - "migrations/0027_add_started_at.up.sql", - "migrations/0028_local_state_dolt_ignore.up.sql", - "migrations/0029_create_local_metadata.up.sql", - "migrations/0030_migrate_local_metadata_keys.up.sql", - "migrations/0031_wisp_events_created_at_index.up.sql", - "migrations/0032_drop_schema_migrations_applied_at.up.sql", - "migrations/0033_add_wisp_type_column.up.sql", - "migrations/0034_add_spec_id_column.up.sql", - "migrations/0035_migrate_infra_to_wisps.up.sql", - "migrations/0036_cleanup_autopush_metadata.up.sql", - "migrations/0037_uuid_primary_keys.up.sql", - "migrations/0038_drop_hop_columns.up.sql", - "migrations/0039_drop_child_counters_fk.up.sql", - "migrations/0040_ignored_tables_also_nonlocal_tables.up.sql", - "migrations/0041_split_dependencies_target.up.sql", - "migrations/0042_add_on_update_cascade.up.sql", - "migrations/0043_drop_dependencies_generated_column.up.sql", - "migrations/0044_update_views_drop_depends_on_id.up.sql", - "migrations/0045_update_blocked_view_drop_depends_on_id.up.sql", - "migrations/0046_add_is_blocked.up.sql", - "migrations/0047_recompute_mixed_is_blocked.up.sql", - "migrations/0048_widen_event_value_columns.up.sql", - "migrations/0049_longtext_large_content_columns.up.sql", - "migrations/0050_dependencies_deterministic_id.up.sql", - "migrations/0051_drop_aux_id_defaults.up.sql", - "migrations/0052_add_date_indexes.up.sql", - "migrations/0053_repair_rig_wisps.up.sql", - "migrations/0054_add_lease_columns.up.sql", - "migrations/0055_move_leases_to_table.up.sql", - "migrations/0056_add_comments_keyset_index.up.sql", - "migrations/0057_events_value_columns_idempotent_longtext.up.sql", - "migrations/0058_heal_wisp_dependencies_split_constraints.up.sql", - "migrations/0059_recompute_null_gate_is_blocked.up.sql", - "migrations/0060_add_storage_class.up.sql", - "migrations/0061_content_derived_aux_ids.up.sql", - "migrations/0062_events_dolt_ignore.up.sql", - "migrations/0063_create_provenance_events.up.sql", - "migrations/0064_create_events_journal.up.sql", - "migrations/0065_widen_wisp_comments_text.up.sql", - "migrations/0066_add_events_journal_actor.up.sql", - "migrations/0067_add_versioned_beads_schema.up.sql", - "migrations/0068_add_attribution_status.up.sql", - "migrations/ignored/0001_create_local_state_tables.up.sql", - "migrations/ignored/0002_create_wisp_child_counters.up.sql", - "migrations/ignored/0003_split_wisp_dependencies_target.up.sql", - "migrations/ignored/0004_add_wisp_aux_fks.up.sql", - "migrations/ignored/0005_drop_wisp_dependencies_generated_column.up.sql", - "migrations/ignored/0006_add_wisp_is_blocked.up.sql", - "migrations/ignored/0007_recompute_wisp_is_blocked.up.sql", - "migrations/ignored/0008_widen_wisp_event_value_columns.up.sql", - "migrations/ignored/0009_aux_row_id_rekey_marker.up.sql", - "migrations/ignored/0010_drop_wisp_id_defaults.up.sql", - "migrations/ignored/0011_cleanup_orphaned_child_counters.up.sql", - "migrations/ignored/0012_create_leases.up.sql", - "migrations/ignored/0013_add_wisp_row_lock.up.sql", - "migrations/ignored/0014_add_wisp_comments_keyset_index.up.sql", - "migrations/ignored/0015_recompute_null_gate_wisp_is_blocked.up.sql", - "migrations/ignored/0016_add_lease_granted_node.up.sql", - "migrations/ignored/0017_add_wisps_defer_until_index.up.sql", - "migrations/ignored/0018_aux_row_id_rekey2_marker.up.sql", - "migrations/ignored/0019_create_events.up.sql", - "migrations/ignored/0020_add_wisp_storage_class.up.sql", - "migrations/ignored/0021_widen_wisp_large_content_columns.up.sql", - "migrations/ignored/0022_create_events_journal.up.sql", - "migrations/ignored/0023_repair_events_journal_shape.up.sql", - "migrations/ignored/0024_widen_wisp_comments_text.up.sql", - "migrations/ignored/0025_add_events_journal_actor.up.sql", - "migrations/ignored/0026_dep_rekey_dedup_marker.up.sql", - "migrations/ignored/0027_add_wisps_current_revision.up.sql", - "migrations/0069_widen_issue_versions_datetime_precision.up.sql", - "migrations/ignored/0028_add_wisps_participation_generation.up.sql", - "migrations/0070_add_participation_generation.up.sql", - "migrations/0071_add_removed_restriction.up.sql", - "migrations/0072_add_epoch_minted_addresses.up.sql", - ], - importpath = "github.com/steveyegge/beads/internal/storage/schema", - visibility = ["//:__subpackages__"], - deps = [ - "//internal/debug", - "//internal/storage/dberrors", - "//internal/storage/depid", - "//internal/storage/issueops", - "//internal/storage/rowid", - "//internal/types", - "@org_golang_x_term//:term", - ], -) - -go_test( - name = "schema_test", - srcs = [ - "aux_row_id_backfill_test.go", - "aux_row_id_drift_test.go", - "call_returning_row_test.go", - "cli_prepared_ddl_test.go", - "content_hash_test.go", - "content_skew_test.go", - "converged_test.go", - "cursor_reality_test.go", - "dep_id_backfill_test.go", - "dolt_ignore_seed_test.go", - "ignored_cursor_untrack_test.go", - "lock_test.go", - "migrate_consent_test.go", - "migration_0067_versioned_beads_test.go", - "migration_0068_add_attribution_status_test.go", - "migration_0069_datetime_precision_test.go", - "migration_0070_add_participation_generation_test.go", - "migration_0071_add_removed_restriction_test.go", - "migration_0072_add_epoch_minted_addresses_test.go", - "migration_body_drain_test.go", - "migration_freeze_test.go", - "migration_watchdog_test.go", - "progress_test.go", - "remote_migrate_gate_test.go", - "schema_skew_test.go", - "schema_test.go", - "shared_store_migrate_gate_test.go", - "smart_remote_migrate_gate_test.go", - ], - data = [":migrations"], - embed = [":schema"], - deps = [ - "//internal/debug", - "//internal/storage/depid", - "//internal/storage/issueops", - "//internal/storage/rowid", - "//internal/testutil", - "@com_github_data_dog_go_sqlmock//:go-sqlmock", - "@com_github_go_sql_driver_mysql//:mysql", - "@org_golang_x_term//:term", - ], -) - -# Migration SQL (up, down, ignored/) and the nondeterminism allowlist, read -# from disk by schema and issueops tests beyond what the library embeds. -filegroup( - name = "migrations", - srcs = glob(["migrations/**"]), - visibility = ["//internal/storage:__subpackages__"], -) - -# --- begin go_srcs (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) --- - -filegroup( - name = "go_srcs", - srcs = glob( - ["**/*.go"], - allow_empty = True, - ), - visibility = ["//:__subpackages__"], -) - -# --- end go_srcs --- - -# --- begin repo_files (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) --- - -filegroup( - name = "repo_doc_files", - srcs = glob( - [ - "**/*.md", - "**/*.mdx", - ], - allow_empty = True, - exclude = ["**/node_modules/**"], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_go_srcs", - srcs = glob( - ["**/*.go"], - allow_empty = True, - exclude = [ - "**/*_test.go", - "**/node_modules/**", - ], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_go_test_srcs", - srcs = glob( - ["**/*_test.go"], - allow_empty = True, - exclude = ["**/node_modules/**"], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_other_files", - srcs = glob( - ["**"], - exclude = [ - "**/*.db", - "**/*.exe", - "**/*.go", - "**/*.md", - "**/*.mdx", - "**/*.out", - "**/*.prof", - "**/*.pyc", - "**/*.test", - "**/__pycache__/**", - "**/node_modules/**", - ], - ), - visibility = ["//:__subpackages__"], -) - -# --- end repo_files --- diff --git a/internal/storage/schema/aux_row_id_backfill.go b/internal/storage/schema/aux_row_id_backfill.go index 0427e61e7..07357bd0c 100644 --- a/internal/storage/schema/aux_row_id_backfill.go +++ b/internal/storage/schema/aux_row_id_backfill.go @@ -268,11 +268,8 @@ func clearAuxRekeyDrifted(ctx context.Context, db DBConn) error { // auxRekeyTable describes one table covered by the re-key. columns is the // frozen SELECT list of every non-id column, in creation order, with datetime -// columns rendered via DATE_FORMAT(x, '%Y-%m-%d %H:%i:%s') server-side so the -// scanned text matches issueops.AuxTimeLayout — the Go-side stamp every -// insert-time row binds — byte-for-byte, rather than depending on -// CAST(x AS CHAR)'s engine-default rendering (see -// TestAuxTimeDateFormatMatchesGoRendering in internal/storage/dolt). +// columns CAST to CHAR server-side so the scanned text is identical across +// drivers and connection settings. // // These lists are part of the id derivation and are FROZEN: they must keep // naming exactly the columns the tables had when this backfill shipped, even @@ -293,19 +290,19 @@ type auxRekeyTable struct { var auxRekeyTables = []auxRekeyTable{ { name: "events", - columns: "issue_id, event_type, actor, old_value, new_value, comment, DATE_FORMAT(created_at, '%Y-%m-%d %H:%i:%s')", + columns: "issue_id, event_type, actor, old_value, new_value, comment, CAST(created_at AS CHAR)", }, { name: "comments", - columns: "issue_id, author, text, DATE_FORMAT(created_at, '%Y-%m-%d %H:%i:%s')", + columns: "issue_id, author, text, CAST(created_at AS CHAR)", }, { name: "issue_snapshots", - columns: "issue_id, DATE_FORMAT(snapshot_time, '%Y-%m-%d %H:%i:%s'), compaction_level, original_size, compressed_size, original_content, archived_events", + columns: "issue_id, CAST(snapshot_time AS CHAR), compaction_level, original_size, compressed_size, original_content, archived_events", }, { name: "compaction_snapshots", - columns: "issue_id, compaction_level, snapshot_json, DATE_FORMAT(created_at, '%Y-%m-%d %H:%i:%s')", + columns: "issue_id, compaction_level, snapshot_json, CAST(created_at AS CHAR)", }, } @@ -575,17 +572,7 @@ func rekeyAuxRowTable(ctx context.Context, db DBConn, t auxRekeyTable) (bool, er if err != nil { return false, err } - // Derive the field count from the driver's own column list rather than - // counting commas in t.columns: a column expression can contain an - // internal comma (DATE_FORMAT(x, '%Y-%m-%d %H:%i:%s')'s format-string - // argument), which a naive strings.Count over-counts as an extra - // top-level field. - colNames, err := rows.Columns() - if err != nil { - _ = rows.Close() - return false, err - } - nFields := len(colNames) - 1 + nFields := strings.Count(t.columns, ",") + 1 groups := make(map[string][]string) for rows.Next() { var id string diff --git a/internal/storage/schema/aux_row_id_backfill_test.go b/internal/storage/schema/aux_row_id_backfill_test.go index 52f501368..7f6de318d 100644 --- a/internal/storage/schema/aux_row_id_backfill_test.go +++ b/internal/storage/schema/aux_row_id_backfill_test.go @@ -22,7 +22,7 @@ func commentDigest(issueID, author, text, createdAt string) string { func expectCommentsSelect(mock sqlmock.Sqlmock) *sqlmock.ExpectedQuery { return mock.ExpectQuery(regexp.QuoteMeta( - "SELECT id, issue_id, author, text, DATE_FORMAT(created_at, '%Y-%m-%d %H:%i:%s') FROM comments")) + "SELECT id, issue_id, author, text, CAST(created_at AS CHAR) FROM comments")) } // TestRekeyAuxRowTableConvergesRandomIDs verifies the core convergence: rows diff --git a/internal/storage/schema/cli_migrations.go b/internal/storage/schema/cli_migrations.go index 1d161cbc6..405c59c43 100644 --- a/internal/storage/schema/cli_migrations.go +++ b/internal/storage/schema/cli_migrations.go @@ -126,57 +126,6 @@ func cliCompatibleMigrationSQL(name, sqlText string) string { // itself is always present here: 0064's prepared RENAME executes on // 2.2.0 (same measurement), so a fresh bundle always needs the column. return cliMigration0066AddEventsJournalActor - case "0067_add_versioned_beads_schema.up.sql": - // Direct DDL for the same reason as 0060: the source migration's - // PREPARE guards (INFORMATION_SCHEMA probes) are what make the raw - // .up.sql idempotent when replayed onto an already-migrated store, - // and the 2.2.x CLI no-ops a prepared ADD COLUMN. Both planes always - // need the column here -- a fresh bundle runs the whole main series - // in order, so `issues` and `wisps` both exist and neither carries - // current_revision yet. The CREATE TABLEs are carried over verbatim: - // IF NOT EXISTS is ordinary unwrapped DDL that the CLI executes. - // Replays over a database that never synced the wisp tables must use - // the frozen source text instead -- see cliSubstituteAssumesWispTables. - return cliMigration0067AddVersionedBeadsSchema - case "0068_add_attribution_status.up.sql": - // Direct DDL for the same reason as 0067: the source migration's - // PREPARE guards (INFORMATION_SCHEMA probes) are what make the raw - // .up.sql idempotent when replayed onto an already-migrated store, - // and the 2.2.x CLI no-ops a prepared ADD COLUMN and MODIFY COLUMN - // alike. issue_versions is always present here -- 0067 runs earlier - // in the same fresh-bundle series -- never carries attribution_status - // yet, and still has durable_state as the JSON type 0067 gave it, so - // both of 0068's steps always fire on a fresh bundle. - return cliMigration0068AddAttributionStatus - case "0069_widen_issue_versions_datetime_precision.up.sql": - // Direct DDL for the same reason as 0068: the source migration's - // PREPARE guards (DATETIME_PRECISION probes) are what make the raw - // .up.sql idempotent when replayed onto an already-widened store, - // and the 2.2.x CLI no-ops a prepared MODIFY COLUMN. Nothing before - // 0069 in the fresh-bundle series widens change_at or removed_at -- - // 0067 creates both as plain DATETIME and 0068 retypes only - // durable_state -- so both of 0069's MODIFYs always fire here too. - return cliMigration0069WidenIssueVersionsDatetimePrecision - case "0070_add_participation_generation.up.sql": - // Direct DDL for the same reason as 0067: the source migration's - // PREPARE guards (INFORMATION_SCHEMA probes) are what make the raw - // .up.sql idempotent when replayed onto an already-migrated store, - // and the 2.2.x CLI no-ops a prepared ADD COLUMN. issues and wisps - // both exist by this point in the series (0067 creates the versioned - // columns on both) and neither carries participation_generation yet, - // so both of 0070's ALTERs always fire on a fresh bundle -- this - // substitute ALTERs wisps directly, the same as 0067's, so it - // belongs on cliSubstituteAssumesWispTables too. - return cliMigration0070AddParticipationGeneration - case "0071_add_removed_restriction.up.sql": - // Direct DDL for the same reason as 0067/0068: the source migration's - // PREPARE guard (an INFORMATION_SCHEMA probe) is what makes the raw - // .up.sql idempotent when replayed onto an already-migrated store, - // and the 2.2.x CLI no-ops a prepared ADD COLUMN. issue_versions is - // always present here -- 0067 runs earlier in the same fresh-bundle - // series -- and never carries removed_restriction yet, so the column - // always needs adding on a fresh bundle. - return cliMigration0071AddRemovedRestriction default: return sqlText } @@ -209,15 +158,6 @@ func cliSubstituteAssumesWispTables(name string) bool { // cliMigration0065WidenWispCommentsText is a bare MODIFY on // wisp_comments. return true - case "0067_add_versioned_beads_schema.up.sql": - // cliMigration0067AddVersionedBeadsSchema drops the source's - // @wisps_cr_needs_add table-exists guard and ALTERs wisps directly. - return true - case "0070_add_participation_generation.up.sql": - // cliMigration0070AddParticipationGeneration drops the source's - // @wisps_pg_needs_add table-exists guard and ALTERs wisps directly, - // the same shape 0067's override already uses for current_revision. - return true default: return false } @@ -265,74 +205,6 @@ ALTER TABLE wisps ADD COLUMN storage_class VARCHAR(16);` const cliMigration0065WidenWispCommentsText = `ALTER TABLE wisp_comments MODIFY COLUMN text LONGTEXT NOT NULL;` const cliMigration0066AddEventsJournalActor = `ALTER TABLE bd_events_journal ADD COLUMN actor VARCHAR(255) NOT NULL DEFAULT '';` -// cliMigration0067AddVersionedBeadsSchema is 0067 with its two guarded -// PREPARE blocks replaced by the direct ALTERs they would run on a fresh -// database. The CREATE TABLEs are the source file's own text: CREATE TABLE -// IF NOT EXISTS executes on the CLI batch path unchanged. -const cliMigration0067AddVersionedBeadsSchema = `CREATE TABLE IF NOT EXISTS issue_versions ( - issue_id VARCHAR(255) NOT NULL, - revision BIGINT NOT NULL, - epoch INT NOT NULL, - durable_state JSON, - change_actor VARCHAR(255), - change_agent VARCHAR(255), - change_message TEXT, - change_at DATETIME NOT NULL, - removed_at DATETIME, - removed_reason VARCHAR(255), - PRIMARY KEY (issue_id, revision) -); -CREATE TABLE IF NOT EXISTS store_epoch ( - id TINYINT(1) NOT NULL DEFAULT 1, - epoch INT NOT NULL DEFAULT 1, - bumped_at DATETIME, - bumped_reason VARCHAR(255), - PRIMARY KEY (id), - CONSTRAINT ck_store_epoch_singleton CHECK (id = 1) -); -ALTER TABLE issues ADD COLUMN current_revision BIGINT NOT NULL DEFAULT 1; -ALTER TABLE wisps ADD COLUMN current_revision BIGINT NOT NULL DEFAULT 1;` - -// cliMigration0068AddAttributionStatus is 0068 with its two guarded PREPARE -// blocks replaced by the direct ALTERs they would run on a fresh database: -// step 6's ADD COLUMN attribution_status, and step 7's MODIFY COLUMN -// durable_state LONGBLOB (the byte-preserving type the review on -// gastownhall/beads#6358 item 4 asked for -- see the migration's step 7 -// header). issue_versions is created earlier in the same series by 0067, -// which still creates durable_state as JSON and whose override is left -// untouched: 0068 is what retypes it. So the column always needs adding and -// the retype always fires here; no wisps twin exists for this table. -const cliMigration0068AddAttributionStatus = `ALTER TABLE issue_versions ADD COLUMN attribution_status VARCHAR(20) NOT NULL; -ALTER TABLE issue_versions MODIFY COLUMN durable_state LONGBLOB;` - -// cliMigration0069WidenIssueVersionsDatetimePrecision is 0069 with its two -// guarded PREPARE blocks replaced by the direct MODIFYs they would run on a -// fresh database: change_at and removed_at from 0067's plain DATETIME to -// DATETIME(6) (the microsecond widen be-hs42e.8 / gastownhall/beads#6132 -// asks for -- see the migration's header). Neither 0067's override nor -// 0068's touches either column, so both retypes always fire here; no wisps -// twin exists for this table. -const cliMigration0069WidenIssueVersionsDatetimePrecision = `ALTER TABLE issue_versions MODIFY COLUMN change_at DATETIME(6) NOT NULL; -ALTER TABLE issue_versions MODIFY COLUMN removed_at DATETIME(6);` - -// cliMigration0070AddParticipationGeneration is 0070 with its two guarded -// PREPARE blocks replaced by the direct ALTERs they would run on a fresh -// database: participation_generation BIGINT NULL on issues, mirrored -// inertly on wisps (design §16.3 steps 4-5, be-dt74u amendment, be-h89oq). -// issues and wisps both exist by this point in the series (0067 creates the -// versioned columns on both) and neither carries participation_generation -// yet, so both ALTERs always fire on a fresh bundle -- this substitute -// ALTERs wisps directly, the same as 0067's, so it belongs on -// cliSubstituteAssumesWispTables too. -const cliMigration0070AddParticipationGeneration = `ALTER TABLE issues ADD COLUMN participation_generation BIGINT NULL; -ALTER TABLE wisps ADD COLUMN participation_generation BIGINT NULL;` - -// cliMigration0071AddRemovedRestriction is 0071 with its one guarded PREPARE -// block replaced by the direct ALTER it would run on a fresh database. -// issue_versions is created earlier in the same series by 0067 and never -// carries removed_restriction yet, so the column always needs adding here. -const cliMigration0071AddRemovedRestriction = `ALTER TABLE issue_versions ADD COLUMN removed_restriction VARCHAR(30);` - const cliMigration0041SplitDependenciesTarget = `DELETE FROM dolt_nonlocal_tables; CALL DOLT_COMMIT('-Am', 'disable nonlocal tables for fk migrations'); SET FOREIGN_KEY_CHECKS = 0; diff --git a/internal/storage/schema/converged.go b/internal/storage/schema/converged.go index 3a2b606b1..f17f6bef7 100644 --- a/internal/storage/schema/converged.go +++ b/internal/storage/schema/converged.go @@ -36,17 +36,12 @@ func alreadyConverged(ctx context.Context, db DBConn, databaseName string, selec return false, nil } - // Put the session on the target database first. Callers arrive in two - // shapes. The proxied CLI open in uow.openAndInitSchema now connects - // straight to the target database on its own steady-state path - // (wy-s8ytnw), so the session is already there and the DATABASE() read - // below is the whole cost. Its fall-through open — and, before that fast - // path existed, every invocation of the hot path — pins the schema-init - // pool with an EMPTY DSN database and only USEs the database after - // GET_LOCK, so a probe that merely ASKED whether the session was already - // on databaseName read NULL from DATABASE() and declined every single - // time: the fast path never fired where it was needed. The selector is - // what keeps that second shape working. + // Put the session on the target database first. The hot path — the proxied + // CLI open in uow.openAndInitSchema — pins its schema-init pool with an + // EMPTY DSN database and only USEs the database after GET_LOCK, so a probe + // that merely ASKED whether the session was already on databaseName read + // NULL from DATABASE() and declined on every single invocation: the fast + // path never fired where it was needed. onTarget, qualifier, err := selectTargetDatabase(ctx, db, databaseName, selector) if err != nil { return false, err @@ -99,9 +94,7 @@ func alreadyConverged(ctx context.Context, db DBConn, databaseName string, selec // deliberately non-ignored table that the next pull's auto-commit puts // into HEAD and push replicates fleet-wide. // - // Two SHOW TABLES listings on the hot path, safe to issue because - // selectTargetDatabase has put the session on the target (see - // readIgnoredCursorState). + // Two always-succeeding reads on the hot path, both bounded to one row. state, err := readIgnoredCursorState(ctx, db, qualifier) if err != nil { return false, err diff --git a/internal/storage/schema/converged_test.go b/internal/storage/schema/converged_test.go index 67d26ead6..8eff9441e 100644 --- a/internal/storage/schema/converged_test.go +++ b/internal/storage/schema/converged_test.go @@ -715,7 +715,7 @@ func TestAlreadyConvergedFailsClosedOnUnreadableState(t *testing.T) { db, mock := newMockDB(t) expectCurrentDatabase(mock, "testdb") - mock.ExpectQuery(regexp.QuoteMeta("SELECT COUNT(*) FROM information_schema.schemata WHERE schema_name = DATABASE()")). + mock.ExpectQuery(`SELECT COUNT\(\*\) FROM information_schema\.tables`). WillReturnError(sql.ErrConnDone) converged, err := alreadyConverged(context.Background(), db, "testdb", nil) diff --git a/internal/storage/schema/cursor_reality_test.go b/internal/storage/schema/cursor_reality_test.go index 5fde1827f..299a2e7f9 100644 --- a/internal/storage/schema/cursor_reality_test.go +++ b/internal/storage/schema/cursor_reality_test.go @@ -5,14 +5,13 @@ import ( "database/sql" "errors" "regexp" - "slices" "strings" "testing" "github.com/DATA-DOG/go-sqlmock" ) -// stubSentinelTables replaces the table-existence probe with a fixed answer +// stubSentinelTables replaces the INFORMATION_SCHEMA probe with a fixed answer // for the duration of a test, following the issueRowCounter seam convention in // this package. The DBConn is never touched, so these run with no database. func stubSentinelTables(t *testing.T, present map[string]bool, probeErr error) *[]string { @@ -46,23 +45,6 @@ func stubSentinelColumns(t *testing.T, present map[string]bool, probeErr error) return &probed } -// expectSentinelColumnProbe mocks the real column-sentinel probe in order: the -// carrier table's existence through sentinelTableExists (a SHOW TABLES read), -// then — only when the table is there — the SHOW COLUMNS read. An absent column -// is an empty SHOW COLUMNS result set, not a zero count. -func expectSentinelColumnProbe(mock sqlmock.Sqlmock, sc schemaSentinelColumn, tablePresent, columnPresent bool) { - expectShowTable(mock, sc.table, tablePresent) - if !tablePresent { - return - } - rows := sqlmock.NewRows([]string{"Field", "Type", "Null", "Key", "Default", "Extra"}) - if columnPresent { - rows.AddRow(sc.column, "varchar(255)", "YES", "", nil, "") - } - mock.ExpectQuery(regexp.QuoteMeta("SHOW COLUMNS FROM " + sc.table + " LIKE '" + sc.column + "'")). - WillReturnRows(rows) -} - // allSentinelTables is the "everything the table sentinels ask for is present" // shape, so column-sentinel cases can say so without repeating the list. Both // the unconditional-floor-0 tables and the floored tables share the same @@ -139,13 +121,12 @@ func TestCursorRealityFloor(t *testing.T) { // These two cases document the two real-world shapes the stub seam // collapses: an absent leases table and a present-but-column-less leases // table both drive sentinelColumnExists to false - // (columns["leases.granted_node"] == false), because the real probe - // reports a missing table (its existence read) and a missing column (its - // SHOW COLUMNS read) alike. They deliberately exercise the same branch — - // the naming records intent, not a distinct seam path; the probe itself - // is pinned by TestSentinelColumnProbeNeverAsksAMissingTable. Both floor - // at 11, which is what lets the replay heal both (0012 re-creates leases, - // 0016 adds the column) without reaching back to 0007. + // (columns["leases.granted_node"] == false), because the real + // schemaColumnExists COUNT(*) is 0 for a missing table and a missing + // column alike. They deliberately exercise the same branch — the naming + // records intent, not a distinct seam path. Both floor at 11, which is + // what lets the replay heal both (0012 re-creates leases, 0016 adds the + // column) without reaching back to 0007. { name: "leases table absent", present: allSentinelTables(), @@ -404,45 +385,6 @@ func TestCursorProbeErrorIsNotTreatedAsContradicted(t *testing.T) { } } -// The floored-table loop must surface a probe failure the same way, and the -// test above cannot reach it: its blanket error fails the floor-0 wisps probe -// and returns there. So each case reads every other sentinel table present and -// fails exactly one floored table. Reading that error as "absent" would clamp a -// healthy at-latest cursor to the table's replayFloor and replay the tail on a -// catalog nobody could read; skipping it would believe the cursor over a table -// that may be missing. -func TestFlooredTableProbeErrorIsNotTreatedAsContradicted(t *testing.T) { - if len(ignoredSource.sentinelFlooredTables) == 0 { - t.Fatal("ignoredSource has no floored sentinel tables; this guard is inert") - } - boom := errors.New("information_schema unavailable") - for _, failing := range ignoredSource.sentinelFlooredTables { - t.Run(failing.table, func(t *testing.T) { - present := allSentinelTables() - orig := sentinelTableExists - t.Cleanup(func() { sentinelTableExists = orig }) - sentinelTableExists = func(_ context.Context, _ DBConn, table string) (bool, error) { - if table == failing.table { - return false, boom - } - return present[table], nil - } - columnProbed := stubSentinelColumns(t, map[string]bool{"leases.granted_node": true}, nil) - - _, limited, err := ignoredSource.cursorRealityFloor(context.Background(), nil) - if !errors.Is(err, boom) { - t.Errorf("floored-table probe error was swallowed: err = %v", err) - } - if limited { - t.Error("an unreadable floored-table probe reported the cursor as contradicted") - } - if len(*columnProbed) != 0 { - t.Errorf("column sentinels %v were probed after a floored-table probe failed", *columnProbed) - } - }) - } -} - // The main series is deliberately unguarded: its tables are not clone-local, // so it cannot reach the contradicted state, and probing would add queries to // every store open for nothing. cursorRealityFloor must be a no-op for it — @@ -691,7 +633,8 @@ func TestMigrationWorkNeededWhenWispTablesAbsent(t *testing.T) { expectScalar(mock, "SELECT COALESCE(MAX(version), 0) FROM schema_migrations", "version", LatestVersion()) expectCursorProbe(mock, "ignored_schema_migrations", true) expectScalar(mock, "SELECT COALESCE(MAX(version), 0) FROM ignored_schema_migrations", "version", LatestIgnoredVersion()) - expectShowTable(mock, ignoredSource.sentinelTables[0], false) + mock.ExpectQuery(regexp.QuoteMeta("FROM INFORMATION_SCHEMA.TABLES")). + WillReturnRows(sqlmock.NewRows([]string{"count"}).AddRow(0)) needed, err := migrationWorkNeeded(context.Background(), db) if err != nil { @@ -720,18 +663,18 @@ func TestMigrationWorkNeededWhenLeaseGrantedNodeAbsent(t *testing.T) { expectScalar(mock, "SELECT COALESCE(MAX(version), 0) FROM schema_migrations", "version", LatestVersion()) expectCursorProbe(mock, "ignored_schema_migrations", true) expectScalar(mock, "SELECT COALESCE(MAX(version), 0) FROM ignored_schema_migrations", "version", LatestIgnoredVersion()) - for _, table := range ignoredSource.sentinelTables { - expectShowTable(mock, table, true) + for range ignoredSource.sentinelTables { + mock.ExpectQuery(regexp.QuoteMeta("FROM INFORMATION_SCHEMA.TABLES")). + WillReturnRows(sqlmock.NewRows([]string{"count"}).AddRow(1)) } // The floored-table sentinels probe the same way and must all corroborate // too, ahead of the column probe below. - for _, st := range ignoredSource.sentinelFlooredTables { - expectShowTable(mock, st.table, true) - } - // leases is present; only its granted_node column is missing. - for _, sc := range ignoredSource.sentinelColumns { - expectSentinelColumnProbe(mock, sc, true, false) + for range ignoredSource.sentinelFlooredTables { + mock.ExpectQuery(regexp.QuoteMeta("FROM INFORMATION_SCHEMA.TABLES")). + WillReturnRows(sqlmock.NewRows([]string{"count"}).AddRow(1)) } + mock.ExpectQuery(regexp.QuoteMeta("FROM INFORMATION_SCHEMA.COLUMNS")). + WillReturnRows(sqlmock.NewRows([]string{"count"}).AddRow(0)) needed, err := migrationWorkNeeded(context.Background(), db) if err != nil { @@ -745,157 +688,6 @@ func TestMigrationWorkNeededWhenLeaseGrantedNodeAbsent(t *testing.T) { } } -// TestIgnoredCursorReadWithLeasesAbsentIssuesNoFailingStatement is the -// missing-carrier shape end to end through the guarded cursor read every -// write-capable open performs. Every released tree (v1.1.0, v1.1.2, v1.2.x) -// has no leases table at all, and an out-of-band copy can arrive at-latest -// without it. The ordered mock admits exactly the carrier's existence read for -// the column sentinel: a SHOW COLUMNS sent to the absent table is unexpected, -// and currentVersion surfaces that rather than swallowing it the way atLatest -// does, so this fails if the probe ever issues the statement that pins a pooled -// Dolt session (be-bv7x). -func TestIgnoredCursorReadWithLeasesAbsentIssuesNoFailingStatement(t *testing.T) { - db, mock, err := sqlmock.New() - if err != nil { - t.Fatalf("sqlmock.New: %v", err) - } - defer db.Close() - - expectCursorProbe(mock, "ignored_schema_migrations", true) - expectScalar(mock, "SELECT COALESCE(MAX(version), 0) FROM ignored_schema_migrations", "version", LatestIgnoredVersion()) - for _, table := range ignoredSource.sentinelTables { - expectShowTable(mock, table, true) - } - for _, st := range ignoredSource.sentinelFlooredTables { - expectShowTable(mock, st.table, true) - } - for _, sc := range ignoredSource.sentinelColumns { - expectSentinelColumnProbe(mock, sc, false, false) - } - - got, err := ignoredSource.currentVersion(context.Background(), db) - if err != nil { - t.Fatalf("currentVersion with leases absent: %v", err) - } - if want := 11; got != want { - t.Errorf("currentVersion = %d, want the leases.granted_node replayFloor %d", got, want) - } - if err := mock.ExpectationsWereMet(); err != nil { - t.Fatalf("unmet ordered sentinel probes: %v", err) - } -} - -// TestSentinelColumnProbeNeverAsksAMissingTable pins the probe mechanism behind -// the column sentinel, through the real sentinelColumnExists and a stubbed -// sentinelTableExists. SHOW COLUMNS errors on a missing table, and with the -// store's multiStatements DSN a failing statement leaves the pooled Dolt -// session pinned to its pre-statement catalog, so tables that other -// connections create afterwards stay invisible to it (be-bv7x; reproduced on -// dolt 2.1.8 and 2.2.0). The carrier table must therefore be established by -// the existence probe, which answers an empty set rather than an error, before -// any SHOW COLUMNS is sent. The mock has no expectations: any statement at all -// fails the probe. -func TestSentinelColumnProbeNeverAsksAMissingTable(t *testing.T) { - if len(ignoredSource.sentinelColumns) == 0 { - t.Fatal("ignoredSource has no sentinel columns; this guard is inert") - } - probed := stubSentinelTables(t, allSentinelTables(), nil) - db, mock, err := sqlmock.New() - if err != nil { - t.Fatalf("sqlmock.New: %v", err) - } - defer db.Close() - - for _, sc := range ignoredSource.sentinelColumns { - present, err := sentinelColumnExists(context.Background(), db, sc.table, sc.column) - if err != nil { - t.Fatalf("sentinelColumnExists(%s.%s) with the table absent: %v", sc.table, sc.column, err) - } - if present { - t.Errorf("sentinelColumnExists(%s.%s) = true with the table absent", sc.table, sc.column) - } - if !slices.Contains(*probed, sc.table) { - t.Errorf("carrier table %s was never probed for existence; probed %v", sc.table, *probed) - } - } - if err := mock.ExpectationsWereMet(); err != nil { - t.Fatalf("unmet SQL expectations: %v", err) - } -} - -// TestSentinelColumnProbeReadsAPresentTableExactly covers the other half: once -// the carrier exists, SHOW COLUMNS answers, and its Field is compared exactly -// because '_' is a LIKE single-character wildcard. -func TestSentinelColumnProbeReadsAPresentTableExactly(t *testing.T) { - present := allSentinelTables() - for _, sc := range ignoredSource.sentinelColumns { - present[sc.table] = true - } - probed := stubSentinelTables(t, present, nil) - db, mock, err := sqlmock.New() - if err != nil { - t.Fatalf("sqlmock.New: %v", err) - } - defer db.Close() - - showColumns := regexp.QuoteMeta("SHOW COLUMNS FROM leases LIKE 'granted_node'") - columns := []string{"Field", "Type", "Null", "Key", "Default", "Extra"} - mock.ExpectQuery(showColumns).WillReturnRows(sqlmock.NewRows(columns).AddRow("grantedXnode", "varchar(255)", "YES", "", nil, "")) - mock.ExpectQuery(showColumns).WillReturnRows(sqlmock.NewRows(columns)) - mock.ExpectQuery(showColumns).WillReturnRows(sqlmock.NewRows(columns).AddRow("granted_node", "varchar(255)", "YES", "", nil, "")) - - for i, want := range []bool{false, false, true} { - got, err := sentinelColumnExists(context.Background(), db, "leases", "granted_node") - if err != nil { - t.Fatalf("probe %d: sentinelColumnExists: %v", i, err) - } - if got != want { - t.Errorf("probe %d: sentinelColumnExists = %v, want %v", i, got, want) - } - } - if want := []string{"leases", "leases", "leases"}; !slices.Equal(*probed, want) { - t.Errorf("carrier existence probes = %v, want %v (one ahead of every SHOW COLUMNS)", *probed, want) - } - if err := mock.ExpectationsWereMet(); err != nil { - t.Fatalf("unmet SQL expectations: %v", err) - } -} - -// A failed carrier probe must surface, not read as "column absent": that would -// clamp a healthy at-latest cursor to the column's replayFloor and replay the -// tail on a catalog nobody could read — the same rule -// TestCursorProbeErrorIsNotTreatedAsContradicted pins for the table sentinels. -func TestSentinelColumnCarrierProbeErrorIsNotTreatedAsContradicted(t *testing.T) { - boom := errors.New("information_schema unavailable") - present := allSentinelTables() - orig := sentinelTableExists - t.Cleanup(func() { sentinelTableExists = orig }) - sentinelTableExists = func(_ context.Context, _ DBConn, table string) (bool, error) { - for _, sc := range ignoredSource.sentinelColumns { - if table == sc.table { - return false, boom - } - } - return present[table], nil - } - db, mock, err := sqlmock.New() - if err != nil { - t.Fatalf("sqlmock.New: %v", err) - } - defer db.Close() - - _, limited, err := ignoredSource.cursorRealityFloor(context.Background(), db) - if !errors.Is(err, boom) { - t.Errorf("carrier probe error was swallowed: err = %v", err) - } - if limited { - t.Error("an unreadable carrier probe reported the cursor as contradicted") - } - if err := mock.ExpectationsWereMet(); err != nil { - t.Fatalf("unmet SQL expectations: %v", err) - } -} - // TestMigrateAppliesUnderContradictedCursor closes the loop the work-needed // test cannot: deciding "work needed" is worthless if the applier then // re-reads the cursor raw, believes it, and applies nothing — MigrateUp would @@ -924,7 +716,8 @@ func TestMigrateAppliesUnderContradictedCursor(t *testing.T) { // The guarded read: cursor claims at-latest, sentinel probe contradicts it. expectCursorProbe(mock, "ignored_schema_migrations", true) expectScalar(mock, "SELECT COALESCE(MAX(version), 0) FROM ignored_schema_migrations", "version", LatestIgnoredVersion()) - expectShowTable(mock, ignoredSource.sentinelTables[0], false) + mock.ExpectQuery(regexp.QuoteMeta("FROM INFORMATION_SCHEMA.TABLES")). + WillReturnRows(sqlmock.NewRows([]string{"count"}).AddRow(0)) // The proof: the applier reaches ignored/0001's SQL. Fail it distinctively // rather than mocking the whole series. @@ -975,17 +768,18 @@ func TestMigrateStartsAboveTheFloorUnderColumnContradiction(t *testing.T) { // corroborate it, and only the leases column does not. expectCursorProbe(mock, "ignored_schema_migrations", true) expectScalar(mock, "SELECT COALESCE(MAX(version), 0) FROM ignored_schema_migrations", "version", LatestIgnoredVersion()) - for _, table := range ignoredSource.sentinelTables { - expectShowTable(mock, table, true) + for range ignoredSource.sentinelTables { + mock.ExpectQuery(regexp.QuoteMeta("FROM INFORMATION_SCHEMA.TABLES")). + WillReturnRows(sqlmock.NewRows([]string{"count"}).AddRow(1)) } // The floored-table sentinels probe the same way and must all corroborate // too, ahead of the column probe below. - for _, st := range ignoredSource.sentinelFlooredTables { - expectShowTable(mock, st.table, true) - } - for _, sc := range ignoredSource.sentinelColumns { - expectSentinelColumnProbe(mock, sc, true, false) + for range ignoredSource.sentinelFlooredTables { + mock.ExpectQuery(regexp.QuoteMeta("FROM INFORMATION_SCHEMA.TABLES")). + WillReturnRows(sqlmock.NewRows([]string{"count"}).AddRow(1)) } + mock.ExpectQuery(regexp.QuoteMeta("FROM INFORMATION_SCHEMA.COLUMNS")). + WillReturnRows(sqlmock.NewRows([]string{"count"}).AddRow(0)) boom := errors.New("stop here: the applier resumed above the floor") mock.ExpectExec(".*").WillReturnError(boom) @@ -1047,84 +841,3 @@ func TestPendingVersionsUnderFloorDoNotReArmAuxMarkers(t *testing.T) { t.Error("ignored/0007 is pending on a cursor-11 store; its unguarded UPDATE would restamp wisps.updated_at") } } - -// TestShowTableExistsAnswersLikeInformationSchema pins showTableExists to the -// INFORMATION_SCHEMA.TABLES count it replaced. Dolt's SHOW TABLES LIKE filter -// matches case-sensitively, while INFORMATION_SCHEMA — like every table -// reference — resolves names case-insensitively, so a LIKE-filtered probe -// reads a table stored as BD_EVENTS_SEQ as absent (dolt 2.1.8 and 2.2.0). The -// probe therefore reads the unfiltered listing and compares names -// case-insensitively and otherwise exactly: a '_' lookalike is not the table, -// and an empty listing is an absent table rather than an error. -func TestShowTableExistsAnswersLikeInformationSchema(t *testing.T) { - db, mock, err := sqlmock.New() - if err != nil { - t.Fatalf("sqlmock.New: %v", err) - } - defer db.Close() - - cases := []struct { - name string - tables []string - probe string - want bool - }{ - {name: "a LIKE lookalike is not the table", tables: []string{"issues", "wispxdependencies"}, probe: "wisp_dependencies", want: false}, - {name: "a name containing the table is not the table", tables: []string{"bd_events_journal", "bd_events_seq"}, probe: "events", want: false}, - {name: "empty database", tables: nil, probe: "wisp_dependencies", want: false}, - {name: "exact name among others", tables: []string{"issues", "wisp_dependencies", "wisps"}, probe: "wisp_dependencies", want: true}, - {name: "stored in another case", tables: []string{"issues", "BD_EVENTS_SEQ"}, probe: "bd_events_seq", want: true}, - } - for _, tc := range cases { - rows := sqlmock.NewRows([]string{"Tables_in_testdb"}) - for _, table := range tc.tables { - rows.AddRow(table) - } - mock.ExpectQuery("^SHOW TABLES$").WillReturnRows(rows) - } - for _, tc := range cases { - got, err := showTableExists(context.Background(), db, tc.probe) - if err != nil { - t.Fatalf("%s: showTableExists: %v", tc.name, err) - } - if got != tc.want { - t.Errorf("%s: showTableExists(%q) = %v, want %v", tc.name, tc.probe, got, tc.want) - } - } - if err := mock.ExpectationsWereMet(); err != nil { - t.Fatalf("unmet expectations: %v", err) - } -} - -// TestCurrentVersionWithoutAUsableDatabaseIssuesNoFailingStatement is the -// be-bv7x rule for the cursor probe itself, which must succeed on any session. -// SHOW TABLES fails with no database selected (1105) and on a session whose -// database was dropped under it (1049), and a failing statement pins the -// pooled session to its pre-statement catalog. The schemata read answers 0 in -// both cases instead, so the probe stops there and reads the cursor as absent, -// exactly as the INFORMATION_SCHEMA.TABLES count did: the strict mock admits -// nothing after it. -func TestCurrentVersionWithoutAUsableDatabaseIssuesNoFailingStatement(t *testing.T) { - for _, src := range []migrationSource{mainSource, ignoredSource} { - t.Run(src.cursorTable, func(t *testing.T) { - db, mock, err := sqlmock.New() - if err != nil { - t.Fatalf("sqlmock.New: %v", err) - } - defer db.Close() - mock.ExpectQuery(regexp.QuoteMeta("SELECT COUNT(*) FROM information_schema.schemata WHERE schema_name = DATABASE()")). - WillReturnRows(sqlmock.NewRows([]string{"count"}).AddRow(0)) - - got, err := src.currentVersion(context.Background(), db) - if err != nil { - t.Fatalf("currentVersion without a usable database: %v", err) - } - if got != 0 { - t.Errorf("currentVersion without a usable database = %d, want 0", got) - } - if err := mock.ExpectationsWereMet(); err != nil { - t.Fatalf("unmet SQL expectations: %v", err) - } - }) - } -} diff --git a/internal/storage/schema/dolt_ignore_seed_test.go b/internal/storage/schema/dolt_ignore_seed_test.go index b68366982..421567caf 100644 --- a/internal/storage/schema/dolt_ignore_seed_test.go +++ b/internal/storage/schema/dolt_ignore_seed_test.go @@ -5,7 +5,6 @@ import ( "database/sql/driver" "errors" "regexp" - "slices" "testing" "github.com/DATA-DOG/go-sqlmock" @@ -209,23 +208,6 @@ func TestSeedDoltIgnorePatternsDegradesToBlindWriteOnPartialRead(t *testing.T) { } } -// The ignored-series table rebuilds (ignored/0001, ignored/0002) stage their -// work through __temp__
intermediates before renaming to the final -// dolt_ignored names. Against a @@dolt_transaction_commit=1 server every -// non-ignored CREATE auto-commits, so without this pattern the intermediates -// materialize as real tables at HEAD and the rename halves wedge dolt_status -// (field incident: three-city shared hub, twice in one day, fleet write -// refusal behind the dirty-table guard). The canonical set must cover the -// intermediates so they are born ignored; dolt's own matcher treats the -// underscores literally (patterns support only ? * %), so the entry matches -// exactly the __temp__ prefix. The engine-level contract is pinned by -// TestEmbeddedTempRebuildIntermediatesNeverReachHistory in embeddeddolt. -func TestDoltIgnorePatternsCoverRebuildIntermediates(t *testing.T) { - if !slices.Contains(doltIgnorePatterns, "__temp__%") { - t.Fatal("doltIgnorePatterns is missing \"__temp__%\": ignored-series rebuild intermediates would auto-commit as tracked tables on dolt_transaction_commit=1 servers") - } -} - func upperASCII(s string) string { b := []byte(s) for i, c := range b { diff --git a/internal/storage/schema/ignored_cursor_untrack.go b/internal/storage/schema/ignored_cursor_untrack.go index 5a7f7d4f0..4b4d9d725 100644 --- a/internal/storage/schema/ignored_cursor_untrack.go +++ b/internal/storage/schema/ignored_cursor_untrack.go @@ -43,11 +43,9 @@ const ( // ignoredCursorUntrackTempTable holds the cursor rows across the drop. // Dolt persists the working set to disk, so an uncommitted scratch table // survives a crash: whatever phase is interrupted, the next open finds - // the rows and finishes the job. The name matches the canonical - // "__temp__%" pattern, so a straggler is hidden from dolt_status rather - // than reported: the cleanup cannot lean on that visibility, which is why - // dropIgnoredCursorScratch probes HEAD directly and stages its sweep with - // a forced DOLT_ADD. + // the rows and finishes the job. The name is deliberately not matched by + // any dolt_ignore pattern — a straggler must show up in dolt_status + // rather than hide. ignoredCursorUntrackTempTable = "__temp__ignored_schema_migrations_untrack" ignoredCursorUntrackCommitMessage = "schema: untrack legacy ignored_schema_migrations so dolt_ignore can apply (gastownhall/beads#4356)" @@ -91,21 +89,16 @@ func (s ignoredCursorState) reconcileNeeded() bool { return s.needsUntrack || s.strayScratch } -// readIgnoredCursorState answers both halves of the gate. On a healthy -// database that is two SHOW TABLES listings, HEAD's and the working set's; a -// dolt_ignore read joins them only when the cursor is tracked at HEAD. None of -// them may fail, and that is not incidental: a Dolt session that issues a -// FAILING statement stays pinned to its pre-statement catalog snapshot for the -// rest of its pooled life (be-bv7x), and this runs on every writable open and -// every lock-free fast-path evaluation. A SHOW TABLES listing fails with no -// database selected (1105) and on a session whose database was dropped (1049), -// so both callers must already have the session on the target database; a -// session that may have no usable database needs tableInCurrentDatabase. +// readIgnoredCursorState answers both halves of the gate in two +// always-succeeding reads. That property is not incidental: a Dolt session +// that issues a FAILING statement stays pinned to its pre-statement catalog +// snapshot for the rest of its pooled life (be-bv7x), and this runs on every +// writable open and every lock-free fast-path evaluation. // // qualifier is the identifier-quoted database name selectTargetDatabase // returned, or empty when the session is already on the target database. The -// scratch probe takes no qualifier: it lists the session's current database, -// which both callers have already made the target. +// scratch probe needs no qualifier: it reads information_schema against +// DATABASE(), which both callers have already put on the target. func readIgnoredCursorState(ctx context.Context, db DBConn, qualifier string) (ignoredCursorState, error) { var state ignoredCursorState @@ -115,10 +108,7 @@ func readIgnoredCursorState(ctx context.Context, db DBConn, qualifier string) (i } state.needsUntrack = needsUntrack - // Both callers have the session on the target database (alreadyConverged - // after selectTargetDatabase, MigrateUp after its preparation), so this - // per-open read takes the SHOW TABLES prober. - stray, err := showTableExists(ctx, db, ignoredCursorUntrackTempTable) + stray, err := schemaTableExists(ctx, db, ignoredCursorUntrackTempTable) if err != nil { return state, fmt.Errorf("probing %s: %w", ignoredCursorUntrackTempTable, err) } @@ -474,17 +464,11 @@ func restoreIgnoredCursorRows(ctx context.Context, db DBConn) error { } // dropIgnoredCursorScratch removes the scratch table and cleans up after the -// windows in which it can end up tracked at HEAD. "__temp__%" closes the one a -// concurrent writer's blanket commit used to open, but it does not close them -// all and cannot reach backwards: a lineage repaired before the pattern -// shipped, a peer still running an older binary, or replication from one can -// all leave the scratch committed. Dropping it locally would then leave a -// permanent delete delta — the same class of tracked residue this whole fix -// exists to remove — so the deletion is committed, scoped to that table. -// -// That commit must FORCE its staging (commitScopedIgnoredTableChange): the -// scratch's name is ignore-matched now, and unforced staging would drop it -// silently while --skip-empty reported success. +// one window in which it can end up tracked: it carries a perfectly +// committable name, so a concurrent writer's blanket commit can sweep it into +// HEAD during the repair. Dropping it locally would then leave a permanent +// delete delta — the same class of tracked residue this whole fix exists to +// remove — so the deletion is committed, scoped to that table. func dropIgnoredCursorScratch(ctx context.Context, db DBConn) error { //nolint:gosec // G201: the table name is a constant. if _, err := db.ExecContext(ctx, "DROP TABLE IF EXISTS "+ignoredCursorUntrackTempTable); err != nil { @@ -512,7 +496,7 @@ func dropIgnoredCursorScratch(ctx context.Context, db DBConn) error { if err := unstageBeforeIgnoredCursorUntrack(ctx, db); err != nil { return fmt.Errorf("unstaging before dropping %s: %w", ignoredCursorUntrackTempTable, err) } - return commitScopedIgnoredTableChange(ctx, db, ignoredCursorUntrackTempTable, ignoredCursorTempSweepCommitMessage) + return commitScopedTableChange(ctx, db, ignoredCursorUntrackTempTable, ignoredCursorTempSweepCommitMessage) } // ignoredCursorCopyColumns is the column list to move out of source, which is diff --git a/internal/storage/schema/ignored_cursor_untrack_test.go b/internal/storage/schema/ignored_cursor_untrack_test.go index 466834a2e..eb18b744c 100644 --- a/internal/storage/schema/ignored_cursor_untrack_test.go +++ b/internal/storage/schema/ignored_cursor_untrack_test.go @@ -120,7 +120,7 @@ func expectIgnoredCursorGate(mock sqlmock.Sqlmock, qualifier string, tracked boo if tracked { expectIgnoreResolution(mock, qualifier, ignoredSource.cursorTable, matches) } - expectShowTable(mock, ignoredCursorUntrackTempTable, stray) + expectSchemaTableExists(mock, ignoredCursorUntrackTempTable, stray) } // expectIgnoredCursorHealNoop mocks the whole open-time reconcile on a healthy @@ -184,12 +184,7 @@ func expectIgnoredCursorScratchDrop(mock sqlmock.Sqlmock, sweptIntoHead bool) { return } expectIgnoredCursorUnstage(mock) - // Forced: the scratch's name matches the seeded "__temp__%" pattern, and - // an unforced DOLT_ADD would drop it from the staging list silently — the - // filter classifies by pattern alone and never consults HEAD, so being - // committed at HEAD does not exempt it. --skip-empty would then report - // success having committed nothing. - mock.ExpectQuery(regexp.QuoteMeta("CALL DOLT_ADD('-f', ?)")). + mock.ExpectQuery(regexp.QuoteMeta("CALL DOLT_ADD(?)")). WithArgs(ignoredCursorUntrackTempTable). WillReturnRows(sqlmock.NewRows([]string{"status"})) mock.ExpectQuery(regexp.QuoteMeta("CALL DOLT_COMMIT('-m', ?, '--skip-empty')")). @@ -488,13 +483,12 @@ func TestHealCopiesLegacyShapeWithoutContentHash(t *testing.T) { } } -// The scratch table can be committed at HEAD by any writer that predates the -// "__temp__%" pattern — a lineage repaired before it shipped, an older binary's -// blanket commit, or replication from such a peer. Dropping it locally would -// then leave a permanent delete delta — the same class of tracked residue this -// whole fix exists to remove — so the deletion is committed, scoped to that -// table, after the staging area is cleared so nothing unrelated rides along -// under its message, and FORCED past the pattern that now matches its name. +// The scratch table carries a perfectly committable name, so a concurrent +// writer's blanket commit can sweep it into HEAD during the repair window. +// Dropping it locally would then leave a permanent delete delta — the same +// class of tracked residue this whole fix exists to remove — so the deletion +// is committed, scoped to that table, after the staging area is cleared so +// nothing unrelated rides along under its message. func TestHealCommitsAStrayScratchTableOutOfHead(t *testing.T) { db, mock := newMockDB(t) @@ -764,7 +758,7 @@ func TestAlreadyConvergedDeclinesOnALegacyTrackedCursor(t *testing.T) { expectDoltIgnoreRead(mock, unqualifiedDoltIgnore, seededIgnorePatterns(LatestVersion())) expectHeadTableProbe(mock, "", ignoredSource.cursorTable, true) expectIgnoreResolution(mock, "", ignoredSource.cursorTable, exactlyIgnored(true)) - expectShowTable(mock, ignoredCursorUntrackTempTable, false) + expectSchemaTableExists(mock, ignoredCursorUntrackTempTable, false) converged, err := alreadyConverged(context.Background(), db, "testdb", nil) if err != nil { @@ -793,7 +787,7 @@ func TestAlreadyConvergedDeclinesOnASurvivingScratchTable(t *testing.T) { expectNoMigrationWorkNeeded(mock) expectDoltIgnoreRead(mock, unqualifiedDoltIgnore, seededIgnorePatterns(LatestVersion())) expectHeadTableProbe(mock, "", ignoredSource.cursorTable, false) - expectShowTable(mock, ignoredCursorUntrackTempTable, true) + expectSchemaTableExists(mock, ignoredCursorUntrackTempTable, true) converged, err := alreadyConverged(context.Background(), db, "testdb", nil) if err != nil { @@ -820,7 +814,7 @@ func TestAlreadyConvergedAcceptsATrackedCursorTheOperatorUnignored(t *testing.T) expectDoltIgnoreRead(mock, unqualifiedDoltIgnore, seededIgnorePatterns(LatestVersion())) expectHeadTableProbe(mock, "", ignoredSource.cursorTable, true) expectIgnoreResolution(mock, "", ignoredSource.cursorTable, exactlyIgnored(false)) - expectShowTable(mock, ignoredCursorUntrackTempTable, false) + expectSchemaTableExists(mock, ignoredCursorUntrackTempTable, false) expectMigrationLockProbe(mock, "testdb", 1) converged, err := alreadyConverged(context.Background(), db, "testdb", nil) @@ -864,7 +858,7 @@ func TestAlreadyConvergedKeepsTheLockProbeLast(t *testing.T) { expectDoltIgnoreRead(mock, unqualifiedDoltIgnore, seededIgnorePatterns(LatestVersion())) expectHeadTableProbe(mock, "", ignoredSource.cursorTable, true) expectIgnoreResolution(mock, "", ignoredSource.cursorTable, exactlyIgnored(true)) - expectShowTable(mock, ignoredCursorUntrackTempTable, false) + expectSchemaTableExists(mock, ignoredCursorUntrackTempTable, false) if _, err := alreadyConverged(context.Background(), db, "testdb", nil); err != nil { t.Fatalf("alreadyConverged() error = %v", err) diff --git a/internal/storage/schema/lock.go b/internal/storage/schema/lock.go index 5f6602229..a51f9cdfd 100644 --- a/internal/storage/schema/lock.go +++ b/internal/storage/schema/lock.go @@ -21,14 +21,6 @@ const ( // lock wait can time out and still leave room for a real retry. migrationLockAcquireTimeoutSeconds = 5 migrationLockCleanupTimeout = 5 * time.Second - // freshBootstrapResetAttempts bounds the heal path's DOLT_RESET retries. - // Small on purpose: this is a last-chance recovery, not a general retry - // budget, and a reset that is genuinely refused must still fail fast. - freshBootstrapResetAttempts = 3 - // freshBootstrapResetRetryDelay spaces those attempts. The aborts this - // absorbs land within a millisecond, so the delay only has to let the - // session settle, not to wait out load. - freshBootstrapResetRetryDelay = 50 * time.Millisecond ) var ( @@ -326,14 +318,7 @@ func MigrateUpWithLock(ctx context.Context, conn *sql.Conn, databaseName string, } } - // The same authority is consent for MigrateUp's own gate, which is that - // version-only test: an init that never sets local consent would otherwise - // be refused the retry of its own interrupted bootstrap. - migrate := MigrateUp - if o.freshBootstrapHeal != nil { - migrate = migrateUpConsented - } - applied, err = migrate(ctx, conn) + applied, err = MigrateUp(ctx, conn) var dirtyErr *DirtyTablesError if err != nil && o.freshBootstrapHeal != nil && errors.As(err, &dirtyErr) { // Authorization is checked after the dirty guard fires and while the @@ -351,61 +336,15 @@ func MigrateUpWithLock(ctx context.Context, conn *sql.Conn, databaseName string, // second reset in the caller's outer retry loop. fmt.Fprintf(stderr, "Discarding interrupted-bootstrap working set (%s) and re-running migrations…\n", strings.Join(dirtyErr.Tables, ", ")) - if resetErr := drainFreshBootstrapReset(ctx, conn); resetErr != nil { - return applied, errors.Join(err, fmt.Errorf("schema: fresh-bootstrap reset: %w", resetErr)) - } - applied, err = migrate(ctx, conn) - } - return applied, err -} - -// drainFreshBootstrapReset discards an interrupted bootstrap's working set, -// retrying an attempt that the server refused for a reason that is not the -// caller's own cancellation. -// -// Retrying matters here specifically because the capability is already spent. -// consumeIfCurrentIncarnation consumes it before the first attempt so that no -// later pass can re-arm a second destructive reset, which also means whatever -// this call returns is final for the database: a reset that fails leaves the -// interrupted bootstrap refusing every future migration with DirtyTablesError, -// and the only way back is a human running 'bd dolt commit'. Treating a single -// refusal as final spends that one chance on a failure the next attempt would -// not have hit. -// -// Dolt does abort a statement that way. On a healthy pinned session, with no -// other session on the server and the statements either side of it succeeding, -// CALL DOLT_RESET can come back as Error 1105 "context canceled" about a -// millisecond after the server logs it starting — an abort from inside the -// server, not a cancellation from this process (gastownhall/beads#5836 lane, -// TestFreshBootstrapHealIncarnation). -// -// Retrying is safe: DOLT_RESET('--hard') is idempotent, and this stays inside -// one authorized heal — same locked session, same incarnation, one consumed -// capability. ctx.Err() is what separates the server aborting us from us being -// canceled; the latter is not transient and must not spend the attempts left. -func drainFreshBootstrapReset(ctx context.Context, conn *sql.Conn) error { - var err error - for attempt := range freshBootstrapResetAttempts { - if attempt > 0 { - fmt.Fprintf(stderr, "Bootstrap reset attempt %d/%d failed (%v); retrying…\n", - attempt, freshBootstrapResetAttempts, err) - select { - case <-ctx.Done(): - return err - case <-time.After(freshBootstrapResetRetryDelay): - } - } // Drained, not Exec'd: the very next thing this path does is re-run the // whole MigrateUp pass on this same pinned connection, so an // undrained proc result set here would poison every statement of it. - if err = DrainCall(ctx, conn, "CALL DOLT_RESET('--hard')"); err == nil { - return nil - } - if ctx.Err() != nil { - return err + if resetErr := DrainCall(ctx, conn, "CALL DOLT_RESET('--hard')"); resetErr != nil { + return applied, errors.Join(err, fmt.Errorf("schema: fresh-bootstrap reset: %w", resetErr)) } + applied, err = MigrateUp(ctx, conn) } - return err + return applied, err } // consumeIfCurrentIncarnation validates and atomically consumes c. All probes diff --git a/internal/storage/schema/lock_test.go b/internal/storage/schema/lock_test.go index d2a1c7eaf..c8cb9dc30 100644 --- a/internal/storage/schema/lock_test.go +++ b/internal/storage/schema/lock_test.go @@ -258,7 +258,7 @@ func TestMigrateUpSeedsIgnorePatternsWhenNoWorkNeeded(t *testing.T) { // MigrateUp must commit the heal itself, scoped and labeled. mock.ExpectQuery(regexp.QuoteMeta("CALL DOLT_ADD('dolt_ignore')")). WillReturnRows(sqlmock.NewRows([]string{"status"})) - mock.ExpectQuery(regexp.QuoteMeta("CALL DOLT_COMMIT('-m', 'schema: seed dolt_ignore patterns', '--skip-empty')")). + mock.ExpectQuery(regexp.QuoteMeta("CALL DOLT_COMMIT('-m', 'schema: seed dolt_ignore patterns')")). WillReturnRows(sqlmock.NewRows([]string{"hash"})) applied, err := MigrateUp(context.Background(), db) @@ -386,7 +386,7 @@ func expectOnePendingMigration(t *testing.T, mock sqlmock.Sqlmock) { // seed must not ride the per-step pass commits). mock.ExpectQuery(regexp.QuoteMeta("CALL DOLT_ADD('dolt_ignore')")). WillReturnRows(sqlmock.NewRows([]string{"status"})) - mock.ExpectQuery(regexp.QuoteMeta("CALL DOLT_COMMIT('-m', 'schema: seed dolt_ignore patterns', '--skip-empty')")). + mock.ExpectQuery(regexp.QuoteMeta("CALL DOLT_COMMIT('-m', 'schema: seed dolt_ignore patterns')")). WillReturnRows(sqlmock.NewRows([]string{"hash"})) expectDoltStatusRows(mock) // MigrateUp captures the pre-pass main cursor for the aux re-key watershed @@ -511,15 +511,18 @@ func expectScalar(mock sqlmock.Sqlmock, query, column string, value any) { WillReturnRows(sqlmock.NewRows([]string{column}).AddRow(value)) } -// expectCursorProbe mocks the existence probe that migrationSource.currentVersion -// issues before it ever reads the cursor table: the schemata read proving the -// session has a usable current database, then the SHOW TABLES listing. The -// probe was added by be-bv7x so a not-yet-created cursor table never poisons -// the pooled Dolt session with a failing statement. +// expectCursorProbe mocks the information_schema existence probe that +// migrationSource.currentVersion issues before it ever reads the cursor +// table. The probe was added by be-bv7x so a not-yet-created cursor table +// never poisons the pooled Dolt session with a failing statement. func expectCursorProbe(mock sqlmock.Sqlmock, table string, exists bool) { - mock.ExpectQuery(regexp.QuoteMeta("SELECT COUNT(*) FROM information_schema.schemata WHERE schema_name = DATABASE()")). - WillReturnRows(sqlmock.NewRows([]string{"count"}).AddRow(1)) - expectShowTable(mock, table, exists) + present := 0 + if exists { + present = 1 + } + mock.ExpectQuery(`SELECT COUNT\(\*\) FROM information_schema\.tables`). + WithArgs(table). + WillReturnRows(sqlmock.NewRows([]string{"count"}).AddRow(present)) } func expectDoltStatusRows(mock sqlmock.Sqlmock) { @@ -703,139 +706,31 @@ func TestMigrateUpWithLockFreshBootstrapHealResetsAndRetries(t *testing.T) { } } -// TestMigrateUpWithLockFreshBootstrapHealRetriesAbortedReset covers the reset -// the server aborts rather than refuses. The capability is consumed before the -// first attempt and cannot be re-armed, so treating that abort as final would -// strand the interrupted bootstrap on DirtyTablesError for good; the heal has -// to finish on a later attempt of the same authorized reset. -func TestMigrateUpWithLockFreshBootstrapHealRetriesAbortedReset(t *testing.T) { - db, mock, err := sqlmock.New() - if err != nil { - t.Fatalf("create sql mock: %v", err) - } - defer db.Close() - - ctx := context.Background() - conn, err := db.Conn(ctx) - if err != nil { - t.Fatalf("pin mock connection: %v", err) - } - defer conn.Close() - - lockName := MigrationLockName("testdb") - mock.ExpectQuery(regexp.QuoteMeta("SELECT GET_LOCK(?, ?)")). - WithArgs(lockName, migrationLockAcquireTimeoutSeconds). - WillReturnRows(sqlmock.NewRows([]string{"locked"}).AddRow(1)) - expectDirtyGuardRefusal(t, mock) - expectFreshBootstrapIdentityMatch(mock) - // The abort Dolt actually produces: a server-side Error 1105 raised about a - // millisecond in, on a session that is fine before and after it. - mock.ExpectQuery(regexp.QuoteMeta("CALL DOLT_RESET('--hard')")). - WillReturnError(errors.New("Error 1105 (HY000): context canceled")) - mock.ExpectQuery(regexp.QuoteMeta("CALL DOLT_RESET('--hard')")). - WillReturnRows(sqlmock.NewRows([]string{"status"})) - expectOnePendingMigration(t, mock) - mock.ExpectQuery(regexp.QuoteMeta("SELECT RELEASE_LOCK(?)")). - WithArgs(lockName). - WillReturnRows(sqlmock.NewRows([]string{"released"}).AddRow(1)) - - capability := testFreshBootstrapHealCapability() - applied, err := MigrateUpWithLock(ctx, conn, "testdb", - WithFreshBootstrapHeal(capability, testBootstrapEndpoint)) - if err != nil { - t.Fatalf("MigrateUpWithLock() error = %v, want the retried reset to heal", err) - } - if applied != 1 { - t.Fatalf("MigrateUpWithLock() applied = %d, want 1", applied) - } - if !capability.consumed.Load() { - t.Fatal("heal ran without consuming the capability") - } - if err := mock.ExpectationsWereMet(); err != nil { - t.Fatalf("unmet SQL expectations: %v", err) - } -} - -// TestMigrateUpWithLockFreshBootstrapHealResetFailureStaysFatal pins the other -// half: retrying is bounded, and a reset the server keeps refusing still fails -// the migration with both the original refusal and the reset error. sqlmock's -// ordered expectations fail the test on a fourth attempt. -func TestMigrateUpWithLockFreshBootstrapHealResetFailureStaysFatal(t *testing.T) { - db, mock, err := sqlmock.New() - if err != nil { - t.Fatalf("create sql mock: %v", err) - } - defer db.Close() - - ctx := context.Background() - conn, err := db.Conn(ctx) - if err != nil { - t.Fatalf("pin mock connection: %v", err) - } - defer conn.Close() - - lockName := MigrationLockName("testdb") - mock.ExpectQuery(regexp.QuoteMeta("SELECT GET_LOCK(?, ?)")). - WithArgs(lockName, migrationLockAcquireTimeoutSeconds). - WillReturnRows(sqlmock.NewRows([]string{"locked"}).AddRow(1)) - expectDirtyGuardRefusal(t, mock) - expectFreshBootstrapIdentityMatch(mock) - for range freshBootstrapResetAttempts { - mock.ExpectQuery(regexp.QuoteMeta("CALL DOLT_RESET('--hard')")). - WillReturnError(errors.New("reset refused")) - } - mock.ExpectQuery(regexp.QuoteMeta("SELECT RELEASE_LOCK(?)")). - WithArgs(lockName). - WillReturnRows(sqlmock.NewRows([]string{"released"}).AddRow(1)) - - _, err = MigrateUpWithLock(ctx, conn, "testdb", - WithFreshBootstrapHeal(testFreshBootstrapHealCapability(), testBootstrapEndpoint)) - var dirtyErr *DirtyTablesError - if !errors.As(err, &dirtyErr) { - t.Fatalf("MigrateUpWithLock() error = %v, want the original *DirtyTablesError joined in", err) - } - if !strings.Contains(err.Error(), "reset refused") { - t.Fatalf("MigrateUpWithLock() error = %v, want the reset failure reported", err) - } - if err := mock.ExpectationsWereMet(); err != nil { - t.Fatalf("unmet SQL expectations: %v", err) - } -} - -// expectIgnoredSentinelProbes mocks the schema lookups currentVersion issues -// to confirm a non-zero ignored cursor against the schema it claims (gh 5033): -// a SHOW TABLES probe per sentinel and floored table (showTableExists), then -// per sentinel column its carrier table's existence and a SHOW COLUMNS read -// (expectSentinelColumnProbe). None of them uses INFORMATION_SCHEMA, whose -// predicates Dolt does not push down, because all of them run on every store -// open. They fire only for a non-zero cursor, in ignoredSource's table then -// floored-table then column sentinel order. +// expectIgnoredSentinelProbes mocks the INFORMATION_SCHEMA lookups +// currentVersion issues to confirm a non-zero ignored cursor against the +// schema it claims (gh 5033). They fire only for a non-zero cursor, in +// ignoredSource's table then floored-table then column sentinel order. func expectIgnoredSentinelProbes(mock sqlmock.Sqlmock, present bool) { - for _, table := range ignoredSource.sentinelTables { - expectShowTable(mock, table, present) + count := 0 + if present { + count = 1 + } + for range ignoredSource.sentinelTables { + mock.ExpectQuery(regexp.QuoteMeta("FROM INFORMATION_SCHEMA.TABLES")). + WillReturnRows(sqlmock.NewRows([]string{"count"}).AddRow(count)) } if present { - for _, st := range ignoredSource.sentinelFlooredTables { - expectShowTable(mock, st.table, true) + for range ignoredSource.sentinelFlooredTables { + mock.ExpectQuery(regexp.QuoteMeta("FROM INFORMATION_SCHEMA.TABLES")). + WillReturnRows(sqlmock.NewRows([]string{"count"}).AddRow(1)) } - for _, sc := range ignoredSource.sentinelColumns { - expectSentinelColumnProbe(mock, sc, true, true) + for range ignoredSource.sentinelColumns { + mock.ExpectQuery(regexp.QuoteMeta("FROM INFORMATION_SCHEMA.COLUMNS")). + WillReturnRows(sqlmock.NewRows([]string{"count"}).AddRow(1)) } } } -// expectShowTable mocks one showTableExists probe: the database's unfiltered -// SHOW TABLES listing, which holds table only when it exists. An absent table -// is a listing without it, never a zero count. The listing always carries an -// unrelated table, so a probe that took any row as presence fails. -func expectShowTable(mock sqlmock.Sqlmock, table string, exists bool) { - rows := sqlmock.NewRows([]string{"Tables_in_testdb"}).AddRow("issues") - if exists { - rows.AddRow(table) - } - mock.ExpectQuery("^SHOW TABLES$").WillReturnRows(rows) -} - func TestMigrateUpWithLockFreshBootstrapHealProbeFailuresStayFatal(t *testing.T) { tests := []struct { name string @@ -1023,124 +918,6 @@ func TestMigrateUpWithLockFreshBootstrapHealCapabilityIsOneShot(t *testing.T) { } } -// expectMigrateConsentReads mocks checkMigrateConsent's reads when no consent -// is held: the main cursor once for the fresh-database exemption, and again -// for the pending count. -func expectMigrateConsentReads(mock sqlmock.Sqlmock, cursor int) { - for range 2 { - expectCursorProbe(mock, "schema_migrations", true) - expectScalar(mock, "SELECT COALESCE(MAX(version), 0) FROM schema_migrations", "version", cursor) - } -} - -// TestMigrateUpWithLockFreshBootstrapHealIsMigrateConsent pins that heal -// authority is consent for MigrateUp's own gate, not just for the caller's -// migration gate. A retried server bootstrap finds the non-zero cursor its -// first pass left behind, and server-mode `bd init` records no local consent, -// so a version-only consent check would refuse the init the database it just -// created. Both migration passes are covered; sqlmock's ordered expectations -// fail either case on a consent read. -func TestMigrateUpWithLockFreshBootstrapHealIsMigrateConsent(t *testing.T) { - cases := map[string]func(t *testing.T, mock sqlmock.Sqlmock){ - "partially migrated": func(t *testing.T, mock sqlmock.Sqlmock) { - expectOnePendingMigration(t, mock) - }, - "partially migrated with dirty debris": func(t *testing.T, mock sqlmock.Sqlmock) { - expectDirtyGuardRefusal(t, mock) - expectFreshBootstrapIdentityMatch(mock) - mock.ExpectQuery(regexp.QuoteMeta("CALL DOLT_RESET('--hard')")). - WillReturnRows(sqlmock.NewRows([]string{"status"})) - expectOnePendingMigration(t, mock) - }, - } - for name, expectPasses := range cases { - t.Run(name, func(t *testing.T) { - failOnSwallowedAdvisory(t) - resetConsentState(t) - db, mock, err := sqlmock.New() - if err != nil { - t.Fatalf("create sql mock: %v", err) - } - defer db.Close() - - ctx := context.Background() - conn, err := db.Conn(ctx) - if err != nil { - t.Fatalf("pin mock connection: %v", err) - } - defer conn.Close() - - lockName := MigrationLockName("testdb") - mock.ExpectQuery(regexp.QuoteMeta("SELECT GET_LOCK(?, ?)")). - WithArgs(lockName, migrationLockAcquireTimeoutSeconds). - WillReturnRows(sqlmock.NewRows([]string{"locked"}).AddRow(1)) - expectPasses(t, mock) - mock.ExpectQuery(regexp.QuoteMeta("SELECT RELEASE_LOCK(?)")). - WithArgs(lockName). - WillReturnRows(sqlmock.NewRows([]string{"released"}).AddRow(1)) - - applied, err := MigrateUpWithLock(ctx, conn, "testdb", - WithFreshBootstrapHeal(testFreshBootstrapHealCapability(), testBootstrapEndpoint)) - if err != nil { - t.Fatalf("MigrateUpWithLock() error = %v", err) - } - if applied != 1 { - t.Fatalf("MigrateUpWithLock() applied = %d, want 1", applied) - } - if err := mock.ExpectationsWereMet(); err != nil { - t.Fatalf("unmet SQL expectations: %v", err) - } - }) - } -} - -// TestMigrateUpWithLockRefusesUnconsentedMigration is the control for the -// test above: the same behind database, opened without heal authority or -// consent, is refused under the lock before any write, and the lock is still -// released. -func TestMigrateUpWithLockRefusesUnconsentedMigration(t *testing.T) { - failOnSwallowedAdvisory(t) - resetConsentState(t) - db, mock, err := sqlmock.New() - if err != nil { - t.Fatalf("create sql mock: %v", err) - } - defer db.Close() - - ctx := context.Background() - conn, err := db.Conn(ctx) - if err != nil { - t.Fatalf("pin mock connection: %v", err) - } - defer conn.Close() - - lockName := MigrationLockName("testdb") - expectConvergedFastPathMiss(mock, "testdb") - mock.ExpectQuery(regexp.QuoteMeta("SELECT GET_LOCK(?, ?)")). - WithArgs(lockName, migrationLockAcquireTimeoutSeconds). - WillReturnRows(sqlmock.NewRows([]string{"locked"}).AddRow(1)) - expectMigrateConsentReads(mock, LatestVersion()-1) - mock.ExpectQuery(regexp.QuoteMeta("SELECT RELEASE_LOCK(?)")). - WithArgs(lockName). - WillReturnRows(sqlmock.NewRows([]string{"released"}).AddRow(1)) - - applied, err := MigrateUpWithLock(ctx, conn, "testdb", WithDatabaseSelector(testDatabaseSelector)) - if applied != 0 { - t.Fatalf("MigrateUpWithLock() applied = %d, want 0", applied) - } - var consentErr *MigrateConsentError - if !errors.As(err, &consentErr) { - t.Fatalf("MigrateUpWithLock() error = %v, want *MigrateConsentError", err) - } - if consentErr.CurrentVersion != LatestVersion()-1 || consentErr.Pending != 1 { - t.Fatalf("refusal = v%d with %d pending, want v%d with 1 pending", - consentErr.CurrentVersion, consentErr.Pending, LatestVersion()-1) - } - if err := mock.ExpectationsWereMet(); err != nil { - t.Fatalf("unmet SQL expectations: %v", err) - } -} - // TestMigrateUpWithLockMigrationGate pins where WithMigrationGate's callback // sits: after the lock and after locked preparation (so the gate reads a // prepared, selected database), before MigrateUp (so a refusal applies no diff --git a/internal/storage/schema/migrate_consent.go b/internal/storage/schema/migrate_consent.go deleted file mode 100644 index a8fcc9eba..000000000 --- a/internal/storage/schema/migrate_consent.go +++ /dev/null @@ -1,235 +0,0 @@ -package schema - -import ( - "context" - "errors" - "fmt" - "os" - "strconv" -) - -// Migration consent (gastownhall Beads 1.2 release remediation). -// -// bd used to auto-apply pending schema migrations the first time a new binary -// opened an existing database — on ANY command, including reads. The 1.2.x -// releases turned that into a trap: users upgraded by a package manager had -// their databases migrated in place with no prompt, and older binaries then -// refuse the migrated database (schema-skew guard). The remote-migrate gate -// (#4259) already refuses the silent migration for remote-backed databases; -// this gate extends the same contract to EVERY existing database: bd never -// initiates a schema migration without explicit operator consent. -// -// Consent is any of: -// - `bd migrate schema` (the verb that names the migration; the root -// command records the intent via SetLocalMigrateConsent before the store -// opens — bare `bd migrate` reconciles metadata and does not consent), -// - --force on `bd migrate` or `bd migrate schema` (the remote-gate -// override, a strict superset), or -// - BD_ALLOW_MIGRATE=1 (scripted/CI form; BD_ALLOW_REMOTE_MIGRATE=1 is -// honored as an alias for continuity with the remote gate's hatch). -// -// Fresh databases (no schema_migrations table, version 0) are exempt: creating -// a database is consent for its schema. A server bootstrap holding -// fresh-bootstrap heal authority keeps that consent across a retry whose -// interrupted first pass left a non-zero cursor (MigrateUpWithLock). The same -// creation principle covers a workspace this invocation just CLONED into -// existence (`bd init --remote`, `bd bootstrap` sync) — those paths record the -// consent explicitly, and the #4259 remote-migrate gate still applies to them -// unchanged. Databases already at this binary's latest version never consult -// the gate. The ignored (clone-local, dolt_ignored) migration sequence is -// deliberately NOT gated on its own: it only materializes per-clone -// bookkeeping tables and runs on every fresh clone — but when the MAIN -// sequence is pending and unconsented, MigrateUp refuses before ANY write, -// ignored sequence and dolt_ignore seeding included. -// -// A refusal fails the open, except for the working-set-reconcile commands -// (bd dolt commit, bd vc commit), which commit at the current schema without -// migrating: the refusal comes before the #4566 dirty-table guard, whose -// documented recovery is that commit (WorkingSetWarning). - -// AllowMigrateEnv, when set to a boolean true ("1", "true", ...), consents to -// applying pending schema migrations to an existing database. It is consulted -// only when migrations are actually pending, so exporting it permanently does -// not warn on every store open. -const AllowMigrateEnv = "BD_ALLOW_MIGRATE" - -// localMigrateConsent is the programmatic consent recorded by -// `bd migrate schema` and by the paths that just cloned the database into -// existence. Process-local by design, like forceAllowRemoteMigrate: it cannot -// leak into child processes. -var localMigrateConsent bool - -// SetLocalMigrateConsent records (or clears) the in-process consent to apply -// pending schema migrations to an existing database. Set by the root command -// when the invoked command is `bd migrate schema`, before both -// autoMigrateOnVersionBump and the main store open. External test packages may -// reset it to false after each test case. -func SetLocalMigrateConsent(v bool) { localMigrateConsent = v } - -// MigrateConsentError is returned when bd would apply pending schema -// migrations to an existing database without operator consent. -type MigrateConsentError struct { - CurrentVersion int - LatestVersion int - Pending int - // UnrecognizedEnv carries a BD_ALLOW_MIGRATE value that was set but not - // understood (only boolean values consent), so a typo'd hatch fails with - // a hint instead of silently staying locked (0054-gate precedent, - // bd-6dnrw.34). - UnrecognizedEnv string -} - -func (e *MigrateConsentError) Error() string { - return fmt.Sprintf("%s — run `bd migrate schema`, or keep using a bd release that matches schema v%d", - e.Refusal(), e.CurrentVersion) -} - -// Refusal states what was refused without prescribing a remedy, for surfaces -// that carry the remedy separately and must not hand an agent a runnable -// migration (cmd/bd's JSON refusal, whose remedies live in Options). -func (e *MigrateConsentError) Refusal() string { - return fmt.Sprintf( - "database schema is at v%d; this bd requires v%d (%d pending migration(s)), and bd no longer migrates a database without explicit consent", - e.CurrentVersion, e.LatestVersion, e.Pending) -} - -// AgentDirective is the non-runnable instruction surfaced to agents in place -// of a ready-to-run migrate command, following the remote-migrate gate's -// convention (RemoteMigrateGateError.AgentDirective). Migrating is one-way, -// and whether anything still has to open this database with an older bd is -// not observable from this process, so the decision goes to the operator. -func (e *MigrateConsentError) AgentDirective() string { - return fmt.Sprintf( - "Operator decision required: this database is at schema v%d and this bd requires v%d. Migrating is ONE-WAY — "+ - "bd releases built for an older schema refuse the migrated database, and whether one still has to open it "+ - "is not observable from this process. Do NOT auto-run the migration or set a consent environment variable "+ - "to get past this refusal: surface migrate_consent.options to the operator and let them choose.", - e.CurrentVersion, e.LatestVersion) -} - -// Options returns the two mutually exclusive paths, migrate or keep the -// current schema, each gated on its precondition and annotated with its risk. -// The migrate command is reachable only through the operator's decision, -// never as a top-level hint. Its verb is the shared-store gate's consent verb -// too, so cmd/bd retargets both with the same --global rewrite. -func (e *MigrateConsentError) Options() []GateOption { - return []GateOption{ - { - ID: "migrate", - When: "the operator chooses to upgrade this database, and nothing that still opens it runs a bd release built for an older schema (confirmed with the operator)", - Commands: []string{SharedConsentCommand}, - Risk: fmt.Sprintf("one-way: v%d -> v%d; bd releases built for an older schema refuse the migrated database", e.CurrentVersion, e.LatestVersion), - }, - { - ID: "keep", - When: fmt.Sprintf("something still has to open this database with a bd release built for schema v%d", e.CurrentVersion), - Commands: []string{}, - Risk: "none to the database; this bd release refuses it until it is migrated, so keep using the release that matches it", - }, - } -} - -// UserMessage renders the full operator-facing refusal. -func (e *MigrateConsentError) UserMessage() string { - msg := fmt.Sprintf( - "This database is at schema v%d; this bd release uses schema v%d.\n"+ - "bd no longer upgrades a database automatically (see the release notes).\n"+ - "\n"+ - " To upgrade this database (one-way; older bd releases cannot read it):\n"+ - " bd migrate schema\n"+ - "\n"+ - " To keep the current schema, keep using the bd release that created it.\n"+ - "\n"+ - " Scripted/CI consent: BD_ALLOW_MIGRATE=1 bd \n", - e.CurrentVersion, e.LatestVersion) - if e.UnrecognizedEnv != "" { - msg += fmt.Sprintf( - "\nNote: %s=%q was set but not understood (use a boolean value, e.g. \"1\" or \"true\"), so it did not consent.\n", - AllowMigrateEnv, e.UnrecognizedEnv) - } - return msg -} - -// WorkingSetWarning renders the stderr warning for a working-set-reconcile -// open (bd dolt commit, bd vc commit) that continues past this refusal. The -// commit needs no migration, and MigrateUp refused before any write, so it -// runs on the current schema; the migration still waits for consent. Both -// storage modes print it (embeddeddolt's openWorkingSetReconcile, server -// mode's LenientOpen). -func (e *MigrateConsentError) WorkingSetWarning() string { - return fmt.Sprintf( - "Warning: %s.\n"+ - " Working-set reconcile command: continuing on schema v%d without\n"+ - " migrating; the commit applies to the working set at the current schema.\n"+ - " To upgrade this database afterwards (one-way): bd migrate schema\n", - e.Refusal(), e.CurrentVersion) -} - -// IsMigrateConsentError reports whether err (or any error it wraps) is a -// *MigrateConsentError. -func IsMigrateConsentError(err error) bool { - var e *MigrateConsentError - return errors.As(err, &e) -} - -// checkMigrateConsent refuses an unconsented migration of an existing -// database. Called at the top of MigrateUp, before any write (dolt_ignore -// seeding included). Returns nil for a fresh database (version 0), a database -// with no pending main-sequence migrations, or when consent was given. -func checkMigrateConsent(ctx context.Context, db DBConn) error { - if localMigrateConsent || forceAllowRemoteMigrate { - return nil // programmatic consent — skip the version reads entirely - } - current, err := mainSource.currentVersion(ctx, db) - if err != nil { - return fmt.Errorf("migrate consent: read current version: %w", err) - } - if current == 0 { - return nil // fresh database — creating it is consent for its schema - } - pending, err := mainSource.pendingVersions(ctx, db) - if err != nil { - return fmt.Errorf("migrate consent: read pending versions: %w", err) - } - return migrateConsentDecision(current, len(pending)) -} - -// migrateConsentDecision is checkMigrateConsent's pure decision core: nil when -// nothing is pending or consent was given (programmatic or env), a typed -// *MigrateConsentError otherwise. current is known to be > 0 here except when -// callers short-circuit earlier; a zero current is still treated as consent -// (fresh database). -func migrateConsentDecision(current, pending int) error { - if current == 0 || pending == 0 { - return nil - } - if localMigrateConsent || forceAllowRemoteMigrate { - return nil - } - unrecognized := "" - for _, env := range []string{AllowMigrateEnv, AllowRemoteMigrateEnv} { - v := os.Getenv(env) - if v == "" { - continue - } - allowed, perr := strconv.ParseBool(v) - if perr != nil { - if env == AllowMigrateEnv { - unrecognized = v - } - continue - } - if allowed { - fmt.Fprintf(defaultStderr(), - "Applying %d pending schema migration(s) (%s=%s): v%d -> v%d\n", - pending, env, v, current, LatestVersion()) - return nil - } - } - return &MigrateConsentError{ - CurrentVersion: current, - LatestVersion: LatestVersion(), - Pending: pending, - UnrecognizedEnv: unrecognized, - } -} diff --git a/internal/storage/schema/migrate_consent_test.go b/internal/storage/schema/migrate_consent_test.go deleted file mode 100644 index 40e75b08d..000000000 --- a/internal/storage/schema/migrate_consent_test.go +++ /dev/null @@ -1,164 +0,0 @@ -package schema - -import ( - "errors" - "os" - "strings" - "testing" -) - -// TestMain grants migration consent for the whole package: the MigrateUp -// machinery tests exercise what runs BELOW the consent gate, exactly as -// production does once an operator has consented. The consent tests below -// clear the grant themselves via resetConsentState. -func TestMain(m *testing.M) { - SetLocalMigrateConsent(true) - os.Exit(m.Run()) -} - -// resetConsentState clears every consent source for one test case (undoing -// TestMain's package-wide grant) and restores it afterwards, so consent tests -// see a clean slate and machinery tests keep their grant. -func resetConsentState(t *testing.T) { - t.Helper() - SetLocalMigrateConsent(false) - SetForceAllowRemoteMigrate(false) - t.Cleanup(func() { - SetLocalMigrateConsent(true) - SetForceAllowRemoteMigrate(false) - }) - t.Setenv(AllowMigrateEnv, "") - t.Setenv(AllowRemoteMigrateEnv, "") -} - -func TestMigrateConsentDecision_FreshDB_Allows(t *testing.T) { - resetConsentState(t) - if err := migrateConsentDecision(0, 12); err != nil { - t.Fatalf("decision = %v, want nil for fresh database (current=0)", err) - } -} - -func TestMigrateConsentDecision_NothingPending_Allows(t *testing.T) { - resetConsentState(t) - if err := migrateConsentDecision(LatestVersion(), 0); err != nil { - t.Fatalf("decision = %v, want nil when nothing is pending", err) - } -} - -func TestMigrateConsentDecision_PendingNoConsent_Refuses(t *testing.T) { - resetConsentState(t) - err := migrateConsentDecision(53, 12) - var consentErr *MigrateConsentError - if !errors.As(err, &consentErr) { - t.Fatalf("decision = %v, want *MigrateConsentError", err) - } - if consentErr.CurrentVersion != 53 || consentErr.Pending != 12 { - t.Fatalf("error fields = v%d/%d pending, want v53/12", consentErr.CurrentVersion, consentErr.Pending) - } - if !IsMigrateConsentError(err) { - t.Fatalf("IsMigrateConsentError(err) = false, want true") - } - msg := consentErr.UserMessage() - for _, want := range []string{"bd migrate schema", AllowMigrateEnv} { - if !strings.Contains(msg, want) { - t.Fatalf("UserMessage missing %q:\n%s", want, msg) - } - } -} - -func TestMigrateConsentDecision_LocalConsent_Allows(t *testing.T) { - resetConsentState(t) - SetLocalMigrateConsent(true) - if err := migrateConsentDecision(53, 12); err != nil { - t.Fatalf("decision = %v, want nil with local consent set", err) - } -} - -func TestMigrateConsentDecision_ForceOverride_Allows(t *testing.T) { - resetConsentState(t) - SetForceAllowRemoteMigrate(true) - if err := migrateConsentDecision(53, 12); err != nil { - t.Fatalf("decision = %v, want nil with the migrate --force override set", err) - } -} - -func TestMigrateConsentDecision_EnvConsent_Allows(t *testing.T) { - for _, env := range []string{AllowMigrateEnv, AllowRemoteMigrateEnv} { - for _, v := range []string{"1", "true", "TRUE"} { - t.Run(env+"="+v, func(t *testing.T) { - resetConsentState(t) - t.Setenv(env, v) - if err := migrateConsentDecision(53, 12); err != nil { - t.Fatalf("decision = %v, want nil with %s=%s", err, env, v) - } - }) - } - } -} - -func TestMigrateConsentDecision_EnvFalse_Refuses(t *testing.T) { - resetConsentState(t) - t.Setenv(AllowMigrateEnv, "0") - if !IsMigrateConsentError(migrateConsentDecision(53, 12)) { - t.Fatalf("want refusal with %s=0", AllowMigrateEnv) - } -} - -func TestMigrateConsentDecision_UnparseableEnv_RefusesWithHint(t *testing.T) { - resetConsentState(t) - t.Setenv(AllowMigrateEnv, "yes-please") - err := migrateConsentDecision(53, 12) - var consentErr *MigrateConsentError - if !errors.As(err, &consentErr) { - t.Fatalf("decision = %v, want *MigrateConsentError", err) - } - if consentErr.UnrecognizedEnv != "yes-please" { - t.Fatalf("UnrecognizedEnv = %q, want the unparseable value surfaced", consentErr.UnrecognizedEnv) - } - if !strings.Contains(consentErr.UserMessage(), "yes-please") { - t.Fatalf("UserMessage does not surface the unparseable env value:\n%s", consentErr.UserMessage()) - } -} - -// TestMigrateConsentError_AgentSurfacesNameNoRemedy pins the #4259 directive -// convention cmd/bd's JSON refusal is built on: Refusal and AgentDirective are -// the top-level fields an agent reads, so neither may carry a runnable -// migration or a consent env var; the migrate command lives only in the -// operator-gated "migrate" option. -func TestMigrateConsentError_AgentSurfacesNameNoRemedy(t *testing.T) { - e := &MigrateConsentError{CurrentVersion: 65, LatestVersion: 69, Pending: 4} - for name, s := range map[string]string{"Refusal": e.Refusal(), "AgentDirective": e.AgentDirective()} { - for _, unwanted := range []string{SharedConsentCommand, "BD_ALLOW"} { - if strings.Contains(s, unwanted) { - t.Errorf("%s carries %q:\n%s", name, unwanted, s) - } - } - } - opts := e.Options() - if len(opts) != 2 || opts[0].ID != "migrate" || opts[1].ID != "keep" { - t.Fatalf("Options = %+v, want exactly migrate then keep", opts) - } - if len(opts[0].Commands) != 1 || opts[0].Commands[0] != SharedConsentCommand { - t.Errorf("migrate option commands = %q, want [%q]", opts[0].Commands, SharedConsentCommand) - } - if len(opts[1].Commands) != 0 { - t.Errorf("keep option commands = %q, want none", opts[1].Commands) - } -} - -// TestMigrateConsentError_WorkingSetWarning pins the warning a -// working-set-reconcile open prints when it continues past the refusal: it -// states the refusal and that the commit runs on the current schema, and it -// never offers the consent env var as the way forward. -func TestMigrateConsentError_WorkingSetWarning(t *testing.T) { - e := &MigrateConsentError{CurrentVersion: 65, LatestVersion: 69, Pending: 4} - w := e.WorkingSetWarning() - for _, want := range []string{e.Refusal(), "Working-set reconcile command: continuing on schema v65 without"} { - if !strings.Contains(w, want) { - t.Errorf("WorkingSetWarning missing %q:\n%s", want, w) - } - } - if strings.Contains(w, "BD_ALLOW") { - t.Errorf("WorkingSetWarning offers a consent env var:\n%s", w) - } -} diff --git a/internal/storage/schema/migration_0067_versioned_beads_test.go b/internal/storage/schema/migration_0067_versioned_beads_test.go deleted file mode 100644 index 8ffb1d89d..000000000 --- a/internal/storage/schema/migration_0067_versioned_beads_test.go +++ /dev/null @@ -1,244 +0,0 @@ -package schema - -import ( - "fmt" - "os" - "strings" - "testing" - - "github.com/steveyegge/beads/internal/testutil" -) - -// Phase 1 of the versioned-beads epic (be-hs42e / gastownhall/beads#6132, -// this slice: be-hs42e.2 / #6134) adds purely additive schema: a per-issue -// version log, a store-global epoch singleton, and a fast-path revision -// column on issues. Nothing reads or writes these surfaces yet — that is -// Phase 2 (#6135) and Phase 3 (#6136). These tests only assert the DDL -// itself: shape, emptiness, and that old, unmodified callers of `issues` -// are unaffected. - -const migration0067Up = "0067_add_versioned_beads_schema.up.sql" -const migration0067Down = "0067_add_versioned_beads_schema.down.sql" - -// TestMigration0067AddsVersionedBeadsSchema is a pure-Go, DB-independent -// check of the frozen migration bytes themselves — it runs even where no -// `dolt` binary is available. -// -// It pins the shape both ADD COLUMNs must keep: an INFORMATION_SCHEMA-guarded -// PREPARE, on both planes. That guard is not decoration — it is what makes a -// raw replay of this file onto an already-migrated store a no-op, which -// internal/storage/dolt's pr4107 replay harness requires of every migration -// >= 0046 (it re-executes the .up.sql bytes directly, so no Go-side guard is -// in the path). Dolt accepts no unprepared conditional DDL that would do the -// same job: ADD COLUMN IF NOT EXISTS, DROP COLUMN IF EXISTS and a top-level -// IF are all parse errors, and a stored procedure's body needs a client-side -// DELIMITER the CLI batch path requires and the Go driver rejects. -// -// The cost is the pre-2.3 CLI hazard a PREPARE'd ADD COLUMN carries (it -// silently vanishes on the CLI-bundle path — see cli_prepared_ddl.go), and -// the fix for that is the same one 0060/0065/0066 use: a direct-DDL override -// in cliCompatibleMigrationSQL, which -// TestBundleMigrationsWithPreparedALTERAreOverriddenOrJustified and -// TestAllMigrationsSQLUsesDirectDDLForKnownCLIIncompatibilities both police. -// The assertion below is the local anchor for that pairing: if someone ever -// unwraps these PREPAREs, the override silently becomes a lie. -func TestMigration0067AddsVersionedBeadsSchema(t *testing.T) { - upSQL, err := MigrationSQL(migration0067Up) - if err != nil { - t.Fatalf("MigrationSQL(%s) error = %v, want the migration file to exist", migration0067Up, err) - } - for _, want := range []string{ - "CREATE TABLE IF NOT EXISTS issue_versions", - "CREATE TABLE IF NOT EXISTS store_epoch", - "PRIMARY KEY (issue_id, revision)", - "ALTER TABLE issues ADD COLUMN current_revision BIGINT NOT NULL DEFAULT 1", - // Shape parity with issues, identical type. Nothing reads or writes - // this column in any phase — see the migration header's asymmetry - // note and ignored/0027. - "ALTER TABLE wisps ADD COLUMN current_revision BIGINT NOT NULL DEFAULT 1", - // Both ALTERs are INFORMATION_SCHEMA-guarded so a raw replay no-ops. - "COLUMN_NAME = 'current_revision'", - "@issues_cr_needs_add", - "@wisps_cr_needs_add", - // The wisps arm carries a second sub-guard the issues arm does not: - // the table itself must exist, so a drifted store that never - // materialized the clone-local wisp tables no-ops instead of - // aborting the whole batch. No test executes that arm against an - // absent wisps table — the full-chain test seeds bounded at v46 and - // 0047's repair recreates wisps before 0067's frozen text runs — so - // this string pin is the only thing standing between dropping the - // sub-guard and a still-green suite. It has to be the TABLES probe: - // the file's other TABLE_NAME = 'wisps' occurrence is the COLUMNS - // probe below, which survives dropping the table-exists arm. - "FROM INFORMATION_SCHEMA.TABLES", - "WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'wisps') > 0", - } { - if !strings.Contains(upSQL, want) { - t.Errorf("0067 up migration missing %q\nfull SQL:\n%s", want, upSQL) - } - } - if !strings.Contains(strings.ToUpper(upSQL), "PREPARE STMT FROM @SQL") { - t.Error("0067 up migration must keep its guarded PREPARE blocks — they are what make a raw .up.sql replay onto an already-migrated store a no-op (pr4107_corruption_test.go's harness bypasses every Go-side guard), and Dolt accepts no unprepared conditional ADD COLUMN. Unwrapping them also invalidates cliMigration0067AddVersionedBeadsSchema.") - } - // The bundle override is what keeps the PREPARE above off the pre-2.3 - // CLI path. Assert it directly rather than trusting the two schema_test - // assertions to stay pointed at this migration. - for _, want := range []string{ - "ALTER TABLE issues ADD COLUMN current_revision BIGINT NOT NULL DEFAULT 1;", - "ALTER TABLE wisps ADD COLUMN current_revision BIGINT NOT NULL DEFAULT 1;", - } { - if !strings.Contains(cliCompatibleMigrationSQL(migration0067Up, upSQL), want) { - t.Errorf("0067's CLI bundle substitute missing direct DDL %q", want) - } - } - if !cliSubstituteAssumesWispTables(migration0067Up) { - t.Error("0067's CLI substitute ALTERs wisps unguarded, so it must be listed in cliSubstituteAssumesWispTables — a replay over a clone that never synced the wisp tables has to fall back to the frozen source text") - } - if strings.Contains(upSQL, "INSERT INTO store_epoch") || strings.Contains(upSQL, "INSERT IGNORE INTO store_epoch") { - t.Error("0067 up migration must not seed store_epoch — every new table starts empty in Phase 1 (no reads/writes of the new surfaces until Phase 2)") - } - - // down.sql files are not part of the embedded FS (only migrations/*.up.sql - // is //go:embed'd — see mainSource.files), so unlike the up side above, - // this reads straight from disk by package-relative path, matching - // TestMigration0035HandlesLegacyWispDependenciesShape's precedent. - downBytes, err := os.ReadFile("migrations/" + migration0067Down) - if err != nil { - t.Fatalf("read %s: %v, want the migration file to exist", migration0067Down, err) - } - downSQL := string(downBytes) - for _, want := range []string{ - "DROP TABLE", - "issue_versions", - "store_epoch", - "ALTER TABLE issues DROP COLUMN current_revision", - // The down side reverses both planes, or a rolled-back workspace - // keeps a wisps column its binary no longer knows about. - "ALTER TABLE wisps DROP COLUMN current_revision", - } { - if !strings.Contains(downSQL, want) { - t.Errorf("0067 down migration missing %q\nfull SQL:\n%s", want, downSQL) - } - } - // Only migrations/*.up.sql is embedded into the CLI fresh bundle - // (mainSource.files), so the pre-2.3 prepared-DDL hazard never reaches a - // down migration through the bundle and the guard is free there — 0060's - // down is the precedent. A manual `dolt sql -f` rollback on a pre-2.3 CLI - // does hit it (the DROP COLUMNs silently no-op); the down file's own - // header steers operators to a server connection or dolt >= 2.3. - if !strings.Contains(strings.ToUpper(downSQL), "PREPARE STMT FROM @SQL") { - t.Error("0067 down migration must guard its DROP COLUMNs the way the up migration guards its ADD COLUMNs, so an issues-only or partially-applied workspace rolls back as safely as it migrated up") - } -} - -// TestMigration0067AddsVersionedBeadsSchemaThroughDoltCLI applies the full -// migration bundle through a real `dolt` binary (skipped without one — see -// testutil.RequireDoltBinary) and checks the shape acceptance criteria a -// pure-Go SQL-text check cannot: actual column types/nullability as Dolt -// reports them, that both new tables start empty, that the composite and -// singleton primary keys are enforced, and compat direction 1 — an -// old-shaped INSERT/SELECT against `issues` that has never heard of -// current_revision still behaves unchanged, because the column is -// NOT NULL DEFAULT 1 rather than a value old callers must supply. -func TestMigration0067AddsVersionedBeadsSchemaThroughDoltCLI(t *testing.T) { - testutil.RequireDoltBinary(t) - - dir := t.TempDir() - runDoltCommand(t, dir, "init", "--name", "test", "--email", "test@example.com") - runDoltSQL(t, dir, AllMigrationsSQL()) - - // issue_versions shape. - requireDoltColumnShape(t, dir, "issue_versions", "issue_id", "varchar(255)", "NO") - requireDoltDataType(t, dir, "issue_versions", "revision", "bigint", "NO") - requireDoltDataType(t, dir, "issue_versions", "epoch", "int", "NO") - // 0067 creates durable_state as JSON, and 0068 step 7 retypes it to - // LONGBLOB (Dolt's JSON type renormalizes numbers, so it cannot hold the - // verbatim bytes a content token hashes -- see 0068's step 7 header). This - // test runs the whole bundle, so the shape it sees is the LONGBLOB one. - requireDoltDataType(t, dir, "issue_versions", "durable_state", "longblob", "YES") - requireDoltColumnShape(t, dir, "issue_versions", "change_actor", "varchar(255)", "YES") - requireDoltColumnShape(t, dir, "issue_versions", "change_agent", "varchar(255)", "YES") - requireDoltColumnShape(t, dir, "issue_versions", "change_message", "text", "YES") - // 0069 widens both change_at and removed_at from DATETIME(0) to - // DATETIME(6) (be-hs42e.8) to stop the column rounding sub-second writes - // up to the next second. This test runs the whole bundle (see the - // durable_state/attribution_status comments above for the same pattern), - // so it asserts the post-widen shape, not the DATETIME(0) shape 0067 - // alone would have produced. - requireDoltColumnShape(t, dir, "issue_versions", "change_at", "datetime(6)", "NO") - requireDoltColumnShape(t, dir, "issue_versions", "removed_at", "datetime(6)", "YES") - requireDoltColumnShape(t, dir, "issue_versions", "removed_reason", "varchar(255)", "YES") - requireDoltNoRows(t, dir, "SELECT issue_id FROM issue_versions", "issue_versions") - - // store_epoch shape — singleton table, starts empty (Phase 1 does not - // seed it; a later phase's lazy-init owns creating the id=1 row). - requireDoltColumnShape(t, dir, "store_epoch", "id", "tinyint(1)", "NO") - requireDoltDataType(t, dir, "store_epoch", "epoch", "int", "NO") - requireDoltColumnShape(t, dir, "store_epoch", "bumped_at", "datetime", "YES") - requireDoltColumnShape(t, dir, "store_epoch", "bumped_reason", "varchar(255)", "YES") - requireDoltNoRows(t, dir, "SELECT id FROM store_epoch", "store_epoch") - - // current_revision on BOTH planes. issues.current_revision is the - // fast-path CAS column later phases read and write; wisps.current_revision - // is shape parity only (nothing ever reads or writes it — see the - // migration header and ignored/0027), and it is asserted here for exactly - // the reason the parity oracle exists: the two planes share - // column-list-shaped code paths, so the shape obligation is real even - // where the semantics are not. Same type on both, or - // TestSchemaParityIssuesVsWisps' type check fails instead of its - // name check. - requireDoltDataType(t, dir, "issues", "current_revision", "bigint", "NO") - requireDoltDataType(t, dir, "wisps", "current_revision", "bigint", "NO") - - // Composite PK (issue_id, revision) is enforced: same pair twice fails. - // attribution_status is 0068's NOT NULL column (not yet Phase 1's - // concern, but present on any schema this test's fresh bundle produces), - // so both inserts must supply it or Dolt rejects them before the PK - // check below ever runs. - runDoltSQL(t, dir, `INSERT INTO issue_versions (issue_id, revision, epoch, change_at, attribution_status) VALUES ('iv-1', 1, 1, '2026-09-01 00:00:00', 'unknown')`) - if err := runDoltSQLExpectingError(t, dir, `INSERT INTO issue_versions (issue_id, revision, epoch, change_at, attribution_status) VALUES ('iv-1', 1, 1, '2026-09-01 00:00:01', 'unknown')`); err == nil { - t.Error("duplicate (issue_id, revision) insert into issue_versions succeeded, want primary key violation") - } - - // Singleton PK (id) is enforced the same way. - runDoltSQL(t, dir, `INSERT INTO store_epoch (id) VALUES (1)`) - if err := runDoltSQLExpectingError(t, dir, `INSERT INTO store_epoch (id) VALUES (1)`); err == nil { - t.Error("duplicate id=1 insert into store_epoch succeeded, want primary key violation") - } - if err := runDoltSQLExpectingError(t, dir, `INSERT INTO store_epoch (id) VALUES (2)`); err == nil { - t.Error("id=2 insert into store_epoch succeeded, want the singleton CHECK constraint to reject any id other than 1") - } - - // Compat direction 1: an old (pre-Phase-1) binary only ever issues - // explicit-column statements against `issues` that don't mention - // current_revision. Those must still work unchanged post-migration. - runDoltSQL(t, dir, `INSERT INTO issues (id, title, description, design, acceptance_criteria, notes) VALUES ('old-style-1', 'Old-style insert', 'd', 'des', 'ac', 'n')`) - rows := queryDoltCSV(t, dir, `SELECT title FROM issues WHERE id = 'old-style-1'`) - if len(rows) != 1 || rows[0]["title"] != "Old-style insert" { - t.Fatalf("old-shaped explicit-column insert/select round-trip failed post-migration: %v", rows) - } -} - -// requireDoltDataType is requireDoltColumnShape's data_type-based sibling: -// it asserts the coarse, display-width-independent information_schema -// column (always "bigint"/"int"/"json", never e.g. "bigint(20)"), for -// column types this package has no existing column_type-string precedent -// to match against. -func requireDoltDataType(t *testing.T, dir, tableName, columnName, wantDataType, wantNullable string) { - t.Helper() - rows := queryDoltCSV(t, dir, fmt.Sprintf(` -SELECT data_type AS data_type, is_nullable AS is_nullable -FROM information_schema.columns -WHERE table_schema = DATABASE() - AND table_name = %s - AND column_name = %s`, doltSQLString(tableName), doltSQLString(columnName))) - if len(rows) != 1 { - t.Fatalf("%s.%s column query returned %d rows, want 1: %v", tableName, columnName, len(rows), rows) - } - if got := rows[0]["data_type"]; got != wantDataType { - t.Fatalf("%s.%s DATA_TYPE = %s, want %s", tableName, columnName, got, wantDataType) - } - if got := rows[0]["is_nullable"]; got != wantNullable { - t.Fatalf("%s.%s IS_NULLABLE = %s, want %s", tableName, columnName, got, wantNullable) - } -} diff --git a/internal/storage/schema/migration_0068_add_attribution_status_test.go b/internal/storage/schema/migration_0068_add_attribution_status_test.go deleted file mode 100644 index 5e26243ae..000000000 --- a/internal/storage/schema/migration_0068_add_attribution_status_test.go +++ /dev/null @@ -1,156 +0,0 @@ -package schema - -import ( - "os" - "strings" - "testing" - - "github.com/steveyegge/beads/internal/testutil" -) - -// Phase 2 of the versioned-beads epic (be-hs42e / gastownhall/beads#6132, -// this slice: be-hs42e.3 / #6135) adds one column: issue_versions gains a -// NOT NULL attribution_status (design §16.4, R14), written by -// RecordVersionInTx / attributionStatusForActor -// (internal/storage/issueops/version_history.go) from the same build that -// ships this migration, so the NOT NULL constraint never rejects a -// pre-existing row. Steps 1-5 of design §16.3 (the version_id PK swap and -// participation_generation) are a different bead's scope and are not in this -// migration file. -// -// Step 7 (added at review, donnabox on #6358 item 4) retypes -// issue_versions.durable_state from JSON to LONGBLOB: Dolt's JSON type -// renormalizes numbers, so it cannot keep the verbatim bytes (R5.1) that a -// content-derived token hashes. The writer now stores the RFC 8785 (JCS) -// canonical bytes; the table is empty in this era, so no data converts. - -const migration0068Up = "0068_add_attribution_status.up.sql" -const migration0068Down = "0068_add_attribution_status.down.sql" - -// TestMigration0068AddsAttributionStatus is a pure-Go, DB-independent check -// of the frozen migration bytes themselves — it runs even where no `dolt` -// binary is available. -// -// It pins the same guarded-PREPARE shape 0067 uses and for the same reason: -// the guard is what makes a raw replay of this file onto an already-migrated -// store a no-op (internal/storage/dolt's pr4107 replay harness requires this -// of every migration >= 0046), and Dolt accepts no unprepared conditional -// ADD COLUMN. The cost is the same pre-2.3 CLI hazard 0060/0065/0066/0067 -// carry, so this migration needs the same direct-DDL override in -// cliCompatibleMigrationSQL, policed by the same two guard tests -// (TestBundleMigrationsWithPreparedALTERAreOverriddenOrJustified and -// TestAllMigrationsSQLUsesDirectDDLForKnownCLIIncompatibilities). -func TestMigration0068AddsAttributionStatus(t *testing.T) { - upSQL, err := MigrationSQL(migration0068Up) - if err != nil { - t.Fatalf("MigrationSQL(%s) error = %v, want the migration file to exist", migration0068Up, err) - } - for _, want := range []string{ - "ALTER TABLE issue_versions ADD COLUMN attribution_status VARCHAR(20) NOT NULL", - "COLUMN_NAME = 'attribution_status'", - "@issue_versions_as_needs_add", - // Step 7: the durable_state retype, guarded on DATA_TYPE (0057's - // shape) so a replay onto a store already at LONGBLOB no-ops. - "ALTER TABLE issue_versions MODIFY COLUMN durable_state LONGBLOB", - "COLUMN_NAME = 'durable_state'", - "@issue_versions_ds_needs_retype", - "DATA_TYPE <> 'longblob'", - } { - if !strings.Contains(upSQL, want) { - t.Errorf("0068 up migration missing %q\nfull SQL:\n%s", want, upSQL) - } - } - if !strings.Contains(strings.ToUpper(upSQL), "PREPARE STMT FROM @SQL") { - t.Error("0068 up migration must keep its guarded PREPARE block — it is what makes a raw .up.sql replay onto an already-migrated store a no-op, and Dolt accepts no unprepared conditional ADD COLUMN. Unwrapping it also invalidates cliMigration0068AddAttributionStatus.") - } - // The bundle override is what keeps the PREPARE above off the pre-2.3 - // CLI path. Assert it directly rather than trusting the two schema_test - // assertions to stay pointed at this migration. - for _, want := range []string{ - "ALTER TABLE issue_versions ADD COLUMN attribution_status VARCHAR(20) NOT NULL;", - // Step 7's retype has to reach the bundle as direct DDL too, or a - // fresh CLI-built database keeps 0067's JSON column while the - // runtime has LONGBLOB. - "ALTER TABLE issue_versions MODIFY COLUMN durable_state LONGBLOB;", - } { - if !strings.Contains(cliCompatibleMigrationSQL(migration0068Up, upSQL), want) { - t.Errorf("0068's CLI bundle substitute missing direct DDL %q", want) - } - } - if cliSubstituteAssumesWispTables(migration0068Up) { - t.Error("0068's CLI substitute touches only issue_versions, which has no wisps-side counterpart table — it must not be listed in cliSubstituteAssumesWispTables") - } - - // down.sql files are not part of the embedded FS (only migrations/*.up.sql - // is //go:embed'd — see mainSource.files), so unlike the up side above, - // this reads straight from disk by package-relative path, matching - // TestMigration0067AddsVersionedBeadsSchema's precedent. - downBytes, err := os.ReadFile("migrations/" + migration0068Down) - if err != nil { - t.Fatalf("read %s: %v, want the migration file to exist", migration0068Down, err) - } - downSQL := string(downBytes) - for _, want := range []string{ - "ALTER TABLE issue_versions DROP COLUMN attribution_status", - "COLUMN_NAME = 'attribution_status'", - // Step 7's reverse: back to the JSON type 0067 created, guarded on - // DATA_TYPE so an already-rolled-back store no-ops. - "ALTER TABLE issue_versions MODIFY COLUMN durable_state JSON", - "COLUMN_NAME = 'durable_state'", - "@issue_versions_ds_is_longblob", - } { - if !strings.Contains(downSQL, want) { - t.Errorf("0068 down migration missing %q\nfull SQL:\n%s", want, downSQL) - } - } - // Only migrations/*.up.sql is embedded into the CLI fresh bundle - // (mainSource.files), so the pre-2.3 prepared-DDL hazard never reaches a - // down migration and the guard is free — 0060's/0067's downs are the - // precedent. - if !strings.Contains(strings.ToUpper(downSQL), "PREPARE STMT FROM @SQL") { - t.Error("0068 down migration must guard its DROP COLUMN the way the up migration guards its ADD COLUMN, so a partially-applied or already-rolled-back workspace rolls back safely") - } -} - -// TestMigration0068AddsAttributionStatusThroughDoltCLI applies the full -// migration bundle through a real `dolt` binary (skipped without one — see -// testutil.RequireDoltBinary) and checks the shape acceptance criteria a -// pure-Go SQL-text check cannot: actual column type/nullability as Dolt -// reports it, and that the NOT NULL constraint is real (an insert that omits -// attribution_status must fail, matching the fixture fix this migration -// forced onto TestMigration0067AddsVersionedBeadsSchemaThroughDoltCLI). -func TestMigration0068AddsAttributionStatusThroughDoltCLI(t *testing.T) { - testutil.RequireDoltBinary(t) - - dir := t.TempDir() - runDoltCommand(t, dir, "init", "--name", "test", "--email", "test@example.com") - runDoltSQL(t, dir, AllMigrationsSQL()) - - requireDoltColumnShape(t, dir, "issue_versions", "attribution_status", "varchar(20)", "NO") - // Step 7: 0067 created durable_state as JSON; after 0068 it is the - // byte-preserving LONGBLOB the writer's JCS canonical form needs. - requireDoltDataType(t, dir, "issue_versions", "durable_state", "longblob", "YES") - requireDoltNoRows(t, dir, "SELECT issue_id FROM issue_versions", "issue_versions") - - if err := runDoltSQLExpectingError(t, dir, `INSERT INTO issue_versions (issue_id, revision, epoch, change_at) VALUES ('iv-1', 1, 1, '2026-09-01 00:00:00')`); err == nil { - t.Error("insert into issue_versions omitting attribution_status succeeded, want a NOT NULL violation") - } - runDoltSQL(t, dir, `INSERT INTO issue_versions (issue_id, revision, epoch, change_at, attribution_status) VALUES ('iv-1', 1, 1, '2026-09-01 00:00:00', 'claimed')`) - rows := queryDoltCSV(t, dir, `SELECT attribution_status FROM issue_versions WHERE issue_id = 'iv-1'`) - if len(rows) != 1 || rows[0]["attribution_status"] != "claimed" { - t.Fatalf("attribution_status round-trip failed post-migration: %v", rows) - } - - // Step 7's actual property, measured rather than inferred from the - // type name: bytes written to durable_state come back byte for byte. - // These three number forms are the ones Dolt's JSON type renormalizes - // (on 2.2.3 the same INSERT into a JSON column reads back as - // {"a":1,"big":9007199254740992,"e":1e+300}), so a regression to JSON - // fails here even if the DATA_TYPE assertion above were loosened. - const verbatim = `{"a":1.0,"big":9007199254740993,"e":1e300}` - runDoltSQL(t, dir, `INSERT INTO issue_versions (issue_id, revision, epoch, change_at, attribution_status, durable_state) VALUES ('iv-2', 1, 1, '2026-09-01 00:00:00', 'claimed', '`+verbatim+`')`) - rows = queryDoltCSV(t, dir, `SELECT durable_state FROM issue_versions WHERE issue_id = 'iv-2'`) - if len(rows) != 1 || rows[0]["durable_state"] != verbatim { - t.Fatalf("durable_state round-trip changed the bytes post-migration: got %v, want %q", rows, verbatim) - } -} diff --git a/internal/storage/schema/migration_0069_datetime_precision_test.go b/internal/storage/schema/migration_0069_datetime_precision_test.go deleted file mode 100644 index 503be1b6d..000000000 --- a/internal/storage/schema/migration_0069_datetime_precision_test.go +++ /dev/null @@ -1,171 +0,0 @@ -package schema - -import ( - "os" - "strings" - "testing" - - "github.com/steveyegge/beads/internal/testutil" -) - -// Migration 0069 (be-hs42e.8 / gastownhall/beads#6132, the DATETIME(6) -// widening slice): 0067 creates issue_versions.change_at and .removed_at as -// plain DATETIME -- precision 0, whole seconds only. Dolt's datetime(0) does -// not truncate sub-second input; it ROUNDS half-up (pinned for a different -// column by testAuditImportCommentSubSecond in -// backend/conformance/audit_labels-comments-events.go, and reconfirmed -// directly against issue_versions below). For a column whose entire job is -// placing history in order, precision 0 has two consequences: a sub-second -// write can read back rounded into the *next* second, and two writes less -// than a second apart in real time can round onto the identical stored -// value and become indistinguishable by change_at. The fix is a widen to -// DATETIME(6) (microsecond). It was first drafted as a step 8 appended to -// the then-unmerged 0068; 0068 shipped on main without it -// (gastownhall/beads#6650), which froze that file, so the widen has its own -// slot here. -// -// removed_at widens in lockstep even though no Go code writes it yet (a -// repo-wide grep for removed_at/RemovedAt turns up only schema: 0067's -// CREATE TABLE and this package's CLI-bundle mirror of it). It is -// change_at's paired lifecycle column on the same row of the same table, so -// leaving it at precision 0 while change_at moves to precision 6 would be -// exactly the asymmetry hazard this bead's own title warns against ("before -// any store accumulates real history") -- a second migration once -// removed_at gets a writer, instead of one now while the table is still -// empty and the ALTER is free. -const migration0069Up = "0069_widen_issue_versions_datetime_precision.up.sql" -const migration0069Down = "0069_widen_issue_versions_datetime_precision.down.sql" - -const migration0069ChangeAtDatetimeGuard = "@issue_versions_change_at_needs_widen" -const migration0069RemovedAtDatetimeGuard = "@issue_versions_removed_at_needs_widen" - -// TestLatestVersionIncludesMigration0069 (pinning LatestVersion() == 69) is -// superseded by TestLatestVersionIncludesMigration0070 -// (migration_0070_add_participation_generation_test.go) now that 0070 claims the -// next free slot; only one such pin lives at a time, matching how this -// test itself already superseded 0068's own version. - -// TestMigration0069WidensChangeAtAndRemovedAtPrecision is the pure-Go, -// DB-independent half of the pin, mirroring -// TestMigration0068AddsAttributionStatus's shape: it checks the frozen -// migration bytes and the CLI-bundle override text directly, no `dolt` -// binary required. -func TestMigration0069WidensChangeAtAndRemovedAtPrecision(t *testing.T) { - upSQL, err := MigrationSQL(migration0069Up) - if err != nil { - t.Fatalf("MigrationSQL(%s) error = %v, want the migration file to exist", migration0069Up, err) - } - for _, want := range []string{ - "ALTER TABLE issue_versions MODIFY COLUMN change_at DATETIME(6) NOT NULL", - "ALTER TABLE issue_versions MODIFY COLUMN removed_at DATETIME(6)", - migration0069ChangeAtDatetimeGuard, - migration0069RemovedAtDatetimeGuard, - "DATETIME_PRECISION", - } { - if !strings.Contains(upSQL, want) { - t.Errorf("0069 up migration missing %q (change_at/removed_at DATETIME(6) widen)\nfull SQL:\n%s", want, upSQL) - } - } - if !strings.Contains(strings.ToUpper(upSQL), "PREPARE STMT FROM @SQL") { - t.Error("0069 up migration must keep its guarded PREPARE blocks -- they are what make a raw .up.sql replay onto an already-widened store a no-op, and Dolt accepts no unprepared conditional MODIFY COLUMN") - } - - bundle := cliCompatibleMigrationSQL(migration0069Up, upSQL) - for _, want := range []string{ - "ALTER TABLE issue_versions MODIFY COLUMN change_at DATETIME(6) NOT NULL;", - "ALTER TABLE issue_versions MODIFY COLUMN removed_at DATETIME(6);", - } { - if !strings.Contains(bundle, want) { - t.Errorf("0069's CLI bundle substitute (cliMigration0069WidenIssueVersionsDatetimePrecision) missing direct DDL %q -- without it a fresh CLI-built database keeps precision-0 change_at/removed_at while the runtime migration path widens them", want) - } - } - if cliSubstituteAssumesWispTables(migration0069Up) { - t.Error("0069's CLI substitute touches only issue_versions, which has no wisps-side counterpart table — it must not be listed in cliSubstituteAssumesWispTables") - } - - // down.sql files are not part of the embedded FS (only - // migrations/*.up.sql is //go:embed'd -- see mainSource.files), so like - // TestMigration0068AddsAttributionStatus's own down check, this reads - // straight from disk by package-relative path. - downBytes, err := os.ReadFile("migrations/" + migration0069Down) - if err != nil { - t.Fatalf("read %s: %v, want the migration file to exist", migration0069Down, err) - } - downSQL := string(downBytes) - for _, want := range []string{ - "ALTER TABLE issue_versions MODIFY COLUMN change_at DATETIME NOT NULL", - "ALTER TABLE issue_versions MODIFY COLUMN removed_at DATETIME", - "COLUMN_NAME = 'change_at'", - "COLUMN_NAME = 'removed_at'", - } { - if !strings.Contains(downSQL, want) { - t.Errorf("0069 down migration missing %q\nfull SQL:\n%s", want, downSQL) - } - } - if !strings.Contains(strings.ToUpper(downSQL), "PREPARE STMT FROM @SQL") { - t.Error("0069 down migration must guard its MODIFYs the way the up migration does, so a store that never took the widen, or was already rolled back, rolls back safely") - } -} - -// TestMigration0069ChangeAtSurvivesSubSecondPrecisionThroughDoltCLI is the -// live half: it demonstrates the bug against a real `dolt` binary (skipped -// without one) and is be-hs42e.8's exit contract's own acceptance criteria, -// verbatim -- "a sub-second write reading back rounded to the next second" -// and "same-second versions colliding". Both fixture values and the exact -// rounding direction were measured directly against dolt 2.3.3 before -// writing this test (SELECT ... datetime_precision, and a scratch -// DATETIME/DATETIME(6) round-trip), not assumed. -func TestMigration0069ChangeAtSurvivesSubSecondPrecisionThroughDoltCLI(t *testing.T) { - testutil.RequireDoltBinary(t) - - dir := t.TempDir() - runDoltCommand(t, dir, "init", "--name", "test", "--email", "test@example.com") - runDoltSQL(t, dir, AllMigrationsSQL()) - - requireDoltColumnShape(t, dir, "issue_versions", "change_at", "datetime(6)", "NO") - requireDoltColumnShape(t, dir, "issue_versions", "removed_at", "datetime(6)", "YES") - - // Acceptance criterion 1: "a sub-second write reading back rounded to - // the next second." .750000 is testAuditImportCommentSubSecond's own - // fixture value, chosen because Dolt's datetime(0) rounding is HALF-UP - // (pinned there for a different column, reconfirmed here directly) -- - // >= .5 always rounds forward a whole second, so at precision 0 this - // value is guaranteed to land on the wrong second, not merely lose - // precision within the right one. - runDoltSQL(t, dir, `INSERT INTO issue_versions - (issue_id, revision, epoch, change_at, attribution_status) - VALUES ('iv-sub', 1, 1, '2026-09-01 00:00:00.750000', 'claimed')`) - rows := queryDoltCSV(t, dir, `SELECT change_at FROM issue_versions WHERE issue_id = 'iv-sub'`) - if len(rows) != 1 { - t.Fatalf("iv-sub round-trip returned %d rows, want 1: %v", len(rows), rows) - } - if got, want := rows[0]["change_at"], "2026-09-01 00:00:00.750000"; got != want { - t.Errorf("change_at sub-second round-trip = %q, want %q (DATETIME(6) must keep microseconds; DATETIME(0) rounds .750000 up to the next second, 2026-09-01 00:00:01)", got, want) - } - - // Acceptance criterion 2: "same-second versions colliding." Two - // revisions of the same issue, 300ms apart in wall-clock terms, both - // inside the [.000, .500) half of the same second -- at precision 0 - // both round DOWN to the identical whole second and become - // indistinguishable by change_at, exactly the ordering defect an - // audit-history column exists to prevent. - runDoltSQL(t, dir, `INSERT INTO issue_versions - (issue_id, revision, epoch, change_at, attribution_status) - VALUES - ('iv-collide', 1, 1, '2026-09-01 00:00:05.100000', 'claimed'), - ('iv-collide', 2, 1, '2026-09-01 00:00:05.400000', 'claimed')`) - rows = queryDoltCSV(t, dir, `SELECT revision, change_at FROM issue_versions WHERE issue_id = 'iv-collide' ORDER BY revision`) - if len(rows) != 2 { - t.Fatalf("iv-collide round-trip returned %d rows, want 2: %v", len(rows), rows) - } - if rows[0]["change_at"] == rows[1]["change_at"] { - t.Errorf("revisions 1 and 2 of iv-collide both read back change_at = %q; two writes 300ms apart must remain distinguishable, not collide onto the same stored second", rows[0]["change_at"]) - } else { - if got, want := rows[0]["change_at"], "2026-09-01 00:00:05.100000"; got != want { - t.Errorf("iv-collide revision 1 change_at = %q, want %q", got, want) - } - if got, want := rows[1]["change_at"], "2026-09-01 00:00:05.400000"; got != want { - t.Errorf("iv-collide revision 2 change_at = %q, want %q", got, want) - } - } -} diff --git a/internal/storage/schema/migration_0070_add_participation_generation_test.go b/internal/storage/schema/migration_0070_add_participation_generation_test.go deleted file mode 100644 index 0993347f2..000000000 --- a/internal/storage/schema/migration_0070_add_participation_generation_test.go +++ /dev/null @@ -1,144 +0,0 @@ -package schema - -import ( - "os" - "strings" - "testing" - - "github.com/steveyegge/beads/internal/testutil" -) - -// Migration 0070 (design §16.3 steps 4-5, be-dt74u amendment, be-h89oq) adds -// a nullable participation_generation to issues, mirrored inertly on wisps. -// NULL means legacy-unmigrated; any non-NULL is a positive declaration -// sourced from store_epoch.epoch. RecordVersionInTx's write fence (design -// §16.2b, in internal/storage/issueops/version_history.go) reads this column -// to decide whether an update-shaped mutation against a legacy record mints -// a version row at all -- a create-shaped mutation stamps a fresh value -// instead. No backfill: NULL is the correct default for every existing row, -// on both planes. -// -// It was first drafted as steps 4-5 appended to the then-unmerged 0068; -// be-v33pa's own attribution_status-only version of 0068 shipped to main -// first (2026-09-28 15:46Z), which froze that file, so these steps have -// their own slot here (mayor ruling 2026-09-29 on tracker be-waare) -- the -// same reason 0069 got its own slot rather than landing as 0068's step 8. -// -// wisps.participation_generation is guarded on the wisps table existing as -// well as the column, mirroring 0067's wisps.current_revision guard exactly -// (see that migration's header): wisps is dolt-ignored/clone-local, so a -// clone that never synced the local wisp tables must no-op rather than -// abort. The clone-local twin for a workspace that never synced the wisps -// table at all is ignored/0028_add_wisps_participation_generation.up.sql. -const migration0070Up = "0070_add_participation_generation.up.sql" -const migration0070Down = "0070_add_participation_generation.down.sql" - -// TestLatestVersionIncludesMigration0070 (pinning LatestVersion() == 70) is -// superseded by TestLatestVersionIncludesMigration0071 -// (migration_0071_add_removed_restriction_test.go) now that 0071 claims the -// next free slot; only one such pin lives at a time, matching how this -// test itself already superseded 0069's own version. - -// TestMigration0070AddsParticipationGeneration is the pure-Go, DB-independent -// half of the pin, mirroring TestMigration0069WidensChangeAtAndRemovedAtPrecision's -// shape: it checks the frozen migration bytes and the CLI-bundle override -// text directly, no `dolt` binary required. -func TestMigration0070AddsParticipationGeneration(t *testing.T) { - upSQL, err := MigrationSQL(migration0070Up) - if err != nil { - t.Fatalf("MigrationSQL(%s) error = %v, want the migration file to exist", migration0070Up, err) - } - for _, want := range []string{ - "ALTER TABLE issues ADD COLUMN participation_generation BIGINT NULL", - "@issues_pg_needs_add", - "ALTER TABLE wisps ADD COLUMN participation_generation BIGINT NULL", - "@wisps_pg_needs_add", - } { - if !strings.Contains(upSQL, want) { - t.Errorf("0070 up migration missing %q (design §16.3 steps 4-5)\nfull SQL:\n%s", want, upSQL) - } - } - if n := strings.Count(upSQL, "COLUMN_NAME = 'participation_generation'"); n != 2 { - t.Errorf("0070 up migration has %d INFORMATION_SCHEMA probes for participation_generation, want 2 (one for issues, one for wisps)\nfull SQL:\n%s", n, upSQL) - } - if !strings.Contains(strings.ToUpper(upSQL), "PREPARE STMT FROM @SQL") { - t.Error("0070 up migration must keep its guarded PREPARE blocks — they are what make a raw .up.sql replay onto an already-migrated store a no-op, and Dolt accepts no unprepared conditional ADD COLUMN. Unwrapping it also invalidates cliMigration0070AddParticipationGeneration.") - } - - // The bundle override is what keeps the two PREPARE blocks above off the - // pre-2.3 CLI path. - bundle := cliCompatibleMigrationSQL(migration0070Up, upSQL) - for _, want := range []string{ - "ALTER TABLE issues ADD COLUMN participation_generation BIGINT NULL;", - "ALTER TABLE wisps ADD COLUMN participation_generation BIGINT NULL;", - } { - if !strings.Contains(bundle, want) { - t.Errorf("0070's CLI bundle substitute (cliMigration0070AddParticipationGeneration) missing direct DDL %q", want) - } - } - // Design §16.3 step 5 (be-dt74u amendment, be-h89oq) makes 0070's CLI - // substitute ALTER wisps directly, the same unconditional-wisps-ALTER - // shape 0067's current_revision override already uses (see - // cliSubstituteAssumesWispTables' 0067 case). - if !cliSubstituteAssumesWispTables(migration0070Up) { - t.Error("0070's CLI substitute ALTERs wisps unguarded, so it must be listed in cliSubstituteAssumesWispTables — a replay over a clone that never synced the wisp tables has to fall back to the frozen source text") - } - - // down.sql files are not part of the embedded FS (only migrations/*.up.sql - // is //go:embed'd — see mainSource.files), so unlike the up side above, - // this reads straight from disk by package-relative path, matching - // TestMigration0068AddsAttributionStatus's precedent. - downBytes, err := os.ReadFile("migrations/" + migration0070Down) - if err != nil { - t.Fatalf("read %s: %v, want the migration file to exist", migration0070Down, err) - } - downSQL := string(downBytes) - for _, want := range []string{ - "ALTER TABLE issues DROP COLUMN participation_generation", - "ALTER TABLE wisps DROP COLUMN participation_generation", - } { - if !strings.Contains(downSQL, want) { - t.Errorf("0070 down migration missing %q\nfull SQL:\n%s", want, downSQL) - } - } - // Only migrations/*.up.sql is embedded into the CLI fresh bundle - // (mainSource.files), so the pre-2.3 prepared-DDL hazard never reaches a - // down migration and the guard is free — 0068's/0069's downs are the - // precedent. - if !strings.Contains(strings.ToUpper(downSQL), "PREPARE STMT FROM @SQL") { - t.Error("0070 down migration must guard its DROP COLUMNs the way the up migration guards its ADD COLUMNs, so a partially-applied or already-rolled-back workspace rolls back safely") - } -} - -// TestMigration0070AddsParticipationGenerationThroughDoltCLI applies the full -// migration bundle through a real `dolt` binary (skipped without one — see -// testutil.RequireDoltBinary) and checks the shape acceptance criteria a -// pure-Go SQL-text check cannot: actual column type/nullability as Dolt -// reports it, on both planes. -func TestMigration0070AddsParticipationGenerationThroughDoltCLI(t *testing.T) { - testutil.RequireDoltBinary(t) - - dir := t.TempDir() - runDoltCommand(t, dir, "init", "--name", "test", "--email", "test@example.com") - runDoltSQL(t, dir, AllMigrationsSQL()) - - requireDoltDataType(t, dir, "issues", "participation_generation", "bigint", "YES") - requireDoltDataType(t, dir, "wisps", "participation_generation", "bigint", "YES") - - // No backfill: design §16.2a's "NULL = legacy-unmigrated" declaration - // needs every pre-existing (and freshly-inserted, pre-fence) row to read - // back NULL. Only issues is round-tripped here — the column is read and - // written on that plane; wisps carries it for schema-parity shape only - // (§16.2a) and is never read or written by this phase. - runDoltSQL(t, dir, `INSERT INTO issues (id, title, description, design, acceptance_criteria, notes) VALUES ('pg-1', 't', 'd', 'des', 'ac', 'n')`) - // id rides along with the target column: dolt sql -r csv emits no data - // line at all for a single-column result whose only value is NULL (just - // the header survives) -- reproduced identically against pre-existing - // unrelated nullable columns (assignee, closed_at), so it's a CSV-writer - // quirk, not something specific to this migration. A second, - // always-populated column keeps the row real. - rows := queryDoltCSV(t, dir, `SELECT id, participation_generation FROM issues WHERE id = 'pg-1'`) - if len(rows) != 1 || rows[0]["participation_generation"] != "" { - t.Fatalf("participation_generation for a freshly-inserted issues row = %v, want NULL (empty string in CSV form)", rows) - } -} diff --git a/internal/storage/schema/migration_0071_add_removed_restriction_test.go b/internal/storage/schema/migration_0071_add_removed_restriction_test.go deleted file mode 100644 index 5ca6598d5..000000000 --- a/internal/storage/schema/migration_0071_add_removed_restriction_test.go +++ /dev/null @@ -1,163 +0,0 @@ -package schema - -import ( - "os" - "strings" - "testing" - - "github.com/steveyegge/beads/internal/storage/issueops" - "github.com/steveyegge/beads/internal/testutil" -) - -// R7.1 as-of read (gastownhall/beads#5898 revision 9, gastownhall/beads#6136) -// adds one nullable column: issue_versions gains -// removed_restriction VARCHAR(30). issue_versions already carries removed_at -// and removed_reason (migration 0067), virgin and unused until this slice; -// AsOfReadInTx (internal/storage/issueops/asof_read.go) reads all three as -// the durable "this version row was removed" marker. removed_restriction -// carries the categorical restriction removed_at's presence alone cannot: -// gone_retention / gone_erasure / gone_reorganization / unknown, never -// live -- live is the absence of a value, not a stored one. - -const migration0071Up = "0071_add_removed_restriction.up.sql" -const migration0071Down = "0071_add_removed_restriction.down.sql" - -// TestLatestVersionIncludesMigration0071 (pinning LatestVersion() == 71) is -// superseded by TestLatestVersionIncludesMigration0072 -// (migration_0072_add_epoch_minted_addresses_test.go) now that 0072 claims -// the next free slot -- only one such pin lives at a time, matching how this -// test itself already superseded 0070's own version. - -// TestMigration0071AddsRemovedRestriction is a pure-Go, DB-independent check -// of the frozen migration bytes themselves — it runs even where no `dolt` -// binary is available. -// -// It pins the same guarded-PREPARE shape 0067/0068 use and for the same -// reason: the guard is what makes a raw replay of this file onto an -// already-migrated store a no-op, and Dolt accepts no unprepared conditional -// ADD COLUMN. The cost is the same pre-2.3 CLI hazard those migrations carry, -// so this migration needs the same direct-DDL override in -// cliCompatibleMigrationSQL, policed by the same two guard tests -// (TestBundleMigrationsWithPreparedALTERAreOverriddenOrJustified and -// TestAllMigrationsSQLUsesDirectDDLForKnownCLIIncompatibilities). -func TestMigration0071AddsRemovedRestriction(t *testing.T) { - upSQL, err := MigrationSQL(migration0071Up) - if err != nil { - t.Fatalf("MigrationSQL(%s) error = %v, want the migration file to exist", migration0071Up, err) - } - for _, want := range []string{ - "ALTER TABLE issue_versions ADD COLUMN removed_restriction VARCHAR(30)", - "COLUMN_NAME = 'removed_restriction'", - "@issue_versions_rr_needs_add", - } { - if !strings.Contains(upSQL, want) { - t.Errorf("0071 up migration missing %q\nfull SQL:\n%s", want, upSQL) - } - } - if !strings.Contains(strings.ToUpper(upSQL), "PREPARE STMT FROM @SQL") { - t.Error("0071 up migration must keep its guarded PREPARE block — it is what makes a raw .up.sql replay onto an already-migrated store a no-op, and Dolt accepts no unprepared conditional ADD COLUMN. Unwrapping it also invalidates cliMigration0071AddRemovedRestriction.") - } - // The bundle override is what keeps the PREPARE above off the pre-2.3 - // CLI path. Assert it directly rather than trusting the two schema_test - // assertions to stay pointed at this migration. - const wantDirectDDL = "ALTER TABLE issue_versions ADD COLUMN removed_restriction VARCHAR(30);" - if !strings.Contains(cliCompatibleMigrationSQL(migration0071Up, upSQL), wantDirectDDL) { - t.Errorf("0071's CLI bundle substitute missing direct DDL %q", wantDirectDDL) - } - if cliSubstituteAssumesWispTables(migration0071Up) { - t.Error("0071's CLI substitute touches only issue_versions, which has no wisps-side counterpart table — it must not be listed in cliSubstituteAssumesWispTables") - } - - // down.sql files are not part of the embedded FS (only migrations/*.up.sql - // is //go:embed'd — see mainSource.files), so unlike the up side above, - // this reads straight from disk by package-relative path, matching - // TestMigration0067AddsVersionedBeadsSchema's / TestMigration0068AddsAttributionStatus's - // precedent. - downBytes, err := os.ReadFile("migrations/" + migration0071Down) - if err != nil { - t.Fatalf("read %s: %v, want the migration file to exist", migration0071Down, err) - } - downSQL := string(downBytes) - for _, want := range []string{ - "ALTER TABLE issue_versions DROP COLUMN removed_restriction", - "COLUMN_NAME = 'removed_restriction'", - } { - if !strings.Contains(downSQL, want) { - t.Errorf("0071 down migration missing %q\nfull SQL:\n%s", want, downSQL) - } - } - // Only migrations/*.up.sql is embedded into the CLI fresh bundle - // (mainSource.files), so the pre-2.3 prepared-DDL hazard never reaches a - // down migration and the guard is free — 0060's/0067's/0068's downs are - // the precedent. - if !strings.Contains(strings.ToUpper(downSQL), "PREPARE STMT FROM @SQL") { - t.Error("0071 down migration must guard its DROP COLUMN the way the up migration guards its ADD COLUMN, so a partially-applied or already-rolled-back workspace rolls back safely") - } -} - -// TestMigration0071HeaderSpellsTheVocabularyTheCodeStores pins the header's -// vocabulary to the constants that store, read and label the column (finding 6 -// of the #6661 review). The header is the one place a writer learns which -// strings to store, and a writer follows it literally: it said gone-retention, -// the code compares gone_retention, so a row written from the header was not -// recognised and rendered without a label. Deriving the expected spellings -// from the constants keeps the two from drifting apart again. -// -// Pure Go, like TestMigration0071AddsRemovedRestriction: it reads the frozen -// bytes and needs no `dolt` binary. -func TestMigration0071HeaderSpellsTheVocabularyTheCodeStores(t *testing.T) { - upSQL, err := MigrationSQL(migration0071Up) - if err != nil { - t.Fatalf("MigrationSQL(%s) error = %v, want the migration file to exist", migration0071Up, err) - } - for _, stored := range []issueops.AsOfRestriction{ - issueops.AsOfRestrictionGoneRetention, - issueops.AsOfRestrictionGoneErasure, - issueops.AsOfRestrictionGoneReorganization, - issueops.AsOfRestrictionUnknown, - } { - if !strings.Contains(upSQL, string(stored)) { - t.Errorf("0071 header never spells the stored value %q", stored) - } - if hyphenated := strings.ReplaceAll(string(stored), "_", "-"); hyphenated != string(stored) && strings.Contains(upSQL, hyphenated) { - t.Errorf("0071 header spells %q as %q; the code stores and compares the underscore spelling, so a writer following the header stores a value that renders unlabeled", stored, hyphenated) - } - } -} - -// TestMigration0071AddsRemovedRestrictionThroughDoltCLI applies the full -// migration bundle through a real `dolt` binary (skipped without one — see -// testutil.RequireDoltBinary) and checks the shape acceptance criteria a -// pure-Go SQL-text check cannot: actual column type/nullability as Dolt -// reports it, and that the column stays NULL for a row that never sets it -// (Live is the absence of a value) while round-tripping any of the four -// restriction strings a row does set it to. -func TestMigration0071AddsRemovedRestrictionThroughDoltCLI(t *testing.T) { - testutil.RequireDoltBinary(t) - - dir := t.TempDir() - runDoltCommand(t, dir, "init", "--name", "test", "--email", "test@example.com") - runDoltSQL(t, dir, AllMigrationsSQL()) - - requireDoltColumnShape(t, dir, "issue_versions", "removed_restriction", "varchar(30)", "YES") - requireDoltNoRows(t, dir, "SELECT issue_id FROM issue_versions", "issue_versions") - - runDoltSQL(t, dir, `INSERT INTO issue_versions (issue_id, revision, epoch, change_at, attribution_status) VALUES ('iv-1', 1, 1, '2026-09-01 00:00:00', 'claimed')`) - // issue_id rides along as a non-NULL anchor column: queryDoltCSV trims the - // whole `dolt sql -r csv` output before parsing, and a row whose ONLY - // selected column is NULL renders as a bare blank line, which - // strings.TrimSpace swallows when it's the trailing line -- silently - // turning "one row, NULL value" into "zero rows" and passing the wrong - // assertion for the wrong reason. Selecting issue_id alongside keeps the - // row's CSV line non-blank so it survives the trim. - rows := queryDoltCSV(t, dir, `SELECT issue_id, removed_restriction FROM issue_versions WHERE issue_id = 'iv-1'`) - if len(rows) != 1 || rows[0]["removed_restriction"] != "" { - t.Fatalf("removed_restriction for a row that never set it = %v, want NULL/empty (Live is the absence of a value)", rows) - } - - runDoltSQL(t, dir, `INSERT INTO issue_versions (issue_id, revision, epoch, change_at, attribution_status, removed_at, removed_reason, removed_restriction) VALUES ('iv-2', 1, 1, '2026-09-01 00:00:00', 'claimed', '2026-09-02 00:00:00', 'retention window elapsed', 'gone_retention')`) - rows = queryDoltCSV(t, dir, `SELECT issue_id, removed_restriction FROM issue_versions WHERE issue_id = 'iv-2'`) - if len(rows) != 1 || rows[0]["removed_restriction"] != "gone_retention" { - t.Fatalf("removed_restriction round-trip failed post-migration: %v", rows) - } -} diff --git a/internal/storage/schema/migration_0072_add_epoch_minted_addresses_test.go b/internal/storage/schema/migration_0072_add_epoch_minted_addresses_test.go deleted file mode 100644 index 67389a0e0..000000000 --- a/internal/storage/schema/migration_0072_add_epoch_minted_addresses_test.go +++ /dev/null @@ -1,184 +0,0 @@ -package schema - -import ( - "os" - "strings" - "testing" - - "github.com/steveyegge/beads/internal/testutil" -) - -// R20's epoch-transition enforcement (gastownhall/beads#5898 revision 9, -// #6136) adds one brand-new table, -// epoch_minted_addresses -- see -// migrations/0072_add_epoch_minted_addresses.up.sql for the full rationale. -// This migration is a plain, unguarded -// CREATE TABLE IF NOT EXISTS with no ALTER and no PREPARE, so it needs none -// of 0067/0068's CLI-hazard-override tests (already covered, for every -// migration including this one, by TestBundleMigrationsWithPreparedALTERAreOverriddenOrJustified -// and TestAllMigrationsSQLUsesDirectDDLForKnownCLIIncompatibilities). - -// TestLatestVersionIncludesMigration0072 pins the real next free slot this -// phase claims, superseding 0071's own version of this test -// (migration_0071_add_removed_restriction_test.go; only one such pin lives at -// a time, the same way 0071's superseded 0070's). -// -// This slice originally claimed 0069 too. It was renumbered because -// R7.1's removed_restriction migration -- a PARALLEL SIBLING of this branch, -// not an ancestor -- had also claimed 0069, and the two are now linearized: -// R7.1 goes first (it is published beneath gastownhall/beads#6661), this one -// takes the next contiguous slot. Both then moved up one more slot when -// upstream's 0069_widen_issue_versions_datetime_precision -// (gastownhall/beads#6675) landed first, and once more when -// gastownhall/beads#6943's 0070_add_participation_generation (the -// versioned-history write fence) was ordered ahead of them: migrations are -// numbered by landing order, so R7.1 is 0071 and this is 0072. -// Two files sharing a -// version number panic checkNoDuplicateVersions at store open, before any -// command's RunE, which bricks every bd invocation rather than merely -// reddening a package -- see the same hazard on #6358 vs #6008. -// -// Deliberately a hardcoded literal for the same reason 0070's and 0071's -// were: LatestVersion() drifting to 72 for the wrong reason (an unrelated -// migration landing first) should still be caught by this test failing to -// explain why 72 is epoch-minted-addresses-shaped. -func TestLatestVersionIncludesMigration0072(t *testing.T) { - const want = 72 - if got := LatestVersion(); got != want { - t.Fatalf("LatestVersion() = %d, want %d (epoch_minted_addresses migration slot claimed by gastownhall/beads#6664)", got, want) - } -} - -const migration0072Up = "0072_add_epoch_minted_addresses.up.sql" -const migration0072Down = "0072_add_epoch_minted_addresses.down.sql" - -// TestMigration0072RecordsSurvivalInTheAddressTable is a pure-Go, -// DB-independent check of the frozen migration bytes: it runs even where no -// `dolt` binary is available. -// -// An address's survival is RECORDED on its own row, not inferred from other -// rows (gastownhall/beads#6664, bee-ghosttrack review 5360880888, Major 1): -// carried_from names the root address of the lineage a token-scheme change -// carried the row from, and gone_at_epoch is the epoch the store lost it in. -// Both are NULL for a fresh mint. The index serves the two lookups that -// follow a lineage (the newest carry of a root, and every carry of a root to -// lose), and the CHECK is the database-level tripwire against a stale or -// rewound epoch being written into a loss marker. It is >=, not >, because a -// carry row is born at the new epoch and can be lost in that same epoch. -// -// The migration is edited IN PLACE and gains no ALTER and no second slot: -// 0072 has never been applied to a store, and an ALTER over an already-applied -// copy of the earlier shape would be a migration slot nothing may claim. -func TestMigration0072RecordsSurvivalInTheAddressTable(t *testing.T) { - upSQL, err := MigrationSQL(migration0072Up) - if err != nil { - t.Fatalf("MigrationSQL(%s) error = %v, want the migration file to exist", migration0072Up, err) - } - for _, want := range []string{ - "CREATE TABLE IF NOT EXISTS epoch_minted_addresses", - "address VARCHAR(255) NOT NULL,", - "store_id VARCHAR(255) NOT NULL,", - "minted_id VARCHAR(255) NOT NULL,", - "minted_epoch INT NOT NULL,", - "minted_at DATETIME NOT NULL,", - "carried_from VARCHAR(255) NULL,", - "gone_at_epoch INT NULL,", - "PRIMARY KEY (address),", - "INDEX idx_epoch_minted_addresses_carried_from (carried_from),", - "CONSTRAINT ck_epoch_minted_addresses_gone_not_before_mint CHECK (gone_at_epoch IS NULL OR gone_at_epoch >= minted_epoch)", - } { - if !strings.Contains(upSQL, want) { - t.Errorf("0072 up migration missing %q\nfull SQL:\n%s", want, upSQL) - } - } - - // Only the statements count here: the header comments describe the - // migration in words that would otherwise trip these checks. - statements := strings.ToUpper(stripSQLLineComments(upSQL)) - if strings.Contains(statements, "ALTER TABLE") { - t.Error("0072 up migration must not ALTER anything: the survival columns belong to its own CREATE TABLE, because the migration is edited in place while no store has applied it") - } - if strings.Contains(statements, "PREPARE") { - t.Error("0072 up migration must stay plain DDL: CREATE TABLE IF NOT EXISTS needs no guarded PREPARE, and adding one would put it on the pre-2.3 CLI hazard path") - } - if got := strings.Count(statements, "CREATE TABLE"); got != 1 { - t.Errorf("0072 up migration has %d CREATE TABLE statements, want exactly 1", got) - } - - // Only migrations/*.up.sql is embedded, so the down side reads from disk by - // package-relative path, as migration 0067's test does. - downBytes, err := os.ReadFile("migrations/" + migration0072Down) - if err != nil { - t.Fatalf("read %s: %v, want the migration file to exist", migration0072Down, err) - } - if !strings.Contains(string(downBytes), "DROP TABLE IF EXISTS epoch_minted_addresses") { - t.Errorf("0072 down migration must drop epoch_minted_addresses, got:\n%s", downBytes) - } -} - -// TestMigration0072RecordsSurvivalInTheAddressTableThroughDoltCLI applies the -// full migration bundle through a real `dolt` binary (skipped without one, see -// testutil.RequireDoltBinary) and checks what a pure-Go SQL-text check cannot: -// the column types and nullability Dolt reports, the index it actually built, -// and that the CHECK is enforced on both INSERT and UPDATE, with the boundary -// it is meant to have. -func TestMigration0072RecordsSurvivalInTheAddressTableThroughDoltCLI(t *testing.T) { - testutil.RequireDoltBinary(t) - - dir := t.TempDir() - runDoltCommand(t, dir, "init", "--name", "test", "--email", "test@example.com") - runDoltSQL(t, dir, AllMigrationsSQL()) - - requireDoltColumnShape(t, dir, "epoch_minted_addresses", "carried_from", "varchar(255)", "YES") - requireDoltDataType(t, dir, "epoch_minted_addresses", "gone_at_epoch", "int", "YES") - // The five columns the mint writes keep their shape, so the mint's - // five-column INSERT is unchanged. - requireDoltColumnShape(t, dir, "epoch_minted_addresses", "address", "varchar(255)", "NO") - requireDoltColumnShape(t, dir, "epoch_minted_addresses", "minted_epoch", "int", "NO") - requireDoltNoRows(t, dir, "SELECT address FROM epoch_minted_addresses", "epoch_minted_addresses") - requireDoltCount(t, dir, - `SELECT COUNT(*) AS c FROM INFORMATION_SCHEMA.STATISTICS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'epoch_minted_addresses' AND INDEX_NAME = 'idx_epoch_minted_addresses_carried_from' AND COLUMN_NAME = 'carried_from'`, - "1") - - // A fresh mint writes the five original columns and is a root row: both - // survival columns come out NULL with no change to the INSERT. - runDoltSQL(t, dir, `INSERT INTO epoch_minted_addresses (address, store_id, minted_id, minted_epoch, minted_at) VALUES ('root-a', 'store', 'record-a', 3, '2026-09-01 00:00:00')`) - requireDoltCount(t, dir, `SELECT COUNT(*) AS c FROM epoch_minted_addresses WHERE address = 'root-a' AND carried_from IS NULL AND gone_at_epoch IS NULL`, "1") - - // A carry row names the lineage root. - runDoltSQL(t, dir, `INSERT INTO epoch_minted_addresses (address, store_id, minted_id, minted_epoch, minted_at, carried_from) VALUES ('carry-a', 'store', 'record-a', 5, '2026-09-01 00:00:00', 'root-a')`) - requireDoltCount(t, dir, `SELECT COUNT(*) AS c FROM epoch_minted_addresses WHERE carried_from = 'root-a'`, "1") - - // The CHECK: an epoch lost in may not precede the epoch minted in. - if err := runDoltSQLExpectingError(t, dir, `UPDATE epoch_minted_addresses SET gone_at_epoch = 2 WHERE address = 'root-a'`); err == nil { - t.Error("UPDATE gone_at_epoch below minted_epoch succeeded, want the CHECK constraint to reject it") - } - if err := runDoltSQLExpectingError(t, dir, `INSERT INTO epoch_minted_addresses (address, store_id, minted_id, minted_epoch, minted_at, gone_at_epoch) VALUES ('bad-b', 'store', 'record-b', 4, '2026-09-01 00:00:00', 3)`); err == nil { - t.Error("INSERT with gone_at_epoch below minted_epoch succeeded, want the CHECK constraint to reject it") - } - // Equal is allowed: a carry row is born at the new epoch and can be lost in - // that same epoch. - runDoltSQL(t, dir, `UPDATE epoch_minted_addresses SET gone_at_epoch = 3 WHERE address = 'root-a'`) - requireDoltCount(t, dir, `SELECT COUNT(*) AS c FROM epoch_minted_addresses WHERE address = 'root-a' AND gone_at_epoch = 3`, "1") - // A later epoch is allowed too. - runDoltSQL(t, dir, `UPDATE epoch_minted_addresses SET gone_at_epoch = 9 WHERE address = 'carry-a'`) - requireDoltCount(t, dir, `SELECT COUNT(*) AS c FROM epoch_minted_addresses WHERE address = 'carry-a' AND gone_at_epoch = 9`, "1") - - // The address stays the primary key. - if err := runDoltSQLExpectingError(t, dir, `INSERT INTO epoch_minted_addresses (address, store_id, minted_id, minted_epoch, minted_at) VALUES ('root-a', 'store', 'record-a', 3, '2026-09-01 00:00:00')`); err == nil { - t.Error("duplicate address insert succeeded, want primary key violation") - } -} - -// stripSQLLineComments drops "--" comment lines so a check over a migration's -// statements is not tripped by the prose in its header. -func stripSQLLineComments(sqlText string) string { - var kept []string - for _, line := range strings.Split(sqlText, "\n") { - if strings.HasPrefix(strings.TrimSpace(line), "--") { - continue - } - kept = append(kept, line) - } - return strings.Join(kept, "\n") -} diff --git a/internal/storage/schema/migration_freeze_test.go b/internal/storage/schema/migration_freeze_test.go deleted file mode 100644 index 678762e11..000000000 --- a/internal/storage/schema/migration_freeze_test.go +++ /dev/null @@ -1,183 +0,0 @@ -package schema - -import ( - "crypto/sha256" - "encoding/hex" - "io/fs" - "testing" -) - -// Migration freeze (Beads 1.2 release remediation). -// -// Migrations 0001–0066 (and ignored/ 0001–0026) are SHIPPED: the 1.2.x -// releases published schema v65 to thousands of installs and the 1.3.x -// releases added 0066 and ignored/ 0025–0026, so these files are now the -// de-facto on-disk contract of every migrated database. Amending a shipped -// migration forks installed schemas from freshly-migrated ones — the exact -// skew the smart gate's fork-skew detection exists to catch at runtime. This -// test catches it in CI instead: the content hash of every shipped -// up-migration is pinned; any change must land as a NEW migration, numbered -// after the newest file in its directory. When a release ships new -// migrations, raise the floors below and pin their hashes. -// -// If this test fails because you edited a shipped migration: revert the edit -// and write a new migration. There is no legitimate reason to update a pinned -// hash for an already-released version. - -const ( - frozenMainThrough = 66 - frozenIgnoredThrough = 26 -) - -var frozenMigrationHashes = map[string]string{ - "0001_create_issues.up.sql": "0c9bc4d97e2c5ee5d276c952333b934cd7d7d18210997d6bfd72d17f0fdb14f0", - "0002_create_dependencies.up.sql": "9ca011fa0b24ddf26f689eebf347cc2f80dc90256b099d91b993ec82c4d74da5", - "0003_create_labels.up.sql": "1780f0dc941d97b74af53d5603646d05e83d94eee5a02ac68ccf929200deaec0", - "0004_create_comments.up.sql": "35ac76b3d0b2d6287cad1329c77c8a0562bb6310ba0ac34ae273d72d4bed0e2a", - "0005_create_events.up.sql": "c8cbe4bebbe0f92c8a0ccc2a1a0d11ad442635811c2dab093c34c9341a8499b2", - "0006_create_config.up.sql": "435f5ef129dd4999a9b393c98d633038f0e443280a26e63f7d4315bf025dab1f", - "0007_create_metadata.up.sql": "f92c4d3dd32cae489142de34c4706900736c91cdc53b0d54e87e71656b828dc9", - "0008_create_child_counters.up.sql": "99db9b4b5a785ddd209dff628364c4c1eff549c3ca5ec48a64c604b15ef95bfb", - "0009_create_issue_snapshots.up.sql": "269ef83f083766b7f2fd2dfd2814ef36a585ae4aa18bcab0468a713d48595e48", - "0010_create_compaction_snapshots.up.sql": "cb34cbe311fd89a17d72067f449a65089f2fc9a7c4b3774d54448db5ebc3d659", - "0011_create_repo_mtimes.up.sql": "bde5204466541dddb53a652fe58561c08b6b3c1fa7feb38a0a582d432d76bda6", - "0012_create_routes.up.sql": "68e8ef1fc336e8ca6973f6b927b09d3206a7ebd96db9c4cdceab52fb8498c431", - "0013_create_issue_counter.up.sql": "7ddb30dabe32a66197de78f62400f41d000cf43d0ba8ba2a3943536504dddcdb", - "0014_create_interactions.up.sql": "0e17f1cb9fc5ee5a7c43ca8e02b9fd59814f8fd4372f9ffb17b50c34388614ce", - "0015_create_federation_peers.up.sql": "6aa04b3f66566b243c9a4fc09136af5c17f0ffe947a489245e3c451b995a59b9", - "0016_default_config.up.sql": "5d556c01404eb06acfb9df2b20fde36a2e91bd8a7d14462acbd3c0ae928ea286", - "0017_create_ready_issues_view.up.sql": "4bab7acbe2d8e441f156cc5912f421dcb214b9b08d7958eacc8e3a7318ab5f57", - "0018_create_blocked_issues_view.up.sql": "84dc6a3950a5447e6665e3d98b43f11a7631dc4e53bab1be332d135e51c9511f", - "0019_wisps_dolt_ignore.up.sql": "3df660feb6b64c84ef74c408fe8f5840a2815936534362ed123122f795a21c26", - "0020_create_wisps.up.sql": "bca95662441635454b879bb44ca1e607df9c5efd8fd802be3d53a7371a5ced86", - "0021_create_wisp_auxiliary.up.sql": "d19284a2be70a448db7746ae009cb264dc86dac0d9094aae9fe80a713a08ee70", - "0022_wisp_dep_type_index.up.sql": "fc1375e64620cd4264516718d8f924d88131881f4c00c51471b7630c11b74a45", - "0023_add_no_history_column.up.sql": "d11cd7cb462239e49c0b2f9b93ac73771106d5450ebc3422bb7737748c6f967b", - "0024_create_custom_status_type_tables.up.sql": "816da3feeed531ca9787dca8d4b8072dd94b64d573cf4b6751c61f2390c88a68", - "0025_update_ready_issues_view.up.sql": "09782d195f22d620ba41802856f9eb6e1a10f68bc5bcc8f3616624f8c28b576a", - "0026_update_blocked_issues_view.up.sql": "33daef6b5a07364fc5f59ab59b0f5c677dcf98d9bd0866c7c3bea874f1f01348", - "0027_add_started_at.up.sql": "dd3b58a8bbed4c49e855a6002981a285f10b6094938534dd3da9b121155f24d6", - "0028_local_state_dolt_ignore.up.sql": "24a816406a6f40c66db98697869d172b2b4366b107788fdd00f44b99abee45c3", - "0029_create_local_metadata.up.sql": "b989fc49ab03e06ff6520f150d6958efdbd3f444b69b824f7b4bb283ca6f2b65", - "0030_migrate_local_metadata_keys.up.sql": "8d55135274c1c75e722e802b250911cc574d52a4ba74fbb13edde84f30f9b3a4", - "0031_wisp_events_created_at_index.up.sql": "2d5cf3a6d4245b48c1b893efcc17b9fa25f1ea807e61290cf54064451fc58d93", - "0032_drop_schema_migrations_applied_at.up.sql": "42b86d6b7352f7e158532cc67f4afa142dc2503ce9f780733c06fc9da3478751", - "0033_add_wisp_type_column.up.sql": "9256746bb0d6ad9ed9afba0f0501259f0a3599b7d7da507dd7d808a13c688c75", - "0034_add_spec_id_column.up.sql": "7add95ead971c57618cfbc52e5cb6771d872785bfc035bde12cd27a76358848e", - "0035_migrate_infra_to_wisps.up.sql": "f917fe319540155572f5c51618cbbe20ec8b9b669d5926d68e3a62629c317c5a", - "0036_cleanup_autopush_metadata.up.sql": "e55f1870c2bc031c574b495ebca650365a9c8088d7a8968fb86a19cab81267e8", - "0037_uuid_primary_keys.up.sql": "7526beadee13aa7d6ee2175e9a543210b07c1a4426c382f251ebd31eebe32745", - "0038_drop_hop_columns.up.sql": "002dc637d45840487c35a01ce15cee04872baa45c21f8e20170a4339135a38e7", - "0039_drop_child_counters_fk.up.sql": "db65f241d1e82da41f34b960abc912f78b72e7dc1cb6dd2298974308edf8b768", - "0040_ignored_tables_also_nonlocal_tables.up.sql": "c7068d67ad3e2b4852549e04d97f25814121de83db919e220bdcb9bdd9b61e5e", - "0041_split_dependencies_target.up.sql": "e84ef393576df31550570fd3217579b1880850c0645b7d2b6ad8cf55a18ac1c0", - "0042_add_on_update_cascade.up.sql": "9d0fbb3d02955d25442c4a628ac518236068a97ecf164edbc814216bd5a55adc", - "0043_drop_dependencies_generated_column.up.sql": "548b8a69ab82179b7ee7ab3cc87ef00b3ae28d5a3a4976f42364f6939e384668", - "0044_update_views_drop_depends_on_id.up.sql": "531524854cc0e2361817d1ad94ba9470b0e81e378a5422ffb8bce71f3500adf9", - "0045_update_blocked_view_drop_depends_on_id.up.sql": "395dcbf186deedb592d8ed144e0b7953455efa0ccf02076d3b63e5e5d05dc241", - "0046_add_is_blocked.up.sql": "4d6a5a5fc5e386f2c538b4f3574b82fb2ba025176997953a0a4fbd2a91cddf68", - "0047_recompute_mixed_is_blocked.up.sql": "4aff5a7cc2e1e0250ce4cdb911d010c936681871fb627bceee4b2dc4647dd0a1", - "0048_widen_event_value_columns.up.sql": "093c7a41145508879844d16cbe5acfa69baddbb19bf3e05f00bc08720adea657", - "0049_longtext_large_content_columns.up.sql": "3359343efb7c28e5803de07e9088670489615d151843d7f23705f3ca8dd6a1e8", - "0050_dependencies_deterministic_id.up.sql": "96265ff898753f3bb34549fafa18bbaeb67519705b8cf4e32e67f1058df6841a", - "0051_drop_aux_id_defaults.up.sql": "deb741d5ac5fc1e0cd2e60eb47e84f384ed3455b18d3e2f12cb4e935fe461059", - "0052_add_date_indexes.up.sql": "60ae32bc26c41da2e743c1172f73930d18482b8a98aea08b9de85f6009f9c01d", - "0053_repair_rig_wisps.up.sql": "f13909f0db89f48db694a782d1805eee7ee1b2b2e4dcb3d4b12352333cbcb330", - "0054_add_lease_columns.up.sql": "2e51058ba15be9d87415949bb03c2c7c2f0e0743c5ae0d3e819b617d95f61680", - "0055_move_leases_to_table.up.sql": "2237ddbe13f5a783c65da3bb1292d9a5d89a5f22fddb0c33d2db57f1492df22c", - "0056_add_comments_keyset_index.up.sql": "acc28e9b1aa7272c16c86fcb9fd4a2038ae47729a60b53bb75f3a9df05cd3a82", - "0057_events_value_columns_idempotent_longtext.up.sql": "f86e0f77be2e58affe1c485767b8dd450408d782c0413a41948ca0c0b6d72d06", - "0058_heal_wisp_dependencies_split_constraints.up.sql": "9c1c6d557a03e71cf6b49123831c9f5b5f910fcea3ca51e982ec4acce04ea6ec", - "0059_recompute_null_gate_is_blocked.up.sql": "f0186029a1c87d579b67e0540ec22ad0bd0fc59793b07f7d24a24e122b77c394", - "0060_add_storage_class.up.sql": "40419632ba75d92dfcf08e16e77a780f014172a17068f6021937df9086a24588", - "0061_content_derived_aux_ids.up.sql": "d2d65b92789f137da8ee586067c862a9c7744921ccac421e1b793f42050b62a2", - "0062_events_dolt_ignore.up.sql": "c7de6089997344784a71e7d82ea5e417baaf66320b4533d225a4a6bc3c74222a", - "0063_create_provenance_events.up.sql": "1ef173be07ca58d17cb09ba3b3f5ca0c96edfcace8d52ae3adde2fd27e8248bd", - "0064_create_events_journal.up.sql": "25ea9ad8e849dc7e32e7c5850631bedc63a5c5da3bdc58ccb2762fd7a6c758fc", - "0065_widen_wisp_comments_text.up.sql": "c7341bb977daff92e073db2579e63058cef88d43f1e09b13fa153a772caac2a6", - "0066_add_events_journal_actor.up.sql": "a524d42ba0eced9350edbf6402dc2952957ff8f75a896bc8339c4cbc8596c793", -} - -var frozenIgnoredMigrationHashes = map[string]string{ - "0001_create_local_state_tables.up.sql": "a73e9a486124f89e90d50f4597fd1d7ad08e5d0d654459ec87b7520b47f1bb5f", - "0002_create_wisp_child_counters.up.sql": "c575c6710169f2e697f3aca60f5f698acc8a64af0edb1e2305b9d377fd845f45", - "0003_split_wisp_dependencies_target.up.sql": "f82947deeafe589598e2a1802eddf17712bd322692f924bb5653b0fbd5c46a0d", - "0004_add_wisp_aux_fks.up.sql": "12e18a8bdd214d6f96625e78998433ccc9c2d0ef4d1a35307f2653e725c800b9", - "0005_drop_wisp_dependencies_generated_column.up.sql": "08780b28ddef6c15f5de558ea08d821b52610f810555739ee1d737779986c607", - "0006_add_wisp_is_blocked.up.sql": "4746f10d7040d0b7a9e127437356de87765d12d2d27ee37c9800191e6b619d14", - "0007_recompute_wisp_is_blocked.up.sql": "c3c8d92846a54183cc99afc09f779f6f5ba8030c16ce46b3ee750314bf134627", - "0008_widen_wisp_event_value_columns.up.sql": "51375854ec081d2e0b1434b5ff8b6d87092b891636214ebb9f1dbef22438cc1a", - "0009_aux_row_id_rekey_marker.up.sql": "3d1ee4f9dde3342d110474c8090a30c474edafe2290e4372a3fc4607c1fdc030", - "0010_drop_wisp_id_defaults.up.sql": "59d6644667b7e34f9d23261cb694d461ddd4953969e75519d370b0cce8afd0e8", - "0011_cleanup_orphaned_child_counters.up.sql": "0efade0ecb620420ee42127d3f67230da108e31f2ff3c4281855a8dc9e187ecd", - "0012_create_leases.up.sql": "e128a1301a2bcb967f684b09318aa5cf7ed9adb5f6dd7d412de3d67d1de2b28d", - "0013_add_wisp_row_lock.up.sql": "127a4af7012769e5f016b13b198b7a57ff8386a10f8a68ffb9ff204989564d1b", - "0014_add_wisp_comments_keyset_index.up.sql": "29ad3654218700481b9541331c714ecbbe96dc81c04dc83fba6bf8d35bed7fa4", - "0015_recompute_null_gate_wisp_is_blocked.up.sql": "e761d5c3ceb9610edbb2e1d153e82d235cc7d7f0b87f28a45bec43835e6a4e19", - "0016_add_lease_granted_node.up.sql": "31a55421b78d8c90db6cfba5bf8e99e87d6123271575282f761aa077e5d36386", - "0017_add_wisps_defer_until_index.up.sql": "1f031aaf2037280e62cbf604748a4157bf25e3a60b3392af7d74835c5059bdd8", - "0018_aux_row_id_rekey2_marker.up.sql": "3c2120bf55d15e71a9f96972007c3d914154d17ea37a2b51b44412e3e3df6ef1", - "0019_create_events.up.sql": "db072d67a403e60d5bcbe924b84e473adb09f2326d36ede82fcdab17bb1e2a87", - "0020_add_wisp_storage_class.up.sql": "9ff476e6c73a3a477aec95143d5cbf180517a9484f647447392576f13181a015", - "0021_widen_wisp_large_content_columns.up.sql": "3cfa1e07a3d02eb7f791814d9642a93b9af3bf63728943afa05dcb0e489919c8", - "0022_create_events_journal.up.sql": "3d0adcb9f5833b4ba70fb48f88e1043250796feb7173abe000ea4de85eb8b3a5", - "0023_repair_events_journal_shape.up.sql": "d0edd83ca4cfbc46bd17799c6497a70c8c011d130d08a00c71daf792ee1dcd51", - "0024_widen_wisp_comments_text.up.sql": "69a96330e00643dfeddc4bf6ae9bf3b743a3cfddd86f16dc7b8912758b1b570d", - "0025_add_events_journal_actor.up.sql": "c81ca9edc80c25f6ab4f92d05d6ea412a780b108d1ed83745027440e9723dceb", - "0026_dep_rekey_dedup_marker.up.sql": "dc5c1b9a74e42184451796068ecba1ca11d8ebd0fe7e8c4bd4900d0cfee30446", -} - -func verifyFrozenMigrations(t *testing.T, fsys fs.FS, dir string, frozen map[string]string, frozenThrough int) { - t.Helper() - entries, err := fs.ReadDir(fsys, dir) - if err != nil { - t.Fatalf("reading embedded %s: %v", dir, err) - } - seen := map[string]bool{} - for _, entry := range entries { - if entry.IsDir() { - continue - } - name := entry.Name() - version, err := parseVersion(name) - if err != nil { - t.Fatalf("unparseable migration filename %q: %v", name, err) - } - if version > frozenThrough { - continue // not yet released — frozen when a release ships it - } - want, ok := frozen[name] - if !ok { - t.Errorf("shipped migration %s (v%d <= frozen floor %d) has no pinned hash — a released version was renamed or replaced; restore the original file", name, version, frozenThrough) - continue - } - seen[name] = true - content, err := fs.ReadFile(fsys, dir+"/"+name) - if err != nil { - t.Fatalf("reading embedded %s/%s: %v", dir, name, err) - } - sum := sha256.Sum256(content) - if got := hex.EncodeToString(sum[:]); got != want { - t.Errorf("shipped migration %s was AMENDED (hash %s, pinned %s) — shipped migrations are frozen; land the change as a new migration instead", name, got, want) - } - } - for name := range frozen { - if !seen[name] { - t.Errorf("pinned migration %s is missing from the embedded FS — a released version was deleted or renamed", name) - } - } -} - -// TestShippedMigrationsAreFrozen pins the content of every up-migration that a -// release has shipped. See the file comment for the policy. -func TestShippedMigrationsAreFrozen(t *testing.T) { - verifyFrozenMigrations(t, upMigrations, "migrations", frozenMigrationHashes, frozenMainThrough) -} - -// TestShippedIgnoredMigrationsAreFrozen is the ignored-sequence twin. The -// ignored tables are clone-local, so an amendment does not fork synced -// history — but it DOES make freshly-materialized clones diverge from -// long-lived ones, the same class of bug one plane over. -func TestShippedIgnoredMigrationsAreFrozen(t *testing.T) { - verifyFrozenMigrations(t, upIgnoredMigrations, "migrations/ignored", frozenIgnoredMigrationHashes, frozenIgnoredThrough) -} diff --git a/internal/storage/schema/migration_repairs.go b/internal/storage/schema/migration_repairs.go index b4fe8ce01..40b463ba0 100644 --- a/internal/storage/schema/migration_repairs.go +++ b/internal/storage/schema/migration_repairs.go @@ -93,39 +93,8 @@ func commitNonlocalRepair(ctx context.Context, db DBConn, message string) error // // It does NOT clear the staging area; a caller with anything possibly staged // must unstage first (see unstagePreExistingTables). -// -// The unforced DOLT_ADD is the right default — it keeps a clone-local table -// out of a repair-labeled commit — but it is WRONG for a table whose own name -// matches a seeded dolt_ignore pattern; such a caller must use -// commitScopedIgnoredTableChange instead. func commitScopedTableChange(ctx context.Context, db DBConn, table, message string) error { - return stageAndCommitScoped(ctx, db, "CALL DOLT_ADD(?)", table, message) -} - -// commitScopedIgnoredTableChange is commitScopedTableChange for the one caller -// whose table name matches a pattern in doltIgnorePatterns: the #4356 untrack -// scratch, which "__temp__%" covers. -// -// Forcing is not optional there. Unforced staging filters ignore-matched names -// out of the staging list SILENTLY (no error), and the filter classifies purely -// by pattern without ever consulting HEAD — so a table that IS committed at HEAD -// is dropped exactly like a fresh one. --skip-empty then turns the empty staged -// diff into a successful no-op, and the caller's uncommitted delete delta -// survives forever: dolt refuses a pull on any non-add unstaged delta whether or -// not it is ignore-matched, so the store stays wedged while dolt_status hides -// the cause. -// -// Scoping still holds: '-f' widens WHICH names may be staged, never HOW MANY, -// so this stages exactly the one table named. Like its unforced twin it does not -// clear the staging area. -func commitScopedIgnoredTableChange(ctx context.Context, db DBConn, table, message string) error { - return stageAndCommitScoped(ctx, db, "CALL DOLT_ADD('-f', ?)", table, message) -} - -// stageAndCommitScoped is the shared body of the two scoped-commit helpers. -// They differ only in whether stageSQL forces past the dolt_ignore filter. -func stageAndCommitScoped(ctx context.Context, db DBConn, stageSQL, table, message string) error { - if err := DrainCall(ctx, db, stageSQL, table); err != nil { + if err := DrainCall(ctx, db, "CALL DOLT_ADD(?)", table); err != nil { return fmt.Errorf("staging %s: %w", table, err) } return DrainCall(ctx, db, "CALL DOLT_COMMIT('-m', ?, '--skip-empty')", message) diff --git a/internal/storage/schema/migration_watchdog_test.go b/internal/storage/schema/migration_watchdog_test.go deleted file mode 100644 index d99976158..000000000 --- a/internal/storage/schema/migration_watchdog_test.go +++ /dev/null @@ -1,308 +0,0 @@ -package schema - -import ( - "bytes" - "context" - "database/sql" - "errors" - "fmt" - "io" - "os" - "strings" - "sync" - "testing" - "time" - - "golang.org/x/term" -) - -// TestRunMigrationWithWatchdog_NoWarningUnderThreshold covers be-yyzzs: a -// migration that finishes well inside the threshold must produce zero -// watchdog output — the warning is a "still working" signal, not a -// per-migration progress line (that already exists via runMigrations' -// "Applying migration..."/"done" prints). -func TestRunMigrationWithWatchdog_NoWarningUnderThreshold(t *testing.T) { - var buf bytes.Buffer - err := runMigrationWithWatchdog(context.Background(), &buf, 42, "add_date_indexes", 100*time.Millisecond, - func(ctx context.Context) error { return nil }) - - if err != nil { - t.Fatalf("unexpected error: %v", err) - } - if got := buf.String(); got != "" { - t.Errorf("want no watchdog output for a fast migration, got %q", got) - } -} - -// TestRunMigrationWithWatchdog_WarnsOnThresholdExceeded covers be-yyzzs: once -// a migration runs past the threshold, a WARN line naming the version, the -// human migration name, and the elapsed time must appear on the writer that -// runMigrations already uses for progress output. -func TestRunMigrationWithWatchdog_WarnsOnThresholdExceeded(t *testing.T) { - var buf bytes.Buffer - err := runMigrationWithWatchdog(context.Background(), &buf, 42, "add_date_indexes", 15*time.Millisecond, - func(ctx context.Context) error { - time.Sleep(40 * time.Millisecond) - return nil - }) - - if err != nil { - t.Fatalf("unexpected error: %v", err) - } - got := buf.String() - if !strings.Contains(got, "WARN") { - t.Errorf("want a WARN-level line, got %q", got) - } - if !strings.Contains(got, fmt.Sprintf("%04d", 42)) { - t.Errorf("want the migration version in the warning, got %q", got) - } - if !strings.Contains(got, "add_date_indexes") { - t.Errorf("want the migration name in the warning, got %q", got) - } - if !strings.Contains(got, "still running") { - t.Errorf("want an elapsed-time/still-running marker, got %q", got) - } -} - -// TestRunMigrationWithWatchdog_RepeatsWarningEveryInterval covers be-yyzzs: -// a migration that keeps running past multiple threshold intervals must get -// a fresh warning each interval, not just once — so an operator watching -// logs can distinguish "still working" from "stopped emitting anything". -func TestRunMigrationWithWatchdog_RepeatsWarningEveryInterval(t *testing.T) { - var buf bytes.Buffer - err := runMigrationWithWatchdog(context.Background(), &buf, 7, "backfill", 10*time.Millisecond, - func(ctx context.Context) error { - time.Sleep(55 * time.Millisecond) - return nil - }) - - if err != nil { - t.Fatalf("unexpected error: %v", err) - } - count := strings.Count(buf.String(), "WARN") - if count < 3 { - t.Errorf("want at least 3 repeated warnings over ~5.5 intervals, got %d in %q", count, buf.String()) - } -} - -// TestRunMigrationWithWatchdog_ContextUnaffected covers be-yyzzs: the -// watchdog is observability only. It must hand fn the exact ctx it was -// given — no wrapping with a new timeout/cancel — and that ctx must still be -// live (Err() == nil) even after the watchdog has fired multiple warnings. -func TestRunMigrationWithWatchdog_ContextUnaffected(t *testing.T) { - type sentinelKey struct{} - ctx := context.WithValue(context.Background(), sentinelKey{}, "sentinel-value") - - var sawValue any - var errDuringRun error - var buf bytes.Buffer - - err := runMigrationWithWatchdog(ctx, &buf, 1, "initial", 10*time.Millisecond, - func(fnCtx context.Context) error { - time.Sleep(35 * time.Millisecond) - sawValue = fnCtx.Value(sentinelKey{}) - errDuringRun = fnCtx.Err() - return nil - }) - - if err != nil { - t.Fatalf("unexpected error: %v", err) - } - if sawValue != "sentinel-value" { - t.Errorf("fn did not receive the original ctx (missing sentinel value); got %v", sawValue) - } - if errDuringRun != nil { - t.Errorf("ctx passed to fn was canceled/expired by the watchdog: %v", errDuringRun) - } - if ctx.Err() != nil { - t.Errorf("caller's ctx was canceled/expired by the watchdog: %v", ctx.Err()) - } -} - -// TestRunMigrationWithWatchdog_ErrorPassthrough covers be-yyzzs: the -// watchdog must never alter the migration's outcome. A migration that runs -// long AND ultimately fails must still surface the exact original error — -// the warning is purely additive, not a substitute for or wrapper around it. -func TestRunMigrationWithWatchdog_ErrorPassthrough(t *testing.T) { - sentinel := errors.New("boom: migration 0007 failed") - var buf bytes.Buffer - - err := runMigrationWithWatchdog(context.Background(), &buf, 7, "backfill", 10*time.Millisecond, - func(ctx context.Context) error { - time.Sleep(25 * time.Millisecond) - return sentinel - }) - - if !errors.Is(err, sentinel) { - t.Errorf("want sentinel error passed through unchanged, got %v", err) - } - if !strings.Contains(buf.String(), "WARN") { - t.Errorf("want the watchdog to still have warned before the failure, got %q", buf.String()) - } -} - -// syncWriter serializes writes so the panic test below can read what the -// watchdog has emitted so far without racing it. The other tests in this file -// use a bare bytes.Buffer because the watchdog goroutine is always joined -// before they read; the panic case reads at a point where a leaked goroutine -// would still be writing, which is the leak it exists to catch. -type syncWriter struct { - mu sync.Mutex - buf bytes.Buffer -} - -func (w *syncWriter) Write(p []byte) (int, error) { - w.mu.Lock() - defer w.mu.Unlock() - return w.buf.Write(p) -} - -func (w *syncWriter) String() string { - w.mu.Lock() - defer w.mu.Unlock() - return w.buf.String() -} - -// TestRunMigrationWithWatchdog_StopsWarnerWhenFnPanics pins the deferred -// close(done)/wg.Wait(): Dolt's embedded engine can panic out of DDL, and a -// caller that recovers instead of dying (internal/httpapi/server.go's -// middleware recovers everything except http.ErrAbortHandler and keeps -// serving) leaves the process alive afterwards. Stopping the warner on the -// straight-line path after fn returns is skipped by a panic, which orphans the -// ticker goroutine into warning about a migration that is no longer running — -// on the deliberately un-gated watchdogStderr — for the rest of that process's -// lifetime. -func TestRunMigrationWithWatchdog_StopsWarnerWhenFnPanics(t *testing.T) { - const interval = 10 * time.Millisecond - out := &syncWriter{} - - func() { - defer func() { - if r := recover(); r == nil { - t.Errorf("want fn's panic to propagate to the caller unchanged, got none") - } - }() - _ = runMigrationWithWatchdog(context.Background(), out, 47, "recompute_mixed_is_blocked", interval, - func(ctx context.Context) error { - time.Sleep(35 * time.Millisecond) - panic("dolt engine panicked out of DDL") - }) - }() - - // The warner must already be joined by the time the panic reaches here, so - // the line count cannot grow any further. - settled := strings.Count(out.String(), "WARN") - if settled == 0 { - t.Fatalf("want at least one WARN before the panic, otherwise the check below is vacuous; got %q", out.String()) - } - time.Sleep(5 * interval) - if after := strings.Count(out.String(), "WARN"); after != settled { - t.Errorf("watchdog goroutine outlived the panicking migration: %d WARN lines when the panic unwound, %d after a further %s", settled, after, 5*interval) - } -} - -// TestMigrationWatchdogIntervalDuration covers be-yyzzs: the threshold must -// default to 5 minutes and be overridable via BEADS_MIGRATION_WATCHDOG_INTERVAL, -// following this codebase's existing timeoutFromEnv/BEADS_*_TIMEOUT convention -// (internal/storage/dolt/store.go's cliExecTimeoutEnv/timeoutFromEnv): unset, -// empty, or unparsable values fall back to the default. -func TestMigrationWatchdogIntervalDuration(t *testing.T) { - cases := []struct { - name string - envVal string - setEnv bool - want time.Duration - }{ - {name: "unset_defaults_to_5m", setEnv: false, want: 5 * time.Minute}, - {name: "empty_defaults_to_5m", setEnv: true, envVal: "", want: 5 * time.Minute}, - {name: "valid_duration_string_overrides", setEnv: true, envVal: "10s", want: 10 * time.Second}, - {name: "invalid_value_falls_back_to_default", setEnv: true, envVal: "not-a-duration", want: 5 * time.Minute}, - // internal/storage/dolt/store.go's parseTimeout — the convention this - // function's doc comment claims to follow — documents "bare numbers - // treated as seconds (e.g. \"90\")". Without this, a plausible - // BEADS_MIGRATION_WATCHDOG_INTERVAL=300 silently means 5m rather than - // the 5m the operator was trying to change. - // NB: not "300" — 300s IS the 5m default, so that value cannot tell - // "parsed as seconds" from "fell back". - {name: "bare_seconds_treated_as_seconds", setEnv: true, envVal: "90", want: 90 * time.Second}, - {name: "bare_seconds_larger_than_default", setEnv: true, envVal: "600", want: 10 * time.Minute}, - {name: "bare_zero_falls_back_to_default", setEnv: true, envVal: "0", want: 5 * time.Minute}, - {name: "negative_falls_back_to_default", setEnv: true, envVal: "-5s", want: 5 * time.Minute}, - } - - for _, tc := range cases { - t.Run(tc.name, func(t *testing.T) { - if tc.setEnv { - t.Setenv(migrationWatchdogIntervalEnv, tc.envVal) - } else { - // The "unset" arm has to actually unset: t.Setenv registers the - // restore of whatever the ambient value was, then os.Unsetenv - // removes it for the duration of the subtest. Without this, - // a shell exporting BEADS_MIGRATION_WATCHDOG_INTERVAL (this - // repo has a history of ambient BEADS_* reddening tests) flows - // straight into the call and the compiled-in default this - // subtest exists to pin is never exercised. - t.Setenv(migrationWatchdogIntervalEnv, "") - os.Unsetenv(migrationWatchdogIntervalEnv) - } - if got := migrationWatchdogIntervalDuration(); got != tc.want { - t.Errorf("migrationWatchdogIntervalDuration() = %v; want %v", got, tc.want) - } - }) - } -} - -// slowMigrationDB makes the first migration body take long enough for a -// short-interval watchdog to fire, so the wiring between runMigrations and -// runMigrationWithWatchdog can be exercised end to end rather than asserted -// on a package variable. -type slowMigrationDB struct { - mockDB - once sync.Once - d time.Duration -} - -func (s *slowMigrationDB) ExecContext(ctx context.Context, q string, a ...any) (sql.Result, error) { - s.once.Do(func() { time.Sleep(s.d) }) - return s.mockDB.ExecContext(ctx, q, a...) -} - -// TestRunMigrationsWatchdogSurvivesNonTTY covers the review point on #5997: -// the package's progress writer is io.Discard whenever os.Stderr is not a -// terminal, so wiring the watchdog to it makes the warning silent under -// bd serve, systemd, CI, and any piped invocation — precisely the situations -// where a migration that has been running for twenty minutes needs to be -// visible. On a tty it worked fine, which is why this went unnoticed. -func TestRunMigrationsWatchdogSurvivesNonTTY(t *testing.T) { - if term.IsTerminal(int(os.Stderr.Fd())) { - t.Skip("stderr is a terminal here; this test asserts the non-tty path") - } - - // Put the progress writer in the state a non-tty caller really sees. - origStderr := stderr - stderr = defaultStderr() - defer func() { stderr = origStderr }() - if stderr != io.Discard { - t.Fatalf("precondition: progress writer off a tty = %T, want io.Discard", stderr) - } - - var watchdogBuf bytes.Buffer - origWatchdog := watchdogStderr - watchdogStderr = &watchdogBuf - defer func() { watchdogStderr = origWatchdog }() - - origCounter := issueRowCounter - issueRowCounter = func(context.Context, DBConn) (int64, error) { return 0, nil } - defer func() { issueRowCounter = origCounter }() - - t.Setenv(migrationWatchdogIntervalEnv, "10ms") - - db := &slowMigrationDB{d: 80 * time.Millisecond} - if _, err := runMigrations(context.Background(), db, mainSource, 0, 39, false); err != nil { - t.Fatalf("runMigrations: %v", err) - } - - if got := watchdogBuf.String(); !strings.Contains(got, "WARN") { - t.Errorf("no watchdog WARN reached the non-tty writer: the warning is discarded exactly where operators need it. got %q", got) - } -} diff --git a/internal/storage/schema/migrations/0067_add_versioned_beads_schema.down.sql b/internal/storage/schema/migrations/0067_add_versioned_beads_schema.down.sql deleted file mode 100644 index 83774f91b..000000000 --- a/internal/storage/schema/migrations/0067_add_versioned_beads_schema.down.sql +++ /dev/null @@ -1,40 +0,0 @@ --- Reverse of 0067. Every surface this reverses is still unread and unwritten --- in Phase 1, so this is a plain schema rollback, not a data-loss concern. --- --- Guarded on INFORMATION_SCHEMA the same way the up migration is, so an --- issues-only or partially-applied workspace rolls back as safely as it --- migrated up (0060's down is the precedent). Only migrations/*.up.sql is --- embedded into the CLI fresh bundle, so the PREPARE hazard --- (cli_prepared_ddl.go) never reaches this file through the bundle. It does --- reach a manual rollback: run this through a server connection or dolt --- >= 2.3, because a pre-2.3 `dolt sql -f` silently no-ops the guarded DROP --- COLUMNs (dolthub/dolt#11345) while still dropping the tables, leaving both --- current_revision columns behind. The residue is inert in Phase 1 -- nothing --- reads the columns, both planes keep them so parity holds, and re-running --- the up migration no-ops -- but the rollback is partial while reporting --- success. 0060's down carries the same hazard. -SET @issues_cr_has = ( - SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS - WHERE TABLE_SCHEMA = DATABASE() - AND TABLE_NAME = 'issues' - AND COLUMN_NAME = 'current_revision' -); -SET @sql = IF(@issues_cr_has > 0, - 'ALTER TABLE issues DROP COLUMN current_revision', - 'SELECT 1'); -PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt; - -SET @wisps_cr_has = ( - SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS - WHERE TABLE_SCHEMA = DATABASE() - AND TABLE_NAME = 'wisps' - AND COLUMN_NAME = 'current_revision' -); -SET @sql = IF(@wisps_cr_has > 0, - 'ALTER TABLE wisps DROP COLUMN current_revision', - 'SELECT 1'); -PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt; - -DROP TABLE IF EXISTS store_epoch; - -DROP TABLE IF EXISTS issue_versions; diff --git a/internal/storage/schema/migrations/0067_add_versioned_beads_schema.up.sql b/internal/storage/schema/migrations/0067_add_versioned_beads_schema.up.sql deleted file mode 100644 index 7f26e52df..000000000 --- a/internal/storage/schema/migrations/0067_add_versioned_beads_schema.up.sql +++ /dev/null @@ -1,112 +0,0 @@ --- Migration 0067: Phase 1 schema for versioned beads (be-hs42e / gastownhall/beads#6132, --- this slice: be-hs42e.2 / #6134). --- --- Purely additive: a per-issue version log (issue_versions), a store-global --- epoch singleton (store_epoch), and a fast-path revision column on BOTH --- record planes (issues.current_revision, wisps.current_revision). Nothing --- reads or writes these surfaces yet -- CAS enforcement, dual-write and --- epoch-bump logic land in Phase 2 (#6135) and Phase 3 (#6136). No --- DML/backfill here: current_revision has a safe default for every existing --- row, and both new tables start completely empty. --- --- SEMANTIC ASYMMETRY, DELIBERATE: only the issues column is ever read or --- written. Versioning is an issues-plane contract -- wisps are the ephemeral --- plane and no phase of this epic (2 or 3) will CAS on, bump, or log a wisp --- revision. The wisps column is a SHAPE obligation, not a semantic one: --- issues and wisps share the row shape and the shared scans build one column --- list for both tables (TestSchemaParityIssuesVsWisps in --- internal/storage/dolt enforces strict column-name parity with no exemption --- list), so a column on one plane only is a live drift hazard for --- column-list-shaped code paths regardless of whether anything reads it. --- wisps.row_lock (0054 + ignored/0013) is the precedent: carried on both --- planes, meaningful on one. --- --- The CREATE TABLEs are guarded with IF NOT EXISTS -- ordinary, unwrapped --- MySQL-flavored DDL -- so a designated-migrator replay (schema_migrations --- bookkeeping row missing, e.g. BD_ALLOW_REMOTE_MIGRATE=1 against a clone --- that already has these tables) doesn't die on Error 1105. --- --- The ADD COLUMNs have no equivalent IF NOT EXISTS: MySQL, which Dolt --- follows, has never had that construct for ADD COLUMN (it's a MariaDB-only --- extension; measured on dolt 2.2.3 -- `syntax error at position 33 near --- 'IF'`). Neither does Dolt accept `DROP COLUMN IF EXISTS` or a top-level --- IF. The only conditional DDL forms Dolt does accept are the PREPARE guard --- below and a stored procedure, and a procedure body's inner semicolons --- need a client-side DELIMITER that the CLI batch path requires and the Go --- driver rejects -- the frozen file cannot satisfy both. So these are the --- guarded PREPARE shape 0060 and 0066 already use for exactly this: their --- INFORMATION_SCHEMA probe makes a raw-SQL replay of this whole file a clean --- no-op on an already-migrated store, which is what --- pr4107_corruption_test.go's replay harness (raw .up.sql from 0046 onward --- onto a live store) requires of every migration >= 0046. --- --- A PREPARE'd ADD COLUMN silently vanishes under the CLI-bundle path on a --- pre-2.3 Dolt CLI (dolthub/dolt#11345; see cli_prepared_ddl.go), so the --- fresh bundle gets a direct-DDL override -- cliMigration0067AddVersionedBeadsSchema --- in cli_migrations.go, the same escape hatch 0060/0065/0066 use, guarded by --- TestBundleMigrationsWithPreparedALTERAreOverriddenOrJustified. --- --- wisps is dolt-ignored (clone-local), so this file's wisps ALTER never runs --- on a fresh clone: it ships with the ignored-series twin ignored/0026, the --- mechanism check D of scripts/check-migration-hygiene.sh enforces and --- ignored/0013 (0054's row_lock) and ignored/0020 (0060's storage_class) --- record. -CREATE TABLE IF NOT EXISTS issue_versions ( - issue_id VARCHAR(255) NOT NULL, - revision BIGINT NOT NULL, - epoch INT NOT NULL, - durable_state JSON, - change_actor VARCHAR(255), - change_agent VARCHAR(255), - change_message TEXT, - change_at DATETIME NOT NULL, - removed_at DATETIME, - removed_reason VARCHAR(255), - PRIMARY KEY (issue_id, revision) -); - --- Singleton: exactly one row, id=1, once a later phase creates it. Phase 1 --- leaves the table empty -- the CHECK just pins the shape the lazy-init that --- eventually seeds it (Phase 2/3) must follow. -CREATE TABLE IF NOT EXISTS store_epoch ( - id TINYINT(1) NOT NULL DEFAULT 1, - epoch INT NOT NULL DEFAULT 1, - bumped_at DATETIME, - bumped_reason VARCHAR(255), - PRIMARY KEY (id), - CONSTRAINT ck_store_epoch_singleton CHECK (id = 1) -); - --- issues.current_revision -- the fast-path CAS column, the only one any --- later phase reads or writes. -SET @issues_cr_needs_add = ( - SELECT IF(COUNT(*) = 0, 1, 0) - FROM INFORMATION_SCHEMA.COLUMNS - WHERE TABLE_SCHEMA = DATABASE() - AND TABLE_NAME = 'issues' - AND COLUMN_NAME = 'current_revision' -); -SET @sql = IF(@issues_cr_needs_add = 1, - 'ALTER TABLE issues ADD COLUMN current_revision BIGINT NOT NULL DEFAULT 1', - 'SELECT 1'); -PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt; - --- wisps.current_revision -- shape parity only (see the asymmetry note above). --- Guarded on the wisps table existing as well as the column: older --- workspaces created issues-only, and a clone that never synced the --- clone-local wisp tables must no-op rather than abort the pass (0060 --- precedent). -SET @wisps_cr_needs_add = IF( - (SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES - WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'wisps') > 0 - AND - (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS - WHERE TABLE_SCHEMA = DATABASE() - AND TABLE_NAME = 'wisps' - AND COLUMN_NAME = 'current_revision') = 0, - 1, 0 -); -SET @sql = IF(@wisps_cr_needs_add = 1, - 'ALTER TABLE wisps ADD COLUMN current_revision BIGINT NOT NULL DEFAULT 1', - 'SELECT 1'); -PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt; diff --git a/internal/storage/schema/migrations/0068_add_attribution_status.down.sql b/internal/storage/schema/migrations/0068_add_attribution_status.down.sql deleted file mode 100644 index 8c9942fe4..000000000 --- a/internal/storage/schema/migrations/0068_add_attribution_status.down.sql +++ /dev/null @@ -1,44 +0,0 @@ --- Reverse of 0068 steps 6 and 7 only (attribution_status, and the --- durable_state LONGBLOB retype), undone in reverse order. Steps 1-5 of --- section 16.3 do not exist in this migration (see 0068's up file), so --- there is nothing else to reverse here. --- --- issue_versions is guaranteed empty in any real rollback scenario for the --- same reason the up migration's NOT NULL-no-default is safe (Phase 2 is --- this table's first writer, shipping in the same build as this column), so --- dropping the column loses no data that could exist independent of this --- migration. --- --- Guarded on INFORMATION_SCHEMA the same way 0067's down is, so a --- partially-applied or already-rolled-back workspace rolls back safely. --- Only migrations/*.up.sql is embedded into the CLI fresh bundle, so the --- PREPARE hazard (cli_prepared_ddl.go) never reaches this file. --- --- Step 7 reverse: put durable_state back to the JSON type 0067 created it --- with. Guarded on DATA_TYPE so a store that never took the retype, or was --- already rolled back, no-ops. issue_versions is empty in any real rollback --- scenario (see above), so there are no LONGBLOB bytes for the JSON type to --- reject or renormalize on the way back. -SET @issue_versions_ds_is_longblob = ( - SELECT IF(DATA_TYPE = 'longblob', 1, 0) - FROM INFORMATION_SCHEMA.COLUMNS - WHERE TABLE_SCHEMA = DATABASE() - AND TABLE_NAME = 'issue_versions' - AND COLUMN_NAME = 'durable_state' -); -SET @sql = IF(@issue_versions_ds_is_longblob = 1, - 'ALTER TABLE issue_versions MODIFY COLUMN durable_state JSON', - 'SELECT 1'); -PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt; - --- Step 6 reverse: drop attribution_status. -SET @issue_versions_as_has = ( - SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS - WHERE TABLE_SCHEMA = DATABASE() - AND TABLE_NAME = 'issue_versions' - AND COLUMN_NAME = 'attribution_status' -); -SET @sql = IF(@issue_versions_as_has > 0, - 'ALTER TABLE issue_versions DROP COLUMN attribution_status', - 'SELECT 1'); -PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt; diff --git a/internal/storage/schema/migrations/0068_add_attribution_status.up.sql b/internal/storage/schema/migrations/0068_add_attribution_status.up.sql deleted file mode 100644 index e9367a543..000000000 --- a/internal/storage/schema/migrations/0068_add_attribution_status.up.sql +++ /dev/null @@ -1,106 +0,0 @@ --- Migration 0068: Phase 2 dual-write schema for versioned beads (be-hs42e.3 --- / gastownhall/beads#6135), step 6 of design section 16.3 (be-dt74u --- amendment, be-hs42e.3's design field), plus step 7 -- the durable_state --- LONGBLOB retype added at review, which has its own header further down. --- --- Steps 1-5 of section 16.3 (version_id CHAR(36) UUID PK swap; --- participation_generation BIGINT NULL on issues and its wisps shape-parity --- mirror) are deliberately NOT in this file: be-v33pa's own scope is BASE --- re-seating + attribution_status (section 16.4) only, per its exit --- contract. A later bead may append the remaining steps to this same file -- --- scripts/check-migration-hygiene.sh Check C only protects a migration --- already shipped on the base branch, and this one is still unmerged. --- --- attribution_status (section 16.4, vocabulary aligned to BDP's --- carried-attribution status, gastownhall/bdp#18, merged 2026-09-07): exactly --- two legal values, 'claimed' (the mutation arrived with an actor) and --- 'unknown' (the mutation path had none). 'imported' is provenance, not an --- assertion, and is NOT a status -- it returns as a separate provenance --- marker in the phase that first imports history, for which no writer --- exists yet. A NOT NULL column with no default is safe here because --- issue_versions (created by 0067, a prior migration in this same set) is --- guaranteed empty at this point in any replay -- Phase 2 is this table's --- first and only writer, and its dual-write code lands in the same build as --- this column, so there is no pre-existing row for the NOT NULL constraint --- to reject. See RecordVersionInTx / attributionStatusForActor in --- internal/storage/issueops/version_history.go for what populates it. --- --- Ordinals are local, not wire addresses: issues.current_revision and --- issue_versions.revision are per-store ordinals (two disconnected clones can --- both hold revision 8 for the same issue). The only durable address of a --- version is version_id (steps 1-3 above, not yet landed). The HTTP API's --- RowVersion/Revision is a compare-and-set token, never an address, and Phase --- 3's read surface returns version_id, never the ordinal. --- --- SINGLE WRITER ONLY until steps 1-3 land: with PRIMARY KEY (issue_id, --- revision), two disconnected writers that each mint the same ordinal --- collide on merge and TryAutoResolveMergeConflicts fails the pull for a --- table it does not know, so enabling versioned history is safe only with a --- single writer per store until the UUID version_id primary key lands and --- the ordinal is demoted to an index. Those steps follow as their own PR. --- "Single writer" means one writer at a time per store, not merely one --- clone: MAX(revision)+1 is not a safe allocator for two concurrent --- transactions in one store either (gastownhall/beads#6379, item 4). --- --- Guarded the same way 0067's ADD COLUMNs are (see that file's header for --- the full explanation of why: no MariaDB-only IF NOT EXISTS on Dolt 2.2.3's --- ADD COLUMN, so an INFORMATION_SCHEMA probe + PREPARE is the only --- replay-safe shape) -- makes a raw-SQL replay of this file a clean no-op on --- an already-migrated store. Needs a CLI-bundle direct-DDL override --- (cliMigration0068AddAttributionStatus in cli_migrations.go), the same --- dolthub/dolt#11345 escape hatch 0067 uses, guarded by --- TestBundleMigrationsWithPreparedALTERAreOverriddenOrJustified. --- --- No wisps twin needed: issue_versions has no wisps-side counterpart table --- at all (design section 16.3), so cliSubstituteAssumesWispTables does not --- apply to this migration either. -SET @issue_versions_as_needs_add = ( - SELECT IF(COUNT(*) = 0, 1, 0) - FROM INFORMATION_SCHEMA.COLUMNS - WHERE TABLE_SCHEMA = DATABASE() - AND TABLE_NAME = 'issue_versions' - AND COLUMN_NAME = 'attribution_status' -); -SET @sql = IF(@issue_versions_as_needs_add = 1, - 'ALTER TABLE issue_versions ADD COLUMN attribution_status VARCHAR(20) NOT NULL', - 'SELECT 1'); -PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt; - --- 0068 step 7, added at review (donnabox on gastownhall/beads#6358 item --- 4): retype issue_versions.durable_state from JSON to LONGBLOB. --- --- 0067 created durable_state as a Dolt JSON column. Dolt's JSON type does --- not keep the bytes it is handed -- it parses and renormalizes them, and --- numbers do not survive the round trip (measured on dolt 2.2.3): 1.0 reads --- back as 1, 9007199254740993 (past 2^53) reads back as 9007199254740992, --- 1e300 reads back as 1e+300. A LONGBLOB returns the same bytes it was --- given, byte for byte (same measurement). That breaks the design's --- verbatim-bytes promise (R5.1) and makes any content-derived token --- impossible, because sha256 over the bytes read back would not equal --- sha256 over the bytes written. The column has to preserve bytes, so it --- becomes a LONGBLOB. The writer (RecordVersionInTx in --- internal/storage/issueops/version_history.go) stores the RFC 8785 (JCS) --- canonical form of the marshalled issue and readers parse those bytes, so --- the stored bytes are exactly the bytes a content token hashes. --- --- No data conversion is needed: issue_versions is empty in the Phase 2 era --- (this migration and the table's first writer ship in the same build -- --- see the attribution_status note above), so the MODIFY changes only the --- column type. Guarded on DATA_TYPE the same way 0057 guards its LONGTEXT --- widenings: the retype fires when the column exists and is not yet --- LONGBLOB, and a raw-SQL replay onto an already-migrated store is a clean --- no-op (a missing column makes the probe yield NULL, and the IF takes the --- SELECT 1 branch, the same way 0067's table-exists guards no-op). The --- CLI-bundle override (cliMigration0068AddAttributionStatus) carries the --- direct MODIFY for the same pre-2.3 CLI reason as step 6. -SET @issue_versions_ds_needs_retype = ( - SELECT IF(DATA_TYPE <> 'longblob', 1, 0) - FROM INFORMATION_SCHEMA.COLUMNS - WHERE TABLE_SCHEMA = DATABASE() - AND TABLE_NAME = 'issue_versions' - AND COLUMN_NAME = 'durable_state' -); -SET @sql = IF(@issue_versions_ds_needs_retype = 1, - 'ALTER TABLE issue_versions MODIFY COLUMN durable_state LONGBLOB', - 'SELECT 1'); -PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt; diff --git a/internal/storage/schema/migrations/0069_widen_issue_versions_datetime_precision.down.sql b/internal/storage/schema/migrations/0069_widen_issue_versions_datetime_precision.down.sql deleted file mode 100644 index 9340c7bf2..000000000 --- a/internal/storage/schema/migrations/0069_widen_issue_versions_datetime_precision.down.sql +++ /dev/null @@ -1,38 +0,0 @@ --- Reverse of 0069: narrow issue_versions.change_at and .removed_at back to --- the plain DATETIME (precision 0) type 0067 created them with. Guarded on --- DATETIME_PRECISION so a store that never took the widen, or was already --- rolled back, no-ops. --- --- Unlike the widen, the narrow is lossy: at precision 0 Dolt reads a --- sub-second value back rounded half-up to the whole second (measured on --- dolt 2.3.3: .750000 reads back as the next second), and widening again --- afterwards is not guaranteed to recover the fraction. That is safe under --- the up file's premise -- issue_versions holds no rows while versioned --- history is inactive, which it is in every build so far -- and it is the --- thing to revisit if that premise ever stops holding. --- --- Only migrations/*.up.sql is embedded into the CLI fresh bundle, so the --- PREPARE hazard (cli_prepared_ddl.go) never reaches this file. -SET @issue_versions_change_at_is_widened = ( - SELECT IF(DATETIME_PRECISION = 6, 1, 0) - FROM INFORMATION_SCHEMA.COLUMNS - WHERE TABLE_SCHEMA = DATABASE() - AND TABLE_NAME = 'issue_versions' - AND COLUMN_NAME = 'change_at' -); -SET @sql = IF(@issue_versions_change_at_is_widened = 1, - 'ALTER TABLE issue_versions MODIFY COLUMN change_at DATETIME NOT NULL', - 'SELECT 1'); -PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt; - -SET @issue_versions_removed_at_is_widened = ( - SELECT IF(DATETIME_PRECISION = 6, 1, 0) - FROM INFORMATION_SCHEMA.COLUMNS - WHERE TABLE_SCHEMA = DATABASE() - AND TABLE_NAME = 'issue_versions' - AND COLUMN_NAME = 'removed_at' -); -SET @sql = IF(@issue_versions_removed_at_is_widened = 1, - 'ALTER TABLE issue_versions MODIFY COLUMN removed_at DATETIME', - 'SELECT 1'); -PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt; diff --git a/internal/storage/schema/migrations/0069_widen_issue_versions_datetime_precision.up.sql b/internal/storage/schema/migrations/0069_widen_issue_versions_datetime_precision.up.sql deleted file mode 100644 index 7aab13a12..000000000 --- a/internal/storage/schema/migrations/0069_widen_issue_versions_datetime_precision.up.sql +++ /dev/null @@ -1,91 +0,0 @@ --- Migration 0069: widen issue_versions.change_at and .removed_at from --- DATETIME (precision 0) to DATETIME(6) (microsecond) -- be-hs42e.8 / --- gastownhall/beads#6132. --- --- This was drafted as a step 8 appended to 0068 while 0068 was still --- unmerged. 0068 then shipped on main (gastownhall/beads#6650) with steps 6 --- and 7 only, which froze it (scripts/check-migration-hygiene.sh check C): --- every clone built since has applied it and recorded its content hash, so --- the widen needs its own slot. --- --- 0067 created both columns as plain DATETIME. Dolt's datetime(0) does not --- truncate sub-second input -- it ROUNDS half-up (pinned for a different --- column by testAuditImportCommentSubSecond in --- backend/conformance/audit_labels-comments-events.go, reconfirmed directly --- against issue_versions by --- TestMigration0069ChangeAtSurvivesSubSecondPrecisionThroughDoltCLI in --- internal/storage/schema). For a column whose entire job is placing --- history in order, precision 0 has two consequences: a sub-second write --- can read back rounded into the *next* second, and two writes less than a --- second apart in real time can round onto the identical stored value and --- become indistinguishable by change_at. --- --- removed_at widens in lockstep even though no Go code writes it yet (a --- repo-wide grep for removed_at/RemovedAt turns up only schema: 0067's --- CREATE TABLE and the CLI-bundle mirror of it in cli_migrations.go). It is --- change_at's paired lifecycle column on the same row of the same table, so --- leaving it at precision 0 while change_at widens would be exactly the --- asymmetry this bead's own title warns against ("before any store --- accumulates real history"). --- --- No data conversion is needed. Widening precision is lossless -- every --- whole-second value is exactly representable at precision 6 -- so each --- MODIFY changes only the column type, whatever the table holds. In --- practice it holds nothing yet: RecordVersionInTx --- (internal/storage/issueops/version_history.go), the table's only writer, --- mints only while versioned history is activated, and no build activates --- it. --- --- Guarded on DATETIME_PRECISION the same way 0068 guards on --- COLUMN_NAME/DATA_TYPE, so a raw-SQL replay of this file onto an --- already-widened store is a clean no-op (internal/storage/dolt's pr4107 --- replay harness requires this of every migration >= 0046), and a missing --- table makes the probe yield NULL, which takes the SELECT 1 branch. Needs --- a CLI-bundle direct-DDL override --- (cliMigration0069WidenIssueVersionsDatetimePrecision in --- cli_migrations.go), the same dolthub/dolt#11345 escape hatch 0067 and 0068 --- use, guarded by TestBundleMigrationsWithPreparedALTERAreOverriddenOrJustified. --- --- No wisps twin needed: issue_versions has no wisps-side counterpart table --- at all (design section 16.3), so cliSubstituteAssumesWispTables does not --- apply to this migration either. --- --- DOLT PLANE, stated here because 0067 created these tables without saying --- so and a reader has to know before writing to them: issue_versions and --- store_epoch REPLICATE. They are ordinary synced tables, deliberately NOT --- registered dolt_ignore'd the way 0064 registers the events journal's --- clone-local pair. A bead's version history is part of the bead and has to --- travel with it -- and the single-writer constraint in 0068's header is a --- statement about what happens when two clones MERGE these tables, which --- only means anything for a table that replicates at all. --- --- The obligation that buys: every operation that mints must STAGE what it --- minted (issue_versions, store_epoch, and issues for the current_revision --- advance), or the rows sit dirty in the working set -- outside the --- DOLT_COMMIT of the very mutation they describe, unreplicated, and able to --- trip DirtyTablesError on the next migration. The staging paths do this --- from one list, issueops.VersionedHistoryStagedTables, keyed off whether --- versioned history is active so no call site has to remember it. -SET @issue_versions_change_at_needs_widen = ( - SELECT IF(DATETIME_PRECISION <> 6, 1, 0) - FROM INFORMATION_SCHEMA.COLUMNS - WHERE TABLE_SCHEMA = DATABASE() - AND TABLE_NAME = 'issue_versions' - AND COLUMN_NAME = 'change_at' -); -SET @sql = IF(@issue_versions_change_at_needs_widen = 1, - 'ALTER TABLE issue_versions MODIFY COLUMN change_at DATETIME(6) NOT NULL', - 'SELECT 1'); -PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt; - -SET @issue_versions_removed_at_needs_widen = ( - SELECT IF(DATETIME_PRECISION <> 6, 1, 0) - FROM INFORMATION_SCHEMA.COLUMNS - WHERE TABLE_SCHEMA = DATABASE() - AND TABLE_NAME = 'issue_versions' - AND COLUMN_NAME = 'removed_at' -); -SET @sql = IF(@issue_versions_removed_at_needs_widen = 1, - 'ALTER TABLE issue_versions MODIFY COLUMN removed_at DATETIME(6)', - 'SELECT 1'); -PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt; diff --git a/internal/storage/schema/migrations/0070_add_participation_generation.down.sql b/internal/storage/schema/migrations/0070_add_participation_generation.down.sql deleted file mode 100644 index e37d545f9..000000000 --- a/internal/storage/schema/migrations/0070_add_participation_generation.down.sql +++ /dev/null @@ -1,34 +0,0 @@ --- Reverse of migration 0070 (participation_generation on issues and wisps, --- design section 16.3 steps 4-5, be-dt74u amendment, be-h89oq), undone in --- reverse order. --- --- issue_versions is guaranteed empty in any real rollback scenario for the --- same reason 0068's NOT NULL-no-default is safe (Phase 2 is that table's --- first writer), but this migration touches issues/wisps, not --- issue_versions -- participation_generation is dropped unconditionally on --- both planes if present, guarded on INFORMATION_SCHEMA the same way 0068's --- down is, so a partially-applied or already-rolled-back workspace rolls --- back safely. Only migrations/*.up.sql is embedded into the CLI fresh --- bundle, so the PREPARE hazard (cli_prepared_ddl.go) never reaches this --- file. -SET @issues_pg_has = ( - SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS - WHERE TABLE_SCHEMA = DATABASE() - AND TABLE_NAME = 'issues' - AND COLUMN_NAME = 'participation_generation' -); -SET @sql = IF(@issues_pg_has > 0, - 'ALTER TABLE issues DROP COLUMN participation_generation', - 'SELECT 1'); -PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt; - -SET @wisps_pg_has = ( - SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS - WHERE TABLE_SCHEMA = DATABASE() - AND TABLE_NAME = 'wisps' - AND COLUMN_NAME = 'participation_generation' -); -SET @sql = IF(@wisps_pg_has > 0, - 'ALTER TABLE wisps DROP COLUMN participation_generation', - 'SELECT 1'); -PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt; diff --git a/internal/storage/schema/migrations/0070_add_participation_generation.up.sql b/internal/storage/schema/migrations/0070_add_participation_generation.up.sql deleted file mode 100644 index 8cf0fb392..000000000 --- a/internal/storage/schema/migrations/0070_add_participation_generation.up.sql +++ /dev/null @@ -1,69 +0,0 @@ --- Migration 0070: participation_generation on issues and wisps (design --- section 16.3 steps 4-5, be-dt74u amendment, be-h89oq). --- --- Originally written as steps 4-5 appended to 0068_add_attribution_status --- (be-h89oq's lineage), while that file was still unmerged. be-v33pa's own --- attribution_status-only version of 0068 shipped to main first (2026-09-28 --- 15:46Z), freezing that file's content -- scripts/check-migration-hygiene.sh --- Check C protects any migration already shipped on the base branch, so --- these steps move here instead of reconciling by editing 0068 (mayor ruling --- 2026-09-29 on tracker be-waare). Numbered 0070: 0068 and 0069 are already --- taken on origin/main, the latter by an unrelated migration --- (widen_issue_versions_datetime_precision) that landed independently while --- this lineage was in flight. --- --- participation_generation BIGINT NULL on issues, mirrored inertly on wisps. --- NULL means legacy-unmigrated; any non-NULL is a positive declaration --- sourced from store_epoch.epoch. RecordVersionInTx's write fence (design --- §16.2b, in internal/storage/issueops/version_history.go) reads this column --- to decide whether an update-shaped mutation against a legacy record mints --- a version row at all -- a create-shaped mutation stamps a fresh value --- instead. No backfill: NULL is the correct default for every existing row, --- on both planes, so a plain ADD COLUMN with no DEFAULT is exactly what's --- wanted. --- --- This migration must run before versioned history is turned on. The fence --- reads this column on every flag-on mint of an issues-plane row, and on a --- store without it that read fails, so the mutation fails with it rather --- than skipping. With the flag off (the default) the seam returns before --- the read, so a store below this migration writes exactly as before. --- --- Guarded the same way 0067/0068's ADD COLUMNs are (INFORMATION_SCHEMA probe --- + PREPARE, since Dolt 2.2.3's ADD COLUMN has no MariaDB-only IF NOT --- EXISTS), making a raw-SQL replay of this file a clean no-op on an --- already-migrated store. Needs a CLI-bundle direct-DDL override --- (cliMigration0070AddParticipationGeneration in cli_migrations.go), the same --- dolthub/dolt#11345 escape hatch 0067/0068 use. -SET @issues_pg_needs_add = ( - SELECT IF(COUNT(*) = 0, 1, 0) - FROM INFORMATION_SCHEMA.COLUMNS - WHERE TABLE_SCHEMA = DATABASE() - AND TABLE_NAME = 'issues' - AND COLUMN_NAME = 'participation_generation' -); -SET @sql = IF(@issues_pg_needs_add = 1, - 'ALTER TABLE issues ADD COLUMN participation_generation BIGINT NULL', - 'SELECT 1'); -PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt; - --- wisps.participation_generation -- shape parity only, never read or written --- on this plane (design §16.2a). Guarded on the wisps table existing as well --- as the column, mirroring 0067's wisps.current_revision guard exactly (see --- that migration's header): wisps is dolt-ignored/clone-local, so a clone --- that never synced the local wisp tables must no-op rather than abort. The --- clone-local twin for a workspace that never synced the wisps table at all --- is ignored/0028_add_wisps_participation_generation.up.sql. -SET @wisps_pg_needs_add = IF( - (SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES - WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'wisps') > 0 - AND - (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS - WHERE TABLE_SCHEMA = DATABASE() - AND TABLE_NAME = 'wisps' - AND COLUMN_NAME = 'participation_generation') = 0, - 1, 0 -); -SET @sql = IF(@wisps_pg_needs_add = 1, - 'ALTER TABLE wisps ADD COLUMN participation_generation BIGINT NULL', - 'SELECT 1'); -PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt; diff --git a/internal/storage/schema/migrations/0071_add_removed_restriction.down.sql b/internal/storage/schema/migrations/0071_add_removed_restriction.down.sql deleted file mode 100644 index 5c3f8bde6..000000000 --- a/internal/storage/schema/migrations/0071_add_removed_restriction.down.sql +++ /dev/null @@ -1,16 +0,0 @@ --- Reverse of 0071: drop issue_versions.removed_restriction. --- --- Guarded on INFORMATION_SCHEMA the same way 0067/0068's downs are, so a --- partially-applied or already-rolled-back workspace rolls back safely. --- Only migrations/*.up.sql is embedded into the CLI fresh bundle, so the --- PREPARE hazard (cli_prepared_ddl.go) never reaches this file. -SET @issue_versions_rr_has = ( - SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS - WHERE TABLE_SCHEMA = DATABASE() - AND TABLE_NAME = 'issue_versions' - AND COLUMN_NAME = 'removed_restriction' -); -SET @sql = IF(@issue_versions_rr_has > 0, - 'ALTER TABLE issue_versions DROP COLUMN removed_restriction', - 'SELECT 1'); -PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt; diff --git a/internal/storage/schema/migrations/0071_add_removed_restriction.up.sql b/internal/storage/schema/migrations/0071_add_removed_restriction.up.sql deleted file mode 100644 index e47123ed2..000000000 --- a/internal/storage/schema/migrations/0071_add_removed_restriction.up.sql +++ /dev/null @@ -1,49 +0,0 @@ --- Migration 0071: R7.1 as-of read (gastownhall/beads#5898 revision 9, --- gastownhall/beads#6136); conformance suite: backend/conformance/ --- versioned_read_contract.go. --- --- issue_versions already carries removed_at DATETIME and removed_reason --- VARCHAR(255) (migration 0067), virgin and unused until now: R7.1's --- AsOfReadInTx (internal/storage/issueops/asof_read.go) reads them as the --- durable "this version row was removed" marker on the resolved row, and --- refuses a served answer whenever removed_at is set (R8: never silently --- substitute a neighboring/surviving/current version). --- --- removed_restriction VARCHAR(30) is the one column this migration adds: the --- categorical restriction (gone_retention / gone_erasure / --- gone_reorganization / unknown) that removed_at's presence alone cannot --- carry. Nullable and NULL for every existing row -- Live is the absence of --- a value, never a stored one -- and populated only alongside removed_at, --- never independently. 30 chars comfortably fits the longest local --- constant (internal/storage/issueops/asof_read.go's AsOfRestriction, --- "gone_reorganization", 19 bytes) with headroom, matching change_agent's --- own no-tight-fit precedent elsewhere in this table. --- --- Forward-compatible with R17 (not yet implemented on this branch): R17's --- eventual real hold/remove/erase primitives will be the production WRITER --- of these three columns; R7.1 only reads them here. No future collision -- --- this migration only adds the one column R17 will also need, it does not --- claim the writing role. --- --- Guarded the same way 0067/0068 guard their ADD COLUMNs (see 0067's header --- for the full explanation): no MariaDB-only IF NOT EXISTS on Dolt 2.2.3's --- ADD COLUMN, so an INFORMATION_SCHEMA probe + PREPARE is the only --- replay-safe shape. Needs a CLI-bundle direct-DDL override --- (cliMigration0071AddRemovedRestriction in cli_migrations.go), the same --- dolthub/dolt#11345 escape hatch 0067/0068 use, guarded by --- TestBundleMigrationsWithPreparedALTERAreOverriddenOrJustified. --- --- No wisps twin: issue_versions has no wisps-side counterpart table (design --- section 16.3), so cliSubstituteAssumesWispTables does not apply here --- either, matching 0068. -SET @issue_versions_rr_needs_add = ( - SELECT IF(COUNT(*) = 0, 1, 0) - FROM INFORMATION_SCHEMA.COLUMNS - WHERE TABLE_SCHEMA = DATABASE() - AND TABLE_NAME = 'issue_versions' - AND COLUMN_NAME = 'removed_restriction' -); -SET @sql = IF(@issue_versions_rr_needs_add = 1, - 'ALTER TABLE issue_versions ADD COLUMN removed_restriction VARCHAR(30)', - 'SELECT 1'); -PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt; diff --git a/internal/storage/schema/migrations/0072_add_epoch_minted_addresses.down.sql b/internal/storage/schema/migrations/0072_add_epoch_minted_addresses.down.sql deleted file mode 100644 index af8d83eea..000000000 --- a/internal/storage/schema/migrations/0072_add_epoch_minted_addresses.down.sql +++ /dev/null @@ -1 +0,0 @@ -DROP TABLE IF EXISTS epoch_minted_addresses; diff --git a/internal/storage/schema/migrations/0072_add_epoch_minted_addresses.up.sql b/internal/storage/schema/migrations/0072_add_epoch_minted_addresses.up.sql deleted file mode 100644 index ec946cf8e..000000000 --- a/internal/storage/schema/migrations/0072_add_epoch_minted_addresses.up.sql +++ /dev/null @@ -1,64 +0,0 @@ --- epoch_minted_addresses: durable storage for R20 epoch-transition --- enforcement (gastownhall/beads#5898 revision 9, --- #6136). Independent of store_epoch (0067, #6135) -- that table holds only --- the store-wide singleton epoch counter (the current generation number); --- this table tracks the individual addresses minted under each generation, --- which store_epoch has no room to hold. It is also independent of --- expected_revision_records (R16, #6133) and any RetentionFixture/R17 --- table: R20 epoch reasoning is evaluated on its own, without consulting --- retention/erasure state, and this migration adds no R17 resolve, remove, --- hold, force-remove, erase, or mint logic. --- --- One row per minted address, not one row per id: MintUnderEpoch is --- deterministic in (store_id, id, epoch) -- see epochAddress() in --- internal/storage/issueops/epoch_cas.go -- so minting the same id again --- under the same epoch reproduces the same address and finds the same --- row, while a later mint of that id under a bumped epoch produces a --- DIFFERENT address and adds a new row alongside the old one. Old rows are --- kept: StillServes and Resolve answer for any address from its own row, --- whatever epoch it was minted in. --- --- Survival is RECORDED on the row, never inferred from other rows --- (gastownhall/beads#6664, bee-ghosttrack review 5360880888, Major 1). --- gone_at_epoch is NULL while the store serves the address and otherwise the --- epoch the store lost it in; it is set once and never cleared, so a lost --- address stays lost. carried_from is NULL for a fresh mint, and for a row a --- token-scheme change carried to a new epoch it is the address of the --- lineage's ROOT (never the previous carry), so every member of a lineage is --- one indexed lookup from the newest. The index on carried_from serves exactly --- the two lookups that follow a lineage: the newest carry of a root, and every --- carry of a root when the lineage is lost. The CHECK is a database-level --- tripwire against a stale or rewound epoch being written into a loss marker; --- it is >=, not >, because a carry row is born at the new epoch and can be lost --- in that same epoch. --- --- address is the PRIMARY KEY: it is the deterministic token the fixture's --- StillServes/Resolve/CurrentAddressFor hooks look addresses up by, so it --- must be unique and indexed; it is never recomputed from the other columns --- at read time; it is written once, whatever the epoch was at mint time. --- --- store_id and minted_id are VARCHAR(255) for parity with this schema's --- other identifier columns. minted_epoch is INT to match store_epoch.epoch's --- own type. minted_at is DATETIME, not a nanosecond-precision integer: unlike --- expected_revision_records.change_at_nanos (R16, 0069 on that slice), no --- R20 contract case compares minted_at for exact round-tripping -- it exists --- for observability only, so it follows store_epoch.bumped_at's own --- DATETIME precedent rather than needing BIGINT. --- --- Plain, unguarded CREATE TABLE IF NOT EXISTS: a brand-new main-plane table --- with no ALTER, no PREPARE, and no dolt-ignored (clone-local) table --- involved, so none of cli_migrations.go's CLI-bundle overrides, an --- ignored/ twin, or a nondeterminism-allowlist entry apply --- (scripts/check-migration-hygiene.sh checks B-E). -CREATE TABLE IF NOT EXISTS epoch_minted_addresses ( - address VARCHAR(255) NOT NULL, - store_id VARCHAR(255) NOT NULL, - minted_id VARCHAR(255) NOT NULL, - minted_epoch INT NOT NULL, - minted_at DATETIME NOT NULL, - carried_from VARCHAR(255) NULL, - gone_at_epoch INT NULL, - PRIMARY KEY (address), - INDEX idx_epoch_minted_addresses_carried_from (carried_from), - CONSTRAINT ck_epoch_minted_addresses_gone_not_before_mint CHECK (gone_at_epoch IS NULL OR gone_at_epoch >= minted_epoch) -); diff --git a/internal/storage/schema/migrations/README.md b/internal/storage/schema/migrations/README.md index c35e61b40..b71532594 100644 --- a/internal/storage/schema/migrations/README.md +++ b/internal/storage/schema/migrations/README.md @@ -62,19 +62,6 @@ duplicate primary key given: [w2,external:e1] Normalize *after* dropping the generated column and its key, not before. (0058 normalizes before, and reaches the abort on any store whose rows collide.) -**Restore any session variable you set, in the script that set it.** Six -migrations here clear `FOREIGN_KEY_CHECKS` and restore it (0041, 0043, 0047, -0050, 0053, 0058); keep that shape. Since wy-s8ytnw the steady-state open in -`uow.openAndInitSchema` hands the pool that ran the migration straight to the -provider for live queries, so a system session variable still changed when a -script reports success leaks into ordinary queries. Pairing the SETs inside one -script is what makes that safe — `initSchemaAttempt` pins one session per -attempt, so both statements land on the same session, and an attempt that dies -between them returns its connection to the pool, which is why the restore -cannot be the caller's job. User variables (`SET @sql`, `SET @needs_add`) are -exempt: nothing outside these scripts reads one, and each assigns before it -reads. - **The `dolt` CLI is not a safe harness for guarded migrations, twice over.** `dolt sql -f`/`-q`/piped-stdin silently no-ops `PREPARE`/`EXECUTE`-driven `ALTER TABLE` — every guarded migration here uses exactly that shape — and diff --git a/internal/storage/schema/migrations/ignored/0027_add_wisps_current_revision.up.sql b/internal/storage/schema/migrations/ignored/0027_add_wisps_current_revision.up.sql deleted file mode 100644 index 2468f789f..000000000 --- a/internal/storage/schema/migrations/ignored/0027_add_wisps_current_revision.up.sql +++ /dev/null @@ -1,46 +0,0 @@ --- Ignored migration 0027: ensure wisps.current_revision exists on every clone --- (be-hs42e.2 / gastownhall/beads#6134). --- --- Numbered 0026 on main until the v1.3.0 forward-port merge, which brought in --- release/1.3.0's own 0026 (dep_rekey_dedup_marker). That one is in the --- shipped v1.3.0 tag, so field stores have already recorded it at ordinal 26 --- and it can never move; this one had not shipped in any release, so it moved --- here. Clones that applied it at 26 from a main build re-run it at 27, which --- the guard below makes a no-op, and the pending-27 pass is also what keeps --- 0026's forced re-key pass reaching them. --- --- Synced migration 0067 adds current_revision to issues and wisps — but wisps --- is dolt-ignored (migration 0019), so its schema is clone-local, and a --- workspace that bootstraps or re-clones from a remote whose --- schema_migrations cursor is already >= 0067 adopts the cursor without ever --- executing 0067. Its wisps table (materialized by ignored/0001, which --- predates current_revision) would then permanently lack the column, and the --- shared issues/wisps column lists would drift by one column between the --- fresh-clone door and the fresh-init door. Same mechanism, same shape, same --- fix as ignored/0013 (wisps.row_lock, wy-pt82l) and ignored/0020 --- (wisps.storage_class, bd-hs7fa). --- --- Carried here for SHAPE only. Nothing in Phase 2 (#6135) or Phase 3 (#6136) --- reads or writes the wisps column: versioning is an issues-plane contract --- and wisps are the ephemeral plane. wisps.row_lock is the precedent for a --- column that exists on both planes and means something on one — see 0067's --- header for the full asymmetry note. --- --- The guard makes this a no-op on in-place-upgraded workspaces where synced --- 0067 already added the column, and on workspaces with no local wisps table --- yet. Definition mirrors 0067 exactly: BIGINT NOT NULL DEFAULT 1, so every --- pre-existing row reads as revision 1 with no backfill. -SET @needs_add = IF( - (SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES - WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'wisps') > 0 - AND - (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS - WHERE TABLE_SCHEMA = DATABASE() - AND TABLE_NAME = 'wisps' - AND COLUMN_NAME = 'current_revision') = 0, - 1, 0 -); -SET @sql = IF(@needs_add = 1, - 'ALTER TABLE wisps ADD COLUMN current_revision BIGINT NOT NULL DEFAULT 1', - 'SELECT 1'); -PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt; diff --git a/internal/storage/schema/migrations/ignored/0028_add_wisps_participation_generation.up.sql b/internal/storage/schema/migrations/ignored/0028_add_wisps_participation_generation.up.sql deleted file mode 100644 index 4ed7a3c58..000000000 --- a/internal/storage/schema/migrations/ignored/0028_add_wisps_participation_generation.up.sql +++ /dev/null @@ -1,36 +0,0 @@ --- Ignored migration 0028: ensure wisps.participation_generation exists on --- every clone (design be-hs42e.3 §16.2-16.3 / be-h89oq / be-x8tpz). --- --- Synced migration 0070 adds participation_generation to issues and wisps -- --- but wisps is dolt-ignored (migration 0019), so its schema is clone-local, --- and a workspace that bootstraps or re-clones from a remote whose --- schema_migrations cursor is already >= 0070 adopts the cursor without ever --- executing 0070. Its wisps table would then permanently lack the column, --- and the shared issues/wisps column lists would drift by one column between --- the fresh-clone door and the fresh-init door. Same mechanism, same shape, --- same fix as ignored/0013 (wisps.row_lock, wy-pt82l), ignored/0020 --- (wisps.storage_class, wy-98eh5), and ignored/0027 (wisps.current_revision). --- --- Carried here for SHAPE only, exactly like 0070's own issues/wisps split: --- nothing reads or writes this column on the wisps plane (design §16.2a). --- wisps.row_lock and wisps.current_revision are the precedent for a column --- that exists on both planes and means something on only one. --- --- The guard makes this a no-op on in-place-upgraded workspaces where synced --- 0070 already added the column, and on workspaces with no local wisps table --- yet. Definition mirrors 0070's own wisps.participation_generation guard --- exactly: BIGINT NULL, no default, no backfill. -SET @needs_add = IF( - (SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES - WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'wisps') > 0 - AND - (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS - WHERE TABLE_SCHEMA = DATABASE() - AND TABLE_NAME = 'wisps' - AND COLUMN_NAME = 'participation_generation') = 0, - 1, 0 -); -SET @sql = IF(@needs_add = 1, - 'ALTER TABLE wisps ADD COLUMN participation_generation BIGINT NULL', - 'SELECT 1'); -PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt; diff --git a/internal/storage/schema/remote_migrate_gate.go b/internal/storage/schema/remote_migrate_gate.go index 0d6075dd6..262edf8dd 100644 --- a/internal/storage/schema/remote_migrate_gate.go +++ b/internal/storage/schema/remote_migrate_gate.go @@ -132,7 +132,7 @@ type RemoteMigrateGateError struct { // of work provider, which reaches its database only through the dbproxy. // It changes WHERE the data-behind remedy can be run, not what it is: in // that mode `bd dolt pull` is refused at the front door - // (proxy.dolt_pull.unsupported, cmd/bd/capability_registry.go), along with + // (proxy.dolt_pull.unsupported, cmd/bd/proxy_capability.go), along with // `bd dolt push`, `bd migrate` and `bd conflicts`. Printing the bare // command there would hand the operator a remedy their own binary rejects, // so the surfaces that name it say to run it where the database lives. @@ -382,11 +382,11 @@ func (e *RemoteMigrateGateError) dataBehindBody() string { if e.Proxied { // Unlike the pull, this consent step is NOT refused here, and // saying otherwise would be wrong in the unsafe direction. The - // proxied capability registry keys on the command path - // (cmd/bd/capability_registry.go): it refuses the bare `migrate` - // verb and its hooks/issues/sync children, but permits - // `migrate schema`, so the two-word path passes - // validateProxyRegistryBeforeProvider to a proxied arm that + // proxied refusal table keys on the command path + // (cmd/bd.proxyMaintenanceRefusals): it registers the bare + // `migrate` verb and its hooks/issues/sync children, but has no + // `migrate schema` row, so the two-word path falls through + // validateProxyMaintenanceBeforeProvider to a proxied arm that // reports the migration the provider open just applied under this // verb's consent. A reader told it is blocked here tries it to // confirm — and forceOrEnvConsent is honored BEFORE this stop is @@ -606,8 +606,7 @@ func (e *RemoteMigrateGateError) AgentDirective() string { } } -// GateOption is one conditional remediation path for the remote-migrate gate -// (and the migration-consent gate, MigrateConsentError.Options). +// GateOption is one conditional remediation path for the remote-migrate gate. // It is intentionally conditional (When) and carries its Risk, so an agent // cannot treat any single command as the unconditional fix. type GateOption struct { @@ -682,8 +681,8 @@ func (e *RemoteMigrateGateError) Options() []GateOption { consentRisk := "co-resident clients still on an older bd will refuse this database until upgraded; running it before the pull completes is the wedge this stop prevents. Its --force consents to migrating a remote-backed shared store (the bare verb's consent is read only with no remote configured); it does not make migrating while behind safe" if e.Proxied { // Unlike the pull, this command is NOT refused here: the - // capability registry permits the two-word "migrate schema" - // path, so it gets through and the provider open applies the + // refusal table has no "migrate schema" row, so the two-word + // path falls through and the provider open applies the // migration under its consent. The precondition is therefore // temporal, not locational — and it is the only thing between // an agent on this topology and the wedge, because forced diff --git a/internal/storage/schema/schema.go b/internal/storage/schema/schema.go index 2c95d0e9f..659b82b03 100644 --- a/internal/storage/schema/schema.go +++ b/internal/storage/schema/schema.go @@ -36,19 +36,6 @@ func defaultStderr() io.Writer { return io.Discard } -// watchdogStderr is where the migration watchdog's WARN lines go. Unlike -// stderr it is deliberately NOT terminal-gated: bd serve, systemd, CI and any -// piped invocation are all non-tty, and they are exactly the cases where a -// migration that has been running for twenty minutes needs to be visible. -// Routing it through stderr would make the warning io.Discard precisely there. -// -// This matches how this package already emits operator warnings that must not -// be swallowed (remote_migrate_gate.go, smart_remote_migrate_gate.go, and the -// skew notices in this file all write to os.Stderr directly); stderr remains -// for the routine per-migration progress lines it was introduced for. -// Overridable in tests. -var watchdogStderr io.Writer = os.Stderr - const largeRigThreshold = 10000 // issueRowCounter returns the current issues-table row count, or an error if @@ -73,90 +60,6 @@ func emitLargeRigNotice(out io.Writer, count int64, err error) { fmt.Fprintf(out, "Large rig detected (%d issues). This migration may take up to 90 seconds; do not interrupt.\n", count) } -// migrationWatchdogInterval is the default interval between soft watchdog -// warnings for a single long-running migration. -const migrationWatchdogInterval = 5 * time.Minute - -// migrationWatchdogIntervalEnv overrides migrationWatchdogInterval. -const migrationWatchdogIntervalEnv = "BEADS_MIGRATION_WATCHDOG_INTERVAL" - -// migrationWatchdogIntervalDuration returns the configured watchdog interval. -// BEADS_MIGRATION_WATCHDOG_INTERVAL overrides the compiled-in default. Valid -// time.ParseDuration strings ("10m", "90s") and bare numbers treated as -// seconds ("90") are both accepted; unset, empty, unparsable, or non-positive -// values fall back to migrationWatchdogInterval. This is the full contract of -// internal/storage/dolt/store.go's parseTimeout, bare-seconds included — -// mirrored rather than reused because that function is unexported in another -// package. -func migrationWatchdogIntervalDuration() time.Duration { - raw := strings.TrimSpace(os.Getenv(migrationWatchdogIntervalEnv)) - if raw == "" { - return migrationWatchdogInterval - } - if d, err := time.ParseDuration(raw); err == nil { - if d > 0 { - return d - } - return migrationWatchdogInterval - } - // Bare numbers mean seconds, matching parseTimeout. Without this an - // operator setting the value to 90 gets 5m and no diagnostic. - if d, err := time.ParseDuration(raw + "s"); err == nil && d > 0 { - return d - } - return migrationWatchdogInterval -} - -// runMigrationWithWatchdog runs fn — the same call runMigrations already -// makes to execMigrationBody — while a background timer watches elapsed -// time. If fn is still running after interval, it logs a WARN line to out -// naming the migration's version, name, and elapsed time, and repeats every -// additional interval for as long as fn keeps running, so an operator -// watching logs can tell "still working" from "stopped emitting anything". -// -// This is observability only, never a circuit breaker: fn receives exactly -// the ctx this function received (no wrapping timeout/cancel), and fn's -// returned error is passed through unchanged. Migrations are allowed to run -// arbitrarily long by design — store.go's own initSchema comment cites -// migration 0047's full-table is_blocked recompute as a real example — and -// no confirmed production hang exists today. See bd show be-m65rs for the -// full rationale before turning this into a hard abort. -func runMigrationWithWatchdog(ctx context.Context, out io.Writer, version int, name string, interval time.Duration, fn func(ctx context.Context) error) error { - start := time.Now() - done := make(chan struct{}) - var wg sync.WaitGroup - wg.Add(1) - go func() { - defer wg.Done() - ticker := time.NewTicker(interval) - defer ticker.Stop() - for { - select { - case <-done: - return - case <-ticker.C: - fmt.Fprintf(out, "WARN: migration %04d (%s) still running after %s — watchdog check every %s, this is not an error\n", - version, name, time.Since(start).Round(time.Millisecond), interval) - } - } - }() - - // Deferred so the warner is stopped on every exit path, including a panic - // out of fn — Dolt's embedded engine can panic out of DDL, and a caller - // that recovers rather than dying (internal/httpapi/server.go's middleware - // recovers everything except http.ErrAbortHandler and keeps serving) would - // otherwise orphan the ticker goroutine, which goes on writing "still - // running" to the un-gated watchdogStderr for a migration that is no longer - // running. A straight-line close/wait after fn returns leaks in exactly - // that case. - defer func() { - close(done) - wg.Wait() - }() - - return fn(ctx) -} - // humanMigrationName turns "0033_add_date_indexes.up.sql" into // "add_date_indexes" for the progress line. func humanMigrationName(filename string) string { @@ -269,8 +172,8 @@ type SchemaBehindError struct { } func (e *SchemaBehindError) Error() string { - return fmt.Sprintf("schema version mismatch: database is at v%d, binary expects v%d; bd does not migrate a database without explicit consent — run `bd migrate schema` in that workspace to migrate, keep using a bd release that matches schema v%d, or set BD_IGNORE_SCHEMA_SKEW=1 to read anyway (queries touching newer schema may fail)", - e.DBVersion, e.BinaryVersion, e.DBVersion) + return fmt.Sprintf("schema version mismatch: database is at v%d, binary expects v%d, and the read-only open cannot migrate it; run any bd write command in that workspace to migrate, or set BD_IGNORE_SCHEMA_SKEW=1 to read anyway (queries touching newer schema may fail)", + e.DBVersion, e.BinaryVersion) } // IsSchemaBehindError reports whether err (or any error it wraps) is a @@ -288,8 +191,10 @@ func IsSchemaBehindError(err error) bool { // to a warning, mirroring forward drift. A fresh DB (version 0) is reported // as behind too: it has no readable schema at all. func CheckBehindDrift(ctx context.Context, db *sql.DB) error { - currentVersion, err := CurrentVersion(ctx, db) - if err != nil { + var currentVersion int + if err := db.QueryRowContext(ctx, + "SELECT COALESCE(MAX(version), 0) FROM schema_migrations", + ).Scan(¤tVersion); err != nil { return fmt.Errorf("schema behind-drift check: %w", err) } if currentVersion >= LatestVersion() { @@ -391,8 +296,8 @@ type schemaSentinelColumn struct { // — just without the timestamp damage. // (ii) The migration that creates the column AND the migration that // creates the table carrying it must both be ABOVE the floor, so the - // replay can still heal both shapes the column probe collapses - // (sentinelColumnExists): table absent entirely, and table present + // replay can still heal both shapes the single INFORMATION_SCHEMA + // COUNT(*) probe collapses: table absent entirely, and table present // but column-less. A floor above the table's creator would strand // the table-absent shape in a permanent re-migration loop. // @@ -441,8 +346,9 @@ var ( // A historical ignored-v16 ordinal collision can leave the local // leases table present but without the column frozen 0016 adds; a // database materialized out of band can be missing the leases table - // altogether. The column probe reads both shapes as absent and the - // replay heals both: 0012 re-creates the table, 0016 adds the column. + // altogether. The single COLUMNS probe reads both shapes as absent and + // the replay heals both: 0012 re-creates the table, 0016 adds the + // column. // // The floor of 11 is what keeps that heal from also being a data loss. // Every released tree (v1.1.0, v1.1.2, v1.2.x) tops the ignored series @@ -471,37 +377,6 @@ var ( // pollutes dolt_status and feeds the dirty-table migration gates. MigrateUp // re-asserts the full set idempotently at the top of every write-mode open. var doltIgnorePatterns = []string{ - // Table-rebuild intermediates: the ignored series stages every rebuild - // through a __temp__
that is renamed to the final (ignored) name. - // Unlike the journal tables below, a __temp__ name CAN reach the - // versioned plane — that is the incident this pattern fixes, and - // ignored_cursor_untrack.go's scratch can still be sitting at HEAD on a - // lineage whose repair window a blanket commit caught before this - // shipped. What holds instead is the weaker invariant - // versionGatedDoltIgnorePatterns actually needs: no __temp__ table is - // one whose COMMITTED lineage is ever read back, so suppressing its - // staging can strand no data. Every creator disposes of its own - // intermediate within the pass that made it — by renaming onto an - // already-ignored final name (ignored/0001, 0002, 0012, 0022; main - // 0055, 0064), by dropping it outright (main 0062's __temp__events_flip, - // the one main-plane intermediate that is never renamed), or, for the - // untrack scratch, by a sweep that forces past this very pattern - // (dropIgnoredCursorScratch). A new __temp__ producer owes the same. - // So the pattern needs no version gate, but it prevents residue rather - // than repairing it: where an older binary already committed some other - // __temp__X at HEAD, the pattern keeps that table's drop or rename delta - // out of every ignore-filtered staging candidate set (dirtyTables with - // excludeIgnored, existingCommittableTables), so no migration commit - // stages it, and dolt pull refuses the uncommitted non-add delta until - // the store is repaired by hand. And it must be asserted before the - // ignored series runs: on a - // @@dolt_transaction_commit=1 server an unignored CREATE __temp__X - // auto-commits a real table at HEAD, and the rename to the ignored final - // name then leaves an unstageable "__temp__X -> X" rename half in - // dolt_status that wedges the store for every open behind the - // dirty-table guard. dolt matches ignore patterns with ? * % only — - // underscores are literal — so this covers exactly the __temp__ prefix. - "__temp__%", // The events journal tables (bd-opisf) are seeded here rather than // version-gated: they have never existed on the versioned plane, so // asserting the pattern before 0064 runs is what keeps the CREATE from @@ -664,18 +539,11 @@ func seedDoltIgnorePatterns(ctx context.Context, db DBConn) (bool, error) { // short-circuit nothing downstream would ever commit the seed, and on the // migration path the seed must be committed before the first step so an // interrupted pass leaves a clean working set (#4566 self-heal contract). -// -// --skip-empty because on a @@dolt_transaction_commit=1 server the seed's -// INSERTs were already dolt-committed at their own transaction boundaries, -// so there is nothing left to stage: without it the labeled commit dies with -// "nothing to commit" and takes the whole pass down on exactly the stores an -// under-seeded heal targets. The seed is durable either way; only the commit -// label is lost on that path. func commitSeededDoltIgnore(ctx context.Context, db DBConn) error { if err := DrainCall(ctx, db, "CALL DOLT_ADD('dolt_ignore')"); err != nil { return fmt.Errorf("staging seeded dolt_ignore patterns: %w", err) } - if err := DrainCall(ctx, db, "CALL DOLT_COMMIT('-m', 'schema: seed dolt_ignore patterns', '--skip-empty')"); err != nil { + if err := DrainCall(ctx, db, "CALL DOLT_COMMIT('-m', 'schema: seed dolt_ignore patterns')"); err != nil { return fmt.Errorf("committing seeded dolt_ignore patterns: %w", err) } return nil @@ -780,21 +648,6 @@ func MigrateUpTo(ctx context.Context, db DBConn, maxVersion int) (int, error) { } func MigrateUp(ctx context.Context, db DBConn) (int, error) { - // Consent gate first, before ANY write — dolt_ignore seeding included: an - // existing database with pending main-sequence migrations must stay - // byte-identical when the operator has not consented (see - // migrate_consent.go). Fresh and already-current databases pass through. - if err := checkMigrateConsent(ctx, db); err != nil { - return 0, err - } - return migrateUpConsented(ctx, db) -} - -// migrateUpConsented is MigrateUp past its consent gate. It exists for the one -// caller whose consent the gate's version test cannot see: an open holding -// fresh-bootstrap heal authority, which proves it created the database it is -// migrating (see MigrateUpWithLock). Every other caller goes through MigrateUp. -func migrateUpConsented(ctx context.Context, db DBConn) (int, error) { // Re-assert the canonical dolt_ignore patterns before anything else, and // in particular before the migrationWorkNeeded short-circuit: a database // whose migration cursors arrived at-latest without executing the seeding @@ -1409,40 +1262,28 @@ func (m migrationSource) bootstrapSQL() string { // hasContentHashColumn reports whether the cursor table already carries the // content_hash column. A not-yet-created table simply reports false. -func (m migrationSource) hasContentHashColumn(ctx context.Context, db DBConn) (bool, error) { - return showColumnExists(ctx, db, m.cursorTable, "content_hash") -} - -// showColumnExists reports whether table carries column, probing the ONE table -// with SHOW COLUMNS rather than INFORMATION_SCHEMA.COLUMNS, whose predicate Dolt -// does not push down: on a server hosting ~30 databases the INFORMATION_SCHEMA -// form scanned every database and took 5-6 s per probe (measured 2026-09-17, -// portharbour, dolt 2.3.x), and cursorRealityFloor runs it on EVERY store open — -// a 15-store federated `gc ready` spent ~80 s of its 90 s here. SHOW COLUMNS on -// the same server answers in 0.15 s. // -// The LIKE narrows the result set, but the Field name is still compared exactly -// because '_' is a LIKE single-character wildcard (the LIKE literal is left -// unescaped on purpose: the exact compare is the guard, and the repair-path -// mocks pin this literal form). A missing table reports false, as the -// INFORMATION_SCHEMA count did. table and column are the hardcoded -// identifiers the migration series names — never user input. -func showColumnExists(ctx context.Context, db DBConn, table, column string) (bool, error) { - //nolint:gosec // G201: table and the LIKE literal are migration-series constants. - rows, err := db.QueryContext(ctx, "SHOW COLUMNS FROM "+table+" LIKE '"+column+"'") +// It probes a single table with SHOW COLUMNS rather than INFORMATION_SCHEMA.COLUMNS, +// whose predicate Dolt does not push down. The LIKE narrows the result set, but +// we still compare the Field name exactly because '_' is a LIKE single-character +// wildcard. +func (m migrationSource) hasContentHashColumn(ctx context.Context, db DBConn) (bool, error) { + //nolint:gosec // G201: m.cursorTable is a hardcoded constant; the LIKE literal is fixed. + rows, err := db.QueryContext(ctx, "SHOW COLUMNS FROM "+m.cursorTable+" LIKE 'content_hash'") if err != nil { - // SHOW COLUMNS errors on a missing table; the INFORMATION_SCHEMA probe - // returned count 0 instead. Preserve that: an absent table has no columns. + // SHOW COLUMNS errors on a missing table; the old INFORMATION_SCHEMA + // probe returned count 0 instead. Preserve that: an absent cursor table + // has no content_hash column. if dberrors.IsTableNotExist(err) { return false, nil } - return false, fmt.Errorf("checking %s.%s: %w", table, column, err) + return false, fmt.Errorf("checking %s.content_hash: %w", m.cursorTable, err) } defer func() { _ = rows.Close() }() cols, err := rows.Columns() if err != nil { - return false, fmt.Errorf("checking %s.%s: %w", table, column, err) + return false, fmt.Errorf("checking %s.content_hash: %w", m.cursorTable, err) } // SHOW COLUMNS returns Field, Type, Null, Key, Default, Extra (and possibly // more on some servers); scan every column into RawBytes and read the first @@ -1454,14 +1295,14 @@ func showColumnExists(ctx context.Context, db DBConn, table, column string) (boo } for rows.Next() { if err := rows.Scan(dest...); err != nil { - return false, fmt.Errorf("checking %s.%s: %w", table, column, err) + return false, fmt.Errorf("checking %s.content_hash: %w", m.cursorTable, err) } - if len(cells) > 0 && string(cells[0]) == column { + if len(cells) > 0 && string(cells[0]) == "content_hash" { return true, nil } } if err := rows.Err(); err != nil { - return false, fmt.Errorf("checking %s.%s: %w", table, column, err) + return false, fmt.Errorf("checking %s.content_hash: %w", m.cursorTable, err) } return false, nil } @@ -1547,16 +1388,19 @@ func (m migrationSource) currentVersion(ctx context.Context, db DBConn) (int, er // not-yet-created cursor table poisons the pooled connection: tables // created afterwards on other connections stay invisible to this one for // the rest of its life in the pool (be-bv7x). - cursorExists, err := tableInCurrentDatabase(ctx, db, m.cursorTable) - if err != nil { + var cursorExists int + if err := db.QueryRowContext(ctx, + "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = DATABASE() AND table_name = ?", + m.cursorTable, + ).Scan(&cursorExists); err != nil { return 0, fmt.Errorf("probing %s existence: %w", m.cursorTable, err) } - if !cursorExists { + if cursorExists == 0 { return 0, nil } var current int - err = db.QueryRowContext(ctx, "SELECT COALESCE(MAX(version), 0) FROM "+m.cursorTable).Scan(¤t) + err := db.QueryRowContext(ctx, "SELECT COALESCE(MAX(version), 0) FROM "+m.cursorTable).Scan(¤t) if err != nil && err != sql.ErrNoRows { if dberrors.IsTableNotExist(err) { return 0, nil @@ -1653,114 +1497,19 @@ func (m migrationSource) cursorRealityFloor(ctx context.Context, db DBConn) (int // sentinelTableExists is a function variable for the same reason // issueRowCounter is: it lets the cursor-reality tests exercise the real -// decision without a live database. It runs on EVERY store open -// (cursorRealityFloor: the sentinel and floored-sentinel tables, and the -// column sentinel's carrier), so it takes the SHOW TABLES prober for the -// reason sentinelColumnExists takes SHOW COLUMNS. -var sentinelTableExists = showTableExists - -// showTableExists reports whether the session's current database holds table, -// probing with SHOW TABLES rather than INFORMATION_SCHEMA.TABLES. Dolt does not -// push the TABLES predicate down any more than the COLUMNS one: the probe walks -// the catalog of every database on the server. On a sql-server hosting 28 -// databases the INFORMATION_SCHEMA read costs 25-30 ms against 0.6 ms for the -// listing (dolt 2.1.8), and the open path asks a dozen times per bd write. -// -// It reads the unfiltered listing and compares names case-insensitively, which -// is what the INFORMATION_SCHEMA count it replaced answers: Dolt resolves and -// counts table names case-insensitively, but its SHOW TABLES LIKE filter -// matches case-sensitively, so a filtered probe would read a table stored as -// BD_EVENTS_SEQ as absent. The comparison is otherwise exact; without a LIKE -// pattern, '_' wildcards cannot match a lookalike either. -// -// Unlike the INFORMATION_SCHEMA count, SHOW TABLES errors when no database is -// selected or the selected one was dropped, so it is only for callers already -// on the target database: the cursor-existence probe in currentVersion, which -// must succeed on any session (be-bv7x), gates it with tableInCurrentDatabase. -// table is a migration-series constant, never user input. -func showTableExists(ctx context.Context, db DBConn, table string) (bool, error) { - rows, err := db.QueryContext(ctx, "SHOW TABLES") - if err != nil { - return false, fmt.Errorf("checking table %s: %w", table, err) - } - defer func() { _ = rows.Close() }() - - cols, err := rows.Columns() - if err != nil { - return false, fmt.Errorf("checking table %s: %w", table, err) - } - // SHOW TABLES returns Tables_in_ (plus Table_type on SHOW FULL TABLES - // and on some servers); the first column is the table name. - cells := make([]sql.RawBytes, len(cols)) - dest := make([]any, len(cols)) - for i := range cells { - dest[i] = &cells[i] - } - for rows.Next() { - if err := rows.Scan(dest...); err != nil { - return false, fmt.Errorf("checking table %s: %w", table, err) - } - if len(cells) > 0 && strings.EqualFold(string(cells[0]), table) { - return true, nil - } - } - if err := rows.Err(); err != nil { - return false, fmt.Errorf("checking table %s: %w", table, err) - } - return false, nil -} - -// tableInCurrentDatabase is showTableExists for a session that may have no -// usable database: the cursor-existence probe runs on any session, so it must -// never issue a statement that can fail (be-bv7x). SHOW TABLES fails with no -// database selected (1105) and on a session whose database was dropped under -// it (1049); the schemata read answers 0 in both cases instead, which reads -// the table as absent, exactly as the INFORMATION_SCHEMA.TABLES count with -// TABLE_SCHEMA = DATABASE() did. Both reads are one round trip each. -// -// The guard is check-then-act, not atomic: a DROP DATABASE landing between the -// two reads still fails the SHOW TABLES and pins the session. On a *sql.DB the -// two reads may also land on different pooled connections, so the guard holds -// only when every connection in the pool is on the same database (a DSN that -// names it). -func tableInCurrentDatabase(ctx context.Context, db DBConn, table string) (bool, error) { - var usable int +// decision without a live database. +var sentinelTableExists = func(ctx context.Context, db DBConn, table string) (bool, error) { + var n int if err := db.QueryRowContext(ctx, - "SELECT COUNT(*) FROM information_schema.schemata WHERE schema_name = DATABASE()", - ).Scan(&usable); err != nil { + `SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES + WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = ?`, + table).Scan(&n); err != nil { return false, err } - if usable == 0 { - return false, nil - } - return showTableExists(ctx, db, table) + return n > 0, nil } -// sentinelColumnExists runs on EVERY store open (cursorRealityFloor), so it -// takes the SHOW COLUMNS prober rather than schemaColumnExists's -// INFORMATION_SCHEMA form: 0.15 s against 5-6 s per probe on a Dolt server -// hosting ~30 databases (measured 2026-09-17). The repair paths keep -// schemaColumnExists as is — they run once per migration, not per open. -// -// SHOW COLUMNS fails on a missing table, and the carrier table is exactly what -// a release-lineage store lacks (no leases before ignored/0012). A failing -// statement is not free here: on the store's multiStatements DSN it pins the -// pooled Dolt session to its pre-statement catalog, so a table another -// connection creates afterwards stays "not found" on it (be-bv7x; reproduced -// on dolt 2.1.8 and 2.2.0). So the carrier's existence is read first through -// sentinelTableExists, whose read succeeds whether or not the table is there, -// and SHOW COLUMNS is only sent to a table that is. A missing table reads as a -// missing column, as the INFORMATION_SCHEMA count did. -var sentinelColumnExists = func(ctx context.Context, db DBConn, table, column string) (bool, error) { - present, err := sentinelTableExists(ctx, db, table) - if err != nil { - return false, fmt.Errorf("probing %s: %w", table, err) - } - if !present { - return false, nil - } - return showColumnExists(ctx, db, table, column) -} +var sentinelColumnExists = schemaColumnExists func (m migrationSource) pendingVersions(ctx context.Context, db DBConn) ([]int, error) { current, err := m.currentVersion(ctx, db) @@ -2003,11 +1752,6 @@ func scanRowByColumn(rows *sql.Rows) (CallRow, error) { // connection. Bodies that invoke a stored procedure (today 0040 and 0041, both // CALL DOLT_COMMIT) are routed through DrainCall so their result sets are // consumed; all other migrations keep the unchanged ExecContext path. -// -// sqlText is always a compile-time embedded migration body (go:embed above; -// frozen once merged per scripts/check-migration-hygiene.sh) — never runtime -// or user input. Executing it verbatim is the migration contract, so SAST -// "SQL injection" findings on this call are accepted by design. func execMigrationBody(ctx context.Context, db DBConn, sqlText string) error { if !procedureCallRe.MatchString(sqlText) { _, err := db.ExecContext(ctx, sqlText) @@ -2133,28 +1877,7 @@ func runMigrations(ctx context.Context, db DBConn, src migrationSource, minVersi fmt.Fprintf(stderr, "Applying migration %04d: %s…\n", mf.version, humanMigrationName(mf.name)) start := time.Now() - // Soft warning only, by design — never turn this into a hard - // timeout/abort. Migrations can legitimately run arbitrarily long - // (see the initSchema comment in internal/storage/dolt/store.go, - // e.g. migration 0047's full-table recompute); aborting mid-DDL - // risks leaving Dolt in a partially-migrated state with no clean - // rollback. Full scoping rationale and rejected alternatives: - // bd show be-m65rs. - // - // The wrap deliberately covers the migration's own SQL body and - // nothing else: the dirty-table snapshot and preMigrationRepair above - // and the cursor INSERT and commitMigrationStep below all run outside - // it, including the per-step DOLT_ADD/DOLT_COMMIT that the comment - // further down calls the expensive, fallible part of this step on the - // production embedded path. Widening the boundary to the whole step - // means moving this loop's snapshot/commit ordering — the atomicity - // contract documented on runMigrations and in commitMigrationStep - // (#4566, #4690) — which is more risk than an observability change - // should carry. So a wedge inside the per-step commit is still silent; - // CHANGELOG.md scopes the user-facing promise to match. - if err := runMigrationWithWatchdog(ctx, watchdogStderr, mf.version, humanMigrationName(mf.name), migrationWatchdogIntervalDuration(), - func(ctx context.Context) error { return execMigrationBody(ctx, db, string(data)) }, - ); err != nil { + if err := execMigrationBody(ctx, db, string(data)); err != nil { return count, fmt.Errorf("migration %s: %w", mf.name, err) } sum := sha256.Sum256(data) diff --git a/internal/storage/schema/schema_skew_test.go b/internal/storage/schema/schema_skew_test.go index 860f90933..5a03d27af 100644 --- a/internal/storage/schema/schema_skew_test.go +++ b/internal/storage/schema/schema_skew_test.go @@ -290,35 +290,3 @@ func TestIsSchemaSkewError_OtherError(t *testing.T) { t.Error("IsSchemaSkewError(non-SchemaSkewError) = true, want false") } } - -// -- CheckBehindDrift unit tests (mock DB) -- - -func TestCheckBehindDrift_FreshDB_ReturnsSchemaBehindError(t *testing.T) { - db, mock, err := sqlmock.New() - if err != nil { - t.Fatalf("sqlmock.New: %v", err) - } - defer db.Close() - - // On a fresh database where schema_migrations does not exist, the cursor probe - // returns 0 and no bare SELECT against schema_migrations is issued. - expectCursorProbe(mock, "schema_migrations", false) - - got := CheckBehindDrift(context.Background(), db) - if got == nil { - t.Fatal("CheckBehindDrift = nil, want *SchemaBehindError for fresh DB (version=0)") - } - var behindErr *SchemaBehindError - if !errors.As(got, &behindErr) { - t.Fatalf("error type = %T (%v), want *SchemaBehindError", got, got) - } - if behindErr.DBVersion != 0 { - t.Errorf("DBVersion = %d, want 0", behindErr.DBVersion) - } - if behindErr.BinaryVersion != LatestVersion() { - t.Errorf("BinaryVersion = %d, want %d", behindErr.BinaryVersion, LatestVersion()) - } - if err := mock.ExpectationsWereMet(); err != nil { - t.Fatalf("unmet sql expectations: %v", err) - } -} diff --git a/internal/storage/schema/schema_test.go b/internal/storage/schema/schema_test.go index b63c0b553..a80f76e9d 100644 --- a/internal/storage/schema/schema_test.go +++ b/internal/storage/schema/schema_test.go @@ -85,7 +85,7 @@ func TestMigrateUpReturnsDirtyTablesErrorForPreExistingDirtyTable(t *testing.T) // pre-existing tables are unstaged, before the dirty-table guards run. mock.ExpectQuery(regexp.QuoteMeta("CALL DOLT_ADD('dolt_ignore')")). WillReturnRows(sqlmock.NewRows([]string{"status"})) - mock.ExpectQuery(regexp.QuoteMeta("CALL DOLT_COMMIT('-m', 'schema: seed dolt_ignore patterns', '--skip-empty')")). + mock.ExpectQuery(regexp.QuoteMeta("CALL DOLT_COMMIT('-m', 'schema: seed dolt_ignore patterns')")). WillReturnRows(sqlmock.NewRows([]string{"hash"})) // committableDirtyTables -> dirtyTables(ctx, db, true): same dirty state. expectDirtyDoltStatusRow(mock, "dependencies", false) @@ -242,13 +242,13 @@ func TestCheckNoDuplicateVersionsPanicsWithBothFilenames(t *testing.T) { checkNoDuplicateVersions(files) } -// TestGoEmbedMigrationSourcesHaveNoDuplicateVersions runs discovery over the +// TestEmbeddedMigrationSourcesHaveNoDuplicateVersions 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 TestGoEmbedMigrationSourcesHaveNoDuplicateVersions(t *testing.T) { +func TestEmbeddedMigrationSourcesHaveNoDuplicateVersions(t *testing.T) { for _, src := range []migrationSource{mainSource, ignoredSource} { files := src.list() // panics on duplicate versions if len(files) == 0 { @@ -1959,24 +1959,6 @@ func TestAllMigrationsSQLUsesDirectDDLForKnownCLIIncompatibilities(t *testing.T) "ALTER TABLE wisp_comments MODIFY COLUMN text LONGTEXT NOT NULL;", // 0066: same prepared-ALTER shape as 0060, same CLI no-op on 2.2.x. "ALTER TABLE bd_events_journal ADD COLUMN actor VARCHAR(255) NOT NULL DEFAULT '';", - // 0067: two-plane prepared ADD COLUMN, same shape as 0060. - "ALTER TABLE issues ADD COLUMN current_revision BIGINT NOT NULL DEFAULT 1;", - "ALTER TABLE wisps ADD COLUMN current_revision BIGINT NOT NULL DEFAULT 1;", - // 0068: single-plane prepared ADD COLUMN, same shape as 0066 (no - // wisps twin — issue_versions has none), plus step 7's prepared - // MODIFY COLUMN (durable_state JSON -> LONGBLOB), same shape as 0065. - "ALTER TABLE issue_versions ADD COLUMN attribution_status VARCHAR(20) NOT NULL;", - "ALTER TABLE issue_versions MODIFY COLUMN durable_state LONGBLOB;", - // 0069: two prepared MODIFY COLUMNs on issue_versions (change_at and - // removed_at to DATETIME(6)), same shape as 0068's step 7. - "ALTER TABLE issue_versions MODIFY COLUMN change_at DATETIME(6) NOT NULL;", - "ALTER TABLE issue_versions MODIFY COLUMN removed_at DATETIME(6);", - // 0070: two-plane prepared ADD COLUMN, same shape as 0067. - "ALTER TABLE issues ADD COLUMN participation_generation BIGINT NULL;", - "ALTER TABLE wisps ADD COLUMN participation_generation BIGINT NULL;", - // 0071: single-plane prepared ADD COLUMN, same shape as 0068's - // attribution_status (no wisps twin -- issue_versions has none). - "ALTER TABLE issue_versions ADD COLUMN removed_restriction VARCHAR(30);", } { if !strings.Contains(got, want) { t.Fatalf("AllMigrationsSQL missing direct CLI DDL %q", want) @@ -1998,24 +1980,6 @@ func TestAllMigrationsSQLUsesDirectDDLForKnownCLIIncompatibilities(t *testing.T) // 0066 guards its ALTER the same way; only its source text carries // this probe (the events table's actor column is a bare CREATE). "COLUMN_NAME = 'actor'", - // 0067 guards both planes' ALTERs the same way. Its two guard - // variables are the per-migration anchors; the probe token would - // also appear in the ignored twin, which is not bundled. - "@issues_cr_needs_add", - "@wisps_cr_needs_add", - // 0068 guards both of its ALTERs the same way; only its source text - // carries these probes. - "@issue_versions_as_needs_add", - "@issue_versions_ds_needs_retype", - // 0069 guards both of its MODIFYs the same way. - "@issue_versions_change_at_needs_widen", - "@issue_versions_removed_at_needs_widen", - // 0070 guards both planes' ALTERs the same way, mirroring 0067's pair. - "@issues_pg_needs_add", - "@wisps_pg_needs_add", - // 0071 guards its ALTER the same way; only its source text carries - // this probe. - "@issue_versions_rr_needs_add", } { if strings.Contains(got, forbidden) { t.Fatalf("AllMigrationsSQL contains source prepared-DDL guard %q", forbidden) @@ -2638,94 +2602,3 @@ DROP TABLE IF EXISTS wisps; requireDoltCount(t, dir, `SELECT COUNT(*) AS c FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'wisps'`, "0") } - -// TestShowTableExistsMatchesInformationSchemaOnDolt proves the per-open table -// probe (the unfiltered SHOW TABLES listing, names compared case-insensitively) -// agrees with the INFORMATION_SCHEMA.TABLES count it replaced, on a real Dolt. -// It covers every table the open path probes — both cursor tables, the -// sentinel and floored-sentinel tables, the column sentinel's carrier and the -// untrack scratch table — under bd's own dolt_ignore patterns, so all but -// schema_migrations live only in the working set. It also covers a name whose -// '_' LIKE-matches a different table, and a table stored in another case: Dolt -// resolves and counts table names case-insensitively there, but its SHOW -// TABLES LIKE filter does not. -func TestShowTableExistsMatchesInformationSchemaOnDolt(t *testing.T) { - testutil.RequireDoltBinary(t) - - dir := filepath.Join(t.TempDir(), "show-tables-probe") - if err := os.MkdirAll(dir, 0o755); err != nil { - t.Fatalf("create probe dir: %v", err) - } - runDoltCommand(t, dir, "init", "--name", "test", "--email", "test@example.com") - - infoSchemaHas := func(table string) bool { - rows := queryDoltCSV(t, dir, fmt.Sprintf(` -SELECT COUNT(*) AS cnt -FROM INFORMATION_SCHEMA.TABLES -WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = '%s'`, table)) - return len(rows) == 1 && rows[0]["cnt"] == "1" - } - // The same listing and comparison showTableExists makes. - showTablesHas := func(table string) bool { - for _, r := range queryDoltCSV(t, dir, "SHOW TABLES") { - for _, v := range r { - if strings.EqualFold(v, table) { - return true - } - } - } - return false - } - agree := func(state, table string, want bool) { - t.Helper() - if got := showTablesHas(table); got != want { - t.Fatalf("%s: SHOW TABLES reported %s present=%v, want %v", state, table, got, want) - } - if got := infoSchemaHas(table); got != want { - t.Fatalf("%s: INFORMATION_SCHEMA reported %s present=%v, want %v", state, table, got, want) - } - } - - probed := []string{mainSource.cursorTable, ignoredSource.cursorTable, ignoredCursorUntrackTempTable} - probed = append(probed, ignoredSource.sentinelTables...) - for _, st := range ignoredSource.sentinelFlooredTables { - probed = append(probed, st.table) - } - for _, sc := range ignoredSource.sentinelColumns { - probed = append(probed, sc.table) - } - - for _, table := range probed { - agree("empty database", table, false) - } - - for _, pattern := range doltIgnorePatterns { - runDoltSQL(t, dir, fmt.Sprintf("REPLACE INTO dolt_ignore VALUES ('%s', true)", pattern)) - } - for _, gated := range versionGatedDoltIgnorePatterns { - runDoltSQL(t, dir, fmt.Sprintf("REPLACE INTO dolt_ignore VALUES ('%s', true)", gated.pattern)) - } - runDoltSQL(t, dir, "CREATE TABLE "+mainSource.cursorTable+" (version INT PRIMARY KEY)") - runDoltSQL(t, dir, "CALL DOLT_COMMIT('-Am', 'committed cursor')") - for _, table := range probed { - if table == mainSource.cursorTable || table == ignoredCursorUntrackTempTable { - continue - } - runDoltSQL(t, dir, "CREATE TABLE "+table+" (id VARCHAR(64) PRIMARY KEY)") - } - requireDoltNoRows(t, dir, "SELECT table_name FROM dolt_status", "dolt_status after creating only dolt_ignored tables") - for _, table := range probed { - agree("migrated shape, dolt_ignored tables in the working set", table, table != ignoredCursorUntrackTempTable) - } - - // 'schemaxmigrations' LIKE-matches 'schema_migrations'; only the name - // comparison keeps it from reading as the cursor table once that is gone. - runDoltSQL(t, dir, "CREATE TABLE schemaxmigrations (id INT PRIMARY KEY)") - runDoltSQL(t, dir, "DROP TABLE "+mainSource.cursorTable) - agree("LIKE wildcard lookalike", mainSource.cursorTable, false) - - // A table stored in another case is the same table to Dolt. - runDoltSQL(t, dir, "RENAME TABLE bd_events_seq TO bd_events_seq_case") - runDoltSQL(t, dir, "RENAME TABLE bd_events_seq_case TO BD_EVENTS_SEQ") - agree("table stored in another case", "bd_events_seq", true) -} diff --git a/internal/storage/schema/smart_remote_migrate_gate.go b/internal/storage/schema/smart_remote_migrate_gate.go index 97dc5b121..3b48b07cb 100644 --- a/internal/storage/schema/smart_remote_migrate_gate.go +++ b/internal/storage/schema/smart_remote_migrate_gate.go @@ -250,8 +250,9 @@ func routeAdoptFastForward(ctx context.Context, db DBConn, ref string, adopt *Fa // to read and routing stays exactly as it was before this check existed. (The // shared unit-of-work provider's injection site wires AheadBehind only; every // other callback stays nil there deliberately, so no new auto-write path -// opens.) With only IsStrictAncestor wired, the fact narrows to the ahead == 0 -// shape; the diverged shape then reads "not behind", which is the pre-widening +// opens.) With only +// IsStrictAncestor wired, the fact narrows to the ahead == 0 shape; the +// diverged shape then reads "not behind", which is the pre-widening // behavior rather than a new hazard. // // Unlike routeAdoptFastForward, a query error is NOT folded into "false": diff --git a/internal/storage/sqlbuild/BUILD.bazel b/internal/storage/sqlbuild/BUILD.bazel deleted file mode 100644 index 7ff089896..000000000 --- a/internal/storage/sqlbuild/BUILD.bazel +++ /dev/null @@ -1,115 +0,0 @@ -load("@rules_go//go:def.bzl", "go_library", "go_test") - -go_library( - name = "sqlbuild", - srcs = [ - "counts.go", - "descendants.go", - "filter.go", - "labels.go", - "ready.go", - "reclaim.go", - "sort.go", - "sqlbuild.go", - ], - importpath = "github.com/steveyegge/beads/internal/storage/sqlbuild", - visibility = ["//:__subpackages__"], - deps = [ - "//internal/storage", - "//internal/storage/domain", - "//internal/types", - ], -) - -go_test( - name = "sqlbuild_test", - srcs = [ - "descendants_test.go", - "filter_test.go", - "imports_boundary_test.go", - "keyset_test.go", - "labels_test.go", - "reclaim_test.go", - "sqlbuild_test.go", - ], - data = [ - ":go_srcs", - "//internal/storage/domain:go_srcs", - ], - embed = [":sqlbuild"], - deps = ["//internal/types"], -) - -# --- begin go_srcs (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) --- - -filegroup( - name = "go_srcs", - srcs = glob( - ["**/*.go"], - allow_empty = True, - ), - visibility = ["//:__subpackages__"], -) - -# --- end go_srcs --- - -# --- begin repo_files (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) --- - -filegroup( - name = "repo_doc_files", - srcs = glob( - [ - "**/*.md", - "**/*.mdx", - ], - allow_empty = True, - exclude = ["**/node_modules/**"], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_go_srcs", - srcs = glob( - ["**/*.go"], - allow_empty = True, - exclude = [ - "**/*_test.go", - "**/node_modules/**", - ], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_go_test_srcs", - srcs = glob( - ["**/*_test.go"], - allow_empty = True, - exclude = ["**/node_modules/**"], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_other_files", - srcs = glob( - ["**"], - exclude = [ - "**/*.db", - "**/*.exe", - "**/*.go", - "**/*.md", - "**/*.mdx", - "**/*.out", - "**/*.prof", - "**/*.pyc", - "**/*.test", - "**/__pycache__/**", - "**/node_modules/**", - ], - ), - visibility = ["//:__subpackages__"], -) - -# --- end repo_files --- diff --git a/internal/storage/sqlbuild/descendants.go b/internal/storage/sqlbuild/descendants.go deleted file mode 100644 index d7a0cb211..000000000 --- a/internal/storage/sqlbuild/descendants.go +++ /dev/null @@ -1,93 +0,0 @@ -package sqlbuild - -import ( - "fmt" - "strings" -) - -// descendantWalkTargetCols are the three mutually-exclusive dependency target -// columns, in the precedence order DepTargetExpr resolves them. -var descendantWalkTargetCols = []string{"depends_on_issue_id", "depends_on_wisp_id", "depends_on_external"} - -func descendantWalkTables(includeWisps bool) []string { - if includeWisps { - return []string{"dependencies", "wisp_dependencies"} - } - return []string{"dependencies"} -} - -// DescendantWalkQuery builds the transitive parent-child descendant walk for -// rootID, as one recursive member per (dependency table, target column) pair. -// -// The shape is load-bearing. Wrapping the edge relation in a non-recursive -// `parent_edges` CTE (dependencies UNION ALL wisp_dependencies, COALESCE'd -// target) leaves Dolt unable to push the recursive join into the wrapper: it -// re-scans the whole materialized relation once per recursion row instead of -// probing idx_dep_type_issue. Joining a base table on a bare column makes an -// index lookup possible, and the per-column IS NULL guards reproduce -// DepTargetExpr's COALESCE precedence exactly. -// -// The join hints are load-bearing too. Which side of the recursive join Dolt -// makes the outer one depends on its table statistics: with none yet for a -// freshly loaded edge table (Dolt 2.2.4) it keeps the recursive working table -// on the inner side and scans the edge table per iteration, which measures no -// faster than the wrapper shape, then flips to index lookups once statistics -// exist. JOIN_ORDER(d,e) LOOKUP_JOIN(d,e) pins the frontier as the outer side -// with an index probe into the edge table on every member regardless of -// statistics; on MySQL the unknown LOOKUP_JOIN hint is ignored with a -// warning — one per hint comment, so 6 per includeWisps=true query, which a -// runner that logs SHOW WARNINGS will see on every call — and other dialects -// read the comment as a comment. -// -// Bind with DescendantWalkArgs; placeholder order follows the member order -// built here. -func DescendantWalkQuery(includeWisps bool) string { - var anchors, recursive []string - for _, table := range descendantWalkTables(includeWisps) { - for i, col := range descendantWalkTargetCols { - anchors = append(anchors, fmt.Sprintf(` - SELECT issue_id, 1, CONCAT(',', ?, ',', issue_id, ',') - FROM %s - WHERE type = 'parent-child' AND %s = ?%s`, - table, col, nullGuards("", descendantWalkTargetCols[:i]))) - - recursive = append(recursive, fmt.Sprintf(` - SELECT /*+ JOIN_ORDER(d,e) LOOKUP_JOIN(d,e) */ e.issue_id, d.depth + 1, CONCAT(d.path, e.issue_id, ',') - FROM descendants d - JOIN %s e ON e.%s = d.id AND e.type = 'parent-child' - WHERE (? <= 0 OR d.depth < ?)%s - AND LOCATE(CONCAT(',', e.issue_id, ','), d.path) = 0`, - table, col, nullGuards("e.", descendantWalkTargetCols[:i]))) - } - } - - members := strings.Join(append(anchors, recursive...), "\n\t\t\t\tUNION ALL") - return fmt.Sprintf(` - WITH RECURSIVE - descendants(id, depth, path) AS (%s - ) - SELECT id, depth FROM descendants WHERE id <> ? - `, members) -} - -// DescendantWalkArgs binds DescendantWalkQuery(includeWisps): rootID twice per -// anchor, maxDepth twice per recursive member, then rootID for the tail filter. -func DescendantWalkArgs(rootID string, maxDepth int, includeWisps bool) []interface{} { - members := len(descendantWalkTables(includeWisps)) * len(descendantWalkTargetCols) - args := make([]interface{}, 0, 4*members+1) - for i := 0; i < members; i++ { - args = append(args, rootID, rootID) - } - for i := 0; i < members; i++ { - args = append(args, maxDepth, maxDepth) - } - return append(args, rootID) -} - -func nullGuards(prefix string, cols []string) string { - var b strings.Builder - for _, col := range cols { - b.WriteString(" AND " + prefix + col + " IS NULL") - } - return b.String() -} diff --git a/internal/storage/sqlbuild/descendants_test.go b/internal/storage/sqlbuild/descendants_test.go deleted file mode 100644 index 28bd586c3..000000000 --- a/internal/storage/sqlbuild/descendants_test.go +++ /dev/null @@ -1,167 +0,0 @@ -package sqlbuild_test - -import ( - "strings" - "testing" - - "github.com/steveyegge/beads/internal/storage/sqlbuild" -) - -// TestDescendantWalkQueryRecursesOffBaseTables pins the shape that makes the -// walk index-friendly on Dolt (be-qfm): no materialized edge wrapper, and every -// recursive member joining a base table on a bare target column so -// idx_dep_type_* applies. A `parent_edges` wrapper re-scans ~4.6k rows per -// recursion row (7.46s for 483 descendants on the field database). -func TestDescendantWalkQueryRecursesOffBaseTables(t *testing.T) { - t.Parallel() - - for _, includeWisps := range []bool{false, true} { - q := sqlbuild.DescendantWalkQuery(includeWisps) - - if strings.Contains(q, "parent_edges") { - t.Errorf("includeWisps=%v: query still materializes a parent_edges wrapper:\n%s", includeWisps, q) - } - if strings.Contains(q, "JOIN (") { - t.Errorf("includeWisps=%v: query joins a derived table, not a base table:\n%s", includeWisps, q) - } - if strings.Contains(q, "COALESCE") { - t.Errorf("includeWisps=%v: COALESCE on the join key defeats idx_dep_type_*:\n%s", includeWisps, q) - } - if !strings.Contains(q, "JOIN dependencies e ON e.depends_on_issue_id = d.id AND e.type = 'parent-child'") { - t.Errorf("includeWisps=%v: missing the indexed issue-target recursive member:\n%s", includeWisps, q) - } - if got := strings.Count(q, "LOCATE(CONCAT(',', e.issue_id, ','), d.path) = 0"); got != recursiveMembers(includeWisps) { - t.Errorf("includeWisps=%v: cycle guard on %d of %d recursive members", includeWisps, got, recursiveMembers(includeWisps)) - } - if got := strings.Count(q, "(? <= 0 OR d.depth < ?)"); got != recursiveMembers(includeWisps) { - t.Errorf("includeWisps=%v: maxDepth guard on %d of %d recursive members", includeWisps, got, recursiveMembers(includeWisps)) - } - if got := strings.Count(q, "SELECT /*+ JOIN_ORDER(d,e) LOOKUP_JOIN(d,e) */ e.issue_id"); got != recursiveMembers(includeWisps) { - t.Errorf("includeWisps=%v: lookup-join hints on %d of %d recursive members", includeWisps, got, recursiveMembers(includeWisps)) - } - if got := strings.Count(q, "type = 'parent-child'"); got != 2*recursiveMembers(includeWisps) { - t.Errorf("includeWisps=%v: dep-type filter on %d members, want %d", includeWisps, got, 2*recursiveMembers(includeWisps)) - } - - wispRefs := strings.Contains(q, "wisp_dependencies") - if wispRefs != includeWisps { - t.Errorf("includeWisps=%v: wisp_dependencies present=%v", includeWisps, wispRefs) - } - } -} - -// TestDescendantWalkArgsMatchPlaceholders keeps the binding aligned with the -// generated member list; a drift here silently shifts rootID onto a maxDepth -// slot and returns the wrong subtree. -func TestDescendantWalkArgsMatchPlaceholders(t *testing.T) { - t.Parallel() - - for _, includeWisps := range []bool{false, true} { - q := sqlbuild.DescendantWalkQuery(includeWisps) - args := sqlbuild.DescendantWalkArgs("root-1", 7, includeWisps) - - if want, got := strings.Count(q, "?"), len(args); want != got { - t.Fatalf("includeWisps=%v: %d placeholders, %d args", includeWisps, want, got) - } - - n := recursiveMembers(includeWisps) - for i := 0; i < 2*n; i++ { - if args[i] != "root-1" { - t.Errorf("includeWisps=%v: anchor arg %d = %v, want root-1", includeWisps, i, args[i]) - } - } - for i := 2 * n; i < 4*n; i++ { - if args[i] != 7 { - t.Errorf("includeWisps=%v: recursive arg %d = %v, want maxDepth 7", includeWisps, i, args[i]) - } - } - if args[len(args)-1] != "root-1" { - t.Errorf("includeWisps=%v: tail arg = %v, want root-1", includeWisps, args[len(args)-1]) - } - } -} - -// TestDescendantWalkQueryReproducesDepTargetPrecedence pins the per-column -// IS NULL guards, the one place the reshaped walk's semantics differ textually -// from the COALESCE it replaces, and binds the walk's column set to -// DepTargetExpr. -// -// Neither of the other new tests sees these guards: the shape test above -// asserts on parent_edges / JOIN ( / COALESCE and on member counts, never on a -// guard, and the real-Dolt reference test seeds every edge through exactly one -// target column, so no fixture row has two targets set for the precedence to -// resolve. Deleting nullGuards' arguments therefore leaves both green. -// -// The expected counts are derived, not restated: a member keyed on the column -// at index j must exclude every column ahead of it in DepTargetExpr's -// precedence order, so column j carries len(cols)-1-j guards per dependency -// table — 2 for the first column, 1 for the second, 0 for the last. That -// derivation is also what binds the two lists: if DepTargetExpr gains a target -// column and descendantWalkTargetCols does not, both the guard counts and the -// member count below disagree with the query. -func TestDescendantWalkQueryReproducesDepTargetPrecedence(t *testing.T) { - t.Parallel() - - cols := depTargetColumns(t) - - for _, includeWisps := range []bool{false, true} { - q := sqlbuild.DescendantWalkQuery(includeWisps) - tables := recursiveMembers(includeWisps) / len(cols) - if tables*len(cols) != recursiveMembers(includeWisps) { - t.Fatalf("includeWisps=%v: %d members is not one per (table, column) over %d columns %v", - includeWisps, recursiveMembers(includeWisps), len(cols), cols) - } - - // One anchor member per (table, column): the anchor is the only place - // the target column is compared to the bound root id unqualified. - for _, col := range cols { - if got := strings.Count(q, "WHERE type = 'parent-child' AND "+col+" = ?"); got != tables { - t.Errorf("includeWisps=%v: %q anchored on %d of %d dependency tables:\n%s", - includeWisps, col, got, tables, q) - } - } - - // nullGuards emits " AND IS NULL"; the leading " AND " is - // what keeps the unqualified count from also matching the "e."-prefixed - // recursive form. - for j, col := range cols { - wantPerTable := len(cols) - 1 - j - for _, prefix := range []string{"", "e."} { - guard := " AND " + prefix + col + " IS NULL" - if got, want := strings.Count(q, guard), wantPerTable*tables; got != want { - t.Errorf("includeWisps=%v: %q appears %d times, want %d — the members after %s no longer exclude it, so the walk does not reproduce DepTargetExpr's precedence:\n%s", - includeWisps, guard, got, want, col, q) - } - } - } - } -} - -// depTargetColumns is the column list inside DepTargetExpr, in its precedence -// order. descendantWalkTargetCols is unexported, so the exported expression it -// claims to mirror is what the tests can bind it to. -func depTargetColumns(t *testing.T) []string { - t.Helper() - - inner, ok := strings.CutPrefix(sqlbuild.DepTargetExpr, "COALESCE(") - if !ok { - t.Fatalf("DepTargetExpr is no longer a COALESCE expression: %q", sqlbuild.DepTargetExpr) - } - inner, ok = strings.CutSuffix(inner, ")") - if !ok { - t.Fatalf("DepTargetExpr is not parenthesised: %q", sqlbuild.DepTargetExpr) - } - cols := strings.Split(inner, ", ") - if len(cols) < 2 { - t.Fatalf("DepTargetExpr resolves %d column(s), expected the typed target set: %q", len(cols), sqlbuild.DepTargetExpr) - } - return cols -} - -// recursiveMembers is one member per (dependency table, target column) pair. -func recursiveMembers(includeWisps bool) int { - if includeWisps { - return 6 - } - return 3 -} diff --git a/internal/storage/sqlbuild/filter.go b/internal/storage/sqlbuild/filter.go index 0fd9b0acf..777f1fa9f 100644 --- a/internal/storage/sqlbuild/filter.go +++ b/internal/storage/sqlbuild/filter.go @@ -296,26 +296,19 @@ func BuildIssueFilterClauses(query string, filter types.IssueFilter, tables Filt {"due_at", "<", filter.DueBefore}, } { if tc.v != nil { - // The UTC spelling of the instant, at whole seconds. The columns - // hold UTC wall clock; an engine whose column carries no zone - // (a SQL `timestamp` WITHOUT time zone) drops a literal's offset, - // so a bound spelled `+02:00` would compare as that wall clock. - // Spelled in UTC, every engine compares the instant. Dolt already - // honors the offset, so this changes nothing there. whereClauses = append(whereClauses, fmt.Sprintf("%s %s ?", tc.col, tc.op)) - args = append(args, tc.v.UTC().Format(time.RFC3339)) + args = append(args, tc.v.Format(time.RFC3339)) } } if filter.AfterCreatedAt != nil { - // Bind the cursor time as time.Time, not a formatted string — the same - // binding EventsSince uses — but in UTC. A driver can send a time.Time - // as its wall clock with no zone (embedded Dolt's does), and the - // columns hold UTC wall clock, so a cursor in another zone would page - // from the wrong position there. It is bound twice under either order - // (the sargable upper bound and the strict bound), followed by the id - // tie-break. - ac := filter.AfterCreatedAt.UTC() + // Bind the cursor time as time.Time, not a formatted string: the issues/ + // wisps created_at columns are DATETIME (NUMERIC affinity), so an RFC3339 + // string parameter mis-compares on the SQLite backend, while a time.Time + // value compares correctly on every backend — the same binding EventsSince + // uses. It is bound twice under either order (the sargable upper bound + // and the strict bound), followed by the id tie-break. + ac := *filter.AfterCreatedAt // AfterCreatedAt alone decides that a position was supplied; AfterPriority // decides which ORDER it is a position in. The two predicates are // ALTERNATIVES, never conjuncts: ANDing them would keep the created diff --git a/internal/storage/sqlbuild/filter_test.go b/internal/storage/sqlbuild/filter_test.go index 08f3b2775..28db63ed0 100644 --- a/internal/storage/sqlbuild/filter_test.go +++ b/internal/storage/sqlbuild/filter_test.go @@ -1,11 +1,6 @@ package sqlbuild -import ( - "testing" - "time" - - "github.com/steveyegge/beads/internal/types" -) +import "testing" // TestGlobToLikePattern exercises globToLikePattern directly against the // package that actually calls it from BuildIssueFilterClauses (be-ucslk4). @@ -40,76 +35,3 @@ func TestGlobToLikePattern(t *testing.T) { }) } } - -// TestTimeBoundsBindTheUTCInstant pins that every time bound is bound as the -// UTC spelling of its instant, truncated to whole seconds. The columns hold -// UTC wall clock, and an engine whose column carries no zone (a SQL -// `timestamp` WITHOUT time zone) reads a literal's wall clock and drops its -// offset, so a bound spelled `T+02:00` would compare two hours late there. -// Binding the UTC spelling makes an offset bound select exactly what its Z -// twin does on every engine (Dolt already honors the offset; the UTC spelling -// of the same instant is the same comparison there). -func TestTimeBoundsBindTheUTCInstant(t *testing.T) { - t.Parallel() - plus2 := time.FixedZone("UTC+2", 2*60*60) - at := time.Date(2026, 10, 17, 12, 0, 0, 900_000_000, plus2) // 10:00:00.9Z - const want = "2026-10-17T10:00:00Z" - bound := func(f *types.IssueFilter) { - f.CreatedAfter, f.CreatedBefore = &at, &at - f.UpdatedAfter, f.UpdatedBefore = &at, &at - f.ClosedAfter, f.ClosedBefore = &at, &at - f.StartedAfter, f.StartedBefore = &at, &at - f.DeferAfter, f.DeferBefore = &at, &at - f.DueAfter, f.DueBefore = &at, &at - } - var filter types.IssueFilter - bound(&filter) - _, args, err := BuildIssueFilterClauses("", filter, IssuesFilterTables) - if err != nil { - t.Fatalf("BuildIssueFilterClauses: %v", err) - } - if len(args) != 12 { - t.Fatalf("args = %v, want the 12 time bounds", args) - } - for i, arg := range args { - if arg != want { - t.Errorf("arg %d = %v, want %q (the bound's UTC instant at whole seconds)", i, arg, want) - } - } -} - -// TestKeysetCursorBindsTheUTCInstant pins that the keyset cursor is bound as -// its instant in UTC, still as a time.Time. A driver can send a time.Time as -// its wall clock with no zone (embedded Dolt's does), and the columns hold UTC -// wall clock, so a cursor in another zone would page from the wrong position -// there. Both orders bind the cursor twice. -func TestKeysetCursorBindsTheUTCInstant(t *testing.T) { - t.Parallel() - plus2 := time.FixedZone("UTC+2", 2*60*60) - cur := time.Date(2026, 10, 17, 12, 0, 0, 0, plus2) - want := time.Date(2026, 10, 17, 10, 0, 0, 0, time.UTC) - p := 2 - for _, tc := range []struct { - name string - filter types.IssueFilter - nargs int - at []int // the cursor's positions in the bound args - }{ - {"created order", types.IssueFilter{AfterCreatedAt: &cur, AfterID: "bd-42"}, 3, []int{0, 1}}, - {"priority order", types.IssueFilter{AfterPriority: &p, AfterCreatedAt: &cur, AfterID: "bd-42"}, 5, []int{2, 3}}, - } { - _, args, err := BuildIssueFilterClauses("", tc.filter, IssuesFilterTables) - if err != nil { - t.Fatalf("%s: BuildIssueFilterClauses: %v", tc.name, err) - } - if len(args) != tc.nargs { - t.Fatalf("%s: args = %v, want %d", tc.name, args, tc.nargs) - } - for _, i := range tc.at { - got, ok := args[i].(time.Time) - if !ok || !got.Equal(want) || got.Location() != time.UTC { - t.Errorf("%s: arg %d = %v (%T), want the time.Time %v", tc.name, i, args[i], args[i], want) - } - } - } -} diff --git a/internal/storage/sqlbuild/ready.go b/internal/storage/sqlbuild/ready.go index 2b78693c8..b111765ca 100644 --- a/internal/storage/sqlbuild/ready.go +++ b/internal/storage/sqlbuild/ready.go @@ -152,15 +152,6 @@ func BuildReadyWorkWhere(filter types.WorkFilter, tables FilterTables, in ReadyW whereClauses = append(whereClauses, fmt.Sprintf("id NOT IN (%s)", placeholders)) } } - for start := 0; start < len(filter.ExcludeIDs); start += QueryBatchSize { - end := start + QueryBatchSize - if end > len(filter.ExcludeIDs) { - end = len(filter.ExcludeIDs) - } - placeholders, batchArgs := InPlaceholders(filter.ExcludeIDs[start:end]) - args = append(args, batchArgs...) - whereClauses = append(whereClauses, fmt.Sprintf("id NOT IN (%s)", placeholders)) - } if len(filter.Labels) > 0 { for _, label := range filter.Labels { diff --git a/internal/storage/sqlbuild/sort.go b/internal/storage/sqlbuild/sort.go index 50ab7182a..7f233976c 100644 --- a/internal/storage/sqlbuild/sort.go +++ b/internal/storage/sqlbuild/sort.go @@ -113,84 +113,16 @@ func OrderBy(sortBy string, sortDesc bool, table string) string { }) } -// sortFields holds exactly the columns Less/LessSummary compare. Sharing one -// comparator core (lessFields/sortKeyCompareFields) between types.Issue and -// types.IssueSummary means the correctness-critical NULL-first and tie-break -// rules are written once instead of drifting across two hand-copied switch -// statements. -type sortFields struct { - id string - priority int - createdAt time.Time - updatedAt time.Time - closedAt *time.Time - status string - issueType string - assignee string - title string -} - -func issueSortFields(i *types.Issue) sortFields { - return sortFields{ - id: i.ID, - priority: i.Priority, - createdAt: i.CreatedAt, - updatedAt: i.UpdatedAt, - closedAt: i.ClosedAt, - status: string(i.Status), - issueType: string(i.IssueType), - assignee: i.Assignee, - title: i.Title, - } -} - -func summarySortFields(s *types.IssueSummary) sortFields { - return sortFields{ - id: s.ID, - priority: s.Priority, - createdAt: s.CreatedAt, - updatedAt: s.UpdatedAt, - closedAt: s.ClosedAt, - status: string(s.Status), - issueType: string(s.IssueType), - assignee: s.Assignee, - title: s.Title, - } -} - // Less is the Go-side mirror of OrderBy for merge sorts over rows fetched // from separate queries (issues + wisps). It must order exactly the way the // SQL does, including NULL-first ascending semantics for nullable columns; // otherwise a post-merge limit cut keeps a different row set than SQL // selected. func Less(a, b *types.Issue, sortBy string, sortDesc bool) bool { - af, bf := issueSortFields(a), issueSortFields(b) - return lessFields(&af, &bf, sortBy, sortDesc) -} - -// LessSummary is Less's sibling for types.IssueSummary, used by the -// summaryProjection merge sort (internal/storage/issueops.searchInTx). It -// shares lessFields with Less so the two can never drift on the NULL/tie-break -// rules that make a post-merge limit cut safe. -func LessSummary(a, b *types.IssueSummary, sortBy string, sortDesc bool) bool { - af, bf := summarySortFields(a), summarySortFields(b) - return lessFields(&af, &bf, sortBy, sortDesc) -} - -// lessFields takes its operands by pointer, not by value. sortFields is a -// ~150-byte struct and this runs O(n log n) times inside the issues+wisps -// merge sort, so passing it by value copied it twice here and twice more into -// sortKeyCompareFields on every comparison. The adapters above still build one -// value per row — that normalization is what buys Less and LessSummary a -// single shared body — but nothing copies it again after that. The locals do -// not escape, so they stay on the stack and this adds no allocation. -func lessFields(a, b *sortFields, sortBy string, sortDesc bool) bool { if sortBy == "id" { // This key used to ignore sortDesc, so a reversed id merge kept the // byte-FIRST rows (the sibling bug idSrcPage.sortGoSide's doc named). - // Living in the shared body means LessSummary honors sortDesc too, - // rather than needing its own copy of the same fix. - return LessID(a.id, b.id, sortDesc) + return LessID(a.ID, b.ID, sortDesc) } def, ok := SortDefs[sortBy] if !ok { @@ -201,46 +133,46 @@ func lessFields(a, b *sortFields, sortBy string, sortDesc bool) bool { if sortDesc { descending = !descending } - if c := sortKeyCompareFields(a, b, sortBy); c != 0 { + if c := sortKeyCompare(a, b, sortBy); c != 0 { if descending { return c > 0 } return c < 0 } - if (sortBy == "" || sortBy == "priority") && !a.createdAt.Equal(b.createdAt) { - return a.createdAt.After(b.createdAt) + if (sortBy == "" || sortBy == "priority") && !a.CreatedAt.Equal(b.CreatedAt) { + return a.CreatedAt.After(b.CreatedAt) } - return a.id < b.id + return a.ID < b.ID } -// sortKeyCompareFields three-way compares the primary sort column in -// ascending order, with MySQL NULL-first semantics for nullable columns. -func sortKeyCompareFields(a, b *sortFields, sortBy string) int { +// sortKeyCompare three-way compares the primary sort column in ascending +// order, with MySQL NULL-first semantics for nullable columns. +func sortKeyCompare(a, b *types.Issue, sortBy string) int { switch sortBy { case "created": - return compareTimesAsc(a.createdAt, b.createdAt) + return compareTimesAsc(a.CreatedAt, b.CreatedAt) case "updated": - return compareTimesAsc(a.updatedAt, b.updatedAt) + return compareTimesAsc(a.UpdatedAt, b.UpdatedAt) case "closed": switch { - case a.closedAt == nil && b.closedAt == nil: + case a.ClosedAt == nil && b.ClosedAt == nil: return 0 - case a.closedAt == nil: + case a.ClosedAt == nil: return -1 - case b.closedAt == nil: + case b.ClosedAt == nil: return 1 } - return compareTimesAsc(*a.closedAt, *b.closedAt) + return compareTimesAsc(*a.ClosedAt, *b.ClosedAt) case "status": - return strings.Compare(a.status, b.status) + return strings.Compare(string(a.Status), string(b.Status)) case "type": - return strings.Compare(a.issueType, b.issueType) + return strings.Compare(string(a.IssueType), string(b.IssueType)) case "assignee": - return strings.Compare(a.assignee, b.assignee) + return strings.Compare(a.Assignee, b.Assignee) case "title": - return strings.Compare(strings.ToLower(a.title), strings.ToLower(b.title)) + return strings.Compare(strings.ToLower(a.Title), strings.ToLower(b.Title)) } - return a.priority - b.priority + return a.Priority - b.Priority } func compareTimesAsc(a, b time.Time) int { diff --git a/internal/storage/sqlbuild/sqlbuild_test.go b/internal/storage/sqlbuild/sqlbuild_test.go index ac2d9bfc2..5565b4544 100644 --- a/internal/storage/sqlbuild/sqlbuild_test.go +++ b/internal/storage/sqlbuild/sqlbuild_test.go @@ -1,9 +1,6 @@ package sqlbuild import ( - "fmt" - "slices" - "sort" "strings" "testing" "time" @@ -91,126 +88,6 @@ func TestLessIDHonorsSortDesc(t *testing.T) { } } -// TestLessSummaryMirrorsOrderBy is TestLessMirrorsOrderBy's sibling for -// LessSummary/types.IssueSummary. Less and LessSummary share the lessFields -// core, so this mostly guards against a future adapter (summarySortFields) -// dropping or mis-mapping a field rather than the tie-break logic itself. -func TestLessSummaryMirrorsOrderBy(t *testing.T) { - t.Parallel() - - now := time.Now().UTC() - older := now.Add(-time.Hour) - a := &types.IssueSummary{ID: "a", Priority: 1, CreatedAt: now} - b := &types.IssueSummary{ID: "b", Priority: 2, CreatedAt: now} - if !LessSummary(a, b, "", false) || LessSummary(b, a, "", false) { - t.Error("default sort must order priority 1 before priority 2") - } - c := &types.IssueSummary{ID: "c", Priority: 1, CreatedAt: older} - if !LessSummary(a, c, "", false) { - t.Error("equal priority must order newer created_at first (created_at DESC)") - } - d := &types.IssueSummary{ID: "d", Priority: 1, CreatedAt: now} - if !LessSummary(a, d, "", false) || LessSummary(d, a, "", false) { - t.Error("full tie must break by id ASC") - } -} - -// TestLessSummaryMatchesLessAcrossSortKeys is TestLessSummaryMirrorsOrderBy's -// broader sibling: instead of spot-checking only the default tie-break chain, -// it sorts a parallel Issue/IssueSummary fixture with Less and LessSummary -// for every SortDefs key (plus "id") in both directions and asserts the two -// produce identical ID order. Less and LessSummary already share the -// lessFields core, so this mainly guards summarySortFields: if a future field -// is added to sortFields without updating summarySortFields, the two -// adapters silently diverge and this is what would catch it. -func TestLessSummaryMatchesLessAcrossSortKeys(t *testing.T) { - t.Parallel() - - day := func(d int) time.Time { return time.Date(2026, 1, d, 0, 0, 0, 0, time.UTC) } - closedOn := func(d int) *time.Time { c := day(d); return &c } - - type row struct { - id string - priority int - createdAt time.Time - updatedAt time.Time - closedAt *time.Time - status types.Status - issueType types.IssueType - assignee string - title string - } - // Distinct (and, for priority, partially tied) values on every field so - // each sort key — including the tie-break chain the "" and "priority" - // keys share — produces a non-trivial, checkable order. - rows := []row{ - {"p1", 3, day(1), day(5), nil, types.StatusOpen, types.TypeBug, "alice", "Zebra"}, - {"p2", 1, day(3), day(2), closedOn(10), types.StatusBlocked, types.TypeEpic, "bob", "apple"}, - {"p3", 2, day(5), day(4), closedOn(8), types.StatusClosed, types.TypeTask, "carol", "Mango"}, - {"p4", 1, day(2), day(1), nil, types.StatusInProgress, types.TypeChore, "dave", "banana"}, - {"p5", 4, day(4), day(3), closedOn(6), types.StatusOpen, types.TypeBug, "eve", "Orange"}, - } - - issues := make([]*types.Issue, len(rows)) - summaries := make([]*types.IssueSummary, len(rows)) - for i, r := range rows { - issues[i] = &types.Issue{ - ID: r.id, Priority: r.priority, CreatedAt: r.createdAt, UpdatedAt: r.updatedAt, - ClosedAt: r.closedAt, Status: r.status, IssueType: r.issueType, Assignee: r.assignee, Title: r.title, - } - summaries[i] = &types.IssueSummary{ - ID: r.id, Priority: r.priority, CreatedAt: r.createdAt, UpdatedAt: r.updatedAt, - ClosedAt: r.closedAt, Status: r.status, IssueType: r.issueType, Assignee: r.assignee, Title: r.title, - } - } - - keys := make([]string, 0, len(SortDefs)+1) - for key := range SortDefs { - keys = append(keys, key) - } - keys = append(keys, "id") - - for _, key := range keys { - for _, desc := range []bool{false, true} { - name := key - if name == "" { - name = "default" - } - if desc { - name += "/desc" - } else { - name += "/asc" - } - t.Run(name, func(t *testing.T) { - t.Parallel() - - sortedIssues := append([]*types.Issue(nil), issues...) - sort.Slice(sortedIssues, func(i, j int) bool { - return Less(sortedIssues[i], sortedIssues[j], key, desc) - }) - sortedSummaries := append([]*types.IssueSummary(nil), summaries...) - sort.Slice(sortedSummaries, func(i, j int) bool { - return LessSummary(sortedSummaries[i], sortedSummaries[j], key, desc) - }) - - gotIssues := make([]string, len(sortedIssues)) - for i, is := range sortedIssues { - gotIssues[i] = is.ID - } - gotSummaries := make([]string, len(sortedSummaries)) - for i, s := range sortedSummaries { - gotSummaries[i] = s.ID - } - - if !slices.Equal(gotIssues, gotSummaries) { - t.Errorf("Less vs LessSummary order diverge for sortBy=%q sortDesc=%v:\n Less: %v\n LessSummary: %v", - key, desc, gotIssues, gotSummaries) - } - }) - } - } -} - func TestBuildReadyWorkOrderPriorityFIFO(t *testing.T) { t.Parallel() @@ -285,30 +162,6 @@ func TestBuildReadyWorkWhereBatchesIDSets(t *testing.T) { } } -func TestBuildReadyWorkWhereAppliesPolicyIDExclusions(t *testing.T) { - t.Parallel() - - ids := make([]string, QueryBatchSize+1) - for i := range ids { - ids[i] = fmt.Sprintf("be-external-%d", i) - } - where, args, err := BuildReadyWorkWhere( - types.WorkFilter{ExcludeIDs: ids}, - IssuesFilterTables, - ReadyWorkWhereInputs{}, - ) - if err != nil { - t.Fatalf("BuildReadyWorkWhere: %v", err) - } - if got := strings.Count(where, "id NOT IN ("); got != 2 { - t.Fatalf("external policy exclusions produced %d NOT IN clauses, want 2", got) - } - wantArgs := len(ids) + len(ReadyWorkExcludeTypes(nil)) - if len(args) != wantArgs { - t.Fatalf("args = %d, want %d", len(args), wantArgs) - } -} - // wy-jpd3.2: --label-any was silently dropped on the ready/claim path (with or // without --parent). BuildReadyWorkWhere must now emit an OR-set membership // clause for LabelsAny that AND-combines with the AND-set Labels and the parent diff --git a/internal/storage/sqlclass/BUILD.bazel b/internal/storage/sqlclass/BUILD.bazel deleted file mode 100644 index 3682f5ad7..000000000 --- a/internal/storage/sqlclass/BUILD.bazel +++ /dev/null @@ -1,89 +0,0 @@ -load("@rules_go//go:def.bzl", "go_library", "go_test") - -go_library( - name = "sqlclass", - srcs = ["sqlclass.go"], - importpath = "github.com/steveyegge/beads/internal/storage/sqlclass", - visibility = ["//:__subpackages__"], - deps = ["@com_github_dolthub_vitess//go/vt/sqlparser"], -) - -go_test( - name = "sqlclass_test", - srcs = ["sqlclass_test.go"], - embed = [":sqlclass"], -) - -# --- begin go_srcs (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) --- - -filegroup( - name = "go_srcs", - srcs = glob( - ["**/*.go"], - allow_empty = True, - ), - visibility = ["//:__subpackages__"], -) - -# --- end go_srcs --- - -# --- begin repo_files (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) --- - -filegroup( - name = "repo_doc_files", - srcs = glob( - [ - "**/*.md", - "**/*.mdx", - ], - allow_empty = True, - exclude = ["**/node_modules/**"], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_go_srcs", - srcs = glob( - ["**/*.go"], - allow_empty = True, - exclude = [ - "**/*_test.go", - "**/node_modules/**", - ], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_go_test_srcs", - srcs = glob( - ["**/*_test.go"], - allow_empty = True, - exclude = ["**/node_modules/**"], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_other_files", - srcs = glob( - ["**"], - exclude = [ - "**/*.db", - "**/*.exe", - "**/*.go", - "**/*.md", - "**/*.mdx", - "**/*.out", - "**/*.prof", - "**/*.pyc", - "**/*.test", - "**/__pycache__/**", - "**/node_modules/**", - ], - ), - visibility = ["//:__subpackages__"], -) - -# --- end repo_files --- diff --git a/internal/storage/sqlcount/BUILD.bazel b/internal/storage/sqlcount/BUILD.bazel deleted file mode 100644 index 3abb00ede..000000000 --- a/internal/storage/sqlcount/BUILD.bazel +++ /dev/null @@ -1,82 +0,0 @@ -load("@rules_go//go:def.bzl", "go_library") - -go_library( - name = "sqlcount", - srcs = ["sqlcount.go"], - importpath = "github.com/steveyegge/beads/internal/storage/sqlcount", - visibility = ["//:__subpackages__"], -) - -# --- begin go_srcs (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) --- - -filegroup( - name = "go_srcs", - srcs = glob( - ["**/*.go"], - allow_empty = True, - ), - visibility = ["//:__subpackages__"], -) - -# --- end go_srcs --- - -# --- begin repo_files (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) --- - -filegroup( - name = "repo_doc_files", - srcs = glob( - [ - "**/*.md", - "**/*.mdx", - ], - allow_empty = True, - exclude = ["**/node_modules/**"], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_go_srcs", - srcs = glob( - ["**/*.go"], - allow_empty = True, - exclude = [ - "**/*_test.go", - "**/node_modules/**", - ], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_go_test_srcs", - srcs = glob( - ["**/*_test.go"], - allow_empty = True, - exclude = ["**/node_modules/**"], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_other_files", - srcs = glob( - ["**"], - exclude = [ - "**/*.db", - "**/*.exe", - "**/*.go", - "**/*.md", - "**/*.mdx", - "**/*.out", - "**/*.prof", - "**/*.pyc", - "**/*.test", - "**/__pycache__/**", - "**/node_modules/**", - ], - ), - visibility = ["//:__subpackages__"], -) - -# --- end repo_files --- diff --git a/internal/storage/sqlcount/sqlcount.go b/internal/storage/sqlcount/sqlcount.go deleted file mode 100644 index 958a6b76a..000000000 --- a/internal/storage/sqlcount/sqlcount.go +++ /dev/null @@ -1,417 +0,0 @@ -// Package sqlcount wraps a database/sql/driver.Connector so a test or -// benchmark can count how many SQL statements a code path actually issues -// against a real driver, and optionally inject artificial per-statement -// latency to approximate a database that is not co-located with the caller. -// -// It exists for the large-batch-apply measurement work: issueops.ApplyBatchInTx -// takes a concrete *sql.Tx, not an interface, so the only seam available to -// count its real round trips is the database/sql/driver boundary itself. Wrapping the -// driver.Connector a backend already builds — mysql.NewConnector for the Dolt -// server backend, doltembed.NewConnector for the embedded backend — is the -// one construction point both backends share, so one wrapper measures both -// without touching either driver or any production code path. -package sqlcount - -import ( - "context" - "database/sql/driver" - "fmt" - "sync/atomic" - "time" -) - -// Counts tracks how many times each driver entry point that represents one -// SQL statement or round trip fired. The zero value is ready to use. Safe -// for concurrent use. -type Counts struct { - prepare int64 - exec int64 - query int64 - stmtExec int64 - stmtQuery int64 - begin int64 - commit int64 - rollback int64 -} - -// Snapshot is a point-in-time, plain copy of Counts, safe to diff, log or -// assert on without synchronization. -type Snapshot struct { - Prepare int64 - Exec int64 - Query int64 - StmtExec int64 - StmtQuery int64 - Begin int64 - Commit int64 - Rollback int64 -} - -// Total is every round trip this wrapper attributes to an executed SQL -// statement: a Prepare, a driver-level Exec/Query issued without a separate -// prepare, or an Exec/Query against an already-prepared statement. Begin, -// Commit and Rollback are reported on Snapshot but excluded here, because a -// caller measuring one transaction's BODY (issueops.ApplyBatchInTx, for -// instance) opens and closes that transaction outside the measured window. -func (s Snapshot) Total() int64 { - return s.Prepare + s.Exec + s.Query + s.StmtExec + s.StmtQuery -} - -// Sub returns the per-field difference s-prior, for isolating one call's -// contribution on a longer-lived connection. -func (s Snapshot) Sub(prior Snapshot) Snapshot { - return Snapshot{ - Prepare: s.Prepare - prior.Prepare, - Exec: s.Exec - prior.Exec, - Query: s.Query - prior.Query, - StmtExec: s.StmtExec - prior.StmtExec, - StmtQuery: s.StmtQuery - prior.StmtQuery, - Begin: s.Begin - prior.Begin, - Commit: s.Commit - prior.Commit, - Rollback: s.Rollback - prior.Rollback, - } -} - -// Snapshot reads every counter. The read is not atomic ACROSS fields — a -// concurrent writer could interleave between two loads — which matches every -// other multi-atomic snapshot in this tree taken for one log line or report. -func (c *Counts) Snapshot() Snapshot { - return Snapshot{ - Prepare: atomic.LoadInt64(&c.prepare), - Exec: atomic.LoadInt64(&c.exec), - Query: atomic.LoadInt64(&c.query), - StmtExec: atomic.LoadInt64(&c.stmtExec), - StmtQuery: atomic.LoadInt64(&c.stmtQuery), - Begin: atomic.LoadInt64(&c.begin), - Commit: atomic.LoadInt64(&c.commit), - Rollback: atomic.LoadInt64(&c.rollback), - } -} - -// Reset zeroes every counter, for reuse across benchmark iterations. -func (c *Counts) Reset() { - atomic.StoreInt64(&c.prepare, 0) - atomic.StoreInt64(&c.exec, 0) - atomic.StoreInt64(&c.query, 0) - atomic.StoreInt64(&c.stmtExec, 0) - atomic.StoreInt64(&c.stmtQuery, 0) - atomic.StoreInt64(&c.begin, 0) - atomic.StoreInt64(&c.commit, 0) - atomic.StoreInt64(&c.rollback, 0) -} - -// Total is c.Snapshot().Total(), for a caller that wants one number without -// composing the two calls. -func (c *Counts) Total() int64 { return c.Snapshot().Total() } - -// options carries WrapConnector's optional behavior. -type options struct { - latency time.Duration -} - -// Option configures WrapConnector. -type Option func(*options) - -// WithLatency injects a sleep before every counted round trip — Prepare, -// Exec, Query, a prepared statement's Exec/Query, Begin, Commit and Rollback -// alike — so a co-located backend can approximate the wall-clock cost of a -// database that is NOT co-located with the caller. It respects the caller's -// context: a canceled or expired context aborts the sleep and returns the -// context's error rather than padding out a call that is already doomed. -func WithLatency(d time.Duration) Option { - return func(o *options) { o.latency = d } -} - -// WrapConnector wraps inner so every statement-shaped call on the -// connections it hands out increments counts, and optionally sleeps first. -// inner is the driver.Connector a backend already constructs -// (mysql.NewConnector for the Dolt server backend, doltembed.NewConnector -// for the embedded backend); wrapping it rather than registering a new -// driver under a new name is what lets a test open a SECOND, counted -// connection against the SAME already-migrated embedded directory or the -// SAME live Dolt server a backend's own store already opened, so the -// production open/migrate path never has to know this wrapper exists. -func WrapConnector(inner driver.Connector, counts *Counts, opts ...Option) driver.Connector { - o := &options{} - for _, opt := range opts { - opt(o) - } - return &wrappedConnector{inner: inner, counts: counts, opts: o} -} - -type wrappedConnector struct { - inner driver.Connector - counts *Counts - opts *options -} - -func (c *wrappedConnector) Connect(ctx context.Context) (driver.Conn, error) { - inner, err := c.inner.Connect(ctx) - if err != nil { - return nil, err - } - return &wrappedConn{inner: inner, counts: c.counts, opts: c.opts}, nil -} - -func (c *wrappedConnector) Driver() driver.Driver { - return &wrappedDriver{inner: c.inner.Driver(), counts: c.counts, opts: c.opts} -} - -// wrappedDriver exists only to satisfy driver.Connector.Driver's return -// type. database/sql calls Driver() for error classification and -// sql.DB.Driver(); this wrapper never uses it to open a second connection -// outside wrappedConnector.Connect. -type wrappedDriver struct { - inner driver.Driver - counts *Counts - opts *options -} - -func (d *wrappedDriver) Open(name string) (driver.Conn, error) { - inner, err := d.inner.Open(name) - if err != nil { - return nil, err - } - return &wrappedConn{inner: inner, counts: d.counts, opts: d.opts}, nil -} - -// sleep injects the configured latency, honoring ctx cancellation. -func (o *options) sleep(ctx context.Context) error { - if o.latency <= 0 { - return nil - } - t := time.NewTimer(o.latency) - defer t.Stop() - select { - case <-t.C: - return nil - case <-ctx.Done(): - return ctx.Err() - } -} - -// wrappedConn wraps a driver.Conn. It implements every optional modern -// interface (ExecerContext, QueryerContext, ConnPrepareContext, -// ConnBeginTx, Pinger, SessionResetter, Validator, NamedValueChecker), -// delegating to the inner connection's implementation when present and -// falling back to the legacy path — or driver.ErrSkip — when not, so -// database/sql's behavior with the wrapper in place matches its behavior -// with the inner driver alone. -// -// NamedValueChecker is always implemented (never omitted) because -// database/sql decides parameter-conversion behavior by checking interface -// satisfaction on the WRAPPER's concrete type, not the inner driver's: -// omitting it here would silently change conversion behavior relative to -// running against the inner driver directly, regardless of what the inner -// driver itself implements. -type wrappedConn struct { - inner driver.Conn - counts *Counts - opts *options -} - -func (c *wrappedConn) Prepare(query string) (driver.Stmt, error) { - atomic.AddInt64(&c.counts.prepare, 1) - inner, err := c.inner.Prepare(query) - if err != nil { - return nil, err - } - return &wrappedStmt{inner: inner, counts: c.counts, opts: c.opts}, nil -} - -func (c *wrappedConn) Close() error { return c.inner.Close() } - -//nolint:staticcheck // legacy driver.Conn.Begin, required by the interface; BeginTx below is preferred when available. -func (c *wrappedConn) Begin() (driver.Tx, error) { - atomic.AddInt64(&c.counts.begin, 1) - inner, err := c.inner.Begin() //nolint:staticcheck - if err != nil { - return nil, err - } - return &wrappedTx{inner: inner, counts: c.counts}, nil -} - -func (c *wrappedConn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error) { - pc, ok := c.inner.(driver.ConnPrepareContext) - if !ok { - return c.Prepare(query) - } - if err := c.opts.sleep(ctx); err != nil { - return nil, err - } - atomic.AddInt64(&c.counts.prepare, 1) - inner, err := pc.PrepareContext(ctx, query) - if err != nil { - return nil, err - } - return &wrappedStmt{inner: inner, counts: c.counts, opts: c.opts}, nil -} - -func (c *wrappedConn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) { - execer, ok := c.inner.(driver.ExecerContext) - if !ok { - return nil, driver.ErrSkip - } - if err := c.opts.sleep(ctx); err != nil { - return nil, err - } - atomic.AddInt64(&c.counts.exec, 1) - return execer.ExecContext(ctx, query, args) -} - -func (c *wrappedConn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) { - queryer, ok := c.inner.(driver.QueryerContext) - if !ok { - return nil, driver.ErrSkip - } - if err := c.opts.sleep(ctx); err != nil { - return nil, err - } - atomic.AddInt64(&c.counts.query, 1) - return queryer.QueryContext(ctx, query, args) -} - -func (c *wrappedConn) BeginTx(ctx context.Context, txOpts driver.TxOptions) (driver.Tx, error) { - bt, ok := c.inner.(driver.ConnBeginTx) - if !ok { - return c.Begin() - } - if err := c.opts.sleep(ctx); err != nil { - return nil, err - } - atomic.AddInt64(&c.counts.begin, 1) - inner, err := bt.BeginTx(ctx, txOpts) - if err != nil { - return nil, err - } - return &wrappedTx{inner: inner, counts: c.counts}, nil -} - -func (c *wrappedConn) Ping(ctx context.Context) error { - if p, ok := c.inner.(driver.Pinger); ok { - return p.Ping(ctx) - } - return nil -} - -func (c *wrappedConn) ResetSession(ctx context.Context) error { - if r, ok := c.inner.(driver.SessionResetter); ok { - return r.ResetSession(ctx) - } - return nil -} - -func (c *wrappedConn) IsValid() bool { - if v, ok := c.inner.(driver.Validator); ok { - return v.IsValid() - } - return true -} - -func (c *wrappedConn) CheckNamedValue(nv *driver.NamedValue) error { - if chk, ok := c.inner.(driver.NamedValueChecker); ok { - return chk.CheckNamedValue(nv) - } - return driver.ErrSkip -} - -// wrappedStmt wraps a driver.Stmt the same way wrappedConn wraps a -// driver.Conn: delegate to a modern optional interface when the inner -// statement implements it, else fall back to the legacy path, and always -// implement NamedValueChecker for the reason documented on wrappedConn. -type wrappedStmt struct { - inner driver.Stmt - counts *Counts - opts *options -} - -func (s *wrappedStmt) Close() error { return s.inner.Close() } -func (s *wrappedStmt) NumInput() int { return s.inner.NumInput() } - -//nolint:staticcheck // legacy driver.Stmt.Exec, required by the interface and used as the ExecContext fallback below. -func (s *wrappedStmt) Exec(args []driver.Value) (driver.Result, error) { - atomic.AddInt64(&s.counts.stmtExec, 1) - return s.inner.Exec(args) //nolint:staticcheck -} - -//nolint:staticcheck // legacy driver.Stmt.Query, required by the interface and used as the QueryContext fallback below. -func (s *wrappedStmt) Query(args []driver.Value) (driver.Rows, error) { - atomic.AddInt64(&s.counts.stmtQuery, 1) - return s.inner.Query(args) //nolint:staticcheck -} - -func (s *wrappedStmt) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) { - if err := s.opts.sleep(ctx); err != nil { - return nil, err - } - if se, ok := s.inner.(driver.StmtExecContext); ok { - atomic.AddInt64(&s.counts.stmtExec, 1) - return se.ExecContext(ctx, args) - } - dargs, err := namedToOrdinal(args) - if err != nil { - return nil, err - } - atomic.AddInt64(&s.counts.stmtExec, 1) - return s.inner.Exec(dargs) //nolint:staticcheck -} - -func (s *wrappedStmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) { - if err := s.opts.sleep(ctx); err != nil { - return nil, err - } - if sq, ok := s.inner.(driver.StmtQueryContext); ok { - atomic.AddInt64(&s.counts.stmtQuery, 1) - return sq.QueryContext(ctx, args) - } - dargs, err := namedToOrdinal(args) - if err != nil { - return nil, err - } - atomic.AddInt64(&s.counts.stmtQuery, 1) - return s.inner.Query(dargs) //nolint:staticcheck -} - -func (s *wrappedStmt) CheckNamedValue(nv *driver.NamedValue) error { - if chk, ok := s.inner.(driver.NamedValueChecker); ok { - return chk.CheckNamedValue(nv) - } - return driver.ErrSkip -} - -// namedToOrdinal converts modern named-value args back to the legacy -// ordinal-positioned driver.Value slice Exec/Query take, for an inner -// statement that implements neither StmtExecContext nor StmtQueryContext. -// Every driver this wrapper targets (mysql, dolthub/driver) implements the -// modern interfaces, so this path is exercised only as a defensive -// fallback, never in the measured code path. -func namedToOrdinal(args []driver.NamedValue) ([]driver.Value, error) { - out := make([]driver.Value, len(args)) - for _, a := range args { - if a.Name != "" { - return nil, fmt.Errorf("sqlcount: named parameter %q not supported by the wrapped driver's legacy Exec/Query", a.Name) - } - if a.Ordinal < 1 || a.Ordinal > len(args) { - return nil, fmt.Errorf("sqlcount: parameter ordinal %d out of range for %d args", a.Ordinal, len(args)) - } - out[a.Ordinal-1] = a.Value - } - return out, nil -} - -// wrappedTx wraps a driver.Tx purely to count Commit/Rollback; it does not -// change the transaction's behavior. -type wrappedTx struct { - inner driver.Tx - counts *Counts -} - -func (t *wrappedTx) Commit() error { - atomic.AddInt64(&t.counts.commit, 1) - return t.inner.Commit() -} - -func (t *wrappedTx) Rollback() error { - atomic.AddInt64(&t.counts.rollback, 1) - return t.inner.Rollback() -} diff --git a/internal/storage/storage.go b/internal/storage/storage.go index 1ea0d8d86..7754e1eaa 100644 --- a/internal/storage/storage.go +++ b/internal/storage/storage.go @@ -10,7 +10,6 @@ import ( "database/sql" "encoding/json" "errors" - "fmt" "time" "github.com/steveyegge/beads/internal/types" @@ -37,17 +36,12 @@ var ( ErrAlreadyIdentified = issueops.ErrAlreadyIdentified ErrVersionMismatch = issueops.ErrVersionMismatch ErrStatusMismatch = issueops.ErrStatusMismatch - ErrNotesOverwrite = issueops.ErrNotesOverwrite ) // CloseOpenChildrenError reports the issue and open-child count that refused a // guarded close. See issueops.CloseOpenChildrenError. type CloseOpenChildrenError = issueops.CloseOpenChildrenError -// VersionMismatchError reports the expected and current row versions that -// refused a guarded write. See issueops.VersionMismatchError. -type VersionMismatchError = issueops.VersionMismatchError - // ErrNotOwner is returned when an actor tries to unclaim an issue that is claimed // by a different actor. Releasing another actor's claim requires the force // escape hatch (bd unclaim --force), reserved for admin/reaper use. @@ -91,32 +85,6 @@ const ( NotClaimableStatusFragment = ": status " ) -// ErrCredentialKeyMismatch is returned when a stored federation peer's password -// cannot be decrypted with the local credential key. federation_peers rows -// travel with the database, but the key file is machine-local and gitignored, -// so a database pulled from another machine carries peer passwords this -// machine's key cannot read. Callers errors.Is it to tell that local key -// problem apart from an unreachable peer. -var ErrCredentialKeyMismatch = errors.New("stored peer credentials cannot be decrypted with this machine's credential key") - -// CredentialKeyMismatchError wraps a federation password decrypt failure with -// the ErrCredentialKeyMismatch sentinel, the machine-local nature of keyFile, -// and the command that re-stores the password on this machine. Both backends -// (dolt, embeddeddolt) call it with their own credentialKeyFile so the operator -// reads one wording whichever store answered. -// -// The machine-local key is context, not an asserted cause: an AES-GCM open also -// fails on a tampered or truncated blob and on a row still encrypted under the -// pre-migration legacy key. Re-adding the peer is the fix in every one of those -// cases, so the remediation holds even where the parenthetical does not. -// -// cause stays wrapped, so the raw cipher error remains readable and both the -// sentinel and cause are reachable through errors.Is / errors.As. -func CredentialKeyMismatchError(keyFile string, cause error) error { - return fmt.Errorf("%w (the key file %s is machine-local and does not replicate with the database); re-run 'bd federation add-peer --user ' on this machine: %w", - ErrCredentialKeyMismatch, keyFile, cause) -} - // CommentPageCursor is the resume position for a keyset page of an issue's // comments: the (created_at, id) of the last comment already returned. The zero // value starts a walk from the beginning of the thread. @@ -286,12 +254,6 @@ type Storage interface { // EdgeReader (that one answers with the stored ROWS, outbound only). Reads // fire no hooks, as for IssueReader. GraphCounter() (issueops.GraphCounter, error) - // BatchGetter returns the guarded batch-read surface for this store: many - // issues by id, hydrated, in ONE snapshot. Its own role rather than a mode - // of IssueReader because a miss here is a RESULT FIELD (Missing) and not - // ErrNotFound — a batch GET is a set read, not a precondition on every - // member succeeding. Reads fire no hooks, as for IssueReader. - BatchGetter() (issueops.BatchGetter, error) // ReadyCounter returns the guarded ready-count surface for this store: the // size of the ready set, which is the number `bd ready`'s pagination // publishes and which no other role answers. Counter's predicate is a @@ -378,41 +340,6 @@ type Storage interface { // assignee and status, which is on_update — the same event the journal // already records for it. See hook_releaser.go. Releaser() (issueops.Releaser, error) - // LeaseReclaimer returns the lease-sweep surface for this store: revert - // every lease that has been held past its deadline back to its - // pre-claim state, for a caller that is a clock rather than a human or a - // request. - // - // It is a WRITE role and its hook decorator WRAPS: a reclaim changes - // assignee and status per reverted row, which is on_update, the same - // event Releaser fires. See hook_lease_reclaimer.go. - LeaseReclaimer() (issueops.LeaseReclaimer, error) - // Heartbeater returns the lease-refresh surface for this store: push the - // lease the caller holds on one issue forward by the store's TTL. - // - // It is a WRITE role and its hook decorator WRAPS: a refresh writes only - // the ephemeral lease row and fires nothing, but a re-arm (a live claim - // with no lease row, HeartbeatResult.Rearmed) is recorded as a mutation - // and fires on_update once. That is why RoleFiresHooks answers true for - // it, and a server must take it off a peeled store. See - // hook_heartbeater.go. - Heartbeater() (issueops.Heartbeater, error) - // BlockedLister returns the blocked-listing surface for this store: what - // `bd blocked` prints. It is a READ role and its hook decorator passes it - // through (hook_blocked_lister.go); the external-dependency decorator - // overrides it so the listing names unsatisfied external blockers, as its - // GetBlockedIssues does. - BlockedLister() (issueops.BlockedLister, error) - - // MoleculeStepper returns the advance-a-molecule surface for this store: - // find a molecule's next ready step after one closed and, on request, - // claim it — the operation behind `bd close --continue`. Every local - // backend answers issueops.NewMoleculeStepper composed over its own - // roles; a remote backend serves it over its wire. - // - // It is a WRITE role and its hook decorator WRAPS: a claimed step fires - // on_update, as every claim does. - MoleculeStepper() (issueops.MoleculeStepper, error) // Issue CRUD CreateIssue(ctx context.Context, issue *types.Issue, actor string) error @@ -433,9 +360,6 @@ type Storage interface { // when the current assignee differs. UnclaimIssueIfAssignee(ctx context.Context, id string, actor string, expectedAssignee string) error UpdateIssueType(ctx context.Context, id string, issueType string, actor string) error - // CloseIssue is the raw close: it applies neither the close guards nor - // close policy, except over HTTP, where it closes with force and a template - // — the one guard force never bypasses — still refuses. CloseIssue(ctx context.Context, id string, reason string, actor string, session string) error // CloseIssueChecked closes an issue, but refuses with ErrCloseOpenChildren // when it has open parent-child dependents, or ErrCloseBlocked when it has a @@ -447,16 +371,9 @@ type Storage interface { // (no TOCTOU). When opts.ExpectedVersion is non-nil it adds an orthogonal // optimistic-concurrency precondition: the close proceeds only if the issue's // current RowVersion still equals *opts.ExpectedVersion, else it refuses with - // ErrVersionMismatch atomically (Force does NOT bypass this check). After - // the version check and before the child and blocker policy it applies the - // close guards in the same transaction: a template refuses with - // *issueops.TemplateReadOnlyError whatever Force says, and unless Force is - // set a pinned issue refuses with *issueops.PinnedError and one assigned to - // someone other than actor with *issueops.CloseNotAssigneeError. Already- - // closed is an idempotent success with Unchanged=true and skips the guards; - // a missing issue returns ErrNotFound. CloseIssue above is the raw close and - // applies neither the guards nor the policy, except that over HTTP, where - // the template guard has no bypass on the wire, a template still refuses. + // ErrVersionMismatch atomically (Force does NOT bypass this check). Already- + // closed is an idempotent success with Unchanged=true; a missing issue returns + // ErrNotFound. CloseIssueChecked(ctx context.Context, id string, actor string, opts CloseIssueOptions) (CloseIssueResult, error) DeleteIssue(ctx context.Context, id string) error SearchIssues(ctx context.Context, query string, filter types.IssueFilter) ([]*types.Issue, error) @@ -465,29 +382,6 @@ type Storage interface { // returns only matching issue IDs. Use when full row hydration is wasted // (e.g., partial-ID resolution in internal/utils/id_parser.go). SearchIssueIDs(ctx context.Context, query string, filter types.IssueFilter) ([]string, error) - // SearchIssueSummaries is a narrow-projection variant of SearchIssues that - // returns []*types.IssueSummary instead of full issues, for list-shaped - // rendering paths that never dereference TEXT/JSON columns. SortBy/SortDesc - // are honored identically to SearchIssues — including SortBy=="id", which - // both paths deliberately leave to the caller: sqlbuild.OrderBy emits no - // ORDER BY for it (sqlbuild.IsGoSideSort), so storage orders id-sorted rows - // only where the issues+wisps merge re-sorts them (sqlbuild.LessSummary, - // mirroring sqlbuild.Less for SearchIssues). The user-facing id order is - // cmd/bd's, applied post-call via utils.NaturalCompareIDs — numeric-aware - // and intentionally different from the lexicographic storage comparator, so - // a storage-side id sorter here would diverge from SearchIssues rather than - // complete it. - // - // Two filter fields behave differently here than on SearchIssues, both - // because types.IssueSummary has no Dependencies field to populate: - // IncludeDependencies is a silent no-op (SearchIssues hydrates dependency - // records; this path has nowhere to put them, so a caller that needs them - // must use SearchIssues), while SkipLabels is honored exactly as it is - // there. Wisps are NOT excluded: the issues+wisps merge runs for any filter - // that does not set SkipWisps, and types.IssueSummary carries the four - // wisp-plane markers so a merged wisp row serializes identically to the one - // SearchIssues would return. - SearchIssueSummaries(ctx context.Context, query string, filter types.IssueFilter) ([]*types.IssueSummary, error) // Dependencies AddDependency(ctx context.Context, dep *types.Dependency, actor string) error @@ -513,29 +407,10 @@ type Storage interface { RemoveLabel(ctx context.Context, issueID, label, actor string) error GetLabels(ctx context.Context, issueID string) ([]string, error) GetIssuesByLabel(ctx context.Context, label string) ([]*types.Issue, error) - // RenameLabel renames a label across every issue and wisp that carries - // it. An issue that already carries newLabel is a merge (the stale - // oldLabel row is dropped, no error) rather than a duplicate-key - // conflict. renamed is the count of issues and wisps that carried - // oldLabel; merged is the subset that already had newLabel; ids lists - // every touched id, both planes together. oldLabel carried by nothing is - // an honest no-op: renamed and merged are 0, ids is nil, err is nil. - // oldLabel and newLabel equal after trimming is refused with - // issueops.ErrRenameLabelSameName instead of treated as a no-op - the - // merge branch above would otherwise wipe the label instead of leaving - // it alone (every carrier already "has newLabel", so the stale-row drop - // removes every row). - RenameLabel(ctx context.Context, oldLabel, newLabel, actor string) (renamed, merged int, ids []string, err error) // Work queries GetReadyWork(ctx context.Context, filter types.WorkFilter) ([]*types.Issue, error) GetReadyWorkWithCounts(ctx context.Context, filter types.WorkFilter) ([]*types.IssueWithCounts, error) - // GetReadyWorkWithCountsAndTotal is GetReadyWorkWithCounts plus the size - // of the whole ready set the page was cut from — the same number - // ReadyWorkCounter.CountReadyWork(filter) answers — resolved in the same - // read transaction and statements as the page. It is what lets - // `bd ready --limit N` print "Showing N of M" without a second pass. - GetReadyWorkWithCountsAndTotal(ctx context.Context, filter types.WorkFilter) ([]*types.IssueWithCounts, int, error) GetBlockedIssues(ctx context.Context, filter types.WorkFilter) ([]*types.BlockedIssue, error) GetEpicsEligibleForClosure(ctx context.Context) ([]*types.EpicStatus, error) @@ -683,7 +558,7 @@ type Storage interface { type CloseIssueOptions struct { Reason string Session string - Force bool // bypass close policy and the pin and assignee guards, never the template (mirrors `bd close --force`) + Force bool // bypass the is_blocked guard (mirrors `bd close --force`) // ExpectedVersion, when non-nil, gates the close on an optimistic-concurrency // check: the close proceeds only if the issue's current RowVersion (the // row_lock token) equals *ExpectedVersion, otherwise it refuses with @@ -810,14 +685,6 @@ type ActiveDatabaseSizer interface { ActiveDatabaseSize(ctx context.Context) (int64, error) } -// ExternalGCLocator supplies the authoritative local working directory for -// the active database's external garbage-collection tool. Implementations -// return *ErrUnsupported when this instance cannot authorize that operation. -// Neither a readable size nor a general store path grants this capability. -type ExternalGCLocator interface { - ExternalGCPath(ctx context.Context) (string, error) -} - // GarbageCollector provides Dolt garbage collection capability. // Callers that need to reclaim disk space should type-assert to this interface. type GarbageCollector interface { @@ -966,30 +833,6 @@ type EventsJournalConfigurer interface { SetEventsJournalEnabled(enabled bool) } -// VersionedHistoryConfigurer controls dual-write issue-version history -// activation on ONE storage instance. Implementations must never use -// process-global state, for the same reason as EventsJournalConfigurer: -// a process can hold several stores at once, and enabling history on one must -// not turn it on for any other. Callers type-assert; a store that does not -// implement it simply cannot record version history. -// -// SINGLE WRITER ONLY until the version_id primary-key swap lands. -// issue_versions is keyed PRIMARY KEY (issue_id, revision), and revision is -// a local ordinal (MAX(revision)+1 inside the writing transaction), so two -// disconnected writers that each mint the same ordinal for the same issue -// collide on merge — and TryAutoResolveMergeConflicts -// (versioncontrolops/mergesettle.go) fails the pull for a table it does not -// know. Enabling versioned history is therefore safe only with a SINGLE -// writer per store until migration 0068 steps 1-3 (UUID version_id primary -// key, ordinal demoted to an index) land; those steps follow as their own -// PR. "Single writer" means one writer at a time per store, not merely one -// clone: MAX(revision)+1 is not a safe allocator for two concurrent -// transactions in one store either (gastownhall/beads#6379, item 4). See -// issueops/version_history.go for why the ordinal is never an address. -type VersionedHistoryConfigurer interface { - SetVersionedHistoryEnabled(enabled bool) -} - // LifecycleManager provides lifecycle inspection beyond Close(). type LifecycleManager interface { IsClosed() bool @@ -1041,178 +884,6 @@ type ReadyWorkCounter interface { CountReadyWork(ctx context.Context, filter types.WorkFilter) (int, error) } -// ExternalDependencyQueryStore returns the narrow set of explicit external -// blocking edges. Policy decorators use this instead of scanning every graph -// edge on each ready-work query. -type ExternalDependencyQueryStore interface { - GetExternalBlockingDependencyRecords(ctx context.Context) (map[string][]*types.Dependency, error) -} - -// BlockersBatchStore is IsBlocked for many ids in a bounded number of reads. -// The answer holds exactly the ids IsBlocked reports blocked, each mapped to -// the blocker list IsBlocked returns for it (nil when it has none); an id -// IsBlocked reports unblocked, or that names no row, is absent. Decorators -// that annotate a page of ids (the external-dependency policy's -// BlockingAnnotator) use it instead of one IsBlocked per id, and fall back to -// that loop on a store without it. It answers for the raw store alone, with no -// external-dependency policy applied (no `external:` blocker is ever in it), so -// it is meant to be called from inside that policy's decorator, not probed -// through UnwrapStore by a caller holding the decorated store. -type BlockersBatchStore interface { - IsBlockedWithBlockersBatch(ctx context.Context, ids []string) (map[string][]string, error) -} - -// RemoteBackendStore is implemented by a DoltStorage that is a pure network -// client of a remote bd serve process (a registered backend whose -// backends.Backend.Remote is true — see internal/storage/backends). It is -// metadata about the store's transport, not a policy decision. Such a store -// serves its ROLES natively and only a slice of the legacy DoltStorage -// method seam, so a decorator that builds role views must compose over the -// inner store's roles rather than rebuild them from that seam: the -// external-deps decorator consults it for exactly that (remote_roles.go). -// It never decides whether a policy runs — the decorator asks -// ExternalDependencyPolicyProber whether to skip its client-side enforcement. -type RemoteBackendStore interface { - IsRemoteBackendStore() bool -} - -// CallerAttributionLimitedStore is implemented by a DoltStorage whose write -// transport cannot carry two caller-authored attribution members: -// -// - a history entry's Provenance label (UpdateRequest.Provenance, -// ReopenRequest.Provenance), because the serving process labels the -// entries it writes itself; -// - a patch's ClosedBySession, which only the close operation carries. -// -// Its role implementations refuse those members rather than drop them, since a -// library caller may mean them. A front door that knows its own value is -// derivable or advisory — `bd reopen`'s fixed "bd: reopen " label, the -// ambient CLAUDE_SESSION_ID on `bd update -s closed` — omits it for such a -// store instead of failing the write; cmd/bd/remote_backend.go records -// which members it omits and why. -type CallerAttributionLimitedStore interface { - CallerAttributionLimited() bool -} - -// Pinger is implemented by a DoltStorage whose cheapest honest liveness check -// is not an issue query. `bd ping` uses it when present and falls back to a -// one-row SearchIssues otherwise. A remote store implements it with its -// authenticated handshake read, which reaches the serving process and checks -// the credential and the workspace pin without touching an issue table its -// legacy method seam may not serve at all. -type Pinger interface { - Ping(ctx context.Context) error -} - -// ExternalDependencyPolicyProber is implemented by a DoltStorage that can -// answer whether the remote server it talks to already enforces bd's -// external-dependency policy itself (design 3.6, "External-dependency server -// policy": upstream's storage.ServerEnforcedPolicy / PolicyEnforcedByServer). -// The external-deps decorator consults this — never RemoteBackendStore, which -// only shapes how it composes its roles — before skipping its own client-side -// enforcement: a remote store whose -// server advertises the capability (httpapi's policy.external_dependencies) -// has already enforced the policy before answering, so a second client-side -// pass would be redundant. A store that does not implement this interface, or -// that implements it and reports false — including a remote store whose -// server is silent on the capability — gets the ordinary client-side -// enforcement. The policy is never silently skipped merely because the store -// is remote. -type ExternalDependencyPolicyProber interface { - ServerEnforcesExternalDependencyPolicy(ctx context.Context) (bool, error) -} - -type livePolicyProbeKey struct{} - -// WithLivePolicyProbe marks ctx as carrying a claim or a close: an -// ExternalDependencyPolicyProber asked under it must answer from the server, -// never from a cache another process wrote. A read can tolerate a cached -// answer for its TTL; a write guard cannot, since a stale "the server -// enforces it" would skip the client-side refusal of externally blocked work. -func WithLivePolicyProbe(ctx context.Context) context.Context { - return context.WithValue(ctx, livePolicyProbeKey{}, true) -} - -// LivePolicyProbe reports whether ctx was marked by WithLivePolicyProbe. -func LivePolicyProbe(ctx context.Context) bool { - live, _ := ctx.Value(livePolicyProbeKey{}).(bool) - return live -} - -// MoleculeAutoCloseProber is implemented by a DoltStorage whose closes may not -// accept issueops.CloseRequest.AutoCloseMolecule — today exactly -// httpclient.Store, whose server advertises it with -// issues.close.autoCloseMolecule. Every local backend runs the auto-close in -// the close's own transaction and does not implement this. -// -// A server that does not advertise it (an older bd serve, or a server whose -// orchestrator owns the molecule lifecycle, as gc does) is answered false, and -// the caller closes WITHOUT the flag: asking for it there is refused before -// dialing, and the molecule's auto-close is that server's to run. -type MoleculeAutoCloseProber interface { - ServesMoleculeAutoClose(ctx context.Context) (bool, error) -} - -// ServesMoleculeAutoClose reports whether closes through s may set -// issueops.CloseRequest.AutoCloseMolecule (MoleculeAutoCloseProber, looked up -// through every decorator). A store that does not implement the prober runs -// the auto-close itself and is answered true. A probe that fails is answered -// true as well: the close dials the same handshake and reports its failure, -// so nothing is hidden by asking for the auto-close. -func ServesMoleculeAutoClose(ctx context.Context, s DoltStorage) bool { - prober, ok := UnwrapStore(s).(MoleculeAutoCloseProber) - if !ok { - return true - } - served, err := prober.ServesMoleculeAutoClose(ctx) - return err != nil || served -} - -// MoleculeAdvanceProber is implemented by a DoltStorage whose MoleculeStepper -// may not be served — today exactly httpclient.Store, whose server advertises -// it with issues.advanceMolecule. Every local backend composes the stepper -// over its own roles and does not implement this. -type MoleculeAdvanceProber interface { - ServesMoleculeAdvance(ctx context.Context) (bool, error) -} - -// ServesMoleculeAdvance reports whether s's MoleculeStepper can run -// (MoleculeAdvanceProber, looked up through every decorator). A store that -// does not implement the prober runs the advance itself and is answered true. -// A probe that fails returns its error. -// -// `bd close --continue` asks BEFORE it closes: the advance is a second -// operation after the close commits, so refusing it only then would leave -// the step closed and the next step unclaimed. That is also why a failed -// probe is not answered true, as ServesMoleculeAutoClose answers it: the -// close would commit and only the advance, dialing the same handshake, would -// fail — the half-done state the question exists to avoid. -func ServesMoleculeAdvance(ctx context.Context, s DoltStorage) (bool, error) { - prober, ok := UnwrapStore(s).(MoleculeAdvanceProber) - if !ok { - return true, nil - } - return prober.ServesMoleculeAdvance(ctx) -} - -// ExcludeIDsUnsupportedStore is implemented by a DoltStorage whose ready-work -// reads cannot express types.WorkFilter.ExcludeIDs over their own transport. -// Today that is exactly httpclient.Store: the v0 wire's listReadyWork and -// countReadyWork operations publish no id-exclusion parameter, so the http -// bridge refuses rather than silently widening the result set whenever a -// filter carries any (design 3.6 / L12, "every field it cannot express -// refuses"). -// -// The external-deps decorator consults this before deciding how to apply its -// OWN additional exclusions (issues blocked by an unsatisfied -// external:: dependency): a store that answers true here -// gets those exclusions applied client-side in Go instead of folded into the -// filter handed down, so the policy still runs — design 3.6 says it is never -// silently skipped — without tripping the wire's own refusal. -type ExcludeIDsUnsupportedStore interface { - ExcludeIDsUnsupported() bool -} - // Transaction provides atomic multi-operation support within a single database transaction. // // The Transaction interface exposes a subset of storage methods that execute within diff --git a/internal/storage/storagecontract/BUILD.bazel b/internal/storage/storagecontract/BUILD.bazel deleted file mode 100644 index b8e628c32..000000000 --- a/internal/storage/storagecontract/BUILD.bazel +++ /dev/null @@ -1,83 +0,0 @@ -load("@rules_go//go:def.bzl", "go_library") - -go_library( - name = "storagecontract", - srcs = ["history_presence.go"], - importpath = "github.com/steveyegge/beads/internal/storage/storagecontract", - visibility = ["//:__subpackages__"], - deps = ["//internal/storage"], -) - -# --- begin go_srcs (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) --- - -filegroup( - name = "go_srcs", - srcs = glob( - ["**/*.go"], - allow_empty = True, - ), - visibility = ["//:__subpackages__"], -) - -# --- end go_srcs --- - -# --- begin repo_files (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) --- - -filegroup( - name = "repo_doc_files", - srcs = glob( - [ - "**/*.md", - "**/*.mdx", - ], - allow_empty = True, - exclude = ["**/node_modules/**"], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_go_srcs", - srcs = glob( - ["**/*.go"], - allow_empty = True, - exclude = [ - "**/*_test.go", - "**/node_modules/**", - ], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_go_test_srcs", - srcs = glob( - ["**/*_test.go"], - allow_empty = True, - exclude = ["**/node_modules/**"], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_other_files", - srcs = glob( - ["**"], - exclude = [ - "**/*.db", - "**/*.exe", - "**/*.go", - "**/*.md", - "**/*.mdx", - "**/*.out", - "**/*.prof", - "**/*.pyc", - "**/*.test", - "**/__pycache__/**", - "**/node_modules/**", - ], - ), - visibility = ["//:__subpackages__"], -) - -# --- end repo_files --- diff --git a/internal/storage/unsupportedgen/BUILD.bazel b/internal/storage/unsupportedgen/BUILD.bazel deleted file mode 100644 index 0a9b6c0d5..000000000 --- a/internal/storage/unsupportedgen/BUILD.bazel +++ /dev/null @@ -1,96 +0,0 @@ -load("@rules_go//go:def.bzl", "go_binary", "go_library", "go_test") - -go_library( - name = "unsupportedgen_lib", - srcs = ["main.go"], - importpath = "github.com/steveyegge/beads/internal/storage/unsupportedgen", - visibility = ["//visibility:private"], - deps = ["//internal/storage"], -) - -go_binary( - name = "unsupportedgen", - embed = [":unsupportedgen_lib"], - visibility = ["//:__subpackages__"], -) - -go_test( - name = "unsupportedgen_test", - srcs = ["main_test.go"], - data = ["//internal/httpclient:go_srcs"], - embed = [":unsupportedgen_lib"], -) - -# --- begin go_srcs (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) --- - -filegroup( - name = "go_srcs", - srcs = glob( - ["**/*.go"], - allow_empty = True, - ), - visibility = ["//:__subpackages__"], -) - -# --- end go_srcs --- - -# --- begin repo_files (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) --- - -filegroup( - name = "repo_doc_files", - srcs = glob( - [ - "**/*.md", - "**/*.mdx", - ], - allow_empty = True, - exclude = ["**/node_modules/**"], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_go_srcs", - srcs = glob( - ["**/*.go"], - allow_empty = True, - exclude = [ - "**/*_test.go", - "**/node_modules/**", - ], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_go_test_srcs", - srcs = glob( - ["**/*_test.go"], - allow_empty = True, - exclude = ["**/node_modules/**"], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_other_files", - srcs = glob( - ["**"], - exclude = [ - "**/*.db", - "**/*.exe", - "**/*.go", - "**/*.md", - "**/*.mdx", - "**/*.out", - "**/*.prof", - "**/*.pyc", - "**/*.test", - "**/__pycache__/**", - "**/node_modules/**", - ], - ), - visibility = ["//:__subpackages__"], -) - -# --- end repo_files --- diff --git a/internal/storage/unsupportedgen/main.go b/internal/storage/unsupportedgen/main.go deleted file mode 100644 index 6f2f899c6..000000000 --- a/internal/storage/unsupportedgen/main.go +++ /dev/null @@ -1,382 +0,0 @@ -// Command unsupportedgen regenerates the typed-unsupported shell a client -// package embeds to satisfy a storage capability interface it only partially -// implements by hand. -// -// It does NOT take a hand-maintained -skip list. The whole point of this tool -// is to make the "which methods does the shell need to cover" question -// self-answering: it parses the target package's own .go sources (every file -// except _test.go files and the file it is about to overwrite) for methods -// already declared on the given -receiver, and generates a stub for every -// OTHER method of -type storage interface is not already covered. A -// hand-written method and a generated stub can therefore never collide on the -// same selector — there is nothing to keep in sync by hand, and nothing to -// drift. -// -// Written from scratch for the S3 native-program reconciliation: this is NOT -// a port of bd-enterprise's generator (which this client's lift did not carry -// over) — only the shape of the file it must reproduce (unsupported_gen.go, -// lifted with bd-enterprise's generator before this tool existed) was read to -// match its style. -// -// Usage (see internal/httpclient/unsupported.go's //go:generate line): -// -// go run ../../storage/unsupportedgen -type DoltStorage -pkg httpclient -receiver Store -out unsupported_gen.go -package main - -import ( - "bytes" - "flag" - "fmt" - "go/ast" - "go/format" - "go/parser" - "go/token" - "log" - "os" - "path/filepath" - "reflect" - "regexp" - "sort" - "strings" - - "github.com/steveyegge/beads/internal/storage" -) - -// registry maps a -type flag value to the reflected interface it names. Adding -// a type this tool can generate a shell for is one line here; there is no -// string-based reflection trick that gets around naming it, because Go cannot -// look up a type by its source name at runtime. -var registry = map[string]reflect.Type{ - "DoltStorage": reflect.TypeOf((*storage.DoltStorage)(nil)).Elem(), -} - -func main() { - typeName := flag.String("type", "", "name of the interface to shell, from the registry in this file (e.g. DoltStorage)") - pkgName := flag.String("pkg", "", "package clause for the generated file") - receiver := flag.String("receiver", "", "base receiver type name (without *) whose existing methods this tool must not shadow") - scanDir := flag.String("scandir", ".", "directory holding the hand-written sources to scan for existing methods") - out := flag.String("out", "", "output file path, relative to scandir") - extraSkip := flag.String("skip", "", "comma-separated extra method names to exclude, beyond what the scan finds (rarely needed)") - flag.Parse() - - if *typeName == "" || *pkgName == "" || *receiver == "" || *out == "" { - log.Fatal("unsupportedgen: -type, -pkg, -receiver and -out are all required") - } - ifaceType, ok := registry[*typeName] - if !ok { - log.Fatalf("unsupportedgen: %q is not in this tool's registry (add it in main.go)", *typeName) - } - - outPath := filepath.Join(*scanDir, *out) - handWritten, err := scanHandWrittenMethods(*scanDir, *receiver, outPath) - if err != nil { - log.Fatalf("unsupportedgen: scanning %s: %v", *scanDir, err) - } - for _, name := range strings.Split(*extraSkip, ",") { - if name = strings.TrimSpace(name); name != "" { - handWritten[name] = true - } - } - - src, skipped, err := generate(*pkgName, *typeName, ifaceType, handWritten) - if err != nil { - log.Fatalf("unsupportedgen: %v", err) - } - if err := os.WriteFile(outPath, src, 0o600); err != nil { - log.Fatalf("unsupportedgen: writing %s: %v", outPath, err) - } - fmt.Fprintf(os.Stderr, "unsupportedgen: wrote %s (%d methods stubbed, %d left to the hand-written set)\n", - outPath, ifaceType.NumMethod()-skipped, skipped) -} - -// scanHandWrittenMethods parses every .go file in dir except _test.go files -// and the generator's own output file, and returns the set of method names -// declared with a receiver of type recv or *recv anywhere in the package. That -// set is exactly the method names this tool must NOT shell: generating a stub -// for one would make its selector ambiguous against the hand-written one. -func scanHandWrittenMethods(dir, recv, outPath string) (map[string]bool, error) { - absOut, err := filepath.Abs(outPath) - if err != nil { - return nil, err - } - found := map[string]bool{} - fset := token.NewFileSet() - entries, err := os.ReadDir(dir) - if err != nil { - return nil, err - } - for _, entry := range entries { - name := entry.Name() - if entry.IsDir() || !strings.HasSuffix(name, ".go") || strings.HasSuffix(name, "_test.go") { - continue - } - path := filepath.Join(dir, name) - if absPath, err := filepath.Abs(path); err == nil && absPath == absOut { - continue // this is the file we are about to overwrite - } - file, err := parser.ParseFile(fset, path, nil, 0) - if err != nil { - return nil, fmt.Errorf("%s: %w", path, err) - } - for _, decl := range file.Decls { - fn, ok := decl.(*ast.FuncDecl) - if !ok || fn.Recv == nil || len(fn.Recv.List) == 0 { - continue - } - if receiverBaseName(fn.Recv.List[0].Type) == recv { - found[fn.Name.Name] = true - } - } - } - return found, nil -} - -// receiverBaseName strips a leading "*" from a receiver type expression and -// returns its identifier, e.g. "*Store" and "Store" both yield "Store". -func receiverBaseName(expr ast.Expr) string { - if star, ok := expr.(*ast.StarExpr); ok { - expr = star.X - } - if ident, ok := expr.(*ast.Ident); ok { - return ident.Name - } - return "" -} - -// qualifiedLeak matches a package-path-qualified identifier the way a generic -// instantiation's reflect.Type.Name() leaks it, e.g. -// "github.com/steveyegge/beads/internal/types.Issue". It requires at least one -// "/" so it never matches an already-short name like "context.Context" or -// "types.IssueFilter". -var qualifiedLeak = regexp.MustCompile(`[A-Za-z0-9_.\-]+(?:/[A-Za-z0-9_.\-]+)+\.[A-Za-z_][A-Za-z0-9_]*`) - -// importSet collects the packages a generated file's method bodies reference, -// keyed by import path, so the output needs no hand-maintained import block. -type importSet map[string]string // import path -> local alias (last path segment) - -func (s importSet) add(pkgPath string) string { - if pkgPath == "" { - return "" - } - alias := pkgPath - if i := strings.LastIndex(pkgPath, "/"); i >= 0 { - alias = pkgPath[i+1:] - } - s[pkgPath] = alias - return alias -} - -// formatType renders t as the Go syntax a stub's signature can use, collecting -// every package it names into imports as it goes. -// -// A plain reflect.Type.String() is NOT enough on its own: for a generic -// instantiation (this package's one case is storage.Iter[T]), reflect leaks -// the type argument's FULL import path into both String() and Name() rather -// than the short package name every other case gets -// ("storage.Iter[github.com/steveyegge/beads/internal/types.Issue]" instead of -// "storage.Iter[types.Issue]"). This function walks the type structurally so -// composite types are built from already-short recursive calls, and for the -// generic-name case specifically, desanitizes the leaked argument with -// qualifiedLeak before recording its package and splicing it back in. -func formatType(t reflect.Type, imports importSet) string { - // A DEFINED type (Name() != "") with a composite underlying kind — e.g. - // encoding/json.RawMessage, whose Kind() is Slice over its underlying - // []byte — must be named by its declaration, not rebuilt structurally from - // its underlying kind: "[]uint8" is a different type than json.RawMessage - // as far as the compiler's interface-satisfaction check is concerned, even - // though they share a representation. Only an UNNAMED composite (a literal - // "[]string" or "map[string]int" parameter, Name()=="") falls through to - // the structural cases below. - if t.Name() != "" && t.PkgPath() != "" { - return namedType(t, imports) - } - - switch t.Kind() { - case reflect.Ptr: - return "*" + formatType(t.Elem(), imports) - case reflect.Slice: - return "[]" + formatType(t.Elem(), imports) - case reflect.Array: - return fmt.Sprintf("[%d]%s", t.Len(), formatType(t.Elem(), imports)) - case reflect.Map: - return fmt.Sprintf("map[%s]%s", formatType(t.Key(), imports), formatType(t.Elem(), imports)) - case reflect.Chan: - return "chan " + formatType(t.Elem(), imports) - case reflect.Func: - in := make([]string, t.NumIn()) - for i := range in { - if t.IsVariadic() && i == len(in)-1 { - in[i] = "..." + formatType(t.In(i).Elem(), imports) - continue - } - in[i] = formatType(t.In(i), imports) - } - out := make([]string, t.NumOut()) - for i := range out { - out[i] = formatType(t.Out(i), imports) - } - switch len(out) { - case 0: - return fmt.Sprintf("func(%s)", strings.Join(in, ", ")) - case 1: - return fmt.Sprintf("func(%s) %s", strings.Join(in, ", "), out[0]) - default: - return fmt.Sprintf("func(%s) (%s)", strings.Join(in, ", "), strings.Join(out, ", ")) - } - } - - if t.Name() != "" { - return t.Name() // predeclared: string, int, bool, error, ... - } - if t.Kind() == reflect.Interface && t.NumMethod() == 0 { - return "interface{}" - } - // Not expected anywhere in storage.DoltStorage today (no bare func types or - // anonymous structs/interfaces in its method set); fail loudly rather than - // emit something a human has to notice is wrong later. - log.Fatalf("unsupportedgen: unhandled anonymous type %s (kind %s) — formatType needs a case for it", t.String(), t.Kind()) - panic("unreachable") -} - -// reflectAliasCanonical maps a (pkgPath, name) pair that reflect reports for -// a known language-level alias's underlying type back to the alias's own -// declared (pkgPath, name), so unsupportedgen's output is stable across Go -// toolchains that change what an alias's underlying type reflects as. -// -// Go 1.27 made encoding/json.RawMessage an alias of -// encoding/json/jsontext.Value. Aliases have no runtime identity of their -// own — reflect only ever sees the underlying type — but which type counts as -// "underlying" moved: on 1.26 and earlier, reflect.TypeOf(json.RawMessage(nil)) -// reports (encoding/json, RawMessage) directly (RawMessage was a defined -// type, not an alias, at that point); starting in 1.27 it reports the new -// alias target, (encoding/json/jsontext, Value). storage.DoltStorage's -// MergeMetadata signature did not change between toolchains, so the -// generator's output must not either; this table is the fix-up. -// -// Add an entry here (never a one-off special case elsewhere in this file) if -// a future Go stdlib release aliases another type this generator formats. -var reflectAliasCanonical = map[[2]string][2]string{ - {"encoding/json/jsontext", "Value"}: {"encoding/json", "RawMessage"}, -} - -// namedType renders a DEFINED type (t.Name() != "" && t.PkgPath() != "") as -// its package-qualified declaration name, desanitizing a generic -// instantiation's leaked type-argument path the same way formatType's doc -// comment describes, and normalizing a known reflect-visible alias target -// (reflectAliasCanonical) back to the name a human wrote in source. -func namedType(t reflect.Type, imports importSet) string { - pkgPath, name := t.PkgPath(), t.Name() - if canon, ok := reflectAliasCanonical[[2]string{pkgPath, name}]; ok { - pkgPath, name = canon[0], canon[1] - } - if open := strings.IndexByte(name, '['); open >= 0 && strings.HasSuffix(name, "]") { - base := name[:open] - args := qualifiedLeak.ReplaceAllStringFunc(name[open+1:len(name)-1], func(leak string) string { - dot := strings.LastIndex(leak, ".") - return imports.add(leak[:dot]) + leak[dot:] - }) - alias := imports.add(pkgPath) - return fmt.Sprintf("%s.%s[%s]", alias, base, args) - } - alias := imports.add(pkgPath) - return alias + "." + name -} - -type stubMethod struct { - name string - params []string - results []string -} - -// generate produces the full source of the typed-unsupported shell: a stub for -// every method of iface not in handWritten. It returns the number of methods -// it left alone (the hand-written complement) alongside the formatted source. -func generate(pkgName, typeName string, iface reflect.Type, handWritten map[string]bool) ([]byte, int, error) { - imports := importSet{} - var methods []stubMethod - skipped := 0 - for i := 0; i < iface.NumMethod(); i++ { - m := iface.Method(i) - if handWritten[m.Name] { - skipped++ - continue - } - mt := m.Type - sm := stubMethod{name: m.Name} - for p := 0; p < mt.NumIn(); p++ { - sm.params = append(sm.params, formatType(mt.In(p), imports)) - } - for r := 0; r < mt.NumOut(); r++ { - sm.results = append(sm.results, formatType(mt.Out(r), imports)) - } - // errUnsupported only has an error to hand back, so a stub can only - // speak for a method whose last (or only) result is literally an - // error. Every method this tool has ever had to stub satisfies that; - // a future storage.DoltStorage method that does not needs a - // hand-written implementation (which puts it in the scanned, - // hand-written set and this tool never sees it), not a guess here. - if len(sm.results) == 0 || sm.results[len(sm.results)-1] != "error" { - log.Fatalf("unsupportedgen: %s's last result is not error (results: %v) — "+ - "it cannot be shelled by errUnsupported; implement it by hand instead", m.Name, sm.results) - } - methods = append(methods, sm) - } - sort.Slice(methods, func(i, j int) bool { return methods[i].name < methods[j].name }) - - var b bytes.Buffer - fmt.Fprintf(&b, "// Code generated by unsupportedgen (internal/storage/unsupportedgen); DO NOT EDIT.\n") - fmt.Fprintf(&b, "// Regenerate with `go generate ./...` after a storage.%s interface change.\n\n", typeName) - fmt.Fprintf(&b, "package %s\n\n", pkgName) - - if len(imports) > 0 { - paths := make([]string, 0, len(imports)) - for p := range imports { - paths = append(paths, p) - } - sort.Strings(paths) - b.WriteString("import (\n") - for _, p := range paths { - fmt.Fprintf(&b, "\t%q\n", p) - } - b.WriteString(")\n\n") - } - - shellName := "unsupported" + typeName - fmt.Fprintf(&b, "// %s is the generated typed-unsupported shell for storage.%s.\n", shellName, typeName) - fmt.Fprintf(&b, "// Every method returns *storage.ErrUnsupported via errUnsupported; embed it and\n") - fmt.Fprintf(&b, "// override the real slice. DO NOT hand-edit — regenerate with `go generate ./...`.\n") - fmt.Fprintf(&b, "type %s struct{}\n\n", shellName) - - for _, m := range methods { - var resultDecl string - switch len(m.results) { - case 0: - resultDecl = "" - case 1: - resultDecl = fmt.Sprintf(" (err %s)", m.results[0]) - default: - named := make([]string, len(m.results)) - for i, r := range m.results[:len(m.results)-1] { - named[i] = "_ " + r - } - named[len(named)-1] = "err " + m.results[len(m.results)-1] - resultDecl = fmt.Sprintf(" (%s)", strings.Join(named, ", ")) - } - params := make([]string, len(m.params)) - for i, p := range m.params { - params[i] = "_ " + p - } - fmt.Fprintf(&b, "func (%s) %s(%s)%s {\n", shellName, m.name, strings.Join(params, ", "), resultDecl) - fmt.Fprintf(&b, "\terr = errUnsupported(%q)\n", m.name) - b.WriteString("\treturn\n}\n\n") - } - - fmt.Fprintf(&b, "// NOTE: partial shell (%d of %d methods generated; %d left to this package's hand-written set).\n", - len(methods), iface.NumMethod(), skipped) - - formatted, err := format.Source(b.Bytes()) - if err != nil { - return nil, 0, fmt.Errorf("formatting generated source: %w\n--- unformatted ---\n%s", err, b.String()) - } - return formatted, skipped, nil -} diff --git a/internal/storage/unsupportedgen/main_test.go b/internal/storage/unsupportedgen/main_test.go deleted file mode 100644 index 7b0f1f3dc..000000000 --- a/internal/storage/unsupportedgen/main_test.go +++ /dev/null @@ -1,86 +0,0 @@ -package main - -import ( - "os" - "path/filepath" - "testing" -) - -// TestGeneratedShellIsFresh is the anti-drift check this tool never had: it -// runs exactly the same scan-then-generate pipeline main() runs for the one -// registered consumer (internal/httpclient's unsupported_gen.go, built from -// storage.DoltStorage with receiver Store), and compares the result byte for -// byte against the checked-in file. A storage.DoltStorage interface change, -// a hand-written method added or removed from httpclient's Store, or an edit -// to this generator's own output shape that nobody reran `go generate` after -// is caught here instead of silently drifting until someone notices the -// shell no longer matches what the generator would produce today. -// -// Regenerate with: go generate ./internal/httpclient (see unsupported.go's -// //go:generate line, which this test mirrors). -func TestGeneratedShellIsFresh(t *testing.T) { - const ( - typeName = "DoltStorage" - pkgName = "httpclient" - receiver = "Store" - outName = "unsupported_gen.go" - ) - - // httpclient lives two levels up from this package - // (internal/storage/unsupportedgen -> internal/httpclient). - scanDir := filepath.Join("..", "..", "httpclient") - outPath := filepath.Join(scanDir, outName) - - ifaceType, ok := registry[typeName] - if !ok { - t.Fatalf("%q is not in this tool's registry (add it in main.go)", typeName) - } - - handWritten, err := scanHandWrittenMethods(scanDir, receiver, outPath) - if err != nil { - t.Fatalf("scanning %s: %v", scanDir, err) - } - - want, _, err := generate(pkgName, typeName, ifaceType, handWritten) - if err != nil { - t.Fatalf("generate: %v", err) - } - - got, err := os.ReadFile(outPath) - if err != nil { - t.Fatalf("reading checked-in %s: %v", outPath, err) - } - - if string(got) != string(want) { - t.Fatalf("%s is stale: it does not match what this generator produces today.\n"+ - "This file is generated and must not be hand-edited.\n"+ - "Regenerate it with:\n"+ - " go generate ./internal/httpclient", outPath) - } -} - -// TestScanHandWrittenMethodsExcludesOwnOutput pins the one correctness -// property the freshness check above silently depends on: scanning must -// never see the generator's own prior output as "hand-written," or every -// method the shell currently stubs would get treated as already covered and -// the next regeneration would stub nothing at all. -func TestScanHandWrittenMethodsExcludesOwnOutput(t *testing.T) { - scanDir := filepath.Join("..", "..", "httpclient") - outPath := filepath.Join(scanDir, "unsupported_gen.go") - - handWritten, err := scanHandWrittenMethods(scanDir, "Store", outPath) - if err != nil { - t.Fatalf("scanning %s: %v", scanDir, err) - } - - // unsupportedDoltStorage's receiver is unsupportedDoltStorage, not Store, - // so none of its stub method names should ever land in the Store-receiver - // hand-written set purely by virtue of being in the output file -- but to - // catch a regression where the output file itself was scanned anyway, - // assert directly that scanning skipped it: a method this generator is - // known to stub today (AsOf, from storage.DoltStorage's version-control - // family) must not appear as hand-written on Store. - if handWritten["AsOf"] { - t.Error(`handWritten["AsOf"] = true, want false -- scanHandWrittenMethods appears to have scanned its own output file`) - } -} diff --git a/internal/storage/uow/BUILD.bazel b/internal/storage/uow/BUILD.bazel deleted file mode 100644 index 19101d195..000000000 --- a/internal/storage/uow/BUILD.bazel +++ /dev/null @@ -1,323 +0,0 @@ -load("@rules_go//go:def.bzl", "go_library", "go_test") -load("@rules_shell//shell:sh_test.bzl", "sh_test") - -go_library( - name = "uow", - srcs = [ - "batch_applier.go", - "batch_closer.go", - "batch_creator.go", - "batchgetter.go", - "blocked_lister.go", - "blocked_recheck.go", - "blockingannotator.go", - "bootstrapper.go", - "commenter.go", - "counter.go", - "cycle_detector.go", - "delete_fence.go", - "deleter.go", - "dependency_editor.go", - "dolt_sql_provider.go", - "doltserver_provider.go", - "doltserver_tx.go", - "edgereader.go", - "errors.go", - "events_journal.go", - "external_doltserver_provider.go", - "graph_counter.go", - "heartbeater.go", - "importer.go", - "init_verifier.go", - "issue_claimer.go", - "issue_operations.go", - "issue_reader.go", - "journal_maintenance.go", - "lease_reclaimer.go", - "maintenance.go", - "memories.go", - "metadata_cas.go", - "molecule.go", - "notifying.go", - "pool.go", - "querier.go", - "ready_claimer.go", - "ready_counter.go", - "relations.go", - "releaser.go", - "stats_reporter.go", - "sweeper.go", - "team_server_schema.go", - "tree_walker.go", - "tx.go", - "uow.go", - "version_reconciler.go", - "wake_defers.go", - "workspace_config.go", - ], - importpath = "github.com/steveyegge/beads/internal/storage/uow", - visibility = ["//:__subpackages__"], - deps = [ - "//internal/config", - "//internal/configfile", - "//internal/debug", - "//internal/hooks", - "//internal/memoryapi", - "//internal/storage", - "//internal/storage/dberrors", - "//internal/storage/dbproxy/proxy", - "//internal/storage/dbproxy/server", - "//internal/storage/dbproxy/util", - "//internal/storage/domain", - "//internal/storage/domain/db", - "//internal/storage/issueops", - "//internal/storage/memoryops", - "//internal/storage/schema", - "//internal/storage/sqlbuild", - "//internal/storage/versioncontrolops", - "//internal/types", - "//internal/workapi", - "//issueops", - "//memoryops", - "@com_github_cenkalti_backoff_v4//:backoff", - "@com_github_go_sql_driver_mysql//:mysql", - ], -) - -# Under --config=prcore/ci and --config=integration (BEADS_TEST_SKIP=dolt) the -# Dolt-backed tests skip and the whole target takes a few seconds. Without a -# lane config (a plain `bazel test //internal/storage/uow:uow_test`) nothing -# skips them: every newTestUOWProvider test starts its own dolt sql-server, -# ~6-8s apiece, and the package needs ~7 min serially (TestBatchApplyContract -# alone ~1 min: BoundsTheItemCount applies 1000+1001 real items), past the -# default medium 300s. size/shard_count cover that configless run, the same -# workload uow_dolt_test (8 shards) runs. -go_test( - name = "uow_test", - size = "large", - srcs = [ - "batch_applier_retry_budget_test.go", - "batch_apply_contract_test.go", - "batch_closer_contract_test.go", - "batch_creator_contract_test.go", - "batch_getter_contract_test.go", - "blocked_lister_contract_test.go", - "blocked_recheck_race_test.go", - "blocking_annotator_contract_test.go", - "bootstrapper_contract_test.go", - "claimer_contract_test.go", - "commenter_contract_test.go", - "counter_contract_test.go", - "cross_record_invariant_contract_test.go", - "cycle_detector_contract_test.go", - "data_behind_migrate_gate_test.go", - "deleter_contract_test.go", - "dependency_editor_contract_test.go", - "dolt_sql_provider_lock_test.go", - "dolt_sql_provider_test.go", - "doltserver_provider_test.go", - "doltserver_tx_test.go", - "dualwrite_history_contract_test.go", - "dualwrite_participation_generation_contract_test.go", - "edge_reader_contract_test.go", - "errors_test.go", - "expected_revision_contract_test.go", - "external_doltserver_provider_test.go", - "graph_counter_contract_test.go", - "heartbeater_contract_test.go", - "history_off_differential_test.go", - "importer_contract_test.go", - "importer_uow_test.go", - "issue_claimer_test.go", - "issue_operations_claim_contract_test.go", - "issue_operations_contract_test.go", - "issue_operations_force_close_policy_test.go", - "issue_operations_test.go", - "issue_operations_update_real_uow_test.go", - "issue_reader_test.go", - "journal_contract_test.go", - "journal_delete_actor_test.go", - "journal_scope_test.go", - "lease_reclaimer_contract_test.go", - "lifecycle_close_reopen_contract_test.go", - "lifecycle_create_contract_test.go", - "lifecycle_role_hooks_test.go", - "lifecycle_update_contract_test.go", - "list_comments_plane_test.go", - "lostupdate_dolt_test.go", - "memories_contract_test.go", - "metadata_cas_contract_test.go", - "metadata_patch_dolt_test.go", - "molecule_contract_test.go", - "notifying_integration_test.go", - "notifying_parity_test.go", - "notifying_test.go", - "preview_provider_test.go", - "querier_contract_test.go", - "reader_contract_test.go", - "ready_claimer_contract_test.go", - "ready_counter_contract_test.go", - "relations_contract_test.go", - "releaser_contract_test.go", - "retention_epoch_contract_test.go", - "role_fixture_kit_test.go", - "session_database_test.go", - "shared_server_cleanup_guard_test.go", - "shared_server_cleanup_test.go", - "shared_store_migrate_gate_test.go", - "stats_reporter_contract_test.go", - "sweeper_contract_test.go", - "team_server_integration_test.go", - "team_server_schema_test.go", - "testmain_test.go", - "tree_walker_contract_test.go", - "tx_test.go", - "upstream_outage_window_test.go", - "version_reconcile_boot_test.go", - "version_reconcile_integration_test.go", - "version_reconciler_contract_test.go", - "versioned_read_contract_test.go", - "workspace_config_contract_test.go", - ], - data = [ - ":go_srcs", - "//cmd/bd:bd_for_tests", - ], - embed = [":uow"], - env = {"BEADS_TEST_BD_BINARY": "$(rlocationpath //cmd/bd:bd_for_tests)"}, - shard_count = 4, - deps = [ - "//backend/conformance", - "//internal/configfile", - "//internal/doltserver", - "//internal/hooks", - "//internal/procid", - "//internal/storage", - "//internal/storage/dberrors", - "//internal/storage/dbproxy/pidfile", - "//internal/storage/dbproxy/proxy", - "//internal/storage/dbproxy/server", - "//internal/storage/domain", - "//internal/storage/domain/db", - "//internal/storage/historyoffequiv", - "//internal/storage/issueops", - "//internal/storage/schema", - "//internal/testutil", - "//internal/testutil/bazeltest", - "//internal/types", - "//internal/workapi", - "//internal/workapi/storereader", - "//issueops", - "@com_github_cenkalti_backoff_v4//:backoff", - "@com_github_data_dog_go_sqlmock//:go-sqlmock", - "@com_github_go_sql_driver_mysql//:mysql", - "@com_github_stretchr_testify//assert", - "@com_github_stretchr_testify//require", - ], -) - -# uow_test as the retired "Test (storage domain + uow)" job ran it (no -short), -# on the dolt-server lane: every server the tests ask internal/testutil for is -# a `dolt sql-server` child of the test process, so the target runs on remote -# workers. It repeats uow_test's `env` (go_test_variant.sh cannot inherit it). -# GOMAXPROCS=4 is the job's runner shape. -# 8 shards keep each under ~2 min remotely (the package is ~7 min in the job). -# -# Tags: -# dolt-server: starts hermetic dolt sql-servers from the pinned dolt in the -# test wrapper's runfiles (BEADS_TEST_DOLT_SERVER=local); excluded from -# --config=prcore/ci, selected by --config=doltserver. Runs remotely: no -# docker, and a missing server fails (REQUIRE=1) instead of skipping, so -# a cached pass is as good as a fresh one. -sh_test( - name = "uow_dolt_test", - size = "large", - srcs = ["//tools/bazel:go_test_variant.sh"], - args = ["$(rootpath :uow_test)"], - data = [ - ":uow_test", - "//cmd/bd:bd_for_tests", - ], - env = { - "BEADS_TEST_BD_BINARY": "$(rlocationpath //cmd/bd:bd_for_tests)", - "BEADS_TEST_DOLT_SERVER": "local", - "BEADS_TEST_REQUIRE_DOLT_CONTAINER": "1", - "GOMAXPROCS": "4", - }, - shard_count = 8, - tags = ["dolt-server"], -) - -# --- begin go_srcs (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) --- - -filegroup( - name = "go_srcs", - srcs = glob( - ["**/*.go"], - allow_empty = True, - ), - visibility = ["//:__subpackages__"], -) - -# --- end go_srcs --- - -# --- begin repo_files (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) --- - -filegroup( - name = "repo_doc_files", - srcs = glob( - [ - "**/*.md", - "**/*.mdx", - ], - allow_empty = True, - exclude = ["**/node_modules/**"], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_go_srcs", - srcs = glob( - ["**/*.go"], - allow_empty = True, - exclude = [ - "**/*_test.go", - "**/node_modules/**", - ], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_go_test_srcs", - srcs = glob( - ["**/*_test.go"], - allow_empty = True, - exclude = ["**/node_modules/**"], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_other_files", - srcs = glob( - ["**"], - exclude = [ - "**/*.db", - "**/*.exe", - "**/*.go", - "**/*.md", - "**/*.mdx", - "**/*.out", - "**/*.prof", - "**/*.pyc", - "**/*.test", - "**/__pycache__/**", - "**/node_modules/**", - ], - ), - visibility = ["//:__subpackages__"], -) - -# --- end repo_files --- diff --git a/internal/storage/uow/batch_applier.go b/internal/storage/uow/batch_applier.go index c566ef2db..927b36a96 100644 --- a/internal/storage/uow/batch_applier.go +++ b/internal/storage/uow/batch_applier.go @@ -5,7 +5,6 @@ import ( "encoding/json" "fmt" "sort" - "time" "github.com/steveyegge/beads/internal/storage" "github.com/steveyegge/beads/internal/storage/domain" @@ -41,69 +40,6 @@ func NewBatchApplier(provider UnitOfWorkProvider) (publicops.BatchApplier, error var _ publicops.BatchApplier = (*batchApplier)(nil) -// retryTxAttemptHeadroom is reserved off the top of an extended retry budget -// so the FINAL retry attempt still has room to run to completion before the -// caller's own context deadline arrives. Without it, an extended budget -// computed as exactly "whatever's left on ctx" could let backoff start one -// more attempt a moment before ctx expires: that attempt is then killed -// mid-transaction by ctx cancellation, turning a write that may have been -// about to succeed into a context-deadline-exceeded error (classified -// non-retryable, a 500) instead of a clean retry-budget exhaustion (a -// reported, retryable write conflict). The retry loop's OWN loser case -// already reports correctly (uow.RunTxResultWithin's IsSerializationError -// contract) — this headroom exists only to make ctx cancellation not race it. -const retryTxAttemptHeadroom = 5 * time.Second - -// retryTxMinBudget is the floor retryTxBudget returns for a MARKED context -// whose remaining deadline (after subtracting retryTxAttemptHeadroom) is too -// small to be a useful retry budget on its own — e.g. an operator-configured -// --large-apply-ceiling set close to expiry, or a request nearly at its own -// deadline. It deliberately does NOT fall back to DefaultTxRetryMaxElapsed -// (15s): claiming a 15s retry budget when ctx itself has only a second or two -// left would be misleading busywork, since ctx cancellation via -// backoff.WithContext ends the loop at its own deadline regardless of what -// MaxElapsedTime says. A small positive floor just guarantees backoff.Retry's -// unconditional first attempt (and typically one quick retry) rather than a -// degenerate zero-or-negative MaxElapsedTime, which the backoff library -// treats as "never stop on elapsed time" — the opposite of what a -// nearly-expired budget should mean. -const retryTxMinBudget = time.Second - -// retryTxBudget picks ApplyBatch's commit-retry budget. It returns exactly -// DefaultTxRetryMaxElapsed unless ctx was marked by -// issueops.WithExtendedRetryBudget, which only internal/httpapi's -// admitLargeApply does, for a request over the large-apply item threshold. -// -// A version of this fix that instead scaled to "any sufficiently long -// ctx.Deadline()" was wrong: an ORDINARY request also carries a real -// deadline (internal/httpapi's requestDeadline, 60s), and that heuristic -// silently raised every ordinary request's retry ceiling from 15s to up to -// 60s. Gating on the explicit marker keeps ordinary requests at EXACTLY the -// pre-existing 15s ceiling, and extends only the path that intentionally -// asked for more room: an over-threshold request gets a context deadline of -// up to its own largeApplyCeiling (5 minutes by default) precisely so a -// genuinely large, slow-to-commit write has room to run — but a Dolt -// commit-time merge conflict on it would otherwise still give up retrying at -// 15s, with minutes of that budget sitting unused. -// -// retryTxAttemptHeadroom is subtracted from the remaining deadline so the -// scaled budget never lets the last attempt start too late to finish; ctx -// cancellation via backoff.WithContext is still the final backstop either -// way, so this can never let a request run past the deadline it already had. -func retryTxBudget(ctx context.Context) time.Duration { - if !publicops.HasExtendedRetryBudget(ctx) { - return DefaultTxRetryMaxElapsed - } - deadline, ok := ctx.Deadline() - if !ok { - return DefaultTxRetryMaxElapsed - } - if budget := time.Until(deadline) - retryTxAttemptHeadroom; budget > retryTxMinBudget { - return budget - } - return retryTxMinBudget -} - // ApplyBatch applies every item in ONE unit of work and commits them together. // // THIS LEG IS A GENUINELY INDEPENDENT BODY, unlike the third leg of MetadataCAS @@ -132,7 +68,7 @@ func (o *batchApplier) ApplyBatch(ctx context.Context, request publicops.ApplyBa if err != nil { return publicops.ApplyBatchResult{}, err } - return RunTxResultWithin(ctx, o.provider, retryTxBudget(ctx), func(ctx context.Context, uw UnitOfWork) (publicops.ApplyBatchResult, string, error) { + return RunTxResult(ctx, o.provider, func(ctx context.Context, uw UnitOfWork) (publicops.ApplyBatchResult, string, error) { run := &uowApplyRun{ plan: plan, uw: uw, @@ -218,10 +154,9 @@ func (r *uowApplyRun) applyCreate(ctx context.Context, index int, item *publicop return err } prepared, err := storageissueops.PreparePublicCreateRequest(publicops.CreateRequest{ - Actor: r.plan.Actor, - Issue: item.Issue, - ForceIDPrefix: r.plan.ForceIDPrefix, - DefaultPriority: item.DefaultPriority, + Actor: r.plan.Actor, + Issue: item.Issue, + ForceIDPrefix: r.plan.ForceIDPrefix, }, storageissueops.PublicCreateContext{ IssuePrefix: createContext.IssuePrefix, AllowedPrefixes: createContext.AllowedPrefixes, @@ -306,12 +241,11 @@ func (r *uowApplyRun) applyUpdate(ctx context.Context, index int, item *publicop ExpectedAssignee: item.ExpectedAssignee, ForceClosePolicy: item.ForceClosePolicy, ForceAssigneeTransfer: item.ForceAssigneeTransfer, - ForceNotesOverwrite: item.ForceNotesOverwrite, } if err := validateUpdateRequest(request); err != nil { return itemErr(err) } - updated, err := r.runUpdate(ctx, request, true) + updated, err := r.runUpdate(ctx, request) if err != nil { return itemErr(err) } @@ -333,10 +267,7 @@ func (r *uowApplyRun) applyUpdate(ctx context.Context, index int, item *publicop // change. ga-v2k49 is a concrete instance of that drift: claimChanged's actor // comparison here and there diverged (one verbatim, one not) until both were // found and fixed independently. -// -// guardTemplate is false only for the metadata splice, which finishes a create -// item of this request rather than modifying an existing row. -func (r *uowApplyRun) runUpdate(ctx context.Context, request publicops.UpdateRequest, guardTemplate bool) (publicops.UpdateResult, error) { +func (r *uowApplyRun) runUpdate(ctx context.Context, request publicops.UpdateRequest) (publicops.UpdateResult, error) { spec, err := updateSpec(request) if err != nil { return publicops.UpdateResult{}, validationError(err) @@ -350,17 +281,9 @@ func (r *uowApplyRun) runUpdate(ctx context.Context, request publicops.UpdateReq return publicops.UpdateResult{}, err } if updatePreconditionsHold(request, before) { - if guardTemplate { - if err := storageissueops.AuthorizeTemplateUpdate(before, request); err != nil { - return publicops.UpdateResult{}, err - } - } if err := authorizeAssigneeTransfer(ctx, r.uw, before, request); err != nil { return publicops.UpdateResult{}, err } - if err := storageissueops.AuthorizeNotesOverwrite(before, request); err != nil { - return publicops.UpdateResult{}, err - } } // ActorMatches (not a verbatim compare, ga-v2k49): see the identical // comment on issueOperations.Update's claimChanged in issue_operations.go @@ -462,9 +385,6 @@ func (r *uowApplyRun) applyDepAdd(ctx context.Context, index int, item *publicop if source == target { return itemErr(fmt.Errorf("%w: %s", publicops.ErrSelfDependency, source)) } - if err := publicops.CheckDottedChildDependency(source, target, item.Type); err != nil { - return itemErr(err) - } sourceWisp, sourceMine := r.planes[source] targetWisp, targetMine := r.planes[target] if sourceMine && targetMine && sourceWisp != targetWisp { @@ -474,19 +394,11 @@ func (r *uowApplyRun) applyDepAdd(ctx context.Context, index int, item *publicop if err != nil { return itemErr(err) } - metadata := item.Metadata - if item.HasSpawner { - metadata, err = storageissueops.StampWaitsForSpawnerID(item.Type, item.Metadata, target) - if err != nil { - return itemErr(fmt.Errorf("%w: %v", publicops.ErrValidation, err)) - } - } dep := &types.Dependency{ IssueID: source, DependsOnID: target, Type: item.Type, - Metadata: metadata, - ThreadID: item.ThreadID, + Metadata: item.Metadata, } if _, err := r.uw.DependencyUseCase().AddDependencies(ctx, []*types.Dependency{dep}, r.plan.Actor, domain.BulkAddDepsOpts{ SkipPerEdgeCycleCheck: r.plan.SkipPerEdgeCycleCheck, @@ -539,13 +451,11 @@ func (r *uowApplyRun) spliceMetadataRefs(ctx context.Context) error { return err } id := r.result.Items[index].IssueID - // Unguarded for templates, as the store-backed splice is: it finishes - // this request's own create. updated, err := r.runUpdate(ctx, publicops.UpdateRequest{ Actor: r.plan.Actor, IssueID: id, Patch: publicops.IssuePatch{Metadata: publicops.MetadataPatch{Set: set}}, - }, false) + }) if err != nil { return &publicops.ItemError{Index: index, Kind: publicops.ItemCreate, Key: item.Create.Key, IssueID: id, Err: err} } diff --git a/internal/storage/uow/batch_applier_retry_budget_test.go b/internal/storage/uow/batch_applier_retry_budget_test.go deleted file mode 100644 index 6df44c1a1..000000000 --- a/internal/storage/uow/batch_applier_retry_budget_test.go +++ /dev/null @@ -1,192 +0,0 @@ -package uow - -import ( - "context" - "sync/atomic" - "testing" - "time" - - "github.com/steveyegge/beads/internal/types" - publicops "github.com/steveyegge/beads/issueops" -) - -// TestRetryTxBudgetScalesToRemainingDeadline pins the fix for a large apply's -// retry budget: RunTxResult's hardcoded 15s ceiling must not silently disable -// retries for the bulk of a large apply's own (much longer) context deadline -// — but ONLY on the path that explicitly asks for that (marked via -// publicops.WithExtendedRetryBudget). An ordinary request's context also -// carries a real deadline (60s); an earlier version of this fix scaled the -// budget to ANY sufficiently long remaining deadline and silently raised -// ordinary requests from 15s to up to 60s of retrying. That regression is -// exactly what the unmarked subtests below pin against. -func TestRetryTxBudgetScalesToRemainingDeadline(t *testing.T) { - t.Run("no deadline, no marker gets the default", func(t *testing.T) { - got := retryTxBudget(context.Background()) - if got != DefaultTxRetryMaxElapsed { - t.Errorf("retryTxBudget(no deadline) = %s, want %s", got, DefaultTxRetryMaxElapsed) - } - }) - - t.Run("a deadline shorter than the default never shrinks the budget when unmarked", func(t *testing.T) { - ctx, cancel := context.WithTimeout(context.Background(), time.Second) - defer cancel() - got := retryTxBudget(ctx) - if got != DefaultTxRetryMaxElapsed { - t.Errorf("retryTxBudget(1s deadline) = %s, want %s (never below the default when unmarked)", got, DefaultTxRetryMaxElapsed) - } - }) - - t.Run("an UNMARKED long deadline (an ordinary request's own 60s) stays at exactly the default", func(t *testing.T) { - ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second) - defer cancel() - got := retryTxBudget(ctx) - if got != DefaultTxRetryMaxElapsed { - t.Errorf("retryTxBudget(60s deadline, unmarked) = %s, want exactly %s (an ordinary request must not retry longer just because its ctx happens to have a deadline)", got, DefaultTxRetryMaxElapsed) - } - }) - - t.Run("a MARKED deadline far past the default extends the budget to match it, minus attempt headroom", func(t *testing.T) { - const ceiling = 5 * time.Minute - ctx, cancel := context.WithTimeout(context.Background(), ceiling) - defer cancel() - ctx = publicops.WithExtendedRetryBudget(ctx) - got := retryTxBudget(ctx) - want := ceiling - retryTxAttemptHeadroom - const slack = 2 * time.Second - if got < want-slack || got > want { - t.Errorf("retryTxBudget(%s deadline, marked) = %s, want ~%s (extended to match a large apply's own run budget, minus headroom for the final attempt)", ceiling, got, want) - } - }) - - t.Run("a MARKED deadline too close to expiry to leave attempt headroom floors at retryTxMinBudget, not the 15s default", func(t *testing.T) { - ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) - defer cancel() - ctx = publicops.WithExtendedRetryBudget(ctx) - got := retryTxBudget(ctx) - if got != retryTxMinBudget { - t.Errorf("retryTxBudget(3s deadline, marked) = %s, want %s (too little headroom to extend; claiming the full 15s default here would be misleading since ctx cancellation ends the loop first regardless)", got, retryTxMinBudget) - } - }) -} - -// alwaysSerializationErrorProvider fails EVERY NewUOW call with a retryable -// serialization error, so a caller through RunTxResultWithin never reaches -// the work function at all. That is what makes it cheap: proving ApplyBatch -// itself (not just the retryTxBudget helper) actually wires retryTxBudget's -// return value into RunTxResultWithin needs no real domain use cases — -// issueops.BatchApplier's huge UnitOfWork surface is never touched, because -// NewUOW itself is the thing failing. -type alwaysSerializationErrorProvider struct { - calls atomic.Int32 -} - -func (p *alwaysSerializationErrorProvider) NewUOW(context.Context) (UnitOfWork, error) { - p.calls.Add(1) - return nil, newMySQLError(1213) // deadlock: uow.isSerializationError's retryable case -} - -func (p *alwaysSerializationErrorProvider) Close(context.Context) error { return nil } - -// minimalValidApplyBatchRequest is the smallest request storage.PlanApplyBatch -// accepts: one actor, one create item. It is never actually applied in the -// tests below — alwaysSerializationErrorProvider fails before uowApplyRun.apply -// ever runs — so its content only has to pass static plan validation. -func minimalValidApplyBatchRequest() publicops.ApplyBatchRequest { - return publicops.ApplyBatchRequest{ - Actor: "tester", - Items: []publicops.ApplyItem{ - { - Kind: publicops.ItemCreate, - Create: &publicops.CreateItem{ - Issue: &types.Issue{ - Title: "retry budget wiring probe", - Status: types.StatusOpen, - Priority: 2, - IssueType: types.TypeTask, - }, - }, - }, - }, - } -} - -// TestApplyBatchHonorsAnExtendedRetryBudgetFromContext tests ApplyBatch -// ITSELF, not just the retryTxBudget helper — the gap the coordinator's -// review flagged: a mutation that hardcodes ApplyBatch's call to -// RunTxResultWithin(ctx, o.provider, DefaultTxRetryMaxElapsed, ...) instead of -// retryTxBudget(ctx) passed every existing test, because nothing exercised -// ApplyBatch with a marked context and actually observed which budget -// governed the retry loop. -// -// Both subtests share the SAME 2-second outer ctx deadline, so a mutation -// that ignores the marker makes them behave IDENTICALLY: -// -// - unmarked: retryTxBudget must return the full 15s default. Since 15s -// vastly exceeds the 2s ctx deadline, ctx's OWN cancellation ends the -// loop first, so the observed error is ctx's DeadlineExceeded, never a -// serialization error, and it takes close to the full 2s. -// - marked: retryTxBudget must return retryTxMinBudget (1s, per the -// "too close to expiry to extend" case above, since 2s of remaining -// deadline is less than retryTxAttemptHeadroom). That 1s budget is -// SHORTER than the 2s ctx deadline, so the retry loop exhausts on its -// own well under 2s, and the observed error is the serialization error -// itself. -// -// If ApplyBatch ignored retryTxBudget's return value and always used the 15s -// default, the marked case would look exactly like the unmarked one: it -// would run nearly the full 2s and end on ctx's DeadlineExceeded instead of -// exhausting fast on the serialization error, and this test would fail. -func TestApplyBatchHonorsAnExtendedRetryBudgetFromContext(t *testing.T) { - const outerDeadline = 2 * time.Second - - t.Run("unmarked: the 15s default outlasts the 2s ctx, so ctx itself ends the loop", func(t *testing.T) { - provider := &alwaysSerializationErrorProvider{} - applier, err := NewBatchApplier(provider) - if err != nil { - t.Fatalf("NewBatchApplier: %v", err) - } - ctx, cancel := context.WithTimeout(context.Background(), outerDeadline) - defer cancel() - - start := time.Now() - _, err = applier.ApplyBatch(ctx, minimalValidApplyBatchRequest()) - elapsed := time.Since(start) - - if err == nil { - t.Fatal("expected an error: NewUOW never succeeds") - } - if IsSerializationError(err) { - t.Errorf("err = %v is a serialization error; want ctx's own DeadlineExceeded, because the 15s default budget should still be governing and outlast this 2s ctx", err) - } - if elapsed < 1500*time.Millisecond { - t.Errorf("elapsed = %s, want close to %s: the loop ended too early for the 15s default to have been the governing budget", elapsed, outerDeadline) - } - if provider.calls.Load() < 2 { - t.Errorf("NewUOW calls = %d, want several retries before ctx ended the loop", provider.calls.Load()) - } - }) - - t.Run("marked: the too-short-to-extend 1s floor governs and exhausts well under the 2s ctx", func(t *testing.T) { - provider := &alwaysSerializationErrorProvider{} - applier, err := NewBatchApplier(provider) - if err != nil { - t.Fatalf("NewBatchApplier: %v", err) - } - ctx, cancel := context.WithTimeout(publicops.WithExtendedRetryBudget(context.Background()), outerDeadline) - defer cancel() - - start := time.Now() - _, err = applier.ApplyBatch(ctx, minimalValidApplyBatchRequest()) - elapsed := time.Since(start) - - if err == nil { - t.Fatal("expected an error: NewUOW never succeeds") - } - if !IsSerializationError(err) { - t.Errorf("err = %v, want the last serialization failure itself: an exhausted retry budget, not ctx cancellation", err) - } - if elapsed > 1500*time.Millisecond { - t.Errorf("elapsed = %s, want well under %s: the marked path's ~1s floor should have governed, not the 2s outer ctx", elapsed, outerDeadline) - } - }) -} diff --git a/internal/storage/uow/batch_apply_contract_test.go b/internal/storage/uow/batch_apply_contract_test.go index 557d8a2fe..255a7c879 100644 --- a/internal/storage/uow/batch_apply_contract_test.go +++ b/internal/storage/uow/batch_apply_contract_test.go @@ -115,18 +115,6 @@ func TestBatchApplyContract(t *testing.T) { t.Run("NormalizesTheWaitsForGate", func(t *testing.T) { conformance.RunBatchApplyNormalizesTheWaitsForGate(t, ctx, fixture) }) - t.Run("StampsSpawnerIDOnlyWhenNamed", func(t *testing.T) { - conformance.RunBatchApplyStampsSpawnerIDOnlyWhenNamed(t, ctx, fixture) - }) - t.Run("CarriesThreadIDOntoTheStoredEdge", func(t *testing.T) { - conformance.RunBatchApplyCarriesThreadIDOntoTheStoredEdge(t, ctx, fixture) - }) - t.Run("CarriesThreadIDOntoAnExistingEdge", func(t *testing.T) { - conformance.RunBatchApplyCarriesThreadIDOntoAnExistingEdge(t, ctx, fixture) - }) - t.Run("RefusesAThreadIDLongerThanItsColumn", func(t *testing.T) { - conformance.RunBatchApplyRefusesAThreadIDLongerThanItsColumn(t, ctx, fixture) - }) t.Run("SplicesAForwardMetadataRef", func(t *testing.T) { conformance.RunBatchApplySplicesAForwardMetadataRef(t, ctx, fixture) }) @@ -157,24 +145,6 @@ func TestBatchApplyContract(t *testing.T) { t.Run("RefusesAnUnusableRequest", func(t *testing.T) { conformance.RunBatchApplyRefusesAnUnusableRequest(t, ctx, fixture) }) - t.Run("UpdateItemsRefuseATemplate", func(t *testing.T) { - conformance.RunBatchApplyUpdateItemsRefuseATemplate(t, ctx, fixture) - }) - t.Run("SplicesTheMetadataOfATemplateItCreates", func(t *testing.T) { - conformance.RunBatchApplySplicesTheMetadataOfATemplateItCreates(t, ctx, fixture) - }) - t.Run("CloseItemsAnswerToTheCloseGuards", func(t *testing.T) { - conformance.RunBatchApplyCloseItemsAnswerToTheCloseGuards(t, ctx, fixture) - }) - t.Run("RefusesADottedChildGatedOnItsOwnParent", func(t *testing.T) { - conformance.RunBatchApplyRefusesADottedChildGatedOnItsOwnParent(t, ctx, fixture) - }) - t.Run("AppliesTheDefaultPriority", func(t *testing.T) { - conformance.RunBatchApplyAppliesTheDefaultPriority(t, ctx, fixture) - }) - t.Run("CarriesTheGateMembers", func(t *testing.T) { - conformance.RunBatchApplyCarriesTheGateMembers(t, ctx, fixture) - }) } func newUOWBatchApplyFixture(t *testing.T, ctx context.Context, prefix string) conformance.BatchApplyFixture { diff --git a/internal/storage/uow/batch_closer.go b/internal/storage/uow/batch_closer.go index 43cfed688..e29707f42 100644 --- a/internal/storage/uow/batch_closer.go +++ b/internal/storage/uow/batch_closer.go @@ -73,24 +73,6 @@ func (o *batchCloser) CloseBatch(ctx context.Context, request publicops.CloseBat } } - if request.AutoCloseMolecule { - closedAt := make([]int, 0, len(result.Outcomes)) - stepIDs := make([]string, 0, len(result.Outcomes)) - for i, outcome := range result.Outcomes { - if outcome.Err == nil { - closedAt = append(closedAt, i) - stepIDs = append(stepIDs, outcome.IssueID) - } - } - autos, err := CloseCompletedMoleculesInUOW(ctx, uw, stepIDs, request.Actor, request.Session) - if err != nil { - return publicops.CloseBatchResult{}, "", err - } - for j, i := range closedAt { - result.Outcomes[i].AutoClosedMolecule, result.Outcomes[i].MoleculeAutoCloseRefusal = autos[j].Root, autos[j].Refusal - } - } - if claimFilter != nil && landed > 0 { claimed, err := ClaimNextInUOW(ctx, uw, request.Actor, *claimFilter) if err != nil { diff --git a/internal/storage/uow/batch_closer_contract_test.go b/internal/storage/uow/batch_closer_contract_test.go index 4ca8d2b4e..43640e738 100644 --- a/internal/storage/uow/batch_closer_contract_test.go +++ b/internal/storage/uow/batch_closer_contract_test.go @@ -47,7 +47,6 @@ func TestBatchCloserContract(t *testing.T) { {name: "AllRefusedBatchRecordsNoHistory", prefix: "bc-nohistory", run: conformance.RunBatchCloserAllRefusedBatchRecordsNoHistory}, {name: "DoesNotMutateTheCallerRequest", prefix: "bc-snapshot", run: conformance.RunBatchCloserDoesNotMutateTheCallerRequest}, {name: "SettlesTheDependersOfWhatItClosed", prefix: "bc-blocked", run: conformance.RunBatchCloserSettlesTheDependersOfWhatItClosed}, - {name: "ItemsAnswerToTheCloseGuards", prefix: "bc-guard", run: conformance.RunBatchCloserItemsAnswerToTheCloseGuards}, } { t.Run(test.name, func(t *testing.T) { test.run(t, ctx, newUOWBatchCloserFixture(t, provider, test.prefix)) diff --git a/internal/storage/uow/batch_creator_contract_test.go b/internal/storage/uow/batch_creator_contract_test.go index 99621f472..0bc5f74fb 100644 --- a/internal/storage/uow/batch_creator_contract_test.go +++ b/internal/storage/uow/batch_creator_contract_test.go @@ -61,9 +61,6 @@ func TestBatchCreatorContract(t *testing.T) { t.Run("EchoesSubSecondTimestamps", func(t *testing.T) { conformance.RunBatchCreatorEchoesSubSecondTimestamps(t, ctx, fixture) }) - t.Run("AppliesTheDefaultPriority", func(t *testing.T) { - conformance.RunBatchCreatorAppliesTheDefaultPriority(t, ctx, fixture) - }) } func newUOWBatchCreatorFixture(t *testing.T, ctx context.Context, prefix string) conformance.BatchCreatorFixture { diff --git a/internal/storage/uow/batch_getter_contract_test.go b/internal/storage/uow/batch_getter_contract_test.go deleted file mode 100644 index 9821923b4..000000000 --- a/internal/storage/uow/batch_getter_contract_test.go +++ /dev/null @@ -1,94 +0,0 @@ -package uow - -import ( - "context" - "testing" - - "github.com/steveyegge/beads/backend/conformance" -) - -// TestBatchGetterContract runs the BatchGetter contract against the -// unit-of-work provider. -// -// For most roles this is the wiring where a genuine seam divergence shows up, -// because the unit of work is a second body. NOT FOR THIS ROLE: it reaches the -// same issueops.ExecuteGetMany through the domain repository, whose runner -// publishes exactly the DBTX method set that function takes. What this leg -// checks is the WRAPPER — that the request survives the trip and that -// ErrValidation still matches errors.Is after crossing two layers whose -// siblings wrap their errors. -// -// One provider for the whole suite and NO t.Parallel: this backend has no -// per-test copy-on-write branch, so the tables are database-global. Every case -// scopes itself by the ids it seeded. -func TestBatchGetterContract(t *testing.T) { - ctx := context.Background() - fixture := newUOWBatchGetterFixture(t, ctx, "bge") - - t.Run("FindsRequestedIssues", func(t *testing.T) { - conformance.RunBatchGetterFindsRequestedIssues(t, ctx, fixture) - }) - t.Run("ReportsMissingIDs", func(t *testing.T) { - conformance.RunBatchGetterReportsMissingIDs(t, ctx, fixture) - }) - t.Run("CollapsesRepeatedIDs", func(t *testing.T) { - conformance.RunBatchGetterCollapsesRepeatedIDs(t, ctx, fixture) - }) - t.Run("AnswersInRequestOrder", func(t *testing.T) { - conformance.RunBatchGetterAnswersInRequestOrder(t, ctx, fixture) - }) - t.Run("ResolvesIDsExactly", func(t *testing.T) { - conformance.RunBatchGetterResolvesIDsExactly(t, ctx, fixture) - }) - t.Run("AnswersAnEmptyRequest", func(t *testing.T) { - conformance.RunBatchGetterAnswersAnEmptyRequest(t, ctx, fixture) - }) - t.Run("RefusesAnUnusableRequest", func(t *testing.T) { - conformance.RunBatchGetterRefusesAnUnusableRequest(t, ctx, fixture) - }) - t.Run("RefusesOverTheCap", func(t *testing.T) { - conformance.RunBatchGetterRefusesOverTheCap(t, ctx, fixture) - }) - t.Run("AcceptsExactlyTheCap", func(t *testing.T) { - conformance.RunBatchGetterAcceptsExactlyTheCap(t, ctx, fixture) - }) - t.Run("LeavesTheRequestAlone", func(t *testing.T) { - conformance.RunBatchGetterLeavesTheRequestAlone(t, ctx, fixture) - }) - t.Run("SharesOneReadStructurally", func(t *testing.T) { - conformance.RunBatchGetterSharesOneReadStructurally(t, ctx, fixture) - }) - t.Run("HydratesLabels", func(t *testing.T) { - conformance.RunBatchGetterHydratesLabels(t, ctx, fixture) - }) - t.Run("CrossesBothPlanes", func(t *testing.T) { - conformance.RunBatchGetterCrossesBothPlanes(t, ctx, fixture) - }) - t.Run("WritesNothing", func(t *testing.T) { - conformance.RunBatchGetterWritesNothing(t, ctx, fixture) - }) -} - -func newUOWBatchGetterFixture(t *testing.T, ctx context.Context, prefix string) conformance.BatchGetterFixture { - t.Helper() - provider := newUOWRoleFixtureProvider(t, ctx, prefix) - // Through the capability accessor, not NewBatchGetter: a provider that - // stopped offering the role is the regression, and a constructor call would - // hide it. - source, ok := provider.(BatchGetterSource) - if !ok { - t.Fatalf("provider %T does not offer the BatchGetter accessor", provider) - } - getter, err := source.BatchGetter() - if err != nil { - t.Fatalf("BatchGetter(): %v", err) - } - kit := newUOWRoleFixtureKit(provider, prefix) - return conformance.BatchGetterFixture{ - IssuePrefix: kit.IssuePrefix, - BatchGetter: getter, - CreateIssue: kit.CreateIssue, - CreateWisp: kit.CreateWisp, - CountHistory: kit.CountHistory, - } -} diff --git a/internal/storage/uow/batchgetter.go b/internal/storage/uow/batchgetter.go deleted file mode 100644 index adbd54cc1..000000000 --- a/internal/storage/uow/batchgetter.go +++ /dev/null @@ -1,49 +0,0 @@ -package uow - -import ( - "context" - "fmt" - - publicops "github.com/steveyegge/beads/issueops" -) - -// BatchGetterSource is the capability accessor a unit-of-work provider offers -// for the batch-read role, the sibling of CounterSource and GraphCounterSource. -type BatchGetterSource interface { - BatchGetter() (publicops.BatchGetter, error) -} - -// batchGetter answers a GetMany through a unit of work. -type batchGetter struct { - provider UnitOfWorkProvider -} - -// BatchGetter returns the guarded batch-read surface for this provider. -func (p *doltSQLProvider) BatchGetter() (publicops.BatchGetter, error) { - return NewBatchGetter(p) -} - -// NewBatchGetter constructs a public BatchGetter backed by provider. -func NewBatchGetter(provider UnitOfWorkProvider) (publicops.BatchGetter, error) { - if isNilUnitOfWorkProvider(provider) { - return nil, fmt.Errorf("new batch getter: unit-of-work provider must not be nil") - } - return &batchGetter{provider: provider}, nil -} - -var _ publicops.BatchGetter = (*batchGetter)(nil) - -// GetMany reaches domain.IssueSQLRepository.GetMany inside ONE read-only unit -// of work, the same seam CountEdges reaches DependencyUseCase.CountEdges -// through: the request's whole vocabulary is validated inside the shared -// body (issueops.ExecuteGetMany), so this leg does no pre-check of its own. -func (c *batchGetter) GetMany(ctx context.Context, request publicops.GetManyRequest) (publicops.GetManyResult, error) { - return RunTxRead(ctx, c.provider, func(ctx context.Context, uw UnitOfWork) (publicops.GetManyResult, error) { - return GetManyInUOW(ctx, uw, request) - }) -} - -// GetManyInUOW is the BatchGetter body, run inside the caller's unit of work. -func GetManyInUOW(ctx context.Context, uw UnitOfWork, request publicops.GetManyRequest) (publicops.GetManyResult, error) { - return uw.IssueUseCase().GetMany(ctx, request) -} diff --git a/internal/storage/uow/blocked_lister.go b/internal/storage/uow/blocked_lister.go deleted file mode 100644 index 4b1e103ba..000000000 --- a/internal/storage/uow/blocked_lister.go +++ /dev/null @@ -1,50 +0,0 @@ -package uow - -import ( - "context" - "fmt" - - storageissueops "github.com/steveyegge/beads/internal/storage/issueops" - publicops "github.com/steveyegge/beads/issueops" -) - -// BlockedListerSource is the capability accessor a unit-of-work provider -// offers for the blocked-listing role. -type BlockedListerSource interface { - BlockedLister() (publicops.BlockedLister, error) -} - -// blockedLister lists the blocked issues through a unit of work. -type blockedLister struct { - provider UnitOfWorkProvider -} - -// BlockedLister returns the blocked-listing surface for this provider. -func (p *doltSQLProvider) BlockedLister() (publicops.BlockedLister, error) { - return NewBlockedLister(p) -} - -// NewBlockedLister constructs a public BlockedLister backed by provider. -func NewBlockedLister(provider UnitOfWorkProvider) (publicops.BlockedLister, error) { - if isNilUnitOfWorkProvider(provider) { - return nil, fmt.Errorf("new blocked lister: unit-of-work provider must not be nil") - } - return &blockedLister{provider: provider}, nil -} - -var _ publicops.BlockedLister = (*blockedLister)(nil) - -// ListBlocked answers one blocked listing inside one read-only unit of work, -// through the use case's GetBlockedIssues: the same body the store legs run -// (issueops.GetBlockedIssuesInTx), and, under a provider that applies the -// external-dependency policy, that provider's decoration of it. -func (l *blockedLister) ListBlocked(ctx context.Context, req publicops.BlockedRequest) (publicops.BlockedResult, error) { - filter := storageissueops.BlockedFilter(req) - return RunTxRead(ctx, l.provider, func(ctx context.Context, uw UnitOfWork) (publicops.BlockedResult, error) { - items, err := uw.IssueUseCase().GetBlockedIssues(ctx, filter) - if err != nil { - return publicops.BlockedResult{}, err - } - return storageissueops.BlockedResultOf(items), nil - }) -} diff --git a/internal/storage/uow/blocked_lister_contract_test.go b/internal/storage/uow/blocked_lister_contract_test.go deleted file mode 100644 index 1c38f51c3..000000000 --- a/internal/storage/uow/blocked_lister_contract_test.go +++ /dev/null @@ -1,31 +0,0 @@ -package uow - -import ( - "context" - "testing" - - "github.com/steveyegge/beads/backend/conformance" -) - -// TestBlockedListerContract runs the BlockedLister contract against the -// unit-of-work provider, through its own capability accessor. -func TestBlockedListerContract(t *testing.T) { - ctx := context.Background() - provider := newUOWRoleFixtureProvider(t, ctx, "blk") - source, ok := provider.(BlockedListerSource) - if !ok { - t.Fatalf("provider %T does not offer the BlockedLister accessor", provider) - } - lister, err := source.BlockedLister() - if err != nil { - t.Fatalf("BlockedLister(): %v", err) - } - kit := newUOWRoleFixtureKit(provider, "blk") - conformance.RunBlockedLister(t, ctx, conformance.BlockedListerFixture{ - IssuePrefix: kit.IssuePrefix, - BlockedLister: lister, - CreateIssue: kit.CreateIssue, - CreateWisp: kit.CreateWisp, - AddDependency: kit.AddDependency, - }) -} diff --git a/internal/storage/uow/blocking_annotator_contract_test.go b/internal/storage/uow/blocking_annotator_contract_test.go index dcf7bbafe..a9df7af0c 100644 --- a/internal/storage/uow/blocking_annotator_contract_test.go +++ b/internal/storage/uow/blocking_annotator_contract_test.go @@ -72,9 +72,6 @@ func TestBlockingAnnotatorContract(t *testing.T) { t.Run("WritesNothing", func(t *testing.T) { conformance.RunBlockingAnnotatorWritesNothing(t, ctx, fixture) }) - t.Run("ReportsTheTransitiveBlockedFlag", func(t *testing.T) { - conformance.RunBlockingAnnotatorReportsTheTransitiveBlockedFlag(t, ctx, fixture) - }) } func newUOWBlockingAnnotatorFixture(t *testing.T, ctx context.Context, prefix string) conformance.BlockingAnnotatorFixture { diff --git a/internal/storage/uow/blockingannotator.go b/internal/storage/uow/blockingannotator.go index 25f575980..6f587a5e4 100644 --- a/internal/storage/uow/blockingannotator.go +++ b/internal/storage/uow/blockingannotator.go @@ -59,14 +59,6 @@ func (a *blockingAnnotator) AnnotateBlocking(ctx context.Context, request public if err != nil { return publicops.BlockingResult{}, err } - result := storageissueops.FinishBlockingAnnotation(anchors, info.BlockedBy, info.Blocks, info.Parent) - if request.IncludeIsBlocked { - flags, err := uw.DependencyUseCase().IsBlockedBatch(ctx, anchors) - if err != nil { - return publicops.BlockingResult{}, err - } - storageissueops.ApplyBlockedFlags(&result, flags) - } - return result, nil + return storageissueops.FinishBlockingAnnotation(anchors, info.BlockedBy, info.Blocks, info.Parent), nil }) } diff --git a/internal/storage/uow/counter.go b/internal/storage/uow/counter.go index 467712fa2..03a155788 100644 --- a/internal/storage/uow/counter.go +++ b/internal/storage/uow/counter.go @@ -85,14 +85,11 @@ func (c *counter) CountByGroup(ctx context.Context, req publicops.CountByGroupRe } // countFilter builds the storage filter from the unit of work the call already -// holds, loading configuration only when IncludeInfra or ExcludeStatus can -// read it — the same two decisions the store-backed body makes, through the -// same builder. ExcludeStatus needs the workspace's custom status names so -// BuildCountFilter's validation does not refuse a status the workspace itself -// defines (PR #7199 review). +// holds, loading configuration only when IncludeInfra can read it — the same two +// decisions the store-backed body makes, through the same builder. func countFilter(ctx context.Context, uw UnitOfWork, req publicops.CountRequest) (types.IssueFilter, error) { var cfg workapi.ListConfig - if req.IncludeInfra || len(req.ExcludeStatus) > 0 { + if req.IncludeInfra { loaded, err := workapi.LoadUOWListConfig(ctx, uw) if err != nil { return types.IssueFilter{}, err diff --git a/internal/storage/uow/counter_contract_test.go b/internal/storage/uow/counter_contract_test.go index 9936e77e1..45e4de73b 100644 --- a/internal/storage/uow/counter_contract_test.go +++ b/internal/storage/uow/counter_contract_test.go @@ -5,9 +5,6 @@ import ( "testing" "github.com/steveyegge/beads/backend/conformance" - "github.com/steveyegge/beads/internal/storage/domain" - "github.com/steveyegge/beads/internal/types" - publicops "github.com/steveyegge/beads/issueops" ) // TestCounterContract runs the Counter contract against the unit-of-work @@ -68,33 +65,6 @@ func TestCounterContract(t *testing.T) { t.Run("WritesNothing", func(t *testing.T) { conformance.RunCounterWritesNothing(t, ctx, fixture) }) - t.Run("ParentIDScopesToChildren", func(t *testing.T) { - conformance.RunCounterParentIDScopesToChildren(t, ctx, fixture) - }) - t.Run("NoParentExcludesChildren", func(t *testing.T) { - conformance.RunCounterNoParentExcludesChildren(t, ctx, fixture) - }) - t.Run("ExcludeTypesNarrowsThePredicate", func(t *testing.T) { - conformance.RunCounterExcludeTypesNarrowsThePredicate(t, ctx, fixture) - }) - t.Run("ExcludeStatusNarrowsThePredicate", func(t *testing.T) { - conformance.RunCounterExcludeStatusNarrowsThePredicate(t, ctx, fixture) - }) - t.Run("ParentIDMatchesListCardinality", func(t *testing.T) { - conformance.RunCounterParentIDMatchesListCardinality(t, ctx, fixture) - }) - t.Run("NoParentMatchesListCardinality", func(t *testing.T) { - conformance.RunCounterNoParentMatchesListCardinality(t, ctx, fixture) - }) - t.Run("ExcludeTypesMatchesListCardinality", func(t *testing.T) { - conformance.RunCounterExcludeTypesMatchesListCardinality(t, ctx, fixture) - }) - t.Run("ParentIDIncludesAWispChild", func(t *testing.T) { - conformance.RunCounterParentIDIncludesAWispChild(t, ctx, fixture) - }) - t.Run("ParentIDAndExcludeStatusComposeOnAClosedChild", func(t *testing.T) { - conformance.RunCounterParentIDAndExcludeStatusComposeOnAClosedChild(t, ctx, fixture) - }) } func newUOWCounterFixture(t *testing.T, ctx context.Context, prefix string) conformance.CounterFixture { @@ -110,99 +80,12 @@ func newUOWCounterFixture(t *testing.T, ctx context.Context, prefix string) conf if err != nil { t.Fatalf("Counter(): %v", err) } - readerSource, ok := provider.(IssueReaderSource) - if !ok { - t.Fatalf("provider %T does not offer the IssueReader accessor", provider) - } - reader, err := readerSource.IssueReader() - if err != nil { - t.Fatalf("IssueReader(): %v", err) - } kit := newUOWRoleFixtureKit(provider, prefix) return conformance.CounterFixture{ - IssuePrefix: kit.IssuePrefix, - Counter: counter, - CreateIssue: kit.CreateIssue, - CreateWisp: kit.CreateWisp, - CountHistory: kit.CountHistory, - AddDependency: kit.AddDependency, - List: reader.List, - } -} - -// TestCounterExcludeStatusAcceptsAWorkspaceCustomStatusWithoutIncludeInfra -// pins the PR #7199 review's blocker fix on the unit-of-work leg: countFilter -// must load the workspace's list configuration when ExcludeStatus is -// non-empty, not only when IncludeInfra is set, so BuildCountFilter's -// validation sees the workspace's own custom statuses. A leg-level test, not -// a conformance case, because CounterFixture cannot drive a -// workspace-configured custom status (counter_contract.go's CounterFixture -// doc) — this needs a real provider with status.custom set. -// -// Its own provider (not newUOWCounterFixture's), so status.custom does not -// leak into the other subtests sharing that fixture's one database. -func TestCounterExcludeStatusAcceptsAWorkspaceCustomStatusWithoutIncludeInfra(t *testing.T) { - ctx := context.Background() - provider := newUOWRoleFixtureProvider(t, ctx, "cntcs") - - if err := RunTx(ctx, provider, func(ctx context.Context, uw UnitOfWork) (string, error) { - return "set status.custom", uw.ConfigUseCase().SetConfig(ctx, "status.custom", "review,qa") - }); err != nil { - t.Fatalf("SetConfig(status.custom): %v", err) - } - - const openID = "cntcs-open" - const reviewID = "cntcs-review" - for _, id := range []string{openID, reviewID} { - id := id - if err := RunTx(ctx, provider, func(ctx context.Context, uw UnitOfWork) (string, error) { - _, err := uw.IssueUseCase().CreateIssue(ctx, domain.CreateIssueParams{ - Issue: &types.Issue{ - ID: id, Title: id, Status: types.StatusOpen, Priority: 2, IssueType: types.TypeTask, - }, - ExplicitID: id, - CreateOnly: true, - }, "tester") - return "seed " + id, err - }); err != nil { - t.Fatalf("CreateIssue(%s): %v", id, err) - } - } - if err := RunTx(ctx, provider, func(ctx context.Context, uw UnitOfWork) (string, error) { - return "move to review", uw.IssueUseCase().UpdateIssue(ctx, reviewID, map[string]any{ - "status": types.Status("review"), - }, "tester") - }); err != nil { - t.Fatalf("UpdateIssue(%s) to review: %v", reviewID, err) - } - - source, ok := provider.(CounterSource) - if !ok { - t.Fatalf("provider %T does not offer the Counter accessor", provider) - } - counter, err := source.Counter() - if err != nil { - t.Fatalf("Counter(): %v", err) - } - - scope := publicops.CountRequest{IDFilter: openID + "," + reviewID} - total, err := counter.Count(ctx, scope) - if err != nil { - t.Fatalf("Count(unfiltered): %v", err) - } - if total.Total != 2 { - t.Fatalf("Count(unfiltered).Total = %d, want 2", total.Total) - } - - // IncludeInfra left UNSET: before the fix this refused with "invalid - // exclude-status \"review\"" because the config load was gated on - // IncludeInfra alone. - scope.ExcludeStatus = []string{"review"} - result, err := counter.Count(ctx, scope) - if err != nil { - t.Fatalf("Count(exclude-status=review, IncludeInfra unset): %v, want the custom status accepted", err) - } - if result.Total != 1 { - t.Fatalf("Count(exclude-status=review).Total = %d, want 1", result.Total) + IssuePrefix: kit.IssuePrefix, + Counter: counter, + CreateIssue: kit.CreateIssue, + CreateWisp: kit.CreateWisp, + CountHistory: kit.CountHistory, } } diff --git a/internal/storage/uow/cross_record_invariant_contract_test.go b/internal/storage/uow/cross_record_invariant_contract_test.go deleted file mode 100644 index b88f0d774..000000000 --- a/internal/storage/uow/cross_record_invariant_contract_test.go +++ /dev/null @@ -1,41 +0,0 @@ -package uow - -import ( - "context" - "testing" - - "github.com/steveyegge/beads/backend/conformance" -) - -// TestCrossRecordInvariantContract wires this leg into the R16.1 -// cross-record invariant contract. Phase 0 leaves every hook nil in every -// backend's fixture kit (architecture §12), so each case below skips by -// name; this file exists so TestEveryLegWiresEveryRoleContract counts this -// leg, and so the cases start running for real the moment this leg's fixture -// kit grows a non-nil hook. -func TestCrossRecordInvariantContract(t *testing.T) { - ctx := context.Background() - fixture := conformance.CrossRecordInvariantFixture{IssuePrefix: "xrec"} - - t.Run("SurvivesTwoPassingPerRecordGuards", func(t *testing.T) { - conformance.RunCrossRecordInvariantSurvivesTwoPassingPerRecordGuards(t, ctx, fixture) - }) - t.Run("StoreInvariantTransactionScopesExactlyTheSpanningRecords", func(t *testing.T) { - conformance.RunStoreInvariantTransactionScopesExactlyTheSpanningRecords(t, ctx, fixture) - }) - t.Run("InvariantRefusalNamesTheViolatedInvariant", func(t *testing.T) { - conformance.RunInvariantRefusalNamesTheViolatedInvariant(t, ctx, fixture) - }) - t.Run("MemoryKeyAliasUniquenessIsAStoreInvariantNotACAS", func(t *testing.T) { - conformance.RunMemoryKeyAliasUniquenessIsAStoreInvariantNotACAS(t, ctx, fixture) - }) - t.Run("MaximumEndpointMultiplicityIsAStoreInvariantNotACAS", func(t *testing.T) { - conformance.RunMaximumEndpointMultiplicityIsAStoreInvariantNotACAS(t, ctx, fixture) - }) - t.Run("AdvisoryLeaseAloneDoesNotPreventTheInvariantViolation", func(t *testing.T) { - conformance.RunAdvisoryLeaseAloneDoesNotPreventTheInvariantViolation(t, ctx, fixture) - }) - t.Run("LeaseAcquisitionRecordsNoHistoryEntry", func(t *testing.T) { - conformance.RunLeaseAcquisitionRecordsNoHistoryEntry(t, ctx, fixture) - }) -} diff --git a/internal/storage/uow/cycle_detector.go b/internal/storage/uow/cycle_detector.go index dfb916d9f..924c67395 100644 --- a/internal/storage/uow/cycle_detector.go +++ b/internal/storage/uow/cycle_detector.go @@ -40,8 +40,8 @@ var _ publicops.CycleDetector = (*cycleDetector)(nil) // rows that were on the cycle, rather than the rows that exist by the time the // second query runs. The two store-backed bodies get the same property from // their own read transaction. -func (c *cycleDetector) DetectCycles(ctx context.Context, req publicops.DetectCyclesRequest) (publicops.CycleReport, error) { +func (c *cycleDetector) DetectCycles(ctx context.Context, _ publicops.DetectCyclesRequest) (publicops.CycleReport, error) { return RunTxRead(ctx, c.provider, func(ctx context.Context, uw UnitOfWork) (publicops.CycleReport, error) { - return uw.DependencyUseCase().DetectCycleReport(ctx, req) + return uw.DependencyUseCase().DetectCycleReport(ctx) }) } diff --git a/internal/storage/uow/cycle_detector_contract_test.go b/internal/storage/uow/cycle_detector_contract_test.go index 83cea47c7..286eece89 100644 --- a/internal/storage/uow/cycle_detector_contract_test.go +++ b/internal/storage/uow/cycle_detector_contract_test.go @@ -36,15 +36,6 @@ func TestCycleDetectorContract(t *testing.T) { t.Run("FollowsOnlyBlockingEdges", func(t *testing.T) { conformance.RunCycleDetectorFollowsOnlyBlockingEdges(t, ctx, fixture) }) - t.Run("IncludeTracksIgnoresAPureTracksLoop", func(t *testing.T) { - conformance.RunCycleDetectorIncludeTracksIgnoresAPureTracksLoop(t, ctx, fixture) - }) - t.Run("IncludeTracksFindsTheMoleculeRootShape", func(t *testing.T) { - conformance.RunCycleDetectorIncludeTracksFindsTheMoleculeRootShape(t, ctx, fixture) - }) - t.Run("IncludeTracksWalksEachEdgeInItsStoredDirection", func(t *testing.T) { - conformance.RunCycleDetectorIncludeTracksWalksEachEdgeInItsStoredDirection(t, ctx, fixture) - }) t.Run("ReportsAnHonestPartial", func(t *testing.T) { conformance.RunCycleDetectorReportsAnHonestPartial(t, ctx, fixture) }) diff --git a/internal/storage/uow/data_behind_migrate_gate_test.go b/internal/storage/uow/data_behind_migrate_gate_test.go index 7cd51cba2..d137d0488 100644 --- a/internal/storage/uow/data_behind_migrate_gate_test.go +++ b/internal/storage/uow/data_behind_migrate_gate_test.go @@ -131,8 +131,8 @@ func TestInitSchemaProxiedDataBehind(t *testing.T) { } } // The env hatch is not the only consent surface that stays reachable - // here: the proxied capability registry keys on the command path and - // permits `migrate schema`, so the forced schema verb gets through too + // here: the proxy refusal table keys on the command path and has no + // `migrate schema` row, so the forced schema verb falls through too // (pinned in cmd/bd by TestProxyMaintenanceAllowsTheSharedConsentVerb). // Both are read by forceOrEnvConsent before this stop is routed, so a // warning that names only the refused bare `bd migrate --force` leaves diff --git a/internal/storage/uow/delete_fence.go b/internal/storage/uow/delete_fence.go deleted file mode 100644 index e880feae7..000000000 --- a/internal/storage/uow/delete_fence.go +++ /dev/null @@ -1,151 +0,0 @@ -package uow - -import ( - "context" - "database/sql" - "database/sql/driver" - "errors" - "fmt" - "hash/fnv" - "time" -) - -const ( - // deleteFenceLockPrefix namespaces this fence within Dolt/MySQL's - // GET_LOCK name space, which is server-wide rather than per-database - // (mirrors internal/storage/dolt's deleteFenceLockPrefix and - // schema.MigrationLockName's bd_schema_init: prefix, same reason): two - // beads databases sharing one Dolt sql-server must not collide on the - // same issue id. - deleteFenceLockPrefix = "bd_delete:" - // deleteFenceLockNameMaxLength mirrors MySQL/Dolt's 64-byte GET_LOCK name limit. - deleteFenceLockNameMaxLength = 64 - // deleteFenceLockTimeoutSeconds bounds how long a guarded delete waits to - // enter the fence. It must comfortably exceed RunTxResult's own - // DefaultTxRetryMaxElapsed (15s): a racer waiting its turn can be queued - // behind another racer that is itself retrying a serialization failure - // for up to that long. - deleteFenceLockTimeoutSeconds = 20 - // deleteFenceReleaseTimeout bounds the RELEASE_LOCK call, run on its own - // context so a caller-canceled ctx cannot strand the lock held. - deleteFenceReleaseTimeout = 5 * time.Second -) - -// ErrDeleteFenceUnavailable marks a transient failure to acquire the per-row -// delete fence lock (timeout or connection error), distinct from a version -// mismatch: it means this guarded delete never got a chance to check the -// row's version at all. -var ErrDeleteFenceUnavailable = errors.New("delete fence lock unavailable") - -// deleteFenceLockName returns a Dolt/MySQL named-lock key within the 64-byte -// limit, hashing down to fnv64a when the readable form would not fit — -// mirrors internal/storage/dolt's deleteFenceLockName (and, one level further -// back, schema.MigrationLockName). -func deleteFenceLockName(databaseName, id string) string { - raw := deleteFenceLockPrefix + databaseName + ":" + id - if len(raw) <= deleteFenceLockNameMaxLength { - return raw - } - h := fnv.New64a() - _, _ = h.Write([]byte(databaseName + ":" + id)) - return fmt.Sprintf("%s%016x", deleteFenceLockPrefix, h.Sum64()) -} - -// withDeleteFence serializes guarded (ExpectedVersion-checked) deletes for a -// single id against each other on a Dolt sql-server, the proxied-mode / -// bd-serve twin of internal/storage/dolt's (*DoltStore).withDeleteFence -// (mc-zndi7.73). -// -// Dolt's commit-time merge treats two concurrent transactions that each -// delete the same row as identical diffs ("row absent") and lands BOTH with -// no conflict — unlike a concurrent update/close racing the same delete, -// which already collides for real because its row_lock bump is a genuine -// edit the merge must reconcile against the delete. That asymmetry is the -// bug: every same-token deleter exits 0, and there is no schema cell a -// guarded delete can write to manufacture a real conflict, because the row -// it would write to is the row it is erasing. -// -// A session-scoped GET_LOCK, acquired before fn runs and released only after -// fn returns, turns that silent double-delete into a queue instead: the -// loser's unit of work begins only after the winner's has already committed -// and is durable, so its existence probe inside deleteInUOW finds the row -// gone and fails through the ordinary NotFoundError path instead of silently -// "succeeding" a second time. No schema or migration change is needed — -// GET_LOCK/RELEASE_LOCK are built-in Dolt/MySQL session primitives, already -// an established repo idiom (schema.MigrateUpWithLock, -// internal/storage/dolt's own delete fence). -// -// UNLIKE internal/storage/dolt's fence, the lock connection here is NOT the -// connection fn's write runs on: fn is left free to call RunTxResult, which -// draws its own (possibly several, across retries) connection from p.db the -// ordinary way. That is safe for exactly the reason the dolt-package fence's -// doc calls out as the hazard to avoid there: THIS provider's pool is never -// constrained to a single connection (no cmd/bd call site ever calls -// SetPoolLimits on the proxied-server or bd-serve UOW chain — see pool.go), -// so a dedicated lock connection can never starve the write of its own -// connection. What matters for correctness is not which physical connection -// runs the write but WHEN its transaction begins: fn (and therefore every -// snapshot it establishes, across every retry) starts only after GET_LOCK has -// already returned success, so a queued loser's first read can only ever -// observe the winner's commit as already landed and durable. -func withDeleteFence(ctx context.Context, p *doltSQLProvider, id string, fn func() error) error { - conn, err := p.db.Conn(ctx) - if err != nil { - return fmt.Errorf("delete fence: acquire connection: %w", err) - } - defer func() { _ = conn.Close() }() - - dbName, err := currentDatabaseName(ctx, conn) - if err != nil { - return fmt.Errorf("delete fence: %w", err) - } - - lockName := deleteFenceLockName(dbName, id) - var locked sql.NullInt64 - if err := conn.QueryRowContext(ctx, "SELECT GET_LOCK(?, ?)", lockName, deleteFenceLockTimeoutSeconds).Scan(&locked); err != nil { - return fmt.Errorf("delete fence: acquire lock: %w: %w", ErrDeleteFenceUnavailable, err) - } - if !locked.Valid || locked.Int64 != 1 { - return fmt.Errorf("delete fence: acquire lock: %w: timed out after %ds", ErrDeleteFenceUnavailable, deleteFenceLockTimeoutSeconds) - } - defer releaseDeleteFenceLock(conn, lockName) - - return fn() -} - -// currentDatabaseName reads the database conn's session is attached to. -// doltSQLProvider carries no database field of its own (the name lives only -// in the DSN each pool connection was opened with), and every connection -// drawn from p.db is already attached to it — buildDSN always includes the -// database component — so SELECT DATABASE() reliably reports the same name -// internal/storage/dolt's fence reads from its store's own field. -func currentDatabaseName(ctx context.Context, conn *sql.Conn) (string, error) { - var name sql.NullString - if err := conn.QueryRowContext(ctx, "SELECT DATABASE()").Scan(&name); err != nil { - return "", fmt.Errorf("select database(): %w", err) - } - return name.String, nil -} - -// releaseDeleteFenceLock releases a lock acquired by withDeleteFence from the -// same session, after fn has returned. It runs on its own short-lived context -// so a caller-canceled ctx cannot strand the lock held past this call. -// -// An unexpected result discards the physical connection (same defensive -// pattern internal/storage/dolt's releaseDeleteFenceLock and schema's -// discardConn use) rather than returning it to the pool: the lock's true -// state on that session is now unknown, and handing it to another caller -// could let a future guarded delete believe it holds the fence when it does -// not. -func releaseDeleteFenceLock(conn *sql.Conn, lockName string) { - cleanupCtx, cancel := context.WithTimeout(context.Background(), deleteFenceReleaseTimeout) - defer cancel() - - var released sql.NullInt64 - err := conn.QueryRowContext(cleanupCtx, "SELECT RELEASE_LOCK(?)", lockName).Scan(&released) - if err != nil || !released.Valid || released.Int64 != 1 { - _ = conn.Raw(func(driverConn any) error { - return driver.ErrBadConn - }) - } -} diff --git a/internal/storage/uow/deleter.go b/internal/storage/uow/deleter.go index 11358aceb..ad3b808c6 100644 --- a/internal/storage/uow/deleter.go +++ b/internal/storage/uow/deleter.go @@ -64,51 +64,16 @@ func (d *deleter) Delete(ctx context.Context, req publicops.DeleteRequest) (publ return deleteInUOW(ctx, uw, req) }) } - - write := func(ctx context.Context) (publicops.DeleteResult, error) { - return RunTxResult(ctx, d.provider, func(ctx context.Context, uw UnitOfWork) (publicops.DeleteResult, string, error) { - result, err := deleteInUOW(ctx, uw, req) - if err != nil || result.Deleted == 0 { - // A deletion that removed nothing labels nothing: the role - // promises at most one history entry per call and none for a - // no-op. - return result, "", err - } - return result, fmt.Sprintf("bd: delete %d issue(s)", result.Deleted), nil - }) - } - - // A guarded (ExpectedVersion-checked) delete additionally runs under the - // per-id delete fence (mc-zndi7.73): without it, two same-token deletes - // race to identical "row absent" diffs that Dolt's commit-time merge - // lands with no conflict, so every racer exits 0 — see withDeleteFence's - // doc for the full design. Every doltSQLProvider here IS a real Dolt - // sql-server session (proxied mode or bd serve; there is no embedded - // backend in this package), so unlike internal/storage/dolt's fence - // (scoped to serverMode only) this one applies whenever a guard is - // present, with no mode check. - // - // d.provider is unwrapped first because the real chain - // (wireProxiedUOWProvider) wraps the bare *doltSQLProvider in a - // notifyingProvider and then an externaldeps uowProvider — both of which - // pass THEMSELVES, not the inner provider, to NewDeleter, so d.provider - // is a decorator here in every real deployment, never the bare provider. - if req.ExpectedVersion != nil { - if sp, ok := UnwrapProvider(d.provider).(*doltSQLProvider); ok { - var result publicops.DeleteResult - err := withDeleteFence(ctx, sp, req.IDs[0], func() error { - var ferr error - result, ferr = write(ctx) - return ferr - }) - if err != nil { - return publicops.DeleteResult{}, err - } - return result, nil + return RunTxResult(ctx, d.provider, func(ctx context.Context, uw UnitOfWork) (publicops.DeleteResult, string, error) { + result, err := deleteInUOW(ctx, uw, req) + if err != nil || result.Deleted == 0 { + // A deletion that removed nothing labels nothing: the role + // promises at most one history entry per call and none for a + // no-op. + return result, "", err } - } - - return write(ctx) + return result, fmt.Sprintf("bd: delete %d issue(s)", result.Deleted), nil + }) } // deleteInUOW is the whole deletion on one unit of work, shared by the preview @@ -164,7 +129,8 @@ func deleteInUOW(ctx context.Context, uw UnitOfWork, req publicops.DeleteRequest // duplicates before either ran. if req.ExpectedVersion != nil { if current := present[req.IDs[0]].RowVersion; current != *req.ExpectedVersion { - return publicops.DeleteResult{}, &publicops.VersionMismatchError{Expected: *req.ExpectedVersion, Current: current} + return publicops.DeleteResult{}, fmt.Errorf("%w: expected %d, got %d", + publicops.ErrVersionMismatch, *req.ExpectedVersion, current) } } diff --git a/internal/storage/uow/dependency_editor_contract_test.go b/internal/storage/uow/dependency_editor_contract_test.go index 3d2cc274a..7efe4364b 100644 --- a/internal/storage/uow/dependency_editor_contract_test.go +++ b/internal/storage/uow/dependency_editor_contract_test.go @@ -5,7 +5,6 @@ import ( "testing" "github.com/steveyegge/beads/backend/conformance" - "github.com/steveyegge/beads/internal/storage" ) // TestUOWDependencyEditorContract runs the shared DependencyEditor contract @@ -30,9 +29,6 @@ func TestUOWDependencyEditorContract(t *testing.T) { {name: "RefusesCrossPlaneCycle", run: conformance.RunDependencyEditorRefusesCrossPlaneCycle}, {name: "AddedEchoesTheRequestOrder", run: conformance.RunDependencyEditorAddedEchoesTheRequestOrder}, {name: "SameTypeReAddIsIdempotent", run: conformance.RunDependencyEditorSameTypeReAddIsIdempotent}, - {name: "SameTypeReAddWithChangedMetadataMintsOneVersion", run: conformance.RunDependencyEditorSameTypeReAddWithChangedMetadataMintsOneVersion}, - {name: "SameTypeReAddWithIdenticalMetadataIsANoOp", run: conformance.RunDependencyEditorSameTypeReAddWithIdenticalMetadataIsANoOp}, - {name: "SameTypeReAddWithChangedThreadMintsOneVersion", run: conformance.RunDependencyEditorSameTypeReAddWithChangedThreadMintsOneVersion}, {name: "RepeatsWithinOneRequestCollapse", run: conformance.RunDependencyEditorRepeatsWithinOneRequestCollapse}, {name: "AttributesItsEventsToTheActor", run: conformance.RunDependencyEditorAttributesItsEventsToTheActor}, {name: "RetypeRefusalLeavesTheOriginalEdge", run: conformance.RunDependencyEditorRetypeRefusalLeavesTheOriginalEdge}, @@ -66,7 +62,6 @@ func TestUOWDependencyEditorContract(t *testing.T) { {name: "AcceptsADiamond", run: conformance.RunDependencyEditorAcceptsADiamond}, {name: "GateScopeFollowsTheEdgeType", run: conformance.RunDependencyEditorGateScopeFollowsTheEdgeType}, {name: "AcceptsBlockingAcrossIssueTypes", run: conformance.RunDependencyEditorAcceptsBlockingAcrossIssueTypes}, - {name: "RefusesADottedChildGatedOnItsOwnParent", run: conformance.RunDependencyEditorRefusesADottedChildGatedOnItsOwnParent}, } { t.Run(test.name, func(t *testing.T) { test.run(t, ctx, fixture) @@ -84,24 +79,14 @@ func newUOWDependencyEditorFixture(t *testing.T, ctx context.Context) conformanc if err != nil { t.Fatalf("NewDependencyEditor: %v", err) } - configurer, ok := provider.(storage.EventsJournalConfigurer) - if !ok { - t.Fatalf("provider %T does not implement storage.EventsJournalConfigurer", provider) - } - versionConfigurer, ok := provider.(storage.VersionedHistoryConfigurer) - if !ok { - t.Fatalf("provider %T does not implement storage.VersionedHistoryConfigurer", provider) - } kit := newUOWRoleFixtureKit(provider, "bd") return conformance.DependencyEditorFixture{ - IssuePrefix: kit.IssuePrefix, - Editor: editor, - CreateIssue: kit.CreateIssue, - CreateWisp: kit.CreateWisp, - AddDependency: kit.AddDependency, - QueryScalar: kit.QueryScalar, - CountHistory: kit.CountHistory, - SetJournalEnabled: configurer.SetEventsJournalEnabled, - SetVersionedHistoryEnabled: versionConfigurer.SetVersionedHistoryEnabled, + IssuePrefix: kit.IssuePrefix, + Editor: editor, + CreateIssue: kit.CreateIssue, + CreateWisp: kit.CreateWisp, + AddDependency: kit.AddDependency, + QueryScalar: kit.QueryScalar, + CountHistory: kit.CountHistory, } } diff --git a/internal/storage/uow/dolt_sql_provider.go b/internal/storage/uow/dolt_sql_provider.go index 23d49c6a8..b32def7ea 100644 --- a/internal/storage/uow/dolt_sql_provider.go +++ b/internal/storage/uow/dolt_sql_provider.go @@ -16,8 +16,6 @@ import ( "github.com/cenkalti/backoff/v4" "github.com/go-sql-driver/mysql" - "github.com/steveyegge/beads/internal/configfile" - "github.com/steveyegge/beads/internal/debug" "github.com/steveyegge/beads/internal/storage" "github.com/steveyegge/beads/internal/storage/dbproxy/proxy" "github.com/steveyegge/beads/internal/storage/dbproxy/util" @@ -29,7 +27,7 @@ import ( const ( defaultBranch = "main" - defaultProxyIdleTimeout = configfile.DefaultProxyIdleTimeout + defaultProxyIdleTimeout = 30 * time.Second ) type doltSQLProvider struct { @@ -62,9 +60,6 @@ type doltSQLProvider struct { // eventsJournalEnabled activates the durable events journal for THIS // provider instance only. See SetEventsJournalEnabled. eventsJournalEnabled atomic.Bool - // versionedHistoryEnabled activates dual-write issue-version history for - // THIS provider instance only. See SetVersionedHistoryEnabled. - versionedHistoryEnabled atomic.Bool } // SetEventsJournalEnabled activates the durable events journal for every unit @@ -81,13 +76,6 @@ func (p *doltSQLProvider) SetEventsJournalEnabled(enabled bool) { p.eventsJournalEnabled.Store(enabled) } -// SetVersionedHistoryEnabled activates dual-write issue-version history for -// every unit of work this provider begins from now on. Per instance, never -// process-global — see SetEventsJournalEnabled. -func (p *doltSQLProvider) SetVersionedHistoryEnabled(enabled bool) { - p.versionedHistoryEnabled.Store(enabled) -} - type bootstrapPreparationError struct { err error retryable bool @@ -180,10 +168,9 @@ func applyProviderOptions(opts []ProviderOption) providerOptions { } var ( - _ UnitOfWorkProvider = (*doltSQLProvider)(nil) - _ TxProvider = (*doltSQLProvider)(nil) - _ storage.EventsJournalConfigurer = (*doltSQLProvider)(nil) - _ storage.VersionedHistoryConfigurer = (*doltSQLProvider)(nil) + _ UnitOfWorkProvider = (*doltSQLProvider)(nil) + _ TxProvider = (*doltSQLProvider)(nil) + _ storage.EventsJournalConfigurer = (*doltSQLProvider)(nil) ) func (p *doltSQLProvider) NewUOW(ctx context.Context) (UnitOfWork, error) { @@ -211,30 +198,25 @@ func (p *doltSQLProvider) BeginTx(ctx context.Context) (Tx, error) { return nil, fmt.Errorf("uow: failed to start transaction: %w", err) } - // Bind journal and versioned-history activation to the connection this unit - // of work is pinned to, AFTER START TRANSACTION so the seq allocation's - // UPDATE and the SELECT that must observe it are inside one transaction on - // one session. Both scopes are released when the connection is - // (doltServerTx.releaseConn / poisonConn), so an entry cannot outlive its - // transaction. The blocked-recheck scope is bound and released the same - // way; Commit takes what it recorded once the transaction has committed. + // Bind journal activation to the connection this unit of work is pinned to, + // AFTER START TRANSACTION so the seq allocation's UPDATE and the SELECT that + // must observe it are inside one transaction on one session. The scope is + // released when the connection is (doltServerTx.releaseConn / poisonConn), + // so an entry cannot outlive its transaction. The blocked-recheck scope is + // bound and released the same way; Commit takes what it recorded once the + // transaction has committed. return &doltServerTx{ conn: conn, clearJournalScope: issueops.ScopeEventsJournalTransaction(conn, p.eventsJournalEnabled.Load()), - clearVersionScope: issueops.ScopeVersionedHistoryTransaction(conn, p.versionedHistoryEnabled.Load()), clearRecheckScope: issueops.ScopeBlockedRecheckTransaction(conn), }, nil } // selectProbeDatabase lets schema's pre-lock convergence probe reach the -// database on a session that is not yet on one. openAndInitSchema has two entry -// shapes since wy-s8ytnw, and only one of them needs this: the steady-state -// open connects straight to the target database, so the probe reads it from -// DATABASE() and returns before consulting the selector at all -// (schema.selectTargetDatabase). The fall-through open still pins its -// schema-init pool with an EMPTY DSN database, and there, without this, the -// probe reads NULL from DATABASE(), declines, and every invocation queues on -// the server-wide migration lock it exists to skip. +// database on a session that is not yet on one. openAndInitSchema pins its +// schema-init pool with an EMPTY DSN database, so without this the probe reads +// NULL from DATABASE(), declines, and every invocation queues on the +// server-wide migration lock it exists to skip. // // The USE MUST remain the DDL repository's own UseDatabase — the exact // statement prepareBootstrap issues below. That identity is the reason the @@ -684,13 +666,9 @@ const pingAttemptTimeout = 10 * time.Second // application-level rejection that can reach us as one, so at boot an OpError // is always the connection itself: refused while the server restarts, reset, // or broken pipe. MySQL's own rejections arrive as *mysql.MySQLError and stay -// permanent here. A name that does not resolve is the exception — it will not -// start resolving, so retrying only spends the whole budget before reporting -// the same misconfiguration. -// -// The db proxy's own upstream-outage report (proxy.IsUpstreamOutageError) is -// a *mysql.MySQLError too, so this function calls it permanent; pingWithRetry -// handles it first and retries it only for upstreamOutageRetryWindow. +// permanent. A name that does not resolve is the exception — it will not start +// resolving, so retrying only spends the whole budget before reporting the +// same misconfiguration. // // context.DeadlineExceeded counts only because pingWithRetry applies a // per-attempt deadline and checks the caller's context *before* consulting @@ -721,45 +699,12 @@ func isTransientPingError(err error) bool { errors.As(err, &opErr) } -// upstreamOutageRetryWindow is how long pingWithRetry keeps retrying the db -// proxy's report that its external upstream is unreachable (refused, socket -// gone, or closed before the greeting; see dbproxy/proxy/upstream_error.go). -// Long enough to ride out an endpoint that is flapping or rebinding (a Dolt -// restart, a front whose target is being replaced, a server briefly at its -// connection limit), short enough that a real outage fails within a couple of -// seconds instead of spending the full 30s bootstrap budget on a dead -// upstream. A var so tests can shorten it. -var upstreamOutageRetryWindow = time.Second - -// outageWindowBackOff clamps the wrapped backoff so that, once the first -// upstream-outage report has been seen, no wait runs past the end of -// upstreamOutageRetryWindow: the last attempt lands at the window's end -// rather than up to one backoff interval after it. -type outageWindowBackOff struct { - backoff.BackOff - deadline time.Time // zero until the first upstream-outage report -} - -func (b *outageWindowBackOff) NextBackOff() time.Duration { - d := b.BackOff.NextBackOff() - if d == backoff.Stop || b.deadline.IsZero() { - return d - } - if remaining := time.Until(b.deadline); remaining < d { - return max(remaining, 0) - } - return d -} - // pingWithRetry pings until the server answers, the error proves durable, or // bo's MaxElapsedTime is spent. Each attempt gets its own deadline: // backoff.Retry bounds the gap between attempts but cannot interrupt one // already in flight, so without this cap a server that accepts TCP and then -// stalls blocks for the caller's entire context. The db proxy's -// upstream-outage report is retried only for upstreamOutageRetryWindow from -// the first time it is seen. +// stalls blocks for the caller's entire context. func pingWithRetry(ctx context.Context, p pinger, bo *backoff.ExponentialBackOff, attemptTimeout time.Duration) error { - window := &outageWindowBackOff{BackOff: bo} return backoff.Retry(func() error { attemptCtx, cancel := context.WithTimeout(ctx, attemptTimeout) defer cancel() @@ -775,20 +720,11 @@ func pingWithRetry(ctx context.Context, p pinger, bo *backoff.ExponentialBackOff if ctx.Err() != nil { return backoff.Permanent(err) } - if proxy.IsUpstreamOutageError(err) { - if window.deadline.IsZero() { - window.deadline = time.Now().Add(upstreamOutageRetryWindow) - } - if !time.Now().Before(window.deadline) { - return backoff.Permanent(err) - } - return err - } if isTransientPingError(err) { return err } return backoff.Permanent(err) - }, backoff.WithContext(window, ctx)) + }, backoff.WithContext(bo, ctx)) } // assertSessionDatabaseOnPool runs assertSessionDatabase on a connection pinned @@ -803,133 +739,35 @@ func assertSessionDatabaseOnPool(ctx context.Context, pool *sql.DB, want string) return assertSessionDatabase(ctx, conn, want) } -// openDB opens dsn and waits out a server that is still coming up, retrying -// transient ping failures for up to 30s (#6003). func openDB(ctx context.Context, dsn string) (*sql.DB, error) { - bo := backoff.NewExponentialBackOff() - bo.MaxElapsedTime = 30 * time.Second - return openPool(dsn, func(conn *sql.DB) error { - return pingWithRetry(ctx, conn, bo, pingAttemptTimeout) - }) -} - -// openDBProbe opens dsn and proves it with ONE bounded ping instead of -// openDB's 30s retry budget. It exists for openAndInitSchema's fast path, -// whose failure is not terminal: every error there falls through to the -// historical open, which keeps the retry budget it has always owned. Retrying -// here as well would charge an unreachable server twice — this budget and then -// the fall-through's, ~60s where the pre-fast-path code spent 30s — to answer -// a question one ping answers. The sibling fast path in internal/storage/dolt -// (openServerConnection's bare PingContext ahead of its no-database init -// connection) is deliberately the same shape. -// -// The one attempt is still capped at pingAttemptTimeout, for the reason -// pingWithRetry caps its own attempts: the DSN sets no ReadTimeout, so a server -// that accepts TCP and then stalls mid-handshake is otherwise bounded only by -// the caller's context. -func openDBProbe(ctx context.Context, dsn string) (*sql.DB, error) { - return openPool(dsn, func(conn *sql.DB) error { - pingCtx, cancel := context.WithTimeout(ctx, pingAttemptTimeout) - defer cancel() - return conn.PingContext(pingCtx) - }) -} - -// openPool opens dsn and hands the pool to prove, which must establish that -// the server answers on it. The error texts are load-bearing — callers match -// on "uow: open db" and "uow: ping db" — and a pool whose prove fails is -// closed here rather than leaked to a caller that only sees an error. -func openPool(dsn string, prove func(*sql.DB) error) (*sql.DB, error) { conn, err := sql.Open("mysql", dsn) if err != nil { return nil, fmt.Errorf("uow: open db: %w", err) } - if err := prove(conn); err != nil { + bo := backoff.NewExponentialBackOff() + bo.MaxElapsedTime = 30 * time.Second + if err := pingWithRetry(ctx, conn, bo, pingAttemptTimeout); err != nil { return nil, errors.Join(fmt.Errorf("uow: ping db: %w", err), conn.Close()) } return conn, nil } func openAndInitSchema(ctx context.Context, ep proxy.Endpoint, database, rootUser, rootPassword, tlsConfigName string, teamServer bool, expectedProjectID string, opts providerOptions) (UnitOfWorkProvider, error) { - newProvider := func(pool *sql.DB) *doltSQLProvider { - return &doltSQLProvider{ - defaultBranch: defaultBranch, - db: pool, - serverEndpoint: "tcp:" + ep.Address(), - teamServer: teamServer, - expectedProjectID: expectedProjectID, - preview: opts.preview, - readOnly: opts.readOnly, - proxiedServerMode: opts.proxiedServerMode, - } - } - - // Fast path (wy-s8ytnw): connect straight to the target database. A - // successful connect IS the existence proof (the same reasoning the - // dolt store applies to Gateway servers), so the steady-state open — - // a database that already exists, which is every open but the very - // first — costs ONE MySQL session instead of two: initSchema runs on - // this pool (USE + the converged-schema reads, or a real migration - // when one is pending; the bare CREATE DATABASE inside the locked - // preparation is refused with 1007 exactly as it is on a no-database - // connection, so `created` stays false and fresh-bootstrap heal can - // never be armed by a database this call did not create), and the - // same pool is then handed to the provider. - // - // Reusing the migrating pool is safe — but NOT because migrations leave - // the session alone. They do not: six shipped migrations issue - // `SET FOREIGN_KEY_CHECKS = 0` and restore `= 1` (0041, 0043, 0047, - // 0050, 0053, 0058 under internal/storage/schema/migrations), and the - // guarded ones leave user variables (`SET @sql`, `SET @needs_add`) set. - // It is safe because of three narrower facts: - // - initSchemaAttempt pins ONE session per attempt (p.db.Conn), so a - // script's SET and its restoring SET cannot land on different - // sessions; - // - every script that clears FOREIGN_KEY_CHECKS restores it before it - // can report success, and a leftover user variable is inert: nothing - // outside these scripts reads one, and each script assigns before it - // reads; - // - every initSchema failure below closes this whole pool, so a script - // that died between the two SETs never hands its session back. - // That is a convention where it used to be structural: before this fast - // path the schema-init pool was always discarded, so a migration could - // leak session state with impunity. Now a migration that leaves a system - // session variable changed on the success path would leak it into live - // query sessions — and an attempt that dies mid-script returns its - // connection to the pool, so the restore has to be part of the script, - // not of the caller. See internal/storage/schema/migrations/README.md. - // - // Any connect failure — the database does not exist yet (1049), the - // server is down, bad credentials — falls through to the historical - // no-database init connection, which owns creation, the ownership - // signal, and every error message callers already match on. The probe is - // single-shot (openDBProbe) precisely so that fall-through cannot cost a - // second full retry budget. - probeConn, probeErr := openDBProbe(ctx, buildDSN(ep, database, rootUser, rootPassword, tlsConfigName)) - if probeErr == nil { - provider := newProvider(probeConn) - if err := provider.initSchema(ctx, database); err != nil { - _ = probeConn.Close() - return nil, fmt.Errorf("uow: init schema: %w", err) - } - return provider, nil - } - - // Advisory only: the fall-through below is the historical open, so this is - // never fatal. But a silently discarded error is a fast path that has - // quietly stopped firing — here that means every bd invocation is back to - // burning two MySQL sessions, the exact cost this path exists to remove, - // with nothing to say so. Same reasoning as the convergence probe in - // internal/storage/schema/lock.go. - debug.Logf("uow: single-session open unavailable for %q, using the no-database init connection: %v\n", - database, probeErr) - initDB, err := openDB(ctx, buildDSN(ep, "", rootUser, rootPassword, tlsConfigName)) if err != nil { return nil, err } - initProvider := newProvider(initDB) + initProvider := &doltSQLProvider{ + defaultBranch: defaultBranch, + db: initDB, + serverEndpoint: "tcp:" + ep.Address(), + teamServer: teamServer, + expectedProjectID: expectedProjectID, + preview: opts.preview, + readOnly: opts.readOnly, + proxiedServerMode: opts.proxiedServerMode, + } if err := initProvider.initSchema(ctx, database); err != nil { _ = initDB.Close() @@ -954,5 +792,14 @@ func openAndInitSchema(ctx context.Context, ep proxy.Endpoint, database, rootUse return nil, errors.Join(err, dbConn.Close()) } - return newProvider(dbConn), nil + return &doltSQLProvider{ + defaultBranch: defaultBranch, + db: dbConn, + serverEndpoint: "tcp:" + ep.Address(), + teamServer: teamServer, + expectedProjectID: expectedProjectID, + preview: opts.preview, + readOnly: opts.readOnly, + proxiedServerMode: opts.proxiedServerMode, + }, nil } diff --git a/internal/storage/uow/dolt_sql_provider_lock_test.go b/internal/storage/uow/dolt_sql_provider_lock_test.go index e897f83f2..d276cbe57 100644 --- a/internal/storage/uow/dolt_sql_provider_lock_test.go +++ b/internal/storage/uow/dolt_sql_provider_lock_test.go @@ -173,10 +173,9 @@ func TestInitSchemaConvergenceProbeRunsWithNoSessionDatabase(t *testing.T) { // migrationWorkNeeded's first question, reached only because the probe put // the session on the database itself. The cursor is far behind this // binary, so there is real work and the fast path declines. - mock.ExpectQuery(regexp.QuoteMeta("SELECT COUNT(*) FROM information_schema.schemata WHERE schema_name = DATABASE()")). + mock.ExpectQuery(`SELECT COUNT\(\*\) FROM information_schema\.tables`). + WithArgs("schema_migrations"). WillReturnRows(sqlmock.NewRows([]string{"count"}).AddRow(1)) - mock.ExpectQuery("^SHOW TABLES$"). - WillReturnRows(sqlmock.NewRows([]string{"Tables_in_beads"}).AddRow("schema_migrations")) mock.ExpectQuery(regexp.QuoteMeta("SELECT COALESCE(MAX(version), 0) FROM schema_migrations")). WillReturnRows(sqlmock.NewRows([]string{"version"}).AddRow(1)) diff --git a/internal/storage/uow/doltserver_provider_test.go b/internal/storage/uow/doltserver_provider_test.go index dab2f6174..8d84bbd3f 100644 --- a/internal/storage/uow/doltserver_provider_test.go +++ b/internal/storage/uow/doltserver_provider_test.go @@ -16,7 +16,6 @@ import ( "github.com/steveyegge/beads/internal/storage/dbproxy/proxy" "github.com/steveyegge/beads/internal/testutil" - "github.com/steveyegge/beads/internal/testutil/bazeltest" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -90,7 +89,11 @@ func TestNewDoltServerUOWProvider_HappyPath(t *testing.T) { require.NoError(t, err) storeRootDir := t.TempDir() shutdownOnInterrupt(t, storeRootDir) - verifiedShutdownCleanup(t, storeRootDir) + t.Cleanup(func() { + if err := proxy.Shutdown(storeRootDir); err != nil { + t.Logf("proxy.Shutdown(%s): %v", storeRootDir, err) + } + }) cfgPath := writeServerConfig(t, port) logPath := filepath.Join(t.TempDir(), "server.log") @@ -115,69 +118,6 @@ func TestNewDoltServerUOWProvider_HappyPath(t *testing.T) { t.Cleanup(func() { _ = provider.Close(context.Background()) }) } -// TestNewDoltServerUOWProvider_CarriesProxiedServerMode pins the link of the -// proxied-server marker that the data-behind gate tests cannot see: they build -// the provider with the field already set, so they stay green if the open -// stops copying the option into the providers it builds, the one the gate -// runs on included. Both polarities, because the open `bd serve` makes for a -// server-mode workspace passes no option and must keep the runnable-here -// remedy. The two opens share one server. -func TestNewDoltServerUOWProvider_CarriesProxiedServerMode(t *testing.T) { - testutil.RequireDoltBinary(t) - bin, err := exec.LookPath("dolt") - require.NoError(t, err) - - bdBin := buildBDBinary(t) - prev := proxy.ResolveExecutable - proxy.ResolveExecutable = func() (string, error) { return bdBin, nil } - t.Cleanup(func() { proxy.ResolveExecutable = prev }) - - t.Setenv("HOME", t.TempDir()) - - port, err := proxy.PickFreePort() - require.NoError(t, err) - storeRootDir := t.TempDir() - shutdownOnInterrupt(t, storeRootDir) - verifiedShutdownCleanup(t, storeRootDir) - cfgPath := writeServerConfig(t, port) - logPath := filepath.Join(t.TempDir(), "server.log") - - for _, tt := range []struct { - name string - opts []ProviderOption - want bool - }{ - {name: "server-mode open", want: false}, - {name: "proxied-server open", opts: []ProviderOption{WithProxiedServerMode()}, want: true}, - } { - t.Run(tt.name, func(t *testing.T) { - provider, err := NewDoltServerUOWProvider( - context.Background(), - storeRootDir, - "beads", - logPath, - cfgPath, - proxy.BackendLocalServer, - "root", - "", - bin, - 0, - 0, - false, - "", - tt.opts..., - ) - require.NoError(t, err) - require.NotNil(t, provider) - t.Cleanup(func() { _ = provider.Close(context.Background()) }) - - sqlProvider, ok := provider.(*doltSQLProvider) - require.True(t, ok, "NewDoltServerUOWProvider returned %T, want *doltSQLProvider", provider) - assert.Equal(t, tt.want, sqlProvider.proxiedServerMode, "provider.proxiedServerMode") - }) - } -} - func TestNewDoltServerUOWProvider_ConcurrentInstantiation(t *testing.T) { testutil.RequireDoltBinary(t) bin, err := exec.LookPath("dolt") @@ -194,7 +134,11 @@ func TestNewDoltServerUOWProvider_ConcurrentInstantiation(t *testing.T) { require.NoError(t, err) storeRootDir := t.TempDir() shutdownOnInterrupt(t, storeRootDir) - verifiedShutdownCleanup(t, storeRootDir) + t.Cleanup(func() { + if err := proxy.Shutdown(storeRootDir); err != nil { + t.Logf("proxy.Shutdown(%s): %v", storeRootDir, err) + } + }) cfgPath := writeServerConfig(t, port) logPath := filepath.Join(t.TempDir(), "server.log") @@ -254,12 +198,11 @@ var ( func buildBDBinary(t *testing.T) string { t.Helper() bdBinaryOnce.Do(func() { - prebuilt, err := bazeltest.PrebuiltBD() - if err != nil { - bdBinaryErr = err - return - } - if prebuilt != "" { + if prebuilt := os.Getenv("BEADS_TEST_BD_BINARY"); prebuilt != "" { + if _, err := os.Stat(prebuilt); err != nil { + bdBinaryErr = fmt.Errorf("BEADS_TEST_BD_BINARY=%q not found: %w", prebuilt, err) + return + } bdBinary = prebuilt return } diff --git a/internal/storage/uow/doltserver_tx.go b/internal/storage/uow/doltserver_tx.go index beebf330b..3a98f27ac 100644 --- a/internal/storage/uow/doltserver_tx.go +++ b/internal/storage/uow/doltserver_tx.go @@ -5,16 +5,9 @@ import ( "database/sql" "database/sql/driver" "errors" - "fmt" - "log" - "slices" - "time" - - "github.com/cenkalti/backoff/v4" "github.com/steveyegge/beads/internal/storage/domain/db" "github.com/steveyegge/beads/internal/storage/issueops" - "github.com/steveyegge/beads/internal/storage/schema" ) type doltServerTx struct { @@ -25,10 +18,6 @@ type doltServerTx struct { // releaseConn and poisonConn — so the activation entry cannot outlive the // transaction it describes, whichever way the transaction ends. clearJournalScope func() - // clearVersionScope is clearJournalScope's counterpart for dual-write - // issue-version history: same binding at BeginTx, same release at - // releaseConn/poisonConn. - clearVersionScope func() // clearRecheckScope is the same binding for the dependents this // transaction's unblocking writes record for the post-commit // blocked-state recheck (gastownhall/beads#6716). @@ -87,156 +76,6 @@ func (t *doltServerTx) Commit(ctx context.Context, message string) error { return t.closeOpenTxAfterFailure(ctx, err) } -const ( - // scopedCommitMaxElapsed bounds the retries of CommitScoped's Dolt commit. - // It is short for the reason the DoltStore's postTxCommitMaxElapsed is: - // the failure is logged rather than returned, so a long fight for a commit - // the next one on the branch carries anyway buys nothing. - scopedCommitMaxElapsed = 5 * time.Second - // scopedCommitGrace pads the detached context past the retry budget. The - // budget only stops scheduling attempts; the deadline is what cancels one - // already hung in flight. - scopedCommitGrace = 2 * time.Second -) - -// CommitScoped commits the transaction and then makes a Dolt commit of -// exactly tables (plus the versioned-history tables when this transaction -// could have minted) named message: DOLT_ADD per table, then -// DOLT_COMMIT('-m') — never '-Am', so another writer's uncommitted change -// elsewhere in the working set is neither committed nor attributed to this -// message. With nothing staged, or under a deferred version commit -// (dolt.auto-commit batch/off), there is no Dolt commit. -// -// THE DOLT COMMIT RUNS AFTER THE SQL COMMIT, never inside the transaction: -// the ordering contract of the DoltStore funnel (runIssueOperationTxWithMessage, -// LatentLabsSpace/NEXUS#92). DOLT_ADD stages whole tables from the session's -// transaction root, so staging inside the open transaction builds the Dolt -// commit from its BEGIN-time snapshot and can write rows a concurrent writer -// committed in the meantime back to their old values. Committed first, this -// transaction's writes go through Dolt's commit-time merge with those -// writers, and the DOLT_ADD on the same pinned session, now outside any -// transaction, stages the merged state. -// -// A serialization failure of the SQL COMMIT is returned for the caller to redo -// the whole unit of work, as Commit's is. Once the SQL COMMIT succeeds the -// write is durable, so the Dolt commit after it never fails CommitScoped (see -// commitStagedTables). -func (t *doltServerTx) CommitScoped(ctx context.Context, tables []string, message string) error { - if t.done { - return errors.New("uow: commit: already done") - } - if message == "" || len(tables) == 0 || issueops.VersionCommitDeferred(ctx) { - return t.Commit(ctx, "") - } - // Resolved before the commit, while the versioned-history scope is bound - // to the pinned session. - staged := append([]string(nil), tables...) - if issueops.VersionedHistoryEnabledFor(t.conn) { - for _, table := range issueops.VersionedHistoryStagedTables() { - if !slices.Contains(staged, table) { - staged = append(staged, table) - } - } - } - if _, err := t.conn.ExecContext(ctx, "COMMIT;"); err != nil { - if isSerializationError(err) { - return err - } - return t.closeOpenTxAfterFailure(ctx, err) - } - t.done = true - clean := t.commitStagedTables(ctx, staged, message) - if t.recheckBlockedAfterCommit(ctx, issueops.TakeBlockedRecheck(t.conn)) && clean { - t.releaseConn() - } else { - t.poisonConn() - } - return nil -} - -// commitStagedTables is CommitScoped's Dolt commit, made on the pinned session -// after the transaction committed. It runs detached from the caller's -// cancellation on its own short budget, so a deadline landing after the SQL -// COMMIT does not skip it, and replays only the conflicts that leave nothing -// behind (serialization failures and Dolt's @@autocommit rollback, which is -// how a conflict surfaces outside a transaction), so re-staging and -// committing again is safe. That is doltAddAndCommitPostTx's retry. -// -// A failure is logged, never returned: the write is durable, and an error -// would make RunTxScoped replay a committed unit of work, which for a -// heartbeat re-arm then reports a refresh. The change rides the next Dolt -// commit on the branch, as a failed post-transaction commit does in the -// DoltStore funnel. Unlike the funnel, this leg counts nothing: uow records -// no metrics, so bd.db.post_tx_commit_dropped, the funnel's counter for the -// same drop, never sees one made here, and the log line is its only signal. -// It reports whether the session may go back to the pool; after a failure its -// state is unknown (a statement cut off mid-flight, or under @@autocommit=0 -// the implicit transaction a DOLT_ADD opened), so it may not. -func (t *doltServerTx) commitStagedTables(ctx context.Context, tables []string, message string) bool { - ctx, cancel := context.WithTimeout(context.WithoutCancel(ctx), scopedCommitMaxElapsed+scopedCommitGrace) - defer cancel() - - bo := backoff.NewExponentialBackOff() - bo.InitialInterval = txRetryInitialInterval - bo.MaxElapsedTime = scopedCommitMaxElapsed - var lastErr error - err := backoff.Retry(func() error { - err := t.stageAndCommitOnce(ctx, tables, message) - if err == nil { - return nil - } - lastErr = err - if isSerializationError(err) || isDoltAutocommitRollbackError(err) { - return err - } - return backoff.Permanent(err) - }, backoff.WithContext(bo, ctx)) - if err != nil && lastErr != nil && !errors.Is(err, lastErr) { - // The deadline expiring between attempts returns the bare context - // error; keep the last real failure, the only diagnostic of a dropped - // commit. - err = fmt.Errorf("%w (last attempt: %w)", err, lastErr) - } - if err != nil { - log.Printf("warning: uow: dolt commit %q failed after its SQL commit (data already committed; change rides the next dolt commit): %v", message, err) - return false - } - return true -} - -// stageAndCommitOnce is one attempt at commitStagedTables: stage each table, -// then commit the staged set alone. -func (t *doltServerTx) stageAndCommitOnce(ctx context.Context, tables []string, message string) error { - for _, table := range tables { - if err := schema.DrainCall(ctx, t.conn, "CALL DOLT_ADD(?)", table); err != nil { - return fmt.Errorf("dolt add %s: %w", table, err) - } - } - // Sessions on one branch share the working set, so a concurrent writer's - // Dolt commit can already have taken this transaction's rows under its - // own message, leaving nothing staged here: skip the empty commit Dolt - // would refuse. The same race between this check and the commit surfaces - // as "nothing to commit", which here means exactly that and not a lost - // write (the SQL COMMIT has landed), so it is no failure either; it is - // logged, as the DoltStore funnel logs it, so the missing audit line is - // explicable. - staged, err := issueops.HasStagedChanges(ctx, t.conn) - if err != nil { - return err - } - if !staged { - return nil - } - err = schema.DrainCall(ctx, t.conn, "CALL DOLT_COMMIT('-m', ?)", message) - if err != nil { - if !issueops.IsNothingToCommitError(err) { - return fmt.Errorf("dolt commit: %w", err) - } - log.Printf("uow: dolt commit %q made no dolt commit (nothing to commit): the staged change was most likely absorbed into a concurrent writer's commit (data already committed)", message) - } - return nil -} - // commitStatement picks the statement that ends the open transaction on the // pinned session for a commit named message. func (t *doltServerTx) commitStatement(ctx context.Context, message string) (string, []interface{}, error) { @@ -252,15 +91,12 @@ func (t *doltServerTx) commitStatement(ctx context.Context, message string) (str } // An empty message selects the EPHEMERAL commit form (bd-aq0ql): a plain // SQL COMMIT persists the transaction's writes into the working set - // without minting a Dolt commit or history. It has these entry points: + // without minting a Dolt commit or history. It has two entry points: // - // - uow.RunTxEphemeral, for work that touches ONLY dolt_ignored state. - // RunTx/RunTxResult treat an empty commitMsg as "nothing to commit" and - // never call Commit at all, so they cannot reach it that way. - // - uow.RunTxScoped, for an attempt that names no tables — a lease - // refresh (bd-lrgn1), which writes only the dolt_ignored leases table - // and must never create a commit. A heartbeat that re-arms names its - // tables instead and takes the scoped commit. + // - uow.RunTxEphemeral, for work that touches ONLY dolt_ignored state — + // today the leases table (bd-lrgn1), whose heartbeats must never create + // commits. RunTx/RunTxResult treat an empty commitMsg as "nothing to + // commit" and never call Commit at all, so they cannot reach it that way. // - RunTx/RunTxResult under a deferred-version-commit context, via the // blanking just above (GH#4995). There the writes are ordinary versioned // rows and the ONLY-dolt_ignored-state rationale does not apply: the @@ -357,7 +193,6 @@ func (t *doltServerTx) rollbackConn(ctx context.Context) error { func (t *doltServerTx) releaseConn() { t.releaseJournalScope() - t.releaseVersionScope() t.releaseRecheckScope() if t.conn != nil { _ = t.conn.Close() @@ -375,15 +210,6 @@ func (t *doltServerTx) releaseJournalScope() { } } -// releaseVersionScope is releaseJournalScope's counterpart for dual-write -// issue-version history. Idempotent for the same reason. -func (t *doltServerTx) releaseVersionScope() { - if t.clearVersionScope != nil { - t.clearVersionScope() - t.clearVersionScope = nil - } -} - // releaseRecheckScope is releaseJournalScope's counterpart for the // blocked-recheck scope. Idempotent for the same reason. func (t *doltServerTx) releaseRecheckScope() { @@ -401,7 +227,6 @@ func (t *doltServerTx) releaseRecheckScope() { // database/sql close the connection and drop it from the pool. func (t *doltServerTx) poisonConn() { t.releaseJournalScope() - t.releaseVersionScope() t.releaseRecheckScope() if t.conn == nil { return diff --git a/internal/storage/uow/doltserver_tx_test.go b/internal/storage/uow/doltserver_tx_test.go index b665ea1dc..036291ba8 100644 --- a/internal/storage/uow/doltserver_tx_test.go +++ b/internal/storage/uow/doltserver_tx_test.go @@ -20,7 +20,6 @@ import ( "testing" "github.com/DATA-DOG/go-sqlmock" - mysql "github.com/go-sql-driver/mysql" "github.com/steveyegge/beads/internal/storage/issueops" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -286,134 +285,3 @@ func TestDoltServerTxRunTxWithDeferredContextSkipsDoltCommit(t *testing.T) { require.NoError(t, mock.ExpectationsWereMet()) assert.Equal(t, 1, p.db.Stats().OpenConnections) } - -// matchHasStaged matches the staged-set check issueops.HasStagedChanges runs -// before CommitScoped's DOLT_COMMIT('-m'). -const matchHasStaged = `SELECT COUNT\(\*\) FROM dolt_status WHERE staged = 1` - -// expectScopedDoltCommit arms one attempt of CommitScoped's Dolt commit: a -// DOLT_ADD per table, the staged-set check reporting staged rows, and, when -// something is staged, a DOLT_COMMIT('-m') answering commitErr. -func expectScopedDoltCommit(mock sqlmock.Sqlmock, tables []string, staged int, message string, commitErr error) { - for _, table := range tables { - mock.ExpectQuery(`CALL DOLT_ADD\(\?\)`).WithArgs(table). - WillReturnRows(sqlmock.NewRows([]string{"status"}).AddRow(0)) - } - mock.ExpectQuery(matchHasStaged).WillReturnRows(sqlmock.NewRows([]string{"count"}).AddRow(staged)) - if staged == 0 { - return - } - commit := mock.ExpectQuery(`CALL DOLT_COMMIT\('-m', \?\)`).WithArgs(message) - if commitErr != nil { - commit.WillReturnError(commitErr) - } else { - commit.WillReturnRows(sqlmock.NewRows([]string{"hash"}).AddRow("c0ffee")) - } -} - -// TestDoltServerTxCommitScopedCommitsSQLBeforeDoltCommit pins the -// LatentLabsSpace/NEXUS#92 ordering on the uow leg: CommitScoped ends the SQL -// transaction with a plain COMMIT and only then stages and Dolt-commits its -// tables, on the same pinned session. Staged inside the open transaction, -// DOLT_ADD builds the Dolt commit from the transaction's start snapshot and -// writes a concurrent writer's committed rows back to their old values. -func TestDoltServerTxCommitScopedCommitsSQLBeforeDoltCommit(t *testing.T) { - p, mock := newMockTxProvider(t) - mock.ExpectExec("START TRANSACTION").WillReturnResult(sqlmock.NewResult(0, 0)) - mock.ExpectExec(matchPlainCommit).WillReturnResult(sqlmock.NewResult(0, 0)) - expectScopedDoltCommit(mock, []string{"events", "issues"}, 2, "bd: re-arm lease x", nil) - - tx, err := p.BeginTx(context.Background()) - require.NoError(t, err) - - require.NoError(t, tx.(ScopedCommitter).CommitScoped(context.Background(), []string{"events", "issues"}, "bd: re-arm lease x")) - require.NoError(t, mock.ExpectationsWereMet(), "the SQL COMMIT must come first, then DOLT_ADD per table and DOLT_COMMIT('-m')") - assert.Equal(t, 1, p.db.Stats().OpenConnections, "a cleanly committed session may return to the pool") - require.ErrorContains(t, tx.Commit(context.Background(), ""), "already done") -} - -// TestDoltServerTxCommitScopedSwallowsAFailedDoltCommit: once the SQL COMMIT -// has landed the write is durable, so a Dolt commit that fails after it must -// not fail CommitScoped (RunTxScoped would replay a committed unit of work). -// The session, whose state the failed statement leaves unknown, is discarded. -func TestDoltServerTxCommitScopedSwallowsAFailedDoltCommit(t *testing.T) { - p, mock := newMockTxProvider(t) - mock.ExpectExec("START TRANSACTION").WillReturnResult(sqlmock.NewResult(0, 0)) - mock.ExpectExec(matchPlainCommit).WillReturnResult(sqlmock.NewResult(0, 0)) - mock.ExpectQuery(`CALL DOLT_ADD\(\?\)`).WithArgs("issues").WillReturnError(errors.New("dolt add exploded")) - - tx, err := p.BeginTx(context.Background()) - require.NoError(t, err) - - require.NoError(t, tx.(ScopedCommitter).CommitScoped(context.Background(), []string{"issues"}, "bd: re-arm lease x"), - "a committed write must not be reported as failed") - require.NoError(t, mock.ExpectationsWereMet(), "a failure other than a conflict is not retried") - assert.Equal(t, 0, p.db.Stats().OpenConnections, "the session must be discarded, not pooled") - require.ErrorContains(t, tx.Commit(context.Background(), ""), "already done") -} - -// TestDoltServerTxCommitScopedRetriesAConflictedDoltCommit: outside a -// transaction, a Dolt commit that conflicts with a concurrent writer is rolled -// back as an @@autocommit merge conflict (1105), not 1213, and is retried like -// one. -func TestDoltServerTxCommitScopedRetriesAConflictedDoltCommit(t *testing.T) { - p, mock := newMockTxProvider(t) - mock.ExpectExec("START TRANSACTION").WillReturnResult(sqlmock.NewResult(0, 0)) - mock.ExpectExec(matchPlainCommit).WillReturnResult(sqlmock.NewResult(0, 0)) - conflict := &mysql.MySQLError{Number: 1105, Message: "Merge conflict detected, @autocommit transaction rolled back"} - expectScopedDoltCommit(mock, []string{"issues"}, 1, "bd: re-arm lease x", conflict) - expectScopedDoltCommit(mock, []string{"issues"}, 1, "bd: re-arm lease x", nil) - - tx, err := p.BeginTx(context.Background()) - require.NoError(t, err) - - require.NoError(t, tx.(ScopedCommitter).CommitScoped(context.Background(), []string{"issues"}, "bd: re-arm lease x")) - require.NoError(t, mock.ExpectationsWereMet(), "the conflicted attempt must be staged and committed again") - assert.Equal(t, 1, p.db.Stats().OpenConnections, "a session whose retry committed may return to the pool") -} - -// TestDoltServerTxCommitScopedReturnsASerializationFailure: a serialization -// failure of the SQL COMMIT is returned for RunTxScoped to redo the whole unit -// of work, and no Dolt commit follows a COMMIT that did not land. -func TestDoltServerTxCommitScopedReturnsASerializationFailure(t *testing.T) { - p, mock := newMockTxProvider(t) - mock.ExpectExec("START TRANSACTION").WillReturnResult(sqlmock.NewResult(0, 0)) - mock.ExpectExec(matchPlainCommit).WillReturnError(&mysql.MySQLError{Number: 1213, Message: "serialization failure"}) - - tx, err := p.BeginTx(context.Background()) - require.NoError(t, err) - - err = tx.(ScopedCommitter).CommitScoped(context.Background(), []string{"issues"}, "bd: re-arm lease x") - require.True(t, isSerializationError(err), "err = %v, want the serialization failure", err) - require.NoError(t, mock.ExpectationsWereMet(), "nothing may be staged or committed after a failed COMMIT") -} - -// TestDoltServerTxCommitScopedMakesNoEmptyDoltCommit: a concurrent writer's -// Dolt commit can take this transaction's rows before its own Dolt commit -// runs, seen either before the commit (nothing staged) or by it ("nothing to -// commit"). Neither is a failure: unlike Commit's in-transaction commit, the -// rows are already committed. -func TestDoltServerTxCommitScopedMakesNoEmptyDoltCommit(t *testing.T) { - for _, tc := range []struct { - name string - staged int - commitErr error - }{ - {name: "nothing staged", staged: 0}, - {name: "nothing to commit", staged: 1, commitErr: errors.New("nothing to commit")}, - } { - t.Run(tc.name, func(t *testing.T) { - p, mock := newMockTxProvider(t) - mock.ExpectExec("START TRANSACTION").WillReturnResult(sqlmock.NewResult(0, 0)) - mock.ExpectExec(matchPlainCommit).WillReturnResult(sqlmock.NewResult(0, 0)) - expectScopedDoltCommit(mock, []string{"issues"}, tc.staged, "bd: re-arm lease x", tc.commitErr) - - tx, err := p.BeginTx(context.Background()) - require.NoError(t, err) - - require.NoError(t, tx.(ScopedCommitter).CommitScoped(context.Background(), []string{"issues"}, "bd: re-arm lease x")) - require.NoError(t, mock.ExpectationsWereMet()) - assert.Equal(t, 1, p.db.Stats().OpenConnections, "the session is clean and may return to the pool") - }) - } -} diff --git a/internal/storage/uow/dualwrite_history_contract_test.go b/internal/storage/uow/dualwrite_history_contract_test.go deleted file mode 100644 index d4ab1b046..000000000 --- a/internal/storage/uow/dualwrite_history_contract_test.go +++ /dev/null @@ -1,229 +0,0 @@ -package uow - -import ( - "context" - "testing" - - "github.com/steveyegge/beads/backend/conformance" - "github.com/steveyegge/beads/internal/storage" - "github.com/steveyegge/beads/internal/storage/domain" - "github.com/steveyegge/beads/internal/types" -) - -// TestDualWriteContract runs the dual-write history contract against the -// unit-of-work provider. All three legs share RecordVersionInTx's one body, -// so this is an engine check rather than a second vote — and here it checks -// the composition, the part that genuinely differs on this leg: dual-write -// history has to land inside the SAME unit of work as the mutation it -// accompanies, and this is the only leg where that mutation's own commit is -// assembled by composition rather than opened directly against a *sql.DB. -func TestDualWriteContract(t *testing.T) { - ctx := context.Background() - fixture := newUOWDualWriteFixture(t, ctx, "dwc", true) - - t.Run("MintsOneVersionRowPerAcceptedMutation", func(t *testing.T) { - conformance.RunDualWriteMintsOneVersionRowPerAcceptedMutation(t, ctx, fixture) - }) - t.Run("NoOpMutationMintsNoRow", func(t *testing.T) { - conformance.RunDualWriteNoOpMutationMintsNoRow(t, ctx, fixture) - }) - t.Run("AttributionIsRecordedWithTheMutation", func(t *testing.T) { - conformance.RunDualWriteAttributionIsRecordedWithTheMutation(t, ctx, fixture) - }) - t.Run("CurrentRevisionMatchesTheNewVersionRow", func(t *testing.T) { - conformance.RunDualWriteCurrentRevisionMatchesTheNewVersionRow(t, ctx, fixture) - }) - t.Run("NoOpMutationLeavesThePriorVersionRowUnperturbed", func(t *testing.T) { - conformance.RunDualWriteNoOpMutationLeavesThePriorVersionRowUnperturbed(t, ctx, fixture) - }) -} - -// TestDualWriteContractFlagOff runs FR-7 against a provider constructed with -// the flag OFF from the start — its own provider, not a shared one, so "flag -// off" means what FR-7 promises rather than "not yet turned on this session". -func TestDualWriteContractFlagOff(t *testing.T) { - ctx := context.Background() - fixture := newUOWDualWriteFixture(t, ctx, "dwcoff", false) - - t.Run("FlagOffProducesNoVersionRows", func(t *testing.T) { - conformance.RunDualWriteFlagOffProducesNoVersionRows(t, ctx, fixture) - }) -} - -// TestDualWriteFixtureKitIsWired is this leg's half of the explicit -// per-leg guardrail design §8.5 calls for in place of AST auto-discovery — -// see the dolt leg's TestDualWriteFixtureKitIsWired for why the guardrail is -// needed even though TestEveryLegWiresEveryRoleContract's scan already -// enumerates and confirms wiring for all six RunDualWriteXxx entrypoints. -func TestDualWriteFixtureKitIsWired(t *testing.T) { - ctx := context.Background() - fixture := newUOWDualWriteFixture(t, ctx, "dwk", true) - - if fixture.Mutate == nil { - t.Error("DualWriteFixture.Mutate is nil") - } - if fixture.MutateAsNoOp == nil { - t.Error("DualWriteFixture.MutateAsNoOp is nil") - } - if fixture.CurrentRevision == nil { - t.Error("DualWriteFixture.CurrentRevision is nil") - } - if fixture.VersionRowCount == nil { - t.Error("DualWriteFixture.VersionRowCount is nil") - } - if fixture.LatestVersionAttribution == nil { - t.Error("DualWriteFixture.LatestVersionAttribution is nil") - } -} - -// TestUOWCreateWithLabelAndEdgeMintsPerRepositoryWrite_KnownDivergence makes -// the uow leg's 1+N+M divergence machine-visible (reviewer MINOR-3 on -// gastownhall/beads#6358). TestDualWriteContract's Mutate creates an issue -// with no labels and no dependencies, so its "exactly one version row per -// accepted mutation" case cannot see what a fuller create does on this leg: -// domain.IssueUseCase.CreateIssue inserts the row and then persists each -// label and each edge through its own repository (domain/db label.go and -// dependency.go), and every one of those repository writes reaches -// issueops.RecordVersionInTx on its own — 1 + N + M rows for ONE -// caller-visible create. The direct legs (dolt, embeddeddolt) mint exactly -// one row for the same shape: issueops.CreateIssuesInTxWithContext runs its -// constituents without minting and mints once per issue after the -// creation-time edges land. -// -// This test pins the CURRENT, measured behavior — three rows for one label -// and one edge — so the divergence is a red test away from being fixed -// rather than a comment. KNOWN DIVERGENCE gastownhall/beads#6379: -// it is EXPECTED TO FLIP when that follow-up lands and the uow create mints -// once; update the want to 1 then and fold the shape into the contract. -func TestUOWCreateWithLabelAndEdgeMintsPerRepositoryWrite_KnownDivergence(t *testing.T) { - ctx := context.Background() - const prefix = "dwle" - provider := newUOWVersionedHistoryProvider(t, ctx, prefix, true) - kit := newUOWRoleFixtureKit(provider, prefix) - - // The edge needs a target that exists; seeded through the kit, with no - // labels and no edges, it doubles as the control for the bare shape. - target := prefix + "-target" - if err := kit.CreateIssue(ctx, &types.Issue{ - ID: target, Title: "t-" + target, IssueType: types.TypeTask, Status: types.StatusOpen, - }, "actor"); err != nil { - t.Fatalf("seeding %s: %v", target, err) - } - - id := prefix + "-label-and-edge" - if err := RunTx(ctx, provider, func(ctx context.Context, uw UnitOfWork) (string, error) { - _, err := uw.IssueUseCase().CreateIssue(ctx, domain.CreateIssueParams{ - Issue: &types.Issue{ - ID: id, Title: "t-" + id, IssueType: types.TypeTask, Status: types.StatusOpen, - }, - ExplicitID: id, - Labels: []string{"one"}, - Dependencies: []domain.DependencySpec{{Type: types.DepBlocks, TargetID: target}}, - CreateOnly: true, - }, "actor") - return "create " + id, err - }); err != nil { - t.Fatalf("creating %s with one label and one edge: %v", id, err) - } - - count := func(what, query string, args ...any) int { - t.Helper() - var n int - if err := kit.QueryScalar(ctx, query, args, &n); err != nil { - t.Fatalf("counting %s: %v", what, err) - } - return n - } - // The rows counted below belong to a create that really carried both - // writes, not to one that dropped a label or an edge on the way in. - if labels := count("labels", "SELECT COUNT(*) FROM labels WHERE issue_id = ?", id); labels != 1 { - t.Fatalf("labels on %s = %d, want 1", id, labels) - } - if edges := count("edges", "SELECT COUNT(*) FROM dependencies WHERE issue_id = ? AND "+ - "COALESCE(depends_on_issue_id, depends_on_wisp_id, depends_on_external) = ?", id, target); edges != 1 { - t.Fatalf("edges %s -> %s = %d, want 1", id, target, edges) - } - - // 1 issue row + 1 label + 1 edge, each minted by its own repository write. - // The direct legs mint exactly 1 for this shape. - const mintedToday = 3 - got := count("version rows for "+id, "SELECT COUNT(*) FROM issue_versions WHERE issue_id = ?", id) - if got != mintedToday { - t.Errorf("version rows for %s after one create with 1 label + 1 edge = %d, want the %d this leg mints today "+ - "(1 issue + 1 label + 1 edge); 1 would mean gastownhall/beads#6379 landed — flip this test into the contract", - id, got, mintedToday) - } - // The bare shape on the same leg mints one row, so the two extra rows - // above are the label and the edge, not something else about this leg. - if bare := count("version rows for "+target, "SELECT COUNT(*) FROM issue_versions WHERE issue_id = ?", target); bare != 1 { - t.Errorf("version rows for %s (no labels, no edges) = %d, want 1", target, bare) - } - t.Logf("KNOWN DIVERGENCE gastownhall/beads#6379: uow create with 1 label + 1 edge minted %d version rows "+ - "(1 issue + 1 label + 1 edge, one per repository write); the direct legs mint exactly 1 for the same shape. "+ - "This test is expected to FLIP when the follow-up lands.", got) -} - -// newUOWVersionedHistoryProvider boots one provider for a versioned-history -// suite and sets the flag as asked — through the capability accessor's -// operator half, the same way newUOWJournalFixture reaches -// storage.EventsJournalConfigurer: a provider that stopped offering the role -// is the regression this assertion exists to catch. -func newUOWVersionedHistoryProvider(t *testing.T, ctx context.Context, prefix string, enabled bool) UnitOfWorkProvider { - t.Helper() - provider := newUOWRoleFixtureProvider(t, ctx, prefix) - configurer, ok := provider.(storage.VersionedHistoryConfigurer) - if !ok { - t.Fatalf("provider %T does not implement storage.VersionedHistoryConfigurer", provider) - } - configurer.SetVersionedHistoryEnabled(enabled) - return provider -} - -func newUOWDualWriteFixture(t *testing.T, ctx context.Context, prefix string, enabled bool) conformance.DualWriteFixture { - t.Helper() - provider := newUOWVersionedHistoryProvider(t, ctx, prefix, enabled) - kit := newUOWRoleFixtureKit(provider, prefix) - return conformance.DualWriteFixture{ - IssuePrefix: prefix, - Mutate: func(ctx context.Context, id string) error { - return RunTx(ctx, provider, func(ctx context.Context, uw UnitOfWork) (string, error) { - _, err := uw.IssueUseCase().CreateIssue(ctx, domain.CreateIssueParams{ - Issue: &types.Issue{ - ID: id, Title: "t-" + id, IssueType: types.TypeTask, Status: types.StatusOpen, - }, - ExplicitID: id, - CreateOnly: true, - }, "actor") - return "create " + id, err - }) - }, - MutateAsNoOp: func(ctx context.Context, id string) error { - // Re-sets title to the exact value Mutate already gave it, so - // issueops.DiscardNoopIssueUpdates discards it before it ever - // reaches RecordVersionInTx. - return RunTx(ctx, provider, func(ctx context.Context, uw UnitOfWork) (string, error) { - return "update " + id, uw.IssueUseCase().UpdateIssue(ctx, id, - map[string]any{"title": "t-" + id}, "actor") - }) - }, - CurrentRevision: func(ctx context.Context, id string) (int64, error) { - var revision int64 - err := kit.QueryScalar(ctx, "SELECT current_revision FROM issues WHERE id = ?", []any{id}, &revision) - return revision, err - }, - VersionRowCount: func(ctx context.Context, id string) (int, error) { - var count int - err := kit.QueryScalar(ctx, "SELECT COUNT(*) FROM issue_versions WHERE issue_id = ?", []any{id}, &count) - return count, err - }, - LatestVersionAttribution: func(ctx context.Context, id string) (actor, agent, message string, err error) { - err = kit.QueryScalar(ctx, ` - SELECT COALESCE(change_actor, ''), COALESCE(change_agent, ''), COALESCE(change_message, '') - FROM issue_versions - WHERE issue_id = ? - ORDER BY revision DESC - LIMIT 1`, []any{id}, &actor, &agent, &message) - return actor, agent, message, err - }, - } -} diff --git a/internal/storage/uow/dualwrite_participation_generation_contract_test.go b/internal/storage/uow/dualwrite_participation_generation_contract_test.go deleted file mode 100644 index 42cf07f8e..000000000 --- a/internal/storage/uow/dualwrite_participation_generation_contract_test.go +++ /dev/null @@ -1,388 +0,0 @@ -package uow - -import ( - "context" - "testing" - - "github.com/steveyegge/beads/backend/conformance" - "github.com/steveyegge/beads/internal/storage" - "github.com/steveyegge/beads/internal/storage/domain" - "github.com/steveyegge/beads/internal/types" -) - -// TestParticipationGenerationFence runs the design §16.2(b) write-fence -// contract (backend/conformance/dualwrite_participation_generation_contract.go) -// against the unit-of-work provider. Like TestDualWriteContract, this is an -// engine check rather than a second vote: all three legs share -// RecordVersionInTx's one body, and this is the only leg where the mutation's -// own commit is assembled by composition rather than opened directly against -// a *sql.DB. -func TestParticipationGenerationFence(t *testing.T) { - ctx := context.Background() - fixture := newUOWParticipationGenerationFixture(t, ctx, "pg") - - t.Run("CreateWithFlagOnStampsAValue", func(t *testing.T) { - conformance.RunParticipationGenerationCreateWithFlagOnStampsAValue(t, ctx, fixture) - }) - t.Run("CreateWithFlagOffLeavesLegacyNull", func(t *testing.T) { - conformance.RunParticipationGenerationCreateWithFlagOffLeavesLegacyNull(t, ctx, fixture) - }) - t.Run("SkipsUpdateOnLegacyRecord", func(t *testing.T) { - conformance.RunParticipationGenerationSkipsUpdateOnLegacyRecord(t, ctx, fixture) - }) - t.Run("ProceedsForPromotedRecord", func(t *testing.T) { - conformance.RunParticipationGenerationProceedsForPromotedRecord(t, ctx, fixture) - }) - t.Run("FlagOffSkipsUpdateOnParticipatingRecord", func(t *testing.T) { - conformance.RunParticipationGenerationFlagOffSkipsUpdateOnParticipatingRecord(t, ctx, fixture) - }) - t.Run("FenceIgnoresSchemaSkewHatch", func(t *testing.T) { - conformance.RunParticipationGenerationFenceIgnoresSchemaSkewHatch(t, ctx, fixture) - }) - t.Run("WispPromotionBeginsHistory", func(t *testing.T) { - conformance.RunParticipationGenerationWispPromotionBeginsHistory(t, ctx, fixture) - }) - t.Run("WispPersistenceMoveBeginsHistory", func(t *testing.T) { - conformance.RunParticipationGenerationWispPersistenceMoveBeginsHistory(t, ctx, fixture) - }) - t.Run("WispFlagClearMintsNothing", func(t *testing.T) { - conformance.RunParticipationGenerationWispFlagClearMintsNothing(t, ctx, fixture) - }) - t.Run("WispRootSquashMintsNothing", func(t *testing.T) { - conformance.RunParticipationGenerationWispRootSquashMintsNothing(t, ctx, fixture) - }) - t.Run("UnflaggedWispCreateMintsNothing", func(t *testing.T) { - conformance.RunParticipationGenerationUnflaggedWispCreateMintsNothing(t, ctx, fixture) - }) - t.Run("NoHistoryIssueCreateDeclaresParticipation", func(t *testing.T) { - conformance.RunParticipationGenerationNoHistoryIssueCreateDeclaresParticipation(t, ctx, fixture) - }) - t.Run("LegacyNoHistoryIssueStaysLegacy", func(t *testing.T) { - conformance.RunParticipationGenerationLegacyNoHistoryIssueStaysLegacy(t, ctx, fixture) - }) -} - -// TestParticipationGenerationFixtureKitIsWired is this leg's half of the -// explicit per-leg guardrail design §8.5 calls for in place of AST -// auto-discovery — see the dolt leg's TestParticipationGenerationFixtureKitIsWired -// for why the guardrail is needed even though TestEveryLegWiresEveryRoleContract's -// scan already enumerates and confirms wiring for every -// RunParticipationGenerationXxx entrypoint. -func TestParticipationGenerationFixtureKitIsWired(t *testing.T) { - ctx := context.Background() - fixture := newUOWParticipationGenerationFixture(t, ctx, "pgk") - - if fixture.SetFlag == nil { - t.Error("ParticipationGenerationFixture.SetFlag is nil") - } - if fixture.Mutate == nil { - t.Error("ParticipationGenerationFixture.Mutate is nil") - } - if fixture.MutateExisting == nil { - t.Error("ParticipationGenerationFixture.MutateExisting is nil") - } - if fixture.CreateWisp == nil { - t.Error("ParticipationGenerationFixture.CreateWisp is nil") - } - if fixture.PromoteWisp == nil { - t.Error("ParticipationGenerationFixture.PromoteWisp is nil") - } - if fixture.MovePersistent == nil { - t.Error("ParticipationGenerationFixture.MovePersistent is nil") - } - if fixture.ClearWispFlag == nil { - t.Error("ParticipationGenerationFixture.ClearWispFlag is nil") - } - if fixture.SquashWispRoot == nil { - t.Error("ParticipationGenerationFixture.SquashWispRoot is nil") - } - if fixture.CreateUnflaggedWisp == nil { - t.Error("ParticipationGenerationFixture.CreateUnflaggedWisp is nil") - } - if fixture.CreateNoHistoryIssue == nil { - t.Error("ParticipationGenerationFixture.CreateNoHistoryIssue is nil") - } - if fixture.CurrentRevision == nil { - t.Error("ParticipationGenerationFixture.CurrentRevision is nil") - } - if fixture.VersionRowCount == nil { - t.Error("ParticipationGenerationFixture.VersionRowCount is nil") - } - if fixture.ParticipationGeneration == nil { - t.Error("ParticipationGenerationFixture.ParticipationGeneration is nil") - } - if fixture.InWispsTable == nil { - t.Error("ParticipationGenerationFixture.InWispsTable is nil") - } -} - -// TestParticipationGenerationUpsertCreate pins the write fence (design -// §16.2b) on the unit-of-work create that is not CreateOnly, the default for -// bd batch, bd todo, tracker sync and mol port. That create's insert is an -// upsert, so db.Insert has to tell a new row (create-shaped mint: stamp the -// column) from an existing one the upsert resolved to (update-shaped: fenced -// when legacy). TestParticipationGenerationFence cannot see that choice: its -// Mutate is CreateOnly, and so is every served create (createParams), so no -// other leg reaches this branch at all. -// -// PromoteOverDualPresenceKeepsLegacy asks the same question of -// PromoteFromEphemeralInTx's upsert: a wisp whose id the issues plane already -// holds (GH#4455's dual presence, written the way a store corrupted before -// that fix holds it) is promoted onto that existing row. -// WispOverLegacyIssuesRowKeepsItLegacy asks it of the wisps-table half of the -// same db.Insert (bd mol port's CreateWisp): that insert never creates an -// issues row, so the only issues row its mint can reach is a twin that -// already held the id. -func TestParticipationGenerationUpsertCreate(t *testing.T) { - ctx := context.Background() - provider := newUOWRoleFixtureProvider(t, ctx, "pgu") - fixture := uowParticipationGenerationFixtureFor(t, provider, "pgu") - - setFlag := func(t *testing.T, enabled bool) { - t.Helper() - if err := fixture.SetFlag(ctx, enabled); err != nil { - t.Fatalf("setting versioned history to %v: %v", enabled, err) - } - } - upsert := func(t *testing.T, id, title string) { - t.Helper() - if err := RunTx(ctx, provider, func(ctx context.Context, uw UnitOfWork) (string, error) { - _, err := uw.IssueUseCase().CreateIssue(ctx, domain.CreateIssueParams{ - Issue: &types.Issue{ - ID: id, Title: title, IssueType: types.TypeTask, Status: types.StatusOpen, - }, - ExplicitID: id, - }, "actor") - return "upsert " + id, err - }); err != nil { - t.Fatalf("upsert-creating %s: %v", id, err) - } - } - requireState := func(t *testing.T, id string, wantStamped bool, wantVersions int, why string) { - t.Helper() - gen, err := fixture.ParticipationGeneration(ctx, id) - if err != nil { - t.Fatalf("reading participation_generation for %s: %v", id, err) - } - if stamped := gen != nil; stamped != wantStamped { - t.Errorf("participation_generation for %s stamped=%v, want %v: %s", id, stamped, wantStamped, why) - } - versions, err := fixture.VersionRowCount(ctx, id) - if err != nil { - t.Fatalf("counting version rows for %s: %v", id, err) - } - if versions != wantVersions { - t.Errorf("version row count for %s = %d, want %d: %s", id, versions, wantVersions, why) - } - } - - t.Run("NewRowIsCreateShaped", func(t *testing.T) { - setFlag(t, true) - id := "pgu-new" - upsert(t, id, "t-"+id) - requireState(t, id, true, 1, "an upsert that inserted the row is a create, so it stamps the column and begins the history") - }) - t.Run("LegacyRowStaysLegacy", func(t *testing.T) { - setFlag(t, false) - id := "pgu-legacy" - upsert(t, id, "t-"+id) - requireState(t, id, false, 0, "a create with history off leaves the row legacy") - setFlag(t, true) - upsert(t, id, "resynced-"+id) - requireState(t, id, false, 0, "an upsert that resolved to a legacy row is update-shaped and fenced; stamping it would promote a legacy record (R2.3)") - }) - t.Run("StampedRowMintsOnce", func(t *testing.T) { - setFlag(t, true) - id := "pgu-stamped" - upsert(t, id, "t-"+id) - upsert(t, id, "resynced-"+id) - requireState(t, id, true, 2, "an upsert that resolved to a participating row mints exactly one more version") - }) - t.Run("PromoteOverDualPresenceKeepsLegacy", func(t *testing.T) { - setFlag(t, false) - id := "pgu-dual" - if err := fixture.Mutate(ctx, id); err != nil { - t.Fatalf("creating legacy %s: %v", id, err) - } - if err := RunTx(ctx, provider, func(ctx context.Context, uw UnitOfWork) (string, error) { - _, err := uw.RawSQLUseCase().Exec(ctx, - "INSERT INTO wisps (id, title, status, priority, issue_type, ephemeral, created_at, updated_at) "+ - "SELECT id, title, status, priority, issue_type, 1, created_at, updated_at FROM issues WHERE id = ?", id) - return "plant wisp twin " + id, err - }); err != nil { - t.Fatalf("planting a wisps-table twin of %s: %v", id, err) - } - setFlag(t, true) - if err := fixture.PromoteWisp(ctx, id); err != nil { - t.Fatalf("promoting %s: %v", id, err) - } - if inWisps, err := fixture.InWispsTable(ctx, id); err != nil { - t.Fatalf("checking the wisps table for %s: %v", id, err) - } else if inWisps { - t.Fatalf("%s is still in the wisps table after its promote", id) - } - requireState(t, id, false, 0, "a promote whose upsert resolved to an existing legacy issues row is update-shaped and fenced; stamping it would promote a legacy record (R2.3)") - }) - t.Run("WispOverLegacyIssuesRowKeepsItLegacy", func(t *testing.T) { - setFlag(t, false) - id := "pgu-wisp-twin" - upsert(t, id, "t-"+id) - requireState(t, id, false, 0, "a create with history off leaves the row legacy") - setFlag(t, true) - if err := RunTx(ctx, provider, func(ctx context.Context, uw UnitOfWork) (string, error) { - _, err := uw.IssueUseCase().CreateWisp(ctx, domain.CreateIssueParams{ - Issue: &types.Issue{ - ID: id, Title: "wisp-" + id, IssueType: types.TypeTask, Status: types.StatusOpen, Ephemeral: true, - }, - ExplicitID: id, - }, "actor") - return "wisp upsert " + id, err - }); err != nil { - t.Fatalf("wisp-creating %s over its issues row: %v", id, err) - } - if inWisps, err := fixture.InWispsTable(ctx, id); err != nil { - t.Fatalf("checking the wisps table for %s: %v", id, err) - } else if !inWisps { - t.Fatalf("%s is not in the wisps table after its wisp create; the case needs both planes to hold the id", id) - } - requireState(t, id, false, 0, "a wisps-table insert never creates the issues row the seam snapshots, so its mint is update-shaped and fenced; stamping that legacy twin would promote a legacy record (R2.3)") - }) -} - -func newUOWParticipationGenerationFixture(t *testing.T, ctx context.Context, prefix string) conformance.ParticipationGenerationFixture { - t.Helper() - return uowParticipationGenerationFixtureFor(t, newUOWRoleFixtureProvider(t, ctx, prefix), prefix) -} - -// uowParticipationGenerationFixtureFor is newUOWParticipationGenerationFixture -// over a provider the caller already holds, for a test that also drives that -// provider directly. -func uowParticipationGenerationFixtureFor(t *testing.T, provider UnitOfWorkProvider, prefix string) conformance.ParticipationGenerationFixture { - t.Helper() - // Through the capability accessor's operator half, the same way - // newUOWVersionedHistoryProvider reaches it above — SetFlag here needs to - // toggle more than once per case, so this fixture keeps its own handle - // rather than fixing the flag once at construction the way - // newUOWVersionedHistoryProvider does for the dual-write fixture. - configurer, ok := provider.(storage.VersionedHistoryConfigurer) - if !ok { - t.Fatalf("provider %T does not implement storage.VersionedHistoryConfigurer", provider) - } - kit := newUOWRoleFixtureKit(provider, prefix) - return conformance.ParticipationGenerationFixture{ - IssuePrefix: prefix, - SetFlag: func(ctx context.Context, enabled bool) error { - configurer.SetVersionedHistoryEnabled(enabled) - return nil - }, - Mutate: func(ctx context.Context, id string) error { - return RunTx(ctx, provider, func(ctx context.Context, uw UnitOfWork) (string, error) { - _, err := uw.IssueUseCase().CreateIssue(ctx, domain.CreateIssueParams{ - Issue: &types.Issue{ - ID: id, Title: "t-" + id, IssueType: types.TypeTask, Status: types.StatusOpen, - }, - ExplicitID: id, - CreateOnly: true, - }, "actor") - return "create " + id, err - }) - }, - MutateExisting: func(ctx context.Context, id string) error { - // A different value than Mutate gave it, not a re-assertion of the - // same title: DiscardNoopIssueUpdates would discard the latter - // before it ever reached the fence this contract is testing. - return RunTx(ctx, provider, func(ctx context.Context, uw UnitOfWork) (string, error) { - return "update " + id, uw.IssueUseCase().UpdateIssue(ctx, id, - map[string]any{"title": "updated-" + id}, "actor") - }) - }, - CreateWisp: func(ctx context.Context, id string) error { - return kit.CreateWisp(ctx, &types.Issue{ - ID: id, Title: "t-" + id, IssueType: types.TypeTask, Status: types.StatusOpen, Ephemeral: true, - }, "actor") - }, - PromoteWisp: func(ctx context.Context, id string) error { - return RunTx(ctx, provider, func(ctx context.Context, uw UnitOfWork) (string, error) { - return "promote " + id, uw.IssueUseCase().PromoteWisp(ctx, id, "actor") - }) - }, - MovePersistent: func(ctx context.Context, id string) error { - // ApplyUpdate's persistence step, which runs MovePersistence and - // so MoveIssuePersistenceInTx's own mint, not a guarded update's. - persistent := types.PersistenceModePersistent - return RunTx(ctx, provider, func(ctx context.Context, uw UnitOfWork) (string, error) { - _, err := uw.IssueUseCase().ApplyUpdate(ctx, id, domain.UpdateSpec{Persistence: &persistent}, "actor") - return "move persistent " + id, err - }) - }, - ClearWispFlag: func(ctx context.Context, id string) error { - // UpdateWisp, as the molecule writer routes bd mol squash's flag - // clear for a row it found on the wisp plane. - return RunTx(ctx, provider, func(ctx context.Context, uw UnitOfWork) (string, error) { - return "clear wisp flag " + id, uw.IssueUseCase().UpdateWisp(ctx, id, - map[string]any{"wisp": false}, "actor") - }) - }, - SquashWispRoot: func(ctx context.Context, id string) error { - // The proxied bd mol squash's shape: CloseWisp then UpdateWisp in - // the one unit of work the squash commits. - return RunTx(ctx, provider, func(ctx context.Context, uw UnitOfWork) (string, error) { - if _, err := uw.IssueUseCase().CloseWisp(ctx, id, domain.CloseIssueParams{Reason: "squashed"}, "actor"); err != nil { - return "", err - } - return "squash " + id, uw.IssueUseCase().UpdateWisp(ctx, id, - map[string]any{"wisp": false}, "actor") - }) - }, - CreateUnflaggedWisp: func(ctx context.Context, id string) error { - // CreateWisp routes by the call, so neither flag is needed to land - // the row in the wisps table. - return kit.CreateWisp(ctx, &types.Issue{ - ID: id, Title: "t-" + id, IssueType: types.TypeTask, Status: types.StatusOpen, - }, "actor") - }, - CreateNoHistoryIssue: func(ctx context.Context, id string) error { - // CreateIssue routes by the call, so the NoHistory flag rides on an - // issues-table row without moving it to the wisps table. - return kit.CreateIssue(ctx, &types.Issue{ - ID: id, Title: "t-" + id, IssueType: types.TypeTask, Status: types.StatusOpen, NoHistory: true, - }, "actor") - }, - CurrentRevision: func(ctx context.Context, id string) (int64, error) { - var revision int64 - err := kit.QueryScalar(ctx, "SELECT current_revision FROM issues WHERE id = ?", []any{id}, &revision) - return revision, err - }, - VersionRowCount: func(ctx context.Context, id string) (int, error) { - var count int - err := kit.QueryScalar(ctx, "SELECT COUNT(*) FROM issue_versions WHERE issue_id = ?", []any{id}, &count) - return count, err - }, - ParticipationGeneration: func(ctx context.Context, id string) (*int64, error) { - // scanRawSQLValue refuses a NULL numeric destination outright - // (issue_operations_contract_test.go's frozen scaffolding surface, - // bd-kue5t) rather than decaying it to zero, so a bare - // participation_generation read cannot scan into *sql.NullInt64 the - // way the dolt/embeddeddolt legs' std-library Scan does. epoch is - // seeded at 1 and only ever increments (version_history.go), so a - // stamped participation_generation is always >= 1 and -1 is an - // unambiguous "still NULL" sentinel for the COALESCE. - var gen int64 - if err := kit.QueryScalar(ctx, - "SELECT COALESCE(participation_generation, -1) FROM issues WHERE id = ?", []any{id}, &gen); err != nil { - return nil, err - } - if gen == -1 { - return nil, nil - } - return &gen, nil - }, - InWispsTable: func(ctx context.Context, id string) (bool, error) { - var rows int - if err := kit.QueryScalar(ctx, "SELECT COUNT(*) FROM wisps WHERE id = ?", []any{id}, &rows); err != nil { - return false, err - } - return rows > 0, nil - }, - } -} diff --git a/internal/storage/uow/edgereader.go b/internal/storage/uow/edgereader.go index f288f1bd6..bc4e456da 100644 --- a/internal/storage/uow/edgereader.go +++ b/internal/storage/uow/edgereader.go @@ -5,7 +5,6 @@ import ( "fmt" storageissueops "github.com/steveyegge/beads/internal/storage/issueops" - "github.com/steveyegge/beads/internal/types" publicops "github.com/steveyegge/beads/issueops" ) @@ -52,49 +51,33 @@ func (r *edgeReader) ReadEdges(ctx context.Context, request publicops.EdgeReadRe return publicops.EdgeReadResult{Anchors: []publicops.AnchorEdges{}}, nil } return RunTxRead(ctx, r.provider, func(ctx context.Context, uw UnitOfWork) (publicops.EdgeReadResult, error) { - return readEdgesInUOW(ctx, uw, anchors, request.Types) + present := make(map[string]struct{}, len(anchors)) + issues, err := uw.IssueUseCase().GetIssuesByIDs(ctx, anchors) + if err != nil { + return publicops.EdgeReadResult{}, err + } + for _, issue := range issues { + if issue != nil { + present[issue.ID] = struct{}{} + } + } + wisps, err := uw.IssueUseCase().GetWispsByIDs(ctx, anchors) + if err != nil { + return publicops.EdgeReadResult{}, err + } + for _, wisp := range wisps { + if wisp != nil { + present[wisp.ID] = struct{}{} + } + } + edges, err := uw.DependencyUseCase().GetIssueDependencyRecords(ctx, anchors) + if err != nil { + return publicops.EdgeReadResult{}, err + } + // The type filter and the order run HERE rather than in the reads + // above, so both implementations narrow and order through one function; + // a filter pushed into one side's query would put the narrowing in SQL + // on one backend and in Go on the other. + return storageissueops.FinishEdgeRead(anchors, present, edges, request.Types), nil }) } - -// ReadEdgesInUOW is the EdgeReader body, run inside the caller's unit of work. -func ReadEdgesInUOW(ctx context.Context, uw UnitOfWork, request publicops.EdgeReadRequest) (publicops.EdgeReadResult, error) { - if err := storageissueops.ValidateEdgeReadRequest(request); err != nil { - return publicops.EdgeReadResult{}, err - } - anchors := storageissueops.EdgeReadAnchors(request.IDs) - if len(anchors) == 0 { - return publicops.EdgeReadResult{Anchors: []publicops.AnchorEdges{}}, nil - } - return readEdgesInUOW(ctx, uw, anchors, request.Types) -} - -func readEdgesInUOW(ctx context.Context, uw UnitOfWork, anchors []string, depTypes []types.DependencyType) (publicops.EdgeReadResult, error) { - present := make(map[string]struct{}, len(anchors)) - issues, err := uw.IssueUseCase().GetIssuesByIDs(ctx, anchors) - if err != nil { - return publicops.EdgeReadResult{}, err - } - for _, issue := range issues { - if issue != nil { - present[issue.ID] = struct{}{} - } - } - wisps, err := uw.IssueUseCase().GetWispsByIDs(ctx, anchors) - if err != nil { - return publicops.EdgeReadResult{}, err - } - for _, wisp := range wisps { - if wisp != nil { - present[wisp.ID] = struct{}{} - } - } - edges, err := uw.DependencyUseCase().GetIssueDependencyRecords(ctx, anchors) - if err != nil { - return publicops.EdgeReadResult{}, err - } - // The type filter and the order run HERE rather than in the reads - // above, so both implementations narrow and order through one function; - // a filter pushed into one side's query would put the narrowing in SQL - // on one backend and in Go on the other. - return storageissueops.FinishEdgeRead(anchors, present, edges, depTypes), nil -} diff --git a/internal/storage/uow/errors.go b/internal/storage/uow/errors.go index b0323a42d..217d5904e 100644 --- a/internal/storage/uow/errors.go +++ b/internal/storage/uow/errors.go @@ -33,21 +33,6 @@ func isSerializationError(err error) bool { return false } -// isDoltAutocommitRollbackError reports whether err is Dolt rolling back a -// statement it ran under @@autocommit because the statement's commit-time -// merge conflicted with a concurrent writer (MySQL 1105, "Merge conflict -// detected, @autocommit transaction rolled back"). Outside an explicit -// transaction this, not 1213, is how such a conflict surfaces, and like a -// serialization failure it guarantees the statement left nothing behind. It -// mirrors the DoltStore's classifier of the same name. -func isDoltAutocommitRollbackError(err error) bool { - var mysqlErr *mysql.MySQLError - return errors.As(err, &mysqlErr) && - mysqlErr.Number == 1105 && - (mysqlErr.Message == "Merge conflict detected, @autocommit transaction rolled back" || - strings.HasPrefix(mysqlErr.Message, "Merge conflict detected, @autocommit transaction rolled back.")) -} - // isDatabaseExistsError reports whether err is the server refusing a bare // CREATE DATABASE because the database already exists (MySQL 1007, // ER_DB_CREATE_EXISTS). The message fallback matches how the rest of the diff --git a/internal/storage/uow/errors_test.go b/internal/storage/uow/errors_test.go index 1a9d8ce77..0796af0ac 100644 --- a/internal/storage/uow/errors_test.go +++ b/internal/storage/uow/errors_test.go @@ -59,37 +59,6 @@ func TestIsDatabaseNotFoundError(t *testing.T) { } } -// The DoltStore's table for its twin classifier, plus the prefix boundary: -// commitStagedTables replays exactly what this accepts, so a false positive -// re-runs a Dolt commit that failed for another reason. -func TestIsDoltAutocommitRollbackError(t *testing.T) { - exact := &mysql.MySQLError{ - Number: 1105, - Message: "Merge conflict detected, @autocommit transaction rolled back", - } - - for _, tt := range []struct { - name string - err error - want bool - }{ - {name: "nil", err: nil, want: false}, - {name: "exact typed Dolt rollback", err: fmt.Errorf("dolt commit: %w", exact), want: true}, - {name: "typed Dolt rollback with recovery guidance", err: &mysql.MySQLError{Number: 1105, Message: exact.Message + ". @autocommit must be disabled"}, want: true}, - {name: "typed text running on past the message", err: &mysql.MySQLError{Number: 1105, Message: exact.Message + "ward"}, want: false}, - {name: "untyped matching text", err: errors.New(exact.Error()), want: false}, - {name: "other typed 1105", err: &mysql.MySQLError{Number: 1105, Message: "Merge conflict detected"}, want: false}, - {name: "typed connection-like 1105", err: &mysql.MySQLError{Number: 1105, Message: "connection lost while validating commit"}, want: false}, - {name: "same text wrong code", err: &mysql.MySQLError{Number: 1213, Message: exact.Message}, want: false}, - } { - t.Run(tt.name, func(t *testing.T) { - if got := isDoltAutocommitRollbackError(tt.err); got != tt.want { - t.Fatalf("isDoltAutocommitRollbackError(%v) = %t, want %t", tt.err, got, tt.want) - } - }) - } -} - func TestClassifyUseDatabaseError(t *testing.T) { // The exact server errors observed from dolt 2.1.8 against a credential // granted only one database. diff --git a/internal/storage/uow/expected_revision_contract_test.go b/internal/storage/uow/expected_revision_contract_test.go deleted file mode 100644 index a1ccfa1b4..000000000 --- a/internal/storage/uow/expected_revision_contract_test.go +++ /dev/null @@ -1,60 +0,0 @@ -package uow - -import ( - "context" - "testing" - - "github.com/steveyegge/beads/backend/conformance" -) - -// TestExpectedRevisionContract wires this leg into the R16/R17 -// expected-revision contract. Every hook is nil (architecture §12's Phase 0 -// default), so each case below skips by name; this file exists so -// TestEveryLegWiresEveryRoleContract counts this leg, and so the cases -// start running for real the moment this leg grows a real -// CompareAndSetVersion (gastownhall/beads#6358). -// -// This leg previously wired every hook to an honest "not implemented" stub -// instead of leaving them nil, so each case ran for real and -// failed for that documented reason rather than skipping. That premise — -// that a later slice would give this leg its CompareAndSetVersion — went -// stale once review moved the real implementation to -// gastownhall/beads#6358 instead; a stub whose failure message blames a -// bead that will never land it is worse than an honest skip, so this file -// reverts to nil hooks (gastownhall/beads#6664, bee-ghosttrack review -// 5268699223). -func TestExpectedRevisionContract(t *testing.T) { - ctx := context.Background() - fixture := conformance.ExpectedRevisionFixture{IssuePrefix: "erev"} - - t.Run("AcceptsAWriteNamingTheCurrentVersion", func(t *testing.T) { - conformance.RunExpectedRevisionAcceptsAWriteNamingTheCurrentVersion(t, ctx, fixture) - }) - t.Run("AcceptsAWriteNamingNoVersion", func(t *testing.T) { - conformance.RunExpectedRevisionAcceptsAWriteNamingNoVersion(t, ctx, fixture) - }) - t.Run("CoversFieldsOutsideAnyWatchedSubset", func(t *testing.T) { - conformance.RunExpectedRevisionCoversFieldsOutsideAnyWatchedSubset(t, ctx, fixture) - }) - t.Run("RefusalReportsTheRefusingVersionAddress", func(t *testing.T) { - conformance.RunRefusalReportsTheRefusingVersionAddress(t, ctx, fixture) - }) - t.Run("RefusalReportsTheRefusingVersionsChangeAttribution", func(t *testing.T) { - conformance.RunRefusalReportsTheRefusingVersionsChangeAttribution(t, ctx, fixture) - }) - t.Run("RefusalIsATypedOutcomeNotAGenericError", func(t *testing.T) { - conformance.RunRefusalIsATypedOutcomeNotAGenericError(t, ctx, fixture) - }) - t.Run("RefusalIsDistinguishableFromAnAcceptedWrite", func(t *testing.T) { - conformance.RunRefusalIsDistinguishableFromAnAcceptedWrite(t, ctx, fixture) - }) - t.Run("RefusalIsDistinguishableFromNotFound", func(t *testing.T) { - conformance.RunRefusalIsDistinguishableFromNotFound(t, ctx, fixture) - }) - t.Run("RefusalIsDistinguishableFromValidationFailure", func(t *testing.T) { - conformance.RunRefusalIsDistinguishableFromValidationFailure(t, ctx, fixture) - }) - t.Run("RefusalNeverSilentlyPicksAWinner", func(t *testing.T) { - conformance.RunRefusalNeverSilentlyPicksAWinner(t, ctx, fixture) - }) -} diff --git a/internal/storage/uow/external_doltserver_provider_test.go b/internal/storage/uow/external_doltserver_provider_test.go index eab4eb81d..608b084fd 100644 --- a/internal/storage/uow/external_doltserver_provider_test.go +++ b/internal/storage/uow/external_doltserver_provider_test.go @@ -74,7 +74,11 @@ func TestNewExternalDoltServerUOWProvider_EndToEnd(t *testing.T) { storeRootDir := t.TempDir() shutdownOnInterrupt(t, storeRootDir) - verifiedShutdownCleanup(t, storeRootDir) + t.Cleanup(func() { + if err := proxy.Shutdown(storeRootDir); err != nil { + t.Logf("proxy.Shutdown(%s): %v", storeRootDir, err) + } + }) logPath := filepath.Join(t.TempDir(), "server.log") ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) @@ -136,7 +140,11 @@ func TestNewExternalDoltServerUOWProvider_ConcurrentInstantiation(t *testing.T) storeRootDir := t.TempDir() shutdownOnInterrupt(t, storeRootDir) - verifiedShutdownCleanup(t, storeRootDir) + t.Cleanup(func() { + if err := proxy.Shutdown(storeRootDir); err != nil { + t.Logf("proxy.Shutdown(%s): %v", storeRootDir, err) + } + }) logPath := filepath.Join(t.TempDir(), "server.log") external := configfile.ExternalDoltConfig{Host: "127.0.0.1", Port: portInt} @@ -220,7 +228,11 @@ func TestNewExternalDoltServerUOWProvider_FreshInitSelfHealsAfterMidPassFailure( storeRootDir := t.TempDir() shutdownOnInterrupt(t, storeRootDir) - verifiedShutdownCleanup(t, storeRootDir) + t.Cleanup(func() { + if err := proxy.Shutdown(storeRootDir); err != nil { + t.Logf("proxy.Shutdown(%s): %v", storeRootDir, err) + } + }) logPath := filepath.Join(t.TempDir(), "server.log") // Fire exactly once, right after migration step 1 (create issues) has @@ -307,7 +319,11 @@ func TestNewExternalDoltServerUOWProvider_PreexistingDirtyDatabaseIsNotHealed(t storeRootDir := t.TempDir() shutdownOnInterrupt(t, storeRootDir) - verifiedShutdownCleanup(t, storeRootDir) + t.Cleanup(func() { + if err := proxy.Shutdown(storeRootDir); err != nil { + t.Logf("proxy.Shutdown(%s): %v", storeRootDir, err) + } + }) logPath := filepath.Join(t.TempDir(), "server.log") ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second) diff --git a/internal/storage/uow/heartbeater.go b/internal/storage/uow/heartbeater.go deleted file mode 100644 index 75151c829..000000000 --- a/internal/storage/uow/heartbeater.go +++ /dev/null @@ -1,84 +0,0 @@ -package uow - -import ( - "context" - "fmt" - - storageissueops "github.com/steveyegge/beads/internal/storage/issueops" - publicops "github.com/steveyegge/beads/issueops" -) - -// HeartbeaterSource is the capability accessor a unit-of-work provider offers -// for the lease-refresh role, the sibling of LeaseReclaimerSource. -type HeartbeaterSource interface { - Heartbeater() (publicops.Heartbeater, error) -} - -// heartbeater refreshes a held lease through a unit of work. -type heartbeater struct { - provider UnitOfWorkProvider -} - -// Heartbeater returns the lease-refresh surface for this provider. -func (p *doltSQLProvider) Heartbeater() (publicops.Heartbeater, error) { - return NewHeartbeater(p) -} - -// NewHeartbeater constructs a public Heartbeater backed by provider. -func NewHeartbeater(provider UnitOfWorkProvider) (publicops.Heartbeater, error) { - if isNilUnitOfWorkProvider(provider) { - return nil, fmt.Errorf("new heartbeater: unit-of-work provider must not be nil") - } - return &heartbeater{provider: provider}, nil -} - -var _ publicops.Heartbeater = (*heartbeater)(nil) - -// Heartbeat reaches domain.IssueUseCase.RenewLease inside ONE unit of work. A -// refresh touches only the dolt_ignored leases table, so it mints no Dolt -// commit and records nothing in dolt_log; a re-arm is recorded as a mutation -// and commits like one. A serialization loser (a heartbeat racing a reclaim, -// release or close) is replayed against a fresh unit of work. -// -// VALIDATION HAPPENS BEFORE THE UNIT OF WORK OPENS, as on the dolt and -// embedded-dolt legs: a refusal then costs no connection. The shared body -// validates again inside the transaction. -func (h *heartbeater) Heartbeat(ctx context.Context, request publicops.HeartbeatRequest) (publicops.HeartbeatResult, error) { - if err := storageissueops.ValidateHeartbeatRequest(request); err != nil { - return publicops.HeartbeatResult{}, err - } - // A lease refusal commits (it may have deleted an orphaned lease row) and - // is reported after the commit; see HeartbeatRefusalCommits. - // - // THE COMMIT IS SCOPED (RunTxScoped). A refresh or a refusal writes only - // the dolt_ignored leases table and commits with the plain SQL COMMIT: no - // working-set scan, no Dolt commit, and so nothing another writer left - // uncommitted (a batch-mode edit) is swept into a heartbeat commit. A - // re-arm is recorded as a mutation and commits exactly the tables - // LeaseRearmVersionCommit names, never DOLT_COMMIT('-Am'), in a Dolt - // commit made after the SQL COMMIT (ScopedCommitter): built inside the - // transaction, it would write an issues row a concurrent writer changed - // meanwhile back to its value at this heartbeat's start. - type outcome struct { - result publicops.HeartbeatResult - refusal error - } - out, err := RunTxScoped(ctx, h.provider, func(ctx context.Context, uw UnitOfWork) (outcome, []string, string, error) { - result, err := uw.IssueUseCase().RenewLease(ctx, request) - if storageissueops.HeartbeatRefusalCommits(err) { - return outcome{refusal: err}, nil, "", nil - } - if err != nil { - return outcome{}, nil, "", err - } - tables, msg := storageissueops.LeaseRearmVersionCommit(request.IssueID, result.Rearmed) - return outcome{result: result}, tables.Sorted(), msg, nil - }) - if err != nil { - return publicops.HeartbeatResult{}, err - } - if out.refusal != nil { - return publicops.HeartbeatResult{}, out.refusal - } - return out.result, nil -} diff --git a/internal/storage/uow/heartbeater_contract_test.go b/internal/storage/uow/heartbeater_contract_test.go deleted file mode 100644 index 6148049d6..000000000 --- a/internal/storage/uow/heartbeater_contract_test.go +++ /dev/null @@ -1,339 +0,0 @@ -package uow - -import ( - "context" - "errors" - "fmt" - "strings" - "testing" - - "github.com/steveyegge/beads/backend/conformance" - "github.com/steveyegge/beads/internal/storage" - "github.com/steveyegge/beads/internal/types" - publicops "github.com/steveyegge/beads/issueops" -) - -// TestHeartbeaterContract runs the Heartbeater contract against the -// unit-of-work provider, which reaches internal/storage/issueops. -// ExecuteHeartbeatInTx through domain.IssueUseCase.RenewLease inside one unit -// of work committed with RunTxScoped: the SQL-only form for a refresh, the -// scoped commit for a re-arm. -func TestHeartbeaterContract(t *testing.T) { - ctx, cancel := context.WithCancel(t.Context()) - defer cancel() - fixture := newUOWHeartbeaterFixture(t, ctx, "hbc") - t.Run("RefreshesTheHoldersLease", func(t *testing.T) { - conformance.RunHeartbeaterRefreshesTheHoldersLease(t, ctx, fixture) - }) - t.Run("ChangesNothingOnTheRow", func(t *testing.T) { - conformance.RunHeartbeaterChangesNothingOnTheRow(t, ctx, fixture) - }) - t.Run("AGuardedWriteWithThePreHeartbeatRevisionStillWins", func(t *testing.T) { - conformance.RunHeartbeaterAGuardedWriteWithThePreHeartbeatRevisionStillWins(t, ctx, fixture) - }) - t.Run("RefusesAForeignHolder", func(t *testing.T) { - conformance.RunHeartbeaterRefusesAForeignHolder(t, ctx, fixture) - }) - t.Run("RefusesAfterClose", func(t *testing.T) { - conformance.RunHeartbeaterRefusesAfterClose(t, ctx, fixture) - }) - t.Run("RefusesAfterRelease", func(t *testing.T) { - conformance.RunHeartbeaterRefusesAfterRelease(t, ctx, fixture) - }) - t.Run("RefusesAfterReclaim", func(t *testing.T) { - conformance.RunHeartbeaterRefusesAfterReclaim(t, ctx, fixture) - }) - t.Run("RefusesAfterStatusMovedOffInProgress", func(t *testing.T) { - conformance.RunHeartbeaterRefusesAfterStatusMovedOffInProgress(t, ctx, fixture) - }) - t.Run("RefusesAWisp", func(t *testing.T) { - conformance.RunHeartbeaterRefusesAWisp(t, ctx, fixture) - }) - t.Run("RearmsAHandDoledClaim", func(t *testing.T) { - conformance.RunHeartbeaterRearmsAHandDoledClaim(t, ctx, fixture) - }) - t.Run("AnswersNotFoundForAnAbsentID", func(t *testing.T) { - conformance.RunHeartbeaterAnswersNotFoundForAnAbsentID(t, ctx, fixture) - }) - t.Run("RefusesABlankOrOverLongActor", func(t *testing.T) { - conformance.RunHeartbeaterRefusesABlankOrOverLongActor(t, ctx, fixture) - }) - t.Run("DoesNotMutateTheRequest", func(t *testing.T) { - conformance.RunHeartbeaterDoesNotMutateTheRequest(t, ctx, fixture) - }) - t.Run("CommitsOnlyItsOwnTables", func(t *testing.T) { - conformance.RunHeartbeaterCommitsOnlyItsOwnTables(t, ctx, fixture) - }) - t.Run("ASameSecondHeartbeatIsNotARearm", func(t *testing.T) { - conformance.RunHeartbeaterASameSecondHeartbeatIsNotARearm(t, ctx, fixture) - }) - t.Run("RefusesAnOrphanedLeaseOnAReleasedIssue", func(t *testing.T) { - conformance.RunHeartbeaterRefusesAnOrphanedLeaseOnAReleasedIssue(t, ctx, fixture) - }) - t.Run("RefusesAnOrphanedLeaseOnAClosedIssue", func(t *testing.T) { - conformance.RunHeartbeaterRefusesAnOrphanedLeaseOnAClosedIssue(t, ctx, fixture) - }) - t.Run("RefusesAnOrphanedLeaseOnAnIssueSomeoneElseHolds", func(t *testing.T) { - conformance.RunHeartbeaterRefusesAnOrphanedLeaseOnAnIssueSomeoneElseHolds(t, ctx, fixture) - }) -} - -// TestHeartbeaterRearmKeepsAConcurrentWrite is the LatentLabsSpace/NEXUS#92 -// regression for the re-arm's scoped commit: another session commits a change -// to a different issues row while the re-arm's transaction is open, and the -// Dolt commit the re-arm makes must stage issues as committed, with that -// change in it, not as the re-arm's start snapshot. DOLT_ADD stages a whole -// table from the session's transaction root, so staging inside the open -// transaction did the latter: HEAD then disagreed with the rows every reader -// sees, for a write still only in the working set (batch mode, or the window -// before the writer's own Dolt commit). ScopedCommitter commits the SQL -// transaction first. -func TestHeartbeaterRearmKeepsAConcurrentWrite(t *testing.T) { - ctx := t.Context() - const prefix = "hbw" - provider := newUOWRoleFixtureProvider(t, ctx, prefix) - kit := newUOWRoleFixtureKit(provider, prefix) - lifecycle, err := provider.(IssueLifecycleSource).IssueLifecycle() - if err != nil { - t.Fatalf("IssueLifecycle(): %v", err) - } - const holder = "hb-holder" - for _, tc := range []struct { - name string - write func(ctx context.Context, id, title string) error - }{ - {"a write with its own dolt commit", func(ctx context.Context, id, title string) error { - return RunTx(ctx, provider, func(ctx context.Context, uw UnitOfWork) (string, error) { - _, err := uw.RawSQLUseCase().Exec(ctx, "UPDATE issues SET title = ? WHERE id = ?", title, id) - return "bd: retitle " + id, err - }) - }}, - {"a write only in the working set", func(ctx context.Context, id, title string) error { - _, err := RunTxEphemeral(ctx, provider, func(ctx context.Context, uw UnitOfWork) (struct{}, error) { - _, err := uw.RawSQLUseCase().Exec(ctx, "UPDATE issues SET title = ? WHERE id = ?", title, id) - return struct{}{}, err - }) - return err - }}, - } { - t.Run(tc.name, func(t *testing.T) { - suffix := strings.ReplaceAll(tc.name, " ", "-") - doled, bystander := prefix+"-doled-"+suffix, prefix+"-bystander-"+suffix - for _, id := range []string{doled, bystander} { - issue := &types.Issue{ID: id, Title: "seed " + id, Status: types.StatusOpen, Priority: 2, IssueType: types.TypeTask} - if err := kit.CreateIssue(ctx, issue, "seeder"); err != nil { - t.Fatalf("seed %s: %v", id, err) - } - } - if _, err := lifecycle.Update(ctx, publicops.UpdateRequest{ - Actor: holder, IssueID: doled, - Patch: publicops.IssuePatch{ - Status: publicops.Field[publicops.Status]{Set: true, Value: types.StatusInProgress}, - Assignee: publicops.Field[string]{Set: true, Value: holder}, - }, - }); err != nil { - t.Fatalf("hand-dole %s: %v", doled, err) - } - - const retitled = "retitled while the re-arm was open" - wrote := false - heartbeater, err := NewHeartbeater(&beforeScopedCommitProvider{ - UnitOfWorkProvider: provider, - before: func(ctx context.Context) error { - if wrote { - return nil - } - wrote = true - return tc.write(ctx, bystander, retitled) - }, - }) - if err != nil { - t.Fatalf("NewHeartbeater: %v", err) - } - result, err := heartbeater.Heartbeat(ctx, publicops.HeartbeatRequest{Actor: holder, IssueID: doled}) - if err != nil { - t.Fatalf("Heartbeat: %v", err) - } - if !result.Rearmed || !wrote { - t.Fatalf("result = %+v, concurrent write ran = %v: the case needs a re-arm with a write inside its window", result, wrote) - } - - for _, at := range []string{"the working set", "HEAD"} { - query := "SELECT title FROM issues WHERE id = ?" - if at == "HEAD" { - query = "SELECT title FROM issues AS OF 'HEAD' WHERE id = ?" - } - var title string - if err := kit.QueryScalar(ctx, query, []any{bystander}, &title); err != nil { - t.Fatalf("read %s title at %s: %v", bystander, at, err) - } - if title != retitled { - t.Errorf("%s title at %s = %q, want %q: the re-arm's Dolt commit staged issues from its start snapshot", bystander, at, title, retitled) - } - } - // The re-arm itself is committed: its reminted row_lock is at HEAD - // (events is dolt_ignored, so the issues row is what its commit - // carries). - var working, head int64 - if err := kit.QueryScalar(ctx, "SELECT row_lock FROM issues WHERE id = ?", []any{doled}, &working); err != nil { - t.Fatalf("read %s row_lock: %v", doled, err) - } - if err := kit.QueryScalar(ctx, "SELECT row_lock FROM issues AS OF 'HEAD' WHERE id = ?", []any{doled}, &head); err != nil { - t.Fatalf("read %s row_lock at HEAD: %v", doled, err) - } - var commits int - if err := kit.QueryScalar(ctx, "SELECT COUNT(*) FROM dolt_log WHERE message = ?", []any{"bd: re-arm lease " + doled}, &commits); err != nil { - t.Fatalf("count re-arm commits: %v", err) - } - if head != working || commits != 1 { - t.Errorf("%s row_lock at HEAD = %d (working set %d), re-arm commits = %d, want the re-arm in one Dolt commit", doled, head, working, commits) - } - }) - } -} - -// beforeScopedCommitProvider runs before ahead of each scoped commit of the -// units of work it opens: a way into the window between a transaction's -// writes and its commit. -type beforeScopedCommitProvider struct { - UnitOfWorkProvider - before func(context.Context) error -} - -func (p *beforeScopedCommitProvider) NewUOW(ctx context.Context) (UnitOfWork, error) { - uw, err := p.UnitOfWorkProvider.NewUOW(ctx) - if err != nil { - return nil, err - } - return &beforeScopedCommitUOW{UnitOfWork: uw, before: p.before}, nil -} - -type beforeScopedCommitUOW struct { - UnitOfWork - before func(context.Context) error -} - -func (u *beforeScopedCommitUOW) Unwrap() UnitOfWork { return u.UnitOfWork } - -func (u *beforeScopedCommitUOW) CommitScoped(ctx context.Context, tables []string, message string) error { - if err := u.before(ctx); err != nil { - return err - } - return commitScoped(ctx, u.UnitOfWork, tables, message) -} - -// TestHeartbeaterRefusesBeforeOpeningAUOW: a malformed heartbeat is a -// deterministic validation failure and must not cost a database connection to -// discover, as on the dolt and embedded-dolt legs. -func TestHeartbeaterRefusesBeforeOpeningAUOW(t *testing.T) { - for _, tc := range []struct { - name string - request publicops.HeartbeatRequest - }{ - {"no actor", publicops.HeartbeatRequest{IssueID: "x"}}, - {"no id", publicops.HeartbeatRequest{Actor: "worker"}}, - } { - t.Run(tc.name, func(t *testing.T) { - provider := &mockUnitOfWorkProvider{newUOWErr: errors.New("unexpected unit-of-work open")} - heartbeater, err := NewHeartbeater(provider) - if err != nil { - t.Fatalf("NewHeartbeater: %v", err) - } - if _, err := heartbeater.Heartbeat(context.Background(), tc.request); !errors.Is(err, publicops.ErrValidation) { - t.Fatalf("err = %v, want ErrValidation", err) - } - if provider.newUOWCalls != 0 { - t.Errorf("an invalid request opened %d units of work", provider.newUOWCalls) - } - }) - } -} - -// newUOWHeartbeaterFixture takes every role through the provider's own -// capability accessors, and the hooked heartbeater off the notifying provider -// the proxied CLI route wraps around its provider. -func newUOWHeartbeaterFixture(t *testing.T, ctx context.Context, prefix string) conformance.HeartbeaterFixture { - t.Helper() - provider := newUOWRoleFixtureProvider(t, ctx, prefix) - source, ok := provider.(HeartbeaterSource) - if !ok { - t.Fatalf("provider %T does not offer the Heartbeater accessor", provider) - } - heartbeater, err := source.Heartbeater() - if err != nil { - t.Fatalf("Heartbeater(): %v", err) - } - claimer, err := provider.(IssueClaimerSource).IssueClaimer() - if err != nil { - t.Fatalf("IssueClaimer(): %v", err) - } - lifecycle, err := provider.(IssueLifecycleSource).IssueLifecycle() - if err != nil { - t.Fatalf("IssueLifecycle(): %v", err) - } - releaser, err := provider.(ReleaserSource).Releaser() - if err != nil { - t.Fatalf("Releaser(): %v", err) - } - reclaimer, err := provider.(LeaseReclaimerSource).LeaseReclaimer() - if err != nil { - t.Fatalf("LeaseReclaimer(): %v", err) - } - configurer, ok := provider.(storage.EventsJournalConfigurer) - if !ok { - t.Fatalf("provider %T does not implement storage.EventsJournalConfigurer", provider) - } - versionConfigurer, ok := provider.(storage.VersionedHistoryConfigurer) - if !ok { - t.Fatalf("provider %T does not implement storage.VersionedHistoryConfigurer", provider) - } - runner, fired := conformance.NewUpdateHookLog(t) - hooked := heartbeater - if runner != nil { - notifying, ok := NewNotifyingProvider(provider, Sinks{Hook: runner}).(HeartbeaterSource) - if !ok { - t.Fatal("the notifying provider does not offer the Heartbeater accessor") - } - if hooked, err = notifying.Heartbeater(); err != nil { - t.Fatalf("hooked Heartbeater(): %v", err) - } - } - kit := newUOWRoleFixtureKit(provider, prefix) - return conformance.HeartbeaterFixture{ - IssuePrefix: kit.IssuePrefix, - Heartbeater: heartbeater, - CreateIssue: kit.CreateIssue, - CreateWisp: kit.CreateWisp, - Claimer: claimer, - Lifecycle: lifecycle, - Releaser: releaser, - LeaseReclaimer: reclaimer, - QueryScalar: kit.QueryScalar, - CountHistory: kit.CountHistory, - ExecUncommitted: func(ctx context.Context, statements []conformance.SQLStatement) error { - _, err := RunTxEphemeral(ctx, provider, func(ctx context.Context, uw UnitOfWork) (struct{}, error) { - for _, stmt := range statements { - if _, err := uw.RawSQLUseCase().Exec(ctx, stmt.Query, stmt.Args...); err != nil { - return struct{}{}, fmt.Errorf("%s: %w", stmt.Query, err) - } - } - return struct{}{}, nil - }) - return err - }, - Exec: func(ctx context.Context, statements []conformance.SQLStatement) error { - return RunTx(ctx, provider, func(ctx context.Context, uw UnitOfWork) (string, error) { - for _, stmt := range statements { - if _, err := uw.RawSQLUseCase().Exec(ctx, stmt.Query, stmt.Args...); err != nil { - return "", fmt.Errorf("%s: %w", stmt.Query, err) - } - } - return "seed heartbeater rows", nil - }) - }, - SetJournalEnabled: configurer.SetEventsJournalEnabled, - SetVersionedHistoryEnabled: versionConfigurer.SetVersionedHistoryEnabled, - HookedHeartbeater: hooked, - FiredUpdates: fired, - } -} diff --git a/internal/storage/uow/history_off_differential_test.go b/internal/storage/uow/history_off_differential_test.go deleted file mode 100644 index aabe0eefe..000000000 --- a/internal/storage/uow/history_off_differential_test.go +++ /dev/null @@ -1,92 +0,0 @@ -package uow - -import ( - "context" - "testing" - "time" - - "github.com/steveyegge/beads/internal/storage/domain" - "github.com/steveyegge/beads/internal/storage/historyoffequiv" - "github.com/steveyegge/beads/internal/types" - publicops "github.com/steveyegge/beads/issueops" -) - -// TestVersionedHistoryOffMatchesPreStackGolden_UOW is the unit-of-work leg's -// versioned-history flag-off differential (internal/storage/historyoffequiv): -// on a provider whose versioned history was never turned on, the scripted -// mutation set must leave every table as the code before the -// versioned-history stack left it, apart from that stack's new columns and -// table staying NULL and empty. -// -// Beyond the shared role scenario this leg drives the domain create that -// leaves CreateOnly unset (the quick, todo and batch proxied servers' path): -// it is an upsert, and the stack gave it an existence probe that must not run -// while history is off (see TestInsertUpsertWithHistoryOffIssuesThePreStackStatements -// in domain/db). The importer role's upsert of an existing row runs too. -func TestVersionedHistoryOffMatchesPreStackGolden_UOW(t *testing.T) { - ctx := context.Background() - provider := newUOWRoleFixtureProvider(t, ctx, historyoffequiv.Prefix) - roles, ok := provider.(historyoffequiv.Roles) - if !ok { - t.Fatalf("provider %T does not offer every role the scenario drives", provider) - } - fixedAt := time.Date(2001, 2, 3, 4, 5, 6, 0, time.UTC) - issue := func(id, title string) *types.Issue { - return &types.Issue{ID: id, Title: title, Description: "about " + id, Status: types.StatusOpen, - Priority: 2, IssueType: types.TypeTask, CreatedAt: fixedAt, UpdatedAt: fixedAt} - } - domainCreate := func(wisp bool, is *types.Issue) func(context.Context) error { - return func(ctx context.Context) error { - return RunTx(ctx, provider, func(ctx context.Context, uw UnitOfWork) (string, error) { - params := domain.CreateIssueParams{Issue: is, ExplicitID: is.ID} - var err error - if wisp { - _, err = uw.IssueUseCase().CreateWisp(ctx, params, "uma") - } else { - _, err = uw.IssueUseCase().CreateIssue(ctx, params, "uma") - } - return "bd: create " + is.ID, err - }) - } - } - historyoffequiv.Run(t, historyoffequiv.Leg{ - Name: "uow", - Roles: roles, - Wake: func(ctx context.Context) error { - _, err := WakeExpiredDefers(ctx, provider) - return err - }, - Extra: []historyoffequiv.Step{ - {Name: "domain upsert new u1", Run: domainCreate(false, issue("ho-u1", "uniform one"))}, - {Name: "domain upsert existing u1", Run: domainCreate(false, issue("ho-u1", "uniform one again"))}, - {Name: "domain upsert new wisp uw", Run: domainCreate(true, issue("ho-uw", "uniform wisp"))}, - {Name: "domain upsert existing wisp uw", Run: domainCreate(true, issue("ho-uw", "uniform wisp again"))}, - {Name: "import upsert u1 and new u2", Run: func(ctx context.Context) error { - importer, err := provider.(interface { - Importer() (publicops.Importer, error) - }).Importer() - if err != nil { - return err - } - u1 := issue("ho-u1", "uniform one imported") - u1.UpdatedAt = fixedAt.Add(time.Hour) - u2 := issue("ho-u2", "uniform two") - u2.Labels = []string{"imported"} - u2.Dependencies = []*types.Dependency{{IssueID: "ho-u2", DependsOnID: "ho-u1", Type: types.DepBlocks, CreatedAt: fixedAt}} - u2.Comments = []*types.Comment{{ID: "ho-u2-comment", IssueID: "ho-u2", Author: "importer", Text: "imported", CreatedAt: fixedAt}} - _, err = importer.ImportBatch(ctx, publicops.ImportBatchRequest{Actor: "importer", - Issues: []*types.Issue{u1, u2}, SkipPrefixValidation: true}) - return err - }}, - }, - Query: func(ctx context.Context, query string) ([][]*string, error) { - return RunTxRead(ctx, provider, func(ctx context.Context, uw UnitOfWork) ([][]*string, error) { - result, err := uw.RawSQLUseCase().Query(ctx, query) - if err != nil { - return nil, err - } - return historyoffequiv.AnyRows(result.Rows), nil - }) - }, - }) -} diff --git a/internal/storage/uow/importer.go b/internal/storage/uow/importer.go index 3d195c44c..059b2b7a3 100644 --- a/internal/storage/uow/importer.go +++ b/internal/storage/uow/importer.go @@ -53,35 +53,6 @@ func (o *importer) ImportBatch(ctx context.Context, request publicops.ImportBatc return RunTxResult(ctx, o.provider, func(ctx context.Context, uw UnitOfWork) (publicops.ImportBatchResult, string, error) { var result publicops.ImportBatchResult - // Seed issue_prefix before CreateIssuesInTxWithResult needs it (via - // NewBatchContext -> ReadConfigPrefix), for the same reason the sync - // below exists: an externally-provisioned database's config table can - // be missing the row config.yaml already carries. Seeding only when - // the table is empty — never overwriting an existing prefix — keeps - // this a pure precondition for the sync 40 lines down, not a second, - // conflicting writer. - // - // PrefixSynced is set HERE and not left to the sync: once the seed has - // written the value, the sync's `stored != request.SyncIssuePrefix` - // test is false and it reports nothing, so a batch that lands no rows - // and no memories would build an empty commit message and RunTxResult - // would roll the seeded row back (tx.go, importBatchCommitMessage). - // The seed IS the prefix write in that case and has to say so. - // - // Unlike the sync, a failure here does NOT degrade to "not synced": - // this write is the precondition for the very next statement, so - // swallowing it would surface as the `issue_prefix config is missing` - // error this seed exists to prevent, and would hide a serialization - // failure from RunTxResult's retry classifier. - if request.SyncIssuePrefix != "" { - if stored, _ := uw.ConfigUseCase().GetConfig(ctx, "issue_prefix"); stored == "" { - if err := uw.ConfigUseCase().SetConfig(ctx, "issue_prefix", request.SyncIssuePrefix); err != nil { - return publicops.ImportBatchResult{}, "", fmt.Errorf("seed issue_prefix: %w", err) - } - result.PrefixSynced = true - } - } - if len(request.Issues) > 0 { runner, err := importStatementRunner(uw) if err != nil { diff --git a/internal/storage/uow/importer_contract_test.go b/internal/storage/uow/importer_contract_test.go index 10c71ddae..0d20fa39e 100644 --- a/internal/storage/uow/importer_contract_test.go +++ b/internal/storage/uow/importer_contract_test.go @@ -34,8 +34,8 @@ func TestImporterContract(t *testing.T) { t.Run("ReportsTheAbsentTargetItDroppedOnce", func(t *testing.T) { conformance.RunImporterReportsTheAbsentTargetItDroppedOnce(t, ctx, fixture) }) - t.Run("WiresTheCrossPlaneEdgeBetweenItsRows", func(t *testing.T) { - conformance.RunImporterWiresTheCrossPlaneEdgeBetweenItsRows(t, ctx, fixture) + t.Run("ReportsTheCrossPlaneEdgeItDropped", func(t *testing.T) { + conformance.RunImporterReportsTheCrossPlaneEdgeItDropped(t, ctx, fixture) }) t.Run("ReportsTheCycleEdgeItDropped", func(t *testing.T) { conformance.RunImporterReportsTheCycleEdgeItDropped(t, ctx, fixture) diff --git a/internal/storage/uow/importer_uow_test.go b/internal/storage/uow/importer_uow_test.go index 57fee1bb0..cd1658303 100644 --- a/internal/storage/uow/importer_uow_test.go +++ b/internal/storage/uow/importer_uow_test.go @@ -40,14 +40,6 @@ func TestImporterUOW(t *testing.T) { } return n } - countHistoryMatching := func(t *testing.T, pattern string) int { - t.Helper() - n, err := kit.CountHistoryMatching(ctx, pattern) - if err != nil { - t.Fatalf("CountHistoryMatching %q: %v", pattern, err) - } - return n - } queryInt := func(t *testing.T, query string, args ...any) int { t.Helper() var n int @@ -126,57 +118,6 @@ func TestImporterUOW(t *testing.T) { } }) - t.Run("CrossPlaneInBatchEdgesAreWiredInTheOneCommit", func(t *testing.T) { - // wy-a648lq: a regular<->wisp edge whose BOTH ends are rows of this - // batch used to be skip-reported by the engine's per-batch plane filter, - // and since a re-import upserts the rows unchanged it could never be - // backfilled. Both directions are asserted because they land in - // different tables and columns. - before := countHistory(t) - when := time.Date(2026, 8, 2, 12, 0, 0, 0, time.UTC) - result, err := imp.ImportBatch(ctx, publicops.ImportBatchRequest{ - Actor: "importer-test", - Issues: []*types.Issue{ - { - ID: "imp-plane-d", Title: "Durable end", Status: types.StatusOpen, - IssueType: types.TypeTask, Priority: 2, - CreatedAt: when, UpdatedAt: when, - }, - { - ID: "imp-plane-w", Title: "Ephemeral end", Status: types.StatusOpen, - IssueType: types.TypeTask, Priority: 2, Ephemeral: true, - Dependencies: []*types.Dependency{{IssueID: "imp-plane-w", DependsOnID: "imp-plane-d", Type: types.DepBlocks}}, - CreatedAt: when, UpdatedAt: when, - }, - { - ID: "imp-plane-d2", Title: "Durable row blocked by the wisp", Status: types.StatusOpen, - IssueType: types.TypeTask, Priority: 2, - Dependencies: []*types.Dependency{{IssueID: "imp-plane-d2", DependsOnID: "imp-plane-w", Type: types.DepBlocks}}, - CreatedAt: when, UpdatedAt: when, - }, - }, - Source: "planes.jsonl", - }) - if err != nil { - t.Fatalf("ImportBatch: %v", err) - } - if result.Created != 3 { - t.Errorf("Created = %d, want 3", result.Created) - } - if len(result.SkippedDependencies) != 0 { - t.Errorf("SkippedDependencies = %+v, want none: both ends of each edge are rows of this batch", result.SkippedDependencies) - } - if after := countHistory(t); after != before+1 { - t.Errorf("history entries = %d, want %d (the edges ride the batch's ONE commit)", after, before+1) - } - if got := queryInt(t, "SELECT COUNT(*) FROM wisp_dependencies WHERE issue_id = 'imp-plane-w' AND depends_on_issue_id = 'imp-plane-d' AND type = 'blocks'"); got != 1 { - t.Errorf("wisp -> durable blocks edge = %d, want 1", got) - } - if got := queryInt(t, "SELECT COUNT(*) FROM dependencies WHERE issue_id = 'imp-plane-d2' AND depends_on_wisp_id = 'imp-plane-w' AND type = 'blocks'"); got != 1 { - t.Errorf("durable -> wisp blocks edge = %d, want 1", got) - } - }) - t.Run("ReimportConvergesWithoutDuplicatingAuxData", func(t *testing.T) { when := time.Date(2026, 8, 1, 12, 0, 0, 0, time.UTC) row := func() *types.Issue { @@ -269,120 +210,6 @@ func TestImporterUOW(t *testing.T) { } }) - t.Run("SeedsPrefixBeforeCreateNeedsIt", func(t *testing.T) { - // Simulate an externally-provisioned database: config.yaml (modeled - // here by SyncIssuePrefix, which is what the CLI populates it from) - // carries a prefix, but the database's own config table does not — - // exactly what NewBatchContext's ReadConfigPrefix treats as "missing" - // (sql.ErrNoRows OR an empty value), the state a provisioner that - // creates the Dolt database directly (bypassing `bd init`, which is - // what normally writes this row) leaves it in. - if err := kit.SetConfig(ctx, "issue_prefix", ""); err != nil { - t.Fatalf("clear issue_prefix: %v", err) - } - - when := time.Date(2026, 8, 2, 12, 0, 0, 0, time.UTC) - result, err := imp.ImportBatch(ctx, publicops.ImportBatchRequest{ - Actor: "importer-test", - Issues: []*types.Issue{{ - ID: "imp-seed", Title: "Seeded via prefix precondition", Status: types.StatusOpen, - IssueType: types.TypeTask, Priority: 2, CreatedAt: when, UpdatedAt: when, - }}, - SkipPrefixValidation: true, - SyncIssuePrefix: "imp", - Source: "seed.jsonl", - }) - if err != nil { - t.Fatalf("ImportBatch against a database with no config-table prefix: %v", err) - } - if result.Created != 1 { - t.Errorf("Created = %d, want 1", result.Created) - } - if got := queryString(t, "SELECT value FROM config WHERE `key` = 'issue_prefix'"); got != "imp" { - t.Errorf("issue_prefix after seed = %q, want %q", got, "imp") - } - // Restore for any later subtest, as the neighbours do. The seed under - // test happens to leave "imp" behind, but a fixture restored by the - // code under test would silently change what a subtest inserted after - // this one runs against. - if err := kit.SetConfig(ctx, "issue_prefix", "imp"); err != nil { - t.Fatalf("restore issue_prefix: %v", err) - } - }) - - t.Run("SeedsPrefixEvenWhenEveryRowIsRejected", func(t *testing.T) { - // The seed must report itself as the prefix write, or a batch that - // lands no rows commits NOTHING and discards it: - // importBatchCommitMessage returns "" when Created == 0, - // MemoriesImported == 0 and !PrefixSynced, RunTxResultWithin skips - // uw.Commit on an empty message (tx.go), and the deferred closeAttempt - // ROLLS BACK the seeded row. Reachable against an - // externally-provisioned database whenever every input row is dropped - // — here by the in-transaction stale guard, in the CLI also by the - // title dedup. SeedsPrefixBeforeCreateNeedsIt cannot catch this - // (Created == 1 carries the commit), and - // PrefixSyncAloneCommitsUnderTheSyncMessage runs with a NON-empty - // stored prefix, so the sync writes there and the seed never fires. - landed := time.Date(2026, 8, 3, 12, 0, 0, 0, time.UTC) - if _, err := imp.ImportBatch(ctx, publicops.ImportBatchRequest{ - Actor: "importer-test", - Issues: []*types.Issue{{ - ID: "imp-rejected", Title: "Row the stale guard will keep", Status: types.StatusOpen, - IssueType: types.TypeTask, Priority: 2, CreatedAt: landed, UpdatedAt: landed, - }}, - Source: "reject-setup.jsonl", - }); err != nil { - t.Fatalf("seed the row the stale guard will keep: %v", err) - } - if err := kit.SetConfig(ctx, "issue_prefix", ""); err != nil { - t.Fatalf("clear issue_prefix: %v", err) - } - - // Both counts are snapshotted: TestImporterUOW shares one database in - // subtest order, and PrefixSyncAloneCommitsUnderTheSyncMessage has - // already committed under the sync message by the time this runs — so - // only a DELTA can tell that the seed's own commit carries it. - const syncMessageLike = "%sync issue_prefix from config.yaml%" - before := countHistory(t) - syncNamedBefore := countHistoryMatching(t, syncMessageLike) - stale := landed.Add(-24 * time.Hour) - result, err := imp.ImportBatch(ctx, publicops.ImportBatchRequest{ - Actor: "importer-test", - Issues: []*types.Issue{{ - ID: "imp-rejected", Title: "Stale snapshot", Status: types.StatusOpen, - IssueType: types.TypeTask, Priority: 3, CreatedAt: stale, UpdatedAt: stale, - }}, - SkipPrefixValidation: true, - SyncIssuePrefix: "impseed", - Source: "reject.jsonl", - }) - if err != nil { - t.Fatalf("all-rejected ImportBatch against a database with no config-table prefix: %v", err) - } - // Assert the batch really landed nothing for the reason this case is - // about, so the prefix assertion below cannot be carried by a row. - if result.Created != 0 || len(result.StaleRejectedIDs) != 1 { - t.Fatalf("result = %+v, want Created 0 with the one row stale-rejected", result) - } - if !result.PrefixSynced { - t.Errorf("PrefixSynced = false, want true — the seed wrote the prefix, so the batch must report it") - } - if got := queryString(t, "SELECT value FROM config WHERE `key` = 'issue_prefix'"); got != "impseed" { - t.Errorf("issue_prefix after an all-rejected batch = %q, want %q (the seed was rolled back)", got, "impseed") - } - if after := countHistory(t); after != before+1 { - t.Errorf("history entries moved %d -> %d, want exactly one commit carrying the seeded prefix", before, after) - } - if syncNamedAfter := countHistoryMatching(t, syncMessageLike); syncNamedAfter != syncNamedBefore+1 { - t.Errorf("history entries naming the issue_prefix sync moved %d -> %d, want exactly one more — the seed's own commit must carry that message", syncNamedBefore, syncNamedAfter) - } - - // Restore for any later subtest. - if err := kit.SetConfig(ctx, "issue_prefix", "imp"); err != nil { - t.Fatalf("restore issue_prefix: %v", err) - } - }) - t.Run("EmptyActorIsRefused", func(t *testing.T) { if _, err := imp.ImportBatch(ctx, publicops.ImportBatchRequest{Source: "noactor.jsonl"}); err == nil { t.Fatal("ImportBatch with empty actor should be refused") diff --git a/internal/storage/uow/issue_operations.go b/internal/storage/uow/issue_operations.go index 18ba064c4..ee8ca59f7 100644 --- a/internal/storage/uow/issue_operations.go +++ b/internal/storage/uow/issue_operations.go @@ -231,15 +231,9 @@ func (o *issueOperations) Update(ctx context.Context, request publicops.UpdateRe // version and status before authorizing, so when one is stale the fence // stands down and ApplyUpdate reports the mismatch instead. if updatePreconditionsHold(attempt, before) { - if err := storageissueops.AuthorizeTemplateUpdate(before, attempt); err != nil { - return publicops.UpdateResult{}, "", err - } if err := authorizeAssigneeTransfer(ctx, uw, before, attempt); err != nil { return publicops.UpdateResult{}, "", err } - if err := storageissueops.AuthorizeNotesOverwrite(before, attempt); err != nil { - return publicops.UpdateResult{}, "", err - } } // ActorMatches (not a verbatim compare, ga-v2k49): a holder re-claiming // under a respelled identity is a real CAS win one layer down (domain/db's @@ -261,13 +255,9 @@ func (o *issueOperations) Update(ctx context.Context, request publicops.UpdateRe // updatePreconditionsHold reports whether request's compare-and-set // preconditions match before — that is, whether ApplyUpdate would get past its -// own guards, leaving the fences as the operative refusals. ExpectedAssignee -// belongs here since the notes fence arrived: the ASSIGNEE fence stands down -// whenever a caller supplies one, but the notes fence deliberately does not, -// and a stale compare-and-set must outrank it (the dolt leg orders -// CheckExpectedFieldsInTx ahead of AuthorizeNotesOverwrite). The comparison is -// ActorMatches, not verbatim ==, matching CheckExpectedFieldsInTx (ga-wzl83); -// a pointer to "" is a real guard meaning "expected unassigned". +// own guards, leaving the assignee fence as the operative refusal. +// ExpectedAssignee needs no entry here: the fence already stands down whenever +// a caller supplies one. func updatePreconditionsHold(request publicops.UpdateRequest, before *types.Issue) bool { if request.ExpectedVersion != nil && *request.ExpectedVersion != before.RowVersion { return false @@ -275,9 +265,6 @@ func updatePreconditionsHold(request publicops.UpdateRequest, before *types.Issu if request.ExpectedStatus != nil && *request.ExpectedStatus != before.Status { return false } - if request.ExpectedAssignee != nil && !storageissueops.ActorMatches(before.Assignee, *request.ExpectedAssignee) { - return false - } return true } @@ -313,9 +300,6 @@ func updateSpec(request publicops.UpdateRequest) (domain.UpdateSpec, error) { setField(fields, "acceptance_criteria", patch.AcceptanceCriteria) setField(fields, "spec_id", patch.SpecID) setField(fields, "await_id", patch.AwaitID) - if patch.Waiters.Set { - fields["waiters"] = storageissueops.PatchWaiters(patch.Waiters) - } setField(fields, "status", patch.Status) setField(fields, "priority", patch.Priority) if patch.IssueType.Set { @@ -328,9 +312,9 @@ func updateSpec(request publicops.UpdateRequest) (domain.UpdateSpec, error) { setField(fields, "external_ref", patch.ExternalRef) setField(fields, "due_at", patch.DueAt) setField(fields, "defer_until", patch.DeferUntil) - // Notes+AppendNotes is refused by ValidateUpdateRequest, which both - // callers (issueOperations.Update and the batch applier) run before - // building a spec — no duplicate check here. + if patch.Notes.Set && patch.AppendNotes.Set { + return domain.UpdateSpec{}, validationError(fmt.Errorf("update: notes and append notes cannot both be set")) + } setField(fields, "notes", patch.Notes) if patch.AppendNotes.Set { fields[storageissueops.OpAppendNotes] = patch.AppendNotes.Value @@ -443,15 +427,8 @@ func (o *issueOperations) Close(ctx context.Context, request publicops.CloseRequ if err != nil { return publicops.CloseResult{}, "", err } - result := publicops.CloseResult{Issue: hydrated, Changed: !semanticIssueEqual(before, hydrated), OpenChildren: closed.OpenChildren} - if attempt.AutoCloseMolecule { - auto, err := CloseCompletedMoleculeInUOW(ctx, uw, attempt.IssueID, attempt.Actor, attempt.Session) - if err != nil { - return publicops.CloseResult{}, "", err - } - result.AutoClosedMolecule, result.MoleculeAutoCloseRefusal = auto.Root, auto.Refusal - } - return result, "close issue", nil + return publicops.CloseResult{Issue: hydrated, Changed: !semanticIssueEqual(before, hydrated), OpenChildren: closed.OpenChildren}, + "close issue", nil }) } diff --git a/internal/storage/uow/issue_operations_claim_contract_test.go b/internal/storage/uow/issue_operations_claim_contract_test.go index 9ecc081a2..c4c84067b 100644 --- a/internal/storage/uow/issue_operations_claim_contract_test.go +++ b/internal/storage/uow/issue_operations_claim_contract_test.go @@ -289,7 +289,7 @@ func TestBatchRunUpdateIdempotentClaimAcrossSpellingReportsNoChange(t *testing.T result, err := run.runUpdate(context.Background(), publicops.UpdateRequest{ Actor: "gastown.mayor", IssueID: "bd-1", Claim: true, - }, true) + }) if err != nil { t.Fatalf("runUpdate: %v, want success (same identity, different separator spelling)", err) } @@ -307,7 +307,7 @@ func TestBatchRunUpdateClaimByGenuinelyDifferentIdentityReportsAChange(t *testin result, err := run.runUpdate(context.Background(), publicops.UpdateRequest{ Actor: "gastown.dog-3", IssueID: "bd-1", Claim: true, - }, true) + }) if err != nil { t.Fatalf("runUpdate: %v", err) } diff --git a/internal/storage/uow/issue_reader.go b/internal/storage/uow/issue_reader.go index 8c62b8779..c600114d9 100644 --- a/internal/storage/uow/issue_reader.go +++ b/internal/storage/uow/issue_reader.go @@ -4,9 +4,7 @@ import ( "context" "fmt" - "github.com/steveyegge/beads/internal/storage/dberrors" "github.com/steveyegge/beads/internal/storage/domain" - "github.com/steveyegge/beads/internal/storage/sqlbuild" "github.com/steveyegge/beads/internal/workapi" publicops "github.com/steveyegge/beads/issueops" ) @@ -122,7 +120,7 @@ func (r *issueReader) List(ctx context.Context, req publicops.ListRequest) (publ if req.ReadyFlag { page, err = uw.IssueUseCase().GetReadyWorkWithCounts(ctx, workapi.ReadyFilterFromIssueFilter(filter)) } else { - page, err = uw.IssueUseCase().SearchIssuesWithCounts(ctx, req.Query, filter) + page, err = uw.IssueUseCase().SearchIssuesWithCounts(ctx, "", filter) } if err != nil { return publicops.IssuePage{}, err @@ -133,57 +131,10 @@ func (r *issueReader) List(ctx context.Context, req publicops.ListRequest) (publ // between this implementation and its store-backed sibling, and it is // an argument to the shared function rather than a second copy of it. items, hasMore := workapi.FinishPageAt(page.Items, req.SortBy, req.Reverse, req.Offset, workapi.PageLimit(req), page.HasMore) - // The same shared hydration step the store-backed sibling runs, through - // the same function and after the same trim. This seam's detail source - // is the one that needs the wisp plane told to it, and the plane it is - // told is the TABLE THE ROW IS IN, resolved here in the page's own - // transaction — never the row's flags, which a durable row can carry - // wrongly (see workapi.CommentPlanes). - newComments := func() workapi.CommentStreamer { return workapi.NewUOWDetailSource(uw) } - planes := func(ctx context.Context, ids []string) (map[string]bool, error) { - return wispPlanesByResidence(ctx, uw, ids) - } - if err := workapi.HydrateListComments(ctx, newComments, planes, items, req.IncludeComments, !req.SkipCounts); err != nil { - return publicops.IssuePage{}, err - } return publicops.IssuePage{Items: items, HasMore: hasMore}, nil }) } -// wispPlanesByResidence reports which of ids live in the WISPS TABLE, which is -// what decides where their comments are. -// -// It is the page-sized form of the question workapi.GetIssueOrWisp answers one -// id at a time behind Reader.Get and behind commenter.AddComment: residence, -// not the row's Ephemeral/NoHistory flags, which `bd import` can leave pointing -// at the other plane (workapi.CommentPlanes says how, and what the listing -// answered before this resolved by table). GetWispsByIDs is the use-case call -// edgeReader.ReadEdges already makes to answer the same question, so this adds -// a caller rather than a mechanism, and one query serves the whole page. -// -// A MISSING WISPS TABLE MEANS NO WISPS, not a failed listing: the table is in -// the optional set every wisp query treats that way -// (sqlbuild.OptionalWispTable), the counts query that produced this page merged -// wisps under the same tolerance, and the store-backed seam's own router -// answers "durable" for it (DoltStore.wispExists). Erroring here would make -// `bd list --include-comments` the one read that cannot run on such a database. -func wispPlanesByResidence(ctx context.Context, uw UnitOfWork, ids []string) (map[string]bool, error) { - wisps, err := uw.IssueUseCase().GetWispsByIDs(ctx, ids) - if err != nil { - if name, ok := dberrors.MissingTableName(err); ok && sqlbuild.OptionalWispTable(name) { - return nil, nil - } - return nil, err - } - planes := make(map[string]bool, len(wisps)) - for _, wisp := range wisps { - if wisp != nil { - planes[wisp.ID] = true - } - } - return planes, nil -} - func (r *issueReader) Get(ctx context.Context, req publicops.GetRequest) (*publicops.IssueDetails, error) { return RunTxRead(ctx, r.provider, func(ctx context.Context, uw UnitOfWork) (*publicops.IssueDetails, error) { src := workapi.NewUOWDetailSource(uw) diff --git a/internal/storage/uow/lease_reclaimer.go b/internal/storage/uow/lease_reclaimer.go deleted file mode 100644 index 1cc522c6b..000000000 --- a/internal/storage/uow/lease_reclaimer.go +++ /dev/null @@ -1,60 +0,0 @@ -package uow - -import ( - "context" - "fmt" - - storageissueops "github.com/steveyegge/beads/internal/storage/issueops" - publicops "github.com/steveyegge/beads/issueops" -) - -// LeaseReclaimerSource is the capability accessor a unit-of-work provider -// offers for the lease-sweep role, the sibling of DeleterSource and -// ReleaserSource. -type LeaseReclaimerSource interface { - LeaseReclaimer() (publicops.LeaseReclaimer, error) -} - -// leaseReclaimer sweeps stale leases through a unit of work. -type leaseReclaimer struct { - provider UnitOfWorkProvider -} - -// LeaseReclaimer returns the guarded lease-sweep surface for this provider. -func (p *doltSQLProvider) LeaseReclaimer() (publicops.LeaseReclaimer, error) { - return NewLeaseReclaimer(p) -} - -// NewLeaseReclaimer constructs a public LeaseReclaimer backed by provider. -func NewLeaseReclaimer(provider UnitOfWorkProvider) (publicops.LeaseReclaimer, error) { - if isNilUnitOfWorkProvider(provider) { - return nil, fmt.Errorf("new lease reclaimer: unit-of-work provider must not be nil") - } - return &leaseReclaimer{provider: provider}, nil -} - -var _ publicops.LeaseReclaimer = (*leaseReclaimer)(nil) - -// Reclaim reaches domain.IssueUseCase.Reclaim inside ONE committing unit of -// work, composing the same conditional commit message the dolt and -// embedded-dolt legs use: a sweep that reverts nothing leaves no history -// entry describing a no-op. -// -// VALIDATION HAPPENS BEFORE THE UNIT OF WORK OPENS, as on those two legs and -// for Releaser's reason: a refusal then changes nothing on the connection as -// well as on the rows. The shared body validates again inside the transaction, -// which is what keeps a leg that skipped this from answering a different -// contract. -func (l *leaseReclaimer) Reclaim(ctx context.Context, request publicops.ReclaimRequest) (publicops.ReclaimResult, error) { - if err := storageissueops.ValidateReclaimRequest(request); err != nil { - return publicops.ReclaimResult{}, err - } - return RunTxResult(ctx, l.provider, func(ctx context.Context, uw UnitOfWork) (publicops.ReclaimResult, string, error) { - result, err := uw.IssueUseCase().Reclaim(ctx, request) - if err != nil { - return result, "", err - } - _, msg := storageissueops.ReclaimVersionCommit(result) - return result, msg, nil - }) -} diff --git a/internal/storage/uow/lease_reclaimer_contract_test.go b/internal/storage/uow/lease_reclaimer_contract_test.go deleted file mode 100644 index edcda454a..000000000 --- a/internal/storage/uow/lease_reclaimer_contract_test.go +++ /dev/null @@ -1,146 +0,0 @@ -package uow - -import ( - "context" - "errors" - "fmt" - "testing" - - "github.com/steveyegge/beads/backend/conformance" - publicops "github.com/steveyegge/beads/issueops" -) - -// TestLeaseReclaimerContract runs the LeaseReclaimer contract against the -// unit-of-work provider, which reaches internal/storage/issueops. -// ExecuteReclaimInTx through domain.IssueUseCase.Reclaim inside one committing -// unit of work. -func TestLeaseReclaimerContract(t *testing.T) { - ctx, cancel := context.WithCancel(t.Context()) - defer cancel() - fixture := newUOWLeaseReclaimerFixture(t, ctx, "lrc") - - t.Run("RevertsAStaleLease", func(t *testing.T) { - conformance.RunLeaseReclaimerRevertsAStaleLease(t, ctx, fixture) - }) - t.Run("MintsARevisionThatFencesTheFormerHolder", func(t *testing.T) { - conformance.RunLeaseReclaimerMintsARevisionThatFencesTheFormerHolder(t, ctx, fixture) - }) - t.Run("LeavesALiveLeaseAlone", func(t *testing.T) { - conformance.RunLeaseReclaimerLeavesALiveLeaseAlone(t, ctx, fixture) - }) - t.Run("HonorsTheGraceWindow", func(t *testing.T) { - conformance.RunLeaseReclaimerHonorsTheGraceWindow(t, ctx, fixture) - }) - t.Run("ScopesToTheNamedIDs", func(t *testing.T) { - conformance.RunLeaseReclaimerScopesToTheNamedIDs(t, ctx, fixture) - }) - t.Run("AnswersAnAbsentIDWithAnEmptyResult", func(t *testing.T) { - conformance.RunLeaseReclaimerAnswersAnAbsentIDWithAnEmptyResult(t, ctx, fixture) - }) - t.Run("ScopesToAssigneesAndLabels", func(t *testing.T) { - conformance.RunLeaseReclaimerScopesToAssigneesAndLabels(t, ctx, fixture) - }) - t.Run("AttributesTheRecoveryEvent", func(t *testing.T) { - conformance.RunLeaseReclaimerAttributesTheRecoveryEvent(t, ctx, fixture) - }) - t.Run("FiresOnUpdateOncePerReclaimedRow", func(t *testing.T) { - conformance.RunLeaseReclaimerFiresOnUpdateOncePerReclaimedRow(t, ctx, fixture) - }) - t.Run("RefusesAMalformedRequest", func(t *testing.T) { - conformance.RunLeaseReclaimerRefusesAMalformedRequest(t, ctx, fixture) - }) - t.Run("RefusesABlankScopeEntry", func(t *testing.T) { - conformance.RunLeaseReclaimerRefusesABlankScopeEntry(t, ctx, fixture) - }) - t.Run("AcceptsExactlyTheCap", func(t *testing.T) { - conformance.RunLeaseReclaimerAcceptsExactlyTheCap(t, ctx, fixture) - }) - t.Run("DoesNotMutateTheCallerRequest", func(t *testing.T) { - conformance.RunLeaseReclaimerDoesNotMutateTheCallerRequest(t, ctx, fixture) - }) -} - -// TestLeaseReclaimerRefusesBeforeOpeningAUOW: a malformed sweep is a -// deterministic validation failure and must not cost a database connection to -// discover, as on the dolt and embedded-dolt legs. The provider fails every -// open, so a refusal that came from inside a unit of work would be counted. -func TestLeaseReclaimerRefusesBeforeOpeningAUOW(t *testing.T) { - for _, tc := range []struct { - name string - request publicops.ReclaimRequest - }{ - {"no actor", publicops.ReclaimRequest{}}, - {"a negative grace window", publicops.ReclaimRequest{Actor: "reaper", OlderThan: -1}}, - {"a blank id", publicops.ReclaimRequest{Actor: "reaper", Filter: publicops.ReclaimFilter{IDs: []string{""}}}}, - } { - t.Run(tc.name, func(t *testing.T) { - provider := &mockUnitOfWorkProvider{newUOWErr: errors.New("unexpected unit-of-work open")} - reclaimer, err := NewLeaseReclaimer(provider) - if err != nil { - t.Fatalf("NewLeaseReclaimer: %v", err) - } - if _, err := reclaimer.Reclaim(context.Background(), tc.request); !errors.Is(err, publicops.ErrValidation) { - t.Fatalf("err = %v, want ErrValidation", err) - } - if provider.newUOWCalls != 0 { - t.Errorf("an invalid request opened %d units of work", provider.newUOWCalls) - } - }) - } -} - -// newUOWLeaseReclaimerFixture takes every role through the provider's own -// capability accessors, and the hooked reclaimer off the notifying provider -// the proxied CLI route wraps around its provider. -func newUOWLeaseReclaimerFixture(t *testing.T, ctx context.Context, prefix string) conformance.LeaseReclaimerFixture { - t.Helper() - provider := newUOWRoleFixtureProvider(t, ctx, prefix) - source, ok := provider.(LeaseReclaimerSource) - if !ok { - t.Fatalf("provider %T does not offer the LeaseReclaimer accessor", provider) - } - reclaimer, err := source.LeaseReclaimer() - if err != nil { - t.Fatalf("LeaseReclaimer(): %v", err) - } - claimer, err := provider.(IssueClaimerSource).IssueClaimer() - if err != nil { - t.Fatalf("IssueClaimer(): %v", err) - } - lifecycle, err := provider.(IssueLifecycleSource).IssueLifecycle() - if err != nil { - t.Fatalf("IssueLifecycle(): %v", err) - } - runner, fired := conformance.NewUpdateHookLog(t) - hooked := reclaimer - if runner != nil { - notifying, ok := NewNotifyingProvider(provider, Sinks{Hook: runner}).(LeaseReclaimerSource) - if !ok { - t.Fatal("the notifying provider does not offer the LeaseReclaimer accessor") - } - if hooked, err = notifying.LeaseReclaimer(); err != nil { - t.Fatalf("hooked LeaseReclaimer(): %v", err) - } - } - kit := newUOWRoleFixtureKit(provider, prefix) - return conformance.LeaseReclaimerFixture{ - IssuePrefix: kit.IssuePrefix, - LeaseReclaimer: reclaimer, - CreateIssue: kit.CreateIssue, - Claimer: claimer, - Lifecycle: lifecycle, - QueryScalar: kit.QueryScalar, - Exec: func(ctx context.Context, statements []conformance.SQLStatement) error { - return RunTx(ctx, provider, func(ctx context.Context, uw UnitOfWork) (string, error) { - for _, stmt := range statements { - if _, err := uw.RawSQLUseCase().Exec(ctx, stmt.Query, stmt.Args...); err != nil { - return "", fmt.Errorf("%s: %w", stmt.Query, err) - } - } - return "seed lease reclaimer rows", nil - }) - }, - HookedLeaseReclaimer: hooked, - FiredUpdates: fired, - } -} diff --git a/internal/storage/uow/lifecycle_close_reopen_contract_test.go b/internal/storage/uow/lifecycle_close_reopen_contract_test.go index 9a77a7f95..0282aa3d2 100644 --- a/internal/storage/uow/lifecycle_close_reopen_contract_test.go +++ b/internal/storage/uow/lifecycle_close_reopen_contract_test.go @@ -63,9 +63,6 @@ func TestLifecycleCloseReopenContract(t *testing.T) { t.Run("ResultsAreHydratedPostStateSnapshots", func(t *testing.T) { conformance.RunLifecycleResultsAreHydratedPostStateSnapshots(t, ctx, fixture) }) - t.Run("ResultsCarryThePostWriteRowVersion", func(t *testing.T) { - conformance.RunLifecycleResultsCarryThePostWriteRowVersion(t, ctx, fixture) - }) t.Run("CloseAndReopenRequireActorAndIssueID", func(t *testing.T) { conformance.RunLifecycleCloseAndReopenRequireActorAndIssueID(t, ctx, fixture) }) @@ -81,9 +78,6 @@ func TestLifecycleCloseReopenContract(t *testing.T) { t.Run("ReopenReblocksItsDependers", func(t *testing.T) { conformance.RunLifecycleReopenReblocksItsDependers(t, ctx, fixture) }) - t.Run("CloseEnforcesTheCloseGuards", func(t *testing.T) { - conformance.RunLifecycleCloseEnforcesTheCloseGuards(t, ctx, fixture) - }) } func newUOWLifecycleCloseReopenFixture(t *testing.T, ctx context.Context, prefix string) conformance.LifecycleCloseReopenFixture { diff --git a/internal/storage/uow/lifecycle_create_contract_test.go b/internal/storage/uow/lifecycle_create_contract_test.go index c1028dcfd..19e1d16b5 100644 --- a/internal/storage/uow/lifecycle_create_contract_test.go +++ b/internal/storage/uow/lifecycle_create_contract_test.go @@ -40,15 +40,6 @@ func TestLifecycleCreateContract(t *testing.T) { t.Run("EchoesSubSecondTimestamps", func(t *testing.T) { conformance.RunLifecycleCreateEchoesSubSecondTimestamps(t, ctx, fixture) }) - t.Run("AppliesTheDefaultPriority", func(t *testing.T) { - conformance.RunLifecycleCreateAppliesTheDefaultPriority(t, ctx, fixture) - }) - t.Run("WritesTheGateMembers", func(t *testing.T) { - conformance.RunLifecycleCreateWritesTheGateMembers(t, ctx, fixture) - }) - t.Run("BoundsTheGateMembers", func(t *testing.T) { - conformance.RunLifecycleCreateBoundsTheGateMembers(t, ctx, fixture) - }) } func newUOWLifecycleCreateFixture(t *testing.T, ctx context.Context, prefix string) conformance.LifecycleCreateFixture { diff --git a/internal/storage/uow/lifecycle_update_contract_test.go b/internal/storage/uow/lifecycle_update_contract_test.go index 1861abb57..e635e5fbd 100644 --- a/internal/storage/uow/lifecycle_update_contract_test.go +++ b/internal/storage/uow/lifecycle_update_contract_test.go @@ -53,18 +53,6 @@ func TestLifecycleUpdateContract(t *testing.T) { t.Run("ConditionalGuardsGateOrdinaryEdits", func(t *testing.T) { conformance.RunLifecycleUpdateConditionalGuardsGateOrdinaryEdits(t, ctx, fixture) }) - t.Run("ExpectedVersionSingleWinnerUnderConcurrency", func(t *testing.T) { - conformance.RunLifecycleUpdateExpectedVersionSingleWinnerUnderConcurrency(t, ctx, fixture) - }) - t.Run("ExpectedVersionSingleWinnerWithDisjointColumnsUnderConcurrency", func(t *testing.T) { - conformance.RunLifecycleUpdateExpectedVersionSingleWinnerWithDisjointColumnsUnderConcurrency(t, ctx, fixture) - }) - t.Run("CloseExpectedVersionSingleWinnerUnderConcurrency", func(t *testing.T) { - conformance.RunLifecycleCloseExpectedVersionSingleWinnerUnderConcurrency(t, ctx, fixture) - }) - t.Run("ExpectedVersionSingleWinnerAcrossUpdateAndCloseUnderConcurrency", func(t *testing.T) { - conformance.RunLifecycleUpdateExpectedVersionSingleWinnerAcrossUpdateAndCloseUnderConcurrency(t, ctx, fixture) - }) t.Run("ConditionalGuardAcceptsRespelledAssignee", func(t *testing.T) { conformance.RunLifecycleUpdateConditionalGuardAcceptsRespelledAssignee(t, ctx, fixture) }) @@ -89,27 +77,9 @@ func TestLifecycleUpdateContract(t *testing.T) { t.Run("PersistentPreservesUnversionedClass", func(t *testing.T) { conformance.RunLifecycleUpdatePersistentPreservesUnversionedClass(t, ctx, fixture) }) - t.Run("WispModesRefuseAnUnversionedRow", func(t *testing.T) { - conformance.RunLifecycleUpdateWispModesRefuseAnUnversionedRow(t, ctx, fixture) - }) - t.Run("PersistentPromotesAWisp", func(t *testing.T) { - conformance.RunLifecycleUpdatePersistentPromotesAWisp(t, ctx, fixture) - }) t.Run("ProvenanceLabelsHistory", func(t *testing.T) { conformance.RunLifecycleUpdateProvenanceLabelsHistory(t, ctx, fixture) }) - t.Run("RefusesATemplate", func(t *testing.T) { - conformance.RunLifecycleUpdateRefusesATemplate(t, ctx, fixture) - }) - t.Run("AllowTemplateEditsATemplate", func(t *testing.T) { - conformance.RunLifecycleUpdateAllowTemplateEditsATemplate(t, ctx, fixture) - }) - t.Run("PatchesTheGateMembers", func(t *testing.T) { - conformance.RunLifecycleUpdatePatchesTheGateMembers(t, ctx, fixture) - }) - t.Run("BoundsTheGateMembers", func(t *testing.T) { - conformance.RunLifecycleUpdateBoundsTheGateMembers(t, ctx, fixture) - }) } func newUOWLifecycleUpdateFixture(t *testing.T, ctx context.Context, prefix string) conformance.LifecycleUpdateFixture { diff --git a/internal/storage/uow/list_comments_plane_test.go b/internal/storage/uow/list_comments_plane_test.go deleted file mode 100644 index f05bb0d16..000000000 --- a/internal/storage/uow/list_comments_plane_test.go +++ /dev/null @@ -1,251 +0,0 @@ -package uow - -import ( - "context" - "errors" - "fmt" - "slices" - "testing" - - mysql "github.com/go-sql-driver/mysql" - - "github.com/steveyegge/beads/internal/storage" - "github.com/steveyegge/beads/internal/storage/domain" - "github.com/steveyegge/beads/internal/types" - publicops "github.com/steveyegge/beads/issueops" -) - -// The unit-of-work seam is the one whose comment source has to be TOLD which -// table to read — the store-backed sibling's source routes an id itself — so it -// is the one where a wrong answer is a silent wrong-table read. These tests -// drive the real Reader.List with --include-comments and assert the comment -// BODIES arrive, because the failure they cover produced an empty comments array -// beside a nonzero comment_count rather than an error or a visibly wrong row. -// -// The rows are the two shapes where a row's flags and its table disagree, and -// both are states the tree maintains on purpose: `bd import` pins a no_history -// record to the durable table while preserving the flag (clearing it would -// change the content hash and break export→import→export byte-identity), and -// pins a wisp_plane record to the wisps table without setting either flag. See -// workapi.CommentPlanes. - -// planeIssues serves one page and answers the residence question from the -// wisps-table membership the test declares, which is what -// IssueUseCase.GetWispsByIDs answers from a real database. -type planeIssues struct { - domain.IssueUseCase - rows []*types.IssueWithCounts - inWisps map[string]*types.Issue - wispErr error - asked [][]string -} - -func (f *planeIssues) SearchIssuesWithCounts(context.Context, string, types.IssueFilter) (domain.SearchCountsPage, error) { - return domain.SearchCountsPage{Items: f.rows}, nil -} - -func (f *planeIssues) GetWispsByIDs(_ context.Context, ids []string) ([]*types.Issue, error) { - f.asked = append(f.asked, ids) - if f.wispErr != nil { - return nil, f.wispErr - } - out := make([]*types.Issue, 0, len(ids)) - for _, id := range ids { - if wisp, ok := f.inWisps[id]; ok { - out = append(out, wisp) - } - } - return out, nil -} - -// planeComments keeps each plane's comments in its own map, the way the two -// comment tables do, so a read of the wrong one comes back empty here exactly as -// it does against a database. -type planeComments struct { - domain.CommentUseCase - issueComments map[string][]*types.Comment - wispComments map[string][]*types.Comment -} - -func (f planeComments) IterCommentsForIssue(_ context.Context, id string) (storage.Iter[types.Comment], error) { - return storage.NewSliceIter(f.issueComments[id]), nil -} - -func (f planeComments) IterCommentsForWisp(_ context.Context, id string) (storage.Iter[types.Comment], error) { - return storage.NewSliceIter(f.wispComments[id]), nil -} - -func planeReader(t *testing.T, issues *planeIssues, comments domain.CommentUseCase) publicops.Reader { - t.Helper() - uw := &mockUnitOfWork{issueUseCase: issues, configUseCase: readerConfig{}, commentUseCase: comments} - reader, err := NewIssueReader(&mockUnitOfWorkProvider{uows: []*mockUnitOfWork{uw}}) - if err != nil { - t.Fatalf("NewIssueReader: %v", err) - } - return reader -} - -func commentTexts(item *types.IssueWithCounts) []string { - out := make([]string, 0, len(item.Comments)) - for _, c := range item.Comments { - out = append(out, c.Text) - } - return out -} - -// TestListHydratesCommentsFromTheRowsTableNotItsFlags is the seam-level -// regression for the row class this reader used to answer with content-free -// rows: a durable bead that still carries no_history, listed with -// --include-comments, whose comments `bd comment` wrote to the ISSUES table and -// `bd show` reads from there. Routing by the flag sent the list read to -// wisp_comments, where the row has nothing, and the page came back asserting one -// comment, carrying none, and flagging nothing. -func TestListHydratesCommentsFromTheRowsTableNotItsFlags(t *testing.T) { - issues := &planeIssues{ - rows: []*types.IssueWithCounts{ - {Issue: &types.Issue{ID: "bd-1", Title: "import-pinned", NoHistory: true}, CommentCount: 1}, - }, - inWisps: map[string]*types.Issue{}, - } - comments := planeComments{ - issueComments: map[string][]*types.Comment{"bd-1": {{ID: "c1", Text: "zzzuniquephrase"}}}, - // Nothing on the wisp plane: the table the flag points at is empty for - // this id, which is why the defect was silent rather than wrong. - wispComments: map[string][]*types.Comment{}, - } - reader := planeReader(t, issues, comments) - - page, err := reader.List(context.Background(), publicops.ListRequest{IncludeComments: true}) - if err != nil { - t.Fatalf("List: %v", err) - } - - if len(page.Items) != 1 { - t.Fatalf("page has %d rows, want 1", len(page.Items)) - } - if got := commentTexts(page.Items[0]); !slices.Equal(got, []string{"zzzuniquephrase"}) { - t.Errorf("comments = %v, want [zzzuniquephrase]: the row lives in the issues table, whatever its no_history flag says", got) - } - if page.Items[0].CommentsOmitted != nil { - t.Errorf("CommentsOmitted = %v on a hydrated row, want unset", *page.Items[0].CommentsOmitted) - } - if len(issues.asked) != 1 || !slices.Equal(issues.asked[0], []string{"bd-1"}) { - t.Errorf("resolved residence with %v, want one lookup for [bd-1]: the plane is a query, not a guess", issues.asked) - } -} - -// TestListHydratesCommentsForAWispTableRowWithNoFlags is the mirror, and the one -// no flag-based rule can get right either: a row the import pinned to the WISPS -// table with neither plane flag set. Its comments are in wisp_comments and the -// flags point at the durable table. -func TestListHydratesCommentsForAWispTableRowWithNoFlags(t *testing.T) { - issues := &planeIssues{ - rows: []*types.IssueWithCounts{ - {Issue: &types.Issue{ID: "bd-2", Title: "plane-pinned"}, CommentCount: 1}, - }, - inWisps: map[string]*types.Issue{"bd-2": {ID: "bd-2"}}, - } - comments := planeComments{ - issueComments: map[string][]*types.Comment{}, - wispComments: map[string][]*types.Comment{"bd-2": {{ID: "c1", Text: "zzzuniquephrase"}}}, - } - reader := planeReader(t, issues, comments) - - page, err := reader.List(context.Background(), publicops.ListRequest{IncludeComments: true}) - if err != nil { - t.Fatalf("List: %v", err) - } - - if len(page.Items) != 1 { - t.Fatalf("page has %d rows, want 1", len(page.Items)) - } - if got := commentTexts(page.Items[0]); !slices.Equal(got, []string{"zzzuniquephrase"}) { - t.Errorf("comments = %v, want [zzzuniquephrase]: the row is in the wisps table, whatever its flags say", got) - } -} - -// TestListResolvesNoPlanesWithoutIncludeComments pins that the default listing — -// which is every `bd list` that does not pass the flag — pays nothing for the -// routing query. The marker path routes no read, so it asks no table where -// anything lives. -func TestListResolvesNoPlanesWithoutIncludeComments(t *testing.T) { - issues := &planeIssues{ - rows: []*types.IssueWithCounts{ - {Issue: &types.Issue{ID: "bd-1", NoHistory: true}, CommentCount: 1}, - }, - inWisps: map[string]*types.Issue{}, - } - reader := planeReader(t, issues, planeComments{}) - - page, err := reader.List(context.Background(), publicops.ListRequest{}) - if err != nil { - t.Fatalf("List: %v", err) - } - - if len(issues.asked) != 0 { - t.Errorf("resolved residence %d times on a listing that asked for no comments, want 0", len(issues.asked)) - } - if page.Items[0].CommentsOmitted == nil || !*page.Items[0].CommentsOmitted { - t.Errorf("CommentsOmitted = %v, want true: the default listing still says the text is missing", page.Items[0].CommentsOmitted) - } -} - -// TestListToleratesAMissingWispsTable keeps `bd list --include-comments` working -// on a database that has no wisps table at all. That table is in the optional set -// every wisp query treats as "no wisps" (sqlbuild.OptionalWispTable) — the counts -// query that produced the page merged wisps under the same tolerance, and the -// store-backed seam's router answers "durable" for it — so a listing must not be -// the one read that cannot run there. -// -// The fixture error is the one this seam receives: a missing table arrives from -// the server as MySQL 1146, and the table it names is what -// dberrors.MissingTableName reads. Classifying it through those two shared -// helpers rather than a local string check is what keeps this tolerance the SAME -// tolerance the counts query already applies. -func TestListToleratesAMissingWispsTable(t *testing.T) { - issues := &planeIssues{ - rows: []*types.IssueWithCounts{ - {Issue: &types.Issue{ID: "bd-1", NoHistory: true}, CommentCount: 1}, - }, - wispErr: fmt.Errorf("getByIDs: %w", &mysql.MySQLError{Number: 1146, Message: "table not found: wisps"}), - } - comments := planeComments{ - issueComments: map[string][]*types.Comment{"bd-1": {{ID: "c1", Text: "zzzuniquephrase"}}}, - } - reader := planeReader(t, issues, comments) - - page, err := reader.List(context.Background(), publicops.ListRequest{IncludeComments: true}) - if err != nil { - t.Fatalf("List over a database with no wisps table: %v", err) - } - if got := commentTexts(page.Items[0]); !slices.Equal(got, []string{"zzzuniquephrase"}) { - t.Errorf("comments = %v, want [zzzuniquephrase]: with no wisps table, every row is durable", got) - } -} - -// TestListFailsWhenResidenceCannotBeRead is the other half of that tolerance: -// any OTHER failure of the routing query fails the call. Hydration promises the -// bodies or an error, and a page that cannot find out where its rows live can -// only keep that promise by saying so — falling back to a plane would answer with -// the wrong table's contents, which is invisible in the response. -func TestListFailsWhenResidenceCannotBeRead(t *testing.T) { - sentinel := errors.New("connection reset") - issues := &planeIssues{ - rows: []*types.IssueWithCounts{ - {Issue: &types.Issue{ID: "bd-1"}, CommentCount: 1}, - }, - wispErr: sentinel, - } - comments := planeComments{ - issueComments: map[string][]*types.Comment{"bd-1": {{ID: "c1", Text: "zzzuniquephrase"}}}, - } - reader := planeReader(t, issues, comments) - - _, err := reader.List(context.Background(), publicops.ListRequest{IncludeComments: true}) - if err == nil { - t.Fatal("List returned nil when residence could not be read, want an error") - } - if !errors.Is(err, sentinel) { - t.Errorf("error = %v, want it to wrap %v", err, sentinel) - } -} diff --git a/internal/storage/uow/lostupdate_dolt_test.go b/internal/storage/uow/lostupdate_dolt_test.go index 9bf15d1ef..1f1a3c393 100644 --- a/internal/storage/uow/lostupdate_dolt_test.go +++ b/internal/storage/uow/lostupdate_dolt_test.go @@ -71,7 +71,11 @@ func TestUOW_ConcurrentMergeOps_NoLostUpdate(t *testing.T) { storeRootDir := t.TempDir() shutdownOnInterrupt(t, storeRootDir) - verifiedShutdownCleanup(t, storeRootDir) + t.Cleanup(func() { + if err := proxy.Shutdown(storeRootDir); err != nil { + t.Logf("proxy.Shutdown(%s): %v", storeRootDir, err) + } + }) logPath := filepath.Join(t.TempDir(), "server.log") ctx, cancel := context.WithTimeout(context.Background(), 180*time.Second) diff --git a/internal/storage/uow/memories.go b/internal/storage/uow/memories.go index 3b8120468..fc5040881 100644 --- a/internal/storage/uow/memories.go +++ b/internal/storage/uow/memories.go @@ -88,16 +88,11 @@ func (m *memories) Recall(ctx context.Context, req memoryops.RecallRequest) (mem return memoryops.RecallResult{}, err } return RunTxRead(ctx, m.provider, func(ctx context.Context, uw UnitOfWork) (memoryops.RecallResult, error) { - // The same whole-table probe as Remember, for the reason given there: - // Found is about the ROW, GetConfig answers "" for a missing row and an - // empty one alike, and the map's key set is the only place the - // difference survives. It is one query on a table of tens of rows. - all, err := uw.ConfigUseCase().GetAllConfig(ctx) + value, err := uw.ConfigUseCase().GetConfig(ctx, storagememoryops.StorageKey(key)) if err != nil { return memoryops.RecallResult{}, err } - value, found := all[storagememoryops.StorageKey(key)] - return memoryops.RecallResult{Key: key, Value: value, Found: found}, nil + return memoryops.RecallResult{Key: key, Value: value, Found: value != ""}, nil }) } @@ -116,14 +111,11 @@ func (m *memories) Forget(ctx context.Context, req memoryops.ForgetRequest) (mem storageKey := storagememoryops.StorageKey(key) result, err := RunTxResult(ctx, m.provider, func(ctx context.Context, uw UnitOfWork) (memoryops.ForgetResult, string, error) { cfg := uw.ConfigUseCase() - // The row probe Remember and Recall use: gating on GetConfig's "" is - // what left a memory stored empty unforgettable. - all, err := cfg.GetAllConfig(ctx) + previous, err := cfg.GetConfig(ctx, storageKey) if err != nil { return memoryops.ForgetResult{}, "", err } - previous, found := all[storageKey] - if !found { + if previous == "" { return memoryops.ForgetResult{Key: key}, "", nil } if err := cfg.DeleteConfig(ctx, storageKey); err != nil { diff --git a/internal/storage/uow/memories_contract_test.go b/internal/storage/uow/memories_contract_test.go index 85865a199..9725f306a 100644 --- a/internal/storage/uow/memories_contract_test.go +++ b/internal/storage/uow/memories_contract_test.go @@ -56,8 +56,8 @@ func TestMemoriesContract(t *testing.T) { t.Run("RecallReportsAMissAsNotFoundNotAnError", func(t *testing.T) { conformance.RunMemoriesRecallReportsAMissAsNotFoundNotAnError(t, ctx, fixture) }) - t.Run("EmptyRowIsPresentUntilForgotten", func(t *testing.T) { - conformance.RunMemoriesEmptyRowIsPresentUntilForgotten(t, ctx, fixture) + t.Run("RecallConflatesStoredEmptyWithAbsent", func(t *testing.T) { + conformance.RunMemoriesRecallConflatesStoredEmptyWithAbsent(t, ctx, fixture) }) t.Run("ForgetRemovesExactlyTheNamedRow", func(t *testing.T) { conformance.RunMemoriesForgetRemovesExactlyTheNamedRow(t, ctx, fixture) diff --git a/internal/storage/uow/molecule.go b/internal/storage/uow/molecule.go deleted file mode 100644 index 859130e5b..000000000 --- a/internal/storage/uow/molecule.go +++ /dev/null @@ -1,182 +0,0 @@ -package uow - -import ( - "context" - "fmt" - "strconv" - - "github.com/steveyegge/beads/internal/storage/domain" - storageissueops "github.com/steveyegge/beads/internal/storage/issueops" - publicops "github.com/steveyegge/beads/issueops" -) - -// MoleculeReaderInUOW binds the molecule rules (issueops.MoleculeReader) to a -// caller's unit of work: the SAME BatchGetter, Relations and EdgeReader bodies -// this backend's roles run (GetManyInUOW, RelatedInUOW, ReadEdgesInUOW), -// against uw, so a close that decides whether it completed a molecule reads -// the state its own writes produced. It is this backend's twin of -// internal/storage/issueops.TxMoleculeReader. -func MoleculeReaderInUOW(uw UnitOfWork) publicops.MoleculeReader { - return publicops.MoleculeReaderOver(uowMoleculeReader{uw: uw}) -} - -type uowMoleculeReader struct{ uw UnitOfWork } - -func (r uowMoleculeReader) GetMany(ctx context.Context, request publicops.GetManyRequest) (publicops.GetManyResult, error) { - return GetManyInUOW(ctx, r.uw, request) -} - -func (r uowMoleculeReader) Related(ctx context.Context, request publicops.RelatedRequest) ([]*publicops.RelatedIssue, error) { - if err := storageissueops.ValidateRelatedRequest(request); err != nil { - return nil, err - } - return RelatedInUOW(ctx, r.uw, request) -} - -func (r uowMoleculeReader) ReadEdges(ctx context.Context, request publicops.EdgeReadRequest) (publicops.EdgeReadResult, error) { - return ReadEdgesInUOW(ctx, r.uw, request) -} - -// CloseCompletedMoleculeInUOW is CloseRequest.AutoCloseMolecule's body for -// this backend: CloseCompletedMoleculesInUOW for one step. -func CloseCompletedMoleculeInUOW(ctx context.Context, uw UnitOfWork, stepID, actor, session string) (storageissueops.MoleculeAutoClose, error) { - autos, err := CloseCompletedMoleculesInUOW(ctx, uw, []string{stepID}, actor, session) - if err != nil { - return storageissueops.MoleculeAutoClose{}, err - } - return autos[0], nil -} - -// CloseCompletedMoleculesInUOW is CloseBatchRequest.AutoCloseMolecule's body -// for this backend, the twin of storageissueops.CloseCompletedMoleculesInTx: -// for every auto-closing molecule root a close of stepIDs could have completed -// (issueops.AutoClosingMoleculeRoots read in uw), it rewrites the root's -// coordination cell (storageissueops.MoleculeCoordinationKey, so two units of -// work deciding the same root conflict at commit and the retry reads the -// other's close; the cost, serialized sibling closes, is -// storageissueops.TouchMoleculeCoordinationInTx's), and when every step under -// the root is now closed (issueops.MoleculeStepsClosed) it closes the root in -// uw — unforced, guarded on the root revision read in the same unit of work, -// recording issueops.MoleculeAutoCloseReason and session. -// -// It answers one MoleculeAutoClose per step id, in order: each root is decided -// once and reported on the first step under it. A refusal of the root -// (issueops.IsMoleculeAutoCloseRefusal) is answered as the Refusal and leaves -// the root untouched; the caller's own closes stand. Any other failure is -// returned and fails the caller's unit of work. -func CloseCompletedMoleculesInUOW(ctx context.Context, uw UnitOfWork, stepIDs []string, actor, session string) ([]storageissueops.MoleculeAutoClose, error) { - reader := MoleculeReaderInUOW(uw) - roots, err := publicops.AutoClosingMoleculeRoots(ctx, reader, stepIDs) - if err != nil { - return nil, err - } - autos := make([]storageissueops.MoleculeAutoClose, len(stepIDs)) - decided := make(map[string]bool, len(roots)) - for i, stepID := range stepIDs { - root := roots[stepID] - if root == nil || decided[root.ID] { - continue - } - decided[root.ID] = true - auto, err := closeCompletedMoleculeRootInUOW(ctx, uw, reader, root, actor, session) - if err != nil { - return nil, err - } - autos[i] = auto - } - return autos, nil -} - -func closeCompletedMoleculeRootInUOW(ctx context.Context, uw UnitOfWork, reader publicops.MoleculeReader, root *publicops.Issue, actor, session string) (storageissueops.MoleculeAutoClose, error) { - // storageissueops.TouchMoleculeCoordinationInTx, through this unit of work. - touch := strconv.FormatInt(storageissueops.FreshRowLock(), 10) - if err := uw.ConfigUseCase().SetLocalMetadata(ctx, storageissueops.MoleculeCoordinationKey(root.ID), touch); err != nil { - return storageissueops.MoleculeAutoClose{}, fmt.Errorf("touch molecule coordination for %s: %w", root.ID, err) - } - complete, err := publicops.MoleculeStepsClosed(ctx, reader, root.ID) - if err != nil || !complete { - return storageissueops.MoleculeAutoClose{}, err - } - req := publicops.MoleculeRootClose(root, actor, session) - // A probe, not a write: an UpdateSpec with no Fields applies nothing. It - // only checks the root's revision against req.ExpectedVersion, the guard - // the close below does not take itself. - if _, err := uw.IssueUseCase().ApplyUpdate(ctx, req.IssueID, domain.UpdateSpec{ExpectedVersion: req.ExpectedVersion}, req.Actor); err != nil { - if publicops.IsMoleculeAutoCloseRefusal(err) { - return storageissueops.RefusedMoleculeAutoClose(err), nil - } - return storageissueops.MoleculeAutoClose{}, fmt.Errorf("auto-closing molecule %s: %w", root.ID, err) - } - params := domain.CloseIssueParams{Reason: req.Reason, Session: req.Session} - var closed domain.CloseIssueResult - if storageissueops.IsWisp(root) { - closed, err = uw.IssueUseCase().CloseWispChecked(ctx, req.IssueID, params, req.Actor, req.Force) - } else { - closed, err = uw.IssueUseCase().CloseIssueChecked(ctx, req.IssueID, params, req.Actor, req.Force) - } - if publicops.IsMoleculeAutoCloseRefusal(err) { - return storageissueops.RefusedMoleculeAutoClose(err), nil - } - if err != nil { - return storageissueops.MoleculeAutoClose{}, fmt.Errorf("auto-closing molecule %s: %w", root.ID, err) - } - if !closed.Closed { - return storageissueops.MoleculeAutoClose{}, nil - } - current := root - if closed.Issue != nil { - current = closed.Issue - } - hydrated, err := hydrateIssueOperation(ctx, uw, current, false, false) - if err != nil { - return storageissueops.MoleculeAutoClose{}, err - } - return storageissueops.MoleculeAutoClose{Root: hydrated}, nil -} - -// MoleculeStepperSource is the capability accessor a unit-of-work provider -// offers for the advance-a-molecule role. -type MoleculeStepperSource interface { - MoleculeStepper() (publicops.MoleculeStepper, error) -} - -// MoleculeStepper returns the advance-a-molecule surface for this provider. -func (p *doltSQLProvider) MoleculeStepper() (publicops.MoleculeStepper, error) { - return NewMoleculeStepper(p) -} - -// NewMoleculeStepper is issueops.NewMoleculeStepper composed over the roles -// THIS provider builds (its batch getter, relations, edge reader, claimer and -// lifecycle), so a wrapper that builds its roles over itself — the notifying -// provider, a policy decorator, the server's timed provider — gets an advance -// whose every read and claim goes through its own layer. -func NewMoleculeStepper(provider UnitOfWorkProvider) (publicops.MoleculeStepper, error) { - if isNilUnitOfWorkProvider(provider) { - return nil, fmt.Errorf("new molecule stepper: unit-of-work provider must not be nil") - } - getter, err := NewBatchGetter(provider) - if err != nil { - return nil, err - } - relations, err := NewIssueRelations(provider) - if err != nil { - return nil, err - } - edges, err := NewEdgeReader(provider) - if err != nil { - return nil, err - } - claimer, err := NewIssueClaimer(provider) - if err != nil { - return nil, err - } - lifecycle, err := NewIssueOperations(provider) - if err != nil { - return nil, err - } - return publicops.NewMoleculeStepper(publicops.MoleculeStepperRoles{ - Reader: publicops.MoleculeReader{BatchGetter: getter, Relations: relations, EdgeReader: edges}, - Claimer: claimer, - Lifecycle: lifecycle, - }), nil -} diff --git a/internal/storage/uow/molecule_contract_test.go b/internal/storage/uow/molecule_contract_test.go deleted file mode 100644 index a4ea4c1ff..000000000 --- a/internal/storage/uow/molecule_contract_test.go +++ /dev/null @@ -1,257 +0,0 @@ -package uow - -import ( - "context" - "fmt" - "sync" - "testing" - - "github.com/steveyegge/beads/backend/conformance" - "github.com/steveyegge/beads/internal/storage/domain" - "github.com/steveyegge/beads/internal/types" - publicops "github.com/steveyegge/beads/issueops" -) - -// TestMoleculeContract runs the molecule contract against the unit-of-work -// backend, whose auto-close body (CloseCompletedMoleculeInUOW) is its own and -// whose stepper is composed over this provider's roles. One provider for the -// suite; sequential subtests namespaced by prefix. -func TestMoleculeContract(t *testing.T) { - ctx := context.Background() - provider := newUOWRoleFixtureProvider(t, ctx, "mol") - fixture := newUOWMoleculeFixture(t, provider, "mol") - - for _, test := range []struct { - name string - run func(*testing.T, context.Context, conformance.MoleculeFixture) - }{ - {"CloseAutoClosesTheCompletedRoot", conformance.RunMoleculeCloseAutoClosesTheCompletedRoot}, - {"AutoCloseIsOptInAndRuleBound", conformance.RunMoleculeAutoCloseIsOptInAndRuleBound}, - {"RecloseHealsAStrandedRoot", conformance.RunMoleculeRecloseHealsAStrandedRoot}, - {"BatchCloseAutoClosesTheRootOnce", conformance.RunMoleculeBatchCloseAutoClosesTheRootOnce}, - {"RefusedRootKeepsTheStepClose", conformance.RunMoleculeRefusedRootKeepsTheStepClose}, - {"GuardedRootKeepsTheStepClose", conformance.RunMoleculeGuardedRootKeepsTheStepClose}, - {"StepperClaimsTheNextReadyStepForTheActor", conformance.RunMoleculeStepperClaimsTheNextReadyStepForTheActor}, - {"StepperNeverTakesAStepAnotherActorHolds", conformance.RunMoleculeStepperNeverTakesAStepAnotherActorHolds}, - {"StepperAnswersCompleteAndOutsideAMolecule", conformance.RunMoleculeStepperAnswersCompleteAndOutsideAMolecule}, - {"StepperClaimsAWispStep", conformance.RunMoleculeStepperClaimsAWispStep}, - } { - t.Run(test.name, func(t *testing.T) { test.run(t, ctx, fixture) }) - } -} - -func newUOWMoleculeFixture(t *testing.T, provider UnitOfWorkProvider, prefix string) conformance.MoleculeFixture { - t.Helper() - lifecycle, err := NewIssueOperations(provider) - if err != nil { - t.Fatalf("NewIssueOperations: %v", err) - } - closer, err := NewBatchCloser(provider) - if err != nil { - t.Fatalf("NewBatchCloser: %v", err) - } - source, ok := provider.(MoleculeStepperSource) - if !ok { - t.Fatalf("provider %T does not offer the MoleculeStepper accessor", provider) - } - stepper, err := source.MoleculeStepper() - if err != nil { - t.Fatalf("MoleculeStepper(): %v", err) - } - kit := newUOWRoleFixtureKit(provider, prefix) - return conformance.MoleculeFixture{ - IssuePrefix: kit.IssuePrefix, - Lifecycle: lifecycle, - BatchCloser: closer, - Stepper: stepper, - CreateIssue: kit.CreateIssue, - CreateWisp: kit.CreateWisp, - AddDependency: kit.AddDependency, - QueryScalar: kit.QueryScalar, - } -} - -// seedRacingMoleculeInUOW seeds an auto-closing molecule root with n open -// steps through kit and answers the steps. -func seedRacingMoleculeInUOW(t *testing.T, ctx context.Context, kit roleFixtureKit, root string, n int) []string { - t.Helper() - if err := kit.CreateIssue(ctx, &types.Issue{ID: root, Title: "molecule " + root, IssueType: types.TypeMolecule, Status: types.StatusOpen, Priority: 2}, "tester"); err != nil { - t.Fatalf("seed %s: %v", root, err) - } - steps := make([]string, n) - for i := range steps { - steps[i] = fmt.Sprintf("%s.%d", root, i+1) - if err := kit.CreateIssue(ctx, &types.Issue{ID: steps[i], Title: "step " + steps[i], IssueType: types.TypeTask, Status: types.StatusOpen, Priority: 2}, "tester"); err != nil { - t.Fatalf("seed %s: %v", steps[i], err) - } - if err := kit.AddDependency(ctx, &types.Dependency{IssueID: steps[i], DependsOnID: root, Type: types.DepParentChild}, "tester"); err != nil { - t.Fatalf("seed edge %s -> %s: %v", steps[i], root, err) - } - } - return steps -} - -func readStatusInUOW(t *testing.T, ctx context.Context, kit roleFixtureKit, id string) string { - t.Helper() - var status string - if err := kit.QueryScalar(ctx, "SELECT status FROM issues WHERE id = ?", []any{id}, &status); err != nil { - t.Fatalf("read %s status: %v", id, err) - } - return status -} - -// closeTwoStepsInRacingUOWs closes steps[0] and steps[1], each followed by -// this backend's auto-close body (CloseCompletedMoleculeInUOW), in two units -// of work that both decide before either commits, so each reads the other's -// step open: the race's interleaving, made deterministic. Neither may -// auto-close the root. It commits both and answers the two commit errors. -func closeTwoStepsInRacingUOWs(t *testing.T, ctx context.Context, provider UnitOfWorkProvider, steps []string) [2]error { - t.Helper() - var uws [2]UnitOfWork - for i := range uws { - uw, err := provider.NewUOW(ctx) - if err != nil { - t.Fatalf("open unit of work %d: %v", i, err) - } - defer uw.Close(ctx) // no-op after Commit; preserves cleanup on early Fatal. - uws[i] = uw - } - for i, uw := range uws { - if _, err := uw.IssueUseCase().CloseIssueChecked(ctx, steps[i], domain.CloseIssueParams{Reason: "done"}, "tester", false); err != nil { - t.Fatalf("close %s: %v", steps[i], err) - } - auto, err := CloseCompletedMoleculeInUOW(ctx, uw, steps[i], "tester", "") - if err != nil { - t.Fatalf("auto-close after %s: %v", steps[i], err) - } - if auto.Root != nil { - t.Fatalf("close %s auto-closed %s, but in its own unit of work %s is still open", steps[i], auto.Root.ID, steps[1-i]) - } - } - return [2]error{uws[0].Commit(ctx, "close "+steps[0]), uws[1].Commit(ctx, "close "+steps[1])} -} - -// TestMoleculeAutoCloseLastStepsCannotBothMissTheRootInUOW is the unit-of-work -// twin of the dolt store's two-transaction regression: two units of work that -// each close one of a molecule's last two open steps and run this backend's -// auto-close body (CloseCompletedMoleculeInUOW) each read the other's step -// open, so neither closes the root. The coordination cell both rewrite must -// make one commit conflict, or the complete molecule's root is stranded open. -func TestMoleculeAutoCloseLastStepsCannotBothMissTheRootInUOW(t *testing.T) { - ctx := context.Background() - provider := newUOWRoleFixtureProvider(t, ctx, "molrace") - kit := newUOWRoleFixtureKit(provider, "molrace") - root := kit.IssuePrefix + "-race" - - commitErrs := closeTwoStepsInRacingUOWs(t, ctx, provider, seedRacingMoleculeInUOW(t, ctx, kit, root, 2)) - t.Logf("last-step commits: %v", commitErrs) - if commitErrs[0] == nil && commitErrs[1] == nil { - t.Fatalf("stranded molecule: both last-step closes committed and %s is %s with every step closed; one unit of work must conflict", - root, readStatusInUOW(t, ctx, kit, root)) - } - for i, err := range commitErrs { - if err != nil && !isSerializationError(err) { - t.Fatalf("commit %d error = %v, want a serialization conflict", i, err) - } - } -} - -// TestMoleculeAutoCloseSiblingClosesConflictBeforeTheLastStepInUOW is the -// unit-of-work twin of the dolt store's pin on the coordination cell's cost: -// two concurrent sibling closes under an open auto-closing root conflict even -// while a third step keeps the molecule from completing. If this stops -// conflicting, the docs that state the cost are stale. -func TestMoleculeAutoCloseSiblingClosesConflictBeforeTheLastStepInUOW(t *testing.T) { - ctx := context.Background() - provider := newUOWRoleFixtureProvider(t, ctx, "molsib") - kit := newUOWRoleFixtureKit(provider, "molsib") - - commitErrs := closeTwoStepsInRacingUOWs(t, ctx, provider, seedRacingMoleculeInUOW(t, ctx, kit, kit.IssuePrefix+"-siblings", 3)) - t.Logf("sibling commits: %v", commitErrs) - if commitErrs[0] == nil && commitErrs[1] == nil { - t.Fatal("two concurrent sibling closes under an open auto-closing root both committed; want one serialization conflict") - } - for i, err := range commitErrs { - if err != nil && !isSerializationError(err) { - t.Fatalf("commit %d error = %v, want a serialization conflict", i, err) - } - } -} - -// raceMoleculeClosesInUOW seeds an auto-closing molecule of n steps under -// root and closes every step at once through lifecycle, whose RunTxResult -// replays each unit of work that loses the coordination conflict. Every close -// must land, and the root must end closed, reported by exactly one of them. -func raceMoleculeClosesInUOW(t *testing.T, ctx context.Context, kit roleFixtureKit, lifecycle publicops.Lifecycle, root string, n int) { - t.Helper() - steps := seedRacingMoleculeInUOW(t, ctx, kit, root, n) - var ( - start = make(chan struct{}) - wg sync.WaitGroup - results = make([]publicops.CloseResult, n) - errs = make([]error, n) - ) - for i, step := range steps { - wg.Add(1) - go func() { - defer wg.Done() - <-start - results[i], errs[i] = lifecycle.Close(ctx, publicops.CloseRequest{ - Actor: "tester", IssueID: step, Reason: "done", AutoCloseMolecule: true, - }) - }() - } - close(start) - wg.Wait() - - reported := 0 - for i, err := range errs { - if err != nil { - t.Fatalf("close %s: %v", steps[i], err) - } - if results[i].AutoClosedMolecule != nil { - reported++ - } - } - if status := readStatusInUOW(t, ctx, kit, root); status != string(types.StatusClosed) { - t.Fatalf("%s is %s after all %d of its steps closed, want closed", root, status, n) - } - if reported != 1 { - t.Errorf("%s was reported by %d closes, want exactly one", root, reported) - } -} - -// TestMoleculeAutoCloseConcurrentLastStepsCloseTheRootInUOW races the closes -// of a molecule's last two steps through this backend's Lifecycle role, whose -// RunTxResult replays the unit of work that loses the coordination conflict: -// in every round the root ends closed, reported by exactly one of the closes. -func TestMoleculeAutoCloseConcurrentLastStepsCloseTheRootInUOW(t *testing.T) { - ctx := context.Background() - provider := newUOWRoleFixtureProvider(t, ctx, "molagg") - kit := newUOWRoleFixtureKit(provider, "molagg") - lifecycle, err := NewIssueOperations(provider) - if err != nil { - t.Fatalf("NewIssueOperations: %v", err) - } - - const rounds = 8 - for round := range rounds { - raceMoleculeClosesInUOW(t, ctx, kit, lifecycle, fmt.Sprintf("%s-race-%d", kit.IssuePrefix, round), 2) - } -} - -// TestMoleculeAutoCloseSiblingFanOutClosesTheRootInUOW is the unit-of-work -// twin of the dolt store's fan-out: all the steps of one molecule closed at -// once serialize on the root's coordination cell, and RunTxResult carries -// every one of them to a commit, closing the root exactly once. -func TestMoleculeAutoCloseSiblingFanOutClosesTheRootInUOW(t *testing.T) { - ctx := context.Background() - provider := newUOWRoleFixtureProvider(t, ctx, "molfan") - kit := newUOWRoleFixtureKit(provider, "molfan") - lifecycle, err := NewIssueOperations(provider) - if err != nil { - t.Fatalf("NewIssueOperations: %v", err) - } - - raceMoleculeClosesInUOW(t, ctx, kit, lifecycle, kit.IssuePrefix+"-fanout", 6) -} diff --git a/internal/storage/uow/notifying.go b/internal/storage/uow/notifying.go index a2148502d..25b3a268d 100644 --- a/internal/storage/uow/notifying.go +++ b/internal/storage/uow/notifying.go @@ -31,8 +31,8 @@ // # Where this plumbing deliberately differs from the DoltStorage one // // Every per-verb firing rule below is matched 1:1 against the decorator that -// implements it (each override cites its twin). These four are the exceptions, -// and they are listed here rather than argued in four places: +// implements it (each override cites its twin). These five are the exceptions, +// and they are listed here rather than argued in five places: // // 1. LABEL MULTIPLICITY ON CREATE. The DoltStorage chain replays a legacy // sequence for a create that carries labels: on_create with the labels @@ -61,7 +61,16 @@ // that writes nothing; the DoltStorage plumbing passes the precondition // into the close. See specWrites. // -// A fifth is not a divergence but is worth stating: a unit of work whose commit +// 5. THE PROMOTION COMMENT. `bd promote` records an audit comment on the +// promoted issue, and this plumbing's one comment verb fires on_update for +// it. The DoltStorage route writes that comment through the legacy +// AddComment, which the decorator wraps only INSIDE a transaction +// (hookTrackingTransaction.AddComment) and not at the store level, where +// promote calls it — so an embedded promote fires nothing. One verb cannot +// tell which caller it is serving, and "a comment landed" is the on_update +// this vocabulary has. +// +// A sixth is not a divergence but is worth stating: a unit of work whose commit // message is empty is ROLLED BACK by RunTxResult, and a rolled-back attempt // fires nothing — so a write that lands nothing reports nothing, on both // plumbings. @@ -229,14 +238,6 @@ func (p *notifyingProvider) GraphCounter() (publicops.GraphCounter, error) { return NewGraphCounter(p) } -func (p *notifyingProvider) BatchGetter() (publicops.BatchGetter, error) { - return NewBatchGetter(p) -} - -func (p *notifyingProvider) MoleculeStepper() (publicops.MoleculeStepper, error) { - return NewMoleculeStepper(p) -} - func (p *notifyingProvider) Counter() (publicops.Counter, error) { return NewCounter(p) } func (p *notifyingProvider) ReadyCounter() (publicops.ReadyCounter, error) { @@ -304,24 +305,6 @@ func (p *notifyingProvider) MetadataCAS() (publicops.MetadataCAS, error) { retur func (p *notifyingProvider) Releaser() (publicops.Releaser, error) { return NewReleaser(p) } -func (p *notifyingProvider) LeaseReclaimer() (publicops.LeaseReclaimer, error) { - return NewLeaseReclaimer(p) -} - -// Heartbeater builds the lease refresh over THIS provider. recordingIssueUC -// declares RenewLease: a refresh records nothing (it writes only the -// ephemeral lease row), and a re-arm records an update, the hook -// storage.HookFiringStore.Heartbeater fires for it on the DoltStorage chain. -func (p *notifyingProvider) Heartbeater() (publicops.Heartbeater, error) { - return NewHeartbeater(p) -} - -// BlockedLister builds the blocked listing over THIS provider. It is a read: -// nothing is recorded and no hook fires. -func (p *notifyingProvider) BlockedLister() (publicops.BlockedLister, error) { - return NewBlockedLister(p) -} - func (p *notifyingProvider) Memories() (memoryops.Memories, error) { return NewMemories(p) } // EventsJournalCursor builds on THIS provider, like every role above it, so a @@ -369,16 +352,6 @@ func (p *notifyingProvider) SetEventsJournalEnabled(enabled bool) { } } -// SetVersionedHistoryEnabled forwards dual-write issue-version history -// activation to the provider that actually binds it to a transaction -// (doltSQLProvider.BeginTx), for the same reason and in the same shape as -// SetEventsJournalEnabled above. -func (p *notifyingProvider) SetVersionedHistoryEnabled(enabled bool) { - if configurer, ok := p.inner.(storage.VersionedHistoryConfigurer); ok { - configurer.SetVersionedHistoryEnabled(enabled) - } -} - // eventsMaintenanceRunner is issueops.EventsMaintenanceRunner under this file's // import alias, named once so the forwarder below reads as an ordinary // capability check. @@ -417,8 +390,6 @@ var ( _ BlockingAnnotatorSource = (*notifyingProvider)(nil) _ TreeWalkerSource = (*notifyingProvider)(nil) _ GraphCounterSource = (*notifyingProvider)(nil) - _ BatchGetterSource = (*notifyingProvider)(nil) - _ MoleculeStepperSource = (*notifyingProvider)(nil) _ CounterSource = (*notifyingProvider)(nil) _ ReadyCounterSource = (*notifyingProvider)(nil) _ ReadyClaimerSource = (*notifyingProvider)(nil) @@ -437,9 +408,6 @@ var ( _ WorkspaceConfigSource = (*notifyingProvider)(nil) _ VersionReconcilerSource = (*notifyingProvider)(nil) _ MemoriesSource = (*notifyingProvider)(nil) - _ LeaseReclaimerSource = (*notifyingProvider)(nil) - _ HeartbeaterSource = (*notifyingProvider)(nil) - _ BlockedListerSource = (*notifyingProvider)(nil) _ EventsJournalCursorSource = (*notifyingProvider)(nil) ) @@ -504,22 +472,6 @@ func (u *notifyingUOW) Commit(ctx context.Context, message string) error { return nil } -// CommitScoped is Commit for a scoped version commit (see ScopedCommitter): -// commit through the chain beneath, then fire the buffered hooks. -func (u *notifyingUOW) CommitScoped(ctx context.Context, tables []string, message string) error { - if err := commitScoped(ctx, u.UnitOfWork, tables, message); err != nil { - return err - } - entries := u.rec.drain() - if u.sinks.Hook == nil { - return nil - } - for _, entry := range entries { - u.sinks.Hook.Run(entry.event, entry.issue) - } - return nil -} - // Close rolls the unit of work back unless it committed, so whatever the // attempt buffered is dropped rather than fired. func (u *notifyingUOW) Close(ctx context.Context) { @@ -1041,39 +993,6 @@ func (u *recordingIssueUC) ReleaseIssue(ctx context.Context, req publicops.Relea return result, wrote, nil } -// RenewLease records an update for a heartbeat that re-armed a lease (a -// mutation: row_lock, updated_at, event, journal, version) and nothing for a -// refresh, which writes only the ephemeral lease row. It is DECLARED for -// ReleaseIssue's reason: an inherited accessor would compile and record -// nothing, silently dropping the re-arm's hook. -func (u *recordingIssueUC) RenewLease(ctx context.Context, request publicops.HeartbeatRequest) (publicops.HeartbeatResult, error) { - result, err := u.IssueUseCase.RenewLease(ctx, request) - if err != nil || !result.Rearmed { - return result, err - } - u.rec.record(opUpdate, u.snap.issue(ctx, result.IssueID)) - return result, nil -} - -// Reclaim records an update for every lease the sweep reverted. -// -// It is DECLARED for the same reason ReleaseIssue above is: an inherited -// accessor would compile and record nothing, silently dropping the hook the -// DoltStorage chain fires once per reverted row. -// -// A sweep reverts only issues, never wisps, so the snapshot is the issues- -// plane snap.issue rather than anyPlane. -func (u *recordingIssueUC) Reclaim(ctx context.Context, request publicops.ReclaimRequest) (publicops.ReclaimResult, error) { - result, err := u.IssueUseCase.Reclaim(ctx, request) - if err != nil { - return result, err - } - for _, r := range result.Reclaimed { - u.rec.record(opUpdate, u.snap.issue(ctx, r.ID)) - } - return result, nil -} - func (u *recordingIssueUC) UpdateWisp(ctx context.Context, id string, updates map[string]any, actor string) error { if err := u.IssueUseCase.UpdateWisp(ctx, id, updates, actor); err != nil { return err @@ -1360,23 +1279,6 @@ func (u *recordingLabelUC) RemoveLabel(ctx context.Context, issueID, label, acto return nil } -// RenameLabel records one update per touched issue/wisp, mirroring -// HookFiringStore.RenameLabel on the DoltStorage plumbing (both fire on_update -// per id). THE SNAPSHOT IS anyPlane, because RenameLabel sweeps both the -// labels and wisp_labels tables together and the returned ids do not say -// which plane each one belongs to - the same reason CompareAndSetMetadataKey -// and ReleaseIssue above use it. -func (u *recordingLabelUC) RenameLabel(ctx context.Context, oldLabel, newLabel, actor string) (renamed, merged int, ids []string, err error) { - renamed, merged, ids, err = u.LabelUseCase.RenameLabel(ctx, oldLabel, newLabel, actor) - if err != nil { - return renamed, merged, ids, err - } - for _, id := range ids { - u.rec.record(opUpdate, u.snap.anyPlane(ctx, id)) - } - return renamed, merged, ids, nil -} - func (u *recordingLabelUC) AddLabels(ctx context.Context, issueID string, labels []string, actor string) error { if err := u.LabelUseCase.AddLabels(ctx, issueID, labels, actor); err != nil { return err diff --git a/internal/storage/uow/notifying_parity_test.go b/internal/storage/uow/notifying_parity_test.go index 72faa8e03..c9fd5d7fd 100644 --- a/internal/storage/uow/notifying_parity_test.go +++ b/internal/storage/uow/notifying_parity_test.go @@ -130,9 +130,7 @@ func TestRecordingIssueUseCaseCoversItsSurface(t *testing.T) { "has no name for a deletion and the row is gone (hook_deleter.go, hookTrackingTransaction.DeleteIssue)" leases := "lease and claim bookkeeping the DoltStorage plumbing fires no hook for: " + "HookFiringStore overrides neither UnclaimIssue, UnclaimIssueIfAssignee, HeartbeatIssue nor " + - "ReclaimExpiredLeases, so `bd unclaim` runs no script on either plumbing, and neither does a " + - "heartbeat through these doors (`bd heartbeat` takes the Heartbeater role, whose re-arm records " + - "an update through RenewLease)" + "ReclaimExpiredLeases, so `bd unclaim` and `bd heartbeat` run no script on either plumbing" assertPartition(t, "IssueUseCase", interfaceMethods(reflect.TypeOf((*domain.IssueUseCase)(nil)).Elem()), reflect.TypeOf((*recordingIssueUC)(nil)), map[string]string{ @@ -147,7 +145,6 @@ func TestRecordingIssueUseCaseCoversItsSurface(t *testing.T) { "GetEpicsEligibleForClosure": reads, "GetIssue": reads, "GetIssuesByIDs": reads, - "GetMany": reads, "GetNewlyUnblockedByClose": reads, "GetNewlyUnblockedByCloseWisp": reads, "GetReadyWork": reads, @@ -174,9 +171,8 @@ func TestRecordingIssueUseCaseCoversItsSurface(t *testing.T) { "ReclaimExpiredLeases": leases, "WakeExpiredDefers": "the deferred-work sweep is a clock crossing rather than a user's " + "mutation, and the DoltStorage plumbing fires nothing for it", - "PromoteWisp": "no route calls it: bd promote moves the row with ApplyUpdate's " + - "persistence spec, which records the update, and its DoltStorage twin, " + - "PromoteFromEphemeral, fires no hook either", + "PromoteWisp": "moves a row between planes without changing the bead; the DoltStorage " + + "plumbing has no promote hook either", }) } @@ -194,26 +190,24 @@ func TestRecordingDependencyUseCaseCoversItsSurface(t *testing.T) { // — an ancestry walk and a reachability walk — so there is nothing // for the recorder to notify about; the writes those gates guard are // recorded by AddDependencies, which is declared below. - "ValidateBlockingHierarchy": reads, - "CycleThroughEdges": reads, - "DetectCycleReport": reads, - "DetectCycles": reads, - "GetBlockingInfo": reads, - "GetDependencyTree": reads, - "GetExternalBlockingDependencyRecords": reads, - "GetForIssueIDs": reads, - "GetIssueDependencyRecords": reads, - "GetWispDependencyRecords": reads, - "IsBlocked": reads, - "IsBlockedBatch": reads, - "IsWispBlocked": reads, - "IterWispWithIssueMetadata": reads, - "IterWithIssueMetadata": reads, - "ListByIssueIDs": reads, - "ListByWispIDs": reads, - "ListWispWithIssueMetadata": reads, - "ListWithIssueMetadata": reads, - "WalkDependencyTree": reads, + "ValidateBlockingHierarchy": reads, + "CycleThroughEdges": reads, + "DetectCycleReport": reads, + "DetectCycles": reads, + "GetBlockingInfo": reads, + "GetDependencyTree": reads, + "GetForIssueIDs": reads, + "GetIssueDependencyRecords": reads, + "GetWispDependencyRecords": reads, + "IsBlocked": reads, + "IsWispBlocked": reads, + "IterWispWithIssueMetadata": reads, + "IterWithIssueMetadata": reads, + "ListByIssueIDs": reads, + "ListByWispIDs": reads, + "ListWispWithIssueMetadata": reads, + "ListWithIssueMetadata": reads, + "WalkDependencyTree": reads, }) } @@ -300,13 +294,11 @@ func TestNotifyingProviderBuildsRolesOnItself(t *testing.T) { "RunNonTx": true, "SetPoolLimits": true, "Unwrap": true, - // Events-journal and versioned-history capability plumbing: activation - // for both binds on the inner provider's transactions, and a retention - // pass writes no bead. None of these build a role, so none of them can - // build one "on itself". - "SetEventsJournalEnabled": true, - "RunEventsMaintenanceTx": true, - "SetVersionedHistoryEnabled": true, + // Events-journal capability plumbing: activation binds on the inner + // provider's transactions, and a retention pass writes no bead. Neither + // builds a role, so neither can build one "on itself". + "SetEventsJournalEnabled": true, + "RunEventsMaintenanceTx": true, } var checked int diff --git a/internal/storage/uow/preview_provider_test.go b/internal/storage/uow/preview_provider_test.go index 034df323c..6f815c55e 100644 --- a/internal/storage/uow/preview_provider_test.go +++ b/internal/storage/uow/preview_provider_test.go @@ -62,22 +62,12 @@ func TestInitSchemaPreviewMissingDatabaseFailsClosed(t *testing.T) { } // TestApplyProviderOptions keeps the option plumbing honest: the ordinary -// callers pass nothing and must stay on the mutating open, and only an open -// that asks for it is treated as proxied-server mode. +// callers pass nothing and must stay on the mutating open. func TestApplyProviderOptions(t *testing.T) { - got := applyProviderOptions(nil) - if got.preview { + if got := applyProviderOptions(nil); got.preview { t.Fatal("applyProviderOptions(nil).preview = true, want false") } - if got.proxiedServerMode { - t.Fatal("applyProviderOptions(nil).proxiedServerMode = true, want false") - } if got := applyProviderOptions([]ProviderOption{WithPreview()}); !got.preview { t.Fatal("applyProviderOptions(WithPreview()).preview = false, want true") } - // The proxied marker changes what a refusal says, not how the open - // behaves, so it must not switch on either open posture as well. - if got := applyProviderOptions([]ProviderOption{WithProxiedServerMode()}); !got.proxiedServerMode || got.preview || got.readOnly { - t.Fatalf("applyProviderOptions(WithProxiedServerMode()) = %+v, want only proxiedServerMode set", got) - } } diff --git a/internal/storage/uow/reader_contract_test.go b/internal/storage/uow/reader_contract_test.go index 54bab3aa0..427219c4e 100644 --- a/internal/storage/uow/reader_contract_test.go +++ b/internal/storage/uow/reader_contract_test.go @@ -92,9 +92,6 @@ func TestReaderContract(t *testing.T) { t.Run("ListEmptyPageIsWellFormed", func(t *testing.T) { conformance.RunReaderListEmptyPageIsWellFormed(t, ctx, fixture) }) - t.Run("ListQueryMatchesTheTitleOrTheID", func(t *testing.T) { - conformance.RunReaderListQueryMatchesTheTitleOrTheID(t, ctx, fixture) - }) t.Run("GetResolvesTheExactIDAcrossBothPlanes", func(t *testing.T) { conformance.RunReaderGetResolvesTheExactIDAcrossBothPlanes(t, ctx, fixture) }) @@ -167,12 +164,6 @@ func TestReaderContract(t *testing.T) { t.Run("ListWispTypeNarrowsTheAdmittedPlaneRatherThanAdmittingIt", func(t *testing.T) { conformance.RunReaderListWispTypeNarrowsTheAdmittedPlaneRatherThanAdmittingIt(t, ctx, fixture) }) - t.Run("ListTimeBoundsAreStrictAndWholeSecond", func(t *testing.T) { - conformance.RunReaderListTimeBoundsAreStrictAndWholeSecond(t, ctx, fixture) - }) - t.Run("ListFilterFamiliesNarrowToTheirRows", func(t *testing.T) { - conformance.RunReaderListFilterFamiliesNarrowToTheirRows(t, ctx, fixture) - }) t.Run("ListBriefDropsTheFreeFormTextAndNothingElse", func(t *testing.T) { conformance.RunReaderListBriefDropsTheFreeFormTextAndNothingElse(t, ctx, fixture) }) @@ -216,10 +207,5 @@ func newUOWReaderFixture(t *testing.T, provider UnitOfWorkProvider, prefix strin return "bd: comment " + issueID, err }) }, - PromoteWisp: func(ctx context.Context, id, actor string) error { - return RunTx(ctx, provider, func(ctx context.Context, uw UnitOfWork) (string, error) { - return "bd: promote " + id, uw.IssueUseCase().PromoteWisp(ctx, id, actor) - }) - }, } } diff --git a/internal/storage/uow/relations.go b/internal/storage/uow/relations.go index 8e090dc99..9db1a0815 100644 --- a/internal/storage/uow/relations.go +++ b/internal/storage/uow/relations.go @@ -48,35 +48,29 @@ func (r *issueRelations) Related(ctx context.Context, request publicops.RelatedR if err := storageissueops.ValidateRelatedRequest(request); err != nil { return nil, err } - return RunTxRead(ctx, r.provider, func(ctx context.Context, uw UnitOfWork) ([]*publicops.RelatedIssue, error) { - return RelatedInUOW(ctx, uw, request) - }) -} - -// RelatedInUOW is the Relations body, run inside the caller's unit of work. -// The request must already be validated. -func RelatedInUOW(ctx context.Context, uw UnitOfWork, request publicops.RelatedRequest) ([]*publicops.RelatedIssue, error) { direction := domain.DepDirectionOut if request.Direction == publicops.RelationIn { direction = domain.DepDirectionIn } - issue, isWisp, err := workapi.GetIssueOrWisp(ctx, workapi.NewUOWDetailSource(uw), request.ID) - if err != nil { - return nil, err - } - filter := domain.DepListFilter{Direction: direction} - var items []*publicops.RelatedIssue - if isWisp { - items, err = uw.DependencyUseCase().ListWispWithIssueMetadata(ctx, issue.ID, filter) - } else { - items, err = uw.DependencyUseCase().ListWithIssueMetadata(ctx, issue.ID, filter) - } - if err != nil { - return nil, err - } - // The filter runs HERE rather than in the use-case call above so both - // implementations narrow and order through one function; passing - // filter.Types down would put the narrowing in the query on one side - // and in Go on the other. - return storageissueops.FinishRelatedPage(items, request.Types), nil + return RunTxRead(ctx, r.provider, func(ctx context.Context, uw UnitOfWork) ([]*publicops.RelatedIssue, error) { + issue, isWisp, err := workapi.GetIssueOrWisp(ctx, workapi.NewUOWDetailSource(uw), request.ID) + if err != nil { + return nil, err + } + filter := domain.DepListFilter{Direction: direction} + var items []*publicops.RelatedIssue + if isWisp { + items, err = uw.DependencyUseCase().ListWispWithIssueMetadata(ctx, issue.ID, filter) + } else { + items, err = uw.DependencyUseCase().ListWithIssueMetadata(ctx, issue.ID, filter) + } + if err != nil { + return nil, err + } + // The filter runs HERE rather than in the use-case call above so both + // implementations narrow and order through one function; passing + // filter.Types down would put the narrowing in the query on one side + // and in Go on the other. + return storageissueops.FinishRelatedPage(items, request.Types), nil + }) } diff --git a/internal/storage/uow/retention_epoch_contract_test.go b/internal/storage/uow/retention_epoch_contract_test.go deleted file mode 100644 index 91d3a5324..000000000 --- a/internal/storage/uow/retention_epoch_contract_test.go +++ /dev/null @@ -1,271 +0,0 @@ -package uow - -import ( - "context" - "fmt" - "testing" - - "github.com/steveyegge/beads/backend/conformance" - storeops "github.com/steveyegge/beads/internal/storage/issueops" -) - -// TestRetentionContract wires this leg into the R20 retention contract. -// Phase 0 leaves every hook nil in every backend's fixture kit (architecture -// §12), so each case below skips by name; this file exists so -// TestEveryLegWiresEveryRoleContract counts this leg, and so the cases start -// running for real the moment this leg's fixture kit grows a non-nil hook. -func TestRetentionContract(t *testing.T) { - ctx := context.Background() - fixture := conformance.RetentionFixture{IssuePrefix: "rete"} - - t.Run("RemovalLeavesTheAddressAbleToAnswer", func(t *testing.T) { - conformance.RunRemovalLeavesTheAddressAbleToAnswer(t, ctx, fixture) - }) - t.Run("RemovalReasonIsRetentionErasureOrReorganizationDistinctly", func(t *testing.T) { - conformance.RunRemovalReasonIsRetentionErasureOrReorganizationDistinctly(t, ctx, fixture) - }) - t.Run("RemovalReportsTheSurvivingRetainedWindow", func(t *testing.T) { - conformance.RunRemovalReportsTheSurvivingRetainedWindow(t, ctx, fixture) - }) - t.Run("RemovalNeverReassignsASurvivingAddress", func(t *testing.T) { - conformance.RunRemovalNeverReassignsASurvivingAddress(t, ctx, fixture) - }) - t.Run("GoneIsDistinguishableFromUnknown", func(t *testing.T) { - conformance.RunGoneIsDistinguishableFromUnknown(t, ctx, fixture) - }) - t.Run("AnAddressNeverResolvesToADifferentState", func(t *testing.T) { - conformance.RunAnAddressNeverResolvesToADifferentState(t, ctx, fixture) - }) - t.Run("ADestructiveOperationEnumeratesAffectedAddressesFirst", func(t *testing.T) { - conformance.RunADestructiveOperationEnumeratesAffectedAddressesFirst(t, ctx, fixture) - }) - t.Run("AHoldPreventsRemovalAndReportsInRetainedBounds", func(t *testing.T) { - conformance.RunAHoldPreventsRemovalAndReportsInRetainedBounds(t, ctx, fixture) - }) - t.Run("ForcingAHeldRemovalRecordsWhoWhenWhy", func(t *testing.T) { - conformance.RunForcingAHeldRemovalRecordsWhoWhenWhy(t, ctx, fixture) - }) - t.Run("EveryRetentionAnswerNamesItsProducingStore", func(t *testing.T) { - conformance.RunEveryRetentionAnswerNamesItsProducingStore(t, ctx, fixture) - }) - t.Run("AStoreWithNoLineageKnowledgeAnswersUnknownNotGone", func(t *testing.T) { - conformance.RunAStoreWithNoLineageKnowledgeAnswersUnknownNotGone(t, ctx, fixture) - }) - t.Run("AStoreThatRemovesStateStillAnswersGoneDurably", func(t *testing.T) { - conformance.RunAStoreThatRemovesStateStillAnswersGoneDurably(t, ctx, fixture) - }) - t.Run("ErasureMintsACorrectedVersionRatherThanEditingInPlace", func(t *testing.T) { - conformance.RunErasureMintsACorrectedVersionRatherThanEditingInPlace(t, ctx, fixture) - }) -} - -// TestEpochContract runs the R20 epoch contract (gastownhall/beads#5898 -// revision 9, #6136) against the unit-of-work -// provider, which reaches the same internal/storage/issueops's epoch Tx -// functions the two store backends wrap — directly against the unit of -// work's own runner (the *baseUOW/base.tx.Runner() pattern -// expected_revision_contract_test.go's expectedRevisionUOWMutateOutside and -// commenter_contract_test.go's SeedCommentAt both use), NOT through the -// domain issue repository: EpochFixture's seven hooks are store-singleton/ -// administrative operations with no natural correspondence to -// IssueUseCase's per-issue CRUD/notification semantics, so unlike -// CompareAndSetVersion there is no domain/use-case method to route through -// in the first place — every hook below needs the escape hatch, not just -// one. -// -// One provider for the whole suite (each newUOWRoleFixtureProvider boots a -// real Dolt sql-server) and NO t.Parallel: this backend has no per-test -// copy-on-write branch, so store_epoch/epoch_minted_addresses are -// database-global and a parallel subtest would corrupt another subtest's -// row — same reasoning as TestExpectedRevisionContract's. -func TestEpochContract(t *testing.T) { - ctx := context.Background() - fixture := newUOWEpochFixture(t, ctx, "epch") - - t.Run("AnEpochBumpIsTriggeredOnlyByRestoreReinitOrSchemeChange", func(t *testing.T) { - conformance.RunAnEpochBumpIsTriggeredOnlyByRestoreReinitOrSchemeChange(t, ctx, fixture) - }) - t.Run("EpochBumpVoidsOnlyAddressesOfVersionsNoLongerServed", func(t *testing.T) { - conformance.RunEpochBumpVoidsOnlyAddressesOfVersionsNoLongerServed(t, ctx, fixture) - }) - t.Run("TokenSchemeChangeCarriesLiveAddressesAndOnlyThose", func(t *testing.T) { - conformance.RunTokenSchemeChangeCarriesLiveAddressesAndOnlyThose(t, ctx, fixture) - }) - t.Run("ACarriedAddressKeepsResolvingAcrossLaterBumps", func(t *testing.T) { - conformance.RunACarriedAddressKeepsResolvingAcrossLaterBumps(t, ctx, fixture) - }) - t.Run("ALostAddressStaysGoneAcrossASchemeChange", func(t *testing.T) { - conformance.RunALostAddressStaysGoneAcrossASchemeChange(t, ctx, fixture) - }) -} - -// newUOWEpochFixture wires this leg's UnitOfWorkProvider into the R20 epoch -// contract. Like newUOWExpectedRevisionFixture, this fixture needs none of -// roleFixtureKit's CreateIssue/CreateWisp/QueryScalar/CountHistory hooks — -// it has no fields for them — so it does not route through -// newUOWRoleFixtureKit at all. -func newUOWEpochFixture(t *testing.T, ctx context.Context, prefix string) conformance.EpochFixture { - t.Helper() - provider := newUOWRoleFixtureProvider(t, ctx, prefix) - return conformance.EpochFixture{ - IssuePrefix: prefix, - CurrentEpoch: epochUOWCurrentEpoch(provider), - BumpEpoch: epochUOWBumpEpoch(provider), - MintUnderEpoch: epochUOWMintUnderEpoch(provider), - StillServes: epochUOWStillServes(provider), - Resolve: epochUOWResolve(provider), - CurrentAddressFor: epochUOWCurrentAddressFor(provider), - LoseVersion: epochUOWLoseVersion(provider), - } -} - -// epochUOWRunner recovers the *baseUOW a given attempt's UnitOfWork actually -// is, the same type assertion expectedRevisionUOWMutateOutside makes: Tx. -// Runner() is unexported outside this package and none of the seven epoch -// operations has a domain/use-case method to reach it through instead. -func epochUOWRunner(uw UnitOfWork) (storeops.DBTX, error) { - base, ok := uw.(*baseUOW) - if !ok { - return nil, fmt.Errorf("epoch CAS: unit of work %T does not expose the runner the epoch Tx functions need", uw) - } - return base.tx.Runner(), nil -} - -func epochUOWCurrentEpoch(provider UnitOfWorkProvider) func(ctx context.Context, storeID string) (int, error) { - return func(ctx context.Context, storeID string) (int, error) { - return RunTxRead(ctx, provider, func(ctx context.Context, uw UnitOfWork) (int, error) { - runner, err := epochUOWRunner(uw) - if err != nil { - return 0, err - } - return storeops.CurrentEpochInTx(ctx, runner, storeID) - }) - } -} - -// epochUOWBumpEpoch chooses the bump by the typed trigger: only a -// token-scheme-change bump carries live addresses to the new epoch, and the -// other two triggers move the counter alone. The choice lives here, in the -// contract-test translation layer, because the shared body has no switch on -// the reason string. -func epochUOWBumpEpoch(provider UnitOfWorkProvider) func(ctx context.Context, storeID string, trigger conformance.EpochBumpTrigger) (int, error) { - return func(ctx context.Context, storeID string, trigger conformance.EpochBumpTrigger) (int, error) { - return RunTxResult(ctx, provider, func(ctx context.Context, uw UnitOfWork) (int, string, error) { - runner, err := epochUOWRunner(uw) - if err != nil { - return 0, "", err - } - bump := storeops.BumpEpochInTx - if trigger == conformance.EpochBumpTriggerTokenSchemeChange { - bump = storeops.BumpEpochCarryingInTx - } - epoch, err := bump(ctx, runner, storeID, trigger.String()) - if err != nil { - return 0, "", err - } - return epoch, fmt.Sprintf("bd: bump epoch for %s (%s)", storeID, trigger.String()), nil - }) - } -} - -func epochUOWMintUnderEpoch(provider UnitOfWorkProvider) func(ctx context.Context, storeID, id string) (conformance.Address, error) { - return func(ctx context.Context, storeID, id string) (conformance.Address, error) { - address, err := RunTxResult(ctx, provider, func(ctx context.Context, uw UnitOfWork) (string, string, error) { - runner, err := epochUOWRunner(uw) - if err != nil { - return "", "", err - } - address, err := storeops.MintUnderEpochInTx(ctx, runner, storeID, id) - if err != nil { - return "", "", err - } - return address, fmt.Sprintf("bd: mint %s under epoch for %s", id, storeID), nil - }) - if err != nil { - return "", err - } - return conformance.Address(address), nil - } -} - -func epochUOWStillServes(provider UnitOfWorkProvider) func(ctx context.Context, storeID string, address conformance.Address) (bool, error) { - return func(ctx context.Context, storeID string, address conformance.Address) (bool, error) { - return RunTxRead(ctx, provider, func(ctx context.Context, uw UnitOfWork) (bool, error) { - runner, err := epochUOWRunner(uw) - if err != nil { - return false, err - } - return storeops.StillServesInTx(ctx, runner, storeID, string(address)) - }) - } -} - -func epochUOWResolve(provider UnitOfWorkProvider) func(ctx context.Context, storeID string, address conformance.Address) (conformance.RetentionAnswer, error) { - return func(ctx context.Context, storeID string, address conformance.Address) (conformance.RetentionAnswer, error) { - result, err := RunTxRead(ctx, provider, func(ctx context.Context, uw UnitOfWork) (storeops.EpochResolveResult, error) { - runner, err := epochUOWRunner(uw) - if err != nil { - return storeops.EpochResolveResult{}, err - } - return storeops.ResolveEpochInTx(ctx, runner, storeID, string(address)) - }) - if err != nil { - return conformance.RetentionAnswer{}, err - } - return epochResolveResultToConformance(result), nil - } -} - -func epochUOWCurrentAddressFor(provider UnitOfWorkProvider) func(ctx context.Context, storeID string, oldAddress conformance.Address) (conformance.Address, error) { - return func(ctx context.Context, storeID string, oldAddress conformance.Address) (conformance.Address, error) { - address, err := RunTxRead(ctx, provider, func(ctx context.Context, uw UnitOfWork) (string, error) { - runner, err := epochUOWRunner(uw) - if err != nil { - return "", err - } - return storeops.CurrentAddressForInTx(ctx, runner, storeID, string(oldAddress)) - }) - if err != nil { - return "", err - } - return conformance.Address(address), nil - } -} - -func epochUOWLoseVersion(provider UnitOfWorkProvider) func(ctx context.Context, storeID string, address conformance.Address) error { - return func(ctx context.Context, storeID string, address conformance.Address) error { - return RunTx(ctx, provider, func(ctx context.Context, uw UnitOfWork) (string, error) { - runner, err := epochUOWRunner(uw) - if err != nil { - return "", err - } - if err := storeops.LoseVersionInTx(ctx, runner, storeID, string(address)); err != nil { - return "", err - } - return fmt.Sprintf("bd: lose version %s for %s", address, storeID), nil - }) - } -} - -// epochRestrictionToConformance translates this leg's local -// storeops.EpochRestriction into backend/conformance's own Restriction -// vocabulary — the contract-test file is the translation boundary, per -// epoch_cas.go's package doc. -func epochRestrictionToConformance(r storeops.EpochRestriction) conformance.Restriction { - switch r { - case storeops.EpochRestrictionLive: - return conformance.RestrictionLive - case storeops.EpochRestrictionGoneReorganization: - return conformance.RestrictionGoneReorganization - default: - return conformance.RestrictionUnknown - } -} - -func epochResolveResultToConformance(r storeops.EpochResolveResult) conformance.RetentionAnswer { - return conformance.RetentionAnswer{ - Restriction: epochRestrictionToConformance(r.Restriction), - ProducingStore: r.ProducingStore, - Epoch: r.Epoch, - } -} diff --git a/internal/storage/uow/shared_server_cleanup_guard_test.go b/internal/storage/uow/shared_server_cleanup_guard_test.go deleted file mode 100644 index 83cf96a4e..000000000 --- a/internal/storage/uow/shared_server_cleanup_guard_test.go +++ /dev/null @@ -1,268 +0,0 @@ -package uow - -import ( - "go/ast" - "go/parser" - "go/token" - "io/fs" - "path/filepath" - "sort" - "strings" - "testing" -) - -// providerConstructors are the calls that make this package provision a -// shared-server workspace: each one starts, or attaches to, a detached -// `dolt sql-server` whose data directory is a t.TempDir(). -var providerConstructors = map[string]bool{ - "NewDoltServerUOWProvider": true, - "NewExternalDoltServerUOWProvider": true, -} - -// shutdownCallers are the only functions allowed to call proxy.Shutdown -// directly. Everything else goes through verifiedShutdownCleanup, which is -// the one that also asserts the daemon is gone. -var shutdownCallers = map[string]bool{ - "verifiedShutdownCleanup": true, - // shutdownOnInterrupt is the SIGINT/SIGTERM handler: it runs in a - // goroutine on its way to os.Exit, where no assertion could be reported - // and no *testing.T is still live to report it to. - "shutdownOnInterrupt": true, -} - -// validationErrorMarker is the ONLY thing that exempts a function from rule -// C: a test whose name ends in it declares, in the one place a reader and a -// linter both see, that it calls a provider constructor purely to watch it -// reject bad arguments — the constructor returns before starting anything, so -// there is no daemon to clean up. -// -// It is a name marker rather than an inference from the body because every -// body-shaped heuristic we tried was dodgeable. Exempting "any function that -// calls require.Error" let -// TestNewExternalDoltServerUOWProvider_PreexistingDirtyDatabaseIsNotHealed — -// which provisions a REAL server, asserts NoError on the constructor, and only -// later asserts an error from a query — delete both cleanup helpers and stay -// green (wy-j2zc8q, mutation G). A marker cannot be reached by accident: a -// fixture has to be renamed to claim it. -const validationErrorMarker = "_ValidationErrors" - -// errorAssertions are the calls that make the marker's claim true. They do not -// grant the exemption (see above); they are checked AGAINST a function that -// claims it, so a renamed-but-provisioning fixture cannot wear the marker -// silently. -var errorAssertions = map[string]bool{ - "Error": true, // require.Error / assert.Error - "ErrorContains": true, -} - -// successAssertions are the calls that make the marker's claim FALSE. A -// constructor that was rejected returns before starting anything, so a -// validation-error fixture has nothing that can succeed: no require.NoError, -// no assert.NoError anywhere in its body. Checking this closes the one dodge -// the marker alone leaves open — renaming a fixture that really does provision -// a server so it wears the marker. -var successAssertions = map[string]bool{ - "NoError": true, // require.NoError / assert.NoError -} - -// TestEveryServerFixtureRegistersVerifiedCleanup is the structural half of -// this package's leak defense. -// -// verifiedShutdownCleanup only helps the fixtures that call it, and the leak -// it exists to stop (wy-j2zc8q) came back three times precisely because the -// cleanup idiom was copy-pasted per fixture: nine near-identical t.Cleanup -// blocks, any of which could be written slightly differently — or omitted — -// in a tenth fixture without anything noticing until a dev box's process -// table showed a detached sql-server serving a directory deleted hours -// earlier. So the rule is checked against this package's own source. -// -// Three rules, over the functions that take a *testing.T (only those can -// register a cleanup at all — a harness METHOD like openProvider reuses a -// root whose owner already registered one, so it is not the place to look): -// -// A. shutdownOnInterrupt and verifiedShutdownCleanup come as a pair. They -// cover the two ways a fixture's server outlives it — a signal, and a -// returning run — and a fixture copied from another with one half -// dropped is the realistic regression. -// B. Only the two sanctioned helpers call proxy.Shutdown; every fixture -// goes through verifiedShutdownCleanup, which also asserts the pid died. -// C. A function that calls a provider constructor registers the cleanup, -// unless its NAME ends in validationErrorMarker — an explicit, greppable -// allowlist rather than an inference from the body, which a fixture that -// provisions a real server and also asserts an error could dodge. -// -// What it does NOT catch: a fixture that provisions a workspace through some -// future path this file does not name. That is what the non-vacuity counters -// at the bottom are for — they fail if a rule stopped matching anything, -// rather than letting the guard go quietly green on a package it no longer -// understands. -func TestEveryServerFixtureRegistersVerifiedCleanup(t *testing.T) { - fset := token.NewFileSet() - pkgs, err := parser.ParseDir(fset, ".", func(fi fs.FileInfo) bool { - return strings.HasSuffix(fi.Name(), "_test.go") - }, 0) - if err != nil { - t.Fatalf("parse package sources: %v", err) - } - if len(pkgs) == 0 { - t.Fatal("no test sources parsed; every rule below would pass vacuously") - } - - var paired, provisioning, exempted int - for _, pkg := range pkgs { - for path, file := range pkg.Files { - base := filepath.Base(path) - for _, decl := range file.Decls { - fn, ok := decl.(*ast.FuncDecl) - if !ok { - continue - } - name := fn.Name.Name - calls := calledNames(fn) - - // Rule B applies to every function, helper or not. - if calls["Shutdown"] && !shutdownCallers[name] { - t.Errorf("%s: %s calls proxy.Shutdown directly; use verifiedShutdownCleanup(t, storeRootDir), "+ - "which also asserts the dolt sql-server pid is gone (wy-j2zc8q)", base, name) - } - if !takesTestingT(fn) || shutdownCallers[name] { - continue - } - - // Rule A. - switch { - case calls["shutdownOnInterrupt"] && !calls["verifiedShutdownCleanup"]: - t.Errorf("%s: %s guards a store root with shutdownOnInterrupt but never registers "+ - "verifiedShutdownCleanup(t, storeRootDir); its dolt sql-server survives a run that "+ - "ends normally (wy-j2zc8q)", base, name) - case calls["verifiedShutdownCleanup"] && !calls["shutdownOnInterrupt"]: - t.Errorf("%s: %s registers verifiedShutdownCleanup but never calls "+ - "shutdownOnInterrupt(t, storeRootDir); its dolt sql-server survives Ctrl-C (wy-j2zc8q)", - base, name) - case calls["shutdownOnInterrupt"]: - paired++ - } - - // Rule C. - if !callsAny(calls, providerConstructors) { - continue - } - if strings.HasSuffix(name, validationErrorMarker) { - // The marker must not be a lie: a function claiming it - // has to actually assert a rejection, and must assert - // nothing SUCCEEDED — a rejected constructor leaves no - // live server, so there is nothing to NoError over. - if !callsAny(calls, errorAssertions) { - t.Errorf("%s: %s is named %q but never asserts the constructor rejected its "+ - "arguments (no require.Error/ErrorContains); drop the marker and register "+ - "verifiedShutdownCleanup(t, storeRootDir) (wy-j2zc8q)", - base, name, validationErrorMarker) - } - if callsAny(calls, successAssertions) { - t.Errorf("%s: %s wears the %q exemption but calls require.NoError/assert.NoError; "+ - "a fixture with something that can succeed is provisioning, not validating — "+ - "drop the marker and register verifiedShutdownCleanup(t, storeRootDir) (wy-j2zc8q)", - base, name, validationErrorMarker) - } - exempted++ - continue - } - provisioning++ - if !calls["verifiedShutdownCleanup"] { - t.Errorf("%s: %s provisions a shared-server workspace but never calls "+ - "verifiedShutdownCleanup(t, storeRootDir); a detached dolt sql-server will outlive "+ - "the run (wy-j2zc8q)", base, name) - } - } - } - } - - // Non-vacuity. Each rule is an assertion about functions that exist; if - // the fixtures were renamed out from under the name sets above, every - // loop body would stop running and the guard would pass while watching - // nothing. - if paired == 0 { - t.Fatal("no function pairs shutdownOnInterrupt with verifiedShutdownCleanup; rule A matched nothing") - } - if provisioning == 0 { - t.Fatalf("no *testing.T function calls any of %v; rule C is watching constructors this package "+ - "no longer uses", sortedKeys(providerConstructors)) - } - // Note there is deliberately NO non-vacuity floor on `exempted`: if the - // marker stops matching, rule C gets STRICTER, and the validation-error - // fixtures fail it loudly rather than anything going quietly green. The - // census is logged so a shrinking numerator is visible in -v output. - t.Logf("rule A paired %d fixture(s); rule C checked %d provisioning function(s), exempted %d marked %q", - paired, provisioning, exempted, validationErrorMarker) -} - -// takesTestingT reports whether fn accepts a *testing.T. Only such a function -// can register a t.Cleanup, so only such a function can be held to the rules -// above: openProvider and friends are methods on a harness whose constructor -// already owns the root's cleanup. -func takesTestingT(fn *ast.FuncDecl) bool { - if fn.Type.Params == nil { - return false - } - for _, field := range fn.Type.Params.List { - star, ok := field.Type.(*ast.StarExpr) - if !ok { - continue - } - sel, ok := star.X.(*ast.SelectorExpr) - if !ok { - continue - } - pkg, ok := sel.X.(*ast.Ident) - if ok && pkg.Name == "testing" && sel.Sel.Name == "T" { - return true - } - } - return false -} - -// calledNames returns the set of function names called anywhere in fn's body, -// by their final identifier: both `Shutdown` from `proxy.Shutdown(...)` and -// the bare `verifiedShutdownCleanup(...)`. Calls inside closures — the -// t.Run(…, func(t *testing.T){…}) subtests these fixtures use — are included, -// and attributed to the enclosing declaration. -// -// Matching the selector's last segment rather than the full `proxy.Shutdown` -// keeps the guard working if the import is ever aliased, at the price of also -// matching some other package's `Shutdown` — a false positive worth having in -// a rule about not shutting servers down by hand. -func calledNames(fn *ast.FuncDecl) map[string]bool { - names := make(map[string]bool) - ast.Inspect(fn, func(n ast.Node) bool { - call, ok := n.(*ast.CallExpr) - if !ok { - return true - } - switch f := call.Fun.(type) { - case *ast.Ident: - names[f.Name] = true - case *ast.SelectorExpr: - names[f.Sel.Name] = true - } - return true - }) - return names -} - -func callsAny(calls, want map[string]bool) bool { - for name := range want { - if calls[name] { - return true - } - } - return false -} - -func sortedKeys(m map[string]bool) []string { - out := make([]string, 0, len(m)) - for k := range m { - out = append(out, k) - } - sort.Strings(out) - return out -} diff --git a/internal/storage/uow/shared_server_cleanup_test.go b/internal/storage/uow/shared_server_cleanup_test.go deleted file mode 100644 index 0f12a85a2..000000000 --- a/internal/storage/uow/shared_server_cleanup_test.go +++ /dev/null @@ -1,175 +0,0 @@ -package uow - -import ( - "testing" - "time" - - "github.com/steveyegge/beads/internal/procid" - "github.com/steveyegge/beads/internal/storage/dbproxy/pidfile" - "github.com/steveyegge/beads/internal/storage/dbproxy/proxy" - "github.com/steveyegge/beads/internal/storage/dbproxy/server" -) - -// verifiedShutdownCleanup is the ONE cleanup every fixture in this package -// that provisions a shared-server workspace registers. It stops the proxy and -// the `dolt sql-server` it manages, and — when the stop did not report clean — -// asserts the recorded server PID is actually gone. -// -// Each of those fixtures used to inline -// -// t.Cleanup(func() { -// if err := proxy.Shutdown(storeRootDir); err != nil { -// t.Logf("proxy.Shutdown(%s): %v", storeRootDir, err) -// } -// }) -// -// which stops the server on the happy path and, on the unhappy one, prints a -// line into a passing test's output and moves on. That is how this package -// produced detached sql-servers still serving temp trees that had been -// deleted hours earlier (wy-j2zc8q): `proxy.Shutdown` has several documented -// refusals — an unverifiable process record, a lock it could not acquire -// inside its 5s budget, a spawn still in flight — and every one of them left a -// live daemon behind a green run. Nine copies of the same silent handler also -// meant a tenth fixture could be written without one and nothing would notice. -// -// Pair it with shutdownOnInterrupt, which covers the SIGINT/SIGTERM path that -// skips t.Cleanup entirely. -func verifiedShutdownCleanup(t *testing.T, storeRootDir string) { - t.Helper() - t.Cleanup(func() { - // Read the backend's record BEFORE shutting down: a successful - // proxy.Shutdown removes the pid file, so afterwards there is - // nothing left to verify against. - record := backendServerRecord(t, storeRootDir) - err := proxy.Shutdown(storeRootDir) - if err == nil { - // Shutdown killed the backend AND confirmed its exit - // (waitForRecordedProcessExit) before removing the record. - // There is nothing left to check. - return - } - t.Logf("proxy.Shutdown(%s): %v", storeRootDir, err) - requireBackendExited(t, record) - }) -} - -const ( - // backendExitTimeout is how long a test waits for the dolt sql-server to - // leave the process table after proxy.Shutdown failed. It is generous on - // purpose: Shutdown's own budget is already 5s of confirmation plus a 2s - // post-kill minimum, and this only runs on the error path, where the - // interesting outcome is "still there", not "took a while". - backendExitTimeout = 30 * time.Second - backendExitPoll = 50 * time.Millisecond -) - -// backendServerRecord returns the pid file the proxy's child manager wrote -// for the dolt sql-server under rootDir (server.PIDFileName). nil means there -// is nothing to verify: no server was started, or it shut down cleanly and -// removed its record. -// -// The whole record is returned, not just the PID: Birth is the process-birth -// token that makes the PID safe to act on at all. -func backendServerRecord(t *testing.T, rootDir string) *pidfile.PidFile { - t.Helper() - pf, err := pidfile.Read(rootDir, server.PIDFileName) - if err != nil { - t.Logf("read %s in %s: %v", server.PIDFileName, rootDir, err) - return nil - } - if pf == nil || pf.Pid <= 0 { - return nil - } - return pf -} - -// requireBackendExited fails the test if the recorded dolt sql-server is -// still running after proxy.Shutdown returned an ERROR, then force-kills it -// so the leak does not outlive the run. -// -// It is deliberately a t.Errorf and not a t.Logf: a surviving server holds -// the temp tree the test is about to delete, which is precisely how this -// package produced sql-servers still serving directories that had been gone -// for hours (wy-j2zc8q). A leak must be visible where it is caused, not -// discovered later by a sweep. -// -// Every step is gated on procid birth identity rather than the bare PID. The -// server is not this process's child, so its PID is reusable the instant it -// exits, and proxy.Shutdown deliberately REFUSES to signal a record it cannot -// verify (ErrUnverifiableProcess) — killing that same PID here would defeat -// the exact protection the package went out of its way to provide. A record -// with no Birth token, or one whose token no longer matches, is therefore -// left alone: not the server we recorded, nothing to report. -func requireBackendExited(t *testing.T, pf *pidfile.PidFile) { - t.Helper() - if pf == nil || pf.Pid <= 0 { - return - } - if pf.Birth == "" { - // A record from before birth tokens were written. There is no safe - // way to ask about that PID, so the check is skipped — say so, rather - // than looking like a silent pass. - t.Logf("backend record for pid %d carries no birth token; skipping the survived-Shutdown check", pf.Pid) - return - } - token := procid.Token(pf.Birth) - - deadline := time.Now().Add(backendExitTimeout) - for { - same, err := procid.Verify(pf.Pid, token) - if err != nil { - t.Logf("procid.Verify(%d): %v", pf.Pid, err) - return - } - if !same { - // Either the process is gone, or the PID now belongs to - // something else. Either way our server is not running. - return - } - if time.Now().After(deadline) { - break - } - time.Sleep(backendExitPoll) - } - - handle, err := procid.Open(pf.Pid, token) - if err != nil { - if backendStillRunning(t, pf.Pid, token) { - t.Errorf("dolt sql-server pid %d survived Shutdown by more than %s and could not be opened to kill: %v", - pf.Pid, backendExitTimeout, err) - } - return - } - killErr := handle.Kill() - _ = handle.Close() - if killErr != nil { - if backendStillRunning(t, pf.Pid, token) { - t.Errorf("dolt sql-server pid %d survived Shutdown by more than %s and could not be killed: %v", - pf.Pid, backendExitTimeout, killErr) - } - return - } - t.Errorf("dolt sql-server pid %d survived Shutdown by more than %s (force-killed)", pf.Pid, backendExitTimeout) -} - -// backendStillRunning re-checks birth identity after procid.Open or -// Handle.Kill failed, and reports whether the recorded server is verifiably -// still there. -// -// Both calls verify the token themselves and return a plain "does not match -// token" error when it no longer does — which is exactly what a process that -// exited between the wait loop's last poll and the kill attempt produces, and -// that error is NOT procid.IsProcessGone. Without this second look, a server -// that shut down a few milliseconds late would be reported as one that -// survived Shutdown entirely. A verify that itself errors reports "not -// running": the run is over, and an inconclusive probe is not evidence of a -// leak. -func backendStillRunning(t *testing.T, pid int, token procid.Token) bool { - t.Helper() - same, err := procid.Verify(pid, token) - if err != nil { - t.Logf("procid.Verify(%d) after a failed kill attempt: %v", pid, err) - return false - } - return same -} diff --git a/internal/storage/uow/stats_reporter_contract_test.go b/internal/storage/uow/stats_reporter_contract_test.go index 327532b6b..36f0ed049 100644 --- a/internal/storage/uow/stats_reporter_contract_test.go +++ b/internal/storage/uow/stats_reporter_contract_test.go @@ -27,12 +27,6 @@ func TestStatsReporterContract(t *testing.T) { t.Run("ExcludesTheWispTier", func(t *testing.T) { conformance.RunStatsReporterExcludesTheWispTier(t, ctx, fixture) }) - t.Run("BreaksOutTheRowsTheDefaultListingSuppresses", func(t *testing.T) { - conformance.RunStatsReporterBreaksOutTheRowsTheDefaultListingSuppresses(t, ctx, fixture) - }) - t.Run("BreaksOutAGateThatIsAlsoATemplate", func(t *testing.T) { - conformance.RunStatsReporterBreaksOutAGateThatIsAlsoATemplate(t, ctx, fixture) - }) t.Run("AStatusOutsideTheTalliesIsCountedOnlyInTotal", func(t *testing.T) { conformance.RunStatsReporterAStatusOutsideTheTalliesIsCountedOnlyInTotal(t, ctx, fixture) }) @@ -66,9 +60,6 @@ func TestStatsReporterContract(t *testing.T) { t.Run("AssigneeStatsMergesTheWispTier", func(t *testing.T) { conformance.RunStatsReporterAssigneeStatsMergesTheWispTier(t, ctx, fixture) }) - t.Run("AssigneeStatsBreaksOutTheSuppressedRows", func(t *testing.T) { - conformance.RunStatsReporterAssigneeStatsBreaksOutTheSuppressedRows(t, ctx, fixture) - }) t.Run("AssigneeStatsPopulatesBothPointers", func(t *testing.T) { conformance.RunStatsReporterAssigneeStatsPopulatesBothPointers(t, ctx, fixture) }) diff --git a/internal/storage/uow/team_server_integration_test.go b/internal/storage/uow/team_server_integration_test.go index 20615be64..5ce079653 100644 --- a/internal/storage/uow/team_server_integration_test.go +++ b/internal/storage/uow/team_server_integration_test.go @@ -39,7 +39,11 @@ func newTeamServerHarness(t *testing.T) *teamServerHarness { storeRootDir := t.TempDir() shutdownOnInterrupt(t, storeRootDir) - verifiedShutdownCleanup(t, storeRootDir) + t.Cleanup(func() { + if err := proxy.Shutdown(storeRootDir); err != nil { + t.Logf("proxy.Shutdown(%s): %v", storeRootDir, err) + } + }) return &teamServerHarness{ port: portInt, @@ -241,7 +245,7 @@ func TestTeamServerMode_Integration(t *testing.T) { require.Error(t, err) assert.Nil(t, p) assert.Contains(t, err.Error(), "bts migrate") - assert.NotContains(t, err.Error(), "bd migrate schema", + assert.NotContains(t, err.Error(), "run any bd write command", "must not suggest bd-driven migration on a bts-owned schema") assert.Equal(t, rolledBack, snapshotMigrations(t, ctx, direct), diff --git a/internal/storage/uow/team_server_schema.go b/internal/storage/uow/team_server_schema.go index f91ca3b0d..d83aca3e1 100644 --- a/internal/storage/uow/team_server_schema.go +++ b/internal/storage/uow/team_server_schema.go @@ -26,7 +26,7 @@ func checkTeamServerSchema(ctx context.Context, conn schema.DBConn, database str case current < latest: // No BD_IGNORE_SCHEMA_SKEW hatch here: it would let a newer bd write // against an older bts schema. Not SchemaBehindError either: its "run - // `bd migrate schema`" advice is wrong for a bts-owned schema. + // any bd write command to migrate" advice is wrong for a bts-owned schema. return fmt.Errorf( "uow: database %q is at schema v%d, this bd expects v%d; the schema is managed by beads-team-server — ask your operator to run 'bts migrate', or use a bd built against schema v%d", database, current, latest, current) diff --git a/internal/storage/uow/team_server_schema_test.go b/internal/storage/uow/team_server_schema_test.go index 9c45f9dad..fb35bcb56 100644 --- a/internal/storage/uow/team_server_schema_test.go +++ b/internal/storage/uow/team_server_schema_test.go @@ -6,7 +6,6 @@ import ( "errors" "io" "os" - "regexp" "strings" "testing" @@ -20,13 +19,13 @@ import ( // table. The probe was added by be-bv7x so a not-yet-created cursor table // never poisons the pooled Dolt session with a failing statement. func expectCursorProbe(mock sqlmock.Sqlmock, table string, exists bool) { - mock.ExpectQuery(regexp.QuoteMeta("SELECT COUNT(*) FROM information_schema.schemata WHERE schema_name = DATABASE()")). - WillReturnRows(sqlmock.NewRows([]string{"count"}).AddRow(1)) - rows := sqlmock.NewRows([]string{"Tables_in_beads"}).AddRow("issues") + present := 0 if exists { - rows.AddRow(table) + present = 1 } - mock.ExpectQuery("^SHOW TABLES$").WillReturnRows(rows) + mock.ExpectQuery(`SELECT COUNT\(\*\) FROM information_schema\.tables`). + WithArgs(table). + WillReturnRows(sqlmock.NewRows([]string{"count"}).AddRow(present)) } func expectVersionQuery(mock sqlmock.Sqlmock, version int) { @@ -109,7 +108,7 @@ func TestCheckTeamServerSchema_Behind_RefusesWithBtsMigrate(t *testing.T) { if !strings.Contains(err.Error(), "bts migrate") { t.Errorf("error %q should point at 'bts migrate'", err) } - if strings.Contains(err.Error(), "bd migrate schema") { + if strings.Contains(err.Error(), "run any bd write command") { t.Errorf("error %q must not suggest bd-driven migration on a bts-owned schema", err) } } diff --git a/internal/storage/uow/testmain_test.go b/internal/storage/uow/testmain_test.go deleted file mode 100644 index 880451b46..000000000 --- a/internal/storage/uow/testmain_test.go +++ /dev/null @@ -1,85 +0,0 @@ -package uow - -import ( - "fmt" - "os" - "testing" - - "github.com/steveyegge/beads/internal/doltserver" - "github.com/steveyegge/beads/internal/storage/schema" - "github.com/steveyegge/beads/internal/testutil" -) - -// suiteRootPrefix is this suite's os.MkdirTemp pattern without its random -// tail. SweepDeadSuiteRoots globs for it, so the two must not drift. -const suiteRootPrefix = "beads-storage-uow-tests-" - -// suiteTempRoot is the TestMain-owned temp directory that every t.TempDir() -// in this package lands under, so SweepSuiteTestServers has a root it can -// vouch for when it reaps leaked dolt sql-servers. -var suiteTempRoot string - -// TestMain gives this package the suite-lifecycle it was missing. -// -// The integration tests here (newTestUOWProvider) start a real proxied -// `dolt sql-server` whose data directory is a t.TempDir(). Cleanup was purely -// per-test — proxy.Shutdown in a t.Cleanup — and there was no TestMain at -// all, so a run killed by `go test -timeout` (which panics the binary and -// skips every Cleanup) left the server running with its data directory -// deleted out from under it, forever. Observed live on a dev box as a -// TestUOWDependencyEditorContract server still serving a temp dir that had -// been gone for hours (wy-j2zc8q). -func TestMain(m *testing.M) { - os.Exit(testMainInner(m)) -} - -func testMainInner(m *testing.M) int { - // Package-wide migration consent: these tests exercise the - // provider/bootstrap machinery that runs BELOW the consent gate - // (schema/migrate_consent.go), exactly as production does once an operator - // has consented. - schema.SetLocalMigrateConsent(true) - - // Clear out the roots of earlier runs of this suite whose process is - // gone, before claiming one of our own. Roots with no owner marker, and - // roots whose owner is still running (a parallel package under - // scripts/test.sh, a second `go test`), are left untouched. - doltserver.SweepDeadSuiteRoots(os.TempDir(), suiteRootPrefix) - - // Pin TMPDIR under a suite-owned root so every t.TempDir() — including - // the storeRootDir a provider serves — is nested under something the - // post-run sweep may vouch for. Same pattern as cmd/bd/doctor's TestMain. - root, pinErr := testutil.PinSuiteTempRoot(suiteRootPrefix + "*") - if pinErr != nil { - fmt.Fprintf(os.Stderr, "FATAL: suite temp root: %v\n", pinErr) - return 1 - } - suiteTempRoot = root - defer os.RemoveAll(root) - - // Claim the root for this process so the NEXT run can tell our debris - // from a concurrent run's live tree. - if err := doltserver.WriteSuiteOwnerMarker(root); err != nil { - fmt.Fprintf(os.Stderr, "Warning: could not claim suite temp root %s: %v\n", root, err) - } - - code := m.Run() - - // Best-effort reap of any dolt sql-server still running under this run's - // own root — the backstop for a test whose Cleanup did not get to run. - swept := doltserver.SweepSuiteTestServers(root) - return doltserver.ApplyLeakPolicy("internal/storage/uow", code, swept) -} - -// TestTempDirLandsUnderSuiteSweepRoot guards the pinning above: if t.TempDir() -// ever stops landing under suiteTempRoot, the post-run sweep silently loses -// its scope and leaked sql-servers become unreapable again. -func TestTempDirLandsUnderSuiteSweepRoot(t *testing.T) { - if suiteTempRoot == "" { - t.Fatal("TestMain did not pin suiteTempRoot; leaked dolt sql-server processes cannot be swept") - } - dir := t.TempDir() - if !testutil.PathUnderSuiteRoot(dir, suiteTempRoot) { - t.Fatalf("t.TempDir() %q is not under suiteTempRoot %q", dir, suiteTempRoot) - } -} diff --git a/internal/storage/uow/tx.go b/internal/storage/uow/tx.go index ba5b21b36..ba7e6af6e 100644 --- a/internal/storage/uow/tx.go +++ b/internal/storage/uow/tx.go @@ -8,7 +8,6 @@ import ( "github.com/steveyegge/beads/internal/storage/domain/db" "github.com/steveyegge/beads/internal/storage/issueops" - publicops "github.com/steveyegge/beads/issueops" ) type Tx interface { @@ -87,63 +86,11 @@ func RunTxResultWithin[T any](ctx context.Context, p UnitOfWorkProvider, maxElap bo.InitialInterval = txRetryInitialInterval bo.MaxElapsedTime = maxElapsed - // lastAttemptDuration and lastErr let the loop decide, BEFORE starting a - // new attempt, whether there is realistically enough time left to finish - // one. maxElapsed is a BUDGET fixed once at the top of the call; it says - // nothing about how long any individual attempt actually takes. A slow - // attempt (a large apply's commit can run well past a minute) can lose - // the race against ctx's own absolute deadline mid-transaction: the retry - // loop would start another attempt because elapsed-so-far is still under - // maxElapsed, that attempt gets killed by ctx cancellation partway - // through, and the caller sees context.DeadlineExceeded — classified a - // generic 500 — instead of the exhausted-write-conflict outcome this loop - // already reports correctly when it gives up for its own reasons - // (classified a retryable 503). Comparing the time left to the PREVIOUS - // attempt's own duration is a cheap, evidence-based stand-in for - // predicting the next one, since a Dolt commit's cost is driven by the - // same plan on every attempt. - // - // This look-ahead is gated on publicops.HasExtendedRetryBudget(ctx), the - // same marker retryTxBudget itself checks, and for the same reason: an - // ordinary (unmarked) caller's attempts are cheap, and its ctx deadline - // is meant to be the thing that ends the loop — see - // TestApplyBatchHonorsAnExtendedRetryBudgetFromContext's "unmarked" - // case. Applying this check unconditionally would fire for those callers - // too: retryTxAttemptHeadroom is sized for a large apply's multi-minute - // commits, so for a near-instant failing attempt it would treat almost - // any ctx with less than retryTxAttemptHeadroom left as too tight, - // cutting the loop short well before ctx's own deadline and turning what - // should be ctx's DeadlineExceeded into a serialization error instead. - // Scoping this to the extended-budget (large apply) path keeps ordinary - // callers' retry-until-ctx-ends-it behavior exactly as before. - var lastAttemptDuration time.Duration - var lastErr error - haveAttempted := false - lookAhead := publicops.HasExtendedRetryBudget(ctx) - err := backoff.Retry(func() error { - if haveAttempted && lookAhead { - if deadline, ok := ctx.Deadline(); ok && time.Until(deadline) < lastAttemptDuration+retryTxAttemptHeadroom { - // A retry this long would almost certainly be cut off by - // ctx's own deadline rather than finish and report its own - // outcome. Stop now and surface the LAST attempt's real - // error — still a retryable conflict — rather than start a - // doomed attempt that ends in ctx.Err(). - return backoff.Permanent(lastErr) - } - } - attemptStart := time.Now() - fail := func(err error) error { - lastAttemptDuration = time.Since(attemptStart) - lastErr = err - haveAttempted = true - return err - } - uw, err := p.NewUOW(ctx) if err != nil { if isSerializationError(err) { - return fail(err) + return err } return backoff.Permanent(err) } @@ -152,7 +99,7 @@ func RunTxResultWithin[T any](ctx context.Context, p UnitOfWorkProvider, maxElap r, commitMsg, err := work(ctx, uw) if err != nil { if isSerializationError(err) { - return fail(err) + return err } return backoff.Permanent(err) } @@ -168,7 +115,7 @@ func RunTxResultWithin[T any](ctx context.Context, p UnitOfWorkProvider, maxElap return nil } if isSerializationError(err) { - return fail(err) + return err } return backoff.Permanent(err) } @@ -181,20 +128,19 @@ func RunTxResultWithin[T any](ctx context.Context, p UnitOfWorkProvider, maxElap } // RunTxEphemeral is RunTxResult for work whose writes touch ONLY ephemeral -// (dolt_ignored) state, which must mint no Dolt commit and no history -// (bd-aq0ql) — today local metadata and the events-journal prune. On success -// the attempt is committed with the SQL-only form (Tx.Commit with an empty -// message ⇒ plain COMMIT, no DOLT_COMMIT), which persists the working set but -// records nothing in dolt_log. A serialization loser is replayed against a -// fresh unit of work, exactly like RunTxResult. +// (dolt_ignored) state — today the leases table, whose writes must mint no +// Dolt commit and no history (bd-lrgn1; ported to proxied mode by bd-aq0ql). +// On success the attempt is committed with the SQL-only form (Tx.Commit with +// an empty message ⇒ plain COMMIT, no DOLT_COMMIT), which persists the +// working set but records nothing in dolt_log — the proxied analog of the +// classic DoltStore.withRetryTx commit discipline that HeartbeatIssue relies +// on. A serialization loser (heartbeat and reclaim/close contend on the same +// lease row by design) is replayed against a fresh unit of work, exactly like +// RunTxResult. // // Do NOT use this for work that writes versioned tables: those writes would // persist while silently bypassing Dolt history. Every versioned write goes -// through RunTx/RunTxResult with a real commit message. A heartbeat is NOT -// ephemeral work: a lease refresh writes only the dolt_ignored leases table -// (bd-lrgn1), but a re-arm writes versioned rows, so the Heartbeater commits -// through RunTxScoped, whose attempt that names no tables takes this same -// plain COMMIT. +// through RunTx/RunTxResult with a real commit message. func RunTxEphemeral[T any](ctx context.Context, p UnitOfWorkProvider, work TxReadFunc[T]) (T, error) { var result T bo := backoff.NewExponentialBackOff() @@ -233,99 +179,6 @@ func RunTxEphemeral[T any](ctx context.Context, p UnitOfWorkProvider, work TxRea return result, err } -// ScopedCommitter is the optional commit a unit of work offers for a write -// whose versioned tables are KNOWN: it commits the SQL transaction, then -// stages exactly tables (plus the versioned-history tables when the -// transaction could have minted) and makes a Dolt commit named message from -// that staged set alone, leaving another writer's uncommitted change to any -// other table in the working set where it was. A staged table is committed -// whole, so another writer's uncommitted change to one of those tables rides -// in this commit, as it does in the DoltStore funnel. With nothing staged, or -// under a deferred version commit, there is no Dolt commit. It is the uow -// twin of the DoltStore funnel's post-transaction DOLT_ADD-per-table commit, -// in the same order — the Dolt commit after the SQL COMMIT, never inside the -// transaction, where it would be built from the transaction's start snapshot -// (LatentLabsSpace/NEXUS#92) — and what a mutation that mostly touches -// dolt_ignored state (a heartbeat re-arm) commits through instead of -// DOLT_COMMIT('-Am'). An error means the SQL transaction did not commit; a -// Dolt commit that fails after it is logged, never returned. -type ScopedCommitter interface { - CommitScoped(ctx context.Context, tables []string, message string) error -} - -// commitScoped commits uw through the first ScopedCommitter on its decorator -// chain. A backend whose unit of work has none (a non-Dolt engine) gets the -// plain SQL COMMIT: there is no version control to scope. -func commitScoped(ctx context.Context, uw UnitOfWork, tables []string, message string) error { - for u := uw; u != nil; { - if scoped, ok := u.(ScopedCommitter); ok { - return scoped.CommitScoped(ctx, tables, message) - } - unwrapper, ok := u.(unitOfWorkUnwrapper) - if !ok { - break - } - u = unwrapper.Unwrap() - } - return uw.Commit(ctx, "") -} - -// TxScopedFunc is the work RunTxScoped runs: its result, and the versioned -// tables the attempt changed with the commit message naming them. No tables -// means the attempt wrote only dolt_ignored state (or nothing). -type TxScopedFunc[T any] func(ctx context.Context, uw UnitOfWork) (result T, tables []string, commitMsg string, err error) - -// RunTxScoped is RunTxEphemeral for work that is USUALLY ephemeral but -// sometimes writes versioned rows it can name: an attempt that reports no -// tables commits with the plain SQL COMMIT (no working-set scan, no Dolt -// commit, nothing swept); one that reports tables commits through -// ScopedCommitter, staging exactly those. Never DOLT_COMMIT('-Am'): that -// would sweep other writers' uncommitted (batch-mode) changes to other tables -// into this commit (a staged table is committed whole). Serialization losers -// are replayed like RunTxEphemeral's. -func RunTxScoped[T any](ctx context.Context, p UnitOfWorkProvider, work TxScopedFunc[T]) (T, error) { - var result T - bo := backoff.NewExponentialBackOff() - bo.InitialInterval = txRetryInitialInterval - bo.MaxElapsedTime = DefaultTxRetryMaxElapsed - - err := backoff.Retry(func() error { - uw, err := p.NewUOW(ctx) - if err != nil { - if isSerializationError(err) { - return err - } - return backoff.Permanent(err) - } - defer closeAttempt(ctx, uw) - - r, tables, msg, err := work(ctx, uw) - if err != nil { - if isSerializationError(err) { - return err - } - return backoff.Permanent(err) - } - - if len(tables) == 0 || msg == "" { - err = uw.Commit(ctx, "") - } else { - err = commitScoped(ctx, uw, tables, msg) - } - if err != nil { - if isSerializationError(err) { - return err - } - return backoff.Permanent(err) - } - - result = r - return nil - }, backoff.WithContext(bo, ctx)) - - return result, err -} - func RunTxRead[T any](ctx context.Context, p UnitOfWorkProvider, work TxReadFunc[T]) (T, error) { var zero T uw, err := p.NewUOW(ctx) diff --git a/internal/storage/uow/tx_test.go b/internal/storage/uow/tx_test.go index 0e3f3ac8a..61bd9d013 100644 --- a/internal/storage/uow/tx_test.go +++ b/internal/storage/uow/tx_test.go @@ -10,17 +10,11 @@ import ( "github.com/go-sql-driver/mysql" "github.com/steveyegge/beads/internal/storage/domain" - publicops "github.com/steveyegge/beads/issueops" ) // mockUnitOfWork implements UnitOfWork for testing type mockUnitOfWork struct { - commitErr error - // commitDelay, when set, is slept inside Commit before returning - // commitErr — lets a test simulate an attempt that takes real wall-clock - // time to fail (a slow commit racing a context deadline), as opposed to - // commitErr alone, which fails instantly. - commitDelay time.Duration + commitErr error commitCount int closed bool configUseCase domain.ConfigUseCase @@ -42,9 +36,6 @@ func (m *mockUnitOfWork) Close(ctx context.Context) { func (m *mockUnitOfWork) Commit(ctx context.Context, message string) error { m.commitCount++ - if m.commitDelay > 0 { - time.Sleep(m.commitDelay) - } return m.commitErr } @@ -512,90 +503,6 @@ func TestRunTx_NewUOWError(t *testing.T) { } } -// TestRunTxResultWithin_SlowAttemptStopsBeforeADoomedRetry pins the fix for -// the gap bee's live 1000-item run exposed: retryTxBudget / maxElapsed is a -// BUDGET fixed once at the top of the call, and says nothing about how long -// any one attempt actually takes. Before this fix, a slow failing attempt -// (its commit loses Dolt's merge after running most of the remaining ctx -// deadline) still triggered a retry whenever elapsed-so-far was under -// maxElapsed — and that retry then got killed mid-transaction by ctx's own -// deadline, surfacing as context.DeadlineExceeded (an internal-httpapi -// isUnavailable() miss -> a generic 500) instead of the exhausted-write- -// conflict outcome this loop already reports correctly when IT gives up -// (uow.IsSerializationError -> httpapi maps that to 503 + Retry-After). -// -// The first attempt's commit sleeps 1.2s (over half the 2s ctx) before -// failing with a retryable deadlock; with retryTxAttemptHeadroom (5s) added -// on top, the ~0.8s left after that attempt can never cover another attempt -// of similar cost, so the loop must stop WITHOUT trying uw2 at all and return -// uw1's own serialization error. -// -// The look-ahead this pins is gated on publicops.HasExtendedRetryBudget, the -// same marker retryTxBudget checks (see that gate's comment in tx.go), so -// this ctx must carry it — an unmarked ctx would skip the new check entirely -// and this test would regress to asserting old, unfixed behavior. -func TestRunTxResultWithin_SlowAttemptStopsBeforeADoomedRetry(t *testing.T) { - uw1 := &mockUnitOfWork{commitErr: newMySQLError(1213), commitDelay: 1200 * time.Millisecond} - uw2 := &mockUnitOfWork{} - provider := &mockUnitOfWorkProvider{uows: []*mockUnitOfWork{uw1, uw2}} - - ctx, cancel := context.WithTimeout(publicops.WithExtendedRetryBudget(context.Background()), 2*time.Second) - defer cancel() - - _, err := RunTxResultWithin(ctx, provider, 10*time.Second, func(ctx context.Context, uw UnitOfWork) (struct{}, string, error) { - return struct{}{}, "slow attempt probe", nil - }) - - if err == nil { - t.Fatal("expected an error: the only attempt tried fails on commit") - } - if !IsSerializationError(err) { - t.Errorf("err = %v, want uw1's own serialization error (-> 503 Retry-After); a ctx-cancellation error here would be the regression (-> a generic 500)", err) - } - if errors.Is(err, context.DeadlineExceeded) { - t.Errorf("err = %v wraps context.DeadlineExceeded: a doomed second attempt was started and got cut off by ctx instead of being skipped", err) - } - if provider.newUOWCalls != 1 { - t.Errorf("NewUOW calls = %d, want exactly 1: a second attempt was started even though it could not have finished before ctx's deadline", provider.newUOWCalls) - } - if uw2.commitCount != 0 { - t.Errorf("uw2.commitCount = %d, want 0: uw2 must never be reached", uw2.commitCount) - } -} - -// TestRunTxResultWithin_ShortAttemptStillRetries is -// TestRunTxResultWithin_SlowAttemptStopsBeforeADoomedRetry's control: when the -// failing attempt is fast relative to what's left on ctx, the new -// look-before-you-retry check must not block the retry that would otherwise -// succeed. -func TestRunTxResultWithin_ShortAttemptStillRetries(t *testing.T) { - uw1 := &mockUnitOfWork{commitErr: newMySQLError(1213), commitDelay: 50 * time.Millisecond} - uw2 := &mockUnitOfWork{} - provider := &mockUnitOfWorkProvider{uows: []*mockUnitOfWork{uw1, uw2}} - - // 8s comfortably exceeds retryTxAttemptHeadroom (5s) by more than the 50ms - // first attempt costs, so the real-time budget this test takes is - // governed by how fast the two attempts actually run (well under a - // second), not by this deadline. Marked for the same reason as the slow- - // attempt test above: the look-ahead only runs when this marker is set. - ctx, cancel := context.WithTimeout(publicops.WithExtendedRetryBudget(context.Background()), 8*time.Second) - defer cancel() - - _, err := RunTxResultWithin(ctx, provider, 15*time.Second, func(ctx context.Context, uw UnitOfWork) (struct{}, string, error) { - return struct{}{}, "short attempt probe", nil - }) - - if err != nil { - t.Fatalf("expected no error after retry, got %v", err) - } - if provider.newUOWCalls < 2 { - t.Errorf("NewUOW calls = %d, want at least 2: a short failed attempt must still be retried", provider.newUOWCalls) - } - if uw2.commitCount != 1 { - t.Errorf("uw2.commitCount = %d, want 1", uw2.commitCount) - } -} - func TestRunTx_WorkSerializationErrorRetries(t *testing.T) { // Work function itself returns serialization error (not commit) uw1 := &mockUnitOfWork{} diff --git a/internal/storage/uow/uow.go b/internal/storage/uow/uow.go index ab69586c5..a141fa5bb 100644 --- a/internal/storage/uow/uow.go +++ b/internal/storage/uow/uow.go @@ -55,20 +55,6 @@ func (u *baseUOW) Commit(ctx context.Context, message string) error { return u.tx.Commit(ctx, message) } -// CommitScoped commits through the transaction's scoped commit when it has -// one (the Dolt server transaction), else with the plain SQL COMMIT. -func (u *baseUOW) CommitScoped(ctx context.Context, tables []string, message string) error { - if scoped, ok := u.tx.(scopedCommitTx); ok { - return scoped.CommitScoped(ctx, tables, message) - } - return u.tx.Commit(ctx, "") -} - -// scopedCommitTx is the optional Tx half of ScopedCommitter. -type scopedCommitTx interface { - CommitScoped(ctx context.Context, tables []string, message string) error -} - func (u *baseUOW) Close(ctx context.Context) { u.tx.RollbackUnlessCommitted(ctx) } diff --git a/internal/storage/uow/upstream_outage_window_test.go b/internal/storage/uow/upstream_outage_window_test.go deleted file mode 100644 index c1cdcf20e..000000000 --- a/internal/storage/uow/upstream_outage_window_test.go +++ /dev/null @@ -1,215 +0,0 @@ -package uow - -import ( - "context" - "encoding/binary" - "io" - "net" - "sync" - "sync/atomic" - "testing" - "time" - - "github.com/go-sql-driver/mysql" - - "github.com/steveyegge/beads/internal/storage/dbproxy/proxy" -) - -func proxyOutageErr() error { - return &mysql.MySQLError{Number: proxy.UpstreamErrorNumber, Message: proxy.UpstreamErrorPrefix + "upstream Dolt server unreachable: dial tcp 127.0.0.1:1: connection refused"} -} - -func shortOutageWindow(t *testing.T, d time.Duration) { - t.Helper() - prev := upstreamOutageRetryWindow - upstreamOutageRetryWindow = d - t.Cleanup(func() { upstreamOutageRetryWindow = prev }) -} - -func TestPingWithRetryRetriesTheProxyOutageReportOnlyBriefly(t *testing.T) { - t.Run("upstream back inside the window recovers", func(t *testing.T) { - shortOutageWindow(t, 2*time.Second) - p := &fakePinger{t: t, errs: []error{proxyOutageErr(), proxyOutageErr(), nil}} - if err := pingWithRetry(context.Background(), p, testPingBackOff(), testPingAttemptTimeout); err != nil { - t.Fatalf("pingWithRetry() error = %v, want recovery inside the outage window", err) - } - if p.calls != 3 { - t.Fatalf("PingContext called %d times, want 3", p.calls) - } - }) - - t.Run("outage for the whole window fails at its end", func(t *testing.T) { - const window = 300 * time.Millisecond - shortOutageWindow(t, window) - var calls atomic.Int32 - p := pingerFunc(func(context.Context) error { calls.Add(1); return proxyOutageErr() }) - bo := testPingBackOff() - bo.InitialInterval = 50 * time.Millisecond - bo.MaxElapsedTime = 30 * time.Second // the full bootstrap budget must not apply - start := time.Now() - err := pingWithRetry(context.Background(), p, bo, testPingAttemptTimeout) - elapsed := time.Since(start) - if !proxy.IsUpstreamOutageError(err) { - t.Fatalf("pingWithRetry() error = %v, want the proxy's outage report", err) - } - if elapsed < window || elapsed > window+500*time.Millisecond { - t.Fatalf("gave up after %s, want just past the %s window", elapsed, window) - } - if calls.Load() < 2 { - t.Fatalf("PingContext called %d times, want the report retried inside the window", calls.Load()) - } - }) - - t.Run("other transient errors keep the full budget", func(t *testing.T) { - shortOutageWindow(t, time.Nanosecond) - p := &fakePinger{t: t, errs: []error{mysql.ErrInvalidConn, mysql.ErrInvalidConn, nil}} - if err := pingWithRetry(context.Background(), p, testPingBackOff(), testPingAttemptTimeout); err != nil { - t.Fatalf("pingWithRetry() error = %v, want nil", err) - } - }) -} - -type pingerFunc func(context.Context) error - -func (f pingerFunc) PingContext(ctx context.Context) error { return f(ctx) } - -// TestOpenDBRidesOutAnUpstreamThatReturnsInsideTheOutageWindow plays the db -// proxy in front of an external upstream that is briefly unreachable: the -// first connections get the proxy's real outage packet, and 300ms after the -// first one the upstream "comes back" (a minimal MySQL server answers). One -// openDB call — one bd command's bootstrap — must succeed without the caller -// retrying. -func TestOpenDBRidesOutAnUpstreamThatReturnsInsideTheOutageWindow(t *testing.T) { - front := newFlappingFront(t, 300*time.Millisecond) - ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) - defer cancel() - start := time.Now() - db, err := openDB(ctx, "root@tcp("+front.addr+")/?timeout=5s") - if err != nil { - t.Fatalf("openDB() error = %v, want recovery once the upstream returned", err) - } - _ = db.Close() - if front.refused.Load() == 0 { - t.Fatal("the command never hit the outage; the test proved nothing") - } - if elapsed := time.Since(start); elapsed > upstreamOutageRetryWindow+time.Second { - t.Fatalf("recovery took %s", elapsed) - } -} - -func TestOpenDBFailsFastWhenTheUpstreamStaysUnreachable(t *testing.T) { - front := newFlappingFront(t, time.Hour) - ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) - defer cancel() - start := time.Now() - _, err := openDB(ctx, "root@tcp("+front.addr+")/?timeout=5s") - elapsed := time.Since(start) - if !proxy.IsUpstreamOutageError(err) { - t.Fatalf("openDB() error = %v, want the proxy's outage report", err) - } - if elapsed > upstreamOutageRetryWindow+time.Second { - t.Fatalf("an unreachable upstream took %s to fail, want about %s", elapsed, upstreamOutageRetryWindow) - } -} - -// flappingFront answers with the db proxy's outage packet until downFor has -// passed since its first connection, then serves as a minimal MySQL server. -type flappingFront struct { - addr string - refused atomic.Int32 -} - -func newFlappingFront(t *testing.T, downFor time.Duration) *flappingFront { - t.Helper() - ln, err := net.Listen("tcp", "127.0.0.1:0") - if err != nil { - t.Fatal(err) - } - f := &flappingFront{addr: ln.Addr().String()} - var wg sync.WaitGroup - t.Cleanup(func() { _ = ln.Close(); wg.Wait() }) - var once sync.Once - var upAt time.Time - wg.Add(1) - go func() { - defer wg.Done() - for { - conn, err := ln.Accept() - if err != nil { - return - } - once.Do(func() { upAt = time.Now().Add(downFor) }) - if time.Now().Before(upAt) { - f.refused.Add(1) - _, _ = conn.Write(proxy.UpstreamOutagePacket("upstream Dolt server unreachable: dial tcp 127.0.0.1:1: connection refused")) - _ = conn.Close() - continue - } - wg.Add(1) - go func() { defer wg.Done(); serveMinimalMySQL(conn) }() - } - }() - return f -} - -// serveMinimalMySQL speaks just enough of the server side of the MySQL -// protocol for go-sql-driver to connect and ping: a greeting, OK to any -// authentication, and OK to every command until COM_QUIT or EOF. -func serveMinimalMySQL(conn net.Conn) { - defer conn.Close() - _ = conn.SetDeadline(time.Now().Add(10 * time.Second)) - const ( - clientLongPassword = 1 << 0 - clientProtocol41 = 1 << 9 - clientTransactions = 1 << 13 - clientSecureConn = 1 << 15 - clientPluginAuth = 1 << 19 - ) - caps := uint32(clientLongPassword | clientProtocol41 | clientTransactions | clientSecureConn | clientPluginAuth) - g := []byte{0x0a} - g = append(g, "8.0.0-fake\x00"...) - g = binary.LittleEndian.AppendUint32(g, 1) - g = append(g, "abcdefgh"...) // auth data part 1 - g = append(g, 0) - g = binary.LittleEndian.AppendUint16(g, uint16(caps)) - g = append(g, 0x21) // utf8_general_ci - g = binary.LittleEndian.AppendUint16(g, 0x0002) - g = binary.LittleEndian.AppendUint16(g, uint16(caps>>16)) - g = append(g, 21) - g = append(g, make([]byte, 10)...) - g = append(g, "ijklmnopqrst\x00"...) // auth data part 2 - g = append(g, "mysql_native_password\x00"...) - ok := []byte{0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00} - - write := func(seq byte, payload []byte) error { - hdr := []byte{byte(len(payload)), byte(len(payload) >> 8), byte(len(payload) >> 16), seq} - _, err := conn.Write(append(hdr, payload...)) - return err - } - read := func() (byte, []byte, error) { - var hdr [4]byte - if _, err := io.ReadFull(conn, hdr[:]); err != nil { - return 0, nil, err - } - body := make([]byte, int(hdr[0])|int(hdr[1])<<8|int(hdr[2])<<16) - _, err := io.ReadFull(conn, body) - return hdr[3], body, err - } - - if write(0, g) != nil { - return - } - seq, _, err := read() // handshake response - if err != nil || write(seq+1, ok) != nil { - return - } - for { - seq, body, err := read() - if err != nil || len(body) == 0 || body[0] == 0x01 { // EOF or COM_QUIT - return - } - if write(seq+1, ok) != nil { - return - } - } -} diff --git a/internal/storage/uow/version_reconcile_boot_test.go b/internal/storage/uow/version_reconcile_boot_test.go deleted file mode 100644 index 3ca80ee30..000000000 --- a/internal/storage/uow/version_reconcile_boot_test.go +++ /dev/null @@ -1,22 +0,0 @@ -package uow - -import ( - "errors" - "testing" -) - -func TestLocalDoltNeverListened(t *testing.T) { - t.Parallel() - if localDoltNeverListened(nil) { - t.Fatal("nil error is not a server that never listened") - } - if !localDoltNeverListened(errors.New("uow: ping db: dial tcp 127.0.0.1:33619: connect: connection refused")) { - t.Fatal("connection refused is a server that never listened") - } - if !localDoltNeverListened(errors.New("dolt sql-server exited before listener became ready")) { - t.Fatal("exit before listen is a server that never listened") - } - if localDoltNeverListened(errors.New("uow: init schema: duplicate column")) { - t.Fatal("a schema error is not a server that never listened") - } -} diff --git a/internal/storage/uow/version_reconcile_integration_test.go b/internal/storage/uow/version_reconcile_integration_test.go index 69226645b..7fb4ebda0 100644 --- a/internal/storage/uow/version_reconcile_integration_test.go +++ b/internal/storage/uow/version_reconcile_integration_test.go @@ -4,7 +4,6 @@ import ( "context" "os/exec" "path/filepath" - "strings" "testing" "github.com/stretchr/testify/require" @@ -15,43 +14,6 @@ import ( publicops "github.com/steveyegge/beads/issueops" ) -func openTestUOWProvider(t *testing.T, bin string) (UnitOfWorkProvider, error) { - t.Helper() - port, err := proxy.PickFreePort() - if err != nil { - return nil, err - } - storeRootDir := t.TempDir() - shutdownOnInterrupt(t, storeRootDir) - verifiedShutdownCleanup(t, storeRootDir) - cfgPath := writeServerConfig(t, port) - logPath := filepath.Join(t.TempDir(), "server.log") - return NewDoltServerUOWProvider( - context.Background(), - storeRootDir, - "beads", - logPath, - cfgPath, - proxy.BackendLocalServer, - "root", - "", - bin, - 0, - 0, - false, - "", - ) -} - -func localDoltNeverListened(err error) bool { - if err == nil { - return false - } - msg := err.Error() - return strings.Contains(msg, "connection refused") || - strings.Contains(msg, "exited before listener became ready") -} - func newTestUOWProvider(t *testing.T) UnitOfWorkProvider { t.Helper() testutil.RequireDoltBinary(t) @@ -65,16 +27,33 @@ func newTestUOWProvider(t *testing.T) UnitOfWorkProvider { t.Setenv("HOME", t.TempDir()) - provider, err := openTestUOWProvider(t, bin) - // This helper starts a private dolt. When that process exits before it - // listens, the open fails with connection refused and the test is the - // only red one (Main, 2026-09-26, - // TestIssueOperationsCreateRoutesInfraTypesToWisps). A second server on - // a new port is the same start the next test would have done anyway. - if err != nil && localDoltNeverListened(err) { - t.Logf("local dolt did not accept connections (%v); starting another", err) - provider, err = openTestUOWProvider(t, bin) - } + port, err := proxy.PickFreePort() + require.NoError(t, err) + storeRootDir := t.TempDir() + shutdownOnInterrupt(t, storeRootDir) + t.Cleanup(func() { + if err := proxy.Shutdown(storeRootDir); err != nil { + t.Logf("proxy.Shutdown(%s): %v", storeRootDir, err) + } + }) + cfgPath := writeServerConfig(t, port) + logPath := filepath.Join(t.TempDir(), "server.log") + + provider, err := NewDoltServerUOWProvider( + context.Background(), + storeRootDir, + "beads", + logPath, + cfgPath, + proxy.BackendLocalServer, + "root", + "", + bin, + 0, + 0, + false, + "", + ) require.NoError(t, err) require.NotNil(t, provider) t.Cleanup(func() { _ = provider.Close(context.Background()) }) diff --git a/internal/storage/uow/versioned_read_contract_test.go b/internal/storage/uow/versioned_read_contract_test.go deleted file mode 100644 index 947f2997f..000000000 --- a/internal/storage/uow/versioned_read_contract_test.go +++ /dev/null @@ -1,404 +0,0 @@ -package uow - -import ( - "context" - "crypto/rand" - "encoding/hex" - "fmt" - "os/exec" - "path/filepath" - "testing" - "time" - - "github.com/stretchr/testify/require" - - "github.com/steveyegge/beads/backend/conformance" - "github.com/steveyegge/beads/internal/storage/dbproxy/proxy" - "github.com/steveyegge/beads/internal/storage/domain" - storageissueops "github.com/steveyegge/beads/internal/storage/issueops" - "github.com/steveyegge/beads/internal/testutil" - "github.com/steveyegge/beads/internal/types" -) - -// asOfReadHarness is this leg's Design D dispatch table (gastownhall/beads#5898 -// revision 9, gastownhall/beads#6136): the conformance suite -// invents storeID values to select among N genuinely isolated stores, and -// isolating them is entirely this harness's job -- AsOfReadInTx itself is -// storeID-oblivious (see the package doc on -// internal/storage/issueops/asof_read.go). Each storeID gets its own fresh -// UnitOfWorkProvider, built lazily on first use (provider) and torn down via -// t.Cleanup on the TestAsOfReadContract-level *testing.T. created tracks -// which (storeID, id) pairs already have a base issues row, since mintAt's -// second and later calls for the same pair must only add a historical -// issue_versions row, never a second base create. -// -// UNLIKE the dolt leg's buildAsOfReadStore (one shared testcontainer, N -// randomly-named databases) or the embeddeddolt leg's buildAsOfReadStore (N -// fully independent on-disk databases, isolation free), this leg spawns one -// real dolt sql-server SUBPROCESS per NewDoltServerUOWProvider call unless -// that call adopts an already-running one. Booting N subprocesses -- one per -// storeID, up to seven across a full TestAsOfReadContract run -- would be -// needlessly heavy, so this harness follows the same "one shared server, many -// databases" shape the dolt leg uses: TestAsOfReadContract does the ONE -// heavyweight, Fatal-permitted setup (dolt/bd binaries, HOME, port, -// storeRootDir, server config, log path) once in its own body, before any -// t.Run, and hands the resulting coordinates to newAsOfReadHarness. provider() -// then calls NewDoltServerUOWProvider -- a pure, Fatal-free function -- against -// those SAME coordinates with a fresh, randomly-named database per storeID. -// proxy.GetCreateDatabaseProxyServerEndpoint's adoption semantics (given the -// same rootDir, a later call dials the already-running server instead of -// spawning a duplicate, regardless of which database name it carries -- see -// TestNewDoltServerUOWProvider_ConcurrentInstantiation) mean only ONE server -// process is ever spawned, no matter how many storeIDs a case invents. -// -// storeID itself is used ONLY as a map key into h.providers here -- never as -// the database name. The conformance suite's storeID strings are built from -// free-form tags (asOfReadStore in backend/conformance/ -// versioned_read_contract.go: fixture.IssuePrefix + "-asof-store-" + tag, -// where tag can itself embed a Restriction's hyphenated String()), so they are -// not assumed to be safe SQL/Dolt identifiers. Generating an independent -// random per-store database name (asOfReadDatabaseName, matching the dolt -// leg's own dbSuffix approach) sidesteps that question entirely rather than -// sanitizing storeID. -// -// provider() cannot use newTestUOWProvider or newUOWRoleFixtureProvider. -// AsOfReadFixture's hook types (Resolve/MintAt/MakeUnanswerable) carry no -// *testing.T of their own, so a provider built lazily inside a hook -- itself -// invoked from inside a t.Run subtest closure, sometimes nested two deep (the -// restriction-loop case) -- has access only to whichever *testing.T this -// harness was constructed with: necessarily the OUTER TestAsOfReadContract's -// t, captured before any t.Run. Both of those helpers call testify's -// require.NoError / t.Fatalf internally, and calling FailNow/SkipNow on a -// *testing.T from a goroutine other than the one running that exact test -- -// which a t.Run subtest's closure always is, relative to the outer test -- -// panics with "subtest may have called FailNow on a parent test" (go's -// testing package requires FailNow/SkipNow to run only on the goroutine -// executing that specific test). provider() is a Fatalf/Skip-free path for -// exactly this reason: its failures come back as a plain error, which flows -// through the calling hook's own (..., error) return to -// conformance.RunAsOfReadXXX, which correctly Fatalfs using the SUBTEST's own -// *testing.T on the SUBTEST's own goroutine. Also unlike -// newUOWRoleFixtureProvider (one provider for a whole role suite, matching -// roleFixtureKit's frozen shape), this harness needs N independently-isolated -// providers -- roleFixtureKit is deliberately not reused here; see this -// file's own note by conformanceToAsOfRestriction on why issueops must stay -// conformance-oblivious, the same boundary that keeps this harness's own -// mapping from living centrally. -// -// See internal/storage/dolt/versioned_read_contract_test.go and -// internal/storage/embeddeddolt/versioned_read_contract_test.go for this -// leg's siblings, which this file mirrors as closely as the three backends' -// construction APIs allow. -type asOfReadHarness struct { - t *testing.T - storeRootDir string - doltBin string - cfgPath string - logPath string - providers map[string]UnitOfWorkProvider - created map[[2]string]bool -} - -func newAsOfReadHarness(t *testing.T, storeRootDir, doltBin, cfgPath, logPath string) *asOfReadHarness { - return &asOfReadHarness{ - t: t, - storeRootDir: storeRootDir, - doltBin: doltBin, - cfgPath: cfgPath, - logPath: logPath, - providers: map[string]UnitOfWorkProvider{}, - created: map[[2]string]bool{}, - } -} - -// provider returns storeID's UnitOfWorkProvider, building it on first use -// against a fresh, randomly-named database on the shared server. See the -// asOfReadHarness doc comment for why this function -- and everything it -// calls -- must never call *testing.T's Fatalf/Skip. -func (h *asOfReadHarness) provider(ctx context.Context, storeID string) (UnitOfWorkProvider, error) { - if p, ok := h.providers[storeID]; ok { - return p, nil - } - database, err := asOfReadDatabaseName() - if err != nil { - return nil, err - } - provider, err := NewDoltServerUOWProvider( - ctx, - h.storeRootDir, - database, - h.logPath, - h.cfgPath, - proxy.BackendLocalServer, - "root", - "", - h.doltBin, - 0, - 0, - false, - "", - ) - if err != nil { - return nil, fmt.Errorf("as-of read fixture: new provider for store %q (database %q): %w", storeID, database, err) - } - // t.Cleanup only appends to a mutex-guarded slice -- unlike Fatalf/Skip it - // never calls runtime.Goexit, so registering it on the outer h.t from - // inside a subtest's goroutine is safe. h.t's own cleanups run only after - // ALL of TestAsOfReadContract's t.Run calls have returned (none of them, - // nor their own nested t.Run calls, ever invoke t.Parallel), so every - // provider this harness builds stays open for the whole test. These - // per-provider Close cleanups are registered AFTER (so, per t.Cleanup's - // LIFO order, run BEFORE) TestAsOfReadContract's own shared - // verifiedShutdownCleanup (proxy.Shutdown) cleanup, so every provider is - // closed while the server they share is still up. - h.t.Cleanup(func() { _ = provider.Close(context.Background()) }) - - if err := RunTx(ctx, provider, func(ctx context.Context, uw UnitOfWork) (string, error) { - return "bd: set issue prefix", uw.ConfigUseCase().SetConfig(ctx, "issue_prefix", "asof") - }); err != nil { - return nil, fmt.Errorf("as-of read fixture: set issue_prefix for store %q: %w", storeID, err) - } - - h.providers[storeID] = provider - return provider, nil -} - -// asOfReadDatabaseName mints a fresh, unique database name, independent of -// any storeID's own content -- see the asOfReadHarness doc comment for why -// storeID itself is never used as one. -func asOfReadDatabaseName() (string, error) { - suffix := make([]byte, 6) - if _, err := rand.Read(suffix); err != nil { - return "", fmt.Errorf("as-of read fixture: generate db name: %w", err) - } - return "asof_" + hex.EncodeToString(suffix), nil -} - -// ensureCreated bare-creates id in storeID's store the first time it is -// asked for, giving RecordVersionAtInTx (called from mintAt) a base issues -// row to snapshot -- recordVersionAtInTx starts with GetIssueInTx, so it has -// nothing to read until this has run once. This goes through the real -// IssueUseCase, the same production entry point batch_creator.go and -// issue_operations.go use, unlike resolve/mintAt/makeUnanswerable below, -// which reach the shared issueops body directly (see the package doc on -// internal/storage/issueops/asof_read.go: "ALL THREE LEGS SHARE THIS BODY"). -func (h *asOfReadHarness) ensureCreated(ctx context.Context, storeID, id string) error { - key := [2]string{storeID, id} - if h.created[key] { - return nil - } - provider, err := h.provider(ctx, storeID) - if err != nil { - return err - } - issue := &types.Issue{ - ID: id, - Title: "as-of read fixture issue", - Status: types.StatusOpen, - Priority: 3, - IssueType: types.TypeTask, - } - err = RunTx(ctx, provider, func(ctx context.Context, uw UnitOfWork) (string, error) { - params := domain.CreateIssueParams{ - Issue: issue, - ExplicitID: issue.ID, - CreateOnly: true, - } - _, err := uw.IssueUseCase().CreateIssue(ctx, params, "asof-read-fixture") - return "as-of read fixture: bare-create " + issue.ID, err - }) - if err != nil { - return fmt.Errorf("as-of read fixture: bare-create %s: %w", id, err) - } - h.created[key] = true - return nil -} - -func (h *asOfReadHarness) resolve(ctx context.Context, storeID, id string, selector conformance.AsOfSelector) (conformance.AsOfReadResult, error) { - provider, err := h.provider(ctx, storeID) - if err != nil { - return conformance.AsOfReadResult{}, err - } - result, err := RunTxRead(ctx, provider, func(ctx context.Context, uw UnitOfWork) (storageissueops.AsOfReadResult, error) { - runner, err := importStatementRunner(uw) - if err != nil { - return storageissueops.AsOfReadResult{}, err - } - return storageissueops.AsOfReadInTx(ctx, runner, storeID, id, toIssueopsSelector(selector)) - }) - if err != nil { - return conformance.AsOfReadResult{}, err - } - return toConformanceResult(result), nil -} - -// mintAt is the two-phase implementation MintAt's contract needs: phase one -// (ensureCreated) runs only once per (storeID, id); phase two -// (RecordVersionAtInTx) runs every call and always supplies the row mintAt -// reports the address of. RecordVersionAtInTx deliberately does not gate on -// versionedHistoryEnabled (this harness never turns that on for its stores), -// so it always mints, and reading MAX(revision) back inside the same -// transaction (read-your-writes) is how we learn which revision it picked -- -// RecordVersionAtInTx itself reports no revision number. -// -// The commit message returned to RunTxResult is deliberately non-empty: an -// empty commitMsg makes RunTxResultWithin skip calling uw.Commit entirely -// (tx.go), so the write would never persist past this attempt's implicit -// rollback -- exactly the RunTxEphemeral hazard its own doc comment warns -// against for versioned tables. -func (h *asOfReadHarness) mintAt(ctx context.Context, storeID, id string, acceptedAt time.Time) (conformance.Address, error) { - if err := h.ensureCreated(ctx, storeID, id); err != nil { - return "", err - } - provider, err := h.provider(ctx, storeID) - if err != nil { - return "", err - } - revision, err := RunTxResult(ctx, provider, func(ctx context.Context, uw UnitOfWork) (int64, string, error) { - runner, err := importStatementRunner(uw) - if err != nil { - return 0, "", err - } - if err := storageissueops.RecordVersionAtInTx(ctx, runner, id, "asof-read-fixture", acceptedAt); err != nil { - return 0, "", err - } - var rev int64 - if err := runner.QueryRowContext(ctx, - `SELECT MAX(revision) FROM issue_versions WHERE issue_id = ?`, id, - ).Scan(&rev); err != nil { - return 0, "", err - } - return rev, "as-of read fixture: mint version for " + id, nil - }) - if err != nil { - return "", err - } - return conformance.Address(storageissueops.AsOfVersionAddress(id, revision)), nil -} - -func (h *asOfReadHarness) makeUnanswerable(ctx context.Context, storeID, id string, restriction conformance.Restriction) error { - asOfRestriction, ok := conformanceToAsOfRestriction[restriction] - if !ok { - return fmt.Errorf("as-of read fixture: unmapped restriction %v", restriction) - } - provider, err := h.provider(ctx, storeID) - if err != nil { - return err - } - reason := fmt.Sprintf("as-of read fixture: made unanswerable as %s", restriction) - return RunTx(ctx, provider, func(ctx context.Context, uw UnitOfWork) (string, error) { - runner, err := importStatementRunner(uw) - if err != nil { - return "", err - } - if err := storageissueops.MarkLatestVersionRemovedInTx(ctx, runner, id, asOfRestriction, reason); err != nil { - return "", err - } - return "as-of read fixture: mark " + id + " removed", nil - }) -} - -// conformanceToAsOfRestriction and its inverse are this leg's translation -// boundary between backend/conformance.Restriction (hyphenated String(), -// int-backed) and issueops.AsOfRestriction (underscored, string-backed -- -// its values ARE the removed_restriction column's contents). issueops must -// stay conformance-oblivious and conformance must stay backend-oblivious -// (internal/storage/issueops/asof_read.go's package doc), so this mapping is -// deliberately duplicated per leg rather than shared centrally. -var conformanceToAsOfRestriction = map[conformance.Restriction]storageissueops.AsOfRestriction{ - conformance.RestrictionLive: storageissueops.AsOfRestrictionLive, - conformance.RestrictionGoneRetention: storageissueops.AsOfRestrictionGoneRetention, - conformance.RestrictionGoneErasure: storageissueops.AsOfRestrictionGoneErasure, - conformance.RestrictionGoneReorganization: storageissueops.AsOfRestrictionGoneReorganization, - conformance.RestrictionUnknown: storageissueops.AsOfRestrictionUnknown, -} - -var asOfRestrictionToConformance = map[storageissueops.AsOfRestriction]conformance.Restriction{ - storageissueops.AsOfRestrictionLive: conformance.RestrictionLive, - storageissueops.AsOfRestrictionGoneRetention: conformance.RestrictionGoneRetention, - storageissueops.AsOfRestrictionGoneErasure: conformance.RestrictionGoneErasure, - storageissueops.AsOfRestrictionGoneReorganization: conformance.RestrictionGoneReorganization, - storageissueops.AsOfRestrictionUnknown: conformance.RestrictionUnknown, -} - -func toIssueopsSelector(selector conformance.AsOfSelector) storageissueops.AsOfSelector { - out := storageissueops.AsOfSelector{At: selector.At} - if selector.Version != nil { - out.Address = string(*selector.Version) - } - return out -} - -func toConformanceResult(result storageissueops.AsOfReadResult) conformance.AsOfReadResult { - if result.Refused { - return conformance.AsOfReadResult{ - Refusal: &conformance.AsOfReadRefusal{ - Restriction: asOfRestrictionToConformance[result.Restriction], - Reason: result.Reason, - }, - } - } - return conformance.AsOfReadResult{ - Address: conformance.Address(result.Address), - State: result.State, - } -} - -// TestAsOfReadContract wires this leg into the R7.1 as-of-read contract -// (gastownhall/beads#5898 revision 9, gastownhall/beads#6136) with a real -// Resolve/MintAt/MakeUnanswerable backed by one isolated UnitOfWorkProvider -// per storeID (asOfReadHarness). -func TestAsOfReadContract(t *testing.T) { - // Everything through logPath below is the same one-time, Fatal-permitted - // setup newTestUOWProvider does, run here directly instead of through - // that helper: newTestUOWProvider both sets up AND builds one provider - // bound to a fixed "beads" database, but this harness needs the setup - // coordinates kept separate so provider() can build N providers, one per - // storeID, lazily from inside subtest closures. See the asOfReadHarness - // doc comment for why provider() must stay Fatalf/Skip-free, which is - // exactly why this block -- the one place in this file where Fatalf/Skip - // is safe -- must run here, before any t.Run. - testutil.RequireDoltBinary(t) - bin, err := exec.LookPath("dolt") - require.NoError(t, err) - - bdBin := buildBDBinary(t) - prev := proxy.ResolveExecutable - proxy.ResolveExecutable = func() (string, error) { return bdBin, nil } - t.Cleanup(func() { proxy.ResolveExecutable = prev }) - - t.Setenv("HOME", t.TempDir()) - - port, err := proxy.PickFreePort() - require.NoError(t, err) - storeRootDir := t.TempDir() - shutdownOnInterrupt(t, storeRootDir) - verifiedShutdownCleanup(t, storeRootDir) - cfgPath := writeServerConfig(t, port) - logPath := filepath.Join(t.TempDir(), "server.log") - - ctx := context.Background() - h := newAsOfReadHarness(t, storeRootDir, bin, cfgPath, logPath) - fixture := conformance.AsOfReadFixture{ - IssuePrefix: "asof", - Resolve: h.resolve, - MintAt: h.mintAt, - MakeUnanswerable: h.makeUnanswerable, - } - - t.Run("SelectorAcceptsEitherAnInstantOrAVersionAddress", func(t *testing.T) { - conformance.RunAsOfReadSelectorAcceptsEitherAnInstantOrAVersionAddress(t, ctx, fixture) - }) - t.Run("ReturnsTheLatestVersionAtOrBeforeTStrictlyExcludingLater", func(t *testing.T) { - conformance.RunAsOfReadReturnsTheLatestVersionAtOrBeforeTStrictlyExcludingLater(t, ctx, fixture) - }) - t.Run("ReportsTheAddressItServed", func(t *testing.T) { - conformance.RunAsOfReadReportsTheAddressItServed(t, ctx, fixture) - }) - t.Run("RefusesAsGoneOrUnknownNeverSubstitutingAnotherVersion", func(t *testing.T) { - conformance.RunAsOfReadRefusesAsGoneOrUnknownNeverSubstitutingAnotherVersion(t, ctx, fixture) - }) - t.Run("IsCompleteOnlyForTheAnsweringStoresHoldings", func(t *testing.T) { - conformance.RunAsOfReadIsCompleteOnlyForTheAnsweringStoresHoldings(t, ctx, fixture) - }) -} diff --git a/internal/storage/version_control.go b/internal/storage/version_control.go index 45830306e..6d974b4f8 100644 --- a/internal/storage/version_control.go +++ b/internal/storage/version_control.go @@ -2,11 +2,7 @@ package storage import ( "context" - "fmt" - "strings" "time" - - "github.com/steveyegge/beads/internal/storage/kvkeys" ) // CommitInfo represents a version control commit. @@ -16,11 +12,6 @@ type CommitInfo struct { Email string Date time.Time Message string - // CommitOrder is Dolt's ancestry-derived position of the commit in the - // history graph (dolt_log.commit_order): a parent always has a lower value - // than its children, regardless of commit dates, which are not required to - // be monotonic along the parent chain. - CommitOrder int64 } // StatusEntry represents a changed table in the working set. @@ -167,9 +158,7 @@ type VersionControl interface { ListBranches(ctx context.Context) ([]string, error) Commit(ctx context.Context, message string) error // CommitWithConfig is like Commit but includes the config table. - // Use after intentional config writes (bd init, bd rename-prefix). Its '-Am' - // stages every dirty table, so a command committing only its own config - // write should use CommitConfigOnly or CommitConfigUserKVOnly instead. + // Use after intentional config writes (bd init, bd config set, bd rename-prefix). // GH#3216: bootstrap paths must use this to commit issue_prefix. CommitWithConfig(ctx context.Context, message string) error // CommitMergeResolution concludes a merge whose conflicts the operator @@ -196,23 +185,6 @@ type VersionControl interface { // racy HEAD-before/HEAD-after comparison (the interface threading tracked // as bd mybd-z9h7j; CommitPending already had the shape). CommitAll(ctx context.Context, message string) (bool, error) - // CommitConfigOnly commits the config table, with the lookup tables - // SetConfig projects from it (custom_statuses, custom_types) — the scoped - // commit for intentional config writes (bd config set) in server mode. - // There, unlike CommitWithConfig ('-Am'), it never stages other tables, so - // a concurrent operation's dirty working set is not swept (GH#2455). - // Embedded mode has no concurrent writer to sweep and commits the whole - // working set, as its CommitWithConfig does. - // GH#4078: without this, server-mode config writes strand the working - // set dirty — generic Commit() excludes config and silently no-ops. - CommitConfigOnly(ctx context.Context, message string) error - // CommitConfigUserKVOnly is the scoped commit for writes of user kv.* data - // (bd remember, bd forget). In server mode it stages the config table alone - // and refuses, committing nothing, when any dirty config row is outside - // kv.*, the screen the pre-pull auto-commit applies. The refusal is a - // *ConfigCommitRefusedError naming those keys. Embedded mode behaves as - // CommitConfigOnly. - CommitConfigUserKVOnly(ctx context.Context, message string) error CommitPending(ctx context.Context, actor string) (bool, error) CommitExists(ctx context.Context, commitHash string) (bool, error) GetCurrentCommit(ctx context.Context) (string, error) @@ -222,21 +194,3 @@ type VersionControl interface { GetConflicts(ctx context.Context) ([]Conflict, error) ResolveConflicts(ctx context.Context, table string, strategy string) error } - -// ConfigCommitRefusedError is CommitConfigUserKVOnly's refusal. Keys are the -// config rows outside the user kv.* namespace that were dirty, or that a -// concurrent writer dirtied while the commit staged: committing the user write -// would have swept them into it (GH#2455). Nothing was committed, and what the -// commit staged was unstaged again unless the error also reports that the -// unstage failed. The user write itself stays in the working set, so a caller -// whose write already landed reports the refusal rather than failing the write. -type ConfigCommitRefusedError struct { - Keys []string -} - -func (e *ConfigCommitRefusedError) Error() string { - return fmt.Sprintf("refusing to commit %d dirty internal config key(s) with a user %s* write: %s; "+ - "only user %s* rows commit with it (GH#2455). The %s* write itself is saved in the "+ - "working set: `bd dolt commit` publishes it together with those keys, or revert the "+ - "keys first", len(e.Keys), kvkeys.Prefix, strings.Join(e.Keys, ", "), kvkeys.Prefix, kvkeys.Prefix) -} diff --git a/internal/storage/version_lister.go b/internal/storage/version_lister.go deleted file mode 100644 index df0946780..000000000 --- a/internal/storage/version_lister.go +++ /dev/null @@ -1,76 +0,0 @@ -package storage - -import ( - "context" - "time" -) - -// IssueVersion is one accepted, immutable durable state of one issue, as -// issue_versions records it. -// -// Revision is a PER-STORE ORDINAL, not a portable address: two disconnected -// clones can both hold revision 8 for the same issue (migration 0067's own -// header says so, and the flag is single-writer-only until the version_id -// primary key lands). Callers must not present it as a citable address. -// -// Its JSON key is "local_revision", NOT "revision", for two reasons that -// compound. types.Issue already ships `json:"revision"` (types.go:1206) and -// that is the row-lock CAS token, an unrelated thing -- and it is a STRING -// there while this is an int64, so a script that learned one shape breaks on -// the other. The name also carries the non-citability with the value, where a -// machine reader sees it, instead of only in a footer no script parses. It -// needs no rename when version_id lands beside it. (bee-ghosttrack, #5898 -// consumer read, 2026-09-20.) -// -// ChangeAgent is read from issue_versions.change_agent (migration 0067) but -// is deliberately NOT part of the JSON shape, which is what `bd versions -// --json` prints. Provenance V2 (gastownhall/beads#7510) replaces the ad hoc -// agent field with an actor chain, so publishing it now would make a public -// contract out of a field that is on its way out. The column and the field -// stay; only the JSON key is withheld. -// -// It lives here rather than in issueops for the same reason HistoryEntry -// does: issueops imports storage, so the shared row type has to sit on this -// side of that edge. -type IssueVersion struct { - IssueID string `json:"issue_id"` - Revision int64 `json:"local_revision"` - Epoch int64 `json:"epoch"` - ChangeActor string `json:"change_actor"` - ChangeAgent string `json:"-"` - ChangeMessage string `json:"change_message"` - ChangeAt time.Time `json:"change_at"` - AttributionStatus string `json:"attribution_status"` - RemovedAt *time.Time `json:"removed_at"` - RemovedReason string `json:"removed_reason"` - RemovedRestriction string `json:"removed_restriction"` - StateBytes int64 `json:"state_bytes"` -} - -// Removed reports whether this version is no longer served -- retention, -// erasure or an epoch reorganization removed it. A removed version keeps its -// row (so the gap is legible) but its durable state is gone, and callers must -// render it as a restriction rather than as an empty version. -func (v IssueVersion) Removed() bool { return v.RemovedAt != nil } - -// VersionLister is implemented by backends that can serve an issue's -// versioned history from the issue_versions table (migration 0067+). -// -// This is a narrow capability rather than a method on Storage on purpose: a -// backend that has no issue_versions table -- a proxied client, a no-db -// workspace, a non-Dolt backend -- should not be forced to implement it, and -// a caller that asserts for it learns the truth instead of receiving a stub -// that answers "no versions" for a store that simply cannot know. Same -// reasoning as ExternalRefHistoryQuerier in history_viewer.go. -// -// Holding this capability says the backend can READ versions. It says nothing -// about whether version recording is switched on -- that is -// VersionedHistoryConfigurer, and a store can legitimately implement both -// while the feature is off, in which case ListVersions truthfully returns -// nothing. -type VersionLister interface { - // ListVersions returns every retained version of issueID, newest first. - // An empty result means no retained versions, never "unsupported" -- - // unsupported is the failed type assertion. - ListVersions(ctx context.Context, issueID string) ([]IssueVersion, error) -} diff --git a/internal/storage/versioncontrolops/BUILD.bazel b/internal/storage/versioncontrolops/BUILD.bazel deleted file mode 100644 index 84fcf6342..000000000 --- a/internal/storage/versioncontrolops/BUILD.bazel +++ /dev/null @@ -1,128 +0,0 @@ -load("@rules_go//go:def.bzl", "go_library", "go_test") - -go_library( - name = "versioncontrolops", - srcs = [ - "automerge.go", - "backup.go", - "blocked_recompute.go", - "branches.go", - "clone.go", - "commit.go", - "compact.go", - "conflicts.go", - "db.go", - "envguards.go", - "fastforward.go", - "flatten.go", - "gc.go", - "mergesettle.go", - "remoterefs.go", - "remotes.go", - "version_control.go", - ], - importpath = "github.com/steveyegge/beads/internal/storage/versioncontrolops", - visibility = ["//:__subpackages__"], - deps = [ - "//internal/githooksenv", - "//internal/gittraceenv", - "//internal/storage", - "//internal/storage/issueops", - "//internal/storage/kvkeys", - ], -) - -go_test( - name = "versioncontrolops_test", - srcs = [ - "automerge_test.go", - "backup_test.go", - "clone_test.go", - "commit_empty_test.go", - "conflicts_test.go", - "fastforward_test.go", - "gc_test.go", - "remotes_githooks_test.go", - "remotes_gittrace_test.go", - "remotes_test.go", - ], - embed = [":versioncontrolops"], - deps = [ - "//internal/githooksenv", - "@com_github_data_dog_go_sqlmock//:go-sqlmock", - ], -) - -# --- begin go_srcs (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) --- - -filegroup( - name = "go_srcs", - srcs = glob( - ["**/*.go"], - allow_empty = True, - ), - visibility = ["//:__subpackages__"], -) - -# --- end go_srcs --- - -# --- begin repo_files (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) --- - -filegroup( - name = "repo_doc_files", - srcs = glob( - [ - "**/*.md", - "**/*.mdx", - ], - allow_empty = True, - exclude = ["**/node_modules/**"], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_go_srcs", - srcs = glob( - ["**/*.go"], - allow_empty = True, - exclude = [ - "**/*_test.go", - "**/node_modules/**", - ], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_go_test_srcs", - srcs = glob( - ["**/*_test.go"], - allow_empty = True, - exclude = ["**/node_modules/**"], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_other_files", - srcs = glob( - ["**"], - exclude = [ - "**/*.db", - "**/*.exe", - "**/*.go", - "**/*.md", - "**/*.mdx", - "**/*.out", - "**/*.prof", - "**/*.pyc", - "**/*.test", - "**/__pycache__/**", - "**/node_modules/**", - ], - ), - visibility = ["//:__subpackages__"], -) - -# --- end repo_files --- diff --git a/internal/storage/versioncontrolops/clone.go b/internal/storage/versioncontrolops/clone.go index f3477081b..3f17f7dbc 100644 --- a/internal/storage/versioncontrolops/clone.go +++ b/internal/storage/versioncontrolops/clone.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "net/url" - "strings" ) // DoltClone clones a Dolt database from a remote URL. @@ -13,27 +12,12 @@ import ( // If user is non-empty, authenticates with that user. Dolt reads the remote // password from DOLT_REMOTE_PASSWORD. func DoltClone(ctx context.Context, conn DBConn, remoteURL, database, user string) error { - return DoltCloneWithRef(ctx, conn, remoteURL, database, user, "") -} - -// DoltCloneWithRef is DoltClone for a git-backed remote whose Dolt data lives -// on the git ref ref. A non-empty ref is passed as DOLT_CLONE's --ref option; -// Dolt reads the data from that ref and records it on the clone's origin -// remote, so later push and pull use it. An empty ref is DoltClone. -func DoltCloneWithRef(ctx context.Context, conn DBConn, remoteURL, database, user, ref string) error { - var parts []string - var args []any + query := "CALL DOLT_CLONE(?, ?)" + args := []any{remoteURL, database} if user != "" { - parts = append(parts, "'--user', ?") - args = append(args, user) + query = "CALL DOLT_CLONE('--user', ?, ?, ?)" + args = []any{user, remoteURL, database} } - if ref = strings.TrimSpace(ref); ref != "" { - parts = append(parts, "'--ref', ?") - args = append(args, ref) - } - parts = append(parts, "?, ?") - args = append(args, remoteURL, database) - query := "CALL DOLT_CLONE(" + strings.Join(parts, ", ") + ")" // GH#4272: the initial fetch runs git hooks just like push/pull; disable // them for the clone window too (see remotes.go for the full rationale). diff --git a/internal/storage/versioncontrolops/clone_test.go b/internal/storage/versioncontrolops/clone_test.go index 342fb3b33..830663ee4 100644 --- a/internal/storage/versioncontrolops/clone_test.go +++ b/internal/storage/versioncontrolops/clone_test.go @@ -27,49 +27,6 @@ func TestDoltCloneWithoutUser(t *testing.T) { } } -// A ref reaches DOLT_CLONE as --ref, verbatim, for both ref shapes. -func TestDoltCloneWithRef(t *testing.T) { - for _, ref := range gitDataRefShapes { - t.Run(ref, func(t *testing.T) { - db, mock, err := sqlmock.New() - if err != nil { - t.Fatalf("sqlmock.New: %v", err) - } - defer db.Close() - - mock.ExpectExec(regexp.QuoteMeta("CALL DOLT_CLONE('--ref', ?, ?, ?)")). - WithArgs(ref, "git+https://example.com/repo.git", "beads"). - WillReturnResult(sqlmock.NewResult(0, 1)) - - if err := DoltCloneWithRef(context.Background(), db, "git+https://example.com/repo.git", "beads", "", ref); err != nil { - t.Fatalf("DoltCloneWithRef: %v", err) - } - if err := mock.ExpectationsWereMet(); err != nil { - t.Fatal(err) - } - }) - } -} - -func TestDoltCloneWithUserAndRef(t *testing.T) { - db, mock, err := sqlmock.New() - if err != nil { - t.Fatalf("sqlmock.New: %v", err) - } - defer db.Close() - - mock.ExpectExec(regexp.QuoteMeta("CALL DOLT_CLONE('--user', ?, '--ref', ?, ?, ?)")). - WithArgs("alice", "refs/dolt/units/team-12542", "git+https://example.com/repo.git", "beads"). - WillReturnResult(sqlmock.NewResult(0, 1)) - - if err := DoltCloneWithRef(context.Background(), db, "git+https://example.com/repo.git", "beads", "alice", "refs/dolt/units/team-12542"); err != nil { - t.Fatalf("DoltCloneWithRef: %v", err) - } - if err := mock.ExpectationsWereMet(); err != nil { - t.Fatal(err) - } -} - func TestDoltCloneWithUser(t *testing.T) { db, mock, err := sqlmock.New() if err != nil { diff --git a/internal/storage/versioncontrolops/commit.go b/internal/storage/versioncontrolops/commit.go index 5b93452b3..48ec20513 100644 --- a/internal/storage/versioncontrolops/commit.go +++ b/internal/storage/versioncontrolops/commit.go @@ -32,46 +32,6 @@ func (t *DirtyTableTracker) DirtyTables() map[string]bool { return t.tables } -// MarkVersionedHistoryDirty adds the tables the versioned-history seam writes -// (issueops.VersionedHistoryStagedTables) when history is enabled on the store -// that owns this transaction. Stores call it once, after the body has run and -// before handing DirtyTables to StageAndCommit, so no individual mutator has -// to remember that minting a version dirties three more tables. -// -// enabled is the store's own activation flag — the same input its -// ScopeVersionedHistoryTransaction call takes. That is the same atomic read -// twice, not one value threaded through: the scope call reads it when the -// transaction opens, this one reads it again after the body has run. So the -// staging set and the minting behavior cannot disagree only as long as -// activation does not change mid-operation — a SetVersionedHistoryEnabled -// flip landing in that window mints rows under the scope and then declines to -// stage them. Activation is wiring-time configuration with no non-test caller -// today, so this is an invariant to preserve rather than a live defect; latch -// the flag once per operation and pass the bool if that ever stops holding. -// -// A tracker with nothing dirty is left alone. Minting only happens alongside a -// mutation, so an empty set means no mutation ran; adding tables there would -// turn an operation that deliberately stages nothing into one that stages, and -// StageAndCommit's own len(dirtyTables)==0 early return is load-bearing for -// those callers. -// -// Keyed on SCOPE, not on whether a row was actually minted -- deliberately. -// Staging a clean table costs one DOLT_ADD that stages nothing, and both -// commit helpers already skip an empty staged set, so the false-positive side -// is free; tracking real mints would mean threading state back out of the seam -// for no behavioral gain. The one thing it does widen is `issues` on the -// dependency paths, which stage only {dependencies, events} today -- and that -// widening is exactly what the current_revision advance requires, gated on -// history being on, so no flag-off commit changes shape. -func (t *DirtyTableTracker) MarkVersionedHistoryDirty(enabled bool) { - if !enabled || len(t.tables) == 0 { - return - } - for _, table := range issueops.VersionedHistoryStagedTables() { - t.MarkDirty(table) - } -} - // StageAndCommit stages only the specified dirty tables and creates a Dolt // version commit. conn must be a non-transactional database connection (the // SQL transaction should already be committed before calling this). diff --git a/internal/storage/versioncontrolops/remotes.go b/internal/storage/versioncontrolops/remotes.go index 9ef349a1e..fdf373004 100644 --- a/internal/storage/versioncontrolops/remotes.go +++ b/internal/storage/versioncontrolops/remotes.go @@ -2,19 +2,14 @@ package versioncontrolops import ( "context" - "database/sql" "fmt" - "strings" "github.com/steveyegge/beads/internal/storage" ) -// ListRemotes returns all configured Dolt remotes: name, URL, and the git ref -// a git-backed remote keeps its data on when one was set. The ref is the -// git_ref key of the dolt_remotes params column, a JSON object of the -// remote's parameters; a remote without the key lists an empty Ref. +// ListRemotes returns all configured Dolt remotes (name and URL). func ListRemotes(ctx context.Context, db DBConn) ([]storage.RemoteInfo, error) { - rows, err := db.QueryContext(ctx, "SELECT name, url, params FROM dolt_remotes") + rows, err := db.QueryContext(ctx, "SELECT name, url FROM dolt_remotes") if err != nil { return nil, fmt.Errorf("list remotes: %w", err) } @@ -23,35 +18,14 @@ func ListRemotes(ctx context.Context, db DBConn) ([]storage.RemoteInfo, error) { var remotes []storage.RemoteInfo for rows.Next() { var r storage.RemoteInfo - var params sql.NullString - if err := rows.Scan(&r.Name, &r.URL, ¶ms); err != nil { + if err := rows.Scan(&r.Name, &r.URL); err != nil { return nil, fmt.Errorf("scan remote: %w", err) } - if params.Valid { - ref, err := storage.GitRefFromParamsJSON(params.String) - if err != nil { - return nil, fmt.Errorf("remote %s: %w", r.Name, err) - } - r.Ref = ref - } remotes = append(remotes, r) } return remotes, rows.Err() } -// AddRemote adds a Dolt remote. A non-empty ref is passed as DOLT_REMOTE's -// --ref option, which Dolt records as the remote's git_ref parameter and -// accepts for git-backed remotes only; for any other scheme Dolt's own -// refusal is returned unchanged. -func AddRemote(ctx context.Context, db DBConn, name, url, ref string) error { - if ref = strings.TrimSpace(ref); ref == "" { - _, err := db.ExecContext(ctx, "CALL DOLT_REMOTE('add', ?, ?)", name, url) - return err - } - _, err := db.ExecContext(ctx, "CALL DOLT_REMOTE('add', '--ref', ?, ?, ?)", ref, name, url) - return err -} - // RemoveRemote removes a configured Dolt remote. func RemoveRemote(ctx context.Context, db DBConn, name string) error { if _, err := db.ExecContext(ctx, "CALL DOLT_REMOTE('remove', ?)", name); err != nil { diff --git a/internal/storage/versioncontrolops/remotes_test.go b/internal/storage/versioncontrolops/remotes_test.go deleted file mode 100644 index 15aaf81de..000000000 --- a/internal/storage/versioncontrolops/remotes_test.go +++ /dev/null @@ -1,132 +0,0 @@ -package versioncontrolops - -import ( - "context" - "regexp" - "strings" - "testing" - - "github.com/DATA-DOG/go-sqlmock" -) - -// The two ref shapes every ref test in this change covers: a branch, for a -// git host that only accepts pushes under refs/heads/, and a ref elsewhere -// in the namespace, for several databases kept in one repository. -var gitDataRefShapes = []string{"refs/heads/issue-data", "refs/dolt/units/team-12542"} - -// Without a ref the add is the same two-argument DOLT_REMOTE call every -// existing remote was created with. -func TestAddRemoteWithoutRef(t *testing.T) { - db, mock, err := sqlmock.New() - if err != nil { - t.Fatalf("sqlmock.New: %v", err) - } - defer db.Close() - - mock.ExpectExec(regexp.QuoteMeta("CALL DOLT_REMOTE('add', ?, ?)")). - WithArgs("origin", "git+https://example.com/repo.git"). - WillReturnResult(sqlmock.NewResult(0, 1)) - - if err := AddRemote(context.Background(), db, "origin", "git+https://example.com/repo.git", " "); err != nil { - t.Fatalf("AddRemote: %v", err) - } - if err := mock.ExpectationsWereMet(); err != nil { - t.Fatal(err) - } -} - -// With a ref the add carries --ref, and the value reaches Dolt verbatim for -// both shapes. -func TestAddRemoteWithRef(t *testing.T) { - for _, ref := range gitDataRefShapes { - t.Run(ref, func(t *testing.T) { - db, mock, err := sqlmock.New() - if err != nil { - t.Fatalf("sqlmock.New: %v", err) - } - defer db.Close() - - mock.ExpectExec(regexp.QuoteMeta("CALL DOLT_REMOTE('add', '--ref', ?, ?, ?)")). - WithArgs(ref, "origin", "git+https://example.com/repo.git"). - WillReturnResult(sqlmock.NewResult(0, 1)) - - if err := AddRemote(context.Background(), db, "origin", "git+https://example.com/repo.git", ref); err != nil { - t.Fatalf("AddRemote: %v", err) - } - if err := mock.ExpectationsWereMet(); err != nil { - t.Fatal(err) - } - }) - } -} - -// ListRemotes reads the ref back out of the params column. A NULL, the text -// "null" a sql-server sends for a remote without parameters, an empty -// object, and an object without git_ref all list as the default (empty Ref); -// a recorded git_ref comes back verbatim. -func TestListRemotesReadsGitRefParam(t *testing.T) { - db, mock, err := sqlmock.New() - if err != nil { - t.Fatalf("sqlmock.New: %v", err) - } - defer db.Close() - - rows := sqlmock.NewRows([]string{"name", "url", "params"}). - AddRow("null-params", "file:///tmp/a", nil). - AddRow("null-text", "file:///tmp/b", "null"). - AddRow("empty-params", "dolthub://org/repo", "{}"). - AddRow("other-params", "aws://bucket/repo", `{"aws-region":"us-east-1"}`). - AddRow("branch", "git+https://example.com/repo.git", `{"git_ref":"refs/heads/issue-data"}`). - AddRow("unit", "git+file:///srv/ledgers", `{"git_ref":"refs/dolt/units/team-12542"}`) - mock.ExpectQuery(regexp.QuoteMeta("SELECT name, url, params FROM dolt_remotes")).WillReturnRows(rows) - - remotes, err := ListRemotes(context.Background(), db) - if err != nil { - t.Fatalf("ListRemotes: %v", err) - } - want := map[string]string{ - "null-params": "", - "null-text": "", - "empty-params": "", - "other-params": "", - "branch": "refs/heads/issue-data", - "unit": "refs/dolt/units/team-12542", - } - if len(remotes) != len(want) { - t.Fatalf("ListRemotes returned %d remotes, want %d: %+v", len(remotes), len(want), remotes) - } - for _, r := range remotes { - if r.Ref != want[r.Name] { - t.Errorf("remote %s: Ref = %q, want %q", r.Name, r.Ref, want[r.Name]) - } - } - if remotes[0].URL != "file:///tmp/a" { - t.Errorf("URL still scanned: got %q", remotes[0].URL) - } - if err := mock.ExpectationsWereMet(); err != nil { - t.Fatal(err) - } -} - -// A params value that is not a JSON object is an error naming the remote, -// never a silent fall-back to the default ref: routing a custom-ref remote -// through the default would push its data to the wrong ref. -func TestListRemotesRejectsMalformedParams(t *testing.T) { - db, mock, err := sqlmock.New() - if err != nil { - t.Fatalf("sqlmock.New: %v", err) - } - defer db.Close() - - rows := sqlmock.NewRows([]string{"name", "url", "params"}). - AddRow("origin", "git+https://example.com/repo.git", "not-json") - mock.ExpectQuery(regexp.QuoteMeta("SELECT name, url, params FROM dolt_remotes")).WillReturnRows(rows) - - _, err = ListRemotes(context.Background(), db) - if err == nil { - t.Fatal("ListRemotes with malformed params = nil error, want error") - } - if !strings.Contains(err.Error(), "origin") || !strings.Contains(err.Error(), "params") { - t.Errorf("error should name the remote and the params column: %v", err) - } -} diff --git a/internal/storage/versioncontrolops/version_control.go b/internal/storage/versioncontrolops/version_control.go index 9ad8b0ff0..f657fe401 100644 --- a/internal/storage/versioncontrolops/version_control.go +++ b/internal/storage/versioncontrolops/version_control.go @@ -43,18 +43,16 @@ func Status(ctx context.Context, db DBConn) (*storage.Status, error) { return status, rows.Err() } -// Log returns recent commit history up to limit entries, newest commit date -// first. Each entry also carries Dolt's commit_order so callers that need graph -// (ancestry) order can sort by CommitOrder instead of trusting commit dates. +// Log returns recent commit history up to limit entries. // If limit is 0 or negative, all entries are returned. func Log(ctx context.Context, db DBConn, limit int) ([]storage.CommitInfo, error) { var query string var args []interface{} if limit > 0 { - query = "SELECT commit_hash, committer, email, date, message, commit_order FROM dolt_log ORDER BY date DESC LIMIT ?" + query = "SELECT commit_hash, committer, email, date, message FROM dolt_log ORDER BY date DESC LIMIT ?" args = []interface{}{limit} } else { - query = "SELECT commit_hash, committer, email, date, message, commit_order FROM dolt_log ORDER BY date DESC" + query = "SELECT commit_hash, committer, email, date, message FROM dolt_log ORDER BY date DESC" } rows, err := db.QueryContext(ctx, query, args...) if err != nil { @@ -65,7 +63,7 @@ func Log(ctx context.Context, db DBConn, limit int) ([]storage.CommitInfo, error var commits []storage.CommitInfo for rows.Next() { var c storage.CommitInfo - if err := rows.Scan(&c.Hash, &c.Author, &c.Email, &c.Date, &c.Message, &c.CommitOrder); err != nil { + if err := rows.Scan(&c.Hash, &c.Author, &c.Email, &c.Date, &c.Message); err != nil { return nil, fmt.Errorf("scan commit: %w", err) } commits = append(commits, c) diff --git a/internal/storage/versioned.go b/internal/storage/versioned.go index 46a745cf5..5722df688 100644 --- a/internal/storage/versioned.go +++ b/internal/storage/versioned.go @@ -39,10 +39,6 @@ type Conflict struct { type RemoteInfo struct { Name string `json:"name"` // Remote name (e.g., "town-beta") URL string `json:"url"` // Remote URL (e.g., "dolthub://org/repo") - // Ref is the git ref a git-backed remote keeps its Dolt data on, when the - // remote was added with one. Empty means DefaultGitDataRef; always empty - // for remotes that are not git-backed. - Ref string `json:"ref,omitempty"` } // SyncStatus describes the synchronization state with a peer. diff --git a/internal/storage/wake_defers.go b/internal/storage/wake_defers.go deleted file mode 100644 index ea2433ed4..000000000 --- a/internal/storage/wake_defers.go +++ /dev/null @@ -1,10 +0,0 @@ -package storage - -import "context" - -// ExpiredDeferWaker lets ready-role decorators preserve the backend's lazy -// wake before selecting work. The sweep uses its own transaction and is -// advisory: implementations warn on failure rather than failing the claim. -type ExpiredDeferWaker interface { - WakeExpiredDefersAdvisory(context.Context) -} diff --git a/internal/telemetry/BUILD.bazel b/internal/telemetry/BUILD.bazel deleted file mode 100644 index 9ad893f45..000000000 --- a/internal/telemetry/BUILD.bazel +++ /dev/null @@ -1,150 +0,0 @@ -load("@rules_go//go:def.bzl", "go_library", "go_test") - -go_library( - name = "telemetry", - srcs = [ - "batch_applier.go", - "batch_closer.go", - "batch_creator.go", - "batch_getter.go", - "blocked_lister.go", - "blockingannotator.go", - "bootstrapper.go", - "commenter.go", - "counter.go", - "cycle_detector.go", - "deleter.go", - "dependency_editor.go", - "edgereader.go", - "graph_counter.go", - "heartbeater.go", - "init_verifier.go", - "issue_claimer.go", - "issue_operations.go", - "issue_reader.go", - "lease_reclaimer.go", - "memories.go", - "metadata_cas.go", - "molecule_stepper.go", - "querier.go", - "ready_claimer.go", - "ready_counter.go", - "relations.go", - "releaser.go", - "stats_reporter.go", - "storage.go", - "sweeper.go", - "telemetry.go", - "tree_walker.go", - "version_reconciler.go", - "workspace_config.go", - ], - importpath = "github.com/steveyegge/beads/internal/telemetry", - visibility = ["//:__subpackages__"], - deps = [ - "//internal/storage", - "//internal/types", - "//issueops", - "//memoryops", - "@io_opentelemetry_go_otel//:otel", - "@io_opentelemetry_go_otel//attribute", - "@io_opentelemetry_go_otel//codes", - "@io_opentelemetry_go_otel//semconv/v1.26.0:v1_26_0", - "@io_opentelemetry_go_otel_exporters_otlp_otlpmetric_otlpmetrichttp//:otlpmetrichttp", - "@io_opentelemetry_go_otel_exporters_stdout_stdoutmetric//:stdoutmetric", - "@io_opentelemetry_go_otel_exporters_stdout_stdouttrace//:stdouttrace", - "@io_opentelemetry_go_otel_metric//:metric", - "@io_opentelemetry_go_otel_metric//noop", - "@io_opentelemetry_go_otel_sdk//resource", - "@io_opentelemetry_go_otel_sdk//trace", - "@io_opentelemetry_go_otel_sdk_metric//:metric", - "@io_opentelemetry_go_otel_trace//:trace", - "@io_opentelemetry_go_otel_trace//noop", - ], -) - -go_test( - name = "telemetry_test", - srcs = [ - "batch_close_policy_test.go", - "issue_claimer_test.go", - "issue_operations_test.go", - "issue_reader_test.go", - "role_accessor_decorator_test.go", - "storage_test.go", - "telemetry_test.go", - ], - embed = [":telemetry"], - deps = [ - "//internal/storage", - "//internal/storage/domain", - "//internal/storage/externaldeps", - "//issueops", - "//memoryops", - "@io_opentelemetry_go_otel//:otel", - "@io_opentelemetry_go_otel//attribute", - "@io_opentelemetry_go_otel_metric//noop", - "@io_opentelemetry_go_otel_trace//noop", - ], -) - -# --- begin repo_files (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) --- - -filegroup( - name = "repo_doc_files", - srcs = glob( - [ - "**/*.md", - "**/*.mdx", - ], - allow_empty = True, - exclude = ["**/node_modules/**"], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_go_srcs", - srcs = glob( - ["**/*.go"], - allow_empty = True, - exclude = [ - "**/*_test.go", - "**/node_modules/**", - ], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_go_test_srcs", - srcs = glob( - ["**/*_test.go"], - allow_empty = True, - exclude = ["**/node_modules/**"], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_other_files", - srcs = glob( - ["**"], - exclude = [ - "**/*.db", - "**/*.exe", - "**/*.go", - "**/*.md", - "**/*.mdx", - "**/*.out", - "**/*.prof", - "**/*.pyc", - "**/*.test", - "**/__pycache__/**", - "**/node_modules/**", - ], - ), - visibility = ["//:__subpackages__"], -) - -# --- end repo_files --- diff --git a/internal/telemetry/batch_close_policy_test.go b/internal/telemetry/batch_close_policy_test.go deleted file mode 100644 index 1bee9370d..000000000 --- a/internal/telemetry/batch_close_policy_test.go +++ /dev/null @@ -1,37 +0,0 @@ -package telemetry - -import ( - "errors" - "testing" - - "github.com/steveyegge/beads/internal/storage" - "github.com/steveyegge/beads/issueops" -) - -type policyBatchCloserStore struct { - storage.DoltStorage - closer issueops.BatchCloser - policy storage.BatchClosePolicy -} - -func (s *policyBatchCloserStore) BatchCloserWithPolicy(policy storage.BatchClosePolicy) (issueops.BatchCloser, error) { - s.policy = policy - return s.closer, nil -} - -func TestInstrumentedStorageForwardsBatchClosePolicy(t *testing.T) { - sentinel := &roleAccessorSentinel{} - raw := &policyBatchCloserStore{closer: sentinel} - store := &InstrumentedStorage{inner: raw} - closer, err := storage.BatchCloserWithPolicy(store, storage.NewBatchClosePolicy(map[string][]string{"blocked": {"external:p:c"}})) - if err != nil { - t.Fatal(err) - } - if !errors.Is(raw.policy.CheckClose("blocked", false), storage.ErrCloseBlocked) { - t.Fatal("telemetry wrapper dropped external policy") - } - instrumented, ok := closer.(*instrumentedBatchCloser) - if !ok || instrumented.inner != sentinel || instrumented.storage != store { - t.Fatalf("policy accessor lost telemetry layer: %T", closer) - } -} diff --git a/internal/telemetry/batch_closer.go b/internal/telemetry/batch_closer.go index 15d2d1498..30901dcb4 100644 --- a/internal/telemetry/batch_closer.go +++ b/internal/telemetry/batch_closer.go @@ -3,7 +3,6 @@ package telemetry import ( "context" - "github.com/steveyegge/beads/internal/storage" "github.com/steveyegge/beads/issueops" ) @@ -18,14 +17,6 @@ func (s *InstrumentedStorage) BatchCloser() (issueops.BatchCloser, error) { return s.WrapBatchCloser(inner), nil } -func (s *InstrumentedStorage) BatchCloserWithPolicy(policy storage.BatchClosePolicy) (issueops.BatchCloser, error) { - inner, err := storage.BatchCloserWithPolicy(s.Unwrap(), policy) - if err != nil { - return nil, err - } - return s.WrapBatchCloser(inner), nil -} - // WrapBatchCloser instruments guarded batch closes with this storage layer's // existing telemetry meter and tracer. func (s *InstrumentedStorage) WrapBatchCloser(inner issueops.BatchCloser) issueops.BatchCloser { diff --git a/internal/telemetry/batch_getter.go b/internal/telemetry/batch_getter.go deleted file mode 100644 index 1b4f5cddb..000000000 --- a/internal/telemetry/batch_getter.go +++ /dev/null @@ -1,36 +0,0 @@ -package telemetry - -import ( - "context" - - "github.com/steveyegge/beads/issueops" -) - -// BatchGetter returns the inner store's batch-read surface wrapped in this -// layer's instrumentation. It recurses instead of delegating: a blind -// delegation would return the inner getter unspanned and untimed. -func (s *InstrumentedStorage) BatchGetter() (issueops.BatchGetter, error) { - inner, err := s.Unwrap().BatchGetter() - if err != nil { - return nil, err - } - return s.WrapBatchGetter(inner), nil -} - -// WrapBatchGetter instruments guarded batch reads with this storage layer's -// existing telemetry meter and tracer. -func (s *InstrumentedStorage) WrapBatchGetter(inner issueops.BatchGetter) issueops.BatchGetter { - return &instrumentedBatchGetter{storage: s, inner: inner} -} - -type instrumentedBatchGetter struct { - storage *InstrumentedStorage - inner issueops.BatchGetter -} - -func (c *instrumentedBatchGetter) GetMany(ctx context.Context, request issueops.GetManyRequest) (result issueops.GetManyResult, err error) { - ctx, span, started := c.storage.op(ctx, "BatchGetter.GetMany") - result, err = c.inner.GetMany(ctx, request) - c.storage.done(ctx, span, started, err) - return result, err -} diff --git a/internal/telemetry/blocked_lister.go b/internal/telemetry/blocked_lister.go deleted file mode 100644 index c050e0d6b..000000000 --- a/internal/telemetry/blocked_lister.go +++ /dev/null @@ -1,38 +0,0 @@ -package telemetry - -import ( - "context" - - "github.com/steveyegge/beads/issueops" -) - -// BlockedLister returns the inner store's blocked-listing surface wrapped in -// this layer's instrumentation. It recurses instead of delegating: a blind -// delegation would return the inner surface unspanned and untimed. -func (s *InstrumentedStorage) BlockedLister() (issueops.BlockedLister, error) { - inner, err := s.Unwrap().BlockedLister() - if err != nil { - return nil, err - } - return s.WrapBlockedLister(inner), nil -} - -// WrapBlockedLister instruments the blocked listing with this storage layer's -// existing telemetry meter and tracer. The external-dependency decorator -// calls it on the lister it builds over itself, so the span survives that -// override. -func (s *InstrumentedStorage) WrapBlockedLister(inner issueops.BlockedLister) issueops.BlockedLister { - return &instrumentedBlockedLister{storage: s, inner: inner} -} - -type instrumentedBlockedLister struct { - storage *InstrumentedStorage - inner issueops.BlockedLister -} - -func (c *instrumentedBlockedLister) ListBlocked(ctx context.Context, request issueops.BlockedRequest) (result issueops.BlockedResult, err error) { - ctx, span, started := c.storage.op(ctx, "BlockedLister.ListBlocked") - result, err = c.inner.ListBlocked(ctx, request) - c.storage.done(ctx, span, started, err) - return result, err -} diff --git a/internal/telemetry/heartbeater.go b/internal/telemetry/heartbeater.go deleted file mode 100644 index e0be3a106..000000000 --- a/internal/telemetry/heartbeater.go +++ /dev/null @@ -1,36 +0,0 @@ -package telemetry - -import ( - "context" - - "github.com/steveyegge/beads/issueops" -) - -// Heartbeater returns the inner store's lease-refresh surface wrapped in this -// layer's instrumentation. It recurses instead of delegating: a blind -// delegation would return the inner surface unspanned and untimed. -func (s *InstrumentedStorage) Heartbeater() (issueops.Heartbeater, error) { - inner, err := s.Unwrap().Heartbeater() - if err != nil { - return nil, err - } - return s.WrapHeartbeater(inner), nil -} - -// WrapHeartbeater instruments the lease refresh with this storage layer's -// existing telemetry meter and tracer. -func (s *InstrumentedStorage) WrapHeartbeater(inner issueops.Heartbeater) issueops.Heartbeater { - return &instrumentedHeartbeater{storage: s, inner: inner} -} - -type instrumentedHeartbeater struct { - storage *InstrumentedStorage - inner issueops.Heartbeater -} - -func (c *instrumentedHeartbeater) Heartbeat(ctx context.Context, request issueops.HeartbeatRequest) (result issueops.HeartbeatResult, err error) { - ctx, span, started := c.storage.op(ctx, "Heartbeater.Heartbeat") - result, err = c.inner.Heartbeat(ctx, request) - c.storage.done(ctx, span, started, err) - return result, err -} diff --git a/internal/telemetry/lease_reclaimer.go b/internal/telemetry/lease_reclaimer.go deleted file mode 100644 index cf61d8356..000000000 --- a/internal/telemetry/lease_reclaimer.go +++ /dev/null @@ -1,36 +0,0 @@ -package telemetry - -import ( - "context" - - "github.com/steveyegge/beads/issueops" -) - -// LeaseReclaimer returns the inner store's lease-sweep surface wrapped in -// this layer's instrumentation. It recurses instead of delegating: a blind -// delegation would return the inner surface unspanned and untimed. -func (s *InstrumentedStorage) LeaseReclaimer() (issueops.LeaseReclaimer, error) { - inner, err := s.Unwrap().LeaseReclaimer() - if err != nil { - return nil, err - } - return s.WrapLeaseReclaimer(inner), nil -} - -// WrapLeaseReclaimer instruments the lease sweep with this storage layer's -// existing telemetry meter and tracer. -func (s *InstrumentedStorage) WrapLeaseReclaimer(inner issueops.LeaseReclaimer) issueops.LeaseReclaimer { - return &instrumentedLeaseReclaimer{storage: s, inner: inner} -} - -type instrumentedLeaseReclaimer struct { - storage *InstrumentedStorage - inner issueops.LeaseReclaimer -} - -func (c *instrumentedLeaseReclaimer) Reclaim(ctx context.Context, request issueops.ReclaimRequest) (result issueops.ReclaimResult, err error) { - ctx, span, started := c.storage.op(ctx, "LeaseReclaimer.Reclaim") - result, err = c.inner.Reclaim(ctx, request) - c.storage.done(ctx, span, started, err) - return result, err -} diff --git a/internal/telemetry/molecule_stepper.go b/internal/telemetry/molecule_stepper.go deleted file mode 100644 index 50ec721ac..000000000 --- a/internal/telemetry/molecule_stepper.go +++ /dev/null @@ -1,35 +0,0 @@ -package telemetry - -import ( - "context" - - "github.com/steveyegge/beads/issueops" -) - -// MoleculeStepper returns the inner store's advance surface wrapped in this -// layer's instrumentation. -func (s *InstrumentedStorage) MoleculeStepper() (issueops.MoleculeStepper, error) { - inner, err := s.Unwrap().MoleculeStepper() - if err != nil { - return nil, err - } - return s.WrapMoleculeStepper(inner), nil -} - -// WrapMoleculeStepper instruments molecule advances with this storage layer's -// existing telemetry meter and tracer. -func (s *InstrumentedStorage) WrapMoleculeStepper(inner issueops.MoleculeStepper) issueops.MoleculeStepper { - return &instrumentedMoleculeStepper{storage: s, inner: inner} -} - -type instrumentedMoleculeStepper struct { - storage *InstrumentedStorage - inner issueops.MoleculeStepper -} - -func (m *instrumentedMoleculeStepper) Advance(ctx context.Context, req issueops.AdvanceRequest) (result issueops.AdvanceResult, err error) { - ctx, span, started := m.storage.op(ctx, "MoleculeStepper.Advance") - result, err = m.inner.Advance(ctx, req) - m.storage.done(ctx, span, started, err) - return result, err -} diff --git a/internal/telemetry/role_accessor_decorator_test.go b/internal/telemetry/role_accessor_decorator_test.go index d1fcdc0ce..cd5609f4c 100644 --- a/internal/telemetry/role_accessor_decorator_test.go +++ b/internal/telemetry/role_accessor_decorator_test.go @@ -10,7 +10,6 @@ import ( "testing" "github.com/steveyegge/beads/internal/storage" - "github.com/steveyegge/beads/internal/storage/externaldeps" "github.com/steveyegge/beads/issueops" "github.com/steveyegge/beads/memoryops" ) @@ -94,7 +93,7 @@ func TestInstrumentedStorageDeclaresEveryRoleAccessor(t *testing.T) { } // roleAccessorStore is a DoltStorage whose only real methods are the -// thirty-one role accessors, each answering with a distinguishable sentinel +// twenty-eight role accessors, each answering with a distinguishable sentinel // so a test can tell an instrumented surface from a passed-through one. // // TWO sentinels rather than one: memoryops.Memories.List and issueops.Reader.List @@ -133,9 +132,6 @@ func (s *roleAccessorStore) TreeWalker() (issueops.TreeWalker, error) { return s func (s *roleAccessorStore) GraphCounter() (issueops.GraphCounter, error) { return s.surface, s.err } -func (s *roleAccessorStore) BatchGetter() (issueops.BatchGetter, error) { - return s.surface, s.err -} func (s *roleAccessorStore) BlockingAnnotator() (issueops.BlockingAnnotator, error) { return s.surface, s.err } @@ -175,17 +171,8 @@ func (s *roleAccessorStore) BatchApplier() (issueops.BatchApplier, error) { func (s *roleAccessorStore) Releaser() (issueops.Releaser, error) { return s.surface, s.err } -func (s *roleAccessorStore) LeaseReclaimer() (issueops.LeaseReclaimer, error) { - return s.surface, s.err -} -func (s *roleAccessorStore) Heartbeater() (issueops.Heartbeater, error) { - return s.surface, s.err -} -func (s *roleAccessorStore) BlockedLister() (issueops.BlockedLister, error) { - return s.surface, s.err -} -// roleAccessorSentinel implements thirty of the thirty-one roles at +// roleAccessorSentinel implements twenty-seven of the twenty-eight roles at // once — every one but memoryops.Memories, whose List collides with // issueops.Reader.List and needs the second sentinel below. // Nothing calls its methods; identity is the whole point. @@ -270,9 +257,6 @@ func (*roleAccessorSentinel) CountReady(context.Context, issueops.ReadyRequest) func (*roleAccessorSentinel) CountEdges(context.Context, issueops.EdgeCountRequest) (issueops.EdgeCountResult, error) { return issueops.EdgeCountResult{}, nil } -func (*roleAccessorSentinel) GetMany(context.Context, issueops.GetManyRequest) (issueops.GetManyResult, error) { - return issueops.GetManyResult{}, nil -} func (*roleAccessorSentinel) Query(context.Context, issueops.QueryRequest) (issueops.IssuePage, error) { return issueops.IssuePage{}, nil @@ -315,15 +299,6 @@ func (*roleAccessorSentinel) CompareAndSetKey(context.Context, issueops.CompareA func (*roleAccessorSentinel) Release(context.Context, issueops.ReleaseRequest) (issueops.ReleaseResult, error) { return issueops.ReleaseResult{}, nil } -func (*roleAccessorSentinel) Reclaim(context.Context, issueops.ReclaimRequest) (issueops.ReclaimResult, error) { - return issueops.ReclaimResult{}, nil -} -func (*roleAccessorSentinel) Heartbeat(context.Context, issueops.HeartbeatRequest) (issueops.HeartbeatResult, error) { - return issueops.HeartbeatResult{}, nil -} -func (*roleAccessorSentinel) ListBlocked(context.Context, issueops.BlockedRequest) (issueops.BlockedResult, error) { - return issueops.BlockedResult{}, nil -} // memoryRoleSentinel is the memory role's sentinel — see // memoryRoleSentinel is the remaining role's sentinel — see @@ -379,7 +354,6 @@ func TestInstrumentedStorageInstrumentsEveryRoleAccessor(t *testing.T) { {"BlockingAnnotator", func() (any, error) { return wrapped.BlockingAnnotator() }, sentinel}, {"TreeWalker", func() (any, error) { return wrapped.TreeWalker() }, sentinel}, {"GraphCounter", func() (any, error) { return wrapped.GraphCounter() }, sentinel}, - {"BatchGetter", func() (any, error) { return wrapped.BatchGetter() }, sentinel}, {"Counter", func() (any, error) { return wrapped.Counter() }, sentinel}, {"WorkspaceConfig", func() (any, error) { return wrapped.WorkspaceConfig() }, sentinel}, {"Memories", func() (any, error) { return wrapped.Memories() }, memorySentinel}, @@ -400,9 +374,6 @@ func TestInstrumentedStorageInstrumentsEveryRoleAccessor(t *testing.T) { {"MetadataCAS", func() (any, error) { return wrapped.MetadataCAS() }, sentinel}, {"BatchApplier", func() (any, error) { return wrapped.BatchApplier() }, sentinel}, {"Releaser", func() (any, error) { return wrapped.Releaser() }, sentinel}, - {"LeaseReclaimer", func() (any, error) { return wrapped.LeaseReclaimer() }, sentinel}, - {"Heartbeater", func() (any, error) { return wrapped.Heartbeater() }, sentinel}, - {"BlockedLister", func() (any, error) { return wrapped.BlockedLister() }, sentinel}, } { t.Run(test.name, func(t *testing.T) { surface, err := test.got() @@ -416,60 +387,6 @@ func TestInstrumentedStorageInstrumentsEveryRoleAccessor(t *testing.T) { } } -// TestExternalDepsReadyCounterSurvivesThisLayer pins the span across the one -// decorator above this one that CANNOT recurse. cmd/bd wires -// hooks -> externaldeps -> telemetry -> store, and externaldeps has to build -// its own ready counter over itself so the total honors its external-dependency -// exclusions — recursing into the inner counter the way the accessors above do -// would count externally blocked issues as ready. That leaves handing the -// finished counter to WrapReadyCounter as the only way this layer keeps its -// turn, so an externaldeps override that skips it silently drops the -// storage.ReadyCounter.CountReady span for every text-mode `bd ready`. -// -// This and its blocked-listing twin below are the only tests that wire a real -// decorator above the instrumented store, which is why WrapReadyCounter's and -// WrapBlockedLister's sole callers outside this package are the things being -// pinned here. -func TestExternalDepsReadyCounterSurvivesThisLayer(t *testing.T) { - t.Setenv("BD_OTEL_STDOUT", "true") - inner := &roleAccessorStore{surface: &roleAccessorSentinel{}} - instrumented, ok := WrapStorage(inner).(*InstrumentedStorage) - if !ok { - t.Fatal("WrapStorage did not instrument the store; telemetry is disabled in this environment") - } - - counter, err := externaldeps.New(instrumented, nil, nil).ReadyCounter() - if err != nil { - t.Fatalf("externaldeps ReadyCounter() error = %v", err) - } - if _, ok := counter.(*instrumentedReadyCounter); !ok { - t.Fatalf("externaldeps ReadyCounter() = %T, want it wrapped by this layer; the storage.ReadyCounter.CountReady span stops being emitted for text-mode bd ready", counter) - } -} - -// TestExternalDepsBlockedListerSurvivesThisLayer is the same pin for the -// blocked listing. externaldeps builds a local store's lister over its own -// GetBlockedIssues, so the listing carries the external blockers that read -// adds, which leaves handing the finished lister to WrapBlockedLister as this -// layer's only turn: an override that skips it silently drops the -// storage.BlockedLister.ListBlocked span for every local `bd blocked`. -func TestExternalDepsBlockedListerSurvivesThisLayer(t *testing.T) { - t.Setenv("BD_OTEL_STDOUT", "true") - inner := &roleAccessorStore{surface: &roleAccessorSentinel{}} - instrumented, ok := WrapStorage(inner).(*InstrumentedStorage) - if !ok { - t.Fatal("WrapStorage did not instrument the store; telemetry is disabled in this environment") - } - - lister, err := externaldeps.New(instrumented, nil, nil).BlockedLister() - if err != nil { - t.Fatalf("externaldeps BlockedLister() error = %v", err) - } - if _, ok := lister.(*instrumentedBlockedLister); !ok { - t.Fatalf("externaldeps BlockedLister() = %T, want it wrapped by this layer; the storage.BlockedLister.ListBlocked span stops being emitted for local bd blocked", lister) - } -} - // TestInstrumentedStorageRoleAccessorsPropagateInnerErrors pins the other half // of the recursion: an accessor that recurses must not swallow the inner // store's refusal, nor hand back a wrapper around nothing. @@ -492,7 +409,6 @@ func TestInstrumentedStorageRoleAccessorsPropagateInnerErrors(t *testing.T) { {"BlockingAnnotator", func() (any, error) { return wrapped.BlockingAnnotator() }}, {"TreeWalker", func() (any, error) { return wrapped.TreeWalker() }}, {"GraphCounter", func() (any, error) { return wrapped.GraphCounter() }}, - {"BatchGetter", func() (any, error) { return wrapped.BatchGetter() }}, {"Counter", func() (any, error) { return wrapped.Counter() }}, {"WorkspaceConfig", func() (any, error) { return wrapped.WorkspaceConfig() }}, {"Memories", func() (any, error) { return wrapped.Memories() }}, @@ -513,9 +429,6 @@ func TestInstrumentedStorageRoleAccessorsPropagateInnerErrors(t *testing.T) { {"MetadataCAS", func() (any, error) { return wrapped.MetadataCAS() }}, {"BatchApplier", func() (any, error) { return wrapped.BatchApplier() }}, {"Releaser", func() (any, error) { return wrapped.Releaser() }}, - {"LeaseReclaimer", func() (any, error) { return wrapped.LeaseReclaimer() }}, - {"Heartbeater", func() (any, error) { return wrapped.Heartbeater() }}, - {"BlockedLister", func() (any, error) { return wrapped.BlockedLister() }}, } { t.Run(test.name, func(t *testing.T) { surface, err := test.got() diff --git a/internal/telemetry/storage.go b/internal/telemetry/storage.go index 98deca157..dbbf9c7c3 100644 --- a/internal/telemetry/storage.go +++ b/internal/telemetry/storage.go @@ -3,7 +3,6 @@ package telemetry import ( "context" "encoding/json" - "strings" "time" "go.opentelemetry.io/otel/attribute" @@ -276,17 +275,6 @@ func (s *InstrumentedStorage) SearchIssueIDs(ctx context.Context, query string, return ids, err } -func (s *InstrumentedStorage) SearchIssueSummaries(ctx context.Context, query string, filter types.IssueFilter) ([]*types.IssueSummary, error) { - attrs := []attribute.KeyValue{attribute.String("bd.query", query)} - ctx, span, t := s.op(ctx, "SearchIssueSummaries", attrs...) - summaries, err := s.inner.SearchIssueSummaries(ctx, query, filter) - if err == nil { - span.SetAttributes(attribute.Int("bd.result.count", len(summaries))) - } - s.done(ctx, span, t, err, attrs...) - return summaries, err -} - // ── Dependencies ──────────────────────────────────────────────────────────── func (s *InstrumentedStorage) AddDependency(ctx context.Context, dep *types.Dependency, actor string) error { @@ -402,23 +390,6 @@ func (s *InstrumentedStorage) RemoveLabel(ctx context.Context, issueID, label, a return err } -func (s *InstrumentedStorage) RenameLabel(ctx context.Context, oldLabel, newLabel, actor string) (renamed, merged int, ids []string, err error) { - attrs := []attribute.KeyValue{ - attribute.String("bd.label.old", oldLabel), - attribute.String("bd.label.new", newLabel), - } - ctx, span, t := s.op(ctx, "RenameLabel", attrs...) - renamed, merged, ids, err = s.inner.RenameLabel(ctx, oldLabel, newLabel, actor) - if err == nil { - span.SetAttributes( - attribute.Int("bd.result.count", renamed), - attribute.Int("bd.merged.count", merged), - ) - } - s.done(ctx, span, t, err, attrs...) - return renamed, merged, ids, err -} - func (s *InstrumentedStorage) GetLabels(ctx context.Context, issueID string) ([]string, error) { attrs := []attribute.KeyValue{attribute.String("bd.issue.id", issueID)} ctx, span, t := s.op(ctx, "GetLabels", attrs...) @@ -457,16 +428,6 @@ func (s *InstrumentedStorage) GetReadyWorkWithCounts(ctx context.Context, filter return v, err } -func (s *InstrumentedStorage) GetReadyWorkWithCountsAndTotal(ctx context.Context, filter types.WorkFilter) ([]*types.IssueWithCounts, int, error) { - ctx, span, t := s.op(ctx, "GetReadyWorkWithCountsAndTotal") - v, total, err := s.inner.GetReadyWorkWithCountsAndTotal(ctx, filter) - if err == nil { - span.SetAttributes(attribute.Int("bd.result.count", len(v))) - } - s.done(ctx, span, t, err) - return v, total, err -} - func (s *InstrumentedStorage) GetBlockedIssues(ctx context.Context, filter types.WorkFilter) ([]*types.BlockedIssue, error) { ctx, span, t := s.op(ctx, "GetBlockedIssues") v, err := s.inner.GetBlockedIssues(ctx, filter) @@ -597,41 +558,6 @@ func (s *InstrumentedStorage) GetAllConfig(ctx context.Context) (map[string]stri return v, err } -// GetConfigByPrefix forwards the domain.ConfigPrefixReader optional fast -// path when the wrapped store provides it, and otherwise falls back to -// instrumented GetAllConfig filtered in-process — so wrapping a store never -// costs it the capability. -// -// cmd/bd discovers that capability with storage.UnwrapStore, the repo's -// standard idiom, which peels all the way to the raw store: on that path the -// inner method is called directly and this span does not fire. The forwarder -// is kept deliberately rather than deleted — it preserves the capability for -// any caller holding the instrumented store, and it is the layer a -// peel-until-implements discovery would stop at if spans on prefix reads ever -// justify that non-standard peel. -func (s *InstrumentedStorage) GetConfigByPrefix(ctx context.Context, prefix string) (map[string]string, error) { - attrs := []attribute.KeyValue{attribute.String("bd.config.prefix", prefix)} - if pr, ok := s.inner.(interface { - GetConfigByPrefix(ctx context.Context, prefix string) (map[string]string, error) - }); ok { - ctx, span, t := s.op(ctx, "GetConfigByPrefix", attrs...) - v, err := pr.GetConfigByPrefix(ctx, prefix) - s.done(ctx, span, t, err, attrs...) - return v, err - } - all, err := s.GetAllConfig(ctx) - if err != nil { - return nil, err - } - out := make(map[string]string) - for k, v := range all { - if strings.HasPrefix(k, prefix) { - out[k] = v - } - } - return out, nil -} - func (s *InstrumentedStorage) SetLocalMetadata(ctx context.Context, key, value string) error { attrs := []attribute.KeyValue{attribute.String("bd.local_metadata.key", key)} ctx, span, t := s.op(ctx, "SetLocalMetadata", attrs...) diff --git a/internal/telemetry/storage_test.go b/internal/telemetry/storage_test.go index 9cdc2a97a..c3e6cd616 100644 --- a/internal/telemetry/storage_test.go +++ b/internal/telemetry/storage_test.go @@ -5,16 +5,8 @@ import ( "testing" "github.com/steveyegge/beads/internal/storage" - "github.com/steveyegge/beads/internal/storage/domain" ) -// InstrumentedStorage forwards the optional prefix-read capability, which -// callers find by type assertion — so a drifting method set would degrade -// them to full scans silently. Pinned from the test build rather than -// storage.go to keep the telemetry package free of a production dependency on -// storage/domain. -var _ domain.ConfigPrefixReader = (*InstrumentedStorage)(nil) - // fakeDoltStore is a stub DoltStorage used to assert wrapper identity and // type chain. The embedded interface is nil; tests must not call any of its // methods (interface-promoted calls would panic). diff --git a/internal/templates/BUILD.bazel b/internal/templates/BUILD.bazel deleted file mode 100644 index 799491071..000000000 --- a/internal/templates/BUILD.bazel +++ /dev/null @@ -1,73 +0,0 @@ -load("@rules_go//go:def.bzl", "go_library") - -go_library( - name = "templates", - srcs = ["agent_skill.go"], - embedsrcs = [ - "skills/beads/SKILL.md", - "skills/beads/agents/openai.yaml", - ], - importpath = "github.com/steveyegge/beads/internal/templates", - visibility = ["//:__subpackages__"], -) - -# --- begin repo_files (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) --- - -filegroup( - name = "repo_doc_files", - srcs = glob( - [ - "**/*.md", - "**/*.mdx", - ], - allow_empty = True, - exclude = ["**/node_modules/**"], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_go_srcs", - srcs = glob( - ["**/*.go"], - allow_empty = True, - exclude = [ - "**/*_test.go", - "**/node_modules/**", - ], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_go_test_srcs", - srcs = glob( - ["**/*_test.go"], - allow_empty = True, - exclude = ["**/node_modules/**"], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_other_files", - srcs = glob( - ["**"], - exclude = [ - "**/*.db", - "**/*.exe", - "**/*.go", - "**/*.md", - "**/*.mdx", - "**/*.out", - "**/*.prof", - "**/*.pyc", - "**/*.test", - "**/__pycache__/**", - "**/node_modules/**", - ], - ), - visibility = ["//:__subpackages__"], -) - -# --- end repo_files --- diff --git a/internal/templates/agents/BUILD.bazel b/internal/templates/agents/BUILD.bazel deleted file mode 100644 index 8ad3255df..000000000 --- a/internal/templates/agents/BUILD.bazel +++ /dev/null @@ -1,89 +0,0 @@ -load("@rules_go//go:def.bzl", "go_library", "go_test") - -go_library( - name = "agents", - srcs = [ - "agents.go", - "render.go", - ], - embedsrcs = [ - "defaults/agents.md.tmpl", - "defaults/beads-section-codex.md", - "defaults/beads-section-minimal.md", - "defaults/beads-section.md", - ], - importpath = "github.com/steveyegge/beads/internal/templates/agents", - visibility = ["//:__subpackages__"], -) - -go_test( - name = "agents_test", - srcs = [ - "agents_test.go", - "golden_test.go", - "render_test.go", - ], - data = glob(["testdata/**"]), - embed = [":agents"], -) - -# --- begin repo_files (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) --- - -filegroup( - name = "repo_doc_files", - srcs = glob( - [ - "**/*.md", - "**/*.mdx", - ], - allow_empty = True, - exclude = ["**/node_modules/**"], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_go_srcs", - srcs = glob( - ["**/*.go"], - allow_empty = True, - exclude = [ - "**/*_test.go", - "**/node_modules/**", - ], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_go_test_srcs", - srcs = glob( - ["**/*_test.go"], - allow_empty = True, - exclude = ["**/node_modules/**"], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_other_files", - srcs = glob( - ["**"], - exclude = [ - "**/*.db", - "**/*.exe", - "**/*.go", - "**/*.md", - "**/*.mdx", - "**/*.out", - "**/*.prof", - "**/*.pyc", - "**/*.test", - "**/__pycache__/**", - "**/node_modules/**", - ], - ), - visibility = ["//:__subpackages__"], -) - -# --- end repo_files --- diff --git a/internal/templates/agents/agents.go b/internal/templates/agents/agents.go index 8978d1914..c0eedc1e4 100644 --- a/internal/templates/agents/agents.go +++ b/internal/templates/agents/agents.go @@ -12,27 +12,11 @@ var defaultTemplate string //go:embed defaults/beads-section.md var beadsSection string -// quickReferencePushLine is the Quick Reference entry in the AGENTS.md -// template that tells agents to push beads data to the Dolt remote. -const quickReferencePushLine = "bd dolt push # Push beads data to remote\n" - // EmbeddedDefault returns the full AGENTS.md template content. func EmbeddedDefault() string { return defaultTemplate } -// EmbeddedDefaultWithOpts returns the AGENTS.md template with the text outside -// its managed section adjusted for opts: under the rule the managed section -// follows, the Quick Reference leaves out "bd dolt push" when no Dolt remote is -// configured or the workspace is declared no-push. The managed section is -// returned as embedded; ReplaceSectionWithOpts renders it for a profile. -func EmbeddedDefaultWithOpts(opts RenderOpts) string { - if !opts.omitsDoltPush() { - return defaultTemplate - } - return strings.ReplaceAll(defaultTemplate, "\n"+quickReferencePushLine, "\n") -} - // EmbeddedBeadsSection returns the beads integration section with markers. // The returned string is trimmed to match the existing agentsBeadsSection behavior // (no trailing newline after the end marker). diff --git a/internal/templates/agents/agents_test.go b/internal/templates/agents/agents_test.go index c6438a38e..3f394a8ae 100644 --- a/internal/templates/agents/agents_test.go +++ b/internal/templates/agents/agents_test.go @@ -5,7 +5,7 @@ import ( "testing" ) -func TestGoEmbedDefault(t *testing.T) { +func TestEmbeddedDefault(t *testing.T) { content := EmbeddedDefault() if content == "" { @@ -28,13 +28,13 @@ func TestGoEmbedDefault(t *testing.T) { } } -// TestGoEmbedDefaultArchitectureSummary guards GH#3683: agents reading the +// TestEmbeddedDefaultArchitectureSummary 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 TestGoEmbedDefaultArchitectureSummary(t *testing.T) { +func TestEmbeddedDefaultArchitectureSummary(t *testing.T) { content := EmbeddedDefault() required := []string{ @@ -59,7 +59,7 @@ func TestGoEmbedDefaultArchitectureSummary(t *testing.T) { } } -func TestGoEmbedBeadsSection(t *testing.T) { +func TestEmbeddedBeadsSection(t *testing.T) { section := EmbeddedBeadsSection() if section == "" { @@ -113,7 +113,7 @@ func TestDefaultContainsBothSections(t *testing.T) { } } -func TestGoEmbedDefaultManagedMarkerIsCurrent(t *testing.T) { +func TestEmbeddedDefaultManagedMarkerIsCurrent(t *testing.T) { content := EmbeddedDefault() idx := strings.Index(content, "\n" - if section := RenderSectionWithOpts(v.profile, v.opts); !strings.HasPrefix(section, marker) { - first, _, _ := strings.Cut(section, "\n") - t.Errorf("%s managed section marker = %q, want %q", v.name, first, strings.TrimSuffix(marker, "\n")) - } - } -} - -// TestManagedSectionNoPushMatchesNoRemote pins that a local-only workspace -// (no-push: true) gets the same managed section as one with no Dolt remote, -// whatever the remote state. -func TestManagedSectionNoPushMatchesNoRemote(t *testing.T) { - for _, profile := range []Profile{ProfileMinimal, ProfileFull} { - want := RenderSectionWithOpts(profile, RenderOpts{}) - for _, hasRemote := range []bool{false, true} { - if got := RenderSectionWithOpts(profile, RenderOpts{HasRemote: hasRemote, NoPush: true}); got != want { - t.Errorf("%s profile with NoPush (HasRemote=%v) differs from the no-remote section", profile, hasRemote) - } - } - } -} - -// assertGoldenFile compares got with the golden file at path, or rewrites the -// file when BD_UPDATE_GOLDEN=1. -func assertGoldenFile(t *testing.T, path, got string) { - t.Helper() - if os.Getenv("BD_UPDATE_GOLDEN") == "1" { - if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { - t.Fatalf("mkdir %s: %v", filepath.Dir(path), err) - } - if err := os.WriteFile(path, []byte(got), 0o644); err != nil { - t.Fatalf("write golden %s: %v", path, err) - } - t.Logf("updated golden %s", path) - return - } - want, err := os.ReadFile(path) - if err != nil { - t.Fatalf("read golden %s (regenerate with BD_UPDATE_GOLDEN=1): %v", path, err) - } - if got == string(want) { - return - } - t.Errorf("rendered output drifted from %s (regenerate with BD_UPDATE_GOLDEN=1 if intended)\n%s", path, firstGoldenDifference(string(want), got)) -} - -// firstGoldenDifference describes the first line on which want and got differ. -func firstGoldenDifference(want, got string) string { - wantLines := strings.Split(want, "\n") - gotLines := strings.Split(got, "\n") - line := func(lines []string, i int) string { - if i >= len(lines) { - return "(end of output)" - } - return fmt.Sprintf("%q", lines[i]) - } - for i := 0; i < len(wantLines) || i < len(gotLines); i++ { - if i >= len(wantLines) || i >= len(gotLines) || wantLines[i] != gotLines[i] { - return fmt.Sprintf("first difference at line %d:\n want: %s\n got: %s", i+1, line(wantLines, i), line(gotLines, i)) - } - } - return "outputs differ" -} diff --git a/internal/templates/agents/render.go b/internal/templates/agents/render.go index 108bf9227..84c6baa40 100644 --- a/internal/templates/agents/render.go +++ b/internal/templates/agents/render.go @@ -52,18 +52,6 @@ type RenderOpts struct { // NoPush indicates the rig is declared local-only (no-push: true in config). // When true, "bd dolt push" is omitted regardless of HasRemote. NoPush bool - // UserLevel words the section for a user-level instructions file, such as - // ~/.claude/CLAUDE.md, that the agent loads in every repository: instead - // of stating that the current project uses bd, it applies only in - // repositories that do, and so do its task-tracking rules. - UserLevel bool -} - -// omitsDoltPush reports whether rendered instructions leave out "bd dolt push": -// no Dolt remote is configured, or the workspace is declared no-push. The -// managed section and the AGENTS.md template around it share this rule. -func (o RenderOpts) omitsDoltPush() bool { - return !o.HasRemote || o.NoPush } // DefaultRenderOpts returns opts that assume a remote is configured, @@ -72,13 +60,6 @@ func DefaultRenderOpts() RenderOpts { return RenderOpts{HasRemote: true} } -// UserLevelRenderOpts returns the fixed opts for a user-level instructions -// file. No one repository's beads config applies to it, so it assumes a -// remote, as DefaultRenderOpts does. -func UserLevelRenderOpts() RenderOpts { - return RenderOpts{HasRemote: true, UserLevel: true} -} - // RenderSection returns the beads integration section for the given profile, // wrapped in markers that include version, profile, and hash metadata for freshness detection. // Assumes a Dolt remote is configured (backward-compatible). Use RenderSectionWithOpts @@ -233,36 +214,13 @@ func templateBodyWithOpts(profile Profile, opts RenderOpts) string { body = strings.TrimSuffix(body, "\n") } - if opts.omitsDoltPush() { + if !opts.HasRemote || opts.NoPush { body = stripDoltPushReferences(body) } - if opts.UserLevel { - body = userLevelWording.Replace(body) - } return body } -// userLevelScope opens a user-level section: the agent loads that file in -// every repository, including ones that do not use beads. -const userLevelScope = "This Beads block applies only in beads repositories: repositories that use **bd (beads)** for issue tracking, with a `.beads/` directory. Ignore it in any other repository." - -// userLevelWording rewords the lines of both profiles' bodies that state the -// current project uses bd, or that set a task-tracking rule with no condition, -// so they apply only in beads repositories (RenderOpts.UserLevel). -var userLevelWording = strings.NewReplacer( - "This project uses **bd (beads)** for issue tracking. Run `bd prime`", - userLevelScope+" In a beads repository, run `bd prime`", - "- Use `bd` for ALL task tracking", - "- In a beads repository, use `bd` for ALL task tracking", - "- Use `bd remember` for durable project facts", - "- In a beads repository, use `bd remember` for durable project facts", - "**IMPORTANT**: This project uses **bd (beads)** for ALL issue tracking.", - userLevelScope+"\n\n**IMPORTANT**: In a beads repository, use **bd (beads)** for ALL issue tracking.", - "- ✅ Use bd for ALL task tracking", - "- ✅ In a beads repository, use bd for ALL task tracking", -) - func normalizeEmbeddedMarkdown(content string) string { return strings.TrimRight(strings.ReplaceAll(content, "\r\n", "\n"), "\n") } diff --git a/internal/templates/agents/render_test.go b/internal/templates/agents/render_test.go index c0bd12d4a..40f5e1320 100644 --- a/internal/templates/agents/render_test.go +++ b/internal/templates/agents/render_test.go @@ -123,18 +123,6 @@ func TestRenderSectionMinimal(t *testing.T) { if strings.Contains(section, "### Priorities") { t.Error("minimal profile should not contain full priorities section") } - - // GH#6111: the memory bullet says which store each content class belongs - // in instead of prohibiting MEMORY.md, because harnesses ship a - // first-party memory whose index file has exactly that name. Assert the - // prohibition's absence rather than the replacement prose, so the pin - // survives any future rewording of the sentence. - if !strings.Contains(section, "bd remember") { - t.Error("minimal profile should still teach bd remember") - } - if strings.Contains(section, "MEMORY.md") { - t.Error("minimal profile should not name MEMORY.md") - } } func TestCodexSectionBody(t *testing.T) { @@ -158,16 +146,6 @@ func TestCodexSectionBody(t *testing.T) { if strings.Contains(body, "BEGIN BEADS") || strings.Contains(body, "END BEADS") { t.Error("CodexSectionBody should not include managed markers") } - - // GH#6111, as above: the memory bullet describes the split instead of - // banning per-tool memory files. This body spelled its prohibition "ad hoc - // memory files" and never named MEMORY.md, so checking only the file name - // would assert nothing about what this surface used to emit. - for _, unwanted := range []string{"MEMORY.md", "ad hoc memory files"} { - if strings.Contains(body, unwanted) { - t.Errorf("CodexSectionBody should not prohibit %q", unwanted) - } - } } func TestRenderSectionHashStability(t *testing.T) { @@ -541,37 +519,3 @@ func TestReplaceSectionWithOptsDetectsRemoteChange(t *testing.T) { t.Error("replaced content should not contain 'bd dolt push' with HasRemote=false") } } - -// A user-level section loads in every repository, so it must not say the -// current one uses bd, and its task-tracking rules hold only in repositories -// that do. The wording is matched against the templates' text, so a template -// edit that the rewording no longer finds fails here. -func TestRenderSectionUserLevelAppliesOnlyInBeadsRepositories(t *testing.T) { - for _, profile := range []Profile{ProfileMinimal, ProfileFull} { - section := RenderSectionWithOpts(profile, UserLevelRenderOpts()) - if strings.Contains(section, "This project uses") { - t.Errorf("%s user-level section says the current project uses bd:\n%s", profile, section) - } - if n := strings.Count(section, userLevelScope); n != 1 { - t.Errorf("%s user-level section states its scope %d times, want 1", profile, n) - } - } - rules := map[Profile][]string{ - ProfileMinimal: { - "- In a beads repository, use `bd` for ALL task tracking — do NOT use TodoWrite", - "- In a beads repository, use `bd remember` for durable project facts", - }, - ProfileFull: { - "**IMPORTANT**: In a beads repository, use **bd (beads)** for ALL issue tracking.", - "- ✅ In a beads repository, use bd for ALL task tracking", - }, - } - for profile, want := range rules { - section := RenderSectionWithOpts(profile, UserLevelRenderOpts()) - for _, rule := range want { - if !strings.Contains(section, rule) { - t.Errorf("%s user-level section lacks the scoped rule %q", profile, rule) - } - } - } -} diff --git a/internal/templates/agents/testdata/managed-section-full-no-remote.md b/internal/templates/agents/testdata/managed-section-full-no-remote.md deleted file mode 100644 index 526550030..000000000 --- a/internal/templates/agents/testdata/managed-section-full-no-remote.md +++ /dev/null @@ -1,129 +0,0 @@ - -## Issue Tracking with bd (beads) - -**IMPORTANT**: This project uses **bd (beads)** for ALL issue tracking. Do NOT use markdown TODOs, task lists, or other tracking methods. - -### Why bd? - -- Dependency-aware: Track blockers and relationships between issues -- Git-friendly: Dolt-powered version control with native sync -- Agent-optimized: JSON output, ready work detection, discovered-from links -- Prevents duplicate tracking systems and confusion - -### Quick Start - -**Check for ready work:** - -```bash -bd ready --json -``` - -**Create new issues:** - -```bash -bd create "Issue title" --description="Detailed context" -t bug|feature|task -p 0-4 --json -bd create "Issue title" --description="What this issue is about" -p 1 --deps discovered-from:bd-123 --json -``` - -**Claim and update:** - -```bash -bd update --claim --json -bd update bd-42 --priority 1 --json -``` - -**Complete work:** - -```bash -bd close bd-42 --reason "Completed" --json -``` - -### Issue Types - -- `bug` - Something broken -- `feature` - New functionality -- `task` - Work item (tests, docs, refactoring) -- `epic` - Large feature with subtasks -- `chore` - Maintenance (dependencies, tooling) - -### Priorities - -- `0` - Critical (security, data loss, broken builds) -- `1` - High (major features, important bugs) -- `2` - Medium (default, nice-to-have) -- `3` - Low (polish, optimization) -- `4` - Backlog (future ideas) - -### Workflow for AI Agents - -1. **Check ready work**: `bd ready` shows unblocked issues -2. **Claim your task atomically**: `bd update --claim` -3. **Work on it**: Implement, test, document -4. **Discover new work?** Create linked issue: - - `bd create "Found bug" --description="Details about what was found" -p 1 --deps discovered-from:` -5. **Complete**: `bd close --reason "Done"` - -### Quality -- Use `--acceptance` and `--design` fields when creating issues -- Use `--validate` to check description completeness - -### Lifecycle -- `bd defer ` / `bd supersede ` for issue management -- `bd stale` / `bd orphans` / `bd lint` for hygiene -- `bd human ` to flag for human decisions -- `bd formula list` / `bd mol pour ` for structured workflows - -### Sync - -bd stores issue history in Dolt: - -- Each write auto-commits to Dolt history -- Do not treat `.beads/issues.jsonl` as the sync protocol - -**Architecture in one line:** issues live in a local Dolt DB; sync uses `refs/dolt/data` on your git remote; `.beads/issues.jsonl` is a passive export. See https://github.com/gastownhall/beads/blob/main/docs/core-concepts/sync-concepts.md for details and anti-patterns. - -### Important Rules - -- ✅ Use bd for ALL task tracking -- ✅ Always use `--json` flag for programmatic use -- ✅ Link discovered work with `discovered-from` dependencies -- ✅ Check `bd ready` before asking "what should I work on?" -- ❌ Do NOT create markdown TODO lists -- ❌ Do NOT use external issue trackers -- ❌ Do NOT duplicate tracking systems - -For more details, see README.md and https://github.com/gastownhall/beads/blob/main/docs/getting-started/quickstart.md. - -## Agent Context Profiles - -The managed Beads block is task-tracking guidance, not permission to override repository, user, or orchestrator instructions. - -- **Conservative (default)**: Use `bd` for task tracking. Do not run git commits, git pushes, or Dolt remote sync unless explicitly asked. At handoff, report changed files, validation, and suggested next commands. -- **Minimal**: Keep tool instruction files as pointers to `bd prime`; use the same conservative git policy unless active instructions say otherwise. -- **Team-maintainer**: Only when the repository explicitly opts in, agents may close beads, run quality gates, commit, and push as part of session close. A current "do not commit" or "do not push" instruction still wins. - -## Session Completion - -This protocol applies when ending a Beads implementation workflow. It is subordinate to explicit user, repository, and orchestrator instructions. - -1. **File issues for remaining work** - Create beads for anything that needs follow-up -2. **Run quality gates** (if code changed) - Tests, linters, builds -3. **Update issue status** - Close finished work, update in-progress items -4. **Handle git/sync by active profile**: - ```bash - # Conservative/minimal/default: report status and proposed commands; wait for approval. - git status - - # Team-maintainer opt-in only, unless current instructions forbid it: - git pull --rebase - git push - git status - ``` -5. **Hand off** - Summarize changes, validation, issue status, and any blocked sync/commit/push step - -**Critical rules:** -- Explicit user or orchestrator instructions override this Beads block. -- Do not commit or push without clear authority from the active profile or the current user request. -- If a required sync or push is blocked, stop and report the exact command and error. - - diff --git a/internal/templates/agents/testdata/managed-section-full-user.md b/internal/templates/agents/testdata/managed-section-full-user.md deleted file mode 100644 index 56e8785fc..000000000 --- a/internal/templates/agents/testdata/managed-section-full-user.md +++ /dev/null @@ -1,133 +0,0 @@ - -## Issue Tracking with bd (beads) - -This Beads block applies only in beads repositories: repositories that use **bd (beads)** for issue tracking, with a `.beads/` directory. Ignore it in any other repository. - -**IMPORTANT**: In a beads repository, use **bd (beads)** for ALL issue tracking. Do NOT use markdown TODOs, task lists, or other tracking methods. - -### Why bd? - -- Dependency-aware: Track blockers and relationships between issues -- Git-friendly: Dolt-powered version control with native sync -- Agent-optimized: JSON output, ready work detection, discovered-from links -- Prevents duplicate tracking systems and confusion - -### Quick Start - -**Check for ready work:** - -```bash -bd ready --json -``` - -**Create new issues:** - -```bash -bd create "Issue title" --description="Detailed context" -t bug|feature|task -p 0-4 --json -bd create "Issue title" --description="What this issue is about" -p 1 --deps discovered-from:bd-123 --json -``` - -**Claim and update:** - -```bash -bd update --claim --json -bd update bd-42 --priority 1 --json -``` - -**Complete work:** - -```bash -bd close bd-42 --reason "Completed" --json -``` - -### Issue Types - -- `bug` - Something broken -- `feature` - New functionality -- `task` - Work item (tests, docs, refactoring) -- `epic` - Large feature with subtasks -- `chore` - Maintenance (dependencies, tooling) - -### Priorities - -- `0` - Critical (security, data loss, broken builds) -- `1` - High (major features, important bugs) -- `2` - Medium (default, nice-to-have) -- `3` - Low (polish, optimization) -- `4` - Backlog (future ideas) - -### Workflow for AI Agents - -1. **Check ready work**: `bd ready` shows unblocked issues -2. **Claim your task atomically**: `bd update --claim` -3. **Work on it**: Implement, test, document -4. **Discover new work?** Create linked issue: - - `bd create "Found bug" --description="Details about what was found" -p 1 --deps discovered-from:` -5. **Complete**: `bd close --reason "Done"` - -### Quality -- Use `--acceptance` and `--design` fields when creating issues -- Use `--validate` to check description completeness - -### Lifecycle -- `bd defer ` / `bd supersede ` for issue management -- `bd stale` / `bd orphans` / `bd lint` for hygiene -- `bd human ` to flag for human decisions -- `bd formula list` / `bd mol pour ` for structured workflows - -### Sync - -bd stores issue history in Dolt: - -- Each write auto-commits to Dolt history -- Use `bd dolt push`/`bd dolt pull` for remote sync -- Do not treat `.beads/issues.jsonl` as the sync protocol - -**Architecture in one line:** issues live in a local Dolt DB; sync uses `refs/dolt/data` on your git remote; `.beads/issues.jsonl` is a passive export. See https://github.com/gastownhall/beads/blob/main/docs/core-concepts/sync-concepts.md for details and anti-patterns. - -### Important Rules - -- ✅ In a beads repository, use bd for ALL task tracking -- ✅ Always use `--json` flag for programmatic use -- ✅ Link discovered work with `discovered-from` dependencies -- ✅ Check `bd ready` before asking "what should I work on?" -- ❌ Do NOT create markdown TODO lists -- ❌ Do NOT use external issue trackers -- ❌ Do NOT duplicate tracking systems - -For more details, see README.md and https://github.com/gastownhall/beads/blob/main/docs/getting-started/quickstart.md. - -## Agent Context Profiles - -The managed Beads block is task-tracking guidance, not permission to override repository, user, or orchestrator instructions. - -- **Conservative (default)**: Use `bd` for task tracking. Do not run git commits, git pushes, or Dolt remote sync unless explicitly asked. At handoff, report changed files, validation, and suggested next commands. -- **Minimal**: Keep tool instruction files as pointers to `bd prime`; use the same conservative git policy unless active instructions say otherwise. -- **Team-maintainer**: Only when the repository explicitly opts in, agents may close beads, run quality gates, commit, and push as part of session close. A current "do not commit" or "do not push" instruction still wins. - -## Session Completion - -This protocol applies when ending a Beads implementation workflow. It is subordinate to explicit user, repository, and orchestrator instructions. - -1. **File issues for remaining work** - Create beads for anything that needs follow-up -2. **Run quality gates** (if code changed) - Tests, linters, builds -3. **Update issue status** - Close finished work, update in-progress items -4. **Handle git/sync by active profile**: - ```bash - # Conservative/minimal/default: report status and proposed commands; wait for approval. - git status - - # Team-maintainer opt-in only, unless current instructions forbid it: - git pull --rebase - bd dolt push - git push - git status - ``` -5. **Hand off** - Summarize changes, validation, issue status, and any blocked sync/commit/push step - -**Critical rules:** -- Explicit user or orchestrator instructions override this Beads block. -- Do not commit or push without clear authority from the active profile or the current user request. -- If a required sync or push is blocked, stop and report the exact command and error. - - diff --git a/internal/templates/agents/testdata/managed-section-full.md b/internal/templates/agents/testdata/managed-section-full.md deleted file mode 100644 index 831c2b736..000000000 --- a/internal/templates/agents/testdata/managed-section-full.md +++ /dev/null @@ -1,131 +0,0 @@ - -## Issue Tracking with bd (beads) - -**IMPORTANT**: This project uses **bd (beads)** for ALL issue tracking. Do NOT use markdown TODOs, task lists, or other tracking methods. - -### Why bd? - -- Dependency-aware: Track blockers and relationships between issues -- Git-friendly: Dolt-powered version control with native sync -- Agent-optimized: JSON output, ready work detection, discovered-from links -- Prevents duplicate tracking systems and confusion - -### Quick Start - -**Check for ready work:** - -```bash -bd ready --json -``` - -**Create new issues:** - -```bash -bd create "Issue title" --description="Detailed context" -t bug|feature|task -p 0-4 --json -bd create "Issue title" --description="What this issue is about" -p 1 --deps discovered-from:bd-123 --json -``` - -**Claim and update:** - -```bash -bd update --claim --json -bd update bd-42 --priority 1 --json -``` - -**Complete work:** - -```bash -bd close bd-42 --reason "Completed" --json -``` - -### Issue Types - -- `bug` - Something broken -- `feature` - New functionality -- `task` - Work item (tests, docs, refactoring) -- `epic` - Large feature with subtasks -- `chore` - Maintenance (dependencies, tooling) - -### Priorities - -- `0` - Critical (security, data loss, broken builds) -- `1` - High (major features, important bugs) -- `2` - Medium (default, nice-to-have) -- `3` - Low (polish, optimization) -- `4` - Backlog (future ideas) - -### Workflow for AI Agents - -1. **Check ready work**: `bd ready` shows unblocked issues -2. **Claim your task atomically**: `bd update --claim` -3. **Work on it**: Implement, test, document -4. **Discover new work?** Create linked issue: - - `bd create "Found bug" --description="Details about what was found" -p 1 --deps discovered-from:` -5. **Complete**: `bd close --reason "Done"` - -### Quality -- Use `--acceptance` and `--design` fields when creating issues -- Use `--validate` to check description completeness - -### Lifecycle -- `bd defer ` / `bd supersede ` for issue management -- `bd stale` / `bd orphans` / `bd lint` for hygiene -- `bd human ` to flag for human decisions -- `bd formula list` / `bd mol pour ` for structured workflows - -### Sync - -bd stores issue history in Dolt: - -- Each write auto-commits to Dolt history -- Use `bd dolt push`/`bd dolt pull` for remote sync -- Do not treat `.beads/issues.jsonl` as the sync protocol - -**Architecture in one line:** issues live in a local Dolt DB; sync uses `refs/dolt/data` on your git remote; `.beads/issues.jsonl` is a passive export. See https://github.com/gastownhall/beads/blob/main/docs/core-concepts/sync-concepts.md for details and anti-patterns. - -### Important Rules - -- ✅ Use bd for ALL task tracking -- ✅ Always use `--json` flag for programmatic use -- ✅ Link discovered work with `discovered-from` dependencies -- ✅ Check `bd ready` before asking "what should I work on?" -- ❌ Do NOT create markdown TODO lists -- ❌ Do NOT use external issue trackers -- ❌ Do NOT duplicate tracking systems - -For more details, see README.md and https://github.com/gastownhall/beads/blob/main/docs/getting-started/quickstart.md. - -## Agent Context Profiles - -The managed Beads block is task-tracking guidance, not permission to override repository, user, or orchestrator instructions. - -- **Conservative (default)**: Use `bd` for task tracking. Do not run git commits, git pushes, or Dolt remote sync unless explicitly asked. At handoff, report changed files, validation, and suggested next commands. -- **Minimal**: Keep tool instruction files as pointers to `bd prime`; use the same conservative git policy unless active instructions say otherwise. -- **Team-maintainer**: Only when the repository explicitly opts in, agents may close beads, run quality gates, commit, and push as part of session close. A current "do not commit" or "do not push" instruction still wins. - -## Session Completion - -This protocol applies when ending a Beads implementation workflow. It is subordinate to explicit user, repository, and orchestrator instructions. - -1. **File issues for remaining work** - Create beads for anything that needs follow-up -2. **Run quality gates** (if code changed) - Tests, linters, builds -3. **Update issue status** - Close finished work, update in-progress items -4. **Handle git/sync by active profile**: - ```bash - # Conservative/minimal/default: report status and proposed commands; wait for approval. - git status - - # Team-maintainer opt-in only, unless current instructions forbid it: - git pull --rebase - bd dolt push - git push - git status - ``` -5. **Hand off** - Summarize changes, validation, issue status, and any blocked sync/commit/push step - -**Critical rules:** -- Explicit user or orchestrator instructions override this Beads block. -- Do not commit or push without clear authority from the active profile or the current user request. -- If a required sync or push is blocked, stop and report the exact command and error. - - diff --git a/internal/templates/agents/testdata/managed-section-minimal-no-remote.md b/internal/templates/agents/testdata/managed-section-minimal-no-remote.md deleted file mode 100644 index 1ab497bff..000000000 --- a/internal/templates/agents/testdata/managed-section-minimal-no-remote.md +++ /dev/null @@ -1,54 +0,0 @@ - -## Beads Issue Tracker - -This project uses **bd (beads)** for issue tracking. Run `bd prime` to see full workflow context and commands. - -### Quick Reference - -```bash -bd ready # Find available work -bd show # View issue details -bd update --claim # Claim work -bd close # Complete work -``` - -### Rules - -- Use `bd` for ALL task tracking — do NOT use TodoWrite, TaskCreate, or markdown TODO lists -- Run `bd prime` for detailed command reference and session close protocol -- Use `bd remember` for durable project facts — not per-tool memory files, which fragment across accounts; keep per-operator preferences in your harness's own memory - -**Architecture in one line:** issues live in a local Dolt DB; sync uses `refs/dolt/data` on your git remote; `.beads/issues.jsonl` is a passive export. See https://github.com/gastownhall/beads/blob/main/docs/core-concepts/sync-concepts.md for details and anti-patterns. - -## Agent Context Profiles - -The managed Beads block is task-tracking guidance, not permission to override repository, user, or orchestrator instructions. - -- **Conservative (default)**: Use `bd` for task tracking. Do not run git commits, git pushes, or Dolt remote sync unless explicitly asked. At handoff, report changed files, validation, and suggested next commands. -- **Minimal**: Keep tool instruction files as pointers to `bd prime`; use the same conservative git policy unless active instructions say otherwise. -- **Team-maintainer**: Only when the repository explicitly opts in, agents may close beads, run quality gates, commit, and push as part of session close. A current "do not commit" or "do not push" instruction still wins. - -## Session Completion - -This protocol applies when ending a Beads implementation workflow. It is subordinate to explicit user, repository, and orchestrator instructions. - -1. **File issues for remaining work** - Create beads for anything that needs follow-up -2. **Run quality gates** (if code changed) - Tests, linters, builds -3. **Update issue status** - Close finished work, update in-progress items -4. **Handle git/sync by active profile**: - ```bash - # Conservative/minimal/default: report status and proposed commands; wait for approval. - git status - - # Team-maintainer opt-in only, unless current instructions forbid it: - git pull --rebase - git push - git status - ``` -5. **Hand off** - Summarize changes, validation, issue status, and any blocked sync/commit/push step - -**Critical rules:** -- Explicit user or orchestrator instructions override this Beads block. -- Do not commit or push without clear authority from the active profile or the current user request. -- If a required sync or push is blocked, stop and report the exact command and error. - diff --git a/internal/templates/agents/testdata/managed-section-minimal-user.md b/internal/templates/agents/testdata/managed-section-minimal-user.md deleted file mode 100644 index 5c51e2c14..000000000 --- a/internal/templates/agents/testdata/managed-section-minimal-user.md +++ /dev/null @@ -1,55 +0,0 @@ - -## Beads Issue Tracker - -This Beads block applies only in beads repositories: repositories that use **bd (beads)** for issue tracking, with a `.beads/` directory. Ignore it in any other repository. In a beads repository, run `bd prime` to see full workflow context and commands. - -### Quick Reference - -```bash -bd ready # Find available work -bd show # View issue details -bd update --claim # Claim work -bd close # Complete work -``` - -### Rules - -- In a beads repository, use `bd` for ALL task tracking — do NOT use TodoWrite, TaskCreate, or markdown TODO lists -- Run `bd prime` for detailed command reference and session close protocol -- In a beads repository, use `bd remember` for durable project facts — not per-tool memory files, which fragment across accounts; keep per-operator preferences in your harness's own memory - -**Architecture in one line:** issues live in a local Dolt DB; sync uses `refs/dolt/data` on your git remote; `.beads/issues.jsonl` is a passive export. See https://github.com/gastownhall/beads/blob/main/docs/core-concepts/sync-concepts.md for details and anti-patterns. - -## Agent Context Profiles - -The managed Beads block is task-tracking guidance, not permission to override repository, user, or orchestrator instructions. - -- **Conservative (default)**: Use `bd` for task tracking. Do not run git commits, git pushes, or Dolt remote sync unless explicitly asked. At handoff, report changed files, validation, and suggested next commands. -- **Minimal**: Keep tool instruction files as pointers to `bd prime`; use the same conservative git policy unless active instructions say otherwise. -- **Team-maintainer**: Only when the repository explicitly opts in, agents may close beads, run quality gates, commit, and push as part of session close. A current "do not commit" or "do not push" instruction still wins. - -## Session Completion - -This protocol applies when ending a Beads implementation workflow. It is subordinate to explicit user, repository, and orchestrator instructions. - -1. **File issues for remaining work** - Create beads for anything that needs follow-up -2. **Run quality gates** (if code changed) - Tests, linters, builds -3. **Update issue status** - Close finished work, update in-progress items -4. **Handle git/sync by active profile**: - ```bash - # Conservative/minimal/default: report status and proposed commands; wait for approval. - git status - - # Team-maintainer opt-in only, unless current instructions forbid it: - git pull --rebase - bd dolt push - git push - git status - ``` -5. **Hand off** - Summarize changes, validation, issue status, and any blocked sync/commit/push step - -**Critical rules:** -- Explicit user or orchestrator instructions override this Beads block. -- Do not commit or push without clear authority from the active profile or the current user request. -- If a required sync or push is blocked, stop and report the exact command and error. - diff --git a/internal/templates/agents/testdata/managed-section-minimal.md b/internal/templates/agents/testdata/managed-section-minimal.md deleted file mode 100644 index 22c48412a..000000000 --- a/internal/templates/agents/testdata/managed-section-minimal.md +++ /dev/null @@ -1,55 +0,0 @@ - -## Beads Issue Tracker - -This project uses **bd (beads)** for issue tracking. Run `bd prime` to see full workflow context and commands. - -### Quick Reference - -```bash -bd ready # Find available work -bd show # View issue details -bd update --claim # Claim work -bd close # Complete work -``` - -### Rules - -- Use `bd` for ALL task tracking — do NOT use TodoWrite, TaskCreate, or markdown TODO lists -- Run `bd prime` for detailed command reference and session close protocol -- Use `bd remember` for durable project facts — not per-tool memory files, which fragment across accounts; keep per-operator preferences in your harness's own memory - -**Architecture in one line:** issues live in a local Dolt DB; sync uses `refs/dolt/data` on your git remote; `.beads/issues.jsonl` is a passive export. See https://github.com/gastownhall/beads/blob/main/docs/core-concepts/sync-concepts.md for details and anti-patterns. - -## Agent Context Profiles - -The managed Beads block is task-tracking guidance, not permission to override repository, user, or orchestrator instructions. - -- **Conservative (default)**: Use `bd` for task tracking. Do not run git commits, git pushes, or Dolt remote sync unless explicitly asked. At handoff, report changed files, validation, and suggested next commands. -- **Minimal**: Keep tool instruction files as pointers to `bd prime`; use the same conservative git policy unless active instructions say otherwise. -- **Team-maintainer**: Only when the repository explicitly opts in, agents may close beads, run quality gates, commit, and push as part of session close. A current "do not commit" or "do not push" instruction still wins. - -## Session Completion - -This protocol applies when ending a Beads implementation workflow. It is subordinate to explicit user, repository, and orchestrator instructions. - -1. **File issues for remaining work** - Create beads for anything that needs follow-up -2. **Run quality gates** (if code changed) - Tests, linters, builds -3. **Update issue status** - Close finished work, update in-progress items -4. **Handle git/sync by active profile**: - ```bash - # Conservative/minimal/default: report status and proposed commands; wait for approval. - git status - - # Team-maintainer opt-in only, unless current instructions forbid it: - git pull --rebase - bd dolt push - git push - git status - ``` -5. **Hand off** - Summarize changes, validation, issue status, and any blocked sync/commit/push step - -**Critical rules:** -- Explicit user or orchestrator instructions override this Beads block. -- Do not commit or push without clear authority from the active profile or the current user request. -- If a required sync or push is blocked, stop and report the exact command and error. - diff --git a/internal/testutil/BUILD.bazel b/internal/testutil/BUILD.bazel deleted file mode 100644 index 28367d0eb..000000000 --- a/internal/testutil/BUILD.bazel +++ /dev/null @@ -1,293 +0,0 @@ -load("@rules_go//go:def.bzl", "go_library", "go_test") -load("@rules_shell//shell:sh_test.bzl", "sh_test") - -go_library( - name = "testutil", - srcs = [ - "container_provider.go", - "container_provider_windows.go", - "dockercontext.go", - "git_env.go", - "git_test_hooks.go", - "localdoltserver.go", - "localdoltserver_linux.go", - "localdoltserver_other.go", - "suite_temp.go", - "testdoltbranch.go", - "testdoltcommon.go", - "testdoltreap.go", - "testdoltserver.go", - "testdoltserver_windows.go", - "tmpfs.go", - ], - importpath = "github.com/steveyegge/beads/internal/testutil", - visibility = ["//:__subpackages__"], - deps = [ - "//internal/execenv", - "//internal/storage/doltutil", - "//internal/testutil/bazeltest", - "@com_github_go_sql_driver_mysql//:mysql", - ] + select({ - "@rules_go//go/platform:aix": [ - "@com_github_testcontainers_testcontainers_go//:testcontainers-go", - "@com_github_testcontainers_testcontainers_go//exec", - "@com_github_testcontainers_testcontainers_go_modules_dolt//:dolt", - ], - "@rules_go//go/platform:android": [ - "@com_github_testcontainers_testcontainers_go//:testcontainers-go", - "@com_github_testcontainers_testcontainers_go//exec", - "@com_github_testcontainers_testcontainers_go_modules_dolt//:dolt", - ], - "@rules_go//go/platform:darwin": [ - "@com_github_testcontainers_testcontainers_go//:testcontainers-go", - "@com_github_testcontainers_testcontainers_go//exec", - "@com_github_testcontainers_testcontainers_go_modules_dolt//:dolt", - ], - "@rules_go//go/platform:dragonfly": [ - "@com_github_testcontainers_testcontainers_go//:testcontainers-go", - "@com_github_testcontainers_testcontainers_go//exec", - "@com_github_testcontainers_testcontainers_go_modules_dolt//:dolt", - ], - "@rules_go//go/platform:freebsd": [ - "@com_github_testcontainers_testcontainers_go//:testcontainers-go", - "@com_github_testcontainers_testcontainers_go//exec", - "@com_github_testcontainers_testcontainers_go_modules_dolt//:dolt", - ], - "@rules_go//go/platform:illumos": [ - "@com_github_testcontainers_testcontainers_go//:testcontainers-go", - "@com_github_testcontainers_testcontainers_go//exec", - "@com_github_testcontainers_testcontainers_go_modules_dolt//:dolt", - ], - "@rules_go//go/platform:ios": [ - "@com_github_testcontainers_testcontainers_go//:testcontainers-go", - "@com_github_testcontainers_testcontainers_go//exec", - "@com_github_testcontainers_testcontainers_go_modules_dolt//:dolt", - ], - "@rules_go//go/platform:js": [ - "@com_github_testcontainers_testcontainers_go//:testcontainers-go", - "@com_github_testcontainers_testcontainers_go//exec", - "@com_github_testcontainers_testcontainers_go_modules_dolt//:dolt", - ], - "@rules_go//go/platform:linux": [ - "@com_github_testcontainers_testcontainers_go//:testcontainers-go", - "@com_github_testcontainers_testcontainers_go//exec", - "@com_github_testcontainers_testcontainers_go_modules_dolt//:dolt", - ], - "@rules_go//go/platform:netbsd": [ - "@com_github_testcontainers_testcontainers_go//:testcontainers-go", - "@com_github_testcontainers_testcontainers_go//exec", - "@com_github_testcontainers_testcontainers_go_modules_dolt//:dolt", - ], - "@rules_go//go/platform:openbsd": [ - "@com_github_testcontainers_testcontainers_go//:testcontainers-go", - "@com_github_testcontainers_testcontainers_go//exec", - "@com_github_testcontainers_testcontainers_go_modules_dolt//:dolt", - ], - "@rules_go//go/platform:osx": [ - "@com_github_testcontainers_testcontainers_go//:testcontainers-go", - "@com_github_testcontainers_testcontainers_go//exec", - "@com_github_testcontainers_testcontainers_go_modules_dolt//:dolt", - ], - "@rules_go//go/platform:plan9": [ - "@com_github_testcontainers_testcontainers_go//:testcontainers-go", - "@com_github_testcontainers_testcontainers_go//exec", - "@com_github_testcontainers_testcontainers_go_modules_dolt//:dolt", - ], - "@rules_go//go/platform:qnx": [ - "@com_github_testcontainers_testcontainers_go//:testcontainers-go", - "@com_github_testcontainers_testcontainers_go//exec", - "@com_github_testcontainers_testcontainers_go_modules_dolt//:dolt", - ], - "@rules_go//go/platform:solaris": [ - "@com_github_testcontainers_testcontainers_go//:testcontainers-go", - "@com_github_testcontainers_testcontainers_go//exec", - "@com_github_testcontainers_testcontainers_go_modules_dolt//:dolt", - ], - "//conditions:default": [], - }), -) - -go_test( - name = "testutil_test", - srcs = [ - "dockercontext_test.go", - "git_env_test.go", - "gm2g3g5r_failopen_test.go", - "gm2g3g5r_failopen_unix_test.go", - "gm2g3g5r_failopen_windows_test.go", - "localdoltserver_test.go", - "suite_temp_test.go", - "testdoltbranch_test.go", - "testdoltreap_test.go", - "testdoltserver_env_test.go", - "testdoltserver_unreachable_test.go", - "tmpfs_test.go", - ], - data = glob(["testdata/**"]), - embed = [":testutil"], - deps = [ - "//internal/storage/doltutil", - "@com_github_go_sql_driver_mysql//:mysql", - ] + select({ - "@rules_go//go/platform:aix": [ - "@com_github_testcontainers_testcontainers_go//:testcontainers-go", - "@com_github_testcontainers_testcontainers_go_modules_dolt//:dolt", - ], - "@rules_go//go/platform:android": [ - "@com_github_testcontainers_testcontainers_go//:testcontainers-go", - "@com_github_testcontainers_testcontainers_go_modules_dolt//:dolt", - ], - "@rules_go//go/platform:darwin": [ - "@com_github_testcontainers_testcontainers_go//:testcontainers-go", - "@com_github_testcontainers_testcontainers_go_modules_dolt//:dolt", - ], - "@rules_go//go/platform:dragonfly": [ - "@com_github_testcontainers_testcontainers_go//:testcontainers-go", - "@com_github_testcontainers_testcontainers_go_modules_dolt//:dolt", - ], - "@rules_go//go/platform:freebsd": [ - "@com_github_testcontainers_testcontainers_go//:testcontainers-go", - "@com_github_testcontainers_testcontainers_go_modules_dolt//:dolt", - ], - "@rules_go//go/platform:illumos": [ - "@com_github_testcontainers_testcontainers_go//:testcontainers-go", - "@com_github_testcontainers_testcontainers_go_modules_dolt//:dolt", - ], - "@rules_go//go/platform:ios": [ - "@com_github_testcontainers_testcontainers_go//:testcontainers-go", - "@com_github_testcontainers_testcontainers_go_modules_dolt//:dolt", - ], - "@rules_go//go/platform:js": [ - "@com_github_testcontainers_testcontainers_go//:testcontainers-go", - "@com_github_testcontainers_testcontainers_go_modules_dolt//:dolt", - ], - "@rules_go//go/platform:linux": [ - "@com_github_testcontainers_testcontainers_go//:testcontainers-go", - "@com_github_testcontainers_testcontainers_go_modules_dolt//:dolt", - ], - "@rules_go//go/platform:netbsd": [ - "@com_github_testcontainers_testcontainers_go//:testcontainers-go", - "@com_github_testcontainers_testcontainers_go_modules_dolt//:dolt", - ], - "@rules_go//go/platform:openbsd": [ - "@com_github_testcontainers_testcontainers_go//:testcontainers-go", - "@com_github_testcontainers_testcontainers_go_modules_dolt//:dolt", - ], - "@rules_go//go/platform:osx": [ - "@com_github_testcontainers_testcontainers_go//:testcontainers-go", - "@com_github_testcontainers_testcontainers_go_modules_dolt//:dolt", - ], - "@rules_go//go/platform:plan9": [ - "@com_github_testcontainers_testcontainers_go//:testcontainers-go", - "@com_github_testcontainers_testcontainers_go_modules_dolt//:dolt", - ], - "@rules_go//go/platform:qnx": [ - "@com_github_testcontainers_testcontainers_go//:testcontainers-go", - "@com_github_testcontainers_testcontainers_go_modules_dolt//:dolt", - ], - "@rules_go//go/platform:solaris": [ - "@com_github_testcontainers_testcontainers_go//:testcontainers-go", - "@com_github_testcontainers_testcontainers_go_modules_dolt//:dolt", - ], - "//conditions:default": [], - }), -) - -# testutil_test on the dolt-server lane: the package's own tests of the -# test-server backends, with the local backend selected and required, so -# TestDoltServerFingerprint's local half (formerly pr.yml's "Test (Dolt -# server fingerprint)" job, every PR) and the TestLocalDoltServer_* tests run -# against the pinned dolt in the test wrapper's runfiles. Every other -# configuration of testutil_test passes BEADS_TEST_SKIP=dolt and skips them. -# The container half needs docker and the image, so no Bazel target runs it: -# it runs under plain `go test` (the container backend is the default there) -# on a host that has both, and TestPinnedDoltCLIMatchesContainerImage -# (//scripts) keeps the image tag on the pinned release. -# -# Tags: -# dolt-server: starts hermetic dolt sql-servers from the pinned dolt in the -# test wrapper's runfiles (BEADS_TEST_DOLT_SERVER=local); excluded from -# --config=prcore/ci, selected by --config=doltserver. -sh_test( - name = "testutil_dolt_test", - size = "medium", - srcs = ["//tools/bazel:go_test_variant.sh"], - args = ["$(rootpath :testutil_test)"], - data = [":testutil_test"], - env = { - "BEADS_TEST_DOLT_SERVER": "local", - "BEADS_TEST_REQUIRE_DOLT_CONTAINER": "1", - }, - tags = ["dolt-server"], -) - -# The pinned dolt sql-server image, read by //scripts:scripts_test (the CI -# pins agree) and //test/docsync (the docs' pin agrees). -exports_files( - ["testdoltcommon.go"], - visibility = [ - "//scripts:__pkg__", - "//test/docsync:__pkg__", - ], -) - -# --- begin repo_files (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) --- - -filegroup( - name = "repo_doc_files", - srcs = glob( - [ - "**/*.md", - "**/*.mdx", - ], - allow_empty = True, - exclude = ["**/node_modules/**"], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_go_srcs", - srcs = glob( - ["**/*.go"], - allow_empty = True, - exclude = [ - "**/*_test.go", - "**/node_modules/**", - ], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_go_test_srcs", - srcs = glob( - ["**/*_test.go"], - allow_empty = True, - exclude = ["**/node_modules/**"], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_other_files", - srcs = glob( - ["**"], - exclude = [ - "**/*.db", - "**/*.exe", - "**/*.go", - "**/*.md", - "**/*.mdx", - "**/*.out", - "**/*.prof", - "**/*.pyc", - "**/*.test", - "**/__pycache__/**", - "**/node_modules/**", - ], - ), - visibility = ["//:__subpackages__"], -) - -# --- end repo_files --- diff --git a/internal/testutil/bazeltest/BUILD.bazel b/internal/testutil/bazeltest/BUILD.bazel deleted file mode 100644 index cdb9d6d9e..000000000 --- a/internal/testutil/bazeltest/BUILD.bazel +++ /dev/null @@ -1,76 +0,0 @@ -load("@rules_go//go:def.bzl", "go_library", "go_test") - -go_library( - name = "bazeltest", - srcs = ["bazeltest.go"], - importpath = "github.com/steveyegge/beads/internal/testutil/bazeltest", - visibility = ["//:__subpackages__"], -) - -go_test( - name = "bazeltest_test", - srcs = ["bazeltest_test.go"], - data = glob(["testdata/**"]), - embed = [":bazeltest"], -) - -# --- begin repo_files (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) --- - -filegroup( - name = "repo_doc_files", - srcs = glob( - [ - "**/*.md", - "**/*.mdx", - ], - allow_empty = True, - exclude = ["**/node_modules/**"], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_go_srcs", - srcs = glob( - ["**/*.go"], - allow_empty = True, - exclude = [ - "**/*_test.go", - "**/node_modules/**", - ], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_go_test_srcs", - srcs = glob( - ["**/*_test.go"], - allow_empty = True, - exclude = ["**/node_modules/**"], - ), - visibility = ["//:__subpackages__"], -) - -filegroup( - name = "repo_other_files", - srcs = glob( - ["**"], - exclude = [ - "**/*.db", - "**/*.exe", - "**/*.go", - "**/*.md", - "**/*.mdx", - "**/*.out", - "**/*.prof", - "**/*.pyc", - "**/*.test", - "**/__pycache__/**", - "**/node_modules/**", - ], - ), - visibility = ["//:__subpackages__"], -) - -# --- end repo_files --- diff --git a/internal/testutil/bazeltest/bazeltest.go b/internal/testutil/bazeltest/bazeltest.go deleted file mode 100644 index a22e9d2d1..000000000 --- a/internal/testutil/bazeltest/bazeltest.go +++ /dev/null @@ -1,236 +0,0 @@ -// Package bazeltest resolves repository paths for tests so the same test code -// works under plain `go test` (working directory inside the package, full -// checkout on disk), under `bazel test` locally, and under remote execution, -// where the only filesystem a test sees is its declared runfiles. -// -// Under plain `go test` every helper reduces to the pre-Bazel behavior, so -// adopting it never changes what `go test` does. -// -// Repository-root resolution order: -// -// 1. BEADS_TEST_REPO_ROOT, an explicit override (debug escape hatch); it is -// honored only when it names a directory holding go.mod -// 2. under Bazel, the runfiles workspace root ($TEST_SRCDIR/$TEST_WORKSPACE); -// it holds exactly the files the test declares in its BUILD `data` -// 3. a walk up from the working directory to go.mod (plain `go test`) -package bazeltest - -import ( - "fmt" - "os" - "path/filepath" - "strings" - "testing" -) - -// RepoRootEnv names the environment variable that overrides repository-root -// resolution for every helper in this package. -const RepoRootEnv = "BEADS_TEST_REPO_ROOT" - -// IsBazel reports whether the test binary runs under `bazel test`. -func IsBazel() bool { - return os.Getenv("TEST_SRCDIR") != "" -} - -// runfilesDir returns the absolute runfiles directory, or "" outside Bazel. -func runfilesDir() string { - dir := os.Getenv("RUNFILES_DIR") - if dir == "" { - dir = os.Getenv("TEST_SRCDIR") - } - if dir == "" { - return "" - } - if abs, err := filepath.Abs(dir); err == nil { - return abs - } - return dir -} - -// workspaceRoot returns the runfiles directory of the main repository, or "" -// outside Bazel. -func workspaceRoot() string { - rf := runfilesDir() - if rf == "" { - return "" - } - ws := os.Getenv("TEST_WORKSPACE") - if ws == "" { - ws = "_main" - } - root := filepath.Join(rf, ws) - if fi, err := os.Stat(root); err != nil || !fi.IsDir() { - return "" - } - return root -} - -// envRoot returns BEADS_TEST_REPO_ROOT and whether it is usable: set and -// holding a go.mod, so a stale or mistyped value never redirects a scan. -func envRoot() (string, bool) { - root := os.Getenv(RepoRootEnv) - if root == "" { - return "", false - } - _, err := os.Stat(filepath.Join(root, "go.mod")) - return root, err == nil -} - -// OverrideRoot returns the repository root when BEADS_TEST_REPO_ROOT names a -// directory holding go.mod or the test runs under Bazel, and "" otherwise. It is the drop-in condition -// for existing path helpers: -// -// if root := bazeltest.OverrideRoot(); root != "" { -// return filepath.Join(root, "docs") -// } -// // pre-Bazel resolution, unchanged under plain go test -// -// Under Bazel the returned tree holds only the test's declared data. -func OverrideRoot() string { - if root, ok := envRoot(); ok { - return root - } - return workspaceRoot() -} - -// RepoRoot resolves the repository root; see the package comment for the -// resolution order. It fails the test when no root can be found. -func RepoRoot(t testing.TB) string { - t.Helper() - if root, ok := envRoot(); ok { - return root - } else if root != "" { - t.Fatalf("%s=%s has no go.mod", RepoRootEnv, root) - } - if root := workspaceRoot(); root != "" { - return root - } - wd, err := os.Getwd() - if err != nil { - t.Fatalf("getwd: %v", err) - } - for dir := wd; ; dir = filepath.Dir(dir) { - if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil { - return dir - } - if filepath.Dir(dir) == dir { - t.Fatalf("could not locate go.mod above %s", wd) - } - } -} - -// CallerDir returns the directory of a source file reported by -// runtime.Caller. Under Bazel the reported path is workspace-relative, so the -// directory is rebuilt from repoPkg (for example "test/docsync") under the -// runfiles workspace root. Outside Bazel it is filepath.Dir(callerFile). -func CallerDir(callerFile, repoPkg string) string { - if root := OverrideRoot(); root != "" { - return filepath.Join(root, filepath.FromSlash(repoPkg)) - } - return filepath.Dir(callerFile) -} - -// Runfile resolves a runfiles-relative path, the form produced by -// $(rlocationpath