beads/cmd/bd/protocol/exit_codes_init_test.go
Steve Yegge 45b62e47b5
test(protocol): pin §E3 exit 11 with a PTY harness (wy-vh5y8) (#4762)
* test(protocol): pin §E3 exit 11 with a PTY harness (wy-vh5y8)

Exit 11 (ExitLocalExistsRefused) is returned only from the interactive
typed-confirmation abort in `bd init --reinit-local` (cmd/bd/init.go, behind
term.IsTerminal on stdin); the non-interactive branch of the same guard returns
12, so no piped-stdin subprocess could produce 11. WP4 (wy-ge2h8) therefore
left TestProtocol_ExitCode11_LocalExistsRefused skipped.

Un-skip it with a PTY-backed run: allocate a pty (creack/pty, a test-only dep
already in go.sum), drive `bd init --reinit-local` against a workspace seeded
with one issue, answer the "Type 'destroy N issues' to confirm:" prompt with
the WRONG text, and assert exit 11 — distinct from the 12 a bad --destroy-token
yields. Also asserts the abort is a no-op (the seeded issue survives). Windows
skips (no unix PTY).

creack/pty is imported only from _test.go, so it is a test-only dependency; no
new non-test dependency enters go.mod.

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

* chore(build): register github.com/creack/pty with Bazel and Nix

The exit-11 PTY test imports github.com/creack/pty, a new direct
require in go.mod. The base's generated build manifests predate it:

- Bazel (`make bazel-sync`): //cmd/bd/protocol:protocol_test failed
  "missing strict dependencies" on the import, and go_deps reported
  com_github_creack_pty missing from use_repo. The sync adds it to
  MODULE.bazel's use_repo and adds @com_github_creack_pty//:pty to
  protocol_test's deps. protocol_race_off, protocol_docker_test and
  protocol_dolt_test all wrap protocol_test, so that one deps line
  covers them. Running the same sync on the base changes nothing.
- Nix: default.nix sets proxyVendor = true, so vendorHash covers the
  module download set of go.mod's requires, and the new require
  changes it. Regenerated via the nixos/nix container: the go-modules
  fixed-output derivation now builds with no hash mismatch, and the
  unwrapped package compiles cmd/bd.

Agent-Signature: claude-code-claude-opus-5-5-max on behalf of Gas City Adopt-PR
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(protocol): fix exit-code header, name a skipped reinit prompt

Review fixups, adopt-pr review loop iteration 1 (synthesis approve,
scorecard 981/1000, no blocker or major findings).

Taken:
- Header (Finding 2): it said all three refusal codes (10/11/12) refuse
  before bd opens any store. That stopped being true once exit 11 ran:
  its typed confirmation fires only after --reinit-local counts the
  issues in a seeded workspace, so that test pays for a full store, as
  130 does. The header now says 10 and 12 are store-free and 11 and 130
  are not.
- Prompt-skip diagnostic (Finding 3): runInitReinitPreflight skips the
  confirmation, with no warning, when countExistingIssues errors (it
  has its own 5s timeout) or counts zero. The review expected that skip
  to exit 0 in the early-exit arm. A mutation run with the seed issue
  dropped shows it does not: the reinit goes on and blocks at init's
  next TTY prompt, "Change role? [y/N]:", so the test fails at the
  prompt deadline. The deadline arm now names the skip when the output
  lacks the "Re-initializing will destroy" warning. Failure path only.

Declined:
- Shared prompt harness (Finding 1): extracting it rewrites the
  base-owned 130 test (pipes, SIGINT, t.Parallel) into a new helper
  after review. The synthesis allows accepting the duplication in the
  reply to the reviewer instead.
- Redundant "!= 12" check (Finding 4): it mirrors the 12 test's own
  "codes must stay distinct" check, so the tests keep one idiom.

Validation: gofmt and go vet are clean. TestProtocol_ExitCode10/11/12/130
pass against the dolthub/dolt-sql-server:2.2.0 test container with
BEADS_PROTOCOL_REQUIRE_DOLT=1, so exit 11 ran rather than skipped. The
seed-dropped mutant fails with the new message.

Agent-Signature: claude-code-claude-opus-5-5-max on behalf of Gas City Adopt-PR
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
Co-authored-by: Eddie the Engineer <adopt-pr@gascity.com>
2026-10-06 23:06:06 +00:00

418 lines
14 KiB
Go

// exit_codes_init_test.go — protocol v0 §E3 (documented-stable exit codes).
//
// Beyond 0 (success) and 1 (general failure), v0 freezes four exit codes as
// scriptable API:
//
// 10 init-safety: remote divergence refused
// 11 init-safety: local data exists, refused
// 12 init-safety: destroy-token missing or wrong
// 130 interactive prompt canceled (SIGINT)
//
// The clause is about the NUMBERS, not about bd's Go constants, so these tests
// assert the literals: a conforming implementation with a different internal
// spelling still has to exit 10 when it refuses a divergent init. New stable
// codes require a spec revision.
//
// The refusal codes 10 and 12 are cheap to drive: `bd init` refuses before it
// opens any store, so those tests need only a built binary and a temp git repo.
// 11 and 130 each pay for a full store before the prompt they exercise: 11's
// typed confirmation fires only once --reinit-local has counted the issues in a
// seeded workspace, and 130's cancellation is only reachable on the success
// path, after the store is created.
package protocol
import (
"bytes"
"errors"
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
"strings"
"sync"
"testing"
"time"
"github.com/creack/pty"
)
// Literal exit codes frozen by §E3. Deliberately NOT bd's Go constants: the
// corpus pins the wire contract an agent scripts against.
const (
exitRemoteDivergenceRefused = 10
exitLocalExistsRefused = 11
exitDestroyTokenMissing = 12
exitPromptCanceled = 130
)
// initEnv is a clean environment for a bd init subprocess. It strips any
// ambient beads workspace so the init under test resolves only from the
// process's working directory, and pins the test Dolt server when one is
// running so a successful init can never reach a real database.
//
// home MUST NOT be the repo the init runs in: bd keeps a user-level ~/.beads
// alongside the workspace-level one, so pointing HOME at the repo would make
// "did the refusal create .beads/?" unanswerable.
func initEnv(home string, extra ...string) []string {
env := []string{
"PATH=" + os.Getenv("PATH"),
"HOME=" + home,
"XDG_CONFIG_HOME=" + filepath.Join(home, "xdg-config"),
"GIT_CONFIG_NOSYSTEM=1",
"BEADS_TEST_MODE=1",
// See the note in helpers_test.go env(): the detached metrics child
// writes under HOME after bd exits and races t.TempDir() cleanup.
"BD_DISABLE_METRICS=1",
"BEADS_DB=",
"BEADS_DIR=",
}
if testDoltPort > 0 {
env = append(env, "BEADS_DOLT_PORT="+strconv.Itoa(testDoltPort))
}
if v := os.Getenv("TMPDIR"); v != "" {
env = append(env, "TMPDIR="+v)
}
return append(env, extra...)
}
func gitIn(t *testing.T, dir string, args ...string) {
t.Helper()
cmd := exec.Command("git", args...)
if dir != "" {
cmd.Dir = dir
}
cmd.Env = initEnv(t.TempDir())
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("git %s: %v\n%s", strings.Join(args, " "), err, out)
}
}
// cloneOfRemoteWithDoltData returns a fresh, un-initialized git repo whose
// origin advertises refs/dolt/data — the state that makes `bd init` refuse a
// local-source init rather than orphan the remote's history.
func cloneOfRemoteWithDoltData(t *testing.T) string {
t.Helper()
bare := filepath.Join(t.TempDir(), "bare.git")
gitIn(t, "", "init", "--bare", bare)
source := t.TempDir()
gitIn(t, source, "init", "-b", "main")
gitIn(t, source, "config", "user.email", "test@protocol.test")
gitIn(t, source, "config", "user.name", "protocol-test")
gitIn(t, source, "commit", "--allow-empty", "-m", "init")
gitIn(t, source, "remote", "add", "origin", bare)
gitIn(t, source, "push", "origin", "main")
gitIn(t, source, "push", "origin", "HEAD:refs/dolt/data")
clone := t.TempDir()
gitIn(t, clone, "init", "-b", "main")
gitIn(t, clone, "remote", "add", "origin", bare)
gitIn(t, clone, "config", "core.hooksPath", ".git/hooks")
return clone
}
// runInitExpectRefusal runs bd init in dir and returns combined output + exit
// code, requiring a non-zero exit.
func runInitExpectRefusal(t *testing.T, dir string, args ...string) (string, int) {
t.Helper()
cmd := exec.Command(buildBD(t), append([]string{"init"}, args...)...)
cmd.Dir = dir
cmd.Env = initEnv(t.TempDir())
out, err := cmd.CombinedOutput()
if err == nil {
t.Fatalf("bd init %s: expected refusal, got success\n%s", strings.Join(args, " "), out)
}
var exitErr *exec.ExitError
if !errors.As(err, &exitErr) {
t.Fatalf("bd init %s: unexpected error type: %v\n%s", strings.Join(args, " "), err, out)
}
return string(out), exitErr.ExitCode()
}
// TestProtocol_ExitCode10_RemoteDivergenceRefused pins §E3's exit 10: a
// local-source init (--force) against an origin that already advertises
// refs/dolt/data must refuse with 10, not with the generic failure code 1 — a
// CI script branches on the difference between "you would have orphaned remote
// history" and "something went wrong".
func TestProtocol_ExitCode10_RemoteDivergenceRefused(t *testing.T) {
t.Parallel()
clone := cloneOfRemoteWithDoltData(t)
out, code := runInitExpectRefusal(t, clone,
"--force", "--prefix", "pe3", "--quiet", "--non-interactive", "--skip-hooks", "--skip-agents")
if code != exitRemoteDivergenceRefused {
t.Errorf("exit code = %d, want %d (§E3 remote-divergence refusal)\n%s", code, exitRemoteDivergenceRefused, out)
}
// A refusal must not half-init the workspace.
if _, err := os.Stat(filepath.Join(clone, ".beads")); err == nil {
t.Errorf("refusal created .beads/ — refusal must be a no-op")
}
}
// TestProtocol_ExitCode12_DestroyTokenMissing pins §E3's exit 12: authorizing
// the cross-boundary operation (--discard-remote) without the destroy token, in
// non-interactive mode, is its own refusal class — distinct from 10, so an
// agent can tell "you must authorize this" from "you must supply the token".
func TestProtocol_ExitCode12_DestroyTokenMissing(t *testing.T) {
t.Parallel()
clone := cloneOfRemoteWithDoltData(t)
out, code := runInitExpectRefusal(t, clone,
"--discard-remote", "--prefix", "pe3", "--quiet", "--non-interactive", "--skip-hooks", "--skip-agents")
if code != exitDestroyTokenMissing {
t.Errorf("exit code = %d, want %d (§E3 destroy-token refusal)\n%s", code, exitDestroyTokenMissing, out)
}
if code == exitRemoteDivergenceRefused {
t.Errorf("destroy-token refusal collapsed into the divergence code — §E3 codes must stay distinct")
}
}
// TestProtocol_ExitCode11_LocalExistsRefused pins §E3's exit 11: exit 11 is
// returned only from the INTERACTIVE typed-confirmation abort in
// `bd init --reinit-local` (cmd/bd/init.go, guarded by term.IsTerminal on
// stdin). The non-interactive branch of the same guard returns 12 instead, so
// no piped-stdin subprocess can produce 11 — the prompt only fires on a real
// terminal. This test allocates a PTY (creack/pty, a test-only dependency),
// drives `bd init --reinit-local` on it against a workspace with issues, answers
// the "Type 'destroy N issues' to confirm:" prompt with the WRONG text, and
// asserts the abort exits 11 — distinct from the 12 an agent supplying a bad
// --destroy-token would get, so a script can tell "the operator declined at the
// prompt" from "the token was wrong".
func TestProtocol_ExitCode11_LocalExistsRefused(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("no unix PTY on Windows; the exit-11 interactive prompt is unreachable here")
}
// Deliberately NOT t.Parallel(): this test drives a PTY with a generous
// prompt deadline and a reader goroutine. newWorkspace already gives it an
// isolated per-test Dolt database, so serializing it costs nothing but keeps
// the PTY interaction free of contention noise.
w := newWorkspace(t)
// The exit-11 prompt only fires when --reinit-local has data to destroy:
// countExistingIssues must return > 0, so seed one issue.
w.create("--title", "seed issue for exit-11 refusal", "--type", "task")
cmd := exec.Command(w.bd, "init", "--reinit-local")
cmd.Dir = w.dir
// CI= / BD_NON_INTERACTIVE=0 mirror the 130 test: strip any ambient signal
// that would push bd onto a non-interactive path. The exit-11 branch keys on
// term.IsTerminal(stdin) directly, which the PTY below already satisfies;
// these just guard against an earlier non-interactive short-circuit.
cmd.Env = append(w.env(), "BD_NON_INTERACTIVE=0", "CI=")
ptmx, err := pty.Start(cmd)
if err != nil {
t.Fatalf("pty.Start bd init --reinit-local: %v", err)
}
defer func() { _ = ptmx.Close() }()
var (
mu sync.Mutex
output bytes.Buffer
promptSeen = make(chan struct{})
once sync.Once
)
// Drain the PTY continuously: the child writes the warning and prompt to the
// same terminal, and an unread PTY can block the writer. Signal promptSeen
// once the confirmation prompt appears.
go func() {
buf := make([]byte, 4096)
for {
n, readErr := ptmx.Read(buf)
if n > 0 {
mu.Lock()
output.Write(buf[:n])
if strings.Contains(output.String(), "to confirm:") {
once.Do(func() { close(promptSeen) })
}
mu.Unlock()
}
if readErr != nil {
return
}
}
}()
waitCh := make(chan error, 1)
go func() { waitCh <- cmd.Wait() }()
got := func() string {
mu.Lock()
defer mu.Unlock()
return output.String()
}
// Like the 130 test, the deadline covers a whole store connection, not just
// process startup: reaching the prompt means counting the existing issues,
// which opens the workspace's Dolt store — tens of seconds on a loaded box.
const promptDeadline = 120 * time.Second
select {
case <-promptSeen:
case err := <-waitCh:
t.Fatalf("bd init --reinit-local exited before prompting: %v\n%s", err, got())
case <-time.After(promptDeadline):
_ = cmd.Process.Kill()
<-waitCh
out := got()
if !strings.Contains(out, "Re-initializing will destroy") {
// runInitReinitPreflight skips the confirmation, without a warning,
// when countExistingIssues errors (it has its own 5s timeout) or
// counts zero. The reinit then goes on and blocks at init's next
// TTY prompt (e.g. "Change role? [y/N]:"), which lands here.
t.Fatalf("timed out waiting for the destroy-confirmation prompt: the reinit preflight never warned, so it skipped the confirmation (its issue count failed, e.g. the 5s store timeout lapsed, or found none)\n%s", out)
}
t.Fatalf("timed out waiting for the destroy-confirmation prompt\n%s", out)
}
// Answer with text that is NOT "destroy N issues": the typed-confirmation
// mismatch is exactly what returns exit 11.
if _, err := ptmx.Write([]byte("definitely not the confirmation\n")); err != nil {
t.Fatalf("write wrong confirmation to PTY: %v", err)
}
var waitErr error
select {
case waitErr = <-waitCh:
case <-time.After(30 * time.Second):
_ = cmd.Process.Kill()
<-waitCh
t.Fatalf("bd did not exit after the wrong confirmation\n%s", got())
}
var exitErr *exec.ExitError
if !errors.As(waitErr, &exitErr) {
t.Fatalf("expected a non-zero exit after wrong confirmation, got %v\n%s", waitErr, got())
}
if code := exitErr.ExitCode(); code != exitLocalExistsRefused {
t.Errorf("exit code = %d, want %d (§E3 local-exists refusal)\n%s", code, exitLocalExistsRefused, got())
}
if code := exitErr.ExitCode(); code == exitDestroyTokenMissing {
t.Errorf("local-exists refusal collapsed into the destroy-token code — §E3 codes must stay distinct")
}
// The abort must be a no-op: the seeded issue must survive.
if out := w.run("list", "--json"); !strings.Contains(out, "seed issue for exit-11 refusal") {
t.Errorf("aborted reinit destroyed data — seeded issue missing:\n%s", out)
}
}
// TestProtocol_ExitCode130_PromptCanceled pins §E3's exit 130: SIGINT at an
// interactive prompt exits 130 (the shell convention, 128+SIGINT), not 1. An
// agent supervising a bd subprocess uses this to tell "the operator canceled"
// from "the command failed".
func TestProtocol_ExitCode130_PromptCanceled(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("no SIGINT on Windows")
}
t.Parallel()
dir := t.TempDir()
gitIn(t, dir, "init", "-b", "main")
stdinR, stdinW, err := os.Pipe()
if err != nil {
t.Fatalf("stdin pipe: %v", err)
}
defer func() { _ = stdinW.Close() }()
outR, outW, err := os.Pipe()
if err != nil {
t.Fatalf("stdout pipe: %v", err)
}
defer func() { _ = outR.Close() }()
cmd := exec.Command(buildBD(t), "init", "--prefix", "pe3", "--contributor")
cmd.Dir = dir
cmd.Stdin = stdinR
cmd.Stdout = outW
cmd.Stderr = outW
// BD_NON_INTERACTIVE=0 keeps the prompt alive in a pipe-driven subprocess;
// without it bd would take the non-interactive path and never prompt.
cmd.Env = initEnv(t.TempDir(), "BD_NON_INTERACTIVE=0", "CI=")
if err := cmd.Start(); err != nil {
t.Fatalf("start bd init: %v", err)
}
_ = stdinR.Close()
_ = outW.Close()
var (
mu sync.Mutex
output bytes.Buffer
promptSeen = make(chan struct{})
once sync.Once
readerDone = make(chan struct{})
)
prompts := [][]byte{
[]byte("Continue with contributor setup? [y/N]: "),
[]byte("Continue anyway? [y/N]: "),
}
go func() {
defer close(readerDone)
buf := make([]byte, 1024)
for {
n, err := outR.Read(buf)
if n > 0 {
mu.Lock()
output.Write(buf[:n])
for _, p := range prompts {
if bytes.Contains(output.Bytes(), p) {
once.Do(func() { close(promptSeen) })
break
}
}
mu.Unlock()
}
if err != nil {
return
}
}
}()
waitCh := make(chan error, 1)
go func() { waitCh <- cmd.Wait() }()
got := func() string {
mu.Lock()
defer mu.Unlock()
return output.String()
}
// The deadline has to cover a WHOLE init, not just process startup. Unlike
// the refusal tests above, this one takes the success path: bd builds the
// store — a real Dolt database, since initEnv pins the suite's server —
// and only then does the contributor wizard prompt. That is tens of seconds
// on a loaded machine, so a short deadline fires mid-init and reports a
// missing prompt when the prompt was merely still coming.
const promptDeadline = 120 * time.Second
select {
case <-promptSeen:
if err := cmd.Process.Signal(os.Interrupt); err != nil {
t.Fatalf("send SIGINT: %v", err)
}
case err := <-waitCh:
t.Fatalf("bd init exited before prompting: %v\n%s", err, got())
case <-time.After(promptDeadline):
_ = cmd.Process.Kill()
<-waitCh
t.Fatalf("timed out waiting for the prompt\n%s", got())
}
err = <-waitCh
select {
case <-readerDone:
case <-time.After(2 * time.Second):
}
var exitErr *exec.ExitError
if !errors.As(err, &exitErr) {
t.Fatalf("canceled init: expected a non-zero exit, got %v\n%s", err, got())
}
if code := exitErr.ExitCode(); code != exitPromptCanceled {
t.Errorf("exit code = %d, want %d (§E3 prompt cancel)\n%s", code, exitPromptCanceled, got())
}
}