beads/cmd/bd/promote_embedded_test.go
Julian Knutsen eaed71fbba
fix(http): bd promote over http through issueops.Promote and issues.update.persistence (S21b) (#7554)
* fix(http): bd promote over http through issueops.Promote and issues.update.persistence (S21b)

`bd promote` refused over a connected HTTP workspace: it called the raw
PromoteFromEphemeral, which the http backend stubs, and updateIssue's patch
did not publish persistence. MC's PR-intake formula
(mol-normalized-issue-pr-v1) runs `gc bd promote <id> --reason ...`, so this
blocked the HTTP cutover.

- One library entry, issueops.Promote(ctx, PromoteRoles{Reader, Lifecycle,
  Commenter}, PromoteRequest{Actor, IssueID, Reason, Provenance}): read the
  row, refuse a non-wisp (*NotAWispError, the sentence bd promote always
  printed), move it with ONE Lifecycle.Update whose patch is Persistence =
  persistent (the role's atomic plane move: same id, labels, deps, events and
  comments carried), then write the promotion comment (a comment failure is
  reported, the promotion stands). A no-op move is a lost race and reports
  NotAWispError without a second comment.
- cmd/bd: the direct route runs it over the store's own role accessors, so a
  connected http workspace and a local one take the same entry. The history
  label is "bd: promote <id>" where the store carries caller labels.
- bd serve: IssuePatchBody.persistence (persistent|ephemeral|no_history,
  anything else a 400 naming patch.persistence) on PATCH
  /v0/beads/issues/{id}, behind the new additive behavior token
  issues.update.persistence. wire_revision unchanged. Spec, apigen and the
  wire-shape golden regenerated; the handler only decodes and passes it to
  the role.
- httpclient: Lifecycle.Update sends patch.persistence only to a server
  advertising the token, and otherwise refuses BEFORE the dial with
  *wire.CapabilityError naming it (unwraps to *storage.ErrUnsupported with
  Capability set). The batchApply item still refuses it
  (W-IssuePatch.Persistence now reads "ON issues:batchApply").
- Conformance: RunLifecycleUpdatePersistentPromotesAWisp on dolt, embedded,
  uow and the served http leg; the served
  PersistentPreservesUnversionedClass case is unparked.
- Served e2e TestE2E_PromoteOverHTTP: the mc-promote shape end to end
  (label kept, comment written, one PATCH, re-promote refused), and under
  the MC server mask a pre-dial refusal naming the token with no write.
- Corpus: mc-promote stays pending, relabelled SRV-1 with the reference
  profile's exact refusal (the 1.3.0 server lacks issues.update.persistence).

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

* fix(promote): run the proxied-server route through issueops.Promote (bd-4y73m9)

The adoption review found (major) that the proxied-server route still
promoted through the old engine, IssueUseCase.PromoteWisp in one
transaction with the comment, while the PR's docs said every route runs
issueops.Promote. Converge rather than reword: runPromoteProxiedServer
resolves the id, then runs issueops.Promote over the provider's own
notifying role accessors (proxiedIssueReader, proxiedIssueLifecycle,
proxiedCommenter), the entry the direct route runs. Both routes now share
promoteFailure (the error mapping) and reportPromotion (the comment
warning and the output), so neither contract can drift.

What a script sees change on the proxied-server route:
- the move is the persistence branch of Lifecycle.Update, so the child
  counter moves with the row: a promoted parent's next
  `bd create --parent` mints .3 after .1/.2 instead of failing with
  "issue already exists";
- on_update fires twice, for the move and for the comment, as on the
  direct route (it fired once, for the comment);
- a promotion is two Dolt commits, "bd: promote <id>" and
  "bd: comment <id>" (it was one);
- a comment that cannot be written leaves the promotion standing behind
  a warning, as on every other route, instead of rolling it back;
- a dual-plane id is refused rather than merged, the update role's
  template guard applies, and a lost race reads "<id> is not a wisp
  (already persistent)".

The PromoteWisp exemption in TestRecordingIssueUseCaseCoversItsSurface
stays, with its reason corrected: no route calls PromoteWisp now, and
its DoltStorage twin fires no hook either. The doc comments on the old
engine (PromoteFromEphemeral on DoltStore and the db layer, PromoteWisp)
no longer say bd promote uses it.

CHANGELOG: the promote entry now names what a script can see on the
direct and proxied-server routes: the hook firings, the comment row
`bd comments` lists, the child-counter carry, the two-commit shape, and
a comment failure that warns instead of rolling back.

Tests:
- TestEmbeddedPromoteMovesAWispParent promotes a wisp parent with .1/.2
  children, a dependency and a comment under a counting on_update hook:
  the hook fires twice, the dependency and the children's parent edge
  survive, `bd comments` lists the old comment and the promotion reason,
  and the next child is .3. At the pre-PR base it fails (the hook fired
  0 times).
- The proxied journey pins the two commits, and new subtests pin the
  child counter (the child_counters row, next child .3) and the two hook
  firings. Against the contributor head each fails: one commit, no
  counter row and a failing create, one firing.

Review iteration 2:
- TestEmbeddedPromoteMovesAWispParent gets its row in the embedded cmd
  shard manifest (shard 64 of 100, from the generator's incremental
  --write). Without it //scripts:go_test_sources_test fails both the
  NotStale and the bare-write guard, and the generator's --check fails.
- notifying.go no longer lists the promotion comment as a divergence.
  The direct route now writes that comment through st.Commenter(),
  which HookFiringStore wraps, so an embedded promote fires on_update
  for it (TestEmbeddedPromoteMovesAWispParent counts both firings). The
  list counts four, and the empty-commit note becomes the fifth item.
- batchapplier.go no longer says persistence is absent from both patch
  bodies: the update body publishes it behind issues.update.persistence,
  and only batchApply refuses it (W-IssuePatch.Persistence).
- Three comments still tied promotion to the old engine: the
  conformance fixture's PromoteWisp no longer claims to do what
  `bd promote` does, the CLI differential's promote scenario names
  issueops.Promote, and export_source's wisp-relations note names
  issueops.MoveIssuePersistenceInTx.
- The Promote unit test compares the whole update patch with
  reflect.DeepEqual. The helper it replaces checked 9 of the patch's 22
  members, so a Promote that also set Description passed it.

Review iteration 3:
- A refused persistence move answered 500 `internal` over http and
  emitted request_error, though the caller could fix the request. The
  role now returns two refusals as ErrValidation, so bd serve answers
  400 invalid_argument and the client maps that back to ErrValidation:
  an unversioned row asked for ephemeral or no_history (the role wraps
  types.ErrUnversionedWispMove, a new sentinel NormalizePersistenceMode
  returns for that case alone, with its message unchanged), and a
  demotion of a row with retained snapshots. A row that breaks its own
  persistence invariants still answers 500.
- RunLifecycleUpdateWispModesRefuseAnUnversionedRow pins the refusal on
  every leg (dolt, embedded dolt, the unit of work, served http): both
  wisp modes match ErrValidation and leave the row durable, unversioned
  and off the wisp plane. Without the wrap the dolt leg fails, and the
  served leg fails on "answered 500 internal".
- TestMoveIssuePersistenceInTxRefusesDemotionWithRetainedSnapshots is
  the first test of the snapshot refusal, and fails without its wrap.
  The unversioned unit test now requires both sentinels. The types tests
  pin the sentinel, its unchanged message, and that no row-invariant
  error matches it: checking the unversioned class ahead of the
  invariant checks fails them.
- CHANGELOG: the promote entry counts five script-visible changes, the
  fifth being the lost-race refusal sentence, and says the two refusals
  now answer 400 over bd serve and gain a `validation failed: ` prefix
  locally.
- NotAWispError's doc comment records that a no-history wisp is refused
  too: the gate reads the Ephemeral flag, as bd promote always has, and
  `bd update --persistent` is the move for that row.

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

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-14 05:51:53 +00:00

323 lines
9.9 KiB
Go

//go:build cgo
package main
import (
"encoding/json"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"testing"
"time"
)
// bdPromote runs "bd promote" with the given args and returns stdout.
func bdPromote(t *testing.T, bd, dir string, args ...string) string {
t.Helper()
fullArgs := append([]string{"promote"}, args...)
cmd := exec.Command(bd, fullArgs...)
cmd.Dir = dir
cmd.Env = bdEnv(dir)
stdout, stderr, err := runCommandBuffers(t, cmd)
if err != nil {
t.Fatalf("bd promote %s failed: %v\nstdout:\n%s\nstderr:\n%s", strings.Join(args, " "), err, stdout.String(), stderr.String())
}
return stdout.String()
}
// bdPromoteFail runs "bd promote" expecting failure.
func bdPromoteFail(t *testing.T, bd, dir string, args ...string) string {
t.Helper()
fullArgs := append([]string{"promote"}, args...)
cmd := exec.Command(bd, fullArgs...)
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if err == nil {
t.Fatalf("expected bd promote %s to fail, but succeeded:\n%s", strings.Join(args, " "), out)
}
return string(out)
}
// plantCountingHook installs the workspace hook name as a script that appends
// its issue id ($1) to a log, one line per firing, and returns the log's path.
func plantCountingHook(t *testing.T, beadsDir, name string) string {
t.Helper()
hookLog := filepath.Join(t.TempDir(), name+".log")
hooksDir := filepath.Join(beadsDir, "hooks")
if err := os.MkdirAll(hooksDir, 0o755); err != nil {
t.Fatalf("mkdir hooks: %v", err)
}
script := "#!/bin/sh\necho \"$1\" >> " + shellQuote(hookLog) + "\n"
if err := os.WriteFile(filepath.Join(hooksDir, name), []byte(script), 0o755); err != nil { //nolint:gosec // G306: a hook must be executable to run at all
t.Fatalf("plant %s: %v", name, err)
}
return hookLog
}
// waitForHookLines polls a plantCountingHook log until it names id want times.
// The command has returned by then, and a command waits for the hooks it
// fired before it exits, so a count past want is a hook that fired too often.
func waitForHookLines(t *testing.T, hookLog, id string, want int) {
t.Helper()
deadline := time.Now().Add(15 * time.Second)
for {
data, _ := os.ReadFile(hookLog) // #nosec G304 -- this test's own temp file
got := 0
for _, line := range strings.Fields(string(data)) {
if line == id {
got++
}
}
if got > want {
t.Fatalf("hook fired %d times for %s, want %d; log:\n%s", got, id, want, data)
}
if got == want {
return
}
if time.Now().After(deadline) {
t.Fatalf("hook fired %d times for %s, want %d; log:\n%s", got, id, want, data)
}
time.Sleep(50 * time.Millisecond)
}
}
func TestEmbeddedPromoteCLI(t *testing.T) {
if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" {
t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt integration tests")
}
t.Parallel()
bd := buildEmbeddedBD(t)
dir, beadsDir, _ := bdInit(t, bd, "--prefix", "pr")
t.Run("promote_basic", func(t *testing.T) {
// Create ephemeral issue
issue := bdCreate(t, bd, dir, "CLI promote basic", "--ephemeral")
// Verify it's ephemeral
got := bdShow(t, bd, dir, issue.ID)
if !got.Ephemeral {
t.Skip("issue not ephemeral; cannot test promote")
}
out := bdPromote(t, bd, dir, issue.ID)
if !strings.Contains(out, "Promoted") {
t.Errorf("expected 'Promoted' in output: %s", out)
}
// Verify no longer ephemeral
got = bdShow(t, bd, dir, issue.ID)
if got.Ephemeral {
t.Error("expected non-ephemeral after promote")
}
})
t.Run("promote_with_reason", func(t *testing.T) {
issue := bdCreate(t, bd, dir, "CLI promote reason", "--ephemeral")
out := bdPromote(t, bd, dir, issue.ID, "--reason", "Important enough to persist")
if !strings.Contains(out, "Promoted") {
t.Errorf("expected 'Promoted' in output: %s", out)
}
})
t.Run("promote_with_reason_short", func(t *testing.T) {
issue := bdCreate(t, bd, dir, "CLI promote -r", "--ephemeral")
out := bdPromote(t, bd, dir, issue.ID, "-r", "Short reason")
if !strings.Contains(out, "Promoted") {
t.Errorf("expected 'Promoted' in output: %s", out)
}
})
t.Run("promote_json", func(t *testing.T) {
issue := bdCreate(t, bd, dir, "CLI promote JSON", "--ephemeral")
cmd := exec.Command(bd, "promote", issue.ID, "--json")
cmd.Dir = dir
cmd.Env = bdEnv(dir)
stdout, stderr, err := runCommandBuffers(t, cmd)
if err != nil {
t.Fatalf("bd promote --json failed: %v\nstdout:\n%s\nstderr:\n%s", err, stdout.String(), stderr.String())
}
s := strings.TrimSpace(stdout.String())
start := strings.Index(s, "{")
if start < 0 {
t.Fatalf("no JSON object in output: %s", s)
}
var m map[string]interface{}
if err := json.Unmarshal([]byte(s[start:]), &m); err != nil {
t.Fatalf("parse promote JSON: %v\n%s", err, s)
}
if m["id"] != issue.ID {
t.Errorf("expected id=%s, got %v", issue.ID, m["id"])
}
})
t.Run("promote_preserves_labels", func(t *testing.T) {
issue := bdCreate(t, bd, dir, "Promote labels", "--ephemeral", "--label", "keep-me")
bdPromote(t, bd, dir, issue.ID)
store := openStore(t, beadsDir, "pr")
labels, err := store.GetLabels(t.Context(), issue.ID)
if err != nil {
t.Fatalf("GetLabels: %v", err)
}
found := false
for _, l := range labels {
if l == "keep-me" {
found = true
}
}
if !found {
t.Errorf("expected label 'keep-me' preserved after promote, got %v", labels)
}
})
t.Run("promote_nonexistent_fails", func(t *testing.T) {
bdPromoteFail(t, bd, dir, "pr-nonexistent999")
})
t.Run("promote_already_permanent_fails", func(t *testing.T) {
issue := bdCreate(t, bd, dir, "Already permanent", "--type", "task")
bdPromoteFail(t, bd, dir, issue.ID)
})
t.Run("promote_no_args_fails", func(t *testing.T) {
cmd := exec.Command(bd, "promote")
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if err == nil {
t.Fatalf("expected promote with no args to fail, got: %s", out)
}
})
}
// TestEmbeddedPromoteMovesAWispParent pins what the direct route's
// persistence move carries for a wisp with children, a dependency and a
// comment. The dependency and the comment reach the durable tables, the
// children keep their parent, and the child counter carries, so the next
// `bd create --parent` mints .3 rather than colliding with the wisp child .1.
// `bd comments` lists the promotion reason, and on_update fires twice: once
// for the move and once for the promotion comment.
func TestEmbeddedPromoteMovesAWispParent(t *testing.T) {
if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" {
t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt integration tests")
}
t.Parallel()
bd := buildEmbeddedBD(t)
dir, beadsDir, _ := bdInit(t, bd, "--prefix", "pw")
target := bdCreate(t, bd, dir, "Promote dep target", "--type", "task")
parent := bdCreate(t, bd, dir, "Promoted wisp parent", "--ephemeral")
var children []string
for i := 1; i <= 2; i++ {
child := bdCreate(t, bd, dir, fmt.Sprintf("Wisp child %d", i), "--ephemeral", "--parent", parent.ID)
if want := fmt.Sprintf("%s.%d", parent.ID, i); child.ID != want {
t.Fatalf("wisp child %d is %s, want %s", i, child.ID, want)
}
children = append(children, child.ID)
}
bdDepAdd(t, bd, dir, parent.ID, target.ID)
bdComment(t, bd, dir, parent.ID, "pre-promote note")
// Planted after the setup, so every line it logs is the promote's.
hookLog := plantCountingHook(t, beadsDir, "on_update")
bdPromote(t, bd, dir, parent.ID, "--reason", "worth keeping")
waitForHookLines(t, hookLog, parent.ID, 2)
if got := bdShow(t, bd, dir, parent.ID); got.Ephemeral {
t.Errorf("%s still ephemeral after promote", parent.ID)
}
assertDepExists(t, beadsDir, "pw", parent.ID, target.ID)
for _, id := range children {
var shown struct {
Parent *string `json:"parent"`
}
if err := json.Unmarshal(parseShowJSON(t, bdShowJSON(t, bd, dir, id)), &shown); err != nil {
t.Fatalf("decode bd show %s: %v", id, err)
}
if shown.Parent == nil || *shown.Parent != parent.ID {
t.Errorf("%s parent after promote = %v, want %s", id, shown.Parent, parent.ID)
}
}
comments := bdComments(t, bd, dir, parent.ID)
for _, text := range []string{"pre-promote note", "Promoted from Level 0: worth keeping"} {
if !strings.Contains(comments, text) {
t.Errorf("bd comments %s does not list %q:\n%s", parent.ID, text, comments)
}
}
if next := bdCreate(t, bd, dir, "Child after promote", "--parent", parent.ID); next.ID != parent.ID+".3" {
t.Errorf("next child of the promoted parent is %s, want %s", next.ID, parent.ID+".3")
}
}
// TestEmbeddedPromoteCLIConcurrent exercises promote concurrently.
func TestEmbeddedPromoteCLIConcurrent(t *testing.T) {
if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" {
t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt integration tests")
}
t.Parallel()
bd := buildEmbeddedBD(t)
dir, _, _ := bdInit(t, bd, "--prefix", "px")
const numWorkers = 6
// Pre-create ephemeral issues
var issueIDs []string
for i := 0; i < numWorkers; i++ {
issue := bdCreate(t, bd, dir, fmt.Sprintf("concurrent-promote-%d", i), "--ephemeral")
issueIDs = append(issueIDs, issue.ID)
}
type workerResult struct {
worker int
out string
err error
}
results := make([]workerResult, numWorkers)
var wg sync.WaitGroup
wg.Add(numWorkers)
for w := 0; w < numWorkers; w++ {
go func(worker int) {
defer wg.Done()
r := workerResult{worker: worker}
cmd := exec.Command(bd, "promote", issueIDs[worker])
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
r.out = string(out)
if err != nil {
r.err = fmt.Errorf("promote %s: %v", issueIDs[worker], err)
}
results[worker] = r
}(w)
}
wg.Wait()
// The driver handles concurrency internally — all workers should succeed.
for _, r := range results {
if r.err != nil {
t.Errorf("worker %d failed: %v", r.worker, r.err)
}
}
// Verify all issues are permanent.
for i, id := range issueIDs {
got := bdShow(t, bd, dir, id)
if got.Ephemeral {
t.Errorf("expected %s (worker %d) to be permanent after promote", id, i)
}
}
}