beads/cmd/bd/init_metrics_test.go
Bee 4eeb41f7d6
fix(test): reap dolt sql-servers leaked by killed test runs, and fail the suite that leaks (#6441)
* fix(test): reap dolt sql-servers leaked by killed test runs

A `go test -timeout` panic skips every t.Cleanup and TestMain defer, so a
test run killed that way left its `dolt sql-server` running forever. Two
gaps kept anything from ever noticing:

1. On darwin the deleted-cwd signal could not fire. macOS lsof prints the
   bare path of a working directory that has been unlinked — no
   " (deleted)" suffix, unlike Linux's /proc/<pid>/cwd — so readDarwinCwd
   always reported deleted=false and selectOrphanTestServerPIDs' orphan arm
   was dead code on this platform. It now stats the path and treats ENOENT
   (and only ENOENT) as deletion, via a pure parseDarwinCwd that takes the
   stat function so the classification is unit-testable.

2. Nothing ever looked at a DEAD run's temp root again. Each suite swept
   only its own root, after its own m.Run() — the one moment a killed run
   never reaches. New in internal/doltserver: WriteSuiteOwnerMarker claims a
   root with the owning PID, and SweepDeadSuiteRoots globs a suite's prefix
   at the START of the next run and reaps only roots whose recorded owner is
   gone. An unclaimed root and a root whose owner is alive (a parallel
   package, a second `go test`) are both left exactly as they are, so this
   keeps SweepOrphanedTestServers' contract: never a shared temp dir, never
   another suite's live server. decideSuiteRoot holds that judgment alone
   and is table-tested; the liveness probe is build-tagged darwin/linux with
   an always-alive stub elsewhere, making the helper a no-op there.

Wired into the cmd/bd and internal/storage/dolt TestMains, plus a new
TestMain for internal/storage/uow — the package that produced the observed
orphan and had no suite lifecycle at all. It pins TMPDIR under a suite root
so every t.TempDir() is sweepable, and newTestUOWProvider's cleanup now
reads the backend pid file before proxy.Shutdown and fails the test if that
server is still alive five seconds later, so a leak is visible where it is
caused rather than inferred later from a sweep.

Each wired TestMain reports a non-empty post-run sweep loudly, and fails the
run on it only under BEADS_TEST_FAIL_ON_LEAK=1 — opt-in so one flaky test
cannot redden a package for everyone. No workflow sets it yet.

Tests: go test ./internal/doltserver/ -run 'Sweep|Orphan|Suite' -count=1;
go test ./internal/storage/uow/ -run TestUOWDependencyEditorContract -count=1
-timeout 10m (real dolt); go test ./cmd/bd/ -run 'TestMain|TestVersion'
-count=1 -timeout 5m; go build ./...; go vet over the three packages;
gofmt -l . clean; golangci-lint (gosec + misspell) clean over the touched
packages. Verified on a live box: the orphan with the deleted cwd was
reaped, while the production server, an unclaimed suite root's server, and
an unrelated dolt server were all left running.

Refs: wy-j2zc8q

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01S1sjyiTjodNqNqEzEFbKAE

* fix(test): bound the sweep to temp roots and verify before killing

Round 2 on the leaked-test-server sweep, addressing an adversarial review of
2d9d656b5. Every finding was about the same thing: the first pass reached
further than its own doc comments claimed.

1. selectOrphanTestServerPIDs' deleted-cwd arm was root-independent, and the
   darwin fix had just brought it to life on macOS. A PRODUCTION server is
   spawned with cmd.Dir = <workspace>/.beads/dolt, so a developer who moved
   that workspace — or whose external volume unmounted — would have their
   live server reaped by any test run on the box. The arm now also requires
   the deleted path to have been under a temp root (tempDirRoots: os.TempDir
   plus /tmp, each in both its literal and symlink-resolved form, which is
   what makes macOS's /var/folders vs /private/var/folders pair match). This
   was a live hazard on linux too.

2. SweepDeadSuiteRoots promised it only touched marker-owned dead roots, but
   reached SweepOrphanedTestServers, whose deleted-cwd arm spans the whole
   box — at suite START, with sibling packages mid-run. It now uses a new
   strictly root-scoped entry point, sweepServersUnderRoots, with the shared
   SIGTERM/SIGKILL loop factored out as reapServerPIDs on each platform. The
   end-of-run sweep in the TestMains still uses the broad one, which finding
   1 has now bounded to temp dirs.

3. The uow cleanup's exit check blind-killed a bare PID. The server is not
   this process's child, so its PID is reusable, and proxy.Shutdown
   deliberately REFUSES to signal a record it cannot verify — killing that
   very PID defeated the protection. It now keeps the whole pidfile record,
   runs only when Shutdown returned an error (success already kills and
   confirms), and gates both the liveness check and the kill on
   procid.Verify / procid.Open with the recorded birth token. Timeout raised
   to 30s, since Shutdown's own budget is 5s + 2s.

4. os.RemoveAll deletes the marker early, so a partial failure left an
   unclaimed root that no later run could ever touch. Removal now does a
   chmod u+w walk first (mirroring cmd/bd's forceRemoveAll, which exists
   because that root doubles as an isolated $HOME full of read-only module
   cache entries), and on a still-failing removal it rewrites the marker with
   the same dead PID so the next run retries.

5. internal/storage/dolt created its root and ran the sweep above the
   isHelperSubprocess early return: eight concurrent helpers each swept and
   each claimed a root that os.Exit then abandoned. Moved below it; a helper
   inherits the parent's live BEADS_TEST_CIRCUIT_DIR through the BEADS_
   allowlist in integration.FilterEnv.

6. SweepDeadSuiteRoots globs a world-writable directory, so a planted root
   with a dead-PID marker could aim it at a tree of someone else's choosing.
   It now requires the root's uid to be os.Getuid() (unix-only file, accept-
   all stub elsewhere, where the sweep is inert anyway).

7. The symlink leg of the selection test skips instead of failing where
   os.Symlink is unprivileged. 8. "favour" -> "favor".

Tests: go test ./internal/doltserver/ -run 'Sweep|Orphan|Suite|Darwin'
-count=1 -v (49 cases, incl. new ones for a moved production workspace, an
unmounted volume, the observed orphan in both macOS path forms, the
root-scoped selection, canonicalRoots, tempDirRoots, the marker-restore path
and the read-only-directory removal); gofmt -l . clean; go vet over
doltserver, uow, cmd/bd and storage/dolt; GOOS=windows and GOOS=linux go vet
./internal/doltserver/; golangci-lint clean over all four trees;
scripts/check-build-tags.sh clean; go build ./...; go test -c for uow,
cmd/bd and storage/dolt. The three live dolt sql-servers on the box were
untouched throughout.

Refs: wy-j2zc8q

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01S1sjyiTjodNqNqEzEFbKAE

* fix(test): isolate helper circuit state, re-verify late exits, bound TMPDIR

Round 3, from the verification pass on efb7a1c39 (PASS-WITH-NITS).

1. internal/storage/dolt: moving the suite root below the isHelperSubprocess
   return also took the per-helper circuit-breaker directory with it, so all
   eight of TestMultiProcessSchemaInit's helpers would have inherited the
   parent's BEADS_TEST_CIRCUIT_DIR and read-modify-written ONE file keyed
   host:port:db, concurrently — a fresh flake risk in the exact test the move
   was meant to protect. The helper branch now makes its own temp directory
   and overrides the variable, as it effectively did before. No sweep and no
   owner marker for helpers: they start no server, and a helper that leaves
   through os.Exit would only abandon a marked root.

2. internal/storage/uow: requireBackendExited could observe the server alive
   and then have it exit before procid.Open/Kill, which report a plain
   "does not match token" that IsProcessGone does not match — so a server
   that stopped a few milliseconds late was reported as one that survived
   Shutdown entirely. Both failure paths now re-verify once and stay silent
   when the process is gone; only a verifiably-live process is a t.Errorf. A
   record with no birth token now logs the skip instead of returning
   silently, so it cannot pass for a clean check.

3. internal/doltserver: tempDirRoots now drops roots too broad to be evidence
   of anything. TMPDIR is an ordinary environment variable, so os.TempDir()
   can hand back "/" or a home directory, and either would put every
   workspace on the box back inside the deleted-cwd arm. isCredibleTempRoot
   rejects the filesystem root, a relative path, and any directory that is or
   contains $HOME, while /tmp, /private/tmp and /var/folders/… survive.

4. The identical SIGTERM/SIGKILL loop in sweep_darwin.go and sweep_linux.go
   is now one reapServerPIDs in sweep_reap_unix.go, taking the platform's
   identity probe as an isServer validator. It sits in a darwin||linux file
   rather than the portable sweep.go because the signaling is POSIX; the
   selection logic that decides which pids reach it stays platform-agnostic
   in sweep.go.

Tests: go test ./internal/doltserver/ -run 'Sweep|Orphan|Suite|Darwin|TempDir'
-count=1 (53 cases; new TestTempDirRootsRejectsOverbroadTMPDIR drives
TMPDIR=/ and TMPDIR=$HOME through the real os.TempDir, and
TestIsCredibleTempRoot tables the predicate); gofmt -l . clean; go vet over
doltserver, uow, storage/dolt and cmd/bd; GOOS=windows and GOOS=linux go vet
./internal/doltserver/; golangci-lint clean over all four trees;
scripts/check-build-tags.sh clean; go build ./...; go test -c for uow,
cmd/bd and storage/dolt.

Refs: wy-j2zc8q

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01S1sjyiTjodNqNqEzEFbKAE

* fix(sweep): keep /tmp credible when HOME is a sandbox under it (wy-j2zc8q)

PR core went red on every Linux runner: TestTempDirRootsBoundTheOrphanArm
and TestTempDirRootsRejectsOverbroadTMPDIR reported tempDirRoots() empty.
Cause: scripts/ci/lib/test-env.sh exports HOME under a mktemp -d root in
/tmp, and isCredibleTempRoot rejected any root that CONTAINS the home —
so /tmp itself was thrown out and the deleted-cwd arm silently disabled on
exactly the boxes whose killed runs it exists to clean up after. macOS
never showed it: the sandbox home lands under /var/folders, and the
hardcoded /tmp survived.

The carve-out: a home living inside the fixed /tmp fallback is a SANDBOX
home that anchors no workspaces, so a root that merely contains it stays
credible. A root that IS the home is still dropped whatever the home looks
like, and the sandbox test is judged against the hardcoded /tmp, never
os.TempDir(), so TMPDIR=/home with HOME=/home/runner still reads as a real
home under an overbroad root.

Tests: the overbroad-TMPDIR test now pins HOME to a real-shaped
non-temp path (the ambient one is the sandbox on CI, where "a workspace
under HOME" and "a path under /tmp" are the same path), plus two new
subtests for the sandbox shape and a sandbox row set in the predicate
table. Reproduced the red and the fix on Linux in docker with
HOME=/tmp/beads-test-env-abc/home; also green with HOME=/home/runner and
/root, and on macOS.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QEmu5ZSFoappn7s9yUQ3aF

* fix(test): stop leaking daemonized shared-server dolt from test fixtures (wy-j2zc8q)

Every uow fixture that provisions a shared-server workspace now registers
ONE cleanup, verifiedShutdownCleanup, which stops the proxy and its
`dolt sql-server` and — when proxy.Shutdown reports an error — asserts the
recorded backend pid is actually gone (procid birth-token gated) instead of
printing a t.Logf line into a passing test. Nine copies of the silent idiom
are gone; an AST guard fails the package if a fixture provisions a workspace
without the cleanup, or calls proxy.Shutdown by hand.

A reaped server now FAILS the suite that leaked it (ApplyLeakPolicy), with
BEADS_TEST_ALLOW_LEAK=1 as the triage-only downgrade. The previous default
warned on stderr and exited 0, which is how all three recurrences rode green
suites and were rediscovered from a dev box's process table.

The dead-run root sweep (owner marker + SweepDeadSuiteRoots) is extended to
the remaining packages that pin a suite temp root and start servers:
cmd/bd/doctor (cgo + !cgo), cmd/bd/doctor/fix (cgo + !cgo), internal/beads.

* test(uow): guard pairs shutdownOnInterrupt with verifiedShutdownCleanup, exempts validation-error fixtures (wy-j2zc8q)

Salvaged from the executor's worktree after the quiet-hours stop: rules A/B/C
over *testing.T functions, non-vacuity counters per rule.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01YWogqyZMByH2fYqUcafXvw

* fix(sweep): treat the per-user temp tree as a sandbox home root (wy-j2zc8q)

isSandboxHome whitelisted only /tmp, so on macOS a suite that pins HOME to a
t.TempDir() (cmd/bd/test_repo_beads_guard_test.go:128,
internal/beads/testmain_test.go:50) put HOME inside os.TempDir() itself
(/var/folders/xx/yy/T). isCredibleTempRoot then dropped os.TempDir() as "a
directory containing the user's home", tempDirRoots() collapsed to
[/tmp /private/tmp], and the deleted-cwd arm of SweepOrphanedTestServers was
inert on every Mac.

Judge sandbox-ness against a FIXED platform table (isUnderFixedTempRoots /
fixedTempRoots): /tmp everywhere, plus /var/folders on darwin,
canonicalRoots-expanded so /private/var/folders matches. TMPDIR still never
votes on its own credibility, so TMPDIR=/home with HOME=/home/runner keeps
reading as a real home under an overbroad root.

goos is a parameter, not a read of runtime.GOOS, so the darwin row is pinned
from Linux too.

Ran: go test -count=1 -run
'TestSandboxHomeUnderPerUserTempRoot|TestTempDirRoots|TestIsCredibleTempRoot|TestCanonicalRoots'
./internal/doltserver/ (ok); the pre-fix repro
HOME=$TMPDIR/fake-home-probe go test -run TestTempDirRootsBoundTheOrphanArm
now passes.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BVJ7A7qWZYATyg2TsqggSA

* test(uow): exempt rule C by explicit name marker, not an error assertion (wy-j2zc8q)

The old rule-C exemption — "a function calling require.Error/ErrorContains
asserts the constructor rejected its arguments" — inferred intent from the
body, and TestNewExternalDoltServerUOWProvider_PreexistingDirtyDatabaseIsNotHealed
satisfied it by accident: it provisions a REAL external server, require.NoError
on the constructor, then asserts an error from a later query. Deleting BOTH
cleanup helpers from it left the guard green (mutation G).

Exempt only on an explicit, greppable name marker (_ValidationErrors), and
check the marker is not a lie: an exempted function must assert a rejection
(require.Error/ErrorContains) and must assert nothing succeeded (no
require.NoError/assert.NoError) — closing the rename-to-dodge path too. No
non-vacuity floor on the exemption count: if the marker stops matching, rule C
only gets stricter and the validation fixtures fail loudly.

Positive controls (both reverted):
  - mutation G (drop both helpers from PreexistingDirty…) → FAIL:
    "TestNewExternalDoltServerUOWProvider_PreexistingDirtyDatabaseIsNotHealed
    provisions a shared-server workspace but never calls
    verifiedShutdownCleanup(t, storeRootDir)"
  - mutation G renamed to wear the marker → FAIL: "wears the
    \"_ValidationErrors\" exemption but calls require.NoError/assert.NoError"

Ran: go test -count=1 ./internal/storage/uow/ -run
'TestEveryServerFixtureRegistersVerifiedCleanup' (ok; census: rule A paired 9,
rule C checked 8, exempted 2). go vet ./internal/storage/uow/ clean.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BVJ7A7qWZYATyg2TsqggSA

* test(doctor): fail the doctor and fix suites on a swept leak (wy-j2zc8q)

ApplyLeakPolicy was wired into 4 of 7 TestMains; the three doctor-side suites
called SweepOrphanedTestServers and discarded its result, so a leaked dolt
sql-server there was reaped silently and the run still went green — the exact
blindness the policy exists to remove.

Four call sites across the three suites, in the pattern
cmd/bd/test_repo_beads_guard_test.go:52-54 and
internal/storage/uow/testmain_test.go:65-66 already use:
  cmd/bd/doctor/testmain_nocgo_test.go     -> "cmd/bd/doctor"
  cmd/bd/doctor/dolt_e2e_test.go (cgo)     -> "cmd/bd/doctor"
  cmd/bd/doctor/fix/testmain_cgo_test.go   -> "cmd/bd/doctor/fix"
  cmd/bd/doctor/fix/testmain_nocgo_test.go -> "cmd/bd/doctor/fix"

Both build-tag variants compile.

Ran: go vet ./cmd/bd/doctor/... (0 issues); CGO_ENABLED=0 go vet
./cmd/bd/doctor/... (0 issues); go build ./... OK; go vet ./internal/doltserver/
./internal/storage/uow/ ./cmd/bd/doctor/... OK; gofmt -l internal cmd empty;
GOOS=linux go build ./... OK; GOOS=windows go build ./... OK.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BVJ7A7qWZYATyg2TsqggSA

* fix(sweep): pin darwin's fixed temp roots to their resolved forms (wy-j2zc8q)

fixedTempRoots("darwin") listed {"/tmp", "/var/folders"} and left the
/private/… forms to canonicalRoots' filepath.EvalSymlinks, which reads the
HOST filesystem: it adds them on a Mac and nothing at all on Linux. The
platform table is a claim ABOUT darwin that TestSandboxHomeUnderPerUserTempRoot
deliberately judges from any platform, so ubuntu CI saw
isUnderFixedTempRoots("/private/var/folders/…/fake-home", "darwin") = false.

Spell the resolved forms out in the table instead — "/private/tmp" and
"/private/var/folders" — so the answer is host-independent. canonicalRoots
dedups, so on a Mac they are exactly what it would have added anyway, and the
darwin lsof cwd form now matches from any judging platform. Linux rows are
unchanged.

The subtest is also given a direct assertion on the table's contents: the
isUnderFixedTempRoots rows alone cannot catch a dropped literal when they run
on a Mac (EvalSymlinks puts it back), so the mutation that started this would
have passed locally forever.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018MPryBr9EE2Px9crZ4NrTT

* test(bd): move the darwin sweep guard onto the leak-fails-the-suite contract (wy-j2zc8q)

TestRunTestsAndSweepReapsOrphanedServer predates ApplyLeakPolicy (097757b3e,
wy-s4zdj) and asserted the OLD policy: a swept orphan still exits 0. With the
policy in place it fails on macOS CI with runTestsAndSweep() = 1, want 0 —
the test was pinning exactly the behavior this PR removes.

Restate it as the two-case contract it is now: the existing case expects 1
(a swept leak fails an otherwise-passing suite), and a sibling sets
doltserver.AllowLeakEnv to "1" and expects 0. Both still assert the fake
server was actually reaped; the fake-dolt shape and the reap wait are
unchanged.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018MPryBr9EE2Px9crZ4NrTT

* fix(sweep): report the leaked server's working directory, not just its PID (wy-j2zc8q)

Both leak lines named PIDs only:

  Info: swept 1 orphaned test dolt sql-server process(es): [21881]
  FAIL: cmd/bd leaked 1 dolt sql-server process(es) [21881]; swept them.

By the time anyone reads that, 21881 has been dead for minutes and identifies
nothing — so a package with hundreds of tests reported a leak with no way to
find which fixture caused it. The sweep already knew the answer: every
candidate carries the cwd it was serving (serverCandidate.cwd), and a leaked
test server's cwd is the .beads/dolt under the suite's temp tree, which Go
names after the test that made it (<root>/TestSomeName1234/001).

Thread it through. selectCandidatePIDs/selectOrphanTestServerPIDs become
selectServers/selectOrphanTestServers returning a new exported SweptServer
{PID, Cwd}, reapServerPIDs becomes reapServers over the same type, and
SweepOrphanedTestServers and ApplyLeakPolicy carry it end to end — every
existing call site is a pass-through from the one to the other, so they are
unchanged. SweptServer.String renders "<pid> cwd=<dir>", which is what the
two stderr lines now print:

  Info: swept 1 ... process(es): [21881 cwd=/tmp/TestFoo123/001/.beads/dolt]

Tests: the darwin ps/lsof selection path now asserts whole SweptServers
(cwd included) rather than PIDs; TestSweptServerNamesItsDirectory pins the
rendering; TestApplyLeakPolicyNamesTheLeakingDirectory captures stderr and
pins the cwd in both the FAIL and the downgraded Warning line; and
TestReapServersReportsTheLeakedDirectory pins the Info line against a real
reaped process. sweep_other.go (non-unix) keeps the same shape.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018MPryBr9EE2Px9crZ4NrTT

* fix(test): stop the shared dolt sql-server three init fixtures daemonize (wy-j2zc8q)

The leak the new guard catches. `bd init` in shared-server mode takes the
shouldInitSharedGlobalDB branch (cmd/bd/init.go:1417) and calls
doltserver.Start(sharedDir), which DAEMONIZES — cmd.Process.Release(), no
Wait, cmd.Dir = <sharedDir>/dolt (doltserver.go:1443-1446). Three cmd/bd
fixtures run that as a subprocess with BEADS_DOLT_SHARED_SERVER=1 and never
stop it, so the server outlives the `bd` that started it, the test that ran
it and the suite whose temp HOME it is serving:

  cmd/bd/dolt_worktree_remote_test.go:34  (--prefix remote-sync)
  cmd/bd/init_test.go:2096                (--prefix bare-fallback)
  cmd/bd/init_test.go:2318                (--prefix bare-core)

Evidence: a full local `go test ./cmd/bd/` left
`dolt sql-server --config <suite root>/.beads/shared-server/dolt-server-config.yaml`
running with cwd /private/var/folders/…/beads-bd-tests-1850365081/.beads/
shared-server/dolt and databases beads_global + remote_sync — the remote-sync
prefix names the fixture. All three resolve the SAME $HOME/.beads/shared-server,
so only the last starter leaves a survivor; the process table names one while
three carry the defect.

stopSharedServerCleanup (new cmd/bd/shared_server_cleanup_test.go) is the one
cleanup they now register, before the first subprocess so the t.Fatalf paths
are covered: record the pid from doltserver.IsRunning BEFORE stopping (a clean
Stop deletes the pid file), capture its procid birth token, doltserver.Stop,
then assert the recorded process actually left — t.Errorf and a force-kill if
it did not. Birth-token gated throughout: the server is not this process's
child, so its PID is reusable the moment it exits. Same shape as
verifiedShutdownCleanup in internal/storage/uow (35faf60e5).

Verified both ways on the same two tests:
  before: FAIL: cmd/bd leaked 1 dolt sql-server process(es)
          [4217 cwd=/private/var/folders/…/beads-bd-tests-1902359010/.beads/shared-server/dolt]
  after:  no Info/FAIL leak line, no sql-server left under any suite root
The three assertion failures in those tests (dolt_worktree_remote_test.go
sync.remote, init_test.go DoltMode server/embedded) reproduce identically with
and without this commit — pre-existing, untouched here.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018MPryBr9EE2Px9crZ4NrTT

* fix(test): stop the shared and proxied servers the init-metrics fixture daemonizes (wy-j2zc8q)

The post-run sweep on 164661ce4c named a fourth leaker on both CI runners:
`bd-metrics-home-*/.beads/shared-server/dolt`, TestInitMetricsEmittedPerDoltMode's
shared_server_via_flag case. That fixture runs `bd init --shared-server` with
HOME set only in the subprocess env, so the daemonized shared server lives
under a tree the in-process stopSharedServerCleanup never looks at. The same
table's proxied_server_via_flag case leaves a proxy plus its backend server
under <repo>/.beads/dolt with proxy.pid / proxy-child.pid records that no
dolt-server.pid cleanup can see.

- stopDoltServerCleanup(t, beadsDir): the existing verified stop, now with
  an explicit beads directory; stopSharedServerCleanup keeps its signature.
- stopProxiedServerCleanup(t, proxyRoot): proxy.Shutdown (the verified stop
  `bd dolt stop` performs) when either record exists.
- runBdInitForMetrics registers both before the subprocess runs, and the
  shared case now passes BEADS_DOLT_SERVER_PORT=<free port> so it actually
  starts a server on a box whose 3308 is already taken instead of silently
  skipping the path (the previously unused extraEnv field carries it).

Probe: with the two registrations commented out the sweep reports both
servers (`swept 2 ... bd-metrics-home-*/.beads/shared-server/dolt ...
bd-metrics-repo-*/.beads/dolt`); with them in place the package is ok.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HXZ9wbaFUnzRWcMj7iBxmq

* fix(test): scope suite leak attribution and verify cleanup (bd-154w)

Maintainer fixup for PR #6441, review attempts 1, 2, and 3.

Wire dbproxy/server into the marked suite-root lifecycle and verify the
standard fixture's backend shutdown before its directory is removed.
Capture synthetic leak-policy stderr, share the suite-root guard helper,
and document the manual repro harness's unconditional Kill/Wait cleanup.

Use SweepSuiteTestServers for every failure-producing post-run sweep,
including CGO, non-CGO, and integration callers. It delegates to the
existing strict root selector so a clean suite cannot reap another live
suite's deleted-cwd leak or fail in its place. Keep abandoned-run cleanup
separate through owner-checked SweepDeadSuiteRoots, and deprecate the
legacy global sweep for TestMain use without deleting contributor tests.
Pin and mark the integration suite's temporary root to retain detection.

Add a Linux process regression proving clean B preserves A's deleted-cwd
server and passes, then A reaps its own server and fails. Retain dead-owner
cleanup coverage and guard both integration temporary-directory APIs.

Pin the storage, CLI, and internal/beads suite roots before fixtures run,
with guards for both temporary-directory APIs. Preserve the storage helper
subprocess early return and the original parent for abandoned-root cleanup.
A Linux subprocess regression exercises the real storage TestMain: an
owned deleted-cwd fixture fails its suite while a foreign process survives.
Keep the CLI sandbox HOME beside fixtures so ~/.beads cannot become a
fixture ancestor and interfere with worktree repository discovery.

Keep integration server data owned but allocate its Unix socket in a short,
private directory with explicit cleanup. Exercise a data path exceeding
the Unix socket path budget while retaining the real SQL round trip.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Agent-Signature: Codex-unknown-model-unknown-reasoning on behalf of Julian Knutsen

---------

Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
Co-authored-by: Eddie the Engineer <julianknutsen@users.noreply.github.com>
2026-09-22 15:08:40 -07:00

641 lines
19 KiB
Go

//go:build cgo
package main
import (
"bytes"
"encoding/json"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"testing"
"github.com/steveyegge/beads/internal/metrics"
"github.com/steveyegge/beads/internal/testutil"
)
type metricsEvent struct {
AppName string `json:"app_name"`
Events []struct {
Name string `json:"name"`
Attributes []struct {
Key string `json:"key"`
Value string `json:"value"`
} `json:"attributes"`
} `json:"events"`
}
func readInitEvent(t *testing.T, home, expectedCommand string) metricsEvent {
t.Helper()
dir := filepath.Join(home, ".beads", "eventsData")
entries, err := os.ReadDir(dir)
if err != nil {
t.Fatalf("read eventsData dir: %v", err)
}
var evtqs []string
for _, e := range entries {
if strings.HasSuffix(e.Name(), ".evtq") {
evtqs = append(evtqs, e.Name())
}
}
if len(evtqs) == 0 {
t.Fatalf("expected at least 1 .evtq file, got 0")
}
for _, name := range evtqs {
body, err := os.ReadFile(filepath.Join(dir, name))
if err != nil {
t.Fatalf("read evtq %s: %v", name, err)
}
var got metricsEvent
if err := json.Unmarshal(body, &got); err != nil {
t.Fatalf("unmarshal evtq %s: %v\n%s", name, err, body)
}
if got.commandAttr() == expectedCommand {
return got
}
}
t.Fatalf("no .evtq with command=%s among %v", expectedCommand, evtqs)
return metricsEvent{}
}
func (e metricsEvent) commandAttr() string {
if len(e.Events) == 0 {
return ""
}
for _, a := range e.Events[0].Attributes {
if a.Key == "command" {
return a.Value
}
}
return ""
}
func (e metricsEvent) attr(key string) (string, bool) {
if len(e.Events) == 0 {
return "", false
}
for _, a := range e.Events[0].Attributes {
if a.Key == key {
return a.Value, true
}
}
return "", false
}
func metricsTestEnv(home string, extra ...string) []string {
base := bdEnv(home)
out := make([]string, 0, len(base)+len(extra)+1)
for _, e := range base {
if strings.HasPrefix(e, "BD_DISABLE_METRICS=") || strings.HasPrefix(e, "DO_NOT_TRACK=") {
continue
}
out = append(out, e)
}
out = append(out, "BD_DISABLE_EVENT_FLUSH=1")
return append(out, extra...)
}
func runBdInitForMetrics(t *testing.T, home string, extraEnv []string, args ...string) {
t.Helper()
bd := buildEmbeddedBD(t)
repo, err := testTempDir("bd-metrics-repo-*")
if err != nil {
t.Fatalf("temp repo: %v", err)
}
initGitRepoAt(t, repo)
// `bd init --shared-server` daemonizes a dolt sql-server under
// <home>/.beads/shared-server — <home> being the HOME this fixture gives
// ONLY to the subprocess, so the in-process SharedServerPath would look in
// the wrong tree — and `--proxied-server` leaves a proxy plus its backend
// server under <repo>/.beads/dolt. Neither was ever stopped; the post-run
// sweep named this fixture by both cwds (wy-j2zc8q). Registered before
// the subprocess so the t.Fatalf paths are covered too.
stopDoltServerCleanup(t, filepath.Join(home, ".beads", "shared-server"))
stopProxiedServerCleanup(t, filepath.Join(repo, ".beads", "dolt"))
full := append([]string{"init", "--non-interactive", "--quiet"}, args...)
cmd := exec.Command(bd, full...)
cmd.Dir = repo
cmd.Env = metricsTestEnv(home, extraEnv...)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
_ = cmd.Run()
}
func TestInitMetricsEmittedPerDoltMode(t *testing.T) {
cases := []struct {
name string
extraArgs []string
extraEnv []string
expectedCommand string
freeServerPort bool
}{
{
name: "embedded_default",
expectedCommand: "init-embedded",
},
{
name: "server_via_flag",
extraArgs: []string{"--server"},
expectedCommand: "init-server",
},
{
name: "shared_server_via_flag",
extraArgs: []string{"--shared-server"},
expectedCommand: "init-shared-server",
// The shared server binds the default port unless told
// otherwise; on a box where 3308 is already someone's dolt the
// init fails before it starts anything, and this case would
// silently stop exercising the shared path.
freeServerPort: true,
},
{
name: "proxied_server_via_flag",
extraArgs: []string{"--proxied-server"},
expectedCommand: "init-proxied-server",
},
}
for _, tc := range cases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
home, err := testTempDir("bd-metrics-home-*")
if err != nil {
t.Fatalf("temp home: %v", err)
}
env := tc.extraEnv
if tc.freeServerPort {
port, err := testutil.FindFreePort()
if err != nil {
t.Fatalf("find free port: %v", err)
}
env = append(env, "BEADS_DOLT_SERVER_PORT="+strconv.Itoa(port))
}
runBdInitForMetrics(t, home, env, tc.extraArgs...)
evt := readInitEvent(t, home, tc.expectedCommand)
if evt.AppName != "beads" {
t.Errorf("app_name = %q, want %q", evt.AppName, "beads")
}
if len(evt.Events) != 1 {
t.Fatalf("events len = %d, want 1", len(evt.Events))
}
if evt.Events[0].Name != "cli_command" {
t.Errorf("event name = %q, want %q", evt.Events[0].Name, "cli_command")
}
if got, _ := evt.attr("command"); got != tc.expectedCommand {
t.Errorf("command attr = %q, want %q", got, tc.expectedCommand)
}
if got, ok := evt.attr("dolt_mode"); ok {
t.Errorf("dolt_mode attr should no longer be set, got %q", got)
}
})
}
}
func writeUserMetricsDisabled(t *testing.T, home string, disabled bool) {
t.Helper()
dir := filepath.Join(home, ".config", "bd")
if err := os.MkdirAll(dir, 0o755); err != nil {
t.Fatalf("mkdir user config: %v", err)
}
body := []byte("metrics.disabled: false\n")
if disabled {
body = []byte("metrics.disabled: true\n")
}
if err := os.WriteFile(filepath.Join(dir, "config.yaml"), body, 0o644); err != nil {
t.Fatalf("write user config: %v", err)
}
}
func evtqFilesIn(t *testing.T, home string) []string {
t.Helper()
dir := filepath.Join(home, ".beads", "eventsData")
entries, err := os.ReadDir(dir)
if err != nil {
return nil
}
var out []string
for _, e := range entries {
if strings.HasSuffix(e.Name(), ".evtq") {
out = append(out, e.Name())
}
}
return out
}
func runBdInitWithEnv(t *testing.T, home string, extraEnv []string) {
t.Helper()
bd := buildEmbeddedBD(t)
repo, err := testTempDir("bd-metrics-repo-*")
if err != nil {
t.Fatalf("temp repo: %v", err)
}
initGitRepoAt(t, repo)
cmd := exec.Command(bd, "init", "--non-interactive", "--quiet")
cmd.Dir = repo
cmd.Env = metricsTestEnv(home, extraEnv...)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
_ = cmd.Run()
}
func TestInitMetricsEnvConfigPrecedence(t *testing.T) {
cases := []struct {
name string
setUserConfig bool
configDisabled bool
envVar []string
wantEvtq bool
}{
{
name: "default_no_config_no_env",
wantEvtq: true,
},
{
name: "config_disabled_env_unset",
setUserConfig: true,
configDisabled: true,
wantEvtq: false,
},
{
name: "config_enabled_env_disables",
setUserConfig: true,
configDisabled: false,
envVar: []string{"BD_DISABLE_METRICS=1"},
wantEvtq: false,
},
{
name: "config_disabled_env_overrides_to_enabled",
setUserConfig: true,
configDisabled: true,
envVar: []string{"BD_DISABLE_METRICS=0"},
wantEvtq: true,
},
}
for _, tc := range cases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
home, err := testTempDir("bd-metrics-prec-*")
if err != nil {
t.Fatalf("temp home: %v", err)
}
if tc.setUserConfig {
writeUserMetricsDisabled(t, home, tc.configDisabled)
}
runBdInitWithEnv(t, home, tc.envVar)
files := evtqFilesIn(t, home)
if tc.wantEvtq && len(files) == 0 {
t.Errorf("expected an .evtq file, got none")
}
if !tc.wantEvtq && len(files) > 0 {
t.Errorf("expected no .evtq files, got %v", files)
}
})
}
}
func TestInitBootstrapsUserConfigWhenMissing(t *testing.T) {
home, err := testTempDir("bd-bootstrap-fresh-*")
if err != nil {
t.Fatalf("temp home: %v", err)
}
runBdInitWithEnv(t, home, nil)
path := filepath.Join(home, ".config", "bd", "config.yaml")
body, err := os.ReadFile(path)
if err != nil {
t.Fatalf("read %s: %v", path, err)
}
got := string(body)
if !strings.Contains(got, metrics.DefaultEndpoint) {
t.Errorf("user config missing metrics endpoint.\nfile: %q", got)
}
if !strings.Contains(got, "endpoint:") {
t.Errorf("user config missing endpoint key.\nfile: %q", got)
}
}
func TestInitPreservesExistingMetricsValuesAndFillsMissingLeaves(t *testing.T) {
home, err := testTempDir("bd-bootstrap-existing-*")
if err != nil {
t.Fatalf("temp home: %v", err)
}
dir := filepath.Join(home, ".config", "bd")
if err := os.MkdirAll(dir, 0o755); err != nil {
t.Fatalf("mkdir: %v", err)
}
path := filepath.Join(dir, "config.yaml")
original := []byte("metrics:\n endpoint: https://example.invalid/custom\n")
if err := os.WriteFile(path, original, 0o644); err != nil {
t.Fatalf("seed: %v", err)
}
runBdInitWithEnv(t, home, nil)
got, err := os.ReadFile(path)
if err != nil {
t.Fatalf("read: %v", err)
}
body := string(got)
if !strings.Contains(body, "https://example.invalid/custom") {
t.Errorf("user-set endpoint was clobbered.\nfile: %q", body)
}
if !strings.Contains(body, "disabled:") {
t.Errorf("missing metrics.disabled was not filled in.\nfile: %q", body)
}
}
func TestInitMetricsDisabledSuppresses(t *testing.T) {
bd := buildEmbeddedBD(t)
home, err := testTempDir("bd-metrics-disabled-home-*")
if err != nil {
t.Fatalf("temp home: %v", err)
}
repo, err := testTempDir("bd-metrics-disabled-repo-*")
if err != nil {
t.Fatalf("temp repo: %v", err)
}
initGitRepoAt(t, repo)
cmd := exec.Command(bd, "init", "--non-interactive", "--quiet")
cmd.Dir = repo
env := append([]string{}, bdEnv(home)...)
env = append(env, "BD_DISABLE_METRICS=1")
cmd.Env = env
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
t.Fatalf("bd init failed: %v\n%s\n%s", err, stdout.String(), stderr.String())
}
dir := filepath.Join(home, ".beads", "eventsData")
if _, err := os.Stat(dir); err == nil {
entries, _ := os.ReadDir(dir)
var evtqs []string
for _, e := range entries {
if strings.HasSuffix(e.Name(), ".evtq") {
evtqs = append(evtqs, e.Name())
}
}
if len(evtqs) > 0 {
t.Errorf("BD_DISABLE_METRICS=1 still produced .evtq files: %v", evtqs)
}
}
}
// allCommandEvents returns the `command` attribute of every cli_command event
// across all queued .evtq files, so a test can assert the exact emission
// cardinality of a single bd invocation (one user command must record exactly
// one cli_command event).
func allCommandEvents(t *testing.T, home string) []string {
t.Helper()
dir := filepath.Join(home, ".beads", "eventsData")
entries, err := os.ReadDir(dir)
if err != nil {
return nil
}
var cmds []string
for _, e := range entries {
if !strings.HasSuffix(e.Name(), ".evtq") {
continue
}
body, err := os.ReadFile(filepath.Join(dir, e.Name()))
if err != nil {
t.Fatalf("read evtq %s: %v", e.Name(), err)
}
var got metricsEvent
if err := json.Unmarshal(body, &got); err != nil {
t.Fatalf("unmarshal evtq %s: %v\n%s", e.Name(), err, body)
}
for _, ev := range got.Events {
if ev.Name != "cli_command" {
continue
}
for _, a := range ev.Attributes {
if a.Key == "command" {
cmds = append(cmds, a.Value)
}
}
}
}
return cmds
}
func runBdForMetrics(t *testing.T, bd, repo, home string, args ...string) (stdout, stderr string) {
t.Helper()
cmd := exec.Command(bd, args...)
cmd.Dir = repo
cmd.Env = metricsTestEnv(home)
var outBuf, errBuf bytes.Buffer
cmd.Stdout = &outBuf
cmd.Stderr = &errBuf
_ = cmd.Run()
return outBuf.String(), errBuf.String()
}
// TestMetricsTodoAliasEmitsSingleEvent is the double-emit regression for PR
// #4419: bare `bd todo` delegates to the todo-list behavior, but it must record
// exactly one cli_command event ("todo"), not also a phantom "todo-list".
func TestMetricsTodoAliasEmitsSingleEvent(t *testing.T) {
bd := buildEmbeddedBD(t)
home, err := testTempDir("bd-metrics-todo-home-*")
if err != nil {
t.Fatalf("temp home: %v", err)
}
repo, err := testTempDir("bd-metrics-todo-repo-*")
if err != nil {
t.Fatalf("temp repo: %v", err)
}
initGitRepoAt(t, repo)
// A store is needed so `bd todo` (which lists task issues) reaches its RunE.
if _, errOut := runBdForMetrics(t, bd, repo, home, "init", "--non-interactive", "--quiet"); errOut != "" {
// init may print warnings; only fail later if todo produces no event.
_ = errOut
}
// Isolate the next invocation by dropping init's queued event.
if err := os.RemoveAll(filepath.Join(home, ".beads", "eventsData")); err != nil {
t.Fatalf("clear eventsData: %v", err)
}
_, errOut := runBdForMetrics(t, bd, repo, home, "todo")
got := allCommandEvents(t, home)
if len(got) != 1 || got[0] != "todo" {
t.Errorf("bd todo emitted %v, want exactly [todo] (double-emit regression)\nstderr:\n%s", got, errOut)
}
}
// TestMetricsReadyGatedAliasEmitsSingleEvent is the double-emit regression for
// PR #4419: `bd ready --gated` delegates to the gate-ready molecule discovery,
// but it must record exactly one cli_command event ("ready"), not also a phantom
// "mol-ready-gated".
func TestMetricsReadyGatedAliasEmitsSingleEvent(t *testing.T) {
bd := buildEmbeddedBD(t)
home, err := testTempDir("bd-metrics-readygated-home-*")
if err != nil {
t.Fatalf("temp home: %v", err)
}
repo, err := testTempDir("bd-metrics-readygated-repo-*")
if err != nil {
t.Fatalf("temp repo: %v", err)
}
initGitRepoAt(t, repo)
// A store is needed so `bd ready --gated` reaches its discovery body.
if _, errOut := runBdForMetrics(t, bd, repo, home, "init", "--non-interactive", "--quiet"); errOut != "" {
_ = errOut
}
// Isolate the next invocation by dropping init's queued event.
if err := os.RemoveAll(filepath.Join(home, ".beads", "eventsData")); err != nil {
t.Fatalf("clear eventsData: %v", err)
}
_, errOut := runBdForMetrics(t, bd, repo, home, "ready", "--gated")
got := allCommandEvents(t, home)
if len(got) != 1 || got[0] != "ready" {
t.Errorf("bd ready --gated emitted %v, want exactly [ready] (double-emit regression)\nstderr:\n%s", got, errOut)
}
}
// TestMetricsWispAliasEmitsSingleEvent is the double-emit regression for PR
// #4419: bare `bd mol wisp <proto>` delegates to the wisp-create behavior, but it
// must record exactly one cli_command event ("wisp"), not also a phantom
// "wisp-create". The proto does not exist, so the command fails after the event
// is recorded; the event count is what this guards.
func TestMetricsWispAliasEmitsSingleEvent(t *testing.T) {
bd := buildEmbeddedBD(t)
home, err := testTempDir("bd-metrics-wisp-home-*")
if err != nil {
t.Fatalf("temp home: %v", err)
}
repo, err := testTempDir("bd-metrics-wisp-repo-*")
if err != nil {
t.Fatalf("temp repo: %v", err)
}
initGitRepoAt(t, repo)
// A store is needed so `bd mol wisp <proto>` reaches its RunE body.
if _, errOut := runBdForMetrics(t, bd, repo, home, "init", "--non-interactive", "--quiet"); errOut != "" {
_ = errOut
}
// Isolate the next invocation by dropping init's queued event.
if err := os.RemoveAll(filepath.Join(home, ".beads", "eventsData")); err != nil {
t.Fatalf("clear eventsData: %v", err)
}
// A non-existent proto makes the create fail, but the "wisp" event is still
// recorded before delegation, which is exactly what this regression checks.
_, errOut := runBdForMetrics(t, bd, repo, home, "mol", "wisp", "mol-nonexistent-proto")
got := allCommandEvents(t, home)
if len(got) != 1 || got[0] != "wisp" {
t.Errorf("bd mol wisp <proto> emitted %v, want exactly [wisp] (double-emit regression)\nstderr:\n%s", got, errOut)
}
}
// TestMetricsRootVersionFlagSuppressesFirstRunNotice is the fresh-home regression
// for PR #4419: `bd --version` and `bd -V` are version probes (like the `version`
// subcommand) and must not print the one-time consent notice to stderr or mark it
// shown, even on a brand-new home with metrics enabled. A positive control proves
// the harness can still fire the notice for a non-suppressed command, so a missing
// notice below reflects real suppression rather than a dead test.
func TestMetricsRootVersionFlagSuppressesFirstRunNotice(t *testing.T) {
bd := buildEmbeddedBD(t)
const noticeMarker = "anonymous usage metrics"
noticeShown := func(home string) bool {
b, err := os.ReadFile(filepath.Join(home, ".config", "bd", "config.yaml"))
if err != nil {
return false
}
return strings.Contains(string(b), "notice_shown")
}
// Positive control: a non-suppressed command on a fresh home prints the notice
// (the consent notice fires before the no-database exit), so the negative
// assertions below are meaningful.
control, err := testTempDir("bd-metrics-version-control-home-*")
if err != nil {
t.Fatalf("temp home: %v", err)
}
controlRepo, err := testTempDir("bd-metrics-version-control-repo-*")
if err != nil {
t.Fatalf("temp repo: %v", err)
}
if _, errOut := runBdForMetrics(t, bd, controlRepo, control, "list"); !strings.Contains(errOut, noticeMarker) {
t.Fatalf("positive control: `bd list` on a fresh home should print the first-run notice; stderr:\n%s", errOut)
}
for _, flag := range []string{"--version", "-V"} {
flag := flag
t.Run("root "+flag+" suppresses notice", func(t *testing.T) {
home, err := testTempDir("bd-metrics-version-home-*")
if err != nil {
t.Fatalf("temp home: %v", err)
}
repo, err := testTempDir("bd-metrics-version-repo-*")
if err != nil {
t.Fatalf("temp repo: %v", err)
}
stdout, errOut := runBdForMetrics(t, bd, repo, home, flag)
if !strings.Contains(stdout, "bd version") {
t.Errorf("`bd %s` should print version to stdout, got stdout:\n%s\nstderr:\n%s", flag, stdout, errOut)
}
if strings.Contains(errOut, noticeMarker) {
t.Errorf("`bd %s` printed the first-run metrics notice to stderr (must be suppressed):\n%s", flag, errOut)
}
if noticeShown(home) {
t.Errorf("`bd %s` marked metrics.notice_shown in user config (must not for a version probe)", flag)
}
})
}
}
// metrics off must not queue a usage event, honoring the "No usage data will be
// collected or sent" promise even though metrics are still enabled for the
// off invocation itself.
func TestMetricsOffEmitsNoEvent(t *testing.T) {
bd := buildEmbeddedBD(t)
home, err := testTempDir("bd-metrics-off-home-*")
if err != nil {
t.Fatalf("temp home: %v", err)
}
repo, err := testTempDir("bd-metrics-off-repo-*")
if err != nil {
t.Fatalf("temp repo: %v", err)
}
initGitRepoAt(t, repo)
// Establish the metrics-enabled baseline and confirm a normal command emits.
runBdForMetrics(t, bd, repo, home, "init", "--non-interactive", "--quiet")
if got := allCommandEvents(t, home); len(got) == 0 {
t.Fatalf("precondition: metrics-enabled `bd init` produced no event; env may be misconfigured")
}
if err := os.RemoveAll(filepath.Join(home, ".beads", "eventsData")); err != nil {
t.Fatalf("clear eventsData: %v", err)
}
_, errOut := runBdForMetrics(t, bd, repo, home, "metrics", "off")
if got := allCommandEvents(t, home); len(got) != 0 {
t.Errorf("bd metrics off emitted %v, want no events\nstderr:\n%s", got, errOut)
}
}